LCOV - code coverage report
Current view: top level - gcc/cp - module.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 94.3 % 11138 10506
Test Date: 2026-06-20 15:32:29 Functions: 97.9 % 476 466
Legend: Lines:     hit not hit

            Line data    Source code
       1              : /* C++ modules.  Experimental!
       2              :    Copyright (C) 2017-2026 Free Software Foundation, Inc.
       3              :    Written by Nathan Sidwell <nathan@acm.org> while at FaceBook
       4              : 
       5              :    This file is part of GCC.
       6              : 
       7              :    GCC is free software; you can redistribute it and/or modify it
       8              :    under the terms of the GNU General Public License as published by
       9              :    the Free Software Foundation; either version 3, or (at your option)
      10              :    any later version.
      11              : 
      12              :    GCC is distributed in the hope that it will be useful, but
      13              :    WITHOUT ANY WARRANTY; without even the implied warranty of
      14              :    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      15              :    General Public License for more details.
      16              : 
      17              : You should have received a copy of the GNU General Public License
      18              : along with GCC; see the file COPYING3.  If not see
      19              : <http://www.gnu.org/licenses/>.  */
      20              : 
      21              : /* Comments in this file have a non-negligible chance of being wrong
      22              :    or at least inaccurate.  Due to (a) my misunderstanding, (b)
      23              :    ambiguities that I have interpreted differently to original intent
      24              :    (c) changes in the specification, (d) my poor wording, (e) source
      25              :    changes.  */
      26              : 
      27              : /* (Incomplete) Design Notes
      28              : 
      29              :    A hash table contains all module names.  Imported modules are
      30              :    present in a modules array, which by construction places an
      31              :    import's dependencies before the import itself.  The single
      32              :    exception is the current TU, which always occupies slot zero (even
      33              :    when it is not a module).
      34              : 
      35              :    Imported decls occupy an entity_ary, an array of binding_slots, indexed
      36              :    by importing module and index within that module.  A flat index is
      37              :    used, as each module reserves a contiguous range of indices.
      38              :    Initially each slot indicates the CMI section containing the
      39              :    streamed decl.  When the decl is imported it will point to the decl
      40              :    itself.
      41              : 
      42              :    Additionally each imported decl is mapped in the entity_map via its
      43              :    DECL_UID to the flat index in the entity_ary.  Thus we can locate
      44              :    the index for any imported decl by using this map and then
      45              :    de-flattening the index via a binary search of the module vector.
      46              :    Cross-module references are by (remapped) module number and
      47              :    module-local index.
      48              : 
      49              :    Each importable DECL contains several flags.  The simple set are
      50              :    DECL_MODULE_EXPORT_P, DECL_MODULE_PURVIEW_P, DECL_MODULE_ATTACH_P
      51              :    and DECL_MODULE_IMPORT_P.  The first indicates whether it is
      52              :    exported, the second whether it is in module or header-unit
      53              :    purview.  The third indicates it is attached to the named module in
      54              :    whose purview it resides and the fourth indicates whether it was an
      55              :    import into this TU or not.  DECL_MODULE_ATTACH_P will be false for
      56              :    all decls in a header-unit, and for those in a named module inside
      57              :    a linkage declaration.
      58              : 
      59              :    The more detailed flags are DECL_MODULE_PARTITION_P,
      60              :    DECL_MODULE_ENTITY_P.  The first is set in a primary interface unit
      61              :    on decls that were read from module partitions (these will have
      62              :    DECL_MODULE_IMPORT_P set too).  Such decls will be streamed out to
      63              :    the primary's CMI.  DECL_MODULE_ENTITY_P is set when an entity is
      64              :    imported, even if it matched a non-imported entity.  Such a decl
      65              :    will not have DECL_MODULE_IMPORT_P set, even though it has an entry
      66              :    in the entity map and array.
      67              : 
      68              :    Header units are module-like.
      69              : 
      70              :    For namespace-scope lookup, the decls for a particular module are
      71              :    held located in a sparse array hanging off the binding of the name.
      72              :    This is partitioned into two: a few fixed slots at the start
      73              :    followed by the sparse slots afterwards.  By construction we only
      74              :    need to append new slots to the end -- there is never a need to
      75              :    insert in the middle.  The fixed slots are MODULE_SLOT_CURRENT for
      76              :    the current TU (regardless of whether it is a module or not),
      77              :    MODULE_SLOT_GLOBAL and MODULE_SLOT_PARTITION.  These latter two
      78              :    slots are used for merging entities across the global module and
      79              :    module partitions respectively.  MODULE_SLOT_PARTITION is only
      80              :    present in a module.  Neither of those two slots is searched during
      81              :    name lookup -- they are internal use only.  This vector is created
      82              :    lazily once we require it, if there is only a declaration from the
      83              :    current TU, a regular binding is present.  It is converted on
      84              :    demand.
      85              : 
      86              :    OPTIMIZATION: Outside of the current TU, we only need ADL to work.
      87              :    We could optimize regular lookup for the current TU by glomming all
      88              :    the visible decls on its slot.  Perhaps wait until design is a
      89              :    little more settled though.
      90              : 
      91              :    There is only one instance of each extern-linkage namespace.  It
      92              :    appears in every module slot that makes it visible.  It also
      93              :    appears in MODULE_SLOT_GLOBAL.  (It is an ODR violation if they
      94              :    collide with some other global module entity.)  We also have an
      95              :    optimization that shares the slot for adjacent modules that declare
      96              :    the same such namespace.
      97              : 
      98              :    A module interface compilation produces a Compiled Module Interface
      99              :    (CMI).  The format used is Encapsulated Lazy Records Of Numbered
     100              :    Declarations, which is essentially ELF's section encapsulation. (As
     101              :    all good nerds are aware, Elrond is half Elf.)  Some sections are
     102              :    named, and contain information about the module as a whole (indices
     103              :    etc), and other sections are referenced by number.  Although I
     104              :    don't defend against actively hostile CMIs, there is some
     105              :    checksumming involved to verify data integrity.  When dumping out
     106              :    an interface, we generate a graph of all the
     107              :    independently-redeclarable DECLS that are needed, and the decls
     108              :    they reference.  From that we determine the strongly connected
     109              :    components (SCC) within this TU.  Each SCC is dumped to a separate
     110              :    numbered section of the CMI.  We generate a binding table section,
     111              :    mapping each namespace&name to a defining section.  This allows
     112              :    lazy loading.
     113              : 
     114              :    Lazy loading employs mmap to map a read-only image of the CMI.
     115              :    It thus only occupies address space and is paged in on demand,
     116              :    backed by the CMI file itself.  If mmap is unavailable, regular
     117              :    FILEIO is used.  Also, there's a bespoke ELF reader/writer here,
     118              :    which implements just the section table and sections (including
     119              :    string sections) of a 32-bit ELF in host byte-order.  You can of
     120              :    course inspect it with readelf.  I figured 32-bit is sufficient,
     121              :    for a single module.  I detect running out of section numbers, but
     122              :    do not implement the ELF overflow mechanism.  At least you'll get
     123              :    an error if that happens.
     124              : 
     125              :    We do not separate declarations and definitions.  My guess is that
     126              :    if you refer to the declaration, you'll also need the definition
     127              :    (template body, inline function, class definition etc).  But this
     128              :    does mean we can get larger SCCs than if we separated them.  It is
     129              :    unclear whether this is a win or not.
     130              : 
     131              :    Notice that we embed section indices into the contents of other
     132              :    sections.  Thus random manipulation of the CMI file by ELF tools
     133              :    may well break it.  The kosher way would probably be to introduce
     134              :    indirection via section symbols, but that would require defining a
     135              :    relocation type.
     136              : 
     137              :    Notice that lazy loading of one module's decls can cause lazy
     138              :    loading of other decls in the same or another module.  Clearly we
     139              :    want to avoid loops.  In a correct program there can be no loops in
     140              :    the module dependency graph, and the above-mentioned SCC algorithm
     141              :    places all intra-module circular dependencies in the same SCC.  It
     142              :    also orders the SCCs wrt each other, so dependent SCCs come first.
     143              :    As we load dependent modules first, we know there can be no
     144              :    reference to a higher-numbered module, and because we write out
     145              :    dependent SCCs first, likewise for SCCs within the module.  This
     146              :    allows us to immediately detect broken references.  When loading,
     147              :    we must ensure the rest of the compiler doesn't cause some
     148              :    unconnected load to occur (for instance, instantiate a template).
     149              : 
     150              : Classes used:
     151              : 
     152              :    dumper - logger
     153              : 
     154              :    data - buffer
     155              : 
     156              :    bytes_in : data - scalar reader
     157              :    bytes_out : data - scalar writer
     158              : 
     159              :    bytes_in::bits_in - bit stream reader
     160              :    bytes_out::bits_out - bit stream writer
     161              : 
     162              :    elf - ELROND format
     163              :    elf_in : elf - ELROND reader
     164              :    elf_out : elf - ELROND writer
     165              : 
     166              :    trees_in : bytes_in - tree reader
     167              :    trees_out : bytes_out - tree writer
     168              : 
     169              :    depset - dependency set
     170              :    depset::hash - hash table of depsets
     171              :    depset::tarjan - SCC determinator
     172              : 
     173              :    uidset<T> - set T's related to a UID
     174              :    uidset<T>::hash hash table of uidset<T>
     175              : 
     176              :    loc_spans - location map data
     177              : 
     178              :    module_state - module object
     179              : 
     180              :    slurping - data needed during loading
     181              : 
     182              :    macro_import - imported macro data
     183              :    macro_export - exported macro data
     184              : 
     185              :    The ELROND objects use mmap, for both reading and writing.  If mmap
     186              :    is unavailable, fileno IO is used to read and write blocks of data.
     187              : 
     188              :    The mapper object uses fileno IO to communicate with the server or
     189              :    program.   */
     190              : 
     191              : /* In experimental (trunk) sources, MODULE_VERSION is a #define passed
     192              :    in from the Makefile.  It records the modification date of the
     193              :    source directory -- that's the only way to stay sane.  In release
     194              :    sources, we (plan to) use the compiler's major.minor versioning.
     195              :    While the format might not change between at minor versions, it
     196              :    seems simplest to tie the two together.  There's no concept of
     197              :    inter-version compatibility.  */
     198              : #define IS_EXPERIMENTAL(V) ((V) >= (1U << 20))
     199              : #define MODULE_MAJOR(V) ((V) / 10000)
     200              : #define MODULE_MINOR(V) ((V) % 10000)
     201              : #define EXPERIMENT(A,B) (IS_EXPERIMENTAL (MODULE_VERSION) ? (A) : (B))
     202              : #ifndef MODULE_VERSION
     203              : #include "bversion.h"
     204              : #define MODULE_VERSION (BUILDING_GCC_MAJOR * 10000U + BUILDING_GCC_MINOR)
     205              : #elif !IS_EXPERIMENTAL (MODULE_VERSION)
     206              : #error "This is not the version I was looking for."
     207              : #endif
     208              : 
     209              : #define _DEFAULT_SOURCE 1 /* To get TZ field of struct tm, if available.  */
     210              : #include "config.h"
     211              : #define INCLUDE_STRING
     212              : #define INCLUDE_VECTOR
     213              : #include "system.h"
     214              : #include "coretypes.h"
     215              : #include "cp-tree.h"
     216              : #include "timevar.h"
     217              : #include "stringpool.h"
     218              : #include "dumpfile.h"
     219              : #include "bitmap.h"
     220              : #include "cgraph.h"
     221              : #include "varasm.h"
     222              : #include "tree-iterator.h"
     223              : #include "cpplib.h"
     224              : #include "mkdeps.h"
     225              : #include "incpath.h"
     226              : #include "libiberty.h"
     227              : #include "stor-layout.h"
     228              : #include "version.h"
     229              : #include "tree-diagnostic.h"
     230              : #include "toplev.h"
     231              : #include "opts.h"
     232              : #include "attribs.h"
     233              : #include "intl.h"
     234              : #include "langhooks.h"
     235              : #include "contracts.h"
     236              : /* This TU doesn't need or want to see the networking.  */
     237              : #define CODY_NETWORKING 0
     238              : #include "mapper-client.h"
     239              : #include <zlib.h> // for crc32, crc32_combine
     240              : 
     241              : #if 0 // 1 for testing no mmap
     242              : #define MAPPED_READING 0
     243              : #define MAPPED_WRITING 0
     244              : #else
     245              : #if HAVE_MMAP_FILE && HAVE_MUNMAP && HAVE_MSYNC
     246              : /* mmap, munmap, msync.  */
     247              : #define MAPPED_READING 1
     248              : #if HAVE_SYSCONF && defined (_SC_PAGE_SIZE)
     249              : /* sysconf (_SC_PAGE_SIZE), ftruncate  */
     250              : /* posix_fallocate used if available.  */
     251              : #define MAPPED_WRITING 1
     252              : #else
     253              : #define MAPPED_WRITING 0
     254              : #endif
     255              : #else
     256              : #define MAPPED_READING 0
     257              : #define MAPPED_WRITING 0
     258              : #endif
     259              : #endif
     260              : 
     261              : /* 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       292386 : static inline cpp_hashnode *cpp_node (tree id)
     280              : {
     281       292386 :   return CPP_HASHNODE (GCC_IDENT_TO_HT_IDENT (id));
     282              : }
     283              : 
     284       156660 : 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       156660 : #pragma GCC diagnostic push
     291       156660 : #pragma GCC diagnostic ignored "-Warray-bounds"
     292       156660 :   return HT_IDENT_TO_GCC_IDENT (HT_NODE (const_cast<cpp_hashnode *> (node)));
     293       156660 : #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         4172 : version2string (unsigned version, verstr_t &out)
     310              : {
     311         4172 :   unsigned major = MODULE_MAJOR (version);
     312         4172 :   unsigned minor = MODULE_MINOR (version);
     313              : 
     314         4172 :   if (IS_EXPERIMENTAL (version))
     315         4172 :     sprintf (out, "%04u/%02u/%02u-%02u:%02u%s",
     316         4172 :              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         4172 : }
     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 pathnames.  */
     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         2904 :   class allocator {
     359              :   public:
     360              :     /* Tools tend to moan if the dtor's not virtual.  */
     361       102669 :     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       900079 :   data ()
     383       900079 :     :buffer (NULL), size (0), pos (0)
     384              :   {
     385              :   }
     386       914561 :   ~data ()
     387              :   {
     388              :     /* Make sure the derived and/or using class know what they're
     389              :        doing.  */
     390       914561 :     gcc_checking_assert (!buffer);
     391       914561 :   }
     392              : 
     393              : protected:
     394    665269971 :   char *use (unsigned count)
     395              :   {
     396    665269971 :     if (size < pos + count)
     397              :       return NULL;
     398    665269971 :     char *res = &buffer[pos];
     399    665269971 :     pos += count;
     400    371881107 :     return res;
     401              :   }
     402              : 
     403              :   unsigned calc_crc (unsigned) const;
     404              : 
     405              : public:
     406     51467192 :   void unuse (unsigned count)
     407              :   {
     408     51467192 :     pos -= count;
     409        28232 :   }
     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       820846 : data::allocator::grow (data &obj, unsigned needed, bool exact)
     423              : {
     424       820846 :   gcc_checking_assert (needed ? needed > obj.size : !obj.size);
     425       820846 :   if (!needed)
     426              :     /* Pick a default size.  */
     427       341760 :     needed = EXPERIMENT (100, 1000);
     428              : 
     429       820846 :   if (!exact)
     430       812476 :     needed *= 2;
     431       820846 :   obj.buffer = grow (obj.buffer, needed);
     432       820846 :   if (obj.buffer)
     433       820846 :     obj.size = needed;
     434              :   else
     435            0 :     obj.pos = obj.size = 0;
     436       820846 : }
     437              : 
     438              : /* Free a buffer.  */
     439              : 
     440              : void
     441       356292 : data::allocator::shrink (data &obj)
     442              : {
     443            0 :   shrink (obj.buffer);
     444       356292 :   obj.buffer = NULL;
     445       356292 :   obj.size = 0;
     446            0 : }
     447              : 
     448              : char *
     449        11748 : data::allocator::grow (char *ptr, unsigned needed)
     450              : {
     451        11748 :   return XRESIZEVAR (char, ptr, needed);
     452              : }
     453              : 
     454              : void
     455        14520 : data::allocator::shrink (char *ptr)
     456              : {
     457        14520 :   XDELETEVEC (ptr);
     458        14520 : }
     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       566745 : data::calc_crc (unsigned l) const
     465              : {
     466       566745 :   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       235225 :   bytes_in ()
     482       235225 :     : parent (), overrun (false)
     483              :   {
     484              :   }
     485       238089 :   ~bytes_in ()
     486              :   {
     487        15943 :   }
     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      1789430 :   bool more_p () const
     499              :   {
     500      1789430 :     return pos != size;
     501              :   }
     502              : 
     503              : public:
     504              :   /* Start reading at OFFSET.  */
     505          805 :   void random_access (unsigned offset)
     506              :   {
     507          805 :     if (offset > size)
     508            0 :       set_overrun ();
     509          805 :     pos = offset;
     510              :   }
     511              : 
     512              : public:
     513      1630914 :   void align (unsigned boundary)
     514              :   {
     515      1630914 :     if (unsigned pad = pos & (boundary - 1))
     516      3158521 :       read (boundary - pad);
     517              :   }
     518              : 
     519              : public:
     520    293388864 :   const char *read (unsigned count)
     521              :   {
     522      1527607 :     char *ptr = use (count);
     523    293388864 :     if (!ptr)
     524            0 :       set_overrun ();
     525    238357324 :     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       236364 :   unsigned get_crc () const
     532              :   {
     533       236364 :     return *(const unsigned *)&buffer[0];
     534              :   }
     535              : 
     536              : public:
     537              :   /* Manipulate the overrun flag.  */
     538    170940945 :   bool get_overrun () const
     539              :   {
     540    170940945 :     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       233314 : bytes_in::check_crc () const
     571              : {
     572       233314 :   if (size < 4)
     573              :     return false;
     574              : 
     575       233314 :   unsigned c_crc = calc_crc (size);
     576       233314 :   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       658922 :   bytes_out (allocator *memory)
     595       658922 :     : parent (), memory (memory)
     596              :   {
     597              :   }
     598       658922 :   ~bytes_out ()
     599              :   {
     600       689042 :   }
     601              : 
     602              : public:
     603    941235468 :   bool streaming_p () const
     604              :   {
     605    941235468 :     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      2217288 :   void align (unsigned boundary)
     619              :   {
     620      2217288 :     if (unsigned pad = pos & (boundary - 1))
     621      2074997 :       write (boundary - pad);
     622      2217288 :   }
     623              : 
     624              : public:
     625    371881107 :   char *write (unsigned count, bool exact = false)
     626              :   {
     627    371881107 :     if (size < pos + count)
     628       464540 :       memory->grow (*this, pos + count, exact);
     629    371881107 :     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        19589 :   void str (const char *ptr)
     644              :   {
     645        19589 :     str (ptr, strlen (ptr));
     646        19589 :   }
     647       300911 :   void cpp_node (const cpp_hashnode *node)
     648              :   {
     649       300911 :     str ((const char *)NODE_NAME (node), NODE_LEN (node));
     650        13064 :   }
     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     51411261 : bit_flush (data& bits, uint32_t& bit_val, unsigned& bit_pos)
     678              : {
     679     51411261 :   gcc_assert (bit_pos);
     680     51411261 :   unsigned bytes = (bit_pos + 7) / 8;
     681     51411261 :   bits.unuse (4 - bytes);
     682     51411261 :   bit_pos = 0;
     683     51411261 :   bit_val = 0;
     684     51411261 :   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     16640197 :   bits_in (bytes_in& in)
     707     16640197 :     : in (in)
     708              :   { }
     709              : 
     710     16640197 :   ~bits_in ()
     711              :   {
     712     15628541 :     bflush ();
     713     16640197 :   }
     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     34511155 :   void bflush ()
     721              :   {
     722     34511155 :     if (bit_pos)
     723     18882614 :       bit_flush (in, bit_val, bit_pos);
     724     34511155 :   }
     725              : 
     726              :   /* Read one bit.  */
     727    570444833 :   bool b ()
     728              :   {
     729    570444833 :     if (!bit_pos)
     730     24962844 :       bit_val = in.u32 ();
     731    570444833 :     bool x = (bit_val >> bit_pos) & 1;
     732    570444833 :     bit_pos = (bit_pos + 1) % 32;
     733    570444833 :     return x;
     734              :   }
     735              : };
     736              : 
     737              : /* Factory function for bits_in.  */
     738              : 
     739              : bytes_in::bits_in
     740     16640197 : bytes_in::stream_bits ()
     741              : {
     742     16640197 :   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     21599093 :   bits_out (bytes_out& out)
     754     21599093 :     : out (out)
     755              :   { }
     756              : 
     757     21599093 :   ~bits_out ()
     758              :   {
     759        78287 :     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     44714165 :   void bflush ()
     768              :   {
     769     44714165 :     if (bit_pos)
     770              :       {
     771     24472260 :         out.u32 (bit_val);
     772     24472260 :         out.lengths[2] += bit_flush (out, bit_val, bit_pos);
     773              :       }
     774     44714165 :     out.spans[2]++;
     775     44714165 :     is_set = -1;
     776     44714165 :   }
     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    740067093 :   void b (bool x)
     783              :   {
     784    740067093 :     if (is_set != x)
     785              :       {
     786     77310121 :         is_set = x;
     787     77310121 :         out.spans[x]++;
     788              :       }
     789    740067093 :     out.lengths[x]++;
     790    740067093 :     bit_val |= unsigned (x) << bit_pos++;
     791    740067093 :     if (bit_pos == 32)
     792              :       {
     793      8056387 :         out.u32 (bit_val);
     794      8056387 :         out.lengths[2] += bit_flush (out, bit_val, bit_pos);
     795              :       }
     796    740067093 :   }
     797              : };
     798              : 
     799              : /* Factory function for bits_out.  */
     800              : 
     801              : bytes_out::bits_out
     802     21599093 : bytes_out::stream_bits ()
     803              : {
     804     21599093 :   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       336188 : bytes_out::set_crc (unsigned *crc_ptr)
     816              : {
     817       336188 :   if (crc_ptr)
     818              :     {
     819       333431 :       gcc_checking_assert (pos >= 4);
     820              : 
     821       333431 :       unsigned crc = calc_crc (pos);
     822       333431 :       unsigned accum = *crc_ptr;
     823              :       /* Only mix the existing *CRC_PTR if it is non-zero.  */
     824       333431 :       accum = accum ? crc32_combine (accum, crc, pos - 4) : crc;
     825       333431 :       *crc_ptr = accum;
     826              : 
     827              :       /* Buffer will be sufficiently aligned.  */
     828       333431 :       *(unsigned *)buffer = crc;
     829              :     }
     830       336188 : }
     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     32537586 : bytes_out::u32 (unsigned val)
     839              : {
     840     32537586 :   if (char *ptr = write (4))
     841              :     {
     842     32537586 :       ptr[0] = val;
     843     32537586 :       ptr[1] = val >> 8;
     844     32537586 :       ptr[2] = val >> 16;
     845     32537586 :       ptr[3] = val >> 24;
     846              :     }
     847     32537586 : }
     848              : 
     849              : unsigned
     850     24972426 : bytes_in::u32 ()
     851              : {
     852     24972426 :   unsigned val = 0;
     853     24972426 :   if (const char *ptr = read (4))
     854              :     {
     855     24972426 :       val |= (unsigned char)ptr[0];
     856     24972426 :       val |= (unsigned char)ptr[1] << 8;
     857     24972426 :       val |= (unsigned char)ptr[2] << 16;
     858     24972426 :       val |= (unsigned char)ptr[3] << 24;
     859              :     }
     860              : 
     861     24972426 :   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    131086060 : bytes_out::i (int v)
     887              : {
     888    131086060 :   if (char *ptr = write (1))
     889              :     {
     890    131086060 :       if (v <= 0x3f && v >= -0x40)
     891    102111475 :         *ptr = v & 0x7f;
     892              :       else
     893              :         {
     894     28974585 :           unsigned bytes = 0;
     895     28974585 :           int probe;
     896     28974585 :           if (v >= 0)
     897            0 :             for (probe = v >> 8; probe > 0x7; probe >>= 8)
     898            0 :               bytes++;
     899              :           else
     900     44269725 :             for (probe = v >> 8; probe < -0x8; probe >>= 8)
     901     15295140 :               bytes++;
     902     28974585 :           *ptr = 0x80 | bytes << 4 | (probe & 0xf);
     903     28974585 :           if ((ptr = write (++bytes)))
     904     73244310 :             for (; bytes--; v >>= 8)
     905     44269725 :               ptr[bytes] = v & 0xff;
     906              :         }
     907              :     }
     908    131086060 : }
     909              : 
     910              : int
     911    102077409 : bytes_in::i ()
     912              : {
     913    102077409 :   int v = 0;
     914    102077409 :   if (const char *ptr = read (1))
     915              :     {
     916    102077409 :       v = *ptr & 0xff;
     917    102077409 :       if (v & 0x80)
     918              :         {
     919     23284176 :           unsigned bytes = (v >> 4) & 0x7;
     920     23284176 :           v &= 0xf;
     921     23284176 :           if (v & 0x8)
     922     23284176 :             v |= -1 ^ 0x7;
     923              :           /* unsigned necessary due to left shifts of -ve values.  */
     924     23284176 :           unsigned uv = unsigned (v);
     925     23284176 :           if ((ptr = read (++bytes)))
     926     60170332 :             while (bytes--)
     927     36886156 :               uv = (uv << 8) | (*ptr++ & 0xff);
     928     23284176 :           v = int (uv);
     929              :         }
     930     78793233 :       else if (v & 0x40)
     931      9975949 :         v |= -1 ^ 0x3f;
     932              :     }
     933              : 
     934    102077409 :   return v;
     935              : }
     936              : 
     937              : void
     938    112013343 : bytes_out::u (unsigned v)
     939              : {
     940    112013343 :   if (char *ptr = write (1))
     941              :     {
     942    112013343 :       if (v <= 0x7f)
     943     97376591 :         *ptr = v;
     944              :       else
     945              :         {
     946     14636752 :           unsigned bytes = 0;
     947     14636752 :           unsigned probe;
     948     17428336 :           for (probe = v >> 8; probe > 0xf; probe >>= 8)
     949      2791584 :             bytes++;
     950     14636752 :           *ptr = 0x80 | bytes << 4 | probe;
     951     14636752 :           if ((ptr = write (++bytes)))
     952     32065088 :             for (; bytes--; v >>= 8)
     953     17428336 :               ptr[bytes] = v & 0xff;
     954              :         }
     955              :     }
     956    112013343 : }
     957              : 
     958              : unsigned
     959     89263453 : bytes_in::u ()
     960              : {
     961     89263453 :   unsigned v = 0;
     962              : 
     963     89263453 :   if (const char *ptr = read (1))
     964              :     {
     965     89263453 :       v = *ptr & 0xff;
     966     89263453 :       if (v & 0x80)
     967              :         {
     968     11766629 :           unsigned bytes = (v >> 4) & 0x7;
     969     11766629 :           v &= 0xf;
     970     11766629 :           if ((ptr = read (++bytes)))
     971     25849161 :             while (bytes--)
     972     14082532 :               v = (v << 8) | (*ptr++ & 0xff);
     973              :         }
     974              :     }
     975              : 
     976     89263453 :   return v;
     977              : }
     978              : 
     979              : void
     980     26761002 : bytes_out::wi (HOST_WIDE_INT v)
     981              : {
     982     26761002 :   if (char *ptr = write (1))
     983              :     {
     984     26761002 :       if (v <= 0x3f && v >= -0x40)
     985      5237439 :         *ptr = v & 0x7f;
     986              :       else
     987              :         {
     988     21523563 :           unsigned bytes = 0;
     989     21523563 :           HOST_WIDE_INT probe;
     990     21523563 :           if (v >= 0)
     991     78698426 :             for (probe = v >> 8; probe > 0x7; probe >>= 8)
     992     57178079 :               bytes++;
     993              :           else
     994        10238 :             for (probe = v >> 8; probe < -0x8; probe >>= 8)
     995         7022 :               bytes++;
     996     21523563 :           *ptr = 0x80 | bytes << 4 | (probe & 0xf);
     997     21523563 :           if ((ptr = write (++bytes)))
     998    100232227 :             for (; bytes--; v >>= 8)
     999     78708664 :               ptr[bytes] = v & 0xff;
    1000              :         }
    1001              :     }
    1002     26761002 : }
    1003              : 
    1004              : HOST_WIDE_INT
    1005     20413122 : bytes_in::wi ()
    1006              : {
    1007     20413122 :   HOST_WIDE_INT v = 0;
    1008     20413122 :   if (const char *ptr = read (1))
    1009              :     {
    1010     20413122 :       v = *ptr & 0xff;
    1011     20413122 :       if (v & 0x80)
    1012              :         {
    1013     18453128 :           unsigned bytes = (v >> 4) & 0x7;
    1014     18453128 :           v &= 0xf;
    1015     18453128 :           if (v & 0x8)
    1016         2119 :             v |= -1 ^ 0x7;
    1017              :           /* unsigned necessary due to left shifts of -ve values.  */
    1018     18453128 :           unsigned HOST_WIDE_INT uv = (unsigned HOST_WIDE_INT) v;
    1019     18453128 :           if ((ptr = read (++bytes)))
    1020     85273154 :             while (bytes--)
    1021     66820026 :               uv = (uv << 8) | (*ptr++ & 0xff);
    1022     18453128 :           v = (HOST_WIDE_INT) uv;
    1023              :         }
    1024      1959994 :       else if (v & 0x40)
    1025         8988 :         v |= -1 ^ 0x3f;
    1026              :     }
    1027              : 
    1028     20413122 :   return v;
    1029              : }
    1030              : 
    1031              : /* unsigned wide ints are just written as signed wide ints.  */
    1032              : 
    1033              : inline void
    1034     26760134 : bytes_out::wu (unsigned HOST_WIDE_INT v)
    1035              : {
    1036     26760134 :   wi ((HOST_WIDE_INT) v);
    1037              : }
    1038              : 
    1039              : inline unsigned HOST_WIDE_INT
    1040     20412548 : bytes_in::wu ()
    1041              : {
    1042     40217743 :   return (unsigned HOST_WIDE_INT) wi ();
    1043              : }
    1044              : 
    1045              : /* size_t written as unsigned or unsigned wide int.  */
    1046              : 
    1047              : inline void
    1048      2175191 : bytes_out::z (size_t s)
    1049              : {
    1050      2175191 :   if (sizeof (s) == sizeof (unsigned))
    1051              :     u (s);
    1052              :   else
    1053      4316872 :     wu (s);
    1054           12 : }
    1055              : 
    1056              : inline size_t
    1057      1613798 : bytes_in::z ()
    1058              : {
    1059      1613798 :   if (sizeof (size_t) == sizeof (unsigned))
    1060              :     return u ();
    1061              :   else
    1062      3227596 :     return wu ();
    1063              : }
    1064              : 
    1065              : /* location_t written as 32- or 64-bit as needed.  */
    1066              : 
    1067     23768808 : inline void bytes_out::loc (location_t l)
    1068              : {
    1069     23768808 :   if (sizeof (location_t) > sizeof (unsigned))
    1070     44953444 :     wu (l);
    1071              :   else
    1072              :     u (l);
    1073      2581415 : }
    1074              : 
    1075     18194404 : inline location_t bytes_in::loc ()
    1076              : {
    1077     18194404 :   if (sizeof (location_t) > sizeof (unsigned))
    1078     36385771 :     return wu ();
    1079              :   else
    1080              :     return u ();
    1081              : }
    1082              : 
    1083              : /* Buffer simply memcpied.  */
    1084              : void *
    1085      2217288 : bytes_out::buf (size_t len)
    1086              : {
    1087      2217288 :   align (sizeof (void *) * 2);
    1088      2217288 :   return write (len);
    1089              : }
    1090              : 
    1091              : void
    1092      2164923 : bytes_out::buf (const void *src, size_t len)
    1093              : {
    1094      2164923 :   if (void *ptr = buf (len))
    1095      2164923 :     memcpy (ptr, src, len);
    1096      2164923 : }
    1097              : 
    1098              : const void *
    1099      1630914 : bytes_in::buf (size_t len)
    1100              : {
    1101      1630914 :   align (sizeof (void *) * 2);
    1102      1630914 :   const char *ptr = read (len);
    1103              : 
    1104      1630914 :   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      2175173 : bytes_out::str (const char *string, size_t len)
    1112              : {
    1113      2141675 :   z (len);
    1114      2141675 :   if (len)
    1115              :     {
    1116      2141675 :       gcc_checking_assert (!string[len]);
    1117      2141675 :       buf (string, len + 1);
    1118              :     }
    1119        33498 : }
    1120              : 
    1121              : const char *
    1122      1613792 : bytes_in::str (size_t *len_p)
    1123              : {
    1124      1613792 :   size_t len = z ();
    1125              : 
    1126              :   /* We're about to trust some user data.  */
    1127      1613792 :   if (overrun)
    1128            0 :     len = 0;
    1129      1613792 :   if (len_p)
    1130      1609851 :     *len_p = len;
    1131      1613792 :   const char *str = NULL;
    1132      1613792 :   if (len)
    1133              :     {
    1134      1613521 :       str = reinterpret_cast<const char *> (buf (len + 1));
    1135      1613521 :       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       292657 : bytes_in::cpp_node ()
    1146              : {
    1147       292657 :   size_t len;
    1148       292657 :   const char *s = str (&len);
    1149       292657 :   if (!len)
    1150              :     return NULL;
    1151       292386 :   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        28232 : bytes_out::printf (const char *format, ...)
    1159              : {
    1160        28232 :   va_list args;
    1161              :   /* Exercise buffer expansion.  */
    1162        28232 :   size_t len = EXPERIMENT (10, 500);
    1163              : 
    1164        55931 :   while (char *ptr = write (len))
    1165              :     {
    1166        55931 :       va_start (args, format);
    1167        55931 :       size_t actual = vsnprintf (ptr, len, format, args) + 1;
    1168        55931 :       va_end (args);
    1169        55931 :       if (actual <= len)
    1170              :         {
    1171        28232 :           unuse (len - actual);
    1172        28232 :           break;
    1173              :         }
    1174        27699 :       unuse (len);
    1175        27699 :       len = actual;
    1176        27699 :     }
    1177        28232 : }
    1178              : 
    1179              : void
    1180         5514 : bytes_out::print_time (const char *kind, const tm *time, const char *tz)
    1181              : {
    1182         5514 :   printf ("%stime: %4u/%02u/%02u %02u:%02u:%02u %s",
    1183         5514 :           kind, time->tm_year + 1900, time->tm_mon + 1, time->tm_mday,
    1184         5514 :           time->tm_hour, time->tm_min, time->tm_sec, tz);
    1185         5514 : }
    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         5932 :   elf (int fd, int e)
    1293        11864 :     :hdr (), sectab (), strtab (), fd (fd), err (fd >= 0 ? 0 : e)
    1294              :   {}
    1295         5850 :   ~elf ()
    1296              :   {
    1297         5850 :     gcc_checking_assert (fd < 0 && !hdr.buffer
    1298              :                          && !sectab.buffer && !strtab.buffer);
    1299         5850 :   }
    1300              : 
    1301              : public:
    1302              :   /* Return the error, if we have an error.  */
    1303       456126 :   int get_error () const
    1304              :   {
    1305       456126 :     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         5814 :   bool begin () const
    1319              :   {
    1320         5814 :     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         8277 : elf::end ()
    1353              : {
    1354              :   /* Close the stream and free the section table.  */
    1355         8277 :   if (fd >= 0 && close (fd))
    1356            0 :     set_error (errno);
    1357         8277 :   fd = -1;
    1358              : 
    1359         8277 :   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         3028 :   elf_in (int fd, int e)
    1376         6056 :     :parent (fd, e)
    1377              :   {
    1378              :   }
    1379         2946 :   ~elf_in ()
    1380              :   {
    1381         2946 :   }
    1382              : 
    1383              : public:
    1384       214800 :   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         1078 :   static void release (elf_in *self ATTRIBUTE_UNUSED, bytes_in &bytes)
    1411              :   {
    1412              : #if MAPPED_READING
    1413         1078 :     if (!(self && self->hdr.buffer && bytes.buffer >= self->hdr.buffer
    1414         1078 :           && bytes.buffer < self->hdr.buffer + self->hdr.pos))
    1415              : #endif
    1416            0 :       data::simple_memory.shrink (bytes.buffer);
    1417         1078 :     bytes.buffer = NULL;
    1418         1078 :     bytes.size = 0;
    1419         1078 :   }
    1420              : 
    1421              : public:
    1422       239322 :   static void grow (data &data, unsigned needed)
    1423              :   {
    1424       239322 :     gcc_checking_assert (!data.buffer);
    1425              : #if !MAPPED_READING
    1426              :     data.buffer = XNEWVEC (char, needed);
    1427              : #endif
    1428       239322 :     data.size = needed;
    1429       239322 :   }
    1430       245922 :   static void shrink (data &data)
    1431              :   {
    1432              : #if !MAPPED_READING
    1433              :     XDELETEVEC (data.buffer);
    1434              : #endif
    1435       245922 :     data.buffer = NULL;
    1436       245922 :     data.size = 0;
    1437            0 :   }
    1438              : 
    1439              : public:
    1440       236318 :   const section *get_section (unsigned s) const
    1441              :   {
    1442       236318 :     if (s * sizeof (section) < sectab.size)
    1443       236318 :       return reinterpret_cast<const section *>
    1444       236318 :         (&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       236318 :   bool read (data *d, const section *s)
    1459              :   {
    1460       236318 :     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         8352 :   void release ()
    1471              :   {
    1472         8352 :     shrink (strtab);
    1473           39 :   }
    1474              : 
    1475              : public:
    1476              :   bool begin (location_t);
    1477         5373 :   bool end ()
    1478              :   {
    1479         5373 :     release ();
    1480              : #if MAPPED_READING
    1481         5373 :     if (hdr.buffer)
    1482         2946 :       munmap (hdr.buffer, hdr.pos);
    1483         5373 :     hdr.buffer = NULL;
    1484              : #endif
    1485         5373 :     shrink (sectab);
    1486              : 
    1487         5373 :     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       345284 :   const char *name (unsigned offset)
    1494              :   {
    1495       690568 :     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         2904 :   elf_out (int fd, int e)
    1517         5696 :     :parent (fd, e), identtab (500), pos (0)
    1518              :   {
    1519              : #if MAPPED_WRITING
    1520         2904 :     offset = extent = 0;
    1521         2904 :     page_size = sysconf (_SC_PAGE_SIZE);
    1522         2904 :     if (page_size < SECTION_ALIGN)
    1523              :       /* Something really strange.  */
    1524            0 :       set_error (EINVAL);
    1525              : #endif
    1526         2904 :   }
    1527         2904 :   ~elf_out ()
    1528         2904 :   {
    1529         2904 :     data::simple_memory.shrink (hdr);
    1530         2904 :     data::simple_memory.shrink (sectab);
    1531         2904 :     data::simple_memory.shrink (strtab);
    1532         2904 :   }
    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         5814 :   unsigned get_section_limit () const
    1550              :   {
    1551         5814 :     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        21491 : bytes_in::begin (location_t loc, elf_in *source, const char *name)
    1589              : {
    1590        21491 :   unsigned snum = source->find (name);
    1591              : 
    1592        21491 :   return begin (loc, source, snum, name);
    1593              : }
    1594              : 
    1595              : /* Begin reading section numbered SNUM with NAME (may be NULL).  */
    1596              : 
    1597              : bool
    1598       233314 : bytes_in::begin (location_t loc, elf_in *source, unsigned snum, const char *name)
    1599              : {
    1600       233314 :   if (!source->read (this, source->find (snum))
    1601       233314 :       || !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       233314 :   pos = 4;
    1612       233314 :   return true;
    1613              : }
    1614              : 
    1615              : /* Finish reading a section.  */
    1616              : 
    1617              : bool
    1618       232197 : bytes_in::end (elf_in *src)
    1619              : {
    1620       232197 :   if (more_p ())
    1621           13 :     set_overrun ();
    1622       232197 :   if (overrun)
    1623           13 :     src->set_error ();
    1624              : 
    1625       232197 :   src->shrink (*this);
    1626              : 
    1627       232197 :   return !overrun;
    1628              : }
    1629              : 
    1630              : /* Begin writing buffer.  */
    1631              : 
    1632              : void
    1633       336188 : bytes_out::begin (bool need_crc)
    1634              : {
    1635            0 :   if (need_crc)
    1636            0 :     pos = 4;
    1637            0 :   memory->grow (*this, 0, false);
    1638       314409 : }
    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       336188 : bytes_out::end (elf_out *sink, unsigned name, unsigned *crc_ptr)
    1646              : {
    1647       336188 :   lengths[3] += pos;
    1648       336188 :   spans[3]++;
    1649              : 
    1650       336188 :   set_crc (crc_ptr);
    1651       336188 :   unsigned sec_num = sink->add (*this, !crc_ptr, name);
    1652       336188 :   memory->shrink (*this);
    1653              : 
    1654       336188 :   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       239322 : elf_in::read (data *data, unsigned pos, unsigned length)
    1720              : {
    1721              : #if MAPPED_READING
    1722       239322 :   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       239322 :   grow (*data, length);
    1735              : #if MAPPED_READING
    1736       239322 :   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       239322 :   return data->buffer;
    1747              : }
    1748              : 
    1749              : /* Read section SNUM of TYPE.  Return section pointer or NULL on error.  */
    1750              : 
    1751              : const elf::section *
    1752       236318 : elf_in::find (unsigned snum, unsigned type)
    1753              : {
    1754       236318 :   const section *sec = get_section (snum);
    1755       236318 :   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        21536 : elf_in::find (const char *sname)
    1765              : {
    1766       137928 :   for (unsigned pos = sectab.size; pos -= sizeof (section); )
    1767              :     {
    1768       137928 :       const section *sec
    1769       137928 :         = reinterpret_cast<const section *> (&sectab.buffer[pos]);
    1770              : 
    1771       275856 :       if (0 == strcmp (sname, name (sec->name)))
    1772        21536 :         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         3028 : elf_in::begin (location_t loc)
    1783              : {
    1784         3028 :   if (!parent::begin ())
    1785              :     return false;
    1786              : 
    1787         3004 :   struct stat stat;
    1788         3004 :   unsigned size = 0;
    1789         3004 :   if (!fstat (fd, &stat))
    1790              :     {
    1791              : #if !defined (HOST_LACKS_INODE_NUMBERS)
    1792         3004 :       device = stat.st_dev;
    1793         3004 :       inode = stat.st_ino;
    1794              : #endif
    1795              :       /* Never generate files > 4GB, check we've not been given one.  */
    1796         3004 :       if (stat.st_size == unsigned (stat.st_size))
    1797         3004 :         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         3004 :   void *mapping = mmap (NULL, size, PROT_READ, MAP_SHARED, fd, 0);
    1804         3004 :   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         3004 :   if (madvise (reinterpret_cast <char *> (mapping), size, MADV_RANDOM))
    1813            0 :     goto fail;
    1814              : 
    1815         3004 :   hdr.buffer = (char *)mapping;
    1816              : #else
    1817              :   read (&hdr, 0, sizeof (header));
    1818              : #endif
    1819         3004 :   hdr.pos = size; /* Record size of the file.  */
    1820              : 
    1821         3004 :   const header *h = reinterpret_cast<const header *> (hdr.buffer);
    1822         3004 :   if (!h)
    1823              :     return false;
    1824              : 
    1825         3004 :   if (h->ident.magic[0] != 0x7f
    1826         3004 :       || h->ident.magic[1] != 'E'
    1827         3004 :       || h->ident.magic[2] != 'L'
    1828         3004 :       || 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         3004 :   if (h->ident.klass != MY_CLASS
    1839         3004 :       || h->ident.data != MY_ENDIAN
    1840         3004 :       || h->ident.version != EV_CURRENT
    1841         3004 :       || h->type != ET_NONE
    1842         3004 :       || h->machine != EM_NONE
    1843         3004 :       || h->ident.osabi != OSABI_NONE)
    1844              :     {
    1845            0 :       error_at (loc, "unexpected encapsulation format or type");
    1846            0 :       goto failed;
    1847              :     }
    1848              : 
    1849         3004 :   int e = -1;
    1850         3004 :   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         3004 :   unsigned strndx = h->shstrndx;
    1859         3004 :   unsigned shnum = h->shnum;
    1860         3004 :   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         3004 :   if (!shnum)
    1875            0 :     goto malformed;
    1876              : 
    1877         3004 :   if (!read (&sectab, h->shoff, shnum * sizeof (section)))
    1878            0 :     goto section_table_fail;
    1879              : 
    1880         3004 :   if (strndx == SHN_XINDEX)
    1881            0 :     strndx = get_section (0)->link;
    1882              : 
    1883         3004 :   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         3004 :   if (!(strtab.size && !strtab.buffer[0]
    1889         3004 :         && !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         3004 :   sectab.pos = h->shoff;
    1895         3004 :   strtab.pos = shnum * sizeof (section);
    1896              : #else
    1897              :   shrink (hdr);
    1898              : #endif
    1899              : 
    1900         3004 :   return true;
    1901              : }
    1902              : 
    1903              : /* Create a new mapping.  */
    1904              : 
    1905              : #if MAPPED_WRITING
    1906              : void
    1907         3654 : 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         7155 :   auto allocate = [](int fd, off_t offset, off_t length)
    1912              :     {
    1913              : #ifdef HAVE_POSIX_FALLOCATE
    1914         3501 :       int result = posix_fallocate (fd, offset, length);
    1915         3501 :       if (result != EINVAL && result != ENOTSUP)
    1916         3501 :         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         3654 :   void *mapping = MAP_FAILED;
    1923         3654 :   if (extending && ext < 1024 * 1024)
    1924              :     {
    1925         3333 :       if (allocate (fd, offset, ext * 2))
    1926         3333 :         mapping = mmap (NULL, ext * 2, PROT_READ | PROT_WRITE,
    1927         3333 :                         MAP_SHARED, fd, offset);
    1928         3333 :       if (mapping != MAP_FAILED)
    1929              :         ext *= 2;
    1930              :     }
    1931              :   if (mapping == MAP_FAILED)
    1932              :     {
    1933          321 :       if (!extending || allocate (fd, offset, ext))
    1934          321 :         mapping = mmap (NULL, ext, PROT_READ | PROT_WRITE,
    1935          321 :                         MAP_SHARED, fd, offset);
    1936          321 :       if (mapping == MAP_FAILED)
    1937              :         {
    1938            0 :           set_error (errno);
    1939              :           mapping = NULL;
    1940              :           ext = 0;
    1941              :         }
    1942              :     }
    1943         3654 :   hdr.buffer = (char *)mapping;
    1944         3654 :   extent = ext;
    1945         3654 : }
    1946              : #endif
    1947              : 
    1948              : /* Flush out the current mapping.  */
    1949              : 
    1950              : #if MAPPED_WRITING
    1951              : void
    1952         3660 : elf_out::remove_mapping ()
    1953              : {
    1954         3660 :   if (hdr.buffer)
    1955              :     {
    1956              :       /* MS_ASYNC dtrt with the removed mapping, including a
    1957              :          subsequent overlapping remap.  */
    1958         3654 :       if (msync (hdr.buffer, extent, MS_ASYNC)
    1959         3654 :           || munmap (hdr.buffer, extent))
    1960              :         /* We're somewhat screwed at this point.  */
    1961            0 :         set_error (errno);
    1962              :     }
    1963              : 
    1964         3660 :   hdr.buffer = NULL;
    1965         3660 : }
    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       809098 : elf_out::grow (char *data, unsigned needed)
    1973              : {
    1974       809098 :   if (!data)
    1975              :     {
    1976              :       /* First allocation, check we're aligned.  */
    1977       341772 :       gcc_checking_assert (!(pos & (SECTION_ALIGN - 1)));
    1978              : #if MAPPED_WRITING
    1979       341772 :       data = hdr.buffer + (pos - offset);
    1980              : #endif
    1981              :     }
    1982              : 
    1983              : #if MAPPED_WRITING
    1984       809098 :   unsigned off = data - hdr.buffer;
    1985       809098 :   if (off + needed > extent)
    1986              :     {
    1987              :       /* We need to grow the mapping.  */
    1988          715 :       unsigned lwm = off & ~(page_size - 1);
    1989          715 :       unsigned hwm = (off + needed + page_size - 1) & ~(page_size - 1);
    1990              : 
    1991          715 :       gcc_checking_assert (hwm > extent);
    1992              : 
    1993          715 :       remove_mapping ();
    1994              : 
    1995          715 :       offset += lwm;
    1996          715 :       create_mapping (extent < hwm - lwm ? hwm - lwm : extent);
    1997              : 
    1998          715 :       data = hdr.buffer + (off - lwm);
    1999              :     }
    2000              : #else
    2001              :   data = allocator::grow (data, needed);
    2002              : #endif
    2003              : 
    2004       809098 :   return data;
    2005              : }
    2006              : 
    2007              : #if MAPPED_WRITING
    2008              : /* Shrinking is a NOP.  */
    2009              : void
    2010       341772 : elf_out::shrink (char *)
    2011              : {
    2012       341772 : }
    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      1513784 : elf_out::strtab_write (const char *s, unsigned l)
    2020              : {
    2021      1513784 :   if (strtab.pos + l > strtab.size)
    2022         1317 :     data::simple_memory.grow (strtab, strtab.pos + l, false);
    2023      1513784 :   memcpy (strtab.buffer + strtab.pos, s, l);
    2024      1513784 :   unsigned res = strtab.pos;
    2025      1513784 :   strtab.pos += l;
    2026      1513784 :   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       604534 : elf_out::strtab_write (tree decl, int inner)
    2034              : {
    2035       604534 :   tree ctx = CP_DECL_CONTEXT (decl);
    2036       604534 :   if (TYPE_P (ctx))
    2037         6489 :     ctx = TYPE_NAME (ctx);
    2038       604534 :   if (ctx != global_namespace)
    2039       308380 :     strtab_write (ctx, -1);
    2040              : 
    2041       604534 :   tree name = DECL_NAME (decl);
    2042       604534 :   if (!name)
    2043          339 :     name = DECL_ASSEMBLER_NAME_RAW (decl);
    2044       604534 :   strtab_write (IDENTIFIER_POINTER (name), IDENTIFIER_LENGTH (name));
    2045              : 
    2046       604534 :   if (inner)
    2047       435046 :     strtab_write (&"::{}"[inner+1], 2);
    2048       604534 : }
    2049              : 
    2050              : /* Map IDENTIFIER IDENT to strtab offset.  Inserts into strtab if not
    2051              :    already there.  */
    2052              : 
    2053              : unsigned
    2054       160049 : elf_out::name (tree ident)
    2055              : {
    2056       160049 :   unsigned res = 0;
    2057       160049 :   if (ident)
    2058              :     {
    2059       159997 :       bool existed;
    2060       159997 :       int *slot = &identtab.get_or_insert (ident, &existed);
    2061       159997 :       if (!existed)
    2062       284754 :         *slot = strtab_write (IDENTIFIER_POINTER (ident),
    2063       142377 :                               IDENTIFIER_LENGTH (ident) + 1);
    2064       159997 :       res = *slot;
    2065              :     }
    2066       160049 :   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        35673 : elf_out::name (const char *literal)
    2074              : {
    2075        35673 :   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       296154 : elf_out::qualified_name (tree decl, bool is_defn)
    2083              : {
    2084       296154 :   gcc_checking_assert (DECL_P (decl) && decl != global_namespace);
    2085       296154 :   unsigned result = strtab.pos;
    2086              : 
    2087       296154 :   strtab_write (decl, is_defn);
    2088       296154 :   strtab_write ("", 1);
    2089              : 
    2090       296154 :   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       341766 : elf_out::add (unsigned type, unsigned name, unsigned off, unsigned size,
    2099              :               unsigned flags)
    2100              : {
    2101       341766 :   gcc_checking_assert (!(off & (SECTION_ALIGN - 1)));
    2102       341766 :   if (sectab.pos + sizeof (section) > sectab.size)
    2103         4859 :     data::simple_memory.grow (sectab, sectab.pos + sizeof (section), false);
    2104       341766 :   section *sec = reinterpret_cast<section *> (sectab.buffer + sectab.pos);
    2105       341766 :   memset (sec, 0, sizeof (section));
    2106       341766 :   sec->type = type;
    2107       341766 :   sec->flags = flags;
    2108       341766 :   sec->name = name;
    2109       341766 :   sec->offset = off;
    2110       341766 :   sec->size = size;
    2111       341766 :   if (flags & SHF_STRINGS)
    2112         5549 :     sec->entsize = 1;
    2113              : 
    2114       341766 :   unsigned res = sectab.pos;
    2115       341766 :   sectab.pos += sizeof (section);
    2116       341766 :   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        11162 : elf_out::write (const data &buffer)
    2124              : {
    2125              : #if MAPPED_WRITING
    2126              :   /* HDR is always mapped.  */
    2127        11162 :   if (&buffer != &hdr)
    2128              :     {
    2129         5584 :       bytes_out out (this);
    2130         5584 :       grow (out, buffer.pos, true);
    2131         5584 :       if (out.buffer)
    2132         5584 :         memcpy (out.buffer, buffer.buffer, buffer.pos);
    2133         5584 :       shrink (out);
    2134         5584 :     }
    2135              :   else
    2136              :     /* We should have been aligned during the first allocation.  */
    2137         5578 :     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        11162 :   unsigned res = pos;
    2146        11162 :   pos += buffer.pos;
    2147              : 
    2148        11162 :   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         9501 :       pos += padding;
    2158              :     }
    2159        11162 :   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       336188 : elf_out::write (const bytes_out &buf)
    2167              : {
    2168       336188 :   gcc_checking_assert (buf.memory == this);
    2169              :   /* A directly mapped buffer.  */
    2170       336188 :   gcc_checking_assert (buf.buffer - hdr.buffer >= 0
    2171              :                        && buf.buffer - hdr.buffer + buf.size <= extent);
    2172       336188 :   unsigned res = pos;
    2173       336188 :   pos += buf.pos;
    2174              : 
    2175              :   /* Align up.  We're not going to advance into the next page. */
    2176       336188 :   pos += -pos & (SECTION_ALIGN - 1);
    2177              : 
    2178       336188 :   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       336188 : elf_out::add (const bytes_out &data, bool string_p, unsigned name)
    2189              : {
    2190       336188 :   unsigned off = write (data);
    2191              : 
    2192       672376 :   return add (string_p ? SHT_STRTAB : SHT_PROGBITS, name,
    2193       336188 :               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         2786 : elf_out::begin ()
    2201              : {
    2202         2786 :   if (!parent::begin ())
    2203              :     return false;
    2204              : 
    2205              :   /* Let the allocators pick a default.  */
    2206         2786 :   data::simple_memory.grow (strtab, 0, false);
    2207         2786 :   data::simple_memory.grow (sectab, 0, false);
    2208              : 
    2209              :   /* The string table starts with an empty string.  */
    2210         2786 :   name ("");
    2211              : 
    2212              :   /* Create the UNDEF section.  */
    2213         2786 :   add (SHT_NONE);
    2214              : 
    2215              : #if MAPPED_WRITING
    2216              :   /* Start a mapping.  */
    2217         2786 :   create_mapping (EXPERIMENT (page_size,
    2218              :                               (32767 + page_size) & ~(page_size - 1)));
    2219         2786 :   if (!hdr.buffer)
    2220              :     return false;
    2221              : #endif
    2222              : 
    2223              :   /* Write an empty header.  */
    2224         2786 :   grow (hdr, sizeof (header), true);
    2225         2786 :   header *h = reinterpret_cast<header *> (hdr.buffer);
    2226         2786 :   memset (h, 0, sizeof (header));
    2227         2786 :   hdr.pos = hdr.size;
    2228         2786 :   write (hdr);
    2229         2786 :   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         2904 : elf_out::end ()
    2237              : {
    2238         2904 :   if (fd >= 0)
    2239              :     {
    2240              :       /* Write the string table.  */
    2241         2792 :       unsigned strnam = name (".strtab");
    2242         2792 :       unsigned stroff = write (strtab);
    2243         2792 :       unsigned strndx = add (SHT_STRTAB, strnam, stroff, strtab.pos,
    2244              :                              SHF_STRINGS);
    2245              : 
    2246              :       /* Store escape values in section[0].  */
    2247         2792 :       if (strndx >= SHN_LORESERVE)
    2248              :         {
    2249            0 :           reinterpret_cast<section *> (sectab.buffer)->link = strndx;
    2250            0 :           strndx = SHN_XINDEX;
    2251              :         }
    2252         2792 :       unsigned shnum = sectab.pos / sizeof (section);
    2253         2792 :       if (shnum >= SHN_LORESERVE)
    2254              :         {
    2255            0 :           reinterpret_cast<section *> (sectab.buffer)->size = shnum;
    2256            0 :           shnum = SHN_XINDEX;
    2257              :         }
    2258              : 
    2259         2792 :       unsigned shoff = write (sectab);
    2260              : 
    2261              : #if MAPPED_WRITING
    2262         2792 :       if (offset)
    2263              :         {
    2264          153 :           remove_mapping ();
    2265          153 :           offset = 0;
    2266          153 :           create_mapping ((sizeof (header) + page_size - 1) & ~(page_size - 1),
    2267              :                           false);
    2268              :         }
    2269         2792 :       unsigned length = pos;
    2270              : #else
    2271              :       if (lseek (fd, 0, SEEK_SET) < 0)
    2272              :         set_error (errno);
    2273              : #endif
    2274              :       /* Write header.  */
    2275         2792 :       if (!get_error ())
    2276              :         {
    2277              :           /* Write the correct header now.  */
    2278         2792 :           header *h = reinterpret_cast<header *> (hdr.buffer);
    2279         2792 :           h->ident.magic[0] = 0x7f;
    2280         2792 :           h->ident.magic[1] = 'E';   /* Elrond */
    2281         2792 :           h->ident.magic[2] = 'L';   /* is an */
    2282         2792 :           h->ident.magic[3] = 'F';   /* elf.  */
    2283         2792 :           h->ident.klass = MY_CLASS;
    2284         2792 :           h->ident.data =  MY_ENDIAN;
    2285         2792 :           h->ident.version = EV_CURRENT;
    2286         2792 :           h->ident.osabi = OSABI_NONE;
    2287         2792 :           h->type = ET_NONE;
    2288         2792 :           h->machine = EM_NONE;
    2289         2792 :           h->version = EV_CURRENT;
    2290         2792 :           h->shoff = shoff;
    2291         2792 :           h->ehsize = sizeof (header);
    2292         2792 :           h->shentsize = sizeof (section);
    2293         2792 :           h->shnum = shnum;
    2294         2792 :           h->shstrndx = strndx;
    2295              : 
    2296         2792 :           pos = 0;
    2297         2792 :           write (hdr);
    2298              :         }
    2299              : 
    2300              : #if MAPPED_WRITING
    2301         2792 :       remove_mapping ();
    2302         2792 :       if (ftruncate (fd, length))
    2303            0 :         set_error (errno);
    2304              : #endif
    2305              :     }
    2306              : 
    2307         2904 :   data::simple_memory.shrink (sectab);
    2308         2904 :   data::simple_memory.shrink (strtab);
    2309              : 
    2310         2904 :   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      6019487 :   template<unsigned I> void set_flag_bit ()
    2417              :   {
    2418            0 :     gcc_checking_assert (I < 2 || !is_binding ());
    2419      6019487 :     discriminator |= 1u << I;
    2420      3799436 :   }
    2421      6255130 :   template<unsigned I> void clear_flag_bit ()
    2422              :   {
    2423            0 :     gcc_checking_assert (I < 2 || !is_binding ());
    2424      6255130 :     discriminator &= ~(1u << I);
    2425      6255130 :   }
    2426    595296061 :   template<unsigned I> bool get_flag_bit () const
    2427              :   {
    2428            0 :     gcc_checking_assert (I < 2 || !is_binding ());
    2429    725245362 :     return bool ((discriminator >> I) & 1);
    2430              :   }
    2431              : 
    2432              : public:
    2433    583686535 :   bool is_binding () const
    2434              :   {
    2435    140003867 :     return !get_flag_bit<DB_ZERO_BIT> ();
    2436              :   }
    2437    307686235 :   entity_kind get_entity_kind () const
    2438              :   {
    2439        39407 :     if (is_binding ())
    2440              :       return EK_BINDING;
    2441    232190410 :     return entity_kind ((discriminator >> DB_KIND_BIT) & ((1u << EK_BITS) - 1));
    2442              :   }
    2443              :   const char *entity_kind_name () const;
    2444              : 
    2445              : public:
    2446      9789203 :   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      9789203 :     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      2432188 :   bool is_pending_entity () const
    2457              :   {
    2458      3864788 :     return (get_entity_kind () == EK_SPECIALIZATION
    2459      1432600 :             || get_entity_kind () == EK_PARTIAL
    2460      3826106 :             || (get_entity_kind () == EK_DECL
    2461      1349453 :                 && 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     52084008 :   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     22844793 :     tree inner = STRIP_TEMPLATE (get_entity ());
    2478     52084008 :     return (get_flag_bit<DB_TU_LOCAL_BIT> ()
    2479     52084008 :             && (strict
    2480         3304 :                 || !VAR_OR_FUNCTION_DECL_P (inner)
    2481         2174 :                 || !NAMESPACE_SCOPE_P (inner)
    2482         2147 :                 || (DECL_LANG_SPECIFIC (inner)
    2483         2108 :                     && DECL_MODULE_PURVIEW_P (inner))));
    2484              :   }
    2485      1463742 :   bool refs_tu_local (bool strict = false) const
    2486              :   {
    2487      1463742 :     return (get_flag_bit<DB_REF_PURVIEW_BIT> ()
    2488      1463742 :             || (strict && get_flag_bit <DB_REF_GLOBAL_BIT> ()));
    2489              :   }
    2490      3530950 :   bool is_exposure (bool strict = false) const
    2491              :   {
    2492      3530950 :     return (get_flag_bit<DB_EXPOSE_PURVIEW_BIT> ()
    2493      3530950 :             || (strict && get_flag_bit <DB_EXPOSE_GLOBAL_BIT> ()));
    2494              :   }
    2495      2387723 :   bool is_ignored_exposure_context () const
    2496              :   {
    2497      2387723 :     return get_flag_bit<DB_IGNORED_EXPOSURE_BIT> ();
    2498              :   }
    2499              : 
    2500              : public:
    2501     31756813 :   bool is_import () const
    2502              :   {
    2503      9719584 :     return get_flag_bit<DB_IMPORTED_BIT> ();
    2504              :   }
    2505     19295648 :   bool is_unreached () const
    2506              :   {
    2507      1232912 :     return get_flag_bit<DB_UNREACHED_BIT> ();
    2508              :   }
    2509      2026021 :   bool is_hidden () const
    2510              :   {
    2511      2026021 :     return get_flag_bit<DB_HIDDEN_BIT> ();
    2512              :   }
    2513      1229423 :   bool is_maybe_recursive () const
    2514              :   {
    2515      1229423 :     return get_flag_bit<DB_MAYBE_RECURSIVE_BIT> ();
    2516              :   }
    2517         1128 :   bool is_entry () const
    2518              :   {
    2519         1128 :     return get_flag_bit<DB_ENTRY_BIT> ();
    2520              :   }
    2521      1499304 :   bool is_type_spec () const
    2522              :   {
    2523      1499304 :     return get_flag_bit<DB_TYPE_SPEC_BIT> ();
    2524              :   }
    2525      1499304 :   bool is_friend_spec () const
    2526              :   {
    2527      1499304 :     return get_flag_bit<DB_FRIEND_SPEC_BIT> ();
    2528              :   }
    2529              : 
    2530              : public:
    2531              :   /* We set these bit outside of depset.  */
    2532           89 :   void set_hidden_binding ()
    2533              :   {
    2534           89 :     set_flag_bit<DB_HIDDEN_BIT> ();
    2535           89 :   }
    2536           36 :   void clear_hidden_binding ()
    2537              :   {
    2538           36 :     clear_flag_bit<DB_HIDDEN_BIT> ();
    2539           36 :   }
    2540              : 
    2541              : public:
    2542     11609526 :   bool is_special () const
    2543              :   {
    2544     11609526 :     return get_flag_bit<DB_SPECIAL_BIT> ();
    2545              :   }
    2546      2220051 :   void set_special ()
    2547              :   {
    2548      2220051 :     set_flag_bit<DB_SPECIAL_BIT> ();
    2549            0 :   }
    2550              : 
    2551              : public:
    2552    193503401 :   tree get_entity () const
    2553              :   {
    2554     52084008 :     return entity;
    2555              :   }
    2556     23391775 :   tree get_name () const
    2557              :   {
    2558     23391775 :     gcc_checking_assert (is_binding ());
    2559     23391775 :     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     19628687 :     inline static hashval_t hash (const compare_type &p)
    2574              :     {
    2575     19628687 :       hashval_t h = pointer_hash<tree_node>::hash (p.first);
    2576     19628687 :       if (p.second)
    2577              :         {
    2578       225106 :           hashval_t nh = IDENTIFIER_HASH_VALUE (p.second);
    2579       225106 :           h = iterative_hash_hashval_t (h, nh);
    2580              :         }
    2581     19628687 :       return h;
    2582              :     }
    2583    103624486 :     inline static bool equal (const value_type b, const compare_type &p)
    2584              :     {
    2585    103624486 :       if (b->entity != p.first)
    2586              :         return false;
    2587              : 
    2588     14318536 :       if (p.second)
    2589        56434 :         return b->discriminator == reinterpret_cast<uintptr_t> (p.second);
    2590              :       else
    2591     14262102 :         return !b->is_binding ();
    2592              :     }
    2593              : 
    2594              :     /* (re)hasher for a binding itself.  */
    2595     78743524 :     inline static hashval_t hash (const value_type b)
    2596              :     {
    2597     78743524 :       hashval_t h = pointer_hash<tree_node>::hash (b->entity);
    2598     78743524 :       if (b->is_binding ())
    2599              :         {
    2600      7576609 :           hashval_t nh = IDENTIFIER_HASH_VALUE (b->get_name ());
    2601      7576609 :           h = iterative_hash_hashval_t (h, nh);
    2602              :         }
    2603     78743524 :       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      3156414 :     static void remove (value_type p)
    2616              :     {
    2617      3156414 :       delete (p);
    2618      3156414 :     }
    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              :     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 = nullptr;
    2649              :     };
    2650              :     vec<dep_adl_info> dep_adl_entity_list;
    2651              : 
    2652              :   public:
    2653       314393 :     hash (size_t size, hash *c = NULL)
    2654       628786 :       : parent (size), chain (c), current (NULL), section (0),
    2655       314393 :         reached_unreached (false), writing_merge_key (false),
    2656       314393 :         ignore_exposure (false)
    2657              :     {
    2658       314393 :       worklist.create (size);
    2659       314393 :       dep_adl_entity_list.create (16);
    2660       314393 :     }
    2661       314393 :     ~hash ()
    2662              :     {
    2663       314393 :       worklist.release ();
    2664       314393 :       dep_adl_entity_list.release ();
    2665       314393 :     }
    2666              : 
    2667              :   public:
    2668    143276763 :     bool is_key_order () const
    2669              :     {
    2670    143276763 :       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       916789 :     temp_override<bool> ignore_exposure_if (bool cond)
    2678              :     {
    2679       719365 :       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       314364 :     tarjan (unsigned size)
    2728       314364 :       : index (0)
    2729              :     {
    2730       314364 :       result.create (size);
    2731       314364 :       stack.create (50);
    2732       314364 :     }
    2733       314364 :     ~tarjan ()
    2734              :     {
    2735       314364 :       gcc_assert (!stack.length ());
    2736       314364 :       stack.release ();
    2737       314364 :     }
    2738              : 
    2739              :   public:
    2740              :     void connect (depset *);
    2741              :   };
    2742              : };
    2743              : 
    2744              : inline
    2745      3156414 : depset::depset (tree entity)
    2746      3156414 :   :entity (entity), discriminator (0), cluster (0), section (0)
    2747              : {
    2748      3156414 :   deps.create (0);
    2749              : }
    2750              : 
    2751              : inline
    2752      3156414 : depset::~depset ()
    2753              : {
    2754      3156414 :   deps.release ();
    2755      3156414 : }
    2756              : 
    2757              : const char *
    2758        44491 : depset::entity_kind_name () const
    2759              : {
    2760              :   /* Same order as entity_kind.  */
    2761        44491 :   static const char *const names[] =
    2762              :     {"decl", "specialization", "partial", "using",
    2763              :      "namespace", "tu-local", "redirect", "binding"};
    2764        44491 :   static_assert (ARRAY_SIZE (names) == EK_EXPLICIT_HWM + 1,
    2765              :                  "names must have an entry for every explicit entity_kind");
    2766        44491 :   entity_kind kind = get_entity_kind ();
    2767        44491 :   gcc_checking_assert (kind < ARRAY_SIZE (names));
    2768        44491 :   return names[kind];
    2769              : }
    2770              : 
    2771              : /* Create a depset for a namespace binding NS::NAME.  */
    2772              : 
    2773       157703 : depset *depset::make_binding (tree ns, tree name)
    2774              : {
    2775       157703 :   depset *binding = new depset (ns);
    2776              : 
    2777       157703 :   binding->discriminator = reinterpret_cast <uintptr_t> (name);
    2778              : 
    2779       157703 :   return binding;
    2780              : }
    2781              : 
    2782      2998711 : depset *depset::make_entity (tree entity, entity_kind ek, bool is_defn)
    2783              : {
    2784      2998711 :   depset *r = new depset (entity);
    2785              : 
    2786      2998711 :   r->discriminator = ((1 << DB_ZERO_BIT)
    2787      2998711 :                       | (ek << DB_KIND_BIT)
    2788      2998711 :                       | is_defn << DB_DEFN_BIT);
    2789              : 
    2790      2998711 :   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     50073132 :   static hashval_t hash (const value_type &k)
    2807              :   {
    2808     50073132 :     hashval_t h = IDENTIFIER_HASH_VALUE (k.id);
    2809     50073132 :     h = iterative_hash_hashval_t (DECL_UID (k.ns), h);
    2810              : 
    2811     50073132 :     return h;
    2812              :   }
    2813     14867989 :   static bool equal (const value_type &k, const value_type &l)
    2814              :   {
    2815     14867989 :     return k.ns == l.ns && k.id == l.id;
    2816              :   }
    2817       229635 :   static void mark_empty (value_type &k)
    2818              :   {
    2819       229635 :     k.ns = k.id = NULL_TREE;
    2820              :   }
    2821         6167 :   static void mark_deleted (value_type &k)
    2822              :   {
    2823         6167 :     k.ns = NULL_TREE;
    2824         6167 :     gcc_checking_assert (k.id);
    2825         6167 :   }
    2826    375204077 :   static bool is_empty (const value_type &k)
    2827              :   {
    2828    375151651 :     return k.ns == NULL_TREE && k.id == NULL_TREE;
    2829              :   }
    2830     19844816 :   static bool is_deleted (const value_type &k)
    2831              :   {
    2832     19844816 :     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 entity 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 iobj_p : 1;
    2974              :   unsigned xobj_p : 1;
    2975              :   unsigned index;
    2976              : 
    2977              :   tree ret;  /* Return type, if appropriate.  */
    2978              :   tree args; /* Arg types, if appropriate.  */
    2979              : 
    2980              :   tree constraints;  /* Constraints.  */
    2981              : 
    2982      3040089 :   merge_key ()
    2983      3040089 :     :ref_q (REF_QUAL_NONE), coro_disc (0), iobj_p (0), xobj_p (0), index (0),
    2984      3040089 :      ret (NULL_TREE), args (NULL_TREE),
    2985      3040089 :      constraints (NULL_TREE)
    2986              :   {
    2987              :   }
    2988              : };
    2989              : 
    2990              : /* Hashmap of merged duplicates.  Usually decls, but can contain
    2991              :    BINFOs.  */
    2992              : typedef hash_map<tree,uintptr_t,
    2993              :                  simple_hashmap_traits<nodel_ptr_hash<tree_node>,uintptr_t> >
    2994              : duplicate_hash_map;
    2995              : 
    2996              : /* Data needed for post-processing.  */
    2997              : struct post_process_data {
    2998              :   tree decl;
    2999              :   location_t start_locus;
    3000              :   location_t end_locus;
    3001              :   bool returns_value;
    3002              :   bool returns_null;
    3003              :   bool returns_abnormally;
    3004              :   bool infinite_loop;
    3005              : };
    3006              : 
    3007              : /* Tree stream reader.  Note that reading a stream doesn't mark the
    3008              :    read trees with TREE_VISITED.  Thus it's quite safe to have
    3009              :    multiple concurrent readers.  Which is good, because lazy
    3010              :    loading.
    3011              : 
    3012              :    It's important that trees_in/out have internal linkage so that the
    3013              :    compiler knows core_bools, lang_type_bools and lang_decl_bools have
    3014              :    only a single caller (tree_node_bools) and inlines them appropriately.  */
    3015              : namespace {
    3016              : class trees_in : public bytes_in {
    3017              :   typedef bytes_in parent;
    3018              : 
    3019              : private:
    3020              :   module_state *state;          /* Module being imported.  */
    3021              :   vec<tree> back_refs;            /* Back references.  */
    3022              :   duplicate_hash_map *duplicates;       /* Map from existings to duplicate.  */
    3023              :   vec<post_process_data> post_decls;      /* Decls to post process.  */
    3024              :   vec<tree> post_types;           /* Types to post process.  */
    3025              :   unsigned unused;              /* Inhibit any interior TREE_USED
    3026              :                                    marking.  */
    3027              : 
    3028              : public:
    3029              :   trees_in (module_state *);
    3030              :   ~trees_in ();
    3031              : 
    3032              : public:
    3033              :   int insert (tree);
    3034              :   tree back_ref (int);
    3035              : 
    3036              : private:
    3037              :   tree start (unsigned = 0);
    3038              : 
    3039              : public:
    3040              :   /* Needed for binfo writing  */
    3041              :   bool core_bools (tree, bits_in&);
    3042              : 
    3043              : private:
    3044              :   /* Stream tree_core, lang_decl_specific and lang_type_specific
    3045              :      bits.  */
    3046              :   bool core_vals (tree);
    3047              :   bool lang_type_bools (tree, bits_in&);
    3048              :   bool lang_type_vals (tree);
    3049              :   bool lang_decl_bools (tree, bits_in&);
    3050              :   bool lang_decl_vals (tree);
    3051              :   bool lang_vals (tree);
    3052              :   bool tree_node_bools (tree);
    3053              :   bool tree_node_vals (tree);
    3054              :   tree tree_value ();
    3055              :   tree decl_value ();
    3056              :   tree tpl_parm_value ();
    3057              : 
    3058              : private:
    3059              :   tree chained_decls ();  /* Follow DECL_CHAIN.  */
    3060              :   vec<tree, va_heap> *vec_chained_decls ();
    3061              :   vec<tree, va_gc> *tree_vec (); /* vec of tree.  */
    3062              :   vec<tree_pair_s, va_gc> *tree_pair_vec (); /* vec of tree_pair.  */
    3063              :   tree tree_list (bool has_purpose);
    3064              : 
    3065              : public:
    3066              :   /* Read a tree node.  */
    3067              :   tree tree_node (bool is_use = false);
    3068              : 
    3069              : private:
    3070              :   bool install_entity (tree decl);
    3071              :   tree tpl_parms (unsigned &tpl_levels);
    3072              :   bool tpl_parms_fini (tree decl, unsigned tpl_levels);
    3073              :   bool tpl_header (tree decl, unsigned *tpl_levels);
    3074              :   int fn_parms_init (tree);
    3075              :   void fn_parms_fini (int tag, tree fn, tree existing, bool has_defn);
    3076              :   unsigned add_indirect_tpl_parms (tree);
    3077              : public:
    3078              :   bool add_indirects (tree);
    3079              : 
    3080              : public:
    3081              :   /* Serialize various definitions. */
    3082              :   bool read_definition (tree decl);
    3083              : 
    3084              : private:
    3085              :   void check_abi_tags (tree existing, tree decl, tree &eattr, tree &dattr);
    3086              :   bool is_matching_decl (tree existing, tree decl, bool is_typedef);
    3087              :   static bool install_implicit_member (tree decl);
    3088              :   bool read_function_def (tree decl, tree maybe_template);
    3089              :   bool read_var_def (tree decl, tree maybe_template);
    3090              :   bool read_class_def (tree decl, tree maybe_template);
    3091              :   bool read_enum_def (tree decl, tree maybe_template);
    3092              : 
    3093              : public:
    3094              :   tree decl_container ();
    3095              :   tree key_mergeable (int tag, merge_kind, tree decl, tree inner, tree type,
    3096              :                       tree container, bool is_attached,
    3097              :                       bool is_imported_temploid_friend);
    3098              :   unsigned binfo_mergeable (tree *);
    3099              : 
    3100              : private:
    3101              :   tree key_local_type (const merge_key&, tree, tree);
    3102              :   uintptr_t *find_duplicate (tree existing);
    3103              :   void register_duplicate (tree decl, tree existing);
    3104              :   /* Mark as an already diagnosed bad duplicate.  */
    3105           54 :   void unmatched_duplicate (tree existing)
    3106              :   {
    3107          108 :     *find_duplicate (existing) |= 1;
    3108           54 :   }
    3109              : 
    3110              : public:
    3111       134953 :   bool is_duplicate (tree decl)
    3112              :   {
    3113       269906 :     return find_duplicate (decl) != NULL;
    3114              :   }
    3115       167129 :   tree maybe_duplicate (tree decl)
    3116              :   {
    3117       167129 :     if (uintptr_t *dup = find_duplicate (decl))
    3118         8915 :       return reinterpret_cast<tree> (*dup & ~uintptr_t (1));
    3119              :     return decl;
    3120              :   }
    3121              :   tree odr_duplicate (tree decl, bool has_defn);
    3122              : 
    3123              : public:
    3124              :   /* Return the decls to postprocess.  */
    3125              :   const vec<post_process_data>& post_process ()
    3126              :   {
    3127              :     return post_decls;
    3128              :   }
    3129              :   /* Return the types to postprocess.  */
    3130              :   const vec<tree>& post_process_type ()
    3131              :   {
    3132              :     return post_types;
    3133              :   }
    3134              : private:
    3135              :   /* Register DATA for postprocessing.  */
    3136       143868 :   void post_process (post_process_data data)
    3137              :   {
    3138       143868 :     post_decls.safe_push (data);
    3139              :   }
    3140              :   /* Register TYPE for postprocessing.  */
    3141           36 :   void post_process_type (tree type)
    3142              :   {
    3143           36 :     gcc_checking_assert (TYPE_P (type));
    3144           36 :     post_types.safe_push (type);
    3145           36 :   }
    3146              : 
    3147              : private:
    3148              :   void assert_definition (tree, bool installing);
    3149              : };
    3150              : } // anon namespace
    3151              : 
    3152       216105 : trees_in::trees_in (module_state *state)
    3153       216105 :   :parent (), state (state), unused (0)
    3154              : {
    3155       216105 :   duplicates = NULL;
    3156       216105 :   back_refs.create (500);
    3157       216105 :   post_decls.create (0);
    3158       216105 :   post_types.create (0);
    3159       216105 : }
    3160              : 
    3161       216105 : trees_in::~trees_in ()
    3162              : {
    3163       319730 :   delete (duplicates);
    3164       216105 :   back_refs.release ();
    3165       216105 :   post_decls.release ();
    3166       216105 :   post_types.release ();
    3167       216105 : }
    3168              : 
    3169              : /* Tree stream writer.  */
    3170              : namespace {
    3171              : class trees_out : public bytes_out {
    3172              :   typedef bytes_out parent;
    3173              : 
    3174              : private:
    3175              :   module_state *state;          /* The module we are writing.  */
    3176              :   ptr_int_hash_map tree_map;    /* Trees to references */
    3177              :   depset::hash *dep_hash;       /* Dependency table.  */
    3178              :   int ref_num;                  /* Back reference number.  */
    3179              :   unsigned section;
    3180              :   bool writing_local_entities;  /* Whether we might walk into a TU-local
    3181              :                                    entity we need to emit placeholders for.  */
    3182              :   bool walking_bit_field_unit;  /* Whether we're walking the underlying
    3183              :                                    storage for a bit field.  There's no other
    3184              :                                    great way to detect this.  */
    3185              : #if CHECKING_P
    3186              :   int importedness;             /* Checker that imports not occurring
    3187              :                                    inappropriately.  +ve imports ok,
    3188              :                                    -ve imports not ok.  */
    3189              : #endif
    3190              : 
    3191              : public:
    3192              :   trees_out (allocator *, module_state *, depset::hash &deps, unsigned sec = 0);
    3193              :   ~trees_out ();
    3194              : 
    3195              : private:
    3196              :   void mark_trees ();
    3197              :   void unmark_trees ();
    3198              : 
    3199              : public:
    3200              :   /* Hey, let's ignore the well known STL iterator idiom.  */
    3201              :   void begin ();
    3202              :   unsigned end (elf_out *sink, unsigned name, unsigned *crc_ptr);
    3203              :   void end ();
    3204              : 
    3205              : public:
    3206              :   enum tags
    3207              :   {
    3208              :     tag_backref = -1,   /* Upper bound on the backrefs.  */
    3209              :     tag_value = 0,      /* Write by value.  */
    3210              :     tag_fixed           /* Lower bound on the fixed trees.  */
    3211              :   };
    3212              : 
    3213              : public:
    3214              :   /* The walk is used for three similar purposes:
    3215              : 
    3216              :       1. The initial scan for dependencies.
    3217              :       2. Once dependencies have been found, ordering them.
    3218              :       3. Writing dependencies to file (streaming_p).
    3219              : 
    3220              :      For cases where it matters, these accessers can be used to determine
    3221              :      which state we're in.  */
    3222    117300620 :   bool is_initial_scan () const
    3223              :   {
    3224    194928973 :     return !streaming_p () && !is_key_order ();
    3225              :   }
    3226     95830282 :   bool is_key_order () const
    3227              :   {
    3228     77628353 :     return dep_hash->is_key_order ();
    3229              :   }
    3230              : 
    3231              : public:
    3232              :   int insert (tree, walk_kind = WK_normal);
    3233              : 
    3234              : private:
    3235              :   void start (tree, bool = false);
    3236              : 
    3237              : private:
    3238              :   walk_kind ref_node (tree);
    3239              : public:
    3240              :   int get_tag (tree);
    3241       623214 :   void set_importing (int i ATTRIBUTE_UNUSED)
    3242              :   {
    3243              : #if CHECKING_P
    3244       623214 :     importedness = i;
    3245              : #endif
    3246              :   }
    3247              : 
    3248              : private:
    3249              :   void core_bools (tree, bits_out&);
    3250              :   void core_vals (tree);
    3251              :   void lang_type_bools (tree, bits_out&);
    3252              :   void lang_type_vals (tree);
    3253              :   void lang_decl_bools (tree, bits_out&);
    3254              :   void lang_decl_vals (tree);
    3255              :   void lang_vals (tree);
    3256              :   void tree_node_bools (tree);
    3257              :   void tree_node_vals (tree);
    3258              : 
    3259              : private:
    3260              :   void chained_decls (tree);
    3261              :   void vec_chained_decls (tree);
    3262              :   void tree_vec (vec<tree, va_gc> *);
    3263              :   void tree_pair_vec (vec<tree_pair_s, va_gc> *);
    3264              :   void tree_list (tree, bool has_purpose);
    3265              : 
    3266              : private:
    3267              :   bool has_tu_local_dep (tree) const;
    3268              :   tree find_tu_local_decl (tree);
    3269              : 
    3270              : public:
    3271              :   /* Mark a node for by-value walking.  */
    3272              :   void mark_by_value (tree);
    3273              : 
    3274              : public:
    3275              :   void tree_node (tree);
    3276              : 
    3277              : private:
    3278              :   void install_entity (tree decl, depset *);
    3279              :   void tpl_parms (tree parms, unsigned &tpl_levels);
    3280              :   void tpl_parms_fini (tree decl, unsigned tpl_levels);
    3281              :   void fn_parms_fini (tree) {}
    3282              :   unsigned add_indirect_tpl_parms (tree);
    3283              : public:
    3284              :   void add_indirects (tree);
    3285              :   void fn_parms_init (tree);
    3286              :   void tpl_header (tree decl, unsigned *tpl_levels);
    3287              : 
    3288              : public:
    3289              :   merge_kind get_merge_kind (tree decl, depset *maybe_dep);
    3290              :   tree decl_container (tree decl);
    3291              :   void key_mergeable (int tag, merge_kind, tree decl, tree inner,
    3292              :                       tree container, depset *maybe_dep);
    3293              :   void binfo_mergeable (tree binfo);
    3294              : 
    3295              : private:
    3296              :   void key_local_type (merge_key&, tree, tree);
    3297              :   bool decl_node (tree, walk_kind ref);
    3298              :   void type_node (tree);
    3299              :   void tree_value (tree);
    3300              :   void tpl_parm_value (tree);
    3301              : 
    3302              : public:
    3303              :   void decl_value (tree, depset *);
    3304              : 
    3305              : public:
    3306              :   /* Serialize various definitions. */
    3307              :   void write_definition (tree decl, bool refs_tu_local = false);
    3308              :   void mark_declaration (tree decl, bool do_defn);
    3309              : 
    3310              : private:
    3311              :   void mark_function_def (tree decl);
    3312              :   void mark_var_def (tree decl);
    3313              :   void mark_class_def (tree decl);
    3314              :   void mark_enum_def (tree decl);
    3315              :   void mark_class_member (tree decl, bool do_defn = true);
    3316              :   void mark_binfos (tree type);
    3317              : 
    3318              : private:
    3319              :   void write_var_def (tree decl);
    3320              :   void write_function_def (tree decl);
    3321              :   void write_class_def (tree decl);
    3322              :   void write_enum_def (tree decl);
    3323              : 
    3324              : private:
    3325              :   static void assert_definition (tree);
    3326              : 
    3327              : public:
    3328              :   static void instrument ();
    3329              : 
    3330              : private:
    3331              :   /* Tree instrumentation. */
    3332              :   static unsigned tree_val_count;
    3333              :   static unsigned decl_val_count;
    3334              :   static unsigned back_ref_count;
    3335              :   static unsigned tu_local_count;
    3336              :   static unsigned null_count;
    3337              : };
    3338              : } // anon namespace
    3339              : 
    3340              : /* Instrumentation counters.  */
    3341              : unsigned trees_out::tree_val_count;
    3342              : unsigned trees_out::decl_val_count;
    3343              : unsigned trees_out::back_ref_count;
    3344              : unsigned trees_out::tu_local_count;
    3345              : unsigned trees_out::null_count;
    3346              : 
    3347       628802 : trees_out::trees_out (allocator *mem, module_state *state, depset::hash &deps,
    3348       628802 :                       unsigned section)
    3349      1257604 :   :parent (mem), state (state), tree_map (500),
    3350       628802 :    dep_hash (&deps), ref_num (0), section (section),
    3351       628802 :    writing_local_entities (false), walking_bit_field_unit (false)
    3352              : {
    3353              : #if CHECKING_P
    3354       628802 :   importedness = 0;
    3355              : #endif
    3356       628802 : }
    3357              : 
    3358       628802 : trees_out::~trees_out ()
    3359              : {
    3360       628802 : }
    3361              : 
    3362              : /********************************************************************/
    3363              : /* Location.  We're aware of the line-map concept and reproduce it
    3364              :    here.  Each imported module allocates a contiguous span of ordinary
    3365              :    maps, and of macro maps.  adhoc maps are serialized by contents,
    3366              :    not pre-allocated.   The scattered linemaps of a module are
    3367              :    coalesced when writing.  */
    3368              : 
    3369              : 
    3370              : /* I use half-open [first,second) ranges.  */
    3371              : typedef std::pair<line_map_uint_t,line_map_uint_t> range_t;
    3372              : 
    3373              : /* A range of locations.  */
    3374              : typedef std::pair<location_t,location_t> loc_range_t;
    3375              : 
    3376              : /* Spans of the line maps that are occupied by this TU.  I.e. not
    3377              :    within imports.  Only extended when in an interface unit.
    3378              :    Interval zero corresponds to the forced header linemap(s).  This
    3379              :    is a singleton object.  */
    3380              : 
    3381              : class loc_spans {
    3382              : public:
    3383              :   /* An interval of line maps.  The line maps here represent a contiguous
    3384              :      non-imported range.  */
    3385         6006 :   struct span {
    3386              :     loc_range_t ordinary;       /* Ordinary map location range. */
    3387              :     loc_range_t macro;          /* Macro map location range.  */
    3388              :     /* Add to locs to get serialized loc.  */
    3389              :     location_diff_t ordinary_delta;
    3390              :     location_diff_t macro_delta;
    3391              :   };
    3392              : 
    3393              : private:
    3394              :   vec<span> *spans;
    3395              :   bool locs_exhausted_p;
    3396              : 
    3397              : public:
    3398              :   loc_spans ()
    3399              :     /* Do not preallocate spans, as that causes
    3400              :        --enable-detailed-mem-stats problems.  */
    3401              :     : spans (nullptr), locs_exhausted_p (false)
    3402              :   {
    3403              :   }
    3404        99765 :   ~loc_spans ()
    3405              :   {
    3406        99765 :     delete spans;
    3407        99765 :   }
    3408              : 
    3409              : public:
    3410          300 :   span &operator[] (unsigned ix)
    3411              :   {
    3412          600 :     return (*spans)[ix];
    3413              :   }
    3414              :   unsigned length () const
    3415              :   {
    3416              :     return spans->length ();
    3417              :   }
    3418              : 
    3419              : public:
    3420        15710 :   bool init_p () const
    3421              :   {
    3422        15710 :     return spans != nullptr;
    3423              :   }
    3424              :   /* Initializer.  */
    3425              :   void init (const line_maps *lmaps, const line_map_ordinary *map);
    3426              : 
    3427              :   /* Slightly skewed preprocessed files can cause us to miss an
    3428              :      initialization in some places.  Fallback initializer.  */
    3429         5831 :   void maybe_init ()
    3430              :   {
    3431         5831 :     if (!init_p ())
    3432            6 :       init (line_table, nullptr);
    3433         5831 :   }
    3434              : 
    3435              : public:
    3436              :   enum {
    3437              :     SPAN_RESERVED = 0,  /* Reserved (fixed) locations.  */
    3438              :     SPAN_FIRST = 1,     /* LWM of locations to stream  */
    3439              :     SPAN_MAIN = 2       /* Main file and onwards.  */
    3440              :   };
    3441              : 
    3442              : public:
    3443       466509 :   location_t main_start () const
    3444              :   {
    3445       466509 :     return (*spans)[SPAN_MAIN].ordinary.first;
    3446              :   }
    3447              : 
    3448              : public:
    3449              :   void open (location_t);
    3450              :   void close ();
    3451              : 
    3452              : public:
    3453              :   /* Propagate imported linemaps to us, if needed.  */
    3454              :   bool maybe_propagate (module_state *import, location_t loc);
    3455              : 
    3456              : public:
    3457              :   /* Whether we can no longer represent new imported locations.  */
    3458            0 :   bool locations_exhausted_p () const
    3459              :   {
    3460            0 :     return locs_exhausted_p;
    3461              :   }
    3462            0 :   void report_location_exhaustion (location_t loc)
    3463              :   {
    3464            0 :     if (!locs_exhausted_p)
    3465              :       {
    3466              :         /* Just give the notice once.  */
    3467            0 :         locs_exhausted_p = true;
    3468            0 :         inform (loc, "unable to represent further imported source locations");
    3469              :       }
    3470              :   }
    3471              : 
    3472              : public:
    3473              :   const span *ordinary (location_t);
    3474              :   const span *macro (location_t);
    3475              : };
    3476              : 
    3477              : static loc_spans spans;
    3478              : 
    3479              : /* Information about ordinary locations we stream out.  */
    3480              : struct ord_loc_info
    3481              : {
    3482              :   const line_map_ordinary *src; // line map we're based on
    3483              :   line_map_uint_t offset;       // offset to this line
    3484              :   line_map_uint_t span;         // number of locs we span
    3485              :   line_map_uint_t remap;        // serialization
    3486              : 
    3487    308418696 :   static int compare (const void *a_, const void *b_)
    3488              :   {
    3489    308418696 :     auto *a = static_cast<const ord_loc_info *> (a_);
    3490    308418696 :     auto *b = static_cast<const ord_loc_info *> (b_);
    3491              : 
    3492    308418696 :     if (a->src != b->src)
    3493     67248036 :       return a->src < b->src ? -1 : +1;
    3494              : 
    3495              :     // Ensure no overlap
    3496    262944507 :     gcc_checking_assert (a->offset + a->span <= b->offset
    3497              :                          || b->offset + b->span <= a->offset);
    3498              : 
    3499    262944507 :     gcc_checking_assert (a->offset != b->offset);
    3500    262944507 :     return a->offset < b->offset ? -1 : +1;
    3501              :   }
    3502              : };
    3503              : struct ord_loc_traits
    3504              : {
    3505              :   typedef ord_loc_info value_type;
    3506              :   typedef value_type compare_type;
    3507              : 
    3508              :   static const bool empty_zero_p = false;
    3509              : 
    3510    132677081 :   static hashval_t hash (const value_type &v)
    3511              :   {
    3512    112761205 :     auto h = pointer_hash<const line_map_ordinary>::hash (v.src);
    3513    132677081 :     return iterative_hash_hashval_t (v.offset, h);
    3514              :   }
    3515    146140952 :   static bool equal (const value_type &v, const compare_type p)
    3516              :   {
    3517    146140952 :     return v.src == p.src && v.offset == p.offset;
    3518              :   }
    3519              : 
    3520      9415441 :   static void mark_empty (value_type &v)
    3521              :   {
    3522      9415441 :     v.src = nullptr;
    3523              :   }
    3524    423575458 :   static bool is_empty (value_type &v)
    3525              :   {
    3526    423575458 :     return !v.src;
    3527              :   }
    3528              : 
    3529              :   static bool is_deleted (value_type &) { return false; }
    3530              :   static void mark_deleted (value_type &) { gcc_unreachable (); }
    3531              : 
    3532      2288388 :   static void remove (value_type &) {}
    3533              : };
    3534              : /* Table keyed by ord_loc_info, used for noting.  */
    3535              : static  hash_table<ord_loc_traits> *ord_loc_table;
    3536              : /* Sorted vector, used for writing.  */
    3537              : static vec<ord_loc_info> *ord_loc_remap;
    3538              : 
    3539              : /* Information about macro locations we stream out.  */
    3540              : struct macro_loc_info
    3541              : {
    3542              :   const line_map_macro *src;    // original expansion
    3543              :   line_map_uint_t remap;        // serialization
    3544              : 
    3545      9392277 :   static int compare (const void *a_, const void *b_)
    3546              :   {
    3547      9392277 :     auto *a = static_cast<const macro_loc_info *> (a_);
    3548      9392277 :     auto *b = static_cast<const macro_loc_info *> (b_);
    3549              : 
    3550      9392277 :     gcc_checking_assert (MAP_START_LOCATION (a->src)
    3551              :                          != MAP_START_LOCATION (b->src));
    3552      9392277 :     if (MAP_START_LOCATION (a->src) < MAP_START_LOCATION (b->src))
    3553              :       return -1;
    3554              :     else
    3555      4527592 :       return +1;
    3556              :   }
    3557              : };
    3558              : struct macro_loc_traits
    3559              : {
    3560              :   typedef macro_loc_info value_type;
    3561              :   typedef const line_map_macro *compare_type;
    3562              : 
    3563              :   static const bool empty_zero_p = false;
    3564              : 
    3565      9971410 :   static hashval_t hash (compare_type p)
    3566              :   {
    3567      9971410 :     return pointer_hash<const line_map_macro>::hash (p);
    3568              :   }
    3569      8447551 :   static hashval_t hash (const value_type &v)
    3570              :   {
    3571      8447551 :     return hash (v.src);
    3572              :   }
    3573              :   static bool equal (const value_type &v, const compare_type p)
    3574              :   {
    3575              :     return v.src == p;
    3576              :   }
    3577              : 
    3578       705010 :   static void mark_empty (value_type &v)
    3579              :   {
    3580       705010 :     v.src = nullptr;
    3581              :   }
    3582     31845449 :   static bool is_empty (value_type &v)
    3583              :   {
    3584     31845449 :     return !v.src;
    3585              :   }
    3586              : 
    3587              :   static bool is_deleted (value_type &) { return false; }
    3588              :   static void mark_deleted (value_type &) { gcc_unreachable (); }
    3589              : 
    3590       161188 :   static void remove (value_type &) {}
    3591              : };
    3592              : /* Table keyed by line_map_macro, used for noting.  */
    3593              : static  hash_table<macro_loc_traits> *macro_loc_table;
    3594              : /* Sorted vector, used for writing.  */
    3595              : static vec<macro_loc_info> *macro_loc_remap;
    3596              : 
    3597              : /* Indirection to allow bsearching imports by ordinary location.  */
    3598              : static vec<module_state *> *ool;
    3599              : 
    3600              : /********************************************************************/
    3601              : /* Data needed by a module during the process of loading.  */
    3602              : struct GTY(()) slurping {
    3603              : 
    3604              :   /* Remap import's module numbering to our numbering.  Values are
    3605              :      shifted by 1.  Bit0 encodes if the import is direct.  */
    3606              :   vec<unsigned, va_heap, vl_embed> *
    3607              :     GTY((skip)) remap;                  /* Module owner remapping.  */
    3608              : 
    3609              :   elf_in *GTY((skip)) from;             /* The elf loader.  */
    3610              : 
    3611              :   /* This map is only for header imports themselves -- the global
    3612              :      headers bitmap hold it for the current TU.  */
    3613              :   bitmap headers;       /* Transitive set of direct imports, including
    3614              :                            self.  Used for macro visibility and
    3615              :                            priority.  */
    3616              : 
    3617              :   /* These objects point into the mmapped area, unless we're not doing
    3618              :      that, or we got frozen or closed.  In those cases they point to
    3619              :      buffers we own.  */
    3620              :   bytes_in macro_defs;  /* Macro definitions.  */
    3621              :   bytes_in macro_tbl;   /* Macro table.  */
    3622              : 
    3623              :   /* Location remapping.  first->ordinary, second->macro.  */
    3624              :   range_t GTY((skip)) loc_deltas;
    3625              : 
    3626              :   unsigned current;     /* Section currently being loaded.  */
    3627              :   unsigned remaining;   /* Number of lazy sections yet to read.  */
    3628              :   unsigned lru;         /* An LRU counter.  */
    3629              : 
    3630              :  public:
    3631              :   slurping (elf_in *);
    3632              :   ~slurping ();
    3633              : 
    3634              :  public:
    3635              :   /* Close the ELF file, if it's open.  */
    3636         5373 :   void close ()
    3637              :   {
    3638         5373 :     if (from)
    3639              :       {
    3640         2946 :         from->end ();
    3641         5892 :         delete from;
    3642         2946 :         from = NULL;
    3643              :       }
    3644         5373 :   }
    3645              : 
    3646              :  public:
    3647              :   void release_macros ();
    3648              : 
    3649              :  public:
    3650         3004 :   void alloc_remap (unsigned size)
    3651              :   {
    3652         3004 :     gcc_assert (!remap);
    3653         3004 :     vec_safe_reserve (remap, size);
    3654         6401 :     for (unsigned ix = size; ix--;)
    3655         3397 :       remap->quick_push (0);
    3656         3004 :   }
    3657      1234730 :   unsigned remap_module (unsigned owner)
    3658              :   {
    3659      1234730 :     if (owner < remap->length ())
    3660      1234730 :       return (*remap)[owner] >> 1;
    3661              :     return 0;
    3662              :   }
    3663              : 
    3664              :  public:
    3665              :   /* GC allocation.  But we must explicitly delete it.   */
    3666         3028 :   static void *operator new (size_t x)
    3667              :   {
    3668         6056 :     return ggc_alloc_atomic (x);
    3669              :   }
    3670         2946 :   static void operator delete (void *p)
    3671              :   {
    3672         2946 :     ggc_free (p);
    3673         2946 :   }
    3674              : };
    3675              : 
    3676         3028 : slurping::slurping (elf_in *from)
    3677         3028 :   : remap (NULL), from (from),
    3678         3028 :     headers (BITMAP_GGC_ALLOC ()), macro_defs (), macro_tbl (),
    3679         3028 :     loc_deltas (0, 0),
    3680         3028 :     current (~0u), remaining (0), lru (0)
    3681              : {
    3682         3028 : }
    3683              : 
    3684         2946 : slurping::~slurping ()
    3685              : {
    3686         2946 :   vec_free (remap);
    3687         2946 :   remap = NULL;
    3688         2946 :   release_macros ();
    3689         2946 :   close ();
    3690         2946 : }
    3691              : 
    3692         5373 : void slurping::release_macros ()
    3693              : {
    3694         5373 :   if (macro_defs.size)
    3695          896 :     elf_in::release (from, macro_defs);
    3696         5373 :   if (macro_tbl.size)
    3697            0 :     elf_in::release (from, macro_tbl);
    3698         5373 : }
    3699              : 
    3700              : /* Flags for extensions that end up being streamed.  */
    3701              : 
    3702              : enum streamed_extensions {
    3703              :   SE_OPENMP_SIMD = 1 << 0,
    3704              :   SE_OPENMP = 1 << 1,
    3705              :   SE_OPENACC = 1 << 2,
    3706              :   SE_BITS = 3
    3707              : };
    3708              : 
    3709              : /* Counter indices.  */
    3710              : enum module_state_counts
    3711              : {
    3712              :   MSC_sec_lwm,
    3713              :   MSC_sec_hwm,
    3714              :   MSC_pendings,
    3715              :   MSC_entities,
    3716              :   MSC_namespaces,
    3717              :   MSC_using_directives,
    3718              :   MSC_bindings,
    3719              :   MSC_macros,
    3720              :   MSC_inits,
    3721              :   MSC_HWM
    3722              : };
    3723              : 
    3724              : /********************************************************************/
    3725              : struct module_state_config;
    3726              : 
    3727              : /* Increasing levels of loadedness.  */
    3728              : enum module_loadedness {
    3729              :   ML_NONE,              /* Not loaded.  */
    3730              :   ML_CONFIG,            /* Config loaed.  */
    3731              :   ML_PREPROCESSOR,      /* Preprocessor loaded.  */
    3732              :   ML_LANGUAGE,          /* Language loaded.  */
    3733              : };
    3734              : 
    3735              : /* Increasing levels of directness (toplevel) of import.  */
    3736              : enum module_directness {
    3737              :   MD_NONE,              /* Not direct.  */
    3738              :   MD_PARTITION_DIRECT,  /* Direct import of a partition.  */
    3739              :   MD_DIRECT,            /* Direct import.  */
    3740              :   MD_PURVIEW_DIRECT,    /* Direct import in purview.  */
    3741              : };
    3742              : 
    3743              : /* State of a particular module. */
    3744              : 
    3745              : class GTY((chain_next ("%h.parent"), for_user)) module_state {
    3746              :  public:
    3747              :   /* We always import & export ourselves.  */
    3748              :   bitmap imports;       /* Transitive modules we're importing.  */
    3749              :   bitmap exports;       /* Subset of that, that we're exporting.  */
    3750              : 
    3751              :   /* For a named module interface A.B, parent is A and name is B.
    3752              :      For a partition M:P, parent is M and name is P.
    3753              :      For an implementation unit I, parent is I's interface and name is NULL.
    3754              :      Otherwise parent is NULL and name will be the flatname.  */
    3755              :   module_state *parent;
    3756              :   tree name;
    3757              : 
    3758              :   slurping *slurp;      /* Data for loading.  */
    3759              : 
    3760              :   const char *flatname; /* Flatname of module.  */
    3761              :   char *filename;       /* CMI Filename */
    3762              : 
    3763              :   /* Indices into the entity_ary.  */
    3764              :   unsigned entity_lwm;
    3765              :   unsigned entity_num;
    3766              : 
    3767              :   /* Location ranges for this module.  adhoc-locs are decomposed, so
    3768              :      don't have a range.  */
    3769              :   loc_range_t GTY((skip)) ordinary_locs;
    3770              :   loc_range_t GTY((skip)) macro_locs; // [lwm,num)
    3771              : 
    3772              :   /* LOC is first set too the importing location.  When initially
    3773              :      loaded it refers to a module loc whose parent is the importing
    3774              :      location.  */
    3775              :   location_t loc;       /* Location referring to module itself.  */
    3776              :   unsigned crc;         /* CRC we saw reading it in. */
    3777              : 
    3778              :   unsigned mod;         /* Module owner number.  */
    3779              :   unsigned remap;       /* Remapping during writing.  */
    3780              : 
    3781              :   unsigned short subst; /* Mangle subst if !0.  */
    3782              : 
    3783              :   /* How loaded this module is.  */
    3784              :   enum module_loadedness loadedness : 2;
    3785              : 
    3786              :   bool module_p : 1;    /* /The/ module of this TU.  */
    3787              :   bool header_p : 1;    /* Is a header unit.  */
    3788              :   bool interface_p : 1; /* An interface.  */
    3789              :   bool partition_p : 1; /* A partition.  */
    3790              : 
    3791              :   /* How directly this module is imported.  */
    3792              :   enum module_directness directness : 2;
    3793              : 
    3794              :   bool exported_p : 1;  /* directness != MD_NONE && exported.  */
    3795              :   bool cmi_noted_p : 1; /* We've told the user about the CMI, don't
    3796              :                            do it again  */
    3797              :   bool active_init_p : 1; /* This module's global initializer needs
    3798              :                            calling.  */
    3799              :   bool inform_cmi_p : 1; /* Inform of a read/write.  */
    3800              :   bool visited_p : 1;    /* A walk-once flag. */
    3801              :   /* Record extensions emitted or permitted.  */
    3802              :   unsigned extensions : SE_BITS;
    3803              :   /* 14 bits used, 2 bits remain  */
    3804              : 
    3805              :  public:
    3806              :   module_state (tree name, module_state *, bool);
    3807              :   ~module_state ();
    3808              : 
    3809              :  public:
    3810         3008 :   void release ()
    3811              :   {
    3812         3008 :     imports = exports = NULL;
    3813         3008 :     slurped ();
    3814         2946 :   }
    3815         5435 :   void slurped ()
    3816              :   {
    3817         5435 :     delete slurp;
    3818         5435 :     slurp = NULL;
    3819         5435 :   }
    3820      1326741 :   elf_in *from () const
    3821              :   {
    3822      1326741 :     return slurp->from;
    3823              :   }
    3824              : 
    3825              :  public:
    3826              :   /* Kind of this module.  */
    3827       144644 :   bool is_module () const
    3828              :   {
    3829       144644 :     return module_p;
    3830              :   }
    3831      2530332 :   bool is_header () const
    3832              :   {
    3833      2530332 :     return header_p;
    3834              :   }
    3835          621 :   bool is_interface () const
    3836              :   {
    3837          621 :     return interface_p;
    3838              :   }
    3839       341830 :   bool is_partition () const
    3840              :   {
    3841       341830 :     return partition_p;
    3842              :   }
    3843              : 
    3844              :   /* How this module is used in the current TU.  */
    3845         3233 :   bool is_exported () const
    3846              :   {
    3847         3233 :     return exported_p;
    3848              :   }
    3849        20973 :   bool is_direct () const
    3850              :   {
    3851        20973 :     return directness >= MD_DIRECT;
    3852              :   }
    3853          281 :   bool is_purview_direct () const
    3854              :   {
    3855          281 :     return directness == MD_PURVIEW_DIRECT;
    3856              :   }
    3857          470 :   bool is_partition_direct () const
    3858              :   {
    3859          470 :     return directness == MD_PARTITION_DIRECT;
    3860              :   }
    3861              : 
    3862              :  public:
    3863              :   /* Is this a real module?  */
    3864        16175 :   bool has_location () const
    3865              :   {
    3866        16175 :     return loc != UNKNOWN_LOCATION;
    3867              :   }
    3868              : 
    3869              :  public:
    3870              :   bool check_circular_import (location_t loc);
    3871              : 
    3872              :  public:
    3873              :   void mangle (bool include_partition);
    3874              : 
    3875              :  public:
    3876              :   void set_import (module_state const *, bool is_export);
    3877              :   void announce (const char *) const;
    3878              : 
    3879              :  public:
    3880              :   /* Read and write module.  */
    3881              :   bool write_begin (elf_out *to, cpp_reader *,
    3882              :                     module_state_config &, unsigned &crc);
    3883              :   void write_end (elf_out *to, cpp_reader *,
    3884              :                   module_state_config &, unsigned &crc);
    3885              :   bool read_initial (cpp_reader *);
    3886              :   bool read_preprocessor (bool);
    3887              :   bool read_language (bool);
    3888              : 
    3889              :  public:
    3890              :   /* Read a section.  */
    3891              :   bool load_section (unsigned snum, binding_slot *mslot);
    3892              :   /* Lazily read a section.  */
    3893              :   bool lazy_load (unsigned index, binding_slot *mslot);
    3894              : 
    3895              :  public:
    3896              :   /* Juggle a limited number of file numbers.  */
    3897              :   static void freeze_an_elf ();
    3898              :   bool maybe_defrost ();
    3899              : 
    3900              :  public:
    3901              :   void maybe_completed_reading ();
    3902              :   bool check_read (bool outermost, bool ok);
    3903              : 
    3904              :  private:
    3905              :   /* The README, for human consumption.  */
    3906              :   void write_readme (elf_out *to, cpp_reader *, const char *dialect);
    3907              :   void write_env (elf_out *to);
    3908              : 
    3909              :  private:
    3910              :   /* Import tables. */
    3911              :   void write_imports (bytes_out &cfg, bool direct);
    3912              :   unsigned read_imports (bytes_in &cfg, cpp_reader *, line_maps *maps);
    3913              : 
    3914              :  private:
    3915              :   void write_imports (elf_out *to, unsigned *crc_ptr);
    3916              :   bool read_imports (cpp_reader *, line_maps *);
    3917              : 
    3918              :  private:
    3919              :   void write_partitions (elf_out *to, unsigned, unsigned *crc_ptr);
    3920              :   bool read_partitions (unsigned);
    3921              : 
    3922              :  private:
    3923              :   void write_config (elf_out *to, struct module_state_config &, unsigned crc);
    3924              :   bool read_config (struct module_state_config &, bool = true);
    3925              :   static void write_counts (elf_out *to, unsigned [MSC_HWM], unsigned *crc_ptr);
    3926              :   bool read_counts (unsigned *);
    3927              : 
    3928              :  public:
    3929              :   void note_cmi_name ();
    3930              : 
    3931              :  private:
    3932              :   static unsigned write_bindings (elf_out *to, vec<depset *> depsets,
    3933              :                                   unsigned *crc_ptr);
    3934              :   bool read_bindings (unsigned count, unsigned lwm, unsigned hwm);
    3935              : 
    3936              :   static void write_namespace (bytes_out &sec, depset *ns_dep);
    3937              :   tree read_namespace (bytes_in &sec);
    3938              : 
    3939              :   void write_namespaces (elf_out *to, vec<depset *> spaces,
    3940              :                          unsigned, unsigned *crc_ptr);
    3941              :   bool read_namespaces (unsigned);
    3942              : 
    3943              :   unsigned write_using_directives (elf_out *to, depset::hash &,
    3944              :                                    vec<depset *> spaces, unsigned *crc_ptr);
    3945              :   bool read_using_directives (unsigned);
    3946              : 
    3947              :   void intercluster_seed (trees_out &sec, unsigned index, depset *dep);
    3948              :   unsigned write_cluster (elf_out *to, depset *depsets[], unsigned size,
    3949              :                           depset::hash &, unsigned *counts, unsigned *crc_ptr);
    3950              :   bool read_cluster (unsigned snum);
    3951              :   bool open_slurp (cpp_reader *);
    3952              : 
    3953              :  private:
    3954              :   unsigned write_inits (elf_out *to, depset::hash &, unsigned *crc_ptr);
    3955              :   bool read_inits (unsigned count);
    3956              : 
    3957              :  private:
    3958              :   unsigned write_pendings (elf_out *to, vec<depset *> depsets,
    3959              :                            depset::hash &, unsigned *crc_ptr);
    3960              :   bool read_pendings (unsigned count);
    3961              : 
    3962              :  private:
    3963              :   void write_entities (elf_out *to, vec<depset *> depsets,
    3964              :                        unsigned count, unsigned *crc_ptr);
    3965              :   bool read_entities (unsigned count, unsigned lwm, unsigned hwm);
    3966              : 
    3967              :  private:
    3968              :   void write_init_maps ();
    3969              :   range_t write_prepare_maps (module_state_config *, bool);
    3970              :   bool read_prepare_maps (const module_state_config *);
    3971              : 
    3972              :   void write_ordinary_maps (elf_out *to, range_t &,
    3973              :                             bool, unsigned *crc_ptr);
    3974              :   bool read_ordinary_maps (line_map_uint_t, unsigned);
    3975              :   void write_macro_maps (elf_out *to, range_t &, unsigned *crc_ptr);
    3976              :   bool read_macro_maps (line_map_uint_t);
    3977              : 
    3978              :   void write_diagnostic_classification (elf_out *, diagnostics::context *,
    3979              :                                         unsigned *);
    3980              :   bool read_diagnostic_classification (diagnostics::context *);
    3981              : 
    3982              :  private:
    3983              :   void write_define (bytes_out &, const cpp_macro *);
    3984              :   cpp_macro *read_define (bytes_in &, cpp_reader *) const;
    3985              :   vec<cpp_hashnode *> *prepare_macros (cpp_reader *);
    3986              :   unsigned write_macros (elf_out *to, vec<cpp_hashnode *> *, unsigned *crc_ptr);
    3987              :   bool read_macros ();
    3988              :   void install_macros ();
    3989              : 
    3990              :  public:
    3991              :   void import_macros ();
    3992              : 
    3993              :  public:
    3994              :   static void undef_macro (cpp_reader *, location_t, cpp_hashnode *);
    3995              :   static cpp_macro *deferred_macro (cpp_reader *, location_t, cpp_hashnode *);
    3996              : 
    3997              :  public:
    3998              :   static bool note_location (location_t);
    3999              :   static void write_location (bytes_out &, location_t);
    4000              :   location_t read_location (bytes_in &) const;
    4001              : 
    4002              :  public:
    4003              :   void set_flatname ();
    4004        52918 :   const char *get_flatname () const
    4005              :   {
    4006        52918 :     return flatname;
    4007              :   }
    4008              :   location_t imported_from () const;
    4009              : 
    4010              :  public:
    4011              :   void set_filename (const Cody::Packet &);
    4012              :   bool do_import (cpp_reader *, bool outermost);
    4013              :   bool check_importable (cpp_reader *);
    4014              : };
    4015              : 
    4016              : /* Hash module state by name.  This cannot be a member of
    4017              :    module_state, because of GTY restrictions.  We never delete from
    4018              :    the hash table, but ggc_ptr_hash doesn't support that
    4019              :    simplification.  */
    4020              : 
    4021              : struct module_state_hash : ggc_ptr_hash<module_state> {
    4022              :   typedef std::pair<tree,uintptr_t> compare_type; /* {name,parent} */
    4023              : 
    4024              :   static inline hashval_t hash (const value_type m);
    4025              :   static inline hashval_t hash (const compare_type &n);
    4026              :   static inline bool equal (const value_type existing,
    4027              :                             const compare_type &candidate);
    4028              : };
    4029              : 
    4030        11616 : module_state::module_state (tree name, module_state *parent, bool partition)
    4031        11616 :   : imports (BITMAP_GGC_ALLOC ()), exports (BITMAP_GGC_ALLOC ()),
    4032        11616 :     parent (parent), name (name), slurp (NULL),
    4033        11616 :     flatname (NULL), filename (NULL),
    4034        11616 :     entity_lwm (~0u >> 1), entity_num (0),
    4035        11616 :     ordinary_locs (0, 0), macro_locs (0, 0),
    4036        11616 :     loc (UNKNOWN_LOCATION),
    4037        11616 :     crc (0), mod (MODULE_UNKNOWN), remap (0), subst (0)
    4038              : {
    4039        11616 :   loadedness = ML_NONE;
    4040              : 
    4041        11616 :   module_p = header_p = interface_p = partition_p = false;
    4042              : 
    4043        11616 :   directness = MD_NONE;
    4044        11616 :   exported_p = false;
    4045              : 
    4046        11616 :   cmi_noted_p = false;
    4047        11616 :   active_init_p = false;
    4048              : 
    4049        11616 :   partition_p = partition;
    4050              : 
    4051        11616 :   inform_cmi_p = false;
    4052        11616 :   visited_p = false;
    4053              : 
    4054        11616 :   extensions = 0;
    4055        11616 :   if (name && TREE_CODE (name) == STRING_CST)
    4056              :     {
    4057         1922 :       header_p = true;
    4058              : 
    4059         1922 :       const char *string = TREE_STRING_POINTER (name);
    4060         1922 :       gcc_checking_assert (string[0] == '.'
    4061              :                            ? IS_DIR_SEPARATOR (string[1])
    4062              :                            : IS_ABSOLUTE_PATH (string));
    4063              :     }
    4064              : 
    4065        11616 :   gcc_checking_assert (!(parent && header_p));
    4066        11616 : }
    4067              : 
    4068           62 : module_state::~module_state ()
    4069              : {
    4070           62 :   release ();
    4071           62 : }
    4072              : 
    4073              : /* Hash module state.  */
    4074              : static hashval_t
    4075        17170 : module_name_hash (const_tree name)
    4076              : {
    4077        17170 :   if (TREE_CODE (name) == STRING_CST)
    4078         3550 :     return htab_hash_string (TREE_STRING_POINTER (name));
    4079              :   else
    4080        13620 :     return IDENTIFIER_HASH_VALUE (name);
    4081              : }
    4082              : 
    4083              : hashval_t
    4084         4825 : module_state_hash::hash (const value_type m)
    4085              : {
    4086         4825 :   hashval_t ph = pointer_hash<void>::hash
    4087         4825 :     (reinterpret_cast<void *> (reinterpret_cast<uintptr_t> (m->parent)
    4088         4825 :                                | m->is_partition ()));
    4089         4825 :   hashval_t nh = module_name_hash (m->name);
    4090         4825 :   return iterative_hash_hashval_t (ph, nh);
    4091              : }
    4092              : 
    4093              : /* Hash a name.  */
    4094              : hashval_t
    4095        12345 : module_state_hash::hash (const compare_type &c)
    4096              : {
    4097        12345 :   hashval_t ph = pointer_hash<void>::hash (reinterpret_cast<void *> (c.second));
    4098        12345 :   hashval_t nh = module_name_hash (c.first);
    4099              : 
    4100        12345 :   return iterative_hash_hashval_t (ph, nh);
    4101              : }
    4102              : 
    4103              : bool
    4104         8461 : module_state_hash::equal (const value_type existing,
    4105              :                           const compare_type &candidate)
    4106              : {
    4107         8461 :   uintptr_t ep = (reinterpret_cast<uintptr_t> (existing->parent)
    4108         8461 :                   | existing->is_partition ());
    4109         8461 :   if (ep != candidate.second)
    4110              :     return false;
    4111              : 
    4112              :   /* Identifier comparison is by pointer.  If the string_csts happen
    4113              :      to be the same object, then they're equal too.  */
    4114         7003 :   if (existing->name == candidate.first)
    4115              :     return true;
    4116              : 
    4117              :   /* If neither are string csts, they can't be equal.  */
    4118         1484 :   if (TREE_CODE (candidate.first) != STRING_CST
    4119          499 :       || TREE_CODE (existing->name) != STRING_CST)
    4120              :     return false;
    4121              : 
    4122              :   /* String equality.  */
    4123          425 :   if (TREE_STRING_LENGTH (existing->name)
    4124          425 :       == TREE_STRING_LENGTH (candidate.first)
    4125          425 :       && !memcmp (TREE_STRING_POINTER (existing->name),
    4126          422 :                   TREE_STRING_POINTER (candidate.first),
    4127          422 :                   TREE_STRING_LENGTH (existing->name)))
    4128          139 :     return true;
    4129              : 
    4130              :   return false;
    4131              : }
    4132              : 
    4133              : /********************************************************************/
    4134              : /* Global state */
    4135              : 
    4136              : /* Mapper name.  */
    4137              : static const char *module_mapper_name;
    4138              : 
    4139              : /* Deferred import queue (FIFO).  */
    4140              : static vec<module_state *, va_heap, vl_embed> *pending_imports;
    4141              : 
    4142              : /* CMI repository path and workspace.  */
    4143              : static char *cmi_repo;
    4144              : static size_t cmi_repo_length;
    4145              : static char *cmi_path;
    4146              : static size_t cmi_path_alloc;
    4147              : 
    4148              : /* Count of available and loaded clusters.  */
    4149              : static unsigned available_clusters;
    4150              : static unsigned loaded_clusters;
    4151              : 
    4152              : /* What the current TU is.  */
    4153              : unsigned module_kind;
    4154              : 
    4155              : /* Global trees.  */
    4156              : static const std::pair<tree *, unsigned> global_tree_arys[] =
    4157              :   {
    4158              :     std::pair<tree *, unsigned> (sizetype_tab, stk_type_kind_last),
    4159              :     std::pair<tree *, unsigned> (integer_types, itk_none),
    4160              :     std::pair<tree *, unsigned> (global_trees, TI_MODULE_HWM),
    4161              :     std::pair<tree *, unsigned> (c_global_trees, CTI_MODULE_HWM),
    4162              :     std::pair<tree *, unsigned> (cp_global_trees, CPTI_MODULE_HWM),
    4163              :     std::pair<tree *, unsigned> (NULL, 0)
    4164              :   };
    4165              : static GTY(()) vec<tree, va_gc> *fixed_trees;
    4166              : static unsigned global_crc;
    4167              : 
    4168              : /* Lazy loading can open many files concurrently, there are
    4169              :    per-process limits on that.  We pay attention to the process limit,
    4170              :    and attempt to increase it when we run out.  Otherwise we use an
    4171              :    LRU scheme to figure out who to flush.  Note that if the import
    4172              :    graph /depth/ exceeds lazy_limit, we'll exceed the limit.  */
    4173              : static unsigned lazy_lru;  /* LRU counter.  */
    4174              : static unsigned lazy_open; /* Number of open modules */
    4175              : static unsigned lazy_limit; /* Current limit of open modules.  */
    4176              : static unsigned lazy_hard_limit; /* Hard limit on open modules.  */
    4177              : /* Account for source, assembler and dump files & directory searches.
    4178              :    We don't keep the source file's open, so we don't have to account
    4179              :    for #include depth.  I think dump files are opened and closed per
    4180              :    pass, but ICBW.  */
    4181              : #define LAZY_HEADROOM 15 /* File descriptor headroom.  */
    4182              : 
    4183              : /* Vector of module state.  Indexed by OWNER.  Index 0 is reserved for the
    4184              :    current TU; imports start at 1.  */
    4185              : static GTY(()) vec<module_state *, va_gc> *modules;
    4186              : 
    4187              : /* Get the module state for the current TU's module.  */
    4188              : 
    4189              : static module_state *
    4190       304515 : this_module() {
    4191       304515 :   return (*modules)[0];
    4192              : }
    4193              : 
    4194              : /* Hash of module state, findable by {name, parent}. */
    4195              : static GTY(()) hash_table<module_state_hash> *modules_hash;
    4196              : 
    4197              : /* Map of imported entities.  We map DECL_UID to index of entity
    4198              :    vector.  */
    4199              : typedef hash_map<unsigned/*UID*/, unsigned/*index*/,
    4200              :                  simple_hashmap_traits<int_hash<unsigned,0>, unsigned>
    4201              :                  > entity_map_t;
    4202              : static entity_map_t *entity_map;
    4203              : /* Doesn't need GTYing, because any tree referenced here is also
    4204              :    findable by, symbol table, specialization table, return type of
    4205              :    reachable function.  */
    4206              : static vec<binding_slot, va_heap, vl_embed> *entity_ary;
    4207              : 
    4208              : /* Members entities of imported classes that are defined in this TU.
    4209              :    These are where the entity's context is not from the current TU.
    4210              :    We need to emit the definition (but not the enclosing class).
    4211              : 
    4212              :    We could find these by walking ALL the imported classes that we
    4213              :    could provide a member definition.  But that's expensive,
    4214              :    especially when you consider lazy implicit member declarations,
    4215              :    which could be ANY imported class.  */
    4216              : static GTY(()) vec<tree, va_gc> *class_members;
    4217              : 
    4218              : /* The same problem exists for class template partial
    4219              :    specializations.  Now that we have constraints, the invariant of
    4220              :    expecting them in the instantiation table no longer holds.  One of
    4221              :    the constrained partial specializations will be there, but the
    4222              :    others not so much.  It's not even an unconstrained partial
    4223              :    specialization in the table :(  so any partial template declaration
    4224              :    is added to this list too.  */
    4225              : static GTY(()) vec<tree, va_gc> *partial_specializations;
    4226              : 
    4227              : /********************************************************************/
    4228              : 
    4229              : /* Our module mapper (created lazily).  */
    4230              : module_client *mapper;
    4231              : 
    4232              : static module_client *make_mapper (location_t loc, class mkdeps *deps);
    4233        33720 : inline module_client *get_mapper (location_t loc, class mkdeps *deps)
    4234              : {
    4235        33720 :   auto *res = mapper;
    4236          313 :   if (!res)
    4237         4929 :     res = make_mapper (loc, deps);
    4238        33720 :   return res;
    4239              : }
    4240              : 
    4241              : /********************************************************************/
    4242              : static tree
    4243       446633 : get_clone_target (tree decl)
    4244              : {
    4245       446633 :   tree target;
    4246              : 
    4247       446633 :   if (TREE_CODE (decl) == TEMPLATE_DECL)
    4248              :     {
    4249        57008 :       tree res_orig = DECL_CLONED_FUNCTION (DECL_TEMPLATE_RESULT (decl));
    4250              : 
    4251        57008 :       target = DECL_TI_TEMPLATE (res_orig);
    4252              :     }
    4253              :   else
    4254       389625 :     target = DECL_CLONED_FUNCTION (decl);
    4255              : 
    4256       446633 :   gcc_checking_assert (DECL_MAYBE_IN_CHARGE_CDTOR_P (target));
    4257              : 
    4258       446633 :   return target;
    4259              : }
    4260              : 
    4261              : /* Like FOR_EACH_CLONE, but will walk cloned templates.  */
    4262              : #define FOR_EVERY_CLONE(CLONE, FN)                      \
    4263              :   if (!DECL_MAYBE_IN_CHARGE_CDTOR_P (FN));              \
    4264              :   else                                                  \
    4265              :     for (CLONE = DECL_CHAIN (FN);                       \
    4266              :          CLONE && DECL_CLONED_FUNCTION_P (CLONE);       \
    4267              :          CLONE = DECL_CHAIN (CLONE))
    4268              : 
    4269              : /* It'd be nice if USE_TEMPLATE was a field of template_info
    4270              :    (a) it'd solve the enum case dealt with below,
    4271              :    (b) both class templates and decl templates would store this in the
    4272              :    same place
    4273              :    (c) this function wouldn't need the by-ref arg, which is annoying.  */
    4274              : 
    4275              : static tree
    4276    136504497 : node_template_info (tree decl, int &use)
    4277              : {
    4278    136504497 :   tree ti = NULL_TREE;
    4279    136504497 :   int use_tpl = -1;
    4280    136504497 :   if (DECL_IMPLICIT_TYPEDEF_P (decl))
    4281              :     {
    4282     24622388 :       tree type = TREE_TYPE (decl);
    4283              : 
    4284     24622388 :       ti = TYPE_TEMPLATE_INFO (type);
    4285     24622388 :       if (ti)
    4286              :         {
    4287      5711807 :           if (TYPE_LANG_SPECIFIC (type))
    4288      5700347 :             use_tpl = CLASSTYPE_USE_TEMPLATE (type);
    4289              :           else
    4290              :             {
    4291              :               /* An enum, where we don't explicitly encode use_tpl.
    4292              :                  If the containing context (a type or a function), is
    4293              :                  an ({im,ex}plicit) instantiation, then this is too.
    4294              :                  If it's a partial or explicit specialization, then
    4295              :                  this is not!.  */
    4296        11460 :               tree ctx = CP_DECL_CONTEXT (decl);
    4297        11460 :               if (TYPE_P (ctx))
    4298        11243 :                 ctx = TYPE_NAME (ctx);
    4299        11460 :               node_template_info (ctx, use);
    4300        11460 :               use_tpl = use != 2 ? use : 0;
    4301              :             }
    4302              :         }
    4303              :     }
    4304    111882109 :   else if (DECL_LANG_SPECIFIC (decl)
    4305    111882109 :            && (VAR_P (decl)
    4306              :                || TREE_CODE (decl) == TYPE_DECL
    4307              :                || TREE_CODE (decl) == FUNCTION_DECL
    4308              :                || TREE_CODE (decl) == FIELD_DECL
    4309              :                || TREE_CODE (decl) == CONCEPT_DECL
    4310              :                || TREE_CODE (decl) == TEMPLATE_DECL))
    4311              :     {
    4312     95931197 :       use_tpl = DECL_USE_TEMPLATE (decl);
    4313     95931197 :       ti = DECL_TEMPLATE_INFO (decl);
    4314              :     }
    4315              : 
    4316    136504497 :   use = use_tpl;
    4317    136504497 :   return ti;
    4318              : }
    4319              : 
    4320              : /* Find the index in entity_ary for an imported DECL.  It should
    4321              :    always be there, but bugs can cause it to be missing, and that can
    4322              :    crash the crash reporting -- let's not do that!  When streaming
    4323              :    out we place entities from this module there too -- with negated
    4324              :    indices.  */
    4325              : 
    4326              : static unsigned
    4327      1742607 : import_entity_index (tree decl, bool null_ok = false)
    4328              : {
    4329      1742607 :   if (unsigned *slot = entity_map->get (DECL_UID (decl)))
    4330      1742562 :     return *slot;
    4331              : 
    4332           45 :   gcc_checking_assert (null_ok);
    4333              :   return ~(~0u >> 1);
    4334              : }
    4335              : 
    4336              : /* Find the module for an imported entity at INDEX in the entity ary.
    4337              :    There must be one.  */
    4338              : 
    4339              : static module_state *
    4340       137071 : import_entity_module (unsigned index)
    4341              : {
    4342       137071 :   if (index > ~(~0u >> 1))
    4343              :     /* This is an index for an exported entity.  */
    4344           60 :     return this_module ();
    4345              : 
    4346              :   /* Do not include the current TU (not an off-by-one error).  */
    4347       137011 :   unsigned pos = 1;
    4348       137011 :   unsigned len = modules->length () - pos;
    4349       313032 :   while (len)
    4350              :     {
    4351       176021 :       unsigned half = len / 2;
    4352       176021 :       module_state *probe = (*modules)[pos + half];
    4353       176021 :       if (index < probe->entity_lwm)
    4354              :         len = half;
    4355       137476 :       else if (index < probe->entity_lwm + probe->entity_num)
    4356              :         return probe;
    4357              :       else
    4358              :         {
    4359          465 :           pos += half + 1;
    4360          465 :           len = len - (half + 1);
    4361              :         }
    4362              :     }
    4363            0 :   gcc_unreachable ();
    4364              : }
    4365              : 
    4366              : 
    4367              : /********************************************************************/
    4368              : /* A dumping machinery.  */
    4369              : 
    4370              : class dumper {
    4371              : public:
    4372              :   enum {
    4373              :     LOCATION = TDF_LINENO,  /* -lineno:Source location streaming.  */
    4374              :     DEPEND = TDF_GRAPH, /* -graph:Dependency graph construction.  */
    4375              :     CLUSTER = TDF_BLOCKS,   /* -blocks:Clusters.  */
    4376              :     TREE = TDF_UID,     /* -uid:Tree streaming.  */
    4377              :     MERGE = TDF_ALIAS,  /* -alias:Mergeable Entities.  */
    4378              :     ELF = TDF_ASMNAME,  /* -asmname:Elf data.  */
    4379              :     MACRO = TDF_VOPS    /* -vops:Macros.  */
    4380              :   };
    4381              : 
    4382              : private:
    4383              :   struct impl {
    4384              :     typedef vec<module_state *, va_heap, vl_embed> stack_t;
    4385              : 
    4386              :     FILE *stream;       /* Dump stream.  */
    4387              :     unsigned indent;    /* Local indentation.  */
    4388              :     bool bol;           /* Beginning of line.  */
    4389              :     stack_t stack;      /* Trailing array of module_state.  */
    4390              : 
    4391              :     bool nested_name (tree);  /* Dump a name following DECL_CONTEXT.  */
    4392              :   };
    4393              : 
    4394              : public:
    4395              :   /* The dumper.  */
    4396              :   impl *dumps;
    4397              :   dump_flags_t flags;
    4398              : 
    4399              : public:
    4400              :   /* Push/pop module state dumping.  */
    4401              :   unsigned push (module_state *);
    4402              :   void pop (unsigned);
    4403              : 
    4404              : public:
    4405              :   /* Change local indentation.  */
    4406    433763748 :   void indent ()
    4407              :   {
    4408          309 :     if (dumps)
    4409       725915 :       dumps->indent++;
    4410              :   }
    4411    433763748 :   void outdent ()
    4412              :   {
    4413    433763748 :     if (dumps)
    4414              :       {
    4415       725915 :         gcc_checking_assert (dumps->indent);
    4416       725915 :         dumps->indent--;
    4417              :       }
    4418    433763748 :   }
    4419              : 
    4420              : public:
    4421              :   /* Is dump enabled?.  */
    4422    264268032 :   bool operator () (int mask = 0)
    4423              :   {
    4424      5174489 :     if (!dumps || !dumps->stream)
    4425              :       return false;
    4426       506165 :     if (mask && !(mask & flags))
    4427         5388 :       return false;
    4428              :     return true;
    4429              :   }
    4430              :   /* Dump some information.  */
    4431              :   bool operator () (const char *, ...);
    4432              : };
    4433              : 
    4434              : /* The dumper.  */
    4435              : static dumper dump = {0, dump_flags_t (0)};
    4436              : 
    4437              : /* Push to dumping M.  Return previous indentation level.  */
    4438              : 
    4439              : unsigned
    4440       134893 : dumper::push (module_state *m)
    4441              : {
    4442       134893 :   FILE *stream = NULL;
    4443       134893 :   if (!dumps || !dumps->stack.length ())
    4444              :     {
    4445       133620 :       stream = dump_begin (module_dump_id, &flags);
    4446       133620 :       if (!stream)
    4447              :         return 0;
    4448              :     }
    4449              : 
    4450         6953 :   if (!dumps || !dumps->stack.space (1))
    4451              :     {
    4452              :       /* Create or extend the dump implementor.  */
    4453         1239 :       unsigned current = dumps ? dumps->stack.length () : 0;
    4454          656 :       unsigned count = current ? current * 2 : EXPERIMENT (1, 20);
    4455         1239 :       size_t alloc = (offsetof (impl, stack)
    4456         1239 :                       + impl::stack_t::embedded_size (count));
    4457         1239 :       dumps = XRESIZEVAR (impl, dumps, alloc);
    4458         1239 :       dumps->stack.embedded_init (count, current);
    4459              :     }
    4460         6953 :   if (stream)
    4461         5680 :     dumps->stream = stream;
    4462              : 
    4463         6953 :   unsigned n = dumps->indent;
    4464         6953 :   dumps->indent = 0;
    4465         6953 :   dumps->bol = true;
    4466         6953 :   dumps->stack.quick_push (m);
    4467         6953 :   if (m)
    4468              :     {
    4469         2039 :       module_state *from = NULL;
    4470              : 
    4471         2039 :       if (dumps->stack.length () > 1)
    4472          654 :         from = dumps->stack[dumps->stack.length () - 2];
    4473              :       else
    4474         1385 :         dump ("");
    4475         3683 :       dump (from ? "Starting module %M (from %M)"
    4476              :             : "Starting module %M", m, from);
    4477              :     }
    4478              : 
    4479              :   return n;
    4480              : }
    4481              : 
    4482              : /* Pop from dumping.  Restore indentation to N.  */
    4483              : 
    4484       134850 : void dumper::pop (unsigned n)
    4485              : {
    4486       134850 :   if (!dumps)
    4487              :     return;
    4488              : 
    4489        13906 :   gcc_checking_assert (dump () && !dumps->indent);
    4490         6953 :   if (module_state *m = dumps->stack[dumps->stack.length () - 1])
    4491              :     {
    4492         2039 :       module_state *from = (dumps->stack.length () > 1
    4493         2039 :                             ? dumps->stack[dumps->stack.length () - 2] : NULL);
    4494         2298 :       dump (from ? "Finishing module %M (returning to %M)"
    4495              :             : "Finishing module %M", m, from);
    4496              :     }
    4497         6953 :   dumps->stack.pop ();
    4498         6953 :   dumps->indent = n;
    4499         6953 :   if (!dumps->stack.length ())
    4500              :     {
    4501         5680 :       dump_end (module_dump_id, dumps->stream);
    4502         5680 :       dumps->stream = NULL;
    4503              :     }
    4504              : }
    4505              : 
    4506              : /* Dump a nested name for arbitrary tree T.  Sometimes it won't have a
    4507              :    name.  */
    4508              : 
    4509              : bool
    4510       524580 : dumper::impl::nested_name (tree t)
    4511              : {
    4512       524580 :   tree ti = NULL_TREE;
    4513       524580 :   int origin = -1;
    4514       524580 :   tree name = NULL_TREE;
    4515              : 
    4516       524580 :   if (t && TREE_CODE (t) == TU_LOCAL_ENTITY)
    4517            0 :     t = TU_LOCAL_ENTITY_NAME (t);
    4518              : 
    4519       524550 :   if (t && TREE_CODE (t) == TREE_BINFO)
    4520          384 :     t = BINFO_TYPE (t);
    4521              : 
    4522       524580 :   if (t && TYPE_P (t))
    4523       255622 :     t = TYPE_NAME (t);
    4524              : 
    4525       524538 :   if (t && DECL_P (t))
    4526              :     {
    4527       441002 :       if (t == global_namespace || DECL_TEMPLATE_PARM_P (t))
    4528              :         ;
    4529       409085 :       else if (tree ctx = DECL_CONTEXT (t))
    4530       318918 :         if (TREE_CODE (ctx) == TRANSLATION_UNIT_DECL
    4531       318918 :             || nested_name (ctx))
    4532       318918 :           fputs ("::", stream);
    4533              : 
    4534       441002 :       int use_tpl;
    4535       441002 :       ti = node_template_info (t, use_tpl);
    4536       138837 :       if (ti && TREE_CODE (TI_TEMPLATE (ti)) == TEMPLATE_DECL
    4537       579794 :           && (DECL_TEMPLATE_RESULT (TI_TEMPLATE (ti)) == t))
    4538              :         t = TI_TEMPLATE (ti);
    4539       441002 :       tree not_tmpl = t;
    4540       441002 :       if (TREE_CODE (t) == TEMPLATE_DECL)
    4541              :         {
    4542        23727 :           fputs ("template ", stream);
    4543        23727 :           not_tmpl = DECL_TEMPLATE_RESULT (t);
    4544              :         }
    4545              : 
    4546        23727 :       if (not_tmpl
    4547       440998 :           && DECL_P (not_tmpl)
    4548       440998 :           && DECL_LANG_SPECIFIC (not_tmpl)
    4549       265125 :           && DECL_MODULE_IMPORT_P (not_tmpl))
    4550              :         {
    4551              :           /* We need to be careful here, so as to not explode on
    4552              :              inconsistent data -- we're probably debugging, because
    4553              :              Something Is Wrong.  */
    4554        24183 :           unsigned index = import_entity_index (t, true);
    4555        24183 :           if (!(index & ~(~0u >> 1)))
    4556        23583 :             origin = import_entity_module (index)->mod;
    4557          600 :           else if (index > ~(~0u >> 1))
    4558              :             /* An imported partition member that we're emitting.  */
    4559              :             origin = 0;
    4560              :           else
    4561           45 :             origin = -2;
    4562              :         }
    4563              : 
    4564       444232 :       name = DECL_NAME (t) ? DECL_NAME (t)
    4565         4309 :         : HAS_DECL_ASSEMBLER_NAME_P (t) ? DECL_ASSEMBLER_NAME_RAW (t)
    4566              :         : NULL_TREE;
    4567              :     }
    4568              :   else
    4569              :     name = t;
    4570              : 
    4571       441002 :   if (name)
    4572       489312 :     switch (TREE_CODE (name))
    4573              :       {
    4574        13636 :       default:
    4575        13636 :         fputs ("#unnamed#", stream);
    4576        13636 :         break;
    4577              : 
    4578       451260 :       case IDENTIFIER_NODE:
    4579       451260 :         fwrite (IDENTIFIER_POINTER (name), 1, IDENTIFIER_LENGTH (name), stream);
    4580       451260 :         break;
    4581              : 
    4582        24324 :       case INTEGER_CST:
    4583        24324 :         print_hex (wi::to_wide (name), stream);
    4584        24324 :         break;
    4585              : 
    4586           92 :       case STRING_CST:
    4587              :         /* If TREE_TYPE is NULL, this is a raw string.  */
    4588          184 :         fwrite (TREE_STRING_POINTER (name), 1,
    4589           92 :                 TREE_STRING_LENGTH (name) - (TREE_TYPE (name) != NULL_TREE),
    4590              :                 stream);
    4591           92 :         break;
    4592              :       }
    4593              :   else
    4594        35268 :     fputs ("#null#", stream);
    4595              : 
    4596       524580 :   if (t && TREE_CODE (t) == FUNCTION_DECL && DECL_COROUTINE_P (t))
    4597           48 :     if (tree ramp = DECL_RAMP_FN (t))
    4598              :       {
    4599           27 :         if (DECL_ACTOR_FN (ramp) == t)
    4600           12 :           fputs (".actor", stream);
    4601           15 :         else if (DECL_DESTROY_FN (ramp) == t)
    4602           15 :           fputs (".destroy", stream);
    4603              :         else
    4604            0 :           gcc_unreachable ();
    4605              :       }
    4606              : 
    4607       524580 :   if (origin >= 0)
    4608              :     {
    4609        24138 :       const module_state *module = (*modules)[origin];
    4610        48276 :       fprintf (stream, "@%s:%d", !module ? "" : !module->name ? "(unnamed)"
    4611        24138 :                : module->get_flatname (), origin);
    4612              :     }
    4613       500442 :   else if (origin == -2)
    4614           45 :     fprintf (stream, "@???");
    4615              : 
    4616       524580 :   if (ti)
    4617              :     {
    4618       138837 :       tree args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (ti));
    4619       138837 :       fputs ("<", stream);
    4620       138837 :       if (args)
    4621       349852 :         for (int ix = 0; ix != TREE_VEC_LENGTH (args); ix++)
    4622              :           {
    4623       211015 :             if (ix)
    4624        72178 :               fputs (",", stream);
    4625       211015 :             nested_name (TREE_VEC_ELT (args, ix));
    4626              :           }
    4627       138837 :       fputs (">", stream);
    4628              :     }
    4629              : 
    4630       524580 :   return true;
    4631              : }
    4632              : 
    4633              : /* Formatted dumping.  FORMAT begins with '+' do not emit a trailing
    4634              :    new line.  (Normally it is appended.)
    4635              :    Escapes:
    4636              :       %C - tree_code
    4637              :       %I - identifier
    4638              :       %K - location_t or line_map_uint_t
    4639              :       %M - module_state
    4640              :       %N - name -- DECL_NAME
    4641              :       %P - context:name pair
    4642              :       %R - unsigned:unsigned ratio
    4643              :       %S - symbol -- DECL_ASSEMBLER_NAME
    4644              :       %U - long unsigned
    4645              :       %V - version
    4646              :       --- the following are printf-like, but without its flexibility
    4647              :       %d - decimal int
    4648              :       %p - pointer
    4649              :       %s - string
    4650              :       %u - unsigned int
    4651              :       %x - hex int
    4652              : 
    4653              :   We do not implement the printf modifiers.  */
    4654              : 
    4655              : bool
    4656       446457 : dumper::operator () (const char *format, ...)
    4657              : {
    4658       446457 :   if (!(*this) ())
    4659              :     return false;
    4660              : 
    4661       382718 :   bool no_nl = format[0] == '+';
    4662       382718 :   format += no_nl;
    4663              : 
    4664       382718 :   if (dumps->bol)
    4665              :     {
    4666              :       /* Module import indent.  */
    4667       197324 :       if (unsigned depth = dumps->stack.length () - 1)
    4668              :         {
    4669        22534 :           const char *prefix = ">>>>";
    4670        45050 :           fprintf (dumps->stream, (depth <= strlen (prefix)
    4671        22516 :                                    ? &prefix[strlen (prefix) - depth]
    4672              :                                    : ">.%d.>"), depth);
    4673              :         }
    4674              : 
    4675              :       /* Local indent.  */
    4676       197324 :       if (unsigned indent = dumps->indent)
    4677              :         {
    4678       106178 :           const char *prefix = "      ";
    4679       207340 :           fprintf (dumps->stream, (indent <= strlen (prefix)
    4680       101162 :                                    ? &prefix[strlen (prefix) - indent]
    4681              :                                    : "  .%d.  "), indent);
    4682              :         }
    4683       197324 :       dumps->bol = false;
    4684              :     }
    4685              : 
    4686       382718 :   va_list args;
    4687       382718 :   va_start (args, format);
    4688      1119839 :   while (const char *esc = strchr (format, '%'))
    4689              :     {
    4690       737121 :       fwrite (format, 1, (size_t)(esc - format), dumps->stream);
    4691       737121 :       format = ++esc;
    4692       737121 :       switch (*format++)
    4693              :         {
    4694            0 :         default:
    4695            0 :           gcc_unreachable ();
    4696              : 
    4697          583 :         case '%':
    4698          583 :           fputc ('%', dumps->stream);
    4699          583 :           break;
    4700              : 
    4701       112426 :         case 'C': /* Code */
    4702       112426 :           {
    4703       112426 :             tree_code code = (tree_code)va_arg (args, unsigned);
    4704       112426 :             fputs (get_tree_code_name (code), dumps->stream);
    4705              :           }
    4706       112426 :           break;
    4707              : 
    4708           81 :         case 'I': /* Identifier.  */
    4709           81 :           {
    4710           81 :             tree t = va_arg (args, tree);
    4711           81 :             dumps->nested_name (t);
    4712              :           }
    4713           81 :           break;
    4714              : 
    4715         4647 :         case 'K': /* location_t, either 32- or 64-bit.  */
    4716         4647 :           {
    4717         4647 :             unsigned long long u = va_arg (args, location_t);
    4718         4647 :             fprintf (dumps->stream, "%llu", u);
    4719              :           }
    4720         4647 :           break;
    4721              : 
    4722         7734 :         case 'M': /* Module. */
    4723         7734 :           {
    4724         7734 :             const char *str = "(none)";
    4725         7734 :             if (module_state *m = va_arg (args, module_state *))
    4726              :               {
    4727         7734 :                 if (!m->has_location ())
    4728              :                   str = "(detached)";
    4729              :                 else
    4730         7734 :                   str = m->get_flatname ();
    4731              :               }
    4732         7734 :             fputs (str, dumps->stream);
    4733              :           }
    4734         7734 :           break;
    4735              : 
    4736       125685 :         case 'N': /* Name.  */
    4737       125685 :           {
    4738       125685 :             tree t = va_arg (args, tree);
    4739       251859 :             while (t && TREE_CODE (t) == OVERLOAD)
    4740          489 :               t = OVL_FUNCTION (t);
    4741       125685 :             fputc ('\'', dumps->stream);
    4742       125685 :             dumps->nested_name (t);
    4743       125685 :             fputc ('\'', dumps->stream);
    4744              :           }
    4745       125685 :           break;
    4746              : 
    4747         7355 :         case 'P': /* Pair.  */
    4748         7355 :           {
    4749         7355 :             tree ctx = va_arg (args, tree);
    4750         7355 :             tree name = va_arg (args, tree);
    4751         7355 :             fputc ('\'', dumps->stream);
    4752         7355 :             dumps->nested_name (ctx);
    4753         7355 :             if (ctx && ctx != global_namespace)
    4754          998 :               fputs ("::", dumps->stream);
    4755         7355 :             dumps->nested_name (name);
    4756         7355 :             fputc ('\'', dumps->stream);
    4757              :           }
    4758         7355 :           break;
    4759              : 
    4760          900 :         case 'R': /* Ratio */
    4761          900 :           {
    4762          900 :             unsigned a = va_arg (args, unsigned);
    4763          900 :             unsigned b = va_arg (args, unsigned);
    4764          900 :             fprintf (dumps->stream, "%.1f", (float) a / (b + !b));
    4765              :           }
    4766          900 :           break;
    4767              : 
    4768        34704 :         case 'S': /* Symbol name */
    4769        34704 :           {
    4770        34704 :             tree t = va_arg (args, tree);
    4771        34704 :             if (t && TYPE_P (t))
    4772        12631 :               t = TYPE_NAME (t);
    4773        32924 :             if (t && HAS_DECL_ASSEMBLER_NAME_P (t)
    4774        31750 :                 && DECL_ASSEMBLER_NAME_SET_P (t))
    4775              :               {
    4776          172 :                 fputc ('(', dumps->stream);
    4777          172 :                 fputs (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (t)),
    4778          172 :                        dumps->stream);
    4779          172 :                 fputc (')', dumps->stream);
    4780              :               }
    4781              :           }
    4782              :           break;
    4783              : 
    4784            0 :         case 'U': /* long unsigned.  */
    4785            0 :           {
    4786            0 :             unsigned long u = va_arg (args, unsigned long);
    4787            0 :             fprintf (dumps->stream, "%lu", u);
    4788              :           }
    4789            0 :           break;
    4790              : 
    4791          832 :         case 'V': /* Version.  */
    4792          832 :           {
    4793          832 :             unsigned v = va_arg (args, unsigned);
    4794          832 :             verstr_t string;
    4795              : 
    4796          832 :             version2string (v, string);
    4797          832 :             fputs (string, dumps->stream);
    4798              :           }
    4799          832 :           break;
    4800              : 
    4801            0 :         case 'c': /* Character.  */
    4802            0 :           {
    4803            0 :             int c = va_arg (args, int);
    4804            0 :             fputc (c, dumps->stream);
    4805              :           }
    4806            0 :           break;
    4807              : 
    4808        63158 :         case 'd': /* Decimal Int.  */
    4809        63158 :           {
    4810        63158 :             int d = va_arg (args, int);
    4811        63158 :             fprintf (dumps->stream, "%d", d);
    4812              :           }
    4813        63158 :           break;
    4814              : 
    4815            0 :         case 'p': /* Pointer. */
    4816            0 :           {
    4817            0 :             void *p = va_arg (args, void *);
    4818            0 :             fprintf (dumps->stream, "%p", p);
    4819              :           }
    4820            0 :           break;
    4821              : 
    4822       127063 :         case 's': /* String. */
    4823       127063 :           {
    4824       127063 :             const char *s = va_arg (args, char *);
    4825       127063 :             gcc_checking_assert (s);
    4826       127063 :             fputs (s, dumps->stream);
    4827              :           }
    4828       127063 :           break;
    4829              : 
    4830       249277 :         case 'u': /* Unsigned.  */
    4831       249277 :           {
    4832       249277 :             unsigned u = va_arg (args, unsigned);
    4833       249277 :             fprintf (dumps->stream, "%u", u);
    4834              :           }
    4835       249277 :           break;
    4836              : 
    4837         2676 :         case 'x': /* Hex. */
    4838         2676 :           {
    4839         2676 :             unsigned x = va_arg (args, unsigned);
    4840         2676 :             fprintf (dumps->stream, "%x", x);
    4841              :           }
    4842         2676 :           break;
    4843              :         }
    4844              :     }
    4845       382718 :   fputs (format, dumps->stream);
    4846       382718 :   va_end (args);
    4847       382718 :   if (!no_nl)
    4848              :     {
    4849       197324 :       dumps->bol = true;
    4850       197324 :       fputc ('\n', dumps->stream);
    4851              :     }
    4852              :   return true;
    4853              : }
    4854              : 
    4855              : struct note_def_cache_hasher : ggc_cache_ptr_hash<tree_node>
    4856              : {
    4857       319307 :   static int keep_cache_entry (tree t)
    4858              :   {
    4859       319307 :     if (!CHECKING_P)
    4860              :       /* GTY is unfortunately not clever enough to conditionalize
    4861              :          this.  */
    4862              :       gcc_unreachable ();
    4863              : 
    4864       319307 :     if (ggc_marked_p (t))
    4865              :       return -1;
    4866              : 
    4867            0 :     unsigned n = dump.push (NULL);
    4868              :     /* This might or might not be an error.  We should note its
    4869              :        dropping whichever.  */
    4870            0 :     dump () && dump ("Dropping %N from note_defs table", t);
    4871            0 :     dump.pop (n);
    4872              : 
    4873            0 :     return 0;
    4874              :   }
    4875              : };
    4876              : 
    4877              : /* We should stream each definition at most once.
    4878              :    This needs to be a cache because there are cases where a definition
    4879              :    ends up being not retained, and we need to drop those so we don't
    4880              :    get confused if memory is reallocated.  */
    4881              : typedef hash_table<note_def_cache_hasher> note_defs_table_t;
    4882              : static GTY((cache)) note_defs_table_t *note_defs;
    4883              : 
    4884              : void
    4885       347062 : trees_in::assert_definition (tree decl ATTRIBUTE_UNUSED,
    4886              :                              bool installing ATTRIBUTE_UNUSED)
    4887              : {
    4888              : #if CHECKING_P
    4889       347062 :   tree *slot = note_defs->find_slot (decl, installing ? INSERT : NO_INSERT);
    4890       347062 :   tree not_tmpl = STRIP_TEMPLATE (decl);
    4891       347062 :   if (installing)
    4892              :     {
    4893              :       /* We must be inserting for the first time.  */
    4894       212109 :       gcc_assert (!*slot);
    4895       212109 :       *slot = decl;
    4896              :     }
    4897              :   else
    4898              :     /* If this is not the mergeable entity, it should not be in the
    4899              :        table.  If it is a non-global-module mergeable entity, it
    4900              :        should be in the table.  Global module entities could have been
    4901              :        defined textually in the current TU and so might or might not
    4902              :        be present.  */
    4903       134953 :     gcc_assert (!is_duplicate (decl)
    4904              :                 ? !slot
    4905              :                 : (slot
    4906              :                    || !DECL_LANG_SPECIFIC (not_tmpl)
    4907              :                    || !DECL_MODULE_PURVIEW_P (not_tmpl)
    4908              :                    || (!DECL_MODULE_IMPORT_P (not_tmpl)
    4909              :                        && header_module_p ())));
    4910              : 
    4911       347062 :   if (not_tmpl != decl)
    4912       205222 :     gcc_assert (!note_defs->find_slot (not_tmpl, NO_INSERT));
    4913              : #endif
    4914       347062 : }
    4915              : 
    4916              : void
    4917       464743 : trees_out::assert_definition (tree decl ATTRIBUTE_UNUSED)
    4918              : {
    4919              : #if CHECKING_P
    4920       464743 :   tree *slot = note_defs->find_slot (decl, INSERT);
    4921       464743 :   gcc_assert (!*slot);
    4922       464743 :   *slot = decl;
    4923       464743 :   if (TREE_CODE (decl) == TEMPLATE_DECL)
    4924       261250 :     gcc_assert (!note_defs->find_slot (DECL_TEMPLATE_RESULT (decl), NO_INSERT));
    4925              : #endif
    4926       464743 : }
    4927              : 
    4928              : /********************************************************************/
    4929              : static bool
    4930        12687 : noisy_p ()
    4931              : {
    4932            0 :   if (quiet_flag)
    4933              :     return false;
    4934              : 
    4935            0 :   pp_needs_newline (global_dc->get_reference_printer ()) = true;
    4936            0 :   diagnostic_set_last_function (global_dc,
    4937              :                                 (diagnostics::diagnostic_info *) nullptr);
    4938              : 
    4939            0 :   return true;
    4940              : }
    4941              : 
    4942              : /* Set the cmi repo.  Strip trailing '/', '.' becomes NULL.  */
    4943              : 
    4944              : static void
    4945       103000 : set_cmi_repo (const char *r)
    4946              : {
    4947       103000 :   XDELETEVEC (cmi_repo);
    4948       103000 :   XDELETEVEC (cmi_path);
    4949       103000 :   cmi_path_alloc = 0;
    4950              : 
    4951       103000 :   cmi_repo = NULL;
    4952       103000 :   cmi_repo_length = 0;
    4953              : 
    4954       103000 :   if (!r || !r[0])
    4955              :     return;
    4956              : 
    4957         4926 :   size_t len = strlen (r);
    4958         4926 :   cmi_repo = XNEWVEC (char, len + 1);
    4959         4926 :   memcpy (cmi_repo, r, len + 1);
    4960              : 
    4961         4926 :   if (len > 1 && IS_DIR_SEPARATOR (cmi_repo[len-1]))
    4962         4926 :     len--;
    4963         4926 :   if (len == 1 && cmi_repo[0] == '.')
    4964           27 :     len--;
    4965         4926 :   cmi_repo[len] = 0;
    4966         4926 :   cmi_repo_length = len;
    4967              : }
    4968              : 
    4969              : /* TO is a repo-relative name.  Provide one that we may use from where
    4970              :    we are.  */
    4971              : 
    4972              : static const char *
    4973         6017 : maybe_add_cmi_prefix (const char *to, size_t *len_p = NULL)
    4974              : {
    4975         6017 :   size_t len = len_p || cmi_repo_length ? strlen (to) : 0;
    4976              : 
    4977         6017 :   if (cmi_repo_length && !IS_ABSOLUTE_PATH (to))
    4978              :     {
    4979         5990 :       if (cmi_path_alloc < cmi_repo_length + len + 2)
    4980              :         {
    4981         4804 :           XDELETEVEC (cmi_path);
    4982         4804 :           cmi_path_alloc = cmi_repo_length + len * 2 + 2;
    4983         4804 :           cmi_path = XNEWVEC (char, cmi_path_alloc);
    4984              : 
    4985         4804 :           memcpy (cmi_path, cmi_repo, cmi_repo_length);
    4986         4804 :           cmi_path[cmi_repo_length] = DIR_SEPARATOR;
    4987              :         }
    4988              : 
    4989         5990 :       memcpy (&cmi_path[cmi_repo_length + 1], to, len + 1);
    4990         5990 :       len += cmi_repo_length + 1;
    4991         5990 :       to = cmi_path;
    4992              :     }
    4993              : 
    4994         6017 :   if (len_p)
    4995         2904 :     *len_p = len;
    4996              : 
    4997         6017 :   return to;
    4998              : }
    4999              : 
    5000              : /* Try and create the directories of PATH.  */
    5001              : 
    5002              : static void
    5003           52 : create_dirs (char *path)
    5004              : {
    5005           52 :   char *base = path;
    5006              :   /* Skip past initial slashes of absolute path.  */
    5007           52 :   while (IS_DIR_SEPARATOR (*base))
    5008            0 :     base++;
    5009              : 
    5010              :   /* Try and create the missing directories.  */
    5011         3462 :   for (; *base; base++)
    5012         3410 :     if (IS_DIR_SEPARATOR (*base))
    5013              :       {
    5014          342 :         char sep = *base;
    5015          342 :         *base = 0;
    5016          342 :         int failed = mkdir (path, S_IRWXU | S_IRWXG | S_IRWXO);
    5017          356 :         dump () && dump ("Mkdir ('%s') errno:=%u", path, failed ? errno : 0);
    5018          342 :         *base = sep;
    5019          342 :         if (failed
    5020              :             /* Maybe racing with another creator (of a *different*
    5021              :                module).  */
    5022           56 :             && errno != EEXIST)
    5023              :           break;
    5024              :       }
    5025           52 : }
    5026              : 
    5027              : /* Given a CLASSTYPE_DECL_LIST VALUE get the template friend decl,
    5028              :    if that's what this is.  */
    5029              : 
    5030              : static tree
    5031        98471 : friend_from_decl_list (tree frnd)
    5032              : {
    5033        98471 :   tree res = frnd;
    5034              : 
    5035        98471 :   if (TREE_CODE (frnd) != TEMPLATE_DECL)
    5036              :     {
    5037        60085 :       tree tmpl = NULL_TREE;
    5038        60085 :       if (TYPE_P (frnd))
    5039              :         {
    5040         9831 :           res = TYPE_NAME (frnd);
    5041         9672 :           if (CLASS_TYPE_P (frnd)
    5042        19503 :               && CLASSTYPE_TEMPLATE_INFO (frnd))
    5043         9663 :             tmpl = CLASSTYPE_TI_TEMPLATE (frnd);
    5044              :         }
    5045        50254 :       else if (DECL_TEMPLATE_INFO (frnd))
    5046              :         {
    5047        50254 :           tmpl = DECL_TI_TEMPLATE (frnd);
    5048        50254 :           if (TREE_CODE (tmpl) != TEMPLATE_DECL)
    5049              :             tmpl = NULL_TREE;
    5050              :         }
    5051              : 
    5052        68819 :       if (tmpl && DECL_TEMPLATE_RESULT (tmpl) == res)
    5053              :         res = tmpl;
    5054              :     }
    5055              : 
    5056        98471 :   return res;
    5057              : }
    5058              : 
    5059              : static tree
    5060        29771 : find_enum_member (tree ctx, tree name)
    5061              : {
    5062        29771 :   for (tree values = TYPE_VALUES (ctx);
    5063       490236 :        values; values = TREE_CHAIN (values))
    5064       481556 :     if (DECL_NAME (TREE_VALUE (values)) == name)
    5065              :       return TREE_VALUE (values);
    5066              : 
    5067              :   return NULL_TREE;
    5068              : }
    5069              : 
    5070              : /********************************************************************/
    5071              : /* Instrumentation gathered writing bytes.  */
    5072              : 
    5073              : void
    5074          300 : bytes_out::instrument ()
    5075              : {
    5076          300 :   dump ("Wrote %u bytes in %u blocks", lengths[3], spans[3]);
    5077          300 :   dump ("Wrote %u bits in %u bytes", lengths[0] + lengths[1], lengths[2]);
    5078          900 :   for (unsigned ix = 0; ix < 2; ix++)
    5079          900 :     dump ("  %u %s spans of %R bits", spans[ix],
    5080              :           ix ? "one" : "zero", lengths[ix], spans[ix]);
    5081          300 :   dump ("  %u blocks with %R bits padding", spans[2],
    5082          300 :         lengths[2] * 8 - (lengths[0] + lengths[1]), spans[2]);
    5083          300 : }
    5084              : 
    5085              : /* Instrumentation gathered writing trees.  */
    5086              : void
    5087         2757 : trees_out::instrument ()
    5088              : {
    5089         2757 :   if (dump (""))
    5090              :     {
    5091          300 :       bytes_out::instrument ();
    5092          300 :       dump ("Wrote:");
    5093          300 :       dump ("  %u decl trees", decl_val_count);
    5094          300 :       dump ("  %u other trees", tree_val_count);
    5095          300 :       dump ("  %u back references", back_ref_count);
    5096          300 :       dump ("  %u TU-local entities", tu_local_count);
    5097          300 :       dump ("  %u null trees", null_count);
    5098              :     }
    5099         2757 : }
    5100              : 
    5101              : /* Setup and teardown for a tree walk.  */
    5102              : 
    5103              : void
    5104      2746743 : trees_out::begin ()
    5105              : {
    5106      2746743 :   gcc_assert (!streaming_p () || !tree_map.elements ());
    5107              : 
    5108      2746743 :   mark_trees ();
    5109      2746743 :   if (streaming_p ())
    5110       314409 :     parent::begin ();
    5111      2746743 : }
    5112              : 
    5113              : unsigned
    5114       314409 : trees_out::end (elf_out *sink, unsigned name, unsigned *crc_ptr)
    5115              : {
    5116       314409 :   gcc_checking_assert (streaming_p ());
    5117              : 
    5118       314409 :   unmark_trees ();
    5119       314409 :   return parent::end (sink, name, crc_ptr);
    5120              : }
    5121              : 
    5122              : void
    5123      2432334 : trees_out::end ()
    5124              : {
    5125      2432334 :   gcc_assert (!streaming_p ());
    5126              : 
    5127      2432334 :   unmark_trees ();
    5128              :   /* Do not parent::end -- we weren't streaming.  */
    5129      2432334 : }
    5130              : 
    5131              : void
    5132      2746743 : trees_out::mark_trees ()
    5133              : {
    5134      2746743 :   if (size_t size = tree_map.elements ())
    5135              :     {
    5136              :       /* This isn't our first rodeo, destroy and recreate the
    5137              :          tree_map.  I'm a bad bad man.  Use the previous size as a
    5138              :          guess for the next one (so not all bad).  */
    5139      2133648 :       tree_map.~ptr_int_hash_map ();
    5140      2133648 :       new (&tree_map) ptr_int_hash_map (size);
    5141              :     }
    5142              : 
    5143              :   /* Install the fixed trees, with +ve references.  */
    5144      2746743 :   unsigned limit = fixed_trees->length ();
    5145    529219375 :   for (unsigned ix = 0; ix != limit; ix++)
    5146              :     {
    5147    526472632 :       tree val = (*fixed_trees)[ix];
    5148    526472632 :       bool existed = tree_map.put (val, ix + tag_fixed);
    5149    526472632 :       gcc_checking_assert (!TREE_VISITED (val) && !existed);
    5150    526472632 :       TREE_VISITED (val) = true;
    5151              :     }
    5152              : 
    5153      2746743 :   ref_num = 0;
    5154      2746743 : }
    5155              : 
    5156              : /* Unmark the trees we encountered  */
    5157              : 
    5158              : void
    5159      2746743 : trees_out::unmark_trees ()
    5160              : {
    5161      2746743 :   ptr_int_hash_map::iterator end (tree_map.end ());
    5162    628958835 :   for (ptr_int_hash_map::iterator iter (tree_map.begin ()); iter != end; ++iter)
    5163              :     {
    5164    626212092 :       tree node = reinterpret_cast<tree> ((*iter).first);
    5165    626212092 :       int ref = (*iter).second;
    5166              :       /* We should have visited the node, and converted its mergeable
    5167              :          reference to a regular reference.  */
    5168    626212092 :       gcc_checking_assert (TREE_VISITED (node)
    5169              :                            && (ref <= tag_backref || ref >= tag_fixed));
    5170    626212092 :       TREE_VISITED (node) = false;
    5171              :     }
    5172      2746743 : }
    5173              : 
    5174              : /* Mark DECL for by-value walking.  We do this by inserting it into
    5175              :    the tree map with a reference of zero.  May be called multiple
    5176              :    times on the same node.  */
    5177              : 
    5178              : void
    5179      4255187 : trees_out::mark_by_value (tree decl)
    5180              : {
    5181      4255187 :   gcc_checking_assert (DECL_P (decl)
    5182              :                        /* Enum consts are INTEGER_CSTS.  */
    5183              :                        || TREE_CODE (decl) == INTEGER_CST
    5184              :                        || TREE_CODE (decl) == TREE_BINFO);
    5185              : 
    5186      4255187 :   if (TREE_VISITED (decl))
    5187              :     /* Must already be forced or fixed.  */
    5188         4044 :     gcc_checking_assert (*tree_map.get (decl) >= tag_value);
    5189              :   else
    5190              :     {
    5191      4251143 :       bool existed = tree_map.put (decl, tag_value);
    5192      4251143 :       gcc_checking_assert (!existed);
    5193      4251143 :       TREE_VISITED (decl) = true;
    5194              :     }
    5195      4255187 : }
    5196              : 
    5197              : int
    5198    119559905 : trees_out::get_tag (tree t)
    5199              : {
    5200    119559905 :   gcc_checking_assert (TREE_VISITED (t));
    5201    119559905 :   return *tree_map.get (t);
    5202              : }
    5203              : 
    5204              : /* Insert T into the map, return its tag number.    */
    5205              : 
    5206              : int
    5207     99739460 : trees_out::insert (tree t, walk_kind walk)
    5208              : {
    5209     99739460 :   gcc_checking_assert (walk != WK_normal || !TREE_VISITED (t));
    5210     99739460 :   int tag = --ref_num;
    5211     99739460 :   bool existed;
    5212     99739460 :   int &slot = tree_map.get_or_insert (t, &existed);
    5213     99739460 :   gcc_checking_assert (TREE_VISITED (t) == existed
    5214              :                        && (!existed
    5215              :                            || (walk == WK_value && slot == tag_value)));
    5216     99739460 :   TREE_VISITED (t) = true;
    5217     99739460 :   slot = tag;
    5218              : 
    5219     99739460 :   return tag;
    5220              : }
    5221              : 
    5222              : /* Insert T into the backreference array.  Return its back reference
    5223              :    number.  */
    5224              : 
    5225              : int
    5226     21660241 : trees_in::insert (tree t)
    5227              : {
    5228     21660241 :   gcc_checking_assert (t || get_overrun ());
    5229     21660241 :   back_refs.safe_push (t);
    5230     21660241 :   return -(int)back_refs.length ();
    5231              : }
    5232              : 
    5233              : /* A chained set of decls.  */
    5234              : 
    5235              : void
    5236       158993 : trees_out::chained_decls (tree decls)
    5237              : {
    5238       366294 :   for (; decls; decls = DECL_CHAIN (decls))
    5239       207301 :     tree_node (decls);
    5240       158993 :   tree_node (NULL_TREE);
    5241       158993 : }
    5242              : 
    5243              : tree
    5244        60793 : trees_in::chained_decls ()
    5245              : {
    5246        60793 :   tree decls = NULL_TREE;
    5247        60793 :   for (tree *chain = &decls;;)
    5248       146014 :     if (tree decl = tree_node ())
    5249              :       {
    5250        85221 :         if (!DECL_P (decl) || DECL_CHAIN (decl))
    5251              :           {
    5252            0 :             set_overrun ();
    5253            0 :             break;
    5254              :           }
    5255        85221 :         *chain = decl;
    5256        85221 :         chain = &DECL_CHAIN (decl);
    5257              :       }
    5258              :     else
    5259        85221 :       break;
    5260              : 
    5261        60793 :   return decls;
    5262              : }
    5263              : 
    5264              : /* A vector of decls following DECL_CHAIN.  */
    5265              : 
    5266              : void
    5267       398358 : trees_out::vec_chained_decls (tree decls)
    5268              : {
    5269       398358 :   if (streaming_p ())
    5270              :     {
    5271              :       unsigned len = 0;
    5272              : 
    5273      1255674 :       for (tree decl = decls; decl; decl = DECL_CHAIN (decl))
    5274      1056541 :         len++;
    5275       199133 :       u (len);
    5276              :     }
    5277              : 
    5278      2511850 :   for (tree decl = decls; decl; decl = DECL_CHAIN (decl))
    5279              :     {
    5280       425799 :       if (DECL_IMPLICIT_TYPEDEF_P (decl)
    5281      2129751 :           && TYPE_NAME (TREE_TYPE (decl)) != decl)
    5282              :         /* An anonynmous struct with a typedef name.  An odd thing to
    5283              :            write.  */
    5284            8 :         tree_node (NULL_TREE);
    5285              :       else
    5286      2113484 :         tree_node (decl);
    5287              :     }
    5288       398358 : }
    5289              : 
    5290              : vec<tree, va_heap> *
    5291       135291 : trees_in::vec_chained_decls ()
    5292              : {
    5293       135291 :   vec<tree, va_heap> *v = NULL;
    5294              : 
    5295       135291 :   if (unsigned len = u ())
    5296              :     {
    5297        70988 :       vec_alloc (v, len);
    5298              : 
    5299       864476 :       for (unsigned ix = 0; ix < len; ix++)
    5300              :         {
    5301       793488 :           tree decl = tree_node ();
    5302       793488 :           if (decl && !DECL_P (decl))
    5303              :             {
    5304            0 :               set_overrun ();
    5305            0 :               break;
    5306              :             }
    5307       793488 :           v->quick_push (decl);
    5308              :         }
    5309              : 
    5310        70988 :       if (get_overrun ())
    5311              :         {
    5312            0 :           vec_free (v);
    5313            0 :           v = NULL;
    5314              :         }
    5315              :     }
    5316              : 
    5317       135291 :   return v;
    5318              : }
    5319              : 
    5320              : /* A vector of trees.  */
    5321              : 
    5322              : void
    5323       280054 : trees_out::tree_vec (vec<tree, va_gc> *v)
    5324              : {
    5325       280054 :   unsigned len = vec_safe_length (v);
    5326       280054 :   if (streaming_p ())
    5327       140004 :     u (len);
    5328       358820 :   for (unsigned ix = 0; ix != len; ix++)
    5329        78766 :     tree_node ((*v)[ix]);
    5330       280054 : }
    5331              : 
    5332              : vec<tree, va_gc> *
    5333        94716 : trees_in::tree_vec ()
    5334              : {
    5335        94716 :   vec<tree, va_gc> *v = NULL;
    5336        94716 :   if (unsigned len = u ())
    5337              :     {
    5338        23912 :       vec_alloc (v, len);
    5339        50443 :       for (unsigned ix = 0; ix != len; ix++)
    5340        26531 :         v->quick_push (tree_node ());
    5341              :     }
    5342        94716 :   return v;
    5343              : }
    5344              : 
    5345              : /* A vector of tree pairs.  */
    5346              : 
    5347              : void
    5348         7124 : trees_out::tree_pair_vec (vec<tree_pair_s, va_gc> *v)
    5349              : {
    5350         7124 :   unsigned len = vec_safe_length (v);
    5351         7124 :   if (streaming_p ())
    5352         3562 :     u (len);
    5353         7124 :   if (len)
    5354        38512 :     for (unsigned ix = 0; ix != len; ix++)
    5355              :       {
    5356        31518 :         tree_pair_s const &s = (*v)[ix];
    5357        31518 :         tree_node (s.purpose);
    5358        31518 :         tree_node (s.value);
    5359              :       }
    5360         7124 : }
    5361              : 
    5362              : vec<tree_pair_s, va_gc> *
    5363         2693 : trees_in::tree_pair_vec ()
    5364              : {
    5365         2693 :   vec<tree_pair_s, va_gc> *v = NULL;
    5366         2693 :   if (unsigned len = u ())
    5367              :     {
    5368         2639 :       vec_alloc (v, len);
    5369        14651 :       for (unsigned ix = 0; ix != len; ix++)
    5370              :         {
    5371        12012 :           tree_pair_s s;
    5372        12012 :           s.purpose = tree_node ();
    5373        12012 :           s.value = tree_node ();
    5374        12012 :           v->quick_push (s);
    5375              :       }
    5376              :     }
    5377         2693 :   return v;
    5378              : }
    5379              : 
    5380              : void
    5381       423548 : trees_out::tree_list (tree list, bool has_purpose)
    5382              : {
    5383      1892739 :   for (; list; list = TREE_CHAIN (list))
    5384              :     {
    5385      1469191 :       gcc_checking_assert (TREE_VALUE (list));
    5386      1469191 :       tree_node (TREE_VALUE (list));
    5387      1469191 :       if (has_purpose)
    5388      1420135 :         tree_node (TREE_PURPOSE (list));
    5389              :     }
    5390       423548 :   tree_node (NULL_TREE);
    5391       423548 : }
    5392              : 
    5393              : tree
    5394       146285 : trees_in::tree_list (bool has_purpose)
    5395              : {
    5396       146285 :   tree res = NULL_TREE;
    5397              : 
    5398       703508 :   for (tree *chain = &res; tree value = tree_node ();
    5399      1114446 :        chain = &TREE_CHAIN (*chain))
    5400              :     {
    5401       557223 :       tree purpose = has_purpose ? tree_node () : NULL_TREE;
    5402       557223 :       *chain = build_tree_list (purpose, value);
    5403       557223 :     }
    5404              : 
    5405       146285 :   return res;
    5406              : }
    5407              : 
    5408              : #define CASE_OMP_SIMD_CODE \
    5409              :     case OMP_SIMD:                      \
    5410              :     case OMP_STRUCTURED_BLOCK:          \
    5411              :     case OMP_LOOP:                      \
    5412              :     case OMP_ORDERED:                   \
    5413              :     case OMP_TILE:                      \
    5414              :     case OMP_UNROLL
    5415              : #define CASE_OMP_CODE \
    5416              :     case OMP_PARALLEL:                  \
    5417              :     case OMP_TASK:                      \
    5418              :     case OMP_FOR:                       \
    5419              :     case OMP_DISTRIBUTE:                \
    5420              :     case OMP_TASKLOOP:                  \
    5421              :     case OMP_TEAMS:                     \
    5422              :     case OMP_TARGET_DATA:               \
    5423              :     case OMP_TARGET:                    \
    5424              :     case OMP_SECTIONS:                  \
    5425              :     case OMP_CRITICAL:                  \
    5426              :     case OMP_SINGLE:                    \
    5427              :     case OMP_SCOPE:                     \
    5428              :     case OMP_TASKGROUP:                 \
    5429              :     case OMP_MASKED:                    \
    5430              :     case OMP_DISPATCH:                  \
    5431              :     case OMP_INTEROP:                   \
    5432              :     case OMP_MASTER:                    \
    5433              :     case OMP_TARGET_UPDATE:             \
    5434              :     case OMP_TARGET_ENTER_DATA:         \
    5435              :     case OMP_TARGET_EXIT_DATA:          \
    5436              :     case OMP_METADIRECTIVE:             \
    5437              :     case OMP_ATOMIC:                    \
    5438              :     case OMP_ATOMIC_READ:               \
    5439              :     case OMP_ATOMIC_CAPTURE_OLD:        \
    5440              :     case OMP_ATOMIC_CAPTURE_NEW
    5441              : #define CASE_OACC_CODE \
    5442              :     case OACC_PARALLEL:                 \
    5443              :     case OACC_KERNELS:                  \
    5444              :     case OACC_SERIAL:                   \
    5445              :     case OACC_DATA:                     \
    5446              :     case OACC_HOST_DATA:                \
    5447              :     case OACC_LOOP:                     \
    5448              :     case OACC_CACHE:                    \
    5449              :     case OACC_DECLARE:                  \
    5450              :     case OACC_ENTER_DATA:               \
    5451              :     case OACC_EXIT_DATA:                \
    5452              :     case OACC_UPDATE
    5453              : 
    5454              : /* Start tree write.  Write information to allocate the receiving
    5455              :    node.  */
    5456              : 
    5457              : void
    5458     20128647 : trees_out::start (tree t, bool code_streamed)
    5459              : {
    5460     20128647 :   if (TYPE_P (t))
    5461              :     {
    5462       770790 :       enum tree_code code = TREE_CODE (t);
    5463       770790 :       gcc_checking_assert (TYPE_MAIN_VARIANT (t) == t);
    5464              :       /* All these types are TYPE_NON_COMMON.  */
    5465       770790 :       gcc_checking_assert (code == RECORD_TYPE
    5466              :                            || code == UNION_TYPE
    5467              :                            || code == ENUMERAL_TYPE
    5468              :                            || code == TEMPLATE_TYPE_PARM
    5469              :                            || code == TEMPLATE_TEMPLATE_PARM
    5470              :                            || code == BOUND_TEMPLATE_TEMPLATE_PARM);
    5471              :     }
    5472              : 
    5473     20128647 :   if (!code_streamed)
    5474     19452558 :     u (TREE_CODE (t));
    5475              : 
    5476     20128647 :   switch (TREE_CODE (t))
    5477              :     {
    5478     18012129 :     default:
    5479     18012129 :       if (VL_EXP_CLASS_P (t))
    5480       775885 :         u (VL_EXP_OPERAND_LENGTH (t));
    5481              :       break;
    5482              : 
    5483       813245 :     case INTEGER_CST:
    5484       813245 :       u (TREE_INT_CST_NUNITS (t));
    5485       813245 :       u (TREE_INT_CST_EXT_NUNITS (t));
    5486       813245 :       break;
    5487              : 
    5488           18 :     case OMP_CLAUSE:
    5489           18 :       u (OMP_CLAUSE_CODE (t));
    5490           18 :       break;
    5491              : 
    5492            6 :     CASE_OMP_SIMD_CODE:
    5493            6 :       state->extensions |= SE_OPENMP_SIMD;
    5494            6 :       break;
    5495              : 
    5496            9 :     CASE_OMP_CODE:
    5497            9 :       state->extensions |= SE_OPENMP;
    5498            9 :       break;
    5499              : 
    5500            6 :     CASE_OACC_CODE:
    5501            6 :       state->extensions |= SE_OPENACC;
    5502            6 :       break;
    5503              : 
    5504        59583 :     case STRING_CST:
    5505        59583 :       str (TREE_STRING_POINTER (t), TREE_STRING_LENGTH (t));
    5506        59583 :       break;
    5507              : 
    5508           18 :     case RAW_DATA_CST:
    5509           18 :       if (RAW_DATA_OWNER (t) == NULL_TREE)
    5510              :         {
    5511              :           /* Stream RAW_DATA_CST with no owner (i.e. data pointing
    5512              :              into libcpp buffers) as something we can stream in as
    5513              :              STRING_CST which owns the data.  */
    5514            6 :           u (0);
    5515              :           /* Can't use str (RAW_DATA_POINTER (t), RAW_DATA_LENGTH (t));
    5516              :              here as there isn't a null termination after it.  */
    5517            6 :           z (RAW_DATA_LENGTH (t));
    5518            6 :           if (RAW_DATA_LENGTH (t))
    5519            6 :             if (void *ptr = buf (RAW_DATA_LENGTH (t) + 1))
    5520              :               {
    5521            6 :                 memcpy (ptr, RAW_DATA_POINTER (t), RAW_DATA_LENGTH (t));
    5522            6 :                 ((char *) ptr)[RAW_DATA_LENGTH (t)] = '\0';
    5523              :               }
    5524              :         }
    5525              :       else
    5526              :         {
    5527           12 :           gcc_assert (RAW_DATA_LENGTH (t));
    5528           12 :           u (RAW_DATA_LENGTH (t));
    5529              :         }
    5530              :       break;
    5531              : 
    5532           18 :     case VECTOR_CST:
    5533           18 :       u (VECTOR_CST_LOG2_NPATTERNS (t));
    5534           18 :       u (VECTOR_CST_NELTS_PER_PATTERN (t));
    5535           18 :       break;
    5536              : 
    5537       136442 :     case TREE_BINFO:
    5538       136442 :       u (BINFO_N_BASE_BINFOS (t));
    5539       136442 :       break;
    5540              : 
    5541      1107173 :     case TREE_VEC:
    5542      1107173 :       u (TREE_VEC_LENGTH (t));
    5543      1107173 :       break;
    5544              : 
    5545            0 :     case FIXED_CST:
    5546            0 :       gcc_unreachable (); /* Not supported in C++.  */
    5547            0 :       break;
    5548              : 
    5549            0 :     case IDENTIFIER_NODE:
    5550            0 :     case SSA_NAME:
    5551            0 :     case TARGET_MEM_REF:
    5552            0 :     case TRANSLATION_UNIT_DECL:
    5553              :       /* We shouldn't meet these.  */
    5554            0 :       gcc_unreachable ();
    5555     20128647 :       break;
    5556              :     }
    5557     20128647 : }
    5558              : 
    5559              : /* Start tree read.  Allocate the receiving node.  */
    5560              : 
    5561              : tree
    5562     15605896 : trees_in::start (unsigned code)
    5563              : {
    5564     15605896 :   tree t = NULL_TREE;
    5565              : 
    5566     15605896 :   if (!code)
    5567     14146796 :     code = u ();
    5568              : 
    5569     15605896 :   switch (code)
    5570              :     {
    5571     14001643 :     default:
    5572     14001643 :       if (code >= MAX_TREE_CODES)
    5573              :         {
    5574            0 :         fail:
    5575            0 :           set_overrun ();
    5576            0 :           return NULL_TREE;
    5577              :         }
    5578     14001643 :       else if (TREE_CODE_CLASS (code) == tcc_vl_exp)
    5579              :         {
    5580       610300 :           unsigned ops = u ();
    5581       610300 :           t = build_vl_exp (tree_code (code), ops);
    5582              :         }
    5583              :       else
    5584     13391343 :         t = make_node (tree_code (code));
    5585              :       break;
    5586              : 
    5587       602137 :     case INTEGER_CST:
    5588       602137 :       {
    5589       602137 :         unsigned n = u ();
    5590       602137 :         unsigned e = u ();
    5591       602137 :         t = make_int_cst (n, e);
    5592              :       }
    5593       602137 :       break;
    5594              : 
    5595           18 :     case OMP_CLAUSE:
    5596           18 :       t = build_omp_clause (UNKNOWN_LOCATION, omp_clause_code (u ()));
    5597           18 :       break;
    5598              : 
    5599            9 :     CASE_OMP_SIMD_CODE:
    5600            9 :       if (!(state->extensions & SE_OPENMP_SIMD))
    5601            0 :         goto fail;
    5602            9 :       t = make_node (tree_code (code));
    5603            9 :       break;
    5604              : 
    5605            9 :     CASE_OMP_CODE:
    5606            9 :       if (!(state->extensions & SE_OPENMP))
    5607            0 :         goto fail;
    5608            9 :       t = make_node (tree_code (code));
    5609            9 :       break;
    5610              : 
    5611            6 :     CASE_OACC_CODE:
    5612            6 :       if (!(state->extensions & SE_OPENACC))
    5613            0 :         goto fail;
    5614            6 :       t = make_node (tree_code (code));
    5615            6 :       break;
    5616              : 
    5617        50805 :     case STRING_CST:
    5618        50805 :       {
    5619        50805 :         size_t l;
    5620        50805 :         const char *chars = str (&l);
    5621        50805 :         t = build_string (l, chars);
    5622              :       }
    5623        50805 :       break;
    5624              : 
    5625            9 :     case RAW_DATA_CST:
    5626            9 :       {
    5627            9 :         size_t l = u ();
    5628            9 :         if (l == 0)
    5629              :           {
    5630              :             /* Stream in RAW_DATA_CST with no owner as STRING_CST
    5631              :                which owns the data.  */
    5632            3 :             const char *chars = str (&l);
    5633            3 :             t = build_string (l, chars);
    5634              :           }
    5635              :         else
    5636              :           {
    5637            6 :             t = make_node (RAW_DATA_CST);
    5638            6 :             RAW_DATA_LENGTH (t) = l;
    5639              :           }
    5640              :       }
    5641            9 :       break;
    5642              : 
    5643           24 :     case VECTOR_CST:
    5644           24 :       {
    5645           24 :         unsigned log2_npats = u ();
    5646           24 :         unsigned elts_per = u ();
    5647           24 :         t = make_vector (log2_npats, elts_per);
    5648              :       }
    5649           24 :       break;
    5650              : 
    5651        92023 :     case TREE_BINFO:
    5652        92023 :       t = make_tree_binfo (u ());
    5653        92023 :       break;
    5654              : 
    5655       859213 :     case TREE_VEC:
    5656       859213 :       t = make_tree_vec (u ());
    5657       859213 :       break;
    5658              : 
    5659            0 :     case FIXED_CST:
    5660            0 :     case IDENTIFIER_NODE:
    5661            0 :     case SSA_NAME:
    5662            0 :     case TARGET_MEM_REF:
    5663            0 :     case TRANSLATION_UNIT_DECL:
    5664            0 :       goto fail;
    5665              :     }
    5666              : 
    5667              :   return t;
    5668              : }
    5669              : 
    5670              : /* The kinds of interface an importer could have for a decl.  */
    5671              : 
    5672              : enum class importer_interface {
    5673              :   unknown,        /* The definition may or may not need to be emitted.  */
    5674              :   external,       /* The definition can always be found in another TU.  */
    5675              :   internal,       /* The definition should be emitted in the importer's TU.  */
    5676              :   always_emit,    /* The definition must be emitted in the importer's TU,
    5677              :                      regardless of if it's used or not. */
    5678              : };
    5679              : 
    5680              : /* Returns what kind of interface an importer will have of DECL.  */
    5681              : 
    5682              : static importer_interface
    5683       763651 : get_importer_interface (tree decl)
    5684              : {
    5685              :   /* Internal linkage entities must be emitted in each importer if
    5686              :      there is a definition available.  */
    5687       763651 :   if (!TREE_PUBLIC (decl))
    5688              :     return importer_interface::internal;
    5689              : 
    5690              :   /* Other entities that aren't vague linkage are either not definitions
    5691              :      or will be publicly emitted in this TU, so importers can just refer
    5692              :      to an external definition.  */
    5693       361476 :   if (!vague_linkage_p (decl))
    5694              :     return importer_interface::external;
    5695              : 
    5696              :   /* For explicit instantiations, importers can always rely on there
    5697              :      being a definition in another TU, unless this is a definition
    5698              :      in a header module: in which case the importer will always need
    5699              :      to emit it.  */
    5700       355078 :   if (DECL_LANG_SPECIFIC (decl)
    5701       355078 :       && DECL_EXPLICIT_INSTANTIATION (decl))
    5702        26454 :     return (header_module_p () && !DECL_EXTERNAL (decl)
    5703        26454 :             ? importer_interface::always_emit
    5704              :             : importer_interface::external);
    5705              : 
    5706              :   /* A gnu_inline function is never emitted in any TU.  */
    5707       328624 :   if (TREE_CODE (decl) == FUNCTION_DECL
    5708       241223 :       && DECL_DECLARED_INLINE_P (decl)
    5709       561858 :       && lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl)))
    5710              :     return importer_interface::external;
    5711              : 
    5712              :   /* Everything else has vague linkage.  */
    5713              :   return importer_interface::unknown;
    5714              : }
    5715              : 
    5716              : /* The structure streamers access the raw fields, because the
    5717              :    alternative, of using the accessor macros can require using
    5718              :    different accessors for the same underlying field, depending on the
    5719              :    tree code.  That's both confusing and annoying.  */
    5720              : 
    5721              : /* Read & write the core boolean flags.  */
    5722              : 
    5723              : void
    5724     20163618 : trees_out::core_bools (tree t, bits_out& bits)
    5725              : {
    5726              : #define WB(X) (bits.b (X))
    5727              : /* Stream X if COND holds, and if !COND stream a dummy value so that the
    5728              :    overall number of bits streamed is independent of the runtime value
    5729              :    of COND, which allows the compiler to better optimize this function.  */
    5730              : #define WB_IF(COND, X) WB ((COND) ? (X) : false)
    5731     20163618 :   tree_code code = TREE_CODE (t);
    5732              : 
    5733     20163618 :   WB (t->base.side_effects_flag);
    5734     20163618 :   WB (t->base.constant_flag);
    5735     20163618 :   WB (t->base.addressable_flag);
    5736     20163618 :   WB (t->base.volatile_flag);
    5737     20163618 :   WB (t->base.readonly_flag);
    5738              :   /* base.asm_written_flag is a property of the current TU's use of
    5739              :      this decl.  */
    5740     20163618 :   WB (t->base.nowarning_flag);
    5741              :   /* base.visited read as zero (it's set for writer, because that's
    5742              :      how we mark nodes).  */
    5743              :   /* base.used_flag is not streamed.  Readers may set TREE_USED of
    5744              :      decls they use.  */
    5745     20163618 :   WB (t->base.nothrow_flag);
    5746     20163618 :   WB (t->base.static_flag);
    5747              :   /* This is TYPE_CACHED_VALUES_P for types.  */
    5748     20163618 :   WB_IF (TREE_CODE_CLASS (code) != tcc_type, t->base.public_flag);
    5749     20163618 :   WB (t->base.private_flag);
    5750     20163618 :   WB (t->base.protected_flag);
    5751     20163618 :   WB (t->base.deprecated_flag);
    5752     20163618 :   WB (t->base.default_def_flag);
    5753              : 
    5754     20163618 :   switch (code)
    5755              :     {
    5756              :     case CALL_EXPR:
    5757              :     case INTEGER_CST:
    5758              :     case SSA_NAME:
    5759              :     case TARGET_MEM_REF:
    5760              :     case TREE_VEC:
    5761              :       /* These use different base.u fields.  */
    5762              :       return;
    5763              : 
    5764     17485623 :     default:
    5765     17485623 :       WB (t->base.u.bits.lang_flag_0);
    5766     17485623 :       bool flag_1 = t->base.u.bits.lang_flag_1;
    5767     17485623 :       if (!flag_1)
    5768              :         ;
    5769       572276 :       else if (code == TEMPLATE_INFO)
    5770              :         /* This is TI_PENDING_TEMPLATE_FLAG, not relevant to reader.  */
    5771              :         flag_1 = false;
    5772       566088 :       else if (code == VAR_DECL)
    5773              :         {
    5774              :           /* This is DECL_INITIALIZED_P.  */
    5775       104735 :           if (TREE_CODE (DECL_CONTEXT (t)) != FUNCTION_DECL)
    5776              :             /* We'll set this when reading the definition.  */
    5777     17485623 :             flag_1 = false;
    5778              :         }
    5779     17485623 :       WB (flag_1);
    5780     17485623 :       WB (t->base.u.bits.lang_flag_2);
    5781     17485623 :       WB (t->base.u.bits.lang_flag_3);
    5782     17485623 :       WB (t->base.u.bits.lang_flag_4);
    5783     17485623 :       WB (t->base.u.bits.lang_flag_5);
    5784     17485623 :       WB (t->base.u.bits.lang_flag_6);
    5785     17485623 :       WB (t->base.u.bits.saturating_flag);
    5786     17485623 :       WB (t->base.u.bits.unsigned_flag);
    5787     17485623 :       WB (t->base.u.bits.packed_flag);
    5788     17485623 :       WB (t->base.u.bits.user_align);
    5789     17485623 :       WB (t->base.u.bits.nameless_flag);
    5790     17485623 :       WB (t->base.u.bits.atomic_flag);
    5791     17485623 :       WB (t->base.u.bits.unavailable_flag);
    5792     17485623 :       break;
    5793              :     }
    5794              : 
    5795     17485623 :   if (TREE_CODE_CLASS (code) == tcc_type)
    5796              :     {
    5797       805761 :       WB (t->type_common.no_force_blk_flag);
    5798       805761 :       WB (t->type_common.needs_constructing_flag);
    5799       805761 :       WB (t->type_common.transparent_aggr_flag);
    5800       805761 :       WB (t->type_common.restrict_flag);
    5801       805761 :       WB (t->type_common.string_flag);
    5802       805761 :       WB (t->type_common.lang_flag_0);
    5803       805761 :       WB (t->type_common.lang_flag_1);
    5804       805761 :       WB (t->type_common.lang_flag_2);
    5805       805761 :       WB (t->type_common.lang_flag_3);
    5806       805761 :       WB (t->type_common.lang_flag_4);
    5807       805761 :       WB (t->type_common.lang_flag_5);
    5808       805761 :       WB (t->type_common.lang_flag_6);
    5809       805761 :       WB (t->type_common.typeless_storage);
    5810              :     }
    5811              : 
    5812     17485623 :   if (TREE_CODE_CLASS (code) != tcc_declaration)
    5813              :     return;
    5814              : 
    5815      4373874 :   if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
    5816              :     {
    5817      4373874 :       WB (t->decl_common.nonlocal_flag);
    5818      4373874 :       WB (t->decl_common.virtual_flag);
    5819      4373874 :       WB (t->decl_common.ignored_flag);
    5820      4373874 :       WB (t->decl_common.abstract_flag);
    5821      4373874 :       WB (t->decl_common.artificial_flag);
    5822      4373874 :       WB (t->decl_common.preserve_flag);
    5823      4373874 :       WB (t->decl_common.debug_expr_is_from);
    5824      4373874 :       WB (t->decl_common.lang_flag_0);
    5825      4373874 :       WB (t->decl_common.lang_flag_1);
    5826      4373874 :       WB (t->decl_common.lang_flag_2);
    5827      4373874 :       WB (t->decl_common.lang_flag_3);
    5828      4373874 :       WB (t->decl_common.lang_flag_4);
    5829              : 
    5830      4373874 :       {
    5831              :         /* This is DECL_INTERFACE_KNOWN: We should redetermine whether
    5832              :            we need to import or export any vague-linkage entities on
    5833              :            stream-in.  */
    5834      4373874 :         bool interface_known = t->decl_common.lang_flag_5;
    5835      4373874 :         if (interface_known
    5836      4373874 :             && get_importer_interface (t) == importer_interface::unknown)
    5837              :           interface_known = false;
    5838      4373874 :         WB (interface_known);
    5839              :       }
    5840              : 
    5841      4373874 :       WB (t->decl_common.lang_flag_6);
    5842      4373874 :       WB (t->decl_common.lang_flag_7);
    5843      4373874 :       WB (t->decl_common.lang_flag_8);
    5844      4373874 :       WB (t->decl_common.decl_flag_0);
    5845              : 
    5846      4373874 :       {
    5847              :         /* DECL_EXTERNAL -> decl_flag_1
    5848              :              == it is defined elsewhere
    5849              :            DECL_NOT_REALLY_EXTERN -> base.not_really_extern
    5850              :              == that was a lie, it is here  */
    5851              : 
    5852      4373874 :         bool is_external = t->decl_common.decl_flag_1;
    5853              :         /* maybe_emit_vtables relies on vtables being marked as
    5854              :            DECL_EXTERNAL and DECL_NOT_REALLY_EXTERN before processing.  */
    5855      4373874 :         if (!is_external && VAR_P (t) && DECL_VTABLE_OR_VTT_P (t))
    5856              :           is_external = true;
    5857              :         /* Things we emit here might well be external from the POV of an
    5858              :            importer.  */
    5859      4373471 :         if (!is_external
    5860      3675733 :             && VAR_OR_FUNCTION_DECL_P (t)
    5861      4723836 :             && get_importer_interface (t) == importer_interface::external)
    5862              :           is_external = true;
    5863      4373874 :         WB (is_external);
    5864              :       }
    5865              : 
    5866      4373874 :       WB (t->decl_common.decl_flag_2);
    5867      4373874 :       WB (t->decl_common.decl_flag_3);
    5868      4373874 :       WB (t->decl_common.not_gimple_reg_flag);
    5869      4373874 :       WB (t->decl_common.decl_by_reference_flag);
    5870      4373874 :       WB (t->decl_common.decl_read_flag);
    5871      4373874 :       WB (t->decl_common.decl_nonshareable_flag);
    5872      4373874 :       WB (t->decl_common.decl_not_flexarray);
    5873              :     }
    5874              :   else
    5875              :     return;
    5876              : 
    5877      4373874 :   if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
    5878              :     {
    5879      2060756 :       WB (t->decl_with_vis.defer_output);
    5880      2060756 :       WB (t->decl_with_vis.hard_register);
    5881      2060756 :       WB (t->decl_with_vis.common_flag);
    5882      2060756 :       WB (t->decl_with_vis.in_text_section);
    5883      2060756 :       WB (t->decl_with_vis.in_constant_pool);
    5884      2060756 :       WB (t->decl_with_vis.dllimport_flag);
    5885      2060756 :       WB (t->decl_with_vis.weak_flag);
    5886      2060756 :       WB (t->decl_with_vis.seen_in_bind_expr);
    5887      2060756 :       WB (t->decl_with_vis.comdat_flag);
    5888      2060756 :       WB (t->decl_with_vis.visibility_specified);
    5889      2060756 :       WB (t->decl_with_vis.init_priority_p);
    5890      2060756 :       WB (t->decl_with_vis.shadowed_for_var_p);
    5891      2060756 :       WB (t->decl_with_vis.cxx_constructor);
    5892      2060756 :       WB (t->decl_with_vis.cxx_destructor);
    5893      2060756 :       WB (t->decl_with_vis.final);
    5894      2060756 :       WB (t->decl_with_vis.regdecl_flag);
    5895              :     }
    5896              :   else
    5897              :     return;
    5898              : 
    5899      2060756 :   if (CODE_CONTAINS_STRUCT (code, TS_FUNCTION_DECL))
    5900              :     {
    5901       605359 :       WB (t->function_decl.static_ctor_flag);
    5902       605359 :       WB (t->function_decl.static_dtor_flag);
    5903       605359 :       WB (t->function_decl.uninlinable);
    5904       605359 :       WB (t->function_decl.possibly_inlined);
    5905       605359 :       WB (t->function_decl.novops_flag);
    5906       605359 :       WB (t->function_decl.returns_twice_flag);
    5907       605359 :       WB (t->function_decl.malloc_flag);
    5908       605359 :       WB (t->function_decl.declared_inline_flag);
    5909       605359 :       WB (t->function_decl.no_inline_warning_flag);
    5910       605359 :       WB (t->function_decl.no_instrument_function_entry_exit);
    5911       605359 :       WB (t->function_decl.no_limit_stack);
    5912       605359 :       WB (t->function_decl.disregard_inline_limits);
    5913       605359 :       WB (t->function_decl.pure_flag);
    5914       605359 :       WB (t->function_decl.looping_const_or_pure_flag);
    5915              : 
    5916       605359 :       WB (t->function_decl.has_debug_args_flag);
    5917       605359 :       WB (t->function_decl.versioned_function);
    5918       605359 :       WB (t->function_decl.replaceable_operator);
    5919              : 
    5920              :       /* decl_type is a (misnamed) 2 bit discriminator.  */
    5921       605359 :       unsigned kind = (unsigned)t->function_decl.decl_type;
    5922       605359 :       WB ((kind >> 0) & 1);
    5923       605359 :       WB ((kind >> 1) & 1);
    5924              :     }
    5925              : #undef WB_IF
    5926              : #undef WB
    5927              : }
    5928              : 
    5929              : bool
    5930     15627742 : trees_in::core_bools (tree t, bits_in& bits)
    5931              : {
    5932              : #define RB(X) ((X) = bits.b ())
    5933              : /* See the comment for WB_IF in trees_out::core_bools.  */
    5934              : #define RB_IF(COND, X) ((COND) ? RB (X) : bits.b ())
    5935              : 
    5936     15627742 :   tree_code code = TREE_CODE (t);
    5937              : 
    5938     15627742 :   RB (t->base.side_effects_flag);
    5939     15627742 :   RB (t->base.constant_flag);
    5940     15627742 :   RB (t->base.addressable_flag);
    5941     15627742 :   RB (t->base.volatile_flag);
    5942     15627742 :   RB (t->base.readonly_flag);
    5943              :   /* base.asm_written_flag is not streamed.  */
    5944     15627742 :   RB (t->base.nowarning_flag);
    5945              :   /* base.visited is not streamed.  */
    5946              :   /* base.used_flag is not streamed.  */
    5947     15627742 :   RB (t->base.nothrow_flag);
    5948     15627742 :   RB (t->base.static_flag);
    5949     15627742 :   RB_IF (TREE_CODE_CLASS (code) != tcc_type, t->base.public_flag);
    5950     15627742 :   RB (t->base.private_flag);
    5951     15627742 :   RB (t->base.protected_flag);
    5952     15627742 :   RB (t->base.deprecated_flag);
    5953     15627742 :   RB (t->base.default_def_flag);
    5954              : 
    5955     15627742 :   switch (code)
    5956              :     {
    5957      2057947 :     case CALL_EXPR:
    5958      2057947 :     case INTEGER_CST:
    5959      2057947 :     case SSA_NAME:
    5960      2057947 :     case TARGET_MEM_REF:
    5961      2057947 :     case TREE_VEC:
    5962              :       /* These use different base.u fields.  */
    5963      2057947 :       goto done;
    5964              : 
    5965     13569795 :     default:
    5966     13569795 :       RB (t->base.u.bits.lang_flag_0);
    5967     13569795 :       RB (t->base.u.bits.lang_flag_1);
    5968     13569795 :       RB (t->base.u.bits.lang_flag_2);
    5969     13569795 :       RB (t->base.u.bits.lang_flag_3);
    5970     13569795 :       RB (t->base.u.bits.lang_flag_4);
    5971     13569795 :       RB (t->base.u.bits.lang_flag_5);
    5972     13569795 :       RB (t->base.u.bits.lang_flag_6);
    5973     13569795 :       RB (t->base.u.bits.saturating_flag);
    5974     13569795 :       RB (t->base.u.bits.unsigned_flag);
    5975     13569795 :       RB (t->base.u.bits.packed_flag);
    5976     13569795 :       RB (t->base.u.bits.user_align);
    5977     13569795 :       RB (t->base.u.bits.nameless_flag);
    5978     13569795 :       RB (t->base.u.bits.atomic_flag);
    5979     13569795 :       RB (t->base.u.bits.unavailable_flag);
    5980     13569795 :       break;
    5981              :     }
    5982              : 
    5983     13569795 :   if (TREE_CODE_CLASS (code) == tcc_type)
    5984              :     {
    5985       588105 :       RB (t->type_common.no_force_blk_flag);
    5986       588105 :       RB (t->type_common.needs_constructing_flag);
    5987       588105 :       RB (t->type_common.transparent_aggr_flag);
    5988       588105 :       RB (t->type_common.restrict_flag);
    5989       588105 :       RB (t->type_common.string_flag);
    5990       588105 :       RB (t->type_common.lang_flag_0);
    5991       588105 :       RB (t->type_common.lang_flag_1);
    5992       588105 :       RB (t->type_common.lang_flag_2);
    5993       588105 :       RB (t->type_common.lang_flag_3);
    5994       588105 :       RB (t->type_common.lang_flag_4);
    5995       588105 :       RB (t->type_common.lang_flag_5);
    5996       588105 :       RB (t->type_common.lang_flag_6);
    5997       588105 :       RB (t->type_common.typeless_storage);
    5998              :     }
    5999              : 
    6000     13569795 :   if (TREE_CODE_CLASS (code) != tcc_declaration)
    6001     10237335 :     goto done;
    6002              : 
    6003      3332460 :   if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
    6004              :     {
    6005      3332460 :       RB (t->decl_common.nonlocal_flag);
    6006      3332460 :       RB (t->decl_common.virtual_flag);
    6007      3332460 :       RB (t->decl_common.ignored_flag);
    6008      3332460 :       RB (t->decl_common.abstract_flag);
    6009      3332460 :       RB (t->decl_common.artificial_flag);
    6010      3332460 :       RB (t->decl_common.preserve_flag);
    6011      3332460 :       RB (t->decl_common.debug_expr_is_from);
    6012      3332460 :       RB (t->decl_common.lang_flag_0);
    6013      3332460 :       RB (t->decl_common.lang_flag_1);
    6014      3332460 :       RB (t->decl_common.lang_flag_2);
    6015      3332460 :       RB (t->decl_common.lang_flag_3);
    6016      3332460 :       RB (t->decl_common.lang_flag_4);
    6017      3332460 :       RB (t->decl_common.lang_flag_5);
    6018      3332460 :       RB (t->decl_common.lang_flag_6);
    6019      3332460 :       RB (t->decl_common.lang_flag_7);
    6020      3332460 :       RB (t->decl_common.lang_flag_8);
    6021      3332460 :       RB (t->decl_common.decl_flag_0);
    6022      3332460 :       RB (t->decl_common.decl_flag_1);
    6023      3332460 :       RB (t->decl_common.decl_flag_2);
    6024      3332460 :       RB (t->decl_common.decl_flag_3);
    6025      3332460 :       RB (t->decl_common.not_gimple_reg_flag);
    6026      3332460 :       RB (t->decl_common.decl_by_reference_flag);
    6027      3332460 :       RB (t->decl_common.decl_read_flag);
    6028      3332460 :       RB (t->decl_common.decl_nonshareable_flag);
    6029      3332460 :       RB (t->decl_common.decl_not_flexarray);
    6030              :     }
    6031              :   else
    6032            0 :     goto done;
    6033              : 
    6034      3332460 :   if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
    6035              :     {
    6036      1534334 :       RB (t->decl_with_vis.defer_output);
    6037      1534334 :       RB (t->decl_with_vis.hard_register);
    6038      1534334 :       RB (t->decl_with_vis.common_flag);
    6039      1534334 :       RB (t->decl_with_vis.in_text_section);
    6040      1534334 :       RB (t->decl_with_vis.in_constant_pool);
    6041      1534334 :       RB (t->decl_with_vis.dllimport_flag);
    6042      1534334 :       RB (t->decl_with_vis.weak_flag);
    6043      1534334 :       RB (t->decl_with_vis.seen_in_bind_expr);
    6044      1534334 :       RB (t->decl_with_vis.comdat_flag);
    6045      1534334 :       RB (t->decl_with_vis.visibility_specified);
    6046      1534334 :       RB (t->decl_with_vis.init_priority_p);
    6047      1534334 :       RB (t->decl_with_vis.shadowed_for_var_p);
    6048      1534334 :       RB (t->decl_with_vis.cxx_constructor);
    6049      1534334 :       RB (t->decl_with_vis.cxx_destructor);
    6050      1534334 :       RB (t->decl_with_vis.final);
    6051      1534334 :       RB (t->decl_with_vis.regdecl_flag);
    6052              :     }
    6053              :   else
    6054      1798126 :     goto done;
    6055              : 
    6056      1534334 :   if (CODE_CONTAINS_STRUCT (code, TS_FUNCTION_DECL))
    6057              :     {
    6058       470536 :       RB (t->function_decl.static_ctor_flag);
    6059       470536 :       RB (t->function_decl.static_dtor_flag);
    6060       470536 :       RB (t->function_decl.uninlinable);
    6061       470536 :       RB (t->function_decl.possibly_inlined);
    6062       470536 :       RB (t->function_decl.novops_flag);
    6063       470536 :       RB (t->function_decl.returns_twice_flag);
    6064       470536 :       RB (t->function_decl.malloc_flag);
    6065       470536 :       RB (t->function_decl.declared_inline_flag);
    6066       470536 :       RB (t->function_decl.no_inline_warning_flag);
    6067       470536 :       RB (t->function_decl.no_instrument_function_entry_exit);
    6068       470536 :       RB (t->function_decl.no_limit_stack);
    6069       470536 :       RB (t->function_decl.disregard_inline_limits);
    6070       470536 :       RB (t->function_decl.pure_flag);
    6071       470536 :       RB (t->function_decl.looping_const_or_pure_flag);
    6072              : 
    6073       470536 :       RB (t->function_decl.has_debug_args_flag);
    6074       470536 :       RB (t->function_decl.versioned_function);
    6075       470536 :       RB (t->function_decl.replaceable_operator);
    6076              : 
    6077              :       /* decl_type is a (misnamed) 2 bit discriminator.  */
    6078       470536 :       unsigned kind = 0;
    6079       470536 :       kind |= unsigned (bits.b ()) << 0;
    6080       470536 :       kind |= unsigned (bits.b ()) << 1;
    6081       470536 :       t->function_decl.decl_type = function_decl_type (kind);
    6082              :     }
    6083              : #undef RB_IF
    6084              : #undef RB
    6085      1063798 : done:
    6086     15627742 :   return !get_overrun ();
    6087              : }
    6088              : 
    6089              : void
    6090      2662530 : trees_out::lang_decl_bools (tree t, bits_out& bits)
    6091              : {
    6092              : #define WB(X) (bits.b (X))
    6093      2662530 :   const struct lang_decl *lang = DECL_LANG_SPECIFIC (t);
    6094              : 
    6095      2662530 :   bits.bflush ();
    6096      2662530 :   WB (lang->u.base.language == lang_cplusplus);
    6097      2662530 :   WB ((lang->u.base.use_template >> 0) & 1);
    6098      2662530 :   WB ((lang->u.base.use_template >> 1) & 1);
    6099              :   /* Do not write lang->u.base.not_really_extern, importer will set
    6100              :      when reading the definition (if any).  */
    6101      2662530 :   WB (lang->u.base.initialized_in_class);
    6102              : 
    6103      2662530 :   WB (lang->u.base.threadprivate_or_deleted_p);
    6104      2662530 :   WB (lang->u.base.anticipated_p);
    6105      2662530 :   WB (lang->u.base.friend_or_tls);
    6106      2662530 :   WB (lang->u.base.unknown_bound_p);
    6107              :   /* Do not write lang->u.base.odr_used, importer will recalculate if
    6108              :      they do ODR use this decl.  */
    6109      2662530 :   WB (lang->u.base.concept_p);
    6110      2662530 :   WB (lang->u.base.var_declared_inline_p);
    6111      2662530 :   WB (lang->u.base.dependent_init_p);
    6112              : 
    6113              :   /* When building a header unit, everything is marked as purview, (so
    6114              :      we know which decls to write).  But when we import them we do not
    6115              :      want to mark them as in module purview.  */
    6116      5238411 :   WB (lang->u.base.module_purview_p && !header_module_p ());
    6117      2662530 :   WB (lang->u.base.module_attach_p);
    6118              :   /* Importer will set module_import_p and module_entity_p themselves
    6119              :      as appropriate.  */
    6120      2662530 :   WB (lang->u.base.module_keyed_decls_p);
    6121              : 
    6122      2662530 :   WB (lang->u.base.omp_declare_mapper_p);
    6123              : 
    6124      2662530 :   switch (lang->u.base.selector)
    6125              :     {
    6126            0 :     default:
    6127            0 :       gcc_unreachable ();
    6128              : 
    6129       605359 :     case lds_fn:  /* lang_decl_fn.  */
    6130       605359 :       WB (lang->u.fn.global_ctor_p);
    6131       605359 :       WB (lang->u.fn.global_dtor_p);
    6132              : 
    6133       605359 :       WB (lang->u.fn.static_function);
    6134       605359 :       WB (lang->u.fn.pure_virtual);
    6135       605359 :       WB (lang->u.fn.defaulted_p);
    6136       605359 :       WB (lang->u.fn.has_in_charge_parm_p);
    6137       605359 :       WB (lang->u.fn.has_vtt_parm_p);
    6138              :       /* There shouldn't be a pending inline at this point.  */
    6139       605359 :       gcc_assert (!lang->u.fn.pending_inline_p);
    6140       605359 :       WB (lang->u.fn.nonconverting);
    6141       605359 :       WB (lang->u.fn.thunk_p);
    6142              : 
    6143       605359 :       WB (lang->u.fn.this_thunk_p);
    6144       605359 :       WB (lang->u.fn.omp_declare_reduction_p);
    6145       605359 :       WB (lang->u.fn.has_dependent_explicit_spec_p);
    6146       605359 :       WB (lang->u.fn.immediate_fn_p);
    6147       605359 :       WB (lang->u.fn.maybe_deleted);
    6148       605359 :       WB (lang->u.fn.coroutine_p);
    6149       605359 :       WB (lang->u.fn.implicit_constexpr);
    6150       605359 :       WB (lang->u.fn.escalated_p);
    6151       605359 :       WB (lang->u.fn.xobj_func);
    6152       605359 :       goto lds_min;
    6153              : 
    6154         3205 :     case lds_decomp:  /* lang_decl_decomp.  */
    6155              :       /* No bools.  */
    6156         3205 :       goto lds_min;
    6157              : 
    6158              :     case lds_min:  /* lang_decl_min.  */
    6159      2662530 :     lds_min:
    6160              :       /* No bools.  */
    6161              :       break;
    6162              : 
    6163              :     case lds_ns:  /* lang_decl_ns.  */
    6164              :       /* No bools.  */
    6165              :       break;
    6166              : 
    6167              :     case lds_parm:  /* lang_decl_parm.  */
    6168              :       /* No bools.  */
    6169              :       break;
    6170              :     }
    6171              : #undef WB
    6172      2662530 : }
    6173              : 
    6174              : bool
    6175      2087622 : trees_in::lang_decl_bools (tree t, bits_in& bits)
    6176              : {
    6177              : #define RB(X) ((X) = bits.b ())
    6178      2087622 :   struct lang_decl *lang = DECL_LANG_SPECIFIC (t);
    6179              : 
    6180      2087622 :   bits.bflush ();
    6181      2087622 :   lang->u.base.language = bits.b () ? lang_cplusplus : lang_c;
    6182      2087622 :   unsigned v;
    6183      2087622 :   v = bits.b () << 0;
    6184      2087622 :   v |= bits.b () << 1;
    6185      2087622 :   lang->u.base.use_template = v;
    6186              :   /* lang->u.base.not_really_extern is not streamed.  */
    6187      2087622 :   RB (lang->u.base.initialized_in_class);
    6188              : 
    6189      2087622 :   RB (lang->u.base.threadprivate_or_deleted_p);
    6190      2087622 :   RB (lang->u.base.anticipated_p);
    6191      2087622 :   RB (lang->u.base.friend_or_tls);
    6192      2087622 :   RB (lang->u.base.unknown_bound_p);
    6193              :   /* lang->u.base.odr_used is not streamed.  */
    6194      2087622 :   RB (lang->u.base.concept_p);
    6195      2087622 :   RB (lang->u.base.var_declared_inline_p);
    6196      2087622 :   RB (lang->u.base.dependent_init_p);
    6197              : 
    6198      2087622 :   RB (lang->u.base.module_purview_p);
    6199      2087622 :   RB (lang->u.base.module_attach_p);
    6200              :   /* module_import_p and module_entity_p are not streamed.  */
    6201      2087622 :   RB (lang->u.base.module_keyed_decls_p);
    6202              : 
    6203      2087622 :   RB (lang->u.base.omp_declare_mapper_p);
    6204              : 
    6205      2087622 :   switch (lang->u.base.selector)
    6206              :     {
    6207            0 :     default:
    6208            0 :       gcc_unreachable ();
    6209              : 
    6210       470536 :     case lds_fn:  /* lang_decl_fn.  */
    6211       470536 :       RB (lang->u.fn.global_ctor_p);
    6212       470536 :       RB (lang->u.fn.global_dtor_p);
    6213              : 
    6214       470536 :       RB (lang->u.fn.static_function);
    6215       470536 :       RB (lang->u.fn.pure_virtual);
    6216       470536 :       RB (lang->u.fn.defaulted_p);
    6217       470536 :       RB (lang->u.fn.has_in_charge_parm_p);
    6218       470536 :       RB (lang->u.fn.has_vtt_parm_p);
    6219              :       /* lang->u.f.n.pending_inline_p is not streamed.  */
    6220       470536 :       RB (lang->u.fn.nonconverting);
    6221       470536 :       RB (lang->u.fn.thunk_p);
    6222              : 
    6223       470536 :       RB (lang->u.fn.this_thunk_p);
    6224       470536 :       RB (lang->u.fn.omp_declare_reduction_p);
    6225       470536 :       RB (lang->u.fn.has_dependent_explicit_spec_p);
    6226       470536 :       RB (lang->u.fn.immediate_fn_p);
    6227       470536 :       RB (lang->u.fn.maybe_deleted);
    6228       470536 :       RB (lang->u.fn.coroutine_p);
    6229       470536 :       RB (lang->u.fn.implicit_constexpr);
    6230       470536 :       RB (lang->u.fn.escalated_p);
    6231       470536 :       RB (lang->u.fn.xobj_func);
    6232       470536 :       goto lds_min;
    6233              : 
    6234         3043 :     case lds_decomp:  /* lang_decl_decomp.  */
    6235              :       /* No bools.  */
    6236         3043 :       goto lds_min;
    6237              : 
    6238              :     case lds_min:  /* lang_decl_min.  */
    6239      2087622 :     lds_min:
    6240              :       /* No bools.  */
    6241              :       break;
    6242              : 
    6243              :     case lds_ns:  /* lang_decl_ns.  */
    6244              :       /* No bools.  */
    6245              :       break;
    6246              : 
    6247              :     case lds_parm:  /* lang_decl_parm.  */
    6248              :       /* No bools.  */
    6249              :       break;
    6250              :     }
    6251              : #undef RB
    6252      2087622 :   return !get_overrun ();
    6253              : }
    6254              : 
    6255              : void
    6256       210637 : trees_out::lang_type_bools (tree t, bits_out& bits)
    6257              : {
    6258              : #define WB(X) (bits.b (X))
    6259       210637 :   const struct lang_type *lang = TYPE_LANG_SPECIFIC (t);
    6260              : 
    6261       210637 :   bits.bflush ();
    6262       210637 :   WB (lang->has_type_conversion);
    6263       210637 :   WB (lang->has_copy_ctor);
    6264       210637 :   WB (lang->has_default_ctor);
    6265       210637 :   WB (lang->const_needs_init);
    6266       210637 :   WB (lang->ref_needs_init);
    6267       210637 :   WB (lang->has_const_copy_assign);
    6268       210637 :   WB ((lang->use_template >> 0) & 1);
    6269       210637 :   WB ((lang->use_template >> 1) & 1);
    6270              : 
    6271       210637 :   WB (lang->has_mutable);
    6272       210637 :   WB (lang->com_interface);
    6273       210637 :   WB (lang->non_pod_class);
    6274       210637 :   WB (lang->nearly_empty_p);
    6275       210637 :   WB (lang->user_align);
    6276       210637 :   WB (lang->has_copy_assign);
    6277       210637 :   WB (lang->has_new);
    6278       210637 :   WB (lang->has_array_new);
    6279              : 
    6280       210637 :   WB ((lang->gets_delete >> 0) & 1);
    6281       210637 :   WB ((lang->gets_delete >> 1) & 1);
    6282       210637 :   WB (lang->interface_only);
    6283       210637 :   WB (lang->interface_unknown);
    6284       210637 :   WB (lang->contains_empty_class_p);
    6285       210637 :   WB (lang->anon_aggr);
    6286       210637 :   WB (lang->non_zero_init);
    6287       210637 :   WB (lang->empty_p);
    6288              : 
    6289       210637 :   WB (lang->vec_new_uses_cookie);
    6290       210637 :   WB (lang->declared_class);
    6291       210637 :   WB (lang->diamond_shaped);
    6292       210637 :   WB (lang->repeated_base);
    6293       210637 :   gcc_checking_assert (!lang->being_defined);
    6294              :   // lang->debug_requested
    6295       210637 :   WB (lang->fields_readonly);
    6296       210637 :   WB (lang->ptrmemfunc_flag);
    6297              : 
    6298       210637 :   WB (lang->lazy_default_ctor);
    6299       210637 :   WB (lang->lazy_copy_ctor);
    6300       210637 :   WB (lang->lazy_copy_assign);
    6301       210637 :   WB (lang->lazy_destructor);
    6302       210637 :   WB (lang->has_const_copy_ctor);
    6303       210637 :   WB (lang->has_complex_copy_ctor);
    6304       210637 :   WB (lang->has_complex_copy_assign);
    6305       210637 :   WB (lang->non_aggregate);
    6306              : 
    6307       210637 :   WB (lang->has_complex_dflt);
    6308       210637 :   WB (lang->has_list_ctor);
    6309       210637 :   WB (lang->non_std_layout);
    6310       210637 :   WB (lang->is_literal);
    6311       210637 :   WB (lang->lazy_move_ctor);
    6312       210637 :   WB (lang->lazy_move_assign);
    6313       210637 :   WB (lang->has_complex_move_ctor);
    6314       210637 :   WB (lang->has_complex_move_assign);
    6315              : 
    6316       210637 :   WB (lang->has_constexpr_ctor);
    6317       210637 :   WB (lang->unique_obj_representations);
    6318       210637 :   WB (lang->unique_obj_representations_set);
    6319       210637 :   gcc_checking_assert (!lang->erroneous);
    6320       210637 :   WB (lang->non_pod_aggregate);
    6321       210637 :   WB (lang->non_aggregate_pod);
    6322              : #undef WB
    6323       210637 : }
    6324              : 
    6325              : bool
    6326       154795 : trees_in::lang_type_bools (tree t, bits_in& bits)
    6327              : {
    6328              : #define RB(X) ((X) = bits.b ())
    6329       154795 :   struct lang_type *lang = TYPE_LANG_SPECIFIC (t);
    6330              : 
    6331       154795 :   bits.bflush ();
    6332       154795 :   RB (lang->has_type_conversion);
    6333       154795 :   RB (lang->has_copy_ctor);
    6334       154795 :   RB (lang->has_default_ctor);
    6335       154795 :   RB (lang->const_needs_init);
    6336       154795 :   RB (lang->ref_needs_init);
    6337       154795 :   RB (lang->has_const_copy_assign);
    6338       154795 :   unsigned v;
    6339       154795 :   v = bits.b () << 0;
    6340       154795 :   v |= bits.b () << 1;
    6341       154795 :   lang->use_template = v;
    6342              : 
    6343       154795 :   RB (lang->has_mutable);
    6344       154795 :   RB (lang->com_interface);
    6345       154795 :   RB (lang->non_pod_class);
    6346       154795 :   RB (lang->nearly_empty_p);
    6347       154795 :   RB (lang->user_align);
    6348       154795 :   RB (lang->has_copy_assign);
    6349       154795 :   RB (lang->has_new);
    6350       154795 :   RB (lang->has_array_new);
    6351              : 
    6352       154795 :   v = bits.b () << 0;
    6353       154795 :   v |= bits.b () << 1;
    6354       154795 :   lang->gets_delete = v;
    6355       154795 :   RB (lang->interface_only);
    6356       154795 :   RB (lang->interface_unknown);
    6357       154795 :   RB (lang->contains_empty_class_p);
    6358       154795 :   RB (lang->anon_aggr);
    6359       154795 :   RB (lang->non_zero_init);
    6360       154795 :   RB (lang->empty_p);
    6361              : 
    6362       154795 :   RB (lang->vec_new_uses_cookie);
    6363       154795 :   RB (lang->declared_class);
    6364       154795 :   RB (lang->diamond_shaped);
    6365       154795 :   RB (lang->repeated_base);
    6366       154795 :   gcc_checking_assert (!lang->being_defined);
    6367       154795 :   gcc_checking_assert (!lang->debug_requested);
    6368       154795 :   RB (lang->fields_readonly);
    6369       154795 :   RB (lang->ptrmemfunc_flag);
    6370              : 
    6371       154795 :   RB (lang->lazy_default_ctor);
    6372       154795 :   RB (lang->lazy_copy_ctor);
    6373       154795 :   RB (lang->lazy_copy_assign);
    6374       154795 :   RB (lang->lazy_destructor);
    6375       154795 :   RB (lang->has_const_copy_ctor);
    6376       154795 :   RB (lang->has_complex_copy_ctor);
    6377       154795 :   RB (lang->has_complex_copy_assign);
    6378       154795 :   RB (lang->non_aggregate);
    6379              : 
    6380       154795 :   RB (lang->has_complex_dflt);
    6381       154795 :   RB (lang->has_list_ctor);
    6382       154795 :   RB (lang->non_std_layout);
    6383       154795 :   RB (lang->is_literal);
    6384       154795 :   RB (lang->lazy_move_ctor);
    6385       154795 :   RB (lang->lazy_move_assign);
    6386       154795 :   RB (lang->has_complex_move_ctor);
    6387       154795 :   RB (lang->has_complex_move_assign);
    6388              : 
    6389       154795 :   RB (lang->has_constexpr_ctor);
    6390       154795 :   RB (lang->unique_obj_representations);
    6391       154795 :   RB (lang->unique_obj_representations_set);
    6392       154795 :   gcc_checking_assert (!lang->erroneous);
    6393       154795 :   RB (lang->non_pod_aggregate);
    6394       154795 :   RB (lang->non_aggregate_pod);
    6395              : #undef RB
    6396       154795 :   return !get_overrun ();
    6397              : }
    6398              : 
    6399              : /* Read & write the core values and pointers.  */
    6400              : 
    6401              : void
    6402     52267036 : trees_out::core_vals (tree t)
    6403              : {
    6404              : #define WU(X) (u (X))
    6405              : #define WT(X) (tree_node (X))
    6406     52267036 :   tree_code code = TREE_CODE (t);
    6407              : 
    6408              :   /* First by shape of the tree.  */
    6409              : 
    6410     52267036 :   if (CODE_CONTAINS_STRUCT (code, TS_DECL_MINIMAL))
    6411              :     {
    6412              :       /* Write this early, for better log information.  */
    6413     11466781 :       WT (t->decl_minimal.name);
    6414     11466781 :       if (!DECL_TEMPLATE_PARM_P (t))
    6415      8865571 :         WT (t->decl_minimal.context);
    6416              : 
    6417     11466781 :       if (state)
    6418      9437280 :         state->write_location (*this, t->decl_minimal.locus);
    6419              : 
    6420     11466781 :       if (streaming_p ())
    6421      4373874 :         if (has_warning_spec (t))
    6422          903 :           u (get_warning_spec (t));
    6423              :     }
    6424              : 
    6425     52267036 :   if (CODE_CONTAINS_STRUCT (code, TS_TYPE_COMMON))
    6426              :     {
    6427              :       /* The only types we write also have TYPE_NON_COMMON.  */
    6428      2825918 :       gcc_checking_assert (CODE_CONTAINS_STRUCT (code, TS_TYPE_NON_COMMON));
    6429              : 
    6430              :       /* We only stream the main variant.  */
    6431      2825918 :       gcc_checking_assert (TYPE_MAIN_VARIANT (t) == t);
    6432              : 
    6433              :       /* Stream the name & context first, for better log information  */
    6434      2825918 :       WT (t->type_common.name);
    6435      2825918 :       WT (t->type_common.context);
    6436              : 
    6437              :       /* By construction we want to make sure we have the canonical
    6438              :          and main variants already in the type table, so emit them
    6439              :          now.  */
    6440      2825918 :       WT (t->type_common.main_variant);
    6441              : 
    6442      2825918 :       tree canonical = t->type_common.canonical;
    6443      2825918 :       if (canonical && DECL_TEMPLATE_PARM_P (TYPE_NAME (t)))
    6444              :         /* We do not want to wander into different templates.
    6445              :            Reconstructed on stream in.  */
    6446              :         canonical = t;
    6447      2825918 :       WT (canonical);
    6448              : 
    6449              :       /* type_common.next_variant is internally manipulated.  */
    6450              :       /* type_common.pointer_to, type_common.reference_to.  */
    6451              : 
    6452      2825918 :       if (streaming_p ())
    6453              :         {
    6454       770790 :           WU (t->type_common.precision);
    6455       770790 :           WU (t->type_common.contains_placeholder_bits);
    6456       770790 :           WU (t->type_common.mode);
    6457       770790 :           WU (t->type_common.align);
    6458              :         }
    6459              : 
    6460      2825918 :       if (!RECORD_OR_UNION_CODE_P (code))
    6461              :         {
    6462      2344382 :           WT (t->type_common.size);
    6463      2344382 :           WT (t->type_common.size_unit);
    6464              :         }
    6465      2825918 :       WT (t->type_common.attributes);
    6466              : 
    6467      2825918 :       WT (t->type_common.common.chain); /* TYPE_STUB_DECL.  */
    6468              :     }
    6469              : 
    6470     52267036 :   if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
    6471              :     {
    6472     11466781 :       if (streaming_p ())
    6473              :         {
    6474      4373874 :           WU (t->decl_common.mode);
    6475      4373874 :           WU (t->decl_common.off_align);
    6476      4373874 :           WU (t->decl_common.align);
    6477              :         }
    6478              : 
    6479              :       /* For templates these hold instantiation (partial and/or
    6480              :          specialization) information.  */
    6481     11466781 :       if (code != TEMPLATE_DECL)
    6482              :         {
    6483     10600362 :           WT (t->decl_common.size);
    6484     10600362 :           WT (t->decl_common.size_unit);
    6485              :         }
    6486              : 
    6487     11466781 :       WT (t->decl_common.attributes);
    6488              :       // FIXME: Does this introduce cross-decl links?  For instance
    6489              :       // from instantiation to the template.  If so, we'll need more
    6490              :       // deduplication logic.  I think we'll need to walk the blocks
    6491              :       // of the owning function_decl's abstract origin in tandem, to
    6492              :       // generate the locating data needed?
    6493     11466781 :       WT (t->decl_common.abstract_origin);
    6494              :     }
    6495              : 
    6496     52267036 :   if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
    6497              :     {
    6498      5425781 :       WT (t->decl_with_vis.assembler_name);
    6499      5425781 :       if (streaming_p ())
    6500      2060756 :         WU (t->decl_with_vis.visibility);
    6501              :     }
    6502              : 
    6503     52267036 :   if (CODE_CONTAINS_STRUCT (code, TS_TYPE_NON_COMMON))
    6504              :     {
    6505      2825918 :       if (code == ENUMERAL_TYPE)
    6506              :         {
    6507              :           /* These fields get set even for opaque enums that lack a
    6508              :              definition, so we stream them directly for each ENUMERAL_TYPE.
    6509              :              We stream TYPE_VALUES as part of the definition.  */
    6510         9588 :           WT (t->type_non_common.maxval);
    6511         9588 :           WT (t->type_non_common.minval);
    6512              :         }
    6513              :       /* Records and unions hold FIELDS, VFIELD & BINFO on these
    6514              :          things.  */
    6515      2816330 :       else if (!RECORD_OR_UNION_CODE_P (code))
    6516              :         {
    6517              :           // FIXME: These are from tpl_parm_value's 'type' writing.
    6518              :           // Perhaps it should just be doing them directly?
    6519      2334794 :           gcc_checking_assert (code == TEMPLATE_TYPE_PARM
    6520              :                                || code == TEMPLATE_TEMPLATE_PARM
    6521              :                                || code == BOUND_TEMPLATE_TEMPLATE_PARM);
    6522      2334794 :           gcc_checking_assert (!TYPE_CACHED_VALUES_P (t));
    6523      2334794 :           WT (t->type_non_common.values);
    6524      2334794 :           WT (t->type_non_common.maxval);
    6525      2334794 :           WT (t->type_non_common.minval);
    6526              :         }
    6527              : 
    6528      2825918 :       WT (t->type_non_common.lang_1);
    6529              :     }
    6530              : 
    6531     52267036 :   if (CODE_CONTAINS_STRUCT (code, TS_EXP))
    6532              :     {
    6533     15981481 :       if (state)
    6534     15664990 :         state->write_location (*this, t->exp.locus);
    6535              : 
    6536     15981481 :       if (streaming_p ())
    6537      7759798 :         if (has_warning_spec (t))
    6538       612948 :           u (get_warning_spec (t));
    6539              : 
    6540     15981481 :       bool vl = TREE_CODE_CLASS (code) == tcc_vl_exp;
    6541     15981481 :       unsigned limit = (vl ? VL_EXP_OPERAND_LENGTH (t)
    6542     15981481 :                         : TREE_OPERAND_LENGTH (t));
    6543     15981481 :       unsigned ix = unsigned (vl);
    6544     15981481 :       if (code == REQUIRES_EXPR)
    6545              :         {
    6546              :           /* The first operand of a REQUIRES_EXPR is a tree chain
    6547              :              of PARM_DECLs.  We need to stream this separately as
    6548              :              otherwise we would only stream the first one.  */
    6549        23474 :           chained_decls (REQUIRES_EXPR_PARMS (t));
    6550        23474 :           ++ix;
    6551              :         }
    6552     44032048 :       for (; ix != limit; ix++)
    6553     28050567 :         WT (TREE_OPERAND (t, ix));
    6554              :     }
    6555              :   else
    6556              :     /* The CODE_CONTAINS tables were inaccurate when I started.  */
    6557     36285555 :     gcc_checking_assert (TREE_CODE_CLASS (code) != tcc_expression
    6558              :                          && TREE_CODE_CLASS (code) != tcc_binary
    6559              :                          && TREE_CODE_CLASS (code) != tcc_unary
    6560              :                          && TREE_CODE_CLASS (code) != tcc_reference
    6561              :                          && TREE_CODE_CLASS (code) != tcc_comparison
    6562              :                          && TREE_CODE_CLASS (code) != tcc_statement
    6563              :                          && TREE_CODE_CLASS (code) != tcc_vl_exp);
    6564              : 
    6565              :   /* Then by CODE.  Special cases and/or 1:1 tree shape
    6566              :      correspondence. */
    6567     52267036 :   switch (code)
    6568              :     {
    6569              :     default:
    6570              :       break;
    6571              : 
    6572            0 :     case ARGUMENT_PACK_SELECT:  /* Transient during instantiation.  */
    6573            0 :     case DEFERRED_PARSE:        /* Expanded upon completion of
    6574              :                                    outermost class.  */
    6575            0 :     case IDENTIFIER_NODE:       /* Streamed specially.  */
    6576            0 :     case BINDING_VECTOR:                /* Only in namespace-scope symbol
    6577              :                                    table.  */
    6578            0 :     case SSA_NAME:
    6579            0 :     case TRANSLATION_UNIT_DECL: /* There is only one, it is a
    6580              :                                    global_tree.  */
    6581            0 :     case USERDEF_LITERAL:       /* Expanded during parsing.  */
    6582            0 :       gcc_unreachable (); /* Should never meet.  */
    6583              : 
    6584              :       /* Constants.  */
    6585           18 :     case COMPLEX_CST:
    6586           18 :       WT (TREE_REALPART (t));
    6587           18 :       WT (TREE_IMAGPART (t));
    6588           18 :       break;
    6589              : 
    6590            0 :     case FIXED_CST:
    6591            0 :       gcc_unreachable (); /* Not supported in C++.  */
    6592              : 
    6593      4788930 :     case INTEGER_CST:
    6594      4788930 :       if (streaming_p ())
    6595              :         {
    6596       813245 :           unsigned num = TREE_INT_CST_EXT_NUNITS (t);
    6597      1629359 :           for (unsigned ix = 0; ix != num; ix++)
    6598       816114 :             wu (TREE_INT_CST_ELT (t, ix));
    6599              :         }
    6600              :       break;
    6601              : 
    6602            0 :     case POLY_INT_CST:
    6603            0 :       if (streaming_p ())
    6604            0 :         for (unsigned ix = 0; ix != NUM_POLY_INT_COEFFS; ix++)
    6605            0 :           WT (POLY_INT_CST_COEFF (t, ix));
    6606              :       break;
    6607              : 
    6608        46588 :     case REAL_CST:
    6609        46588 :       if (streaming_p ())
    6610        23248 :         buf (TREE_REAL_CST_PTR (t), sizeof (real_value));
    6611              :       break;
    6612              : 
    6613              :     case STRING_CST:
    6614              :       /* Streamed during start.  */
    6615              :       break;
    6616              : 
    6617           36 :     case RAW_DATA_CST:
    6618           36 :       if (RAW_DATA_OWNER (t) == NULL_TREE)
    6619              :         break; /* Streamed as STRING_CST during start.  */
    6620           24 :       WT (RAW_DATA_OWNER (t));
    6621           24 :       if (streaming_p ())
    6622              :         {
    6623           12 :           if (TREE_CODE (RAW_DATA_OWNER (t)) == RAW_DATA_CST)
    6624            6 :             z (RAW_DATA_POINTER (t) - RAW_DATA_POINTER (RAW_DATA_OWNER (t)));
    6625            6 :           else if (TREE_CODE (RAW_DATA_OWNER (t)) == STRING_CST)
    6626            6 :             z (RAW_DATA_POINTER (t)
    6627            6 :                - TREE_STRING_POINTER (RAW_DATA_OWNER (t)));
    6628              :           else
    6629            0 :             gcc_unreachable ();
    6630              :         }
    6631              :       break;
    6632              : 
    6633           36 :     case VECTOR_CST:
    6634          102 :       for (unsigned ix = vector_cst_encoded_nelts (t); ix--;)
    6635           66 :         WT (VECTOR_CST_ENCODED_ELT (t, ix));
    6636              :       break;
    6637              : 
    6638              :       /* Decls.  */
    6639       641801 :     case VAR_DECL:
    6640       641801 :       if (DECL_CONTEXT (t)
    6641       641801 :           && TREE_CODE (DECL_CONTEXT (t)) != FUNCTION_DECL)
    6642              :         {
    6643       133853 :           if (DECL_HAS_VALUE_EXPR_P (t))
    6644           18 :             WT (DECL_VALUE_EXPR (t));
    6645              :           break;
    6646              :         }
    6647              :       /* FALLTHROUGH  */
    6648              : 
    6649      5234885 :     case RESULT_DECL:
    6650      5234885 :     case PARM_DECL:
    6651      5234885 :       if (DECL_HAS_VALUE_EXPR_P (t))
    6652        50268 :         WT (DECL_VALUE_EXPR (t));
    6653              :       /* FALLTHROUGH  */
    6654              : 
    6655      5454111 :     case CONST_DECL:
    6656      5454111 :     case IMPORTED_DECL:
    6657      5454111 :       WT (t->decl_common.initial);
    6658      5454111 :       break;
    6659              : 
    6660       159807 :     case FIELD_DECL:
    6661       159807 :       WT (t->field_decl.offset);
    6662       159807 :       WT (t->field_decl.bit_field_type);
    6663       159807 :       {
    6664       159807 :         auto ovr = make_temp_override (walking_bit_field_unit, true);
    6665       159807 :         WT (t->field_decl.qualifier); /* bitfield unit.  */
    6666       159807 :       }
    6667       159807 :       WT (t->field_decl.bit_offset);
    6668       159807 :       WT (t->field_decl.fcontext);
    6669       159807 :       WT (t->decl_common.initial);
    6670       159807 :       break;
    6671              : 
    6672        53119 :     case LABEL_DECL:
    6673        53119 :       if (streaming_p ())
    6674              :         {
    6675        26558 :           WU (t->label_decl.label_decl_uid);
    6676        26558 :           WU (t->label_decl.eh_landing_pad_nr);
    6677              :         }
    6678              :       break;
    6679              : 
    6680      1210932 :     case FUNCTION_DECL:
    6681      1210932 :       if (streaming_p ())
    6682              :         {
    6683              :           /* Builtins can be streamed by value when a header declares
    6684              :              them.  */
    6685       605359 :           WU (DECL_BUILT_IN_CLASS (t));
    6686       605359 :           if (DECL_BUILT_IN_CLASS (t) != NOT_BUILT_IN)
    6687        11766 :             WU (DECL_UNCHECKED_FUNCTION_CODE (t));
    6688              :         }
    6689              : 
    6690      1210932 :       WT (t->function_decl.personality);
    6691              :       /* Rather than streaming target/optimize nodes, we should reconstruct
    6692              :          them on stream-in from any attributes applied to the function.  */
    6693      1210932 :       if (streaming_p () && t->function_decl.function_specific_target)
    6694            0 :         warning_at (DECL_SOURCE_LOCATION (t), 0,
    6695              :                     "%<target%> attribute currently unsupported in modules");
    6696      1210932 :       if (streaming_p () && t->function_decl.function_specific_optimization)
    6697            3 :         warning_at (DECL_SOURCE_LOCATION (t), 0,
    6698              :                     "%<optimize%> attribute currently unsupported in modules");
    6699      1210932 :       WT (t->function_decl.vindex);
    6700              : 
    6701      1210932 :       if (DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (t))
    6702         8192 :         WT (lookup_explicit_specifier (t));
    6703              :       break;
    6704              : 
    6705       143759 :     case USING_DECL:
    6706              :       /* USING_DECL_DECLS  */
    6707       143759 :       WT (t->decl_common.initial);
    6708              :       /* FALLTHROUGH  */
    6709              : 
    6710      3572561 :     case TYPE_DECL:
    6711              :       /* USING_DECL: USING_DECL_SCOPE  */
    6712              :       /* TYPE_DECL: DECL_ORIGINAL_TYPE */
    6713      3572561 :       WT (t->decl_non_common.result);
    6714      3572561 :       break;
    6715              : 
    6716              :       /* Miscellaneous common nodes.  */
    6717       798604 :     case BLOCK:
    6718       798604 :       if (state)
    6719              :         {
    6720       798604 :           state->write_location (*this, t->block.locus);
    6721       798604 :           state->write_location (*this, t->block.end_locus);
    6722              :         }
    6723              : 
    6724              :       /* DECL_LOCAL_DECL_P decls are first encountered here and
    6725              :          streamed by value.  */
    6726      1219856 :       for (tree decls = t->block.vars; decls; decls = DECL_CHAIN (decls))
    6727              :         {
    6728       421252 :           if (VAR_OR_FUNCTION_DECL_P (decls)
    6729       421252 :               && DECL_LOCAL_DECL_P (decls))
    6730              :             {
    6731              :               /* Make sure this is the first encounter, and mark for
    6732              :                  walk-by-value.  */
    6733          312 :               gcc_checking_assert (!TREE_VISITED (decls)
    6734              :                                    && !DECL_TEMPLATE_INFO (decls));
    6735          312 :               mark_by_value (decls);
    6736              :             }
    6737       421252 :           tree_node (decls);
    6738              :         }
    6739       798604 :       tree_node (NULL_TREE);
    6740              : 
    6741              :       /* nonlocalized_vars is a middle-end thing.  */
    6742       798604 :       WT (t->block.subblocks);
    6743       798604 :       WT (t->block.supercontext);
    6744              :       // FIXME: As for decl's abstract_origin, does this introduce crosslinks?
    6745       798604 :       WT (t->block.abstract_origin);
    6746              :       /* fragment_origin, fragment_chain are middle-end things.  */
    6747       798604 :       WT (t->block.chain);
    6748              :       /* nonlocalized_vars, block_num & die are middle endy/debug
    6749              :          things.  */
    6750       798604 :       break;
    6751              : 
    6752      1582796 :     case CALL_EXPR:
    6753      1582796 :       if (streaming_p ())
    6754       757577 :         WU (t->base.u.ifn);
    6755              :       break;
    6756              : 
    6757              :     case CONSTRUCTOR:
    6758              :       // This must be streamed /after/ we've streamed the type,
    6759              :       // because it can directly refer to elements of the type. Eg,
    6760              :       // FIELD_DECLs of a RECORD_TYPE.
    6761              :       break;
    6762              : 
    6763           36 :     case OMP_CLAUSE:
    6764           36 :       {
    6765              :         /* The ompcode is serialized in start.  */
    6766           36 :         if (streaming_p ())
    6767           18 :           WU (t->omp_clause.subcode.map_kind);
    6768           36 :         if (state)
    6769           36 :           state->write_location (*this, t->omp_clause.locus);
    6770              : 
    6771           36 :         unsigned len = omp_clause_num_ops[OMP_CLAUSE_CODE (t)];
    6772          120 :         for (unsigned ix = 0; ix != len; ix++)
    6773           84 :           WT (t->omp_clause.ops[ix]);
    6774              :       }
    6775              :       break;
    6776              : 
    6777       661155 :     case STATEMENT_LIST:
    6778      2753050 :       for (tree stmt : tsi_range (t))
    6779      2091895 :         if (stmt)
    6780      2091895 :           WT (stmt);
    6781       661155 :       WT (NULL_TREE);
    6782       661155 :       break;
    6783              : 
    6784            0 :     case OPTIMIZATION_NODE:
    6785            0 :     case TARGET_OPTION_NODE:
    6786              :       // FIXME: Our representation for these two nodes is a cache of
    6787              :       // the resulting set of options.  Not a record of the options
    6788              :       // that got changed by a particular attribute or pragma.  Instead
    6789              :       // of recording that, we probably should just rebuild the options
    6790              :       // on stream-in from the function attributes.  This could introduce
    6791              :       // strangeness if the importer has some incompatible set of flags
    6792              :       // but we currently assume users "know what they're doing" in such
    6793              :       // a case anyway.
    6794            0 :       gcc_unreachable ();
    6795       272930 :       break;
    6796              : 
    6797       272930 :     case TREE_BINFO:
    6798       272930 :       {
    6799       272930 :         WT (t->binfo.common.chain);
    6800       272930 :         WT (t->binfo.offset);
    6801       272930 :         WT (t->binfo.inheritance);
    6802       272930 :         WT (t->binfo.vptr_field);
    6803              : 
    6804       272930 :         WT (t->binfo.vtable);
    6805       272930 :         WT (t->binfo.virtuals);
    6806       272930 :         WT (t->binfo.vtt_subvtt);
    6807       272930 :         WT (t->binfo.vtt_vptr);
    6808              : 
    6809       272930 :         tree_vec (BINFO_BASE_ACCESSES (t));
    6810       272930 :         unsigned num = vec_safe_length (BINFO_BASE_ACCESSES (t));
    6811       348644 :         for (unsigned ix = 0; ix != num; ix++)
    6812        75714 :           WT (BINFO_BASE_BINFO (t, ix));
    6813              :       }
    6814              :       break;
    6815              : 
    6816      3960784 :     case TREE_LIST:
    6817      3960784 :       WT (t->list.purpose);
    6818      3960784 :       WT (t->list.value);
    6819      3960784 :       WT (t->list.common.chain);
    6820      3960784 :       break;
    6821              : 
    6822      3382232 :     case TREE_VEC:
    6823      9384083 :       for (unsigned ix = TREE_VEC_LENGTH (t); ix--;)
    6824      6001851 :         WT (TREE_VEC_ELT (t, ix));
    6825              :       /* We stash NON_DEFAULT_TEMPLATE_ARGS_COUNT on TREE_CHAIN!  */
    6826      3382232 :       gcc_checking_assert (!t->type_common.common.chain
    6827              :                            || (TREE_CODE (t->type_common.common.chain)
    6828              :                                == INTEGER_CST));
    6829      3382232 :       WT (t->type_common.common.chain);
    6830      3382232 :       break;
    6831              : 
    6832              :       /* C++-specific nodes ...  */
    6833       277885 :     case BASELINK:
    6834       277885 :       WT (((lang_tree_node *)t)->baselink.binfo);
    6835       277885 :       WT (((lang_tree_node *)t)->baselink.functions);
    6836       277885 :       WT (((lang_tree_node *)t)->baselink.access_binfo);
    6837       277885 :       WT (((lang_tree_node *)t)->baselink.common.chain);
    6838       277885 :       break;
    6839              : 
    6840       123474 :     case CONSTRAINT_INFO:
    6841       123474 :       WT (((lang_tree_node *)t)->constraint_info.template_reqs);
    6842       123474 :       WT (((lang_tree_node *)t)->constraint_info.declarator_reqs);
    6843       123474 :       WT (((lang_tree_node *)t)->constraint_info.associated_constr);
    6844       123474 :       break;
    6845              : 
    6846        19604 :     case DEFERRED_NOEXCEPT:
    6847        19604 :       WT (((lang_tree_node *)t)->deferred_noexcept.pattern);
    6848        19604 :       WT (((lang_tree_node *)t)->deferred_noexcept.args);
    6849        19604 :       break;
    6850              : 
    6851        19001 :     case LAMBDA_EXPR:
    6852        19001 :       WT (((lang_tree_node *)t)->lambda_expression.capture_list);
    6853        19001 :       WT (((lang_tree_node *)t)->lambda_expression.this_capture);
    6854        19001 :       WT (((lang_tree_node *)t)->lambda_expression.extra_scope);
    6855        19001 :       WT (((lang_tree_node *)t)->lambda_expression.regen_info);
    6856        19001 :       WT (((lang_tree_node *)t)->lambda_expression.extra_args);
    6857              :       /* pending_proxies is a parse-time thing.  */
    6858        19001 :       gcc_assert (!((lang_tree_node *)t)->lambda_expression.pending_proxies);
    6859        19001 :       if (state)
    6860        18998 :         state->write_location
    6861        18998 :           (*this, ((lang_tree_node *)t)->lambda_expression.locus);
    6862        19001 :       if (streaming_p ())
    6863              :         {
    6864         6379 :           WU (((lang_tree_node *)t)->lambda_expression.default_capture_mode);
    6865         6379 :           WU (((lang_tree_node *)t)->lambda_expression.discriminator_scope);
    6866         6379 :           WU (((lang_tree_node *)t)->lambda_expression.discriminator_sig);
    6867              :         }
    6868              :       break;
    6869              : 
    6870      2854260 :     case OVERLOAD:
    6871      2854260 :       WT (((lang_tree_node *)t)->overload.function);
    6872      2854260 :       WT (t->common.chain);
    6873      2854260 :       break;
    6874              : 
    6875           33 :     case PTRMEM_CST:
    6876           33 :       WT (((lang_tree_node *)t)->ptrmem.member);
    6877           33 :       if (state)
    6878           24 :         state->write_location (*this, ((lang_tree_node *)t)->ptrmem.locus);
    6879              :       break;
    6880              : 
    6881        21505 :     case STATIC_ASSERT:
    6882        21505 :       WT (((lang_tree_node *)t)->static_assertion.condition);
    6883        21505 :       WT (((lang_tree_node *)t)->static_assertion.message);
    6884        21505 :       if (state)
    6885        21505 :         state->write_location
    6886        21505 :           (*this, ((lang_tree_node *)t)->static_assertion.location);
    6887              :       break;
    6888              : 
    6889       866419 :     case TEMPLATE_DECL:
    6890              :       /* Streamed with the template_decl node itself.  */
    6891       866419 :       gcc_checking_assert
    6892              :         (TREE_VISITED (((lang_tree_node *)t)->template_decl.arguments));
    6893       866419 :       gcc_checking_assert
    6894              :         (TREE_VISITED (((lang_tree_node *)t)->template_decl.result));
    6895       866419 :       if (DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (t))
    6896        16496 :         WT (DECL_CHAIN (t));
    6897              :       break;
    6898              : 
    6899      1965775 :     case TEMPLATE_INFO:
    6900      1965775 :       {
    6901      1965775 :         WT (((lang_tree_node *)t)->template_info.tmpl);
    6902      1965775 :         WT (((lang_tree_node *)t)->template_info.args);
    6903      1965775 :         WT (((lang_tree_node *)t)->template_info.partial);
    6904              : 
    6905      1965775 :         const auto *ac = (((lang_tree_node *)t)
    6906              :                           ->template_info.deferred_access_checks);
    6907      1965775 :         unsigned len = vec_safe_length (ac);
    6908      1965775 :         if (streaming_p ())
    6909       980776 :           u (len);
    6910      1965775 :         if (len)
    6911              :           {
    6912            0 :             for (unsigned ix = 0; ix != len; ix++)
    6913              :               {
    6914            0 :                 const auto &m = (*ac)[ix];
    6915            0 :                 WT (m.binfo);
    6916            0 :                 WT (m.decl);
    6917            0 :                 WT (m.diag_decl);
    6918            0 :                 if (state)
    6919            0 :                   state->write_location (*this, m.loc);
    6920              :               }
    6921              :           }
    6922              :       }
    6923              :       break;
    6924              : 
    6925      2506716 :     case TEMPLATE_PARM_INDEX:
    6926      2506716 :       if (streaming_p ())
    6927              :         {
    6928       557490 :           WU (((lang_tree_node *)t)->tpi.index);
    6929       557490 :           WU (((lang_tree_node *)t)->tpi.level);
    6930       557490 :           WU (((lang_tree_node *)t)->tpi.orig_level);
    6931              :         }
    6932      2506716 :       WT (((lang_tree_node *)t)->tpi.decl);
    6933              :       /* TEMPLATE_PARM_DESCENDANTS (AKA TREE_CHAIN) is an internal
    6934              :          cache, do not stream.  */
    6935      2506716 :       break;
    6936              : 
    6937        29901 :     case TRAIT_EXPR:
    6938        29901 :       WT (((lang_tree_node *)t)->trait_expression.type1);
    6939        29901 :       WT (((lang_tree_node *)t)->trait_expression.type2);
    6940        29901 :       if (state)
    6941        23364 :         state->write_location
    6942        23364 :           (*this, ((lang_tree_node *)t)->trait_expression.locus);
    6943        29901 :       if (streaming_p ())
    6944        11512 :         WU (((lang_tree_node *)t)->trait_expression.kind);
    6945              :       break;
    6946              : 
    6947            4 :     case TU_LOCAL_ENTITY:
    6948            4 :       WT (((lang_tree_node *)t)->tu_local_entity.name);
    6949            4 :       if (state)
    6950            4 :         state->write_location
    6951            4 :           (*this, ((lang_tree_node *)t)->tu_local_entity.loc);
    6952              :       break;
    6953              :     }
    6954              : 
    6955     52267036 :   if (CODE_CONTAINS_STRUCT (code, TS_TYPED))
    6956              :     {
    6957              :       /* We want to stream the type of a expression-like nodes /after/
    6958              :          we've streamed the operands.  The type often contains (bits
    6959              :          of the) types of the operands, and with things like decltype
    6960              :          and noexcept in play, we really want to stream the decls
    6961              :          defining the type before we try and stream the type on its
    6962              :          own.  Otherwise we can find ourselves trying to read in a
    6963              :          decl, when we're already partially reading in a component of
    6964              :          its type.  And that's bad.  */
    6965     49338070 :       tree type = t->typed.type;
    6966     49338070 :       unsigned prec = 0;
    6967              : 
    6968     49338070 :       switch (code)
    6969              :         {
    6970              :         default:
    6971              :           break;
    6972              : 
    6973              :         case TEMPLATE_DECL:
    6974              :           /* We fill in the template's type separately.  */
    6975     49338070 :           type = NULL_TREE;
    6976              :           break;
    6977              : 
    6978      3428802 :         case TYPE_DECL:
    6979      3428802 :           if (DECL_ORIGINAL_TYPE (t) && t == TYPE_NAME (type))
    6980              :             /* This is a typedef.  We set its type separately.  */
    6981              :             type = NULL_TREE;
    6982              :           break;
    6983              : 
    6984         9588 :         case ENUMERAL_TYPE:
    6985         9588 :           if (type && !ENUM_FIXED_UNDERLYING_TYPE_P (t))
    6986              :             {
    6987              :               /* Type is a restricted range integer type derived from the
    6988              :                  integer_types.  Find the right one.  */
    6989         5666 :               prec = TYPE_PRECISION (type);
    6990         5666 :               tree name = DECL_NAME (TYPE_NAME (type));
    6991              : 
    6992        74034 :               for (unsigned itk = itk_none; itk--;)
    6993        74034 :                 if (integer_types[itk]
    6994        74034 :                     && DECL_NAME (TYPE_NAME (integer_types[itk])) == name)
    6995              :                   {
    6996              :                     type = integer_types[itk];
    6997              :                     break;
    6998              :                   }
    6999         5666 :               gcc_assert (type != t->typed.type);
    7000              :             }
    7001              :           break;
    7002              :         }
    7003              : 
    7004     49338070 :       WT (type);
    7005     49338070 :       if (prec && streaming_p ())
    7006         2831 :         WU (prec);
    7007              :     }
    7008              : 
    7009     52267036 :   if (TREE_CODE (t) == CONSTRUCTOR)
    7010              :     {
    7011       134926 :       unsigned len = vec_safe_length (t->constructor.elts);
    7012       134926 :       if (streaming_p ())
    7013        66464 :         WU (len);
    7014       134926 :       if (len)
    7015       489234 :         for (unsigned ix = 0; ix != len; ix++)
    7016              :           {
    7017       417796 :             const constructor_elt &elt = (*t->constructor.elts)[ix];
    7018              : 
    7019       417796 :             WT (elt.index);
    7020       417796 :             WT (elt.value);
    7021              :           }
    7022              :     }
    7023              : 
    7024              : #undef WT
    7025              : #undef WU
    7026     52267036 : }
    7027              : 
    7028              : // Streaming in a reference to a decl can cause that decl to be
    7029              : // TREE_USED, which is the mark_used behaviour we need most of the
    7030              : // time.  The trees_in::unused can be incremented to inhibit this,
    7031              : // which is at least needed for vtables.
    7032              : 
    7033              : bool
    7034     15605896 : trees_in::core_vals (tree t)
    7035              : {
    7036              : #define RU(X) ((X) = u ())
    7037              : #define RUC(T,X) ((X) = T (u ()))
    7038              : #define RT(X) ((X) = tree_node ())
    7039              : #define RTU(X) ((X) = tree_node (true))
    7040     15605896 :   tree_code code = TREE_CODE (t);
    7041              : 
    7042              :   /* First by tree shape.  */
    7043     15605896 :   if (CODE_CONTAINS_STRUCT (code, TS_DECL_MINIMAL))
    7044              :     {
    7045      3332460 :       RT (t->decl_minimal.name);
    7046      3332460 :       if (!DECL_TEMPLATE_PARM_P (t))
    7047      2901316 :         RT (t->decl_minimal.context);
    7048              : 
    7049              :       /* Don't zap the locus just yet, we don't record it correctly
    7050              :          and thus lose all location information.  */
    7051      3332460 :       t->decl_minimal.locus = state->read_location (*this);
    7052      3332460 :       if (has_warning_spec (t))
    7053          653 :         put_warning_spec (t, u ());
    7054              :     }
    7055              : 
    7056     15605896 :   if (CODE_CONTAINS_STRUCT (code, TS_TYPE_COMMON))
    7057              :     {
    7058       566259 :       RT (t->type_common.name);
    7059       566259 :       RT (t->type_common.context);
    7060              : 
    7061       566259 :       RT (t->type_common.main_variant);
    7062       566259 :       RT (t->type_common.canonical);
    7063              : 
    7064              :       /* type_common.next_variant is internally manipulated.  */
    7065              :       /* type_common.pointer_to, type_common.reference_to.  */
    7066              : 
    7067       566259 :       RU (t->type_common.precision);
    7068       566259 :       RU (t->type_common.contains_placeholder_bits);
    7069       566259 :       RUC (machine_mode, t->type_common.mode);
    7070       566259 :       RU (t->type_common.align);
    7071              : 
    7072       566259 :       if (!RECORD_OR_UNION_CODE_P (code))
    7073              :         {
    7074       391274 :           RT (t->type_common.size);
    7075       391274 :           RT (t->type_common.size_unit);
    7076              :         }
    7077       566259 :       RT (t->type_common.attributes);
    7078              : 
    7079       566259 :       RT (t->type_common.common.chain); /* TYPE_STUB_DECL.  */
    7080              :     }
    7081              : 
    7082     15605896 :   if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
    7083              :     {
    7084      3332460 :       RUC (machine_mode, t->decl_common.mode);
    7085      3332460 :       RU (t->decl_common.off_align);
    7086      3332460 :       RU (t->decl_common.align);
    7087              : 
    7088      3332460 :       if (code != TEMPLATE_DECL)
    7089              :         {
    7090      2999766 :           RT (t->decl_common.size);
    7091      2999766 :           RT (t->decl_common.size_unit);
    7092              :         }
    7093              : 
    7094      3332460 :       RT (t->decl_common.attributes);
    7095      3332460 :       RT (t->decl_common.abstract_origin);
    7096              :     }
    7097              : 
    7098     15605896 :   if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
    7099              :     {
    7100      1534334 :       RT (t->decl_with_vis.assembler_name);
    7101      1534334 :       RUC (symbol_visibility, t->decl_with_vis.visibility);
    7102              :     }
    7103              : 
    7104     15605896 :   if (CODE_CONTAINS_STRUCT (code, TS_TYPE_NON_COMMON))
    7105              :     {
    7106       566259 :       if (code == ENUMERAL_TYPE)
    7107              :         {
    7108              :           /* These fields get set even for opaque enums that lack a
    7109              :              definition, so we stream them directly for each ENUMERAL_TYPE.
    7110              :              We stream TYPE_VALUES as part of the definition.  */
    7111         3235 :           RT (t->type_non_common.maxval);
    7112         3235 :           RT (t->type_non_common.minval);
    7113              :         }
    7114              :       /* Records and unions hold FIELDS, VFIELD & BINFO on these
    7115              :          things.  */
    7116       563024 :       else if (!RECORD_OR_UNION_CODE_P (code))
    7117              :         {
    7118              :           /* This is not clobbering TYPE_CACHED_VALUES, because this
    7119              :              is a type that doesn't have any.  */
    7120       388039 :           gcc_checking_assert (!TYPE_CACHED_VALUES_P (t));
    7121       388039 :           RT (t->type_non_common.values);
    7122       388039 :           RT (t->type_non_common.maxval);
    7123       388039 :           RT (t->type_non_common.minval);
    7124              :         }
    7125              : 
    7126       566259 :       RT (t->type_non_common.lang_1);
    7127              :     }
    7128              : 
    7129     15605896 :   if (CODE_CONTAINS_STRUCT (code, TS_EXP))
    7130              :     {
    7131      6174553 :       t->exp.locus = state->read_location (*this);
    7132      6174553 :       if (has_warning_spec (t))
    7133       486129 :         put_warning_spec (t, u ());
    7134              : 
    7135      6174553 :       bool vl = TREE_CODE_CLASS (code) == tcc_vl_exp;
    7136      6174553 :       unsigned limit = (vl ? VL_EXP_OPERAND_LENGTH (t)
    7137      6174553 :                         : TREE_OPERAND_LENGTH (t));
    7138      6174553 :       unsigned ix = unsigned (vl);
    7139      6174553 :       if (code == REQUIRES_EXPR)
    7140              :         {
    7141         7711 :           REQUIRES_EXPR_PARMS (t) = chained_decls ();
    7142         7711 :           ++ix;
    7143              :         }
    7144     16898953 :       for (; ix != limit; ix++)
    7145     10724400 :         RTU (TREE_OPERAND (t, ix));
    7146              :     }
    7147              : 
    7148              :   /* Then by CODE.  Special cases and/or 1:1 tree shape
    7149              :      correspondence. */
    7150     15605896 :   switch (code)
    7151              :     {
    7152              :     default:
    7153              :       break;
    7154              : 
    7155              :     case ARGUMENT_PACK_SELECT:
    7156              :     case DEFERRED_PARSE:
    7157              :     case IDENTIFIER_NODE:
    7158              :     case BINDING_VECTOR:
    7159              :     case SSA_NAME:
    7160              :     case TRANSLATION_UNIT_DECL:
    7161              :     case USERDEF_LITERAL:
    7162              :       return false; /* Should never meet.  */
    7163              : 
    7164              :       /* Constants.  */
    7165            9 :     case COMPLEX_CST:
    7166            9 :       RT (TREE_REALPART (t));
    7167            9 :       RT (TREE_IMAGPART (t));
    7168            9 :       break;
    7169              : 
    7170              :     case FIXED_CST:
    7171              :       /* Not supported in C++.  */
    7172              :       return false;
    7173              : 
    7174       602137 :     case INTEGER_CST:
    7175       602137 :       {
    7176       602137 :         unsigned num = TREE_INT_CST_EXT_NUNITS (t);
    7177      1206453 :         for (unsigned ix = 0; ix != num; ix++)
    7178       604316 :           TREE_INT_CST_ELT (t, ix) = wu ();
    7179              :       }
    7180              :       break;
    7181              : 
    7182              :     case POLY_INT_CST:
    7183            0 :       for (unsigned ix = 0; ix != NUM_POLY_INT_COEFFS; ix++)
    7184            0 :         RT (POLY_INT_CST_COEFF (t, ix));
    7185              :       break;
    7186              : 
    7187        16774 :     case REAL_CST:
    7188        16774 :       if (const void *bytes = buf (sizeof (real_value)))
    7189        16774 :         memcpy (TREE_REAL_CST_PTR (t), bytes, sizeof (real_value));
    7190              :       break;
    7191              : 
    7192              :     case STRING_CST:
    7193              :       /* Streamed during start.  */
    7194              :       break;
    7195              : 
    7196            6 :     case RAW_DATA_CST:
    7197            6 :       RT (RAW_DATA_OWNER (t));
    7198            6 :       gcc_assert (TREE_CODE (RAW_DATA_OWNER (t)) == STRING_CST
    7199              :                   && TREE_STRING_LENGTH (RAW_DATA_OWNER (t)));
    7200            6 :       RAW_DATA_POINTER (t) = TREE_STRING_POINTER (RAW_DATA_OWNER (t)) + z ();
    7201            6 :       break;
    7202              : 
    7203           24 :     case VECTOR_CST:
    7204           63 :       for (unsigned ix = vector_cst_encoded_nelts (t); ix--;)
    7205           39 :         RT (VECTOR_CST_ENCODED_ELT (t, ix));
    7206              :       break;
    7207              : 
    7208              :       /* Decls.  */
    7209       228339 :     case VAR_DECL:
    7210       228339 :       if (DECL_CONTEXT (t)
    7211       228339 :           && TREE_CODE (DECL_CONTEXT (t)) != FUNCTION_DECL)
    7212              :         {
    7213        42489 :           if (DECL_HAS_VALUE_EXPR_P (t))
    7214              :             {
    7215            9 :               tree val = tree_node ();
    7216            9 :               SET_DECL_VALUE_EXPR (t, val);
    7217              :             }
    7218              :           break;
    7219              :         }
    7220              :       /* FALLTHROUGH  */
    7221              : 
    7222      1524200 :     case RESULT_DECL:
    7223      1524200 :     case PARM_DECL:
    7224      1524200 :       if (DECL_HAS_VALUE_EXPR_P (t))
    7225              :         {
    7226              :           /* The DECL_VALUE hash table is a cache, thus if we're
    7227              :              reading a duplicate (which we end up discarding), the
    7228              :              value expr will also be cleaned up at the next gc.  */
    7229        14339 :           tree val = tree_node ();
    7230        14339 :           SET_DECL_VALUE_EXPR (t, val);
    7231              :         }
    7232              :       /* FALLTHROUGH  */
    7233              : 
    7234      1569837 :     case CONST_DECL:
    7235      1569837 :     case IMPORTED_DECL:
    7236      1569837 :       RT (t->decl_common.initial);
    7237      1569837 :       break;
    7238              : 
    7239        56799 :     case FIELD_DECL:
    7240        56799 :       RT (t->field_decl.offset);
    7241        56799 :       RT (t->field_decl.bit_field_type);
    7242        56799 :       RT (t->field_decl.qualifier);
    7243        56799 :       RT (t->field_decl.bit_offset);
    7244        56799 :       RT (t->field_decl.fcontext);
    7245        56799 :       RT (t->decl_common.initial);
    7246        56799 :       break;
    7247              : 
    7248        18303 :     case LABEL_DECL:
    7249        18303 :       RU (t->label_decl.label_decl_uid);
    7250        18303 :       RU (t->label_decl.eh_landing_pad_nr);
    7251        18303 :       break;
    7252              : 
    7253       470536 :     case FUNCTION_DECL:
    7254       470536 :       {
    7255       470536 :         unsigned bltin = u ();
    7256       470536 :         t->function_decl.built_in_class = built_in_class (bltin);
    7257       470536 :         if (bltin != NOT_BUILT_IN)
    7258              :           {
    7259         9575 :             bltin = u ();
    7260         9575 :             DECL_UNCHECKED_FUNCTION_CODE (t) = built_in_function (bltin);
    7261              :           }
    7262              : 
    7263       470536 :         RT (t->function_decl.personality);
    7264              :         /* These properties are not streamed, and should be reconstructed
    7265              :            from any function attributes.  */
    7266              :         // t->function_decl.function_specific_target);
    7267              :         // t->function_decl.function_specific_optimization);
    7268       470536 :         RT (t->function_decl.vindex);
    7269              : 
    7270       470536 :         if (DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (t))
    7271              :           {
    7272         3830 :             tree spec;
    7273         3830 :             RT (spec);
    7274         3830 :             store_explicit_specifier (t, spec);
    7275              :           }
    7276              :       }
    7277              :       break;
    7278              : 
    7279        51935 :     case USING_DECL:
    7280              :       /* USING_DECL_DECLS  */
    7281        51935 :       RT (t->decl_common.initial);
    7282              :       /* FALLTHROUGH  */
    7283              : 
    7284       835268 :     case TYPE_DECL:
    7285              :       /* USING_DECL: USING_DECL_SCOPE  */
    7286              :       /* TYPE_DECL: DECL_ORIGINAL_TYPE */
    7287       835268 :       RT (t->decl_non_common.result);
    7288       835268 :       break;
    7289              : 
    7290              :       /* Miscellaneous common nodes.  */
    7291       316370 :     case BLOCK:
    7292       316370 :       t->block.locus = state->read_location (*this);
    7293       316370 :       t->block.end_locus = state->read_location (*this);
    7294              : 
    7295       316370 :       for (tree *chain = &t->block.vars;;)
    7296       476523 :         if (tree decl = tree_node ())
    7297              :           {
    7298              :             /* For a deduplicated local type or enumerator, chain the
    7299              :                duplicate decl instead of the canonical in-TU decl.  Seeing
    7300              :                a duplicate here means the containing function whose body
    7301              :                we're streaming in is a duplicate too, so we'll end up
    7302              :                discarding this BLOCK (and the rest of the duplicate function
    7303              :                body) anyway.  */
    7304       160153 :             decl = maybe_duplicate (decl);
    7305              : 
    7306       160153 :             if (!DECL_P (decl))
    7307              :               {
    7308            0 :                 set_overrun ();
    7309            0 :                 break;
    7310              :               }
    7311              : 
    7312              :             /* If DECL_CHAIN is already set then this was a backreference to a
    7313              :                local type or enumerator from a previous read (PR c++/114630).
    7314              :                Let's copy the node so we can keep building the chain for ODR
    7315              :                checking later.  */
    7316       160153 :             if (DECL_CHAIN (decl))
    7317              :               {
    7318           12 :                 gcc_checking_assert (TREE_CODE (decl) == TYPE_DECL
    7319              :                                      && find_duplicate (DECL_CONTEXT (decl)));
    7320            6 :                 decl = copy_decl (decl);
    7321              :               }
    7322              : 
    7323       160153 :             *chain = decl;
    7324       160153 :             chain = &DECL_CHAIN (decl);
    7325              :           }
    7326              :         else
    7327       160153 :           break;
    7328              : 
    7329              :       /* nonlocalized_vars is middle-end.  */
    7330       316370 :       RT (t->block.subblocks);
    7331       316370 :       RT (t->block.supercontext);
    7332       316370 :       RT (t->block.abstract_origin);
    7333              :       /* fragment_origin, fragment_chain are middle-end.  */
    7334       316370 :       RT (t->block.chain);
    7335              :       /* nonlocalized_vars, block_num, die are middle endy/debug
    7336              :          things.  */
    7337       316370 :       break;
    7338              : 
    7339       596597 :     case CALL_EXPR:
    7340       596597 :       RUC (internal_fn, t->base.u.ifn);
    7341       596597 :       break;
    7342              : 
    7343              :     case CONSTRUCTOR:
    7344              :       // Streamed after the node's type.
    7345              :       break;
    7346              : 
    7347           18 :     case OMP_CLAUSE:
    7348           18 :       {
    7349           18 :         RU (t->omp_clause.subcode.map_kind);
    7350           18 :         t->omp_clause.locus = state->read_location (*this);
    7351              : 
    7352           18 :         unsigned len = omp_clause_num_ops[OMP_CLAUSE_CODE (t)];
    7353           66 :         for (unsigned ix = 0; ix != len; ix++)
    7354           48 :           RT (t->omp_clause.ops[ix]);
    7355              :       }
    7356              :       break;
    7357              : 
    7358       282844 :     case STATEMENT_LIST:
    7359       282844 :       {
    7360       282844 :         tree_stmt_iterator iter = tsi_start (t);
    7361      1172537 :         for (tree stmt; RT (stmt);)
    7362              :           {
    7363       889693 :             if (TREE_CODE (stmt) == DEBUG_BEGIN_STMT
    7364       168380 :                 && !MAY_HAVE_DEBUG_MARKER_STMTS)
    7365            0 :               continue;
    7366       889693 :             tsi_link_after (&iter, stmt, TSI_CONTINUE_LINKING);
    7367              :           }
    7368              :       }
    7369       282844 :       break;
    7370              : 
    7371            0 :     case OPTIMIZATION_NODE:
    7372            0 :     case TARGET_OPTION_NODE:
    7373              :       /* Not implemented, see trees_out::core_vals.  */
    7374            0 :       gcc_unreachable ();
    7375        92023 :       break;
    7376              : 
    7377        92023 :     case TREE_BINFO:
    7378        92023 :       RT (t->binfo.common.chain);
    7379        92023 :       RT (t->binfo.offset);
    7380        92023 :       RT (t->binfo.inheritance);
    7381        92023 :       RT (t->binfo.vptr_field);
    7382              : 
    7383              :       /* Do not mark the vtables as USED in the address expressions
    7384              :          here.  */
    7385        92023 :       unused++;
    7386        92023 :       RT (t->binfo.vtable);
    7387        92023 :       RT (t->binfo.virtuals);
    7388        92023 :       RT (t->binfo.vtt_subvtt);
    7389        92023 :       RT (t->binfo.vtt_vptr);
    7390        92023 :       unused--;
    7391              : 
    7392        92023 :       BINFO_BASE_ACCESSES (t) = tree_vec ();
    7393        92023 :       if (!get_overrun ())
    7394              :         {
    7395        92023 :           unsigned num = vec_safe_length (BINFO_BASE_ACCESSES (t));
    7396       117160 :           for (unsigned ix = 0; ix != num; ix++)
    7397        25137 :             BINFO_BASE_APPEND (t, tree_node ());
    7398              :         }
    7399              :       break;
    7400              : 
    7401      1342331 :     case TREE_LIST:
    7402      1342331 :       RT (t->list.purpose);
    7403      1342331 :       RT (t->list.value);
    7404      1342331 :       RT (t->list.common.chain);
    7405      1342331 :       break;
    7406              : 
    7407       859213 :     case TREE_VEC:
    7408      2310690 :       for (unsigned ix = TREE_VEC_LENGTH (t); ix--;)
    7409      1451477 :         RT (TREE_VEC_ELT (t, ix));
    7410       859213 :       RT (t->type_common.common.chain);
    7411       859213 :       break;
    7412              : 
    7413              :       /* C++-specific nodes ...  */
    7414       109912 :     case BASELINK:
    7415       109912 :       RT (((lang_tree_node *)t)->baselink.binfo);
    7416       109912 :       RTU (((lang_tree_node *)t)->baselink.functions);
    7417       109912 :       RT (((lang_tree_node *)t)->baselink.access_binfo);
    7418       109912 :       RT (((lang_tree_node *)t)->baselink.common.chain);
    7419       109912 :       break;
    7420              : 
    7421        45220 :     case CONSTRAINT_INFO:
    7422        45220 :       RT (((lang_tree_node *)t)->constraint_info.template_reqs);
    7423        45220 :       RT (((lang_tree_node *)t)->constraint_info.declarator_reqs);
    7424        45220 :       RT (((lang_tree_node *)t)->constraint_info.associated_constr);
    7425        45220 :       break;
    7426              : 
    7427         7635 :     case DEFERRED_NOEXCEPT:
    7428         7635 :       RT (((lang_tree_node *)t)->deferred_noexcept.pattern);
    7429         7635 :       RT (((lang_tree_node *)t)->deferred_noexcept.args);
    7430         7635 :       break;
    7431              : 
    7432         4120 :     case LAMBDA_EXPR:
    7433         4120 :       RT (((lang_tree_node *)t)->lambda_expression.capture_list);
    7434         4120 :       RT (((lang_tree_node *)t)->lambda_expression.this_capture);
    7435         4120 :       RT (((lang_tree_node *)t)->lambda_expression.extra_scope);
    7436         4120 :       RT (((lang_tree_node *)t)->lambda_expression.regen_info);
    7437         4120 :       RT (((lang_tree_node *)t)->lambda_expression.extra_args);
    7438              :       /* lambda_expression.pending_proxies is NULL  */
    7439         4120 :       ((lang_tree_node *)t)->lambda_expression.locus
    7440         4120 :         = state->read_location (*this);
    7441         4120 :       RUC (cp_lambda_default_capture_mode_type,
    7442              :            ((lang_tree_node *)t)->lambda_expression.default_capture_mode);
    7443         4120 :       RU (((lang_tree_node *)t)->lambda_expression.discriminator_scope);
    7444         4120 :       RU (((lang_tree_node *)t)->lambda_expression.discriminator_sig);
    7445         4120 :       break;
    7446              : 
    7447       576820 :     case OVERLOAD:
    7448       576820 :       RT (((lang_tree_node *)t)->overload.function);
    7449       576820 :       RT (t->common.chain);
    7450       576820 :       break;
    7451              : 
    7452           12 :     case PTRMEM_CST:
    7453           12 :       RTU (((lang_tree_node *)t)->ptrmem.member);
    7454           12 :       ((lang_tree_node *)t)->ptrmem.locus = state->read_location (*this);
    7455           12 :       break;
    7456              : 
    7457         8213 :     case STATIC_ASSERT:
    7458         8213 :       RT (((lang_tree_node *)t)->static_assertion.condition);
    7459         8213 :       RT (((lang_tree_node *)t)->static_assertion.message);
    7460         8213 :       ((lang_tree_node *)t)->static_assertion.location
    7461         8213 :         = state->read_location (*this);
    7462         8213 :       break;
    7463              : 
    7464       332694 :     case TEMPLATE_DECL:
    7465              :       /* Streamed when reading the raw template decl itself.  */
    7466       332694 :       gcc_assert (((lang_tree_node *)t)->template_decl.arguments);
    7467       332694 :       gcc_assert (((lang_tree_node *)t)->template_decl.result);
    7468       332694 :       if (DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (t))
    7469         7162 :         RT (DECL_CHAIN (t));
    7470              :       break;
    7471              : 
    7472       742236 :     case TEMPLATE_INFO:
    7473       742236 :       RT (((lang_tree_node *)t)->template_info.tmpl);
    7474       742236 :       RT (((lang_tree_node *)t)->template_info.args);
    7475       742236 :       RT (((lang_tree_node *)t)->template_info.partial);
    7476       742236 :       if (unsigned len = u ())
    7477              :         {
    7478            0 :           auto &ac = (((lang_tree_node *)t)
    7479              :                       ->template_info.deferred_access_checks);
    7480            0 :           vec_alloc (ac, len);
    7481            0 :           for (unsigned ix = 0; ix != len; ix++)
    7482              :             {
    7483            0 :               deferred_access_check m;
    7484              : 
    7485            0 :               RT (m.binfo);
    7486            0 :               RT (m.decl);
    7487            0 :               RT (m.diag_decl);
    7488            0 :               m.loc = state->read_location (*this);
    7489            0 :               ac->quick_push (m);
    7490              :             }
    7491              :         }
    7492              :       break;
    7493              : 
    7494       413232 :     case TEMPLATE_PARM_INDEX:
    7495       413232 :       RU (((lang_tree_node *)t)->tpi.index);
    7496       413232 :       RU (((lang_tree_node *)t)->tpi.level);
    7497       413232 :       RU (((lang_tree_node *)t)->tpi.orig_level);
    7498       413232 :       RT (((lang_tree_node *)t)->tpi.decl);
    7499       413232 :       break;
    7500              : 
    7501         8493 :     case TRAIT_EXPR:
    7502         8493 :       RT (((lang_tree_node *)t)->trait_expression.type1);
    7503         8493 :       RT (((lang_tree_node *)t)->trait_expression.type2);
    7504         8493 :       ((lang_tree_node *)t)->trait_expression.locus
    7505         8493 :         = state->read_location (*this);
    7506         8493 :       RUC (cp_trait_kind, ((lang_tree_node *)t)->trait_expression.kind);
    7507         8493 :       break;
    7508              : 
    7509            2 :     case TU_LOCAL_ENTITY:
    7510            2 :       RT (((lang_tree_node *)t)->tu_local_entity.name);
    7511            2 :       ((lang_tree_node *)t)->tu_local_entity.loc
    7512            2 :         = state->read_location (*this);
    7513              :     }
    7514              : 
    7515     15605896 :   if (CODE_CONTAINS_STRUCT (code, TS_TYPED))
    7516              :     {
    7517     14486220 :       tree type = tree_node ();
    7518              : 
    7519     14486220 :       if (type && code == ENUMERAL_TYPE && !ENUM_FIXED_UNDERLYING_TYPE_P (t))
    7520              :         {
    7521         1764 :           unsigned precision = u ();
    7522              : 
    7523         1764 :           type = build_distinct_type_copy (type);
    7524         1764 :           TYPE_PRECISION (type) = precision;
    7525         3528 :           set_min_and_max_values_for_integral_type (type, precision,
    7526         1764 :                                                     TYPE_SIGN (type));
    7527              :         }
    7528              : 
    7529     14486220 :       if (code != TEMPLATE_DECL)
    7530     14153526 :         t->typed.type = type;
    7531              :     }
    7532              : 
    7533     15605896 :   if (TREE_CODE (t) == CONSTRUCTOR)
    7534        54172 :     if (unsigned len = u ())
    7535              :       {
    7536        32672 :         vec_alloc (t->constructor.elts, len);
    7537       213234 :         for (unsigned ix = 0; ix != len; ix++)
    7538              :           {
    7539       180562 :             constructor_elt elt;
    7540              : 
    7541       180562 :             RT (elt.index);
    7542       180562 :             RTU (elt.value);
    7543       180562 :             t->constructor.elts->quick_push (elt);
    7544              :           }
    7545              :       }
    7546              : 
    7547              : #undef RT
    7548              : #undef RM
    7549              : #undef RU
    7550     15605896 :   return !get_overrun ();
    7551              : }
    7552              : 
    7553              : void
    7554      6183518 : trees_out::lang_decl_vals (tree t)
    7555              : {
    7556      6183518 :   const struct lang_decl *lang = DECL_LANG_SPECIFIC (t);
    7557              : #define WU(X) (u (X))
    7558              : #define WT(X) (tree_node (X))
    7559              :   /* Module index already written.  */
    7560      6183518 :   switch (lang->u.base.selector)
    7561              :     {
    7562            0 :     default:
    7563            0 :       gcc_unreachable ();
    7564              : 
    7565      1210932 :     case lds_fn:  /* lang_decl_fn.  */
    7566      1210932 :       if (streaming_p ())
    7567              :         {
    7568       605359 :           if (DECL_NAME (t) && IDENTIFIER_OVL_OP_P (DECL_NAME (t)))
    7569       103830 :             WU (lang->u.fn.ovl_op_code);
    7570              :         }
    7571              : 
    7572      1508092 :       if (DECL_CLASS_SCOPE_P (t) || DECL_UNIQUE_FRIEND_P (t))
    7573       944980 :         WT (lang->u.fn.context);
    7574              : 
    7575      1210932 :       if (lang->u.fn.thunk_p)
    7576              :         {
    7577              :           /* The thunked-to function.  */
    7578         1736 :           WT (lang->u.fn.befriending_classes);
    7579         1736 :           if (streaming_p ())
    7580          868 :             wi (lang->u.fn.u5.fixed_offset);
    7581              :         }
    7582      1209196 :       else if (decl_tls_wrapper_p (t))
    7583              :         /* The wrapped variable.  */
    7584           18 :         WT (lang->u.fn.befriending_classes);
    7585              :       else
    7586      1209178 :         WT (lang->u.fn.u5.cloned_function);
    7587              : 
    7588      1210932 :       if (FNDECL_USED_AUTO (t))
    7589         4963 :         WT (lang->u.fn.u.saved_auto_return_type);
    7590              : 
    7591      1210932 :       goto lds_min;
    7592              : 
    7593         6432 :     case lds_decomp:  /* lang_decl_decomp.  */
    7594         6432 :       WT (lang->u.decomp.base);
    7595         6432 :       goto lds_min;
    7596              : 
    7597      3558742 :     case lds_min:  /* lang_decl_min.  */
    7598      3558742 :     lds_min:
    7599      3558742 :       WT (lang->u.min.template_info);
    7600      3558742 :       {
    7601      3558742 :         tree access = lang->u.min.access;
    7602              : 
    7603              :         /* DECL_ACCESS needs to be maintained by the definition of the
    7604              :            (derived) class that changes the access.  The other users
    7605              :            of DECL_ACCESS need to write it here.  */
    7606      1210932 :         if (!DECL_THUNK_P (t)
    7607      4767938 :             && (DECL_CONTEXT (t) && TYPE_P (DECL_CONTEXT (t))))
    7608              :           access = NULL_TREE;
    7609              : 
    7610      3558742 :         WT (access);
    7611              :       }
    7612              :       /* A friend template specialisation stashes its owning class on its
    7613              :          DECL_CHAIN; we need to reconstruct this, but it needs to happen
    7614              :          after we stream the template_info so readers can know this is such
    7615              :          an entity.  */
    7616      3558742 :       if (decl_specialization_friend_p (t))
    7617          120 :         WT (t->common.chain);
    7618              :       break;
    7619              : 
    7620              :     case lds_ns:  /* lang_decl_ns.  */
    7621              :       break;
    7622              : 
    7623      2624453 :     case lds_parm:  /* lang_decl_parm.  */
    7624      2624453 :       if (streaming_p ())
    7625              :         {
    7626       892880 :           WU (lang->u.parm.level);
    7627       892880 :           WU (lang->u.parm.index);
    7628              :         }
    7629              :       break;
    7630              :     }
    7631              : #undef WU
    7632              : #undef WT
    7633      6183518 : }
    7634              : 
    7635              : bool
    7636      2087622 : trees_in::lang_decl_vals (tree t)
    7637              : {
    7638      2087622 :   struct lang_decl *lang = DECL_LANG_SPECIFIC (t);
    7639              : #define RU(X) ((X) = u ())
    7640              : #define RT(X) ((X) = tree_node ())
    7641              : 
    7642              :   /* Module index already read.  */
    7643      2087622 :   switch (lang->u.base.selector)
    7644              :     {
    7645            0 :     default:
    7646            0 :       gcc_unreachable ();
    7647              : 
    7648       470536 :     case lds_fn:  /* lang_decl_fn.  */
    7649       470536 :       if (DECL_NAME (t) && IDENTIFIER_OVL_OP_P (DECL_NAME (t)))
    7650              :         {
    7651        80978 :           unsigned code = u ();
    7652              : 
    7653              :           /* Check consistency.  */
    7654        80978 :           if (code >= OVL_OP_MAX
    7655        80978 :               || (ovl_op_info[IDENTIFIER_ASSIGN_OP_P (DECL_NAME (t))][code]
    7656        80978 :                   .ovl_op_code) == OVL_OP_ERROR_MARK)
    7657            0 :             set_overrun ();
    7658              :           else
    7659        80978 :             lang->u.fn.ovl_op_code = code;
    7660              :         }
    7661              : 
    7662       582566 :       if (DECL_CLASS_SCOPE_P (t) || DECL_UNIQUE_FRIEND_P (t))
    7663       372020 :         RT (lang->u.fn.context);
    7664              : 
    7665       470536 :       if (lang->u.fn.thunk_p)
    7666              :         {
    7667          574 :           RT (lang->u.fn.befriending_classes);
    7668          574 :           lang->u.fn.u5.fixed_offset = wi ();
    7669              :         }
    7670       469962 :       else if (decl_tls_wrapper_p (t))
    7671           15 :         RT (lang->u.fn.befriending_classes);
    7672              :       else
    7673       469947 :         RT (lang->u.fn.u5.cloned_function);
    7674              : 
    7675       470536 :       if (FNDECL_USED_AUTO (t))
    7676         1483 :         RT (lang->u.fn.u.saved_auto_return_type);
    7677       470536 :       goto lds_min;
    7678              : 
    7679         3043 :     case lds_decomp:  /* lang_decl_decomp.  */
    7680         3043 :       RT (lang->u.decomp.base);
    7681         3043 :       goto lds_min;
    7682              : 
    7683      1396264 :     case lds_min:  /* lang_decl_min.  */
    7684      1396264 :     lds_min:
    7685      1396264 :       RT (lang->u.min.template_info);
    7686      1396264 :       RT (lang->u.min.access);
    7687      1396264 :       if (decl_specialization_friend_p (t))
    7688           51 :         RT (t->common.chain);
    7689              :       break;
    7690              : 
    7691              :     case lds_ns:  /* lang_decl_ns.  */
    7692              :       break;
    7693              : 
    7694       691203 :     case lds_parm:  /* lang_decl_parm.  */
    7695       691203 :       RU (lang->u.parm.level);
    7696       691203 :       RU (lang->u.parm.index);
    7697       691203 :       break;
    7698              :     }
    7699              : #undef RU
    7700              : #undef RT
    7701      2087622 :   return !get_overrun ();
    7702              : }
    7703              : 
    7704              : /* Most of the value contents of lang_type is streamed in
    7705              :    define_class.  */
    7706              : 
    7707              : void
    7708       421320 : trees_out::lang_type_vals (tree t)
    7709              : {
    7710       421320 :   const struct lang_type *lang = TYPE_LANG_SPECIFIC (t);
    7711              : #define WU(X) (u (X))
    7712              : #define WT(X) (tree_node (X))
    7713       421320 :   if (streaming_p ())
    7714       210637 :     WU (lang->align);
    7715              : #undef WU
    7716              : #undef WT
    7717       421320 : }
    7718              : 
    7719              : bool
    7720       154795 : trees_in::lang_type_vals (tree t)
    7721              : {
    7722       154795 :   struct lang_type *lang = TYPE_LANG_SPECIFIC (t);
    7723              : #define RU(X) ((X) = u ())
    7724              : #define RT(X) ((X) = tree_node ())
    7725       154795 :   RU (lang->align);
    7726              : #undef RU
    7727              : #undef RT
    7728       154795 :   return !get_overrun ();
    7729              : }
    7730              : 
    7731              : /* Write out the bools of T, including information about any
    7732              :    LANG_SPECIFIC information.  Including allocation of any lang
    7733              :    specific object.  */
    7734              : 
    7735              : void
    7736     20163618 : trees_out::tree_node_bools (tree t)
    7737              : {
    7738     20163618 :   gcc_checking_assert (streaming_p ());
    7739              : 
    7740              :   /* We should never stream a namespace.  */
    7741     20163618 :   gcc_checking_assert (TREE_CODE (t) != NAMESPACE_DECL
    7742              :                        || DECL_NAMESPACE_ALIAS (t));
    7743              : 
    7744     20163618 :   bits_out bits = stream_bits ();
    7745     20163618 :   core_bools (t, bits);
    7746              : 
    7747     20163618 :   switch (TREE_CODE_CLASS (TREE_CODE (t)))
    7748              :     {
    7749      4373874 :     case tcc_declaration:
    7750      4373874 :       {
    7751      4373874 :         bool specific = DECL_LANG_SPECIFIC (t) != NULL;
    7752      4373874 :         bits.b (specific);
    7753      4373874 :         if (specific && VAR_P (t))
    7754       366069 :           bits.b (DECL_DECOMPOSITION_P (t));
    7755      2662530 :         if (specific)
    7756      2662530 :           lang_decl_bools (t, bits);
    7757              :       }
    7758              :       break;
    7759              : 
    7760       805761 :     case tcc_type:
    7761       805761 :       {
    7762       805761 :         bool specific = (TYPE_MAIN_VARIANT (t) == t
    7763       805761 :                          && TYPE_LANG_SPECIFIC (t) != NULL);
    7764       805761 :         gcc_assert (TYPE_LANG_SPECIFIC (t)
    7765              :                     == TYPE_LANG_SPECIFIC (TYPE_MAIN_VARIANT (t)));
    7766              : 
    7767       805761 :         bits.b (specific);
    7768       805761 :         if (specific)
    7769       210637 :           lang_type_bools (t, bits);
    7770              :       }
    7771              :       break;
    7772              : 
    7773              :     default:
    7774              :       break;
    7775              :     }
    7776              : 
    7777     20163618 :   bits.bflush ();
    7778     20163618 : }
    7779              : 
    7780              : bool
    7781     15627742 : trees_in::tree_node_bools (tree t)
    7782              : {
    7783     15627742 :   bits_in bits = stream_bits ();
    7784     15627742 :   bool ok = core_bools (t, bits);
    7785              : 
    7786     15627742 :   if (ok)
    7787     15627742 :     switch (TREE_CODE_CLASS (TREE_CODE (t)))
    7788              :       {
    7789      3332460 :       case tcc_declaration:
    7790      3332460 :         if (bits.b ())
    7791              :           {
    7792      2087622 :             bool decomp = VAR_P (t) && bits.b ();
    7793              : 
    7794      2087622 :             ok = maybe_add_lang_decl_raw (t, decomp);
    7795      2087622 :             if (ok)
    7796      2087622 :               ok = lang_decl_bools (t, bits);
    7797              :           }
    7798              :         break;
    7799              : 
    7800       588105 :       case tcc_type:
    7801       588105 :         if (bits.b ())
    7802              :           {
    7803       154795 :             ok = maybe_add_lang_type_raw (t);
    7804       154795 :             if (ok)
    7805       154795 :               ok = lang_type_bools (t, bits);
    7806              :           }
    7807              :         break;
    7808              : 
    7809              :       default:
    7810              :         break;
    7811              :       }
    7812              : 
    7813     15627742 :   bits.bflush ();
    7814     15627742 :   if (!ok || get_overrun ())
    7815            0 :     return false;
    7816              : 
    7817              :   return true;
    7818     15627742 : }
    7819              : 
    7820              : 
    7821              : /* Write out the lang-specific vals of node T.  */
    7822              : 
    7823              : void
    7824     52267036 : trees_out::lang_vals (tree t)
    7825              : {
    7826     52267036 :   switch (TREE_CODE_CLASS (TREE_CODE (t)))
    7827              :     {
    7828     11466781 :     case tcc_declaration:
    7829     11466781 :       if (DECL_LANG_SPECIFIC (t))
    7830      6183518 :         lang_decl_vals (t);
    7831              :       break;
    7832              : 
    7833      2825918 :     case tcc_type:
    7834      2825918 :       if (TYPE_MAIN_VARIANT (t) == t && TYPE_LANG_SPECIFIC (t))
    7835       421320 :         lang_type_vals (t);
    7836              :       break;
    7837              : 
    7838              :     default:
    7839              :       break;
    7840              :     }
    7841     52267036 : }
    7842              : 
    7843              : bool
    7844     15605896 : trees_in::lang_vals (tree t)
    7845              : {
    7846     15605896 :   bool ok = true;
    7847              : 
    7848     15605896 :   switch (TREE_CODE_CLASS (TREE_CODE (t)))
    7849              :     {
    7850      3332460 :     case tcc_declaration:
    7851      3332460 :       if (DECL_LANG_SPECIFIC (t))
    7852      2087622 :         ok = lang_decl_vals (t);
    7853              :       break;
    7854              : 
    7855       566259 :     case tcc_type:
    7856       566259 :       if (TYPE_LANG_SPECIFIC (t))
    7857       154795 :         ok = lang_type_vals (t);
    7858              :       else
    7859       411464 :         TYPE_LANG_SPECIFIC (t) = TYPE_LANG_SPECIFIC (TYPE_MAIN_VARIANT (t));
    7860              :       break;
    7861              : 
    7862              :     default:
    7863              :       break;
    7864              :     }
    7865              : 
    7866     15605896 :   return ok;
    7867              : }
    7868              : 
    7869              : /* Write out the value fields of node T.  */
    7870              : 
    7871              : void
    7872     52267036 : trees_out::tree_node_vals (tree t)
    7873              : {
    7874     52267036 :   core_vals (t);
    7875     52267036 :   lang_vals (t);
    7876     52267036 : }
    7877              : 
    7878              : bool
    7879     15605896 : trees_in::tree_node_vals (tree t)
    7880              : {
    7881     15605896 :   bool ok = core_vals (t);
    7882     15605896 :   if (ok)
    7883     15605896 :     ok = lang_vals (t);
    7884              : 
    7885     15605896 :   return ok;
    7886              : }
    7887              : 
    7888              : 
    7889              : /* If T is a back reference, fixed reference or NULL, write out its
    7890              :    code and return WK_none.  Otherwise return WK_value if we must write
    7891              :    by value, or WK_normal otherwise.  */
    7892              : 
    7893              : walk_kind
    7894    342136177 : trees_out::ref_node (tree t)
    7895              : {
    7896    342136177 :   if (!t)
    7897              :     {
    7898    137353448 :       if (streaming_p ())
    7899              :         {
    7900              :           /* NULL_TREE -> tt_null.  */
    7901     52261387 :           null_count++;
    7902     52261387 :           i (tt_null);
    7903              :         }
    7904    137353448 :       return WK_none;
    7905              :     }
    7906              : 
    7907    204782729 :   if (!TREE_VISITED (t))
    7908              :     return WK_normal;
    7909              : 
    7910              :   /* An already-visited tree.  It must be in the map.  */
    7911    119559905 :   int val = get_tag (t);
    7912              : 
    7913    119559905 :   if (val == tag_value)
    7914              :     /* An entry we should walk into.  */
    7915              :     return WK_value;
    7916              : 
    7917    117727170 :   const char *kind;
    7918              : 
    7919    117727170 :   if (val <= tag_backref)
    7920              :     {
    7921              :       /* Back reference -> -ve number  */
    7922     89765293 :       if (streaming_p ())
    7923     42506186 :         i (val);
    7924              :       kind = "backref";
    7925              :     }
    7926     27961877 :   else if (val >= tag_fixed)
    7927              :     {
    7928              :       /* Fixed reference -> tt_fixed */
    7929     27961877 :       val -= tag_fixed;
    7930     27961877 :       if (streaming_p ())
    7931      9836772 :         i (tt_fixed), u (val);
    7932              :       kind = "fixed";
    7933              :     }
    7934              : 
    7935    117727170 :   if (streaming_p ())
    7936              :     {
    7937     52342958 :       back_ref_count++;
    7938     52342958 :       dump (dumper::TREE)
    7939        14184 :         && dump ("Wrote %s:%d %C:%N%S", kind, val, TREE_CODE (t), t, t);
    7940              :     }
    7941              :   return WK_none;
    7942              : }
    7943              : 
    7944              : tree
    7945     32718902 : trees_in::back_ref (int tag)
    7946              : {
    7947     32718902 :   tree res = NULL_TREE;
    7948              : 
    7949     32718902 :   if (tag < 0 && unsigned (~tag) < back_refs.length ())
    7950     32718902 :     res = back_refs[~tag];
    7951              : 
    7952     32718902 :   if (!res
    7953              :       /* Checking TREE_CODE is a dereference, so we know this is not a
    7954              :          wild pointer.  Checking the code provides evidence we've not
    7955              :          corrupted something.  */
    7956     32718902 :       || TREE_CODE (res) >= MAX_TREE_CODES)
    7957            0 :     set_overrun ();
    7958              :   else
    7959     32733442 :     dump (dumper::TREE) && dump ("Read backref:%d found %C:%N%S", tag,
    7960              :                                  TREE_CODE (res), res, res);
    7961     32718902 :   return res;
    7962              : }
    7963              : 
    7964              : unsigned
    7965      4828964 : trees_out::add_indirect_tpl_parms (tree parms)
    7966              : {
    7967      4828964 :   unsigned len = 0;
    7968      9084036 :   for (; parms; parms = TREE_CHAIN (parms), len++)
    7969              :     {
    7970      5109302 :       if (TREE_VISITED (parms))
    7971              :         break;
    7972              : 
    7973      4255072 :       int tag = insert (parms);
    7974      4255072 :       if (streaming_p ())
    7975      4255399 :         dump (dumper::TREE)
    7976          153 :           && dump ("Indirect:%d template's parameter %u %C:%N",
    7977          153 :                    tag, len, TREE_CODE (parms), parms);
    7978              :     }
    7979              : 
    7980      4828964 :   if (streaming_p ())
    7981       803551 :     u (len);
    7982              : 
    7983      4828964 :   return len;
    7984              : }
    7985              : 
    7986              : unsigned
    7987       546485 : trees_in::add_indirect_tpl_parms (tree parms)
    7988              : {
    7989       546485 :   unsigned len = u ();
    7990       975133 :   for (unsigned ix = 0; ix != len; parms = TREE_CHAIN (parms), ix++)
    7991              :     {
    7992       428648 :       int tag = insert (parms);
    7993       429113 :       dump (dumper::TREE)
    7994          159 :         && dump ("Indirect:%d template's parameter %u %C:%N",
    7995          159 :                  tag, ix, TREE_CODE (parms), parms);
    7996              :     }
    7997              : 
    7998       546485 :   return len;
    7999              : }
    8000              : 
    8001              : /* We've just found DECL by name.  Insert nodes that come with it, but
    8002              :    cannot be found by name, so we'll not accidentally walk into them.  */
    8003              : 
    8004              : void
    8005     10693162 : trees_out::add_indirects (tree decl)
    8006              : {
    8007     10693162 :   unsigned count = 0;
    8008              : 
    8009              :   // FIXME:OPTIMIZATION We'll eventually want default fn parms of
    8010              :   // templates and perhaps default template parms too.  The former can
    8011              :   // be referenced from instantiations (as they are lazily
    8012              :   // instantiated).  Also (deferred?) exception specifications of
    8013              :   // templates.  See the note about PARM_DECLs in trees_out::decl_node.
    8014     10693162 :   tree inner = decl;
    8015     10693162 :   if (TREE_CODE (decl) == TEMPLATE_DECL)
    8016              :     {
    8017      4828964 :       count += add_indirect_tpl_parms (DECL_TEMPLATE_PARMS (decl));
    8018              : 
    8019      4828964 :       inner = DECL_TEMPLATE_RESULT (decl);
    8020      4828964 :       int tag = insert (inner);
    8021      4828964 :       if (streaming_p ())
    8022       803551 :         dump (dumper::TREE)
    8023          219 :           && dump ("Indirect:%d template's result %C:%N",
    8024          219 :                    tag, TREE_CODE (inner), inner);
    8025      4828964 :       count++;
    8026              :     }
    8027              : 
    8028     10693162 :   if (TREE_CODE (inner) == TYPE_DECL)
    8029              :     {
    8030              :       /* Make sure the type is in the map too.  Otherwise we get
    8031              :          different RECORD_TYPEs for the same type, and things go
    8032              :          south.  */
    8033      5643538 :       tree type = TREE_TYPE (inner);
    8034      5643538 :       gcc_checking_assert (DECL_ORIGINAL_TYPE (inner)
    8035              :                            || TYPE_NAME (type) == inner);
    8036      5643538 :       int tag = insert (type);
    8037      5643538 :       if (streaming_p ())
    8038       624478 :         dump (dumper::TREE) && dump ("Indirect:%d decl's type %C:%N", tag,
    8039          362 :                                      TREE_CODE (type), type);
    8040      5643538 :       count++;
    8041              :     }
    8042              : 
    8043     10693162 :   if (streaming_p ())
    8044              :     {
    8045      1622791 :       u (count);
    8046      1623320 :       dump (dumper::TREE) && dump ("Inserted %u indirects", count);
    8047              :     }
    8048     10693162 : }
    8049              : 
    8050              : bool
    8051      1100300 : trees_in::add_indirects (tree decl)
    8052              : {
    8053      1100300 :   unsigned count = 0;
    8054              : 
    8055      1100300 :   tree inner = decl;
    8056      1100300 :   if (TREE_CODE (inner) == TEMPLATE_DECL)
    8057              :     {
    8058       546485 :       count += add_indirect_tpl_parms (DECL_TEMPLATE_PARMS (decl));
    8059              : 
    8060       546485 :       inner = DECL_TEMPLATE_RESULT (decl);
    8061       546485 :       int tag = insert (inner);
    8062       546485 :       dump (dumper::TREE)
    8063          228 :         && dump ("Indirect:%d templates's result %C:%N", tag,
    8064          228 :                  TREE_CODE (inner), inner);
    8065       546485 :       count++;
    8066              :     }
    8067              : 
    8068      1100300 :   if (TREE_CODE (inner) == TYPE_DECL)
    8069              :     {
    8070       422895 :       tree type = TREE_TYPE (inner);
    8071       422895 :       gcc_checking_assert (DECL_ORIGINAL_TYPE (inner)
    8072              :                            || TYPE_NAME (type) == inner);
    8073       422895 :       int tag = insert (type);
    8074       422895 :       dump (dumper::TREE)
    8075          362 :         && dump ("Indirect:%d decl's type %C:%N", tag, TREE_CODE (type), type);
    8076       422895 :       count++;
    8077              :     }
    8078              : 
    8079      1100905 :   dump (dumper::TREE) && dump ("Inserted %u indirects", count);
    8080      1100300 :   return count == u ();
    8081              : }
    8082              : 
    8083              : /* Stream a template parameter.  There are 4.5 kinds of parameter:
    8084              :    a) Template - TEMPLATE_DECL->TYPE_DECL->TEMPLATE_TEMPLATE_PARM
    8085              :         TEMPLATE_TYPE_PARM_INDEX TPI
    8086              :    b) Type - TYPE_DECL->TEMPLATE_TYPE_PARM TEMPLATE_TYPE_PARM_INDEX TPI
    8087              :    c.1) NonTYPE - PARM_DECL DECL_INITIAL TPI We meet this first
    8088              :    c.2) NonTYPE - CONST_DECL DECL_INITIAL Same TPI
    8089              :    d) BoundTemplate - TYPE_DECL->BOUND_TEMPLATE_TEMPLATE_PARM
    8090              :        TEMPLATE_TYPE_PARM_INDEX->TPI
    8091              :        TEMPLATE_TEMPLATE_PARM_INFO->TEMPLATE_INFO
    8092              : 
    8093              :    All of these point to a TEMPLATE_PARM_INDEX, and #B also has a TEMPLATE_INFO
    8094              : */
    8095              : 
    8096              : void
    8097      2601210 : trees_out::tpl_parm_value (tree parm)
    8098              : {
    8099      2601210 :   gcc_checking_assert (DECL_P (parm) && DECL_TEMPLATE_PARM_P (parm));
    8100              : 
    8101      2601210 :   int parm_tag = insert (parm);
    8102      2601210 :   if (streaming_p ())
    8103              :     {
    8104       580889 :       i (tt_tpl_parm);
    8105       580889 :       dump (dumper::TREE) && dump ("Writing template parm:%d %C:%N",
    8106          114 :                                    parm_tag, TREE_CODE (parm), parm);
    8107       580889 :       start (parm);
    8108       580889 :       tree_node_bools (parm);
    8109              :     }
    8110              : 
    8111      2601210 :   tree inner = parm;
    8112      2601210 :   if (TREE_CODE (inner) == TEMPLATE_DECL)
    8113              :     {
    8114         6789 :       inner = DECL_TEMPLATE_RESULT (inner);
    8115         6789 :       int inner_tag = insert (inner);
    8116         6789 :       if (streaming_p ())
    8117              :         {
    8118         1788 :           dump (dumper::TREE) && dump ("Writing inner template parm:%d %C:%N",
    8119            0 :                                        inner_tag, TREE_CODE (inner), inner);
    8120         1788 :           start (inner);
    8121         1788 :           tree_node_bools (inner);
    8122              :         }
    8123              :     }
    8124              : 
    8125      2601210 :   tree type = NULL_TREE;
    8126      2601210 :   if (TREE_CODE (inner) == TYPE_DECL)
    8127              :     {
    8128      2334794 :       type = TREE_TYPE (inner);
    8129      2334794 :       int type_tag = insert (type);
    8130      2334794 :       if (streaming_p ())
    8131              :         {
    8132       525265 :           dump (dumper::TREE) && dump ("Writing template parm type:%d %C:%N",
    8133          108 :                                        type_tag, TREE_CODE (type), type);
    8134       525265 :           start (type);
    8135       525265 :           tree_node_bools (type);
    8136              :         }
    8137              :     }
    8138              : 
    8139      2601210 :   if (inner != parm)
    8140              :     {
    8141              :       /* This is a template-template parameter.  */
    8142         6789 :       unsigned tpl_levels = 0;
    8143         6789 :       tpl_header (parm, &tpl_levels);
    8144         6789 :       tpl_parms_fini (parm, tpl_levels);
    8145              :     }
    8146              : 
    8147      2601210 :   tree_node_vals (parm);
    8148      2601210 :   if (inner != parm)
    8149         6789 :     tree_node_vals (inner);
    8150      2601210 :   if (type)
    8151              :     {
    8152      2334794 :       tree_node_vals (type);
    8153      2334794 :       if (DECL_NAME (inner) == auto_identifier
    8154      2334794 :           || DECL_NAME (inner) == decltype_auto_identifier)
    8155              :         {
    8156              :           /* Placeholder auto.  */
    8157       107041 :           tree_node (DECL_INITIAL (inner));
    8158       107041 :           tree_node (DECL_SIZE_UNIT (inner));
    8159              :         }
    8160              :     }
    8161              : 
    8162      2601210 :   if (streaming_p ())
    8163       580889 :     dump (dumper::TREE) && dump ("Wrote template parm:%d %C:%N",
    8164          114 :                                  parm_tag, TREE_CODE (parm), parm);
    8165      2601210 : }
    8166              : 
    8167              : tree
    8168       431144 : trees_in::tpl_parm_value ()
    8169              : {
    8170       431144 :   tree parm = start ();
    8171       431144 :   if (!parm || !tree_node_bools (parm))
    8172            0 :     return NULL_TREE;
    8173              : 
    8174       431144 :   int parm_tag = insert (parm);
    8175       431144 :   dump (dumper::TREE) && dump ("Reading template parm:%d %C:%N",
    8176          198 :                                parm_tag, TREE_CODE (parm), parm);
    8177              : 
    8178       431144 :   tree inner = parm;
    8179       431144 :   if (TREE_CODE (inner) == TEMPLATE_DECL)
    8180              :     {
    8181         1229 :       inner = start ();
    8182         1229 :       if (!inner || !tree_node_bools (inner))
    8183            0 :         return NULL_TREE;
    8184         1229 :       int inner_tag = insert (inner);
    8185         1229 :       dump (dumper::TREE) && dump ("Reading inner template parm:%d %C:%N",
    8186            0 :                                    inner_tag, TREE_CODE (inner), inner);
    8187         1229 :       DECL_TEMPLATE_RESULT (parm) = inner;
    8188              :     }
    8189              : 
    8190       431144 :   tree type = NULL_TREE;
    8191       431144 :   if (TREE_CODE (inner) == TYPE_DECL)
    8192              :     {
    8193       388039 :       type = start ();
    8194       388039 :       if (!type || !tree_node_bools (type))
    8195            0 :         return NULL_TREE;
    8196       388039 :       int type_tag = insert (type);
    8197       388039 :       dump (dumper::TREE) && dump ("Reading template parm type:%d %C:%N",
    8198          120 :                                    type_tag, TREE_CODE (type), type);
    8199              : 
    8200       388039 :       TREE_TYPE (inner) = TREE_TYPE (parm) = type;
    8201       388039 :       TYPE_NAME (type) = parm;
    8202              :     }
    8203              : 
    8204       431144 :   if (inner != parm)
    8205              :     {
    8206              :       /* A template template parameter.  */
    8207         1229 :       unsigned tpl_levels = 0;
    8208         1229 :       tpl_header (parm, &tpl_levels);
    8209         1229 :       tpl_parms_fini (parm, tpl_levels);
    8210              :     }
    8211              : 
    8212       431144 :   tree_node_vals (parm);
    8213       431144 :   if (inner != parm)
    8214         1229 :     tree_node_vals (inner);
    8215       431144 :   if (type)
    8216              :     {
    8217       388039 :       tree_node_vals (type);
    8218       388039 :       if (DECL_NAME (inner) == auto_identifier
    8219       388039 :           || DECL_NAME (inner) == decltype_auto_identifier)
    8220              :         {
    8221              :           /* Placeholder auto.  */
    8222        33503 :           DECL_INITIAL (inner) = tree_node ();
    8223        33503 :           DECL_SIZE_UNIT (inner) = tree_node ();
    8224              :         }
    8225       388039 :       if (TYPE_CANONICAL (type))
    8226              :         {
    8227       388039 :           gcc_checking_assert (TYPE_CANONICAL (type) == type);
    8228       388039 :           TYPE_CANONICAL (type) = canonical_type_parameter (type);
    8229              :         }
    8230              :     }
    8231              : 
    8232       431144 :   dump (dumper::TREE) && dump ("Read template parm:%d %C:%N",
    8233          198 :                                parm_tag, TREE_CODE (parm), parm);
    8234              : 
    8235              :   return parm;
    8236              : }
    8237              : 
    8238              : void
    8239      1645506 : trees_out::install_entity (tree decl, depset *dep)
    8240              : {
    8241      1645506 :   gcc_checking_assert (streaming_p ());
    8242              : 
    8243              :   /* Write the entity index, so we can insert it as soon as we
    8244              :      know this is new.  */
    8245      1645506 :   u (dep ? dep->cluster + 1 : 0);
    8246      1645506 :   if (CHECKING_P && dep)
    8247              :     {
    8248              :       /* Add it to the entity map, such that we can tell it is
    8249              :          part of us.  */
    8250      1193553 :       bool existed;
    8251      1193553 :       unsigned *slot = &entity_map->get_or_insert
    8252      1193553 :         (DECL_UID (decl), &existed);
    8253      1193553 :       if (existed)
    8254              :         /* If it existed, it should match.  */
    8255         1630 :         gcc_checking_assert (decl == (*entity_ary)[*slot]);
    8256      1193553 :       *slot = ~dep->cluster;
    8257              :     }
    8258      1645506 : }
    8259              : 
    8260              : bool
    8261      1229031 : trees_in::install_entity (tree decl)
    8262              : {
    8263      1229031 :   unsigned entity_index = u ();
    8264      1229031 :   if (!entity_index)
    8265              :     return false;
    8266              : 
    8267       890665 :   if (entity_index > state->entity_num)
    8268              :     {
    8269            0 :       set_overrun ();
    8270            0 :       return false;
    8271              :     }
    8272              : 
    8273              :   /* Insert the real decl into the entity ary.  */
    8274       890665 :   unsigned ident = state->entity_lwm + entity_index - 1;
    8275       890665 :   (*entity_ary)[ident] = decl;
    8276              : 
    8277              :   /* And into the entity map, if it's not already there.  */
    8278       890665 :   tree not_tmpl = STRIP_TEMPLATE (decl);
    8279       890665 :   if (!DECL_LANG_SPECIFIC (not_tmpl)
    8280      1684988 :       || !DECL_MODULE_ENTITY_P (not_tmpl))
    8281              :     {
    8282              :       /* We don't want to use retrofit_lang_decl directly so that we aren't
    8283              :          affected by the language state when we load in.  */
    8284       889527 :       if (!DECL_LANG_SPECIFIC (not_tmpl))
    8285              :         {
    8286        96342 :           maybe_add_lang_decl_raw (not_tmpl, false);
    8287        96342 :           SET_DECL_LANGUAGE (not_tmpl, lang_cplusplus);
    8288              :         }
    8289       889527 :       DECL_MODULE_ENTITY_P (not_tmpl) = true;
    8290              : 
    8291              :       /* Insert into the entity hash (it cannot already be there).  */
    8292       889527 :       bool existed;
    8293       889527 :       unsigned &slot = entity_map->get_or_insert (DECL_UID (decl), &existed);
    8294       889527 :       gcc_checking_assert (!existed);
    8295       889527 :       slot = ident;
    8296              :     }
    8297              :   else
    8298              :     {
    8299         1138 :       unsigned *slot = entity_map->get (DECL_UID (decl));
    8300              : 
    8301              :       /* The entity must be in the entity map already.  However, DECL may
    8302              :          be the DECL_TEMPLATE_RESULT of an existing partial specialisation
    8303              :          if we matched it while streaming another instantiation; in this
    8304              :          case we already registered that TEMPLATE_DECL.  */
    8305         1138 :       if (!slot)
    8306              :         {
    8307            9 :           tree type = TREE_TYPE (decl);
    8308            9 :           gcc_checking_assert (TREE_CODE (decl) == TYPE_DECL
    8309              :                                && CLASS_TYPE_P (type)
    8310              :                                && CLASSTYPE_TEMPLATE_SPECIALIZATION (type));
    8311            9 :           slot = entity_map->get (DECL_UID (CLASSTYPE_TI_TEMPLATE (type)));
    8312              :         }
    8313            9 :       gcc_checking_assert (slot);
    8314              : 
    8315         1138 :       if (state->is_partition ())
    8316              :         {
    8317              :           /* The decl is already in the entity map, but we see it again now
    8318              :              from a partition: we want to overwrite if the original decl
    8319              :              wasn't also from a (possibly different) partition.  Otherwise,
    8320              :              for things like template instantiations, make_dependency might
    8321              :              not realise that this is also provided from a partition and
    8322              :              should be considered part of this module (and thus always
    8323              :              emitted into the primary interface's CMI).  */
    8324          432 :           module_state *imp = import_entity_module (*slot);
    8325          432 :           if (!imp->is_partition ())
    8326          297 :             *slot = ident;
    8327              :         }
    8328              :     }
    8329              : 
    8330              :   return true;
    8331              : }
    8332              : 
    8333              : static bool has_definition (tree decl);
    8334              : 
    8335              : /* DECL is a decl node that must be written by value.  DEP is the
    8336              :    decl's depset.  */
    8337              : 
    8338              : void
    8339      4507246 : trees_out::decl_value (tree decl, depset *dep)
    8340              : {
    8341              :   /* We should not be writing clones or template parms.  */
    8342      4507246 :   gcc_checking_assert (DECL_P (decl)
    8343              :                        && !DECL_CLONED_FUNCTION_P (decl)
    8344              :                        && !DECL_TEMPLATE_PARM_P (decl));
    8345              : 
    8346              :   /* We should never be writing non-typedef ptrmemfuncs by value.  */
    8347      4507246 :   gcc_checking_assert (TREE_CODE (decl) != TYPE_DECL
    8348              :                        || DECL_ORIGINAL_TYPE (decl)
    8349              :                        || !TYPE_PTRMEMFUNC_P (TREE_TYPE (decl)));
    8350              : 
    8351              :   /* There's no need to walk any of the contents of a known TU-local entity,
    8352              :      since importers should never see any of it regardless.  But make sure we
    8353              :      at least note its location so importers can use it for diagnostics.  */
    8354      4507246 :   if (dep && dep->is_tu_local ())
    8355              :     {
    8356          416 :       gcc_checking_assert (is_initial_scan ());
    8357          416 :       insert (decl, WK_value);
    8358          416 :       state->note_location (DECL_SOURCE_LOCATION (decl));
    8359          416 :       return;
    8360              :     }
    8361              : 
    8362      4506830 :   merge_kind mk = get_merge_kind (decl, dep);
    8363              : 
    8364      4506830 :   if (CHECKING_P)
    8365              :     {
    8366              :       /* Never start in the middle of a template.  */
    8367      4506830 :       int use_tpl = -1;
    8368      4506830 :       if (tree ti = node_template_info (decl, use_tpl))
    8369      1568174 :         gcc_checking_assert (TREE_CODE (TI_TEMPLATE (ti)) == OVERLOAD
    8370              :                              || TREE_CODE (TI_TEMPLATE (ti)) == FIELD_DECL
    8371              :                              || (DECL_TEMPLATE_RESULT (TI_TEMPLATE (ti))
    8372              :                                  != decl));
    8373              :     }
    8374              : 
    8375      4506830 :   if (streaming_p ())
    8376              :     {
    8377              :       /* A new node -> tt_decl.  */
    8378      1645506 :       decl_val_count++;
    8379      1645506 :       i (tt_decl);
    8380      1645506 :       u (mk);
    8381      1645506 :       start (decl);
    8382              : 
    8383      1645506 :       if (mk != MK_unique)
    8384              :         {
    8385      1357188 :           bits_out bits = stream_bits ();
    8386      1357188 :           if (!(mk & MK_template_mask) && !state->is_header ())
    8387              :             {
    8388              :               /* Tell the importer whether this is a global module entity,
    8389              :                  or a module entity.  */
    8390       312457 :               tree o = get_originating_module_decl (decl);
    8391       312457 :               bool is_attached = false;
    8392              : 
    8393       312457 :               tree not_tmpl = STRIP_TEMPLATE (o);
    8394       312457 :               if (DECL_LANG_SPECIFIC (not_tmpl)
    8395       498957 :                   && DECL_MODULE_ATTACH_P (not_tmpl))
    8396              :                 is_attached = true;
    8397              : 
    8398       312457 :               bits.b (is_attached);
    8399              :             }
    8400      1357188 :           bits.b (dep && dep->has_defn ());
    8401      1357188 :         }
    8402      1645506 :       tree_node_bools (decl);
    8403              :     }
    8404              : 
    8405      4506830 :   int tag = insert (decl, WK_value);
    8406      4506830 :   if (streaming_p ())
    8407      1645506 :     dump (dumper::TREE)
    8408          683 :       && dump ("Writing %s:%d %C:%N%S", merge_kind_name[mk], tag,
    8409          683 :                TREE_CODE (decl), decl, decl);
    8410              : 
    8411      4506830 :   tree inner = decl;
    8412      4506830 :   int inner_tag = 0;
    8413      4506830 :   if (TREE_CODE (decl) == TEMPLATE_DECL)
    8414              :     {
    8415      1289422 :       inner = DECL_TEMPLATE_RESULT (decl);
    8416      1289422 :       inner_tag = insert (inner, WK_value);
    8417              : 
    8418              :       /* On stream-in we assume that a template and its result will
    8419              :          have the same type.  */
    8420      1289422 :       gcc_checking_assert (TREE_TYPE (decl) == TREE_TYPE (inner));
    8421              : 
    8422      1289422 :       if (streaming_p ())
    8423              :         {
    8424       429792 :           int code = TREE_CODE (inner);
    8425       429792 :           u (code);
    8426       429792 :           start (inner, true);
    8427       429792 :           tree_node_bools (inner);
    8428       429792 :           dump (dumper::TREE)
    8429          132 :             && dump ("Writing %s:%d %C:%N%S", merge_kind_name[mk], inner_tag,
    8430          132 :                      TREE_CODE (inner), inner, inner);
    8431              :         }
    8432              :     }
    8433              : 
    8434      4506830 :   tree type = NULL_TREE;
    8435      4506830 :   int type_tag = 0;
    8436      4506830 :   tree stub_decl = NULL_TREE;
    8437      4506830 :   int stub_tag = 0;
    8438      4506830 :   if (TREE_CODE (inner) == TYPE_DECL)
    8439              :     {
    8440      1612224 :       type = TREE_TYPE (inner);
    8441      1612224 :       bool has_type = (type == TYPE_MAIN_VARIANT (type)
    8442      1612224 :                        && TYPE_NAME (type) == inner);
    8443              : 
    8444      1612224 :       if (streaming_p ())
    8445       544547 :         u (has_type ? TREE_CODE (type) : 0);
    8446              : 
    8447      1612224 :       if (has_type)
    8448              :         {
    8449       736607 :           type_tag = insert (type, WK_value);
    8450       736607 :           if (streaming_p ())
    8451              :             {
    8452       245511 :               start (type, true);
    8453       245511 :               tree_node_bools (type);
    8454       245511 :               dump (dumper::TREE)
    8455          155 :                 && dump ("Writing type:%d %C:%N", type_tag,
    8456          155 :                          TREE_CODE (type), type);
    8457              :             }
    8458              : 
    8459       736607 :           stub_decl = TYPE_STUB_DECL (type);
    8460       736607 :           bool has_stub = inner != stub_decl;
    8461       736607 :           if (streaming_p ())
    8462       245511 :             u (has_stub ? TREE_CODE (stub_decl) : 0);
    8463       736607 :           if (has_stub)
    8464              :             {
    8465         2361 :               stub_tag = insert (stub_decl);
    8466         2361 :               if (streaming_p ())
    8467              :                 {
    8468          786 :                   start (stub_decl, true);
    8469          786 :                   tree_node_bools (stub_decl);
    8470          786 :                   dump (dumper::TREE)
    8471            0 :                     && dump ("Writing stub_decl:%d %C:%N", stub_tag,
    8472            0 :                              TREE_CODE (stub_decl), stub_decl);
    8473              :                 }
    8474              :             }
    8475              :           else
    8476              :             stub_decl = NULL_TREE;
    8477              :         }
    8478              :       else
    8479              :         /* Regular typedef.  */
    8480              :         type = NULL_TREE;
    8481              :     }
    8482              : 
    8483              :   /* Stream the container, we want it correctly canonicalized before
    8484              :      we start emitting keys for this decl.  */
    8485      4506830 :   tree container = decl_container (decl);
    8486      4506830 :   unsigned tpl_levels = 0;
    8487              : 
    8488              :   /* Also tell the importer whether this is a temploid friend attached
    8489              :      to a different module (which has implications for merging), so that
    8490              :      importers can reconstruct this information on stream-in.  */
    8491      4506830 :   if (TREE_CODE (inner) == FUNCTION_DECL || TREE_CODE (inner) == TYPE_DECL)
    8492              :     {
    8493      3427500 :       tree* temploid_friend_slot = imported_temploid_friends->get (decl);
    8494      3427500 :       gcc_checking_assert (!temploid_friend_slot || *temploid_friend_slot);
    8495      3427500 :       tree_node (temploid_friend_slot ? *temploid_friend_slot : NULL_TREE);
    8496              :     }
    8497              : 
    8498      4506830 :   {
    8499      4506830 :     auto wmk = make_temp_override (dep_hash->writing_merge_key, true);
    8500      4506830 :     if (decl != inner)
    8501      1289422 :       tpl_header (decl, &tpl_levels);
    8502      4506830 :     if (TREE_CODE (inner) == FUNCTION_DECL)
    8503      1815276 :       fn_parms_init (inner);
    8504              : 
    8505              :     /* Now write out the merging information, and then really
    8506              :        install the tag values.  */
    8507      4506830 :     key_mergeable (tag, mk, decl, inner, container, dep);
    8508              : 
    8509      4506830 :     if (streaming_p ())
    8510      4510729 :       dump (dumper::MERGE)
    8511         1101 :         && dump ("Wrote:%d's %s merge key %C:%N", tag,
    8512         1101 :                  merge_kind_name[mk], TREE_CODE (decl), decl);
    8513      4506830 :   }
    8514              : 
    8515      4506830 :   if (TREE_CODE (inner) == FUNCTION_DECL)
    8516      4506830 :     fn_parms_fini (inner);
    8517              : 
    8518      4506830 :   if (!is_key_order ())
    8519      3311035 :     tree_node_vals (decl);
    8520              : 
    8521      4506830 :   if (inner_tag)
    8522              :     {
    8523      1289422 :       if (!is_key_order ())
    8524       859630 :         tree_node_vals (inner);
    8525      1289422 :       tpl_parms_fini (decl, tpl_levels);
    8526              :     }
    8527              : 
    8528      4506830 :   if (type && !is_key_order ())
    8529              :     {
    8530       491096 :       tree_node_vals (type);
    8531       491096 :       if (stub_decl)
    8532         1575 :         tree_node_vals (stub_decl);
    8533              :     }
    8534              : 
    8535      4506830 :   if (!is_key_order ())
    8536              :     {
    8537      3311035 :       if (mk & MK_template_mask
    8538      2311499 :           || mk == MK_partial
    8539      2311499 :           || mk == MK_friend_spec)
    8540              :         {
    8541        38682 :           if (mk != MK_partial)
    8542              :             {
    8543              :               // FIXME: We should make use of the merge-key by
    8544              :               // exposing it outside of key_mergeable.  But this gets
    8545              :               // the job done.
    8546       999536 :               auto *entry = reinterpret_cast <spec_entry *> (dep->deps[0]);
    8547              : 
    8548       999536 :               if (streaming_p ())
    8549       499768 :                 u (get_mergeable_specialization_flags (mk & MK_tmpl_decl_mask,
    8550              :                                                        entry->tmpl, decl));
    8551       999536 :               tree_node (entry->tmpl);
    8552       999536 :               tree_node (entry->args);
    8553              :             }
    8554              :           else
    8555              :             {
    8556        38682 :               tree ti = get_template_info (inner);
    8557        38682 :               tree_node (TI_TEMPLATE (ti));
    8558        38682 :               tree_node (TI_ARGS (ti));
    8559              :             }
    8560              :         }
    8561      3311035 :       tree_node (get_constraints (decl));
    8562              :     }
    8563              : 
    8564      4506830 :   if (streaming_p ())
    8565              :     {
    8566              :       /* Do not stray outside this section.  */
    8567      1645506 :       gcc_checking_assert (!dep || dep->section == dep_hash->section);
    8568              : 
    8569              :       /* Write the entity index, so we can insert it as soon as we
    8570              :          know this is new.  */
    8571      1645506 :       install_entity (decl, dep);
    8572              :     }
    8573              : 
    8574      4506830 :   if (DECL_LANG_SPECIFIC (inner)
    8575      3808615 :       && DECL_MODULE_KEYED_DECLS_P (inner)
    8576      4508033 :       && streaming_p ())
    8577              :     {
    8578              :       /* Stream the keyed entities.  There may be keyed entities that we
    8579              :          choose not to stream, such as a lambda in a non-inline variable's
    8580              :          initializer, so don't build dependencies for them here; any deps
    8581              :          we need should be acquired during write_definition (possibly
    8582              :          indirectly).  */
    8583          394 :       auto *attach_vec = keyed_table->get (inner);
    8584          394 :       unsigned num = attach_vec->length ();
    8585          394 :       u (num);
    8586          824 :       for (unsigned ix = 0; ix != num; ix++)
    8587              :         {
    8588          430 :           tree attached = (*attach_vec)[ix];
    8589          430 :           if (attached)
    8590              :             {
    8591          430 :               tree ti = TYPE_TEMPLATE_INFO (TREE_TYPE (attached));
    8592          430 :               if (!dep_hash->find_dependency (attached)
    8593          430 :                   && !(ti && dep_hash->find_dependency (TI_TEMPLATE (ti))))
    8594              :                 attached = NULL_TREE;
    8595              :             }
    8596              : 
    8597          430 :           tree_node (attached);
    8598          472 :           dump (dumper::MERGE)
    8599           30 :             && dump ("Written %d[%u] attached decl %N", tag, ix, attached);
    8600              :         }
    8601              :     }
    8602              : 
    8603      4506830 :   bool is_typedef = false;
    8604      4506830 :   if (!type && TREE_CODE (inner) == TYPE_DECL)
    8605              :     {
    8606       875617 :       tree t = TREE_TYPE (inner);
    8607       875617 :       unsigned tdef_flags = 0;
    8608       875617 :       if (DECL_ORIGINAL_TYPE (inner)
    8609       875617 :           && TYPE_NAME (TREE_TYPE (inner)) == inner)
    8610              :         {
    8611       875617 :           tdef_flags |= 1;
    8612       875617 :           if (TYPE_STRUCTURAL_EQUALITY_P (t)
    8613       191652 :               && TYPE_DEPENDENT_P_VALID (t)
    8614      1058545 :               && TYPE_DEPENDENT_P (t))
    8615              :             tdef_flags |= 2;
    8616              :         }
    8617       875617 :       if (streaming_p ())
    8618       299036 :         u (tdef_flags);
    8619              : 
    8620       875617 :       if (tdef_flags & 1)
    8621              :         {
    8622              :           /* A typedef type.  */
    8623       875617 :           int type_tag = insert (t);
    8624       875617 :           if (streaming_p ())
    8625       299036 :             dump (dumper::TREE)
    8626          206 :               && dump ("Cloned:%d %s %C:%N", type_tag,
    8627              :                        tdef_flags & 2 ? "depalias" : "typedef",
    8628          206 :                        TREE_CODE (t), t);
    8629              : 
    8630              :           is_typedef = true;
    8631              :         }
    8632              :     }
    8633              : 
    8634      4506830 :   if (streaming_p () && DECL_MAYBE_IN_CHARGE_CDTOR_P (decl))
    8635              :     {
    8636       136685 :       bool cloned_p
    8637       136685 :         = (DECL_CHAIN (decl) && DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl)));
    8638        97303 :       bool needs_vtt_parm_p
    8639        97303 :         = (cloned_p && CLASSTYPE_VBASECLASSES (DECL_CONTEXT (decl)));
    8640        97303 :       bool omit_inherited_parms_p
    8641        97303 :         = (cloned_p && DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl)
    8642        77814 :            && base_ctor_omit_inherited_parms (decl));
    8643       136685 :       unsigned flags = (int (cloned_p) << 0
    8644       136685 :                         | int (needs_vtt_parm_p) << 1
    8645       136685 :                         | int (omit_inherited_parms_p) << 2);
    8646       136685 :       u (flags);
    8647       136764 :       dump (dumper::TREE) && dump ("CDTOR %N is %scloned",
    8648              :                                    decl, cloned_p ? "" : "not ");
    8649              :     }
    8650              : 
    8651      4506830 :   if (streaming_p () && VAR_P (decl) && CP_DECL_THREAD_LOCAL_P (decl))
    8652          172 :     u (decl_tls_model (decl));
    8653              : 
    8654      4506830 :   if (streaming_p ())
    8655      1645506 :     dump (dumper::TREE) && dump ("Written decl:%d %C:%N", tag,
    8656          683 :                                  TREE_CODE (decl), decl);
    8657              : 
    8658      4506830 :   if (NAMESPACE_SCOPE_P (inner))
    8659      2534904 :     gcc_checking_assert (!dep == (VAR_OR_FUNCTION_DECL_P (inner)
    8660              :                                   && DECL_LOCAL_DECL_P (inner)));
    8661      3239225 :   else if ((TREE_CODE (inner) == TYPE_DECL
    8662       870141 :             && !is_typedef
    8663       166481 :             && TYPE_NAME (TREE_TYPE (inner)) == inner)
    8664      3942885 :            || TREE_CODE (inner) == FUNCTION_DECL)
    8665              :     {
    8666      1536206 :       bool write_defn = !dep && has_definition (decl);
    8667      1536206 :       if (streaming_p ())
    8668       512299 :         u (write_defn);
    8669      1536206 :       if (write_defn)
    8670            6 :         write_definition (decl);
    8671              :     }
    8672              : }
    8673              : 
    8674              : tree
    8675      1229031 : trees_in::decl_value ()
    8676              : {
    8677      1229031 :   int tag = 0;
    8678      1229031 :   bool is_attached = false;
    8679      1229031 :   bool has_defn = false;
    8680      1229031 :   unsigned mk_u = u ();
    8681      1229031 :   if (mk_u >= MK_hwm || !merge_kind_name[mk_u])
    8682              :     {
    8683            0 :       set_overrun ();
    8684            0 :       return NULL_TREE;
    8685              :     }
    8686              : 
    8687      1229031 :   unsigned saved_unused = unused;
    8688      1229031 :   unused = 0;
    8689              : 
    8690      1229031 :   merge_kind mk = merge_kind (mk_u);
    8691              : 
    8692      1229031 :   tree decl = start ();
    8693      1229031 :   if (decl)
    8694              :     {
    8695      1229031 :       if (mk != MK_unique)
    8696              :         {
    8697      1011656 :           bits_in bits = stream_bits ();
    8698      1011656 :           if (!(mk & MK_template_mask) && !state->is_header ())
    8699        87108 :             is_attached = bits.b ();
    8700              : 
    8701      1011656 :           has_defn = bits.b ();
    8702      1011656 :         }
    8703              : 
    8704      1229031 :       if (!tree_node_bools (decl))
    8705            0 :         decl = NULL_TREE;
    8706              :     }
    8707              : 
    8708              :   /* Insert into map.  */
    8709      1229031 :   tag = insert (decl);
    8710      1229031 :   if (decl)
    8711      1229031 :     dump (dumper::TREE)
    8712          964 :       && dump ("Reading:%d %C", tag, TREE_CODE (decl));
    8713              : 
    8714      1229031 :   tree inner = decl;
    8715      1229031 :   int inner_tag = 0;
    8716      1229031 :   if (decl && TREE_CODE (decl) == TEMPLATE_DECL)
    8717              :     {
    8718       331465 :       int code = u ();
    8719       331465 :       inner = start (code);
    8720       331465 :       if (inner && tree_node_bools (inner))
    8721       331465 :         DECL_TEMPLATE_RESULT (decl) = inner;
    8722              :       else
    8723            0 :         decl = NULL_TREE;
    8724              : 
    8725       331465 :       inner_tag = insert (inner);
    8726       331465 :       if (decl)
    8727       331465 :         dump (dumper::TREE)
    8728          204 :           && dump ("Reading:%d %C", inner_tag, TREE_CODE (inner));
    8729              :     }
    8730              : 
    8731      1229031 :   tree type = NULL_TREE;
    8732      1229031 :   int type_tag = 0;
    8733      1229031 :   tree stub_decl = NULL_TREE;
    8734      1229031 :   int stub_tag = 0;
    8735      1229031 :   if (decl && TREE_CODE (inner) == TYPE_DECL)
    8736              :     {
    8737       394839 :       if (unsigned type_code = u ())
    8738              :         {
    8739       178206 :           type = start (type_code);
    8740       178206 :           if (type && tree_node_bools (type))
    8741              :             {
    8742       178206 :               TREE_TYPE (inner) = type;
    8743       178206 :               TYPE_NAME (type) = inner;
    8744              :             }
    8745              :           else
    8746            0 :             decl = NULL_TREE;
    8747              : 
    8748       178206 :           type_tag = insert (type);
    8749       178206 :           if (decl)
    8750       178206 :             dump (dumper::TREE)
    8751          212 :               && dump ("Reading type:%d %C", type_tag, TREE_CODE (type));
    8752              : 
    8753       178206 :           if (unsigned stub_code = u ())
    8754              :             {
    8755          441 :               stub_decl = start (stub_code);
    8756          441 :               if (stub_decl && tree_node_bools (stub_decl))
    8757              :                 {
    8758          441 :                   TREE_TYPE (stub_decl) = type;
    8759          441 :                   TYPE_STUB_DECL (type) = stub_decl;
    8760              :                 }
    8761              :               else
    8762            0 :                 decl = NULL_TREE;
    8763              : 
    8764          441 :               stub_tag = insert (stub_decl);
    8765          441 :               if (decl)
    8766          441 :                 dump (dumper::TREE)
    8767            0 :                   && dump ("Reading stub_decl:%d %C", stub_tag,
    8768            0 :                            TREE_CODE (stub_decl));
    8769              :             }
    8770              :         }
    8771              :     }
    8772              : 
    8773      1229031 :   if (!decl)
    8774              :     {
    8775            0 :     bail:
    8776            0 :       if (inner_tag != 0)
    8777            0 :         back_refs[~inner_tag] = NULL_TREE;
    8778            0 :       if (type_tag != 0)
    8779            0 :         back_refs[~type_tag] = NULL_TREE;
    8780            0 :       if (stub_tag != 0)
    8781            0 :         back_refs[~stub_tag] = NULL_TREE;
    8782            0 :       if (tag != 0)
    8783            0 :         back_refs[~tag] = NULL_TREE;
    8784            0 :       set_overrun ();
    8785              :       /* Bail.  */
    8786            0 :       unused = saved_unused;
    8787            0 :       return NULL_TREE;
    8788              :     }
    8789              : 
    8790              :   /* Read the container, to ensure it's already been streamed in.  */
    8791      1229031 :   tree container = decl_container ();
    8792      1229031 :   unsigned tpl_levels = 0;
    8793              : 
    8794              :   /* If this is an imported temploid friend, get the owning decl its
    8795              :      attachment is determined by (or NULL_TREE otherwise).  */
    8796      1229031 :   tree temploid_friend = NULL_TREE;
    8797      1229031 :   if (TREE_CODE (inner) == FUNCTION_DECL || TREE_CODE (inner) == TYPE_DECL)
    8798       865375 :     temploid_friend = tree_node ();
    8799              : 
    8800              :   /* Figure out if this decl is already known about.  */
    8801      1229031 :   int parm_tag = 0;
    8802              : 
    8803      1229031 :   if (decl != inner)
    8804       331465 :     if (!tpl_header (decl, &tpl_levels))
    8805            0 :       goto bail;
    8806      1229031 :   if (TREE_CODE (inner) == FUNCTION_DECL)
    8807       470536 :     parm_tag = fn_parms_init (inner);
    8808              : 
    8809      1229031 :   tree existing = key_mergeable (tag, mk, decl, inner, type, container,
    8810              :                                  is_attached, temploid_friend);
    8811      1229031 :   tree existing_inner = existing;
    8812      1229031 :   if (existing)
    8813              :     {
    8814       437447 :       if (existing == error_mark_node)
    8815            0 :         goto bail;
    8816              : 
    8817       437447 :       if (TREE_CODE (STRIP_TEMPLATE (existing)) == TYPE_DECL)
    8818              :         {
    8819       162406 :           tree etype = TREE_TYPE (existing);
    8820       162406 :           if (TYPE_LANG_SPECIFIC (etype)
    8821       117243 :               && COMPLETE_TYPE_P (etype)
    8822       230607 :               && !CLASSTYPE_MEMBER_VEC (etype))
    8823              :             /* Give it a member vec, we're likely gonna be looking
    8824              :                inside it.  */
    8825        14227 :             set_class_bindings (etype, -1);
    8826              :         }
    8827              : 
    8828              :       /* Install the existing decl into the back ref array.  */
    8829       437447 :       register_duplicate (decl, existing);
    8830       437447 :       back_refs[~tag] = existing;
    8831       437447 :       if (inner_tag != 0)
    8832              :         {
    8833       145579 :           existing_inner = DECL_TEMPLATE_RESULT (existing);
    8834       145579 :           back_refs[~inner_tag] = existing_inner;
    8835              :         }
    8836              : 
    8837       437447 :       if (type_tag != 0)
    8838              :         {
    8839        77903 :           tree existing_type = TREE_TYPE (existing);
    8840        77903 :           back_refs[~type_tag] = existing_type;
    8841        77903 :           if (stub_tag != 0)
    8842          245 :             back_refs[~stub_tag] = TYPE_STUB_DECL (existing_type);
    8843              :         }
    8844              :     }
    8845              : 
    8846      1229031 :   if (parm_tag)
    8847       470536 :     fn_parms_fini (parm_tag, inner, existing_inner, has_defn);
    8848              : 
    8849      1229031 :   if (!tree_node_vals (decl))
    8850            0 :     goto bail;
    8851              : 
    8852      1229031 :   if (inner_tag)
    8853              :     {
    8854       331465 :       gcc_checking_assert (DECL_TEMPLATE_RESULT (decl) == inner);
    8855              : 
    8856       331465 :       if (!tree_node_vals (inner))
    8857            0 :         goto bail;
    8858              : 
    8859       331465 :       if (!tpl_parms_fini (decl, tpl_levels))
    8860            0 :         goto bail;
    8861              :     }
    8862              : 
    8863      1229031 :   if (type && (!tree_node_vals (type)
    8864       178206 :                || (stub_decl && !tree_node_vals (stub_decl))))
    8865            0 :     goto bail;
    8866              : 
    8867      1229031 :   spec_entry spec;
    8868      1229031 :   unsigned spec_flags = 0;
    8869      1229031 :   if (mk & MK_template_mask
    8870       848523 :       || mk == MK_partial
    8871       848523 :       || mk == MK_friend_spec)
    8872              :     {
    8873        10828 :       if (mk == MK_partial)
    8874              :         spec_flags = 2;
    8875              :       else
    8876       380508 :         spec_flags = u ();
    8877              : 
    8878       391336 :       spec.tmpl = tree_node ();
    8879       391336 :       spec.args = tree_node ();
    8880              :     }
    8881              :   /* Hold constraints on the spec field, for a short while.  */
    8882      1229031 :   spec.spec = tree_node ();
    8883              : 
    8884      1229995 :   dump (dumper::TREE) && dump ("Read:%d %C:%N", tag, TREE_CODE (decl), decl);
    8885              : 
    8886      1229031 :   existing = back_refs[~tag];
    8887      1229031 :   bool installed = install_entity (existing);
    8888      1229031 :   bool is_new = existing == decl;
    8889              : 
    8890      1229031 :   if (DECL_LANG_SPECIFIC (inner)
    8891      2306180 :       && DECL_MODULE_KEYED_DECLS_P (inner))
    8892              :     {
    8893              :       /* Read and maybe install the attached entities.  */
    8894          377 :       bool existed;
    8895          377 :       auto &set = keyed_table->get_or_insert (STRIP_TEMPLATE (existing),
    8896              :                                               &existed);
    8897          377 :       unsigned num = u ();
    8898          377 :       if (is_new == existed)
    8899            0 :         set_overrun ();
    8900          377 :       if (is_new)
    8901          244 :         set.reserve (num);
    8902          803 :       for (unsigned ix = 0; !get_overrun () && ix != num; ix++)
    8903              :         {
    8904          426 :           tree attached = tree_node ();
    8905          426 :           dump (dumper::MERGE)
    8906          105 :             && dump ("Read %d[%u] %s attached decl %N", tag, ix,
    8907              :                      is_new ? "new" : "matched", attached);
    8908          426 :           if (is_new)
    8909          274 :             set.quick_push (attached);
    8910          152 :           else if (set[ix] != attached)
    8911              :             {
    8912            3 :               if (!set[ix] || !attached)
    8913              :                 /* One import left a hole for a lambda dep we chose not
    8914              :                    to stream, but another import chose to stream that lambda.
    8915              :                    Let's not error here: hopefully we'll complain later in
    8916              :                    is_matching_decl about whatever caused us to make a
    8917              :                    different decision.  */
    8918              :                 ;
    8919              :               else
    8920            0 :                 set_overrun ();
    8921              :             }
    8922              :         }
    8923              :     }
    8924              : 
    8925              :   /* Regular typedefs will have a NULL TREE_TYPE at this point.  */
    8926      1229031 :   unsigned tdef_flags = 0;
    8927      1229031 :   bool is_typedef = false;
    8928      1229031 :   if (!type && TREE_CODE (inner) == TYPE_DECL)
    8929              :     {
    8930       216633 :       tdef_flags = u ();
    8931       216633 :       if (tdef_flags & 1)
    8932       216633 :         is_typedef = true;
    8933              :     }
    8934              : 
    8935      1229031 :   if (is_new)
    8936              :     {
    8937              :       /* A newly discovered node.  */
    8938       791584 :       if (TREE_CODE (decl) == FUNCTION_DECL && DECL_VIRTUAL_P (decl))
    8939              :         /* Mark this identifier as naming a virtual function --
    8940              :            lookup_overrides relies on this optimization.  */
    8941         6620 :         IDENTIFIER_VIRTUAL_P (DECL_NAME (decl)) = true;
    8942              : 
    8943       791584 :       if (installed)
    8944              :         {
    8945              :           /* Mark the entity as imported.  */
    8946       505973 :           retrofit_lang_decl (inner);
    8947       505973 :           DECL_MODULE_IMPORT_P (inner) = true;
    8948              :         }
    8949              : 
    8950       791584 :       if (temploid_friend)
    8951           38 :         imported_temploid_friends->put (decl, temploid_friend);
    8952              : 
    8953       791584 :       if (spec.spec)
    8954        33716 :         set_constraints (decl, spec.spec);
    8955              : 
    8956       791584 :       if (TREE_CODE (decl) == INTEGER_CST && !TREE_OVERFLOW (decl))
    8957              :         {
    8958            0 :           decl = cache_integer_cst (decl, true);
    8959            0 :           back_refs[~tag] = decl;
    8960              :         }
    8961              : 
    8962       791584 :       if (is_typedef)
    8963              :         {
    8964              :           /* Frob it to be ready for cloning.  */
    8965       132130 :           TREE_TYPE (inner) = DECL_ORIGINAL_TYPE (inner);
    8966       132130 :           DECL_ORIGINAL_TYPE (inner) = NULL_TREE;
    8967       132130 :           if (TREE_CODE (TREE_TYPE (inner)) != TU_LOCAL_ENTITY)
    8968              :             {
    8969       132127 :               set_underlying_type (inner);
    8970       132127 :               if (tdef_flags & 2)
    8971              :                 {
    8972              :                   /* Match instantiate_alias_template's handling.  */
    8973        33552 :                   tree type = TREE_TYPE (inner);
    8974        33552 :                   TYPE_DEPENDENT_P (type) = true;
    8975        33552 :                   TYPE_DEPENDENT_P_VALID (type) = true;
    8976        33552 :                   SET_TYPE_STRUCTURAL_EQUALITY (type);
    8977              :                 }
    8978              :             }
    8979              :         }
    8980              : 
    8981       791584 :       if (inner_tag)
    8982              :         /* Set the TEMPLATE_DECL's type.  */
    8983       185886 :         TREE_TYPE (decl) = TREE_TYPE (inner);
    8984              : 
    8985              :       /* Redetermine whether we need to import or export this declaration
    8986              :          for this TU.  But for extern templates we know we must import:
    8987              :          they'll be defined in a different TU.
    8988              :          FIXME: How do dllexport and dllimport interact across a module?
    8989              :          See also https://github.com/itanium-cxx-abi/cxx-abi/issues/170.
    8990              :          May have to revisit?  */
    8991       791584 :       if (type
    8992       100303 :           && CLASS_TYPE_P (type)
    8993        86523 :           && TYPE_LANG_SPECIFIC (type)
    8994       878107 :           && !(CLASSTYPE_EXPLICIT_INSTANTIATION (type)
    8995          769 :                && CLASSTYPE_INTERFACE_KNOWN (type)
    8996          769 :                && CLASSTYPE_INTERFACE_ONLY (type)))
    8997              :         {
    8998        85806 :           CLASSTYPE_INTERFACE_ONLY (type) = false;
    8999        85806 :           CLASSTYPE_INTERFACE_UNKNOWN (type) = true;
    9000              :         }
    9001              : 
    9002              :       /* Add to specialization tables now that constraints etc are
    9003              :          added.  */
    9004       791584 :       if (mk == MK_partial)
    9005              :         {
    9006         4672 :           bool is_type = TREE_CODE (inner) == TYPE_DECL;
    9007         4672 :           spec.spec = is_type ? type : inner;
    9008         4672 :           add_mergeable_specialization (!is_type, &spec, decl, spec_flags);
    9009              :         }
    9010       786912 :       else if (mk & MK_template_mask)
    9011              :         {
    9012       229222 :           bool is_type = !(mk & MK_tmpl_decl_mask);
    9013       229222 :           spec.spec = is_type ? type : mk & MK_tmpl_tmpl_mask ? inner : decl;
    9014       229222 :           add_mergeable_specialization (!is_type, &spec, decl, spec_flags);
    9015              :         }
    9016              : 
    9017       791584 :       if (NAMESPACE_SCOPE_P (decl)
    9018       166193 :           && (mk == MK_named || mk == MK_unique
    9019       166193 :               || mk == MK_enum || mk == MK_friend_spec)
    9020       866100 :           && !(VAR_OR_FUNCTION_DECL_P (decl) && DECL_LOCAL_DECL_P (decl)))
    9021        74415 :         add_module_namespace_decl (CP_DECL_CONTEXT (decl), decl);
    9022              : 
    9023       791584 :       if (DECL_ARTIFICIAL (decl)
    9024       216867 :           && TREE_CODE (decl) == FUNCTION_DECL
    9025        23239 :           && !DECL_TEMPLATE_INFO (decl)
    9026        22927 :           && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
    9027        22749 :           && TYPE_SIZE (DECL_CONTEXT (decl))
    9028       793089 :           && !DECL_THUNK_P (decl))
    9029              :         /* A new implicit member function, when the class is
    9030              :            complete.  This means the importee declared it, and
    9031              :            we must now add it to the class.  Note that implicit
    9032              :            member fns of template instantiations do not themselves
    9033              :            look like templates.  */
    9034          931 :         if (!install_implicit_member (inner))
    9035            0 :           set_overrun ();
    9036              : 
    9037              :       /* When importing a TLS wrapper from a header unit, we haven't
    9038              :          actually emitted its definition yet. Remember it so we can
    9039              :          do this later.  */
    9040       791584 :       if (state->is_header ()
    9041       791584 :           && decl_tls_wrapper_p (decl))
    9042            6 :         note_vague_linkage_fn (decl);
    9043              : 
    9044              :       /* Apply relevant attributes.
    9045              :          FIXME should probably use cplus_decl_attributes for this,
    9046              :          but it's not yet ready for modules.  */
    9047              : 
    9048       791584 :       if (VAR_OR_FUNCTION_DECL_P (inner))
    9049       477798 :         if (tree attr = lookup_attribute ("section", DECL_ATTRIBUTES (inner)))
    9050              :           {
    9051            6 :             tree section_name = TREE_VALUE (TREE_VALUE (attr));
    9052            6 :             set_decl_section_name (inner, TREE_STRING_POINTER (section_name));
    9053              :           }
    9054              : 
    9055              :       /* Setup aliases for the declaration.  */
    9056       791584 :       if (tree alias = lookup_attribute ("alias", DECL_ATTRIBUTES (decl)))
    9057              :         {
    9058            3 :           alias = TREE_VALUE (TREE_VALUE (alias));
    9059            3 :           alias = get_identifier (TREE_STRING_POINTER (alias));
    9060            3 :           assemble_alias (decl, alias);
    9061              :         }
    9062              :     }
    9063              :   else
    9064              :     {
    9065              :       /* DECL is the to-be-discarded decl.  Its internal pointers will
    9066              :          be to the EXISTING's structure.  Frob it to point to its
    9067              :          own other structures, so loading its definition will alter
    9068              :          it, and not the existing decl.  */
    9069       439209 :       dump (dumper::MERGE) && dump ("Deduping %N", existing);
    9070              : 
    9071       437447 :       if (inner_tag)
    9072       145579 :         DECL_TEMPLATE_RESULT (decl) = inner;
    9073              : 
    9074       437447 :       if (type)
    9075              :         {
    9076              :           /* Point at the to-be-discarded type & decl.  */
    9077        77903 :           TYPE_NAME (type) = inner;
    9078        77903 :           TREE_TYPE (inner) = type;
    9079              : 
    9080       155561 :           TYPE_STUB_DECL (type) = stub_decl ? stub_decl : inner;
    9081        77903 :           if (stub_decl)
    9082          245 :             TREE_TYPE (stub_decl) = type;
    9083              : 
    9084        77903 :           tree etype = TREE_TYPE (existing);
    9085              : 
    9086              :           /* Handle separate declarations with different attributes.  */
    9087        77903 :           tree &dattr = TYPE_ATTRIBUTES (type);
    9088        77903 :           tree &eattr = TYPE_ATTRIBUTES (etype);
    9089        77903 :           check_abi_tags (existing, decl, eattr, dattr);
    9090              :           // TODO: handle other conflicting type attributes
    9091        77903 :           eattr = merge_attributes (eattr, dattr);
    9092              : 
    9093              :           /* When merging a partial specialisation, the existing decl may have
    9094              :              had its TYPE_CANONICAL adjusted.  If so we should use structural
    9095              :              equality to ensure is_matching_decl doesn't get confused.  */
    9096        77903 :           if ((spec_flags & 2)
    9097        77903 :               && TYPE_CANONICAL (type) != TYPE_CANONICAL (etype))
    9098            3 :             SET_TYPE_STRUCTURAL_EQUALITY (type);
    9099              :         }
    9100              : 
    9101       437447 :       if (inner_tag)
    9102              :         /* Set the TEMPLATE_DECL's type.  */
    9103       145579 :         TREE_TYPE (decl) = TREE_TYPE (inner);
    9104              : 
    9105       437447 :       if (!is_matching_decl (existing, decl, is_typedef))
    9106           42 :         unmatched_duplicate (existing);
    9107              : 
    9108       437447 :       if (TREE_CODE (inner) == FUNCTION_DECL)
    9109              :         {
    9110       200242 :           tree e_inner = STRIP_TEMPLATE (existing);
    9111       200242 :           for (auto parm = DECL_ARGUMENTS (inner);
    9112       598782 :                parm; parm = DECL_CHAIN (parm))
    9113       398540 :             DECL_CONTEXT (parm) = e_inner;
    9114              :         }
    9115              : 
    9116              :       /* And our result is the existing node.  */
    9117       437447 :       decl = existing;
    9118              :     }
    9119              : 
    9120      1229031 :   if (mk == MK_friend_spec)
    9121              :     {
    9122            0 :       tree e = match_mergeable_specialization (true, &spec);
    9123            0 :       if (!e)
    9124              :         {
    9125            0 :           spec.spec = inner;
    9126            0 :           add_mergeable_specialization (true, &spec, decl, spec_flags);
    9127              :         }
    9128            0 :       else if (e != existing)
    9129            0 :         set_overrun ();
    9130              :     }
    9131              : 
    9132      1229031 :   if (is_typedef)
    9133              :     {
    9134              :       /* Insert the type into the array now.  */
    9135       216633 :       tag = insert (TREE_TYPE (decl));
    9136       216633 :       dump (dumper::TREE)
    9137          247 :         && dump ("Cloned:%d typedef %C:%N",
    9138          247 :                  tag, TREE_CODE (TREE_TYPE (decl)), TREE_TYPE (decl));
    9139              :     }
    9140              : 
    9141      1229031 :   unused = saved_unused;
    9142              : 
    9143      1229031 :   if (DECL_MAYBE_IN_CHARGE_CDTOR_P (decl))
    9144              :     {
    9145       105824 :       unsigned flags = u ();
    9146              : 
    9147       105824 :       if (is_new)
    9148              :         {
    9149        65477 :           bool cloned_p = flags & 1;
    9150        65577 :           dump (dumper::TREE) && dump ("CDTOR %N is %scloned",
    9151              :                                        decl, cloned_p ? "" : "not ");
    9152        65477 :           if (cloned_p)
    9153              :             {
    9154              :               /* Update the member vec, if there is one (we're in a different
    9155              :                  cluster to the class defn) and this isn't a primary template
    9156              :                  specialization (as in tsubst_function_decl).  */
    9157        46382 :               bool up = (CLASSTYPE_MEMBER_VEC (DECL_CONTEXT (decl))
    9158        46382 :                          && !primary_template_specialization_p (decl));
    9159        46382 :               build_cdtor_clones (decl, flags & 2, flags & 4, up);
    9160              :             }
    9161              :         }
    9162              :     }
    9163              : 
    9164      1229031 :   if (VAR_P (decl) && CP_DECL_THREAD_LOCAL_P (decl))
    9165              :     {
    9166          160 :       enum tls_model model = tls_model (u ());
    9167          160 :       if (is_new)
    9168          140 :         set_decl_tls_model (decl, model);
    9169              :     }
    9170              : 
    9171      1229031 :   if (!NAMESPACE_SCOPE_P (inner)
    9172       916557 :       && ((TREE_CODE (inner) == TYPE_DECL
    9173       212679 :            && !is_typedef
    9174        38002 :            && TYPE_NAME (TREE_TYPE (inner)) == inner)
    9175       878555 :           || TREE_CODE (inner) == FUNCTION_DECL)
    9176      1625542 :       && u ())
    9177            3 :     read_definition (decl);
    9178              : 
    9179              :   return decl;
    9180              : }
    9181              : 
    9182              : /* DECL is an unnameable member of CTX.  Return a suitable identifying
    9183              :    index.  */
    9184              : 
    9185              : static unsigned
    9186         1481 : get_field_ident (tree ctx, tree decl)
    9187              : {
    9188         1481 :   gcc_checking_assert (TREE_CODE (decl) == USING_DECL
    9189              :                        || !DECL_NAME (decl)
    9190              :                        || IDENTIFIER_ANON_P (DECL_NAME (decl)));
    9191              : 
    9192         1481 :   unsigned ix = 0;
    9193         1481 :   for (tree fields = TYPE_FIELDS (ctx);
    9194        10889 :        fields; fields = DECL_CHAIN (fields))
    9195              :     {
    9196        10889 :       if (fields == decl)
    9197         1481 :         return ix;
    9198              : 
    9199         9408 :       if (DECL_CONTEXT (fields) == ctx
    9200         9408 :           && (TREE_CODE (fields) == USING_DECL
    9201         9387 :               || (TREE_CODE (fields) == FIELD_DECL
    9202           92 :                   && (!DECL_NAME (fields)
    9203           48 :                       || IDENTIFIER_ANON_P (DECL_NAME (fields))))))
    9204              :         /* Count this field.  */
    9205           47 :         ix++;
    9206              :     }
    9207            0 :   gcc_unreachable ();
    9208              : }
    9209              : 
    9210              : static tree
    9211         1055 : lookup_field_ident (tree ctx, unsigned ix)
    9212              : {
    9213         1055 :   for (tree fields = TYPE_FIELDS (ctx);
    9214         8104 :        fields; fields = DECL_CHAIN (fields))
    9215         8104 :     if (DECL_CONTEXT (fields) == ctx
    9216         8104 :         && (TREE_CODE (fields) == USING_DECL
    9217         8092 :             || (TREE_CODE (fields) == FIELD_DECL
    9218         1120 :                 && (!DECL_NAME (fields)
    9219           25 :                     || IDENTIFIER_ANON_P (DECL_NAME (fields))))))
    9220         1101 :       if (!ix--)
    9221              :         return fields;
    9222              : 
    9223              :   return NULL_TREE;
    9224              : }
    9225              : 
    9226              : /* Reference DECL.  REF indicates the walk kind we are performing.
    9227              :    Return true if we should write this decl by value.  */
    9228              : 
    9229              : bool
    9230     15709174 : trees_out::decl_node (tree decl, walk_kind ref)
    9231              : {
    9232     15709174 :   gcc_checking_assert (DECL_P (decl) && !DECL_TEMPLATE_PARM_P (decl)
    9233              :                        && DECL_CONTEXT (decl));
    9234              : 
    9235     15709174 :   if (ref == WK_value)
    9236              :     {
    9237      1523823 :       depset *dep = dep_hash->find_dependency (decl);
    9238      1523823 :       decl_value (decl, dep);
    9239      1523823 :       return false;
    9240              :     }
    9241              : 
    9242     14185351 :   switch (TREE_CODE (decl))
    9243              :     {
    9244              :     default:
    9245              :       break;
    9246              : 
    9247      1500232 :     case FUNCTION_DECL:
    9248      1500232 :       gcc_checking_assert (!DECL_LOCAL_DECL_P (decl));
    9249              :       break;
    9250              : 
    9251              :     case RESULT_DECL:
    9252              :       /* Unlike PARM_DECLs, RESULT_DECLs are only generated and
    9253              :          referenced when we're inside the function itself.  */
    9254              :       return true;
    9255              : 
    9256       233244 :     case PARM_DECL:
    9257       233244 :       {
    9258       233244 :         if (streaming_p ())
    9259       101311 :           i (tt_parm);
    9260       233244 :         tree_node (DECL_CONTEXT (decl));
    9261              : 
    9262              :         /* That must have put this in the map.  */
    9263       233244 :         walk_kind ref = ref_node (decl);
    9264       233244 :         if (ref != WK_none)
    9265              :           // FIXME:OPTIMIZATION We can wander into bits of the
    9266              :           // template this was instantiated from, for instance
    9267              :           // deferred noexcept and default parms, or references
    9268              :           // to parms from earlier forward-decls (PR c++/119608).
    9269              :           //
    9270              :           // Currently we'll end up cloning those bits of tree.
    9271              :           // It would be nice to reference those specific nodes.
    9272              :           // I think putting those things in the map when we
    9273              :           // reference their template by name.
    9274              :           //
    9275              :           // See the note in add_indirects.
    9276              :           return true;
    9277              : 
    9278            0 :         if (streaming_p ())
    9279            0 :           dump (dumper::TREE)
    9280            0 :             && dump ("Wrote %s reference %N",
    9281            0 :                      TREE_CODE (decl) == PARM_DECL ? "parameter" : "result",
    9282              :                      decl);
    9283              :       }
    9284              :       return false;
    9285              : 
    9286              :     case IMPORTED_DECL:
    9287              :       /* This describes a USING_DECL to the ME's debug machinery.  It
    9288              :          originates from the fortran FE, and has nothing to do with
    9289              :          C++ modules.  */
    9290              :       return true;
    9291              : 
    9292              :     case LABEL_DECL:
    9293              :       return true;
    9294              : 
    9295        84691 :     case CONST_DECL:
    9296        84691 :       {
    9297              :         /* If I end up cloning enum decls, implementing C++20 using
    9298              :            E::v, this will need tweaking.   */
    9299        84691 :         if (streaming_p ())
    9300        19919 :           i (tt_enum_decl);
    9301        84691 :         tree ctx = DECL_CONTEXT (decl);
    9302        84691 :         gcc_checking_assert (TREE_CODE (ctx) == ENUMERAL_TYPE);
    9303        84691 :         tree_node (ctx);
    9304        84691 :         tree_node (DECL_NAME (decl));
    9305              : 
    9306        84691 :         int tag = insert (decl);
    9307        84691 :         if (streaming_p ())
    9308        19919 :           dump (dumper::TREE)
    9309           21 :             && dump ("Wrote enum decl:%d %C:%N", tag, TREE_CODE (decl), decl);
    9310              :         return false;
    9311              :       }
    9312        32545 :       break;
    9313              : 
    9314        32545 :     case USING_DECL:
    9315        32545 :       if (TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
    9316              :         break;
    9317              :       /* FALLTHROUGH  */
    9318              : 
    9319       212214 :     case FIELD_DECL:
    9320       212214 :       {
    9321       212214 :         if (streaming_p ())
    9322        12136 :           i (tt_data_member);
    9323              : 
    9324       212214 :         tree ctx = DECL_CONTEXT (decl);
    9325       212214 :         tree_node (ctx);
    9326              : 
    9327       212214 :         tree name = NULL_TREE;
    9328              : 
    9329       212214 :         if (TREE_CODE (decl) == USING_DECL)
    9330              :           ;
    9331              :         else
    9332              :           {
    9333       210619 :             name = DECL_NAME (decl);
    9334       408734 :             if (name && IDENTIFIER_ANON_P (name))
    9335              :               name = NULL_TREE;
    9336              :           }
    9337              : 
    9338       212214 :         tree_node (name);
    9339       212214 :         if (!name && streaming_p ())
    9340              :           {
    9341         1481 :             unsigned ix = get_field_ident (ctx, decl);
    9342         1481 :             u (ix);
    9343              :           }
    9344              : 
    9345       212214 :         int tag = insert (decl);
    9346       212214 :         if (streaming_p ())
    9347        12136 :           dump (dumper::TREE)
    9348           26 :             && dump ("Wrote member:%d %C:%N", tag, TREE_CODE (decl), decl);
    9349              :         return false;
    9350              :       }
    9351       652223 :       break;
    9352              : 
    9353       652223 :     case VAR_DECL:
    9354       652223 :       gcc_checking_assert (!DECL_LOCAL_DECL_P (decl));
    9355       652223 :       if (DECL_VTABLE_OR_VTT_P (decl))
    9356              :         {
    9357              :           /* VTT or VTABLE, they are all on the vtables list.  */
    9358         4220 :           tree ctx = CP_DECL_CONTEXT (decl);
    9359         4220 :           tree vtable = CLASSTYPE_VTABLES (ctx);
    9360         4325 :           for (unsigned ix = 0; ; vtable = DECL_CHAIN (vtable), ix++)
    9361         4325 :             if (vtable == decl)
    9362              :               {
    9363         4220 :                 gcc_checking_assert (DECL_VIRTUAL_P (decl));
    9364         4220 :                 if (streaming_p ())
    9365              :                   {
    9366           43 :                     u (tt_vtable);
    9367           43 :                     u (ix);
    9368           43 :                     dump (dumper::TREE)
    9369            0 :                       && dump ("Writing vtable %N[%u]", ctx, ix);
    9370              :                   }
    9371         4220 :                 tree_node (ctx);
    9372         4220 :                 return false;
    9373              :               }
    9374              :           gcc_unreachable ();
    9375              :         }
    9376              : 
    9377       648003 :       if (DECL_TINFO_P (decl))
    9378              :         {
    9379         8486 :         tinfo:
    9380              :           /* A typeinfo, tt_tinfo_typedef or tt_tinfo_var.  */
    9381        15631 :           bool is_var = VAR_P (decl);
    9382        15631 :           tree type = TREE_TYPE (decl);
    9383        15631 :           unsigned ix = get_pseudo_tinfo_index (type);
    9384        15631 :           if (streaming_p ())
    9385              :             {
    9386         9880 :               i (is_var ? tt_tinfo_var : tt_tinfo_typedef);
    9387         6998 :               u (ix);
    9388              :             }
    9389              : 
    9390        15631 :           if (is_var)
    9391              :             {
    9392              :               /* We also need the type it is for and mangled name, so
    9393              :                  the reader doesn't need to complete the type (which
    9394              :                  would break section ordering).  The type it is for is
    9395              :                  stashed on the name's TREE_TYPE.  */
    9396         8486 :               tree name = DECL_NAME (decl);
    9397         8486 :               tree_node (name);
    9398         8486 :               type = TREE_TYPE (name);
    9399         8486 :               tree_node (type);
    9400              :             }
    9401              : 
    9402        15631 :           int tag = insert (decl);
    9403        15631 :           if (streaming_p ())
    9404         6998 :             dump (dumper::TREE)
    9405           27 :               && dump ("Wrote tinfo_%s:%d %u %N", is_var ? "var" : "type",
    9406              :                        tag, ix, type);
    9407              : 
    9408        15631 :           if (!is_var)
    9409              :             {
    9410         7145 :               tag = insert (type);
    9411         7145 :               if (streaming_p ())
    9412         2882 :                 dump (dumper::TREE)
    9413            9 :                   && dump ("Wrote tinfo_type:%d %u %N", tag, ix, type);
    9414              :             }
    9415        15631 :           return false;
    9416              :         }
    9417              : 
    9418       639517 :       if (DECL_NTTP_OBJECT_P (decl))
    9419              :         {
    9420              :           /* A NTTP parm object.  */
    9421           42 :           if (streaming_p ())
    9422           10 :             i (tt_nttp_var);
    9423           42 :           tree_node (tparm_object_argument (decl));
    9424           42 :           tree_node (DECL_NAME (decl));
    9425           42 :           int tag = insert (decl);
    9426           42 :           if (streaming_p ())
    9427           10 :             dump (dumper::TREE)
    9428            0 :               && dump ("Wrote nttp object:%d %N", tag, DECL_NAME (decl));
    9429           42 :           return false;
    9430              :         }
    9431              : 
    9432              :       break;
    9433              : 
    9434      4863039 :     case TYPE_DECL:
    9435      4863039 :       if (DECL_TINFO_P (decl))
    9436         7145 :         goto tinfo;
    9437              :       break;
    9438              :     }
    9439              : 
    9440     12881936 :   if (DECL_THUNK_P (decl))
    9441              :     {
    9442              :       /* Thunks are similar to binfos -- write the thunked-to decl and
    9443              :          then thunk-specific key info.  */
    9444            0 :       if (streaming_p ())
    9445              :         {
    9446            0 :           i (tt_thunk);
    9447            0 :           i (THUNK_FIXED_OFFSET (decl));
    9448              :         }
    9449              : 
    9450              :       tree target = decl;
    9451            0 :       while (DECL_THUNK_P (target))
    9452            0 :         target = THUNK_TARGET (target);
    9453            0 :       tree_node (target);
    9454            0 :       tree_node (THUNK_VIRTUAL_OFFSET (decl));
    9455            0 :       int tag = insert (decl);
    9456            0 :       if (streaming_p ())
    9457            0 :         dump (dumper::TREE)
    9458            0 :           && dump ("Wrote:%d thunk %N to %N", tag, DECL_NAME (decl), target);
    9459            0 :       return false;
    9460              :     }
    9461              : 
    9462     12881936 :   if (DECL_CLONED_FUNCTION_P (decl))
    9463              :     {
    9464       446633 :       tree target = get_clone_target (decl);
    9465       446633 :       if (streaming_p ())
    9466       211744 :         i (tt_clone_ref);
    9467              : 
    9468       446633 :       tree_node (target);
    9469       446633 :       tree_node (DECL_NAME (decl));
    9470       446633 :       if (DECL_VIRTUAL_P (decl))
    9471        30444 :         tree_node (DECL_VINDEX (decl));
    9472       446633 :       int tag = insert (decl);
    9473       446633 :       if (streaming_p ())
    9474       211744 :         dump (dumper::TREE)
    9475          164 :           && dump ("Wrote:%d clone %N of %N", tag, DECL_NAME (decl), target);
    9476       446633 :       return false;
    9477              :     }
    9478              : 
    9479              :   /* Everything left should be a thing that is in the entity table.
    9480              :      Mostly things that can be defined outside of their (original
    9481              :      declaration) context.  */
    9482     12435303 :   gcc_checking_assert (TREE_CODE (decl) == TEMPLATE_DECL
    9483              :                        || VAR_P (decl)
    9484              :                        || TREE_CODE (decl) == FUNCTION_DECL
    9485              :                        || TREE_CODE (decl) == TYPE_DECL
    9486              :                        || TREE_CODE (decl) == USING_DECL
    9487              :                        || TREE_CODE (decl) == CONCEPT_DECL
    9488              :                        || TREE_CODE (decl) == NAMESPACE_DECL);
    9489              : 
    9490     12435303 :   int use_tpl = -1;
    9491     12435303 :   tree ti = node_template_info (decl, use_tpl);
    9492     12435303 :   tree tpl = NULL_TREE;
    9493              : 
    9494              :   /* If this is the TEMPLATE_DECL_RESULT of a TEMPLATE_DECL, get the
    9495              :      TEMPLATE_DECL.  Note TI_TEMPLATE is not a TEMPLATE_DECL for
    9496              :      (some) friends, so we need to check that.  */
    9497              :   // FIXME: Should local friend template specializations be by value?
    9498              :   // They don't get idents so we'll never know they're imported, but I
    9499              :   // think we can only reach them from the TU that defines the
    9500              :   // befriending class?
    9501      4541749 :   if (ti && TREE_CODE (TI_TEMPLATE (ti)) == TEMPLATE_DECL
    9502     16976986 :       && DECL_TEMPLATE_RESULT (TI_TEMPLATE (ti)) == decl)
    9503              :     {
    9504              :       tpl = TI_TEMPLATE (ti);
    9505      1146441 :     partial_template:
    9506      1146441 :       if (streaming_p ())
    9507              :         {
    9508         3767 :           i (tt_template);
    9509         3767 :           dump (dumper::TREE)
    9510            9 :             && dump ("Writing implicit template %C:%N%S",
    9511            9 :                      TREE_CODE (tpl), tpl, tpl);
    9512              :         }
    9513      1146441 :       tree_node (tpl);
    9514              : 
    9515              :       /* Streaming TPL caused us to visit DECL and maybe its type,
    9516              :          if it wasn't TU-local.  */
    9517      1146441 :       if (CHECKING_P && !has_tu_local_dep (tpl))
    9518              :         {
    9519      1146414 :           gcc_checking_assert (TREE_VISITED (decl));
    9520      1146414 :           if (DECL_IMPLICIT_TYPEDEF_P (decl))
    9521       592049 :             gcc_checking_assert (TREE_VISITED (TREE_TYPE (decl)));
    9522              :         }
    9523              :       return false;
    9524              :     }
    9525              : 
    9526     11406300 :   tree ctx = CP_DECL_CONTEXT (decl);
    9527     11406300 :   depset *dep = NULL;
    9528     11406300 :   if (streaming_p ())
    9529      1909622 :     dep = dep_hash->find_dependency (decl);
    9530      9496678 :   else if (TREE_CODE (ctx) != FUNCTION_DECL
    9531       381842 :            || TREE_CODE (decl) == TEMPLATE_DECL
    9532       345771 :            || DECL_IMPLICIT_TYPEDEF_P (decl)
    9533      9805287 :            || (DECL_LANG_SPECIFIC (decl)
    9534       163483 :                && DECL_MODULE_IMPORT_P (decl)))
    9535              :     {
    9536      9188069 :       auto kind = (TREE_CODE (decl) == NAMESPACE_DECL
    9537       714077 :                    && !DECL_NAMESPACE_ALIAS (decl)
    9538      9188069 :                    ? depset::EK_NAMESPACE : depset::EK_DECL);
    9539      9188069 :       dep = dep_hash->add_dependency (decl, kind);
    9540              :     }
    9541              : 
    9542     11097691 :   if (!dep || dep->is_tu_local ())
    9543              :     {
    9544              :       /* Some internal entity of context.  Do by value.  */
    9545       595700 :       decl_value (decl, dep);
    9546       595700 :       return false;
    9547              :     }
    9548              : 
    9549     10810600 :   if (dep->get_entity_kind () == depset::EK_REDIRECT)
    9550              :     {
    9551              :       /* The DECL_TEMPLATE_RESULT of a partial specialization.
    9552              :          Write the partial specialization's template.  */
    9553       117438 :       depset *redirect = dep->deps[0];
    9554       117438 :       gcc_checking_assert (redirect->get_entity_kind () == depset::EK_PARTIAL);
    9555       117438 :       tpl = redirect->get_entity ();
    9556       117438 :       goto partial_template;
    9557              :     }
    9558              : 
    9559     10693162 :   if (streaming_p ())
    9560              :     {
    9561              :       /* Locate the entity.  */
    9562      1622791 :       unsigned index = dep->cluster;
    9563      1622791 :       unsigned import = 0;
    9564              : 
    9565      1622791 :       if (dep->is_import ())
    9566        10362 :         import = dep->section;
    9567      1612429 :       else if (CHECKING_P)
    9568              :         /* It should be what we put there.  */
    9569      1612429 :         gcc_checking_assert (index == ~import_entity_index (decl));
    9570              : 
    9571              : #if CHECKING_P
    9572        10362 :       gcc_assert (!import || importedness >= 0);
    9573              : #endif
    9574      1622791 :       i (tt_entity);
    9575      1622791 :       u (import);
    9576      1622791 :       u (index);
    9577              :     }
    9578              : 
    9579     10693162 :   int tag = insert (decl);
    9580     10693162 :   if (streaming_p () && dump (dumper::TREE))
    9581              :     {
    9582          529 :       char const *kind = "import";
    9583          529 :       module_state *from = this_module ();
    9584          529 :       if (dep->is_import ())
    9585              :         /* Rediscover the unremapped index.  */
    9586           78 :         from = import_entity_module (import_entity_index (decl));
    9587              :       else
    9588              :         {
    9589          451 :           tree o = get_originating_module_decl (decl);
    9590          451 :           o = STRIP_TEMPLATE (o);
    9591          902 :           kind = (DECL_LANG_SPECIFIC (o) && DECL_MODULE_PURVIEW_P (o)
    9592          451 :                   ? "purview" : "GMF");
    9593              :         }
    9594          529 :       dump ("Wrote %s:%d %C:%N@%M", kind,
    9595          529 :             tag, TREE_CODE (decl), decl, from);
    9596              :     }
    9597              : 
    9598     10693162 :   add_indirects (decl);
    9599              : 
    9600     10693162 :   return false;
    9601              : }
    9602              : 
    9603              : void
    9604     12768204 : trees_out::type_node (tree type)
    9605              : {
    9606     12768204 :   gcc_assert (TYPE_P (type));
    9607              : 
    9608     12768204 :   tree root = (TYPE_NAME (type)
    9609     12768204 :                ? TREE_TYPE (TYPE_NAME (type)) : TYPE_MAIN_VARIANT (type));
    9610     12768204 :   gcc_checking_assert (root);
    9611              : 
    9612     12768204 :   if (type != root)
    9613              :     {
    9614      2859517 :       if (streaming_p ())
    9615       590956 :         i (tt_variant_type);
    9616      2859517 :       tree_node (root);
    9617              : 
    9618      2859517 :       int flags = -1;
    9619              : 
    9620      2859517 :       if (TREE_CODE (type) == FUNCTION_TYPE
    9621      2859517 :           || TREE_CODE (type) == METHOD_TYPE)
    9622              :         {
    9623       671151 :           int quals = type_memfn_quals (type);
    9624       671151 :           int rquals = type_memfn_rqual (type);
    9625       671151 :           tree raises = TYPE_RAISES_EXCEPTIONS (type);
    9626       671151 :           bool late = TYPE_HAS_LATE_RETURN_TYPE (type);
    9627              : 
    9628       671151 :           if (raises != TYPE_RAISES_EXCEPTIONS (root)
    9629        21515 :               || rquals != type_memfn_rqual (root)
    9630        15439 :               || quals != type_memfn_quals (root)
    9631       686572 :               || late != TYPE_HAS_LATE_RETURN_TYPE (root))
    9632       671151 :             flags = rquals | (int (late) << 2) | (quals << 3);
    9633              :         }
    9634              :       else
    9635              :         {
    9636      2188366 :           if (TYPE_USER_ALIGN (type))
    9637        24066 :             flags = TYPE_ALIGN_RAW (type);
    9638              :         }
    9639              : 
    9640      2859517 :       if (streaming_p ())
    9641       590956 :         i (flags);
    9642              : 
    9643      2859517 :       if (flags < 0)
    9644              :         ;
    9645       695217 :       else if (TREE_CODE (type) == FUNCTION_TYPE
    9646       695217 :                || TREE_CODE (type) == METHOD_TYPE)
    9647              :         {
    9648       671151 :           tree raises = TYPE_RAISES_EXCEPTIONS (type);
    9649       671151 :           if (raises == TYPE_RAISES_EXCEPTIONS (root))
    9650        21515 :             raises = error_mark_node;
    9651       671151 :           tree_node (raises);
    9652              :         }
    9653              : 
    9654              :       /* build_type_attribute_variant creates a new TYPE_MAIN_VARIANT, so
    9655              :          variants should all have the same set of attributes.  */
    9656      2859517 :       gcc_checking_assert (TYPE_ATTRIBUTES (type)
    9657              :                            == TYPE_ATTRIBUTES (TYPE_MAIN_VARIANT (type)));
    9658              : 
    9659      2859517 :       if (streaming_p ())
    9660              :         {
    9661              :           /* Qualifiers.  */
    9662       590956 :           int rquals = cp_type_quals (root);
    9663       590956 :           int quals = cp_type_quals (type);
    9664       590956 :           if (quals == rquals)
    9665       273479 :             quals = -1;
    9666       590956 :           i (quals);
    9667              :         }
    9668              : 
    9669      2859517 :       if (ref_node (type) != WK_none)
    9670              :         {
    9671      2859517 :           int tag = insert (type);
    9672      2859517 :           if (streaming_p ())
    9673              :             {
    9674       590956 :               i (0);
    9675       590956 :               dump (dumper::TREE)
    9676          203 :                 && dump ("Wrote:%d variant type %C", tag, TREE_CODE (type));
    9677              :             }
    9678              :         }
    9679      2859517 :       return;
    9680              :     }
    9681              : 
    9682      9908687 :   if (tree name = TYPE_NAME (type))
    9683      3920115 :     if ((TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name))
    9684      3079446 :         || DECL_TEMPLATE_PARM_P (name)
    9685      2095679 :         || TREE_CODE (type) == RECORD_TYPE
    9686       364924 :         || TREE_CODE (type) == UNION_TYPE
    9687      4276986 :         || TREE_CODE (type) == ENUMERAL_TYPE)
    9688              :       {
    9689      3666108 :         gcc_checking_assert (DECL_P (name));
    9690              : 
    9691              :         /* We can meet template parms that we didn't meet in the
    9692              :            tpl_parms walk, because we're referring to a derived type
    9693              :            that was previously constructed from equivalent template
    9694              :            parms. */
    9695      3666108 :         if (streaming_p ())
    9696              :           {
    9697       252323 :             i (tt_typedef_type);
    9698       252323 :             dump (dumper::TREE)
    9699           59 :               && dump ("Writing %stypedef %C:%N",
    9700           59 :                        DECL_IMPLICIT_TYPEDEF_P (name) ? "implicit " : "",
    9701           59 :                        TREE_CODE (name), name);
    9702              :           }
    9703      3666108 :         tree_node (name);
    9704      3666108 :         if (streaming_p ())
    9705       252323 :           dump (dumper::TREE) && dump ("Wrote typedef %C:%N%S",
    9706           59 :                                        TREE_CODE (name), name, name);
    9707              : 
    9708              :         /* We'll have either visited this type or have newly discovered
    9709              :            that it's TU-local; either way we won't need to visit it again.  */
    9710      3666108 :         gcc_checking_assert (TREE_VISITED (type) || has_tu_local_dep (name));
    9711      3666108 :         return;
    9712              :       }
    9713              : 
    9714      6242579 :   if (TYPE_PTRMEMFUNC_P (type))
    9715              :     {
    9716              :       /* This is a distinct type node, masquerading as a structure. */
    9717         5513 :       tree fn_type = TYPE_PTRMEMFUNC_FN_TYPE (type);
    9718         5513 :       if (streaming_p ())
    9719         1551 :         i (tt_ptrmem_type);
    9720         5513 :       tree_node (fn_type);
    9721         5513 :       int tag = insert (type);
    9722         5513 :       if (streaming_p ())
    9723         1554 :         dump (dumper::TREE) && dump ("Written:%d ptrmem type", tag);
    9724         5513 :       return;
    9725              :     }
    9726              : 
    9727      6237066 :   if (streaming_p ())
    9728              :     {
    9729      1978774 :       u (tt_derived_type);
    9730      1978774 :       u (TREE_CODE (type));
    9731              :     }
    9732              : 
    9733      6237066 :   tree_node (TREE_TYPE (type));
    9734      6237066 :   switch (TREE_CODE (type))
    9735              :     {
    9736            0 :     default:
    9737              :       /* We should never meet a type here that is indescribable in
    9738              :          terms of other types.  */
    9739            0 :       gcc_unreachable ();
    9740              : 
    9741        94616 :     case ARRAY_TYPE:
    9742        94616 :       tree_node (TYPE_DOMAIN (type));
    9743        94616 :       if (streaming_p ())
    9744              :         /* Dependent arrays are constructed with TYPE_DEPENENT_P
    9745              :            already set.  */
    9746        30317 :         u (TYPE_DEPENDENT_P (type));
    9747              :       break;
    9748              : 
    9749              :     case COMPLEX_TYPE:
    9750              :       /* No additional data.  */
    9751              :       break;
    9752              : 
    9753           12 :     case BOOLEAN_TYPE:
    9754              :       /* A non-standard boolean type.  */
    9755           12 :       if (streaming_p ())
    9756            6 :         u (TYPE_PRECISION (type));
    9757              :       break;
    9758              : 
    9759        87746 :     case INTEGER_TYPE:
    9760        87746 :       if (TREE_TYPE (type))
    9761              :         {
    9762              :           /* A range type (representing an array domain).  */
    9763        82059 :           tree_node (TYPE_MIN_VALUE (type));
    9764        82059 :           tree_node (TYPE_MAX_VALUE (type));
    9765              :         }
    9766              :       else
    9767              :         {
    9768              :           /* A new integral type (representing a bitfield).  */
    9769         5687 :           if (streaming_p ())
    9770              :             {
    9771         1271 :               unsigned prec = TYPE_PRECISION (type);
    9772         1271 :               bool unsigned_p = TYPE_UNSIGNED (type);
    9773              : 
    9774         1271 :               u ((prec << 1) | unsigned_p);
    9775              :             }
    9776              :         }
    9777              :       break;
    9778              : 
    9779      1361378 :     case METHOD_TYPE:
    9780      1361378 :     case FUNCTION_TYPE:
    9781      1361378 :       {
    9782      1361378 :         gcc_checking_assert (type_memfn_rqual (type) == REF_QUAL_NONE);
    9783              : 
    9784      1361378 :         tree arg_types = TYPE_ARG_TYPES (type);
    9785      1361378 :         if (TREE_CODE (type) == METHOD_TYPE)
    9786              :           {
    9787       877433 :             tree_node (TREE_TYPE (TREE_VALUE (arg_types)));
    9788       877433 :             arg_types = TREE_CHAIN (arg_types);
    9789              :           }
    9790      1361378 :         tree_node (arg_types);
    9791              :       }
    9792      1361378 :       break;
    9793              : 
    9794         1616 :     case OFFSET_TYPE:
    9795         1616 :       tree_node (TYPE_OFFSET_BASETYPE (type));
    9796         1616 :       break;
    9797              : 
    9798              :     case POINTER_TYPE:
    9799              :       /* No additional data.  */
    9800              :       break;
    9801              : 
    9802      1069040 :     case REFERENCE_TYPE:
    9803      1069040 :       if (streaming_p ())
    9804       235689 :         u (TYPE_REF_IS_RVALUE (type));
    9805              :       break;
    9806              : 
    9807      1267625 :     case DECLTYPE_TYPE:
    9808      1267625 :     case TYPEOF_TYPE:
    9809      1267625 :     case DEPENDENT_OPERATOR_TYPE:
    9810      1267625 :       tree_node (TYPE_VALUES_RAW (type));
    9811      1267625 :       if (TREE_CODE (type) == DECLTYPE_TYPE)
    9812              :         /* We stash a whole bunch of things into decltype's
    9813              :            flags.  */
    9814       102749 :         if (streaming_p ())
    9815        34971 :           tree_node_bools (type);
    9816              :       break;
    9817              : 
    9818         8661 :     case TRAIT_TYPE:
    9819         8661 :       tree_node (TRAIT_TYPE_KIND_RAW (type));
    9820         8661 :       tree_node (TRAIT_TYPE_TYPE1 (type));
    9821         8661 :       tree_node (TRAIT_TYPE_TYPE2 (type));
    9822         8661 :       break;
    9823              : 
    9824              :     case TYPE_ARGUMENT_PACK:
    9825              :       /* No additional data.  */
    9826              :       break;
    9827              : 
    9828       210791 :     case TYPE_PACK_EXPANSION:
    9829       210791 :       if (streaming_p ())
    9830        85165 :         u (PACK_EXPANSION_LOCAL_P (type));
    9831       421582 :       tree_node (PACK_EXPANSION_PARAMETER_PACKS (type));
    9832       210791 :       tree_node (PACK_EXPANSION_EXTRA_ARGS (type));
    9833       210791 :       break;
    9834              : 
    9835           40 :     case PACK_INDEX_TYPE:
    9836           40 :       tree_node (PACK_INDEX_PACK (type));
    9837           40 :       tree_node (PACK_INDEX_INDEX (type));
    9838           40 :       break;
    9839              : 
    9840       255123 :     case TYPENAME_TYPE:
    9841       255123 :       {
    9842       255123 :         tree_node (TYPE_CONTEXT (type));
    9843       255123 :         tree_node (DECL_NAME (TYPE_NAME (type)));
    9844       255123 :         tree_node (TYPENAME_TYPE_FULLNAME (type));
    9845       255123 :         if (streaming_p ())
    9846        88362 :           u (get_typename_tag (type));
    9847              :         }
    9848              :       break;
    9849              : 
    9850          264 :     case UNBOUND_CLASS_TEMPLATE:
    9851          264 :       {
    9852          264 :         tree decl = TYPE_NAME (type);
    9853          264 :         tree_node (DECL_CONTEXT (decl));
    9854          264 :         tree_node (DECL_NAME (decl));
    9855          264 :         tree_node (DECL_TEMPLATE_PARMS (decl));
    9856              :       }
    9857          264 :       break;
    9858              : 
    9859           42 :     case VECTOR_TYPE:
    9860           42 :       if (streaming_p ())
    9861              :         {
    9862           21 :           poly_uint64 nunits = TYPE_VECTOR_SUBPARTS (type);
    9863           42 :           for (unsigned ix = 0; ix != NUM_POLY_INT_COEFFS; ix++)
    9864           21 :             wu (nunits.coeffs[ix]);
    9865              :         }
    9866              :       break;
    9867              : 
    9868              :     case META_TYPE:
    9869              :       /* No additional data.  */
    9870              :       break;
    9871              : 
    9872            8 :     case SPLICE_SCOPE:
    9873            8 :       if (streaming_p ())
    9874            4 :         u (SPLICE_SCOPE_TYPE_P (type));
    9875            8 :       tree_node (SPLICE_SCOPE_EXPR (type));
    9876            8 :       break;
    9877              :     }
    9878              : 
    9879      6237066 :   tree_node (TYPE_ATTRIBUTES (type));
    9880              : 
    9881              :   /* We may have met the type during emitting the above.  */
    9882      6237066 :   if (ref_node (type) != WK_none)
    9883              :     {
    9884      5654628 :       int tag = insert (type);
    9885      5654628 :       if (streaming_p ())
    9886              :         {
    9887      1706336 :           i (0);
    9888      1706336 :           dump (dumper::TREE)
    9889          558 :             && dump ("Wrote:%d derived type %C", tag, TREE_CODE (type));
    9890              :         }
    9891              :     }
    9892              : 
    9893              :   return;
    9894              : }
    9895              : 
    9896              : /* T is (mostly*) a non-mergeable node that must be written by value.
    9897              :    The mergeable case is a BINFO, which are as-if DECLSs.   */
    9898              : 
    9899              : void
    9900     38991639 : trees_out::tree_value (tree t)
    9901              : {
    9902              :   /* We should never be writing a type by value.  tree_type should
    9903              :      have streamed it, or we're going via its TYPE_DECL.  */
    9904     38991639 :   gcc_checking_assert (!TYPE_P (t));
    9905              : 
    9906     38991639 :   if (DECL_P (t))
    9907              :     /* No template, type, var or function, except anonymous
    9908              :        non-context vars and types.  */
    9909      1017302 :     gcc_checking_assert ((TREE_CODE (t) != TEMPLATE_DECL
    9910              :                           && (TREE_CODE (t) != TYPE_DECL
    9911              :                               || (DECL_ARTIFICIAL (t) && !DECL_CONTEXT (t)))
    9912              :                           && (TREE_CODE (t) != VAR_DECL
    9913              :                               || ((!DECL_NAME (t)
    9914              :                                    || IDENTIFIER_INTERNAL_P (DECL_NAME (t)))
    9915              :                                   && !DECL_CONTEXT (t)))
    9916              :                           && TREE_CODE (t) != FUNCTION_DECL));
    9917              : 
    9918     58889106 :   if (is_initial_scan () && EXPR_P (t))
    9919      7905192 :     dep_hash->add_dependent_adl_entities (t);
    9920              : 
    9921     38991639 :   if (streaming_p ())
    9922              :     {
    9923              :       /* A new node -> tt_node.  */
    9924     15476053 :       tree_val_count++;
    9925     15476053 :       i (tt_node);
    9926     15476053 :       start (t);
    9927     15476053 :       tree_node_bools (t);
    9928              :     }
    9929              : 
    9930     38991639 :   if (TREE_CODE (t) == TREE_BINFO)
    9931              :     /* Binfos are decl-like and need merging information.  */
    9932       272930 :     binfo_mergeable (t);
    9933              : 
    9934     38991639 :   int tag = insert (t, WK_value);
    9935     38991639 :   if (streaming_p ())
    9936     15476053 :     dump (dumper::TREE)
    9937         2823 :       && dump ("Writing tree:%d %C:%N", tag, TREE_CODE (t), t);
    9938              : 
    9939     38991639 :   int type_tag = 0;
    9940     38991639 :   tree type = NULL_TREE;
    9941     38991639 :   if (TREE_CODE (t) == TYPE_DECL)
    9942              :     {
    9943           28 :       type = TREE_TYPE (t);
    9944              : 
    9945              :       /* We only support a limited set of features for uncontexted types;
    9946              :          these are typically types created in the language-independent
    9947              :          parts of the frontend (such as ubsan).  */
    9948           28 :       gcc_checking_assert (RECORD_OR_UNION_TYPE_P (type)
    9949              :                            && TYPE_MAIN_VARIANT (type) == type
    9950              :                            && TYPE_NAME (type) == t
    9951              :                            && TYPE_STUB_DECL (type) == t
    9952              :                            && !TYPE_VFIELD (type)
    9953              :                            && !TYPE_BINFO (type)
    9954              :                            && !CLASS_TYPE_P (type));
    9955              : 
    9956           28 :       if (streaming_p ())
    9957              :         {
    9958           14 :           start (type);
    9959           14 :           tree_node_bools (type);
    9960              :         }
    9961              : 
    9962           28 :       type_tag = insert (type, WK_value);
    9963           28 :       if (streaming_p ())
    9964           14 :         dump (dumper::TREE)
    9965            0 :           && dump ("Writing type: %d %C:%N", type_tag,
    9966            0 :                    TREE_CODE (type), type);
    9967              :     }
    9968              : 
    9969     38991639 :   tree_node_vals (t);
    9970              : 
    9971     38991639 :   if (type)
    9972              :     {
    9973           28 :       tree_node_vals (type);
    9974           28 :       tree_node (TYPE_SIZE (type));
    9975           28 :       tree_node (TYPE_SIZE_UNIT (type));
    9976           28 :       chained_decls (TYPE_FIELDS (type));
    9977           28 :       if (streaming_p ())
    9978           14 :         dump (dumper::TREE)
    9979            0 :           && dump ("Written type:%d %C:%N", type_tag, TREE_CODE (type), type);
    9980              :     }
    9981              : 
    9982              :   /* For uncontexted VAR_DECLs we need to stream the definition so that
    9983              :      importers can recreate their value.  */
    9984     38991639 :   if (TREE_CODE (t) == VAR_DECL)
    9985              :     {
    9986          788 :       gcc_checking_assert (!DECL_NONTRIVIALLY_INITIALIZED_P (t));
    9987          788 :       tree_node (DECL_INITIAL (t));
    9988              :     }
    9989              : 
    9990     38991639 :   if (streaming_p ())
    9991     15478876 :     dump (dumper::TREE) && dump ("Written tree:%d %C:%N", tag, TREE_CODE (t), t);
    9992     38991639 : }
    9993              : 
    9994              : tree
    9995     12097339 : trees_in::tree_value ()
    9996              : {
    9997     12097339 :   tree t = start ();
    9998     12097339 :   if (!t || !tree_node_bools (t))
    9999            0 :     return NULL_TREE;
   10000              : 
   10001     12097339 :   tree existing = t;
   10002     12097339 :   if (TREE_CODE (t) == TREE_BINFO)
   10003              :     {
   10004        92023 :       tree type;
   10005        92023 :       unsigned ix = binfo_mergeable (&type);
   10006        92023 :       if (TYPE_BINFO (type))
   10007              :         {
   10008              :           /* We already have a definition, this must be a duplicate.  */
   10009        41470 :           dump (dumper::MERGE)
   10010          271 :             && dump ("Deduping binfo %N[%u]", type, ix);
   10011        41470 :           existing = TYPE_BINFO (type);
   10012        56773 :           while (existing && ix--)
   10013        15303 :             existing = TREE_CHAIN (existing);
   10014        41470 :           if (existing)
   10015        41470 :             register_duplicate (t, existing);
   10016              :           else
   10017              :             /* Error, mismatch -- diagnose in read_class_def's
   10018              :                checking.  */
   10019              :             existing = t;
   10020              :         }
   10021              :     }
   10022              : 
   10023              :   /* Insert into map.  */
   10024     12097339 :   int tag = insert (existing);
   10025     12097339 :   dump (dumper::TREE)
   10026         3677 :     && dump ("Reading tree:%d %C", tag, TREE_CODE (t));
   10027              : 
   10028     12097339 :   int type_tag = 0;
   10029     12097339 :   tree type = NULL_TREE;
   10030     12097339 :   if (TREE_CODE (t) == TYPE_DECL)
   10031              :     {
   10032           14 :       type = start ();
   10033           14 :       if (!type || !tree_node_bools (type))
   10034              :         t = NULL_TREE;
   10035              : 
   10036           14 :       type_tag = insert (type);
   10037           14 :       if (t)
   10038           14 :         dump (dumper::TREE)
   10039            0 :           && dump ("Reading type:%d %C", type_tag, TREE_CODE (type));
   10040              :     }
   10041              : 
   10042              :   if (!t)
   10043              :     {
   10044            0 : bail:
   10045            0 :       back_refs[~tag] = NULL_TREE;
   10046            0 :       if (type_tag)
   10047            0 :         back_refs[~type_tag] = NULL_TREE;
   10048            0 :       set_overrun ();
   10049            0 :       return NULL_TREE;
   10050              :     }
   10051              : 
   10052     12097339 :   if (!tree_node_vals (t))
   10053            0 :     goto bail;
   10054              : 
   10055     12097339 :   if (type)
   10056              :     {
   10057           14 :       if (!tree_node_vals (type))
   10058            0 :         goto bail;
   10059              : 
   10060           14 :       TYPE_SIZE (type) = tree_node ();
   10061           14 :       TYPE_SIZE_UNIT (type) = tree_node ();
   10062           14 :       TYPE_FIELDS (type) = chained_decls ();
   10063           14 :       if (get_overrun ())
   10064            0 :         goto bail;
   10065              : 
   10066           14 :       dump (dumper::TREE)
   10067            0 :         && dump ("Read type:%d %C:%N", type_tag, TREE_CODE (type), type);
   10068              :     }
   10069              : 
   10070     12097339 :   if (TREE_CODE (t) == VAR_DECL)
   10071              :     {
   10072          359 :       DECL_INITIAL (t) = tree_node ();
   10073          359 :       if (TREE_STATIC (t))
   10074            8 :         varpool_node::finalize_decl (t);
   10075              :     }
   10076              : 
   10077     12097339 :   if (TREE_CODE (t) == LAMBDA_EXPR
   10078     12097339 :       && CLASSTYPE_LAMBDA_EXPR (TREE_TYPE (t)))
   10079              :     {
   10080         1937 :       existing = CLASSTYPE_LAMBDA_EXPR (TREE_TYPE (t));
   10081         1937 :       back_refs[~tag] = existing;
   10082              :     }
   10083              : 
   10084     12101016 :   dump (dumper::TREE) && dump ("Read tree:%d %C:%N", tag, TREE_CODE (t), t);
   10085              : 
   10086     12097339 :   if (TREE_CODE (existing) == INTEGER_CST && !TREE_OVERFLOW (existing))
   10087              :     {
   10088       602137 :       existing = cache_integer_cst (t, true);
   10089       602137 :       back_refs[~tag] = existing;
   10090              :     }
   10091              : 
   10092              :   return existing;
   10093              : }
   10094              : 
   10095              : /* Whether DECL has a TU-local dependency in the hash.  */
   10096              : 
   10097              : bool
   10098      1146802 : trees_out::has_tu_local_dep (tree decl) const
   10099              : {
   10100              :   /* Only the contexts of fields or enums remember that they're
   10101              :      TU-local.  */
   10102      1146802 :   if (DECL_CONTEXT (decl)
   10103      1146802 :       && (TREE_CODE (decl) == FIELD_DECL
   10104      1146799 :           || TREE_CODE (decl) == CONST_DECL))
   10105            3 :     decl = TYPE_NAME (DECL_CONTEXT (decl));
   10106              : 
   10107      1146802 :   depset *dep = dep_hash->find_dependency (decl);
   10108      1146802 :   if (!dep)
   10109              :     {
   10110              :       /* This might be the DECL_TEMPLATE_RESULT of a TEMPLATE_DECL
   10111              :          which we found was TU-local and gave up early.  */
   10112        14395 :       int use_tpl = -1;
   10113        14395 :       if (tree ti = node_template_info (decl, use_tpl))
   10114         2344 :         dep = dep_hash->find_dependency (TI_TEMPLATE (ti));
   10115              :     }
   10116              : 
   10117      1146802 :   return dep && dep->is_tu_local ();
   10118              : }
   10119              : 
   10120              : /* If T depends on a TU-local entity, return that decl.  */
   10121              : 
   10122              : tree
   10123          395 : trees_out::find_tu_local_decl (tree t)
   10124              : {
   10125              :   /* We need to have walked all deps first before we can check.  */
   10126          395 :   gcc_checking_assert (!is_initial_scan ());
   10127              : 
   10128          951 :   auto walker = [](tree *tp, int *walk_subtrees, void *data) -> tree
   10129              :     {
   10130          556 :       auto self = (trees_out *)data;
   10131              : 
   10132          556 :       tree decl = NULL_TREE;
   10133          556 :       if (TYPE_P (*tp))
   10134              :         {
   10135              :           /* A PMF type is a record type, which we otherwise wouldn't walk;
   10136              :              return whether the function type is TU-local.  */
   10137          370 :           if (TYPE_PTRMEMFUNC_P (*tp))
   10138              :             {
   10139            3 :               *walk_subtrees = 0;
   10140            3 :               return self->find_tu_local_decl (TYPE_PTRMEMFUNC_FN_TYPE (*tp));
   10141              :             }
   10142              :           else
   10143          367 :             decl = TYPE_MAIN_DECL (*tp);
   10144              :         }
   10145          186 :       else if (DECL_P (*tp))
   10146              :         decl = *tp;
   10147              : 
   10148          373 :       if (decl)
   10149              :         {
   10150              :           /* We found a DECL, this will tell us whether we're TU-local.  */
   10151           59 :           *walk_subtrees = 0;
   10152           59 :           return self->has_tu_local_dep (decl) ? decl : NULL_TREE;
   10153              :         }
   10154              :       return NULL_TREE;
   10155              :     };
   10156              : 
   10157              :   /* We need to walk without duplicates so that we step into the pointed-to
   10158              :      types of array types.  */
   10159          395 :   return cp_walk_tree_without_duplicates (&t, walker, this);
   10160              : }
   10161              : 
   10162              : /* Get the name for TU-local decl T to be used in diagnostics.  */
   10163              : 
   10164              : static tree
   10165          206 : name_for_tu_local_decl (tree t)
   10166              : {
   10167          206 :   int flags = (TFF_SCOPE | TFF_DECL_SPECIFIERS);
   10168          206 :   const char *str = decl_as_string (t, flags);
   10169          206 :   return get_identifier (str);
   10170              : }
   10171              : 
   10172              : /* Stream out tree node T.  We automatically create local back
   10173              :    references, which is essentially a single pass lisp
   10174              :    self-referential structure pretty-printer.  */
   10175              : 
   10176              : void
   10177    332205980 : trees_out::tree_node (tree t)
   10178              : {
   10179    332205980 :   dump.indent ();
   10180    332205980 :   walk_kind ref = ref_node (t);
   10181    332205980 :   if (ref == WK_none)
   10182    253897810 :     goto done;
   10183              : 
   10184              :   /* Find TU-local entities and intercept streaming to instead write a
   10185              :      placeholder value; this way we don't need to emit such decls.
   10186              :      We only need to do this when writing a definition of an entity
   10187              :      that we know names a TU-local entity.  */
   10188     91609660 :   if (!is_initial_scan () && writing_local_entities)
   10189              :     {
   10190          952 :       tree local_decl = NULL_TREE;
   10191          952 :       if (DECL_P (t) && has_tu_local_dep (t))
   10192              :         local_decl = t;
   10193              :       /* Consider a type to be TU-local if it refers to any TU-local decl,
   10194              :          no matter how deep.
   10195              : 
   10196              :          This worsens diagnostics slightly, as we often no longer point
   10197              :          directly to the at-fault entity when instantiating.  However, this
   10198              :          reduces the module size slightly and means that much less of pt.cc
   10199              :          needs to know about us.  */
   10200          848 :       else if (TYPE_P (t))
   10201          142 :         local_decl = find_tu_local_decl (t);
   10202          706 :       else if (EXPR_P (t))
   10203          250 :         local_decl = find_tu_local_decl (TREE_TYPE (t));
   10204              : 
   10205          496 :       if (local_decl)
   10206              :         {
   10207          158 :           int tag = insert (t, WK_value);
   10208          158 :           if (streaming_p ())
   10209              :             {
   10210          158 :               tu_local_count++;
   10211          158 :               i (tt_tu_local);
   10212          158 :               dump (dumper::TREE)
   10213            0 :                 && dump ("Writing TU-local entity:%d %C:%N",
   10214            0 :                          tag, TREE_CODE (t), t);
   10215              :             }
   10216          158 :           tree_node (name_for_tu_local_decl (local_decl));
   10217          158 :           if (state)
   10218          158 :             state->write_location (*this, DECL_SOURCE_LOCATION (local_decl));
   10219          158 :           goto done;
   10220              :         }
   10221              :     }
   10222              : 
   10223     78308012 :   if (ref != WK_normal)
   10224      1832735 :     goto skip_normal;
   10225              : 
   10226     76475277 :   if (TREE_CODE (t) == IDENTIFIER_NODE)
   10227              :     {
   10228              :       /* An identifier node -> tt_id, tt_conv_id, tt_anon_id, tt_lambda_id,
   10229              :          tt_internal_id.  */
   10230      9101252 :       int code = tt_id;
   10231      9101252 :       if (IDENTIFIER_ANON_P (t))
   10232        35394 :         code = IDENTIFIER_LAMBDA_P (t) ? tt_lambda_id : tt_anon_id;
   10233      9065858 :       else if (IDENTIFIER_INTERNAL_P (t))
   10234              :         code = tt_internal_id;
   10235      9065844 :       else if (IDENTIFIER_CONV_OP_P (t))
   10236        13730 :         code = tt_conv_id;
   10237              : 
   10238      9101252 :       if (streaming_p ())
   10239      1776556 :         i (code);
   10240              : 
   10241      9101252 :       if (code == tt_conv_id)
   10242              :         {
   10243        13730 :           tree type = TREE_TYPE (t);
   10244        13730 :           gcc_checking_assert (type || t == conv_op_identifier);
   10245        13730 :           tree_node (type);
   10246              :         }
   10247      9087522 :       else if (code == tt_id && streaming_p ())
   10248      1761592 :         str (IDENTIFIER_POINTER (t), IDENTIFIER_LENGTH (t));
   10249      7325930 :       else if (code == tt_internal_id && streaming_p ())
   10250            7 :         str (prefix_for_internal_label (t));
   10251              : 
   10252      9101252 :       int tag = insert (t);
   10253      9101252 :       if (streaming_p ())
   10254              :         {
   10255              :           /* We know the ordering of the 5 id tags.  */
   10256      1776556 :           static const char *const kinds[] =
   10257              :             {"", "conv_op ", "anon ", "lambda ", "internal "};
   10258      1776556 :           dump (dumper::TREE)
   10259         1074 :             && dump ("Written:%d %sidentifier:%N", tag,
   10260         1071 :                      kinds[code - tt_id],
   10261            3 :                      code == tt_conv_id ? TREE_TYPE (t) : t);
   10262              :         }
   10263      9101252 :       goto done;
   10264              :     }
   10265              : 
   10266     67374025 :   if (TREE_CODE (t) == TREE_BINFO)
   10267              :     {
   10268              :       /* A BINFO -> tt_binfo.
   10269              :          We must do this by reference.  We stream the binfo tree
   10270              :          itself when streaming its owning RECORD_TYPE.  That we got
   10271              :          here means the dominating type is not in this SCC.  */
   10272        78624 :       if (streaming_p ())
   10273         2561 :         i (tt_binfo);
   10274        78624 :       binfo_mergeable (t);
   10275        78624 :       gcc_checking_assert (!TREE_VISITED (t));
   10276        78624 :       int tag = insert (t);
   10277        78624 :       if (streaming_p ())
   10278         2561 :         dump (dumper::TREE) && dump ("Inserting binfo:%d %N", tag, t);
   10279        78624 :       goto done;
   10280              :     }
   10281              : 
   10282     67295401 :   if (TREE_CODE (t) == INTEGER_CST
   10283      4797474 :       && !TREE_OVERFLOW (t)
   10284     72092875 :       && TREE_CODE (TREE_TYPE (t)) == ENUMERAL_TYPE)
   10285              :     {
   10286              :       /* An integral constant of enumeral type.  See if it matches one
   10287              :          of the enumeration values.  */
   10288        46896 :       for (tree values = TYPE_VALUES (TREE_TYPE (t));
   10289       971062 :            values; values = TREE_CHAIN (values))
   10290              :         {
   10291       968692 :           tree decl = TREE_VALUE (values);
   10292       968692 :           if (tree_int_cst_equal (DECL_INITIAL (decl), t))
   10293              :             {
   10294        44526 :               if (streaming_p ())
   10295        13084 :                 u (tt_enum_value);
   10296        44526 :               tree_node (decl);
   10297        44568 :               dump (dumper::TREE) && dump ("Written enum value %N", decl);
   10298        44526 :               goto done;
   10299              :             }
   10300              :         }
   10301              :       /* It didn't match.  We'll write it a an explicit INTEGER_CST
   10302              :          node.  */
   10303              :     }
   10304              : 
   10305     67250875 :   if (TYPE_P (t))
   10306              :     {
   10307     12768204 :       type_node (t);
   10308     12768204 :       goto done;
   10309              :     }
   10310              : 
   10311     54482671 :   if (DECL_P (t))
   10312              :     {
   10313     16817246 :       if (DECL_TEMPLATE_PARM_P (t))
   10314              :         {
   10315      2601210 :           tpl_parm_value (t);
   10316      2601210 :           goto done;
   10317              :         }
   10318              : 
   10319     14216036 :       if (!DECL_CONTEXT (t))
   10320              :         {
   10321              :           /* There are a few cases of decls with no context.  We'll write
   10322              :              these by value, but first assert they are cases we expect.  */
   10323        30685 :           gcc_checking_assert (ref == WK_normal);
   10324        30685 :           switch (TREE_CODE (t))
   10325              :             {
   10326            0 :             default: gcc_unreachable ();
   10327              : 
   10328        11728 :             case LABEL_DECL:
   10329              :               /* CASE_LABEL_EXPRs contain uncontexted LABEL_DECLs.  */
   10330        11728 :               gcc_checking_assert (!DECL_NAME (t));
   10331              :               break;
   10332              : 
   10333          788 :             case VAR_DECL:
   10334              :               /* AGGR_INIT_EXPRs cons up anonymous uncontexted VAR_DECLs,
   10335              :                  and internal vars are created by sanitizers and
   10336              :                  __builtin_source_location.  */
   10337          788 :               gcc_checking_assert ((!DECL_NAME (t)
   10338              :                                     || IDENTIFIER_INTERNAL_P (DECL_NAME (t)))
   10339              :                                    && DECL_ARTIFICIAL (t));
   10340              :               break;
   10341              : 
   10342        18141 :             case PARM_DECL:
   10343              :               /* REQUIRES_EXPRs have a chain of uncontexted PARM_DECLS,
   10344              :                  and an implicit this parm in an NSDMI has no context.  */
   10345        18141 :               gcc_checking_assert (CONSTRAINT_VAR_P (t)
   10346              :                                    || DECL_NAME (t) == this_identifier);
   10347              :               break;
   10348              : 
   10349           28 :             case TYPE_DECL:
   10350              :               /* Some parts of the compiler need internal struct types;
   10351              :                  these types may not have an appropriate context to use.
   10352              :                  Walk the whole type (including its definition) by value.  */
   10353           28 :               gcc_checking_assert (DECL_ARTIFICIAL (t)
   10354              :                                    && TYPE_ARTIFICIAL (TREE_TYPE (t))
   10355              :                                    && RECORD_OR_UNION_TYPE_P (TREE_TYPE (t))
   10356              :                                    && !CLASS_TYPE_P (TREE_TYPE (t)));
   10357              :               break;
   10358              :             }
   10359        30685 :           mark_declaration (t, has_definition (t));
   10360        30685 :           goto by_value;
   10361              :         }
   10362              :     }
   10363              : 
   10364     37665425 :  skip_normal:
   10365     53683511 :   if (DECL_P (t) && !decl_node (t, ref))
   10366     14722557 :     goto done;
   10367              : 
   10368              :   /* Otherwise by value */
   10369     38991639 :  by_value:
   10370     38991639 :   tree_value (t);
   10371              : 
   10372    332205980 :  done:
   10373              :   /* And, breath out.  */
   10374    332205980 :   dump.outdent ();
   10375    332205980 : }
   10376              : 
   10377              : /* Stream in a tree node.  */
   10378              : 
   10379              : tree
   10380     98245293 : trees_in::tree_node (bool is_use)
   10381              : {
   10382     98245293 :   if (get_overrun ())
   10383              :     return NULL_TREE;
   10384              : 
   10385     98245293 :   dump.indent ();
   10386     98245293 :   int tag = i ();
   10387     98245293 :   tree res = NULL_TREE;
   10388     98245293 :   switch (tag)
   10389              :     {
   10390     32299593 :     default:
   10391              :       /* backref, pull it out of the map.  */
   10392     32299593 :       res = back_ref (tag);
   10393     32299593 :       break;
   10394              : 
   10395              :     case tt_null:
   10396              :       /* NULL_TREE.  */
   10397              :       break;
   10398              : 
   10399          158 :     case tt_tu_local:
   10400          158 :       {
   10401              :         /* A translation-unit-local entity.  */
   10402          158 :         res = make_node (TU_LOCAL_ENTITY);
   10403          158 :         int tag = insert (res);
   10404              : 
   10405          158 :         TU_LOCAL_ENTITY_NAME (res) = tree_node ();
   10406          158 :         TU_LOCAL_ENTITY_LOCATION (res) = state->read_location (*this);
   10407          158 :         dump (dumper::TREE) && dump ("Read TU-local entity:%d %N", tag, res);
   10408              :       }
   10409              :       break;
   10410              : 
   10411      7571081 :     case tt_fixed:
   10412              :       /* A fixed ref, find it in the fixed_ref array.   */
   10413      7571081 :       {
   10414      7571081 :         unsigned fix = u ();
   10415      7571081 :         if (fix < (*fixed_trees).length ())
   10416              :           {
   10417      7571081 :             res = (*fixed_trees)[fix];
   10418      7571081 :             dump (dumper::TREE) && dump ("Read fixed:%u %C:%N%S", fix,
   10419         5046 :                                          TREE_CODE (res), res, res);
   10420              :           }
   10421              : 
   10422      7571081 :         if (!res)
   10423            0 :           set_overrun ();
   10424              :       }
   10425              :       break;
   10426              : 
   10427        83144 :     case tt_parm:
   10428        83144 :       {
   10429        83144 :         tree fn = tree_node ();
   10430        83144 :         if (fn && TREE_CODE (fn) == FUNCTION_DECL)
   10431        83144 :           res = tree_node ();
   10432        83144 :         if (res)
   10433        83144 :           dump (dumper::TREE)
   10434           21 :             && dump ("Read %s reference %N",
   10435           21 :                      TREE_CODE (res) == PARM_DECL ? "parameter" : "result",
   10436              :                      res);
   10437              :       }
   10438              :       break;
   10439              : 
   10440     12097339 :     case tt_node:
   10441              :       /* A new node.  Stream it in.  */
   10442     12097339 :       res = tree_value ();
   10443     12097339 :       break;
   10444              : 
   10445      1229031 :     case tt_decl:
   10446              :       /* A new decl.  Stream it in.  */
   10447      1229031 :       res = decl_value ();
   10448      1229031 :       break;
   10449              : 
   10450       431144 :     case tt_tpl_parm:
   10451              :       /* A template parameter.  Stream it in.  */
   10452       431144 :       res = tpl_parm_value ();
   10453       431144 :       break;
   10454              : 
   10455      1249242 :     case tt_id:
   10456              :       /* An identifier node.  */
   10457      1249242 :       {
   10458      1249242 :         size_t l;
   10459      1249242 :         const char *chars = str (&l);
   10460      1249242 :         res = get_identifier_with_length (chars, l);
   10461      1249242 :         int tag = insert (res);
   10462      1249242 :         dump (dumper::TREE)
   10463         1488 :           && dump ("Read identifier:%d %N", tag, res);
   10464              :       }
   10465      1249242 :       break;
   10466              : 
   10467         3342 :     case tt_conv_id:
   10468              :       /* A conversion operator.  Get the type and recreate the
   10469              :          identifier.  */
   10470         3342 :       {
   10471         3342 :         tree type = tree_node ();
   10472         3342 :         if (!get_overrun ())
   10473              :           {
   10474         3342 :             res = type ? make_conv_op_name (type) : conv_op_identifier;
   10475         3342 :             int tag = insert (res);
   10476         3342 :             dump (dumper::TREE)
   10477           27 :               && dump ("Created conv_op:%d %S for %N", tag, res, type);
   10478              :           }
   10479              :       }
   10480              :       break;
   10481              : 
   10482         6678 :     case tt_anon_id:
   10483         6678 :     case tt_lambda_id:
   10484              :       /* An anonymous or lambda id.  */
   10485         6678 :       {
   10486         6678 :         res = make_anon_name ();
   10487         6678 :         if (tag == tt_lambda_id)
   10488         3938 :           IDENTIFIER_LAMBDA_P (res) = true;
   10489         6678 :         int tag = insert (res);
   10490         6678 :         dump (dumper::TREE)
   10491            3 :           && dump ("Read %s identifier:%d %N",
   10492            3 :                    IDENTIFIER_LAMBDA_P (res) ? "lambda" : "anon", tag, res);
   10493              :       }
   10494              :       break;
   10495              : 
   10496            8 :     case tt_internal_id:
   10497              :       /* An internal label.  */
   10498            8 :       {
   10499            8 :         const char *prefix = str ();
   10500            8 :         res = generate_internal_label (prefix);
   10501            8 :         int tag = insert (res);
   10502            8 :         dump (dumper::TREE)
   10503            1 :           && dump ("Read internal identifier:%d %N", tag, res);
   10504              :       }
   10505              :       break;
   10506              : 
   10507       196339 :     case tt_typedef_type:
   10508       196339 :       res = tree_node ();
   10509       196339 :       if (res)
   10510              :         {
   10511       196339 :           dump (dumper::TREE)
   10512           74 :             && dump ("Read %stypedef %C:%N",
   10513           74 :                      DECL_IMPLICIT_TYPEDEF_P (res) ? "implicit " : "",
   10514           74 :                      TREE_CODE (res), res);
   10515       196339 :           if (TREE_CODE (res) != TU_LOCAL_ENTITY)
   10516       196338 :             res = TREE_TYPE (res);
   10517              :         }
   10518              :       break;
   10519              : 
   10520      1506889 :     case tt_derived_type:
   10521              :       /* A type derived from some other type.  */
   10522      1506889 :       {
   10523      1506889 :         enum tree_code code = tree_code (u ());
   10524      1506889 :         res = tree_node ();
   10525              : 
   10526      1506889 :         switch (code)
   10527              :           {
   10528            0 :           default:
   10529            0 :             set_overrun ();
   10530            0 :             break;
   10531              : 
   10532        21370 :           case ARRAY_TYPE:
   10533        21370 :             {
   10534        21370 :               tree elt_type = res;
   10535        21370 :               tree domain = tree_node ();
   10536        21370 :               int dep = u ();
   10537        21370 :               if (!get_overrun ())
   10538              :                 {
   10539        21370 :                   res = build_cplus_array_type (elt_type, domain, dep);
   10540              :                   /* If we're an array of an incomplete imported type,
   10541              :                      save it for post-processing so that we can attempt
   10542              :                      to complete the type later if it will get a
   10543              :                      definition later in the cluster.  */
   10544        21370 :                   if (!dep
   10545        18580 :                       && !COMPLETE_TYPE_P (elt_type)
   10546           36 :                       && CLASS_TYPE_P (elt_type)
   10547           36 :                       && DECL_LANG_SPECIFIC (TYPE_NAME (elt_type))
   10548        21406 :                       && DECL_MODULE_IMPORT_P (TYPE_NAME (elt_type)))
   10549           36 :                     post_process_type (res);
   10550              :                 }
   10551              :             }
   10552              :             break;
   10553              : 
   10554          265 :           case COMPLEX_TYPE:
   10555          265 :             if (!get_overrun ())
   10556          265 :               res = build_complex_type (res);
   10557              :             break;
   10558              : 
   10559            9 :           case BOOLEAN_TYPE:
   10560            9 :             {
   10561            9 :               unsigned precision = u ();
   10562            9 :               if (!get_overrun ())
   10563            9 :                 res = build_nonstandard_boolean_type (precision);
   10564              :             }
   10565              :             break;
   10566              : 
   10567        19599 :           case INTEGER_TYPE:
   10568        19599 :             if (res)
   10569              :               {
   10570              :                 /* A range type (representing an array domain).  */
   10571        18551 :                 tree min = tree_node ();
   10572        18551 :                 tree max = tree_node ();
   10573              : 
   10574        18551 :                 if (!get_overrun ())
   10575        18551 :                   res = build_range_type (res, min, max);
   10576              :               }
   10577              :             else
   10578              :               {
   10579              :                 /* A new integral type (representing a bitfield).  */
   10580         1048 :                 unsigned enc = u ();
   10581         1048 :                 if (!get_overrun ())
   10582         1048 :                   res = build_nonstandard_integer_type (enc >> 1, enc & 1);
   10583              :               }
   10584              :             break;
   10585              : 
   10586       426412 :           case FUNCTION_TYPE:
   10587       426412 :           case METHOD_TYPE:
   10588       426412 :             {
   10589       426412 :               tree klass =  code == METHOD_TYPE ? tree_node () : NULL_TREE;
   10590       426412 :               tree args = tree_node ();
   10591       426412 :               if (!get_overrun ())
   10592              :                 {
   10593       426412 :                   if (klass)
   10594       271106 :                     res = build_method_type_directly (klass, res, args);
   10595              :                   else
   10596       155306 :                     res = cp_build_function_type (res, args);
   10597              :                 }
   10598              :             }
   10599              :             break;
   10600              : 
   10601          264 :           case OFFSET_TYPE:
   10602          264 :             {
   10603          264 :               tree base = tree_node ();
   10604          264 :               if (!get_overrun ())
   10605          264 :                 res = build_offset_type (base, res);
   10606              :             }
   10607              :             break;
   10608              : 
   10609       205307 :           case POINTER_TYPE:
   10610       205307 :             if (!get_overrun ())
   10611       205307 :               res = build_pointer_type (res);
   10612              :             break;
   10613              : 
   10614       177067 :           case REFERENCE_TYPE:
   10615       177067 :             {
   10616       177067 :               bool rval = bool (u ());
   10617       177067 :               if (!get_overrun ())
   10618       177067 :                 res = cp_build_reference_type (res, rval);
   10619              :             }
   10620              :             break;
   10621              : 
   10622       458487 :           case DECLTYPE_TYPE:
   10623       458487 :           case TYPEOF_TYPE:
   10624       458487 :           case DEPENDENT_OPERATOR_TYPE:
   10625       458487 :             {
   10626       458487 :               tree expr = tree_node ();
   10627       458487 :               if (!get_overrun ())
   10628              :                 {
   10629       458487 :                   res = cxx_make_type (code);
   10630       458487 :                   TYPE_VALUES_RAW (res) = expr;
   10631       458487 :                   if (code == DECLTYPE_TYPE)
   10632        21846 :                     tree_node_bools (res);
   10633       458487 :                   SET_TYPE_STRUCTURAL_EQUALITY (res);
   10634              :                 }
   10635              :             }
   10636              :             break;
   10637              : 
   10638         2307 :           case TRAIT_TYPE:
   10639         2307 :             {
   10640         2307 :               tree kind = tree_node ();
   10641         2307 :               tree type1 = tree_node ();
   10642         2307 :               tree type2 = tree_node ();
   10643         2307 :               if (!get_overrun ())
   10644              :                 {
   10645         2307 :                   res = cxx_make_type (TRAIT_TYPE);
   10646         2307 :                   TRAIT_TYPE_KIND_RAW (res) = kind;
   10647         2307 :                   TRAIT_TYPE_TYPE1 (res) = type1;
   10648         2307 :                   TRAIT_TYPE_TYPE2 (res) = type2;
   10649         2307 :                   SET_TYPE_STRUCTURAL_EQUALITY (res);
   10650              :                 }
   10651              :             }
   10652              :             break;
   10653              : 
   10654        62237 :           case TYPE_ARGUMENT_PACK:
   10655        62237 :             if (!get_overrun ())
   10656              :               {
   10657        62237 :                 tree pack = cxx_make_type (TYPE_ARGUMENT_PACK);
   10658        62237 :                 ARGUMENT_PACK_ARGS (pack) = res;
   10659        62237 :                 res = pack;
   10660              :               }
   10661              :             break;
   10662              : 
   10663        65747 :           case TYPE_PACK_EXPANSION:
   10664        65747 :             {
   10665        65747 :               bool local = u ();
   10666        65747 :               tree param_packs = tree_node ();
   10667        65747 :               tree extra_args = tree_node ();
   10668        65747 :               if (!get_overrun ())
   10669              :                 {
   10670        65747 :                   tree expn = cxx_make_type (TYPE_PACK_EXPANSION);
   10671        65747 :                   SET_TYPE_STRUCTURAL_EQUALITY (expn);
   10672        65747 :                   PACK_EXPANSION_PATTERN (expn) = res;
   10673       131494 :                   PACK_EXPANSION_PARAMETER_PACKS (expn) = param_packs;
   10674        65747 :                   PACK_EXPANSION_EXTRA_ARGS (expn) = extra_args;
   10675        65747 :                   PACK_EXPANSION_LOCAL_P (expn) = local;
   10676        65747 :                   res = expn;
   10677              :                 }
   10678              :             }
   10679              :             break;
   10680              : 
   10681           25 :           case PACK_INDEX_TYPE:
   10682           25 :             {
   10683           25 :               tree pack = tree_node ();
   10684           25 :               tree index = tree_node ();
   10685           25 :               if (!get_overrun ())
   10686           25 :                 res = make_pack_index (pack, index);
   10687              :             }
   10688              :             break;
   10689              : 
   10690        67617 :           case TYPENAME_TYPE:
   10691        67617 :             {
   10692        67617 :               tree ctx = tree_node ();
   10693        67617 :               tree name = tree_node ();
   10694        67617 :               tree fullname = tree_node ();
   10695        67617 :               enum tag_types tag_type = tag_types (u ());
   10696              : 
   10697        67617 :               if (!get_overrun ())
   10698        67617 :                 res = build_typename_type (ctx, name, fullname, tag_type);
   10699              :             }
   10700              :             break;
   10701              : 
   10702           52 :           case UNBOUND_CLASS_TEMPLATE:
   10703           52 :             {
   10704           52 :               tree ctx = tree_node ();
   10705           52 :               tree name = tree_node ();
   10706           52 :               tree parms = tree_node ();
   10707              : 
   10708           52 :               if (!get_overrun ())
   10709           52 :                 res = make_unbound_class_template_raw (ctx, name, parms);
   10710              :             }
   10711              :             break;
   10712              : 
   10713              :           case VECTOR_TYPE:
   10714              :             {
   10715              :               poly_uint64 nunits;
   10716           60 :               for (unsigned ix = 0; ix != NUM_POLY_INT_COEFFS; ix++)
   10717           30 :                 nunits.coeffs[ix] = wu ();
   10718           30 :               if (!get_overrun ())
   10719           30 :                 res = build_vector_type (res, nunits);
   10720              :             }
   10721              :             break;
   10722              : 
   10723           90 :           case META_TYPE:
   10724           90 :             if (!get_overrun ())
   10725           90 :               res = meta_info_type_node;
   10726              :             break;
   10727              : 
   10728            4 :           case SPLICE_SCOPE:
   10729            4 :             {
   10730            4 :               bool type = u ();
   10731            4 :               tree expr = tree_node ();
   10732              : 
   10733            4 :               if (!get_overrun ())
   10734            4 :                 res = make_splice_scope (expr, type);
   10735              :             }
   10736              :             break;
   10737              :           }
   10738              : 
   10739              :         /* In the exporting TU, a derived type with attributes was built by
   10740              :            build_type_attribute_variant as a distinct copy, with itself as
   10741              :            TYPE_MAIN_VARIANT.  We repeat that on import to get the version
   10742              :            without attributes as TYPE_CANONICAL.  */
   10743      1506889 :         if (tree attribs = tree_node ())
   10744        17298 :           res = cp_build_type_attribute_variant (res, attribs);
   10745              : 
   10746      1506889 :         int tag = i ();
   10747      1506889 :         if (!tag)
   10748              :           {
   10749      1288570 :             tag = insert (res);
   10750      1288570 :             if (res)
   10751      1288570 :               dump (dumper::TREE)
   10752          678 :                 && dump ("Created:%d derived type %C", tag, code);
   10753              :           }
   10754              :         else
   10755       218319 :           res = back_ref (tag);
   10756              :       }
   10757              :       break;
   10758              : 
   10759       432431 :     case tt_variant_type:
   10760              :       /* Variant of some type.  */
   10761       432431 :       {
   10762       432431 :         res = tree_node ();
   10763       432431 :         int flags = i ();
   10764       432431 :         if (get_overrun ())
   10765              :           ;
   10766       432431 :         else if (flags < 0)
   10767              :           /* No change.  */;
   10768       208462 :         else if (TREE_CODE (res) == FUNCTION_TYPE
   10769       208462 :                  || TREE_CODE (res) == METHOD_TYPE)
   10770              :           {
   10771       206889 :             cp_ref_qualifier rqual = cp_ref_qualifier (flags & 3);
   10772       206889 :             bool late = (flags >> 2) & 1;
   10773       206889 :             cp_cv_quals quals = cp_cv_quals (flags >> 3);
   10774              : 
   10775       206889 :             tree raises = tree_node ();
   10776       206889 :             if (raises == error_mark_node)
   10777         7213 :               raises = TYPE_RAISES_EXCEPTIONS (res);
   10778              : 
   10779       206889 :             res = build_cp_fntype_variant (res, rqual, raises, late);
   10780       206889 :             if (TREE_CODE (res) == FUNCTION_TYPE)
   10781        73171 :               res = apply_memfn_quals (res, quals, rqual);
   10782              :           }
   10783              :         else
   10784              :           {
   10785         1573 :             res = build_aligned_type (res, (1u << flags) >> 1);
   10786         1573 :             TYPE_USER_ALIGN (res) = true;
   10787              :           }
   10788              : 
   10789       432431 :         int quals = i ();
   10790       432431 :         if (quals >= 0 && !get_overrun ())
   10791       225119 :           res = cp_build_qualified_type (res, quals);
   10792              : 
   10793       432431 :         int tag = i ();
   10794       432431 :         if (!tag)
   10795              :           {
   10796       432431 :             tag = insert (res);
   10797       432431 :             if (res)
   10798       432431 :               dump (dumper::TREE)
   10799          292 :                 && dump ("Created:%d variant type %C", tag, TREE_CODE (res));
   10800              :           }
   10801              :         else
   10802            0 :           res = back_ref (tag);
   10803              :       }
   10804              :       break;
   10805              : 
   10806         5096 :     case tt_tinfo_var:
   10807         5096 :     case tt_tinfo_typedef:
   10808              :       /* A tinfo var or typedef.  */
   10809         5096 :       {
   10810         5096 :         bool is_var = tag == tt_tinfo_var;
   10811         5096 :         unsigned ix = u ();
   10812         5096 :         tree type = NULL_TREE;
   10813              : 
   10814         5096 :         if (is_var)
   10815              :           {
   10816         3092 :             tree name = tree_node ();
   10817         3092 :             type = tree_node ();
   10818              : 
   10819         3092 :             if (!get_overrun ())
   10820         3092 :               res = get_tinfo_decl_direct (type, name, int (ix));
   10821              :           }
   10822              :         else
   10823              :           {
   10824         2004 :             if (!get_overrun ())
   10825              :               {
   10826         2004 :                 type = get_pseudo_tinfo_type (ix);
   10827         2004 :                 res = TYPE_NAME (type);
   10828              :               }
   10829              :           }
   10830         5096 :         if (res)
   10831              :           {
   10832         5096 :             int tag = insert (res);
   10833         5096 :             dump (dumper::TREE)
   10834           36 :               && dump ("Created tinfo_%s:%d %S:%u for %N",
   10835              :                        is_var ? "var" : "decl", tag, res, ix, type);
   10836         5096 :             if (!is_var)
   10837              :               {
   10838         2004 :                 tag = insert (type);
   10839         2004 :                 dump (dumper::TREE)
   10840           12 :                   && dump ("Created tinfo_type:%d %u %N", tag, ix, type);
   10841              :               }
   10842              :           }
   10843              :       }
   10844              :       break;
   10845              : 
   10846         1093 :     case tt_ptrmem_type:
   10847              :       /* A pointer to member function.  */
   10848         1093 :       {
   10849         1093 :         tree type = tree_node ();
   10850         1093 :         if (type && TREE_CODE (type) == POINTER_TYPE
   10851         2186 :             && TREE_CODE (TREE_TYPE (type)) == METHOD_TYPE)
   10852              :           {
   10853         1093 :             res = build_ptrmemfunc_type (type);
   10854         1093 :             int tag = insert (res);
   10855         1096 :             dump (dumper::TREE) && dump ("Created:%d ptrmem type", tag);
   10856              :           }
   10857              :         else
   10858            0 :           set_overrun ();
   10859              :       }
   10860              :       break;
   10861              : 
   10862            9 :     case tt_nttp_var:
   10863              :       /* An NTTP object. */
   10864            9 :       {
   10865            9 :         tree init = tree_node ();
   10866            9 :         tree name = tree_node ();
   10867            9 :         if (!get_overrun ())
   10868              :           {
   10869              :             /* We don't want to check the initializer as that may require
   10870              :                name lookup, which could recursively start lazy loading.
   10871              :                Instead we know that INIT is already valid so we can just
   10872              :                apply that directly.  */
   10873            9 :             res = get_template_parm_object (init, name, /*check_init=*/false);
   10874            9 :             int tag = insert (res);
   10875            9 :             dump (dumper::TREE)
   10876            0 :               && dump ("Created nttp object:%d %N", tag, name);
   10877            9 :             vec_safe_push (post_load_decls, res);
   10878              :           }
   10879              :       }
   10880              :       break;
   10881              : 
   10882         7961 :     case tt_enum_value:
   10883              :       /* An enum const value.  */
   10884         7961 :       {
   10885         7961 :         if (tree decl = tree_node ())
   10886              :           {
   10887         7979 :             dump (dumper::TREE) && dump ("Read enum value %N", decl);
   10888         7961 :             res = DECL_INITIAL (decl);
   10889              :           }
   10890              : 
   10891         7961 :         if (!res)
   10892            0 :           set_overrun ();
   10893              :       }
   10894              :       break;
   10895              : 
   10896        14127 :     case tt_enum_decl:
   10897              :       /* An enum decl.  */
   10898        14127 :       {
   10899        14127 :         tree ctx = tree_node ();
   10900        14127 :         tree name = tree_node ();
   10901              : 
   10902        14127 :         if (!get_overrun ()
   10903        14127 :             && TREE_CODE (ctx) == ENUMERAL_TYPE)
   10904        14127 :           res = find_enum_member (ctx, name);
   10905              : 
   10906        14127 :         if (!res)
   10907            0 :           set_overrun ();
   10908              :         else
   10909              :           {
   10910        14127 :             int tag = insert (res);
   10911        14127 :             dump (dumper::TREE)
   10912           18 :               && dump ("Read enum decl:%d %C:%N", tag, TREE_CODE (res), res);
   10913              :           }
   10914              :       }
   10915              :       break;
   10916              : 
   10917         9076 :     case tt_data_member:
   10918              :       /* A data member.  */
   10919         9076 :       {
   10920         9076 :         tree ctx = tree_node ();
   10921         9076 :         tree name = tree_node ();
   10922              : 
   10923         9076 :         if (!get_overrun ()
   10924         9076 :             && RECORD_OR_UNION_TYPE_P (ctx))
   10925              :           {
   10926         9076 :             if (name)
   10927         8021 :               res = lookup_class_binding (ctx, name);
   10928              :             else
   10929         1055 :               res = lookup_field_ident (ctx, u ());
   10930              : 
   10931         9076 :             if (!res
   10932         9076 :                 || (TREE_CODE (res) != FIELD_DECL
   10933         9076 :                     && TREE_CODE (res) != USING_DECL)
   10934        18152 :                 || DECL_CONTEXT (res) != ctx)
   10935            0 :               res = NULL_TREE;
   10936              :           }
   10937              : 
   10938         9076 :         if (!res)
   10939            0 :           set_overrun ();
   10940              :         else
   10941              :           {
   10942         9076 :             int tag = insert (res);
   10943         9076 :             dump (dumper::TREE)
   10944           26 :               && dump ("Read member:%d %C:%N", tag, TREE_CODE (res), res);
   10945              :           }
   10946              :       }
   10947              :       break;
   10948              : 
   10949         1699 :     case tt_binfo:
   10950              :       /* A BINFO.  Walk the tree of the dominating type.  */
   10951         1699 :       {
   10952         1699 :         tree type;
   10953         1699 :         unsigned ix = binfo_mergeable (&type);
   10954         1699 :         if (type)
   10955              :           {
   10956         1699 :             res = TYPE_BINFO (type);
   10957         1785 :             for (; ix && res; res = TREE_CHAIN (res))
   10958           86 :               ix--;
   10959         1699 :             if (!res)
   10960            0 :               set_overrun ();
   10961              :           }
   10962              : 
   10963         1699 :         if (get_overrun ())
   10964              :           break;
   10965              : 
   10966              :         /* Insert binfo into backreferences.  */
   10967         1699 :         tag = insert (res);
   10968         1699 :         dump (dumper::TREE) && dump ("Read binfo:%d %N", tag, res);
   10969              :       }
   10970         1699 :       break;
   10971              : 
   10972           73 :     case tt_vtable:
   10973           73 :       {
   10974           73 :         unsigned ix = u ();
   10975           73 :         tree ctx = tree_node ();
   10976           73 :         dump (dumper::TREE) && dump ("Reading vtable %N[%u]", ctx, ix);
   10977           73 :         if (TREE_CODE (ctx) == RECORD_TYPE && TYPE_LANG_SPECIFIC (ctx))
   10978           85 :           for (res = CLASSTYPE_VTABLES (ctx); res; res = DECL_CHAIN (res))
   10979           85 :             if (!ix--)
   10980              :               break;
   10981           73 :         if (!res)
   10982            0 :           set_overrun ();
   10983              :       }
   10984              :       break;
   10985              : 
   10986            0 :     case tt_thunk:
   10987            0 :       {
   10988            0 :         int fixed = i ();
   10989            0 :         tree target = tree_node ();
   10990            0 :         tree virt = tree_node ();
   10991              : 
   10992            0 :         for (tree thunk = DECL_THUNKS (target);
   10993            0 :              thunk; thunk = DECL_CHAIN (thunk))
   10994            0 :           if (THUNK_FIXED_OFFSET (thunk) == fixed
   10995            0 :               && !THUNK_VIRTUAL_OFFSET (thunk) == !virt
   10996            0 :               && (!virt
   10997            0 :                   || tree_int_cst_equal (virt, THUNK_VIRTUAL_OFFSET (thunk))))
   10998              :             {
   10999            0 :               res = thunk;
   11000            0 :               break;
   11001              :             }
   11002              : 
   11003            0 :         int tag = insert (res);
   11004            0 :         if (res)
   11005            0 :           dump (dumper::TREE)
   11006            0 :             && dump ("Read:%d thunk %N to %N", tag, DECL_NAME (res), target);
   11007              :         else
   11008            0 :           set_overrun ();
   11009              :       }
   11010              :       break;
   11011              : 
   11012       157153 :     case tt_clone_ref:
   11013       157153 :       {
   11014       157153 :         tree target = tree_node ();
   11015       157153 :         tree name = tree_node ();
   11016              : 
   11017       157153 :         if (DECL_P (target) && DECL_MAYBE_IN_CHARGE_CDTOR_P (target))
   11018              :           {
   11019       157153 :             tree clone;
   11020       243971 :             FOR_EVERY_CLONE (clone, target)
   11021       243971 :               if (DECL_NAME (clone) == name)
   11022              :                 {
   11023       157153 :                   res = clone;
   11024       157153 :                   break;
   11025              :                 }
   11026              :           }
   11027              : 
   11028              :         /* A clone might have a different vtable entry.  */
   11029       157153 :         if (res && DECL_VIRTUAL_P (res))
   11030         8619 :           DECL_VINDEX (res) = tree_node ();
   11031              : 
   11032       157153 :         if (!res)
   11033            0 :           set_overrun ();
   11034       157153 :         int tag = insert (res);
   11035       157153 :         if (res)
   11036       157153 :           dump (dumper::TREE)
   11037          230 :             && dump ("Read:%d clone %N of %N", tag, DECL_NAME (res), target);
   11038              :         else
   11039            0 :           set_overrun ();
   11040              :        }
   11041              :       break;
   11042              : 
   11043      1100300 :     case tt_entity:
   11044              :       /* Index into the entity table.  Perhaps not loaded yet!  */
   11045      1100300 :       {
   11046      1100300 :         unsigned origin = state->slurp->remap_module (u ());
   11047      1100300 :         unsigned ident = u ();
   11048      1100300 :         module_state *from = (*modules)[origin];
   11049              : 
   11050      1100300 :         if (!origin || ident >= from->entity_num)
   11051            0 :           set_overrun ();
   11052      1100300 :         if (!get_overrun ())
   11053              :           {
   11054      1100300 :             binding_slot *slot = &(*entity_ary)[from->entity_lwm + ident];
   11055      1100300 :             if (slot->is_lazy ())
   11056        55287 :               if (!from->lazy_load (ident, slot))
   11057            0 :                 set_overrun ();
   11058      1100300 :             res = *slot;
   11059              :           }
   11060              : 
   11061      1100300 :         if (res)
   11062              :           {
   11063      1100300 :             const char *kind = (origin != state->mod ? "Imported" : "Named");
   11064      1100300 :             int tag = insert (res);
   11065      1100300 :             dump (dumper::TREE)
   11066          605 :               && dump ("%s:%d %C:%N@%M", kind, tag, TREE_CODE (res),
   11067          605 :                        res, (*modules)[origin]);
   11068              : 
   11069      1100300 :             if (!add_indirects (res))
   11070              :               {
   11071            0 :                 set_overrun ();
   11072            0 :                 res = NULL_TREE;
   11073              :               }
   11074              :           }
   11075              :       }
   11076              :       break;
   11077              : 
   11078         3094 :     case tt_template:
   11079              :       /* A template.  */
   11080         3094 :       if (tree tpl = tree_node ())
   11081              :         {
   11082         3094 :           res = (TREE_CODE (tpl) == TU_LOCAL_ENTITY ?
   11083         3094 :                  tpl : DECL_TEMPLATE_RESULT (tpl));
   11084         3094 :           dump (dumper::TREE)
   11085            9 :             && dump ("Read template %C:%N", TREE_CODE (res), res);
   11086              :         }
   11087              :       break;
   11088              :     }
   11089              : 
   11090     98245293 :   if (is_use && !unused && res && DECL_P (res) && !TREE_USED (res))
   11091              :     {
   11092              :       /* Mark decl used as mark_used does -- we cannot call
   11093              :          mark_used in the middle of streaming, we only need a subset
   11094              :          of its functionality.   */
   11095       767645 :       TREE_USED (res) = true;
   11096              : 
   11097              :       /* And for structured bindings also the underlying decl.  */
   11098       767645 :       if (DECL_DECOMPOSITION_P (res) && !DECL_DECOMP_IS_BASE (res))
   11099         2010 :         TREE_USED (DECL_DECOMP_BASE (res)) = true;
   11100              : 
   11101       767645 :       if (DECL_CLONED_FUNCTION_P (res))
   11102         7352 :         TREE_USED (DECL_CLONED_FUNCTION (res)) = true;
   11103              :     }
   11104              : 
   11105     98245293 :   dump.outdent ();
   11106     98245293 :   return res;
   11107              : }
   11108              : 
   11109              : void
   11110      2133934 : trees_out::tpl_parms (tree parms, unsigned &tpl_levels)
   11111              : {
   11112      2133934 :   if (!parms)
   11113              :     return;
   11114              : 
   11115      1438093 :   if (TREE_VISITED (parms))
   11116              :     {
   11117       600370 :       ref_node (parms);
   11118       600370 :       return;
   11119              :     }
   11120              : 
   11121       837723 :   tpl_parms (TREE_CHAIN (parms), tpl_levels);
   11122              : 
   11123       837723 :   tree vec = TREE_VALUE (parms);
   11124       837723 :   unsigned len = TREE_VEC_LENGTH (vec);
   11125              :   /* Depth.  */
   11126       837723 :   int tag = insert (parms);
   11127       837723 :   if (streaming_p ())
   11128              :     {
   11129       223452 :       i (len + 1);
   11130       223518 :       dump (dumper::TREE)
   11131           66 :         && dump ("Writing template parms:%d level:%N length:%d",
   11132           66 :                  tag, TREE_PURPOSE (parms), len);
   11133              :     }
   11134       837723 :   tree_node (TREE_PURPOSE (parms));
   11135              : 
   11136      2318645 :   for (unsigned ix = 0; ix != len; ix++)
   11137              :     {
   11138      1480922 :       tree parm = TREE_VEC_ELT (vec, ix);
   11139      1480922 :       tree decl = TREE_VALUE (parm);
   11140              : 
   11141      1480922 :       gcc_checking_assert (DECL_TEMPLATE_PARM_P (decl));
   11142      1480922 :       if (CHECKING_P)
   11143      1480922 :         switch (TREE_CODE (decl))
   11144              :           {
   11145            0 :           default: gcc_unreachable ();
   11146              : 
   11147         3879 :           case TEMPLATE_DECL:
   11148         3879 :             gcc_assert ((TREE_CODE (TREE_TYPE (decl)) == TEMPLATE_TEMPLATE_PARM)
   11149              :                         && (TREE_CODE (DECL_TEMPLATE_RESULT (decl)) == TYPE_DECL)
   11150              :                         && (TYPE_NAME (TREE_TYPE (decl)) == decl));
   11151              :             break;
   11152              : 
   11153      1382435 :           case TYPE_DECL:
   11154      1382435 :             gcc_assert ((TREE_CODE (TREE_TYPE (decl)) == TEMPLATE_TYPE_PARM)
   11155              :                         && (TYPE_NAME (TREE_TYPE (decl)) == decl));
   11156              :             break;
   11157              : 
   11158        94608 :           case PARM_DECL:
   11159        94608 :             gcc_assert ((TREE_CODE (DECL_INITIAL (decl)) == TEMPLATE_PARM_INDEX)
   11160              :                         && (TREE_CODE (TEMPLATE_PARM_DECL (DECL_INITIAL (decl)))
   11161              :                             == CONST_DECL)
   11162              :                         && (DECL_TEMPLATE_PARM_P
   11163              :                             (TEMPLATE_PARM_DECL (DECL_INITIAL (decl)))));
   11164              :             break;
   11165              :           }
   11166              : 
   11167      1480922 :       tree_node (decl);
   11168      1480922 :       tree_node (TEMPLATE_PARM_CONSTRAINTS (parm));
   11169              :     }
   11170              : 
   11171       837723 :   tpl_levels++;
   11172              : }
   11173              : 
   11174              : tree
   11175       332694 : trees_in::tpl_parms (unsigned &tpl_levels)
   11176              : {
   11177       332694 :   tree parms = NULL_TREE;
   11178              : 
   11179       702382 :   while (int len = i ())
   11180              :     {
   11181       369688 :       if (len < 0)
   11182              :         {
   11183       200990 :           parms = back_ref (len);
   11184       200990 :           continue;
   11185              :         }
   11186              : 
   11187       168698 :       len -= 1;
   11188       168698 :       parms = tree_cons (NULL_TREE, NULL_TREE, parms);
   11189       168698 :       int tag = insert (parms);
   11190       168698 :       TREE_PURPOSE (parms) = tree_node ();
   11191              : 
   11192       168698 :       dump (dumper::TREE)
   11193          105 :         && dump ("Reading template parms:%d level:%N length:%d",
   11194          105 :                  tag, TREE_PURPOSE (parms), len);
   11195              : 
   11196       168698 :       tree vec = make_tree_vec (len);
   11197       444394 :       for (int ix = 0; ix != len; ix++)
   11198              :         {
   11199       275696 :           tree decl = tree_node ();
   11200       275696 :           if (!decl)
   11201              :             return NULL_TREE;
   11202              : 
   11203       275696 :           tree parm = build_tree_list (NULL, decl);
   11204       275696 :           TEMPLATE_PARM_CONSTRAINTS (parm) = tree_node ();
   11205              : 
   11206       275696 :           TREE_VEC_ELT (vec, ix) = parm;
   11207              :         }
   11208              : 
   11209       168698 :       TREE_VALUE (parms) = vec;
   11210       168698 :       tpl_levels++;
   11211              :     }
   11212              : 
   11213              :   return parms;
   11214              : }
   11215              : 
   11216              : void
   11217      1296211 : trees_out::tpl_parms_fini (tree tmpl, unsigned tpl_levels)
   11218              : {
   11219      1296211 :   for (tree parms = DECL_TEMPLATE_PARMS (tmpl);
   11220      2133934 :        tpl_levels--; parms = TREE_CHAIN (parms))
   11221              :     {
   11222       837723 :       tree vec = TREE_VALUE (parms);
   11223              : 
   11224       837723 :       tree_node (TREE_TYPE (vec));
   11225      2318645 :       for (unsigned ix = TREE_VEC_LENGTH (vec); ix--;)
   11226              :         {
   11227      1480922 :           tree parm = TREE_VEC_ELT (vec, ix);
   11228      1480922 :           tree dflt = TREE_PURPOSE (parm);
   11229      1480922 :           tree_node (dflt);
   11230              : 
   11231              :           /* Template template parameters need a context of their owning
   11232              :              template. This is quite tricky to infer correctly on stream-in
   11233              :              (see PR c++/98881) so we'll just provide it directly.  */
   11234      1480922 :           tree decl = TREE_VALUE (parm);
   11235      1480922 :           if (TREE_CODE (decl) == TEMPLATE_DECL)
   11236         3879 :             tree_node (DECL_CONTEXT (decl));
   11237              :         }
   11238              :     }
   11239      1296211 : }
   11240              : 
   11241              : bool
   11242       332694 : trees_in::tpl_parms_fini (tree tmpl, unsigned tpl_levels)
   11243              : {
   11244       332694 :   for (tree parms = DECL_TEMPLATE_PARMS (tmpl);
   11245       501392 :        tpl_levels--; parms = TREE_CHAIN (parms))
   11246              :     {
   11247       168698 :       tree vec = TREE_VALUE (parms);
   11248              : 
   11249       168698 :       TREE_TYPE (vec) = tree_node ();
   11250       444394 :       for (unsigned ix = TREE_VEC_LENGTH (vec); ix--;)
   11251              :         {
   11252       275696 :           tree parm = TREE_VEC_ELT (vec, ix);
   11253       275696 :           tree dflt = tree_node ();
   11254       275696 :           TREE_PURPOSE (parm) = dflt;
   11255              : 
   11256       275696 :           tree decl = TREE_VALUE (parm);
   11257       275696 :           if (TREE_CODE (decl) == TEMPLATE_DECL)
   11258          852 :             DECL_CONTEXT (decl) = tree_node ();
   11259              : 
   11260       275696 :           if (get_overrun ())
   11261              :             return false;
   11262              :         }
   11263              :     }
   11264              :   return true;
   11265              : }
   11266              : 
   11267              : /* PARMS is a LIST, one node per level.
   11268              :    TREE_VALUE is a TREE_VEC of parm info for that level.
   11269              :    each ELT is a TREE_LIST
   11270              :    TREE_VALUE is PARM_DECL, TYPE_DECL or TEMPLATE_DECL
   11271              :    TREE_PURPOSE is the default value.  */
   11272              : 
   11273              : void
   11274      1296211 : trees_out::tpl_header (tree tpl, unsigned *tpl_levels)
   11275              : {
   11276      1296211 :   tree parms = DECL_TEMPLATE_PARMS (tpl);
   11277      1296211 :   tpl_parms (parms, *tpl_levels);
   11278              : 
   11279              :   /* Mark end.  */
   11280      1296211 :   if (streaming_p ())
   11281       431580 :     u (0);
   11282              : 
   11283      1296211 :   if (*tpl_levels)
   11284       787513 :     tree_node (TEMPLATE_PARMS_CONSTRAINTS (parms));
   11285      1296211 : }
   11286              : 
   11287              : bool
   11288       332694 : trees_in::tpl_header (tree tpl, unsigned *tpl_levels)
   11289              : {
   11290       332694 :   tree parms = tpl_parms (*tpl_levels);
   11291       332694 :   if (!parms)
   11292              :     return false;
   11293              : 
   11294       332694 :   DECL_TEMPLATE_PARMS (tpl) = parms;
   11295              : 
   11296       332694 :   if (*tpl_levels)
   11297       166569 :     TEMPLATE_PARMS_CONSTRAINTS (parms) = tree_node ();
   11298              : 
   11299              :   return true;
   11300              : }
   11301              : 
   11302              : /* Stream skeleton parm nodes, with their flags, type & parm indices.
   11303              :    All the parms will have consecutive tags.  */
   11304              : 
   11305              : void
   11306      1815276 : trees_out::fn_parms_init (tree fn)
   11307              : {
   11308              :   /* First init them.  */
   11309      1815276 :   int base_tag = ref_num - 1;
   11310      1815276 :   int ix = 0;
   11311      1815276 :   for (tree parm = DECL_ARGUMENTS (fn);
   11312      5484516 :        parm; parm = DECL_CHAIN (parm), ix++)
   11313              :     {
   11314      3669240 :       if (streaming_p ())
   11315              :         {
   11316      1223043 :           start (parm);
   11317      1223043 :           tree_node_bools (parm);
   11318              :         }
   11319      3669240 :       int tag = insert (parm);
   11320      3669240 :       gcc_checking_assert (base_tag - ix == tag);
   11321              :     }
   11322              :   /* Mark the end.  */
   11323      1815276 :   if (streaming_p ())
   11324       605359 :     u (0);
   11325              : 
   11326              :   /* Now stream their contents.  */
   11327      1815276 :   ix = 0;
   11328      1815276 :   for (tree parm = DECL_ARGUMENTS (fn);
   11329      5484516 :        parm; parm = DECL_CHAIN (parm), ix++)
   11330              :     {
   11331      3669240 :       if (streaming_p ())
   11332      1223043 :         dump (dumper::TREE)
   11333          222 :           && dump ("Writing parm:%d %u (%N) of %N",
   11334              :                    base_tag - ix, ix, parm, fn);
   11335      3669240 :       tree_node_vals (parm);
   11336              :     }
   11337              : 
   11338      1815276 :   if (!streaming_p ())
   11339              :     {
   11340              :       /* We must walk contract specifiers so the dependency graph is
   11341              :          complete.  */
   11342      1209917 :       tree contract = get_fn_contract_specifiers (fn);
   11343      2419834 :       for (; contract; contract = TREE_CHAIN (contract))
   11344            0 :         tree_node (contract);
   11345              :     }
   11346              : 
   11347              :   /* Write a reference to contracts pre/post functions, if any, to avoid
   11348              :      regenerating them in importers.  */
   11349      1815276 :   tree_node (DECL_PRE_FN (fn));
   11350      1815276 :   tree_node (DECL_POST_FN (fn));
   11351      1815276 : }
   11352              : 
   11353              : /* Build skeleton parm nodes, read their flags, type & parm indices.  */
   11354              : 
   11355              : int
   11356       470536 : trees_in::fn_parms_init (tree fn)
   11357              : {
   11358       470536 :   int base_tag = ~(int)back_refs.length ();
   11359              : 
   11360       470536 :   tree *parm_ptr = &DECL_ARGUMENTS (fn);
   11361       470536 :   int ix = 0;
   11362      1419524 :   for (; int code = u (); ix++)
   11363              :     {
   11364       948988 :       tree parm = start (code);
   11365       948988 :       if (!tree_node_bools (parm))
   11366              :         return 0;
   11367              : 
   11368       948988 :       int tag = insert (parm);
   11369       948988 :       gcc_checking_assert (base_tag - ix == tag);
   11370       948988 :       *parm_ptr = parm;
   11371       948988 :       parm_ptr = &DECL_CHAIN (parm);
   11372       948988 :     }
   11373              : 
   11374       470536 :   ix = 0;
   11375       470536 :   for (tree parm = DECL_ARGUMENTS (fn);
   11376      1419524 :        parm; parm = DECL_CHAIN (parm), ix++)
   11377              :     {
   11378       948988 :       dump (dumper::TREE)
   11379          362 :         && dump ("Reading parm:%d %u (%N) of %N",
   11380              :                  base_tag - ix, ix, parm, fn);
   11381       948988 :       if (!tree_node_vals (parm))
   11382              :         return 0;
   11383              : 
   11384              :       /* Apply relevant attributes.
   11385              :          FIXME should probably use cplus_decl_attributes for this,
   11386              :          but it's not yet ready for modules.  */
   11387              : 
   11388              :       /* TREE_USED is deliberately not streamed for most declarations,
   11389              :          but needs to be set if we have the [[maybe_unused]] attribute.  */
   11390       948988 :       if (lookup_attribute ("unused", DECL_ATTRIBUTES (parm))
   11391       948988 :           || lookup_attribute ("maybe_unused", DECL_ATTRIBUTES (parm)))
   11392              :         {
   11393         2153 :           TREE_USED (parm) = true;
   11394         2153 :           DECL_READ_P (parm) = true;
   11395              :         }
   11396              :     }
   11397              : 
   11398              :   /* Reload references to contract functions, if any.  */
   11399       470536 :   tree pre_fn = tree_node ();
   11400       470536 :   tree post_fn = tree_node ();
   11401       470536 :   set_contract_functions (fn, pre_fn, post_fn);
   11402              : 
   11403       470536 :   return base_tag;
   11404              : }
   11405              : 
   11406              : /* Read the remaining parm node data.  Replace with existing (if
   11407              :    non-null) in the map.  */
   11408              : 
   11409              : void
   11410       470536 : trees_in::fn_parms_fini (int tag, tree fn, tree existing, bool is_defn)
   11411              : {
   11412       670778 :   tree existing_parm = existing ? DECL_ARGUMENTS (existing) : NULL_TREE;
   11413       470536 :   tree parms = DECL_ARGUMENTS (fn);
   11414      1419524 :   for (tree parm = parms; parm; parm = DECL_CHAIN (parm))
   11415              :     {
   11416       948988 :       if (existing_parm)
   11417              :         {
   11418       583466 :           if (is_defn && !DECL_SAVED_TREE (existing))
   11419              :             {
   11420              :               /* If we're about to become the definition, set the
   11421              :                  names of the parms from us.  */
   11422        15145 :               DECL_NAME (existing_parm) = DECL_NAME (parm);
   11423        15145 :               DECL_SOURCE_LOCATION (existing_parm) = DECL_SOURCE_LOCATION (parm);
   11424              : 
   11425              :               /* And some other flags important for codegen are only set
   11426              :                  by the definition.  */
   11427        15145 :               TREE_ADDRESSABLE (existing_parm) = TREE_ADDRESSABLE (parm);
   11428        15145 :               DECL_BY_REFERENCE (existing_parm) = DECL_BY_REFERENCE (parm);
   11429        15145 :               DECL_NONLOCAL (existing_parm) = DECL_NONLOCAL (parm);
   11430        15145 :               DECL_ARG_TYPE (existing_parm) = DECL_ARG_TYPE (parm);
   11431              : 
   11432              :               /* Invisiref parms had their types adjusted by cp_genericize. */
   11433        15145 :               if (DECL_BY_REFERENCE (parm))
   11434              :                 {
   11435            6 :                   TREE_TYPE (existing_parm) = TREE_TYPE (parm);
   11436            6 :                   relayout_decl (existing_parm);
   11437              :                 }
   11438              :             }
   11439              : 
   11440       395685 :           back_refs[~tag] = existing_parm;
   11441       395685 :           existing_parm = DECL_CHAIN (existing_parm);
   11442              :         }
   11443       948988 :       tag--;
   11444              :     }
   11445       470536 : }
   11446              : 
   11447              : /* Encode into KEY the position of the local type (class or enum)
   11448              :    declaration DECL within FN.  The position is encoded as the
   11449              :    index of the innermost BLOCK (numbered in BFS order) along with
   11450              :    the index within its BLOCK_VARS list.  */
   11451              : 
   11452              : void
   11453        19800 : trees_out::key_local_type (merge_key& key, tree decl, tree fn)
   11454              : {
   11455        19800 :   auto_vec<tree, 4> blocks;
   11456        19800 :   blocks.quick_push (DECL_INITIAL (fn));
   11457        19800 :   unsigned block_ix = 0;
   11458        95724 :   while (block_ix != blocks.length ())
   11459              :     {
   11460        37962 :       tree block = blocks[block_ix];
   11461        37962 :       unsigned decl_ix = 0;
   11462       115164 :       for (tree var = BLOCK_VARS (block); var; var = DECL_CHAIN (var))
   11463              :         {
   11464        97002 :           if (TREE_CODE (var) != TYPE_DECL)
   11465        60405 :             continue;
   11466        36597 :           if (var == decl)
   11467              :             {
   11468        19800 :               key.index = (block_ix << 10) | decl_ix;
   11469        19800 :               return;
   11470              :             }
   11471        16797 :           ++decl_ix;
   11472              :         }
   11473        37956 :       for (tree sub = BLOCK_SUBBLOCKS (block); sub; sub = BLOCK_CHAIN (sub))
   11474        19794 :         blocks.safe_push (sub);
   11475        18162 :       ++block_ix;
   11476              :     }
   11477              : 
   11478              :   /* Not-found value.  */
   11479            0 :   key.index = 1023;
   11480        19800 : }
   11481              : 
   11482              : /* Look up the local type corresponding at the position encoded by
   11483              :    KEY within FN and named NAME.  */
   11484              : 
   11485              : tree
   11486         4272 : trees_in::key_local_type (const merge_key& key, tree fn, tree name)
   11487              : {
   11488         4272 :   if (!DECL_INITIAL (fn))
   11489              :     return NULL_TREE;
   11490              : 
   11491         1902 :   const unsigned block_pos = key.index >> 10;
   11492         1902 :   const unsigned decl_pos = key.index & 1023;
   11493              : 
   11494         1902 :   if (decl_pos == 1023)
   11495              :     return NULL_TREE;
   11496              : 
   11497         1902 :   auto_vec<tree, 4> blocks;
   11498         1902 :   blocks.quick_push (DECL_INITIAL (fn));
   11499         1902 :   unsigned block_ix = 0;
   11500         8688 :   while (block_ix != blocks.length ())
   11501              :     {
   11502         3393 :       tree block = blocks[block_ix];
   11503         3393 :       if (block_ix == block_pos)
   11504              :         {
   11505         1902 :           unsigned decl_ix = 0;
   11506         5158 :           for (tree var = BLOCK_VARS (block); var; var = DECL_CHAIN (var))
   11507              :             {
   11508         5158 :               if (TREE_CODE (var) != TYPE_DECL)
   11509         2278 :                 continue;
   11510              :               /* Prefer using the identifier as the key for more robustness
   11511              :                  to ODR violations, except for anonymous types since their
   11512              :                  compiler-generated identifiers aren't stable.  */
   11513         5760 :               if (IDENTIFIER_ANON_P (name)
   11514         2880 :                   ? decl_ix == decl_pos
   11515          323 :                   : DECL_NAME (var) == name)
   11516              :                 return var;
   11517          978 :               ++decl_ix;
   11518              :             }
   11519              :           return NULL_TREE;
   11520              :         }
   11521         3091 :       for (tree sub = BLOCK_SUBBLOCKS (block); sub; sub = BLOCK_CHAIN (sub))
   11522         1600 :         blocks.safe_push (sub);
   11523         1491 :       ++block_ix;
   11524              :     }
   11525              : 
   11526              :   return NULL_TREE;
   11527         1902 : }
   11528              : 
   11529              : /* DEP is the depset of some decl we're streaming by value.  Determine
   11530              :    the merging behaviour.  */
   11531              : 
   11532              : merge_kind
   11533      4506830 : trees_out::get_merge_kind (tree decl, depset *dep)
   11534              : {
   11535      4506830 :   if (!dep)
   11536              :     {
   11537       925710 :       if (VAR_OR_FUNCTION_DECL_P (decl))
   11538              :         {
   11539              :           /* Any var or function with template info should have DEP.  */
   11540       518826 :           gcc_checking_assert (!DECL_LANG_SPECIFIC (decl)
   11541              :                                || !DECL_TEMPLATE_INFO (decl));
   11542       518826 :           if (DECL_LOCAL_DECL_P (decl))
   11543              :             return MK_unique;
   11544              :         }
   11545              : 
   11546              :       /* Either unique, or some member of a class that cannot have an
   11547              :          out-of-class definition.  For instance a FIELD_DECL.  */
   11548       925398 :       tree ctx = CP_DECL_CONTEXT (decl);
   11549       925398 :       if (TREE_CODE (ctx) == FUNCTION_DECL)
   11550              :         {
   11551              :           /* USING_DECLs and NAMESPACE_DECLs cannot have DECL_TEMPLATE_INFO --
   11552              :              this isn't permitting them to have one.   */
   11553       595440 :           gcc_checking_assert (TREE_CODE (decl) == USING_DECL
   11554              :                                || TREE_CODE (decl) == NAMESPACE_DECL
   11555              :                                || !DECL_LANG_SPECIFIC (decl)
   11556              :                                || !DECL_TEMPLATE_INFO (decl));
   11557              : 
   11558              :           return MK_unique;
   11559              :         }
   11560              : 
   11561       329958 :       if (TREE_CODE (decl) == TEMPLATE_DECL
   11562       329958 :           && DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (decl))
   11563              :         return MK_local_friend;
   11564              : 
   11565       329958 :       gcc_checking_assert (TYPE_P (ctx));
   11566              : 
   11567              :       /* Internal-only types will not need to dedup their members.  */
   11568       329958 :       if (!DECL_CONTEXT (TYPE_NAME (ctx)))
   11569              :         return MK_unique;
   11570              : 
   11571       329902 :       if (TREE_CODE (decl) == USING_DECL)
   11572              :         return MK_field;
   11573              : 
   11574       217093 :       if (TREE_CODE (decl) == FIELD_DECL)
   11575              :         {
   11576       159751 :           if (DECL_NAME (decl))
   11577              :             {
   11578              :               /* Anonymous FIELD_DECLs have a NULL name.  */
   11579       129072 :               gcc_checking_assert (!IDENTIFIER_ANON_P (DECL_NAME (decl)));
   11580              :               return MK_named;
   11581              :             }
   11582              : 
   11583        30679 :           if (walking_bit_field_unit)
   11584              :             {
   11585              :               /* The underlying storage unit for a bitfield.  We do not
   11586              :                  need to dedup it, because it's only reachable through
   11587              :                  the bitfields it represents.  And those are deduped.  */
   11588              :               // FIXME: Is that assertion correct -- do we ever fish it
   11589              :               // out and put it in an expr?
   11590          528 :               gcc_checking_assert (!DECL_NAME (decl)
   11591              :                                    && !RECORD_OR_UNION_TYPE_P (TREE_TYPE (decl))
   11592              :                                    && !DECL_BIT_FIELD_REPRESENTATIVE (decl));
   11593          528 :               gcc_checking_assert ((TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
   11594              :                                     ? TREE_CODE (TREE_TYPE (TREE_TYPE (decl)))
   11595              :                                     : TREE_CODE (TREE_TYPE (decl)))
   11596              :                                    == INTEGER_TYPE);
   11597              :               return MK_unique;
   11598              :             }
   11599              : 
   11600              :           return MK_field;
   11601              :         }
   11602              : 
   11603        57342 :       if (TREE_CODE (decl) == CONST_DECL)
   11604              :         return MK_named;
   11605              : 
   11606        10202 :       if (TREE_CODE (decl) == VAR_DECL
   11607        10202 :           && DECL_VTABLE_OR_VTT_P (decl))
   11608              :         return MK_vtable;
   11609              : 
   11610         1736 :       if (DECL_THUNK_P (decl))
   11611              :         /* Thunks are unique-enough, because they're only referenced
   11612              :            from the vtable.  And that's either new (so we want the
   11613              :            thunks), or it's a duplicate (so it will be dropped).  */
   11614              :         return MK_unique;
   11615              : 
   11616              :       /* There should be no other cases.  */
   11617            0 :       gcc_unreachable ();
   11618              :     }
   11619              : 
   11620      3581120 :   gcc_checking_assert (TREE_CODE (decl) != FIELD_DECL
   11621              :                        && TREE_CODE (decl) != USING_DECL
   11622              :                        && TREE_CODE (decl) != CONST_DECL);
   11623              : 
   11624      3581120 :   if (is_key_order ())
   11625              :     {
   11626              :       /* When doing the mergeablilty graph, there's an indirection to
   11627              :          the actual depset.  */
   11628      1193553 :       gcc_assert (dep->is_special ());
   11629      1193553 :       dep = dep->deps[0];
   11630              :     }
   11631              : 
   11632      3581120 :   gcc_checking_assert (decl == dep->get_entity ());
   11633              : 
   11634      3581120 :   merge_kind mk = MK_named;
   11635      3581120 :   switch (dep->get_entity_kind ())
   11636              :     {
   11637            0 :     default:
   11638            0 :       gcc_unreachable ();
   11639              : 
   11640              :     case depset::EK_PARTIAL:
   11641              :       mk = MK_partial;
   11642              :       break;
   11643              : 
   11644      2023793 :     case depset::EK_DECL:
   11645      2023793 :       {
   11646      2023793 :         tree ctx = CP_DECL_CONTEXT (decl);
   11647              : 
   11648      2023793 :         switch (TREE_CODE (ctx))
   11649              :           {
   11650            0 :           default:
   11651            0 :             gcc_unreachable ();
   11652              : 
   11653        20064 :           case FUNCTION_DECL:
   11654        20064 :             gcc_checking_assert
   11655              :               (DECL_IMPLICIT_TYPEDEF_P (STRIP_TEMPLATE (decl)));
   11656              : 
   11657        20064 :             if (has_definition (ctx))
   11658              :               mk = MK_local_type;
   11659              :             else
   11660              :               /* We're not providing a definition of the context to key
   11661              :                  the local type into; use the keyed map instead.  */
   11662         1216 :               mk = MK_keyed;
   11663              :             break;
   11664              : 
   11665      2003729 :           case RECORD_TYPE:
   11666      2003729 :           case UNION_TYPE:
   11667      2003729 :           case NAMESPACE_DECL:
   11668      2003729 :             if (DECL_NAME (decl) == as_base_identifier)
   11669              :               {
   11670              :                 mk = MK_as_base;
   11671              :                 break;
   11672              :               }
   11673              : 
   11674              :             /* A lambda may have a class as its context, even though it
   11675              :                isn't a member in the traditional sense; see the test
   11676              :                g++.dg/modules/lambda-6_a.C.  */
   11677      2497940 :             if (DECL_IMPLICIT_TYPEDEF_P (STRIP_TEMPLATE (decl))
   11678      2178137 :                 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
   11679              :               {
   11680          988 :                 if (get_keyed_decl_scope (decl))
   11681              :                   mk = MK_keyed;
   11682              :                 else
   11683              :                   /* Lambdas not attached to any mangling scope are TU-local
   11684              :                      and so cannot be deduplicated.  */
   11685       598585 :                   mk = MK_unique;
   11686              :                 break;
   11687              :               }
   11688              : 
   11689      1912465 :             if (TREE_CODE (decl) == TEMPLATE_DECL
   11690      1912465 :                 ? DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (decl)
   11691       822612 :                 : decl_specialization_friend_p (decl))
   11692              :               {
   11693              :                 mk = MK_local_friend;
   11694              :                 break;
   11695              :               }
   11696              : 
   11697      1887541 :             if (DECL_DECOMPOSITION_P (decl))
   11698              :               {
   11699              :                 mk = MK_unique;
   11700              :                 break;
   11701              :               }
   11702              : 
   11703      1887064 :             if (IDENTIFIER_ANON_P (DECL_NAME (decl)))
   11704              :               {
   11705        32870 :                 if (RECORD_OR_UNION_TYPE_P (ctx))
   11706              :                   mk = MK_field;
   11707         1177 :                 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
   11708         1177 :                          && UNSCOPED_ENUM_P (TREE_TYPE (decl))
   11709         2354 :                          && TYPE_VALUES (TREE_TYPE (decl)))
   11710              :                   /* Keyed by first enum value, and underlying type.  */
   11711              :                   mk = MK_enum;
   11712              :                 else
   11713              :                   /* No way to merge it, it is an ODR land-mine.  */
   11714              :                   mk = MK_unique;
   11715              :               }
   11716              :           }
   11717              :       }
   11718              :       break;
   11719              : 
   11720      1499304 :     case depset::EK_SPECIALIZATION:
   11721      1499304 :       {
   11722      1499304 :         gcc_checking_assert (dep->is_special ());
   11723              : 
   11724      1499304 :         if (TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
   11725              :           /* An block-scope classes of templates are themselves
   11726              :              templates.  */
   11727         5613 :           gcc_checking_assert (DECL_IMPLICIT_TYPEDEF_P (decl));
   11728              : 
   11729      1499304 :         if (dep->is_friend_spec ())
   11730              :           mk = MK_friend_spec;
   11731      1499304 :         else if (dep->is_type_spec ())
   11732              :           mk = MK_type_spec;
   11733              :         else
   11734      1064316 :           mk = MK_decl_spec;
   11735              : 
   11736      1499304 :         if (TREE_CODE (decl) == TEMPLATE_DECL)
   11737              :           {
   11738       123876 :             spec_entry *entry = reinterpret_cast <spec_entry *> (dep->deps[0]);
   11739       123876 :             if (TREE_CODE (entry->spec) != TEMPLATE_DECL)
   11740        12774 :               mk = merge_kind (mk | MK_tmpl_tmpl_mask);
   11741              :           }
   11742              :       }
   11743              :       break;
   11744              :     }
   11745              : 
   11746              :   return mk;
   11747              : }
   11748              : 
   11749              : 
   11750              : /* The container of DECL -- not necessarily its context!  */
   11751              : 
   11752              : tree
   11753      4506830 : trees_out::decl_container (tree decl)
   11754              : {
   11755      4506830 :   int use_tpl;
   11756      4506830 :   tree tpl = NULL_TREE;
   11757      4506830 :   if (tree template_info = node_template_info (decl, use_tpl))
   11758      1568174 :     tpl = TI_TEMPLATE (template_info);
   11759      4506830 :   if (tpl == decl)
   11760            0 :     tpl = nullptr;
   11761              : 
   11762              :   /* Stream the template we're instantiated from.  */
   11763      4506830 :   tree_node (tpl);
   11764              : 
   11765      4506830 :   tree container = NULL_TREE;
   11766      4506830 :   if (TREE_CODE (decl) == TEMPLATE_DECL
   11767      4506830 :       ? DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (decl)
   11768      3217408 :       : decl_specialization_friend_p (decl))
   11769        24924 :     container = DECL_CHAIN (decl);
   11770              :   else
   11771      4481906 :     container = CP_DECL_CONTEXT (decl);
   11772              : 
   11773      4506830 :   if (TYPE_P (container))
   11774      2642903 :     container = TYPE_NAME (container);
   11775              : 
   11776      4506830 :   tree_node (container);
   11777              : 
   11778      4506830 :   return container;
   11779              : }
   11780              : 
   11781              : tree
   11782      1229031 : trees_in::decl_container ()
   11783              : {
   11784              :   /* The maybe-template.  */
   11785      1229031 :   (void)tree_node ();
   11786              : 
   11787      1229031 :   tree container = tree_node ();
   11788              : 
   11789      1229031 :   return container;
   11790              : }
   11791              : 
   11792              : /* Gets a 2-bit discriminator to distinguish coroutine actor or destroy
   11793              :    functions from a normal function.  */
   11794              : 
   11795              : static int
   11796      1260634 : get_coroutine_discriminator (tree inner)
   11797              : {
   11798      1260634 :   if (DECL_COROUTINE_P (inner))
   11799           72 :     if (tree ramp = DECL_RAMP_FN (inner))
   11800              :       {
   11801           18 :         if (DECL_ACTOR_FN (ramp) == inner)
   11802              :           return 1;
   11803            9 :         else if (DECL_DESTROY_FN (ramp) == inner)
   11804              :           return 2;
   11805              :         else
   11806            0 :           gcc_unreachable ();
   11807              :       }
   11808              :   return 0;
   11809              : }
   11810              : 
   11811              : /* Write out key information about a mergeable DEP.  Does not write
   11812              :    the contents of DEP itself.  The context has already been
   11813              :    written.  The container has already been streamed.  */
   11814              : 
   11815              : void
   11816      4506830 : trees_out::key_mergeable (int tag, merge_kind mk, tree decl, tree inner,
   11817              :                           tree container, depset *dep)
   11818              : {
   11819      4506830 :   if (dep && is_key_order ())
   11820              :     {
   11821      1193553 :       gcc_checking_assert (dep->is_special ());
   11822      1193553 :       dep = dep->deps[0];
   11823              :     }
   11824              : 
   11825      4506830 :   if (streaming_p ())
   11826      1645506 :     dump (dumper::MERGE)
   11827         1101 :       && dump ("Writing:%d's %s merge key (%s) %C:%N", tag, merge_kind_name[mk],
   11828          993 :                dep ? dep->entity_kind_name () : "contained",
   11829         1101 :                TREE_CODE (decl), decl);
   11830              : 
   11831              :   /* Now write the locating information. */
   11832      4506830 :   if (mk & MK_template_mask)
   11833              :     {
   11834              :       /* Specializations are located via their originating template,
   11835              :          and the set of template args they specialize.  */
   11836      1499304 :       gcc_checking_assert (dep && dep->is_special ());
   11837      1499304 :       spec_entry *entry = reinterpret_cast <spec_entry *> (dep->deps[0]);
   11838              : 
   11839      1499304 :       tree_node (entry->tmpl);
   11840      1499304 :       tree_node (entry->args);
   11841      1499304 :       if (mk & MK_tmpl_decl_mask)
   11842      1064316 :         if (flag_concepts && TREE_CODE (inner) == VAR_DECL)
   11843              :           {
   11844              :             /* Variable template partial specializations might need
   11845              :                constraints (see spec_hasher::equal).  It's simpler to
   11846              :                write NULL when we don't need them.  */
   11847        22905 :             tree constraints = NULL_TREE;
   11848              : 
   11849        22905 :             if (uses_template_parms (entry->args))
   11850          723 :               constraints = get_constraints (inner);
   11851        22905 :             tree_node (constraints);
   11852              :           }
   11853              : 
   11854      1499304 :       if (CHECKING_P)
   11855              :         {
   11856              :           /* Make sure we can locate the decl.  */
   11857      1499304 :           tree existing = match_mergeable_specialization
   11858      1499304 :             (bool (mk & MK_tmpl_decl_mask), entry);
   11859              : 
   11860      1499304 :           gcc_assert (existing);
   11861      1499304 :           if (mk & MK_tmpl_decl_mask)
   11862              :             {
   11863      1064316 :               if (mk & MK_tmpl_tmpl_mask)
   11864        10059 :                 existing = DECL_TI_TEMPLATE (existing);
   11865              :             }
   11866              :           else
   11867              :             {
   11868       434988 :               if (mk & MK_tmpl_tmpl_mask)
   11869         2715 :                 existing = CLASSTYPE_TI_TEMPLATE (existing);
   11870              :               else
   11871       432273 :                 existing = TYPE_NAME (existing);
   11872              :             }
   11873              : 
   11874              :           /* The walkabout should have found ourselves.  */
   11875      1499304 :           gcc_checking_assert (TREE_CODE (decl) == TYPE_DECL
   11876              :                                ? same_type_p (TREE_TYPE (decl),
   11877              :                                               TREE_TYPE (existing))
   11878              :                                : existing == decl);
   11879              :         }
   11880              :     }
   11881      3007526 :   else if (mk != MK_unique)
   11882              :     {
   11883      2408941 :       merge_key key;
   11884      2408941 :       tree name = DECL_NAME (decl);
   11885              : 
   11886      2408941 :       switch (mk)
   11887              :         {
   11888            0 :         default:
   11889            0 :           gcc_unreachable ();
   11890              : 
   11891      2030406 :         case MK_named:
   11892      2030406 :         case MK_friend_spec:
   11893      2030406 :           if (IDENTIFIER_CONV_OP_P (name))
   11894         7114 :             name = conv_op_identifier;
   11895              : 
   11896      2030406 :           if (TREE_CODE (inner) == FUNCTION_DECL)
   11897              :             {
   11898              :               /* Functions are distinguished by parameter types.  */
   11899      1128967 :               tree fn_type = TREE_TYPE (inner);
   11900              : 
   11901      1128967 :               key.ref_q = type_memfn_rqual (fn_type);
   11902      1128967 :               key.coro_disc = get_coroutine_discriminator (inner);
   11903      1128967 :               key.iobj_p = DECL_IOBJ_MEMBER_FUNCTION_P (inner);
   11904      1128967 :               key.xobj_p = DECL_XOBJ_MEMBER_FUNCTION_P (inner);
   11905      1128967 :               key.args = TYPE_ARG_TYPES (fn_type);
   11906              : 
   11907      1128967 :               if (tree reqs = get_constraints (inner))
   11908              :                 {
   11909        81309 :                   if (cxx_dialect < cxx20)
   11910           48 :                     reqs = CI_ASSOCIATED_CONSTRAINTS (reqs);
   11911              :                   else
   11912       162570 :                     reqs = CI_DECLARATOR_REQS (reqs);
   11913        81309 :                   key.constraints = reqs;
   11914              :                 }
   11915              : 
   11916      1128967 :               if (IDENTIFIER_CONV_OP_P (name)
   11917      1128967 :                   || (decl != inner
   11918       617067 :                       && !(name == fun_identifier
   11919              :                            /* In case the user names something _FUN  */
   11920          144 :                            && LAMBDA_TYPE_P (DECL_CONTEXT (inner)))))
   11921              :                 /* And a function template, or conversion operator needs
   11922              :                    the return type.  Except for the _FUN thunk of a
   11923              :                    generic lambda, which has a recursive decl_type'd
   11924              :                    return type.  */
   11925              :                 // FIXME: What if the return type is a voldemort?
   11926       624109 :                 key.ret = fndecl_declared_return_type (inner);
   11927              :             }
   11928              :           break;
   11929              : 
   11930       174653 :         case MK_field:
   11931       174653 :           {
   11932       174653 :             unsigned ix = 0;
   11933       174653 :             if (TREE_CODE (inner) != FIELD_DECL)
   11934              :               name = NULL_TREE;
   11935              :             else
   11936        30151 :               gcc_checking_assert (!name || !IDENTIFIER_ANON_P (name));
   11937              : 
   11938       174653 :             for (tree field = TYPE_FIELDS (TREE_TYPE (container));
   11939      3506755 :                  ; field = DECL_CHAIN (field))
   11940              :               {
   11941      3681408 :                 tree finner = STRIP_TEMPLATE (field);
   11942      3681408 :                 if (TREE_CODE (finner) == TREE_CODE (inner))
   11943              :                   {
   11944      1266499 :                     if (finner == inner)
   11945              :                       break;
   11946      1091846 :                     ix++;
   11947              :                   }
   11948      3506755 :               }
   11949       174653 :             key.index = ix;
   11950              :           }
   11951       174653 :           break;
   11952              : 
   11953         8466 :         case MK_vtable:
   11954         8466 :           {
   11955         8466 :             tree vtable = CLASSTYPE_VTABLES (TREE_TYPE (container));
   11956        11082 :             for (unsigned ix = 0; ; vtable = DECL_CHAIN (vtable), ix++)
   11957        11082 :               if (vtable == decl)
   11958              :                 {
   11959         8466 :                   key.index = ix;
   11960         8466 :                   break;
   11961              :                 }
   11962         8466 :             name = NULL_TREE;
   11963              :           }
   11964         8466 :           break;
   11965              : 
   11966        90276 :         case MK_as_base:
   11967        90276 :           gcc_checking_assert
   11968              :             (decl == TYPE_NAME (CLASSTYPE_AS_BASE (TREE_TYPE (container))));
   11969              :           break;
   11970              : 
   11971        24924 :         case MK_local_friend:
   11972        24924 :           {
   11973              :             /* Find by index on the class's DECL_LIST.  We set TREE_CHAIN to
   11974              :                point to the class in push_template_decl or grokfndecl.  */
   11975        24924 :             unsigned ix = 0;
   11976        24924 :             for (tree decls = CLASSTYPE_DECL_LIST (TREE_CHAIN (decl));
   11977       695769 :                  decls; decls = TREE_CHAIN (decls))
   11978       695769 :               if (!TREE_PURPOSE (decls))
   11979              :                 {
   11980        95442 :                   tree frnd = friend_from_decl_list (TREE_VALUE (decls));
   11981        95442 :                   if (frnd == decl)
   11982              :                     break;
   11983        70518 :                   ix++;
   11984              :                 }
   11985        24924 :             key.index = ix;
   11986        24924 :             name = NULL_TREE;
   11987              :           }
   11988        24924 :           break;
   11989              : 
   11990        19800 :         case MK_local_type:
   11991        19800 :           key_local_type (key, STRIP_TEMPLATE (decl), container);
   11992        19800 :           break;
   11993              : 
   11994         1177 :         case MK_enum:
   11995         1177 :           {
   11996              :             /* Anonymous enums are located by their first identifier,
   11997              :                and underlying type.  */
   11998         1177 :             tree type = TREE_TYPE (decl);
   11999              : 
   12000         1177 :             gcc_checking_assert (UNSCOPED_ENUM_P (type));
   12001              :             /* Using the type name drops the bit precision we might
   12002              :                have been using on the enum.  */
   12003         1177 :             key.ret = TYPE_NAME (ENUM_UNDERLYING_TYPE (type));
   12004         1177 :             if (tree values = TYPE_VALUES (type))
   12005         1177 :               name = DECL_NAME (TREE_VALUE (values));
   12006              :           }
   12007              :           break;
   12008              : 
   12009         1216 :         case MK_keyed:
   12010         1216 :           {
   12011         1216 :             tree scope = get_keyed_decl_scope (inner);
   12012         1216 :             gcc_checking_assert (scope);
   12013              : 
   12014         1216 :             auto *root = keyed_table->get (scope);
   12015         1216 :             unsigned ix = root->length ();
   12016              :             /* If we don't find it, we'll write a really big number
   12017              :                that the reader will ignore.  */
   12018         1334 :             while (ix--)
   12019         1334 :               if ((*root)[ix] == inner)
   12020              :                 break;
   12021              : 
   12022              :             /* Use the keyed-to decl as the 'name'.  */
   12023         1216 :             name = scope;
   12024         1216 :             key.index = ix;
   12025              :           }
   12026         1216 :           break;
   12027              : 
   12028        58023 :         case MK_partial:
   12029        58023 :           {
   12030        58023 :             tree ti = get_template_info (inner);
   12031        58023 :             key.constraints = get_constraints (inner);
   12032        58023 :             key.ret = TI_TEMPLATE (ti);
   12033        58023 :             key.args = TI_ARGS (ti);
   12034              :           }
   12035        58023 :           break;
   12036              :         }
   12037              : 
   12038      2408941 :       tree_node (name);
   12039      2408941 :       if (streaming_p ())
   12040              :         {
   12041              :           /* Check we have enough bits for the index.  */
   12042       857420 :           gcc_checking_assert (key.index < (1u << (sizeof (unsigned) * 8 - 6)));
   12043              : 
   12044       857420 :           unsigned code = ((key.ref_q << 0)
   12045       857420 :                            | (key.coro_disc << 2)
   12046       857420 :                            | (key.iobj_p << 4)
   12047       857420 :                            | (key.xobj_p << 5)
   12048       857420 :                            | (key.index << 6));
   12049       857420 :           u (code);
   12050              :         }
   12051              : 
   12052      2408941 :       if (mk == MK_enum)
   12053         1177 :         tree_node (key.ret);
   12054      2407764 :       else if (mk == MK_partial
   12055      2349741 :                || (mk == MK_named && inner
   12056      2030406 :                    && TREE_CODE (inner) == FUNCTION_DECL))
   12057              :         {
   12058      1186990 :           tree_node (key.ret);
   12059      1186990 :           tree arg = key.args;
   12060      1186990 :           if (mk == MK_named)
   12061      3291885 :             while (arg && arg != void_list_node)
   12062              :               {
   12063      2162918 :                 tree_node (TREE_VALUE (arg));
   12064      2162918 :                 arg = TREE_CHAIN (arg);
   12065              :               }
   12066      1186990 :           tree_node (arg);
   12067      1186990 :           tree_node (key.constraints);
   12068              :         }
   12069              :     }
   12070      4506830 : }
   12071              : 
   12072              : /* DECL is a new declaration that may be duplicated in OVL.  Use KEY
   12073              :    to find its clone, or NULL.  If DECL's DECL_NAME is NULL, this
   12074              :    has been found by a proxy.  It will be an enum type located by its
   12075              :    first member.
   12076              : 
   12077              :    We're conservative with matches, so ambiguous decls will be
   12078              :    registered as different, then lead to a lookup error if the two
   12079              :    modules are both visible.  Perhaps we want to do something similar
   12080              :    to duplicate decls to get ODR errors on loading?  We already have
   12081              :    some special casing for namespaces.  */
   12082              : 
   12083              : static tree
   12084       304469 : check_mergeable_decl (merge_kind mk, tree decl, tree ovl, merge_key const &key)
   12085              : {
   12086       304469 :   tree found = NULL_TREE;
   12087      1454729 :   for (ovl_iterator iter (ovl); !found && iter; ++iter)
   12088              :     {
   12089       697255 :       tree match = *iter;
   12090              : 
   12091       697255 :       tree d_inner = decl;
   12092       697255 :       tree m_inner = match;
   12093              : 
   12094       805061 :     again:
   12095       805061 :       if (TREE_CODE (d_inner) != TREE_CODE (m_inner))
   12096              :         {
   12097       124529 :           if (TREE_CODE (match) == NAMESPACE_DECL
   12098       124529 :               && !DECL_NAMESPACE_ALIAS (match))
   12099              :             /* Namespaces are never overloaded.  */
   12100              :             found = match;
   12101              : 
   12102       124529 :           continue;
   12103              :         }
   12104              : 
   12105       680532 :       switch (TREE_CODE (d_inner))
   12106              :         {
   12107       259327 :         case TEMPLATE_DECL:
   12108       259327 :           if (template_heads_equivalent_p (d_inner, m_inner))
   12109              :             {
   12110       107806 :               d_inner = DECL_TEMPLATE_RESULT (d_inner);
   12111       107806 :               m_inner = DECL_TEMPLATE_RESULT (m_inner);
   12112       107806 :               if (d_inner == error_mark_node
   12113       107806 :                   && TYPE_DECL_ALIAS_P (m_inner))
   12114              :                 {
   12115              :                   found = match;
   12116              :                   break;
   12117              :                 }
   12118       107806 :               goto again;
   12119              :             }
   12120              :           break;
   12121              : 
   12122       320804 :         case FUNCTION_DECL:
   12123       320804 :           if (tree m_type = TREE_TYPE (m_inner))
   12124       320804 :             if ((!key.ret
   12125       158668 :                  || same_type_p (key.ret, fndecl_declared_return_type (m_inner)))
   12126       293947 :                 && type_memfn_rqual (m_type) == key.ref_q
   12127       293681 :                 && key.iobj_p == DECL_IOBJ_MEMBER_FUNCTION_P (m_inner)
   12128       587106 :                 && key.xobj_p == DECL_XOBJ_MEMBER_FUNCTION_P (m_inner)
   12129       293660 :                 && compparms (key.args, TYPE_ARG_TYPES (m_type))
   12130       131667 :                 && get_coroutine_discriminator (m_inner) == key.coro_disc
   12131              :                 /* Reject if old is a "C" builtin and new is not "C".
   12132              :                    Matches decls_match behaviour.  */
   12133       131664 :                 && (!DECL_IS_UNDECLARED_BUILTIN (m_inner)
   12134         8208 :                     || !DECL_EXTERN_C_P (m_inner)
   12135         7981 :                     || DECL_EXTERN_C_P (d_inner))
   12136              :                 /* Reject if one is a different member of a
   12137              :                    guarded/pre/post fn set.  */
   12138       131639 :                 && (!flag_contracts
   12139       241580 :                     || (DECL_IS_PRE_FN_P (d_inner)
   12140       120790 :                         == DECL_IS_PRE_FN_P (m_inner)))
   12141       452443 :                 && (!flag_contracts
   12142       241580 :                     || (DECL_IS_POST_FN_P (d_inner)
   12143       120790 :                         == DECL_IS_POST_FN_P (m_inner))))
   12144              :               {
   12145       131639 :                 tree m_reqs = get_constraints (m_inner);
   12146       131639 :                 if (m_reqs)
   12147              :                   {
   12148         9683 :                     if (cxx_dialect < cxx20)
   12149            8 :                       m_reqs = CI_ASSOCIATED_CONSTRAINTS (m_reqs);
   12150              :                     else
   12151        19358 :                       m_reqs = CI_DECLARATOR_REQS (m_reqs);
   12152              :                   }
   12153              : 
   12154       131639 :                 if (cp_tree_equal (key.constraints, m_reqs))
   12155       171033 :                   found = match;
   12156              :               }
   12157              :           break;
   12158              : 
   12159        60932 :         case TYPE_DECL:
   12160       121864 :           if (DECL_IMPLICIT_TYPEDEF_P (d_inner)
   12161        60932 :               == DECL_IMPLICIT_TYPEDEF_P (m_inner))
   12162              :             {
   12163        60896 :               if (!IDENTIFIER_ANON_P (DECL_NAME (m_inner)))
   12164        60767 :                 return match;
   12165          129 :               else if (mk == MK_enum
   12166          129 :                        && (TYPE_NAME (ENUM_UNDERLYING_TYPE (TREE_TYPE (m_inner)))
   12167          129 :                            == key.ret))
   12168              :                 found = match;
   12169              :             }
   12170              :           /* With -freflection, typedef struct { } A is now represented the same
   12171              :              as typedef struct A_ { } A except the TYPE_DECL for A_ is invisible
   12172              :              to name lookup, so we won't be able to find and match it directly.
   12173              :              But we will find the in-TU A (m_inner), through which we can obtain
   12174              :              the in-TU A_ when d_inner is the streamed-in A_.  */
   12175           36 :           else if (flag_reflection
   12176           10 :                    && TYPE_DECL_WAS_UNNAMED (d_inner)
   12177           42 :                    && DECL_ORIGINAL_TYPE (m_inner))
   12178              :             {
   12179            6 :               tree orig = TYPE_NAME (DECL_ORIGINAL_TYPE (m_inner));
   12180            6 :               if (TYPE_DECL_WAS_UNNAMED (orig)
   12181           12 :                   && DECL_NAME (orig) == DECL_NAME (d_inner))
   12182              :                 found = orig;
   12183              :             }
   12184              :           break;
   12185              : 
   12186              :         default:
   12187              :           found = match;
   12188              :           break;
   12189              :         }
   12190              :     }
   12191              : 
   12192       243702 :   return found;
   12193              : }
   12194              : 
   12195              : /* DECL, INNER & TYPE are a skeleton set of nodes for a decl.  Only
   12196              :    the bools have been filled in.  Read its merging key and merge it.
   12197              :    Returns the existing decl if there is one.  */
   12198              : 
   12199              : tree
   12200      1229031 : trees_in::key_mergeable (int tag, merge_kind mk, tree decl, tree inner,
   12201              :                          tree type, tree container, bool is_attached,
   12202              :                          bool is_imported_temploid_friend)
   12203              : {
   12204      1229031 :   const char *kind = "new";
   12205      1229031 :   tree existing = NULL_TREE;
   12206              : 
   12207      1229031 :   if (mk & MK_template_mask)
   12208              :     {
   12209              :       // FIXME: We could stream the specialization hash?
   12210       380508 :       spec_entry spec;
   12211       380508 :       spec.tmpl = tree_node ();
   12212       380508 :       spec.args = tree_node ();
   12213              : 
   12214       380508 :       if (get_overrun ())
   12215            0 :         return error_mark_node;
   12216              : 
   12217       380508 :       DECL_NAME (decl) = DECL_NAME (spec.tmpl);
   12218       380508 :       DECL_CONTEXT (decl) = DECL_CONTEXT (spec.tmpl);
   12219       380508 :       DECL_NAME (inner) = DECL_NAME (decl);
   12220       380508 :       DECL_CONTEXT (inner) = DECL_CONTEXT (decl);
   12221              : 
   12222       380508 :       tree constr = NULL_TREE;
   12223       380508 :       bool is_decl = mk & MK_tmpl_decl_mask;
   12224       380508 :       if (is_decl)
   12225              :         {
   12226       271579 :           if (flag_concepts && TREE_CODE (inner) == VAR_DECL)
   12227              :             {
   12228         5333 :               constr = tree_node ();
   12229         5333 :               if (constr)
   12230            0 :                 set_constraints (inner, constr);
   12231              :             }
   12232       540490 :           spec.spec = (mk & MK_tmpl_tmpl_mask) ? inner : decl;
   12233              :         }
   12234              :       else
   12235       108929 :         spec.spec = type;
   12236       380508 :       existing = match_mergeable_specialization (is_decl, &spec);
   12237       380508 :       if (constr)
   12238              :         /* We'll add these back later, if this is the new decl.  */
   12239            0 :         remove_constraints (inner);
   12240              : 
   12241       380508 :       if (!existing)
   12242              :         ; /* We'll add to the table once read.  */
   12243       151286 :       else if (mk & MK_tmpl_decl_mask)
   12244              :         {
   12245              :           /* A declaration specialization.  */
   12246       104632 :           if (mk & MK_tmpl_tmpl_mask)
   12247         1053 :             existing = DECL_TI_TEMPLATE (existing);
   12248              :         }
   12249              :       else
   12250              :         {
   12251              :           /* A type specialization.  */
   12252        46654 :           if (mk & MK_tmpl_tmpl_mask)
   12253          241 :             existing = CLASSTYPE_TI_TEMPLATE (existing);
   12254              :           else
   12255        46413 :             existing = TYPE_NAME (existing);
   12256              :         }
   12257              :     }
   12258       848523 :   else if (mk == MK_unique)
   12259              :     kind = "unique";
   12260              :   else
   12261              :     {
   12262       631148 :       tree name = tree_node ();
   12263              : 
   12264       631148 :       merge_key key;
   12265       631148 :       unsigned code = u ();
   12266       631148 :       key.ref_q = cp_ref_qualifier ((code >> 0) & 3);
   12267       631148 :       key.coro_disc = (code >> 2) & 3;
   12268       631148 :       key.iobj_p = (code >> 4) & 1;
   12269       631148 :       key.xobj_p = (code >> 5) & 1;
   12270       631148 :       key.index = code >> 6;
   12271              : 
   12272       631148 :       if (mk == MK_enum)
   12273          238 :         key.ret = tree_node ();
   12274       630910 :       else if (mk == MK_partial
   12275       620082 :                || ((mk == MK_named || mk == MK_friend_spec)
   12276       527063 :                    && TREE_CODE (inner) == FUNCTION_DECL))
   12277              :         {
   12278       303715 :           key.ret = tree_node ();
   12279       303715 :           tree arg, *arg_ptr = &key.args;
   12280       303715 :           while ((arg = tree_node ())
   12281       859338 :                  && arg != void_list_node
   12282      1428819 :                  && mk != MK_partial)
   12283              :             {
   12284       557138 :               *arg_ptr = tree_cons (NULL_TREE, arg, NULL_TREE);
   12285       557138 :               arg_ptr = &TREE_CHAIN (*arg_ptr);
   12286              :             }
   12287       303715 :           *arg_ptr = arg;
   12288       303715 :           key.constraints = tree_node ();
   12289              :         }
   12290              : 
   12291       631148 :       if (get_overrun ())
   12292            0 :         return error_mark_node;
   12293              : 
   12294       631148 :       if (mk < MK_indirect_lwm)
   12295              :         {
   12296       619026 :           DECL_NAME (decl) = name;
   12297       619026 :           DECL_CONTEXT (decl) = FROB_CONTEXT (container);
   12298              :         }
   12299       631148 :       DECL_NAME (inner) = DECL_NAME (decl);
   12300       631148 :       DECL_CONTEXT (inner) = DECL_CONTEXT (decl);
   12301              : 
   12302       631148 :       if (mk == MK_partial)
   12303              :         {
   12304        10828 :           for (tree spec = DECL_TEMPLATE_SPECIALIZATIONS (key.ret);
   12305        55429 :                spec; spec = TREE_CHAIN (spec))
   12306              :             {
   12307        50757 :               tree tmpl = TREE_VALUE (spec);
   12308        50757 :               tree ti = get_template_info (tmpl);
   12309        50757 :               if (template_args_equal (key.args, TI_ARGS (ti))
   12310        57706 :                   && cp_tree_equal (key.constraints,
   12311              :                                     get_constraints
   12312         6949 :                                     (DECL_TEMPLATE_RESULT (tmpl))))
   12313              :                 {
   12314              :                   existing = tmpl;
   12315              :                   break;
   12316              :                 }
   12317              :             }
   12318              :         }
   12319       620320 :       else if (mk == MK_keyed
   12320          399 :                && DECL_LANG_SPECIFIC (name)
   12321       620719 :                && DECL_MODULE_KEYED_DECLS_P (name))
   12322              :         {
   12323          399 :           gcc_checking_assert (TREE_CODE (container) == NAMESPACE_DECL
   12324              :                                || TREE_CODE (container) == TYPE_DECL
   12325              :                                || TREE_CODE (container) == FUNCTION_DECL);
   12326          399 :           if (auto *set = keyed_table->get (name))
   12327       631297 :             if (key.index < set->length ())
   12328              :               {
   12329          149 :                 existing = (*set)[key.index];
   12330          149 :                 if (existing)
   12331              :                   {
   12332          149 :                     gcc_checking_assert
   12333              :                       (DECL_IMPLICIT_TYPEDEF_P (existing));
   12334          149 :                     if (inner != decl)
   12335           91 :                       existing
   12336           91 :                         = CLASSTYPE_TI_TEMPLATE (TREE_TYPE (existing));
   12337              :                   }
   12338              :               }
   12339              :         }
   12340              :       else
   12341       619921 :         switch (TREE_CODE (container))
   12342              :           {
   12343            0 :           default:
   12344            0 :             gcc_unreachable ();
   12345              : 
   12346       151472 :           case NAMESPACE_DECL:
   12347       151472 :             if (is_attached
   12348       151472 :                 && !is_imported_temploid_friend
   12349       151472 :                 && !(state->is_module () || state->is_partition ()))
   12350              :               kind = "unique";
   12351              :             else
   12352              :               {
   12353       149389 :                 gcc_checking_assert (mk == MK_named || mk == MK_enum);
   12354       149389 :                 tree mvec;
   12355       149389 :                 tree *vslot = mergeable_namespace_slots (container, name,
   12356              :                                                          is_attached, &mvec);
   12357       149389 :                 existing = check_mergeable_decl (mk, decl, *vslot, key);
   12358       149389 :                 if (!existing)
   12359        72161 :                   add_mergeable_namespace_entity (vslot, decl);
   12360              :                 else
   12361              :                   {
   12362              :                     /* Note that we now have duplicates to deal with in
   12363              :                        name lookup.  */
   12364        77228 :                     if (is_attached)
   12365           66 :                       BINDING_VECTOR_PARTITION_DUPS_P (mvec) = true;
   12366              :                     else
   12367        77162 :                       BINDING_VECTOR_GLOBAL_DUPS_P (mvec) = true;
   12368              :                   }
   12369              :               }
   12370       155744 :             break;
   12371              : 
   12372         4272 :           case FUNCTION_DECL:
   12373         4272 :             gcc_checking_assert (mk == MK_local_type);
   12374         4272 :             existing = key_local_type (key, container, name);
   12375         4272 :             if (existing && inner != decl)
   12376         3426 :               existing = TYPE_TI_TEMPLATE (TREE_TYPE (existing));
   12377              :             break;
   12378              : 
   12379       464177 :           case TYPE_DECL:
   12380       464177 :             gcc_checking_assert (!is_imported_temploid_friend);
   12381       464177 :             int use_tmpl = 0;
   12382         5949 :             if (is_attached && !(state->is_module () || state->is_partition ())
   12383              :                 /* Implicit or in-class defaulted member functions
   12384              :                    can come from anywhere.  */
   12385         4279 :                 && !(TREE_CODE (decl) == FUNCTION_DECL
   12386         1271 :                      && !DECL_THUNK_P (decl)
   12387         1271 :                      && DECL_DEFAULTED_FN (decl)
   12388          820 :                      && !DECL_DEFAULTED_OUTSIDE_CLASS_P (decl))
   12389              :                 /* As can members of template specialisations.  */
   12390       467636 :                 && !(node_template_info (container, use_tmpl)
   12391         1295 :                      && use_tmpl != 0))
   12392              :               kind = "unique";
   12393              :             else
   12394              :               {
   12395       460966 :                 tree ctx = TREE_TYPE (container);
   12396              : 
   12397              :                 /* For some reason templated enumeral types are not marked
   12398              :                    as COMPLETE_TYPE_P, even though they have members.
   12399              :                    This may well be a bug elsewhere.  */
   12400       460966 :                 if (TREE_CODE (ctx) == ENUMERAL_TYPE)
   12401        15644 :                   existing = find_enum_member (ctx, name);
   12402       445322 :                 else if (COMPLETE_TYPE_P (ctx))
   12403              :                   {
   12404       194946 :                     switch (mk)
   12405              :                       {
   12406            0 :                       default:
   12407            0 :                         gcc_unreachable ();
   12408              : 
   12409       155509 :                       case MK_named:
   12410       155509 :                         existing = lookup_class_binding (ctx, name);
   12411       155509 :                         if (existing)
   12412              :                           {
   12413       155080 :                             tree inner = decl;
   12414       155080 :                             if (TREE_CODE (inner) == TEMPLATE_DECL
   12415       155080 :                                 && !DECL_MEMBER_TEMPLATE_P (inner))
   12416        68977 :                               inner = DECL_TEMPLATE_RESULT (inner);
   12417              : 
   12418       155080 :                             existing = check_mergeable_decl
   12419       155080 :                               (mk, inner, existing, key);
   12420              : 
   12421       155080 :                             if (!existing && DECL_ALIAS_TEMPLATE_P (decl))
   12422              :                               {} // FIXME: Insert into specialization
   12423              :                             // tables, we'll need the arguments for that!
   12424              :                           }
   12425              :                         break;
   12426              : 
   12427        26880 :                       case MK_field:
   12428        26880 :                         {
   12429        26880 :                           unsigned ix = key.index;
   12430        26880 :                           for (tree field = TYPE_FIELDS (ctx);
   12431       892926 :                                field; field = DECL_CHAIN (field))
   12432              :                             {
   12433       892926 :                               tree finner = STRIP_TEMPLATE (field);
   12434       892926 :                               if (TREE_CODE (finner) == TREE_CODE (inner))
   12435       299740 :                                 if (!ix--)
   12436              :                                   {
   12437              :                                     existing = field;
   12438              :                                     break;
   12439              :                                   }
   12440              :                             }
   12441              :                         }
   12442              :                         break;
   12443              : 
   12444         1484 :                       case MK_vtable:
   12445         1484 :                         {
   12446         1484 :                           unsigned ix = key.index;
   12447         1484 :                           for (tree vtable = CLASSTYPE_VTABLES (ctx);
   12448         1823 :                                vtable; vtable = DECL_CHAIN (vtable))
   12449         1570 :                             if (!ix--)
   12450              :                               {
   12451              :                                 existing = vtable;
   12452              :                                 break;
   12453              :                               }
   12454              :                         }
   12455              :                         break;
   12456              : 
   12457         8044 :                       case MK_as_base:
   12458         8044 :                         {
   12459         8044 :                           tree as_base = CLASSTYPE_AS_BASE (ctx);
   12460         8044 :                           if (as_base && as_base != ctx)
   12461         8044 :                             existing = TYPE_NAME (as_base);
   12462              :                         }
   12463              :                         break;
   12464              : 
   12465         3029 :                       case MK_local_friend:
   12466         3029 :                         {
   12467         3029 :                           unsigned ix = key.index;
   12468         3029 :                           for (tree decls = CLASSTYPE_DECL_LIST (ctx);
   12469        85052 :                                decls; decls = TREE_CHAIN (decls))
   12470        85052 :                             if (!TREE_PURPOSE (decls) && !ix--)
   12471              :                               {
   12472         3029 :                                 existing
   12473         3029 :                                   = friend_from_decl_list (TREE_VALUE (decls));
   12474         3029 :                                 break;
   12475              :                               }
   12476              :                         }
   12477              :                         break;
   12478              :                       }
   12479              : 
   12480       194946 :                     if (existing && mk < MK_indirect_lwm && mk != MK_partial
   12481       190733 :                         && TREE_CODE (decl) == TEMPLATE_DECL
   12482       282888 :                         && !DECL_MEMBER_TEMPLATE_P (decl))
   12483              :                       {
   12484        70977 :                         tree ti;
   12485        70977 :                         if (DECL_IMPLICIT_TYPEDEF_P (existing))
   12486          914 :                           ti = TYPE_TEMPLATE_INFO (TREE_TYPE (existing));
   12487              :                         else
   12488        70063 :                           ti = DECL_TEMPLATE_INFO (existing);
   12489        70977 :                         existing = TI_TEMPLATE (ti);
   12490              :                       }
   12491              :                   }
   12492              :               }
   12493              :           }
   12494              :     }
   12495              : 
   12496      1229031 :   dump (dumper::MERGE)
   12497         3208 :     && dump ("Read:%d's %s merge key (%s) %C:%N", tag, merge_kind_name[mk],
   12498         3208 :              existing ? "matched" : kind, TREE_CODE (decl), decl);
   12499              : 
   12500              :   return existing;
   12501              : }
   12502              : 
   12503              : void
   12504       351554 : trees_out::binfo_mergeable (tree binfo)
   12505              : {
   12506       351554 :   tree dom = binfo;
   12507       444644 :   while (tree parent = BINFO_INHERITANCE_CHAIN (dom))
   12508              :     dom = parent;
   12509       351554 :   tree type = BINFO_TYPE (dom);
   12510       351554 :   gcc_checking_assert (TYPE_BINFO (type) == dom);
   12511       351554 :   tree_node (type);
   12512       351554 :   if (streaming_p ())
   12513              :     {
   12514              :       unsigned ix = 0;
   12515       187968 :       for (; dom != binfo; dom = TREE_CHAIN (dom))
   12516        48965 :         ix++;
   12517       139003 :       u (ix);
   12518              :     }
   12519       351554 : }
   12520              : 
   12521              : unsigned
   12522        93722 : trees_in::binfo_mergeable (tree *type)
   12523              : {
   12524        93722 :   *type = tree_node ();
   12525        93722 :   return u ();
   12526              : }
   12527              : 
   12528              : /* DECL is a just streamed declaration with attributes DATTR that should
   12529              :    have matching ABI tags as EXISTING's attributes EATTR.  Check that the
   12530              :    ABI tags match, and report an error if not.  */
   12531              : 
   12532              : void
   12533       298606 : trees_in::check_abi_tags (tree existing, tree decl, tree &eattr, tree &dattr)
   12534              : {
   12535       298606 :   tree etags = lookup_attribute ("abi_tag", eattr);
   12536       298606 :   tree dtags = lookup_attribute ("abi_tag", dattr);
   12537       298606 :   if ((etags == nullptr) != (dtags == nullptr)
   12538       298606 :       || (etags && !attribute_value_equal (etags, dtags)))
   12539              :     {
   12540           33 :       if (etags)
   12541           24 :         etags = TREE_VALUE (etags);
   12542           33 :       if (dtags)
   12543           24 :         dtags = TREE_VALUE (dtags);
   12544              : 
   12545              :       /* We only error if mangling wouldn't consider the tags equivalent.
   12546              :          Since tags might have been inherited during mangling, ignore
   12547              :          inherited tags if there's a mangled-ness mismatch.  */
   12548           33 :       bool ignore_inherited_p
   12549           33 :         = (DECL_ASSEMBLER_NAME_SET_P (STRIP_TEMPLATE (existing))
   12550           33 :            != DECL_ASSEMBLER_NAME_SET_P (STRIP_TEMPLATE (decl)));
   12551           33 :       if (!equal_abi_tags (etags, dtags, ignore_inherited_p))
   12552              :         {
   12553           21 :           auto_diagnostic_group d;
   12554           21 :           if (dtags)
   12555           15 :             error_at (DECL_SOURCE_LOCATION (decl),
   12556              :                       "mismatching abi tags for %qD with tags %qE",
   12557              :                       decl, dtags);
   12558              :           else
   12559            6 :             error_at (DECL_SOURCE_LOCATION (decl),
   12560              :                       "mismatching abi tags for %qD with no tags", decl);
   12561           21 :           if (etags)
   12562           15 :             inform (DECL_SOURCE_LOCATION (existing),
   12563              :                     "existing declaration here with tags %qE", etags);
   12564              :           else
   12565            6 :             inform (DECL_SOURCE_LOCATION (existing),
   12566              :                     "existing declaration here with no tags");
   12567           21 :         }
   12568              : 
   12569              :       /* Always use the existing abi_tags as the canonical set so that
   12570              :          later processing doesn't get confused.  */
   12571           33 :       if (dtags)
   12572           24 :         dattr = remove_attribute ("abi_tag", dattr);
   12573           33 :       if (etags)
   12574           24 :         duplicate_one_attribute (&dattr, eattr, "abi_tag");
   12575              :     }
   12576       298606 : }
   12577              : 
   12578              : /* DECL is a just streamed mergeable decl that should match EXISTING.  Check
   12579              :    it does and issue an appropriate diagnostic if not.  Merge any
   12580              :    bits from DECL to EXISTING.  This is stricter matching than
   12581              :    decls_match, because we can rely on ODR-sameness, and we cannot use
   12582              :    decls_match because it can cause instantiations of constraints.  */
   12583              : 
   12584              : bool
   12585       437447 : trees_in::is_matching_decl (tree existing, tree decl, bool is_typedef)
   12586              : {
   12587              :   // FIXME: We should probably do some duplicate decl-like stuff here
   12588              :   // (beware, default parms should be the same?)  Can we just call
   12589              :   // duplicate_decls and teach it how to handle the module-specific
   12590              :   // permitted/required duplications?
   12591              : 
   12592              :   // We know at this point that the decls have matched by key, so we
   12593              :   // can elide some of the checking
   12594       437447 :   gcc_checking_assert (TREE_CODE (existing) == TREE_CODE (decl));
   12595              : 
   12596       437447 :   tree d_inner = decl;
   12597       437447 :   tree e_inner = existing;
   12598       437447 :   if (TREE_CODE (decl) == TEMPLATE_DECL)
   12599              :     {
   12600       145579 :       d_inner = DECL_TEMPLATE_RESULT (d_inner);
   12601       145579 :       e_inner = DECL_TEMPLATE_RESULT (e_inner);
   12602       145579 :       gcc_checking_assert (TREE_CODE (e_inner) == TREE_CODE (d_inner));
   12603              :     }
   12604              : 
   12605              :   // FIXME: do more precise errors at point of mismatch
   12606       437447 :   const char *mismatch_msg = nullptr;
   12607              : 
   12608       437447 :   if (VAR_OR_FUNCTION_DECL_P (d_inner)
   12609       437447 :       && DECL_EXTERN_C_P (d_inner) != DECL_EXTERN_C_P (e_inner))
   12610              :     {
   12611            6 :       mismatch_msg = G_("conflicting language linkage for imported "
   12612              :                         "declaration %#qD");
   12613            6 :       goto mismatch;
   12614              :     }
   12615       437441 :   else if (TREE_CODE (d_inner) == FUNCTION_DECL)
   12616              :     {
   12617       200242 :       tree e_ret = fndecl_declared_return_type (existing);
   12618       200242 :       tree d_ret = fndecl_declared_return_type (decl);
   12619              : 
   12620        85431 :       if (decl != d_inner && DECL_NAME (d_inner) == fun_identifier
   12621       200266 :           && LAMBDA_TYPE_P (DECL_CONTEXT (d_inner)))
   12622              :         /* This has a recursive type that will compare different.  */;
   12623       200230 :       else if (!same_type_p (d_ret, e_ret))
   12624              :         {
   12625           25 :           mismatch_msg = G_("conflicting type for imported declaration %#qD");
   12626           25 :           goto mismatch;
   12627              :         }
   12628              : 
   12629       200217 :       tree& e_type = TREE_TYPE (e_inner);
   12630       200217 :       tree d_type = TREE_TYPE (d_inner);
   12631              : 
   12632       200217 :       for (tree e_args = TYPE_ARG_TYPES (e_type),
   12633       200217 :              d_args = TYPE_ARG_TYPES (d_type);
   12634       343783 :            e_args != d_args && (e_args || d_args);
   12635       143566 :            e_args = TREE_CHAIN (e_args), d_args = TREE_CHAIN (d_args))
   12636              :         {
   12637       143566 :           if (!(e_args && d_args))
   12638              :             {
   12639            0 :               mismatch_msg = G_("conflicting argument list for imported "
   12640              :                                 "declaration %#qD");
   12641            0 :               goto mismatch;
   12642              :             }
   12643              : 
   12644       143566 :           if (!same_type_p (TREE_VALUE (d_args), TREE_VALUE (e_args)))
   12645              :             {
   12646            0 :               mismatch_msg = G_("conflicting argument types for imported "
   12647              :                                 "declaration %#qD");
   12648            0 :               goto mismatch;
   12649              :             }
   12650              :         }
   12651              : 
   12652              :       /* If EXISTING has an undeduced or uninstantiated exception
   12653              :          specification, but DECL does not, propagate the exception
   12654              :          specification.  Otherwise we end up asserting or trying to
   12655              :          instantiate it in the middle of loading.   */
   12656       200217 :       tree e_spec = TYPE_RAISES_EXCEPTIONS (e_type);
   12657       200217 :       tree d_spec = TYPE_RAISES_EXCEPTIONS (d_type);
   12658       293128 :       if (DECL_MAYBE_DELETED (e_inner) || DEFERRED_NOEXCEPT_SPEC_P (e_spec))
   12659              :         {
   12660        31676 :           if (!(DECL_MAYBE_DELETED (d_inner)
   12661        15780 :                 || DEFERRED_NOEXCEPT_SPEC_P (d_spec))
   12662        47237 :               || (UNEVALUATED_NOEXCEPT_SPEC_P (e_spec)
   12663        25810 :                   && !UNEVALUATED_NOEXCEPT_SPEC_P (d_spec)))
   12664              :             {
   12665          141 :               dump (dumper::MERGE)
   12666            6 :                 && dump ("Propagating instantiated noexcept to %N", existing);
   12667          141 :               gcc_checking_assert (existing == e_inner);
   12668          141 :               e_type = build_exception_variant (e_type, d_spec);
   12669              : 
   12670              :               /* Propagate to existing clones.  */
   12671          141 :               tree clone;
   12672          399 :               FOR_EACH_CLONE (clone, existing)
   12673          258 :                 TREE_TYPE (clone)
   12674          516 :                   = build_exception_variant (TREE_TYPE (clone), d_spec);
   12675              :             }
   12676              :         }
   12677       184321 :       else if (!DECL_MAYBE_DELETED (d_inner)
   12678       263117 :                && !DEFERRED_NOEXCEPT_SPEC_P (d_spec)
   12679       368641 :                && !comp_except_specs (d_spec, e_spec, ce_type))
   12680              :         {
   12681         1736 :           mismatch_msg = G_("conflicting %<noexcept%> specifier for "
   12682              :                             "imported declaration %#qD");
   12683         1736 :           goto mismatch;
   12684              :         }
   12685              : 
   12686              :       /* Similarly if EXISTING has an undeduced return type, but DECL's
   12687              :          is already deduced.  */
   12688       198481 :       bool e_undeduced = undeduced_auto_decl (existing);
   12689       198481 :       bool d_undeduced = undeduced_auto_decl (decl);
   12690       198481 :       if (e_undeduced && !d_undeduced)
   12691              :         {
   12692           16 :           dump (dumper::MERGE)
   12693            0 :             && dump ("Propagating deduced return type to %N", existing);
   12694           16 :           gcc_checking_assert (existing == e_inner);
   12695           16 :           FNDECL_USED_AUTO (existing) = true;
   12696           16 :           DECL_SAVED_AUTO_RETURN_TYPE (existing) = TREE_TYPE (e_type);
   12697           16 :           e_type = change_return_type (TREE_TYPE (d_type), e_type);
   12698              :         }
   12699       198465 :       else if (d_undeduced && !e_undeduced)
   12700              :         /* EXISTING was deduced, leave it alone.  */;
   12701       198462 :       else if (type_uses_auto (d_ret)
   12702       198462 :                && !same_type_p (TREE_TYPE (d_type), TREE_TYPE (e_type)))
   12703              :         {
   12704            9 :           mismatch_msg = G_("conflicting deduced return type for "
   12705              :                             "imported declaration %#qD");
   12706            9 :           goto mismatch;
   12707              :         }
   12708              : 
   12709              :       /* Similarly if EXISTING has undeduced constexpr, but DECL's
   12710              :          is already deduced.  */
   12711       198472 :       if (DECL_DECLARED_CONSTEXPR_P (e_inner)
   12712       198472 :           == DECL_DECLARED_CONSTEXPR_P (d_inner))
   12713              :         /* Already matches.  */;
   12714            2 :       else if (DECL_DECLARED_CONSTEXPR_P (d_inner)
   12715            2 :                && (DECL_MAYBE_DELETED (e_inner)
   12716            0 :                    || decl_implicit_constexpr_p (d_inner)))
   12717              :         /* DECL was deduced, copy to EXISTING.  */
   12718              :         {
   12719            1 :           DECL_DECLARED_CONSTEXPR_P (e_inner) = true;
   12720            1 :           if (decl_implicit_constexpr_p (d_inner))
   12721            0 :             DECL_LANG_SPECIFIC (e_inner)->u.fn.implicit_constexpr = true;
   12722              :         }
   12723            1 :       else if (DECL_DECLARED_CONSTEXPR_P (e_inner)
   12724            1 :                && (DECL_MAYBE_DELETED (d_inner)
   12725            0 :                    || decl_implicit_constexpr_p (e_inner)))
   12726              :         /* EXISTING was deduced, leave it alone.  */;
   12727              :       else
   12728              :         {
   12729            0 :           mismatch_msg = G_("conflicting %<constexpr%> for imported "
   12730              :                             "declaration %#qD");
   12731            0 :           goto mismatch;
   12732              :         }
   12733              : 
   12734              :       /* Don't synthesize a defaulted function if we're importing one
   12735              :          we've already determined.  */
   12736       198472 :       if (!DECL_MAYBE_DELETED (d_inner))
   12737       198355 :         DECL_MAYBE_DELETED (e_inner) = false;
   12738              :     }
   12739       237199 :   else if (is_typedef)
   12740              :     {
   12741        84503 :       if (!DECL_ORIGINAL_TYPE (e_inner)
   12742        84503 :           || !same_type_p (DECL_ORIGINAL_TYPE (d_inner),
   12743              :                            DECL_ORIGINAL_TYPE (e_inner)))
   12744              :         {
   12745            3 :           mismatch_msg = G_("conflicting imported declaration %q#D");
   12746            3 :           goto mismatch;
   12747              :         }
   12748              :     }
   12749              :   /* Using cp_tree_equal because we can meet TYPE_ARGUMENT_PACKs
   12750              :      here. I suspect the entities that directly do that are things
   12751              :      that shouldn't go to duplicate_decls (FIELD_DECLs etc).   */
   12752       152696 :   else if (!cp_tree_equal (TREE_TYPE (decl), TREE_TYPE (existing)))
   12753              :     {
   12754              :       mismatch_msg = G_("conflicting type for imported declaration %#qD");
   12755         2501 :     mismatch:
   12756         2501 :       if (DECL_IS_UNDECLARED_BUILTIN (existing))
   12757              :         /* Just like duplicate_decls, presum the user knows what
   12758              :            they're doing in overriding a builtin.   */
   12759         1758 :         TREE_TYPE (existing) = TREE_TYPE (decl);
   12760          743 :       else if (decl_function_context (decl))
   12761              :         /* The type of a mergeable local entity (such as a function scope
   12762              :            capturing lambda's closure type fields) can depend on an
   12763              :            unmergeable local entity (such as a local variable), so type
   12764              :            equality isn't feasible in general for local entities.  */;
   12765              :       else
   12766              :         {
   12767           21 :           gcc_checking_assert (mismatch_msg);
   12768           21 :           auto_diagnostic_group d;
   12769           21 :           error_at (DECL_SOURCE_LOCATION (decl), mismatch_msg, decl);
   12770           21 :           inform (DECL_SOURCE_LOCATION (existing),
   12771              :                   "existing declaration %#qD", existing);
   12772           21 :           return false;
   12773           21 :         }
   12774              :     }
   12775              : 
   12776       437426 :   if (DECL_IS_UNDECLARED_BUILTIN (existing)
   12777       437426 :       && !DECL_IS_UNDECLARED_BUILTIN (decl))
   12778              :     {
   12779              :       /* We're matching a builtin that the user has yet to declare.
   12780              :          We are the one!  This is very much duplicate-decl
   12781              :          shenanigans. */
   12782         2041 :       DECL_SOURCE_LOCATION (existing) = DECL_SOURCE_LOCATION (decl);
   12783         2041 :       if (TREE_CODE (decl) != TYPE_DECL)
   12784              :         {
   12785              :           /* Propagate exceptions etc.  */
   12786         2018 :           TREE_TYPE (existing) = TREE_TYPE (decl);
   12787         2018 :           TREE_NOTHROW (existing) = TREE_NOTHROW (decl);
   12788              :         }
   12789              :       /* This is actually an import! */
   12790         2041 :       DECL_MODULE_IMPORT_P (existing) = true;
   12791              : 
   12792              :       /* Yay, sliced!  */
   12793         2041 :       existing->base = decl->base;
   12794              : 
   12795         2041 :       if (TREE_CODE (decl) == FUNCTION_DECL)
   12796              :         {
   12797              :           /* Ew :(  */
   12798         2018 :           memcpy (&existing->decl_common.size,
   12799              :                   &decl->decl_common.size,
   12800              :                   (offsetof (tree_decl_common, pt_uid)
   12801              :                    - offsetof (tree_decl_common, size)));
   12802         2018 :           auto bltin_class = DECL_BUILT_IN_CLASS (decl);
   12803         2018 :           existing->function_decl.built_in_class = bltin_class;
   12804         2018 :           auto fncode = DECL_UNCHECKED_FUNCTION_CODE (decl);
   12805         2018 :           DECL_UNCHECKED_FUNCTION_CODE (existing) = fncode;
   12806         2018 :           if (existing->function_decl.built_in_class == BUILT_IN_NORMAL)
   12807              :             {
   12808         1808 :               if (builtin_decl_explicit_p (built_in_function (fncode)))
   12809         1808 :                 switch (fncode)
   12810              :                   {
   12811            0 :                   case BUILT_IN_STPCPY:
   12812            0 :                     set_builtin_decl_implicit_p
   12813            0 :                       (built_in_function (fncode), true);
   12814            0 :                     break;
   12815         1808 :                   default:
   12816         1808 :                     set_builtin_decl_declared_p
   12817         1808 :                       (built_in_function (fncode), true);
   12818         1808 :                     break;
   12819              :                   }
   12820         1808 :               copy_attributes_to_builtin (decl);
   12821              :             }
   12822              :         }
   12823              :     }
   12824              : 
   12825       437426 :   if (VAR_OR_FUNCTION_DECL_P (decl)
   12826       437426 :       && DECL_TEMPLATE_INSTANTIATED (decl))
   12827              :     /* Don't instantiate again!  */
   12828         9177 :     DECL_TEMPLATE_INSTANTIATED (existing) = true;
   12829              : 
   12830       437426 :   if (TREE_CODE (d_inner) == FUNCTION_DECL
   12831       437426 :       && DECL_DECLARED_INLINE_P (d_inner))
   12832              :     {
   12833       162002 :       DECL_DECLARED_INLINE_P (e_inner) = true;
   12834       162002 :       if (!DECL_SAVED_TREE (e_inner)
   12835        80194 :           && lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (d_inner))
   12836       162019 :           && !lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (e_inner)))
   12837              :         {
   12838           51 :           DECL_INTERFACE_KNOWN (e_inner)
   12839           17 :             |= DECL_INTERFACE_KNOWN (d_inner);
   12840           17 :           DECL_DISREGARD_INLINE_LIMITS (e_inner)
   12841           17 :             |= DECL_DISREGARD_INLINE_LIMITS (d_inner);
   12842              :           // TODO: we will eventually want to merge all decl attributes
   12843           17 :           duplicate_one_attribute (&DECL_ATTRIBUTES (e_inner),
   12844           17 :                                    DECL_ATTRIBUTES (d_inner), "gnu_inline");
   12845              :         }
   12846              :     }
   12847       437426 :   if (!DECL_EXTERNAL (d_inner))
   12848       205276 :     DECL_EXTERNAL (e_inner) = false;
   12849              : 
   12850       437426 :   if (VAR_OR_FUNCTION_DECL_P (d_inner))
   12851       441406 :     check_abi_tags (existing, decl,
   12852       220703 :                     DECL_ATTRIBUTES (e_inner), DECL_ATTRIBUTES (d_inner));
   12853              : 
   12854       437426 :   if (TREE_CODE (decl) == TEMPLATE_DECL)
   12855              :     {
   12856              :       /* Merge default template arguments.  */
   12857       145577 :       tree d_parms = DECL_INNERMOST_TEMPLATE_PARMS (decl);
   12858       145577 :       tree e_parms = DECL_INNERMOST_TEMPLATE_PARMS (existing);
   12859       145577 :       gcc_checking_assert (TREE_VEC_LENGTH (d_parms)
   12860              :                            == TREE_VEC_LENGTH (e_parms));
   12861       417277 :       for (int i = 0; i < TREE_VEC_LENGTH (d_parms); ++i)
   12862              :         {
   12863       271709 :           tree d_default = TREE_PURPOSE (TREE_VEC_ELT (d_parms, i));
   12864       271709 :           tree& e_default = TREE_PURPOSE (TREE_VEC_ELT (e_parms, i));
   12865       271709 :           if (e_default == NULL_TREE)
   12866       229976 :             e_default = d_default;
   12867        41733 :           else if (d_default != NULL_TREE
   12868        41733 :                    && !cp_tree_equal (d_default, e_default))
   12869              :             {
   12870            9 :               auto_diagnostic_group d;
   12871            9 :               tree d_parm = TREE_VALUE (TREE_VEC_ELT (d_parms, i));
   12872            9 :               tree e_parm = TREE_VALUE (TREE_VEC_ELT (e_parms, i));
   12873            9 :               error_at (DECL_SOURCE_LOCATION (d_parm),
   12874              :                         "conflicting default argument for %#qD", d_parm);
   12875            9 :               inform (DECL_SOURCE_LOCATION (e_parm),
   12876              :                       "existing default declared here");
   12877            9 :               return false;
   12878            9 :             }
   12879              :         }
   12880              :     }
   12881              : 
   12882       437417 :   if (TREE_CODE (d_inner) == FUNCTION_DECL)
   12883              :     {
   12884              :       /* Merge default function arguments.  */
   12885       200233 :       tree d_parm = FUNCTION_FIRST_USER_PARMTYPE (d_inner);
   12886       200233 :       tree e_parm = FUNCTION_FIRST_USER_PARMTYPE (e_inner);
   12887       200233 :       int i = 0;
   12888       477325 :       for (; d_parm && d_parm != void_list_node;
   12889       277092 :            d_parm = TREE_CHAIN (d_parm), e_parm = TREE_CHAIN (e_parm), ++i)
   12890              :         {
   12891       277104 :           tree d_default = TREE_PURPOSE (d_parm);
   12892       277104 :           tree& e_default = TREE_PURPOSE (e_parm);
   12893       277104 :           if (e_default == NULL_TREE)
   12894       260528 :             e_default = d_default;
   12895        16576 :           else if (d_default != NULL_TREE
   12896        16576 :                    && !cp_tree_equal (d_default, e_default))
   12897              :             {
   12898           12 :               auto_diagnostic_group d;
   12899           12 :               error_at (get_fndecl_argument_location (d_inner, i),
   12900              :                         "conflicting default argument for parameter %P of %#qD",
   12901              :                         i, decl);
   12902           12 :               inform (get_fndecl_argument_location (e_inner, i),
   12903              :                       "existing default declared here");
   12904           12 :               return false;
   12905           12 :             }
   12906              :         }
   12907              :     }
   12908              : 
   12909              :   return true;
   12910              : }
   12911              : 
   12912              : /* FN is an implicit member function that we've discovered is new to
   12913              :    the class.  Add it to the TYPE_FIELDS chain and the method vector.
   12914              :    Reset the appropriate classtype lazy flag.   */
   12915              : 
   12916              : bool
   12917          931 : trees_in::install_implicit_member (tree fn)
   12918              : {
   12919          931 :   tree ctx = DECL_CONTEXT (fn);
   12920          931 :   tree name = DECL_NAME (fn);
   12921              :   /* We know these are synthesized, so the set of expected prototypes
   12922              :      is quite restricted.  We're not validating correctness, just
   12923              :      distinguishing between the small set of possibilities.  */
   12924          931 :   tree parm_type = TREE_VALUE (FUNCTION_FIRST_USER_PARMTYPE (fn));
   12925          931 :   if (IDENTIFIER_CTOR_P (name))
   12926              :     {
   12927          647 :       if (CLASSTYPE_LAZY_DEFAULT_CTOR (ctx)
   12928          647 :           && VOID_TYPE_P (parm_type))
   12929          167 :         CLASSTYPE_LAZY_DEFAULT_CTOR (ctx) = false;
   12930          480 :       else if (!TYPE_REF_P (parm_type))
   12931              :         return false;
   12932          480 :       else if (CLASSTYPE_LAZY_COPY_CTOR (ctx)
   12933          480 :                && !TYPE_REF_IS_RVALUE (parm_type))
   12934          243 :         CLASSTYPE_LAZY_COPY_CTOR (ctx) = false;
   12935          237 :       else if (CLASSTYPE_LAZY_MOVE_CTOR (ctx))
   12936          237 :         CLASSTYPE_LAZY_MOVE_CTOR (ctx) = false;
   12937              :       else
   12938              :         return false;
   12939              :     }
   12940          284 :   else if (IDENTIFIER_DTOR_P (name))
   12941              :     {
   12942          240 :       if (CLASSTYPE_LAZY_DESTRUCTOR (ctx))
   12943          240 :         CLASSTYPE_LAZY_DESTRUCTOR (ctx) = false;
   12944              :       else
   12945              :         return false;
   12946          240 :       if (DECL_VIRTUAL_P (fn))
   12947              :         /* A virtual dtor should have been created when the class
   12948              :            became complete.  */
   12949              :         return false;
   12950              :     }
   12951           44 :   else if (name == assign_op_identifier)
   12952              :     {
   12953           44 :       if (!TYPE_REF_P (parm_type))
   12954              :         return false;
   12955           44 :       else if (CLASSTYPE_LAZY_COPY_ASSIGN (ctx)
   12956           44 :                && !TYPE_REF_IS_RVALUE (parm_type))
   12957           22 :         CLASSTYPE_LAZY_COPY_ASSIGN (ctx) = false;
   12958           22 :       else if (CLASSTYPE_LAZY_MOVE_ASSIGN (ctx))
   12959           22 :         CLASSTYPE_LAZY_MOVE_ASSIGN (ctx) = false;
   12960              :       else
   12961              :         return false;
   12962              :     }
   12963              :   else
   12964              :     return false;
   12965              : 
   12966          976 :   dump (dumper::MERGE) && dump ("Adding implicit member %N", fn);
   12967              : 
   12968          931 :   DECL_CHAIN (fn) = TYPE_FIELDS (ctx);
   12969          931 :   TYPE_FIELDS (ctx) = fn;
   12970              : 
   12971          931 :   add_method (ctx, fn, false);
   12972              : 
   12973              :     /* Propagate TYPE_FIELDS.  */
   12974          931 :   fixup_type_variants (ctx);
   12975              : 
   12976          931 :   return true;
   12977              : }
   12978              : 
   12979              : /* Return non-zero if DECL has a definition that would be interesting to
   12980              :    write out.  */
   12981              : 
   12982              : static bool
   12983      2084473 : has_definition (tree decl)
   12984              : {
   12985      2084479 :   bool is_tmpl = TREE_CODE (decl) == TEMPLATE_DECL;
   12986      2084479 :   if (is_tmpl)
   12987       457624 :     decl = DECL_TEMPLATE_RESULT (decl);
   12988              : 
   12989      2084479 :   switch (TREE_CODE (decl))
   12990              :     {
   12991              :     default:
   12992              :       break;
   12993              : 
   12994       781646 :     case FUNCTION_DECL:
   12995       781646 :       if (!DECL_SAVED_TREE (decl))
   12996              :         /* Not defined.  */
   12997              :         break;
   12998              : 
   12999       352601 :       if (DECL_DECLARED_INLINE_P (decl))
   13000              :         return true;
   13001              : 
   13002        28780 :       if (header_module_p ())
   13003              :         /* We always need to write definitions in header modules,
   13004              :            since there's no TU to emit them in otherwise.  */
   13005              :         return true;
   13006              : 
   13007        17083 :       if (DECL_TEMPLATE_INFO (decl))
   13008              :         {
   13009        15792 :           int use_tpl = DECL_USE_TEMPLATE (decl);
   13010              : 
   13011              :           // FIXME: Partial specializations have definitions too.
   13012        15792 :           if (use_tpl < 2)
   13013              :             return true;
   13014              :         }
   13015              : 
   13016              :       /* Coroutine transform functions always need to be emitted
   13017              :          into the importing TU if the ramp function will be.  */
   13018         1351 :       if (DECL_COROUTINE_P (decl))
   13019           12 :         if (tree ramp = DECL_RAMP_FN (decl))
   13020              :           return has_definition (ramp);
   13021              :       break;
   13022              : 
   13023       869433 :     case TYPE_DECL:
   13024       869433 :       {
   13025       869433 :         tree type = TREE_TYPE (decl);
   13026       869433 :         if (type == TYPE_MAIN_VARIANT (type)
   13027       399664 :             && decl == TYPE_NAME (type)
   13028      1269097 :             && (TREE_CODE (type) == ENUMERAL_TYPE
   13029       399664 :                 ? TYPE_VALUES (type) : TYPE_FIELDS (type)))
   13030              :           return true;
   13031              :       }
   13032              :       break;
   13033              : 
   13034       110599 :     case VAR_DECL:
   13035              :       /* DECL_INITIALIZED_P might not be set on a dependent VAR_DECL.  */
   13036       110599 :       if (DECL_LANG_SPECIFIC (decl)
   13037       109751 :           && DECL_TEMPLATE_INFO (decl)
   13038       180735 :           && DECL_INITIAL (decl))
   13039              :         return true;
   13040              :       else
   13041              :         {
   13042        45590 :           if (!DECL_INITIALIZED_P (decl))
   13043              :             /* Not defined.  */
   13044              :             return false;
   13045              : 
   13046        38202 :           if (header_module_p ())
   13047              :             /* We always need to write definitions in header modules,
   13048              :                since there's no TU to emit them in otherwise.  */
   13049              :             return true;
   13050              : 
   13051        12730 :           if (decl_maybe_constant_var_p (decl))
   13052              :             /* We might need its constant value.  */
   13053              :             return true;
   13054              : 
   13055          482 :           if (vague_linkage_p (decl))
   13056              :             /* These are emitted as needed.  */
   13057              :             return true;
   13058              : 
   13059              :           return false;
   13060              :         }
   13061         7905 :       break;
   13062              : 
   13063         7905 :     case CONCEPT_DECL:
   13064         7905 :       if (DECL_INITIAL (decl))
   13065              :         return true;
   13066              : 
   13067              :       break;
   13068              :     }
   13069              : 
   13070              :   return false;
   13071              : }
   13072              : 
   13073              : uintptr_t *
   13074       649204 : trees_in::find_duplicate (tree existing)
   13075              : {
   13076       302142 :   if (!duplicates)
   13077              :     return NULL;
   13078              : 
   13079       411494 :   return duplicates->get (existing);
   13080              : }
   13081              : 
   13082              : /* We're starting to read a duplicate DECL.  EXISTING is the already
   13083              :    known node.  */
   13084              : 
   13085              : void
   13086       624496 : trees_in::register_duplicate (tree decl, tree existing)
   13087              : {
   13088       624496 :   if (!duplicates)
   13089       103625 :     duplicates = new duplicate_hash_map (40);
   13090              : 
   13091       624496 :   bool existed;
   13092       624496 :   uintptr_t &slot = duplicates->get_or_insert (existing, &existed);
   13093       624496 :   gcc_checking_assert (!existed);
   13094       624496 :   slot = reinterpret_cast<uintptr_t> (decl);
   13095              : 
   13096       624496 :   if (TREE_CODE (decl) == TEMPLATE_DECL)
   13097              :     /* Also register the DECL_TEMPLATE_RESULT as a duplicate so
   13098              :        that passing decl's _RESULT to maybe_duplicate naturally
   13099              :        gives us existing's _RESULT back.  */
   13100       291158 :     register_duplicate (DECL_TEMPLATE_RESULT (decl),
   13101       145579 :                         DECL_TEMPLATE_RESULT (existing));
   13102       624496 : }
   13103              : 
   13104              : /* We've read a definition of MAYBE_EXISTING.  If not a duplicate,
   13105              :    return MAYBE_EXISTING (into which the definition should be
   13106              :    installed).  Otherwise return NULL if already known bad, or the
   13107              :    duplicate we read (for ODR checking, or extracting additional merge
   13108              :    information).  */
   13109              : 
   13110              : tree
   13111       347062 : trees_in::odr_duplicate (tree maybe_existing, bool has_defn)
   13112              : {
   13113       347062 :   tree res = NULL_TREE;
   13114              : 
   13115       533398 :   if (uintptr_t *dup = find_duplicate (maybe_existing))
   13116              :     {
   13117       147740 :       if (!(*dup & 1))
   13118       147737 :         res = reinterpret_cast<tree> (*dup);
   13119              :     }
   13120              :   else
   13121              :     res = maybe_existing;
   13122              : 
   13123       347062 :   assert_definition (maybe_existing, res && !has_defn);
   13124              : 
   13125              :   // FIXME: We probably need to return the template, so that the
   13126              :   // template header can be checked?
   13127       347062 :   return res ? STRIP_TEMPLATE (res) : NULL_TREE;
   13128              : }
   13129              : 
   13130              : /* The following writer functions rely on the current behaviour of
   13131              :    depset::hash::add_dependency making the decl and defn depset nodes
   13132              :    depend on each other.  That way we don't have to worry about seeding
   13133              :    the tree map with named decls that cannot be looked up by name (I.e
   13134              :    template and function parms).  We know the decl and definition will
   13135              :    be in the same cluster, which is what we want.  */
   13136              : 
   13137              : void
   13138       579963 : trees_out::write_function_def (tree decl)
   13139              : {
   13140       579963 :   tree_node (DECL_RESULT (decl));
   13141              : 
   13142       579963 :   {
   13143              :     /* The function body for a non-inline function or function template
   13144              :        is ignored for determining exposures.  This should only matter
   13145              :        for templates (we don't emit the bodies of non-inline functions
   13146              :        to begin with).  */
   13147       579963 :     auto ovr = dep_hash->ignore_exposure_if (!DECL_DECLARED_INLINE_P (decl));
   13148       579963 :     tree_node (DECL_INITIAL (decl));
   13149       579963 :     tree_node (DECL_SAVED_TREE (decl));
   13150       579963 :   }
   13151              : 
   13152      1237908 :   tree_node (DECL_FRIEND_CONTEXT (decl));
   13153              : 
   13154       579963 :   constexpr_fundef *cexpr = retrieve_constexpr_fundef (decl);
   13155              : 
   13156       579963 :   if (streaming_p ())
   13157       289940 :     u (cexpr != nullptr);
   13158       579963 :   if (cexpr)
   13159              :     {
   13160       134183 :       chained_decls (cexpr->parms);
   13161       134183 :       tree_node (cexpr->result);
   13162       134183 :       tree_node (cexpr->body);
   13163              :     }
   13164              : 
   13165       579963 :   function* f = DECL_STRUCT_FUNCTION (decl);
   13166              : 
   13167       579963 :   if (streaming_p ())
   13168              :     {
   13169       289940 :       unsigned flags = 0;
   13170              : 
   13171              :       /* Whether the importer should emit this definition, if used.  */
   13172       289940 :       flags |= 1 * (DECL_NOT_REALLY_EXTERN (decl)
   13173       289940 :                     && (get_importer_interface (decl)
   13174              :                         != importer_interface::external));
   13175              : 
   13176              :       /* Make sure DECL_REALLY_EXTERN and DECL_INTERFACE_KNOWN are consistent
   13177              :          on non-templates or we'll crash later in import_export_decl.  */
   13178       193125 :       gcc_checking_assert (flags || DECL_INTERFACE_KNOWN (decl)
   13179              :                            || (DECL_LANG_SPECIFIC (decl)
   13180              :                                && DECL_LOCAL_DECL_P (decl)
   13181              :                                && DECL_OMP_DECLARE_REDUCTION_P (decl))
   13182              :                            || (DECL_LANG_SPECIFIC (decl)
   13183              :                                && DECL_TEMPLATE_INFO (decl)
   13184              :                                && uses_template_parms (DECL_TI_ARGS (decl))));
   13185              : 
   13186       289940 :       if (f)
   13187              :         {
   13188       288834 :           flags |= 2;
   13189              :           /* These flags are needed in tsubst_lambda_expr.  */
   13190       288834 :           flags |= 4 * f->language->returns_value;
   13191       288834 :           flags |= 8 * f->language->returns_null;
   13192       288834 :           flags |= 16 * f->language->returns_abnormally;
   13193       288834 :           flags |= 32 * f->language->infinite_loop;
   13194              :         }
   13195              : 
   13196       289940 :       u (flags);
   13197              :     }
   13198              : 
   13199       579963 :   if (state && f)
   13200              :     {
   13201       577751 :       state->write_location (*this, f->function_start_locus);
   13202       577751 :       state->write_location (*this, f->function_end_locus);
   13203              :     }
   13204              : 
   13205       579963 :   if (DECL_COROUTINE_P (decl))
   13206              :     {
   13207           40 :       tree ramp = DECL_RAMP_FN (decl);
   13208           40 :       tree_node (ramp);
   13209           40 :       if (!ramp)
   13210              :         {
   13211           28 :           tree_node (DECL_ACTOR_FN (decl));
   13212           28 :           tree_node (DECL_DESTROY_FN (decl));
   13213              :         }
   13214              :     }
   13215       579963 : }
   13216              : 
   13217              : void
   13218            0 : trees_out::mark_function_def (tree)
   13219              : {
   13220            0 : }
   13221              : 
   13222              : bool
   13223       230376 : trees_in::read_function_def (tree decl, tree maybe_template)
   13224              : {
   13225       230913 :   dump () && dump ("Reading function definition %N", decl);
   13226       230376 :   tree result = tree_node ();
   13227       230376 :   tree initial = tree_node ();
   13228       230376 :   tree saved = tree_node ();
   13229       230376 :   tree context = tree_node ();
   13230       230376 :   post_process_data pdata {};
   13231       230376 :   pdata.decl = maybe_template;
   13232              : 
   13233       230376 :   tree maybe_dup = odr_duplicate (maybe_template, DECL_SAVED_TREE (decl));
   13234       460749 :   bool installing = maybe_dup && !DECL_SAVED_TREE (decl);
   13235              : 
   13236       230376 :   constexpr_fundef cexpr;
   13237       230376 :   if (u ())
   13238              :     {
   13239        52630 :       cexpr.parms = chained_decls ();
   13240        52630 :       cexpr.result = tree_node ();
   13241        52630 :       cexpr.body = tree_node ();
   13242        52630 :       cexpr.decl = decl;
   13243              :     }
   13244              :   else
   13245       177746 :     cexpr.decl = NULL_TREE;
   13246              : 
   13247       230376 :   unsigned flags = u ();
   13248       230376 :   if (flags & 2)
   13249              :     {
   13250       229493 :       pdata.start_locus = state->read_location (*this);
   13251       229493 :       pdata.end_locus = state->read_location (*this);
   13252       229493 :       pdata.returns_value = flags & 4;
   13253       229493 :       pdata.returns_null = flags & 8;
   13254       229493 :       pdata.returns_abnormally = flags & 16;
   13255       229493 :       pdata.infinite_loop = flags & 32;
   13256              :     }
   13257              : 
   13258       230376 :   tree coro_actor = NULL_TREE;
   13259       230376 :   tree coro_destroy = NULL_TREE;
   13260       230376 :   tree coro_ramp = NULL_TREE;
   13261       230376 :   if (DECL_COROUTINE_P (decl))
   13262              :     {
   13263           18 :       coro_ramp = tree_node ();
   13264           18 :       if (!coro_ramp)
   13265              :         {
   13266           12 :           coro_actor = tree_node ();
   13267           12 :           coro_destroy = tree_node ();
   13268           12 :           if ((coro_actor == NULL_TREE) != (coro_destroy == NULL_TREE))
   13269            0 :             set_overrun ();
   13270              :         }
   13271              :     }
   13272              : 
   13273       230376 :   if (get_overrun ())
   13274              :     return NULL_TREE;
   13275              : 
   13276       230376 :   if (installing)
   13277              :     {
   13278       143871 :       DECL_NOT_REALLY_EXTERN (decl) = flags & 1;
   13279       143871 :       DECL_RESULT (decl) = result;
   13280       143871 :       DECL_INITIAL (decl) = initial;
   13281       143871 :       DECL_SAVED_TREE (decl) = saved;
   13282              : 
   13283              :       /* Some entities (like anticipated builtins) were declared without
   13284              :          DECL_ARGUMENTS, so update them now.  But don't do it if there's
   13285              :          already an argument list, because we've already built the
   13286              :          definition referencing those merged PARM_DECLs.  */
   13287       143871 :       if (!DECL_ARGUMENTS (decl))
   13288         6871 :         DECL_ARGUMENTS (decl) = DECL_ARGUMENTS (maybe_dup);
   13289              : 
   13290       143871 :       if (context)
   13291         6208 :         SET_DECL_FRIEND_CONTEXT (decl, context);
   13292       143871 :       if (cexpr.decl)
   13293        38135 :         register_constexpr_fundef (cexpr);
   13294              : 
   13295       143871 :       if (coro_ramp)
   13296            6 :         coro_set_ramp_function (decl, coro_ramp);
   13297       143865 :       else if (coro_actor && coro_destroy)
   13298            3 :         coro_set_transform_functions (decl, coro_actor, coro_destroy);
   13299              : 
   13300       143871 :       if (DECL_LOCAL_DECL_P (decl))
   13301              :         /* Block-scope OMP UDRs aren't real functions, and don't need a
   13302              :            function structure to be allocated or to be expanded.  */
   13303            3 :         gcc_checking_assert (DECL_OMP_DECLARE_REDUCTION_P (decl));
   13304              :       else
   13305       143868 :         post_process (pdata);
   13306              :     }
   13307              :   else if (maybe_dup)
   13308              :     {
   13309              :       // FIXME:QOI Check matching defn
   13310              :     }
   13311              : 
   13312              :   return true;
   13313              : }
   13314              : 
   13315              : /* Also for CONCEPT_DECLs.  */
   13316              : 
   13317              : void
   13318       139402 : trees_out::write_var_def (tree decl)
   13319              : {
   13320              :   /* The initializer of a non-inline variable or variable template is
   13321              :      ignored for determining exposures.  */
   13322       139402 :   auto ovr = dep_hash->ignore_exposure_if (VAR_P (decl)
   13323       150985 :                                            && !DECL_INLINE_VAR_P (decl));
   13324              : 
   13325       139402 :   tree init = DECL_INITIAL (decl);
   13326       139402 :   tree_node (init);
   13327       139402 :   if (!init)
   13328              :     {
   13329         1732 :       tree dyn_init = NULL_TREE;
   13330              : 
   13331              :       /* We only need to write initializers in header modules.  */
   13332         2916 :       if (header_module_p () && DECL_NONTRIVIALLY_INITIALIZED_P (decl))
   13333              :         {
   13334          450 :           dyn_init = value_member (decl,
   13335          450 :                                    CP_DECL_THREAD_LOCAL_P (decl)
   13336              :                                    ? tls_aggregates : static_aggregates);
   13337          450 :           gcc_checking_assert (dyn_init);
   13338              :           /* Mark it so write_inits knows this is needed.  */
   13339          450 :           TREE_LANG_FLAG_0 (dyn_init) = true;
   13340          450 :           dyn_init = TREE_PURPOSE (dyn_init);
   13341              :         }
   13342         1732 :       tree_node (dyn_init);
   13343              :     }
   13344       139402 : }
   13345              : 
   13346              : void
   13347            0 : trees_out::mark_var_def (tree)
   13348              : {
   13349            0 : }
   13350              : 
   13351              : bool
   13352        45200 : trees_in::read_var_def (tree decl, tree maybe_template)
   13353              : {
   13354              :   /* Do not mark the virtual table entries as used.  */
   13355        45200 :   bool vtable = VAR_P (decl) && DECL_VTABLE_OR_VTT_P (decl);
   13356        45200 :   unused += vtable;
   13357        45200 :   tree init = tree_node ();
   13358        45200 :   tree dyn_init = init ? NULL_TREE : tree_node ();
   13359        45200 :   unused -= vtable;
   13360              : 
   13361        45200 :   if (get_overrun ())
   13362              :     return false;
   13363              : 
   13364        45200 :   bool initialized = (VAR_P (decl) ? bool (DECL_INITIALIZED_P (decl))
   13365        45200 :                       : bool (DECL_INITIAL (decl)));
   13366        45200 :   tree maybe_dup = odr_duplicate (maybe_template, initialized);
   13367        45200 :   bool installing = maybe_dup && !initialized;
   13368        45200 :   if (installing)
   13369              :     {
   13370        28272 :       DECL_INITIAL (decl) = init;
   13371        28272 :       if (DECL_EXTERNAL (decl))
   13372         3553 :         DECL_NOT_REALLY_EXTERN (decl) = true;
   13373        28272 :       if (VAR_P (decl))
   13374              :         {
   13375        24687 :           DECL_INITIALIZED_P (decl) = true;
   13376        24687 :           if (maybe_dup && DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (maybe_dup))
   13377        24279 :             DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
   13378        24687 :           tentative_decl_linkage (decl);
   13379        24687 :           if (DECL_EXPLICIT_INSTANTIATION (decl)
   13380        24687 :               && !DECL_EXTERNAL (decl))
   13381            9 :             setup_explicit_instantiation_definition_linkage (decl);
   13382              :           /* Class non-template static members are handled in read_class_def.
   13383              :              But still handle specialisations of member templates.  */
   13384        49374 :           if ((!DECL_CLASS_SCOPE_P (decl)
   13385        16025 :                || primary_template_specialization_p (decl))
   13386        33454 :               && (DECL_IMPLICIT_INSTANTIATION (decl)
   13387         8698 :                   || (DECL_EXPLICIT_INSTANTIATION (decl)
   13388           21 :                       && !DECL_EXTERNAL (decl))))
   13389           78 :             note_vague_linkage_variable (decl);
   13390              :         }
   13391        28272 :       if (!dyn_init)
   13392              :         ;
   13393          216 :       else if (CP_DECL_THREAD_LOCAL_P (decl))
   13394           96 :         tls_aggregates = tree_cons (dyn_init, decl, tls_aggregates);
   13395              :       else
   13396          120 :         static_aggregates = tree_cons (dyn_init, decl, static_aggregates);
   13397              :     }
   13398              :   else if (maybe_dup)
   13399              :     {
   13400              :       // FIXME:QOI Check matching defn
   13401              :     }
   13402              : 
   13403              :   return true;
   13404              : }
   13405              : 
   13406              : /* If MEMBER doesn't have an independent life outside the class,
   13407              :    return it (or its TEMPLATE_DECL).  Otherwise NULL.  */
   13408              : 
   13409              : static tree
   13410       279840 : member_owned_by_class (tree member)
   13411              : {
   13412       279840 :   gcc_assert (DECL_P (member));
   13413              : 
   13414              :   /* Clones are owned by their origin.  */
   13415       279840 :   if (DECL_CLONED_FUNCTION_P (member))
   13416              :     return NULL;
   13417              : 
   13418       279840 :   if (TREE_CODE (member) == FIELD_DECL)
   13419              :     /* FIELD_DECLS can have template info in some cases.  We always
   13420              :        want the FIELD_DECL though, as there's never a TEMPLATE_DECL
   13421              :        wrapping them.  */
   13422              :     return member;
   13423              : 
   13424       120561 :   int use_tpl = -1;
   13425       120561 :   if (tree ti = node_template_info (member, use_tpl))
   13426              :     {
   13427              :       // FIXME: Don't bail on things that CANNOT have their own
   13428              :       // template header.  No, make sure they're in the same cluster.
   13429            0 :       if (use_tpl > 0)
   13430              :         return NULL_TREE;
   13431              : 
   13432            0 :       if (DECL_TEMPLATE_RESULT (TI_TEMPLATE (ti)) == member)
   13433       279840 :         member = TI_TEMPLATE (ti);
   13434              :     }
   13435              :   return member;
   13436              : }
   13437              : 
   13438              : void
   13439       200934 : trees_out::write_class_def (tree defn)
   13440              : {
   13441       200934 :   gcc_assert (DECL_P (defn));
   13442       200934 :   if (streaming_p ())
   13443       100864 :     dump () && dump ("Writing class definition %N", defn);
   13444              : 
   13445       200934 :   tree type = TREE_TYPE (defn);
   13446       200934 :   tree_node (TYPE_SIZE (type));
   13447       200934 :   tree_node (TYPE_SIZE_UNIT (type));
   13448       200934 :   tree_node (TYPE_VFIELD (type));
   13449       200934 :   tree_node (TYPE_BINFO (type));
   13450              : 
   13451       200934 :   vec_chained_decls (TYPE_FIELDS (type));
   13452              : 
   13453              :   /* Every class but __as_base has a type-specific.  */
   13454       398358 :   gcc_checking_assert (!TYPE_LANG_SPECIFIC (type) == IS_FAKE_BASE_TYPE (type));
   13455              : 
   13456       200934 :   if (TYPE_LANG_SPECIFIC (type))
   13457              :     {
   13458       197424 :       {
   13459       197424 :         vec<tree, va_gc> *v = CLASSTYPE_MEMBER_VEC (type);
   13460       197424 :         if (!v)
   13461              :           {
   13462        47194 :             gcc_checking_assert (!streaming_p ());
   13463              :             /* Force a class vector.  */
   13464        47194 :             v = set_class_bindings (type, -1);
   13465        47194 :             gcc_checking_assert (v);
   13466              :           }
   13467              : 
   13468       197424 :         unsigned len = v->length ();
   13469       197424 :         if (streaming_p ())
   13470        98689 :           u (len);
   13471      1663316 :         for (unsigned ix = 0; ix != len; ix++)
   13472              :           {
   13473      1465892 :             tree m = (*v)[ix];
   13474      1465892 :             if (TREE_CODE (m) == TYPE_DECL
   13475       426507 :                 && DECL_ARTIFICIAL (m)
   13476      1680271 :                 && TYPE_STUB_DECL (TREE_TYPE (m)) == m)
   13477              :               /* This is a using-decl for a type, or an anonymous
   13478              :                  struct (maybe with a typedef name).  Write the type.  */
   13479        15718 :               m = TREE_TYPE (m);
   13480      1465892 :             tree_node (m);
   13481              :           }
   13482              :       }
   13483       197424 :       tree_node (CLASSTYPE_LAMBDA_EXPR (type));
   13484              : 
   13485              :       /* TYPE_CONTAINS_VPTR_P looks at the vbase vector, which the
   13486              :          reader won't know at this point.  */
   13487       197424 :       int has_vptr = TYPE_CONTAINS_VPTR_P (type);
   13488              : 
   13489       197424 :       if (streaming_p ())
   13490              :         {
   13491        98689 :           unsigned nvbases = vec_safe_length (CLASSTYPE_VBASECLASSES (type));
   13492        98689 :           u (nvbases);
   13493        98689 :           i (has_vptr);
   13494              :         }
   13495              : 
   13496       197424 :       if (has_vptr)
   13497              :         {
   13498         7124 :           tree_vec (CLASSTYPE_PURE_VIRTUALS (type));
   13499         7124 :           tree_pair_vec (CLASSTYPE_VCALL_INDICES (type));
   13500         7124 :           tree_node (CLASSTYPE_KEY_METHOD (type));
   13501              :         }
   13502              :     }
   13503              : 
   13504       200934 :   if (TYPE_LANG_SPECIFIC (type))
   13505              :     {
   13506       197424 :       tree_node (CLASSTYPE_PRIMARY_BINFO (type));
   13507              : 
   13508       197424 :       tree as_base = CLASSTYPE_AS_BASE (type);
   13509       197424 :       if (as_base)
   13510       102883 :         as_base = TYPE_NAME (as_base);
   13511       197424 :       tree_node (as_base);
   13512              : 
   13513              :       /* Write the vtables.  */
   13514       197424 :       tree vtables = CLASSTYPE_VTABLES (type);
   13515       197424 :       vec_chained_decls (vtables);
   13516       403314 :       for (; vtables; vtables = TREE_CHAIN (vtables))
   13517         8466 :         write_definition (vtables);
   13518              : 
   13519       197424 :       {
   13520              :         /* Friend declarations in class definitions are ignored when
   13521              :            determining exposures.  */
   13522       197424 :         auto ovr = dep_hash->ignore_exposure_if (true);
   13523              : 
   13524              :         /* Write the friend classes.  */
   13525       197424 :         tree_list (CLASSTYPE_FRIEND_CLASSES (type), false);
   13526              : 
   13527              :         /* Write the friend functions.  */
   13528       197424 :         for (tree friends = DECL_FRIENDLIST (defn);
   13529       226124 :              friends; friends = TREE_CHAIN (friends))
   13530              :           {
   13531        28700 :             tree_node (FRIEND_NAME (friends));
   13532        28700 :             tree_list (FRIEND_DECLS (friends), false);
   13533              :           }
   13534              :         /* End of friend fns.  */
   13535       197424 :         tree_node (NULL_TREE);
   13536       197424 :       }
   13537              : 
   13538              :       /* Write the decl list.  We don't need to ignore exposures of friend
   13539              :          decls here as any such decls should already have been added and
   13540              :          ignored above.  */
   13541       197424 :       tree_list (CLASSTYPE_DECL_LIST (type), true);
   13542              : 
   13543       197424 :       if (TYPE_CONTAINS_VPTR_P (type))
   13544              :         {
   13545              :           /* Write the thunks.  */
   13546         7124 :           for (tree decls = TYPE_FIELDS (type);
   13547       201880 :                decls; decls = DECL_CHAIN (decls))
   13548       194756 :             if (TREE_CODE (decls) == FUNCTION_DECL
   13549       140674 :                 && DECL_VIRTUAL_P (decls)
   13550       231614 :                 && DECL_THUNKS (decls))
   13551              :               {
   13552         1308 :                 tree_node (decls);
   13553              :                 /* Thunks are always unique, so chaining is ok.  */
   13554         1308 :                 chained_decls (DECL_THUNKS (decls));
   13555              :               }
   13556         7124 :           tree_node (NULL_TREE);
   13557              :         }
   13558              :     }
   13559       200934 : }
   13560              : 
   13561              : void
   13562       279840 : trees_out::mark_class_member (tree member, bool do_defn)
   13563              : {
   13564       279840 :   gcc_assert (DECL_P (member));
   13565              : 
   13566       279840 :   member = member_owned_by_class (member);
   13567       279840 :   if (member)
   13568       559680 :     mark_declaration (member, do_defn && has_definition (member));
   13569       279840 : }
   13570              : 
   13571              : void
   13572       200962 : trees_out::mark_class_def (tree defn)
   13573              : {
   13574       200962 :   gcc_assert (DECL_P (defn));
   13575       200962 :   tree type = TREE_TYPE (defn);
   13576              :   /* Mark the class members that are not type-decls and cannot have
   13577              :      independent definitions.  */
   13578      2306044 :   for (tree member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
   13579      2105082 :     if (TREE_CODE (member) == FIELD_DECL
   13580      2105082 :         || TREE_CODE (member) == USING_DECL
   13581              :         /* A cloned enum-decl from 'using enum unrelated;'   */
   13582      2105082 :         || (TREE_CODE (member) == CONST_DECL
   13583        16514 :             && DECL_CONTEXT (member) == type))
   13584              :       {
   13585       279840 :         mark_class_member (member);
   13586       279840 :         if (TREE_CODE (member) == FIELD_DECL)
   13587       159279 :           if (tree repr = DECL_BIT_FIELD_REPRESENTATIVE (member))
   13588              :             /* If we're marking a class template definition, then
   13589              :                this'll contain the width (as set by grokbitfield)
   13590              :                instead of a decl.  */
   13591         2990 :             if (DECL_P (repr))
   13592         2334 :               mark_declaration (repr, false);
   13593              :       }
   13594              : 
   13595              :   /* Mark the binfo hierarchy.  */
   13596       473892 :   for (tree child = TYPE_BINFO (type); child; child = TREE_CHAIN (child))
   13597       272930 :     mark_by_value (child);
   13598              : 
   13599       200962 :   if (TYPE_LANG_SPECIFIC (type))
   13600              :     {
   13601       197428 :       for (tree vtable = CLASSTYPE_VTABLES (type);
   13602       205894 :            vtable; vtable = TREE_CHAIN (vtable))
   13603         8466 :         mark_declaration (vtable, true);
   13604              : 
   13605       197428 :       if (TYPE_CONTAINS_VPTR_P (type))
   13606              :         /* Mark the thunks, they belong to the class definition,
   13607              :            /not/ the thunked-to function.  */
   13608         7124 :         for (tree decls = TYPE_FIELDS (type);
   13609       201880 :              decls; decls = DECL_CHAIN (decls))
   13610       194756 :           if (TREE_CODE (decls) == FUNCTION_DECL)
   13611       140674 :             for (tree thunks = DECL_THUNKS (decls);
   13612       142410 :                  thunks; thunks = DECL_CHAIN (thunks))
   13613         1736 :               mark_declaration (thunks, false);
   13614              :     }
   13615       200962 : }
   13616              : 
   13617              : /* Nop sorting, needed for resorting the member vec.  */
   13618              : 
   13619              : static void
   13620     11546540 : nop (void *, void *, void *)
   13621              : {
   13622     11546540 : }
   13623              : 
   13624              : bool
   13625        68337 : trees_in::read_class_def (tree defn, tree maybe_template)
   13626              : {
   13627        68337 :   gcc_assert (DECL_P (defn));
   13628        68970 :   dump () && dump ("Reading class definition %N", defn);
   13629        68337 :   tree type = TREE_TYPE (defn);
   13630        68337 :   tree size = tree_node ();
   13631        68337 :   tree size_unit = tree_node ();
   13632        68337 :   tree vfield = tree_node ();
   13633        68337 :   tree binfo = tree_node ();
   13634        68337 :   vec<tree, va_gc> *vbase_vec = NULL;
   13635        68337 :   vec<tree, va_gc> *member_vec = NULL;
   13636        68337 :   vec<tree, va_gc> *pure_virts = NULL;
   13637        68337 :   vec<tree_pair_s, va_gc> *vcall_indices = NULL;
   13638        68337 :   tree key_method = NULL_TREE;
   13639        68337 :   tree lambda = NULL_TREE;
   13640              : 
   13641              :   /* Read the fields.  */
   13642        68337 :   vec<tree, va_heap> *fields = vec_chained_decls ();
   13643              : 
   13644        68337 :   if (TYPE_LANG_SPECIFIC (type))
   13645              :     {
   13646        66954 :       if (unsigned len = u ())
   13647              :         {
   13648        66954 :           vec_alloc (member_vec, len);
   13649       606620 :           for (unsigned ix = 0; ix != len; ix++)
   13650              :             {
   13651       539666 :               tree m = tree_node ();
   13652       539666 :               if (get_overrun ())
   13653              :                 break;
   13654       539666 :               if (TYPE_P (m))
   13655         5694 :                 m = TYPE_STUB_DECL (m);
   13656       539666 :               member_vec->quick_push (m);
   13657              :             }
   13658              :         }
   13659        66954 :       lambda = tree_node ();
   13660              : 
   13661        66954 :       if (!get_overrun ())
   13662              :         {
   13663        66954 :           unsigned nvbases = u ();
   13664        66954 :           if (nvbases)
   13665              :             {
   13666          285 :               vec_alloc (vbase_vec, nvbases);
   13667         1300 :               for (tree child = binfo; child; child = TREE_CHAIN (child))
   13668         1015 :                 if (BINFO_VIRTUAL_P (child))
   13669          285 :                   vbase_vec->quick_push (child);
   13670              :             }
   13671              :         }
   13672              : 
   13673        66954 :       if (!get_overrun ())
   13674              :         {
   13675        66954 :           int has_vptr = i ();
   13676        66954 :           if (has_vptr)
   13677              :             {
   13678         2693 :               pure_virts = tree_vec ();
   13679         2693 :               vcall_indices = tree_pair_vec ();
   13680         2693 :               key_method = tree_node ();
   13681              :             }
   13682              :         }
   13683              :     }
   13684              : 
   13685        68337 :   tree maybe_dup = odr_duplicate (maybe_template, TYPE_SIZE (type));
   13686        68337 :   bool installing = maybe_dup && !TYPE_SIZE (type);
   13687        38344 :   if (installing)
   13688              :     {
   13689        38344 :       if (maybe_dup != defn)
   13690              :         {
   13691              :           // FIXME: This is needed on other defns too, almost
   13692              :           // duplicate-decl like?  See is_matching_decl too.
   13693              :           /* Copy flags from the duplicate.  */
   13694          360 :           tree type_dup = TREE_TYPE (maybe_dup);
   13695              : 
   13696              :           /* Core pieces.  */
   13697          360 :           TYPE_MODE_RAW (type) = TYPE_MODE_RAW (type_dup);
   13698          360 :           TYPE_ALIGN_RAW (type) = TYPE_ALIGN_RAW (type_dup);
   13699          720 :           TYPE_WARN_IF_NOT_ALIGN_RAW (type)
   13700          360 :             = TYPE_WARN_IF_NOT_ALIGN_RAW (type_dup);
   13701          360 :           TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (type_dup);
   13702              : 
   13703          360 :           SET_DECL_MODE (defn, DECL_MODE (maybe_dup));
   13704          360 :           DECL_SIZE (defn) = DECL_SIZE (maybe_dup);
   13705          360 :           DECL_SIZE_UNIT (defn) = DECL_SIZE_UNIT (maybe_dup);
   13706          360 :           DECL_ALIGN_RAW (defn) = DECL_ALIGN_RAW (maybe_dup);
   13707          720 :           DECL_WARN_IF_NOT_ALIGN_RAW (defn)
   13708          360 :             = DECL_WARN_IF_NOT_ALIGN_RAW (maybe_dup);
   13709          360 :           DECL_USER_ALIGN (defn) = DECL_USER_ALIGN (maybe_dup);
   13710              : 
   13711          360 :           TYPE_TYPELESS_STORAGE (type) = TYPE_TYPELESS_STORAGE (type_dup);
   13712          360 :           TYPE_CXX_ODR_P (type) = TYPE_CXX_ODR_P (type_dup);
   13713          360 :           TYPE_NO_FORCE_BLK (type) = TYPE_NO_FORCE_BLK (type_dup);
   13714          360 :           TYPE_TRANSPARENT_AGGR (type) = TYPE_TRANSPARENT_AGGR (type_dup);
   13715          720 :           TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type)
   13716          360 :             = TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type_dup);
   13717              : 
   13718          360 :           TYPE_EMPTY_P (type) = TYPE_EMPTY_P (type_dup);
   13719          360 :           TREE_ADDRESSABLE (type) = TREE_ADDRESSABLE (type_dup);
   13720              : 
   13721              :           /* C++ pieces.  */
   13722          360 :           TYPE_POLYMORPHIC_P (type) = TYPE_POLYMORPHIC_P (type_dup);
   13723          360 :           CLASSTYPE_FINAL (type) = CLASSTYPE_FINAL (type_dup);
   13724              : 
   13725          720 :           TYPE_HAS_USER_CONSTRUCTOR (type)
   13726          360 :             = TYPE_HAS_USER_CONSTRUCTOR (type_dup);
   13727          720 :           TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
   13728          360 :             = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type_dup);
   13729          720 :           TYPE_NEEDS_CONSTRUCTING (type)
   13730          360 :             = TYPE_NEEDS_CONSTRUCTING (type_dup);
   13731              : 
   13732          360 :           if (auto ls = TYPE_LANG_SPECIFIC (type_dup))
   13733              :             {
   13734          360 :               if (TYPE_LANG_SPECIFIC (type))
   13735              :                 {
   13736         1080 :                   CLASSTYPE_BEFRIENDING_CLASSES (type_dup)
   13737          360 :                     = CLASSTYPE_BEFRIENDING_CLASSES (type);
   13738          720 :                   SET_CLASSTYPE_TYPEINFO_VAR (type_dup,
   13739              :                                               CLASSTYPE_TYPEINFO_VAR (type));
   13740              :                 }
   13741         1570 :               for (tree v = type; v; v = TYPE_NEXT_VARIANT (v))
   13742         1210 :                 TYPE_LANG_SPECIFIC (v) = ls;
   13743              :             }
   13744              :         }
   13745              : 
   13746        38344 :       TYPE_SIZE (type) = size;
   13747        38344 :       TYPE_SIZE_UNIT (type) = size_unit;
   13748              : 
   13749        38344 :       if (fields)
   13750              :         {
   13751        38344 :           tree *chain = &TYPE_FIELDS (type);
   13752        38344 :           unsigned len = fields->length ();
   13753       502686 :           for (unsigned ix = 0; ix != len; ix++)
   13754              :             {
   13755       464342 :               tree decl = (*fields)[ix];
   13756              : 
   13757       464342 :               if (!decl)
   13758              :                 {
   13759              :                   /* An anonymous struct with typedef name.  */
   13760            3 :                   tree tdef = (*fields)[ix+1];
   13761            3 :                   decl = TYPE_STUB_DECL (TREE_TYPE (tdef));
   13762            3 :                   gcc_checking_assert (IDENTIFIER_ANON_P (DECL_NAME (decl))
   13763              :                                        && decl != tdef);
   13764              :                 }
   13765              : 
   13766       838427 :               gcc_checking_assert (!*chain == !DECL_CLONED_FUNCTION_P (decl));
   13767       464342 :               *chain = decl;
   13768       464342 :               chain = &DECL_CHAIN (decl);
   13769              : 
   13770       464342 :               if (TREE_CODE (decl) == FIELD_DECL
   13771       464342 :                   && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
   13772              :                 {
   13773          299 :                   tree anon_type = TYPE_MAIN_VARIANT (TREE_TYPE (decl));
   13774          299 :                   if (DECL_NAME (defn) == as_base_identifier)
   13775              :                     /* ANON_AGGR_TYPE_FIELD should already point to the
   13776              :                        original FIELD_DECL; don't overwrite it to point
   13777              :                        to the as-base FIELD_DECL copy.  */
   13778           26 :                     gcc_checking_assert (ANON_AGGR_TYPE_FIELD (anon_type));
   13779              :                   else
   13780          286 :                     SET_ANON_AGGR_TYPE_FIELD (anon_type, decl);
   13781              :                 }
   13782              : 
   13783       464342 :               if (TREE_CODE (decl) == USING_DECL
   13784       464342 :                   && TREE_CODE (USING_DECL_SCOPE (decl)) == RECORD_TYPE)
   13785              :                 {
   13786              :                   /* Reconstruct DECL_ACCESS.  */
   13787        19039 :                   tree decls = USING_DECL_DECLS (decl);
   13788        19039 :                   tree access = declared_access (decl);
   13789              : 
   13790        22513 :                   for (ovl_iterator iter (decls); iter; ++iter)
   13791              :                     {
   13792         2263 :                       tree d = *iter;
   13793              : 
   13794         2263 :                       retrofit_lang_decl (d);
   13795         2263 :                       tree list = DECL_ACCESS (d);
   13796              : 
   13797         2263 :                       if (!purpose_member (type, list))
   13798         2728 :                         DECL_ACCESS (d) = tree_cons (type, access, list);
   13799              :                     }
   13800              :                 }
   13801              : 
   13802       464342 :               if (TREE_CODE (decl) == VAR_DECL
   13803        13441 :                   && TREE_CODE (maybe_template) != TEMPLATE_DECL)
   13804        11576 :                 note_vague_linkage_variable (decl);
   13805              :             }
   13806              :         }
   13807              : 
   13808        38344 :       TYPE_VFIELD (type) = vfield;
   13809        38344 :       TYPE_BINFO (type) = binfo;
   13810              : 
   13811        38344 :       if (TYPE_LANG_SPECIFIC (type))
   13812              :         {
   13813        37475 :           if (!TYPE_POLYMORPHIC_P (type))
   13814        35884 :             SET_CLASSTYPE_LAMBDA_EXPR (type, lambda);
   13815              :           else
   13816         1591 :             gcc_checking_assert (lambda == NULL_TREE);
   13817              : 
   13818        37475 :           CLASSTYPE_MEMBER_VEC (type) = member_vec;
   13819        37475 :           CLASSTYPE_PURE_VIRTUALS (type) = pure_virts;
   13820        37475 :           CLASSTYPE_VCALL_INDICES (type) = vcall_indices;
   13821              : 
   13822        37475 :           if (TYPE_POLYMORPHIC_P (type))
   13823         1591 :             SET_CLASSTYPE_KEY_METHOD (type, key_method);
   13824              :           else
   13825        35884 :             gcc_checking_assert (key_method == NULL_TREE);
   13826              : 
   13827        37475 :           CLASSTYPE_VBASECLASSES (type) = vbase_vec;
   13828              : 
   13829              :           /* Resort the member vector.  */
   13830        37475 :           resort_type_member_vec (member_vec, NULL, nop, NULL);
   13831              :         }
   13832              :     }
   13833              :   else if (maybe_dup)
   13834              :     {
   13835              :       // FIXME:QOI Check matching defn
   13836              :     }
   13837              : 
   13838        68337 :   if (TYPE_LANG_SPECIFIC (type))
   13839              :     {
   13840        66954 :       tree primary = tree_node ();
   13841        66954 :       tree as_base = tree_node ();
   13842              : 
   13843        66954 :       if (as_base)
   13844        34542 :         as_base = TREE_TYPE (as_base);
   13845              : 
   13846              :       /* Read the vtables.  */
   13847        66954 :       vec<tree, va_heap> *vtables = vec_chained_decls ();
   13848        66954 :       if (vtables)
   13849              :         {
   13850         2651 :           unsigned len = vtables->length ();
   13851         5783 :           for (unsigned ix = 0; ix != len; ix++)
   13852              :             {
   13853         3132 :               tree vtable = (*vtables)[ix];
   13854         3132 :               read_var_def (vtable, vtable);
   13855              :             }
   13856              :         }
   13857              : 
   13858        66954 :       tree friend_classes = tree_list (false);
   13859        66954 :       tree friend_functions = NULL_TREE;
   13860        66954 :       for (tree *chain = &friend_functions;
   13861        79331 :            tree name = tree_node (); chain = &TREE_CHAIN (*chain))
   13862              :         {
   13863        12377 :           tree val = tree_list (false);
   13864        12377 :           *chain = build_tree_list (name, val);
   13865        12377 :         }
   13866        66954 :       tree decl_list = tree_list (true);
   13867              : 
   13868        66954 :       if (installing)
   13869              :         {
   13870        37475 :           CLASSTYPE_PRIMARY_BINFO (type) = primary;
   13871        37475 :           CLASSTYPE_AS_BASE (type) = as_base;
   13872              : 
   13873        37475 :           if (vtables)
   13874              :             {
   13875         1603 :               if ((!CLASSTYPE_KEY_METHOD (type)
   13876              :                    /* Sneaky user may have defined it inline
   13877              :                       out-of-class.  */
   13878         1152 :                    || DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (type)))
   13879              :                   /* An imported non-template class attached to a module
   13880              :                      doesn't need to have its vtables emitted here.  */
   13881         1759 :                   && (CLASSTYPE_USE_TEMPLATE (type)
   13882          295 :                       || !DECL_MODULE_ATTACH_P (defn)))
   13883         1135 :                 vec_safe_push (keyed_classes, type);
   13884         1603 :               unsigned len = vtables->length ();
   13885         1603 :               tree *chain = &CLASSTYPE_VTABLES (type);
   13886         3504 :               for (unsigned ix = 0; ix != len; ix++)
   13887              :                 {
   13888         1901 :                   tree vtable = (*vtables)[ix];
   13889         1901 :                   gcc_checking_assert (!*chain);
   13890         1901 :                   *chain = vtable;
   13891         1901 :                   chain = &DECL_CHAIN (vtable);
   13892              :                 }
   13893              :             }
   13894        37475 :           CLASSTYPE_FRIEND_CLASSES (type) = friend_classes;
   13895        37475 :           DECL_FRIENDLIST (defn) = friend_functions;
   13896        37475 :           CLASSTYPE_DECL_LIST (type) = decl_list;
   13897              : 
   13898        40575 :           for (; friend_classes; friend_classes = TREE_CHAIN (friend_classes))
   13899              :             {
   13900         3100 :               tree f = TREE_VALUE (friend_classes);
   13901         3100 :               if (TREE_CODE (f) == TEMPLATE_DECL)
   13902         1282 :                 f = TREE_TYPE (f);
   13903              : 
   13904         3100 :               if (CLASS_TYPE_P (f))
   13905              :                 {
   13906         3062 :                   CLASSTYPE_BEFRIENDING_CLASSES (f)
   13907         6124 :                     = tree_cons (NULL_TREE, type,
   13908         3062 :                                  CLASSTYPE_BEFRIENDING_CLASSES (f));
   13909         3106 :                   dump () && dump ("Class %N befriending %C:%N",
   13910            6 :                                    type, TREE_CODE (f), f);
   13911              :                 }
   13912              :             }
   13913              : 
   13914        44985 :           for (; friend_functions;
   13915         7510 :                friend_functions = TREE_CHAIN (friend_functions))
   13916         7510 :             for (tree friend_decls = TREE_VALUE (friend_functions);
   13917        17066 :                  friend_decls; friend_decls = TREE_CHAIN (friend_decls))
   13918              :               {
   13919         9556 :                 tree f = TREE_VALUE (friend_decls);
   13920         9556 :                 if (TREE_CODE (f) == TU_LOCAL_ENTITY)
   13921           36 :                   continue;
   13922              : 
   13923         9520 :                 DECL_BEFRIENDING_CLASSES (f)
   13924         9520 :                   = tree_cons (NULL_TREE, type, DECL_BEFRIENDING_CLASSES (f));
   13925         9586 :                 dump () && dump ("Class %N befriending %C:%N",
   13926           30 :                                  type, TREE_CODE (f), f);
   13927              :               }
   13928              :         }
   13929              : 
   13930        66954 :       if (TYPE_CONTAINS_VPTR_P (type))
   13931              :         /* Read and install the thunks.  */
   13932         3131 :         while (tree vfunc = tree_node ())
   13933              :           {
   13934          438 :             tree thunks = chained_decls ();
   13935          438 :             if (installing)
   13936          270 :               SET_DECL_THUNKS (vfunc, thunks);
   13937              :           }
   13938              : 
   13939        66954 :       vec_free (vtables);
   13940              :     }
   13941              : 
   13942              :   /* Propagate to all variants.  */
   13943        68337 :   if (installing)
   13944        38344 :     fixup_type_variants (type);
   13945              : 
   13946              :   /* IS_FAKE_BASE_TYPE is inaccurate at this point, because if this is
   13947              :      the fake base, we've not hooked it into the containing class's
   13948              :      data structure yet.  Fortunately it has a unique name.  */
   13949        38344 :   if (installing
   13950        38344 :       && DECL_NAME (defn) != as_base_identifier
   13951        37475 :       && (!CLASSTYPE_TEMPLATE_INFO (type)
   13952        31610 :           || !uses_template_parms (TI_ARGS (CLASSTYPE_TEMPLATE_INFO (type)))))
   13953              :     /* Emit debug info.  It'd be nice to know if the interface TU
   13954              :        already emitted this.  */
   13955        20853 :     rest_of_type_compilation (type, !LOCAL_CLASS_P (type));
   13956              : 
   13957        68337 :   vec_free (fields);
   13958              : 
   13959        68337 :   return !get_overrun ();
   13960              : }
   13961              : 
   13962              : void
   13963         9368 : trees_out::write_enum_def (tree decl)
   13964              : {
   13965         9368 :   tree type = TREE_TYPE (decl);
   13966              : 
   13967         9368 :   tree_node (TYPE_VALUES (type));
   13968              :   /* Note that we stream TYPE_MIN/MAX_VALUE directly as part of the
   13969              :      ENUMERAL_TYPE.  */
   13970         9368 : }
   13971              : 
   13972              : void
   13973         9368 : trees_out::mark_enum_def (tree decl)
   13974              : {
   13975         9368 :   tree type = TREE_TYPE (decl);
   13976              : 
   13977        48756 :   for (tree values = TYPE_VALUES (type); values; values = TREE_CHAIN (values))
   13978              :     {
   13979        39388 :       tree cst = TREE_VALUE (values);
   13980        39388 :       mark_by_value (cst);
   13981              :       /* We must mark the init to avoid circularity in tt_enum_int.  */
   13982        39388 :       if (tree init = DECL_INITIAL (cst))
   13983        38952 :         if (TREE_CODE (init) == INTEGER_CST)
   13984        38220 :           mark_by_value (init);
   13985              :     }
   13986         9368 : }
   13987              : 
   13988              : bool
   13989         3149 : trees_in::read_enum_def (tree defn, tree maybe_template)
   13990              : {
   13991         3149 :   tree type = TREE_TYPE (defn);
   13992         3149 :   tree values = tree_node ();
   13993              : 
   13994         3149 :   if (get_overrun ())
   13995              :     return false;
   13996              : 
   13997         3149 :   tree maybe_dup = odr_duplicate (maybe_template, TYPE_VALUES (type));
   13998         6298 :   bool installing = maybe_dup && !TYPE_VALUES (type);
   13999              : 
   14000         3149 :   if (installing)
   14001              :     {
   14002         1622 :       TYPE_VALUES (type) = values;
   14003              :       /* Note that we stream TYPE_MIN/MAX_VALUE directly as part of the
   14004              :          ENUMERAL_TYPE.  */
   14005              : 
   14006         2629 :       rest_of_type_compilation (type, DECL_NAMESPACE_SCOPE_P (defn));
   14007              :     }
   14008         1527 :   else if (maybe_dup)
   14009              :     {
   14010         1527 :       tree known = TYPE_VALUES (type);
   14011         8488 :       for (; known && values;
   14012         6961 :            known = TREE_CHAIN (known), values = TREE_CHAIN (values))
   14013              :         {
   14014         6970 :           tree known_decl = TREE_VALUE (known);
   14015         6970 :           tree new_decl = TREE_VALUE (values);
   14016              : 
   14017         6970 :           if (DECL_NAME (known_decl) != DECL_NAME (new_decl))
   14018              :             break;
   14019              : 
   14020         6964 :           new_decl = maybe_duplicate (new_decl);
   14021              : 
   14022         6964 :           if (!cp_tree_equal (DECL_INITIAL (known_decl),
   14023         6964 :                               DECL_INITIAL (new_decl)))
   14024              :             break;
   14025              :         }
   14026              : 
   14027         1527 :       if (known || values)
   14028              :         {
   14029           12 :           auto_diagnostic_group d;
   14030           12 :           error_at (DECL_SOURCE_LOCATION (maybe_dup),
   14031              :                     "definition of %qD does not match", maybe_dup);
   14032           12 :           inform (DECL_SOURCE_LOCATION (defn),
   14033              :                   "existing definition %qD", defn);
   14034              : 
   14035           12 :           tree known_decl = NULL_TREE, new_decl = NULL_TREE;
   14036              : 
   14037           12 :           if (known)
   14038            9 :             known_decl = TREE_VALUE (known);
   14039           12 :           if (values)
   14040           12 :             new_decl = maybe_duplicate (TREE_VALUE (values));
   14041              : 
   14042           12 :           if (known_decl && new_decl)
   14043              :             {
   14044            9 :               inform (DECL_SOURCE_LOCATION (new_decl),
   14045              :                       "enumerator %qD does not match ...", new_decl);
   14046            9 :               inform (DECL_SOURCE_LOCATION (known_decl),
   14047              :                       "... this enumerator %qD", known_decl);
   14048              :             }
   14049            3 :           else if (known_decl || new_decl)
   14050              :             {
   14051            3 :               tree extra = known_decl ? known_decl : new_decl;
   14052            3 :               inform (DECL_SOURCE_LOCATION (extra),
   14053              :                       "additional enumerators beginning with %qD", extra);
   14054              :             }
   14055              :           else
   14056            0 :             inform (DECL_SOURCE_LOCATION (maybe_dup),
   14057              :                     "enumeration range differs");
   14058              : 
   14059              :           /* Mark it bad.  */
   14060           12 :           unmatched_duplicate (maybe_template);
   14061           12 :         }
   14062              :     }
   14063              : 
   14064              :   return true;
   14065              : }
   14066              : 
   14067              : /* Write out the body of DECL.  See above circularity note.  */
   14068              : 
   14069              : void
   14070       929667 : trees_out::write_definition (tree decl, bool refs_tu_local)
   14071              : {
   14072       929667 :   auto ovr = make_temp_override (writing_local_entities,
   14073       929667 :                                  writing_local_entities || refs_tu_local);
   14074              : 
   14075       929667 :   if (streaming_p ())
   14076              :     {
   14077       464743 :       assert_definition (decl);
   14078       464743 :       dump ()
   14079          952 :         && dump ("Writing definition %C:%N", TREE_CODE (decl), decl);
   14080              :     }
   14081              :   else
   14082       464924 :     dump (dumper::DEPEND)
   14083           96 :       && dump ("Depending definition %C:%N", TREE_CODE (decl), decl);
   14084              : 
   14085      1452206 :  again:
   14086      1452206 :   switch (TREE_CODE (decl))
   14087              :     {
   14088            0 :     default:
   14089            0 :       gcc_unreachable ();
   14090              : 
   14091       522539 :     case TEMPLATE_DECL:
   14092       522539 :       decl = DECL_TEMPLATE_RESULT (decl);
   14093       522539 :       goto again;
   14094              : 
   14095       579963 :     case FUNCTION_DECL:
   14096       579963 :       write_function_def (decl);
   14097       579963 :       break;
   14098              : 
   14099       210302 :     case TYPE_DECL:
   14100       210302 :       {
   14101       210302 :         tree type = TREE_TYPE (decl);
   14102       210302 :         gcc_assert (TYPE_MAIN_VARIANT (type) == type
   14103              :                     && TYPE_NAME (type) == decl);
   14104       210302 :         if (TREE_CODE (type) == ENUMERAL_TYPE)
   14105         9368 :           write_enum_def (decl);
   14106              :         else
   14107       200934 :           write_class_def (decl);
   14108              :       }
   14109              :       break;
   14110              : 
   14111       139402 :     case VAR_DECL:
   14112       139402 :     case CONCEPT_DECL:
   14113       139402 :       write_var_def (decl);
   14114       139402 :       break;
   14115              :     }
   14116       929667 : }
   14117              : 
   14118              : /* Mark a declaration for by-value walking.  If DO_DEFN is true, mark
   14119              :    its body too.  */
   14120              : 
   14121              : void
   14122      3904337 : trees_out::mark_declaration (tree decl, bool do_defn)
   14123              : {
   14124      3904337 :   mark_by_value (decl);
   14125              : 
   14126      3904337 :   if (TREE_CODE (decl) == TEMPLATE_DECL)
   14127      1289445 :     decl = DECL_TEMPLATE_RESULT (decl);
   14128              : 
   14129      3904337 :   if (!do_defn)
   14130              :     return;
   14131              : 
   14132       929689 :   switch (TREE_CODE (decl))
   14133              :     {
   14134            0 :     default:
   14135            0 :       gcc_unreachable ();
   14136              : 
   14137              :     case FUNCTION_DECL:
   14138              :       mark_function_def (decl);
   14139              :       break;
   14140              : 
   14141       210330 :     case TYPE_DECL:
   14142       210330 :       {
   14143       210330 :         tree type = TREE_TYPE (decl);
   14144       210330 :         gcc_assert (TYPE_MAIN_VARIANT (type) == type
   14145              :                     && TYPE_NAME (type) == decl);
   14146       210330 :         if (TREE_CODE (type) == ENUMERAL_TYPE)
   14147         9368 :           mark_enum_def (decl);
   14148              :         else
   14149       200962 :           mark_class_def (decl);
   14150              :       }
   14151              :       break;
   14152              : 
   14153              :     case VAR_DECL:
   14154              :     case CONCEPT_DECL:
   14155              :       mark_var_def (decl);
   14156              :       break;
   14157              :     }
   14158              : }
   14159              : 
   14160              : /* Read in the body of DECL.  See above circularity note.  */
   14161              : 
   14162              : bool
   14163       343930 : trees_in::read_definition (tree decl)
   14164              : {
   14165       345235 :   dump () && dump ("Reading definition %C %N", TREE_CODE (decl), decl);
   14166              : 
   14167              :   tree maybe_template = decl;
   14168              : 
   14169       343930 :  again:
   14170       549152 :   switch (TREE_CODE (decl))
   14171              :     {
   14172              :     default:
   14173              :       break;
   14174              : 
   14175       205222 :     case TEMPLATE_DECL:
   14176       205222 :       decl = DECL_TEMPLATE_RESULT (decl);
   14177       205222 :       goto again;
   14178              : 
   14179       230376 :     case FUNCTION_DECL:
   14180       230376 :       return read_function_def (decl, maybe_template);
   14181              : 
   14182        71486 :     case TYPE_DECL:
   14183        71486 :       {
   14184        71486 :         tree type = TREE_TYPE (decl);
   14185        71486 :         gcc_assert (TYPE_MAIN_VARIANT (type) == type
   14186              :                     && TYPE_NAME (type) == decl);
   14187        71486 :         if (TREE_CODE (type) == ENUMERAL_TYPE)
   14188         3149 :           return read_enum_def (decl, maybe_template);
   14189              :         else
   14190        68337 :           return read_class_def (decl, maybe_template);
   14191              :       }
   14192        42068 :       break;
   14193              : 
   14194        42068 :     case VAR_DECL:
   14195        42068 :     case CONCEPT_DECL:
   14196        42068 :       return read_var_def (decl, maybe_template);
   14197              :     }
   14198              : 
   14199              :   return false;
   14200              : }
   14201              : 
   14202              : /* Lookup an maybe insert a slot for depset for KEY.  */
   14203              : 
   14204              : depset **
   14205     19403581 : depset::hash::entity_slot (tree entity, bool insert)
   14206              : {
   14207     19403581 :   traits::compare_type key (entity, NULL);
   14208     29275032 :   depset **slot = find_slot_with_hash (key, traits::hash (key),
   14209              :                                        insert ? INSERT : NO_INSERT);
   14210              : 
   14211     19403581 :   return slot;
   14212              : }
   14213              : 
   14214              : depset **
   14215       225106 : depset::hash::binding_slot (tree ctx, tree name, bool insert)
   14216              : {
   14217       225106 :   traits::compare_type key (ctx, name);
   14218       292509 :   depset **slot = find_slot_with_hash (key, traits::hash (key),
   14219              :                                        insert ? INSERT : NO_INSERT);
   14220              : 
   14221       225106 :   return slot;
   14222              : }
   14223              : 
   14224              : depset *
   14225      9413827 : depset::hash::find_dependency (tree decl)
   14226              : {
   14227      9413827 :   depset **slot = entity_slot (decl, false);
   14228              : 
   14229      9413827 :   return slot ? *slot : NULL;
   14230              : }
   14231              : 
   14232              : depset *
   14233        67403 : depset::hash::find_binding (tree ctx, tree name)
   14234              : {
   14235        67403 :   depset **slot = binding_slot (ctx, name, false);
   14236              : 
   14237        67403 :   return slot ? *slot : NULL;
   14238              : }
   14239              : 
   14240              : static bool is_tu_local_entity (tree decl, bool explain = false);
   14241              : static bool is_tu_local_value (tree decl, tree expr, bool explain = false);
   14242              : static bool has_tu_local_tmpl_arg (tree decl, tree args, bool explain);
   14243              : 
   14244              : /* Returns true if DECL is a TU-local entity, as defined by [basic.link].
   14245              :    If EXPLAIN is true, emit an informative note about why DECL is TU-local.  */
   14246              : 
   14247              : static bool
   14248      4798825 : is_tu_local_entity (tree decl, bool explain/*=false*/)
   14249              : {
   14250      4798825 :   gcc_checking_assert (DECL_P (decl));
   14251      4798825 :   location_t loc = DECL_SOURCE_LOCATION (decl);
   14252      4798825 :   tree type = TREE_TYPE (decl);
   14253              : 
   14254              :   /* Only types, functions, variables, and template (specialisations)
   14255              :      can be TU-local.  */
   14256      4798825 :   if (TREE_CODE (decl) != TYPE_DECL
   14257              :       && TREE_CODE (decl) != FUNCTION_DECL
   14258              :       && TREE_CODE (decl) != VAR_DECL
   14259              :       && TREE_CODE (decl) != TEMPLATE_DECL)
   14260              :     return false;
   14261              : 
   14262              :   /* An explicit type alias is not an entity; we don't want to stream
   14263              :      such aliases if they refer to TU-local entities, so propagate this
   14264              :      from the original type. The built-in declarations of 'int' and such
   14265              :      are never TU-local.  */
   14266      4795125 :   if (TREE_CODE (decl) == TYPE_DECL
   14267      1622343 :       && !DECL_SELF_REFERENCE_P (decl)
   14268      6351586 :       && !DECL_IMPLICIT_TYPEDEF_P (decl))
   14269              :     {
   14270       837785 :       tree orig = DECL_ORIGINAL_TYPE (decl);
   14271       837785 :       if (orig && TYPE_NAME (orig))
   14272              :         {
   14273       174802 :           if (explain)
   14274           11 :             inform (loc, "%qD is an alias of TU-local type %qT", decl, orig);
   14275       174802 :           return is_tu_local_entity (TYPE_NAME (orig), explain);
   14276              :         }
   14277              :       else
   14278              :         return false;
   14279              :     }
   14280              : 
   14281              :   /* Check specializations first for slightly better explanations.  */
   14282      3957340 :   int use_tpl = -1;
   14283      3957340 :   tree ti = node_template_info (decl, use_tpl);
   14284      4851001 :   if (use_tpl > 0 && TREE_CODE (TI_TEMPLATE (ti)) == TEMPLATE_DECL)
   14285              :     {
   14286              :       /* A specialization of a TU-local template.  */
   14287       893502 :       tree tmpl = TI_TEMPLATE (ti);
   14288       893502 :       if (is_tu_local_entity (tmpl))
   14289              :         {
   14290           72 :           if (explain)
   14291              :             {
   14292           18 :               inform (loc, "%qD is a specialization of TU-local template %qD",
   14293              :                       decl, tmpl);
   14294           18 :               is_tu_local_entity (tmpl, /*explain=*/true);
   14295              :             }
   14296           72 :           return true;
   14297              :         }
   14298              : 
   14299              :       /* A specialization of a template with any TU-local template argument.  */
   14300       893430 :       if (has_tu_local_tmpl_arg (decl, TI_ARGS (ti), explain))
   14301              :         return true;
   14302              : 
   14303              :       /* FIXME A specialization of a template whose (possibly instantiated)
   14304              :          declaration is an exposure.  This should always be covered by the
   14305              :          above cases??  */
   14306              :     }
   14307              : 
   14308              :   /* A type, function, variable, or template with internal linkage.  */
   14309      3957236 :   linkage_kind kind = decl_linkage (decl);
   14310      3957236 :   if (kind == lk_internal
   14311              :       /* But although weakrefs are marked static, don't consider them
   14312              :          to be TU-local.  */
   14313      3957236 :       && !lookup_attribute ("weakref", DECL_ATTRIBUTES (decl)))
   14314              :     {
   14315          846 :       if (explain)
   14316          168 :         inform (loc, "%qD declared with internal linkage", decl);
   14317          846 :       return true;
   14318              :     }
   14319              : 
   14320              :   /* Does not have a name with linkage and is declared, or introduced by a
   14321              :      lambda-expression, within the definition of a TU-local entity.  */
   14322      3956390 :   if (kind == lk_none)
   14323              :     {
   14324       399362 :       tree ctx = CP_DECL_CONTEXT (decl);
   14325       509283 :       if (LAMBDA_TYPE_P (type))
   14326        78114 :         if (tree extra = LAMBDA_TYPE_EXTRA_SCOPE (type))
   14327       399362 :           ctx = extra;
   14328              : 
   14329       399362 :       if (TREE_CODE (ctx) == NAMESPACE_DECL)
   14330              :         {
   14331           31 :           if (!TREE_PUBLIC (ctx))
   14332              :             {
   14333            0 :               if (explain)
   14334            0 :                 inform (loc, "%qD has no linkage and is declared in an "
   14335              :                         "anonymous namespace", decl);
   14336            0 :               return true;
   14337              :             }
   14338              :         }
   14339       399331 :       else if (TYPE_P (ctx))
   14340              :         {
   14341        46317 :           tree ctx_decl = TYPE_MAIN_DECL (ctx);
   14342        46317 :           if (is_tu_local_entity (ctx_decl))
   14343              :             {
   14344            6 :               if (explain)
   14345              :                 {
   14346            0 :                   inform (loc, "%qD has no linkage and is declared within "
   14347              :                           "TU-local entity %qT", decl, ctx);
   14348            0 :                   is_tu_local_entity (ctx_decl, /*explain=*/true);
   14349              :                 }
   14350            6 :               return true;
   14351              :             }
   14352              :         }
   14353       353014 :       else if (is_tu_local_entity (ctx))
   14354              :         {
   14355           33 :           if (explain)
   14356              :             {
   14357            6 :               inform (loc, "%qD has no linkage and is declared within "
   14358              :                       "TU-local entity %qD", decl, ctx);
   14359            6 :               is_tu_local_entity (ctx, /*explain=*/true);
   14360              :             }
   14361           33 :           return true;
   14362              :         }
   14363              :     }
   14364              : 
   14365              :   /* A type with no name that is defined outside a class-specifier, function
   14366              :      body, or initializer; or is introduced by a defining-type-specifier that
   14367              :      is used to declare only TU-local entities.
   14368              : 
   14369              :      We consider types with names for linkage purposes as having names, since
   14370              :      these aren't really TU-local.  */
   14371      3956351 :   tree inner = STRIP_TEMPLATE (decl);
   14372      1685463 :   if (inner
   14373      3956351 :       && TREE_CODE (inner) == TYPE_DECL
   14374      2985219 :       && TYPE_ANON_P (type)
   14375        42517 :       && !DECL_SELF_REFERENCE_P (inner)
   14376              :       /* An enum with an enumerator name for linkage.  */
   14377      1724020 :       && !(UNSCOPED_ENUM_P (type) && TYPE_VALUES (type)))
   14378              :     {
   14379        36487 :       tree main_decl = TYPE_MAIN_DECL (type);
   14380        72464 :       if (LAMBDA_TYPE_P (type))
   14381              :         {
   14382              :           /* A lambda expression is, in practice, TU-local iff it has no
   14383              :              mangling scope.  This currently doesn't line up exactly with
   14384              :              the standard's definition due to some ABI issues, but it's
   14385              :              pretty close, and avoids other issues down the line.  */
   14386        71828 :           if (!LAMBDA_TYPE_EXTRA_SCOPE (type))
   14387              :             {
   14388            4 :               if (explain)
   14389            2 :                 inform (loc, "%qT has no name and cannot be differentiated "
   14390              :                         "from similar lambdas in other TUs", type);
   14391            4 :               return true;
   14392              :             }
   14393              :         }
   14394         1146 :       else if (!DECL_CLASS_SCOPE_P (main_decl)
   14395          603 :                && !decl_function_context (main_decl))
   14396              :         {
   14397           30 :           if (explain)
   14398           12 :             inform (loc, "%qT has no name and is not defined within a class, "
   14399              :                     "function, or initializer", type);
   14400           30 :           return true;
   14401              :         }
   14402              : 
   14403              :       // FIXME introduced by a defining-type-specifier only declaring TU-local
   14404              :       // entities; does this refer to e.g. 'static struct {} a;"?  I can't
   14405              :       // think of any cases where this isn't covered by earlier cases.  */
   14406              :     }
   14407              : 
   14408              :   return false;
   14409              : }
   14410              : 
   14411              : /* Helper for is_tu_local_entity.  Returns true if one of the ARGS of
   14412              :    DECL is TU-local.  Emits an explanation if EXPLAIN is true.  */
   14413              : 
   14414              : static bool
   14415      1016214 : has_tu_local_tmpl_arg (tree decl, tree args, bool explain)
   14416              : {
   14417      1016214 :   if (!args || TREE_CODE (args) != TREE_VEC)
   14418              :     return false;
   14419              : 
   14420      2747433 :   for (tree a : tree_vec_range (args))
   14421              :     {
   14422      1731251 :       if (TREE_CODE (a) == TREE_VEC)
   14423              :         {
   14424       122784 :           if (has_tu_local_tmpl_arg (decl, a, explain))
   14425           32 :             return true;
   14426              :         }
   14427              :       else if (!WILDCARD_TYPE_P (a))
   14428              :         {
   14429      1462507 :           if (DECL_P (a) && is_tu_local_entity (a))
   14430              :             {
   14431            0 :               if (explain)
   14432              :                 {
   14433            0 :                   inform (DECL_SOURCE_LOCATION (decl),
   14434              :                           "%qD has TU-local template argument %qD",
   14435              :                           decl, a);
   14436            0 :                   is_tu_local_entity (a, /*explain=*/true);
   14437              :                 }
   14438            0 :               return true;
   14439              :             }
   14440              : 
   14441      1462507 :           if (TYPE_P (a) && TYPE_NAME (a) && is_tu_local_entity (TYPE_NAME (a)))
   14442              :             {
   14443           17 :               if (explain)
   14444              :                 {
   14445            1 :                   inform (DECL_SOURCE_LOCATION (decl),
   14446              :                           "%qD has TU-local template argument %qT",
   14447              :                           decl, a);
   14448            1 :                   is_tu_local_entity (TYPE_NAME (a), /*explain=*/true);
   14449              :                 }
   14450           17 :               return true;
   14451              :             }
   14452              : 
   14453      1462490 :           if (EXPR_P (a) && is_tu_local_value (decl, a, explain))
   14454              :             return true;
   14455              :         }
   14456              :     }
   14457              : 
   14458      1016182 :   return false;
   14459              : }
   14460              : 
   14461              : /* Returns true if EXPR (part of the initializer for DECL) is a TU-local value
   14462              :    or object.  Emits an explanation if EXPLAIN is true.  */
   14463              : 
   14464              : static bool
   14465       106300 : is_tu_local_value (tree decl, tree expr, bool explain/*=false*/)
   14466              : {
   14467       106300 :   if (!expr)
   14468              :     return false;
   14469              : 
   14470       104404 :   tree e = expr;
   14471       104404 :   STRIP_ANY_LOCATION_WRAPPER (e);
   14472       104404 :   STRIP_NOPS (e);
   14473       104404 :   if (TREE_CODE (e) == TARGET_EXPR)
   14474            0 :     e = TARGET_EXPR_INITIAL (e);
   14475            0 :   if (!e)
   14476              :     return false;
   14477              : 
   14478              :   /* It is, or is a pointer to, a TU-local function or the object associated
   14479              :      with a TU-local variable.  */
   14480       104404 :   tree object = NULL_TREE;
   14481       104404 :   if (TREE_CODE (e) == ADDR_EXPR)
   14482         2794 :     object = TREE_OPERAND (e, 0);
   14483       101610 :   else if (TREE_CODE (e) == PTRMEM_CST)
   14484            0 :     object = PTRMEM_CST_MEMBER (e);
   14485       101610 :   else if (VAR_OR_FUNCTION_DECL_P (e))
   14486              :     object = e;
   14487              : 
   14488         2794 :   if (object
   14489         3524 :       && VAR_OR_FUNCTION_DECL_P (object)
   14490         3611 :       && is_tu_local_entity (object))
   14491              :     {
   14492           54 :       if (explain)
   14493              :         {
   14494              :           /* We've lost a lot of location information by the time we get here,
   14495              :              so let's just do our best effort.  */
   14496           18 :           auto loc = cp_expr_loc_or_loc (expr, DECL_SOURCE_LOCATION (decl));
   14497           18 :           if (VAR_P (object))
   14498            9 :             inform (loc, "%qD refers to TU-local object %qD", decl, object);
   14499              :           else
   14500            9 :             inform (loc, "%qD refers to TU-local function %qD", decl, object);
   14501           18 :           is_tu_local_entity (object, true);
   14502              :         }
   14503           54 :       return true;
   14504              :     }
   14505              : 
   14506              :   /* It is an object of class or array type and any of its subobjects or
   14507              :      any of the objects or functions to which its non-static data members
   14508              :      of reference type refer is TU-local and is usable in constant
   14509              :      expressions.  */
   14510       104350 :   if (TREE_CODE (e) == CONSTRUCTOR && AGGREGATE_TYPE_P (TREE_TYPE (e)))
   14511        52970 :     for (auto &f : CONSTRUCTOR_ELTS (e))
   14512        42317 :       if (is_tu_local_value (decl, f.value, explain))
   14513              :         return true;
   14514              : 
   14515              :   return false;
   14516              : }
   14517              : 
   14518              : /* Complains if DECL is a TU-local entity imported from a named module.
   14519              :    Returns TRUE if instantiation should fail.  */
   14520              : 
   14521              : bool
   14522   8607048786 : instantiating_tu_local_entity (tree decl)
   14523              : {
   14524   8607048786 :   if (!modules_p ())
   14525              :     return false;
   14526              : 
   14527     34294278 :   if (TREE_CODE (decl) == TU_LOCAL_ENTITY)
   14528              :     {
   14529           92 :       auto_diagnostic_group d;
   14530           92 :       error ("instantiation exposes TU-local entity %qD",
   14531           92 :              TU_LOCAL_ENTITY_NAME (decl));
   14532           92 :       inform (TU_LOCAL_ENTITY_LOCATION (decl), "declared here");
   14533           92 :       return true;
   14534           92 :     }
   14535              : 
   14536              :   /* Currently, only TU-local variables and functions, or possibly
   14537              :      templates thereof, will be emitted from named modules.  */
   14538     34294186 :   tree inner = STRIP_TEMPLATE (decl);
   14539     34294186 :   if (!VAR_OR_FUNCTION_DECL_P (inner))
   14540              :     return false;
   14541              : 
   14542              :   /* From this point we will only be emitting warnings; if we're not
   14543              :      warning about this case then there's no need to check further.  */
   14544      1429117 :   if (!warn_expose_global_module_tu_local
   14545      2858234 :       || !warning_enabled_at (DECL_SOURCE_LOCATION (decl),
   14546      1429117 :                               OPT_Wexpose_global_module_tu_local))
   14547        11128 :     return false;
   14548              : 
   14549      1417989 :   if (!is_tu_local_entity (decl))
   14550              :     return false;
   14551              : 
   14552           65 :   if (!DECL_LANG_SPECIFIC (inner)
   14553          120 :       || !DECL_MODULE_IMPORT_P (inner))
   14554              :     return false;
   14555              : 
   14556              :   /* Referencing TU-local entities from a header is generally OK.
   14557              :      We don't have an easy way to detect if this declaration came
   14558              :      from a header via a separate named module, but we can just
   14559              :      ignore that case for warning purposes.  */
   14560            9 :   unsigned index = import_entity_index (decl);
   14561            9 :   module_state *mod = import_entity_module (index);
   14562            9 :   if (mod->is_header ())
   14563              :     return false;
   14564              : 
   14565            9 :   auto_diagnostic_group d;
   14566            9 :   pedwarn (input_location, OPT_Wexpose_global_module_tu_local,
   14567              :            "instantiation exposes TU-local entity %qD", decl);
   14568            9 :   inform (DECL_SOURCE_LOCATION (decl), "declared here");
   14569              : 
   14570              :   /* We treat TU-local entities from the GMF as not actually being
   14571              :      TU-local as an extension, so allow instantiation to proceed.  */
   14572            9 :   return false;
   14573            9 : }
   14574              : 
   14575              : /* DECL is a newly discovered dependency.  Create the depset, if it
   14576              :    doesn't already exist.  Add it to the worklist if so.
   14577              : 
   14578              :    DECL will be an OVL_USING_P OVERLOAD, if it's from a binding that's
   14579              :    a using decl.
   14580              : 
   14581              :    We do not have to worry about adding the same dependency more than
   14582              :    once.  First it's harmless, but secondly the TREE_VISITED marking
   14583              :    prevents us wanting to do it anyway.  */
   14584              : 
   14585              : depset *
   14586      8338577 : depset::hash::make_dependency (tree decl, entity_kind ek)
   14587              : {
   14588              :   /* Make sure we're being told consistent information.  */
   14589     15599549 :   gcc_checking_assert ((ek == EK_NAMESPACE)
   14590              :                        == (TREE_CODE (decl) == NAMESPACE_DECL
   14591              :                            && !DECL_NAMESPACE_ALIAS (decl)));
   14592      8338577 :   gcc_checking_assert (ek != EK_BINDING && ek != EK_REDIRECT);
   14593      8338577 :   gcc_checking_assert (TREE_CODE (decl) != FIELD_DECL
   14594              :                        && (TREE_CODE (decl) != USING_DECL
   14595              :                            || TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL));
   14596      8338577 :   gcc_checking_assert (!is_key_order ());
   14597      8338577 :   if (ek == EK_USING)
   14598        39362 :     gcc_checking_assert (TREE_CODE (decl) == OVERLOAD);
   14599      8338577 :   if (ek == EK_TU_LOCAL)
   14600           93 :     gcc_checking_assert (DECL_DECLARES_FUNCTION_P (decl));
   14601              : 
   14602      8338577 :   if (TREE_CODE (decl) == TEMPLATE_DECL)
   14603              :     /* The template should have copied these from its result decl.  */
   14604      3184238 :     gcc_checking_assert (DECL_MODULE_EXPORT_P (decl)
   14605              :                          == DECL_MODULE_EXPORT_P (DECL_TEMPLATE_RESULT (decl)));
   14606              : 
   14607      8338577 :   depset **slot = entity_slot (decl, true);
   14608      8338577 :   depset *dep = *slot;
   14609      8338577 :   bool for_binding = ek == EK_FOR_BINDING;
   14610              : 
   14611      8338577 :   if (!dep)
   14612              :     {
   14613       727237 :       if ((DECL_IMPLICIT_TYPEDEF_P (decl)
   14614              :            /* ... not an enum, for instance.  */
   14615       360685 :            && RECORD_OR_UNION_TYPE_P (TREE_TYPE (decl))
   14616       355693 :            && TYPE_LANG_SPECIFIC (TREE_TYPE (decl))
   14617       325564 :            && CLASSTYPE_USE_TEMPLATE (TREE_TYPE (decl)) == 2)
   14618      2493062 :           || (VAR_P (decl)
   14619        97046 :               && DECL_LANG_SPECIFIC (decl)
   14620        96986 :               && DECL_USE_TEMPLATE (decl) == 2))
   14621              :         {
   14622              :           /* A partial or explicit specialization. Partial
   14623              :              specializations might not be in the hash table, because
   14624              :              there can be multiple differently-constrained variants.
   14625              : 
   14626              :              template<typename T> class silly;
   14627              :              template<typename T> requires true class silly {};
   14628              : 
   14629              :              We need to find them, insert their TEMPLATE_DECL in the
   14630              :              dep_hash, and then convert the dep we just found into a
   14631              :              redirect.  */
   14632              : 
   14633        47131 :           tree ti = get_template_info (decl);
   14634        47131 :           tree tmpl = TI_TEMPLATE (ti);
   14635        47131 :           tree partial = NULL_TREE;
   14636        47131 :           for (tree spec = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
   14637       169739 :                spec; spec = TREE_CHAIN (spec))
   14638       147914 :             if (DECL_TEMPLATE_RESULT (TREE_VALUE (spec)) == decl)
   14639              :               {
   14640              :                 partial = TREE_VALUE (spec);
   14641              :                 break;
   14642              :               }
   14643              : 
   14644        47131 :           if (partial)
   14645              :             {
   14646              :               /* Eagerly create an empty redirect.  The following
   14647              :                  make_dependency call could cause hash reallocation,
   14648              :                  and invalidate slot's value.  */
   14649        25306 :               depset *redirect = make_entity (decl, EK_REDIRECT);
   14650              : 
   14651              :               /* Redirects are never reached -- always snap to their target.  */
   14652        25306 :               redirect->set_flag_bit<DB_UNREACHED_BIT> ();
   14653              : 
   14654        25306 :               *slot = redirect;
   14655              : 
   14656        25306 :               depset *tmpl_dep = make_dependency (partial, EK_PARTIAL);
   14657        25306 :               gcc_checking_assert (tmpl_dep->get_entity_kind () == EK_PARTIAL);
   14658              : 
   14659        25306 :               redirect->deps.safe_push (tmpl_dep);
   14660              : 
   14661        25306 :               return redirect;
   14662              :             }
   14663              :         }
   14664              : 
   14665      1779852 :       bool has_def = ek != EK_USING && has_definition (decl);
   14666      1740490 :       if (ek > EK_BINDING)
   14667       153967 :         ek = EK_DECL;
   14668              : 
   14669              :       /* The only OVERLOADS we should see are USING decls from
   14670              :          bindings.  */
   14671      1779852 :       *slot = dep = make_entity (decl, ek, has_def);
   14672              : 
   14673      1779852 :       if (CHECKING_P && TREE_CODE (decl) == TEMPLATE_DECL)
   14674              :         /* The template_result should otherwise not be in the
   14675              :            table, or be an empty redirect (created above).  */
   14676       457624 :         if (auto *eslot = entity_slot (DECL_TEMPLATE_RESULT (decl), false))
   14677        25306 :           gcc_checking_assert ((*eslot)->get_entity_kind () == EK_REDIRECT
   14678              :                                && !(*eslot)->deps.length ());
   14679              : 
   14680      1779852 :       if (ignore_exposure)
   14681        45317 :         dep->set_flag_bit<DB_IGNORED_EXPOSURE_BIT> ();
   14682              : 
   14683      1779852 :       if (ek != EK_USING)
   14684              :         {
   14685      1740490 :           tree not_tmpl = STRIP_TEMPLATE (decl);
   14686      1740490 :           bool imported_from_module_p = false;
   14687              : 
   14688      1740490 :           if (DECL_LANG_SPECIFIC (not_tmpl)
   14689      3325532 :               && DECL_MODULE_IMPORT_P (not_tmpl))
   14690              :             {
   14691              :               /* Store the module number and index in cluster/section,
   14692              :                  so we don't have to look them up again.  */
   14693        88844 :               unsigned index = import_entity_index (decl);
   14694        88844 :               module_state *from = import_entity_module (index);
   14695              :               /* Remap will be zero for imports from partitions, which
   14696              :                  we want to treat as-if declared in this TU.  */
   14697        88844 :               if (from->remap)
   14698              :                 {
   14699        88090 :                   dep->cluster = index - from->entity_lwm;
   14700        88090 :                   dep->section = from->remap;
   14701        88090 :                   dep->set_flag_bit<DB_IMPORTED_BIT> ();
   14702              : 
   14703        88090 :                   if (!from->is_header ())
   14704      1740490 :                     imported_from_module_p = true;
   14705              :                 }
   14706              :             }
   14707              : 
   14708              :           /* Check for TU-local entities.  This is unnecessary in header
   14709              :              units because we can export internal-linkage decls, and
   14710              :              no declarations are exposures.  Similarly, if the decl was
   14711              :              imported from a non-header module we know it cannot have
   14712              :              been TU-local.  */
   14713      1740490 :           if (!header_module_p () && !imported_from_module_p)
   14714              :             {
   14715       817377 :               if (is_tu_local_entity (decl))
   14716          305 :                 dep->set_flag_bit<DB_TU_LOCAL_BIT> ();
   14717              : 
   14718       817377 :               if (VAR_P (decl)
   14719        43311 :                   && decl_maybe_constant_var_p (decl)
   14720       859438 :                   && is_tu_local_value (decl, DECL_INITIAL (decl)))
   14721              :                 {
   14722              :                   /* A potentially-constant variable initialized to a TU-local
   14723              :                      value is not usable in constant expressions within other
   14724              :                      translation units.  We can achieve this by simply not
   14725              :                      streaming the definition in such cases.  */
   14726           24 :                   dep->clear_flag_bit<DB_DEFN_BIT> ();
   14727              : 
   14728           24 :                   if (DECL_DECLARED_CONSTEXPR_P (decl)
   14729           39 :                       || DECL_INLINE_VAR_P (decl))
   14730              :                     /* A constexpr variable initialized to a TU-local value,
   14731              :                        or an inline value (PR c++/119996), is an exposure.
   14732              : 
   14733              :                        For simplicity, we don't support "non-strict" TU-local
   14734              :                        values: even if the TU-local entity we refer to in the
   14735              :                        initialiser is in the GMF, we still won't consider this
   14736              :                        valid in constant expressions in other TUs, and so
   14737              :                        complain accordingly.  */
   14738           15 :                     dep->set_flag_bit<DB_EXPOSE_PURVIEW_BIT> ();
   14739              :                 }
   14740              :             }
   14741              : 
   14742              :           /* A namespace-scope type may be declared in one module unit
   14743              :              and defined in another; make sure that we're found when
   14744              :              completing the class.  */
   14745      1740490 :           if (ek == EK_DECL
   14746       683437 :               && !dep->is_import ()
   14747       675009 :               && dep->has_defn ()
   14748       356719 :               && DECL_NAMESPACE_SCOPE_P (not_tmpl)
   14749       129669 :               && DECL_IMPLICIT_TYPEDEF_P (not_tmpl)
   14750              :               /* Anonymous types can't be forward-declared.  */
   14751      1774846 :               && !IDENTIFIER_ANON_P (DECL_NAME (not_tmpl)))
   14752        33850 :             dep->set_flag_bit<DB_IS_PENDING_BIT> ();
   14753              : 
   14754              :           /* Namespace-scope functions can be found by ADL by template
   14755              :              instantiations in this module.  We need to create bindings
   14756              :              for them so that name lookup recognises they exist, if they
   14757              :              won't be discarded.  add_binding_entity is too early to do
   14758              :              this for GM functions, because if nobody ends up using them
   14759              :              we'll have leftover bindings laying around, and it's tricky
   14760              :              to delete them and any namespaces they've implicitly created
   14761              :              deps on.  The downside is this means we don't pick up on
   14762              :              using-decls, but by [module.global.frag] p3.6 we don't have
   14763              :              to.  */
   14764      1740490 :           if (ek == EK_DECL
   14765      1740490 :               && !for_binding
   14766       529482 :               && !dep->is_import ()
   14767       521060 :               && !dep->is_tu_local ()
   14768       520956 :               && DECL_NAMESPACE_SCOPE_P (decl)
   14769        60727 :               && DECL_DECLARES_FUNCTION_P (decl)
   14770              :               /* Compiler-generated functions won't participate in ADL.  */
   14771        47054 :               && !DECL_ARTIFICIAL (decl)
   14772              :               /* A hidden friend doesn't need a binding.  */
   14773      1780067 :               && !(DECL_LANG_SPECIFIC (not_tmpl)
   14774        39577 :                    && DECL_UNIQUE_FRIEND_P (not_tmpl)))
   14775              :             {
   14776              :               /* This will only affect GM functions.  */
   14777        48146 :               gcc_checking_assert (!DECL_LANG_SPECIFIC (not_tmpl)
   14778              :                                    || !DECL_MODULE_PURVIEW_P (not_tmpl));
   14779              :               /* We shouldn't see any instantiations or specialisations.  */
   14780        24073 :               gcc_checking_assert (!DECL_LANG_SPECIFIC (decl)
   14781              :                                    || !DECL_USE_TEMPLATE (decl));
   14782              : 
   14783        24073 :               tree ns = CP_DECL_CONTEXT (decl);
   14784        24073 :               tree name = DECL_NAME (decl);
   14785        24073 :               depset *binding = find_binding (ns, name);
   14786        24073 :               if (!binding)
   14787              :                 {
   14788         7618 :                   binding = make_binding (ns, name);
   14789         7618 :                   add_namespace_context (binding, ns);
   14790              : 
   14791         7618 :                   depset **slot = binding_slot (ns, name, /*insert=*/true);
   14792         7618 :                   *slot = binding;
   14793              :                 }
   14794              : 
   14795        24073 :               binding->deps.safe_push (dep);
   14796        24073 :               dep->deps.safe_push (binding);
   14797        24108 :               dump (dumper::DEPEND)
   14798            9 :                 && dump ("Built ADL binding for %C:%N",
   14799            9 :                          TREE_CODE (decl), decl);
   14800              :             }
   14801              :         }
   14802              : 
   14803      1779852 :       if (!dep->is_import ())
   14804      1691762 :         worklist.safe_push (dep);
   14805              :     }
   14806      6533419 :   else if (!ignore_exposure)
   14807      5781288 :     dep->clear_flag_bit<DB_IGNORED_EXPOSURE_BIT> ();
   14808              : 
   14809      8313271 :   dump (dumper::DEPEND)
   14810        36712 :     && dump ("%s on %s %C:%N found",
   14811              :              ek == EK_REDIRECT ? "Redirect"
   14812        36712 :              : (for_binding || ek == EK_TU_LOCAL) ? "Binding"
   14813              :              : "Dependency",
   14814        36712 :              dep->entity_kind_name (), TREE_CODE (decl), decl);
   14815              : 
   14816      8313271 :   return dep;
   14817              : }
   14818              : 
   14819              : /* Whether REF is an exposure of a member type of SOURCE.
   14820              : 
   14821              :    This comes up with exposures of class-scope lambdas, that we currently
   14822              :    treat as TU-local due to ABI reasons.  In such a case the type of the
   14823              :    lambda will be exposed in two places, first by the class type it is in
   14824              :    the TYPE_FIELDS list of, and second by the actual member declaring that
   14825              :    lambda.  We only want the second case to warn.  */
   14826              : 
   14827              : static bool
   14828          253 : is_exposure_of_member_type (depset *source, depset *ref)
   14829              : {
   14830          253 :   gcc_checking_assert (source->refs_tu_local (/*strict=*/true)
   14831              :                        && ref->is_tu_local (/*strict=*/true));
   14832          253 :   tree source_entity = STRIP_TEMPLATE (source->get_entity ());
   14833          253 :   tree ref_entity = STRIP_TEMPLATE (ref->get_entity ());
   14834              : 
   14835          253 :   if (!source->is_tu_local (/*strict=*/true)
   14836          241 :       && source_entity
   14837          241 :       && ref_entity
   14838          241 :       && DECL_IMPLICIT_TYPEDEF_P (source_entity)
   14839            2 :       && DECL_IMPLICIT_TYPEDEF_P (ref_entity)
   14840            2 :       && DECL_CLASS_SCOPE_P (ref_entity)
   14841          255 :       && DECL_CONTEXT (ref_entity) == TREE_TYPE (source_entity))
   14842              :     {
   14843            4 :       gcc_checking_assert (LAMBDA_TYPE_P (TREE_TYPE (ref_entity)));
   14844              :       return true;
   14845              :     }
   14846              :   else
   14847              :     return false;
   14848              : }
   14849              : 
   14850              : /* DEP is a newly discovered dependency.  Append it to current's
   14851              :    depset.  */
   14852              : 
   14853              : void
   14854      6270107 : depset::hash::add_dependency (depset *dep)
   14855              : {
   14856      6270107 :   gcc_checking_assert (current && !is_key_order ());
   14857      6270107 :   current->deps.safe_push (dep);
   14858              : 
   14859      6270107 :   if (dep->is_tu_local (/*strict=*/true))
   14860              :     {
   14861          325 :       if (dep->is_tu_local ())
   14862          260 :         current->set_flag_bit<DB_REF_PURVIEW_BIT> ();
   14863              :       else
   14864           65 :         current->set_flag_bit<DB_REF_GLOBAL_BIT> ();
   14865              : 
   14866          325 :       if (!ignore_exposure && !is_exposure_of_member_type (current, dep))
   14867              :         {
   14868          133 :           if (dep->is_tu_local ())
   14869           91 :             current->set_flag_bit<DB_EXPOSE_PURVIEW_BIT> ();
   14870              :           else
   14871           42 :             current->set_flag_bit<DB_EXPOSE_GLOBAL_BIT> ();
   14872              :         }
   14873              :     }
   14874              : 
   14875      6270107 :   if (current->get_entity_kind () == EK_USING
   14876        39362 :       && DECL_IMPLICIT_TYPEDEF_P (dep->get_entity ())
   14877      6275489 :       && TREE_CODE (TREE_TYPE (dep->get_entity ())) == ENUMERAL_TYPE)
   14878              :     {
   14879              :       /* CURRENT is an unwrapped using-decl and DECL is an enum's
   14880              :          implicit typedef.  Is CURRENT a member of the enum?  */
   14881         4963 :       tree c_decl = OVL_FUNCTION (current->get_entity ());
   14882              : 
   14883         4963 :       if (TREE_CODE (c_decl) == CONST_DECL
   14884         9882 :           && (current->deps[0]->get_entity ()
   14885         4919 :               == CP_DECL_CONTEXT (dep->get_entity ())))
   14886              :         /* Make DECL depend on CURRENT.  */
   14887         4865 :         dep->deps.safe_push (current);
   14888              :     }
   14889              : 
   14890              :   /* If two dependencies recursively depend on each other existing within
   14891              :      their own merge keys, we must ensure that the first dep we saw while
   14892              :      walking is written first in this cluster.  See sort_cluster for more
   14893              :      details.  */
   14894      6270107 :   if (writing_merge_key)
   14895              :     {
   14896      1118838 :       if (!dep->is_maybe_recursive () && !current->is_maybe_recursive ())
   14897        59745 :         current->set_flag_bit<DB_ENTRY_BIT> ();
   14898      1118838 :       dep->set_flag_bit<DB_MAYBE_RECURSIVE_BIT> ();
   14899      1118838 :       current->set_flag_bit<DB_MAYBE_RECURSIVE_BIT> ();
   14900              :     }
   14901              : 
   14902      6270107 :   if (dep->is_unreached ())
   14903              :     {
   14904              :       /* The dependency is reachable now.  */
   14905       456801 :       reached_unreached = true;
   14906       456801 :       dep->clear_flag_bit<DB_UNREACHED_BIT> ();
   14907       456801 :       dump (dumper::DEPEND)
   14908           30 :         && dump ("Reaching unreached %s %C:%N", dep->entity_kind_name (),
   14909           30 :                  TREE_CODE (dep->get_entity ()), dep->get_entity ());
   14910              :     }
   14911      6270107 : }
   14912              : 
   14913              : depset *
   14914      9188069 : depset::hash::add_dependency (tree decl, entity_kind ek)
   14915              : {
   14916      9188069 :   depset *dep;
   14917              : 
   14918      9188069 :   if (is_key_order ())
   14919              :     {
   14920      2909448 :       dep = find_dependency (decl);
   14921      2909448 :       if (dep)
   14922              :         {
   14923      1321520 :           current->deps.safe_push (dep);
   14924      1321520 :           dump (dumper::MERGE)
   14925          723 :             && dump ("Key dependency on %s %C:%N found",
   14926          723 :                      dep->entity_kind_name (), TREE_CODE (decl), decl);
   14927              :         }
   14928              :       else
   14929              :         {
   14930              :           /* It's not a mergeable decl, look for it in the original
   14931              :              table.  */
   14932      1587928 :           dep = chain->find_dependency (decl);
   14933      1587928 :           gcc_checking_assert (dep);
   14934              :         }
   14935              :     }
   14936              :   else
   14937              :     {
   14938      6278621 :       dep = make_dependency (decl, ek);
   14939      6278621 :       if (dep->get_entity_kind () != EK_REDIRECT)
   14940      6218380 :         add_dependency (dep);
   14941              :     }
   14942              : 
   14943      9188069 :   return dep;
   14944              : }
   14945              : 
   14946              : void
   14947       713350 : depset::hash::add_namespace_context (depset *dep, tree ns)
   14948              : {
   14949       713350 :   depset *ns_dep = make_dependency (ns, depset::EK_NAMESPACE);
   14950       713350 :   dep->deps.safe_push (ns_dep);
   14951              : 
   14952              :   /* Mark it as special if imported so we don't walk connect when
   14953              :      SCCing.  */
   14954       713350 :   if (!dep->is_binding () && ns_dep->is_import ())
   14955            0 :     dep->set_special ();
   14956       713350 : }
   14957              : 
   14958              : struct add_binding_data
   14959              : {
   14960              :   tree ns;
   14961              :   bitmap partitions;
   14962              :   depset *binding;
   14963              :   depset::hash *hash;
   14964              :   bool met_namespace;
   14965              : };
   14966              : 
   14967              : /* Return true if we are, or contain something that is exported.  */
   14968              : 
   14969              : bool
   14970      5845436 : depset::hash::add_binding_entity (tree decl, WMB_Flags flags, void *data_)
   14971              : {
   14972      5845436 :   auto data = static_cast <add_binding_data *> (data_);
   14973      5845436 :   decl = strip_using_decl (decl);
   14974              : 
   14975      5845436 :   if (!(TREE_CODE (decl) == NAMESPACE_DECL && !DECL_NAMESPACE_ALIAS (decl)))
   14976              :     {
   14977      5836906 :       tree inner = decl;
   14978              : 
   14979      5836906 :       if (TREE_CODE (inner) == CONST_DECL
   14980         9661 :           && TREE_CODE (DECL_CONTEXT (inner)) == ENUMERAL_TYPE
   14981              :           /* A using-decl could make a CONST_DECL purview for a non-purview
   14982              :              enumeration.  */
   14983      5846567 :           && (!DECL_LANG_SPECIFIC (inner) || !DECL_MODULE_PURVIEW_P (inner)))
   14984         9620 :         inner = TYPE_NAME (DECL_CONTEXT (inner));
   14985      5827286 :       else if (TREE_CODE (inner) == TEMPLATE_DECL)
   14986       141118 :         inner = DECL_TEMPLATE_RESULT (inner);
   14987              : 
   14988     11489762 :       if ((!DECL_LANG_SPECIFIC (inner) || !DECL_MODULE_PURVIEW_P (inner))
   14989     11315142 :           && !((flags & WMB_Using) && (flags & WMB_Purview)))
   14990              :         /* Ignore entities not within the module purview.  We'll need to
   14991              :            create bindings for any non-discarded function calls for ADL,
   14992              :            but it's simpler to handle that at the point of use rather
   14993              :            than trying to clear out bindings after the fact.  */
   14994              :         return false;
   14995              : 
   14996       197875 :       if ((flags & WMB_Hidden)
   14997         5513 :           && DECL_LANG_SPECIFIC (inner)
   14998       203388 :           && DECL_UNIQUE_FRIEND_P (inner))
   14999              :         /* Hidden friends will be found via ADL on the class type,
   15000              :            and so do not need to have bindings.  Anticipated builtin
   15001              :            functions and the hidden decl underlying a DECL_LOCAL_DECL_P
   15002              :            also don't need exporting, but we should create a binding
   15003              :            anyway so that we can have a common decl to match against.  */
   15004              :         return false;
   15005              : 
   15006       192451 :       bool internal_decl = false;
   15007       192451 :       if (!header_module_p () && is_tu_local_entity (decl)
   15008       192712 :           && !((flags & WMB_Using) && (flags & WMB_Export)))
   15009              :         {
   15010              :           /* A TU-local entity.  For ADL we still need to create bindings
   15011              :              for internal-linkage functions attached to a named module.  */
   15012          150 :           if (DECL_DECLARES_FUNCTION_P (inner)
   15013          105 :               && DECL_LANG_SPECIFIC (inner)
   15014          360 :               && DECL_MODULE_ATTACH_P (inner))
   15015              :             {
   15016           93 :               gcc_checking_assert (!DECL_MODULE_EXPORT_P (inner));
   15017              :               internal_decl = true;
   15018              :             }
   15019              :           else
   15020              :             return false;
   15021              :         }
   15022              : 
   15023       192289 :       if ((TREE_CODE (decl) == VAR_DECL
   15024       192289 :            || TREE_CODE (decl) == TYPE_DECL)
   15025       192289 :           && DECL_TINFO_P (decl))
   15026              :         /* Ignore TINFO things.  */
   15027              :         return false;
   15028              : 
   15029       192289 :       if (TREE_CODE (decl) == VAR_DECL && DECL_NTTP_OBJECT_P (decl))
   15030              :         /* Ignore NTTP objects.  */
   15031              :         return false;
   15032              : 
   15033       192289 :       if (deduction_guide_p (decl))
   15034              :         {
   15035              :           /* Ignore deduction guides, bindings for them will be created within
   15036              :              find_dependencies for their class template.  But still build a dep
   15037              :              for them so that we don't discard them.  */
   15038         1616 :           data->hash->make_dependency (decl, EK_FOR_BINDING);
   15039         1616 :           return false;
   15040              :         }
   15041              : 
   15042       190673 :       if (!(flags & WMB_Using) && CP_DECL_CONTEXT (decl) != data->ns)
   15043              :         {
   15044              :           /* An unscoped enum constant implicitly brought into the containing
   15045              :              namespace.  We treat this like a using-decl.  */
   15046         4085 :           gcc_checking_assert (TREE_CODE (decl) == CONST_DECL);
   15047              : 
   15048         4085 :           flags = WMB_Flags (flags | WMB_Using);
   15049         4085 :           if (DECL_MODULE_EXPORT_P (TYPE_NAME (TREE_TYPE (decl)))
   15050              :               /* A using-decl can make an enum constant exported for a
   15051              :                  non-exported enumeration.  */
   15052         4085 :               || (DECL_LANG_SPECIFIC (decl) && DECL_MODULE_EXPORT_P (decl)))
   15053         3854 :             flags = WMB_Flags (flags | WMB_Export);
   15054              :         }
   15055              : 
   15056       190673 :       if (!data->binding)
   15057              :         /* No binding to check.  */;
   15058        41567 :       else if (flags & WMB_Using)
   15059              :         {
   15060              :           /* Look in the binding to see if we already have this
   15061              :              using.  */
   15062       160268 :           for (unsigned ix = data->binding->deps.length (); --ix;)
   15063              :             {
   15064       129477 :               depset *d = data->binding->deps[ix];
   15065       258954 :               if (d->get_entity_kind () == EK_USING
   15066       129477 :                   && OVL_FUNCTION (d->get_entity ()) == decl)
   15067              :                 {
   15068            3 :                   if (!(flags & WMB_Hidden))
   15069            3 :                     d->clear_hidden_binding ();
   15070            3 :                   OVL_PURVIEW_P (d->get_entity ()) = true;
   15071            3 :                   if (flags & WMB_Export)
   15072            3 :                     OVL_EXPORT_P (d->get_entity ()) = true;
   15073            3 :                   return bool (flags & WMB_Export);
   15074              :                 }
   15075              :             }
   15076              :         }
   15077        26170 :       else if (flags & WMB_Dups)
   15078              :         {
   15079              :           /* Look in the binding to see if we already have this decl.  */
   15080           78 :           for (unsigned ix = data->binding->deps.length (); --ix;)
   15081              :             {
   15082           39 :               depset *d = data->binding->deps[ix];
   15083           39 :               if (d->get_entity () == decl)
   15084              :                 {
   15085           33 :                   if (!(flags & WMB_Hidden))
   15086           33 :                     d->clear_hidden_binding ();
   15087           33 :                   return false;
   15088              :                 }
   15089              :             }
   15090              :         }
   15091              : 
   15092              :       /* We're adding something.  */
   15093       190637 :       if (!data->binding)
   15094              :         {
   15095       149106 :           data->binding = make_binding (data->ns, DECL_NAME (decl));
   15096       149106 :           data->hash->add_namespace_context (data->binding, data->ns);
   15097              : 
   15098       149106 :           depset **slot = data->hash->binding_slot (data->ns,
   15099       149106 :                                                     DECL_NAME (decl), true);
   15100       149106 :           gcc_checking_assert (!*slot);
   15101       149106 :           *slot = data->binding;
   15102              :         }
   15103              : 
   15104              :       /* Make sure nobody left a tree visited lying about.  */
   15105       190637 :       gcc_checking_assert (!TREE_VISITED (decl));
   15106              : 
   15107       190637 :       if (flags & WMB_Using)
   15108              :         {
   15109        39362 :           decl = ovl_make (decl, NULL_TREE);
   15110        39362 :           OVL_USING_P (decl) = true;
   15111        39362 :           OVL_PURVIEW_P (decl) = true;
   15112        39362 :           if (flags & WMB_Export)
   15113        38272 :             OVL_EXPORT_P (decl) = true;
   15114              :         }
   15115              : 
   15116       190637 :       entity_kind ek = EK_FOR_BINDING;
   15117       190637 :       if (internal_decl)
   15118              :         ek = EK_TU_LOCAL;
   15119       190544 :       else if (flags & WMB_Using)
   15120        39362 :         ek = EK_USING;
   15121              : 
   15122       190637 :       depset *dep = data->hash->make_dependency (decl, ek);
   15123       190637 :       if (flags & WMB_Hidden)
   15124           89 :         dep->set_hidden_binding ();
   15125       190637 :       data->binding->deps.safe_push (dep);
   15126              :       /* Binding and contents are mutually dependent.  */
   15127       190637 :       dep->deps.safe_push (data->binding);
   15128              : 
   15129       190637 :       return (flags & WMB_Using
   15130       190637 :               ? flags & WMB_Export : DECL_MODULE_EXPORT_P (decl));
   15131              :     }
   15132         8530 :   else if (!data->met_namespace)
   15133              :     {
   15134              :       /* Namespace, walk exactly once.  */
   15135         8521 :       data->met_namespace = true;
   15136         8521 :       if (data->hash->add_namespace_entities (decl, data->partitions))
   15137              :         {
   15138              :           /* It contains an exported thing, so it is exported.  */
   15139         1707 :           gcc_checking_assert (DECL_MODULE_PURVIEW_P (decl));
   15140         1707 :           gcc_checking_assert (TREE_PUBLIC (decl) || header_module_p ());
   15141         1707 :           DECL_MODULE_EXPORT_P (decl) = true;
   15142              :         }
   15143              : 
   15144         8521 :       if (DECL_MODULE_PURVIEW_P (decl))
   15145              :         {
   15146         2072 :           data->hash->make_dependency (decl, depset::EK_NAMESPACE);
   15147              : 
   15148         2072 :           return DECL_MODULE_EXPORT_P (decl);
   15149              :         }
   15150              :     }
   15151              : 
   15152              :   return false;
   15153              : }
   15154              : 
   15155              : /* Recursively find all the namespace bindings of NS.  Add a depset
   15156              :    for every binding that contains an export or module-linkage entity.
   15157              :    Add a defining depset for every such decl that we need to write a
   15158              :    definition.  Such defining depsets depend on the binding depset.
   15159              :    Returns true if we contain something exported.  */
   15160              : 
   15161              : bool
   15162        11307 : depset::hash::add_namespace_entities (tree ns, bitmap partitions)
   15163              : {
   15164        12519 :   dump () && dump ("Looking for writables in %N", ns);
   15165        11307 :   dump.indent ();
   15166              : 
   15167        11307 :   unsigned count = 0;
   15168        11307 :   add_binding_data data;
   15169        11307 :   data.ns = ns;
   15170        11307 :   data.partitions = partitions;
   15171        11307 :   data.hash = this;
   15172              : 
   15173     15215373 :   for (tree binding : *DECL_NAMESPACE_BINDINGS (ns))
   15174              :     {
   15175      7602033 :       data.binding = nullptr;
   15176      7602033 :       data.met_namespace = false;
   15177      7602033 :       if (walk_module_binding (binding, partitions, add_binding_entity, &data))
   15178       141335 :         count++;
   15179              :     }
   15180              : 
   15181              :   /* Seed any using-directives so that we emit the relevant namespaces.  */
   15182        11910 :   for (tree udir : NAMESPACE_LEVEL (ns)->using_directives)
   15183          207 :     if (TREE_CODE (udir) == USING_DECL && DECL_MODULE_PURVIEW_P (udir))
   15184              :       {
   15185          172 :         make_dependency (USING_DECL_DECLS (udir), depset::EK_NAMESPACE);
   15186          172 :         if (DECL_MODULE_EXPORT_P (udir))
   15187          101 :           count++;
   15188              :       }
   15189              : 
   15190        11307 :   if (count)
   15191         4023 :     dump () && dump ("Found %u entries", count);
   15192        11307 :   dump.outdent ();
   15193              : 
   15194        11307 :   return count != 0;
   15195              : }
   15196              : 
   15197              : void
   15198          215 : depset::hash::add_partial_entities (vec<tree, va_gc> *partial_classes)
   15199              : {
   15200        21622 :   for (unsigned ix = 0; ix != partial_classes->length (); ix++)
   15201              :     {
   15202        21407 :       tree inner = (*partial_classes)[ix];
   15203              : 
   15204        21407 :       depset *dep = make_dependency (inner, depset::EK_DECL);
   15205              : 
   15206        21407 :       if (dep->get_entity_kind () == depset::EK_REDIRECT)
   15207              :         {
   15208        21407 :           dep = dep->deps[0];
   15209              :           /* We should have recorded the template as a partial
   15210              :              specialization.  */
   15211        21407 :           gcc_checking_assert (dep->get_entity_kind ()
   15212              :                                == depset::EK_PARTIAL);
   15213              : 
   15214              :           /* Only emit GM entities if reached.  */
   15215        21407 :           if (!DECL_LANG_SPECIFIC (inner)
   15216        33931 :               || !DECL_MODULE_PURVIEW_P (inner))
   15217         9748 :             dep->set_flag_bit<DB_UNREACHED_BIT> ();
   15218              :         }
   15219              :       else
   15220              :         {
   15221              :           /* It was an explicit specialization, not a partial one.
   15222              :              We should have already added this.  */
   15223            0 :           gcc_checking_assert (dep->get_entity_kind ()
   15224              :                                == depset::EK_SPECIALIZATION);
   15225            0 :           gcc_checking_assert (dep->is_special ());
   15226              :         }
   15227              :     }
   15228          215 : }
   15229              : 
   15230              : /* Add the members of imported classes that we defined in this TU.
   15231              :    This will also include lazily created implicit member function
   15232              :    declarations.  (All others will be definitions.)  */
   15233              : 
   15234              : void
   15235           12 : depset::hash::add_class_entities (vec<tree, va_gc> *class_members)
   15236              : {
   15237           24 :   for (unsigned ix = 0; ix != class_members->length (); ix++)
   15238              :     {
   15239           12 :       tree defn = (*class_members)[ix];
   15240           12 :       depset *dep = make_dependency (defn, EK_INNER_DECL);
   15241              : 
   15242           12 :       if (dep->get_entity_kind () == EK_REDIRECT)
   15243            0 :         dep = dep->deps[0];
   15244              : 
   15245              :       /* Only non-instantiations need marking as pendings.  */
   15246           24 :       if (dep->get_entity_kind () == EK_DECL)
   15247           12 :         dep->set_flag_bit <DB_IS_PENDING_BIT> ();
   15248              :     }
   15249           12 : }
   15250              : 
   15251              : /* Add any entities found via dependent ADL.  */
   15252              : 
   15253              : void
   15254      7905192 : depset::hash::add_dependent_adl_entities (tree expr)
   15255              : {
   15256      7905192 :   gcc_checking_assert (!is_key_order ());
   15257              : 
   15258              :   /* This is not needed for header units where everything is
   15259              :      visible to name lookup, nothing is discarded.  */
   15260      7905192 :   if (header_module_p ())
   15261      7868784 :     return;
   15262              : 
   15263      3322403 :   if (TREE_CODE (current->get_entity ()) != TEMPLATE_DECL)
   15264              :     return;
   15265              : 
   15266      1849445 :   dep_adl_info info;
   15267      1849445 :   auto_vec<tree, 3> args;
   15268      1849445 :   switch (TREE_CODE (expr))
   15269              :     {
   15270       196357 :     case CALL_EXPR:
   15271       196357 :       if (!KOENIG_LOOKUP_P (expr)
   15272       196357 :           || !type_dependent_expression_p_push (expr))
   15273       189484 :         return;
   15274         6873 :       info.name = CALL_EXPR_FN (expr);
   15275         6873 :       if (!info.name)
   15276              :         return;
   15277         6873 :       if (TREE_CODE (info.name) == TEMPLATE_ID_EXPR)
   15278         1180 :         info.name = TREE_OPERAND (info.name, 0);
   15279         6873 :       if (TREE_CODE (info.name) == TU_LOCAL_ENTITY)
   15280              :         return;
   15281        12111 :       if (!identifier_p (info.name))
   15282         6731 :         info.name = OVL_NAME (info.name);
   15283        21970 :       for (int ix = 0; ix < call_expr_nargs (expr); ix++)
   15284        15097 :         args.safe_push (CALL_EXPR_ARG (expr, ix));
   15285              :       break;
   15286              : 
   15287        14311 :     case LE_EXPR:
   15288        14311 :     case GE_EXPR:
   15289        14311 :     case LT_EXPR:
   15290        14311 :     case GT_EXPR:
   15291        14311 :       info.rewrite = SPACESHIP_EXPR;
   15292        14311 :       goto overloadable_expr;
   15293              : 
   15294         7625 :     case NE_EXPR:
   15295         7625 :       info.rewrite = EQ_EXPR;
   15296         7625 :       goto overloadable_expr;
   15297              : 
   15298        15773 :     case EQ_EXPR:
   15299              :       /* Not strictly a rewrite candidate, but we need to ensure
   15300              :          that lookup of a matching NE_EXPR can succeed if that
   15301              :          would inhibit a rewrite with reversed parameters.  */
   15302        15773 :       info.rewrite = NE_EXPR;
   15303        15773 :       goto overloadable_expr;
   15304              : 
   15305       116732 :     case COMPOUND_EXPR:
   15306       116732 :     case MEMBER_REF:
   15307       116732 :     case MULT_EXPR:
   15308       116732 :     case TRUNC_DIV_EXPR:
   15309       116732 :     case TRUNC_MOD_EXPR:
   15310       116732 :     case PLUS_EXPR:
   15311       116732 :     case MINUS_EXPR:
   15312       116732 :     case LSHIFT_EXPR:
   15313       116732 :     case RSHIFT_EXPR:
   15314       116732 :     case SPACESHIP_EXPR:
   15315       116732 :     case BIT_AND_EXPR:
   15316       116732 :     case BIT_XOR_EXPR:
   15317       116732 :     case BIT_IOR_EXPR:
   15318       116732 :     case TRUTH_ANDIF_EXPR:
   15319       116732 :     case TRUTH_ORIF_EXPR:
   15320       116732 :     overloadable_expr:
   15321       116732 :       if (!type_dependent_expression_p_push (expr))
   15322              :         return;
   15323        85257 :       info.name = ovl_op_identifier (TREE_CODE (expr));
   15324        85257 :       gcc_checking_assert (tree_operand_length (expr) == 2);
   15325        85257 :       args.safe_push (TREE_OPERAND (expr, 0));
   15326        85257 :       args.safe_push (TREE_OPERAND (expr, 1));
   15327        85257 :       break;
   15328              : 
   15329              :     default:
   15330              :       return;
   15331              :     }
   15332              : 
   15333              :   /* If all arguments are type-dependent we don't need to do
   15334              :      anything further, we won't find new entities.  */
   15335        92130 :   bool all_type_dependent = true;
   15336       411070 :   for (tree arg : args)
   15337       171088 :     if (!type_dependent_expression_p_push (arg))
   15338              :       {
   15339              :         all_type_dependent = false;
   15340              :         break;
   15341              :       }
   15342        92130 :   if (all_type_dependent)
   15343              :     return;
   15344              : 
   15345        36408 :   gcc_checking_assert (!info.args);
   15346        36408 :   info.args = make_tree_vector ();
   15347       185064 :   for (tree arg : args)
   15348        75840 :     vec_safe_push (info.args, arg);
   15349              : 
   15350              :   /* We need to defer name lookup until after walking, otherwise
   15351              :      we get confused by stray TREE_VISITEDs.  */
   15352        36408 :   dep_adl_entity_list.safe_push (info);
   15353      1849445 : }
   15354              : 
   15355              : /* We add the partial & explicit specializations, and the explicit
   15356              :    instantiations.  */
   15357              : 
   15358              : static void
   15359      1055901 : specialization_add (bool decl_p, spec_entry *entry, void *data_)
   15360              : {
   15361      1055901 :   vec<spec_entry *> *data = reinterpret_cast <vec<spec_entry *> *> (data_);
   15362              : 
   15363      1055901 :   if (!decl_p)
   15364              :     {
   15365              :       /* We exclusively use decls to locate things.  Make sure there's
   15366              :          no mismatch between the two specialization tables we keep.
   15367              :          pt.cc optimizes instantiation lookup using a complicated
   15368              :          heuristic.  We don't attempt to replicate that algorithm, but
   15369              :          observe its behaviour and reproduce it upon read back.  */
   15370              : 
   15371       313500 :        gcc_checking_assert (TREE_CODE (entry->spec) == ENUMERAL_TYPE
   15372              :                            || DECL_CLASS_TEMPLATE_P (entry->tmpl));
   15373              : 
   15374       313500 :        gcc_checking_assert (!match_mergeable_specialization (true, entry));
   15375              :     }
   15376       742401 :   else if (VAR_OR_FUNCTION_DECL_P (entry->spec))
   15377       364728 :     gcc_checking_assert (!DECL_LOCAL_DECL_P (entry->spec));
   15378              : 
   15379      1055901 :   data->safe_push (entry);
   15380      1055901 : }
   15381              : 
   15382              : /* Arbitrary stable comparison.  */
   15383              : 
   15384              : static int
   15385     64012639 : specialization_cmp (const void *a_, const void *b_)
   15386              : {
   15387     64012639 :   const spec_entry *ea = *reinterpret_cast<const spec_entry *const *> (a_);
   15388     64012639 :   const spec_entry *eb = *reinterpret_cast<const spec_entry *const *> (b_);
   15389              : 
   15390     64012639 :   if (ea == eb)
   15391              :     return 0;
   15392              : 
   15393     64012639 :   tree a = ea->spec;
   15394     64012639 :   tree b = eb->spec;
   15395     64012639 :   if (TYPE_P (a))
   15396              :     {
   15397     18038736 :       a = TYPE_NAME (a);
   15398     18038736 :       b = TYPE_NAME (b);
   15399              :     }
   15400              : 
   15401     64012639 :   if (a == b)
   15402              :     /* This can happen with friend specializations.  Just order by
   15403              :        entry address.  See note in depset_cmp.  */
   15404            0 :     return ea < eb ? -1 : +1;
   15405              : 
   15406     64012639 :   return DECL_UID (a) < DECL_UID (b) ? -1 : +1;
   15407              : }
   15408              : 
   15409              : /* We add all kinds of specialializations.  Implicit specializations
   15410              :    should only streamed and walked if they are reachable from
   15411              :    elsewhere.  Hence the UNREACHED flag.  This is making the
   15412              :    assumption that it is cheaper to reinstantiate them on demand
   15413              :    elsewhere, rather than stream them in when we instantiate their
   15414              :    general template.  Also, if we do stream them, we can only do that
   15415              :    if they are not internal (which they can become if they themselves
   15416              :    touch an internal entity?).  */
   15417              : 
   15418              : void
   15419         5572 : depset::hash::add_specializations (bool decl_p)
   15420              : {
   15421         5572 :   vec<spec_entry *> data;
   15422         5572 :   data.create (100);
   15423         5572 :   walk_specializations (decl_p, specialization_add, &data);
   15424         5572 :   data.qsort (specialization_cmp);
   15425      1061473 :   while (data.length ())
   15426              :     {
   15427      1055901 :       spec_entry *entry = data.pop ();
   15428      1055901 :       tree spec = entry->spec;
   15429      1055901 :       int use_tpl = 0;
   15430      1055901 :       bool is_friend = false;
   15431              : 
   15432      1055901 :       if (decl_p && DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (entry->tmpl))
   15433              :         /* A friend of a template.  This is keyed to the
   15434              :            instantiation.  */
   15435              :         is_friend = true;
   15436              : 
   15437      1055901 :       if (decl_p)
   15438              :         {
   15439       742401 :           if (tree ti = DECL_TEMPLATE_INFO (spec))
   15440              :             {
   15441       742401 :               tree tmpl = TI_TEMPLATE (ti);
   15442              : 
   15443       742401 :               use_tpl = DECL_USE_TEMPLATE (spec);
   15444       742401 :               if (spec == DECL_TEMPLATE_RESULT (tmpl))
   15445              :                 {
   15446         5495 :                   spec = tmpl;
   15447         5495 :                   gcc_checking_assert (DECL_USE_TEMPLATE (spec) == use_tpl);
   15448              :                 }
   15449       736906 :               else if (is_friend)
   15450              :                 {
   15451         5017 :                   if (TI_TEMPLATE (ti) != entry->tmpl
   15452         5017 :                       || !template_args_equal (TI_ARGS (ti), entry->tmpl))
   15453         5017 :                     goto template_friend;
   15454              :                 }
   15455              :             }
   15456              :           else
   15457              :             {
   15458            0 :             template_friend:;
   15459         5017 :               gcc_checking_assert (is_friend);
   15460              :               /* This is a friend of a template class, but not the one
   15461              :                  that generated entry->spec itself (i.e. it's an
   15462              :                  equivalent clone).  We do not need to record
   15463              :                  this.  */
   15464         5017 :               continue;
   15465              :             }
   15466              :         }
   15467              :       else
   15468              :         {
   15469       313500 :           if (TREE_CODE (spec) == ENUMERAL_TYPE)
   15470              :             {
   15471         1489 :               tree ctx = DECL_CONTEXT (TYPE_NAME (spec));
   15472              : 
   15473         1489 :               if (TYPE_P (ctx))
   15474         1483 :                 use_tpl = CLASSTYPE_USE_TEMPLATE (ctx);
   15475              :               else
   15476            6 :                 use_tpl = DECL_USE_TEMPLATE (ctx);
   15477              :             }
   15478              :           else
   15479       312011 :             use_tpl = CLASSTYPE_USE_TEMPLATE (spec);
   15480              : 
   15481       313500 :           tree ti = TYPE_TEMPLATE_INFO (spec);
   15482       313500 :           tree tmpl = TI_TEMPLATE (ti);
   15483              : 
   15484       313500 :           spec = TYPE_NAME (spec);
   15485       313500 :           if (spec == DECL_TEMPLATE_RESULT (tmpl))
   15486              :             {
   15487         1607 :               spec = tmpl;
   15488         1607 :               use_tpl = DECL_USE_TEMPLATE (spec);
   15489              :             }
   15490              :         }
   15491              : 
   15492      1050884 :       bool needs_reaching = false;
   15493      1050884 :       if (use_tpl == 1)
   15494              :         /* Implicit instantiations only walked if we reach them.  */
   15495              :         needs_reaching = true;
   15496        83626 :       else if (!DECL_LANG_SPECIFIC (STRIP_TEMPLATE (spec))
   15497       150360 :                || !DECL_MODULE_PURVIEW_P (STRIP_TEMPLATE (spec)))
   15498              :         /* Likewise, GMF explicit or partial specializations.  */
   15499              :         needs_reaching = true;
   15500              : 
   15501              : #if false && CHECKING_P
   15502              :       /* The instantiation isn't always on
   15503              :          DECL_TEMPLATE_INSTANTIATIONS, */
   15504              :       // FIXME: we probably need to remember this information?
   15505              :       /* Verify the specialization is on the
   15506              :          DECL_TEMPLATE_INSTANTIATIONS of the template.  */
   15507              :       for (tree cons = DECL_TEMPLATE_INSTANTIATIONS (entry->tmpl);
   15508              :            cons; cons = TREE_CHAIN (cons))
   15509              :         if (TREE_VALUE (cons) == entry->spec)
   15510              :           {
   15511              :             gcc_assert (entry->args == TREE_PURPOSE (cons));
   15512              :             goto have_spec;
   15513              :           }
   15514              :       gcc_unreachable ();
   15515              :     have_spec:;
   15516              : #endif
   15517              : 
   15518              :       /* Make sure nobody left a tree visited lying about.  */
   15519      1050884 :       gcc_checking_assert (!TREE_VISITED (spec));
   15520      1050884 :       depset *dep = make_dependency (spec, depset::EK_SPECIALIZATION);
   15521      1050884 :       if (dep->is_special ())
   15522            0 :         gcc_unreachable ();
   15523              :       else
   15524              :         {
   15525      1050884 :           if (dep->get_entity_kind () == depset::EK_REDIRECT)
   15526        24386 :             dep = dep->deps[0];
   15527      1026498 :           else if (dep->get_entity_kind () == depset::EK_SPECIALIZATION)
   15528              :             {
   15529      1026498 :               dep->set_special ();
   15530      1026498 :               dep->deps.safe_push (reinterpret_cast<depset *> (entry));
   15531      1026498 :               if (!decl_p)
   15532       293013 :                 dep->set_flag_bit<DB_TYPE_SPEC_BIT> ();
   15533              :             }
   15534              : 
   15535      1050884 :           if (needs_reaching)
   15536      1005812 :             dep->set_flag_bit<DB_UNREACHED_BIT> ();
   15537      1050884 :           if (is_friend)
   15538            0 :             dep->set_flag_bit<DB_FRIEND_SPEC_BIT> ();
   15539              :         }
   15540              :     }
   15541         5572 :   data.release ();
   15542         5572 : }
   15543              : 
   15544              : /* Add a depset into the mergeable hash.  */
   15545              : 
   15546              : void
   15547      1193553 : depset::hash::add_mergeable (depset *mergeable)
   15548              : {
   15549      1193553 :   gcc_checking_assert (is_key_order ());
   15550      1193553 :   entity_kind ek = mergeable->get_entity_kind ();
   15551      1193553 :   tree decl = mergeable->get_entity ();
   15552      1193553 :   gcc_checking_assert (ek < EK_DIRECT_HWM);
   15553              : 
   15554      1193553 :   depset **slot = entity_slot (decl, true);
   15555      1193553 :   gcc_checking_assert (!*slot);
   15556      1193553 :   depset *dep = make_entity (decl, ek);
   15557      1193553 :   *slot = dep;
   15558              : 
   15559      1193553 :   worklist.safe_push (dep);
   15560              : 
   15561              :   /* So we can locate the mergeable depset this depset refers to,
   15562              :      mark the first dep.  */
   15563      1193553 :   dep->set_special ();
   15564      1193553 :   dep->deps.safe_push (mergeable);
   15565      1193553 : }
   15566              : 
   15567              : /* Find the innermost-namespace scope of DECL, and that
   15568              :    namespace-scope decl.  */
   15569              : 
   15570              : tree
   15571     38836548 : find_pending_key (tree decl, tree *decl_p = nullptr)
   15572              : {
   15573     38836548 :   tree ns = decl;
   15574     46736240 :   do
   15575              :     {
   15576     46736240 :       decl = ns;
   15577     46736240 :       ns = CP_DECL_CONTEXT (ns);
   15578     46736240 :       if (TYPE_P (ns))
   15579      4898542 :         ns = TYPE_NAME (ns);
   15580              :     }
   15581     46736240 :   while (TREE_CODE (ns) != NAMESPACE_DECL);
   15582              : 
   15583     38836548 :   if (decl_p)
   15584     38283525 :     *decl_p = decl;
   15585              : 
   15586     38836548 :   return ns;
   15587              : }
   15588              : 
   15589              : /* Creates bindings and dependencies for all deduction guides of
   15590              :    the given class template DECL as needed.  */
   15591              : 
   15592              : void
   15593        55984 : depset::hash::add_deduction_guides (tree decl)
   15594              : {
   15595              :   /* Alias templates never have deduction guides.  */
   15596        55984 :   if (DECL_ALIAS_TEMPLATE_P (decl))
   15597        54999 :     return;
   15598              : 
   15599              :   /* We don't need to do anything for class-scope deduction guides,
   15600              :      as they will be added as members anyway.  */
   15601        55984 :   if (!DECL_NAMESPACE_SCOPE_P (decl))
   15602              :     return;
   15603              : 
   15604        43330 :   tree ns = CP_DECL_CONTEXT (decl);
   15605        43330 :   tree name = dguide_name (decl);
   15606              : 
   15607              :   /* We always add all deduction guides with a given name at once,
   15608              :      so if there's already a binding there's nothing to do.  */
   15609        43330 :   if (find_binding (ns, name))
   15610              :     return;
   15611              : 
   15612        40323 :   tree guides = lookup_qualified_name (ns, name, LOOK_want::NORMAL,
   15613              :                                        /*complain=*/false);
   15614        40323 :   if (guides == error_mark_node)
   15615              :     return;
   15616              : 
   15617          985 :   depset *binding = nullptr;
   15618         4743 :   for (tree t : lkp_range (guides))
   15619              :     {
   15620         2773 :       gcc_checking_assert (!TREE_VISITED (t));
   15621         2773 :       depset *dep = make_dependency (t, EK_FOR_BINDING);
   15622              : 
   15623              :       /* We don't want to create bindings for imported deduction guides, as
   15624              :          this would potentially cause name lookup to return duplicates.  */
   15625         2773 :       if (dep->is_import ())
   15626            6 :         continue;
   15627              : 
   15628         2767 :       if (!binding)
   15629              :         {
   15630              :           /* We have bindings to add.  */
   15631          979 :           binding = make_binding (ns, name);
   15632          979 :           add_namespace_context (binding, ns);
   15633              : 
   15634          979 :           depset **slot = binding_slot (ns, name, /*insert=*/true);
   15635          979 :           *slot = binding;
   15636              :         }
   15637              : 
   15638         2767 :       binding->deps.safe_push (dep);
   15639         2767 :       dep->deps.safe_push (binding);
   15640         2767 :       dump (dumper::DEPEND)
   15641            0 :         && dump ("Built binding for deduction guide %C:%N",
   15642            0 :                  TREE_CODE (decl), decl);
   15643              :     }
   15644              : }
   15645              : 
   15646              : /* Iteratively find dependencies.  During the walk we may find more
   15647              :    entries on the same binding that need walking.  */
   15648              : 
   15649              : void
   15650       314393 : depset::hash::find_dependencies (module_state *module)
   15651              : {
   15652       314393 :   trees_out walker (NULL, module, *this);
   15653       314393 :   vec<depset *> unreached;
   15654       628786 :   unreached.create (worklist.length ());
   15655              : 
   15656         1126 :   for (;;)
   15657              :     {
   15658       315519 :       reached_unreached = false;
   15659      5427736 :       while (worklist.length ())
   15660              :         {
   15661      5112217 :           depset *item = worklist.pop ();
   15662              : 
   15663      5112217 :           gcc_checking_assert (!item->is_binding ());
   15664      5112217 :           if (item->is_unreached ())
   15665      2679883 :             unreached.quick_push (item);
   15666              :           else
   15667              :             {
   15668      2432334 :               current = item;
   15669      2432334 :               tree decl = current->get_entity ();
   15670      2432334 :               dump (is_key_order () ? dumper::MERGE : dumper::DEPEND)
   15671      2433657 :                 && dump ("Dependencies of %s %C:%N",
   15672         1323 :                          is_key_order () ? "key-order"
   15673         1323 :                          : current->entity_kind_name (), TREE_CODE (decl), decl);
   15674      2432334 :               dump.indent ();
   15675      2432334 :               walker.begin ();
   15676      2432334 :               if (current->get_entity_kind () == EK_USING)
   15677        39362 :                 walker.tree_node (OVL_FUNCTION (decl));
   15678      2392972 :               else if (current->get_entity_kind () == EK_TU_LOCAL)
   15679              :                 /* We only stream its name and location.  */
   15680           93 :                 module->note_location (DECL_SOURCE_LOCATION (decl));
   15681      2392879 :               else if (TREE_VISITED (decl))
   15682              :                 /* A global tree.  */;
   15683      2390347 :               else if (current->get_entity_kind () == EK_NAMESPACE)
   15684              :                 {
   15685         2624 :                   module->note_location (DECL_SOURCE_LOCATION (decl));
   15686         2624 :                   add_namespace_context (current, CP_DECL_CONTEXT (decl));
   15687              :                 }
   15688              :               else
   15689              :                 {
   15690      2387723 :                   walker.mark_declaration (decl, current->has_defn ());
   15691              : 
   15692      2387723 :                   if (!is_key_order ()
   15693      2387723 :                       && item->is_pending_entity ())
   15694              :                     {
   15695       553023 :                       tree ns = find_pending_key (decl, nullptr);
   15696       553023 :                       add_namespace_context (item, ns);
   15697              :                     }
   15698              : 
   15699      2387723 :                   auto ovr = make_temp_override
   15700      2387723 :                     (ignore_exposure, item->is_ignored_exposure_context ());
   15701      2387723 :                   walker.decl_value (decl, current);
   15702      2387723 :                   if (current->has_defn ())
   15703       460688 :                     walker.write_definition (decl, current->refs_tu_local ());
   15704      2387723 :                 }
   15705      2432334 :               walker.end ();
   15706              : 
   15707              :               /* If we see either a class template or a deduction guide, make
   15708              :                  sure to add all visible deduction guides.  We need to check
   15709              :                  both in case they have been added in separate modules, or
   15710              :                  one is in the GMF and would have otherwise been discarded.  */
   15711      2432334 :               if (!is_key_order ()
   15712      2432334 :                   && DECL_CLASS_TEMPLATE_P (decl))
   15713        53211 :                 add_deduction_guides (decl);
   15714      2432334 :               if (!is_key_order ()
   15715      2432334 :                   && deduction_guide_p (decl))
   15716         2773 :                 add_deduction_guides (TYPE_NAME (TREE_TYPE (TREE_TYPE (decl))));
   15717              : 
   15718              :               /* Handle dependent ADL for [module.global.frag] p3.3.  */
   15719      2432334 :               if (!is_key_order () && !dep_adl_entity_list.is_empty ())
   15720              :                 {
   15721        19365 :                   processing_template_decl_sentinel ptds;
   15722        19365 :                   ++processing_template_decl;
   15723        55773 :                   for (auto &info : dep_adl_entity_list)
   15724              :                     {
   15725        36408 :                       tree lookup = lookup_arg_dependent (info.name, NULL_TREE,
   15726              :                                                           info.args, true);
   15727       113942 :                       for (tree fn : lkp_range (lookup))
   15728        41126 :                         add_dependency (make_dependency (fn, EK_DECL));
   15729              : 
   15730        36408 :                       if (info.rewrite)
   15731              :                         {
   15732         8420 :                           tree rewrite_name = ovl_op_identifier (info.rewrite);
   15733         8420 :                           lookup = lookup_arg_dependent (rewrite_name, NULL_TREE,
   15734              :                                                          info.args, true);
   15735        27441 :                           for (tree fn : lkp_range (lookup))
   15736        10601 :                             add_dependency (make_dependency (fn, EK_DECL));
   15737              :                         }
   15738        36408 :                       release_tree_vector (info.args);
   15739              :                     }
   15740        19365 :                   dep_adl_entity_list.truncate (0);
   15741        19365 :                 }
   15742              : 
   15743      2432334 :               if (!is_key_order ()
   15744      1238781 :                   && TREE_CODE (decl) == TEMPLATE_DECL
   15745      2862207 :                   && !DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (decl))
   15746              :                 {
   15747              :                   /* Mark all the explicit & partial specializations as
   15748              :                      reachable.  We search both specialization lists as some
   15749              :                      constrained partial specializations for class types are
   15750              :                      only found in DECL_TEMPLATE_SPECIALIZATIONS.  */
   15751      1229940 :                   auto mark_reached = [this](tree spec)
   15752              :                     {
   15753       808333 :                       if (TYPE_P (spec))
   15754       233872 :                         spec = TYPE_NAME (spec);
   15755       808333 :                       int use_tpl;
   15756       808333 :                       node_template_info (spec, use_tpl);
   15757       808333 :                       if (use_tpl & 2)
   15758              :                         {
   15759        82683 :                           depset *spec_dep = find_dependency (spec);
   15760        82683 :                           if (spec_dep->get_entity_kind () == EK_REDIRECT)
   15761        18623 :                             spec_dep = spec_dep->deps[0];
   15762        82683 :                           if (spec_dep->is_unreached ())
   15763              :                             {
   15764        16981 :                               reached_unreached = true;
   15765        16981 :                               spec_dep->clear_flag_bit<DB_UNREACHED_BIT> ();
   15766        16981 :                               dump (dumper::DEPEND)
   15767            0 :                                 && dump ("Reaching unreached specialization"
   15768            0 :                                          " %C:%N", TREE_CODE (spec), spec);
   15769              :                             }
   15770              :                         }
   15771      1229940 :                     };
   15772              : 
   15773       421607 :                   for (tree cons = DECL_TEMPLATE_INSTANTIATIONS (decl);
   15774      1210594 :                        cons; cons = TREE_CHAIN (cons))
   15775       788987 :                     mark_reached (TREE_VALUE (cons));
   15776       421607 :                   for (tree cons = DECL_TEMPLATE_SPECIALIZATIONS (decl);
   15777       440953 :                        cons; cons = TREE_CHAIN (cons))
   15778        19346 :                     mark_reached (TREE_VALUE (cons));
   15779              :                 }
   15780              : 
   15781      2432334 :               dump.outdent ();
   15782      2432334 :               current = NULL;
   15783              :             }
   15784              :         }
   15785              : 
   15786       315519 :       if (!reached_unreached)
   15787              :         break;
   15788              : 
   15789              :       /* It's possible the we reached the unreached before we
   15790              :          processed it in the above loop, so we'll be doing this an
   15791              :          extra time.  However, to avoid that we have to do some
   15792              :          bit shuffling that also involves a scan of the list.
   15793              :          Swings & roundabouts I guess.  */
   15794         1126 :       std::swap (worklist, unreached);
   15795              :     }
   15796              : 
   15797       314393 :   unreached.release ();
   15798       314393 : }
   15799              : 
   15800              : /* Compare two entries of a single binding.  TYPE_DECL before
   15801              :    non-exported before exported.  */
   15802              : 
   15803              : static int
   15804       924372 : binding_cmp (const void *a_, const void *b_)
   15805              : {
   15806       924372 :   depset *a = *(depset *const *)a_;
   15807       924372 :   depset *b = *(depset *const *)b_;
   15808              : 
   15809       924372 :   tree a_ent = a->get_entity ();
   15810       924372 :   tree b_ent = b->get_entity ();
   15811       924372 :   gcc_checking_assert (a_ent != b_ent
   15812              :                        && !a->is_binding ()
   15813              :                        && !b->is_binding ());
   15814              : 
   15815              :   /* Implicit typedefs come first.  */
   15816       924372 :   bool a_implicit = DECL_IMPLICIT_TYPEDEF_P (a_ent);
   15817       924372 :   bool b_implicit = DECL_IMPLICIT_TYPEDEF_P (b_ent);
   15818       924238 :   if (a_implicit || b_implicit)
   15819              :     {
   15820              :       /* A binding with two implicit type decls?  That's unpossible!  */
   15821          268 :       gcc_checking_assert (!(a_implicit && b_implicit));
   15822          402 :       return a_implicit ? -1 : +1;  /* Implicit first.  */
   15823              :     }
   15824              : 
   15825              :   /* TU-local before non-TU-local.  */
   15826       924104 :   bool a_internal = a->get_entity_kind () == depset::EK_TU_LOCAL;
   15827       924104 :   bool b_internal = b->get_entity_kind () == depset::EK_TU_LOCAL;
   15828       924104 :   if (a_internal != b_internal)
   15829            0 :     return a_internal ? -1 : +1;  /* Internal first.  */
   15830              : 
   15831              :   /* Hidden before non-hidden.  */
   15832       924104 :   bool a_hidden = a->is_hidden ();
   15833       924104 :   bool b_hidden = b->is_hidden ();
   15834       924104 :   if (a_hidden != b_hidden)
   15835            0 :     return a_hidden ? -1 : +1;
   15836              : 
   15837       924104 :   bool a_using = a->get_entity_kind () == depset::EK_USING;
   15838       924104 :   bool a_export;
   15839       924104 :   if (a_using)
   15840              :     {
   15841       292592 :       a_export = OVL_EXPORT_P (a_ent);
   15842       292592 :       a_ent = OVL_FUNCTION (a_ent);
   15843              :     }
   15844       631512 :   else if (TREE_CODE (a_ent) == CONST_DECL
   15845            0 :            && DECL_LANG_SPECIFIC (a_ent)
   15846       631512 :            && DECL_MODULE_EXPORT_P (a_ent))
   15847              :     a_export = true;
   15848              :   else
   15849       631512 :     a_export = DECL_MODULE_EXPORT_P (TREE_CODE (a_ent) == CONST_DECL
   15850              :                                      ? TYPE_NAME (TREE_TYPE (a_ent))
   15851              :                                      : STRIP_TEMPLATE (a_ent));
   15852              : 
   15853       924104 :   bool b_using = b->get_entity_kind () == depset::EK_USING;
   15854       924104 :   bool b_export;
   15855       924104 :   if (b_using)
   15856              :     {
   15857       306291 :       b_export = OVL_EXPORT_P (b_ent);
   15858       306291 :       b_ent = OVL_FUNCTION (b_ent);
   15859              :     }
   15860       617813 :   else if (TREE_CODE (b_ent) == CONST_DECL
   15861            0 :            && DECL_LANG_SPECIFIC (b_ent)
   15862       617813 :            && DECL_MODULE_EXPORT_P (b_ent))
   15863              :     b_export = true;
   15864              :   else
   15865       617813 :     b_export = DECL_MODULE_EXPORT_P (TREE_CODE (b_ent) == CONST_DECL
   15866              :                                      ? TYPE_NAME (TREE_TYPE (b_ent))
   15867              :                                      : STRIP_TEMPLATE (b_ent));
   15868              : 
   15869              :   /* Non-exports before exports.  */
   15870       924104 :   if (a_export != b_export)
   15871       218411 :     return a_export ? +1 : -1;
   15872              : 
   15873              :   /* At this point we don't care, but want a stable sort.  */
   15874              : 
   15875       784383 :   if (a_using != b_using)
   15876              :     /* using first.  */
   15877        24852 :     return a_using? -1 : +1;
   15878              : 
   15879       766496 :   return DECL_UID (a_ent) < DECL_UID (b_ent) ? -1 : +1;
   15880              : }
   15881              : 
   15882              : /* True iff TMPL has an explicit instantiation definition.
   15883              : 
   15884              :    This is local to module.cc because register_specialization skips adding most
   15885              :    instantiations unless module_maybe_has_cmi_p.  */
   15886              : 
   15887              : static bool
   15888           76 : template_has_explicit_inst (tree tmpl)
   15889              : {
   15890           88 :   for (tree t = DECL_TEMPLATE_INSTANTIATIONS (tmpl); t; t = TREE_CHAIN (t))
   15891              :     {
   15892           24 :       tree spec = TREE_VALUE (t);
   15893           24 :       if (DECL_EXPLICIT_INSTANTIATION (spec)
   15894           24 :           && !DECL_REALLY_EXTERN (spec))
   15895              :         return true;
   15896              :     }
   15897              :   return false;
   15898              : }
   15899              : 
   15900              : /* Complain about DEP that exposes a TU-local entity.
   15901              : 
   15902              :    If STRICT, DEP only referenced entities from the GMF.  Returns TRUE
   15903              :    if we explained anything.  */
   15904              : 
   15905              : bool
   15906          127 : depset::hash::diagnose_bad_internal_ref (depset *dep, bool strict)
   15907              : {
   15908          127 :   tree decl = dep->get_entity ();
   15909              : 
   15910              :   /* Don't need to walk if we're not going to be emitting
   15911              :      any diagnostics anyway.  */
   15912          148 :   if (strict && !warning_enabled_at (DECL_SOURCE_LOCATION (decl),
   15913           21 :                                      OPT_Wexpose_global_module_tu_local))
   15914              :     return false;
   15915              : 
   15916          523 :   for (depset *rdep : dep->deps)
   15917          135 :     if (!rdep->is_binding () && rdep->is_tu_local (strict)
   15918          369 :         && !is_exposure_of_member_type (dep, rdep))
   15919              :       {
   15920              :         // FIXME:QOI Better location information?  We're
   15921              :         // losing, so it doesn't matter about efficiency.
   15922          118 :         tree exposed = rdep->get_entity ();
   15923          118 :         auto_diagnostic_group d;
   15924          118 :         if (strict)
   15925              :           {
   15926              :             /* Allow suppressing the warning from the point of declaration
   15927              :                of the otherwise-exposed decl, for cases we know that
   15928              :                exposures will never be 'bad'.  */
   15929           27 :             if (warning_enabled_at (DECL_SOURCE_LOCATION (exposed),
   15930           27 :                                     OPT_Wexpose_global_module_tu_local)
   15931           45 :                 && pedwarn (DECL_SOURCE_LOCATION (decl),
   15932           18 :                             OPT_Wexpose_global_module_tu_local,
   15933              :                             "%qD exposes TU-local entity %qD", decl, exposed))
   15934              :               {
   15935           18 :                 bool informed = is_tu_local_entity (exposed, /*explain=*/true);
   15936           18 :                 gcc_checking_assert (informed);
   15937              :                 return true;
   15938              :               }
   15939              :           }
   15940              :         else
   15941              :           {
   15942           91 :             error_at (DECL_SOURCE_LOCATION (decl),
   15943              :                       "%qD exposes TU-local entity %qD", decl, exposed);
   15944           91 :             bool informed = is_tu_local_entity (exposed, /*explain=*/true);
   15945           91 :             gcc_checking_assert (informed);
   15946           91 :             if (dep->is_tu_local (/*strict=*/true))
   15947            3 :               inform (DECL_SOURCE_LOCATION (decl),
   15948              :                       "%qD is also TU-local but has been exposed elsewhere",
   15949              :                       decl);
   15950           91 :             return true;
   15951              :           }
   15952          118 :       }
   15953              : 
   15954              :   return false;
   15955              : }
   15956              : 
   15957              : /* Warn about a template DEP that references a TU-local entity.
   15958              : 
   15959              :    If STRICT, DEP only referenced entities from the GMF.  Returns TRUE
   15960              :    if we explained anything.  */
   15961              : 
   15962              : bool
   15963           94 : depset::hash::diagnose_template_names_tu_local (depset *dep, bool strict)
   15964              : {
   15965           94 :   tree decl = dep->get_entity ();
   15966              : 
   15967              :   /* Don't bother walking if we know we won't be emitting anything.  */
   15968           94 :   if (!warning_enabled_at (DECL_SOURCE_LOCATION (decl),
   15969           94 :                            OPT_Wtemplate_names_tu_local)
   15970              :       /* Only warn strictly if users haven't silenced this warning here.  */
   15971          121 :       || (strict && !warning_enabled_at (DECL_SOURCE_LOCATION (decl),
   15972           27 :                                          OPT_Wexpose_global_module_tu_local)))
   15973            0 :     return false;
   15974              : 
   15975              :   /* Friend decls in a class body are ignored, but this is harmless:
   15976              :      it should not impact any consumers.  */
   15977           94 :   if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (decl)))
   15978              :     return false;
   15979              : 
   15980              :   /* We should now only be warning about templates.  */
   15981           76 :   gcc_checking_assert
   15982              :     (TREE_CODE (decl) == TEMPLATE_DECL
   15983              :      && VAR_OR_FUNCTION_DECL_P (DECL_TEMPLATE_RESULT (decl)));
   15984              : 
   15985              :   /* Don't warn if we've seen any explicit instantiation definitions,
   15986              :      the intent might be for importers to only use those.  */
   15987           76 :   if (template_has_explicit_inst (decl))
   15988              :     return false;
   15989              : 
   15990          268 :   for (depset *rdep : dep->deps)
   15991          134 :     if (!rdep->is_binding () && rdep->is_tu_local (strict))
   15992              :       {
   15993           67 :         tree ref = rdep->get_entity ();
   15994           67 :         auto_diagnostic_group d;
   15995           67 :         if (strict)
   15996              :           {
   15997           15 :             if (warning_enabled_at (DECL_SOURCE_LOCATION (ref),
   15998           15 :                                     OPT_Wexpose_global_module_tu_local)
   15999           21 :                 && warning_at (DECL_SOURCE_LOCATION (decl),
   16000            6 :                                OPT_Wtemplate_names_tu_local,
   16001              :                                "%qD refers to TU-local entity %qD, which may "
   16002              :                                "cause issues when instantiating in other TUs",
   16003              :                                decl, ref))
   16004              :               {
   16005            6 :                 is_tu_local_entity (ref, /*explain=*/true);
   16006            6 :                 return true;
   16007              :               }
   16008              :           }
   16009           52 :         else if (warning_at (DECL_SOURCE_LOCATION (decl),
   16010           52 :                              OPT_Wtemplate_names_tu_local,
   16011              :                              "%qD refers to TU-local entity %qD and cannot "
   16012              :                              "be instantiated in other TUs", decl, ref))
   16013              :           {
   16014           52 :             is_tu_local_entity (ref, /*explain=*/true);
   16015           52 :             return true;
   16016              :           }
   16017           67 :       }
   16018              : 
   16019              :   return false;
   16020              : }
   16021              : 
   16022              : /* Sort the bindings, issue errors about bad internal refs.  */
   16023              : 
   16024              : bool
   16025         2786 : depset::hash::finalize_dependencies ()
   16026              : {
   16027         2786 :   bool ok = true;
   16028      3928508 :   for (depset *dep : *this)
   16029              :     {
   16030      1962861 :       if (dep->is_binding ())
   16031              :         {
   16032              :           /* Keep the containing namespace dep first.  */
   16033       157703 :           gcc_checking_assert (dep->deps.length () > 1
   16034              :                                && (dep->deps[0]->get_entity_kind ()
   16035              :                                    == EK_NAMESPACE)
   16036              :                                && (dep->deps[0]->get_entity ()
   16037              :                                    == dep->get_entity ()));
   16038       157703 :           if (dep->deps.length () > 2)
   16039        15008 :             gcc_qsort (&dep->deps[1], dep->deps.length () - 1,
   16040              :                        sizeof (dep->deps[1]), binding_cmp);
   16041              : 
   16042              :           /* Bindings shouldn't refer to imported entities.  */
   16043       157703 :           if (CHECKING_P)
   16044       848289 :             for (depset *entity : dep->deps)
   16045       375180 :               gcc_checking_assert (!entity->is_import ());
   16046       157703 :           continue;
   16047       157703 :         }
   16048              : 
   16049              :       /* Otherwise, we'll check for bad internal refs.
   16050              :          Don't complain about any references from TU-local entities.  */
   16051      1805158 :       if (dep->is_tu_local ())
   16052          264 :         continue;
   16053              : 
   16054              :       /* We already complained about usings of non-external entities in
   16055              :          check_can_export_using_decl, don't do it again here.  */
   16056      1804894 :       if (dep->get_entity_kind () == EK_USING)
   16057        39362 :         continue;
   16058              : 
   16059      1765532 :       if (dep->is_exposure ())
   16060              :         {
   16061          106 :           bool explained = diagnose_bad_internal_ref (dep);
   16062              : 
   16063              :           /* A TU-local variable will always be considered an exposure,
   16064              :              so we don't have to worry about strict-only handling.  */
   16065          106 :           tree decl = dep->get_entity ();
   16066          106 :           if (!explained
   16067           15 :               && VAR_P (decl)
   16068          121 :               && (DECL_DECLARED_CONSTEXPR_P (decl)
   16069            6 :                   || DECL_INLINE_VAR_P (decl)))
   16070              :             {
   16071           15 :               auto_diagnostic_group d;
   16072           15 :               if (DECL_DECLARED_CONSTEXPR_P (decl))
   16073            9 :                 error_at (DECL_SOURCE_LOCATION (decl),
   16074              :                           "%qD is declared %<constexpr%> and is initialized to "
   16075              :                           "a TU-local value", decl);
   16076              :               else
   16077              :                 {
   16078              :                   /* This can only occur with references.  */
   16079            6 :                   gcc_checking_assert (TYPE_REF_P (TREE_TYPE (decl)));
   16080            6 :                   error_at (DECL_SOURCE_LOCATION (decl),
   16081              :                             "%qD is a reference declared %<inline%> and is "
   16082              :                             "constant-initialized to a TU-local value", decl);
   16083              :                 }
   16084           15 :               bool informed = is_tu_local_value (decl, DECL_INITIAL (decl),
   16085              :                                                  /*explain=*/true);
   16086           15 :               gcc_checking_assert (informed);
   16087           15 :               explained = true;
   16088           15 :             }
   16089              : 
   16090              :           /* We should have emitted an error above, unless the warning was
   16091              :              silenced.  */
   16092          106 :           gcc_checking_assert (explained);
   16093          106 :           ok = false;
   16094          106 :           continue;
   16095          106 :         }
   16096              : 
   16097              :       /* In all other cases, we're just warning (rather than erroring).
   16098              :          We don't want to do too much warning, so let's just bail after
   16099              :          the first warning we successfully emit.  */
   16100      1765444 :       if (warn_expose_global_module_tu_local
   16101      1765426 :           && !dep->is_tu_local (/*strict=*/true)
   16102      1765388 :           && dep->is_exposure (/*strict=*/true)
   16103      1765447 :           && diagnose_bad_internal_ref (dep, /*strict=*/true))
   16104           18 :         continue;
   16105              : 
   16106      1765460 :       if (warn_template_names_tu_local
   16107       271185 :           && dep->refs_tu_local ()
   16108      1765475 :           && diagnose_template_names_tu_local (dep))
   16109           52 :         continue;
   16110              : 
   16111      1765356 :       if (warn_template_names_tu_local
   16112       271133 :           && warn_expose_global_module_tu_local
   16113       271133 :           && !dep->is_tu_local (/*strict=*/true)
   16114       271109 :           && dep->refs_tu_local (/*strict=*/true)
   16115           30 :           && !dep->is_exposure (/*strict=*/true)
   16116      1765383 :           && diagnose_template_names_tu_local (dep, /*strict=*/true))
   16117              :         continue;
   16118              :     }
   16119              : 
   16120         2786 :   return ok;
   16121              : }
   16122              : 
   16123              : /* Core of TARJAN's algorithm to find Strongly Connected Components
   16124              :    within a graph.  See https://en.wikipedia.org/wiki/
   16125              :    Tarjan%27s_strongly_connected_components_algorithm for details.
   16126              : 
   16127              :    We use depset::section as lowlink.  Completed nodes have
   16128              :    depset::cluster containing the cluster number, with the top
   16129              :    bit set.
   16130              : 
   16131              :    A useful property is that the output vector is a reverse
   16132              :    topological sort of the resulting DAG.  In our case that means
   16133              :    dependent SCCs are found before their dependers.  We make use of
   16134              :    that property.  */
   16135              : 
   16136              : void
   16137      2589017 : depset::tarjan::connect (depset *v)
   16138              : {
   16139      2589017 :   gcc_checking_assert (v->is_binding ()
   16140              :                        || !(v->is_tu_local ()
   16141              :                             || v->is_unreached ()
   16142              :                             || v->is_import ()));
   16143              : 
   16144      2589017 :   v->cluster = v->section = ++index;
   16145      2589017 :   stack.safe_push (v);
   16146              : 
   16147              :   /* Walk all our dependencies, ignore a first marked slot  */
   16148     22654091 :   for (unsigned ix = v->is_special (); ix != v->deps.length (); ix++)
   16149              :     {
   16150      8743203 :       depset *dep = v->deps[ix];
   16151              : 
   16152      8743203 :       if (dep->is_binding ()
   16153     17269234 :           || !(dep->is_import () || dep->is_tu_local ()))
   16154              :         {
   16155      8731657 :           unsigned lwm = dep->cluster;
   16156              : 
   16157      8731657 :           if (!dep->cluster)
   16158              :             {
   16159              :               /* A new node.  Connect it.  */
   16160      1447797 :               connect (dep);
   16161      1447797 :               lwm = dep->section;
   16162              :             }
   16163              : 
   16164      8731657 :           if (dep->section && v->section > lwm)
   16165      1365451 :             v->section = lwm;
   16166              :         }
   16167              :     }
   16168              : 
   16169      2589017 :   if (v->section == v->cluster)
   16170              :     {
   16171              :       /* Root of a new SCC.  Push all the members onto the result list. */
   16172              :       unsigned num = v->cluster;
   16173      2589017 :       depset *p;
   16174      2589017 :       do
   16175              :         {
   16176      2589017 :           p = stack.pop ();
   16177      2589017 :           p->cluster = num;
   16178      2589017 :           p->section = 0;
   16179      2589017 :           result.quick_push (p);
   16180              :         }
   16181      2589017 :       while (p != v);
   16182              :     }
   16183      2589017 : }
   16184              : 
   16185              : /* Compare two depsets.  The specific ordering is unimportant, we're
   16186              :    just trying to get consistency.  */
   16187              : 
   16188              : static int
   16189    124300010 : depset_cmp (const void *a_, const void *b_)
   16190              : {
   16191    124300010 :   depset *a = *(depset *const *)a_;
   16192    124300010 :   depset *b = *(depset *const *)b_;
   16193              : 
   16194    124300010 :   depset::entity_kind a_kind = a->get_entity_kind ();
   16195    124300010 :   depset::entity_kind b_kind = b->get_entity_kind ();
   16196              : 
   16197    124300010 :   if  (a_kind != b_kind)
   16198              :     /* Different entity kinds, order by that.  */
   16199      6246353 :     return a_kind < b_kind ? -1 : +1;
   16200              : 
   16201    119893295 :   tree a_decl = a->get_entity ();
   16202    119893295 :   tree b_decl = b->get_entity ();
   16203    119893295 :   if (a_kind == depset::EK_USING)
   16204              :     {
   16205              :       /* If one is a using, the other must be too.  */
   16206      2258815 :       a_decl = OVL_FUNCTION (a_decl);
   16207      2258815 :       b_decl = OVL_FUNCTION (b_decl);
   16208              :     }
   16209              : 
   16210    119893295 :   if (a_decl != b_decl)
   16211              :     /* Different entities, order by their UID.  */
   16212    112141298 :     return DECL_UID (a_decl) < DECL_UID (b_decl) ? -1 : +1;
   16213              : 
   16214      7751997 :   if (a_kind == depset::EK_BINDING)
   16215              :     {
   16216              :       /* Both are bindings.  Order by identifier hash.  */
   16217      7748729 :       gcc_checking_assert (a->get_name () != b->get_name ());
   16218      7748729 :       hashval_t ah = IDENTIFIER_HASH_VALUE (a->get_name ());
   16219      7748729 :       hashval_t bh = IDENTIFIER_HASH_VALUE (b->get_name ());
   16220     11543906 :       return (ah == bh ? 0 : ah < bh ? -1 : +1);
   16221              :     }
   16222              : 
   16223              :   /* They are the same decl.  This can happen with two using decls
   16224              :      pointing to the same target.  The best we can aim for is
   16225              :      consistently telling qsort how to order them.  Hopefully we'll
   16226              :      never have to debug a case that depends on this.  Oh, who am I
   16227              :      kidding?  Good luck.  */
   16228         3268 :   gcc_checking_assert (a_kind == depset::EK_USING);
   16229              : 
   16230              :   /* Order by depset address.  Not the best, but it is something.  */
   16231         3268 :   return a < b ? -1 : +1;
   16232              : }
   16233              : 
   16234              : /* Sort the clusters in SCC such that those that depend on one another
   16235              :    are placed later.   */
   16236              : 
   16237              : // FIXME: I am not convinced this is needed and, if needed,
   16238              : // sufficient.  We emit the decls in this order but that emission
   16239              : // could walk into later decls (from the body of the decl, or default
   16240              : // arg-like things).  Why doesn't that walk do the right thing?  And
   16241              : // if it DTRT why do we need to sort here -- won't things naturally
   16242              : // work?  I think part of the issue is that when we're going to refer
   16243              : // to an entity by name, and that entity is in the same cluster as us,
   16244              : // we need to actually walk that entity, if we've not already walked
   16245              : // it.
   16246              : static void
   16247       311607 : sort_cluster (depset::hash *original, depset *scc[], unsigned size)
   16248              : {
   16249       311607 :   depset::hash table (size, original);
   16250              : 
   16251       311607 :   dump.indent ();
   16252              : 
   16253              :   /* Place bindings last, usings before that.  It's not strictly
   16254              :      necessary, but it does make things neater.  Says Mr OCD.  */
   16255              :   unsigned bind_lwm = size;
   16256              :   unsigned use_lwm = size;
   16257      1701965 :   for (unsigned ix = 0; ix != use_lwm;)
   16258              :     {
   16259      1390358 :       depset *dep = scc[ix];
   16260      1390358 :       switch (dep->get_entity_kind ())
   16261              :         {
   16262       157446 :         case depset::EK_BINDING:
   16263              :           /* Move to end.  No increment.  Notice this could be moving
   16264              :              a using decl, which we'll then move again.  */
   16265       157446 :           if (--bind_lwm != ix)
   16266              :             {
   16267        90301 :               scc[ix] = scc[bind_lwm];
   16268        90301 :               scc[bind_lwm] = dep;
   16269              :             }
   16270       157446 :           if (use_lwm > bind_lwm)
   16271              :             {
   16272       125423 :               use_lwm--;
   16273       125423 :               break;
   16274              :             }
   16275              :           /* We must have copied a using or TU-local, so move it too.  */
   16276        32023 :           dep = scc[ix];
   16277        32023 :           gcc_checking_assert
   16278              :             (dep->get_entity_kind () == depset::EK_USING
   16279              :              || dep->get_entity_kind () == depset::EK_TU_LOCAL);
   16280              :           /* FALLTHROUGH  */
   16281              : 
   16282        71382 :         case depset::EK_USING:
   16283        71382 :         case depset::EK_TU_LOCAL:
   16284        71382 :           if (--use_lwm != ix)
   16285              :             {
   16286        53804 :               scc[ix] = scc[use_lwm];
   16287        53804 :               scc[use_lwm] = dep;
   16288              :             }
   16289              :           break;
   16290              : 
   16291      1193553 :         case depset::EK_DECL:
   16292      1193553 :         case depset::EK_SPECIALIZATION:
   16293      1193553 :         case depset::EK_PARTIAL:
   16294      1193553 :           table.add_mergeable (dep);
   16295      1193553 :           ix++;
   16296      1193553 :           break;
   16297              : 
   16298            0 :         default:
   16299            0 :           gcc_unreachable ();
   16300              :         }
   16301              :     }
   16302              : 
   16303       311607 :   gcc_checking_assert (use_lwm <= bind_lwm);
   16304       311895 :   dump (dumper::MERGE) && dump ("Ordering %u/%u depsets", use_lwm, size);
   16305              : 
   16306       311607 :   table.find_dependencies (nullptr);
   16307              : 
   16308       311607 :   auto_vec<depset *> order = table.connect ();
   16309       623214 :   gcc_checking_assert (order.length () == use_lwm);
   16310              : 
   16311              :   /* Now rewrite entries [0,lwm), in the dependency order we
   16312              :      discovered.  Usually each entity is in its own cluster.  Rarely,
   16313              :      we can get multi-entity clusters, in which case all but one must
   16314              :      only be reached from within the cluster.  This happens for
   16315              :      something like:
   16316              : 
   16317              :      template<typename T>
   16318              :      auto Foo (const T &arg) -> TPL<decltype (arg)>;
   16319              : 
   16320              :      The instantiation of TPL will be in the specialization table, and
   16321              :      refer to Foo via arg.  But we can only get to that specialization
   16322              :      from Foo's declaration, so we only need to treat Foo as mergeable
   16323              :      (We'll do structural comparison of TPL<decltype (arg)>).
   16324              : 
   16325              :      We approximate finding the single cluster entry dep by checking for
   16326              :      entities recursively depending on a dep first seen when streaming
   16327              :      its own merge key; the first dep we see in such a cluster should be
   16328              :      the first one streamed.  */
   16329              :   unsigned entry_pos = ~0u;
   16330              :   unsigned cluster = ~0u;
   16331      3010320 :   for (unsigned ix = 0; ix != order.length (); ix++)
   16332              :     {
   16333      1193553 :       gcc_checking_assert (order[ix]->is_special ());
   16334      1193553 :       bool tight = order[ix]->cluster == cluster;
   16335      1193553 :       depset *dep = order[ix]->deps[0];
   16336      1194546 :       dump (dumper::MERGE)
   16337         1983 :         && dump ("Mergeable %u is %N%s%s", ix, dep->get_entity (),
   16338          993 :                  tight ? " (tight)" : "", dep->is_entry () ? " (entry)" : "");
   16339      1193553 :       scc[ix] = dep;
   16340      1193553 :       if (tight)
   16341              :         {
   16342          126 :           gcc_checking_assert (dep->is_maybe_recursive ());
   16343          126 :           if (dep->is_entry ())
   16344              :             {
   16345              :               /* There should only be one entry dep in a cluster.  */
   16346            9 :               gcc_checking_assert (!scc[entry_pos]->is_entry ());
   16347            9 :               gcc_checking_assert (scc[entry_pos]->is_maybe_recursive ());
   16348            9 :               scc[ix] = scc[entry_pos];
   16349            9 :               scc[entry_pos] = dep;
   16350              :             }
   16351              :         }
   16352              :       else
   16353              :         entry_pos = ix;
   16354      1193553 :       cluster = order[ix]->cluster;
   16355              :     }
   16356              : 
   16357       311895 :   dump (dumper::MERGE) && dump ("Ordered %u keys", order.length ());
   16358       311607 :   dump.outdent ();
   16359       311607 : }
   16360              : 
   16361              : /* Reduce graph to SCCS clusters.  SCCS will be populated with the
   16362              :    depsets in dependency order.  Each depset's CLUSTER field contains
   16363              :    its cluster number.  Each SCC has a unique cluster number, and are
   16364              :    contiguous in SCCS. Cluster numbers are otherwise arbitrary.  */
   16365              : 
   16366              : vec<depset *>
   16367       314364 : depset::hash::connect ()
   16368              : {
   16369       314364 :   tarjan connector (size ());
   16370       314364 :   vec<depset *> deps;
   16371       314364 :   deps.create (size ());
   16372      6625340 :   for (depset *item : *this)
   16373              :     {
   16374      3155488 :       entity_kind kind = item->get_entity_kind ();
   16375      2998042 :       if (kind == EK_BINDING
   16376      2998042 :           || !(kind == EK_REDIRECT
   16377      2972736 :                || item->is_tu_local ()
   16378      2972605 :                || item->is_unreached ()
   16379      2440184 :                || item->is_import ()))
   16380      2589017 :         deps.quick_push (item);
   16381              :     }
   16382              : 
   16383              :   /* Iteration over the hash table is an unspecified ordering.  While
   16384              :      that has advantages, it causes 2 problems.  Firstly repeatable
   16385              :      builds are tricky.  Secondly creating testcases that check
   16386              :      dependencies are correct by making sure a bad ordering would
   16387              :      happen if that was wrong.  */
   16388      1769948 :   deps.qsort (depset_cmp);
   16389              : 
   16390      2903381 :   while (deps.length ())
   16391              :     {
   16392      2589017 :       depset *v = deps.pop ();
   16393      2589017 :       dump (dumper::CLUSTER) &&
   16394         1800 :         (v->is_binding ()
   16395          210 :          ? dump ("Connecting binding %P", v->get_entity (), v->get_name ())
   16396         1590 :          : dump ("Connecting %s %s %C:%N",
   16397         1590 :                  is_key_order () ? "key-order"
   16398          870 :                  : !v->has_defn () ? "declaration" : "definition",
   16399         1590 :                  v->entity_kind_name (), TREE_CODE (v->get_entity ()),
   16400              :                  v->get_entity ()));
   16401      2589017 :       if (!v->cluster)
   16402      1141220 :         connector.connect (v);
   16403              :     }
   16404              : 
   16405       314364 :   deps.release ();
   16406       628728 :   return connector.result;
   16407       314364 : }
   16408              : 
   16409              : /* Initialize location spans.  */
   16410              : 
   16411              : void
   16412         4929 : loc_spans::init (const line_maps *lmaps, const line_map_ordinary *map)
   16413              : {
   16414         4929 :   gcc_checking_assert (!init_p ());
   16415         4929 :   spans = new vec<span> ();
   16416         4929 :   spans->reserve (20);
   16417              : 
   16418         4929 :   span interval;
   16419         4929 :   interval.ordinary.first = 0;
   16420         4929 :   interval.macro.second = MAX_LOCATION_T + 1;
   16421         4929 :   interval.ordinary_delta = interval.macro_delta = 0;
   16422              : 
   16423              :   /* A span for reserved fixed locs.  */
   16424         4929 :   interval.ordinary.second
   16425         4929 :     = MAP_START_LOCATION (LINEMAPS_ORDINARY_MAP_AT (line_table, 0));
   16426         4929 :   interval.macro.first = interval.macro.second;
   16427         4929 :   dump (dumper::LOCATION)
   16428           42 :     && dump ("Fixed span %u ordinary:[%K,%K) macro:[%K,%K)", spans->length (),
   16429              :              interval.ordinary.first, interval.ordinary.second,
   16430              :              interval.macro.first, interval.macro.second);
   16431         4929 :   spans->quick_push (interval);
   16432              : 
   16433              :   /* A span for command line & forced headers.  */
   16434         4929 :   interval.ordinary.first = interval.ordinary.second;
   16435         4929 :   interval.macro.second = interval.macro.first;
   16436         4929 :   if (map)
   16437              :     {
   16438         4923 :       interval.ordinary.second = map->start_location;
   16439         4923 :       interval.macro.first = LINEMAPS_MACRO_LOWEST_LOCATION (lmaps);
   16440              :     }
   16441         4929 :   dump (dumper::LOCATION)
   16442           21 :     && dump ("Pre span %u ordinary:[%K,%K) macro:[%K,%K)", spans->length (),
   16443              :              interval.ordinary.first, interval.ordinary.second,
   16444              :              interval.macro.first, interval.macro.second);
   16445         4929 :   spans->quick_push (interval);
   16446              : 
   16447              :   /* Start an interval for the main file.  */
   16448         4929 :   interval.ordinary.first = interval.ordinary.second;
   16449         4929 :   interval.macro.second = interval.macro.first;
   16450         4929 :   dump (dumper::LOCATION)
   16451           21 :     && dump ("Main span %u ordinary:[%K,*) macro:[*,%K)", spans->length (),
   16452              :              interval.ordinary.first, interval.macro.second);
   16453         4929 :   spans->quick_push (interval);
   16454         4929 : }
   16455              : 
   16456              : /* Reopen the span, if we want the about-to-be-inserted set of maps to
   16457              :    be propagated in our own location table.  I.e. we are the primary
   16458              :    interface and we're importing a partition.  */
   16459              : 
   16460              : bool
   16461         3066 : loc_spans::maybe_propagate (module_state *import, location_t hwm)
   16462              : {
   16463         3066 :   bool opened = (module_interface_p () && !module_partition_p ()
   16464         3527 :                  && import->is_partition ());
   16465          172 :   if (opened)
   16466          172 :     open (hwm);
   16467         3066 :   return opened;
   16468              : }
   16469              : 
   16470              : /* Open a new linemap interval.  The just-created ordinary map is the
   16471              :    first map of the interval.  */
   16472              : 
   16473              : void
   16474         1077 : loc_spans::open (location_t hwm)
   16475              : {
   16476         1077 :   span interval;
   16477         1077 :   interval.ordinary.first = interval.ordinary.second = hwm;
   16478         2154 :   interval.macro.first = interval.macro.second
   16479         1077 :     = LINEMAPS_MACRO_LOWEST_LOCATION (line_table);
   16480         1077 :   interval.ordinary_delta = interval.macro_delta = 0;
   16481         1077 :   dump (dumper::LOCATION)
   16482            0 :     && dump ("Opening span %u ordinary:[%K,... macro:...,%K)",
   16483            0 :              spans->length (), interval.ordinary.first,
   16484              :              interval.macro.second);
   16485         1077 :   if (spans->length ())
   16486              :     {
   16487              :       /* No overlapping!  */
   16488         1077 :       auto &last = spans->last ();
   16489         1077 :       gcc_checking_assert (interval.ordinary.first >= last.ordinary.second);
   16490         1077 :       gcc_checking_assert (interval.macro.second <= last.macro.first);
   16491              :     }
   16492         1077 :   spans->safe_push (interval);
   16493         1077 : }
   16494              : 
   16495              : /* Close out the current linemap interval.  The last maps are within
   16496              :    the interval.  */
   16497              : 
   16498              : void
   16499         6003 : loc_spans::close ()
   16500              : {
   16501         6003 :   span &interval = spans->last ();
   16502              : 
   16503         6003 :   interval.ordinary.second
   16504         6003 :     = ((line_table->highest_location
   16505         6003 :         + (loc_one << line_table->default_range_bits))
   16506         6003 :        & ~((loc_one << line_table->default_range_bits) - 1));
   16507         6003 :   interval.macro.first = LINEMAPS_MACRO_LOWEST_LOCATION (line_table);
   16508         6003 :   dump (dumper::LOCATION)
   16509           21 :     && dump ("Closing span %u ordinary:[%K,%K) macro:[%K,%K)",
   16510           21 :              spans->length () - 1,
   16511              :              interval.ordinary.first,interval.ordinary.second,
   16512              :              interval.macro.first, interval.macro.second);
   16513         6003 : }
   16514              : 
   16515              : /* Given an ordinary location LOC, return the lmap_interval it resides
   16516              :    in.  NULL if it is not in an interval.  */
   16517              : 
   16518              : const loc_spans::span *
   16519     38905710 : loc_spans::ordinary (location_t loc)
   16520              : {
   16521     38905710 :   unsigned len = spans->length ();
   16522     77685449 :   unsigned pos = 0;
   16523     77695412 :   while (len)
   16524              :     {
   16525     77680508 :       unsigned half = len / 2;
   16526     77680508 :       const span &probe = (*spans)[pos + half];
   16527     77680508 :       if (loc < probe.ordinary.first)
   16528              :         len = half;
   16529     77670545 :       else if (loc < probe.ordinary.second)
   16530              :         return &probe;
   16531              :       else
   16532              :         {
   16533     38779739 :           pos += half + 1;
   16534     38779739 :           len = len - (half + 1);
   16535              :         }
   16536              :     }
   16537              :   return NULL;
   16538              : }
   16539              : 
   16540              : /* Likewise, given a macro location LOC, return the lmap interval it
   16541              :    resides in.   */
   16542              : 
   16543              : const loc_spans::span *
   16544      3039030 : loc_spans::macro (location_t loc)
   16545              : {
   16546      3039030 :   unsigned len = spans->length ();
   16547      6075038 :   unsigned pos = 0;
   16548      6075056 :   while (len)
   16549              :     {
   16550      6075026 :       unsigned half = len / 2;
   16551      6075026 :       const span &probe = (*spans)[pos + half];
   16552      6075026 :       if (loc >= probe.macro.second)
   16553              :         len = half;
   16554      6075008 :       else if (loc >= probe.macro.first)
   16555              :         return &probe;
   16556              :       else
   16557              :         {
   16558      3036008 :           pos += half + 1;
   16559      3036008 :           len = len - (half + 1);
   16560              :         }
   16561              :     }
   16562              :   return NULL;
   16563              : }
   16564              : 
   16565              : /* Return the ordinary location closest to FROM.  */
   16566              : 
   16567              : static location_t
   16568         6888 : ordinary_loc_of (line_maps *lmaps, location_t from)
   16569              : {
   16570        13779 :   while (!IS_ORDINARY_LOC (from))
   16571              :     {
   16572            3 :       if (IS_ADHOC_LOC (from))
   16573            3 :         from = get_location_from_adhoc_loc (lmaps, from);
   16574            3 :       if (from >= LINEMAPS_MACRO_LOWEST_LOCATION (lmaps))
   16575              :         {
   16576              :           /* Find the ordinary location nearest FROM.  */
   16577            0 :           const line_map *map = linemap_lookup (lmaps, from);
   16578            0 :           const line_map_macro *mac_map = linemap_check_macro (map);
   16579            0 :           from = mac_map->get_expansion_point_location ();
   16580              :         }
   16581              :     }
   16582         6888 :   return from;
   16583              : }
   16584              : 
   16585              : static module_state **
   16586        12345 : get_module_slot (tree name, module_state *parent, bool partition, bool insert)
   16587              : {
   16588        12345 :   module_state_hash::compare_type ct (name, uintptr_t (parent) | partition);
   16589        12345 :   hashval_t hv = module_state_hash::hash (ct);
   16590              : 
   16591        12345 :   return modules_hash->find_slot_with_hash (ct, hv, insert ? INSERT : NO_INSERT);
   16592              : }
   16593              : 
   16594              : static module_state *
   16595       147222 : get_primary (module_state *parent)
   16596              : {
   16597       151239 :   while (parent->is_partition ())
   16598          655 :     parent = parent->parent;
   16599              : 
   16600       150584 :   if (!parent->name)
   16601              :     // Implementation unit has null name
   16602        91096 :     parent = parent->parent;
   16603              : 
   16604       146068 :   return parent;
   16605              : }
   16606              : 
   16607              : /* Find or create module NAME & PARENT in the hash table.  */
   16608              : 
   16609              : module_state *
   16610        12345 : get_module (tree name, module_state *parent, bool partition)
   16611              : {
   16612              :   /* We might be given an empty NAME if preprocessing fails to handle
   16613              :      a header-name token.  */
   16614        12345 :   if (name && TREE_CODE (name) == STRING_CST
   16615        15232 :       && TREE_STRING_LENGTH (name) == 0)
   16616              :     return nullptr;
   16617              : 
   16618        12345 :   if (partition)
   16619              :     {
   16620         1072 :       if (!parent)
   16621          226 :         parent = get_primary (this_module ());
   16622              : 
   16623         1072 :       if (!parent->is_partition () && !parent->flatname)
   16624          253 :         parent->set_flatname ();
   16625              :     }
   16626              : 
   16627        12345 :   module_state **slot = get_module_slot (name, parent, partition, true);
   16628        12345 :   module_state *state = *slot;
   16629        12345 :   if (!state)
   16630              :     {
   16631         6687 :       state = (new (ggc_alloc<module_state> ())
   16632         6687 :                module_state (name, parent, partition));
   16633         6687 :       *slot = state;
   16634              :     }
   16635              :   return state;
   16636              : }
   16637              : 
   16638              : /* Process string name PTR into a module_state.  */
   16639              : 
   16640              : static module_state *
   16641          451 : get_module (const char *ptr)
   16642              : {
   16643              :   /* On DOS based file systems, there is an ambiguity with A:B which can be
   16644              :      interpreted as a module Module:Partition or Drive:PATH.  Interpret strings
   16645              :      which clearly starts as pathnames as header-names and everything else is
   16646              :      treated as a (possibly malformed) named moduled.  */
   16647          451 :   if (IS_DIR_SEPARATOR (ptr[ptr[0] == '.']) // ./FOO or /FOO
   16648              : #if HAVE_DOS_BASED_FILE_SYSTEM
   16649              :       || (HAS_DRIVE_SPEC (ptr) && IS_DIR_SEPARATOR (ptr[2])) // A:/FOO
   16650              : #endif
   16651              :       || false)
   16652              :     /* A header name.  */
   16653          112 :     return get_module (build_string (strlen (ptr), ptr));
   16654              : 
   16655              :   bool partition = false;
   16656              :   module_state *mod = NULL;
   16657              : 
   16658         1312 :   for (const char *probe = ptr;; probe++)
   16659         1651 :     if (!*probe || *probe == '.' || *probe == ':')
   16660              :       {
   16661          426 :         if (probe == ptr)
   16662              :           return NULL;
   16663              : 
   16664          426 :         mod = get_module (get_identifier_with_length (ptr, probe - ptr),
   16665              :                           mod, partition);
   16666          426 :         ptr = probe;
   16667          426 :         if (*ptr == ':')
   16668              :           {
   16669           84 :             if (partition)
   16670              :               return NULL;
   16671              :             partition = true;
   16672              :           }
   16673              : 
   16674          426 :         if (!*ptr++)
   16675              :           break;
   16676              :       }
   16677         1225 :     else if (!(ISALPHA (*probe) || *probe == '_'
   16678           18 :                || (probe != ptr && ISDIGIT (*probe))))
   16679              :       return NULL;
   16680              : 
   16681              :   return mod;
   16682              : }
   16683              : 
   16684              : /* Create a new mapper connecting to OPTION.  */
   16685              : 
   16686              : module_client *
   16687         4929 : make_mapper (location_t loc, class mkdeps *deps)
   16688              : {
   16689         4929 :   timevar_start (TV_MODULE_MAPPER);
   16690         4929 :   const char *option = module_mapper_name;
   16691         4929 :   if (!option)
   16692         4881 :     option = getenv ("CXX_MODULE_MAPPER");
   16693              : 
   16694         9858 :   mapper = module_client::open_module_client
   16695         4929 :     (loc, option, deps, &set_cmi_repo,
   16696         4929 :      (save_decoded_options[0].opt_index == OPT_SPECIAL_program_name)
   16697         4929 :      && save_decoded_options[0].arg != progname
   16698              :      ? save_decoded_options[0].arg : nullptr);
   16699              : 
   16700         4929 :   timevar_stop (TV_MODULE_MAPPER);
   16701              : 
   16702         4929 :   return mapper;
   16703              : }
   16704              : 
   16705              : static unsigned lazy_snum;
   16706              : 
   16707              : static bool
   16708        12057 : recursive_lazy (unsigned snum = ~0u)
   16709              : {
   16710        12057 :   if (lazy_snum)
   16711              :     {
   16712            0 :       error_at (input_location, "recursive lazy load");
   16713            0 :       return true;
   16714              :     }
   16715              : 
   16716        12057 :   lazy_snum = snum;
   16717        12057 :   return false;
   16718              : }
   16719              : 
   16720              : /* If THIS has an interface dependency on itself, report an error and
   16721              :    return false.  */
   16722              : 
   16723              : bool
   16724         2912 : module_state::check_circular_import (location_t from)
   16725              : {
   16726         2912 :   if (this == this_module ())
   16727              :     {
   16728              :       /* Cannot import the current module.  */
   16729            9 :       auto_diagnostic_group d;
   16730            9 :       error_at (from, "module %qs depends on itself", get_flatname ());
   16731            9 :       if (!header_module_p ())
   16732            6 :         inform (loc, "module %qs declared here", get_flatname ());
   16733            9 :       return false;
   16734            9 :     }
   16735              :   return true;
   16736              : }
   16737              : 
   16738              : /* Module name substitutions.  */
   16739              : static vec<module_state *,va_heap> substs;
   16740              : 
   16741              : void
   16742         9106 : module_state::mangle (bool include_partition)
   16743              : {
   16744         9106 :   if (subst)
   16745          425 :     mangle_module_substitution (subst);
   16746              :   else
   16747              :     {
   16748         8681 :       if (parent)
   16749          864 :         parent->mangle (include_partition);
   16750         8681 :       if (include_partition || !is_partition ())
   16751              :         {
   16752              :           // Partitions are significant for global initializer
   16753              :           // functions
   16754         8481 :           bool partition = is_partition () && !parent->is_partition ();
   16755         8481 :           subst = mangle_module_component (name, partition);
   16756         8481 :           substs.safe_push (this);
   16757              :         }
   16758              :     }
   16759         9106 : }
   16760              : 
   16761              : void
   16762         8242 : mangle_module (int mod, bool include_partition)
   16763              : {
   16764         8242 :   module_state *imp = (*modules)[mod];
   16765              : 
   16766         8242 :   gcc_checking_assert (!imp->is_header ());
   16767              : 
   16768         8242 :   if (!imp->name)
   16769              :     /* Set when importing the primary module interface.  */
   16770          223 :     imp = imp->parent;
   16771              : 
   16772              :   /* Ensure this is actually a module unit.  */
   16773          223 :   gcc_checking_assert (imp);
   16774              : 
   16775         8242 :   imp->mangle (include_partition);
   16776         8242 : }
   16777              : 
   16778              : /* Clean up substitutions.  */
   16779              : void
   16780         7771 : mangle_module_fini ()
   16781              : {
   16782        16252 :   while (substs.length ())
   16783         8481 :     substs.pop ()->subst = 0;
   16784         7771 : }
   16785              : 
   16786              : /* Announce WHAT about the module.  */
   16787              : 
   16788              : void
   16789        12687 : module_state::announce (const char *what) const
   16790              : {
   16791        12687 :   if (noisy_p ())
   16792              :     {
   16793            0 :       fprintf (stderr, " %s:%s", what, get_flatname ());
   16794            0 :       fflush (stderr);
   16795              :     }
   16796        12687 : }
   16797              : 
   16798              : /* A human-readable README section.  The contents of this section to
   16799              :    not contribute to the CRC, so the contents can change per
   16800              :    compilation.  That allows us to embed CWD, hostname, build time and
   16801              :    what not.  It is a STRTAB that may be extracted with:
   16802              :      readelf -pgnu.c++.README $(module).gcm */
   16803              : 
   16804              : void
   16805         2757 : module_state::write_readme (elf_out *to, cpp_reader *reader, const char *dialect)
   16806              : {
   16807         2757 :   bytes_out readme (to);
   16808              : 
   16809         2757 :   readme.begin (false);
   16810              : 
   16811         2757 :   readme.printf ("GNU C++ %s",
   16812         2757 :                  is_header () ? "header unit"
   16813         1854 :                  : !is_partition () ? "primary interface"
   16814          199 :                  : is_interface () ? "interface partition"
   16815              :                  : "internal partition");
   16816              : 
   16817              :   /* Compiler's version.  */
   16818         2757 :   readme.printf ("compiler: %s", version_string);
   16819              : 
   16820              :   /* Module format version.  */
   16821         2757 :   verstr_t string;
   16822         2757 :   version2string (MODULE_VERSION, string);
   16823         2757 :   readme.printf ("version: %s", string);
   16824              : 
   16825              :   /* Module information.  */
   16826         2757 :   readme.printf ("module: %s", get_flatname ());
   16827         2757 :   readme.printf ("source: %s", main_input_filename);
   16828         2757 :   readme.printf ("dialect: %s", dialect);
   16829         2757 :   if (extensions)
   16830           30 :     readme.printf ("extensions: %s%s%s",
   16831              :                    extensions & SE_OPENMP ? "-fopenmp"
   16832            6 :                    : extensions & SE_OPENMP_SIMD ? "-fopenmp-simd" : "",
   16833              :                    (extensions & SE_OPENACC)
   16834            3 :                    && (extensions & (SE_OPENMP | SE_OPENMP_SIMD))
   16835              :                    ? " " : "",
   16836           12 :                    extensions & SE_OPENACC ? "-fopenacc" : "");
   16837              : 
   16838              :   /* The following fields could be expected to change between
   16839              :      otherwise identical compilations.  Consider a distributed build
   16840              :      system.  We should have a way of overriding that.  */
   16841         2757 :   if (char *cwd = getcwd (NULL, 0))
   16842              :     {
   16843         2757 :       readme.printf ("cwd: %s", cwd);
   16844         2757 :       free (cwd);
   16845              :     }
   16846         5514 :   readme.printf ("repository: %s", cmi_repo ? cmi_repo : ".");
   16847              : #if NETWORKING
   16848              :   {
   16849              :     char hostname[64];
   16850              :     if (!gethostname (hostname, sizeof (hostname)))
   16851              :       readme.printf ("host: %s", hostname);
   16852              :   }
   16853              : #endif
   16854         2757 :   {
   16855              :     /* This of course will change!  */
   16856         2757 :     time_t stampy;
   16857         2757 :     auto kind = cpp_get_date (reader, &stampy);
   16858         2757 :     if (kind != CPP_time_kind::UNKNOWN)
   16859              :       {
   16860         2757 :         struct tm *time;
   16861              : 
   16862         2757 :         time = gmtime (&stampy);
   16863         2757 :         readme.print_time ("build", time, "UTC");
   16864              : 
   16865         2757 :         if (kind == CPP_time_kind::DYNAMIC)
   16866              :           {
   16867         2757 :             time = localtime (&stampy);
   16868         2757 :             readme.print_time ("local", time,
   16869              : #if defined (__USE_MISC) || defined (__USE_BSD) /* Is there a better way?  */
   16870              :                                time->tm_zone
   16871              : #else
   16872              :                                ""
   16873              : #endif
   16874              :                                );
   16875              :           }
   16876              :       }
   16877              :   }
   16878              : 
   16879              :   /* Its direct imports.  */
   16880         3425 :   for (unsigned ix = 1; ix < modules->length (); ix++)
   16881              :     {
   16882          668 :       module_state *state = (*modules)[ix];
   16883              : 
   16884          668 :       if (state->is_direct ())
   16885          981 :         readme.printf ("%s: %s %s", state->exported_p ? "export" : "import",
   16886              :                        state->get_flatname (), state->filename);
   16887              :     }
   16888              : 
   16889         2757 :   readme.end (to, to->name (MOD_SNAME_PFX ".README"), NULL);
   16890         2757 : }
   16891              : 
   16892              : /* Sort environment var names in reverse order.  */
   16893              : 
   16894              : static int
   16895            0 : env_var_cmp (const void *a_, const void *b_)
   16896              : {
   16897            0 :   const unsigned char *a = *(const unsigned char *const *)a_;
   16898            0 :   const unsigned char *b = *(const unsigned char *const *)b_;
   16899              : 
   16900            0 :   for (unsigned ix = 0; ; ix++)
   16901              :     {
   16902            0 :       bool a_end = !a[ix] || a[ix] == '=';
   16903            0 :       if (a[ix] == b[ix])
   16904              :         {
   16905            0 :           if (a_end)
   16906              :             break;
   16907              :         }
   16908              :       else
   16909              :         {
   16910            0 :           bool b_end = !b[ix] || b[ix] == '=';
   16911              : 
   16912            0 :           if (!a_end && !b_end)
   16913            0 :             return a[ix] < b[ix] ? +1 : -1;
   16914            0 :           if (a_end && b_end)
   16915              :             break;
   16916            0 :           return a_end ? +1 : -1;
   16917              :         }
   16918            0 :     }
   16919              : 
   16920              :   return 0;
   16921              : }
   16922              : 
   16923              : /* Write the environment. It is a STRTAB that may be extracted with:
   16924              :      readelf -pgnu.c++.ENV $(module).gcm */
   16925              : 
   16926              : void
   16927            0 : module_state::write_env (elf_out *to)
   16928              : {
   16929            0 :   vec<const char *> vars;
   16930            0 :   vars.create (20);
   16931              : 
   16932            0 :   extern char **environ;
   16933            0 :   while (const char *var = environ[vars.length ()])
   16934            0 :     vars.safe_push (var);
   16935            0 :   vars.qsort (env_var_cmp);
   16936              : 
   16937            0 :   bytes_out env (to);
   16938            0 :   env.begin (false);
   16939            0 :   while (vars.length ())
   16940            0 :     env.printf ("%s", vars.pop ());
   16941            0 :   env.end (to, to->name (MOD_SNAME_PFX ".ENV"), NULL);
   16942              : 
   16943            0 :   vars.release ();
   16944            0 : }
   16945              : 
   16946              : /* Write the direct or indirect imports.
   16947              :    u:N
   16948              :    {
   16949              :      u:index
   16950              :      s:name
   16951              :      u32:crc
   16952              :      s:filename (direct)
   16953              :      u:exported (direct)
   16954              :    } imports[N]
   16955              :  */
   16956              : 
   16957              : void
   16958          906 : module_state::write_imports (bytes_out &sec, bool direct)
   16959              : {
   16960          906 :   unsigned count = 0;
   16961              : 
   16962         1916 :   for (unsigned ix = 1; ix < modules->length (); ix++)
   16963              :     {
   16964         1010 :       module_state *imp = (*modules)[ix];
   16965              : 
   16966         1010 :       if (imp->remap && imp->is_direct () == direct)
   16967          484 :         count++;
   16968              :     }
   16969              : 
   16970          906 :   gcc_assert (!direct || count);
   16971              : 
   16972          906 :   sec.u (count);
   16973         1916 :   for (unsigned ix = 1; ix < modules->length (); ix++)
   16974              :     {
   16975         1010 :       module_state *imp = (*modules)[ix];
   16976              : 
   16977         1010 :       if (imp->remap && imp->is_direct () == direct)
   16978              :         {
   16979          646 :           dump () && dump ("Writing %simport:%u->%u %M (crc=%x)",
   16980              :                            !direct ? "indirect "
   16981           81 :                            : imp->exported_p ? "exported " : "",
   16982              :                            ix, imp->remap, imp, imp->crc);
   16983          484 :           sec.u (imp->remap);
   16984          484 :           sec.str (imp->get_flatname ());
   16985          484 :           sec.u32 (imp->crc);
   16986          484 :           if (direct)
   16987              :             {
   16988          475 :               write_location (sec, imp->imported_from ());
   16989          475 :               sec.str (imp->filename);
   16990          475 :               int exportedness = 0;
   16991          475 :               if (imp->exported_p)
   16992              :                 exportedness = +1;
   16993          281 :               else if (!imp->is_purview_direct ())
   16994           13 :                 exportedness = -1;
   16995          475 :               sec.i (exportedness);
   16996              :             }
   16997              :         }
   16998              :     }
   16999          906 : }
   17000              : 
   17001              : /* READER, LMAPS  != NULL == direct imports,
   17002              :    == NUL == indirect imports.  */
   17003              : 
   17004              : unsigned
   17005          768 : module_state::read_imports (bytes_in &sec, cpp_reader *reader, line_maps *lmaps)
   17006              : {
   17007          768 :   unsigned count = sec.u ();
   17008          768 :   unsigned loaded = 0;
   17009              : 
   17010         1942 :   while (count--)
   17011              :     {
   17012          406 :       unsigned ix = sec.u ();
   17013          406 :       if (ix >= slurp->remap->length () || !ix || (*slurp->remap)[ix])
   17014              :         {
   17015            0 :           sec.set_overrun ();
   17016            0 :           break;
   17017              :         }
   17018              : 
   17019          406 :       const char *name = sec.str (NULL);
   17020          406 :       module_state *imp = get_module (name);
   17021          406 :       unsigned crc = sec.u32 ();
   17022          406 :       int exportedness = 0;
   17023              : 
   17024              :       /* If the import is a partition, it must be the same primary
   17025              :          module as this TU.  */
   17026          406 :       if (imp && imp->is_partition () &&
   17027              :           (!named_module_p ()
   17028          135 :            || (get_primary (this_module ()) != get_primary (imp))))
   17029              :         imp = NULL;
   17030              : 
   17031          406 :       if (!imp)
   17032            0 :         sec.set_overrun ();
   17033          406 :       if (sec.get_overrun ())
   17034              :         break;
   17035              : 
   17036          406 :       if (lmaps)
   17037              :         {
   17038              :           /* A direct import, maybe load it.  */
   17039          402 :           location_t floc = read_location (sec);
   17040          402 :           const char *fname = sec.str (NULL);
   17041          402 :           exportedness = sec.i ();
   17042              : 
   17043          402 :           if (sec.get_overrun ())
   17044              :             break;
   17045              : 
   17046          402 :           if (!imp->check_circular_import (floc))
   17047            3 :             continue;
   17048              : 
   17049          399 :           if (imp->loadedness == ML_NONE)
   17050              :             {
   17051          315 :               imp->loc = floc;
   17052          315 :               imp->crc = crc;
   17053          315 :               if (!imp->get_flatname ())
   17054          272 :                 imp->set_flatname ();
   17055              : 
   17056          315 :               unsigned n = dump.push (imp);
   17057              : 
   17058          315 :               if (!imp->filename && fname)
   17059          272 :                 imp->filename = xstrdup (fname);
   17060              : 
   17061          315 :               if (imp->is_partition ())
   17062           33 :                 dump () && dump ("Importing elided partition %M", imp);
   17063              : 
   17064          315 :               if (!imp->do_import (reader, false))
   17065            3 :                 imp = NULL;
   17066          315 :               dump.pop (n);
   17067          315 :               if (!imp)
   17068            3 :                 continue;
   17069              :             }
   17070              : 
   17071          396 :           if (is_partition ())
   17072              :             {
   17073           66 :               if (!imp->is_direct () && !imp->is_partition_direct ())
   17074              :                 {
   17075           30 :                   imp->directness = MD_PARTITION_DIRECT;
   17076           30 :                   linemap_module_reparent (line_table, imp->loc, floc);
   17077              :                 }
   17078           66 :               if (exportedness > 0)
   17079            6 :                 imp->exported_p = true;
   17080              :             }
   17081              :         }
   17082              :       else
   17083              :         {
   17084              :           /* An indirect import, find it, it should already be here.  */
   17085            4 :           if (imp->loadedness == ML_NONE)
   17086              :             {
   17087            0 :               error_at (loc, "indirect import %qs is not already loaded", name);
   17088            0 :               continue;
   17089              :             }
   17090              :         }
   17091              : 
   17092          400 :       if (imp->crc != crc)
   17093            0 :         error_at (loc, "import %qs has CRC mismatch", imp->get_flatname ());
   17094              : 
   17095          400 :       (*slurp->remap)[ix] = (imp->mod << 1) | (lmaps != NULL);
   17096              : 
   17097          400 :       if (lmaps && exportedness >= 0)
   17098          382 :         set_import (imp, bool (exportedness));
   17099          580 :       dump () && dump ("Found %simport:%u %M->%u", !lmaps ? "indirect "
   17100           90 :                        : exportedness > 0 ? "exported "
   17101           51 :                        : exportedness < 0 ? "gmf" : "", ix, imp,
   17102              :                        imp->mod);
   17103          400 :       loaded++;
   17104              :     }
   17105              : 
   17106          768 :   return loaded;
   17107              : }
   17108              : 
   17109              : /* Write the import table to MOD_SNAME_PFX.imp.  */
   17110              : 
   17111              : void
   17112          453 : module_state::write_imports (elf_out *to, unsigned *crc_ptr)
   17113              : {
   17114          531 :   dump () && dump ("Writing imports");
   17115          453 :   dump.indent ();
   17116              : 
   17117          453 :   bytes_out sec (to);
   17118          453 :   sec.begin ();
   17119              : 
   17120          453 :   write_imports (sec, true);
   17121          453 :   write_imports (sec, false);
   17122              : 
   17123          453 :   sec.end (to, to->name (MOD_SNAME_PFX ".imp"), crc_ptr);
   17124          453 :   dump.outdent ();
   17125          453 : }
   17126              : 
   17127              : bool
   17128          384 : module_state::read_imports (cpp_reader *reader, line_maps *lmaps)
   17129              : {
   17130          384 :   bytes_in sec;
   17131              : 
   17132          384 :   if (!sec.begin (loc, from (), MOD_SNAME_PFX ".imp"))
   17133              :     return false;
   17134              : 
   17135          471 :   dump () && dump ("Reading %u imports", slurp->remap->length () - 1);
   17136          384 :   dump.indent ();
   17137              : 
   17138              :   /* Read the imports.  */
   17139          384 :   unsigned direct = read_imports (sec, reader, lmaps);
   17140          384 :   unsigned indirect = read_imports (sec, NULL, NULL);
   17141          384 :   if (direct + indirect + 1 != slurp->remap->length ())
   17142            6 :     from ()->set_error (elf::E_BAD_IMPORT);
   17143              : 
   17144          384 :   dump.outdent ();
   17145          384 :   if (!sec.end (from ()))
   17146              :     return false;
   17147              :   return true;
   17148          384 : }
   17149              : 
   17150              : /* We're the primary module interface, but have partitions.  Document
   17151              :    them so that non-partition module implementation units know which
   17152              :    have already been loaded.  */
   17153              : 
   17154              : void
   17155          133 : module_state::write_partitions (elf_out *to, unsigned count, unsigned *crc_ptr)
   17156              : {
   17157          157 :   dump () && dump ("Writing %u elided partitions", count);
   17158          133 :   dump.indent ();
   17159              : 
   17160          133 :   bytes_out sec (to);
   17161          133 :   sec.begin ();
   17162              : 
   17163          341 :   for (unsigned ix = 1; ix != modules->length (); ix++)
   17164              :     {
   17165          208 :       module_state *imp = (*modules)[ix];
   17166          208 :       if (imp->is_partition ())
   17167              :         {
   17168          223 :           dump () && dump ("Writing elided partition %M (crc=%x)",
   17169              :                            imp, imp->crc);
   17170          184 :           sec.str (imp->get_flatname ());
   17171          184 :           sec.u32 (imp->crc);
   17172          359 :           write_location (sec, imp->is_direct ()
   17173          175 :                           ? imp->imported_from () : UNKNOWN_LOCATION);
   17174          184 :           sec.str (imp->filename);
   17175              :         }
   17176              :     }
   17177              : 
   17178          133 :   sec.end (to, to->name (MOD_SNAME_PFX ".prt"), crc_ptr);
   17179          133 :   dump.outdent ();
   17180          133 : }
   17181              : 
   17182              : bool
   17183           27 : module_state::read_partitions (unsigned count)
   17184              : {
   17185           27 :   bytes_in sec;
   17186           27 :   if (!sec.begin (loc, from (), MOD_SNAME_PFX ".prt"))
   17187              :     return false;
   17188              : 
   17189           33 :   dump () && dump ("Reading %u elided partitions", count);
   17190           27 :   dump.indent ();
   17191              : 
   17192           66 :   while (count--)
   17193              :     {
   17194           39 :       const char *name = sec.str (NULL);
   17195           39 :       unsigned crc = sec.u32 ();
   17196           39 :       location_t floc = read_location (sec);
   17197           39 :       const char *fname = sec.str (NULL);
   17198              : 
   17199           39 :       if (sec.get_overrun ())
   17200              :         break;
   17201              : 
   17202           48 :       dump () && dump ("Reading elided partition %s (crc=%x)", name, crc);
   17203              : 
   17204           39 :       module_state *imp = get_module (name);
   17205           39 :       if (!imp  /* Partition should be ...  */
   17206           39 :           || !imp->is_partition () /* a partition ...  */
   17207           39 :           || imp->loadedness != ML_NONE  /* that is not yet loaded ...  */
   17208           78 :           || get_primary (imp) != this) /* whose primary is this.  */
   17209              :         {
   17210            0 :           sec.set_overrun ();
   17211            0 :           break;
   17212              :         }
   17213              : 
   17214           39 :       if (!imp->has_location ())
   17215           30 :         imp->loc = floc;
   17216           39 :       imp->crc = crc;
   17217           39 :       if (!imp->filename && fname[0])
   17218           30 :         imp->filename = xstrdup (fname);
   17219              :     }
   17220              : 
   17221           27 :   dump.outdent ();
   17222           27 :   if (!sec.end (from ()))
   17223              :     return false;
   17224              :   return true;
   17225           27 : }
   17226              : 
   17227              : /* Data for config reading and writing.  */
   17228              : struct module_state_config {
   17229              :   const char *dialect_str = get_dialect ();
   17230              :   line_map_uint_t ordinary_locs = 0;
   17231              :   line_map_uint_t macro_locs = 0;
   17232              :   unsigned num_imports = 0;
   17233              :   unsigned num_partitions = 0;
   17234              :   unsigned num_entities = 0;
   17235              :   unsigned loc_range_bits = 0;
   17236              :   unsigned active_init = 0;
   17237              : 
   17238        98071 :   static void release ()
   17239              :   {
   17240        98071 :     XDELETEVEC (dialect);
   17241        98071 :     dialect = NULL;
   17242              :   }
   17243              : 
   17244              : private:
   17245              :   static const char *get_dialect ();
   17246              :   static char *dialect;
   17247              : };
   17248              : 
   17249              : char *module_state_config::dialect;
   17250              : 
   17251              : /* Generate a string of the significant compilation options.
   17252              :    Generally assume the user knows what they're doing, in the same way
   17253              :    that object files can be mixed.  */
   17254              : 
   17255              : const char *
   17256         5954 : module_state_config::get_dialect ()
   17257              : {
   17258         5954 :   if (!dialect)
   17259         9428 :     dialect = concat (get_cxx_dialect_name (cxx_dialect),
   17260              :                       /* C++ implies these, only show if disabled.  */
   17261         4714 :                       flag_exceptions ? "" : "/no-exceptions",
   17262         4714 :                       flag_rtti ? "" : "/no-rtti",
   17263         4714 :                       flag_new_inheriting_ctors ? "" : "/old-inheriting-ctors",
   17264              :                       /* C++ 20 implies concepts and coroutines.  */
   17265         1473 :                       cxx_dialect < cxx20 && flag_concepts ? "/concepts" : "",
   17266         4714 :                       (cxx_dialect < cxx20 && flag_coroutines
   17267              :                        ? "/coroutines" : ""),
   17268         4714 :                       flag_module_implicit_inline ? "/implicit-inline" : "",
   17269         4714 :                       flag_contracts ? "/contracts" : "",
   17270         4714 :                       flag_reflection ? "/reflection" : "",
   17271              :                       NULL);
   17272              : 
   17273         5954 :   return dialect;
   17274              : }
   17275              : 
   17276              : /* Contents of a cluster.  */
   17277              : enum cluster_tag {
   17278              :   ct_decl,      /* A decl.  */
   17279              :   ct_defn,      /* A definition.  */
   17280              :   ct_bind,      /* A binding.  */
   17281              :   ct_hwm
   17282              : };
   17283              : 
   17284              : /* Binding modifiers.  */
   17285              : enum ct_bind_flags
   17286              : {
   17287              :   cbf_export = 0x1,     /* An exported decl.  */
   17288              :   cbf_hidden = 0x2,     /* A hidden (friend) decl.  */
   17289              :   cbf_using = 0x4,      /* A using decl.  */
   17290              :   cbf_internal = 0x8,   /* A TU-local decl.  */
   17291              : };
   17292              : 
   17293              : /* DEP belongs to a different cluster, seed it to prevent
   17294              :    unfortunately timed duplicate import.  */
   17295              : // FIXME: QOI For inter-cluster references we could just only pick
   17296              : // one entity from an earlier cluster.  Even better track
   17297              : // dependencies between earlier clusters
   17298              : 
   17299              : void
   17300      8759326 : module_state::intercluster_seed (trees_out &sec, unsigned index_hwm, depset *dep)
   17301              : {
   17302      8759326 :   if (dep->is_tu_local ())
   17303              :     /* We only stream placeholders for TU-local entities anyway.  */;
   17304      8759117 :   else if (dep->is_import () || dep->cluster < index_hwm)
   17305              :     {
   17306      3869412 :       tree ent = dep->get_entity ();
   17307      3869412 :       if (!TREE_VISITED (ent))
   17308              :         {
   17309      1620977 :           sec.tree_node (ent);
   17310      1621427 :           dump (dumper::CLUSTER)
   17311          450 :             && dump ("Seeded %s %N",
   17312          450 :                      dep->is_import () ? "import" : "intercluster", ent);
   17313              :         }
   17314              :     }
   17315      8759326 : }
   17316              : 
   17317              : /* Write the cluster of depsets in SCC[0-SIZE).
   17318              :    dep->section -> section number
   17319              :    dep->cluster -> entity number
   17320              :  */
   17321              : 
   17322              : unsigned
   17323       311607 : module_state::write_cluster (elf_out *to, depset *scc[], unsigned size,
   17324              :                              depset::hash &table, unsigned *counts,
   17325              :                              unsigned *crc_ptr)
   17326              : {
   17327       313141 :   dump () && dump ("Writing section:%u %u depsets", table.section, size);
   17328       311607 :   dump.indent ();
   17329              : 
   17330       311607 :   trees_out sec (to, this, table, table.section);
   17331       311607 :   sec.begin ();
   17332       311607 :   unsigned index_lwm = counts[MSC_entities];
   17333              : 
   17334              :   /* Determine entity numbers, mark for writing.   */
   17335       311916 :   dump (dumper::CLUSTER) && dump ("Cluster members:") && (dump.indent (), true);
   17336      1701965 :   for (unsigned ix = 0; ix != size; ix++)
   17337              :     {
   17338      1390358 :       depset *b = scc[ix];
   17339              : 
   17340      1390358 :       switch (b->get_entity_kind ())
   17341              :         {
   17342            0 :         default:
   17343            0 :           gcc_unreachable ();
   17344              : 
   17345       157446 :         case depset::EK_BINDING:
   17346       157446 :           {
   17347       157446 :             dump (dumper::CLUSTER)
   17348          210 :               && dump ("[%u]=%s %P", ix, b->entity_kind_name (),
   17349              :                        b->get_entity (), b->get_name ());
   17350       157446 :             depset *ns_dep = b->deps[0];
   17351       157446 :             gcc_checking_assert (ns_dep->get_entity_kind ()
   17352              :                                  == depset::EK_NAMESPACE
   17353              :                                  && ns_dep->get_entity () == b->get_entity ());
   17354       374666 :             for (unsigned jx = b->deps.length (); --jx;)
   17355              :               {
   17356       217220 :                 depset *dep = b->deps[jx];
   17357              :                 // We could be declaring something that is also a
   17358              :                 // (merged) import
   17359       256627 :                 gcc_checking_assert (dep->is_import ()
   17360              :                                      || TREE_VISITED (dep->get_entity ())
   17361              :                                      || (dep->get_entity_kind ()
   17362              :                                          == depset::EK_USING)
   17363              :                                      || (dep->get_entity_kind ()
   17364              :                                          == depset::EK_TU_LOCAL));
   17365              :               }
   17366              :           }
   17367              :           break;
   17368              : 
   17369      1193553 :         case depset::EK_DECL:
   17370      1193553 :         case depset::EK_SPECIALIZATION:
   17371      1193553 :         case depset::EK_PARTIAL:
   17372      1193553 :           b->cluster = counts[MSC_entities]++;
   17373      1193553 :           sec.mark_declaration (b->get_entity (), b->has_defn ());
   17374              :           /* FALLTHROUGH  */
   17375              : 
   17376      1232912 :         case depset::EK_USING:
   17377      1232912 :         case depset::EK_TU_LOCAL:
   17378      2465824 :           gcc_checking_assert (!b->is_import ()
   17379              :                                && !b->is_unreached ());
   17380      1393532 :           dump (dumper::CLUSTER)
   17381         1161 :             && dump ("[%u]=%s %s %N", ix, b->entity_kind_name (),
   17382          729 :                      b->has_defn () ? "definition" : "declaration",
   17383              :                      b->get_entity ());
   17384              :           break;
   17385              :         }
   17386              :     }
   17387       311916 :   dump (dumper::CLUSTER) && (dump.outdent (), true);
   17388              : 
   17389              :   /* Ensure every out-of-cluster decl is referenced before we start
   17390              :      streaming.  We must do both imports *and* earlier clusters,
   17391              :      because the latter could reach into the former and cause a
   17392              :      duplicate loop.   */
   17393       311607 :   sec.set_importing (+1);
   17394      1701965 :   for (unsigned ix = 0; ix != size; ix++)
   17395              :     {
   17396      1390358 :       depset *b = scc[ix];
   17397     17611030 :       for (unsigned jx = b->is_special (); jx != b->deps.length (); jx++)
   17398              :         {
   17399      7419080 :           depset *dep = b->deps[jx];
   17400              : 
   17401      7419080 :           if (dep->is_binding ())
   17402              :             {
   17403      1774590 :               for (unsigned ix = dep->deps.length (); --ix;)
   17404              :                 {
   17405      1340246 :                   depset *bind = dep->deps[ix];
   17406      1340246 :                   if (bind->get_entity_kind () == depset::EK_USING)
   17407       467090 :                     bind = bind->deps[1];
   17408              : 
   17409      1340246 :                   intercluster_seed (sec, index_lwm, bind);
   17410              :                 }
   17411              :               /* Also check the namespace itself.  */
   17412       217172 :               dep = dep->deps[0];
   17413              :             }
   17414              : 
   17415      7419080 :           intercluster_seed (sec, index_lwm, dep);
   17416              :         }
   17417              :     }
   17418       311607 :   sec.tree_node (NULL_TREE);
   17419              :   /* We're done importing now.  */
   17420       311607 :   sec.set_importing (-1);
   17421              : 
   17422              :   /* Write non-definitions.  */
   17423      1701965 :   for (unsigned ix = 0; ix != size; ix++)
   17424              :     {
   17425      1390358 :       depset *b = scc[ix];
   17426      1390358 :       tree decl = b->get_entity ();
   17427      1390358 :       switch (b->get_entity_kind ())
   17428              :         {
   17429            0 :         default:
   17430            0 :           gcc_unreachable ();
   17431       157446 :           break;
   17432              : 
   17433       157446 :         case depset::EK_BINDING:
   17434       157446 :           {
   17435       157446 :             gcc_assert (TREE_CODE (decl) == NAMESPACE_DECL);
   17436       158644 :             dump () && dump ("Depset:%u binding %C:%P", ix, TREE_CODE (decl),
   17437              :                              decl, b->get_name ());
   17438       157446 :             sec.u (ct_bind);
   17439       157446 :             sec.tree_node (decl);
   17440       157446 :             sec.tree_node (b->get_name ());
   17441              : 
   17442              :             /* Write in reverse order, so reading will see the exports
   17443              :                first, thus building the overload chain will be
   17444              :                optimized.  */
   17445       532112 :             for (unsigned jx = b->deps.length (); --jx;)
   17446              :               {
   17447       217220 :                 depset *dep = b->deps[jx];
   17448       217220 :                 tree bound = dep->get_entity ();
   17449       217220 :                 unsigned flags = 0;
   17450       217220 :                 if (dep->get_entity_kind () == depset::EK_TU_LOCAL)
   17451              :                   flags |= cbf_internal;
   17452       217172 :                 else if (dep->get_entity_kind () == depset::EK_USING)
   17453              :                   {
   17454        39359 :                     tree ovl = bound;
   17455        39359 :                     bound = OVL_FUNCTION (bound);
   17456        39359 :                     if (!(TREE_CODE (bound) == CONST_DECL
   17457         4916 :                           && UNSCOPED_ENUM_P (TREE_TYPE (bound))
   17458         4118 :                           && decl == TYPE_NAME (TREE_TYPE (bound))))
   17459              :                       /* An unscoped enumerator in its enumeration's
   17460              :                          scope is not a using.  */
   17461              :                       flags |= cbf_using;
   17462        39359 :                     if (OVL_EXPORT_P (ovl))
   17463        38272 :                       flags |= cbf_export;
   17464              :                   }
   17465              :                 else
   17466              :                   {
   17467              :                     /* An implicit typedef must be at one.  */
   17468       177813 :                     gcc_assert (!DECL_IMPLICIT_TYPEDEF_P (bound) || jx == 1);
   17469       177813 :                     if (dep->is_hidden ())
   17470              :                       flags |= cbf_hidden;
   17471       177724 :                     else if (DECL_MODULE_EXPORT_P (STRIP_TEMPLATE (bound)))
   17472       141608 :                       flags |= cbf_export;
   17473              :                   }
   17474              : 
   17475       217220 :                 gcc_checking_assert (DECL_P (bound));
   17476              : 
   17477       217220 :                 sec.i (flags);
   17478       217220 :                 if (flags & cbf_internal)
   17479              :                   {
   17480           48 :                     sec.tree_node (name_for_tu_local_decl (bound));
   17481           48 :                     write_location (sec, DECL_SOURCE_LOCATION (bound));
   17482              :                   }
   17483              :                 else
   17484       217172 :                   sec.tree_node (bound);
   17485              :               }
   17486              : 
   17487              :             /* Terminate the list.  */
   17488       157446 :             sec.i (-1);
   17489              :           }
   17490       157446 :           break;
   17491              : 
   17492        39359 :         case depset::EK_USING:
   17493        39359 :         case depset::EK_TU_LOCAL:
   17494        39386 :           dump () && dump ("Depset:%u %s %C:%N", ix, b->entity_kind_name (),
   17495           27 :                            TREE_CODE (decl), decl);
   17496              :           break;
   17497              : 
   17498      1193553 :         case depset::EK_SPECIALIZATION:
   17499      1193553 :         case depset::EK_PARTIAL:
   17500      1193553 :         case depset::EK_DECL:
   17501      1196700 :           dump () && dump ("Depset:%u %s entity:%u %C:%N", ix,
   17502              :                            b->entity_kind_name (), b->cluster,
   17503         3147 :                            TREE_CODE (decl), decl);
   17504              : 
   17505      1193553 :           sec.u (ct_decl);
   17506      1193553 :           sec.tree_node (decl);
   17507              : 
   17508      1393505 :           dump () && dump ("Wrote declaration entity:%u %C:%N",
   17509         3147 :                            b->cluster, TREE_CODE (decl), decl);
   17510              :           break;
   17511              :         }
   17512              :     }
   17513              : 
   17514              :   depset *namer = NULL;
   17515              : 
   17516              :   /* Write out definitions  */
   17517      1701965 :   for (unsigned ix = 0; ix != size; ix++)
   17518              :     {
   17519      1390358 :       depset *b = scc[ix];
   17520      1390358 :       tree decl = b->get_entity ();
   17521      1390358 :       switch (b->get_entity_kind ())
   17522              :         {
   17523              :         default:
   17524              :           break;
   17525              : 
   17526      1193553 :         case depset::EK_SPECIALIZATION:
   17527      1193553 :         case depset::EK_PARTIAL:
   17528      1193553 :         case depset::EK_DECL:
   17529      1193553 :           if (!namer)
   17530       296154 :             namer = b;
   17531              : 
   17532      1193553 :           if (b->has_defn ())
   17533              :             {
   17534       460507 :               sec.u (ct_defn);
   17535       460507 :               sec.tree_node (decl);
   17536       461450 :               dump () && dump ("Writing definition %N", decl);
   17537       460507 :               sec.write_definition (decl, b->refs_tu_local ());
   17538              : 
   17539       460507 :               if (!namer->has_defn ())
   17540      1390358 :                 namer = b;
   17541              :             }
   17542              :           break;
   17543              :         }
   17544              :     }
   17545              : 
   17546              :   /* We don't find the section by name.  Use depset's decl's name for
   17547              :      human friendliness.  */
   17548       311607 :   unsigned name = 0;
   17549       311607 :   tree naming_decl = NULL_TREE;
   17550       311607 :   if (namer)
   17551              :     {
   17552       296154 :       naming_decl = namer->get_entity ();
   17553       296154 :       if (namer->get_entity_kind () == depset::EK_USING)
   17554              :         /* This unfortunately names the section from the target of the
   17555              :            using decl.  But the name is only a guide, so Do Not Care.  */
   17556            0 :         naming_decl = OVL_FUNCTION (naming_decl);
   17557       296154 :       if (DECL_IMPLICIT_TYPEDEF_P (naming_decl))
   17558              :         /* Lose any anonymousness.  */
   17559       104495 :         naming_decl = TYPE_NAME (TREE_TYPE (naming_decl));
   17560       296154 :       name = to->qualified_name (naming_decl, namer->has_defn ());
   17561              :     }
   17562              : 
   17563       311607 :   unsigned bytes = sec.pos;
   17564       311607 :   unsigned snum = sec.end (to, name, crc_ptr);
   17565              : 
   17566      1701965 :   for (unsigned ix = size; ix--;)
   17567      1390358 :     gcc_checking_assert (scc[ix]->section == snum);
   17568              : 
   17569       311607 :   dump.outdent ();
   17570       313141 :   dump () && dump ("Wrote section:%u named-by:%N", table.section, naming_decl);
   17571              : 
   17572       311607 :   return bytes;
   17573       311607 : }
   17574              : 
   17575              : /* Read a cluster from section SNUM.  */
   17576              : 
   17577              : bool
   17578       211778 : module_state::read_cluster (unsigned snum)
   17579              : {
   17580       211778 :   trees_in sec (this);
   17581              : 
   17582       211778 :   if (!sec.begin (loc, from (), snum))
   17583              :     return false;
   17584              : 
   17585       213029 :   dump () && dump ("Reading section:%u", snum);
   17586       211778 :   dump.indent ();
   17587              : 
   17588              :   /* We care about structural equality.  */
   17589       211778 :   comparing_dependent_aliases++;
   17590              : 
   17591              :   /* First seed the imports.  */
   17592      1310098 :   while (tree import = sec.tree_node ())
   17593      2408604 :     dump (dumper::CLUSTER) && dump ("Seeded import %N", import);
   17594              : 
   17595      1556298 :   while (!sec.get_overrun () && sec.more_p ())
   17596              :     {
   17597      1344520 :       unsigned ct = sec.u ();
   17598      1344520 :       switch (ct)
   17599              :         {
   17600            0 :         default:
   17601            0 :           sec.set_overrun ();
   17602            0 :           break;
   17603              : 
   17604       109928 :         case ct_bind:
   17605              :           /* A set of namespace bindings.  */
   17606       109928 :           {
   17607       109928 :             tree ns = sec.tree_node ();
   17608       109928 :             tree name = sec.tree_node ();
   17609       109928 :             tree decls = NULL_TREE;
   17610       109928 :             tree visible = NULL_TREE;
   17611       109928 :             tree internal = NULL_TREE;
   17612       109928 :             tree type = NULL_TREE;
   17613       109928 :             bool dedup = false;
   17614       109928 :             bool global_p = is_header ();
   17615              : 
   17616              :             /* We rely on the bindings being in the reverse order of
   17617              :                the resulting overload set.  */
   17618       258196 :             for (;;)
   17619              :               {
   17620       258196 :                 int flags = sec.i ();
   17621       258196 :                 if (flags < 0)
   17622              :                   break;
   17623              : 
   17624       148268 :                 if ((flags & cbf_hidden)
   17625           65 :                     && (flags & (cbf_using | cbf_export)))
   17626            0 :                   sec.set_overrun ();
   17627       148268 :                 if ((flags & cbf_internal)
   17628           15 :                     && flags != cbf_internal)
   17629            0 :                   sec.set_overrun ();
   17630              : 
   17631            0 :                 if (flags & cbf_internal)
   17632              :                   {
   17633           15 :                     tree name = sec.tree_node ();
   17634           15 :                     location_t loc = read_location (sec);
   17635           15 :                     if (sec.get_overrun ())
   17636              :                       break;
   17637              : 
   17638           15 :                     tree decl = make_node (TU_LOCAL_ENTITY);
   17639           15 :                     TU_LOCAL_ENTITY_NAME (decl) = name;
   17640           15 :                     TU_LOCAL_ENTITY_LOCATION (decl) = loc;
   17641           15 :                     internal = tree_cons (NULL_TREE, decl, internal);
   17642           15 :                     continue;
   17643           15 :                   }
   17644              : 
   17645       148253 :                 tree decl = sec.tree_node ();
   17646       148253 :                 if (sec.get_overrun ())
   17647              :                   break;
   17648              : 
   17649       148253 :                 if (!global_p)
   17650              :                   {
   17651              :                     /* Check if the decl could require GM merging.  */
   17652         8898 :                     tree orig = get_originating_module_decl (decl);
   17653         8898 :                     tree inner = STRIP_TEMPLATE (orig);
   17654         8898 :                     if (!DECL_LANG_SPECIFIC (inner)
   17655        17796 :                         || !DECL_MODULE_ATTACH_P (inner))
   17656              :                       global_p = true;
   17657              :                   }
   17658              : 
   17659       148253 :                 if (decls && TREE_CODE (decl) == TYPE_DECL)
   17660              :                   {
   17661              :                     /* Stat hack.  */
   17662           54 :                     if (type || !DECL_IMPLICIT_TYPEDEF_P (decl))
   17663            0 :                       sec.set_overrun ();
   17664              : 
   17665           54 :                     if (flags & cbf_using)
   17666              :                       {
   17667            3 :                         type = build_lang_decl_loc (UNKNOWN_LOCATION,
   17668              :                                                     USING_DECL,
   17669            3 :                                                     DECL_NAME (decl),
   17670              :                                                     NULL_TREE);
   17671            3 :                         USING_DECL_DECLS (type) = decl;
   17672            3 :                         USING_DECL_SCOPE (type) = CP_DECL_CONTEXT (decl);
   17673            3 :                         DECL_CONTEXT (type) = ns;
   17674              : 
   17675            3 :                         DECL_MODULE_PURVIEW_P (type) = true;
   17676            3 :                         if (flags & cbf_export)
   17677            3 :                           DECL_MODULE_EXPORT_P (type) = true;
   17678              :                       }
   17679              :                     else
   17680              :                       type = decl;
   17681              :                   }
   17682              :                 else
   17683              :                   {
   17684       148199 :                     if ((flags & cbf_using) &&
   17685        17249 :                         !DECL_DECLARES_FUNCTION_P (decl))
   17686              :                       {
   17687              :                         /* We should only see a single non-function using-decl
   17688              :                            for a binding; more than that would clash.  */
   17689         4810 :                         if (decls)
   17690            0 :                           sec.set_overrun ();
   17691              : 
   17692              :                         /* FIXME: Propagate the location of the using-decl
   17693              :                            for use in diagnostics.  */
   17694         4810 :                         decls = build_lang_decl_loc (UNKNOWN_LOCATION,
   17695              :                                                      USING_DECL,
   17696         4810 :                                                      DECL_NAME (decl),
   17697              :                                                      NULL_TREE);
   17698         4810 :                         USING_DECL_DECLS (decls) = decl;
   17699              :                         /* We don't currently record the actual scope of the
   17700              :                            using-declaration, but this approximation should
   17701              :                            generally be good enough.  */
   17702         4810 :                         USING_DECL_SCOPE (decls) = CP_DECL_CONTEXT (decl);
   17703         4810 :                         DECL_CONTEXT (decls) = ns;
   17704              : 
   17705         4810 :                         DECL_MODULE_PURVIEW_P (decls) = true;
   17706         4810 :                         if (flags & cbf_export)
   17707         4798 :                           DECL_MODULE_EXPORT_P (decls) = true;
   17708              :                       }
   17709       143389 :                     else if (decls
   17710       105103 :                              || (flags & (cbf_hidden | cbf_using))
   17711       242148 :                              || DECL_FUNCTION_TEMPLATE_P (decl))
   17712              :                       {
   17713        60595 :                         decls = ovl_make (decl, decls);
   17714        60595 :                         if (flags & cbf_using)
   17715              :                           {
   17716        12439 :                             dedup = true;
   17717        12439 :                             OVL_USING_P (decls) = true;
   17718        12439 :                             OVL_PURVIEW_P (decls) = true;
   17719        12439 :                             if (flags & cbf_export)
   17720        12424 :                               OVL_EXPORT_P (decls) = true;
   17721              :                           }
   17722              : 
   17723        60595 :                         if (flags & cbf_hidden)
   17724           65 :                           OVL_HIDDEN_P (decls) = true;
   17725        60530 :                         else if (dedup)
   17726        15531 :                           OVL_DEDUP_P (decls) = true;
   17727              :                       }
   17728              :                     else
   17729              :                       decls = decl;
   17730              : 
   17731       148187 :                     if (flags & cbf_export
   17732       148199 :                         || (!(flags & cbf_hidden)
   17733         8881 :                             && (is_module () || is_partition ())))
   17734              :                       visible = decls;
   17735              :                   }
   17736              :               }
   17737              : 
   17738       109928 :             if (!decls && !internal)
   17739            0 :               sec.set_overrun ();
   17740              : 
   17741       109928 :             if (sec.get_overrun ())
   17742              :               break; /* Bail.  */
   17743              : 
   17744       110801 :             dump () && dump ("Binding of %P", ns, name);
   17745       109928 :             if (!set_module_binding (ns, name, mod, global_p,
   17746       109928 :                                      is_module () || is_partition (),
   17747              :                                      decls, type, visible, internal))
   17748            0 :               sec.set_overrun ();
   17749              :           }
   17750              :           break;
   17751              : 
   17752       890665 :         case ct_decl:
   17753              :           /* A decl.  */
   17754       890665 :           {
   17755       890665 :             tree decl = sec.tree_node ();
   17756      2450657 :             dump () && dump ("Read declaration of %N", decl);
   17757              :           }
   17758              :           break;
   17759              : 
   17760       343927 :         case ct_defn:
   17761       343927 :           {
   17762       343927 :             tree decl = sec.tree_node ();
   17763       345232 :             dump () && dump ("Reading definition of %N", decl);
   17764       343927 :             sec.read_definition (decl);
   17765              :           }
   17766       343927 :           break;
   17767              :         }
   17768              :     }
   17769              : 
   17770              :   /* When lazy loading is in effect, we can be in the middle of
   17771              :      parsing or instantiating a function.  Save it away.
   17772              :      push_function_context does too much work.   */
   17773       211778 :   tree old_cfd = current_function_decl;
   17774       211778 :   struct function *old_cfun = cfun;
   17775       400814 :   for (const post_process_data& pdata : sec.post_process ())
   17776              :     {
   17777       143868 :       tree decl = pdata.decl;
   17778              : 
   17779       143868 :       bool abstract = false;
   17780       143868 :       if (TREE_CODE (decl) == TEMPLATE_DECL)
   17781              :         {
   17782        89943 :           abstract = true;
   17783        89943 :           decl = DECL_TEMPLATE_RESULT (decl);
   17784              :         }
   17785              : 
   17786       143868 :       current_function_decl = decl;
   17787       143868 :       allocate_struct_function (decl, abstract);
   17788       143868 :       cfun->language = ggc_cleared_alloc<language_function> ();
   17789       143868 :       cfun->language->base.x_stmt_tree.stmts_are_full_exprs_p = 1;
   17790       143868 :       cfun->function_start_locus = pdata.start_locus;
   17791       143868 :       cfun->function_end_locus = pdata.end_locus;
   17792       143868 :       cfun->language->returns_value = pdata.returns_value;
   17793       143868 :       cfun->language->returns_null = pdata.returns_null;
   17794       143868 :       cfun->language->returns_abnormally = pdata.returns_abnormally;
   17795       143868 :       cfun->language->infinite_loop = pdata.infinite_loop;
   17796       143868 :       cfun->coroutine_component = DECL_COROUTINE_P (decl);
   17797              : 
   17798              :       /* Make sure we emit explicit instantiations.
   17799              :          FIXME do we want to do this in expand_or_defer_fn instead?  */
   17800       143868 :       if (DECL_EXPLICIT_INSTANTIATION (decl)
   17801       143868 :           && !DECL_EXTERNAL (decl))
   17802           27 :         setup_explicit_instantiation_definition_linkage (decl);
   17803              : 
   17804       143868 :       if (abstract)
   17805              :         ;
   17806        53925 :       else if (DECL_MAYBE_IN_CHARGE_CDTOR_P (decl))
   17807         9076 :         vec_safe_push (post_load_decls, decl);
   17808              :       else
   17809              :         {
   17810        44849 :           bool aggr = aggregate_value_p (DECL_RESULT (decl), decl);
   17811              : #ifdef PCC_STATIC_STRUCT_RETURN
   17812              :           cfun->returns_pcc_struct = aggr;
   17813              : #endif
   17814        44849 :           cfun->returns_struct = aggr;
   17815        44849 :           expand_or_defer_fn (decl);
   17816              : 
   17817              :           /* If we first see this function after at_eof, it doesn't get
   17818              :              note_vague_linkage_fn from tentative_decl_linkage, so the loop in
   17819              :              c_parse_final_cleanups won't consider it.  But with DECL_COMDAT we
   17820              :              can just clear DECL_EXTERNAL and let cgraph decide.
   17821              :              FIXME handle this outside module.cc after GCC 15.  */
   17822         4891 :           if (at_eof && DECL_COMDAT (decl) && DECL_EXTERNAL (decl)
   17823        46830 :               && DECL_NOT_REALLY_EXTERN (decl))
   17824         1938 :             DECL_EXTERNAL (decl) = false;
   17825              :         }
   17826              : 
   17827              :     }
   17828       211854 :   for (const tree& type : sec.post_process_type ())
   17829              :     {
   17830              :       /* Attempt to complete an array type now in case its element type
   17831              :          had a definition streamed later in the cluster.  */
   17832           36 :       gcc_checking_assert (TREE_CODE (type) == ARRAY_TYPE);
   17833           36 :       complete_type (type);
   17834              :     }
   17835       211778 :   set_cfun (old_cfun);
   17836       211778 :   current_function_decl = old_cfd;
   17837       211778 :   comparing_dependent_aliases--;
   17838              : 
   17839       211778 :   dump.outdent ();
   17840       213029 :   dump () && dump ("Read section:%u", snum);
   17841              : 
   17842       211778 :   loaded_clusters++;
   17843              : 
   17844       211778 :   if (!sec.end (from ()))
   17845              :     return false;
   17846              : 
   17847              :   return true;
   17848       211778 : }
   17849              : 
   17850              : void
   17851       160363 : module_state::write_namespace (bytes_out &sec, depset *dep)
   17852              : {
   17853       160363 :   unsigned ns_num = dep->cluster;
   17854       160363 :   unsigned ns_import = 0;
   17855              : 
   17856       160363 :   if (dep->is_import ())
   17857            0 :     ns_import = dep->section;
   17858       160363 :   else if (dep->get_entity () != global_namespace)
   17859       105154 :     ns_num++;
   17860              : 
   17861       160363 :   sec.u (ns_import);
   17862       160363 :   sec.u (ns_num);
   17863       160363 : }
   17864              : 
   17865              : tree
   17866       198624 : module_state::read_namespace (bytes_in &sec)
   17867              : {
   17868       198624 :   unsigned ns_import = sec.u ();
   17869       198624 :   unsigned ns_num = sec.u ();
   17870       198624 :   tree ns = NULL_TREE;
   17871              : 
   17872       198624 :   if (ns_import || ns_num)
   17873              :     {
   17874       126845 :       if (!ns_import)
   17875       126845 :         ns_num--;
   17876              : 
   17877       126845 :       if (unsigned origin = slurp->remap_module (ns_import))
   17878              :         {
   17879       126845 :           module_state *from = (*modules)[origin];
   17880       126845 :           if (ns_num < from->entity_num)
   17881              :             {
   17882       126845 :               binding_slot &slot = (*entity_ary)[from->entity_lwm + ns_num];
   17883              : 
   17884       126845 :               if (!slot.is_lazy ())
   17885       126845 :                 ns = slot;
   17886              :             }
   17887              :         }
   17888              :       else
   17889            0 :         sec.set_overrun ();
   17890              :     }
   17891              :   else
   17892        71779 :     ns = global_namespace;
   17893              : 
   17894       198624 :   return ns;
   17895              : }
   17896              : 
   17897              : /* SPACES is a sorted vector of namespaces.  Write out the namespaces
   17898              :    to MOD_SNAME_PFX.nms section.   */
   17899              : 
   17900              : void
   17901          587 : module_state::write_namespaces (elf_out *to, vec<depset *> spaces,
   17902              :                                 unsigned num, unsigned *crc_p)
   17903              : {
   17904          645 :   dump () && dump ("Writing namespaces");
   17905          587 :   dump.indent ();
   17906              : 
   17907          587 :   bytes_out sec (to);
   17908          587 :   sec.begin ();
   17909              : 
   17910         3190 :   for (unsigned ix = 0; ix != num; ix++)
   17911              :     {
   17912         2603 :       depset *b = spaces[ix];
   17913         2603 :       tree ns = b->get_entity ();
   17914              : 
   17915              :       /* This could be an anonymous namespace even for a named module,
   17916              :          since we can still emit no-linkage decls.  */
   17917         2603 :       gcc_checking_assert (TREE_CODE (ns) == NAMESPACE_DECL);
   17918              : 
   17919         2603 :       unsigned flags = 0;
   17920         2603 :       if (TREE_PUBLIC (ns))
   17921         2545 :         flags |= 1;
   17922         2603 :       if (DECL_NAMESPACE_INLINE_P (ns))
   17923          490 :         flags |= 2;
   17924         2603 :       if (DECL_MODULE_PURVIEW_P (ns))
   17925         2057 :         flags |= 4;
   17926         2603 :       if (DECL_MODULE_EXPORT_P (ns))
   17927         1838 :         flags |= 8;
   17928         2603 :       if (TREE_DEPRECATED (ns))
   17929           17 :         flags |= 16;
   17930              : 
   17931         2743 :       dump () && dump ("Writing namespace:%u %N%s%s%s%s",
   17932              :                        b->cluster, ns,
   17933          140 :                        flags & 1 ? ", public" : "",
   17934          140 :                        flags & 2 ? ", inline" : "",
   17935          140 :                        flags & 4 ? ", purview" : "",
   17936          140 :                        flags & 8 ? ", export" : "",
   17937          140 :                        flags & 16 ? ", deprecated" : "");
   17938         2603 :       sec.u (b->cluster);
   17939         2603 :       sec.u (to->name (DECL_NAME (ns)));
   17940         2603 :       write_namespace (sec, b->deps[0]);
   17941              : 
   17942         2603 :       sec.u (flags);
   17943         2603 :       write_location (sec, DECL_SOURCE_LOCATION (ns));
   17944              : 
   17945         2603 :       if (DECL_NAMESPACE_INLINE_P (ns))
   17946              :         {
   17947          490 :           if (tree attr = lookup_attribute ("abi_tag", DECL_ATTRIBUTES (ns)))
   17948              :             {
   17949          127 :               tree tags = TREE_VALUE (attr);
   17950          127 :               sec.u (list_length (tags));
   17951          257 :               for (tree tag = tags; tag; tag = TREE_CHAIN (tag))
   17952          130 :                 sec.str (TREE_STRING_POINTER (TREE_VALUE (tag)));
   17953              :             }
   17954              :           else
   17955          363 :             sec.u (0);
   17956              :         }
   17957              :     }
   17958              : 
   17959          587 :   sec.end (to, to->name (MOD_SNAME_PFX ".nms"), crc_p);
   17960          587 :   dump.outdent ();
   17961          587 : }
   17962              : 
   17963              : /* Read the namespace hierarchy from MOD_SNAME_PFX.namespace.  Fill in
   17964              :    SPACES from that data.  */
   17965              : 
   17966              : bool
   17967          603 : module_state::read_namespaces (unsigned num)
   17968              : {
   17969          603 :   bytes_in sec;
   17970              : 
   17971          603 :   if (!sec.begin (loc, from (), MOD_SNAME_PFX ".nms"))
   17972              :     return false;
   17973              : 
   17974          673 :   dump () && dump ("Reading namespaces");
   17975          603 :   dump.indent ();
   17976              : 
   17977         3483 :   for (unsigned ix = 0; ix != num; ix++)
   17978              :     {
   17979         2880 :       unsigned entity_index = sec.u ();
   17980         2880 :       unsigned name = sec.u ();
   17981              : 
   17982         2880 :       tree parent = read_namespace (sec);
   17983              : 
   17984              :       /* See comment in write_namespace about why not bits.  */
   17985         2880 :       unsigned flags = sec.u ();
   17986         2880 :       location_t src_loc = read_location (sec);
   17987         2880 :       unsigned tags_count = (flags & 2) ? sec.u () : 0;
   17988              : 
   17989         2880 :       if (entity_index >= entity_num
   17990         2880 :           || !parent
   17991         2880 :           || (flags & 0xc) == 0x8)
   17992            0 :         sec.set_overrun ();
   17993              : 
   17994              :       tree tags = NULL_TREE;
   17995         3024 :       while (tags_count--)
   17996              :         {
   17997          144 :           size_t len;
   17998          144 :           const char *str = sec.str (&len);
   17999          144 :           tags = tree_cons (NULL_TREE, build_string (len + 1, str), tags);
   18000          144 :           tags = nreverse (tags);
   18001              :         }
   18002              : 
   18003         2880 :       if (sec.get_overrun ())
   18004              :         break;
   18005              : 
   18006         5702 :       tree id = name ? get_identifier (from ()->name (name)) : NULL_TREE;
   18007              : 
   18008         3074 :       dump () && dump ("Read namespace:%u %P%s%s%s%s",
   18009              :                        entity_index, parent, id,
   18010           97 :                        flags & 1 ? ", public" : "",
   18011              :                        flags & 2 ? ", inline" : "",
   18012           97 :                        flags & 4 ? ", purview" : "",
   18013           97 :                        flags & 8 ? ", export" : "",
   18014           97 :                        flags & 16 ? ", deprecated" : "");
   18015         2880 :       bool visible_p = ((flags & 8)
   18016         2880 :                         || ((flags & 1)
   18017          533 :                             && (flags & 4)
   18018          101 :                             && (is_partition () || is_module ())));
   18019         2880 :       tree inner = add_imported_namespace (parent, id, src_loc, mod,
   18020              :                                            bool (flags & 2), visible_p);
   18021         2880 :       if (!inner)
   18022              :         {
   18023            0 :           sec.set_overrun ();
   18024            0 :           break;
   18025              :         }
   18026              : 
   18027         2880 :       if (is_partition ())
   18028              :         {
   18029           48 :           if (flags & 4)
   18030           42 :             DECL_MODULE_PURVIEW_P (inner) = true;
   18031           48 :           if (flags & 8)
   18032           27 :             DECL_MODULE_EXPORT_P (inner) = true;
   18033              :         }
   18034              : 
   18035         2880 :       if (flags & 16)
   18036           19 :         TREE_DEPRECATED (inner) = true;
   18037              : 
   18038         2880 :       if (tags)
   18039          141 :         DECL_ATTRIBUTES (inner)
   18040          282 :           = tree_cons (get_identifier ("abi_tag"), tags, DECL_ATTRIBUTES (inner));
   18041              : 
   18042              :       /* Install the namespace.  */
   18043         2880 :       (*entity_ary)[entity_lwm + entity_index] = inner;
   18044         2880 :       if (DECL_MODULE_IMPORT_P (inner))
   18045              :         {
   18046            0 :           bool existed;
   18047            0 :           unsigned *slot = &entity_map->get_or_insert
   18048            0 :             (DECL_UID (inner), &existed);
   18049            0 :           if (existed)
   18050              :             /* If it existed, it should match.  */
   18051            0 :             gcc_checking_assert (inner == (*entity_ary)[*slot]);
   18052              :           else
   18053            0 :             *slot = entity_lwm + entity_index;
   18054              :         }
   18055              :     }
   18056              : 
   18057          603 :   dump.outdent ();
   18058          603 :   if (!sec.end (from ()))
   18059              :     return false;
   18060              :   return true;
   18061          603 : }
   18062              : 
   18063              : unsigned
   18064          587 : module_state::write_using_directives (elf_out *to, depset::hash &table,
   18065              :                                       vec<depset *> spaces, unsigned *crc_p)
   18066              : {
   18067          645 :   dump () && dump ("Writing using-directives");
   18068          587 :   dump.indent ();
   18069              : 
   18070          587 :   bytes_out sec (to);
   18071          587 :   sec.begin ();
   18072              : 
   18073          587 :   unsigned num = 0;
   18074         3777 :   auto emit_one_ns = [&](depset *parent_dep)
   18075              :     {
   18076         3190 :       tree parent = parent_dep->get_entity ();
   18077         3718 :       for (auto udir : NAMESPACE_LEVEL (parent)->using_directives)
   18078              :         {
   18079          180 :           if (TREE_CODE (udir) != USING_DECL || !DECL_MODULE_PURVIEW_P (udir))
   18080           17 :             continue;
   18081          163 :           bool exported = DECL_MODULE_EXPORT_P (udir);
   18082          163 :           tree target = USING_DECL_DECLS (udir);
   18083          163 :           depset *target_dep = table.find_dependency (target);
   18084              : 
   18085              :           /* An using-directive imported from a different module might not
   18086              :              have been walked earlier (PR c++/122915).  But importers will
   18087              :              be able to just refer to the decl in that module unless it was
   18088              :              a partition anyway, so we don't have anything to do here.  */
   18089          163 :           if (!target_dep)
   18090              :             {
   18091            6 :               gcc_checking_assert (DECL_MODULE_IMPORT_P (udir));
   18092            6 :               continue;
   18093              :             }
   18094              : 
   18095          181 :           dump () && dump ("Writing using-directive in %N for %N",
   18096              :                            parent, target);
   18097          157 :           sec.u (exported);
   18098          157 :           write_namespace (sec, parent_dep);
   18099          157 :           write_namespace (sec, target_dep);
   18100          157 :           ++num;
   18101              :         }
   18102         3777 :     };
   18103              : 
   18104          587 :   emit_one_ns (table.find_dependency (global_namespace));
   18105         4364 :   for (depset *parent_dep : spaces)
   18106         2603 :     emit_one_ns (parent_dep);
   18107              : 
   18108          587 :   sec.end (to, to->name (MOD_SNAME_PFX ".udi"), crc_p);
   18109          587 :   dump.outdent ();
   18110              : 
   18111          587 :   return num;
   18112          587 : }
   18113              : 
   18114              : bool
   18115          159 : module_state::read_using_directives (unsigned num)
   18116              : {
   18117          159 :   if (!bitmap_bit_p (this_module ()->imports, mod))
   18118              :     {
   18119           13 :       dump () && dump ("Ignoring using-directives because module %M "
   18120              :                        "is not visible in this TU", this);
   18121           10 :       return true;
   18122              :     }
   18123              : 
   18124          149 :   bytes_in sec;
   18125              : 
   18126          149 :   if (!sec.begin (loc, from (), MOD_SNAME_PFX ".udi"))
   18127              :     return false;
   18128              : 
   18129          161 :   dump () && dump ("Reading using-directives");
   18130          149 :   dump.indent ();
   18131              : 
   18132          326 :   for (unsigned ix = 0; ix != num; ++ix)
   18133              :     {
   18134          177 :       bool exported = sec.u ();
   18135          177 :       tree parent = read_namespace (sec);
   18136          177 :       tree target = read_namespace (sec);
   18137          177 :       if (sec.get_overrun ())
   18138              :         break;
   18139              : 
   18140          189 :       dump () && dump ("Read using-directive in %N for %N", parent, target);
   18141          177 :       if (exported || is_module () || is_partition ())
   18142          150 :         add_imported_using_namespace (parent, target);
   18143              :     }
   18144              : 
   18145          149 :   dump.outdent ();
   18146          149 :   if (!sec.end (from ()))
   18147              :     return false;
   18148              :   return true;
   18149          149 : }
   18150              : 
   18151              : /* Write the binding TABLE to MOD_SNAME_PFX.bnd   */
   18152              : 
   18153              : unsigned
   18154         2757 : module_state::write_bindings (elf_out *to, vec<depset *> sccs, unsigned *crc_p)
   18155              : {
   18156         3057 :   dump () && dump ("Writing binding table");
   18157         2757 :   dump.indent ();
   18158              : 
   18159         2757 :   unsigned num = 0;
   18160         2757 :   bytes_out sec (to);
   18161         2757 :   sec.begin ();
   18162              : 
   18163      2796442 :   for (unsigned ix = 0; ix != sccs.length (); ix++)
   18164              :     {
   18165      1395464 :       depset *b = sccs[ix];
   18166      1395464 :       if (b->is_binding ())
   18167              :         {
   18168       157446 :           tree ns = b->get_entity ();
   18169       158644 :           dump () && dump ("Bindings %P section:%u", ns, b->get_name (),
   18170              :                            b->section);
   18171       157446 :           sec.u (to->name (b->get_name ()));
   18172       157446 :           write_namespace (sec, b->deps[0]);
   18173       157446 :           sec.u (b->section);
   18174       157446 :           num++;
   18175              :         }
   18176              :     }
   18177              : 
   18178         2757 :   sec.end (to, to->name (MOD_SNAME_PFX ".bnd"), crc_p);
   18179         2757 :   dump.outdent ();
   18180              : 
   18181         2757 :   return num;
   18182         2757 : }
   18183              : 
   18184              : /* Read the binding table from MOD_SNAME_PFX.bind.  */
   18185              : 
   18186              : bool
   18187         2940 : module_state::read_bindings (unsigned num, unsigned lwm, unsigned hwm)
   18188              : {
   18189         2940 :   bytes_in sec;
   18190              : 
   18191         2940 :   if (!sec.begin (loc, from (), MOD_SNAME_PFX ".bnd"))
   18192              :     return false;
   18193              : 
   18194         3469 :   dump () && dump ("Reading binding table");
   18195         2940 :   dump.indent ();
   18196       198330 :   for (; !sec.get_overrun () && num--;)
   18197              :     {
   18198       195390 :       const char *name = from ()->name (sec.u ());
   18199       195390 :       tree ns = read_namespace (sec);
   18200       195390 :       unsigned snum = sec.u ();
   18201              : 
   18202       195390 :       if (!ns || !name || (snum - lwm) >= (hwm - lwm))
   18203            0 :         sec.set_overrun ();
   18204       195390 :       if (!sec.get_overrun ())
   18205              :         {
   18206       195390 :           tree id = get_identifier (name);
   18207       196901 :           dump () && dump ("Bindings %P section:%u", ns, id, snum);
   18208       195390 :           if (mod && !import_module_binding (ns, id, mod, snum))
   18209              :             break;
   18210              :         }
   18211              :     }
   18212              : 
   18213         2940 :   dump.outdent ();
   18214         2940 :   if (!sec.end (from ()))
   18215              :     return false;
   18216              :   return true;
   18217         2940 : }
   18218              : 
   18219              : /* Write the entity table to MOD_SNAME_PFX.ent
   18220              : 
   18221              :    Each entry is a section number.  */
   18222              : 
   18223              : void
   18224         2497 : module_state::write_entities (elf_out *to, vec<depset *> depsets,
   18225              :                               unsigned count, unsigned *crc_p)
   18226              : {
   18227         2744 :   dump () && dump ("Writing entities");
   18228         2497 :   dump.indent ();
   18229              : 
   18230         2497 :   bytes_out sec (to);
   18231         2497 :   sec.begin ();
   18232              : 
   18233         2497 :   unsigned current = 0;
   18234      1397940 :   for (unsigned ix = 0; ix < depsets.length (); ix++)
   18235              :     {
   18236      1395443 :       depset *d = depsets[ix];
   18237              : 
   18238      2594096 :       switch (d->get_entity_kind ())
   18239              :         {
   18240              :         default:
   18241              :           break;
   18242              : 
   18243         5100 :         case depset::EK_NAMESPACE:
   18244         5100 :           if (!d->is_import () && d->get_entity () != global_namespace)
   18245              :             {
   18246         2603 :               gcc_checking_assert (d->cluster == current);
   18247         2603 :               current++;
   18248         2603 :               sec.u (0);
   18249              :             }
   18250              :           break;
   18251              : 
   18252      1193553 :         case depset::EK_DECL:
   18253      1193553 :         case depset::EK_SPECIALIZATION:
   18254      1193553 :         case depset::EK_PARTIAL:
   18255      2387106 :           gcc_checking_assert (!d->is_unreached ()
   18256              :                                && !d->is_import ()
   18257              :                                && d->cluster == current
   18258              :                                && d->section);
   18259      1193553 :           current++;
   18260      1193553 :           sec.u (d->section);
   18261      1193553 :           break;
   18262              :         }
   18263              :     }
   18264         2497 :   gcc_assert (count == current);
   18265         2497 :   sec.end (to, to->name (MOD_SNAME_PFX ".ent"), crc_p);
   18266         2497 :   dump.outdent ();
   18267         2497 : }
   18268              : 
   18269              : bool
   18270         2714 : module_state::read_entities (unsigned count, unsigned lwm, unsigned hwm)
   18271              : {
   18272         2714 :   trees_in sec (this);
   18273              : 
   18274         2714 :   if (!sec.begin (loc, from (), MOD_SNAME_PFX ".ent"))
   18275              :     return false;
   18276              : 
   18277         3189 :   dump () && dump ("Reading entities");
   18278         2714 :   dump.indent ();
   18279              : 
   18280      1334014 :   for (binding_slot *slot = entity_ary->begin () + entity_lwm; count--; slot++)
   18281              :     {
   18282      1331300 :       unsigned snum = sec.u ();
   18283      1331300 :       if (snum && (snum - lwm) >= (hwm - lwm))
   18284            0 :         sec.set_overrun ();
   18285      1331300 :       if (sec.get_overrun ())
   18286              :         break;
   18287              : 
   18288      1331300 :       if (snum)
   18289      1328420 :         slot->set_lazy (snum << 2);
   18290              :     }
   18291              : 
   18292         2714 :   dump.outdent ();
   18293         2714 :   if (!sec.end (from ()))
   18294              :     return false;
   18295              :   return true;
   18296         2714 : }
   18297              : 
   18298              : /* Write the pending table to MOD_SNAME_PFX.pnd
   18299              : 
   18300              :    The pending table holds information about clusters that need to be
   18301              :    loaded because they contain information about something that is not
   18302              :    found by namespace-scope lookup.
   18303              : 
   18304              :    The three cases are:
   18305              : 
   18306              :    (a) Template (maybe-partial) specializations that we have
   18307              :    instantiated or defined.  When an importer needs to instantiate
   18308              :    that template, they /must have/ the partial, explicit & extern
   18309              :    specializations available.  If they have the other specializations
   18310              :    available, they'll have less work to do.  Thus, when we're about to
   18311              :    instantiate FOO, we have to be able to ask 'are there any
   18312              :    specialization of FOO in our imports?'.
   18313              : 
   18314              :    (b) (Maybe-implicit) member functions definitions.  A class could
   18315              :    be defined in one header, and an inline member defined in a
   18316              :    different header (this occurs in the STL).  Similarly, like the
   18317              :    specialization case, an implicit member function could have been
   18318              :    'instantiated' in one module, and it'd be nice to not have to
   18319              :    reinstantiate it in another.
   18320              : 
   18321              :    (c) Classes completed elsewhere.  A class could be declared in one
   18322              :    header and defined in another.  We need to know to load the class
   18323              :    definition before looking in it.  It does highlight an issue --
   18324              :    there could be an intermediate import between the outermost containing
   18325              :    namespace-scope class and the innermost being-defined class.  This is
   18326              :    actually possible with all of these cases, so be aware -- we're not
   18327              :    just talking of one level of import to get to the innermost namespace.
   18328              : 
   18329              :    This gets complicated fast, it took me multiple attempts to even
   18330              :    get something remotely working.  Partially because I focussed on
   18331              :    optimizing what I think turns out to be a smaller problem, given
   18332              :    the known need to do the more general case *anyway*.  I document
   18333              :    the smaller problem, because it does appear to be the natural way
   18334              :    to do it.  It's trap!
   18335              : 
   18336              :    **** THE TRAP
   18337              : 
   18338              :    Let's refer to the primary template or the containing class as the
   18339              :    KEY.  And the specialization or member as the PENDING-ENTITY.  (To
   18340              :    avoid having to say those mouthfuls all the time.)
   18341              : 
   18342              :    In either case, we have an entity and we need some way of mapping
   18343              :    that to a set of entities that need to be loaded before we can
   18344              :    proceed with whatever processing of the entity we were going to do.
   18345              : 
   18346              :    We need to link the key to the pending-entity in some way.  Given a
   18347              :    key, tell me the pending-entities I need to have loaded.  However
   18348              :    we tie the key to the pending-entity must not rely on the key being
   18349              :    loaded -- that'd defeat the lazy loading scheme.
   18350              : 
   18351              :    As the key will be an import in we know its entity number (either
   18352              :    because we imported it, or we're writing it out too).  Thus we can
   18353              :    generate a map of key-indices to pending-entities.  The
   18354              :    pending-entity indices will be into our span of the entity table,
   18355              :    and thus allow them to be lazily loaded.  The key index will be
   18356              :    into another slot of the entity table.  Notice that this checking
   18357              :    could be expensive, we don't want to iterate over a bunch of
   18358              :    pending-entity indices (across multiple imports), every time we're
   18359              :    about do to the thing with the key.  We need to quickly determine
   18360              :    'definitely nothing needed'.
   18361              : 
   18362              :    That's almost good enough, except that key indices are not unique
   18363              :    in a couple of cases :( Specifically the Global Module or a module
   18364              :    partition can result in multiple modules assigning an entity index
   18365              :    for the key.  The decl-merging on loading will detect that so we
   18366              :    only have one Key loaded, and in the entity hash it'll indicate the
   18367              :    entity index of first load.  Which might be different to how we
   18368              :    know it.  Notice this is restricted to GM entities or this-module
   18369              :    entities.  Foreign imports cannot have this.
   18370              : 
   18371              :    We can simply resolve this in the direction of how this module
   18372              :    referred to the key to how the importer knows it.  Look in the
   18373              :    entity table slot that we nominate, maybe lazy load it, and then
   18374              :    lookup the resultant entity in the entity hash to learn how the
   18375              :    importer knows it.
   18376              : 
   18377              :    But we need to go in the other direction :( Given the key, find all
   18378              :    the index-aliases of that key.  We can partially solve that by
   18379              :    adding an alias hash table.  Whenever we load a merged decl, add or
   18380              :    augment a mapping from the entity (or its entity-index) to the
   18381              :    newly-discovered index.  Then when we look for pending entities of
   18382              :    a key, we also iterate over this aliases this mapping provides.
   18383              : 
   18384              :    But that requires the alias to be loaded.  And that's not
   18385              :    necessarily true.
   18386              : 
   18387              :    *** THE SIMPLER WAY
   18388              : 
   18389              :    The remaining fixed thing we have is the innermost namespace
   18390              :    containing the ultimate namespace-scope container of the key and
   18391              :    the name of that container (which might be the key itself).  I.e. a
   18392              :    namespace-decl/identifier/module tuple.  Let's call this the
   18393              :    top-key.  We'll discover that the module is not important here,
   18394              :    because of cross-module possibilities mentioned in case #c above.
   18395              :    We can't markup namespace-binding slots.  The best we can do is
   18396              :    mark the binding vector with 'there's something here', and have
   18397              :    another map from namespace/identifier pairs to a vector of pending
   18398              :    entity indices.
   18399              : 
   18400              :    Maintain a pending-entity map.  This is keyed by top-key, and
   18401              :    maps to a vector of pending-entity indices.  On the binding vector
   18402              :    have flags saying whether the pending-name-entity map has contents.
   18403              :    (We might want to further extend the key to be GM-vs-Partition and
   18404              :    specialization-vs-member, but let's not get ahead of ourselves.)
   18405              : 
   18406              :    For every key-like entity, find the outermost namespace-scope
   18407              :    name.  Use that to lookup in the pending-entity map and then make
   18408              :    sure the specified entities are loaded.
   18409              : 
   18410              :    An optimization might be to have a flag in each key-entity saying
   18411              :    that its top key might be in the entity table.  It's not clear to
   18412              :    me how to set that flag cheaply -- cheaper than just looking.
   18413              : 
   18414              :    FIXME: It'd be nice to have a bit in decls to tell us whether to
   18415              :    even try this.  We can have a 'already done' flag, that we set when
   18416              :    we've done KLASS's lazy pendings.  When we import a module that
   18417              :    registers pendings on the same top-key as KLASS we need to clear
   18418              :    the flag.  A recursive walk of the top-key clearing the bit will
   18419              :    suffice.  Plus we only need to recurse on classes that have the bit
   18420              :    set.  (That means we need to set the bit on parents of KLASS here,
   18421              :    don't forget.)  However, first: correctness, second: efficiency.  */
   18422              : 
   18423              : unsigned
   18424         2757 : module_state::write_pendings (elf_out *to, vec<depset *> depsets,
   18425              :                               depset::hash &table, unsigned *crc_p)
   18426              : {
   18427         3057 :   dump () && dump ("Writing pending-entities");
   18428         2757 :   dump.indent ();
   18429              : 
   18430         2757 :   trees_out sec (to, this, table);
   18431         2757 :   sec.begin ();
   18432              : 
   18433         2757 :   unsigned count = 0;
   18434         2757 :   tree cache_ns = NULL_TREE;
   18435         2757 :   tree cache_id = NULL_TREE;
   18436         2757 :   unsigned cache_section = ~0;
   18437      1398221 :   for (unsigned ix = 0; ix < depsets.length (); ix++)
   18438              :     {
   18439      1395464 :       depset *d = depsets[ix];
   18440              : 
   18441      1395464 :       if (d->is_binding ())
   18442       842511 :         continue;
   18443              : 
   18444      1238018 :       if (d->is_import ())
   18445            0 :         continue;
   18446              : 
   18447      1238018 :       if (!d->is_pending_entity ())
   18448       685065 :         continue;
   18449              : 
   18450       552953 :       tree key_decl = nullptr;
   18451       552953 :       tree key_ns = find_pending_key (d->get_entity (), &key_decl);
   18452       552953 :       tree key_name = DECL_NAME (key_decl);
   18453              : 
   18454       552953 :       if (IDENTIFIER_ANON_P (key_name))
   18455              :         {
   18456            6 :           gcc_checking_assert (IDENTIFIER_LAMBDA_P (key_name));
   18457           12 :           if (tree attached = LAMBDA_TYPE_EXTRA_SCOPE (TREE_TYPE (key_decl)))
   18458            6 :             key_name = DECL_NAME (attached);
   18459              :           else
   18460              :             {
   18461              :               /* There's nothing to attach it to.  Must
   18462              :                  always reinstantiate.  */
   18463            0 :               dump ()
   18464            0 :                 && dump ("Unattached lambda %N[%u] section:%u",
   18465            0 :                          d->get_entity_kind () == depset::EK_DECL
   18466              :                          ? "Member" : "Specialization", d->get_entity (),
   18467              :                          d->cluster, d->section);
   18468            0 :               continue;
   18469              :             }
   18470              :         }
   18471              : 
   18472       552953 :       char const *also = "";
   18473       552953 :       if (d->section == cache_section
   18474       368306 :           && key_ns == cache_ns
   18475       368306 :           && key_name == cache_id)
   18476              :         /* Same section & key as previous, no need to repeat ourselves.  */
   18477              :         also = "also ";
   18478              :       else
   18479              :         {
   18480       249730 :           cache_ns = key_ns;
   18481       249730 :           cache_id = key_name;
   18482       249730 :           cache_section = d->section;
   18483       249730 :           gcc_checking_assert (table.find_dependency (cache_ns));
   18484       249730 :           sec.tree_node (cache_ns);
   18485       249730 :           sec.tree_node (cache_id);
   18486       249730 :           sec.u (d->cluster);
   18487       249730 :           count++;
   18488              :         }
   18489       554409 :       dump () && dump ("Pending %s %N entity:%u section:%u %skeyed to %P",
   18490          728 :                        d->get_entity_kind () == depset::EK_DECL
   18491              :                        ? "member" : "specialization", d->get_entity (),
   18492              :                        d->cluster, cache_section, also, cache_ns, cache_id);
   18493              :       }
   18494         2757 :   sec.end (to, to->name (MOD_SNAME_PFX ".pnd"), crc_p);
   18495         2757 :   dump.outdent ();
   18496              : 
   18497         2757 :   return count;
   18498         2757 : }
   18499              : 
   18500              : bool
   18501         1568 : module_state::read_pendings (unsigned count)
   18502              : {
   18503         1568 :   trees_in sec (this);
   18504              : 
   18505         1568 :   if (!sec.begin (loc, from (), MOD_SNAME_PFX ".pnd"))
   18506              :     return false;
   18507              : 
   18508         1887 :   dump () && dump ("Reading %u pendings", count);
   18509         1568 :   dump.indent ();
   18510              : 
   18511       277359 :   for (unsigned ix = 0; ix != count; ix++)
   18512              :     {
   18513       275791 :       pending_key key;
   18514       275791 :       unsigned index;
   18515              : 
   18516       275791 :       key.ns = sec.tree_node ();
   18517       275791 :       key.id = sec.tree_node ();
   18518       275791 :       index = sec.u ();
   18519              : 
   18520       275791 :       if (!key.ns || !key.id
   18521       275791 :           || !(TREE_CODE (key.ns) == NAMESPACE_DECL
   18522       275791 :                && !DECL_NAMESPACE_ALIAS (key.ns))
   18523       275791 :           || !identifier_p (key.id)
   18524       551582 :           || index >= entity_num)
   18525            0 :         sec.set_overrun ();
   18526              : 
   18527       275791 :       if (sec.get_overrun ())
   18528              :         break;
   18529              : 
   18530       276572 :       dump () && dump ("Pending:%u keyed to %P", index, key.ns, key.id);
   18531              : 
   18532       275791 :       index += entity_lwm;
   18533       275791 :       auto &vec = pending_table->get_or_insert (key);
   18534       275791 :       vec.safe_push (index);
   18535              :     }
   18536              : 
   18537         1568 :   dump.outdent ();
   18538         1568 :   if (!sec.end (from ()))
   18539              :     return false;
   18540              :   return true;
   18541         1568 : }
   18542              : 
   18543              : /* Read & write locations.  */
   18544              : enum loc_kind {
   18545              :   LK_ORDINARY,
   18546              :   LK_MACRO,
   18547              :   LK_IMPORT_ORDINARY,
   18548              :   LK_IMPORT_MACRO,
   18549              :   LK_ADHOC,
   18550              :   LK_RESERVED,
   18551              : };
   18552              : 
   18553              : static const module_state *
   18554         7448 : module_for_ordinary_loc (location_t loc)
   18555              : {
   18556         7448 :   unsigned pos = 0;
   18557        14896 :   unsigned len = ool->length () - pos;
   18558              : 
   18559         7451 :   while (len)
   18560              :     {
   18561         7451 :       unsigned half = len / 2;
   18562         7451 :       module_state *probe = (*ool)[pos + half];
   18563         7451 :       if (loc < probe->ordinary_locs.first)
   18564              :         len = half;
   18565         7448 :       else if (loc < probe->ordinary_locs.first + probe->ordinary_locs.second)
   18566              :         return probe;
   18567              :       else
   18568              :         {
   18569            0 :           pos += half + 1;
   18570            0 :           len = len - (half + 1);
   18571              :         }
   18572              :     }
   18573              : 
   18574              :   return nullptr;
   18575              : }
   18576              : 
   18577              : static const module_state *
   18578           15 : module_for_macro_loc (location_t loc)
   18579              : {
   18580           15 :   unsigned pos = 1;
   18581           15 :   unsigned len = modules->length () - pos;
   18582              : 
   18583           15 :   while (len)
   18584              :     {
   18585           15 :       unsigned half = len / 2;
   18586           15 :       module_state *probe = (*modules)[pos + half];
   18587           15 :       if (loc < probe->macro_locs.first)
   18588              :         {
   18589            0 :           pos += half + 1;
   18590            0 :           len = len - (half + 1);
   18591              :         }
   18592           15 :       else if (loc >= probe->macro_locs.first + probe->macro_locs.second)
   18593              :         len = half;
   18594              :       else
   18595              :         return probe;
   18596              :     }
   18597              : 
   18598              :   return NULL;
   18599              : }
   18600              : 
   18601              : location_t
   18602         1307 : module_state::imported_from () const
   18603              : {
   18604         1307 :   location_t from = loc;
   18605         1307 :   line_map_ordinary const *fmap
   18606         1307 :     = linemap_check_ordinary (linemap_lookup (line_table, from));
   18607              : 
   18608         1307 :   if (MAP_MODULE_P (fmap))
   18609         1307 :     from = linemap_included_from (fmap);
   18610              : 
   18611         1307 :   return from;
   18612              : }
   18613              : 
   18614              : /* Note that LOC will need writing.  This allows us to prune locations
   18615              :    that are not needed.  */
   18616              : 
   18617              : bool
   18618     27025718 : module_state::note_location (location_t loc)
   18619              : {
   18620     27025718 :   bool added = false;
   18621     27025718 :   if (!macro_loc_table && !ord_loc_table)
   18622              :     ;
   18623     27025718 :   else if (loc < RESERVED_LOCATION_COUNT)
   18624              :     ;
   18625     24509403 :   else if (IS_ADHOC_LOC (loc))
   18626              :     {
   18627      3062197 :       location_t locus = get_location_from_adhoc_loc (line_table, loc);
   18628      3062197 :       note_location (locus);
   18629      3062197 :       source_range range = get_range_from_loc (line_table, loc);
   18630      3062197 :       if (range.m_start != locus)
   18631      2954575 :         note_location (range.m_start);
   18632      3062197 :       note_location (range.m_finish);
   18633              :     }
   18634     21447206 :   else if (loc >= LINEMAPS_MACRO_LOWEST_LOCATION (line_table))
   18635              :     {
   18636      1523874 :       if (spans.macro (loc))
   18637              :         {
   18638      1523859 :           const line_map *map = linemap_lookup (line_table, loc);
   18639      1523859 :           const line_map_macro *mac_map = linemap_check_macro (map);
   18640      1523859 :           hashval_t hv = macro_loc_traits::hash (mac_map);
   18641      1523859 :           macro_loc_info *slot
   18642      1523859 :             = macro_loc_table->find_slot_with_hash (mac_map, hv, INSERT);
   18643      1523859 :           if (!slot->src)
   18644              :             {
   18645       161188 :               slot->src = mac_map;
   18646       161188 :               slot->remap = 0;
   18647              :               // Expansion locations could themselves be from a
   18648              :               // macro, we need to note them all.
   18649       161188 :               note_location (mac_map->m_expansion);
   18650       161188 :               gcc_checking_assert (mac_map->n_tokens);
   18651       161188 :               location_t tloc = UNKNOWN_LOCATION;
   18652      6026264 :               for (unsigned ix = mac_map->n_tokens * 2; ix--;)
   18653      5865076 :                 if (mac_map->macro_locations[ix] != tloc)
   18654              :                   {
   18655      3095713 :                     tloc = mac_map->macro_locations[ix];
   18656      3095713 :                     note_location (tloc);
   18657              :                   }
   18658              :               added = true;
   18659              :             }
   18660              :         }
   18661              :     }
   18662     19923332 :   else if (IS_ORDINARY_LOC (loc))
   18663              :     {
   18664     19923332 :       if (spans.ordinary (loc))
   18665              :         {
   18666     19915876 :           const line_map *map = linemap_lookup (line_table, loc);
   18667     19915876 :           const line_map_ordinary *ord_map = linemap_check_ordinary (map);
   18668     19915876 :           ord_loc_info lkup;
   18669     19915876 :           lkup.src = ord_map;
   18670     19915876 :           lkup.span = loc_one << ord_map->m_column_and_range_bits;
   18671     19915876 :           lkup.offset = (loc - MAP_START_LOCATION (ord_map)) & ~(lkup.span - 1);
   18672     19915876 :           lkup.remap = 0;
   18673     19915876 :           ord_loc_info *slot = (ord_loc_table->find_slot_with_hash
   18674     19915876 :                                 (lkup, ord_loc_traits::hash (lkup), INSERT));
   18675     19915876 :           if (!slot->src)
   18676              :             {
   18677      2288870 :               *slot = lkup;
   18678      2288870 :               added = true;
   18679              :             }
   18680              :         }
   18681              :     }
   18682              :   else
   18683            0 :     gcc_unreachable ();
   18684     27025718 :   return added;
   18685              : }
   18686              : 
   18687              : /* If we're not streaming, record that we need location LOC.
   18688              :    Otherwise stream it.  */
   18689              : 
   18690              : void
   18691     40472027 : module_state::write_location (bytes_out &sec, location_t loc)
   18692              : {
   18693     40472027 :   if (!sec.streaming_p ())
   18694              :     {
   18695     14405033 :       note_location (loc);
   18696     14405033 :       return;
   18697              :     }
   18698              : 
   18699     26066994 :   if (loc < RESERVED_LOCATION_COUNT)
   18700              :     {
   18701      2581433 :       dump (dumper::LOCATION) && dump ("Reserved location %K", loc);
   18702      2581415 :       sec.loc (LK_RESERVED + loc);
   18703              :     }
   18704     23485579 :   else if (IS_ADHOC_LOC (loc))
   18705              :     {
   18706      2988048 :       dump (dumper::LOCATION) && dump ("Adhoc location");
   18707      2988045 :       sec.u (LK_ADHOC);
   18708      2988045 :       location_t locus = get_location_from_adhoc_loc (line_table, loc);
   18709      2988045 :       write_location (sec, locus);
   18710      2988045 :       source_range range = get_range_from_loc (line_table, loc);
   18711      2988045 :       if (range.m_start == locus)
   18712              :         /* Compress.  */
   18713       100951 :         range.m_start = UNKNOWN_LOCATION;
   18714      2988045 :       write_location (sec, range.m_start);
   18715      2988045 :       write_location (sec, range.m_finish);
   18716      2988045 :       unsigned discriminator = get_discriminator_from_adhoc_loc (line_table, loc);
   18717      2988045 :       sec.u (discriminator);
   18718              :     }
   18719     20497534 :   else if (loc >= LINEMAPS_MACRO_LOWEST_LOCATION (line_table))
   18720              :     {
   18721      1515156 :       const macro_loc_info *info = nullptr;
   18722      1515156 :       line_map_uint_t offset = 0;
   18723      1515156 :       if (unsigned hwm = macro_loc_remap->length ())
   18724              :         {
   18725      1515150 :           info = macro_loc_remap->begin ();
   18726     22351878 :           while (hwm != 1)
   18727              :             {
   18728     19321578 :               unsigned mid = hwm / 2;
   18729     19321578 :               if (MAP_START_LOCATION (info[mid].src) <= loc)
   18730              :                 {
   18731      9784037 :                   info += mid;
   18732      9784037 :                   hwm -= mid;
   18733              :                 }
   18734              :               else
   18735              :                 hwm = mid;
   18736              :             }
   18737      1515150 :           offset = loc - MAP_START_LOCATION (info->src);
   18738      1515150 :           if (offset > info->src->n_tokens)
   18739            9 :             info = nullptr;
   18740              :         }
   18741              : 
   18742      1515156 :       gcc_checking_assert (bool (info) == bool (spans.macro (loc)));
   18743              : 
   18744      1515156 :       if (info)
   18745              :         {
   18746      1515141 :           offset += info->remap;
   18747      1515141 :           sec.u (LK_MACRO);
   18748      1515141 :           sec.loc (offset);
   18749      1515141 :           dump (dumper::LOCATION)
   18750            9 :             && dump ("Macro location %K output %K", loc, offset);
   18751              :         }
   18752           15 :       else if (const module_state *import = module_for_macro_loc (loc))
   18753              :         {
   18754           15 :           auto off = loc - import->macro_locs.first;
   18755           15 :           sec.u (LK_IMPORT_MACRO);
   18756           15 :           sec.u (import->remap);
   18757           15 :           sec.loc (off);
   18758           15 :           dump (dumper::LOCATION)
   18759            0 :             && dump ("Imported macro location %K output %u:%K",
   18760            0 :                      loc, import->remap, off);
   18761              :         }
   18762              :       else
   18763            0 :         gcc_unreachable ();
   18764              :     }
   18765     18982378 :   else if (IS_ORDINARY_LOC (loc))
   18766              :     {
   18767              :       /* If we ran out of locations for imported decls, this location could
   18768              :          be a module unit's location.  In that case, remap the location
   18769              :          to be where we imported the module from.  */
   18770     18982378 :       if (spans.locations_exhausted_p () || CHECKING_P)
   18771              :         {
   18772     18982378 :           const line_map_ordinary *map
   18773     18982378 :             = linemap_check_ordinary (linemap_lookup (line_table, loc));
   18774     18982378 :           if (MAP_MODULE_P (map) && loc == MAP_START_LOCATION (map))
   18775              :             {
   18776            0 :               gcc_checking_assert (spans.locations_exhausted_p ());
   18777            0 :               write_location (sec, linemap_included_from (map));
   18778            0 :               return;
   18779              :             }
   18780              :         }
   18781              : 
   18782     18982378 :       const ord_loc_info *info = nullptr;
   18783     18982378 :       line_map_uint_t offset = 0;
   18784     18982378 :       if (line_map_uint_t hwm = ord_loc_remap->length ())
   18785              :         {
   18786     18982378 :           info = ord_loc_remap->begin ();
   18787    281452270 :           while (hwm != 1)
   18788              :             {
   18789    243487514 :               auto mid = hwm / 2;
   18790    243487514 :               if (MAP_START_LOCATION (info[mid].src) + info[mid].offset <= loc)
   18791              :                 {
   18792    126602706 :                   info += mid;
   18793    126602706 :                   hwm -= mid;
   18794              :                 }
   18795              :               else
   18796              :                 hwm = mid;
   18797              :             }
   18798     18982378 :           offset = loc - MAP_START_LOCATION (info->src) - info->offset;
   18799     18982378 :           if (offset > info->span)
   18800         7448 :             info = nullptr;
   18801              :         }
   18802              : 
   18803     18982378 :       gcc_checking_assert (bool (info) == bool (spans.ordinary (loc)));
   18804              : 
   18805     18982378 :       if (info)
   18806              :         {
   18807     18974930 :           offset += info->remap;
   18808     18974930 :           sec.u (LK_ORDINARY);
   18809     18974930 :           sec.loc (offset);
   18810              : 
   18811     18974930 :           dump (dumper::LOCATION)
   18812           78 :             && dump ("Ordinary location %K output %K", loc, offset);
   18813              :         }
   18814         7448 :       else if (const module_state *import = module_for_ordinary_loc (loc))
   18815              :         {
   18816         7448 :           auto off = loc - import->ordinary_locs.first;
   18817         7448 :           sec.u (LK_IMPORT_ORDINARY);
   18818         7448 :           sec.u (import->remap);
   18819         7448 :           sec.loc (off);
   18820         7448 :           dump (dumper::LOCATION)
   18821            0 :             && dump ("Imported ordinary location %K output %u:%K",
   18822            0 :                      loc, import->remap, off);
   18823              :         }
   18824              :       else
   18825            0 :         gcc_unreachable ();
   18826              :     }
   18827              :   else
   18828            0 :     gcc_unreachable ();
   18829              : }
   18830              : 
   18831              : location_t
   18832     21758691 : module_state::read_location (bytes_in &sec) const
   18833              : {
   18834     21758691 :   location_t locus = UNKNOWN_LOCATION;
   18835     21758691 :   unsigned kind = sec.u ();
   18836     21758691 :   switch (kind)
   18837              :      {
   18838      2046744 :     default:
   18839      2046744 :       {
   18840      2046744 :         if (kind < LK_RESERVED + RESERVED_LOCATION_COUNT)
   18841      2046744 :           locus = location_t (kind - LK_RESERVED);
   18842              :         else
   18843            0 :           sec.set_overrun ();
   18844      2046744 :         dump (dumper::LOCATION)
   18845            0 :           && dump ("Reserved location %K", locus);
   18846              :       }
   18847              :       break;
   18848              : 
   18849      2336027 :      case LK_ADHOC:
   18850      2336027 :       {
   18851      2336027 :         dump (dumper::LOCATION) && dump ("Adhoc location");
   18852      2336027 :         locus = read_location (sec);
   18853      2336027 :         source_range range;
   18854      2336027 :         range.m_start = read_location (sec);
   18855      2336027 :         if (range.m_start == UNKNOWN_LOCATION)
   18856        72394 :           range.m_start = locus;
   18857      2336027 :         range.m_finish = read_location (sec);
   18858      2336027 :         unsigned discriminator = sec.u ();
   18859      2336027 :         if (locus != loc && range.m_start != loc && range.m_finish != loc)
   18860      2336027 :           locus = line_table->get_or_create_combined_loc (locus, range,
   18861              :                                                           nullptr, discriminator);
   18862              :       }
   18863              :       break;
   18864              : 
   18865      1601548 :     case LK_MACRO:
   18866      1601548 :       {
   18867      1601548 :         auto off = sec.loc ();
   18868              : 
   18869      1601548 :         if (macro_locs.second)
   18870              :           {
   18871      1601548 :             if (off < macro_locs.second)
   18872      1601548 :               locus = off + macro_locs.first;
   18873              :             else
   18874            0 :               sec.set_overrun ();
   18875              :           }
   18876              :         else
   18877            0 :           locus = loc;
   18878      1601548 :         dump (dumper::LOCATION)
   18879            0 :           && dump ("Macro %K becoming %K", off, locus);
   18880              :       }
   18881              :       break;
   18882              : 
   18883     15770668 :     case LK_ORDINARY:
   18884     15770668 :       {
   18885     15770668 :         auto off = sec.loc ();
   18886     15770668 :         if (ordinary_locs.second)
   18887              :           {
   18888     15770668 :             if (off < ordinary_locs.second)
   18889     15770668 :               locus = off + ordinary_locs.first;
   18890              :             else
   18891            0 :               sec.set_overrun ();
   18892              :           }
   18893              :         else
   18894            0 :           locus = loc;
   18895              : 
   18896     15770668 :         dump (dumper::LOCATION)
   18897            0 :           && dump ("Ordinary location %K becoming %K", off, locus);
   18898              :       }
   18899              :       break;
   18900              : 
   18901         3704 :      case LK_IMPORT_MACRO:
   18902         3704 :      case LK_IMPORT_ORDINARY:
   18903         3704 :        {
   18904         3704 :          unsigned mod = sec.u ();
   18905         3704 :          location_t off = sec.loc ();
   18906         3704 :          const module_state *import = NULL;
   18907              : 
   18908         3704 :          if (!mod && !slurp->remap)
   18909              :            /* This is an early read of a partition location during the
   18910              :               read of our ordinary location map.  */
   18911              :            import = this;
   18912              :          else
   18913              :            {
   18914         3704 :              mod = slurp->remap_module (mod);
   18915         3704 :              if (!mod)
   18916            0 :                sec.set_overrun ();
   18917              :              else
   18918         3704 :                import = (*modules)[mod];
   18919              :            }
   18920              : 
   18921         3704 :          if (import)
   18922              :            {
   18923         3704 :              if (kind == LK_IMPORT_MACRO)
   18924              :                {
   18925           22 :                  if (!import->macro_locs.second)
   18926            0 :                    locus = import->loc;
   18927           22 :                  else if (off < import->macro_locs.second)
   18928           22 :                    locus = off + import->macro_locs.first;
   18929              :                  else
   18930            0 :                    sec.set_overrun ();
   18931              :                }
   18932              :              else
   18933              :                {
   18934         3682 :                  if (!import->ordinary_locs.second)
   18935            0 :                    locus = import->loc;
   18936         3682 :                  else if (off < import->ordinary_locs.second)
   18937         3682 :                    locus = import->ordinary_locs.first + off;
   18938              :                  else
   18939            0 :                    sec.set_overrun ();
   18940              :                }
   18941              :            }
   18942              :        }
   18943              :        break;
   18944              :     }
   18945              : 
   18946     21758691 :   return locus;
   18947              : }
   18948              : 
   18949              : /* Allocate hash tables to record needed locations.  */
   18950              : 
   18951              : void
   18952         2786 : module_state::write_init_maps ()
   18953              : {
   18954         2786 :   macro_loc_table = new hash_table<macro_loc_traits> (EXPERIMENT (1, 400));
   18955         2786 :   ord_loc_table = new hash_table<ord_loc_traits> (EXPERIMENT (1, 400));
   18956         2786 : }
   18957              : 
   18958              : /* Prepare the span adjustments.  We prune unneeded locations -- at
   18959              :    this point every needed location must have been seen by
   18960              :    note_location.  */
   18961              : 
   18962              : range_t
   18963         2757 : module_state::write_prepare_maps (module_state_config *cfg, bool has_partitions)
   18964              : {
   18965         3057 :   dump () && dump ("Preparing locations");
   18966         2757 :   dump.indent ();
   18967              : 
   18968         3057 :   dump () && dump ("Reserved locations [%K,%K) macro [%K,%K)",
   18969          300 :                    spans[loc_spans::SPAN_RESERVED].ordinary.first,
   18970          300 :                    spans[loc_spans::SPAN_RESERVED].ordinary.second,
   18971          300 :                    spans[loc_spans::SPAN_RESERVED].macro.first,
   18972          300 :                    spans[loc_spans::SPAN_RESERVED].macro.second);
   18973              : 
   18974         2757 :   range_t info {0, 0};
   18975              : 
   18976              :   // Sort the noted lines.
   18977         2757 :   vec_alloc (ord_loc_remap, ord_loc_table->size ());
   18978         2757 :   for (auto iter = ord_loc_table->begin (), end = ord_loc_table->end ();
   18979      4552421 :        iter != end; ++iter)
   18980      2274832 :     ord_loc_remap->quick_push (*iter);
   18981         2757 :   ord_loc_remap->qsort (&ord_loc_info::compare);
   18982              : 
   18983              :   // Note included-from maps.
   18984         2757 :   bool added = false;
   18985         2757 :   const line_map_ordinary *current = nullptr;
   18986      2283103 :   for (auto iter = ord_loc_remap->begin (), end = ord_loc_remap->end ();
   18987      2277589 :        iter != end; ++iter)
   18988      2274832 :     if (iter->src != current)
   18989              :       {
   18990        33956 :         current = iter->src;
   18991        13556 :         for (auto probe = current;
   18992        33956 :              auto from = linemap_included_from (probe);
   18993        13556 :              probe = linemap_check_ordinary (linemap_lookup (line_table, from)))
   18994              :           {
   18995        30831 :             if (has_partitions)
   18996              :               {
   18997              :                 // Partition locations need to elide their module map
   18998              :                 // entry.
   18999          220 :                 probe
   19000          220 :                   = linemap_check_ordinary (linemap_lookup (line_table, from));
   19001          220 :                 if (MAP_MODULE_P (probe))
   19002          187 :                   from = linemap_included_from (probe);
   19003              :               }
   19004              : 
   19005        30831 :             if (!note_location (from))
   19006              :               break;
   19007        13556 :             added = true;
   19008        13556 :           }
   19009              :       }
   19010         2757 :   if (added)
   19011              :     {
   19012              :       // Reconstruct the line array as we added items to the hash table.
   19013          501 :       vec_free (ord_loc_remap);
   19014          501 :       vec_alloc (ord_loc_remap, ord_loc_table->size ());
   19015          501 :       for (auto iter = ord_loc_table->begin (), end = ord_loc_table->end ();
   19016      4553353 :            iter != end; ++iter)
   19017      2276426 :         ord_loc_remap->quick_push (*iter);
   19018          501 :       ord_loc_remap->qsort (&ord_loc_info::compare);
   19019              :     }
   19020         2757 :   delete ord_loc_table;
   19021         2757 :   ord_loc_table = nullptr;
   19022              : 
   19023              :   // Merge (sufficiently) adjacent spans, and calculate remapping.
   19024         2757 :   constexpr line_map_uint_t adjacency = 2; // Allow 2 missing lines.
   19025         5514 :   auto begin = ord_loc_remap->begin (), end = ord_loc_remap->end ();
   19026         2757 :   auto dst = begin;
   19027         2757 :   line_map_uint_t offset = 0;
   19028         2757 :   unsigned range_bits = 0;
   19029         2757 :   ord_loc_info *base = nullptr;
   19030      2291145 :   for (auto iter = begin; iter != end; ++iter)
   19031              :     {
   19032      2288388 :       if (base && iter->src == base->src)
   19033              :         {
   19034      4287418 :           if (base->offset + base->span +
   19035      2259216 :               ((adjacency << base->src->m_column_and_range_bits)
   19036              :                // If there are few c&r bits, allow further separation.
   19037      2259216 :                | (adjacency << 4))
   19038      2259216 :               >= iter->offset)
   19039              :             {
   19040              :               // Merge.
   19041      2028202 :               offset -= base->span;
   19042      2028202 :               base->span = iter->offset + iter->span - base->offset;
   19043      2028202 :               offset += base->span;
   19044      2028202 :               continue;
   19045              :             }
   19046              :         }
   19047        29172 :       else if (range_bits < iter->src->m_range_bits)
   19048         2661 :         range_bits = iter->src->m_range_bits;
   19049              : 
   19050       260186 :       offset += ((loc_one << iter->src->m_range_bits) - 1);
   19051       260186 :       offset &= ~((loc_one << iter->src->m_range_bits) - 1);
   19052       260186 :       iter->remap = offset;
   19053       260186 :       offset += iter->span;
   19054       260186 :       base = dst;
   19055       260186 :       *dst++ = *iter;
   19056              :     }
   19057         2757 :   ord_loc_remap->truncate (dst - begin);
   19058              : 
   19059         2757 :   info.first = ord_loc_remap->length ();
   19060         2757 :   cfg->ordinary_locs = offset;
   19061         2757 :   cfg->loc_range_bits = range_bits;
   19062         3057 :   dump () && dump ("Ordinary maps:%K locs:%K range_bits:%u",
   19063              :                    info.first,
   19064              :                    cfg->ordinary_locs,
   19065              :                    cfg->loc_range_bits);
   19066              : 
   19067              :   // Remap the macro locations.
   19068         2757 :   vec_alloc (macro_loc_remap, macro_loc_table->size ());
   19069         2757 :   for (auto iter = macro_loc_table->begin (), end = macro_loc_table->end ();
   19070       325133 :        iter != end; ++iter)
   19071       161188 :     macro_loc_remap->quick_push (*iter);
   19072         2757 :   delete macro_loc_table;
   19073         2757 :   macro_loc_table = nullptr;
   19074              : 
   19075         2757 :   macro_loc_remap->qsort (&macro_loc_info::compare);
   19076         2757 :   offset = 0;
   19077         8271 :   for (auto iter = macro_loc_remap->begin (), end = macro_loc_remap->end ();
   19078       163945 :        iter != end; ++iter)
   19079              :     {
   19080       161188 :       auto mac = iter->src;
   19081       161188 :       iter->remap = offset;
   19082       161188 :       offset += mac->n_tokens;
   19083              :     }
   19084         2757 :   info.second = macro_loc_remap->length ();
   19085         2757 :   cfg->macro_locs = offset;
   19086              : 
   19087         3057 :   dump () && dump ("Macro maps:%K locs:%K", info.second, cfg->macro_locs);
   19088              : 
   19089         2757 :   dump.outdent ();
   19090              : 
   19091              :   // If we have no ordinary locs, we must also have no macro locs.
   19092         2757 :   gcc_checking_assert (cfg->ordinary_locs || !cfg->macro_locs);
   19093              : 
   19094         2757 :   return info;
   19095              : }
   19096              : 
   19097              : bool
   19098         2991 : module_state::read_prepare_maps (const module_state_config *cfg)
   19099              : {
   19100         2991 :   location_t ordinary = line_table->highest_location + 1;
   19101         2991 :   ordinary += cfg->ordinary_locs;
   19102              : 
   19103         2991 :   location_t macro = LINEMAPS_MACRO_LOWEST_LOCATION (line_table);
   19104         2991 :   macro -= cfg->macro_locs;
   19105              : 
   19106         2991 :   if (ordinary < LINE_MAP_MAX_LOCATION_WITH_COLS
   19107         2991 :       && macro >= LINE_MAP_MAX_LOCATION)
   19108              :     /* OK, we have enough locations.  */
   19109              :     return true;
   19110              : 
   19111            0 :   ordinary_locs.first = ordinary_locs.second = 0;
   19112            0 :   macro_locs.first = macro_locs.second = 0;
   19113              : 
   19114            0 :   spans.report_location_exhaustion (loc);
   19115              : 
   19116              :   return false;
   19117              : }
   19118              : 
   19119              : /* Write & read the location maps. Not called if there are no
   19120              :    locations.   */
   19121              : 
   19122              : void
   19123         2661 : module_state::write_ordinary_maps (elf_out *to, range_t &info,
   19124              :                                    bool has_partitions, unsigned *crc_p)
   19125              : {
   19126         2939 :   dump () && dump ("Writing ordinary location maps");
   19127         2661 :   dump.indent ();
   19128              : 
   19129         2661 :   vec<const char *> filenames;
   19130         2661 :   filenames.create (20);
   19131              : 
   19132              :   /* Determine the unique filenames.  */
   19133         2661 :   const line_map_ordinary *current = nullptr;
   19134       268169 :   for (auto iter = ord_loc_remap->begin (), end = ord_loc_remap->end ();
   19135       262847 :        iter != end; ++iter)
   19136       260186 :     if (iter->src != current)
   19137              :       {
   19138        29172 :         current = iter->src;
   19139        29172 :         const char *fname = ORDINARY_MAP_FILE_NAME (iter->src);
   19140              : 
   19141              :         /* We should never find a module linemap in an interval.  */
   19142        29172 :         gcc_checking_assert (!MAP_MODULE_P (iter->src));
   19143              : 
   19144              :         /* We expect very few filenames, so just an array.
   19145              :            (Not true when headers are still in play :()  */
   19146      2126538 :         for (unsigned jx = filenames.length (); jx--;)
   19147              :           {
   19148      2081998 :             const char *name = filenames[jx];
   19149      2081998 :             if (0 == strcmp (name, fname))
   19150              :               {
   19151              :                 /* Reset the linemap's name, because for things like
   19152              :                    preprocessed input we could have multiple instances
   19153              :                    of the same name, and we'd rather not percolate
   19154              :                    that.  */
   19155        13804 :                 const_cast<line_map_ordinary *> (iter->src)->to_file = name;
   19156        13804 :                 fname = NULL;
   19157        13804 :                 break;
   19158              :               }
   19159              :           }
   19160        29172 :         if (fname)
   19161        15368 :           filenames.safe_push (fname);
   19162              :       }
   19163              : 
   19164         2661 :   bytes_out sec (to);
   19165         2661 :   sec.begin ();
   19166              : 
   19167              :   /* Write the filenames.  */
   19168         2661 :   unsigned len = filenames.length ();
   19169         2661 :   sec.u (len);
   19170         2939 :   dump () && dump ("%u source file names", len);
   19171        18029 :   for (unsigned ix = 0; ix != len; ix++)
   19172              :     {
   19173        15368 :       const char *fname = filenames[ix];
   19174        15383 :       dump (dumper::LOCATION) && dump ("Source file[%u]=%s", ix, fname);
   19175        15368 :       sec.str (fname);
   19176              :     }
   19177              : 
   19178         2661 :   sec.loc (info.first); /* Num maps.  */
   19179         2661 :   const ord_loc_info *base = nullptr;
   19180       268169 :   for (auto iter = ord_loc_remap->begin (), end = ord_loc_remap->end ();
   19181       262847 :        iter != end; ++iter)
   19182              :     {
   19183       260186 :       dump (dumper::LOCATION)
   19184           36 :         && dump ("Span:%K ordinary [%K+%K,+%K)->[%K,+%K)",
   19185           36 :                  (location_t) (iter - ord_loc_remap->begin ()),
   19186           18 :                  MAP_START_LOCATION (iter->src),
   19187              :                  iter->offset, iter->span, iter->remap,
   19188              :                  iter->span);
   19189              : 
   19190       260186 :       if (!base || iter->src != base->src)
   19191        29172 :         base = iter;
   19192       260186 :       sec.loc (iter->offset - base->offset);
   19193       260186 :       if (base == iter)
   19194              :         {
   19195        29172 :           sec.u (iter->src->sysp);
   19196        29172 :           sec.u (iter->src->m_range_bits);
   19197        29172 :           sec.u (iter->src->m_column_and_range_bits - iter->src->m_range_bits);
   19198              : 
   19199        29172 :           const char *fname = ORDINARY_MAP_FILE_NAME (iter->src);
   19200      6480578 :           for (unsigned ix = 0; ix != filenames.length (); ix++)
   19201      3240289 :             if (filenames[ix] == fname)
   19202              :               {
   19203        29172 :                 sec.u (ix);
   19204        29172 :                 break;
   19205              :               }
   19206        29172 :           unsigned line = ORDINARY_MAP_STARTING_LINE_NUMBER (iter->src);
   19207        29172 :           line += iter->offset >> iter->src->m_column_and_range_bits;
   19208        29172 :           sec.u (line);
   19209              :         }
   19210       260186 :       sec.loc (iter->remap);
   19211       260186 :       if (base == iter)
   19212              :         {
   19213              :           /* Write the included from location, which means reading it
   19214              :              while reading in the ordinary maps.  So we'd better not
   19215              :              be getting ahead of ourselves.  */
   19216        29172 :           location_t from = linemap_included_from (iter->src);
   19217        29172 :           gcc_checking_assert (from < MAP_START_LOCATION (iter->src));
   19218        29172 :           if (from != UNKNOWN_LOCATION && has_partitions)
   19219              :             {
   19220              :               /* A partition's span will have a from pointing at a
   19221              :                  MODULE_INC.  Find that map's from.  */
   19222          214 :               line_map_ordinary const *fmap
   19223          214 :                 = linemap_check_ordinary (linemap_lookup (line_table, from));
   19224          214 :               if (MAP_MODULE_P (fmap))
   19225          181 :                 from = linemap_included_from (fmap);
   19226              :             }
   19227        29172 :           write_location (sec, from);
   19228              :         }
   19229              :     }
   19230              : 
   19231         2661 :   filenames.release ();
   19232              : 
   19233         2661 :   sec.end (to, to->name (MOD_SNAME_PFX ".olm"), crc_p);
   19234         2661 :   dump.outdent ();
   19235         2661 : }
   19236              : 
   19237              : /* Return the prefix to use for dumping a #pragma diagnostic change to DK.  */
   19238              : 
   19239              : static const char *
   19240          999 : dk_string (enum diagnostics::kind dk)
   19241              : {
   19242          999 :   gcc_assert (dk > diagnostics::kind::unspecified
   19243              :               && dk < diagnostics::kind::last_diagnostic_kind);
   19244          999 :   if (dk == diagnostics::kind::ignored)
   19245              :     /* diagnostics/kinds.def has an empty string for ignored.  */
   19246              :     return "ignored: ";
   19247              :   else
   19248            0 :     return diagnostics::get_text_for_kind (dk);
   19249              : }
   19250              : 
   19251              : /* Dump one #pragma GCC diagnostic entry.  */
   19252              : 
   19253              : static bool
   19254         2035 : dump_dc_change (unsigned index, unsigned opt, enum diagnostics::kind dk)
   19255              : {
   19256         2035 :   if (dk == diagnostics::kind::pop)
   19257         1036 :     return dump (" Index %u: pop from %d", index, opt);
   19258              :   else
   19259          999 :     return dump (" Index %u: %s%s", index, dk_string (dk),
   19260         1998 :                  cl_options[opt].opt_text);
   19261              : }
   19262              : 
   19263              : /* Write out any #pragma GCC diagnostic info to the .dgc section.  */
   19264              : 
   19265              : void
   19266         5418 : module_state::write_diagnostic_classification (elf_out *to,
   19267              :                                                diagnostics::context *dc,
   19268              :                                                unsigned *crc_p)
   19269              : {
   19270         5418 :   auto &changes = dc->get_classification_history ();
   19271              : 
   19272         5418 :   bytes_out sec (to);
   19273         5418 :   if (sec.streaming_p ())
   19274              :     {
   19275         2661 :       sec.begin ();
   19276         2939 :       dump () && dump ("Writing diagnostic change locations");
   19277         2661 :       dump.indent ();
   19278              :     }
   19279              : 
   19280         5418 :   unsigned len = changes.length ();
   19281              : 
   19282              :   /* We don't want to write out any entries that came from one of our imports.
   19283              :      But then we need to adjust the total, and change diagnostics::kind::pop
   19284              :      targets to match the index in our actual output.  So remember how many
   19285              :      lines we had skipped at each step, where -1 means this line itself
   19286              :      is skipped.  */
   19287         5418 :   int skips = 0;
   19288         5418 :   auto_vec<int> skips_at (len);
   19289         5418 :   skips_at.safe_grow (len);
   19290              : 
   19291        68860 :   for (unsigned i = 0; i < len; ++i)
   19292              :     {
   19293        63442 :       const auto &c = changes[i];
   19294        63442 :       skips_at[i] = skips;
   19295        63442 :       if (linemap_location_from_module_p (line_table, c.location))
   19296              :         {
   19297        14196 :           ++skips;
   19298        14196 :           skips_at[i] = -1;
   19299        14196 :           continue;
   19300              :         }
   19301              :     }
   19302              : 
   19303         5418 :   if (sec.streaming_p ())
   19304              :     {
   19305         2661 :       sec.u (len - skips);
   19306         2939 :       dump () && dump ("Diagnostic changes: %u", len - skips);
   19307              :     }
   19308              : 
   19309        68860 :   for (unsigned i = 0; i < len; ++i)
   19310              :     {
   19311        63442 :       if (skips_at[i] == -1)
   19312        14196 :         continue;
   19313              : 
   19314        49246 :       const auto &c = changes[i];
   19315        49246 :       write_location (sec, c.location);
   19316        49246 :       if (sec.streaming_p ())
   19317              :         {
   19318        24623 :           unsigned opt = c.option;
   19319        24623 :           if (c.kind == diagnostics::kind::pop)
   19320        12599 :             opt -= skips_at[opt];
   19321        24623 :           sec.u (opt);
   19322        24623 :           sec.u (static_cast<unsigned> (c.kind));
   19323        65410 :           dump () && dump_dc_change (i - skips_at[i], opt, c.kind);
   19324              :         }
   19325              :     }
   19326              : 
   19327         5418 :   if (sec.streaming_p ())
   19328              :     {
   19329         2661 :       sec.end (to, to->name (MOD_SNAME_PFX ".dgc"), crc_p);
   19330         2661 :       dump.outdent ();
   19331              :     }
   19332         5418 : }
   19333              : 
   19334              : /* Read any #pragma GCC diagnostic info from the .dgc section.  */
   19335              : 
   19336              : bool
   19337         2933 : module_state::read_diagnostic_classification (diagnostics::context *dc)
   19338              : {
   19339         2933 :   bytes_in sec;
   19340              : 
   19341         2933 :   if (!sec.begin (loc, from (), MOD_SNAME_PFX ".dgc"))
   19342              :     return false;
   19343              : 
   19344         3450 :   dump () && dump ("Reading diagnostic change locations");
   19345         2933 :   dump.indent ();
   19346              : 
   19347         2933 :   unsigned len = sec.u ();
   19348         3450 :   dump () && dump ("Diagnostic changes: %u", len);
   19349              : 
   19350         2933 :   auto &changes = dc->get_classification_history ();
   19351         2933 :   int offset = changes.length ();
   19352         2933 :   changes.reserve (len + 1);
   19353        27473 :   for (unsigned i = 0; i < len; ++i)
   19354              :     {
   19355        24540 :       location_t loc = read_location (sec);
   19356        24540 :       int opt = sec.u ();
   19357        24540 :       enum diagnostics::kind kind = (enum diagnostics::kind) sec.u ();
   19358        24540 :       if (kind == diagnostics::kind::pop)
   19359              :         /* For a pop, opt is the 'changes' index to return to.  */
   19360        12580 :         opt += offset;
   19361        24540 :       changes.quick_push ({ loc, opt, kind });
   19362        24607 :       dump () && dump_dc_change (changes.length () - 1, opt, kind);
   19363              :     }
   19364              : 
   19365              :   /* Did the import pop all its diagnostic changes?  */
   19366         2933 :   bool last_was_reset = (len == 0);
   19367         2933 :   if (len)
   19368          232 :     for (int i = changes.length () - 1; ; --i)
   19369              :       {
   19370        10837 :         gcc_checking_assert (i >= offset);
   19371              : 
   19372        10837 :         const auto &c = changes[i];
   19373        10837 :         if (c.kind != diagnostics::kind::pop)
   19374              :           break;
   19375        10828 :         else if (c.option == offset)
   19376              :           {
   19377              :             last_was_reset = true;
   19378              :             break;
   19379              :           }
   19380              :         else
   19381              :           /* As in update_effective_level_from_pragmas, the loop will decrement
   19382              :              i so we actually jump to c.option - 1.  */
   19383        10721 :           i = c.option;
   19384        10721 :       }
   19385         2933 :   if (!last_was_reset)
   19386              :     {
   19387              :       /* It didn't, so add a pop at its last location to avoid affecting later
   19388              :          imports.  */
   19389            9 :       location_t last_loc = ordinary_locs.first + ordinary_locs.second - 1;
   19390            9 :       changes.quick_push ({ last_loc, offset, diagnostics::kind::pop });
   19391           15 :       dump () && dump (" Adding final pop from index %d", offset);
   19392              :     }
   19393              : 
   19394         2933 :   dump.outdent ();
   19395         2933 :   if (!sec.end (from ()))
   19396              :     return false;
   19397              : 
   19398              :   return true;
   19399         2933 : }
   19400              : 
   19401              : void
   19402          124 : module_state::write_macro_maps (elf_out *to, range_t &info, unsigned *crc_p)
   19403              : {
   19404          136 :   dump () && dump ("Writing macro location maps");
   19405          124 :   dump.indent ();
   19406              : 
   19407          124 :   bytes_out sec (to);
   19408          124 :   sec.begin ();
   19409              : 
   19410          136 :   dump () && dump ("Macro maps:%K", info.second);
   19411          124 :   sec.loc (info.second);
   19412              : 
   19413          124 :   line_map_uint_t macro_num = 0;
   19414          248 :   for (auto iter = macro_loc_remap->end (), begin = macro_loc_remap->begin ();
   19415       161312 :        iter-- != begin;)
   19416              :     {
   19417       161188 :       auto mac = iter->src;
   19418       161188 :       sec.loc (iter->remap);
   19419       161188 :       sec.u (mac->n_tokens);
   19420       161188 :       sec.cpp_node (mac->macro);
   19421       161188 :       write_location (sec, mac->m_expansion);
   19422       161188 :       const location_t *locs = mac->macro_locations;
   19423              :       /* There are lots of identical runs.  */
   19424       161188 :       location_t prev = UNKNOWN_LOCATION;
   19425       161188 :       unsigned count = 0;
   19426       161188 :       unsigned runs = 0;
   19427      6026264 :       for (unsigned jx = mac->n_tokens * 2; jx--;)
   19428              :         {
   19429      5865076 :           location_t tok_loc = locs[jx];
   19430      5865076 :           if (tok_loc == prev)
   19431              :             {
   19432      2769363 :               count++;
   19433      2769363 :               continue;
   19434              :             }
   19435      3095713 :           runs++;
   19436      3095713 :           sec.u (count);
   19437      3095713 :           count = 1;
   19438      3095713 :           prev = tok_loc;
   19439      3095713 :           write_location (sec, tok_loc);
   19440              :         }
   19441       161188 :       sec.u (count);
   19442       161188 :       dump (dumper::LOCATION)
   19443            9 :         && dump ("Macro:%K %I %u/%u*2 locations [%K,%K)->%K",
   19444            9 :                  macro_num, identifier (mac->macro),
   19445              :                  runs, mac->n_tokens,
   19446              :                  MAP_START_LOCATION (mac),
   19447            9 :                  MAP_START_LOCATION (mac) + mac->n_tokens,
   19448              :                  iter->remap);
   19449       161188 :       macro_num++;
   19450              :     }
   19451          124 :   gcc_assert (macro_num == info.second);
   19452              : 
   19453          124 :   sec.end (to, to->name (MOD_SNAME_PFX ".mlm"), crc_p);
   19454          124 :   dump.outdent ();
   19455          124 : }
   19456              : 
   19457              : bool
   19458         2933 : module_state::read_ordinary_maps (line_map_uint_t num_ord_locs,
   19459              :                                   unsigned range_bits)
   19460              : {
   19461         2933 :   bytes_in sec;
   19462              : 
   19463         2933 :   if (!sec.begin (loc, from (), MOD_SNAME_PFX ".olm"))
   19464              :     return false;
   19465         3450 :   dump () && dump ("Reading ordinary location maps");
   19466         2933 :   dump.indent ();
   19467              : 
   19468              :   /* Read the filename table.  */
   19469         2933 :   unsigned len = sec.u ();
   19470         3450 :   dump () && dump ("%u source file names", len);
   19471         2933 :   vec<const char *> filenames;
   19472         2933 :   filenames.create (len);
   19473        19933 :   for (unsigned ix = 0; ix != len; ix++)
   19474              :     {
   19475        17000 :       size_t l;
   19476        17000 :       const char *buf = sec.str (&l);
   19477        17000 :       char *fname = XNEWVEC (char, l + 1);
   19478        17000 :       memcpy (fname, buf, l + 1);
   19479        17000 :       dump (dumper::LOCATION) && dump ("Source file[%u]=%s", ix, fname);
   19480              :       /* We leak these names into the line-map table.  But it
   19481              :          doesn't own them.  */
   19482        17000 :       filenames.quick_push (fname);
   19483              :     }
   19484              : 
   19485         2933 :   line_map_uint_t num_ordinary = sec.loc ();
   19486         3450 :   dump () && dump ("Ordinary maps:%K, range_bits:%u",
   19487              :                    num_ordinary, range_bits);
   19488              : 
   19489         2933 :   location_t offset = line_table->highest_location + 1;
   19490         2933 :   offset += ((loc_one << range_bits) - 1);
   19491         2933 :   offset &= ~((loc_one << range_bits) - 1);
   19492         2933 :   ordinary_locs.first = offset;
   19493              : 
   19494         2933 :   bool propagated = spans.maybe_propagate (this, offset);
   19495         2933 :   line_map_ordinary *maps = static_cast<line_map_ordinary *>
   19496         2933 :     (line_map_new_raw (line_table, false, num_ordinary));
   19497              : 
   19498         2933 :   const line_map_ordinary *base = nullptr;
   19499       305278 :   for (line_map_uint_t ix = 0; ix != num_ordinary && !sec.get_overrun (); ix++)
   19500              :     {
   19501       302345 :       line_map_ordinary *map = &maps[ix];
   19502              : 
   19503       302345 :       location_t offset = sec.loc ();
   19504       302345 :       if (!offset)
   19505              :         {
   19506        33247 :           map->reason = LC_RENAME;
   19507        33247 :           map->sysp = sec.u ();
   19508        33247 :           map->m_range_bits = sec.u ();
   19509        33247 :           map->m_column_and_range_bits = sec.u () + map->m_range_bits;
   19510        33247 :           unsigned fnum = sec.u ();
   19511        66494 :           map->to_file = (fnum < filenames.length () ? filenames[fnum] : "");
   19512        33247 :           map->to_line = sec.u ();
   19513        33247 :           base = map;
   19514              :         }
   19515              :       else
   19516              :         {
   19517       269098 :           *map = *base;
   19518       269098 :           map->to_line += offset >> map->m_column_and_range_bits;
   19519              :         }
   19520       302345 :       location_t remap = sec.loc ();
   19521       302345 :       map->start_location = remap + ordinary_locs.first;
   19522       302345 :       if (base == map)
   19523              :         {
   19524              :           /* Root the outermost map at our location.  */
   19525        33247 :           ordinary_locs.second = remap;
   19526        33247 :           location_t from = read_location (sec);
   19527        33247 :           map->included_from = from != UNKNOWN_LOCATION ? from : loc;
   19528              :         }
   19529              :     }
   19530              : 
   19531         2933 :   ordinary_locs.second = num_ord_locs;
   19532              :   /* highest_location is the one handed out, not the next one to
   19533              :      hand out.  */
   19534         2933 :   line_table->highest_location = ordinary_locs.first + ordinary_locs.second - 1;
   19535              : 
   19536         2933 :   if (line_table->highest_location >= LINE_MAP_MAX_LOCATION_WITH_COLS)
   19537              :     /* We shouldn't run out of locations, as we checked before
   19538              :        starting.  */
   19539            0 :     sec.set_overrun ();
   19540         3450 :   dump () && dump ("Ordinary location [%K,+%K)",
   19541              :                    ordinary_locs.first, ordinary_locs.second);
   19542              : 
   19543         2933 :   if (propagated)
   19544          169 :     spans.close ();
   19545              : 
   19546         2933 :   filenames.release ();
   19547              : 
   19548         2933 :   dump.outdent ();
   19549         2933 :   if (!sec.end (from ()))
   19550              :     return false;
   19551              : 
   19552              :   return true;
   19553         2933 : }
   19554              : 
   19555              : bool
   19556          133 : module_state::read_macro_maps (line_map_uint_t num_macro_locs)
   19557              : {
   19558          133 :   bytes_in sec;
   19559              : 
   19560          133 :   if (!sec.begin (loc, from (), MOD_SNAME_PFX ".mlm"))
   19561              :     return false;
   19562          142 :   dump () && dump ("Reading macro location maps");
   19563          133 :   dump.indent ();
   19564              : 
   19565          133 :   line_map_uint_t num_macros = sec.loc ();
   19566          142 :   dump () && dump ("Macro maps:%K locs:%K",
   19567              :                    num_macros, num_macro_locs);
   19568              : 
   19569          266 :   bool propagated = spans.maybe_propagate (this,
   19570          133 :                                            line_table->highest_location + 1);
   19571              : 
   19572          133 :   location_t offset = LINEMAPS_MACRO_LOWEST_LOCATION (line_table);
   19573          133 :   macro_locs.second = num_macro_locs;
   19574          133 :   macro_locs.first = offset - num_macro_locs;
   19575              : 
   19576          142 :   dump () && dump ("Macro loc delta %K", offset);
   19577          142 :   dump () && dump ("Macro locations [%K,%K)",
   19578              :                    macro_locs.first, macro_locs.second);
   19579              : 
   19580       204787 :   for (line_map_uint_t ix = 0; ix != num_macros && !sec.get_overrun (); ix++)
   19581              :     {
   19582       204654 :       location_t offset = sec.loc ();
   19583       204654 :       unsigned n_tokens = sec.u ();
   19584       204654 :       cpp_hashnode *node = sec.cpp_node ();
   19585       204654 :       location_t exp_loc = read_location (sec);
   19586              : 
   19587       204654 :       const line_map_macro *macro
   19588       204654 :         = linemap_enter_macro (line_table, node, exp_loc, n_tokens);
   19589       204654 :       if (!macro)
   19590              :         /* We shouldn't run out of locations, as we checked that we
   19591              :            had enough before starting.  */
   19592              :         break;
   19593       204654 :       gcc_checking_assert (MAP_START_LOCATION (macro)
   19594              :                            == offset + macro_locs.first);
   19595              : 
   19596       204654 :       location_t *locs = macro->macro_locations;
   19597       204654 :       location_t tok_loc = UNKNOWN_LOCATION;
   19598       204654 :       unsigned count = sec.u ();
   19599       204654 :       unsigned runs = 0;
   19600      7502784 :       for (unsigned jx = macro->n_tokens * 2; jx-- && !sec.get_overrun ();)
   19601              :         {
   19602     11160986 :           while (!count-- && !sec.get_overrun ())
   19603              :             {
   19604      3862856 :               runs++;
   19605      3862856 :               tok_loc = read_location (sec);
   19606      3862856 :               count = sec.u ();
   19607              :             }
   19608      7298130 :           locs[jx] = tok_loc;
   19609              :         }
   19610       204654 :       if (count)
   19611            0 :         sec.set_overrun ();
   19612       204684 :       dump (dumper::LOCATION)
   19613            0 :         && dump ("Macro:%K %I %u/%u*2 locations [%K,%K)",
   19614              :                  ix, identifier (node), runs, n_tokens,
   19615              :                  MAP_START_LOCATION (macro),
   19616            0 :                  MAP_START_LOCATION (macro) + n_tokens);
   19617              :     }
   19618              : 
   19619          142 :   dump () && dump ("Macro location lwm:%K", macro_locs.first);
   19620          133 :   if (propagated)
   19621            3 :     spans.close ();
   19622              : 
   19623          133 :   dump.outdent ();
   19624          133 :   if (!sec.end (from ()))
   19625              :     return false;
   19626              : 
   19627              :   return true;
   19628          133 : }
   19629              : 
   19630              : /* Serialize the definition of MACRO.  */
   19631              : 
   19632              : void
   19633        78287 : module_state::write_define (bytes_out &sec, const cpp_macro *macro)
   19634              : {
   19635        78287 :   sec.u (macro->count);
   19636              : 
   19637        78287 :   bytes_out::bits_out bits = sec.stream_bits ();
   19638        78287 :   bits.b (macro->fun_like);
   19639        78287 :   bits.b (macro->variadic);
   19640        78287 :   bits.b (macro->syshdr);
   19641        78287 :   bits.bflush ();
   19642              : 
   19643        78287 :   write_location (sec, macro->line);
   19644        78287 :   if (macro->fun_like)
   19645              :     {
   19646         9800 :       sec.u (macro->paramc);
   19647         9800 :       const cpp_hashnode *const *parms = macro->parm.params;
   19648        24668 :       for (unsigned ix = 0; ix != macro->paramc; ix++)
   19649        14868 :         sec.cpp_node (parms[ix]);
   19650              :     }
   19651              : 
   19652              :   unsigned len = 0;
   19653       250182 :   for (unsigned ix = 0; ix != macro->count; ix++)
   19654              :     {
   19655       171895 :       const cpp_token *token = &macro->exp.tokens[ix];
   19656       171895 :       write_location (sec, token->src_loc);
   19657       171895 :       sec.u (token->type);
   19658       171895 :       sec.u (token->flags);
   19659       171895 :       switch (cpp_token_val_index (token))
   19660              :         {
   19661            0 :         default:
   19662            0 :           gcc_unreachable ();
   19663              : 
   19664        13064 :         case CPP_TOKEN_FLD_ARG_NO:
   19665              :           /* An argument reference.  */
   19666        13064 :           sec.u (token->val.macro_arg.arg_no);
   19667        13064 :           sec.cpp_node (token->val.macro_arg.spelling);
   19668        13064 :           break;
   19669              : 
   19670        33498 :         case CPP_TOKEN_FLD_NODE:
   19671              :           /* An identifier.  */
   19672        33498 :           sec.cpp_node (token->val.node.node);
   19673        33498 :           if (token->val.node.spelling == token->val.node.node)
   19674              :             /* The spelling will usually be the same.  so optimize
   19675              :                that.  */
   19676        33498 :             sec.str (NULL, 0);
   19677              :           else
   19678            0 :             sec.cpp_node (token->val.node.spelling);
   19679              :           break;
   19680              : 
   19681              :         case CPP_TOKEN_FLD_NONE:
   19682              :           break;
   19683              : 
   19684        55980 :         case CPP_TOKEN_FLD_STR:
   19685              :           /* A string, number or comment.  Not always NUL terminated,
   19686              :              we stream out in a single concatenation with embedded
   19687              :              NULs as that's a safe default.  */
   19688        55980 :           len += token->val.str.len + 1;
   19689        55980 :           sec.u (token->val.str.len);
   19690        55980 :           break;
   19691              : 
   19692            0 :         case CPP_TOKEN_FLD_SOURCE:
   19693            0 :         case CPP_TOKEN_FLD_TOKEN_NO:
   19694            0 :         case CPP_TOKEN_FLD_PRAGMA:
   19695              :           /* These do not occur inside a macro itself.  */
   19696            0 :           gcc_unreachable ();
   19697              :         }
   19698              :     }
   19699              : 
   19700        78287 :   if (len)
   19701              :     {
   19702        52359 :       char *ptr = reinterpret_cast<char *> (sec.buf (len));
   19703        52359 :       len = 0;
   19704       155630 :       for (unsigned ix = 0; ix != macro->count; ix++)
   19705              :         {
   19706       103271 :           const cpp_token *token = &macro->exp.tokens[ix];
   19707       103271 :           if (cpp_token_val_index (token) == CPP_TOKEN_FLD_STR)
   19708              :             {
   19709        55980 :               memcpy (ptr + len, token->val.str.text,
   19710        55980 :                       token->val.str.len);
   19711        55980 :               len += token->val.str.len;
   19712        55980 :               ptr[len++] = 0;
   19713              :             }
   19714              :         }
   19715              :     }
   19716        78287 : }
   19717              : 
   19718              : /* Read a macro definition.  */
   19719              : 
   19720              : cpp_macro *
   19721          799 : module_state::read_define (bytes_in &sec, cpp_reader *reader) const
   19722              : {
   19723          799 :   unsigned count = sec.u ();
   19724              :   /* We rely on knowing cpp_reader's hash table is ident_hash, and
   19725              :      its subobject allocator is stringpool_ggc_alloc and that is just
   19726              :      a wrapper for ggc_alloc_atomic.  */
   19727          799 :   cpp_macro *macro
   19728         1598 :     = (cpp_macro *)ggc_alloc_atomic (sizeof (cpp_macro)
   19729          799 :                                      + sizeof (cpp_token) * (count - !!count));
   19730          799 :   memset (macro, 0, sizeof (cpp_macro) + sizeof (cpp_token) * (count - !!count));
   19731              : 
   19732          799 :   macro->count = count;
   19733          799 :   macro->kind = cmk_macro;
   19734          799 :   macro->imported_p = true;
   19735              : 
   19736          799 :   bytes_in::bits_in bits = sec.stream_bits ();
   19737          799 :   macro->fun_like = bits.b ();
   19738          799 :   macro->variadic = bits.b ();
   19739          799 :   macro->syshdr = bits.b ();
   19740          799 :   bits.bflush ();
   19741              : 
   19742          799 :   macro->line = read_location (sec);
   19743              : 
   19744          799 :   if (macro->fun_like)
   19745              :     {
   19746           83 :       unsigned paramc = sec.u ();
   19747           83 :       cpp_hashnode **params
   19748           83 :         = (cpp_hashnode **)ggc_alloc_atomic (sizeof (cpp_hashnode *) * paramc);
   19749           83 :       macro->paramc = paramc;
   19750           83 :       macro->parm.params = params;
   19751          177 :       for (unsigned ix = 0; ix != paramc; ix++)
   19752           94 :         params[ix] = sec.cpp_node ();
   19753              :     }
   19754              : 
   19755              :   unsigned len = 0;
   19756         2210 :   for (unsigned ix = 0; ix != count && !sec.get_overrun (); ix++)
   19757              :     {
   19758         1411 :       cpp_token *token = &macro->exp.tokens[ix];
   19759         1411 :       token->src_loc = read_location (sec);
   19760         1411 :       token->type = cpp_ttype (sec.u ());
   19761         1411 :       token->flags = sec.u ();
   19762         1411 :       switch (cpp_token_val_index (token))
   19763              :         {
   19764            0 :         default:
   19765            0 :           sec.set_overrun ();
   19766            0 :           break;
   19767              : 
   19768           77 :         case CPP_TOKEN_FLD_ARG_NO:
   19769              :           /* An argument reference.  */
   19770           77 :           {
   19771           77 :             unsigned arg_no = sec.u ();
   19772           77 :             if (arg_no - 1 >= macro->paramc)
   19773            0 :               sec.set_overrun ();
   19774           77 :             token->val.macro_arg.arg_no = arg_no;
   19775           77 :             token->val.macro_arg.spelling = sec.cpp_node ();
   19776              :           }
   19777           77 :           break;
   19778              : 
   19779          271 :         case CPP_TOKEN_FLD_NODE:
   19780              :           /* An identifier.  */
   19781          271 :           token->val.node.node = sec.cpp_node ();
   19782          271 :           token->val.node.spelling = sec.cpp_node ();
   19783          271 :           if (!token->val.node.spelling)
   19784          271 :             token->val.node.spelling = token->val.node.node;
   19785              :           break;
   19786              : 
   19787              :         case CPP_TOKEN_FLD_NONE:
   19788              :           break;
   19789              : 
   19790          622 :         case CPP_TOKEN_FLD_STR:
   19791              :           /* A string, number or comment.  */
   19792          622 :           token->val.str.len = sec.u ();
   19793          622 :           len += token->val.str.len + 1;
   19794          622 :           break;
   19795              :         }
   19796              :     }
   19797              : 
   19798          799 :   if (len)
   19799          619 :     if (const char *ptr = reinterpret_cast<const char *> (sec.buf (len)))
   19800              :       {
   19801              :         /* There should be a final NUL.  */
   19802          619 :         if (ptr[len-1])
   19803            0 :           sec.set_overrun ();
   19804              :         /* cpp_alloc_token_string will add a final NUL.  */
   19805          619 :         const unsigned char *buf
   19806          619 :           = cpp_alloc_token_string (reader, (const unsigned char *)ptr, len - 1);
   19807          619 :         len = 0;
   19808         1561 :         for (unsigned ix = 0; ix != count && !sec.get_overrun (); ix++)
   19809              :           {
   19810          942 :             cpp_token *token = &macro->exp.tokens[ix];
   19811          942 :             if (cpp_token_val_index (token) == CPP_TOKEN_FLD_STR)
   19812              :               {
   19813          622 :                 token->val.str.text = buf + len;
   19814          622 :                 len += token->val.str.len;
   19815          622 :                 if (buf[len++])
   19816            0 :                   sec.set_overrun ();
   19817              :               }
   19818              :           }
   19819              :       }
   19820              : 
   19821          799 :   if (sec.get_overrun ())
   19822            0 :     return NULL;
   19823              :   return macro;
   19824          799 : }
   19825              : 
   19826              : /* Exported macro data.  */
   19827              : struct GTY(()) macro_export {
   19828              :   cpp_macro *def;
   19829              :   location_t undef_loc;
   19830              : 
   19831       110193 :   macro_export ()
   19832       110193 :     :def (NULL), undef_loc (UNKNOWN_LOCATION)
   19833              :   {
   19834              :   }
   19835              : };
   19836              : 
   19837              : /* Imported macro data.  */
   19838              : class macro_import {
   19839              : public:
   19840              :   struct slot {
   19841              : #if defined (WORDS_BIGENDIAN) && SIZEOF_VOID_P == 8
   19842              :     int offset;
   19843              : #endif
   19844              :     /* We need to ensure we don't use the LSB for representation, as
   19845              :        that's the union discriminator below.  */
   19846              :     unsigned bits;
   19847              : 
   19848              : #if !(defined (WORDS_BIGENDIAN) && SIZEOF_VOID_P == 8)
   19849              :     int offset;
   19850              : #endif
   19851              : 
   19852              :   public:
   19853              :     enum Layout {
   19854              :       L_DEF = 1,
   19855              :       L_UNDEF = 2,
   19856              :       L_BOTH = 3,
   19857              :       L_MODULE_SHIFT = 2
   19858              :     };
   19859              : 
   19860              :   public:
   19861              :     /* Not a regular ctor, because we put it in a union, and that's
   19862              :        not allowed in C++ 98.  */
   19863       196678 :     static slot ctor (unsigned module, unsigned defness)
   19864              :     {
   19865       196678 :       gcc_checking_assert (defness);
   19866       196678 :       slot s;
   19867       196678 :       s.bits = defness | (module << L_MODULE_SHIFT);
   19868       196678 :       s.offset = -1;
   19869       196678 :       return s;
   19870              :     }
   19871              : 
   19872              :   public:
   19873       159563 :     unsigned get_defness () const
   19874              :     {
   19875       159563 :       return bits & L_BOTH;
   19876              :     }
   19877       113007 :     unsigned get_module () const
   19878              :     {
   19879       113007 :       return bits >> L_MODULE_SHIFT;
   19880              :     }
   19881           12 :     void become_undef ()
   19882              :     {
   19883           12 :       bits &= ~unsigned (L_DEF);
   19884           12 :       bits |= unsigned (L_UNDEF);
   19885              :     }
   19886              :   };
   19887              : 
   19888              : private:
   19889              :   typedef vec<slot, va_heap, vl_embed> ary_t;
   19890              :   union either {
   19891              :     /* Discriminated by bits 0|1 != 0.  The expected case is that
   19892              :        there will be exactly one slot per macro, hence the effort of
   19893              :        packing that.  */
   19894              :     ary_t *ary;
   19895              :     slot single;
   19896              :   } u;
   19897              : 
   19898              : public:
   19899       160536 :   macro_import ()
   19900       160536 :   {
   19901       160536 :     u.ary = NULL;
   19902              :   }
   19903              : 
   19904              : private:
   19905      8641517 :   bool single_p () const
   19906              :   {
   19907      8641517 :     return u.single.bits & slot::L_BOTH;
   19908              :   }
   19909      8802187 :   bool occupied_p () const
   19910              :   {
   19911      8802187 :     return u.ary != NULL;
   19912              :   }
   19913              : 
   19914              : public:
   19915         2355 :   unsigned length () const
   19916              :   {
   19917         2355 :     gcc_checking_assert (occupied_p ());
   19918         2355 :     return single_p () ? 1 : u.ary->length ();
   19919              :   }
   19920      8493632 :   slot &operator[] (unsigned ix)
   19921              :   {
   19922      8493632 :     gcc_checking_assert (occupied_p ());
   19923      8493632 :     if (single_p ())
   19924              :       {
   19925      8387325 :         gcc_checking_assert (!ix);
   19926      8387325 :         return u.single;
   19927              :       }
   19928              :     else
   19929       106307 :       return (*u.ary)[ix];
   19930              :   }
   19931              : 
   19932              : public:
   19933              :   slot &exported ();
   19934              :   slot &append (unsigned module, unsigned defness);
   19935              : };
   19936              : 
   19937              : /* O is a new import to append to the list for.  If we're an empty
   19938              :    set, initialize us.  */
   19939              : 
   19940              : macro_import::slot &
   19941       196678 : macro_import::append (unsigned module, unsigned defness)
   19942              : {
   19943       196678 :   if (!occupied_p ())
   19944              :     {
   19945       160536 :       u.single = slot::ctor (module, defness);
   19946       160536 :       return u.single;
   19947              :     }
   19948              :   else
   19949              :     {
   19950        36142 :       bool single = single_p ();
   19951        36142 :       ary_t *m = single ? NULL : u.ary;
   19952        36142 :       vec_safe_reserve (m, 1 + single);
   19953        36142 :       if (single)
   19954        36139 :         m->quick_push (u.single);
   19955        36142 :       u.ary = m;
   19956        36142 :       return *u.ary->quick_push (slot::ctor (module, defness));
   19957              :     }
   19958              : }
   19959              : 
   19960              : /* We're going to export something.  Make sure the first import slot
   19961              :    is us.  */
   19962              : 
   19963              : macro_import::slot &
   19964       109522 : macro_import::exported ()
   19965              : {
   19966       109522 :   if (occupied_p () && !(*this)[0].get_module ())
   19967              :     {
   19968          134 :       slot &res = (*this)[0];
   19969          134 :       res.bits |= slot::L_DEF;
   19970          134 :       return res;
   19971              :     }
   19972              : 
   19973       109388 :   slot *a = &append (0, slot::L_DEF);
   19974       109388 :   if (!single_p ())
   19975              :     {
   19976        31807 :       slot &f = (*this)[0];
   19977        31807 :       std::swap (f, *a);
   19978        31807 :       a = &f;
   19979              :     }
   19980              :   return *a;
   19981              : }
   19982              : 
   19983              : /* The import (&exported) macros.  cpp_hasnode's deferred field
   19984              :    indexes this array (offset by 1, so zero means 'not present'.  */
   19985              : 
   19986              : static vec<macro_import, va_heap, vl_embed> *macro_imports;
   19987              : 
   19988              : /* The exported macros.  A macro_import slot's zeroth element's offset
   19989              :    indexes this array.  If the zeroth slot is not for module zero,
   19990              :    there is no export.  */
   19991              : 
   19992              : static GTY(()) vec<macro_export, va_gc> *macro_exports;
   19993              : 
   19994              : /* The reachable set of header imports from this TU.  */
   19995              : 
   19996              : static GTY(()) bitmap headers;
   19997              : 
   19998              : /* Get the (possibly empty) macro imports for NODE.  */
   19999              : 
   20000              : static macro_import &
   20001       165005 : get_macro_imports (cpp_hashnode *node)
   20002              : {
   20003       165005 :   if (node->deferred)
   20004         4469 :     return (*macro_imports)[node->deferred - 1];
   20005              : 
   20006       160536 :   vec_safe_reserve (macro_imports, 1);
   20007       160536 :   node->deferred = macro_imports->length () + 1;
   20008       160536 :   return *vec_safe_push (macro_imports, macro_import ());
   20009              : }
   20010              : 
   20011              : /* Get the macro export for export EXP of NODE.  */
   20012              : 
   20013              : static macro_export &
   20014       109522 : get_macro_export (macro_import::slot &slot)
   20015              : {
   20016       109522 :   if (slot.offset >= 0)
   20017          134 :     return (*macro_exports)[slot.offset];
   20018              : 
   20019       109388 :   vec_safe_reserve (macro_exports, 1);
   20020       109388 :   slot.offset = macro_exports->length ();
   20021       109388 :   return *macro_exports->quick_push (macro_export ());
   20022              : }
   20023              : 
   20024              : /* If NODE is an exportable macro, add it to the export set.  */
   20025              : 
   20026              : static int
   20027      3967986 : maybe_add_macro (cpp_reader *, cpp_hashnode *node, void *data_)
   20028              : {
   20029      3967986 :   bool exporting = false;
   20030              : 
   20031      3967986 :   if (cpp_user_macro_p (node))
   20032       510965 :     if (cpp_macro *macro = node->value.macro)
   20033              :       /* Ignore imported, builtins, command line and forced header macros.  */
   20034       510509 :       if (!macro->imported_p
   20035       510509 :           && !macro->lazy && macro->line >= spans.main_start ())
   20036              :         {
   20037        77715 :           gcc_checking_assert (macro->kind == cmk_macro);
   20038              :           /* I don't want to deal with this corner case, that I suspect is
   20039              :              a devil's advocate reading of the standard.  */
   20040        77715 :           gcc_checking_assert (!macro->extra_tokens);
   20041              : 
   20042        77715 :           macro_import::slot &slot = get_macro_imports (node).exported ();
   20043        77715 :           macro_export &exp = get_macro_export (slot);
   20044        77715 :           exp.def = macro;
   20045        77715 :           exporting = true;
   20046              :         }
   20047              : 
   20048      3890271 :   if (!exporting && node->deferred)
   20049              :     {
   20050          610 :       macro_import &imports = (*macro_imports)[node->deferred - 1];
   20051          610 :       macro_import::slot &slot = imports[0];
   20052          610 :       if (!slot.get_module ())
   20053              :         {
   20054          579 :           gcc_checking_assert (slot.get_defness ());
   20055              :           exporting = true;
   20056              :         }
   20057              :     }
   20058              : 
   20059        77715 :   if (exporting)
   20060        78294 :     static_cast<vec<cpp_hashnode *> *> (data_)->safe_push (node);
   20061              : 
   20062      3967986 :   return 1; /* Don't stop.  */
   20063              : }
   20064              : 
   20065              : /* Order cpp_hashnodes A_ and B_ by their exported macro locations.  */
   20066              : 
   20067              : static int
   20068      4095728 : macro_loc_cmp (const void *a_, const void *b_)
   20069              : {
   20070      4095728 :   const cpp_hashnode *node_a = *(const cpp_hashnode *const *)a_;
   20071      4095728 :   macro_import &import_a = (*macro_imports)[node_a->deferred - 1];
   20072      4095728 :   const macro_export &export_a = (*macro_exports)[import_a[0].offset];
   20073      4095728 :   location_t loc_a = export_a.def ? export_a.def->line : export_a.undef_loc;
   20074              : 
   20075      4095728 :   const cpp_hashnode *node_b = *(const cpp_hashnode *const *)b_;
   20076      4095728 :   macro_import &import_b = (*macro_imports)[node_b->deferred - 1];
   20077      4095728 :   const macro_export &export_b = (*macro_exports)[import_b[0].offset];
   20078      4095728 :   location_t loc_b = export_b.def ? export_b.def->line : export_b.undef_loc;
   20079              : 
   20080      4095728 :   if (loc_a < loc_b)
   20081              :     return +1;
   20082      2104521 :   else if (loc_a > loc_b)
   20083              :     return -1;
   20084              :   else
   20085            0 :     return 0;
   20086              : }
   20087              : 
   20088              : /* Gather the macro definitions and undefinitions that we will need to
   20089              :    write out.   */
   20090              : 
   20091              : vec<cpp_hashnode *> *
   20092          903 : module_state::prepare_macros (cpp_reader *reader)
   20093              : {
   20094          903 :   vec<cpp_hashnode *> *macros;
   20095          903 :   vec_alloc (macros, 100);
   20096              : 
   20097          903 :   cpp_forall_identifiers (reader, maybe_add_macro, macros);
   20098              : 
   20099          927 :   dump (dumper::MACRO) && dump ("No more than %u macros", macros->length ());
   20100              : 
   20101          903 :   macros->qsort (macro_loc_cmp);
   20102              : 
   20103              :   // Note the locations.
   20104        80100 :   for (unsigned ix = macros->length (); ix--;)
   20105              :     {
   20106        78294 :       cpp_hashnode *node = (*macros)[ix];
   20107        78294 :       macro_import::slot &slot = (*macro_imports)[node->deferred - 1][0];
   20108        78294 :       macro_export &mac = (*macro_exports)[slot.offset];
   20109              : 
   20110        78294 :       if (IDENTIFIER_KEYWORD_P (identifier (node)))
   20111            1 :         continue;
   20112              : 
   20113        78293 :       if (mac.undef_loc != UNKNOWN_LOCATION)
   20114           12 :         note_location (mac.undef_loc);
   20115        78293 :       if (mac.def)
   20116              :         {
   20117        78287 :           note_location (mac.def->line);
   20118       250182 :           for (unsigned ix = 0; ix != mac.def->count; ix++)
   20119       171895 :             note_location (mac.def->exp.tokens[ix].src_loc);
   20120              :         }
   20121              :     }
   20122              : 
   20123          903 :   return macros;
   20124              : }
   20125              : 
   20126              : /* Write out the exported defines.  This is two sections, one
   20127              :    containing the definitions, the other a table of node names.  */
   20128              : 
   20129              : unsigned
   20130          903 : module_state::write_macros (elf_out *to, vec<cpp_hashnode *> *macros,
   20131              :                             unsigned *crc_p)
   20132              : {
   20133          970 :   dump () && dump ("Writing macros");
   20134          903 :   dump.indent ();
   20135              : 
   20136              :   /* Write the defs */
   20137          903 :   bytes_out sec (to);
   20138          903 :   sec.begin ();
   20139              : 
   20140          903 :   unsigned count = 0;
   20141        80100 :   for (unsigned ix = macros->length (); ix--;)
   20142              :     {
   20143        78294 :       cpp_hashnode *node = (*macros)[ix];
   20144        78294 :       macro_import::slot &slot = (*macro_imports)[node->deferred - 1][0];
   20145        78294 :       gcc_assert (!slot.get_module () && slot.get_defness ());
   20146              : 
   20147        78294 :       macro_export &mac = (*macro_exports)[slot.offset];
   20148        78294 :       gcc_assert (!!(slot.get_defness () & macro_import::slot::L_UNDEF)
   20149              :                   == (mac.undef_loc != UNKNOWN_LOCATION)
   20150              :                   && !!(slot.get_defness () & macro_import::slot::L_DEF)
   20151              :                   == (mac.def != NULL));
   20152              : 
   20153        78294 :       if (IDENTIFIER_KEYWORD_P (identifier (node)))
   20154              :         {
   20155            1 :           warning_at (mac.def->line, 0,
   20156              :                       "not exporting %<#define %E%> as it is a keyword",
   20157              :                       identifier (node));
   20158            1 :           slot.offset = 0;
   20159            1 :           continue;
   20160              :         }
   20161              : 
   20162        78293 :       count++;
   20163        78293 :       slot.offset = sec.pos;
   20164        78293 :       dump (dumper::MACRO)
   20165           24 :         && dump ("Writing macro %s%s%s %I at %u",
   20166           24 :                  slot.get_defness () & macro_import::slot::L_UNDEF
   20167              :                  ? "#undef" : "",
   20168           24 :                  slot.get_defness () == macro_import::slot::L_BOTH
   20169              :                  ? " & " : "",
   20170           24 :                  slot.get_defness () & macro_import::slot::L_DEF
   20171              :                  ? "#define" : "",
   20172              :                  identifier (node), slot.offset);
   20173        78293 :       if (mac.undef_loc != UNKNOWN_LOCATION)
   20174           12 :         write_location (sec, mac.undef_loc);
   20175        78293 :       if (mac.def)
   20176        78287 :         write_define (sec, mac.def);
   20177              :     }
   20178          903 :   if (count)
   20179              :     // We may have ended on a tokenless macro with a very short
   20180              :     // location, that will cause problems reading its bit flags.
   20181          145 :     sec.u (0);
   20182          903 :   sec.end (to, to->name (MOD_SNAME_PFX ".def"), crc_p);
   20183              : 
   20184          903 :   if (count)
   20185              :     {
   20186              :       /* Write the table.  */
   20187          145 :       bytes_out sec (to);
   20188          145 :       sec.begin ();
   20189          145 :       sec.u (count);
   20190              : 
   20191        78583 :       for (unsigned ix = macros->length (); ix--;)
   20192              :         {
   20193        78293 :           const cpp_hashnode *node = (*macros)[ix];
   20194        78293 :           macro_import::slot &slot = (*macro_imports)[node->deferred - 1][0];
   20195              : 
   20196        78293 :           if (slot.offset)
   20197              :             {
   20198        78293 :               sec.cpp_node (node);
   20199        78293 :               sec.u (slot.get_defness ());
   20200        78293 :               sec.u (slot.offset);
   20201              :             }
   20202              :         }
   20203          145 :       sec.end (to, to->name (MOD_SNAME_PFX ".mac"), crc_p);
   20204          145 :     }
   20205              : 
   20206          903 :   dump.outdent ();
   20207          903 :   return count;
   20208          903 : }
   20209              : 
   20210              : bool
   20211          935 : module_state::read_macros ()
   20212              : {
   20213              :   /* Get the def section.  */
   20214          935 :   if (!slurp->macro_defs.begin (loc, from (), MOD_SNAME_PFX ".def"))
   20215              :     return false;
   20216              : 
   20217              :   /* Get the tbl section, if there are defs. */
   20218          935 :   if (slurp->macro_defs.more_p ()
   20219          935 :       && !slurp->macro_tbl.begin (loc, from (), MOD_SNAME_PFX ".mac"))
   20220              :     return false;
   20221              : 
   20222              :   return true;
   20223              : }
   20224              : 
   20225              : /* Install the macro name table.  */
   20226              : 
   20227              : void
   20228          941 : module_state::install_macros ()
   20229              : {
   20230          941 :   bytes_in &sec = slurp->macro_tbl;
   20231          941 :   if (!sec.size)
   20232              :     return;
   20233              : 
   20234          204 :   dump () && dump ("Reading macro table %M", this);
   20235          182 :   dump.indent ();
   20236              : 
   20237          182 :   unsigned count = sec.u ();
   20238          204 :   dump () && dump ("%u macros", count);
   20239        87472 :   while (count--)
   20240              :     {
   20241        87290 :       cpp_hashnode *node = sec.cpp_node ();
   20242        87290 :       macro_import &imp = get_macro_imports (node);
   20243        87290 :       unsigned flags = sec.u () & macro_import::slot::L_BOTH;
   20244        87290 :       if (!flags)
   20245            0 :         sec.set_overrun ();
   20246              : 
   20247        87290 :       if (sec.get_overrun ())
   20248              :         break;
   20249              : 
   20250        87290 :       macro_import::slot &slot = imp.append (mod, flags);
   20251        87290 :       slot.offset = sec.u ();
   20252              : 
   20253        87290 :       dump (dumper::MACRO)
   20254           84 :         && dump ("Read %s macro %s%s%s %I at %u",
   20255           30 :                  imp.length () > 1 ? "add" : "new",
   20256           27 :                  flags & macro_import::slot::L_UNDEF ? "#undef" : "",
   20257              :                  flags == macro_import::slot::L_BOTH ? " & " : "",
   20258           30 :                  flags & macro_import::slot::L_DEF ? "#define" : "",
   20259              :                  identifier (node), slot.offset);
   20260              : 
   20261              :       /* We'll leak an imported definition's TOKEN_FLD_STR's data
   20262              :          here.  But that only happens when we've had to resolve the
   20263              :          deferred macro before this import -- why are you doing
   20264              :          that?  */
   20265        87290 :       if (cpp_macro *cur = cpp_set_deferred_macro (node))
   20266        31795 :         if (!cur->imported_p)
   20267              :           {
   20268        31795 :             macro_import::slot &slot = imp.exported ();
   20269        31795 :             macro_export &exp = get_macro_export (slot);
   20270        31795 :             exp.def = cur;
   20271       119267 :             dump (dumper::MACRO)
   20272            0 :               && dump ("Saving current #define %I", identifier (node));
   20273              :           }
   20274              :     }
   20275              : 
   20276              :   /* We're now done with the table.  */
   20277          182 :   elf_in::release (slurp->from, sec);
   20278              : 
   20279          182 :   dump.outdent ();
   20280              : }
   20281              : 
   20282              : /* Import the transitive macros.  */
   20283              : 
   20284              : void
   20285          899 : module_state::import_macros ()
   20286              : {
   20287          899 :   bitmap_ior_into (headers, slurp->headers);
   20288              : 
   20289          899 :   bitmap_iterator bititer;
   20290          899 :   unsigned bitnum;
   20291         1840 :   EXECUTE_IF_SET_IN_BITMAP (slurp->headers, 0, bitnum, bititer)
   20292          941 :     (*modules)[bitnum]->install_macros ();
   20293          899 : }
   20294              : 
   20295              : /* NODE is being undefined at LOC.  Record it in the export table, if
   20296              :    necessary.  */
   20297              : 
   20298              : void
   20299       317503 : module_state::undef_macro (cpp_reader *, location_t loc, cpp_hashnode *node)
   20300              : {
   20301       317503 :   if (!node->deferred)
   20302              :     /* The macro is not imported, so our undef is irrelevant.  */
   20303              :     return;
   20304              : 
   20305           12 :   unsigned n = dump.push (NULL);
   20306              : 
   20307           12 :   macro_import::slot &slot = (*macro_imports)[node->deferred - 1].exported ();
   20308           12 :   macro_export &exp = get_macro_export (slot);
   20309              : 
   20310           12 :   exp.undef_loc = loc;
   20311           12 :   slot.become_undef ();
   20312           12 :   exp.def = NULL;
   20313              : 
   20314           18 :   dump (dumper::MACRO) && dump ("Recording macro #undef %I", identifier (node));
   20315              : 
   20316           12 :   dump.pop (n);
   20317              : }
   20318              : 
   20319              : /* NODE is a deferred macro node.  Determine the definition and return
   20320              :    it, with NULL if undefined.  May issue diagnostics.
   20321              : 
   20322              :    This can leak memory, when merging declarations -- the string
   20323              :    contents (TOKEN_FLD_STR) of each definition are allocated in
   20324              :    unreclaimable cpp objstack.  Only one will win.  However, I do not
   20325              :    expect this to be common -- mostly macros have a single point of
   20326              :    definition.  Perhaps we could restore the objstack to its position
   20327              :    after the first imported definition (if that wins)?  The macros
   20328              :    themselves are GC'd.  */
   20329              : 
   20330              : cpp_macro *
   20331          775 : module_state::deferred_macro (cpp_reader *reader, location_t loc,
   20332              :                               cpp_hashnode *node)
   20333              : {
   20334          775 :   macro_import &imports = (*macro_imports)[node->deferred - 1];
   20335              : 
   20336          775 :   unsigned n = dump.push (NULL);
   20337          781 :   dump (dumper::MACRO) && dump ("Deferred macro %I", identifier (node));
   20338              : 
   20339          775 :   bitmap visible (BITMAP_GGC_ALLOC ());
   20340              : 
   20341          775 :   if (!((imports[0].get_defness () & macro_import::slot::L_UNDEF)
   20342            0 :         && !imports[0].get_module ()))
   20343              :     {
   20344              :       /* Calculate the set of visible header imports.  */
   20345          775 :       bitmap_copy (visible, headers);
   20346         1856 :       for (unsigned ix = imports.length (); ix--;)
   20347              :         {
   20348         1081 :           const macro_import::slot &slot = imports[ix];
   20349         1081 :           unsigned mod = slot.get_module ();
   20350         1081 :           if ((slot.get_defness () & macro_import::slot::L_UNDEF)
   20351         1081 :               && bitmap_bit_p (visible, mod))
   20352              :             {
   20353           12 :               bitmap arg = mod ? (*modules)[mod]->slurp->headers : headers;
   20354           12 :               bitmap_and_compl_into (visible, arg);
   20355           12 :               bitmap_set_bit (visible, mod);
   20356              :             }
   20357              :         }
   20358              :     }
   20359          775 :   bitmap_set_bit (visible, 0);
   20360              : 
   20361              :   /* Now find the macros that are still visible.  */
   20362          775 :   bool failed = false;
   20363          775 :   cpp_macro *def = NULL;
   20364          775 :   vec<macro_export> defs;
   20365          775 :   defs.create (imports.length ());
   20366         1856 :   for (unsigned ix = imports.length (); ix--;)
   20367              :     {
   20368         1081 :       const macro_import::slot &slot = imports[ix];
   20369         1081 :       unsigned mod = slot.get_module ();
   20370         1081 :       if (bitmap_bit_p (visible, mod))
   20371              :         {
   20372         1069 :           macro_export *pushed = NULL;
   20373         1069 :           if (mod)
   20374              :             {
   20375          805 :               const module_state *imp = (*modules)[mod];
   20376          805 :               bytes_in &sec = imp->slurp->macro_defs;
   20377          805 :               if (!sec.get_overrun ())
   20378              :                 {
   20379          805 :                   dump (dumper::MACRO)
   20380            6 :                     && dump ("Reading macro %s%s%s %I module %M at %u",
   20381            6 :                              slot.get_defness () & macro_import::slot::L_UNDEF
   20382              :                              ? "#undef" : "",
   20383            6 :                              slot.get_defness () == macro_import::slot::L_BOTH
   20384              :                              ? " & " : "",
   20385            6 :                              slot.get_defness () & macro_import::slot::L_DEF
   20386              :                              ? "#define" : "",
   20387            6 :                              identifier (node), imp, slot.offset);
   20388          805 :                   sec.random_access (slot.offset);
   20389              : 
   20390          805 :                   macro_export exp;
   20391          805 :                   if (slot.get_defness () & macro_import::slot::L_UNDEF)
   20392           12 :                     exp.undef_loc = imp->read_location (sec);
   20393          805 :                   if (slot.get_defness () & macro_import::slot::L_DEF)
   20394          799 :                     exp.def = imp->read_define (sec, reader);
   20395          805 :                   if (sec.get_overrun ())
   20396            0 :                     error_at (loc, "macro definitions of %qE corrupted",
   20397            0 :                               imp->name);
   20398              :                   else
   20399          805 :                     pushed = defs.quick_push (exp);
   20400              :                 }
   20401              :             }
   20402              :           else
   20403          264 :             pushed = defs.quick_push ((*macro_exports)[slot.offset]);
   20404         1069 :           if (pushed && pushed->def)
   20405              :             {
   20406         1063 :               if (!def)
   20407              :                 def = pushed->def;
   20408          291 :               else if (cpp_compare_macros (def, pushed->def))
   20409         1081 :                 failed = true;
   20410              :             }
   20411              :         }
   20412              :     }
   20413              : 
   20414          775 :   if (failed)
   20415              :     {
   20416              :       /* If LOC is the first loc, this is the end of file check, which
   20417              :          is a warning.  */
   20418           15 :       auto_diagnostic_group d;
   20419           15 :       if (loc == MAP_START_LOCATION (LINEMAPS_ORDINARY_MAP_AT (line_table, 0)))
   20420            9 :         warning_at (loc, OPT_Winvalid_imported_macros,
   20421              :                     "inconsistent imported macro definition %qE",
   20422              :                     identifier (node));
   20423              :       else
   20424            6 :         error_at (loc, "inconsistent imported macro definition %qE",
   20425              :                   identifier (node));
   20426           60 :       for (unsigned ix = defs.length (); ix--;)
   20427              :         {
   20428           30 :           macro_export &exp = defs[ix];
   20429           30 :           if (exp.undef_loc)
   20430            0 :             inform (exp.undef_loc, "%<#undef %E%>", identifier (node));
   20431           30 :           if (exp.def)
   20432           30 :             inform (exp.def->line, "%<#define %s%>",
   20433              :                     cpp_macro_definition (reader, node, exp.def));
   20434              :         }
   20435           15 :       def = NULL;
   20436           15 :     }
   20437              : 
   20438          775 :   defs.release ();
   20439              : 
   20440          775 :   dump.pop (n);
   20441              : 
   20442          775 :   return def;
   20443              : }
   20444              : 
   20445              : /* Stream the static aggregates.  Sadly some headers (ahem:
   20446              :    iostream) contain static vars, and rely on them to run global
   20447              :    ctors.  */
   20448              : unsigned
   20449          903 : module_state::write_inits (elf_out *to, depset::hash &table, unsigned *crc_ptr)
   20450              : {
   20451          903 :   if (!static_aggregates && !tls_aggregates)
   20452              :     return 0;
   20453              : 
   20454           45 :   dump () && dump ("Writing initializers");
   20455           45 :   dump.indent ();
   20456              : 
   20457           45 :   static_aggregates = nreverse (static_aggregates);
   20458           45 :   tls_aggregates = nreverse (tls_aggregates);
   20459              : 
   20460           45 :   unsigned count = 0;
   20461           45 :   trees_out sec (to, this, table, ~0u);
   20462           45 :   sec.begin ();
   20463              : 
   20464           45 :   tree list = static_aggregates;
   20465          135 :   for (int passes = 0; passes != 2; passes++)
   20466              :     {
   20467          258 :       for (tree init = list; init; init = TREE_CHAIN (init))
   20468          168 :         if (TREE_LANG_FLAG_0 (init))
   20469              :           {
   20470          144 :             if (STATIC_INIT_DECOMP_BASE_P (init))
   20471              :               {
   20472              :                 /* Ensure that in the returned result chain if the
   20473              :                    STATIC_INIT_DECOMP_*BASE_P flags are set, there is
   20474              :                    always one or more STATIC_INIT_DECOMP_BASE_P TREE_LIST
   20475              :                    followed by one or more STATIC_INIT_DECOMP_NONBASE_P.  */
   20476           21 :                 int phase = 0;
   20477           21 :                 tree last = NULL_TREE;
   20478           21 :                 for (tree init2 = TREE_CHAIN (init);
   20479          102 :                      init2; init2 = TREE_CHAIN (init2))
   20480              :                   {
   20481          123 :                     if (phase == 0 && STATIC_INIT_DECOMP_BASE_P (init2))
   20482              :                       ;
   20483          102 :                     else if (phase == 0
   20484          123 :                              && STATIC_INIT_DECOMP_NONBASE_P (init2))
   20485              :                       {
   20486          102 :                         phase = TREE_LANG_FLAG_0 (init2) ? 2 : 1;
   20487              :                         last = init2;
   20488              :                       }
   20489           81 :                     else if (IN_RANGE (phase, 1, 2)
   20490          162 :                              && STATIC_INIT_DECOMP_NONBASE_P (init2))
   20491              :                       {
   20492           60 :                         if (TREE_LANG_FLAG_0 (init2))
   20493           81 :                           phase = 2;
   20494              :                         last = init2;
   20495              :                       }
   20496              :                     else
   20497              :                       break;
   20498              :                   }
   20499           21 :                 if (phase == 2)
   20500              :                   {
   20501              :                     /* In that case, add markers about it so that the
   20502              :                        STATIC_INIT_DECOMP_BASE_P and
   20503              :                        STATIC_INIT_DECOMP_NONBASE_P flags can be restored.  */
   20504           21 :                     sec.tree_node (build_int_cst (integer_type_node,
   20505           21 :                                                   2 * passes + 1));
   20506           21 :                     phase = 1;
   20507          123 :                     for (tree init2 = init; init2 != TREE_CHAIN (last);
   20508          102 :                          init2 = TREE_CHAIN (init2))
   20509          102 :                       if (TREE_LANG_FLAG_0 (init2))
   20510              :                         {
   20511          102 :                           tree decl = TREE_VALUE (init2);
   20512          102 :                           if (phase == 1
   20513          102 :                               && STATIC_INIT_DECOMP_NONBASE_P (init2))
   20514              :                             {
   20515           21 :                               sec.tree_node (build_int_cst (integer_type_node,
   20516           21 :                                                             2 * passes + 2));
   20517           21 :                               phase = 2;
   20518              :                             }
   20519          102 :                           dump ("Initializer:%u for %N", count, decl);
   20520          102 :                           sec.tree_node (decl);
   20521          102 :                           ++count;
   20522              :                         }
   20523           21 :                     sec.tree_node (integer_zero_node);
   20524           21 :                     init = last;
   20525           21 :                     continue;
   20526           21 :                   }
   20527              :               }
   20528              : 
   20529          123 :             tree decl = TREE_VALUE (init);
   20530              : 
   20531          123 :             dump ("Initializer:%u for %N", count, decl);
   20532          123 :             sec.tree_node (decl);
   20533          123 :             ++count;
   20534              :           }
   20535              : 
   20536           90 :       list = tls_aggregates;
   20537              :     }
   20538              : 
   20539           45 :   sec.end (to, to->name (MOD_SNAME_PFX ".ini"), crc_ptr);
   20540           45 :   dump.outdent ();
   20541              : 
   20542           45 :   return count;
   20543           45 : }
   20544              : 
   20545              : /* We have to defer some post-load processing until we've completed
   20546              :    reading, because they can cause more reading.  */
   20547              : 
   20548              : static void
   20549        12647 : post_load_processing ()
   20550              : {
   20551              :   /* We mustn't cause a GC, our caller should have arranged for that
   20552              :      not to happen.  */
   20553        12647 :   gcc_checking_assert (function_depth);
   20554              : 
   20555        12647 :   if (!post_load_decls)
   20556              :     return;
   20557              : 
   20558         8069 :   tree old_cfd = current_function_decl;
   20559         8069 :   struct function *old_cfun = cfun;
   20560        17154 :   while (post_load_decls->length ())
   20561              :     {
   20562         9085 :       tree decl = post_load_decls->pop ();
   20563              : 
   20564         9140 :       dump () && dump ("Post-load processing of %N", decl);
   20565              : 
   20566         9085 :       if (VAR_P (decl) && DECL_NTTP_OBJECT_P (decl))
   20567              :         {
   20568            9 :           if (!DECL_SIZE (decl))
   20569              :             {
   20570            3 :               push_to_top_level ();
   20571            3 :               cp_finish_decl (decl, DECL_INITIAL (decl), false, NULL_TREE, 0);
   20572            3 :               pop_from_top_level ();
   20573              :             }
   20574            9 :           continue;
   20575              :         }
   20576              : 
   20577         9076 :       gcc_checking_assert (DECL_MAYBE_IN_CHARGE_CDTOR_P (decl));
   20578         9076 :       expand_or_defer_fn (decl);
   20579              :       /* As in module_state::read_cluster.  */
   20580          986 :       if (at_eof && DECL_COMDAT (decl) && DECL_EXTERNAL (decl)
   20581         9409 :           && DECL_NOT_REALLY_EXTERN (decl))
   20582          292 :         DECL_EXTERNAL (decl) = false;
   20583              :     }
   20584              : 
   20585         8069 :   set_cfun (old_cfun);
   20586         8069 :   current_function_decl = old_cfd;
   20587              : }
   20588              : 
   20589              : bool
   20590           45 : module_state::read_inits (unsigned count)
   20591              : {
   20592           45 :   trees_in sec (this);
   20593           45 :   if (!sec.begin (loc, from (), from ()->find (MOD_SNAME_PFX ".ini")))
   20594              :     return false;
   20595           57 :   dump () && dump ("Reading %u initializers", count);
   20596           45 :   dump.indent ();
   20597              : 
   20598           45 :   lazy_snum = ~0u;
   20599           45 :   int decomp_phase = 0;
   20600           45 :   tree *aggrp = NULL;
   20601          270 :   for (unsigned ix = 0; ix != count; ix++)
   20602              :     {
   20603          225 :       tree last = NULL_TREE;
   20604          225 :       if (decomp_phase)
   20605          102 :         last = *aggrp;
   20606              :       /* Merely referencing the decl causes its initializer to be read
   20607              :          and added to the correct list.  */
   20608          225 :       tree decl = sec.tree_node ();
   20609              :       /* module_state::write_inits can add special INTEGER_CST markers in
   20610              :          between the decls.  1 means STATIC_INIT_DECOMP_BASE_P entries
   20611              :          follow in static_aggregates, 2 means STATIC_INIT_DECOMP_NONBASE_P
   20612              :          entries follow in static_aggregates, 3 means
   20613              :          STATIC_INIT_DECOMP_BASE_P entries follow in tls_aggregates,
   20614              :          4 means STATIC_INIT_DECOMP_NONBASE_P follow in tls_aggregates,
   20615              :          0 means end of STATIC_INIT_DECOMP_{,NON}BASE_P sequence.  */
   20616          225 :       if (tree_fits_shwi_p (decl))
   20617              :         {
   20618           63 :           if (sec.get_overrun ())
   20619              :             break;
   20620           63 :           decomp_phase = tree_to_shwi (decl);
   20621           63 :           if (decomp_phase)
   20622              :             {
   20623           42 :               aggrp = decomp_phase > 2 ? &tls_aggregates : &static_aggregates;
   20624              :               last = *aggrp;
   20625              :             }
   20626           63 :           decl = sec.tree_node ();
   20627              :         }
   20628              : 
   20629          225 :       if (sec.get_overrun ())
   20630              :         break;
   20631          225 :       if (decl)
   20632          225 :         dump ("Initializer:%u for %N", ix, decl);
   20633          225 :       if (decomp_phase)
   20634              :         {
   20635          102 :           tree init = *aggrp;
   20636          102 :           gcc_assert (TREE_VALUE (init) == decl && TREE_CHAIN (init) == last);
   20637          102 :           if ((decomp_phase & 1) != 0)
   20638           21 :             STATIC_INIT_DECOMP_BASE_P (init) = 1;
   20639              :           else
   20640           81 :             STATIC_INIT_DECOMP_NONBASE_P (init) = 1;
   20641              :         }
   20642              :     }
   20643           45 :   if (decomp_phase && !sec.get_overrun ())
   20644              :     {
   20645            0 :       tree decl = sec.tree_node ();
   20646            0 :       gcc_assert (integer_zerop (decl));
   20647              :     }
   20648           45 :   lazy_snum = 0;
   20649           45 :   post_load_processing ();
   20650           45 :   dump.outdent ();
   20651           45 :   if (!sec.end (from ()))
   20652              :     return false;
   20653              :   return true;
   20654           45 : }
   20655              : 
   20656              : void
   20657         2757 : module_state::write_counts (elf_out *to, unsigned counts[MSC_HWM],
   20658              :                             unsigned *crc_ptr)
   20659              : {
   20660         2757 :   bytes_out cfg (to);
   20661              : 
   20662         2757 :   cfg.begin ();
   20663              : 
   20664        27570 :   for (unsigned ix = MSC_HWM; ix--;)
   20665        24813 :     cfg.u (counts[ix]);
   20666              : 
   20667         2757 :   if (dump ())
   20668              :     {
   20669          300 :       dump ("Cluster sections are [%u,%u)",
   20670              :             counts[MSC_sec_lwm], counts[MSC_sec_hwm]);
   20671          300 :       dump ("Bindings %u", counts[MSC_bindings]);
   20672          300 :       dump ("Pendings %u", counts[MSC_pendings]);
   20673          300 :       dump ("Entities %u", counts[MSC_entities]);
   20674          300 :       dump ("Namespaces %u", counts[MSC_namespaces]);
   20675          300 :       dump ("Using-directives %u", counts[MSC_using_directives]);
   20676          300 :       dump ("Macros %u", counts[MSC_macros]);
   20677          300 :       dump ("Initializers %u", counts[MSC_inits]);
   20678              :     }
   20679              : 
   20680         2757 :   cfg.end (to, to->name (MOD_SNAME_PFX ".cnt"), crc_ptr);
   20681         2757 : }
   20682              : 
   20683              : bool
   20684         2940 : module_state::read_counts (unsigned counts[MSC_HWM])
   20685              : {
   20686         2940 :   bytes_in cfg;
   20687              : 
   20688         2940 :   if (!cfg.begin (loc, from (), MOD_SNAME_PFX ".cnt"))
   20689              :     return false;
   20690              : 
   20691        29400 :   for (unsigned ix = MSC_HWM; ix--;)
   20692        26460 :     counts[ix] = cfg.u ();
   20693              : 
   20694         2940 :   if (dump ())
   20695              :     {
   20696          529 :       dump ("Declaration sections are [%u,%u)",
   20697              :             counts[MSC_sec_lwm], counts[MSC_sec_hwm]);
   20698          529 :       dump ("Bindings %u", counts[MSC_bindings]);
   20699          529 :       dump ("Pendings %u", counts[MSC_pendings]);
   20700          529 :       dump ("Entities %u", counts[MSC_entities]);
   20701          529 :       dump ("Namespaces %u", counts[MSC_namespaces]);
   20702          529 :       dump ("Using-directives %u", counts[MSC_using_directives]);
   20703          529 :       dump ("Macros %u", counts[MSC_macros]);
   20704          529 :       dump ("Initializers %u", counts[MSC_inits]);
   20705              :     }
   20706              : 
   20707         2940 :   return cfg.end (from ());
   20708         2940 : }
   20709              : 
   20710              : /* Tool configuration:  MOD_SNAME_PFX .config
   20711              : 
   20712              :    This is data that confirms current state (or fails).  */
   20713              : 
   20714              : void
   20715         2757 : module_state::write_config (elf_out *to, module_state_config &config,
   20716              :                             unsigned inner_crc)
   20717              : {
   20718         2757 :   bytes_out cfg (to);
   20719              : 
   20720         2757 :   cfg.begin ();
   20721              : 
   20722              :   /* Write version and inner crc as u32 values, for easier
   20723              :      debug inspection.  */
   20724         3057 :   dump () && dump ("Writing version=%V, inner_crc=%x",
   20725              :                    MODULE_VERSION, inner_crc);
   20726         2757 :   cfg.u32 (unsigned (MODULE_VERSION));
   20727         2757 :   cfg.u32 (inner_crc);
   20728              : 
   20729         2757 :   cfg.u (to->name (is_header () ? "" : get_flatname ()));
   20730              : 
   20731              :   /* Configuration. */
   20732         3057 :   dump () && dump ("Writing target='%s', host='%s'",
   20733              :                    TARGET_MACHINE, HOST_MACHINE);
   20734         2757 :   unsigned target = to->name (TARGET_MACHINE);
   20735         2757 :   unsigned host = (!strcmp (TARGET_MACHINE, HOST_MACHINE)
   20736              :                    ? target : to->name (HOST_MACHINE));
   20737         2757 :   cfg.u (target);
   20738         2757 :   cfg.u (host);
   20739              : 
   20740         2757 :   cfg.str (config.dialect_str);
   20741         2757 :   cfg.u (extensions);
   20742              : 
   20743              :   /* Global tree information.  We write the globals crc separately,
   20744              :      rather than mix it directly into the overall crc, as it is used
   20745              :      to ensure data match between instances of the compiler, not
   20746              :      integrity of the file.  */
   20747         3057 :   dump () && dump ("Writing globals=%u, crc=%x",
   20748              :                    fixed_trees->length (), global_crc);
   20749         2757 :   cfg.u (fixed_trees->length ());
   20750         2757 :   cfg.u32 (global_crc);
   20751              : 
   20752         2757 :   if (is_partition ())
   20753          199 :     cfg.u (is_interface ());
   20754              : 
   20755         2757 :   cfg.u (config.num_imports);
   20756         2757 :   cfg.u (config.num_partitions);
   20757         2757 :   cfg.u (config.num_entities);
   20758              : 
   20759         2757 :   cfg.loc (config.ordinary_locs);
   20760         2757 :   cfg.loc (config.macro_locs);
   20761         2757 :   cfg.u (config.loc_range_bits);
   20762              : 
   20763         2757 :   cfg.u (config.active_init);
   20764              : 
   20765              :   /* Now generate CRC, we'll have incorporated the inner CRC because
   20766              :      of its serialization above.  */
   20767         2757 :   cfg.end (to, to->name (MOD_SNAME_PFX ".cfg"), &crc);
   20768         3057 :   dump () && dump ("Writing CRC=%x", crc);
   20769         2757 : }
   20770              : 
   20771              : void
   20772           40 : module_state::note_cmi_name ()
   20773              : {
   20774           40 :   if (!cmi_noted_p && filename)
   20775              :     {
   20776           40 :       cmi_noted_p = true;
   20777           40 :       inform (loc, "compiled module file is %qs",
   20778              :               maybe_add_cmi_prefix (filename));
   20779              :     }
   20780           40 : }
   20781              : 
   20782              : bool
   20783         3050 : module_state::read_config (module_state_config &config, bool complain)
   20784              : {
   20785         3050 :   bytes_in cfg;
   20786              : 
   20787         3050 :   if (!cfg.begin (loc, from (), MOD_SNAME_PFX ".cfg"))
   20788              :     return false;
   20789              : 
   20790              :   /* Check version.  */
   20791         3050 :   unsigned my_ver = MODULE_VERSION;
   20792         3050 :   unsigned their_ver = cfg.u32 ();
   20793         3582 :   dump () && dump  (my_ver == their_ver ? "Version %V"
   20794              :                     : "Expecting %V found %V", my_ver, their_ver);
   20795         3050 :   if (their_ver != my_ver)
   20796              :     {
   20797              :       /* The compiler versions differ.  Close enough? */
   20798            0 :       verstr_t my_string, their_string;
   20799              : 
   20800            0 :       version2string (my_ver, my_string);
   20801            0 :       version2string (their_ver, their_string);
   20802              : 
   20803              :       /* Reject when either is non-experimental or when experimental
   20804              :          major versions differ.  */
   20805            0 :       auto_diagnostic_group d;
   20806            0 :       bool reject_p = ((!IS_EXPERIMENTAL (my_ver)
   20807              :                         || !IS_EXPERIMENTAL (their_ver)
   20808            0 :                         || MODULE_MAJOR (my_ver) != MODULE_MAJOR (their_ver))
   20809              :                        /* The 'I know what I'm doing' switch.  */
   20810            0 :                        && !flag_module_version_ignore);
   20811            0 :       bool inform_p = true;
   20812            0 :       if (!complain)
   20813              :         inform_p = false;
   20814            0 :       else if (reject_p)
   20815              :         {
   20816            0 :           cfg.set_overrun ();
   20817            0 :           error_at (loc, "compiled module is %sversion %s",
   20818              :                     IS_EXPERIMENTAL (their_ver) ? "experimental " : "",
   20819              :                     their_string);
   20820              :         }
   20821              :       else
   20822            0 :         inform_p = warning_at (loc, 0, "compiled module is %sversion %s",
   20823              :                              IS_EXPERIMENTAL (their_ver) ? "experimental " : "",
   20824              :                              their_string);
   20825              : 
   20826            0 :       if (inform_p)
   20827              :         {
   20828            0 :           inform (loc, "compiler is %sversion %s%s%s",
   20829              :                   IS_EXPERIMENTAL (my_ver) ? "experimental " : "",
   20830              :                   my_string,
   20831            0 :                   reject_p ? "" : flag_module_version_ignore
   20832            0 :                   ? ", be it on your own head!" : ", close enough?",
   20833              :                   reject_p ? "" : " \xc2\xaf\\_(\xe3\x83\x84)_/\xc2\xaf");
   20834            0 :           note_cmi_name ();
   20835              :         }
   20836              : 
   20837            0 :       if (reject_p)
   20838            0 :         goto done;
   20839            0 :     }
   20840              : 
   20841              :   /*  We wrote the inner crc merely to merge it, so simply read it
   20842              :       back and forget it.  */
   20843         3050 :   cfg.u32 ();
   20844              : 
   20845              :   /* Check module name.  */
   20846         3050 :   {
   20847         3050 :     const char *their_name = from ()->name (cfg.u ());
   20848         3050 :     const char *our_name = "";
   20849              : 
   20850         3050 :     if (!is_header ())
   20851         2012 :       our_name = get_flatname ();
   20852              : 
   20853              :     /* Header units can be aliased, so name checking is
   20854              :        inappropriate.  */
   20855         3050 :     if (0 != strcmp (their_name, our_name))
   20856              :       {
   20857            0 :         error_at (loc,
   20858            0 :                   their_name[0] && our_name[0] ? G_("module %qs found")
   20859              :                   : their_name[0]
   20860              :                   ? G_("header module expected, module %qs found")
   20861              :                   : G_("module %qs expected, header module found"),
   20862            0 :                   their_name[0] ? their_name : our_name);
   20863            0 :         cfg.set_overrun ();
   20864            0 :         goto done;
   20865              :       }
   20866              :   }
   20867              : 
   20868              :   /* Check the CRC after the above sanity checks, so that the user is
   20869              :      clued in.  */
   20870         3050 :   {
   20871         3050 :     unsigned e_crc = crc;
   20872         3050 :     crc = cfg.get_crc ();
   20873         3582 :     dump () && dump ("Reading CRC=%x", crc);
   20874              :     /* When not complaining we haven't set directness yet, so ignore the
   20875              :        mismatch. */
   20876         3050 :     if (complain && !is_direct () && crc != e_crc)
   20877              :       {
   20878            3 :         error_at (loc, "module %qs CRC mismatch", get_flatname ());
   20879            3 :         cfg.set_overrun ();
   20880            3 :         goto done;
   20881              :       }
   20882              :   }
   20883              : 
   20884              :   /* Check target & host.  */
   20885         3047 :   {
   20886         3047 :     const char *their_target = from ()->name (cfg.u ());
   20887         3047 :     const char *their_host = from ()->name (cfg.u ());
   20888         3579 :     dump () && dump ("Read target='%s', host='%s'", their_target, their_host);
   20889         3047 :     if (strcmp (their_target, TARGET_MACHINE)
   20890         3047 :         || strcmp (their_host, HOST_MACHINE))
   20891              :       {
   20892            0 :         error_at (loc, "target & host is %qs:%qs, expected %qs:%qs",
   20893              :                   their_target, TARGET_MACHINE, their_host, HOST_MACHINE);
   20894            0 :         cfg.set_overrun ();
   20895            0 :         goto done;
   20896              :       }
   20897              :   }
   20898              : 
   20899              :   /* Check compilation dialect.  This must match.  */
   20900         3047 :   {
   20901         3047 :     const char *their_dialect = cfg.str ();
   20902         3047 :     if (strcmp (their_dialect, config.dialect_str))
   20903              :       {
   20904            1 :         if (complain)
   20905            1 :           error_at (loc, "language dialect differs %qs, expected %qs",
   20906              :                     their_dialect, config.dialect_str);
   20907            1 :         cfg.set_overrun ();
   20908            1 :         goto done;
   20909              :       }
   20910              :   }
   20911              : 
   20912              :   /* Check for extensions.  If they set any, we must have them set
   20913              :      too.  */
   20914         3046 :   {
   20915         3046 :     unsigned ext = cfg.u ();
   20916         3046 :     unsigned allowed = (flag_openmp ? SE_OPENMP | SE_OPENMP_SIMD : 0);
   20917         3046 :     if (flag_openmp_simd)
   20918            3 :       allowed |= SE_OPENMP_SIMD;
   20919         3046 :     if (flag_openacc)
   20920            3 :       allowed |= SE_OPENACC;
   20921              : 
   20922         3046 :     if (unsigned bad = ext & ~allowed)
   20923              :       {
   20924            9 :         if (bad & SE_OPENMP)
   20925            3 :           error_at (loc, "module contains OpenMP, use %<-fopenmp%> to enable");
   20926            6 :         else if (bad & SE_OPENMP_SIMD)
   20927            3 :           error_at (loc, "module contains OpenMP, use %<-fopenmp%> or "
   20928              :                          "%<-fopenmp-simd%> to enable");
   20929            9 :         if (bad & SE_OPENACC)
   20930            3 :           error_at (loc, "module contains OpenACC, use %<-fopenacc%> to "
   20931              :                          "enable");
   20932            9 :         cfg.set_overrun ();
   20933            9 :         goto done;
   20934              :       }
   20935         3037 :     extensions = ext;
   20936              :   }
   20937              : 
   20938              :   /* Check global trees.  */
   20939         3037 :   {
   20940         3037 :     unsigned their_fixed_length = cfg.u ();
   20941         3037 :     unsigned their_fixed_crc = cfg.u32 ();
   20942         3569 :     dump () && dump ("Read globals=%u, crc=%x",
   20943              :                      their_fixed_length, their_fixed_crc);
   20944         3037 :     if (!flag_preprocess_only
   20945         3037 :         && (their_fixed_length != fixed_trees->length ()
   20946         2983 :             || their_fixed_crc != global_crc))
   20947              :       {
   20948            0 :         error_at (loc, "fixed tree mismatch");
   20949            0 :         cfg.set_overrun ();
   20950            0 :         goto done;
   20951              :       }
   20952              :   }
   20953              : 
   20954              :   /* All non-partitions are interfaces.  */
   20955         3037 :   interface_p = !is_partition () || cfg.u ();
   20956              : 
   20957         3037 :   config.num_imports = cfg.u ();
   20958         3037 :   config.num_partitions = cfg.u ();
   20959         3037 :   config.num_entities = cfg.u ();
   20960              : 
   20961         3037 :   config.ordinary_locs = cfg.loc ();
   20962         3037 :   config.macro_locs = cfg.loc ();
   20963         3037 :   config.loc_range_bits = cfg.u ();
   20964              : 
   20965         3037 :   config.active_init = cfg.u ();
   20966              : 
   20967         3050 :  done:
   20968         3050 :   return cfg.end (from ());
   20969         3050 : }
   20970              : 
   20971              : /* Comparator for ordering the Ordered Ordinary Location array.  */
   20972              : 
   20973              : static int
   20974          124 : ool_cmp (const void *a_, const void *b_)
   20975              : {
   20976          124 :   auto *a = *static_cast<const module_state *const *> (a_);
   20977          124 :   auto *b = *static_cast<const module_state *const *> (b_);
   20978          124 :   if (a == b)
   20979              :     return 0;
   20980          124 :   else if (a->ordinary_locs.first < b->ordinary_locs.first)
   20981              :     return -1;
   20982              :   else
   20983           52 :     return +1;
   20984              : }
   20985              : 
   20986              : /* Use ELROND format to record the following sections:
   20987              :      qualified-names        : binding value(s)
   20988              :      MOD_SNAME_PFX.README   : human readable, strings
   20989              :      MOD_SNAME_PFX.ENV      : environment strings, strings
   20990              :      MOD_SNAME_PFX.nms      : namespace hierarchy
   20991              :      MOD_SNAME_PFX.udi      : namespace using-directives
   20992              :      MOD_SNAME_PFX.bnd      : binding table
   20993              :      MOD_SNAME_PFX.spc      : specialization table
   20994              :      MOD_SNAME_PFX.imp      : import table
   20995              :      MOD_SNAME_PFX.ent      : entity table
   20996              :      MOD_SNAME_PFX.prt      : partitions table
   20997              :      MOD_SNAME_PFX.olm      : ordinary line maps
   20998              :      MOD_SNAME_PFX.mlm      : macro line maps
   20999              :      MOD_SNAME_PFX.def      : macro definitions
   21000              :      MOD_SNAME_PFX.mac      : macro index
   21001              :      MOD_SNAME_PFX.ini      : inits
   21002              :      MOD_SNAME_PFX.cnt      : counts
   21003              :      MOD_SNAME_PFX.cfg      : config data
   21004              : */
   21005              : 
   21006              : bool
   21007         2786 : module_state::write_begin (elf_out *to, cpp_reader *reader,
   21008              :                            module_state_config &config, unsigned &crc)
   21009              : {
   21010              :   /* Figure out remapped module numbers, which might elide
   21011              :      partitions.  */
   21012         2786 :   bitmap partitions = NULL;
   21013         2786 :   if (!is_header () && !is_partition ())
   21014         1684 :     partitions = BITMAP_GGC_ALLOC ();
   21015         2786 :   write_init_maps ();
   21016              : 
   21017         2786 :   unsigned mod_hwm = 1;
   21018         3461 :   for (unsigned ix = 1; ix != modules->length (); ix++)
   21019              :     {
   21020          675 :       module_state *imp = (*modules)[ix];
   21021              : 
   21022              :       /* Promote any non-partition direct import from a partition, unless
   21023              :          we're a partition.  */
   21024          618 :       if (!is_partition () && !imp->is_partition ()
   21025         1109 :           && imp->is_partition_direct ())
   21026           12 :         imp->directness = MD_PURVIEW_DIRECT;
   21027              : 
   21028              :       /* Write any import that is not a partition, unless we're a
   21029              :          partition.  */
   21030          675 :       if (!partitions || !imp->is_partition ())
   21031          491 :         imp->remap = mod_hwm++;
   21032              :       else
   21033              :         {
   21034          223 :           dump () && dump ("Partition %M %u", imp, ix);
   21035          184 :           bitmap_set_bit (partitions, ix);
   21036          184 :           imp->remap = 0;
   21037              :           /* All interface partitions must be exported.  */
   21038          184 :           if (imp->is_interface () && !bitmap_bit_p (exports, imp->mod))
   21039              :             {
   21040            3 :               error_at (imp->loc, "interface partition is not exported");
   21041            3 :               bitmap_set_bit (exports, imp->mod);
   21042              :             }
   21043              : 
   21044              :           /* All the partition entities should have been loaded when
   21045              :              loading the partition.  */
   21046              :           if (CHECKING_P)
   21047         1233 :             for (unsigned jx = 0; jx != imp->entity_num; jx++)
   21048              :               {
   21049         1049 :                 binding_slot *slot = &(*entity_ary)[imp->entity_lwm + jx];
   21050         1049 :                 gcc_checking_assert (!slot->is_lazy ());
   21051              :               }
   21052              :         }
   21053              : 
   21054          675 :       if (imp->is_direct () && (imp->remap || imp->is_partition ()))
   21055          657 :         note_location (imp->imported_from ());
   21056              :     }
   21057              : 
   21058         2786 :   if (partitions && bitmap_empty_p (partitions))
   21059              :     /* No partitions present.  */
   21060              :     partitions = nullptr;
   21061              : 
   21062              :   /* Find the set of decls we must write out.  */
   21063         2786 :   depset::hash table (DECL_NAMESPACE_BINDINGS (global_namespace)->size () * 8);
   21064              :   /* Add the specializations before the writables, so that we can
   21065              :      detect injected friend specializations.  */
   21066         2786 :   table.add_specializations (true);
   21067         2786 :   table.add_specializations (false);
   21068         2786 :   if (partial_specializations)
   21069              :     {
   21070          215 :       table.add_partial_entities (partial_specializations);
   21071          215 :       partial_specializations = NULL;
   21072              :     }
   21073         2786 :   table.add_namespace_entities (global_namespace, partitions);
   21074         2786 :   if (class_members)
   21075              :     {
   21076           12 :       table.add_class_entities (class_members);
   21077           12 :       class_members = NULL;
   21078              :     }
   21079              : 
   21080              :   /* Now join everything up.  */
   21081         2786 :   table.find_dependencies (this);
   21082              : 
   21083         2786 :   if (!table.finalize_dependencies ())
   21084              :     return false;
   21085              : 
   21086              : #if CHECKING_P
   21087              :   /* We're done verifying at-most once reading, reset to verify
   21088              :      at-most once writing.  */
   21089         2757 :   note_defs = note_defs_table_t::create_ggc (1000);
   21090              : #endif
   21091              : 
   21092              :   /* Determine Strongly Connected Components.  This will also strip any
   21093              :      unnecessary dependencies on imported or TU-local entities.  */
   21094         2757 :   vec<depset *> sccs = table.connect ();
   21095              : 
   21096         2757 :   vec_alloc (ool, modules->length ());
   21097         3425 :   for (unsigned ix = modules->length (); --ix;)
   21098              :     {
   21099          668 :       auto *import = (*modules)[ix];
   21100          668 :       if (import->loadedness > ML_NONE
   21101          668 :           && !(partitions && bitmap_bit_p (partitions, import->mod)))
   21102          484 :         ool->quick_push (import);
   21103              :     }
   21104         2757 :   ool->qsort (ool_cmp);
   21105              : 
   21106         2757 :   write_diagnostic_classification (nullptr, global_dc, nullptr);
   21107              : 
   21108         2757 :   vec<cpp_hashnode *> *macros = nullptr;
   21109         2757 :   if (is_header ())
   21110          903 :     macros = prepare_macros (reader);
   21111              : 
   21112         2757 :   config.num_imports = mod_hwm;
   21113         2757 :   config.num_partitions = modules->length () - mod_hwm;
   21114         2757 :   auto map_info = write_prepare_maps (&config, bool (config.num_partitions));
   21115         2757 :   unsigned counts[MSC_HWM];
   21116         2757 :   memset (counts, 0, sizeof (counts));
   21117              : 
   21118              :   /* depset::cluster is the cluster number,
   21119              :      depset::section is unspecified scratch value.
   21120              : 
   21121              :      The following loops make use of the tarjan property that
   21122              :      dependencies will be earlier in the SCCS array.  */
   21123              : 
   21124              :   /* This first loop determines the number of depsets in each SCC, and
   21125              :      also the number of namespaces we're dealing with.  During the
   21126              :      loop, the meaning of a couple of depset fields now change:
   21127              : 
   21128              :      depset::cluster -> size_of cluster, if first of cluster & !namespace
   21129              :      depset::section -> section number of cluster (if !namespace). */
   21130              : 
   21131         2757 :   unsigned n_spaces = 0;
   21132         2757 :   counts[MSC_sec_lwm] = counts[MSC_sec_hwm] = to->get_section_limit ();
   21133       319470 :   for (unsigned size, ix = 0; ix < sccs.length (); ix += size)
   21134              :     {
   21135       316713 :       depset **base = &sccs[ix];
   21136              : 
   21137       599037 :       if (base[0]->get_entity_kind () == depset::EK_NAMESPACE)
   21138              :         {
   21139         5106 :           n_spaces++;
   21140         5106 :           size = 1;
   21141              :         }
   21142              :       else
   21143              :         {
   21144              :           /* Count the members in this cluster.  */
   21145      1390358 :           for (size = 1; ix + size < sccs.length (); size++)
   21146      1387932 :             if (base[size]->cluster != base[0]->cluster)
   21147              :               break;
   21148              : 
   21149      1701965 :           for (unsigned jx = 0; jx != size; jx++)
   21150              :             {
   21151              :               /* Set the section number.  */
   21152      1390358 :               base[jx]->cluster = ~(~0u >> 1); /* A bad value.  */
   21153      1390358 :               base[jx]->section = counts[MSC_sec_hwm];
   21154              :             }
   21155              : 
   21156              :           /* Save the size in the first member's cluster slot.  */
   21157       311607 :           base[0]->cluster = size;
   21158              : 
   21159       311607 :           counts[MSC_sec_hwm]++;
   21160              :         }
   21161              :     }
   21162              : 
   21163              :   /* Write the clusters.  Namespace decls are put in the spaces array.
   21164              :      The meaning of depset::cluster changes to provide the
   21165              :      unnamed-decl count of the depset's decl (and remains zero for
   21166              :      non-decls and non-unnamed).  */
   21167         2757 :   unsigned bytes = 0;
   21168         2757 :   vec<depset *> spaces;
   21169         2757 :   spaces.create (n_spaces);
   21170              : 
   21171       319470 :   for (unsigned size, ix = 0; ix < sccs.length (); ix += size)
   21172              :     {
   21173       316713 :       depset **base = &sccs[ix];
   21174              : 
   21175       316713 :       if (base[0]->get_entity_kind () == depset::EK_NAMESPACE)
   21176              :         {
   21177         5106 :           tree decl = base[0]->get_entity ();
   21178         5106 :           if (decl == global_namespace)
   21179         2503 :             base[0]->cluster = 0;
   21180         2603 :           else if (!base[0]->is_import ())
   21181              :             {
   21182         2603 :               base[0]->cluster = counts[MSC_entities]++;
   21183         2603 :               spaces.quick_push (base[0]);
   21184         2603 :               counts[MSC_namespaces]++;
   21185         2603 :               if (CHECKING_P)
   21186              :                 {
   21187              :                   /* Add it to the entity map, such that we can tell it is
   21188              :                      part of us.  */
   21189         2603 :                   bool existed;
   21190         2603 :                   unsigned *slot = &entity_map->get_or_insert
   21191         2603 :                     (DECL_UID (decl), &existed);
   21192         2603 :                   if (existed)
   21193              :                     /* It must have come from a partition.  */
   21194            0 :                     gcc_checking_assert
   21195              :                       (import_entity_module (*slot)->is_partition ());
   21196         2603 :                   *slot = ~base[0]->cluster;
   21197              :                 }
   21198       319346 :               dump (dumper::CLUSTER) && dump ("Cluster namespace %N", decl);
   21199              :             }
   21200              :           size = 1;
   21201              :         }
   21202              :       else
   21203              :         {
   21204       311607 :           size = base[0]->cluster;
   21205              : 
   21206              :           /* Cluster is now used to number entities.  */
   21207       311607 :           base[0]->cluster = ~(~0u >> 1); /* A bad value.  */
   21208              : 
   21209       311607 :           sort_cluster (&table, base, size);
   21210              : 
   21211              :           /* Record the section for consistency checking during stream
   21212              :              out -- we don't want to start writing decls in different
   21213              :              sections.  */
   21214       311607 :           table.section = base[0]->section;
   21215       311607 :           bytes += write_cluster (to, base, size, table, counts, &crc);
   21216       311607 :           table.section = 0;
   21217              :         }
   21218              :     }
   21219              : 
   21220              :   /* depset::cluster - entity number (on entities)
   21221              :      depset::section - cluster number  */
   21222              :   /* We'd better have written as many sections and found as many
   21223              :      namespaces as we predicted.  */
   21224         5514 :   gcc_assert (counts[MSC_sec_hwm] == to->get_section_limit ()
   21225              :               && spaces.length () == counts[MSC_namespaces]);
   21226              : 
   21227              :   /* Write the entities.  None happens if we contain namespaces or
   21228              :      nothing. */
   21229         2757 :   config.num_entities = counts[MSC_entities];
   21230         2757 :   if (counts[MSC_entities])
   21231         2497 :     write_entities (to, sccs, counts[MSC_entities], &crc);
   21232              : 
   21233              :   /* Write the namespaces.  */
   21234         2757 :   if (counts[MSC_namespaces])
   21235          587 :     write_namespaces (to, spaces, counts[MSC_namespaces], &crc);
   21236              : 
   21237              :   /* Write any using-directives.  */
   21238         2757 :   if (counts[MSC_namespaces])
   21239          587 :     counts[MSC_using_directives]
   21240          587 :       = write_using_directives (to, table, spaces, &crc);
   21241              : 
   21242              :   /* Write the bindings themselves.  */
   21243         2757 :   counts[MSC_bindings] = write_bindings (to, sccs, &crc);
   21244              : 
   21245              :   /* Write the unnamed.  */
   21246         2757 :   counts[MSC_pendings] = write_pendings (to, sccs, table, &crc);
   21247              : 
   21248              :   /* Write the import table.  */
   21249         2757 :   if (config.num_imports > 1)
   21250          453 :     write_imports (to, &crc);
   21251              : 
   21252              :   /* Write elided partition table.  */
   21253         2757 :   if (config.num_partitions)
   21254          133 :     write_partitions (to, config.num_partitions, &crc);
   21255              : 
   21256              :   /* Write the line maps.  */
   21257         2757 :   if (config.ordinary_locs)
   21258              :     {
   21259         2661 :       write_ordinary_maps (to, map_info, bool (config.num_partitions), &crc);
   21260         2661 :       write_diagnostic_classification (to, global_dc, &crc);
   21261              :     }
   21262         2757 :   if (config.macro_locs)
   21263          124 :     write_macro_maps (to, map_info, &crc);
   21264              : 
   21265         2757 :   if (is_header ())
   21266              :     {
   21267          903 :       counts[MSC_macros] = write_macros (to, macros, &crc);
   21268          903 :       counts[MSC_inits] = write_inits (to, table, &crc);
   21269          903 :       vec_free (macros);
   21270              :     }
   21271              : 
   21272         2757 :   unsigned clusters = counts[MSC_sec_hwm] - counts[MSC_sec_lwm];
   21273         2757 :   dump () && dump ("Wrote %u clusters, average %u bytes/cluster",
   21274          300 :                    clusters, (bytes + clusters / 2) / (clusters + !clusters));
   21275         2757 :   trees_out::instrument ();
   21276              : 
   21277         2757 :   write_counts (to, counts, &crc);
   21278              : 
   21279         2757 :   spaces.release ();
   21280         2757 :   sccs.release ();
   21281              : 
   21282         2757 :   vec_free (macro_loc_remap);
   21283         2757 :   vec_free (ord_loc_remap);
   21284         2757 :   vec_free (ool);
   21285              : 
   21286              :   // FIXME:QOI:  Have a command line switch to control more detailed
   21287              :   // information (which might leak data you do not want to leak).
   21288              :   // Perhaps (some of) the write_readme contents should also be
   21289              :   // so-controlled.
   21290         2757 :   if (false)
   21291              :     write_env (to);
   21292              : 
   21293         2757 :   return true;
   21294         2786 : }
   21295              : 
   21296              : // Finish module writing after we've emitted all dynamic initializers.
   21297              : 
   21298              : void
   21299         2757 : module_state::write_end (elf_out *to, cpp_reader *reader,
   21300              :                          module_state_config &config, unsigned &crc)
   21301              : {
   21302              :   /* And finish up.  */
   21303         2757 :   write_config (to, config, crc);
   21304              : 
   21305              :   /* Human-readable info.  */
   21306         2757 :   write_readme (to, reader, config.dialect_str);
   21307              : 
   21308         3057 :   dump () && dump ("Wrote %u sections", to->get_section_limit ());
   21309         2757 : }
   21310              : 
   21311              : /* Initial read of a CMI.  Checks config, loads up imports and line
   21312              :    maps.  */
   21313              : 
   21314              : bool
   21315         3004 : module_state::read_initial (cpp_reader *reader)
   21316              : {
   21317         3004 :   module_state_config config;
   21318         3004 :   bool ok = true;
   21319              : 
   21320         3004 :   if (ok && !read_config (config))
   21321              :     ok = false;
   21322              : 
   21323         2991 :   bool have_locs = ok && read_prepare_maps (&config);
   21324              : 
   21325              :   /* Ordinary maps before the imports.  */
   21326         2991 :   if (!(have_locs && config.ordinary_locs))
   21327           71 :     ordinary_locs.first = line_table->highest_location + 1;
   21328         2933 :   else if (!read_ordinary_maps (config.ordinary_locs, config.loc_range_bits))
   21329              :     ok = false;
   21330              : 
   21331              :   /* Allocate the REMAP vector.  */
   21332         3004 :   slurp->alloc_remap (config.num_imports);
   21333              : 
   21334         3004 :   if (ok)
   21335              :     {
   21336              :       /* Read the import table.  Decrement current to stop this CMI
   21337              :          from being evicted during the import. */
   21338         2991 :       slurp->current--;
   21339         2991 :       if (config.num_imports > 1 && !read_imports (reader, line_table))
   21340              :         ok = false;
   21341         2991 :       slurp->current++;
   21342              :     }
   21343              : 
   21344              :   /* Read the elided partition table, if we're the primary partition.  */
   21345         2991 :   if (ok && config.num_partitions && is_module ()
   21346         3018 :       && !read_partitions (config.num_partitions))
   21347              :     ok = false;
   21348              : 
   21349              :   /* Determine the module's number.  */
   21350         3004 :   gcc_checking_assert (mod == MODULE_UNKNOWN);
   21351         3004 :   gcc_checking_assert (this != this_module ());
   21352              : 
   21353         3004 :   {
   21354              :     /* Allocate space in the entities array now -- that array must be
   21355              :        monotonically in step with the modules array.  */
   21356         3004 :     entity_lwm = vec_safe_length (entity_ary);
   21357         3004 :     entity_num = config.num_entities;
   21358         3004 :     gcc_checking_assert (modules->length () == 1
   21359              :                          || modules->last ()->entity_lwm <= entity_lwm);
   21360         3004 :     vec_safe_reserve (entity_ary, config.num_entities);
   21361              : 
   21362         3004 :     binding_slot slot;
   21363         3004 :     slot.u.binding = NULL_TREE;
   21364      1334325 :     for (unsigned count = config.num_entities; count--;)
   21365      1331321 :       entity_ary->quick_push (slot);
   21366              :   }
   21367              : 
   21368              :   /* We'll run out of other resources before we run out of module
   21369              :      indices.  */
   21370         3004 :   mod = modules->length ();
   21371         3004 :   vec_safe_push (modules, this);
   21372              : 
   21373              :   /* We always import and export ourselves. */
   21374         3004 :   bitmap_set_bit (imports, mod);
   21375         3004 :   bitmap_set_bit (exports, mod);
   21376              : 
   21377         3004 :   if (ok)
   21378         2991 :     (*slurp->remap)[0] = mod << 1;
   21379         3533 :   dump () && dump ("Assigning %M module number %u", this, mod);
   21380              : 
   21381              :   /* We should not have been frozen during the importing done by
   21382              :      read_config.  */
   21383         3004 :   gcc_assert (!from ()->is_frozen ());
   21384              : 
   21385              :   /* Macro maps after the imports.  */
   21386         3004 :   if (!(ok && have_locs && config.macro_locs))
   21387         2871 :     macro_locs.first = LINEMAPS_MACRO_LOWEST_LOCATION (line_table);
   21388          133 :   else if (!read_macro_maps (config.macro_locs))
   21389              :     ok = false;
   21390              : 
   21391              :   /* Diagnostic classification streaming needs to come after reading
   21392              :      macro maps to handle _Pragmas in macros.  */
   21393         2991 :   if (ok && have_locs && config.ordinary_locs
   21394         5937 :       && !read_diagnostic_classification (global_dc))
   21395              :     ok = false;
   21396              : 
   21397              :   /* Note whether there's an active initializer.  */
   21398         3004 :   active_init_p = !is_header () && bool (config.active_init);
   21399              : 
   21400         3004 :   gcc_assert (slurp->current == ~0u);
   21401         3004 :   return ok;
   21402              : }
   21403              : 
   21404              : /* Read a preprocessor state.  */
   21405              : 
   21406              : bool
   21407          941 : module_state::read_preprocessor (bool outermost)
   21408              : {
   21409          941 :   gcc_checking_assert (is_header () && slurp
   21410              :                        && slurp->remap_module (0) == mod);
   21411              : 
   21412          941 :   if (loadedness == ML_PREPROCESSOR)
   21413            6 :     return !(from () && from ()->get_error ());
   21414              : 
   21415          935 :   bool ok = true;
   21416              : 
   21417              :   /* Read direct header imports.  */
   21418          935 :   unsigned len = slurp->remap->length ();
   21419          977 :   for (unsigned ix = 1; ok && ix != len; ix++)
   21420              :     {
   21421           42 :       unsigned map = (*slurp->remap)[ix];
   21422           42 :       if (map & 1)
   21423              :         {
   21424           42 :           module_state *import = (*modules)[map >> 1];
   21425           42 :           if (import->is_header ())
   21426              :             {
   21427           42 :               ok = import->read_preprocessor (false);
   21428           42 :               bitmap_ior_into (slurp->headers, import->slurp->headers);
   21429              :             }
   21430              :         }
   21431              :     }
   21432              : 
   21433              :   /* Record as a direct header.  */
   21434          935 :   if (ok)
   21435          935 :     bitmap_set_bit (slurp->headers, mod);
   21436              : 
   21437          935 :   if (ok && !read_macros ())
   21438              :     ok = false;
   21439              : 
   21440          935 :   loadedness = ML_PREPROCESSOR;
   21441          935 :   announce ("macros");
   21442              : 
   21443          935 :   if (flag_preprocess_only)
   21444              :     /* We're done with the string table.  */
   21445           39 :     from ()->release ();
   21446              : 
   21447          935 :   return check_read (outermost, ok);
   21448              : }
   21449              : 
   21450              : /* Read language state.  */
   21451              : 
   21452              : bool
   21453         3024 : module_state::read_language (bool outermost)
   21454              : {
   21455         3024 :   gcc_checking_assert (!lazy_snum);
   21456              : 
   21457         3024 :   if (loadedness == ML_LANGUAGE)
   21458           84 :     return !(slurp && from () && from ()->get_error ());
   21459              : 
   21460         2940 :   gcc_checking_assert (slurp && slurp->current == ~0u
   21461              :                        && slurp->remap_module (0) == mod);
   21462              : 
   21463         2940 :   bool ok = true;
   21464              : 
   21465              :   /* Read direct imports.  */
   21466         2940 :   unsigned len = slurp->remap->length ();
   21467         3340 :   for (unsigned ix = 1; ok && ix != len; ix++)
   21468              :     {
   21469          400 :       unsigned map = (*slurp->remap)[ix];
   21470          400 :       if (map & 1)
   21471              :         {
   21472          396 :           module_state *import = (*modules)[map >> 1];
   21473          396 :           if (!import->read_language (false))
   21474          400 :             ok = false;
   21475              :         }
   21476              :     }
   21477              : 
   21478         2940 :   unsigned counts[MSC_HWM];
   21479              : 
   21480         2940 :   if (ok && !read_counts (counts))
   21481              :     ok = false;
   21482              : 
   21483         2940 :   function_depth++; /* Prevent unexpected GCs.  */
   21484              : 
   21485         2940 :   if (ok && counts[MSC_entities] != entity_num)
   21486              :     ok = false;
   21487         2940 :   if (ok && counts[MSC_entities]
   21488         2714 :       && !read_entities (counts[MSC_entities],
   21489              :                          counts[MSC_sec_lwm], counts[MSC_sec_hwm]))
   21490              :     ok = false;
   21491              : 
   21492              :   /* Read the namespace hierarchy.  */
   21493         2940 :   if (ok && counts[MSC_namespaces]
   21494         3543 :       && !read_namespaces (counts[MSC_namespaces]))
   21495              :     ok = false;
   21496              : 
   21497              :   /* Read any using-directives.  */
   21498         2940 :   if (ok && counts[MSC_using_directives]
   21499         3099 :       && !read_using_directives (counts[MSC_using_directives]))
   21500              :     ok = false;
   21501              : 
   21502         2940 :   if (ok && !read_bindings (counts[MSC_bindings],
   21503              :                             counts[MSC_sec_lwm], counts[MSC_sec_hwm]))
   21504              :     ok = false;
   21505              : 
   21506              :   /* And unnamed.  */
   21507         2940 :   if (ok && counts[MSC_pendings] && !read_pendings (counts[MSC_pendings]))
   21508              :     ok = false;
   21509              : 
   21510         2940 :   if (ok)
   21511              :     {
   21512         2940 :       slurp->remaining = counts[MSC_sec_hwm] - counts[MSC_sec_lwm];
   21513         2940 :       available_clusters += counts[MSC_sec_hwm] - counts[MSC_sec_lwm];
   21514              :     }
   21515              : 
   21516         2940 :   if (!flag_module_lazy
   21517         2940 :       || (is_partition ()
   21518          235 :           && module_interface_p ()
   21519          208 :           && !module_partition_p ()))
   21520              :     {
   21521              :       /* Read the sections in forward order, so that dependencies are read
   21522              :          first.  See note about tarjan_connect.  */
   21523          545 :       ggc_collect ();
   21524              : 
   21525          545 :       lazy_snum = ~0u;
   21526              : 
   21527          545 :       unsigned hwm = counts[MSC_sec_hwm];
   21528       144423 :       for (unsigned ix = counts[MSC_sec_lwm]; ok && ix != hwm; ix++)
   21529       143878 :         if (!load_section (ix, NULL))
   21530              :           {
   21531              :             ok = false;
   21532              :             break;
   21533              :           }
   21534          545 :       lazy_snum = 0;
   21535          545 :       post_load_processing ();
   21536              : 
   21537          545 :       ggc_collect ();
   21538              : 
   21539          545 :       if (ok && CHECKING_P)
   21540       516364 :         for (unsigned ix = 0; ix != entity_num; ix++)
   21541       515819 :           gcc_assert (!(*entity_ary)[ix + entity_lwm].is_lazy ());
   21542              :     }
   21543              : 
   21544              :   // If the import is a header-unit, we need to register initializers
   21545              :   // of any static objects it contains (looking at you _Ioinit).
   21546              :   // Notice, the ordering of these initializers will be that of a
   21547              :   // dynamic initializer at this point in the current TU.  (Other
   21548              :   // instances of these objects in other TUs will be initialized as
   21549              :   // part of that TU's global initializers.)
   21550         2940 :   if (ok && counts[MSC_inits] && !read_inits (counts[MSC_inits]))
   21551              :     ok = false;
   21552              : 
   21553         2940 :   function_depth--;
   21554              : 
   21555         3301 :   announce (flag_module_lazy ? "lazy" : "imported");
   21556         2940 :   loadedness = ML_LANGUAGE;
   21557              : 
   21558         2940 :   gcc_assert (slurp->current == ~0u);
   21559              : 
   21560              :   /* We're done with the string table.  */
   21561         2940 :   from ()->release ();
   21562              : 
   21563         2940 :   return check_read (outermost, ok);
   21564              : }
   21565              : 
   21566              : bool
   21567       211778 : module_state::maybe_defrost ()
   21568              : {
   21569       211778 :   bool ok = true;
   21570       211778 :   if (from ()->is_frozen ())
   21571              :     {
   21572            9 :       if (lazy_open >= lazy_limit)
   21573            3 :         freeze_an_elf ();
   21574           18 :       dump () && dump ("Defrosting '%s'", filename);
   21575            9 :       ok = from ()->defrost (maybe_add_cmi_prefix (filename));
   21576            9 :       lazy_open++;
   21577              :     }
   21578              : 
   21579       211778 :   return ok;
   21580              : }
   21581              : 
   21582              : /* Load section SNUM, dealing with laziness.  It doesn't matter if we
   21583              :    have multiple concurrent loads, because we do not use TREE_VISITED
   21584              :    when reading back in.  */
   21585              : 
   21586              : bool
   21587       211778 : module_state::load_section (unsigned snum, binding_slot *mslot)
   21588              : {
   21589       211778 :   if (from ()->get_error ())
   21590              :     return false;
   21591              : 
   21592       211778 :   if (snum >= slurp->current)
   21593            0 :     from ()->set_error (elf::E_BAD_LAZY);
   21594       211778 :   else if (maybe_defrost ())
   21595              :     {
   21596       211778 :       unsigned old_current = slurp->current;
   21597       211778 :       slurp->current = snum;
   21598       211778 :       slurp->lru = 0;  /* Do not swap out.  */
   21599       211778 :       slurp->remaining--;
   21600       211778 :       read_cluster (snum);
   21601       211778 :       slurp->lru = ++lazy_lru;
   21602       211778 :       slurp->current = old_current;
   21603              :     }
   21604              : 
   21605       211778 :   if (mslot && mslot->is_lazy ())
   21606              :     {
   21607              :       /* Oops, the section didn't set this slot.  */
   21608            0 :       from ()->set_error (elf::E_BAD_DATA);
   21609            0 :       *mslot = NULL_TREE;
   21610              :     }
   21611              : 
   21612       211778 :   bool ok = !from ()->get_error ();
   21613       211778 :   if (!ok)
   21614              :     {
   21615            0 :       error_at (loc, "failed to read compiled module cluster %u: %s",
   21616            0 :                 snum, from ()->get_error (filename));
   21617            0 :       note_cmi_name ();
   21618              :     }
   21619              : 
   21620       211778 :   maybe_completed_reading ();
   21621              : 
   21622       211778 :   return ok;
   21623              : }
   21624              : 
   21625              : void
   21626       218641 : module_state::maybe_completed_reading ()
   21627              : {
   21628       218641 :   if (loadedness == ML_LANGUAGE && slurp->current == ~0u && !slurp->remaining)
   21629              :     {
   21630         2427 :       lazy_open--;
   21631              :       /* We no longer need the macros, all tokenizing has been done.  */
   21632         2427 :       slurp->release_macros ();
   21633              : 
   21634         2427 :       from ()->end ();
   21635         2427 :       slurp->close ();
   21636         2427 :       slurped ();
   21637              :     }
   21638       218641 : }
   21639              : 
   21640              : /* After a reading operation, make sure things are still ok.  If not,
   21641              :    emit an error and clean up.  */
   21642              : 
   21643              : bool
   21644         6900 : module_state::check_read (bool outermost, bool ok)
   21645              : {
   21646         6900 :   gcc_checking_assert (!outermost || slurp->current == ~0u);
   21647              : 
   21648         6900 :   if (!ok)
   21649           34 :     from ()->set_error ();
   21650              : 
   21651         6900 :   if (int e = from ()->get_error ())
   21652              :     {
   21653           40 :       auto_diagnostic_group d;
   21654           40 :       error_at (loc, "failed to read compiled module: %s",
   21655           40 :                 from ()->get_error (filename));
   21656           40 :       note_cmi_name ();
   21657              : 
   21658           40 :       if (e == EMFILE
   21659           40 :           || e == ENFILE
   21660              : #if MAPPED_READING
   21661           40 :           || e == ENOMEM
   21662              : #endif
   21663              :           || false)
   21664            0 :         inform (loc, "consider using %<-fno-module-lazy%>,"
   21665              :                 " increasing %<-param-lazy-modules=%u%> value,"
   21666              :                 " or increasing the per-process file descriptor limit",
   21667              :                 param_lazy_modules);
   21668           40 :       else if (e == ENOENT)
   21669           21 :         inform (loc, "imports must be built before being imported");
   21670              : 
   21671           40 :       if (outermost)
   21672           37 :         fatal_error (loc, "returning to the gate for a mechanical issue");
   21673              : 
   21674            3 :       ok = false;
   21675            3 :     }
   21676              : 
   21677         6863 :   maybe_completed_reading ();
   21678              : 
   21679         6863 :   return ok;
   21680              : }
   21681              : 
   21682              : /* Return the IDENTIFIER_NODE naming module IX.  This is the name
   21683              :    including dots.  */
   21684              : 
   21685              : char const *
   21686          408 : module_name (unsigned ix, bool header_ok)
   21687              : {
   21688          408 :   if (modules)
   21689              :     {
   21690          408 :       module_state *imp = (*modules)[ix];
   21691              : 
   21692          408 :       if (ix && !imp->name)
   21693            0 :         imp = imp->parent;
   21694              : 
   21695          408 :       if (header_ok || !imp->is_header ())
   21696          408 :         return imp->get_flatname ();
   21697              :     }
   21698              : 
   21699              :   return NULL;
   21700              : }
   21701              : 
   21702              : /* Return the bitmap describing what modules are imported.  Remember,
   21703              :    we always import ourselves.  */
   21704              : 
   21705              : bitmap
   21706       129734 : get_import_bitmap ()
   21707              : {
   21708       129734 :   return this_module ()->imports;
   21709              : }
   21710              : 
   21711              : /* Get the original decl for an instantiation at TINST, or NULL_TREE
   21712              :    if we're not an instantiation.  */
   21713              : 
   21714              : static tree
   21715       211613 : orig_decl_for_instantiation (tinst_level *tinst)
   21716              : {
   21717       211613 :   if (!tinst || TREE_CODE (tinst->tldcl) == TEMPLATE_FOR_STMT)
   21718              :     return NULL_TREE;
   21719              : 
   21720       104767 :   tree decl = tinst->tldcl;
   21721       104767 :   if (TREE_CODE (decl) == TREE_LIST)
   21722            0 :     decl = TREE_PURPOSE (decl);
   21723       104767 :   if (TYPE_P (decl))
   21724        16079 :     decl = TYPE_NAME (decl);
   21725              :   return decl;
   21726              : }
   21727              : 
   21728              : /* Return the visible imports and path of instantiation for an
   21729              :    instantiation at TINST.  If TINST is nullptr, we're not in an
   21730              :    instantiation, and thus will return the visible imports of the
   21731              :    current TU (and NULL *PATH_MAP_P).   We cache the information on
   21732              :    the tinst level itself.  */
   21733              : 
   21734              : static bitmap
   21735       156120 : path_of_instantiation (tinst_level *tinst, bitmap *path_map_p)
   21736              : {
   21737       156120 :   gcc_checking_assert (modules_p ());
   21738              : 
   21739       156120 :   tree decl = orig_decl_for_instantiation (tinst);
   21740       156120 :   if (!decl)
   21741              :     {
   21742        68202 :       gcc_assert (!tinst || !tinst->next);
   21743              :       /* Not inside an instantiation, just the regular case.  */
   21744        68202 :       *path_map_p = nullptr;
   21745        68202 :       return get_import_bitmap ();
   21746              :     }
   21747              : 
   21748        87918 :   if (!tinst->path)
   21749              :     {
   21750              :       /* Calculate.  */
   21751        26195 :       bitmap visible = path_of_instantiation (tinst->next, path_map_p);
   21752        26195 :       bitmap path_map = *path_map_p;
   21753              : 
   21754        26195 :       if (!path_map)
   21755              :         {
   21756         3389 :           path_map = BITMAP_GGC_ALLOC ();
   21757         3389 :           bitmap_set_bit (path_map, 0);
   21758              :         }
   21759              : 
   21760        26195 :       if (unsigned mod = get_originating_module (decl))
   21761         3867 :         if (!bitmap_bit_p (path_map, mod))
   21762              :           {
   21763              :             /* This is brand new information!  */
   21764          172 :             bitmap new_path = BITMAP_GGC_ALLOC ();
   21765          172 :             bitmap_copy (new_path, path_map);
   21766          172 :             bitmap_set_bit (new_path, mod);
   21767          172 :             path_map = new_path;
   21768              : 
   21769          172 :             bitmap imports = (*modules)[mod]->imports;
   21770          172 :             if (bitmap_intersect_compl_p (imports, visible))
   21771              :               {
   21772              :                 /* IMPORTS contains additional modules to VISIBLE.  */
   21773           33 :                 bitmap new_visible = BITMAP_GGC_ALLOC ();
   21774              : 
   21775           33 :                 bitmap_ior (new_visible, visible, imports);
   21776           33 :                 visible = new_visible;
   21777              :               }
   21778              :           }
   21779              : 
   21780        26195 :       tinst->path = path_map;
   21781        26195 :       tinst->visible = visible;
   21782              :     }
   21783              : 
   21784        87918 :   *path_map_p = tinst->path;
   21785        87918 :   return tinst->visible;
   21786              : }
   21787              : 
   21788              : /* Return the bitmap describing what modules are visible along the
   21789              :    path of instantiation.  If we're not an instantiation, this will be
   21790              :    the visible imports of the TU.  *PATH_MAP_P is filled in with the
   21791              :    modules owning the instantiation path -- we see the module-linkage
   21792              :    entities of those modules.  */
   21793              : 
   21794              : bitmap
   21795     31890902 : visible_instantiation_path (bitmap *path_map_p)
   21796              : {
   21797     31890902 :   if (!modules_p ())
   21798              :     return NULL;
   21799              : 
   21800       129925 :   return path_of_instantiation (current_instantiation (), path_map_p);
   21801              : }
   21802              : 
   21803              : /* Returns the bitmap describing what modules were visible from the
   21804              :    module that the current instantiation originated from.  If we're
   21805              :    not an instantiation, returns NULL.  *MODULE_P is filled in with
   21806              :    the originating module of the definition for this instantiation.  */
   21807              : 
   21808              : bitmap
   21809        55493 : visible_from_instantiation_origination (unsigned *module_p)
   21810              : {
   21811        55493 :   if (!modules_p ())
   21812              :     return NULL;
   21813              : 
   21814        55493 :   tree decl = orig_decl_for_instantiation (current_instantiation ());
   21815        55493 :   if (!decl)
   21816              :     return NULL;
   21817              : 
   21818        16849 :   *module_p = get_originating_module (decl);
   21819        16849 :   return (*modules)[*module_p]->imports;
   21820              : }
   21821              : 
   21822              : /* We've just directly imported IMPORT.  Update our import/export
   21823              :    bitmaps.  IS_EXPORT is true if we're reexporting the OTHER.  */
   21824              : 
   21825              : void
   21826         3091 : module_state::set_import (module_state const *import, bool is_export)
   21827              : {
   21828         3091 :   gcc_checking_assert (this != import);
   21829              : 
   21830              :   /* We see IMPORT's exports (which includes IMPORT).  If IMPORT is
   21831              :      the primary interface or a partition we'll see its imports.  */
   21832         3091 :   bitmap_ior_into (imports, import->is_module () || import->is_partition ()
   21833              :                    ? import->imports : import->exports);
   21834              : 
   21835         3091 :   if (is_export)
   21836              :     /* We'll export OTHER's exports.  */
   21837          480 :     bitmap_ior_into (exports, import->exports);
   21838         3091 : }
   21839              : 
   21840              : /* Return the declaring entity of DECL.  That is the decl determining
   21841              :    how to decorate DECL with module information.  Returns NULL_TREE if
   21842              :    it's the global module.  */
   21843              : 
   21844              : tree
   21845    108196795 : get_originating_module_decl (tree decl)
   21846              : {
   21847              :   /* An enumeration constant.  */
   21848    108196795 :   if (TREE_CODE (decl) == CONST_DECL
   21849         8381 :       && DECL_CONTEXT (decl)
   21850    108205176 :       && (TREE_CODE (DECL_CONTEXT (decl)) == ENUMERAL_TYPE))
   21851         7604 :     decl = TYPE_NAME (DECL_CONTEXT (decl));
   21852    108189191 :   else if (TREE_CODE (decl) == FIELD_DECL
   21853    108156406 :            || TREE_CODE (decl) == USING_DECL
   21854    216325462 :            || CONST_DECL_USING_P (decl))
   21855              :     {
   21856        53697 :       decl = DECL_CONTEXT (decl);
   21857        53697 :       if (TREE_CODE (decl) != FUNCTION_DECL)
   21858        53697 :         decl = TYPE_NAME (decl);
   21859              :     }
   21860              : 
   21861    108196795 :   gcc_checking_assert (TREE_CODE (decl) == TEMPLATE_DECL
   21862              :                        || TREE_CODE (decl) == FUNCTION_DECL
   21863              :                        || TREE_CODE (decl) == TYPE_DECL
   21864              :                        || TREE_CODE (decl) == VAR_DECL
   21865              :                        || TREE_CODE (decl) == CONCEPT_DECL
   21866              :                        || TREE_CODE (decl) == NAMESPACE_DECL);
   21867              : 
   21868    110791963 :   for (;;)
   21869              :     {
   21870              :       /* Uninstantiated template friends are owned by the befriending
   21871              :          class -- not their context.  */
   21872    109494379 :       if (TREE_CODE (decl) == TEMPLATE_DECL
   21873    109494379 :           && DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (decl))
   21874        11545 :         decl = TYPE_NAME (DECL_CHAIN (decl));
   21875              : 
   21876              :       /* An imported temploid friend is attached to the same module the
   21877              :          befriending class was.  */
   21878    109494379 :       if (imported_temploid_friends)
   21879      3838392 :         if (tree *slot = imported_temploid_friends->get (decl))
   21880          741 :           decl = *slot;
   21881              : 
   21882    109494379 :       int use;
   21883    109494379 :       if (tree ti = node_template_info (decl, use))
   21884              :         {
   21885       985371 :           decl = TI_TEMPLATE (ti);
   21886       985371 :           if (TREE_CODE (decl) != TEMPLATE_DECL)
   21887              :             {
   21888              :               /* A friend template specialization.  */
   21889           43 :               gcc_checking_assert (OVL_P (decl));
   21890           43 :               return global_namespace;
   21891              :             }
   21892              :         }
   21893              :       else
   21894              :         {
   21895    108509008 :           tree ctx = CP_DECL_CONTEXT (decl);
   21896    108509008 :           if (TREE_CODE (ctx) == NAMESPACE_DECL)
   21897              :             break;
   21898              : 
   21899       312256 :           if (TYPE_P (ctx))
   21900              :             {
   21901       280779 :               ctx = TYPE_NAME (ctx);
   21902       280779 :               if (!ctx)
   21903              :                 {
   21904              :                   /* Some kind of internal type.  */
   21905            0 :                   gcc_checking_assert (DECL_ARTIFICIAL (decl));
   21906            0 :                   return global_namespace;
   21907              :                 }
   21908              :             }
   21909       312256 :           decl = ctx;
   21910              :         }
   21911      1297584 :     }
   21912              : 
   21913    108196752 :   return decl;
   21914              : }
   21915              : 
   21916              : /* If DECL is imported, return which module imported it, or 0 for the current
   21917              :    module.  Except that if GLOBAL_M1, return -1 for decls attached to the
   21918              :    global module.  */
   21919              : 
   21920              : int
   21921      1606566 : get_originating_module (tree decl, bool global_m1)
   21922              : {
   21923      1606566 :   tree owner = get_originating_module_decl (decl);
   21924      1606566 :   tree not_tmpl = STRIP_TEMPLATE (owner);
   21925              : 
   21926      1606566 :   if (!DECL_LANG_SPECIFIC (not_tmpl))
   21927       475648 :     return global_m1 ? -1 : 0;
   21928              : 
   21929      2210172 :   if (global_m1 && !DECL_MODULE_ATTACH_P (not_tmpl))
   21930              :     return -1;
   21931              : 
   21932       119299 :   int mod = !DECL_MODULE_IMPORT_P (not_tmpl) ? 0 : get_importing_module (owner);
   21933       119299 :   gcc_checking_assert (!global_m1 || !(*modules)[mod]->is_header ());
   21934              :   return mod;
   21935              : }
   21936              : 
   21937              : /* DECL is imported, return which module imported it.
   21938              :    If FLEXIBLE, return -1 if not found, otherwise checking ICE.  */
   21939              : 
   21940              : unsigned
   21941        16046 : get_importing_module (tree decl, bool flexible)
   21942              : {
   21943        16046 :   unsigned index = import_entity_index (decl, flexible);
   21944        16046 :   if (index == ~(~0u >> 1))
   21945              :     return -1;
   21946        16046 :   module_state *module = import_entity_module (index);
   21947              : 
   21948        16046 :   return module->mod;
   21949              : }
   21950              : 
   21951              : /* Is it permissible to redeclare OLDDECL with NEWDECL.
   21952              : 
   21953              :    If NEWDECL is NULL, assumes that OLDDECL will be redeclared using
   21954              :    the current scope's module and attachment.  */
   21955              : 
   21956              : bool
   21957       206024 : module_may_redeclare (tree olddecl, tree newdecl)
   21958              : {
   21959       206024 :   tree decl = olddecl;
   21960       278330 :   for (;;)
   21961              :     {
   21962       242177 :       tree ctx = CP_DECL_CONTEXT (decl);
   21963       242177 :       if (TREE_CODE (ctx) == NAMESPACE_DECL)
   21964              :         // Found the namespace-scope decl.
   21965              :         break;
   21966        37611 :       if (!CLASS_TYPE_P (ctx))
   21967              :         // We've met a non-class scope.  Such a thing is not
   21968              :         // reopenable, so we must be ok.
   21969              :         return true;
   21970        36153 :       decl = TYPE_NAME (ctx);
   21971        36153 :     }
   21972              : 
   21973       204566 :   int use_tpl = 0;
   21974       204566 :   if (node_template_info (STRIP_TEMPLATE (decl), use_tpl) && use_tpl)
   21975              :     // Specializations of any kind can be redeclared anywhere.
   21976              :     // FIXME: Should we be checking this in more places on the scope chain?
   21977              :     return true;
   21978              : 
   21979       145888 :   module_state *old_mod = get_primary (this_module ());
   21980       145888 :   module_state *new_mod = old_mod;
   21981              : 
   21982       145888 :   tree old_origin = get_originating_module_decl (decl);
   21983       145888 :   tree old_inner = STRIP_TEMPLATE (old_origin);
   21984       145888 :   bool olddecl_attached_p = (DECL_LANG_SPECIFIC (old_inner)
   21985       222447 :                              && DECL_MODULE_ATTACH_P (old_inner));
   21986       222447 :   if (DECL_LANG_SPECIFIC (old_inner) && DECL_MODULE_IMPORT_P (old_inner))
   21987              :     {
   21988          843 :       unsigned index = import_entity_index (old_origin);
   21989          843 :       old_mod = get_primary (import_entity_module (index));
   21990              :     }
   21991              : 
   21992       145888 :   bool newdecl_attached_p = module_attach_p ();
   21993       145888 :   if (newdecl)
   21994              :     {
   21995        36275 :       tree new_origin = get_originating_module_decl (newdecl);
   21996        36275 :       tree new_inner = STRIP_TEMPLATE (new_origin);
   21997        36275 :       newdecl_attached_p = (DECL_LANG_SPECIFIC (new_inner)
   21998        70299 :                             && DECL_MODULE_ATTACH_P (new_inner));
   21999        70299 :       if (DECL_LANG_SPECIFIC (new_inner) && DECL_MODULE_IMPORT_P (new_inner))
   22000              :         {
   22001          175 :           unsigned index = import_entity_index (new_origin);
   22002          175 :           new_mod = get_primary (import_entity_module (index));
   22003              :         }
   22004              :     }
   22005              : 
   22006              :   /* Module attachment needs to match.  */
   22007       145888 :   if (olddecl_attached_p == newdecl_attached_p)
   22008              :     {
   22009       145744 :       if (!olddecl_attached_p)
   22010              :         /* Both are GM entities, OK.  */
   22011              :         return true;
   22012              : 
   22013         1428 :       if (new_mod == old_mod)
   22014              :         /* Both attached to same named module, OK.  */
   22015              :         return true;
   22016              :     }
   22017              : 
   22018              :   /* Attached to different modules, error.  */
   22019          171 :   decl = newdecl ? newdecl : olddecl;
   22020          171 :   location_t loc = newdecl ? DECL_SOURCE_LOCATION (newdecl) : input_location;
   22021          171 :   if (DECL_IS_UNDECLARED_BUILTIN (olddecl))
   22022              :     {
   22023            3 :       if (newdecl_attached_p)
   22024            3 :         error_at (loc, "declaring %qD in module %qs conflicts with builtin "
   22025              :                   "in global module", decl, new_mod->get_flatname ());
   22026              :       else
   22027            0 :         error_at (loc, "declaration %qD conflicts with builtin", decl);
   22028              :     }
   22029          333 :   else if (DECL_LANG_SPECIFIC (old_inner) && DECL_MODULE_IMPORT_P (old_inner))
   22030              :     {
   22031          153 :       auto_diagnostic_group d;
   22032          153 :       if (newdecl_attached_p)
   22033           75 :         error_at (loc, "redeclaring %qD in module %qs conflicts with import",
   22034              :                   decl, new_mod->get_flatname ());
   22035              :       else
   22036           78 :         error_at (loc, "redeclaring %qD in global module conflicts with import",
   22037              :                   decl);
   22038              : 
   22039          153 :       if (olddecl_attached_p)
   22040          105 :         inform (DECL_SOURCE_LOCATION (olddecl),
   22041              :                 "import declared attached to module %qs",
   22042              :                 old_mod->get_flatname ());
   22043              :       else
   22044           48 :         inform (DECL_SOURCE_LOCATION (olddecl),
   22045              :                 "import declared in global module");
   22046          153 :     }
   22047              :   else
   22048              :     {
   22049           15 :       auto_diagnostic_group d;
   22050           15 :       if (newdecl_attached_p)
   22051           15 :         error_at (loc, "conflicting declaration of %qD in module %qs",
   22052              :                   decl, new_mod->get_flatname ());
   22053              :       else
   22054            0 :         error_at (loc, "conflicting declaration of %qD in global module",
   22055              :                   decl);
   22056              : 
   22057           15 :       if (olddecl_attached_p)
   22058            0 :         inform (DECL_SOURCE_LOCATION (olddecl),
   22059              :                 "previously declared in module %qs",
   22060              :                 old_mod->get_flatname ());
   22061              :       else
   22062           15 :         inform (DECL_SOURCE_LOCATION (olddecl),
   22063              :                 "previously declared in global module");
   22064           15 :     }
   22065              :   return false;
   22066              : }
   22067              : 
   22068              : /* DECL is being created by this TU.  Record it came from here.  We
   22069              :    record module purview, so we can see if partial or explicit
   22070              :    specialization needs to be written out, even though its purviewness
   22071              :    comes from the most general template.  */
   22072              : 
   22073              : void
   22074    930306561 : set_instantiating_module (tree decl)
   22075              : {
   22076    930306561 :   gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
   22077              :               || VAR_P (decl)
   22078              :               || TREE_CODE (decl) == TYPE_DECL
   22079              :               || TREE_CODE (decl) == CONCEPT_DECL
   22080              :               || TREE_CODE (decl) == TEMPLATE_DECL
   22081              :               || TREE_CODE (decl) == CONST_DECL
   22082              :               || (TREE_CODE (decl) == NAMESPACE_DECL
   22083              :                   && DECL_NAMESPACE_ALIAS (decl)));
   22084              : 
   22085    930306561 :   if (!modules_p ())
   22086              :     return;
   22087              : 
   22088      3786759 :   decl = STRIP_TEMPLATE (decl);
   22089              : 
   22090      3786759 :   if (!DECL_LANG_SPECIFIC (decl) && module_purview_p ())
   22091       370565 :     retrofit_lang_decl (decl);
   22092              : 
   22093      3786759 :   if (DECL_LANG_SPECIFIC (decl))
   22094              :     {
   22095      2897379 :       DECL_MODULE_PURVIEW_P (decl) = module_purview_p ();
   22096              :       /* If this was imported, we'll still be in the entity_hash.  */
   22097      2897379 :       DECL_MODULE_IMPORT_P (decl) = false;
   22098              :     }
   22099              : }
   22100              : 
   22101              : /* If DECL is a class member, whose class is not defined in this TU
   22102              :    (it was imported), remember this decl.  */
   22103              : 
   22104              : void
   22105       151922 : set_defining_module (tree decl)
   22106              : {
   22107       151922 :   gcc_checking_assert (!DECL_LANG_SPECIFIC (decl)
   22108              :                        || !DECL_MODULE_IMPORT_P (decl));
   22109              : 
   22110       151922 :   if (module_maybe_has_cmi_p ())
   22111              :     {
   22112              :       /* We need to track all declarations within a module, not just those
   22113              :          in the module purview, because we don't necessarily know yet if
   22114              :          this module will require a CMI while in the global fragment.  */
   22115       102206 :       tree ctx = DECL_CONTEXT (decl);
   22116       102206 :       if (ctx
   22117       102206 :           && (TREE_CODE (ctx) == RECORD_TYPE || TREE_CODE (ctx) == UNION_TYPE)
   22118        21250 :           && DECL_LANG_SPECIFIC (TYPE_NAME (ctx))
   22119       113647 :           && DECL_MODULE_IMPORT_P (TYPE_NAME (ctx)))
   22120              :         {
   22121              :           /* This entity's context is from an import.  We may need to
   22122              :              record this entity to make sure we emit it in the CMI.
   22123              :              Template specializations are in the template hash tables,
   22124              :              so we don't need to record them here as well.  */
   22125           39 :           int use_tpl = -1;
   22126           39 :           tree ti = node_template_info (decl, use_tpl);
   22127           39 :           if (use_tpl <= 0)
   22128              :             {
   22129           39 :               if (ti)
   22130              :                 {
   22131           21 :                   gcc_checking_assert (!use_tpl);
   22132              :                   /* Get to the TEMPLATE_DECL.  */
   22133           21 :                   decl = TI_TEMPLATE (ti);
   22134              :                 }
   22135              : 
   22136              :               /* Record it on the class_members list.  */
   22137           39 :               vec_safe_push (class_members, decl);
   22138              :             }
   22139              :         }
   22140              :     }
   22141       151922 : }
   22142              : 
   22143              : /* Also remember DECL if it's a newly declared class template partial
   22144              :    specialization, because these are not necessarily added to the
   22145              :    instantiation tables.  */
   22146              : 
   22147              : void
   22148      8034870 : set_defining_module_for_partial_spec (tree decl)
   22149              : {
   22150      8034870 :   if (module_maybe_has_cmi_p ()
   22151        25309 :       && DECL_IMPLICIT_TYPEDEF_P (decl)
   22152      8056280 :       && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
   22153        21410 :     vec_safe_push (partial_specializations, decl);
   22154      8034870 : }
   22155              : 
   22156              : /* Record that DECL is declared in this TU, and note attachment and
   22157              :    exporting for namespace-scope entities.  FRIEND_P is true if
   22158              :    this is a friend declaration.  */
   22159              : 
   22160              : void
   22161    301642106 : set_originating_module (tree decl, bool friend_p ATTRIBUTE_UNUSED)
   22162              : {
   22163    301642106 :   set_instantiating_module (decl);
   22164              : 
   22165              :   /* DECL_CONTEXT may not be set yet when we're called for
   22166              :      non-namespace-scope entities.  */
   22167    301642106 :   if (!DECL_CONTEXT (decl) || !DECL_NAMESPACE_SCOPE_P (decl))
   22168              :     return;
   22169              : 
   22170    112437603 :   gcc_checking_assert (friend_p || decl == get_originating_module_decl (decl));
   22171              : 
   22172    112437603 :   if (module_attach_p ())
   22173              :     {
   22174         4287 :       retrofit_lang_decl (decl);
   22175         4287 :       DECL_MODULE_ATTACH_P (decl) = true;
   22176              :     }
   22177              : 
   22178              :   /* It is ill-formed to export a declaration with internal linkage.  However,
   22179              :      at the point this function is called we don't yet always know whether this
   22180              :      declaration has internal linkage; instead we defer this check for callers
   22181              :      to do once visibility has been determined.  */
   22182    112437603 :   if (module_exporting_p ())
   22183       157952 :     DECL_MODULE_EXPORT_P (decl) = true;
   22184              : }
   22185              : 
   22186              : /* Checks whether DECL within a module unit has valid linkage for its kind.
   22187              :    Must be called after visibility for DECL has been finalised.  */
   22188              : 
   22189              : void
   22190    378643586 : check_module_decl_linkage (tree decl)
   22191              : {
   22192    378643586 :   if (!module_has_cmi_p ())
   22193              :     return;
   22194              : 
   22195              :   /* A header unit shall not contain a definition of a non-inline function
   22196              :      or variable (not template) whose name has external linkage.  */
   22197       532576 :   if (header_module_p ()
   22198       480918 :       && !processing_template_decl
   22199       159534 :       && ((TREE_CODE (decl) == FUNCTION_DECL
   22200        94398 :            && !DECL_DECLARED_INLINE_P (decl))
   22201       123619 :           || (TREE_CODE (decl) == VAR_DECL
   22202        37266 :               && !DECL_INLINE_VAR_P (decl)))
   22203        45164 :       && decl_defined_p (decl)
   22204         4473 :       && !(DECL_LANG_SPECIFIC (decl)
   22205         4473 :            && DECL_TEMPLATE_INSTANTIATION (decl))
   22206       537049 :       && DECL_EXTERNAL_LINKAGE_P (decl))
   22207           60 :     error_at (DECL_SOURCE_LOCATION (decl),
   22208              :               "external linkage definition of %qD in header module must "
   22209              :               "be declared %<inline%>", decl);
   22210              : 
   22211              :   /* An internal-linkage declaration cannot be generally be exported.
   22212              :      But it's OK to export any declaration from a header unit, including
   22213              :      internal linkage declarations.  */
   22214       532576 :   if (!header_module_p () && DECL_MODULE_EXPORT_P (decl))
   22215              :     {
   22216              :       /* Let's additionally treat any exported declaration within an
   22217              :          internal namespace as exporting a declaration with internal
   22218              :          linkage, as this would also implicitly export the internal
   22219              :          linkage namespace.  */
   22220         1986 :       if (decl_anon_ns_mem_p (decl))
   22221              :         {
   22222           50 :           error_at (DECL_SOURCE_LOCATION (decl),
   22223              :                     "exporting declaration %qD declared in unnamed namespace",
   22224              :                     decl);
   22225           50 :           DECL_MODULE_EXPORT_P (decl) = false;
   22226              :         }
   22227         1936 :       else if (decl_linkage (decl) == lk_internal)
   22228              :         {
   22229           17 :           error_at (DECL_SOURCE_LOCATION (decl),
   22230              :                     "exporting declaration %qD with internal linkage", decl);
   22231           17 :           DECL_MODULE_EXPORT_P (decl) = false;
   22232              :         }
   22233              :     }
   22234              : }
   22235              : 
   22236              : /* Given a scope CTX, find the scope we want to attach the key to,
   22237              :    or NULL if no key scope is required.  */
   22238              : 
   22239              : static tree
   22240         2830 : adjust_key_scope (tree ctx)
   22241              : {
   22242              :   /* For members, key it to the containing type to handle deduplication
   22243              :      correctly.  For fields, this is necessary as FIELD_DECLs have no
   22244              :      dep and so would only be streamed after the lambda type, defeating
   22245              :      our ability to merge them.
   22246              : 
   22247              :      Other class-scope key decls might depend on the type of the lambda
   22248              :      but be within the same cluster; we need to ensure that we never
   22249              :      first see the key decl while streaming the lambda type as merging
   22250              :      would then fail when comparing the partially-streamed lambda type
   22251              :      of the key decl with the existing (PR c++/122310).
   22252              : 
   22253              :      Perhaps sort_cluster can be adjusted to handle this better, but
   22254              :      this is a simple workaround (and might down on the number of
   22255              :      entries in keyed_table as a bonus).  */
   22256         4711 :   while (!DECL_NAMESPACE_SCOPE_P (ctx))
   22257         3762 :     if (DECL_CLASS_SCOPE_P (ctx))
   22258         1767 :       ctx = TYPE_NAME (DECL_CONTEXT (ctx));
   22259              :     else
   22260          114 :       ctx = DECL_CONTEXT (ctx);
   22261              : 
   22262         2830 :   return ctx;
   22263              : }
   22264              : 
   22265              : /* DECL is keyed to CTX for odr purposes.  */
   22266              : 
   22267              : void
   22268      1717669 : maybe_key_decl (tree ctx, tree decl)
   22269              : {
   22270      1717669 :   if (!modules_p ())
   22271              :     return;
   22272              : 
   22273              :   /* We only need to deal here with decls attached to var, field,
   22274              :      parm, type, function, or concept decls.  */
   22275        11945 :   if (TREE_CODE (ctx) != VAR_DECL
   22276        11945 :       && TREE_CODE (ctx) != FIELD_DECL
   22277              :       && TREE_CODE (ctx) != PARM_DECL
   22278              :       && TREE_CODE (ctx) != TYPE_DECL
   22279              :       && TREE_CODE (ctx) != FUNCTION_DECL
   22280              :       && TREE_CODE (ctx) != CONCEPT_DECL)
   22281              :     return;
   22282              : 
   22283        23887 :   gcc_checking_assert (LAMBDA_TYPE_P (TREE_TYPE (decl))
   22284              :                        || TREE_CODE (ctx) == FUNCTION_DECL);
   22285              : 
   22286              :   /* We don't need to use the keyed map for functions with definitions,
   22287              :      as we can instead use the MK_local_type handling for streaming.  */
   22288        11945 :   if (TREE_CODE (ctx) == FUNCTION_DECL
   22289        11945 :       && (has_definition (ctx)
   22290              :           /* If we won't be streaming this definition there's also no
   22291              :              need to record the key, as it will not be useful for merging
   22292              :              (this function is non-inline and so a matching declaration
   22293              :              will always be an ODR violation anyway).  */
   22294          120 :           || !module_maybe_has_cmi_p ()))
   22295              :     return;
   22296              : 
   22297          662 :   ctx = adjust_key_scope (ctx);
   22298              : 
   22299          662 :   if (!keyed_table)
   22300          164 :     keyed_table = new keyed_map_t (EXPERIMENT (1, 400));
   22301              : 
   22302          662 :   auto &vec = keyed_table->get_or_insert (ctx);
   22303          662 :   if (!vec.length ())
   22304              :     {
   22305          610 :       retrofit_lang_decl (ctx);
   22306          610 :       DECL_MODULE_KEYED_DECLS_P (ctx) = true;
   22307              :     }
   22308          662 :   if (CHECKING_P)
   22309          824 :     for (tree t : vec)
   22310           58 :       gcc_checking_assert (t != decl);
   22311              : 
   22312          662 :   vec.safe_push (decl);
   22313              : }
   22314              : 
   22315              : /* Find the scope that the local type or lambda DECL is keyed to, if any.  */
   22316              : 
   22317              : static tree
   22318         2204 : get_keyed_decl_scope (tree decl)
   22319              : {
   22320         2204 :   gcc_checking_assert (DECL_IMPLICIT_TYPEDEF_P (STRIP_TEMPLATE (decl)));
   22321              : 
   22322         6612 :   tree scope = (LAMBDA_TYPE_P (TREE_TYPE (decl))
   22323         4180 :                 ? LAMBDA_TYPE_EXTRA_SCOPE (TREE_TYPE (decl))
   22324         2204 :                 : CP_DECL_CONTEXT (decl));
   22325         2204 :   if (!scope)
   22326              :     return NULL_TREE;
   22327              : 
   22328         2168 :   gcc_checking_assert (TREE_CODE (scope) == VAR_DECL
   22329              :                        || TREE_CODE (scope) == FIELD_DECL
   22330              :                        || TREE_CODE (scope) == PARM_DECL
   22331              :                        || TREE_CODE (scope) == TYPE_DECL
   22332              :                        || (TREE_CODE (scope) == FUNCTION_DECL
   22333              :                            && !has_definition (scope))
   22334              :                        || TREE_CODE (scope) == CONCEPT_DECL);
   22335              : 
   22336         2168 :   scope = adjust_key_scope (scope);
   22337              : 
   22338         4336 :   gcc_checking_assert (scope
   22339              :                        && DECL_LANG_SPECIFIC (scope)
   22340              :                        && DECL_MODULE_KEYED_DECLS_P (scope));
   22341              :   return scope;
   22342              : }
   22343              : 
   22344              : /* DECL is an instantiated friend that should be attached to the same
   22345              :    module that ORIG is.  */
   22346              : 
   22347              : void
   22348      2473211 : propagate_defining_module (tree decl, tree orig)
   22349              : {
   22350      2473211 :   if (!modules_p ())
   22351              :     return;
   22352              : 
   22353         7364 :   tree not_tmpl = STRIP_TEMPLATE (orig);
   22354        14672 :   if (DECL_LANG_SPECIFIC (not_tmpl) && DECL_MODULE_ATTACH_P (not_tmpl))
   22355              :     {
   22356          126 :       tree inner = STRIP_TEMPLATE (decl);
   22357          126 :       retrofit_lang_decl (inner);
   22358          126 :       DECL_MODULE_ATTACH_P (inner) = true;
   22359              :     }
   22360              : 
   22361        14672 :   if (DECL_LANG_SPECIFIC (not_tmpl) && DECL_MODULE_IMPORT_P (not_tmpl))
   22362              :     {
   22363          387 :       bool exists = imported_temploid_friends->put (decl, orig);
   22364              : 
   22365              :       /* We should only be called if lookup for an existing decl
   22366              :          failed, in which case there shouldn't already be an entry
   22367              :          in the map.  */
   22368          387 :       gcc_assert (!exists);
   22369              :     }
   22370              : }
   22371              : 
   22372              : /* NEWDECL matched with OLDDECL, transfer defining module information
   22373              :    onto OLDDECL.  We've already validated attachment matches.  */
   22374              : 
   22375              : void
   22376     19311012 : transfer_defining_module (tree olddecl, tree newdecl)
   22377              : {
   22378     19311012 :   if (!modules_p ())
   22379              :     return;
   22380              : 
   22381        60855 :   tree old_inner = STRIP_TEMPLATE (olddecl);
   22382        60855 :   tree new_inner = STRIP_TEMPLATE (newdecl);
   22383              : 
   22384        60855 :   if (DECL_LANG_SPECIFIC (new_inner))
   22385              :     {
   22386        60715 :       gcc_checking_assert (DECL_LANG_SPECIFIC (old_inner));
   22387        60715 :       if (DECL_MODULE_PURVIEW_P (new_inner))
   22388        22634 :         DECL_MODULE_PURVIEW_P (old_inner) = true;
   22389        60715 :       if (!DECL_MODULE_IMPORT_P (new_inner))
   22390        60715 :         DECL_MODULE_IMPORT_P (old_inner) = false;
   22391              :     }
   22392              : 
   22393        60855 :   if (tree *p = imported_temploid_friends->get (newdecl))
   22394              :     {
   22395           70 :       tree orig = *p;
   22396           70 :       tree &slot = imported_temploid_friends->get_or_insert (olddecl);
   22397           70 :       if (!slot)
   22398           47 :         slot = orig;
   22399           23 :       else if (slot != orig)
   22400              :         /* This can happen when multiple classes declare the same
   22401              :            friend function (e.g. g++.dg/modules/tpl-friend-4);
   22402              :            make sure we at least attach to the same module.  */
   22403            3 :         gcc_checking_assert (get_originating_module (slot)
   22404              :                              == get_originating_module (orig));
   22405              :     }
   22406              : }
   22407              : 
   22408              : /* DECL is being freed, clear data we don't need anymore.  */
   22409              : 
   22410              : void
   22411        43460 : remove_defining_module (tree decl)
   22412              : {
   22413        43460 :   if (!modules_p ())
   22414              :     return;
   22415              : 
   22416        43460 :   if (imported_temploid_friends)
   22417        43460 :     imported_temploid_friends->remove (decl);
   22418              : }
   22419              : 
   22420              : /* Create the flat name string.  It is simplest to have it handy.  */
   22421              : 
   22422              : void
   22423         6377 : module_state::set_flatname ()
   22424              : {
   22425         6377 :   gcc_checking_assert (!flatname);
   22426         6377 :   if (parent)
   22427              :     {
   22428          680 :       auto_vec<tree,5> ids;
   22429          680 :       size_t len = 0;
   22430          680 :       char const *primary = NULL;
   22431          680 :       size_t pfx_len = 0;
   22432              : 
   22433          680 :       for (module_state *probe = this;
   22434         1663 :            probe;
   22435          983 :            probe = probe->parent)
   22436         1495 :         if (is_partition () && !probe->is_partition ())
   22437              :           {
   22438          512 :             primary = probe->get_flatname ();
   22439          512 :             pfx_len = strlen (primary);
   22440          512 :             break;
   22441              :           }
   22442              :         else
   22443              :           {
   22444          983 :             ids.safe_push (probe->name);
   22445          983 :             len += IDENTIFIER_LENGTH (probe->name) + 1;
   22446              :           }
   22447              : 
   22448          680 :       char *flat = XNEWVEC (char, pfx_len + len + is_partition ());
   22449          680 :       flatname = flat;
   22450              : 
   22451          680 :       if (primary)
   22452              :         {
   22453          512 :           memcpy (flat, primary, pfx_len);
   22454          512 :           flat += pfx_len;
   22455          512 :           *flat++ = ':';
   22456              :         }
   22457              : 
   22458         1663 :       for (unsigned len = 0; ids.length ();)
   22459              :         {
   22460          983 :           if (len)
   22461          303 :             flat[len++] = '.';
   22462          983 :           tree elt = ids.pop ();
   22463          983 :           unsigned l = IDENTIFIER_LENGTH (elt);
   22464          983 :           memcpy (flat + len, IDENTIFIER_POINTER (elt), l + 1);
   22465          983 :           len += l;
   22466              :         }
   22467          680 :     }
   22468         5697 :   else if (is_header ())
   22469         1922 :     flatname = TREE_STRING_POINTER (name);
   22470              :   else
   22471         3775 :     flatname = IDENTIFIER_POINTER (name);
   22472         6377 : }
   22473              : 
   22474              : /* Open the GCM file and prepare to read.  Return whether that was
   22475              :    successful.  */
   22476              : 
   22477              : bool
   22478         3074 : module_state::open_slurp (cpp_reader *reader)
   22479              : {
   22480         3074 :   if (slurp)
   22481              :     return true;
   22482              : 
   22483         3028 :   if (lazy_open >= lazy_limit)
   22484            9 :     freeze_an_elf ();
   22485              : 
   22486         3028 :   int fd = -1;
   22487         3028 :   int e = ENOENT;
   22488         3028 :   if (filename)
   22489              :     {
   22490         3028 :       const char *file = maybe_add_cmi_prefix (filename);
   22491         3557 :       dump () && dump ("CMI is %s", file);
   22492         3028 :       if (note_module_cmi_yes || inform_cmi_p)
   22493           12 :         inform (loc, "reading CMI %qs", file);
   22494              :       /* Add the CMI file to the dependency tracking. */
   22495         3028 :       if (cpp_get_deps (reader))
   22496           15 :         deps_add_dep (cpp_get_deps (reader), file);
   22497         3028 :       fd = open (file, O_RDONLY | O_CLOEXEC | O_BINARY);
   22498         3028 :       e = errno;
   22499              :     }
   22500              : 
   22501         3028 :   gcc_checking_assert (!slurp);
   22502         6032 :   slurp = new slurping (new elf_in (fd, e));
   22503              : 
   22504         3028 :   bool ok = from ()->begin (loc);
   22505         3028 :   if (ok)
   22506              :     {
   22507         3004 :       lazy_open++;
   22508         3004 :       slurp->lru = ++lazy_lru;
   22509              :     }
   22510              :   return ok;
   22511              : }
   22512              : 
   22513              : /* Return whether importing this GCM would work without an error in
   22514              :    read_config.  */
   22515              : 
   22516              : bool
   22517           52 : module_state::check_importable (cpp_reader *reader)
   22518              : {
   22519           52 :   if (loadedness > ML_CONFIG)
   22520              :     return true;
   22521           49 :   if (!open_slurp (reader))
   22522              :     return false;
   22523           46 :   module_state_config config;
   22524           46 :   return read_config (config, /*complain*/false);
   22525              : }
   22526              : 
   22527              : /* Read the CMI file for a module.  */
   22528              : 
   22529              : bool
   22530         3025 : module_state::do_import (cpp_reader *reader, bool outermost)
   22531              : {
   22532         3025 :   gcc_assert (global_namespace == current_scope () && loadedness == ML_NONE);
   22533              : 
   22534              :   /* If this TU is a partition of the module we're importing,
   22535              :      that module is the primary module interface.  */
   22536         3025 :   if (this_module ()->is_partition ()
   22537         3076 :       && this == get_primary (this_module ()))
   22538            9 :     module_p = true;
   22539              : 
   22540         3025 :   loc = linemap_module_loc (line_table, loc, get_flatname ());
   22541              : 
   22542         3025 :   bool ok = open_slurp (reader);
   22543         3025 :   if (!from ()->get_error ())
   22544              :     {
   22545         3004 :       announce ("importing");
   22546         3004 :       loadedness = ML_CONFIG;
   22547         3004 :       ok = read_initial (reader);
   22548              :     }
   22549              : 
   22550         3025 :   gcc_assert (slurp->current == ~0u);
   22551              : 
   22552         3025 :   return check_read (outermost, ok);
   22553              : }
   22554              : 
   22555              : /* Attempt to increase the file descriptor limit.  */
   22556              : 
   22557              : static bool
   22558         4935 : try_increase_lazy (unsigned want)
   22559              : {
   22560         4935 :   gcc_checking_assert (lazy_open >= lazy_limit);
   22561              : 
   22562              :   /* If we're increasing, saturate at hard limit.  */
   22563         4935 :   if (want > lazy_hard_limit && lazy_limit < lazy_hard_limit)
   22564         4935 :     want = lazy_hard_limit;
   22565              : 
   22566              : #if HAVE_SETRLIMIT
   22567         4935 :   if ((!lazy_limit || !param_lazy_modules)
   22568         4923 :       && lazy_hard_limit
   22569         4923 :       && want <= lazy_hard_limit)
   22570              :     {
   22571         4923 :       struct rlimit rlimit;
   22572         4923 :       rlimit.rlim_cur = want + LAZY_HEADROOM;
   22573         4923 :       rlimit.rlim_max = lazy_hard_limit + LAZY_HEADROOM;
   22574         4923 :       if (!setrlimit (RLIMIT_NOFILE, &rlimit))
   22575         4923 :         lazy_limit = want;
   22576              :     }
   22577              : #endif
   22578              : 
   22579         4935 :   return lazy_open < lazy_limit;
   22580              : }
   22581              : 
   22582              : /* Pick a victim module to freeze its reader.  */
   22583              : 
   22584              : void
   22585           12 : module_state::freeze_an_elf ()
   22586              : {
   22587           12 :   if (try_increase_lazy (lazy_open * 2))
   22588              :     return;
   22589              : 
   22590           12 :   module_state *victim = NULL;
   22591           12 :   for (unsigned ix = modules->length (); ix--;)
   22592              :     {
   22593           30 :       module_state *candidate = (*modules)[ix];
   22594           30 :       if (candidate && candidate->slurp && candidate->slurp->lru
   22595           60 :           && candidate->from ()->is_freezable ()
   22596           39 :           && (!victim || victim->slurp->lru > candidate->slurp->lru))
   22597              :         victim = candidate;
   22598              :     }
   22599              : 
   22600           12 :   if (victim)
   22601              :     {
   22602           18 :       dump () && dump ("Freezing '%s'", victim->filename);
   22603            9 :       if (victim->slurp->macro_defs.size)
   22604              :         /* Save the macro definitions to a buffer.  */
   22605            0 :         victim->from ()->preserve (victim->slurp->macro_defs);
   22606            9 :       if (victim->slurp->macro_tbl.size)
   22607              :         /* Save the macro definitions to a buffer.  */
   22608            0 :         victim->from ()->preserve (victim->slurp->macro_tbl);
   22609            9 :       victim->from ()->freeze ();
   22610            9 :       lazy_open--;
   22611              :     }
   22612              :   else
   22613            3 :     dump () && dump ("No module available for freezing");
   22614              : }
   22615              : 
   22616              : /* Load the lazy slot *MSLOT, INDEX'th slot of the module.  */
   22617              : 
   22618              : bool
   22619        62010 : module_state::lazy_load (unsigned index, binding_slot *mslot)
   22620              : {
   22621        62010 :   unsigned n = dump.push (this);
   22622              : 
   22623        62010 :   gcc_checking_assert (function_depth);
   22624              : 
   22625        62010 :   unsigned cookie = mslot->get_lazy ();
   22626        62010 :   unsigned snum = cookie >> 2;
   22627        62390 :   dump () && dump ("Loading entity %M[%u] section:%u", this, index, snum);
   22628              : 
   22629        62010 :   bool ok = load_section (snum, mslot);
   22630              : 
   22631        62010 :   dump.pop (n);
   22632              : 
   22633        62010 :   return ok;
   22634              : }
   22635              : 
   22636              : /* Load MOD's binding for NS::ID into *MSLOT.  *MSLOT contains the
   22637              :    lazy cookie.  OUTER is true if this is the outermost lazy, (used
   22638              :    for diagnostics).  */
   22639              : 
   22640              : void
   22641         5890 : lazy_load_binding (unsigned mod, tree ns, tree id, binding_slot *mslot)
   22642              : {
   22643         5890 :   int count = errorcount + warningcount;
   22644              : 
   22645         5890 :   bool timer_running = timevar_cond_start (TV_MODULE_IMPORT);
   22646              : 
   22647              :   /* Make sure lazy loading from a template context behaves as if
   22648              :      from a non-template context.  */
   22649         5890 :   processing_template_decl_sentinel ptds;
   22650              : 
   22651              :   /* Stop GC happening, even in outermost loads (because our caller
   22652              :      could well be building up a lookup set).  */
   22653         5890 :   function_depth++;
   22654              : 
   22655         5890 :   gcc_checking_assert (mod);
   22656         5890 :   module_state *module = (*modules)[mod];
   22657         5890 :   unsigned n = dump.push (module);
   22658              : 
   22659         5890 :   unsigned snum = mslot->get_lazy ();
   22660         6206 :   dump () && dump ("Lazily binding %P@%N section:%u", ns, id,
   22661              :                    module->name, snum);
   22662              : 
   22663         5890 :   bool ok = !recursive_lazy (snum);
   22664         5890 :   if (ok)
   22665              :     {
   22666         5890 :       ok = module->load_section (snum, mslot);
   22667         5890 :       lazy_snum = 0;
   22668         5890 :       post_load_processing ();
   22669              :     }
   22670              : 
   22671         5890 :   dump.pop (n);
   22672              : 
   22673         5890 :   function_depth--;
   22674              : 
   22675         5890 :   timevar_cond_stop (TV_MODULE_IMPORT, timer_running);
   22676              : 
   22677         5890 :   if (!ok)
   22678            0 :     fatal_error (input_location,
   22679            0 :                  module->is_header ()
   22680              :                  ? G_("failed to load binding %<%E%s%E%>")
   22681              :                  : G_("failed to load binding %<%E%s%E@%s%>"),
   22682            0 :                  ns, &"::"[ns == global_namespace ? 2 : 0], id,
   22683              :                  module->get_flatname ());
   22684              : 
   22685         5890 :   if (count != errorcount + warningcount)
   22686           27 :     inform (input_location,
   22687           27 :             module->is_header ()
   22688              :             ? G_("during load of binding %<%E%s%E%>")
   22689              :             : G_("during load of binding %<%E%s%E@%s%>"),
   22690           27 :             ns, &"::"[ns == global_namespace ? 2 : 0], id,
   22691              :             module->get_flatname ());
   22692         5890 : }
   22693              : 
   22694              : /* Load any pending entities keyed to NS and NAME.
   22695              :    Used to find pending types if we don't yet have a decl built.  */
   22696              : 
   22697              : void
   22698     37823479 : lazy_load_pendings (tree ns, tree name)
   22699              : {
   22700              :   /* Make sure lazy loading from a template context behaves as if
   22701              :      from a non-template context.  */
   22702     37823479 :   processing_template_decl_sentinel ptds;
   22703              : 
   22704     37823479 :   pending_key key;
   22705     37823479 :   key.ns = ns;
   22706     37823479 :   key.id = name;
   22707              : 
   22708     37823479 :   auto *pending_vec = pending_table ? pending_table->get (key) : nullptr;
   22709     37818552 :   if (!pending_vec)
   22710     37817312 :     return;
   22711              : 
   22712         6167 :   int count = errorcount + warningcount;
   22713              : 
   22714         6167 :   bool timer_running = timevar_cond_start (TV_MODULE_IMPORT);
   22715         6167 :   bool ok = !recursive_lazy ();
   22716         6167 :   if (ok)
   22717              :     {
   22718         6167 :       function_depth++; /* Prevent GC */
   22719         6167 :       unsigned n = dump.push (NULL);
   22720         6633 :       dump () && dump ("Reading %u pending entities keyed to %P",
   22721              :                        pending_vec->length (), key.ns, key.id);
   22722         6167 :       for (unsigned ix = pending_vec->length (); ix--;)
   22723              :         {
   22724        70276 :           unsigned index = (*pending_vec)[ix];
   22725        70276 :           binding_slot *slot = &(*entity_ary)[index];
   22726              : 
   22727        70276 :           if (slot->is_lazy ())
   22728              :             {
   22729         6723 :               module_state *import = import_entity_module (index);
   22730         6723 :               if (!import->lazy_load (index - import->entity_lwm, slot))
   22731        70276 :                 ok = false;
   22732              :             }
   22733       139996 :           else if (dump ())
   22734              :             {
   22735          338 :               module_state *import = import_entity_module (index);
   22736          338 :               dump () && dump ("Entity %M[%u] already loaded",
   22737          338 :                                import, index - import->entity_lwm);
   22738              :             }
   22739              :         }
   22740              : 
   22741         6167 :       pending_table->remove (key);
   22742         6167 :       dump.pop (n);
   22743         6167 :       lazy_snum = 0;
   22744         6167 :       post_load_processing ();
   22745         6167 :       function_depth--;
   22746              :     }
   22747              : 
   22748         6167 :   timevar_cond_stop (TV_MODULE_IMPORT, timer_running);
   22749              : 
   22750         6167 :   if (!ok)
   22751            0 :     fatal_error (input_location, "failed to load pendings for %<%E%s%E%>",
   22752            0 :                  key.ns, &"::"[key.ns == global_namespace ? 2 : 0], key.id);
   22753              : 
   22754         6167 :   if (count != errorcount + warningcount)
   22755            0 :     inform (input_location, "during load of pendings for %<%E%s%E%>",
   22756            0 :             key.ns, &"::"[key.ns == global_namespace ? 2 : 0], key.id);
   22757     37823479 : }
   22758              : 
   22759              : /* Load any pending entities keyed to the top-key of DECL.  */
   22760              : 
   22761              : void
   22762     37730572 : lazy_load_pendings (tree decl)
   22763              : {
   22764     37730572 :   tree key_decl;
   22765     37730572 :   tree ns = find_pending_key (decl, &key_decl);
   22766     37730572 :   return lazy_load_pendings (ns, DECL_NAME (key_decl));
   22767              : }
   22768              : 
   22769              : static void
   22770         2746 : direct_import (module_state *import, cpp_reader *reader)
   22771              : {
   22772         2746 :   timevar_start (TV_MODULE_IMPORT);
   22773         2746 :   unsigned n = dump.push (import);
   22774              : 
   22775         2746 :   gcc_checking_assert (import->is_direct () && import->has_location ());
   22776         2746 :   if (import->loadedness == ML_NONE)
   22777         1801 :     if (!import->do_import (reader, true))
   22778            0 :       gcc_unreachable ();
   22779              : 
   22780         2709 :   this_module ()->set_import (import, import->exported_p);
   22781              : 
   22782         2709 :   if (import->loadedness < ML_LANGUAGE)
   22783              :     {
   22784         2628 :       if (!keyed_table)
   22785         2307 :         keyed_table = new keyed_map_t (EXPERIMENT (1, 400));
   22786         2628 :       import->read_language (true);
   22787              :     }
   22788              : 
   22789         2709 :   dump.pop (n);
   22790         2709 :   timevar_stop (TV_MODULE_IMPORT);
   22791         2709 : }
   22792              : 
   22793              : /* Import module IMPORT.  */
   22794              : 
   22795              : void
   22796         2513 : import_module (module_state *import, location_t from_loc, bool exporting_p,
   22797              :                tree, cpp_reader *reader)
   22798              : {
   22799              :   /* A non-partition implementation unit has no name.  */
   22800         2513 :   if (!this_module ()->name && this_module ()->parent == import)
   22801              :     {
   22802            3 :       auto_diagnostic_group d;
   22803            3 :       error_at (from_loc, "import of %qs within its own implementation unit",
   22804              :                 import->get_flatname());
   22805            3 :       inform (import->loc, "module declared here");
   22806            3 :       return;
   22807            3 :     }
   22808              : 
   22809         2510 :   if (!import->check_circular_import (from_loc))
   22810              :     return;
   22811              : 
   22812         2504 :   if (!import->is_header () && current_lang_depth ())
   22813              :     /* Only header units should appear inside language
   22814              :        specifications.  The std doesn't specify this, but I think
   22815              :        that's an error in resolving US 033, because language linkage
   22816              :        is also our escape clause to getting things into the global
   22817              :        module, so we don't want to confuse things by having to think
   22818              :        about whether 'extern "C++" { import foo; }' puts foo's
   22819              :        contents into the global module all of a sudden.  */
   22820            6 :     warning (0, "import of named module %qs inside language-linkage block",
   22821              :              import->get_flatname ());
   22822              : 
   22823         2504 :   if (exporting_p || module_exporting_p ())
   22824          325 :     import->exported_p = true;
   22825              : 
   22826         2504 :   if (import->loadedness != ML_NONE)
   22827              :     {
   22828          942 :       from_loc = ordinary_loc_of (line_table, from_loc);
   22829          942 :       linemap_module_reparent (line_table, import->loc, from_loc);
   22830              :     }
   22831              : 
   22832         2504 :   gcc_checking_assert (import->is_direct () && import->has_location ());
   22833              : 
   22834         2504 :   direct_import (import, reader);
   22835              : }
   22836              : 
   22837              : /* Declare the name of the current module to be NAME.  EXPORTING_p is
   22838              :    true if this TU is the exporting module unit.  */
   22839              : 
   22840              : void
   22841         3158 : declare_module (module_state *module, location_t from_loc, bool exporting_p,
   22842              :                 tree, cpp_reader *reader)
   22843              : {
   22844         3158 :   gcc_assert (global_namespace == current_scope ());
   22845              : 
   22846         3158 :   module_state *current = this_module ();
   22847         3158 :   if (module_purview_p () || module->loadedness > ML_CONFIG)
   22848              :     {
   22849            6 :       auto_diagnostic_group d;
   22850           12 :       error_at (from_loc, module_purview_p ()
   22851              :                 ? G_("module already declared")
   22852              :                 : G_("module already imported"));
   22853            6 :       if (module_purview_p ())
   22854            0 :         module = current;
   22855           12 :       inform (module->loc, module_purview_p ()
   22856              :               ? G_("module %qs declared here")
   22857              :               : G_("module %qs imported here"),
   22858              :               module->get_flatname ());
   22859            6 :       return;
   22860            6 :     }
   22861              : 
   22862         3152 :   gcc_checking_assert (module->is_module ());
   22863         3152 :   gcc_checking_assert (module->is_direct () && module->has_location ());
   22864              : 
   22865              :   /* Yer a module, 'arry.  */
   22866         3152 :   module_kind = module->is_header () ? MK_HEADER : MK_NAMED | MK_ATTACH;
   22867              : 
   22868              :   // Even in header units, we consider the decls to be purview
   22869         3152 :   module_kind |= MK_PURVIEW;
   22870              : 
   22871         3152 :   if (module->is_partition ())
   22872          208 :     module_kind |= MK_PARTITION;
   22873         3152 :   if (exporting_p)
   22874              :     {
   22875         2842 :       module->interface_p = true;
   22876         2842 :       module_kind |= MK_INTERFACE;
   22877              :     }
   22878              : 
   22879         3152 :   if (module_has_cmi_p ())
   22880              :     {
   22881              :       /* Copy the importing information we may have already done.  We
   22882              :          do not need to separate out the imports that only happen in
   22883              :          the GMF, in spite of what the literal wording of the std
   22884              :          might imply.  See p2191, the core list had a discussion
   22885              :          where the module implementors agreed that the GMF of a named
   22886              :          module is invisible to importers.  */
   22887         2910 :       module->imports = current->imports;
   22888              : 
   22889         2910 :       module->mod = 0;
   22890         2910 :       (*modules)[0] = module;
   22891              :     }
   22892              :   else
   22893              :     {
   22894          242 :       module->interface_p = true;
   22895          242 :       current->parent = module; /* So mangler knows module identity. */
   22896          242 :       direct_import (module, reader);
   22897              :     }
   22898              : }
   22899              : 
   22900              : /* Return true IFF we must emit a module global initializer function
   22901              :    (which will be called by importers' init code).  */
   22902              : 
   22903              : bool
   22904       105510 : module_global_init_needed ()
   22905              : {
   22906       105510 :   return module_has_cmi_p () && !header_module_p ();
   22907              : }
   22908              : 
   22909              : /* Calculate which, if any, import initializers need calling.  */
   22910              : 
   22911              : bool
   22912        98071 : module_determine_import_inits ()
   22913              : {
   22914        98071 :   if (!modules || header_module_p ())
   22915              :     return false;
   22916              : 
   22917              :   /* Prune active_init_p.  We need the same bitmap allocation
   22918              :      scheme as for the imports member.  */
   22919         3830 :   function_depth++; /* Disable GC.  */
   22920         3830 :   bitmap covered_imports (BITMAP_GGC_ALLOC ());
   22921              : 
   22922         3830 :   bool any = false;
   22923              : 
   22924              :   /* Because indirect imports are before their direct import, and
   22925              :      we're scanning the array backwards, we only need one pass!  */
   22926         6671 :   for (unsigned ix = modules->length (); --ix;)
   22927              :     {
   22928         2841 :       module_state *import = (*modules)[ix];
   22929              : 
   22930         2841 :       if (!import->active_init_p)
   22931              :         ;
   22932           52 :       else if (bitmap_bit_p (covered_imports, ix))
   22933            6 :         import->active_init_p = false;
   22934              :       else
   22935              :         {
   22936              :           /* Everything this imports is therefore handled by its
   22937              :              initializer, so doesn't need initializing by us.  */
   22938           46 :           bitmap_ior_into (covered_imports, import->imports);
   22939           46 :           any = true;
   22940              :         }
   22941              :     }
   22942         3830 :   function_depth--;
   22943              : 
   22944         3830 :   return any;
   22945              : }
   22946              : 
   22947              : /* Emit calls to each direct import's global initializer.  Including
   22948              :    direct imports of directly imported header units.  The initializers
   22949              :    of (static) entities in header units will be called by their
   22950              :    importing modules (for the instance contained within that), or by
   22951              :    the current TU (for the instances we've brought in).  Of course
   22952              :    such header unit behaviour is evil, but iostream went through that
   22953              :    door some time ago.  */
   22954              : 
   22955              : void
   22956           46 : module_add_import_initializers ()
   22957              : {
   22958           46 :   if (!modules || header_module_p ())
   22959            0 :     return;
   22960              : 
   22961           46 :   tree fntype = build_function_type (void_type_node, void_list_node);
   22962           46 :   releasing_vec args;  // There are no args
   22963              : 
   22964          104 :   for (unsigned ix = modules->length (); --ix;)
   22965              :     {
   22966           58 :       module_state *import = (*modules)[ix];
   22967           58 :       if (import->active_init_p)
   22968              :         {
   22969           46 :           tree name = mangle_module_global_init (ix);
   22970           46 :           tree fndecl = build_lang_decl (FUNCTION_DECL, name, fntype);
   22971              : 
   22972           46 :           DECL_CONTEXT (fndecl) = FROB_CONTEXT (global_namespace);
   22973           46 :           SET_DECL_ASSEMBLER_NAME (fndecl, name);
   22974           46 :           TREE_PUBLIC (fndecl) = true;
   22975           46 :           determine_visibility (fndecl);
   22976              : 
   22977           46 :           tree call = cp_build_function_call_vec (fndecl, &args,
   22978              :                                                   tf_warning_or_error);
   22979           46 :           finish_expr_stmt (call);
   22980              :         }
   22981              :     }
   22982           46 : }
   22983              : 
   22984              : /* NAME & LEN are a preprocessed header name, possibly including the
   22985              :    surrounding "" or <> characters.  Return the raw string name of the
   22986              :    module to which it refers.  This will be an absolute path, or begin
   22987              :    with ./, so it is immediately distinguishable from a (non-header
   22988              :    unit) module name.  If READER is non-null, ask the preprocessor to
   22989              :    locate the header to which it refers using the appropriate include
   22990              :    path.  Note that we do never do \ processing of the string, as that
   22991              :    matches the preprocessor's behaviour.  */
   22992              : 
   22993              : static const char *
   22994        25753 : canonicalize_header_name (cpp_reader *reader, location_t loc, bool unquoted,
   22995              :                           const char *str, size_t &len_r)
   22996              : {
   22997        25753 :   size_t len = len_r;
   22998        25753 :   static char *buf = 0;
   22999        25753 :   static size_t alloc = 0;
   23000              : 
   23001        25753 :   if (!unquoted)
   23002              :     {
   23003            4 :       gcc_checking_assert (len >= 2
   23004              :                            && ((reader && str[0] == '<' && str[len-1] == '>')
   23005              :                                || (str[0] == '"' && str[len-1] == '"')));
   23006            4 :       str += 1;
   23007            4 :       len -= 2;
   23008              :     }
   23009              : 
   23010        25753 :   if (reader)
   23011              :     {
   23012            4 :       gcc_assert (!unquoted);
   23013              : 
   23014            4 :       if (len >= alloc)
   23015              :         {
   23016            4 :           alloc = len + 1;
   23017            4 :           buf = XRESIZEVEC (char, buf, alloc);
   23018              :         }
   23019            4 :       memcpy (buf, str, len);
   23020            4 :       buf[len] = 0;
   23021              : 
   23022            8 :       if (const char *hdr
   23023            4 :           = cpp_probe_header_unit (reader, buf, str[-1] == '<', loc))
   23024              :         {
   23025            4 :           len = strlen (hdr);
   23026            4 :           str = hdr;
   23027              :         }
   23028              :       else
   23029            0 :         str = buf;
   23030              :     }
   23031              : 
   23032        25753 :   if (!(str[0] == '.' ? IS_DIR_SEPARATOR (str[1]) : IS_ABSOLUTE_PATH (str)))
   23033              :     {
   23034              :       /* Prepend './'  */
   23035            9 :       if (len + 3 > alloc)
   23036              :         {
   23037            9 :           alloc = len + 3;
   23038            9 :           buf = XRESIZEVEC (char, buf, alloc);
   23039              :         }
   23040              : 
   23041            9 :       buf[0] = '.';
   23042            9 :       buf[1] = DIR_SEPARATOR;
   23043            9 :       memmove (buf + 2, str, len);
   23044            9 :       len += 2;
   23045            9 :       buf[len] = 0;
   23046            9 :       str = buf;
   23047              :     }
   23048              : 
   23049        25753 :   len_r = len;
   23050        25753 :   return str;
   23051              : }
   23052              : 
   23053              : /* Set the CMI name from a cody packet.  Issue an error if
   23054              :    ill-formed.  */
   23055              : 
   23056         5789 : void module_state::set_filename (const Cody::Packet &packet)
   23057              : {
   23058         5789 :   if (packet.GetCode () == Cody::Client::PC_PATHNAME)
   23059              :     {
   23060              :       /* If we've seen this import before we better have the same CMI.  */
   23061         5786 :       const std::string &path = packet.GetString ();
   23062         5786 :       if (!filename)
   23063         5783 :         filename = xstrdup (packet.GetString ().c_str ());
   23064            3 :       else if (filename != path)
   23065            0 :         error_at (loc, "mismatching compiled module interface: "
   23066              :                   "had %qs, got %qs", filename, path.c_str ());
   23067              :     }
   23068              :   else
   23069              :     {
   23070            3 :       gcc_checking_assert (packet.GetCode () == Cody::Client::PC_ERROR);
   23071            3 :       fatal_error (loc, "unknown compiled module interface: %s",
   23072            3 :                    packet.GetString ().c_str ());
   23073              :     }
   23074         5786 : }
   23075              : 
   23076              : /* The list of importable headers from C++ Table 24.  */
   23077              : 
   23078              : static const char *
   23079              : importable_headers[] =
   23080              :   {
   23081              :     "algorithm", "any", "array", "atomic",
   23082              :     "barrier", "bit", "bitset",
   23083              :     "charconv", "chrono", "compare", "complex", "concepts",
   23084              :     "condition_variable", "contracts", "coroutine",
   23085              :     "debugging", "deque",
   23086              :     "exception", "execution", "expected",
   23087              :     "filesystem", "flat_map", "flat_set", "format", "forward_list",
   23088              :     "fstream", "functional", "future",
   23089              :     "generator",
   23090              :     "hazard_pointer", "hive",
   23091              :     "initializer_list", "inplace_vector", "iomanip", "ios", "iosfwd",
   23092              :     "iostream", "istream", "iterator",
   23093              :     "latch", "limits", "linalg", "list", "locale",
   23094              :     "map", "mdspan", "memory", "memory_resource", "meta", "mutex",
   23095              :     "new", "numbers", "numeric",
   23096              :     "optional", "ostream",
   23097              :     "print",
   23098              :     "queue",
   23099              :     "random", "ranges", "ratio", "rcu", "regex",
   23100              :     "scoped_allocator", "semaphore", "set", "shared_mutex", "simd",
   23101              :     "source_location", "span", "spanstream", "sstream", "stack", "stacktrace",
   23102              :     "stdexcept", "stdfloat", "stop_token", "streambuf", "string",
   23103              :     "string_view", "syncstream", "system_error",
   23104              :     "text_encoding", "thread", "tuple", "type_traits", "typeindex", "typeinfo",
   23105              :     "unordered_map", "unordered_set",
   23106              :     "utility",
   23107              :     "valarray", "variant", "vector", "version"
   23108              :   };
   23109              : 
   23110              : /* True iff <name> is listed as an importable standard header.  */
   23111              : 
   23112              : static bool
   23113        23487 : is_importable_header (const char *name)
   23114              : {
   23115        23487 :   unsigned lo = 0;
   23116        23487 :   unsigned hi = ARRAY_SIZE (importable_headers);
   23117       178094 :   while (hi > lo)
   23118              :     {
   23119       157399 :       unsigned mid = (lo + hi)/2;
   23120       157399 :       int cmp = strcmp (name, importable_headers[mid]);
   23121       157399 :       if (cmp > 0)
   23122        36389 :         lo = mid + 1;
   23123       121010 :       else if (cmp < 0)
   23124              :         hi = mid;
   23125              :       else
   23126              :         return true;
   23127              :     }
   23128              :   return false;
   23129              : }
   23130              : 
   23131              : /* Figure out whether to treat HEADER as an include or an import.  */
   23132              : 
   23133              : static char *
   23134        24822 : maybe_translate_include (cpp_reader *reader, line_maps *lmaps, location_t loc,
   23135              :                          _cpp_file *file, bool angle, const char **alternate)
   23136              : {
   23137        24822 :   if (!modules_p ())
   23138              :     {
   23139              :       /* Turn off.  */
   23140            0 :       cpp_get_callbacks (reader)->translate_include = NULL;
   23141            0 :       return nullptr;
   23142              :     }
   23143              : 
   23144        24822 :   const char *path = _cpp_get_file_path (file);
   23145              : 
   23146        24822 :   dump.push (NULL);
   23147              : 
   23148        26341 :   dump () && dump ("Checking include translation '%s'", path);
   23149        24822 :   auto *mapper = get_mapper (cpp_main_loc (reader), cpp_get_deps (reader));
   23150              : 
   23151        24822 :   size_t len = strlen (path);
   23152        24822 :   path = canonicalize_header_name (NULL, loc, true, path, len);
   23153        24822 :   auto packet = mapper->IncludeTranslate (path, Cody::Flags::None, len);
   23154              : 
   23155        24822 :   enum class xlate_kind {
   23156              :     unknown, text, import, invalid
   23157        24822 :   } translate = xlate_kind::unknown;
   23158              : 
   23159        24822 :   if (packet.GetCode () == Cody::Client::PC_BOOL)
   23160        24773 :     translate = packet.GetInteger () ? xlate_kind::text : xlate_kind::unknown;
   23161           49 :   else if (packet.GetCode () == Cody::Client::PC_PATHNAME)
   23162              :     {
   23163              :       /* Record the CMI name for when we do the import.
   23164              :          We may already know about this import, but libcpp doesn't yet.  */
   23165           49 :       module_state *import = get_module (build_string (len, path));
   23166           49 :       import->set_filename (packet);
   23167           49 :       if (import->check_importable (reader))
   23168              :         translate = xlate_kind::import;
   23169              :       else
   23170            0 :         translate = xlate_kind::invalid;
   23171              :     }
   23172              :   else
   23173              :     {
   23174            0 :       gcc_checking_assert (packet.GetCode () == Cody::Client::PC_ERROR);
   23175            0 :       error_at (loc, "cannot determine %<#include%> translation of %s: %s",
   23176            0 :                 path, packet.GetString ().c_str ());
   23177              :     }
   23178              : 
   23179        24822 :   bool note = (translate == xlate_kind::invalid);
   23180        24822 :   if (note_include_translate_yes && translate == xlate_kind::import)
   23181              :     note = true;
   23182        24817 :   else if (note_include_translate_no && translate == xlate_kind::unknown)
   23183              :     note = true;
   23184        24814 :   else if (note_includes)
   23185              :     /* We do not expect the note_includes vector to be large, so O(N)
   23186              :        iteration.  */
   23187          432 :     for (unsigned ix = note_includes->length (); !note && ix--;)
   23188          216 :       if (!strcmp ((*note_includes)[ix], path))
   23189            1 :         note = true;
   23190              : 
   23191              :   /* Maybe try importing a different header instead.  */
   23192        24822 :   if (alternate && translate == xlate_kind::unknown)
   23193              :     {
   23194        24375 :       const char *fname = _cpp_get_file_name (file);
   23195              :       /* Redirect importable <name> to <bits/stdc++.h>.  */
   23196              :       /* ??? Generalize to use a .json.  */
   23197        24375 :       expanded_location eloc = expand_location (loc);
   23198        27153 :       auto indir = [](const char *f, const char *d)
   23199              :       {
   23200         2778 :         if (!filename_ncmp (f, d, strlen (d))) return true;
   23201              :         /* Also check canonical paths (c++/123879).  */
   23202          929 :         auto cf = lrealpath (f); auto cd = lrealpath (d);
   23203          929 :         bool r = cf && cd && !filename_ncmp (cf, cd, strlen (cd));
   23204          929 :         free (cf); free (cd);
   23205          929 :         return r;
   23206              :       };
   23207        23487 :       if (angle && is_importable_header (fname)
   23208              :           /* Exclude <version> which often goes with import std.  */
   23209         2792 :           && strcmp (fname, "version") != 0
   23210              :           /* Don't redirect #includes between headers under the same include
   23211              :              path directory (i.e. between library headers); if the import
   23212              :              brings in the current file we then get redefinition errors.  */
   23213         2778 :           && !indir (eloc.file, _cpp_get_file_dir (file)->name)
   23214              :           /* ??? These are needed when running a toolchain from the build
   23215              :              directory, because libsupc++ headers aren't linked into
   23216              :              libstdc++-v3/include with the other headers.  */
   23217          851 :           && !strstr (eloc.file, "libstdc++-v3/include")
   23218        24859 :           && !strstr (eloc.file, "libsupc++"))
   23219          401 :         *alternate = "bits/stdc++.h";
   23220              :     }
   23221              : 
   23222        24822 :   if (note)
   23223           12 :     inform (loc, translate == xlate_kind::import
   23224              :             ? G_("include %qs translated to import")
   23225              :             : translate == xlate_kind::invalid
   23226            3 :             ? G_("import of %qs failed, falling back to include")
   23227              :             : G_("include %qs processed textually"), path);
   23228              : 
   23229        27857 :   dump () && dump (translate == xlate_kind::import
   23230              :                    ? "Translating include to import"
   23231              :                    : "Keeping include as include");
   23232        24822 :   dump.pop (0);
   23233              : 
   23234        24822 :   if (translate != xlate_kind::import)
   23235              :     return nullptr;
   23236              : 
   23237              :   /* Create the translation text.  */
   23238           49 :   loc = ordinary_loc_of (lmaps, loc);
   23239           49 :   const line_map_ordinary *map
   23240           49 :     = linemap_check_ordinary (linemap_lookup (lmaps, loc));
   23241           49 :   unsigned col = SOURCE_COLUMN (map, loc);
   23242           49 :   col -= (col != 0); /* Columns are 1-based.  */
   23243              : 
   23244           49 :   unsigned alloc = len + col + 60;
   23245           49 :   char *res = XNEWVEC (char, alloc);
   23246              : 
   23247           49 :   strcpy (res, "__import");
   23248           49 :   unsigned actual = 8;
   23249           49 :   if (col > actual)
   23250              :     {
   23251              :       /* Pad out so the filename appears at the same position.  */
   23252           46 :       memset (res + actual, ' ', col - actual);
   23253           46 :       actual = col;
   23254              :     }
   23255              :   /* No need to encode characters, that's not how header names are
   23256              :      handled.  */
   23257           49 :   actual += snprintf (res + actual, alloc - actual,
   23258              :                       "\"%s\" [[__translated]];\n", path);
   23259           49 :   gcc_checking_assert (actual < alloc);
   23260              : 
   23261              :   /* cpplib will delete the buffer.  */
   23262              :   return res;
   23263        24822 : }
   23264              : 
   23265              : static void
   23266          927 : begin_header_unit (cpp_reader *reader)
   23267              : {
   23268              :   /* Set the module header name from the main_input_filename.  */
   23269          927 :   const char *main = main_input_filename;
   23270          927 :   size_t len = strlen (main);
   23271          927 :   main = canonicalize_header_name (NULL, 0, true, main, len);
   23272          927 :   module_state *module = get_module (build_string (len, main));
   23273              : 
   23274          927 :   preprocess_module (module, cpp_main_loc (reader), false, false, true, reader);
   23275          927 : }
   23276              : 
   23277              : /* We've just properly entered the main source file.  I.e. after the
   23278              :    command line, builtins and forced headers.  Record the line map and
   23279              :    location of this map.  Note we may be called more than once.  The
   23280              :    first call sticks.  */
   23281              : 
   23282              : void
   23283        99967 : module_begin_main_file (cpp_reader *reader, line_maps *lmaps,
   23284              :                        const line_map_ordinary *map)
   23285              : {
   23286        99967 :   gcc_checking_assert (lmaps == line_table);
   23287        99967 :   if (modules_p () && !spans.init_p ())
   23288              :     {
   23289         4923 :       unsigned n = dump.push (NULL);
   23290         4923 :       spans.init (lmaps, map);
   23291         4923 :       dump.pop (n);
   23292         4923 :       if (flag_header_unit && !cpp_get_options (reader)->preprocessed)
   23293              :         {
   23294              :           /* Tell the preprocessor this is an include file.  */
   23295          918 :           cpp_retrofit_as_include (reader);
   23296          918 :           begin_header_unit (reader);
   23297              :         }
   23298              :     }
   23299        99967 : }
   23300              : 
   23301              : /* Process the pending_import queue, making sure we know the
   23302              :    filenames.   */
   23303              : 
   23304              : static void
   23305         5834 : name_pending_imports (cpp_reader *reader)
   23306              : {
   23307         5834 :   auto *mapper = get_mapper (cpp_main_loc (reader), cpp_get_deps (reader));
   23308              : 
   23309         5834 :   if (!vec_safe_length (pending_imports))
   23310              :     /* Not doing anything.  */
   23311              :     return;
   23312              : 
   23313         4987 :   timevar_start (TV_MODULE_MAPPER);
   23314              : 
   23315         4987 :   auto n = dump.push (NULL);
   23316         5606 :   dump () && dump ("Resolving direct import names");
   23317         4987 :   bool want_deps = (bool (mapper->get_flags () & Cody::Flags::NameOnly)
   23318         4987 :                     || cpp_get_deps (reader));
   23319         4987 :   bool any = false;
   23320              : 
   23321        10911 :   for (unsigned ix = 0; ix != pending_imports->length (); ix++)
   23322              :     {
   23323         5924 :       module_state *module = (*pending_imports)[ix];
   23324         5924 :       gcc_checking_assert (module->is_direct ());
   23325         5924 :       if (!module->filename && !module->visited_p)
   23326              :         {
   23327         5812 :           bool export_p = (module->is_module ()
   23328         5812 :                            && (module->is_partition ()
   23329         3022 :                                || module->is_exported ()));
   23330              : 
   23331         5812 :           Cody::Flags flags = Cody::Flags::None;
   23332         5812 :           if (flag_preprocess_only
   23333         5812 :               && !(module->is_header () && !export_p))
   23334              :             {
   23335          141 :               if (!want_deps)
   23336           72 :                 continue;
   23337              :               flags = Cody::Flags::NameOnly;
   23338              :             }
   23339              : 
   23340         5740 :           if (!any)
   23341              :             {
   23342         4913 :               any = true;
   23343         4913 :               mapper->Cork ();
   23344              :             }
   23345         5740 :           if (export_p)
   23346         2964 :             mapper->ModuleExport (module->get_flatname (), flags);
   23347              :           else
   23348         2776 :             mapper->ModuleImport (module->get_flatname (), flags);
   23349         5740 :           module->visited_p = true;
   23350              :         }
   23351              :     }
   23352              : 
   23353         4987 :   if (any)
   23354              :     {
   23355         4913 :       auto response = mapper->Uncork ();
   23356         4913 :       auto r_iter = response.begin ();
   23357        10745 :       for (unsigned ix = 0; ix != pending_imports->length (); ix++)
   23358              :         {
   23359         5835 :           module_state *module = (*pending_imports)[ix];
   23360         5835 :           if (module->visited_p)
   23361              :             {
   23362         5740 :               module->visited_p = false;
   23363         5740 :               gcc_checking_assert (!module->filename);
   23364              : 
   23365         5740 :               module->set_filename (*r_iter);
   23366         5737 :               ++r_iter;
   23367              :             }
   23368              :         }
   23369         4910 :     }
   23370              : 
   23371         4984 :   dump.pop (n);
   23372              : 
   23373         4984 :   timevar_stop (TV_MODULE_MAPPER);
   23374              : }
   23375              : 
   23376              : /* We've just lexed a module-specific control line for MODULE.  Mark
   23377              :    the module as a direct import, and possibly load up its macro
   23378              :    state.  Returns the primary module, if this is a module
   23379              :    declaration.  */
   23380              : /* Perhaps we should offer a preprocessing mode where we read the
   23381              :    directives from the header unit, rather than require the header's
   23382              :    CMI.  */
   23383              : 
   23384              : module_state *
   23385         5944 : preprocess_module (module_state *module, location_t from_loc,
   23386              :                    bool in_purview, bool is_import, bool is_export,
   23387              :                    cpp_reader *reader)
   23388              : {
   23389         5944 :   if (!is_import)
   23390              :     {
   23391         3314 :       if (in_purview || module->loc)
   23392              :         {
   23393              :           /* We've already seen a module declaration.  If only preprocessing
   23394              :              then we won't complain in declare_module, so complain here.  */
   23395           42 :           if (flag_preprocess_only)
   23396            6 :             error_at (from_loc,
   23397              :                       in_purview
   23398              :                       ? G_("module already declared")
   23399              :                       : G_("module already imported"));
   23400              :           /* Always pretend this was an import to aid error recovery.  */
   23401              :           is_import = true;
   23402              :         }
   23403              :       else
   23404              :         {
   23405              :           /* Record it is the module.  */
   23406         3272 :           module->module_p = true;
   23407         3272 :           if (is_export)
   23408              :             {
   23409         2944 :               module->exported_p = true;
   23410         2944 :               module->interface_p = true;
   23411              :             }
   23412              :         }
   23413              :     }
   23414              : 
   23415         5944 :   if (module->directness < MD_DIRECT + in_purview)
   23416              :     {
   23417              :       /* Mark as a direct import.  */
   23418         5897 :       module->directness = module_directness (MD_DIRECT + in_purview);
   23419              : 
   23420              :       /* Set the location to be most informative for users.  */
   23421         5897 :       from_loc = ordinary_loc_of (line_table, from_loc);
   23422         5897 :       if (module->loadedness != ML_NONE)
   23423            6 :         linemap_module_reparent (line_table, module->loc, from_loc);
   23424              :       else
   23425              :         {
   23426              :           /* Don't overwrite the location if we're importing ourselves
   23427              :              after already having seen a module-declaration.  */
   23428         5891 :           if (!(is_import && module->is_module ()))
   23429         5861 :             module->loc = from_loc;
   23430         5891 :           if (!module->flatname)
   23431         5852 :             module->set_flatname ();
   23432              :         }
   23433              :     }
   23434              : 
   23435         5944 :   auto desired = ML_CONFIG;
   23436         5944 :   if (is_import
   23437         2672 :       && module->is_header ()
   23438         6869 :       && (!cpp_get_options (reader)->preprocessed
   23439            3 :           || cpp_get_options (reader)->directives_only))
   23440              :     /* We need preprocessor state now.  */
   23441              :     desired = ML_PREPROCESSOR;
   23442              : 
   23443         5944 :   if (!is_import || module->loadedness < desired)
   23444              :     {
   23445         5924 :       vec_safe_push (pending_imports, module);
   23446              : 
   23447         5924 :       if (desired == ML_PREPROCESSOR)
   23448              :         {
   23449          905 :           unsigned n = dump.push (NULL);
   23450              : 
   23451         1104 :           dump () && dump ("Reading %M preprocessor state", module);
   23452          905 :           name_pending_imports (reader);
   23453              : 
   23454              :           /* Preserve the state of the line-map.  */
   23455          905 :           auto pre_hwm = LINEMAPS_ORDINARY_USED (line_table);
   23456              : 
   23457              :           /* We only need to close the span, if we're going to emit a
   23458              :              CMI.  But that's a little tricky -- our token scanner
   23459              :              needs to be smarter -- and this isn't much state.
   23460              :              Remember, we've not parsed anything at this point, so
   23461              :              our module state flags are inadequate.  */
   23462          905 :           spans.maybe_init ();
   23463          905 :           spans.close ();
   23464              : 
   23465          905 :           timevar_start (TV_MODULE_IMPORT);
   23466              : 
   23467              :           /* Load the config of each pending import -- we must assign
   23468              :              module numbers monotonically.  */
   23469         1999 :           for (unsigned ix = 0; ix != pending_imports->length (); ix++)
   23470              :             {
   23471         1094 :               auto *import = (*pending_imports)[ix];
   23472         1272 :               if (!(import->is_module ()
   23473          178 :                     && (import->is_partition () || import->is_exported ()))
   23474          922 :                   && import->loadedness == ML_NONE
   23475         2015 :                   && (!flag_preprocess_only
   23476           51 :                       || (import->is_header ()
   23477              :                           /* Allow a missing/unimportable GCM with -MG.
   23478              :                              FIXME We should also try falling back to #include
   23479              :                              before giving up entirely.  */
   23480           42 :                           && (!cpp_get_options (reader)->deps.missing_files
   23481            3 :                               || import->check_importable (reader)))))
   23482              :                 {
   23483          909 :                   unsigned n = dump.push (import);
   23484          909 :                   import->do_import (reader, true);
   23485          909 :                   dump.pop (n);
   23486              :                 }
   23487              :             }
   23488          905 :           vec_free (pending_imports);
   23489              : 
   23490              :           /* Restore the line-map state.  */
   23491          905 :           spans.open (linemap_module_restore (line_table, pre_hwm));
   23492              : 
   23493              :           /* Now read the preprocessor state of this particular
   23494              :              import.  */
   23495          905 :           if (module->loadedness == ML_CONFIG
   23496          905 :               && module->read_preprocessor (true))
   23497          899 :             module->import_macros ();
   23498              : 
   23499          905 :           timevar_stop (TV_MODULE_IMPORT);
   23500              : 
   23501          905 :           dump.pop (n);
   23502              :         }
   23503              :     }
   23504              : 
   23505         5944 :   return is_import ? NULL : get_primary (module);
   23506              : }
   23507              : 
   23508              : /* We've completed phase-4 translation.  Emit any dependency
   23509              :    information for the not-yet-loaded direct imports, and fill in
   23510              :    their file names.  We'll have already loaded up the direct header
   23511              :    unit wavefront.  */
   23512              : 
   23513              : void
   23514         4929 : preprocessed_module (cpp_reader *reader)
   23515              : {
   23516         4929 :   unsigned n = dump.push (NULL);
   23517              : 
   23518         5512 :   dump () && dump ("Completed phase-4 (tokenization) processing");
   23519              : 
   23520         4929 :   name_pending_imports (reader);
   23521         4926 :   vec_free (pending_imports);
   23522              : 
   23523         4926 :   spans.maybe_init ();
   23524         4926 :   spans.close ();
   23525              : 
   23526         4926 :   using iterator = hash_table<module_state_hash>::iterator;
   23527         4926 :   if (mkdeps *deps = cpp_get_deps (reader))
   23528              :     {
   23529              :       /* Walk the module hash, informing the dependency machinery.  */
   23530           57 :       iterator end = modules_hash->end ();
   23531          342 :       for (iterator iter = modules_hash->begin (); iter != end; ++iter)
   23532              :         {
   23533          114 :           module_state *module = *iter;
   23534              : 
   23535          114 :           if (module->is_direct ())
   23536              :             {
   23537           90 :               if (module->is_module ()
   23538           90 :                   && (module->is_interface () || module->is_partition ()))
   23539           36 :                 deps_add_module_target (deps, module->get_flatname (),
   23540           36 :                                         maybe_add_cmi_prefix (module->filename),
   23541           36 :                                         module->is_header (),
   23542           36 :                                         module->is_exported ());
   23543              :               else
   23544           54 :                 deps_add_module_dep (deps, module->get_flatname ());
   23545              :             }
   23546              :         }
   23547              :     }
   23548              : 
   23549         4926 :   if (flag_header_unit && !flag_preprocess_only)
   23550              :     {
   23551              :       /* Find the main module -- remember, it's not yet in the module
   23552              :          array.  */
   23553          915 :       iterator end = modules_hash->end ();
   23554         1938 :       for (iterator iter = modules_hash->begin (); iter != end; ++iter)
   23555              :         {
   23556          969 :           module_state *module = *iter;
   23557          969 :           if (module->is_module ())
   23558              :             {
   23559          915 :               declare_module (module, cpp_main_loc (reader), true, NULL, reader);
   23560          915 :               module_kind |= MK_EXPORTING;
   23561          915 :               break;
   23562              :             }
   23563              :         }
   23564              :     }
   23565              : 
   23566         4926 :   dump.pop (n);
   23567         4926 : }
   23568              : 
   23569              : /* VAL is a global tree, add it to the global vec if it is
   23570              :    interesting.  Add some of its targets, if they too are
   23571              :    interesting.  We do not add identifiers, as they can be re-found
   23572              :    via the identifier hash table.  There is a cost to the number of
   23573              :    global trees.  */
   23574              : 
   23575              : static int
   23576      3054433 : maybe_add_global (tree val, unsigned &crc)
   23577              : {
   23578      3054433 :   int v = 0;
   23579              : 
   23580      3054433 :   if (val && !(identifier_p (val) || TREE_VISITED (val)))
   23581              :     {
   23582       939894 :       TREE_VISITED (val) = true;
   23583       939894 :       crc = crc32_unsigned (crc, fixed_trees->length ());
   23584       939894 :       vec_safe_push (fixed_trees, val);
   23585       939894 :       v++;
   23586              : 
   23587       939894 :       if (CODE_CONTAINS_STRUCT (TREE_CODE (val), TS_TYPED))
   23588       939894 :         v += maybe_add_global (TREE_TYPE (val), crc);
   23589       939894 :       if (CODE_CONTAINS_STRUCT (TREE_CODE (val), TS_TYPE_COMMON))
   23590       616123 :         v += maybe_add_global (TYPE_NAME (val), crc);
   23591              :     }
   23592              : 
   23593      3054433 :   return v;
   23594              : }
   23595              : 
   23596              : /* Initialize module state.  Create the hash table, determine the
   23597              :    global trees.  Create the module for current TU.  */
   23598              : 
   23599              : void
   23600         4929 : init_modules (cpp_reader *reader)
   23601              : {
   23602              :   /* PCH should not be reachable because of lang-specs, but the
   23603              :      user could have overridden that.  */
   23604         4929 :   if (pch_file)
   23605            0 :     fatal_error (input_location,
   23606              :                  "C++ modules are incompatible with precompiled headers");
   23607              : 
   23608         4929 :   if (cpp_get_options (reader)->traditional)
   23609            0 :     fatal_error (input_location,
   23610              :                  "C++ modules are incompatible with traditional preprocessing");
   23611              : 
   23612              :   /* :: is always exported.  */
   23613         4929 :   DECL_MODULE_EXPORT_P (global_namespace) = true;
   23614              : 
   23615         4929 :   modules_hash = hash_table<module_state_hash>::create_ggc (31);
   23616         4929 :   vec_safe_reserve (modules, 20);
   23617              : 
   23618              :   /* Create module for current TU.  */
   23619         4929 :   module_state *current
   23620         4929 :     = new (ggc_alloc<module_state> ()) module_state (NULL_TREE, NULL, false);
   23621         4929 :   current->mod = 0;
   23622         4929 :   bitmap_set_bit (current->imports, 0);
   23623         4929 :   modules->quick_push (current);
   23624              : 
   23625         4929 :   gcc_checking_assert (!fixed_trees);
   23626              : 
   23627         4929 :   headers = BITMAP_GGC_ALLOC ();
   23628              : 
   23629         4929 :   if (note_includes)
   23630              :     /* Canonicalize header names.  */
   23631            2 :     for (unsigned ix = 0; ix != note_includes->length (); ix++)
   23632              :       {
   23633            1 :         const char *hdr = (*note_includes)[ix];
   23634            1 :         size_t len = strlen (hdr);
   23635              : 
   23636            1 :         bool system = hdr[0] == '<';
   23637            1 :         bool user = hdr[0] == '"';
   23638            1 :         bool delimed = system || user;
   23639              : 
   23640            1 :         if (len <= (delimed ? 2 : 0)
   23641            1 :             || (delimed && hdr[len-1] != (system ? '>' : '"')))
   23642            0 :           error ("invalid header name %qs", hdr);
   23643              : 
   23644            1 :         hdr = canonicalize_header_name (delimed ? reader : NULL,
   23645              :                                         0, !delimed, hdr, len);
   23646            1 :         char *path = XNEWVEC (char, len + 1);
   23647            1 :         memcpy (path, hdr, len);
   23648            1 :         path[len] = 0;
   23649              : 
   23650            1 :         (*note_includes)[ix] = path;
   23651              :       }
   23652              : 
   23653         4929 :   if (note_cmis)
   23654              :     /* Canonicalize & mark module names.  */
   23655           12 :     for (unsigned ix = 0; ix != note_cmis->length (); ix++)
   23656              :       {
   23657            6 :         const char *name = (*note_cmis)[ix];
   23658            6 :         size_t len = strlen (name);
   23659              : 
   23660            6 :         bool is_system = name[0] == '<';
   23661            6 :         bool is_user = name[0] == '"';
   23662            6 :         bool is_pathname = false;
   23663            6 :         if (!(is_system || is_user))
   23664           12 :           for (unsigned ix = len; !is_pathname && ix--;)
   23665            9 :             is_pathname = IS_DIR_SEPARATOR (name[ix]);
   23666            6 :         if (is_system || is_user || is_pathname)
   23667              :           {
   23668            3 :             if (len <= (is_pathname ? 0 : 2)
   23669            3 :                 || (!is_pathname && name[len-1] != (is_system ? '>' : '"')))
   23670              :               {
   23671            0 :                 error ("invalid header name %qs", name);
   23672            0 :                 continue;
   23673              :               }
   23674              :             else
   23675            3 :               name = canonicalize_header_name (is_pathname ? nullptr : reader,
   23676              :                                                0, is_pathname, name, len);
   23677              :           }
   23678            6 :         if (auto module = get_module (name))
   23679            6 :           module->inform_cmi_p = 1;
   23680              :         else
   23681            0 :           error ("invalid module name %qs", name);
   23682              :       }
   23683              : 
   23684         4929 :   dump.push (NULL);
   23685              : 
   23686              :   /* Determine lazy handle bound.  */
   23687         4929 :   {
   23688         4929 :     unsigned limit = 1000;
   23689              : #if HAVE_GETRLIMIT
   23690         4929 :     struct rlimit rlimit;
   23691         4929 :     if (!getrlimit (RLIMIT_NOFILE, &rlimit))
   23692              :       {
   23693         4929 :         lazy_hard_limit = (rlimit.rlim_max < 1000000
   23694         4929 :                            ? unsigned (rlimit.rlim_max) : 1000000);
   23695         4929 :         lazy_hard_limit = (lazy_hard_limit > LAZY_HEADROOM
   23696         4929 :                            ? lazy_hard_limit - LAZY_HEADROOM : 0);
   23697         4929 :         if (rlimit.rlim_cur < limit)
   23698            0 :           limit = unsigned (rlimit.rlim_cur);
   23699              :       }
   23700              : #endif
   23701         4929 :     limit = limit > LAZY_HEADROOM ? limit - LAZY_HEADROOM : 1;
   23702              : 
   23703         4929 :     if (unsigned parm = param_lazy_modules)
   23704              :       {
   23705         4929 :         if (parm <= limit || !lazy_hard_limit || !try_increase_lazy (parm))
   23706            6 :           lazy_limit = parm;
   23707              :       }
   23708              :     else
   23709            0 :       lazy_limit = limit;
   23710              :   }
   23711              : 
   23712         4929 :   if (dump ())
   23713              :     {
   23714          583 :       verstr_t ver;
   23715          583 :       version2string (MODULE_VERSION, ver);
   23716          583 :       dump ("Source: %s", main_input_filename);
   23717          583 :       dump ("Compiler: %s", version_string);
   23718          583 :       dump ("Modules: %s", ver);
   23719          583 :       dump ("Checking: %s",
   23720              : #if CHECKING_P
   23721              :             "checking"
   23722              : #elif ENABLE_ASSERT_CHECKING
   23723              :             "asserting"
   23724              : #else
   23725              :             "release"
   23726              : #endif
   23727              :             );
   23728          583 :       dump ("Compiled by: "
   23729              : #ifdef __GNUC__
   23730              :             "GCC %d.%d, %s", __GNUC__, __GNUC_MINOR__,
   23731              : #ifdef __OPTIMIZE__
   23732              :             "optimizing"
   23733              : #else
   23734              :             "not optimizing"
   23735              : #endif
   23736              : #else
   23737              :             "not GCC"
   23738              : #endif
   23739              :             );
   23740          583 :       dump ("Reading: %s", MAPPED_READING ? "mmap" : "fileio");
   23741          583 :       dump ("Writing: %s", MAPPED_WRITING ? "mmap" : "fileio");
   23742          583 :       dump ("Lazy limit: %u", lazy_limit);
   23743          583 :       dump ("Lazy hard limit: %u", lazy_hard_limit);
   23744          583 :       dump ("");
   23745              :     }
   23746              : 
   23747              :   /* Construct the global tree array.  This is an array of unique
   23748              :      global trees (& types).  Do this now, rather than lazily, as
   23749              :      some global trees are lazily created and we don't want that to
   23750              :      mess with our syndrome of fixed trees.  */
   23751         4929 :   unsigned crc = 0;
   23752         4929 :   vec_alloc (fixed_trees, 250);
   23753              : 
   23754         5512 :   dump () && dump ("+Creating globals");
   23755              :   /* Insert the TRANSLATION_UNIT_DECL.  */
   23756         4929 :   TREE_VISITED (DECL_CONTEXT (global_namespace)) = true;
   23757         4929 :   fixed_trees->quick_push (DECL_CONTEXT (global_namespace));
   23758        29574 :   for (unsigned jx = 0; global_tree_arys[jx].first; jx++)
   23759              :     {
   23760        24645 :       const tree *ptr = global_tree_arys[jx].first;
   23761        24645 :       unsigned limit = global_tree_arys[jx].second;
   23762              : 
   23763      1508274 :       for (unsigned ix = 0; ix != limit; ix++, ptr++)
   23764              :         {
   23765      1483629 :           !(ix & 31) && dump ("") && dump ("+\t%u:%u:", jx, ix);
   23766      1483629 :           unsigned v = maybe_add_global (*ptr, crc);
   23767      1659112 :           dump () && dump ("+%u", v);
   23768              :         }
   23769              :     }
   23770              :   /* OS- and machine-specific types are dynamically registered at
   23771              :      runtime, so cannot be part of global_tree_arys.  */
   23772         4929 :   registered_builtin_types && dump ("") && dump ("+\tB:");
   23773        19716 :   for (tree t = registered_builtin_types; t; t = TREE_CHAIN (t))
   23774              :     {
   23775        14787 :       unsigned v = maybe_add_global (TREE_VALUE (t), crc);
   23776        16536 :       dump () && dump ("+%u", v);
   23777              :     }
   23778         4929 :   global_crc = crc32_unsigned (crc, fixed_trees->length ());
   23779         4929 :   dump ("") && dump ("Created %u unique globals, crc=%x",
   23780              :                      fixed_trees->length (), global_crc);
   23781       949752 :   for (unsigned ix = fixed_trees->length (); ix--;)
   23782       944823 :     TREE_VISITED ((*fixed_trees)[ix]) = false;
   23783              : 
   23784         4929 :   dump.pop (0);
   23785              : 
   23786         4929 :   if (!flag_module_lazy)
   23787              :     /* Get the mapper now, if we're not being lazy.  */
   23788          313 :     get_mapper (cpp_main_loc (reader), cpp_get_deps (reader));
   23789              : 
   23790         4929 :   if (!flag_preprocess_only)
   23791              :     {
   23792         4785 :       pending_table = new pending_map_t (EXPERIMENT (1, 400));
   23793         4785 :       entity_map = new entity_map_t (EXPERIMENT (1, 400));
   23794         4785 :       vec_safe_reserve (entity_ary, EXPERIMENT (1, 400));
   23795         4785 :       imported_temploid_friends
   23796         4785 :         = decl_tree_cache_map::create_ggc (EXPERIMENT (1, 400));
   23797              :     }
   23798              : 
   23799              : #if CHECKING_P
   23800         4929 :   note_defs = note_defs_table_t::create_ggc (1000);
   23801              : #endif
   23802              : 
   23803         4929 :   if (flag_header_unit && cpp_get_options (reader)->preprocessed)
   23804            9 :     begin_header_unit (reader);
   23805              : 
   23806              :   /* Collect here to make sure things are tagged correctly (when
   23807              :      aggressively GC'd).  */
   23808         4929 :   ggc_collect ();
   23809         4929 : }
   23810              : 
   23811              : /* If NODE is a deferred macro, load it.  */
   23812              : 
   23813              : static int
   23814        83107 : load_macros (cpp_reader *reader, cpp_hashnode *node, void *)
   23815              : {
   23816        83107 :   location_t main_loc
   23817        83107 :     = MAP_START_LOCATION (LINEMAPS_ORDINARY_MAP_AT (line_table, 0));
   23818              : 
   23819        83107 :   if (cpp_user_macro_p (node)
   23820        83107 :       && !node->value.macro)
   23821              :     {
   23822           72 :       cpp_macro *macro = cpp_get_deferred_macro (reader, node, main_loc);
   23823           72 :       dump () && dump ("Loaded macro #%s %I",
   23824              :                        macro ? "define" : "undef", identifier (node));
   23825              :     }
   23826              : 
   23827        83107 :   return 1;
   23828              : }
   23829              : 
   23830              : /* At the end of tokenizing, we no longer need the macro tables of
   23831              :    imports.  But the user might have requested some checking.  */
   23832              : 
   23833              : void
   23834        98278 : maybe_check_all_macros (cpp_reader *reader)
   23835              : {
   23836        98278 :   if (!warn_imported_macros)
   23837              :     return;
   23838              : 
   23839              :   /* Force loading of any remaining deferred macros.  This will
   23840              :      produce diagnostics if they are ill-formed.  */
   23841           21 :   unsigned n = dump.push (NULL);
   23842           21 :   cpp_forall_identifiers (reader, load_macros, NULL);
   23843           21 :   dump.pop (n);
   23844              : }
   23845              : 
   23846              : // State propagated from finish_module_processing to fini_modules
   23847              : 
   23848              : struct module_processing_cookie
   23849              : {
   23850              :   elf_out out;
   23851              :   module_state_config config;
   23852              :   char *cmi_name;
   23853              :   char *tmp_name;
   23854              :   unsigned crc;
   23855              :   bool began;
   23856              : 
   23857         2904 :   module_processing_cookie (char *cmi, char *tmp, int fd, int e)
   23858         2904 :     : out (fd, e), cmi_name (cmi), tmp_name (tmp), crc (0), began (false)
   23859              :   {
   23860              :   }
   23861         2904 :   ~module_processing_cookie ()
   23862              :   {
   23863         2904 :     XDELETEVEC (tmp_name);
   23864         2904 :     XDELETEVEC (cmi_name);
   23865         2904 :   }
   23866              : };
   23867              : 
   23868              : /* Write the CMI, if we're a module interface.  */
   23869              : 
   23870              : void *
   23871        98071 : finish_module_processing (cpp_reader *reader)
   23872              : {
   23873        98071 :   module_processing_cookie *cookie = nullptr;
   23874              : 
   23875        98071 :   if (header_module_p ())
   23876          915 :     module_kind &= ~MK_EXPORTING;
   23877              : 
   23878        98071 :   if (!modules || !this_module ()->name)
   23879              :     {
   23880        95164 :       if (flag_module_only)
   23881            6 :         warning (0, "%<-fmodule-only%> used for non-interface");
   23882              :     }
   23883         2907 :   else if (!flag_syntax_only)
   23884              :     {
   23885         2904 :       int fd = -1;
   23886         2904 :       int e = -1;
   23887              : 
   23888         2904 :       timevar_start (TV_MODULE_EXPORT);
   23889              : 
   23890              :       /* Force a valid but empty line map at the end.  This simplifies
   23891              :          the line table preparation and writing logic.  */
   23892         2904 :       linemap_add (line_table, LC_ENTER, false, "", 0);
   23893              : 
   23894              :       /* We write to a tmpname, and then atomically rename.  */
   23895         2904 :       char *cmi_name = NULL;
   23896         2904 :       char *tmp_name = NULL;
   23897         2904 :       module_state *state = this_module ();
   23898              : 
   23899         2904 :       unsigned n = dump.push (state);
   23900         2904 :       state->announce ("creating");
   23901         2904 :       if (state->filename)
   23902              :         {
   23903         2904 :           size_t len = 0;
   23904         2904 :           cmi_name = xstrdup (maybe_add_cmi_prefix (state->filename, &len));
   23905         2904 :           tmp_name = XNEWVEC (char, len + 3);
   23906         2904 :           memcpy (tmp_name, cmi_name, len);
   23907         2904 :           strcpy (&tmp_name[len], "~");
   23908              : 
   23909         2904 :           if (!errorcount)
   23910           52 :             for (unsigned again = 2; ; again--)
   23911              :               {
   23912         2844 :                 fd = open (tmp_name,
   23913              :                            O_RDWR | O_CREAT | O_TRUNC | O_CLOEXEC | O_BINARY,
   23914              :                            S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH);
   23915         2844 :                 e = errno;
   23916         2844 :                 if (fd >= 0 || !again || e != ENOENT)
   23917              :                   break;
   23918           52 :                 create_dirs (tmp_name);
   23919              :               }
   23920         2904 :           if (note_module_cmi_yes || state->inform_cmi_p)
   23921            3 :             inform (state->loc, "writing CMI %qs", cmi_name);
   23922         3204 :           dump () && dump ("CMI is %s", cmi_name);
   23923              :         }
   23924              : 
   23925         2904 :       cookie = new module_processing_cookie (cmi_name, tmp_name, fd, e);
   23926              : 
   23927         2904 :       if (errorcount)
   23928              :         /* Don't write the module if we have reported errors.  */;
   23929         2792 :       else if (erroneous_templates
   23930         2792 :                && !erroneous_templates->is_empty ())
   23931              :         {
   23932              :           /* Don't write the module if it contains an erroneous template.
   23933              :              Also emit notes about where errors occurred in case
   23934              :              -Wno-template-body was passed.  */
   23935            6 :           auto_diagnostic_group d;
   23936            6 :           error_at (state->loc, "not writing module %qs due to errors "
   23937              :                     "in template bodies", state->get_flatname ());
   23938            6 :           if (!warn_template_body)
   23939            3 :             inform (state->loc, "enable %<-Wtemplate-body%> for more details");
   23940           12 :           for (auto e : *erroneous_templates)
   23941            6 :             inform (e.second, "first error in %qD appeared here", e.first);
   23942            6 :         }
   23943         2786 :       else if (cookie->out.begin ())
   23944              :         {
   23945              :           /* So crashes finger-point the module decl.  */
   23946         2786 :           iloc_sentinel ils = state->loc;
   23947         2786 :           if (state->write_begin (&cookie->out, reader, cookie->config,
   23948         2786 :                                   cookie->crc))
   23949         2757 :             cookie->began = true;
   23950         2786 :         }
   23951              : 
   23952         2904 :       dump.pop (n);
   23953         2904 :       timevar_stop (TV_MODULE_EXPORT);
   23954              : 
   23955         2904 :       ggc_collect ();
   23956              :     }
   23957              : 
   23958        98071 :   if (modules)
   23959              :     {
   23960         4745 :       unsigned n = dump.push (NULL);
   23961         5328 :       dump () && dump ("Imported %u modules", modules->length () - 1);
   23962         5328 :       dump () && dump ("Containing %u clusters", available_clusters);
   23963         4745 :       dump () && dump ("Loaded %u clusters (%u%%)", loaded_clusters,
   23964          583 :                        (loaded_clusters * 100 + available_clusters / 2) /
   23965          583 :                        (available_clusters + !available_clusters));
   23966         4745 :       dump.pop (n);
   23967              :     }
   23968              : 
   23969        98071 :   return cookie;
   23970              : }
   23971              : 
   23972              : // Do the final emission of a module.  At this point we know whether
   23973              : // the module static initializer is a NOP or not.
   23974              : 
   23975              : static void
   23976         2904 : late_finish_module (cpp_reader *reader,  module_processing_cookie *cookie,
   23977              :                     bool init_fn_non_empty)
   23978              : {
   23979         2904 :   timevar_start (TV_MODULE_EXPORT);
   23980              : 
   23981         2904 :   module_state *state = this_module ();
   23982         2904 :   unsigned n = dump.push (state);
   23983         2904 :   state->announce ("finishing");
   23984              : 
   23985         2904 :   cookie->config.active_init = init_fn_non_empty;
   23986         2904 :   if (cookie->began)
   23987         2757 :     state->write_end (&cookie->out, reader, cookie->config, cookie->crc);
   23988              : 
   23989         2904 :   if (cookie->out.end () && cookie->cmi_name)
   23990              :     {
   23991              :       /* Some OS's do not replace NEWNAME if it already exists.
   23992              :          This'll have a race condition in erroneous concurrent
   23993              :          builds.  */
   23994         2792 :       unlink (cookie->cmi_name);
   23995         2792 :       if (rename (cookie->tmp_name, cookie->cmi_name))
   23996              :         {
   23997            0 :           dump () && dump ("Rename ('%s','%s') errno=%u",
   23998            0 :                            cookie->tmp_name, cookie->cmi_name, errno);
   23999            0 :           cookie->out.set_error (errno);
   24000              :         }
   24001              :     }
   24002              : 
   24003         2904 :   if (cookie->out.get_error () && cookie->began)
   24004              :     {
   24005            0 :       error_at (state->loc, "failed to write compiled module: %s",
   24006            0 :                 cookie->out.get_error (state->filename));
   24007            0 :       state->note_cmi_name ();
   24008              :     }
   24009              : 
   24010         2904 :   if (!errorcount)
   24011              :     {
   24012         2751 :       auto *mapper = get_mapper (cpp_main_loc (reader), cpp_get_deps (reader));
   24013         2751 :       mapper->ModuleCompiled (state->get_flatname ());
   24014              :     }
   24015          153 :   else if (cookie->cmi_name)
   24016              :     {
   24017              :       /* We failed, attempt to erase all evidence we even tried.  */
   24018          153 :       unlink (cookie->tmp_name);
   24019          153 :       unlink (cookie->cmi_name);
   24020              :     }
   24021              : 
   24022         2904 :   delete cookie;
   24023         2904 :   dump.pop (n);
   24024         2904 :   timevar_stop (TV_MODULE_EXPORT);
   24025         2904 : }
   24026              : 
   24027              : void
   24028        98071 : fini_modules (cpp_reader *reader, void *cookie, bool has_inits)
   24029              : {
   24030        98071 :   if (cookie)
   24031         2904 :     late_finish_module (reader,
   24032              :                         static_cast<module_processing_cookie *> (cookie),
   24033              :                         has_inits);
   24034              : 
   24035              :   /* We're done with the macro tables now.  */
   24036        98071 :   vec_free (macro_exports);
   24037        98071 :   vec_free (macro_imports);
   24038        98071 :   headers = NULL;
   24039              : 
   24040              :   /* We're now done with everything but the module names.  */
   24041        98071 :   set_cmi_repo (NULL);
   24042        98071 :   if (mapper)
   24043              :     {
   24044         4745 :       timevar_start (TV_MODULE_MAPPER);
   24045         4745 :       module_client::close_module_client (0, mapper);
   24046         4745 :       mapper = nullptr;
   24047         4745 :       timevar_stop (TV_MODULE_MAPPER);
   24048              :     }
   24049        98071 :   module_state_config::release ();
   24050              : 
   24051              : #if CHECKING_P
   24052        98071 :   note_defs = NULL;
   24053              : #endif
   24054              : 
   24055        98071 :   if (modules)
   24056         7691 :     for (unsigned ix = modules->length (); --ix;)
   24057         2946 :       if (module_state *state = (*modules)[ix])
   24058         2946 :         state->release ();
   24059              : 
   24060              :   /* No need to lookup modules anymore.  */
   24061        98071 :   modules_hash = NULL;
   24062              : 
   24063              :   /* Or entity array.  We still need the entity map to find import numbers.  */
   24064        98071 :   vec_free (entity_ary);
   24065        98071 :   entity_ary = NULL;
   24066              : 
   24067              :   /* Or remember any pending entities.  */
   24068       102816 :   delete pending_table;
   24069        98071 :   pending_table = NULL;
   24070              : 
   24071              :   /* Or any keys -- Let it go!  */
   24072       100542 :   delete keyed_table;
   24073        98071 :   keyed_table = NULL;
   24074              : 
   24075              :   /* Allow a GC, we've possibly made much data unreachable.  */
   24076        98071 :   ggc_collect ();
   24077        98071 : }
   24078              : 
   24079              : /* If CODE is a module option, handle it & return true.  Otherwise
   24080              :    return false.  For unknown reasons I cannot get the option
   24081              :    generation machinery to set fmodule-mapper or -fmodule-header to
   24082              :    make a string type option variable.  */
   24083              : 
   24084              : bool
   24085      1943221 : handle_module_option (unsigned code, const char *str, int)
   24086              : {
   24087      1943221 :   auto hdr = CMS_header;
   24088              : 
   24089      1943221 :   switch (opt_code (code))
   24090              :     {
   24091           48 :     case OPT_fmodule_mapper_:
   24092           48 :       module_mapper_name = str;
   24093           48 :       return true;
   24094              : 
   24095           12 :     case OPT_fmodule_header_:
   24096           12 :       {
   24097           12 :         if (!strcmp (str, "user"))
   24098              :           hdr = CMS_user;
   24099           12 :         else if (!strcmp (str, "system"))
   24100              :           hdr = CMS_system;
   24101              :         else
   24102            0 :           error ("unknown header kind %qs", str);
   24103              :       }
   24104              :       /* Fallthrough.  */
   24105              : 
   24106          930 :     case OPT_fmodule_header:
   24107          930 :       flag_header_unit = hdr;
   24108          930 :       flag_modules = 1;
   24109          930 :       return true;
   24110              : 
   24111            1 :     case OPT_flang_info_include_translate_:
   24112            1 :       vec_safe_push (note_includes, str);
   24113            1 :       return true;
   24114              : 
   24115            6 :     case OPT_flang_info_module_cmi_:
   24116            6 :       vec_safe_push (note_cmis, str);
   24117            6 :       return true;
   24118              : 
   24119              :     default:
   24120              :       return false;
   24121              :     }
   24122              : }
   24123              : 
   24124              : /* Set preprocessor callbacks and options for modules.  */
   24125              : 
   24126              : void
   24127        99764 : module_preprocess_options (cpp_reader *reader)
   24128              : {
   24129        99764 :   gcc_checking_assert (!lang_hooks.preprocess_undef);
   24130        99764 :   if (modules_p ())
   24131              :     {
   24132         4929 :       auto *cb = cpp_get_callbacks (reader);
   24133              : 
   24134         4929 :       cb->translate_include = maybe_translate_include;
   24135         4929 :       cb->user_deferred_macro = module_state::deferred_macro;
   24136         4929 :       if (flag_header_unit)
   24137              :         {
   24138              :           /* If the preprocessor hook is already in use, that
   24139              :              implementation will call the undef langhook.  */
   24140          927 :           if (cb->undef)
   24141            0 :             lang_hooks.preprocess_undef = module_state::undef_macro;
   24142              :           else
   24143          927 :             cb->undef = module_state::undef_macro;
   24144              :         }
   24145         4929 :       auto *opt = cpp_get_options (reader);
   24146         4929 :       opt->module_directives = true;
   24147         4929 :       if (flag_no_output)
   24148           18 :         opt->directives_only = true;
   24149         4929 :       if (opt->main_search == CMS_none)
   24150         4924 :         opt->main_search = cpp_main_search (flag_header_unit);
   24151              :     }
   24152        99764 : }
   24153              : 
   24154              : #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.