LCOV - code coverage report
Current view: top level - gcc/cp - module.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 94.3 % 11089 10452
Test Date: 2026-04-20 14:57:17 Functions: 97.9 % 475 465
Legend: Lines:     hit not hit

            Line data    Source code
       1              : /* C++ modules.  Experimental!
       2              :    Copyright (C) 2017-2026 Free Software Foundation, Inc.
       3              :    Written by Nathan Sidwell <nathan@acm.org> while at FaceBook
       4              : 
       5              :    This file is part of GCC.
       6              : 
       7              :    GCC is free software; you can redistribute it and/or modify it
       8              :    under the terms of the GNU General Public License as published by
       9              :    the Free Software Foundation; either version 3, or (at your option)
      10              :    any later version.
      11              : 
      12              :    GCC is distributed in the hope that it will be useful, but
      13              :    WITHOUT ANY WARRANTY; without even the implied warranty of
      14              :    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      15              :    General Public License for more details.
      16              : 
      17              : You should have received a copy of the GNU General Public License
      18              : along with GCC; see the file COPYING3.  If not see
      19              : <http://www.gnu.org/licenses/>.  */
      20              : 
      21              : /* Comments in this file have a non-negligible chance of being wrong
      22              :    or at least inaccurate.  Due to (a) my misunderstanding, (b)
      23              :    ambiguities that I have interpretted differently to original intent
      24              :    (c) changes in the specification, (d) my poor wording, (e) source
      25              :    changes.  */
      26              : 
      27              : /* (Incomplete) Design Notes
      28              : 
      29              :    A hash table contains all module names.  Imported modules are
      30              :    present in a modules array, which by construction places an
      31              :    import's dependencies before the import itself.  The single
      32              :    exception is the current TU, which always occupies slot zero (even
      33              :    when it is not a module).
      34              : 
      35              :    Imported decls occupy an entity_ary, an array of binding_slots, indexed
      36              :    by importing module and index within that module.  A flat index is
      37              :    used, as each module reserves a contiguous range of indices.
      38              :    Initially each slot indicates the CMI section containing the
      39              :    streamed decl.  When the decl is imported it will point to the decl
      40              :    itself.
      41              : 
      42              :    Additionally each imported decl is mapped in the entity_map via its
      43              :    DECL_UID to the flat index in the entity_ary.  Thus we can locate
      44              :    the index for any imported decl by using this map and then
      45              :    de-flattening the index via a binary seach of the module vector.
      46              :    Cross-module references are by (remapped) module number and
      47              :    module-local index.
      48              : 
      49              :    Each importable DECL contains several flags.  The simple set are
      50              :    DECL_MODULE_EXPORT_P, DECL_MODULE_PURVIEW_P, DECL_MODULE_ATTACH_P
      51              :    and DECL_MODULE_IMPORT_P.  The first indicates whether it is
      52              :    exported, the second whether it is in module or header-unit
      53              :    purview.  The third indicates it is attached to the named module in
      54              :    whose purview it resides and the fourth indicates whether it was an
      55              :    import into this TU or not.  DECL_MODULE_ATTACH_P will be false for
      56              :    all decls in a header-unit, and for those in a named module inside
      57              :    a linkage declaration.
      58              : 
      59              :    The more detailed flags are DECL_MODULE_PARTITION_P,
      60              :    DECL_MODULE_ENTITY_P.  The first is set in a primary interface unit
      61              :    on decls that were read from module partitions (these will have
      62              :    DECL_MODULE_IMPORT_P set too).  Such decls will be streamed out to
      63              :    the primary's CMI.  DECL_MODULE_ENTITY_P is set when an entity is
      64              :    imported, even if it matched a non-imported entity.  Such a decl
      65              :    will not have DECL_MODULE_IMPORT_P set, even though it has an entry
      66              :    in the entity map and array.
      67              : 
      68              :    Header units are module-like.
      69              : 
      70              :    For namespace-scope lookup, the decls for a particular module are
      71              :    held located in a sparse array hanging off the binding of the name.
      72              :    This is partitioned into two: a few fixed slots at the start
      73              :    followed by the sparse slots afterwards.  By construction we only
      74              :    need to append new slots to the end -- there is never a need to
      75              :    insert in the middle.  The fixed slots are MODULE_SLOT_CURRENT for
      76              :    the current TU (regardless of whether it is a module or not),
      77              :    MODULE_SLOT_GLOBAL and MODULE_SLOT_PARTITION.  These latter two
      78              :    slots are used for merging entities across the global module and
      79              :    module partitions respectively.  MODULE_SLOT_PARTITION is only
      80              :    present in a module.  Neither of those two slots is searched during
      81              :    name lookup -- they are internal use only.  This vector is created
      82              :    lazily once we require it, if there is only a declaration from the
      83              :    current TU, a regular binding is present.  It is converted on
      84              :    demand.
      85              : 
      86              :    OPTIMIZATION: Outside of the current TU, we only need ADL to work.
      87              :    We could optimize regular lookup for the current TU by glomming all
      88              :    the visible decls on its slot.  Perhaps wait until design is a
      89              :    little more settled though.
      90              : 
      91              :    There is only one instance of each extern-linkage namespace.  It
      92              :    appears in every module slot that makes it visible.  It also
      93              :    appears in MODULE_SLOT_GLOBAL.  (It is an ODR violation if they
      94              :    collide with some other global module entity.)  We also have an
      95              :    optimization that shares the slot for adjacent modules that declare
      96              :    the same such namespace.
      97              : 
      98              :    A module interface compilation produces a Compiled Module Interface
      99              :    (CMI).  The format used is Encapsulated Lazy Records Of Numbered
     100              :    Declarations, which is essentially ELF's section encapsulation. (As
     101              :    all good nerds are aware, Elrond is half Elf.)  Some sections are
     102              :    named, and contain information about the module as a whole (indices
     103              :    etc), and other sections are referenced by number.  Although I
     104              :    don't defend against actively hostile CMIs, there is some
     105              :    checksumming involved to verify data integrity.  When dumping out
     106              :    an interface, we generate a graph of all the
     107              :    independently-redeclarable DECLS that are needed, and the decls
     108              :    they reference.  From that we determine the strongly connected
     109              :    components (SCC) within this TU.  Each SCC is dumped to a separate
     110              :    numbered section of the CMI.  We generate a binding table section,
     111              :    mapping each namespace&name to a defining section.  This allows
     112              :    lazy loading.
     113              : 
     114              :    Lazy loading employs mmap to map a read-only image of the CMI.
     115              :    It thus only occupies address space and is paged in on demand,
     116              :    backed by the CMI file itself.  If mmap is unavailable, regular
     117              :    FILEIO is used.  Also, there's a bespoke ELF reader/writer here,
     118              :    which implements just the section table and sections (including
     119              :    string sections) of a 32-bit ELF in host byte-order.  You can of
     120              :    course inspect it with readelf.  I figured 32-bit is sufficient,
     121              :    for a single module.  I detect running out of section numbers, but
     122              :    do not implement the ELF overflow mechanism.  At least you'll get
     123              :    an error if that happens.
     124              : 
     125              :    We do not separate declarations and definitions.  My guess is that
     126              :    if you refer to the declaration, you'll also need the definition
     127              :    (template body, inline function, class definition etc).  But this
     128              :    does mean we can get larger SCCs than if we separated them.  It is
     129              :    unclear whether this is a win or not.
     130              : 
     131              :    Notice that we embed section indices into the contents of other
     132              :    sections.  Thus random manipulation of the CMI file by ELF tools
     133              :    may well break it.  The kosher way would probably be to introduce
     134              :    indirection via section symbols, but that would require defining a
     135              :    relocation type.
     136              : 
     137              :    Notice that lazy loading of one module's decls can cause lazy
     138              :    loading of other decls in the same or another module.  Clearly we
     139              :    want to avoid loops.  In a correct program there can be no loops in
     140              :    the module dependency graph, and the above-mentioned SCC algorithm
     141              :    places all intra-module circular dependencies in the same SCC.  It
     142              :    also orders the SCCs wrt each other, so dependent SCCs come first.
     143              :    As we load dependent modules first, we know there can be no
     144              :    reference to a higher-numbered module, and because we write out
     145              :    dependent SCCs first, likewise for SCCs within the module.  This
     146              :    allows us to immediately detect broken references.  When loading,
     147              :    we must ensure the rest of the compiler doesn't cause some
     148              :    unconnected load to occur (for instance, instantiate a template).
     149              : 
     150              : Classes used:
     151              : 
     152              :    dumper - logger
     153              : 
     154              :    data - buffer
     155              : 
     156              :    bytes_in : data - scalar reader
     157              :    bytes_out : data - scalar writer
     158              : 
     159              :    bytes_in::bits_in - bit stream reader
     160              :    bytes_out::bits_out - bit stream writer
     161              : 
     162              :    elf - ELROND format
     163              :    elf_in : elf - ELROND reader
     164              :    elf_out : elf - ELROND writer
     165              : 
     166              :    trees_in : bytes_in - tree reader
     167              :    trees_out : bytes_out - tree writer
     168              : 
     169              :    depset - dependency set
     170              :    depset::hash - hash table of depsets
     171              :    depset::tarjan - SCC determinator
     172              : 
     173              :    uidset<T> - set T's related to a UID
     174              :    uidset<T>::hash hash table of uidset<T>
     175              : 
     176              :    loc_spans - location map data
     177              : 
     178              :    module_state - module object
     179              : 
     180              :    slurping - data needed during loading
     181              : 
     182              :    macro_import - imported macro data
     183              :    macro_export - exported macro data
     184              : 
     185              :    The ELROND objects use mmap, for both reading and writing.  If mmap
     186              :    is unavailable, fileno IO is used to read and write blocks of data.
     187              : 
     188              :    The mapper object uses fileno IO to communicate with the server or
     189              :    program.   */
     190              : 
     191              : /* In expermental (trunk) sources, MODULE_VERSION is a #define passed
     192              :    in from the Makefile.  It records the modification date of the
     193              :    source directory -- that's the only way to stay sane.  In release
     194              :    sources, we (plan to) use the compiler's major.minor versioning.
     195              :    While the format might not change between at minor versions, it
     196              :    seems simplest to tie the two together.  There's no concept of
     197              :    inter-version compatibility.  */
     198              : #define IS_EXPERIMENTAL(V) ((V) >= (1U << 20))
     199              : #define MODULE_MAJOR(V) ((V) / 10000)
     200              : #define MODULE_MINOR(V) ((V) % 10000)
     201              : #define EXPERIMENT(A,B) (IS_EXPERIMENTAL (MODULE_VERSION) ? (A) : (B))
     202              : #ifndef MODULE_VERSION
     203              : #include "bversion.h"
     204              : #define MODULE_VERSION (BUILDING_GCC_MAJOR * 10000U + BUILDING_GCC_MINOR)
     205              : #elif !IS_EXPERIMENTAL (MODULE_VERSION)
     206              : #error "This is not the version I was looking for."
     207              : #endif
     208              : 
     209              : #define _DEFAULT_SOURCE 1 /* To get TZ field of struct tm, if available.  */
     210              : #include "config.h"
     211              : #define INCLUDE_STRING
     212              : #define INCLUDE_VECTOR
     213              : #include "system.h"
     214              : #include "coretypes.h"
     215              : #include "cp-tree.h"
     216              : #include "timevar.h"
     217              : #include "stringpool.h"
     218              : #include "dumpfile.h"
     219              : #include "bitmap.h"
     220              : #include "cgraph.h"
     221              : #include "varasm.h"
     222              : #include "tree-iterator.h"
     223              : #include "cpplib.h"
     224              : #include "mkdeps.h"
     225              : #include "incpath.h"
     226              : #include "libiberty.h"
     227              : #include "stor-layout.h"
     228              : #include "version.h"
     229              : #include "tree-diagnostic.h"
     230              : #include "toplev.h"
     231              : #include "opts.h"
     232              : #include "attribs.h"
     233              : #include "intl.h"
     234              : #include "langhooks.h"
     235              : #include "contracts.h"
     236              : /* This TU doesn't need or want to see the networking.  */
     237              : #define CODY_NETWORKING 0
     238              : #include "mapper-client.h"
     239              : #include <zlib.h> // for crc32, crc32_combine
     240              : 
     241              : #if 0 // 1 for testing no mmap
     242              : #define MAPPED_READING 0
     243              : #define MAPPED_WRITING 0
     244              : #else
     245              : #if HAVE_MMAP_FILE && HAVE_MUNMAP && HAVE_MSYNC
     246              : /* mmap, munmap, msync.  */
     247              : #define MAPPED_READING 1
     248              : #if HAVE_SYSCONF && defined (_SC_PAGE_SIZE)
     249              : /* sysconf (_SC_PAGE_SIZE), ftruncate  */
     250              : /* posix_fallocate used if available.  */
     251              : #define MAPPED_WRITING 1
     252              : #else
     253              : #define MAPPED_WRITING 0
     254              : #endif
     255              : #else
     256              : #define MAPPED_READING 0
     257              : #define MAPPED_WRITING 0
     258              : #endif
     259              : #endif
     260              : 
     261              : /* Some open(2) flag differences, what a colourful world it is!  */
     262              : #if defined (O_CLOEXEC)
     263              : // OK
     264              : #elif defined (_O_NOINHERIT)
     265              : /* Windows' _O_NOINHERIT matches O_CLOEXEC flag */
     266              : #define O_CLOEXEC _O_NOINHERIT
     267              : #else
     268              : #define O_CLOEXEC 0
     269              : #endif
     270              : #if defined (O_BINARY)
     271              : // Ok?
     272              : #elif defined (_O_BINARY)
     273              : /* Windows' open(2) call defaults to text!  */
     274              : #define O_BINARY _O_BINARY
     275              : #else
     276              : #define O_BINARY 0
     277              : #endif
     278              : 
     279       283778 : static inline cpp_hashnode *cpp_node (tree id)
     280              : {
     281       283778 :   return CPP_HASHNODE (GCC_IDENT_TO_HT_IDENT (id));
     282              : }
     283              : 
     284       147684 : static inline tree identifier (const cpp_hashnode *node)
     285              : {
     286              :   /* HT_NODE() expands to node->ident that HT_IDENT_TO_GCC_IDENT()
     287              :      then subtracts a nonzero constant, deriving a pointer to
     288              :      a different member than ident.  That's strictly undefined
     289              :      and detected by -Warray-bounds.  Suppress it.  See PR 101372.  */
     290       147684 : #pragma GCC diagnostic push
     291       147684 : #pragma GCC diagnostic ignored "-Warray-bounds"
     292       147684 :   return HT_IDENT_TO_GCC_IDENT (HT_NODE (const_cast<cpp_hashnode *> (node)));
     293       147684 : #pragma GCC diagnostic pop
     294              : }
     295              : 
     296              : /* Id for dumping module information.  */
     297              : int module_dump_id;
     298              : 
     299              : /* We have a special module owner.  */
     300              : #define MODULE_UNKNOWN (~0U)    /* Not yet known.  */
     301              : 
     302              : /* Prefix for section names.  */
     303              : #define MOD_SNAME_PFX ".gnu.c++"
     304              : 
     305              : /* Format a version for user consumption.  */
     306              : 
     307              : typedef char verstr_t[32];
     308              : static void
     309         4128 : version2string (unsigned version, verstr_t &out)
     310              : {
     311         4128 :   unsigned major = MODULE_MAJOR (version);
     312         4128 :   unsigned minor = MODULE_MINOR (version);
     313              : 
     314         4128 :   if (IS_EXPERIMENTAL (version))
     315         4128 :     sprintf (out, "%04u/%02u/%02u-%02u:%02u%s",
     316         4128 :              2000 + major / 10000, (major / 100) % 100, (major % 100),
     317              :              minor / 100, minor % 100,
     318              :              EXPERIMENT ("", " (experimental)"));
     319              :   else
     320            0 :     sprintf (out, "%u.%u", major, minor);
     321         4128 : }
     322              : 
     323              : /* Include files to note translation for.  */
     324              : static vec<const char *, va_heap, vl_embed> *note_includes;
     325              : 
     326              : /* Modules to note CMI pathames.  */
     327              : static vec<const char *, va_heap, vl_embed> *note_cmis;
     328              : 
     329              : /* Traits to hash an arbitrary pointer.  Entries are not deletable,
     330              :    and removal is a noop (removal needed upon destruction).  */
     331              : template <typename T>
     332              : struct nodel_ptr_hash : pointer_hash<T>, typed_noop_remove <T *> {
     333              :   /* Nothing is deletable.  Everything is insertable.  */
     334              :   static bool is_deleted (T *) { return false; }
     335              :   static void mark_deleted (T *) { gcc_unreachable (); }
     336              : };
     337              : 
     338              : /* Map from pointer to signed integer.   */
     339              : typedef simple_hashmap_traits<nodel_ptr_hash<void>, int> ptr_int_traits;
     340              : typedef hash_map<void *,signed,ptr_int_traits> ptr_int_hash_map;
     341              : 
     342              : /********************************************************************/
     343              : /* Basic streaming & ELF.  Serialization is usually via mmap.  For
     344              :    writing we slide a buffer over the output file, syncing it
     345              :    approproiately.  For reading we simply map the whole file (as a
     346              :    file-backed read-only map -- it's just address space, leaving the
     347              :    OS pager to deal with getting the data to us).  Some buffers need
     348              :    to be more conventional malloc'd contents.   */
     349              : 
     350              : /* Variable length buffer.  */
     351              : 
     352              : namespace {
     353              : 
     354              : constexpr line_map_uint_t loc_one = 1;
     355              : 
     356              : class data {
     357              : public:
     358         2860 :   class allocator {
     359              :   public:
     360              :     /* Tools tend to moan if the dtor's not virtual.  */
     361       101279 :     virtual ~allocator () {}
     362              : 
     363              :   public:
     364              :     void grow (data &obj, unsigned needed, bool exact);
     365              :     void shrink (data &obj);
     366              : 
     367              :   public:
     368              :     virtual char *grow (char *ptr, unsigned needed);
     369              :     virtual void shrink (char *ptr);
     370              :   };
     371              : 
     372              : public:
     373              :   char *buffer;         /* Buffer being transferred.  */
     374              :   /* Although size_t would be the usual size, we know we never get
     375              :      more than 4GB of buffer -- because that's the limit of the
     376              :      encapsulation format.  And if you need bigger imports, you're
     377              :      doing it wrong.  */
     378              :   unsigned size;        /* Allocated size of buffer.  */
     379              :   unsigned pos;         /* Position in buffer.  */
     380              : 
     381              : public:
     382       809828 :   data ()
     383       809828 :     :buffer (NULL), size (0), pos (0)
     384              :   {
     385              :   }
     386       824105 :   ~data ()
     387              :   {
     388              :     /* Make sure the derived and/or using class know what they're
     389              :        doing.  */
     390       824105 :     gcc_checking_assert (!buffer);
     391       824105 :   }
     392              : 
     393              : protected:
     394    587654759 :   char *use (unsigned count)
     395              :   {
     396    587654759 :     if (size < pos + count)
     397              :       return NULL;
     398    587654759 :     char *res = &buffer[pos];
     399    587654759 :     pos += count;
     400    302755538 :     return res;
     401              :   }
     402              : 
     403              :   unsigned calc_crc (unsigned) const;
     404              : 
     405              : public:
     406     44863763 :   void unuse (unsigned count)
     407              :   {
     408     44863763 :     pos -= count;
     409        27785 :   }
     410              : 
     411              : public:
     412              :   static allocator simple_memory;
     413              : };
     414              : } // anon namespace
     415              : 
     416              : /* The simple data allocator.  */
     417              : data::allocator data::simple_memory;
     418              : 
     419              : /* Grow buffer to at least size NEEDED.  */
     420              : 
     421              : void
     422       707973 : data::allocator::grow (data &obj, unsigned needed, bool exact)
     423              : {
     424       707973 :   gcc_checking_assert (needed ? needed > obj.size : !obj.size);
     425       707973 :   if (!needed)
     426              :     /* Pick a default size.  */
     427       299320 :     needed = EXPERIMENT (100, 1000);
     428              : 
     429       707973 :   if (!exact)
     430       699735 :     needed *= 2;
     431       707973 :   obj.buffer = grow (obj.buffer, needed);
     432       707973 :   if (obj.buffer)
     433       707973 :     obj.size = needed;
     434              :   else
     435            0 :     obj.pos = obj.size = 0;
     436       707973 : }
     437              : 
     438              : /* Free a buffer.  */
     439              : 
     440              : void
     441       313632 : data::allocator::shrink (data &obj)
     442              : {
     443            0 :   shrink (obj.buffer);
     444       313632 :   obj.buffer = NULL;
     445       313632 :   obj.size = 0;
     446            0 : }
     447              : 
     448              : char *
     449        11492 : data::allocator::grow (char *ptr, unsigned needed)
     450              : {
     451        11492 :   return XRESIZEVAR (char, ptr, needed);
     452              : }
     453              : 
     454              : void
     455        14300 : data::allocator::shrink (char *ptr)
     456              : {
     457        14300 :   XDELETEVEC (ptr);
     458        14300 : }
     459              : 
     460              : /* Calculate the crc32 of the buffer.  Note the CRC is stored in the
     461              :    first 4 bytes, so don't include them.  */
     462              : 
     463              : unsigned
     464       518775 : data::calc_crc (unsigned l) const
     465              : {
     466       518775 :   return crc32 (0, (unsigned char *)buffer + 4, l - 4);
     467              : }
     468              : 
     469              : class elf_in;
     470              : 
     471              : /* Byte stream reader.  */
     472              : 
     473              : namespace {
     474              : class bytes_in : public data {
     475              :   typedef data parent;
     476              : 
     477              : protected:
     478              :   bool overrun;  /* Sticky read-too-much flag.  */
     479              : 
     480              : public:
     481       229539 :   bytes_in ()
     482       229539 :     : parent (), overrun (false)
     483              :   {
     484              :   }
     485       232364 :   ~bytes_in ()
     486              :   {
     487        15734 :   }
     488              : 
     489              : public:
     490              :   /* Begin reading a named section.  */
     491              :   bool begin (location_t loc, elf_in *src, const char *name);
     492              :   /* Begin reading a numbered section with optional name.  */
     493              :   bool begin (location_t loc, elf_in *src, unsigned, const char * = NULL);
     494              :   /* Complete reading a buffer.  Propagate errors and return true on
     495              :      success.  */
     496              :   bool end (elf_in *src);
     497              :   /* Return true if there is unread data.  */
     498      1747817 :   bool more_p () const
     499              :   {
     500      1747817 :     return pos != size;
     501              :   }
     502              : 
     503              : public:
     504              :   /* Start reading at OFFSET.  */
     505          656 :   void random_access (unsigned offset)
     506              :   {
     507          656 :     if (offset > size)
     508            0 :       set_overrun ();
     509          656 :     pos = offset;
     510              :   }
     511              : 
     512              : public:
     513      1578623 :   void align (unsigned boundary)
     514              :   {
     515      1578623 :     if (unsigned pad = pos & (boundary - 1))
     516      3058453 :       read (boundary - pad);
     517              :   }
     518              : 
     519              : public:
     520    284899221 :   const char *read (unsigned count)
     521              :   {
     522      1479830 :     char *ptr = use (count);
     523    284899221 :     if (!ptr)
     524            0 :       set_overrun ();
     525    231419848 :     return ptr;
     526              :   }
     527              : 
     528              : public:
     529              :   bool check_crc () const;
     530              :   /* We store the CRC in the first 4 bytes, using host endianness.  */
     531       230663 :   unsigned get_crc () const
     532              :   {
     533       230663 :     return *(const unsigned *)&buffer[0];
     534              :   }
     535              : 
     536              : public:
     537              :   /* Manipulate the overrun flag.  */
     538    166084107 :   bool get_overrun () const
     539              :   {
     540    166084107 :     return overrun;
     541              :   }
     542           26 :   void set_overrun ()
     543              :   {
     544           26 :     overrun = true;
     545            0 :   }
     546              : 
     547              : public:
     548              :   unsigned u32 ();      /* Read uncompressed integer.  */
     549              : 
     550              : public:
     551              :   int c () ATTRIBUTE_UNUSED;            /* Read a char.  */
     552              :   int i ();             /* Read a signed int.  */
     553              :   unsigned u ();        /* Read an unsigned int.  */
     554              :   size_t z ();          /* Read a size_t.  */
     555              :   location_t loc ();    /* Read a location_t.  */
     556              :   HOST_WIDE_INT wi ();  /* Read a HOST_WIDE_INT.  */
     557              :   unsigned HOST_WIDE_INT wu (); /* Read an unsigned HOST_WIDE_INT.  */
     558              :   const char *str (size_t * = NULL); /* Read a string.  */
     559              :   const void *buf (size_t); /* Read a fixed-length buffer.  */
     560              :   cpp_hashnode *cpp_node (); /* Read a cpp node.  */
     561              : 
     562              :   struct bits_in;
     563              :   bits_in stream_bits ();
     564              : };
     565              : } // anon namespace
     566              : 
     567              : /* Verify the buffer's CRC is correct.  */
     568              : 
     569              : bool
     570       227652 : bytes_in::check_crc () const
     571              : {
     572       227652 :   if (size < 4)
     573              :     return false;
     574              : 
     575       227652 :   unsigned c_crc = calc_crc (size);
     576       227652 :   if (c_crc != get_crc ())
     577              :     return false;
     578              : 
     579              :   return true;
     580              : }
     581              : 
     582              : class elf_out;
     583              : 
     584              : /* Byte stream writer.  */
     585              : 
     586              : namespace {
     587              : class bytes_out : public data {
     588              :   typedef data parent;
     589              : 
     590              : public:
     591              :   allocator *memory;    /* Obtainer of memory.  */
     592              : 
     593              : public:
     594       574440 :   bytes_out (allocator *memory)
     595       574440 :     : parent (), memory (memory)
     596              :   {
     597              :   }
     598       574440 :   ~bytes_out ()
     599              :   {
     600       604074 :   }
     601              : 
     602              : public:
     603    762230644 :   bool streaming_p () const
     604              :   {
     605    762230644 :     return memory != NULL;
     606              :   }
     607              : 
     608              : public:
     609              :   void set_crc (unsigned *crc_ptr);
     610              : 
     611              : public:
     612              :   /* Begin writing, maybe reserve space for CRC.  */
     613              :   void begin (bool need_crc = true);
     614              :   /* Finish writing.  Spill to section by number.  */
     615              :   unsigned end (elf_out *, unsigned, unsigned *crc_ptr = NULL);
     616              : 
     617              : public:
     618      1889252 :   void align (unsigned boundary)
     619              :   {
     620      1889252 :     if (unsigned pad = pos & (boundary - 1))
     621      1769092 :       write (boundary - pad);
     622      1889252 :   }
     623              : 
     624              : public:
     625    302755538 :   char *write (unsigned count, bool exact = false)
     626              :   {
     627    302755538 :     if (size < pos + count)
     628       394407 :       memory->grow (*this, pos + count, exact);
     629    302755538 :     return use (count);
     630              :   }
     631              : 
     632              : public:
     633              :   void u32 (unsigned);  /* Write uncompressed integer.  */
     634              : 
     635              : public:
     636              :   void c (unsigned char) ATTRIBUTE_UNUSED; /* Write unsigned char.  */
     637              :   void i (int);         /* Write signed int.  */
     638              :   void u (unsigned);    /* Write unsigned int.  */
     639              :   void z (size_t s);    /* Write size_t.  */
     640              :   void loc (location_t); /* Write location_t.  */
     641              :   void wi (HOST_WIDE_INT); /* Write HOST_WIDE_INT.  */
     642              :   void wu (unsigned HOST_WIDE_INT);  /* Write unsigned HOST_WIDE_INT.  */
     643        17845 :   void str (const char *ptr)
     644              :   {
     645        17845 :     str (ptr, strlen (ptr));
     646        17845 :   }
     647       276789 :   void cpp_node (const cpp_hashnode *node)
     648              :   {
     649       276789 :     str ((const char *)NODE_NAME (node), NODE_LEN (node));
     650        12815 :   }
     651              :   void str (const char *, size_t);  /* Write string of known length.  */
     652              :   void buf (const void *, size_t);  /* Write fixed length buffer.  */
     653              :   void *buf (size_t); /* Create a writable buffer */
     654              : 
     655              :   struct bits_out;
     656              :   bits_out stream_bits ();
     657              : 
     658              : public:
     659              :   /* Format a NUL-terminated raw string.  */
     660              :   void printf (const char *, ...) ATTRIBUTE_PRINTF_2;
     661              :   void print_time (const char *, const tm *, const char *);
     662              : 
     663              : public:
     664              :   /* Dump instrumentation.  */
     665              :   static void instrument ();
     666              : 
     667              : protected:
     668              :   /* Instrumentation.  */
     669              :   static unsigned spans[4];
     670              :   static unsigned lengths[4];
     671              : };
     672              : } // anon namespace
     673              : 
     674              : /* Finish bit packet.  Rewind the bytes not used.  */
     675              : 
     676              : static unsigned
     677     44808721 : bit_flush (data& bits, uint32_t& bit_val, unsigned& bit_pos)
     678              : {
     679     44808721 :   gcc_assert (bit_pos);
     680     44808721 :   unsigned bytes = (bit_pos + 7) / 8;
     681     44808721 :   bits.unuse (4 - bytes);
     682     44808721 :   bit_pos = 0;
     683     44808721 :   bit_val = 0;
     684     44808721 :   return bytes;
     685              : }
     686              : 
     687              : /* Bit stream reader (RAII-enabled).  Bools are packed into bytes.  You
     688              :    cannot mix bools and non-bools.  Use bflush to flush the current stream
     689              :    of bools on demand.  Upon destruction bflush is called.
     690              : 
     691              :    When reading, we don't know how many bools we'll read in.  So read
     692              :    4 bytes-worth, and then rewind when flushing if we didn't need them
     693              :    all.  You can't have a block of bools closer than 4 bytes to the
     694              :    end of the buffer.
     695              : 
     696              :    Both bits_in and bits_out maintain the necessary state for bit packing,
     697              :    and since these objects are locally constructed the compiler can more
     698              :    easily track their state across consecutive reads/writes and optimize
     699              :    away redundant buffering checks.  */
     700              : 
     701              : struct bytes_in::bits_in {
     702              :   bytes_in& in;
     703              :   uint32_t bit_val = 0;
     704              :   unsigned bit_pos = 0;
     705              : 
     706     16137045 :   bits_in (bytes_in& in)
     707     16137045 :     : in (in)
     708              :   { }
     709              : 
     710     16137045 :   ~bits_in ()
     711              :   {
     712     15148686 :     bflush ();
     713     16137045 :   }
     714              : 
     715              :   bits_in(bits_in&&) = default;
     716              :   bits_in(const bits_in&) = delete;
     717              :   bits_in& operator=(const bits_in&) = delete;
     718              : 
     719              :   /* Completed a block of bools.  */
     720     33470088 :   void bflush ()
     721              :   {
     722     33470088 :     if (bit_pos)
     723     18321402 :       bit_flush (in, bit_val, bit_pos);
     724     33470088 :   }
     725              : 
     726              :   /* Read one bit.  */
     727    553414473 :   bool b ()
     728              :   {
     729    553414473 :     if (!bit_pos)
     730     24233565 :       bit_val = in.u32 ();
     731    553414473 :     bool x = (bit_val >> bit_pos) & 1;
     732    553414473 :     bit_pos = (bit_pos + 1) % 32;
     733    553414473 :     return x;
     734              :   }
     735              : };
     736              : 
     737              : /* Factory function for bits_in.  */
     738              : 
     739              : bytes_in::bits_in
     740     16137045 : bytes_in::stream_bits ()
     741              : {
     742     16137045 :   return bits_in (*this);
     743              : }
     744              : 
     745              : /* Bit stream writer (RAII-enabled), counterpart to bits_in.  */
     746              : 
     747              : struct bytes_out::bits_out {
     748              :   bytes_out& out;
     749              :   uint32_t bit_val = 0;
     750              :   unsigned bit_pos = 0;
     751              :   char is_set = -1;
     752              : 
     753     17506542 :   bits_out (bytes_out& out)
     754     17506542 :     : out (out)
     755              :   { }
     756              : 
     757     17506542 :   ~bits_out ()
     758              :   {
     759        73799 :     bflush ();
     760              :   }
     761              : 
     762              :   bits_out(bits_out&&) = default;
     763              :   bits_out(const bits_out&) = delete;
     764              :   bits_out& operator=(const bits_out&) = delete;
     765              : 
     766              :   /* Completed a block of bools.  */
     767     36293167 :   void bflush ()
     768              :   {
     769     36293167 :     if (bit_pos)
     770              :       {
     771     19910176 :         out.u32 (bit_val);
     772     19910176 :         out.lengths[2] += bit_flush (out, bit_val, bit_pos);
     773              :       }
     774     36293167 :     out.spans[2]++;
     775     36293167 :     is_set = -1;
     776     36293167 :   }
     777              : 
     778              :   /* Write one bit.
     779              : 
     780              :      It may be worth optimizing for most bools being zero.  Some kind of
     781              :      run-length encoding?  */
     782    600692977 :   void b (bool x)
     783              :   {
     784    600692977 :     if (is_set != x)
     785              :       {
     786     62915283 :         is_set = x;
     787     62915283 :         out.spans[x]++;
     788              :       }
     789    600692977 :     out.lengths[x]++;
     790    600692977 :     bit_val |= unsigned (x) << bit_pos++;
     791    600692977 :     if (bit_pos == 32)
     792              :       {
     793      6577143 :         out.u32 (bit_val);
     794      6577143 :         out.lengths[2] += bit_flush (out, bit_val, bit_pos);
     795              :       }
     796    600692977 :   }
     797              : };
     798              : 
     799              : /* Factory function for bits_out.  */
     800              : 
     801              : bytes_out::bits_out
     802     17506542 : bytes_out::stream_bits ()
     803              : {
     804     17506542 :   return bits_out (*this);
     805              : }
     806              : 
     807              : /* Instrumentation.  */
     808              : unsigned bytes_out::spans[4];
     809              : unsigned bytes_out::lengths[4];
     810              : 
     811              : /* If CRC_PTR non-null, set the CRC of the buffer.  Mix the CRC into
     812              :    that pointed to by CRC_PTR.  */
     813              : 
     814              : void
     815       293836 : bytes_out::set_crc (unsigned *crc_ptr)
     816              : {
     817       293836 :   if (crc_ptr)
     818              :     {
     819       291123 :       gcc_checking_assert (pos >= 4);
     820              : 
     821       291123 :       unsigned crc = calc_crc (pos);
     822       291123 :       unsigned accum = *crc_ptr;
     823              :       /* Only mix the existing *CRC_PTR if it is non-zero.  */
     824       291123 :       accum = accum ? crc32_combine (accum, crc, pos - 4) : crc;
     825       291123 :       *crc_ptr = accum;
     826              : 
     827              :       /* Buffer will be sufficiently aligned.  */
     828       291123 :       *(unsigned *)buffer = crc;
     829              :     }
     830       293836 : }
     831              : 
     832              : /* Exactly 4 bytes.  Used internally for bool packing and a few other
     833              :    places.  We can't simply use uint32_t because (a) alignment and
     834              :    (b) we need little-endian for the bool streaming rewinding to make
     835              :    sense.  */
     836              : 
     837              : void
     838     26496119 : bytes_out::u32 (unsigned val)
     839              : {
     840     26496119 :   if (char *ptr = write (4))
     841              :     {
     842     26496119 :       ptr[0] = val;
     843     26496119 :       ptr[1] = val >> 8;
     844     26496119 :       ptr[2] = val >> 16;
     845     26496119 :       ptr[3] = val >> 24;
     846              :     }
     847     26496119 : }
     848              : 
     849              : unsigned
     850     24243026 : bytes_in::u32 ()
     851              : {
     852     24243026 :   unsigned val = 0;
     853     24243026 :   if (const char *ptr = read (4))
     854              :     {
     855     24243026 :       val |= (unsigned char)ptr[0];
     856     24243026 :       val |= (unsigned char)ptr[1] << 8;
     857     24243026 :       val |= (unsigned char)ptr[2] << 16;
     858     24243026 :       val |= (unsigned char)ptr[3] << 24;
     859              :     }
     860              : 
     861     24243026 :   return val;
     862              : }
     863              : 
     864              : /* Chars are unsigned and written as single bytes. */
     865              : 
     866              : void
     867            0 : bytes_out::c (unsigned char v)
     868              : {
     869            0 :   if (char *ptr = write (1))
     870            0 :     *ptr = v;
     871            0 : }
     872              : 
     873              : int
     874            0 : bytes_in::c ()
     875              : {
     876            0 :   int v = 0;
     877            0 :   if (const char *ptr = read (1))
     878            0 :     v = (unsigned char)ptr[0];
     879            0 :   return v;
     880              : }
     881              : 
     882              : /* Ints 7-bit as a byte. Otherwise a 3bit count of following bytes in
     883              :    big-endian form.  4 bits are in the first byte.  */
     884              : 
     885              : void
     886    106684559 : bytes_out::i (int v)
     887              : {
     888    106684559 :   if (char *ptr = write (1))
     889              :     {
     890    106684559 :       if (v <= 0x3f && v >= -0x40)
     891     83741820 :         *ptr = v & 0x7f;
     892              :       else
     893              :         {
     894     22942739 :           unsigned bytes = 0;
     895     22942739 :           int probe;
     896     22942739 :           if (v >= 0)
     897            0 :             for (probe = v >> 8; probe > 0x7; probe >>= 8)
     898            0 :               bytes++;
     899              :           else
     900     34999060 :             for (probe = v >> 8; probe < -0x8; probe >>= 8)
     901     12056321 :               bytes++;
     902     22942739 :           *ptr = 0x80 | bytes << 4 | (probe & 0xf);
     903     22942739 :           if ((ptr = write (++bytes)))
     904     57941799 :             for (; bytes--; v >>= 8)
     905     34999060 :               ptr[bytes] = v & 0xff;
     906              :         }
     907              :     }
     908    106684559 : }
     909              : 
     910              : int
     911     99133421 : bytes_in::i ()
     912              : {
     913     99133421 :   int v = 0;
     914     99133421 :   if (const char *ptr = read (1))
     915              :     {
     916     99133421 :       v = *ptr & 0xff;
     917     99133421 :       if (v & 0x80)
     918              :         {
     919     22678618 :           unsigned bytes = (v >> 4) & 0x7;
     920     22678618 :           v &= 0xf;
     921     22678618 :           if (v & 0x8)
     922     22678618 :             v |= -1 ^ 0x7;
     923              :           /* unsigned necessary due to left shifts of -ve values.  */
     924     22678618 :           unsigned uv = unsigned (v);
     925     22678618 :           if ((ptr = read (++bytes)))
     926     58958520 :             while (bytes--)
     927     36279902 :               uv = (uv << 8) | (*ptr++ & 0xff);
     928     22678618 :           v = int (uv);
     929              :         }
     930     76454803 :       else if (v & 0x40)
     931      9618256 :         v |= -1 ^ 0x3f;
     932              :     }
     933              : 
     934     99133421 :   return v;
     935              : }
     936              : 
     937              : void
     938     91602363 : bytes_out::u (unsigned v)
     939              : {
     940     91602363 :   if (char *ptr = write (1))
     941              :     {
     942     91602363 :       if (v <= 0x7f)
     943     79699437 :         *ptr = v;
     944              :       else
     945              :         {
     946     11902926 :           unsigned bytes = 0;
     947     11902926 :           unsigned probe;
     948     14130973 :           for (probe = v >> 8; probe > 0xf; probe >>= 8)
     949      2228047 :             bytes++;
     950     11902926 :           *ptr = 0x80 | bytes << 4 | probe;
     951     11902926 :           if ((ptr = write (++bytes)))
     952     26033899 :             for (; bytes--; v >>= 8)
     953     14130973 :               ptr[bytes] = v & 0xff;
     954              :         }
     955              :     }
     956     91602363 : }
     957              : 
     958              : unsigned
     959     86675992 : bytes_in::u ()
     960              : {
     961     86675992 :   unsigned v = 0;
     962              : 
     963     86675992 :   if (const char *ptr = read (1))
     964              :     {
     965     86675992 :       v = *ptr & 0xff;
     966     86675992 :       if (v & 0x80)
     967              :         {
     968     11430072 :           unsigned bytes = (v >> 4) & 0x7;
     969     11430072 :           v &= 0xf;
     970     11430072 :           if ((ptr = read (++bytes)))
     971     25120086 :             while (bytes--)
     972     13690014 :               v = (v << 8) | (*ptr++ & 0xff);
     973              :         }
     974              :     }
     975              : 
     976     86675992 :   return v;
     977              : }
     978              : 
     979              : void
     980     21857590 : bytes_out::wi (HOST_WIDE_INT v)
     981              : {
     982     21857590 :   if (char *ptr = write (1))
     983              :     {
     984     21857590 :       if (v <= 0x3f && v >= -0x40)
     985      4301734 :         *ptr = v & 0x7f;
     986              :       else
     987              :         {
     988     17555856 :           unsigned bytes = 0;
     989     17555856 :           HOST_WIDE_INT probe;
     990     17555856 :           if (v >= 0)
     991     63830327 :             for (probe = v >> 8; probe > 0x7; probe >>= 8)
     992     46277202 :               bytes++;
     993              :           else
     994         8864 :             for (probe = v >> 8; probe < -0x8; probe >>= 8)
     995         6133 :               bytes++;
     996     17555856 :           *ptr = 0x80 | bytes << 4 | (probe & 0xf);
     997     17555856 :           if ((ptr = write (++bytes)))
     998     81395047 :             for (; bytes--; v >>= 8)
     999     63839191 :               ptr[bytes] = v & 0xff;
    1000              :         }
    1001              :     }
    1002     21857590 : }
    1003              : 
    1004              : HOST_WIDE_INT
    1005     19788786 : bytes_in::wi ()
    1006              : {
    1007     19788786 :   HOST_WIDE_INT v = 0;
    1008     19788786 :   if (const char *ptr = read (1))
    1009              :     {
    1010     19788786 :       v = *ptr & 0xff;
    1011     19788786 :       if (v & 0x80)
    1012              :         {
    1013     17890853 :           unsigned bytes = (v >> 4) & 0x7;
    1014     17890853 :           v &= 0xf;
    1015     17890853 :           if (v & 0x8)
    1016         2086 :             v |= -1 ^ 0x7;
    1017              :           /* unsigned necessary due to left shifts of -ve values.  */
    1018     17890853 :           unsigned HOST_WIDE_INT uv = (unsigned HOST_WIDE_INT) v;
    1019     17890853 :           if ((ptr = read (++bytes)))
    1020     82654323 :             while (bytes--)
    1021     64763470 :               uv = (uv << 8) | (*ptr++ & 0xff);
    1022     17890853 :           v = (HOST_WIDE_INT) uv;
    1023              :         }
    1024      1897933 :       else if (v & 0x40)
    1025         8559 :         v |= -1 ^ 0x3f;
    1026              :     }
    1027              : 
    1028     19788786 :   return v;
    1029              : }
    1030              : 
    1031              : /* unsigned wide ints are just written as signed wide ints.  */
    1032              : 
    1033              : inline void
    1034     21856890 : bytes_out::wu (unsigned HOST_WIDE_INT v)
    1035              : {
    1036     21856890 :   wi ((HOST_WIDE_INT) v);
    1037              : }
    1038              : 
    1039              : inline unsigned HOST_WIDE_INT
    1040     19788212 : bytes_in::wu ()
    1041              : {
    1042     38982159 :   return (unsigned HOST_WIDE_INT) wi ();
    1043              : }
    1044              : 
    1045              : /* size_t written as unsigned or unsigned wide int.  */
    1046              : 
    1047              : inline void
    1048      1854706 : bytes_out::z (size_t s)
    1049              : {
    1050      1854706 :   if (sizeof (s) == sizeof (unsigned))
    1051              :     u (s);
    1052              :   else
    1053      3676440 :     wu (s);
    1054           12 : }
    1055              : 
    1056              : inline size_t
    1057      1561659 : bytes_in::z ()
    1058              : {
    1059      1561659 :   if (sizeof (size_t) == sizeof (unsigned))
    1060              :     return u ();
    1061              :   else
    1062      3123318 :     return wu ();
    1063              : }
    1064              : 
    1065              : /* location_t written as 32- or 64-bit as needed.  */
    1066              : 
    1067     19310885 : inline void bytes_out::loc (location_t l)
    1068              : {
    1069     19310885 :   if (sizeof (location_t) > sizeof (unsigned))
    1070     36583633 :     wu (l);
    1071              :   else
    1072              :     u (l);
    1073      2035424 : }
    1074              : 
    1075     17635256 : inline location_t bytes_in::loc ()
    1076              : {
    1077     17635256 :   if (sizeof (location_t) > sizeof (unsigned))
    1078     35267514 :     return wu ();
    1079              :   else
    1080              :     return u ();
    1081              : }
    1082              : 
    1083              : /* Buffer simply memcpied.  */
    1084              : void *
    1085      1889252 : bytes_out::buf (size_t len)
    1086              : {
    1087      1889252 :   align (sizeof (void *) * 2);
    1088      1889252 :   return write (len);
    1089              : }
    1090              : 
    1091              : void
    1092      1840561 : bytes_out::buf (const void *src, size_t len)
    1093              : {
    1094      1840561 :   if (void *ptr = buf (len))
    1095      1840561 :     memcpy (ptr, src, len);
    1096      1840561 : }
    1097              : 
    1098              : const void *
    1099      1578623 : bytes_in::buf (size_t len)
    1100              : {
    1101      1578623 :   align (sizeof (void *) * 2);
    1102      1578623 :   const char *ptr = read (len);
    1103              : 
    1104      1578623 :   return ptr;
    1105              : }
    1106              : 
    1107              : /* strings as an size_t length, followed by the buffer.  Make sure
    1108              :    there's a NUL terminator on read.  */
    1109              : 
    1110              : void
    1111      1854688 : bytes_out::str (const char *string, size_t len)
    1112              : {
    1113      1821728 :   z (len);
    1114      1821728 :   if (len)
    1115              :     {
    1116      1821728 :       gcc_checking_assert (!string[len]);
    1117      1821728 :       buf (string, len + 1);
    1118              :     }
    1119        32960 : }
    1120              : 
    1121              : const char *
    1122      1561653 : bytes_in::str (size_t *len_p)
    1123              : {
    1124      1561653 :   size_t len = z ();
    1125              : 
    1126              :   /* We're about to trust some user data.  */
    1127      1561653 :   if (overrun)
    1128            0 :     len = 0;
    1129      1561653 :   if (len_p)
    1130      1557759 :     *len_p = len;
    1131      1561653 :   const char *str = NULL;
    1132      1561653 :   if (len)
    1133              :     {
    1134      1561390 :       str = reinterpret_cast<const char *> (buf (len + 1));
    1135      1561390 :       if (!str || str[len])
    1136              :         {
    1137            0 :           set_overrun ();
    1138            0 :           str = NULL;
    1139              :         }
    1140              :     }
    1141            0 :   return str ? str : "";
    1142              : }
    1143              : 
    1144              : cpp_hashnode *
    1145       284041 : bytes_in::cpp_node ()
    1146              : {
    1147       284041 :   size_t len;
    1148       284041 :   const char *s = str (&len);
    1149       284041 :   if (!len)
    1150              :     return NULL;
    1151       283778 :   return ::cpp_node (get_identifier_with_length (s, len));
    1152              : }
    1153              : 
    1154              : /* Format a string directly to the buffer, including a terminating
    1155              :    NUL.  Intended for human consumption.  */
    1156              : 
    1157              : void
    1158        27785 : bytes_out::printf (const char *format, ...)
    1159              : {
    1160        27785 :   va_list args;
    1161              :   /* Exercise buffer expansion.  */
    1162        27785 :   size_t len = EXPERIMENT (10, 500);
    1163              : 
    1164        55042 :   while (char *ptr = write (len))
    1165              :     {
    1166        55042 :       va_start (args, format);
    1167        55042 :       size_t actual = vsnprintf (ptr, len, format, args) + 1;
    1168        55042 :       va_end (args);
    1169        55042 :       if (actual <= len)
    1170              :         {
    1171        27785 :           unuse (len - actual);
    1172        27785 :           break;
    1173              :         }
    1174        27257 :       unuse (len);
    1175        27257 :       len = actual;
    1176        27257 :     }
    1177        27785 : }
    1178              : 
    1179              : void
    1180         5426 : bytes_out::print_time (const char *kind, const tm *time, const char *tz)
    1181              : {
    1182         5426 :   printf ("%stime: %4u/%02u/%02u %02u:%02u:%02u %s",
    1183         5426 :           kind, time->tm_year + 1900, time->tm_mon + 1, time->tm_mday,
    1184         5426 :           time->tm_hour, time->tm_min, time->tm_sec, tz);
    1185         5426 : }
    1186              : 
    1187              : /* Encapsulated Lazy Records Of Named Declarations.
    1188              :    Header: Stunningly Elf32_Ehdr-like
    1189              :    Sections: Sectional data
    1190              :      [1-N) : User data sections
    1191              :      N .strtab  : strings, stunningly ELF STRTAB-like
    1192              :    Index: Section table, stunningly ELF32_Shdr-like.   */
    1193              : 
    1194              : class elf {
    1195              : protected:
    1196              :   /* Constants used within the format.  */
    1197              :   enum private_constants {
    1198              :     /* File kind. */
    1199              :     ET_NONE = 0,
    1200              :     EM_NONE = 0,
    1201              :     OSABI_NONE = 0,
    1202              : 
    1203              :     /* File format. */
    1204              :     EV_CURRENT = 1,
    1205              :     CLASS32 = 1,
    1206              :     DATA2LSB = 1,
    1207              :     DATA2MSB = 2,
    1208              : 
    1209              :     /* Section numbering.  */
    1210              :     SHN_UNDEF = 0,
    1211              :     SHN_LORESERVE = 0xff00,
    1212              :     SHN_XINDEX = 0xffff,
    1213              : 
    1214              :     /* Section types.  */
    1215              :     SHT_NONE = 0,       /* No contents.  */
    1216              :     SHT_PROGBITS = 1, /* Random bytes.  */
    1217              :     SHT_STRTAB = 3,     /* A string table.  */
    1218              : 
    1219              :     /* Section flags.  */
    1220              :     SHF_NONE = 0x00,    /* Nothing.  */
    1221              :     SHF_STRINGS = 0x20,  /* NUL-Terminated strings.  */
    1222              : 
    1223              :     /* I really hope we do not get CMI files larger than 4GB.  */
    1224              :     MY_CLASS = CLASS32,
    1225              :     /* It is host endianness that is relevant.  */
    1226              :     MY_ENDIAN = DATA2LSB
    1227              : #ifdef WORDS_BIGENDIAN
    1228              :     ^ DATA2LSB ^ DATA2MSB
    1229              : #endif
    1230              :   };
    1231              : 
    1232              : public:
    1233              :   /* Constants visible to users.  */
    1234              :   enum public_constants {
    1235              :     /* Special error codes.  Breaking layering a bit.  */
    1236              :     E_BAD_DATA = -1,  /* Random unexpected data errors.  */
    1237              :     E_BAD_LAZY = -2,  /* Badly ordered laziness.  */
    1238              :     E_BAD_IMPORT = -3 /* A nested import failed.  */
    1239              :   };
    1240              : 
    1241              : protected:
    1242              :   /* File identification.  On-disk representation.  */
    1243              :   struct ident {
    1244              :     uint8_t magic[4];   /* 0x7f, 'E', 'L', 'F' */
    1245              :     uint8_t klass;      /* 4:CLASS32 */
    1246              :     uint8_t data;       /* 5:DATA2[LM]SB */
    1247              :     uint8_t version;    /* 6:EV_CURRENT  */
    1248              :     uint8_t osabi;      /* 7:OSABI_NONE */
    1249              :     uint8_t abiver;     /* 8: 0 */
    1250              :     uint8_t pad[7];     /* 9-15 */
    1251              :   };
    1252              :   /* File header.  On-disk representation.  */
    1253              :   struct header {
    1254              :     struct ident ident;
    1255              :     uint16_t type;      /* ET_NONE */
    1256              :     uint16_t machine;   /* EM_NONE */
    1257              :     uint32_t version;   /* EV_CURRENT */
    1258              :     uint32_t entry;     /* 0 */
    1259              :     uint32_t phoff;     /* 0 */
    1260              :     uint32_t shoff;     /* Section Header Offset in file */
    1261              :     uint32_t flags;
    1262              :     uint16_t ehsize;    /* ELROND Header SIZE -- sizeof (header) */
    1263              :     uint16_t phentsize; /* 0 */
    1264              :     uint16_t phnum;     /* 0 */
    1265              :     uint16_t shentsize; /* Section Header SIZE -- sizeof (section) */
    1266              :     uint16_t shnum;     /* Section Header NUM */
    1267              :     uint16_t shstrndx;  /* Section Header STRing iNDeX */
    1268              :   };
    1269              :   /* File section.  On-disk representation.  */
    1270              :   struct section {
    1271              :     uint32_t name;      /* String table offset.  */
    1272              :     uint32_t type;      /* SHT_* */
    1273              :     uint32_t flags;     /* SHF_* */
    1274              :     uint32_t addr;      /* 0 */
    1275              :     uint32_t offset;    /* OFFSET in file */
    1276              :     uint32_t size;      /* SIZE of section */
    1277              :     uint32_t link;      /* 0 */
    1278              :     uint32_t info;      /* 0 */
    1279              :     uint32_t addralign; /* 0 */
    1280              :     uint32_t entsize;   /* ENTry SIZE, usually 0 */
    1281              :   };
    1282              : 
    1283              : protected:
    1284              :   data hdr;     /* The header.  */
    1285              :   data sectab;  /* The section table.  */
    1286              :   data strtab;  /* String table.  */
    1287              :   int fd;       /* File descriptor we're reading or writing.  */
    1288              :   int err;      /* Sticky error code.  */
    1289              : 
    1290              : public:
    1291              :   /* Construct from STREAM.  E is errno if STREAM NULL.  */
    1292         5849 :   elf (int fd, int e)
    1293        11698 :     :hdr (), sectab (), strtab (), fd (fd), err (fd >= 0 ? 0 : e)
    1294              :   {}
    1295         5767 :   ~elf ()
    1296              :   {
    1297         5767 :     gcc_checking_assert (fd < 0 && !hdr.buffer
    1298              :                          && !sectab.buffer && !strtab.buffer);
    1299         5767 :   }
    1300              : 
    1301              : public:
    1302              :   /* Return the error, if we have an error.  */
    1303       444925 :   int get_error () const
    1304              :   {
    1305       444925 :     return err;
    1306              :   }
    1307              :   /* Set the error, unless it's already been set.  */
    1308           53 :   void set_error (int e = E_BAD_DATA)
    1309              :   {
    1310           53 :     if (!err)
    1311           19 :       err = e;
    1312            0 :   }
    1313              :   /* Get an error string.  */
    1314              :   const char *get_error (const char *) const;
    1315              : 
    1316              : public:
    1317              :   /* Begin reading/writing file.  Return false on error.  */
    1318         5731 :   bool begin () const
    1319              :   {
    1320         5731 :     return !get_error ();
    1321              :   }
    1322              :   /* Finish reading/writing file.  Return false on error.  */
    1323              :   bool end ();
    1324              : };
    1325              : 
    1326              : /* Return error string.  */
    1327              : 
    1328              : const char *
    1329           40 : elf::get_error (const char *name) const
    1330              : {
    1331           40 :   if (!name)
    1332              :     return "Unknown CMI mapping";
    1333              : 
    1334           40 :   switch (err)
    1335              :     {
    1336            0 :     case 0:
    1337            0 :       gcc_unreachable ();
    1338              :     case E_BAD_DATA:
    1339              :       return "Bad file data";
    1340            6 :     case E_BAD_IMPORT:
    1341            6 :       return "Bad import dependency";
    1342            0 :     case E_BAD_LAZY:
    1343            0 :       return "Bad lazy ordering";
    1344           21 :     default:
    1345           21 :       return xstrerror (err);
    1346              :     }
    1347              : }
    1348              : 
    1349              : /* Finish file, return true if there's an error.  */
    1350              : 
    1351              : bool
    1352         8158 : elf::end ()
    1353              : {
    1354              :   /* Close the stream and free the section table.  */
    1355         8158 :   if (fd >= 0 && close (fd))
    1356            0 :     set_error (errno);
    1357         8158 :   fd = -1;
    1358              : 
    1359         8158 :   return !get_error ();
    1360              : }
    1361              : 
    1362              : /* ELROND reader.  */
    1363              : 
    1364              : class elf_in : public elf {
    1365              :   typedef elf parent;
    1366              : 
    1367              : private:
    1368              :   /* For freezing & defrosting.  */
    1369              : #if !defined (HOST_LACKS_INODE_NUMBERS)
    1370              :   dev_t device;
    1371              :   ino_t inode;
    1372              : #endif
    1373              : 
    1374              : public:
    1375         2989 :   elf_in (int fd, int e)
    1376         5978 :     :parent (fd, e)
    1377              :   {
    1378              :   }
    1379         2907 :   ~elf_in ()
    1380              :   {
    1381         2907 :   }
    1382              : 
    1383              : public:
    1384       209393 :   bool is_frozen () const
    1385              :   {
    1386           18 :     return fd < 0 && hdr.pos;
    1387              :   }
    1388           18 :   bool is_freezable () const
    1389              :   {
    1390            9 :     return fd >= 0 && hdr.pos;
    1391              :   }
    1392              :   void freeze ();
    1393              :   bool defrost (const char *);
    1394              : 
    1395              :   /* If BYTES is in the mmapped area, allocate a new buffer for it.  */
    1396            0 :   void preserve (bytes_in &bytes ATTRIBUTE_UNUSED)
    1397              :   {
    1398              : #if MAPPED_READING
    1399            0 :     if (hdr.buffer && bytes.buffer >= hdr.buffer
    1400            0 :         && bytes.buffer < hdr.buffer + hdr.pos)
    1401              :       {
    1402            0 :         char *buf = bytes.buffer;
    1403            0 :         bytes.buffer = data::simple_memory.grow (NULL, bytes.size);
    1404            0 :         memcpy (bytes.buffer, buf, bytes.size);
    1405              :       }
    1406              : #endif
    1407            0 :   }
    1408              :   /* If BYTES is not in SELF's mmapped area, free it.  SELF might be
    1409              :      NULL. */
    1410         1063 :   static void release (elf_in *self ATTRIBUTE_UNUSED, bytes_in &bytes)
    1411              :   {
    1412              : #if MAPPED_READING
    1413         1063 :     if (!(self && self->hdr.buffer && bytes.buffer >= self->hdr.buffer
    1414         1063 :           && bytes.buffer < self->hdr.buffer + self->hdr.pos))
    1415              : #endif
    1416            0 :       data::simple_memory.shrink (bytes.buffer);
    1417         1063 :     bytes.buffer = NULL;
    1418         1063 :     bytes.size = 0;
    1419         1063 :   }
    1420              : 
    1421              : public:
    1422       233582 :   static void grow (data &data, unsigned needed)
    1423              :   {
    1424       233582 :     gcc_checking_assert (!data.buffer);
    1425              : #if !MAPPED_READING
    1426              :     data.buffer = XNEWVEC (char, needed);
    1427              : #endif
    1428       233582 :     data.size = needed;
    1429       233582 :   }
    1430       240086 :   static void shrink (data &data)
    1431              :   {
    1432              : #if !MAPPED_READING
    1433              :     XDELETEVEC (data.buffer);
    1434              : #endif
    1435       240086 :     data.buffer = NULL;
    1436       240086 :     data.size = 0;
    1437            0 :   }
    1438              : 
    1439              : public:
    1440       230617 :   const section *get_section (unsigned s) const
    1441              :   {
    1442       230617 :     if (s * sizeof (section) < sectab.size)
    1443       230617 :       return reinterpret_cast<const section *>
    1444       230617 :         (&sectab.buffer[s * sizeof (section)]);
    1445              :     else
    1446              :       return NULL;
    1447              :   }
    1448              :   unsigned get_section_limit () const
    1449              :   {
    1450              :     return sectab.size / sizeof (section);
    1451              :   }
    1452              : 
    1453              : protected:
    1454              :   const char *read (data *, unsigned, unsigned);
    1455              : 
    1456              : public:
    1457              :   /* Read section by number.  */
    1458       230617 :   bool read (data *d, const section *s)
    1459              :   {
    1460       230617 :     return s && read (d, s->offset, s->size);
    1461              :   }
    1462              : 
    1463              :   /* Find section by name.  */
    1464              :   unsigned find (const char *name);
    1465              :   /* Find section by index.  */
    1466              :   const section *find (unsigned snum, unsigned type = SHT_PROGBITS);
    1467              : 
    1468              : public:
    1469              :   /* Release the string table, when we're done with it.  */
    1470         8238 :   void release ()
    1471              :   {
    1472         8238 :     shrink (strtab);
    1473           39 :   }
    1474              : 
    1475              : public:
    1476              :   bool begin (location_t);
    1477         5298 :   bool end ()
    1478              :   {
    1479         5298 :     release ();
    1480              : #if MAPPED_READING
    1481         5298 :     if (hdr.buffer)
    1482         2907 :       munmap (hdr.buffer, hdr.pos);
    1483         5298 :     hdr.buffer = NULL;
    1484              : #endif
    1485         5298 :     shrink (sectab);
    1486              : 
    1487         5298 :     return parent::end ();
    1488              :   }
    1489              : 
    1490              : public:
    1491              :   /* Return string name at OFFSET.  Checks OFFSET range.  Always
    1492              :      returns non-NULL.  We know offset 0 is an empty string.  */
    1493       337899 :   const char *name (unsigned offset)
    1494              :   {
    1495       675798 :     return &strtab.buffer[offset < strtab.size ? offset : 0];
    1496              :   }
    1497              : };
    1498              : 
    1499              : /* ELROND writer.  */
    1500              : 
    1501              : class elf_out : public elf, public data::allocator {
    1502              :   typedef elf parent;
    1503              :   /* Desired section alignment on disk.  */
    1504              :   static const int SECTION_ALIGN = 16;
    1505              : 
    1506              : private:
    1507              :   ptr_int_hash_map identtab;    /* Map of IDENTIFIERS to strtab offsets. */
    1508              :   unsigned pos;                 /* Write position in file.  */
    1509              : #if MAPPED_WRITING
    1510              :   unsigned offset;              /* Offset of the mapping.  */
    1511              :   unsigned extent;              /* Length of mapping.  */
    1512              :   unsigned page_size;           /* System page size.  */
    1513              : #endif
    1514              : 
    1515              : public:
    1516         2860 :   elf_out (int fd, int e)
    1517         5608 :     :parent (fd, e), identtab (500), pos (0)
    1518              :   {
    1519              : #if MAPPED_WRITING
    1520         2860 :     offset = extent = 0;
    1521         2860 :     page_size = sysconf (_SC_PAGE_SIZE);
    1522         2860 :     if (page_size < SECTION_ALIGN)
    1523              :       /* Something really strange.  */
    1524            0 :       set_error (EINVAL);
    1525              : #endif
    1526         2860 :   }
    1527         2860 :   ~elf_out ()
    1528         2860 :   {
    1529         2860 :     data::simple_memory.shrink (hdr);
    1530         2860 :     data::simple_memory.shrink (sectab);
    1531         2860 :     data::simple_memory.shrink (strtab);
    1532         2860 :   }
    1533              : 
    1534              : #if MAPPED_WRITING
    1535              : private:
    1536              :   void create_mapping (unsigned ext, bool extending = true);
    1537              :   void remove_mapping ();
    1538              : #endif
    1539              : 
    1540              : protected:
    1541              :   using allocator::grow;
    1542              :   char *grow (char *, unsigned needed) final override;
    1543              : #if MAPPED_WRITING
    1544              :   using allocator::shrink;
    1545              :   void shrink (char *) final override;
    1546              : #endif
    1547              : 
    1548              : public:
    1549         5726 :   unsigned get_section_limit () const
    1550              :   {
    1551         5726 :     return sectab.pos / sizeof (section);
    1552              :   }
    1553              : 
    1554              : protected:
    1555              :   unsigned add (unsigned type, unsigned name = 0,
    1556              :                 unsigned off = 0, unsigned size = 0, unsigned flags = SHF_NONE);
    1557              :   unsigned write (const data &);
    1558              : #if MAPPED_WRITING
    1559              :   unsigned write (const bytes_out &);
    1560              : #endif
    1561              : 
    1562              : public:
    1563              :   /* IDENTIFIER to strtab offset.  */
    1564              :   unsigned name (tree ident);
    1565              :   /* String literal to strtab offset.  */
    1566              :   unsigned name (const char *n);
    1567              :   /* Qualified name of DECL to strtab offset.  */
    1568              :   unsigned qualified_name (tree decl, bool is_defn);
    1569              : 
    1570              : private:
    1571              :   unsigned strtab_write (const char *s, unsigned l);
    1572              :   void strtab_write (tree decl, int);
    1573              : 
    1574              : public:
    1575              :   /* Add a section with contents or strings.  */
    1576              :   unsigned add (const bytes_out &, bool string_p, unsigned name);
    1577              : 
    1578              : public:
    1579              :   /* Begin and end writing.  */
    1580              :   bool begin ();
    1581              :   bool end ();
    1582              : };
    1583              : 
    1584              : /* Begin reading section NAME (of type PROGBITS) from SOURCE.
    1585              :    Data always checked for CRC.  */
    1586              : 
    1587              : bool
    1588        21197 : bytes_in::begin (location_t loc, elf_in *source, const char *name)
    1589              : {
    1590        21197 :   unsigned snum = source->find (name);
    1591              : 
    1592        21197 :   return begin (loc, source, snum, name);
    1593              : }
    1594              : 
    1595              : /* Begin reading section numbered SNUM with NAME (may be NULL).  */
    1596              : 
    1597              : bool
    1598       227652 : bytes_in::begin (location_t loc, elf_in *source, unsigned snum, const char *name)
    1599              : {
    1600       227652 :   if (!source->read (this, source->find (snum))
    1601       227652 :       || !size || !check_crc ())
    1602              :     {
    1603            0 :       source->set_error (elf::E_BAD_DATA);
    1604            0 :       source->shrink (*this);
    1605            0 :       if (name)
    1606            0 :         error_at (loc, "section %qs is missing or corrupted", name);
    1607              :       else
    1608            0 :         error_at (loc, "section #%u is missing or corrupted", snum);
    1609            0 :       return false;
    1610              :     }
    1611       227652 :   pos = 4;
    1612       227652 :   return true;
    1613              : }
    1614              : 
    1615              : /* Finish reading a section.  */
    1616              : 
    1617              : bool
    1618       226550 : bytes_in::end (elf_in *src)
    1619              : {
    1620       226550 :   if (more_p ())
    1621           13 :     set_overrun ();
    1622       226550 :   if (overrun)
    1623           13 :     src->set_error ();
    1624              : 
    1625       226550 :   src->shrink (*this);
    1626              : 
    1627       226550 :   return !overrun;
    1628              : }
    1629              : 
    1630              : /* Begin writing buffer.  */
    1631              : 
    1632              : void
    1633       293836 : bytes_out::begin (bool need_crc)
    1634              : {
    1635            0 :   if (need_crc)
    1636            0 :     pos = 4;
    1637            0 :   memory->grow (*this, 0, false);
    1638       272411 : }
    1639              : 
    1640              : /* Finish writing buffer.  Stream out to SINK as named section NAME.
    1641              :    Return section number or 0 on failure.  If CRC_PTR is true, crc
    1642              :    the data.  Otherwise it is a string section.  */
    1643              : 
    1644              : unsigned
    1645       293836 : bytes_out::end (elf_out *sink, unsigned name, unsigned *crc_ptr)
    1646              : {
    1647       293836 :   lengths[3] += pos;
    1648       293836 :   spans[3]++;
    1649              : 
    1650       293836 :   set_crc (crc_ptr);
    1651       293836 :   unsigned sec_num = sink->add (*this, !crc_ptr, name);
    1652       293836 :   memory->shrink (*this);
    1653              : 
    1654       293836 :   return sec_num;
    1655              : }
    1656              : 
    1657              : /* Close and open the file, without destroying it.  */
    1658              : 
    1659              : void
    1660            9 : elf_in::freeze ()
    1661              : {
    1662            9 :   gcc_checking_assert (!is_frozen ());
    1663              : #if MAPPED_READING
    1664            9 :   if (munmap (hdr.buffer, hdr.pos) < 0)
    1665            0 :     set_error (errno);
    1666              : #endif
    1667            9 :   if (close (fd) < 0)
    1668            0 :     set_error (errno);
    1669            9 :   fd = -1;
    1670            9 : }
    1671              : 
    1672              : bool
    1673            9 : elf_in::defrost (const char *name)
    1674              : {
    1675            9 :   gcc_checking_assert (is_frozen ());
    1676            9 :   struct stat stat;
    1677              : 
    1678            9 :   fd = open (name, O_RDONLY | O_CLOEXEC | O_BINARY);
    1679            9 :   if (fd < 0 || fstat (fd, &stat) < 0)
    1680            0 :     set_error (errno);
    1681              :   else
    1682              :     {
    1683            9 :       bool ok = hdr.pos == unsigned (stat.st_size);
    1684              : #ifndef HOST_LACKS_INODE_NUMBERS
    1685            9 :       if (device != stat.st_dev
    1686            9 :           || inode != stat.st_ino)
    1687              :         ok = false;
    1688              : #endif
    1689            9 :       if (!ok)
    1690            0 :         set_error (EMFILE);
    1691              : #if MAPPED_READING
    1692            0 :       if (ok)
    1693              :         {
    1694            9 :           char *mapping = reinterpret_cast<char *>
    1695            9 :             (mmap (NULL, hdr.pos, PROT_READ, MAP_SHARED, fd, 0));
    1696            9 :           if (mapping == MAP_FAILED)
    1697            0 :           fail:
    1698            0 :               set_error (errno);
    1699              :           else
    1700              :             {
    1701            9 :               if (madvise (mapping, hdr.pos, MADV_RANDOM))
    1702            0 :                 goto fail;
    1703              : 
    1704              :               /* These buffers are never NULL in this case.  */
    1705            9 :               strtab.buffer = mapping + strtab.pos;
    1706            9 :               sectab.buffer = mapping + sectab.pos;
    1707            9 :               hdr.buffer = mapping;
    1708              :             }
    1709              :         }
    1710              : #endif
    1711              :     }
    1712              : 
    1713            9 :   return !get_error ();
    1714              : }
    1715              : 
    1716              : /* Read at current position into BUFFER.  Return true on success.  */
    1717              : 
    1718              : const char *
    1719       233582 : elf_in::read (data *data, unsigned pos, unsigned length)
    1720              : {
    1721              : #if MAPPED_READING
    1722       233582 :   if (pos + length > hdr.pos)
    1723              :     {
    1724            0 :       set_error (EINVAL);
    1725            0 :       return NULL;
    1726              :     }
    1727              : #else
    1728              :   if (pos != ~0u && lseek (fd, pos, SEEK_SET) < 0)
    1729              :     {
    1730              :       set_error (errno);
    1731              :       return NULL;
    1732              :     }
    1733              : #endif
    1734       233582 :   grow (*data, length);
    1735              : #if MAPPED_READING
    1736       233582 :   data->buffer = hdr.buffer + pos;
    1737              : #else
    1738              :   if (::read (fd, data->buffer, data->size) != ssize_t (length))
    1739              :     {
    1740              :       set_error (errno);
    1741              :       shrink (*data);
    1742              :       return NULL;
    1743              :     }
    1744              : #endif
    1745              : 
    1746       233582 :   return data->buffer;
    1747              : }
    1748              : 
    1749              : /* Read section SNUM of TYPE.  Return section pointer or NULL on error.  */
    1750              : 
    1751              : const elf::section *
    1752       230617 : elf_in::find (unsigned snum, unsigned type)
    1753              : {
    1754       230617 :   const section *sec = get_section (snum);
    1755       230617 :   if (!snum || !sec || sec->type != type)
    1756            0 :     return NULL;
    1757              :   return sec;
    1758              : }
    1759              : 
    1760              : /* Find a section NAME and TYPE.  Return section number, or zero on
    1761              :    failure.  */
    1762              : 
    1763              : unsigned
    1764        21242 : elf_in::find (const char *sname)
    1765              : {
    1766       136011 :   for (unsigned pos = sectab.size; pos -= sizeof (section); )
    1767              :     {
    1768       136011 :       const section *sec
    1769       136011 :         = reinterpret_cast<const section *> (&sectab.buffer[pos]);
    1770              : 
    1771       272022 :       if (0 == strcmp (sname, name (sec->name)))
    1772        21242 :         return pos / sizeof (section);
    1773              :     }
    1774              : 
    1775              :   return 0;
    1776              : }
    1777              : 
    1778              : /* Begin reading file.  Verify header.  Pull in section and string
    1779              :    tables.  Return true on success.  */
    1780              : 
    1781              : bool
    1782         2989 : elf_in::begin (location_t loc)
    1783              : {
    1784         2989 :   if (!parent::begin ())
    1785              :     return false;
    1786              : 
    1787         2965 :   struct stat stat;
    1788         2965 :   unsigned size = 0;
    1789         2965 :   if (!fstat (fd, &stat))
    1790              :     {
    1791              : #if !defined (HOST_LACKS_INODE_NUMBERS)
    1792         2965 :       device = stat.st_dev;
    1793         2965 :       inode = stat.st_ino;
    1794              : #endif
    1795              :       /* Never generate files > 4GB, check we've not been given one.  */
    1796         2965 :       if (stat.st_size == unsigned (stat.st_size))
    1797         2965 :         size = unsigned (stat.st_size);
    1798              :     }
    1799              : 
    1800              : #if MAPPED_READING
    1801              :   /* MAP_SHARED so that the file is backing store.  If someone else
    1802              :      concurrently writes it, they're wrong.  */
    1803         2965 :   void *mapping = mmap (NULL, size, PROT_READ, MAP_SHARED, fd, 0);
    1804         2965 :   if (mapping == MAP_FAILED)
    1805              :     {
    1806            0 :     fail:
    1807            0 :       set_error (errno);
    1808            0 :       return false;
    1809              :     }
    1810              :   /* We'll be hopping over this randomly.  Some systems declare the
    1811              :      first parm as char *, and other declare it as void *.  */
    1812         2965 :   if (madvise (reinterpret_cast <char *> (mapping), size, MADV_RANDOM))
    1813            0 :     goto fail;
    1814              : 
    1815         2965 :   hdr.buffer = (char *)mapping;
    1816              : #else
    1817              :   read (&hdr, 0, sizeof (header));
    1818              : #endif
    1819         2965 :   hdr.pos = size; /* Record size of the file.  */
    1820              : 
    1821         2965 :   const header *h = reinterpret_cast<const header *> (hdr.buffer);
    1822         2965 :   if (!h)
    1823              :     return false;
    1824              : 
    1825         2965 :   if (h->ident.magic[0] != 0x7f
    1826         2965 :       || h->ident.magic[1] != 'E'
    1827         2965 :       || h->ident.magic[2] != 'L'
    1828         2965 :       || h->ident.magic[3] != 'F')
    1829              :     {
    1830            0 :       error_at (loc, "not Encapsulated Lazy Records of Named Declarations");
    1831            0 :     failed:
    1832            0 :       shrink (hdr);
    1833            0 :       return false;
    1834              :     }
    1835              : 
    1836              :   /* We expect a particular format -- the ELF is not intended to be
    1837              :      distributable.  */
    1838         2965 :   if (h->ident.klass != MY_CLASS
    1839         2965 :       || h->ident.data != MY_ENDIAN
    1840         2965 :       || h->ident.version != EV_CURRENT
    1841         2965 :       || h->type != ET_NONE
    1842         2965 :       || h->machine != EM_NONE
    1843         2965 :       || h->ident.osabi != OSABI_NONE)
    1844              :     {
    1845            0 :       error_at (loc, "unexpected encapsulation format or type");
    1846            0 :       goto failed;
    1847              :     }
    1848              : 
    1849         2965 :   int e = -1;
    1850         2965 :   if (!h->shoff || h->shentsize != sizeof (section))
    1851              :     {
    1852            0 :     malformed:
    1853            0 :       set_error (e);
    1854            0 :       error_at (loc, "encapsulation is malformed");
    1855            0 :       goto failed;
    1856              :     }
    1857              : 
    1858         2965 :   unsigned strndx = h->shstrndx;
    1859         2965 :   unsigned shnum = h->shnum;
    1860         2965 :   if (shnum == SHN_XINDEX)
    1861              :     {
    1862            0 :       if (!read (&sectab, h->shoff, sizeof (section)))
    1863              :         {
    1864            0 :         section_table_fail:
    1865            0 :           e = errno;
    1866            0 :           goto malformed;
    1867              :         }
    1868            0 :       shnum = get_section (0)->size;
    1869              :       /* Freeing does mean we'll re-read it in the case we're not
    1870              :          mapping, but this is going to be rare.  */
    1871            0 :       shrink (sectab);
    1872              :     }
    1873              : 
    1874         2965 :   if (!shnum)
    1875            0 :     goto malformed;
    1876              : 
    1877         2965 :   if (!read (&sectab, h->shoff, shnum * sizeof (section)))
    1878            0 :     goto section_table_fail;
    1879              : 
    1880         2965 :   if (strndx == SHN_XINDEX)
    1881            0 :     strndx = get_section (0)->link;
    1882              : 
    1883         2965 :   if (!read (&strtab, find (strndx, SHT_STRTAB)))
    1884            0 :     goto malformed;
    1885              : 
    1886              :   /* The string table should be at least one byte, with NUL chars
    1887              :      at either end.  */
    1888         2965 :   if (!(strtab.size && !strtab.buffer[0]
    1889         2965 :         && !strtab.buffer[strtab.size - 1]))
    1890            0 :     goto malformed;
    1891              : 
    1892              : #if MAPPED_READING
    1893              :   /* Record the offsets of the section and string tables.  */
    1894         2965 :   sectab.pos = h->shoff;
    1895         2965 :   strtab.pos = shnum * sizeof (section);
    1896              : #else
    1897              :   shrink (hdr);
    1898              : #endif
    1899              : 
    1900         2965 :   return true;
    1901              : }
    1902              : 
    1903              : /* Create a new mapping.  */
    1904              : 
    1905              : #if MAPPED_WRITING
    1906              : void
    1907         3537 : elf_out::create_mapping (unsigned ext, bool extending)
    1908              : {
    1909              :   /* A wrapper around posix_fallocate, falling back to ftruncate
    1910              :      if the underlying filesystem does not support the operation.  */
    1911         6932 :   auto allocate = [](int fd, off_t offset, off_t length)
    1912              :     {
    1913              : #ifdef HAVE_POSIX_FALLOCATE
    1914         3395 :       int result = posix_fallocate (fd, offset, length);
    1915         3395 :       if (result != EINVAL)
    1916         3395 :         return result == 0;
    1917              :       /* Not supported by the underlying filesystem, fallback to ftruncate.  */
    1918              : #endif
    1919            0 :       return ftruncate (fd, offset + length) == 0;
    1920              :     };
    1921              : 
    1922         3537 :   void *mapping = MAP_FAILED;
    1923         3537 :   if (extending && ext < 1024 * 1024)
    1924              :     {
    1925         3251 :       if (allocate (fd, offset, ext * 2))
    1926         3251 :         mapping = mmap (NULL, ext * 2, PROT_READ | PROT_WRITE,
    1927         3251 :                         MAP_SHARED, fd, offset);
    1928         3251 :       if (mapping != MAP_FAILED)
    1929              :         ext *= 2;
    1930              :     }
    1931              :   if (mapping == MAP_FAILED)
    1932              :     {
    1933          286 :       if (!extending || allocate (fd, offset, ext))
    1934          286 :         mapping = mmap (NULL, ext, PROT_READ | PROT_WRITE,
    1935          286 :                         MAP_SHARED, fd, offset);
    1936          286 :       if (mapping == MAP_FAILED)
    1937              :         {
    1938            0 :           set_error (errno);
    1939              :           mapping = NULL;
    1940              :           ext = 0;
    1941              :         }
    1942              :     }
    1943         3537 :   hdr.buffer = (char *)mapping;
    1944         3537 :   extent = ext;
    1945         3537 : }
    1946              : #endif
    1947              : 
    1948              : /* Flush out the current mapping.  */
    1949              : 
    1950              : #if MAPPED_WRITING
    1951              : void
    1952         3543 : elf_out::remove_mapping ()
    1953              : {
    1954         3543 :   if (hdr.buffer)
    1955              :     {
    1956              :       /* MS_ASYNC dtrt with the removed mapping, including a
    1957              :          subsequent overlapping remap.  */
    1958         3537 :       if (msync (hdr.buffer, extent, MS_ASYNC)
    1959         3537 :           || munmap (hdr.buffer, extent))
    1960              :         /* We're somewhat screwed at this point.  */
    1961            0 :         set_error (errno);
    1962              :     }
    1963              : 
    1964         3543 :   hdr.buffer = NULL;
    1965         3543 : }
    1966              : #endif
    1967              : 
    1968              : /* Grow a mapping of PTR to be NEEDED bytes long.  This gets
    1969              :    interesting if the new size grows the EXTENT.  */
    1970              : 
    1971              : char *
    1972       696481 : elf_out::grow (char *data, unsigned needed)
    1973              : {
    1974       696481 :   if (!data)
    1975              :     {
    1976              :       /* First allocation, check we're aligned.  */
    1977       299332 :       gcc_checking_assert (!(pos & (SECTION_ALIGN - 1)));
    1978              : #if MAPPED_WRITING
    1979       299332 :       data = hdr.buffer + (pos - offset);
    1980              : #endif
    1981              :     }
    1982              : 
    1983              : #if MAPPED_WRITING
    1984       696481 :   unsigned off = data - hdr.buffer;
    1985       696481 :   if (off + needed > extent)
    1986              :     {
    1987              :       /* We need to grow the mapping.  */
    1988          653 :       unsigned lwm = off & ~(page_size - 1);
    1989          653 :       unsigned hwm = (off + needed + page_size - 1) & ~(page_size - 1);
    1990              : 
    1991          653 :       gcc_checking_assert (hwm > extent);
    1992              : 
    1993          653 :       remove_mapping ();
    1994              : 
    1995          653 :       offset += lwm;
    1996          653 :       create_mapping (extent < hwm - lwm ? hwm - lwm : extent);
    1997              : 
    1998          653 :       data = hdr.buffer + (off - lwm);
    1999              :     }
    2000              : #else
    2001              :   data = allocator::grow (data, needed);
    2002              : #endif
    2003              : 
    2004       696481 :   return data;
    2005              : }
    2006              : 
    2007              : #if MAPPED_WRITING
    2008              : /* Shrinking is a NOP.  */
    2009              : void
    2010       299332 : elf_out::shrink (char *)
    2011              : {
    2012       299332 : }
    2013              : #endif
    2014              : 
    2015              : /* Write S of length L to the strtab buffer.  L must include the ending
    2016              :    NUL, if that's what you want.  */
    2017              : 
    2018              : unsigned
    2019      1300075 : elf_out::strtab_write (const char *s, unsigned l)
    2020              : {
    2021      1300075 :   if (strtab.pos + l > strtab.size)
    2022         1262 :     data::simple_memory.grow (strtab, strtab.pos + l, false);
    2023      1300075 :   memcpy (strtab.buffer + strtab.pos, s, l);
    2024      1300075 :   unsigned res = strtab.pos;
    2025      1300075 :   strtab.pos += l;
    2026      1300075 :   return res;
    2027              : }
    2028              : 
    2029              : /* Write qualified name of decl.  INNER >0 if this is a definition, <0
    2030              :    if this is a qualifier of an outer name.  */
    2031              : 
    2032              : void
    2033       513276 : elf_out::strtab_write (tree decl, int inner)
    2034              : {
    2035       513276 :   tree ctx = CP_DECL_CONTEXT (decl);
    2036       513276 :   if (TYPE_P (ctx))
    2037         5308 :     ctx = TYPE_NAME (ctx);
    2038       513276 :   if (ctx != global_namespace)
    2039       257310 :     strtab_write (ctx, -1);
    2040              : 
    2041       513276 :   tree name = DECL_NAME (decl);
    2042       513276 :   if (!name)
    2043          339 :     name = DECL_ASSEMBLER_NAME_RAW (decl);
    2044       513276 :   strtab_write (IDENTIFIER_POINTER (name), IDENTIFIER_LENGTH (name));
    2045              : 
    2046       513276 :   if (inner)
    2047       365441 :     strtab_write (&"::{}"[inner+1], 2);
    2048       513276 : }
    2049              : 
    2050              : /* Map IDENTIFIER IDENT to strtab offset.  Inserts into strtab if not
    2051              :    already there.  */
    2052              : 
    2053              : unsigned
    2054       145402 : elf_out::name (tree ident)
    2055              : {
    2056       145402 :   unsigned res = 0;
    2057       145402 :   if (ident)
    2058              :     {
    2059       145350 :       bool existed;
    2060       145350 :       int *slot = &identtab.get_or_insert (ident, &existed);
    2061       145350 :       if (!existed)
    2062       260586 :         *slot = strtab_write (IDENTIFIER_POINTER (ident),
    2063       130293 :                               IDENTIFIER_LENGTH (ident) + 1);
    2064       145350 :       res = *slot;
    2065              :     }
    2066       145402 :   return res;
    2067              : }
    2068              : 
    2069              : /* Map LITERAL to strtab offset.  Does not detect duplicates and
    2070              :    expects LITERAL to remain live until strtab is written out.  */
    2071              : 
    2072              : unsigned
    2073        35099 : elf_out::name (const char *literal)
    2074              : {
    2075        35099 :   return strtab_write (literal, strlen (literal) + 1);
    2076              : }
    2077              : 
    2078              : /* Map a DECL's qualified name to strtab offset.  Does not detect
    2079              :    duplicates.  */
    2080              : 
    2081              : unsigned
    2082       255966 : elf_out::qualified_name (tree decl, bool is_defn)
    2083              : {
    2084       255966 :   gcc_checking_assert (DECL_P (decl) && decl != global_namespace);
    2085       255966 :   unsigned result = strtab.pos;
    2086              : 
    2087       255966 :   strtab_write (decl, is_defn);
    2088       255966 :   strtab_write ("", 1);
    2089              : 
    2090       255966 :   return result;
    2091              : }
    2092              : 
    2093              : /* Add section to file.  Return section number.  TYPE & NAME identify
    2094              :    the section.  OFF and SIZE identify the file location of its
    2095              :    data.  FLAGS contains additional info.  */
    2096              : 
    2097              : unsigned
    2098       299326 : elf_out::add (unsigned type, unsigned name, unsigned off, unsigned size,
    2099              :               unsigned flags)
    2100              : {
    2101       299326 :   gcc_checking_assert (!(off & (SECTION_ALIGN - 1)));
    2102       299326 :   if (sectab.pos + sizeof (section) > sectab.size)
    2103         4746 :     data::simple_memory.grow (sectab, sectab.pos + sizeof (section), false);
    2104       299326 :   section *sec = reinterpret_cast<section *> (sectab.buffer + sectab.pos);
    2105       299326 :   memset (sec, 0, sizeof (section));
    2106       299326 :   sec->type = type;
    2107       299326 :   sec->flags = flags;
    2108       299326 :   sec->name = name;
    2109       299326 :   sec->offset = off;
    2110       299326 :   sec->size = size;
    2111       299326 :   if (flags & SHF_STRINGS)
    2112         5461 :     sec->entsize = 1;
    2113              : 
    2114       299326 :   unsigned res = sectab.pos;
    2115       299326 :   sectab.pos += sizeof (section);
    2116       299326 :   return res / sizeof (section);
    2117              : }
    2118              : 
    2119              : /* Pad to the next alignment boundary, then write BUFFER to disk.
    2120              :    Return the position of the start of the write, or zero on failure.   */
    2121              : 
    2122              : unsigned
    2123        10986 : elf_out::write (const data &buffer)
    2124              : {
    2125              : #if MAPPED_WRITING
    2126              :   /* HDR is always mapped.  */
    2127        10986 :   if (&buffer != &hdr)
    2128              :     {
    2129         5496 :       bytes_out out (this);
    2130         5496 :       grow (out, buffer.pos, true);
    2131         5496 :       if (out.buffer)
    2132         5496 :         memcpy (out.buffer, buffer.buffer, buffer.pos);
    2133         5496 :       shrink (out);
    2134         5496 :     }
    2135              :   else
    2136              :     /* We should have been aligned during the first allocation.  */
    2137         5490 :     gcc_checking_assert (!(pos & (SECTION_ALIGN - 1)));
    2138              : #else
    2139              :   if (::write (fd, buffer.buffer, buffer.pos) != ssize_t (buffer.pos))
    2140              :     {
    2141              :       set_error (errno);
    2142              :       return 0;
    2143              :     }
    2144              : #endif
    2145        10986 :   unsigned res = pos;
    2146        10986 :   pos += buffer.pos;
    2147              : 
    2148        10986 :   if (unsigned padding = -pos & (SECTION_ALIGN - 1))
    2149              :     {
    2150              : #if !MAPPED_WRITING
    2151              :       /* Align the section on disk, should help the necessary copies.
    2152              :          fseeking to extend is non-portable.  */
    2153              :       static char zero[SECTION_ALIGN];
    2154              :       if (::write (fd, &zero, padding) != ssize_t (padding))
    2155              :         set_error (errno);
    2156              : #endif
    2157         9362 :       pos += padding;
    2158              :     }
    2159        10986 :   return res;
    2160              : }
    2161              : 
    2162              : /* Write a streaming buffer.  It must be using us as an allocator.  */
    2163              : 
    2164              : #if MAPPED_WRITING
    2165              : unsigned
    2166       293836 : elf_out::write (const bytes_out &buf)
    2167              : {
    2168       293836 :   gcc_checking_assert (buf.memory == this);
    2169              :   /* A directly mapped buffer.  */
    2170       293836 :   gcc_checking_assert (buf.buffer - hdr.buffer >= 0
    2171              :                        && buf.buffer - hdr.buffer + buf.size <= extent);
    2172       293836 :   unsigned res = pos;
    2173       293836 :   pos += buf.pos;
    2174              : 
    2175              :   /* Align up.  We're not going to advance into the next page. */
    2176       293836 :   pos += -pos & (SECTION_ALIGN - 1);
    2177              : 
    2178       293836 :   return res;
    2179              : }
    2180              : #endif
    2181              : 
    2182              : /* Write data and add section.  STRING_P is true for a string
    2183              :    section, false for PROGBITS.  NAME identifies the section (0 is the
    2184              :    empty name).  DATA is the contents.  Return section number or 0 on
    2185              :    failure (0 is the undef section).  */
    2186              : 
    2187              : unsigned
    2188       293836 : elf_out::add (const bytes_out &data, bool string_p, unsigned name)
    2189              : {
    2190       293836 :   unsigned off = write (data);
    2191              : 
    2192       587672 :   return add (string_p ? SHT_STRTAB : SHT_PROGBITS, name,
    2193       293836 :               off, data.pos, string_p ? SHF_STRINGS : SHF_NONE);
    2194              : }
    2195              : 
    2196              : /* Begin writing the file.  Initialize the section table and write an
    2197              :    empty header.  Return false on failure.  */
    2198              : 
    2199              : bool
    2200         2742 : elf_out::begin ()
    2201              : {
    2202         2742 :   if (!parent::begin ())
    2203              :     return false;
    2204              : 
    2205              :   /* Let the allocators pick a default.  */
    2206         2742 :   data::simple_memory.grow (strtab, 0, false);
    2207         2742 :   data::simple_memory.grow (sectab, 0, false);
    2208              : 
    2209              :   /* The string table starts with an empty string.  */
    2210         2742 :   name ("");
    2211              : 
    2212              :   /* Create the UNDEF section.  */
    2213         2742 :   add (SHT_NONE);
    2214              : 
    2215              : #if MAPPED_WRITING
    2216              :   /* Start a mapping.  */
    2217         2742 :   create_mapping (EXPERIMENT (page_size,
    2218              :                               (32767 + page_size) & ~(page_size - 1)));
    2219         2742 :   if (!hdr.buffer)
    2220              :     return false;
    2221              : #endif
    2222              : 
    2223              :   /* Write an empty header.  */
    2224         2742 :   grow (hdr, sizeof (header), true);
    2225         2742 :   header *h = reinterpret_cast<header *> (hdr.buffer);
    2226         2742 :   memset (h, 0, sizeof (header));
    2227         2742 :   hdr.pos = hdr.size;
    2228         2742 :   write (hdr);
    2229         2742 :   return !get_error ();
    2230              : }
    2231              : 
    2232              : /* Finish writing the file.  Write out the string & section tables.
    2233              :    Fill in the header.  Return true on error.  */
    2234              : 
    2235              : bool
    2236         2860 : elf_out::end ()
    2237              : {
    2238         2860 :   if (fd >= 0)
    2239              :     {
    2240              :       /* Write the string table.  */
    2241         2748 :       unsigned strnam = name (".strtab");
    2242         2748 :       unsigned stroff = write (strtab);
    2243         2748 :       unsigned strndx = add (SHT_STRTAB, strnam, stroff, strtab.pos,
    2244              :                              SHF_STRINGS);
    2245              : 
    2246              :       /* Store escape values in section[0].  */
    2247         2748 :       if (strndx >= SHN_LORESERVE)
    2248              :         {
    2249            0 :           reinterpret_cast<section *> (sectab.buffer)->link = strndx;
    2250            0 :           strndx = SHN_XINDEX;
    2251              :         }
    2252         2748 :       unsigned shnum = sectab.pos / sizeof (section);
    2253         2748 :       if (shnum >= SHN_LORESERVE)
    2254              :         {
    2255            0 :           reinterpret_cast<section *> (sectab.buffer)->size = shnum;
    2256            0 :           shnum = SHN_XINDEX;
    2257              :         }
    2258              : 
    2259         2748 :       unsigned shoff = write (sectab);
    2260              : 
    2261              : #if MAPPED_WRITING
    2262         2748 :       if (offset)
    2263              :         {
    2264          142 :           remove_mapping ();
    2265          142 :           offset = 0;
    2266          142 :           create_mapping ((sizeof (header) + page_size - 1) & ~(page_size - 1),
    2267              :                           false);
    2268              :         }
    2269         2748 :       unsigned length = pos;
    2270              : #else
    2271              :       if (lseek (fd, 0, SEEK_SET) < 0)
    2272              :         set_error (errno);
    2273              : #endif
    2274              :       /* Write header.  */
    2275         2748 :       if (!get_error ())
    2276              :         {
    2277              :           /* Write the correct header now.  */
    2278         2748 :           header *h = reinterpret_cast<header *> (hdr.buffer);
    2279         2748 :           h->ident.magic[0] = 0x7f;
    2280         2748 :           h->ident.magic[1] = 'E';   /* Elrond */
    2281         2748 :           h->ident.magic[2] = 'L';   /* is an */
    2282         2748 :           h->ident.magic[3] = 'F';   /* elf.  */
    2283         2748 :           h->ident.klass = MY_CLASS;
    2284         2748 :           h->ident.data =  MY_ENDIAN;
    2285         2748 :           h->ident.version = EV_CURRENT;
    2286         2748 :           h->ident.osabi = OSABI_NONE;
    2287         2748 :           h->type = ET_NONE;
    2288         2748 :           h->machine = EM_NONE;
    2289         2748 :           h->version = EV_CURRENT;
    2290         2748 :           h->shoff = shoff;
    2291         2748 :           h->ehsize = sizeof (header);
    2292         2748 :           h->shentsize = sizeof (section);
    2293         2748 :           h->shnum = shnum;
    2294         2748 :           h->shstrndx = strndx;
    2295              : 
    2296         2748 :           pos = 0;
    2297         2748 :           write (hdr);
    2298              :         }
    2299              : 
    2300              : #if MAPPED_WRITING
    2301         2748 :       remove_mapping ();
    2302         2748 :       if (ftruncate (fd, length))
    2303            0 :         set_error (errno);
    2304              : #endif
    2305              :     }
    2306              : 
    2307         2860 :   data::simple_memory.shrink (sectab);
    2308         2860 :   data::simple_memory.shrink (strtab);
    2309              : 
    2310         2860 :   return parent::end ();
    2311              : }
    2312              : 
    2313              : /********************************************************************/
    2314              : 
    2315              : /* A dependency set.  This is used during stream out to determine the
    2316              :    connectivity of the graph.  Every namespace-scope declaration that
    2317              :    needs writing has a depset.  The depset is filled with the (depsets
    2318              :    of) declarations within this module that it references.  For a
    2319              :    declaration that'll generally be named types.  For definitions
    2320              :    it'll also be declarations in the body.
    2321              : 
    2322              :    From that we can convert the graph to a DAG, via determining the
    2323              :    Strongly Connected Clusters.  Each cluster is streamed
    2324              :    independently, and thus we achieve lazy loading.
    2325              : 
    2326              :    Other decls that get a depset are namespaces themselves and
    2327              :    unnameable declarations.   */
    2328              : 
    2329              : class depset {
    2330              : private:
    2331              :   tree entity;  /* Entity, or containing namespace.  */
    2332              :   uintptr_t discriminator;  /* Flags or identifier.  */
    2333              : 
    2334              : public:
    2335              :   /* The kinds of entity the depset could describe.  The ordering is
    2336              :      significant, see entity_kind_name.  */
    2337              :   enum entity_kind
    2338              :   {
    2339              :     EK_DECL,            /* A decl.  */
    2340              :     EK_SPECIALIZATION,  /* A specialization.  */
    2341              :     EK_PARTIAL,         /* A partial specialization.  */
    2342              :     EK_USING,           /* A using declaration (at namespace scope).  */
    2343              :     EK_NAMESPACE,       /* A namespace.  */
    2344              :     EK_TU_LOCAL,        /* A TU-local decl for ADL.  */
    2345              :     EK_REDIRECT,        /* Redirect to a template_decl.  */
    2346              :     EK_EXPLICIT_HWM,
    2347              :     EK_BINDING = EK_EXPLICIT_HWM, /* Implicitly encoded.  */
    2348              :     EK_FOR_BINDING,     /* A decl being inserted for a binding.  */
    2349              :     EK_INNER_DECL,      /* A decl defined outside of its imported
    2350              :                            context.  */
    2351              :     EK_DIRECT_HWM = EK_PARTIAL + 1,
    2352              : 
    2353              :     EK_BITS = 3         /* Only need to encode below EK_EXPLICIT_HWM.  */
    2354              :   };
    2355              :   static_assert (EK_EXPLICIT_HWM < (1u << EK_BITS),
    2356              :                  "not enough bits reserved for entity_kind");
    2357              : 
    2358              : private:
    2359              :   /* Placement of bit fields in discriminator.  */
    2360              :   enum disc_bits
    2361              :   {
    2362              :     DB_ZERO_BIT, /* Set to disambiguate identifier from flags  */
    2363              :     DB_SPECIAL_BIT, /* First dep slot is special.  */
    2364              :     DB_KIND_BIT, /* Kind of the entity.  */
    2365              :     DB_KIND_BITS = EK_BITS,
    2366              :     DB_DEFN_BIT = DB_KIND_BIT + DB_KIND_BITS,
    2367              :     DB_IS_PENDING_BIT,          /* Is a maybe-pending entity.  */
    2368              :     DB_TU_LOCAL_BIT,            /* Is a TU-local entity.  */
    2369              :     DB_REF_GLOBAL_BIT,          /* Refers to a GMF TU-local entity.  */
    2370              :     DB_REF_PURVIEW_BIT,         /* Refers to a purview TU-local entity.  */
    2371              :     DB_EXPOSE_GLOBAL_BIT,       /* Exposes a GMF TU-local entity.  */
    2372              :     DB_EXPOSE_PURVIEW_BIT,      /* Exposes a purview TU-local entity.  */
    2373              :     DB_IGNORED_EXPOSURE_BIT,    /* Only seen where exposures are ignored.  */
    2374              :     DB_IMPORTED_BIT,            /* An imported entity.  */
    2375              :     DB_UNREACHED_BIT,           /* A yet-to-be reached entity.  */
    2376              :     DB_MAYBE_RECURSIVE_BIT,     /* An entity maybe in a recursive cluster.  */
    2377              :     DB_ENTRY_BIT,               /* The first reached recursive dep.  */
    2378              :     DB_HIDDEN_BIT,              /* A hidden binding.  */
    2379              :     /* The following bits are not independent, but enumerating them is
    2380              :        awkward.  */
    2381              :     DB_TYPE_SPEC_BIT,           /* Specialization in the type table.  */
    2382              :     DB_FRIEND_SPEC_BIT,         /* An instantiated template friend.  */
    2383              :     DB_HWM,
    2384              :   };
    2385              :   static_assert (DB_HWM <= sizeof(discriminator) * CHAR_BIT,
    2386              :                  "not enough bits in discriminator");
    2387              : 
    2388              : public:
    2389              :   /* The first slot is special for EK_SPECIALIZATIONS it is a
    2390              :      spec_entry pointer.  It is not relevant for the SCC
    2391              :      determination.  */
    2392              :   vec<depset *> deps;  /* Depsets we reference.  */
    2393              : 
    2394              : public:
    2395              :   unsigned cluster; /* Strongly connected cluster, later entity number  */
    2396              :   unsigned section; /* Section written to.  */
    2397              :   /* During SCC construction, section is lowlink, until the depset is
    2398              :      removed from the stack.  See Tarjan algorithm for details.  */
    2399              : 
    2400              : private:
    2401              :   /* Construction via factories.  Destruction via hash traits.  */
    2402              :   depset (tree entity);
    2403              :   ~depset ();
    2404              : 
    2405              : public:
    2406              :   static depset *make_binding (tree, tree);
    2407              :   static depset *make_entity (tree, entity_kind, bool = false);
    2408              :   /* Late setting a binding name -- /then/ insert into hash!  */
    2409              :   inline void set_binding_name (tree name)
    2410              :   {
    2411              :     gcc_checking_assert (!get_name ());
    2412              :     discriminator = reinterpret_cast<uintptr_t> (name);
    2413              :   }
    2414              : 
    2415              : private:
    2416      4908677 :   template<unsigned I> void set_flag_bit ()
    2417              :   {
    2418            0 :     gcc_checking_assert (I < 2 || !is_binding ());
    2419      4908677 :     discriminator |= 1u << I;
    2420      3072526 :   }
    2421      5093842 :   template<unsigned I> void clear_flag_bit ()
    2422              :   {
    2423            0 :     gcc_checking_assert (I < 2 || !is_binding ());
    2424      5093842 :     discriminator &= ~(1u << I);
    2425      5093842 :   }
    2426    489640872 :   template<unsigned I> bool get_flag_bit () const
    2427              :   {
    2428            0 :     gcc_checking_assert (I < 2 || !is_binding ());
    2429    595472397 :     return bool ((discriminator >> I) & 1);
    2430              :   }
    2431              : 
    2432              : public:
    2433    480068454 :   bool is_binding () const
    2434              :   {
    2435    113997893 :     return !get_flag_bit<DB_ZERO_BIT> ();
    2436              :   }
    2437    252168186 :   entity_kind get_entity_kind () const
    2438              :   {
    2439        29830 :     if (is_binding ())
    2440              :       return EK_BINDING;
    2441    189041464 :     return entity_kind ((discriminator >> DB_KIND_BIT) & ((1u << EK_BITS) - 1));
    2442              :   }
    2443              :   const char *entity_kind_name () const;
    2444              : 
    2445              : public:
    2446      8122187 :   bool has_defn () const
    2447              :   {
    2448              :     /* Never consider TU-local entities as having definitions, since
    2449              :        we will never be accessing them from importers anyway.  */
    2450      8122187 :     return get_flag_bit<DB_DEFN_BIT> () && !is_tu_local ();
    2451              :   }
    2452              : 
    2453              : public:
    2454              :   /* This entity might be found other than by namespace-scope lookup;
    2455              :      see module_state::write_pendings for more details.  */
    2456      2010875 :   bool is_pending_entity () const
    2457              :   {
    2458      3213096 :     return (get_entity_kind () == EK_SPECIALIZATION
    2459      1202221 :             || get_entity_kind () == EK_PARTIAL
    2460      3179780 :             || (get_entity_kind () == EK_DECL
    2461      1134301 :                 && get_flag_bit<DB_IS_PENDING_BIT> ()));
    2462              :   }
    2463              : 
    2464              : public:
    2465              :   /* Only consider global module entities as being TU-local
    2466              :      when STRICT is set; otherwise, as an extension we support
    2467              :      emitting declarations referencing TU-local GMF entities
    2468              :      (and only check purview entities), to assist in migration.  */
    2469     42241492 :   bool is_tu_local (bool strict = false) const
    2470              :   {
    2471              :     /* Non-strict is only intended for migration purposes, so
    2472              :        for simplicity's sake we only care about whether this is
    2473              :        a non-purview variable or function at namespace scope;
    2474              :        these are the most common cases (coming from C), and
    2475              :        that way we don't have to care about diagnostics for
    2476              :        nested types and so forth.  */
    2477     18230088 :     tree inner = STRIP_TEMPLATE (get_entity ());
    2478     42241492 :     return (get_flag_bit<DB_TU_LOCAL_BIT> ()
    2479     42241492 :             && (strict
    2480         3260 :                 || !VAR_OR_FUNCTION_DECL_P (inner)
    2481         2130 :                 || !NAMESPACE_SCOPE_P (inner)
    2482         2103 :                 || (DECL_LANG_SPECIFIC (inner)
    2483         2064 :                     && DECL_MODULE_PURVIEW_P (inner))));
    2484              :   }
    2485      1305626 :   bool refs_tu_local (bool strict = false) const
    2486              :   {
    2487      1305626 :     return (get_flag_bit<DB_REF_PURVIEW_BIT> ()
    2488      1305626 :             || (strict && get_flag_bit <DB_REF_GLOBAL_BIT> ()));
    2489              :   }
    2490      2944308 :   bool is_exposure (bool strict = false) const
    2491              :   {
    2492      2944308 :     return (get_flag_bit<DB_EXPOSE_PURVIEW_BIT> ()
    2493      2944308 :             || (strict && get_flag_bit <DB_EXPOSE_GLOBAL_BIT> ()));
    2494              :   }
    2495      1976271 :   bool is_ignored_exposure_context () const
    2496              :   {
    2497      1976271 :     return get_flag_bit<DB_IGNORED_EXPOSURE_BIT> ();
    2498              :   }
    2499              : 
    2500              : public:
    2501     25807650 :   bool is_import () const
    2502              :   {
    2503      7889195 :     return get_flag_bit<DB_IMPORTED_BIT> ();
    2504              :   }
    2505     15676428 :   bool is_unreached () const
    2506              :   {
    2507      1017609 :     return get_flag_bit<DB_UNREACHED_BIT> ();
    2508              :   }
    2509      1472619 :   bool is_hidden () const
    2510              :   {
    2511      1472619 :     return get_flag_bit<DB_HIDDEN_BIT> ();
    2512              :   }
    2513       980347 :   bool is_maybe_recursive () const
    2514              :   {
    2515       980347 :     return get_flag_bit<DB_MAYBE_RECURSIVE_BIT> ();
    2516              :   }
    2517         1117 :   bool is_entry () const
    2518              :   {
    2519         1117 :     return get_flag_bit<DB_ENTRY_BIT> ();
    2520              :   }
    2521      1212903 :   bool is_type_spec () const
    2522              :   {
    2523      1212903 :     return get_flag_bit<DB_TYPE_SPEC_BIT> ();
    2524              :   }
    2525      1212903 :   bool is_friend_spec () const
    2526              :   {
    2527      1212903 :     return get_flag_bit<DB_FRIEND_SPEC_BIT> ();
    2528              :   }
    2529              : 
    2530              : public:
    2531              :   /* We set these bit outside of depset.  */
    2532           88 :   void set_hidden_binding ()
    2533              :   {
    2534           88 :     set_flag_bit<DB_HIDDEN_BIT> ();
    2535           88 :   }
    2536           36 :   void clear_hidden_binding ()
    2537              :   {
    2538           36 :     clear_flag_bit<DB_HIDDEN_BIT> ();
    2539           36 :   }
    2540              : 
    2541              : public:
    2542      9572418 :   bool is_special () const
    2543              :   {
    2544      9572418 :     return get_flag_bit<DB_SPECIAL_BIT> ();
    2545              :   }
    2546      1836151 :   void set_special ()
    2547              :   {
    2548      1836151 :     set_flag_bit<DB_SPECIAL_BIT> ();
    2549            0 :   }
    2550              : 
    2551              : public:
    2552    161823104 :   tree get_entity () const
    2553              :   {
    2554     42241492 :     return entity;
    2555              :   }
    2556     19699956 :   tree get_name () const
    2557              :   {
    2558     19699956 :     gcc_checking_assert (is_binding ());
    2559     19699956 :     return reinterpret_cast <tree> (discriminator);
    2560              :   }
    2561              : 
    2562              : public:
    2563              :   /* Traits for a hash table of pointers to bindings.  */
    2564              :   struct traits {
    2565              :     /* Each entry is a pointer to a depset. */
    2566              :     typedef depset *value_type;
    2567              :     /* We lookup by container:maybe-identifier pair.  */
    2568              :     typedef std::pair<tree,tree> compare_type;
    2569              : 
    2570              :     static const bool empty_zero_p = true;
    2571              : 
    2572              :     /* hash and equality for compare_type.  */
    2573     15968282 :     inline static hashval_t hash (const compare_type &p)
    2574              :     {
    2575     15968282 :       hashval_t h = pointer_hash<tree_node>::hash (p.first);
    2576     15968282 :       if (p.second)
    2577              :         {
    2578       192295 :           hashval_t nh = IDENTIFIER_HASH_VALUE (p.second);
    2579       192295 :           h = iterative_hash_hashval_t (h, nh);
    2580              :         }
    2581     15968282 :       return h;
    2582              :     }
    2583     86703746 :     inline static bool equal (const value_type b, const compare_type &p)
    2584              :     {
    2585     86703746 :       if (b->entity != p.first)
    2586              :         return false;
    2587              : 
    2588     11264218 :       if (p.second)
    2589        25253 :         return b->discriminator == reinterpret_cast<uintptr_t> (p.second);
    2590              :       else
    2591     11238965 :         return !b->is_binding ();
    2592              :     }
    2593              : 
    2594              :     /* (re)hasher for a binding itself.  */
    2595     67263139 :     inline static hashval_t hash (const value_type b)
    2596              :     {
    2597     67263139 :       hashval_t h = pointer_hash<tree_node>::hash (b->entity);
    2598     67263139 :       if (b->is_binding ())
    2599              :         {
    2600      5332392 :           hashval_t nh = IDENTIFIER_HASH_VALUE (b->get_name ());
    2601      5332392 :           h = iterative_hash_hashval_t (h, nh);
    2602              :         }
    2603     67263139 :       return h;
    2604              :     }
    2605              : 
    2606              :     /* Empty via NULL.  */
    2607            0 :     static inline void mark_empty (value_type &p) {p = NULL;}
    2608              :     static inline bool is_empty (value_type p) {return !p;}
    2609              : 
    2610              :     /* Nothing is deletable.  Everything is insertable.  */
    2611              :     static bool is_deleted (value_type) { return false; }
    2612              :     static void mark_deleted (value_type) { gcc_unreachable (); }
    2613              : 
    2614              :     /* We own the entities in the hash table.  */
    2615      2633382 :     static void remove (value_type p)
    2616              :     {
    2617      2633382 :       delete (p);
    2618      2633382 :     }
    2619              :   };
    2620              : 
    2621              : public:
    2622              :   class hash : public hash_table<traits> {
    2623              :     typedef traits::compare_type key_t;
    2624              :     typedef hash_table<traits> parent;
    2625              : 
    2626              :   public:
    2627              :     vec<depset *> worklist;  /* Worklist of decls to walk.  */
    2628              :     hash *chain;             /* Original table.  */
    2629              :     depset *current;         /* Current depset being depended.  */
    2630              :     unsigned section;        /* When writing out, the section.  */
    2631              :     bool reached_unreached;  /* We reached an unreached entity.  */
    2632              :     bool writing_merge_key;  /* We're writing merge key information.  */
    2633              : 
    2634              :   private:
    2635              :     bool ignore_exposure;    /* In a context where referencing a TU-local
    2636              :                                 entity is not an exposure.  */
    2637              : 
    2638              :   private:
    2639              :     /* Information needed to do dependent ADL for discovering
    2640              :        more decl-reachable entities.  Cached during walking to
    2641              :        prevent tree marking from interfering with lookup.  */
    2642      7015240 :     struct dep_adl_info {
    2643              :       /* The name of the call or operator.  */
    2644              :       tree name = NULL_TREE;
    2645              :       /* If not ERROR_MARK, a rewrite candidate for this operator.  */
    2646              :       tree_code rewrite = ERROR_MARK;
    2647              :       /* Argument list for the call.  */
    2648              :       vec<tree, va_gc>* args = make_tree_vector ();
    2649              :     };
    2650              :     vec<dep_adl_info> dep_adl_entity_list;
    2651              : 
    2652              :   public:
    2653       272395 :     hash (size_t size, hash *c = NULL)
    2654       544790 :       : parent (size), chain (c), current (NULL), section (0),
    2655       272395 :         reached_unreached (false), writing_merge_key (false),
    2656       272395 :         ignore_exposure (false)
    2657              :     {
    2658       272395 :       worklist.create (size);
    2659       272395 :       dep_adl_entity_list.create (16);
    2660       272395 :     }
    2661       272395 :     ~hash ()
    2662              :     {
    2663       272395 :       worklist.release ();
    2664       272395 :       dep_adl_entity_list.release ();
    2665       272395 :     }
    2666              : 
    2667              :   public:
    2668    115845640 :     bool is_key_order () const
    2669              :     {
    2670    115845640 :       return chain != NULL;
    2671              :     }
    2672              : 
    2673              :   public:
    2674              :     /* Returns a temporary override that will additionally consider this
    2675              :        to be a context where exposures of TU-local entities are ignored
    2676              :        if COND is true.  */
    2677       758783 :     temp_override<bool> ignore_exposure_if (bool cond)
    2678              :     {
    2679       592377 :       return make_temp_override (ignore_exposure, ignore_exposure || cond);
    2680              :     }
    2681              : 
    2682              :   private:
    2683              :     depset **entity_slot (tree entity, bool = true);
    2684              :     depset **binding_slot (tree ctx, tree name, bool = true);
    2685              :     depset *maybe_add_declaration (tree decl);
    2686              : 
    2687              :   public:
    2688              :     depset *find_dependency (tree entity);
    2689              :     depset *find_binding (tree ctx, tree name);
    2690              :     depset *make_dependency (tree decl, entity_kind);
    2691              :     void add_dependency (depset *);
    2692              : 
    2693              :   public:
    2694              :     void add_mergeable (depset *);
    2695              :     depset *add_dependency (tree decl, entity_kind);
    2696              :     void add_namespace_context (depset *, tree ns);
    2697              : 
    2698              :   private:
    2699              :     static bool add_binding_entity (tree, WMB_Flags, void *);
    2700              : 
    2701              :   public:
    2702              :     bool add_namespace_entities (tree ns, bitmap partitions);
    2703              :     void add_specializations (bool decl_p);
    2704              :     void add_partial_entities (vec<tree, va_gc> *);
    2705              :     void add_class_entities (vec<tree, va_gc> *);
    2706              :     void add_dependent_adl_entities (tree expr);
    2707              : 
    2708              :   private:
    2709              :     void add_deduction_guides (tree decl);
    2710              : 
    2711              :   public:
    2712              :     void find_dependencies (module_state *);
    2713              :     bool finalize_dependencies ();
    2714              :     vec<depset *> connect ();
    2715              : 
    2716              :   private:
    2717              :     bool diagnose_bad_internal_ref (depset *dep, bool strict = false);
    2718              :     bool diagnose_template_names_tu_local (depset *dep, bool strict = false);
    2719              :   };
    2720              : 
    2721              : public:
    2722              :   struct tarjan {
    2723              :     vec<depset *> result;
    2724              :     vec<depset *> stack;
    2725              :     unsigned index;
    2726              : 
    2727       272366 :     tarjan (unsigned size)
    2728       272366 :       : index (0)
    2729              :     {
    2730       272366 :       result.create (size);
    2731       272366 :       stack.create (50);
    2732       272366 :     }
    2733       272366 :     ~tarjan ()
    2734              :     {
    2735       272366 :       gcc_assert (!stack.length ());
    2736       272366 :       stack.release ();
    2737       272366 :     }
    2738              : 
    2739              :   public:
    2740              :     void connect (depset *);
    2741              :   };
    2742              : };
    2743              : 
    2744              : inline
    2745      2633382 : depset::depset (tree entity)
    2746      2633382 :   :entity (entity), discriminator (0), cluster (0), section (0)
    2747              : {
    2748      2633382 :   deps.create (0);
    2749              : }
    2750              : 
    2751              : inline
    2752      2633382 : depset::~depset ()
    2753              : {
    2754      2633382 :   deps.release ();
    2755      2633382 : }
    2756              : 
    2757              : const char *
    2758        44209 : depset::entity_kind_name () const
    2759              : {
    2760              :   /* Same order as entity_kind.  */
    2761        44209 :   static const char *const names[] =
    2762              :     {"decl", "specialization", "partial", "using",
    2763              :      "namespace", "tu-local", "redirect", "binding"};
    2764        44209 :   static_assert (ARRAY_SIZE (names) == EK_EXPLICIT_HWM + 1,
    2765              :                  "names must have an entry for every explicit entity_kind");
    2766        44209 :   entity_kind kind = get_entity_kind ();
    2767        44209 :   gcc_checking_assert (kind < ARRAY_SIZE (names));
    2768        44209 :   return names[kind];
    2769              : }
    2770              : 
    2771              : /* Create a depset for a namespace binding NS::NAME.  */
    2772              : 
    2773       143296 : depset *depset::make_binding (tree ns, tree name)
    2774              : {
    2775       143296 :   depset *binding = new depset (ns);
    2776              : 
    2777       143296 :   binding->discriminator = reinterpret_cast <uintptr_t> (name);
    2778              : 
    2779       143296 :   return binding;
    2780              : }
    2781              : 
    2782      2490086 : depset *depset::make_entity (tree entity, entity_kind ek, bool is_defn)
    2783              : {
    2784      2490086 :   depset *r = new depset (entity);
    2785              : 
    2786      2490086 :   r->discriminator = ((1 << DB_ZERO_BIT)
    2787      2490086 :                       | (ek << DB_KIND_BIT)
    2788      2490086 :                       | is_defn << DB_DEFN_BIT);
    2789              : 
    2790      2490086 :   return r;
    2791              : }
    2792              : 
    2793              : class pending_key
    2794              : {
    2795              : public:
    2796              :   tree ns;
    2797              :   tree id;
    2798              : };
    2799              : 
    2800              : template<>
    2801              : struct default_hash_traits<pending_key>
    2802              : {
    2803              :   using value_type = pending_key;
    2804              : 
    2805              :   static const bool empty_zero_p = false;
    2806     42602830 :   static hashval_t hash (const value_type &k)
    2807              :   {
    2808     42602830 :     hashval_t h = IDENTIFIER_HASH_VALUE (k.id);
    2809     42602830 :     h = iterative_hash_hashval_t (DECL_UID (k.ns), h);
    2810              : 
    2811     42602830 :     return h;
    2812              :   }
    2813     14256352 :   static bool equal (const value_type &k, const value_type &l)
    2814              :   {
    2815     14256352 :     return k.ns == l.ns && k.id == l.id;
    2816              :   }
    2817       225045 :   static void mark_empty (value_type &k)
    2818              :   {
    2819       225045 :     k.ns = k.id = NULL_TREE;
    2820              :   }
    2821         6045 :   static void mark_deleted (value_type &k)
    2822              :   {
    2823         6045 :     k.ns = NULL_TREE;
    2824         6045 :     gcc_checking_assert (k.id);
    2825         6045 :   }
    2826    308847452 :   static bool is_empty (const value_type &k)
    2827              :   {
    2828    308796570 :     return k.ns == NULL_TREE && k.id == NULL_TREE;
    2829              :   }
    2830     18907661 :   static bool is_deleted (const value_type &k)
    2831              :   {
    2832     18907661 :     return k.ns == NULL_TREE && k.id != NULL_TREE;
    2833              :   }
    2834              :   static void remove (value_type &)
    2835              :   {
    2836              :   }
    2837              : };
    2838              : 
    2839              : typedef hash_map<pending_key, auto_vec<unsigned>> pending_map_t;
    2840              : 
    2841              : /* Not-loaded entities that are keyed to a namespace-scope
    2842              :    identifier.  See module_state::write_pendings for details.  */
    2843              : pending_map_t *pending_table;
    2844              : 
    2845              : /* Decls that need some post processing once a batch of lazy loads has
    2846              :    completed.  */
    2847              : vec<tree, va_heap, vl_embed> *post_load_decls;
    2848              : 
    2849              : /* Some entities are keyed to another entitity for ODR purposes.
    2850              :    For example, at namespace scope, 'inline auto var = []{};', that
    2851              :    lambda is keyed to 'var', and follows its ODRness.  */
    2852              : typedef hash_map<tree, auto_vec<tree>> keyed_map_t;
    2853              : static keyed_map_t *keyed_table;
    2854              : 
    2855              : static tree get_keyed_decl_scope (tree);
    2856              : 
    2857              : /* Instantiations of temploid friends imported from another module
    2858              :    need to be attached to the same module as the temploid.  This maps
    2859              :    these decls to the temploid they are instantiated from, as there is
    2860              :    no other easy way to get this information.  */
    2861              : static GTY((cache)) decl_tree_cache_map *imported_temploid_friends;
    2862              : 
    2863              : /********************************************************************/
    2864              : /* Tree streaming.   The tree streaming is very specific to the tree
    2865              :    structures themselves.  A tag indicates the kind of tree being
    2866              :    streamed.  -ve tags indicate backreferences to already-streamed
    2867              :    trees.  Backreferences are auto-numbered.  */
    2868              : 
    2869              : /* Tree tags.  */
    2870              : enum tree_tag {
    2871              :   tt_null,              /* NULL_TREE.  */
    2872              :   tt_tu_local,          /* A TU-local entity.  */
    2873              :   tt_fixed,             /* Fixed vector index.  */
    2874              : 
    2875              :   tt_node,              /* By-value node.  */
    2876              :   tt_decl,              /* By-value mergeable decl.  */
    2877              :   tt_tpl_parm,          /* Template parm.  */
    2878              : 
    2879              :   /* The ordering of the following 5 is relied upon in
    2880              :      trees_out::tree_node.  */
    2881              :   tt_id,                /* Identifier node.  */
    2882              :   tt_conv_id,           /* Conversion operator name.  */
    2883              :   tt_anon_id,           /* Anonymous name.  */
    2884              :   tt_lambda_id,         /* Lambda name.  */
    2885              :   tt_internal_id,       /* Internal name.  */
    2886              : 
    2887              :   tt_typedef_type,      /* A (possibly implicit) typedefed type.  */
    2888              :   tt_derived_type,      /* A type derived from another type.  */
    2889              :   tt_variant_type,      /* A variant of another type.  */
    2890              : 
    2891              :   tt_tinfo_var,         /* Typeinfo object. */
    2892              :   tt_tinfo_typedef,     /* Typeinfo typedef.  */
    2893              :   tt_ptrmem_type,       /* Pointer to member type.  */
    2894              :   tt_nttp_var,          /* NTTP_OBJECT VAR_DECL.  */
    2895              : 
    2896              :   tt_parm,              /* Function parameter or result.  */
    2897              :   tt_enum_value,        /* An enum value.  */
    2898              :   tt_enum_decl,         /* An enum decl.  */
    2899              :   tt_data_member,       /* Data member/using-decl.  */
    2900              : 
    2901              :   tt_binfo,             /* A BINFO.  */
    2902              :   tt_vtable,            /* A vtable.  */
    2903              :   tt_thunk,             /* A thunk.  */
    2904              :   tt_clone_ref,
    2905              : 
    2906              :   tt_entity,            /* A extra-cluster entity.  */
    2907              : 
    2908              :   tt_template,          /* The TEMPLATE_RESULT of a template.  */
    2909              : };
    2910              : 
    2911              : enum walk_kind {
    2912              :   WK_none,      /* No walk to do (a back- or fixed-ref happened).  */
    2913              :   WK_normal,    /* Normal walk (by-name if possible).  */
    2914              : 
    2915              :   WK_value,     /* By-value walk.  */
    2916              : };
    2917              : 
    2918              : enum merge_kind
    2919              : {
    2920              :   MK_unique,    /* Known unique.  */
    2921              :   MK_named,     /* Found by CTX, NAME + maybe_arg types etc.  */
    2922              :   MK_field,     /* Found by CTX and index on TYPE_FIELDS  */
    2923              :   MK_vtable,    /* Found by CTX and index on TYPE_VTABLES  */
    2924              :   MK_as_base,   /* Found by CTX.  */
    2925              : 
    2926              :   MK_partial,
    2927              : 
    2928              :   MK_enum,      /* Found by CTX, & 1stMemberNAME.  */
    2929              :   MK_keyed,     /* Found by key & index.  */
    2930              :   MK_local_type, /* Found by CTX, index.  */
    2931              : 
    2932              :   MK_friend_spec,  /* Like named, but has a tmpl & args too.  */
    2933              :   MK_local_friend, /* Found by CTX, index.  */
    2934              : 
    2935              :   MK_indirect_lwm = MK_enum,
    2936              : 
    2937              :   /* Template specialization kinds below. These are all found via
    2938              :      primary template and specialization args.  */
    2939              :   MK_template_mask = 0x10,  /* A template specialization.  */
    2940              : 
    2941              :   MK_tmpl_decl_mask = 0x4, /* In decl table.  */
    2942              : 
    2943              :   MK_tmpl_tmpl_mask = 0x1, /* We want TEMPLATE_DECL.  */
    2944              : 
    2945              :   MK_type_spec = MK_template_mask,
    2946              :   MK_decl_spec = MK_template_mask | MK_tmpl_decl_mask,
    2947              : 
    2948              :   MK_hwm = 0x20
    2949              : };
    2950              : /* This is more than a debugging array.  NULLs are used to determine
    2951              :    an invalid merge_kind number.  */
    2952              : static char const *const merge_kind_name[MK_hwm] =
    2953              :   {
    2954              :     "unique", "named", "field", "vtable",       /* 0...3  */
    2955              :     "asbase", "partial", "enum", "attached",    /* 4...7  */
    2956              : 
    2957              :     "local type", "friend spec", "local friend", NULL,  /* 8...11 */
    2958              :     NULL, NULL, NULL, NULL,
    2959              : 
    2960              :     "type spec", "type tmpl spec",  /* 16,17 type (template).  */
    2961              :     NULL, NULL,
    2962              : 
    2963              :     "decl spec", "decl tmpl spec",  /* 20,21 decl (template).  */
    2964              :     NULL, NULL,
    2965              :     NULL, NULL, NULL, NULL,
    2966              :     NULL, NULL, NULL, NULL,
    2967              :   };
    2968              : 
    2969              : /* Mergeable entity location data.  */
    2970              : struct merge_key {
    2971              :   cp_ref_qualifier ref_q : 2;
    2972              :   unsigned coro_disc : 2;  /* Discriminator for coroutine transforms.  */
    2973              :   unsigned index;
    2974              : 
    2975              :   tree ret;  /* Return type, if appropriate.  */
    2976              :   tree args; /* Arg types, if appropriate.  */
    2977              : 
    2978              :   tree constraints;  /* Constraints.  */
    2979              : 
    2980      2640871 :   merge_key ()
    2981      2640871 :     :ref_q (REF_QUAL_NONE), coro_disc (0), index (0),
    2982      2640871 :      ret (NULL_TREE), args (NULL_TREE),
    2983      2640871 :      constraints (NULL_TREE)
    2984              :   {
    2985              :   }
    2986              : };
    2987              : 
    2988              : /* Hashmap of merged duplicates.  Usually decls, but can contain
    2989              :    BINFOs.  */
    2990              : typedef hash_map<tree,uintptr_t,
    2991              :                  simple_hashmap_traits<nodel_ptr_hash<tree_node>,uintptr_t> >
    2992              : duplicate_hash_map;
    2993              : 
    2994              : /* Data needed for post-processing.  */
    2995              : struct post_process_data {
    2996              :   tree decl;
    2997              :   location_t start_locus;
    2998              :   location_t end_locus;
    2999              :   bool returns_value;
    3000              :   bool returns_null;
    3001              :   bool returns_abnormally;
    3002              :   bool infinite_loop;
    3003              : };
    3004              : 
    3005              : /* Tree stream reader.  Note that reading a stream doesn't mark the
    3006              :    read trees with TREE_VISITED.  Thus it's quite safe to have
    3007              :    multiple concurrent readers.  Which is good, because lazy
    3008              :    loading.
    3009              : 
    3010              :    It's important that trees_in/out have internal linkage so that the
    3011              :    compiler knows core_bools, lang_type_bools and lang_decl_bools have
    3012              :    only a single caller (tree_node_bools) and inlines them appropriately.  */
    3013              : namespace {
    3014              : class trees_in : public bytes_in {
    3015              :   typedef bytes_in parent;
    3016              : 
    3017              : private:
    3018              :   module_state *state;          /* Module being imported.  */
    3019              :   vec<tree> back_refs;            /* Back references.  */
    3020              :   duplicate_hash_map *duplicates;       /* Map from existings to duplicate.  */
    3021              :   vec<post_process_data> post_decls;      /* Decls to post process.  */
    3022              :   vec<tree> post_types;           /* Types to post process.  */
    3023              :   unsigned unused;              /* Inhibit any interior TREE_USED
    3024              :                                    marking.  */
    3025              : 
    3026              : public:
    3027              :   trees_in (module_state *);
    3028              :   ~trees_in ();
    3029              : 
    3030              : public:
    3031              :   int insert (tree);
    3032              :   tree back_ref (int);
    3033              : 
    3034              : private:
    3035              :   tree start (unsigned = 0);
    3036              : 
    3037              : public:
    3038              :   /* Needed for binfo writing  */
    3039              :   bool core_bools (tree, bits_in&);
    3040              : 
    3041              : private:
    3042              :   /* Stream tree_core, lang_decl_specific and lang_type_specific
    3043              :      bits.  */
    3044              :   bool core_vals (tree);
    3045              :   bool lang_type_bools (tree, bits_in&);
    3046              :   bool lang_type_vals (tree);
    3047              :   bool lang_decl_bools (tree, bits_in&);
    3048              :   bool lang_decl_vals (tree);
    3049              :   bool lang_vals (tree);
    3050              :   bool tree_node_bools (tree);
    3051              :   bool tree_node_vals (tree);
    3052              :   tree tree_value ();
    3053              :   tree decl_value ();
    3054              :   tree tpl_parm_value ();
    3055              : 
    3056              : private:
    3057              :   tree chained_decls ();  /* Follow DECL_CHAIN.  */
    3058              :   vec<tree, va_heap> *vec_chained_decls ();
    3059              :   vec<tree, va_gc> *tree_vec (); /* vec of tree.  */
    3060              :   vec<tree_pair_s, va_gc> *tree_pair_vec (); /* vec of tree_pair.  */
    3061              :   tree tree_list (bool has_purpose);
    3062              : 
    3063              : public:
    3064              :   /* Read a tree node.  */
    3065              :   tree tree_node (bool is_use = false);
    3066              : 
    3067              : private:
    3068              :   bool install_entity (tree decl);
    3069              :   tree tpl_parms (unsigned &tpl_levels);
    3070              :   bool tpl_parms_fini (tree decl, unsigned tpl_levels);
    3071              :   bool tpl_header (tree decl, unsigned *tpl_levels);
    3072              :   int fn_parms_init (tree);
    3073              :   void fn_parms_fini (int tag, tree fn, tree existing, bool has_defn);
    3074              :   unsigned add_indirect_tpl_parms (tree);
    3075              : public:
    3076              :   bool add_indirects (tree);
    3077              : 
    3078              : public:
    3079              :   /* Serialize various definitions. */
    3080              :   bool read_definition (tree decl);
    3081              : 
    3082              : private:
    3083              :   void check_abi_tags (tree existing, tree decl, tree &eattr, tree &dattr);
    3084              :   bool is_matching_decl (tree existing, tree decl, bool is_typedef);
    3085              :   static bool install_implicit_member (tree decl);
    3086              :   bool read_function_def (tree decl, tree maybe_template);
    3087              :   bool read_var_def (tree decl, tree maybe_template);
    3088              :   bool read_class_def (tree decl, tree maybe_template);
    3089              :   bool read_enum_def (tree decl, tree maybe_template);
    3090              : 
    3091              : public:
    3092              :   tree decl_container ();
    3093              :   tree key_mergeable (int tag, merge_kind, tree decl, tree inner, tree type,
    3094              :                       tree container, bool is_attached,
    3095              :                       bool is_imported_temploid_friend);
    3096              :   unsigned binfo_mergeable (tree *);
    3097              : 
    3098              : private:
    3099              :   tree key_local_type (const merge_key&, tree, tree);
    3100              :   uintptr_t *find_duplicate (tree existing);
    3101              :   void register_duplicate (tree decl, tree existing);
    3102              :   /* Mark as an already diagnosed bad duplicate.  */
    3103           54 :   void unmatched_duplicate (tree existing)
    3104              :   {
    3105          108 :     *find_duplicate (existing) |= 1;
    3106           54 :   }
    3107              : 
    3108              : public:
    3109       134511 :   bool is_duplicate (tree decl)
    3110              :   {
    3111       269022 :     return find_duplicate (decl) != NULL;
    3112              :   }
    3113       159799 :   tree maybe_duplicate (tree decl)
    3114              :   {
    3115       159799 :     if (uintptr_t *dup = find_duplicate (decl))
    3116         8897 :       return reinterpret_cast<tree> (*dup & ~uintptr_t (1));
    3117              :     return decl;
    3118              :   }
    3119              :   tree odr_duplicate (tree decl, bool has_defn);
    3120              : 
    3121              : public:
    3122              :   /* Return the decls to postprocess.  */
    3123              :   const vec<post_process_data>& post_process ()
    3124              :   {
    3125              :     return post_decls;
    3126              :   }
    3127              :   /* Return the types to postprocess.  */
    3128              :   const vec<tree>& post_process_type ()
    3129              :   {
    3130              :     return post_types;
    3131              :   }
    3132              : private:
    3133              :   /* Register DATA for postprocessing.  */
    3134       138524 :   void post_process (post_process_data data)
    3135              :   {
    3136       138524 :     post_decls.safe_push (data);
    3137              :   }
    3138              :   /* Register TYPE for postprocessing.  */
    3139           36 :   void post_process_type (tree type)
    3140              :   {
    3141           36 :     gcc_checking_assert (TYPE_P (type));
    3142           36 :     post_types.safe_push (type);
    3143           36 :   }
    3144              : 
    3145              : private:
    3146              :   void assert_definition (tree, bool installing);
    3147              : };
    3148              : } // anon namespace
    3149              : 
    3150       210668 : trees_in::trees_in (module_state *state)
    3151       210668 :   :parent (), state (state), unused (0)
    3152              : {
    3153       210668 :   duplicates = NULL;
    3154       210668 :   back_refs.create (500);
    3155       210668 :   post_decls.create (0);
    3156       210668 :   post_types.create (0);
    3157       210668 : }
    3158              : 
    3159       210668 : trees_in::~trees_in ()
    3160              : {
    3161       313453 :   delete (duplicates);
    3162       210668 :   back_refs.release ();
    3163       210668 :   post_decls.release ();
    3164       210668 :   post_types.release ();
    3165       210668 : }
    3166              : 
    3167              : /* Tree stream writer.  */
    3168              : namespace {
    3169              : class trees_out : public bytes_out {
    3170              :   typedef bytes_out parent;
    3171              : 
    3172              : private:
    3173              :   module_state *state;          /* The module we are writing.  */
    3174              :   ptr_int_hash_map tree_map;    /* Trees to references */
    3175              :   depset::hash *dep_hash;       /* Dependency table.  */
    3176              :   int ref_num;                  /* Back reference number.  */
    3177              :   unsigned section;
    3178              :   bool writing_local_entities;  /* Whether we might walk into a TU-local
    3179              :                                    entity we need to emit placeholders for.  */
    3180              :   bool walking_bit_field_unit;  /* Whether we're walking the underlying
    3181              :                                    storage for a bit field.  There's no other
    3182              :                                    great way to detect this.  */
    3183              : #if CHECKING_P
    3184              :   int importedness;             /* Checker that imports not occurring
    3185              :                                    inappropriately.  +ve imports ok,
    3186              :                                    -ve imports not ok.  */
    3187              : #endif
    3188              : 
    3189              : public:
    3190              :   trees_out (allocator *, module_state *, depset::hash &deps, unsigned sec = 0);
    3191              :   ~trees_out ();
    3192              : 
    3193              : private:
    3194              :   void mark_trees ();
    3195              :   void unmark_trees ();
    3196              : 
    3197              : public:
    3198              :   /* Hey, let's ignore the well known STL iterator idiom.  */
    3199              :   void begin ();
    3200              :   unsigned end (elf_out *sink, unsigned name, unsigned *crc_ptr);
    3201              :   void end ();
    3202              : 
    3203              : public:
    3204              :   enum tags
    3205              :   {
    3206              :     tag_backref = -1,   /* Upper bound on the backrefs.  */
    3207              :     tag_value = 0,      /* Write by value.  */
    3208              :     tag_fixed           /* Lower bound on the fixed trees.  */
    3209              :   };
    3210              : 
    3211              : public:
    3212              :   /* The walk is used for three similar purposes:
    3213              : 
    3214              :       1. The initial scan for dependencies.
    3215              :       2. Once dependencies have been found, ordering them.
    3216              :       3. Writing dependencies to file (streaming_p).
    3217              : 
    3218              :      For cases where it matters, these accessers can be used to determine
    3219              :      which state we're in.  */
    3220     94515830 :   bool is_initial_scan () const
    3221              :   {
    3222    156937922 :     return !streaming_p () && !is_key_order ();
    3223              :   }
    3224     77399254 :   bool is_key_order () const
    3225              :   {
    3226     62422092 :     return dep_hash->is_key_order ();
    3227              :   }
    3228              : 
    3229              : public:
    3230              :   int insert (tree, walk_kind = WK_normal);
    3231              : 
    3232              : private:
    3233              :   void start (tree, bool = false);
    3234              : 
    3235              : private:
    3236              :   walk_kind ref_node (tree);
    3237              : public:
    3238              :   int get_tag (tree);
    3239       539306 :   void set_importing (int i ATTRIBUTE_UNUSED)
    3240              :   {
    3241              : #if CHECKING_P
    3242       539306 :     importedness = i;
    3243              : #endif
    3244              :   }
    3245              : 
    3246              : private:
    3247              :   void core_bools (tree, bits_out&);
    3248              :   void core_vals (tree);
    3249              :   void lang_type_bools (tree, bits_out&);
    3250              :   void lang_type_vals (tree);
    3251              :   void lang_decl_bools (tree, bits_out&);
    3252              :   void lang_decl_vals (tree);
    3253              :   void lang_vals (tree);
    3254              :   void tree_node_bools (tree);
    3255              :   void tree_node_vals (tree);
    3256              : 
    3257              : private:
    3258              :   void chained_decls (tree);
    3259              :   void vec_chained_decls (tree);
    3260              :   void tree_vec (vec<tree, va_gc> *);
    3261              :   void tree_pair_vec (vec<tree_pair_s, va_gc> *);
    3262              :   void tree_list (tree, bool has_purpose);
    3263              : 
    3264              : private:
    3265              :   bool has_tu_local_dep (tree) const;
    3266              :   tree find_tu_local_decl (tree);
    3267              : 
    3268              : public:
    3269              :   /* Mark a node for by-value walking.  */
    3270              :   void mark_by_value (tree);
    3271              : 
    3272              : public:
    3273              :   void tree_node (tree);
    3274              : 
    3275              : private:
    3276              :   void install_entity (tree decl, depset *);
    3277              :   void tpl_parms (tree parms, unsigned &tpl_levels);
    3278              :   void tpl_parms_fini (tree decl, unsigned tpl_levels);
    3279              :   void fn_parms_fini (tree) {}
    3280              :   unsigned add_indirect_tpl_parms (tree);
    3281              : public:
    3282              :   void add_indirects (tree);
    3283              :   void fn_parms_init (tree);
    3284              :   void tpl_header (tree decl, unsigned *tpl_levels);
    3285              : 
    3286              : public:
    3287              :   merge_kind get_merge_kind (tree decl, depset *maybe_dep);
    3288              :   tree decl_container (tree decl);
    3289              :   void key_mergeable (int tag, merge_kind, tree decl, tree inner,
    3290              :                       tree container, depset *maybe_dep);
    3291              :   void binfo_mergeable (tree binfo);
    3292              : 
    3293              : private:
    3294              :   void key_local_type (merge_key&, tree, tree);
    3295              :   bool decl_node (tree, walk_kind ref);
    3296              :   void type_node (tree);
    3297              :   void tree_value (tree);
    3298              :   void tpl_parm_value (tree);
    3299              : 
    3300              : public:
    3301              :   void decl_value (tree, depset *);
    3302              : 
    3303              : public:
    3304              :   /* Serialize various definitions. */
    3305              :   void write_definition (tree decl, bool refs_tu_local = false);
    3306              :   void mark_declaration (tree decl, bool do_defn);
    3307              : 
    3308              : private:
    3309              :   void mark_function_def (tree decl);
    3310              :   void mark_var_def (tree decl);
    3311              :   void mark_class_def (tree decl);
    3312              :   void mark_enum_def (tree decl);
    3313              :   void mark_class_member (tree decl, bool do_defn = true);
    3314              :   void mark_binfos (tree type);
    3315              : 
    3316              : private:
    3317              :   void write_var_def (tree decl);
    3318              :   void write_function_def (tree decl);
    3319              :   void write_class_def (tree decl);
    3320              :   void write_enum_def (tree decl);
    3321              : 
    3322              : private:
    3323              :   static void assert_definition (tree);
    3324              : 
    3325              : public:
    3326              :   static void instrument ();
    3327              : 
    3328              : private:
    3329              :   /* Tree instrumentation. */
    3330              :   static unsigned tree_val_count;
    3331              :   static unsigned decl_val_count;
    3332              :   static unsigned back_ref_count;
    3333              :   static unsigned tu_local_count;
    3334              :   static unsigned null_count;
    3335              : };
    3336              : } // anon namespace
    3337              : 
    3338              : /* Instrumentation counters.  */
    3339              : unsigned trees_out::tree_val_count;
    3340              : unsigned trees_out::decl_val_count;
    3341              : unsigned trees_out::back_ref_count;
    3342              : unsigned trees_out::tu_local_count;
    3343              : unsigned trees_out::null_count;
    3344              : 
    3345       544806 : trees_out::trees_out (allocator *mem, module_state *state, depset::hash &deps,
    3346       544806 :                       unsigned section)
    3347      1089612 :   :parent (mem), state (state), tree_map (500),
    3348       544806 :    dep_hash (&deps), ref_num (0), section (section),
    3349       544806 :    writing_local_entities (false), walking_bit_field_unit (false)
    3350              : {
    3351              : #if CHECKING_P
    3352       544806 :   importedness = 0;
    3353              : #endif
    3354       544806 : }
    3355              : 
    3356       544806 : trees_out::~trees_out ()
    3357              : {
    3358       544806 : }
    3359              : 
    3360              : /********************************************************************/
    3361              : /* Location.  We're aware of the line-map concept and reproduce it
    3362              :    here.  Each imported module allocates a contiguous span of ordinary
    3363              :    maps, and of macro maps.  adhoc maps are serialized by contents,
    3364              :    not pre-allocated.   The scattered linemaps of a module are
    3365              :    coalesced when writing.  */
    3366              : 
    3367              : 
    3368              : /* I use half-open [first,second) ranges.  */
    3369              : typedef std::pair<line_map_uint_t,line_map_uint_t> range_t;
    3370              : 
    3371              : /* A range of locations.  */
    3372              : typedef std::pair<location_t,location_t> loc_range_t;
    3373              : 
    3374              : /* Spans of the line maps that are occupied by this TU.  I.e. not
    3375              :    within imports.  Only extended when in an interface unit.
    3376              :    Interval zero corresponds to the forced header linemap(s).  This
    3377              :    is a singleton object.  */
    3378              : 
    3379              : class loc_spans {
    3380              : public:
    3381              :   /* An interval of line maps.  The line maps here represent a contiguous
    3382              :      non-imported range.  */
    3383         5914 :   struct span {
    3384              :     loc_range_t ordinary;       /* Ordinary map location range. */
    3385              :     loc_range_t macro;          /* Macro map location range.  */
    3386              :     /* Add to locs to get serialized loc.  */
    3387              :     location_diff_t ordinary_delta;
    3388              :     location_diff_t macro_delta;
    3389              :   };
    3390              : 
    3391              : private:
    3392              :   vec<span> *spans;
    3393              :   bool locs_exhausted_p;
    3394              : 
    3395              : public:
    3396              :   loc_spans ()
    3397              :     /* Do not preallocate spans, as that causes
    3398              :        --enable-detailed-mem-stats problems.  */
    3399              :     : spans (nullptr), locs_exhausted_p (false)
    3400              :   {
    3401              :   }
    3402        98419 :   ~loc_spans ()
    3403              :   {
    3404        98419 :     delete spans;
    3405        98419 :   }
    3406              : 
    3407              : public:
    3408          300 :   span &operator[] (unsigned ix)
    3409              :   {
    3410          600 :     return (*spans)[ix];
    3411              :   }
    3412              :   unsigned length () const
    3413              :   {
    3414              :     return spans->length ();
    3415              :   }
    3416              : 
    3417              : public:
    3418        15471 :   bool init_p () const
    3419              :   {
    3420        15471 :     return spans != nullptr;
    3421              :   }
    3422              :   /* Initializer.  */
    3423              :   void init (const line_maps *lmaps, const line_map_ordinary *map);
    3424              : 
    3425              :   /* Slightly skewed preprocessed files can cause us to miss an
    3426              :      initialization in some places.  Fallback initializer.  */
    3427         5742 :   void maybe_init ()
    3428              :   {
    3429         5742 :     if (!init_p ())
    3430            6 :       init (line_table, nullptr);
    3431         5742 :   }
    3432              : 
    3433              : public:
    3434              :   enum {
    3435              :     SPAN_RESERVED = 0,  /* Reserved (fixed) locations.  */
    3436              :     SPAN_FIRST = 1,     /* LWM of locations to stream  */
    3437              :     SPAN_MAIN = 2       /* Main file and onwards.  */
    3438              :   };
    3439              : 
    3440              : public:
    3441       455525 :   location_t main_start () const
    3442              :   {
    3443       455525 :     return (*spans)[SPAN_MAIN].ordinary.first;
    3444              :   }
    3445              : 
    3446              : public:
    3447              :   void open (location_t);
    3448              :   void close ();
    3449              : 
    3450              : public:
    3451              :   /* Propagate imported linemaps to us, if needed.  */
    3452              :   bool maybe_propagate (module_state *import, location_t loc);
    3453              : 
    3454              : public:
    3455              :   /* Whether we can no longer represent new imported locations.  */
    3456            0 :   bool locations_exhausted_p () const
    3457              :   {
    3458            0 :     return locs_exhausted_p;
    3459              :   }
    3460            0 :   void report_location_exhaustion (location_t loc)
    3461              :   {
    3462            0 :     if (!locs_exhausted_p)
    3463              :       {
    3464              :         /* Just give the notice once.  */
    3465            0 :         locs_exhausted_p = true;
    3466            0 :         inform (loc, "unable to represent further imported source locations");
    3467              :       }
    3468              :   }
    3469              : 
    3470              : public:
    3471              :   const span *ordinary (location_t);
    3472              :   const span *macro (location_t);
    3473              : };
    3474              : 
    3475              : static loc_spans spans;
    3476              : 
    3477              : /* Information about ordinary locations we stream out.  */
    3478              : struct ord_loc_info
    3479              : {
    3480              :   const line_map_ordinary *src; // line map we're based on
    3481              :   line_map_uint_t offset;       // offset to this line
    3482              :   line_map_uint_t span;         // number of locs we span
    3483              :   line_map_uint_t remap;        // serialization
    3484              : 
    3485    253849696 :   static int compare (const void *a_, const void *b_)
    3486              :   {
    3487    253849696 :     auto *a = static_cast<const ord_loc_info *> (a_);
    3488    253849696 :     auto *b = static_cast<const ord_loc_info *> (b_);
    3489              : 
    3490    253849696 :     if (a->src != b->src)
    3491     56089329 :       return a->src < b->src ? -1 : +1;
    3492              : 
    3493              :     // Ensure no overlap
    3494    215924668 :     gcc_checking_assert (a->offset + a->span <= b->offset
    3495              :                          || b->offset + b->span <= a->offset);
    3496              : 
    3497    215924668 :     gcc_checking_assert (a->offset != b->offset);
    3498    215924668 :     return a->offset < b->offset ? -1 : +1;
    3499              :   }
    3500              : };
    3501              : struct ord_loc_traits
    3502              : {
    3503              :   typedef ord_loc_info value_type;
    3504              :   typedef value_type compare_type;
    3505              : 
    3506              :   static const bool empty_zero_p = false;
    3507              : 
    3508    105239970 :   static hashval_t hash (const value_type &v)
    3509              :   {
    3510     89121428 :     auto h = pointer_hash<const line_map_ordinary>::hash (v.src);
    3511    105239970 :     return iterative_hash_hashval_t (v.offset, h);
    3512              :   }
    3513    116008618 :   static bool equal (const value_type &v, const compare_type p)
    3514              :   {
    3515    116008618 :     return v.src == p.src && v.offset == p.offset;
    3516              :   }
    3517              : 
    3518      7697616 :   static void mark_empty (value_type &v)
    3519              :   {
    3520      7697616 :     v.src = nullptr;
    3521              :   }
    3522    340945185 :   static bool is_empty (value_type &v)
    3523              :   {
    3524    340945185 :     return !v.src;
    3525              :   }
    3526              : 
    3527              :   static bool is_deleted (value_type &) { return false; }
    3528              :   static void mark_deleted (value_type &) { gcc_unreachable (); }
    3529              : 
    3530      1898304 :   static void remove (value_type &) {}
    3531              : };
    3532              : /* Table keyed by ord_loc_info, used for noting.  */
    3533              : static  hash_table<ord_loc_traits> *ord_loc_table;
    3534              : /* Sorted vector, used for writing.  */
    3535              : static vec<ord_loc_info> *ord_loc_remap;
    3536              : 
    3537              : /* Information about macro locations we stream out.  */
    3538              : struct macro_loc_info
    3539              : {
    3540              :   const line_map_macro *src;    // original expansion
    3541              :   line_map_uint_t remap;        // serialization
    3542              : 
    3543      8325618 :   static int compare (const void *a_, const void *b_)
    3544              :   {
    3545      8325618 :     auto *a = static_cast<const macro_loc_info *> (a_);
    3546      8325618 :     auto *b = static_cast<const macro_loc_info *> (b_);
    3547              : 
    3548      8325618 :     gcc_checking_assert (MAP_START_LOCATION (a->src)
    3549              :                          != MAP_START_LOCATION (b->src));
    3550      8325618 :     if (MAP_START_LOCATION (a->src) < MAP_START_LOCATION (b->src))
    3551              :       return -1;
    3552              :     else
    3553      4008835 :       return +1;
    3554              :   }
    3555              : };
    3556              : struct macro_loc_traits
    3557              : {
    3558              :   typedef macro_loc_info value_type;
    3559              :   typedef const line_map_macro *compare_type;
    3560              : 
    3561              :   static const bool empty_zero_p = false;
    3562              : 
    3563      8536445 :   static hashval_t hash (compare_type p)
    3564              :   {
    3565      8536445 :     return pointer_hash<const line_map_macro>::hash (p);
    3566              :   }
    3567      7237317 :   static hashval_t hash (const value_type &v)
    3568              :   {
    3569      7237317 :     return hash (v.src);
    3570              :   }
    3571              :   static bool equal (const value_type &v, const compare_type p)
    3572              :   {
    3573              :     return v.src == p;
    3574              :   }
    3575              : 
    3576       651642 :   static void mark_empty (value_type &v)
    3577              :   {
    3578       651642 :     v.src = nullptr;
    3579              :   }
    3580     27080816 :   static bool is_empty (value_type &v)
    3581              :   {
    3582     27080816 :     return !v.src;
    3583              :   }
    3584              : 
    3585              :   static bool is_deleted (value_type &) { return false; }
    3586              :   static void mark_deleted (value_type &) { gcc_unreachable (); }
    3587              : 
    3588       142666 :   static void remove (value_type &) {}
    3589              : };
    3590              : /* Table keyed by line_map_macro, used for noting.  */
    3591              : static  hash_table<macro_loc_traits> *macro_loc_table;
    3592              : /* Sorted vector, used for writing.  */
    3593              : static vec<macro_loc_info> *macro_loc_remap;
    3594              : 
    3595              : /* Indirection to allow bsearching imports by ordinary location.  */
    3596              : static vec<module_state *> *ool;
    3597              : 
    3598              : /********************************************************************/
    3599              : /* Data needed by a module during the process of loading.  */
    3600              : struct GTY(()) slurping {
    3601              : 
    3602              :   /* Remap import's module numbering to our numbering.  Values are
    3603              :      shifted by 1.  Bit0 encodes if the import is direct.  */
    3604              :   vec<unsigned, va_heap, vl_embed> *
    3605              :     GTY((skip)) remap;                  /* Module owner remapping.  */
    3606              : 
    3607              :   elf_in *GTY((skip)) from;             /* The elf loader.  */
    3608              : 
    3609              :   /* This map is only for header imports themselves -- the global
    3610              :      headers bitmap hold it for the current TU.  */
    3611              :   bitmap headers;       /* Transitive set of direct imports, including
    3612              :                            self.  Used for macro visibility and
    3613              :                            priority.  */
    3614              : 
    3615              :   /* These objects point into the mmapped area, unless we're not doing
    3616              :      that, or we got frozen or closed.  In those cases they point to
    3617              :      buffers we own.  */
    3618              :   bytes_in macro_defs;  /* Macro definitions.  */
    3619              :   bytes_in macro_tbl;   /* Macro table.  */
    3620              : 
    3621              :   /* Location remapping.  first->ordinary, second->macro.  */
    3622              :   range_t GTY((skip)) loc_deltas;
    3623              : 
    3624              :   unsigned current;     /* Section currently being loaded.  */
    3625              :   unsigned remaining;   /* Number of lazy sections yet to read.  */
    3626              :   unsigned lru;         /* An LRU counter.  */
    3627              : 
    3628              :  public:
    3629              :   slurping (elf_in *);
    3630              :   ~slurping ();
    3631              : 
    3632              :  public:
    3633              :   /* Close the ELF file, if it's open.  */
    3634         5298 :   void close ()
    3635              :   {
    3636         5298 :     if (from)
    3637              :       {
    3638         2907 :         from->end ();
    3639         5814 :         delete from;
    3640         2907 :         from = NULL;
    3641              :       }
    3642         5298 :   }
    3643              : 
    3644              :  public:
    3645              :   void release_macros ();
    3646              : 
    3647              :  public:
    3648         2965 :   void alloc_remap (unsigned size)
    3649              :   {
    3650         2965 :     gcc_assert (!remap);
    3651         2965 :     vec_safe_reserve (remap, size);
    3652         6322 :     for (unsigned ix = size; ix--;)
    3653         3357 :       remap->quick_push (0);
    3654         2965 :   }
    3655      1202242 :   unsigned remap_module (unsigned owner)
    3656              :   {
    3657      1202242 :     if (owner < remap->length ())
    3658      1202242 :       return (*remap)[owner] >> 1;
    3659              :     return 0;
    3660              :   }
    3661              : 
    3662              :  public:
    3663              :   /* GC allocation.  But we must explicitly delete it.   */
    3664         2989 :   static void *operator new (size_t x)
    3665              :   {
    3666         5978 :     return ggc_alloc_atomic (x);
    3667              :   }
    3668         2907 :   static void operator delete (void *p)
    3669              :   {
    3670         2907 :     ggc_free (p);
    3671         2907 :   }
    3672              : };
    3673              : 
    3674         2989 : slurping::slurping (elf_in *from)
    3675         2989 :   : remap (NULL), from (from),
    3676         2989 :     headers (BITMAP_GGC_ALLOC ()), macro_defs (), macro_tbl (),
    3677         2989 :     loc_deltas (0, 0),
    3678         2989 :     current (~0u), remaining (0), lru (0)
    3679              : {
    3680         2989 : }
    3681              : 
    3682         2907 : slurping::~slurping ()
    3683              : {
    3684         2907 :   vec_free (remap);
    3685         2907 :   remap = NULL;
    3686         2907 :   release_macros ();
    3687         2907 :   close ();
    3688         2907 : }
    3689              : 
    3690         5298 : void slurping::release_macros ()
    3691              : {
    3692         5298 :   if (macro_defs.size)
    3693          882 :     elf_in::release (from, macro_defs);
    3694         5298 :   if (macro_tbl.size)
    3695            0 :     elf_in::release (from, macro_tbl);
    3696         5298 : }
    3697              : 
    3698              : /* Flags for extensions that end up being streamed.  */
    3699              : 
    3700              : enum streamed_extensions {
    3701              :   SE_OPENMP_SIMD = 1 << 0,
    3702              :   SE_OPENMP = 1 << 1,
    3703              :   SE_OPENACC = 1 << 2,
    3704              :   SE_BITS = 3
    3705              : };
    3706              : 
    3707              : /* Counter indices.  */
    3708              : enum module_state_counts
    3709              : {
    3710              :   MSC_sec_lwm,
    3711              :   MSC_sec_hwm,
    3712              :   MSC_pendings,
    3713              :   MSC_entities,
    3714              :   MSC_namespaces,
    3715              :   MSC_using_directives,
    3716              :   MSC_bindings,
    3717              :   MSC_macros,
    3718              :   MSC_inits,
    3719              :   MSC_HWM
    3720              : };
    3721              : 
    3722              : /********************************************************************/
    3723              : struct module_state_config;
    3724              : 
    3725              : /* Increasing levels of loadedness.  */
    3726              : enum module_loadedness {
    3727              :   ML_NONE,              /* Not loaded.  */
    3728              :   ML_CONFIG,            /* Config loaed.  */
    3729              :   ML_PREPROCESSOR,      /* Preprocessor loaded.  */
    3730              :   ML_LANGUAGE,          /* Language loaded.  */
    3731              : };
    3732              : 
    3733              : /* Increasing levels of directness (toplevel) of import.  */
    3734              : enum module_directness {
    3735              :   MD_NONE,              /* Not direct.  */
    3736              :   MD_PARTITION_DIRECT,  /* Direct import of a partition.  */
    3737              :   MD_DIRECT,            /* Direct import.  */
    3738              :   MD_PURVIEW_DIRECT,    /* Direct import in purview.  */
    3739              : };
    3740              : 
    3741              : /* State of a particular module. */
    3742              : 
    3743              : class GTY((chain_next ("%h.parent"), for_user)) module_state {
    3744              :  public:
    3745              :   /* We always import & export ourselves.  */
    3746              :   bitmap imports;       /* Transitive modules we're importing.  */
    3747              :   bitmap exports;       /* Subset of that, that we're exporting.  */
    3748              : 
    3749              :   /* For a named module interface A.B, parent is A and name is B.
    3750              :      For a partition M:P, parent is M and name is P.
    3751              :      For an implementation unit I, parent is I's interface and name is NULL.
    3752              :      Otherwise parent is NULL and name will be the flatname.  */
    3753              :   module_state *parent;
    3754              :   tree name;
    3755              : 
    3756              :   slurping *slurp;      /* Data for loading.  */
    3757              : 
    3758              :   const char *flatname; /* Flatname of module.  */
    3759              :   char *filename;       /* CMI Filename */
    3760              : 
    3761              :   /* Indices into the entity_ary.  */
    3762              :   unsigned entity_lwm;
    3763              :   unsigned entity_num;
    3764              : 
    3765              :   /* Location ranges for this module.  adhoc-locs are decomposed, so
    3766              :      don't have a range.  */
    3767              :   loc_range_t GTY((skip)) ordinary_locs;
    3768              :   loc_range_t GTY((skip)) macro_locs; // [lwm,num)
    3769              : 
    3770              :   /* LOC is first set too the importing location.  When initially
    3771              :      loaded it refers to a module loc whose parent is the importing
    3772              :      location.  */
    3773              :   location_t loc;       /* Location referring to module itself.  */
    3774              :   unsigned crc;         /* CRC we saw reading it in. */
    3775              : 
    3776              :   unsigned mod;         /* Module owner number.  */
    3777              :   unsigned remap;       /* Remapping during writing.  */
    3778              : 
    3779              :   unsigned short subst; /* Mangle subst if !0.  */
    3780              : 
    3781              :   /* How loaded this module is.  */
    3782              :   enum module_loadedness loadedness : 2;
    3783              : 
    3784              :   bool module_p : 1;    /* /The/ module of this TU.  */
    3785              :   bool header_p : 1;    /* Is a header unit.  */
    3786              :   bool interface_p : 1; /* An interface.  */
    3787              :   bool partition_p : 1; /* A partition.  */
    3788              : 
    3789              :   /* How directly this module is imported.  */
    3790              :   enum module_directness directness : 2;
    3791              : 
    3792              :   bool exported_p : 1;  /* directness != MD_NONE && exported.  */
    3793              :   bool cmi_noted_p : 1; /* We've told the user about the CMI, don't
    3794              :                            do it again  */
    3795              :   bool active_init_p : 1; /* This module's global initializer needs
    3796              :                            calling.  */
    3797              :   bool inform_cmi_p : 1; /* Inform of a read/write.  */
    3798              :   bool visited_p : 1;    /* A walk-once flag. */
    3799              :   /* Record extensions emitted or permitted.  */
    3800              :   unsigned extensions : SE_BITS;
    3801              :   /* 14 bits used, 2 bits remain  */
    3802              : 
    3803              :  public:
    3804              :   module_state (tree name, module_state *, bool);
    3805              :   ~module_state ();
    3806              : 
    3807              :  public:
    3808         2966 :   void release ()
    3809              :   {
    3810         2966 :     imports = exports = NULL;
    3811         2966 :     slurped ();
    3812         2907 :   }
    3813         5357 :   void slurped ()
    3814              :   {
    3815         5357 :     delete slurp;
    3816         5357 :     slurp = NULL;
    3817         5357 :   }
    3818      1293615 :   elf_in *from () const
    3819              :   {
    3820      1293615 :     return slurp->from;
    3821              :   }
    3822              : 
    3823              :  public:
    3824              :   /* Kind of this module.  */
    3825       142465 :   bool is_module () const
    3826              :   {
    3827       142465 :     return module_p;
    3828              :   }
    3829      2340138 :   bool is_header () const
    3830              :   {
    3831      2340138 :     return header_p;
    3832              :   }
    3833          606 :   bool is_interface () const
    3834              :   {
    3835          606 :     return interface_p;
    3836              :   }
    3837       313085 :   bool is_partition () const
    3838              :   {
    3839       313085 :     return partition_p;
    3840              :   }
    3841              : 
    3842              :   /* How this module is used in the current TU.  */
    3843         3191 :   bool is_exported () const
    3844              :   {
    3845         3191 :     return exported_p;
    3846              :   }
    3847        20699 :   bool is_direct () const
    3848              :   {
    3849        20699 :     return directness >= MD_DIRECT;
    3850              :   }
    3851          277 :   bool is_purview_direct () const
    3852              :   {
    3853          277 :     return directness == MD_PURVIEW_DIRECT;
    3854              :   }
    3855          466 :   bool is_partition_direct () const
    3856              :   {
    3857          466 :     return directness == MD_PARTITION_DIRECT;
    3858              :   }
    3859              : 
    3860              :  public:
    3861              :   /* Is this a real module?  */
    3862        16052 :   bool has_location () const
    3863              :   {
    3864        16052 :     return loc != UNKNOWN_LOCATION;
    3865              :   }
    3866              : 
    3867              :  public:
    3868              :   bool check_circular_import (location_t loc);
    3869              : 
    3870              :  public:
    3871              :   void mangle (bool include_partition);
    3872              : 
    3873              :  public:
    3874              :   void set_import (module_state const *, bool is_export);
    3875              :   void announce (const char *) const;
    3876              : 
    3877              :  public:
    3878              :   /* Read and write module.  */
    3879              :   bool write_begin (elf_out *to, cpp_reader *,
    3880              :                     module_state_config &, unsigned &crc);
    3881              :   void write_end (elf_out *to, cpp_reader *,
    3882              :                   module_state_config &, unsigned &crc);
    3883              :   bool read_initial (cpp_reader *);
    3884              :   bool read_preprocessor (bool);
    3885              :   bool read_language (bool);
    3886              : 
    3887              :  public:
    3888              :   /* Read a section.  */
    3889              :   bool load_section (unsigned snum, binding_slot *mslot);
    3890              :   /* Lazily read a section.  */
    3891              :   bool lazy_load (unsigned index, binding_slot *mslot);
    3892              : 
    3893              :  public:
    3894              :   /* Juggle a limited number of file numbers.  */
    3895              :   static void freeze_an_elf ();
    3896              :   bool maybe_defrost ();
    3897              : 
    3898              :  public:
    3899              :   void maybe_completed_reading ();
    3900              :   bool check_read (bool outermost, bool ok);
    3901              : 
    3902              :  private:
    3903              :   /* The README, for human consumption.  */
    3904              :   void write_readme (elf_out *to, cpp_reader *, const char *dialect);
    3905              :   void write_env (elf_out *to);
    3906              : 
    3907              :  private:
    3908              :   /* Import tables. */
    3909              :   void write_imports (bytes_out &cfg, bool direct);
    3910              :   unsigned read_imports (bytes_in &cfg, cpp_reader *, line_maps *maps);
    3911              : 
    3912              :  private:
    3913              :   void write_imports (elf_out *to, unsigned *crc_ptr);
    3914              :   bool read_imports (cpp_reader *, line_maps *);
    3915              : 
    3916              :  private:
    3917              :   void write_partitions (elf_out *to, unsigned, unsigned *crc_ptr);
    3918              :   bool read_partitions (unsigned);
    3919              : 
    3920              :  private:
    3921              :   void write_config (elf_out *to, struct module_state_config &, unsigned crc);
    3922              :   bool read_config (struct module_state_config &, bool = true);
    3923              :   static void write_counts (elf_out *to, unsigned [MSC_HWM], unsigned *crc_ptr);
    3924              :   bool read_counts (unsigned *);
    3925              : 
    3926              :  public:
    3927              :   void note_cmi_name ();
    3928              : 
    3929              :  private:
    3930              :   static unsigned write_bindings (elf_out *to, vec<depset *> depsets,
    3931              :                                   unsigned *crc_ptr);
    3932              :   bool read_bindings (unsigned count, unsigned lwm, unsigned hwm);
    3933              : 
    3934              :   static void write_namespace (bytes_out &sec, depset *ns_dep);
    3935              :   tree read_namespace (bytes_in &sec);
    3936              : 
    3937              :   void write_namespaces (elf_out *to, vec<depset *> spaces,
    3938              :                          unsigned, unsigned *crc_ptr);
    3939              :   bool read_namespaces (unsigned);
    3940              : 
    3941              :   unsigned write_using_directives (elf_out *to, depset::hash &,
    3942              :                                    vec<depset *> spaces, unsigned *crc_ptr);
    3943              :   bool read_using_directives (unsigned);
    3944              : 
    3945              :   void intercluster_seed (trees_out &sec, unsigned index, depset *dep);
    3946              :   unsigned write_cluster (elf_out *to, depset *depsets[], unsigned size,
    3947              :                           depset::hash &, unsigned *counts, unsigned *crc_ptr);
    3948              :   bool read_cluster (unsigned snum);
    3949              :   bool open_slurp (cpp_reader *);
    3950              : 
    3951              :  private:
    3952              :   unsigned write_inits (elf_out *to, depset::hash &, unsigned *crc_ptr);
    3953              :   bool read_inits (unsigned count);
    3954              : 
    3955              :  private:
    3956              :   unsigned write_pendings (elf_out *to, vec<depset *> depsets,
    3957              :                            depset::hash &, unsigned *crc_ptr);
    3958              :   bool read_pendings (unsigned count);
    3959              : 
    3960              :  private:
    3961              :   void write_entities (elf_out *to, vec<depset *> depsets,
    3962              :                        unsigned count, unsigned *crc_ptr);
    3963              :   bool read_entities (unsigned count, unsigned lwm, unsigned hwm);
    3964              : 
    3965              :  private:
    3966              :   void write_init_maps ();
    3967              :   range_t write_prepare_maps (module_state_config *, bool);
    3968              :   bool read_prepare_maps (const module_state_config *);
    3969              : 
    3970              :   void write_ordinary_maps (elf_out *to, range_t &,
    3971              :                             bool, unsigned *crc_ptr);
    3972              :   bool read_ordinary_maps (line_map_uint_t, unsigned);
    3973              :   void write_macro_maps (elf_out *to, range_t &, unsigned *crc_ptr);
    3974              :   bool read_macro_maps (line_map_uint_t);
    3975              : 
    3976              :   void write_diagnostic_classification (elf_out *, diagnostics::context *,
    3977              :                                         unsigned *);
    3978              :   bool read_diagnostic_classification (diagnostics::context *);
    3979              : 
    3980              :  private:
    3981              :   void write_define (bytes_out &, const cpp_macro *);
    3982              :   cpp_macro *read_define (bytes_in &, cpp_reader *) const;
    3983              :   vec<cpp_hashnode *> *prepare_macros (cpp_reader *);
    3984              :   unsigned write_macros (elf_out *to, vec<cpp_hashnode *> *, unsigned *crc_ptr);
    3985              :   bool read_macros ();
    3986              :   void install_macros ();
    3987              : 
    3988              :  public:
    3989              :   void import_macros ();
    3990              : 
    3991              :  public:
    3992              :   static void undef_macro (cpp_reader *, location_t, cpp_hashnode *);
    3993              :   static cpp_macro *deferred_macro (cpp_reader *, location_t, cpp_hashnode *);
    3994              : 
    3995              :  public:
    3996              :   static bool note_location (location_t);
    3997              :   static void write_location (bytes_out &, location_t);
    3998              :   location_t read_location (bytes_in &) const;
    3999              : 
    4000              :  public:
    4001              :   void set_flatname ();
    4002        52612 :   const char *get_flatname () const
    4003              :   {
    4004        52612 :     return flatname;
    4005              :   }
    4006              :   location_t imported_from () const;
    4007              : 
    4008              :  public:
    4009              :   void set_filename (const Cody::Packet &);
    4010              :   bool do_import (cpp_reader *, bool outermost);
    4011              :   bool check_importable (cpp_reader *);
    4012              : };
    4013              : 
    4014              : /* Hash module state by name.  This cannot be a member of
    4015              :    module_state, because of GTY restrictions.  We never delete from
    4016              :    the hash table, but ggc_ptr_hash doesn't support that
    4017              :    simplification.  */
    4018              : 
    4019              : struct module_state_hash : ggc_ptr_hash<module_state> {
    4020              :   typedef std::pair<tree,uintptr_t> compare_type; /* {name,parent} */
    4021              : 
    4022              :   static inline hashval_t hash (const value_type m);
    4023              :   static inline hashval_t hash (const compare_type &n);
    4024              :   static inline bool equal (const value_type existing,
    4025              :                             const compare_type &candidate);
    4026              : };
    4027              : 
    4028        11452 : module_state::module_state (tree name, module_state *parent, bool partition)
    4029        11452 :   : imports (BITMAP_GGC_ALLOC ()), exports (BITMAP_GGC_ALLOC ()),
    4030        11452 :     parent (parent), name (name), slurp (NULL),
    4031        11452 :     flatname (NULL), filename (NULL),
    4032        11452 :     entity_lwm (~0u >> 1), entity_num (0),
    4033        11452 :     ordinary_locs (0, 0), macro_locs (0, 0),
    4034        11452 :     loc (UNKNOWN_LOCATION),
    4035        11452 :     crc (0), mod (MODULE_UNKNOWN), remap (0), subst (0)
    4036              : {
    4037        11452 :   loadedness = ML_NONE;
    4038              : 
    4039        11452 :   module_p = header_p = interface_p = partition_p = false;
    4040              : 
    4041        11452 :   directness = MD_NONE;
    4042        11452 :   exported_p = false;
    4043              : 
    4044        11452 :   cmi_noted_p = false;
    4045        11452 :   active_init_p = false;
    4046              : 
    4047        11452 :   partition_p = partition;
    4048              : 
    4049        11452 :   inform_cmi_p = false;
    4050        11452 :   visited_p = false;
    4051              : 
    4052        11452 :   extensions = 0;
    4053        11452 :   if (name && TREE_CODE (name) == STRING_CST)
    4054              :     {
    4055         1893 :       header_p = true;
    4056              : 
    4057         1893 :       const char *string = TREE_STRING_POINTER (name);
    4058         1893 :       gcc_checking_assert (string[0] == '.'
    4059              :                            ? IS_DIR_SEPARATOR (string[1])
    4060              :                            : IS_ABSOLUTE_PATH (string));
    4061              :     }
    4062              : 
    4063        11452 :   gcc_checking_assert (!(parent && header_p));
    4064        11452 : }
    4065              : 
    4066           59 : module_state::~module_state ()
    4067              : {
    4068           59 :   release ();
    4069           59 : }
    4070              : 
    4071              : /* Hash module state.  */
    4072              : static hashval_t
    4073        16900 : module_name_hash (const_tree name)
    4074              : {
    4075        16900 :   if (TREE_CODE (name) == STRING_CST)
    4076         3504 :     return htab_hash_string (TREE_STRING_POINTER (name));
    4077              :   else
    4078        13396 :     return IDENTIFIER_HASH_VALUE (name);
    4079              : }
    4080              : 
    4081              : hashval_t
    4082         4724 : module_state_hash::hash (const value_type m)
    4083              : {
    4084         4724 :   hashval_t ph = pointer_hash<void>::hash
    4085         4724 :     (reinterpret_cast<void *> (reinterpret_cast<uintptr_t> (m->parent)
    4086         4724 :                                | m->is_partition ()));
    4087         4724 :   hashval_t nh = module_name_hash (m->name);
    4088         4724 :   return iterative_hash_hashval_t (ph, nh);
    4089              : }
    4090              : 
    4091              : /* Hash a name.  */
    4092              : hashval_t
    4093        12176 : module_state_hash::hash (const compare_type &c)
    4094              : {
    4095        12176 :   hashval_t ph = pointer_hash<void>::hash (reinterpret_cast<void *> (c.second));
    4096        12176 :   hashval_t nh = module_name_hash (c.first);
    4097              : 
    4098        12176 :   return iterative_hash_hashval_t (ph, nh);
    4099              : }
    4100              : 
    4101              : bool
    4102         8347 : module_state_hash::equal (const value_type existing,
    4103              :                           const compare_type &candidate)
    4104              : {
    4105         8347 :   uintptr_t ep = (reinterpret_cast<uintptr_t> (existing->parent)
    4106         8347 :                   | existing->is_partition ());
    4107         8347 :   if (ep != candidate.second)
    4108              :     return false;
    4109              : 
    4110              :   /* Identifier comparison is by pointer.  If the string_csts happen
    4111              :      to be the same object, then they're equal too.  */
    4112         6903 :   if (existing->name == candidate.first)
    4113              :     return true;
    4114              : 
    4115              :   /* If neither are string csts, they can't be equal.  */
    4116         1464 :   if (TREE_CODE (candidate.first) != STRING_CST
    4117          495 :       || TREE_CODE (existing->name) != STRING_CST)
    4118              :     return false;
    4119              : 
    4120              :   /* String equality.  */
    4121          425 :   if (TREE_STRING_LENGTH (existing->name)
    4122          425 :       == TREE_STRING_LENGTH (candidate.first)
    4123          425 :       && !memcmp (TREE_STRING_POINTER (existing->name),
    4124          422 :                   TREE_STRING_POINTER (candidate.first),
    4125          422 :                   TREE_STRING_LENGTH (existing->name)))
    4126          139 :     return true;
    4127              : 
    4128              :   return false;
    4129              : }
    4130              : 
    4131              : /********************************************************************/
    4132              : /* Global state */
    4133              : 
    4134              : /* Mapper name.  */
    4135              : static const char *module_mapper_name;
    4136              : 
    4137              : /* Deferred import queue (FIFO).  */
    4138              : static vec<module_state *, va_heap, vl_embed> *pending_imports;
    4139              : 
    4140              : /* CMI repository path and workspace.  */
    4141              : static char *cmi_repo;
    4142              : static size_t cmi_repo_length;
    4143              : static char *cmi_path;
    4144              : static size_t cmi_path_alloc;
    4145              : 
    4146              : /* Count of available and loaded clusters.  */
    4147              : static unsigned available_clusters;
    4148              : static unsigned loaded_clusters;
    4149              : 
    4150              : /* What the current TU is.  */
    4151              : unsigned module_kind;
    4152              : 
    4153              : /* Global trees.  */
    4154              : static const std::pair<tree *, unsigned> global_tree_arys[] =
    4155              :   {
    4156              :     std::pair<tree *, unsigned> (sizetype_tab, stk_type_kind_last),
    4157              :     std::pair<tree *, unsigned> (integer_types, itk_none),
    4158              :     std::pair<tree *, unsigned> (global_trees, TI_MODULE_HWM),
    4159              :     std::pair<tree *, unsigned> (c_global_trees, CTI_MODULE_HWM),
    4160              :     std::pair<tree *, unsigned> (cp_global_trees, CPTI_MODULE_HWM),
    4161              :     std::pair<tree *, unsigned> (NULL, 0)
    4162              :   };
    4163              : static GTY(()) vec<tree, va_gc> *fixed_trees;
    4164              : static unsigned global_crc;
    4165              : 
    4166              : /* Lazy loading can open many files concurrently, there are
    4167              :    per-process limits on that.  We pay attention to the process limit,
    4168              :    and attempt to increase it when we run out.  Otherwise we use an
    4169              :    LRU scheme to figure out who to flush.  Note that if the import
    4170              :    graph /depth/ exceeds lazy_limit, we'll exceed the limit.  */
    4171              : static unsigned lazy_lru;  /* LRU counter.  */
    4172              : static unsigned lazy_open; /* Number of open modules */
    4173              : static unsigned lazy_limit; /* Current limit of open modules.  */
    4174              : static unsigned lazy_hard_limit; /* Hard limit on open modules.  */
    4175              : /* Account for source, assembler and dump files & directory searches.
    4176              :    We don't keep the source file's open, so we don't have to account
    4177              :    for #include depth.  I think dump files are opened and closed per
    4178              :    pass, but ICBW.  */
    4179              : #define LAZY_HEADROOM 15 /* File descriptor headroom.  */
    4180              : 
    4181              : /* Vector of module state.  Indexed by OWNER.  Index 0 is reserved for the
    4182              :    current TU; imports start at 1.  */
    4183              : static GTY(()) vec<module_state *, va_gc> *modules;
    4184              : 
    4185              : /* Get the module state for the current TU's module.  */
    4186              : 
    4187              : static module_state *
    4188       268473 : this_module() {
    4189       268473 :   return (*modules)[0];
    4190              : }
    4191              : 
    4192              : /* Hash of module state, findable by {name, parent}. */
    4193              : static GTY(()) hash_table<module_state_hash> *modules_hash;
    4194              : 
    4195              : /* Map of imported entities.  We map DECL_UID to index of entity
    4196              :    vector.  */
    4197              : typedef hash_map<unsigned/*UID*/, unsigned/*index*/,
    4198              :                  simple_hashmap_traits<int_hash<unsigned,0>, unsigned>
    4199              :                  > entity_map_t;
    4200              : static entity_map_t *entity_map;
    4201              : /* Doesn't need GTYing, because any tree referenced here is also
    4202              :    findable by, symbol table, specialization table, return type of
    4203              :    reachable function.  */
    4204              : static vec<binding_slot, va_heap, vl_embed> *entity_ary;
    4205              : 
    4206              : /* Members entities of imported classes that are defined in this TU.
    4207              :    These are where the entity's context is not from the current TU.
    4208              :    We need to emit the definition (but not the enclosing class).
    4209              : 
    4210              :    We could find these by walking ALL the imported classes that we
    4211              :    could provide a member definition.  But that's expensive,
    4212              :    especially when you consider lazy implicit member declarations,
    4213              :    which could be ANY imported class.  */
    4214              : static GTY(()) vec<tree, va_gc> *class_members;
    4215              : 
    4216              : /* The same problem exists for class template partial
    4217              :    specializations.  Now that we have constraints, the invariant of
    4218              :    expecting them in the instantiation table no longer holds.  One of
    4219              :    the constrained partial specializations will be there, but the
    4220              :    others not so much.  It's not even an unconstrained partial
    4221              :    spacialization in the table :(  so any partial template declaration
    4222              :    is added to this list too.  */
    4223              : static GTY(()) vec<tree, va_gc> *partial_specializations;
    4224              : 
    4225              : /********************************************************************/
    4226              : 
    4227              : /* Our module mapper (created lazily).  */
    4228              : module_client *mapper;
    4229              : 
    4230              : static module_client *make_mapper (location_t loc, class mkdeps *deps);
    4231        29272 : inline module_client *get_mapper (location_t loc, class mkdeps *deps)
    4232              : {
    4233        29272 :   auto *res = mapper;
    4234          299 :   if (!res)
    4235         4854 :     res = make_mapper (loc, deps);
    4236        29272 :   return res;
    4237              : }
    4238              : 
    4239              : /********************************************************************/
    4240              : static tree
    4241       350315 : get_clone_target (tree decl)
    4242              : {
    4243       350315 :   tree target;
    4244              : 
    4245       350315 :   if (TREE_CODE (decl) == TEMPLATE_DECL)
    4246              :     {
    4247        43252 :       tree res_orig = DECL_CLONED_FUNCTION (DECL_TEMPLATE_RESULT (decl));
    4248              : 
    4249        43252 :       target = DECL_TI_TEMPLATE (res_orig);
    4250              :     }
    4251              :   else
    4252       307063 :     target = DECL_CLONED_FUNCTION (decl);
    4253              : 
    4254       350315 :   gcc_checking_assert (DECL_MAYBE_IN_CHARGE_CDTOR_P (target));
    4255              : 
    4256       350315 :   return target;
    4257              : }
    4258              : 
    4259              : /* Like FOR_EACH_CLONE, but will walk cloned templates.  */
    4260              : #define FOR_EVERY_CLONE(CLONE, FN)                      \
    4261              :   if (!DECL_MAYBE_IN_CHARGE_CDTOR_P (FN));              \
    4262              :   else                                                  \
    4263              :     for (CLONE = DECL_CHAIN (FN);                       \
    4264              :          CLONE && DECL_CLONED_FUNCTION_P (CLONE);       \
    4265              :          CLONE = DECL_CHAIN (CLONE))
    4266              : 
    4267              : /* It'd be nice if USE_TEMPLATE was a field of template_info
    4268              :    (a) it'd solve the enum case dealt with below,
    4269              :    (b) both class templates and decl templates would store this in the
    4270              :    same place
    4271              :    (c) this function wouldn't need the by-ref arg, which is annoying.  */
    4272              : 
    4273              : static tree
    4274    130146499 : node_template_info (tree decl, int &use)
    4275              : {
    4276    130146499 :   tree ti = NULL_TREE;
    4277    130146499 :   int use_tpl = -1;
    4278    130146499 :   if (DECL_IMPLICIT_TYPEDEF_P (decl))
    4279              :     {
    4280     23052351 :       tree type = TREE_TYPE (decl);
    4281              : 
    4282     23052351 :       ti = TYPE_TEMPLATE_INFO (type);
    4283     23052351 :       if (ti)
    4284              :         {
    4285      4586548 :           if (TYPE_LANG_SPECIFIC (type))
    4286      4577127 :             use_tpl = CLASSTYPE_USE_TEMPLATE (type);
    4287              :           else
    4288              :             {
    4289              :               /* An enum, where we don't explicitly encode use_tpl.
    4290              :                  If the containing context (a type or a function), is
    4291              :                  an ({im,ex}plicit) instantiation, then this is too.
    4292              :                  If it's a partial or explicit specialization, then
    4293              :                  this is not!.  */
    4294         9421 :               tree ctx = CP_DECL_CONTEXT (decl);
    4295         9421 :               if (TYPE_P (ctx))
    4296         9255 :                 ctx = TYPE_NAME (ctx);
    4297         9421 :               node_template_info (ctx, use);
    4298         9421 :               use_tpl = use != 2 ? use : 0;
    4299              :             }
    4300              :         }
    4301              :     }
    4302    107094148 :   else if (DECL_LANG_SPECIFIC (decl)
    4303    107094148 :            && (VAR_P (decl)
    4304              :                || TREE_CODE (decl) == TYPE_DECL
    4305              :                || TREE_CODE (decl) == FUNCTION_DECL
    4306              :                || TREE_CODE (decl) == FIELD_DECL
    4307              :                || TREE_CODE (decl) == CONCEPT_DECL
    4308              :                || TREE_CODE (decl) == TEMPLATE_DECL))
    4309              :     {
    4310     91888949 :       use_tpl = DECL_USE_TEMPLATE (decl);
    4311     91888949 :       ti = DECL_TEMPLATE_INFO (decl);
    4312              :     }
    4313              : 
    4314    130146499 :   use = use_tpl;
    4315    130146499 :   return ti;
    4316              : }
    4317              : 
    4318              : /* Find the index in entity_ary for an imported DECL.  It should
    4319              :    always be there, but bugs can cause it to be missing, and that can
    4320              :    crash the crash reporting -- let's not do that!  When streaming
    4321              :    out we place entities from this module there too -- with negated
    4322              :    indices.  */
    4323              : 
    4324              : static unsigned
    4325      1445979 : import_entity_index (tree decl, bool null_ok = false)
    4326              : {
    4327      1445979 :   if (unsigned *slot = entity_map->get (DECL_UID (decl)))
    4328      1445934 :     return *slot;
    4329              : 
    4330           45 :   gcc_checking_assert (null_ok);
    4331              :   return ~(~0u >> 1);
    4332              : }
    4333              : 
    4334              : /* Find the module for an imported entity at INDEX in the entity ary.
    4335              :    There must be one.  */
    4336              : 
    4337              : static module_state *
    4338       133197 : import_entity_module (unsigned index)
    4339              : {
    4340       133197 :   if (index > ~(~0u >> 1))
    4341              :     /* This is an index for an exported entity.  */
    4342           60 :     return this_module ();
    4343              : 
    4344              :   /* Do not include the current TU (not an off-by-one error).  */
    4345       133137 :   unsigned pos = 1;
    4346       133137 :   unsigned len = modules->length () - pos;
    4347       304681 :   while (len)
    4348              :     {
    4349       171544 :       unsigned half = len / 2;
    4350       171544 :       module_state *probe = (*modules)[pos + half];
    4351       171544 :       if (index < probe->entity_lwm)
    4352              :         len = half;
    4353       133603 :       else if (index < probe->entity_lwm + probe->entity_num)
    4354              :         return probe;
    4355              :       else
    4356              :         {
    4357          466 :           pos += half + 1;
    4358          466 :           len = len - (half + 1);
    4359              :         }
    4360              :     }
    4361            0 :   gcc_unreachable ();
    4362              : }
    4363              : 
    4364              : 
    4365              : /********************************************************************/
    4366              : /* A dumping machinery.  */
    4367              : 
    4368              : class dumper {
    4369              : public:
    4370              :   enum {
    4371              :     LOCATION = TDF_LINENO,  /* -lineno:Source location streaming.  */
    4372              :     DEPEND = TDF_GRAPH, /* -graph:Dependency graph construction.  */
    4373              :     CLUSTER = TDF_BLOCKS,   /* -blocks:Clusters.  */
    4374              :     TREE = TDF_UID,     /* -uid:Tree streaming.  */
    4375              :     MERGE = TDF_ALIAS,  /* -alias:Mergeable Entities.  */
    4376              :     ELF = TDF_ASMNAME,  /* -asmname:Elf data.  */
    4377              :     MACRO = TDF_VOPS    /* -vops:Macros.  */
    4378              :   };
    4379              : 
    4380              : private:
    4381              :   struct impl {
    4382              :     typedef vec<module_state *, va_heap, vl_embed> stack_t;
    4383              : 
    4384              :     FILE *stream;       /* Dump stream.  */
    4385              :     unsigned indent;    /* Local indentation.  */
    4386              :     bool bol;           /* Beginning of line.  */
    4387              :     stack_t stack;      /* Trailing array of module_state.  */
    4388              : 
    4389              :     bool nested_name (tree);  /* Dump a name following DECL_CONTEXT.  */
    4390              :   };
    4391              : 
    4392              : public:
    4393              :   /* The dumper.  */
    4394              :   impl *dumps;
    4395              :   dump_flags_t flags;
    4396              : 
    4397              : public:
    4398              :   /* Push/pop module state dumping.  */
    4399              :   unsigned push (module_state *);
    4400              :   void pop (unsigned);
    4401              : 
    4402              : public:
    4403              :   /* Change local indentation.  */
    4404    367829467 :   void indent ()
    4405              :   {
    4406          309 :     if (dumps)
    4407       725731 :       dumps->indent++;
    4408              :   }
    4409    367829467 :   void outdent ()
    4410              :   {
    4411    367829467 :     if (dumps)
    4412              :       {
    4413       725731 :         gcc_checking_assert (dumps->indent);
    4414       725731 :         dumps->indent--;
    4415              :       }
    4416    367829467 :   }
    4417              : 
    4418              : public:
    4419              :   /* Is dump enabled?.  */
    4420    231712981 :   bool operator () (int mask = 0)
    4421              :   {
    4422      4459157 :     if (!dumps || !dumps->stream)
    4423              :       return false;
    4424       505640 :     if (mask && !(mask & flags))
    4425         5388 :       return false;
    4426              :     return true;
    4427              :   }
    4428              :   /* Dump some information.  */
    4429              :   bool operator () (const char *, ...);
    4430              : };
    4431              : 
    4432              : /* The dumper.  */
    4433              : static dumper dump = {0, dump_flags_t (0)};
    4434              : 
    4435              : /* Push to dumping M.  Return previous indentation level.  */
    4436              : 
    4437              : unsigned
    4438       125440 : dumper::push (module_state *m)
    4439              : {
    4440       125440 :   FILE *stream = NULL;
    4441       125440 :   if (!dumps || !dumps->stack.length ())
    4442              :     {
    4443       124167 :       stream = dump_begin (module_dump_id, &flags);
    4444       124167 :       if (!stream)
    4445              :         return 0;
    4446              :     }
    4447              : 
    4448         6932 :   if (!dumps || !dumps->stack.space (1))
    4449              :     {
    4450              :       /* Create or extend the dump implementor.  */
    4451         1239 :       unsigned current = dumps ? dumps->stack.length () : 0;
    4452          656 :       unsigned count = current ? current * 2 : EXPERIMENT (1, 20);
    4453         1239 :       size_t alloc = (offsetof (impl, stack)
    4454         1239 :                       + impl::stack_t::embedded_size (count));
    4455         1239 :       dumps = XRESIZEVAR (impl, dumps, alloc);
    4456         1239 :       dumps->stack.embedded_init (count, current);
    4457              :     }
    4458         6932 :   if (stream)
    4459         5659 :     dumps->stream = stream;
    4460              : 
    4461         6932 :   unsigned n = dumps->indent;
    4462         6932 :   dumps->indent = 0;
    4463         6932 :   dumps->bol = true;
    4464         6932 :   dumps->stack.quick_push (m);
    4465         6932 :   if (m)
    4466              :     {
    4467         2039 :       module_state *from = NULL;
    4468              : 
    4469         2039 :       if (dumps->stack.length () > 1)
    4470          654 :         from = dumps->stack[dumps->stack.length () - 2];
    4471              :       else
    4472         1385 :         dump ("");
    4473         3683 :       dump (from ? "Starting module %M (from %M)"
    4474              :             : "Starting module %M", m, from);
    4475              :     }
    4476              : 
    4477              :   return n;
    4478              : }
    4479              : 
    4480              : /* Pop from dumping.  Restore indentation to N.  */
    4481              : 
    4482       125397 : void dumper::pop (unsigned n)
    4483              : {
    4484       125397 :   if (!dumps)
    4485              :     return;
    4486              : 
    4487        13864 :   gcc_checking_assert (dump () && !dumps->indent);
    4488         6932 :   if (module_state *m = dumps->stack[dumps->stack.length () - 1])
    4489              :     {
    4490         2039 :       module_state *from = (dumps->stack.length () > 1
    4491         2039 :                             ? dumps->stack[dumps->stack.length () - 2] : NULL);
    4492         2298 :       dump (from ? "Finishing module %M (returning to %M)"
    4493              :             : "Finishing module %M", m, from);
    4494              :     }
    4495         6932 :   dumps->stack.pop ();
    4496         6932 :   dumps->indent = n;
    4497         6932 :   if (!dumps->stack.length ())
    4498              :     {
    4499         5659 :       dump_end (module_dump_id, dumps->stream);
    4500         5659 :       dumps->stream = NULL;
    4501              :     }
    4502              : }
    4503              : 
    4504              : /* Dump a nested name for arbitrary tree T.  Sometimes it won't have a
    4505              :    name.  */
    4506              : 
    4507              : bool
    4508       522063 : dumper::impl::nested_name (tree t)
    4509              : {
    4510       522063 :   tree ti = NULL_TREE;
    4511       522063 :   int origin = -1;
    4512       522063 :   tree name = NULL_TREE;
    4513              : 
    4514       522063 :   if (t && TREE_CODE (t) == TU_LOCAL_ENTITY)
    4515            0 :     t = TU_LOCAL_ENTITY_NAME (t);
    4516              : 
    4517       522033 :   if (t && TREE_CODE (t) == TREE_BINFO)
    4518          384 :     t = BINFO_TYPE (t);
    4519              : 
    4520       522063 :   if (t && TYPE_P (t))
    4521       254250 :     t = TYPE_NAME (t);
    4522              : 
    4523       522021 :   if (t && DECL_P (t))
    4524              :     {
    4525       438704 :       if (t == global_namespace || DECL_TEMPLATE_PARM_P (t))
    4526              :         ;
    4527       406815 :       else if (tree ctx = DECL_CONTEXT (t))
    4528       317227 :         if (TREE_CODE (ctx) == TRANSLATION_UNIT_DECL
    4529       317227 :             || nested_name (ctx))
    4530       317227 :           fputs ("::", stream);
    4531              : 
    4532       438704 :       int use_tpl;
    4533       438704 :       ti = node_template_info (t, use_tpl);
    4534       138000 :       if (ti && TREE_CODE (TI_TEMPLATE (ti)) == TEMPLATE_DECL
    4535       576659 :           && (DECL_TEMPLATE_RESULT (TI_TEMPLATE (ti)) == t))
    4536              :         t = TI_TEMPLATE (ti);
    4537       438704 :       tree not_tmpl = t;
    4538       438704 :       if (TREE_CODE (t) == TEMPLATE_DECL)
    4539              :         {
    4540        23713 :           fputs ("template ", stream);
    4541        23713 :           not_tmpl = DECL_TEMPLATE_RESULT (t);
    4542              :         }
    4543              : 
    4544        23713 :       if (not_tmpl
    4545       438700 :           && DECL_P (not_tmpl)
    4546       438700 :           && DECL_LANG_SPECIFIC (not_tmpl)
    4547       264055 :           && DECL_MODULE_IMPORT_P (not_tmpl))
    4548              :         {
    4549              :           /* We need to be careful here, so as to not explode on
    4550              :              inconsistent data -- we're probably debugging, because
    4551              :              Something Is Wrong.  */
    4552        24167 :           unsigned index = import_entity_index (t, true);
    4553        24167 :           if (!(index & ~(~0u >> 1)))
    4554        23567 :             origin = import_entity_module (index)->mod;
    4555          600 :           else if (index > ~(~0u >> 1))
    4556              :             /* An imported partition member that we're emitting.  */
    4557              :             origin = 0;
    4558              :           else
    4559           45 :             origin = -2;
    4560              :         }
    4561              : 
    4562       441934 :       name = DECL_NAME (t) ? DECL_NAME (t)
    4563         4309 :         : HAS_DECL_ASSEMBLER_NAME_P (t) ? DECL_ASSEMBLER_NAME_RAW (t)
    4564              :         : NULL_TREE;
    4565              :     }
    4566              :   else
    4567              :     name = t;
    4568              : 
    4569       438704 :   if (name)
    4570       486981 :     switch (TREE_CODE (name))
    4571              :       {
    4572        13635 :       default:
    4573        13635 :         fputs ("#unnamed#", stream);
    4574        13635 :         break;
    4575              : 
    4576       448962 :       case IDENTIFIER_NODE:
    4577       448962 :         fwrite (IDENTIFIER_POINTER (name), 1, IDENTIFIER_LENGTH (name), stream);
    4578       448962 :         break;
    4579              : 
    4580        24292 :       case INTEGER_CST:
    4581        24292 :         print_hex (wi::to_wide (name), stream);
    4582        24292 :         break;
    4583              : 
    4584           92 :       case STRING_CST:
    4585              :         /* If TREE_TYPE is NULL, this is a raw string.  */
    4586          184 :         fwrite (TREE_STRING_POINTER (name), 1,
    4587           92 :                 TREE_STRING_LENGTH (name) - (TREE_TYPE (name) != NULL_TREE),
    4588              :                 stream);
    4589           92 :         break;
    4590              :       }
    4591              :   else
    4592        35082 :     fputs ("#null#", stream);
    4593              : 
    4594       522063 :   if (t && TREE_CODE (t) == FUNCTION_DECL && DECL_COROUTINE_P (t))
    4595           48 :     if (tree ramp = DECL_RAMP_FN (t))
    4596              :       {
    4597           27 :         if (DECL_ACTOR_FN (ramp) == t)
    4598           12 :           fputs (".actor", stream);
    4599           15 :         else if (DECL_DESTROY_FN (ramp) == t)
    4600           15 :           fputs (".destroy", stream);
    4601              :         else
    4602            0 :           gcc_unreachable ();
    4603              :       }
    4604              : 
    4605       522063 :   if (origin >= 0)
    4606              :     {
    4607        24122 :       const module_state *module = (*modules)[origin];
    4608        48244 :       fprintf (stream, "@%s:%d", !module ? "" : !module->name ? "(unnamed)"
    4609        24122 :                : module->get_flatname (), origin);
    4610              :     }
    4611       497941 :   else if (origin == -2)
    4612           45 :     fprintf (stream, "@???");
    4613              : 
    4614       522063 :   if (ti)
    4615              :     {
    4616       138000 :       tree args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (ti));
    4617       138000 :       fputs ("<", stream);
    4618       138000 :       if (args)
    4619       347775 :         for (int ix = 0; ix != TREE_VEC_LENGTH (args); ix++)
    4620              :           {
    4621       209775 :             if (ix)
    4622        71775 :               fputs (",", stream);
    4623       209775 :             nested_name (TREE_VEC_ELT (args, ix));
    4624              :           }
    4625       138000 :       fputs (">", stream);
    4626              :     }
    4627              : 
    4628       522063 :   return true;
    4629              : }
    4630              : 
    4631              : /* Formatted dumping.  FORMAT begins with '+' do not emit a trailing
    4632              :    new line.  (Normally it is appended.)
    4633              :    Escapes:
    4634              :       %C - tree_code
    4635              :       %I - identifier
    4636              :       %K - location_t or line_map_uint_t
    4637              :       %M - module_state
    4638              :       %N - name -- DECL_NAME
    4639              :       %P - context:name pair
    4640              :       %R - unsigned:unsigned ratio
    4641              :       %S - symbol -- DECL_ASSEMBLER_NAME
    4642              :       %U - long unsigned
    4643              :       %V - version
    4644              :       --- the following are printf-like, but without its flexibility
    4645              :       %d - decimal int
    4646              :       %p - pointer
    4647              :       %s - string
    4648              :       %u - unsigned int
    4649              :       %x - hex int
    4650              : 
    4651              :   We do not implement the printf modifiers.  */
    4652              : 
    4653              : bool
    4654       445012 : dumper::operator () (const char *format, ...)
    4655              : {
    4656       445012 :   if (!(*this) ())
    4657              :     return false;
    4658              : 
    4659       382367 :   bool no_nl = format[0] == '+';
    4660       382367 :   format += no_nl;
    4661              : 
    4662       382367 :   if (dumps->bol)
    4663              :     {
    4664              :       /* Module import indent.  */
    4665       196973 :       if (unsigned depth = dumps->stack.length () - 1)
    4666              :         {
    4667        22526 :           const char *prefix = ">>>>";
    4668        45034 :           fprintf (dumps->stream, (depth <= strlen (prefix)
    4669        22508 :                                    ? &prefix[strlen (prefix) - depth]
    4670              :                                    : ">.%d.>"), depth);
    4671              :         }
    4672              : 
    4673              :       /* Local indent.  */
    4674       196973 :       if (unsigned indent = dumps->indent)
    4675              :         {
    4676       106149 :           const char *prefix = "      ";
    4677       207282 :           fprintf (dumps->stream, (indent <= strlen (prefix)
    4678       101133 :                                    ? &prefix[strlen (prefix) - indent]
    4679              :                                    : "  .%d.  "), indent);
    4680              :         }
    4681       196973 :       dumps->bol = false;
    4682              :     }
    4683              : 
    4684       382367 :   va_list args;
    4685       382367 :   va_start (args, format);
    4686      1118282 :   while (const char *esc = strchr (format, '%'))
    4687              :     {
    4688       735915 :       fwrite (format, 1, (size_t)(esc - format), dumps->stream);
    4689       735915 :       format = ++esc;
    4690       735915 :       switch (*format++)
    4691              :         {
    4692            0 :         default:
    4693            0 :           gcc_unreachable ();
    4694              : 
    4695          583 :         case '%':
    4696          583 :           fputc ('%', dumps->stream);
    4697          583 :           break;
    4698              : 
    4699       112142 :         case 'C': /* Code */
    4700       112142 :           {
    4701       112142 :             tree_code code = (tree_code)va_arg (args, unsigned);
    4702       112142 :             fputs (get_tree_code_name (code), dumps->stream);
    4703              :           }
    4704       112142 :           break;
    4705              : 
    4706           81 :         case 'I': /* Identifier.  */
    4707           81 :           {
    4708           81 :             tree t = va_arg (args, tree);
    4709           81 :             dumps->nested_name (t);
    4710              :           }
    4711           81 :           break;
    4712              : 
    4713         4647 :         case 'K': /* location_t, either 32- or 64-bit.  */
    4714         4647 :           {
    4715         4647 :             unsigned long long u = va_arg (args, location_t);
    4716         4647 :             fprintf (dumps->stream, "%llu", u);
    4717              :           }
    4718         4647 :           break;
    4719              : 
    4720         7734 :         case 'M': /* Module. */
    4721         7734 :           {
    4722         7734 :             const char *str = "(none)";
    4723         7734 :             if (module_state *m = va_arg (args, module_state *))
    4724              :               {
    4725         7734 :                 if (!m->has_location ())
    4726              :                   str = "(detached)";
    4727              :                 else
    4728         7734 :                   str = m->get_flatname ();
    4729              :               }
    4730         7734 :             fputs (str, dumps->stream);
    4731              :           }
    4732         7734 :           break;
    4733              : 
    4734       125395 :         case 'N': /* Name.  */
    4735       125395 :           {
    4736       125395 :             tree t = va_arg (args, tree);
    4737       251279 :             while (t && TREE_CODE (t) == OVERLOAD)
    4738          489 :               t = OVL_FUNCTION (t);
    4739       125395 :             fputc ('\'', dumps->stream);
    4740       125395 :             dumps->nested_name (t);
    4741       125395 :             fputc ('\'', dumps->stream);
    4742              :           }
    4743       125395 :           break;
    4744              : 
    4745         7355 :         case 'P': /* Pair.  */
    4746         7355 :           {
    4747         7355 :             tree ctx = va_arg (args, tree);
    4748         7355 :             tree name = va_arg (args, tree);
    4749         7355 :             fputc ('\'', dumps->stream);
    4750         7355 :             dumps->nested_name (ctx);
    4751         7355 :             if (ctx && ctx != global_namespace)
    4752          998 :               fputs ("::", dumps->stream);
    4753         7355 :             dumps->nested_name (name);
    4754         7355 :             fputc ('\'', dumps->stream);
    4755              :           }
    4756         7355 :           break;
    4757              : 
    4758          900 :         case 'R': /* Ratio */
    4759          900 :           {
    4760          900 :             unsigned a = va_arg (args, unsigned);
    4761          900 :             unsigned b = va_arg (args, unsigned);
    4762          900 :             fprintf (dumps->stream, "%.1f", (float) a / (b + !b));
    4763              :           }
    4764          900 :           break;
    4765              : 
    4766        34704 :         case 'S': /* Symbol name */
    4767        34704 :           {
    4768        34704 :             tree t = va_arg (args, tree);
    4769        34704 :             if (t && TYPE_P (t))
    4770        12631 :               t = TYPE_NAME (t);
    4771        32924 :             if (t && HAS_DECL_ASSEMBLER_NAME_P (t)
    4772        31750 :                 && DECL_ASSEMBLER_NAME_SET_P (t))
    4773              :               {
    4774          172 :                 fputc ('(', dumps->stream);
    4775          172 :                 fputs (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (t)),
    4776          172 :                        dumps->stream);
    4777          172 :                 fputc (')', dumps->stream);
    4778              :               }
    4779              :           }
    4780              :           break;
    4781              : 
    4782            0 :         case 'U': /* long unsigned.  */
    4783            0 :           {
    4784            0 :             unsigned long u = va_arg (args, unsigned long);
    4785            0 :             fprintf (dumps->stream, "%lu", u);
    4786              :           }
    4787            0 :           break;
    4788              : 
    4789          832 :         case 'V': /* Version.  */
    4790          832 :           {
    4791          832 :             unsigned v = va_arg (args, unsigned);
    4792          832 :             verstr_t string;
    4793              : 
    4794          832 :             version2string (v, string);
    4795          832 :             fputs (string, dumps->stream);
    4796              :           }
    4797          832 :           break;
    4798              : 
    4799            0 :         case 'c': /* Character.  */
    4800            0 :           {
    4801            0 :             int c = va_arg (args, int);
    4802            0 :             fputc (c, dumps->stream);
    4803              :           }
    4804            0 :           break;
    4805              : 
    4806        63146 :         case 'd': /* Decimal Int.  */
    4807        63146 :           {
    4808        63146 :             int d = va_arg (args, int);
    4809        63146 :             fprintf (dumps->stream, "%d", d);
    4810              :           }
    4811        63146 :           break;
    4812              : 
    4813            0 :         case 'p': /* Pointer. */
    4814            0 :           {
    4815            0 :             void *p = va_arg (args, void *);
    4816            0 :             fprintf (dumps->stream, "%p", p);
    4817              :           }
    4818            0 :           break;
    4819              : 
    4820       126462 :         case 's': /* String. */
    4821       126462 :           {
    4822       126462 :             const char *s = va_arg (args, char *);
    4823       126462 :             gcc_checking_assert (s);
    4824       126462 :             fputs (s, dumps->stream);
    4825              :           }
    4826       126462 :           break;
    4827              : 
    4828       249258 :         case 'u': /* Unsigned.  */
    4829       249258 :           {
    4830       249258 :             unsigned u = va_arg (args, unsigned);
    4831       249258 :             fprintf (dumps->stream, "%u", u);
    4832              :           }
    4833       249258 :           break;
    4834              : 
    4835         2676 :         case 'x': /* Hex. */
    4836         2676 :           {
    4837         2676 :             unsigned x = va_arg (args, unsigned);
    4838         2676 :             fprintf (dumps->stream, "%x", x);
    4839              :           }
    4840         2676 :           break;
    4841              :         }
    4842              :     }
    4843       382367 :   fputs (format, dumps->stream);
    4844       382367 :   va_end (args);
    4845       382367 :   if (!no_nl)
    4846              :     {
    4847       196973 :       dumps->bol = true;
    4848       196973 :       fputc ('\n', dumps->stream);
    4849              :     }
    4850              :   return true;
    4851              : }
    4852              : 
    4853              : struct note_def_cache_hasher : ggc_cache_ptr_hash<tree_node>
    4854              : {
    4855       313601 :   static int keep_cache_entry (tree t)
    4856              :   {
    4857       313601 :     if (!CHECKING_P)
    4858              :       /* GTY is unfortunately not clever enough to conditionalize
    4859              :          this.  */
    4860              :       gcc_unreachable ();
    4861              : 
    4862       313601 :     if (ggc_marked_p (t))
    4863              :       return -1;
    4864              : 
    4865            0 :     unsigned n = dump.push (NULL);
    4866              :     /* This might or might not be an error.  We should note its
    4867              :        dropping whichever.  */
    4868            0 :     dump () && dump ("Dropping %N from note_defs table", t);
    4869            0 :     dump.pop (n);
    4870              : 
    4871            0 :     return 0;
    4872              :   }
    4873              : };
    4874              : 
    4875              : /* We should stream each definition at most once.
    4876              :    This needs to be a cache because there are cases where a definition
    4877              :    ends up being not retained, and we need to drop those so we don't
    4878              :    get confused if memory is reallocated.  */
    4879              : typedef hash_table<note_def_cache_hasher> note_defs_table_t;
    4880              : static GTY((cache)) note_defs_table_t *note_defs;
    4881              : 
    4882              : void
    4883       339071 : trees_in::assert_definition (tree decl ATTRIBUTE_UNUSED,
    4884              :                              bool installing ATTRIBUTE_UNUSED)
    4885              : {
    4886              : #if CHECKING_P
    4887       339071 :   tree *slot = note_defs->find_slot (decl, installing ? INSERT : NO_INSERT);
    4888       339071 :   tree not_tmpl = STRIP_TEMPLATE (decl);
    4889       339071 :   if (installing)
    4890              :     {
    4891              :       /* We must be inserting for the first time.  */
    4892       204560 :       gcc_assert (!*slot);
    4893       204560 :       *slot = decl;
    4894              :     }
    4895              :   else
    4896              :     /* If this is not the mergeable entity, it should not be in the
    4897              :        table.  If it is a non-global-module mergeable entity, it
    4898              :        should be in the table.  Global module entities could have been
    4899              :        defined textually in the current TU and so might or might not
    4900              :        be present.  */
    4901       134511 :     gcc_assert (!is_duplicate (decl)
    4902              :                 ? !slot
    4903              :                 : (slot
    4904              :                    || !DECL_LANG_SPECIFIC (not_tmpl)
    4905              :                    || !DECL_MODULE_PURVIEW_P (not_tmpl)
    4906              :                    || (!DECL_MODULE_IMPORT_P (not_tmpl)
    4907              :                        && header_module_p ())));
    4908              : 
    4909       339071 :   if (not_tmpl != decl)
    4910       200922 :     gcc_assert (!note_defs->find_slot (not_tmpl, NO_INSERT));
    4911              : #endif
    4912       339071 : }
    4913              : 
    4914              : void
    4915       384861 : trees_out::assert_definition (tree decl ATTRIBUTE_UNUSED)
    4916              : {
    4917              : #if CHECKING_P
    4918       384861 :   tree *slot = note_defs->find_slot (decl, INSERT);
    4919       384861 :   gcc_assert (!*slot);
    4920       384861 :   *slot = decl;
    4921       384861 :   if (TREE_CODE (decl) == TEMPLATE_DECL)
    4922       214724 :     gcc_assert (!note_defs->find_slot (DECL_TEMPLATE_RESULT (decl), NO_INSERT));
    4923              : #endif
    4924       384861 : }
    4925              : 
    4926              : /********************************************************************/
    4927              : static bool
    4928        12507 : noisy_p ()
    4929              : {
    4930            0 :   if (quiet_flag)
    4931              :     return false;
    4932              : 
    4933            0 :   pp_needs_newline (global_dc->get_reference_printer ()) = true;
    4934            0 :   diagnostic_set_last_function (global_dc,
    4935              :                                 (diagnostics::diagnostic_info *) nullptr);
    4936              : 
    4937            0 :   return true;
    4938              : }
    4939              : 
    4940              : /* Set the cmi repo.  Strip trailing '/', '.' becomes NULL.  */
    4941              : 
    4942              : static void
    4943       101557 : set_cmi_repo (const char *r)
    4944              : {
    4945       101557 :   XDELETEVEC (cmi_repo);
    4946       101557 :   XDELETEVEC (cmi_path);
    4947       101557 :   cmi_path_alloc = 0;
    4948              : 
    4949       101557 :   cmi_repo = NULL;
    4950       101557 :   cmi_repo_length = 0;
    4951              : 
    4952       101557 :   if (!r || !r[0])
    4953              :     return;
    4954              : 
    4955         4851 :   size_t len = strlen (r);
    4956         4851 :   cmi_repo = XNEWVEC (char, len + 1);
    4957         4851 :   memcpy (cmi_repo, r, len + 1);
    4958              : 
    4959         4851 :   if (len > 1 && IS_DIR_SEPARATOR (cmi_repo[len-1]))
    4960         4851 :     len--;
    4961         4851 :   if (len == 1 && cmi_repo[0] == '.')
    4962           21 :     len--;
    4963         4851 :   cmi_repo[len] = 0;
    4964         4851 :   cmi_repo_length = len;
    4965              : }
    4966              : 
    4967              : /* TO is a repo-relative name.  Provide one that we may use from where
    4968              :    we are.  */
    4969              : 
    4970              : static const char *
    4971         5934 : maybe_add_cmi_prefix (const char *to, size_t *len_p = NULL)
    4972              : {
    4973         5934 :   size_t len = len_p || cmi_repo_length ? strlen (to) : 0;
    4974              : 
    4975         5934 :   if (cmi_repo_length && !IS_ABSOLUTE_PATH (to))
    4976              :     {
    4977         5913 :       if (cmi_path_alloc < cmi_repo_length + len + 2)
    4978              :         {
    4979         4735 :           XDELETEVEC (cmi_path);
    4980         4735 :           cmi_path_alloc = cmi_repo_length + len * 2 + 2;
    4981         4735 :           cmi_path = XNEWVEC (char, cmi_path_alloc);
    4982              : 
    4983         4735 :           memcpy (cmi_path, cmi_repo, cmi_repo_length);
    4984         4735 :           cmi_path[cmi_repo_length] = DIR_SEPARATOR;
    4985              :         }
    4986              : 
    4987         5913 :       memcpy (&cmi_path[cmi_repo_length + 1], to, len + 1);
    4988         5913 :       len += cmi_repo_length + 1;
    4989         5913 :       to = cmi_path;
    4990              :     }
    4991              : 
    4992         5934 :   if (len_p)
    4993         2860 :     *len_p = len;
    4994              : 
    4995         5934 :   return to;
    4996              : }
    4997              : 
    4998              : /* Try and create the directories of PATH.  */
    4999              : 
    5000              : static void
    5001           45 : create_dirs (char *path)
    5002              : {
    5003           45 :   char *base = path;
    5004              :   /* Skip past initial slashes of absolute path.  */
    5005           45 :   while (IS_DIR_SEPARATOR (*base))
    5006            0 :     base++;
    5007              : 
    5008              :   /* Try and create the missing directories.  */
    5009         2843 :   for (; *base; base++)
    5010         2798 :     if (IS_DIR_SEPARATOR (*base))
    5011              :       {
    5012          275 :         char sep = *base;
    5013          275 :         *base = 0;
    5014          275 :         int failed = mkdir (path, S_IRWXU | S_IRWXG | S_IRWXO);
    5015          291 :         dump () && dump ("Mkdir ('%s') errno:=%u", path, failed ? errno : 0);
    5016          275 :         *base = sep;
    5017          275 :         if (failed
    5018              :             /* Maybe racing with another creator (of a *different*
    5019              :                module).  */
    5020           49 :             && errno != EEXIST)
    5021              :           break;
    5022              :       }
    5023           45 : }
    5024              : 
    5025              : /* Given a CLASSTYPE_DECL_LIST VALUE get the template friend decl,
    5026              :    if that's what this is.  */
    5027              : 
    5028              : static tree
    5029        80930 : friend_from_decl_list (tree frnd)
    5030              : {
    5031        80930 :   tree res = frnd;
    5032              : 
    5033        80930 :   if (TREE_CODE (frnd) != TEMPLATE_DECL)
    5034              :     {
    5035        48901 :       tree tmpl = NULL_TREE;
    5036        48901 :       if (TYPE_P (frnd))
    5037              :         {
    5038         8010 :           res = TYPE_NAME (frnd);
    5039         7881 :           if (CLASS_TYPE_P (frnd)
    5040        15891 :               && CLASSTYPE_TEMPLATE_INFO (frnd))
    5041         7872 :             tmpl = CLASSTYPE_TI_TEMPLATE (frnd);
    5042              :         }
    5043        40891 :       else if (DECL_TEMPLATE_INFO (frnd))
    5044              :         {
    5045        40891 :           tmpl = DECL_TI_TEMPLATE (frnd);
    5046        40891 :           if (TREE_CODE (tmpl) != TEMPLATE_DECL)
    5047              :             tmpl = NULL_TREE;
    5048              :         }
    5049              : 
    5050        55985 :       if (tmpl && DECL_TEMPLATE_RESULT (tmpl) == res)
    5051              :         res = tmpl;
    5052              :     }
    5053              : 
    5054        80930 :   return res;
    5055              : }
    5056              : 
    5057              : static tree
    5058        29386 : find_enum_member (tree ctx, tree name)
    5059              : {
    5060        29386 :   for (tree values = TYPE_VALUES (ctx);
    5061       485777 :        values; values = TREE_CHAIN (values))
    5062       477259 :     if (DECL_NAME (TREE_VALUE (values)) == name)
    5063              :       return TREE_VALUE (values);
    5064              : 
    5065              :   return NULL_TREE;
    5066              : }
    5067              : 
    5068              : /********************************************************************/
    5069              : /* Instrumentation gathered writing bytes.  */
    5070              : 
    5071              : void
    5072          300 : bytes_out::instrument ()
    5073              : {
    5074          300 :   dump ("Wrote %u bytes in %u blocks", lengths[3], spans[3]);
    5075          300 :   dump ("Wrote %u bits in %u bytes", lengths[0] + lengths[1], lengths[2]);
    5076          900 :   for (unsigned ix = 0; ix < 2; ix++)
    5077          900 :     dump ("  %u %s spans of %R bits", spans[ix],
    5078              :           ix ? "one" : "zero", lengths[ix], spans[ix]);
    5079          300 :   dump ("  %u blocks with %R bits padding", spans[2],
    5080          300 :         lengths[2] * 8 - (lengths[0] + lengths[1]), spans[2]);
    5081          300 : }
    5082              : 
    5083              : /* Instrumentation gathered writing trees.  */
    5084              : void
    5085         2713 : trees_out::instrument ()
    5086              : {
    5087         2713 :   if (dump (""))
    5088              :     {
    5089          300 :       bytes_out::instrument ();
    5090          300 :       dump ("Wrote:");
    5091          300 :       dump ("  %u decl trees", decl_val_count);
    5092          300 :       dump ("  %u other trees", tree_val_count);
    5093          300 :       dump ("  %u back references", back_ref_count);
    5094          300 :       dump ("  %u TU-local entities", tu_local_count);
    5095          300 :       dump ("  %u null trees", null_count);
    5096              :     }
    5097         2713 : }
    5098              : 
    5099              : /* Setup and teardown for a tree walk.  */
    5100              : 
    5101              : void
    5102      2283432 : trees_out::begin ()
    5103              : {
    5104      2283432 :   gcc_assert (!streaming_p () || !tree_map.elements ());
    5105              : 
    5106      2283432 :   mark_trees ();
    5107      2283432 :   if (streaming_p ())
    5108       272411 :     parent::begin ();
    5109      2283432 : }
    5110              : 
    5111              : unsigned
    5112       272411 : trees_out::end (elf_out *sink, unsigned name, unsigned *crc_ptr)
    5113              : {
    5114       272411 :   gcc_checking_assert (streaming_p ());
    5115              : 
    5116       272411 :   unmark_trees ();
    5117       272411 :   return parent::end (sink, name, crc_ptr);
    5118              : }
    5119              : 
    5120              : void
    5121      2011021 : trees_out::end ()
    5122              : {
    5123      2011021 :   gcc_assert (!streaming_p ());
    5124              : 
    5125      2011021 :   unmark_trees ();
    5126              :   /* Do not parent::end -- we weren't streaming.  */
    5127      2011021 : }
    5128              : 
    5129              : void
    5130      2283432 : trees_out::mark_trees ()
    5131              : {
    5132      2283432 :   if (size_t size = tree_map.elements ())
    5133              :     {
    5134              :       /* This isn't our first rodeo, destroy and recreate the
    5135              :          tree_map.  I'm a bad bad man.  Use the previous size as a
    5136              :          guess for the next one (so not all bad).  */
    5137      1752567 :       tree_map.~ptr_int_hash_map ();
    5138      1752567 :       new (&tree_map) ptr_int_hash_map (size);
    5139              :     }
    5140              : 
    5141              :   /* Install the fixed trees, with +ve references.  */
    5142      2283432 :   unsigned limit = fixed_trees->length ();
    5143    439804352 :   for (unsigned ix = 0; ix != limit; ix++)
    5144              :     {
    5145    437520920 :       tree val = (*fixed_trees)[ix];
    5146    437520920 :       bool existed = tree_map.put (val, ix + tag_fixed);
    5147    437520920 :       gcc_checking_assert (!TREE_VISITED (val) && !existed);
    5148    437520920 :       TREE_VISITED (val) = true;
    5149              :     }
    5150              : 
    5151      2283432 :   ref_num = 0;
    5152      2283432 : }
    5153              : 
    5154              : /* Unmark the trees we encountered  */
    5155              : 
    5156              : void
    5157      2283432 : trees_out::unmark_trees ()
    5158              : {
    5159      2283432 :   ptr_int_hash_map::iterator end (tree_map.end ());
    5160    520352198 :   for (ptr_int_hash_map::iterator iter (tree_map.begin ()); iter != end; ++iter)
    5161              :     {
    5162    518068766 :       tree node = reinterpret_cast<tree> ((*iter).first);
    5163    518068766 :       int ref = (*iter).second;
    5164              :       /* We should have visited the node, and converted its mergeable
    5165              :          reference to a regular reference.  */
    5166    518068766 :       gcc_checking_assert (TREE_VISITED (node)
    5167              :                            && (ref <= tag_backref || ref >= tag_fixed));
    5168    518068766 :       TREE_VISITED (node) = false;
    5169              :     }
    5170      2283432 : }
    5171              : 
    5172              : /* Mark DECL for by-value walking.  We do this by inserting it into
    5173              :    the tree map with a reference of zero.  May be called multiple
    5174              :    times on the same node.  */
    5175              : 
    5176              : void
    5177      3529072 : trees_out::mark_by_value (tree decl)
    5178              : {
    5179      3529072 :   gcc_checking_assert (DECL_P (decl)
    5180              :                        /* Enum consts are INTEGER_CSTS.  */
    5181              :                        || TREE_CODE (decl) == INTEGER_CST
    5182              :                        || TREE_CODE (decl) == TREE_BINFO);
    5183              : 
    5184      3529072 :   if (TREE_VISITED (decl))
    5185              :     /* Must already be forced or fixed.  */
    5186         3586 :     gcc_checking_assert (*tree_map.get (decl) >= tag_value);
    5187              :   else
    5188              :     {
    5189      3525486 :       bool existed = tree_map.put (decl, tag_value);
    5190      3525486 :       gcc_checking_assert (!existed);
    5191      3525486 :       TREE_VISITED (decl) = true;
    5192              :     }
    5193      3529072 : }
    5194              : 
    5195              : int
    5196     96842503 : trees_out::get_tag (tree t)
    5197              : {
    5198     96842503 :   gcc_checking_assert (TREE_VISITED (t));
    5199     96842503 :   return *tree_map.get (t);
    5200              : }
    5201              : 
    5202              : /* Insert T into the map, return its tag number.    */
    5203              : 
    5204              : int
    5205     80547846 : trees_out::insert (tree t, walk_kind walk)
    5206              : {
    5207     80547846 :   gcc_checking_assert (walk != WK_normal || !TREE_VISITED (t));
    5208     80547846 :   int tag = --ref_num;
    5209     80547846 :   bool existed;
    5210     80547846 :   int &slot = tree_map.get_or_insert (t, &existed);
    5211     80547846 :   gcc_checking_assert (TREE_VISITED (t) == existed
    5212              :                        && (!existed
    5213              :                            || (walk == WK_value && slot == tag_value)));
    5214     80547846 :   TREE_VISITED (t) = true;
    5215     80547846 :   slot = tag;
    5216              : 
    5217     80547846 :   return tag;
    5218              : }
    5219              : 
    5220              : /* Insert T into the backreference array.  Return its back reference
    5221              :    number.  */
    5222              : 
    5223              : int
    5224     21015845 : trees_in::insert (tree t)
    5225              : {
    5226     21015845 :   gcc_checking_assert (t || get_overrun ());
    5227     21015845 :   back_refs.safe_push (t);
    5228     21015845 :   return -(int)back_refs.length ();
    5229              : }
    5230              : 
    5231              : /* A chained set of decls.  */
    5232              : 
    5233              : void
    5234       128536 : trees_out::chained_decls (tree decls)
    5235              : {
    5236       292565 :   for (; decls; decls = DECL_CHAIN (decls))
    5237       164029 :     tree_node (decls);
    5238       128536 :   tree_node (NULL_TREE);
    5239       128536 : }
    5240              : 
    5241              : tree
    5242        57004 : trees_in::chained_decls ()
    5243              : {
    5244        57004 :   tree decls = NULL_TREE;
    5245        57004 :   for (tree *chain = &decls;;)
    5246       136519 :     if (tree decl = tree_node ())
    5247              :       {
    5248        79515 :         if (!DECL_P (decl) || DECL_CHAIN (decl))
    5249              :           {
    5250            0 :             set_overrun ();
    5251            0 :             break;
    5252              :           }
    5253        79515 :         *chain = decl;
    5254        79515 :         chain = &DECL_CHAIN (decl);
    5255              :       }
    5256              :     else
    5257        79515 :       break;
    5258              : 
    5259        57004 :   return decls;
    5260              : }
    5261              : 
    5262              : /* A vector of decls following DECL_CHAIN.  */
    5263              : 
    5264              : void
    5265       335612 : trees_out::vec_chained_decls (tree decls)
    5266              : {
    5267       335612 :   if (streaming_p ())
    5268              :     {
    5269              :       unsigned len = 0;
    5270              : 
    5271      1025703 :       for (tree decl = decls; decl; decl = DECL_CHAIN (decl))
    5272       857943 :         len++;
    5273       167760 :       u (len);
    5274              :     }
    5275              : 
    5276      2051908 :   for (tree decl = decls; decl; decl = DECL_CHAIN (decl))
    5277              :     {
    5278       354689 :       if (DECL_IMPLICIT_TYPEDEF_P (decl)
    5279      1729429 :           && TYPE_NAME (TREE_TYPE (decl)) != decl)
    5280              :         /* An anonynmous struct with a typedef name.  An odd thing to
    5281              :            write.  */
    5282            8 :         tree_node (NULL_TREE);
    5283              :       else
    5284      1716288 :         tree_node (decl);
    5285              :     }
    5286       335612 : }
    5287              : 
    5288              : vec<tree, va_heap> *
    5289       132315 : trees_in::vec_chained_decls ()
    5290              : {
    5291       132315 :   vec<tree, va_heap> *v = NULL;
    5292              : 
    5293       132315 :   if (unsigned len = u ())
    5294              :     {
    5295        69471 :       vec_alloc (v, len);
    5296              : 
    5297       841966 :       for (unsigned ix = 0; ix < len; ix++)
    5298              :         {
    5299       772495 :           tree decl = tree_node ();
    5300       772495 :           if (decl && !DECL_P (decl))
    5301              :             {
    5302            0 :               set_overrun ();
    5303            0 :               break;
    5304              :             }
    5305       772495 :           v->quick_push (decl);
    5306              :         }
    5307              : 
    5308        69471 :       if (get_overrun ())
    5309              :         {
    5310            0 :           vec_free (v);
    5311            0 :           v = NULL;
    5312              :         }
    5313              :     }
    5314              : 
    5315       132315 :   return v;
    5316              : }
    5317              : 
    5318              : /* A vector of trees.  */
    5319              : 
    5320              : void
    5321       236636 : trees_out::tree_vec (vec<tree, va_gc> *v)
    5322              : {
    5323       236636 :   unsigned len = vec_safe_length (v);
    5324       236636 :   if (streaming_p ())
    5325       118295 :     u (len);
    5326       303644 :   for (unsigned ix = 0; ix != len; ix++)
    5327        67008 :     tree_node ((*v)[ix]);
    5328       236636 : }
    5329              : 
    5330              : vec<tree, va_gc> *
    5331        92874 : trees_in::tree_vec ()
    5332              : {
    5333        92874 :   vec<tree, va_gc> *v = NULL;
    5334        92874 :   if (unsigned len = u ())
    5335              :     {
    5336        23569 :       vec_alloc (v, len);
    5337        49753 :       for (unsigned ix = 0; ix != len; ix++)
    5338        26184 :         v->quick_push (tree_node ());
    5339              :     }
    5340        92874 :   return v;
    5341              : }
    5342              : 
    5343              : /* A vector of tree pairs.  */
    5344              : 
    5345              : void
    5346         5988 : trees_out::tree_pair_vec (vec<tree_pair_s, va_gc> *v)
    5347              : {
    5348         5988 :   unsigned len = vec_safe_length (v);
    5349         5988 :   if (streaming_p ())
    5350         2994 :     u (len);
    5351         5988 :   if (len)
    5352        32136 :     for (unsigned ix = 0; ix != len; ix++)
    5353              :       {
    5354        26266 :         tree_pair_s const &s = (*v)[ix];
    5355        26266 :         tree_node (s.purpose);
    5356        26266 :         tree_node (s.value);
    5357              :       }
    5358         5988 : }
    5359              : 
    5360              : vec<tree_pair_s, va_gc> *
    5361         2671 : trees_in::tree_pair_vec ()
    5362              : {
    5363         2671 :   vec<tree_pair_s, va_gc> *v = NULL;
    5364         2671 :   if (unsigned len = u ())
    5365              :     {
    5366         2617 :       vec_alloc (v, len);
    5367        14575 :       for (unsigned ix = 0; ix != len; ix++)
    5368              :         {
    5369        11958 :           tree_pair_s s;
    5370        11958 :           s.purpose = tree_node ();
    5371        11958 :           s.value = tree_node ();
    5372        11958 :           v->quick_push (s);
    5373              :       }
    5374              :     }
    5375         2671 :   return v;
    5376              : }
    5377              : 
    5378              : void
    5379       356342 : trees_out::tree_list (tree list, bool has_purpose)
    5380              : {
    5381      1549665 :   for (; list; list = TREE_CHAIN (list))
    5382              :     {
    5383      1193323 :       gcc_checking_assert (TREE_VALUE (list));
    5384      1193323 :       tree_node (TREE_VALUE (list));
    5385      1193323 :       if (has_purpose)
    5386      1153535 :         tree_node (TREE_PURPOSE (list));
    5387              :     }
    5388       356342 :   tree_node (NULL_TREE);
    5389       356342 : }
    5390              : 
    5391              : tree
    5392       143034 : trees_in::tree_list (bool has_purpose)
    5393              : {
    5394       143034 :   tree res = NULL_TREE;
    5395              : 
    5396       685713 :   for (tree *chain = &res; tree value = tree_node ();
    5397      1085358 :        chain = &TREE_CHAIN (*chain))
    5398              :     {
    5399       542679 :       tree purpose = has_purpose ? tree_node () : NULL_TREE;
    5400       542679 :       *chain = build_tree_list (purpose, value);
    5401       542679 :     }
    5402              : 
    5403       143034 :   return res;
    5404              : }
    5405              : 
    5406              : #define CASE_OMP_SIMD_CODE \
    5407              :     case OMP_SIMD:                      \
    5408              :     case OMP_STRUCTURED_BLOCK:          \
    5409              :     case OMP_LOOP:                      \
    5410              :     case OMP_ORDERED:                   \
    5411              :     case OMP_TILE:                      \
    5412              :     case OMP_UNROLL
    5413              : #define CASE_OMP_CODE \
    5414              :     case OMP_PARALLEL:                  \
    5415              :     case OMP_TASK:                      \
    5416              :     case OMP_FOR:                       \
    5417              :     case OMP_DISTRIBUTE:                \
    5418              :     case OMP_TASKLOOP:                  \
    5419              :     case OMP_TEAMS:                     \
    5420              :     case OMP_TARGET_DATA:               \
    5421              :     case OMP_TARGET:                    \
    5422              :     case OMP_SECTIONS:                  \
    5423              :     case OMP_CRITICAL:                  \
    5424              :     case OMP_SINGLE:                    \
    5425              :     case OMP_SCOPE:                     \
    5426              :     case OMP_TASKGROUP:                 \
    5427              :     case OMP_MASKED:                    \
    5428              :     case OMP_DISPATCH:                  \
    5429              :     case OMP_INTEROP:                   \
    5430              :     case OMP_MASTER:                    \
    5431              :     case OMP_TARGET_UPDATE:             \
    5432              :     case OMP_TARGET_ENTER_DATA:         \
    5433              :     case OMP_TARGET_EXIT_DATA:          \
    5434              :     case OMP_METADIRECTIVE:             \
    5435              :     case OMP_ATOMIC:                    \
    5436              :     case OMP_ATOMIC_READ:               \
    5437              :     case OMP_ATOMIC_CAPTURE_OLD:        \
    5438              :     case OMP_ATOMIC_CAPTURE_NEW
    5439              : #define CASE_OACC_CODE \
    5440              :     case OACC_PARALLEL:                 \
    5441              :     case OACC_KERNELS:                  \
    5442              :     case OACC_SERIAL:                   \
    5443              :     case OACC_DATA:                     \
    5444              :     case OACC_HOST_DATA:                \
    5445              :     case OACC_LOOP:                     \
    5446              :     case OACC_CACHE:                    \
    5447              :     case OACC_DECLARE:                  \
    5448              :     case OACC_ENTER_DATA:               \
    5449              :     case OACC_EXIT_DATA:                \
    5450              :     case OACC_UPDATE
    5451              : 
    5452              : /* Start tree write.  Write information to allocate the receiving
    5453              :    node.  */
    5454              : 
    5455              : void
    5456     16281765 : trees_out::start (tree t, bool code_streamed)
    5457              : {
    5458     16281765 :   if (TYPE_P (t))
    5459              :     {
    5460       636523 :       enum tree_code code = TREE_CODE (t);
    5461       636523 :       gcc_checking_assert (TYPE_MAIN_VARIANT (t) == t);
    5462              :       /* All these types are TYPE_NON_COMMON.  */
    5463       636523 :       gcc_checking_assert (code == RECORD_TYPE
    5464              :                            || code == UNION_TYPE
    5465              :                            || code == ENUMERAL_TYPE
    5466              :                            || code == TEMPLATE_TYPE_PARM
    5467              :                            || code == TEMPLATE_TEMPLATE_PARM
    5468              :                            || code == BOUND_TEMPLATE_TEMPLATE_PARM);
    5469              :     }
    5470              : 
    5471     16281765 :   if (!code_streamed)
    5472     15721465 :     u (TREE_CODE (t));
    5473              : 
    5474     16281765 :   switch (TREE_CODE (t))
    5475              :     {
    5476     14518871 :     default:
    5477     14518871 :       if (VL_EXP_CLASS_P (t))
    5478       616421 :         u (VL_EXP_OPERAND_LENGTH (t));
    5479              :       break;
    5480              : 
    5481       689208 :     case INTEGER_CST:
    5482       689208 :       u (TREE_INT_CST_NUNITS (t));
    5483       689208 :       u (TREE_INT_CST_EXT_NUNITS (t));
    5484       689208 :       break;
    5485              : 
    5486           18 :     case OMP_CLAUSE:
    5487           18 :       u (OMP_CLAUSE_CODE (t));
    5488           18 :       break;
    5489              : 
    5490            6 :     CASE_OMP_SIMD_CODE:
    5491            6 :       state->extensions |= SE_OPENMP_SIMD;
    5492            6 :       break;
    5493              : 
    5494            9 :     CASE_OMP_CODE:
    5495            9 :       state->extensions |= SE_OPENMP;
    5496            9 :       break;
    5497              : 
    5498            6 :     CASE_OACC_CODE:
    5499            6 :       state->extensions |= SE_OPENACC;
    5500            6 :       break;
    5501              : 
    5502        49046 :     case STRING_CST:
    5503        49046 :       str (TREE_STRING_POINTER (t), TREE_STRING_LENGTH (t));
    5504        49046 :       break;
    5505              : 
    5506           18 :     case RAW_DATA_CST:
    5507           18 :       if (RAW_DATA_OWNER (t) == NULL_TREE)
    5508              :         {
    5509              :           /* Stream RAW_DATA_CST with no owner (i.e. data pointing
    5510              :              into libcpp buffers) as something we can stream in as
    5511              :              STRING_CST which owns the data.  */
    5512            6 :           u (0);
    5513              :           /* Can't use str (RAW_DATA_POINTER (t), RAW_DATA_LENGTH (t));
    5514              :              here as there isn't a null termination after it.  */
    5515            6 :           z (RAW_DATA_LENGTH (t));
    5516            6 :           if (RAW_DATA_LENGTH (t))
    5517            6 :             if (void *ptr = buf (RAW_DATA_LENGTH (t) + 1))
    5518              :               {
    5519            6 :                 memcpy (ptr, RAW_DATA_POINTER (t), RAW_DATA_LENGTH (t));
    5520            6 :                 ((char *) ptr)[RAW_DATA_LENGTH (t)] = '\0';
    5521              :               }
    5522              :         }
    5523              :       else
    5524              :         {
    5525           12 :           gcc_assert (RAW_DATA_LENGTH (t));
    5526           12 :           u (RAW_DATA_LENGTH (t));
    5527              :         }
    5528              :       break;
    5529              : 
    5530           18 :     case VECTOR_CST:
    5531           18 :       u (VECTOR_CST_LOG2_NPATTERNS (t));
    5532           18 :       u (VECTOR_CST_NELTS_PER_PATTERN (t));
    5533           18 :       break;
    5534              : 
    5535       115301 :     case TREE_BINFO:
    5536       115301 :       u (BINFO_N_BASE_BINFOS (t));
    5537       115301 :       break;
    5538              : 
    5539       909264 :     case TREE_VEC:
    5540       909264 :       u (TREE_VEC_LENGTH (t));
    5541       909264 :       break;
    5542              : 
    5543            0 :     case FIXED_CST:
    5544            0 :       gcc_unreachable (); /* Not supported in C++.  */
    5545            0 :       break;
    5546              : 
    5547            0 :     case IDENTIFIER_NODE:
    5548            0 :     case SSA_NAME:
    5549            0 :     case TARGET_MEM_REF:
    5550            0 :     case TRANSLATION_UNIT_DECL:
    5551              :       /* We shouldn't meet these.  */
    5552            0 :       gcc_unreachable ();
    5553     16281765 :       break;
    5554              :     }
    5555     16281765 : }
    5556              : 
    5557              : /* Start tree read.  Allocate the receiving node.  */
    5558              : 
    5559              : tree
    5560     15126656 : trees_in::start (unsigned code)
    5561              : {
    5562     15126656 :   tree t = NULL_TREE;
    5563              : 
    5564     15126656 :   if (!code)
    5565     13701968 :     code = u ();
    5566              : 
    5567     15126656 :   switch (code)
    5568              :     {
    5569     13558124 :     default:
    5570     13558124 :       if (code >= MAX_TREE_CODES)
    5571              :         {
    5572            0 :         fail:
    5573            0 :           set_overrun ();
    5574            0 :           return NULL_TREE;
    5575              :         }
    5576     13558124 :       else if (TREE_CODE_CLASS (code) == tcc_vl_exp)
    5577              :         {
    5578       589410 :           unsigned ops = u ();
    5579       589410 :           t = build_vl_exp (tree_code (code), ops);
    5580              :         }
    5581              :       else
    5582     12968714 :         t = make_node (tree_code (code));
    5583              :       break;
    5584              : 
    5585       589438 :     case INTEGER_CST:
    5586       589438 :       {
    5587       589438 :         unsigned n = u ();
    5588       589438 :         unsigned e = u ();
    5589       589438 :         t = make_int_cst (n, e);
    5590              :       }
    5591       589438 :       break;
    5592              : 
    5593           18 :     case OMP_CLAUSE:
    5594           18 :       t = build_omp_clause (UNKNOWN_LOCATION, omp_clause_code (u ()));
    5595           18 :       break;
    5596              : 
    5597            9 :     CASE_OMP_SIMD_CODE:
    5598            9 :       if (!(state->extensions & SE_OPENMP_SIMD))
    5599            0 :         goto fail;
    5600            9 :       t = make_node (tree_code (code));
    5601            9 :       break;
    5602              : 
    5603            9 :     CASE_OMP_CODE:
    5604            9 :       if (!(state->extensions & SE_OPENMP))
    5605            0 :         goto fail;
    5606            9 :       t = make_node (tree_code (code));
    5607            9 :       break;
    5608              : 
    5609            6 :     CASE_OACC_CODE:
    5610            6 :       if (!(state->extensions & SE_OPENACC))
    5611            0 :         goto fail;
    5612            6 :       t = make_node (tree_code (code));
    5613            6 :       break;
    5614              : 
    5615        50063 :     case STRING_CST:
    5616        50063 :       {
    5617        50063 :         size_t l;
    5618        50063 :         const char *chars = str (&l);
    5619        50063 :         t = build_string (l, chars);
    5620              :       }
    5621        50063 :       break;
    5622              : 
    5623            9 :     case RAW_DATA_CST:
    5624            9 :       {
    5625            9 :         size_t l = u ();
    5626            9 :         if (l == 0)
    5627              :           {
    5628              :             /* Stream in RAW_DATA_CST with no owner as STRING_CST
    5629              :                which owns the data.  */
    5630            3 :             const char *chars = str (&l);
    5631            3 :             t = build_string (l, chars);
    5632              :           }
    5633              :         else
    5634              :           {
    5635            6 :             t = make_node (RAW_DATA_CST);
    5636            6 :             RAW_DATA_LENGTH (t) = l;
    5637              :           }
    5638              :       }
    5639            9 :       break;
    5640              : 
    5641           24 :     case VECTOR_CST:
    5642           24 :       {
    5643           24 :         unsigned log2_npats = u ();
    5644           24 :         unsigned elts_per = u ();
    5645           24 :         t = make_vector (log2_npats, elts_per);
    5646              :       }
    5647           24 :       break;
    5648              : 
    5649        90203 :     case TREE_BINFO:
    5650        90203 :       t = make_tree_binfo (u ());
    5651        90203 :       break;
    5652              : 
    5653       838753 :     case TREE_VEC:
    5654       838753 :       t = make_tree_vec (u ());
    5655       838753 :       break;
    5656              : 
    5657            0 :     case FIXED_CST:
    5658            0 :     case IDENTIFIER_NODE:
    5659            0 :     case SSA_NAME:
    5660            0 :     case TARGET_MEM_REF:
    5661            0 :     case TRANSLATION_UNIT_DECL:
    5662            0 :       goto fail;
    5663              :     }
    5664              : 
    5665              :   return t;
    5666              : }
    5667              : 
    5668              : /* The kinds of interface an importer could have for a decl.  */
    5669              : 
    5670              : enum class importer_interface {
    5671              :   unknown,        /* The definition may or may not need to be emitted.  */
    5672              :   external,       /* The definition can always be found in another TU.  */
    5673              :   internal,       /* The definition should be emitted in the importer's TU.  */
    5674              :   always_emit,    /* The definition must be emitted in the importer's TU,
    5675              :                      regardless of if it's used or not. */
    5676              : };
    5677              : 
    5678              : /* Returns what kind of interface an importer will have of DECL.  */
    5679              : 
    5680              : static importer_interface
    5681       603673 : get_importer_interface (tree decl)
    5682              : {
    5683              :   /* Internal linkage entities must be emitted in each importer if
    5684              :      there is a definition available.  */
    5685       603673 :   if (!TREE_PUBLIC (decl))
    5686              :     return importer_interface::internal;
    5687              : 
    5688              :   /* Other entities that aren't vague linkage are either not definitions
    5689              :      or will be publicly emitted in this TU, so importers can just refer
    5690              :      to an external definition.  */
    5691       298507 :   if (!vague_linkage_p (decl))
    5692              :     return importer_interface::external;
    5693              : 
    5694              :   /* For explicit instantiations, importers can always rely on there
    5695              :      being a definition in another TU, unless this is a definition
    5696              :      in a header module: in which case the importer will always need
    5697              :      to emit it.  */
    5698       292639 :   if (DECL_LANG_SPECIFIC (decl)
    5699       292639 :       && DECL_EXPLICIT_INSTANTIATION (decl))
    5700        22579 :     return (header_module_p () && !DECL_EXTERNAL (decl)
    5701        22579 :             ? importer_interface::always_emit
    5702              :             : importer_interface::external);
    5703              : 
    5704              :   /* A gnu_inline function is never emitted in any TU.  */
    5705       270060 :   if (TREE_CODE (decl) == FUNCTION_DECL
    5706       193425 :       && DECL_DECLARED_INLINE_P (decl)
    5707       456870 :       && lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl)))
    5708              :     return importer_interface::external;
    5709              : 
    5710              :   /* Everything else has vague linkage.  */
    5711              :   return importer_interface::unknown;
    5712              : }
    5713              : 
    5714              : /* The structure streamers access the raw fields, because the
    5715              :    alternative, of using the accessor macros can require using
    5716              :    different accessors for the same underlying field, depending on the
    5717              :    tree code.  That's both confusing and annoying.  */
    5718              : 
    5719              : /* Read & write the core boolean flags.  */
    5720              : 
    5721              : void
    5722     16309192 : trees_out::core_bools (tree t, bits_out& bits)
    5723              : {
    5724              : #define WB(X) (bits.b (X))
    5725              : /* Stream X if COND holds, and if !COND stream a dummy value so that the
    5726              :    overall number of bits streamed is independent of the runtime value
    5727              :    of COND, which allows the compiler to better optimize this function.  */
    5728              : #define WB_IF(COND, X) WB ((COND) ? (X) : false)
    5729     16309192 :   tree_code code = TREE_CODE (t);
    5730              : 
    5731     16309192 :   WB (t->base.side_effects_flag);
    5732     16309192 :   WB (t->base.constant_flag);
    5733     16309192 :   WB (t->base.addressable_flag);
    5734     16309192 :   WB (t->base.volatile_flag);
    5735     16309192 :   WB (t->base.readonly_flag);
    5736              :   /* base.asm_written_flag is a property of the current TU's use of
    5737              :      this decl.  */
    5738     16309192 :   WB (t->base.nowarning_flag);
    5739              :   /* base.visited read as zero (it's set for writer, because that's
    5740              :      how we mark nodes).  */
    5741              :   /* base.used_flag is not streamed.  Readers may set TREE_USED of
    5742              :      decls they use.  */
    5743     16309192 :   WB (t->base.nothrow_flag);
    5744     16309192 :   WB (t->base.static_flag);
    5745              :   /* This is TYPE_CACHED_VALUES_P for types.  */
    5746     16309192 :   WB_IF (TREE_CODE_CLASS (code) != tcc_type, t->base.public_flag);
    5747     16309192 :   WB (t->base.private_flag);
    5748     16309192 :   WB (t->base.protected_flag);
    5749     16309192 :   WB (t->base.deprecated_flag);
    5750     16309192 :   WB (t->base.default_def_flag);
    5751              : 
    5752     16309192 :   switch (code)
    5753              :     {
    5754              :     case CALL_EXPR:
    5755              :     case INTEGER_CST:
    5756              :     case SSA_NAME:
    5757              :     case TARGET_MEM_REF:
    5758              :     case TREE_VEC:
    5759              :       /* These use different base.u fields.  */
    5760              :       return;
    5761              : 
    5762     14108021 :     default:
    5763     14108021 :       WB (t->base.u.bits.lang_flag_0);
    5764     14108021 :       bool flag_1 = t->base.u.bits.lang_flag_1;
    5765     14108021 :       if (!flag_1)
    5766              :         ;
    5767       451539 :       else if (code == TEMPLATE_INFO)
    5768              :         /* This is TI_PENDING_TEMPLATE_FLAG, not relevant to reader.  */
    5769              :         flag_1 = false;
    5770       446263 :       else if (code == VAR_DECL)
    5771              :         {
    5772              :           /* This is DECL_INITIALIZED_P.  */
    5773        83722 :           if (TREE_CODE (DECL_CONTEXT (t)) != FUNCTION_DECL)
    5774              :             /* We'll set this when reading the definition.  */
    5775     14108021 :             flag_1 = false;
    5776              :         }
    5777     14108021 :       WB (flag_1);
    5778     14108021 :       WB (t->base.u.bits.lang_flag_2);
    5779     14108021 :       WB (t->base.u.bits.lang_flag_3);
    5780     14108021 :       WB (t->base.u.bits.lang_flag_4);
    5781     14108021 :       WB (t->base.u.bits.lang_flag_5);
    5782     14108021 :       WB (t->base.u.bits.lang_flag_6);
    5783     14108021 :       WB (t->base.u.bits.saturating_flag);
    5784     14108021 :       WB (t->base.u.bits.unsigned_flag);
    5785     14108021 :       WB (t->base.u.bits.packed_flag);
    5786     14108021 :       WB (t->base.u.bits.user_align);
    5787     14108021 :       WB (t->base.u.bits.nameless_flag);
    5788     14108021 :       WB (t->base.u.bits.atomic_flag);
    5789     14108021 :       WB (t->base.u.bits.unavailable_flag);
    5790     14108021 :       break;
    5791              :     }
    5792              : 
    5793     14108021 :   if (TREE_CODE_CLASS (code) == tcc_type)
    5794              :     {
    5795       663950 :       WB (t->type_common.no_force_blk_flag);
    5796       663950 :       WB (t->type_common.needs_constructing_flag);
    5797       663950 :       WB (t->type_common.transparent_aggr_flag);
    5798       663950 :       WB (t->type_common.restrict_flag);
    5799       663950 :       WB (t->type_common.string_flag);
    5800       663950 :       WB (t->type_common.lang_flag_0);
    5801       663950 :       WB (t->type_common.lang_flag_1);
    5802       663950 :       WB (t->type_common.lang_flag_2);
    5803       663950 :       WB (t->type_common.lang_flag_3);
    5804       663950 :       WB (t->type_common.lang_flag_4);
    5805       663950 :       WB (t->type_common.lang_flag_5);
    5806       663950 :       WB (t->type_common.lang_flag_6);
    5807       663950 :       WB (t->type_common.typeless_storage);
    5808              :     }
    5809              : 
    5810     14108021 :   if (TREE_CODE_CLASS (code) != tcc_declaration)
    5811              :     return;
    5812              : 
    5813      3563662 :   if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
    5814              :     {
    5815      3563662 :       WB (t->decl_common.nonlocal_flag);
    5816      3563662 :       WB (t->decl_common.virtual_flag);
    5817      3563662 :       WB (t->decl_common.ignored_flag);
    5818      3563662 :       WB (t->decl_common.abstract_flag);
    5819      3563662 :       WB (t->decl_common.artificial_flag);
    5820      3563662 :       WB (t->decl_common.preserve_flag);
    5821      3563662 :       WB (t->decl_common.debug_expr_is_from);
    5822      3563662 :       WB (t->decl_common.lang_flag_0);
    5823      3563662 :       WB (t->decl_common.lang_flag_1);
    5824      3563662 :       WB (t->decl_common.lang_flag_2);
    5825      3563662 :       WB (t->decl_common.lang_flag_3);
    5826      3563662 :       WB (t->decl_common.lang_flag_4);
    5827              : 
    5828      3563662 :       {
    5829              :         /* This is DECL_INTERFACE_KNOWN: We should redetermine whether
    5830              :            we need to import or export any vague-linkage entities on
    5831              :            stream-in.  */
    5832      3563662 :         bool interface_known = t->decl_common.lang_flag_5;
    5833      3563662 :         if (interface_known
    5834      3563662 :             && get_importer_interface (t) == importer_interface::unknown)
    5835              :           interface_known = false;
    5836      3563662 :         WB (interface_known);
    5837              :       }
    5838              : 
    5839      3563662 :       WB (t->decl_common.lang_flag_6);
    5840      3563662 :       WB (t->decl_common.lang_flag_7);
    5841      3563662 :       WB (t->decl_common.lang_flag_8);
    5842      3563662 :       WB (t->decl_common.decl_flag_0);
    5843              : 
    5844      3563662 :       {
    5845              :         /* DECL_EXTERNAL -> decl_flag_1
    5846              :              == it is defined elsewhere
    5847              :            DECL_NOT_REALLY_EXTERN -> base.not_really_extern
    5848              :              == that was a lie, it is here  */
    5849              : 
    5850      3563662 :         bool is_external = t->decl_common.decl_flag_1;
    5851              :         /* maybe_emit_vtables relies on vtables being marked as
    5852              :            DECL_EXTERNAL and DECL_NOT_REALLY_EXTERN before processing.  */
    5853      3563662 :         if (!is_external && VAR_P (t) && DECL_VTABLE_OR_VTT_P (t))
    5854              :           is_external = true;
    5855              :         /* Things we emit here might well be external from the POV of an
    5856              :            importer.  */
    5857      3563382 :         if (!is_external
    5858      2991921 :             && VAR_OR_FUNCTION_DECL_P (t)
    5859      3833790 :             && get_importer_interface (t) == importer_interface::external)
    5860              :           is_external = true;
    5861      3563662 :         WB (is_external);
    5862              :       }
    5863              : 
    5864      3563662 :       WB (t->decl_common.decl_flag_2);
    5865      3563662 :       WB (t->decl_common.decl_flag_3);
    5866      3563662 :       WB (t->decl_common.not_gimple_reg_flag);
    5867      3563662 :       WB (t->decl_common.decl_by_reference_flag);
    5868      3563662 :       WB (t->decl_common.decl_read_flag);
    5869      3563662 :       WB (t->decl_common.decl_nonshareable_flag);
    5870      3563662 :       WB (t->decl_common.decl_not_flexarray);
    5871              :     }
    5872              :   else
    5873              :     return;
    5874              : 
    5875      3563662 :   if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
    5876              :     {
    5877      1680840 :       WB (t->decl_with_vis.defer_output);
    5878      1680840 :       WB (t->decl_with_vis.hard_register);
    5879      1680840 :       WB (t->decl_with_vis.common_flag);
    5880      1680840 :       WB (t->decl_with_vis.in_text_section);
    5881      1680840 :       WB (t->decl_with_vis.in_constant_pool);
    5882      1680840 :       WB (t->decl_with_vis.dllimport_flag);
    5883      1680840 :       WB (t->decl_with_vis.weak_flag);
    5884      1680840 :       WB (t->decl_with_vis.seen_in_bind_expr);
    5885      1680840 :       WB (t->decl_with_vis.comdat_flag);
    5886      1680840 :       WB (t->decl_with_vis.visibility_specified);
    5887      1680840 :       WB (t->decl_with_vis.init_priority_p);
    5888      1680840 :       WB (t->decl_with_vis.shadowed_for_var_p);
    5889      1680840 :       WB (t->decl_with_vis.cxx_constructor);
    5890      1680840 :       WB (t->decl_with_vis.cxx_destructor);
    5891      1680840 :       WB (t->decl_with_vis.final);
    5892      1680840 :       WB (t->decl_with_vis.regdecl_flag);
    5893              :     }
    5894              :   else
    5895              :     return;
    5896              : 
    5897      1680840 :   if (CODE_CONTAINS_STRUCT (code, TS_FUNCTION_DECL))
    5898              :     {
    5899       492952 :       WB (t->function_decl.static_ctor_flag);
    5900       492952 :       WB (t->function_decl.static_dtor_flag);
    5901       492952 :       WB (t->function_decl.uninlinable);
    5902       492952 :       WB (t->function_decl.possibly_inlined);
    5903       492952 :       WB (t->function_decl.novops_flag);
    5904       492952 :       WB (t->function_decl.returns_twice_flag);
    5905       492952 :       WB (t->function_decl.malloc_flag);
    5906       492952 :       WB (t->function_decl.declared_inline_flag);
    5907       492952 :       WB (t->function_decl.no_inline_warning_flag);
    5908       492952 :       WB (t->function_decl.no_instrument_function_entry_exit);
    5909       492952 :       WB (t->function_decl.no_limit_stack);
    5910       492952 :       WB (t->function_decl.disregard_inline_limits);
    5911       492952 :       WB (t->function_decl.pure_flag);
    5912       492952 :       WB (t->function_decl.looping_const_or_pure_flag);
    5913              : 
    5914       492952 :       WB (t->function_decl.has_debug_args_flag);
    5915       492952 :       WB (t->function_decl.versioned_function);
    5916       492952 :       WB (t->function_decl.replaceable_operator);
    5917              : 
    5918              :       /* decl_type is a (misnamed) 2 bit discriminator.  */
    5919       492952 :       unsigned kind = (unsigned)t->function_decl.decl_type;
    5920       492952 :       WB ((kind >> 0) & 1);
    5921       492952 :       WB ((kind >> 1) & 1);
    5922              :     }
    5923              : #undef WB_IF
    5924              : #undef WB
    5925              : }
    5926              : 
    5927              : bool
    5928     15148036 : trees_in::core_bools (tree t, bits_in& bits)
    5929              : {
    5930              : #define RB(X) ((X) = bits.b ())
    5931              : /* See the comment for WB_IF in trees_out::core_bools.  */
    5932              : #define RB_IF(COND, X) ((COND) ? RB (X) : bits.b ())
    5933              : 
    5934     15148036 :   tree_code code = TREE_CODE (t);
    5935              : 
    5936     15148036 :   RB (t->base.side_effects_flag);
    5937     15148036 :   RB (t->base.constant_flag);
    5938     15148036 :   RB (t->base.addressable_flag);
    5939     15148036 :   RB (t->base.volatile_flag);
    5940     15148036 :   RB (t->base.readonly_flag);
    5941              :   /* base.asm_written_flag is not streamed.  */
    5942     15148036 :   RB (t->base.nowarning_flag);
    5943              :   /* base.visited is not streamed.  */
    5944              :   /* base.used_flag is not streamed.  */
    5945     15148036 :   RB (t->base.nothrow_flag);
    5946     15148036 :   RB (t->base.static_flag);
    5947     15148036 :   RB_IF (TREE_CODE_CLASS (code) != tcc_type, t->base.public_flag);
    5948     15148036 :   RB (t->base.private_flag);
    5949     15148036 :   RB (t->base.protected_flag);
    5950     15148036 :   RB (t->base.deprecated_flag);
    5951     15148036 :   RB (t->base.default_def_flag);
    5952              : 
    5953     15148036 :   switch (code)
    5954              :     {
    5955      2005134 :     case CALL_EXPR:
    5956      2005134 :     case INTEGER_CST:
    5957      2005134 :     case SSA_NAME:
    5958      2005134 :     case TARGET_MEM_REF:
    5959      2005134 :     case TREE_VEC:
    5960              :       /* These use different base.u fields.  */
    5961      2005134 :       goto done;
    5962              : 
    5963     13142902 :     default:
    5964     13142902 :       RB (t->base.u.bits.lang_flag_0);
    5965     13142902 :       RB (t->base.u.bits.lang_flag_1);
    5966     13142902 :       RB (t->base.u.bits.lang_flag_2);
    5967     13142902 :       RB (t->base.u.bits.lang_flag_3);
    5968     13142902 :       RB (t->base.u.bits.lang_flag_4);
    5969     13142902 :       RB (t->base.u.bits.lang_flag_5);
    5970     13142902 :       RB (t->base.u.bits.lang_flag_6);
    5971     13142902 :       RB (t->base.u.bits.saturating_flag);
    5972     13142902 :       RB (t->base.u.bits.unsigned_flag);
    5973     13142902 :       RB (t->base.u.bits.packed_flag);
    5974     13142902 :       RB (t->base.u.bits.user_align);
    5975     13142902 :       RB (t->base.u.bits.nameless_flag);
    5976     13142902 :       RB (t->base.u.bits.atomic_flag);
    5977     13142902 :       RB (t->base.u.bits.unavailable_flag);
    5978     13142902 :       break;
    5979              :     }
    5980              : 
    5981     13142902 :   if (TREE_CODE_CLASS (code) == tcc_type)
    5982              :     {
    5983       573142 :       RB (t->type_common.no_force_blk_flag);
    5984       573142 :       RB (t->type_common.needs_constructing_flag);
    5985       573142 :       RB (t->type_common.transparent_aggr_flag);
    5986       573142 :       RB (t->type_common.restrict_flag);
    5987       573142 :       RB (t->type_common.string_flag);
    5988       573142 :       RB (t->type_common.lang_flag_0);
    5989       573142 :       RB (t->type_common.lang_flag_1);
    5990       573142 :       RB (t->type_common.lang_flag_2);
    5991       573142 :       RB (t->type_common.lang_flag_3);
    5992       573142 :       RB (t->type_common.lang_flag_4);
    5993       573142 :       RB (t->type_common.lang_flag_5);
    5994       573142 :       RB (t->type_common.lang_flag_6);
    5995       573142 :       RB (t->type_common.typeless_storage);
    5996              :     }
    5997              : 
    5998     13142902 :   if (TREE_CODE_CLASS (code) != tcc_declaration)
    5999      9903503 :     goto done;
    6000              : 
    6001      3239399 :   if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
    6002              :     {
    6003      3239399 :       RB (t->decl_common.nonlocal_flag);
    6004      3239399 :       RB (t->decl_common.virtual_flag);
    6005      3239399 :       RB (t->decl_common.ignored_flag);
    6006      3239399 :       RB (t->decl_common.abstract_flag);
    6007      3239399 :       RB (t->decl_common.artificial_flag);
    6008      3239399 :       RB (t->decl_common.preserve_flag);
    6009      3239399 :       RB (t->decl_common.debug_expr_is_from);
    6010      3239399 :       RB (t->decl_common.lang_flag_0);
    6011      3239399 :       RB (t->decl_common.lang_flag_1);
    6012      3239399 :       RB (t->decl_common.lang_flag_2);
    6013      3239399 :       RB (t->decl_common.lang_flag_3);
    6014      3239399 :       RB (t->decl_common.lang_flag_4);
    6015      3239399 :       RB (t->decl_common.lang_flag_5);
    6016      3239399 :       RB (t->decl_common.lang_flag_6);
    6017      3239399 :       RB (t->decl_common.lang_flag_7);
    6018      3239399 :       RB (t->decl_common.lang_flag_8);
    6019      3239399 :       RB (t->decl_common.decl_flag_0);
    6020      3239399 :       RB (t->decl_common.decl_flag_1);
    6021      3239399 :       RB (t->decl_common.decl_flag_2);
    6022      3239399 :       RB (t->decl_common.decl_flag_3);
    6023      3239399 :       RB (t->decl_common.not_gimple_reg_flag);
    6024      3239399 :       RB (t->decl_common.decl_by_reference_flag);
    6025      3239399 :       RB (t->decl_common.decl_read_flag);
    6026      3239399 :       RB (t->decl_common.decl_nonshareable_flag);
    6027      3239399 :       RB (t->decl_common.decl_not_flexarray);
    6028              :     }
    6029              :   else
    6030            0 :     goto done;
    6031              : 
    6032      3239399 :   if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
    6033              :     {
    6034      1489945 :       RB (t->decl_with_vis.defer_output);
    6035      1489945 :       RB (t->decl_with_vis.hard_register);
    6036      1489945 :       RB (t->decl_with_vis.common_flag);
    6037      1489945 :       RB (t->decl_with_vis.in_text_section);
    6038      1489945 :       RB (t->decl_with_vis.in_constant_pool);
    6039      1489945 :       RB (t->decl_with_vis.dllimport_flag);
    6040      1489945 :       RB (t->decl_with_vis.weak_flag);
    6041      1489945 :       RB (t->decl_with_vis.seen_in_bind_expr);
    6042      1489945 :       RB (t->decl_with_vis.comdat_flag);
    6043      1489945 :       RB (t->decl_with_vis.visibility_specified);
    6044      1489945 :       RB (t->decl_with_vis.init_priority_p);
    6045      1489945 :       RB (t->decl_with_vis.shadowed_for_var_p);
    6046      1489945 :       RB (t->decl_with_vis.cxx_constructor);
    6047      1489945 :       RB (t->decl_with_vis.cxx_destructor);
    6048      1489945 :       RB (t->decl_with_vis.final);
    6049      1489945 :       RB (t->decl_with_vis.regdecl_flag);
    6050              :     }
    6051              :   else
    6052      1749454 :     goto done;
    6053              : 
    6054      1489945 :   if (CODE_CONTAINS_STRUCT (code, TS_FUNCTION_DECL))
    6055              :     {
    6056       458495 :       RB (t->function_decl.static_ctor_flag);
    6057       458495 :       RB (t->function_decl.static_dtor_flag);
    6058       458495 :       RB (t->function_decl.uninlinable);
    6059       458495 :       RB (t->function_decl.possibly_inlined);
    6060       458495 :       RB (t->function_decl.novops_flag);
    6061       458495 :       RB (t->function_decl.returns_twice_flag);
    6062       458495 :       RB (t->function_decl.malloc_flag);
    6063       458495 :       RB (t->function_decl.declared_inline_flag);
    6064       458495 :       RB (t->function_decl.no_inline_warning_flag);
    6065       458495 :       RB (t->function_decl.no_instrument_function_entry_exit);
    6066       458495 :       RB (t->function_decl.no_limit_stack);
    6067       458495 :       RB (t->function_decl.disregard_inline_limits);
    6068       458495 :       RB (t->function_decl.pure_flag);
    6069       458495 :       RB (t->function_decl.looping_const_or_pure_flag);
    6070              : 
    6071       458495 :       RB (t->function_decl.has_debug_args_flag);
    6072       458495 :       RB (t->function_decl.versioned_function);
    6073       458495 :       RB (t->function_decl.replaceable_operator);
    6074              : 
    6075              :       /* decl_type is a (misnamed) 2 bit discriminator.  */
    6076       458495 :       unsigned kind = 0;
    6077       458495 :       kind |= unsigned (bits.b ()) << 0;
    6078       458495 :       kind |= unsigned (bits.b ()) << 1;
    6079       458495 :       t->function_decl.decl_type = function_decl_type (kind);
    6080              :     }
    6081              : #undef RB_IF
    6082              : #undef RB
    6083      1031450 : done:
    6084     15148036 :   return !get_overrun ();
    6085              : }
    6086              : 
    6087              : void
    6088      2227895 : trees_out::lang_decl_bools (tree t, bits_out& bits)
    6089              : {
    6090              : #define WB(X) (bits.b (X))
    6091      2227895 :   const struct lang_decl *lang = DECL_LANG_SPECIFIC (t);
    6092              : 
    6093      2227895 :   bits.bflush ();
    6094      2227895 :   WB (lang->u.base.language == lang_cplusplus);
    6095      2227895 :   WB ((lang->u.base.use_template >> 0) & 1);
    6096      2227895 :   WB ((lang->u.base.use_template >> 1) & 1);
    6097              :   /* Do not write lang->u.base.not_really_extern, importer will set
    6098              :      when reading the definition (if any).  */
    6099      2227895 :   WB (lang->u.base.initialized_in_class);
    6100              : 
    6101      2227895 :   WB (lang->u.base.threadprivate_or_deleted_p);
    6102      2227895 :   WB (lang->u.base.anticipated_p);
    6103      2227895 :   WB (lang->u.base.friend_or_tls);
    6104      2227895 :   WB (lang->u.base.unknown_bound_p);
    6105              :   /* Do not write lang->u.base.odr_used, importer will recalculate if
    6106              :      they do ODR use this decl.  */
    6107      2227895 :   WB (lang->u.base.concept_p);
    6108      2227895 :   WB (lang->u.base.var_declared_inline_p);
    6109      2227895 :   WB (lang->u.base.dependent_init_p);
    6110              : 
    6111              :   /* When building a header unit, everthing is marked as purview, (so
    6112              :      we know which decls to write).  But when we import them we do not
    6113              :      want to mark them as in module purview.  */
    6114      4373875 :   WB (lang->u.base.module_purview_p && !header_module_p ());
    6115      2227895 :   WB (lang->u.base.module_attach_p);
    6116              :   /* Importer will set module_import_p and module_entity_p themselves
    6117              :      as appropriate.  */
    6118      2227895 :   WB (lang->u.base.module_keyed_decls_p);
    6119              : 
    6120      2227895 :   WB (lang->u.base.omp_declare_mapper_p);
    6121              : 
    6122      2227895 :   switch (lang->u.base.selector)
    6123              :     {
    6124            0 :     default:
    6125            0 :       gcc_unreachable ();
    6126              : 
    6127       492952 :     case lds_fn:  /* lang_decl_fn.  */
    6128       492952 :       WB (lang->u.fn.global_ctor_p);
    6129       492952 :       WB (lang->u.fn.global_dtor_p);
    6130              : 
    6131       492952 :       WB (lang->u.fn.static_function);
    6132       492952 :       WB (lang->u.fn.pure_virtual);
    6133       492952 :       WB (lang->u.fn.defaulted_p);
    6134       492952 :       WB (lang->u.fn.has_in_charge_parm_p);
    6135       492952 :       WB (lang->u.fn.has_vtt_parm_p);
    6136              :       /* There shouldn't be a pending inline at this point.  */
    6137       492952 :       gcc_assert (!lang->u.fn.pending_inline_p);
    6138       492952 :       WB (lang->u.fn.nonconverting);
    6139       492952 :       WB (lang->u.fn.thunk_p);
    6140              : 
    6141       492952 :       WB (lang->u.fn.this_thunk_p);
    6142       492952 :       WB (lang->u.fn.omp_declare_reduction_p);
    6143       492952 :       WB (lang->u.fn.has_dependent_explicit_spec_p);
    6144       492952 :       WB (lang->u.fn.immediate_fn_p);
    6145       492952 :       WB (lang->u.fn.maybe_deleted);
    6146       492952 :       WB (lang->u.fn.coroutine_p);
    6147       492952 :       WB (lang->u.fn.implicit_constexpr);
    6148       492952 :       WB (lang->u.fn.escalated_p);
    6149       492952 :       WB (lang->u.fn.xobj_func);
    6150       492952 :       goto lds_min;
    6151              : 
    6152         1909 :     case lds_decomp:  /* lang_decl_decomp.  */
    6153              :       /* No bools.  */
    6154         1909 :       goto lds_min;
    6155              : 
    6156              :     case lds_min:  /* lang_decl_min.  */
    6157      2227895 :     lds_min:
    6158              :       /* No bools.  */
    6159              :       break;
    6160              : 
    6161              :     case lds_ns:  /* lang_decl_ns.  */
    6162              :       /* No bools.  */
    6163              :       break;
    6164              : 
    6165              :     case lds_parm:  /* lang_decl_parm.  */
    6166              :       /* No bools.  */
    6167              :       break;
    6168              :     }
    6169              : #undef WB
    6170      2227895 : }
    6171              : 
    6172              : bool
    6173      2033175 : trees_in::lang_decl_bools (tree t, bits_in& bits)
    6174              : {
    6175              : #define RB(X) ((X) = bits.b ())
    6176      2033175 :   struct lang_decl *lang = DECL_LANG_SPECIFIC (t);
    6177              : 
    6178      2033175 :   bits.bflush ();
    6179      2033175 :   lang->u.base.language = bits.b () ? lang_cplusplus : lang_c;
    6180      2033175 :   unsigned v;
    6181      2033175 :   v = bits.b () << 0;
    6182      2033175 :   v |= bits.b () << 1;
    6183      2033175 :   lang->u.base.use_template = v;
    6184              :   /* lang->u.base.not_really_extern is not streamed.  */
    6185      2033175 :   RB (lang->u.base.initialized_in_class);
    6186              : 
    6187      2033175 :   RB (lang->u.base.threadprivate_or_deleted_p);
    6188      2033175 :   RB (lang->u.base.anticipated_p);
    6189      2033175 :   RB (lang->u.base.friend_or_tls);
    6190      2033175 :   RB (lang->u.base.unknown_bound_p);
    6191              :   /* lang->u.base.odr_used is not streamed.  */
    6192      2033175 :   RB (lang->u.base.concept_p);
    6193      2033175 :   RB (lang->u.base.var_declared_inline_p);
    6194      2033175 :   RB (lang->u.base.dependent_init_p);
    6195              : 
    6196      2033175 :   RB (lang->u.base.module_purview_p);
    6197      2033175 :   RB (lang->u.base.module_attach_p);
    6198              :   /* module_import_p and module_entity_p are not streamed.  */
    6199      2033175 :   RB (lang->u.base.module_keyed_decls_p);
    6200              : 
    6201      2033175 :   RB (lang->u.base.omp_declare_mapper_p);
    6202              : 
    6203      2033175 :   switch (lang->u.base.selector)
    6204              :     {
    6205            0 :     default:
    6206            0 :       gcc_unreachable ();
    6207              : 
    6208       458495 :     case lds_fn:  /* lang_decl_fn.  */
    6209       458495 :       RB (lang->u.fn.global_ctor_p);
    6210       458495 :       RB (lang->u.fn.global_dtor_p);
    6211              : 
    6212       458495 :       RB (lang->u.fn.static_function);
    6213       458495 :       RB (lang->u.fn.pure_virtual);
    6214       458495 :       RB (lang->u.fn.defaulted_p);
    6215       458495 :       RB (lang->u.fn.has_in_charge_parm_p);
    6216       458495 :       RB (lang->u.fn.has_vtt_parm_p);
    6217              :       /* lang->u.f.n.pending_inline_p is not streamed.  */
    6218       458495 :       RB (lang->u.fn.nonconverting);
    6219       458495 :       RB (lang->u.fn.thunk_p);
    6220              : 
    6221       458495 :       RB (lang->u.fn.this_thunk_p);
    6222       458495 :       RB (lang->u.fn.omp_declare_reduction_p);
    6223       458495 :       RB (lang->u.fn.has_dependent_explicit_spec_p);
    6224       458495 :       RB (lang->u.fn.immediate_fn_p);
    6225       458495 :       RB (lang->u.fn.maybe_deleted);
    6226       458495 :       RB (lang->u.fn.coroutine_p);
    6227       458495 :       RB (lang->u.fn.implicit_constexpr);
    6228       458495 :       RB (lang->u.fn.escalated_p);
    6229       458495 :       RB (lang->u.fn.xobj_func);
    6230       458495 :       goto lds_min;
    6231              : 
    6232         2032 :     case lds_decomp:  /* lang_decl_decomp.  */
    6233              :       /* No bools.  */
    6234         2032 :       goto lds_min;
    6235              : 
    6236              :     case lds_min:  /* lang_decl_min.  */
    6237      2033175 :     lds_min:
    6238              :       /* No bools.  */
    6239              :       break;
    6240              : 
    6241              :     case lds_ns:  /* lang_decl_ns.  */
    6242              :       /* No bools.  */
    6243              :       break;
    6244              : 
    6245              :     case lds_parm:  /* lang_decl_parm.  */
    6246              :       /* No bools.  */
    6247              :       break;
    6248              :     }
    6249              : #undef RB
    6250      2033175 :   return !get_overrun ();
    6251              : }
    6252              : 
    6253              : void
    6254       175739 : trees_out::lang_type_bools (tree t, bits_out& bits)
    6255              : {
    6256              : #define WB(X) (bits.b (X))
    6257       175739 :   const struct lang_type *lang = TYPE_LANG_SPECIFIC (t);
    6258              : 
    6259       175739 :   bits.bflush ();
    6260       175739 :   WB (lang->has_type_conversion);
    6261       175739 :   WB (lang->has_copy_ctor);
    6262       175739 :   WB (lang->has_default_ctor);
    6263       175739 :   WB (lang->const_needs_init);
    6264       175739 :   WB (lang->ref_needs_init);
    6265       175739 :   WB (lang->has_const_copy_assign);
    6266       175739 :   WB ((lang->use_template >> 0) & 1);
    6267       175739 :   WB ((lang->use_template >> 1) & 1);
    6268              : 
    6269       175739 :   WB (lang->has_mutable);
    6270       175739 :   WB (lang->com_interface);
    6271       175739 :   WB (lang->non_pod_class);
    6272       175739 :   WB (lang->nearly_empty_p);
    6273       175739 :   WB (lang->user_align);
    6274       175739 :   WB (lang->has_copy_assign);
    6275       175739 :   WB (lang->has_new);
    6276       175739 :   WB (lang->has_array_new);
    6277              : 
    6278       175739 :   WB ((lang->gets_delete >> 0) & 1);
    6279       175739 :   WB ((lang->gets_delete >> 1) & 1);
    6280       175739 :   WB (lang->interface_only);
    6281       175739 :   WB (lang->interface_unknown);
    6282       175739 :   WB (lang->contains_empty_class_p);
    6283       175739 :   WB (lang->anon_aggr);
    6284       175739 :   WB (lang->non_zero_init);
    6285       175739 :   WB (lang->empty_p);
    6286              : 
    6287       175739 :   WB (lang->vec_new_uses_cookie);
    6288       175739 :   WB (lang->declared_class);
    6289       175739 :   WB (lang->diamond_shaped);
    6290       175739 :   WB (lang->repeated_base);
    6291       175739 :   gcc_checking_assert (!lang->being_defined);
    6292              :   // lang->debug_requested
    6293       175739 :   WB (lang->fields_readonly);
    6294       175739 :   WB (lang->ptrmemfunc_flag);
    6295              : 
    6296       175739 :   WB (lang->lazy_default_ctor);
    6297       175739 :   WB (lang->lazy_copy_ctor);
    6298       175739 :   WB (lang->lazy_copy_assign);
    6299       175739 :   WB (lang->lazy_destructor);
    6300       175739 :   WB (lang->has_const_copy_ctor);
    6301       175739 :   WB (lang->has_complex_copy_ctor);
    6302       175739 :   WB (lang->has_complex_copy_assign);
    6303       175739 :   WB (lang->non_aggregate);
    6304              : 
    6305       175739 :   WB (lang->has_complex_dflt);
    6306       175739 :   WB (lang->has_list_ctor);
    6307       175739 :   WB (lang->non_std_layout);
    6308       175739 :   WB (lang->is_literal);
    6309       175739 :   WB (lang->lazy_move_ctor);
    6310       175739 :   WB (lang->lazy_move_assign);
    6311       175739 :   WB (lang->has_complex_move_ctor);
    6312       175739 :   WB (lang->has_complex_move_assign);
    6313              : 
    6314       175739 :   WB (lang->has_constexpr_ctor);
    6315       175739 :   WB (lang->unique_obj_representations);
    6316       175739 :   WB (lang->unique_obj_representations_set);
    6317       175739 :   gcc_checking_assert (!lang->erroneous);
    6318       175739 :   WB (lang->non_pod_aggregate);
    6319       175739 :   WB (lang->non_aggregate_pod);
    6320              : #undef WB
    6321       175739 : }
    6322              : 
    6323              : bool
    6324       151182 : trees_in::lang_type_bools (tree t, bits_in& bits)
    6325              : {
    6326              : #define RB(X) ((X) = bits.b ())
    6327       151182 :   struct lang_type *lang = TYPE_LANG_SPECIFIC (t);
    6328              : 
    6329       151182 :   bits.bflush ();
    6330       151182 :   RB (lang->has_type_conversion);
    6331       151182 :   RB (lang->has_copy_ctor);
    6332       151182 :   RB (lang->has_default_ctor);
    6333       151182 :   RB (lang->const_needs_init);
    6334       151182 :   RB (lang->ref_needs_init);
    6335       151182 :   RB (lang->has_const_copy_assign);
    6336       151182 :   unsigned v;
    6337       151182 :   v = bits.b () << 0;
    6338       151182 :   v |= bits.b () << 1;
    6339       151182 :   lang->use_template = v;
    6340              : 
    6341       151182 :   RB (lang->has_mutable);
    6342       151182 :   RB (lang->com_interface);
    6343       151182 :   RB (lang->non_pod_class);
    6344       151182 :   RB (lang->nearly_empty_p);
    6345       151182 :   RB (lang->user_align);
    6346       151182 :   RB (lang->has_copy_assign);
    6347       151182 :   RB (lang->has_new);
    6348       151182 :   RB (lang->has_array_new);
    6349              : 
    6350       151182 :   v = bits.b () << 0;
    6351       151182 :   v |= bits.b () << 1;
    6352       151182 :   lang->gets_delete = v;
    6353       151182 :   RB (lang->interface_only);
    6354       151182 :   RB (lang->interface_unknown);
    6355       151182 :   RB (lang->contains_empty_class_p);
    6356       151182 :   RB (lang->anon_aggr);
    6357       151182 :   RB (lang->non_zero_init);
    6358       151182 :   RB (lang->empty_p);
    6359              : 
    6360       151182 :   RB (lang->vec_new_uses_cookie);
    6361       151182 :   RB (lang->declared_class);
    6362       151182 :   RB (lang->diamond_shaped);
    6363       151182 :   RB (lang->repeated_base);
    6364       151182 :   gcc_checking_assert (!lang->being_defined);
    6365       151182 :   gcc_checking_assert (!lang->debug_requested);
    6366       151182 :   RB (lang->fields_readonly);
    6367       151182 :   RB (lang->ptrmemfunc_flag);
    6368              : 
    6369       151182 :   RB (lang->lazy_default_ctor);
    6370       151182 :   RB (lang->lazy_copy_ctor);
    6371       151182 :   RB (lang->lazy_copy_assign);
    6372       151182 :   RB (lang->lazy_destructor);
    6373       151182 :   RB (lang->has_const_copy_ctor);
    6374       151182 :   RB (lang->has_complex_copy_ctor);
    6375       151182 :   RB (lang->has_complex_copy_assign);
    6376       151182 :   RB (lang->non_aggregate);
    6377              : 
    6378       151182 :   RB (lang->has_complex_dflt);
    6379       151182 :   RB (lang->has_list_ctor);
    6380       151182 :   RB (lang->non_std_layout);
    6381       151182 :   RB (lang->is_literal);
    6382       151182 :   RB (lang->lazy_move_ctor);
    6383       151182 :   RB (lang->lazy_move_assign);
    6384       151182 :   RB (lang->has_complex_move_ctor);
    6385       151182 :   RB (lang->has_complex_move_assign);
    6386              : 
    6387       151182 :   RB (lang->has_constexpr_ctor);
    6388       151182 :   RB (lang->unique_obj_representations);
    6389       151182 :   RB (lang->unique_obj_representations_set);
    6390       151182 :   gcc_checking_assert (!lang->erroneous);
    6391       151182 :   RB (lang->non_pod_aggregate);
    6392       151182 :   RB (lang->non_aggregate_pod);
    6393              : #undef RB
    6394       151182 :   return !get_overrun ();
    6395              : }
    6396              : 
    6397              : /* Read & write the core values and pointers.  */
    6398              : 
    6399              : void
    6400     42182538 : trees_out::core_vals (tree t)
    6401              : {
    6402              : #define WU(X) (u (X))
    6403              : #define WT(X) (tree_node (X))
    6404     42182538 :   tree_code code = TREE_CODE (t);
    6405              : 
    6406              :   /* First by shape of the tree.  */
    6407              : 
    6408     42182538 :   if (CODE_CONTAINS_STRUCT (code, TS_DECL_MINIMAL))
    6409              :     {
    6410              :       /* Write this early, for better log information.  */
    6411      9329369 :       WT (t->decl_minimal.name);
    6412      9329369 :       if (!DECL_TEMPLATE_PARM_P (t))
    6413      7210066 :         WT (t->decl_minimal.context);
    6414              : 
    6415      9329369 :       if (state)
    6416      7675883 :         state->write_location (*this, t->decl_minimal.locus);
    6417              : 
    6418      9329369 :       if (streaming_p ())
    6419      3563662 :         if (has_warning_spec (t))
    6420          746 :           u (get_warning_spec (t));
    6421              :     }
    6422              : 
    6423     42182538 :   if (CODE_CONTAINS_STRUCT (code, TS_TYPE_COMMON))
    6424              :     {
    6425              :       /* The only types we write also have TYPE_NON_COMMON.  */
    6426      2315408 :       gcc_checking_assert (CODE_CONTAINS_STRUCT (code, TS_TYPE_NON_COMMON));
    6427              : 
    6428              :       /* We only stream the main variant.  */
    6429      2315408 :       gcc_checking_assert (TYPE_MAIN_VARIANT (t) == t);
    6430              : 
    6431              :       /* Stream the name & context first, for better log information  */
    6432      2315408 :       WT (t->type_common.name);
    6433      2315408 :       WT (t->type_common.context);
    6434              : 
    6435              :       /* By construction we want to make sure we have the canonical
    6436              :          and main variants already in the type table, so emit them
    6437              :          now.  */
    6438      2315408 :       WT (t->type_common.main_variant);
    6439              : 
    6440      2315408 :       tree canonical = t->type_common.canonical;
    6441      2315408 :       if (canonical && DECL_TEMPLATE_PARM_P (TYPE_NAME (t)))
    6442              :         /* We do not want to wander into different templates.
    6443              :            Reconstructed on stream in.  */
    6444              :         canonical = t;
    6445      2315408 :       WT (canonical);
    6446              : 
    6447              :       /* type_common.next_variant is internally manipulated.  */
    6448              :       /* type_common.pointer_to, type_common.reference_to.  */
    6449              : 
    6450      2315408 :       if (streaming_p ())
    6451              :         {
    6452       636523 :           WU (t->type_common.precision);
    6453       636523 :           WU (t->type_common.contains_placeholder_bits);
    6454       636523 :           WU (t->type_common.mode);
    6455       636523 :           WU (t->type_common.align);
    6456              :         }
    6457              : 
    6458      2315408 :       if (!RECORD_OR_UNION_CODE_P (code))
    6459              :         {
    6460      1912832 :           WT (t->type_common.size);
    6461      1912832 :           WT (t->type_common.size_unit);
    6462              :         }
    6463      2315408 :       WT (t->type_common.attributes);
    6464              : 
    6465      2315408 :       WT (t->type_common.common.chain); /* TYPE_STUB_DECL.  */
    6466              :     }
    6467              : 
    6468     42182538 :   if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
    6469              :     {
    6470      9329369 :       if (streaming_p ())
    6471              :         {
    6472      3563662 :           WU (t->decl_common.mode);
    6473      3563662 :           WU (t->decl_common.off_align);
    6474      3563662 :           WU (t->decl_common.align);
    6475              :         }
    6476              : 
    6477              :       /* For templates these hold instantiation (partial and/or
    6478              :          specialization) information.  */
    6479      9329369 :       if (code != TEMPLATE_DECL)
    6480              :         {
    6481      8615191 :           WT (t->decl_common.size);
    6482      8615191 :           WT (t->decl_common.size_unit);
    6483              :         }
    6484              : 
    6485      9329369 :       WT (t->decl_common.attributes);
    6486              :       // FIXME: Does this introduce cross-decl links?  For instance
    6487              :       // from instantiation to the template.  If so, we'll need more
    6488              :       // deduplication logic.  I think we'll need to walk the blocks
    6489              :       // of the owning function_decl's abstract origin in tandem, to
    6490              :       // generate the locating data needed?
    6491      9329369 :       WT (t->decl_common.abstract_origin);
    6492              :     }
    6493              : 
    6494     42182538 :   if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
    6495              :     {
    6496      4418651 :       WT (t->decl_with_vis.assembler_name);
    6497      4418651 :       if (streaming_p ())
    6498      1680840 :         WU (t->decl_with_vis.visibility);
    6499              :     }
    6500              : 
    6501     42182538 :   if (CODE_CONTAINS_STRUCT (code, TS_TYPE_NON_COMMON))
    6502              :     {
    6503      2315408 :       if (code == ENUMERAL_TYPE)
    6504              :         {
    6505              :           /* These fields get set even for opaque enums that lack a
    6506              :              definition, so we stream them directly for each ENUMERAL_TYPE.
    6507              :              We stream TYPE_VALUES as part of the definition.  */
    6508         8518 :           WT (t->type_non_common.maxval);
    6509         8518 :           WT (t->type_non_common.minval);
    6510              :         }
    6511              :       /* Records and unions hold FIELDS, VFIELD & BINFO on these
    6512              :          things.  */
    6513      2306890 :       else if (!RECORD_OR_UNION_CODE_P (code))
    6514              :         {
    6515              :           // FIXME: These are from tpl_parm_value's 'type' writing.
    6516              :           // Perhaps it should just be doing them directly?
    6517      1904314 :           gcc_checking_assert (code == TEMPLATE_TYPE_PARM
    6518              :                                || code == TEMPLATE_TEMPLATE_PARM
    6519              :                                || code == BOUND_TEMPLATE_TEMPLATE_PARM);
    6520      1904314 :           gcc_checking_assert (!TYPE_CACHED_VALUES_P (t));
    6521      1904314 :           WT (t->type_non_common.values);
    6522      1904314 :           WT (t->type_non_common.maxval);
    6523      1904314 :           WT (t->type_non_common.minval);
    6524              :         }
    6525              : 
    6526      2315408 :       WT (t->type_non_common.lang_1);
    6527              :     }
    6528              : 
    6529     42182538 :   if (CODE_CONTAINS_STRUCT (code, TS_EXP))
    6530              :     {
    6531     12691633 :       if (state)
    6532     12431657 :         state->write_location (*this, t->exp.locus);
    6533              : 
    6534     12691633 :       if (streaming_p ())
    6535      6159583 :         if (has_warning_spec (t))
    6536       490766 :           u (get_warning_spec (t));
    6537              : 
    6538     12691633 :       bool vl = TREE_CODE_CLASS (code) == tcc_vl_exp;
    6539     12691633 :       unsigned limit = (vl ? VL_EXP_OPERAND_LENGTH (t)
    6540     12691633 :                         : TREE_OPERAND_LENGTH (t));
    6541     12691633 :       unsigned ix = unsigned (vl);
    6542     12691633 :       if (code == REQUIRES_EXPR)
    6543              :         {
    6544              :           /* The first operand of a REQUIRES_EXPR is a tree chain
    6545              :              of PARM_DECLs.  We need to stream this separately as
    6546              :              otherwise we would only stream the first one.  */
    6547        20075 :           chained_decls (REQUIRES_EXPR_PARMS (t));
    6548        20075 :           ++ix;
    6549              :         }
    6550     34840104 :       for (; ix != limit; ix++)
    6551     22148471 :         WT (TREE_OPERAND (t, ix));
    6552              :     }
    6553              :   else
    6554              :     /* The CODE_CONTAINS tables were inaccurate when I started.  */
    6555     29490905 :     gcc_checking_assert (TREE_CODE_CLASS (code) != tcc_expression
    6556              :                          && TREE_CODE_CLASS (code) != tcc_binary
    6557              :                          && TREE_CODE_CLASS (code) != tcc_unary
    6558              :                          && TREE_CODE_CLASS (code) != tcc_reference
    6559              :                          && TREE_CODE_CLASS (code) != tcc_comparison
    6560              :                          && TREE_CODE_CLASS (code) != tcc_statement
    6561              :                          && TREE_CODE_CLASS (code) != tcc_vl_exp);
    6562              : 
    6563              :   /* Then by CODE.  Special cases and/or 1:1 tree shape
    6564              :      correspondance. */
    6565     42182538 :   switch (code)
    6566              :     {
    6567              :     default:
    6568              :       break;
    6569              : 
    6570            0 :     case ARGUMENT_PACK_SELECT:  /* Transient during instantiation.  */
    6571            0 :     case DEFERRED_PARSE:        /* Expanded upon completion of
    6572              :                                    outermost class.  */
    6573            0 :     case IDENTIFIER_NODE:       /* Streamed specially.  */
    6574            0 :     case BINDING_VECTOR:                /* Only in namespace-scope symbol
    6575              :                                    table.  */
    6576            0 :     case SSA_NAME:
    6577            0 :     case TRANSLATION_UNIT_DECL: /* There is only one, it is a
    6578              :                                    global_tree.  */
    6579            0 :     case USERDEF_LITERAL:       /* Expanded during parsing.  */
    6580            0 :       gcc_unreachable (); /* Should never meet.  */
    6581              : 
    6582              :       /* Constants.  */
    6583           18 :     case COMPLEX_CST:
    6584           18 :       WT (TREE_REALPART (t));
    6585           18 :       WT (TREE_IMAGPART (t));
    6586           18 :       break;
    6587              : 
    6588            0 :     case FIXED_CST:
    6589            0 :       gcc_unreachable (); /* Not supported in C++.  */
    6590              : 
    6591      3914240 :     case INTEGER_CST:
    6592      3914240 :       if (streaming_p ())
    6593              :         {
    6594       689208 :           unsigned num = TREE_INT_CST_EXT_NUNITS (t);
    6595      1380486 :           for (unsigned ix = 0; ix != num; ix++)
    6596       691278 :             wu (TREE_INT_CST_ELT (t, ix));
    6597              :         }
    6598              :       break;
    6599              : 
    6600            0 :     case POLY_INT_CST:
    6601            0 :       if (streaming_p ())
    6602            0 :         for (unsigned ix = 0; ix != NUM_POLY_INT_COEFFS; ix++)
    6603            0 :           WT (POLY_INT_CST_COEFF (t, ix));
    6604              :       break;
    6605              : 
    6606        37737 :     case REAL_CST:
    6607        37737 :       if (streaming_p ())
    6608        18833 :         buf (TREE_REAL_CST_PTR (t), sizeof (real_value));
    6609              :       break;
    6610              : 
    6611              :     case STRING_CST:
    6612              :       /* Streamed during start.  */
    6613              :       break;
    6614              : 
    6615           36 :     case RAW_DATA_CST:
    6616           36 :       if (RAW_DATA_OWNER (t) == NULL_TREE)
    6617              :         break; /* Streamed as STRING_CST during start.  */
    6618           24 :       WT (RAW_DATA_OWNER (t));
    6619           24 :       if (streaming_p ())
    6620              :         {
    6621           12 :           if (TREE_CODE (RAW_DATA_OWNER (t)) == RAW_DATA_CST)
    6622            6 :             z (RAW_DATA_POINTER (t) - RAW_DATA_POINTER (RAW_DATA_OWNER (t)));
    6623            6 :           else if (TREE_CODE (RAW_DATA_OWNER (t)) == STRING_CST)
    6624            6 :             z (RAW_DATA_POINTER (t)
    6625            6 :                - TREE_STRING_POINTER (RAW_DATA_OWNER (t)));
    6626              :           else
    6627            0 :             gcc_unreachable ();
    6628              :         }
    6629              :       break;
    6630              : 
    6631           36 :     case VECTOR_CST:
    6632          102 :       for (unsigned ix = vector_cst_encoded_nelts (t); ix--;)
    6633           66 :         WT (VECTOR_CST_ENCODED_ELT (t, ix));
    6634              :       break;
    6635              : 
    6636              :       /* Decls.  */
    6637       498384 :     case VAR_DECL:
    6638       498384 :       if (DECL_CONTEXT (t)
    6639       498384 :           && TREE_CODE (DECL_CONTEXT (t)) != FUNCTION_DECL)
    6640              :         {
    6641       116991 :           if (DECL_HAS_VALUE_EXPR_P (t))
    6642           18 :             WT (DECL_VALUE_EXPR (t));
    6643              :           break;
    6644              :         }
    6645              :       /* FALLTHROUGH  */
    6646              : 
    6647      4215897 :     case RESULT_DECL:
    6648      4215897 :     case PARM_DECL:
    6649      4215897 :       if (DECL_HAS_VALUE_EXPR_P (t))
    6650        35786 :         WT (DECL_VALUE_EXPR (t));
    6651              :       /* FALLTHROUGH  */
    6652              : 
    6653      4397133 :     case CONST_DECL:
    6654      4397133 :     case IMPORTED_DECL:
    6655      4397133 :       WT (t->decl_common.initial);
    6656      4397133 :       break;
    6657              : 
    6658       128231 :     case FIELD_DECL:
    6659       128231 :       WT (t->field_decl.offset);
    6660       128231 :       WT (t->field_decl.bit_field_type);
    6661       128231 :       {
    6662       128231 :         auto ovr = make_temp_override (walking_bit_field_unit, true);
    6663       128231 :         WT (t->field_decl.qualifier); /* bitfield unit.  */
    6664       128231 :       }
    6665       128231 :       WT (t->field_decl.bit_offset);
    6666       128231 :       WT (t->field_decl.fcontext);
    6667       128231 :       WT (t->decl_common.initial);
    6668       128231 :       break;
    6669              : 
    6670        39095 :     case LABEL_DECL:
    6671        39095 :       if (streaming_p ())
    6672              :         {
    6673        19546 :           WU (t->label_decl.label_decl_uid);
    6674        19546 :           WU (t->label_decl.eh_landing_pad_nr);
    6675              :         }
    6676              :       break;
    6677              : 
    6678       986118 :     case FUNCTION_DECL:
    6679       986118 :       if (streaming_p ())
    6680              :         {
    6681              :           /* Builtins can be streamed by value when a header declares
    6682              :              them.  */
    6683       492952 :           WU (DECL_BUILT_IN_CLASS (t));
    6684       492952 :           if (DECL_BUILT_IN_CLASS (t) != NOT_BUILT_IN)
    6685         9713 :             WU (DECL_UNCHECKED_FUNCTION_CODE (t));
    6686              :         }
    6687              : 
    6688       986118 :       WT (t->function_decl.personality);
    6689              :       /* Rather than streaming target/optimize nodes, we should reconstruct
    6690              :          them on stream-in from any attributes applied to the function.  */
    6691       986118 :       if (streaming_p () && t->function_decl.function_specific_target)
    6692            0 :         warning_at (DECL_SOURCE_LOCATION (t), 0,
    6693              :                     "%<target%> attribute currently unsupported in modules");
    6694       986118 :       if (streaming_p () && t->function_decl.function_specific_optimization)
    6695            3 :         warning_at (DECL_SOURCE_LOCATION (t), 0,
    6696              :                     "%<optimize%> attribute currently unsupported in modules");
    6697       986118 :       WT (t->function_decl.vindex);
    6698              : 
    6699       986118 :       if (DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (t))
    6700         6616 :         WT (lookup_explicit_specifier (t));
    6701              :       break;
    6702              : 
    6703       117545 :     case USING_DECL:
    6704              :       /* USING_DECL_DECLS  */
    6705       117545 :       WT (t->decl_common.initial);
    6706              :       /* FALLTHROUGH  */
    6707              : 
    6708      2933794 :     case TYPE_DECL:
    6709              :       /* USING_DECL: USING_DECL_SCOPE  */
    6710              :       /* TYPE_DECL: DECL_ORIGINAL_TYPE */
    6711      2933794 :       WT (t->decl_non_common.result);
    6712      2933794 :       break;
    6713              : 
    6714              :       /* Miscellaneous common nodes.  */
    6715       634656 :     case BLOCK:
    6716       634656 :       if (state)
    6717              :         {
    6718       634656 :           state->write_location (*this, t->block.locus);
    6719       634656 :           state->write_location (*this, t->block.end_locus);
    6720              :         }
    6721              : 
    6722              :       /* DECL_LOCAL_DECL_P decls are first encountered here and
    6723              :          streamed by value.  */
    6724       956900 :       for (tree decls = t->block.vars; decls; decls = DECL_CHAIN (decls))
    6725              :         {
    6726       322244 :           if (VAR_OR_FUNCTION_DECL_P (decls)
    6727       322244 :               && DECL_LOCAL_DECL_P (decls))
    6728              :             {
    6729              :               /* Make sure this is the first encounter, and mark for
    6730              :                  walk-by-value.  */
    6731          278 :               gcc_checking_assert (!TREE_VISITED (decls)
    6732              :                                    && !DECL_TEMPLATE_INFO (decls));
    6733          278 :               mark_by_value (decls);
    6734              :             }
    6735       322244 :           tree_node (decls);
    6736              :         }
    6737       634656 :       tree_node (NULL_TREE);
    6738              : 
    6739              :       /* nonlocalized_vars is a middle-end thing.  */
    6740       634656 :       WT (t->block.subblocks);
    6741       634656 :       WT (t->block.supercontext);
    6742              :       // FIXME: As for decl's abstract_origin, does this introduce crosslinks?
    6743       634656 :       WT (t->block.abstract_origin);
    6744              :       /* fragment_origin, fragment_chain are middle-end things.  */
    6745       634656 :       WT (t->block.chain);
    6746              :       /* nonlocalized_vars, block_num & die are middle endy/debug
    6747              :          things.  */
    6748       634656 :       break;
    6749              : 
    6750      1259872 :     case CALL_EXPR:
    6751      1259872 :       if (streaming_p ())
    6752       602699 :         WU (t->base.u.ifn);
    6753              :       break;
    6754              : 
    6755              :     case CONSTRUCTOR:
    6756              :       // This must be streamed /after/ we've streamed the type,
    6757              :       // because it can directly refer to elements of the type. Eg,
    6758              :       // FIELD_DECLs of a RECORD_TYPE.
    6759              :       break;
    6760              : 
    6761           36 :     case OMP_CLAUSE:
    6762           36 :       {
    6763              :         /* The ompcode is serialized in start.  */
    6764           36 :         if (streaming_p ())
    6765           18 :           WU (t->omp_clause.subcode.map_kind);
    6766           36 :         if (state)
    6767           36 :           state->write_location (*this, t->omp_clause.locus);
    6768              : 
    6769           36 :         unsigned len = omp_clause_num_ops[OMP_CLAUSE_CODE (t)];
    6770          120 :         for (unsigned ix = 0; ix != len; ix++)
    6771           84 :           WT (t->omp_clause.ops[ix]);
    6772              :       }
    6773              :       break;
    6774              : 
    6775       548978 :     case STATEMENT_LIST:
    6776      2266599 :       for (tree stmt : tsi_range (t))
    6777      1717621 :         if (stmt)
    6778      1717621 :           WT (stmt);
    6779       548978 :       WT (NULL_TREE);
    6780       548978 :       break;
    6781              : 
    6782            0 :     case OPTIMIZATION_NODE:
    6783            0 :     case TARGET_OPTION_NODE:
    6784              :       // FIXME: Our representation for these two nodes is a cache of
    6785              :       // the resulting set of options.  Not a record of the options
    6786              :       // that got changed by a particular attribute or pragma.  Instead
    6787              :       // of recording that, we probably should just rebuild the options
    6788              :       // on stream-in from the function attributes.  This could introduce
    6789              :       // strangeness if the importer has some incompatible set of flags
    6790              :       // but we currently assume users "know what they're doing" in such
    6791              :       // a case anyway.
    6792            0 :       gcc_unreachable ();
    6793       230648 :       break;
    6794              : 
    6795       230648 :     case TREE_BINFO:
    6796       230648 :       {
    6797       230648 :         WT (t->binfo.common.chain);
    6798       230648 :         WT (t->binfo.offset);
    6799       230648 :         WT (t->binfo.inheritance);
    6800       230648 :         WT (t->binfo.vptr_field);
    6801              : 
    6802       230648 :         WT (t->binfo.vtable);
    6803       230648 :         WT (t->binfo.virtuals);
    6804       230648 :         WT (t->binfo.vtt_subvtt);
    6805       230648 :         WT (t->binfo.vtt_vptr);
    6806              : 
    6807       230648 :         tree_vec (BINFO_BASE_ACCESSES (t));
    6808       230648 :         unsigned num = vec_safe_length (BINFO_BASE_ACCESSES (t));
    6809       295056 :         for (unsigned ix = 0; ix != num; ix++)
    6810        64408 :           WT (BINFO_BASE_BINFO (t, ix));
    6811              :       }
    6812              :       break;
    6813              : 
    6814      3247711 :     case TREE_LIST:
    6815      3247711 :       WT (t->list.purpose);
    6816      3247711 :       WT (t->list.value);
    6817      3247711 :       WT (t->list.common.chain);
    6818      3247711 :       break;
    6819              : 
    6820      2759408 :     case TREE_VEC:
    6821      7617679 :       for (unsigned ix = TREE_VEC_LENGTH (t); ix--;)
    6822      4858271 :         WT (TREE_VEC_ELT (t, ix));
    6823              :       /* We stash NON_DEFAULT_TEMPLATE_ARGS_COUNT on TREE_CHAIN!  */
    6824      2759408 :       gcc_checking_assert (!t->type_common.common.chain
    6825              :                            || (TREE_CODE (t->type_common.common.chain)
    6826              :                                == INTEGER_CST));
    6827      2759408 :       WT (t->type_common.common.chain);
    6828      2759408 :       break;
    6829              : 
    6830              :       /* C++-specific nodes ...  */
    6831       226661 :     case BASELINK:
    6832       226661 :       WT (((lang_tree_node *)t)->baselink.binfo);
    6833       226661 :       WT (((lang_tree_node *)t)->baselink.functions);
    6834       226661 :       WT (((lang_tree_node *)t)->baselink.access_binfo);
    6835       226661 :       WT (((lang_tree_node *)t)->baselink.common.chain);
    6836       226661 :       break;
    6837              : 
    6838        99948 :     case CONSTRAINT_INFO:
    6839        99948 :       WT (((lang_tree_node *)t)->constraint_info.template_reqs);
    6840        99948 :       WT (((lang_tree_node *)t)->constraint_info.declarator_reqs);
    6841        99948 :       WT (((lang_tree_node *)t)->constraint_info.associated_constr);
    6842        99948 :       break;
    6843              : 
    6844        15326 :     case DEFERRED_NOEXCEPT:
    6845        15326 :       WT (((lang_tree_node *)t)->deferred_noexcept.pattern);
    6846        15326 :       WT (((lang_tree_node *)t)->deferred_noexcept.args);
    6847        15326 :       break;
    6848              : 
    6849        14158 :     case LAMBDA_EXPR:
    6850        14158 :       WT (((lang_tree_node *)t)->lambda_expression.capture_list);
    6851        14158 :       WT (((lang_tree_node *)t)->lambda_expression.this_capture);
    6852        14158 :       WT (((lang_tree_node *)t)->lambda_expression.extra_scope);
    6853        14158 :       WT (((lang_tree_node *)t)->lambda_expression.regen_info);
    6854        14158 :       WT (((lang_tree_node *)t)->lambda_expression.extra_args);
    6855              :       /* pending_proxies is a parse-time thing.  */
    6856        14158 :       gcc_assert (!((lang_tree_node *)t)->lambda_expression.pending_proxies);
    6857        14158 :       if (state)
    6858        14155 :         state->write_location
    6859        14155 :           (*this, ((lang_tree_node *)t)->lambda_expression.locus);
    6860        14158 :       if (streaming_p ())
    6861              :         {
    6862         4783 :           WU (((lang_tree_node *)t)->lambda_expression.default_capture_mode);
    6863         4783 :           WU (((lang_tree_node *)t)->lambda_expression.discriminator_scope);
    6864         4783 :           WU (((lang_tree_node *)t)->lambda_expression.discriminator_sig);
    6865              :         }
    6866              :       break;
    6867              : 
    6868      2204542 :     case OVERLOAD:
    6869      2204542 :       WT (((lang_tree_node *)t)->overload.function);
    6870      2204542 :       WT (t->common.chain);
    6871      2204542 :       break;
    6872              : 
    6873            0 :     case PTRMEM_CST:
    6874            0 :       WT (((lang_tree_node *)t)->ptrmem.member);
    6875            0 :       break;
    6876              : 
    6877        17727 :     case STATIC_ASSERT:
    6878        17727 :       WT (((lang_tree_node *)t)->static_assertion.condition);
    6879        17727 :       WT (((lang_tree_node *)t)->static_assertion.message);
    6880        17727 :       if (state)
    6881        17727 :         state->write_location
    6882        17727 :           (*this, ((lang_tree_node *)t)->static_assertion.location);
    6883              :       break;
    6884              : 
    6885       714178 :     case TEMPLATE_DECL:
    6886              :       /* Streamed with the template_decl node itself.  */
    6887       714178 :       gcc_checking_assert
    6888              :         (TREE_VISITED (((lang_tree_node *)t)->template_decl.arguments));
    6889       714178 :       gcc_checking_assert
    6890              :         (TREE_VISITED (((lang_tree_node *)t)->template_decl.result));
    6891       714178 :       if (DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (t))
    6892        13264 :         WT (DECL_CHAIN (t));
    6893              :       break;
    6894              : 
    6895      1607661 :     case TEMPLATE_INFO:
    6896      1607661 :       {
    6897      1607661 :         WT (((lang_tree_node *)t)->template_info.tmpl);
    6898      1607661 :         WT (((lang_tree_node *)t)->template_info.args);
    6899      1607661 :         WT (((lang_tree_node *)t)->template_info.partial);
    6900              : 
    6901      1607661 :         const auto *ac = (((lang_tree_node *)t)
    6902              :                           ->template_info.deferred_access_checks);
    6903      1607661 :         unsigned len = vec_safe_length (ac);
    6904      1607661 :         if (streaming_p ())
    6905       801971 :           u (len);
    6906      1607661 :         if (len)
    6907              :           {
    6908            0 :             for (unsigned ix = 0; ix != len; ix++)
    6909              :               {
    6910            0 :                 const auto &m = (*ac)[ix];
    6911            0 :                 WT (m.binfo);
    6912            0 :                 WT (m.decl);
    6913            0 :                 WT (m.diag_decl);
    6914            0 :                 if (state)
    6915            0 :                   state->write_location (*this, m.loc);
    6916              :               }
    6917              :           }
    6918              :       }
    6919              :       break;
    6920              : 
    6921      2043552 :     case TEMPLATE_PARM_INDEX:
    6922      2043552 :       if (streaming_p ())
    6923              :         {
    6924       457833 :           WU (((lang_tree_node *)t)->tpi.index);
    6925       457833 :           WU (((lang_tree_node *)t)->tpi.level);
    6926       457833 :           WU (((lang_tree_node *)t)->tpi.orig_level);
    6927              :         }
    6928      2043552 :       WT (((lang_tree_node *)t)->tpi.decl);
    6929              :       /* TEMPLATE_PARM_DESCENDANTS (AKA TREE_CHAIN) is an internal
    6930              :          cache, do not stream.  */
    6931      2043552 :       break;
    6932              : 
    6933        26934 :     case TRAIT_EXPR:
    6934        26934 :       WT (((lang_tree_node *)t)->trait_expression.type1);
    6935        26934 :       WT (((lang_tree_node *)t)->trait_expression.type2);
    6936        26934 :       if (streaming_p ())
    6937        10366 :         WU (((lang_tree_node *)t)->trait_expression.kind);
    6938              :       break;
    6939              : 
    6940            4 :     case TU_LOCAL_ENTITY:
    6941            4 :       WT (((lang_tree_node *)t)->tu_local_entity.name);
    6942            4 :       if (state)
    6943            4 :         state->write_location
    6944            4 :           (*this, ((lang_tree_node *)t)->tu_local_entity.loc);
    6945              :       break;
    6946              :     }
    6947              : 
    6948     42182538 :   if (CODE_CONTAINS_STRUCT (code, TS_TYPED))
    6949              :     {
    6950              :       /* We want to stream the type of a expression-like nodes /after/
    6951              :          we've streamed the operands.  The type often contains (bits
    6952              :          of the) types of the operands, and with things like decltype
    6953              :          and noexcept in play, we really want to stream the decls
    6954              :          defining the type before we try and stream the type on its
    6955              :          own.  Otherwise we can find ourselves trying to read in a
    6956              :          decl, when we're already partially reading in a component of
    6957              :          its type.  And that's bad.  */
    6958     39807216 :       tree type = t->typed.type;
    6959     39807216 :       unsigned prec = 0;
    6960              : 
    6961     39807216 :       switch (code)
    6962              :         {
    6963              :         default:
    6964              :           break;
    6965              : 
    6966              :         case TEMPLATE_DECL:
    6967              :           /* We fill in the template's type separately.  */
    6968     39807216 :           type = NULL_TREE;
    6969              :           break;
    6970              : 
    6971      2816249 :         case TYPE_DECL:
    6972      2816249 :           if (DECL_ORIGINAL_TYPE (t) && t == TYPE_NAME (type))
    6973              :             /* This is a typedef.  We set its type separately.  */
    6974              :             type = NULL_TREE;
    6975              :           break;
    6976              : 
    6977         8518 :         case ENUMERAL_TYPE:
    6978         8518 :           if (type && !ENUM_FIXED_UNDERLYING_TYPE_P (t))
    6979              :             {
    6980              :               /* Type is a restricted range integer type derived from the
    6981              :                  integer_types.  Find the right one.  */
    6982         5124 :               prec = TYPE_PRECISION (type);
    6983         5124 :               tree name = DECL_NAME (TYPE_NAME (type));
    6984              : 
    6985        66954 :               for (unsigned itk = itk_none; itk--;)
    6986        66954 :                 if (integer_types[itk]
    6987        66954 :                     && DECL_NAME (TYPE_NAME (integer_types[itk])) == name)
    6988              :                   {
    6989              :                     type = integer_types[itk];
    6990              :                     break;
    6991              :                   }
    6992         5124 :               gcc_assert (type != t->typed.type);
    6993              :             }
    6994              :           break;
    6995              :         }
    6996              : 
    6997     39807216 :       WT (type);
    6998     39807216 :       if (prec && streaming_p ())
    6999         2560 :         WU (prec);
    7000              :     }
    7001              : 
    7002     42182538 :   if (TREE_CODE (t) == CONSTRUCTOR)
    7003              :     {
    7004       110717 :       unsigned len = vec_safe_length (t->constructor.elts);
    7005       110717 :       if (streaming_p ())
    7006        54603 :         WU (len);
    7007       110717 :       if (len)
    7008       405565 :         for (unsigned ix = 0; ix != len; ix++)
    7009              :           {
    7010       346770 :             const constructor_elt &elt = (*t->constructor.elts)[ix];
    7011              : 
    7012       346770 :             WT (elt.index);
    7013       346770 :             WT (elt.value);
    7014              :           }
    7015              :     }
    7016              : 
    7017              : #undef WT
    7018              : #undef WU
    7019     42182538 : }
    7020              : 
    7021              : // Streaming in a reference to a decl can cause that decl to be
    7022              : // TREE_USED, which is the mark_used behaviour we need most of the
    7023              : // time.  The trees_in::unused can be incremented to inhibit this,
    7024              : // which is at least needed for vtables.
    7025              : 
    7026              : bool
    7027     15126656 : trees_in::core_vals (tree t)
    7028              : {
    7029              : #define RU(X) ((X) = u ())
    7030              : #define RUC(T,X) ((X) = T (u ()))
    7031              : #define RT(X) ((X) = tree_node ())
    7032              : #define RTU(X) ((X) = tree_node (true))
    7033     15126656 :   tree_code code = TREE_CODE (t);
    7034              : 
    7035              :   /* First by tree shape.  */
    7036     15126656 :   if (CODE_CONTAINS_STRUCT (code, TS_DECL_MINIMAL))
    7037              :     {
    7038      3239399 :       RT (t->decl_minimal.name);
    7039      3239399 :       if (!DECL_TEMPLATE_PARM_P (t))
    7040      2819489 :         RT (t->decl_minimal.context);
    7041              : 
    7042              :       /* Don't zap the locus just yet, we don't record it correctly
    7043              :          and thus lose all location information.  */
    7044      3239399 :       t->decl_minimal.locus = state->read_location (*this);
    7045      3239399 :       if (has_warning_spec (t))
    7046          660 :         put_warning_spec (t, u ());
    7047              :     }
    7048              : 
    7049     15126656 :   if (CODE_CONTAINS_STRUCT (code, TS_TYPE_COMMON))
    7050              :     {
    7051       551762 :       RT (t->type_common.name);
    7052       551762 :       RT (t->type_common.context);
    7053              : 
    7054       551762 :       RT (t->type_common.main_variant);
    7055       551762 :       RT (t->type_common.canonical);
    7056              : 
    7057              :       /* type_common.next_variant is internally manipulated.  */
    7058              :       /* type_common.pointer_to, type_common.reference_to.  */
    7059              : 
    7060       551762 :       RU (t->type_common.precision);
    7061       551762 :       RU (t->type_common.contains_placeholder_bits);
    7062       551762 :       RUC (machine_mode, t->type_common.mode);
    7063       551762 :       RU (t->type_common.align);
    7064              : 
    7065       551762 :       if (!RECORD_OR_UNION_CODE_P (code))
    7066              :         {
    7067       380752 :           RT (t->type_common.size);
    7068       380752 :           RT (t->type_common.size_unit);
    7069              :         }
    7070       551762 :       RT (t->type_common.attributes);
    7071              : 
    7072       551762 :       RT (t->type_common.common.chain); /* TYPE_STUB_DECL.  */
    7073              :     }
    7074              : 
    7075     15126656 :   if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
    7076              :     {
    7077      3239399 :       RUC (machine_mode, t->decl_common.mode);
    7078      3239399 :       RU (t->decl_common.off_align);
    7079      3239399 :       RU (t->decl_common.align);
    7080              : 
    7081      3239399 :       if (code != TEMPLATE_DECL)
    7082              :         {
    7083      2913294 :           RT (t->decl_common.size);
    7084      2913294 :           RT (t->decl_common.size_unit);
    7085              :         }
    7086              : 
    7087      3239399 :       RT (t->decl_common.attributes);
    7088      3239399 :       RT (t->decl_common.abstract_origin);
    7089              :     }
    7090              : 
    7091     15126656 :   if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
    7092              :     {
    7093      1489945 :       RT (t->decl_with_vis.assembler_name);
    7094      1489945 :       RUC (symbol_visibility, t->decl_with_vis.visibility);
    7095              :     }
    7096              : 
    7097     15126656 :   if (CODE_CONTAINS_STRUCT (code, TS_TYPE_NON_COMMON))
    7098              :     {
    7099       551762 :       if (code == ENUMERAL_TYPE)
    7100              :         {
    7101              :           /* These fields get set even for opaque enums that lack a
    7102              :              definition, so we stream them directly for each ENUMERAL_TYPE.
    7103              :              We stream TYPE_VALUES as part of the definition.  */
    7104         3168 :           RT (t->type_non_common.maxval);
    7105         3168 :           RT (t->type_non_common.minval);
    7106              :         }
    7107              :       /* Records and unions hold FIELDS, VFIELD & BINFO on these
    7108              :          things.  */
    7109       548594 :       else if (!RECORD_OR_UNION_CODE_P (code))
    7110              :         {
    7111              :           /* This is not clobbering TYPE_CACHED_VALUES, because this
    7112              :              is a type that doesn't have any.  */
    7113       377584 :           gcc_checking_assert (!TYPE_CACHED_VALUES_P (t));
    7114       377584 :           RT (t->type_non_common.values);
    7115       377584 :           RT (t->type_non_common.maxval);
    7116       377584 :           RT (t->type_non_common.minval);
    7117              :         }
    7118              : 
    7119       551762 :       RT (t->type_non_common.lang_1);
    7120              :     }
    7121              : 
    7122     15126656 :   if (CODE_CONTAINS_STRUCT (code, TS_EXP))
    7123              :     {
    7124      5944309 :       t->exp.locus = state->read_location (*this);
    7125      5944309 :       if (has_warning_spec (t))
    7126       472506 :         put_warning_spec (t, u ());
    7127              : 
    7128      5944309 :       bool vl = TREE_CODE_CLASS (code) == tcc_vl_exp;
    7129      5944309 :       unsigned limit = (vl ? VL_EXP_OPERAND_LENGTH (t)
    7130      5944309 :                         : TREE_OPERAND_LENGTH (t));
    7131      5944309 :       unsigned ix = unsigned (vl);
    7132      5944309 :       if (code == REQUIRES_EXPR)
    7133              :         {
    7134         7359 :           REQUIRES_EXPR_PARMS (t) = chained_decls ();
    7135         7359 :           ++ix;
    7136              :         }
    7137     16259698 :       for (; ix != limit; ix++)
    7138     10315389 :         RTU (TREE_OPERAND (t, ix));
    7139              :     }
    7140              : 
    7141              :   /* Then by CODE.  Special cases and/or 1:1 tree shape
    7142              :      correspondance. */
    7143     15126656 :   switch (code)
    7144              :     {
    7145              :     default:
    7146              :       break;
    7147              : 
    7148              :     case ARGUMENT_PACK_SELECT:
    7149              :     case DEFERRED_PARSE:
    7150              :     case IDENTIFIER_NODE:
    7151              :     case BINDING_VECTOR:
    7152              :     case SSA_NAME:
    7153              :     case TRANSLATION_UNIT_DECL:
    7154              :     case USERDEF_LITERAL:
    7155              :       return false; /* Should never meet.  */
    7156              : 
    7157              :       /* Constants.  */
    7158            9 :     case COMPLEX_CST:
    7159            9 :       RT (TREE_REALPART (t));
    7160            9 :       RT (TREE_IMAGPART (t));
    7161            9 :       break;
    7162              : 
    7163              :     case FIXED_CST:
    7164              :       /* Not suported in C++.  */
    7165              :       return false;
    7166              : 
    7167       589438 :     case INTEGER_CST:
    7168       589438 :       {
    7169       589438 :         unsigned num = TREE_INT_CST_EXT_NUNITS (t);
    7170      1180705 :         for (unsigned ix = 0; ix != num; ix++)
    7171       591267 :           TREE_INT_CST_ELT (t, ix) = wu ();
    7172              :       }
    7173              :       break;
    7174              : 
    7175              :     case POLY_INT_CST:
    7176            0 :       for (unsigned ix = 0; ix != NUM_POLY_INT_COEFFS; ix++)
    7177            0 :         RT (POLY_INT_CST_COEFF (t, ix));
    7178              :       break;
    7179              : 
    7180        16756 :     case REAL_CST:
    7181        16756 :       if (const void *bytes = buf (sizeof (real_value)))
    7182        16756 :         memcpy (TREE_REAL_CST_PTR (t), bytes, sizeof (real_value));
    7183              :       break;
    7184              : 
    7185              :     case STRING_CST:
    7186              :       /* Streamed during start.  */
    7187              :       break;
    7188              : 
    7189            6 :     case RAW_DATA_CST:
    7190            6 :       RT (RAW_DATA_OWNER (t));
    7191            6 :       gcc_assert (TREE_CODE (RAW_DATA_OWNER (t)) == STRING_CST
    7192              :                   && TREE_STRING_LENGTH (RAW_DATA_OWNER (t)));
    7193            6 :       RAW_DATA_POINTER (t) = TREE_STRING_POINTER (RAW_DATA_OWNER (t)) + z ();
    7194            6 :       break;
    7195              : 
    7196           24 :     case VECTOR_CST:
    7197           63 :       for (unsigned ix = vector_cst_encoded_nelts (t); ix--;)
    7198           39 :         RT (VECTOR_CST_ENCODED_ELT (t, ix));
    7199              :       break;
    7200              : 
    7201              :       /* Decls.  */
    7202       215593 :     case VAR_DECL:
    7203       215593 :       if (DECL_CONTEXT (t)
    7204       215593 :           && TREE_CODE (DECL_CONTEXT (t)) != FUNCTION_DECL)
    7205              :         {
    7206        41889 :           if (DECL_HAS_VALUE_EXPR_P (t))
    7207              :             {
    7208            9 :               tree val = tree_node ();
    7209            9 :               SET_DECL_VALUE_EXPR (t, val);
    7210              :             }
    7211              :           break;
    7212              :         }
    7213              :       /* FALLTHROUGH  */
    7214              : 
    7215      1473165 :     case RESULT_DECL:
    7216      1473165 :     case PARM_DECL:
    7217      1473165 :       if (DECL_HAS_VALUE_EXPR_P (t))
    7218              :         {
    7219              :           /* The DECL_VALUE hash table is a cache, thus if we're
    7220              :              reading a duplicate (which we end up discarding), the
    7221              :              value expr will also be cleaned up at the next gc.  */
    7222        13147 :           tree val = tree_node ();
    7223        13147 :           SET_DECL_VALUE_EXPR (t, val);
    7224              :         }
    7225              :       /* FALLTHROUGH  */
    7226              : 
    7227      1518163 :     case CONST_DECL:
    7228      1518163 :     case IMPORTED_DECL:
    7229      1518163 :       RT (t->decl_common.initial);
    7230      1518163 :       break;
    7231              : 
    7232        55489 :     case FIELD_DECL:
    7233        55489 :       RT (t->field_decl.offset);
    7234        55489 :       RT (t->field_decl.bit_field_type);
    7235        55489 :       RT (t->field_decl.qualifier);
    7236        55489 :       RT (t->field_decl.bit_offset);
    7237        55489 :       RT (t->field_decl.fcontext);
    7238        55489 :       RT (t->decl_common.initial);
    7239        55489 :       break;
    7240              : 
    7241        17312 :     case LABEL_DECL:
    7242        17312 :       RU (t->label_decl.label_decl_uid);
    7243        17312 :       RU (t->label_decl.eh_landing_pad_nr);
    7244        17312 :       break;
    7245              : 
    7246       458495 :     case FUNCTION_DECL:
    7247       458495 :       {
    7248       458495 :         unsigned bltin = u ();
    7249       458495 :         t->function_decl.built_in_class = built_in_class (bltin);
    7250       458495 :         if (bltin != NOT_BUILT_IN)
    7251              :           {
    7252         9540 :             bltin = u ();
    7253         9540 :             DECL_UNCHECKED_FUNCTION_CODE (t) = built_in_function (bltin);
    7254              :           }
    7255              : 
    7256       458495 :         RT (t->function_decl.personality);
    7257              :         /* These properties are not streamed, and should be reconstructed
    7258              :            from any function attributes.  */
    7259              :         // t->function_decl.function_specific_target);
    7260              :         // t->function_decl.function_specific_optimization);
    7261       458495 :         RT (t->function_decl.vindex);
    7262              : 
    7263       458495 :         if (DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (t))
    7264              :           {
    7265         3722 :             tree spec;
    7266         3722 :             RT (spec);
    7267         3722 :             store_explicit_specifier (t, spec);
    7268              :           }
    7269              :       }
    7270              :       break;
    7271              : 
    7272        51120 :     case USING_DECL:
    7273              :       /* USING_DECL_DECLS  */
    7274        51120 :       RT (t->decl_common.initial);
    7275              :       /* FALLTHROUGH  */
    7276              : 
    7277       815666 :     case TYPE_DECL:
    7278              :       /* USING_DECL: USING_DECL_SCOPE  */
    7279              :       /* TYPE_DECL: DECL_ORIGINAL_TYPE */
    7280       815666 :       RT (t->decl_non_common.result);
    7281       815666 :       break;
    7282              : 
    7283              :       /* Miscellaneous common nodes.  */
    7284       303403 :     case BLOCK:
    7285       303403 :       t->block.locus = state->read_location (*this);
    7286       303403 :       t->block.end_locus = state->read_location (*this);
    7287              : 
    7288       303403 :       for (tree *chain = &t->block.vars;;)
    7289       456234 :         if (tree decl = tree_node ())
    7290              :           {
    7291              :             /* For a deduplicated local type or enumerator, chain the
    7292              :                duplicate decl instead of the canonical in-TU decl.  Seeing
    7293              :                a duplicate here means the containing function whose body
    7294              :                we're streaming in is a duplicate too, so we'll end up
    7295              :                discarding this BLOCK (and the rest of the duplicate function
    7296              :                body) anyway.  */
    7297       152831 :             decl = maybe_duplicate (decl);
    7298              : 
    7299       152831 :             if (!DECL_P (decl))
    7300              :               {
    7301            0 :                 set_overrun ();
    7302            0 :                 break;
    7303              :               }
    7304              : 
    7305              :             /* If DECL_CHAIN is already set then this was a backreference to a
    7306              :                local type or enumerator from a previous read (PR c++/114630).
    7307              :                Let's copy the node so we can keep building the chain for ODR
    7308              :                checking later.  */
    7309       152831 :             if (DECL_CHAIN (decl))
    7310              :               {
    7311           12 :                 gcc_checking_assert (TREE_CODE (decl) == TYPE_DECL
    7312              :                                      && find_duplicate (DECL_CONTEXT (decl)));
    7313            6 :                 decl = copy_decl (decl);
    7314              :               }
    7315              : 
    7316       152831 :             *chain = decl;
    7317       152831 :             chain = &DECL_CHAIN (decl);
    7318              :           }
    7319              :         else
    7320       152831 :           break;
    7321              : 
    7322              :       /* nonlocalized_vars is middle-end.  */
    7323       303403 :       RT (t->block.subblocks);
    7324       303403 :       RT (t->block.supercontext);
    7325       303403 :       RT (t->block.abstract_origin);
    7326              :       /* fragment_origin, fragment_chain are middle-end.  */
    7327       303403 :       RT (t->block.chain);
    7328              :       /* nonlocalized_vars, block_num, die are middle endy/debug
    7329              :          things.  */
    7330       303403 :       break;
    7331              : 
    7332       576943 :     case CALL_EXPR:
    7333       576943 :       RUC (internal_fn, t->base.u.ifn);
    7334       576943 :       break;
    7335              : 
    7336              :     case CONSTRUCTOR:
    7337              :       // Streamed after the node's type.
    7338              :       break;
    7339              : 
    7340           18 :     case OMP_CLAUSE:
    7341           18 :       {
    7342           18 :         RU (t->omp_clause.subcode.map_kind);
    7343           18 :         t->omp_clause.locus = state->read_location (*this);
    7344              : 
    7345           18 :         unsigned len = omp_clause_num_ops[OMP_CLAUSE_CODE (t)];
    7346           66 :         for (unsigned ix = 0; ix != len; ix++)
    7347           48 :           RT (t->omp_clause.ops[ix]);
    7348              :       }
    7349              :       break;
    7350              : 
    7351       274397 :     case STATEMENT_LIST:
    7352       274397 :       {
    7353       274397 :         tree_stmt_iterator iter = tsi_start (t);
    7354      1136073 :         for (tree stmt; RT (stmt);)
    7355              :           {
    7356       861676 :             if (TREE_CODE (stmt) == DEBUG_BEGIN_STMT
    7357       166842 :                 && !MAY_HAVE_DEBUG_MARKER_STMTS)
    7358            0 :               continue;
    7359       861676 :             tsi_link_after (&iter, stmt, TSI_CONTINUE_LINKING);
    7360              :           }
    7361              :       }
    7362       274397 :       break;
    7363              : 
    7364            0 :     case OPTIMIZATION_NODE:
    7365            0 :     case TARGET_OPTION_NODE:
    7366              :       /* Not implemented, see trees_out::core_vals.  */
    7367            0 :       gcc_unreachable ();
    7368        90203 :       break;
    7369              : 
    7370        90203 :     case TREE_BINFO:
    7371        90203 :       RT (t->binfo.common.chain);
    7372        90203 :       RT (t->binfo.offset);
    7373        90203 :       RT (t->binfo.inheritance);
    7374        90203 :       RT (t->binfo.vptr_field);
    7375              : 
    7376              :       /* Do not mark the vtables as USED in the address expressions
    7377              :          here.  */
    7378        90203 :       unused++;
    7379        90203 :       RT (t->binfo.vtable);
    7380        90203 :       RT (t->binfo.virtuals);
    7381        90203 :       RT (t->binfo.vtt_subvtt);
    7382        90203 :       RT (t->binfo.vtt_vptr);
    7383        90203 :       unused--;
    7384              : 
    7385        90203 :       BINFO_BASE_ACCESSES (t) = tree_vec ();
    7386        90203 :       if (!get_overrun ())
    7387              :         {
    7388        90203 :           unsigned num = vec_safe_length (BINFO_BASE_ACCESSES (t));
    7389       115001 :           for (unsigned ix = 0; ix != num; ix++)
    7390        24798 :             BINFO_BASE_APPEND (t, tree_node ());
    7391              :         }
    7392              :       break;
    7393              : 
    7394      1302740 :     case TREE_LIST:
    7395      1302740 :       RT (t->list.purpose);
    7396      1302740 :       RT (t->list.value);
    7397      1302740 :       RT (t->list.common.chain);
    7398      1302740 :       break;
    7399              : 
    7400       838753 :     case TREE_VEC:
    7401      2258936 :       for (unsigned ix = TREE_VEC_LENGTH (t); ix--;)
    7402      1420183 :         RT (TREE_VEC_ELT (t, ix));
    7403       838753 :       RT (t->type_common.common.chain);
    7404       838753 :       break;
    7405              : 
    7406              :       /* C++-specific nodes ...  */
    7407       106607 :     case BASELINK:
    7408       106607 :       RT (((lang_tree_node *)t)->baselink.binfo);
    7409       106607 :       RTU (((lang_tree_node *)t)->baselink.functions);
    7410       106607 :       RT (((lang_tree_node *)t)->baselink.access_binfo);
    7411       106607 :       RT (((lang_tree_node *)t)->baselink.common.chain);
    7412       106607 :       break;
    7413              : 
    7414        43551 :     case CONSTRAINT_INFO:
    7415        43551 :       RT (((lang_tree_node *)t)->constraint_info.template_reqs);
    7416        43551 :       RT (((lang_tree_node *)t)->constraint_info.declarator_reqs);
    7417        43551 :       RT (((lang_tree_node *)t)->constraint_info.associated_constr);
    7418        43551 :       break;
    7419              : 
    7420         7462 :     case DEFERRED_NOEXCEPT:
    7421         7462 :       RT (((lang_tree_node *)t)->deferred_noexcept.pattern);
    7422         7462 :       RT (((lang_tree_node *)t)->deferred_noexcept.args);
    7423         7462 :       break;
    7424              : 
    7425         4053 :     case LAMBDA_EXPR:
    7426         4053 :       RT (((lang_tree_node *)t)->lambda_expression.capture_list);
    7427         4053 :       RT (((lang_tree_node *)t)->lambda_expression.this_capture);
    7428         4053 :       RT (((lang_tree_node *)t)->lambda_expression.extra_scope);
    7429         4053 :       RT (((lang_tree_node *)t)->lambda_expression.regen_info);
    7430         4053 :       RT (((lang_tree_node *)t)->lambda_expression.extra_args);
    7431              :       /* lambda_expression.pending_proxies is NULL  */
    7432         4053 :       ((lang_tree_node *)t)->lambda_expression.locus
    7433         4053 :         = state->read_location (*this);
    7434         4053 :       RUC (cp_lambda_default_capture_mode_type,
    7435              :            ((lang_tree_node *)t)->lambda_expression.default_capture_mode);
    7436         4053 :       RU (((lang_tree_node *)t)->lambda_expression.discriminator_scope);
    7437         4053 :       RU (((lang_tree_node *)t)->lambda_expression.discriminator_sig);
    7438         4053 :       break;
    7439              : 
    7440       567827 :     case OVERLOAD:
    7441       567827 :       RT (((lang_tree_node *)t)->overload.function);
    7442       567827 :       RT (t->common.chain);
    7443       567827 :       break;
    7444              : 
    7445            0 :     case PTRMEM_CST:
    7446            0 :       RT (((lang_tree_node *)t)->ptrmem.member);
    7447            0 :       break;
    7448              : 
    7449         7990 :     case STATIC_ASSERT:
    7450         7990 :       RT (((lang_tree_node *)t)->static_assertion.condition);
    7451         7990 :       RT (((lang_tree_node *)t)->static_assertion.message);
    7452         7990 :       ((lang_tree_node *)t)->static_assertion.location
    7453         7990 :         = state->read_location (*this);
    7454         7990 :       break;
    7455              : 
    7456       326105 :     case TEMPLATE_DECL:
    7457              :       /* Streamed when reading the raw template decl itself.  */
    7458       326105 :       gcc_assert (((lang_tree_node *)t)->template_decl.arguments);
    7459       326105 :       gcc_assert (((lang_tree_node *)t)->template_decl.result);
    7460       326105 :       if (DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (t))
    7461         7028 :         RT (DECL_CHAIN (t));
    7462              :       break;
    7463              : 
    7464       724874 :     case TEMPLATE_INFO:
    7465       724874 :       RT (((lang_tree_node *)t)->template_info.tmpl);
    7466       724874 :       RT (((lang_tree_node *)t)->template_info.args);
    7467       724874 :       RT (((lang_tree_node *)t)->template_info.partial);
    7468       724874 :       if (unsigned len = u ())
    7469              :         {
    7470            0 :           auto &ac = (((lang_tree_node *)t)
    7471              :                       ->template_info.deferred_access_checks);
    7472            0 :           vec_alloc (ac, len);
    7473            0 :           for (unsigned ix = 0; ix != len; ix++)
    7474              :             {
    7475            0 :               deferred_access_check m;
    7476              : 
    7477            0 :               RT (m.binfo);
    7478            0 :               RT (m.decl);
    7479            0 :               RT (m.diag_decl);
    7480            0 :               m.loc = state->read_location (*this);
    7481            0 :               ac->quick_push (m);
    7482              :             }
    7483              :         }
    7484              :       break;
    7485              : 
    7486       402308 :     case TEMPLATE_PARM_INDEX:
    7487       402308 :       RU (((lang_tree_node *)t)->tpi.index);
    7488       402308 :       RU (((lang_tree_node *)t)->tpi.level);
    7489       402308 :       RU (((lang_tree_node *)t)->tpi.orig_level);
    7490       402308 :       RT (((lang_tree_node *)t)->tpi.decl);
    7491       402308 :       break;
    7492              : 
    7493         8175 :     case TRAIT_EXPR:
    7494         8175 :       RT (((lang_tree_node *)t)->trait_expression.type1);
    7495         8175 :       RT (((lang_tree_node *)t)->trait_expression.type2);
    7496         8175 :       RUC (cp_trait_kind, ((lang_tree_node *)t)->trait_expression.kind);
    7497         8175 :       break;
    7498              : 
    7499            2 :     case TU_LOCAL_ENTITY:
    7500            2 :       RT (((lang_tree_node *)t)->tu_local_entity.name);
    7501            2 :       ((lang_tree_node *)t)->tu_local_entity.loc
    7502            2 :         = state->read_location (*this);
    7503              :     }
    7504              : 
    7505     15126656 :   if (CODE_CONTAINS_STRUCT (code, TS_TYPED))
    7506              :     {
    7507     14039374 :       tree type = tree_node ();
    7508              : 
    7509     14039374 :       if (type && code == ENUMERAL_TYPE && !ENUM_FIXED_UNDERLYING_TYPE_P (t))
    7510              :         {
    7511         1743 :           unsigned precision = u ();
    7512              : 
    7513         1743 :           type = build_distinct_type_copy (type);
    7514         1743 :           TYPE_PRECISION (type) = precision;
    7515         3486 :           set_min_and_max_values_for_integral_type (type, precision,
    7516         1743 :                                                     TYPE_SIGN (type));
    7517              :         }
    7518              : 
    7519     14039374 :       if (code != TEMPLATE_DECL)
    7520     13713269 :         t->typed.type = type;
    7521              :     }
    7522              : 
    7523     15126656 :   if (TREE_CODE (t) == CONSTRUCTOR)
    7524        52524 :     if (unsigned len = u ())
    7525              :       {
    7526        31535 :         vec_alloc (t->constructor.elts, len);
    7527       209949 :         for (unsigned ix = 0; ix != len; ix++)
    7528              :           {
    7529       178414 :             constructor_elt elt;
    7530              : 
    7531       178414 :             RT (elt.index);
    7532       178414 :             RTU (elt.value);
    7533       178414 :             t->constructor.elts->quick_push (elt);
    7534              :           }
    7535              :       }
    7536              : 
    7537              : #undef RT
    7538              : #undef RM
    7539              : #undef RU
    7540     15126656 :   return !get_overrun ();
    7541              : }
    7542              : 
    7543              : void
    7544      5160126 : trees_out::lang_decl_vals (tree t)
    7545              : {
    7546      5160126 :   const struct lang_decl *lang = DECL_LANG_SPECIFIC (t);
    7547              : #define WU(X) (u (X))
    7548              : #define WT(X) (tree_node (X))
    7549              :   /* Module index already written.  */
    7550      5160126 :   switch (lang->u.base.selector)
    7551              :     {
    7552            0 :     default:
    7553            0 :       gcc_unreachable ();
    7554              : 
    7555       986118 :     case lds_fn:  /* lang_decl_fn.  */
    7556       986118 :       if (streaming_p ())
    7557              :         {
    7558       492952 :           if (DECL_NAME (t) && IDENTIFIER_OVL_OP_P (DECL_NAME (t)))
    7559        83255 :             WU (lang->u.fn.ovl_op_code);
    7560              :         }
    7561              : 
    7562      1239256 :       if (DECL_CLASS_SCOPE_P (t) || DECL_UNIQUE_FRIEND_P (t))
    7563       758496 :         WT (lang->u.fn.context);
    7564              : 
    7565       986118 :       if (lang->u.fn.thunk_p)
    7566              :         {
    7567              :           /* The thunked-to function.  */
    7568         1400 :           WT (lang->u.fn.befriending_classes);
    7569         1400 :           if (streaming_p ())
    7570          700 :             wi (lang->u.fn.u5.fixed_offset);
    7571              :         }
    7572       984718 :       else if (decl_tls_wrapper_p (t))
    7573              :         /* The wrapped variable.  */
    7574           18 :         WT (lang->u.fn.befriending_classes);
    7575              :       else
    7576       984700 :         WT (lang->u.fn.u5.cloned_function);
    7577              : 
    7578       986118 :       if (FNDECL_USED_AUTO (t))
    7579         3637 :         WT (lang->u.fn.u.saved_auto_return_type);
    7580              : 
    7581       986118 :       goto lds_min;
    7582              : 
    7583         3834 :     case lds_decomp:  /* lang_decl_decomp.  */
    7584         3834 :       WT (lang->u.decomp.base);
    7585         3834 :       goto lds_min;
    7586              : 
    7587      3009561 :     case lds_min:  /* lang_decl_min.  */
    7588      3009561 :     lds_min:
    7589      3009561 :       WT (lang->u.min.template_info);
    7590      3009561 :       {
    7591      3009561 :         tree access = lang->u.min.access;
    7592              : 
    7593              :         /* DECL_ACCESS needs to be maintained by the definition of the
    7594              :            (derived) class that changes the access.  The other users
    7595              :            of DECL_ACCESS need to write it here.  */
    7596       986118 :         if (!DECL_THUNK_P (t)
    7597      3994279 :             && (DECL_CONTEXT (t) && TYPE_P (DECL_CONTEXT (t))))
    7598              :           access = NULL_TREE;
    7599              : 
    7600      3009561 :         WT (access);
    7601              :       }
    7602              :       /* A friend template specialisation stashes its owning class on its
    7603              :          DECL_CHAIN; we need to reconstruct this, but it needs to happen
    7604              :          after we stream the template_info so readers can know this is such
    7605              :          an entity.  */
    7606      3009561 :       if (decl_specialization_friend_p (t))
    7607          102 :         WT (t->common.chain);
    7608              :       break;
    7609              : 
    7610              :     case lds_ns:  /* lang_decl_ns.  */
    7611              :       break;
    7612              : 
    7613      2150320 :     case lds_parm:  /* lang_decl_parm.  */
    7614      2150320 :       if (streaming_p ())
    7615              :         {
    7616       731689 :           WU (lang->u.parm.level);
    7617       731689 :           WU (lang->u.parm.index);
    7618              :         }
    7619              :       break;
    7620              :     }
    7621              : #undef WU
    7622              : #undef WT
    7623      5160126 : }
    7624              : 
    7625              : bool
    7626      2033175 : trees_in::lang_decl_vals (tree t)
    7627              : {
    7628      2033175 :   struct lang_decl *lang = DECL_LANG_SPECIFIC (t);
    7629              : #define RU(X) ((X) = u ())
    7630              : #define RT(X) ((X) = tree_node ())
    7631              : 
    7632              :   /* Module index already read.  */
    7633      2033175 :   switch (lang->u.base.selector)
    7634              :     {
    7635            0 :     default:
    7636            0 :       gcc_unreachable ();
    7637              : 
    7638       458495 :     case lds_fn:  /* lang_decl_fn.  */
    7639       458495 :       if (DECL_NAME (t) && IDENTIFIER_OVL_OP_P (DECL_NAME (t)))
    7640              :         {
    7641        79650 :           unsigned code = u ();
    7642              : 
    7643              :           /* Check consistency.  */
    7644        79650 :           if (code >= OVL_OP_MAX
    7645        79650 :               || (ovl_op_info[IDENTIFIER_ASSIGN_OP_P (DECL_NAME (t))][code]
    7646        79650 :                   .ovl_op_code) == OVL_OP_ERROR_MARK)
    7647            0 :             set_overrun ();
    7648              :           else
    7649        79650 :             lang->u.fn.ovl_op_code = code;
    7650              :         }
    7651              : 
    7652       568897 :       if (DECL_CLASS_SCOPE_P (t) || DECL_UNIQUE_FRIEND_P (t))
    7653       361306 :         RT (lang->u.fn.context);
    7654              : 
    7655       458495 :       if (lang->u.fn.thunk_p)
    7656              :         {
    7657          574 :           RT (lang->u.fn.befriending_classes);
    7658          574 :           lang->u.fn.u5.fixed_offset = wi ();
    7659              :         }
    7660       457921 :       else if (decl_tls_wrapper_p (t))
    7661           15 :         RT (lang->u.fn.befriending_classes);
    7662              :       else
    7663       457906 :         RT (lang->u.fn.u5.cloned_function);
    7664              : 
    7665       458495 :       if (FNDECL_USED_AUTO (t))
    7666         1434 :         RT (lang->u.fn.u.saved_auto_return_type);
    7667       458495 :       goto lds_min;
    7668              : 
    7669         2032 :     case lds_decomp:  /* lang_decl_decomp.  */
    7670         2032 :       RT (lang->u.decomp.base);
    7671         2032 :       goto lds_min;
    7672              : 
    7673      1361158 :     case lds_min:  /* lang_decl_min.  */
    7674      1361158 :     lds_min:
    7675      1361158 :       RT (lang->u.min.template_info);
    7676      1361158 :       RT (lang->u.min.access);
    7677      1361158 :       if (decl_specialization_friend_p (t))
    7678           51 :         RT (t->common.chain);
    7679              :       break;
    7680              : 
    7681              :     case lds_ns:  /* lang_decl_ns.  */
    7682              :       break;
    7683              : 
    7684       671862 :     case lds_parm:  /* lang_decl_parm.  */
    7685       671862 :       RU (lang->u.parm.level);
    7686       671862 :       RU (lang->u.parm.index);
    7687       671862 :       break;
    7688              :     }
    7689              : #undef RU
    7690              : #undef RT
    7691      2033175 :   return !get_overrun ();
    7692              : }
    7693              : 
    7694              : /* Most of the value contents of lang_type is streamed in
    7695              :    define_class.  */
    7696              : 
    7697              : void
    7698       351524 : trees_out::lang_type_vals (tree t)
    7699              : {
    7700       351524 :   const struct lang_type *lang = TYPE_LANG_SPECIFIC (t);
    7701              : #define WU(X) (u (X))
    7702              : #define WT(X) (tree_node (X))
    7703       351524 :   if (streaming_p ())
    7704       175739 :     WU (lang->align);
    7705              : #undef WU
    7706              : #undef WT
    7707       351524 : }
    7708              : 
    7709              : bool
    7710       151182 : trees_in::lang_type_vals (tree t)
    7711              : {
    7712       151182 :   struct lang_type *lang = TYPE_LANG_SPECIFIC (t);
    7713              : #define RU(X) ((X) = u ())
    7714              : #define RT(X) ((X) = tree_node ())
    7715       151182 :   RU (lang->align);
    7716              : #undef RU
    7717              : #undef RT
    7718       151182 :   return !get_overrun ();
    7719              : }
    7720              : 
    7721              : /* Write out the bools of T, including information about any
    7722              :    LANG_SPECIFIC information.  Including allocation of any lang
    7723              :    specific object.  */
    7724              : 
    7725              : void
    7726     16309192 : trees_out::tree_node_bools (tree t)
    7727              : {
    7728     16309192 :   gcc_checking_assert (streaming_p ());
    7729              : 
    7730              :   /* We should never stream a namespace.  */
    7731     16309192 :   gcc_checking_assert (TREE_CODE (t) != NAMESPACE_DECL
    7732              :                        || DECL_NAMESPACE_ALIAS (t));
    7733              : 
    7734     16309192 :   bits_out bits = stream_bits ();
    7735     16309192 :   core_bools (t, bits);
    7736              : 
    7737     16309192 :   switch (TREE_CODE_CLASS (TREE_CODE (t)))
    7738              :     {
    7739      3563662 :     case tcc_declaration:
    7740      3563662 :       {
    7741      3563662 :         bool specific = DECL_LANG_SPECIFIC (t) != NULL;
    7742      3563662 :         bits.b (specific);
    7743      3563662 :         if (specific && VAR_P (t))
    7744       327811 :           bits.b (DECL_DECOMPOSITION_P (t));
    7745      2227895 :         if (specific)
    7746      2227895 :           lang_decl_bools (t, bits);
    7747              :       }
    7748              :       break;
    7749              : 
    7750       663950 :     case tcc_type:
    7751       663950 :       {
    7752       663950 :         bool specific = (TYPE_MAIN_VARIANT (t) == t
    7753       663950 :                          && TYPE_LANG_SPECIFIC (t) != NULL);
    7754       663950 :         gcc_assert (TYPE_LANG_SPECIFIC (t)
    7755              :                     == TYPE_LANG_SPECIFIC (TYPE_MAIN_VARIANT (t)));
    7756              : 
    7757       663950 :         bits.b (specific);
    7758       663950 :         if (specific)
    7759       175739 :           lang_type_bools (t, bits);
    7760              :       }
    7761              :       break;
    7762              : 
    7763              :     default:
    7764              :       break;
    7765              :     }
    7766              : 
    7767     16309192 :   bits.bflush ();
    7768     16309192 : }
    7769              : 
    7770              : bool
    7771     15148036 : trees_in::tree_node_bools (tree t)
    7772              : {
    7773     15148036 :   bits_in bits = stream_bits ();
    7774     15148036 :   bool ok = core_bools (t, bits);
    7775              : 
    7776     15148036 :   if (ok)
    7777     15148036 :     switch (TREE_CODE_CLASS (TREE_CODE (t)))
    7778              :       {
    7779      3239399 :       case tcc_declaration:
    7780      3239399 :         if (bits.b ())
    7781              :           {
    7782      2033175 :             bool decomp = VAR_P (t) && bits.b ();
    7783              : 
    7784      2033175 :             ok = maybe_add_lang_decl_raw (t, decomp);
    7785      2033175 :             if (ok)
    7786      2033175 :               ok = lang_decl_bools (t, bits);
    7787              :           }
    7788              :         break;
    7789              : 
    7790       573142 :       case tcc_type:
    7791       573142 :         if (bits.b ())
    7792              :           {
    7793       151182 :             ok = maybe_add_lang_type_raw (t);
    7794       151182 :             if (ok)
    7795       151182 :               ok = lang_type_bools (t, bits);
    7796              :           }
    7797              :         break;
    7798              : 
    7799              :       default:
    7800              :         break;
    7801              :       }
    7802              : 
    7803     15148036 :   bits.bflush ();
    7804     15148036 :   if (!ok || get_overrun ())
    7805            0 :     return false;
    7806              : 
    7807              :   return true;
    7808     15148036 : }
    7809              : 
    7810              : 
    7811              : /* Write out the lang-specific vals of node T.  */
    7812              : 
    7813              : void
    7814     42182538 : trees_out::lang_vals (tree t)
    7815              : {
    7816     42182538 :   switch (TREE_CODE_CLASS (TREE_CODE (t)))
    7817              :     {
    7818      9329369 :     case tcc_declaration:
    7819      9329369 :       if (DECL_LANG_SPECIFIC (t))
    7820      5160126 :         lang_decl_vals (t);
    7821              :       break;
    7822              : 
    7823      2315408 :     case tcc_type:
    7824      2315408 :       if (TYPE_MAIN_VARIANT (t) == t && TYPE_LANG_SPECIFIC (t))
    7825       351524 :         lang_type_vals (t);
    7826              :       break;
    7827              : 
    7828              :     default:
    7829              :       break;
    7830              :     }
    7831     42182538 : }
    7832              : 
    7833              : bool
    7834     15126656 : trees_in::lang_vals (tree t)
    7835              : {
    7836     15126656 :   bool ok = true;
    7837              : 
    7838     15126656 :   switch (TREE_CODE_CLASS (TREE_CODE (t)))
    7839              :     {
    7840      3239399 :     case tcc_declaration:
    7841      3239399 :       if (DECL_LANG_SPECIFIC (t))
    7842      2033175 :         ok = lang_decl_vals (t);
    7843              :       break;
    7844              : 
    7845       551762 :     case tcc_type:
    7846       551762 :       if (TYPE_LANG_SPECIFIC (t))
    7847       151182 :         ok = lang_type_vals (t);
    7848              :       else
    7849       400580 :         TYPE_LANG_SPECIFIC (t) = TYPE_LANG_SPECIFIC (TYPE_MAIN_VARIANT (t));
    7850              :       break;
    7851              : 
    7852              :     default:
    7853              :       break;
    7854              :     }
    7855              : 
    7856     15126656 :   return ok;
    7857              : }
    7858              : 
    7859              : /* Write out the value fields of node T.  */
    7860              : 
    7861              : void
    7862     42182538 : trees_out::tree_node_vals (tree t)
    7863              : {
    7864     42182538 :   core_vals (t);
    7865     42182538 :   lang_vals (t);
    7866     42182538 : }
    7867              : 
    7868              : bool
    7869     15126656 : trees_in::tree_node_vals (tree t)
    7870              : {
    7871     15126656 :   bool ok = core_vals (t);
    7872     15126656 :   if (ok)
    7873     15126656 :     ok = lang_vals (t);
    7874              : 
    7875     15126656 :   return ok;
    7876              : }
    7877              : 
    7878              : 
    7879              : /* If T is a back reference, fixed reference or NULL, write out its
    7880              :    code and return WK_none.  Otherwise return WK_value if we must write
    7881              :    by value, or WK_normal otherwise.  */
    7882              : 
    7883              : walk_kind
    7884    277652241 : trees_out::ref_node (tree t)
    7885              : {
    7886    277652241 :   if (!t)
    7887              :     {
    7888    112103206 :       if (streaming_p ())
    7889              :         {
    7890              :           /* NULL_TREE -> tt_null.  */
    7891     42746674 :           null_count++;
    7892     42746674 :           i (tt_null);
    7893              :         }
    7894    112103206 :       return WK_none;
    7895              :     }
    7896              : 
    7897    165549035 :   if (!TREE_VISITED (t))
    7898              :     return WK_normal;
    7899              : 
    7900              :   /* An already-visited tree.  It must be in the map.  */
    7901     96842503 :   int val = get_tag (t);
    7902              : 
    7903     96842503 :   if (val == tag_value)
    7904              :     /* An entry we should walk into.  */
    7905              :     return WK_value;
    7906              : 
    7907     95318396 :   const char *kind;
    7908              : 
    7909     95318396 :   if (val <= tag_backref)
    7910              :     {
    7911              :       /* Back reference -> -ve number  */
    7912     72548176 :       if (streaming_p ())
    7913     34357496 :         i (val);
    7914              :       kind = "backref";
    7915              :     }
    7916     22770220 :   else if (val >= tag_fixed)
    7917              :     {
    7918              :       /* Fixed reference -> tt_fixed */
    7919     22770220 :       val -= tag_fixed;
    7920     22770220 :       if (streaming_p ())
    7921      8061041 :         i (tt_fixed), u (val);
    7922              :       kind = "fixed";
    7923              :     }
    7924              : 
    7925     95318396 :   if (streaming_p ())
    7926              :     {
    7927     42418537 :       back_ref_count++;
    7928     42418537 :       dump (dumper::TREE)
    7929        14184 :         && dump ("Wrote %s:%d %C:%N%S", kind, val, TREE_CODE (t), t, t);
    7930              :     }
    7931              :   return WK_none;
    7932              : }
    7933              : 
    7934              : tree
    7935     31769089 : trees_in::back_ref (int tag)
    7936              : {
    7937     31769089 :   tree res = NULL_TREE;
    7938              : 
    7939     31769089 :   if (tag < 0 && unsigned (~tag) < back_refs.length ())
    7940     31769089 :     res = back_refs[~tag];
    7941              : 
    7942     31769089 :   if (!res
    7943              :       /* Checking TREE_CODE is a dereference, so we know this is not a
    7944              :          wild pointer.  Checking the code provides evidence we've not
    7945              :          corrupted something.  */
    7946     31769089 :       || TREE_CODE (res) >= MAX_TREE_CODES)
    7947            0 :     set_overrun ();
    7948              :   else
    7949     31783629 :     dump (dumper::TREE) && dump ("Read backref:%d found %C:%N%S", tag,
    7950              :                                  TREE_CODE (res), res, res);
    7951     31769089 :   return res;
    7952              : }
    7953              : 
    7954              : unsigned
    7955      3834030 : trees_out::add_indirect_tpl_parms (tree parms)
    7956              : {
    7957      3834030 :   unsigned len = 0;
    7958      7199619 :   for (; parms; parms = TREE_CHAIN (parms), len++)
    7959              :     {
    7960      4056650 :       if (TREE_VISITED (parms))
    7961              :         break;
    7962              : 
    7963      3365589 :       int tag = insert (parms);
    7964      3365589 :       if (streaming_p ())
    7965      3365916 :         dump (dumper::TREE)
    7966          153 :           && dump ("Indirect:%d template's parameter %u %C:%N",
    7967          153 :                    tag, len, TREE_CODE (parms), parms);
    7968              :     }
    7969              : 
    7970      3834030 :   if (streaming_p ())
    7971       650424 :     u (len);
    7972              : 
    7973      3834030 :   return len;
    7974              : }
    7975              : 
    7976              : unsigned
    7977       536090 : trees_in::add_indirect_tpl_parms (tree parms)
    7978              : {
    7979       536090 :   unsigned len = u ();
    7980       961732 :   for (unsigned ix = 0; ix != len; parms = TREE_CHAIN (parms), ix++)
    7981              :     {
    7982       425642 :       int tag = insert (parms);
    7983       426107 :       dump (dumper::TREE)
    7984          159 :         && dump ("Indirect:%d template's parameter %u %C:%N",
    7985          159 :                  tag, ix, TREE_CODE (parms), parms);
    7986              :     }
    7987              : 
    7988       536090 :   return len;
    7989              : }
    7990              : 
    7991              : /* We've just found DECL by name.  Insert nodes that come with it, but
    7992              :    cannot be found by name, so we'll not accidentally walk into them.  */
    7993              : 
    7994              : void
    7995      8594699 : trees_out::add_indirects (tree decl)
    7996              : {
    7997      8594699 :   unsigned count = 0;
    7998              : 
    7999              :   // FIXME:OPTIMIZATION We'll eventually want default fn parms of
    8000              :   // templates and perhaps default template parms too.  The former can
    8001              :   // be referenced from instantiations (as they are lazily
    8002              :   // instantiated).  Also (deferred?) exception specifications of
    8003              :   // templates.  See the note about PARM_DECLs in trees_out::decl_node.
    8004      8594699 :   tree inner = decl;
    8005      8594699 :   if (TREE_CODE (decl) == TEMPLATE_DECL)
    8006              :     {
    8007      3834030 :       count += add_indirect_tpl_parms (DECL_TEMPLATE_PARMS (decl));
    8008              : 
    8009      3834030 :       inner = DECL_TEMPLATE_RESULT (decl);
    8010      3834030 :       int tag = insert (inner);
    8011      3834030 :       if (streaming_p ())
    8012       650424 :         dump (dumper::TREE)
    8013          219 :           && dump ("Indirect:%d template's result %C:%N",
    8014          219 :                    tag, TREE_CODE (inner), inner);
    8015      3834030 :       count++;
    8016              :     }
    8017              : 
    8018      8594699 :   if (TREE_CODE (inner) == TYPE_DECL)
    8019              :     {
    8020              :       /* Make sure the type is in the map too.  Otherwise we get
    8021              :          different RECORD_TYPEs for the same type, and things go
    8022              :          south.  */
    8023      4581937 :       tree type = TREE_TYPE (inner);
    8024      4581937 :       gcc_checking_assert (DECL_ORIGINAL_TYPE (inner)
    8025              :                            || TYPE_NAME (type) == inner);
    8026      4581937 :       int tag = insert (type);
    8027      4581937 :       if (streaming_p ())
    8028       517480 :         dump (dumper::TREE) && dump ("Indirect:%d decl's type %C:%N", tag,
    8029          362 :                                      TREE_CODE (type), type);
    8030      4581937 :       count++;
    8031              :     }
    8032              : 
    8033      8594699 :   if (streaming_p ())
    8034              :     {
    8035      1329738 :       u (count);
    8036      1330267 :       dump (dumper::TREE) && dump ("Inserted %u indirects", count);
    8037              :     }
    8038      8594699 : }
    8039              : 
    8040              : bool
    8041      1071698 : trees_in::add_indirects (tree decl)
    8042              : {
    8043      1071698 :   unsigned count = 0;
    8044              : 
    8045      1071698 :   tree inner = decl;
    8046      1071698 :   if (TREE_CODE (inner) == TEMPLATE_DECL)
    8047              :     {
    8048       536090 :       count += add_indirect_tpl_parms (DECL_TEMPLATE_PARMS (decl));
    8049              : 
    8050       536090 :       inner = DECL_TEMPLATE_RESULT (decl);
    8051       536090 :       int tag = insert (inner);
    8052       536090 :       dump (dumper::TREE)
    8053          228 :         && dump ("Indirect:%d templates's result %C:%N", tag,
    8054          228 :                  TREE_CODE (inner), inner);
    8055       536090 :       count++;
    8056              :     }
    8057              : 
    8058      1071698 :   if (TREE_CODE (inner) == TYPE_DECL)
    8059              :     {
    8060       407096 :       tree type = TREE_TYPE (inner);
    8061       407096 :       gcc_checking_assert (DECL_ORIGINAL_TYPE (inner)
    8062              :                            || TYPE_NAME (type) == inner);
    8063       407096 :       int tag = insert (type);
    8064       407096 :       dump (dumper::TREE)
    8065          362 :         && dump ("Indirect:%d decl's type %C:%N", tag, TREE_CODE (type), type);
    8066       407096 :       count++;
    8067              :     }
    8068              : 
    8069      1072303 :   dump (dumper::TREE) && dump ("Inserted %u indirects", count);
    8070      1071698 :   return count == u ();
    8071              : }
    8072              : 
    8073              : /* Stream a template parameter.  There are 4.5 kinds of parameter:
    8074              :    a) Template - TEMPLATE_DECL->TYPE_DECL->TEMPLATE_TEMPLATE_PARM
    8075              :         TEMPLATE_TYPE_PARM_INDEX TPI
    8076              :    b) Type - TYPE_DECL->TEMPLATE_TYPE_PARM TEMPLATE_TYPE_PARM_INDEX TPI
    8077              :    c.1) NonTYPE - PARM_DECL DECL_INITIAL TPI We meet this first
    8078              :    c.2) NonTYPE - CONST_DECL DECL_INITIAL Same TPI
    8079              :    d) BoundTemplate - TYPE_DECL->BOUND_TEMPLATE_TEMPLATE_PARM
    8080              :        TEMPLATE_TYPE_PARM_INDEX->TPI
    8081              :        TEMPLATE_TEMPLATE_PARM_INFO->TEMPLATE_INFO
    8082              : 
    8083              :    All of these point to a TEMPLATE_PARM_INDEX, and #B also has a TEMPLATE_INFO
    8084              : */
    8085              : 
    8086              : void
    8087      2119303 : trees_out::tpl_parm_value (tree parm)
    8088              : {
    8089      2119303 :   gcc_checking_assert (DECL_P (parm) && DECL_TEMPLATE_PARM_P (parm));
    8090              : 
    8091      2119303 :   int parm_tag = insert (parm);
    8092      2119303 :   if (streaming_p ())
    8093              :     {
    8094       477393 :       i (tt_tpl_parm);
    8095       477393 :       dump (dumper::TREE) && dump ("Writing template parm:%d %C:%N",
    8096          114 :                                    parm_tag, TREE_CODE (parm), parm);
    8097       477393 :       start (parm);
    8098       477393 :       tree_node_bools (parm);
    8099              :     }
    8100              : 
    8101      2119303 :   tree inner = parm;
    8102      2119303 :   if (TREE_CODE (inner) == TEMPLATE_DECL)
    8103              :     {
    8104         6048 :       inner = DECL_TEMPLATE_RESULT (inner);
    8105         6048 :       int inner_tag = insert (inner);
    8106         6048 :       if (streaming_p ())
    8107              :         {
    8108         1590 :           dump (dumper::TREE) && dump ("Writing inner template parm:%d %C:%N",
    8109            0 :                                        inner_tag, TREE_CODE (inner), inner);
    8110         1590 :           start (inner);
    8111         1590 :           tree_node_bools (inner);
    8112              :         }
    8113              :     }
    8114              : 
    8115      2119303 :   tree type = NULL_TREE;
    8116      2119303 :   if (TREE_CODE (inner) == TYPE_DECL)
    8117              :     {
    8118      1904314 :       type = TREE_TYPE (inner);
    8119      1904314 :       int type_tag = insert (type);
    8120      1904314 :       if (streaming_p ())
    8121              :         {
    8122       431013 :           dump (dumper::TREE) && dump ("Writing template parm type:%d %C:%N",
    8123          108 :                                        type_tag, TREE_CODE (type), type);
    8124       431013 :           start (type);
    8125       431013 :           tree_node_bools (type);
    8126              :         }
    8127              :     }
    8128              : 
    8129      2119303 :   if (inner != parm)
    8130              :     {
    8131              :       /* This is a template-template parameter.  */
    8132         6048 :       unsigned tpl_levels = 0;
    8133         6048 :       tpl_header (parm, &tpl_levels);
    8134         6048 :       tpl_parms_fini (parm, tpl_levels);
    8135              :     }
    8136              : 
    8137      2119303 :   tree_node_vals (parm);
    8138      2119303 :   if (inner != parm)
    8139         6048 :     tree_node_vals (inner);
    8140      2119303 :   if (type)
    8141              :     {
    8142      1904314 :       tree_node_vals (type);
    8143      1904314 :       if (DECL_NAME (inner) == auto_identifier
    8144      1904314 :           || DECL_NAME (inner) == decltype_auto_identifier)
    8145              :         {
    8146              :           /* Placeholder auto.  */
    8147        86329 :           tree_node (DECL_INITIAL (inner));
    8148        86329 :           tree_node (DECL_SIZE_UNIT (inner));
    8149              :         }
    8150              :     }
    8151              : 
    8152      2119303 :   if (streaming_p ())
    8153       477393 :     dump (dumper::TREE) && dump ("Wrote template parm:%d %C:%N",
    8154          114 :                                  parm_tag, TREE_CODE (parm), parm);
    8155      2119303 : }
    8156              : 
    8157              : tree
    8158       419910 : trees_in::tpl_parm_value ()
    8159              : {
    8160       419910 :   tree parm = start ();
    8161       419910 :   if (!parm || !tree_node_bools (parm))
    8162            0 :     return NULL_TREE;
    8163              : 
    8164       419910 :   int parm_tag = insert (parm);
    8165       419910 :   dump (dumper::TREE) && dump ("Reading template parm:%d %C:%N",
    8166          198 :                                parm_tag, TREE_CODE (parm), parm);
    8167              : 
    8168       419910 :   tree inner = parm;
    8169       419910 :   if (TREE_CODE (inner) == TEMPLATE_DECL)
    8170              :     {
    8171         1219 :       inner = start ();
    8172         1219 :       if (!inner || !tree_node_bools (inner))
    8173            0 :         return NULL_TREE;
    8174         1219 :       int inner_tag = insert (inner);
    8175         1219 :       dump (dumper::TREE) && dump ("Reading inner template parm:%d %C:%N",
    8176            0 :                                    inner_tag, TREE_CODE (inner), inner);
    8177         1219 :       DECL_TEMPLATE_RESULT (parm) = inner;
    8178              :     }
    8179              : 
    8180       419910 :   tree type = NULL_TREE;
    8181       419910 :   if (TREE_CODE (inner) == TYPE_DECL)
    8182              :     {
    8183       377584 :       type = start ();
    8184       377584 :       if (!type || !tree_node_bools (type))
    8185            0 :         return NULL_TREE;
    8186       377584 :       int type_tag = insert (type);
    8187       377584 :       dump (dumper::TREE) && dump ("Reading template parm type:%d %C:%N",
    8188          120 :                                    type_tag, TREE_CODE (type), type);
    8189              : 
    8190       377584 :       TREE_TYPE (inner) = TREE_TYPE (parm) = type;
    8191       377584 :       TYPE_NAME (type) = parm;
    8192              :     }
    8193              : 
    8194       419910 :   if (inner != parm)
    8195              :     {
    8196              :       /* A template template parameter.  */
    8197         1219 :       unsigned tpl_levels = 0;
    8198         1219 :       tpl_header (parm, &tpl_levels);
    8199         1219 :       tpl_parms_fini (parm, tpl_levels);
    8200              :     }
    8201              : 
    8202       419910 :   tree_node_vals (parm);
    8203       419910 :   if (inner != parm)
    8204         1219 :     tree_node_vals (inner);
    8205       419910 :   if (type)
    8206              :     {
    8207       377584 :       tree_node_vals (type);
    8208       377584 :       if (DECL_NAME (inner) == auto_identifier
    8209       377584 :           || DECL_NAME (inner) == decltype_auto_identifier)
    8210              :         {
    8211              :           /* Placeholder auto.  */
    8212        32532 :           DECL_INITIAL (inner) = tree_node ();
    8213        32532 :           DECL_SIZE_UNIT (inner) = tree_node ();
    8214              :         }
    8215       377584 :       if (TYPE_CANONICAL (type))
    8216              :         {
    8217       377584 :           gcc_checking_assert (TYPE_CANONICAL (type) == type);
    8218       377584 :           TYPE_CANONICAL (type) = canonical_type_parameter (type);
    8219              :         }
    8220              :     }
    8221              : 
    8222       419910 :   dump (dumper::TREE) && dump ("Read template parm:%d %C:%N",
    8223          198 :                                parm_tag, TREE_CODE (parm), parm);
    8224              : 
    8225              :   return parm;
    8226              : }
    8227              : 
    8228              : void
    8229      1340504 : trees_out::install_entity (tree decl, depset *dep)
    8230              : {
    8231      1340504 :   gcc_checking_assert (streaming_p ());
    8232              : 
    8233              :   /* Write the entity index, so we can insert it as soon as we
    8234              :      know this is new.  */
    8235      1340504 :   u (dep ? dep->cluster + 1 : 0);
    8236      1340504 :   if (CHECKING_P && dep)
    8237              :     {
    8238              :       /* Add it to the entity map, such that we can tell it is
    8239              :          part of us.  */
    8240       987827 :       bool existed;
    8241       987827 :       unsigned *slot = &entity_map->get_or_insert
    8242       987827 :         (DECL_UID (decl), &existed);
    8243       987827 :       if (existed)
    8244              :         /* If it existed, it should match.  */
    8245         1617 :         gcc_checking_assert (decl == (*entity_ary)[*slot]);
    8246       987827 :       *slot = ~dep->cluster;
    8247              :     }
    8248      1340504 : }
    8249              : 
    8250              : bool
    8251      1193383 : trees_in::install_entity (tree decl)
    8252              : {
    8253      1193383 :   unsigned entity_index = u ();
    8254      1193383 :   if (!entity_index)
    8255              :     return false;
    8256              : 
    8257       869662 :   if (entity_index > state->entity_num)
    8258              :     {
    8259            0 :       set_overrun ();
    8260            0 :       return false;
    8261              :     }
    8262              : 
    8263              :   /* Insert the real decl into the entity ary.  */
    8264       869662 :   unsigned ident = state->entity_lwm + entity_index - 1;
    8265       869662 :   (*entity_ary)[ident] = decl;
    8266              : 
    8267              :   /* And into the entity map, if it's not already there.  */
    8268       869662 :   tree not_tmpl = STRIP_TEMPLATE (decl);
    8269       869662 :   if (!DECL_LANG_SPECIFIC (not_tmpl)
    8270      1643465 :       || !DECL_MODULE_ENTITY_P (not_tmpl))
    8271              :     {
    8272              :       /* We don't want to use retrofit_lang_decl directly so that we aren't
    8273              :          affected by the language state when we load in.  */
    8274       868537 :       if (!DECL_LANG_SPECIFIC (not_tmpl))
    8275              :         {
    8276        95859 :           maybe_add_lang_decl_raw (not_tmpl, false);
    8277        95859 :           SET_DECL_LANGUAGE (not_tmpl, lang_cplusplus);
    8278              :         }
    8279       868537 :       DECL_MODULE_ENTITY_P (not_tmpl) = true;
    8280              : 
    8281              :       /* Insert into the entity hash (it cannot already be there).  */
    8282       868537 :       bool existed;
    8283       868537 :       unsigned &slot = entity_map->get_or_insert (DECL_UID (decl), &existed);
    8284       868537 :       gcc_checking_assert (!existed);
    8285       868537 :       slot = ident;
    8286              :     }
    8287              :   else
    8288              :     {
    8289         1125 :       unsigned *slot = entity_map->get (DECL_UID (decl));
    8290              : 
    8291              :       /* The entity must be in the entity map already.  However, DECL may
    8292              :          be the DECL_TEMPLATE_RESULT of an existing partial specialisation
    8293              :          if we matched it while streaming another instantiation; in this
    8294              :          case we already registered that TEMPLATE_DECL.  */
    8295         1125 :       if (!slot)
    8296              :         {
    8297            9 :           tree type = TREE_TYPE (decl);
    8298            9 :           gcc_checking_assert (TREE_CODE (decl) == TYPE_DECL
    8299              :                                && CLASS_TYPE_P (type)
    8300              :                                && CLASSTYPE_TEMPLATE_SPECIALIZATION (type));
    8301            9 :           slot = entity_map->get (DECL_UID (CLASSTYPE_TI_TEMPLATE (type)));
    8302              :         }
    8303            9 :       gcc_checking_assert (slot);
    8304              : 
    8305         1125 :       if (state->is_partition ())
    8306              :         {
    8307              :           /* The decl is already in the entity map, but we see it again now
    8308              :              from a partition: we want to overwrite if the original decl
    8309              :              wasn't also from a (possibly different) partition.  Otherwise,
    8310              :              for things like template instantiations, make_dependency might
    8311              :              not realise that this is also provided from a partition and
    8312              :              should be considered part of this module (and thus always
    8313              :              emitted into the primary interface's CMI).  */
    8314          420 :           module_state *imp = import_entity_module (*slot);
    8315          420 :           if (!imp->is_partition ())
    8316          285 :             *slot = ident;
    8317              :         }
    8318              :     }
    8319              : 
    8320              :   return true;
    8321              : }
    8322              : 
    8323              : static bool has_definition (tree decl);
    8324              : 
    8325              : /* DECL is a decl node that must be written by value.  DEP is the
    8326              :    decl's depset.  */
    8327              : 
    8328              : void
    8329      3685688 : trees_out::decl_value (tree decl, depset *dep)
    8330              : {
    8331              :   /* We should not be writing clones or template parms.  */
    8332      3685688 :   gcc_checking_assert (DECL_P (decl)
    8333              :                        && !DECL_CLONED_FUNCTION_P (decl)
    8334              :                        && !DECL_TEMPLATE_PARM_P (decl));
    8335              : 
    8336              :   /* We should never be writing non-typedef ptrmemfuncs by value.  */
    8337      3685688 :   gcc_checking_assert (TREE_CODE (decl) != TYPE_DECL
    8338              :                        || DECL_ORIGINAL_TYPE (decl)
    8339              :                        || !TYPE_PTRMEMFUNC_P (TREE_TYPE (decl)));
    8340              : 
    8341              :   /* There's no need to walk any of the contents of a known TU-local entity,
    8342              :      since importers should never see any of it regardless.  But make sure we
    8343              :      at least note its location so importers can use it for diagnostics.  */
    8344      3685688 :   if (dep && dep->is_tu_local ())
    8345              :     {
    8346          416 :       gcc_checking_assert (is_initial_scan ());
    8347          416 :       insert (decl, WK_value);
    8348          416 :       state->note_location (DECL_SOURCE_LOCATION (decl));
    8349          416 :       return;
    8350              :     }
    8351              : 
    8352      3685272 :   merge_kind mk = get_merge_kind (decl, dep);
    8353              : 
    8354      3685272 :   if (CHECKING_P)
    8355              :     {
    8356              :       /* Never start in the middle of a template.  */
    8357      3685272 :       int use_tpl = -1;
    8358      3685272 :       if (tree ti = node_template_info (decl, use_tpl))
    8359      1271356 :         gcc_checking_assert (TREE_CODE (TI_TEMPLATE (ti)) == OVERLOAD
    8360              :                              || TREE_CODE (TI_TEMPLATE (ti)) == FIELD_DECL
    8361              :                              || (DECL_TEMPLATE_RESULT (TI_TEMPLATE (ti))
    8362              :                                  != decl));
    8363              :     }
    8364              : 
    8365      3685272 :   if (streaming_p ())
    8366              :     {
    8367              :       /* A new node -> tt_decl.  */
    8368      1340504 :       decl_val_count++;
    8369      1340504 :       i (tt_decl);
    8370      1340504 :       u (mk);
    8371      1340504 :       start (decl);
    8372              : 
    8373      1340504 :       if (mk != MK_unique)
    8374              :         {
    8375      1123551 :           bits_out bits = stream_bits ();
    8376      1123551 :           if (!(mk & MK_template_mask) && !state->is_header ())
    8377              :             {
    8378              :               /* Tell the importer whether this is a global module entity,
    8379              :                  or a module entity.  */
    8380       187089 :               tree o = get_originating_module_decl (decl);
    8381       187089 :               bool is_attached = false;
    8382              : 
    8383       187089 :               tree not_tmpl = STRIP_TEMPLATE (o);
    8384       187089 :               if (DECL_LANG_SPECIFIC (not_tmpl)
    8385       300168 :                   && DECL_MODULE_ATTACH_P (not_tmpl))
    8386              :                 is_attached = true;
    8387              : 
    8388       187089 :               bits.b (is_attached);
    8389              :             }
    8390      1123551 :           bits.b (dep && dep->has_defn ());
    8391      1123551 :         }
    8392      1340504 :       tree_node_bools (decl);
    8393              :     }
    8394              : 
    8395      3685272 :   int tag = insert (decl, WK_value);
    8396      3685272 :   if (streaming_p ())
    8397      1340504 :     dump (dumper::TREE)
    8398          683 :       && dump ("Writing %s:%d %C:%N%S", merge_kind_name[mk], tag,
    8399          683 :                TREE_CODE (decl), decl, decl);
    8400              : 
    8401      3685272 :   tree inner = decl;
    8402      3685272 :   int inner_tag = 0;
    8403      3685272 :   if (TREE_CODE (decl) == TEMPLATE_DECL)
    8404              :     {
    8405      1062172 :       inner = DECL_TEMPLATE_RESULT (decl);
    8406      1062172 :       inner_tag = insert (inner, WK_value);
    8407              : 
    8408              :       /* On stream-in we assume that a template and its result will
    8409              :          have the same type.  */
    8410      1062172 :       gcc_checking_assert (TREE_TYPE (decl) == TREE_TYPE (inner));
    8411              : 
    8412      1062172 :       if (streaming_p ())
    8413              :         {
    8414       354042 :           int code = TREE_CODE (inner);
    8415       354042 :           u (code);
    8416       354042 :           start (inner, true);
    8417       354042 :           tree_node_bools (inner);
    8418       354042 :           dump (dumper::TREE)
    8419          132 :             && dump ("Writing %s:%d %C:%N%S", merge_kind_name[mk], inner_tag,
    8420          132 :                      TREE_CODE (inner), inner, inner);
    8421              :         }
    8422              :     }
    8423              : 
    8424      3685272 :   tree type = NULL_TREE;
    8425      3685272 :   int type_tag = 0;
    8426      3685272 :   tree stub_decl = NULL_TREE;
    8427      3685272 :   int stub_tag = 0;
    8428      3685272 :   if (TREE_CODE (inner) == TYPE_DECL)
    8429              :     {
    8430      1345126 :       type = TREE_TYPE (inner);
    8431      1345126 :       bool has_type = (type == TYPE_MAIN_VARIANT (type)
    8432      1345126 :                        && TYPE_NAME (type) == inner);
    8433              : 
    8434      1345126 :       if (streaming_p ())
    8435       454000 :         u (has_type ? TREE_CODE (type) : 0);
    8436              : 
    8437      1345126 :       if (has_type)
    8438              :         {
    8439       616562 :           type_tag = insert (type, WK_value);
    8440       616562 :           if (streaming_p ())
    8441              :             {
    8442       205496 :               start (type, true);
    8443       205496 :               tree_node_bools (type);
    8444       205496 :               dump (dumper::TREE)
    8445          155 :                 && dump ("Writing type:%d %C:%N", type_tag,
    8446          155 :                          TREE_CODE (type), type);
    8447              :             }
    8448              : 
    8449       616562 :           stub_decl = TYPE_STUB_DECL (type);
    8450       616562 :           bool has_stub = inner != stub_decl;
    8451       616562 :           if (streaming_p ())
    8452       205496 :             u (has_stub ? TREE_CODE (stub_decl) : 0);
    8453       616562 :           if (has_stub)
    8454              :             {
    8455         2289 :               stub_tag = insert (stub_decl);
    8456         2289 :               if (streaming_p ())
    8457              :                 {
    8458          762 :                   start (stub_decl, true);
    8459          762 :                   tree_node_bools (stub_decl);
    8460          762 :                   dump (dumper::TREE)
    8461            0 :                     && dump ("Writing stub_decl:%d %C:%N", stub_tag,
    8462            0 :                              TREE_CODE (stub_decl), stub_decl);
    8463              :                 }
    8464              :             }
    8465              :           else
    8466              :             stub_decl = NULL_TREE;
    8467              :         }
    8468              :       else
    8469              :         /* Regular typedef.  */
    8470              :         type = NULL_TREE;
    8471              :     }
    8472              : 
    8473              :   /* Stream the container, we want it correctly canonicalized before
    8474              :      we start emitting keys for this decl.  */
    8475      3685272 :   tree container = decl_container (decl);
    8476      3685272 :   unsigned tpl_levels = 0;
    8477              : 
    8478              :   /* Also tell the importer whether this is a temploid friend attached
    8479              :      to a different module (which has implications for merging), so that
    8480              :      importers can reconstruct this information on stream-in.  */
    8481      3685272 :   if (TREE_CODE (inner) == FUNCTION_DECL || TREE_CODE (inner) == TYPE_DECL)
    8482              :     {
    8483      2823366 :       tree* temploid_friend_slot = imported_temploid_friends->get (decl);
    8484      2823366 :       gcc_checking_assert (!temploid_friend_slot || *temploid_friend_slot);
    8485      2823366 :       tree_node (temploid_friend_slot ? *temploid_friend_slot : NULL_TREE);
    8486              :     }
    8487              : 
    8488      3685272 :   {
    8489      3685272 :     auto wmk = make_temp_override (dep_hash->writing_merge_key, true);
    8490      3685272 :     if (decl != inner)
    8491      1062172 :       tpl_header (decl, &tpl_levels);
    8492      3685272 :     if (TREE_CODE (inner) == FUNCTION_DECL)
    8493      1478240 :       fn_parms_init (inner);
    8494              : 
    8495              :     /* Now write out the merging information, and then really
    8496              :        install the tag values.  */
    8497      3685272 :     key_mergeable (tag, mk, decl, inner, container, dep);
    8498              : 
    8499      3685272 :     if (streaming_p ())
    8500      3689171 :       dump (dumper::MERGE)
    8501         1101 :         && dump ("Wrote:%d's %s merge key %C:%N", tag,
    8502         1101 :                  merge_kind_name[mk], TREE_CODE (decl), decl);
    8503      3685272 :   }
    8504              : 
    8505      3685272 :   if (TREE_CODE (inner) == FUNCTION_DECL)
    8506      3685272 :     fn_parms_fini (inner);
    8507              : 
    8508      3685272 :   if (!is_key_order ())
    8509      2695709 :     tree_node_vals (decl);
    8510              : 
    8511      3685272 :   if (inner_tag)
    8512              :     {
    8513      1062172 :       if (!is_key_order ())
    8514       708130 :         tree_node_vals (inner);
    8515      1062172 :       tpl_parms_fini (decl, tpl_levels);
    8516              :     }
    8517              : 
    8518      3685272 :   if (type && !is_key_order ())
    8519              :     {
    8520       411066 :       tree_node_vals (type);
    8521       411066 :       if (stub_decl)
    8522         1527 :         tree_node_vals (stub_decl);
    8523              :     }
    8524              : 
    8525      3685272 :   if (!is_key_order ())
    8526              :     {
    8527      2695709 :       if (mk & MK_template_mask
    8528      1887107 :           || mk == MK_partial
    8529      1887107 :           || mk == MK_friend_spec)
    8530              :         {
    8531        33316 :           if (mk != MK_partial)
    8532              :             {
    8533              :               // FIXME: We should make use of the merge-key by
    8534              :               // exposing it outside of key_mergeable.  But this gets
    8535              :               // the job done.
    8536       808602 :               auto *entry = reinterpret_cast <spec_entry *> (dep->deps[0]);
    8537              : 
    8538       808602 :               if (streaming_p ())
    8539       404301 :                 u (get_mergeable_specialization_flags (mk & MK_tmpl_decl_mask,
    8540              :                                                        entry->tmpl, decl));
    8541       808602 :               tree_node (entry->tmpl);
    8542       808602 :               tree_node (entry->args);
    8543              :             }
    8544              :           else
    8545              :             {
    8546        33316 :               tree ti = get_template_info (inner);
    8547        33316 :               tree_node (TI_TEMPLATE (ti));
    8548        33316 :               tree_node (TI_ARGS (ti));
    8549              :             }
    8550              :         }
    8551      2695709 :       tree_node (get_constraints (decl));
    8552              :     }
    8553              : 
    8554      3685272 :   if (streaming_p ())
    8555              :     {
    8556              :       /* Do not stray outside this section.  */
    8557      1340504 :       gcc_checking_assert (!dep || dep->section == dep_hash->section);
    8558              : 
    8559              :       /* Write the entity index, so we can insert it as soon as we
    8560              :          know this is new.  */
    8561      1340504 :       install_entity (decl, dep);
    8562              :     }
    8563              : 
    8564      3685272 :   if (DECL_LANG_SPECIFIC (inner)
    8565      3240941 :       && DECL_MODULE_KEYED_DECLS_P (inner)
    8566      3686283 :       && streaming_p ())
    8567              :     {
    8568              :       /* Stream the keyed entities.  There may be keyed entities that we
    8569              :          choose not to stream, such as a lambda in a non-inline variable's
    8570              :          initializer, so don't build dependencies for them here; any deps
    8571              :          we need should be acquired during write_definition (possibly
    8572              :          indirectly).  */
    8573          330 :       auto *attach_vec = keyed_table->get (inner);
    8574          330 :       unsigned num = attach_vec->length ();
    8575          330 :       u (num);
    8576          694 :       for (unsigned ix = 0; ix != num; ix++)
    8577              :         {
    8578          364 :           tree attached = (*attach_vec)[ix];
    8579          364 :           if (attached)
    8580              :             {
    8581          364 :               tree ti = TYPE_TEMPLATE_INFO (TREE_TYPE (attached));
    8582          364 :               if (!dep_hash->find_dependency (attached)
    8583          364 :                   && !(ti && dep_hash->find_dependency (TI_TEMPLATE (ti))))
    8584              :                 attached = NULL_TREE;
    8585              :             }
    8586              : 
    8587          364 :           tree_node (attached);
    8588          406 :           dump (dumper::MERGE)
    8589           30 :             && dump ("Written %d[%u] attached decl %N", tag, ix, attached);
    8590              :         }
    8591              :     }
    8592              : 
    8593      3685272 :   bool is_typedef = false;
    8594      3685272 :   if (!type && TREE_CODE (inner) == TYPE_DECL)
    8595              :     {
    8596       728564 :       tree t = TREE_TYPE (inner);
    8597       728564 :       unsigned tdef_flags = 0;
    8598       728564 :       if (DECL_ORIGINAL_TYPE (inner)
    8599       728564 :           && TYPE_NAME (TREE_TYPE (inner)) == inner)
    8600              :         {
    8601       728564 :           tdef_flags |= 1;
    8602       728564 :           if (TYPE_STRUCTURAL_EQUALITY_P (t)
    8603       158309 :               && TYPE_DEPENDENT_P_VALID (t)
    8604       879733 :               && TYPE_DEPENDENT_P (t))
    8605              :             tdef_flags |= 2;
    8606              :         }
    8607       728564 :       if (streaming_p ())
    8608       248504 :         u (tdef_flags);
    8609              : 
    8610       728564 :       if (tdef_flags & 1)
    8611              :         {
    8612              :           /* A typedef type.  */
    8613       728564 :           int type_tag = insert (t);
    8614       728564 :           if (streaming_p ())
    8615       248504 :             dump (dumper::TREE)
    8616          206 :               && dump ("Cloned:%d %s %C:%N", type_tag,
    8617              :                        tdef_flags & 2 ? "depalias" : "typedef",
    8618          206 :                        TREE_CODE (t), t);
    8619              : 
    8620              :           is_typedef = true;
    8621              :         }
    8622              :     }
    8623              : 
    8624      3685272 :   if (streaming_p () && DECL_MAYBE_IN_CHARGE_CDTOR_P (decl))
    8625              :     {
    8626       108172 :       bool cloned_p
    8627       108172 :         = (DECL_CHAIN (decl) && DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl)));
    8628        76174 :       bool needs_vtt_parm_p
    8629        76174 :         = (cloned_p && CLASSTYPE_VBASECLASSES (DECL_CONTEXT (decl)));
    8630        76174 :       bool omit_inherited_parms_p
    8631        76174 :         = (cloned_p && DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl)
    8632        60731 :            && base_ctor_omit_inherited_parms (decl));
    8633       108172 :       unsigned flags = (int (cloned_p) << 0
    8634       108172 :                         | int (needs_vtt_parm_p) << 1
    8635       108172 :                         | int (omit_inherited_parms_p) << 2);
    8636       108172 :       u (flags);
    8637       108251 :       dump (dumper::TREE) && dump ("CDTOR %N is %scloned",
    8638              :                                    decl, cloned_p ? "" : "not ");
    8639              :     }
    8640              : 
    8641      3685272 :   if (streaming_p () && VAR_P (decl) && CP_DECL_THREAD_LOCAL_P (decl))
    8642          166 :     u (decl_tls_model (decl));
    8643              : 
    8644      3685272 :   if (streaming_p ())
    8645      1340504 :     dump (dumper::TREE) && dump ("Written decl:%d %C:%N", tag,
    8646          683 :                                  TREE_CODE (decl), decl);
    8647              : 
    8648      3685272 :   if (NAMESPACE_SCOPE_P (inner))
    8649      2147774 :     gcc_checking_assert (!dep == (VAR_OR_FUNCTION_DECL_P (inner)
    8650              :                                   && DECL_LOCAL_DECL_P (inner)));
    8651      2611249 :   else if ((TREE_CODE (inner) == TYPE_DECL
    8652       719812 :             && !is_typedef
    8653       137903 :             && TYPE_NAME (TREE_TYPE (inner)) == inner)
    8654      3193158 :            || TREE_CODE (inner) == FUNCTION_DECL)
    8655              :     {
    8656      1236608 :       bool write_defn = !dep && has_definition (decl);
    8657      1236608 :       if (streaming_p ())
    8658       412377 :         u (write_defn);
    8659      1236608 :       if (write_defn)
    8660            6 :         write_definition (decl);
    8661              :     }
    8662              : }
    8663              : 
    8664              : tree
    8665      1193383 : trees_in::decl_value ()
    8666              : {
    8667      1193383 :   int tag = 0;
    8668      1193383 :   bool is_attached = false;
    8669      1193383 :   bool has_defn = false;
    8670      1193383 :   unsigned mk_u = u ();
    8671      1193383 :   if (mk_u >= MK_hwm || !merge_kind_name[mk_u])
    8672              :     {
    8673            0 :       set_overrun ();
    8674            0 :       return NULL_TREE;
    8675              :     }
    8676              : 
    8677      1193383 :   unsigned saved_unused = unused;
    8678      1193383 :   unused = 0;
    8679              : 
    8680      1193383 :   merge_kind mk = merge_kind (mk_u);
    8681              : 
    8682      1193383 :   tree decl = start ();
    8683      1193383 :   if (decl)
    8684              :     {
    8685      1193383 :       if (mk != MK_unique)
    8686              :         {
    8687       988359 :           bits_in bits = stream_bits ();
    8688       988359 :           if (!(mk & MK_template_mask) && !state->is_header ())
    8689        86126 :             is_attached = bits.b ();
    8690              : 
    8691       988359 :           has_defn = bits.b ();
    8692       988359 :         }
    8693              : 
    8694      1193383 :       if (!tree_node_bools (decl))
    8695            0 :         decl = NULL_TREE;
    8696              :     }
    8697              : 
    8698              :   /* Insert into map.  */
    8699      1193383 :   tag = insert (decl);
    8700      1193383 :   if (decl)
    8701      1193383 :     dump (dumper::TREE)
    8702          964 :       && dump ("Reading:%d %C", tag, TREE_CODE (decl));
    8703              : 
    8704      1193383 :   tree inner = decl;
    8705      1193383 :   int inner_tag = 0;
    8706      1193383 :   if (decl && TREE_CODE (decl) == TEMPLATE_DECL)
    8707              :     {
    8708       324886 :       int code = u ();
    8709       324886 :       inner = start (code);
    8710       324886 :       if (inner && tree_node_bools (inner))
    8711       324886 :         DECL_TEMPLATE_RESULT (decl) = inner;
    8712              :       else
    8713            0 :         decl = NULL_TREE;
    8714              : 
    8715       324886 :       inner_tag = insert (inner);
    8716       324886 :       if (decl)
    8717       324886 :         dump (dumper::TREE)
    8718          204 :           && dump ("Reading:%d %C", inner_tag, TREE_CODE (inner));
    8719              :     }
    8720              : 
    8721      1193383 :   tree type = NULL_TREE;
    8722      1193383 :   int type_tag = 0;
    8723      1193383 :   tree stub_decl = NULL_TREE;
    8724      1193383 :   int stub_tag = 0;
    8725      1193383 :   if (decl && TREE_CODE (inner) == TYPE_DECL)
    8726              :     {
    8727       386507 :       if (unsigned type_code = u ())
    8728              :         {
    8729       174164 :           type = start (type_code);
    8730       174164 :           if (type && tree_node_bools (type))
    8731              :             {
    8732       174164 :               TREE_TYPE (inner) = type;
    8733       174164 :               TYPE_NAME (type) = inner;
    8734              :             }
    8735              :           else
    8736            0 :             decl = NULL_TREE;
    8737              : 
    8738       174164 :           type_tag = insert (type);
    8739       174164 :           if (decl)
    8740       174164 :             dump (dumper::TREE)
    8741          212 :               && dump ("Reading type:%d %C", type_tag, TREE_CODE (type));
    8742              : 
    8743       174164 :           if (unsigned stub_code = u ())
    8744              :             {
    8745          441 :               stub_decl = start (stub_code);
    8746          441 :               if (stub_decl && tree_node_bools (stub_decl))
    8747              :                 {
    8748          441 :                   TREE_TYPE (stub_decl) = type;
    8749          441 :                   TYPE_STUB_DECL (type) = stub_decl;
    8750              :                 }
    8751              :               else
    8752            0 :                 decl = NULL_TREE;
    8753              : 
    8754          441 :               stub_tag = insert (stub_decl);
    8755          441 :               if (decl)
    8756          441 :                 dump (dumper::TREE)
    8757            0 :                   && dump ("Reading stub_decl:%d %C", stub_tag,
    8758            0 :                            TREE_CODE (stub_decl));
    8759              :             }
    8760              :         }
    8761              :     }
    8762              : 
    8763      1193383 :   if (!decl)
    8764              :     {
    8765            0 :     bail:
    8766            0 :       if (inner_tag != 0)
    8767            0 :         back_refs[~inner_tag] = NULL_TREE;
    8768            0 :       if (type_tag != 0)
    8769            0 :         back_refs[~type_tag] = NULL_TREE;
    8770            0 :       if (stub_tag != 0)
    8771            0 :         back_refs[~stub_tag] = NULL_TREE;
    8772            0 :       if (tag != 0)
    8773            0 :         back_refs[~tag] = NULL_TREE;
    8774            0 :       set_overrun ();
    8775              :       /* Bail.  */
    8776            0 :       unused = saved_unused;
    8777            0 :       return NULL_TREE;
    8778              :     }
    8779              : 
    8780              :   /* Read the container, to ensure it's already been streamed in.  */
    8781      1193383 :   tree container = decl_container ();
    8782      1193383 :   unsigned tpl_levels = 0;
    8783              : 
    8784              :   /* If this is an imported temploid friend, get the owning decl its
    8785              :      attachment is determined by (or NULL_TREE otherwise).  */
    8786      1193383 :   tree temploid_friend = NULL_TREE;
    8787      1193383 :   if (TREE_CODE (inner) == FUNCTION_DECL || TREE_CODE (inner) == TYPE_DECL)
    8788       845002 :     temploid_friend = tree_node ();
    8789              : 
    8790              :   /* Figure out if this decl is already known about.  */
    8791      1193383 :   int parm_tag = 0;
    8792              : 
    8793      1193383 :   if (decl != inner)
    8794       324886 :     if (!tpl_header (decl, &tpl_levels))
    8795            0 :       goto bail;
    8796      1193383 :   if (TREE_CODE (inner) == FUNCTION_DECL)
    8797       458495 :     parm_tag = fn_parms_init (inner);
    8798              : 
    8799      1193383 :   tree existing = key_mergeable (tag, mk, decl, inner, type, container,
    8800              :                                  is_attached, temploid_friend);
    8801      1193383 :   tree existing_inner = existing;
    8802      1193383 :   if (existing)
    8803              :     {
    8804       435586 :       if (existing == error_mark_node)
    8805            0 :         goto bail;
    8806              : 
    8807       435586 :       if (TREE_CODE (STRIP_TEMPLATE (existing)) == TYPE_DECL)
    8808              :         {
    8809       161772 :           tree etype = TREE_TYPE (existing);
    8810       161772 :           if (TYPE_LANG_SPECIFIC (etype)
    8811       116718 :               && COMPLETE_TYPE_P (etype)
    8812       229791 :               && !CLASSTYPE_MEMBER_VEC (etype))
    8813              :             /* Give it a member vec, we're likely gonna be looking
    8814              :                inside it.  */
    8815        14166 :             set_class_bindings (etype, -1);
    8816              :         }
    8817              : 
    8818              :       /* Install the existing decl into the back ref array.  */
    8819       435586 :       register_duplicate (decl, existing);
    8820       435586 :       back_refs[~tag] = existing;
    8821       435586 :       if (inner_tag != 0)
    8822              :         {
    8823       145505 :           existing_inner = DECL_TEMPLATE_RESULT (existing);
    8824       145505 :           back_refs[~inner_tag] = existing_inner;
    8825              :         }
    8826              : 
    8827       435586 :       if (type_tag != 0)
    8828              :         {
    8829        77383 :           tree existing_type = TREE_TYPE (existing);
    8830        77383 :           back_refs[~type_tag] = existing_type;
    8831        77383 :           if (stub_tag != 0)
    8832          245 :             back_refs[~stub_tag] = TYPE_STUB_DECL (existing_type);
    8833              :         }
    8834              :     }
    8835              : 
    8836      1193383 :   if (parm_tag)
    8837       458495 :     fn_parms_fini (parm_tag, inner, existing_inner, has_defn);
    8838              : 
    8839      1193383 :   if (!tree_node_vals (decl))
    8840            0 :     goto bail;
    8841              : 
    8842      1193383 :   if (inner_tag)
    8843              :     {
    8844       324886 :       gcc_checking_assert (DECL_TEMPLATE_RESULT (decl) == inner);
    8845              : 
    8846       324886 :       if (!tree_node_vals (inner))
    8847            0 :         goto bail;
    8848              : 
    8849       324886 :       if (!tpl_parms_fini (decl, tpl_levels))
    8850            0 :         goto bail;
    8851              :     }
    8852              : 
    8853      1193383 :   if (type && (!tree_node_vals (type)
    8854       174164 :                || (stub_decl && !tree_node_vals (stub_decl))))
    8855            0 :     goto bail;
    8856              : 
    8857      1193383 :   spec_entry spec;
    8858      1193383 :   unsigned spec_flags = 0;
    8859      1193383 :   if (mk & MK_template_mask
    8860       823553 :       || mk == MK_partial
    8861       823553 :       || mk == MK_friend_spec)
    8862              :     {
    8863        10771 :       if (mk == MK_partial)
    8864              :         spec_flags = 2;
    8865              :       else
    8866       369830 :         spec_flags = u ();
    8867              : 
    8868       380601 :       spec.tmpl = tree_node ();
    8869       380601 :       spec.args = tree_node ();
    8870              :     }
    8871              :   /* Hold constraints on the spec field, for a short while.  */
    8872      1193383 :   spec.spec = tree_node ();
    8873              : 
    8874      1194347 :   dump (dumper::TREE) && dump ("Read:%d %C:%N", tag, TREE_CODE (decl), decl);
    8875              : 
    8876      1193383 :   existing = back_refs[~tag];
    8877      1193383 :   bool installed = install_entity (existing);
    8878      1193383 :   bool is_new = existing == decl;
    8879              : 
    8880      1193383 :   if (DECL_LANG_SPECIFIC (inner)
    8881      2241954 :       && DECL_MODULE_KEYED_DECLS_P (inner))
    8882              :     {
    8883              :       /* Read and maybe install the attached entities.  */
    8884          375 :       bool existed;
    8885          375 :       auto &set = keyed_table->get_or_insert (STRIP_TEMPLATE (existing),
    8886              :                                               &existed);
    8887          375 :       unsigned num = u ();
    8888          375 :       if (is_new == existed)
    8889            0 :         set_overrun ();
    8890          375 :       if (is_new)
    8891          242 :         set.reserve (num);
    8892          799 :       for (unsigned ix = 0; !get_overrun () && ix != num; ix++)
    8893              :         {
    8894          424 :           tree attached = tree_node ();
    8895          424 :           dump (dumper::MERGE)
    8896          105 :             && dump ("Read %d[%u] %s attached decl %N", tag, ix,
    8897              :                      is_new ? "new" : "matched", attached);
    8898          424 :           if (is_new)
    8899          272 :             set.quick_push (attached);
    8900          152 :           else if (set[ix] != attached)
    8901              :             {
    8902            3 :               if (!set[ix] || !attached)
    8903              :                 /* One import left a hole for a lambda dep we chose not
    8904              :                    to stream, but another import chose to stream that lambda.
    8905              :                    Let's not error here: hopefully we'll complain later in
    8906              :                    is_matching_decl about whatever caused us to make a
    8907              :                    different decision.  */
    8908              :                 ;
    8909              :               else
    8910            0 :                 set_overrun ();
    8911              :             }
    8912              :         }
    8913              :     }
    8914              : 
    8915              :   /* Regular typedefs will have a NULL TREE_TYPE at this point.  */
    8916      1193383 :   unsigned tdef_flags = 0;
    8917      1193383 :   bool is_typedef = false;
    8918      1193383 :   if (!type && TREE_CODE (inner) == TYPE_DECL)
    8919              :     {
    8920       212343 :       tdef_flags = u ();
    8921       212343 :       if (tdef_flags & 1)
    8922       212343 :         is_typedef = true;
    8923              :     }
    8924              : 
    8925      1193383 :   if (is_new)
    8926              :     {
    8927              :       /* A newly discovered node.  */
    8928       757797 :       if (TREE_CODE (decl) == FUNCTION_DECL && DECL_VIRTUAL_P (decl))
    8929              :         /* Mark this identifier as naming a virtual function --
    8930              :            lookup_overrides relies on this optimization.  */
    8931         6570 :         IDENTIFIER_VIRTUAL_P (DECL_NAME (decl)) = true;
    8932              : 
    8933       757797 :       if (installed)
    8934              :         {
    8935              :           /* Mark the entity as imported.  */
    8936       486732 :           retrofit_lang_decl (inner);
    8937       486732 :           DECL_MODULE_IMPORT_P (inner) = true;
    8938              :         }
    8939              : 
    8940       757797 :       if (temploid_friend)
    8941           38 :         imported_temploid_friends->put (decl, temploid_friend);
    8942              : 
    8943       757797 :       if (spec.spec)
    8944        32134 :         set_constraints (decl, spec.spec);
    8945              : 
    8946       757797 :       if (TREE_CODE (decl) == INTEGER_CST && !TREE_OVERFLOW (decl))
    8947              :         {
    8948            0 :           decl = cache_integer_cst (decl, true);
    8949            0 :           back_refs[~tag] = decl;
    8950              :         }
    8951              : 
    8952       757797 :       if (is_typedef)
    8953              :         {
    8954              :           /* Frob it to be ready for cloning.  */
    8955       127954 :           TREE_TYPE (inner) = DECL_ORIGINAL_TYPE (inner);
    8956       127954 :           DECL_ORIGINAL_TYPE (inner) = NULL_TREE;
    8957       127954 :           if (TREE_CODE (TREE_TYPE (inner)) != TU_LOCAL_ENTITY)
    8958              :             {
    8959       127951 :               set_underlying_type (inner);
    8960       127951 :               if (tdef_flags & 2)
    8961              :                 {
    8962              :                   /* Match instantiate_alias_template's handling.  */
    8963        32557 :                   tree type = TREE_TYPE (inner);
    8964        32557 :                   TYPE_DEPENDENT_P (type) = true;
    8965        32557 :                   TYPE_DEPENDENT_P_VALID (type) = true;
    8966        32557 :                   SET_TYPE_STRUCTURAL_EQUALITY (type);
    8967              :                 }
    8968              :             }
    8969              :         }
    8970              : 
    8971       757797 :       if (inner_tag)
    8972              :         /* Set the TEMPLATE_DECL's type.  */
    8973       179381 :         TREE_TYPE (decl) = TREE_TYPE (inner);
    8974              : 
    8975              :       /* Redetermine whether we need to import or export this declaration
    8976              :          for this TU.  But for extern templates we know we must import:
    8977              :          they'll be defined in a different TU.
    8978              :          FIXME: How do dllexport and dllimport interact across a module?
    8979              :          See also https://github.com/itanium-cxx-abi/cxx-abi/issues/170.
    8980              :          May have to revisit?  */
    8981       757797 :       if (type
    8982        96781 :           && CLASS_TYPE_P (type)
    8983        83354 :           && TYPE_LANG_SPECIFIC (type)
    8984       841151 :           && !(CLASSTYPE_EXPLICIT_INSTANTIATION (type)
    8985          762 :                && CLASSTYPE_INTERFACE_KNOWN (type)
    8986          762 :                && CLASSTYPE_INTERFACE_ONLY (type)))
    8987              :         {
    8988        82644 :           CLASSTYPE_INTERFACE_ONLY (type) = false;
    8989        82644 :           CLASSTYPE_INTERFACE_UNKNOWN (type) = true;
    8990              :         }
    8991              : 
    8992              :       /* Add to specialization tables now that constraints etc are
    8993              :          added.  */
    8994       757797 :       if (mk == MK_partial)
    8995              :         {
    8996         4607 :           bool is_type = TREE_CODE (inner) == TYPE_DECL;
    8997         4607 :           spec.spec = is_type ? type : inner;
    8998         4607 :           add_mergeable_specialization (!is_type, &spec, decl, spec_flags);
    8999              :         }
    9000       753190 :       else if (mk & MK_template_mask)
    9001              :         {
    9002       219905 :           bool is_type = !(mk & MK_tmpl_decl_mask);
    9003       219905 :           spec.spec = is_type ? type : mk & MK_tmpl_tmpl_mask ? inner : decl;
    9004       219905 :           add_mergeable_specialization (!is_type, &spec, decl, spec_flags);
    9005              :         }
    9006              : 
    9007       757797 :       if (NAMESPACE_SCOPE_P (decl)
    9008       160307 :           && (mk == MK_named || mk == MK_unique
    9009       160307 :               || mk == MK_enum || mk == MK_friend_spec)
    9010       830072 :           && !(VAR_OR_FUNCTION_DECL_P (decl) && DECL_LOCAL_DECL_P (decl)))
    9011        72176 :         add_module_namespace_decl (CP_DECL_CONTEXT (decl), decl);
    9012              : 
    9013       757797 :       if (DECL_ARTIFICIAL (decl)
    9014       204688 :           && TREE_CODE (decl) == FUNCTION_DECL
    9015        21428 :           && !DECL_TEMPLATE_INFO (decl)
    9016        21116 :           && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
    9017        20944 :           && TYPE_SIZE (DECL_CONTEXT (decl))
    9018       759281 :           && !DECL_THUNK_P (decl))
    9019              :         /* A new implicit member function, when the class is
    9020              :            complete.  This means the importee declared it, and
    9021              :            we must now add it to the class.  Note that implicit
    9022              :            member fns of template instantiations do not themselves
    9023              :            look like templates.  */
    9024          910 :         if (!install_implicit_member (inner))
    9025            0 :           set_overrun ();
    9026              : 
    9027              :       /* When importing a TLS wrapper from a header unit, we haven't
    9028              :          actually emitted its definition yet. Remember it so we can
    9029              :          do this later.  */
    9030       757797 :       if (state->is_header ()
    9031       757797 :           && decl_tls_wrapper_p (decl))
    9032            6 :         note_vague_linkage_fn (decl);
    9033              : 
    9034              :       /* Apply relevant attributes.
    9035              :          FIXME should probably use cplus_decl_attributes for this,
    9036              :          but it's not yet ready for modules.  */
    9037              : 
    9038       757797 :       if (VAR_OR_FUNCTION_DECL_P (inner))
    9039       454137 :         if (tree attr = lookup_attribute ("section", DECL_ATTRIBUTES (inner)))
    9040              :           {
    9041            6 :             tree section_name = TREE_VALUE (TREE_VALUE (attr));
    9042            6 :             set_decl_section_name (inner, TREE_STRING_POINTER (section_name));
    9043              :           }
    9044              : 
    9045              :       /* Setup aliases for the declaration.  */
    9046       757797 :       if (tree alias = lookup_attribute ("alias", DECL_ATTRIBUTES (decl)))
    9047              :         {
    9048            3 :           alias = TREE_VALUE (TREE_VALUE (alias));
    9049            3 :           alias = get_identifier (TREE_STRING_POINTER (alias));
    9050            3 :           assemble_alias (decl, alias);
    9051              :         }
    9052              :     }
    9053              :   else
    9054              :     {
    9055              :       /* DECL is the to-be-discarded decl.  Its internal pointers will
    9056              :          be to the EXISTING's structure.  Frob it to point to its
    9057              :          own other structures, so loading its definition will alter
    9058              :          it, and not the existing decl.  */
    9059       437348 :       dump (dumper::MERGE) && dump ("Deduping %N", existing);
    9060              : 
    9061       435586 :       if (inner_tag)
    9062       145505 :         DECL_TEMPLATE_RESULT (decl) = inner;
    9063              : 
    9064       435586 :       if (type)
    9065              :         {
    9066              :           /* Point at the to-be-discarded type & decl.  */
    9067        77383 :           TYPE_NAME (type) = inner;
    9068        77383 :           TREE_TYPE (inner) = type;
    9069              : 
    9070       154521 :           TYPE_STUB_DECL (type) = stub_decl ? stub_decl : inner;
    9071        77383 :           if (stub_decl)
    9072          245 :             TREE_TYPE (stub_decl) = type;
    9073              : 
    9074        77383 :           tree etype = TREE_TYPE (existing);
    9075              : 
    9076              :           /* Handle separate declarations with different attributes.  */
    9077        77383 :           tree &dattr = TYPE_ATTRIBUTES (type);
    9078        77383 :           tree &eattr = TYPE_ATTRIBUTES (etype);
    9079        77383 :           check_abi_tags (existing, decl, eattr, dattr);
    9080              :           // TODO: handle other conflicting type attributes
    9081        77383 :           eattr = merge_attributes (eattr, dattr);
    9082              : 
    9083              :           /* When merging a partial specialisation, the existing decl may have
    9084              :              had its TYPE_CANONICAL adjusted.  If so we should use structural
    9085              :              equality to ensure is_matching_decl doesn't get confused.  */
    9086        77383 :           if ((spec_flags & 2)
    9087        77383 :               && TYPE_CANONICAL (type) != TYPE_CANONICAL (etype))
    9088            3 :             SET_TYPE_STRUCTURAL_EQUALITY (type);
    9089              :         }
    9090              : 
    9091       435586 :       if (inner_tag)
    9092              :         /* Set the TEMPLATE_DECL's type.  */
    9093       145505 :         TREE_TYPE (decl) = TREE_TYPE (inner);
    9094              : 
    9095       435586 :       if (!is_matching_decl (existing, decl, is_typedef))
    9096           42 :         unmatched_duplicate (existing);
    9097              : 
    9098       435586 :       if (TREE_CODE (inner) == FUNCTION_DECL)
    9099              :         {
    9100       199182 :           tree e_inner = STRIP_TEMPLATE (existing);
    9101       199182 :           for (auto parm = DECL_ARGUMENTS (inner);
    9102       595896 :                parm; parm = DECL_CHAIN (parm))
    9103       396714 :             DECL_CONTEXT (parm) = e_inner;
    9104              :         }
    9105              : 
    9106              :       /* And our result is the existing node.  */
    9107       435586 :       decl = existing;
    9108              :     }
    9109              : 
    9110      1193383 :   if (mk == MK_friend_spec)
    9111              :     {
    9112            0 :       tree e = match_mergeable_specialization (true, &spec);
    9113            0 :       if (!e)
    9114              :         {
    9115            0 :           spec.spec = inner;
    9116            0 :           add_mergeable_specialization (true, &spec, decl, spec_flags);
    9117              :         }
    9118            0 :       else if (e != existing)
    9119            0 :         set_overrun ();
    9120              :     }
    9121              : 
    9122      1193383 :   if (is_typedef)
    9123              :     {
    9124              :       /* Insert the type into the array now.  */
    9125       212343 :       tag = insert (TREE_TYPE (decl));
    9126       212343 :       dump (dumper::TREE)
    9127          247 :         && dump ("Cloned:%d typedef %C:%N",
    9128          247 :                  tag, TREE_CODE (TREE_TYPE (decl)), TREE_TYPE (decl));
    9129              :     }
    9130              : 
    9131      1193383 :   unused = saved_unused;
    9132              : 
    9133      1193383 :   if (DECL_MAYBE_IN_CHARGE_CDTOR_P (decl))
    9134              :     {
    9135       102657 :       unsigned flags = u ();
    9136              : 
    9137       102657 :       if (is_new)
    9138              :         {
    9139        62406 :           bool cloned_p = flags & 1;
    9140        62506 :           dump (dumper::TREE) && dump ("CDTOR %N is %scloned",
    9141              :                                        decl, cloned_p ? "" : "not ");
    9142        62406 :           if (cloned_p)
    9143              :             {
    9144              :               /* Update the member vec, if there is one (we're in a different
    9145              :                  cluster to the class defn) and this isn't a primary template
    9146              :                  specialization (as in tsubst_function_decl).  */
    9147        44018 :               bool up = (CLASSTYPE_MEMBER_VEC (DECL_CONTEXT (decl))
    9148        44018 :                          && !primary_template_specialization_p (decl));
    9149        44018 :               build_cdtor_clones (decl, flags & 2, flags & 4, up);
    9150              :             }
    9151              :         }
    9152              :     }
    9153              : 
    9154      1193383 :   if (VAR_P (decl) && CP_DECL_THREAD_LOCAL_P (decl))
    9155              :     {
    9156          160 :       enum tls_model model = tls_model (u ());
    9157          160 :       if (is_new)
    9158          140 :         set_decl_tls_model (decl, model);
    9159              :     }
    9160              : 
    9161      1193383 :   if (!NAMESPACE_SCOPE_P (inner)
    9162       887207 :       && ((TREE_CODE (inner) == TYPE_DECL
    9163       208505 :            && !is_typedef
    9164        37102 :            && TYPE_NAME (TREE_TYPE (inner)) == inner)
    9165       850105 :           || TREE_CODE (inner) == FUNCTION_DECL)
    9166      1578581 :       && u ())
    9167            3 :     read_definition (decl);
    9168              : 
    9169              :   return decl;
    9170              : }
    9171              : 
    9172              : /* DECL is an unnameable member of CTX.  Return a suitable identifying
    9173              :    index.  */
    9174              : 
    9175              : static unsigned
    9176         1144 : get_field_ident (tree ctx, tree decl)
    9177              : {
    9178         1144 :   gcc_checking_assert (TREE_CODE (decl) == USING_DECL
    9179              :                        || !DECL_NAME (decl)
    9180              :                        || IDENTIFIER_ANON_P (DECL_NAME (decl)));
    9181              : 
    9182         1144 :   unsigned ix = 0;
    9183         1144 :   for (tree fields = TYPE_FIELDS (ctx);
    9184         8472 :        fields; fields = DECL_CHAIN (fields))
    9185              :     {
    9186         8472 :       if (fields == decl)
    9187         1144 :         return ix;
    9188              : 
    9189         7328 :       if (DECL_CONTEXT (fields) == ctx
    9190         7328 :           && (TREE_CODE (fields) == USING_DECL
    9191         7313 :               || (TREE_CODE (fields) == FIELD_DECL
    9192           65 :                   && (!DECL_NAME (fields)
    9193           36 :                       || IDENTIFIER_ANON_P (DECL_NAME (fields))))))
    9194              :         /* Count this field.  */
    9195           32 :         ix++;
    9196              :     }
    9197            0 :   gcc_unreachable ();
    9198              : }
    9199              : 
    9200              : static tree
    9201          997 : lookup_field_ident (tree ctx, unsigned ix)
    9202              : {
    9203          997 :   for (tree fields = TYPE_FIELDS (ctx);
    9204         7439 :        fields; fields = DECL_CHAIN (fields))
    9205         7439 :     if (DECL_CONTEXT (fields) == ctx
    9206         7439 :         && (TREE_CODE (fields) == USING_DECL
    9207         7427 :             || (TREE_CODE (fields) == FIELD_DECL
    9208         1058 :                 && (!DECL_NAME (fields)
    9209           25 :                     || IDENTIFIER_ANON_P (DECL_NAME (fields))))))
    9210         1039 :       if (!ix--)
    9211              :         return fields;
    9212              : 
    9213              :   return NULL_TREE;
    9214              : }
    9215              : 
    9216              : /* Reference DECL.  REF indicates the walk kind we are performing.
    9217              :    Return true if we should write this decl by value.  */
    9218              : 
    9219              : bool
    9220     12619547 : trees_out::decl_node (tree decl, walk_kind ref)
    9221              : {
    9222     12619547 :   gcc_checking_assert (DECL_P (decl) && !DECL_TEMPLATE_PARM_P (decl)
    9223              :                        && DECL_CONTEXT (decl));
    9224              : 
    9225     12619547 :   if (ref == WK_value)
    9226              :     {
    9227      1261823 :       depset *dep = dep_hash->find_dependency (decl);
    9228      1261823 :       decl_value (decl, dep);
    9229      1261823 :       return false;
    9230              :     }
    9231              : 
    9232     11357724 :   switch (TREE_CODE (decl))
    9233              :     {
    9234              :     default:
    9235              :       break;
    9236              : 
    9237      1196992 :     case FUNCTION_DECL:
    9238      1196992 :       gcc_checking_assert (!DECL_LOCAL_DECL_P (decl));
    9239              :       break;
    9240              : 
    9241              :     case RESULT_DECL:
    9242              :       /* Unlike PARM_DECLs, RESULT_DECLs are only generated and
    9243              :          referenced when we're inside the function itself.  */
    9244              :       return true;
    9245              : 
    9246       181610 :     case PARM_DECL:
    9247       181610 :       {
    9248       181610 :         if (streaming_p ())
    9249        79556 :           i (tt_parm);
    9250       181610 :         tree_node (DECL_CONTEXT (decl));
    9251              : 
    9252              :         /* That must have put this in the map.  */
    9253       181610 :         walk_kind ref = ref_node (decl);
    9254       181610 :         if (ref != WK_none)
    9255              :           // FIXME:OPTIMIZATION We can wander into bits of the
    9256              :           // template this was instantiated from, for instance
    9257              :           // deferred noexcept and default parms, or references
    9258              :           // to parms from earlier forward-decls (PR c++/119608).
    9259              :           //
    9260              :           // Currently we'll end up cloning those bits of tree.
    9261              :           // It would be nice to reference those specific nodes.
    9262              :           // I think putting those things in the map when we
    9263              :           // reference their template by name.
    9264              :           //
    9265              :           // See the note in add_indirects.
    9266              :           return true;
    9267              : 
    9268            0 :         if (streaming_p ())
    9269            0 :           dump (dumper::TREE)
    9270            0 :             && dump ("Wrote %s reference %N",
    9271            0 :                      TREE_CODE (decl) == PARM_DECL ? "parameter" : "result",
    9272              :                      decl);
    9273              :       }
    9274              :       return false;
    9275              : 
    9276              :     case IMPORTED_DECL:
    9277              :       /* This describes a USING_DECL to the ME's debug machinery.  It
    9278              :          originates from the fortran FE, and has nothing to do with
    9279              :          C++ modules.  */
    9280              :       return true;
    9281              : 
    9282              :     case LABEL_DECL:
    9283              :       return true;
    9284              : 
    9285        66835 :     case CONST_DECL:
    9286        66835 :       {
    9287              :         /* If I end up cloning enum decls, implementing C++20 using
    9288              :            E::v, this will need tweaking.   */
    9289        66835 :         if (streaming_p ())
    9290        15646 :           i (tt_enum_decl);
    9291        66835 :         tree ctx = DECL_CONTEXT (decl);
    9292        66835 :         gcc_checking_assert (TREE_CODE (ctx) == ENUMERAL_TYPE);
    9293        66835 :         tree_node (ctx);
    9294        66835 :         tree_node (DECL_NAME (decl));
    9295              : 
    9296        66835 :         int tag = insert (decl);
    9297        66835 :         if (streaming_p ())
    9298        15646 :           dump (dumper::TREE)
    9299           21 :             && dump ("Wrote enum decl:%d %C:%N", tag, TREE_CODE (decl), decl);
    9300              :         return false;
    9301              :       }
    9302        23584 :       break;
    9303              : 
    9304        23584 :     case USING_DECL:
    9305        23584 :       if (TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
    9306              :         break;
    9307              :       /* FALLTHROUGH  */
    9308              : 
    9309       160665 :     case FIELD_DECL:
    9310       160665 :       {
    9311       160665 :         if (streaming_p ())
    9312         8787 :           i (tt_data_member);
    9313              : 
    9314       160665 :         tree ctx = DECL_CONTEXT (decl);
    9315       160665 :         tree_node (ctx);
    9316              : 
    9317       160665 :         tree name = NULL_TREE;
    9318              : 
    9319       160665 :         if (TREE_CODE (decl) == USING_DECL)
    9320              :           ;
    9321              :         else
    9322              :           {
    9323       159469 :             name = DECL_NAME (decl);
    9324       309330 :             if (name && IDENTIFIER_ANON_P (name))
    9325              :               name = NULL_TREE;
    9326              :           }
    9327              : 
    9328       160665 :         tree_node (name);
    9329       160665 :         if (!name && streaming_p ())
    9330              :           {
    9331         1144 :             unsigned ix = get_field_ident (ctx, decl);
    9332         1144 :             u (ix);
    9333              :           }
    9334              : 
    9335       160665 :         int tag = insert (decl);
    9336       160665 :         if (streaming_p ())
    9337         8787 :           dump (dumper::TREE)
    9338           26 :             && dump ("Wrote member:%d %C:%N", tag, TREE_CODE (decl), decl);
    9339              :         return false;
    9340              :       }
    9341       500726 :       break;
    9342              : 
    9343       500726 :     case VAR_DECL:
    9344       500726 :       gcc_checking_assert (!DECL_LOCAL_DECL_P (decl));
    9345       500726 :       if (DECL_VTABLE_OR_VTT_P (decl))
    9346              :         {
    9347              :           /* VTT or VTABLE, they are all on the vtables list.  */
    9348         3484 :           tree ctx = CP_DECL_CONTEXT (decl);
    9349         3484 :           tree vtable = CLASSTYPE_VTABLES (ctx);
    9350         3547 :           for (unsigned ix = 0; ; vtable = DECL_CHAIN (vtable), ix++)
    9351         3547 :             if (vtable == decl)
    9352              :               {
    9353         3484 :                 gcc_checking_assert (DECL_VIRTUAL_P (decl));
    9354         3484 :                 if (streaming_p ())
    9355              :                   {
    9356           43 :                     u (tt_vtable);
    9357           43 :                     u (ix);
    9358           43 :                     dump (dumper::TREE)
    9359            0 :                       && dump ("Writing vtable %N[%u]", ctx, ix);
    9360              :                   }
    9361         3484 :                 tree_node (ctx);
    9362         3484 :                 return false;
    9363              :               }
    9364              :           gcc_unreachable ();
    9365              :         }
    9366              : 
    9367       497242 :       if (DECL_TINFO_P (decl))
    9368              :         {
    9369         7053 :         tinfo:
    9370              :           /* A typeinfo, tt_tinfo_typedef or tt_tinfo_var.  */
    9371        12986 :           bool is_var = VAR_P (decl);
    9372        12986 :           tree type = TREE_TYPE (decl);
    9373        12986 :           unsigned ix = get_pseudo_tinfo_index (type);
    9374        12986 :           if (streaming_p ())
    9375              :             {
    9376         8189 :               i (is_var ? tt_tinfo_var : tt_tinfo_typedef);
    9377         5804 :               u (ix);
    9378              :             }
    9379              : 
    9380        12986 :           if (is_var)
    9381              :             {
    9382              :               /* We also need the type it is for and mangled name, so
    9383              :                  the reader doesn't need to complete the type (which
    9384              :                  would break section ordering).  The type it is for is
    9385              :                  stashed on the name's TREE_TYPE.  */
    9386         7053 :               tree name = DECL_NAME (decl);
    9387         7053 :               tree_node (name);
    9388         7053 :               type = TREE_TYPE (name);
    9389         7053 :               tree_node (type);
    9390              :             }
    9391              : 
    9392        12986 :           int tag = insert (decl);
    9393        12986 :           if (streaming_p ())
    9394         5804 :             dump (dumper::TREE)
    9395           27 :               && dump ("Wrote tinfo_%s:%d %u %N", is_var ? "var" : "type",
    9396              :                        tag, ix, type);
    9397              : 
    9398        12986 :           if (!is_var)
    9399              :             {
    9400         5933 :               tag = insert (type);
    9401         5933 :               if (streaming_p ())
    9402         2385 :                 dump (dumper::TREE)
    9403            9 :                   && dump ("Wrote tinfo_type:%d %u %N", tag, ix, type);
    9404              :             }
    9405        12986 :           return false;
    9406              :         }
    9407              : 
    9408       490189 :       if (DECL_NTTP_OBJECT_P (decl))
    9409              :         {
    9410              :           /* A NTTP parm object.  */
    9411           36 :           if (streaming_p ())
    9412            8 :             i (tt_nttp_var);
    9413           36 :           tree_node (tparm_object_argument (decl));
    9414           36 :           tree_node (DECL_NAME (decl));
    9415           36 :           int tag = insert (decl);
    9416           36 :           if (streaming_p ())
    9417            8 :             dump (dumper::TREE)
    9418            0 :               && dump ("Wrote nttp object:%d %N", tag, DECL_NAME (decl));
    9419           36 :           return false;
    9420              :         }
    9421              : 
    9422              :       break;
    9423              : 
    9424      3934413 :     case TYPE_DECL:
    9425      3934413 :       if (DECL_TINFO_P (decl))
    9426         5933 :         goto tinfo;
    9427              :       break;
    9428              :     }
    9429              : 
    9430     10325831 :   if (DECL_THUNK_P (decl))
    9431              :     {
    9432              :       /* Thunks are similar to binfos -- write the thunked-to decl and
    9433              :          then thunk-specific key info.  */
    9434            0 :       if (streaming_p ())
    9435              :         {
    9436            0 :           i (tt_thunk);
    9437            0 :           i (THUNK_FIXED_OFFSET (decl));
    9438              :         }
    9439              : 
    9440              :       tree target = decl;
    9441            0 :       while (DECL_THUNK_P (target))
    9442            0 :         target = THUNK_TARGET (target);
    9443            0 :       tree_node (target);
    9444            0 :       tree_node (THUNK_VIRTUAL_OFFSET (decl));
    9445            0 :       int tag = insert (decl);
    9446            0 :       if (streaming_p ())
    9447            0 :         dump (dumper::TREE)
    9448            0 :           && dump ("Wrote:%d thunk %N to %N", tag, DECL_NAME (decl), target);
    9449            0 :       return false;
    9450              :     }
    9451              : 
    9452     10325831 :   if (DECL_CLONED_FUNCTION_P (decl))
    9453              :     {
    9454       350315 :       tree target = get_clone_target (decl);
    9455       350315 :       if (streaming_p ())
    9456       165850 :         i (tt_clone_ref);
    9457              : 
    9458       350315 :       tree_node (target);
    9459       350315 :       tree_node (DECL_NAME (decl));
    9460       350315 :       if (DECL_VIRTUAL_P (decl))
    9461        25061 :         tree_node (DECL_VINDEX (decl));
    9462       350315 :       int tag = insert (decl);
    9463       350315 :       if (streaming_p ())
    9464       165850 :         dump (dumper::TREE)
    9465          164 :           && dump ("Wrote:%d clone %N of %N", tag, DECL_NAME (decl), target);
    9466       350315 :       return false;
    9467              :     }
    9468              : 
    9469              :   /* Everything left should be a thing that is in the entity table.
    9470              :      Mostly things that can be defined outside of their (original
    9471              :      declaration) context.  */
    9472      9975516 :   gcc_checking_assert (TREE_CODE (decl) == TEMPLATE_DECL
    9473              :                        || VAR_P (decl)
    9474              :                        || TREE_CODE (decl) == FUNCTION_DECL
    9475              :                        || TREE_CODE (decl) == TYPE_DECL
    9476              :                        || TREE_CODE (decl) == USING_DECL
    9477              :                        || TREE_CODE (decl) == CONCEPT_DECL
    9478              :                        || TREE_CODE (decl) == NAMESPACE_DECL);
    9479              : 
    9480      9975516 :   int use_tpl = -1;
    9481      9975516 :   tree ti = node_template_info (decl, use_tpl);
    9482      9975516 :   tree tpl = NULL_TREE;
    9483              : 
    9484              :   /* If this is the TEMPLATE_DECL_RESULT of a TEMPLATE_DECL, get the
    9485              :      TEMPLATE_DECL.  Note TI_TEMPLATE is not a TEMPLATE_DECL for
    9486              :      (some) friends, so we need to check that.  */
    9487              :   // FIXME: Should local friend template specializations be by value?
    9488              :   // They don't get idents so we'll never know they're imported, but I
    9489              :   // think we can only reach them from the TU that defines the
    9490              :   // befriending class?
    9491      3668002 :   if (ti && TREE_CODE (TI_TEMPLATE (ti)) == TEMPLATE_DECL
    9492     13643461 :       && DECL_TEMPLATE_RESULT (TI_TEMPLATE (ti)) == decl)
    9493              :     {
    9494              :       tpl = TI_TEMPLATE (ti);
    9495       933223 :     partial_template:
    9496       933223 :       if (streaming_p ())
    9497              :         {
    9498         3074 :           i (tt_template);
    9499         3074 :           dump (dumper::TREE)
    9500            9 :             && dump ("Writing implicit template %C:%N%S",
    9501            9 :                      TREE_CODE (tpl), tpl, tpl);
    9502              :         }
    9503       933223 :       tree_node (tpl);
    9504              : 
    9505              :       /* Streaming TPL caused us to visit DECL and maybe its type,
    9506              :          if it wasn't TU-local.  */
    9507       933223 :       if (CHECKING_P && !has_tu_local_dep (tpl))
    9508              :         {
    9509       933196 :           gcc_checking_assert (TREE_VISITED (decl));
    9510       933196 :           if (DECL_IMPLICIT_TYPEDEF_P (decl))
    9511       482866 :             gcc_checking_assert (TREE_VISITED (TREE_TYPE (decl)));
    9512              :         }
    9513              :       return false;
    9514              :     }
    9515              : 
    9516      9142211 :   tree ctx = CP_DECL_CONTEXT (decl);
    9517      9142211 :   depset *dep = NULL;
    9518      9142211 :   if (streaming_p ())
    9519      1545430 :     dep = dep_hash->find_dependency (decl);
    9520      7596781 :   else if (TREE_CODE (ctx) != FUNCTION_DECL
    9521       289594 :            || TREE_CODE (decl) == TEMPLATE_DECL
    9522       262033 :            || DECL_IMPLICIT_TYPEDEF_P (decl)
    9523      7828423 :            || (DECL_LANG_SPECIFIC (decl)
    9524       145839 :                && DECL_MODULE_IMPORT_P (decl)))
    9525              :     {
    9526      7365139 :       auto kind = (TREE_CODE (decl) == NAMESPACE_DECL
    9527       594321 :                    && !DECL_NAMESPACE_ALIAS (decl)
    9528      7365139 :                    ? depset::EK_NAMESPACE : depset::EK_DECL);
    9529      7365139 :       dep = dep_hash->add_dependency (decl, kind);
    9530              :     }
    9531              : 
    9532      8910569 :   if (!dep || dep->is_tu_local ())
    9533              :     {
    9534              :       /* Some internal entity of context.  Do by value.  */
    9535       447594 :       decl_value (decl, dep);
    9536       447594 :       return false;
    9537              :     }
    9538              : 
    9539      8694617 :   if (dep->get_entity_kind () == depset::EK_REDIRECT)
    9540              :     {
    9541              :       /* The DECL_TEMPLATE_RESULT of a partial specialization.
    9542              :          Write the partial specialization's template.  */
    9543        99918 :       depset *redirect = dep->deps[0];
    9544        99918 :       gcc_checking_assert (redirect->get_entity_kind () == depset::EK_PARTIAL);
    9545        99918 :       tpl = redirect->get_entity ();
    9546        99918 :       goto partial_template;
    9547              :     }
    9548              : 
    9549      8594699 :   if (streaming_p ())
    9550              :     {
    9551              :       /* Locate the entity.  */
    9552      1329738 :       unsigned index = dep->cluster;
    9553      1329738 :       unsigned import = 0;
    9554              : 
    9555      1329738 :       if (dep->is_import ())
    9556        10358 :         import = dep->section;
    9557      1319380 :       else if (CHECKING_P)
    9558              :         /* It should be what we put there.  */
    9559      1319380 :         gcc_checking_assert (index == ~import_entity_index (decl));
    9560              : 
    9561              : #if CHECKING_P
    9562        10358 :       gcc_assert (!import || importedness >= 0);
    9563              : #endif
    9564      1329738 :       i (tt_entity);
    9565      1329738 :       u (import);
    9566      1329738 :       u (index);
    9567              :     }
    9568              : 
    9569      8594699 :   int tag = insert (decl);
    9570      8594699 :   if (streaming_p () && dump (dumper::TREE))
    9571              :     {
    9572          529 :       char const *kind = "import";
    9573          529 :       module_state *from = this_module ();
    9574          529 :       if (dep->is_import ())
    9575              :         /* Rediscover the unremapped index.  */
    9576           78 :         from = import_entity_module (import_entity_index (decl));
    9577              :       else
    9578              :         {
    9579          451 :           tree o = get_originating_module_decl (decl);
    9580          451 :           o = STRIP_TEMPLATE (o);
    9581          902 :           kind = (DECL_LANG_SPECIFIC (o) && DECL_MODULE_PURVIEW_P (o)
    9582          451 :                   ? "purview" : "GMF");
    9583              :         }
    9584          529 :       dump ("Wrote %s:%d %C:%N@%M", kind,
    9585          529 :             tag, TREE_CODE (decl), decl, from);
    9586              :     }
    9587              : 
    9588      8594699 :   add_indirects (decl);
    9589              : 
    9590      8594699 :   return false;
    9591              : }
    9592              : 
    9593              : void
    9594     10296874 : trees_out::type_node (tree type)
    9595              : {
    9596     10296874 :   gcc_assert (TYPE_P (type));
    9597              : 
    9598     10296874 :   tree root = (TYPE_NAME (type)
    9599     10296874 :                ? TREE_TYPE (TYPE_NAME (type)) : TYPE_MAIN_VARIANT (type));
    9600     10296874 :   gcc_checking_assert (root);
    9601              : 
    9602     10296874 :   if (type != root)
    9603              :     {
    9604      2319183 :       if (streaming_p ())
    9605       483432 :         i (tt_variant_type);
    9606      2319183 :       tree_node (root);
    9607              : 
    9608      2319183 :       int flags = -1;
    9609              : 
    9610      2319183 :       if (TREE_CODE (type) == FUNCTION_TYPE
    9611      2319183 :           || TREE_CODE (type) == METHOD_TYPE)
    9612              :         {
    9613       547413 :           int quals = type_memfn_quals (type);
    9614       547413 :           int rquals = type_memfn_rqual (type);
    9615       547413 :           tree raises = TYPE_RAISES_EXCEPTIONS (type);
    9616       547413 :           bool late = TYPE_HAS_LATE_RETURN_TYPE (type);
    9617              : 
    9618       547413 :           if (raises != TYPE_RAISES_EXCEPTIONS (root)
    9619        17143 :               || rquals != type_memfn_rqual (root)
    9620        12421 :               || quals != type_memfn_quals (root)
    9621       559816 :               || late != TYPE_HAS_LATE_RETURN_TYPE (root))
    9622       547413 :             flags = rquals | (int (late) << 2) | (quals << 3);
    9623              :         }
    9624              :       else
    9625              :         {
    9626      1771770 :           if (TYPE_USER_ALIGN (type))
    9627        18429 :             flags = TYPE_ALIGN_RAW (type);
    9628              :         }
    9629              : 
    9630      2319183 :       if (streaming_p ())
    9631       483432 :         i (flags);
    9632              : 
    9633      2319183 :       if (flags < 0)
    9634              :         ;
    9635       565842 :       else if (TREE_CODE (type) == FUNCTION_TYPE
    9636       565842 :                || TREE_CODE (type) == METHOD_TYPE)
    9637              :         {
    9638       547413 :           tree raises = TYPE_RAISES_EXCEPTIONS (type);
    9639       547413 :           if (raises == TYPE_RAISES_EXCEPTIONS (root))
    9640        17143 :             raises = error_mark_node;
    9641       547413 :           tree_node (raises);
    9642              :         }
    9643              : 
    9644              :       /* build_type_attribute_variant creates a new TYPE_MAIN_VARIANT, so
    9645              :          variants should all have the same set of attributes.  */
    9646      2319183 :       gcc_checking_assert (TYPE_ATTRIBUTES (type)
    9647              :                            == TYPE_ATTRIBUTES (TYPE_MAIN_VARIANT (type)));
    9648              : 
    9649      2319183 :       if (streaming_p ())
    9650              :         {
    9651              :           /* Qualifiers.  */
    9652       483432 :           int rquals = cp_type_quals (root);
    9653       483432 :           int quals = cp_type_quals (type);
    9654       483432 :           if (quals == rquals)
    9655       223548 :             quals = -1;
    9656       483432 :           i (quals);
    9657              :         }
    9658              : 
    9659      2319183 :       if (ref_node (type) != WK_none)
    9660              :         {
    9661      2319183 :           int tag = insert (type);
    9662      2319183 :           if (streaming_p ())
    9663              :             {
    9664       483432 :               i (0);
    9665       483432 :               dump (dumper::TREE)
    9666          203 :                 && dump ("Wrote:%d variant type %C", tag, TREE_CODE (type));
    9667              :             }
    9668              :         }
    9669      2319183 :       return;
    9670              :     }
    9671              : 
    9672      7977691 :   if (tree name = TYPE_NAME (type))
    9673      3149202 :     if ((TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name))
    9674      2473605 :         || DECL_TEMPLATE_PARM_P (name)
    9675      1678185 :         || TREE_CODE (type) == RECORD_TYPE
    9676       297058 :         || TREE_CODE (type) == UNION_TYPE
    9677      3439401 :         || TREE_CODE (type) == ENUMERAL_TYPE)
    9678              :       {
    9679      2941393 :         gcc_checking_assert (DECL_P (name));
    9680              : 
    9681              :         /* We can meet template parms that we didn't meet in the
    9682              :            tpl_parms walk, because we're referring to a derived type
    9683              :            that was previously constructed from equivalent template
    9684              :            parms. */
    9685      2941393 :         if (streaming_p ())
    9686              :           {
    9687       203396 :             i (tt_typedef_type);
    9688       203396 :             dump (dumper::TREE)
    9689           59 :               && dump ("Writing %stypedef %C:%N",
    9690           59 :                        DECL_IMPLICIT_TYPEDEF_P (name) ? "implicit " : "",
    9691           59 :                        TREE_CODE (name), name);
    9692              :           }
    9693      2941393 :         tree_node (name);
    9694      2941393 :         if (streaming_p ())
    9695       203396 :           dump (dumper::TREE) && dump ("Wrote typedef %C:%N%S",
    9696           59 :                                        TREE_CODE (name), name, name);
    9697              : 
    9698              :         /* We'll have either visited this type or have newly discovered
    9699              :            that it's TU-local; either way we won't need to visit it again.  */
    9700      2941393 :         gcc_checking_assert (TREE_VISITED (type) || has_tu_local_dep (name));
    9701      2941393 :         return;
    9702              :       }
    9703              : 
    9704      5036298 :   if (TYPE_PTRMEMFUNC_P (type))
    9705              :     {
    9706              :       /* This is a distinct type node, masquerading as a structure. */
    9707         4674 :       tree fn_type = TYPE_PTRMEMFUNC_FN_TYPE (type);
    9708         4674 :       if (streaming_p ())
    9709         1319 :         i (tt_ptrmem_type);
    9710         4674 :       tree_node (fn_type);
    9711         4674 :       int tag = insert (type);
    9712         4674 :       if (streaming_p ())
    9713         1322 :         dump (dumper::TREE) && dump ("Written:%d ptrmem type", tag);
    9714         4674 :       return;
    9715              :     }
    9716              : 
    9717      5031624 :   if (streaming_p ())
    9718              :     {
    9719      1604573 :       u (tt_derived_type);
    9720      1604573 :       u (TREE_CODE (type));
    9721              :     }
    9722              : 
    9723      5031624 :   tree_node (TREE_TYPE (type));
    9724      5031624 :   switch (TREE_CODE (type))
    9725              :     {
    9726            0 :     default:
    9727              :       /* We should never meet a type here that is indescribable in
    9728              :          terms of other types.  */
    9729            0 :       gcc_unreachable ();
    9730              : 
    9731        76525 :     case ARRAY_TYPE:
    9732        76525 :       tree_node (TYPE_DOMAIN (type));
    9733        76525 :       if (streaming_p ())
    9734              :         /* Dependent arrays are constructed with TYPE_DEPENENT_P
    9735              :            already set.  */
    9736        24876 :         u (TYPE_DEPENDENT_P (type));
    9737              :       break;
    9738              : 
    9739              :     case COMPLEX_TYPE:
    9740              :       /* No additional data.  */
    9741              :       break;
    9742              : 
    9743           12 :     case BOOLEAN_TYPE:
    9744              :       /* A non-standard boolean type.  */
    9745           12 :       if (streaming_p ())
    9746            6 :         u (TYPE_PRECISION (type));
    9747              :       break;
    9748              : 
    9749        70927 :     case INTEGER_TYPE:
    9750        70927 :       if (TREE_TYPE (type))
    9751              :         {
    9752              :           /* A range type (representing an array domain).  */
    9753        67001 :           tree_node (TYPE_MIN_VALUE (type));
    9754        67001 :           tree_node (TYPE_MAX_VALUE (type));
    9755              :         }
    9756              :       else
    9757              :         {
    9758              :           /* A new integral type (representing a bitfield).  */
    9759         3926 :           if (streaming_p ())
    9760              :             {
    9761          975 :               unsigned prec = TYPE_PRECISION (type);
    9762          975 :               bool unsigned_p = TYPE_UNSIGNED (type);
    9763              : 
    9764          975 :               u ((prec << 1) | unsigned_p);
    9765              :             }
    9766              :         }
    9767              :       break;
    9768              : 
    9769      1102179 :     case METHOD_TYPE:
    9770      1102179 :     case FUNCTION_TYPE:
    9771      1102179 :       {
    9772      1102179 :         gcc_checking_assert (type_memfn_rqual (type) == REF_QUAL_NONE);
    9773              : 
    9774      1102179 :         tree arg_types = TYPE_ARG_TYPES (type);
    9775      1102179 :         if (TREE_CODE (type) == METHOD_TYPE)
    9776              :           {
    9777       697404 :             tree_node (TREE_TYPE (TREE_VALUE (arg_types)));
    9778       697404 :             arg_types = TREE_CHAIN (arg_types);
    9779              :           }
    9780      1102179 :         tree_node (arg_types);
    9781              :       }
    9782      1102179 :       break;
    9783              : 
    9784         1410 :     case OFFSET_TYPE:
    9785         1410 :       tree_node (TYPE_OFFSET_BASETYPE (type));
    9786         1410 :       break;
    9787              : 
    9788              :     case POINTER_TYPE:
    9789              :       /* No additional data.  */
    9790              :       break;
    9791              : 
    9792       857514 :     case REFERENCE_TYPE:
    9793       857514 :       if (streaming_p ())
    9794       189967 :         u (TYPE_REF_IS_RVALUE (type));
    9795              :       break;
    9796              : 
    9797      1012545 :     case DECLTYPE_TYPE:
    9798      1012545 :     case TYPEOF_TYPE:
    9799      1012545 :     case DEPENDENT_OPERATOR_TYPE:
    9800      1012545 :       tree_node (TYPE_VALUES_RAW (type));
    9801      1012545 :       if (TREE_CODE (type) == DECLTYPE_TYPE)
    9802              :         /* We stash a whole bunch of things into decltype's
    9803              :            flags.  */
    9804        80463 :         if (streaming_p ())
    9805        27427 :           tree_node_bools (type);
    9806              :       break;
    9807              : 
    9808         7893 :     case TRAIT_TYPE:
    9809         7893 :       tree_node (TRAIT_TYPE_KIND_RAW (type));
    9810         7893 :       tree_node (TRAIT_TYPE_TYPE1 (type));
    9811         7893 :       tree_node (TRAIT_TYPE_TYPE2 (type));
    9812         7893 :       break;
    9813              : 
    9814              :     case TYPE_ARGUMENT_PACK:
    9815              :       /* No additional data.  */
    9816              :       break;
    9817              : 
    9818       174392 :     case TYPE_PACK_EXPANSION:
    9819       174392 :       if (streaming_p ())
    9820        70884 :         u (PACK_EXPANSION_LOCAL_P (type));
    9821       348784 :       tree_node (PACK_EXPANSION_PARAMETER_PACKS (type));
    9822       174392 :       tree_node (PACK_EXPANSION_EXTRA_ARGS (type));
    9823       174392 :       break;
    9824              : 
    9825           31 :     case PACK_INDEX_TYPE:
    9826           31 :       tree_node (PACK_INDEX_PACK (type));
    9827           31 :       tree_node (PACK_INDEX_INDEX (type));
    9828           31 :       break;
    9829              : 
    9830       209858 :     case TYPENAME_TYPE:
    9831       209858 :       {
    9832       209858 :         tree_node (TYPE_CONTEXT (type));
    9833       209858 :         tree_node (DECL_NAME (TYPE_NAME (type)));
    9834       209858 :         tree_node (TYPENAME_TYPE_FULLNAME (type));
    9835       209858 :         if (streaming_p ())
    9836        73271 :           u (get_typename_tag (type));
    9837              :         }
    9838              :       break;
    9839              : 
    9840          240 :     case UNBOUND_CLASS_TEMPLATE:
    9841          240 :       {
    9842          240 :         tree decl = TYPE_NAME (type);
    9843          240 :         tree_node (DECL_CONTEXT (decl));
    9844          240 :         tree_node (DECL_NAME (decl));
    9845          240 :         tree_node (DECL_TEMPLATE_PARMS (decl));
    9846              :       }
    9847          240 :       break;
    9848              : 
    9849           42 :     case VECTOR_TYPE:
    9850           42 :       if (streaming_p ())
    9851              :         {
    9852           21 :           poly_uint64 nunits = TYPE_VECTOR_SUBPARTS (type);
    9853           42 :           for (unsigned ix = 0; ix != NUM_POLY_INT_COEFFS; ix++)
    9854           21 :             wu (nunits.coeffs[ix]);
    9855              :         }
    9856              :       break;
    9857              : 
    9858              :     case META_TYPE:
    9859              :       /* No additional data.  */
    9860              :       break;
    9861              : 
    9862            4 :     case SPLICE_SCOPE:
    9863            4 :       if (streaming_p ())
    9864            2 :         u (SPLICE_SCOPE_TYPE_P (type));
    9865            4 :       tree_node (SPLICE_SCOPE_EXPR (type));
    9866            4 :       break;
    9867              :     }
    9868              : 
    9869      5031624 :   tree_node (TYPE_ATTRIBUTES (type));
    9870              : 
    9871              :   /* We may have met the type during emitting the above.  */
    9872      5031624 :   if (ref_node (type) != WK_none)
    9873              :     {
    9874      4565583 :       int tag = insert (type);
    9875      4565583 :       if (streaming_p ())
    9876              :         {
    9877      1386208 :           i (0);
    9878      1386208 :           dump (dumper::TREE)
    9879          558 :             && dump ("Wrote:%d derived type %C", tag, TREE_CODE (type));
    9880              :         }
    9881              :     }
    9882              : 
    9883              :   return;
    9884              : }
    9885              : 
    9886              : /* T is (mostly*) a non-mergeable node that must be written by value.
    9887              :    The mergeable case is a BINFO, which are as-if DECLSs.   */
    9888              : 
    9889              : void
    9890     31350756 : trees_out::tree_value (tree t)
    9891              : {
    9892              :   /* We should never be writing a type by value.  tree_type should
    9893              :      have streamed it, or we're going via its TYPE_DECL.  */
    9894     31350756 :   gcc_checking_assert (!TYPE_P (t));
    9895              : 
    9896     31350756 :   if (DECL_P (t))
    9897              :     /* No template, type, var or function, except anonymous
    9898              :        non-context vars and types.  */
    9899       812995 :     gcc_checking_assert ((TREE_CODE (t) != TEMPLATE_DECL
    9900              :                           && (TREE_CODE (t) != TYPE_DECL
    9901              :                               || (DECL_ARTIFICIAL (t) && !DECL_CONTEXT (t)))
    9902              :                           && (TREE_CODE (t) != VAR_DECL
    9903              :                               || ((!DECL_NAME (t)
    9904              :                                    || IDENTIFIER_INTERNAL_P (DECL_NAME (t)))
    9905              :                                   && !DECL_CONTEXT (t)))
    9906              :                           && TREE_CODE (t) != FUNCTION_DECL));
    9907              : 
    9908     47295851 :   if (is_initial_scan () && EXPR_P (t))
    9909      6272074 :     dep_hash->add_dependent_adl_entities (t);
    9910              : 
    9911     31350756 :   if (streaming_p ())
    9912              :     {
    9913              :       /* A new node -> tt_node.  */
    9914     12475774 :       tree_val_count++;
    9915     12475774 :       i (tt_node);
    9916     12475774 :       start (t);
    9917     12475774 :       tree_node_bools (t);
    9918              :     }
    9919              : 
    9920     31350756 :   if (TREE_CODE (t) == TREE_BINFO)
    9921              :     /* Binfos are decl-like and need merging information.  */
    9922       230648 :     binfo_mergeable (t);
    9923              : 
    9924     31350756 :   int tag = insert (t, WK_value);
    9925     31350756 :   if (streaming_p ())
    9926     12475774 :     dump (dumper::TREE)
    9927         2823 :       && dump ("Writing tree:%d %C:%N", tag, TREE_CODE (t), t);
    9928              : 
    9929     31350756 :   int type_tag = 0;
    9930     31350756 :   tree type = NULL_TREE;
    9931     31350756 :   if (TREE_CODE (t) == TYPE_DECL)
    9932              :     {
    9933           28 :       type = TREE_TYPE (t);
    9934              : 
    9935              :       /* We only support a limited set of features for uncontexted types;
    9936              :          these are typically types created in the language-independent
    9937              :          parts of the frontend (such as ubsan).  */
    9938           28 :       gcc_checking_assert (RECORD_OR_UNION_TYPE_P (type)
    9939              :                            && TYPE_MAIN_VARIANT (type) == type
    9940              :                            && TYPE_NAME (type) == t
    9941              :                            && TYPE_STUB_DECL (type) == t
    9942              :                            && !TYPE_VFIELD (type)
    9943              :                            && !TYPE_BINFO (type)
    9944              :                            && !CLASS_TYPE_P (type));
    9945              : 
    9946           28 :       if (streaming_p ())
    9947              :         {
    9948           14 :           start (type);
    9949           14 :           tree_node_bools (type);
    9950              :         }
    9951              : 
    9952           28 :       type_tag = insert (type, WK_value);
    9953           28 :       if (streaming_p ())
    9954           14 :         dump (dumper::TREE)
    9955            0 :           && dump ("Writing type: %d %C:%N", type_tag,
    9956            0 :                    TREE_CODE (type), type);
    9957              :     }
    9958              : 
    9959     31350756 :   tree_node_vals (t);
    9960              : 
    9961     31350756 :   if (type)
    9962              :     {
    9963           28 :       tree_node_vals (type);
    9964           28 :       tree_node (TYPE_SIZE (type));
    9965           28 :       tree_node (TYPE_SIZE_UNIT (type));
    9966           28 :       chained_decls (TYPE_FIELDS (type));
    9967           28 :       if (streaming_p ())
    9968           14 :         dump (dumper::TREE)
    9969            0 :           && dump ("Written type:%d %C:%N", type_tag, TREE_CODE (type), type);
    9970              :     }
    9971              : 
    9972              :   /* For uncontexted VAR_DECLs we need to stream the definition so that
    9973              :      importers can recreate their value.  */
    9974     31350756 :   if (TREE_CODE (t) == VAR_DECL)
    9975              :     {
    9976          666 :       gcc_checking_assert (!DECL_NONTRIVIALLY_INITIALIZED_P (t));
    9977          666 :       tree_node (DECL_INITIAL (t));
    9978              :     }
    9979              : 
    9980     31350756 :   if (streaming_p ())
    9981     12478597 :     dump (dumper::TREE) && dump ("Written tree:%d %C:%N", tag, TREE_CODE (t), t);
    9982     31350756 : }
    9983              : 
    9984              : tree
    9985     11709858 : trees_in::tree_value ()
    9986              : {
    9987     11709858 :   tree t = start ();
    9988     11709858 :   if (!t || !tree_node_bools (t))
    9989            0 :     return NULL_TREE;
    9990              : 
    9991     11709858 :   tree existing = t;
    9992     11709858 :   if (TREE_CODE (t) == TREE_BINFO)
    9993              :     {
    9994        90203 :       tree type;
    9995        90203 :       unsigned ix = binfo_mergeable (&type);
    9996        90203 :       if (TYPE_BINFO (type))
    9997              :         {
    9998              :           /* We already have a definition, this must be a duplicate.  */
    9999        41374 :           dump (dumper::MERGE)
   10000          271 :             && dump ("Deduping binfo %N[%u]", type, ix);
   10001        41374 :           existing = TYPE_BINFO (type);
   10002        56682 :           while (existing && ix--)
   10003        15308 :             existing = TREE_CHAIN (existing);
   10004        41374 :           if (existing)
   10005        41374 :             register_duplicate (t, existing);
   10006              :           else
   10007              :             /* Error, mismatch -- diagnose in read_class_def's
   10008              :                checking.  */
   10009              :             existing = t;
   10010              :         }
   10011              :     }
   10012              : 
   10013              :   /* Insert into map.  */
   10014     11709858 :   int tag = insert (existing);
   10015     11709858 :   dump (dumper::TREE)
   10016         3677 :     && dump ("Reading tree:%d %C", tag, TREE_CODE (t));
   10017              : 
   10018     11709858 :   int type_tag = 0;
   10019     11709858 :   tree type = NULL_TREE;
   10020     11709858 :   if (TREE_CODE (t) == TYPE_DECL)
   10021              :     {
   10022           14 :       type = start ();
   10023           14 :       if (!type || !tree_node_bools (type))
   10024              :         t = NULL_TREE;
   10025              : 
   10026           14 :       type_tag = insert (type);
   10027           14 :       if (t)
   10028           14 :         dump (dumper::TREE)
   10029            0 :           && dump ("Reading type:%d %C", type_tag, TREE_CODE (type));
   10030              :     }
   10031              : 
   10032              :   if (!t)
   10033              :     {
   10034            0 : bail:
   10035            0 :       back_refs[~tag] = NULL_TREE;
   10036            0 :       if (type_tag)
   10037            0 :         back_refs[~type_tag] = NULL_TREE;
   10038            0 :       set_overrun ();
   10039            0 :       return NULL_TREE;
   10040              :     }
   10041              : 
   10042     11709858 :   if (!tree_node_vals (t))
   10043            0 :     goto bail;
   10044              : 
   10045     11709858 :   if (type)
   10046              :     {
   10047           14 :       if (!tree_node_vals (type))
   10048            0 :         goto bail;
   10049              : 
   10050           14 :       TYPE_SIZE (type) = tree_node ();
   10051           14 :       TYPE_SIZE_UNIT (type) = tree_node ();
   10052           14 :       TYPE_FIELDS (type) = chained_decls ();
   10053           14 :       if (get_overrun ())
   10054            0 :         goto bail;
   10055              : 
   10056           14 :       dump (dumper::TREE)
   10057            0 :         && dump ("Read type:%d %C:%N", type_tag, TREE_CODE (type), type);
   10058              :     }
   10059              : 
   10060     11709858 :   if (TREE_CODE (t) == VAR_DECL)
   10061              :     {
   10062          339 :       DECL_INITIAL (t) = tree_node ();
   10063          339 :       if (TREE_STATIC (t))
   10064            8 :         varpool_node::finalize_decl (t);
   10065              :     }
   10066              : 
   10067     11709858 :   if (TREE_CODE (t) == LAMBDA_EXPR
   10068     11709858 :       && CLASSTYPE_LAMBDA_EXPR (TREE_TYPE (t)))
   10069              :     {
   10070         1920 :       existing = CLASSTYPE_LAMBDA_EXPR (TREE_TYPE (t));
   10071         1920 :       back_refs[~tag] = existing;
   10072              :     }
   10073              : 
   10074     11713535 :   dump (dumper::TREE) && dump ("Read tree:%d %C:%N", tag, TREE_CODE (t), t);
   10075              : 
   10076     11709858 :   if (TREE_CODE (existing) == INTEGER_CST && !TREE_OVERFLOW (existing))
   10077              :     {
   10078       589438 :       existing = cache_integer_cst (t, true);
   10079       589438 :       back_refs[~tag] = existing;
   10080              :     }
   10081              : 
   10082              :   return existing;
   10083              : }
   10084              : 
   10085              : /* Whether DECL has a TU-local dependency in the hash.  */
   10086              : 
   10087              : bool
   10088       933584 : trees_out::has_tu_local_dep (tree decl) const
   10089              : {
   10090              :   /* Only the contexts of fields or enums remember that they're
   10091              :      TU-local.  */
   10092       933584 :   if (DECL_CONTEXT (decl)
   10093       933584 :       && (TREE_CODE (decl) == FIELD_DECL
   10094       933581 :           || TREE_CODE (decl) == CONST_DECL))
   10095            3 :     decl = TYPE_NAME (DECL_CONTEXT (decl));
   10096              : 
   10097       933584 :   depset *dep = dep_hash->find_dependency (decl);
   10098       933584 :   if (!dep)
   10099              :     {
   10100              :       /* This might be the DECL_TEMPLATE_RESULT of a TEMPLATE_DECL
   10101              :          which we found was TU-local and gave up early.  */
   10102        12069 :       int use_tpl = -1;
   10103        12069 :       if (tree ti = node_template_info (decl, use_tpl))
   10104         2013 :         dep = dep_hash->find_dependency (TI_TEMPLATE (ti));
   10105              :     }
   10106              : 
   10107       933584 :   return dep && dep->is_tu_local ();
   10108              : }
   10109              : 
   10110              : /* If T depends on a TU-local entity, return that decl.  */
   10111              : 
   10112              : tree
   10113          395 : trees_out::find_tu_local_decl (tree t)
   10114              : {
   10115              :   /* We need to have walked all deps first before we can check.  */
   10116          395 :   gcc_checking_assert (!is_initial_scan ());
   10117              : 
   10118          951 :   auto walker = [](tree *tp, int *walk_subtrees, void *data) -> tree
   10119              :     {
   10120          556 :       auto self = (trees_out *)data;
   10121              : 
   10122          556 :       tree decl = NULL_TREE;
   10123          556 :       if (TYPE_P (*tp))
   10124              :         {
   10125              :           /* A PMF type is a record type, which we otherwise wouldn't walk;
   10126              :              return whether the function type is TU-local.  */
   10127          370 :           if (TYPE_PTRMEMFUNC_P (*tp))
   10128              :             {
   10129            3 :               *walk_subtrees = 0;
   10130            3 :               return self->find_tu_local_decl (TYPE_PTRMEMFUNC_FN_TYPE (*tp));
   10131              :             }
   10132              :           else
   10133          367 :             decl = TYPE_MAIN_DECL (*tp);
   10134              :         }
   10135          186 :       else if (DECL_P (*tp))
   10136              :         decl = *tp;
   10137              : 
   10138          373 :       if (decl)
   10139              :         {
   10140              :           /* We found a DECL, this will tell us whether we're TU-local.  */
   10141           59 :           *walk_subtrees = 0;
   10142           59 :           return self->has_tu_local_dep (decl) ? decl : NULL_TREE;
   10143              :         }
   10144              :       return NULL_TREE;
   10145              :     };
   10146              : 
   10147              :   /* We need to walk without duplicates so that we step into the pointed-to
   10148              :      types of array types.  */
   10149          395 :   return cp_walk_tree_without_duplicates (&t, walker, this);
   10150              : }
   10151              : 
   10152              : /* Get the name for TU-local decl T to be used in diagnostics.  */
   10153              : 
   10154              : static tree
   10155          206 : name_for_tu_local_decl (tree t)
   10156              : {
   10157          206 :   int flags = (TFF_SCOPE | TFF_DECL_SPECIFIERS);
   10158          206 :   const char *str = decl_as_string (t, flags);
   10159          206 :   return get_identifier (str);
   10160              : }
   10161              : 
   10162              : /* Stream out tree node T.  We automatically create local back
   10163              :    references, which is essentially a single pass lisp
   10164              :    self-referential structure pretty-printer.  */
   10165              : 
   10166              : void
   10167    269627699 : trees_out::tree_node (tree t)
   10168              : {
   10169    269627699 :   dump.indent ();
   10170    269627699 :   walk_kind ref = ref_node (t);
   10171    269627699 :   if (ref == WK_none)
   10172    206463436 :     goto done;
   10173              : 
   10174              :   /* Find TU-local entities and intercept streaming to instead write a
   10175              :      placeholder value; this way we don't need to emit such decls.
   10176              :      We only need to do this when writing a definition of an entity
   10177              :      that we know names a TU-local entity.  */
   10178     73957245 :   if (!is_initial_scan () && writing_local_entities)
   10179              :     {
   10180          952 :       tree local_decl = NULL_TREE;
   10181          952 :       if (DECL_P (t) && has_tu_local_dep (t))
   10182              :         local_decl = t;
   10183              :       /* Consider a type to be TU-local if it refers to any TU-local decl,
   10184              :          no matter how deep.
   10185              : 
   10186              :          This worsens diagnostics slightly, as we often no longer point
   10187              :          directly to the at-fault entity when instantiating.  However, this
   10188              :          reduces the module size slightly and means that much less of pt.cc
   10189              :          needs to know about us.  */
   10190          848 :       else if (TYPE_P (t))
   10191          142 :         local_decl = find_tu_local_decl (t);
   10192          706 :       else if (EXPR_P (t))
   10193          250 :         local_decl = find_tu_local_decl (TREE_TYPE (t));
   10194              : 
   10195          496 :       if (local_decl)
   10196              :         {
   10197          158 :           int tag = insert (t, WK_value);
   10198          158 :           if (streaming_p ())
   10199              :             {
   10200          158 :               tu_local_count++;
   10201          158 :               i (tt_tu_local);
   10202          158 :               dump (dumper::TREE)
   10203            0 :                 && dump ("Writing TU-local entity:%d %C:%N",
   10204            0 :                          tag, TREE_CODE (t), t);
   10205              :             }
   10206          158 :           tree_node (name_for_tu_local_decl (local_decl));
   10207          158 :           if (state)
   10208          158 :             state->write_location (*this, DECL_SOURCE_LOCATION (local_decl));
   10209          158 :           goto done;
   10210              :         }
   10211              :     }
   10212              : 
   10213     63164105 :   if (ref != WK_normal)
   10214      1524107 :     goto skip_normal;
   10215              : 
   10216     61639998 :   if (TREE_CODE (t) == IDENTIFIER_NODE)
   10217              :     {
   10218              :       /* An identifier node -> tt_id, tt_conv_id, tt_anon_id, tt_lambda_id,
   10219              :          tt_internal_id.  */
   10220      7468138 :       int code = tt_id;
   10221      7468138 :       if (IDENTIFIER_ANON_P (t))
   10222        28274 :         code = IDENTIFIER_LAMBDA_P (t) ? tt_lambda_id : tt_anon_id;
   10223      7439864 :       else if (IDENTIFIER_INTERNAL_P (t))
   10224              :         code = tt_internal_id;
   10225      7439850 :       else if (IDENTIFIER_CONV_OP_P (t))
   10226        11178 :         code = tt_conv_id;
   10227              : 
   10228      7468138 :       if (streaming_p ())
   10229      1490269 :         i (code);
   10230              : 
   10231      7468138 :       if (code == tt_conv_id)
   10232              :         {
   10233        11178 :           tree type = TREE_TYPE (t);
   10234        11178 :           gcc_checking_assert (type || t == conv_op_identifier);
   10235        11178 :           tree_node (type);
   10236              :         }
   10237      7456960 :       else if (code == tt_id && streaming_p ())
   10238      1478048 :         str (IDENTIFIER_POINTER (t), IDENTIFIER_LENGTH (t));
   10239      5978912 :       else if (code == tt_internal_id && streaming_p ())
   10240            7 :         str (prefix_for_internal_label (t));
   10241              : 
   10242      7468138 :       int tag = insert (t);
   10243      7468138 :       if (streaming_p ())
   10244              :         {
   10245              :           /* We know the ordering of the 5 id tags.  */
   10246      1490269 :           static const char *const kinds[] =
   10247              :             {"", "conv_op ", "anon ", "lambda ", "internal "};
   10248      1490269 :           dump (dumper::TREE)
   10249         1074 :             && dump ("Written:%d %sidentifier:%N", tag,
   10250         1071 :                      kinds[code - tt_id],
   10251            3 :                      code == tt_conv_id ? TREE_TYPE (t) : t);
   10252              :         }
   10253      7468138 :       goto done;
   10254              :     }
   10255              : 
   10256     54171860 :   if (TREE_CODE (t) == TREE_BINFO)
   10257              :     {
   10258              :       /* A BINFO -> tt_binfo.
   10259              :          We must do this by reference.  We stream the binfo tree
   10260              :          itself when streaming its owning RECORD_TYPE.  That we got
   10261              :          here means the dominating type is not in this SCC.  */
   10262        63295 :       if (streaming_p ())
   10263         1955 :         i (tt_binfo);
   10264        63295 :       binfo_mergeable (t);
   10265        63295 :       gcc_checking_assert (!TREE_VISITED (t));
   10266        63295 :       int tag = insert (t);
   10267        63295 :       if (streaming_p ())
   10268         1955 :         dump (dumper::TREE) && dump ("Inserting binfo:%d %N", tag, t);
   10269        63295 :       goto done;
   10270              :     }
   10271              : 
   10272     54108565 :   if (TREE_CODE (t) == INTEGER_CST
   10273      3916683 :       && !TREE_OVERFLOW (t)
   10274     58025248 :       && TREE_CODE (TREE_TYPE (t)) == ENUMERAL_TYPE)
   10275              :     {
   10276              :       /* An integral constant of enumeral type.  See if it matches one
   10277              :          of the enumeration values.  */
   10278        35981 :       for (tree values = TYPE_VALUES (TREE_TYPE (t));
   10279       833589 :            values; values = TREE_CHAIN (values))
   10280              :         {
   10281       831687 :           tree decl = TREE_VALUE (values);
   10282       831687 :           if (tree_int_cst_equal (DECL_INITIAL (decl), t))
   10283              :             {
   10284        34079 :               if (streaming_p ())
   10285         9846 :                 u (tt_enum_value);
   10286        34079 :               tree_node (decl);
   10287        34121 :               dump (dumper::TREE) && dump ("Written enum value %N", decl);
   10288        34079 :               goto done;
   10289              :             }
   10290              :         }
   10291              :       /* It didn't match.  We'll write it a an explicit INTEGER_CST
   10292              :          node.  */
   10293              :     }
   10294              : 
   10295     54074486 :   if (TYPE_P (t))
   10296              :     {
   10297     10296874 :       type_node (t);
   10298     10296874 :       goto done;
   10299              :     }
   10300              : 
   10301     43777612 :   if (DECL_P (t))
   10302              :     {
   10303     13502135 :       if (DECL_TEMPLATE_PARM_P (t))
   10304              :         {
   10305      2119303 :           tpl_parm_value (t);
   10306      2119303 :           goto done;
   10307              :         }
   10308              : 
   10309     11382832 :       if (!DECL_CONTEXT (t))
   10310              :         {
   10311              :           /* There are a few cases of decls with no context.  We'll write
   10312              :              these by value, but first assert they are cases we expect.  */
   10313        25108 :           gcc_checking_assert (ref == WK_normal);
   10314        25108 :           switch (TREE_CODE (t))
   10315              :             {
   10316            0 :             default: gcc_unreachable ();
   10317              : 
   10318         9148 :             case LABEL_DECL:
   10319              :               /* CASE_LABEL_EXPRs contain uncontexted LABEL_DECLs.  */
   10320         9148 :               gcc_checking_assert (!DECL_NAME (t));
   10321              :               break;
   10322              : 
   10323          666 :             case VAR_DECL:
   10324              :               /* AGGR_INIT_EXPRs cons up anonymous uncontexted VAR_DECLs,
   10325              :                  and internal vars are created by sanitizers and
   10326              :                  __builtin_source_location.  */
   10327          666 :               gcc_checking_assert ((!DECL_NAME (t)
   10328              :                                     || IDENTIFIER_INTERNAL_P (DECL_NAME (t)))
   10329              :                                    && DECL_ARTIFICIAL (t));
   10330              :               break;
   10331              : 
   10332        15266 :             case PARM_DECL:
   10333              :               /* REQUIRES_EXPRs have a chain of uncontexted PARM_DECLS,
   10334              :                  and an implicit this parm in an NSDMI has no context.  */
   10335        15266 :               gcc_checking_assert (CONSTRAINT_VAR_P (t)
   10336              :                                    || DECL_NAME (t) == this_identifier);
   10337              :               break;
   10338              : 
   10339           28 :             case TYPE_DECL:
   10340              :               /* Some parts of the compiler need internal struct types;
   10341              :                  these types may not have an appropriate context to use.
   10342              :                  Walk the whole type (including its definition) by value.  */
   10343           28 :               gcc_checking_assert (DECL_ARTIFICIAL (t)
   10344              :                                    && TYPE_ARTIFICIAL (TREE_TYPE (t))
   10345              :                                    && RECORD_OR_UNION_TYPE_P (TREE_TYPE (t))
   10346              :                                    && !CLASS_TYPE_P (TREE_TYPE (t)));
   10347              :               break;
   10348              :             }
   10349        25108 :           mark_declaration (t, has_definition (t));
   10350        25108 :           goto by_value;
   10351              :         }
   10352              :     }
   10353              : 
   10354     30275477 :  skip_normal:
   10355     43157308 :   if (DECL_P (t) && !decl_node (t, ref))
   10356     11831660 :     goto done;
   10357              : 
   10358              :   /* Otherwise by value */
   10359     31350756 :  by_value:
   10360     31350756 :   tree_value (t);
   10361              : 
   10362    269627699 :  done:
   10363              :   /* And, breath out.  */
   10364    269627699 :   dump.outdent ();
   10365    269627699 : }
   10366              : 
   10367              : /* Stream in a tree node.  */
   10368              : 
   10369              : tree
   10370     95400860 : trees_in::tree_node (bool is_use)
   10371              : {
   10372     95400860 :   if (get_overrun ())
   10373              :     return NULL_TREE;
   10374              : 
   10375     95400860 :   dump.indent ();
   10376     95400860 :   int tag = i ();
   10377     95400860 :   tree res = NULL_TREE;
   10378     95400860 :   switch (tag)
   10379              :     {
   10380     31360759 :     default:
   10381              :       /* backref, pull it out of the map.  */
   10382     31360759 :       res = back_ref (tag);
   10383     31360759 :       break;
   10384              : 
   10385              :     case tt_null:
   10386              :       /* NULL_TREE.  */
   10387              :       break;
   10388              : 
   10389          158 :     case tt_tu_local:
   10390          158 :       {
   10391              :         /* A translation-unit-local entity.  */
   10392          158 :         res = make_node (TU_LOCAL_ENTITY);
   10393          158 :         int tag = insert (res);
   10394              : 
   10395          158 :         TU_LOCAL_ENTITY_NAME (res) = tree_node ();
   10396          158 :         TU_LOCAL_ENTITY_LOCATION (res) = state->read_location (*this);
   10397          158 :         dump (dumper::TREE) && dump ("Read TU-local entity:%d %N", tag, res);
   10398              :       }
   10399              :       break;
   10400              : 
   10401      7344014 :     case tt_fixed:
   10402              :       /* A fixed ref, find it in the fixed_ref array.   */
   10403      7344014 :       {
   10404      7344014 :         unsigned fix = u ();
   10405      7344014 :         if (fix < (*fixed_trees).length ())
   10406              :           {
   10407      7344014 :             res = (*fixed_trees)[fix];
   10408      7344014 :             dump (dumper::TREE) && dump ("Read fixed:%u %C:%N%S", fix,
   10409         5046 :                                          TREE_CODE (res), res, res);
   10410              :           }
   10411              : 
   10412      7338968 :         if (!res)
   10413            0 :           set_overrun ();
   10414              :       }
   10415              :       break;
   10416              : 
   10417        77611 :     case tt_parm:
   10418        77611 :       {
   10419        77611 :         tree fn = tree_node ();
   10420        77611 :         if (fn && TREE_CODE (fn) == FUNCTION_DECL)
   10421        77611 :           res = tree_node ();
   10422        77611 :         if (res)
   10423        77611 :           dump (dumper::TREE)
   10424           21 :             && dump ("Read %s reference %N",
   10425           21 :                      TREE_CODE (res) == PARM_DECL ? "parameter" : "result",
   10426              :                      res);
   10427              :       }
   10428              :       break;
   10429              : 
   10430     11709858 :     case tt_node:
   10431              :       /* A new node.  Stream it in.  */
   10432     11709858 :       res = tree_value ();
   10433     11709858 :       break;
   10434              : 
   10435      1193383 :     case tt_decl:
   10436              :       /* A new decl.  Stream it in.  */
   10437      1193383 :       res = decl_value ();
   10438      1193383 :       break;
   10439              : 
   10440       419910 :     case tt_tpl_parm:
   10441              :       /* A template parameter.  Stream it in.  */
   10442       419910 :       res = tpl_parm_value ();
   10443       419910 :       break;
   10444              : 
   10445      1207062 :     case tt_id:
   10446              :       /* An identifier node.  */
   10447      1207062 :       {
   10448      1207062 :         size_t l;
   10449      1207062 :         const char *chars = str (&l);
   10450      1207062 :         res = get_identifier_with_length (chars, l);
   10451      1207062 :         int tag = insert (res);
   10452      1207062 :         dump (dumper::TREE)
   10453         1488 :           && dump ("Read identifier:%d %N", tag, res);
   10454              :       }
   10455      1207062 :       break;
   10456              : 
   10457         3197 :     case tt_conv_id:
   10458              :       /* A conversion operator.  Get the type and recreate the
   10459              :          identifier.  */
   10460         3197 :       {
   10461         3197 :         tree type = tree_node ();
   10462         3197 :         if (!get_overrun ())
   10463              :           {
   10464         3197 :             res = type ? make_conv_op_name (type) : conv_op_identifier;
   10465         3197 :             int tag = insert (res);
   10466         3197 :             dump (dumper::TREE)
   10467           27 :               && dump ("Created conv_op:%d %S for %N", tag, res, type);
   10468              :           }
   10469              :       }
   10470              :       break;
   10471              : 
   10472         6552 :     case tt_anon_id:
   10473         6552 :     case tt_lambda_id:
   10474              :       /* An anonymous or lambda id.  */
   10475         6552 :       {
   10476         6552 :         res = make_anon_name ();
   10477         6552 :         if (tag == tt_lambda_id)
   10478         3878 :           IDENTIFIER_LAMBDA_P (res) = true;
   10479         6552 :         int tag = insert (res);
   10480         6555 :         dump (dumper::TREE)
   10481            3 :           && dump ("Read %s identifier:%d %N",
   10482            3 :                    IDENTIFIER_LAMBDA_P (res) ? "lambda" : "anon", tag, res);
   10483              :       }
   10484              :       break;
   10485              : 
   10486            8 :     case tt_internal_id:
   10487              :       /* An internal label.  */
   10488            8 :       {
   10489            8 :         const char *prefix = str ();
   10490            8 :         res = generate_internal_label (prefix);
   10491            8 :         int tag = insert (res);
   10492            8 :         dump (dumper::TREE)
   10493            1 :           && dump ("Read internal identifier:%d %N", tag, res);
   10494              :       }
   10495              :       break;
   10496              : 
   10497       190183 :     case tt_typedef_type:
   10498       190183 :       res = tree_node ();
   10499       190183 :       if (res)
   10500              :         {
   10501       190183 :           dump (dumper::TREE)
   10502           74 :             && dump ("Read %stypedef %C:%N",
   10503           74 :                      DECL_IMPLICIT_TYPEDEF_P (res) ? "implicit " : "",
   10504           74 :                      TREE_CODE (res), res);
   10505       190183 :           if (TREE_CODE (res) != TU_LOCAL_ENTITY)
   10506       190182 :             res = TREE_TYPE (res);
   10507              :         }
   10508              :       break;
   10509              : 
   10510      1461904 :     case tt_derived_type:
   10511              :       /* A type derived from some other type.  */
   10512      1461904 :       {
   10513      1461904 :         enum tree_code code = tree_code (u ());
   10514      1461904 :         res = tree_node ();
   10515              : 
   10516      1461904 :         switch (code)
   10517              :           {
   10518            0 :           default:
   10519            0 :             set_overrun ();
   10520            0 :             break;
   10521              : 
   10522        20774 :           case ARRAY_TYPE:
   10523        20774 :             {
   10524        20774 :               tree elt_type = res;
   10525        20774 :               tree domain = tree_node ();
   10526        20774 :               int dep = u ();
   10527        20774 :               if (!get_overrun ())
   10528              :                 {
   10529        20774 :                   res = build_cplus_array_type (elt_type, domain, dep);
   10530              :                   /* If we're an array of an incomplete imported type,
   10531              :                      save it for post-processing so that we can attempt
   10532              :                      to complete the type later if it will get a
   10533              :                      definition later in the cluster.  */
   10534        20774 :                   if (!dep
   10535        17989 :                       && !COMPLETE_TYPE_P (elt_type)
   10536           36 :                       && CLASS_TYPE_P (elt_type)
   10537           36 :                       && DECL_LANG_SPECIFIC (TYPE_NAME (elt_type))
   10538        20810 :                       && DECL_MODULE_IMPORT_P (TYPE_NAME (elt_type)))
   10539           36 :                     post_process_type (res);
   10540              :                 }
   10541              :             }
   10542              :             break;
   10543              : 
   10544          214 :           case COMPLEX_TYPE:
   10545          214 :             if (!get_overrun ())
   10546          214 :               res = build_complex_type (res);
   10547              :             break;
   10548              : 
   10549            9 :           case BOOLEAN_TYPE:
   10550            9 :             {
   10551            9 :               unsigned precision = u ();
   10552            9 :               if (!get_overrun ())
   10553            9 :                 res = build_nonstandard_boolean_type (precision);
   10554              :             }
   10555              :             break;
   10556              : 
   10557        18953 :           case INTEGER_TYPE:
   10558        18953 :             if (res)
   10559              :               {
   10560              :                 /* A range type (representing an array domain).  */
   10561        18042 :                 tree min = tree_node ();
   10562        18042 :                 tree max = tree_node ();
   10563              : 
   10564        18042 :                 if (!get_overrun ())
   10565        18042 :                   res = build_range_type (res, min, max);
   10566              :               }
   10567              :             else
   10568              :               {
   10569              :                 /* A new integral type (representing a bitfield).  */
   10570          911 :                 unsigned enc = u ();
   10571          911 :                 if (!get_overrun ())
   10572          911 :                   res = build_nonstandard_integer_type (enc >> 1, enc & 1);
   10573              :               }
   10574              :             break;
   10575              : 
   10576       414180 :           case FUNCTION_TYPE:
   10577       414180 :           case METHOD_TYPE:
   10578       414180 :             {
   10579       414180 :               tree klass =  code == METHOD_TYPE ? tree_node () : NULL_TREE;
   10580       414180 :               tree args = tree_node ();
   10581       414180 :               if (!get_overrun ())
   10582              :                 {
   10583       414180 :                   if (klass)
   10584       263371 :                     res = build_method_type_directly (klass, res, args);
   10585              :                   else
   10586       150809 :                     res = cp_build_function_type (res, args);
   10587              :                 }
   10588              :             }
   10589              :             break;
   10590              : 
   10591          255 :           case OFFSET_TYPE:
   10592          255 :             {
   10593          255 :               tree base = tree_node ();
   10594          255 :               if (!get_overrun ())
   10595          255 :                 res = build_offset_type (base, res);
   10596              :             }
   10597              :             break;
   10598              : 
   10599       198080 :           case POINTER_TYPE:
   10600       198080 :             if (!get_overrun ())
   10601       198080 :               res = build_pointer_type (res);
   10602              :             break;
   10603              : 
   10604       171231 :           case REFERENCE_TYPE:
   10605       171231 :             {
   10606       171231 :               bool rval = bool (u ());
   10607       171231 :               if (!get_overrun ())
   10608       171231 :                 res = cp_build_reference_type (res, rval);
   10609              :             }
   10610              :             break;
   10611              : 
   10612       444363 :           case DECLTYPE_TYPE:
   10613       444363 :           case TYPEOF_TYPE:
   10614       444363 :           case DEPENDENT_OPERATOR_TYPE:
   10615       444363 :             {
   10616       444363 :               tree expr = tree_node ();
   10617       444363 :               if (!get_overrun ())
   10618              :                 {
   10619       444363 :                   res = cxx_make_type (code);
   10620       444363 :                   TYPE_VALUES_RAW (res) = expr;
   10621       444363 :                   if (code == DECLTYPE_TYPE)
   10622        21380 :                     tree_node_bools (res);
   10623       444363 :                   SET_TYPE_STRUCTURAL_EQUALITY (res);
   10624              :                 }
   10625              :             }
   10626              :             break;
   10627              : 
   10628         2202 :           case TRAIT_TYPE:
   10629         2202 :             {
   10630         2202 :               tree kind = tree_node ();
   10631         2202 :               tree type1 = tree_node ();
   10632         2202 :               tree type2 = tree_node ();
   10633         2202 :               if (!get_overrun ())
   10634              :                 {
   10635         2202 :                   res = cxx_make_type (TRAIT_TYPE);
   10636         2202 :                   TRAIT_TYPE_KIND_RAW (res) = kind;
   10637         2202 :                   TRAIT_TYPE_TYPE1 (res) = type1;
   10638         2202 :                   TRAIT_TYPE_TYPE2 (res) = type2;
   10639         2202 :                   SET_TYPE_STRUCTURAL_EQUALITY (res);
   10640              :                 }
   10641              :             }
   10642              :             break;
   10643              : 
   10644        60893 :           case TYPE_ARGUMENT_PACK:
   10645        60893 :             if (!get_overrun ())
   10646              :               {
   10647        60893 :                 tree pack = cxx_make_type (TYPE_ARGUMENT_PACK);
   10648        60893 :                 ARGUMENT_PACK_ARGS (pack) = res;
   10649              :                 res = pack;
   10650              :               }
   10651              :             break;
   10652              : 
   10653        64239 :           case TYPE_PACK_EXPANSION:
   10654        64239 :             {
   10655        64239 :               bool local = u ();
   10656        64239 :               tree param_packs = tree_node ();
   10657        64239 :               tree extra_args = tree_node ();
   10658        64239 :               if (!get_overrun ())
   10659              :                 {
   10660        64239 :                   tree expn = cxx_make_type (TYPE_PACK_EXPANSION);
   10661        64239 :                   SET_TYPE_STRUCTURAL_EQUALITY (expn);
   10662        64239 :                   PACK_EXPANSION_PATTERN (expn) = res;
   10663       128478 :                   PACK_EXPANSION_PARAMETER_PACKS (expn) = param_packs;
   10664        64239 :                   PACK_EXPANSION_EXTRA_ARGS (expn) = extra_args;
   10665        64239 :                   PACK_EXPANSION_LOCAL_P (expn) = local;
   10666        64239 :                   res = expn;
   10667              :                 }
   10668              :             }
   10669              :             break;
   10670              : 
   10671           25 :           case PACK_INDEX_TYPE:
   10672           25 :             {
   10673           25 :               tree pack = tree_node ();
   10674           25 :               tree index = tree_node ();
   10675           25 :               if (!get_overrun ())
   10676           25 :                 res = make_pack_index (pack, index);
   10677              :             }
   10678              :             break;
   10679              : 
   10680        66371 :           case TYPENAME_TYPE:
   10681        66371 :             {
   10682        66371 :               tree ctx = tree_node ();
   10683        66371 :               tree name = tree_node ();
   10684        66371 :               tree fullname = tree_node ();
   10685        66371 :               enum tag_types tag_type = tag_types (u ());
   10686              : 
   10687        66371 :               if (!get_overrun ())
   10688        66371 :                 res = build_typename_type (ctx, name, fullname, tag_type);
   10689              :             }
   10690              :             break;
   10691              : 
   10692           52 :           case UNBOUND_CLASS_TEMPLATE:
   10693           52 :             {
   10694           52 :               tree ctx = tree_node ();
   10695           52 :               tree name = tree_node ();
   10696           52 :               tree parms = tree_node ();
   10697              : 
   10698           52 :               if (!get_overrun ())
   10699           52 :                 res = make_unbound_class_template_raw (ctx, name, parms);
   10700              :             }
   10701              :             break;
   10702              : 
   10703              :           case VECTOR_TYPE:
   10704              :             {
   10705              :               poly_uint64 nunits;
   10706           60 :               for (unsigned ix = 0; ix != NUM_POLY_INT_COEFFS; ix++)
   10707           30 :                 nunits.coeffs[ix] = wu ();
   10708           30 :               if (!get_overrun ())
   10709           30 :                 res = build_vector_type (res, nunits);
   10710              :             }
   10711              :             break;
   10712              : 
   10713           31 :           case META_TYPE:
   10714           31 :             if (!get_overrun ())
   10715           31 :               res = meta_info_type_node;
   10716              :             break;
   10717              : 
   10718            2 :           case SPLICE_SCOPE:
   10719            2 :             {
   10720            2 :               bool type = u ();
   10721            2 :               tree expr = tree_node ();
   10722              : 
   10723            2 :               if (!get_overrun ())
   10724            2 :                 res = make_splice_scope (expr, type);
   10725              :             }
   10726              :             break;
   10727              :           }
   10728              : 
   10729              :         /* In the exporting TU, a derived type with attributes was built by
   10730              :            build_type_attribute_variant as a distinct copy, with itself as
   10731              :            TYPE_MAIN_VARIANT.  We repeat that on import to get the version
   10732              :            without attributes as TYPE_CANONICAL.  */
   10733      1461904 :         if (tree attribs = tree_node ())
   10734        16810 :           res = cp_build_type_attribute_variant (res, attribs);
   10735              : 
   10736      1461904 :         int tag = i ();
   10737      1461904 :         if (!tag)
   10738              :           {
   10739      1250414 :             tag = insert (res);
   10740      1250414 :             if (res)
   10741      1250414 :               dump (dumper::TREE)
   10742          678 :                 && dump ("Created:%d derived type %C", tag, code);
   10743              :           }
   10744              :         else
   10745       211490 :           res = back_ref (tag);
   10746              :       }
   10747              :       break;
   10748              : 
   10749       420619 :     case tt_variant_type:
   10750              :       /* Variant of some type.  */
   10751       420619 :       {
   10752       420619 :         res = tree_node ();
   10753       420619 :         int flags = i ();
   10754       420619 :         if (get_overrun ())
   10755              :           ;
   10756       420619 :         else if (flags < 0)
   10757              :           /* No change.  */;
   10758       202283 :         else if (TREE_CODE (res) == FUNCTION_TYPE
   10759       202283 :                  || TREE_CODE (res) == METHOD_TYPE)
   10760              :           {
   10761       200696 :             cp_ref_qualifier rqual = cp_ref_qualifier (flags & 3);
   10762       200696 :             bool late = (flags >> 2) & 1;
   10763       200696 :             cp_cv_quals quals = cp_cv_quals (flags >> 3);
   10764              : 
   10765       200696 :             tree raises = tree_node ();
   10766       200696 :             if (raises == error_mark_node)
   10767         7338 :               raises = TYPE_RAISES_EXCEPTIONS (res);
   10768              : 
   10769       200696 :             res = build_cp_fntype_variant (res, rqual, raises, late);
   10770       200696 :             if (TREE_CODE (res) == FUNCTION_TYPE)
   10771        71013 :               res = apply_memfn_quals (res, quals, rqual);
   10772              :           }
   10773              :         else
   10774              :           {
   10775         1587 :             res = build_aligned_type (res, (1u << flags) >> 1);
   10776         1587 :             TYPE_USER_ALIGN (res) = true;
   10777              :           }
   10778              : 
   10779       420619 :         int quals = i ();
   10780       420619 :         if (quals >= 0 && !get_overrun ())
   10781       219500 :           res = cp_build_qualified_type (res, quals);
   10782              : 
   10783       420619 :         int tag = i ();
   10784       420619 :         if (!tag)
   10785              :           {
   10786       420619 :             tag = insert (res);
   10787       420619 :             if (res)
   10788       420619 :               dump (dumper::TREE)
   10789          292 :                 && dump ("Created:%d variant type %C", tag, TREE_CODE (res));
   10790              :           }
   10791              :         else
   10792            0 :           res = back_ref (tag);
   10793              :       }
   10794              :       break;
   10795              : 
   10796         5026 :     case tt_tinfo_var:
   10797         5026 :     case tt_tinfo_typedef:
   10798              :       /* A tinfo var or typedef.  */
   10799         5026 :       {
   10800         5026 :         bool is_var = tag == tt_tinfo_var;
   10801         5026 :         unsigned ix = u ();
   10802         5026 :         tree type = NULL_TREE;
   10803              : 
   10804         5026 :         if (is_var)
   10805              :           {
   10806         3062 :             tree name = tree_node ();
   10807         3062 :             type = tree_node ();
   10808              : 
   10809         3062 :             if (!get_overrun ())
   10810         3062 :               res = get_tinfo_decl_direct (type, name, int (ix));
   10811              :           }
   10812              :         else
   10813              :           {
   10814         1964 :             if (!get_overrun ())
   10815              :               {
   10816         1964 :                 type = get_pseudo_tinfo_type (ix);
   10817         1964 :                 res = TYPE_NAME (type);
   10818              :               }
   10819              :           }
   10820         5026 :         if (res)
   10821              :           {
   10822         5026 :             int tag = insert (res);
   10823         5026 :             dump (dumper::TREE)
   10824           36 :               && dump ("Created tinfo_%s:%d %S:%u for %N",
   10825              :                        is_var ? "var" : "decl", tag, res, ix, type);
   10826         5026 :             if (!is_var)
   10827              :               {
   10828         1964 :                 tag = insert (type);
   10829         1964 :                 dump (dumper::TREE)
   10830           12 :                   && dump ("Created tinfo_type:%d %u %N", tag, ix, type);
   10831              :               }
   10832              :           }
   10833              :       }
   10834              :       break;
   10835              : 
   10836         1087 :     case tt_ptrmem_type:
   10837              :       /* A pointer to member function.  */
   10838         1087 :       {
   10839         1087 :         tree type = tree_node ();
   10840         1087 :         if (type && TREE_CODE (type) == POINTER_TYPE
   10841         2174 :             && TREE_CODE (TREE_TYPE (type)) == METHOD_TYPE)
   10842              :           {
   10843         1087 :             res = build_ptrmemfunc_type (type);
   10844         1087 :             int tag = insert (res);
   10845         1090 :             dump (dumper::TREE) && dump ("Created:%d ptrmem type", tag);
   10846              :           }
   10847              :         else
   10848            0 :           set_overrun ();
   10849              :       }
   10850              :       break;
   10851              : 
   10852            7 :     case tt_nttp_var:
   10853              :       /* An NTTP object. */
   10854            7 :       {
   10855            7 :         tree init = tree_node ();
   10856            7 :         tree name = tree_node ();
   10857            7 :         if (!get_overrun ())
   10858              :           {
   10859              :             /* We don't want to check the initializer as that may require
   10860              :                name lookup, which could recursively start lazy loading.
   10861              :                Instead we know that INIT is already valid so we can just
   10862              :                apply that directly.  */
   10863            7 :             res = get_template_parm_object (init, name, /*check_init=*/false);
   10864            7 :             int tag = insert (res);
   10865            7 :             dump (dumper::TREE)
   10866            0 :               && dump ("Created nttp object:%d %N", tag, name);
   10867              :           }
   10868              :       }
   10869              :       break;
   10870              : 
   10871         7580 :     case tt_enum_value:
   10872              :       /* An enum const value.  */
   10873         7580 :       {
   10874         7580 :         if (tree decl = tree_node ())
   10875              :           {
   10876         7598 :             dump (dumper::TREE) && dump ("Read enum value %N", decl);
   10877         7580 :             res = DECL_INITIAL (decl);
   10878              :           }
   10879              : 
   10880         7580 :         if (!res)
   10881            0 :           set_overrun ();
   10882              :       }
   10883              :       break;
   10884              : 
   10885        13912 :     case tt_enum_decl:
   10886              :       /* An enum decl.  */
   10887        13912 :       {
   10888        13912 :         tree ctx = tree_node ();
   10889        13912 :         tree name = tree_node ();
   10890              : 
   10891        13912 :         if (!get_overrun ()
   10892        13912 :             && TREE_CODE (ctx) == ENUMERAL_TYPE)
   10893        13912 :           res = find_enum_member (ctx, name);
   10894              : 
   10895        13912 :         if (!res)
   10896            0 :           set_overrun ();
   10897              :         else
   10898              :           {
   10899        13912 :             int tag = insert (res);
   10900        13912 :             dump (dumper::TREE)
   10901           18 :               && dump ("Read enum decl:%d %C:%N", tag, TREE_CODE (res), res);
   10902              :           }
   10903              :       }
   10904              :       break;
   10905              : 
   10906         7816 :     case tt_data_member:
   10907              :       /* A data member.  */
   10908         7816 :       {
   10909         7816 :         tree ctx = tree_node ();
   10910         7816 :         tree name = tree_node ();
   10911              : 
   10912         7816 :         if (!get_overrun ()
   10913         7816 :             && RECORD_OR_UNION_TYPE_P (ctx))
   10914              :           {
   10915         7816 :             if (name)
   10916         6819 :               res = lookup_class_binding (ctx, name);
   10917              :             else
   10918          997 :               res = lookup_field_ident (ctx, u ());
   10919              : 
   10920         7816 :             if (!res
   10921         7816 :                 || (TREE_CODE (res) != FIELD_DECL
   10922         7816 :                     && TREE_CODE (res) != USING_DECL)
   10923        15632 :                 || DECL_CONTEXT (res) != ctx)
   10924              :               res = NULL_TREE;
   10925              :           }
   10926              : 
   10927         7816 :         if (!res)
   10928            0 :           set_overrun ();
   10929              :         else
   10930              :           {
   10931         7816 :             int tag = insert (res);
   10932         7816 :             dump (dumper::TREE)
   10933           26 :               && dump ("Read member:%d %C:%N", tag, TREE_CODE (res), res);
   10934              :           }
   10935              :       }
   10936              :       break;
   10937              : 
   10938         1689 :     case tt_binfo:
   10939              :       /* A BINFO.  Walk the tree of the dominating type.  */
   10940         1689 :       {
   10941         1689 :         tree type;
   10942         1689 :         unsigned ix = binfo_mergeable (&type);
   10943         1689 :         if (type)
   10944              :           {
   10945         1689 :             res = TYPE_BINFO (type);
   10946         1775 :             for (; ix && res; res = TREE_CHAIN (res))
   10947           86 :               ix--;
   10948         1689 :             if (!res)
   10949            0 :               set_overrun ();
   10950              :           }
   10951              : 
   10952         1689 :         if (get_overrun ())
   10953              :           break;
   10954              : 
   10955              :         /* Insert binfo into backreferences.  */
   10956         1689 :         tag = insert (res);
   10957         1689 :         dump (dumper::TREE) && dump ("Read binfo:%d %N", tag, res);
   10958              :       }
   10959         1689 :       break;
   10960              : 
   10961           82 :     case tt_vtable:
   10962           82 :       {
   10963           82 :         unsigned ix = u ();
   10964           82 :         tree ctx = tree_node ();
   10965           82 :         dump (dumper::TREE) && dump ("Reading vtable %N[%u]", ctx, ix);
   10966           82 :         if (TREE_CODE (ctx) == RECORD_TYPE && TYPE_LANG_SPECIFIC (ctx))
   10967           94 :           for (res = CLASSTYPE_VTABLES (ctx); res; res = DECL_CHAIN (res))
   10968           94 :             if (!ix--)
   10969              :               break;
   10970           82 :         if (!res)
   10971            0 :           set_overrun ();
   10972              :       }
   10973              :       break;
   10974              : 
   10975            0 :     case tt_thunk:
   10976            0 :       {
   10977            0 :         int fixed = i ();
   10978            0 :         tree target = tree_node ();
   10979            0 :         tree virt = tree_node ();
   10980              : 
   10981            0 :         for (tree thunk = DECL_THUNKS (target);
   10982            0 :              thunk; thunk = DECL_CHAIN (thunk))
   10983            0 :           if (THUNK_FIXED_OFFSET (thunk) == fixed
   10984            0 :               && !THUNK_VIRTUAL_OFFSET (thunk) == !virt
   10985            0 :               && (!virt
   10986            0 :                   || tree_int_cst_equal (virt, THUNK_VIRTUAL_OFFSET (thunk))))
   10987              :             {
   10988              :               res = thunk;
   10989              :               break;
   10990              :             }
   10991              : 
   10992            0 :         int tag = insert (res);
   10993            0 :         if (res)
   10994            0 :           dump (dumper::TREE)
   10995            0 :             && dump ("Read:%d thunk %N to %N", tag, DECL_NAME (res), target);
   10996              :         else
   10997            0 :           set_overrun ();
   10998              :       }
   10999              :       break;
   11000              : 
   11001       151524 :     case tt_clone_ref:
   11002       151524 :       {
   11003       151524 :         tree target = tree_node ();
   11004       151524 :         tree name = tree_node ();
   11005              : 
   11006       151524 :         if (DECL_P (target) && DECL_MAYBE_IN_CHARGE_CDTOR_P (target))
   11007              :           {
   11008       151524 :             tree clone;
   11009       235459 :             FOR_EVERY_CLONE (clone, target)
   11010       235459 :               if (DECL_NAME (clone) == name)
   11011              :                 {
   11012              :                   res = clone;
   11013              :                   break;
   11014              :                 }
   11015              :           }
   11016              : 
   11017              :         /* A clone might have a different vtable entry.  */
   11018       151524 :         if (res && DECL_VIRTUAL_P (res))
   11019         8543 :           DECL_VINDEX (res) = tree_node ();
   11020              : 
   11021       151524 :         if (!res)
   11022            0 :           set_overrun ();
   11023       151524 :         int tag = insert (res);
   11024       151524 :         if (res)
   11025       151524 :           dump (dumper::TREE)
   11026          230 :             && dump ("Read:%d clone %N of %N", tag, DECL_NAME (res), target);
   11027              :         else
   11028            0 :           set_overrun ();
   11029              :        }
   11030              :       break;
   11031              : 
   11032      1071698 :     case tt_entity:
   11033              :       /* Index into the entity table.  Perhaps not loaded yet!  */
   11034      1071698 :       {
   11035      1071698 :         unsigned origin = state->slurp->remap_module (u ());
   11036      1071698 :         unsigned ident = u ();
   11037      1071698 :         module_state *from = (*modules)[origin];
   11038              : 
   11039      1071698 :         if (!origin || ident >= from->entity_num)
   11040            0 :           set_overrun ();
   11041      1071698 :         if (!get_overrun ())
   11042              :           {
   11043      1071698 :             binding_slot *slot = &(*entity_ary)[from->entity_lwm + ident];
   11044      1071698 :             if (slot->is_lazy ())
   11045        51247 :               if (!from->lazy_load (ident, slot))
   11046            0 :                 set_overrun ();
   11047      1071698 :             res = *slot;
   11048              :           }
   11049              : 
   11050      1071698 :         if (res)
   11051              :           {
   11052      1071698 :             const char *kind = (origin != state->mod ? "Imported" : "Named");
   11053      1071698 :             int tag = insert (res);
   11054      1071698 :             dump (dumper::TREE)
   11055          605 :               && dump ("%s:%d %C:%N@%M", kind, tag, TREE_CODE (res),
   11056          605 :                        res, (*modules)[origin]);
   11057              : 
   11058      1071698 :             if (!add_indirects (res))
   11059              :               {
   11060            0 :                 set_overrun ();
   11061            0 :                 res = NULL_TREE;
   11062              :               }
   11063              :           }
   11064              :       }
   11065              :       break;
   11066              : 
   11067         3103 :     case tt_template:
   11068              :       /* A template.  */
   11069         3103 :       if (tree tpl = tree_node ())
   11070              :         {
   11071         3103 :           res = (TREE_CODE (tpl) == TU_LOCAL_ENTITY ?
   11072         3103 :                  tpl : DECL_TEMPLATE_RESULT (tpl));
   11073         3103 :           dump (dumper::TREE)
   11074            9 :             && dump ("Read template %C:%N", TREE_CODE (res), res);
   11075              :         }
   11076              :       break;
   11077              :     }
   11078              : 
   11079     95400860 :   if (is_use && !unused && res && DECL_P (res) && !TREE_USED (res))
   11080              :     {
   11081              :       /* Mark decl used as mark_used does -- we cannot call
   11082              :          mark_used in the middle of streaming, we only need a subset
   11083              :          of its functionality.   */
   11084       732722 :       TREE_USED (res) = true;
   11085              : 
   11086              :       /* And for structured bindings also the underlying decl.  */
   11087       732722 :       if (DECL_DECOMPOSITION_P (res) && !DECL_DECOMP_IS_BASE (res))
   11088         1336 :         TREE_USED (DECL_DECOMP_BASE (res)) = true;
   11089              : 
   11090       732722 :       if (DECL_CLONED_FUNCTION_P (res))
   11091         7161 :         TREE_USED (DECL_CLONED_FUNCTION (res)) = true;
   11092              :     }
   11093              : 
   11094     95400860 :   dump.outdent ();
   11095     95400860 :   return res;
   11096              : }
   11097              : 
   11098              : void
   11099      1760629 : trees_out::tpl_parms (tree parms, unsigned &tpl_levels)
   11100              : {
   11101      1760629 :   if (!parms)
   11102              :     return;
   11103              : 
   11104      1184534 :   if (TREE_VISITED (parms))
   11105              :     {
   11106       492125 :       ref_node (parms);
   11107       492125 :       return;
   11108              :     }
   11109              : 
   11110       692409 :   tpl_parms (TREE_CHAIN (parms), tpl_levels);
   11111              : 
   11112       692409 :   tree vec = TREE_VALUE (parms);
   11113       692409 :   unsigned len = TREE_VEC_LENGTH (vec);
   11114              :   /* Depth.  */
   11115       692409 :   int tag = insert (parms);
   11116       692409 :   if (streaming_p ())
   11117              :     {
   11118       185566 :       i (len + 1);
   11119       185632 :       dump (dumper::TREE)
   11120           66 :         && dump ("Writing template parms:%d level:%N length:%d",
   11121           66 :                  tag, TREE_PURPOSE (parms), len);
   11122              :     }
   11123       692409 :   tree_node (TREE_PURPOSE (parms));
   11124              : 
   11125      1904735 :   for (unsigned ix = 0; ix != len; ix++)
   11126              :     {
   11127      1212326 :       tree parm = TREE_VEC_ELT (vec, ix);
   11128      1212326 :       tree decl = TREE_VALUE (parm);
   11129              : 
   11130      1212326 :       gcc_checking_assert (DECL_TEMPLATE_PARM_P (decl));
   11131      1212326 :       if (CHECKING_P)
   11132      1212326 :         switch (TREE_CODE (decl))
   11133              :           {
   11134            0 :           default: gcc_unreachable ();
   11135              : 
   11136         3441 :           case TEMPLATE_DECL:
   11137         3441 :             gcc_assert ((TREE_CODE (TREE_TYPE (decl)) == TEMPLATE_TEMPLATE_PARM)
   11138              :                         && (TREE_CODE (DECL_TEMPLATE_RESULT (decl)) == TYPE_DECL)
   11139              :                         && (TYPE_NAME (TREE_TYPE (decl)) == decl));
   11140              :             break;
   11141              : 
   11142      1133051 :           case TYPE_DECL:
   11143      1133051 :             gcc_assert ((TREE_CODE (TREE_TYPE (decl)) == TEMPLATE_TYPE_PARM)
   11144              :                         && (TYPE_NAME (TREE_TYPE (decl)) == decl));
   11145              :             break;
   11146              : 
   11147        75834 :           case PARM_DECL:
   11148        75834 :             gcc_assert ((TREE_CODE (DECL_INITIAL (decl)) == TEMPLATE_PARM_INDEX)
   11149              :                         && (TREE_CODE (TEMPLATE_PARM_DECL (DECL_INITIAL (decl)))
   11150              :                             == CONST_DECL)
   11151              :                         && (DECL_TEMPLATE_PARM_P
   11152              :                             (TEMPLATE_PARM_DECL (DECL_INITIAL (decl)))));
   11153              :             break;
   11154              :           }
   11155              : 
   11156      1212326 :       tree_node (decl);
   11157      1212326 :       tree_node (TEMPLATE_PARM_CONSTRAINTS (parm));
   11158              :     }
   11159              : 
   11160       692409 :   tpl_levels++;
   11161              : }
   11162              : 
   11163              : tree
   11164       326105 : trees_in::tpl_parms (unsigned &tpl_levels)
   11165              : {
   11166       326105 :   tree parms = NULL_TREE;
   11167              : 
   11168       688230 :   while (int len = i ())
   11169              :     {
   11170       362125 :       if (len < 0)
   11171              :         {
   11172       196840 :           parms = back_ref (len);
   11173       196840 :           continue;
   11174              :         }
   11175              : 
   11176       165285 :       len -= 1;
   11177       165285 :       parms = tree_cons (NULL_TREE, NULL_TREE, parms);
   11178       165285 :       int tag = insert (parms);
   11179       165285 :       TREE_PURPOSE (parms) = tree_node ();
   11180              : 
   11181       165285 :       dump (dumper::TREE)
   11182          105 :         && dump ("Reading template parms:%d level:%N length:%d",
   11183          105 :                  tag, TREE_PURPOSE (parms), len);
   11184              : 
   11185       165285 :       tree vec = make_tree_vec (len);
   11186       435714 :       for (int ix = 0; ix != len; ix++)
   11187              :         {
   11188       270429 :           tree decl = tree_node ();
   11189       270429 :           if (!decl)
   11190              :             return NULL_TREE;
   11191              : 
   11192       270429 :           tree parm = build_tree_list (NULL, decl);
   11193       270429 :           TEMPLATE_PARM_CONSTRAINTS (parm) = tree_node ();
   11194              : 
   11195       270429 :           TREE_VEC_ELT (vec, ix) = parm;
   11196              :         }
   11197              : 
   11198       165285 :       TREE_VALUE (parms) = vec;
   11199       165285 :       tpl_levels++;
   11200              :     }
   11201              : 
   11202              :   return parms;
   11203              : }
   11204              : 
   11205              : void
   11206      1068220 : trees_out::tpl_parms_fini (tree tmpl, unsigned tpl_levels)
   11207              : {
   11208      1068220 :   for (tree parms = DECL_TEMPLATE_PARMS (tmpl);
   11209      1760629 :        tpl_levels--; parms = TREE_CHAIN (parms))
   11210              :     {
   11211       692409 :       tree vec = TREE_VALUE (parms);
   11212              : 
   11213       692409 :       tree_node (TREE_TYPE (vec));
   11214      1904735 :       for (unsigned ix = TREE_VEC_LENGTH (vec); ix--;)
   11215              :         {
   11216      1212326 :           tree parm = TREE_VEC_ELT (vec, ix);
   11217      1212326 :           tree dflt = TREE_PURPOSE (parm);
   11218      1212326 :           tree_node (dflt);
   11219              : 
   11220              :           /* Template template parameters need a context of their owning
   11221              :              template. This is quite tricky to infer correctly on stream-in
   11222              :              (see PR c++/98881) so we'll just provide it directly.  */
   11223      1212326 :           tree decl = TREE_VALUE (parm);
   11224      1212326 :           if (TREE_CODE (decl) == TEMPLATE_DECL)
   11225         3441 :             tree_node (DECL_CONTEXT (decl));
   11226              :         }
   11227              :     }
   11228      1068220 : }
   11229              : 
   11230              : bool
   11231       326105 : trees_in::tpl_parms_fini (tree tmpl, unsigned tpl_levels)
   11232              : {
   11233       326105 :   for (tree parms = DECL_TEMPLATE_PARMS (tmpl);
   11234       491390 :        tpl_levels--; parms = TREE_CHAIN (parms))
   11235              :     {
   11236       165285 :       tree vec = TREE_VALUE (parms);
   11237              : 
   11238       165285 :       TREE_TYPE (vec) = tree_node ();
   11239       435714 :       for (unsigned ix = TREE_VEC_LENGTH (vec); ix--;)
   11240              :         {
   11241       270429 :           tree parm = TREE_VEC_ELT (vec, ix);
   11242       270429 :           tree dflt = tree_node ();
   11243       270429 :           TREE_PURPOSE (parm) = dflt;
   11244              : 
   11245       270429 :           tree decl = TREE_VALUE (parm);
   11246       270429 :           if (TREE_CODE (decl) == TEMPLATE_DECL)
   11247          843 :             DECL_CONTEXT (decl) = tree_node ();
   11248              : 
   11249       270429 :           if (get_overrun ())
   11250              :             return false;
   11251              :         }
   11252              :     }
   11253              :   return true;
   11254              : }
   11255              : 
   11256              : /* PARMS is a LIST, one node per level.
   11257              :    TREE_VALUE is a TREE_VEC of parm info for that level.
   11258              :    each ELT is a TREE_LIST
   11259              :    TREE_VALUE is PARM_DECL, TYPE_DECL or TEMPLATE_DECL
   11260              :    TREE_PURPOSE is the default value.  */
   11261              : 
   11262              : void
   11263      1068220 : trees_out::tpl_header (tree tpl, unsigned *tpl_levels)
   11264              : {
   11265      1068220 :   tree parms = DECL_TEMPLATE_PARMS (tpl);
   11266      1068220 :   tpl_parms (parms, *tpl_levels);
   11267              : 
   11268              :   /* Mark end.  */
   11269      1068220 :   if (streaming_p ())
   11270       355632 :     u (0);
   11271              : 
   11272      1068220 :   if (*tpl_levels)
   11273       651176 :     tree_node (TEMPLATE_PARMS_CONSTRAINTS (parms));
   11274      1068220 : }
   11275              : 
   11276              : bool
   11277       326105 : trees_in::tpl_header (tree tpl, unsigned *tpl_levels)
   11278              : {
   11279       326105 :   tree parms = tpl_parms (*tpl_levels);
   11280       326105 :   if (!parms)
   11281              :     return false;
   11282              : 
   11283       326105 :   DECL_TEMPLATE_PARMS (tpl) = parms;
   11284              : 
   11285       326105 :   if (*tpl_levels)
   11286       163167 :     TEMPLATE_PARMS_CONSTRAINTS (parms) = tree_node ();
   11287              : 
   11288              :   return true;
   11289              : }
   11290              : 
   11291              : /* Stream skeleton parm nodes, with their flags, type & parm indices.
   11292              :    All the parms will have consecutive tags.  */
   11293              : 
   11294              : void
   11295      1478240 : trees_out::fn_parms_init (tree fn)
   11296              : {
   11297              :   /* First init them.  */
   11298      1478240 :   int base_tag = ref_num - 1;
   11299      1478240 :   int ix = 0;
   11300      1478240 :   for (tree parm = DECL_ARGUMENTS (fn);
   11301      4463897 :        parm; parm = DECL_CHAIN (parm), ix++)
   11302              :     {
   11303      2985657 :       if (streaming_p ())
   11304              :         {
   11305       995177 :           start (parm);
   11306       995177 :           tree_node_bools (parm);
   11307              :         }
   11308      2985657 :       int tag = insert (parm);
   11309      2985657 :       gcc_checking_assert (base_tag - ix == tag);
   11310              :     }
   11311              :   /* Mark the end.  */
   11312      1478240 :   if (streaming_p ())
   11313       492952 :     u (0);
   11314              : 
   11315              :   /* Now stream their contents.  */
   11316      1478240 :   ix = 0;
   11317      1478240 :   for (tree parm = DECL_ARGUMENTS (fn);
   11318      4463897 :        parm; parm = DECL_CHAIN (parm), ix++)
   11319              :     {
   11320      2985657 :       if (streaming_p ())
   11321       995177 :         dump (dumper::TREE)
   11322          222 :           && dump ("Writing parm:%d %u (%N) of %N",
   11323              :                    base_tag - ix, ix, parm, fn);
   11324      2985657 :       tree_node_vals (parm);
   11325              :     }
   11326              : 
   11327      1478240 :   if (!streaming_p ())
   11328              :     {
   11329              :       /* We must walk contract specifiers so the dependency graph is
   11330              :          complete.  */
   11331       985288 :       tree contract = get_fn_contract_specifiers (fn);
   11332      1970576 :       for (; contract; contract = TREE_CHAIN (contract))
   11333            0 :         tree_node (contract);
   11334              :     }
   11335              : 
   11336              :   /* Write a reference to contracts pre/post functions, if any, to avoid
   11337              :      regenerating them in importers.  */
   11338      1478240 :   tree_node (DECL_PRE_FN (fn));
   11339      1478240 :   tree_node (DECL_POST_FN (fn));
   11340      1478240 : }
   11341              : 
   11342              : /* Build skeleton parm nodes, read their flags, type & parm indices.  */
   11343              : 
   11344              : int
   11345       458495 : trees_in::fn_parms_init (tree fn)
   11346              : {
   11347       458495 :   int base_tag = ~(int)back_refs.length ();
   11348              : 
   11349       458495 :   tree *parm_ptr = &DECL_ARGUMENTS (fn);
   11350       458495 :   int ix = 0;
   11351      1383692 :   for (; int code = u (); ix++)
   11352              :     {
   11353       925197 :       tree parm = start (code);
   11354       925197 :       if (!tree_node_bools (parm))
   11355              :         return 0;
   11356              : 
   11357       925197 :       int tag = insert (parm);
   11358       925197 :       gcc_checking_assert (base_tag - ix == tag);
   11359       925197 :       *parm_ptr = parm;
   11360       925197 :       parm_ptr = &DECL_CHAIN (parm);
   11361       925197 :     }
   11362              : 
   11363       458495 :   ix = 0;
   11364       458495 :   for (tree parm = DECL_ARGUMENTS (fn);
   11365      1383692 :        parm; parm = DECL_CHAIN (parm), ix++)
   11366              :     {
   11367       925197 :       dump (dumper::TREE)
   11368          362 :         && dump ("Reading parm:%d %u (%N) of %N",
   11369              :                  base_tag - ix, ix, parm, fn);
   11370       925197 :       if (!tree_node_vals (parm))
   11371              :         return 0;
   11372              : 
   11373              :       /* Apply relevant attributes.
   11374              :          FIXME should probably use cplus_decl_attributes for this,
   11375              :          but it's not yet ready for modules.  */
   11376              : 
   11377              :       /* TREE_USED is deliberately not streamed for most declarations,
   11378              :          but needs to be set if we have the [[maybe_unused]] attribute.  */
   11379       925197 :       if (lookup_attribute ("unused", DECL_ATTRIBUTES (parm))
   11380       925197 :           || lookup_attribute ("maybe_unused", DECL_ATTRIBUTES (parm)))
   11381              :         {
   11382         2096 :           TREE_USED (parm) = true;
   11383         2096 :           DECL_READ_P (parm) = true;
   11384              :         }
   11385              :     }
   11386              : 
   11387              :   /* Reload references to contract functions, if any.  */
   11388       458495 :   tree pre_fn = tree_node ();
   11389       458495 :   tree post_fn = tree_node ();
   11390       458495 :   set_contract_functions (fn, pre_fn, post_fn);
   11391              : 
   11392       458495 :   return base_tag;
   11393              : }
   11394              : 
   11395              : /* Read the remaining parm node data.  Replace with existing (if
   11396              :    non-null) in the map.  */
   11397              : 
   11398              : void
   11399       458495 : trees_in::fn_parms_fini (int tag, tree fn, tree existing, bool is_defn)
   11400              : {
   11401       657677 :   tree existing_parm = existing ? DECL_ARGUMENTS (existing) : NULL_TREE;
   11402       458495 :   tree parms = DECL_ARGUMENTS (fn);
   11403      1383692 :   for (tree parm = parms; parm; parm = DECL_CHAIN (parm))
   11404              :     {
   11405       925197 :       if (existing_parm)
   11406              :         {
   11407       580997 :           if (is_defn && !DECL_SAVED_TREE (existing))
   11408              :             {
   11409              :               /* If we're about to become the definition, set the
   11410              :                  names of the parms from us.  */
   11411        14952 :               DECL_NAME (existing_parm) = DECL_NAME (parm);
   11412        14952 :               DECL_SOURCE_LOCATION (existing_parm) = DECL_SOURCE_LOCATION (parm);
   11413              : 
   11414              :               /* And some other flags important for codegen are only set
   11415              :                  by the definition.  */
   11416        14952 :               TREE_ADDRESSABLE (existing_parm) = TREE_ADDRESSABLE (parm);
   11417        14952 :               DECL_BY_REFERENCE (existing_parm) = DECL_BY_REFERENCE (parm);
   11418        14952 :               DECL_NONLOCAL (existing_parm) = DECL_NONLOCAL (parm);
   11419        14952 :               DECL_ARG_TYPE (existing_parm) = DECL_ARG_TYPE (parm);
   11420              : 
   11421              :               /* Invisiref parms had their types adjusted by cp_genericize. */
   11422        14952 :               if (DECL_BY_REFERENCE (parm))
   11423              :                 {
   11424            6 :                   TREE_TYPE (existing_parm) = TREE_TYPE (parm);
   11425            6 :                   relayout_decl (existing_parm);
   11426              :                 }
   11427              :             }
   11428              : 
   11429       393880 :           back_refs[~tag] = existing_parm;
   11430       393880 :           existing_parm = DECL_CHAIN (existing_parm);
   11431              :         }
   11432       925197 :       tag--;
   11433              :     }
   11434       458495 : }
   11435              : 
   11436              : /* Encode into KEY the position of the local type (class or enum)
   11437              :    declaration DECL within FN.  The position is encoded as the
   11438              :    index of the innermost BLOCK (numbered in BFS order) along with
   11439              :    the index within its BLOCK_VARS list.  */
   11440              : 
   11441              : void
   11442        15003 : trees_out::key_local_type (merge_key& key, tree decl, tree fn)
   11443              : {
   11444        15003 :   auto_vec<tree, 4> blocks;
   11445        15003 :   blocks.quick_push (DECL_INITIAL (fn));
   11446        15003 :   unsigned block_ix = 0;
   11447        71001 :   while (block_ix != blocks.length ())
   11448              :     {
   11449        27999 :       tree block = blocks[block_ix];
   11450        27999 :       unsigned decl_ix = 0;
   11451        82914 :       for (tree var = BLOCK_VARS (block); var; var = DECL_CHAIN (var))
   11452              :         {
   11453        69918 :           if (TREE_CODE (var) != TYPE_DECL)
   11454        42837 :             continue;
   11455        27081 :           if (var == decl)
   11456              :             {
   11457        15003 :               key.index = (block_ix << 10) | decl_ix;
   11458        15003 :               return;
   11459              :             }
   11460        12078 :           ++decl_ix;
   11461              :         }
   11462        27123 :       for (tree sub = BLOCK_SUBBLOCKS (block); sub; sub = BLOCK_CHAIN (sub))
   11463        14127 :         blocks.safe_push (sub);
   11464        12996 :       ++block_ix;
   11465              :     }
   11466              : 
   11467              :   /* Not-found value.  */
   11468            0 :   key.index = 1023;
   11469        15003 : }
   11470              : 
   11471              : /* Look up the local type corresponding at the position encoded by
   11472              :    KEY within FN and named NAME.  */
   11473              : 
   11474              : tree
   11475         4196 : trees_in::key_local_type (const merge_key& key, tree fn, tree name)
   11476              : {
   11477         4196 :   if (!DECL_INITIAL (fn))
   11478              :     return NULL_TREE;
   11479              : 
   11480         1892 :   const unsigned block_pos = key.index >> 10;
   11481         1892 :   const unsigned decl_pos = key.index & 1023;
   11482              : 
   11483         1892 :   if (decl_pos == 1023)
   11484              :     return NULL_TREE;
   11485              : 
   11486         1892 :   auto_vec<tree, 4> blocks;
   11487         1892 :   blocks.quick_push (DECL_INITIAL (fn));
   11488         1892 :   unsigned block_ix = 0;
   11489         8658 :   while (block_ix != blocks.length ())
   11490              :     {
   11491         3383 :       tree block = blocks[block_ix];
   11492         3383 :       if (block_ix == block_pos)
   11493              :         {
   11494         1892 :           unsigned decl_ix = 0;
   11495         5138 :           for (tree var = BLOCK_VARS (block); var; var = DECL_CHAIN (var))
   11496              :             {
   11497         5138 :               if (TREE_CODE (var) != TYPE_DECL)
   11498         2268 :                 continue;
   11499              :               /* Prefer using the identifier as the key for more robustness
   11500              :                  to ODR violations, except for anonymous types since their
   11501              :                  compiler-generated identifiers aren't stable.  */
   11502         5740 :               if (IDENTIFIER_ANON_P (name)
   11503         2870 :                   ? decl_ix == decl_pos
   11504          323 :                   : DECL_NAME (var) == name)
   11505              :                 return var;
   11506          978 :               ++decl_ix;
   11507              :             }
   11508              :           return NULL_TREE;
   11509              :         }
   11510         3091 :       for (tree sub = BLOCK_SUBBLOCKS (block); sub; sub = BLOCK_CHAIN (sub))
   11511         1600 :         blocks.safe_push (sub);
   11512         1491 :       ++block_ix;
   11513              :     }
   11514              : 
   11515              :   return NULL_TREE;
   11516         1892 : }
   11517              : 
   11518              : /* DEP is the depset of some decl we're streaming by value.  Determine
   11519              :    the merging behaviour.  */
   11520              : 
   11521              : merge_kind
   11522      3685272 : trees_out::get_merge_kind (tree decl, depset *dep)
   11523              : {
   11524      3685272 :   if (!dep)
   11525              :     {
   11526       721330 :       if (VAR_OR_FUNCTION_DECL_P (decl))
   11527              :         {
   11528              :           /* Any var or function with template info should have DEP.  */
   11529       390420 :           gcc_checking_assert (!DECL_LANG_SPECIFIC (decl)
   11530              :                                || !DECL_TEMPLATE_INFO (decl));
   11531       390420 :           if (DECL_LOCAL_DECL_P (decl))
   11532              :             return MK_unique;
   11533              :         }
   11534              : 
   11535              :       /* Either unique, or some member of a class that cannot have an
   11536              :          out-of-class definition.  For instance a FIELD_DECL.  */
   11537       721052 :       tree ctx = CP_DECL_CONTEXT (decl);
   11538       721052 :       if (TREE_CODE (ctx) == FUNCTION_DECL)
   11539              :         {
   11540              :           /* USING_DECLs and NAMESPACE_DECLs cannot have DECL_TEMPLATE_INFO --
   11541              :              this isn't permitting them to have one.   */
   11542       447334 :           gcc_checking_assert (TREE_CODE (decl) == USING_DECL
   11543              :                                || TREE_CODE (decl) == NAMESPACE_DECL
   11544              :                                || !DECL_LANG_SPECIFIC (decl)
   11545              :                                || !DECL_TEMPLATE_INFO (decl));
   11546              : 
   11547              :           return MK_unique;
   11548              :         }
   11549              : 
   11550       273718 :       if (TREE_CODE (decl) == TEMPLATE_DECL
   11551       273718 :           && DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (decl))
   11552              :         return MK_local_friend;
   11553              : 
   11554       273718 :       gcc_checking_assert (TYPE_P (ctx));
   11555              : 
   11556              :       /* Internal-only types will not need to dedup their members.  */
   11557       273718 :       if (!DECL_CONTEXT (TYPE_NAME (ctx)))
   11558              :         return MK_unique;
   11559              : 
   11560       273662 :       if (TREE_CODE (decl) == USING_DECL)
   11561              :         return MK_field;
   11562              : 
   11563       178505 :       if (TREE_CODE (decl) == FIELD_DECL)
   11564              :         {
   11565       128175 :           if (DECL_NAME (decl))
   11566              :             {
   11567              :               /* Anonymous FIELD_DECLs have a NULL name.  */
   11568       102482 :               gcc_checking_assert (!IDENTIFIER_ANON_P (DECL_NAME (decl)));
   11569              :               return MK_named;
   11570              :             }
   11571              : 
   11572        25693 :           if (walking_bit_field_unit)
   11573              :             {
   11574              :               /* The underlying storage unit for a bitfield.  We do not
   11575              :                  need to dedup it, because it's only reachable through
   11576              :                  the bitfields it represents.  And those are deduped.  */
   11577              :               // FIXME: Is that assertion correct -- do we ever fish it
   11578              :               // out and put it in an expr?
   11579          446 :               gcc_checking_assert (!DECL_NAME (decl)
   11580              :                                    && !RECORD_OR_UNION_TYPE_P (TREE_TYPE (decl))
   11581              :                                    && !DECL_BIT_FIELD_REPRESENTATIVE (decl));
   11582          446 :               gcc_checking_assert ((TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
   11583              :                                     ? TREE_CODE (TREE_TYPE (TREE_TYPE (decl)))
   11584              :                                     : TREE_CODE (TREE_TYPE (decl)))
   11585              :                                    == INTEGER_TYPE);
   11586              :               return MK_unique;
   11587              :             }
   11588              : 
   11589              :           return MK_field;
   11590              :         }
   11591              : 
   11592        50330 :       if (TREE_CODE (decl) == CONST_DECL)
   11593              :         return MK_named;
   11594              : 
   11595         8442 :       if (TREE_CODE (decl) == VAR_DECL
   11596         8442 :           && DECL_VTABLE_OR_VTT_P (decl))
   11597              :         return MK_vtable;
   11598              : 
   11599         1400 :       if (DECL_THUNK_P (decl))
   11600              :         /* Thunks are unique-enough, because they're only referenced
   11601              :            from the vtable.  And that's either new (so we want the
   11602              :            thunks), or it's a duplicate (so it will be dropped).  */
   11603              :         return MK_unique;
   11604              : 
   11605              :       /* There should be no other cases.  */
   11606            0 :       gcc_unreachable ();
   11607              :     }
   11608              : 
   11609      2963942 :   gcc_checking_assert (TREE_CODE (decl) != FIELD_DECL
   11610              :                        && TREE_CODE (decl) != USING_DECL
   11611              :                        && TREE_CODE (decl) != CONST_DECL);
   11612              : 
   11613      2963942 :   if (is_key_order ())
   11614              :     {
   11615              :       /* When doing the mergeablilty graph, there's an indirection to
   11616              :          the actual depset.  */
   11617       987827 :       gcc_assert (dep->is_special ());
   11618       987827 :       dep = dep->deps[0];
   11619              :     }
   11620              : 
   11621      2963942 :   gcc_checking_assert (decl == dep->get_entity ());
   11622              : 
   11623      2963942 :   merge_kind mk = MK_named;
   11624      2963942 :   switch (dep->get_entity_kind ())
   11625              :     {
   11626            0 :     default:
   11627            0 :       gcc_unreachable ();
   11628              : 
   11629              :     case depset::EK_PARTIAL:
   11630              :       mk = MK_partial;
   11631              :       break;
   11632              : 
   11633      1701065 :     case depset::EK_DECL:
   11634      1701065 :       {
   11635      1701065 :         tree ctx = CP_DECL_CONTEXT (decl);
   11636              : 
   11637      1701065 :         switch (TREE_CODE (ctx))
   11638              :           {
   11639            0 :           default:
   11640            0 :             gcc_unreachable ();
   11641              : 
   11642        15267 :           case FUNCTION_DECL:
   11643        15267 :             gcc_checking_assert
   11644              :               (DECL_IMPLICIT_TYPEDEF_P (STRIP_TEMPLATE (decl)));
   11645              : 
   11646        15267 :             if (has_definition (ctx))
   11647              :               mk = MK_local_type;
   11648              :             else
   11649              :               /* We're not providing a definition of the context to key
   11650              :                  the local type into; use the keyed map instead.  */
   11651         1018 :               mk = MK_keyed;
   11652              :             break;
   11653              : 
   11654      1685798 :           case RECORD_TYPE:
   11655      1685798 :           case UNION_TYPE:
   11656      1685798 :           case NAMESPACE_DECL:
   11657      1685798 :             if (DECL_NAME (decl) == as_base_identifier)
   11658              :               {
   11659              :                 mk = MK_as_base;
   11660              :                 break;
   11661              :               }
   11662              : 
   11663              :             /* A lambda may have a class as its context, even though it
   11664              :                isn't a member in the traditional sense; see the test
   11665              :                g++.dg/modules/lambda-6_a.C.  */
   11666      2112173 :             if (DECL_IMPLICIT_TYPEDEF_P (STRIP_TEMPLATE (decl))
   11667      1838606 :                 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
   11668              :               {
   11669          790 :                 if (get_keyed_decl_scope (decl))
   11670              :                   mk = MK_keyed;
   11671              :                 else
   11672              :                   /* Lambdas not attached to any mangling scope are TU-local
   11673              :                      and so cannot be deduplicated.  */
   11674       450027 :                   mk = MK_unique;
   11675              :                 break;
   11676              :               }
   11677              : 
   11678      1608478 :             if (TREE_CODE (decl) == TEMPLATE_DECL
   11679      1608478 :                 ? DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (decl)
   11680       705465 :                 : decl_specialization_friend_p (decl))
   11681              :               {
   11682              :                 mk = MK_local_friend;
   11683              :                 break;
   11684              :               }
   11685              : 
   11686      1588429 :             if (DECL_DECOMPOSITION_P (decl))
   11687              :               {
   11688              :                 mk = MK_unique;
   11689              :                 break;
   11690              :               }
   11691              : 
   11692      1587952 :             if (IDENTIFIER_ANON_P (DECL_NAME (decl)))
   11693              :               {
   11694        26840 :                 if (RECORD_OR_UNION_TYPE_P (ctx))
   11695              :                   mk = MK_field;
   11696         1141 :                 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
   11697         1141 :                          && UNSCOPED_ENUM_P (TREE_TYPE (decl))
   11698         2282 :                          && TYPE_VALUES (TREE_TYPE (decl)))
   11699              :                   /* Keyed by first enum value, and underlying type.  */
   11700              :                   mk = MK_enum;
   11701              :                 else
   11702              :                   /* No way to merge it, it is an ODR land-mine.  */
   11703              :                   mk = MK_unique;
   11704              :               }
   11705              :           }
   11706              :       }
   11707              :       break;
   11708              : 
   11709      1212903 :     case depset::EK_SPECIALIZATION:
   11710      1212903 :       {
   11711      1212903 :         gcc_checking_assert (dep->is_special ());
   11712              : 
   11713      1212903 :         if (TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
   11714              :           /* An block-scope classes of templates are themselves
   11715              :              templates.  */
   11716         4926 :           gcc_checking_assert (DECL_IMPLICIT_TYPEDEF_P (decl));
   11717              : 
   11718      1212903 :         if (dep->is_friend_spec ())
   11719              :           mk = MK_friend_spec;
   11720      1212903 :         else if (dep->is_type_spec ())
   11721              :           mk = MK_type_spec;
   11722              :         else
   11723       853986 :           mk = MK_decl_spec;
   11724              : 
   11725      1212903 :         if (TREE_CODE (decl) == TEMPLATE_DECL)
   11726              :           {
   11727        95844 :             spec_entry *entry = reinterpret_cast <spec_entry *> (dep->deps[0]);
   11728        95844 :             if (TREE_CODE (entry->spec) != TEMPLATE_DECL)
   11729         9915 :               mk = merge_kind (mk | MK_tmpl_tmpl_mask);
   11730              :           }
   11731              :       }
   11732              :       break;
   11733              :     }
   11734              : 
   11735              :   return mk;
   11736              : }
   11737              : 
   11738              : 
   11739              : /* The container of DECL -- not necessarily its context!  */
   11740              : 
   11741              : tree
   11742      3685272 : trees_out::decl_container (tree decl)
   11743              : {
   11744      3685272 :   int use_tpl;
   11745      3685272 :   tree tpl = NULL_TREE;
   11746      3685272 :   if (tree template_info = node_template_info (decl, use_tpl))
   11747      1271356 :     tpl = TI_TEMPLATE (template_info);
   11748      3685272 :   if (tpl == decl)
   11749            0 :     tpl = nullptr;
   11750              : 
   11751              :   /* Stream the template we're instantiated from.  */
   11752      3685272 :   tree_node (tpl);
   11753              : 
   11754      3685272 :   tree container = NULL_TREE;
   11755      3685272 :   if (TREE_CODE (decl) == TEMPLATE_DECL
   11756      3685272 :       ? DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (decl)
   11757      2623100 :       : decl_specialization_friend_p (decl))
   11758        20049 :     container = DECL_CHAIN (decl);
   11759              :   else
   11760      3665223 :     container = CP_DECL_CONTEXT (decl);
   11761              : 
   11762      3685272 :   if (TYPE_P (container))
   11763      2163675 :     container = TYPE_NAME (container);
   11764              : 
   11765      3685272 :   tree_node (container);
   11766              : 
   11767      3685272 :   return container;
   11768              : }
   11769              : 
   11770              : tree
   11771      1193383 : trees_in::decl_container ()
   11772              : {
   11773              :   /* The maybe-template.  */
   11774      1193383 :   (void)tree_node ();
   11775              : 
   11776      1193383 :   tree container = tree_node ();
   11777              : 
   11778      1193383 :   return container;
   11779              : }
   11780              : 
   11781              : /* Gets a 2-bit discriminator to distinguish coroutine actor or destroy
   11782              :    functions from a normal function.  */
   11783              : 
   11784              : static int
   11785      1063094 : get_coroutine_discriminator (tree inner)
   11786              : {
   11787      1063094 :   if (DECL_COROUTINE_P (inner))
   11788           66 :     if (tree ramp = DECL_RAMP_FN (inner))
   11789              :       {
   11790           18 :         if (DECL_ACTOR_FN (ramp) == inner)
   11791              :           return 1;
   11792            9 :         else if (DECL_DESTROY_FN (ramp) == inner)
   11793              :           return 2;
   11794              :         else
   11795            0 :           gcc_unreachable ();
   11796              :       }
   11797              :   return 0;
   11798              : }
   11799              : 
   11800              : /* Write out key information about a mergeable DEP.  Does not write
   11801              :    the contents of DEP itself.  The context has already been
   11802              :    written.  The container has already been streamed.  */
   11803              : 
   11804              : void
   11805      3685272 : trees_out::key_mergeable (int tag, merge_kind mk, tree decl, tree inner,
   11806              :                           tree container, depset *dep)
   11807              : {
   11808      3685272 :   if (dep && is_key_order ())
   11809              :     {
   11810       987827 :       gcc_checking_assert (dep->is_special ());
   11811       987827 :       dep = dep->deps[0];
   11812              :     }
   11813              : 
   11814      3685272 :   if (streaming_p ())
   11815      1340504 :     dump (dumper::MERGE)
   11816         1101 :       && dump ("Writing:%d's %s merge key (%s) %C:%N", tag, merge_kind_name[mk],
   11817          993 :                dep ? dep->entity_kind_name () : "contained",
   11818         1101 :                TREE_CODE (decl), decl);
   11819              : 
   11820              :   /* Now write the locating information. */
   11821      3685272 :   if (mk & MK_template_mask)
   11822              :     {
   11823              :       /* Specializations are located via their originating template,
   11824              :          and the set of template args they specialize.  */
   11825      1212903 :       gcc_checking_assert (dep && dep->is_special ());
   11826      1212903 :       spec_entry *entry = reinterpret_cast <spec_entry *> (dep->deps[0]);
   11827              : 
   11828      1212903 :       tree_node (entry->tmpl);
   11829      1212903 :       tree_node (entry->args);
   11830      1212903 :       if (mk & MK_tmpl_decl_mask)
   11831       853986 :         if (flag_concepts && TREE_CODE (inner) == VAR_DECL)
   11832              :           {
   11833              :             /* Variable template partial specializations might need
   11834              :                constraints (see spec_hasher::equal).  It's simpler to
   11835              :                write NULL when we don't need them.  */
   11836        18957 :             tree constraints = NULL_TREE;
   11837              : 
   11838        18957 :             if (uses_template_parms (entry->args))
   11839          531 :               constraints = get_constraints (inner);
   11840        18957 :             tree_node (constraints);
   11841              :           }
   11842              : 
   11843      1212903 :       if (CHECKING_P)
   11844              :         {
   11845              :           /* Make sure we can locate the decl.  */
   11846      1212903 :           tree existing = match_mergeable_specialization
   11847      1212903 :             (bool (mk & MK_tmpl_decl_mask), entry);
   11848              : 
   11849      1212903 :           gcc_assert (existing);
   11850      1212903 :           if (mk & MK_tmpl_decl_mask)
   11851              :             {
   11852       853986 :               if (mk & MK_tmpl_tmpl_mask)
   11853         7815 :                 existing = DECL_TI_TEMPLATE (existing);
   11854              :             }
   11855              :           else
   11856              :             {
   11857       358917 :               if (mk & MK_tmpl_tmpl_mask)
   11858         2100 :                 existing = CLASSTYPE_TI_TEMPLATE (existing);
   11859              :               else
   11860       356817 :                 existing = TYPE_NAME (existing);
   11861              :             }
   11862              : 
   11863              :           /* The walkabout should have found ourselves.  */
   11864      1212903 :           gcc_checking_assert (TREE_CODE (decl) == TYPE_DECL
   11865              :                                ? same_type_p (TREE_TYPE (decl),
   11866              :                                               TREE_TYPE (existing))
   11867              :                                : existing == decl);
   11868              :         }
   11869              :     }
   11870      2472369 :   else if (mk != MK_unique)
   11871              :     {
   11872      2022342 :       merge_key key;
   11873      2022342 :       tree name = DECL_NAME (decl);
   11874              : 
   11875      2022342 :       switch (mk)
   11876              :         {
   11877            0 :         default:
   11878            0 :           gcc_unreachable ();
   11879              : 
   11880      1705482 :         case MK_named:
   11881      1705482 :         case MK_friend_spec:
   11882      1705482 :           if (IDENTIFIER_CONV_OP_P (name))
   11883         5902 :             name = conv_op_identifier;
   11884              : 
   11885      1705482 :           if (TREE_CODE (inner) == FUNCTION_DECL)
   11886              :             {
   11887              :               /* Functions are distinguished by parameter types.  */
   11888       931774 :               tree fn_type = TREE_TYPE (inner);
   11889              : 
   11890       931774 :               key.ref_q = type_memfn_rqual (fn_type);
   11891       931774 :               key.coro_disc = get_coroutine_discriminator (inner);
   11892       931774 :               key.args = TYPE_ARG_TYPES (fn_type);
   11893              : 
   11894       931774 :               if (tree reqs = get_constraints (inner))
   11895              :                 {
   11896        67248 :                   if (cxx_dialect < cxx20)
   11897           48 :                     reqs = CI_ASSOCIATED_CONSTRAINTS (reqs);
   11898              :                   else
   11899       134448 :                     reqs = CI_DECLARATOR_REQS (reqs);
   11900        67248 :                   key.constraints = reqs;
   11901              :                 }
   11902              : 
   11903       931774 :               if (IDENTIFIER_CONV_OP_P (name)
   11904       931774 :                   || (decl != inner
   11905       501009 :                       && !(name == fun_identifier
   11906              :                            /* In case the user names something _FUN  */
   11907          108 :                            && LAMBDA_TYPE_P (DECL_CONTEXT (inner)))))
   11908              :                 /* And a function template, or conversion operator needs
   11909              :                    the return type.  Except for the _FUN thunk of a
   11910              :                    generic lambda, which has a recursive decl_type'd
   11911              :                    return type.  */
   11912              :                 // FIXME: What if the return type is a voldemort?
   11913       506857 :                 key.ret = fndecl_declared_return_type (inner);
   11914              :             }
   11915              :           break;
   11916              : 
   11917       146103 :         case MK_field:
   11918       146103 :           {
   11919       146103 :             unsigned ix = 0;
   11920       146103 :             if (TREE_CODE (inner) != FIELD_DECL)
   11921              :               name = NULL_TREE;
   11922              :             else
   11923        25247 :               gcc_checking_assert (!name || !IDENTIFIER_ANON_P (name));
   11924              : 
   11925       146103 :             for (tree field = TYPE_FIELDS (TREE_TYPE (container));
   11926      3207027 :                  ; field = DECL_CHAIN (field))
   11927              :               {
   11928      3353130 :                 tree finner = STRIP_TEMPLATE (field);
   11929      3353130 :                 if (TREE_CODE (finner) == TREE_CODE (inner))
   11930              :                   {
   11931      1145185 :                     if (finner == inner)
   11932              :                       break;
   11933       999082 :                     ix++;
   11934              :                   }
   11935      3207027 :               }
   11936       146103 :             key.index = ix;
   11937              :           }
   11938       146103 :           break;
   11939              : 
   11940         7042 :         case MK_vtable:
   11941         7042 :           {
   11942         7042 :             tree vtable = CLASSTYPE_VTABLES (TREE_TYPE (container));
   11943         9082 :             for (unsigned ix = 0; ; vtable = DECL_CHAIN (vtable), ix++)
   11944         9082 :               if (vtable == decl)
   11945              :                 {
   11946         7042 :                   key.index = ix;
   11947         7042 :                   break;
   11948              :                 }
   11949         7042 :             name = NULL_TREE;
   11950              :           }
   11951         7042 :           break;
   11952              : 
   11953        76530 :         case MK_as_base:
   11954        76530 :           gcc_checking_assert
   11955              :             (decl == TYPE_NAME (CLASSTYPE_AS_BASE (TREE_TYPE (container))));
   11956              :           break;
   11957              : 
   11958        20049 :         case MK_local_friend:
   11959        20049 :           {
   11960              :             /* Find by index on the class's DECL_LIST.  We set TREE_CHAIN to
   11961              :                point to the class in push_template_decl or grokfndecl.  */
   11962        20049 :             unsigned ix = 0;
   11963        20049 :             for (tree decls = CLASSTYPE_DECL_LIST (TREE_CHAIN (decl));
   11964       558537 :                  decls; decls = TREE_CHAIN (decls))
   11965       558537 :               if (!TREE_PURPOSE (decls))
   11966              :                 {
   11967        77931 :                   tree frnd = friend_from_decl_list (TREE_VALUE (decls));
   11968        77931 :                   if (frnd == decl)
   11969              :                     break;
   11970        57882 :                   ix++;
   11971              :                 }
   11972        20049 :             key.index = ix;
   11973        20049 :             name = NULL_TREE;
   11974              :           }
   11975        20049 :           break;
   11976              : 
   11977        15003 :         case MK_local_type:
   11978        15003 :           key_local_type (key, STRIP_TEMPLATE (decl), container);
   11979        15003 :           break;
   11980              : 
   11981         1141 :         case MK_enum:
   11982         1141 :           {
   11983              :             /* Anonymous enums are located by their first identifier,
   11984              :                and underlying type.  */
   11985         1141 :             tree type = TREE_TYPE (decl);
   11986              : 
   11987         1141 :             gcc_checking_assert (UNSCOPED_ENUM_P (type));
   11988              :             /* Using the type name drops the bit precision we might
   11989              :                have been using on the enum.  */
   11990         1141 :             key.ret = TYPE_NAME (ENUM_UNDERLYING_TYPE (type));
   11991         1141 :             if (tree values = TYPE_VALUES (type))
   11992         1141 :               name = DECL_NAME (TREE_VALUE (values));
   11993              :           }
   11994              :           break;
   11995              : 
   11996         1018 :         case MK_keyed:
   11997         1018 :           {
   11998         1018 :             tree scope = get_keyed_decl_scope (inner);
   11999         1018 :             gcc_checking_assert (scope);
   12000              : 
   12001         1018 :             auto *root = keyed_table->get (scope);
   12002         1018 :             unsigned ix = root->length ();
   12003              :             /* If we don't find it, we'll write a really big number
   12004              :                that the reader will ignore.  */
   12005         1130 :             while (ix--)
   12006         1130 :               if ((*root)[ix] == inner)
   12007              :                 break;
   12008              : 
   12009              :             /* Use the keyed-to decl as the 'name'.  */
   12010         1018 :             name = scope;
   12011         1018 :             key.index = ix;
   12012              :           }
   12013         1018 :           break;
   12014              : 
   12015        49974 :         case MK_partial:
   12016        49974 :           {
   12017        49974 :             tree ti = get_template_info (inner);
   12018        49974 :             key.constraints = get_constraints (inner);
   12019        49974 :             key.ret = TI_TEMPLATE (ti);
   12020        49974 :             key.args = TI_ARGS (ti);
   12021              :           }
   12022        49974 :           break;
   12023              :         }
   12024              : 
   12025      2022342 :       tree_node (name);
   12026      2022342 :       if (streaming_p ())
   12027              :         {
   12028              :           /* Check we have enough bits for the index.  */
   12029       719250 :           gcc_checking_assert (key.index < (1u << (sizeof (unsigned) * 8 - 4)));
   12030              : 
   12031       719250 :           unsigned code = ((key.ref_q << 0)
   12032       719250 :                            | (key.coro_disc << 2)
   12033       719250 :                            | (key.index << 4));
   12034       719250 :           u (code);
   12035              :         }
   12036              : 
   12037      2022342 :       if (mk == MK_enum)
   12038         1141 :         tree_node (key.ret);
   12039      2021201 :       else if (mk == MK_partial
   12040      1971227 :                || (mk == MK_named && inner
   12041      1705482 :                    && TREE_CODE (inner) == FUNCTION_DECL))
   12042              :         {
   12043       981748 :           tree_node (key.ret);
   12044       981748 :           tree arg = key.args;
   12045       981748 :           if (mk == MK_named)
   12046      2703666 :             while (arg && arg != void_list_node)
   12047              :               {
   12048      1771892 :                 tree_node (TREE_VALUE (arg));
   12049      1771892 :                 arg = TREE_CHAIN (arg);
   12050              :               }
   12051       981748 :           tree_node (arg);
   12052       981748 :           tree_node (key.constraints);
   12053              :         }
   12054              :     }
   12055      3685272 : }
   12056              : 
   12057              : /* DECL is a new declaration that may be duplicated in OVL.  Use KEY
   12058              :    to find its clone, or NULL.  If DECL's DECL_NAME is NULL, this
   12059              :    has been found by a proxy.  It will be an enum type located by its
   12060              :    first member.
   12061              : 
   12062              :    We're conservative with matches, so ambiguous decls will be
   12063              :    registered as different, then lead to a lookup error if the two
   12064              :    modules are both visible.  Perhaps we want to do something similar
   12065              :    to duplicate decls to get ODR errors on loading?  We already have
   12066              :    some special casing for namespaces.  */
   12067              : 
   12068              : static tree
   12069       301956 : check_mergeable_decl (merge_kind mk, tree decl, tree ovl, merge_key const &key)
   12070              : {
   12071       301956 :   tree found = NULL_TREE;
   12072      1444292 :   for (ovl_iterator iter (ovl); !found && iter; ++iter)
   12073              :     {
   12074       693208 :       tree match = *iter;
   12075              : 
   12076       693208 :       tree d_inner = decl;
   12077       693208 :       tree m_inner = match;
   12078              : 
   12079       801485 :     again:
   12080       801485 :       if (TREE_CODE (d_inner) != TREE_CODE (m_inner))
   12081              :         {
   12082       123361 :           if (TREE_CODE (match) == NAMESPACE_DECL
   12083       123361 :               && !DECL_NAMESPACE_ALIAS (match))
   12084              :             /* Namespaces are never overloaded.  */
   12085              :             found = match;
   12086              : 
   12087       123361 :           continue;
   12088              :         }
   12089              : 
   12090       678124 :       switch (TREE_CODE (d_inner))
   12091              :         {
   12092       258093 :         case TEMPLATE_DECL:
   12093       258093 :           if (template_heads_equivalent_p (d_inner, m_inner))
   12094              :             {
   12095       108277 :               d_inner = DECL_TEMPLATE_RESULT (d_inner);
   12096       108277 :               m_inner = DECL_TEMPLATE_RESULT (m_inner);
   12097       108277 :               if (d_inner == error_mark_node
   12098       108277 :                   && TYPE_DECL_ALIAS_P (m_inner))
   12099              :                 {
   12100              :                   found = match;
   12101              :                   break;
   12102              :                 }
   12103       108277 :               goto again;
   12104              :             }
   12105              :           break;
   12106              : 
   12107       319601 :         case FUNCTION_DECL:
   12108       319601 :           if (tree m_type = TREE_TYPE (m_inner))
   12109       319601 :             if ((!key.ret
   12110       158933 :                  || same_type_p (key.ret, fndecl_declared_return_type (m_inner)))
   12111       292513 :                 && type_memfn_rqual (m_type) == key.ref_q
   12112       292247 :                 && compparms (key.args, TYPE_ARG_TYPES (m_type))
   12113       131320 :                 && get_coroutine_discriminator (m_inner) == key.coro_disc
   12114              :                 /* Reject if old is a "C" builtin and new is not "C".
   12115              :                    Matches decls_match behaviour.  */
   12116       131317 :                 && (!DECL_IS_UNDECLARED_BUILTIN (m_inner)
   12117         8160 :                     || !DECL_EXTERN_C_P (m_inner)
   12118         7945 :                     || DECL_EXTERN_C_P (d_inner))
   12119              :                 /* Reject if one is a different member of a
   12120              :                    guarded/pre/post fn set.  */
   12121       131292 :                 && (!flag_contracts
   12122       241106 :                     || (DECL_IS_PRE_FN_P (d_inner)
   12123       120553 :                         == DECL_IS_PRE_FN_P (m_inner)))
   12124       450893 :                 && (!flag_contracts
   12125       241106 :                     || (DECL_IS_POST_FN_P (d_inner)
   12126       120553 :                         == DECL_IS_POST_FN_P (m_inner))))
   12127              :               {
   12128       131292 :                 tree m_reqs = get_constraints (m_inner);
   12129       131292 :                 if (m_reqs)
   12130              :                   {
   12131         9615 :                     if (cxx_dialect < cxx20)
   12132            8 :                       m_reqs = CI_ASSOCIATED_CONSTRAINTS (m_reqs);
   12133              :                     else
   12134        19222 :                       m_reqs = CI_DECLARATOR_REQS (m_reqs);
   12135              :                   }
   12136              : 
   12137       131292 :                 if (cp_tree_equal (key.constraints, m_reqs))
   12138       170609 :                   found = match;
   12139              :               }
   12140              :           break;
   12141              : 
   12142        61038 :         case TYPE_DECL:
   12143       122076 :           if (DECL_IMPLICIT_TYPEDEF_P (d_inner)
   12144        61038 :               == DECL_IMPLICIT_TYPEDEF_P (m_inner))
   12145              :             {
   12146        61010 :               if (!IDENTIFIER_ANON_P (DECL_NAME (m_inner)))
   12147        60881 :                 return match;
   12148          129 :               else if (mk == MK_enum
   12149          129 :                        && (TYPE_NAME (ENUM_UNDERLYING_TYPE (TREE_TYPE (m_inner)))
   12150          129 :                            == key.ret))
   12151              :                 found = match;
   12152              :             }
   12153              :           break;
   12154              : 
   12155              :         default:
   12156              :           found = match;
   12157              :           break;
   12158              :         }
   12159              :     }
   12160              : 
   12161       241075 :   return found;
   12162              : }
   12163              : 
   12164              : /* DECL, INNER & TYPE are a skeleton set of nodes for a decl.  Only
   12165              :    the bools have been filled in.  Read its merging key and merge it.
   12166              :    Returns the existing decl if there is one.  */
   12167              : 
   12168              : tree
   12169      1193383 : trees_in::key_mergeable (int tag, merge_kind mk, tree decl, tree inner,
   12170              :                          tree type, tree container, bool is_attached,
   12171              :                          bool is_imported_temploid_friend)
   12172              : {
   12173      1193383 :   const char *kind = "new";
   12174      1193383 :   tree existing = NULL_TREE;
   12175              : 
   12176      1193383 :   if (mk & MK_template_mask)
   12177              :     {
   12178              :       // FIXME: We could stream the specialization hash?
   12179       369830 :       spec_entry spec;
   12180       369830 :       spec.tmpl = tree_node ();
   12181       369830 :       spec.args = tree_node ();
   12182              : 
   12183       369830 :       if (get_overrun ())
   12184            0 :         return error_mark_node;
   12185              : 
   12186       369830 :       DECL_NAME (decl) = DECL_NAME (spec.tmpl);
   12187       369830 :       DECL_CONTEXT (decl) = DECL_CONTEXT (spec.tmpl);
   12188       369830 :       DECL_NAME (inner) = DECL_NAME (decl);
   12189       369830 :       DECL_CONTEXT (inner) = DECL_CONTEXT (decl);
   12190              : 
   12191       369830 :       tree constr = NULL_TREE;
   12192       369830 :       bool is_decl = mk & MK_tmpl_decl_mask;
   12193       369830 :       if (is_decl)
   12194              :         {
   12195       263703 :           if (flag_concepts && TREE_CODE (inner) == VAR_DECL)
   12196              :             {
   12197         5262 :               constr = tree_node ();
   12198         5262 :               if (constr)
   12199            0 :                 set_constraints (inner, constr);
   12200              :             }
   12201       524782 :           spec.spec = (mk & MK_tmpl_tmpl_mask) ? inner : decl;
   12202              :         }
   12203              :       else
   12204       106127 :         spec.spec = type;
   12205       369830 :       existing = match_mergeable_specialization (is_decl, &spec);
   12206       369830 :       if (constr)
   12207              :         /* We'll add these back later, if this is the new decl.  */
   12208            0 :         remove_constraints (inner);
   12209              : 
   12210       369830 :       if (!existing)
   12211              :         ; /* We'll add to the table once read.  */
   12212       149925 :       else if (mk & MK_tmpl_decl_mask)
   12213              :         {
   12214              :           /* A declaration specialization.  */
   12215       103742 :           if (mk & MK_tmpl_tmpl_mask)
   12216         1053 :             existing = DECL_TI_TEMPLATE (existing);
   12217              :         }
   12218              :       else
   12219              :         {
   12220              :           /* A type specialization.  */
   12221        46183 :           if (mk & MK_tmpl_tmpl_mask)
   12222          241 :             existing = CLASSTYPE_TI_TEMPLATE (existing);
   12223              :           else
   12224        45942 :             existing = TYPE_NAME (existing);
   12225              :         }
   12226              :     }
   12227       823553 :   else if (mk == MK_unique)
   12228              :     kind = "unique";
   12229              :   else
   12230              :     {
   12231       618529 :       tree name = tree_node ();
   12232              : 
   12233       618529 :       merge_key key;
   12234       618529 :       unsigned code = u ();
   12235       618529 :       key.ref_q = cp_ref_qualifier ((code >> 0) & 3);
   12236       618529 :       key.coro_disc = (code >> 2) & 3;
   12237       618529 :       key.index = code >> 4;
   12238              : 
   12239       618529 :       if (mk == MK_enum)
   12240          236 :         key.ret = tree_node ();
   12241       618293 :       else if (mk == MK_partial
   12242       607522 :                || ((mk == MK_named || mk == MK_friend_spec)
   12243       516118 :                    && TREE_CODE (inner) == FUNCTION_DECL))
   12244              :         {
   12245       297078 :           key.ret = tree_node ();
   12246       297078 :           tree arg, *arg_ptr = &key.args;
   12247       297078 :           while ((arg = tree_node ())
   12248       840257 :                  && arg != void_list_node
   12249      1397175 :                  && mk != MK_partial)
   12250              :             {
   12251       544663 :               *arg_ptr = tree_cons (NULL_TREE, arg, NULL_TREE);
   12252       544663 :               arg_ptr = &TREE_CHAIN (*arg_ptr);
   12253              :             }
   12254       297078 :           *arg_ptr = arg;
   12255       297078 :           key.constraints = tree_node ();
   12256              :         }
   12257              : 
   12258       618529 :       if (get_overrun ())
   12259            0 :         return error_mark_node;
   12260              : 
   12261       618529 :       if (mk < MK_indirect_lwm)
   12262              :         {
   12263       606621 :           DECL_NAME (decl) = name;
   12264       606621 :           DECL_CONTEXT (decl) = FROB_CONTEXT (container);
   12265              :         }
   12266       618529 :       DECL_NAME (inner) = DECL_NAME (decl);
   12267       618529 :       DECL_CONTEXT (inner) = DECL_CONTEXT (decl);
   12268              : 
   12269       618529 :       if (mk == MK_partial)
   12270              :         {
   12271        10771 :           for (tree spec = DECL_TEMPLATE_SPECIALIZATIONS (key.ret);
   12272        55300 :                spec; spec = TREE_CHAIN (spec))
   12273              :             {
   12274        50693 :               tree tmpl = TREE_VALUE (spec);
   12275        50693 :               tree ti = get_template_info (tmpl);
   12276        50693 :               if (template_args_equal (key.args, TI_ARGS (ti))
   12277        57650 :                   && cp_tree_equal (key.constraints,
   12278              :                                     get_constraints
   12279         6957 :                                     (DECL_TEMPLATE_RESULT (tmpl))))
   12280              :                 {
   12281              :                   existing = tmpl;
   12282              :                   break;
   12283              :                 }
   12284              :             }
   12285              :         }
   12286       607758 :       else if (mk == MK_keyed
   12287          397 :                && DECL_LANG_SPECIFIC (name)
   12288       608155 :                && DECL_MODULE_KEYED_DECLS_P (name))
   12289              :         {
   12290          397 :           gcc_checking_assert (TREE_CODE (container) == NAMESPACE_DECL
   12291              :                                || TREE_CODE (container) == TYPE_DECL
   12292              :                                || TREE_CODE (container) == FUNCTION_DECL);
   12293          397 :           if (auto *set = keyed_table->get (name))
   12294       618678 :             if (key.index < set->length ())
   12295              :               {
   12296          149 :                 existing = (*set)[key.index];
   12297          149 :                 if (existing)
   12298              :                   {
   12299          149 :                     gcc_checking_assert
   12300              :                       (DECL_IMPLICIT_TYPEDEF_P (existing));
   12301          149 :                     if (inner != decl)
   12302           91 :                       existing
   12303           91 :                         = CLASSTYPE_TI_TEMPLATE (TREE_TYPE (existing));
   12304              :                   }
   12305              :               }
   12306              :         }
   12307              :       else
   12308       607361 :         switch (TREE_CODE (container))
   12309              :           {
   12310            0 :           default:
   12311            0 :             gcc_unreachable ();
   12312              : 
   12313       149330 :           case NAMESPACE_DECL:
   12314       149330 :             if (is_attached
   12315       149330 :                 && !is_imported_temploid_friend
   12316       149330 :                 && !(state->is_module () || state->is_partition ()))
   12317              :               kind = "unique";
   12318              :             else
   12319              :               {
   12320       147294 :                 gcc_checking_assert (mk == MK_named || mk == MK_enum);
   12321       147294 :                 tree mvec;
   12322       147294 :                 tree *vslot = mergeable_namespace_slots (container, name,
   12323              :                                                          is_attached, &mvec);
   12324       147294 :                 existing = check_mergeable_decl (mk, decl, *vslot, key);
   12325       147294 :                 if (!existing)
   12326        69969 :                   add_mergeable_namespace_entity (vslot, decl);
   12327              :                 else
   12328              :                   {
   12329              :                     /* Note that we now have duplicates to deal with in
   12330              :                        name lookup.  */
   12331        77325 :                     if (is_attached)
   12332           57 :                       BINDING_VECTOR_PARTITION_DUPS_P (mvec) = true;
   12333              :                     else
   12334        77268 :                       BINDING_VECTOR_GLOBAL_DUPS_P (mvec) = true;
   12335              :                   }
   12336              :               }
   12337       153526 :             break;
   12338              : 
   12339         4196 :           case FUNCTION_DECL:
   12340         4196 :             gcc_checking_assert (mk == MK_local_type);
   12341         4196 :             existing = key_local_type (key, container, name);
   12342         4196 :             if (existing && inner != decl)
   12343         3406 :               existing = TYPE_TI_TEMPLATE (TREE_TYPE (existing));
   12344              :             break;
   12345              : 
   12346       453835 :           case TYPE_DECL:
   12347       453835 :             gcc_checking_assert (!is_imported_temploid_friend);
   12348       453835 :             int use_tmpl = 0;
   12349         5739 :             if (is_attached && !(state->is_module () || state->is_partition ())
   12350              :                 /* Implicit or in-class defaulted member functions
   12351              :                    can come from anywhere.  */
   12352         4087 :                 && !(TREE_CODE (decl) == FUNCTION_DECL
   12353         1169 :                      && !DECL_THUNK_P (decl)
   12354         1169 :                      && DECL_DEFAULTED_FN (decl)
   12355          721 :                      && !DECL_DEFAULTED_OUTSIDE_CLASS_P (decl))
   12356              :                 /* As can members of template specialisations.  */
   12357       457201 :                 && !(node_template_info (container, use_tmpl)
   12358         1235 :                      && use_tmpl != 0))
   12359              :               kind = "unique";
   12360              :             else
   12361              :               {
   12362       450699 :                 tree ctx = TREE_TYPE (container);
   12363              : 
   12364              :                 /* For some reason templated enumeral types are not marked
   12365              :                    as COMPLETE_TYPE_P, even though they have members.
   12366              :                    This may well be a bug elsewhere.  */
   12367       450699 :                 if (TREE_CODE (ctx) == ENUMERAL_TYPE)
   12368        15474 :                   existing = find_enum_member (ctx, name);
   12369       435225 :                 else if (COMPLETE_TYPE_P (ctx))
   12370              :                   {
   12371       194338 :                     switch (mk)
   12372              :                       {
   12373            0 :                       default:
   12374            0 :                         gcc_unreachable ();
   12375              : 
   12376       155075 :                       case MK_named:
   12377       155075 :                         existing = lookup_class_binding (ctx, name);
   12378       155075 :                         if (existing)
   12379              :                           {
   12380       154662 :                             tree inner = decl;
   12381       154662 :                             if (TREE_CODE (inner) == TEMPLATE_DECL
   12382       154662 :                                 && !DECL_MEMBER_TEMPLATE_P (inner))
   12383        68894 :                               inner = DECL_TEMPLATE_RESULT (inner);
   12384              : 
   12385       154662 :                             existing = check_mergeable_decl
   12386       154662 :                               (mk, inner, existing, key);
   12387              : 
   12388       154662 :                             if (!existing && DECL_ALIAS_TEMPLATE_P (decl))
   12389              :                               {} // FIXME: Insert into specialization
   12390              :                             // tables, we'll need the arguments for that!
   12391              :                           }
   12392              :                         break;
   12393              : 
   12394        26802 :                       case MK_field:
   12395        26802 :                         {
   12396        26802 :                           unsigned ix = key.index;
   12397        26802 :                           for (tree field = TYPE_FIELDS (ctx);
   12398       892306 :                                field; field = DECL_CHAIN (field))
   12399              :                             {
   12400       892306 :                               tree finner = STRIP_TEMPLATE (field);
   12401       892306 :                               if (TREE_CODE (finner) == TREE_CODE (inner))
   12402       299461 :                                 if (!ix--)
   12403              :                                   {
   12404              :                                     existing = field;
   12405              :                                     break;
   12406              :                                   }
   12407              :                             }
   12408              :                         }
   12409              :                         break;
   12410              : 
   12411         1484 :                       case MK_vtable:
   12412         1484 :                         {
   12413         1484 :                           unsigned ix = key.index;
   12414         1484 :                           for (tree vtable = CLASSTYPE_VTABLES (ctx);
   12415         1823 :                                vtable; vtable = DECL_CHAIN (vtable))
   12416         1570 :                             if (!ix--)
   12417              :                               {
   12418              :                                 existing = vtable;
   12419              :                                 break;
   12420              :                               }
   12421              :                         }
   12422              :                         break;
   12423              : 
   12424         7978 :                       case MK_as_base:
   12425         7978 :                         {
   12426         7978 :                           tree as_base = CLASSTYPE_AS_BASE (ctx);
   12427         7978 :                           if (as_base && as_base != ctx)
   12428         7978 :                             existing = TYPE_NAME (as_base);
   12429              :                         }
   12430              :                         break;
   12431              : 
   12432         2999 :                       case MK_local_friend:
   12433         2999 :                         {
   12434         2999 :                           unsigned ix = key.index;
   12435         2999 :                           for (tree decls = CLASSTYPE_DECL_LIST (ctx);
   12436        84495 :                                decls; decls = TREE_CHAIN (decls))
   12437        84495 :                             if (!TREE_PURPOSE (decls) && !ix--)
   12438              :                               {
   12439         2999 :                                 existing
   12440         2999 :                                   = friend_from_decl_list (TREE_VALUE (decls));
   12441         2999 :                                 break;
   12442              :                               }
   12443              :                         }
   12444              :                         break;
   12445              :                       }
   12446              : 
   12447       194338 :                     if (existing && mk < MK_indirect_lwm && mk != MK_partial
   12448       190176 :                         && TREE_CODE (decl) == TEMPLATE_DECL
   12449       282123 :                         && !DECL_MEMBER_TEMPLATE_P (decl))
   12450              :                       {
   12451        70878 :                         tree ti;
   12452        70878 :                         if (DECL_IMPLICIT_TYPEDEF_P (existing))
   12453          898 :                           ti = TYPE_TEMPLATE_INFO (TREE_TYPE (existing));
   12454              :                         else
   12455        69980 :                           ti = DECL_TEMPLATE_INFO (existing);
   12456        70878 :                         existing = TI_TEMPLATE (ti);
   12457              :                       }
   12458              :                   }
   12459              :               }
   12460              :           }
   12461              :     }
   12462              : 
   12463      1193383 :   dump (dumper::MERGE)
   12464         3208 :     && dump ("Read:%d's %s merge key (%s) %C:%N", tag, merge_kind_name[mk],
   12465         3208 :              existing ? "matched" : kind, TREE_CODE (decl), decl);
   12466              : 
   12467              :   return existing;
   12468              : }
   12469              : 
   12470              : void
   12471       293943 : trees_out::binfo_mergeable (tree binfo)
   12472              : {
   12473       293943 :   tree dom = binfo;
   12474       372567 :   while (tree parent = BINFO_INHERITANCE_CHAIN (dom))
   12475              :     dom = parent;
   12476       293943 :   tree type = BINFO_TYPE (dom);
   12477       293943 :   gcc_checking_assert (TYPE_BINFO (type) == dom);
   12478       293943 :   tree_node (type);
   12479       293943 :   if (streaming_p ())
   12480              :     {
   12481              :       unsigned ix = 0;
   12482       158382 :       for (; dom != binfo; dom = TREE_CHAIN (dom))
   12483        41126 :         ix++;
   12484       117256 :       u (ix);
   12485              :     }
   12486       293943 : }
   12487              : 
   12488              : unsigned
   12489        91892 : trees_in::binfo_mergeable (tree *type)
   12490              : {
   12491        91892 :   *type = tree_node ();
   12492        91892 :   return u ();
   12493              : }
   12494              : 
   12495              : /* DECL is a just streamed declaration with attributes DATTR that should
   12496              :    have matching ABI tags as EXISTING's attributes EATTR.  Check that the
   12497              :    ABI tags match, and report an error if not.  */
   12498              : 
   12499              : void
   12500       296980 : trees_in::check_abi_tags (tree existing, tree decl, tree &eattr, tree &dattr)
   12501              : {
   12502       296980 :   tree etags = lookup_attribute ("abi_tag", eattr);
   12503       296980 :   tree dtags = lookup_attribute ("abi_tag", dattr);
   12504       296980 :   if ((etags == nullptr) != (dtags == nullptr)
   12505       296980 :       || (etags && !attribute_value_equal (etags, dtags)))
   12506              :     {
   12507           30 :       if (etags)
   12508           21 :         etags = TREE_VALUE (etags);
   12509           30 :       if (dtags)
   12510           24 :         dtags = TREE_VALUE (dtags);
   12511              : 
   12512              :       /* We only error if mangling wouldn't consider the tags equivalent.  */
   12513           30 :       if (!equal_abi_tags (etags, dtags))
   12514              :         {
   12515           21 :           auto_diagnostic_group d;
   12516           21 :           if (dtags)
   12517           15 :             error_at (DECL_SOURCE_LOCATION (decl),
   12518              :                       "mismatching abi tags for %qD with tags %qE",
   12519              :                       decl, dtags);
   12520              :           else
   12521            6 :             error_at (DECL_SOURCE_LOCATION (decl),
   12522              :                       "mismatching abi tags for %qD with no tags", decl);
   12523           21 :           if (etags)
   12524           15 :             inform (DECL_SOURCE_LOCATION (existing),
   12525              :                     "existing declaration here with tags %qE", etags);
   12526              :           else
   12527            6 :             inform (DECL_SOURCE_LOCATION (existing),
   12528              :                     "existing declaration here with no tags");
   12529           21 :         }
   12530              : 
   12531              :       /* Always use the existing abi_tags as the canonical set so that
   12532              :          later processing doesn't get confused.  */
   12533           30 :       if (dtags)
   12534           24 :         dattr = remove_attribute ("abi_tag", dattr);
   12535           30 :       if (etags)
   12536           21 :         duplicate_one_attribute (&dattr, eattr, "abi_tag");
   12537              :     }
   12538       296980 : }
   12539              : 
   12540              : /* DECL is a just streamed mergeable decl that should match EXISTING.  Check
   12541              :    it does and issue an appropriate diagnostic if not.  Merge any
   12542              :    bits from DECL to EXISTING.  This is stricter matching than
   12543              :    decls_match, because we can rely on ODR-sameness, and we cannot use
   12544              :    decls_match because it can cause instantiations of constraints.  */
   12545              : 
   12546              : bool
   12547       435586 : trees_in::is_matching_decl (tree existing, tree decl, bool is_typedef)
   12548              : {
   12549              :   // FIXME: We should probably do some duplicate decl-like stuff here
   12550              :   // (beware, default parms should be the same?)  Can we just call
   12551              :   // duplicate_decls and teach it how to handle the module-specific
   12552              :   // permitted/required duplications?
   12553              : 
   12554              :   // We know at this point that the decls have matched by key, so we
   12555              :   // can elide some of the checking
   12556       435586 :   gcc_checking_assert (TREE_CODE (existing) == TREE_CODE (decl));
   12557              : 
   12558       435586 :   tree d_inner = decl;
   12559       435586 :   tree e_inner = existing;
   12560       435586 :   if (TREE_CODE (decl) == TEMPLATE_DECL)
   12561              :     {
   12562       145505 :       d_inner = DECL_TEMPLATE_RESULT (d_inner);
   12563       145505 :       e_inner = DECL_TEMPLATE_RESULT (e_inner);
   12564       145505 :       gcc_checking_assert (TREE_CODE (e_inner) == TREE_CODE (d_inner));
   12565              :     }
   12566              : 
   12567              :   // FIXME: do more precise errors at point of mismatch
   12568       435586 :   const char *mismatch_msg = nullptr;
   12569              : 
   12570       435586 :   if (VAR_OR_FUNCTION_DECL_P (d_inner)
   12571       435586 :       && DECL_EXTERN_C_P (d_inner) != DECL_EXTERN_C_P (e_inner))
   12572              :     {
   12573            6 :       mismatch_msg = G_("conflicting language linkage for imported "
   12574              :                         "declaration %#qD");
   12575            6 :       goto mismatch;
   12576              :     }
   12577       435580 :   else if (TREE_CODE (d_inner) == FUNCTION_DECL)
   12578              :     {
   12579       199182 :       tree e_ret = fndecl_declared_return_type (existing);
   12580       199182 :       tree d_ret = fndecl_declared_return_type (decl);
   12581              : 
   12582        85295 :       if (decl != d_inner && DECL_NAME (d_inner) == fun_identifier
   12583       199206 :           && LAMBDA_TYPE_P (DECL_CONTEXT (d_inner)))
   12584              :         /* This has a recursive type that will compare different.  */;
   12585       199170 :       else if (!same_type_p (d_ret, e_ret))
   12586              :         {
   12587           21 :           mismatch_msg = G_("conflicting type for imported declaration %#qD");
   12588           21 :           goto mismatch;
   12589              :         }
   12590              : 
   12591       199161 :       tree e_type = TREE_TYPE (e_inner);
   12592       199161 :       tree d_type = TREE_TYPE (d_inner);
   12593              : 
   12594       199161 :       for (tree e_args = TYPE_ARG_TYPES (e_type),
   12595       199161 :              d_args = TYPE_ARG_TYPES (d_type);
   12596       342574 :            e_args != d_args && (e_args || d_args);
   12597       143413 :            e_args = TREE_CHAIN (e_args), d_args = TREE_CHAIN (d_args))
   12598              :         {
   12599       143413 :           if (!(e_args && d_args))
   12600              :             {
   12601            0 :               mismatch_msg = G_("conflicting argument list for imported "
   12602              :                                 "declaration %#qD");
   12603            0 :               goto mismatch;
   12604              :             }
   12605              : 
   12606       143413 :           if (!same_type_p (TREE_VALUE (d_args), TREE_VALUE (e_args)))
   12607              :             {
   12608            0 :               mismatch_msg = G_("conflicting argument types for imported "
   12609              :                                 "declaration %#qD");
   12610            0 :               goto mismatch;
   12611              :             }
   12612              :         }
   12613              : 
   12614              :       /* If EXISTING has an undeduced or uninstantiated exception
   12615              :          specification, but DECL does not, propagate the exception
   12616              :          specification.  Otherwise we end up asserting or trying to
   12617              :          instantiate it in the middle of loading.   */
   12618       199161 :       tree e_spec = TYPE_RAISES_EXCEPTIONS (e_type);
   12619       199161 :       tree d_spec = TYPE_RAISES_EXCEPTIONS (d_type);
   12620       291759 :       if (DECL_MAYBE_DELETED (e_inner) || DEFERRED_NOEXCEPT_SPEC_P (e_spec))
   12621              :         {
   12622        31470 :           if (!(DECL_MAYBE_DELETED (d_inner)
   12623        15677 :                 || DEFERRED_NOEXCEPT_SPEC_P (d_spec))
   12624        46916 :               || (UNEVALUATED_NOEXCEPT_SPEC_P (e_spec)
   12625        25580 :                   && !UNEVALUATED_NOEXCEPT_SPEC_P (d_spec)))
   12626              :             {
   12627          147 :               dump (dumper::MERGE)
   12628            6 :                 && dump ("Propagating instantiated noexcept to %N", existing);
   12629          147 :               gcc_checking_assert (existing == e_inner);
   12630          147 :               TREE_TYPE (existing) = d_type;
   12631              : 
   12632              :               /* Propagate to existing clones.  */
   12633          147 :               tree clone;
   12634          411 :               FOR_EACH_CLONE (clone, existing)
   12635              :                 {
   12636          264 :                   if (TREE_TYPE (clone) == e_type)
   12637          264 :                     TREE_TYPE (clone) = d_type;
   12638              :                   else
   12639            0 :                     TREE_TYPE (clone)
   12640            0 :                       = build_exception_variant (TREE_TYPE (clone), d_spec);
   12641              :                 }
   12642              :             }
   12643              :         }
   12644       183368 :       else if (!DECL_MAYBE_DELETED (d_inner)
   12645       261959 :                && !DEFERRED_NOEXCEPT_SPEC_P (d_spec)
   12646       366735 :                && !comp_except_specs (d_spec, e_spec, ce_type))
   12647              :         {
   12648         1737 :           mismatch_msg = G_("conflicting %<noexcept%> specifier for "
   12649              :                             "imported declaration %#qD");
   12650         1737 :           goto mismatch;
   12651              :         }
   12652              : 
   12653              :       /* Similarly if EXISTING has an undeduced return type, but DECL's
   12654              :          is already deduced.  */
   12655       197424 :       bool e_undeduced = undeduced_auto_decl (existing);
   12656       197424 :       bool d_undeduced = undeduced_auto_decl (decl);
   12657       197424 :       if (e_undeduced && !d_undeduced)
   12658              :         {
   12659           13 :           dump (dumper::MERGE)
   12660            0 :             && dump ("Propagating deduced return type to %N", existing);
   12661           13 :           gcc_checking_assert (existing == e_inner);
   12662           13 :           FNDECL_USED_AUTO (existing) = true;
   12663           13 :           DECL_SAVED_AUTO_RETURN_TYPE (existing) = TREE_TYPE (e_type);
   12664           13 :           TREE_TYPE (existing) = change_return_type (TREE_TYPE (d_type), e_type);
   12665              :         }
   12666       197411 :       else if (d_undeduced && !e_undeduced)
   12667              :         /* EXISTING was deduced, leave it alone.  */;
   12668       197408 :       else if (type_uses_auto (d_ret)
   12669       197408 :                && !same_type_p (TREE_TYPE (d_type), TREE_TYPE (e_type)))
   12670              :         {
   12671            9 :           mismatch_msg = G_("conflicting deduced return type for "
   12672              :                             "imported declaration %#qD");
   12673            9 :           goto mismatch;
   12674              :         }
   12675              : 
   12676              :       /* Similarly if EXISTING has undeduced constexpr, but DECL's
   12677              :          is already deduced.  */
   12678       197415 :       if (DECL_DECLARED_CONSTEXPR_P (e_inner)
   12679       197415 :           == DECL_DECLARED_CONSTEXPR_P (d_inner))
   12680              :         /* Already matches.  */;
   12681            2 :       else if (DECL_DECLARED_CONSTEXPR_P (d_inner)
   12682            2 :                && (DECL_MAYBE_DELETED (e_inner)
   12683            0 :                    || decl_implicit_constexpr_p (d_inner)))
   12684              :         /* DECL was deduced, copy to EXISTING.  */
   12685              :         {
   12686            1 :           DECL_DECLARED_CONSTEXPR_P (e_inner) = true;
   12687            1 :           if (decl_implicit_constexpr_p (d_inner))
   12688            0 :             DECL_LANG_SPECIFIC (e_inner)->u.fn.implicit_constexpr = true;
   12689              :         }
   12690            1 :       else if (DECL_DECLARED_CONSTEXPR_P (e_inner)
   12691            1 :                && (DECL_MAYBE_DELETED (d_inner)
   12692            0 :                    || decl_implicit_constexpr_p (e_inner)))
   12693              :         /* EXISTING was deduced, leave it alone.  */;
   12694              :       else
   12695              :         {
   12696            0 :           mismatch_msg = G_("conflicting %<constexpr%> for imported "
   12697              :                             "declaration %#qD");
   12698            0 :           goto mismatch;
   12699              :         }
   12700              : 
   12701              :       /* Don't synthesize a defaulted function if we're importing one
   12702              :          we've already determined.  */
   12703       197415 :       if (!DECL_MAYBE_DELETED (d_inner))
   12704       197298 :         DECL_MAYBE_DELETED (e_inner) = false;
   12705              :     }
   12706       236398 :   else if (is_typedef)
   12707              :     {
   12708        84389 :       if (!DECL_ORIGINAL_TYPE (e_inner)
   12709        84389 :           || !same_type_p (DECL_ORIGINAL_TYPE (d_inner),
   12710              :                            DECL_ORIGINAL_TYPE (e_inner)))
   12711              :         {
   12712            3 :           mismatch_msg = G_("conflicting imported declaration %q#D");
   12713            3 :           goto mismatch;
   12714              :         }
   12715              :     }
   12716              :   /* Using cp_tree_equal because we can meet TYPE_ARGUMENT_PACKs
   12717              :      here. I suspect the entities that directly do that are things
   12718              :      that shouldn't go to duplicate_decls (FIELD_DECLs etc).   */
   12719       152009 :   else if (!cp_tree_equal (TREE_TYPE (decl), TREE_TYPE (existing)))
   12720              :     {
   12721              :       mismatch_msg = G_("conflicting type for imported declaration %#qD");
   12722         2498 :     mismatch:
   12723         2498 :       if (DECL_IS_UNDECLARED_BUILTIN (existing))
   12724              :         /* Just like duplicate_decls, presum the user knows what
   12725              :            they're doing in overriding a builtin.   */
   12726         1755 :         TREE_TYPE (existing) = TREE_TYPE (decl);
   12727          743 :       else if (decl_function_context (decl))
   12728              :         /* The type of a mergeable local entity (such as a function scope
   12729              :            capturing lambda's closure type fields) can depend on an
   12730              :            unmergeable local entity (such as a local variable), so type
   12731              :            equality isn't feasible in general for local entities.  */;
   12732              :       else
   12733              :         {
   12734           21 :           gcc_checking_assert (mismatch_msg);
   12735           21 :           auto_diagnostic_group d;
   12736           21 :           error_at (DECL_SOURCE_LOCATION (decl), mismatch_msg, decl);
   12737           21 :           inform (DECL_SOURCE_LOCATION (existing),
   12738              :                   "existing declaration %#qD", existing);
   12739           21 :           return false;
   12740           21 :         }
   12741              :     }
   12742              : 
   12743       435565 :   if (DECL_IS_UNDECLARED_BUILTIN (existing)
   12744       435565 :       && !DECL_IS_UNDECLARED_BUILTIN (decl))
   12745              :     {
   12746              :       /* We're matching a builtin that the user has yet to declare.
   12747              :          We are the one!  This is very much duplicate-decl
   12748              :          shenanigans. */
   12749         2028 :       DECL_SOURCE_LOCATION (existing) = DECL_SOURCE_LOCATION (decl);
   12750         2028 :       if (TREE_CODE (decl) != TYPE_DECL)
   12751              :         {
   12752              :           /* Propagate exceptions etc.  */
   12753         2007 :           TREE_TYPE (existing) = TREE_TYPE (decl);
   12754         2007 :           TREE_NOTHROW (existing) = TREE_NOTHROW (decl);
   12755              :         }
   12756              :       /* This is actually an import! */
   12757         2028 :       DECL_MODULE_IMPORT_P (existing) = true;
   12758              : 
   12759              :       /* Yay, sliced!  */
   12760         2028 :       existing->base = decl->base;
   12761              : 
   12762         2028 :       if (TREE_CODE (decl) == FUNCTION_DECL)
   12763              :         {
   12764              :           /* Ew :(  */
   12765         2007 :           memcpy (&existing->decl_common.size,
   12766              :                   &decl->decl_common.size,
   12767              :                   (offsetof (tree_decl_common, pt_uid)
   12768              :                    - offsetof (tree_decl_common, size)));
   12769         2007 :           auto bltin_class = DECL_BUILT_IN_CLASS (decl);
   12770         2007 :           existing->function_decl.built_in_class = bltin_class;
   12771         2007 :           auto fncode = DECL_UNCHECKED_FUNCTION_CODE (decl);
   12772         2007 :           DECL_UNCHECKED_FUNCTION_CODE (existing) = fncode;
   12773         2007 :           if (existing->function_decl.built_in_class == BUILT_IN_NORMAL)
   12774              :             {
   12775         1809 :               if (builtin_decl_explicit_p (built_in_function (fncode)))
   12776         1809 :                 switch (fncode)
   12777              :                   {
   12778            0 :                   case BUILT_IN_STPCPY:
   12779            0 :                     set_builtin_decl_implicit_p
   12780            0 :                       (built_in_function (fncode), true);
   12781            0 :                     break;
   12782         1809 :                   default:
   12783         1809 :                     set_builtin_decl_declared_p
   12784         1809 :                       (built_in_function (fncode), true);
   12785         1809 :                     break;
   12786              :                   }
   12787         1809 :               copy_attributes_to_builtin (decl);
   12788              :             }
   12789              :         }
   12790              :     }
   12791              : 
   12792       435565 :   if (VAR_OR_FUNCTION_DECL_P (decl)
   12793       435565 :       && DECL_TEMPLATE_INSTANTIATED (decl))
   12794              :     /* Don't instantiate again!  */
   12795         8903 :     DECL_TEMPLATE_INSTANTIATED (existing) = true;
   12796              : 
   12797       435565 :   if (TREE_CODE (d_inner) == FUNCTION_DECL
   12798       435565 :       && DECL_DECLARED_INLINE_P (d_inner))
   12799              :     {
   12800       160855 :       DECL_DECLARED_INLINE_P (e_inner) = true;
   12801       160855 :       if (!DECL_SAVED_TREE (e_inner)
   12802        79407 :           && lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (d_inner))
   12803       160872 :           && !lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (e_inner)))
   12804              :         {
   12805           51 :           DECL_INTERFACE_KNOWN (e_inner)
   12806           17 :             |= DECL_INTERFACE_KNOWN (d_inner);
   12807           17 :           DECL_DISREGARD_INLINE_LIMITS (e_inner)
   12808           17 :             |= DECL_DISREGARD_INLINE_LIMITS (d_inner);
   12809              :           // TODO: we will eventually want to merge all decl attributes
   12810           17 :           duplicate_one_attribute (&DECL_ATTRIBUTES (e_inner),
   12811           17 :                                    DECL_ATTRIBUTES (d_inner), "gnu_inline");
   12812              :         }
   12813              :     }
   12814       435565 :   if (!DECL_EXTERNAL (d_inner))
   12815       204598 :     DECL_EXTERNAL (e_inner) = false;
   12816              : 
   12817       435565 :   if (VAR_OR_FUNCTION_DECL_P (d_inner))
   12818       439194 :     check_abi_tags (existing, decl,
   12819       219597 :                     DECL_ATTRIBUTES (e_inner), DECL_ATTRIBUTES (d_inner));
   12820              : 
   12821       435565 :   if (TREE_CODE (decl) == TEMPLATE_DECL)
   12822              :     {
   12823              :       /* Merge default template arguments.  */
   12824       145503 :       tree d_parms = DECL_INNERMOST_TEMPLATE_PARMS (decl);
   12825       145503 :       tree e_parms = DECL_INNERMOST_TEMPLATE_PARMS (existing);
   12826       145503 :       gcc_checking_assert (TREE_VEC_LENGTH (d_parms)
   12827              :                            == TREE_VEC_LENGTH (e_parms));
   12828       417159 :       for (int i = 0; i < TREE_VEC_LENGTH (d_parms); ++i)
   12829              :         {
   12830       271665 :           tree d_default = TREE_PURPOSE (TREE_VEC_ELT (d_parms, i));
   12831       271665 :           tree& e_default = TREE_PURPOSE (TREE_VEC_ELT (e_parms, i));
   12832       271665 :           if (e_default == NULL_TREE)
   12833       230148 :             e_default = d_default;
   12834        41517 :           else if (d_default != NULL_TREE
   12835        41517 :                    && !cp_tree_equal (d_default, e_default))
   12836              :             {
   12837            9 :               auto_diagnostic_group d;
   12838            9 :               tree d_parm = TREE_VALUE (TREE_VEC_ELT (d_parms, i));
   12839            9 :               tree e_parm = TREE_VALUE (TREE_VEC_ELT (e_parms, i));
   12840            9 :               error_at (DECL_SOURCE_LOCATION (d_parm),
   12841              :                         "conflicting default argument for %#qD", d_parm);
   12842            9 :               inform (DECL_SOURCE_LOCATION (e_parm),
   12843              :                       "existing default declared here");
   12844            9 :               return false;
   12845            9 :             }
   12846              :         }
   12847              :     }
   12848              : 
   12849       435556 :   if (TREE_CODE (d_inner) == FUNCTION_DECL)
   12850              :     {
   12851              :       /* Merge default function arguments.  */
   12852       199173 :       tree d_parm = FUNCTION_FIRST_USER_PARMTYPE (d_inner);
   12853       199173 :       tree e_parm = FUNCTION_FIRST_USER_PARMTYPE (e_inner);
   12854       199173 :       int i = 0;
   12855       475017 :       for (; d_parm && d_parm != void_list_node;
   12856       275844 :            d_parm = TREE_CHAIN (d_parm), e_parm = TREE_CHAIN (e_parm), ++i)
   12857              :         {
   12858       275856 :           tree d_default = TREE_PURPOSE (d_parm);
   12859       275856 :           tree& e_default = TREE_PURPOSE (e_parm);
   12860       275856 :           if (e_default == NULL_TREE)
   12861       259280 :             e_default = d_default;
   12862        16576 :           else if (d_default != NULL_TREE
   12863        16576 :                    && !cp_tree_equal (d_default, e_default))
   12864              :             {
   12865           12 :               auto_diagnostic_group d;
   12866           12 :               error_at (get_fndecl_argument_location (d_inner, i),
   12867              :                         "conflicting default argument for parameter %P of %#qD",
   12868              :                         i, decl);
   12869           12 :               inform (get_fndecl_argument_location (e_inner, i),
   12870              :                       "existing default declared here");
   12871           12 :               return false;
   12872           12 :             }
   12873              :         }
   12874              :     }
   12875              : 
   12876              :   return true;
   12877              : }
   12878              : 
   12879              : /* FN is an implicit member function that we've discovered is new to
   12880              :    the class.  Add it to the TYPE_FIELDS chain and the method vector.
   12881              :    Reset the appropriate classtype lazy flag.   */
   12882              : 
   12883              : bool
   12884          910 : trees_in::install_implicit_member (tree fn)
   12885              : {
   12886          910 :   tree ctx = DECL_CONTEXT (fn);
   12887          910 :   tree name = DECL_NAME (fn);
   12888              :   /* We know these are synthesized, so the set of expected prototypes
   12889              :      is quite restricted.  We're not validating correctness, just
   12890              :      distinguishing beteeen the small set of possibilities.  */
   12891          910 :   tree parm_type = TREE_VALUE (FUNCTION_FIRST_USER_PARMTYPE (fn));
   12892          910 :   if (IDENTIFIER_CTOR_P (name))
   12893              :     {
   12894          626 :       if (CLASSTYPE_LAZY_DEFAULT_CTOR (ctx)
   12895          626 :           && VOID_TYPE_P (parm_type))
   12896          160 :         CLASSTYPE_LAZY_DEFAULT_CTOR (ctx) = false;
   12897          466 :       else if (!TYPE_REF_P (parm_type))
   12898              :         return false;
   12899          466 :       else if (CLASSTYPE_LAZY_COPY_CTOR (ctx)
   12900          466 :                && !TYPE_REF_IS_RVALUE (parm_type))
   12901          236 :         CLASSTYPE_LAZY_COPY_CTOR (ctx) = false;
   12902          230 :       else if (CLASSTYPE_LAZY_MOVE_CTOR (ctx))
   12903          230 :         CLASSTYPE_LAZY_MOVE_CTOR (ctx) = false;
   12904              :       else
   12905              :         return false;
   12906              :     }
   12907          284 :   else if (IDENTIFIER_DTOR_P (name))
   12908              :     {
   12909          222 :       if (CLASSTYPE_LAZY_DESTRUCTOR (ctx))
   12910          222 :         CLASSTYPE_LAZY_DESTRUCTOR (ctx) = false;
   12911              :       else
   12912              :         return false;
   12913          222 :       if (DECL_VIRTUAL_P (fn))
   12914              :         /* A virtual dtor should have been created when the class
   12915              :            became complete.  */
   12916              :         return false;
   12917              :     }
   12918           62 :   else if (name == assign_op_identifier)
   12919              :     {
   12920           62 :       if (!TYPE_REF_P (parm_type))
   12921              :         return false;
   12922           62 :       else if (CLASSTYPE_LAZY_COPY_ASSIGN (ctx)
   12923           62 :                && !TYPE_REF_IS_RVALUE (parm_type))
   12924           31 :         CLASSTYPE_LAZY_COPY_ASSIGN (ctx) = false;
   12925           31 :       else if (CLASSTYPE_LAZY_MOVE_ASSIGN (ctx))
   12926           31 :         CLASSTYPE_LAZY_MOVE_ASSIGN (ctx) = false;
   12927              :       else
   12928              :         return false;
   12929              :     }
   12930              :   else
   12931              :     return false;
   12932              : 
   12933          955 :   dump (dumper::MERGE) && dump ("Adding implicit member %N", fn);
   12934              : 
   12935          910 :   DECL_CHAIN (fn) = TYPE_FIELDS (ctx);
   12936          910 :   TYPE_FIELDS (ctx) = fn;
   12937              : 
   12938          910 :   add_method (ctx, fn, false);
   12939              : 
   12940              :     /* Propagate TYPE_FIELDS.  */
   12941          910 :   fixup_type_variants (ctx);
   12942              : 
   12943          910 :   return true;
   12944              : }
   12945              : 
   12946              : /* Return non-zero if DECL has a definition that would be interesting to
   12947              :    write out.  */
   12948              : 
   12949              : static bool
   12950      1732257 : has_definition (tree decl)
   12951              : {
   12952      1732263 :   bool is_tmpl = TREE_CODE (decl) == TEMPLATE_DECL;
   12953      1732263 :   if (is_tmpl)
   12954       379270 :     decl = DECL_TEMPLATE_RESULT (decl);
   12955              : 
   12956      1732263 :   switch (TREE_CODE (decl))
   12957              :     {
   12958              :     default:
   12959              :       break;
   12960              : 
   12961       644120 :     case FUNCTION_DECL:
   12962       644120 :       if (!DECL_SAVED_TREE (decl))
   12963              :         /* Not defined.  */
   12964              :         break;
   12965              : 
   12966       287536 :       if (DECL_DECLARED_INLINE_P (decl))
   12967              :         return true;
   12968              : 
   12969        23391 :       if (header_module_p ())
   12970              :         /* We always need to write definitions in header modules,
   12971              :            since there's no TU to emit them in otherwise.  */
   12972              :         return true;
   12973              : 
   12974        11322 :       if (DECL_TEMPLATE_INFO (decl))
   12975              :         {
   12976        10039 :           int use_tpl = DECL_USE_TEMPLATE (decl);
   12977              : 
   12978              :           // FIXME: Partial specializations have definitions too.
   12979        10039 :           if (use_tpl < 2)
   12980              :             return true;
   12981              :         }
   12982              : 
   12983              :       /* Coroutine transform functions always need to be emitted
   12984              :          into the importing TU if the ramp function will be.  */
   12985         1343 :       if (DECL_COROUTINE_P (decl))
   12986           12 :         if (tree ramp = DECL_RAMP_FN (decl))
   12987              :           return has_definition (ramp);
   12988              :       break;
   12989              : 
   12990       727412 :     case TYPE_DECL:
   12991       727412 :       {
   12992       727412 :         tree type = TREE_TYPE (decl);
   12993       727412 :         if (type == TYPE_MAIN_VARIANT (type)
   12994       335790 :             && decl == TYPE_NAME (type)
   12995      1063202 :             && (TREE_CODE (type) == ENUMERAL_TYPE
   12996       335790 :                 ? TYPE_VALUES (type) : TYPE_FIELDS (type)))
   12997              :           return true;
   12998              :       }
   12999              :       break;
   13000              : 
   13001        94370 :     case VAR_DECL:
   13002              :       /* DECL_INITIALIZED_P might not be set on a dependent VAR_DECL.  */
   13003        94370 :       if (DECL_LANG_SPECIFIC (decl)
   13004        93644 :           && DECL_TEMPLATE_INFO (decl)
   13005       152680 :           && DECL_INITIAL (decl))
   13006              :         return true;
   13007              :       else
   13008              :         {
   13009        40498 :           if (!DECL_INITIALIZED_P (decl))
   13010              :             /* Not defined.  */
   13011              :             return false;
   13012              : 
   13013        34048 :           if (header_module_p ())
   13014              :             /* We always need to write definitions in header modules,
   13015              :                since there's no TU to emit them in otherwise.  */
   13016              :             return true;
   13017              : 
   13018         9377 :           if (decl_maybe_constant_var_p (decl))
   13019              :             /* We might need its constant value.  */
   13020              :             return true;
   13021              : 
   13022          431 :           if (vague_linkage_p (decl))
   13023              :             /* These are emitted as needed.  */
   13024              :             return true;
   13025              : 
   13026              :           return false;
   13027              :         }
   13028         6869 :       break;
   13029              : 
   13030         6869 :     case CONCEPT_DECL:
   13031         6869 :       if (DECL_INITIAL (decl))
   13032              :         return true;
   13033              : 
   13034              :       break;
   13035              :     }
   13036              : 
   13037              :   return false;
   13038              : }
   13039              : 
   13040              : uintptr_t *
   13041       633441 : trees_in::find_duplicate (tree existing)
   13042              : {
   13043       294370 :   if (!duplicates)
   13044              :     return NULL;
   13045              : 
   13046       409309 :   return duplicates->get (existing);
   13047              : }
   13048              : 
   13049              : /* We're starting to read a duplicate DECL.  EXISTING is the already
   13050              :    known node.  */
   13051              : 
   13052              : void
   13053       622465 : trees_in::register_duplicate (tree decl, tree existing)
   13054              : {
   13055       622465 :   if (!duplicates)
   13056       102785 :     duplicates = new duplicate_hash_map (40);
   13057              : 
   13058       622465 :   bool existed;
   13059       622465 :   uintptr_t &slot = duplicates->get_or_insert (existing, &existed);
   13060       622465 :   gcc_checking_assert (!existed);
   13061       622465 :   slot = reinterpret_cast<uintptr_t> (decl);
   13062              : 
   13063       622465 :   if (TREE_CODE (decl) == TEMPLATE_DECL)
   13064              :     /* Also register the DECL_TEMPLATE_RESULT as a duplicate so
   13065              :        that passing decl's _RESULT to maybe_duplicate naturally
   13066              :        gives us existing's _RESULT back.  */
   13067       291010 :     register_duplicate (DECL_TEMPLATE_RESULT (decl),
   13068       145505 :                         DECL_TEMPLATE_RESULT (existing));
   13069       622465 : }
   13070              : 
   13071              : /* We've read a definition of MAYBE_EXISTING.  If not a duplicate,
   13072              :    return MAYBE_EXISTING (into which the definition should be
   13073              :    installed).  Otherwise return NULL if already known bad, or the
   13074              :    duplicate we read (for ODR checking, or extracting additional merge
   13075              :    information).  */
   13076              : 
   13077              : tree
   13078       339071 : trees_in::odr_duplicate (tree maybe_existing, bool has_defn)
   13079              : {
   13080       339071 :   tree res = NULL_TREE;
   13081              : 
   13082       525189 :   if (uintptr_t *dup = find_duplicate (maybe_existing))
   13083              :     {
   13084       147101 :       if (!(*dup & 1))
   13085       147098 :         res = reinterpret_cast<tree> (*dup);
   13086              :     }
   13087              :   else
   13088              :     res = maybe_existing;
   13089              : 
   13090       339071 :   assert_definition (maybe_existing, res && !has_defn);
   13091              : 
   13092              :   // FIXME: We probably need to return the template, so that the
   13093              :   // template header can be checked?
   13094       339071 :   return res ? STRIP_TEMPLATE (res) : NULL_TREE;
   13095              : }
   13096              : 
   13097              : /* The following writer functions rely on the current behaviour of
   13098              :    depset::hash::add_dependency making the decl and defn depset nodes
   13099              :    depend on eachother.  That way we don't have to worry about seeding
   13100              :    the tree map with named decls that cannot be looked up by name (I.e
   13101              :    template and function parms).  We know the decl and definition will
   13102              :    be in the same cluster, which is what we want.  */
   13103              : 
   13104              : void
   13105       470665 : trees_out::write_function_def (tree decl)
   13106              : {
   13107       470665 :   tree_node (DECL_RESULT (decl));
   13108              : 
   13109       470665 :   {
   13110              :     /* The function body for a non-inline function or function template
   13111              :        is ignored for determining exposures.  This should only matter
   13112              :        for templates (we don't emit the bodies of non-inline functions
   13113              :        to begin with).  */
   13114       470665 :     auto ovr = dep_hash->ignore_exposure_if (!DECL_DECLARED_INLINE_P (decl));
   13115       470665 :     tree_node (DECL_INITIAL (decl));
   13116       470665 :     tree_node (DECL_SAVED_TREE (decl));
   13117       470665 :   }
   13118              : 
   13119      1004554 :   tree_node (DECL_FRIEND_CONTEXT (decl));
   13120              : 
   13121       470665 :   constexpr_fundef *cexpr = retrieve_constexpr_fundef (decl);
   13122              : 
   13123       470665 :   if (streaming_p ())
   13124       235291 :     u (cexpr != nullptr);
   13125       470665 :   if (cexpr)
   13126              :     {
   13127       107377 :       chained_decls (cexpr->parms);
   13128       107377 :       tree_node (cexpr->result);
   13129       107377 :       tree_node (cexpr->body);
   13130              :     }
   13131              : 
   13132       470665 :   function* f = DECL_STRUCT_FUNCTION (decl);
   13133              : 
   13134       470665 :   if (streaming_p ())
   13135              :     {
   13136       235291 :       unsigned flags = 0;
   13137              : 
   13138              :       /* Whether the importer should emit this definition, if used.  */
   13139       235291 :       flags |= 1 * (DECL_NOT_REALLY_EXTERN (decl)
   13140       235291 :                     && (get_importer_interface (decl)
   13141              :                         != importer_interface::external));
   13142              : 
   13143              :       /* Make sure DECL_REALLY_EXTERN and DECL_INTERFACE_KNOWN are consistent
   13144              :          on non-templates or we'll crash later in import_export_decl.  */
   13145       156297 :       gcc_checking_assert (flags || DECL_INTERFACE_KNOWN (decl)
   13146              :                            || (DECL_LANG_SPECIFIC (decl)
   13147              :                                && DECL_LOCAL_DECL_P (decl)
   13148              :                                && DECL_OMP_DECLARE_REDUCTION_P (decl))
   13149              :                            || (DECL_LANG_SPECIFIC (decl)
   13150              :                                && DECL_TEMPLATE_INFO (decl)
   13151              :                                && uses_template_parms (DECL_TI_ARGS (decl))));
   13152              : 
   13153       235291 :       if (f)
   13154              :         {
   13155       234402 :           flags |= 2;
   13156              :           /* These flags are needed in tsubst_lambda_expr.  */
   13157       234402 :           flags |= 4 * f->language->returns_value;
   13158       234402 :           flags |= 8 * f->language->returns_null;
   13159       234402 :           flags |= 16 * f->language->returns_abnormally;
   13160       234402 :           flags |= 32 * f->language->infinite_loop;
   13161              :         }
   13162              : 
   13163       235291 :       u (flags);
   13164              :     }
   13165              : 
   13166       470665 :   if (state && f)
   13167              :     {
   13168       468887 :       state->write_location (*this, f->function_start_locus);
   13169       468887 :       state->write_location (*this, f->function_end_locus);
   13170              :     }
   13171              : 
   13172       470665 :   if (DECL_COROUTINE_P (decl))
   13173              :     {
   13174           36 :       tree ramp = DECL_RAMP_FN (decl);
   13175           36 :       tree_node (ramp);
   13176           36 :       if (!ramp)
   13177              :         {
   13178           24 :           tree_node (DECL_ACTOR_FN (decl));
   13179           24 :           tree_node (DECL_DESTROY_FN (decl));
   13180              :         }
   13181              :     }
   13182       470665 : }
   13183              : 
   13184              : void
   13185            0 : trees_out::mark_function_def (tree)
   13186              : {
   13187            0 : }
   13188              : 
   13189              : bool
   13190       224752 : trees_in::read_function_def (tree decl, tree maybe_template)
   13191              : {
   13192       225287 :   dump () && dump ("Reading function definition %N", decl);
   13193       224752 :   tree result = tree_node ();
   13194       224752 :   tree initial = tree_node ();
   13195       224752 :   tree saved = tree_node ();
   13196       224752 :   tree context = tree_node ();
   13197       224752 :   post_process_data pdata {};
   13198       224752 :   pdata.decl = maybe_template;
   13199              : 
   13200       224752 :   tree maybe_dup = odr_duplicate (maybe_template, DECL_SAVED_TREE (decl));
   13201       449501 :   bool installing = maybe_dup && !DECL_SAVED_TREE (decl);
   13202              : 
   13203       224752 :   constexpr_fundef cexpr;
   13204       224752 :   if (u ())
   13205              :     {
   13206        49193 :       cexpr.parms = chained_decls ();
   13207        49193 :       cexpr.result = tree_node ();
   13208        49193 :       cexpr.body = tree_node ();
   13209        49193 :       cexpr.decl = decl;
   13210              :     }
   13211              :   else
   13212       175559 :     cexpr.decl = NULL_TREE;
   13213              : 
   13214       224752 :   unsigned flags = u ();
   13215       224752 :   if (flags & 2)
   13216              :     {
   13217       223861 :       pdata.start_locus = state->read_location (*this);
   13218       223861 :       pdata.end_locus = state->read_location (*this);
   13219       223861 :       pdata.returns_value = flags & 4;
   13220       223861 :       pdata.returns_null = flags & 8;
   13221       223861 :       pdata.returns_abnormally = flags & 16;
   13222       223861 :       pdata.infinite_loop = flags & 32;
   13223              :     }
   13224              : 
   13225       224752 :   tree coro_actor = NULL_TREE;
   13226       224752 :   tree coro_destroy = NULL_TREE;
   13227       224752 :   tree coro_ramp = NULL_TREE;
   13228       224752 :   if (DECL_COROUTINE_P (decl))
   13229              :     {
   13230           18 :       coro_ramp = tree_node ();
   13231           18 :       if (!coro_ramp)
   13232              :         {
   13233           12 :           coro_actor = tree_node ();
   13234           12 :           coro_destroy = tree_node ();
   13235           12 :           if ((coro_actor == NULL_TREE) != (coro_destroy == NULL_TREE))
   13236            0 :             set_overrun ();
   13237              :         }
   13238              :     }
   13239              : 
   13240       224752 :   if (get_overrun ())
   13241              :     return NULL_TREE;
   13242              : 
   13243       224752 :   if (installing)
   13244              :     {
   13245       138527 :       DECL_NOT_REALLY_EXTERN (decl) = flags & 1;
   13246       138527 :       DECL_RESULT (decl) = result;
   13247       138527 :       DECL_INITIAL (decl) = initial;
   13248       138527 :       DECL_SAVED_TREE (decl) = saved;
   13249              : 
   13250              :       /* Some entities (like anticipated builtins) were declared without
   13251              :          DECL_ARGUMENTS, so update them now.  But don't do it if there's
   13252              :          already an argument list, because we've already built the
   13253              :          definition referencing those merged PARM_DECLs.  */
   13254       138527 :       if (!DECL_ARGUMENTS (decl))
   13255         6426 :         DECL_ARGUMENTS (decl) = DECL_ARGUMENTS (maybe_dup);
   13256              : 
   13257       138527 :       if (context)
   13258         5986 :         SET_DECL_FRIEND_CONTEXT (decl, context);
   13259       138527 :       if (cexpr.decl)
   13260        35097 :         register_constexpr_fundef (cexpr);
   13261              : 
   13262       138527 :       if (coro_ramp)
   13263            6 :         coro_set_ramp_function (decl, coro_ramp);
   13264       138521 :       else if (coro_actor && coro_destroy)
   13265            3 :         coro_set_transform_functions (decl, coro_actor, coro_destroy);
   13266              : 
   13267       138527 :       if (DECL_LOCAL_DECL_P (decl))
   13268              :         /* Block-scope OMP UDRs aren't real functions, and don't need a
   13269              :            function structure to be allocated or to be expanded.  */
   13270            3 :         gcc_checking_assert (DECL_OMP_DECLARE_REDUCTION_P (decl));
   13271              :       else
   13272       138524 :         post_process (pdata);
   13273              :     }
   13274              :   else if (maybe_dup)
   13275              :     {
   13276              :       // FIXME:QOI Check matching defn
   13277              :     }
   13278              : 
   13279              :   return true;
   13280              : }
   13281              : 
   13282              : /* Also for CONCEPT_DECLs.  */
   13283              : 
   13284              : void
   13285       121712 : trees_out::write_var_def (tree decl)
   13286              : {
   13287              :   /* The initializer of a non-inline variable or variable template is
   13288              :      ignored for determining exposures.  */
   13289       121712 :   auto ovr = dep_hash->ignore_exposure_if (VAR_P (decl)
   13290       131943 :                                            && !DECL_INLINE_VAR_P (decl));
   13291              : 
   13292       121712 :   tree init = DECL_INITIAL (decl);
   13293       121712 :   tree_node (init);
   13294       121712 :   if (!init)
   13295              :     {
   13296         1508 :       tree dyn_init = NULL_TREE;
   13297              : 
   13298              :       /* We only need to write initializers in header modules.  */
   13299         2672 :       if (header_module_p () && DECL_NONTRIVIALLY_INITIALIZED_P (decl))
   13300              :         {
   13301          450 :           dyn_init = value_member (decl,
   13302          450 :                                    CP_DECL_THREAD_LOCAL_P (decl)
   13303              :                                    ? tls_aggregates : static_aggregates);
   13304          450 :           gcc_checking_assert (dyn_init);
   13305              :           /* Mark it so write_inits knows this is needed.  */
   13306          450 :           TREE_LANG_FLAG_0 (dyn_init) = true;
   13307          450 :           dyn_init = TREE_PURPOSE (dyn_init);
   13308              :         }
   13309         1508 :       tree_node (dyn_init);
   13310              :     }
   13311       121712 : }
   13312              : 
   13313              : void
   13314            0 : trees_out::mark_var_def (tree)
   13315              : {
   13316            0 : }
   13317              : 
   13318              : bool
   13319        44391 : trees_in::read_var_def (tree decl, tree maybe_template)
   13320              : {
   13321              :   /* Do not mark the virtual table entries as used.  */
   13322        44391 :   bool vtable = VAR_P (decl) && DECL_VTABLE_OR_VTT_P (decl);
   13323        44391 :   unused += vtable;
   13324        44391 :   tree init = tree_node ();
   13325        44391 :   tree dyn_init = init ? NULL_TREE : tree_node ();
   13326        44391 :   unused -= vtable;
   13327              : 
   13328        44391 :   if (get_overrun ())
   13329              :     return false;
   13330              : 
   13331        44391 :   bool initialized = (VAR_P (decl) ? bool (DECL_INITIALIZED_P (decl))
   13332        44391 :                       : bool (DECL_INITIAL (decl)));
   13333        44391 :   tree maybe_dup = odr_duplicate (maybe_template, initialized);
   13334        44391 :   bool installing = maybe_dup && !initialized;
   13335        44391 :   if (installing)
   13336              :     {
   13337        27522 :       DECL_INITIAL (decl) = init;
   13338        27522 :       if (DECL_EXTERNAL (decl))
   13339         3523 :         DECL_NOT_REALLY_EXTERN (decl) = true;
   13340        27522 :       if (VAR_P (decl))
   13341              :         {
   13342        24169 :           DECL_INITIALIZED_P (decl) = true;
   13343        24169 :           if (maybe_dup && DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (maybe_dup))
   13344        23761 :             DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
   13345        24169 :           tentative_decl_linkage (decl);
   13346        24169 :           if (DECL_EXPLICIT_INSTANTIATION (decl)
   13347        24169 :               && !DECL_EXTERNAL (decl))
   13348            9 :             setup_explicit_instantiation_definition_linkage (decl);
   13349              :           /* Class non-template static members are handled in read_class_def.
   13350              :              But still handle specialisations of member templates.  */
   13351        48338 :           if ((!DECL_CLASS_SCOPE_P (decl)
   13352        15738 :                || primary_template_specialization_p (decl))
   13353        32719 :               && (DECL_IMPLICIT_INSTANTIATION (decl)
   13354         8473 :                   || (DECL_EXPLICIT_INSTANTIATION (decl)
   13355           21 :                       && !DECL_EXTERNAL (decl))))
   13356           86 :             note_vague_linkage_variable (decl);
   13357              :         }
   13358        27522 :       if (!dyn_init)
   13359              :         ;
   13360          216 :       else if (CP_DECL_THREAD_LOCAL_P (decl))
   13361           96 :         tls_aggregates = tree_cons (dyn_init, decl, tls_aggregates);
   13362              :       else
   13363          120 :         static_aggregates = tree_cons (dyn_init, decl, static_aggregates);
   13364              :     }
   13365              :   else if (maybe_dup)
   13366              :     {
   13367              :       // FIXME:QOI Check matching defn
   13368              :     }
   13369              : 
   13370              :   return true;
   13371              : }
   13372              : 
   13373              : /* If MEMBER doesn't have an independent life outside the class,
   13374              :    return it (or its TEMPLATE_DECL).  Otherwise NULL.  */
   13375              : 
   13376              : static tree
   13377       230178 : member_owned_by_class (tree member)
   13378              : {
   13379       230178 :   gcc_assert (DECL_P (member));
   13380              : 
   13381              :   /* Clones are owned by their origin.  */
   13382       230178 :   if (DECL_CLONED_FUNCTION_P (member))
   13383              :     return NULL;
   13384              : 
   13385       230178 :   if (TREE_CODE (member) == FIELD_DECL)
   13386              :     /* FIELD_DECLS can have template info in some cases.  We always
   13387              :        want the FIELD_DECL though, as there's never a TEMPLATE_DECL
   13388              :        wrapping them.  */
   13389              :     return member;
   13390              : 
   13391       102393 :   int use_tpl = -1;
   13392       102393 :   if (tree ti = node_template_info (member, use_tpl))
   13393              :     {
   13394              :       // FIXME: Don't bail on things that CANNOT have their own
   13395              :       // template header.  No, make sure they're in the same cluster.
   13396            0 :       if (use_tpl > 0)
   13397              :         return NULL_TREE;
   13398              : 
   13399            0 :       if (DECL_TEMPLATE_RESULT (TI_TEMPLATE (ti)) == member)
   13400       230178 :         member = TI_TEMPLATE (ti);
   13401              :     }
   13402              :   return member;
   13403              : }
   13404              : 
   13405              : void
   13406       169206 : trees_out::write_class_def (tree defn)
   13407              : {
   13408       169206 :   gcc_assert (DECL_P (defn));
   13409       169206 :   if (streaming_p ())
   13410        85000 :     dump () && dump ("Writing class definition %N", defn);
   13411              : 
   13412       169206 :   tree type = TREE_TYPE (defn);
   13413       169206 :   tree_node (TYPE_SIZE (type));
   13414       169206 :   tree_node (TYPE_SIZE_UNIT (type));
   13415       169206 :   tree_node (TYPE_VFIELD (type));
   13416       169206 :   tree_node (TYPE_BINFO (type));
   13417              : 
   13418       169206 :   vec_chained_decls (TYPE_FIELDS (type));
   13419              : 
   13420              :   /* Every class but __as_base has a type-specific.  */
   13421       335612 :   gcc_checking_assert (!TYPE_LANG_SPECIFIC (type) == IS_FAKE_BASE_TYPE (type));
   13422              : 
   13423       169206 :   if (TYPE_LANG_SPECIFIC (type))
   13424              :     {
   13425       166406 :       {
   13426       166406 :         vec<tree, va_gc> *v = CLASSTYPE_MEMBER_VEC (type);
   13427       166406 :         if (!v)
   13428              :           {
   13429        41410 :             gcc_checking_assert (!streaming_p ());
   13430              :             /* Force a class vector.  */
   13431        41410 :             v = set_class_bindings (type, -1);
   13432        41410 :             gcc_checking_assert (v);
   13433              :           }
   13434              : 
   13435       166406 :         unsigned len = v->length ();
   13436       166406 :         if (streaming_p ())
   13437        83180 :           u (len);
   13438      1365900 :         for (unsigned ix = 0; ix != len; ix++)
   13439              :           {
   13440      1199494 :             tree m = (*v)[ix];
   13441      1199494 :             if (TREE_CODE (m) == TYPE_DECL
   13442       355133 :                 && DECL_ARTIFICIAL (m)
   13443      1379465 :                 && TYPE_STUB_DECL (TREE_TYPE (m)) == m)
   13444              :               /* This is a using-decl for a type, or an anonymous
   13445              :                  struct (maybe with a typedef name).  Write the type.  */
   13446        12532 :               m = TREE_TYPE (m);
   13447      1199494 :             tree_node (m);
   13448              :           }
   13449              :       }
   13450       166406 :       tree_node (CLASSTYPE_LAMBDA_EXPR (type));
   13451              : 
   13452              :       /* TYPE_CONTAINS_VPTR_P looks at the vbase vector, which the
   13453              :          reader won't know at this point.  */
   13454       166406 :       int has_vptr = TYPE_CONTAINS_VPTR_P (type);
   13455              : 
   13456       166406 :       if (streaming_p ())
   13457              :         {
   13458        83180 :           unsigned nvbases = vec_safe_length (CLASSTYPE_VBASECLASSES (type));
   13459        83180 :           u (nvbases);
   13460        83180 :           i (has_vptr);
   13461              :         }
   13462              : 
   13463       166406 :       if (has_vptr)
   13464              :         {
   13465         5988 :           tree_vec (CLASSTYPE_PURE_VIRTUALS (type));
   13466         5988 :           tree_pair_vec (CLASSTYPE_VCALL_INDICES (type));
   13467         5988 :           tree_node (CLASSTYPE_KEY_METHOD (type));
   13468              :         }
   13469              :     }
   13470              : 
   13471       169206 :   if (TYPE_LANG_SPECIFIC (type))
   13472              :     {
   13473       166406 :       tree_node (CLASSTYPE_PRIMARY_BINFO (type));
   13474              : 
   13475       166406 :       tree as_base = CLASSTYPE_AS_BASE (type);
   13476       166406 :       if (as_base)
   13477        86171 :         as_base = TYPE_NAME (as_base);
   13478       166406 :       tree_node (as_base);
   13479              : 
   13480              :       /* Write the vtables.  */
   13481       166406 :       tree vtables = CLASSTYPE_VTABLES (type);
   13482       166406 :       vec_chained_decls (vtables);
   13483       339854 :       for (; vtables; vtables = TREE_CHAIN (vtables))
   13484         7042 :         write_definition (vtables);
   13485              : 
   13486       166406 :       {
   13487              :         /* Friend declarations in class definitions are ignored when
   13488              :            determining exposures.  */
   13489       166406 :         auto ovr = dep_hash->ignore_exposure_if (true);
   13490              : 
   13491              :         /* Write the friend classes.  */
   13492       166406 :         tree_list (CLASSTYPE_FRIEND_CLASSES (type), false);
   13493              : 
   13494              :         /* Write the friend functions.  */
   13495       166406 :         for (tree friends = DECL_FRIENDLIST (defn);
   13496       189936 :              friends; friends = TREE_CHAIN (friends))
   13497              :           {
   13498        23530 :             tree_node (FRIEND_NAME (friends));
   13499        23530 :             tree_list (FRIEND_DECLS (friends), false);
   13500              :           }
   13501              :         /* End of friend fns.  */
   13502       166406 :         tree_node (NULL_TREE);
   13503       166406 :       }
   13504              : 
   13505              :       /* Write the decl list.  We don't need to ignore exposures of friend
   13506              :          decls here as any such decls should already have been added and
   13507              :          ignored above.  */
   13508       166406 :       tree_list (CLASSTYPE_DECL_LIST (type), true);
   13509              : 
   13510       166406 :       if (TYPE_CONTAINS_VPTR_P (type))
   13511              :         {
   13512              :           /* Write the thunks.  */
   13513         5988 :           for (tree decls = TYPE_FIELDS (type);
   13514       168950 :                decls; decls = DECL_CHAIN (decls))
   13515       162962 :             if (TREE_CODE (decls) == FUNCTION_DECL
   13516       118024 :                 && DECL_VIRTUAL_P (decls)
   13517       193882 :                 && DECL_THUNKS (decls))
   13518              :               {
   13519         1056 :                 tree_node (decls);
   13520              :                 /* Thunks are always unique, so chaining is ok.  */
   13521         1056 :                 chained_decls (DECL_THUNKS (decls));
   13522              :               }
   13523         5988 :           tree_node (NULL_TREE);
   13524              :         }
   13525              :     }
   13526       169206 : }
   13527              : 
   13528              : void
   13529       230178 : trees_out::mark_class_member (tree member, bool do_defn)
   13530              : {
   13531       230178 :   gcc_assert (DECL_P (member));
   13532              : 
   13533       230178 :   member = member_owned_by_class (member);
   13534       230178 :   if (member)
   13535       460356 :     mark_declaration (member, do_defn && has_definition (member));
   13536       230178 : }
   13537              : 
   13538              : void
   13539       169234 : trees_out::mark_class_def (tree defn)
   13540              : {
   13541       169234 :   gcc_assert (DECL_P (defn));
   13542       169234 :   tree type = TREE_TYPE (defn);
   13543              :   /* Mark the class members that are not type-decls and cannot have
   13544              :      independent definitions.  */
   13545      1878544 :   for (tree member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
   13546      1709310 :     if (TREE_CODE (member) == FIELD_DECL
   13547      1709310 :         || TREE_CODE (member) == USING_DECL
   13548              :         /* A cloned enum-decl from 'using enum unrelated;'   */
   13549      1709310 :         || (TREE_CODE (member) == CONST_DECL
   13550        14886 :             && DECL_CONTEXT (member) == type))
   13551              :       {
   13552       230178 :         mark_class_member (member);
   13553       230178 :         if (TREE_CODE (member) == FIELD_DECL)
   13554       127785 :           if (tree repr = DECL_BIT_FIELD_REPRESENTATIVE (member))
   13555              :             /* If we're marking a class template definition, then
   13556              :                this'll contain the width (as set by grokbitfield)
   13557              :                instead of a decl.  */
   13558         2542 :             if (DECL_P (repr))
   13559         2012 :               mark_declaration (repr, false);
   13560              :       }
   13561              : 
   13562              :   /* Mark the binfo hierarchy.  */
   13563       399882 :   for (tree child = TYPE_BINFO (type); child; child = TREE_CHAIN (child))
   13564       230648 :     mark_by_value (child);
   13565              : 
   13566       169234 :   if (TYPE_LANG_SPECIFIC (type))
   13567              :     {
   13568       166410 :       for (tree vtable = CLASSTYPE_VTABLES (type);
   13569       173452 :            vtable; vtable = TREE_CHAIN (vtable))
   13570         7042 :         mark_declaration (vtable, true);
   13571              : 
   13572       166410 :       if (TYPE_CONTAINS_VPTR_P (type))
   13573              :         /* Mark the thunks, they belong to the class definition,
   13574              :            /not/ the thunked-to function.  */
   13575         5988 :         for (tree decls = TYPE_FIELDS (type);
   13576       168950 :              decls; decls = DECL_CHAIN (decls))
   13577       162962 :           if (TREE_CODE (decls) == FUNCTION_DECL)
   13578       118024 :             for (tree thunks = DECL_THUNKS (decls);
   13579       119424 :                  thunks; thunks = DECL_CHAIN (thunks))
   13580         1400 :               mark_declaration (thunks, false);
   13581              :     }
   13582       169234 : }
   13583              : 
   13584              : /* Nop sorting, needed for resorting the member vec.  */
   13585              : 
   13586              : static void
   13587     11044634 : nop (void *, void *, void *)
   13588              : {
   13589     11044634 : }
   13590              : 
   13591              : bool
   13592        66842 : trees_in::read_class_def (tree defn, tree maybe_template)
   13593              : {
   13594        66842 :   gcc_assert (DECL_P (defn));
   13595        67475 :   dump () && dump ("Reading class definition %N", defn);
   13596        66842 :   tree type = TREE_TYPE (defn);
   13597        66842 :   tree size = tree_node ();
   13598        66842 :   tree size_unit = tree_node ();
   13599        66842 :   tree vfield = tree_node ();
   13600        66842 :   tree binfo = tree_node ();
   13601        66842 :   vec<tree, va_gc> *vbase_vec = NULL;
   13602        66842 :   vec<tree, va_gc> *member_vec = NULL;
   13603        66842 :   vec<tree, va_gc> *pure_virts = NULL;
   13604        66842 :   vec<tree_pair_s, va_gc> *vcall_indices = NULL;
   13605        66842 :   tree key_method = NULL_TREE;
   13606        66842 :   tree lambda = NULL_TREE;
   13607              : 
   13608              :   /* Read the fields.  */
   13609        66842 :   vec<tree, va_heap> *fields = vec_chained_decls ();
   13610              : 
   13611        66842 :   if (TYPE_LANG_SPECIFIC (type))
   13612              :     {
   13613        65473 :       if (unsigned len = u ())
   13614              :         {
   13615        65473 :           vec_alloc (member_vec, len);
   13616       590901 :           for (unsigned ix = 0; ix != len; ix++)
   13617              :             {
   13618       525428 :               tree m = tree_node ();
   13619       525428 :               if (get_overrun ())
   13620              :                 break;
   13621       525428 :               if (TYPE_P (m))
   13622         5346 :                 m = TYPE_STUB_DECL (m);
   13623       525428 :               member_vec->quick_push (m);
   13624              :             }
   13625              :         }
   13626        65473 :       lambda = tree_node ();
   13627              : 
   13628        65473 :       if (!get_overrun ())
   13629              :         {
   13630        65473 :           unsigned nvbases = u ();
   13631        65473 :           if (nvbases)
   13632              :             {
   13633          285 :               vec_alloc (vbase_vec, nvbases);
   13634         1300 :               for (tree child = binfo; child; child = TREE_CHAIN (child))
   13635         1015 :                 if (BINFO_VIRTUAL_P (child))
   13636          285 :                   vbase_vec->quick_push (child);
   13637              :             }
   13638              :         }
   13639              : 
   13640        65473 :       if (!get_overrun ())
   13641              :         {
   13642        65473 :           int has_vptr = i ();
   13643        65473 :           if (has_vptr)
   13644              :             {
   13645         2671 :               pure_virts = tree_vec ();
   13646         2671 :               vcall_indices = tree_pair_vec ();
   13647         2671 :               key_method = tree_node ();
   13648              :             }
   13649              :         }
   13650              :     }
   13651              : 
   13652        66842 :   tree maybe_dup = odr_duplicate (maybe_template, TYPE_SIZE (type));
   13653        66842 :   bool installing = maybe_dup && !TYPE_SIZE (type);
   13654        36944 :   if (installing)
   13655              :     {
   13656        36944 :       if (maybe_dup != defn)
   13657              :         {
   13658              :           // FIXME: This is needed on other defns too, almost
   13659              :           // duplicate-decl like?  See is_matching_decl too.
   13660              :           /* Copy flags from the duplicate.  */
   13661          252 :           tree type_dup = TREE_TYPE (maybe_dup);
   13662              : 
   13663              :           /* Core pieces.  */
   13664          252 :           TYPE_MODE_RAW (type) = TYPE_MODE_RAW (type_dup);
   13665          252 :           TYPE_ALIGN_RAW (type) = TYPE_ALIGN_RAW (type_dup);
   13666          504 :           TYPE_WARN_IF_NOT_ALIGN_RAW (type)
   13667          252 :             = TYPE_WARN_IF_NOT_ALIGN_RAW (type_dup);
   13668          252 :           TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (type_dup);
   13669              : 
   13670          252 :           SET_DECL_MODE (defn, DECL_MODE (maybe_dup));
   13671          252 :           DECL_SIZE (defn) = DECL_SIZE (maybe_dup);
   13672          252 :           DECL_SIZE_UNIT (defn) = DECL_SIZE_UNIT (maybe_dup);
   13673          252 :           DECL_ALIGN_RAW (defn) = DECL_ALIGN_RAW (maybe_dup);
   13674          504 :           DECL_WARN_IF_NOT_ALIGN_RAW (defn)
   13675          252 :             = DECL_WARN_IF_NOT_ALIGN_RAW (maybe_dup);
   13676          252 :           DECL_USER_ALIGN (defn) = DECL_USER_ALIGN (maybe_dup);
   13677              : 
   13678          252 :           TYPE_TYPELESS_STORAGE (type) = TYPE_TYPELESS_STORAGE (type_dup);
   13679          252 :           TYPE_CXX_ODR_P (type) = TYPE_CXX_ODR_P (type_dup);
   13680          252 :           TYPE_NO_FORCE_BLK (type) = TYPE_NO_FORCE_BLK (type_dup);
   13681          252 :           TYPE_TRANSPARENT_AGGR (type) = TYPE_TRANSPARENT_AGGR (type_dup);
   13682          504 :           TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type)
   13683          252 :             = TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type_dup);
   13684              : 
   13685          252 :           TYPE_EMPTY_P (type) = TYPE_EMPTY_P (type_dup);
   13686          252 :           TREE_ADDRESSABLE (type) = TREE_ADDRESSABLE (type_dup);
   13687              : 
   13688              :           /* C++ pieces.  */
   13689          252 :           TYPE_POLYMORPHIC_P (type) = TYPE_POLYMORPHIC_P (type_dup);
   13690          252 :           CLASSTYPE_FINAL (type) = CLASSTYPE_FINAL (type_dup);
   13691              : 
   13692          504 :           TYPE_HAS_USER_CONSTRUCTOR (type)
   13693          252 :             = TYPE_HAS_USER_CONSTRUCTOR (type_dup);
   13694          504 :           TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
   13695          252 :             = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type_dup);
   13696          504 :           TYPE_NEEDS_CONSTRUCTING (type)
   13697          252 :             = TYPE_NEEDS_CONSTRUCTING (type_dup);
   13698              : 
   13699          252 :           if (auto ls = TYPE_LANG_SPECIFIC (type_dup))
   13700              :             {
   13701          252 :               if (TYPE_LANG_SPECIFIC (type))
   13702              :                 {
   13703          756 :                   CLASSTYPE_BEFRIENDING_CLASSES (type_dup)
   13704          252 :                     = CLASSTYPE_BEFRIENDING_CLASSES (type);
   13705          252 :                   if (!ANON_AGGR_TYPE_P (type))
   13706          756 :                     CLASSTYPE_TYPEINFO_VAR (type_dup)
   13707          252 :                       = CLASSTYPE_TYPEINFO_VAR (type);
   13708              :                 }
   13709         1143 :               for (tree v = type; v; v = TYPE_NEXT_VARIANT (v))
   13710          891 :                 TYPE_LANG_SPECIFIC (v) = ls;
   13711              :             }
   13712              :         }
   13713              : 
   13714        36944 :       TYPE_SIZE (type) = size;
   13715        36944 :       TYPE_SIZE_UNIT (type) = size_unit;
   13716              : 
   13717        36944 :       if (fields)
   13718              :         {
   13719        36944 :           tree *chain = &TYPE_FIELDS (type);
   13720        36944 :           unsigned len = fields->length ();
   13721       481920 :           for (unsigned ix = 0; ix != len; ix++)
   13722              :             {
   13723       444976 :               tree decl = (*fields)[ix];
   13724              : 
   13725       444976 :               if (!decl)
   13726              :                 {
   13727              :                   /* An anonymous struct with typedef name.  */
   13728            3 :                   tree tdef = (*fields)[ix+1];
   13729            3 :                   decl = TYPE_STUB_DECL (TREE_TYPE (tdef));
   13730            3 :                   gcc_checking_assert (IDENTIFIER_ANON_P (DECL_NAME (decl))
   13731              :                                        && decl != tdef);
   13732              :                 }
   13733              : 
   13734       804285 :               gcc_checking_assert (!*chain == !DECL_CLONED_FUNCTION_P (decl));
   13735       444976 :               *chain = decl;
   13736       444976 :               chain = &DECL_CHAIN (decl);
   13737              : 
   13738       444976 :               if (TREE_CODE (decl) == FIELD_DECL
   13739       444976 :                   && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
   13740              :                 {
   13741          287 :                   tree anon_type = TYPE_MAIN_VARIANT (TREE_TYPE (decl));
   13742          287 :                   if (DECL_NAME (defn) == as_base_identifier)
   13743              :                     /* ANON_AGGR_TYPE_FIELD should already point to the
   13744              :                        original FIELD_DECL; don't overwrite it to point
   13745              :                        to the as-base FIELD_DECL copy.  */
   13746           13 :                     gcc_checking_assert (ANON_AGGR_TYPE_FIELD (anon_type));
   13747              :                   else
   13748          274 :                     ANON_AGGR_TYPE_FIELD (anon_type) = decl;
   13749              :                 }
   13750              : 
   13751       444976 :               if (TREE_CODE (decl) == USING_DECL
   13752       444976 :                   && TREE_CODE (USING_DECL_SCOPE (decl)) == RECORD_TYPE)
   13753              :                 {
   13754              :                   /* Reconstruct DECL_ACCESS.  */
   13755        18299 :                   tree decls = USING_DECL_DECLS (decl);
   13756        18299 :                   tree access = declared_access (decl);
   13757              : 
   13758        21589 :                   for (ovl_iterator iter (decls); iter; ++iter)
   13759              :                     {
   13760         2130 :                       tree d = *iter;
   13761              : 
   13762         2130 :                       retrofit_lang_decl (d);
   13763         2130 :                       tree list = DECL_ACCESS (d);
   13764              : 
   13765         2130 :                       if (!purpose_member (type, list))
   13766         2569 :                         DECL_ACCESS (d) = tree_cons (type, access, list);
   13767              :                     }
   13768              :                 }
   13769              : 
   13770       444976 :               if (TREE_CODE (decl) == VAR_DECL
   13771        13163 :                   && TREE_CODE (maybe_template) != TEMPLATE_DECL)
   13772        11324 :                 note_vague_linkage_variable (decl);
   13773              :             }
   13774              :         }
   13775              : 
   13776        36944 :       TYPE_VFIELD (type) = vfield;
   13777        36944 :       TYPE_BINFO (type) = binfo;
   13778              : 
   13779        36944 :       if (TYPE_LANG_SPECIFIC (type))
   13780              :         {
   13781        36089 :           if (!TYPE_POLYMORPHIC_P (type))
   13782        34520 :             SET_CLASSTYPE_LAMBDA_EXPR (type, lambda);
   13783              :           else
   13784         1569 :             gcc_checking_assert (lambda == NULL_TREE);
   13785              : 
   13786        36089 :           CLASSTYPE_MEMBER_VEC (type) = member_vec;
   13787        36089 :           CLASSTYPE_PURE_VIRTUALS (type) = pure_virts;
   13788        36089 :           CLASSTYPE_VCALL_INDICES (type) = vcall_indices;
   13789              : 
   13790        36089 :           if (TYPE_POLYMORPHIC_P (type))
   13791         1569 :             SET_CLASSTYPE_KEY_METHOD (type, key_method);
   13792              :           else
   13793        34520 :             gcc_checking_assert (key_method == NULL_TREE);
   13794              : 
   13795        36089 :           CLASSTYPE_VBASECLASSES (type) = vbase_vec;
   13796              : 
   13797              :           /* Resort the member vector.  */
   13798        36089 :           resort_type_member_vec (member_vec, NULL, nop, NULL);
   13799              :         }
   13800              :     }
   13801              :   else if (maybe_dup)
   13802              :     {
   13803              :       // FIXME:QOI Check matching defn
   13804              :     }
   13805              : 
   13806        66842 :   if (TYPE_LANG_SPECIFIC (type))
   13807              :     {
   13808        65473 :       tree primary = tree_node ();
   13809        65473 :       tree as_base = tree_node ();
   13810              : 
   13811        65473 :       if (as_base)
   13812        33663 :         as_base = TREE_TYPE (as_base);
   13813              : 
   13814              :       /* Read the vtables.  */
   13815        65473 :       vec<tree, va_heap> *vtables = vec_chained_decls ();
   13816        65473 :       if (vtables)
   13817              :         {
   13818         2629 :           unsigned len = vtables->length ();
   13819         5739 :           for (unsigned ix = 0; ix != len; ix++)
   13820              :             {
   13821         3110 :               tree vtable = (*vtables)[ix];
   13822         3110 :               read_var_def (vtable, vtable);
   13823              :             }
   13824              :         }
   13825              : 
   13826        65473 :       tree friend_classes = tree_list (false);
   13827        65473 :       tree friend_functions = NULL_TREE;
   13828        65473 :       for (tree *chain = &friend_functions;
   13829        77561 :            tree name = tree_node (); chain = &TREE_CHAIN (*chain))
   13830              :         {
   13831        12088 :           tree val = tree_list (false);
   13832        12088 :           *chain = build_tree_list (name, val);
   13833        12088 :         }
   13834        65473 :       tree decl_list = tree_list (true);
   13835              : 
   13836        65473 :       if (installing)
   13837              :         {
   13838        36089 :           CLASSTYPE_PRIMARY_BINFO (type) = primary;
   13839        36089 :           CLASSTYPE_AS_BASE (type) = as_base;
   13840              : 
   13841        36089 :           if (vtables)
   13842              :             {
   13843         1581 :               if ((!CLASSTYPE_KEY_METHOD (type)
   13844              :                    /* Sneaky user may have defined it inline
   13845              :                       out-of-class.  */
   13846         1116 :                    || DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (type)))
   13847              :                   /* An imported non-template class attached to a module
   13848              :                      doesn't need to have its vtables emitted here.  */
   13849         1725 :                   && (CLASSTYPE_USE_TEMPLATE (type)
   13850          279 :                       || !DECL_MODULE_ATTACH_P (defn)))
   13851         1119 :                 vec_safe_push (keyed_classes, type);
   13852         1581 :               unsigned len = vtables->length ();
   13853         1581 :               tree *chain = &CLASSTYPE_VTABLES (type);
   13854         3460 :               for (unsigned ix = 0; ix != len; ix++)
   13855              :                 {
   13856         1879 :                   tree vtable = (*vtables)[ix];
   13857         1879 :                   gcc_checking_assert (!*chain);
   13858         1879 :                   *chain = vtable;
   13859         1879 :                   chain = &DECL_CHAIN (vtable);
   13860              :                 }
   13861              :             }
   13862        36089 :           CLASSTYPE_FRIEND_CLASSES (type) = friend_classes;
   13863        36089 :           DECL_FRIENDLIST (defn) = friend_functions;
   13864        36089 :           CLASSTYPE_DECL_LIST (type) = decl_list;
   13865              : 
   13866        39138 :           for (; friend_classes; friend_classes = TREE_CHAIN (friend_classes))
   13867              :             {
   13868         3049 :               tree f = TREE_VALUE (friend_classes);
   13869         3049 :               if (TREE_CODE (f) == TEMPLATE_DECL)
   13870         1251 :                 f = TREE_TYPE (f);
   13871              : 
   13872         3049 :               if (CLASS_TYPE_P (f))
   13873              :                 {
   13874         3011 :                   CLASSTYPE_BEFRIENDING_CLASSES (f)
   13875         6022 :                     = tree_cons (NULL_TREE, type,
   13876         3011 :                                  CLASSTYPE_BEFRIENDING_CLASSES (f));
   13877         3055 :                   dump () && dump ("Class %N befriending %C:%N",
   13878            6 :                                    type, TREE_CODE (f), f);
   13879              :                 }
   13880              :             }
   13881              : 
   13882        43351 :           for (; friend_functions;
   13883         7262 :                friend_functions = TREE_CHAIN (friend_functions))
   13884         7262 :             for (tree friend_decls = TREE_VALUE (friend_functions);
   13885        16520 :                  friend_decls; friend_decls = TREE_CHAIN (friend_decls))
   13886              :               {
   13887         9258 :                 tree f = TREE_VALUE (friend_decls);
   13888         9258 :                 if (TREE_CODE (f) == TU_LOCAL_ENTITY)
   13889           36 :                   continue;
   13890              : 
   13891         9222 :                 DECL_BEFRIENDING_CLASSES (f)
   13892         9222 :                   = tree_cons (NULL_TREE, type, DECL_BEFRIENDING_CLASSES (f));
   13893         9288 :                 dump () && dump ("Class %N befriending %C:%N",
   13894           30 :                                  type, TREE_CODE (f), f);
   13895              :               }
   13896              :         }
   13897              : 
   13898        65473 :       if (TYPE_CONTAINS_VPTR_P (type))
   13899              :         /* Read and install the thunks.  */
   13900         3109 :         while (tree vfunc = tree_node ())
   13901              :           {
   13902          438 :             tree thunks = chained_decls ();
   13903          438 :             if (installing)
   13904          270 :               SET_DECL_THUNKS (vfunc, thunks);
   13905              :           }
   13906              : 
   13907        65473 :       vec_free (vtables);
   13908              :     }
   13909              : 
   13910              :   /* Propagate to all variants.  */
   13911        66842 :   if (installing)
   13912        36944 :     fixup_type_variants (type);
   13913              : 
   13914              :   /* IS_FAKE_BASE_TYPE is inaccurate at this point, because if this is
   13915              :      the fake base, we've not hooked it into the containing class's
   13916              :      data structure yet.  Fortunately it has a unique name.  */
   13917        36944 :   if (installing
   13918        36944 :       && DECL_NAME (defn) != as_base_identifier
   13919        36089 :       && (!CLASSTYPE_TEMPLATE_INFO (type)
   13920        30470 :           || !uses_template_parms (TI_ARGS (CLASSTYPE_TEMPLATE_INFO (type)))))
   13921              :     /* Emit debug info.  It'd be nice to know if the interface TU
   13922              :        already emitted this.  */
   13923        20052 :     rest_of_type_compilation (type, !LOCAL_CLASS_P (type));
   13924              : 
   13925        66842 :   vec_free (fields);
   13926              : 
   13927        66842 :   return !get_overrun ();
   13928              : }
   13929              : 
   13930              : void
   13931         8320 : trees_out::write_enum_def (tree decl)
   13932              : {
   13933         8320 :   tree type = TREE_TYPE (decl);
   13934              : 
   13935         8320 :   tree_node (TYPE_VALUES (type));
   13936              :   /* Note that we stream TYPE_MIN/MAX_VALUE directly as part of the
   13937              :      ENUMERAL_TYPE.  */
   13938         8320 : }
   13939              : 
   13940              : void
   13941         8320 : trees_out::mark_enum_def (tree decl)
   13942              : {
   13943         8320 :   tree type = TREE_TYPE (decl);
   13944              : 
   13945        42972 :   for (tree values = TYPE_VALUES (type); values; values = TREE_CHAIN (values))
   13946              :     {
   13947        34652 :       tree cst = TREE_VALUE (values);
   13948        34652 :       mark_by_value (cst);
   13949              :       /* We must mark the init to avoid circularity in tt_enum_int.  */
   13950        34652 :       if (tree init = DECL_INITIAL (cst))
   13951        34324 :         if (TREE_CODE (init) == INTEGER_CST)
   13952        33656 :           mark_by_value (init);
   13953              :     }
   13954         8320 : }
   13955              : 
   13956              : bool
   13957         3086 : trees_in::read_enum_def (tree defn, tree maybe_template)
   13958              : {
   13959         3086 :   tree type = TREE_TYPE (defn);
   13960         3086 :   tree values = tree_node ();
   13961              : 
   13962         3086 :   if (get_overrun ())
   13963              :     return false;
   13964              : 
   13965         3086 :   tree maybe_dup = odr_duplicate (maybe_template, TYPE_VALUES (type));
   13966         6172 :   bool installing = maybe_dup && !TYPE_VALUES (type);
   13967              : 
   13968         3086 :   if (installing)
   13969              :     {
   13970         1567 :       TYPE_VALUES (type) = values;
   13971              :       /* Note that we stream TYPE_MIN/MAX_VALUE directly as part of the
   13972              :          ENUMERAL_TYPE.  */
   13973              : 
   13974         2534 :       rest_of_type_compilation (type, DECL_NAMESPACE_SCOPE_P (defn));
   13975              :     }
   13976         1519 :   else if (maybe_dup)
   13977              :     {
   13978         1519 :       tree known = TYPE_VALUES (type);
   13979         8472 :       for (; known && values;
   13980         6953 :            known = TREE_CHAIN (known), values = TREE_CHAIN (values))
   13981              :         {
   13982         6962 :           tree known_decl = TREE_VALUE (known);
   13983         6962 :           tree new_decl = TREE_VALUE (values);
   13984              : 
   13985         6962 :           if (DECL_NAME (known_decl) != DECL_NAME (new_decl))
   13986              :             break;
   13987              : 
   13988         6956 :           new_decl = maybe_duplicate (new_decl);
   13989              : 
   13990         6956 :           if (!cp_tree_equal (DECL_INITIAL (known_decl),
   13991         6956 :                               DECL_INITIAL (new_decl)))
   13992              :             break;
   13993              :         }
   13994              : 
   13995         1519 :       if (known || values)
   13996              :         {
   13997           12 :           auto_diagnostic_group d;
   13998           12 :           error_at (DECL_SOURCE_LOCATION (maybe_dup),
   13999              :                     "definition of %qD does not match", maybe_dup);
   14000           12 :           inform (DECL_SOURCE_LOCATION (defn),
   14001              :                   "existing definition %qD", defn);
   14002              : 
   14003           12 :           tree known_decl = NULL_TREE, new_decl = NULL_TREE;
   14004              : 
   14005           12 :           if (known)
   14006            9 :             known_decl = TREE_VALUE (known);
   14007           12 :           if (values)
   14008           12 :             new_decl = maybe_duplicate (TREE_VALUE (values));
   14009              : 
   14010           12 :           if (known_decl && new_decl)
   14011              :             {
   14012            9 :               inform (DECL_SOURCE_LOCATION (new_decl),
   14013              :                       "enumerator %qD does not match ...", new_decl);
   14014            9 :               inform (DECL_SOURCE_LOCATION (known_decl),
   14015              :                       "... this enumerator %qD", known_decl);
   14016              :             }
   14017            3 :           else if (known_decl || new_decl)
   14018              :             {
   14019            3 :               tree extra = known_decl ? known_decl : new_decl;
   14020            3 :               inform (DECL_SOURCE_LOCATION (extra),
   14021              :                       "additional enumerators beginning with %qD", extra);
   14022              :             }
   14023              :           else
   14024            0 :             inform (DECL_SOURCE_LOCATION (maybe_dup),
   14025              :                     "enumeration range differs");
   14026              : 
   14027              :           /* Mark it bad.  */
   14028           12 :           unmatched_duplicate (maybe_template);
   14029           12 :         }
   14030              :     }
   14031              : 
   14032              :   return true;
   14033              : }
   14034              : 
   14035              : /* Write out the body of DECL.  See above circularity note.  */
   14036              : 
   14037              : void
   14038       769903 : trees_out::write_definition (tree decl, bool refs_tu_local)
   14039              : {
   14040       769903 :   auto ovr = make_temp_override (writing_local_entities,
   14041       769903 :                                  writing_local_entities || refs_tu_local);
   14042              : 
   14043       769903 :   if (streaming_p ())
   14044              :     {
   14045       384861 :       assert_definition (decl);
   14046       384861 :       dump ()
   14047          952 :         && dump ("Writing definition %C:%N", TREE_CODE (decl), decl);
   14048              :     }
   14049              :   else
   14050       385042 :     dump (dumper::DEPEND)
   14051           96 :       && dump ("Depending definition %C:%N", TREE_CODE (decl), decl);
   14052              : 
   14053      1199390 :  again:
   14054      1199390 :   switch (TREE_CODE (decl))
   14055              :     {
   14056            0 :     default:
   14057            0 :       gcc_unreachable ();
   14058              : 
   14059       429487 :     case TEMPLATE_DECL:
   14060       429487 :       decl = DECL_TEMPLATE_RESULT (decl);
   14061       429487 :       goto again;
   14062              : 
   14063       470665 :     case FUNCTION_DECL:
   14064       470665 :       write_function_def (decl);
   14065       470665 :       break;
   14066              : 
   14067       177526 :     case TYPE_DECL:
   14068       177526 :       {
   14069       177526 :         tree type = TREE_TYPE (decl);
   14070       177526 :         gcc_assert (TYPE_MAIN_VARIANT (type) == type
   14071              :                     && TYPE_NAME (type) == decl);
   14072       177526 :         if (TREE_CODE (type) == ENUMERAL_TYPE)
   14073         8320 :           write_enum_def (decl);
   14074              :         else
   14075       169206 :           write_class_def (decl);
   14076              :       }
   14077              :       break;
   14078              : 
   14079       121712 :     case VAR_DECL:
   14080       121712 :     case CONCEPT_DECL:
   14081       121712 :       write_var_def (decl);
   14082       121712 :       break;
   14083              :     }
   14084       769903 : }
   14085              : 
   14086              : /* Mark a declaration for by-value walking.  If DO_DEFN is true, mark
   14087              :    its body too.  */
   14088              : 
   14089              : void
   14090      3229838 : trees_out::mark_declaration (tree decl, bool do_defn)
   14091              : {
   14092      3229838 :   mark_by_value (decl);
   14093              : 
   14094      3229838 :   if (TREE_CODE (decl) == TEMPLATE_DECL)
   14095      1062195 :     decl = DECL_TEMPLATE_RESULT (decl);
   14096              : 
   14097      3229838 :   if (!do_defn)
   14098              :     return;
   14099              : 
   14100       769925 :   switch (TREE_CODE (decl))
   14101              :     {
   14102            0 :     default:
   14103            0 :       gcc_unreachable ();
   14104              : 
   14105              :     case FUNCTION_DECL:
   14106              :       mark_function_def (decl);
   14107              :       break;
   14108              : 
   14109       177554 :     case TYPE_DECL:
   14110       177554 :       {
   14111       177554 :         tree type = TREE_TYPE (decl);
   14112       177554 :         gcc_assert (TYPE_MAIN_VARIANT (type) == type
   14113              :                     && TYPE_NAME (type) == decl);
   14114       177554 :         if (TREE_CODE (type) == ENUMERAL_TYPE)
   14115         8320 :           mark_enum_def (decl);
   14116              :         else
   14117       169234 :           mark_class_def (decl);
   14118              :       }
   14119              :       break;
   14120              : 
   14121              :     case VAR_DECL:
   14122              :     case CONCEPT_DECL:
   14123              :       mark_var_def (decl);
   14124              :       break;
   14125              :     }
   14126              : }
   14127              : 
   14128              : /* Read in the body of DECL.  See above circularity note.  */
   14129              : 
   14130              : bool
   14131       335961 : trees_in::read_definition (tree decl)
   14132              : {
   14133       337264 :   dump () && dump ("Reading definition %C %N", TREE_CODE (decl), decl);
   14134              : 
   14135              :   tree maybe_template = decl;
   14136              : 
   14137       335961 :  again:
   14138       536883 :   switch (TREE_CODE (decl))
   14139              :     {
   14140              :     default:
   14141              :       break;
   14142              : 
   14143       200922 :     case TEMPLATE_DECL:
   14144       200922 :       decl = DECL_TEMPLATE_RESULT (decl);
   14145       200922 :       goto again;
   14146              : 
   14147       224752 :     case FUNCTION_DECL:
   14148       224752 :       return read_function_def (decl, maybe_template);
   14149              : 
   14150        69928 :     case TYPE_DECL:
   14151        69928 :       {
   14152        69928 :         tree type = TREE_TYPE (decl);
   14153        69928 :         gcc_assert (TYPE_MAIN_VARIANT (type) == type
   14154              :                     && TYPE_NAME (type) == decl);
   14155        69928 :         if (TREE_CODE (type) == ENUMERAL_TYPE)
   14156         3086 :           return read_enum_def (decl, maybe_template);
   14157              :         else
   14158        66842 :           return read_class_def (decl, maybe_template);
   14159              :       }
   14160        41281 :       break;
   14161              : 
   14162        41281 :     case VAR_DECL:
   14163        41281 :     case CONCEPT_DECL:
   14164        41281 :       return read_var_def (decl, maybe_template);
   14165              :     }
   14166              : 
   14167              :   return false;
   14168              : }
   14169              : 
   14170              : /* Lookup an maybe insert a slot for depset for KEY.  */
   14171              : 
   14172              : depset **
   14173     15775987 : depset::hash::entity_slot (tree entity, bool insert)
   14174              : {
   14175     15775987 :   traits::compare_type key (entity, NULL);
   14176     23801516 :   depset **slot = find_slot_with_hash (key, traits::hash (key),
   14177              :                                        insert ? INSERT : NO_INSERT);
   14178              : 
   14179     15775987 :   return slot;
   14180              : }
   14181              : 
   14182              : depset **
   14183       192295 : depset::hash::binding_slot (tree ctx, tree name, bool insert)
   14184              : {
   14185       192295 :   traits::compare_type key (ctx, name);
   14186       241294 :   depset **slot = find_slot_with_hash (key, traits::hash (key),
   14187              :                                        insert ? INSERT : NO_INSERT);
   14188              : 
   14189       192295 :   return slot;
   14190              : }
   14191              : 
   14192              : depset *
   14193      7646259 : depset::hash::find_dependency (tree decl)
   14194              : {
   14195      7646259 :   depset **slot = entity_slot (decl, false);
   14196              : 
   14197      7646259 :   return slot ? *slot : NULL;
   14198              : }
   14199              : 
   14200              : depset *
   14201        48999 : depset::hash::find_binding (tree ctx, tree name)
   14202              : {
   14203        48999 :   depset **slot = binding_slot (ctx, name, false);
   14204              : 
   14205        48999 :   return slot ? *slot : NULL;
   14206              : }
   14207              : 
   14208              : static bool is_tu_local_entity (tree decl, bool explain = false);
   14209              : static bool is_tu_local_value (tree decl, tree expr, bool explain = false);
   14210              : static bool has_tu_local_tmpl_arg (tree decl, tree args, bool explain);
   14211              : 
   14212              : /* Returns true if DECL is a TU-local entity, as defined by [basic.link].
   14213              :    If EXPLAIN is true, emit an informative note about why DECL is TU-local.  */
   14214              : 
   14215              : static bool
   14216      3486866 : is_tu_local_entity (tree decl, bool explain/*=false*/)
   14217              : {
   14218      3486866 :   gcc_checking_assert (DECL_P (decl));
   14219      3486866 :   location_t loc = DECL_SOURCE_LOCATION (decl);
   14220      3486866 :   tree type = TREE_TYPE (decl);
   14221              : 
   14222              :   /* Only types, functions, variables, and template (specialisations)
   14223              :      can be TU-local.  */
   14224      3486866 :   if (TREE_CODE (decl) != TYPE_DECL
   14225              :       && TREE_CODE (decl) != FUNCTION_DECL
   14226              :       && TREE_CODE (decl) != VAR_DECL
   14227              :       && TREE_CODE (decl) != TEMPLATE_DECL)
   14228              :     return false;
   14229              : 
   14230              :   /* An explicit type alias is not an entity; we don't want to stream
   14231              :      such aliases if they refer to TU-local entities, so propagate this
   14232              :      from the original type. The built-in declarations of 'int' and such
   14233              :      are never TU-local.  */
   14234      3483613 :   if (TREE_CODE (decl) == TYPE_DECL
   14235      1135246 :       && !DECL_SELF_REFERENCE_P (decl)
   14236      4573506 :       && !DECL_IMPLICIT_TYPEDEF_P (decl))
   14237              :     {
   14238       589304 :       tree orig = DECL_ORIGINAL_TYPE (decl);
   14239       589304 :       if (orig && TYPE_NAME (orig))
   14240              :         {
   14241       122097 :           if (explain)
   14242           11 :             inform (loc, "%qD is an alias of TU-local type %qT", decl, orig);
   14243       122097 :           return is_tu_local_entity (TYPE_NAME (orig), explain);
   14244              :         }
   14245              :       else
   14246              :         return false;
   14247              :     }
   14248              : 
   14249              :   /* Check specializations first for slightly better explanations.  */
   14250      2894309 :   int use_tpl = -1;
   14251      2894309 :   tree ti = node_template_info (decl, use_tpl);
   14252      3520998 :   if (use_tpl > 0 && TREE_CODE (TI_TEMPLATE (ti)) == TEMPLATE_DECL)
   14253              :     {
   14254              :       /* A specialization of a TU-local template.  */
   14255       626557 :       tree tmpl = TI_TEMPLATE (ti);
   14256       626557 :       if (is_tu_local_entity (tmpl))
   14257              :         {
   14258           72 :           if (explain)
   14259              :             {
   14260           18 :               inform (loc, "%qD is a specialization of TU-local template %qD",
   14261              :                       decl, tmpl);
   14262           18 :               is_tu_local_entity (tmpl, /*explain=*/true);
   14263              :             }
   14264           72 :           return true;
   14265              :         }
   14266              : 
   14267              :       /* A specialization of a template with any TU-local template argument.  */
   14268       626485 :       if (has_tu_local_tmpl_arg (decl, TI_ARGS (ti), explain))
   14269              :         return true;
   14270              : 
   14271              :       /* FIXME A specialization of a template whose (possibly instantiated)
   14272              :          declaration is an exposure.  This should always be covered by the
   14273              :          above cases??  */
   14274              :     }
   14275              : 
   14276              :   /* A type, function, variable, or template with internal linkage.  */
   14277      2894205 :   linkage_kind kind = decl_linkage (decl);
   14278      2894205 :   if (kind == lk_internal
   14279              :       /* But although weakrefs are marked static, don't consider them
   14280              :          to be TU-local.  */
   14281      2894205 :       && !lookup_attribute ("weakref", DECL_ATTRIBUTES (decl)))
   14282              :     {
   14283          844 :       if (explain)
   14284          168 :         inform (loc, "%qD declared with internal linkage", decl);
   14285          844 :       return true;
   14286              :     }
   14287              : 
   14288              :   /* Does not have a name with linkage and is declared, or introduced by a
   14289              :      lambda-expression, within the definition of a TU-local entity.  */
   14290      2893361 :   if (kind == lk_none)
   14291              :     {
   14292       307383 :       tree ctx = CP_DECL_CONTEXT (decl);
   14293       387913 :       if (LAMBDA_TYPE_P (type))
   14294        55264 :         if (tree extra = LAMBDA_TYPE_EXTRA_SCOPE (type))
   14295       307383 :           ctx = extra;
   14296              : 
   14297       307383 :       if (TREE_CODE (ctx) == NAMESPACE_DECL)
   14298              :         {
   14299           31 :           if (!TREE_PUBLIC (ctx))
   14300              :             {
   14301            0 :               if (explain)
   14302            0 :                 inform (loc, "%qD has no linkage and is declared in an "
   14303              :                         "anonymous namespace", decl);
   14304            0 :               return true;
   14305              :             }
   14306              :         }
   14307       307352 :       else if (TYPE_P (ctx))
   14308              :         {
   14309        34745 :           tree ctx_decl = TYPE_MAIN_DECL (ctx);
   14310        34745 :           if (is_tu_local_entity (ctx_decl))
   14311              :             {
   14312            6 :               if (explain)
   14313              :                 {
   14314            0 :                   inform (loc, "%qD has no linkage and is declared within "
   14315              :                           "TU-local entity %qT", decl, ctx);
   14316            0 :                   is_tu_local_entity (ctx_decl, /*explain=*/true);
   14317              :                 }
   14318            6 :               return true;
   14319              :             }
   14320              :         }
   14321       272607 :       else if (is_tu_local_entity (ctx))
   14322              :         {
   14323           33 :           if (explain)
   14324              :             {
   14325            6 :               inform (loc, "%qD has no linkage and is declared within "
   14326              :                       "TU-local entity %qD", decl, ctx);
   14327            6 :               is_tu_local_entity (ctx, /*explain=*/true);
   14328              :             }
   14329           33 :           return true;
   14330              :         }
   14331              :     }
   14332              : 
   14333              :   /* A type with no name that is defined outside a class-specifier, function
   14334              :      body, or initializer; or is introduced by a defining-type-specifier that
   14335              :      is used to declare only TU-local entities.
   14336              : 
   14337              :      We consider types with names for linkage purposes as having names, since
   14338              :      these aren't really TU-local.  */
   14339      2893322 :   tree inner = STRIP_TEMPLATE (decl);
   14340      1202845 :   if (inner
   14341      2893322 :       && TREE_CODE (inner) == TYPE_DECL
   14342      2076037 :       && TYPE_ANON_P (type)
   14343        30091 :       && !DECL_SELF_REFERENCE_P (inner)
   14344              :       /* An enum with an enumerator name for linkage.  */
   14345      1230636 :       && !(UNSCOPED_ENUM_P (type) && TYPE_VALUES (type)))
   14346              :     {
   14347        26285 :       tree main_decl = TYPE_MAIN_DECL (type);
   14348        52202 :       if (LAMBDA_TYPE_P (type))
   14349              :         {
   14350              :           /* A lambda expression is, in practice, TU-local iff it has no
   14351              :              mangling scope.  This currently doesn't line up exactly with
   14352              :              the standard's definition due to some ABI issues, but it's
   14353              :              pretty close, and avoids other issues down the line.  */
   14354        51742 :           if (!LAMBDA_TYPE_EXTRA_SCOPE (type))
   14355              :             {
   14356            4 :               if (explain)
   14357            2 :                 inform (loc, "%qT has no name and cannot be differentiated "
   14358              :                         "from similar lambdas in other TUs", type);
   14359            4 :               return true;
   14360              :             }
   14361              :         }
   14362          828 :       else if (!DECL_CLASS_SCOPE_P (main_decl)
   14363          444 :                && !decl_function_context (main_decl))
   14364              :         {
   14365           30 :           if (explain)
   14366           12 :             inform (loc, "%qT has no name and is not defined within a class, "
   14367              :                     "function, or initializer", type);
   14368           30 :           return true;
   14369              :         }
   14370              : 
   14371              :       // FIXME introduced by a defining-type-specifier only declaring TU-local
   14372              :       // entities; does this refer to e.g. 'static struct {} a;"?  I can't
   14373              :       // think of any cases where this isn't covered by earlier cases.  */
   14374              :     }
   14375              : 
   14376              :   return false;
   14377              : }
   14378              : 
   14379              : /* Helper for is_tu_local_entity.  Returns true if one of the ARGS of
   14380              :    DECL is TU-local.  Emits an explanation if EXPLAIN is true.  */
   14381              : 
   14382              : static bool
   14383       711467 : has_tu_local_tmpl_arg (tree decl, tree args, bool explain)
   14384              : {
   14385       711467 :   if (!args || TREE_CODE (args) != TREE_VEC)
   14386              :     return false;
   14387              : 
   14388      1918613 :   for (tree a : tree_vec_range (args))
   14389              :     {
   14390      1207178 :       if (TREE_CODE (a) == TREE_VEC)
   14391              :         {
   14392        84982 :           if (has_tu_local_tmpl_arg (decl, a, explain))
   14393           32 :             return true;
   14394              :         }
   14395              :       else if (!WILDCARD_TYPE_P (a))
   14396              :         {
   14397      1021112 :           if (DECL_P (a) && is_tu_local_entity (a))
   14398              :             {
   14399            0 :               if (explain)
   14400              :                 {
   14401            0 :                   inform (DECL_SOURCE_LOCATION (decl),
   14402              :                           "%qD has TU-local template argument %qD",
   14403              :                           decl, a);
   14404            0 :                   is_tu_local_entity (a, /*explain=*/true);
   14405              :                 }
   14406            0 :               return true;
   14407              :             }
   14408              : 
   14409      1021112 :           if (TYPE_P (a) && TYPE_NAME (a) && is_tu_local_entity (TYPE_NAME (a)))
   14410              :             {
   14411           17 :               if (explain)
   14412              :                 {
   14413            1 :                   inform (DECL_SOURCE_LOCATION (decl),
   14414              :                           "%qD has TU-local template argument %qT",
   14415              :                           decl, a);
   14416            1 :                   is_tu_local_entity (TYPE_NAME (a), /*explain=*/true);
   14417              :                 }
   14418           17 :               return true;
   14419              :             }
   14420              : 
   14421      1021095 :           if (EXPR_P (a) && is_tu_local_value (decl, a, explain))
   14422              :             return true;
   14423              :         }
   14424              :     }
   14425              : 
   14426       711435 :   return false;
   14427              : }
   14428              : 
   14429              : /* Returns true if EXPR (part of the initializer for DECL) is a TU-local value
   14430              :    or object.  Emits an explanation if EXPLAIN is true.  */
   14431              : 
   14432              : static bool
   14433        70042 : is_tu_local_value (tree decl, tree expr, bool explain/*=false*/)
   14434              : {
   14435        70042 :   if (!expr)
   14436              :     return false;
   14437              : 
   14438        68657 :   tree e = expr;
   14439        68657 :   STRIP_ANY_LOCATION_WRAPPER (e);
   14440        68657 :   STRIP_NOPS (e);
   14441        68657 :   if (TREE_CODE (e) == TARGET_EXPR)
   14442            0 :     e = TARGET_EXPR_INITIAL (e);
   14443            0 :   if (!e)
   14444              :     return false;
   14445              : 
   14446              :   /* It is, or is a pointer to, a TU-local function or the object associated
   14447              :      with a TU-local variable.  */
   14448        68657 :   tree object = NULL_TREE;
   14449        68657 :   if (TREE_CODE (e) == ADDR_EXPR)
   14450         1891 :     object = TREE_OPERAND (e, 0);
   14451        66766 :   else if (TREE_CODE (e) == PTRMEM_CST)
   14452            0 :     object = PTRMEM_CST_MEMBER (e);
   14453        66766 :   else if (VAR_OR_FUNCTION_DECL_P (e))
   14454              :     object = e;
   14455              : 
   14456         1891 :   if (object
   14457         2387 :       && VAR_OR_FUNCTION_DECL_P (object)
   14458         2474 :       && is_tu_local_entity (object))
   14459              :     {
   14460           54 :       if (explain)
   14461              :         {
   14462              :           /* We've lost a lot of location information by the time we get here,
   14463              :              so let's just do our best effort.  */
   14464           18 :           auto loc = cp_expr_loc_or_loc (expr, DECL_SOURCE_LOCATION (decl));
   14465           18 :           if (VAR_P (object))
   14466            9 :             inform (loc, "%qD refers to TU-local object %qD", decl, object);
   14467              :           else
   14468            9 :             inform (loc, "%qD refers to TU-local function %qD", decl, object);
   14469           18 :           is_tu_local_entity (object, true);
   14470              :         }
   14471           54 :       return true;
   14472              :     }
   14473              : 
   14474              :   /* It is an object of class or array type and any of its subobjects or
   14475              :      any of the objects or functions to which its non-static data members
   14476              :      of reference type refer is TU-local and is usable in constant
   14477              :      expressions.  */
   14478        68603 :   if (TREE_CODE (e) == CONSTRUCTOR && AGGREGATE_TYPE_P (TREE_TYPE (e)))
   14479        31804 :     for (auto &f : CONSTRUCTOR_ELTS (e))
   14480        24786 :       if (is_tu_local_value (decl, f.value, explain))
   14481              :         return true;
   14482              : 
   14483              :   return false;
   14484              : }
   14485              : 
   14486              : /* Complains if DECL is a TU-local entity imported from a named module.
   14487              :    Returns TRUE if instantiation should fail.  */
   14488              : 
   14489              : bool
   14490   8628613719 : instantiating_tu_local_entity (tree decl)
   14491              : {
   14492   8628613719 :   if (!modules_p ())
   14493              :     return false;
   14494              : 
   14495     27266139 :   if (TREE_CODE (decl) == TU_LOCAL_ENTITY)
   14496              :     {
   14497           92 :       auto_diagnostic_group d;
   14498           92 :       error ("instantiation exposes TU-local entity %qD",
   14499           92 :              TU_LOCAL_ENTITY_NAME (decl));
   14500           92 :       inform (TU_LOCAL_ENTITY_LOCATION (decl), "declared here");
   14501           92 :       return true;
   14502           92 :     }
   14503              : 
   14504              :   /* Currently, only TU-local variables and functions, or possibly
   14505              :      templates thereof, will be emitted from named modules.  */
   14506     27266047 :   tree inner = STRIP_TEMPLATE (decl);
   14507     27266047 :   if (!VAR_OR_FUNCTION_DECL_P (inner))
   14508              :     return false;
   14509              : 
   14510              :   /* From this point we will only be emitting warnings; if we're not
   14511              :      warning about this case then there's no need to check further.  */
   14512      1123059 :   if (!warn_expose_global_module_tu_local
   14513      2246118 :       || !warning_enabled_at (DECL_SOURCE_LOCATION (decl),
   14514      1123059 :                               OPT_Wexpose_global_module_tu_local))
   14515         9000 :     return false;
   14516              : 
   14517      1114059 :   if (!is_tu_local_entity (decl))
   14518              :     return false;
   14519              : 
   14520           65 :   if (!DECL_LANG_SPECIFIC (inner)
   14521          120 :       || !DECL_MODULE_IMPORT_P (inner))
   14522              :     return false;
   14523              : 
   14524              :   /* Referencing TU-local entities from a header is generally OK.
   14525              :      We don't have an easy way to detect if this declaration came
   14526              :      from a header via a separate named module, but we can just
   14527              :      ignore that case for warning purposes.  */
   14528            9 :   unsigned index = import_entity_index (decl);
   14529            9 :   module_state *mod = import_entity_module (index);
   14530            9 :   if (mod->is_header ())
   14531              :     return false;
   14532              : 
   14533            9 :   auto_diagnostic_group d;
   14534            9 :   pedwarn (input_location, OPT_Wexpose_global_module_tu_local,
   14535              :            "instantiation exposes TU-local entity %qD", decl);
   14536            9 :   inform (DECL_SOURCE_LOCATION (decl), "declared here");
   14537              : 
   14538              :   /* We treat TU-local entities from the GMF as not actually being
   14539              :      TU-local as an extension, so allow instantation to proceed.  */
   14540            9 :   return false;
   14541            9 : }
   14542              : 
   14543              : /* DECL is a newly discovered dependency.  Create the depset, if it
   14544              :    doesn't already exist.  Add it to the worklist if so.
   14545              : 
   14546              :    DECL will be an OVL_USING_P OVERLOAD, if it's from a binding that's
   14547              :    a using decl.
   14548              : 
   14549              :    We do not have to worry about adding the same dependency more than
   14550              :    once.  First it's harmless, but secondly the TREE_VISITED marking
   14551              :    prevents us wanting to do it anyway.  */
   14552              : 
   14553              : depset *
   14554      6762631 : depset::hash::make_dependency (tree decl, entity_kind ek)
   14555              : {
   14556              :   /* Make sure we're being told consistent information.  */
   14557     12626080 :   gcc_checking_assert ((ek == EK_NAMESPACE)
   14558              :                        == (TREE_CODE (decl) == NAMESPACE_DECL
   14559              :                            && !DECL_NAMESPACE_ALIAS (decl)));
   14560      6762631 :   gcc_checking_assert (ek != EK_BINDING && ek != EK_REDIRECT);
   14561      6762631 :   gcc_checking_assert (TREE_CODE (decl) != FIELD_DECL
   14562              :                        && (TREE_CODE (decl) != USING_DECL
   14563              :                            || TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL));
   14564      6762631 :   gcc_checking_assert (!is_key_order ());
   14565      6762631 :   if (ek == EK_USING)
   14566        29785 :     gcc_checking_assert (TREE_CODE (decl) == OVERLOAD);
   14567      6762631 :   if (ek == EK_TU_LOCAL)
   14568           93 :     gcc_checking_assert (DECL_DECLARES_FUNCTION_P (decl));
   14569              : 
   14570      6762631 :   if (TREE_CODE (decl) == TEMPLATE_DECL)
   14571              :     /* The template should have copied these from its result decl.  */
   14572      2541997 :     gcc_checking_assert (DECL_MODULE_EXPORT_P (decl)
   14573              :                          == DECL_MODULE_EXPORT_P (DECL_TEMPLATE_RESULT (decl)));
   14574              : 
   14575      6762631 :   depset **slot = entity_slot (decl, true);
   14576      6762631 :   depset *dep = *slot;
   14577      6762631 :   bool for_binding = ek == EK_FOR_BINDING;
   14578              : 
   14579      6762631 :   if (!dep)
   14580              :     {
   14581       606998 :       if ((DECL_IMPLICIT_TYPEDEF_P (decl)
   14582              :            /* ... not an enum, for instance.  */
   14583       302395 :            && RECORD_OR_UNION_TYPE_P (TREE_TYPE (decl))
   14584       298008 :            && TYPE_LANG_SPECIFIC (TREE_TYPE (decl))
   14585       272461 :            && CLASSTYPE_USE_TEMPLATE (TREE_TYPE (decl)) == 2)
   14586      2075237 :           || (VAR_P (decl)
   14587        82726 :               && DECL_LANG_SPECIFIC (decl)
   14588        82666 :               && DECL_USE_TEMPLATE (decl) == 2))
   14589              :         {
   14590              :           /* A partial or explicit specialization. Partial
   14591              :              specializations might not be in the hash table, because
   14592              :              there can be multiple differently-constrained variants.
   14593              : 
   14594              :              template<typename T> class silly;
   14595              :              template<typename T> requires true class silly {};
   14596              : 
   14597              :              We need to find them, insert their TEMPLATE_DECL in the
   14598              :              dep_hash, and then convert the dep we just found into a
   14599              :              redirect.  */
   14600              : 
   14601        40583 :           tree ti = get_template_info (decl);
   14602        40583 :           tree tmpl = TI_TEMPLATE (ti);
   14603        40583 :           tree partial = NULL_TREE;
   14604        40583 :           for (tree spec = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
   14605       142191 :                spec; spec = TREE_CHAIN (spec))
   14606       123270 :             if (DECL_TEMPLATE_RESULT (TREE_VALUE (spec)) == decl)
   14607              :               {
   14608              :                 partial = TREE_VALUE (spec);
   14609              :                 break;
   14610              :               }
   14611              : 
   14612        40583 :           if (partial)
   14613              :             {
   14614              :               /* Eagerly create an empty redirect.  The following
   14615              :                  make_dependency call could cause hash reallocation,
   14616              :                  and invalidate slot's value.  */
   14617        21662 :               depset *redirect = make_entity (decl, EK_REDIRECT);
   14618              : 
   14619              :               /* Redirects are never reached -- always snap to their target.  */
   14620        21662 :               redirect->set_flag_bit<DB_UNREACHED_BIT> ();
   14621              : 
   14622        21662 :               *slot = redirect;
   14623              : 
   14624        21662 :               depset *tmpl_dep = make_dependency (partial, EK_PARTIAL);
   14625        21662 :               gcc_checking_assert (tmpl_dep->get_entity_kind () == EK_PARTIAL);
   14626              : 
   14627        21662 :               redirect->deps.safe_push (tmpl_dep);
   14628              : 
   14629        21662 :               return redirect;
   14630              :             }
   14631              :         }
   14632              : 
   14633      1480597 :       bool has_def = ek != EK_USING && has_definition (decl);
   14634      1450812 :       if (ek > EK_BINDING)
   14635       146942 :         ek = EK_DECL;
   14636              : 
   14637              :       /* The only OVERLOADS we should see are USING decls from
   14638              :          bindings.  */
   14639      1480597 :       *slot = dep = make_entity (decl, ek, has_def);
   14640              : 
   14641      1480597 :       if (CHECKING_P && TREE_CODE (decl) == TEMPLATE_DECL)
   14642              :         /* The template_result should otherwise not be in the
   14643              :            table, or be an empty redirect (created above).  */
   14644       379270 :         if (auto *eslot = entity_slot (DECL_TEMPLATE_RESULT (decl), false))
   14645        21662 :           gcc_checking_assert ((*eslot)->get_entity_kind () == EK_REDIRECT
   14646              :                                && !(*eslot)->deps.length ());
   14647              : 
   14648      1480597 :       if (ignore_exposure)
   14649        31060 :         dep->set_flag_bit<DB_IGNORED_EXPOSURE_BIT> ();
   14650              : 
   14651      1480597 :       if (ek != EK_USING)
   14652              :         {
   14653      1450812 :           tree not_tmpl = STRIP_TEMPLATE (decl);
   14654      1450812 :           bool imported_from_module_p = false;
   14655              : 
   14656      1450812 :           if (DECL_LANG_SPECIFIC (not_tmpl)
   14657      2802688 :               && DECL_MODULE_IMPORT_P (not_tmpl))
   14658              :             {
   14659              :               /* Store the module number and index in cluster/section,
   14660              :                  so we don't have to look them up again.  */
   14661        85451 :               unsigned index = import_entity_index (decl);
   14662        85451 :               module_state *from = import_entity_module (index);
   14663              :               /* Remap will be zero for imports from partitions, which
   14664              :                  we want to treat as-if declared in this TU.  */
   14665        85451 :               if (from->remap)
   14666              :                 {
   14667        84709 :                   dep->cluster = index - from->entity_lwm;
   14668        84709 :                   dep->section = from->remap;
   14669        84709 :                   dep->set_flag_bit<DB_IMPORTED_BIT> ();
   14670              : 
   14671        84709 :                   if (!from->is_header ())
   14672      1450812 :                     imported_from_module_p = true;
   14673              :                 }
   14674              :             }
   14675              : 
   14676              :           /* Check for TU-local entities.  This is unnecessary in header
   14677              :              units because we can export internal-linkage decls, and
   14678              :              no declarations are exposures.  Similarly, if the decl was
   14679              :              imported from a non-header module we know it cannot have
   14680              :              been TU-local.  */
   14681      1450812 :           if (!header_module_p () && !imported_from_module_p)
   14682              :             {
   14683       550904 :               if (is_tu_local_entity (decl))
   14684          303 :                 dep->set_flag_bit<DB_TU_LOCAL_BIT> ();
   14685              : 
   14686       550904 :               if (VAR_P (decl)
   14687        30873 :                   && decl_maybe_constant_var_p (decl)
   14688       580787 :                   && is_tu_local_value (decl, DECL_INITIAL (decl)))
   14689              :                 {
   14690              :                   /* A potentially-constant variable initialized to a TU-local
   14691              :                      value is not usable in constant expressions within other
   14692              :                      translation units.  We can achieve this by simply not
   14693              :                      streaming the definition in such cases.  */
   14694           24 :                   dep->clear_flag_bit<DB_DEFN_BIT> ();
   14695              : 
   14696           24 :                   if (DECL_DECLARED_CONSTEXPR_P (decl)
   14697           39 :                       || DECL_INLINE_VAR_P (decl))
   14698              :                     /* A constexpr variable initialized to a TU-local value,
   14699              :                        or an inline value (PR c++/119996), is an exposure.
   14700              : 
   14701              :                        For simplicity, we don't support "non-strict" TU-local
   14702              :                        values: even if the TU-local entity we refer to in the
   14703              :                        initialiser is in the GMF, we still won't consider this
   14704              :                        valid in constant expressions in other TUs, and so
   14705              :                        complain accordingly.  */
   14706           15 :                     dep->set_flag_bit<DB_EXPOSE_PURVIEW_BIT> ();
   14707              :                 }
   14708              :             }
   14709              : 
   14710              :           /* A namespace-scope type may be declared in one module unit
   14711              :              and defined in another; make sure that we're found when
   14712              :              completing the class.  */
   14713      1450812 :           if (ek == EK_DECL
   14714       575861 :               && !dep->is_import ()
   14715       567433 :               && dep->has_defn ()
   14716       296762 :               && DECL_NAMESPACE_SCOPE_P (not_tmpl)
   14717       109252 :               && DECL_IMPLICIT_TYPEDEF_P (not_tmpl)
   14718              :               /* Anonymous types can't be forward-declared.  */
   14719      1480494 :               && !IDENTIFIER_ANON_P (DECL_NAME (not_tmpl)))
   14720        29199 :             dep->set_flag_bit<DB_IS_PENDING_BIT> ();
   14721              : 
   14722              :           /* Namespace-scope functions can be found by ADL by template
   14723              :              instantiations in this module.  We need to create bindings
   14724              :              for them so that name lookup recognises they exist, if they
   14725              :              won't be discarded.  add_binding_entity is too early to do
   14726              :              this for GM functions, because if nobody ends up using them
   14727              :              we'll have leftover bindings laying around, and it's tricky
   14728              :              to delete them and any namespaces they've implicitly created
   14729              :              deps on.  The downside is this means we don't pick up on
   14730              :              using-decls, but by [module.global.frag] p3.6 we don't have
   14731              :              to.  */
   14732      1450812 :           if (ek == EK_DECL
   14733      1450812 :               && !for_binding
   14734       428931 :               && !dep->is_import ()
   14735       420509 :               && !dep->is_tu_local ()
   14736       420405 :               && DECL_NAMESPACE_SCOPE_P (decl)
   14737        39213 :               && DECL_DECLARES_FUNCTION_P (decl)
   14738              :               /* Compiler-generated functions won't participate in ADL.  */
   14739        30265 :               && !DECL_ARTIFICIAL (decl)
   14740              :               /* A hidden friend doesn't need a binding.  */
   14741      1474866 :               && !(DECL_LANG_SPECIFIC (not_tmpl)
   14742        24054 :                    && DECL_UNIQUE_FRIEND_P (not_tmpl)))
   14743              :             {
   14744              :               /* This will only affect GM functions.  */
   14745        22760 :               gcc_checking_assert (!DECL_LANG_SPECIFIC (not_tmpl)
   14746              :                                    || !DECL_MODULE_PURVIEW_P (not_tmpl));
   14747              :               /* We shouldn't see any instantiations or specialisations.  */
   14748        11380 :               gcc_checking_assert (!DECL_LANG_SPECIFIC (decl)
   14749              :                                    || !DECL_USE_TEMPLATE (decl));
   14750              : 
   14751        11380 :               tree ns = CP_DECL_CONTEXT (decl);
   14752        11380 :               tree name = DECL_NAME (decl);
   14753        11380 :               depset *binding = find_binding (ns, name);
   14754        11380 :               if (!binding)
   14755              :                 {
   14756         3803 :                   binding = make_binding (ns, name);
   14757         3803 :                   add_namespace_context (binding, ns);
   14758              : 
   14759         3803 :                   depset **slot = binding_slot (ns, name, /*insert=*/true);
   14760         3803 :                   *slot = binding;
   14761              :                 }
   14762              : 
   14763        11380 :               binding->deps.safe_push (dep);
   14764        11380 :               dep->deps.safe_push (binding);
   14765        11415 :               dump (dumper::DEPEND)
   14766            9 :                 && dump ("Built ADL binding for %C:%N",
   14767            9 :                          TREE_CODE (decl), decl);
   14768              :             }
   14769              :         }
   14770              : 
   14771      1480597 :       if (!dep->is_import ())
   14772      1395888 :         worklist.safe_push (dep);
   14773              :     }
   14774      5260372 :   else if (!ignore_exposure)
   14775      4717516 :     dep->clear_flag_bit<DB_IGNORED_EXPOSURE_BIT> ();
   14776              : 
   14777      6740969 :   dump (dumper::DEPEND)
   14778        36430 :     && dump ("%s on %s %C:%N found",
   14779              :              ek == EK_REDIRECT ? "Redirect"
   14780        36430 :              : (for_binding || ek == EK_TU_LOCAL) ? "Binding"
   14781              :              : "Dependency",
   14782        36430 :              dep->entity_kind_name (), TREE_CODE (decl), decl);
   14783              : 
   14784      6740969 :   return dep;
   14785              : }
   14786              : 
   14787              : /* Whether REF is an exposure of a member type of SOURCE.
   14788              : 
   14789              :    This comes up with exposures of class-scope lambdas, that we currently
   14790              :    treat as TU-local due to ABI reasons.  In such a case the type of the
   14791              :    lambda will be exposed in two places, first by the class type it is in
   14792              :    the TYPE_FIELDS list of, and second by the actual member declaring that
   14793              :    lambda.  We only want the second case to warn.  */
   14794              : 
   14795              : static bool
   14796          253 : is_exposure_of_member_type (depset *source, depset *ref)
   14797              : {
   14798          253 :   gcc_checking_assert (source->refs_tu_local (/*strict=*/true)
   14799              :                        && ref->is_tu_local (/*strict=*/true));
   14800          253 :   tree source_entity = STRIP_TEMPLATE (source->get_entity ());
   14801          253 :   tree ref_entity = STRIP_TEMPLATE (ref->get_entity ());
   14802              : 
   14803          253 :   if (!source->is_tu_local (/*strict=*/true)
   14804          241 :       && source_entity
   14805          241 :       && ref_entity
   14806          241 :       && DECL_IMPLICIT_TYPEDEF_P (source_entity)
   14807            2 :       && DECL_IMPLICIT_TYPEDEF_P (ref_entity)
   14808            2 :       && DECL_CLASS_SCOPE_P (ref_entity)
   14809          255 :       && DECL_CONTEXT (ref_entity) == TREE_TYPE (source_entity))
   14810              :     {
   14811            4 :       gcc_checking_assert (LAMBDA_TYPE_P (TREE_TYPE (ref_entity)));
   14812              :       return true;
   14813              :     }
   14814              :   else
   14815              :     return false;
   14816              : }
   14817              : 
   14818              : /* DEP is a newly discovered dependency.  Append it to current's
   14819              :    depset.  */
   14820              : 
   14821              : void
   14822      5025749 : depset::hash::add_dependency (depset *dep)
   14823              : {
   14824      5025749 :   gcc_checking_assert (current && !is_key_order ());
   14825      5025749 :   current->deps.safe_push (dep);
   14826              : 
   14827      5025749 :   if (dep->is_tu_local (/*strict=*/true))
   14828              :     {
   14829          323 :       if (dep->is_tu_local ())
   14830          260 :         current->set_flag_bit<DB_REF_PURVIEW_BIT> ();
   14831              :       else
   14832           63 :         current->set_flag_bit<DB_REF_GLOBAL_BIT> ();
   14833              : 
   14834          323 :       if (!ignore_exposure && !is_exposure_of_member_type (current, dep))
   14835              :         {
   14836          133 :           if (dep->is_tu_local ())
   14837           91 :             current->set_flag_bit<DB_EXPOSE_PURVIEW_BIT> ();
   14838              :           else
   14839           42 :             current->set_flag_bit<DB_EXPOSE_GLOBAL_BIT> ();
   14840              :         }
   14841              :     }
   14842              : 
   14843      5025749 :   if (current->get_entity_kind () == EK_USING
   14844        29785 :       && DECL_IMPLICIT_TYPEDEF_P (dep->get_entity ())
   14845      5030796 :       && TREE_CODE (TREE_TYPE (dep->get_entity ())) == ENUMERAL_TYPE)
   14846              :     {
   14847              :       /* CURRENT is an unwrapped using-decl and DECL is an enum's
   14848              :          implicit typedef.  Is CURRENT a member of the enum?  */
   14849         4692 :       tree c_decl = OVL_FUNCTION (current->get_entity ());
   14850              : 
   14851         4692 :       if (TREE_CODE (c_decl) == CONST_DECL
   14852         9342 :           && (current->deps[0]->get_entity ()
   14853         4650 :               == CP_DECL_CONTEXT (dep->get_entity ())))
   14854              :         /* Make DECL depend on CURRENT.  */
   14855         4596 :         dep->deps.safe_push (current);
   14856              :     }
   14857              : 
   14858              :   /* If two dependencies recursively depend on each other existing within
   14859              :      their own merge keys, we must ensure that the first dep we saw while
   14860              :      walking is written first in this cluster.  See sort_cluster for more
   14861              :      details.  */
   14862      5025749 :   if (writing_merge_key)
   14863              :     {
   14864       889741 :       if (!dep->is_maybe_recursive () && !current->is_maybe_recursive ())
   14865        49320 :         current->set_flag_bit<DB_ENTRY_BIT> ();
   14866       889741 :       dep->set_flag_bit<DB_MAYBE_RECURSIVE_BIT> ();
   14867       889741 :       current->set_flag_bit<DB_MAYBE_RECURSIVE_BIT> ();
   14868              :     }
   14869              : 
   14870      5025749 :   if (dep->is_unreached ())
   14871              :     {
   14872              :       /* The dependency is reachable now.  */
   14873       366840 :       reached_unreached = true;
   14874       366840 :       dep->clear_flag_bit<DB_UNREACHED_BIT> ();
   14875       366840 :       dump (dumper::DEPEND)
   14876           30 :         && dump ("Reaching unreached %s %C:%N", dep->entity_kind_name (),
   14877           30 :                  TREE_CODE (dep->get_entity ()), dep->get_entity ());
   14878              :     }
   14879      5025749 : }
   14880              : 
   14881              : depset *
   14882      7365139 : depset::hash::add_dependency (tree decl, entity_kind ek)
   14883              : {
   14884      7365139 :   depset *dep;
   14885              : 
   14886      7365139 :   if (is_key_order ())
   14887              :     {
   14888      2355977 :       dep = find_dependency (decl);
   14889      2355977 :       if (dep)
   14890              :         {
   14891      1088227 :           current->deps.safe_push (dep);
   14892      1088227 :           dump (dumper::MERGE)
   14893          723 :             && dump ("Key dependency on %s %C:%N found",
   14894          723 :                      dep->entity_kind_name (), TREE_CODE (decl), decl);
   14895              :         }
   14896              :       else
   14897              :         {
   14898              :           /* It's not a mergeable decl, look for it in the original
   14899              :              table.  */
   14900      1267750 :           dep = chain->find_dependency (decl);
   14901      1267750 :           gcc_checking_assert (dep);
   14902              :         }
   14903              :     }
   14904              :   else
   14905              :     {
   14906      5009162 :       dep = make_dependency (decl, ek);
   14907      5009162 :       if (dep->get_entity_kind () != EK_REDIRECT)
   14908      4957954 :         add_dependency (dep);
   14909              :     }
   14910              : 
   14911      7365139 :   return dep;
   14912              : }
   14913              : 
   14914              : void
   14915       595902 : depset::hash::add_namespace_context (depset *dep, tree ns)
   14916              : {
   14917       595902 :   depset *ns_dep = make_dependency (ns, depset::EK_NAMESPACE);
   14918       595902 :   dep->deps.safe_push (ns_dep);
   14919              : 
   14920              :   /* Mark it as special if imported so we don't walk connect when
   14921              :      SCCing.  */
   14922       595902 :   if (!dep->is_binding () && ns_dep->is_import ())
   14923            0 :     dep->set_special ();
   14924       595902 : }
   14925              : 
   14926              : struct add_binding_data
   14927              : {
   14928              :   tree ns;
   14929              :   bitmap partitions;
   14930              :   depset *binding;
   14931              :   depset::hash *hash;
   14932              :   bool met_namespace;
   14933              : };
   14934              : 
   14935              : /* Return true if we are, or contain something that is exported.  */
   14936              : 
   14937              : bool
   14938      5688557 : depset::hash::add_binding_entity (tree decl, WMB_Flags flags, void *data_)
   14939              : {
   14940      5688557 :   auto data = static_cast <add_binding_data *> (data_);
   14941      5688557 :   decl = strip_using_decl (decl);
   14942              : 
   14943      5688557 :   if (!(TREE_CODE (decl) == NAMESPACE_DECL && !DECL_NAMESPACE_ALIAS (decl)))
   14944              :     {
   14945      5680450 :       tree inner = decl;
   14946              : 
   14947      5680450 :       if (TREE_CODE (inner) == CONST_DECL
   14948         7304 :           && TREE_CODE (DECL_CONTEXT (inner)) == ENUMERAL_TYPE
   14949              :           /* A using-decl could make a CONST_DECL purview for a non-purview
   14950              :              enumeration.  */
   14951      5687754 :           && (!DECL_LANG_SPECIFIC (inner) || !DECL_MODULE_PURVIEW_P (inner)))
   14952         7263 :         inner = TYPE_NAME (DECL_CONTEXT (inner));
   14953      5673187 :       else if (TREE_CODE (inner) == TEMPLATE_DECL)
   14954       116467 :         inner = DECL_TEMPLATE_RESULT (inner);
   14955              : 
   14956     11187809 :       if ((!DECL_LANG_SPECIFIC (inner) || !DECL_MODULE_PURVIEW_P (inner))
   14957     11020335 :           && !((flags & WMB_Using) && (flags & WMB_Purview)))
   14958              :         /* Ignore entities not within the module purview.  We'll need to
   14959              :            create bindings for any non-discarded function calls for ADL,
   14960              :            but it's simpler to handle that at the point of use rather
   14961              :            than trying to clear out bindings after the fact.  */
   14962              :         return false;
   14963              : 
   14964       181737 :       if ((flags & WMB_Hidden)
   14965         5401 :           && DECL_LANG_SPECIFIC (inner)
   14966       187138 :           && DECL_UNIQUE_FRIEND_P (inner))
   14967              :         /* Hidden friends will be found via ADL on the class type,
   14968              :            and so do not need to have bindings.  Anticipated builtin
   14969              :            functions and the hidden decl underlying a DECL_LOCAL_DECL_P
   14970              :            also don't need exporting, but we should create a binding
   14971              :            anyway so that we can have a common decl to match against.  */
   14972              :         return false;
   14973              : 
   14974       176424 :       bool internal_decl = false;
   14975       176424 :       if (!header_module_p () && is_tu_local_entity (decl)
   14976       176685 :           && !((flags & WMB_Using) && (flags & WMB_Export)))
   14977              :         {
   14978              :           /* A TU-local entity.  For ADL we still need to create bindings
   14979              :              for internal-linkage functions attached to a named module.  */
   14980          150 :           if (DECL_DECLARES_FUNCTION_P (inner)
   14981          105 :               && DECL_LANG_SPECIFIC (inner)
   14982          360 :               && DECL_MODULE_ATTACH_P (inner))
   14983              :             {
   14984           93 :               gcc_checking_assert (!DECL_MODULE_EXPORT_P (inner));
   14985              :               internal_decl = true;
   14986              :             }
   14987              :           else
   14988              :             return false;
   14989              :         }
   14990              : 
   14991       176262 :       if ((TREE_CODE (decl) == VAR_DECL
   14992       176262 :            || TREE_CODE (decl) == TYPE_DECL)
   14993       176262 :           && DECL_TINFO_P (decl))
   14994              :         /* Ignore TINFO things.  */
   14995              :         return false;
   14996              : 
   14997       176262 :       if (TREE_CODE (decl) == VAR_DECL && DECL_NTTP_OBJECT_P (decl))
   14998              :         /* Ignore NTTP objects.  */
   14999              :         return false;
   15000              : 
   15001       176262 :       if (deduction_guide_p (decl))
   15002              :         {
   15003              :           /* Ignore deduction guides, bindings for them will be created within
   15004              :              find_dependencies for their class template.  But still build a dep
   15005              :              for them so that we don't discard them.  */
   15006         1610 :           data->hash->make_dependency (decl, EK_FOR_BINDING);
   15007         1610 :           return false;
   15008              :         }
   15009              : 
   15010       174652 :       if (!(flags & WMB_Using) && CP_DECL_CONTEXT (decl) != data->ns)
   15011              :         {
   15012              :           /* An unscoped enum constant implicitly brought into the containing
   15013              :              namespace.  We treat this like a using-decl.  */
   15014         3908 :           gcc_checking_assert (TREE_CODE (decl) == CONST_DECL);
   15015              : 
   15016         3908 :           flags = WMB_Flags (flags | WMB_Using);
   15017         3908 :           if (DECL_MODULE_EXPORT_P (TYPE_NAME (TREE_TYPE (decl)))
   15018              :               /* A using-decl can make an enum constant exported for a
   15019              :                  non-exported enumeration.  */
   15020         3908 :               || (DECL_LANG_SPECIFIC (decl) && DECL_MODULE_EXPORT_P (decl)))
   15021         3677 :             flags = WMB_Flags (flags | WMB_Export);
   15022              :         }
   15023              : 
   15024       174652 :       if (!data->binding)
   15025              :         /* No binding to check.  */;
   15026        35961 :       else if (flags & WMB_Using)
   15027              :         {
   15028              :           /* Look in the binding to see if we already have this
   15029              :              using.  */
   15030       100800 :           for (unsigned ix = data->binding->deps.length (); --ix;)
   15031              :             {
   15032        80675 :               depset *d = data->binding->deps[ix];
   15033       161350 :               if (d->get_entity_kind () == EK_USING
   15034        80675 :                   && OVL_FUNCTION (d->get_entity ()) == decl)
   15035              :                 {
   15036            3 :                   if (!(flags & WMB_Hidden))
   15037            3 :                     d->clear_hidden_binding ();
   15038            3 :                   OVL_PURVIEW_P (d->get_entity ()) = true;
   15039            3 :                   if (flags & WMB_Export)
   15040            3 :                     OVL_EXPORT_P (d->get_entity ()) = true;
   15041            3 :                   return bool (flags & WMB_Export);
   15042              :                 }
   15043              :             }
   15044              :         }
   15045        25897 :       else if (flags & WMB_Dups)
   15046              :         {
   15047              :           /* Look in the binding to see if we already have this decl.  */
   15048           78 :           for (unsigned ix = data->binding->deps.length (); --ix;)
   15049              :             {
   15050           39 :               depset *d = data->binding->deps[ix];
   15051           39 :               if (d->get_entity () == decl)
   15052              :                 {
   15053           33 :                   if (!(flags & WMB_Hidden))
   15054           33 :                     d->clear_hidden_binding ();
   15055           33 :                   return false;
   15056              :                 }
   15057              :             }
   15058              :         }
   15059              : 
   15060              :       /* We're adding something.  */
   15061       174616 :       if (!data->binding)
   15062              :         {
   15063       138691 :           data->binding = make_binding (data->ns, DECL_NAME (decl));
   15064       138691 :           data->hash->add_namespace_context (data->binding, data->ns);
   15065              : 
   15066       138691 :           depset **slot = data->hash->binding_slot (data->ns,
   15067       138691 :                                                     DECL_NAME (decl), true);
   15068       138691 :           gcc_checking_assert (!*slot);
   15069       138691 :           *slot = data->binding;
   15070              :         }
   15071              : 
   15072              :       /* Make sure nobody left a tree visited lying about.  */
   15073       174616 :       gcc_checking_assert (!TREE_VISITED (decl));
   15074              : 
   15075       174616 :       if (flags & WMB_Using)
   15076              :         {
   15077        29785 :           decl = ovl_make (decl, NULL_TREE);
   15078        29785 :           OVL_USING_P (decl) = true;
   15079        29785 :           OVL_PURVIEW_P (decl) = true;
   15080        29785 :           if (flags & WMB_Export)
   15081        28806 :             OVL_EXPORT_P (decl) = true;
   15082              :         }
   15083              : 
   15084       174616 :       entity_kind ek = EK_FOR_BINDING;
   15085       174616 :       if (internal_decl)
   15086              :         ek = EK_TU_LOCAL;
   15087       174523 :       else if (flags & WMB_Using)
   15088        29785 :         ek = EK_USING;
   15089              : 
   15090       174616 :       depset *dep = data->hash->make_dependency (decl, ek);
   15091       174616 :       if (flags & WMB_Hidden)
   15092           88 :         dep->set_hidden_binding ();
   15093       174616 :       data->binding->deps.safe_push (dep);
   15094              :       /* Binding and contents are mutually dependent.  */
   15095       174616 :       dep->deps.safe_push (data->binding);
   15096              : 
   15097       174616 :       return (flags & WMB_Using
   15098       174616 :               ? flags & WMB_Export : DECL_MODULE_EXPORT_P (decl));
   15099              :     }
   15100         8107 :   else if (!data->met_namespace)
   15101              :     {
   15102              :       /* Namespace, walk exactly once.  */
   15103         8098 :       data->met_namespace = true;
   15104         8098 :       if (data->hash->add_namespace_entities (decl, data->partitions))
   15105              :         {
   15106              :           /* It contains an exported thing, so it is exported.  */
   15107         1612 :           gcc_checking_assert (DECL_MODULE_PURVIEW_P (decl));
   15108         1612 :           gcc_checking_assert (TREE_PUBLIC (decl) || header_module_p ());
   15109         1612 :           DECL_MODULE_EXPORT_P (decl) = true;
   15110              :         }
   15111              : 
   15112         8098 :       if (DECL_MODULE_PURVIEW_P (decl))
   15113              :         {
   15114         1975 :           data->hash->make_dependency (decl, depset::EK_NAMESPACE);
   15115              : 
   15116         1975 :           return DECL_MODULE_EXPORT_P (decl);
   15117              :         }
   15118              :     }
   15119              : 
   15120              :   return false;
   15121              : }
   15122              : 
   15123              : /* Recursively find all the namespace bindings of NS.  Add a depset
   15124              :    for every binding that contains an export or module-linkage entity.
   15125              :    Add a defining depset for every such decl that we need to write a
   15126              :    definition.  Such defining depsets depend on the binding depset.
   15127              :    Returns true if we contain something exported.  */
   15128              : 
   15129              : bool
   15130        10840 : depset::hash::add_namespace_entities (tree ns, bitmap partitions)
   15131              : {
   15132        12052 :   dump () && dump ("Looking for writables in %N", ns);
   15133        10840 :   dump.indent ();
   15134              : 
   15135        10840 :   unsigned count = 0;
   15136        10840 :   add_binding_data data;
   15137        10840 :   data.ns = ns;
   15138        10840 :   data.partitions = partitions;
   15139        10840 :   data.hash = this;
   15140              : 
   15141     14879548 :   for (tree binding : *DECL_NAMESPACE_BINDINGS (ns))
   15142              :     {
   15143      7434354 :       data.binding = nullptr;
   15144      7434354 :       data.met_namespace = false;
   15145      7434354 :       if (walk_module_binding (binding, partitions, add_binding_entity, &data))
   15146       130990 :         count++;
   15147              :     }
   15148              : 
   15149              :   /* Seed any using-directives so that we emit the relevant namespaces.  */
   15150        11413 :   for (tree udir : NAMESPACE_LEVEL (ns)->using_directives)
   15151          197 :     if (TREE_CODE (udir) == USING_DECL && DECL_MODULE_PURVIEW_P (udir))
   15152              :       {
   15153          168 :         make_dependency (USING_DECL_DECLS (udir), depset::EK_NAMESPACE);
   15154          168 :         if (DECL_MODULE_EXPORT_P (udir))
   15155           97 :           count++;
   15156              :       }
   15157              : 
   15158        10840 :   if (count)
   15159         3887 :     dump () && dump ("Found %u entries", count);
   15160        10840 :   dump.outdent ();
   15161              : 
   15162        10840 :   return count != 0;
   15163              : }
   15164              : 
   15165              : void
   15166          207 : depset::hash::add_partial_entities (vec<tree, va_gc> *partial_classes)
   15167              : {
   15168        18558 :   for (unsigned ix = 0; ix != partial_classes->length (); ix++)
   15169              :     {
   15170        18351 :       tree inner = (*partial_classes)[ix];
   15171              : 
   15172        18351 :       depset *dep = make_dependency (inner, depset::EK_DECL);
   15173              : 
   15174        18351 :       if (dep->get_entity_kind () == depset::EK_REDIRECT)
   15175              :         {
   15176        18351 :           dep = dep->deps[0];
   15177              :           /* We should have recorded the template as a partial
   15178              :              specialization.  */
   15179        18351 :           gcc_checking_assert (dep->get_entity_kind ()
   15180              :                                == depset::EK_PARTIAL);
   15181              : 
   15182              :           /* Only emit GM entities if reached.  */
   15183        18351 :           if (!DECL_LANG_SPECIFIC (inner)
   15184        30610 :               || !DECL_MODULE_PURVIEW_P (inner))
   15185         6891 :             dep->set_flag_bit<DB_UNREACHED_BIT> ();
   15186              :         }
   15187              :       else
   15188              :         {
   15189              :           /* It was an explicit specialization, not a partial one.
   15190              :              We should have already added this.  */
   15191            0 :           gcc_checking_assert (dep->get_entity_kind ()
   15192              :                                == depset::EK_SPECIALIZATION);
   15193            0 :           gcc_checking_assert (dep->is_special ());
   15194              :         }
   15195              :     }
   15196          207 : }
   15197              : 
   15198              : /* Add the members of imported classes that we defined in this TU.
   15199              :    This will also include lazily created implicit member function
   15200              :    declarations.  (All others will be definitions.)  */
   15201              : 
   15202              : void
   15203           12 : depset::hash::add_class_entities (vec<tree, va_gc> *class_members)
   15204              : {
   15205           24 :   for (unsigned ix = 0; ix != class_members->length (); ix++)
   15206              :     {
   15207           12 :       tree defn = (*class_members)[ix];
   15208           12 :       depset *dep = make_dependency (defn, EK_INNER_DECL);
   15209              : 
   15210           12 :       if (dep->get_entity_kind () == EK_REDIRECT)
   15211            0 :         dep = dep->deps[0];
   15212              : 
   15213              :       /* Only non-instantiations need marking as pendings.  */
   15214           24 :       if (dep->get_entity_kind () == EK_DECL)
   15215           12 :         dep->set_flag_bit <DB_IS_PENDING_BIT> ();
   15216              :     }
   15217           12 : }
   15218              : 
   15219              : /* Add any entities found via dependent ADL.  */
   15220              : 
   15221              : void
   15222      6272074 : depset::hash::add_dependent_adl_entities (tree expr)
   15223              : {
   15224      6272074 :   gcc_checking_assert (!is_key_order ());
   15225      6272074 :   if (TREE_CODE (current->get_entity ()) != TEMPLATE_DECL)
   15226      6100315 :     return;
   15227              : 
   15228      3507620 :   dep_adl_info info;
   15229      3507620 :   switch (TREE_CODE (expr))
   15230              :     {
   15231       379434 :     case CALL_EXPR:
   15232       379434 :       if (!KOENIG_LOOKUP_P (expr))
   15233              :         return;
   15234        22059 :       info.name = CALL_EXPR_FN (expr);
   15235        22059 :       if (!info.name)
   15236              :         return;
   15237        21961 :       if (TREE_CODE (info.name) == TEMPLATE_ID_EXPR)
   15238         2639 :         info.name = TREE_OPERAND (info.name, 0);
   15239        21961 :       if (TREE_CODE (info.name) == TU_LOCAL_ENTITY)
   15240              :         return;
   15241        32754 :       if (!identifier_p (info.name))
   15242        21679 :         info.name = OVL_NAME (info.name);
   15243        64664 :       for (int ix = 0; ix < call_expr_nargs (expr); ix++)
   15244        42704 :         vec_safe_push (info.args, CALL_EXPR_ARG (expr, ix));
   15245              :       break;
   15246              : 
   15247        30252 :     case LE_EXPR:
   15248        30252 :     case GE_EXPR:
   15249        30252 :     case LT_EXPR:
   15250        30252 :     case GT_EXPR:
   15251        30252 :       info.rewrite = SPACESHIP_EXPR;
   15252        30252 :       goto overloadable_expr;
   15253              : 
   15254        14829 :     case NE_EXPR:
   15255        14829 :       info.rewrite = EQ_EXPR;
   15256        14829 :       goto overloadable_expr;
   15257              : 
   15258        30677 :     case EQ_EXPR:
   15259              :       /* Not strictly a rewrite candidate, but we need to ensure
   15260              :          that lookup of a matching NE_EXPR can succeed if that
   15261              :          would inhibit a rewrite with reversed parameters.  */
   15262        30677 :       info.rewrite = NE_EXPR;
   15263        30677 :       goto overloadable_expr;
   15264              : 
   15265       233459 :     case COMPOUND_EXPR:
   15266       233459 :     case MEMBER_REF:
   15267       233459 :     case MULT_EXPR:
   15268       233459 :     case TRUNC_DIV_EXPR:
   15269       233459 :     case TRUNC_MOD_EXPR:
   15270       233459 :     case PLUS_EXPR:
   15271       233459 :     case MINUS_EXPR:
   15272       233459 :     case LSHIFT_EXPR:
   15273       233459 :     case RSHIFT_EXPR:
   15274       233459 :     case SPACESHIP_EXPR:
   15275       233459 :     case BIT_AND_EXPR:
   15276       233459 :     case BIT_XOR_EXPR:
   15277       233459 :     case BIT_IOR_EXPR:
   15278       233459 :     case TRUTH_ANDIF_EXPR:
   15279       233459 :     case TRUTH_ORIF_EXPR:
   15280       233459 :     overloadable_expr:
   15281       233459 :       info.name = ovl_op_identifier (TREE_CODE (expr));
   15282       233459 :       gcc_checking_assert (tree_operand_length (expr) == 2);
   15283       233459 :       vec_safe_push (info.args, TREE_OPERAND (expr, 0));
   15284       233459 :       vec_safe_push (info.args, TREE_OPERAND (expr, 1));
   15285       233459 :       break;
   15286              : 
   15287              :     default:
   15288              :       return;
   15289              :     }
   15290              : 
   15291              :   /* If all arguments are type-dependent we don't need to do
   15292              :      anything further, we won't find new entities.  */
   15293       427178 :   processing_template_decl_sentinel ptds;
   15294       255419 :   ++processing_template_decl;
   15295       255419 :   if (!any_type_dependent_arguments_p (info.args))
   15296        83660 :     return;
   15297              : 
   15298              :   /* We need to defer name lookup until after walking, otherwise
   15299              :      we get confused by stray TREE_VISITEDs.  */
   15300       171759 :   dep_adl_entity_list.safe_push (info);
   15301              : }
   15302              : 
   15303              : /* We add the partial & explicit specializations, and the explicit
   15304              :    instantiations.  */
   15305              : 
   15306              : static void
   15307       873276 : specialization_add (bool decl_p, spec_entry *entry, void *data_)
   15308              : {
   15309       873276 :   vec<spec_entry *> *data = reinterpret_cast <vec<spec_entry *> *> (data_);
   15310              : 
   15311       873276 :   if (!decl_p)
   15312              :     {
   15313              :       /* We exclusively use decls to locate things.  Make sure there's
   15314              :          no mismatch between the two specialization tables we keep.
   15315              :          pt.cc optimizes instantiation lookup using a complicated
   15316              :          heuristic.  We don't attempt to replicate that algorithm, but
   15317              :          observe its behaviour and reproduce it upon read back.  */
   15318              : 
   15319       262144 :        gcc_checking_assert (TREE_CODE (entry->spec) == ENUMERAL_TYPE
   15320              :                            || DECL_CLASS_TEMPLATE_P (entry->tmpl));
   15321              : 
   15322       262144 :        gcc_checking_assert (!match_mergeable_specialization (true, entry));
   15323              :     }
   15324       611132 :   else if (VAR_OR_FUNCTION_DECL_P (entry->spec))
   15325       300930 :     gcc_checking_assert (!DECL_LOCAL_DECL_P (entry->spec));
   15326              : 
   15327       873276 :   data->safe_push (entry);
   15328       873276 : }
   15329              : 
   15330              : /* Arbitrary stable comparison.  */
   15331              : 
   15332              : static int
   15333     52620276 : specialization_cmp (const void *a_, const void *b_)
   15334              : {
   15335     52620276 :   const spec_entry *ea = *reinterpret_cast<const spec_entry *const *> (a_);
   15336     52620276 :   const spec_entry *eb = *reinterpret_cast<const spec_entry *const *> (b_);
   15337              : 
   15338     52620276 :   if (ea == eb)
   15339              :     return 0;
   15340              : 
   15341     52620276 :   tree a = ea->spec;
   15342     52620276 :   tree b = eb->spec;
   15343     52620276 :   if (TYPE_P (a))
   15344              :     {
   15345     14988729 :       a = TYPE_NAME (a);
   15346     14988729 :       b = TYPE_NAME (b);
   15347              :     }
   15348              : 
   15349     52620276 :   if (a == b)
   15350              :     /* This can happen with friend specializations.  Just order by
   15351              :        entry address.  See note in depset_cmp.  */
   15352            0 :     return ea < eb ? -1 : +1;
   15353              : 
   15354     52620276 :   return DECL_UID (a) < DECL_UID (b) ? -1 : +1;
   15355              : }
   15356              : 
   15357              : /* We add all kinds of specialializations.  Implicit specializations
   15358              :    should only streamed and walked if they are reachable from
   15359              :    elsewhere.  Hence the UNREACHED flag.  This is making the
   15360              :    assumption that it is cheaper to reinstantiate them on demand
   15361              :    elsewhere, rather than stream them in when we instantiate their
   15362              :    general template.  Also, if we do stream them, we can only do that
   15363              :    if they are not internal (which they can become if they themselves
   15364              :    touch an internal entity?).  */
   15365              : 
   15366              : void
   15367         5484 : depset::hash::add_specializations (bool decl_p)
   15368              : {
   15369         5484 :   vec<spec_entry *> data;
   15370         5484 :   data.create (100);
   15371         5484 :   walk_specializations (decl_p, specialization_add, &data);
   15372         5484 :   data.qsort (specialization_cmp);
   15373       878760 :   while (data.length ())
   15374              :     {
   15375       873276 :       spec_entry *entry = data.pop ();
   15376       873276 :       tree spec = entry->spec;
   15377       873276 :       int use_tpl = 0;
   15378       873276 :       bool is_friend = false;
   15379              : 
   15380       873276 :       if (decl_p && DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (entry->tmpl))
   15381              :         /* A friend of a template.  This is keyed to the
   15382              :            instantiation.  */
   15383              :         is_friend = true;
   15384              : 
   15385       873276 :       if (decl_p)
   15386              :         {
   15387       611132 :           if (tree ti = DECL_TEMPLATE_INFO (spec))
   15388              :             {
   15389       611132 :               tree tmpl = TI_TEMPLATE (ti);
   15390              : 
   15391       611132 :               use_tpl = DECL_USE_TEMPLATE (spec);
   15392       611132 :               if (spec == DECL_TEMPLATE_RESULT (tmpl))
   15393              :                 {
   15394         4502 :                   spec = tmpl;
   15395         4502 :                   gcc_checking_assert (DECL_USE_TEMPLATE (spec) == use_tpl);
   15396              :                 }
   15397       606630 :               else if (is_friend)
   15398              :                 {
   15399         4090 :                   if (TI_TEMPLATE (ti) != entry->tmpl
   15400         4090 :                       || !template_args_equal (TI_ARGS (ti), entry->tmpl))
   15401         4090 :                     goto template_friend;
   15402              :                 }
   15403              :             }
   15404              :           else
   15405              :             {
   15406            0 :             template_friend:;
   15407         4090 :               gcc_checking_assert (is_friend);
   15408              :               /* This is a friend of a template class, but not the one
   15409              :                  that generated entry->spec itself (i.e. it's an
   15410              :                  equivalent clone).  We do not need to record
   15411              :                  this.  */
   15412         4090 :               continue;
   15413              :             }
   15414              :         }
   15415              :       else
   15416              :         {
   15417       262144 :           if (TREE_CODE (spec) == ENUMERAL_TYPE)
   15418              :             {
   15419         1273 :               tree ctx = DECL_CONTEXT (TYPE_NAME (spec));
   15420              : 
   15421         1273 :               if (TYPE_P (ctx))
   15422         1267 :                 use_tpl = CLASSTYPE_USE_TEMPLATE (ctx);
   15423              :               else
   15424            6 :                 use_tpl = DECL_USE_TEMPLATE (ctx);
   15425              :             }
   15426              :           else
   15427       260871 :             use_tpl = CLASSTYPE_USE_TEMPLATE (spec);
   15428              : 
   15429       262144 :           tree ti = TYPE_TEMPLATE_INFO (spec);
   15430       262144 :           tree tmpl = TI_TEMPLATE (ti);
   15431              : 
   15432       262144 :           spec = TYPE_NAME (spec);
   15433       262144 :           if (spec == DECL_TEMPLATE_RESULT (tmpl))
   15434              :             {
   15435         1311 :               spec = tmpl;
   15436         1311 :               use_tpl = DECL_USE_TEMPLATE (spec);
   15437              :             }
   15438              :         }
   15439              : 
   15440       869186 :       bool needs_reaching = false;
   15441       869186 :       if (use_tpl == 1)
   15442              :         /* Implicit instantiations only walked if we reach them.  */
   15443              :         needs_reaching = true;
   15444        72839 :       else if (!DECL_LANG_SPECIFIC (STRIP_TEMPLATE (spec))
   15445       133690 :                || !DECL_MODULE_PURVIEW_P (STRIP_TEMPLATE (spec)))
   15446              :         /* Likewise, GMF explicit or partial specializations.  */
   15447              :         needs_reaching = true;
   15448              : 
   15449              : #if false && CHECKING_P
   15450              :       /* The instantiation isn't always on
   15451              :          DECL_TEMPLATE_INSTANTIATIONS, */
   15452              :       // FIXME: we probably need to remember this information?
   15453              :       /* Verify the specialization is on the
   15454              :          DECL_TEMPLATE_INSTANTIATIONS of the template.  */
   15455              :       for (tree cons = DECL_TEMPLATE_INSTANTIATIONS (entry->tmpl);
   15456              :            cons; cons = TREE_CHAIN (cons))
   15457              :         if (TREE_VALUE (cons) == entry->spec)
   15458              :           {
   15459              :             gcc_assert (entry->args == TREE_PURPOSE (cons));
   15460              :             goto have_spec;
   15461              :           }
   15462              :       gcc_unreachable ();
   15463              :     have_spec:;
   15464              : #endif
   15465              : 
   15466              :       /* Make sure nobody left a tree visited lying about.  */
   15467       869186 :       gcc_checking_assert (!TREE_VISITED (spec));
   15468       869186 :       depset *dep = make_dependency (spec, depset::EK_SPECIALIZATION);
   15469       869186 :       if (dep->is_special ())
   15470            0 :         gcc_unreachable ();
   15471              :       else
   15472              :         {
   15473       869186 :           if (dep->get_entity_kind () == depset::EK_REDIRECT)
   15474        20862 :             dep = dep->deps[0];
   15475       848324 :           else if (dep->get_entity_kind () == depset::EK_SPECIALIZATION)
   15476              :             {
   15477       848324 :               dep->set_special ();
   15478       848324 :               dep->deps.safe_push (reinterpret_cast<depset *> (entry));
   15479       848324 :               if (!decl_p)
   15480       244593 :                 dep->set_flag_bit<DB_TYPE_SPEC_BIT> ();
   15481              :             }
   15482              : 
   15483       869186 :           if (needs_reaching)
   15484       824736 :             dep->set_flag_bit<DB_UNREACHED_BIT> ();
   15485       869186 :           if (is_friend)
   15486            0 :             dep->set_flag_bit<DB_FRIEND_SPEC_BIT> ();
   15487              :         }
   15488              :     }
   15489         5484 :   data.release ();
   15490         5484 : }
   15491              : 
   15492              : /* Add a depset into the mergeable hash.  */
   15493              : 
   15494              : void
   15495       987827 : depset::hash::add_mergeable (depset *mergeable)
   15496              : {
   15497       987827 :   gcc_checking_assert (is_key_order ());
   15498       987827 :   entity_kind ek = mergeable->get_entity_kind ();
   15499       987827 :   tree decl = mergeable->get_entity ();
   15500       987827 :   gcc_checking_assert (ek < EK_DIRECT_HWM);
   15501              : 
   15502       987827 :   depset **slot = entity_slot (decl, true);
   15503       987827 :   gcc_checking_assert (!*slot);
   15504       987827 :   depset *dep = make_entity (decl, ek);
   15505       987827 :   *slot = dep;
   15506              : 
   15507       987827 :   worklist.safe_push (dep);
   15508              : 
   15509              :   /* So we can locate the mergeable depset this depset refers to,
   15510              :      mark the first dep.  */
   15511       987827 :   dep->set_special ();
   15512       987827 :   dep->deps.safe_push (mergeable);
   15513       987827 : }
   15514              : 
   15515              : /* Find the innermost-namespace scope of DECL, and that
   15516              :    namespace-scope decl.  */
   15517              : 
   15518              : tree
   15519     31439336 : find_pending_key (tree decl, tree *decl_p = nullptr)
   15520              : {
   15521     31439336 :   tree ns = decl;
   15522     37810870 :   do
   15523              :     {
   15524     37810870 :       decl = ns;
   15525     37810870 :       ns = CP_DECL_CONTEXT (ns);
   15526     37810870 :       if (TYPE_P (ns))
   15527      3972104 :         ns = TYPE_NAME (ns);
   15528              :     }
   15529     37810870 :   while (TREE_CODE (ns) != NAMESPACE_DECL);
   15530              : 
   15531     31439336 :   if (decl_p)
   15532     30989114 :     *decl_p = decl;
   15533              : 
   15534     31439336 :   return ns;
   15535              : }
   15536              : 
   15537              : /* Creates bindings and dependencies for all deduction guides of
   15538              :    the given class template DECL as needed.  */
   15539              : 
   15540              : void
   15541        47670 : depset::hash::add_deduction_guides (tree decl)
   15542              : {
   15543              :   /* Alias templates never have deduction guides.  */
   15544        47670 :   if (DECL_ALIAS_TEMPLATE_P (decl))
   15545        46862 :     return;
   15546              : 
   15547              :   /* We don't need to do anything for class-scope deduction guides,
   15548              :      as they will be added as members anyway.  */
   15549        47670 :   if (!DECL_NAMESPACE_SCOPE_P (decl))
   15550              :     return;
   15551              : 
   15552        37619 :   tree ns = CP_DECL_CONTEXT (decl);
   15553        37619 :   tree name = dguide_name (decl);
   15554              : 
   15555              :   /* We always add all deduction guides with a given name at once,
   15556              :      so if there's already a binding there's nothing to do.  */
   15557        37619 :   if (find_binding (ns, name))
   15558              :     return;
   15559              : 
   15560        35223 :   tree guides = lookup_qualified_name (ns, name, LOOK_want::NORMAL,
   15561              :                                        /*complain=*/false);
   15562        35223 :   if (guides == error_mark_node)
   15563              :     return;
   15564              : 
   15565          808 :   depset *binding = nullptr;
   15566         3808 :   for (tree t : lkp_range (guides))
   15567              :     {
   15568         2192 :       gcc_checking_assert (!TREE_VISITED (t));
   15569         2192 :       depset *dep = make_dependency (t, EK_FOR_BINDING);
   15570              : 
   15571              :       /* We don't want to create bindings for imported deduction guides, as
   15572              :          this would potentially cause name lookup to return duplicates.  */
   15573         2192 :       if (dep->is_import ())
   15574            6 :         continue;
   15575              : 
   15576         2186 :       if (!binding)
   15577              :         {
   15578              :           /* We have bindings to add.  */
   15579          802 :           binding = make_binding (ns, name);
   15580          802 :           add_namespace_context (binding, ns);
   15581              : 
   15582          802 :           depset **slot = binding_slot (ns, name, /*insert=*/true);
   15583          802 :           *slot = binding;
   15584              :         }
   15585              : 
   15586         2186 :       binding->deps.safe_push (dep);
   15587         2186 :       dep->deps.safe_push (binding);
   15588         2186 :       dump (dumper::DEPEND)
   15589            0 :         && dump ("Built binding for deduction guide %C:%N",
   15590            0 :                  TREE_CODE (decl), decl);
   15591              :     }
   15592              : }
   15593              : 
   15594              : /* Iteratively find dependencies.  During the walk we may find more
   15595              :    entries on the same binding that need walking.  */
   15596              : 
   15597              : void
   15598       272395 : depset::hash::find_dependencies (module_state *module)
   15599              : {
   15600       272395 :   trees_out walker (NULL, module, *this);
   15601       272395 :   vec<depset *> unreached;
   15602       544790 :   unreached.create (worklist.length ());
   15603              : 
   15604         1080 :   for (;;)
   15605              :     {
   15606       273475 :       reached_unreached = false;
   15607      4369506 :       while (worklist.length ())
   15608              :         {
   15609      4096031 :           depset *item = worklist.pop ();
   15610              : 
   15611      4096031 :           gcc_checking_assert (!item->is_binding ());
   15612      4096031 :           if (item->is_unreached ())
   15613      2085010 :             unreached.quick_push (item);
   15614              :           else
   15615              :             {
   15616      2011021 :               current = item;
   15617      2011021 :               tree decl = current->get_entity ();
   15618      2011021 :               dump (is_key_order () ? dumper::MERGE : dumper::DEPEND)
   15619      2012344 :                 && dump ("Dependencies of %s %C:%N",
   15620         1323 :                          is_key_order () ? "key-order"
   15621         1323 :                          : current->entity_kind_name (), TREE_CODE (decl), decl);
   15622      2011021 :               dump.indent ();
   15623      2011021 :               walker.begin ();
   15624      2011021 :               if (current->get_entity_kind () == EK_USING)
   15625        29785 :                 walker.tree_node (OVL_FUNCTION (decl));
   15626      1981236 :               else if (current->get_entity_kind () == EK_TU_LOCAL)
   15627              :                 /* We only stream its name and location.  */
   15628           93 :                 module->note_location (DECL_SOURCE_LOCATION (decl));
   15629      1981143 :               else if (TREE_VISITED (decl))
   15630              :                 /* A global tree.  */;
   15631      1978655 :               else if (current->get_entity_kind () == EK_NAMESPACE)
   15632              :                 {
   15633         2384 :                   module->note_location (DECL_SOURCE_LOCATION (decl));
   15634         2384 :                   add_namespace_context (current, CP_DECL_CONTEXT (decl));
   15635              :                 }
   15636              :               else
   15637              :                 {
   15638      1976271 :                   walker.mark_declaration (decl, current->has_defn ());
   15639              : 
   15640      1976271 :                   if (!is_key_order ()
   15641      1976271 :                       && item->is_pending_entity ())
   15642              :                     {
   15643       450222 :                       tree ns = find_pending_key (decl, nullptr);
   15644       450222 :                       add_namespace_context (item, ns);
   15645              :                     }
   15646              : 
   15647      1976271 :                   auto ovr = make_temp_override
   15648      1976271 :                     (ignore_exposure, item->is_ignored_exposure_context ());
   15649      1976271 :                   walker.decl_value (decl, current);
   15650      1976271 :                   if (current->has_defn ())
   15651       381518 :                     walker.write_definition (decl, current->refs_tu_local ());
   15652      1976271 :                 }
   15653      2011021 :               walker.end ();
   15654              : 
   15655              :               /* If we see either a class template or a deduction guide, make
   15656              :                  sure to add all visible deduction guides.  We need to check
   15657              :                  both in case they have been added in separate modules, or
   15658              :                  one is in the GMF and would have otherwise been discarded.  */
   15659      2011021 :               if (!is_key_order ()
   15660      2011021 :                   && DECL_CLASS_TEMPLATE_P (decl))
   15661        45478 :                 add_deduction_guides (decl);
   15662      2011021 :               if (!is_key_order ()
   15663      2011021 :                   && deduction_guide_p (decl))
   15664         2192 :                 add_deduction_guides (TYPE_NAME (TREE_TYPE (TREE_TYPE (decl))));
   15665              : 
   15666              :               /* Handle dependent ADL for [module.global.frag] p3.3.  */
   15667      2011021 :               if (!is_key_order () && !dep_adl_entity_list.is_empty ())
   15668              :                 {
   15669        62927 :                   processing_template_decl_sentinel ptds;
   15670        62927 :                   ++processing_template_decl;
   15671       234686 :                   for (auto &info : dep_adl_entity_list)
   15672              :                     {
   15673       171759 :                       tree lookup = lookup_arg_dependent (info.name, NULL_TREE,
   15674              :                                                           info.args, true);
   15675       396780 :                       for (tree fn : lkp_range (lookup))
   15676        53262 :                         add_dependency (make_dependency (fn, EK_DECL));
   15677              : 
   15678       171759 :                       if (info.rewrite)
   15679              :                         {
   15680        52562 :                           tree rewrite_name = ovl_op_identifier (info.rewrite);
   15681        52562 :                           lookup = lookup_arg_dependent (rewrite_name, NULL_TREE,
   15682              :                                                          info.args, true);
   15683       119657 :                           for (tree fn : lkp_range (lookup))
   15684        14533 :                             add_dependency (make_dependency (fn, EK_DECL));
   15685              :                         }
   15686       171759 :                       release_tree_vector (info.args);
   15687              :                     }
   15688        62927 :                   dep_adl_entity_list.truncate (0);
   15689        62927 :                 }
   15690              : 
   15691      2011021 :               if (!is_key_order ()
   15692      1023194 :                   && TREE_CODE (decl) == TEMPLATE_DECL
   15693      2365144 :                   && !DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (decl))
   15694              :                 {
   15695              :                   /* Mark all the explicit & partial specializations as
   15696              :                      reachable.  We search both specialization lists as some
   15697              :                      constrained partial specializations for class types are
   15698              :                      only found in DECL_TEMPLATE_SPECIALIZATIONS.  */
   15699      1005556 :                   auto mark_reached = [this](tree spec)
   15700              :                     {
   15701       658083 :                       if (TYPE_P (spec))
   15702       193208 :                         spec = TYPE_NAME (spec);
   15703       658083 :                       int use_tpl;
   15704       658083 :                       node_template_info (spec, use_tpl);
   15705       658083 :                       if (use_tpl & 2)
   15706              :                         {
   15707        71330 :                           depset *spec_dep = find_dependency (spec);
   15708        71330 :                           if (spec_dep->get_entity_kind () == EK_REDIRECT)
   15709        16015 :                             spec_dep = spec_dep->deps[0];
   15710        71330 :                           if (spec_dep->is_unreached ())
   15711              :                             {
   15712         9426 :                               reached_unreached = true;
   15713         9426 :                               spec_dep->clear_flag_bit<DB_UNREACHED_BIT> ();
   15714         9426 :                               dump (dumper::DEPEND)
   15715            0 :                                 && dump ("Reaching unreached specialization"
   15716            0 :                                          " %C:%N", TREE_CODE (spec), spec);
   15717              :                             }
   15718              :                         }
   15719      1005556 :                     };
   15720              : 
   15721       347473 :                   for (tree cons = DECL_TEMPLATE_INSTANTIATIONS (decl);
   15722       988893 :                        cons; cons = TREE_CHAIN (cons))
   15723       641420 :                     mark_reached (TREE_VALUE (cons));
   15724       347473 :                   for (tree cons = DECL_TEMPLATE_SPECIALIZATIONS (decl);
   15725       364136 :                        cons; cons = TREE_CHAIN (cons))
   15726        16663 :                     mark_reached (TREE_VALUE (cons));
   15727              :                 }
   15728              : 
   15729      2011021 :               dump.outdent ();
   15730      2011021 :               current = NULL;
   15731              :             }
   15732              :         }
   15733              : 
   15734       273475 :       if (!reached_unreached)
   15735              :         break;
   15736              : 
   15737              :       /* It's possible the we reached the unreached before we
   15738              :          processed it in the above loop, so we'll be doing this an
   15739              :          extra time.  However, to avoid that we have to do some
   15740              :          bit shuffling that also involves a scan of the list.
   15741              :          Swings & roundabouts I guess.  */
   15742         1080 :       std::swap (worklist, unreached);
   15743              :     }
   15744              : 
   15745       272395 :   unreached.release ();
   15746       272395 : }
   15747              : 
   15748              : /* Compare two entries of a single binding.  TYPE_DECL before
   15749              :    non-exported before exported.  */
   15750              : 
   15751              : static int
   15752       657514 : binding_cmp (const void *a_, const void *b_)
   15753              : {
   15754       657514 :   depset *a = *(depset *const *)a_;
   15755       657514 :   depset *b = *(depset *const *)b_;
   15756              : 
   15757       657514 :   tree a_ent = a->get_entity ();
   15758       657514 :   tree b_ent = b->get_entity ();
   15759       657514 :   gcc_checking_assert (a_ent != b_ent
   15760              :                        && !a->is_binding ()
   15761              :                        && !b->is_binding ());
   15762              : 
   15763              :   /* Implicit typedefs come first.  */
   15764       657514 :   bool a_implicit = DECL_IMPLICIT_TYPEDEF_P (a_ent);
   15765       657514 :   bool b_implicit = DECL_IMPLICIT_TYPEDEF_P (b_ent);
   15766       657388 :   if (a_implicit || b_implicit)
   15767              :     {
   15768              :       /* A binding with two implicit type decls?  That's unpossible!  */
   15769          252 :       gcc_checking_assert (!(a_implicit && b_implicit));
   15770          378 :       return a_implicit ? -1 : +1;  /* Implicit first.  */
   15771              :     }
   15772              : 
   15773              :   /* TU-local before non-TU-local.  */
   15774       657262 :   bool a_internal = a->get_entity_kind () == depset::EK_TU_LOCAL;
   15775       657262 :   bool b_internal = b->get_entity_kind () == depset::EK_TU_LOCAL;
   15776       657262 :   if (a_internal != b_internal)
   15777            0 :     return a_internal ? -1 : +1;  /* Internal first.  */
   15778              : 
   15779              :   /* Hidden before non-hidden.  */
   15780       657262 :   bool a_hidden = a->is_hidden ();
   15781       657262 :   bool b_hidden = b->is_hidden ();
   15782       657262 :   if (a_hidden != b_hidden)
   15783            0 :     return a_hidden ? -1 : +1;
   15784              : 
   15785       657262 :   bool a_using = a->get_entity_kind () == depset::EK_USING;
   15786       657262 :   bool a_export;
   15787       657262 :   if (a_using)
   15788              :     {
   15789       176528 :       a_export = OVL_EXPORT_P (a_ent);
   15790       176528 :       a_ent = OVL_FUNCTION (a_ent);
   15791              :     }
   15792       480734 :   else if (TREE_CODE (a_ent) == CONST_DECL
   15793            0 :            && DECL_LANG_SPECIFIC (a_ent)
   15794       480734 :            && DECL_MODULE_EXPORT_P (a_ent))
   15795              :     a_export = true;
   15796              :   else
   15797       480734 :     a_export = DECL_MODULE_EXPORT_P (TREE_CODE (a_ent) == CONST_DECL
   15798              :                                      ? TYPE_NAME (TREE_TYPE (a_ent))
   15799              :                                      : STRIP_TEMPLATE (a_ent));
   15800              : 
   15801       657262 :   bool b_using = b->get_entity_kind () == depset::EK_USING;
   15802       657262 :   bool b_export;
   15803       657262 :   if (b_using)
   15804              :     {
   15805       179762 :       b_export = OVL_EXPORT_P (b_ent);
   15806       179762 :       b_ent = OVL_FUNCTION (b_ent);
   15807              :     }
   15808       477500 :   else if (TREE_CODE (b_ent) == CONST_DECL
   15809            0 :            && DECL_LANG_SPECIFIC (b_ent)
   15810       477500 :            && DECL_MODULE_EXPORT_P (b_ent))
   15811              :     b_export = true;
   15812              :   else
   15813       477500 :     b_export = DECL_MODULE_EXPORT_P (TREE_CODE (b_ent) == CONST_DECL
   15814              :                                      ? TYPE_NAME (TREE_TYPE (b_ent))
   15815              :                                      : STRIP_TEMPLATE (b_ent));
   15816              : 
   15817              :   /* Non-exports before exports.  */
   15818       657262 :   if (a_export != b_export)
   15819        93997 :     return a_export ? +1 : -1;
   15820              : 
   15821              :   /* At this point we don't care, but want a stable sort.  */
   15822              : 
   15823       596992 :   if (a_using != b_using)
   15824              :     /* using first.  */
   15825        24569 :     return a_using? -1 : +1;
   15826              : 
   15827       579297 :   return DECL_UID (a_ent) < DECL_UID (b_ent) ? -1 : +1;
   15828              : }
   15829              : 
   15830              : /* True iff TMPL has an explicit instantiation definition.
   15831              : 
   15832              :    This is local to module.cc because register_specialization skips adding most
   15833              :    instantiations unless module_maybe_has_cmi_p.  */
   15834              : 
   15835              : static bool
   15836           76 : template_has_explicit_inst (tree tmpl)
   15837              : {
   15838           88 :   for (tree t = DECL_TEMPLATE_INSTANTIATIONS (tmpl); t; t = TREE_CHAIN (t))
   15839              :     {
   15840           24 :       tree spec = TREE_VALUE (t);
   15841           24 :       if (DECL_EXPLICIT_INSTANTIATION (spec)
   15842           24 :           && !DECL_REALLY_EXTERN (spec))
   15843              :         return true;
   15844              :     }
   15845              :   return false;
   15846              : }
   15847              : 
   15848              : /* Complain about DEP that exposes a TU-local entity.
   15849              : 
   15850              :    If STRICT, DEP only referenced entities from the GMF.  Returns TRUE
   15851              :    if we explained anything.  */
   15852              : 
   15853              : bool
   15854          127 : depset::hash::diagnose_bad_internal_ref (depset *dep, bool strict)
   15855              : {
   15856          127 :   tree decl = dep->get_entity ();
   15857              : 
   15858              :   /* Don't need to walk if we're not going to be emitting
   15859              :      any diagnostics anyway.  */
   15860          148 :   if (strict && !warning_enabled_at (DECL_SOURCE_LOCATION (decl),
   15861           21 :                                      OPT_Wexpose_global_module_tu_local))
   15862              :     return false;
   15863              : 
   15864          523 :   for (depset *rdep : dep->deps)
   15865          135 :     if (!rdep->is_binding () && rdep->is_tu_local (strict)
   15866          369 :         && !is_exposure_of_member_type (dep, rdep))
   15867              :       {
   15868              :         // FIXME:QOI Better location information?  We're
   15869              :         // losing, so it doesn't matter about efficiency.
   15870          118 :         tree exposed = rdep->get_entity ();
   15871          118 :         auto_diagnostic_group d;
   15872          118 :         if (strict)
   15873              :           {
   15874              :             /* Allow suppressing the warning from the point of declaration
   15875              :                of the otherwise-exposed decl, for cases we know that
   15876              :                exposures will never be 'bad'.  */
   15877           27 :             if (warning_enabled_at (DECL_SOURCE_LOCATION (exposed),
   15878           27 :                                     OPT_Wexpose_global_module_tu_local)
   15879           45 :                 && pedwarn (DECL_SOURCE_LOCATION (decl),
   15880           18 :                             OPT_Wexpose_global_module_tu_local,
   15881              :                             "%qD exposes TU-local entity %qD", decl, exposed))
   15882              :               {
   15883           18 :                 bool informed = is_tu_local_entity (exposed, /*explain=*/true);
   15884           18 :                 gcc_checking_assert (informed);
   15885              :                 return true;
   15886              :               }
   15887              :           }
   15888              :         else
   15889              :           {
   15890           91 :             error_at (DECL_SOURCE_LOCATION (decl),
   15891              :                       "%qD exposes TU-local entity %qD", decl, exposed);
   15892           91 :             bool informed = is_tu_local_entity (exposed, /*explain=*/true);
   15893           91 :             gcc_checking_assert (informed);
   15894           91 :             if (dep->is_tu_local (/*strict=*/true))
   15895            3 :               inform (DECL_SOURCE_LOCATION (decl),
   15896              :                       "%qD is also TU-local but has been exposed elsewhere",
   15897              :                       decl);
   15898           91 :             return true;
   15899              :           }
   15900          118 :       }
   15901              : 
   15902              :   return false;
   15903              : }
   15904              : 
   15905              : /* Warn about a template DEP that references a TU-local entity.
   15906              : 
   15907              :    If STRICT, DEP only referenced entities from the GMF.  Returns TRUE
   15908              :    if we explained anything.  */
   15909              : 
   15910              : bool
   15911           94 : depset::hash::diagnose_template_names_tu_local (depset *dep, bool strict)
   15912              : {
   15913           94 :   tree decl = dep->get_entity ();
   15914              : 
   15915              :   /* Don't bother walking if we know we won't be emitting anything.  */
   15916           94 :   if (!warning_enabled_at (DECL_SOURCE_LOCATION (decl),
   15917           94 :                            OPT_Wtemplate_names_tu_local)
   15918              :       /* Only warn strictly if users haven't silenced this warning here.  */
   15919          121 :       || (strict && !warning_enabled_at (DECL_SOURCE_LOCATION (decl),
   15920           27 :                                          OPT_Wexpose_global_module_tu_local)))
   15921            0 :     return false;
   15922              : 
   15923              :   /* Friend decls in a class body are ignored, but this is harmless:
   15924              :      it should not impact any consumers.  */
   15925           94 :   if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (decl)))
   15926              :     return false;
   15927              : 
   15928              :   /* We should now only be warning about templates.  */
   15929           76 :   gcc_checking_assert
   15930              :     (TREE_CODE (decl) == TEMPLATE_DECL
   15931              :      && VAR_OR_FUNCTION_DECL_P (DECL_TEMPLATE_RESULT (decl)));
   15932              : 
   15933              :   /* Don't warn if we've seen any explicit instantiation definitions,
   15934              :      the intent might be for importers to only use those.  */
   15935           76 :   if (template_has_explicit_inst (decl))
   15936              :     return false;
   15937              : 
   15938          268 :   for (depset *rdep : dep->deps)
   15939          134 :     if (!rdep->is_binding () && rdep->is_tu_local (strict))
   15940              :       {
   15941           67 :         tree ref = rdep->get_entity ();
   15942           67 :         auto_diagnostic_group d;
   15943           67 :         if (strict)
   15944              :           {
   15945           15 :             if (warning_enabled_at (DECL_SOURCE_LOCATION (ref),
   15946           15 :                                     OPT_Wexpose_global_module_tu_local)
   15947           21 :                 && warning_at (DECL_SOURCE_LOCATION (decl),
   15948            6 :                                OPT_Wtemplate_names_tu_local,
   15949              :                                "%qD refers to TU-local entity %qD, which may "
   15950              :                                "cause issues when instantiating in other TUs",
   15951              :                                decl, ref))
   15952              :               {
   15953            6 :                 is_tu_local_entity (ref, /*explain=*/true);
   15954            6 :                 return true;
   15955              :               }
   15956              :           }
   15957           52 :         else if (warning_at (DECL_SOURCE_LOCATION (decl),
   15958           52 :                              OPT_Wtemplate_names_tu_local,
   15959              :                              "%qD refers to TU-local entity %qD and cannot "
   15960              :                              "be instantiated in other TUs", decl, ref))
   15961              :           {
   15962           52 :             is_tu_local_entity (ref, /*explain=*/true);
   15963           52 :             return true;
   15964              :           }
   15965           67 :       }
   15966              : 
   15967              :   return false;
   15968              : }
   15969              : 
   15970              : /* Sort the bindings, issue errors about bad internal refs.  */
   15971              : 
   15972              : bool
   15973         2742 : depset::hash::finalize_dependencies ()
   15974              : {
   15975         2742 :   bool ok = true;
   15976      3293852 :   for (depset *dep : *this)
   15977              :     {
   15978      1645555 :       if (dep->is_binding ())
   15979              :         {
   15980              :           /* Keep the containing namespace dep first.  */
   15981       143296 :           gcc_checking_assert (dep->deps.length () > 1
   15982              :                                && (dep->deps[0]->get_entity_kind ()
   15983              :                                    == EK_NAMESPACE)
   15984              :                                && (dep->deps[0]->get_entity ()
   15985              :                                    == dep->get_entity ()));
   15986       143296 :           if (dep->deps.length () > 2)
   15987        11746 :             gcc_qsort (&dep->deps[1], dep->deps.length () - 1,
   15988              :                        sizeof (dep->deps[1]), binding_cmp);
   15989              : 
   15990              :           /* Bindings shouldn't refer to imported entities.  */
   15991       143296 :           if (CHECKING_P)
   15992       761366 :             for (depset *entity : dep->deps)
   15993       331478 :               gcc_checking_assert (!entity->is_import ());
   15994       143296 :           continue;
   15995       143296 :         }
   15996              : 
   15997              :       /* Otherwise, we'll check for bad internal refs.
   15998              :          Don't complain about any references from TU-local entities.  */
   15999      1502259 :       if (dep->is_tu_local ())
   16000          264 :         continue;
   16001              : 
   16002              :       /* We already complained about usings of non-external entities in
   16003              :          check_can_export_using_decl, don't do it again here.  */
   16004      1501995 :       if (dep->get_entity_kind () == EK_USING)
   16005        29785 :         continue;
   16006              : 
   16007      1472210 :       if (dep->is_exposure ())
   16008              :         {
   16009          106 :           bool explained = diagnose_bad_internal_ref (dep);
   16010              : 
   16011              :           /* A TU-local variable will always be considered an exposure,
   16012              :              so we don't have to worry about strict-only handling.  */
   16013          106 :           tree decl = dep->get_entity ();
   16014          106 :           if (!explained
   16015           15 :               && VAR_P (decl)
   16016          121 :               && (DECL_DECLARED_CONSTEXPR_P (decl)
   16017            6 :                   || DECL_INLINE_VAR_P (decl)))
   16018              :             {
   16019           15 :               auto_diagnostic_group d;
   16020           15 :               if (DECL_DECLARED_CONSTEXPR_P (decl))
   16021            9 :                 error_at (DECL_SOURCE_LOCATION (decl),
   16022              :                           "%qD is declared %<constexpr%> and is initialized to "
   16023              :                           "a TU-local value", decl);
   16024              :               else
   16025              :                 {
   16026              :                   /* This can only occur with references.  */
   16027            6 :                   gcc_checking_assert (TYPE_REF_P (TREE_TYPE (decl)));
   16028            6 :                   error_at (DECL_SOURCE_LOCATION (decl),
   16029              :                             "%qD is a reference declared %<inline%> and is "
   16030              :                             "constant-initialized to a TU-local value", decl);
   16031              :                 }
   16032           15 :               bool informed = is_tu_local_value (decl, DECL_INITIAL (decl),
   16033              :                                                  /*explain=*/true);
   16034           15 :               gcc_checking_assert (informed);
   16035           15 :               explained = true;
   16036           15 :             }
   16037              : 
   16038              :           /* We should have emitted an error above, unless the warning was
   16039              :              silenced.  */
   16040          106 :           gcc_checking_assert (explained);
   16041          106 :           ok = false;
   16042          106 :           continue;
   16043          106 :         }
   16044              : 
   16045              :       /* In all other cases, we're just warning (rather than erroring).
   16046              :          We don't want to do too much warning, so let's just bail after
   16047              :          the first warning we successfully emit.  */
   16048      1472122 :       if (warn_expose_global_module_tu_local
   16049      1472104 :           && !dep->is_tu_local (/*strict=*/true)
   16050      1472068 :           && dep->is_exposure (/*strict=*/true)
   16051      1472125 :           && diagnose_bad_internal_ref (dep, /*strict=*/true))
   16052           18 :         continue;
   16053              : 
   16054      1472138 :       if (warn_template_names_tu_local
   16055       271297 :           && dep->refs_tu_local ()
   16056      1472153 :           && diagnose_template_names_tu_local (dep))
   16057           52 :         continue;
   16058              : 
   16059      1472034 :       if (warn_template_names_tu_local
   16060       271245 :           && warn_expose_global_module_tu_local
   16061       271245 :           && !dep->is_tu_local (/*strict=*/true)
   16062       271221 :           && dep->refs_tu_local (/*strict=*/true)
   16063           30 :           && !dep->is_exposure (/*strict=*/true)
   16064      1472061 :           && diagnose_template_names_tu_local (dep, /*strict=*/true))
   16065              :         continue;
   16066              :     }
   16067              : 
   16068         2742 :   return ok;
   16069              : }
   16070              : 
   16071              : /* Core of TARJAN's algorithm to find Strongly Connected Components
   16072              :    within a graph.  See https://en.wikipedia.org/wiki/
   16073              :    Tarjan%27s_strongly_connected_components_algorithm for details.
   16074              : 
   16075              :    We use depset::section as lowlink.  Completed nodes have
   16076              :    depset::cluster containing the cluster number, with the top
   16077              :    bit set.
   16078              : 
   16079              :    A useful property is that the output vector is a reverse
   16080              :    topological sort of the resulting DAG.  In our case that means
   16081              :    dependent SCCs are found before their dependers.  We make use of
   16082              :    that property.  */
   16083              : 
   16084              : void
   16085      2153297 : depset::tarjan::connect (depset *v)
   16086              : {
   16087      2153297 :   gcc_checking_assert (v->is_binding ()
   16088              :                        || !(v->is_tu_local ()
   16089              :                             || v->is_unreached ()
   16090              :                             || v->is_import ()));
   16091              : 
   16092      2153297 :   v->cluster = v->section = ++index;
   16093      2153297 :   stack.safe_push (v);
   16094              : 
   16095              :   /* Walk all our dependencies, ignore a first marked slot  */
   16096     18476178 :   for (unsigned ix = v->is_special (); ix != v->deps.length (); ix++)
   16097              :     {
   16098      7089245 :       depset *dep = v->deps[ix];
   16099              : 
   16100      7089245 :       if (dep->is_binding ()
   16101     13990613 :           || !(dep->is_import () || dep->is_tu_local ()))
   16102              :         {
   16103      7077705 :           unsigned lwm = dep->cluster;
   16104              : 
   16105      7077705 :           if (!dep->cluster)
   16106              :             {
   16107              :               /* A new node.  Connect it.  */
   16108      1196220 :               connect (dep);
   16109      1196220 :               lwm = dep->section;
   16110              :             }
   16111              : 
   16112      7077705 :           if (dep->section && v->section > lwm)
   16113      1110218 :             v->section = lwm;
   16114              :         }
   16115              :     }
   16116              : 
   16117      2153297 :   if (v->section == v->cluster)
   16118              :     {
   16119              :       /* Root of a new SCC.  Push all the members onto the result list. */
   16120              :       unsigned num = v->cluster;
   16121      2153297 :       depset *p;
   16122      2153297 :       do
   16123              :         {
   16124      2153297 :           p = stack.pop ();
   16125      2153297 :           p->cluster = num;
   16126      2153297 :           p->section = 0;
   16127      2153297 :           result.quick_push (p);
   16128              :         }
   16129      2153297 :       while (p != v);
   16130              :     }
   16131      2153297 : }
   16132              : 
   16133              : /* Compare two depsets.  The specific ordering is unimportant, we're
   16134              :    just trying to get consistency.  */
   16135              : 
   16136              : static int
   16137    102004577 : depset_cmp (const void *a_, const void *b_)
   16138              : {
   16139    102004577 :   depset *a = *(depset *const *)a_;
   16140    102004577 :   depset *b = *(depset *const *)b_;
   16141              : 
   16142    102004577 :   depset::entity_kind a_kind = a->get_entity_kind ();
   16143    102004577 :   depset::entity_kind b_kind = b->get_entity_kind ();
   16144              : 
   16145    102004577 :   if  (a_kind != b_kind)
   16146              :     /* Different entity kinds, order by that.  */
   16147      5195011 :     return a_kind < b_kind ? -1 : +1;
   16148              : 
   16149     98335832 :   tree a_decl = a->get_entity ();
   16150     98335832 :   tree b_decl = b->get_entity ();
   16151     98335832 :   if (a_kind == depset::EK_USING)
   16152              :     {
   16153              :       /* If one is a using, the other must be too.  */
   16154      1684904 :       a_decl = OVL_FUNCTION (a_decl);
   16155      1684904 :       b_decl = OVL_FUNCTION (b_decl);
   16156              :     }
   16157              : 
   16158     98335832 :   if (a_decl != b_decl)
   16159              :     /* Different entities, order by their UID.  */
   16160     91293351 :     return DECL_UID (a_decl) < DECL_UID (b_decl) ? -1 : +1;
   16161              : 
   16162      7042481 :   if (a_kind == depset::EK_BINDING)
   16163              :     {
   16164              :       /* Both are bindings.  Order by identifier hash.  */
   16165      7039335 :       gcc_checking_assert (a->get_name () != b->get_name ());
   16166      7039335 :       hashval_t ah = IDENTIFIER_HASH_VALUE (a->get_name ());
   16167      7039335 :       hashval_t bh = IDENTIFIER_HASH_VALUE (b->get_name ());
   16168     10487084 :       return (ah == bh ? 0 : ah < bh ? -1 : +1);
   16169              :     }
   16170              : 
   16171              :   /* They are the same decl.  This can happen with two using decls
   16172              :      pointing to the same target.  The best we can aim for is
   16173              :      consistently telling qsort how to order them.  Hopefully we'll
   16174              :      never have to debug a case that depends on this.  Oh, who am I
   16175              :      kidding?  Good luck.  */
   16176         3146 :   gcc_checking_assert (a_kind == depset::EK_USING);
   16177              : 
   16178              :   /* Order by depset address.  Not the best, but it is something.  */
   16179         3146 :   return a < b ? -1 : +1;
   16180              : }
   16181              : 
   16182              : /* Sort the clusters in SCC such that those that depend on one another
   16183              :    are placed later.   */
   16184              : 
   16185              : // FIXME: I am not convinced this is needed and, if needed,
   16186              : // sufficient.  We emit the decls in this order but that emission
   16187              : // could walk into later decls (from the body of the decl, or default
   16188              : // arg-like things).  Why doesn't that walk do the right thing?  And
   16189              : // if it DTRT why do we need to sort here -- won't things naturally
   16190              : // work?  I think part of the issue is that when we're going to refer
   16191              : // to an entity by name, and that entity is in the same cluster as us,
   16192              : // we need to actually walk that entity, if we've not already walked
   16193              : // it.
   16194              : static void
   16195       269653 : sort_cluster (depset::hash *original, depset *scc[], unsigned size)
   16196              : {
   16197       269653 :   depset::hash table (size, original);
   16198              : 
   16199       269653 :   dump.indent ();
   16200              : 
   16201              :   /* Place bindings last, usings before that.  It's not strictly
   16202              :      necessary, but it does make things neater.  Says Mr OCD.  */
   16203              :   unsigned bind_lwm = size;
   16204              :   unsigned use_lwm = size;
   16205      1430301 :   for (unsigned ix = 0; ix != use_lwm;)
   16206              :     {
   16207      1160648 :       depset *dep = scc[ix];
   16208      1160648 :       switch (dep->get_entity_kind ())
   16209              :         {
   16210       143039 :         case depset::EK_BINDING:
   16211              :           /* Move to end.  No increment.  Notice this could be moving
   16212              :              a using decl, which we'll then move again.  */
   16213       143039 :           if (--bind_lwm != ix)
   16214              :             {
   16215        79189 :               scc[ix] = scc[bind_lwm];
   16216        79189 :               scc[bind_lwm] = dep;
   16217              :             }
   16218       143039 :           if (use_lwm > bind_lwm)
   16219              :             {
   16220       116971 :               use_lwm--;
   16221       116971 :               break;
   16222              :             }
   16223              :           /* We must have copied a using or TU-local, so move it too.  */
   16224        26068 :           dep = scc[ix];
   16225        26068 :           gcc_checking_assert
   16226              :             (dep->get_entity_kind () == depset::EK_USING
   16227              :              || dep->get_entity_kind () == depset::EK_TU_LOCAL);
   16228              :           /* FALLTHROUGH  */
   16229              : 
   16230        55850 :         case depset::EK_USING:
   16231        55850 :         case depset::EK_TU_LOCAL:
   16232        55850 :           if (--use_lwm != ix)
   16233              :             {
   16234        40377 :               scc[ix] = scc[use_lwm];
   16235        40377 :               scc[use_lwm] = dep;
   16236              :             }
   16237              :           break;
   16238              : 
   16239       987827 :         case depset::EK_DECL:
   16240       987827 :         case depset::EK_SPECIALIZATION:
   16241       987827 :         case depset::EK_PARTIAL:
   16242       987827 :           table.add_mergeable (dep);
   16243       987827 :           ix++;
   16244       987827 :           break;
   16245              : 
   16246            0 :         default:
   16247            0 :           gcc_unreachable ();
   16248              :         }
   16249              :     }
   16250              : 
   16251       269653 :   gcc_checking_assert (use_lwm <= bind_lwm);
   16252       269941 :   dump (dumper::MERGE) && dump ("Ordering %u/%u depsets", use_lwm, size);
   16253              : 
   16254       269653 :   table.find_dependencies (nullptr);
   16255              : 
   16256       269653 :   auto_vec<depset *> order = table.connect ();
   16257       539306 :   gcc_checking_assert (order.length () == use_lwm);
   16258              : 
   16259              :   /* Now rewrite entries [0,lwm), in the dependency order we
   16260              :      discovered.  Usually each entity is in its own cluster.  Rarely,
   16261              :      we can get multi-entity clusters, in which case all but one must
   16262              :      only be reached from within the cluster.  This happens for
   16263              :      something like:
   16264              : 
   16265              :      template<typename T>
   16266              :      auto Foo (const T &arg) -> TPL<decltype (arg)>;
   16267              : 
   16268              :      The instantiation of TPL will be in the specialization table, and
   16269              :      refer to Foo via arg.  But we can only get to that specialization
   16270              :      from Foo's declaration, so we only need to treat Foo as mergable
   16271              :      (We'll do structural comparison of TPL<decltype (arg)>).
   16272              : 
   16273              :      We approximate finding the single cluster entry dep by checking for
   16274              :      entities recursively depending on a dep first seen when streaming
   16275              :      its own merge key; the first dep we see in such a cluster should be
   16276              :      the first one streamed.  */
   16277              :   unsigned entry_pos = ~0u;
   16278              :   unsigned cluster = ~0u;
   16279      2514960 :   for (unsigned ix = 0; ix != order.length (); ix++)
   16280              :     {
   16281       987827 :       gcc_checking_assert (order[ix]->is_special ());
   16282       987827 :       bool tight = order[ix]->cluster == cluster;
   16283       987827 :       depset *dep = order[ix]->deps[0];
   16284       988820 :       dump (dumper::MERGE)
   16285         1983 :         && dump ("Mergeable %u is %N%s%s", ix, dep->get_entity (),
   16286          993 :                  tight ? " (tight)" : "", dep->is_entry () ? " (entry)" : "");
   16287       987827 :       scc[ix] = dep;
   16288       987827 :       if (tight)
   16289              :         {
   16290          115 :           gcc_checking_assert (dep->is_maybe_recursive ());
   16291          115 :           if (dep->is_entry ())
   16292              :             {
   16293              :               /* There should only be one entry dep in a cluster.  */
   16294            9 :               gcc_checking_assert (!scc[entry_pos]->is_entry ());
   16295            9 :               gcc_checking_assert (scc[entry_pos]->is_maybe_recursive ());
   16296            9 :               scc[ix] = scc[entry_pos];
   16297            9 :               scc[entry_pos] = dep;
   16298              :             }
   16299              :         }
   16300              :       else
   16301              :         entry_pos = ix;
   16302       987827 :       cluster = order[ix]->cluster;
   16303              :     }
   16304              : 
   16305       269941 :   dump (dumper::MERGE) && dump ("Ordered %u keys", order.length ());
   16306       269653 :   dump.outdent ();
   16307       269653 : }
   16308              : 
   16309              : /* Reduce graph to SCCS clusters.  SCCS will be populated with the
   16310              :    depsets in dependency order.  Each depset's CLUSTER field contains
   16311              :    its cluster number.  Each SCC has a unique cluster number, and are
   16312              :    contiguous in SCCS. Cluster numbers are otherwise arbitrary.  */
   16313              : 
   16314              : vec<depset *>
   16315       272366 : depset::hash::connect ()
   16316              : {
   16317       272366 :   tarjan connector (size ());
   16318       272366 :   vec<depset *> deps;
   16319       272366 :   deps.create (size ());
   16320      5537278 :   for (depset *item : *this)
   16321              :     {
   16322      2632456 :       entity_kind kind = item->get_entity_kind ();
   16323      2489417 :       if (kind == EK_BINDING
   16324      2489417 :           || !(kind == EK_REDIRECT
   16325      2467755 :                || item->is_tu_local ()
   16326      2467624 :                || item->is_unreached ()
   16327      2018868 :                || item->is_import ()))
   16328      2153297 :         deps.quick_push (item);
   16329              :     }
   16330              : 
   16331              :   /* Iteration over the hash table is an unspecified ordering.  While
   16332              :      that has advantages, it causes 2 problems.  Firstly repeatable
   16333              :      builds are tricky.  Secondly creating testcases that check
   16334              :      dependencies are correct by making sure a bad ordering would
   16335              :      happen if that was wrong.  */
   16336      1501809 :   deps.qsort (depset_cmp);
   16337              : 
   16338      2425663 :   while (deps.length ())
   16339              :     {
   16340      2153297 :       depset *v = deps.pop ();
   16341      2153297 :       dump (dumper::CLUSTER) &&
   16342         1800 :         (v->is_binding ()
   16343          210 :          ? dump ("Connecting binding %P", v->get_entity (), v->get_name ())
   16344         1590 :          : dump ("Connecting %s %s %C:%N",
   16345         1590 :                  is_key_order () ? "key-order"
   16346          870 :                  : !v->has_defn () ? "declaration" : "definition",
   16347         1590 :                  v->entity_kind_name (), TREE_CODE (v->get_entity ()),
   16348              :                  v->get_entity ()));
   16349      2153297 :       if (!v->cluster)
   16350       957077 :         connector.connect (v);
   16351              :     }
   16352              : 
   16353       272366 :   deps.release ();
   16354       544732 :   return connector.result;
   16355       272366 : }
   16356              : 
   16357              : /* Initialize location spans.  */
   16358              : 
   16359              : void
   16360         4854 : loc_spans::init (const line_maps *lmaps, const line_map_ordinary *map)
   16361              : {
   16362         4854 :   gcc_checking_assert (!init_p ());
   16363         4854 :   spans = new vec<span> ();
   16364         4854 :   spans->reserve (20);
   16365              : 
   16366         4854 :   span interval;
   16367         4854 :   interval.ordinary.first = 0;
   16368         4854 :   interval.macro.second = MAX_LOCATION_T + 1;
   16369         4854 :   interval.ordinary_delta = interval.macro_delta = 0;
   16370              : 
   16371              :   /* A span for reserved fixed locs.  */
   16372         4854 :   interval.ordinary.second
   16373         4854 :     = MAP_START_LOCATION (LINEMAPS_ORDINARY_MAP_AT (line_table, 0));
   16374         4854 :   interval.macro.first = interval.macro.second;
   16375         4854 :   dump (dumper::LOCATION)
   16376           42 :     && dump ("Fixed span %u ordinary:[%K,%K) macro:[%K,%K)", spans->length (),
   16377              :              interval.ordinary.first, interval.ordinary.second,
   16378              :              interval.macro.first, interval.macro.second);
   16379         4854 :   spans->quick_push (interval);
   16380              : 
   16381              :   /* A span for command line & forced headers.  */
   16382         4854 :   interval.ordinary.first = interval.ordinary.second;
   16383         4854 :   interval.macro.second = interval.macro.first;
   16384         4854 :   if (map)
   16385              :     {
   16386         4848 :       interval.ordinary.second = map->start_location;
   16387         4848 :       interval.macro.first = LINEMAPS_MACRO_LOWEST_LOCATION (lmaps);
   16388              :     }
   16389         4854 :   dump (dumper::LOCATION)
   16390           21 :     && dump ("Pre span %u ordinary:[%K,%K) macro:[%K,%K)", spans->length (),
   16391              :              interval.ordinary.first, interval.ordinary.second,
   16392              :              interval.macro.first, interval.macro.second);
   16393         4854 :   spans->quick_push (interval);
   16394              : 
   16395              :   /* Start an interval for the main file.  */
   16396         4854 :   interval.ordinary.first = interval.ordinary.second;
   16397         4854 :   interval.macro.second = interval.macro.first;
   16398         4854 :   dump (dumper::LOCATION)
   16399           21 :     && dump ("Main span %u ordinary:[%K,*) macro:[*,%K)", spans->length (),
   16400              :              interval.ordinary.first, interval.macro.second);
   16401         4854 :   spans->quick_push (interval);
   16402         4854 : }
   16403              : 
   16404              : /* Reopen the span, if we want the about-to-be-inserted set of maps to
   16405              :    be propagated in our own location table.  I.e. we are the primary
   16406              :    interface and we're importing a partition.  */
   16407              : 
   16408              : bool
   16409         3025 : loc_spans::maybe_propagate (module_state *import, location_t hwm)
   16410              : {
   16411         3025 :   bool opened = (module_interface_p () && !module_partition_p ()
   16412         3480 :                  && import->is_partition ());
   16413          169 :   if (opened)
   16414          169 :     open (hwm);
   16415         3025 :   return opened;
   16416              : }
   16417              : 
   16418              : /* Open a new linemap interval.  The just-created ordinary map is the
   16419              :    first map of the interval.  */
   16420              : 
   16421              : void
   16422         1060 : loc_spans::open (location_t hwm)
   16423              : {
   16424         1060 :   span interval;
   16425         1060 :   interval.ordinary.first = interval.ordinary.second = hwm;
   16426         2120 :   interval.macro.first = interval.macro.second
   16427         1060 :     = LINEMAPS_MACRO_LOWEST_LOCATION (line_table);
   16428         1060 :   interval.ordinary_delta = interval.macro_delta = 0;
   16429         1060 :   dump (dumper::LOCATION)
   16430            0 :     && dump ("Opening span %u ordinary:[%K,... macro:...,%K)",
   16431            0 :              spans->length (), interval.ordinary.first,
   16432              :              interval.macro.second);
   16433         1060 :   if (spans->length ())
   16434              :     {
   16435              :       /* No overlapping!  */
   16436         1060 :       auto &last = spans->last ();
   16437         1060 :       gcc_checking_assert (interval.ordinary.first >= last.ordinary.second);
   16438         1060 :       gcc_checking_assert (interval.macro.second <= last.macro.first);
   16439              :     }
   16440         1060 :   spans->safe_push (interval);
   16441         1060 : }
   16442              : 
   16443              : /* Close out the current linemap interval.  The last maps are within
   16444              :    the interval.  */
   16445              : 
   16446              : void
   16447         5911 : loc_spans::close ()
   16448              : {
   16449         5911 :   span &interval = spans->last ();
   16450              : 
   16451         5911 :   interval.ordinary.second
   16452         5911 :     = ((line_table->highest_location
   16453         5911 :         + (loc_one << line_table->default_range_bits))
   16454         5911 :        & ~((loc_one << line_table->default_range_bits) - 1));
   16455         5911 :   interval.macro.first = LINEMAPS_MACRO_LOWEST_LOCATION (line_table);
   16456         5911 :   dump (dumper::LOCATION)
   16457           21 :     && dump ("Closing span %u ordinary:[%K,%K) macro:[%K,%K)",
   16458           21 :              spans->length () - 1,
   16459              :              interval.ordinary.first,interval.ordinary.second,
   16460              :              interval.macro.first, interval.macro.second);
   16461         5911 : }
   16462              : 
   16463              : /* Given an ordinary location LOC, return the lmap_interval it resides
   16464              :    in.  NULL if it is not in an interval.  */
   16465              : 
   16466              : const loc_spans::span *
   16467     31510159 : loc_spans::ordinary (location_t loc)
   16468              : {
   16469     31510159 :   unsigned len = spans->length ();
   16470     62893409 :   unsigned pos = 0;
   16471     62903310 :   while (len)
   16472              :     {
   16473     62888468 :       unsigned half = len / 2;
   16474     62888468 :       const span &probe = (*spans)[pos + half];
   16475     62888468 :       if (loc < probe.ordinary.first)
   16476              :         len = half;
   16477     62878567 :       else if (loc < probe.ordinary.second)
   16478              :         return &probe;
   16479              :       else
   16480              :         {
   16481     31383250 :           pos += half + 1;
   16482     31383250 :           len = len - (half + 1);
   16483              :         }
   16484              :     }
   16485              :   return NULL;
   16486              : }
   16487              : 
   16488              : /* Likewise, given a macro location LOC, return the lmap interval it
   16489              :    resides in.   */
   16490              : 
   16491              : const loc_spans::span *
   16492      2591629 : loc_spans::macro (location_t loc)
   16493              : {
   16494      2591629 :   unsigned len = spans->length ();
   16495      5180160 :   unsigned pos = 0;
   16496      5180178 :   while (len)
   16497              :     {
   16498      5180148 :       unsigned half = len / 2;
   16499      5180148 :       const span &probe = (*spans)[pos + half];
   16500      5180148 :       if (loc >= probe.macro.second)
   16501              :         len = half;
   16502      5180130 :       else if (loc >= probe.macro.first)
   16503              :         return &probe;
   16504              :       else
   16505              :         {
   16506      2588531 :           pos += half + 1;
   16507      2588531 :           len = len - (half + 1);
   16508              :         }
   16509              :     }
   16510              :   return NULL;
   16511              : }
   16512              : 
   16513              : /* Return the ordinary location closest to FROM.  */
   16514              : 
   16515              : static location_t
   16516         6792 : ordinary_loc_of (line_maps *lmaps, location_t from)
   16517              : {
   16518        13587 :   while (!IS_ORDINARY_LOC (from))
   16519              :     {
   16520            3 :       if (IS_ADHOC_LOC (from))
   16521            3 :         from = get_location_from_adhoc_loc (lmaps, from);
   16522            3 :       if (from >= LINEMAPS_MACRO_LOWEST_LOCATION (lmaps))
   16523              :         {
   16524              :           /* Find the ordinary location nearest FROM.  */
   16525            0 :           const line_map *map = linemap_lookup (lmaps, from);
   16526            0 :           const line_map_macro *mac_map = linemap_check_macro (map);
   16527            0 :           from = mac_map->get_expansion_point_location ();
   16528              :         }
   16529              :     }
   16530         6792 :   return from;
   16531              : }
   16532              : 
   16533              : static module_state **
   16534        12176 : get_module_slot (tree name, module_state *parent, bool partition, bool insert)
   16535              : {
   16536        12176 :   module_state_hash::compare_type ct (name, uintptr_t (parent) | partition);
   16537        12176 :   hashval_t hv = module_state_hash::hash (ct);
   16538              : 
   16539        12176 :   return modules_hash->find_slot_with_hash (ct, hv, insert ? INSERT : NO_INSERT);
   16540              : }
   16541              : 
   16542              : static module_state *
   16543       121593 : get_primary (module_state *parent)
   16544              : {
   16545       125548 :   while (parent->is_partition ())
   16546          643 :     parent = parent->parent;
   16547              : 
   16548       124905 :   if (!parent->name)
   16549              :     // Implementation unit has null name
   16550        66619 :     parent = parent->parent;
   16551              : 
   16552       120442 :   return parent;
   16553              : }
   16554              : 
   16555              : /* Find or create module NAME & PARENT in the hash table.  */
   16556              : 
   16557              : module_state *
   16558        12176 : get_module (tree name, module_state *parent, bool partition)
   16559              : {
   16560              :   /* We might be given an empty NAME if preprocessing fails to handle
   16561              :      a header-name token.  */
   16562        12176 :   if (name && TREE_CODE (name) == STRING_CST
   16563        15020 :       && TREE_STRING_LENGTH (name) == 0)
   16564              :     return nullptr;
   16565              : 
   16566        12176 :   if (partition)
   16567              :     {
   16568         1045 :       if (!parent)
   16569          220 :         parent = get_primary (this_module ());
   16570              : 
   16571         1045 :       if (!parent->is_partition () && !parent->flatname)
   16572          247 :         parent->set_flatname ();
   16573              :     }
   16574              : 
   16575        12176 :   module_state **slot = get_module_slot (name, parent, partition, true);
   16576        12176 :   module_state *state = *slot;
   16577        12176 :   if (!state)
   16578              :     {
   16579         6598 :       state = (new (ggc_alloc<module_state> ())
   16580         6598 :                module_state (name, parent, partition));
   16581         6598 :       *slot = state;
   16582              :     }
   16583              :   return state;
   16584              : }
   16585              : 
   16586              : /* Process string name PTR into a module_state.  */
   16587              : 
   16588              : static module_state *
   16589          447 : get_module (const char *ptr)
   16590              : {
   16591              :   /* On DOS based file systems, there is an ambiguity with A:B which can be
   16592              :      interpreted as a module Module:Partition or Drive:PATH.  Interpret strings
   16593              :      which clearly starts as pathnames as header-names and everything else is
   16594              :      treated as a (possibly malformed) named moduled.  */
   16595          447 :   if (IS_DIR_SEPARATOR (ptr[ptr[0] == '.']) // ./FOO or /FOO
   16596              : #if HAVE_DOS_BASED_FILE_SYSTEM
   16597              :       || (HAS_DRIVE_SPEC (ptr) && IS_DIR_SEPARATOR (ptr[2])) // A:/FOO
   16598              : #endif
   16599              :       || false)
   16600              :     /* A header name.  */
   16601          111 :     return get_module (build_string (strlen (ptr), ptr));
   16602              : 
   16603              :   bool partition = false;
   16604              :   module_state *mod = NULL;
   16605              : 
   16606         1273 :   for (const char *probe = ptr;; probe++)
   16607         1609 :     if (!*probe || *probe == '.' || *probe == ':')
   16608              :       {
   16609          420 :         if (probe == ptr)
   16610              :           return NULL;
   16611              : 
   16612          420 :         mod = get_module (get_identifier_with_length (ptr, probe - ptr),
   16613              :                           mod, partition);
   16614          420 :         ptr = probe;
   16615          420 :         if (*ptr == ':')
   16616              :           {
   16617           81 :             if (partition)
   16618              :               return NULL;
   16619              :             partition = true;
   16620              :           }
   16621              : 
   16622          420 :         if (!*ptr++)
   16623              :           break;
   16624              :       }
   16625         1189 :     else if (!(ISALPHA (*probe) || *probe == '_'
   16626           18 :                || (probe != ptr && ISDIGIT (*probe))))
   16627              :       return NULL;
   16628              : 
   16629              :   return mod;
   16630              : }
   16631              : 
   16632              : /* Create a new mapper connecting to OPTION.  */
   16633              : 
   16634              : module_client *
   16635         4854 : make_mapper (location_t loc, class mkdeps *deps)
   16636              : {
   16637         4854 :   timevar_start (TV_MODULE_MAPPER);
   16638         4854 :   const char *option = module_mapper_name;
   16639         4854 :   if (!option)
   16640         4812 :     option = getenv ("CXX_MODULE_MAPPER");
   16641              : 
   16642         9708 :   mapper = module_client::open_module_client
   16643         4854 :     (loc, option, deps, &set_cmi_repo,
   16644         4854 :      (save_decoded_options[0].opt_index == OPT_SPECIAL_program_name)
   16645         4854 :      && save_decoded_options[0].arg != progname
   16646              :      ? save_decoded_options[0].arg : nullptr);
   16647              : 
   16648         4854 :   timevar_stop (TV_MODULE_MAPPER);
   16649              : 
   16650         4854 :   return mapper;
   16651              : }
   16652              : 
   16653              : static unsigned lazy_snum;
   16654              : 
   16655              : static bool
   16656        11904 : recursive_lazy (unsigned snum = ~0u)
   16657              : {
   16658        11904 :   if (lazy_snum)
   16659              :     {
   16660            0 :       error_at (input_location, "recursive lazy load");
   16661            0 :       return true;
   16662              :     }
   16663              : 
   16664        11904 :   lazy_snum = snum;
   16665        11904 :   return false;
   16666              : }
   16667              : 
   16668              : /* If THIS has an interface dependency on itself, report an error and
   16669              :    return false.  */
   16670              : 
   16671              : bool
   16672         2876 : module_state::check_circular_import (location_t from)
   16673              : {
   16674         2876 :   if (this == this_module ())
   16675              :     {
   16676              :       /* Cannot import the current module.  */
   16677            9 :       auto_diagnostic_group d;
   16678            9 :       error_at (from, "module %qs depends on itself", get_flatname ());
   16679            9 :       if (!header_module_p ())
   16680            6 :         inform (loc, "module %qs declared here", get_flatname ());
   16681            9 :       return false;
   16682            9 :     }
   16683              :   return true;
   16684              : }
   16685              : 
   16686              : /* Module name substitutions.  */
   16687              : static vec<module_state *,va_heap> substs;
   16688              : 
   16689              : void
   16690         8863 : module_state::mangle (bool include_partition)
   16691              : {
   16692         8863 :   if (subst)
   16693          386 :     mangle_module_substitution (subst);
   16694              :   else
   16695              :     {
   16696         8477 :       if (parent)
   16697          846 :         parent->mangle (include_partition);
   16698         8477 :       if (include_partition || !is_partition ())
   16699              :         {
   16700              :           // Partitions are significant for global initializer
   16701              :           // functions
   16702         8289 :           bool partition = is_partition () && !parent->is_partition ();
   16703         8289 :           subst = mangle_module_component (name, partition);
   16704         8289 :           substs.safe_push (this);
   16705              :         }
   16706              :     }
   16707         8863 : }
   16708              : 
   16709              : void
   16710         8017 : mangle_module (int mod, bool include_partition)
   16711              : {
   16712         8017 :   module_state *imp = (*modules)[mod];
   16713              : 
   16714         8017 :   gcc_checking_assert (!imp->is_header ());
   16715              : 
   16716         8017 :   if (!imp->name)
   16717              :     /* Set when importing the primary module interface.  */
   16718          223 :     imp = imp->parent;
   16719              : 
   16720              :   /* Ensure this is actually a module unit.  */
   16721          223 :   gcc_checking_assert (imp);
   16722              : 
   16723         8017 :   imp->mangle (include_partition);
   16724         8017 : }
   16725              : 
   16726              : /* Clean up substitutions.  */
   16727              : void
   16728         7585 : mangle_module_fini ()
   16729              : {
   16730        15874 :   while (substs.length ())
   16731         8289 :     substs.pop ()->subst = 0;
   16732         7585 : }
   16733              : 
   16734              : /* Announce WHAT about the module.  */
   16735              : 
   16736              : void
   16737        12507 : module_state::announce (const char *what) const
   16738              : {
   16739        12507 :   if (noisy_p ())
   16740              :     {
   16741            0 :       fprintf (stderr, " %s:%s", what, get_flatname ());
   16742            0 :       fflush (stderr);
   16743              :     }
   16744        12507 : }
   16745              : 
   16746              : /* A human-readable README section.  The contents of this section to
   16747              :    not contribute to the CRC, so the contents can change per
   16748              :    compilation.  That allows us to embed CWD, hostname, build time and
   16749              :    what not.  It is a STRTAB that may be extracted with:
   16750              :      readelf -pgnu.c++.README $(module).gcm */
   16751              : 
   16752              : void
   16753         2713 : module_state::write_readme (elf_out *to, cpp_reader *reader, const char *dialect)
   16754              : {
   16755         2713 :   bytes_out readme (to);
   16756              : 
   16757         2713 :   readme.begin (false);
   16758              : 
   16759         2713 :   readme.printf ("GNU C++ %s",
   16760         2713 :                  is_header () ? "header unit"
   16761         1824 :                  : !is_partition () ? "primary interface"
   16762          193 :                  : is_interface () ? "interface partition"
   16763              :                  : "internal partition");
   16764              : 
   16765              :   /* Compiler's version.  */
   16766         2713 :   readme.printf ("compiler: %s", version_string);
   16767              : 
   16768              :   /* Module format version.  */
   16769         2713 :   verstr_t string;
   16770         2713 :   version2string (MODULE_VERSION, string);
   16771         2713 :   readme.printf ("version: %s", string);
   16772              : 
   16773              :   /* Module information.  */
   16774         2713 :   readme.printf ("module: %s", get_flatname ());
   16775         2713 :   readme.printf ("source: %s", main_input_filename);
   16776         2713 :   readme.printf ("dialect: %s", dialect);
   16777         2713 :   if (extensions)
   16778           30 :     readme.printf ("extensions: %s%s%s",
   16779              :                    extensions & SE_OPENMP ? "-fopenmp"
   16780            6 :                    : extensions & SE_OPENMP_SIMD ? "-fopenmp-simd" : "",
   16781              :                    (extensions & SE_OPENACC)
   16782            3 :                    && (extensions & (SE_OPENMP | SE_OPENMP_SIMD))
   16783              :                    ? " " : "",
   16784           12 :                    extensions & SE_OPENACC ? "-fopenacc" : "");
   16785              : 
   16786              :   /* The following fields could be expected to change between
   16787              :      otherwise identical compilations.  Consider a distributed build
   16788              :      system.  We should have a way of overriding that.  */
   16789         2713 :   if (char *cwd = getcwd (NULL, 0))
   16790              :     {
   16791         2713 :       readme.printf ("cwd: %s", cwd);
   16792         2713 :       free (cwd);
   16793              :     }
   16794         5426 :   readme.printf ("repository: %s", cmi_repo ? cmi_repo : ".");
   16795              : #if NETWORKING
   16796              :   {
   16797              :     char hostname[64];
   16798              :     if (!gethostname (hostname, sizeof (hostname)))
   16799              :       readme.printf ("host: %s", hostname);
   16800              :   }
   16801              : #endif
   16802         2713 :   {
   16803              :     /* This of course will change!  */
   16804         2713 :     time_t stampy;
   16805         2713 :     auto kind = cpp_get_date (reader, &stampy);
   16806         2713 :     if (kind != CPP_time_kind::UNKNOWN)
   16807              :       {
   16808         2713 :         struct tm *time;
   16809              : 
   16810         2713 :         time = gmtime (&stampy);
   16811         2713 :         readme.print_time ("build", time, "UTC");
   16812              : 
   16813         2713 :         if (kind == CPP_time_kind::DYNAMIC)
   16814              :           {
   16815         2713 :             time = localtime (&stampy);
   16816         2713 :             readme.print_time ("local", time,
   16817              : #if defined (__USE_MISC) || defined (__USE_BSD) /* Is there a better way?  */
   16818              :                                time->tm_zone
   16819              : #else
   16820              :                                ""
   16821              : #endif
   16822              :                                );
   16823              :           }
   16824              :       }
   16825              :   }
   16826              : 
   16827              :   /* Its direct imports.  */
   16828         3374 :   for (unsigned ix = 1; ix < modules->length (); ix++)
   16829              :     {
   16830          661 :       module_state *state = (*modules)[ix];
   16831              : 
   16832          661 :       if (state->is_direct ())
   16833          970 :         readme.printf ("%s: %s %s", state->exported_p ? "export" : "import",
   16834              :                        state->get_flatname (), state->filename);
   16835              :     }
   16836              : 
   16837         2713 :   readme.end (to, to->name (MOD_SNAME_PFX ".README"), NULL);
   16838         2713 : }
   16839              : 
   16840              : /* Sort environment var names in reverse order.  */
   16841              : 
   16842              : static int
   16843            0 : env_var_cmp (const void *a_, const void *b_)
   16844              : {
   16845            0 :   const unsigned char *a = *(const unsigned char *const *)a_;
   16846            0 :   const unsigned char *b = *(const unsigned char *const *)b_;
   16847              : 
   16848            0 :   for (unsigned ix = 0; ; ix++)
   16849              :     {
   16850            0 :       bool a_end = !a[ix] || a[ix] == '=';
   16851            0 :       if (a[ix] == b[ix])
   16852              :         {
   16853            0 :           if (a_end)
   16854              :             break;
   16855              :         }
   16856              :       else
   16857              :         {
   16858            0 :           bool b_end = !b[ix] || b[ix] == '=';
   16859              : 
   16860            0 :           if (!a_end && !b_end)
   16861            0 :             return a[ix] < b[ix] ? +1 : -1;
   16862            0 :           if (a_end && b_end)
   16863              :             break;
   16864            0 :           return a_end ? +1 : -1;
   16865              :         }
   16866            0 :     }
   16867              : 
   16868              :   return 0;
   16869              : }
   16870              : 
   16871              : /* Write the environment. It is a STRTAB that may be extracted with:
   16872              :      readelf -pgnu.c++.ENV $(module).gcm */
   16873              : 
   16874              : void
   16875            0 : module_state::write_env (elf_out *to)
   16876              : {
   16877            0 :   vec<const char *> vars;
   16878            0 :   vars.create (20);
   16879              : 
   16880            0 :   extern char **environ;
   16881            0 :   while (const char *var = environ[vars.length ()])
   16882            0 :     vars.safe_push (var);
   16883            0 :   vars.qsort (env_var_cmp);
   16884              : 
   16885            0 :   bytes_out env (to);
   16886            0 :   env.begin (false);
   16887            0 :   while (vars.length ())
   16888            0 :     env.printf ("%s", vars.pop ());
   16889            0 :   env.end (to, to->name (MOD_SNAME_PFX ".ENV"), NULL);
   16890              : 
   16891            0 :   vars.release ();
   16892            0 : }
   16893              : 
   16894              : /* Write the direct or indirect imports.
   16895              :    u:N
   16896              :    {
   16897              :      u:index
   16898              :      s:name
   16899              :      u32:crc
   16900              :      s:filename (direct)
   16901              :      u:exported (direct)
   16902              :    } imports[N]
   16903              :  */
   16904              : 
   16905              : void
   16906          898 : module_state::write_imports (bytes_out &sec, bool direct)
   16907              : {
   16908          898 :   unsigned count = 0;
   16909              : 
   16910         1900 :   for (unsigned ix = 1; ix < modules->length (); ix++)
   16911              :     {
   16912         1002 :       module_state *imp = (*modules)[ix];
   16913              : 
   16914         1002 :       if (imp->remap && imp->is_direct () == direct)
   16915          480 :         count++;
   16916              :     }
   16917              : 
   16918          898 :   gcc_assert (!direct || count);
   16919              : 
   16920          898 :   sec.u (count);
   16921         1900 :   for (unsigned ix = 1; ix < modules->length (); ix++)
   16922              :     {
   16923         1002 :       module_state *imp = (*modules)[ix];
   16924              : 
   16925         1002 :       if (imp->remap && imp->is_direct () == direct)
   16926              :         {
   16927          642 :           dump () && dump ("Writing %simport:%u->%u %M (crc=%x)",
   16928              :                            !direct ? "indirect "
   16929           81 :                            : imp->exported_p ? "exported " : "",
   16930              :                            ix, imp->remap, imp, imp->crc);
   16931          480 :           sec.u (imp->remap);
   16932          480 :           sec.str (imp->get_flatname ());
   16933          480 :           sec.u32 (imp->crc);
   16934          480 :           if (direct)
   16935              :             {
   16936          471 :               write_location (sec, imp->imported_from ());
   16937          471 :               sec.str (imp->filename);
   16938          471 :               int exportedness = 0;
   16939          471 :               if (imp->exported_p)
   16940              :                 exportedness = +1;
   16941          277 :               else if (!imp->is_purview_direct ())
   16942           13 :                 exportedness = -1;
   16943          471 :               sec.i (exportedness);
   16944              :             }
   16945              :         }
   16946              :     }
   16947          898 : }
   16948              : 
   16949              : /* READER, LMAPS  != NULL == direct imports,
   16950              :    == NUL == indirect imports.  */
   16951              : 
   16952              : unsigned
   16953          766 : module_state::read_imports (bytes_in &sec, cpp_reader *reader, line_maps *lmaps)
   16954              : {
   16955          766 :   unsigned count = sec.u ();
   16956          766 :   unsigned loaded = 0;
   16957              : 
   16958         1937 :   while (count--)
   16959              :     {
   16960          405 :       unsigned ix = sec.u ();
   16961          405 :       if (ix >= slurp->remap->length () || !ix || (*slurp->remap)[ix])
   16962              :         {
   16963            0 :           sec.set_overrun ();
   16964            0 :           break;
   16965              :         }
   16966              : 
   16967          405 :       const char *name = sec.str (NULL);
   16968          405 :       module_state *imp = get_module (name);
   16969          405 :       unsigned crc = sec.u32 ();
   16970          405 :       int exportedness = 0;
   16971              : 
   16972              :       /* If the import is a partition, it must be the same primary
   16973              :          module as this TU.  */
   16974          405 :       if (imp && imp->is_partition () &&
   16975              :           (!named_module_p ()
   16976          135 :            || (get_primary (this_module ()) != get_primary (imp))))
   16977              :         imp = NULL;
   16978              : 
   16979          405 :       if (!imp)
   16980            0 :         sec.set_overrun ();
   16981          405 :       if (sec.get_overrun ())
   16982              :         break;
   16983              : 
   16984          405 :       if (lmaps)
   16985              :         {
   16986              :           /* A direct import, maybe load it.  */
   16987          401 :           location_t floc = read_location (sec);
   16988          401 :           const char *fname = sec.str (NULL);
   16989          401 :           exportedness = sec.i ();
   16990              : 
   16991          401 :           if (sec.get_overrun ())
   16992              :             break;
   16993              : 
   16994          401 :           if (!imp->check_circular_import (floc))
   16995            3 :             continue;
   16996              : 
   16997          398 :           if (imp->loadedness == ML_NONE)
   16998              :             {
   16999          314 :               imp->loc = floc;
   17000          314 :               imp->crc = crc;
   17001          314 :               if (!imp->get_flatname ())
   17002          271 :                 imp->set_flatname ();
   17003              : 
   17004          314 :               unsigned n = dump.push (imp);
   17005              : 
   17006          314 :               if (!imp->filename && fname)
   17007          271 :                 imp->filename = xstrdup (fname);
   17008              : 
   17009          314 :               if (imp->is_partition ())
   17010           33 :                 dump () && dump ("Importing elided partition %M", imp);
   17011              : 
   17012          314 :               if (!imp->do_import (reader, false))
   17013            3 :                 imp = NULL;
   17014          314 :               dump.pop (n);
   17015          314 :               if (!imp)
   17016            3 :                 continue;
   17017              :             }
   17018              : 
   17019          395 :           if (is_partition ())
   17020              :             {
   17021           66 :               if (!imp->is_direct () && !imp->is_partition_direct ())
   17022              :                 {
   17023           30 :                   imp->directness = MD_PARTITION_DIRECT;
   17024           30 :                   linemap_module_reparent (line_table, imp->loc, floc);
   17025              :                 }
   17026           66 :               if (exportedness > 0)
   17027            6 :                 imp->exported_p = true;
   17028              :             }
   17029              :         }
   17030              :       else
   17031              :         {
   17032              :           /* An indirect import, find it, it should already be here.  */
   17033            4 :           if (imp->loadedness == ML_NONE)
   17034              :             {
   17035            0 :               error_at (loc, "indirect import %qs is not already loaded", name);
   17036            0 :               continue;
   17037              :             }
   17038              :         }
   17039              : 
   17040          399 :       if (imp->crc != crc)
   17041            0 :         error_at (loc, "import %qs has CRC mismatch", imp->get_flatname ());
   17042              : 
   17043          399 :       (*slurp->remap)[ix] = (imp->mod << 1) | (lmaps != NULL);
   17044              : 
   17045          399 :       if (lmaps && exportedness >= 0)
   17046          381 :         set_import (imp, bool (exportedness));
   17047          579 :       dump () && dump ("Found %simport:%u %M->%u", !lmaps ? "indirect "
   17048           90 :                        : exportedness > 0 ? "exported "
   17049           51 :                        : exportedness < 0 ? "gmf" : "", ix, imp,
   17050              :                        imp->mod);
   17051          399 :       loaded++;
   17052              :     }
   17053              : 
   17054          766 :   return loaded;
   17055              : }
   17056              : 
   17057              : /* Write the import table to MOD_SNAME_PFX.imp.  */
   17058              : 
   17059              : void
   17060          449 : module_state::write_imports (elf_out *to, unsigned *crc_ptr)
   17061              : {
   17062          527 :   dump () && dump ("Writing imports");
   17063          449 :   dump.indent ();
   17064              : 
   17065          449 :   bytes_out sec (to);
   17066          449 :   sec.begin ();
   17067              : 
   17068          449 :   write_imports (sec, true);
   17069          449 :   write_imports (sec, false);
   17070              : 
   17071          449 :   sec.end (to, to->name (MOD_SNAME_PFX ".imp"), crc_ptr);
   17072          449 :   dump.outdent ();
   17073          449 : }
   17074              : 
   17075              : bool
   17076          383 : module_state::read_imports (cpp_reader *reader, line_maps *lmaps)
   17077              : {
   17078          383 :   bytes_in sec;
   17079              : 
   17080          383 :   if (!sec.begin (loc, from (), MOD_SNAME_PFX ".imp"))
   17081              :     return false;
   17082              : 
   17083          470 :   dump () && dump ("Reading %u imports", slurp->remap->length () - 1);
   17084          383 :   dump.indent ();
   17085              : 
   17086              :   /* Read the imports.  */
   17087          383 :   unsigned direct = read_imports (sec, reader, lmaps);
   17088          383 :   unsigned indirect = read_imports (sec, NULL, NULL);
   17089          383 :   if (direct + indirect + 1 != slurp->remap->length ())
   17090            6 :     from ()->set_error (elf::E_BAD_IMPORT);
   17091              : 
   17092          383 :   dump.outdent ();
   17093          383 :   if (!sec.end (from ()))
   17094              :     return false;
   17095              :   return true;
   17096          383 : }
   17097              : 
   17098              : /* We're the primary module interface, but have partitions.  Document
   17099              :    them so that non-partition module implementation units know which
   17100              :    have already been loaded.  */
   17101              : 
   17102              : void
   17103          130 : module_state::write_partitions (elf_out *to, unsigned count, unsigned *crc_ptr)
   17104              : {
   17105          154 :   dump () && dump ("Writing %u elided partitions", count);
   17106          130 :   dump.indent ();
   17107              : 
   17108          130 :   bytes_out sec (to);
   17109          130 :   sec.begin ();
   17110              : 
   17111          335 :   for (unsigned ix = 1; ix != modules->length (); ix++)
   17112              :     {
   17113          205 :       module_state *imp = (*modules)[ix];
   17114          205 :       if (imp->is_partition ())
   17115              :         {
   17116          220 :           dump () && dump ("Writing elided partition %M (crc=%x)",
   17117              :                            imp, imp->crc);
   17118          181 :           sec.str (imp->get_flatname ());
   17119          181 :           sec.u32 (imp->crc);
   17120          353 :           write_location (sec, imp->is_direct ()
   17121          172 :                           ? imp->imported_from () : UNKNOWN_LOCATION);
   17122          181 :           sec.str (imp->filename);
   17123              :         }
   17124              :     }
   17125              : 
   17126          130 :   sec.end (to, to->name (MOD_SNAME_PFX ".prt"), crc_ptr);
   17127          130 :   dump.outdent ();
   17128          130 : }
   17129              : 
   17130              : bool
   17131           24 : module_state::read_partitions (unsigned count)
   17132              : {
   17133           24 :   bytes_in sec;
   17134           24 :   if (!sec.begin (loc, from (), MOD_SNAME_PFX ".prt"))
   17135              :     return false;
   17136              : 
   17137           30 :   dump () && dump ("Reading %u elided partitions", count);
   17138           24 :   dump.indent ();
   17139              : 
   17140           60 :   while (count--)
   17141              :     {
   17142           36 :       const char *name = sec.str (NULL);
   17143           36 :       unsigned crc = sec.u32 ();
   17144           36 :       location_t floc = read_location (sec);
   17145           36 :       const char *fname = sec.str (NULL);
   17146              : 
   17147           36 :       if (sec.get_overrun ())
   17148              :         break;
   17149              : 
   17150           45 :       dump () && dump ("Reading elided partition %s (crc=%x)", name, crc);
   17151              : 
   17152           36 :       module_state *imp = get_module (name);
   17153           36 :       if (!imp  /* Partition should be ...  */
   17154           36 :           || !imp->is_partition () /* a partition ...  */
   17155           36 :           || imp->loadedness != ML_NONE  /* that is not yet loaded ...  */
   17156           72 :           || get_primary (imp) != this) /* whose primary is this.  */
   17157              :         {
   17158            0 :           sec.set_overrun ();
   17159            0 :           break;
   17160              :         }
   17161              : 
   17162           36 :       if (!imp->has_location ())
   17163           30 :         imp->loc = floc;
   17164           36 :       imp->crc = crc;
   17165           36 :       if (!imp->filename && fname[0])
   17166           30 :         imp->filename = xstrdup (fname);
   17167              :     }
   17168              : 
   17169           24 :   dump.outdent ();
   17170           24 :   if (!sec.end (from ()))
   17171              :     return false;
   17172              :   return true;
   17173           24 : }
   17174              : 
   17175              : /* Data for config reading and writing.  */
   17176              : struct module_state_config {
   17177              :   const char *dialect_str = get_dialect ();
   17178              :   line_map_uint_t ordinary_locs = 0;
   17179              :   line_map_uint_t macro_locs = 0;
   17180              :   unsigned num_imports = 0;
   17181              :   unsigned num_partitions = 0;
   17182              :   unsigned num_entities = 0;
   17183              :   unsigned loc_range_bits = 0;
   17184              :   unsigned active_init = 0;
   17185              : 
   17186        96703 :   static void release ()
   17187              :   {
   17188        96703 :     XDELETEVEC (dialect);
   17189        96703 :     dialect = NULL;
   17190              :   }
   17191              : 
   17192              : private:
   17193              :   static const char *get_dialect ();
   17194              :   static char *dialect;
   17195              : };
   17196              : 
   17197              : char *module_state_config::dialect;
   17198              : 
   17199              : /* Generate a string of the significant compilation options.
   17200              :    Generally assume the user knows what they're doing, in the same way
   17201              :    that object files can be mixed.  */
   17202              : 
   17203              : const char *
   17204         5871 : module_state_config::get_dialect ()
   17205              : {
   17206         5871 :   if (!dialect)
   17207         9286 :     dialect = concat (get_cxx_dialect_name (cxx_dialect),
   17208              :                       /* C++ implies these, only show if disabled.  */
   17209         4643 :                       flag_exceptions ? "" : "/no-exceptions",
   17210         4643 :                       flag_rtti ? "" : "/no-rtti",
   17211         4643 :                       flag_new_inheriting_ctors ? "" : "/old-inheriting-ctors",
   17212              :                       /* C++ 20 implies concepts and coroutines.  */
   17213         1460 :                       cxx_dialect < cxx20 && flag_concepts ? "/concepts" : "",
   17214         4643 :                       (cxx_dialect < cxx20 && flag_coroutines
   17215              :                        ? "/coroutines" : ""),
   17216         4643 :                       flag_module_implicit_inline ? "/implicit-inline" : "",
   17217         4643 :                       flag_contracts ? "/contracts" : "",
   17218              :                       NULL);
   17219              : 
   17220         5871 :   return dialect;
   17221              : }
   17222              : 
   17223              : /* Contents of a cluster.  */
   17224              : enum cluster_tag {
   17225              :   ct_decl,      /* A decl.  */
   17226              :   ct_defn,      /* A definition.  */
   17227              :   ct_bind,      /* A binding.  */
   17228              :   ct_hwm
   17229              : };
   17230              : 
   17231              : /* Binding modifiers.  */
   17232              : enum ct_bind_flags
   17233              : {
   17234              :   cbf_export = 0x1,     /* An exported decl.  */
   17235              :   cbf_hidden = 0x2,     /* A hidden (friend) decl.  */
   17236              :   cbf_using = 0x4,      /* A using decl.  */
   17237              :   cbf_internal = 0x8,   /* A TU-local decl.  */
   17238              : };
   17239              : 
   17240              : /* DEP belongs to a different cluster, seed it to prevent
   17241              :    unfortunately timed duplicate import.  */
   17242              : // FIXME: QOI For inter-cluster references we could just only pick
   17243              : // one entity from an earlier cluster.  Even better track
   17244              : // dependencies between earlier clusters
   17245              : 
   17246              : void
   17247      6906304 : module_state::intercluster_seed (trees_out &sec, unsigned index_hwm, depset *dep)
   17248              : {
   17249      6906304 :   if (dep->is_tu_local ())
   17250              :     /* We only stream placeholders for TU-local entities anyway.  */;
   17251      6906095 :   else if (dep->is_import () || dep->cluster < index_hwm)
   17252              :     {
   17253      3152599 :       tree ent = dep->get_entity ();
   17254      3152599 :       if (!TREE_VISITED (ent))
   17255              :         {
   17256      1328114 :           sec.tree_node (ent);
   17257      1328564 :           dump (dumper::CLUSTER)
   17258          450 :             && dump ("Seeded %s %N",
   17259          450 :                      dep->is_import () ? "import" : "intercluster", ent);
   17260              :         }
   17261              :     }
   17262      6906304 : }
   17263              : 
   17264              : /* Write the cluster of depsets in SCC[0-SIZE).
   17265              :    dep->section -> section number
   17266              :    dep->cluster -> entity number
   17267              :  */
   17268              : 
   17269              : unsigned
   17270       269653 : module_state::write_cluster (elf_out *to, depset *scc[], unsigned size,
   17271              :                              depset::hash &table, unsigned *counts,
   17272              :                              unsigned *crc_ptr)
   17273              : {
   17274       271187 :   dump () && dump ("Writing section:%u %u depsets", table.section, size);
   17275       269653 :   dump.indent ();
   17276              : 
   17277       269653 :   trees_out sec (to, this, table, table.section);
   17278       269653 :   sec.begin ();
   17279       269653 :   unsigned index_lwm = counts[MSC_entities];
   17280              : 
   17281              :   /* Determine entity numbers, mark for writing.   */
   17282       269962 :   dump (dumper::CLUSTER) && dump ("Cluster members:") && (dump.indent (), true);
   17283      1430301 :   for (unsigned ix = 0; ix != size; ix++)
   17284              :     {
   17285      1160648 :       depset *b = scc[ix];
   17286              : 
   17287      1160648 :       switch (b->get_entity_kind ())
   17288              :         {
   17289            0 :         default:
   17290            0 :           gcc_unreachable ();
   17291              : 
   17292       143039 :         case depset::EK_BINDING:
   17293       143039 :           {
   17294       143039 :             dump (dumper::CLUSTER)
   17295          210 :               && dump ("[%u]=%s %P", ix, b->entity_kind_name (),
   17296              :                        b->get_entity (), b->get_name ());
   17297       143039 :             depset *ns_dep = b->deps[0];
   17298       143039 :             gcc_checking_assert (ns_dep->get_entity_kind ()
   17299              :                                  == depset::EK_NAMESPACE
   17300              :                                  && ns_dep->get_entity () == b->get_entity ());
   17301       330964 :             for (unsigned jx = b->deps.length (); --jx;)
   17302              :               {
   17303       187925 :                 depset *dep = b->deps[jx];
   17304              :                 // We could be declaring something that is also a
   17305              :                 // (merged) import
   17306       217755 :                 gcc_checking_assert (dep->is_import ()
   17307              :                                      || TREE_VISITED (dep->get_entity ())
   17308              :                                      || (dep->get_entity_kind ()
   17309              :                                          == depset::EK_USING)
   17310              :                                      || (dep->get_entity_kind ()
   17311              :                                          == depset::EK_TU_LOCAL));
   17312              :               }
   17313              :           }
   17314              :           break;
   17315              : 
   17316       987827 :         case depset::EK_DECL:
   17317       987827 :         case depset::EK_SPECIALIZATION:
   17318       987827 :         case depset::EK_PARTIAL:
   17319       987827 :           b->cluster = counts[MSC_entities]++;
   17320       987827 :           sec.mark_declaration (b->get_entity (), b->has_defn ());
   17321              :           /* FALLTHROUGH  */
   17322              : 
   17323      1017609 :         case depset::EK_USING:
   17324      1017609 :         case depset::EK_TU_LOCAL:
   17325      2035218 :           gcc_checking_assert (!b->is_import ()
   17326              :                                && !b->is_unreached ());
   17327      1163822 :           dump (dumper::CLUSTER)
   17328         1161 :             && dump ("[%u]=%s %s %N", ix, b->entity_kind_name (),
   17329          729 :                      b->has_defn () ? "definition" : "declaration",
   17330              :                      b->get_entity ());
   17331              :           break;
   17332              :         }
   17333              :     }
   17334       269962 :   dump (dumper::CLUSTER) && (dump.outdent (), true);
   17335              : 
   17336              :   /* Ensure every out-of-cluster decl is referenced before we start
   17337              :      streaming.  We must do both imports *and* earlier clusters,
   17338              :      because the latter could reach into the former and cause a
   17339              :      duplicate loop.   */
   17340       269653 :   sec.set_importing (+1);
   17341      1430301 :   for (unsigned ix = 0; ix != size; ix++)
   17342              :     {
   17343      1160648 :       depset *b = scc[ix];
   17344     14312159 :       for (unsigned jx = b->is_special (); jx != b->deps.length (); jx++)
   17345              :         {
   17346      5998655 :           depset *dep = b->deps[jx];
   17347              : 
   17348      5998655 :           if (dep->is_binding ())
   17349              :             {
   17350      1283403 :               for (unsigned ix = dep->deps.length (); --ix;)
   17351              :                 {
   17352       907649 :                   depset *bind = dep->deps[ix];
   17353       907649 :                   if (bind->get_entity_kind () == depset::EK_USING)
   17354       260410 :                     bind = bind->deps[1];
   17355              : 
   17356       907649 :                   intercluster_seed (sec, index_lwm, bind);
   17357              :                 }
   17358              :               /* Also check the namespace itself.  */
   17359       187877 :               dep = dep->deps[0];
   17360              :             }
   17361              : 
   17362      5998655 :           intercluster_seed (sec, index_lwm, dep);
   17363              :         }
   17364              :     }
   17365       269653 :   sec.tree_node (NULL_TREE);
   17366              :   /* We're done importing now.  */
   17367       269653 :   sec.set_importing (-1);
   17368              : 
   17369              :   /* Write non-definitions.  */
   17370      1430301 :   for (unsigned ix = 0; ix != size; ix++)
   17371              :     {
   17372      1160648 :       depset *b = scc[ix];
   17373      1160648 :       tree decl = b->get_entity ();
   17374      1160648 :       switch (b->get_entity_kind ())
   17375              :         {
   17376            0 :         default:
   17377            0 :           gcc_unreachable ();
   17378       143039 :           break;
   17379              : 
   17380       143039 :         case depset::EK_BINDING:
   17381       143039 :           {
   17382       143039 :             gcc_assert (TREE_CODE (decl) == NAMESPACE_DECL);
   17383       144237 :             dump () && dump ("Depset:%u binding %C:%P", ix, TREE_CODE (decl),
   17384              :                              decl, b->get_name ());
   17385       143039 :             sec.u (ct_bind);
   17386       143039 :             sec.tree_node (decl);
   17387       143039 :             sec.tree_node (b->get_name ());
   17388              : 
   17389              :             /* Write in reverse order, so reading will see the exports
   17390              :                first, thus building the overload chain will be
   17391              :                optimized.  */
   17392       474003 :             for (unsigned jx = b->deps.length (); --jx;)
   17393              :               {
   17394       187925 :                 depset *dep = b->deps[jx];
   17395       187925 :                 tree bound = dep->get_entity ();
   17396       187925 :                 unsigned flags = 0;
   17397       187925 :                 if (dep->get_entity_kind () == depset::EK_TU_LOCAL)
   17398              :                   flags |= cbf_internal;
   17399       187877 :                 else if (dep->get_entity_kind () == depset::EK_USING)
   17400              :                   {
   17401        29782 :                     tree ovl = bound;
   17402        29782 :                     bound = OVL_FUNCTION (bound);
   17403        29782 :                     if (!(TREE_CODE (bound) == CONST_DECL
   17404         4647 :                           && UNSCOPED_ENUM_P (TREE_TYPE (bound))
   17405         3941 :                           && decl == TYPE_NAME (TREE_TYPE (bound))))
   17406              :                       /* An unscoped enumerator in its enumeration's
   17407              :                          scope is not a using.  */
   17408              :                       flags |= cbf_using;
   17409        29782 :                     if (OVL_EXPORT_P (ovl))
   17410        28806 :                       flags |= cbf_export;
   17411              :                   }
   17412              :                 else
   17413              :                   {
   17414              :                     /* An implicit typedef must be at one.  */
   17415       158095 :                     gcc_assert (!DECL_IMPLICIT_TYPEDEF_P (bound) || jx == 1);
   17416       158095 :                     if (dep->is_hidden ())
   17417              :                       flags |= cbf_hidden;
   17418       158007 :                     else if (DECL_MODULE_EXPORT_P (STRIP_TEMPLATE (bound)))
   17419       135208 :                       flags |= cbf_export;
   17420              :                   }
   17421              : 
   17422       187925 :                 gcc_checking_assert (DECL_P (bound));
   17423              : 
   17424       187925 :                 sec.i (flags);
   17425       187925 :                 if (flags & cbf_internal)
   17426              :                   {
   17427           48 :                     sec.tree_node (name_for_tu_local_decl (bound));
   17428           48 :                     write_location (sec, DECL_SOURCE_LOCATION (bound));
   17429              :                   }
   17430              :                 else
   17431       187877 :                   sec.tree_node (bound);
   17432              :               }
   17433              : 
   17434              :             /* Terminate the list.  */
   17435       143039 :             sec.i (-1);
   17436              :           }
   17437       143039 :           break;
   17438              : 
   17439        29782 :         case depset::EK_USING:
   17440        29782 :         case depset::EK_TU_LOCAL:
   17441        29809 :           dump () && dump ("Depset:%u %s %C:%N", ix, b->entity_kind_name (),
   17442           27 :                            TREE_CODE (decl), decl);
   17443              :           break;
   17444              : 
   17445       987827 :         case depset::EK_SPECIALIZATION:
   17446       987827 :         case depset::EK_PARTIAL:
   17447       987827 :         case depset::EK_DECL:
   17448       990974 :           dump () && dump ("Depset:%u %s entity:%u %C:%N", ix,
   17449              :                            b->entity_kind_name (), b->cluster,
   17450         3147 :                            TREE_CODE (decl), decl);
   17451              : 
   17452       987827 :           sec.u (ct_decl);
   17453       987827 :           sec.tree_node (decl);
   17454              : 
   17455      1163795 :           dump () && dump ("Wrote declaration entity:%u %C:%N",
   17456         3147 :                            b->cluster, TREE_CODE (decl), decl);
   17457              :           break;
   17458              :         }
   17459              :     }
   17460              : 
   17461              :   depset *namer = NULL;
   17462              : 
   17463              :   /* Write out definitions  */
   17464      1430301 :   for (unsigned ix = 0; ix != size; ix++)
   17465              :     {
   17466      1160648 :       depset *b = scc[ix];
   17467      1160648 :       tree decl = b->get_entity ();
   17468      1160648 :       switch (b->get_entity_kind ())
   17469              :         {
   17470              :         default:
   17471              :           break;
   17472              : 
   17473       987827 :         case depset::EK_SPECIALIZATION:
   17474       987827 :         case depset::EK_PARTIAL:
   17475       987827 :         case depset::EK_DECL:
   17476       987827 :           if (!namer)
   17477       255966 :             namer = b;
   17478              : 
   17479       987827 :           if (b->has_defn ())
   17480              :             {
   17481       381337 :               sec.u (ct_defn);
   17482       381337 :               sec.tree_node (decl);
   17483       382280 :               dump () && dump ("Writing definition %N", decl);
   17484       381337 :               sec.write_definition (decl, b->refs_tu_local ());
   17485              : 
   17486       381337 :               if (!namer->has_defn ())
   17487      1160648 :                 namer = b;
   17488              :             }
   17489              :           break;
   17490              :         }
   17491              :     }
   17492              : 
   17493              :   /* We don't find the section by name.  Use depset's decl's name for
   17494              :      human friendliness.  */
   17495       269653 :   unsigned name = 0;
   17496       269653 :   tree naming_decl = NULL_TREE;
   17497       269653 :   if (namer)
   17498              :     {
   17499       255966 :       naming_decl = namer->get_entity ();
   17500       255966 :       if (namer->get_entity_kind () == depset::EK_USING)
   17501              :         /* This unfortunately names the section from the target of the
   17502              :            using decl.  But the name is only a guide, so Do Not Care.  */
   17503            0 :         naming_decl = OVL_FUNCTION (naming_decl);
   17504       255966 :       if (DECL_IMPLICIT_TYPEDEF_P (naming_decl))
   17505              :         /* Lose any anonymousness.  */
   17506        88477 :         naming_decl = TYPE_NAME (TREE_TYPE (naming_decl));
   17507       255966 :       name = to->qualified_name (naming_decl, namer->has_defn ());
   17508              :     }
   17509              : 
   17510       269653 :   unsigned bytes = sec.pos;
   17511       269653 :   unsigned snum = sec.end (to, name, crc_ptr);
   17512              : 
   17513      1430301 :   for (unsigned ix = size; ix--;)
   17514      1160648 :     gcc_checking_assert (scc[ix]->section == snum);
   17515              : 
   17516       269653 :   dump.outdent ();
   17517       271187 :   dump () && dump ("Wrote section:%u named-by:%N", table.section, naming_decl);
   17518              : 
   17519       269653 :   return bytes;
   17520       269653 : }
   17521              : 
   17522              : /* Read a cluster from section SNUM.  */
   17523              : 
   17524              : bool
   17525       206410 : module_state::read_cluster (unsigned snum)
   17526              : {
   17527       206410 :   trees_in sec (this);
   17528              : 
   17529       206410 :   if (!sec.begin (loc, from (), snum))
   17530              :     return false;
   17531              : 
   17532       207661 :   dump () && dump ("Reading section:%u", snum);
   17533       206410 :   dump.indent ();
   17534              : 
   17535              :   /* We care about structural equality.  */
   17536       206410 :   comparing_dependent_aliases++;
   17537              : 
   17538              :   /* First seed the imports.  */
   17539      1276181 :   while (tree import = sec.tree_node ())
   17540      2346138 :     dump (dumper::CLUSTER) && dump ("Seeded import %N", import);
   17541              : 
   17542      1520346 :   while (!sec.get_overrun () && sec.more_p ())
   17543              :     {
   17544      1313936 :       unsigned ct = sec.u ();
   17545      1313936 :       switch (ct)
   17546              :         {
   17547            0 :         default:
   17548            0 :           sec.set_overrun ();
   17549            0 :           break;
   17550              : 
   17551       108316 :         case ct_bind:
   17552              :           /* A set of namespace bindings.  */
   17553       108316 :           {
   17554       108316 :             tree ns = sec.tree_node ();
   17555       108316 :             tree name = sec.tree_node ();
   17556       108316 :             tree decls = NULL_TREE;
   17557       108316 :             tree visible = NULL_TREE;
   17558       108316 :             tree internal = NULL_TREE;
   17559       108316 :             tree type = NULL_TREE;
   17560       108316 :             bool dedup = false;
   17561       108316 :             bool global_p = is_header ();
   17562              : 
   17563              :             /* We rely on the bindings being in the reverse order of
   17564              :                the resulting overload set.  */
   17565       254696 :             for (;;)
   17566              :               {
   17567       254696 :                 int flags = sec.i ();
   17568       254696 :                 if (flags < 0)
   17569              :                   break;
   17570              : 
   17571       146380 :                 if ((flags & cbf_hidden)
   17572           62 :                     && (flags & (cbf_using | cbf_export)))
   17573            0 :                   sec.set_overrun ();
   17574       146380 :                 if ((flags & cbf_internal)
   17575           15 :                     && flags != cbf_internal)
   17576            0 :                   sec.set_overrun ();
   17577              : 
   17578            0 :                 if (flags & cbf_internal)
   17579              :                   {
   17580           15 :                     tree name = sec.tree_node ();
   17581           15 :                     location_t loc = read_location (sec);
   17582           15 :                     if (sec.get_overrun ())
   17583              :                       break;
   17584              : 
   17585           15 :                     tree decl = make_node (TU_LOCAL_ENTITY);
   17586           15 :                     TU_LOCAL_ENTITY_NAME (decl) = name;
   17587           15 :                     TU_LOCAL_ENTITY_LOCATION (decl) = loc;
   17588           15 :                     internal = tree_cons (NULL_TREE, decl, internal);
   17589           15 :                     continue;
   17590           15 :                   }
   17591              : 
   17592       146365 :                 tree decl = sec.tree_node ();
   17593       146365 :                 if (sec.get_overrun ())
   17594              :                   break;
   17595              : 
   17596       146365 :                 if (!global_p)
   17597              :                   {
   17598              :                     /* Check if the decl could require GM merging.  */
   17599         8820 :                     tree orig = get_originating_module_decl (decl);
   17600         8820 :                     tree inner = STRIP_TEMPLATE (orig);
   17601         8820 :                     if (!DECL_LANG_SPECIFIC (inner)
   17602        17640 :                         || !DECL_MODULE_ATTACH_P (inner))
   17603              :                       global_p = true;
   17604              :                   }
   17605              : 
   17606       146365 :                 if (decls && TREE_CODE (decl) == TYPE_DECL)
   17607              :                   {
   17608              :                     /* Stat hack.  */
   17609           51 :                     if (type || !DECL_IMPLICIT_TYPEDEF_P (decl))
   17610            0 :                       sec.set_overrun ();
   17611              : 
   17612           51 :                     if (flags & cbf_using)
   17613              :                       {
   17614            3 :                         type = build_lang_decl_loc (UNKNOWN_LOCATION,
   17615              :                                                     USING_DECL,
   17616            3 :                                                     DECL_NAME (decl),
   17617              :                                                     NULL_TREE);
   17618            3 :                         USING_DECL_DECLS (type) = decl;
   17619            3 :                         USING_DECL_SCOPE (type) = CP_DECL_CONTEXT (decl);
   17620            3 :                         DECL_CONTEXT (type) = ns;
   17621              : 
   17622            3 :                         DECL_MODULE_PURVIEW_P (type) = true;
   17623            3 :                         if (flags & cbf_export)
   17624            3 :                           DECL_MODULE_EXPORT_P (type) = true;
   17625              :                       }
   17626              :                     else
   17627              :                       type = decl;
   17628              :                   }
   17629              :                 else
   17630              :                   {
   17631       146314 :                     if ((flags & cbf_using) &&
   17632        17224 :                         !DECL_DECLARES_FUNCTION_P (decl))
   17633              :                       {
   17634              :                         /* We should only see a single non-function using-decl
   17635              :                            for a binding; more than that would clash.  */
   17636         4792 :                         if (decls)
   17637            0 :                           sec.set_overrun ();
   17638              : 
   17639              :                         /* FIXME: Propagate the location of the using-decl
   17640              :                            for use in diagnostics.  */
   17641         4792 :                         decls = build_lang_decl_loc (UNKNOWN_LOCATION,
   17642              :                                                      USING_DECL,
   17643         4792 :                                                      DECL_NAME (decl),
   17644              :                                                      NULL_TREE);
   17645         4792 :                         USING_DECL_DECLS (decls) = decl;
   17646              :                         /* We don't currently record the actual scope of the
   17647              :                            using-declaration, but this approximation should
   17648              :                            generally be good enough.  */
   17649         4792 :                         USING_DECL_SCOPE (decls) = CP_DECL_CONTEXT (decl);
   17650         4792 :                         DECL_CONTEXT (decls) = ns;
   17651              : 
   17652         4792 :                         DECL_MODULE_PURVIEW_P (decls) = true;
   17653         4792 :                         if (flags & cbf_export)
   17654         4780 :                           DECL_MODULE_EXPORT_P (decls) = true;
   17655              :                       }
   17656       141522 :                     else if (decls
   17657       103509 :                              || (flags & (cbf_hidden | cbf_using))
   17658       238691 :                              || DECL_FUNCTION_TEMPLATE_P (decl))
   17659              :                       {
   17660        60046 :                         decls = ovl_make (decl, decls);
   17661        60046 :                         if (flags & cbf_using)
   17662              :                           {
   17663        12432 :                             dedup = true;
   17664        12432 :                             OVL_USING_P (decls) = true;
   17665        12432 :                             OVL_PURVIEW_P (decls) = true;
   17666        12432 :                             if (flags & cbf_export)
   17667        12417 :                               OVL_EXPORT_P (decls) = true;
   17668              :                           }
   17669              : 
   17670        60046 :                         if (flags & cbf_hidden)
   17671           62 :                           OVL_HIDDEN_P (decls) = true;
   17672        59984 :                         else if (dedup)
   17673        15523 :                           OVL_DEDUP_P (decls) = true;
   17674              :                       }
   17675              :                     else
   17676              :                       decls = decl;
   17677              : 
   17678       146302 :                     if (flags & cbf_export
   17679       146314 :                         || (!(flags & cbf_hidden)
   17680         8833 :                             && (is_module () || is_partition ())))
   17681              :                       visible = decls;
   17682              :                   }
   17683              :               }
   17684              : 
   17685       108316 :             if (!decls && !internal)
   17686            0 :               sec.set_overrun ();
   17687              : 
   17688       108316 :             if (sec.get_overrun ())
   17689              :               break; /* Bail.  */
   17690              : 
   17691       109189 :             dump () && dump ("Binding of %P", ns, name);
   17692       108316 :             if (!set_module_binding (ns, name, mod, global_p,
   17693       108316 :                                      is_module () || is_partition (),
   17694              :                                      decls, type, visible, internal))
   17695            0 :               sec.set_overrun ();
   17696              :           }
   17697              :           break;
   17698              : 
   17699       869662 :         case ct_decl:
   17700              :           /* A decl.  */
   17701       869662 :           {
   17702       869662 :             tree decl = sec.tree_node ();
   17703      2393700 :             dump () && dump ("Read declaration of %N", decl);
   17704              :           }
   17705              :           break;
   17706              : 
   17707       335958 :         case ct_defn:
   17708       335958 :           {
   17709       335958 :             tree decl = sec.tree_node ();
   17710       337261 :             dump () && dump ("Reading definition of %N", decl);
   17711       335958 :             sec.read_definition (decl);
   17712              :           }
   17713       335958 :           break;
   17714              :         }
   17715              :     }
   17716              : 
   17717              :   /* When lazy loading is in effect, we can be in the middle of
   17718              :      parsing or instantiating a function.  Save it away.
   17719              :      push_function_context does too much work.   */
   17720       206410 :   tree old_cfd = current_function_decl;
   17721       206410 :   struct function *old_cfun = cfun;
   17722       387794 :   for (const post_process_data& pdata : sec.post_process ())
   17723              :     {
   17724       138524 :       tree decl = pdata.decl;
   17725              : 
   17726       138524 :       bool abstract = false;
   17727       138524 :       if (TREE_CODE (decl) == TEMPLATE_DECL)
   17728              :         {
   17729        86817 :           abstract = true;
   17730        86817 :           decl = DECL_TEMPLATE_RESULT (decl);
   17731              :         }
   17732              : 
   17733       138524 :       current_function_decl = decl;
   17734       138524 :       allocate_struct_function (decl, abstract);
   17735       138524 :       cfun->language = ggc_cleared_alloc<language_function> ();
   17736       138524 :       cfun->language->base.x_stmt_tree.stmts_are_full_exprs_p = 1;
   17737       138524 :       cfun->function_start_locus = pdata.start_locus;
   17738       138524 :       cfun->function_end_locus = pdata.end_locus;
   17739       138524 :       cfun->language->returns_value = pdata.returns_value;
   17740       138524 :       cfun->language->returns_null = pdata.returns_null;
   17741       138524 :       cfun->language->returns_abnormally = pdata.returns_abnormally;
   17742       138524 :       cfun->language->infinite_loop = pdata.infinite_loop;
   17743       138524 :       cfun->coroutine_component = DECL_COROUTINE_P (decl);
   17744              : 
   17745              :       /* Make sure we emit explicit instantiations.
   17746              :          FIXME do we want to do this in expand_or_defer_fn instead?  */
   17747       138524 :       if (DECL_EXPLICIT_INSTANTIATION (decl)
   17748       138524 :           && !DECL_EXTERNAL (decl))
   17749           27 :         setup_explicit_instantiation_definition_linkage (decl);
   17750              : 
   17751       138524 :       if (abstract)
   17752              :         ;
   17753        51707 :       else if (DECL_MAYBE_IN_CHARGE_CDTOR_P (decl))
   17754         8865 :         vec_safe_push (post_load_decls, decl);
   17755              :       else
   17756              :         {
   17757        42842 :           bool aggr = aggregate_value_p (DECL_RESULT (decl), decl);
   17758              : #ifdef PCC_STATIC_STRUCT_RETURN
   17759              :           cfun->returns_pcc_struct = aggr;
   17760              : #endif
   17761        42842 :           cfun->returns_struct = aggr;
   17762        42842 :           expand_or_defer_fn (decl);
   17763              : 
   17764              :           /* If we first see this function after at_eof, it doesn't get
   17765              :              note_vague_linkage_fn from tentative_decl_linkage, so the loop in
   17766              :              c_parse_final_cleanups won't consider it.  But with DECL_COMDAT we
   17767              :              can just clear DECL_EXTERNAL and let cgraph decide.
   17768              :              FIXME handle this outside module.cc after GCC 15.  */
   17769         4244 :           if (at_eof && DECL_COMDAT (decl) && DECL_EXTERNAL (decl)
   17770        44500 :               && DECL_NOT_REALLY_EXTERN (decl))
   17771         1618 :             DECL_EXTERNAL (decl) = false;
   17772              :         }
   17773              : 
   17774              :     }
   17775       206486 :   for (const tree& type : sec.post_process_type ())
   17776              :     {
   17777              :       /* Attempt to complete an array type now in case its element type
   17778              :          had a definition streamed later in the cluster.  */
   17779           36 :       gcc_checking_assert (TREE_CODE (type) == ARRAY_TYPE);
   17780           36 :       complete_type (type);
   17781              :     }
   17782       206410 :   set_cfun (old_cfun);
   17783       206410 :   current_function_decl = old_cfd;
   17784       206410 :   comparing_dependent_aliases--;
   17785              : 
   17786       206410 :   dump.outdent ();
   17787       207661 :   dump () && dump ("Read section:%u", snum);
   17788              : 
   17789       206410 :   loaded_clusters++;
   17790              : 
   17791       206410 :   if (!sec.end (from ()))
   17792              :     return false;
   17793              : 
   17794              :   return true;
   17795       206410 : }
   17796              : 
   17797              : void
   17798       145708 : module_state::write_namespace (bytes_out &sec, depset *dep)
   17799              : {
   17800       145708 :   unsigned ns_num = dep->cluster;
   17801       145708 :   unsigned ns_import = 0;
   17802              : 
   17803       145708 :   if (dep->is_import ())
   17804            0 :     ns_import = dep->section;
   17805       145708 :   else if (dep->get_entity () != global_namespace)
   17806        92853 :     ns_num++;
   17807              : 
   17808       145708 :   sec.u (ns_import);
   17809       145708 :   sec.u (ns_num);
   17810       145708 : }
   17811              : 
   17812              : tree
   17813       193271 : module_state::read_namespace (bytes_in &sec)
   17814              : {
   17815       193271 :   unsigned ns_import = sec.u ();
   17816       193271 :   unsigned ns_num = sec.u ();
   17817       193271 :   tree ns = NULL_TREE;
   17818              : 
   17819       193271 :   if (ns_import || ns_num)
   17820              :     {
   17821       123030 :       if (!ns_import)
   17822       123030 :         ns_num--;
   17823              : 
   17824       123030 :       if (unsigned origin = slurp->remap_module (ns_import))
   17825              :         {
   17826       123030 :           module_state *from = (*modules)[origin];
   17827       123030 :           if (ns_num < from->entity_num)
   17828              :             {
   17829       123030 :               binding_slot &slot = (*entity_ary)[from->entity_lwm + ns_num];
   17830              : 
   17831       123030 :               if (!slot.is_lazy ())
   17832       123030 :                 ns = slot;
   17833              :             }
   17834              :         }
   17835              :       else
   17836            0 :         sec.set_overrun ();
   17837              :     }
   17838              :   else
   17839        70241 :     ns = global_namespace;
   17840              : 
   17841       193271 :   return ns;
   17842              : }
   17843              : 
   17844              : /* SPACES is a sorted vector of namespaces.  Write out the namespaces
   17845              :    to MOD_SNAME_PFX.nms section.   */
   17846              : 
   17847              : void
   17848          577 : module_state::write_namespaces (elf_out *to, vec<depset *> spaces,
   17849              :                                 unsigned num, unsigned *crc_p)
   17850              : {
   17851          635 :   dump () && dump ("Writing namespaces");
   17852          577 :   dump.indent ();
   17853              : 
   17854          577 :   bytes_out sec (to);
   17855          577 :   sec.begin ();
   17856              : 
   17857         2940 :   for (unsigned ix = 0; ix != num; ix++)
   17858              :     {
   17859         2363 :       depset *b = spaces[ix];
   17860         2363 :       tree ns = b->get_entity ();
   17861              : 
   17862              :       /* This could be an anonymous namespace even for a named module,
   17863              :          since we can still emit no-linkage decls.  */
   17864         2363 :       gcc_checking_assert (TREE_CODE (ns) == NAMESPACE_DECL);
   17865              : 
   17866         2363 :       unsigned flags = 0;
   17867         2363 :       if (TREE_PUBLIC (ns))
   17868         2305 :         flags |= 1;
   17869         2363 :       if (DECL_NAMESPACE_INLINE_P (ns))
   17870          441 :         flags |= 2;
   17871         2363 :       if (DECL_MODULE_PURVIEW_P (ns))
   17872         1960 :         flags |= 4;
   17873         2363 :       if (DECL_MODULE_EXPORT_P (ns))
   17874         1741 :         flags |= 8;
   17875         2363 :       if (TREE_DEPRECATED (ns))
   17876           14 :         flags |= 16;
   17877              : 
   17878         2503 :       dump () && dump ("Writing namespace:%u %N%s%s%s%s",
   17879              :                        b->cluster, ns,
   17880          140 :                        flags & 1 ? ", public" : "",
   17881          140 :                        flags & 2 ? ", inline" : "",
   17882          140 :                        flags & 4 ? ", purview" : "",
   17883          140 :                        flags & 8 ? ", export" : "",
   17884          140 :                        flags & 16 ? ", deprecated" : "");
   17885         2363 :       sec.u (b->cluster);
   17886         2363 :       sec.u (to->name (DECL_NAME (ns)));
   17887         2363 :       write_namespace (sec, b->deps[0]);
   17888              : 
   17889         2363 :       sec.u (flags);
   17890         2363 :       write_location (sec, DECL_SOURCE_LOCATION (ns));
   17891              : 
   17892         2363 :       if (DECL_NAMESPACE_INLINE_P (ns))
   17893              :         {
   17894          441 :           if (tree attr = lookup_attribute ("abi_tag", DECL_ATTRIBUTES (ns)))
   17895              :             {
   17896          119 :               tree tags = TREE_VALUE (attr);
   17897          119 :               sec.u (list_length (tags));
   17898          241 :               for (tree tag = tags; tag; tag = TREE_CHAIN (tag))
   17899          122 :                 sec.str (TREE_STRING_POINTER (TREE_VALUE (tag)));
   17900              :             }
   17901              :           else
   17902          322 :             sec.u (0);
   17903              :         }
   17904              :     }
   17905              : 
   17906          577 :   sec.end (to, to->name (MOD_SNAME_PFX ".nms"), crc_p);
   17907          577 :   dump.outdent ();
   17908          577 : }
   17909              : 
   17910              : /* Read the namespace hierarchy from MOD_SNAME_PFX.namespace.  Fill in
   17911              :    SPACES from that data.  */
   17912              : 
   17913              : bool
   17914          595 : module_state::read_namespaces (unsigned num)
   17915              : {
   17916          595 :   bytes_in sec;
   17917              : 
   17918          595 :   if (!sec.begin (loc, from (), MOD_SNAME_PFX ".nms"))
   17919              :     return false;
   17920              : 
   17921          665 :   dump () && dump ("Reading namespaces");
   17922          595 :   dump.indent ();
   17923              : 
   17924         3402 :   for (unsigned ix = 0; ix != num; ix++)
   17925              :     {
   17926         2807 :       unsigned entity_index = sec.u ();
   17927         2807 :       unsigned name = sec.u ();
   17928              : 
   17929         2807 :       tree parent = read_namespace (sec);
   17930              : 
   17931              :       /* See comment in write_namespace about why not bits.  */
   17932         2807 :       unsigned flags = sec.u ();
   17933         2807 :       location_t src_loc = read_location (sec);
   17934         2807 :       unsigned tags_count = (flags & 2) ? sec.u () : 0;
   17935              : 
   17936         2807 :       if (entity_index >= entity_num
   17937         2807 :           || !parent
   17938         2807 :           || (flags & 0xc) == 0x8)
   17939            0 :         sec.set_overrun ();
   17940              : 
   17941              :       tree tags = NULL_TREE;
   17942         2947 :       while (tags_count--)
   17943              :         {
   17944          140 :           size_t len;
   17945          140 :           const char *str = sec.str (&len);
   17946          140 :           tags = tree_cons (NULL_TREE, build_string (len + 1, str), tags);
   17947          140 :           tags = nreverse (tags);
   17948              :         }
   17949              : 
   17950         2807 :       if (sec.get_overrun ())
   17951              :         break;
   17952              : 
   17953         5556 :       tree id = name ? get_identifier (from ()->name (name)) : NULL_TREE;
   17954              : 
   17955         3001 :       dump () && dump ("Read namespace:%u %P%s%s%s%s",
   17956              :                        entity_index, parent, id,
   17957           97 :                        flags & 1 ? ", public" : "",
   17958              :                        flags & 2 ? ", inline" : "",
   17959           97 :                        flags & 4 ? ", purview" : "",
   17960           97 :                        flags & 8 ? ", export" : "",
   17961           97 :                        flags & 16 ? ", deprecated" : "");
   17962         2807 :       bool visible_p = ((flags & 8)
   17963         2807 :                         || ((flags & 1)
   17964          533 :                             && (flags & 4)
   17965          101 :                             && (is_partition () || is_module ())));
   17966         2807 :       tree inner = add_imported_namespace (parent, id, src_loc, mod,
   17967              :                                            bool (flags & 2), visible_p);
   17968         2807 :       if (!inner)
   17969              :         {
   17970            0 :           sec.set_overrun ();
   17971            0 :           break;
   17972              :         }
   17973              : 
   17974         2807 :       if (is_partition ())
   17975              :         {
   17976           48 :           if (flags & 4)
   17977           42 :             DECL_MODULE_PURVIEW_P (inner) = true;
   17978           48 :           if (flags & 8)
   17979           27 :             DECL_MODULE_EXPORT_P (inner) = true;
   17980              :         }
   17981              : 
   17982         2807 :       if (flags & 16)
   17983           19 :         TREE_DEPRECATED (inner) = true;
   17984              : 
   17985         2807 :       if (tags)
   17986          137 :         DECL_ATTRIBUTES (inner)
   17987          274 :           = tree_cons (get_identifier ("abi_tag"), tags, DECL_ATTRIBUTES (inner));
   17988              : 
   17989              :       /* Install the namespace.  */
   17990         2807 :       (*entity_ary)[entity_lwm + entity_index] = inner;
   17991         2807 :       if (DECL_MODULE_IMPORT_P (inner))
   17992              :         {
   17993            0 :           bool existed;
   17994            0 :           unsigned *slot = &entity_map->get_or_insert
   17995            0 :             (DECL_UID (inner), &existed);
   17996            0 :           if (existed)
   17997              :             /* If it existed, it should match.  */
   17998            0 :             gcc_checking_assert (inner == (*entity_ary)[*slot]);
   17999              :           else
   18000            0 :             *slot = entity_lwm + entity_index;
   18001              :         }
   18002              :     }
   18003              : 
   18004          595 :   dump.outdent ();
   18005          595 :   if (!sec.end (from ()))
   18006              :     return false;
   18007              :   return true;
   18008          595 : }
   18009              : 
   18010              : unsigned
   18011          577 : module_state::write_using_directives (elf_out *to, depset::hash &table,
   18012              :                                       vec<depset *> spaces, unsigned *crc_p)
   18013              : {
   18014          635 :   dump () && dump ("Writing using-directives");
   18015          577 :   dump.indent ();
   18016              : 
   18017          577 :   bytes_out sec (to);
   18018          577 :   sec.begin ();
   18019              : 
   18020          577 :   unsigned num = 0;
   18021         3517 :   auto emit_one_ns = [&](depset *parent_dep)
   18022              :     {
   18023         2940 :       tree parent = parent_dep->get_entity ();
   18024         3447 :       for (auto udir : NAMESPACE_LEVEL (parent)->using_directives)
   18025              :         {
   18026          173 :           if (TREE_CODE (udir) != USING_DECL || !DECL_MODULE_PURVIEW_P (udir))
   18027           14 :             continue;
   18028          159 :           bool exported = DECL_MODULE_EXPORT_P (udir);
   18029          159 :           tree target = USING_DECL_DECLS (udir);
   18030          159 :           depset *target_dep = table.find_dependency (target);
   18031              : 
   18032              :           /* An using-directive imported from a different module might not
   18033              :              have been walked earlier (PR c++/122915).  But importers will
   18034              :              be able to just refer to the decl in that module unless it was
   18035              :              a partition anyway, so we don't have anything to do here.  */
   18036          159 :           if (!target_dep)
   18037              :             {
   18038            6 :               gcc_checking_assert (DECL_MODULE_IMPORT_P (udir));
   18039            6 :               continue;
   18040              :             }
   18041              : 
   18042          177 :           dump () && dump ("Writing using-directive in %N for %N",
   18043              :                            parent, target);
   18044          153 :           sec.u (exported);
   18045          153 :           write_namespace (sec, parent_dep);
   18046          153 :           write_namespace (sec, target_dep);
   18047          153 :           ++num;
   18048              :         }
   18049         3517 :     };
   18050              : 
   18051          577 :   emit_one_ns (table.find_dependency (global_namespace));
   18052         4094 :   for (depset *parent_dep : spaces)
   18053         2363 :     emit_one_ns (parent_dep);
   18054              : 
   18055          577 :   sec.end (to, to->name (MOD_SNAME_PFX ".udi"), crc_p);
   18056          577 :   dump.outdent ();
   18057              : 
   18058          577 :   return num;
   18059          577 : }
   18060              : 
   18061              : bool
   18062          157 : module_state::read_using_directives (unsigned num)
   18063              : {
   18064          157 :   if (!bitmap_bit_p (this_module ()->imports, mod))
   18065              :     {
   18066           12 :       dump () && dump ("Ignoring using-directives because module %M "
   18067              :                        "is not visible in this TU", this);
   18068            9 :       return true;
   18069              :     }
   18070              : 
   18071          148 :   bytes_in sec;
   18072              : 
   18073          148 :   if (!sec.begin (loc, from (), MOD_SNAME_PFX ".udi"))
   18074              :     return false;
   18075              : 
   18076          160 :   dump () && dump ("Reading using-directives");
   18077          148 :   dump.indent ();
   18078              : 
   18079          324 :   for (unsigned ix = 0; ix != num; ++ix)
   18080              :     {
   18081          176 :       bool exported = sec.u ();
   18082          176 :       tree parent = read_namespace (sec);
   18083          176 :       tree target = read_namespace (sec);
   18084          176 :       if (sec.get_overrun ())
   18085              :         break;
   18086              : 
   18087          188 :       dump () && dump ("Read using-directive in %N for %N", parent, target);
   18088          176 :       if (exported || is_module () || is_partition ())
   18089          149 :         add_imported_using_namespace (parent, target);
   18090              :     }
   18091              : 
   18092          148 :   dump.outdent ();
   18093          148 :   if (!sec.end (from ()))
   18094              :     return false;
   18095              :   return true;
   18096          148 : }
   18097              : 
   18098              : /* Write the binding TABLE to MOD_SNAME_PFX.bnd   */
   18099              : 
   18100              : unsigned
   18101         2713 : module_state::write_bindings (elf_out *to, vec<depset *> sccs, unsigned *crc_p)
   18102              : {
   18103         3013 :   dump () && dump ("Writing binding table");
   18104         2713 :   dump.indent ();
   18105              : 
   18106         2713 :   unsigned num = 0;
   18107         2713 :   bytes_out sec (to);
   18108         2713 :   sec.begin ();
   18109              : 
   18110      2336366 :   for (unsigned ix = 0; ix != sccs.length (); ix++)
   18111              :     {
   18112      1165470 :       depset *b = sccs[ix];
   18113      1165470 :       if (b->is_binding ())
   18114              :         {
   18115       143039 :           tree ns = b->get_entity ();
   18116       144237 :           dump () && dump ("Bindings %P section:%u", ns, b->get_name (),
   18117              :                            b->section);
   18118       143039 :           sec.u (to->name (b->get_name ()));
   18119       143039 :           write_namespace (sec, b->deps[0]);
   18120       143039 :           sec.u (b->section);
   18121       143039 :           num++;
   18122              :         }
   18123              :     }
   18124              : 
   18125         2713 :   sec.end (to, to->name (MOD_SNAME_PFX ".bnd"), crc_p);
   18126         2713 :   dump.outdent ();
   18127              : 
   18128         2713 :   return num;
   18129         2713 : }
   18130              : 
   18131              : /* Read the binding table from MOD_SNAME_PFX.bind.  */
   18132              : 
   18133              : bool
   18134         2901 : module_state::read_bindings (unsigned num, unsigned lwm, unsigned hwm)
   18135              : {
   18136         2901 :   bytes_in sec;
   18137              : 
   18138         2901 :   if (!sec.begin (loc, from (), MOD_SNAME_PFX ".bnd"))
   18139              :     return false;
   18140              : 
   18141         3430 :   dump () && dump ("Reading binding table");
   18142         2901 :   dump.indent ();
   18143       193013 :   for (; !sec.get_overrun () && num--;)
   18144              :     {
   18145       190112 :       const char *name = from ()->name (sec.u ());
   18146       190112 :       tree ns = read_namespace (sec);
   18147       190112 :       unsigned snum = sec.u ();
   18148              : 
   18149       190112 :       if (!ns || !name || (snum - lwm) >= (hwm - lwm))
   18150            0 :         sec.set_overrun ();
   18151       190112 :       if (!sec.get_overrun ())
   18152              :         {
   18153       190112 :           tree id = get_identifier (name);
   18154       191623 :           dump () && dump ("Bindings %P section:%u", ns, id, snum);
   18155       190112 :           if (mod && !import_module_binding (ns, id, mod, snum))
   18156              :             break;
   18157              :         }
   18158              :     }
   18159              : 
   18160         2901 :   dump.outdent ();
   18161         2901 :   if (!sec.end (from ()))
   18162              :     return false;
   18163              :   return true;
   18164         2901 : }
   18165              : 
   18166              : /* Write the entity table to MOD_SNAME_PFX.ent
   18167              : 
   18168              :    Each entry is a section number.  */
   18169              : 
   18170              : void
   18171         2453 : module_state::write_entities (elf_out *to, vec<depset *> depsets,
   18172              :                               unsigned count, unsigned *crc_p)
   18173              : {
   18174         2700 :   dump () && dump ("Writing entities");
   18175         2453 :   dump.indent ();
   18176              : 
   18177         2453 :   bytes_out sec (to);
   18178         2453 :   sec.begin ();
   18179              : 
   18180         2453 :   unsigned current = 0;
   18181      1167902 :   for (unsigned ix = 0; ix < depsets.length (); ix++)
   18182              :     {
   18183      1165449 :       depset *d = depsets[ix];
   18184              : 
   18185      2158092 :       switch (d->get_entity_kind ())
   18186              :         {
   18187              :         default:
   18188              :           break;
   18189              : 
   18190         4816 :         case depset::EK_NAMESPACE:
   18191         4816 :           if (!d->is_import () && d->get_entity () != global_namespace)
   18192              :             {
   18193         2363 :               gcc_checking_assert (d->cluster == current);
   18194         2363 :               current++;
   18195         2363 :               sec.u (0);
   18196              :             }
   18197              :           break;
   18198              : 
   18199       987827 :         case depset::EK_DECL:
   18200       987827 :         case depset::EK_SPECIALIZATION:
   18201       987827 :         case depset::EK_PARTIAL:
   18202      1975654 :           gcc_checking_assert (!d->is_unreached ()
   18203              :                                && !d->is_import ()
   18204              :                                && d->cluster == current
   18205              :                                && d->section);
   18206       987827 :           current++;
   18207       987827 :           sec.u (d->section);
   18208       987827 :           break;
   18209              :         }
   18210              :     }
   18211         2453 :   gcc_assert (count == current);
   18212         2453 :   sec.end (to, to->name (MOD_SNAME_PFX ".ent"), crc_p);
   18213         2453 :   dump.outdent ();
   18214         2453 : }
   18215              : 
   18216              : bool
   18217         2675 : module_state::read_entities (unsigned count, unsigned lwm, unsigned hwm)
   18218              : {
   18219         2675 :   trees_in sec (this);
   18220              : 
   18221         2675 :   if (!sec.begin (loc, from (), MOD_SNAME_PFX ".ent"))
   18222              :     return false;
   18223              : 
   18224         3150 :   dump () && dump ("Reading entities");
   18225         2675 :   dump.indent ();
   18226              : 
   18227      1295883 :   for (binding_slot *slot = entity_ary->begin () + entity_lwm; count--; slot++)
   18228              :     {
   18229      1293208 :       unsigned snum = sec.u ();
   18230      1293208 :       if (snum && (snum - lwm) >= (hwm - lwm))
   18231            0 :         sec.set_overrun ();
   18232      1293208 :       if (sec.get_overrun ())
   18233              :         break;
   18234              : 
   18235      1293208 :       if (snum)
   18236      1290401 :         slot->set_lazy (snum << 2);
   18237              :     }
   18238              : 
   18239         2675 :   dump.outdent ();
   18240         2675 :   if (!sec.end (from ()))
   18241              :     return false;
   18242              :   return true;
   18243         2675 : }
   18244              : 
   18245              : /* Write the pending table to MOD_SNAME_PFX.pnd
   18246              : 
   18247              :    The pending table holds information about clusters that need to be
   18248              :    loaded because they contain information about something that is not
   18249              :    found by namespace-scope lookup.
   18250              : 
   18251              :    The three cases are:
   18252              : 
   18253              :    (a) Template (maybe-partial) specializations that we have
   18254              :    instantiated or defined.  When an importer needs to instantiate
   18255              :    that template, they /must have/ the partial, explicit & extern
   18256              :    specializations available.  If they have the other specializations
   18257              :    available, they'll have less work to do.  Thus, when we're about to
   18258              :    instantiate FOO, we have to be able to ask 'are there any
   18259              :    specialization of FOO in our imports?'.
   18260              : 
   18261              :    (b) (Maybe-implicit) member functions definitions.  A class could
   18262              :    be defined in one header, and an inline member defined in a
   18263              :    different header (this occurs in the STL).  Similarly, like the
   18264              :    specialization case, an implicit member function could have been
   18265              :    'instantiated' in one module, and it'd be nice to not have to
   18266              :    reinstantiate it in another.
   18267              : 
   18268              :    (c) Classes completed elsewhere.  A class could be declared in one
   18269              :    header and defined in another.  We need to know to load the class
   18270              :    definition before looking in it.  It does highlight an issue --
   18271              :    there could be an intermediate import between the outermost containing
   18272              :    namespace-scope class and the innermost being-defined class.  This is
   18273              :    actually possible with all of these cases, so be aware -- we're not
   18274              :    just talking of one level of import to get to the innermost namespace.
   18275              : 
   18276              :    This gets complicated fast, it took me multiple attempts to even
   18277              :    get something remotely working.  Partially because I focussed on
   18278              :    optimizing what I think turns out to be a smaller problem, given
   18279              :    the known need to do the more general case *anyway*.  I document
   18280              :    the smaller problem, because it does appear to be the natural way
   18281              :    to do it.  It's trap!
   18282              : 
   18283              :    **** THE TRAP
   18284              : 
   18285              :    Let's refer to the primary template or the containing class as the
   18286              :    KEY.  And the specialization or member as the PENDING-ENTITY.  (To
   18287              :    avoid having to say those mouthfuls all the time.)
   18288              : 
   18289              :    In either case, we have an entity and we need some way of mapping
   18290              :    that to a set of entities that need to be loaded before we can
   18291              :    proceed with whatever processing of the entity we were going to do.
   18292              : 
   18293              :    We need to link the key to the pending-entity in some way.  Given a
   18294              :    key, tell me the pending-entities I need to have loaded.  However
   18295              :    we tie the key to the pending-entity must not rely on the key being
   18296              :    loaded -- that'd defeat the lazy loading scheme.
   18297              : 
   18298              :    As the key will be an import in we know its entity number (either
   18299              :    because we imported it, or we're writing it out too).  Thus we can
   18300              :    generate a map of key-indices to pending-entities.  The
   18301              :    pending-entity indices will be into our span of the entity table,
   18302              :    and thus allow them to be lazily loaded.  The key index will be
   18303              :    into another slot of the entity table.  Notice that this checking
   18304              :    could be expensive, we don't want to iterate over a bunch of
   18305              :    pending-entity indices (across multiple imports), every time we're
   18306              :    about do to the thing with the key.  We need to quickly determine
   18307              :    'definitely nothing needed'.
   18308              : 
   18309              :    That's almost good enough, except that key indices are not unique
   18310              :    in a couple of cases :( Specifically the Global Module or a module
   18311              :    partition can result in multiple modules assigning an entity index
   18312              :    for the key.  The decl-merging on loading will detect that so we
   18313              :    only have one Key loaded, and in the entity hash it'll indicate the
   18314              :    entity index of first load.  Which might be different to how we
   18315              :    know it.  Notice this is restricted to GM entities or this-module
   18316              :    entities.  Foreign imports cannot have this.
   18317              : 
   18318              :    We can simply resolve this in the direction of how this module
   18319              :    referred to the key to how the importer knows it.  Look in the
   18320              :    entity table slot that we nominate, maybe lazy load it, and then
   18321              :    lookup the resultant entity in the entity hash to learn how the
   18322              :    importer knows it.
   18323              : 
   18324              :    But we need to go in the other direction :( Given the key, find all
   18325              :    the index-aliases of that key.  We can partially solve that by
   18326              :    adding an alias hash table.  Whenever we load a merged decl, add or
   18327              :    augment a mapping from the entity (or its entity-index) to the
   18328              :    newly-discovered index.  Then when we look for pending entities of
   18329              :    a key, we also iterate over this aliases this mapping provides.
   18330              : 
   18331              :    But that requires the alias to be loaded.  And that's not
   18332              :    necessarily true.
   18333              : 
   18334              :    *** THE SIMPLER WAY
   18335              : 
   18336              :    The remaining fixed thing we have is the innermost namespace
   18337              :    containing the ultimate namespace-scope container of the key and
   18338              :    the name of that container (which might be the key itself).  I.e. a
   18339              :    namespace-decl/identifier/module tuple.  Let's call this the
   18340              :    top-key.  We'll discover that the module is not important here,
   18341              :    because of cross-module possibilities mentioned in case #c above.
   18342              :    We can't markup namespace-binding slots.  The best we can do is
   18343              :    mark the binding vector with 'there's something here', and have
   18344              :    another map from namespace/identifier pairs to a vector of pending
   18345              :    entity indices.
   18346              : 
   18347              :    Maintain a pending-entity map.  This is keyed by top-key, and
   18348              :    maps to a vector of pending-entity indices.  On the binding vector
   18349              :    have flags saying whether the pending-name-entity map has contents.
   18350              :    (We might want to further extend the key to be GM-vs-Partition and
   18351              :    specialization-vs-member, but let's not get ahead of ourselves.)
   18352              : 
   18353              :    For every key-like entity, find the outermost namespace-scope
   18354              :    name.  Use that to lookup in the pending-entity map and then make
   18355              :    sure the specified entities are loaded.
   18356              : 
   18357              :    An optimization might be to have a flag in each key-entity saying
   18358              :    that its top key might be in the entity table.  It's not clear to
   18359              :    me how to set that flag cheaply -- cheaper than just looking.
   18360              : 
   18361              :    FIXME: It'd be nice to have a bit in decls to tell us whether to
   18362              :    even try this.  We can have a 'already done' flag, that we set when
   18363              :    we've done KLASS's lazy pendings.  When we import a module that
   18364              :    registers pendings on the same top-key as KLASS we need to clear
   18365              :    the flag.  A recursive walk of the top-key clearing the bit will
   18366              :    suffice.  Plus we only need to recurse on classes that have the bit
   18367              :    set.  (That means we need to set the bit on parents of KLASS here,
   18368              :    don't forget.)  However, first: correctness, second: efficiency.  */
   18369              : 
   18370              : unsigned
   18371         2713 : module_state::write_pendings (elf_out *to, vec<depset *> depsets,
   18372              :                               depset::hash &table, unsigned *crc_p)
   18373              : {
   18374         3013 :   dump () && dump ("Writing pending-entities");
   18375         2713 :   dump.indent ();
   18376              : 
   18377         2713 :   trees_out sec (to, this, table);
   18378         2713 :   sec.begin ();
   18379              : 
   18380         2713 :   unsigned count = 0;
   18381         2713 :   tree cache_ns = NULL_TREE;
   18382         2713 :   tree cache_id = NULL_TREE;
   18383         2713 :   unsigned cache_section = ~0;
   18384      1168183 :   for (unsigned ix = 0; ix < depsets.length (); ix++)
   18385              :     {
   18386      1165470 :       depset *d = depsets[ix];
   18387              : 
   18388      1165470 :       if (d->is_binding ())
   18389       715318 :         continue;
   18390              : 
   18391      1022431 :       if (d->is_import ())
   18392            0 :         continue;
   18393              : 
   18394      1022431 :       if (!d->is_pending_entity ())
   18395       572279 :         continue;
   18396              : 
   18397       450152 :       tree key_decl = nullptr;
   18398       450152 :       tree key_ns = find_pending_key (d->get_entity (), &key_decl);
   18399       450152 :       tree key_name = DECL_NAME (key_decl);
   18400              : 
   18401       450152 :       if (IDENTIFIER_ANON_P (key_name))
   18402              :         {
   18403            6 :           gcc_checking_assert (IDENTIFIER_LAMBDA_P (key_name));
   18404           12 :           if (tree attached = LAMBDA_TYPE_EXTRA_SCOPE (TREE_TYPE (key_decl)))
   18405            6 :             key_name = DECL_NAME (attached);
   18406              :           else
   18407              :             {
   18408              :               /* There's nothing to attach it to.  Must
   18409              :                  always reinstantiate.  */
   18410            0 :               dump ()
   18411            0 :                 && dump ("Unattached lambda %N[%u] section:%u",
   18412            0 :                          d->get_entity_kind () == depset::EK_DECL
   18413              :                          ? "Member" : "Specialization", d->get_entity (),
   18414              :                          d->cluster, d->section);
   18415            0 :               continue;
   18416              :             }
   18417              :         }
   18418              : 
   18419       450152 :       char const *also = "";
   18420       450152 :       if (d->section == cache_section
   18421       294221 :           && key_ns == cache_ns
   18422       294221 :           && key_name == cache_id)
   18423              :         /* Same section & key as previous, no need to repeat ourselves.  */
   18424              :         also = "also ";
   18425              :       else
   18426              :         {
   18427       207051 :           cache_ns = key_ns;
   18428       207051 :           cache_id = key_name;
   18429       207051 :           cache_section = d->section;
   18430       207051 :           gcc_checking_assert (table.find_dependency (cache_ns));
   18431       207051 :           sec.tree_node (cache_ns);
   18432       207051 :           sec.tree_node (cache_id);
   18433       207051 :           sec.u (d->cluster);
   18434       207051 :           count++;
   18435              :         }
   18436       451608 :       dump () && dump ("Pending %s %N entity:%u section:%u %skeyed to %P",
   18437          728 :                        d->get_entity_kind () == depset::EK_DECL
   18438              :                        ? "member" : "specialization", d->get_entity (),
   18439              :                        d->cluster, cache_section, also, cache_ns, cache_id);
   18440              :       }
   18441         2713 :   sec.end (to, to->name (MOD_SNAME_PFX ".pnd"), crc_p);
   18442         2713 :   dump.outdent ();
   18443              : 
   18444         2713 :   return count;
   18445         2713 : }
   18446              : 
   18447              : bool
   18448         1538 : module_state::read_pendings (unsigned count)
   18449              : {
   18450         1538 :   trees_in sec (this);
   18451              : 
   18452         1538 :   if (!sec.begin (loc, from (), MOD_SNAME_PFX ".pnd"))
   18453              :     return false;
   18454              : 
   18455         1857 :   dump () && dump ("Reading %u pendings", count);
   18456         1538 :   dump.indent ();
   18457              : 
   18458       268441 :   for (unsigned ix = 0; ix != count; ix++)
   18459              :     {
   18460       266903 :       pending_key key;
   18461       266903 :       unsigned index;
   18462              : 
   18463       266903 :       key.ns = sec.tree_node ();
   18464       266903 :       key.id = sec.tree_node ();
   18465       266903 :       index = sec.u ();
   18466              : 
   18467       266903 :       if (!key.ns || !key.id
   18468       266903 :           || !(TREE_CODE (key.ns) == NAMESPACE_DECL
   18469       266903 :                && !DECL_NAMESPACE_ALIAS (key.ns))
   18470       266903 :           || !identifier_p (key.id)
   18471       533806 :           || index >= entity_num)
   18472            0 :         sec.set_overrun ();
   18473              : 
   18474       266903 :       if (sec.get_overrun ())
   18475              :         break;
   18476              : 
   18477       267684 :       dump () && dump ("Pending:%u keyed to %P", index, key.ns, key.id);
   18478              : 
   18479       266903 :       index += entity_lwm;
   18480       266903 :       auto &vec = pending_table->get_or_insert (key);
   18481       266903 :       vec.safe_push (index);
   18482              :     }
   18483              : 
   18484         1538 :   dump.outdent ();
   18485         1538 :   if (!sec.end (from ()))
   18486              :     return false;
   18487              :   return true;
   18488         1538 : }
   18489              : 
   18490              : /* Read & write locations.  */
   18491              : enum loc_kind {
   18492              :   LK_ORDINARY,
   18493              :   LK_MACRO,
   18494              :   LK_IMPORT_ORDINARY,
   18495              :   LK_IMPORT_MACRO,
   18496              :   LK_ADHOC,
   18497              :   LK_RESERVED,
   18498              : };
   18499              : 
   18500              : static const module_state *
   18501         7417 : module_for_ordinary_loc (location_t loc)
   18502              : {
   18503         7417 :   unsigned pos = 0;
   18504        14834 :   unsigned len = ool->length () - pos;
   18505              : 
   18506         7420 :   while (len)
   18507              :     {
   18508         7420 :       unsigned half = len / 2;
   18509         7420 :       module_state *probe = (*ool)[pos + half];
   18510         7420 :       if (loc < probe->ordinary_locs.first)
   18511              :         len = half;
   18512         7417 :       else if (loc < probe->ordinary_locs.first + probe->ordinary_locs.second)
   18513              :         return probe;
   18514              :       else
   18515              :         {
   18516            0 :           pos += half + 1;
   18517            0 :           len = len - (half + 1);
   18518              :         }
   18519              :     }
   18520              : 
   18521              :   return nullptr;
   18522              : }
   18523              : 
   18524              : static const module_state *
   18525           15 : module_for_macro_loc (location_t loc)
   18526              : {
   18527           15 :   unsigned pos = 1;
   18528           15 :   unsigned len = modules->length () - pos;
   18529              : 
   18530           15 :   while (len)
   18531              :     {
   18532           15 :       unsigned half = len / 2;
   18533           15 :       module_state *probe = (*modules)[pos + half];
   18534           15 :       if (loc < probe->macro_locs.first)
   18535              :         {
   18536            0 :           pos += half + 1;
   18537            0 :           len = len - (half + 1);
   18538              :         }
   18539           15 :       else if (loc >= probe->macro_locs.first + probe->macro_locs.second)
   18540              :         len = half;
   18541              :       else
   18542              :         return probe;
   18543              :     }
   18544              : 
   18545              :   return NULL;
   18546              : }
   18547              : 
   18548              : location_t
   18549         1293 : module_state::imported_from () const
   18550              : {
   18551         1293 :   location_t from = loc;
   18552         1293 :   line_map_ordinary const *fmap
   18553         1293 :     = linemap_check_ordinary (linemap_lookup (line_table, from));
   18554              : 
   18555         1293 :   if (MAP_MODULE_P (fmap))
   18556         1293 :     from = linemap_included_from (fmap);
   18557              : 
   18558         1293 :   return from;
   18559              : }
   18560              : 
   18561              : /* Note that LOC will need writing.  This allows us to prune locations
   18562              :    that are not needed.  */
   18563              : 
   18564              : bool
   18565     21840548 : module_state::note_location (location_t loc)
   18566              : {
   18567     21840548 :   bool added = false;
   18568     21840548 :   if (!macro_loc_table && !ord_loc_table)
   18569              :     ;
   18570     21840548 :   else if (loc < RESERVED_LOCATION_COUNT)
   18571              :     ;
   18572     19853613 :   else if (IS_ADHOC_LOC (loc))
   18573              :     {
   18574      2428503 :       location_t locus = get_location_from_adhoc_loc (line_table, loc);
   18575      2428503 :       note_location (locus);
   18576      2428503 :       source_range range = get_range_from_loc (line_table, loc);
   18577      2428503 :       if (range.m_start != locus)
   18578      2347942 :         note_location (range.m_start);
   18579      2428503 :       note_location (range.m_finish);
   18580              :     }
   18581     17425110 :   else if (loc >= LINEMAPS_MACRO_LOWEST_LOCATION (line_table))
   18582              :     {
   18583      1299143 :       if (spans.macro (loc))
   18584              :         {
   18585      1299128 :           const line_map *map = linemap_lookup (line_table, loc);
   18586      1299128 :           const line_map_macro *mac_map = linemap_check_macro (map);
   18587      1299128 :           hashval_t hv = macro_loc_traits::hash (mac_map);
   18588      1299128 :           macro_loc_info *slot
   18589      1299128 :             = macro_loc_table->find_slot_with_hash (mac_map, hv, INSERT);
   18590      1299128 :           if (!slot->src)
   18591              :             {
   18592       142666 :               slot->src = mac_map;
   18593       142666 :               slot->remap = 0;
   18594              :               // Expansion locations could themselves be from a
   18595              :               // macro, we need to note them all.
   18596       142666 :               note_location (mac_map->m_expansion);
   18597       142666 :               gcc_checking_assert (mac_map->n_tokens);
   18598       142666 :               location_t tloc = UNKNOWN_LOCATION;
   18599      5241072 :               for (unsigned ix = mac_map->n_tokens * 2; ix--;)
   18600      5098406 :                 if (mac_map->macro_locations[ix] != tloc)
   18601              :                   {
   18602      2694801 :                     tloc = mac_map->macro_locations[ix];
   18603      2694801 :                     note_location (tloc);
   18604              :                   }
   18605              :               added = true;
   18606              :             }
   18607              :         }
   18608              :     }
   18609     16125967 :   else if (IS_ORDINARY_LOC (loc))
   18610              :     {
   18611     16125967 :       if (spans.ordinary (loc))
   18612              :         {
   18613     16118542 :           const line_map *map = linemap_lookup (line_table, loc);
   18614     16118542 :           const line_map_ordinary *ord_map = linemap_check_ordinary (map);
   18615     16118542 :           ord_loc_info lkup;
   18616     16118542 :           lkup.src = ord_map;
   18617     16118542 :           lkup.span = loc_one << ord_map->m_column_and_range_bits;
   18618     16118542 :           lkup.offset = (loc - MAP_START_LOCATION (ord_map)) & ~(lkup.span - 1);
   18619     16118542 :           lkup.remap = 0;
   18620     16118542 :           ord_loc_info *slot = (ord_loc_table->find_slot_with_hash
   18621     16118542 :                                 (lkup, ord_loc_traits::hash (lkup), INSERT));
   18622     16118542 :           if (!slot->src)
   18623              :             {
   18624      1898786 :               *slot = lkup;
   18625      1898786 :               added = true;
   18626              :             }
   18627              :         }
   18628              :     }
   18629              :   else
   18630            0 :     gcc_unreachable ();
   18631     21840548 :   return added;
   18632              : }
   18633              : 
   18634              : /* If we're not streaming, record that we need location LOC.
   18635              :    Otherwise stream it.  */
   18636              : 
   18637              : void
   18638     32611173 : module_state::write_location (bytes_out &sec, location_t loc)
   18639              : {
   18640     32611173 :   if (!sec.streaming_p ())
   18641              :     {
   18642     11526943 :       note_location (loc);
   18643     11526943 :       return;
   18644              :     }
   18645              : 
   18646     21084230 :   if (loc < RESERVED_LOCATION_COUNT)
   18647              :     {
   18648      2035442 :       dump (dumper::LOCATION) && dump ("Reserved location %K", loc);
   18649      2035424 :       sec.loc (LK_RESERVED + loc);
   18650              :     }
   18651     19048806 :   else if (IS_ADHOC_LOC (loc))
   18652              :     {
   18653      2372131 :       dump (dumper::LOCATION) && dump ("Adhoc location");
   18654      2372128 :       sec.u (LK_ADHOC);
   18655      2372128 :       location_t locus = get_location_from_adhoc_loc (line_table, loc);
   18656      2372128 :       write_location (sec, locus);
   18657      2372128 :       source_range range = get_range_from_loc (line_table, loc);
   18658      2372128 :       if (range.m_start == locus)
   18659              :         /* Compress.  */
   18660        75653 :         range.m_start = UNKNOWN_LOCATION;
   18661      2372128 :       write_location (sec, range.m_start);
   18662      2372128 :       write_location (sec, range.m_finish);
   18663      2372128 :       unsigned discriminator = get_discriminator_from_adhoc_loc (line_table, loc);
   18664      2372128 :       sec.u (discriminator);
   18665              :     }
   18666     16676678 :   else if (loc >= LINEMAPS_MACRO_LOWEST_LOCATION (line_table))
   18667              :     {
   18668      1292486 :       const macro_loc_info *info = nullptr;
   18669      1292486 :       line_map_uint_t offset = 0;
   18670      1292486 :       if (unsigned hwm = macro_loc_remap->length ())
   18671              :         {
   18672      1292480 :           info = macro_loc_remap->begin ();
   18673     19133768 :           while (hwm != 1)
   18674              :             {
   18675     16548808 :               unsigned mid = hwm / 2;
   18676     16548808 :               if (MAP_START_LOCATION (info[mid].src) <= loc)
   18677              :                 {
   18678      8348307 :                   info += mid;
   18679      8348307 :                   hwm -= mid;
   18680              :                 }
   18681              :               else
   18682              :                 hwm = mid;
   18683              :             }
   18684      1292480 :           offset = loc - MAP_START_LOCATION (info->src);
   18685      1292480 :           if (offset > info->src->n_tokens)
   18686            9 :             info = nullptr;
   18687              :         }
   18688              : 
   18689      1292486 :       gcc_checking_assert (bool (info) == bool (spans.macro (loc)));
   18690              : 
   18691      1292486 :       if (info)
   18692              :         {
   18693      1292471 :           offset += info->remap;
   18694      1292471 :           sec.u (LK_MACRO);
   18695      1292471 :           sec.loc (offset);
   18696      1292471 :           dump (dumper::LOCATION)
   18697            9 :             && dump ("Macro location %K output %K", loc, offset);
   18698              :         }
   18699           15 :       else if (const module_state *import = module_for_macro_loc (loc))
   18700              :         {
   18701           15 :           auto off = loc - import->macro_locs.first;
   18702           15 :           sec.u (LK_IMPORT_MACRO);
   18703           15 :           sec.u (import->remap);
   18704           15 :           sec.loc (off);
   18705           15 :           dump (dumper::LOCATION)
   18706            0 :             && dump ("Imported macro location %K output %u:%K",
   18707            0 :                      loc, import->remap, off);
   18708              :         }
   18709              :       else
   18710            0 :         gcc_unreachable ();
   18711              :     }
   18712     15384192 :   else if (IS_ORDINARY_LOC (loc))
   18713              :     {
   18714              :       /* If we ran out of locations for imported decls, this location could
   18715              :          be a module unit's location.  In that case, remap the location
   18716              :          to be where we imported the module from.  */
   18717     15384192 :       if (spans.locations_exhausted_p () || CHECKING_P)
   18718              :         {
   18719     15384192 :           const line_map_ordinary *map
   18720     15384192 :             = linemap_check_ordinary (linemap_lookup (line_table, loc));
   18721     15384192 :           if (MAP_MODULE_P (map) && loc == MAP_START_LOCATION (map))
   18722              :             {
   18723            0 :               gcc_checking_assert (spans.locations_exhausted_p ());
   18724            0 :               write_location (sec, linemap_included_from (map));
   18725            0 :               return;
   18726              :             }
   18727              :         }
   18728              : 
   18729     15384192 :       const ord_loc_info *info = nullptr;
   18730     15384192 :       line_map_uint_t offset = 0;
   18731     15384192 :       if (line_map_uint_t hwm = ord_loc_remap->length ())
   18732              :         {
   18733     15384192 :           info = ord_loc_remap->begin ();
   18734    226644558 :           while (hwm != 1)
   18735              :             {
   18736    195876174 :               auto mid = hwm / 2;
   18737    195876174 :               if (MAP_START_LOCATION (info[mid].src) + info[mid].offset <= loc)
   18738              :                 {
   18739    102099427 :                   info += mid;
   18740    102099427 :                   hwm -= mid;
   18741              :                 }
   18742              :               else
   18743              :                 hwm = mid;
   18744              :             }
   18745     15384192 :           offset = loc - MAP_START_LOCATION (info->src) - info->offset;
   18746     15384192 :           if (offset > info->span)
   18747         7417 :             info = nullptr;
   18748              :         }
   18749              : 
   18750     15384192 :       gcc_checking_assert (bool (info) == bool (spans.ordinary (loc)));
   18751              : 
   18752     15384192 :       if (info)
   18753              :         {
   18754     15376775 :           offset += info->remap;
   18755     15376775 :           sec.u (LK_ORDINARY);
   18756     15376775 :           sec.loc (offset);
   18757              : 
   18758     15376775 :           dump (dumper::LOCATION)
   18759           78 :             && dump ("Ordinary location %K output %K", loc, offset);
   18760              :         }
   18761         7417 :       else if (const module_state *import = module_for_ordinary_loc (loc))
   18762              :         {
   18763         7417 :           auto off = loc - import->ordinary_locs.first;
   18764         7417 :           sec.u (LK_IMPORT_ORDINARY);
   18765         7417 :           sec.u (import->remap);
   18766         7417 :           sec.loc (off);
   18767         7417 :           dump (dumper::LOCATION)
   18768            0 :             && dump ("Imported ordinary location %K output %u:%K",
   18769            0 :                      loc, import->remap, off);
   18770              :         }
   18771              :       else
   18772            0 :         gcc_unreachable ();
   18773              :     }
   18774              :   else
   18775            0 :     gcc_unreachable ();
   18776              : }
   18777              : 
   18778              : location_t
   18779     21056241 : module_state::read_location (bytes_in &sec) const
   18780              : {
   18781     21056241 :   location_t locus = UNKNOWN_LOCATION;
   18782     21056241 :   unsigned kind = sec.u ();
   18783     21056241 :   switch (kind)
   18784              :      {
   18785      1964233 :     default:
   18786      1964233 :       {
   18787      1964233 :         if (kind < LK_RESERVED + RESERVED_LOCATION_COUNT)
   18788      1964233 :           locus = location_t (kind - LK_RESERVED);
   18789              :         else
   18790            0 :           sec.set_overrun ();
   18791      1964233 :         dump (dumper::LOCATION)
   18792            0 :           && dump ("Reserved location %K", locus);
   18793              :       }
   18794              :       break;
   18795              : 
   18796      2249627 :      case LK_ADHOC:
   18797      2249627 :       {
   18798      2249627 :         dump (dumper::LOCATION) && dump ("Adhoc location");
   18799      2249627 :         locus = read_location (sec);
   18800      2249627 :         source_range range;
   18801      2249627 :         range.m_start = read_location (sec);
   18802      2249627 :         if (range.m_start == UNKNOWN_LOCATION)
   18803        68566 :           range.m_start = locus;
   18804      2249627 :         range.m_finish = read_location (sec);
   18805      2249627 :         unsigned discriminator = sec.u ();
   18806      2249627 :         if (locus != loc && range.m_start != loc && range.m_finish != loc)
   18807      2249627 :           locus = line_table->get_or_create_combined_loc (locus, range,
   18808              :                                                           nullptr, discriminator);
   18809              :       }
   18810              :       break;
   18811              : 
   18812      1570001 :     case LK_MACRO:
   18813      1570001 :       {
   18814      1570001 :         auto off = sec.loc ();
   18815              : 
   18816      1570001 :         if (macro_locs.second)
   18817              :           {
   18818      1570001 :             if (off < macro_locs.second)
   18819      1570001 :               locus = off + macro_locs.first;
   18820              :             else
   18821            0 :               sec.set_overrun ();
   18822              :           }
   18823              :         else
   18824            0 :           locus = loc;
   18825      1570001 :         dump (dumper::LOCATION)
   18826            0 :           && dump ("Macro %K becoming %K", off, locus);
   18827              :       }
   18828              :       break;
   18829              : 
   18830     15268694 :     case LK_ORDINARY:
   18831     15268694 :       {
   18832     15268694 :         auto off = sec.loc ();
   18833     15268694 :         if (ordinary_locs.second)
   18834              :           {
   18835     15268694 :             if (off < ordinary_locs.second)
   18836     15268694 :               locus = off + ordinary_locs.first;
   18837              :             else
   18838            0 :               sec.set_overrun ();
   18839              :           }
   18840              :         else
   18841            0 :           locus = loc;
   18842              : 
   18843     15268694 :         dump (dumper::LOCATION)
   18844            0 :           && dump ("Ordinary location %K becoming %K", off, locus);
   18845              :       }
   18846              :       break;
   18847              : 
   18848         3686 :      case LK_IMPORT_MACRO:
   18849         3686 :      case LK_IMPORT_ORDINARY:
   18850         3686 :        {
   18851         3686 :          unsigned mod = sec.u ();
   18852         3686 :          location_t off = sec.loc ();
   18853         3686 :          const module_state *import = NULL;
   18854              : 
   18855         3686 :          if (!mod && !slurp->remap)
   18856              :            /* This is an early read of a partition location during the
   18857              :               read of our ordinary location map.  */
   18858              :            import = this;
   18859              :          else
   18860              :            {
   18861         3686 :              mod = slurp->remap_module (mod);
   18862         3686 :              if (!mod)
   18863            0 :                sec.set_overrun ();
   18864              :              else
   18865         3686 :                import = (*modules)[mod];
   18866              :            }
   18867              : 
   18868         3686 :          if (import)
   18869              :            {
   18870         3686 :              if (kind == LK_IMPORT_MACRO)
   18871              :                {
   18872           24 :                  if (!import->macro_locs.second)
   18873            0 :                    locus = import->loc;
   18874           24 :                  else if (off < import->macro_locs.second)
   18875           24 :                    locus = off + import->macro_locs.first;
   18876              :                  else
   18877            0 :                    sec.set_overrun ();
   18878              :                }
   18879              :              else
   18880              :                {
   18881         3662 :                  if (!import->ordinary_locs.second)
   18882            0 :                    locus = import->loc;
   18883         3662 :                  else if (off < import->ordinary_locs.second)
   18884         3662 :                    locus = import->ordinary_locs.first + off;
   18885              :                  else
   18886            0 :                    sec.set_overrun ();
   18887              :                }
   18888              :            }
   18889              :        }
   18890              :        break;
   18891              :     }
   18892              : 
   18893     21056241 :   return locus;
   18894              : }
   18895              : 
   18896              : /* Allocate hash tables to record needed locations.  */
   18897              : 
   18898              : void
   18899         2742 : module_state::write_init_maps ()
   18900              : {
   18901         2742 :   macro_loc_table = new hash_table<macro_loc_traits> (EXPERIMENT (1, 400));
   18902         2742 :   ord_loc_table = new hash_table<ord_loc_traits> (EXPERIMENT (1, 400));
   18903         2742 : }
   18904              : 
   18905              : /* Prepare the span adjustments.  We prune unneeded locations -- at
   18906              :    this point every needed location must have been seen by
   18907              :    note_location.  */
   18908              : 
   18909              : range_t
   18910         2713 : module_state::write_prepare_maps (module_state_config *cfg, bool has_partitions)
   18911              : {
   18912         3013 :   dump () && dump ("Preparing locations");
   18913         2713 :   dump.indent ();
   18914              : 
   18915         3013 :   dump () && dump ("Reserved locations [%K,%K) macro [%K,%K)",
   18916          300 :                    spans[loc_spans::SPAN_RESERVED].ordinary.first,
   18917          300 :                    spans[loc_spans::SPAN_RESERVED].ordinary.second,
   18918          300 :                    spans[loc_spans::SPAN_RESERVED].macro.first,
   18919          300 :                    spans[loc_spans::SPAN_RESERVED].macro.second);
   18920              : 
   18921         2713 :   range_t info {0, 0};
   18922              : 
   18923              :   // Sort the noted lines.
   18924         2713 :   vec_alloc (ord_loc_remap, ord_loc_table->size ());
   18925         2713 :   for (auto iter = ord_loc_table->begin (), end = ord_loc_table->end ();
   18926      3775583 :        iter != end; ++iter)
   18927      1886435 :     ord_loc_remap->quick_push (*iter);
   18928         2713 :   ord_loc_remap->qsort (&ord_loc_info::compare);
   18929              : 
   18930              :   // Note included-from maps.
   18931         2713 :   bool added = false;
   18932         2713 :   const line_map_ordinary *current = nullptr;
   18933      1894574 :   for (auto iter = ord_loc_remap->begin (), end = ord_loc_remap->end ();
   18934      1889148 :        iter != end; ++iter)
   18935      1886435 :     if (iter->src != current)
   18936              :       {
   18937        30292 :         current = iter->src;
   18938        11869 :         for (auto probe = current;
   18939        30292 :              auto from = linemap_included_from (probe);
   18940        11869 :              probe = linemap_check_ordinary (linemap_lookup (line_table, from)))
   18941              :           {
   18942        27233 :             if (has_partitions)
   18943              :               {
   18944              :                 // Partition locations need to elide their module map
   18945              :                 // entry.
   18946          217 :                 probe
   18947          217 :                   = linemap_check_ordinary (linemap_lookup (line_table, from));
   18948          217 :                 if (MAP_MODULE_P (probe))
   18949          184 :                   from = linemap_included_from (probe);
   18950              :               }
   18951              : 
   18952        27233 :             if (!note_location (from))
   18953              :               break;
   18954        11869 :             added = true;
   18955        11869 :           }
   18956              :       }
   18957         2713 :   if (added)
   18958              :     {
   18959              :       // Reconstruct the line array as we added items to the hash table.
   18960          486 :       vec_free (ord_loc_remap);
   18961          486 :       vec_alloc (ord_loc_remap, ord_loc_table->size ());
   18962          486 :       for (auto iter = ord_loc_table->begin (), end = ord_loc_table->end ();
   18963      3773524 :            iter != end; ++iter)
   18964      1886519 :         ord_loc_remap->quick_push (*iter);
   18965          486 :       ord_loc_remap->qsort (&ord_loc_info::compare);
   18966              :     }
   18967         2713 :   delete ord_loc_table;
   18968         2713 :   ord_loc_table = nullptr;
   18969              : 
   18970              :   // Merge (sufficiently) adjacent spans, and calculate remapping.
   18971         2713 :   constexpr line_map_uint_t adjacency = 2; // Allow 2 missing lines.
   18972         5426 :   auto begin = ord_loc_remap->begin (), end = ord_loc_remap->end ();
   18973         2713 :   auto dst = begin;
   18974         2713 :   line_map_uint_t offset = 0;
   18975         2713 :   unsigned range_bits = 0;
   18976         2713 :   ord_loc_info *base = nullptr;
   18977      1901017 :   for (auto iter = begin; iter != end; ++iter)
   18978              :     {
   18979      1898304 :       if (base && iter->src == base->src)
   18980              :         {
   18981      3546806 :           if (base->offset + base->span +
   18982      1872479 :               ((adjacency << base->src->m_column_and_range_bits)
   18983              :                // If there are few c&r bits, allow further separation.
   18984      1872479 :                | (adjacency << 4))
   18985      1872479 :               >= iter->offset)
   18986              :             {
   18987              :               // Merge.
   18988      1674327 :               offset -= base->span;
   18989      1674327 :               base->span = iter->offset + iter->span - base->offset;
   18990      1674327 :               offset += base->span;
   18991      1674327 :               continue;
   18992              :             }
   18993              :         }
   18994        25825 :       else if (range_bits < iter->src->m_range_bits)
   18995         2617 :         range_bits = iter->src->m_range_bits;
   18996              : 
   18997       223977 :       offset += ((loc_one << iter->src->m_range_bits) - 1);
   18998       223977 :       offset &= ~((loc_one << iter->src->m_range_bits) - 1);
   18999       223977 :       iter->remap = offset;
   19000       223977 :       offset += iter->span;
   19001       223977 :       base = dst;
   19002       223977 :       *dst++ = *iter;
   19003              :     }
   19004         2713 :   ord_loc_remap->truncate (dst - begin);
   19005              : 
   19006         2713 :   info.first = ord_loc_remap->length ();
   19007         2713 :   cfg->ordinary_locs = offset;
   19008         2713 :   cfg->loc_range_bits = range_bits;
   19009         3013 :   dump () && dump ("Ordinary maps:%K locs:%K range_bits:%u",
   19010              :                    info.first,
   19011              :                    cfg->ordinary_locs,
   19012              :                    cfg->loc_range_bits);
   19013              : 
   19014              :   // Remap the macro locations.
   19015         2713 :   vec_alloc (macro_loc_remap, macro_loc_table->size ());
   19016         2713 :   for (auto iter = macro_loc_table->begin (), end = macro_loc_table->end ();
   19017       288045 :        iter != end; ++iter)
   19018       142666 :     macro_loc_remap->quick_push (*iter);
   19019         2713 :   delete macro_loc_table;
   19020         2713 :   macro_loc_table = nullptr;
   19021              : 
   19022         2713 :   macro_loc_remap->qsort (&macro_loc_info::compare);
   19023         2713 :   offset = 0;
   19024         8139 :   for (auto iter = macro_loc_remap->begin (), end = macro_loc_remap->end ();
   19025       145379 :        iter != end; ++iter)
   19026              :     {
   19027       142666 :       auto mac = iter->src;
   19028       142666 :       iter->remap = offset;
   19029       142666 :       offset += mac->n_tokens;
   19030              :     }
   19031         2713 :   info.second = macro_loc_remap->length ();
   19032         2713 :   cfg->macro_locs = offset;
   19033              : 
   19034         3013 :   dump () && dump ("Macro maps:%K locs:%K", info.second, cfg->macro_locs);
   19035              : 
   19036         2713 :   dump.outdent ();
   19037              : 
   19038              :   // If we have no ordinary locs, we must also have no macro locs.
   19039         2713 :   gcc_checking_assert (cfg->ordinary_locs || !cfg->macro_locs);
   19040              : 
   19041         2713 :   return info;
   19042              : }
   19043              : 
   19044              : bool
   19045         2952 : module_state::read_prepare_maps (const module_state_config *cfg)
   19046              : {
   19047         2952 :   location_t ordinary = line_table->highest_location + 1;
   19048         2952 :   ordinary += cfg->ordinary_locs;
   19049              : 
   19050         2952 :   location_t macro = LINEMAPS_MACRO_LOWEST_LOCATION (line_table);
   19051         2952 :   macro -= cfg->macro_locs;
   19052              : 
   19053         2952 :   if (ordinary < LINE_MAP_MAX_LOCATION_WITH_COLS
   19054         2952 :       && macro >= LINE_MAP_MAX_LOCATION)
   19055              :     /* OK, we have enough locations.  */
   19056              :     return true;
   19057              : 
   19058            0 :   ordinary_locs.first = ordinary_locs.second = 0;
   19059            0 :   macro_locs.first = macro_locs.second = 0;
   19060              : 
   19061            0 :   spans.report_location_exhaustion (loc);
   19062              : 
   19063              :   return false;
   19064              : }
   19065              : 
   19066              : /* Write & read the location maps. Not called if there are no
   19067              :    locations.   */
   19068              : 
   19069              : void
   19070         2617 : module_state::write_ordinary_maps (elf_out *to, range_t &info,
   19071              :                                    bool has_partitions, unsigned *crc_p)
   19072              : {
   19073         2895 :   dump () && dump ("Writing ordinary location maps");
   19074         2617 :   dump.indent ();
   19075              : 
   19076         2617 :   vec<const char *> filenames;
   19077         2617 :   filenames.create (20);
   19078              : 
   19079              :   /* Determine the unique filenames.  */
   19080         2617 :   const line_map_ordinary *current = nullptr;
   19081       231828 :   for (auto iter = ord_loc_remap->begin (), end = ord_loc_remap->end ();
   19082       226594 :        iter != end; ++iter)
   19083       223977 :     if (iter->src != current)
   19084              :       {
   19085        25825 :         current = iter->src;
   19086        25825 :         const char *fname = ORDINARY_MAP_FILE_NAME (iter->src);
   19087              : 
   19088              :         /* We should never find a module linemap in an interval.  */
   19089        25825 :         gcc_checking_assert (!MAP_MODULE_P (iter->src));
   19090              : 
   19091              :         /* We expect very few filenames, so just an array.
   19092              :            (Not true when headers are still in play :()  */
   19093      1765263 :         for (unsigned jx = filenames.length (); jx--;)
   19094              :           {
   19095      1725748 :             const char *name = filenames[jx];
   19096      1725748 :             if (0 == strcmp (name, fname))
   19097              :               {
   19098              :                 /* Reset the linemap's name, because for things like
   19099              :                    preprocessed input we could have multiple instances
   19100              :                    of the same name, and we'd rather not percolate
   19101              :                    that.  */
   19102        12135 :                 const_cast<line_map_ordinary *> (iter->src)->to_file = name;
   19103        12135 :                 fname = NULL;
   19104        12135 :                 break;
   19105              :               }
   19106              :           }
   19107        25825 :         if (fname)
   19108        13690 :           filenames.safe_push (fname);
   19109              :       }
   19110              : 
   19111         2617 :   bytes_out sec (to);
   19112         2617 :   sec.begin ();
   19113              : 
   19114              :   /* Write the filenames.  */
   19115         2617 :   unsigned len = filenames.length ();
   19116         2617 :   sec.u (len);
   19117         2895 :   dump () && dump ("%u source file names", len);
   19118        16307 :   for (unsigned ix = 0; ix != len; ix++)
   19119              :     {
   19120        13690 :       const char *fname = filenames[ix];
   19121        13705 :       dump (dumper::LOCATION) && dump ("Source file[%u]=%s", ix, fname);
   19122        13690 :       sec.str (fname);
   19123              :     }
   19124              : 
   19125         2617 :   sec.loc (info.first); /* Num maps.  */
   19126         2617 :   const ord_loc_info *base = nullptr;
   19127       231828 :   for (auto iter = ord_loc_remap->begin (), end = ord_loc_remap->end ();
   19128       226594 :        iter != end; ++iter)
   19129              :     {
   19130       223977 :       dump (dumper::LOCATION)
   19131           36 :         && dump ("Span:%K ordinary [%K+%K,+%K)->[%K,+%K)",
   19132           36 :                  (location_t) (iter - ord_loc_remap->begin ()),
   19133           18 :                  MAP_START_LOCATION (iter->src),
   19134              :                  iter->offset, iter->span, iter->remap,
   19135              :                  iter->span);
   19136              : 
   19137       223977 :       if (!base || iter->src != base->src)
   19138        25825 :         base = iter;
   19139       223977 :       sec.loc (iter->offset - base->offset);
   19140       223977 :       if (base == iter)
   19141              :         {
   19142        25825 :           sec.u (iter->src->sysp);
   19143        25825 :           sec.u (iter->src->m_range_bits);
   19144        25825 :           sec.u (iter->src->m_column_and_range_bits - iter->src->m_range_bits);
   19145              : 
   19146        25825 :           const char *fname = ORDINARY_MAP_FILE_NAME (iter->src);
   19147      5442786 :           for (unsigned ix = 0; ix != filenames.length (); ix++)
   19148      2721393 :             if (filenames[ix] == fname)
   19149              :               {
   19150        25825 :                 sec.u (ix);
   19151        25825 :                 break;
   19152              :               }
   19153        25825 :           unsigned line = ORDINARY_MAP_STARTING_LINE_NUMBER (iter->src);
   19154        25825 :           line += iter->offset >> iter->src->m_column_and_range_bits;
   19155        25825 :           sec.u (line);
   19156              :         }
   19157       223977 :       sec.loc (iter->remap);
   19158       223977 :       if (base == iter)
   19159              :         {
   19160              :           /* Write the included from location, which means reading it
   19161              :              while reading in the ordinary maps.  So we'd better not
   19162              :              be getting ahead of ourselves.  */
   19163        25825 :           location_t from = linemap_included_from (iter->src);
   19164        25825 :           gcc_checking_assert (from < MAP_START_LOCATION (iter->src));
   19165        25825 :           if (from != UNKNOWN_LOCATION && has_partitions)
   19166              :             {
   19167              :               /* A partition's span will have a from pointing at a
   19168              :                  MODULE_INC.  Find that map's from.  */
   19169          211 :               line_map_ordinary const *fmap
   19170          211 :                 = linemap_check_ordinary (linemap_lookup (line_table, from));
   19171          211 :               if (MAP_MODULE_P (fmap))
   19172          178 :                 from = linemap_included_from (fmap);
   19173              :             }
   19174        25825 :           write_location (sec, from);
   19175              :         }
   19176              :     }
   19177              : 
   19178         2617 :   filenames.release ();
   19179              : 
   19180         2617 :   sec.end (to, to->name (MOD_SNAME_PFX ".olm"), crc_p);
   19181         2617 :   dump.outdent ();
   19182         2617 : }
   19183              : 
   19184              : /* Return the prefix to use for dumping a #pragma diagnostic change to DK.  */
   19185              : 
   19186              : static const char *
   19187          990 : dk_string (enum diagnostics::kind dk)
   19188              : {
   19189          990 :   gcc_assert (dk > diagnostics::kind::unspecified
   19190              :               && dk < diagnostics::kind::last_diagnostic_kind);
   19191          990 :   if (dk == diagnostics::kind::ignored)
   19192              :     /* diagnostics/kinds.def has an empty string for ignored.  */
   19193              :     return "ignored: ";
   19194              :   else
   19195            0 :     return diagnostics::get_text_for_kind (dk);
   19196              : }
   19197              : 
   19198              : /* Dump one #pragma GCC diagnostic entry.  */
   19199              : 
   19200              : static bool
   19201         2014 : dump_dc_change (unsigned index, unsigned opt, enum diagnostics::kind dk)
   19202              : {
   19203         2014 :   if (dk == diagnostics::kind::pop)
   19204         1024 :     return dump (" Index %u: pop from %d", index, opt);
   19205              :   else
   19206          990 :     return dump (" Index %u: %s%s", index, dk_string (dk),
   19207         1980 :                  cl_options[opt].opt_text);
   19208              : }
   19209              : 
   19210              : /* Write out any #pragma GCC diagnostic info to the .dgc section.  */
   19211              : 
   19212              : void
   19213         5330 : module_state::write_diagnostic_classification (elf_out *to,
   19214              :                                                diagnostics::context *dc,
   19215              :                                                unsigned *crc_p)
   19216              : {
   19217         5330 :   auto &changes = dc->get_classification_history ();
   19218              : 
   19219         5330 :   bytes_out sec (to);
   19220         5330 :   if (sec.streaming_p ())
   19221              :     {
   19222         2617 :       sec.begin ();
   19223         2895 :       dump () && dump ("Writing diagnostic change locations");
   19224         2617 :       dump.indent ();
   19225              :     }
   19226              : 
   19227         5330 :   unsigned len = changes.length ();
   19228              : 
   19229              :   /* We don't want to write out any entries that came from one of our imports.
   19230              :      But then we need to adjust the total, and change diagnostics::kind::pop
   19231              :      targets to match the index in our actual output.  So remember how many
   19232              :      lines we had skipped at each step, where -1 means this line itself
   19233              :      is skipped.  */
   19234         5330 :   int skips = 0;
   19235         5330 :   auto_vec<int> skips_at (len);
   19236         5330 :   skips_at.safe_grow (len);
   19237              : 
   19238        59964 :   for (unsigned i = 0; i < len; ++i)
   19239              :     {
   19240        54634 :       const auto &c = changes[i];
   19241        54634 :       skips_at[i] = skips;
   19242        54634 :       if (linemap_location_from_module_p (line_table, c.location))
   19243              :         {
   19244        13320 :           ++skips;
   19245        13320 :           skips_at[i] = -1;
   19246        13320 :           continue;
   19247              :         }
   19248              :     }
   19249              : 
   19250         5330 :   if (sec.streaming_p ())
   19251              :     {
   19252         2617 :       sec.u (len - skips);
   19253         2895 :       dump () && dump ("Diagnostic changes: %u", len - skips);
   19254              :     }
   19255              : 
   19256        59964 :   for (unsigned i = 0; i < len; ++i)
   19257              :     {
   19258        54634 :       if (skips_at[i] == -1)
   19259        13320 :         continue;
   19260              : 
   19261        41314 :       const auto &c = changes[i];
   19262        41314 :       write_location (sec, c.location);
   19263        41314 :       if (sec.streaming_p ())
   19264              :         {
   19265        20657 :           unsigned opt = c.option;
   19266        20657 :           if (c.kind == diagnostics::kind::pop)
   19267        10534 :             opt -= skips_at[opt];
   19268        20657 :           sec.u (opt);
   19269        20657 :           sec.u (static_cast<unsigned> (c.kind));
   19270        56581 :           dump () && dump_dc_change (i - skips_at[i], opt, c.kind);
   19271              :         }
   19272              :     }
   19273              : 
   19274         5330 :   if (sec.streaming_p ())
   19275              :     {
   19276         2617 :       sec.end (to, to->name (MOD_SNAME_PFX ".dgc"), crc_p);
   19277         2617 :       dump.outdent ();
   19278              :     }
   19279         5330 : }
   19280              : 
   19281              : /* Read any #pragma GCC diagnostic info from the .dgc section.  */
   19282              : 
   19283              : bool
   19284         2894 : module_state::read_diagnostic_classification (diagnostics::context *dc)
   19285              : {
   19286         2894 :   bytes_in sec;
   19287              : 
   19288         2894 :   if (!sec.begin (loc, from (), MOD_SNAME_PFX ".dgc"))
   19289              :     return false;
   19290              : 
   19291         3411 :   dump () && dump ("Reading diagnostic change locations");
   19292         2894 :   dump.indent ();
   19293              : 
   19294         2894 :   unsigned len = sec.u ();
   19295         3411 :   dump () && dump ("Diagnostic changes: %u", len);
   19296              : 
   19297         2894 :   auto &changes = dc->get_classification_history ();
   19298         2894 :   int offset = changes.length ();
   19299         2894 :   changes.reserve (len + 1);
   19300        26405 :   for (unsigned i = 0; i < len; ++i)
   19301              :     {
   19302        23511 :       location_t loc = read_location (sec);
   19303        23511 :       int opt = sec.u ();
   19304        23511 :       enum diagnostics::kind kind = (enum diagnostics::kind) sec.u ();
   19305        23511 :       if (kind == diagnostics::kind::pop)
   19306              :         /* For a pop, opt is the 'changes' index to return to.  */
   19307        12034 :         opt += offset;
   19308        23511 :       changes.quick_push ({ loc, opt, kind });
   19309        23578 :       dump () && dump_dc_change (changes.length () - 1, opt, kind);
   19310              :     }
   19311              : 
   19312              :   /* Did the import pop all its diagnostic changes?  */
   19313         2894 :   bool last_was_reset = (len == 0);
   19314         2894 :   if (len)
   19315          228 :     for (int i = changes.length () - 1; ; --i)
   19316              :       {
   19317        10301 :         gcc_checking_assert (i >= offset);
   19318              : 
   19319        10301 :         const auto &c = changes[i];
   19320        10301 :         if (c.kind != diagnostics::kind::pop)
   19321              :           break;
   19322        10292 :         else if (c.option == offset)
   19323              :           {
   19324              :             last_was_reset = true;
   19325              :             break;
   19326              :           }
   19327              :         else
   19328              :           /* As in update_effective_level_from_pragmas, the loop will decrement
   19329              :              i so we actually jump to c.option - 1.  */
   19330        10187 :           i = c.option;
   19331        10187 :       }
   19332         2894 :   if (!last_was_reset)
   19333              :     {
   19334              :       /* It didn't, so add a pop at its last location to avoid affecting later
   19335              :          imports.  */
   19336            9 :       location_t last_loc = ordinary_locs.first + ordinary_locs.second - 1;
   19337            9 :       changes.quick_push ({ last_loc, offset, diagnostics::kind::pop });
   19338           15 :       dump () && dump (" Adding final pop from index %d", offset);
   19339              :     }
   19340              : 
   19341         2894 :   dump.outdent ();
   19342         2894 :   if (!sec.end (from ()))
   19343              :     return false;
   19344              : 
   19345              :   return true;
   19346         2894 : }
   19347              : 
   19348              : void
   19349          120 : module_state::write_macro_maps (elf_out *to, range_t &info, unsigned *crc_p)
   19350              : {
   19351          132 :   dump () && dump ("Writing macro location maps");
   19352          120 :   dump.indent ();
   19353              : 
   19354          120 :   bytes_out sec (to);
   19355          120 :   sec.begin ();
   19356              : 
   19357          132 :   dump () && dump ("Macro maps:%K", info.second);
   19358          120 :   sec.loc (info.second);
   19359              : 
   19360          120 :   line_map_uint_t macro_num = 0;
   19361          240 :   for (auto iter = macro_loc_remap->end (), begin = macro_loc_remap->begin ();
   19362       142786 :        iter-- != begin;)
   19363              :     {
   19364       142666 :       auto mac = iter->src;
   19365       142666 :       sec.loc (iter->remap);
   19366       142666 :       sec.u (mac->n_tokens);
   19367       142666 :       sec.cpp_node (mac->macro);
   19368       142666 :       write_location (sec, mac->m_expansion);
   19369       142666 :       const location_t *locs = mac->macro_locations;
   19370              :       /* There are lots of identical runs.  */
   19371       142666 :       location_t prev = UNKNOWN_LOCATION;
   19372       142666 :       unsigned count = 0;
   19373       142666 :       unsigned runs = 0;
   19374      5241072 :       for (unsigned jx = mac->n_tokens * 2; jx--;)
   19375              :         {
   19376      5098406 :           location_t tok_loc = locs[jx];
   19377      5098406 :           if (tok_loc == prev)
   19378              :             {
   19379      2403605 :               count++;
   19380      2403605 :               continue;
   19381              :             }
   19382      2694801 :           runs++;
   19383      2694801 :           sec.u (count);
   19384      2694801 :           count = 1;
   19385      2694801 :           prev = tok_loc;
   19386      2694801 :           write_location (sec, tok_loc);
   19387              :         }
   19388       142666 :       sec.u (count);
   19389       142666 :       dump (dumper::LOCATION)
   19390            9 :         && dump ("Macro:%K %I %u/%u*2 locations [%K,%K)->%K",
   19391            9 :                  macro_num, identifier (mac->macro),
   19392              :                  runs, mac->n_tokens,
   19393              :                  MAP_START_LOCATION (mac),
   19394            9 :                  MAP_START_LOCATION (mac) + mac->n_tokens,
   19395              :                  iter->remap);
   19396       142666 :       macro_num++;
   19397              :     }
   19398          120 :   gcc_assert (macro_num == info.second);
   19399              : 
   19400          120 :   sec.end (to, to->name (MOD_SNAME_PFX ".mlm"), crc_p);
   19401          120 :   dump.outdent ();
   19402          120 : }
   19403              : 
   19404              : bool
   19405         2894 : module_state::read_ordinary_maps (line_map_uint_t num_ord_locs,
   19406              :                                   unsigned range_bits)
   19407              : {
   19408         2894 :   bytes_in sec;
   19409              : 
   19410         2894 :   if (!sec.begin (loc, from (), MOD_SNAME_PFX ".olm"))
   19411              :     return false;
   19412         3411 :   dump () && dump ("Reading ordinary location maps");
   19413         2894 :   dump.indent ();
   19414              : 
   19415              :   /* Read the filename table.  */
   19416         2894 :   unsigned len = sec.u ();
   19417         3411 :   dump () && dump ("%u source file names", len);
   19418         2894 :   vec<const char *> filenames;
   19419         2894 :   filenames.create (len);
   19420        19344 :   for (unsigned ix = 0; ix != len; ix++)
   19421              :     {
   19422        16450 :       size_t l;
   19423        16450 :       const char *buf = sec.str (&l);
   19424        16450 :       char *fname = XNEWVEC (char, l + 1);
   19425        16450 :       memcpy (fname, buf, l + 1);
   19426        16450 :       dump (dumper::LOCATION) && dump ("Source file[%u]=%s", ix, fname);
   19427              :       /* We leak these names into the line-map table.  But it
   19428              :          doesn't own them.  */
   19429        16450 :       filenames.quick_push (fname);
   19430              :     }
   19431              : 
   19432         2894 :   line_map_uint_t num_ordinary = sec.loc ();
   19433         3411 :   dump () && dump ("Ordinary maps:%K, range_bits:%u",
   19434              :                    num_ordinary, range_bits);
   19435              : 
   19436         2894 :   location_t offset = line_table->highest_location + 1;
   19437         2894 :   offset += ((loc_one << range_bits) - 1);
   19438         2894 :   offset &= ~((loc_one << range_bits) - 1);
   19439         2894 :   ordinary_locs.first = offset;
   19440              : 
   19441         2894 :   bool propagated = spans.maybe_propagate (this, offset);
   19442         2894 :   line_map_ordinary *maps = static_cast<line_map_ordinary *>
   19443         2894 :     (line_map_new_raw (line_table, false, num_ordinary));
   19444              : 
   19445         2894 :   const line_map_ordinary *base = nullptr;
   19446       294363 :   for (line_map_uint_t ix = 0; ix != num_ordinary && !sec.get_overrun (); ix++)
   19447              :     {
   19448       291469 :       line_map_ordinary *map = &maps[ix];
   19449              : 
   19450       291469 :       location_t offset = sec.loc ();
   19451       291469 :       if (!offset)
   19452              :         {
   19453        31978 :           map->reason = LC_RENAME;
   19454        31978 :           map->sysp = sec.u ();
   19455        31978 :           map->m_range_bits = sec.u ();
   19456        31978 :           map->m_column_and_range_bits = sec.u () + map->m_range_bits;
   19457        31978 :           unsigned fnum = sec.u ();
   19458        63956 :           map->to_file = (fnum < filenames.length () ? filenames[fnum] : "");
   19459        31978 :           map->to_line = sec.u ();
   19460        31978 :           base = map;
   19461              :         }
   19462              :       else
   19463              :         {
   19464       259491 :           *map = *base;
   19465       259491 :           map->to_line += offset >> map->m_column_and_range_bits;
   19466              :         }
   19467       291469 :       location_t remap = sec.loc ();
   19468       291469 :       map->start_location = remap + ordinary_locs.first;
   19469       291469 :       if (base == map)
   19470              :         {
   19471              :           /* Root the outermost map at our location.  */
   19472        31978 :           ordinary_locs.second = remap;
   19473        31978 :           location_t from = read_location (sec);
   19474        31978 :           map->included_from = from != UNKNOWN_LOCATION ? from : loc;
   19475              :         }
   19476              :     }
   19477              : 
   19478         2894 :   ordinary_locs.second = num_ord_locs;
   19479              :   /* highest_location is the one handed out, not the next one to
   19480              :      hand out.  */
   19481         2894 :   line_table->highest_location = ordinary_locs.first + ordinary_locs.second - 1;
   19482              : 
   19483         2894 :   if (line_table->highest_location >= LINE_MAP_MAX_LOCATION_WITH_COLS)
   19484              :     /* We shouldn't run out of locations, as we checked before
   19485              :        starting.  */
   19486            0 :     sec.set_overrun ();
   19487         3411 :   dump () && dump ("Ordinary location [%K,+%K)",
   19488              :                    ordinary_locs.first, ordinary_locs.second);
   19489              : 
   19490         2894 :   if (propagated)
   19491          166 :     spans.close ();
   19492              : 
   19493         2894 :   filenames.release ();
   19494              : 
   19495         2894 :   dump.outdent ();
   19496         2894 :   if (!sec.end (from ()))
   19497              :     return false;
   19498              : 
   19499              :   return true;
   19500         2894 : }
   19501              : 
   19502              : bool
   19503          131 : module_state::read_macro_maps (line_map_uint_t num_macro_locs)
   19504              : {
   19505          131 :   bytes_in sec;
   19506              : 
   19507          131 :   if (!sec.begin (loc, from (), MOD_SNAME_PFX ".mlm"))
   19508              :     return false;
   19509          140 :   dump () && dump ("Reading macro location maps");
   19510          131 :   dump.indent ();
   19511              : 
   19512          131 :   line_map_uint_t num_macros = sec.loc ();
   19513          140 :   dump () && dump ("Macro maps:%K locs:%K",
   19514              :                    num_macros, num_macro_locs);
   19515              : 
   19516          262 :   bool propagated = spans.maybe_propagate (this,
   19517          131 :                                            line_table->highest_location + 1);
   19518              : 
   19519          131 :   location_t offset = LINEMAPS_MACRO_LOWEST_LOCATION (line_table);
   19520          131 :   macro_locs.second = num_macro_locs;
   19521          131 :   macro_locs.first = offset - num_macro_locs;
   19522              : 
   19523          140 :   dump () && dump ("Macro loc delta %K", offset);
   19524          140 :   dump () && dump ("Macro locations [%K,%K)",
   19525              :                    macro_locs.first, macro_locs.second);
   19526              : 
   19527       201047 :   for (line_map_uint_t ix = 0; ix != num_macros && !sec.get_overrun (); ix++)
   19528              :     {
   19529       200916 :       location_t offset = sec.loc ();
   19530       200916 :       unsigned n_tokens = sec.u ();
   19531       200916 :       cpp_hashnode *node = sec.cpp_node ();
   19532       200916 :       location_t exp_loc = read_location (sec);
   19533              : 
   19534       200916 :       const line_map_macro *macro
   19535       200916 :         = linemap_enter_macro (line_table, node, exp_loc, n_tokens);
   19536       200916 :       if (!macro)
   19537              :         /* We shouldn't run out of locations, as we checked that we
   19538              :            had enough before starting.  */
   19539              :         break;
   19540       200916 :       gcc_checking_assert (MAP_START_LOCATION (macro)
   19541              :                            == offset + macro_locs.first);
   19542              : 
   19543       200916 :       location_t *locs = macro->macro_locations;
   19544       200916 :       location_t tok_loc = UNKNOWN_LOCATION;
   19545       200916 :       unsigned count = sec.u ();
   19546       200916 :       unsigned runs = 0;
   19547      7376092 :       for (unsigned jx = macro->n_tokens * 2; jx-- && !sec.get_overrun ();)
   19548              :         {
   19549     10970513 :           while (!count-- && !sec.get_overrun ())
   19550              :             {
   19551      3795337 :               runs++;
   19552      3795337 :               tok_loc = read_location (sec);
   19553      3795337 :               count = sec.u ();
   19554              :             }
   19555      7175176 :           locs[jx] = tok_loc;
   19556              :         }
   19557       200916 :       if (count)
   19558            0 :         sec.set_overrun ();
   19559       200946 :       dump (dumper::LOCATION)
   19560            0 :         && dump ("Macro:%K %I %u/%u*2 locations [%K,%K)",
   19561              :                  ix, identifier (node), runs, n_tokens,
   19562              :                  MAP_START_LOCATION (macro),
   19563            0 :                  MAP_START_LOCATION (macro) + n_tokens);
   19564              :     }
   19565              : 
   19566          140 :   dump () && dump ("Macro location lwm:%K", macro_locs.first);
   19567          131 :   if (propagated)
   19568            3 :     spans.close ();
   19569              : 
   19570          131 :   dump.outdent ();
   19571          131 :   if (!sec.end (from ()))
   19572              :     return false;
   19573              : 
   19574              :   return true;
   19575          131 : }
   19576              : 
   19577              : /* Serialize the definition of MACRO.  */
   19578              : 
   19579              : void
   19580        73799 : module_state::write_define (bytes_out &sec, const cpp_macro *macro)
   19581              : {
   19582        73799 :   sec.u (macro->count);
   19583              : 
   19584        73799 :   bytes_out::bits_out bits = sec.stream_bits ();
   19585        73799 :   bits.b (macro->fun_like);
   19586        73799 :   bits.b (macro->variadic);
   19587        73799 :   bits.b (macro->syshdr);
   19588        73799 :   bits.bflush ();
   19589              : 
   19590        73799 :   write_location (sec, macro->line);
   19591        73799 :   if (macro->fun_like)
   19592              :     {
   19593         9574 :       sec.u (macro->paramc);
   19594         9574 :       const cpp_hashnode *const *parms = macro->parm.params;
   19595        24117 :       for (unsigned ix = 0; ix != macro->paramc; ix++)
   19596        14543 :         sec.cpp_node (parms[ix]);
   19597              :     }
   19598              : 
   19599              :   unsigned len = 0;
   19600       240402 :   for (unsigned ix = 0; ix != macro->count; ix++)
   19601              :     {
   19602       166603 :       const cpp_token *token = &macro->exp.tokens[ix];
   19603       166603 :       write_location (sec, token->src_loc);
   19604       166603 :       sec.u (token->type);
   19605       166603 :       sec.u (token->flags);
   19606       166603 :       switch (cpp_token_val_index (token))
   19607              :         {
   19608            0 :         default:
   19609            0 :           gcc_unreachable ();
   19610              : 
   19611        12815 :         case CPP_TOKEN_FLD_ARG_NO:
   19612              :           /* An argument reference.  */
   19613        12815 :           sec.u (token->val.macro_arg.arg_no);
   19614        12815 :           sec.cpp_node (token->val.macro_arg.spelling);
   19615        12815 :           break;
   19616              : 
   19617        32960 :         case CPP_TOKEN_FLD_NODE:
   19618              :           /* An identifier.  */
   19619        32960 :           sec.cpp_node (token->val.node.node);
   19620        32960 :           if (token->val.node.spelling == token->val.node.node)
   19621              :             /* The spelling will usually be the same.  so optimize
   19622              :                that.  */
   19623        32960 :             sec.str (NULL, 0);
   19624              :           else
   19625            0 :             sec.cpp_node (token->val.node.spelling);
   19626              :           break;
   19627              : 
   19628              :         case CPP_TOKEN_FLD_NONE:
   19629              :           break;
   19630              : 
   19631        52434 :         case CPP_TOKEN_FLD_STR:
   19632              :           /* A string, number or comment.  Not always NUL terminated,
   19633              :              we stream out in a single contatenation with embedded
   19634              :              NULs as that's a safe default.  */
   19635        52434 :           len += token->val.str.len + 1;
   19636        52434 :           sec.u (token->val.str.len);
   19637        52434 :           break;
   19638              : 
   19639            0 :         case CPP_TOKEN_FLD_SOURCE:
   19640            0 :         case CPP_TOKEN_FLD_TOKEN_NO:
   19641            0 :         case CPP_TOKEN_FLD_PRAGMA:
   19642              :           /* These do not occur inside a macro itself.  */
   19643            0 :           gcc_unreachable ();
   19644              :         }
   19645              :     }
   19646              : 
   19647        73799 :   if (len)
   19648              :     {
   19649        48685 :       char *ptr = reinterpret_cast<char *> (sec.buf (len));
   19650        48685 :       len = 0;
   19651       147959 :       for (unsigned ix = 0; ix != macro->count; ix++)
   19652              :         {
   19653        99274 :           const cpp_token *token = &macro->exp.tokens[ix];
   19654        99274 :           if (cpp_token_val_index (token) == CPP_TOKEN_FLD_STR)
   19655              :             {
   19656        52434 :               memcpy (ptr + len, token->val.str.text,
   19657        52434 :                       token->val.str.len);
   19658        52434 :               len += token->val.str.len;
   19659        52434 :               ptr[len++] = 0;
   19660              :             }
   19661              :         }
   19662              :     }
   19663        73799 : }
   19664              : 
   19665              : /* Read a macro definition.  */
   19666              : 
   19667              : cpp_macro *
   19668          650 : module_state::read_define (bytes_in &sec, cpp_reader *reader) const
   19669              : {
   19670          650 :   unsigned count = sec.u ();
   19671              :   /* We rely on knowing cpp_reader's hash table is ident_hash, and
   19672              :      its subobject allocator is stringpool_ggc_alloc and that is just
   19673              :      a wrapper for ggc_alloc_atomic.  */
   19674          650 :   cpp_macro *macro
   19675         1300 :     = (cpp_macro *)ggc_alloc_atomic (sizeof (cpp_macro)
   19676          650 :                                      + sizeof (cpp_token) * (count - !!count));
   19677          650 :   memset (macro, 0, sizeof (cpp_macro) + sizeof (cpp_token) * (count - !!count));
   19678              : 
   19679          650 :   macro->count = count;
   19680          650 :   macro->kind = cmk_macro;
   19681          650 :   macro->imported_p = true;
   19682              : 
   19683          650 :   bytes_in::bits_in bits = sec.stream_bits ();
   19684          650 :   macro->fun_like = bits.b ();
   19685          650 :   macro->variadic = bits.b ();
   19686          650 :   macro->syshdr = bits.b ();
   19687          650 :   bits.bflush ();
   19688              : 
   19689          650 :   macro->line = read_location (sec);
   19690              : 
   19691          650 :   if (macro->fun_like)
   19692              :     {
   19693           79 :       unsigned paramc = sec.u ();
   19694           79 :       cpp_hashnode **params
   19695           79 :         = (cpp_hashnode **)ggc_alloc_atomic (sizeof (cpp_hashnode *) * paramc);
   19696           79 :       macro->paramc = paramc;
   19697           79 :       macro->parm.params = params;
   19698          169 :       for (unsigned ix = 0; ix != paramc; ix++)
   19699           90 :         params[ix] = sec.cpp_node ();
   19700              :     }
   19701              : 
   19702              :   unsigned len = 0;
   19703         1890 :   for (unsigned ix = 0; ix != count && !sec.get_overrun (); ix++)
   19704              :     {
   19705         1240 :       cpp_token *token = &macro->exp.tokens[ix];
   19706         1240 :       token->src_loc = read_location (sec);
   19707         1240 :       token->type = cpp_ttype (sec.u ());
   19708         1240 :       token->flags = sec.u ();
   19709         1240 :       switch (cpp_token_val_index (token))
   19710              :         {
   19711            0 :         default:
   19712            0 :           sec.set_overrun ();
   19713            0 :           break;
   19714              : 
   19715           73 :         case CPP_TOKEN_FLD_ARG_NO:
   19716              :           /* An argument reference.  */
   19717           73 :           {
   19718           73 :             unsigned arg_no = sec.u ();
   19719           73 :             if (arg_no - 1 >= macro->paramc)
   19720            0 :               sec.set_overrun ();
   19721           73 :             token->val.macro_arg.arg_no = arg_no;
   19722           73 :             token->val.macro_arg.spelling = sec.cpp_node ();
   19723              :           }
   19724           73 :           break;
   19725              : 
   19726          263 :         case CPP_TOKEN_FLD_NODE:
   19727              :           /* An identifier.  */
   19728          263 :           token->val.node.node = sec.cpp_node ();
   19729          263 :           token->val.node.spelling = sec.cpp_node ();
   19730          263 :           if (!token->val.node.spelling)
   19731          263 :             token->val.node.spelling = token->val.node.node;
   19732              :           break;
   19733              : 
   19734              :         case CPP_TOKEN_FLD_NONE:
   19735              :           break;
   19736              : 
   19737          479 :         case CPP_TOKEN_FLD_STR:
   19738              :           /* A string, number or comment.  */
   19739          479 :           token->val.str.len = sec.u ();
   19740          479 :           len += token->val.str.len + 1;
   19741          479 :           break;
   19742              :         }
   19743              :     }
   19744              : 
   19745          650 :   if (len)
   19746          477 :     if (const char *ptr = reinterpret_cast<const char *> (sec.buf (len)))
   19747              :       {
   19748              :         /* There should be a final NUL.  */
   19749          477 :         if (ptr[len-1])
   19750            0 :           sec.set_overrun ();
   19751              :         /* cpp_alloc_token_string will add a final NUL.  */
   19752          477 :         const unsigned char *buf
   19753          477 :           = cpp_alloc_token_string (reader, (const unsigned char *)ptr, len - 1);
   19754          477 :         len = 0;
   19755         1267 :         for (unsigned ix = 0; ix != count && !sec.get_overrun (); ix++)
   19756              :           {
   19757          790 :             cpp_token *token = &macro->exp.tokens[ix];
   19758          790 :             if (cpp_token_val_index (token) == CPP_TOKEN_FLD_STR)
   19759              :               {
   19760          479 :                 token->val.str.text = buf + len;
   19761          479 :                 len += token->val.str.len;
   19762          479 :                 if (buf[len++])
   19763            0 :                   sec.set_overrun ();
   19764              :               }
   19765              :           }
   19766              :       }
   19767              : 
   19768          650 :   if (sec.get_overrun ())
   19769            0 :     return NULL;
   19770              :   return macro;
   19771          650 : }
   19772              : 
   19773              : /* Exported macro data.  */
   19774              : struct GTY(()) macro_export {
   19775              :   cpp_macro *def;
   19776              :   location_t undef_loc;
   19777              : 
   19778       104169 :   macro_export ()
   19779       104169 :     :def (NULL), undef_loc (UNKNOWN_LOCATION)
   19780              :   {
   19781              :   }
   19782              : };
   19783              : 
   19784              : /* Imported macro data.  */
   19785              : class macro_import {
   19786              : public:
   19787              :   struct slot {
   19788              : #if defined (WORDS_BIGENDIAN) && SIZEOF_VOID_P == 8
   19789              :     int offset;
   19790              : #endif
   19791              :     /* We need to ensure we don't use the LSB for representation, as
   19792              :        that's the union discriminator below.  */
   19793              :     unsigned bits;
   19794              : 
   19795              : #if !(defined (WORDS_BIGENDIAN) && SIZEOF_VOID_P == 8)
   19796              :     int offset;
   19797              : #endif
   19798              : 
   19799              :   public:
   19800              :     enum Layout {
   19801              :       L_DEF = 1,
   19802              :       L_UNDEF = 2,
   19803              :       L_BOTH = 3,
   19804              :       L_MODULE_SHIFT = 2
   19805              :     };
   19806              : 
   19807              :   public:
   19808              :     /* Not a regular ctor, because we put it in a union, and that's
   19809              :        not allowed in C++ 98.  */
   19810       185949 :     static slot ctor (unsigned module, unsigned defness)
   19811              :     {
   19812       185949 :       gcc_checking_assert (defness);
   19813       185949 :       slot s;
   19814       185949 :       s.bits = defness | (module << L_MODULE_SHIFT);
   19815       185949 :       s.offset = -1;
   19816       185949 :       return s;
   19817              :     }
   19818              : 
   19819              :   public:
   19820       150107 :     unsigned get_defness () const
   19821              :     {
   19822       150107 :       return bits & L_BOTH;
   19823              :     }
   19824       106242 :     unsigned get_module () const
   19825              :     {
   19826       106242 :       return bits >> L_MODULE_SHIFT;
   19827              :     }
   19828           12 :     void become_undef ()
   19829              :     {
   19830           12 :       bits &= ~unsigned (L_DEF);
   19831           12 :       bits |= unsigned (L_UNDEF);
   19832              :     }
   19833              :   };
   19834              : 
   19835              : private:
   19836              :   typedef vec<slot, va_heap, vl_embed> ary_t;
   19837              :   union either {
   19838              :     /* Discriminated by bits 0|1 != 0.  The expected case is that
   19839              :        there will be exactly one slot per macro, hence the effort of
   19840              :        packing that.  */
   19841              :     ary_t *ary;
   19842              :     slot single;
   19843              :   } u;
   19844              : 
   19845              : public:
   19846       151425 :   macro_import ()
   19847       151425 :   {
   19848       151425 :     u.ary = NULL;
   19849              :   }
   19850              : 
   19851              : private:
   19852      8129038 :   bool single_p () const
   19853              :   {
   19854      8129038 :     return u.single.bits & slot::L_BOTH;
   19855              :   }
   19856      8280472 :   bool occupied_p () const
   19857              :   {
   19858      8280472 :     return u.ary != NULL;
   19859              :   }
   19860              : 
   19861              : public:
   19862         1908 :   unsigned length () const
   19863              :   {
   19864         1908 :     gcc_checking_assert (occupied_p ());
   19865         1908 :     return single_p () ? 1 : u.ary->length ();
   19866              :   }
   19867      7989093 :   slot &operator[] (unsigned ix)
   19868              :   {
   19869      7989093 :     gcc_checking_assert (occupied_p ());
   19870      7989093 :     if (single_p ())
   19871              :       {
   19872      7902429 :         gcc_checking_assert (!ix);
   19873      7902429 :         return u.single;
   19874              :       }
   19875              :     else
   19876        86664 :       return (*u.ary)[ix];
   19877              :   }
   19878              : 
   19879              : public:
   19880              :   slot &exported ();
   19881              :   slot &append (unsigned module, unsigned defness);
   19882              : };
   19883              : 
   19884              : /* O is a new import to append to the list for.  If we're an empty
   19885              :    set, initialize us.  */
   19886              : 
   19887              : macro_import::slot &
   19888       185949 : macro_import::append (unsigned module, unsigned defness)
   19889              : {
   19890       185949 :   if (!occupied_p ())
   19891              :     {
   19892       151425 :       u.single = slot::ctor (module, defness);
   19893       151425 :       return u.single;
   19894              :     }
   19895              :   else
   19896              :     {
   19897        34524 :       bool single = single_p ();
   19898        34524 :       ary_t *m = single ? NULL : u.ary;
   19899        34524 :       vec_safe_reserve (m, 1 + single);
   19900        34524 :       if (single)
   19901        34521 :         m->quick_push (u.single);
   19902        34524 :       u.ary = m;
   19903        34524 :       return *u.ary->quick_push (slot::ctor (module, defness));
   19904              :     }
   19905              : }
   19906              : 
   19907              : /* We're going to export something.  Make sure the first import slot
   19908              :    is us.  */
   19909              : 
   19910              : macro_import::slot &
   19911       103522 : macro_import::exported ()
   19912              : {
   19913       103522 :   if (occupied_p () && !(*this)[0].get_module ())
   19914              :     {
   19915            9 :       slot &res = (*this)[0];
   19916            9 :       res.bits |= slot::L_DEF;
   19917            9 :       return res;
   19918              :     }
   19919              : 
   19920       103513 :   slot *a = &append (0, slot::L_DEF);
   19921       103513 :   if (!single_p ())
   19922              :     {
   19923        30262 :       slot &f = (*this)[0];
   19924        30262 :       std::swap (f, *a);
   19925        30262 :       a = &f;
   19926              :     }
   19927              :   return *a;
   19928              : }
   19929              : 
   19930              : /* The import (&exported) macros.  cpp_hasnode's deferred field
   19931              :    indexes this array (offset by 1, so zero means 'not present'.  */
   19932              : 
   19933              : static vec<macro_import, va_heap, vl_embed> *macro_imports;
   19934              : 
   19935              : /* The exported macros.  A macro_import slot's zeroth element's offset
   19936              :    indexes this array.  If the zeroth slot is not for module zero,
   19937              :    there is no export.  */
   19938              : 
   19939              : static GTY(()) vec<macro_export, va_gc> *macro_exports;
   19940              : 
   19941              : /* The reachable set of header imports from this TU.  */
   19942              : 
   19943              : static GTY(()) bitmap headers;
   19944              : 
   19945              : /* Get the (possibly empty) macro imports for NODE.  */
   19946              : 
   19947              : static macro_import &
   19948       155696 : get_macro_imports (cpp_hashnode *node)
   19949              : {
   19950       155696 :   if (node->deferred)
   19951         4271 :     return (*macro_imports)[node->deferred - 1];
   19952              : 
   19953       151425 :   vec_safe_reserve (macro_imports, 1);
   19954       151425 :   node->deferred = macro_imports->length () + 1;
   19955       151425 :   return *vec_safe_push (macro_imports, macro_import ());
   19956              : }
   19957              : 
   19958              : /* Get the macro export for export EXP of NODE.  */
   19959              : 
   19960              : static macro_export &
   19961       103522 : get_macro_export (macro_import::slot &slot)
   19962              : {
   19963       103522 :   if (slot.offset >= 0)
   19964            9 :     return (*macro_exports)[slot.offset];
   19965              : 
   19966       103513 :   vec_safe_reserve (macro_exports, 1);
   19967       103513 :   slot.offset = macro_exports->length ();
   19968       103513 :   return *macro_exports->quick_push (macro_export ());
   19969              : }
   19970              : 
   19971              : /* If NODE is an exportable macro, add it to the export set.  */
   19972              : 
   19973              : static int
   19974      3887935 : maybe_add_macro (cpp_reader *, cpp_hashnode *node, void *data_)
   19975              : {
   19976      3887935 :   bool exporting = false;
   19977              : 
   19978      3887935 :   if (cpp_user_macro_p (node))
   19979       499280 :     if (cpp_macro *macro = node->value.macro)
   19980              :       /* Ignore imported, builtins, command line and forced header macros.  */
   19981       498844 :       if (!macro->imported_p
   19982       498844 :           && !macro->lazy && macro->line >= spans.main_start ())
   19983              :         {
   19984        73260 :           gcc_checking_assert (macro->kind == cmk_macro);
   19985              :           /* I don't want to deal with this corner case, that I suspect is
   19986              :              a devil's advocate reading of the standard.  */
   19987        73260 :           gcc_checking_assert (!macro->extra_tokens);
   19988              : 
   19989        73260 :           macro_import::slot &slot = get_macro_imports (node).exported ();
   19990        73260 :           macro_export &exp = get_macro_export (slot);
   19991        73260 :           exp.def = macro;
   19992        73260 :           exporting = true;
   19993              :         }
   19994              : 
   19995      3814675 :   if (!exporting && node->deferred)
   19996              :     {
   19997          577 :       macro_import &imports = (*macro_imports)[node->deferred - 1];
   19998          577 :       macro_import::slot &slot = imports[0];
   19999          577 :       if (!slot.get_module ())
   20000              :         {
   20001          546 :           gcc_checking_assert (slot.get_defness ());
   20002              :           exporting = true;
   20003              :         }
   20004              :     }
   20005              : 
   20006        73260 :   if (exporting)
   20007        73806 :     static_cast<vec<cpp_hashnode *> *> (data_)->safe_push (node);
   20008              : 
   20009      3887935 :   return 1; /* Don't stop.  */
   20010              : }
   20011              : 
   20012              : /* Order cpp_hashnodes A_ and B_ by their exported macro locations.  */
   20013              : 
   20014              : static int
   20015      3852176 : macro_loc_cmp (const void *a_, const void *b_)
   20016              : {
   20017      3852176 :   const cpp_hashnode *node_a = *(const cpp_hashnode *const *)a_;
   20018      3852176 :   macro_import &import_a = (*macro_imports)[node_a->deferred - 1];
   20019      3852176 :   const macro_export &export_a = (*macro_exports)[import_a[0].offset];
   20020      3852176 :   location_t loc_a = export_a.def ? export_a.def->line : export_a.undef_loc;
   20021              : 
   20022      3852176 :   const cpp_hashnode *node_b = *(const cpp_hashnode *const *)b_;
   20023      3852176 :   macro_import &import_b = (*macro_imports)[node_b->deferred - 1];
   20024      3852176 :   const macro_export &export_b = (*macro_exports)[import_b[0].offset];
   20025      3852176 :   location_t loc_b = export_b.def ? export_b.def->line : export_b.undef_loc;
   20026              : 
   20027      3852176 :   if (loc_a < loc_b)
   20028              :     return +1;
   20029      1979256 :   else if (loc_a > loc_b)
   20030              :     return -1;
   20031              :   else
   20032            0 :     return 0;
   20033              : }
   20034              : 
   20035              : /* Gather the macro definitions and undefinitions that we will need to
   20036              :    write out.   */
   20037              : 
   20038              : vec<cpp_hashnode *> *
   20039          889 : module_state::prepare_macros (cpp_reader *reader)
   20040              : {
   20041          889 :   vec<cpp_hashnode *> *macros;
   20042          889 :   vec_alloc (macros, 100);
   20043              : 
   20044          889 :   cpp_forall_identifiers (reader, maybe_add_macro, macros);
   20045              : 
   20046          913 :   dump (dumper::MACRO) && dump ("No more than %u macros", macros->length ());
   20047              : 
   20048          889 :   macros->qsort (macro_loc_cmp);
   20049              : 
   20050              :   // Note the locations.
   20051        75584 :   for (unsigned ix = macros->length (); ix--;)
   20052              :     {
   20053        73806 :       cpp_hashnode *node = (*macros)[ix];
   20054        73806 :       macro_import::slot &slot = (*macro_imports)[node->deferred - 1][0];
   20055        73806 :       macro_export &mac = (*macro_exports)[slot.offset];
   20056              : 
   20057        73806 :       if (IDENTIFIER_KEYWORD_P (identifier (node)))
   20058            1 :         continue;
   20059              : 
   20060        73805 :       if (mac.undef_loc != UNKNOWN_LOCATION)
   20061           12 :         note_location (mac.undef_loc);
   20062        73805 :       if (mac.def)
   20063              :         {
   20064        73799 :           note_location (mac.def->line);
   20065       240402 :           for (unsigned ix = 0; ix != mac.def->count; ix++)
   20066       166603 :             note_location (mac.def->exp.tokens[ix].src_loc);
   20067              :         }
   20068              :     }
   20069              : 
   20070          889 :   return macros;
   20071              : }
   20072              : 
   20073              : /* Write out the exported defines.  This is two sections, one
   20074              :    containing the definitions, the other a table of node names.  */
   20075              : 
   20076              : unsigned
   20077          889 : module_state::write_macros (elf_out *to, vec<cpp_hashnode *> *macros,
   20078              :                             unsigned *crc_p)
   20079              : {
   20080          956 :   dump () && dump ("Writing macros");
   20081          889 :   dump.indent ();
   20082              : 
   20083              :   /* Write the defs */
   20084          889 :   bytes_out sec (to);
   20085          889 :   sec.begin ();
   20086              : 
   20087          889 :   unsigned count = 0;
   20088        75584 :   for (unsigned ix = macros->length (); ix--;)
   20089              :     {
   20090        73806 :       cpp_hashnode *node = (*macros)[ix];
   20091        73806 :       macro_import::slot &slot = (*macro_imports)[node->deferred - 1][0];
   20092        73806 :       gcc_assert (!slot.get_module () && slot.get_defness ());
   20093              : 
   20094        73806 :       macro_export &mac = (*macro_exports)[slot.offset];
   20095        73806 :       gcc_assert (!!(slot.get_defness () & macro_import::slot::L_UNDEF)
   20096              :                   == (mac.undef_loc != UNKNOWN_LOCATION)
   20097              :                   && !!(slot.get_defness () & macro_import::slot::L_DEF)
   20098              :                   == (mac.def != NULL));
   20099              : 
   20100        73806 :       if (IDENTIFIER_KEYWORD_P (identifier (node)))
   20101              :         {
   20102            1 :           warning_at (mac.def->line, 0,
   20103              :                       "not exporting %<#define %E%> as it is a keyword",
   20104              :                       identifier (node));
   20105            1 :           slot.offset = 0;
   20106            1 :           continue;
   20107              :         }
   20108              : 
   20109        73805 :       count++;
   20110        73805 :       slot.offset = sec.pos;
   20111        73805 :       dump (dumper::MACRO)
   20112           24 :         && dump ("Writing macro %s%s%s %I at %u",
   20113           24 :                  slot.get_defness () & macro_import::slot::L_UNDEF
   20114              :                  ? "#undef" : "",
   20115           24 :                  slot.get_defness () == macro_import::slot::L_BOTH
   20116              :                  ? " & " : "",
   20117           24 :                  slot.get_defness () & macro_import::slot::L_DEF
   20118              :                  ? "#define" : "",
   20119              :                  identifier (node), slot.offset);
   20120        73805 :       if (mac.undef_loc != UNKNOWN_LOCATION)
   20121           12 :         write_location (sec, mac.undef_loc);
   20122        73805 :       if (mac.def)
   20123        73799 :         write_define (sec, mac.def);
   20124              :     }
   20125          889 :   if (count)
   20126              :     // We may have ended on a tokenless macro with a very short
   20127              :     // location, that will cause problems reading its bit flags.
   20128          144 :     sec.u (0);
   20129          889 :   sec.end (to, to->name (MOD_SNAME_PFX ".def"), crc_p);
   20130              : 
   20131          889 :   if (count)
   20132              :     {
   20133              :       /* Write the table.  */
   20134          144 :       bytes_out sec (to);
   20135          144 :       sec.begin ();
   20136          144 :       sec.u (count);
   20137              : 
   20138        74093 :       for (unsigned ix = macros->length (); ix--;)
   20139              :         {
   20140        73805 :           const cpp_hashnode *node = (*macros)[ix];
   20141        73805 :           macro_import::slot &slot = (*macro_imports)[node->deferred - 1][0];
   20142              : 
   20143        73805 :           if (slot.offset)
   20144              :             {
   20145        73805 :               sec.cpp_node (node);
   20146        73805 :               sec.u (slot.get_defness ());
   20147        73805 :               sec.u (slot.offset);
   20148              :             }
   20149              :         }
   20150          144 :       sec.end (to, to->name (MOD_SNAME_PFX ".mac"), crc_p);
   20151          144 :     }
   20152              : 
   20153          889 :   dump.outdent ();
   20154          889 :   return count;
   20155          889 : }
   20156              : 
   20157              : bool
   20158          921 : module_state::read_macros ()
   20159              : {
   20160              :   /* Get the def section.  */
   20161          921 :   if (!slurp->macro_defs.begin (loc, from (), MOD_SNAME_PFX ".def"))
   20162              :     return false;
   20163              : 
   20164              :   /* Get the tbl section, if there are defs. */
   20165          921 :   if (slurp->macro_defs.more_p ()
   20166          921 :       && !slurp->macro_tbl.begin (loc, from (), MOD_SNAME_PFX ".mac"))
   20167              :     return false;
   20168              : 
   20169              :   return true;
   20170              : }
   20171              : 
   20172              : /* Install the macro name table.  */
   20173              : 
   20174              : void
   20175          927 : module_state::install_macros ()
   20176              : {
   20177          927 :   bytes_in &sec = slurp->macro_tbl;
   20178          927 :   if (!sec.size)
   20179              :     return;
   20180              : 
   20181          203 :   dump () && dump ("Reading macro table %M", this);
   20182          181 :   dump.indent ();
   20183              : 
   20184          181 :   unsigned count = sec.u ();
   20185          203 :   dump () && dump ("%u macros", count);
   20186        82617 :   while (count--)
   20187              :     {
   20188        82436 :       cpp_hashnode *node = sec.cpp_node ();
   20189        82436 :       macro_import &imp = get_macro_imports (node);
   20190        82436 :       unsigned flags = sec.u () & macro_import::slot::L_BOTH;
   20191        82436 :       if (!flags)
   20192            0 :         sec.set_overrun ();
   20193              : 
   20194        82436 :       if (sec.get_overrun ())
   20195              :         break;
   20196              : 
   20197        82436 :       macro_import::slot &slot = imp.append (mod, flags);
   20198        82436 :       slot.offset = sec.u ();
   20199              : 
   20200        82436 :       dump (dumper::MACRO)
   20201           84 :         && dump ("Read %s macro %s%s%s %I at %u",
   20202           30 :                  imp.length () > 1 ? "add" : "new",
   20203           27 :                  flags & macro_import::slot::L_UNDEF ? "#undef" : "",
   20204              :                  flags == macro_import::slot::L_BOTH ? " & " : "",
   20205           30 :                  flags & macro_import::slot::L_DEF ? "#define" : "",
   20206              :                  identifier (node), slot.offset);
   20207              : 
   20208              :       /* We'll leak an imported definition's TOKEN_FLD_STR's data
   20209              :          here.  But that only happens when we've had to resolve the
   20210              :          deferred macro before this import -- why are you doing
   20211              :          that?  */
   20212        82436 :       if (cpp_macro *cur = cpp_set_deferred_macro (node))
   20213        30250 :         if (!cur->imported_p)
   20214              :           {
   20215        30250 :             macro_import::slot &slot = imp.exported ();
   20216        30250 :             macro_export &exp = get_macro_export (slot);
   20217        30250 :             exp.def = cur;
   20218       112867 :             dump (dumper::MACRO)
   20219            0 :               && dump ("Saving current #define %I", identifier (node));
   20220              :           }
   20221              :     }
   20222              : 
   20223              :   /* We're now done with the table.  */
   20224          181 :   elf_in::release (slurp->from, sec);
   20225              : 
   20226          181 :   dump.outdent ();
   20227              : }
   20228              : 
   20229              : /* Import the transitive macros.  */
   20230              : 
   20231              : void
   20232          885 : module_state::import_macros ()
   20233              : {
   20234          885 :   bitmap_ior_into (headers, slurp->headers);
   20235              : 
   20236          885 :   bitmap_iterator bititer;
   20237          885 :   unsigned bitnum;
   20238         1812 :   EXECUTE_IF_SET_IN_BITMAP (slurp->headers, 0, bitnum, bititer)
   20239          927 :     (*modules)[bitnum]->install_macros ();
   20240          885 : }
   20241              : 
   20242              : /* NODE is being undefined at LOC.  Record it in the export table, if
   20243              :    necessary.  */
   20244              : 
   20245              : void
   20246       268184 : module_state::undef_macro (cpp_reader *, location_t loc, cpp_hashnode *node)
   20247              : {
   20248       268184 :   if (!node->deferred)
   20249              :     /* The macro is not imported, so our undef is irrelevant.  */
   20250              :     return;
   20251              : 
   20252           12 :   unsigned n = dump.push (NULL);
   20253              : 
   20254           12 :   macro_import::slot &slot = (*macro_imports)[node->deferred - 1].exported ();
   20255           12 :   macro_export &exp = get_macro_export (slot);
   20256              : 
   20257           12 :   exp.undef_loc = loc;
   20258           12 :   slot.become_undef ();
   20259           12 :   exp.def = NULL;
   20260              : 
   20261           18 :   dump (dumper::MACRO) && dump ("Recording macro #undef %I", identifier (node));
   20262              : 
   20263           12 :   dump.pop (n);
   20264              : }
   20265              : 
   20266              : /* NODE is a deferred macro node.  Determine the definition and return
   20267              :    it, with NULL if undefined.  May issue diagnostics.
   20268              : 
   20269              :    This can leak memory, when merging declarations -- the string
   20270              :    contents (TOKEN_FLD_STR) of each definition are allocated in
   20271              :    unreclaimable cpp objstack.  Only one will win.  However, I do not
   20272              :    expect this to be common -- mostly macros have a single point of
   20273              :    definition.  Perhaps we could restore the objstack to its position
   20274              :    after the first imported definition (if that wins)?  The macros
   20275              :    themselves are GC'd.  */
   20276              : 
   20277              : cpp_macro *
   20278          626 : module_state::deferred_macro (cpp_reader *reader, location_t loc,
   20279              :                               cpp_hashnode *node)
   20280              : {
   20281          626 :   macro_import &imports = (*macro_imports)[node->deferred - 1];
   20282              : 
   20283          626 :   unsigned n = dump.push (NULL);
   20284          632 :   dump (dumper::MACRO) && dump ("Deferred macro %I", identifier (node));
   20285              : 
   20286          626 :   bitmap visible (BITMAP_GGC_ALLOC ());
   20287              : 
   20288          626 :   if (!((imports[0].get_defness () & macro_import::slot::L_UNDEF)
   20289            0 :         && !imports[0].get_module ()))
   20290              :     {
   20291              :       /* Calculate the set of visible header imports.  */
   20292          626 :       bitmap_copy (visible, headers);
   20293         1420 :       for (unsigned ix = imports.length (); ix--;)
   20294              :         {
   20295          794 :           const macro_import::slot &slot = imports[ix];
   20296          794 :           unsigned mod = slot.get_module ();
   20297          794 :           if ((slot.get_defness () & macro_import::slot::L_UNDEF)
   20298          794 :               && bitmap_bit_p (visible, mod))
   20299              :             {
   20300           12 :               bitmap arg = mod ? (*modules)[mod]->slurp->headers : headers;
   20301           12 :               bitmap_and_compl_into (visible, arg);
   20302           12 :               bitmap_set_bit (visible, mod);
   20303              :             }
   20304              :         }
   20305              :     }
   20306          626 :   bitmap_set_bit (visible, 0);
   20307              : 
   20308              :   /* Now find the macros that are still visible.  */
   20309          626 :   bool failed = false;
   20310          626 :   cpp_macro *def = NULL;
   20311          626 :   vec<macro_export> defs;
   20312          626 :   defs.create (imports.length ());
   20313         1420 :   for (unsigned ix = imports.length (); ix--;)
   20314              :     {
   20315          794 :       const macro_import::slot &slot = imports[ix];
   20316          794 :       unsigned mod = slot.get_module ();
   20317          794 :       if (bitmap_bit_p (visible, mod))
   20318              :         {
   20319          782 :           macro_export *pushed = NULL;
   20320          782 :           if (mod)
   20321              :             {
   20322          656 :               const module_state *imp = (*modules)[mod];
   20323          656 :               bytes_in &sec = imp->slurp->macro_defs;
   20324          656 :               if (!sec.get_overrun ())
   20325              :                 {
   20326          656 :                   dump (dumper::MACRO)
   20327            6 :                     && dump ("Reading macro %s%s%s %I module %M at %u",
   20328            6 :                              slot.get_defness () & macro_import::slot::L_UNDEF
   20329              :                              ? "#undef" : "",
   20330            6 :                              slot.get_defness () == macro_import::slot::L_BOTH
   20331              :                              ? " & " : "",
   20332            6 :                              slot.get_defness () & macro_import::slot::L_DEF
   20333              :                              ? "#define" : "",
   20334            6 :                              identifier (node), imp, slot.offset);
   20335          656 :                   sec.random_access (slot.offset);
   20336              : 
   20337          656 :                   macro_export exp;
   20338          656 :                   if (slot.get_defness () & macro_import::slot::L_UNDEF)
   20339           12 :                     exp.undef_loc = imp->read_location (sec);
   20340          656 :                   if (slot.get_defness () & macro_import::slot::L_DEF)
   20341          650 :                     exp.def = imp->read_define (sec, reader);
   20342          656 :                   if (sec.get_overrun ())
   20343            0 :                     error_at (loc, "macro definitions of %qE corrupted",
   20344            0 :                               imp->name);
   20345              :                   else
   20346          656 :                     pushed = defs.quick_push (exp);
   20347              :                 }
   20348              :             }
   20349              :           else
   20350          126 :             pushed = defs.quick_push ((*macro_exports)[slot.offset]);
   20351          782 :           if (pushed && pushed->def)
   20352              :             {
   20353          776 :               if (!def)
   20354              :                 def = pushed->def;
   20355          153 :               else if (cpp_compare_macros (def, pushed->def))
   20356          794 :                 failed = true;
   20357              :             }
   20358              :         }
   20359              :     }
   20360              : 
   20361          626 :   if (failed)
   20362              :     {
   20363              :       /* If LOC is the first loc, this is the end of file check, which
   20364              :          is a warning.  */
   20365           15 :       auto_diagnostic_group d;
   20366           15 :       if (loc == MAP_START_LOCATION (LINEMAPS_ORDINARY_MAP_AT (line_table, 0)))
   20367            9 :         warning_at (loc, OPT_Winvalid_imported_macros,
   20368              :                     "inconsistent imported macro definition %qE",
   20369              :                     identifier (node));
   20370              :       else
   20371            6 :         error_at (loc, "inconsistent imported macro definition %qE",
   20372              :                   identifier (node));
   20373           60 :       for (unsigned ix = defs.length (); ix--;)
   20374              :         {
   20375           30 :           macro_export &exp = defs[ix];
   20376           30 :           if (exp.undef_loc)
   20377            0 :             inform (exp.undef_loc, "%<#undef %E%>", identifier (node));
   20378           30 :           if (exp.def)
   20379           30 :             inform (exp.def->line, "%<#define %s%>",
   20380              :                     cpp_macro_definition (reader, node, exp.def));
   20381              :         }
   20382           15 :       def = NULL;
   20383           15 :     }
   20384              : 
   20385          626 :   defs.release ();
   20386              : 
   20387          626 :   dump.pop (n);
   20388              : 
   20389          626 :   return def;
   20390              : }
   20391              : 
   20392              : /* Stream the static aggregates.  Sadly some headers (ahem:
   20393              :    iostream) contain static vars, and rely on them to run global
   20394              :    ctors.  */
   20395              : unsigned
   20396          889 : module_state::write_inits (elf_out *to, depset::hash &table, unsigned *crc_ptr)
   20397              : {
   20398          889 :   if (!static_aggregates && !tls_aggregates)
   20399              :     return 0;
   20400              : 
   20401           45 :   dump () && dump ("Writing initializers");
   20402           45 :   dump.indent ();
   20403              : 
   20404           45 :   static_aggregates = nreverse (static_aggregates);
   20405           45 :   tls_aggregates = nreverse (tls_aggregates);
   20406              : 
   20407           45 :   unsigned count = 0;
   20408           45 :   trees_out sec (to, this, table, ~0u);
   20409           45 :   sec.begin ();
   20410              : 
   20411           45 :   tree list = static_aggregates;
   20412          135 :   for (int passes = 0; passes != 2; passes++)
   20413              :     {
   20414          258 :       for (tree init = list; init; init = TREE_CHAIN (init))
   20415          168 :         if (TREE_LANG_FLAG_0 (init))
   20416              :           {
   20417          144 :             if (STATIC_INIT_DECOMP_BASE_P (init))
   20418              :               {
   20419              :                 /* Ensure that in the returned result chain if the
   20420              :                    STATIC_INIT_DECOMP_*BASE_P flags are set, there is
   20421              :                    always one or more STATIC_INIT_DECOMP_BASE_P TREE_LIST
   20422              :                    followed by one or more STATIC_INIT_DECOMP_NONBASE_P.  */
   20423           21 :                 int phase = 0;
   20424           21 :                 tree last = NULL_TREE;
   20425           21 :                 for (tree init2 = TREE_CHAIN (init);
   20426          102 :                      init2; init2 = TREE_CHAIN (init2))
   20427              :                   {
   20428          123 :                     if (phase == 0 && STATIC_INIT_DECOMP_BASE_P (init2))
   20429              :                       ;
   20430          102 :                     else if (phase == 0
   20431          123 :                              && STATIC_INIT_DECOMP_NONBASE_P (init2))
   20432              :                       {
   20433          102 :                         phase = TREE_LANG_FLAG_0 (init2) ? 2 : 1;
   20434              :                         last = init2;
   20435              :                       }
   20436           81 :                     else if (IN_RANGE (phase, 1, 2)
   20437          162 :                              && STATIC_INIT_DECOMP_NONBASE_P (init2))
   20438              :                       {
   20439           60 :                         if (TREE_LANG_FLAG_0 (init2))
   20440           81 :                           phase = 2;
   20441              :                         last = init2;
   20442              :                       }
   20443              :                     else
   20444              :                       break;
   20445              :                   }
   20446           21 :                 if (phase == 2)
   20447              :                   {
   20448              :                     /* In that case, add markers about it so that the
   20449              :                        STATIC_INIT_DECOMP_BASE_P and
   20450              :                        STATIC_INIT_DECOMP_NONBASE_P flags can be restored.  */
   20451           21 :                     sec.tree_node (build_int_cst (integer_type_node,
   20452           21 :                                                   2 * passes + 1));
   20453           21 :                     phase = 1;
   20454          123 :                     for (tree init2 = init; init2 != TREE_CHAIN (last);
   20455          102 :                          init2 = TREE_CHAIN (init2))
   20456          102 :                       if (TREE_LANG_FLAG_0 (init2))
   20457              :                         {
   20458          102 :                           tree decl = TREE_VALUE (init2);
   20459          102 :                           if (phase == 1
   20460          102 :                               && STATIC_INIT_DECOMP_NONBASE_P (init2))
   20461              :                             {
   20462           21 :                               sec.tree_node (build_int_cst (integer_type_node,
   20463           21 :                                                             2 * passes + 2));
   20464           21 :                               phase = 2;
   20465              :                             }
   20466          102 :                           dump ("Initializer:%u for %N", count, decl);
   20467          102 :                           sec.tree_node (decl);
   20468          102 :                           ++count;
   20469              :                         }
   20470           21 :                     sec.tree_node (integer_zero_node);
   20471           21 :                     init = last;
   20472           21 :                     continue;
   20473           21 :                   }
   20474              :               }
   20475              : 
   20476          123 :             tree decl = TREE_VALUE (init);
   20477              : 
   20478          123 :             dump ("Initializer:%u for %N", count, decl);
   20479          123 :             sec.tree_node (decl);
   20480          123 :             ++count;
   20481              :           }
   20482              : 
   20483           90 :       list = tls_aggregates;
   20484              :     }
   20485              : 
   20486           45 :   sec.end (to, to->name (MOD_SNAME_PFX ".ini"), crc_ptr);
   20487           45 :   dump.outdent ();
   20488              : 
   20489           45 :   return count;
   20490           45 : }
   20491              : 
   20492              : /* We have to defer some post-load processing until we've completed
   20493              :    reading, because they can cause more reading.  */
   20494              : 
   20495              : static void
   20496        12474 : post_load_processing ()
   20497              : {
   20498              :   /* We mustn't cause a GC, our caller should have arranged for that
   20499              :      not to happen.  */
   20500        12474 :   gcc_checking_assert (function_depth);
   20501              : 
   20502        12474 :   if (!post_load_decls)
   20503              :     return;
   20504              : 
   20505         7931 :   tree old_cfd = current_function_decl;
   20506         7931 :   struct function *old_cfun = cfun;
   20507        16796 :   while (post_load_decls->length ())
   20508              :     {
   20509         8865 :       tree decl = post_load_decls->pop ();
   20510              : 
   20511         8920 :       dump () && dump ("Post-load processing of %N", decl);
   20512              : 
   20513         8865 :       gcc_checking_assert (DECL_MAYBE_IN_CHARGE_CDTOR_P (decl));
   20514         8865 :       expand_or_defer_fn (decl);
   20515              :       /* As in module_state::read_cluster.  */
   20516          873 :       if (at_eof && DECL_COMDAT (decl) && DECL_EXTERNAL (decl)
   20517         9132 :           && DECL_NOT_REALLY_EXTERN (decl))
   20518          226 :         DECL_EXTERNAL (decl) = false;
   20519              :     }
   20520              : 
   20521         7931 :   set_cfun (old_cfun);
   20522         7931 :   current_function_decl = old_cfd;
   20523              : }
   20524              : 
   20525              : bool
   20526           45 : module_state::read_inits (unsigned count)
   20527              : {
   20528           45 :   trees_in sec (this);
   20529           45 :   if (!sec.begin (loc, from (), from ()->find (MOD_SNAME_PFX ".ini")))
   20530              :     return false;
   20531           57 :   dump () && dump ("Reading %u initializers", count);
   20532           45 :   dump.indent ();
   20533              : 
   20534           45 :   lazy_snum = ~0u;
   20535           45 :   int decomp_phase = 0;
   20536           45 :   tree *aggrp = NULL;
   20537          270 :   for (unsigned ix = 0; ix != count; ix++)
   20538              :     {
   20539          225 :       tree last = NULL_TREE;
   20540          225 :       if (decomp_phase)
   20541          102 :         last = *aggrp;
   20542              :       /* Merely referencing the decl causes its initializer to be read
   20543              :          and added to the correct list.  */
   20544          225 :       tree decl = sec.tree_node ();
   20545              :       /* module_state::write_inits can add special INTEGER_CST markers in
   20546              :          between the decls.  1 means STATIC_INIT_DECOMP_BASE_P entries
   20547              :          follow in static_aggregates, 2 means STATIC_INIT_DECOMP_NONBASE_P
   20548              :          entries follow in static_aggregates, 3 means
   20549              :          STATIC_INIT_DECOMP_BASE_P entries follow in tls_aggregates,
   20550              :          4 means STATIC_INIT_DECOMP_NONBASE_P follow in tls_aggregates,
   20551              :          0 means end of STATIC_INIT_DECOMP_{,NON}BASE_P sequence.  */
   20552          225 :       if (tree_fits_shwi_p (decl))
   20553              :         {
   20554           63 :           if (sec.get_overrun ())
   20555              :             break;
   20556           63 :           decomp_phase = tree_to_shwi (decl);
   20557           63 :           if (decomp_phase)
   20558              :             {
   20559           42 :               aggrp = decomp_phase > 2 ? &tls_aggregates : &static_aggregates;
   20560              :               last = *aggrp;
   20561              :             }
   20562           63 :           decl = sec.tree_node ();
   20563              :         }
   20564              : 
   20565          225 :       if (sec.get_overrun ())
   20566              :         break;
   20567          225 :       if (decl)
   20568          225 :         dump ("Initializer:%u for %N", ix, decl);
   20569          225 :       if (decomp_phase)
   20570              :         {
   20571          102 :           tree init = *aggrp;
   20572          102 :           gcc_assert (TREE_VALUE (init) == decl && TREE_CHAIN (init) == last);
   20573          102 :           if ((decomp_phase & 1) != 0)
   20574           21 :             STATIC_INIT_DECOMP_BASE_P (init) = 1;
   20575              :           else
   20576           81 :             STATIC_INIT_DECOMP_NONBASE_P (init) = 1;
   20577              :         }
   20578              :     }
   20579           45 :   if (decomp_phase && !sec.get_overrun ())
   20580              :     {
   20581            0 :       tree decl = sec.tree_node ();
   20582            0 :       gcc_assert (integer_zerop (decl));
   20583              :     }
   20584           45 :   lazy_snum = 0;
   20585           45 :   post_load_processing ();
   20586           45 :   dump.outdent ();
   20587           45 :   if (!sec.end (from ()))
   20588              :     return false;
   20589              :   return true;
   20590           45 : }
   20591              : 
   20592              : void
   20593         2713 : module_state::write_counts (elf_out *to, unsigned counts[MSC_HWM],
   20594              :                             unsigned *crc_ptr)
   20595              : {
   20596         2713 :   bytes_out cfg (to);
   20597              : 
   20598         2713 :   cfg.begin ();
   20599              : 
   20600        27130 :   for (unsigned ix = MSC_HWM; ix--;)
   20601        24417 :     cfg.u (counts[ix]);
   20602              : 
   20603         2713 :   if (dump ())
   20604              :     {
   20605          300 :       dump ("Cluster sections are [%u,%u)",
   20606              :             counts[MSC_sec_lwm], counts[MSC_sec_hwm]);
   20607          300 :       dump ("Bindings %u", counts[MSC_bindings]);
   20608          300 :       dump ("Pendings %u", counts[MSC_pendings]);
   20609          300 :       dump ("Entities %u", counts[MSC_entities]);
   20610          300 :       dump ("Namespaces %u", counts[MSC_namespaces]);
   20611          300 :       dump ("Using-directives %u", counts[MSC_using_directives]);
   20612          300 :       dump ("Macros %u", counts[MSC_macros]);
   20613          300 :       dump ("Initializers %u", counts[MSC_inits]);
   20614              :     }
   20615              : 
   20616         2713 :   cfg.end (to, to->name (MOD_SNAME_PFX ".cnt"), crc_ptr);
   20617         2713 : }
   20618              : 
   20619              : bool
   20620         2901 : module_state::read_counts (unsigned counts[MSC_HWM])
   20621              : {
   20622         2901 :   bytes_in cfg;
   20623              : 
   20624         2901 :   if (!cfg.begin (loc, from (), MOD_SNAME_PFX ".cnt"))
   20625              :     return false;
   20626              : 
   20627        29010 :   for (unsigned ix = MSC_HWM; ix--;)
   20628        26109 :     counts[ix] = cfg.u ();
   20629              : 
   20630         2901 :   if (dump ())
   20631              :     {
   20632          529 :       dump ("Declaration sections are [%u,%u)",
   20633              :             counts[MSC_sec_lwm], counts[MSC_sec_hwm]);
   20634          529 :       dump ("Bindings %u", counts[MSC_bindings]);
   20635          529 :       dump ("Pendings %u", counts[MSC_pendings]);
   20636          529 :       dump ("Entities %u", counts[MSC_entities]);
   20637          529 :       dump ("Namespaces %u", counts[MSC_namespaces]);
   20638          529 :       dump ("Using-directives %u", counts[MSC_using_directives]);
   20639          529 :       dump ("Macros %u", counts[MSC_macros]);
   20640          529 :       dump ("Initializers %u", counts[MSC_inits]);
   20641              :     }
   20642              : 
   20643         2901 :   return cfg.end (from ());
   20644         2901 : }
   20645              : 
   20646              : /* Tool configuration:  MOD_SNAME_PFX .config
   20647              : 
   20648              :    This is data that confirms current state (or fails).  */
   20649              : 
   20650              : void
   20651         2713 : module_state::write_config (elf_out *to, module_state_config &config,
   20652              :                             unsigned inner_crc)
   20653              : {
   20654         2713 :   bytes_out cfg (to);
   20655              : 
   20656         2713 :   cfg.begin ();
   20657              : 
   20658              :   /* Write version and inner crc as u32 values, for easier
   20659              :      debug inspection.  */
   20660         3013 :   dump () && dump ("Writing version=%V, inner_crc=%x",
   20661              :                    MODULE_VERSION, inner_crc);
   20662         2713 :   cfg.u32 (unsigned (MODULE_VERSION));
   20663         2713 :   cfg.u32 (inner_crc);
   20664              : 
   20665         2713 :   cfg.u (to->name (is_header () ? "" : get_flatname ()));
   20666              : 
   20667              :   /* Configuration. */
   20668         3013 :   dump () && dump ("Writing target='%s', host='%s'",
   20669              :                    TARGET_MACHINE, HOST_MACHINE);
   20670         2713 :   unsigned target = to->name (TARGET_MACHINE);
   20671         2713 :   unsigned host = (!strcmp (TARGET_MACHINE, HOST_MACHINE)
   20672              :                    ? target : to->name (HOST_MACHINE));
   20673         2713 :   cfg.u (target);
   20674         2713 :   cfg.u (host);
   20675              : 
   20676         2713 :   cfg.str (config.dialect_str);
   20677         2713 :   cfg.u (extensions);
   20678              : 
   20679              :   /* Global tree information.  We write the globals crc separately,
   20680              :      rather than mix it directly into the overall crc, as it is used
   20681              :      to ensure data match between instances of the compiler, not
   20682              :      integrity of the file.  */
   20683         3013 :   dump () && dump ("Writing globals=%u, crc=%x",
   20684              :                    fixed_trees->length (), global_crc);
   20685         2713 :   cfg.u (fixed_trees->length ());
   20686         2713 :   cfg.u32 (global_crc);
   20687              : 
   20688         2713 :   if (is_partition ())
   20689          193 :     cfg.u (is_interface ());
   20690              : 
   20691         2713 :   cfg.u (config.num_imports);
   20692         2713 :   cfg.u (config.num_partitions);
   20693         2713 :   cfg.u (config.num_entities);
   20694              : 
   20695         2713 :   cfg.loc (config.ordinary_locs);
   20696         2713 :   cfg.loc (config.macro_locs);
   20697         2713 :   cfg.u (config.loc_range_bits);
   20698              : 
   20699         2713 :   cfg.u (config.active_init);
   20700              : 
   20701              :   /* Now generate CRC, we'll have incorporated the inner CRC because
   20702              :      of its serialization above.  */
   20703         2713 :   cfg.end (to, to->name (MOD_SNAME_PFX ".cfg"), &crc);
   20704         3013 :   dump () && dump ("Writing CRC=%x", crc);
   20705         2713 : }
   20706              : 
   20707              : void
   20708           40 : module_state::note_cmi_name ()
   20709              : {
   20710           40 :   if (!cmi_noted_p && filename)
   20711              :     {
   20712           40 :       cmi_noted_p = true;
   20713           40 :       inform (loc, "compiled module file is %qs",
   20714              :               maybe_add_cmi_prefix (filename));
   20715              :     }
   20716           40 : }
   20717              : 
   20718              : bool
   20719         3011 : module_state::read_config (module_state_config &config, bool complain)
   20720              : {
   20721         3011 :   bytes_in cfg;
   20722              : 
   20723         3011 :   if (!cfg.begin (loc, from (), MOD_SNAME_PFX ".cfg"))
   20724              :     return false;
   20725              : 
   20726              :   /* Check version.  */
   20727         3011 :   unsigned my_ver = MODULE_VERSION;
   20728         3011 :   unsigned their_ver = cfg.u32 ();
   20729         3543 :   dump () && dump  (my_ver == their_ver ? "Version %V"
   20730              :                     : "Expecting %V found %V", my_ver, their_ver);
   20731         3011 :   if (their_ver != my_ver)
   20732              :     {
   20733              :       /* The compiler versions differ.  Close enough? */
   20734            0 :       verstr_t my_string, their_string;
   20735              : 
   20736            0 :       version2string (my_ver, my_string);
   20737            0 :       version2string (their_ver, their_string);
   20738              : 
   20739              :       /* Reject when either is non-experimental or when experimental
   20740              :          major versions differ.  */
   20741            0 :       auto_diagnostic_group d;
   20742            0 :       bool reject_p = ((!IS_EXPERIMENTAL (my_ver)
   20743              :                         || !IS_EXPERIMENTAL (their_ver)
   20744            0 :                         || MODULE_MAJOR (my_ver) != MODULE_MAJOR (their_ver))
   20745              :                        /* The 'I know what I'm doing' switch.  */
   20746            0 :                        && !flag_module_version_ignore);
   20747            0 :       bool inform_p = true;
   20748            0 :       if (!complain)
   20749              :         inform_p = false;
   20750            0 :       else if (reject_p)
   20751              :         {
   20752            0 :           cfg.set_overrun ();
   20753            0 :           error_at (loc, "compiled module is %sversion %s",
   20754              :                     IS_EXPERIMENTAL (their_ver) ? "experimental " : "",
   20755              :                     their_string);
   20756              :         }
   20757              :       else
   20758            0 :         inform_p = warning_at (loc, 0, "compiled module is %sversion %s",
   20759              :                              IS_EXPERIMENTAL (their_ver) ? "experimental " : "",
   20760              :                              their_string);
   20761              : 
   20762            0 :       if (inform_p)
   20763              :         {
   20764            0 :           inform (loc, "compiler is %sversion %s%s%s",
   20765              :                   IS_EXPERIMENTAL (my_ver) ? "experimental " : "",
   20766              :                   my_string,
   20767            0 :                   reject_p ? "" : flag_module_version_ignore
   20768            0 :                   ? ", be it on your own head!" : ", close enough?",
   20769              :                   reject_p ? "" : " \xc2\xaf\\_(\xe3\x83\x84)_/\xc2\xaf");
   20770            0 :           note_cmi_name ();
   20771              :         }
   20772              : 
   20773            0 :       if (reject_p)
   20774            0 :         goto done;
   20775            0 :     }
   20776              : 
   20777              :   /*  We wrote the inner crc merely to merge it, so simply read it
   20778              :       back and forget it.  */
   20779         3011 :   cfg.u32 ();
   20780              : 
   20781              :   /* Check module name.  */
   20782         3011 :   {
   20783         3011 :     const char *their_name = from ()->name (cfg.u ());
   20784         3011 :     const char *our_name = "";
   20785              : 
   20786         3011 :     if (!is_header ())
   20787         1988 :       our_name = get_flatname ();
   20788              : 
   20789              :     /* Header units can be aliased, so name checking is
   20790              :        inappropriate.  */
   20791         3011 :     if (0 != strcmp (their_name, our_name))
   20792              :       {
   20793            0 :         error_at (loc,
   20794            0 :                   their_name[0] && our_name[0] ? G_("module %qs found")
   20795              :                   : their_name[0]
   20796              :                   ? G_("header module expected, module %qs found")
   20797              :                   : G_("module %qs expected, header module found"),
   20798            0 :                   their_name[0] ? their_name : our_name);
   20799            0 :         cfg.set_overrun ();
   20800            0 :         goto done;
   20801              :       }
   20802              :   }
   20803              : 
   20804              :   /* Check the CRC after the above sanity checks, so that the user is
   20805              :      clued in.  */
   20806         3011 :   {
   20807         3011 :     unsigned e_crc = crc;
   20808         3011 :     crc = cfg.get_crc ();
   20809         3543 :     dump () && dump ("Reading CRC=%x", crc);
   20810              :     /* When not complaining we haven't set directness yet, so ignore the
   20811              :        mismatch. */
   20812         3011 :     if (complain && !is_direct () && crc != e_crc)
   20813              :       {
   20814            3 :         error_at (loc, "module %qs CRC mismatch", get_flatname ());
   20815            3 :         cfg.set_overrun ();
   20816            3 :         goto done;
   20817              :       }
   20818              :   }
   20819              : 
   20820              :   /* Check target & host.  */
   20821         3008 :   {
   20822         3008 :     const char *their_target = from ()->name (cfg.u ());
   20823         3008 :     const char *their_host = from ()->name (cfg.u ());
   20824         3540 :     dump () && dump ("Read target='%s', host='%s'", their_target, their_host);
   20825         3008 :     if (strcmp (their_target, TARGET_MACHINE)
   20826         3008 :         || strcmp (their_host, HOST_MACHINE))
   20827              :       {
   20828            0 :         error_at (loc, "target & host is %qs:%qs, expected %qs:%qs",
   20829              :                   their_target, TARGET_MACHINE, their_host, HOST_MACHINE);
   20830            0 :         cfg.set_overrun ();
   20831            0 :         goto done;
   20832              :       }
   20833              :   }
   20834              : 
   20835              :   /* Check compilation dialect.  This must match.  */
   20836         3008 :   {
   20837         3008 :     const char *their_dialect = cfg.str ();
   20838         3008 :     if (strcmp (their_dialect, config.dialect_str))
   20839              :       {
   20840            1 :         if (complain)
   20841            1 :           error_at (loc, "language dialect differs %qs, expected %qs",
   20842              :                     their_dialect, config.dialect_str);
   20843            1 :         cfg.set_overrun ();
   20844            1 :         goto done;
   20845              :       }
   20846              :   }
   20847              : 
   20848              :   /* Check for extensions.  If they set any, we must have them set
   20849              :      too.  */
   20850         3007 :   {
   20851         3007 :     unsigned ext = cfg.u ();
   20852         3007 :     unsigned allowed = (flag_openmp ? SE_OPENMP | SE_OPENMP_SIMD : 0);
   20853         3007 :     if (flag_openmp_simd)
   20854            3 :       allowed |= SE_OPENMP_SIMD;
   20855         3007 :     if (flag_openacc)
   20856            3 :       allowed |= SE_OPENACC;
   20857              : 
   20858         3007 :     if (unsigned bad = ext & ~allowed)
   20859              :       {
   20860            9 :         if (bad & SE_OPENMP)
   20861            3 :           error_at (loc, "module contains OpenMP, use %<-fopenmp%> to enable");
   20862            6 :         else if (bad & SE_OPENMP_SIMD)
   20863            3 :           error_at (loc, "module contains OpenMP, use %<-fopenmp%> or "
   20864              :                          "%<-fopenmp-simd%> to enable");
   20865            9 :         if (bad & SE_OPENACC)
   20866            3 :           error_at (loc, "module contains OpenACC, use %<-fopenacc%> to "
   20867              :                          "enable");
   20868            9 :         cfg.set_overrun ();
   20869            9 :         goto done;
   20870              :       }
   20871         2998 :     extensions = ext;
   20872              :   }
   20873              : 
   20874              :   /* Check global trees.  */
   20875         2998 :   {
   20876         2998 :     unsigned their_fixed_length = cfg.u ();
   20877         2998 :     unsigned their_fixed_crc = cfg.u32 ();
   20878         3530 :     dump () && dump ("Read globals=%u, crc=%x",
   20879              :                      their_fixed_length, their_fixed_crc);
   20880         2998 :     if (!flag_preprocess_only
   20881         2998 :         && (their_fixed_length != fixed_trees->length ()
   20882         2944 :             || their_fixed_crc != global_crc))
   20883              :       {
   20884            0 :         error_at (loc, "fixed tree mismatch");
   20885            0 :         cfg.set_overrun ();
   20886            0 :         goto done;
   20887              :       }
   20888              :   }
   20889              : 
   20890              :   /* All non-partitions are interfaces.  */
   20891         2998 :   interface_p = !is_partition () || cfg.u ();
   20892              : 
   20893         2998 :   config.num_imports = cfg.u ();
   20894         2998 :   config.num_partitions = cfg.u ();
   20895         2998 :   config.num_entities = cfg.u ();
   20896              : 
   20897         2998 :   config.ordinary_locs = cfg.loc ();
   20898         2998 :   config.macro_locs = cfg.loc ();
   20899         2998 :   config.loc_range_bits = cfg.u ();
   20900              : 
   20901         2998 :   config.active_init = cfg.u ();
   20902              : 
   20903         3011 :  done:
   20904         3011 :   return cfg.end (from ());
   20905         3011 : }
   20906              : 
   20907              : /* Comparator for ordering the Ordered Ordinary Location array.  */
   20908              : 
   20909              : static int
   20910          124 : ool_cmp (const void *a_, const void *b_)
   20911              : {
   20912          124 :   auto *a = *static_cast<const module_state *const *> (a_);
   20913          124 :   auto *b = *static_cast<const module_state *const *> (b_);
   20914          124 :   if (a == b)
   20915              :     return 0;
   20916          124 :   else if (a->ordinary_locs.first < b->ordinary_locs.first)
   20917              :     return -1;
   20918              :   else
   20919           52 :     return +1;
   20920              : }
   20921              : 
   20922              : /* Use ELROND format to record the following sections:
   20923              :      qualified-names        : binding value(s)
   20924              :      MOD_SNAME_PFX.README   : human readable, strings
   20925              :      MOD_SNAME_PFX.ENV      : environment strings, strings
   20926              :      MOD_SNAME_PFX.nms      : namespace hierarchy
   20927              :      MOD_SNAME_PFX.udi      : namespace using-directives
   20928              :      MOD_SNAME_PFX.bnd      : binding table
   20929              :      MOD_SNAME_PFX.spc      : specialization table
   20930              :      MOD_SNAME_PFX.imp      : import table
   20931              :      MOD_SNAME_PFX.ent      : entity table
   20932              :      MOD_SNAME_PFX.prt      : partitions table
   20933              :      MOD_SNAME_PFX.olm      : ordinary line maps
   20934              :      MOD_SNAME_PFX.mlm      : macro line maps
   20935              :      MOD_SNAME_PFX.def      : macro definitions
   20936              :      MOD_SNAME_PFX.mac      : macro index
   20937              :      MOD_SNAME_PFX.ini      : inits
   20938              :      MOD_SNAME_PFX.cnt      : counts
   20939              :      MOD_SNAME_PFX.cfg      : config data
   20940              : */
   20941              : 
   20942              : bool
   20943         2742 : module_state::write_begin (elf_out *to, cpp_reader *reader,
   20944              :                            module_state_config &config, unsigned &crc)
   20945              : {
   20946              :   /* Figure out remapped module numbers, which might elide
   20947              :      partitions.  */
   20948         2742 :   bitmap partitions = NULL;
   20949         2742 :   if (!is_header () && !is_partition ())
   20950         1660 :     partitions = BITMAP_GGC_ALLOC ();
   20951         2742 :   write_init_maps ();
   20952              : 
   20953         2742 :   unsigned mod_hwm = 1;
   20954         3410 :   for (unsigned ix = 1; ix != modules->length (); ix++)
   20955              :     {
   20956          668 :       module_state *imp = (*modules)[ix];
   20957              : 
   20958              :       /* Promote any non-partition direct import from a partition, unless
   20959              :          we're a partition.  */
   20960          611 :       if (!is_partition () && !imp->is_partition ()
   20961         1098 :           && imp->is_partition_direct ())
   20962           12 :         imp->directness = MD_PURVIEW_DIRECT;
   20963              : 
   20964              :       /* Write any import that is not a partition, unless we're a
   20965              :          partition.  */
   20966          668 :       if (!partitions || !imp->is_partition ())
   20967          487 :         imp->remap = mod_hwm++;
   20968              :       else
   20969              :         {
   20970          220 :           dump () && dump ("Partition %M %u", imp, ix);
   20971          181 :           bitmap_set_bit (partitions, ix);
   20972          181 :           imp->remap = 0;
   20973              :           /* All interface partitions must be exported.  */
   20974          181 :           if (imp->is_interface () && !bitmap_bit_p (exports, imp->mod))
   20975              :             {
   20976            3 :               error_at (imp->loc, "interface partition is not exported");
   20977            3 :               bitmap_set_bit (exports, imp->mod);
   20978              :             }
   20979              : 
   20980              :           /* All the partition entities should have been loaded when
   20981              :              loading the partition.  */
   20982              :           if (CHECKING_P)
   20983         1218 :             for (unsigned jx = 0; jx != imp->entity_num; jx++)
   20984              :               {
   20985         1037 :                 binding_slot *slot = &(*entity_ary)[imp->entity_lwm + jx];
   20986         1037 :                 gcc_checking_assert (!slot->is_lazy ());
   20987              :               }
   20988              :         }
   20989              : 
   20990          668 :       if (imp->is_direct () && (imp->remap || imp->is_partition ()))
   20991          650 :         note_location (imp->imported_from ());
   20992              :     }
   20993              : 
   20994         2742 :   if (partitions && bitmap_empty_p (partitions))
   20995              :     /* No partitions present.  */
   20996              :     partitions = nullptr;
   20997              : 
   20998              :   /* Find the set of decls we must write out.  */
   20999         2742 :   depset::hash table (DECL_NAMESPACE_BINDINGS (global_namespace)->size () * 8);
   21000              :   /* Add the specializations before the writables, so that we can
   21001              :      detect injected friend specializations.  */
   21002         2742 :   table.add_specializations (true);
   21003         2742 :   table.add_specializations (false);
   21004         2742 :   if (partial_specializations)
   21005              :     {
   21006          207 :       table.add_partial_entities (partial_specializations);
   21007          207 :       partial_specializations = NULL;
   21008              :     }
   21009         2742 :   table.add_namespace_entities (global_namespace, partitions);
   21010         2742 :   if (class_members)
   21011              :     {
   21012           12 :       table.add_class_entities (class_members);
   21013           12 :       class_members = NULL;
   21014              :     }
   21015              : 
   21016              :   /* Now join everything up.  */
   21017         2742 :   table.find_dependencies (this);
   21018              : 
   21019         2742 :   if (!table.finalize_dependencies ())
   21020              :     return false;
   21021              : 
   21022              : #if CHECKING_P
   21023              :   /* We're done verifying at-most once reading, reset to verify
   21024              :      at-most once writing.  */
   21025         2713 :   note_defs = note_defs_table_t::create_ggc (1000);
   21026              : #endif
   21027              : 
   21028              :   /* Determine Strongly Connected Components.  This will also strip any
   21029              :      unnecessary dependencies on imported or TU-local entities.  */
   21030         2713 :   vec<depset *> sccs = table.connect ();
   21031              : 
   21032         2713 :   vec_alloc (ool, modules->length ());
   21033         3374 :   for (unsigned ix = modules->length (); --ix;)
   21034              :     {
   21035          661 :       auto *import = (*modules)[ix];
   21036          661 :       if (import->loadedness > ML_NONE
   21037          661 :           && !(partitions && bitmap_bit_p (partitions, import->mod)))
   21038          480 :         ool->quick_push (import);
   21039              :     }
   21040         2713 :   ool->qsort (ool_cmp);
   21041              : 
   21042         2713 :   write_diagnostic_classification (nullptr, global_dc, nullptr);
   21043              : 
   21044         2713 :   vec<cpp_hashnode *> *macros = nullptr;
   21045         2713 :   if (is_header ())
   21046          889 :     macros = prepare_macros (reader);
   21047              : 
   21048         2713 :   config.num_imports = mod_hwm;
   21049         2713 :   config.num_partitions = modules->length () - mod_hwm;
   21050         2713 :   auto map_info = write_prepare_maps (&config, bool (config.num_partitions));
   21051         2713 :   unsigned counts[MSC_HWM];
   21052         2713 :   memset (counts, 0, sizeof (counts));
   21053              : 
   21054              :   /* depset::cluster is the cluster number,
   21055              :      depset::section is unspecified scratch value.
   21056              : 
   21057              :      The following loops make use of the tarjan property that
   21058              :      dependencies will be earlier in the SCCS array.  */
   21059              : 
   21060              :   /* This first loop determines the number of depsets in each SCC, and
   21061              :      also the number of namespaces we're dealing with.  During the
   21062              :      loop, the meaning of a couple of depset fields now change:
   21063              : 
   21064              :      depset::cluster -> size_of cluster, if first of cluster & !namespace
   21065              :      depset::section -> section number of cluster (if !namespace). */
   21066              : 
   21067         2713 :   unsigned n_spaces = 0;
   21068         2713 :   counts[MSC_sec_lwm] = counts[MSC_sec_hwm] = to->get_section_limit ();
   21069       277188 :   for (unsigned size, ix = 0; ix < sccs.length (); ix += size)
   21070              :     {
   21071       274475 :       depset **base = &sccs[ix];
   21072              : 
   21073       518316 :       if (base[0]->get_entity_kind () == depset::EK_NAMESPACE)
   21074              :         {
   21075         4822 :           n_spaces++;
   21076         4822 :           size = 1;
   21077              :         }
   21078              :       else
   21079              :         {
   21080              :           /* Count the members in this cluster.  */
   21081      1160648 :           for (size = 1; ix + size < sccs.length (); size++)
   21082      1158266 :             if (base[size]->cluster != base[0]->cluster)
   21083              :               break;
   21084              : 
   21085      1430301 :           for (unsigned jx = 0; jx != size; jx++)
   21086              :             {
   21087              :               /* Set the section number.  */
   21088      1160648 :               base[jx]->cluster = ~(~0u >> 1); /* A bad value.  */
   21089      1160648 :               base[jx]->section = counts[MSC_sec_hwm];
   21090              :             }
   21091              : 
   21092              :           /* Save the size in the first member's cluster slot.  */
   21093       269653 :           base[0]->cluster = size;
   21094              : 
   21095       269653 :           counts[MSC_sec_hwm]++;
   21096              :         }
   21097              :     }
   21098              : 
   21099              :   /* Write the clusters.  Namespace decls are put in the spaces array.
   21100              :      The meaning of depset::cluster changes to provide the
   21101              :      unnamed-decl count of the depset's decl (and remains zero for
   21102              :      non-decls and non-unnamed).  */
   21103         2713 :   unsigned bytes = 0;
   21104         2713 :   vec<depset *> spaces;
   21105         2713 :   spaces.create (n_spaces);
   21106              : 
   21107       277188 :   for (unsigned size, ix = 0; ix < sccs.length (); ix += size)
   21108              :     {
   21109       274475 :       depset **base = &sccs[ix];
   21110              : 
   21111       274475 :       if (base[0]->get_entity_kind () == depset::EK_NAMESPACE)
   21112              :         {
   21113         4822 :           tree decl = base[0]->get_entity ();
   21114         4822 :           if (decl == global_namespace)
   21115         2459 :             base[0]->cluster = 0;
   21116         2363 :           else if (!base[0]->is_import ())
   21117              :             {
   21118         2363 :               base[0]->cluster = counts[MSC_entities]++;
   21119         2363 :               spaces.quick_push (base[0]);
   21120         2363 :               counts[MSC_namespaces]++;
   21121         2363 :               if (CHECKING_P)
   21122              :                 {
   21123              :                   /* Add it to the entity map, such that we can tell it is
   21124              :                      part of us.  */
   21125         2363 :                   bool existed;
   21126         2363 :                   unsigned *slot = &entity_map->get_or_insert
   21127         2363 :                     (DECL_UID (decl), &existed);
   21128         2363 :                   if (existed)
   21129              :                     /* It must have come from a partition.  */
   21130            0 :                     gcc_checking_assert
   21131              :                       (import_entity_module (*slot)->is_partition ());
   21132         2363 :                   *slot = ~base[0]->cluster;
   21133              :                 }
   21134       276868 :               dump (dumper::CLUSTER) && dump ("Cluster namespace %N", decl);
   21135              :             }
   21136              :           size = 1;
   21137              :         }
   21138              :       else
   21139              :         {
   21140       269653 :           size = base[0]->cluster;
   21141              : 
   21142              :           /* Cluster is now used to number entities.  */
   21143       269653 :           base[0]->cluster = ~(~0u >> 1); /* A bad value.  */
   21144              : 
   21145       269653 :           sort_cluster (&table, base, size);
   21146              : 
   21147              :           /* Record the section for consistency checking during stream
   21148              :              out -- we don't want to start writing decls in different
   21149              :              sections.  */
   21150       269653 :           table.section = base[0]->section;
   21151       269653 :           bytes += write_cluster (to, base, size, table, counts, &crc);
   21152       269653 :           table.section = 0;
   21153              :         }
   21154              :     }
   21155              : 
   21156              :   /* depset::cluster - entity number (on entities)
   21157              :      depset::section - cluster number  */
   21158              :   /* We'd better have written as many sections and found as many
   21159              :      namespaces as we predicted.  */
   21160         5426 :   gcc_assert (counts[MSC_sec_hwm] == to->get_section_limit ()
   21161              :               && spaces.length () == counts[MSC_namespaces]);
   21162              : 
   21163              :   /* Write the entitites.  None happens if we contain namespaces or
   21164              :      nothing. */
   21165         2713 :   config.num_entities = counts[MSC_entities];
   21166         2713 :   if (counts[MSC_entities])
   21167         2453 :     write_entities (to, sccs, counts[MSC_entities], &crc);
   21168              : 
   21169              :   /* Write the namespaces.  */
   21170         2713 :   if (counts[MSC_namespaces])
   21171          577 :     write_namespaces (to, spaces, counts[MSC_namespaces], &crc);
   21172              : 
   21173              :   /* Write any using-directives.  */
   21174         2713 :   if (counts[MSC_namespaces])
   21175          577 :     counts[MSC_using_directives]
   21176          577 :       = write_using_directives (to, table, spaces, &crc);
   21177              : 
   21178              :   /* Write the bindings themselves.  */
   21179         2713 :   counts[MSC_bindings] = write_bindings (to, sccs, &crc);
   21180              : 
   21181              :   /* Write the unnamed.  */
   21182         2713 :   counts[MSC_pendings] = write_pendings (to, sccs, table, &crc);
   21183              : 
   21184              :   /* Write the import table.  */
   21185         2713 :   if (config.num_imports > 1)
   21186          449 :     write_imports (to, &crc);
   21187              : 
   21188              :   /* Write elided partition table.  */
   21189         2713 :   if (config.num_partitions)
   21190          130 :     write_partitions (to, config.num_partitions, &crc);
   21191              : 
   21192              :   /* Write the line maps.  */
   21193         2713 :   if (config.ordinary_locs)
   21194              :     {
   21195         2617 :       write_ordinary_maps (to, map_info, bool (config.num_partitions), &crc);
   21196         2617 :       write_diagnostic_classification (to, global_dc, &crc);
   21197              :     }
   21198         2713 :   if (config.macro_locs)
   21199          120 :     write_macro_maps (to, map_info, &crc);
   21200              : 
   21201         2713 :   if (is_header ())
   21202              :     {
   21203          889 :       counts[MSC_macros] = write_macros (to, macros, &crc);
   21204          889 :       counts[MSC_inits] = write_inits (to, table, &crc);
   21205          889 :       vec_free (macros);
   21206              :     }
   21207              : 
   21208         2713 :   unsigned clusters = counts[MSC_sec_hwm] - counts[MSC_sec_lwm];
   21209         2713 :   dump () && dump ("Wrote %u clusters, average %u bytes/cluster",
   21210          300 :                    clusters, (bytes + clusters / 2) / (clusters + !clusters));
   21211         2713 :   trees_out::instrument ();
   21212              : 
   21213         2713 :   write_counts (to, counts, &crc);
   21214              : 
   21215         2713 :   spaces.release ();
   21216         2713 :   sccs.release ();
   21217              : 
   21218         2713 :   vec_free (macro_loc_remap);
   21219         2713 :   vec_free (ord_loc_remap);
   21220         2713 :   vec_free (ool);
   21221              : 
   21222              :   // FIXME:QOI:  Have a command line switch to control more detailed
   21223              :   // information (which might leak data you do not want to leak).
   21224              :   // Perhaps (some of) the write_readme contents should also be
   21225              :   // so-controlled.
   21226         2713 :   if (false)
   21227              :     write_env (to);
   21228              : 
   21229         2713 :   return true;
   21230         2742 : }
   21231              : 
   21232              : // Finish module writing after we've emitted all dynamic initializers.
   21233              : 
   21234              : void
   21235         2713 : module_state::write_end (elf_out *to, cpp_reader *reader,
   21236              :                          module_state_config &config, unsigned &crc)
   21237              : {
   21238              :   /* And finish up.  */
   21239         2713 :   write_config (to, config, crc);
   21240              : 
   21241              :   /* Human-readable info.  */
   21242         2713 :   write_readme (to, reader, config.dialect_str);
   21243              : 
   21244         3013 :   dump () && dump ("Wrote %u sections", to->get_section_limit ());
   21245         2713 : }
   21246              : 
   21247              : /* Initial read of a CMI.  Checks config, loads up imports and line
   21248              :    maps.  */
   21249              : 
   21250              : bool
   21251         2965 : module_state::read_initial (cpp_reader *reader)
   21252              : {
   21253         2965 :   module_state_config config;
   21254         2965 :   bool ok = true;
   21255              : 
   21256         2965 :   if (ok && !read_config (config))
   21257              :     ok = false;
   21258              : 
   21259         2952 :   bool have_locs = ok && read_prepare_maps (&config);
   21260              : 
   21261              :   /* Ordinary maps before the imports.  */
   21262         2952 :   if (!(have_locs && config.ordinary_locs))
   21263           71 :     ordinary_locs.first = line_table->highest_location + 1;
   21264         2894 :   else if (!read_ordinary_maps (config.ordinary_locs, config.loc_range_bits))
   21265              :     ok = false;
   21266              : 
   21267              :   /* Allocate the REMAP vector.  */
   21268         2965 :   slurp->alloc_remap (config.num_imports);
   21269              : 
   21270         2965 :   if (ok)
   21271              :     {
   21272              :       /* Read the import table.  Decrement current to stop this CMI
   21273              :          from being evicted during the import. */
   21274         2952 :       slurp->current--;
   21275         2952 :       if (config.num_imports > 1 && !read_imports (reader, line_table))
   21276              :         ok = false;
   21277         2952 :       slurp->current++;
   21278              :     }
   21279              : 
   21280              :   /* Read the elided partition table, if we're the primary partition.  */
   21281         2952 :   if (ok && config.num_partitions && is_module ()
   21282         2976 :       && !read_partitions (config.num_partitions))
   21283              :     ok = false;
   21284              : 
   21285              :   /* Determine the module's number.  */
   21286         2965 :   gcc_checking_assert (mod == MODULE_UNKNOWN);
   21287         2965 :   gcc_checking_assert (this != this_module ());
   21288              : 
   21289         2965 :   {
   21290              :     /* Allocate space in the entities array now -- that array must be
   21291              :        monotonically in step with the modules array.  */
   21292         2965 :     entity_lwm = vec_safe_length (entity_ary);
   21293         2965 :     entity_num = config.num_entities;
   21294         2965 :     gcc_checking_assert (modules->length () == 1
   21295              :                          || modules->last ()->entity_lwm <= entity_lwm);
   21296         2965 :     vec_safe_reserve (entity_ary, config.num_entities);
   21297              : 
   21298         2965 :     binding_slot slot;
   21299         2965 :     slot.u.binding = NULL_TREE;
   21300      1296194 :     for (unsigned count = config.num_entities; count--;)
   21301      1293229 :       entity_ary->quick_push (slot);
   21302              :   }
   21303              : 
   21304              :   /* We'll run out of other resources before we run out of module
   21305              :      indices.  */
   21306         2965 :   mod = modules->length ();
   21307         2965 :   vec_safe_push (modules, this);
   21308              : 
   21309              :   /* We always import and export ourselves. */
   21310         2965 :   bitmap_set_bit (imports, mod);
   21311         2965 :   bitmap_set_bit (exports, mod);
   21312              : 
   21313         2965 :   if (ok)
   21314         2952 :     (*slurp->remap)[0] = mod << 1;
   21315         3494 :   dump () && dump ("Assigning %M module number %u", this, mod);
   21316              : 
   21317              :   /* We should not have been frozen during the importing done by
   21318              :      read_config.  */
   21319         2965 :   gcc_assert (!from ()->is_frozen ());
   21320              : 
   21321              :   /* Macro maps after the imports.  */
   21322         2965 :   if (!(ok && have_locs && config.macro_locs))
   21323         2834 :     macro_locs.first = LINEMAPS_MACRO_LOWEST_LOCATION (line_table);
   21324          131 :   else if (!read_macro_maps (config.macro_locs))
   21325              :     ok = false;
   21326              : 
   21327              :   /* Diagnostic classification streaming needs to come after reading
   21328              :      macro maps to handle _Pragmas in macros.  */
   21329         2952 :   if (ok && have_locs && config.ordinary_locs
   21330         5859 :       && !read_diagnostic_classification (global_dc))
   21331              :     ok = false;
   21332              : 
   21333              :   /* Note whether there's an active initializer.  */
   21334         2965 :   active_init_p = !is_header () && bool (config.active_init);
   21335              : 
   21336         2965 :   gcc_assert (slurp->current == ~0u);
   21337         2965 :   return ok;
   21338              : }
   21339              : 
   21340              : /* Read a preprocessor state.  */
   21341              : 
   21342              : bool
   21343          927 : module_state::read_preprocessor (bool outermost)
   21344              : {
   21345          927 :   gcc_checking_assert (is_header () && slurp
   21346              :                        && slurp->remap_module (0) == mod);
   21347              : 
   21348          927 :   if (loadedness == ML_PREPROCESSOR)
   21349            6 :     return !(from () && from ()->get_error ());
   21350              : 
   21351          921 :   bool ok = true;
   21352              : 
   21353              :   /* Read direct header imports.  */
   21354          921 :   unsigned len = slurp->remap->length ();
   21355          963 :   for (unsigned ix = 1; ok && ix != len; ix++)
   21356              :     {
   21357           42 :       unsigned map = (*slurp->remap)[ix];
   21358           42 :       if (map & 1)
   21359              :         {
   21360           42 :           module_state *import = (*modules)[map >> 1];
   21361           42 :           if (import->is_header ())
   21362              :             {
   21363           42 :               ok = import->read_preprocessor (false);
   21364           42 :               bitmap_ior_into (slurp->headers, import->slurp->headers);
   21365              :             }
   21366              :         }
   21367              :     }
   21368              : 
   21369              :   /* Record as a direct header.  */
   21370          921 :   if (ok)
   21371          921 :     bitmap_set_bit (slurp->headers, mod);
   21372              : 
   21373          921 :   if (ok && !read_macros ())
   21374              :     ok = false;
   21375              : 
   21376          921 :   loadedness = ML_PREPROCESSOR;
   21377          921 :   announce ("macros");
   21378              : 
   21379          921 :   if (flag_preprocess_only)
   21380              :     /* We're done with the string table.  */
   21381           39 :     from ()->release ();
   21382              : 
   21383          921 :   return check_read (outermost, ok);
   21384              : }
   21385              : 
   21386              : /* Read language state.  */
   21387              : 
   21388              : bool
   21389         2985 : module_state::read_language (bool outermost)
   21390              : {
   21391         2985 :   gcc_checking_assert (!lazy_snum);
   21392              : 
   21393         2985 :   if (loadedness == ML_LANGUAGE)
   21394           84 :     return !(slurp && from () && from ()->get_error ());
   21395              : 
   21396         2901 :   gcc_checking_assert (slurp && slurp->current == ~0u
   21397              :                        && slurp->remap_module (0) == mod);
   21398              : 
   21399         2901 :   bool ok = true;
   21400              : 
   21401              :   /* Read direct imports.  */
   21402         2901 :   unsigned len = slurp->remap->length ();
   21403         3300 :   for (unsigned ix = 1; ok && ix != len; ix++)
   21404              :     {
   21405          399 :       unsigned map = (*slurp->remap)[ix];
   21406          399 :       if (map & 1)
   21407              :         {
   21408          395 :           module_state *import = (*modules)[map >> 1];
   21409          395 :           if (!import->read_language (false))
   21410          399 :             ok = false;
   21411              :         }
   21412              :     }
   21413              : 
   21414         2901 :   unsigned counts[MSC_HWM];
   21415              : 
   21416         2901 :   if (ok && !read_counts (counts))
   21417              :     ok = false;
   21418              : 
   21419         2901 :   function_depth++; /* Prevent unexpected GCs.  */
   21420              : 
   21421         2901 :   if (ok && counts[MSC_entities] != entity_num)
   21422              :     ok = false;
   21423         2901 :   if (ok && counts[MSC_entities]
   21424         2675 :       && !read_entities (counts[MSC_entities],
   21425              :                          counts[MSC_sec_lwm], counts[MSC_sec_hwm]))
   21426              :     ok = false;
   21427              : 
   21428              :   /* Read the namespace hierarchy.  */
   21429         2901 :   if (ok && counts[MSC_namespaces]
   21430         3496 :       && !read_namespaces (counts[MSC_namespaces]))
   21431              :     ok = false;
   21432              : 
   21433              :   /* Read any using-directives.  */
   21434         2901 :   if (ok && counts[MSC_using_directives]
   21435         3058 :       && !read_using_directives (counts[MSC_using_directives]))
   21436              :     ok = false;
   21437              : 
   21438         2901 :   if (ok && !read_bindings (counts[MSC_bindings],
   21439              :                             counts[MSC_sec_lwm], counts[MSC_sec_hwm]))
   21440              :     ok = false;
   21441              : 
   21442              :   /* And unnamed.  */
   21443         2901 :   if (ok && counts[MSC_pendings] && !read_pendings (counts[MSC_pendings]))
   21444              :     ok = false;
   21445              : 
   21446         2901 :   if (ok)
   21447              :     {
   21448         2901 :       slurp->remaining = counts[MSC_sec_hwm] - counts[MSC_sec_lwm];
   21449         2901 :       available_clusters += counts[MSC_sec_hwm] - counts[MSC_sec_lwm];
   21450              :     }
   21451              : 
   21452         2901 :   if (!flag_module_lazy
   21453         2901 :       || (is_partition ()
   21454          232 :           && module_interface_p ()
   21455          205 :           && !module_partition_p ()))
   21456              :     {
   21457              :       /* Read the sections in forward order, so that dependencies are read
   21458              :          first.  See note about tarjan_connect.  */
   21459          525 :       ggc_collect ();
   21460              : 
   21461          525 :       lazy_snum = ~0u;
   21462              : 
   21463          525 :       unsigned hwm = counts[MSC_sec_hwm];
   21464       143389 :       for (unsigned ix = counts[MSC_sec_lwm]; ok && ix != hwm; ix++)
   21465       142864 :         if (!load_section (ix, NULL))
   21466              :           {
   21467              :             ok = false;
   21468              :             break;
   21469              :           }
   21470          525 :       lazy_snum = 0;
   21471          525 :       post_load_processing ();
   21472              : 
   21473          525 :       ggc_collect ();
   21474              : 
   21475          525 :       if (ok && CHECKING_P)
   21476       513991 :         for (unsigned ix = 0; ix != entity_num; ix++)
   21477       513466 :           gcc_assert (!(*entity_ary)[ix + entity_lwm].is_lazy ());
   21478              :     }
   21479              : 
   21480              :   // If the import is a header-unit, we need to register initializers
   21481              :   // of any static objects it contains (looking at you _Ioinit).
   21482              :   // Notice, the ordering of these initializers will be that of a
   21483              :   // dynamic initializer at this point in the current TU.  (Other
   21484              :   // instances of these objects in other TUs will be initialized as
   21485              :   // part of that TU's global initializers.)
   21486         2901 :   if (ok && counts[MSC_inits] && !read_inits (counts[MSC_inits]))
   21487              :     ok = false;
   21488              : 
   21489         2901 :   function_depth--;
   21490              : 
   21491         3245 :   announce (flag_module_lazy ? "lazy" : "imported");
   21492         2901 :   loadedness = ML_LANGUAGE;
   21493              : 
   21494         2901 :   gcc_assert (slurp->current == ~0u);
   21495              : 
   21496              :   /* We're done with the string table.  */
   21497         2901 :   from ()->release ();
   21498              : 
   21499         2901 :   return check_read (outermost, ok);
   21500              : }
   21501              : 
   21502              : bool
   21503       206410 : module_state::maybe_defrost ()
   21504              : {
   21505       206410 :   bool ok = true;
   21506       206410 :   if (from ()->is_frozen ())
   21507              :     {
   21508            9 :       if (lazy_open >= lazy_limit)
   21509            3 :         freeze_an_elf ();
   21510           18 :       dump () && dump ("Defrosting '%s'", filename);
   21511            9 :       ok = from ()->defrost (maybe_add_cmi_prefix (filename));
   21512            9 :       lazy_open++;
   21513              :     }
   21514              : 
   21515       206410 :   return ok;
   21516              : }
   21517              : 
   21518              : /* Load section SNUM, dealing with laziness.  It doesn't matter if we
   21519              :    have multiple concurrent loads, because we do not use TREE_VISITED
   21520              :    when reading back in.  */
   21521              : 
   21522              : bool
   21523       206410 : module_state::load_section (unsigned snum, binding_slot *mslot)
   21524              : {
   21525       206410 :   if (from ()->get_error ())
   21526              :     return false;
   21527              : 
   21528       206410 :   if (snum >= slurp->current)
   21529            0 :     from ()->set_error (elf::E_BAD_LAZY);
   21530       206410 :   else if (maybe_defrost ())
   21531              :     {
   21532       206410 :       unsigned old_current = slurp->current;
   21533       206410 :       slurp->current = snum;
   21534       206410 :       slurp->lru = 0;  /* Do not swap out.  */
   21535       206410 :       slurp->remaining--;
   21536       206410 :       read_cluster (snum);
   21537       206410 :       slurp->lru = ++lazy_lru;
   21538       206410 :       slurp->current = old_current;
   21539              :     }
   21540              : 
   21541       206410 :   if (mslot && mslot->is_lazy ())
   21542              :     {
   21543              :       /* Oops, the section didn't set this slot.  */
   21544            0 :       from ()->set_error (elf::E_BAD_DATA);
   21545            0 :       *mslot = NULL_TREE;
   21546              :     }
   21547              : 
   21548       206410 :   bool ok = !from ()->get_error ();
   21549       206410 :   if (!ok)
   21550              :     {
   21551            0 :       error_at (loc, "failed to read compiled module cluster %u: %s",
   21552            0 :                 snum, from ()->get_error (filename));
   21553            0 :       note_cmi_name ();
   21554              :     }
   21555              : 
   21556       206410 :   maybe_completed_reading ();
   21557              : 
   21558       206410 :   return ok;
   21559              : }
   21560              : 
   21561              : void
   21562       213181 : module_state::maybe_completed_reading ()
   21563              : {
   21564       213181 :   if (loadedness == ML_LANGUAGE && slurp->current == ~0u && !slurp->remaining)
   21565              :     {
   21566         2391 :       lazy_open--;
   21567              :       /* We no longer need the macros, all tokenizing has been done.  */
   21568         2391 :       slurp->release_macros ();
   21569              : 
   21570         2391 :       from ()->end ();
   21571         2391 :       slurp->close ();
   21572         2391 :       slurped ();
   21573              :     }
   21574       213181 : }
   21575              : 
   21576              : /* After a reading operation, make sure things are still ok.  If not,
   21577              :    emit an error and clean up.  */
   21578              : 
   21579              : bool
   21580         6808 : module_state::check_read (bool outermost, bool ok)
   21581              : {
   21582         6808 :   gcc_checking_assert (!outermost || slurp->current == ~0u);
   21583              : 
   21584         6808 :   if (!ok)
   21585           34 :     from ()->set_error ();
   21586              : 
   21587         6808 :   if (int e = from ()->get_error ())
   21588              :     {
   21589           40 :       auto_diagnostic_group d;
   21590           40 :       error_at (loc, "failed to read compiled module: %s",
   21591           40 :                 from ()->get_error (filename));
   21592           40 :       note_cmi_name ();
   21593              : 
   21594           40 :       if (e == EMFILE
   21595           40 :           || e == ENFILE
   21596              : #if MAPPED_READING
   21597           40 :           || e == ENOMEM
   21598              : #endif
   21599              :           || false)
   21600            0 :         inform (loc, "consider using %<-fno-module-lazy%>,"
   21601              :                 " increasing %<-param-lazy-modules=%u%> value,"
   21602              :                 " or increasing the per-process file descriptor limit",
   21603              :                 param_lazy_modules);
   21604           40 :       else if (e == ENOENT)
   21605           21 :         inform (loc, "imports must be built before being imported");
   21606              : 
   21607           40 :       if (outermost)
   21608           37 :         fatal_error (loc, "returning to the gate for a mechanical issue");
   21609              : 
   21610            3 :       ok = false;
   21611            3 :     }
   21612              : 
   21613         6771 :   maybe_completed_reading ();
   21614              : 
   21615         6771 :   return ok;
   21616              : }
   21617              : 
   21618              : /* Return the IDENTIFIER_NODE naming module IX.  This is the name
   21619              :    including dots.  */
   21620              : 
   21621              : char const *
   21622          408 : module_name (unsigned ix, bool header_ok)
   21623              : {
   21624          408 :   if (modules)
   21625              :     {
   21626          408 :       module_state *imp = (*modules)[ix];
   21627              : 
   21628          408 :       if (ix && !imp->name)
   21629            0 :         imp = imp->parent;
   21630              : 
   21631          408 :       if (header_ok || !imp->is_header ())
   21632          408 :         return imp->get_flatname ();
   21633              :     }
   21634              : 
   21635              :   return NULL;
   21636              : }
   21637              : 
   21638              : /* Return the bitmap describing what modules are imported.  Remember,
   21639              :    we always import ourselves.  */
   21640              : 
   21641              : bitmap
   21642       119720 : get_import_bitmap ()
   21643              : {
   21644       119720 :   return this_module ()->imports;
   21645              : }
   21646              : 
   21647              : /* Get the original decl for an instantiation at TINST, or NULL_TREE
   21648              :    if we're not an instantiation.  */
   21649              : 
   21650              : static tree
   21651       186117 : orig_decl_for_instantiation (tinst_level *tinst)
   21652              : {
   21653       186117 :   if (!tinst || TREE_CODE (tinst->tldcl) == TEMPLATE_FOR_STMT)
   21654              :     return NULL_TREE;
   21655              : 
   21656        89068 :   tree decl = tinst->tldcl;
   21657        89068 :   if (TREE_CODE (decl) == TREE_LIST)
   21658            0 :     decl = TREE_PURPOSE (decl);
   21659        89068 :   if (TYPE_P (decl))
   21660        13028 :     decl = TYPE_NAME (decl);
   21661              :   return decl;
   21662              : }
   21663              : 
   21664              : /* Return the visible imports and path of instantiation for an
   21665              :    instantiation at TINST.  If TINST is nullptr, we're not in an
   21666              :    instantiation, and thus will return the visible imports of the
   21667              :    current TU (and NULL *PATH_MAP_P).   We cache the information on
   21668              :    the tinst level itself.  */
   21669              : 
   21670              : static bitmap
   21671       130840 : path_of_instantiation (tinst_level *tinst, bitmap *path_map_p)
   21672              : {
   21673       130840 :   gcc_checking_assert (modules_p ());
   21674              : 
   21675       130840 :   tree decl = orig_decl_for_instantiation (tinst);
   21676       130840 :   if (!decl)
   21677              :     {
   21678        58559 :       gcc_assert (!tinst || !tinst->next);
   21679              :       /* Not inside an instantiation, just the regular case.  */
   21680        58559 :       *path_map_p = nullptr;
   21681        58559 :       return get_import_bitmap ();
   21682              :     }
   21683              : 
   21684        72281 :   if (!tinst->path)
   21685              :     {
   21686              :       /* Calculate.  */
   21687        21459 :       bitmap visible = path_of_instantiation (tinst->next, path_map_p);
   21688        21459 :       bitmap path_map = *path_map_p;
   21689              : 
   21690        21459 :       if (!path_map)
   21691              :         {
   21692         2725 :           path_map = BITMAP_GGC_ALLOC ();
   21693         2725 :           bitmap_set_bit (path_map, 0);
   21694              :         }
   21695              : 
   21696        21459 :       if (unsigned mod = get_originating_module (decl))
   21697         3856 :         if (!bitmap_bit_p (path_map, mod))
   21698              :           {
   21699              :             /* This is brand new information!  */
   21700          170 :             bitmap new_path = BITMAP_GGC_ALLOC ();
   21701          170 :             bitmap_copy (new_path, path_map);
   21702          170 :             bitmap_set_bit (new_path, mod);
   21703          170 :             path_map = new_path;
   21704              : 
   21705          170 :             bitmap imports = (*modules)[mod]->imports;
   21706          170 :             if (bitmap_intersect_compl_p (imports, visible))
   21707              :               {
   21708              :                 /* IMPORTS contains additional modules to VISIBLE.  */
   21709           32 :                 bitmap new_visible = BITMAP_GGC_ALLOC ();
   21710              : 
   21711           32 :                 bitmap_ior (new_visible, visible, imports);
   21712           32 :                 visible = new_visible;
   21713              :               }
   21714              :           }
   21715              : 
   21716        21459 :       tinst->path = path_map;
   21717        21459 :       tinst->visible = visible;
   21718              :     }
   21719              : 
   21720        72281 :   *path_map_p = tinst->path;
   21721        72281 :   return tinst->visible;
   21722              : }
   21723              : 
   21724              : /* Return the bitmap describing what modules are visible along the
   21725              :    path of instantiation.  If we're not an instantiation, this will be
   21726              :    the visible imports of the TU.  *PATH_MAP_P is filled in with the
   21727              :    modules owning the instantiation path -- we see the module-linkage
   21728              :    entities of those modules.  */
   21729              : 
   21730              : bitmap
   21731     33769899 : visible_instantiation_path (bitmap *path_map_p)
   21732              : {
   21733     33769899 :   if (!modules_p ())
   21734              :     return NULL;
   21735              : 
   21736       109381 :   return path_of_instantiation (current_instantiation (), path_map_p);
   21737              : }
   21738              : 
   21739              : /* Returns the bitmap describing what modules were visible from the
   21740              :    module that the current instantiation originated from.  If we're
   21741              :    not an instantiation, returns NULL.  *MODULE_P is filled in with
   21742              :    the originating module of the definition for this instantiation.  */
   21743              : 
   21744              : bitmap
   21745        55277 : visible_from_instantiation_origination (unsigned *module_p)
   21746              : {
   21747        55277 :   if (!modules_p ())
   21748              :     return NULL;
   21749              : 
   21750        55277 :   tree decl = orig_decl_for_instantiation (current_instantiation ());
   21751        55277 :   if (!decl)
   21752              :     return NULL;
   21753              : 
   21754        16787 :   *module_p = get_originating_module (decl);
   21755        16787 :   return (*modules)[*module_p]->imports;
   21756              : }
   21757              : 
   21758              : /* We've just directly imported IMPORT.  Update our import/export
   21759              :    bitmaps.  IS_EXPORT is true if we're reexporting the OTHER.  */
   21760              : 
   21761              : void
   21762         3052 : module_state::set_import (module_state const *import, bool is_export)
   21763              : {
   21764         3052 :   gcc_checking_assert (this != import);
   21765              : 
   21766              :   /* We see IMPORT's exports (which includes IMPORT).  If IMPORT is
   21767              :      the primary interface or a partition we'll see its imports.  */
   21768         3052 :   bitmap_ior_into (imports, import->is_module () || import->is_partition ()
   21769              :                    ? import->imports : import->exports);
   21770              : 
   21771         3052 :   if (is_export)
   21772              :     /* We'll export OTHER's exports.  */
   21773          477 :     bitmap_ior_into (exports, import->exports);
   21774         3052 : }
   21775              : 
   21776              : /* Return the declaring entity of DECL.  That is the decl determining
   21777              :    how to decorate DECL with module information.  Returns NULL_TREE if
   21778              :    it's the global module.  */
   21779              : 
   21780              : tree
   21781    107537900 : get_originating_module_decl (tree decl)
   21782              : {
   21783              :   /* An enumeration constant.  */
   21784    107537900 :   if (TREE_CODE (decl) == CONST_DECL
   21785         6000 :       && DECL_CONTEXT (decl)
   21786    107543900 :       && (TREE_CODE (DECL_CONTEXT (decl)) == ENUMERAL_TYPE))
   21787         5481 :     decl = TYPE_NAME (DECL_CONTEXT (decl));
   21788    107532419 :   else if (TREE_CODE (decl) == FIELD_DECL
   21789    107514177 :            || TREE_CODE (decl) == USING_DECL
   21790    215034506 :            || CONST_DECL_USING_P (decl))
   21791              :     {
   21792        30851 :       decl = DECL_CONTEXT (decl);
   21793        30851 :       if (TREE_CODE (decl) != FUNCTION_DECL)
   21794        30851 :         decl = TYPE_NAME (decl);
   21795              :     }
   21796              : 
   21797    107537900 :   gcc_checking_assert (TREE_CODE (decl) == TEMPLATE_DECL
   21798              :                        || TREE_CODE (decl) == FUNCTION_DECL
   21799              :                        || TREE_CODE (decl) == TYPE_DECL
   21800              :                        || TREE_CODE (decl) == VAR_DECL
   21801              :                        || TREE_CODE (decl) == CONCEPT_DECL
   21802              :                        || TREE_CODE (decl) == NAMESPACE_DECL);
   21803              : 
   21804    109487934 :   for (;;)
   21805              :     {
   21806              :       /* Uninstantiated template friends are owned by the befriending
   21807              :          class -- not their context.  */
   21808    108512917 :       if (TREE_CODE (decl) == TEMPLATE_DECL
   21809    108512917 :           && DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (decl))
   21810         8539 :         decl = TYPE_NAME (DECL_CHAIN (decl));
   21811              : 
   21812              :       /* An imported temploid friend is attached to the same module the
   21813              :          befriending class was.  */
   21814    108512917 :       if (imported_temploid_friends)
   21815      3082257 :         if (tree *slot = imported_temploid_friends->get (decl))
   21816          741 :           decl = *slot;
   21817              : 
   21818    108512917 :       int use;
   21819    108512917 :       if (tree ti = node_template_info (decl, use))
   21820              :         {
   21821       773536 :           decl = TI_TEMPLATE (ti);
   21822       773536 :           if (TREE_CODE (decl) != TEMPLATE_DECL)
   21823              :             {
   21824              :               /* A friend template specialization.  */
   21825           34 :               gcc_checking_assert (OVL_P (decl));
   21826           34 :               return global_namespace;
   21827              :             }
   21828              :         }
   21829              :       else
   21830              :         {
   21831    107739381 :           tree ctx = CP_DECL_CONTEXT (decl);
   21832    107739381 :           if (TREE_CODE (ctx) == NAMESPACE_DECL)
   21833              :             break;
   21834              : 
   21835       201515 :           if (TYPE_P (ctx))
   21836              :             {
   21837       183171 :               ctx = TYPE_NAME (ctx);
   21838       183171 :               if (!ctx)
   21839              :                 {
   21840              :                   /* Some kind of internal type.  */
   21841            0 :                   gcc_checking_assert (DECL_ARTIFICIAL (decl));
   21842            0 :                   return global_namespace;
   21843              :                 }
   21844              :             }
   21845       201515 :           decl = ctx;
   21846              :         }
   21847       975017 :     }
   21848              : 
   21849    107537866 :   return decl;
   21850              : }
   21851              : 
   21852              : /* If DECL is imported, return which module imported it, or 0 for the current
   21853              :    module.  Except that if GLOBAL_M1, return -1 for decls attached to the
   21854              :    global module.  */
   21855              : 
   21856              : int
   21857      1403809 : get_originating_module (tree decl, bool global_m1)
   21858              : {
   21859      1403809 :   tree owner = get_originating_module_decl (decl);
   21860      1403809 :   tree not_tmpl = STRIP_TEMPLATE (owner);
   21861              : 
   21862      1403809 :   if (!DECL_LANG_SPECIFIC (not_tmpl))
   21863       342580 :     return global_m1 ? -1 : 0;
   21864              : 
   21865      2066580 :   if (global_m1 && !DECL_MODULE_ATTACH_P (not_tmpl))
   21866              :     return -1;
   21867              : 
   21868       117549 :   int mod = !DECL_MODULE_IMPORT_P (not_tmpl) ? 0 : get_importing_module (owner);
   21869       117549 :   gcc_checking_assert (!global_m1 || !(*modules)[mod]->is_header ());
   21870              :   return mod;
   21871              : }
   21872              : 
   21873              : /* DECL is imported, return which module imported it.
   21874              :    If FLEXIBLE, return -1 if not found, otherwise checking ICE.  */
   21875              : 
   21876              : unsigned
   21877        15876 : get_importing_module (tree decl, bool flexible)
   21878              : {
   21879        15876 :   unsigned index = import_entity_index (decl, flexible);
   21880        15876 :   if (index == ~(~0u >> 1))
   21881              :     return -1;
   21882        15876 :   module_state *module = import_entity_module (index);
   21883              : 
   21884        15876 :   return module->mod;
   21885              : }
   21886              : 
   21887              : /* Is it permissible to redeclare OLDDECL with NEWDECL.
   21888              : 
   21889              :    If NEWDECL is NULL, assumes that OLDDECL will be redeclared using
   21890              :    the current scope's module and attachment.  */
   21891              : 
   21892              : bool
   21893       170355 : module_may_redeclare (tree olddecl, tree newdecl)
   21894              : {
   21895       170355 :   tree decl = olddecl;
   21896       228793 :   for (;;)
   21897              :     {
   21898       199574 :       tree ctx = CP_DECL_CONTEXT (decl);
   21899       199574 :       if (TREE_CODE (ctx) == NAMESPACE_DECL)
   21900              :         // Found the namespace-scope decl.
   21901              :         break;
   21902        30436 :       if (!CLASS_TYPE_P (ctx))
   21903              :         // We've met a non-class scope.  Such a thing is not
   21904              :         // reopenable, so we must be ok.
   21905              :         return true;
   21906        29219 :       decl = TYPE_NAME (ctx);
   21907        29219 :     }
   21908              : 
   21909       169138 :   int use_tpl = 0;
   21910       169138 :   if (node_template_info (STRIP_TEMPLATE (decl), use_tpl) && use_tpl)
   21911              :     // Specializations of any kind can be redeclared anywhere.
   21912              :     // FIXME: Should we be checking this in more places on the scope chain?
   21913              :     return true;
   21914              : 
   21915       120265 :   module_state *old_mod = get_primary (this_module ());
   21916       120265 :   module_state *new_mod = old_mod;
   21917              : 
   21918       120265 :   tree old_origin = get_originating_module_decl (decl);
   21919       120265 :   tree old_inner = STRIP_TEMPLATE (old_origin);
   21920       120265 :   bool olddecl_attached_p = (DECL_LANG_SPECIFIC (old_inner)
   21921       189892 :                              && DECL_MODULE_ATTACH_P (old_inner));
   21922       189892 :   if (DECL_LANG_SPECIFIC (old_inner) && DECL_MODULE_IMPORT_P (old_inner))
   21923              :     {
   21924          843 :       unsigned index = import_entity_index (old_origin);
   21925          843 :       old_mod = get_primary (import_entity_module (index));
   21926              :     }
   21927              : 
   21928       120265 :   bool newdecl_attached_p = module_attach_p ();
   21929       120265 :   if (newdecl)
   21930              :     {
   21931        29962 :       tree new_origin = get_originating_module_decl (newdecl);
   21932        29962 :       tree new_inner = STRIP_TEMPLATE (new_origin);
   21933        29962 :       newdecl_attached_p = (DECL_LANG_SPECIFIC (new_inner)
   21934        58320 :                             && DECL_MODULE_ATTACH_P (new_inner));
   21935        58320 :       if (DECL_LANG_SPECIFIC (new_inner) && DECL_MODULE_IMPORT_P (new_inner))
   21936              :         {
   21937          175 :           unsigned index = import_entity_index (new_origin);
   21938          175 :           new_mod = get_primary (import_entity_module (index));
   21939              :         }
   21940              :     }
   21941              : 
   21942              :   /* Module attachment needs to match.  */
   21943       120265 :   if (olddecl_attached_p == newdecl_attached_p)
   21944              :     {
   21945       120121 :       if (!olddecl_attached_p)
   21946              :         /* Both are GM entities, OK.  */
   21947              :         return true;
   21948              : 
   21949         1401 :       if (new_mod == old_mod)
   21950              :         /* Both attached to same named module, OK.  */
   21951              :         return true;
   21952              :     }
   21953              : 
   21954              :   /* Attached to different modules, error.  */
   21955          171 :   decl = newdecl ? newdecl : olddecl;
   21956          171 :   location_t loc = newdecl ? DECL_SOURCE_LOCATION (newdecl) : input_location;
   21957          171 :   if (DECL_IS_UNDECLARED_BUILTIN (olddecl))
   21958              :     {
   21959            3 :       if (newdecl_attached_p)
   21960            3 :         error_at (loc, "declaring %qD in module %qs conflicts with builtin "
   21961              :                   "in global module", decl, new_mod->get_flatname ());
   21962              :       else
   21963            0 :         error_at (loc, "declaration %qD conflicts with builtin", decl);
   21964              :     }
   21965          333 :   else if (DECL_LANG_SPECIFIC (old_inner) && DECL_MODULE_IMPORT_P (old_inner))
   21966              :     {
   21967          153 :       auto_diagnostic_group d;
   21968          153 :       if (newdecl_attached_p)
   21969           75 :         error_at (loc, "redeclaring %qD in module %qs conflicts with import",
   21970              :                   decl, new_mod->get_flatname ());
   21971              :       else
   21972           78 :         error_at (loc, "redeclaring %qD in global module conflicts with import",
   21973              :                   decl);
   21974              : 
   21975          153 :       if (olddecl_attached_p)
   21976          105 :         inform (DECL_SOURCE_LOCATION (olddecl),
   21977              :                 "import declared attached to module %qs",
   21978              :                 old_mod->get_flatname ());
   21979              :       else
   21980           48 :         inform (DECL_SOURCE_LOCATION (olddecl),
   21981              :                 "import declared in global module");
   21982          153 :     }
   21983              :   else
   21984              :     {
   21985           15 :       auto_diagnostic_group d;
   21986           15 :       if (newdecl_attached_p)
   21987           15 :         error_at (loc, "conflicting declaration of %qD in module %qs",
   21988              :                   decl, new_mod->get_flatname ());
   21989              :       else
   21990            0 :         error_at (loc, "conflicting declaration of %qD in global module",
   21991              :                   decl);
   21992              : 
   21993           15 :       if (olddecl_attached_p)
   21994            0 :         inform (DECL_SOURCE_LOCATION (olddecl),
   21995              :                 "previously declared in module %qs",
   21996              :                 old_mod->get_flatname ());
   21997              :       else
   21998           15 :         inform (DECL_SOURCE_LOCATION (olddecl),
   21999              :                 "previously declared in global module");
   22000           15 :     }
   22001              :   return false;
   22002              : }
   22003              : 
   22004              : /* DECL is being created by this TU.  Record it came from here.  We
   22005              :    record module purview, so we can see if partial or explicit
   22006              :    specialization needs to be written out, even though its purviewness
   22007              :    comes from the most general template.  */
   22008              : 
   22009              : void
   22010    932342388 : set_instantiating_module (tree decl)
   22011              : {
   22012    932342388 :   gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
   22013              :               || VAR_P (decl)
   22014              :               || TREE_CODE (decl) == TYPE_DECL
   22015              :               || TREE_CODE (decl) == CONCEPT_DECL
   22016              :               || TREE_CODE (decl) == TEMPLATE_DECL
   22017              :               || TREE_CODE (decl) == CONST_DECL
   22018              :               || (TREE_CODE (decl) == NAMESPACE_DECL
   22019              :                   && DECL_NAMESPACE_ALIAS (decl)));
   22020              : 
   22021    932342388 :   if (!modules_p ())
   22022              :     return;
   22023              : 
   22024      3070740 :   decl = STRIP_TEMPLATE (decl);
   22025              : 
   22026      3070740 :   if (!DECL_LANG_SPECIFIC (decl) && module_purview_p ())
   22027       361151 :     retrofit_lang_decl (decl);
   22028              : 
   22029      3070740 :   if (DECL_LANG_SPECIFIC (decl))
   22030              :     {
   22031      2425969 :       DECL_MODULE_PURVIEW_P (decl) = module_purview_p ();
   22032              :       /* If this was imported, we'll still be in the entity_hash.  */
   22033      2425969 :       DECL_MODULE_IMPORT_P (decl) = false;
   22034              :     }
   22035              : }
   22036              : 
   22037              : /* If DECL is a class member, whose class is not defined in this TU
   22038              :    (it was imported), remember this decl.  */
   22039              : 
   22040              : void
   22041       125663 : set_defining_module (tree decl)
   22042              : {
   22043       125663 :   gcc_checking_assert (!DECL_LANG_SPECIFIC (decl)
   22044              :                        || !DECL_MODULE_IMPORT_P (decl));
   22045              : 
   22046       125663 :   if (module_maybe_has_cmi_p ())
   22047              :     {
   22048              :       /* We need to track all declarations within a module, not just those
   22049              :          in the module purview, because we don't necessarily know yet if
   22050              :          this module will require a CMI while in the global fragment.  */
   22051        86554 :       tree ctx = DECL_CONTEXT (decl);
   22052        86554 :       if (ctx
   22053        86554 :           && (TREE_CODE (ctx) == RECORD_TYPE || TREE_CODE (ctx) == UNION_TYPE)
   22054        17022 :           && DECL_LANG_SPECIFIC (TYPE_NAME (ctx))
   22055        97742 :           && DECL_MODULE_IMPORT_P (TYPE_NAME (ctx)))
   22056              :         {
   22057              :           /* This entity's context is from an import.  We may need to
   22058              :              record this entity to make sure we emit it in the CMI.
   22059              :              Template specializations are in the template hash tables,
   22060              :              so we don't need to record them here as well.  */
   22061           39 :           int use_tpl = -1;
   22062           39 :           tree ti = node_template_info (decl, use_tpl);
   22063           39 :           if (use_tpl <= 0)
   22064              :             {
   22065           39 :               if (ti)
   22066              :                 {
   22067           21 :                   gcc_checking_assert (!use_tpl);
   22068              :                   /* Get to the TEMPLATE_DECL.  */
   22069           21 :                   decl = TI_TEMPLATE (ti);
   22070              :                 }
   22071              : 
   22072              :               /* Record it on the class_members list.  */
   22073           39 :               vec_safe_push (class_members, decl);
   22074              :             }
   22075              :         }
   22076              :     }
   22077       125663 : }
   22078              : 
   22079              : /* Also remember DECL if it's a newly declared class template partial
   22080              :    specialization, because these are not necessarily added to the
   22081              :    instantiation tables.  */
   22082              : 
   22083              : void
   22084      7997518 : set_defining_module_for_partial_spec (tree decl)
   22085              : {
   22086      7997518 :   if (module_maybe_has_cmi_p ()
   22087        21665 :       && DECL_IMPLICIT_TYPEDEF_P (decl)
   22088      8015872 :       && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
   22089        18354 :     vec_safe_push (partial_specializations, decl);
   22090      7997518 : }
   22091              : 
   22092              : /* Record that DECL is declared in this TU, and note attachment and
   22093              :    exporting for namespace-scope entities.  FRIEND_P is true if
   22094              :    this is a friend declaration.  */
   22095              : 
   22096              : void
   22097    301133866 : set_originating_module (tree decl, bool friend_p ATTRIBUTE_UNUSED)
   22098              : {
   22099    301133866 :   set_instantiating_module (decl);
   22100              : 
   22101              :   /* DECL_CONTEXT may not be set yet when we're called for
   22102              :      non-namespace-scope entities.  */
   22103    301133866 :   if (!DECL_CONTEXT (decl) || !DECL_NAMESPACE_SCOPE_P (decl))
   22104              :     return;
   22105              : 
   22106    112085993 :   gcc_checking_assert (friend_p || decl == get_originating_module_decl (decl));
   22107              : 
   22108    112085993 :   if (module_attach_p ())
   22109              :     {
   22110         4222 :       retrofit_lang_decl (decl);
   22111         4222 :       DECL_MODULE_ATTACH_P (decl) = true;
   22112              :     }
   22113              : 
   22114              :   /* It is ill-formed to export a declaration with internal linkage.  However,
   22115              :      at the point this function is called we don't yet always know whether this
   22116              :      declaration has internal linkage; instead we defer this check for callers
   22117              :      to do once visibility has been determined.  */
   22118    112085993 :   if (module_exporting_p ())
   22119       154965 :     DECL_MODULE_EXPORT_P (decl) = true;
   22120              : }
   22121              : 
   22122              : /* Checks whether DECL within a module unit has valid linkage for its kind.
   22123              :    Must be called after visibility for DECL has been finalised.  */
   22124              : 
   22125              : void
   22126    377922411 : check_module_decl_linkage (tree decl)
   22127              : {
   22128    377922411 :   if (!module_has_cmi_p ())
   22129              :     return;
   22130              : 
   22131              :   /* A header unit shall not contain a definition of a non-inline function
   22132              :      or variable (not template) whose name has external linkage.  */
   22133       521441 :   if (header_module_p ()
   22134       470710 :       && !processing_template_decl
   22135       155789 :       && ((TREE_CODE (decl) == FUNCTION_DECL
   22136        92098 :            && !DECL_DECLARED_INLINE_P (decl))
   22137       120499 :           || (TREE_CODE (decl) == VAR_DECL
   22138        36231 :               && !DECL_INLINE_VAR_P (decl)))
   22139        44405 :       && decl_defined_p (decl)
   22140         4397 :       && !(DECL_LANG_SPECIFIC (decl)
   22141         4397 :            && DECL_TEMPLATE_INSTANTIATION (decl))
   22142       525838 :       && decl_linkage (decl) == lk_external)
   22143           60 :     error_at (DECL_SOURCE_LOCATION (decl),
   22144              :               "external linkage definition of %qD in header module must "
   22145              :               "be declared %<inline%>", decl);
   22146              : 
   22147              :   /* An internal-linkage declaration cannot be generally be exported.
   22148              :      But it's OK to export any declaration from a header unit, including
   22149              :      internal linkage declarations.  */
   22150       521441 :   if (!header_module_p () && DECL_MODULE_EXPORT_P (decl))
   22151              :     {
   22152              :       /* Let's additionally treat any exported declaration within an
   22153              :          internal namespace as exporting a declaration with internal
   22154              :          linkage, as this would also implicitly export the internal
   22155              :          linkage namespace.  */
   22156         1955 :       if (decl_anon_ns_mem_p (decl))
   22157              :         {
   22158           50 :           error_at (DECL_SOURCE_LOCATION (decl),
   22159              :                     "exporting declaration %qD declared in unnamed namespace",
   22160              :                     decl);
   22161           50 :           DECL_MODULE_EXPORT_P (decl) = false;
   22162              :         }
   22163         1905 :       else if (decl_linkage (decl) == lk_internal)
   22164              :         {
   22165           17 :           error_at (DECL_SOURCE_LOCATION (decl),
   22166              :                     "exporting declaration %qD with internal linkage", decl);
   22167           17 :           DECL_MODULE_EXPORT_P (decl) = false;
   22168              :         }
   22169              :     }
   22170              : }
   22171              : 
   22172              : /* Given a scope CTX, find the scope we want to attach the key to,
   22173              :    or NULL if no key scope is required.  */
   22174              : 
   22175              : static tree
   22176         2352 : adjust_key_scope (tree ctx)
   22177              : {
   22178              :   /* For members, key it to the containing type to handle deduplication
   22179              :      correctly.  For fields, this is necessary as FIELD_DECLs have no
   22180              :      dep and so would only be streamed after the lambda type, defeating
   22181              :      our ability to merge them.
   22182              : 
   22183              :      Other class-scope key decls might depend on the type of the lambda
   22184              :      but be within the same cluster; we need to ensure that we never
   22185              :      first see the key decl while streaming the lambda type as merging
   22186              :      would then fail when comparing the partially-streamed lambda type
   22187              :      of the key decl with the existing (PR c++/122310).
   22188              : 
   22189              :      Perhaps sort_cluster can be adjusted to handle this better, but
   22190              :      this is a simple workaround (and might down on the number of
   22191              :      entries in keyed_table as a bonus).  */
   22192         3848 :   while (!DECL_NAMESPACE_SCOPE_P (ctx))
   22193         2992 :     if (DECL_CLASS_SCOPE_P (ctx))
   22194         1382 :       ctx = TYPE_NAME (DECL_CONTEXT (ctx));
   22195              :     else
   22196          114 :       ctx = DECL_CONTEXT (ctx);
   22197              : 
   22198         2352 :   return ctx;
   22199              : }
   22200              : 
   22201              : /* DECL is keyed to CTX for odr purposes.  */
   22202              : 
   22203              : void
   22204      1700611 : maybe_key_decl (tree ctx, tree decl)
   22205              : {
   22206      1700611 :   if (!modules_p ())
   22207              :     return;
   22208              : 
   22209              :   /* We only need to deal here with decls attached to var, field,
   22210              :      parm, type, function, or concept decls.  */
   22211         9697 :   if (TREE_CODE (ctx) != VAR_DECL
   22212         9697 :       && TREE_CODE (ctx) != FIELD_DECL
   22213              :       && TREE_CODE (ctx) != PARM_DECL
   22214              :       && TREE_CODE (ctx) != TYPE_DECL
   22215              :       && TREE_CODE (ctx) != FUNCTION_DECL
   22216              :       && TREE_CODE (ctx) != CONCEPT_DECL)
   22217              :     return;
   22218              : 
   22219        19391 :   gcc_checking_assert (LAMBDA_TYPE_P (TREE_TYPE (decl))
   22220              :                        || TREE_CODE (ctx) == FUNCTION_DECL);
   22221              : 
   22222              :   /* We don't need to use the keyed map for functions with definitions,
   22223              :      as we can instead use the MK_local_type handling for streaming.  */
   22224         9697 :   if (TREE_CODE (ctx) == FUNCTION_DECL
   22225         9697 :       && (has_definition (ctx)
   22226              :           /* If we won't be streaming this definition there's also no
   22227              :              need to record the key, as it will not be useful for merging
   22228              :              (this function is non-inline and so a matching declaration
   22229              :              will always be an ODR violation anyway).  */
   22230          120 :           || !module_maybe_has_cmi_p ()))
   22231              :     return;
   22232              : 
   22233          580 :   ctx = adjust_key_scope (ctx);
   22234              : 
   22235          580 :   if (!keyed_table)
   22236          154 :     keyed_table = new keyed_map_t (EXPERIMENT (1, 400));
   22237              : 
   22238          580 :   auto &vec = keyed_table->get_or_insert (ctx);
   22239          580 :   if (!vec.length ())
   22240              :     {
   22241          530 :       retrofit_lang_decl (ctx);
   22242          530 :       DECL_MODULE_KEYED_DECLS_P (ctx) = true;
   22243              :     }
   22244          580 :   if (CHECKING_P)
   22245          736 :     for (tree t : vec)
   22246           56 :       gcc_checking_assert (t != decl);
   22247              : 
   22248          580 :   vec.safe_push (decl);
   22249              : }
   22250              : 
   22251              : /* Find the scope that the local type or lambda DECL is keyed to, if any.  */
   22252              : 
   22253              : static tree
   22254         1808 : get_keyed_decl_scope (tree decl)
   22255              : {
   22256         1808 :   gcc_checking_assert (DECL_IMPLICIT_TYPEDEF_P (STRIP_TEMPLATE (decl)));
   22257              : 
   22258         5424 :   tree scope = (LAMBDA_TYPE_P (TREE_TYPE (decl))
   22259         3388 :                 ? LAMBDA_TYPE_EXTRA_SCOPE (TREE_TYPE (decl))
   22260         1808 :                 : CP_DECL_CONTEXT (decl));
   22261         1808 :   if (!scope)
   22262              :     return NULL_TREE;
   22263              : 
   22264         1772 :   gcc_checking_assert (TREE_CODE (scope) == VAR_DECL
   22265              :                        || TREE_CODE (scope) == FIELD_DECL
   22266              :                        || TREE_CODE (scope) == PARM_DECL
   22267              :                        || TREE_CODE (scope) == TYPE_DECL
   22268              :                        || (TREE_CODE (scope) == FUNCTION_DECL
   22269              :                            && !has_definition (scope))
   22270              :                        || TREE_CODE (scope) == CONCEPT_DECL);
   22271              : 
   22272         1772 :   scope = adjust_key_scope (scope);
   22273              : 
   22274         3544 :   gcc_checking_assert (scope
   22275              :                        && DECL_LANG_SPECIFIC (scope)
   22276              :                        && DECL_MODULE_KEYED_DECLS_P (scope));
   22277              :   return scope;
   22278              : }
   22279              : 
   22280              : /* DECL is an instantiated friend that should be attached to the same
   22281              :    module that ORIG is.  */
   22282              : 
   22283              : void
   22284      2525727 : propagate_defining_module (tree decl, tree orig)
   22285              : {
   22286      2525727 :   if (!modules_p ())
   22287              :     return;
   22288              : 
   22289         6023 :   tree not_tmpl = STRIP_TEMPLATE (orig);
   22290        12025 :   if (DECL_LANG_SPECIFIC (not_tmpl) && DECL_MODULE_ATTACH_P (not_tmpl))
   22291              :     {
   22292          126 :       tree inner = STRIP_TEMPLATE (decl);
   22293          126 :       retrofit_lang_decl (inner);
   22294          126 :       DECL_MODULE_ATTACH_P (inner) = true;
   22295              :     }
   22296              : 
   22297        12025 :   if (DECL_LANG_SPECIFIC (not_tmpl) && DECL_MODULE_IMPORT_P (not_tmpl))
   22298              :     {
   22299          387 :       bool exists = imported_temploid_friends->put (decl, orig);
   22300              : 
   22301              :       /* We should only be called if lookup for an existing decl
   22302              :          failed, in which case there shouldn't already be an entry
   22303              :          in the map.  */
   22304          387 :       gcc_assert (!exists);
   22305              :     }
   22306              : }
   22307              : 
   22308              : /* NEWDECL matched with OLDDECL, transfer defining module information
   22309              :    onto OLDDECL.  We've already validated attachment matches.  */
   22310              : 
   22311              : void
   22312     19370188 : transfer_defining_module (tree olddecl, tree newdecl)
   22313              : {
   22314     19370188 :   if (!modules_p ())
   22315              :     return;
   22316              : 
   22317        49802 :   tree old_inner = STRIP_TEMPLATE (olddecl);
   22318        49802 :   tree new_inner = STRIP_TEMPLATE (newdecl);
   22319              : 
   22320        49802 :   if (DECL_LANG_SPECIFIC (new_inner))
   22321              :     {
   22322        49690 :       gcc_checking_assert (DECL_LANG_SPECIFIC (old_inner));
   22323        49690 :       if (DECL_MODULE_PURVIEW_P (new_inner))
   22324        22337 :         DECL_MODULE_PURVIEW_P (old_inner) = true;
   22325        49690 :       if (!DECL_MODULE_IMPORT_P (new_inner))
   22326        49690 :         DECL_MODULE_IMPORT_P (old_inner) = false;
   22327              :     }
   22328              : 
   22329        49802 :   if (tree *p = imported_temploid_friends->get (newdecl))
   22330              :     {
   22331           70 :       tree orig = *p;
   22332           70 :       tree &slot = imported_temploid_friends->get_or_insert (olddecl);
   22333           70 :       if (!slot)
   22334           47 :         slot = orig;
   22335           23 :       else if (slot != orig)
   22336              :         /* This can happen when multiple classes declare the same
   22337              :            friend function (e.g. g++.dg/modules/tpl-friend-4);
   22338              :            make sure we at least attach to the same module.  */
   22339            3 :         gcc_checking_assert (get_originating_module (slot)
   22340              :                              == get_originating_module (orig));
   22341              :     }
   22342              : }
   22343              : 
   22344              : /* DECL is being freed, clear data we don't need anymore.  */
   22345              : 
   22346              : void
   22347        35566 : remove_defining_module (tree decl)
   22348              : {
   22349        35566 :   if (!modules_p ())
   22350              :     return;
   22351              : 
   22352        35566 :   if (imported_temploid_friends)
   22353        35566 :     imported_temploid_friends->remove (decl);
   22354              : }
   22355              : 
   22356              : /* Create the flat name string.  It is simplest to have it handy.  */
   22357              : 
   22358              : void
   22359         6288 : module_state::set_flatname ()
   22360              : {
   22361         6288 :   gcc_checking_assert (!flatname);
   22362         6288 :   if (parent)
   22363              :     {
   22364          668 :       auto_vec<tree,5> ids;
   22365          668 :       size_t len = 0;
   22366          668 :       char const *primary = NULL;
   22367          668 :       size_t pfx_len = 0;
   22368              : 
   22369          668 :       for (module_state *probe = this;
   22370         1639 :            probe;
   22371          971 :            probe = probe->parent)
   22372         1471 :         if (is_partition () && !probe->is_partition ())
   22373              :           {
   22374          500 :             primary = probe->get_flatname ();
   22375          500 :             pfx_len = strlen (primary);
   22376          500 :             break;
   22377              :           }
   22378              :         else
   22379              :           {
   22380          971 :             ids.safe_push (probe->name);
   22381          971 :             len += IDENTIFIER_LENGTH (probe->name) + 1;
   22382              :           }
   22383              : 
   22384          668 :       char *flat = XNEWVEC (char, pfx_len + len + is_partition ());
   22385          668 :       flatname = flat;
   22386              : 
   22387          668 :       if (primary)
   22388              :         {
   22389          500 :           memcpy (flat, primary, pfx_len);
   22390          500 :           flat += pfx_len;
   22391          500 :           *flat++ = ':';
   22392              :         }
   22393              : 
   22394         1639 :       for (unsigned len = 0; ids.length ();)
   22395              :         {
   22396          971 :           if (len)
   22397          303 :             flat[len++] = '.';
   22398          971 :           tree elt = ids.pop ();
   22399          971 :           unsigned l = IDENTIFIER_LENGTH (elt);
   22400          971 :           memcpy (flat + len, IDENTIFIER_POINTER (elt), l + 1);
   22401          971 :           len += l;
   22402              :         }
   22403          668 :     }
   22404         5620 :   else if (is_header ())
   22405         1893 :     flatname = TREE_STRING_POINTER (name);
   22406              :   else
   22407         3727 :     flatname = IDENTIFIER_POINTER (name);
   22408         6288 : }
   22409              : 
   22410              : /* Open the GCM file and prepare to read.  Return whether that was
   22411              :    successful.  */
   22412              : 
   22413              : bool
   22414         3035 : module_state::open_slurp (cpp_reader *reader)
   22415              : {
   22416         3035 :   if (slurp)
   22417              :     return true;
   22418              : 
   22419         2989 :   if (lazy_open >= lazy_limit)
   22420            9 :     freeze_an_elf ();
   22421              : 
   22422         2989 :   int fd = -1;
   22423         2989 :   int e = ENOENT;
   22424         2989 :   if (filename)
   22425              :     {
   22426         2989 :       const char *file = maybe_add_cmi_prefix (filename);
   22427         3518 :       dump () && dump ("CMI is %s", file);
   22428         2989 :       if (note_module_cmi_yes || inform_cmi_p)
   22429           12 :         inform (loc, "reading CMI %qs", file);
   22430              :       /* Add the CMI file to the dependency tracking. */
   22431         2989 :       if (cpp_get_deps (reader))
   22432           15 :         deps_add_dep (cpp_get_deps (reader), file);
   22433         2989 :       fd = open (file, O_RDONLY | O_CLOEXEC | O_BINARY);
   22434         2989 :       e = errno;
   22435              :     }
   22436              : 
   22437         2989 :   gcc_checking_assert (!slurp);
   22438         5954 :   slurp = new slurping (new elf_in (fd, e));
   22439              : 
   22440         2989 :   bool ok = from ()->begin (loc);
   22441         2989 :   if (ok)
   22442              :     {
   22443         2965 :       lazy_open++;
   22444         2965 :       slurp->lru = ++lazy_lru;
   22445              :     }
   22446              :   return ok;
   22447              : }
   22448              : 
   22449              : /* Return whether importing this GCM would work without an error in
   22450              :    read_config.  */
   22451              : 
   22452              : bool
   22453           52 : module_state::check_importable (cpp_reader *reader)
   22454              : {
   22455           52 :   if (loadedness > ML_CONFIG)
   22456              :     return true;
   22457           49 :   if (!open_slurp (reader))
   22458              :     return false;
   22459           46 :   module_state_config config;
   22460           46 :   return read_config (config, /*complain*/false);
   22461              : }
   22462              : 
   22463              : /* Read the CMI file for a module.  */
   22464              : 
   22465              : bool
   22466         2986 : module_state::do_import (cpp_reader *reader, bool outermost)
   22467              : {
   22468         2986 :   gcc_assert (global_namespace == current_scope () && loadedness == ML_NONE);
   22469              : 
   22470              :   /* If this TU is a partition of the module we're importing,
   22471              :      that module is the primary module interface.  */
   22472         2986 :   if (this_module ()->is_partition ()
   22473         3037 :       && this == get_primary (this_module ()))
   22474            9 :     module_p = true;
   22475              : 
   22476         2986 :   loc = linemap_module_loc (line_table, loc, get_flatname ());
   22477              : 
   22478         2986 :   bool ok = open_slurp (reader);
   22479         2986 :   if (!from ()->get_error ())
   22480              :     {
   22481         2965 :       announce ("importing");
   22482         2965 :       loadedness = ML_CONFIG;
   22483         2965 :       ok = read_initial (reader);
   22484              :     }
   22485              : 
   22486         2986 :   gcc_assert (slurp->current == ~0u);
   22487              : 
   22488         2986 :   return check_read (outermost, ok);
   22489              : }
   22490              : 
   22491              : /* Attempt to increase the file descriptor limit.  */
   22492              : 
   22493              : static bool
   22494         4860 : try_increase_lazy (unsigned want)
   22495              : {
   22496         4860 :   gcc_checking_assert (lazy_open >= lazy_limit);
   22497              : 
   22498              :   /* If we're increasing, saturate at hard limit.  */
   22499         4860 :   if (want > lazy_hard_limit && lazy_limit < lazy_hard_limit)
   22500         4860 :     want = lazy_hard_limit;
   22501              : 
   22502              : #if HAVE_SETRLIMIT
   22503         4860 :   if ((!lazy_limit || !param_lazy_modules)
   22504         4848 :       && lazy_hard_limit
   22505         4848 :       && want <= lazy_hard_limit)
   22506              :     {
   22507         4848 :       struct rlimit rlimit;
   22508         4848 :       rlimit.rlim_cur = want + LAZY_HEADROOM;
   22509         4848 :       rlimit.rlim_max = lazy_hard_limit + LAZY_HEADROOM;
   22510         4848 :       if (!setrlimit (RLIMIT_NOFILE, &rlimit))
   22511         4848 :         lazy_limit = want;
   22512              :     }
   22513              : #endif
   22514              : 
   22515         4860 :   return lazy_open < lazy_limit;
   22516              : }
   22517              : 
   22518              : /* Pick a victim module to freeze its reader.  */
   22519              : 
   22520              : void
   22521           12 : module_state::freeze_an_elf ()
   22522              : {
   22523           12 :   if (try_increase_lazy (lazy_open * 2))
   22524              :     return;
   22525              : 
   22526           12 :   module_state *victim = NULL;
   22527           12 :   for (unsigned ix = modules->length (); ix--;)
   22528              :     {
   22529           30 :       module_state *candidate = (*modules)[ix];
   22530           30 :       if (candidate && candidate->slurp && candidate->slurp->lru
   22531           60 :           && candidate->from ()->is_freezable ()
   22532           39 :           && (!victim || victim->slurp->lru > candidate->slurp->lru))
   22533              :         victim = candidate;
   22534              :     }
   22535              : 
   22536           12 :   if (victim)
   22537              :     {
   22538           18 :       dump () && dump ("Freezing '%s'", victim->filename);
   22539            9 :       if (victim->slurp->macro_defs.size)
   22540              :         /* Save the macro definitions to a buffer.  */
   22541            0 :         victim->from ()->preserve (victim->slurp->macro_defs);
   22542            9 :       if (victim->slurp->macro_tbl.size)
   22543              :         /* Save the macro definitions to a buffer.  */
   22544            0 :         victim->from ()->preserve (victim->slurp->macro_tbl);
   22545            9 :       victim->from ()->freeze ();
   22546            9 :       lazy_open--;
   22547              :     }
   22548              :   else
   22549            3 :     dump () && dump ("No module available for freezing");
   22550              : }
   22551              : 
   22552              : /* Load the lazy slot *MSLOT, INDEX'th slot of the module.  */
   22553              : 
   22554              : bool
   22555        57687 : module_state::lazy_load (unsigned index, binding_slot *mslot)
   22556              : {
   22557        57687 :   unsigned n = dump.push (this);
   22558              : 
   22559        57687 :   gcc_checking_assert (function_depth);
   22560              : 
   22561        57687 :   unsigned cookie = mslot->get_lazy ();
   22562        57687 :   unsigned snum = cookie >> 2;
   22563        58067 :   dump () && dump ("Loading entity %M[%u] section:%u", this, index, snum);
   22564              : 
   22565        57687 :   bool ok = load_section (snum, mslot);
   22566              : 
   22567        57687 :   dump.pop (n);
   22568              : 
   22569        57687 :   return ok;
   22570              : }
   22571              : 
   22572              : /* Load MOD's binding for NS::ID into *MSLOT.  *MSLOT contains the
   22573              :    lazy cookie.  OUTER is true if this is the outermost lazy, (used
   22574              :    for diagnostics).  */
   22575              : 
   22576              : void
   22577         5859 : lazy_load_binding (unsigned mod, tree ns, tree id, binding_slot *mslot)
   22578              : {
   22579         5859 :   int count = errorcount + warningcount;
   22580              : 
   22581         5859 :   bool timer_running = timevar_cond_start (TV_MODULE_IMPORT);
   22582              : 
   22583              :   /* Make sure lazy loading from a template context behaves as if
   22584              :      from a non-template context.  */
   22585         5859 :   processing_template_decl_sentinel ptds;
   22586              : 
   22587              :   /* Stop GC happening, even in outermost loads (because our caller
   22588              :      could well be building up a lookup set).  */
   22589         5859 :   function_depth++;
   22590              : 
   22591         5859 :   gcc_checking_assert (mod);
   22592         5859 :   module_state *module = (*modules)[mod];
   22593         5859 :   unsigned n = dump.push (module);
   22594              : 
   22595         5859 :   unsigned snum = mslot->get_lazy ();
   22596         6175 :   dump () && dump ("Lazily binding %P@%N section:%u", ns, id,
   22597              :                    module->name, snum);
   22598              : 
   22599         5859 :   bool ok = !recursive_lazy (snum);
   22600         5859 :   if (ok)
   22601              :     {
   22602         5859 :       ok = module->load_section (snum, mslot);
   22603         5859 :       lazy_snum = 0;
   22604         5859 :       post_load_processing ();
   22605              :     }
   22606              : 
   22607         5859 :   dump.pop (n);
   22608              : 
   22609         5859 :   function_depth--;
   22610              : 
   22611         5859 :   timevar_cond_stop (TV_MODULE_IMPORT, timer_running);
   22612              : 
   22613         5859 :   if (!ok)
   22614            0 :     fatal_error (input_location,
   22615            0 :                  module->is_header ()
   22616              :                  ? G_("failed to load binding %<%E%s%E%>")
   22617              :                  : G_("failed to load binding %<%E%s%E@%s%>"),
   22618            0 :                  ns, &"::"[ns == global_namespace ? 2 : 0], id,
   22619              :                  module->get_flatname ());
   22620              : 
   22621         5859 :   if (count != errorcount + warningcount)
   22622           27 :     inform (input_location,
   22623           27 :             module->is_header ()
   22624              :             ? G_("during load of binding %<%E%s%E%>")
   22625              :             : G_("during load of binding %<%E%s%E@%s%>"),
   22626           27 :             ns, &"::"[ns == global_namespace ? 2 : 0], id,
   22627              :             module->get_flatname ());
   22628         5859 : }
   22629              : 
   22630              : /* Load any pending entities keyed to NS and NAME.
   22631              :    Used to find pending types if we don't yet have a decl built.  */
   22632              : 
   22633              : void
   22634     30617428 : lazy_load_pendings (tree ns, tree name)
   22635              : {
   22636              :   /* Make sure lazy loading from a template context behaves as if
   22637              :      from a non-template context.  */
   22638     30617428 :   processing_template_decl_sentinel ptds;
   22639              : 
   22640     30617428 :   pending_key key;
   22641     30617428 :   key.ns = ns;
   22642     30617428 :   key.id = name;
   22643              : 
   22644     30617428 :   auto *pending_vec = pending_table ? pending_table->get (key) : nullptr;
   22645     30612576 :   if (!pending_vec)
   22646     30611383 :     return;
   22647              : 
   22648         6045 :   int count = errorcount + warningcount;
   22649              : 
   22650         6045 :   bool timer_running = timevar_cond_start (TV_MODULE_IMPORT);
   22651         6045 :   bool ok = !recursive_lazy ();
   22652         6045 :   if (ok)
   22653              :     {
   22654         6045 :       function_depth++; /* Prevent GC */
   22655         6045 :       unsigned n = dump.push (NULL);
   22656         6511 :       dump () && dump ("Reading %u pending entities keyed to %P",
   22657              :                        pending_vec->length (), key.ns, key.id);
   22658         6045 :       for (unsigned ix = pending_vec->length (); ix--;)
   22659              :         {
   22660        68565 :           unsigned index = (*pending_vec)[ix];
   22661        68565 :           binding_slot *slot = &(*entity_ary)[index];
   22662              : 
   22663        68565 :           if (slot->is_lazy ())
   22664              :             {
   22665         6440 :               module_state *import = import_entity_module (index);
   22666         6440 :               if (!import->lazy_load (index - import->entity_lwm, slot))
   22667        68565 :                 ok = false;
   22668              :             }
   22669       136735 :           else if (dump ())
   22670              :             {
   22671          338 :               module_state *import = import_entity_module (index);
   22672          338 :               dump () && dump ("Entity %M[%u] already loaded",
   22673          338 :                                import, index - import->entity_lwm);
   22674              :             }
   22675              :         }
   22676              : 
   22677         6045 :       pending_table->remove (key);
   22678         6045 :       dump.pop (n);
   22679         6045 :       lazy_snum = 0;
   22680         6045 :       post_load_processing ();
   22681         6045 :       function_depth--;
   22682              :     }
   22683              : 
   22684         6045 :   timevar_cond_stop (TV_MODULE_IMPORT, timer_running);
   22685              : 
   22686         6045 :   if (!ok)
   22687            0 :     fatal_error (input_location, "failed to load pendings for %<%E%s%E%>",
   22688            0 :                  key.ns, &"::"[key.ns == global_namespace ? 2 : 0], key.id);
   22689              : 
   22690         6045 :   if (count != errorcount + warningcount)
   22691            0 :     inform (input_location, "during load of pendings for %<%E%s%E%>",
   22692            0 :             key.ns, &"::"[key.ns == global_namespace ? 2 : 0], key.id);
   22693     30617428 : }
   22694              : 
   22695              : /* Load any pending entities keyed to the top-key of DECL.  */
   22696              : 
   22697              : void
   22698     30538962 : lazy_load_pendings (tree decl)
   22699              : {
   22700     30538962 :   tree key_decl;
   22701     30538962 :   tree ns = find_pending_key (decl, &key_decl);
   22702     30538962 :   return lazy_load_pendings (ns, DECL_NAME (key_decl));
   22703              : }
   22704              : 
   22705              : static void
   22706         2708 : direct_import (module_state *import, cpp_reader *reader)
   22707              : {
   22708         2708 :   timevar_start (TV_MODULE_IMPORT);
   22709         2708 :   unsigned n = dump.push (import);
   22710              : 
   22711         2708 :   gcc_checking_assert (import->is_direct () && import->has_location ());
   22712         2708 :   if (import->loadedness == ML_NONE)
   22713         1777 :     if (!import->do_import (reader, true))
   22714            0 :       gcc_unreachable ();
   22715              : 
   22716         2671 :   this_module ()->set_import (import, import->exported_p);
   22717              : 
   22718         2671 :   if (import->loadedness < ML_LANGUAGE)
   22719              :     {
   22720         2590 :       if (!keyed_table)
   22721         2275 :         keyed_table = new keyed_map_t (EXPERIMENT (1, 400));
   22722         2590 :       import->read_language (true);
   22723              :     }
   22724              : 
   22725         2671 :   dump.pop (n);
   22726         2671 :   timevar_stop (TV_MODULE_IMPORT);
   22727         2671 : }
   22728              : 
   22729              : /* Import module IMPORT.  */
   22730              : 
   22731              : void
   22732         2478 : import_module (module_state *import, location_t from_loc, bool exporting_p,
   22733              :                tree, cpp_reader *reader)
   22734              : {
   22735              :   /* A non-partition implementation unit has no name.  */
   22736         2478 :   if (!this_module ()->name && this_module ()->parent == import)
   22737              :     {
   22738            3 :       auto_diagnostic_group d;
   22739            3 :       error_at (from_loc, "import of %qs within its own implementation unit",
   22740              :                 import->get_flatname());
   22741            3 :       inform (import->loc, "module declared here");
   22742            3 :       return;
   22743            3 :     }
   22744              : 
   22745         2475 :   if (!import->check_circular_import (from_loc))
   22746              :     return;
   22747              : 
   22748         2469 :   if (!import->is_header () && current_lang_depth ())
   22749              :     /* Only header units should appear inside language
   22750              :        specifications.  The std doesn't specify this, but I think
   22751              :        that's an error in resolving US 033, because language linkage
   22752              :        is also our escape clause to getting things into the global
   22753              :        module, so we don't want to confuse things by having to think
   22754              :        about whether 'extern "C++" { import foo; }' puts foo's
   22755              :        contents into the global module all of a sudden.  */
   22756            6 :     warning (0, "import of named module %qs inside language-linkage block",
   22757              :              import->get_flatname ());
   22758              : 
   22759         2469 :   if (exporting_p || module_exporting_p ())
   22760          322 :     import->exported_p = true;
   22761              : 
   22762         2469 :   if (import->loadedness != ML_NONE)
   22763              :     {
   22764          928 :       from_loc = ordinary_loc_of (line_table, from_loc);
   22765          928 :       linemap_module_reparent (line_table, import->loc, from_loc);
   22766              :     }
   22767              : 
   22768         2469 :   gcc_checking_assert (import->is_direct () && import->has_location ());
   22769              : 
   22770         2469 :   direct_import (import, reader);
   22771              : }
   22772              : 
   22773              : /* Declare the name of the current module to be NAME.  EXPORTING_p is
   22774              :    true if this TU is the exporting module unit.  */
   22775              : 
   22776              : void
   22777         3111 : declare_module (module_state *module, location_t from_loc, bool exporting_p,
   22778              :                 tree, cpp_reader *reader)
   22779              : {
   22780         3111 :   gcc_assert (global_namespace == current_scope ());
   22781              : 
   22782         3111 :   module_state *current = this_module ();
   22783         3111 :   if (module_purview_p () || module->loadedness > ML_CONFIG)
   22784              :     {
   22785            6 :       auto_diagnostic_group d;
   22786           12 :       error_at (from_loc, module_purview_p ()
   22787              :                 ? G_("module already declared")
   22788              :                 : G_("module already imported"));
   22789            6 :       if (module_purview_p ())
   22790            0 :         module = current;
   22791           12 :       inform (module->loc, module_purview_p ()
   22792              :               ? G_("module %qs declared here")
   22793              :               : G_("module %qs imported here"),
   22794              :               module->get_flatname ());
   22795            6 :       return;
   22796            6 :     }
   22797              : 
   22798         3105 :   gcc_checking_assert (module->is_module ());
   22799         3105 :   gcc_checking_assert (module->is_direct () && module->has_location ());
   22800              : 
   22801              :   /* Yer a module, 'arry.  */
   22802         3105 :   module_kind = module->is_header () ? MK_HEADER : MK_NAMED | MK_ATTACH;
   22803              : 
   22804              :   // Even in header units, we consider the decls to be purview
   22805         3105 :   module_kind |= MK_PURVIEW;
   22806              : 
   22807         3105 :   if (module->is_partition ())
   22808          202 :     module_kind |= MK_PARTITION;
   22809         3105 :   if (exporting_p)
   22810              :     {
   22811         2801 :       module->interface_p = true;
   22812         2801 :       module_kind |= MK_INTERFACE;
   22813              :     }
   22814              : 
   22815         3105 :   if (module_has_cmi_p ())
   22816              :     {
   22817              :       /* Copy the importing information we may have already done.  We
   22818              :          do not need to separate out the imports that only happen in
   22819              :          the GMF, inspite of what the literal wording of the std
   22820              :          might imply.  See p2191, the core list had a discussion
   22821              :          where the module implementors agreed that the GMF of a named
   22822              :          module is invisible to importers.  */
   22823         2866 :       module->imports = current->imports;
   22824              : 
   22825         2866 :       module->mod = 0;
   22826         2866 :       (*modules)[0] = module;
   22827              :     }
   22828              :   else
   22829              :     {
   22830          239 :       module->interface_p = true;
   22831          239 :       current->parent = module; /* So mangler knows module identity. */
   22832          239 :       direct_import (module, reader);
   22833              :     }
   22834              : }
   22835              : 
   22836              : /* Return true IFF we must emit a module global initializer function
   22837              :    (which will be called by importers' init code).  */
   22838              : 
   22839              : bool
   22840       104065 : module_global_init_needed ()
   22841              : {
   22842       104065 :   return module_has_cmi_p () && !header_module_p ();
   22843              : }
   22844              : 
   22845              : /* Calculate which, if any, import initializers need calling.  */
   22846              : 
   22847              : bool
   22848        96703 : module_determine_import_inits ()
   22849              : {
   22850        96703 :   if (!modules || header_module_p ())
   22851              :     return false;
   22852              : 
   22853              :   /* Prune active_init_p.  We need the same bitmap allocation
   22854              :      scheme as for the imports member.  */
   22855         3769 :   function_depth++; /* Disable GC.  */
   22856         3769 :   bitmap covered_imports (BITMAP_GGC_ALLOC ());
   22857              : 
   22858         3769 :   bool any = false;
   22859              : 
   22860              :   /* Because indirect imports are before their direct import, and
   22861              :      we're scanning the array backwards, we only need one pass!  */
   22862         6571 :   for (unsigned ix = modules->length (); --ix;)
   22863              :     {
   22864         2802 :       module_state *import = (*modules)[ix];
   22865              : 
   22866         2802 :       if (!import->active_init_p)
   22867              :         ;
   22868           52 :       else if (bitmap_bit_p (covered_imports, ix))
   22869            6 :         import->active_init_p = false;
   22870              :       else
   22871              :         {
   22872              :           /* Everything this imports is therefore handled by its
   22873              :              initializer, so doesn't need initializing by us.  */
   22874           46 :           bitmap_ior_into (covered_imports, import->imports);
   22875           46 :           any = true;
   22876              :         }
   22877              :     }
   22878         3769 :   function_depth--;
   22879              : 
   22880         3769 :   return any;
   22881              : }
   22882              : 
   22883              : /* Emit calls to each direct import's global initializer.  Including
   22884              :    direct imports of directly imported header units.  The initializers
   22885              :    of (static) entities in header units will be called by their
   22886              :    importing modules (for the instance contained within that), or by
   22887              :    the current TU (for the instances we've brought in).  Of course
   22888              :    such header unit behaviour is evil, but iostream went through that
   22889              :    door some time ago.  */
   22890              : 
   22891              : void
   22892           46 : module_add_import_initializers ()
   22893              : {
   22894           46 :   if (!modules || header_module_p ())
   22895            0 :     return;
   22896              : 
   22897           46 :   tree fntype = build_function_type (void_type_node, void_list_node);
   22898           46 :   releasing_vec args;  // There are no args
   22899              : 
   22900          104 :   for (unsigned ix = modules->length (); --ix;)
   22901              :     {
   22902           58 :       module_state *import = (*modules)[ix];
   22903           58 :       if (import->active_init_p)
   22904              :         {
   22905           46 :           tree name = mangle_module_global_init (ix);
   22906           46 :           tree fndecl = build_lang_decl (FUNCTION_DECL, name, fntype);
   22907              : 
   22908           46 :           DECL_CONTEXT (fndecl) = FROB_CONTEXT (global_namespace);
   22909           46 :           SET_DECL_ASSEMBLER_NAME (fndecl, name);
   22910           46 :           TREE_PUBLIC (fndecl) = true;
   22911           46 :           determine_visibility (fndecl);
   22912              : 
   22913           46 :           tree call = cp_build_function_call_vec (fndecl, &args,
   22914              :                                                   tf_warning_or_error);
   22915           46 :           finish_expr_stmt (call);
   22916              :         }
   22917              :     }
   22918           46 : }
   22919              : 
   22920              : /* NAME & LEN are a preprocessed header name, possibly including the
   22921              :    surrounding "" or <> characters.  Return the raw string name of the
   22922              :    module to which it refers.  This will be an absolute path, or begin
   22923              :    with ./, so it is immediately distinguishable from a (non-header
   22924              :    unit) module name.  If READER is non-null, ask the preprocessor to
   22925              :    locate the header to which it refers using the appropriate include
   22926              :    path.  Note that we do never do \ processing of the string, as that
   22927              :    matches the preprocessor's behaviour.  */
   22928              : 
   22929              : static const char *
   22930        21438 : canonicalize_header_name (cpp_reader *reader, location_t loc, bool unquoted,
   22931              :                           const char *str, size_t &len_r)
   22932              : {
   22933        21438 :   size_t len = len_r;
   22934        21438 :   static char *buf = 0;
   22935        21438 :   static size_t alloc = 0;
   22936              : 
   22937        21438 :   if (!unquoted)
   22938              :     {
   22939            4 :       gcc_checking_assert (len >= 2
   22940              :                            && ((reader && str[0] == '<' && str[len-1] == '>')
   22941              :                                || (str[0] == '"' && str[len-1] == '"')));
   22942            4 :       str += 1;
   22943            4 :       len -= 2;
   22944              :     }
   22945              : 
   22946        21438 :   if (reader)
   22947              :     {
   22948            4 :       gcc_assert (!unquoted);
   22949              : 
   22950            4 :       if (len >= alloc)
   22951              :         {
   22952            4 :           alloc = len + 1;
   22953            4 :           buf = XRESIZEVEC (char, buf, alloc);
   22954              :         }
   22955            4 :       memcpy (buf, str, len);
   22956            4 :       buf[len] = 0;
   22957              : 
   22958            8 :       if (const char *hdr
   22959            4 :           = cpp_probe_header_unit (reader, buf, str[-1] == '<', loc))
   22960              :         {
   22961            4 :           len = strlen (hdr);
   22962            4 :           str = hdr;
   22963              :         }
   22964              :       else
   22965            0 :         str = buf;
   22966              :     }
   22967              : 
   22968        21438 :   if (!(str[0] == '.' ? IS_DIR_SEPARATOR (str[1]) : IS_ABSOLUTE_PATH (str)))
   22969              :     {
   22970              :       /* Prepend './'  */
   22971            9 :       if (len + 3 > alloc)
   22972              :         {
   22973            9 :           alloc = len + 3;
   22974            9 :           buf = XRESIZEVEC (char, buf, alloc);
   22975              :         }
   22976              : 
   22977            9 :       buf[0] = '.';
   22978            9 :       buf[1] = DIR_SEPARATOR;
   22979            9 :       memmove (buf + 2, str, len);
   22980            9 :       len += 2;
   22981            9 :       buf[len] = 0;
   22982            9 :       str = buf;
   22983              :     }
   22984              : 
   22985        21438 :   len_r = len;
   22986        21438 :   return str;
   22987              : }
   22988              : 
   22989              : /* Set the CMI name from a cody packet.  Issue an error if
   22990              :    ill-formed.  */
   22991              : 
   22992         5707 : void module_state::set_filename (const Cody::Packet &packet)
   22993              : {
   22994         5707 :   if (packet.GetCode () == Cody::Client::PC_PATHNAME)
   22995              :     {
   22996              :       /* If we've seen this import before we better have the same CMI.  */
   22997         5704 :       const std::string &path = packet.GetString ();
   22998         5704 :       if (!filename)
   22999         5701 :         filename = xstrdup (packet.GetString ().c_str ());
   23000            3 :       else if (filename != path)
   23001            0 :         error_at (loc, "mismatching compiled module interface: "
   23002              :                   "had %qs, got %qs", filename, path.c_str ());
   23003              :     }
   23004              :   else
   23005              :     {
   23006            3 :       gcc_checking_assert (packet.GetCode () == Cody::Client::PC_ERROR);
   23007            3 :       fatal_error (loc, "unknown compiled module interface: %s",
   23008            3 :                    packet.GetString ().c_str ());
   23009              :     }
   23010         5704 : }
   23011              : 
   23012              : /* The list of importable headers from C++ Table 24.  */
   23013              : 
   23014              : static const char *
   23015              : importable_headers[] =
   23016              :   {
   23017              :     "algorithm", "any", "array", "atomic",
   23018              :     "barrier", "bit", "bitset",
   23019              :     "charconv", "chrono", "compare", "complex", "concepts",
   23020              :     "condition_variable", "contracts", "coroutine",
   23021              :     "debugging", "deque",
   23022              :     "exception", "execution", "expected",
   23023              :     "filesystem", "flat_map", "flat_set", "format", "forward_list",
   23024              :     "fstream", "functional", "future",
   23025              :     "generator",
   23026              :     "hazard_pointer", "hive",
   23027              :     "initializer_list", "inplace_vector", "iomanip", "ios", "iosfwd",
   23028              :     "iostream", "istream", "iterator",
   23029              :     "latch", "limits", "linalg", "list", "locale",
   23030              :     "map", "mdspan", "memory", "memory_resource", "meta", "mutex",
   23031              :     "new", "numbers", "numeric",
   23032              :     "optional", "ostream",
   23033              :     "print",
   23034              :     "queue",
   23035              :     "random", "ranges", "ratio", "rcu", "regex",
   23036              :     "scoped_allocator", "semaphore", "set", "shared_mutex", "simd",
   23037              :     "source_location", "span", "spanstream", "sstream", "stack", "stacktrace",
   23038              :     "stdexcept", "stdfloat", "stop_token", "streambuf", "string",
   23039              :     "string_view", "syncstream", "system_error",
   23040              :     "text_encoding", "thread", "tuple", "type_traits", "typeindex", "typeinfo",
   23041              :     "unordered_map", "unordered_set",
   23042              :     "utility",
   23043              :     "valarray", "variant", "vector", "version"
   23044              :   };
   23045              : 
   23046              : /* True iff <name> is listed as an importable standard header.  */
   23047              : 
   23048              : static bool
   23049        19280 : is_importable_header (const char *name)
   23050              : {
   23051        19280 :   unsigned lo = 0;
   23052        19280 :   unsigned hi = ARRAY_SIZE (importable_headers);
   23053       145952 :   while (hi > lo)
   23054              :     {
   23055       128985 :       unsigned mid = (lo + hi)/2;
   23056       128985 :       int cmp = strcmp (name, importable_headers[mid]);
   23057       128985 :       if (cmp > 0)
   23058        30099 :         lo = mid + 1;
   23059        98886 :       else if (cmp < 0)
   23060              :         hi = mid;
   23061              :       else
   23062              :         return true;
   23063              :     }
   23064              :   return false;
   23065              : }
   23066              : 
   23067              : /* Figure out whether to treat HEADER as an include or an import.  */
   23068              : 
   23069              : static char *
   23070        20521 : maybe_translate_include (cpp_reader *reader, line_maps *lmaps, location_t loc,
   23071              :                          _cpp_file *file, bool angle, const char **alternate)
   23072              : {
   23073        20521 :   if (!modules_p ())
   23074              :     {
   23075              :       /* Turn off.  */
   23076            0 :       cpp_get_callbacks (reader)->translate_include = NULL;
   23077            0 :       return nullptr;
   23078              :     }
   23079              : 
   23080        20521 :   const char *path = _cpp_get_file_path (file);
   23081              : 
   23082        20521 :   dump.push (NULL);
   23083              : 
   23084        22019 :   dump () && dump ("Checking include translation '%s'", path);
   23085        20521 :   auto *mapper = get_mapper (cpp_main_loc (reader), cpp_get_deps (reader));
   23086              : 
   23087        20521 :   size_t len = strlen (path);
   23088        20521 :   path = canonicalize_header_name (NULL, loc, true, path, len);
   23089        20521 :   auto packet = mapper->IncludeTranslate (path, Cody::Flags::None, len);
   23090              : 
   23091        20521 :   enum class xlate_kind {
   23092              :     unknown, text, import, invalid
   23093        20521 :   } translate = xlate_kind::unknown;
   23094              : 
   23095        20521 :   if (packet.GetCode () == Cody::Client::PC_BOOL)
   23096        20472 :     translate = packet.GetInteger () ? xlate_kind::text : xlate_kind::unknown;
   23097           49 :   else if (packet.GetCode () == Cody::Client::PC_PATHNAME)
   23098              :     {
   23099              :       /* Record the CMI name for when we do the import.
   23100              :          We may already know about this import, but libcpp doesn't yet.  */
   23101           49 :       module_state *import = get_module (build_string (len, path));
   23102           49 :       import->set_filename (packet);
   23103           49 :       if (import->check_importable (reader))
   23104              :         translate = xlate_kind::import;
   23105              :       else
   23106            0 :         translate = xlate_kind::invalid;
   23107              :     }
   23108              :   else
   23109              :     {
   23110            0 :       gcc_checking_assert (packet.GetCode () == Cody::Client::PC_ERROR);
   23111            0 :       error_at (loc, "cannot determine %<#include%> translation of %s: %s",
   23112            0 :                 path, packet.GetString ().c_str ());
   23113              :     }
   23114              : 
   23115        20521 :   bool note = (translate == xlate_kind::invalid);
   23116        20521 :   if (note_include_translate_yes && translate == xlate_kind::import)
   23117              :     note = true;
   23118        20516 :   else if (note_include_translate_no && translate == xlate_kind::unknown)
   23119              :     note = true;
   23120        20513 :   else if (note_includes)
   23121              :     /* We do not expect the note_includes vector to be large, so O(N)
   23122              :        iteration.  */
   23123          422 :     for (unsigned ix = note_includes->length (); !note && ix--;)
   23124          211 :       if (!strcmp ((*note_includes)[ix], path))
   23125            1 :         note = true;
   23126              : 
   23127              :   /* Maybe try importing a different header instead.  */
   23128        20521 :   if (alternate && translate == xlate_kind::unknown)
   23129              :     {
   23130        20095 :       const char *fname = _cpp_get_file_name (file);
   23131              :       /* Redirect importable <name> to <bits/stdc++.h>.  */
   23132              :       /* ??? Generalize to use a .json.  */
   23133        20095 :       expanded_location eloc = expand_location (loc);
   23134        19280 :       if (angle && is_importable_header (fname)
   23135              :           /* Exclude <version> which often goes with import std.  */
   23136         2313 :           && strcmp (fname, "version") != 0
   23137              :           /* Don't redirect #includes between headers under the same include
   23138              :              path directory (i.e. between library headers); if the import
   23139              :              brings in the current file we then get redefinition errors.  */
   23140         2302 :           && !strstr (eloc.file, _cpp_get_file_dir (file)->name)
   23141              :           /* ??? These are needed when running a toolchain from the build
   23142              :              directory, because libsupc++ headers aren't linked into
   23143              :              libstdc++-v3/include with the other headers.  */
   23144          816 :           && !strstr (eloc.file, "libstdc++-v3/include")
   23145        20544 :           && !strstr (eloc.file, "libsupc++"))
   23146          380 :         *alternate = "bits/stdc++.h";
   23147              :     }
   23148              : 
   23149        20521 :   if (note)
   23150           12 :     inform (loc, translate == xlate_kind::import
   23151              :             ? G_("include %qs translated to import")
   23152              :             : translate == xlate_kind::invalid
   23153            3 :             ? G_("import of %qs failed, falling back to include")
   23154              :             : G_("include %qs processed textually"), path);
   23155              : 
   23156        23514 :   dump () && dump (translate == xlate_kind::import
   23157              :                    ? "Translating include to import"
   23158              :                    : "Keeping include as include");
   23159        20521 :   dump.pop (0);
   23160              : 
   23161        20521 :   if (translate != xlate_kind::import)
   23162              :     return nullptr;
   23163              : 
   23164              :   /* Create the translation text.  */
   23165           49 :   loc = ordinary_loc_of (lmaps, loc);
   23166           49 :   const line_map_ordinary *map
   23167           49 :     = linemap_check_ordinary (linemap_lookup (lmaps, loc));
   23168           49 :   unsigned col = SOURCE_COLUMN (map, loc);
   23169           49 :   col -= (col != 0); /* Columns are 1-based.  */
   23170              : 
   23171           49 :   unsigned alloc = len + col + 60;
   23172           49 :   char *res = XNEWVEC (char, alloc);
   23173              : 
   23174           49 :   strcpy (res, "__import");
   23175           49 :   unsigned actual = 8;
   23176           49 :   if (col > actual)
   23177              :     {
   23178              :       /* Pad out so the filename appears at the same position.  */
   23179           46 :       memset (res + actual, ' ', col - actual);
   23180           46 :       actual = col;
   23181              :     }
   23182              :   /* No need to encode characters, that's not how header names are
   23183              :      handled.  */
   23184           49 :   actual += snprintf (res + actual, alloc - actual,
   23185              :                       "\"%s\" [[__translated]];\n", path);
   23186           49 :   gcc_checking_assert (actual < alloc);
   23187              : 
   23188              :   /* cpplib will delete the buffer.  */
   23189              :   return res;
   23190        20521 : }
   23191              : 
   23192              : static void
   23193          913 : begin_header_unit (cpp_reader *reader)
   23194              : {
   23195              :   /* Set the module header name from the main_input_filename.  */
   23196          913 :   const char *main = main_input_filename;
   23197          913 :   size_t len = strlen (main);
   23198          913 :   main = canonicalize_header_name (NULL, 0, true, main, len);
   23199          913 :   module_state *module = get_module (build_string (len, main));
   23200              : 
   23201          913 :   preprocess_module (module, cpp_main_loc (reader), false, false, true, reader);
   23202          913 : }
   23203              : 
   23204              : /* We've just properly entered the main source file.  I.e. after the
   23205              :    command line, builtins and forced headers.  Record the line map and
   23206              :    location of this map.  Note we may be called more than once.  The
   23207              :    first call sticks.  */
   23208              : 
   23209              : void
   23210        98609 : module_begin_main_file (cpp_reader *reader, line_maps *lmaps,
   23211              :                        const line_map_ordinary *map)
   23212              : {
   23213        98609 :   gcc_checking_assert (lmaps == line_table);
   23214        98609 :   if (modules_p () && !spans.init_p ())
   23215              :     {
   23216         4848 :       unsigned n = dump.push (NULL);
   23217         4848 :       spans.init (lmaps, map);
   23218         4848 :       dump.pop (n);
   23219         4848 :       if (flag_header_unit && !cpp_get_options (reader)->preprocessed)
   23220              :         {
   23221              :           /* Tell the preprocessor this is an include file.  */
   23222          904 :           cpp_retrofit_as_include (reader);
   23223          904 :           begin_header_unit (reader);
   23224              :         }
   23225              :     }
   23226        98609 : }
   23227              : 
   23228              : /* Process the pending_import queue, making sure we know the
   23229              :    filenames.   */
   23230              : 
   23231              : static void
   23232         5745 : name_pending_imports (cpp_reader *reader)
   23233              : {
   23234         5745 :   auto *mapper = get_mapper (cpp_main_loc (reader), cpp_get_deps (reader));
   23235              : 
   23236         5745 :   if (!vec_safe_length (pending_imports))
   23237              :     /* Not doing anything.  */
   23238              :     return;
   23239              : 
   23240         4915 :   timevar_start (TV_MODULE_MAPPER);
   23241              : 
   23242         4915 :   auto n = dump.push (NULL);
   23243         5534 :   dump () && dump ("Resolving direct import names");
   23244         4915 :   bool want_deps = (bool (mapper->get_flags () & Cody::Flags::NameOnly)
   23245         4915 :                     || cpp_get_deps (reader));
   23246         4915 :   bool any = false;
   23247              : 
   23248        10757 :   for (unsigned ix = 0; ix != pending_imports->length (); ix++)
   23249              :     {
   23250         5842 :       module_state *module = (*pending_imports)[ix];
   23251         5842 :       gcc_checking_assert (module->is_direct ());
   23252         5842 :       if (!module->filename && !module->visited_p)
   23253              :         {
   23254         5730 :           bool export_p = (module->is_module ()
   23255         5730 :                            && (module->is_partition ()
   23256         2981 :                                || module->is_exported ()));
   23257              : 
   23258         5730 :           Cody::Flags flags = Cody::Flags::None;
   23259         5730 :           if (flag_preprocess_only
   23260         5730 :               && !(module->is_header () && !export_p))
   23261              :             {
   23262          141 :               if (!want_deps)
   23263           72 :                 continue;
   23264              :               flags = Cody::Flags::NameOnly;
   23265              :             }
   23266              : 
   23267         5658 :           if (!any)
   23268              :             {
   23269         4841 :               any = true;
   23270         4841 :               mapper->Cork ();
   23271              :             }
   23272         5658 :           if (export_p)
   23273         2920 :             mapper->ModuleExport (module->get_flatname (), flags);
   23274              :           else
   23275         2738 :             mapper->ModuleImport (module->get_flatname (), flags);
   23276         5658 :           module->visited_p = true;
   23277              :         }
   23278              :     }
   23279              : 
   23280         4915 :   if (any)
   23281              :     {
   23282         4841 :       auto response = mapper->Uncork ();
   23283         4841 :       auto r_iter = response.begin ();
   23284        10591 :       for (unsigned ix = 0; ix != pending_imports->length (); ix++)
   23285              :         {
   23286         5753 :           module_state *module = (*pending_imports)[ix];
   23287         5753 :           if (module->visited_p)
   23288              :             {
   23289         5658 :               module->visited_p = false;
   23290         5658 :               gcc_checking_assert (!module->filename);
   23291              : 
   23292         5658 :               module->set_filename (*r_iter);
   23293         5655 :               ++r_iter;
   23294              :             }
   23295              :         }
   23296         4838 :     }
   23297              : 
   23298         4912 :   dump.pop (n);
   23299              : 
   23300         4912 :   timevar_stop (TV_MODULE_MAPPER);
   23301              : }
   23302              : 
   23303              : /* We've just lexed a module-specific control line for MODULE.  Mark
   23304              :    the module as a direct import, and possibly load up its macro
   23305              :    state.  Returns the primary module, if this is a module
   23306              :    declaration.  */
   23307              : /* Perhaps we should offer a preprocessing mode where we read the
   23308              :    directives from the header unit, rather than require the header's
   23309              :    CMI.  */
   23310              : 
   23311              : module_state *
   23312         5862 : preprocess_module (module_state *module, location_t from_loc,
   23313              :                    bool in_purview, bool is_import, bool is_export,
   23314              :                    cpp_reader *reader)
   23315              : {
   23316         5862 :   if (!is_import)
   23317              :     {
   23318         3267 :       if (in_purview || module->loc)
   23319              :         {
   23320              :           /* We've already seen a module declaration.  If only preprocessing
   23321              :              then we won't complain in declare_module, so complain here.  */
   23322           42 :           if (flag_preprocess_only)
   23323            6 :             error_at (from_loc,
   23324              :                       in_purview
   23325              :                       ? G_("module already declared")
   23326              :                       : G_("module already imported"));
   23327              :           /* Always pretend this was an import to aid error recovery.  */
   23328              :           is_import = true;
   23329              :         }
   23330              :       else
   23331              :         {
   23332              :           /* Record it is the module.  */
   23333         3225 :           module->module_p = true;
   23334         3225 :           if (is_export)
   23335              :             {
   23336         2903 :               module->exported_p = true;
   23337         2903 :               module->interface_p = true;
   23338              :             }
   23339              :         }
   23340              :     }
   23341              : 
   23342         5862 :   if (module->directness < MD_DIRECT + in_purview)
   23343              :     {
   23344              :       /* Mark as a direct import.  */
   23345         5815 :       module->directness = module_directness (MD_DIRECT + in_purview);
   23346              : 
   23347              :       /* Set the location to be most informative for users.  */
   23348         5815 :       from_loc = ordinary_loc_of (line_table, from_loc);
   23349         5815 :       if (module->loadedness != ML_NONE)
   23350            6 :         linemap_module_reparent (line_table, module->loc, from_loc);
   23351              :       else
   23352              :         {
   23353              :           /* Don't overwrite the location if we're importing ourselves
   23354              :              after already having seen a module-declaration.  */
   23355         5809 :           if (!(is_import && module->is_module ()))
   23356         5779 :             module->loc = from_loc;
   23357         5809 :           if (!module->flatname)
   23358         5770 :             module->set_flatname ();
   23359              :         }
   23360              :     }
   23361              : 
   23362         5862 :   auto desired = ML_CONFIG;
   23363         5862 :   if (is_import
   23364         2637 :       && module->is_header ()
   23365         6773 :       && (!cpp_get_options (reader)->preprocessed
   23366            3 :           || cpp_get_options (reader)->directives_only))
   23367              :     /* We need preprocessor state now.  */
   23368              :     desired = ML_PREPROCESSOR;
   23369              : 
   23370         5862 :   if (!is_import || module->loadedness < desired)
   23371              :     {
   23372         5842 :       vec_safe_push (pending_imports, module);
   23373              : 
   23374         5842 :       if (desired == ML_PREPROCESSOR)
   23375              :         {
   23376          891 :           unsigned n = dump.push (NULL);
   23377              : 
   23378         1090 :           dump () && dump ("Reading %M preprocessor state", module);
   23379          891 :           name_pending_imports (reader);
   23380              : 
   23381              :           /* Preserve the state of the line-map.  */
   23382          891 :           auto pre_hwm = LINEMAPS_ORDINARY_USED (line_table);
   23383              : 
   23384              :           /* We only need to close the span, if we're going to emit a
   23385              :              CMI.  But that's a little tricky -- our token scanner
   23386              :              needs to be smarter -- and this isn't much state.
   23387              :              Remember, we've not parsed anything at this point, so
   23388              :              our module state flags are inadequate.  */
   23389          891 :           spans.maybe_init ();
   23390          891 :           spans.close ();
   23391              : 
   23392          891 :           timevar_start (TV_MODULE_IMPORT);
   23393              : 
   23394              :           /* Load the config of each pending import -- we must assign
   23395              :              module numbers monotonically.  */
   23396         1970 :           for (unsigned ix = 0; ix != pending_imports->length (); ix++)
   23397              :             {
   23398         1079 :               auto *import = (*pending_imports)[ix];
   23399         1256 :               if (!(import->is_module ()
   23400          177 :                     && (import->is_partition () || import->is_exported ()))
   23401          908 :                   && import->loadedness == ML_NONE
   23402         1986 :                   && (!flag_preprocess_only
   23403           51 :                       || (import->is_header ()
   23404              :                           /* Allow a missing/unimportable GCM with -MG.
   23405              :                              FIXME We should also try falling back to #include
   23406              :                              before giving up entirely.  */
   23407           42 :                           && (!cpp_get_options (reader)->deps.missing_files
   23408            3 :                               || import->check_importable (reader)))))
   23409              :                 {
   23410          895 :                   unsigned n = dump.push (import);
   23411          895 :                   import->do_import (reader, true);
   23412          895 :                   dump.pop (n);
   23413              :                 }
   23414              :             }
   23415          891 :           vec_free (pending_imports);
   23416              : 
   23417              :           /* Restore the line-map state.  */
   23418          891 :           spans.open (linemap_module_restore (line_table, pre_hwm));
   23419              : 
   23420              :           /* Now read the preprocessor state of this particular
   23421              :              import.  */
   23422          891 :           if (module->loadedness == ML_CONFIG
   23423          891 :               && module->read_preprocessor (true))
   23424          885 :             module->import_macros ();
   23425              : 
   23426          891 :           timevar_stop (TV_MODULE_IMPORT);
   23427              : 
   23428          891 :           dump.pop (n);
   23429              :         }
   23430              :     }
   23431              : 
   23432         5862 :   return is_import ? NULL : get_primary (module);
   23433              : }
   23434              : 
   23435              : /* We've completed phase-4 translation.  Emit any dependency
   23436              :    information for the not-yet-loaded direct imports, and fill in
   23437              :    their file names.  We'll have already loaded up the direct header
   23438              :    unit wavefront.  */
   23439              : 
   23440              : void
   23441         4854 : preprocessed_module (cpp_reader *reader)
   23442              : {
   23443         4854 :   unsigned n = dump.push (NULL);
   23444              : 
   23445         5437 :   dump () && dump ("Completed phase-4 (tokenization) processing");
   23446              : 
   23447         4854 :   name_pending_imports (reader);
   23448         4851 :   vec_free (pending_imports);
   23449              : 
   23450         4851 :   spans.maybe_init ();
   23451         4851 :   spans.close ();
   23452              : 
   23453         4851 :   using iterator = hash_table<module_state_hash>::iterator;
   23454         4851 :   if (mkdeps *deps = cpp_get_deps (reader))
   23455              :     {
   23456              :       /* Walk the module hash, informing the dependency machinery.  */
   23457           57 :       iterator end = modules_hash->end ();
   23458          342 :       for (iterator iter = modules_hash->begin (); iter != end; ++iter)
   23459              :         {
   23460          114 :           module_state *module = *iter;
   23461              : 
   23462          114 :           if (module->is_direct ())
   23463              :             {
   23464           90 :               if (module->is_module ()
   23465           90 :                   && (module->is_interface () || module->is_partition ()))
   23466           36 :                 deps_add_module_target (deps, module->get_flatname (),
   23467           36 :                                         maybe_add_cmi_prefix (module->filename),
   23468           36 :                                         module->is_header (),
   23469           36 :                                         module->is_exported ());
   23470              :               else
   23471           54 :                 deps_add_module_dep (deps, module->get_flatname ());
   23472              :             }
   23473              :         }
   23474              :     }
   23475              : 
   23476         4851 :   if (flag_header_unit && !flag_preprocess_only)
   23477              :     {
   23478              :       /* Find the main module -- remember, it's not yet in the module
   23479              :          array.  */
   23480          901 :       iterator end = modules_hash->end ();
   23481         1910 :       for (iterator iter = modules_hash->begin (); iter != end; ++iter)
   23482              :         {
   23483          955 :           module_state *module = *iter;
   23484          955 :           if (module->is_module ())
   23485              :             {
   23486          901 :               declare_module (module, cpp_main_loc (reader), true, NULL, reader);
   23487          901 :               module_kind |= MK_EXPORTING;
   23488          901 :               break;
   23489              :             }
   23490              :         }
   23491              :     }
   23492              : 
   23493         4851 :   dump.pop (n);
   23494         4851 : }
   23495              : 
   23496              : /* VAL is a global tree, add it to the global vec if it is
   23497              :    interesting.  Add some of its targets, if they too are
   23498              :    interesting.  We do not add identifiers, as they can be re-found
   23499              :    via the identifier hash table.  There is a cost to the number of
   23500              :    global trees.  */
   23501              : 
   23502              : static int
   23503      3007946 : maybe_add_global (tree val, unsigned &crc)
   23504              : {
   23505      3007946 :   int v = 0;
   23506              : 
   23507      3007946 :   if (val && !(identifier_p (val) || TREE_VISITED (val)))
   23508              :     {
   23509       925582 :       TREE_VISITED (val) = true;
   23510       925582 :       crc = crc32_unsigned (crc, fixed_trees->length ());
   23511       925582 :       vec_safe_push (fixed_trees, val);
   23512       925582 :       v++;
   23513              : 
   23514       925582 :       if (CODE_CONTAINS_STRUCT (TREE_CODE (val), TS_TYPED))
   23515       925582 :         v += maybe_add_global (TREE_TYPE (val), crc);
   23516       925582 :       if (CODE_CONTAINS_STRUCT (TREE_CODE (val), TS_TYPE_COMMON))
   23517       606748 :         v += maybe_add_global (TYPE_NAME (val), crc);
   23518              :     }
   23519              : 
   23520      3007946 :   return v;
   23521              : }
   23522              : 
   23523              : /* Initialize module state.  Create the hash table, determine the
   23524              :    global trees.  Create the module for current TU.  */
   23525              : 
   23526              : void
   23527         4854 : init_modules (cpp_reader *reader)
   23528              : {
   23529              :   /* PCH should not be reachable because of lang-specs, but the
   23530              :      user could have overriden that.  */
   23531         4854 :   if (pch_file)
   23532            0 :     fatal_error (input_location,
   23533              :                  "C++ modules are incompatible with precompiled headers");
   23534              : 
   23535         4854 :   if (cpp_get_options (reader)->traditional)
   23536            0 :     fatal_error (input_location,
   23537              :                  "C++ modules are incompatible with traditional preprocessing");
   23538              : 
   23539              :   /* :: is always exported.  */
   23540         4854 :   DECL_MODULE_EXPORT_P (global_namespace) = true;
   23541              : 
   23542         4854 :   modules_hash = hash_table<module_state_hash>::create_ggc (31);
   23543         4854 :   vec_safe_reserve (modules, 20);
   23544              : 
   23545              :   /* Create module for current TU.  */
   23546         4854 :   module_state *current
   23547         4854 :     = new (ggc_alloc<module_state> ()) module_state (NULL_TREE, NULL, false);
   23548         4854 :   current->mod = 0;
   23549         4854 :   bitmap_set_bit (current->imports, 0);
   23550         4854 :   modules->quick_push (current);
   23551              : 
   23552         4854 :   gcc_checking_assert (!fixed_trees);
   23553              : 
   23554         4854 :   headers = BITMAP_GGC_ALLOC ();
   23555              : 
   23556         4854 :   if (note_includes)
   23557              :     /* Canonicalize header names.  */
   23558            2 :     for (unsigned ix = 0; ix != note_includes->length (); ix++)
   23559              :       {
   23560            1 :         const char *hdr = (*note_includes)[ix];
   23561            1 :         size_t len = strlen (hdr);
   23562              : 
   23563            1 :         bool system = hdr[0] == '<';
   23564            1 :         bool user = hdr[0] == '"';
   23565            1 :         bool delimed = system || user;
   23566              : 
   23567            1 :         if (len <= (delimed ? 2 : 0)
   23568            1 :             || (delimed && hdr[len-1] != (system ? '>' : '"')))
   23569            0 :           error ("invalid header name %qs", hdr);
   23570              : 
   23571            1 :         hdr = canonicalize_header_name (delimed ? reader : NULL,
   23572              :                                         0, !delimed, hdr, len);
   23573            1 :         char *path = XNEWVEC (char, len + 1);
   23574            1 :         memcpy (path, hdr, len);
   23575            1 :         path[len] = 0;
   23576              : 
   23577            1 :         (*note_includes)[ix] = path;
   23578              :       }
   23579              : 
   23580         4854 :   if (note_cmis)
   23581              :     /* Canonicalize & mark module names.  */
   23582           12 :     for (unsigned ix = 0; ix != note_cmis->length (); ix++)
   23583              :       {
   23584            6 :         const char *name = (*note_cmis)[ix];
   23585            6 :         size_t len = strlen (name);
   23586              : 
   23587            6 :         bool is_system = name[0] == '<';
   23588            6 :         bool is_user = name[0] == '"';
   23589            6 :         bool is_pathname = false;
   23590            6 :         if (!(is_system || is_user))
   23591           12 :           for (unsigned ix = len; !is_pathname && ix--;)
   23592            9 :             is_pathname = IS_DIR_SEPARATOR (name[ix]);
   23593            6 :         if (is_system || is_user || is_pathname)
   23594              :           {
   23595            3 :             if (len <= (is_pathname ? 0 : 2)
   23596            3 :                 || (!is_pathname && name[len-1] != (is_system ? '>' : '"')))
   23597              :               {
   23598            0 :                 error ("invalid header name %qs", name);
   23599            0 :                 continue;
   23600              :               }
   23601              :             else
   23602            3 :               name = canonicalize_header_name (is_pathname ? nullptr : reader,
   23603              :                                                0, is_pathname, name, len);
   23604              :           }
   23605            6 :         if (auto module = get_module (name))
   23606            6 :           module->inform_cmi_p = 1;
   23607              :         else
   23608            0 :           error ("invalid module name %qs", name);
   23609              :       }
   23610              : 
   23611         4854 :   dump.push (NULL);
   23612              : 
   23613              :   /* Determine lazy handle bound.  */
   23614         4854 :   {
   23615         4854 :     unsigned limit = 1000;
   23616              : #if HAVE_GETRLIMIT
   23617         4854 :     struct rlimit rlimit;
   23618         4854 :     if (!getrlimit (RLIMIT_NOFILE, &rlimit))
   23619              :       {
   23620         4854 :         lazy_hard_limit = (rlimit.rlim_max < 1000000
   23621         4854 :                            ? unsigned (rlimit.rlim_max) : 1000000);
   23622         4854 :         lazy_hard_limit = (lazy_hard_limit > LAZY_HEADROOM
   23623         4854 :                            ? lazy_hard_limit - LAZY_HEADROOM : 0);
   23624         4854 :         if (rlimit.rlim_cur < limit)
   23625            0 :           limit = unsigned (rlimit.rlim_cur);
   23626              :       }
   23627              : #endif
   23628         4854 :     limit = limit > LAZY_HEADROOM ? limit - LAZY_HEADROOM : 1;
   23629              : 
   23630         4854 :     if (unsigned parm = param_lazy_modules)
   23631              :       {
   23632         4854 :         if (parm <= limit || !lazy_hard_limit || !try_increase_lazy (parm))
   23633            6 :           lazy_limit = parm;
   23634              :       }
   23635              :     else
   23636            0 :       lazy_limit = limit;
   23637              :   }
   23638              : 
   23639         4854 :   if (dump ())
   23640              :     {
   23641          583 :       verstr_t ver;
   23642          583 :       version2string (MODULE_VERSION, ver);
   23643          583 :       dump ("Source: %s", main_input_filename);
   23644          583 :       dump ("Compiler: %s", version_string);
   23645          583 :       dump ("Modules: %s", ver);
   23646          583 :       dump ("Checking: %s",
   23647              : #if CHECKING_P
   23648              :             "checking"
   23649              : #elif ENABLE_ASSERT_CHECKING
   23650              :             "asserting"
   23651              : #else
   23652              :             "release"
   23653              : #endif
   23654              :             );
   23655          583 :       dump ("Compiled by: "
   23656              : #ifdef __GNUC__
   23657              :             "GCC %d.%d, %s", __GNUC__, __GNUC_MINOR__,
   23658              : #ifdef __OPTIMIZE__
   23659              :             "optimizing"
   23660              : #else
   23661              :             "not optimizing"
   23662              : #endif
   23663              : #else
   23664              :             "not GCC"
   23665              : #endif
   23666              :             );
   23667          583 :       dump ("Reading: %s", MAPPED_READING ? "mmap" : "fileio");
   23668          583 :       dump ("Writing: %s", MAPPED_WRITING ? "mmap" : "fileio");
   23669          583 :       dump ("Lazy limit: %u", lazy_limit);
   23670          583 :       dump ("Lazy hard limit: %u", lazy_hard_limit);
   23671          583 :       dump ("");
   23672              :     }
   23673              : 
   23674              :   /* Construct the global tree array.  This is an array of unique
   23675              :      global trees (& types).  Do this now, rather than lazily, as
   23676              :      some global trees are lazily created and we don't want that to
   23677              :      mess with our syndrome of fixed trees.  */
   23678         4854 :   unsigned crc = 0;
   23679         4854 :   vec_alloc (fixed_trees, 250);
   23680              : 
   23681         5437 :   dump () && dump ("+Creating globals");
   23682              :   /* Insert the TRANSLATION_UNIT_DECL.  */
   23683         4854 :   TREE_VISITED (DECL_CONTEXT (global_namespace)) = true;
   23684         4854 :   fixed_trees->quick_push (DECL_CONTEXT (global_namespace));
   23685        29124 :   for (unsigned jx = 0; global_tree_arys[jx].first; jx++)
   23686              :     {
   23687        24270 :       const tree *ptr = global_tree_arys[jx].first;
   23688        24270 :       unsigned limit = global_tree_arys[jx].second;
   23689              : 
   23690      1485324 :       for (unsigned ix = 0; ix != limit; ix++, ptr++)
   23691              :         {
   23692      1461054 :           !(ix & 31) && dump ("") && dump ("+\t%u:%u:", jx, ix);
   23693      1461054 :           unsigned v = maybe_add_global (*ptr, crc);
   23694      1636537 :           dump () && dump ("+%u", v);
   23695              :         }
   23696              :     }
   23697              :   /* OS- and machine-specific types are dynamically registered at
   23698              :      runtime, so cannot be part of global_tree_arys.  */
   23699         4854 :   registered_builtin_types && dump ("") && dump ("+\tB:");
   23700        19416 :   for (tree t = registered_builtin_types; t; t = TREE_CHAIN (t))
   23701              :     {
   23702        14562 :       unsigned v = maybe_add_global (TREE_VALUE (t), crc);
   23703        16311 :       dump () && dump ("+%u", v);
   23704              :     }
   23705         4854 :   global_crc = crc32_unsigned (crc, fixed_trees->length ());
   23706         4854 :   dump ("") && dump ("Created %u unique globals, crc=%x",
   23707              :                      fixed_trees->length (), global_crc);
   23708       935290 :   for (unsigned ix = fixed_trees->length (); ix--;)
   23709       930436 :     TREE_VISITED ((*fixed_trees)[ix]) = false;
   23710              : 
   23711         4854 :   dump.pop (0);
   23712              : 
   23713         4854 :   if (!flag_module_lazy)
   23714              :     /* Get the mapper now, if we're not being lazy.  */
   23715          299 :     get_mapper (cpp_main_loc (reader), cpp_get_deps (reader));
   23716              : 
   23717         4854 :   if (!flag_preprocess_only)
   23718              :     {
   23719         4710 :       pending_table = new pending_map_t (EXPERIMENT (1, 400));
   23720         4710 :       entity_map = new entity_map_t (EXPERIMENT (1, 400));
   23721         4710 :       vec_safe_reserve (entity_ary, EXPERIMENT (1, 400));
   23722         4710 :       imported_temploid_friends
   23723         4710 :         = decl_tree_cache_map::create_ggc (EXPERIMENT (1, 400));
   23724              :     }
   23725              : 
   23726              : #if CHECKING_P
   23727         4854 :   note_defs = note_defs_table_t::create_ggc (1000);
   23728              : #endif
   23729              : 
   23730         4854 :   if (flag_header_unit && cpp_get_options (reader)->preprocessed)
   23731            9 :     begin_header_unit (reader);
   23732              : 
   23733              :   /* Collect here to make sure things are tagged correctly (when
   23734              :      aggressively GC'd).  */
   23735         4854 :   ggc_collect ();
   23736         4854 : }
   23737              : 
   23738              : /* If NODE is a deferred macro, load it.  */
   23739              : 
   23740              : static int
   23741        82953 : load_macros (cpp_reader *reader, cpp_hashnode *node, void *)
   23742              : {
   23743        82953 :   location_t main_loc
   23744        82953 :     = MAP_START_LOCATION (LINEMAPS_ORDINARY_MAP_AT (line_table, 0));
   23745              : 
   23746        82953 :   if (cpp_user_macro_p (node)
   23747        82953 :       && !node->value.macro)
   23748              :     {
   23749           72 :       cpp_macro *macro = cpp_get_deferred_macro (reader, node, main_loc);
   23750           72 :       dump () && dump ("Loaded macro #%s %I",
   23751              :                        macro ? "define" : "undef", identifier (node));
   23752              :     }
   23753              : 
   23754        82953 :   return 1;
   23755              : }
   23756              : 
   23757              : /* At the end of tokenizing, we no longer need the macro tables of
   23758              :    imports.  But the user might have requested some checking.  */
   23759              : 
   23760              : void
   23761        96910 : maybe_check_all_macros (cpp_reader *reader)
   23762              : {
   23763        96910 :   if (!warn_imported_macros)
   23764              :     return;
   23765              : 
   23766              :   /* Force loading of any remaining deferred macros.  This will
   23767              :      produce diagnostics if they are ill-formed.  */
   23768           21 :   unsigned n = dump.push (NULL);
   23769           21 :   cpp_forall_identifiers (reader, load_macros, NULL);
   23770           21 :   dump.pop (n);
   23771              : }
   23772              : 
   23773              : // State propagated from finish_module_processing to fini_modules
   23774              : 
   23775              : struct module_processing_cookie
   23776              : {
   23777              :   elf_out out;
   23778              :   module_state_config config;
   23779              :   char *cmi_name;
   23780              :   char *tmp_name;
   23781              :   unsigned crc;
   23782              :   bool began;
   23783              : 
   23784         2860 :   module_processing_cookie (char *cmi, char *tmp, int fd, int e)
   23785         2860 :     : out (fd, e), cmi_name (cmi), tmp_name (tmp), crc (0), began (false)
   23786              :   {
   23787              :   }
   23788         2860 :   ~module_processing_cookie ()
   23789              :   {
   23790         2860 :     XDELETEVEC (tmp_name);
   23791         2860 :     XDELETEVEC (cmi_name);
   23792         2860 :   }
   23793              : };
   23794              : 
   23795              : /* Write the CMI, if we're a module interface.  */
   23796              : 
   23797              : void *
   23798        96703 : finish_module_processing (cpp_reader *reader)
   23799              : {
   23800        96703 :   module_processing_cookie *cookie = nullptr;
   23801              : 
   23802        96703 :   if (header_module_p ())
   23803          901 :     module_kind &= ~MK_EXPORTING;
   23804              : 
   23805        96703 :   if (!modules || !this_module ()->name)
   23806              :     {
   23807        93840 :       if (flag_module_only)
   23808            6 :         warning (0, "%<-fmodule-only%> used for non-interface");
   23809              :     }
   23810         2863 :   else if (!flag_syntax_only)
   23811              :     {
   23812         2860 :       int fd = -1;
   23813         2860 :       int e = -1;
   23814              : 
   23815         2860 :       timevar_start (TV_MODULE_EXPORT);
   23816              : 
   23817              :       /* Force a valid but empty line map at the end.  This simplifies
   23818              :          the line table preparation and writing logic.  */
   23819         2860 :       linemap_add (line_table, LC_ENTER, false, "", 0);
   23820              : 
   23821              :       /* We write to a tmpname, and then atomically rename.  */
   23822         2860 :       char *cmi_name = NULL;
   23823         2860 :       char *tmp_name = NULL;
   23824         2860 :       module_state *state = this_module ();
   23825              : 
   23826         2860 :       unsigned n = dump.push (state);
   23827         2860 :       state->announce ("creating");
   23828         2860 :       if (state->filename)
   23829              :         {
   23830         2860 :           size_t len = 0;
   23831         2860 :           cmi_name = xstrdup (maybe_add_cmi_prefix (state->filename, &len));
   23832         2860 :           tmp_name = XNEWVEC (char, len + 3);
   23833         2860 :           memcpy (tmp_name, cmi_name, len);
   23834         2860 :           strcpy (&tmp_name[len], "~");
   23835              : 
   23836         2860 :           if (!errorcount)
   23837           45 :             for (unsigned again = 2; ; again--)
   23838              :               {
   23839         2793 :                 fd = open (tmp_name,
   23840              :                            O_RDWR | O_CREAT | O_TRUNC | O_CLOEXEC | O_BINARY,
   23841              :                            S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH);
   23842         2793 :                 e = errno;
   23843         2793 :                 if (fd >= 0 || !again || e != ENOENT)
   23844              :                   break;
   23845           45 :                 create_dirs (tmp_name);
   23846              :               }
   23847         2860 :           if (note_module_cmi_yes || state->inform_cmi_p)
   23848            3 :             inform (state->loc, "writing CMI %qs", cmi_name);
   23849         3160 :           dump () && dump ("CMI is %s", cmi_name);
   23850              :         }
   23851              : 
   23852         2860 :       cookie = new module_processing_cookie (cmi_name, tmp_name, fd, e);
   23853              : 
   23854         2860 :       if (errorcount)
   23855              :         /* Don't write the module if we have reported errors.  */;
   23856         2748 :       else if (erroneous_templates
   23857         2748 :                && !erroneous_templates->is_empty ())
   23858              :         {
   23859              :           /* Don't write the module if it contains an erroneous template.
   23860              :              Also emit notes about where errors occurred in case
   23861              :              -Wno-template-body was passed.  */
   23862            6 :           auto_diagnostic_group d;
   23863            6 :           error_at (state->loc, "not writing module %qs due to errors "
   23864              :                     "in template bodies", state->get_flatname ());
   23865            6 :           if (!warn_template_body)
   23866            3 :             inform (state->loc, "enable %<-Wtemplate-body%> for more details");
   23867           12 :           for (auto e : *erroneous_templates)
   23868            6 :             inform (e.second, "first error in %qD appeared here", e.first);
   23869            6 :         }
   23870         2742 :       else if (cookie->out.begin ())
   23871              :         {
   23872              :           /* So crashes finger-point the module decl.  */
   23873         2742 :           iloc_sentinel ils = state->loc;
   23874         2742 :           if (state->write_begin (&cookie->out, reader, cookie->config,
   23875         2742 :                                   cookie->crc))
   23876         2713 :             cookie->began = true;
   23877         2742 :         }
   23878              : 
   23879         2860 :       dump.pop (n);
   23880         2860 :       timevar_stop (TV_MODULE_EXPORT);
   23881              : 
   23882         2860 :       ggc_collect ();
   23883              :     }
   23884              : 
   23885        96703 :   if (modules)
   23886              :     {
   23887         4670 :       unsigned n = dump.push (NULL);
   23888         5253 :       dump () && dump ("Imported %u modules", modules->length () - 1);
   23889         5253 :       dump () && dump ("Containing %u clusters", available_clusters);
   23890         4670 :       dump () && dump ("Loaded %u clusters (%u%%)", loaded_clusters,
   23891          583 :                        (loaded_clusters * 100 + available_clusters / 2) /
   23892          583 :                        (available_clusters + !available_clusters));
   23893         4670 :       dump.pop (n);
   23894              :     }
   23895              : 
   23896        96703 :   return cookie;
   23897              : }
   23898              : 
   23899              : // Do the final emission of a module.  At this point we know whether
   23900              : // the module static initializer is a NOP or not.
   23901              : 
   23902              : static void
   23903         2860 : late_finish_module (cpp_reader *reader,  module_processing_cookie *cookie,
   23904              :                     bool init_fn_non_empty)
   23905              : {
   23906         2860 :   timevar_start (TV_MODULE_EXPORT);
   23907              : 
   23908         2860 :   module_state *state = this_module ();
   23909         2860 :   unsigned n = dump.push (state);
   23910         2860 :   state->announce ("finishing");
   23911              : 
   23912         2860 :   cookie->config.active_init = init_fn_non_empty;
   23913         2860 :   if (cookie->began)
   23914         2713 :     state->write_end (&cookie->out, reader, cookie->config, cookie->crc);
   23915              : 
   23916         2860 :   if (cookie->out.end () && cookie->cmi_name)
   23917              :     {
   23918              :       /* Some OS's do not replace NEWNAME if it already exists.
   23919              :          This'll have a race condition in erroneous concurrent
   23920              :          builds.  */
   23921         2748 :       unlink (cookie->cmi_name);
   23922         2748 :       if (rename (cookie->tmp_name, cookie->cmi_name))
   23923              :         {
   23924            0 :           dump () && dump ("Rename ('%s','%s') errno=%u",
   23925            0 :                            cookie->tmp_name, cookie->cmi_name, errno);
   23926            0 :           cookie->out.set_error (errno);
   23927              :         }
   23928              :     }
   23929              : 
   23930         2860 :   if (cookie->out.get_error () && cookie->began)
   23931              :     {
   23932            0 :       error_at (state->loc, "failed to write compiled module: %s",
   23933            0 :                 cookie->out.get_error (state->filename));
   23934            0 :       state->note_cmi_name ();
   23935              :     }
   23936              : 
   23937         2860 :   if (!errorcount)
   23938              :     {
   23939         2707 :       auto *mapper = get_mapper (cpp_main_loc (reader), cpp_get_deps (reader));
   23940         2707 :       mapper->ModuleCompiled (state->get_flatname ());
   23941              :     }
   23942          153 :   else if (cookie->cmi_name)
   23943              :     {
   23944              :       /* We failed, attempt to erase all evidence we even tried.  */
   23945          153 :       unlink (cookie->tmp_name);
   23946          153 :       unlink (cookie->cmi_name);
   23947              :     }
   23948              : 
   23949         2860 :   delete cookie;
   23950         2860 :   dump.pop (n);
   23951         2860 :   timevar_stop (TV_MODULE_EXPORT);
   23952         2860 : }
   23953              : 
   23954              : void
   23955        96703 : fini_modules (cpp_reader *reader, void *cookie, bool has_inits)
   23956              : {
   23957        96703 :   if (cookie)
   23958         2860 :     late_finish_module (reader,
   23959              :                         static_cast<module_processing_cookie *> (cookie),
   23960              :                         has_inits);
   23961              : 
   23962              :   /* We're done with the macro tables now.  */
   23963        96703 :   vec_free (macro_exports);
   23964        96703 :   vec_free (macro_imports);
   23965        96703 :   headers = NULL;
   23966              : 
   23967              :   /* We're now done with everything but the module names.  */
   23968        96703 :   set_cmi_repo (NULL);
   23969        96703 :   if (mapper)
   23970              :     {
   23971         4670 :       timevar_start (TV_MODULE_MAPPER);
   23972         4670 :       module_client::close_module_client (0, mapper);
   23973         4670 :       mapper = nullptr;
   23974         4670 :       timevar_stop (TV_MODULE_MAPPER);
   23975              :     }
   23976        96703 :   module_state_config::release ();
   23977              : 
   23978              : #if CHECKING_P
   23979        96703 :   note_defs = NULL;
   23980              : #endif
   23981              : 
   23982        96703 :   if (modules)
   23983         7577 :     for (unsigned ix = modules->length (); --ix;)
   23984         2907 :       if (module_state *state = (*modules)[ix])
   23985         2907 :         state->release ();
   23986              : 
   23987              :   /* No need to lookup modules anymore.  */
   23988        96703 :   modules_hash = NULL;
   23989              : 
   23990              :   /* Or entity array.  We still need the entity map to find import numbers.  */
   23991        96703 :   vec_free (entity_ary);
   23992        96703 :   entity_ary = NULL;
   23993              : 
   23994              :   /* Or remember any pending entities.  */
   23995       101373 :   delete pending_table;
   23996        96703 :   pending_table = NULL;
   23997              : 
   23998              :   /* Or any keys -- Let it go!  */
   23999        99132 :   delete keyed_table;
   24000        96703 :   keyed_table = NULL;
   24001              : 
   24002              :   /* Allow a GC, we've possibly made much data unreachable.  */
   24003        96703 :   ggc_collect ();
   24004        96703 : }
   24005              : 
   24006              : /* If CODE is a module option, handle it & return true.  Otherwise
   24007              :    return false.  For unknown reasons I cannot get the option
   24008              :    generation machinery to set fmodule-mapper or -fmodule-header to
   24009              :    make a string type option variable.  */
   24010              : 
   24011              : bool
   24012      1920832 : handle_module_option (unsigned code, const char *str, int)
   24013              : {
   24014      1920832 :   auto hdr = CMS_header;
   24015              : 
   24016      1920832 :   switch (opt_code (code))
   24017              :     {
   24018           42 :     case OPT_fmodule_mapper_:
   24019           42 :       module_mapper_name = str;
   24020           42 :       return true;
   24021              : 
   24022           12 :     case OPT_fmodule_header_:
   24023           12 :       {
   24024           12 :         if (!strcmp (str, "user"))
   24025              :           hdr = CMS_user;
   24026           12 :         else if (!strcmp (str, "system"))
   24027              :           hdr = CMS_system;
   24028              :         else
   24029            0 :           error ("unknown header kind %qs", str);
   24030              :       }
   24031              :       /* Fallthrough.  */
   24032              : 
   24033          916 :     case OPT_fmodule_header:
   24034          916 :       flag_header_unit = hdr;
   24035          916 :       flag_modules = 1;
   24036          916 :       return true;
   24037              : 
   24038            1 :     case OPT_flang_info_include_translate_:
   24039            1 :       vec_safe_push (note_includes, str);
   24040            1 :       return true;
   24041              : 
   24042            6 :     case OPT_flang_info_module_cmi_:
   24043            6 :       vec_safe_push (note_cmis, str);
   24044            6 :       return true;
   24045              : 
   24046              :     default:
   24047              :       return false;
   24048              :     }
   24049              : }
   24050              : 
   24051              : /* Set preprocessor callbacks and options for modules.  */
   24052              : 
   24053              : void
   24054        98418 : module_preprocess_options (cpp_reader *reader)
   24055              : {
   24056        98418 :   gcc_checking_assert (!lang_hooks.preprocess_undef);
   24057        98418 :   if (modules_p ())
   24058              :     {
   24059         4854 :       auto *cb = cpp_get_callbacks (reader);
   24060              : 
   24061         4854 :       cb->translate_include = maybe_translate_include;
   24062         4854 :       cb->user_deferred_macro = module_state::deferred_macro;
   24063         4854 :       if (flag_header_unit)
   24064              :         {
   24065              :           /* If the preprocessor hook is already in use, that
   24066              :              implementation will call the undef langhook.  */
   24067          913 :           if (cb->undef)
   24068            0 :             lang_hooks.preprocess_undef = module_state::undef_macro;
   24069              :           else
   24070          913 :             cb->undef = module_state::undef_macro;
   24071              :         }
   24072         4854 :       auto *opt = cpp_get_options (reader);
   24073         4854 :       opt->module_directives = true;
   24074         4854 :       if (flag_no_output)
   24075           18 :         opt->directives_only = true;
   24076         4854 :       if (opt->main_search == CMS_none)
   24077         4852 :         opt->main_search = cpp_main_search (flag_header_unit);
   24078              :     }
   24079        98418 : }
   24080              : 
   24081              : #include "gt-cp-module.h"
        

Generated by: LCOV version 2.4-beta

LCOV profile is generated on x86_64 machine using following configure options: configure --disable-bootstrap --enable-coverage=opt --enable-languages=c,c++,fortran,go,jit,lto,rust,m2 --enable-host-shared. GCC test suite is run with the built compiler.