LCOV - code coverage report
Current view: top level - gcc/cp - module.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 94.2 % 11078 10441
Test Date: 2026-03-28 14:25:54 Functions: 97.9 % 475 465
Legend: Lines:     hit not hit

            Line data    Source code
       1              : /* C++ modules.  Experimental!
       2              :    Copyright (C) 2017-2026 Free Software Foundation, Inc.
       3              :    Written by Nathan Sidwell <nathan@acm.org> while at FaceBook
       4              : 
       5              :    This file is part of GCC.
       6              : 
       7              :    GCC is free software; you can redistribute it and/or modify it
       8              :    under the terms of the GNU General Public License as published by
       9              :    the Free Software Foundation; either version 3, or (at your option)
      10              :    any later version.
      11              : 
      12              :    GCC is distributed in the hope that it will be useful, but
      13              :    WITHOUT ANY WARRANTY; without even the implied warranty of
      14              :    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      15              :    General Public License for more details.
      16              : 
      17              : You should have received a copy of the GNU General Public License
      18              : along with GCC; see the file COPYING3.  If not see
      19              : <http://www.gnu.org/licenses/>.  */
      20              : 
      21              : /* Comments in this file have a non-negligible chance of being wrong
      22              :    or at least inaccurate.  Due to (a) my misunderstanding, (b)
      23              :    ambiguities that I have interpretted differently to original intent
      24              :    (c) changes in the specification, (d) my poor wording, (e) source
      25              :    changes.  */
      26              : 
      27              : /* (Incomplete) Design Notes
      28              : 
      29              :    A hash table contains all module names.  Imported modules are
      30              :    present in a modules array, which by construction places an
      31              :    import's dependencies before the import itself.  The single
      32              :    exception is the current TU, which always occupies slot zero (even
      33              :    when it is not a module).
      34              : 
      35              :    Imported decls occupy an entity_ary, an array of binding_slots, indexed
      36              :    by importing module and index within that module.  A flat index is
      37              :    used, as each module reserves a contiguous range of indices.
      38              :    Initially each slot indicates the CMI section containing the
      39              :    streamed decl.  When the decl is imported it will point to the decl
      40              :    itself.
      41              : 
      42              :    Additionally each imported decl is mapped in the entity_map via its
      43              :    DECL_UID to the flat index in the entity_ary.  Thus we can locate
      44              :    the index for any imported decl by using this map and then
      45              :    de-flattening the index via a binary seach of the module vector.
      46              :    Cross-module references are by (remapped) module number and
      47              :    module-local index.
      48              : 
      49              :    Each importable DECL contains several flags.  The simple set are
      50              :    DECL_MODULE_EXPORT_P, DECL_MODULE_PURVIEW_P, DECL_MODULE_ATTACH_P
      51              :    and DECL_MODULE_IMPORT_P.  The first indicates whether it is
      52              :    exported, the second whether it is in module or header-unit
      53              :    purview.  The third indicates it is attached to the named module in
      54              :    whose purview it resides and the fourth indicates whether it was an
      55              :    import into this TU or not.  DECL_MODULE_ATTACH_P will be false for
      56              :    all decls in a header-unit, and for those in a named module inside
      57              :    a linkage declaration.
      58              : 
      59              :    The more detailed flags are DECL_MODULE_PARTITION_P,
      60              :    DECL_MODULE_ENTITY_P.  The first is set in a primary interface unit
      61              :    on decls that were read from module partitions (these will have
      62              :    DECL_MODULE_IMPORT_P set too).  Such decls will be streamed out to
      63              :    the primary's CMI.  DECL_MODULE_ENTITY_P is set when an entity is
      64              :    imported, even if it matched a non-imported entity.  Such a decl
      65              :    will not have DECL_MODULE_IMPORT_P set, even though it has an entry
      66              :    in the entity map and array.
      67              : 
      68              :    Header units are module-like.
      69              : 
      70              :    For namespace-scope lookup, the decls for a particular module are
      71              :    held located in a sparse array hanging off the binding of the name.
      72              :    This is partitioned into two: a few fixed slots at the start
      73              :    followed by the sparse slots afterwards.  By construction we only
      74              :    need to append new slots to the end -- there is never a need to
      75              :    insert in the middle.  The fixed slots are MODULE_SLOT_CURRENT for
      76              :    the current TU (regardless of whether it is a module or not),
      77              :    MODULE_SLOT_GLOBAL and MODULE_SLOT_PARTITION.  These latter two
      78              :    slots are used for merging entities across the global module and
      79              :    module partitions respectively.  MODULE_SLOT_PARTITION is only
      80              :    present in a module.  Neither of those two slots is searched during
      81              :    name lookup -- they are internal use only.  This vector is created
      82              :    lazily once we require it, if there is only a declaration from the
      83              :    current TU, a regular binding is present.  It is converted on
      84              :    demand.
      85              : 
      86              :    OPTIMIZATION: Outside of the current TU, we only need ADL to work.
      87              :    We could optimize regular lookup for the current TU by glomming all
      88              :    the visible decls on its slot.  Perhaps wait until design is a
      89              :    little more settled though.
      90              : 
      91              :    There is only one instance of each extern-linkage namespace.  It
      92              :    appears in every module slot that makes it visible.  It also
      93              :    appears in MODULE_SLOT_GLOBAL.  (It is an ODR violation if they
      94              :    collide with some other global module entity.)  We also have an
      95              :    optimization that shares the slot for adjacent modules that declare
      96              :    the same such namespace.
      97              : 
      98              :    A module interface compilation produces a Compiled Module Interface
      99              :    (CMI).  The format used is Encapsulated Lazy Records Of Numbered
     100              :    Declarations, which is essentially ELF's section encapsulation. (As
     101              :    all good nerds are aware, Elrond is half Elf.)  Some sections are
     102              :    named, and contain information about the module as a whole (indices
     103              :    etc), and other sections are referenced by number.  Although I
     104              :    don't defend against actively hostile CMIs, there is some
     105              :    checksumming involved to verify data integrity.  When dumping out
     106              :    an interface, we generate a graph of all the
     107              :    independently-redeclarable DECLS that are needed, and the decls
     108              :    they reference.  From that we determine the strongly connected
     109              :    components (SCC) within this TU.  Each SCC is dumped to a separate
     110              :    numbered section of the CMI.  We generate a binding table section,
     111              :    mapping each namespace&name to a defining section.  This allows
     112              :    lazy loading.
     113              : 
     114              :    Lazy loading employs mmap to map a read-only image of the CMI.
     115              :    It thus only occupies address space and is paged in on demand,
     116              :    backed by the CMI file itself.  If mmap is unavailable, regular
     117              :    FILEIO is used.  Also, there's a bespoke ELF reader/writer here,
     118              :    which implements just the section table and sections (including
     119              :    string sections) of a 32-bit ELF in host byte-order.  You can of
     120              :    course inspect it with readelf.  I figured 32-bit is sufficient,
     121              :    for a single module.  I detect running out of section numbers, but
     122              :    do not implement the ELF overflow mechanism.  At least you'll get
     123              :    an error if that happens.
     124              : 
     125              :    We do not separate declarations and definitions.  My guess is that
     126              :    if you refer to the declaration, you'll also need the definition
     127              :    (template body, inline function, class definition etc).  But this
     128              :    does mean we can get larger SCCs than if we separated them.  It is
     129              :    unclear whether this is a win or not.
     130              : 
     131              :    Notice that we embed section indices into the contents of other
     132              :    sections.  Thus random manipulation of the CMI file by ELF tools
     133              :    may well break it.  The kosher way would probably be to introduce
     134              :    indirection via section symbols, but that would require defining a
     135              :    relocation type.
     136              : 
     137              :    Notice that lazy loading of one module's decls can cause lazy
     138              :    loading of other decls in the same or another module.  Clearly we
     139              :    want to avoid loops.  In a correct program there can be no loops in
     140              :    the module dependency graph, and the above-mentioned SCC algorithm
     141              :    places all intra-module circular dependencies in the same SCC.  It
     142              :    also orders the SCCs wrt each other, so dependent SCCs come first.
     143              :    As we load dependent modules first, we know there can be no
     144              :    reference to a higher-numbered module, and because we write out
     145              :    dependent SCCs first, likewise for SCCs within the module.  This
     146              :    allows us to immediately detect broken references.  When loading,
     147              :    we must ensure the rest of the compiler doesn't cause some
     148              :    unconnected load to occur (for instance, instantiate a template).
     149              : 
     150              : Classes used:
     151              : 
     152              :    dumper - logger
     153              : 
     154              :    data - buffer
     155              : 
     156              :    bytes_in : data - scalar reader
     157              :    bytes_out : data - scalar writer
     158              : 
     159              :    bytes_in::bits_in - bit stream reader
     160              :    bytes_out::bits_out - bit stream writer
     161              : 
     162              :    elf - ELROND format
     163              :    elf_in : elf - ELROND reader
     164              :    elf_out : elf - ELROND writer
     165              : 
     166              :    trees_in : bytes_in - tree reader
     167              :    trees_out : bytes_out - tree writer
     168              : 
     169              :    depset - dependency set
     170              :    depset::hash - hash table of depsets
     171              :    depset::tarjan - SCC determinator
     172              : 
     173              :    uidset<T> - set T's related to a UID
     174              :    uidset<T>::hash hash table of uidset<T>
     175              : 
     176              :    loc_spans - location map data
     177              : 
     178              :    module_state - module object
     179              : 
     180              :    slurping - data needed during loading
     181              : 
     182              :    macro_import - imported macro data
     183              :    macro_export - exported macro data
     184              : 
     185              :    The ELROND objects use mmap, for both reading and writing.  If mmap
     186              :    is unavailable, fileno IO is used to read and write blocks of data.
     187              : 
     188              :    The mapper object uses fileno IO to communicate with the server or
     189              :    program.   */
     190              : 
     191              : /* In expermental (trunk) sources, MODULE_VERSION is a #define passed
     192              :    in from the Makefile.  It records the modification date of the
     193              :    source directory -- that's the only way to stay sane.  In release
     194              :    sources, we (plan to) use the compiler's major.minor versioning.
     195              :    While the format might not change between at minor versions, it
     196              :    seems simplest to tie the two together.  There's no concept of
     197              :    inter-version compatibility.  */
     198              : #define IS_EXPERIMENTAL(V) ((V) >= (1U << 20))
     199              : #define MODULE_MAJOR(V) ((V) / 10000)
     200              : #define MODULE_MINOR(V) ((V) % 10000)
     201              : #define EXPERIMENT(A,B) (IS_EXPERIMENTAL (MODULE_VERSION) ? (A) : (B))
     202              : #ifndef MODULE_VERSION
     203              : #include "bversion.h"
     204              : #define MODULE_VERSION (BUILDING_GCC_MAJOR * 10000U + BUILDING_GCC_MINOR)
     205              : #elif !IS_EXPERIMENTAL (MODULE_VERSION)
     206              : #error "This is not the version I was looking for."
     207              : #endif
     208              : 
     209              : #define _DEFAULT_SOURCE 1 /* To get TZ field of struct tm, if available.  */
     210              : #include "config.h"
     211              : #define INCLUDE_STRING
     212              : #define INCLUDE_VECTOR
     213              : #include "system.h"
     214              : #include "coretypes.h"
     215              : #include "cp-tree.h"
     216              : #include "timevar.h"
     217              : #include "stringpool.h"
     218              : #include "dumpfile.h"
     219              : #include "bitmap.h"
     220              : #include "cgraph.h"
     221              : #include "varasm.h"
     222              : #include "tree-iterator.h"
     223              : #include "cpplib.h"
     224              : #include "mkdeps.h"
     225              : #include "incpath.h"
     226              : #include "libiberty.h"
     227              : #include "stor-layout.h"
     228              : #include "version.h"
     229              : #include "tree-diagnostic.h"
     230              : #include "toplev.h"
     231              : #include "opts.h"
     232              : #include "attribs.h"
     233              : #include "intl.h"
     234              : #include "langhooks.h"
     235              : #include "contracts.h"
     236              : /* This TU doesn't need or want to see the networking.  */
     237              : #define CODY_NETWORKING 0
     238              : #include "mapper-client.h"
     239              : #include <zlib.h> // for crc32, crc32_combine
     240              : 
     241              : #if 0 // 1 for testing no mmap
     242              : #define MAPPED_READING 0
     243              : #define MAPPED_WRITING 0
     244              : #else
     245              : #if HAVE_MMAP_FILE && HAVE_MUNMAP && HAVE_MSYNC
     246              : /* mmap, munmap, msync.  */
     247              : #define MAPPED_READING 1
     248              : #if HAVE_SYSCONF && defined (_SC_PAGE_SIZE)
     249              : /* sysconf (_SC_PAGE_SIZE), ftruncate  */
     250              : /* posix_fallocate used if available.  */
     251              : #define MAPPED_WRITING 1
     252              : #else
     253              : #define MAPPED_WRITING 0
     254              : #endif
     255              : #else
     256              : #define MAPPED_READING 0
     257              : #define MAPPED_WRITING 0
     258              : #endif
     259              : #endif
     260              : 
     261              : /* Some open(2) flag differences, what a colourful world it is!  */
     262              : #if defined (O_CLOEXEC)
     263              : // OK
     264              : #elif defined (_O_NOINHERIT)
     265              : /* Windows' _O_NOINHERIT matches O_CLOEXEC flag */
     266              : #define O_CLOEXEC _O_NOINHERIT
     267              : #else
     268              : #define O_CLOEXEC 0
     269              : #endif
     270              : #if defined (O_BINARY)
     271              : // Ok?
     272              : #elif defined (_O_BINARY)
     273              : /* Windows' open(2) call defaults to text!  */
     274              : #define O_BINARY _O_BINARY
     275              : #else
     276              : #define O_BINARY 0
     277              : #endif
     278              : 
     279       275217 : static inline cpp_hashnode *cpp_node (tree id)
     280              : {
     281       275217 :   return CPP_HASHNODE (GCC_IDENT_TO_HT_IDENT (id));
     282              : }
     283              : 
     284       147412 : 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       147412 : #pragma GCC diagnostic push
     291       147412 : #pragma GCC diagnostic ignored "-Warray-bounds"
     292       147412 :   return HT_IDENT_TO_GCC_IDENT (HT_NODE (const_cast<cpp_hashnode *> (node)));
     293       147412 : #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         4082 : version2string (unsigned version, verstr_t &out)
     310              : {
     311         4082 :   unsigned major = MODULE_MAJOR (version);
     312         4082 :   unsigned minor = MODULE_MINOR (version);
     313              : 
     314         4082 :   if (IS_EXPERIMENTAL (version))
     315         4082 :     sprintf (out, "%04u/%02u/%02u-%02u:%02u%s",
     316         4082 :              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         4082 : }
     322              : 
     323              : /* Include files to note translation for.  */
     324              : static vec<const char *, va_heap, vl_embed> *note_includes;
     325              : 
     326              : /* Modules to note CMI pathames.  */
     327              : static vec<const char *, va_heap, vl_embed> *note_cmis;
     328              : 
     329              : /* Traits to hash an arbitrary pointer.  Entries are not deletable,
     330              :    and removal is a noop (removal needed upon destruction).  */
     331              : template <typename T>
     332              : struct nodel_ptr_hash : pointer_hash<T>, typed_noop_remove <T *> {
     333              :   /* Nothing is deletable.  Everything is insertable.  */
     334              :   static bool is_deleted (T *) { return false; }
     335              :   static void mark_deleted (T *) { gcc_unreachable (); }
     336              : };
     337              : 
     338              : /* Map from pointer to signed integer.   */
     339              : typedef simple_hashmap_traits<nodel_ptr_hash<void>, int> ptr_int_traits;
     340              : typedef hash_map<void *,signed,ptr_int_traits> ptr_int_hash_map;
     341              : 
     342              : /********************************************************************/
     343              : /* Basic streaming & ELF.  Serialization is usually via mmap.  For
     344              :    writing we slide a buffer over the output file, syncing it
     345              :    approproiately.  For reading we simply map the whole file (as a
     346              :    file-backed read-only map -- it's just address space, leaving the
     347              :    OS pager to deal with getting the data to us).  Some buffers need
     348              :    to be more conventional malloc'd contents.   */
     349              : 
     350              : /* Variable length buffer.  */
     351              : 
     352              : namespace {
     353              : 
     354              : constexpr line_map_uint_t loc_one = 1;
     355              : 
     356              : class data {
     357              : public:
     358         2829 :   class allocator {
     359              :   public:
     360              :     /* Tools tend to moan if the dtor's not virtual.  */
     361       100948 :     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       751695 :   data ()
     383       751695 :     :buffer (NULL), size (0), pos (0)
     384              :   {
     385              :   }
     386       765850 :   ~data ()
     387              :   {
     388              :     /* Make sure the derived and/or using class know what they're
     389              :        doing.  */
     390       765850 :     gcc_checking_assert (!buffer);
     391       765850 :   }
     392              : 
     393              : protected:
     394    526173831 :   char *use (unsigned count)
     395              :   {
     396    526173831 :     if (size < pos + count)
     397              :       return NULL;
     398    526173831 :     char *res = &buffer[pos];
     399    526173831 :     pos += count;
     400    260775798 :     return res;
     401              :   }
     402              : 
     403              :   unsigned calc_crc (unsigned) const;
     404              : 
     405              : public:
     406     39878732 :   void unuse (unsigned count)
     407              :   {
     408     39878732 :     pos -= count;
     409        27497 :   }
     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       643871 : data::allocator::grow (data &obj, unsigned needed, bool exact)
     423              : {
     424       643871 :   gcc_checking_assert (needed ? needed > obj.size : !obj.size);
     425       643871 :   if (!needed)
     426              :     /* Pick a default size.  */
     427       274268 :     needed = EXPERIMENT (100, 1000);
     428              : 
     429       643871 :   if (!exact)
     430       635717 :     needed *= 2;
     431       643871 :   obj.buffer = grow (obj.buffer, needed);
     432       643871 :   if (obj.buffer)
     433       643871 :     obj.size = needed;
     434              :   else
     435            0 :     obj.pos = obj.size = 0;
     436       643871 : }
     437              : 
     438              : /* Free a buffer.  */
     439              : 
     440              : void
     441       288425 : data::allocator::shrink (data &obj)
     442              : {
     443            0 :   shrink (obj.buffer);
     444       288425 :   obj.buffer = NULL;
     445       288425 :   obj.size = 0;
     446            0 : }
     447              : 
     448              : char *
     449        11343 : data::allocator::grow (char *ptr, unsigned needed)
     450              : {
     451        11343 :   return XRESIZEVAR (char, ptr, needed);
     452              : }
     453              : 
     454              : void
     455        14145 : data::allocator::shrink (char *ptr)
     456              : {
     457        14145 :   XDELETEVEC (ptr);
     458        14145 : }
     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       485586 : data::calc_crc (unsigned l) const
     465              : {
     466       485586 :   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       221304 :   bytes_in ()
     482       221304 :     : parent (), overrun (false)
     483              :   {
     484              :   }
     485       224109 :   ~bytes_in ()
     486              :   {
     487        15624 :   }
     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      1647612 :   bool more_p () const
     499              :   {
     500      1647612 :     return pos != size;
     501              :   }
     502              : 
     503              : public:
     504              :   /* Start reading at OFFSET.  */
     505          651 :   void random_access (unsigned offset)
     506              :   {
     507          651 :     if (offset > size)
     508            0 :       set_overrun ();
     509          651 :     pos = offset;
     510              :   }
     511              : 
     512              : public:
     513      1521721 :   void align (unsigned boundary)
     514              :   {
     515      1521721 :     if (unsigned pad = pos & (boundary - 1))
     516      2947770 :       read (boundary - pad);
     517              :   }
     518              : 
     519              : public:
     520    265398033 :   const char *read (unsigned count)
     521              :   {
     522      1426049 :     char *ptr = use (count);
     523    265398033 :     if (!ptr)
     524            0 :       set_overrun ();
     525    215909978 :     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       222422 :   unsigned get_crc () const
     532              :   {
     533       222422 :     return *(const unsigned *)&buffer[0];
     534              :   }
     535              : 
     536              : public:
     537              :   /* Manipulate the overrun flag.  */
     538    154830761 :   bool get_overrun () const
     539              :   {
     540    154830761 :     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       219431 : bytes_in::check_crc () const
     571              : {
     572       219431 :   if (size < 4)
     573              :     return false;
     574              : 
     575       219431 :   unsigned c_crc = calc_crc (size);
     576       219431 :   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       524593 :   bytes_out (allocator *memory)
     595       524593 :     : parent (), memory (memory)
     596              :   {
     597              :   }
     598       524593 :   ~bytes_out ()
     599              :   {
     600       553914 :   }
     601              : 
     602              : public:
     603    651655692 :   bool streaming_p () const
     604              :   {
     605    651655692 :     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      1711534 :   void align (unsigned boundary)
     619              :   {
     620      1711534 :     if (unsigned pad = pos & (boundary - 1))
     621      1603332 :       write (boundary - pad);
     622      1711534 :   }
     623              : 
     624              : public:
     625    260775798 :   char *write (unsigned count, bool exact = false)
     626              :   {
     627    260775798 :     if (size < pos + count)
     628       355534 :       memory->grow (*this, pos + count, exact);
     629    260775798 :     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        16942 :   void str (const char *ptr)
     644              :   {
     645        16942 :     str (ptr, strlen (ptr));
     646        16942 :   }
     647       268281 :   void cpp_node (const cpp_hashnode *node)
     648              :   {
     649       268281 :     str ((const char *)NODE_NAME (node), NODE_LEN (node));
     650        12815 :   }
     651              :   void str (const char *, size_t);  /* Write string of known length.  */
     652              :   void buf (const void *, size_t);  /* Write fixed length buffer.  */
     653              :   void *buf (size_t); /* Create a writable buffer */
     654              : 
     655              :   struct bits_out;
     656              :   bits_out stream_bits ();
     657              : 
     658              : public:
     659              :   /* Format a NUL-terminated raw string.  */
     660              :   void printf (const char *, ...) ATTRIBUTE_PRINTF_2;
     661              :   void print_time (const char *, const tm *, const char *);
     662              : 
     663              : public:
     664              :   /* Dump instrumentation.  */
     665              :   static void instrument ();
     666              : 
     667              : protected:
     668              :   /* Instrumentation.  */
     669              :   static unsigned spans[4];
     670              :   static unsigned lengths[4];
     671              : };
     672              : } // anon namespace
     673              : 
     674              : /* Finish bit packet.  Rewind the bytes not used.  */
     675              : 
     676              : static unsigned
     677     39824257 : bit_flush (data& bits, uint32_t& bit_val, unsigned& bit_pos)
     678              : {
     679     39824257 :   gcc_assert (bit_pos);
     680     39824257 :   unsigned bytes = (bit_pos + 7) / 8;
     681     39824257 :   bits.unuse (4 - bytes);
     682     39824257 :   bit_pos = 0;
     683     39824257 :   bit_val = 0;
     684     39824257 :   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     15003536 :   bits_in (bytes_in& in)
     707     15003536 :     : in (in)
     708              :   { }
     709              : 
     710     15003536 :   ~bits_in ()
     711              :   {
     712     14078272 :     bflush ();
     713     15003536 :   }
     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     31133812 :   void bflush ()
     721              :   {
     722     31133812 :     if (bit_pos)
     723     17055540 :       bit_flush (in, bit_val, bit_pos);
     724     31133812 :   }
     725              : 
     726              :   /* Read one bit.  */
     727    514582568 :   bool b ()
     728              :   {
     729    514582568 :     if (!bit_pos)
     730     22568288 :       bit_val = in.u32 ();
     731    514582568 :     bool x = (bit_val >> bit_pos) & 1;
     732    514582568 :     bit_pos = (bit_pos + 1) % 32;
     733    514582568 :     return x;
     734              :   }
     735              : };
     736              : 
     737              : /* Factory function for bits_in.  */
     738              : 
     739              : bytes_in::bits_in
     740     15003536 : bytes_in::stream_bits ()
     741              : {
     742     15003536 :   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     14961300 :   bits_out (bytes_out& out)
     754     14961300 :     : out (out)
     755              :   { }
     756              : 
     757     14961300 :   ~bits_out ()
     758              :   {
     759        73663 :     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     31062765 :   void bflush ()
     768              :   {
     769     31062765 :     if (bit_pos)
     770              :       {
     771     17083152 :         out.u32 (bit_val);
     772     17083152 :         out.lengths[2] += bit_flush (out, bit_val, bit_pos);
     773              :       }
     774     31062765 :     out.spans[2]++;
     775     31062765 :     is_set = -1;
     776     31062765 :   }
     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    514517851 :   void b (bool x)
     783              :   {
     784    514517851 :     if (is_set != x)
     785              :       {
     786     54465305 :         is_set = x;
     787     54465305 :         out.spans[x]++;
     788              :       }
     789    514517851 :     out.lengths[x]++;
     790    514517851 :     bit_val |= unsigned (x) << bit_pos++;
     791    514517851 :     if (bit_pos == 32)
     792              :       {
     793      5685565 :         out.u32 (bit_val);
     794      5685565 :         out.lengths[2] += bit_flush (out, bit_val, bit_pos);
     795              :       }
     796    514517851 :   }
     797              : };
     798              : 
     799              : /* Factory function for bits_out.  */
     800              : 
     801              : bytes_out::bits_out
     802     14961300 : bytes_out::stream_bits ()
     803              : {
     804     14961300 :   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       268840 : bytes_out::set_crc (unsigned *crc_ptr)
     816              : {
     817       268840 :   if (crc_ptr)
     818              :     {
     819       266155 :       gcc_checking_assert (pos >= 4);
     820              : 
     821       266155 :       unsigned crc = calc_crc (pos);
     822       266155 :       unsigned accum = *crc_ptr;
     823              :       /* Only mix the existing *CRC_PTR if it is non-zero.  */
     824       266155 :       accum = accum ? crc32_combine (accum, crc, pos - 4) : crc;
     825       266155 :       *crc_ptr = accum;
     826              : 
     827              :       /* Buffer will be sufficiently aligned.  */
     828       266155 :       *(unsigned *)buffer = crc;
     829              :     }
     830       268840 : }
     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     22777425 : bytes_out::u32 (unsigned val)
     839              : {
     840     22777425 :   if (char *ptr = write (4))
     841              :     {
     842     22777425 :       ptr[0] = val;
     843     22777425 :       ptr[1] = val >> 8;
     844     22777425 :       ptr[2] = val >> 16;
     845     22777425 :       ptr[3] = val >> 24;
     846              :     }
     847     22777425 : }
     848              : 
     849              : unsigned
     850     22577689 : bytes_in::u32 ()
     851              : {
     852     22577689 :   unsigned val = 0;
     853     22577689 :   if (const char *ptr = read (4))
     854              :     {
     855     22577689 :       val |= (unsigned char)ptr[0];
     856     22577689 :       val |= (unsigned char)ptr[1] << 8;
     857     22577689 :       val |= (unsigned char)ptr[2] << 16;
     858     22577689 :       val |= (unsigned char)ptr[3] << 24;
     859              :     }
     860              : 
     861     22577689 :   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     91800002 : bytes_out::i (int v)
     887              : {
     888     91800002 :   if (char *ptr = write (1))
     889              :     {
     890     91800002 :       if (v <= 0x3f && v >= -0x40)
     891     72590282 :         *ptr = v & 0x7f;
     892              :       else
     893              :         {
     894     19209720 :           unsigned bytes = 0;
     895     19209720 :           int probe;
     896     19209720 :           if (v >= 0)
     897            0 :             for (probe = v >> 8; probe > 0x7; probe >>= 8)
     898            0 :               bytes++;
     899              :           else
     900     29083774 :             for (probe = v >> 8; probe < -0x8; probe >>= 8)
     901      9874054 :               bytes++;
     902     19209720 :           *ptr = 0x80 | bytes << 4 | (probe & 0xf);
     903     19209720 :           if ((ptr = write (++bytes)))
     904     48293494 :             for (; bytes--; v >>= 8)
     905     29083774 :               ptr[bytes] = v & 0xff;
     906              :         }
     907              :     }
     908     91800002 : }
     909              : 
     910              : int
     911     92381290 : bytes_in::i ()
     912              : {
     913     92381290 :   int v = 0;
     914     92381290 :   if (const char *ptr = read (1))
     915              :     {
     916     92381290 :       v = *ptr & 0xff;
     917     92381290 :       if (v & 0x80)
     918              :         {
     919     20745664 :           unsigned bytes = (v >> 4) & 0x7;
     920     20745664 :           v &= 0xf;
     921     20745664 :           if (v & 0x8)
     922     20745664 :             v |= -1 ^ 0x7;
     923              :           /* unsigned necessary due to left shifts of -ve values.  */
     924     20745664 :           unsigned uv = unsigned (v);
     925     20745664 :           if ((ptr = read (++bytes)))
     926     53292931 :             while (bytes--)
     927     32547267 :               uv = (uv << 8) | (*ptr++ & 0xff);
     928     20745664 :           v = int (uv);
     929              :         }
     930     71635626 :       else if (v & 0x40)
     931      9269230 :         v |= -1 ^ 0x3f;
     932              :     }
     933              : 
     934     92381290 :   return v;
     935              : }
     936              : 
     937              : void
     938     79222515 : bytes_out::u (unsigned v)
     939              : {
     940     79222515 :   if (char *ptr = write (1))
     941              :     {
     942     79222515 :       if (v <= 0x7f)
     943     68994059 :         *ptr = v;
     944              :       else
     945              :         {
     946     10228456 :           unsigned bytes = 0;
     947     10228456 :           unsigned probe;
     948     12108431 :           for (probe = v >> 8; probe > 0xf; probe >>= 8)
     949      1879975 :             bytes++;
     950     10228456 :           *ptr = 0x80 | bytes << 4 | probe;
     951     10228456 :           if ((ptr = write (++bytes)))
     952     22336887 :             for (; bytes--; v >>= 8)
     953     12108431 :               ptr[bytes] = v & 0xff;
     954              :         }
     955              :     }
     956     79222515 : }
     957              : 
     958              : unsigned
     959     80959251 : bytes_in::u ()
     960              : {
     961     80959251 :   unsigned v = 0;
     962              : 
     963     80959251 :   if (const char *ptr = read (1))
     964              :     {
     965     80959251 :       v = *ptr & 0xff;
     966     80959251 :       if (v & 0x80)
     967              :         {
     968     10676775 :           unsigned bytes = (v >> 4) & 0x7;
     969     10676775 :           v &= 0xf;
     970     10676775 :           if ((ptr = read (++bytes)))
     971     23458986 :             while (bytes--)
     972     12782211 :               v = (v << 8) | (*ptr++ & 0xff);
     973              :         }
     974              :     }
     975              : 
     976     80959251 :   return v;
     977              : }
     978              : 
     979              : void
     980     18975694 : bytes_out::wi (HOST_WIDE_INT v)
     981              : {
     982     18975694 :   if (char *ptr = write (1))
     983              :     {
     984     18975694 :       if (v <= 0x3f && v >= -0x40)
     985      3783049 :         *ptr = v & 0x7f;
     986              :       else
     987              :         {
     988     15192645 :           unsigned bytes = 0;
     989     15192645 :           HOST_WIDE_INT probe;
     990     15192645 :           if (v >= 0)
     991     54900880 :             for (probe = v >> 8; probe > 0x7; probe >>= 8)
     992     39710751 :               bytes++;
     993              :           else
     994         8282 :             for (probe = v >> 8; probe < -0x8; probe >>= 8)
     995         5766 :               bytes++;
     996     15192645 :           *ptr = 0x80 | bytes << 4 | (probe & 0xf);
     997     15192645 :           if ((ptr = write (++bytes)))
     998     70101807 :             for (; bytes--; v >>= 8)
     999     54909162 :               ptr[bytes] = v & 0xff;
    1000              :         }
    1001              :     }
    1002     18975694 : }
    1003              : 
    1004              : HOST_WIDE_INT
    1005     18470027 : bytes_in::wi ()
    1006              : {
    1007     18470027 :   HOST_WIDE_INT v = 0;
    1008     18470027 :   if (const char *ptr = read (1))
    1009              :     {
    1010     18470027 :       v = *ptr & 0xff;
    1011     18470027 :       if (v & 0x80)
    1012              :         {
    1013     16639567 :           unsigned bytes = (v >> 4) & 0x7;
    1014     16639567 :           v &= 0xf;
    1015     16639567 :           if (v & 0x8)
    1016         2012 :             v |= -1 ^ 0x7;
    1017              :           /* unsigned necessary due to left shifts of -ve values.  */
    1018     16639567 :           unsigned HOST_WIDE_INT uv = (unsigned HOST_WIDE_INT) v;
    1019     16639567 :           if ((ptr = read (++bytes)))
    1020     76730787 :             while (bytes--)
    1021     60091220 :               uv = (uv << 8) | (*ptr++ & 0xff);
    1022     16639567 :           v = (HOST_WIDE_INT) uv;
    1023              :         }
    1024      1830460 :       else if (v & 0x40)
    1025         8277 :         v |= -1 ^ 0x3f;
    1026              :     }
    1027              : 
    1028     18470027 :   return v;
    1029              : }
    1030              : 
    1031              : /* unsigned wide ints are just written as signed wide ints.  */
    1032              : 
    1033              : inline void
    1034     18975106 : bytes_out::wu (unsigned HOST_WIDE_INT v)
    1035              : {
    1036     18975106 :   wi ((HOST_WIDE_INT) v);
    1037              : }
    1038              : 
    1039              : inline unsigned HOST_WIDE_INT
    1040     18469485 : bytes_in::wu ()
    1041              : {
    1042     36357472 :   return (unsigned HOST_WIDE_INT) wi ();
    1043              : }
    1044              : 
    1045              : /* size_t written as unsigned or unsigned wide int.  */
    1046              : 
    1047              : inline void
    1048      1680160 : bytes_out::z (size_t s)
    1049              : {
    1050      1680160 :   if (sizeof (s) == sizeof (unsigned))
    1051              :     u (s);
    1052              :   else
    1053      3327348 :     wu (s);
    1054           12 : }
    1055              : 
    1056              : inline size_t
    1057      1505320 : bytes_in::z ()
    1058              : {
    1059      1505320 :   if (sizeof (size_t) == sizeof (unsigned))
    1060              :     return u ();
    1061              :   else
    1062      3010640 :     return wu ();
    1063              : }
    1064              : 
    1065              : /* location_t written as 32- or 64-bit as needed.  */
    1066              : 
    1067     16672448 : inline void bytes_out::loc (location_t l)
    1068              : {
    1069     16672448 :   if (sizeof (location_t) > sizeof (unsigned))
    1070     31609743 :     wu (l);
    1071              :   else
    1072              :     u (l);
    1073      1732468 : }
    1074              : 
    1075     16385615 : inline location_t bytes_in::loc ()
    1076              : {
    1077     16385615 :   if (sizeof (location_t) > sizeof (unsigned))
    1078     32768252 :     return wu ();
    1079              :   else
    1080              :     return u ();
    1081              : }
    1082              : 
    1083              : /* Buffer simply memcpied.  */
    1084              : void *
    1085      1711534 : bytes_out::buf (size_t len)
    1086              : {
    1087      1711534 :   align (sizeof (void *) * 2);
    1088      1711534 :   return write (len);
    1089              : }
    1090              : 
    1091              : void
    1092      1662966 : bytes_out::buf (const void *src, size_t len)
    1093              : {
    1094      1662966 :   if (void *ptr = buf (len))
    1095      1662966 :     memcpy (ptr, src, len);
    1096      1662966 : }
    1097              : 
    1098              : const void *
    1099      1521721 : bytes_in::buf (size_t len)
    1100              : {
    1101      1521721 :   align (sizeof (void *) * 2);
    1102      1521721 :   const char *ptr = read (len);
    1103              : 
    1104      1521721 :   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      1680142 : bytes_out::str (const char *string, size_t len)
    1112              : {
    1113      1647182 :   z (len);
    1114      1647182 :   if (len)
    1115              :     {
    1116      1647182 :       gcc_checking_assert (!string[len]);
    1117      1647182 :       buf (string, len + 1);
    1118              :     }
    1119        32960 : }
    1120              : 
    1121              : const char *
    1122      1505314 : bytes_in::str (size_t *len_p)
    1123              : {
    1124      1505314 :   size_t len = z ();
    1125              : 
    1126              :   /* We're about to trust some user data.  */
    1127      1505314 :   if (overrun)
    1128            0 :     len = 0;
    1129      1505314 :   if (len_p)
    1130      1501440 :     *len_p = len;
    1131      1505314 :   const char *str = NULL;
    1132      1505314 :   if (len)
    1133              :     {
    1134      1505051 :       str = reinterpret_cast<const char *> (buf (len + 1));
    1135      1505051 :       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       275480 : bytes_in::cpp_node ()
    1146              : {
    1147       275480 :   size_t len;
    1148       275480 :   const char *s = str (&len);
    1149       275480 :   if (!len)
    1150              :     return NULL;
    1151       275217 :   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        27497 : bytes_out::printf (const char *format, ...)
    1159              : {
    1160        27497 :   va_list args;
    1161              :   /* Exercise buffer expansion.  */
    1162        27497 :   size_t len = EXPERIMENT (10, 500);
    1163              : 
    1164        54475 :   while (char *ptr = write (len))
    1165              :     {
    1166        54475 :       va_start (args, format);
    1167        54475 :       size_t actual = vsnprintf (ptr, len, format, args) + 1;
    1168        54475 :       va_end (args);
    1169        54475 :       if (actual <= len)
    1170              :         {
    1171        27497 :           unuse (len - actual);
    1172        27497 :           break;
    1173              :         }
    1174        26978 :       unuse (len);
    1175        26978 :       len = actual;
    1176        26978 :     }
    1177        27497 : }
    1178              : 
    1179              : void
    1180         5370 : bytes_out::print_time (const char *kind, const tm *time, const char *tz)
    1181              : {
    1182         5370 :   printf ("%stime: %4u/%02u/%02u %02u:%02u:%02u %s",
    1183         5370 :           kind, time->tm_year + 1900, time->tm_mon + 1, time->tm_mday,
    1184         5370 :           time->tm_hour, time->tm_min, time->tm_sec, tz);
    1185         5370 : }
    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         5798 :   elf (int fd, int e)
    1293        11596 :     :hdr (), sectab (), strtab (), fd (fd), err (fd >= 0 ? 0 : e)
    1294              :   {}
    1295         5716 :   ~elf ()
    1296              :   {
    1297         5716 :     gcc_checking_assert (fd < 0 && !hdr.buffer
    1298              :                          && !sectab.buffer && !strtab.buffer);
    1299         5716 :   }
    1300              : 
    1301              : public:
    1302              :   /* Return the error, if we have an error.  */
    1303       428515 :   int get_error () const
    1304              :   {
    1305       428515 :     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         5683 :   bool begin () const
    1319              :   {
    1320         5683 :     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         8089 : elf::end ()
    1353              : {
    1354              :   /* Close the stream and free the section table.  */
    1355         8089 :   if (fd >= 0 && close (fd))
    1356            0 :     set_error (errno);
    1357         8089 :   fd = -1;
    1358              : 
    1359         8089 :   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         2969 :   elf_in (int fd, int e)
    1376         5938 :     :parent (fd, e)
    1377              :   {
    1378              :   }
    1379         2887 :   ~elf_in ()
    1380              :   {
    1381         2887 :   }
    1382              : 
    1383              : public:
    1384       201303 :   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         1057 :   static void release (elf_in *self ATTRIBUTE_UNUSED, bytes_in &bytes)
    1411              :   {
    1412              : #if MAPPED_READING
    1413         1057 :     if (!(self && self->hdr.buffer && bytes.buffer >= self->hdr.buffer
    1414         1057 :           && bytes.buffer < self->hdr.buffer + self->hdr.pos))
    1415              : #endif
    1416            0 :       data::simple_memory.shrink (bytes.buffer);
    1417         1057 :     bytes.buffer = NULL;
    1418         1057 :     bytes.size = 0;
    1419         1057 :   }
    1420              : 
    1421              : public:
    1422       225321 :   static void grow (data &data, unsigned needed)
    1423              :   {
    1424       225321 :     gcc_checking_assert (!data.buffer);
    1425              : #if !MAPPED_READING
    1426              :     data.buffer = XNEWVEC (char, needed);
    1427              : #endif
    1428       225321 :     data.size = needed;
    1429       225321 :   }
    1430       231775 :   static void shrink (data &data)
    1431              :   {
    1432              : #if !MAPPED_READING
    1433              :     XDELETEVEC (data.buffer);
    1434              : #endif
    1435       231775 :     data.buffer = NULL;
    1436       231775 :     data.size = 0;
    1437            0 :   }
    1438              : 
    1439              : public:
    1440       222376 :   const section *get_section (unsigned s) const
    1441              :   {
    1442       222376 :     if (s * sizeof (section) < sectab.size)
    1443       222376 :       return reinterpret_cast<const section *>
    1444       222376 :         (&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       222376 :   bool read (data *d, const section *s)
    1459              :   {
    1460       222376 :     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         8180 :   void release ()
    1471              :   {
    1472         8180 :     shrink (strtab);
    1473           39 :   }
    1474              : 
    1475              : public:
    1476              :   bool begin (location_t);
    1477         5260 :   bool end ()
    1478              :   {
    1479         5260 :     release ();
    1480              : #if MAPPED_READING
    1481         5260 :     if (hdr.buffer)
    1482         2887 :       munmap (hdr.buffer, hdr.pos);
    1483         5260 :     hdr.buffer = NULL;
    1484              : #endif
    1485         5260 :     shrink (sectab);
    1486              : 
    1487         5260 :     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       328879 :   const char *name (unsigned offset)
    1494              :   {
    1495       657758 :     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         2829 :   elf_out (int fd, int e)
    1517         5549 :     :parent (fd, e), identtab (500), pos (0)
    1518              :   {
    1519              : #if MAPPED_WRITING
    1520         2829 :     offset = extent = 0;
    1521         2829 :     page_size = sysconf (_SC_PAGE_SIZE);
    1522         2829 :     if (page_size < SECTION_ALIGN)
    1523              :       /* Something really strange.  */
    1524            0 :       set_error (EINVAL);
    1525              : #endif
    1526         2829 :   }
    1527         2829 :   ~elf_out ()
    1528         2829 :   {
    1529         2829 :     data::simple_memory.shrink (hdr);
    1530         2829 :     data::simple_memory.shrink (sectab);
    1531         2829 :     data::simple_memory.shrink (strtab);
    1532         2829 :   }
    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         5667 :   unsigned get_section_limit () const
    1550              :   {
    1551         5667 :     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        21046 : bytes_in::begin (location_t loc, elf_in *source, const char *name)
    1589              : {
    1590        21046 :   unsigned snum = source->find (name);
    1591              : 
    1592        21046 :   return begin (loc, source, snum, name);
    1593              : }
    1594              : 
    1595              : /* Begin reading section numbered SNUM with NAME (may be NULL).  */
    1596              : 
    1597              : bool
    1598       219431 : bytes_in::begin (location_t loc, elf_in *source, unsigned snum, const char *name)
    1599              : {
    1600       219431 :   if (!source->read (this, source->find (snum))
    1601       219431 :       || !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       219431 :   pos = 4;
    1612       219431 :   return true;
    1613              : }
    1614              : 
    1615              : /* Finish reading a section.  */
    1616              : 
    1617              : bool
    1618       218335 : bytes_in::end (elf_in *src)
    1619              : {
    1620       218335 :   if (more_p ())
    1621           13 :     set_overrun ();
    1622       218335 :   if (overrun)
    1623           13 :     src->set_error ();
    1624              : 
    1625       218335 :   src->shrink (*this);
    1626              : 
    1627       218335 :   return !overrun;
    1628              : }
    1629              : 
    1630              : /* Begin writing buffer.  */
    1631              : 
    1632              : void
    1633       268840 : bytes_out::begin (bool need_crc)
    1634              : {
    1635            0 :   if (need_crc)
    1636            0 :     pos = 4;
    1637            0 :   memory->grow (*this, 0, false);
    1638       247644 : }
    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       268840 : bytes_out::end (elf_out *sink, unsigned name, unsigned *crc_ptr)
    1646              : {
    1647       268840 :   lengths[3] += pos;
    1648       268840 :   spans[3]++;
    1649              : 
    1650       268840 :   set_crc (crc_ptr);
    1651       268840 :   unsigned sec_num = sink->add (*this, !crc_ptr, name);
    1652       268840 :   memory->shrink (*this);
    1653              : 
    1654       268840 :   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       225321 : elf_in::read (data *data, unsigned pos, unsigned length)
    1720              : {
    1721              : #if MAPPED_READING
    1722       225321 :   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       225321 :   grow (*data, length);
    1735              : #if MAPPED_READING
    1736       225321 :   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       225321 :   return data->buffer;
    1747              : }
    1748              : 
    1749              : /* Read section SNUM of TYPE.  Return section pointer or NULL on error.  */
    1750              : 
    1751              : const elf::section *
    1752       222376 : elf_in::find (unsigned snum, unsigned type)
    1753              : {
    1754       222376 :   const section *sec = get_section (snum);
    1755       222376 :   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        21091 : elf_in::find (const char *sname)
    1765              : {
    1766       135020 :   for (unsigned pos = sectab.size; pos -= sizeof (section); )
    1767              :     {
    1768       135020 :       const section *sec
    1769       135020 :         = reinterpret_cast<const section *> (&sectab.buffer[pos]);
    1770              : 
    1771       270040 :       if (0 == strcmp (sname, name (sec->name)))
    1772        21091 :         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         2969 : elf_in::begin (location_t loc)
    1783              : {
    1784         2969 :   if (!parent::begin ())
    1785              :     return false;
    1786              : 
    1787         2945 :   struct stat stat;
    1788         2945 :   unsigned size = 0;
    1789         2945 :   if (!fstat (fd, &stat))
    1790              :     {
    1791              : #if !defined (HOST_LACKS_INODE_NUMBERS)
    1792         2945 :       device = stat.st_dev;
    1793         2945 :       inode = stat.st_ino;
    1794              : #endif
    1795              :       /* Never generate files > 4GB, check we've not been given one.  */
    1796         2945 :       if (stat.st_size == unsigned (stat.st_size))
    1797         2945 :         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         2945 :   void *mapping = mmap (NULL, size, PROT_READ, MAP_SHARED, fd, 0);
    1804         2945 :   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         2945 :   if (madvise (reinterpret_cast <char *> (mapping), size, MADV_RANDOM))
    1813            0 :     goto fail;
    1814              : 
    1815         2945 :   hdr.buffer = (char *)mapping;
    1816              : #else
    1817              :   read (&hdr, 0, sizeof (header));
    1818              : #endif
    1819         2945 :   hdr.pos = size; /* Record size of the file.  */
    1820              : 
    1821         2945 :   const header *h = reinterpret_cast<const header *> (hdr.buffer);
    1822         2945 :   if (!h)
    1823              :     return false;
    1824              : 
    1825         2945 :   if (h->ident.magic[0] != 0x7f
    1826         2945 :       || h->ident.magic[1] != 'E'
    1827         2945 :       || h->ident.magic[2] != 'L'
    1828         2945 :       || 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         2945 :   if (h->ident.klass != MY_CLASS
    1839         2945 :       || h->ident.data != MY_ENDIAN
    1840         2945 :       || h->ident.version != EV_CURRENT
    1841         2945 :       || h->type != ET_NONE
    1842         2945 :       || h->machine != EM_NONE
    1843         2945 :       || h->ident.osabi != OSABI_NONE)
    1844              :     {
    1845            0 :       error_at (loc, "unexpected encapsulation format or type");
    1846            0 :       goto failed;
    1847              :     }
    1848              : 
    1849         2945 :   int e = -1;
    1850         2945 :   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         2945 :   unsigned strndx = h->shstrndx;
    1859         2945 :   unsigned shnum = h->shnum;
    1860         2945 :   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         2945 :   if (!shnum)
    1875            0 :     goto malformed;
    1876              : 
    1877         2945 :   if (!read (&sectab, h->shoff, shnum * sizeof (section)))
    1878            0 :     goto section_table_fail;
    1879              : 
    1880         2945 :   if (strndx == SHN_XINDEX)
    1881            0 :     strndx = get_section (0)->link;
    1882              : 
    1883         2945 :   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         2945 :   if (!(strtab.size && !strtab.buffer[0]
    1889         2945 :         && !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         2945 :   sectab.pos = h->shoff;
    1895         2945 :   strtab.pos = shnum * sizeof (section);
    1896              : #else
    1897              :   shrink (hdr);
    1898              : #endif
    1899              : 
    1900         2945 :   return true;
    1901              : }
    1902              : 
    1903              : /* Create a new mapping.  */
    1904              : 
    1905              : #if MAPPED_WRITING
    1906              : void
    1907         3473 : 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         6808 :   auto allocate = [](int fd, off_t offset, off_t length)
    1912              :     {
    1913              : #ifdef HAVE_POSIX_FALLOCATE
    1914         3335 :       int result = posix_fallocate (fd, offset, length);
    1915         3335 :       if (result != EINVAL)
    1916         3335 :         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         3473 :   void *mapping = MAP_FAILED;
    1923         3473 :   if (extending && ext < 1024 * 1024)
    1924              :     {
    1925         3205 :       if (allocate (fd, offset, ext * 2))
    1926         3205 :         mapping = mmap (NULL, ext * 2, PROT_READ | PROT_WRITE,
    1927         3205 :                         MAP_SHARED, fd, offset);
    1928         3205 :       if (mapping != MAP_FAILED)
    1929              :         ext *= 2;
    1930              :     }
    1931              :   if (mapping == MAP_FAILED)
    1932              :     {
    1933          268 :       if (!extending || allocate (fd, offset, ext))
    1934          268 :         mapping = mmap (NULL, ext, PROT_READ | PROT_WRITE,
    1935          268 :                         MAP_SHARED, fd, offset);
    1936          268 :       if (mapping == MAP_FAILED)
    1937              :         {
    1938            0 :           set_error (errno);
    1939              :           mapping = NULL;
    1940              :           ext = 0;
    1941              :         }
    1942              :     }
    1943         3473 :   hdr.buffer = (char *)mapping;
    1944         3473 :   extent = ext;
    1945         3473 : }
    1946              : #endif
    1947              : 
    1948              : /* Flush out the current mapping.  */
    1949              : 
    1950              : #if MAPPED_WRITING
    1951              : void
    1952         3479 : elf_out::remove_mapping ()
    1953              : {
    1954         3479 :   if (hdr.buffer)
    1955              :     {
    1956              :       /* MS_ASYNC dtrt with the removed mapping, including a
    1957              :          subsequent overlapping remap.  */
    1958         3473 :       if (msync (hdr.buffer, extent, MS_ASYNC)
    1959         3473 :           || munmap (hdr.buffer, extent))
    1960              :         /* We're somewhat screwed at this point.  */
    1961            0 :         set_error (errno);
    1962              :     }
    1963              : 
    1964         3479 :   hdr.buffer = NULL;
    1965         3479 : }
    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       632528 : elf_out::grow (char *data, unsigned needed)
    1973              : {
    1974       632528 :   if (!data)
    1975              :     {
    1976              :       /* First allocation, check we're aligned.  */
    1977       274280 :       gcc_checking_assert (!(pos & (SECTION_ALIGN - 1)));
    1978              : #if MAPPED_WRITING
    1979       274280 :       data = hdr.buffer + (pos - offset);
    1980              : #endif
    1981              :     }
    1982              : 
    1983              : #if MAPPED_WRITING
    1984       632528 :   unsigned off = data - hdr.buffer;
    1985       632528 :   if (off + needed > extent)
    1986              :     {
    1987              :       /* We need to grow the mapping.  */
    1988          621 :       unsigned lwm = off & ~(page_size - 1);
    1989          621 :       unsigned hwm = (off + needed + page_size - 1) & ~(page_size - 1);
    1990              : 
    1991          621 :       gcc_checking_assert (hwm > extent);
    1992              : 
    1993          621 :       remove_mapping ();
    1994              : 
    1995          621 :       offset += lwm;
    1996          621 :       create_mapping (extent < hwm - lwm ? hwm - lwm : extent);
    1997              : 
    1998          621 :       data = hdr.buffer + (off - lwm);
    1999              :     }
    2000              : #else
    2001              :   data = allocator::grow (data, needed);
    2002              : #endif
    2003              : 
    2004       632528 :   return data;
    2005              : }
    2006              : 
    2007              : #if MAPPED_WRITING
    2008              : /* Shrinking is a NOP.  */
    2009              : void
    2010       274280 : elf_out::shrink (char *)
    2011              : {
    2012       274280 : }
    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      1177747 : elf_out::strtab_write (const char *s, unsigned l)
    2020              : {
    2021      1177747 :   if (strtab.pos + l > strtab.size)
    2022         1231 :     data::simple_memory.grow (strtab, strtab.pos + l, false);
    2023      1177747 :   memcpy (strtab.buffer + strtab.pos, s, l);
    2024      1177747 :   unsigned res = strtab.pos;
    2025      1177747 :   strtab.pos += l;
    2026      1177747 :   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       461094 : elf_out::strtab_write (tree decl, int inner)
    2034              : {
    2035       461094 :   tree ctx = CP_DECL_CONTEXT (decl);
    2036       461094 :   if (TYPE_P (ctx))
    2037         4655 :     ctx = TYPE_NAME (ctx);
    2038       461094 :   if (ctx != global_namespace)
    2039       227822 :     strtab_write (ctx, -1);
    2040              : 
    2041       461094 :   tree name = DECL_NAME (decl);
    2042       461094 :   if (!name)
    2043          363 :     name = DECL_ASSEMBLER_NAME_RAW (decl);
    2044       461094 :   strtab_write (IDENTIFIER_POINTER (name), IDENTIFIER_LENGTH (name));
    2045              : 
    2046       461094 :   if (inner)
    2047       325746 :     strtab_write (&"::{}"[inner+1], 2);
    2048       461094 : }
    2049              : 
    2050              : /* Map IDENTIFIER IDENT to strtab offset.  Inserts into strtab if not
    2051              :    already there.  */
    2052              : 
    2053              : unsigned
    2054       136493 : elf_out::name (tree ident)
    2055              : {
    2056       136493 :   unsigned res = 0;
    2057       136493 :   if (ident)
    2058              :     {
    2059       136441 :       bool existed;
    2060       136441 :       int *slot = &identtab.get_or_insert (ident, &existed);
    2061       136441 :       if (!existed)
    2062       245810 :         *slot = strtab_write (IDENTIFIER_POINTER (ident),
    2063       122905 :                               IDENTIFIER_LENGTH (ident) + 1);
    2064       136441 :       res = *slot;
    2065              :     }
    2066       136493 :   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        34730 : elf_out::name (const char *literal)
    2074              : {
    2075        34730 :   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       233272 : elf_out::qualified_name (tree decl, bool is_defn)
    2083              : {
    2084       233272 :   gcc_checking_assert (DECL_P (decl) && decl != global_namespace);
    2085       233272 :   unsigned result = strtab.pos;
    2086              : 
    2087       233272 :   strtab_write (decl, is_defn);
    2088       233272 :   strtab_write ("", 1);
    2089              : 
    2090       233272 :   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       274274 : elf_out::add (unsigned type, unsigned name, unsigned off, unsigned size,
    2099              :               unsigned flags)
    2100              : {
    2101       274274 :   gcc_checking_assert (!(off & (SECTION_ALIGN - 1)));
    2102       274274 :   if (sectab.pos + sizeof (section) > sectab.size)
    2103         4684 :     data::simple_memory.grow (sectab, sectab.pos + sizeof (section), false);
    2104       274274 :   section *sec = reinterpret_cast<section *> (sectab.buffer + sectab.pos);
    2105       274274 :   memset (sec, 0, sizeof (section));
    2106       274274 :   sec->type = type;
    2107       274274 :   sec->flags = flags;
    2108       274274 :   sec->name = name;
    2109       274274 :   sec->offset = off;
    2110       274274 :   sec->size = size;
    2111       274274 :   if (flags & SHF_STRINGS)
    2112         5405 :     sec->entsize = 1;
    2113              : 
    2114       274274 :   unsigned res = sectab.pos;
    2115       274274 :   sectab.pos += sizeof (section);
    2116       274274 :   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        10874 : elf_out::write (const data &buffer)
    2124              : {
    2125              : #if MAPPED_WRITING
    2126              :   /* HDR is always mapped.  */
    2127        10874 :   if (&buffer != &hdr)
    2128              :     {
    2129         5440 :       bytes_out out (this);
    2130         5440 :       grow (out, buffer.pos, true);
    2131         5440 :       if (out.buffer)
    2132         5440 :         memcpy (out.buffer, buffer.buffer, buffer.pos);
    2133         5440 :       shrink (out);
    2134         5440 :     }
    2135              :   else
    2136              :     /* We should have been aligned during the first allocation.  */
    2137         5434 :     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        10874 :   unsigned res = pos;
    2146        10874 :   pos += buffer.pos;
    2147              : 
    2148        10874 :   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         9259 :       pos += padding;
    2158              :     }
    2159        10874 :   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       268840 : elf_out::write (const bytes_out &buf)
    2167              : {
    2168       268840 :   gcc_checking_assert (buf.memory == this);
    2169              :   /* A directly mapped buffer.  */
    2170       268840 :   gcc_checking_assert (buf.buffer - hdr.buffer >= 0
    2171              :                        && buf.buffer - hdr.buffer + buf.size <= extent);
    2172       268840 :   unsigned res = pos;
    2173       268840 :   pos += buf.pos;
    2174              : 
    2175              :   /* Align up.  We're not going to advance into the next page. */
    2176       268840 :   pos += -pos & (SECTION_ALIGN - 1);
    2177              : 
    2178       268840 :   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       268840 : elf_out::add (const bytes_out &data, bool string_p, unsigned name)
    2189              : {
    2190       268840 :   unsigned off = write (data);
    2191              : 
    2192       537680 :   return add (string_p ? SHT_STRTAB : SHT_PROGBITS, name,
    2193       268840 :               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         2714 : elf_out::begin ()
    2201              : {
    2202         2714 :   if (!parent::begin ())
    2203              :     return false;
    2204              : 
    2205              :   /* Let the allocators pick a default.  */
    2206         2714 :   data::simple_memory.grow (strtab, 0, false);
    2207         2714 :   data::simple_memory.grow (sectab, 0, false);
    2208              : 
    2209              :   /* The string table starts with an empty string.  */
    2210         2714 :   name ("");
    2211              : 
    2212              :   /* Create the UNDEF section.  */
    2213         2714 :   add (SHT_NONE);
    2214              : 
    2215              : #if MAPPED_WRITING
    2216              :   /* Start a mapping.  */
    2217         2714 :   create_mapping (EXPERIMENT (page_size,
    2218              :                               (32767 + page_size) & ~(page_size - 1)));
    2219         2714 :   if (!hdr.buffer)
    2220              :     return false;
    2221              : #endif
    2222              : 
    2223              :   /* Write an empty header.  */
    2224         2714 :   grow (hdr, sizeof (header), true);
    2225         2714 :   header *h = reinterpret_cast<header *> (hdr.buffer);
    2226         2714 :   memset (h, 0, sizeof (header));
    2227         2714 :   hdr.pos = hdr.size;
    2228         2714 :   write (hdr);
    2229         2714 :   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         2829 : elf_out::end ()
    2237              : {
    2238         2829 :   if (fd >= 0)
    2239              :     {
    2240              :       /* Write the string table.  */
    2241         2720 :       unsigned strnam = name (".strtab");
    2242         2720 :       unsigned stroff = write (strtab);
    2243         2720 :       unsigned strndx = add (SHT_STRTAB, strnam, stroff, strtab.pos,
    2244              :                              SHF_STRINGS);
    2245              : 
    2246              :       /* Store escape values in section[0].  */
    2247         2720 :       if (strndx >= SHN_LORESERVE)
    2248              :         {
    2249            0 :           reinterpret_cast<section *> (sectab.buffer)->link = strndx;
    2250            0 :           strndx = SHN_XINDEX;
    2251              :         }
    2252         2720 :       unsigned shnum = sectab.pos / sizeof (section);
    2253         2720 :       if (shnum >= SHN_LORESERVE)
    2254              :         {
    2255            0 :           reinterpret_cast<section *> (sectab.buffer)->size = shnum;
    2256            0 :           shnum = SHN_XINDEX;
    2257              :         }
    2258              : 
    2259         2720 :       unsigned shoff = write (sectab);
    2260              : 
    2261              : #if MAPPED_WRITING
    2262         2720 :       if (offset)
    2263              :         {
    2264          138 :           remove_mapping ();
    2265          138 :           offset = 0;
    2266          138 :           create_mapping ((sizeof (header) + page_size - 1) & ~(page_size - 1),
    2267              :                           false);
    2268              :         }
    2269         2720 :       unsigned length = pos;
    2270              : #else
    2271              :       if (lseek (fd, 0, SEEK_SET) < 0)
    2272              :         set_error (errno);
    2273              : #endif
    2274              :       /* Write header.  */
    2275         2720 :       if (!get_error ())
    2276              :         {
    2277              :           /* Write the correct header now.  */
    2278         2720 :           header *h = reinterpret_cast<header *> (hdr.buffer);
    2279         2720 :           h->ident.magic[0] = 0x7f;
    2280         2720 :           h->ident.magic[1] = 'E';   /* Elrond */
    2281         2720 :           h->ident.magic[2] = 'L';   /* is an */
    2282         2720 :           h->ident.magic[3] = 'F';   /* elf.  */
    2283         2720 :           h->ident.klass = MY_CLASS;
    2284         2720 :           h->ident.data =  MY_ENDIAN;
    2285         2720 :           h->ident.version = EV_CURRENT;
    2286         2720 :           h->ident.osabi = OSABI_NONE;
    2287         2720 :           h->type = ET_NONE;
    2288         2720 :           h->machine = EM_NONE;
    2289         2720 :           h->version = EV_CURRENT;
    2290         2720 :           h->shoff = shoff;
    2291         2720 :           h->ehsize = sizeof (header);
    2292         2720 :           h->shentsize = sizeof (section);
    2293         2720 :           h->shnum = shnum;
    2294         2720 :           h->shstrndx = strndx;
    2295              : 
    2296         2720 :           pos = 0;
    2297         2720 :           write (hdr);
    2298              :         }
    2299              : 
    2300              : #if MAPPED_WRITING
    2301         2720 :       remove_mapping ();
    2302         2720 :       if (ftruncate (fd, length))
    2303            0 :         set_error (errno);
    2304              : #endif
    2305              :     }
    2306              : 
    2307         2829 :   data::simple_memory.shrink (sectab);
    2308         2829 :   data::simple_memory.shrink (strtab);
    2309              : 
    2310         2829 :   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      4093807 :   template<unsigned I> void set_flag_bit ()
    2417              :   {
    2418            0 :     gcc_checking_assert (I < 2 || !is_binding ());
    2419      4093807 :     discriminator |= 1u << I;
    2420      2497615 :   }
    2421      4346752 :   template<unsigned I> void clear_flag_bit ()
    2422              :   {
    2423            0 :     gcc_checking_assert (I < 2 || !is_binding ());
    2424      4346752 :     discriminator &= ~(1u << I);
    2425      4346752 :   }
    2426    416102823 :   template<unsigned I> bool get_flag_bit () const
    2427              :   {
    2428            0 :     gcc_checking_assert (I < 2 || !is_binding ());
    2429    505725646 :     return bool ((discriminator >> I) & 1);
    2430              :   }
    2431              : 
    2432              : public:
    2433    407778279 :   bool is_binding () const
    2434              :   {
    2435     96467190 :     return !get_flag_bit<DB_ZERO_BIT> ();
    2436              :   }
    2437    215653861 :   entity_kind get_entity_kind () const
    2438              :   {
    2439        21254 :     if (is_binding ())
    2440              :       return EK_BINDING;
    2441    161656847 :     return entity_kind ((discriminator >> DB_KIND_BIT) & ((1u << EK_BITS) - 1));
    2442              :   }
    2443              :   const char *entity_kind_name () const;
    2444              : 
    2445              : public:
    2446      7108542 :   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      7108542 :     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      1751652 :   bool is_pending_entity () const
    2457              :   {
    2458      2809650 :     return (get_entity_kind () == EK_SPECIALIZATION
    2459      1057998 :             || get_entity_kind () == EK_PARTIAL
    2460      2779804 :             || (get_entity_kind () == EK_DECL
    2461      1002295 :                 && 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     35755137 :   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     15112408 :     tree inner = STRIP_TEMPLATE (get_entity ());
    2478     35755137 :     return (get_flag_bit<DB_TU_LOCAL_BIT> ()
    2479     35755137 :             && (strict
    2480         3173 :                 || !VAR_OR_FUNCTION_DECL_P (inner)
    2481         2043 :                 || !NAMESPACE_SCOPE_P (inner)
    2482         2016 :                 || (DECL_LANG_SPECIFIC (inner)
    2483         2016 :                     && DECL_MODULE_PURVIEW_P (inner))));
    2484              :   }
    2485       835784 :   bool refs_tu_local (bool strict = false) const
    2486              :   {
    2487       835784 :     return (get_flag_bit<DB_REF_PURVIEW_BIT> ()
    2488       835784 :             || (strict && get_flag_bit <DB_REF_GLOBAL_BIT> ()));
    2489              :   }
    2490      2613324 :   bool is_exposure (bool strict = false) const
    2491              :   {
    2492      2613324 :     return (get_flag_bit<DB_EXPOSE_PURVIEW_BIT> ()
    2493      2613324 :             || (strict && get_flag_bit <DB_EXPOSE_GLOBAL_BIT> ()));
    2494              :   }
    2495      1725795 :   bool is_ignored_exposure_context () const
    2496              :   {
    2497      1725795 :     return get_flag_bit<DB_IGNORED_EXPOSURE_BIT> ();
    2498              :   }
    2499              : 
    2500              : public:
    2501     21951948 :   bool is_import () const
    2502              :   {
    2503      6691186 :     return get_flag_bit<DB_IMPORTED_BIT> ();
    2504              :   }
    2505     13375233 :   bool is_unreached () const
    2506              :   {
    2507       883795 :     return get_flag_bit<DB_UNREACHED_BIT> ();
    2508              :   }
    2509      1039905 :   bool is_hidden () const
    2510              :   {
    2511      1039905 :     return get_flag_bit<DB_HIDDEN_BIT> ();
    2512              :   }
    2513       741719 :   bool is_maybe_recursive () const
    2514              :   {
    2515       741719 :     return get_flag_bit<DB_MAYBE_RECURSIVE_BIT> ();
    2516              :   }
    2517         1090 :   bool is_entry () const
    2518              :   {
    2519         1090 :     return get_flag_bit<DB_ENTRY_BIT> ();
    2520              :   }
    2521      1040403 :   bool is_type_spec () const
    2522              :   {
    2523      1040403 :     return get_flag_bit<DB_TYPE_SPEC_BIT> ();
    2524              :   }
    2525      1040403 :   bool is_friend_spec () const
    2526              :   {
    2527      1040403 :     return get_flag_bit<DB_FRIEND_SPEC_BIT> ();
    2528              :   }
    2529              : 
    2530              : public:
    2531              :   /* We set these bit outside of depset.  */
    2532           88 :   void set_hidden_binding ()
    2533              :   {
    2534           88 :     set_flag_bit<DB_HIDDEN_BIT> ();
    2535           88 :   }
    2536           31 :   void clear_hidden_binding ()
    2537              :   {
    2538           31 :     clear_flag_bit<DB_HIDDEN_BIT> ();
    2539           31 :   }
    2540              : 
    2541              : public:
    2542      8324544 :   bool is_special () const
    2543              :   {
    2544      8324544 :     return get_flag_bit<DB_SPECIAL_BIT> ();
    2545              :   }
    2546      1596192 :   void set_special ()
    2547              :   {
    2548      1596192 :     set_flag_bit<DB_SPECIAL_BIT> ();
    2549            0 :   }
    2550              : 
    2551              : public:
    2552    138563310 :   tree get_entity () const
    2553              :   {
    2554     35755137 :     return entity;
    2555              :   }
    2556     18364754 :   tree get_name () const
    2557              :   {
    2558     18364754 :     gcc_checking_assert (is_binding ());
    2559     18364754 :     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     13682853 :     inline static hashval_t hash (const compare_type &p)
    2574              :     {
    2575     13682853 :       hashval_t h = pointer_hash<tree_node>::hash (p.first);
    2576     13682853 :       if (p.second)
    2577              :         {
    2578       171197 :           hashval_t nh = IDENTIFIER_HASH_VALUE (p.second);
    2579       171197 :           h = iterative_hash_hashval_t (h, nh);
    2580              :         }
    2581     13682853 :       return h;
    2582              :     }
    2583     69358567 :     inline static bool equal (const value_type b, const compare_type &p)
    2584              :     {
    2585     69358567 :       if (b->entity != p.first)
    2586              :         return false;
    2587              : 
    2588      9796541 :       if (p.second)
    2589        23505 :         return b->discriminator == reinterpret_cast<uintptr_t> (p.second);
    2590              :       else
    2591      9773036 :         return !b->is_binding ();
    2592              :     }
    2593              : 
    2594              :     /* (re)hasher for a binding itself.  */
    2595     53096358 :     inline static hashval_t hash (const value_type b)
    2596              :     {
    2597     53096358 :       hashval_t h = pointer_hash<tree_node>::hash (b->entity);
    2598     53096358 :       if (b->is_binding ())
    2599              :         {
    2600      4876900 :           hashval_t nh = IDENTIFIER_HASH_VALUE (b->get_name ());
    2601      4876900 :           h = iterative_hash_hashval_t (h, nh);
    2602              :         }
    2603     53096358 :       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      2304098 :     static void remove (value_type p)
    2616              :     {
    2617      2304098 :       delete (p);
    2618      2304098 :     }
    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      5799332 :     struct dep_adl_info {
    2643              :       /* The name of the call or operator.  */
    2644              :       tree name = NULL_TREE;
    2645              :       /* If not ERROR_MARK, a rewrite candidate for this operator.  */
    2646              :       tree_code rewrite = ERROR_MARK;
    2647              :       /* Argument list for the call.  */
    2648              :       vec<tree, va_gc>* args = make_tree_vector ();
    2649              :     };
    2650              :     vec<dep_adl_info> dep_adl_entity_list;
    2651              : 
    2652              :   public:
    2653       247628 :     hash (size_t size, hash *c = NULL)
    2654       495256 :       : parent (size), chain (c), current (NULL), section (0),
    2655       247628 :         reached_unreached (false), writing_merge_key (false),
    2656       247628 :         ignore_exposure (false)
    2657              :     {
    2658       247628 :       worklist.create (size);
    2659       247628 :       dep_adl_entity_list.create (16);
    2660       247628 :     }
    2661       247628 :     ~hash ()
    2662              :     {
    2663       247628 :       worklist.release ();
    2664       247628 :       dep_adl_entity_list.release ();
    2665       247628 :     }
    2666              : 
    2667              :   public:
    2668     98685525 :     bool is_key_order () const
    2669              :     {
    2670     98685525 :       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       662215 :     temp_override<bool> ignore_exposure_if (bool cond)
    2678              :     {
    2679       514315 :       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       247599 :     tarjan (unsigned size)
    2728       247599 :       : index (0)
    2729              :     {
    2730       247599 :       result.create (size);
    2731       247599 :       stack.create (50);
    2732       247599 :     }
    2733       247599 :     ~tarjan ()
    2734              :     {
    2735       247599 :       gcc_assert (!stack.length ());
    2736       247599 :       stack.release ();
    2737       247599 :     }
    2738              : 
    2739              :   public:
    2740              :     void connect (depset *);
    2741              :   };
    2742              : };
    2743              : 
    2744              : inline
    2745      2304098 : depset::depset (tree entity)
    2746      2304098 :   :entity (entity), discriminator (0), cluster (0), section (0)
    2747              : {
    2748      2304098 :   deps.create (0);
    2749              : }
    2750              : 
    2751              : inline
    2752      2304098 : depset::~depset ()
    2753              : {
    2754      2304098 :   deps.release ();
    2755      2304098 : }
    2756              : 
    2757              : const char *
    2758        44180 : depset::entity_kind_name () const
    2759              : {
    2760              :   /* Same order as entity_kind.  */
    2761        44180 :   static const char *const names[] =
    2762              :     {"decl", "specialization", "partial", "using",
    2763              :      "namespace", "tu-local", "redirect", "binding"};
    2764        44180 :   static_assert (ARRAY_SIZE (names) == EK_EXPLICIT_HWM + 1,
    2765              :                  "names must have an entry for every explicit entity_kind");
    2766        44180 :   entity_kind kind = get_entity_kind ();
    2767        44180 :   gcc_checking_assert (kind < ARRAY_SIZE (names));
    2768        44180 :   return names[kind];
    2769              : }
    2770              : 
    2771              : /* Create a depset for a namespace binding NS::NAME.  */
    2772              : 
    2773       134530 : depset *depset::make_binding (tree ns, tree name)
    2774              : {
    2775       134530 :   depset *binding = new depset (ns);
    2776              : 
    2777       134530 :   binding->discriminator = reinterpret_cast <uintptr_t> (name);
    2778              : 
    2779       134530 :   return binding;
    2780              : }
    2781              : 
    2782      2169568 : depset *depset::make_entity (tree entity, entity_kind ek, bool is_defn)
    2783              : {
    2784      2169568 :   depset *r = new depset (entity);
    2785              : 
    2786      2169568 :   r->discriminator = ((1 << DB_ZERO_BIT)
    2787      2169568 :                       | (ek << DB_KIND_BIT)
    2788      2169568 :                       | is_defn << DB_DEFN_BIT);
    2789              : 
    2790      2169568 :   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     39055192 :   static hashval_t hash (const value_type &k)
    2807              :   {
    2808     39055192 :     hashval_t h = IDENTIFIER_HASH_VALUE (k.id);
    2809     39055192 :     h = iterative_hash_hashval_t (DECL_UID (k.ns), h);
    2810              : 
    2811     39055192 :     return h;
    2812              :   }
    2813     13720411 :   static bool equal (const value_type &k, const value_type &l)
    2814              :   {
    2815     13720411 :     return k.ns == l.ns && k.id == l.id;
    2816              :   }
    2817       208475 :   static void mark_empty (value_type &k)
    2818              :   {
    2819       208475 :     k.ns = k.id = NULL_TREE;
    2820              :   }
    2821         6009 :   static void mark_deleted (value_type &k)
    2822              :   {
    2823         6009 :     k.ns = NULL_TREE;
    2824         6009 :     gcc_checking_assert (k.id);
    2825         6009 :   }
    2826    284844990 :   static bool is_empty (const value_type &k)
    2827              :   {
    2828    284798065 :     return k.ns == NULL_TREE && k.id == NULL_TREE;
    2829              :   }
    2830     18610945 :   static bool is_deleted (const value_type &k)
    2831              :   {
    2832     18610945 :     return k.ns == NULL_TREE && k.id != NULL_TREE;
    2833              :   }
    2834              :   static void remove (value_type &)
    2835              :   {
    2836              :   }
    2837              : };
    2838              : 
    2839              : typedef hash_map<pending_key, auto_vec<unsigned>> pending_map_t;
    2840              : 
    2841              : /* Not-loaded entities that are keyed to a namespace-scope
    2842              :    identifier.  See module_state::write_pendings for details.  */
    2843              : pending_map_t *pending_table;
    2844              : 
    2845              : /* Decls that need some post processing once a batch of lazy loads has
    2846              :    completed.  */
    2847              : vec<tree, va_heap, vl_embed> *post_load_decls;
    2848              : 
    2849              : /* Some entities are keyed to another entitity for ODR purposes.
    2850              :    For example, at namespace scope, 'inline auto var = []{};', that
    2851              :    lambda is keyed to 'var', and follows its ODRness.  */
    2852              : typedef hash_map<tree, auto_vec<tree>> keyed_map_t;
    2853              : static keyed_map_t *keyed_table;
    2854              : 
    2855              : static tree get_keyed_decl_scope (tree);
    2856              : 
    2857              : /* Instantiations of temploid friends imported from another module
    2858              :    need to be attached to the same module as the temploid.  This maps
    2859              :    these decls to the temploid they are instantiated from, as there is
    2860              :    no other easy way to get this information.  */
    2861              : static GTY((cache)) decl_tree_cache_map *imported_temploid_friends;
    2862              : 
    2863              : /********************************************************************/
    2864              : /* Tree streaming.   The tree streaming is very specific to the tree
    2865              :    structures themselves.  A tag indicates the kind of tree being
    2866              :    streamed.  -ve tags indicate backreferences to already-streamed
    2867              :    trees.  Backreferences are auto-numbered.  */
    2868              : 
    2869              : /* Tree tags.  */
    2870              : enum tree_tag {
    2871              :   tt_null,              /* NULL_TREE.  */
    2872              :   tt_tu_local,          /* A TU-local entity.  */
    2873              :   tt_fixed,             /* Fixed vector index.  */
    2874              : 
    2875              :   tt_node,              /* By-value node.  */
    2876              :   tt_decl,              /* By-value mergeable decl.  */
    2877              :   tt_tpl_parm,          /* Template parm.  */
    2878              : 
    2879              :   /* The ordering of the following 5 is relied upon in
    2880              :      trees_out::tree_node.  */
    2881              :   tt_id,                /* Identifier node.  */
    2882              :   tt_conv_id,           /* Conversion operator name.  */
    2883              :   tt_anon_id,           /* Anonymous name.  */
    2884              :   tt_lambda_id,         /* Lambda name.  */
    2885              :   tt_internal_id,       /* Internal name.  */
    2886              : 
    2887              :   tt_typedef_type,      /* A (possibly implicit) typedefed type.  */
    2888              :   tt_derived_type,      /* A type derived from another type.  */
    2889              :   tt_variant_type,      /* A variant of another type.  */
    2890              : 
    2891              :   tt_tinfo_var,         /* Typeinfo object. */
    2892              :   tt_tinfo_typedef,     /* Typeinfo typedef.  */
    2893              :   tt_ptrmem_type,       /* Pointer to member type.  */
    2894              :   tt_nttp_var,          /* NTTP_OBJECT VAR_DECL.  */
    2895              : 
    2896              :   tt_parm,              /* Function parameter or result.  */
    2897              :   tt_enum_value,        /* An enum value.  */
    2898              :   tt_enum_decl,         /* An enum decl.  */
    2899              :   tt_data_member,       /* Data member/using-decl.  */
    2900              : 
    2901              :   tt_binfo,             /* A BINFO.  */
    2902              :   tt_vtable,            /* A vtable.  */
    2903              :   tt_thunk,             /* A thunk.  */
    2904              :   tt_clone_ref,
    2905              : 
    2906              :   tt_entity,            /* A extra-cluster entity.  */
    2907              : 
    2908              :   tt_template,          /* The TEMPLATE_RESULT of a template.  */
    2909              : };
    2910              : 
    2911              : enum walk_kind {
    2912              :   WK_none,      /* No walk to do (a back- or fixed-ref happened).  */
    2913              :   WK_normal,    /* Normal walk (by-name if possible).  */
    2914              : 
    2915              :   WK_value,     /* By-value walk.  */
    2916              : };
    2917              : 
    2918              : enum merge_kind
    2919              : {
    2920              :   MK_unique,    /* Known unique.  */
    2921              :   MK_named,     /* Found by CTX, NAME + maybe_arg types etc.  */
    2922              :   MK_field,     /* Found by CTX and index on TYPE_FIELDS  */
    2923              :   MK_vtable,    /* Found by CTX and index on TYPE_VTABLES  */
    2924              :   MK_as_base,   /* Found by CTX.  */
    2925              : 
    2926              :   MK_partial,
    2927              : 
    2928              :   MK_enum,      /* Found by CTX, & 1stMemberNAME.  */
    2929              :   MK_keyed,     /* Found by key & index.  */
    2930              :   MK_local_type, /* Found by CTX, index.  */
    2931              : 
    2932              :   MK_friend_spec,  /* Like named, but has a tmpl & args too.  */
    2933              :   MK_local_friend, /* Found by CTX, index.  */
    2934              : 
    2935              :   MK_indirect_lwm = MK_enum,
    2936              : 
    2937              :   /* Template specialization kinds below. These are all found via
    2938              :      primary template and specialization args.  */
    2939              :   MK_template_mask = 0x10,  /* A template specialization.  */
    2940              : 
    2941              :   MK_tmpl_decl_mask = 0x4, /* In decl table.  */
    2942              : 
    2943              :   MK_tmpl_tmpl_mask = 0x1, /* We want TEMPLATE_DECL.  */
    2944              : 
    2945              :   MK_type_spec = MK_template_mask,
    2946              :   MK_decl_spec = MK_template_mask | MK_tmpl_decl_mask,
    2947              : 
    2948              :   MK_hwm = 0x20
    2949              : };
    2950              : /* This is more than a debugging array.  NULLs are used to determine
    2951              :    an invalid merge_kind number.  */
    2952              : static char const *const merge_kind_name[MK_hwm] =
    2953              :   {
    2954              :     "unique", "named", "field", "vtable",       /* 0...3  */
    2955              :     "asbase", "partial", "enum", "attached",    /* 4...7  */
    2956              : 
    2957              :     "local type", "friend spec", "local friend", NULL,  /* 8...11 */
    2958              :     NULL, NULL, NULL, NULL,
    2959              : 
    2960              :     "type spec", "type tmpl spec",  /* 16,17 type (template).  */
    2961              :     NULL, NULL,
    2962              : 
    2963              :     "decl spec", "decl tmpl spec",  /* 20,21 decl (template).  */
    2964              :     NULL, NULL,
    2965              :     NULL, NULL, NULL, NULL,
    2966              :     NULL, NULL, NULL, NULL,
    2967              :   };
    2968              : 
    2969              : /* Mergeable entity location data.  */
    2970              : struct merge_key {
    2971              :   cp_ref_qualifier ref_q : 2;
    2972              :   unsigned coro_disc : 2;  /* Discriminator for coroutine transforms.  */
    2973              :   unsigned index;
    2974              : 
    2975              :   tree ret;  /* Return type, if appropriate.  */
    2976              :   tree args; /* Arg types, if appropriate.  */
    2977              : 
    2978              :   tree constraints;  /* Constraints.  */
    2979              : 
    2980      2364783 :   merge_key ()
    2981      2364783 :     :ref_q (REF_QUAL_NONE), coro_disc (0), index (0),
    2982      2364783 :      ret (NULL_TREE), args (NULL_TREE),
    2983      2364783 :      constraints (NULL_TREE)
    2984              :   {
    2985              :   }
    2986              : };
    2987              : 
    2988              : /* Hashmap of merged duplicates.  Usually decls, but can contain
    2989              :    BINFOs.  */
    2990              : typedef hash_map<tree,uintptr_t,
    2991              :                  simple_hashmap_traits<nodel_ptr_hash<tree_node>,uintptr_t> >
    2992              : duplicate_hash_map;
    2993              : 
    2994              : /* Data needed for post-processing.  */
    2995              : struct post_process_data {
    2996              :   tree decl;
    2997              :   location_t start_locus;
    2998              :   location_t end_locus;
    2999              :   bool returns_value;
    3000              :   bool returns_null;
    3001              :   bool returns_abnormally;
    3002              :   bool infinite_loop;
    3003              : };
    3004              : 
    3005              : /* Tree stream reader.  Note that reading a stream doesn't mark the
    3006              :    read trees with TREE_VISITED.  Thus it's quite safe to have
    3007              :    multiple concurrent readers.  Which is good, because lazy
    3008              :    loading.
    3009              : 
    3010              :    It's important that trees_in/out have internal linkage so that the
    3011              :    compiler knows core_bools, lang_type_bools and lang_decl_bools have
    3012              :    only a single caller (tree_node_bools) and inlines them appropriately.  */
    3013              : namespace {
    3014              : class trees_in : public bytes_in {
    3015              :   typedef bytes_in parent;
    3016              : 
    3017              : private:
    3018              :   module_state *state;          /* Module being imported.  */
    3019              :   vec<tree> back_refs;            /* Back references.  */
    3020              :   duplicate_hash_map *duplicates;       /* Map from existings to duplicate.  */
    3021              :   vec<post_process_data> post_decls;      /* Decls to post process.  */
    3022              :   vec<tree> post_types;           /* Types to post process.  */
    3023              :   unsigned unused;              /* Inhibit any interior TREE_USED
    3024              :                                    marking.  */
    3025              : 
    3026              : public:
    3027              :   trees_in (module_state *);
    3028              :   ~trees_in ();
    3029              : 
    3030              : public:
    3031              :   int insert (tree);
    3032              :   tree back_ref (int);
    3033              : 
    3034              : private:
    3035              :   tree start (unsigned = 0);
    3036              : 
    3037              : public:
    3038              :   /* Needed for binfo writing  */
    3039              :   bool core_bools (tree, bits_in&);
    3040              : 
    3041              : private:
    3042              :   /* Stream tree_core, lang_decl_specific and lang_type_specific
    3043              :      bits.  */
    3044              :   bool core_vals (tree);
    3045              :   bool lang_type_bools (tree, bits_in&);
    3046              :   bool lang_type_vals (tree);
    3047              :   bool lang_decl_bools (tree, bits_in&);
    3048              :   bool lang_decl_vals (tree);
    3049              :   bool lang_vals (tree);
    3050              :   bool tree_node_bools (tree);
    3051              :   bool tree_node_vals (tree);
    3052              :   tree tree_value ();
    3053              :   tree decl_value ();
    3054              :   tree tpl_parm_value ();
    3055              : 
    3056              : private:
    3057              :   tree chained_decls ();  /* Follow DECL_CHAIN.  */
    3058              :   vec<tree, va_heap> *vec_chained_decls ();
    3059              :   vec<tree, va_gc> *tree_vec (); /* vec of tree.  */
    3060              :   vec<tree_pair_s, va_gc> *tree_pair_vec (); /* vec of tree_pair.  */
    3061              :   tree tree_list (bool has_purpose);
    3062              : 
    3063              : public:
    3064              :   /* Read a tree node.  */
    3065              :   tree tree_node (bool is_use = false);
    3066              : 
    3067              : private:
    3068              :   bool install_entity (tree decl);
    3069              :   tree tpl_parms (unsigned &tpl_levels);
    3070              :   bool tpl_parms_fini (tree decl, unsigned tpl_levels);
    3071              :   bool tpl_header (tree decl, unsigned *tpl_levels);
    3072              :   int fn_parms_init (tree);
    3073              :   void fn_parms_fini (int tag, tree fn, tree existing, bool has_defn);
    3074              :   unsigned add_indirect_tpl_parms (tree);
    3075              : public:
    3076              :   bool add_indirects (tree);
    3077              : 
    3078              : public:
    3079              :   /* Serialize various definitions. */
    3080              :   bool read_definition (tree decl);
    3081              : 
    3082              : private:
    3083              :   void check_abi_tags (tree existing, tree decl, tree &eattr, tree &dattr);
    3084              :   bool is_matching_decl (tree existing, tree decl, bool is_typedef);
    3085              :   static bool install_implicit_member (tree decl);
    3086              :   bool read_function_def (tree decl, tree maybe_template);
    3087              :   bool read_var_def (tree decl, tree maybe_template);
    3088              :   bool read_class_def (tree decl, tree maybe_template);
    3089              :   bool read_enum_def (tree decl, tree maybe_template);
    3090              : 
    3091              : public:
    3092              :   tree decl_container ();
    3093              :   tree key_mergeable (int tag, merge_kind, tree decl, tree inner, tree type,
    3094              :                       tree container, bool is_attached,
    3095              :                       bool is_imported_temploid_friend);
    3096              :   unsigned binfo_mergeable (tree *);
    3097              : 
    3098              : private:
    3099              :   tree key_local_type (const merge_key&, tree, tree);
    3100              :   uintptr_t *find_duplicate (tree existing);
    3101              :   void register_duplicate (tree decl, tree existing);
    3102              :   /* Mark as an already diagnosed bad duplicate.  */
    3103           54 :   void unmatched_duplicate (tree existing)
    3104              :   {
    3105          108 :     *find_duplicate (existing) |= 1;
    3106           54 :   }
    3107              : 
    3108              : public:
    3109       134534 :   bool is_duplicate (tree decl)
    3110              :   {
    3111       269068 :     return find_duplicate (decl) != NULL;
    3112              :   }
    3113       148618 :   tree maybe_duplicate (tree decl)
    3114              :   {
    3115       148618 :     if (uintptr_t *dup = find_duplicate (decl))
    3116         8897 :       return reinterpret_cast<tree> (*dup & ~uintptr_t (1));
    3117              :     return decl;
    3118              :   }
    3119              :   tree odr_duplicate (tree decl, bool has_defn);
    3120              : 
    3121              : public:
    3122              :   /* Return the decls to postprocess.  */
    3123              :   const vec<post_process_data>& post_process ()
    3124              :   {
    3125              :     return post_decls;
    3126              :   }
    3127              :   /* Return the types to postprocess.  */
    3128              :   const vec<tree>& post_process_type ()
    3129              :   {
    3130              :     return post_types;
    3131              :   }
    3132              : private:
    3133              :   /* Register DATA for postprocessing.  */
    3134       119690 :   void post_process (post_process_data data)
    3135              :   {
    3136       119690 :     post_decls.safe_push (data);
    3137              :   }
    3138              :   /* Register TYPE for postprocessing.  */
    3139           32 :   void post_process_type (tree type)
    3140              :   {
    3141           32 :     gcc_checking_assert (TYPE_P (type));
    3142           32 :     post_types.safe_push (type);
    3143           32 :   }
    3144              : 
    3145              : private:
    3146              :   void assert_definition (tree, bool installing);
    3147              : };
    3148              : } // anon namespace
    3149              : 
    3150       202565 : trees_in::trees_in (module_state *state)
    3151       202565 :   :parent (), state (state), unused (0)
    3152              : {
    3153       202565 :   duplicates = NULL;
    3154       202565 :   back_refs.create (500);
    3155       202565 :   post_decls.create (0);
    3156       202565 :   post_types.create (0);
    3157       202565 : }
    3158              : 
    3159       202565 : trees_in::~trees_in ()
    3160              : {
    3161       303927 :   delete (duplicates);
    3162       202565 :   back_refs.release ();
    3163       202565 :   post_decls.release ();
    3164       202565 :   post_types.release ();
    3165       202565 : }
    3166              : 
    3167              : /* Tree stream writer.  */
    3168              : namespace {
    3169              : class trees_out : public bytes_out {
    3170              :   typedef bytes_out parent;
    3171              : 
    3172              : private:
    3173              :   module_state *state;          /* The module we are writing.  */
    3174              :   ptr_int_hash_map tree_map;    /* Trees to references */
    3175              :   depset::hash *dep_hash;       /* Dependency table.  */
    3176              :   int ref_num;                  /* Back reference number.  */
    3177              :   unsigned section;
    3178              :   bool writing_local_entities;  /* Whether we might walk into a TU-local
    3179              :                                    entity we need to emit placeholders for.  */
    3180              :   bool walking_bit_field_unit;  /* Whether we're walking the underlying
    3181              :                                    storage for a bit field.  There's no other
    3182              :                                    great way to detect this.  */
    3183              : #if CHECKING_P
    3184              :   int importedness;             /* Checker that imports not occurring
    3185              :                                    inappropriately.  +ve imports ok,
    3186              :                                    -ve imports not ok.  */
    3187              : #endif
    3188              : 
    3189              : public:
    3190              :   trees_out (allocator *, module_state *, depset::hash &deps, unsigned sec = 0);
    3191              :   ~trees_out ();
    3192              : 
    3193              : private:
    3194              :   void mark_trees ();
    3195              :   void unmark_trees ();
    3196              : 
    3197              : public:
    3198              :   /* Hey, let's ignore the well known STL iterator idiom.  */
    3199              :   void begin ();
    3200              :   unsigned end (elf_out *sink, unsigned name, unsigned *crc_ptr);
    3201              :   void end ();
    3202              : 
    3203              : public:
    3204              :   enum tags
    3205              :   {
    3206              :     tag_backref = -1,   /* Upper bound on the backrefs.  */
    3207              :     tag_value = 0,      /* Write by value.  */
    3208              :     tag_fixed           /* Lower bound on the fixed trees.  */
    3209              :   };
    3210              : 
    3211              : public:
    3212              :   /* The walk is used for three similar purposes:
    3213              : 
    3214              :       1. The initial scan for dependencies.
    3215              :       2. Once dependencies have been found, ordering them.
    3216              :       3. Writing dependencies to file (streaming_p).
    3217              : 
    3218              :      For cases where it matters, these accessers can be used to determine
    3219              :      which state we're in.  */
    3220     80270998 :   bool is_initial_scan () const
    3221              :   {
    3222    133151148 :     return !streaming_p () && !is_key_order ();
    3223              :   }
    3224     65911936 :   bool is_key_order () const
    3225              :   {
    3226     52880150 :     return dep_hash->is_key_order ();
    3227              :   }
    3228              : 
    3229              : public:
    3230              :   int insert (tree, walk_kind = WK_normal);
    3231              : 
    3232              : private:
    3233              :   void start (tree, bool = false);
    3234              : 
    3235              : private:
    3236              :   walk_kind ref_node (tree);
    3237              : public:
    3238              :   int get_tag (tree);
    3239       489828 :   void set_importing (int i ATTRIBUTE_UNUSED)
    3240              :   {
    3241              : #if CHECKING_P
    3242       489828 :     importedness = i;
    3243              : #endif
    3244              :   }
    3245              : 
    3246              : private:
    3247              :   void core_bools (tree, bits_out&);
    3248              :   void core_vals (tree);
    3249              :   void lang_type_bools (tree, bits_out&);
    3250              :   void lang_type_vals (tree);
    3251              :   void lang_decl_bools (tree, bits_out&);
    3252              :   void lang_decl_vals (tree);
    3253              :   void lang_vals (tree);
    3254              :   void tree_node_bools (tree);
    3255              :   void tree_node_vals (tree);
    3256              : 
    3257              : private:
    3258              :   void chained_decls (tree);
    3259              :   void vec_chained_decls (tree);
    3260              :   void tree_vec (vec<tree, va_gc> *);
    3261              :   void tree_pair_vec (vec<tree_pair_s, va_gc> *);
    3262              :   void tree_list (tree, bool has_purpose);
    3263              : 
    3264              : private:
    3265              :   bool has_tu_local_dep (tree) const;
    3266              :   tree find_tu_local_decl (tree);
    3267              : 
    3268              : public:
    3269              :   /* Mark a node for by-value walking.  */
    3270              :   void mark_by_value (tree);
    3271              : 
    3272              : public:
    3273              :   void tree_node (tree);
    3274              : 
    3275              : private:
    3276              :   void install_entity (tree decl, depset *);
    3277              :   void tpl_parms (tree parms, unsigned &tpl_levels);
    3278              :   void tpl_parms_fini (tree decl, unsigned tpl_levels);
    3279              :   void fn_parms_fini (tree) {}
    3280              :   unsigned add_indirect_tpl_parms (tree);
    3281              : public:
    3282              :   void add_indirects (tree);
    3283              :   void fn_parms_init (tree);
    3284              :   void tpl_header (tree decl, unsigned *tpl_levels);
    3285              : 
    3286              : public:
    3287              :   merge_kind get_merge_kind (tree decl, depset *maybe_dep);
    3288              :   tree decl_container (tree decl);
    3289              :   void key_mergeable (int tag, merge_kind, tree decl, tree inner,
    3290              :                       tree container, depset *maybe_dep);
    3291              :   void binfo_mergeable (tree binfo);
    3292              : 
    3293              : private:
    3294              :   void key_local_type (merge_key&, tree, tree);
    3295              :   bool decl_node (tree, walk_kind ref);
    3296              :   void type_node (tree);
    3297              :   void tree_value (tree);
    3298              :   void tpl_parm_value (tree);
    3299              : 
    3300              : public:
    3301              :   void decl_value (tree, depset *);
    3302              : 
    3303              : public:
    3304              :   /* Serialize various definitions. */
    3305              :   void write_definition (tree decl, bool refs_tu_local = false);
    3306              :   void mark_declaration (tree decl, bool do_defn);
    3307              : 
    3308              : private:
    3309              :   void mark_function_def (tree decl);
    3310              :   void mark_var_def (tree decl);
    3311              :   void mark_class_def (tree decl);
    3312              :   void mark_enum_def (tree decl);
    3313              :   void mark_class_member (tree decl, bool do_defn = true);
    3314              :   void mark_binfos (tree type);
    3315              : 
    3316              : private:
    3317              :   void write_var_def (tree decl);
    3318              :   void write_function_def (tree decl);
    3319              :   void write_class_def (tree decl);
    3320              :   void write_enum_def (tree decl);
    3321              : 
    3322              : private:
    3323              :   static void assert_definition (tree);
    3324              : 
    3325              : public:
    3326              :   static void instrument ();
    3327              : 
    3328              : private:
    3329              :   /* Tree instrumentation. */
    3330              :   static unsigned tree_val_count;
    3331              :   static unsigned decl_val_count;
    3332              :   static unsigned back_ref_count;
    3333              :   static unsigned tu_local_count;
    3334              :   static unsigned null_count;
    3335              : };
    3336              : } // anon namespace
    3337              : 
    3338              : /* Instrumentation counters.  */
    3339              : unsigned trees_out::tree_val_count;
    3340              : unsigned trees_out::decl_val_count;
    3341              : unsigned trees_out::back_ref_count;
    3342              : unsigned trees_out::tu_local_count;
    3343              : unsigned trees_out::null_count;
    3344              : 
    3345       495272 : trees_out::trees_out (allocator *mem, module_state *state, depset::hash &deps,
    3346       495272 :                       unsigned section)
    3347       990544 :   :parent (mem), state (state), tree_map (500),
    3348       495272 :    dep_hash (&deps), ref_num (0), section (section),
    3349       495272 :    writing_local_entities (false), walking_bit_field_unit (false)
    3350              : {
    3351              : #if CHECKING_P
    3352       495272 :   importedness = 0;
    3353              : #endif
    3354       495272 : }
    3355              : 
    3356       495272 : trees_out::~trees_out ()
    3357              : {
    3358       495272 : }
    3359              : 
    3360              : /********************************************************************/
    3361              : /* Location.  We're aware of the line-map concept and reproduce it
    3362              :    here.  Each imported module allocates a contiguous span of ordinary
    3363              :    maps, and of macro maps.  adhoc maps are serialized by contents,
    3364              :    not pre-allocated.   The scattered linemaps of a module are
    3365              :    coalesced when writing.  */
    3366              : 
    3367              : 
    3368              : /* I use half-open [first,second) ranges.  */
    3369              : typedef std::pair<line_map_uint_t,line_map_uint_t> range_t;
    3370              : 
    3371              : /* A range of locations.  */
    3372              : typedef std::pair<location_t,location_t> loc_range_t;
    3373              : 
    3374              : /* Spans of the line maps that are occupied by this TU.  I.e. not
    3375              :    within imports.  Only extended when in an interface unit.
    3376              :    Interval zero corresponds to the forced header linemap(s).  This
    3377              :    is a singleton object.  */
    3378              : 
    3379              : class loc_spans {
    3380              : public:
    3381              :   /* An interval of line maps.  The line maps here represent a contiguous
    3382              :      non-imported range.  */
    3383         5862 :   struct span {
    3384              :     loc_range_t ordinary;       /* Ordinary map location range. */
    3385              :     loc_range_t macro;          /* Macro map location range.  */
    3386              :     /* Add to locs to get serialized loc.  */
    3387              :     location_diff_t ordinary_delta;
    3388              :     location_diff_t macro_delta;
    3389              :   };
    3390              : 
    3391              : private:
    3392              :   vec<span> *spans;
    3393              :   bool locs_exhausted_p;
    3394              : 
    3395              : public:
    3396              :   loc_spans ()
    3397              :     /* Do not preallocate spans, as that causes
    3398              :        --enable-detailed-mem-stats problems.  */
    3399              :     : spans (nullptr), locs_exhausted_p (false)
    3400              :   {
    3401              :   }
    3402        98119 :   ~loc_spans ()
    3403              :   {
    3404        98119 :     delete spans;
    3405        98119 :   }
    3406              : 
    3407              : public:
    3408          297 :   span &operator[] (unsigned ix)
    3409              :   {
    3410          594 :     return (*spans)[ix];
    3411              :   }
    3412              :   unsigned length () const
    3413              :   {
    3414              :     return spans->length ();
    3415              :   }
    3416              : 
    3417              : public:
    3418        15345 :   bool init_p () const
    3419              :   {
    3420        15345 :     return spans != nullptr;
    3421              :   }
    3422              :   /* Initializer.  */
    3423              :   void init (const line_maps *lmaps, const line_map_ordinary *map);
    3424              : 
    3425              :   /* Slightly skewed preprocessed files can cause us to miss an
    3426              :      initialization in some places.  Fallback initializer.  */
    3427         5696 :   void maybe_init ()
    3428              :   {
    3429         5696 :     if (!init_p ())
    3430            6 :       init (line_table, nullptr);
    3431         5696 :   }
    3432              : 
    3433              : public:
    3434              :   enum {
    3435              :     SPAN_RESERVED = 0,  /* Reserved (fixed) locations.  */
    3436              :     SPAN_FIRST = 1,     /* LWM of locations to stream  */
    3437              :     SPAN_MAIN = 2       /* Main file and onwards.  */
    3438              :   };
    3439              : 
    3440              : public:
    3441       452762 :   location_t main_start () const
    3442              :   {
    3443       452762 :     return (*spans)[SPAN_MAIN].ordinary.first;
    3444              :   }
    3445              : 
    3446              : public:
    3447              :   void open (location_t);
    3448              :   void close ();
    3449              : 
    3450              : public:
    3451              :   /* Propagate imported linemaps to us, if needed.  */
    3452              :   bool maybe_propagate (module_state *import, location_t loc);
    3453              : 
    3454              : public:
    3455              :   /* Whether we can no longer represent new imported locations.  */
    3456            0 :   bool locations_exhausted_p () const
    3457              :   {
    3458            0 :     return locs_exhausted_p;
    3459              :   }
    3460            0 :   void report_location_exhaustion (location_t loc)
    3461              :   {
    3462            0 :     if (!locs_exhausted_p)
    3463              :       {
    3464              :         /* Just give the notice once.  */
    3465            0 :         locs_exhausted_p = true;
    3466            0 :         inform (loc, "unable to represent further imported source locations");
    3467              :       }
    3468              :   }
    3469              : 
    3470              : public:
    3471              :   const span *ordinary (location_t);
    3472              :   const span *macro (location_t);
    3473              : };
    3474              : 
    3475              : static loc_spans spans;
    3476              : 
    3477              : /* Information about ordinary locations we stream out.  */
    3478              : struct ord_loc_info
    3479              : {
    3480              :   const line_map_ordinary *src; // line map we're based on
    3481              :   line_map_uint_t offset;       // offset to this line
    3482              :   line_map_uint_t span;         // number of locs we span
    3483              :   line_map_uint_t remap;        // serialization
    3484              : 
    3485    219737571 :   static int compare (const void *a_, const void *b_)
    3486              :   {
    3487    219737571 :     auto *a = static_cast<const ord_loc_info *> (a_);
    3488    219737571 :     auto *b = static_cast<const ord_loc_info *> (b_);
    3489              : 
    3490    219737571 :     if (a->src != b->src)
    3491     49279667 :       return a->src < b->src ? -1 : +1;
    3492              : 
    3493              :     // Ensure no overlap
    3494    186428642 :     gcc_checking_assert (a->offset + a->span <= b->offset
    3495              :                          || b->offset + b->span <= a->offset);
    3496              : 
    3497    186428642 :     gcc_checking_assert (a->offset != b->offset);
    3498    186428642 :     return a->offset < b->offset ? -1 : +1;
    3499              :   }
    3500              : };
    3501              : struct ord_loc_traits
    3502              : {
    3503              :   typedef ord_loc_info value_type;
    3504              :   typedef value_type compare_type;
    3505              : 
    3506              :   static const bool empty_zero_p = false;
    3507              : 
    3508     91599162 :   static hashval_t hash (const value_type &v)
    3509              :   {
    3510     77781025 :     auto h = pointer_hash<const line_map_ordinary>::hash (v.src);
    3511     91599162 :     return iterative_hash_hashval_t (v.offset, h);
    3512              :   }
    3513    100758676 :   static bool equal (const value_type &v, const compare_type p)
    3514              :   {
    3515    100758676 :     return v.src == p.src && v.offset == p.offset;
    3516              :   }
    3517              : 
    3518      6648806 :   static void mark_empty (value_type &v)
    3519              :   {
    3520      6648806 :     v.src = nullptr;
    3521              :   }
    3522    293860363 :   static bool is_empty (value_type &v)
    3523              :   {
    3524    293860363 :     return !v.src;
    3525              :   }
    3526              : 
    3527              :   static bool is_deleted (value_type &) { return false; }
    3528              :   static void mark_deleted (value_type &) { gcc_unreachable (); }
    3529              : 
    3530      1656824 :   static void remove (value_type &) {}
    3531              : };
    3532              : /* Table keyed by ord_loc_info, used for noting.  */
    3533              : static  hash_table<ord_loc_traits> *ord_loc_table;
    3534              : /* Sorted vector, used for writing.  */
    3535              : static vec<ord_loc_info> *ord_loc_remap;
    3536              : 
    3537              : /* Information about macro locations we stream out.  */
    3538              : struct macro_loc_info
    3539              : {
    3540              :   const line_map_macro *src;    // original expansion
    3541              :   line_map_uint_t remap;        // serialization
    3542              : 
    3543      7854646 :   static int compare (const void *a_, const void *b_)
    3544              :   {
    3545      7854646 :     auto *a = static_cast<const macro_loc_info *> (a_);
    3546      7854646 :     auto *b = static_cast<const macro_loc_info *> (b_);
    3547              : 
    3548      7854646 :     gcc_checking_assert (MAP_START_LOCATION (a->src)
    3549              :                          != MAP_START_LOCATION (b->src));
    3550      7854646 :     if (MAP_START_LOCATION (a->src) < MAP_START_LOCATION (b->src))
    3551              :       return -1;
    3552              :     else
    3553      3778774 :       return +1;
    3554              :   }
    3555              : };
    3556              : struct macro_loc_traits
    3557              : {
    3558              :   typedef macro_loc_info value_type;
    3559              :   typedef const line_map_macro *compare_type;
    3560              : 
    3561              :   static const bool empty_zero_p = false;
    3562              : 
    3563      7781788 :   static hashval_t hash (compare_type p)
    3564              :   {
    3565      7781788 :     return pointer_hash<const line_map_macro>::hash (p);
    3566              :   }
    3567      6606373 :   static hashval_t hash (const value_type &v)
    3568              :   {
    3569      6606373 :     return hash (v.src);
    3570              :   }
    3571              :   static bool equal (const value_type &v, const compare_type p)
    3572              :   {
    3573              :     return v.src == p;
    3574              :   }
    3575              : 
    3576       618774 :   static void mark_empty (value_type &v)
    3577              :   {
    3578       618774 :     v.src = nullptr;
    3579              :   }
    3580     24529028 :   static bool is_empty (value_type &v)
    3581              :   {
    3582     24529028 :     return !v.src;
    3583              :   }
    3584              : 
    3585              :   static bool is_deleted (value_type &) { return false; }
    3586              :   static void mark_deleted (value_type &) { gcc_unreachable (); }
    3587              : 
    3588       134294 :   static void remove (value_type &) {}
    3589              : };
    3590              : /* Table keyed by line_map_macro, used for noting.  */
    3591              : static  hash_table<macro_loc_traits> *macro_loc_table;
    3592              : /* Sorted vector, used for writing.  */
    3593              : static vec<macro_loc_info> *macro_loc_remap;
    3594              : 
    3595              : /* Indirection to allow bsearching imports by ordinary location.  */
    3596              : static vec<module_state *> *ool;
    3597              : 
    3598              : /********************************************************************/
    3599              : /* Data needed by a module during the process of loading.  */
    3600              : struct GTY(()) slurping {
    3601              : 
    3602              :   /* Remap import's module numbering to our numbering.  Values are
    3603              :      shifted by 1.  Bit0 encodes if the import is direct.  */
    3604              :   vec<unsigned, va_heap, vl_embed> *
    3605              :     GTY((skip)) remap;                  /* Module owner remapping.  */
    3606              : 
    3607              :   elf_in *GTY((skip)) from;             /* The elf loader.  */
    3608              : 
    3609              :   /* This map is only for header imports themselves -- the global
    3610              :      headers bitmap hold it for the current TU.  */
    3611              :   bitmap headers;       /* Transitive set of direct imports, including
    3612              :                            self.  Used for macro visibility and
    3613              :                            priority.  */
    3614              : 
    3615              :   /* These objects point into the mmapped area, unless we're not doing
    3616              :      that, or we got frozen or closed.  In those cases they point to
    3617              :      buffers we own.  */
    3618              :   bytes_in macro_defs;  /* Macro definitions.  */
    3619              :   bytes_in macro_tbl;   /* Macro table.  */
    3620              : 
    3621              :   /* Location remapping.  first->ordinary, second->macro.  */
    3622              :   range_t GTY((skip)) loc_deltas;
    3623              : 
    3624              :   unsigned current;     /* Section currently being loaded.  */
    3625              :   unsigned remaining;   /* Number of lazy sections yet to read.  */
    3626              :   unsigned lru;         /* An LRU counter.  */
    3627              : 
    3628              :  public:
    3629              :   slurping (elf_in *);
    3630              :   ~slurping ();
    3631              : 
    3632              :  public:
    3633              :   /* Close the ELF file, if it's open.  */
    3634         5260 :   void close ()
    3635              :   {
    3636         5260 :     if (from)
    3637              :       {
    3638         2887 :         from->end ();
    3639         5774 :         delete from;
    3640         2887 :         from = NULL;
    3641              :       }
    3642         5260 :   }
    3643              : 
    3644              :  public:
    3645              :   void release_macros ();
    3646              : 
    3647              :  public:
    3648         2945 :   void alloc_remap (unsigned size)
    3649              :   {
    3650         2945 :     gcc_assert (!remap);
    3651         2945 :     vec_safe_reserve (remap, size);
    3652         6282 :     for (unsigned ix = size; ix--;)
    3653         3337 :       remap->quick_push (0);
    3654         2945 :   }
    3655      1171122 :   unsigned remap_module (unsigned owner)
    3656              :   {
    3657      1171122 :     if (owner < remap->length ())
    3658      1171122 :       return (*remap)[owner] >> 1;
    3659              :     return 0;
    3660              :   }
    3661              : 
    3662              :  public:
    3663              :   /* GC allocation.  But we must explicitly delete it.   */
    3664         2969 :   static void *operator new (size_t x)
    3665              :   {
    3666         5938 :     return ggc_alloc_atomic (x);
    3667              :   }
    3668         2887 :   static void operator delete (void *p)
    3669              :   {
    3670         2887 :     ggc_free (p);
    3671         2887 :   }
    3672              : };
    3673              : 
    3674         2969 : slurping::slurping (elf_in *from)
    3675         2969 :   : remap (NULL), from (from),
    3676         2969 :     headers (BITMAP_GGC_ALLOC ()), macro_defs (), macro_tbl (),
    3677         2969 :     loc_deltas (0, 0),
    3678         2969 :     current (~0u), remaining (0), lru (0)
    3679              : {
    3680         2969 : }
    3681              : 
    3682         2887 : slurping::~slurping ()
    3683              : {
    3684         2887 :   vec_free (remap);
    3685         2887 :   remap = NULL;
    3686         2887 :   release_macros ();
    3687         2887 :   close ();
    3688         2887 : }
    3689              : 
    3690         5260 : void slurping::release_macros ()
    3691              : {
    3692         5260 :   if (macro_defs.size)
    3693          876 :     elf_in::release (from, macro_defs);
    3694         5260 :   if (macro_tbl.size)
    3695            0 :     elf_in::release (from, macro_tbl);
    3696         5260 : }
    3697              : 
    3698              : /* Flags for extensions that end up being streamed.  */
    3699              : 
    3700              : enum streamed_extensions {
    3701              :   SE_OPENMP_SIMD = 1 << 0,
    3702              :   SE_OPENMP = 1 << 1,
    3703              :   SE_OPENACC = 1 << 2,
    3704              :   SE_BITS = 3
    3705              : };
    3706              : 
    3707              : /* Counter indices.  */
    3708              : enum module_state_counts
    3709              : {
    3710              :   MSC_sec_lwm,
    3711              :   MSC_sec_hwm,
    3712              :   MSC_pendings,
    3713              :   MSC_entities,
    3714              :   MSC_namespaces,
    3715              :   MSC_using_directives,
    3716              :   MSC_bindings,
    3717              :   MSC_macros,
    3718              :   MSC_inits,
    3719              :   MSC_HWM
    3720              : };
    3721              : 
    3722              : /********************************************************************/
    3723              : struct module_state_config;
    3724              : 
    3725              : /* Increasing levels of loadedness.  */
    3726              : enum module_loadedness {
    3727              :   ML_NONE,              /* Not loaded.  */
    3728              :   ML_CONFIG,            /* Config loaed.  */
    3729              :   ML_PREPROCESSOR,      /* Preprocessor loaded.  */
    3730              :   ML_LANGUAGE,          /* Language loaded.  */
    3731              : };
    3732              : 
    3733              : /* Increasing levels of directness (toplevel) of import.  */
    3734              : enum module_directness {
    3735              :   MD_NONE,              /* Not direct.  */
    3736              :   MD_PARTITION_DIRECT,  /* Direct import of a partition.  */
    3737              :   MD_DIRECT,            /* Direct import.  */
    3738              :   MD_PURVIEW_DIRECT,    /* Direct import in purview.  */
    3739              : };
    3740              : 
    3741              : /* State of a particular module. */
    3742              : 
    3743              : class GTY((chain_next ("%h.parent"), for_user)) module_state {
    3744              :  public:
    3745              :   /* We always import & export ourselves.  */
    3746              :   bitmap imports;       /* Transitive modules we're importing.  */
    3747              :   bitmap exports;       /* Subset of that, that we're exporting.  */
    3748              : 
    3749              :   /* For a named module interface A.B, parent is A and name is B.
    3750              :      For a partition M:P, parent is M and name is P.
    3751              :      For an implementation unit I, parent is I's interface and name is NULL.
    3752              :      Otherwise parent is NULL and name will be the flatname.  */
    3753              :   module_state *parent;
    3754              :   tree name;
    3755              : 
    3756              :   slurping *slurp;      /* Data for loading.  */
    3757              : 
    3758              :   const char *flatname; /* Flatname of module.  */
    3759              :   char *filename;       /* CMI Filename */
    3760              : 
    3761              :   /* Indices into the entity_ary.  */
    3762              :   unsigned entity_lwm;
    3763              :   unsigned entity_num;
    3764              : 
    3765              :   /* Location ranges for this module.  adhoc-locs are decomposed, so
    3766              :      don't have a range.  */
    3767              :   loc_range_t GTY((skip)) ordinary_locs;
    3768              :   loc_range_t GTY((skip)) macro_locs; // [lwm,num)
    3769              : 
    3770              :   /* LOC is first set too the importing location.  When initially
    3771              :      loaded it refers to a module loc whose parent is the importing
    3772              :      location.  */
    3773              :   location_t loc;       /* Location referring to module itself.  */
    3774              :   unsigned crc;         /* CRC we saw reading it in. */
    3775              : 
    3776              :   unsigned mod;         /* Module owner number.  */
    3777              :   unsigned remap;       /* Remapping during writing.  */
    3778              : 
    3779              :   unsigned short subst; /* Mangle subst if !0.  */
    3780              : 
    3781              :   /* How loaded this module is.  */
    3782              :   enum module_loadedness loadedness : 2;
    3783              : 
    3784              :   bool module_p : 1;    /* /The/ module of this TU.  */
    3785              :   bool header_p : 1;    /* Is a header unit.  */
    3786              :   bool interface_p : 1; /* An interface.  */
    3787              :   bool partition_p : 1; /* A partition.  */
    3788              : 
    3789              :   /* How directly this module is imported.  */
    3790              :   enum module_directness directness : 2;
    3791              : 
    3792              :   bool exported_p : 1;  /* directness != MD_NONE && exported.  */
    3793              :   bool cmi_noted_p : 1; /* We've told the user about the CMI, don't
    3794              :                            do it again  */
    3795              :   bool active_init_p : 1; /* This module's global initializer needs
    3796              :                            calling.  */
    3797              :   bool inform_cmi_p : 1; /* Inform of a read/write.  */
    3798              :   bool visited_p : 1;    /* A walk-once flag. */
    3799              :   /* Record extensions emitted or permitted.  */
    3800              :   unsigned extensions : SE_BITS;
    3801              :   /* 14 bits used, 2 bits remain  */
    3802              : 
    3803              :  public:
    3804              :   module_state (tree name, module_state *, bool);
    3805              :   ~module_state ();
    3806              : 
    3807              :  public:
    3808         2944 :   void release ()
    3809              :   {
    3810         2944 :     imports = exports = NULL;
    3811         2944 :     slurped ();
    3812         2887 :   }
    3813         5317 :   void slurped ()
    3814              :   {
    3815         5317 :     delete slurp;
    3816         5317 :     slurp = NULL;
    3817         5317 :   }
    3818      1244816 :   elf_in *from () const
    3819              :   {
    3820      1244816 :     return slurp->from;
    3821              :   }
    3822              : 
    3823              :  public:
    3824              :   /* Kind of this module.  */
    3825       133015 :   bool is_module () const
    3826              :   {
    3827       133015 :     return module_p;
    3828              :   }
    3829      2112023 :   bool is_header () const
    3830              :   {
    3831      2112023 :     return header_p;
    3832              :   }
    3833          588 :   bool is_interface () const
    3834              :   {
    3835          588 :     return interface_p;
    3836              :   }
    3837       294291 :   bool is_partition () const
    3838              :   {
    3839       294291 :     return partition_p;
    3840              :   }
    3841              : 
    3842              :   /* How this module is used in the current TU.  */
    3843         3166 :   bool is_exported () const
    3844              :   {
    3845         3166 :     return exported_p;
    3846              :   }
    3847        20527 :   bool is_direct () const
    3848              :   {
    3849        20527 :     return directness >= MD_DIRECT;
    3850              :   }
    3851          277 :   bool is_purview_direct () const
    3852              :   {
    3853          277 :     return directness == MD_PURVIEW_DIRECT;
    3854              :   }
    3855          464 :   bool is_partition_direct () const
    3856              :   {
    3857          464 :     return directness == MD_PARTITION_DIRECT;
    3858              :   }
    3859              : 
    3860              :  public:
    3861              :   /* Is this a real module?  */
    3862        15864 :   bool has_location () const
    3863              :   {
    3864        15864 :     return loc != UNKNOWN_LOCATION;
    3865              :   }
    3866              : 
    3867              :  public:
    3868              :   bool check_circular_import (location_t loc);
    3869              : 
    3870              :  public:
    3871              :   void mangle (bool include_partition);
    3872              : 
    3873              :  public:
    3874              :   void set_import (module_state const *, bool is_export);
    3875              :   void announce (const char *) const;
    3876              : 
    3877              :  public:
    3878              :   /* Read and write module.  */
    3879              :   bool write_begin (elf_out *to, cpp_reader *,
    3880              :                     module_state_config &, unsigned &crc);
    3881              :   void write_end (elf_out *to, cpp_reader *,
    3882              :                   module_state_config &, unsigned &crc);
    3883              :   bool read_initial (cpp_reader *);
    3884              :   bool read_preprocessor (bool);
    3885              :   bool read_language (bool);
    3886              : 
    3887              :  public:
    3888              :   /* Read a section.  */
    3889              :   bool load_section (unsigned snum, binding_slot *mslot);
    3890              :   /* Lazily read a section.  */
    3891              :   bool lazy_load (unsigned index, binding_slot *mslot);
    3892              : 
    3893              :  public:
    3894              :   /* Juggle a limited number of file numbers.  */
    3895              :   static void freeze_an_elf ();
    3896              :   bool maybe_defrost ();
    3897              : 
    3898              :  public:
    3899              :   void maybe_completed_reading ();
    3900              :   bool check_read (bool outermost, bool ok);
    3901              : 
    3902              :  private:
    3903              :   /* The README, for human consumption.  */
    3904              :   void write_readme (elf_out *to, cpp_reader *, const char *dialect);
    3905              :   void write_env (elf_out *to);
    3906              : 
    3907              :  private:
    3908              :   /* Import tables. */
    3909              :   void write_imports (bytes_out &cfg, bool direct);
    3910              :   unsigned read_imports (bytes_in &cfg, cpp_reader *, line_maps *maps);
    3911              : 
    3912              :  private:
    3913              :   void write_imports (elf_out *to, unsigned *crc_ptr);
    3914              :   bool read_imports (cpp_reader *, line_maps *);
    3915              : 
    3916              :  private:
    3917              :   void write_partitions (elf_out *to, unsigned, unsigned *crc_ptr);
    3918              :   bool read_partitions (unsigned);
    3919              : 
    3920              :  private:
    3921              :   void write_config (elf_out *to, struct module_state_config &, unsigned crc);
    3922              :   bool read_config (struct module_state_config &, bool = true);
    3923              :   static void write_counts (elf_out *to, unsigned [MSC_HWM], unsigned *crc_ptr);
    3924              :   bool read_counts (unsigned *);
    3925              : 
    3926              :  public:
    3927              :   void note_cmi_name ();
    3928              : 
    3929              :  private:
    3930              :   static unsigned write_bindings (elf_out *to, vec<depset *> depsets,
    3931              :                                   unsigned *crc_ptr);
    3932              :   bool read_bindings (unsigned count, unsigned lwm, unsigned hwm);
    3933              : 
    3934              :   static void write_namespace (bytes_out &sec, depset *ns_dep);
    3935              :   tree read_namespace (bytes_in &sec);
    3936              : 
    3937              :   void write_namespaces (elf_out *to, vec<depset *> spaces,
    3938              :                          unsigned, unsigned *crc_ptr);
    3939              :   bool read_namespaces (unsigned);
    3940              : 
    3941              :   unsigned write_using_directives (elf_out *to, depset::hash &,
    3942              :                                    vec<depset *> spaces, unsigned *crc_ptr);
    3943              :   bool read_using_directives (unsigned);
    3944              : 
    3945              :   void intercluster_seed (trees_out &sec, unsigned index, depset *dep);
    3946              :   unsigned write_cluster (elf_out *to, depset *depsets[], unsigned size,
    3947              :                           depset::hash &, unsigned *counts, unsigned *crc_ptr);
    3948              :   bool read_cluster (unsigned snum);
    3949              :   bool open_slurp (cpp_reader *);
    3950              : 
    3951              :  private:
    3952              :   unsigned write_inits (elf_out *to, depset::hash &, unsigned *crc_ptr);
    3953              :   bool read_inits (unsigned count);
    3954              : 
    3955              :  private:
    3956              :   unsigned write_pendings (elf_out *to, vec<depset *> depsets,
    3957              :                            depset::hash &, unsigned *crc_ptr);
    3958              :   bool read_pendings (unsigned count);
    3959              : 
    3960              :  private:
    3961              :   void write_entities (elf_out *to, vec<depset *> depsets,
    3962              :                        unsigned count, unsigned *crc_ptr);
    3963              :   bool read_entities (unsigned count, unsigned lwm, unsigned hwm);
    3964              : 
    3965              :  private:
    3966              :   void write_init_maps ();
    3967              :   range_t write_prepare_maps (module_state_config *, bool);
    3968              :   bool read_prepare_maps (const module_state_config *);
    3969              : 
    3970              :   void write_ordinary_maps (elf_out *to, range_t &,
    3971              :                             bool, unsigned *crc_ptr);
    3972              :   bool read_ordinary_maps (line_map_uint_t, unsigned);
    3973              :   void write_macro_maps (elf_out *to, range_t &, unsigned *crc_ptr);
    3974              :   bool read_macro_maps (line_map_uint_t);
    3975              : 
    3976              :   void write_diagnostic_classification (elf_out *, diagnostics::context *,
    3977              :                                         unsigned *);
    3978              :   bool read_diagnostic_classification (diagnostics::context *);
    3979              : 
    3980              :  private:
    3981              :   void write_define (bytes_out &, const cpp_macro *);
    3982              :   cpp_macro *read_define (bytes_in &, cpp_reader *) const;
    3983              :   vec<cpp_hashnode *> *prepare_macros (cpp_reader *);
    3984              :   unsigned write_macros (elf_out *to, vec<cpp_hashnode *> *, unsigned *crc_ptr);
    3985              :   bool read_macros ();
    3986              :   void install_macros ();
    3987              : 
    3988              :  public:
    3989              :   void import_macros ();
    3990              : 
    3991              :  public:
    3992              :   static void undef_macro (cpp_reader *, location_t, cpp_hashnode *);
    3993              :   static cpp_macro *deferred_macro (cpp_reader *, location_t, cpp_hashnode *);
    3994              : 
    3995              :  public:
    3996              :   static bool note_location (location_t);
    3997              :   static void write_location (bytes_out &, location_t);
    3998              :   location_t read_location (bytes_in &) const;
    3999              : 
    4000              :  public:
    4001              :   void set_flatname ();
    4002        52112 :   const char *get_flatname () const
    4003              :   {
    4004        52112 :     return flatname;
    4005              :   }
    4006              :   location_t imported_from () const;
    4007              : 
    4008              :  public:
    4009              :   void set_filename (const Cody::Packet &);
    4010              :   bool do_import (cpp_reader *, bool outermost);
    4011              :   bool check_importable (cpp_reader *);
    4012              : };
    4013              : 
    4014              : /* Hash module state by name.  This cannot be a member of
    4015              :    module_state, because of GTY restrictions.  We never delete from
    4016              :    the hash table, but ggc_ptr_hash doesn't support that
    4017              :    simplification.  */
    4018              : 
    4019              : struct module_state_hash : ggc_ptr_hash<module_state> {
    4020              :   typedef std::pair<tree,uintptr_t> compare_type; /* {name,parent} */
    4021              : 
    4022              :   static inline hashval_t hash (const value_type m);
    4023              :   static inline hashval_t hash (const compare_type &n);
    4024              :   static inline bool equal (const value_type existing,
    4025              :                             const compare_type &candidate);
    4026              : };
    4027              : 
    4028        11355 : module_state::module_state (tree name, module_state *parent, bool partition)
    4029        11355 :   : imports (BITMAP_GGC_ALLOC ()), exports (BITMAP_GGC_ALLOC ()),
    4030        11355 :     parent (parent), name (name), slurp (NULL),
    4031        11355 :     flatname (NULL), filename (NULL),
    4032        11355 :     entity_lwm (~0u >> 1), entity_num (0),
    4033        11355 :     ordinary_locs (0, 0), macro_locs (0, 0),
    4034        11355 :     loc (UNKNOWN_LOCATION),
    4035        11355 :     crc (0), mod (MODULE_UNKNOWN), remap (0), subst (0)
    4036              : {
    4037        11355 :   loadedness = ML_NONE;
    4038              : 
    4039        11355 :   module_p = header_p = interface_p = partition_p = false;
    4040              : 
    4041        11355 :   directness = MD_NONE;
    4042        11355 :   exported_p = false;
    4043              : 
    4044        11355 :   cmi_noted_p = false;
    4045        11355 :   active_init_p = false;
    4046              : 
    4047        11355 :   partition_p = partition;
    4048              : 
    4049        11355 :   inform_cmi_p = false;
    4050        11355 :   visited_p = false;
    4051              : 
    4052        11355 :   extensions = 0;
    4053        11355 :   if (name && TREE_CODE (name) == STRING_CST)
    4054              :     {
    4055         1881 :       header_p = true;
    4056              : 
    4057         1881 :       const char *string = TREE_STRING_POINTER (name);
    4058         1881 :       gcc_checking_assert (string[0] == '.'
    4059              :                            ? IS_DIR_SEPARATOR (string[1])
    4060              :                            : IS_ABSOLUTE_PATH (string));
    4061              :     }
    4062              : 
    4063        11355 :   gcc_checking_assert (!(parent && header_p));
    4064        11355 : }
    4065              : 
    4066           57 : module_state::~module_state ()
    4067              : {
    4068           57 :   release ();
    4069           57 : }
    4070              : 
    4071              : /* Hash module state.  */
    4072              : static hashval_t
    4073        16768 : module_name_hash (const_tree name)
    4074              : {
    4075        16768 :   if (TREE_CODE (name) == STRING_CST)
    4076         3486 :     return htab_hash_string (TREE_STRING_POINTER (name));
    4077              :   else
    4078        13282 :     return IDENTIFIER_HASH_VALUE (name);
    4079              : }
    4080              : 
    4081              : hashval_t
    4082         4704 : module_state_hash::hash (const value_type m)
    4083              : {
    4084         4704 :   hashval_t ph = pointer_hash<void>::hash
    4085         4704 :     (reinterpret_cast<void *> (reinterpret_cast<uintptr_t> (m->parent)
    4086         4704 :                                | m->is_partition ()));
    4087         4704 :   hashval_t nh = module_name_hash (m->name);
    4088         4704 :   return iterative_hash_hashval_t (ph, nh);
    4089              : }
    4090              : 
    4091              : /* Hash a name.  */
    4092              : hashval_t
    4093        12064 : module_state_hash::hash (const compare_type &c)
    4094              : {
    4095        12064 :   hashval_t ph = pointer_hash<void>::hash (reinterpret_cast<void *> (c.second));
    4096        12064 :   hashval_t nh = module_name_hash (c.first);
    4097              : 
    4098        12064 :   return iterative_hash_hashval_t (ph, nh);
    4099              : }
    4100              : 
    4101              : bool
    4102         8260 : module_state_hash::equal (const value_type existing,
    4103              :                           const compare_type &candidate)
    4104              : {
    4105         8260 :   uintptr_t ep = (reinterpret_cast<uintptr_t> (existing->parent)
    4106         8260 :                   | existing->is_partition ());
    4107         8260 :   if (ep != candidate.second)
    4108              :     return false;
    4109              : 
    4110              :   /* Identifier comparison is by pointer.  If the string_csts happen
    4111              :      to be the same object, then they're equal too.  */
    4112         6858 :   if (existing->name == candidate.first)
    4113              :     return true;
    4114              : 
    4115              :   /* If neither are string csts, they can't be equal.  */
    4116         1474 :   if (TREE_CODE (candidate.first) != STRING_CST
    4117          495 :       || TREE_CODE (existing->name) != STRING_CST)
    4118              :     return false;
    4119              : 
    4120              :   /* String equality.  */
    4121          425 :   if (TREE_STRING_LENGTH (existing->name)
    4122          425 :       == TREE_STRING_LENGTH (candidate.first)
    4123          425 :       && !memcmp (TREE_STRING_POINTER (existing->name),
    4124          422 :                   TREE_STRING_POINTER (candidate.first),
    4125          422 :                   TREE_STRING_LENGTH (existing->name)))
    4126          139 :     return true;
    4127              : 
    4128              :   return false;
    4129              : }
    4130              : 
    4131              : /********************************************************************/
    4132              : /* Global state */
    4133              : 
    4134              : /* Mapper name.  */
    4135              : static const char *module_mapper_name;
    4136              : 
    4137              : /* Deferred import queue (FIFO).  */
    4138              : static vec<module_state *, va_heap, vl_embed> *pending_imports;
    4139              : 
    4140              : /* CMI repository path and workspace.  */
    4141              : static char *cmi_repo;
    4142              : static size_t cmi_repo_length;
    4143              : static char *cmi_path;
    4144              : static size_t cmi_path_alloc;
    4145              : 
    4146              : /* Count of available and loaded clusters.  */
    4147              : static unsigned available_clusters;
    4148              : static unsigned loaded_clusters;
    4149              : 
    4150              : /* What the current TU is.  */
    4151              : unsigned module_kind;
    4152              : 
    4153              : /* Global trees.  */
    4154              : static const std::pair<tree *, unsigned> global_tree_arys[] =
    4155              :   {
    4156              :     std::pair<tree *, unsigned> (sizetype_tab, stk_type_kind_last),
    4157              :     std::pair<tree *, unsigned> (integer_types, itk_none),
    4158              :     std::pair<tree *, unsigned> (global_trees, TI_MODULE_HWM),
    4159              :     std::pair<tree *, unsigned> (c_global_trees, CTI_MODULE_HWM),
    4160              :     std::pair<tree *, unsigned> (cp_global_trees, CPTI_MODULE_HWM),
    4161              :     std::pair<tree *, unsigned> (NULL, 0)
    4162              :   };
    4163              : static GTY(()) vec<tree, va_gc> *fixed_trees;
    4164              : static unsigned global_crc;
    4165              : 
    4166              : /* Lazy loading can open many files concurrently, there are
    4167              :    per-process limits on that.  We pay attention to the process limit,
    4168              :    and attempt to increase it when we run out.  Otherwise we use an
    4169              :    LRU scheme to figure out who to flush.  Note that if the import
    4170              :    graph /depth/ exceeds lazy_limit, we'll exceed the limit.  */
    4171              : static unsigned lazy_lru;  /* LRU counter.  */
    4172              : static unsigned lazy_open; /* Number of open modules */
    4173              : static unsigned lazy_limit; /* Current limit of open modules.  */
    4174              : static unsigned lazy_hard_limit; /* Hard limit on open modules.  */
    4175              : /* Account for source, assembler and dump files & directory searches.
    4176              :    We don't keep the source file's open, so we don't have to account
    4177              :    for #include depth.  I think dump files are opened and closed per
    4178              :    pass, but ICBW.  */
    4179              : #define LAZY_HEADROOM 15 /* File descriptor headroom.  */
    4180              : 
    4181              : /* Vector of module state.  Indexed by OWNER.  Index 0 is reserved for the
    4182              :    current TU; imports start at 1.  */
    4183              : static GTY(()) vec<module_state *, va_gc> *modules;
    4184              : 
    4185              : /* Get the module state for the current TU's module.  */
    4186              : 
    4187              : static module_state *
    4188       249523 : this_module() {
    4189       249523 :   return (*modules)[0];
    4190              : }
    4191              : 
    4192              : /* Hash of module state, findable by {name, parent}. */
    4193              : static GTY(()) hash_table<module_state_hash> *modules_hash;
    4194              : 
    4195              : /* Map of imported entities.  We map DECL_UID to index of entity
    4196              :    vector.  */
    4197              : typedef hash_map<unsigned/*UID*/, unsigned/*index*/,
    4198              :                  simple_hashmap_traits<int_hash<unsigned,0>, unsigned>
    4199              :                  > entity_map_t;
    4200              : static entity_map_t *entity_map;
    4201              : /* Doesn't need GTYing, because any tree referenced here is also
    4202              :    findable by, symbol table, specialization table, return type of
    4203              :    reachable function.  */
    4204              : static vec<binding_slot, va_heap, vl_embed> *entity_ary;
    4205              : 
    4206              : /* Members entities of imported classes that are defined in this TU.
    4207              :    These are where the entity's context is not from the current TU.
    4208              :    We need to emit the definition (but not the enclosing class).
    4209              : 
    4210              :    We could find these by walking ALL the imported classes that we
    4211              :    could provide a member definition.  But that's expensive,
    4212              :    especially when you consider lazy implicit member declarations,
    4213              :    which could be ANY imported class.  */
    4214              : static GTY(()) vec<tree, va_gc> *class_members;
    4215              : 
    4216              : /* The same problem exists for class template partial
    4217              :    specializations.  Now that we have constraints, the invariant of
    4218              :    expecting them in the instantiation table no longer holds.  One of
    4219              :    the constrained partial specializations will be there, but the
    4220              :    others not so much.  It's not even an unconstrained partial
    4221              :    spacialization in the table :(  so any partial template declaration
    4222              :    is added to this list too.  */
    4223              : static GTY(()) vec<tree, va_gc> *partial_specializations;
    4224              : 
    4225              : /********************************************************************/
    4226              : 
    4227              : /* Our module mapper (created lazily).  */
    4228              : module_client *mapper;
    4229              : 
    4230              : static module_client *make_mapper (location_t loc, class mkdeps *deps);
    4231        28116 : inline module_client *get_mapper (location_t loc, class mkdeps *deps)
    4232              : {
    4233        28116 :   auto *res = mapper;
    4234          296 :   if (!res)
    4235         4814 :     res = make_mapper (loc, deps);
    4236        28116 :   return res;
    4237              : }
    4238              : 
    4239              : /********************************************************************/
    4240              : static tree
    4241       293319 : get_clone_target (tree decl)
    4242              : {
    4243       293319 :   tree target;
    4244              : 
    4245       293319 :   if (TREE_CODE (decl) == TEMPLATE_DECL)
    4246              :     {
    4247        33732 :       tree res_orig = DECL_CLONED_FUNCTION (DECL_TEMPLATE_RESULT (decl));
    4248              : 
    4249        33732 :       target = DECL_TI_TEMPLATE (res_orig);
    4250              :     }
    4251              :   else
    4252       259587 :     target = DECL_CLONED_FUNCTION (decl);
    4253              : 
    4254       293319 :   gcc_checking_assert (DECL_MAYBE_IN_CHARGE_CDTOR_P (target));
    4255              : 
    4256       293319 :   return target;
    4257              : }
    4258              : 
    4259              : /* Like FOR_EACH_CLONE, but will walk cloned templates.  */
    4260              : #define FOR_EVERY_CLONE(CLONE, FN)                      \
    4261              :   if (!DECL_MAYBE_IN_CHARGE_CDTOR_P (FN));              \
    4262              :   else                                                  \
    4263              :     for (CLONE = DECL_CHAIN (FN);                       \
    4264              :          CLONE && DECL_CLONED_FUNCTION_P (CLONE);       \
    4265              :          CLONE = DECL_CHAIN (CLONE))
    4266              : 
    4267              : /* It'd be nice if USE_TEMPLATE was a field of template_info
    4268              :    (a) it'd solve the enum case dealt with below,
    4269              :    (b) both class templates and decl templates would store this in the
    4270              :    same place
    4271              :    (c) this function wouldn't need the by-ref arg, which is annoying.  */
    4272              : 
    4273              : static tree
    4274    194872148 : node_template_info (tree decl, int &use)
    4275              : {
    4276    194872148 :   tree ti = NULL_TREE;
    4277    194872148 :   int use_tpl = -1;
    4278    194872148 :   if (DECL_IMPLICIT_TYPEDEF_P (decl))
    4279              :     {
    4280     21762899 :       tree type = TREE_TYPE (decl);
    4281              : 
    4282     21762899 :       ti = TYPE_TEMPLATE_INFO (type);
    4283     21762899 :       if (ti)
    4284              :         {
    4285      3882431 :           if (TYPE_LANG_SPECIFIC (type))
    4286      3873880 :             use_tpl = CLASSTYPE_USE_TEMPLATE (type);
    4287              :           else
    4288              :             {
    4289              :               /* An enum, where we don't explicitly encode use_tpl.
    4290              :                  If the containing context (a type or a function), is
    4291              :                  an ({im,ex}plicit) instantiation, then this is too.
    4292              :                  If it's a partial or explicit specialization, then
    4293              :                  this is not!.  */
    4294         8551 :               tree ctx = CP_DECL_CONTEXT (decl);
    4295         8551 :               if (TYPE_P (ctx))
    4296         8419 :                 ctx = TYPE_NAME (ctx);
    4297         8551 :               node_template_info (ctx, use);
    4298         8551 :               use_tpl = use != 2 ? use : 0;
    4299              :             }
    4300              :         }
    4301              :     }
    4302    173109249 :   else if (DECL_LANG_SPECIFIC (decl)
    4303    173109249 :            && (VAR_P (decl)
    4304              :                || TREE_CODE (decl) == TYPE_DECL
    4305              :                || TREE_CODE (decl) == FUNCTION_DECL
    4306              :                || TREE_CODE (decl) == FIELD_DECL
    4307              :                || TREE_CODE (decl) == CONCEPT_DECL
    4308              :                || TREE_CODE (decl) == TEMPLATE_DECL))
    4309              :     {
    4310     88145837 :       use_tpl = DECL_USE_TEMPLATE (decl);
    4311     88145837 :       ti = DECL_TEMPLATE_INFO (decl);
    4312              :     }
    4313              : 
    4314    194872148 :   use = use_tpl;
    4315    194872148 :   return ti;
    4316              : }
    4317              : 
    4318              : /* Find the index in entity_ary for an imported DECL.  It should
    4319              :    always be there, but bugs can cause it to be missing, and that can
    4320              :    crash the crash reporting -- let's not do that!  When streaming
    4321              :    out we place entities from this module there too -- with negated
    4322              :    indices.  */
    4323              : 
    4324              : static unsigned
    4325      1242900 : import_entity_index (tree decl, bool null_ok = false)
    4326              : {
    4327      1242900 :   if (unsigned *slot = entity_map->get (DECL_UID (decl)))
    4328      1242855 :     return *slot;
    4329              : 
    4330           45 :   gcc_checking_assert (null_ok);
    4331              :   return ~(~0u >> 1);
    4332              : }
    4333              : 
    4334              : /* Find the module for an imported entity at INDEX in the entity ary.
    4335              :    There must be one.  */
    4336              : 
    4337              : static module_state *
    4338       107620 : import_entity_module (unsigned index)
    4339              : {
    4340       107620 :   if (index > ~(~0u >> 1))
    4341              :     /* This is an index for an exported entity.  */
    4342           60 :     return this_module ();
    4343              : 
    4344              :   /* Do not include the current TU (not an off-by-one error).  */
    4345       107560 :   unsigned pos = 1;
    4346       107560 :   unsigned len = modules->length () - pos;
    4347       253335 :   while (len)
    4348              :     {
    4349       145775 :       unsigned half = len / 2;
    4350       145775 :       module_state *probe = (*modules)[pos + half];
    4351       145775 :       if (index < probe->entity_lwm)
    4352              :         len = half;
    4353       108026 :       else if (index < probe->entity_lwm + probe->entity_num)
    4354              :         return probe;
    4355              :       else
    4356              :         {
    4357          466 :           pos += half + 1;
    4358          466 :           len = len - (half + 1);
    4359              :         }
    4360              :     }
    4361            0 :   gcc_unreachable ();
    4362              : }
    4363              : 
    4364              : 
    4365              : /********************************************************************/
    4366              : /* A dumping machinery.  */
    4367              : 
    4368              : class dumper {
    4369              : public:
    4370              :   enum {
    4371              :     LOCATION = TDF_LINENO,  /* -lineno:Source location streaming.  */
    4372              :     DEPEND = TDF_GRAPH, /* -graph:Dependency graph construction.  */
    4373              :     CLUSTER = TDF_BLOCKS,   /* -blocks:Clusters.  */
    4374              :     TREE = TDF_UID,     /* -uid:Tree streaming.  */
    4375              :     MERGE = TDF_ALIAS,  /* -alias:Mergeable Entities.  */
    4376              :     ELF = TDF_ASMNAME,  /* -asmname:Elf data.  */
    4377              :     MACRO = TDF_VOPS    /* -vops:Macros.  */
    4378              :   };
    4379              : 
    4380              : private:
    4381              :   struct impl {
    4382              :     typedef vec<module_state *, va_heap, vl_embed> stack_t;
    4383              : 
    4384              :     FILE *stream;       /* Dump stream.  */
    4385              :     unsigned indent;    /* Local indentation.  */
    4386              :     bool bol;           /* Beginning of line.  */
    4387              :     stack_t stack;      /* Trailing array of module_state.  */
    4388              : 
    4389              :     bool nested_name (tree);  /* Dump a name following DECL_CONTEXT.  */
    4390              :   };
    4391              : 
    4392              : public:
    4393              :   /* The dumper.  */
    4394              :   impl *dumps;
    4395              :   dump_flags_t flags;
    4396              : 
    4397              : public:
    4398              :   /* Push/pop module state dumping.  */
    4399              :   unsigned push (module_state *);
    4400              :   void pop (unsigned);
    4401              : 
    4402              : public:
    4403              :   /* Change local indentation.  */
    4404    322370959 :   void indent ()
    4405              :   {
    4406          309 :     if (dumps)
    4407       720765 :       dumps->indent++;
    4408              :   }
    4409    322370959 :   void outdent ()
    4410              :   {
    4411    322370959 :     if (dumps)
    4412              :       {
    4413       720765 :         gcc_checking_assert (dumps->indent);
    4414       720765 :         dumps->indent--;
    4415              :       }
    4416    322370959 :   }
    4417              : 
    4418              : public:
    4419              :   /* Is dump enabled?.  */
    4420    206882964 :   bool operator () (int mask = 0)
    4421              :   {
    4422      4012475 :     if (!dumps || !dumps->stream)
    4423              :       return false;
    4424       502780 :     if (mask && !(mask & flags))
    4425         5376 :       return false;
    4426              :     return true;
    4427              :   }
    4428              :   /* Dump some information.  */
    4429              :   bool operator () (const char *, ...);
    4430              : };
    4431              : 
    4432              : /* The dumper.  */
    4433              : static dumper dump = {0, dump_flags_t (0)};
    4434              : 
    4435              : /* Push to dumping M.  Return previous indentation level.  */
    4436              : 
    4437              : unsigned
    4438       116163 : dumper::push (module_state *m)
    4439              : {
    4440       116163 :   FILE *stream = NULL;
    4441       116163 :   if (!dumps || !dumps->stack.length ())
    4442              :     {
    4443       114908 :       stream = dump_begin (module_dump_id, &flags);
    4444       114908 :       if (!stream)
    4445              :         return 0;
    4446              :     }
    4447              : 
    4448         6868 :   if (!dumps || !dumps->stack.space (1))
    4449              :     {
    4450              :       /* Create or extend the dump implementor.  */
    4451         1227 :       unsigned current = dumps ? dumps->stack.length () : 0;
    4452          650 :       unsigned count = current ? current * 2 : EXPERIMENT (1, 20);
    4453         1227 :       size_t alloc = (offsetof (impl, stack)
    4454         1227 :                       + impl::stack_t::embedded_size (count));
    4455         1227 :       dumps = XRESIZEVAR (impl, dumps, alloc);
    4456         1227 :       dumps->stack.embedded_init (count, current);
    4457              :     }
    4458         6868 :   if (stream)
    4459         5613 :     dumps->stream = stream;
    4460              : 
    4461         6868 :   unsigned n = dumps->indent;
    4462         6868 :   dumps->indent = 0;
    4463         6868 :   dumps->bol = true;
    4464         6868 :   dumps->stack.quick_push (m);
    4465         6868 :   if (m)
    4466              :     {
    4467         2009 :       module_state *from = NULL;
    4468              : 
    4469         2009 :       if (dumps->stack.length () > 1)
    4470          642 :         from = dumps->stack[dumps->stack.length () - 2];
    4471              :       else
    4472         1367 :         dump ("");
    4473         3635 :       dump (from ? "Starting module %M (from %M)"
    4474              :             : "Starting module %M", m, from);
    4475              :     }
    4476              : 
    4477              :   return n;
    4478              : }
    4479              : 
    4480              : /* Pop from dumping.  Restore indentation to N.  */
    4481              : 
    4482       116120 : void dumper::pop (unsigned n)
    4483              : {
    4484       116120 :   if (!dumps)
    4485              :     return;
    4486              : 
    4487        13736 :   gcc_checking_assert (dump () && !dumps->indent);
    4488         6868 :   if (module_state *m = dumps->stack[dumps->stack.length () - 1])
    4489              :     {
    4490         2009 :       module_state *from = (dumps->stack.length () > 1
    4491         2009 :                             ? dumps->stack[dumps->stack.length () - 2] : NULL);
    4492         2268 :       dump (from ? "Finishing module %M (returning to %M)"
    4493              :             : "Finishing module %M", m, from);
    4494              :     }
    4495         6868 :   dumps->stack.pop ();
    4496         6868 :   dumps->indent = n;
    4497         6868 :   if (!dumps->stack.length ())
    4498              :     {
    4499         5613 :       dump_end (module_dump_id, dumps->stream);
    4500         5613 :       dumps->stream = NULL;
    4501              :     }
    4502              : }
    4503              : 
    4504              : /* Dump a nested name for arbitrary tree T.  Sometimes it won't have a
    4505              :    name.  */
    4506              : 
    4507              : bool
    4508       521928 : dumper::impl::nested_name (tree t)
    4509              : {
    4510       521928 :   tree ti = NULL_TREE;
    4511       521928 :   int origin = -1;
    4512       521928 :   tree name = NULL_TREE;
    4513              : 
    4514       521928 :   if (t && TREE_CODE (t) == TU_LOCAL_ENTITY)
    4515            0 :     t = TU_LOCAL_ENTITY_NAME (t);
    4516              : 
    4517       521898 :   if (t && TREE_CODE (t) == TREE_BINFO)
    4518          384 :     t = BINFO_TYPE (t);
    4519              : 
    4520       521928 :   if (t && TYPE_P (t))
    4521       254323 :     t = TYPE_NAME (t);
    4522              : 
    4523       521886 :   if (t && DECL_P (t))
    4524              :     {
    4525       438646 :       if (t == global_namespace || DECL_TEMPLATE_PARM_P (t))
    4526              :         ;
    4527       406873 :       else if (tree ctx = DECL_CONTEXT (t))
    4528       317190 :         if (TREE_CODE (ctx) == TRANSLATION_UNIT_DECL
    4529       317190 :             || nested_name (ctx))
    4530       317190 :           fputs ("::", stream);
    4531              : 
    4532       438646 :       int use_tpl;
    4533       438646 :       ti = node_template_info (t, use_tpl);
    4534       138019 :       if (ti && TREE_CODE (TI_TEMPLATE (ti)) == TEMPLATE_DECL
    4535       576620 :           && (DECL_TEMPLATE_RESULT (TI_TEMPLATE (ti)) == t))
    4536              :         t = TI_TEMPLATE (ti);
    4537       438646 :       tree not_tmpl = t;
    4538       438646 :       if (TREE_CODE (t) == TEMPLATE_DECL)
    4539              :         {
    4540        23516 :           fputs ("template ", stream);
    4541        23516 :           not_tmpl = DECL_TEMPLATE_RESULT (t);
    4542              :         }
    4543              : 
    4544        23516 :       if (not_tmpl
    4545       438642 :           && DECL_P (not_tmpl)
    4546       438642 :           && DECL_LANG_SPECIFIC (not_tmpl)
    4547       263698 :           && DECL_MODULE_IMPORT_P (not_tmpl))
    4548              :         {
    4549              :           /* We need to be careful here, so as to not explode on
    4550              :              inconsistent data -- we're probably debugging, because
    4551              :              Something Is Wrong.  */
    4552        23975 :           unsigned index = import_entity_index (t, true);
    4553        23975 :           if (!(index & ~(~0u >> 1)))
    4554        23417 :             origin = import_entity_module (index)->mod;
    4555          558 :           else if (index > ~(~0u >> 1))
    4556              :             /* An imported partition member that we're emitting.  */
    4557              :             origin = 0;
    4558              :           else
    4559           45 :             origin = -2;
    4560              :         }
    4561              : 
    4562       441871 :       name = DECL_NAME (t) ? DECL_NAME (t)
    4563         4298 :         : HAS_DECL_ASSEMBLER_NAME_P (t) ? DECL_ASSEMBLER_NAME_RAW (t)
    4564              :         : NULL_TREE;
    4565              :     }
    4566              :   else
    4567              :     name = t;
    4568              : 
    4569       438646 :   if (name)
    4570       486857 :     switch (TREE_CODE (name))
    4571              :       {
    4572        13619 :       default:
    4573        13619 :         fputs ("#unnamed#", stream);
    4574        13619 :         break;
    4575              : 
    4576       448876 :       case IDENTIFIER_NODE:
    4577       448876 :         fwrite (IDENTIFIER_POINTER (name), 1, IDENTIFIER_LENGTH (name), stream);
    4578       448876 :         break;
    4579              : 
    4580        24270 :       case INTEGER_CST:
    4581        24270 :         print_hex (wi::to_wide (name), stream);
    4582        24270 :         break;
    4583              : 
    4584           92 :       case STRING_CST:
    4585              :         /* If TREE_TYPE is NULL, this is a raw string.  */
    4586          184 :         fwrite (TREE_STRING_POINTER (name), 1,
    4587           92 :                 TREE_STRING_LENGTH (name) - (TREE_TYPE (name) != NULL_TREE),
    4588              :                 stream);
    4589           92 :         break;
    4590              :       }
    4591              :   else
    4592        35071 :     fputs ("#null#", stream);
    4593              : 
    4594       521928 :   if (t && TREE_CODE (t) == FUNCTION_DECL && DECL_COROUTINE_P (t))
    4595           48 :     if (tree ramp = DECL_RAMP_FN (t))
    4596              :       {
    4597           27 :         if (DECL_ACTOR_FN (ramp) == t)
    4598           12 :           fputs (".actor", stream);
    4599           15 :         else if (DECL_DESTROY_FN (ramp) == t)
    4600           15 :           fputs (".destroy", stream);
    4601              :         else
    4602            0 :           gcc_unreachable ();
    4603              :       }
    4604              : 
    4605       521928 :   if (origin >= 0)
    4606              :     {
    4607        23930 :       const module_state *module = (*modules)[origin];
    4608        47860 :       fprintf (stream, "@%s:%d", !module ? "" : !module->name ? "(unnamed)"
    4609        23930 :                : module->get_flatname (), origin);
    4610              :     }
    4611       497998 :   else if (origin == -2)
    4612           45 :     fprintf (stream, "@???");
    4613              : 
    4614       521928 :   if (ti)
    4615              :     {
    4616       138019 :       tree args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (ti));
    4617       138019 :       fputs ("<", stream);
    4618       138019 :       if (args)
    4619       347865 :         for (int ix = 0; ix != TREE_VEC_LENGTH (args); ix++)
    4620              :           {
    4621       209846 :             if (ix)
    4622        71827 :               fputs (",", stream);
    4623       209846 :             nested_name (TREE_VEC_ELT (args, ix));
    4624              :           }
    4625       138019 :       fputs (">", stream);
    4626              :     }
    4627              : 
    4628       521928 :   return true;
    4629              : }
    4630              : 
    4631              : /* Formatted dumping.  FORMAT begins with '+' do not emit a trailing
    4632              :    new line.  (Normally it is appended.)
    4633              :    Escapes:
    4634              :       %C - tree_code
    4635              :       %I - identifier
    4636              :       %K - location_t or line_map_uint_t
    4637              :       %M - module_state
    4638              :       %N - name -- DECL_NAME
    4639              :       %P - context:name pair
    4640              :       %R - unsigned:unsigned ratio
    4641              :       %S - symbol -- DECL_ASSEMBLER_NAME
    4642              :       %U - long unsigned
    4643              :       %V - version
    4644              :       --- the following are printf-like, but without its flexibility
    4645              :       %d - decimal int
    4646              :       %p - pointer
    4647              :       %s - string
    4648              :       %u - unsigned int
    4649              :       %x - hex int
    4650              : 
    4651              :   We do not implement the printf modifiers.  */
    4652              : 
    4653              : bool
    4654       441680 : dumper::operator () (const char *format, ...)
    4655              : {
    4656       441680 :   if (!(*this) ())
    4657              :     return false;
    4658              : 
    4659       379536 :   bool no_nl = format[0] == '+';
    4660       379536 :   format += no_nl;
    4661              : 
    4662       379536 :   if (dumps->bol)
    4663              :     {
    4664              :       /* Module import indent.  */
    4665       196050 :       if (unsigned depth = dumps->stack.length () - 1)
    4666              :         {
    4667        22427 :           const char *prefix = ">>>>";
    4668        44836 :           fprintf (dumps->stream, (depth <= strlen (prefix)
    4669        22409 :                                    ? &prefix[strlen (prefix) - depth]
    4670              :                                    : ">.%d.>"), depth);
    4671              :         }
    4672              : 
    4673              :       /* Local indent.  */
    4674       196050 :       if (unsigned indent = dumps->indent)
    4675              :         {
    4676       105849 :           const char *prefix = "      ";
    4677       206682 :           fprintf (dumps->stream, (indent <= strlen (prefix)
    4678       100833 :                                    ? &prefix[strlen (prefix) - indent]
    4679              :                                    : "  .%d.  "), indent);
    4680              :         }
    4681       196050 :       dumps->bol = false;
    4682              :     }
    4683              : 
    4684       379536 :   va_list args;
    4685       379536 :   va_start (args, format);
    4686      1112103 :   while (const char *esc = strchr (format, '%'))
    4687              :     {
    4688       732567 :       fwrite (format, 1, (size_t)(esc - format), dumps->stream);
    4689       732567 :       format = ++esc;
    4690       732567 :       switch (*format++)
    4691              :         {
    4692            0 :         default:
    4693            0 :           gcc_unreachable ();
    4694              : 
    4695          577 :         case '%':
    4696          577 :           fputc ('%', dumps->stream);
    4697          577 :           break;
    4698              : 
    4699       112023 :         case 'C': /* Code */
    4700       112023 :           {
    4701       112023 :             tree_code code = (tree_code)va_arg (args, unsigned);
    4702       112023 :             fputs (get_tree_code_name (code), dumps->stream);
    4703              :           }
    4704       112023 :           break;
    4705              : 
    4706           81 :         case 'I': /* Identifier.  */
    4707           81 :           {
    4708           81 :             tree t = va_arg (args, tree);
    4709           81 :             dumps->nested_name (t);
    4710              :           }
    4711           81 :           break;
    4712              : 
    4713         4596 :         case 'K': /* location_t, either 32- or 64-bit.  */
    4714         4596 :           {
    4715         4596 :             unsigned long long u = va_arg (args, location_t);
    4716         4596 :             fprintf (dumps->stream, "%llu", u);
    4717              :           }
    4718         4596 :           break;
    4719              : 
    4720         7617 :         case 'M': /* Module. */
    4721         7617 :           {
    4722         7617 :             const char *str = "(none)";
    4723         7617 :             if (module_state *m = va_arg (args, module_state *))
    4724              :               {
    4725         7617 :                 if (!m->has_location ())
    4726              :                   str = "(detached)";
    4727              :                 else
    4728         7617 :                   str = m->get_flatname ();
    4729              :               }
    4730         7617 :             fputs (str, dumps->stream);
    4731              :           }
    4732         7617 :           break;
    4733              : 
    4734       125165 :         case 'N': /* Name.  */
    4735       125165 :           {
    4736       125165 :             tree t = va_arg (args, tree);
    4737       250819 :             while (t && TREE_CODE (t) == OVERLOAD)
    4738          489 :               t = OVL_FUNCTION (t);
    4739       125165 :             fputc ('\'', dumps->stream);
    4740       125165 :             dumps->nested_name (t);
    4741       125165 :             fputc ('\'', dumps->stream);
    4742              :           }
    4743       125165 :           break;
    4744              : 
    4745         7319 :         case 'P': /* Pair.  */
    4746         7319 :           {
    4747         7319 :             tree ctx = va_arg (args, tree);
    4748         7319 :             tree name = va_arg (args, tree);
    4749         7319 :             fputc ('\'', dumps->stream);
    4750         7319 :             dumps->nested_name (ctx);
    4751         7319 :             if (ctx && ctx != global_namespace)
    4752          998 :               fputs ("::", dumps->stream);
    4753         7319 :             dumps->nested_name (name);
    4754         7319 :             fputc ('\'', dumps->stream);
    4755              :           }
    4756         7319 :           break;
    4757              : 
    4758          891 :         case 'R': /* Ratio */
    4759          891 :           {
    4760          891 :             unsigned a = va_arg (args, unsigned);
    4761          891 :             unsigned b = va_arg (args, unsigned);
    4762          891 :             fprintf (dumps->stream, "%.1f", (float) a / (b + !b));
    4763              :           }
    4764          891 :           break;
    4765              : 
    4766        34704 :         case 'S': /* Symbol name */
    4767        34704 :           {
    4768        34704 :             tree t = va_arg (args, tree);
    4769        34704 :             if (t && TYPE_P (t))
    4770        12631 :               t = TYPE_NAME (t);
    4771        32924 :             if (t && HAS_DECL_ASSEMBLER_NAME_P (t)
    4772        31750 :                 && DECL_ASSEMBLER_NAME_SET_P (t))
    4773              :               {
    4774          172 :                 fputc ('(', dumps->stream);
    4775          172 :                 fputs (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (t)),
    4776          172 :                        dumps->stream);
    4777          172 :                 fputc (')', dumps->stream);
    4778              :               }
    4779              :           }
    4780              :           break;
    4781              : 
    4782            0 :         case 'U': /* long unsigned.  */
    4783            0 :           {
    4784            0 :             unsigned long u = va_arg (args, unsigned long);
    4785            0 :             fprintf (dumps->stream, "%lu", u);
    4786              :           }
    4787            0 :           break;
    4788              : 
    4789          820 :         case 'V': /* Version.  */
    4790          820 :           {
    4791          820 :             unsigned v = va_arg (args, unsigned);
    4792          820 :             verstr_t string;
    4793              : 
    4794          820 :             version2string (v, string);
    4795          820 :             fputs (string, dumps->stream);
    4796              :           }
    4797          820 :           break;
    4798              : 
    4799            0 :         case 'c': /* Character.  */
    4800            0 :           {
    4801            0 :             int c = va_arg (args, int);
    4802            0 :             fputc (c, dumps->stream);
    4803              :           }
    4804            0 :           break;
    4805              : 
    4806        63075 :         case 'd': /* Decimal Int.  */
    4807        63075 :           {
    4808        63075 :             int d = va_arg (args, int);
    4809        63075 :             fprintf (dumps->stream, "%d", d);
    4810              :           }
    4811        63075 :           break;
    4812              : 
    4813            0 :         case 'p': /* Pointer. */
    4814            0 :           {
    4815            0 :             void *p = va_arg (args, void *);
    4816            0 :             fprintf (dumps->stream, "%p", p);
    4817              :           }
    4818            0 :           break;
    4819              : 
    4820       126196 :         case 's': /* String. */
    4821       126196 :           {
    4822       126196 :             const char *s = va_arg (args, char *);
    4823       126196 :             gcc_checking_assert (s);
    4824       126196 :             fputs (s, dumps->stream);
    4825              :           }
    4826       126196 :           break;
    4827              : 
    4828       246866 :         case 'u': /* Unsigned.  */
    4829       246866 :           {
    4830       246866 :             unsigned u = va_arg (args, unsigned);
    4831       246866 :             fprintf (dumps->stream, "%u", u);
    4832              :           }
    4833       246866 :           break;
    4834              : 
    4835         2637 :         case 'x': /* Hex. */
    4836         2637 :           {
    4837         2637 :             unsigned x = va_arg (args, unsigned);
    4838         2637 :             fprintf (dumps->stream, "%x", x);
    4839              :           }
    4840         2637 :           break;
    4841              :         }
    4842              :     }
    4843       379536 :   fputs (format, dumps->stream);
    4844       379536 :   va_end (args);
    4845       379536 :   if (!no_nl)
    4846              :     {
    4847       196050 :       dumps->bol = true;
    4848       196050 :       fputc ('\n', dumps->stream);
    4849              :     }
    4850              :   return true;
    4851              : }
    4852              : 
    4853              : struct note_def_cache_hasher : ggc_cache_ptr_hash<tree_node>
    4854              : {
    4855       287205 :   static int keep_cache_entry (tree t)
    4856              :   {
    4857       287205 :     if (!CHECKING_P)
    4858              :       /* GTY is unfortunately not clever enough to conditionalize
    4859              :          this.  */
    4860              :       gcc_unreachable ();
    4861              : 
    4862       287205 :     if (ggc_marked_p (t))
    4863              :       return -1;
    4864              : 
    4865            0 :     unsigned n = dump.push (NULL);
    4866              :     /* This might or might not be an error.  We should note its
    4867              :        dropping whichever.  */
    4868            0 :     dump () && dump ("Dropping %N from note_defs table", t);
    4869            0 :     dump.pop (n);
    4870              : 
    4871            0 :     return 0;
    4872              :   }
    4873              : };
    4874              : 
    4875              : /* We should stream each definition at most once.
    4876              :    This needs to be a cache because there are cases where a definition
    4877              :    ends up being not retained, and we need to drop those so we don't
    4878              :    get confused if memory is reallocated.  */
    4879              : typedef hash_table<note_def_cache_hasher> note_defs_table_t;
    4880              : static GTY((cache)) note_defs_table_t *note_defs;
    4881              : 
    4882              : void
    4883       315364 : trees_in::assert_definition (tree decl ATTRIBUTE_UNUSED,
    4884              :                              bool installing ATTRIBUTE_UNUSED)
    4885              : {
    4886              : #if CHECKING_P
    4887       315364 :   tree *slot = note_defs->find_slot (decl, installing ? INSERT : NO_INSERT);
    4888       315364 :   tree not_tmpl = STRIP_TEMPLATE (decl);
    4889       315364 :   if (installing)
    4890              :     {
    4891              :       /* We must be inserting for the first time.  */
    4892       180830 :       gcc_assert (!*slot);
    4893       180830 :       *slot = decl;
    4894              :     }
    4895              :   else
    4896              :     /* If this is not the mergeable entity, it should not be in the
    4897              :        table.  If it is a non-global-module mergeable entity, it
    4898              :        should be in the table.  Global module entities could have been
    4899              :        defined textually in the current TU and so might or might not
    4900              :        be present.  */
    4901       134534 :     gcc_assert (!is_duplicate (decl)
    4902              :                 ? !slot
    4903              :                 : (slot
    4904              :                    || !DECL_LANG_SPECIFIC (not_tmpl)
    4905              :                    || !DECL_MODULE_PURVIEW_P (not_tmpl)
    4906              :                    || (!DECL_MODULE_IMPORT_P (not_tmpl)
    4907              :                        && header_module_p ())));
    4908              : 
    4909       315364 :   if (not_tmpl != decl)
    4910       184514 :     gcc_assert (!note_defs->find_slot (not_tmpl, NO_INSERT));
    4911              : #endif
    4912       315364 : }
    4913              : 
    4914              : void
    4915       336114 : trees_out::assert_definition (tree decl ATTRIBUTE_UNUSED)
    4916              : {
    4917              : #if CHECKING_P
    4918       336114 :   tree *slot = note_defs->find_slot (decl, INSERT);
    4919       336114 :   gcc_assert (!*slot);
    4920       336114 :   *slot = decl;
    4921       336114 :   if (TREE_CODE (decl) == TEMPLATE_DECL)
    4922       185125 :     gcc_assert (!note_defs->find_slot (DECL_TEMPLATE_RESULT (decl), NO_INSERT));
    4923              : #endif
    4924       336114 : }
    4925              : 
    4926              : /********************************************************************/
    4927              : static bool
    4928        12399 : noisy_p ()
    4929              : {
    4930            0 :   if (quiet_flag)
    4931              :     return false;
    4932              : 
    4933            0 :   pp_needs_newline (global_dc->get_reference_printer ()) = true;
    4934            0 :   diagnostic_set_last_function (global_dc,
    4935              :                                 (diagnostics::diagnostic_info *) nullptr);
    4936              : 
    4937            0 :   return true;
    4938              : }
    4939              : 
    4940              : /* Set the cmi repo.  Strip trailing '/', '.' becomes NULL.  */
    4941              : 
    4942              : static void
    4943       101251 : set_cmi_repo (const char *r)
    4944              : {
    4945       101251 :   XDELETEVEC (cmi_repo);
    4946       101251 :   XDELETEVEC (cmi_path);
    4947       101251 :   cmi_path_alloc = 0;
    4948              : 
    4949       101251 :   cmi_repo = NULL;
    4950       101251 :   cmi_repo_length = 0;
    4951              : 
    4952       101251 :   if (!r || !r[0])
    4953              :     return;
    4954              : 
    4955         4811 :   size_t len = strlen (r);
    4956         4811 :   cmi_repo = XNEWVEC (char, len + 1);
    4957         4811 :   memcpy (cmi_repo, r, len + 1);
    4958              : 
    4959         4811 :   if (len > 1 && IS_DIR_SEPARATOR (cmi_repo[len-1]))
    4960         4811 :     len--;
    4961         4811 :   if (len == 1 && cmi_repo[0] == '.')
    4962           21 :     len--;
    4963         4811 :   cmi_repo[len] = 0;
    4964         4811 :   cmi_repo_length = len;
    4965              : }
    4966              : 
    4967              : /* TO is a repo-relative name.  Provide one that we may use from where
    4968              :    we are.  */
    4969              : 
    4970              : static const char *
    4971         5883 : maybe_add_cmi_prefix (const char *to, size_t *len_p = NULL)
    4972              : {
    4973         5883 :   size_t len = len_p || cmi_repo_length ? strlen (to) : 0;
    4974              : 
    4975         5883 :   if (cmi_repo_length && !IS_ABSOLUTE_PATH (to))
    4976              :     {
    4977         5862 :       if (cmi_path_alloc < cmi_repo_length + len + 2)
    4978              :         {
    4979         4695 :           XDELETEVEC (cmi_path);
    4980         4695 :           cmi_path_alloc = cmi_repo_length + len * 2 + 2;
    4981         4695 :           cmi_path = XNEWVEC (char, cmi_path_alloc);
    4982              : 
    4983         4695 :           memcpy (cmi_path, cmi_repo, cmi_repo_length);
    4984         4695 :           cmi_path[cmi_repo_length] = DIR_SEPARATOR;
    4985              :         }
    4986              : 
    4987         5862 :       memcpy (&cmi_path[cmi_repo_length + 1], to, len + 1);
    4988         5862 :       len += cmi_repo_length + 1;
    4989         5862 :       to = cmi_path;
    4990              :     }
    4991              : 
    4992         5883 :   if (len_p)
    4993         2829 :     *len_p = len;
    4994              : 
    4995         5883 :   return to;
    4996              : }
    4997              : 
    4998              : /* Try and create the directories of PATH.  */
    4999              : 
    5000              : static void
    5001           29 : create_dirs (char *path)
    5002              : {
    5003           29 :   char *base = path;
    5004              :   /* Skip past initial slashes of absolute path.  */
    5005           29 :   while (IS_DIR_SEPARATOR (*base))
    5006            0 :     base++;
    5007              : 
    5008              :   /* Try and create the missing directories.  */
    5009         1788 :   for (; *base; base++)
    5010         1759 :     if (IS_DIR_SEPARATOR (*base))
    5011              :       {
    5012          169 :         char sep = *base;
    5013          169 :         *base = 0;
    5014          169 :         int failed = mkdir (path, S_IRWXU | S_IRWXG | S_IRWXO);
    5015          175 :         dump () && dump ("Mkdir ('%s') errno:=%u", path, failed ? errno : 0);
    5016          169 :         *base = sep;
    5017          169 :         if (failed
    5018              :             /* Maybe racing with another creator (of a *different*
    5019              :                module).  */
    5020           35 :             && errno != EEXIST)
    5021              :           break;
    5022              :       }
    5023           29 : }
    5024              : 
    5025              : /* Given a CLASSTYPE_DECL_LIST VALUE get the template friend decl,
    5026              :    if that's what this is.  */
    5027              : 
    5028              : static tree
    5029        68540 : friend_from_decl_list (tree frnd)
    5030              : {
    5031        68540 :   tree res = frnd;
    5032              : 
    5033        68540 :   if (TREE_CODE (frnd) != TEMPLATE_DECL)
    5034              :     {
    5035        40427 :       tree tmpl = NULL_TREE;
    5036        40427 :       if (TYPE_P (frnd))
    5037              :         {
    5038         6558 :           res = TYPE_NAME (frnd);
    5039         6459 :           if (CLASS_TYPE_P (frnd)
    5040        13017 :               && CLASSTYPE_TEMPLATE_INFO (frnd))
    5041         6450 :             tmpl = CLASSTYPE_TI_TEMPLATE (frnd);
    5042              :         }
    5043        33869 :       else if (DECL_TEMPLATE_INFO (frnd))
    5044              :         {
    5045        33869 :           tmpl = DECL_TI_TEMPLATE (frnd);
    5046        33869 :           if (TREE_CODE (tmpl) != TEMPLATE_DECL)
    5047              :             tmpl = NULL_TREE;
    5048              :         }
    5049              : 
    5050        46191 :       if (tmpl && DECL_TEMPLATE_RESULT (tmpl) == res)
    5051              :         res = tmpl;
    5052              :     }
    5053              : 
    5054        68540 :   return res;
    5055              : }
    5056              : 
    5057              : static tree
    5058        28229 : find_enum_member (tree ctx, tree name)
    5059              : {
    5060        28229 :   for (tree values = TYPE_VALUES (ctx);
    5061       482910 :        values; values = TREE_CHAIN (values))
    5062       475201 :     if (DECL_NAME (TREE_VALUE (values)) == name)
    5063              :       return TREE_VALUE (values);
    5064              : 
    5065              :   return NULL_TREE;
    5066              : }
    5067              : 
    5068              : /********************************************************************/
    5069              : /* Instrumentation gathered writing bytes.  */
    5070              : 
    5071              : void
    5072          297 : bytes_out::instrument ()
    5073              : {
    5074          297 :   dump ("Wrote %u bytes in %u blocks", lengths[3], spans[3]);
    5075          297 :   dump ("Wrote %u bits in %u bytes", lengths[0] + lengths[1], lengths[2]);
    5076          891 :   for (unsigned ix = 0; ix < 2; ix++)
    5077          891 :     dump ("  %u %s spans of %R bits", spans[ix],
    5078              :           ix ? "one" : "zero", lengths[ix], spans[ix]);
    5079          297 :   dump ("  %u blocks with %R bits padding", spans[2],
    5080          297 :         lengths[2] * 8 - (lengths[0] + lengths[1]), spans[2]);
    5081          297 : }
    5082              : 
    5083              : /* Instrumentation gathered writing trees.  */
    5084              : void
    5085         2685 : trees_out::instrument ()
    5086              : {
    5087         2685 :   if (dump (""))
    5088              :     {
    5089          297 :       bytes_out::instrument ();
    5090          297 :       dump ("Wrote:");
    5091          297 :       dump ("  %u decl trees", decl_val_count);
    5092          297 :       dump ("  %u other trees", tree_val_count);
    5093          297 :       dump ("  %u back references", back_ref_count);
    5094          297 :       dump ("  %u TU-local entities", tu_local_count);
    5095          297 :       dump ("  %u null trees", null_count);
    5096              :     }
    5097         2685 : }
    5098              : 
    5099              : /* Setup and teardown for a tree walk.  */
    5100              : 
    5101              : void
    5102      1999442 : trees_out::begin ()
    5103              : {
    5104      1999442 :   gcc_assert (!streaming_p () || !tree_map.elements ());
    5105              : 
    5106      1999442 :   mark_trees ();
    5107      1999442 :   if (streaming_p ())
    5108       247644 :     parent::begin ();
    5109      1999442 : }
    5110              : 
    5111              : unsigned
    5112       247644 : trees_out::end (elf_out *sink, unsigned name, unsigned *crc_ptr)
    5113              : {
    5114       247644 :   gcc_checking_assert (streaming_p ());
    5115              : 
    5116       247644 :   unmark_trees ();
    5117       247644 :   return parent::end (sink, name, crc_ptr);
    5118              : }
    5119              : 
    5120              : void
    5121      1751798 : trees_out::end ()
    5122              : {
    5123      1751798 :   gcc_assert (!streaming_p ());
    5124              : 
    5125      1751798 :   unmark_trees ();
    5126              :   /* Do not parent::end -- we weren't streaming.  */
    5127      1751798 : }
    5128              : 
    5129              : void
    5130      1999442 : trees_out::mark_trees ()
    5131              : {
    5132      1999442 :   if (size_t size = tree_map.elements ())
    5133              :     {
    5134              :       /* This isn't our first rodeo, destroy and recreate the
    5135              :          tree_map.  I'm a bad bad man.  Use the previous size as a
    5136              :          guess for the next one (so not all bad).  */
    5137      1516066 :       tree_map.~ptr_int_hash_map ();
    5138      1516066 :       new (&tree_map) ptr_int_hash_map (size);
    5139              :     }
    5140              : 
    5141              :   /* Install the fixed trees, with +ve references.  */
    5142      1999442 :   unsigned limit = fixed_trees->length ();
    5143    385697211 :   for (unsigned ix = 0; ix != limit; ix++)
    5144              :     {
    5145    383697769 :       tree val = (*fixed_trees)[ix];
    5146    383697769 :       bool existed = tree_map.put (val, ix + tag_fixed);
    5147    383697769 :       gcc_checking_assert (!TREE_VISITED (val) && !existed);
    5148    383697769 :       TREE_VISITED (val) = true;
    5149              :     }
    5150              : 
    5151      1999442 :   ref_num = 0;
    5152      1999442 : }
    5153              : 
    5154              : /* Unmark the trees we encountered  */
    5155              : 
    5156              : void
    5157      1999442 : trees_out::unmark_trees ()
    5158              : {
    5159      1999442 :   ptr_int_hash_map::iterator end (tree_map.end ());
    5160    454259101 :   for (ptr_int_hash_map::iterator iter (tree_map.begin ()); iter != end; ++iter)
    5161              :     {
    5162    452259659 :       tree node = reinterpret_cast<tree> ((*iter).first);
    5163    452259659 :       int ref = (*iter).second;
    5164              :       /* We should have visited the node, and converted its mergeable
    5165              :          reference to a regular reference.  */
    5166    452259659 :       gcc_checking_assert (TREE_VISITED (node)
    5167              :                            && (ref <= tag_backref || ref >= tag_fixed));
    5168    452259659 :       TREE_VISITED (node) = false;
    5169              :     }
    5170      1999442 : }
    5171              : 
    5172              : /* Mark DECL for by-value walking.  We do this by inserting it into
    5173              :    the tree map with a reference of zero.  May be called multiple
    5174              :    times on the same node.  */
    5175              : 
    5176              : void
    5177      3088453 : trees_out::mark_by_value (tree decl)
    5178              : {
    5179      3088453 :   gcc_checking_assert (DECL_P (decl)
    5180              :                        /* Enum consts are INTEGER_CSTS.  */
    5181              :                        || TREE_CODE (decl) == INTEGER_CST
    5182              :                        || TREE_CODE (decl) == TREE_BINFO);
    5183              : 
    5184      3088453 :   if (TREE_VISITED (decl))
    5185              :     /* Must already be forced or fixed.  */
    5186         3302 :     gcc_checking_assert (*tree_map.get (decl) >= tag_value);
    5187              :   else
    5188              :     {
    5189      3085151 :       bool existed = tree_map.put (decl, tag_value);
    5190      3085151 :       gcc_checking_assert (!existed);
    5191      3085151 :       TREE_VISITED (decl) = true;
    5192              :     }
    5193      3088453 : }
    5194              : 
    5195              : int
    5196     82900697 : trees_out::get_tag (tree t)
    5197              : {
    5198     82900697 :   gcc_checking_assert (TREE_VISITED (t));
    5199     82900697 :   return *tree_map.get (t);
    5200              : }
    5201              : 
    5202              : /* Insert T into the map, return its tag number.    */
    5203              : 
    5204              : int
    5205     68561890 : trees_out::insert (tree t, walk_kind walk)
    5206              : {
    5207     68561890 :   gcc_checking_assert (walk != WK_normal || !TREE_VISITED (t));
    5208     68561890 :   int tag = --ref_num;
    5209     68561890 :   bool existed;
    5210     68561890 :   int &slot = tree_map.get_or_insert (t, &existed);
    5211     68561890 :   gcc_checking_assert (TREE_VISITED (t) == existed
    5212              :                        && (!existed
    5213              :                            || (walk == WK_value && slot == tag_value)));
    5214     68561890 :   TREE_VISITED (t) = true;
    5215     68561890 :   slot = tag;
    5216              : 
    5217     68561890 :   return tag;
    5218              : }
    5219              : 
    5220              : /* Insert T into the backreference array.  Return its back reference
    5221              :    number.  */
    5222              : 
    5223              : int
    5224     19710331 : trees_in::insert (tree t)
    5225              : {
    5226     19710331 :   gcc_checking_assert (t || get_overrun ());
    5227     19710331 :   back_refs.safe_push (t);
    5228     19710331 :   return -(int)back_refs.length ();
    5229              : }
    5230              : 
    5231              : /* A chained set of decls.  */
    5232              : 
    5233              : void
    5234       113652 : trees_out::chained_decls (tree decls)
    5235              : {
    5236       256734 :   for (; decls; decls = DECL_CHAIN (decls))
    5237       143082 :     tree_node (decls);
    5238       113652 :   tree_node (NULL_TREE);
    5239       113652 : }
    5240              : 
    5241              : tree
    5242        53669 : trees_in::chained_decls ()
    5243              : {
    5244        53669 :   tree decls = NULL_TREE;
    5245        53669 :   for (tree *chain = &decls;;)
    5246       128278 :     if (tree decl = tree_node ())
    5247              :       {
    5248        74609 :         if (!DECL_P (decl) || DECL_CHAIN (decl))
    5249              :           {
    5250            0 :             set_overrun ();
    5251            0 :             break;
    5252              :           }
    5253        74609 :         *chain = decl;
    5254        74609 :         chain = &DECL_CHAIN (decl);
    5255              :       }
    5256              :     else
    5257        74609 :       break;
    5258              : 
    5259        53669 :   return decls;
    5260              : }
    5261              : 
    5262              : /* A vector of decls following DECL_CHAIN.  */
    5263              : 
    5264              : void
    5265       298266 : trees_out::vec_chained_decls (tree decls)
    5266              : {
    5267       298266 :   if (streaming_p ())
    5268              :     {
    5269              :       unsigned len = 0;
    5270              : 
    5271       886649 :       for (tree decl = decls; decl; decl = DECL_CHAIN (decl))
    5272       737562 :         len++;
    5273       149087 :       u (len);
    5274              :     }
    5275              : 
    5276      1773800 :   for (tree decl = decls; decl; decl = DECL_CHAIN (decl))
    5277              :     {
    5278       311263 :       if (DECL_IMPLICIT_TYPEDEF_P (decl)
    5279      1487135 :           && TYPE_NAME (TREE_TYPE (decl)) != decl)
    5280              :         /* An anonynmous struct with a typedef name.  An odd thing to
    5281              :            write.  */
    5282            8 :         tree_node (NULL_TREE);
    5283              :       else
    5284      1475526 :         tree_node (decl);
    5285              :     }
    5286       298266 : }
    5287              : 
    5288              : vec<tree, va_heap> *
    5289       125742 : trees_in::vec_chained_decls ()
    5290              : {
    5291       125742 :   vec<tree, va_heap> *v = NULL;
    5292              : 
    5293       125742 :   if (unsigned len = u ())
    5294              :     {
    5295        65981 :       vec_alloc (v, len);
    5296              : 
    5297       787266 :       for (unsigned ix = 0; ix < len; ix++)
    5298              :         {
    5299       721285 :           tree decl = tree_node ();
    5300       721285 :           if (decl && !DECL_P (decl))
    5301              :             {
    5302            0 :               set_overrun ();
    5303            0 :               break;
    5304              :             }
    5305       721285 :           v->quick_push (decl);
    5306              :         }
    5307              : 
    5308        65981 :       if (get_overrun ())
    5309              :         {
    5310            0 :           vec_free (v);
    5311            0 :           v = NULL;
    5312              :         }
    5313              :     }
    5314              : 
    5315       125742 :   return v;
    5316              : }
    5317              : 
    5318              : /* A vector of trees.  */
    5319              : 
    5320              : void
    5321       210398 : trees_out::tree_vec (vec<tree, va_gc> *v)
    5322              : {
    5323       210398 :   unsigned len = vec_safe_length (v);
    5324       210398 :   if (streaming_p ())
    5325       105176 :     u (len);
    5326       270082 :   for (unsigned ix = 0; ix != len; ix++)
    5327        59684 :     tree_node ((*v)[ix]);
    5328       210398 : }
    5329              : 
    5330              : vec<tree, va_gc> *
    5331        88151 : trees_in::tree_vec ()
    5332              : {
    5333        88151 :   vec<tree, va_gc> *v = NULL;
    5334        88151 :   if (unsigned len = u ())
    5335              :     {
    5336        22398 :       vec_alloc (v, len);
    5337        47152 :       for (unsigned ix = 0; ix != len; ix++)
    5338        24754 :         v->quick_push (tree_node ());
    5339              :     }
    5340        88151 :   return v;
    5341              : }
    5342              : 
    5343              : /* A vector of tree pairs.  */
    5344              : 
    5345              : void
    5346         5256 : trees_out::tree_pair_vec (vec<tree_pair_s, va_gc> *v)
    5347              : {
    5348         5256 :   unsigned len = vec_safe_length (v);
    5349         5256 :   if (streaming_p ())
    5350         2628 :     u (len);
    5351         5256 :   if (len)
    5352        27964 :     for (unsigned ix = 0; ix != len; ix++)
    5353              :       {
    5354        22818 :         tree_pair_s const &s = (*v)[ix];
    5355        22818 :         tree_node (s.purpose);
    5356        22818 :         tree_node (s.value);
    5357              :       }
    5358         5256 : }
    5359              : 
    5360              : vec<tree_pair_s, va_gc> *
    5361         2497 : trees_in::tree_pair_vec ()
    5362              : {
    5363         2497 :   vec<tree_pair_s, va_gc> *v = NULL;
    5364         2497 :   if (unsigned len = u ())
    5365              :     {
    5366         2447 :       vec_alloc (v, len);
    5367        13523 :       for (unsigned ix = 0; ix != len; ix++)
    5368              :         {
    5369        11076 :           tree_pair_s s;
    5370        11076 :           s.purpose = tree_node ();
    5371        11076 :           s.value = tree_node ();
    5372        11076 :           v->quick_push (s);
    5373              :       }
    5374              :     }
    5375         2497 :   return v;
    5376              : }
    5377              : 
    5378              : void
    5379       315978 : trees_out::tree_list (tree list, bool has_purpose)
    5380              : {
    5381      1340637 :   for (; list; list = TREE_CHAIN (list))
    5382              :     {
    5383      1024659 :       gcc_checking_assert (TREE_VALUE (list));
    5384      1024659 :       tree_node (TREE_VALUE (list));
    5385      1024659 :       if (has_purpose)
    5386       991047 :         tree_node (TREE_PURPOSE (list));
    5387              :     }
    5388       315978 :   tree_node (NULL_TREE);
    5389       315978 : }
    5390              : 
    5391              : tree
    5392       135652 : trees_in::tree_list (bool has_purpose)
    5393              : {
    5394       135652 :   tree res = NULL_TREE;
    5395              : 
    5396       640448 :   for (tree *chain = &res; tree value = tree_node ();
    5397      1009592 :        chain = &TREE_CHAIN (*chain))
    5398              :     {
    5399       504796 :       tree purpose = has_purpose ? tree_node () : NULL_TREE;
    5400       504796 :       *chain = build_tree_list (purpose, value);
    5401       504796 :     }
    5402              : 
    5403       135652 :   return res;
    5404              : }
    5405              : 
    5406              : #define CASE_OMP_SIMD_CODE \
    5407              :     case OMP_SIMD:                      \
    5408              :     case OMP_STRUCTURED_BLOCK:          \
    5409              :     case OMP_LOOP:                      \
    5410              :     case OMP_ORDERED:                   \
    5411              :     case OMP_TILE:                      \
    5412              :     case OMP_UNROLL
    5413              : #define CASE_OMP_CODE \
    5414              :     case OMP_PARALLEL:                  \
    5415              :     case OMP_TASK:                      \
    5416              :     case OMP_FOR:                       \
    5417              :     case OMP_DISTRIBUTE:                \
    5418              :     case OMP_TASKLOOP:                  \
    5419              :     case OMP_TEAMS:                     \
    5420              :     case OMP_TARGET_DATA:               \
    5421              :     case OMP_TARGET:                    \
    5422              :     case OMP_SECTIONS:                  \
    5423              :     case OMP_CRITICAL:                  \
    5424              :     case OMP_SINGLE:                    \
    5425              :     case OMP_SCOPE:                     \
    5426              :     case OMP_TASKGROUP:                 \
    5427              :     case OMP_MASKED:                    \
    5428              :     case OMP_DISPATCH:                  \
    5429              :     case OMP_INTEROP:                   \
    5430              :     case OMP_MASTER:                    \
    5431              :     case OMP_TARGET_UPDATE:             \
    5432              :     case OMP_TARGET_ENTER_DATA:         \
    5433              :     case OMP_TARGET_EXIT_DATA:          \
    5434              :     case OMP_METADIRECTIVE:             \
    5435              :     case OMP_ATOMIC:                    \
    5436              :     case OMP_ATOMIC_READ:               \
    5437              :     case OMP_ATOMIC_CAPTURE_OLD:        \
    5438              :     case OMP_ATOMIC_CAPTURE_NEW
    5439              : #define CASE_OACC_CODE \
    5440              :     case OACC_PARALLEL:                 \
    5441              :     case OACC_KERNELS:                  \
    5442              :     case OACC_SERIAL:                   \
    5443              :     case OACC_DATA:                     \
    5444              :     case OACC_HOST_DATA:                \
    5445              :     case OACC_LOOP:                     \
    5446              :     case OACC_CACHE:                    \
    5447              :     case OACC_DECLARE:                  \
    5448              :     case OACC_ENTER_DATA:               \
    5449              :     case OACC_EXIT_DATA:                \
    5450              :     case OACC_UPDATE
    5451              : 
    5452              : /* Start tree write.  Write information to allocate the receiving
    5453              :    node.  */
    5454              : 
    5455              : void
    5456     13883233 : trees_out::start (tree t, bool code_streamed)
    5457              : {
    5458     13883233 :   if (TYPE_P (t))
    5459              :     {
    5460       553848 :       enum tree_code code = TREE_CODE (t);
    5461       553848 :       gcc_checking_assert (TYPE_MAIN_VARIANT (t) == t);
    5462              :       /* All these types are TYPE_NON_COMMON.  */
    5463       553848 :       gcc_checking_assert (code == RECORD_TYPE
    5464              :                            || code == UNION_TYPE
    5465              :                            || code == ENUMERAL_TYPE
    5466              :                            || code == TEMPLATE_TYPE_PARM
    5467              :                            || code == TEMPLATE_TEMPLATE_PARM
    5468              :                            || code == BOUND_TEMPLATE_TEMPLATE_PARM);
    5469              :     }
    5470              : 
    5471     13883233 :   if (!code_streamed)
    5472     13395669 :     u (TREE_CODE (t));
    5473              : 
    5474     13883233 :   switch (TREE_CODE (t))
    5475              :     {
    5476     12332064 :     default:
    5477     12332064 :       if (VL_EXP_CLASS_P (t))
    5478       523666 :         u (VL_EXP_OPERAND_LENGTH (t));
    5479              :       break;
    5480              : 
    5481       620550 :     case INTEGER_CST:
    5482       620550 :       u (TREE_INT_CST_NUNITS (t));
    5483       620550 :       u (TREE_INT_CST_EXT_NUNITS (t));
    5484       620550 :       break;
    5485              : 
    5486           18 :     case OMP_CLAUSE:
    5487           18 :       u (OMP_CLAUSE_CODE (t));
    5488           18 :       break;
    5489              : 
    5490            6 :     CASE_OMP_SIMD_CODE:
    5491            6 :       state->extensions |= SE_OPENMP_SIMD;
    5492            6 :       break;
    5493              : 
    5494            9 :     CASE_OMP_CODE:
    5495            9 :       state->extensions |= SE_OPENMP;
    5496            9 :       break;
    5497              : 
    5498            6 :     CASE_OACC_CODE:
    5499            6 :       state->extensions |= SE_OPENACC;
    5500            6 :       break;
    5501              : 
    5502        45261 :     case STRING_CST:
    5503        45261 :       str (TREE_STRING_POINTER (t), TREE_STRING_LENGTH (t));
    5504        45261 :       break;
    5505              : 
    5506           18 :     case RAW_DATA_CST:
    5507           18 :       if (RAW_DATA_OWNER (t) == NULL_TREE)
    5508              :         {
    5509              :           /* Stream RAW_DATA_CST with no owner (i.e. data pointing
    5510              :              into libcpp buffers) as something we can stream in as
    5511              :              STRING_CST which owns the data.  */
    5512            6 :           u (0);
    5513              :           /* Can't use str (RAW_DATA_POINTER (t), RAW_DATA_LENGTH (t));
    5514              :              here as there isn't a null termination after it.  */
    5515            6 :           z (RAW_DATA_LENGTH (t));
    5516            6 :           if (RAW_DATA_LENGTH (t))
    5517            6 :             if (void *ptr = buf (RAW_DATA_LENGTH (t) + 1))
    5518              :               {
    5519            6 :                 memcpy (ptr, RAW_DATA_POINTER (t), RAW_DATA_LENGTH (t));
    5520            6 :                 ((char *) ptr)[RAW_DATA_LENGTH (t)] = '\0';
    5521              :               }
    5522              :         }
    5523              :       else
    5524              :         {
    5525           12 :           gcc_assert (RAW_DATA_LENGTH (t));
    5526           12 :           u (RAW_DATA_LENGTH (t));
    5527              :         }
    5528              :       break;
    5529              : 
    5530           18 :     case VECTOR_CST:
    5531           18 :       u (VECTOR_CST_LOG2_NPATTERNS (t));
    5532           18 :       u (VECTOR_CST_NELTS_PER_PATTERN (t));
    5533           18 :       break;
    5534              : 
    5535       102548 :     case TREE_BINFO:
    5536       102548 :       u (BINFO_N_BASE_BINFOS (t));
    5537       102548 :       break;
    5538              : 
    5539       782735 :     case TREE_VEC:
    5540       782735 :       u (TREE_VEC_LENGTH (t));
    5541       782735 :       break;
    5542              : 
    5543            0 :     case FIXED_CST:
    5544            0 :       gcc_unreachable (); /* Not supported in C++.  */
    5545            0 :       break;
    5546              : 
    5547            0 :     case IDENTIFIER_NODE:
    5548            0 :     case SSA_NAME:
    5549            0 :     case TARGET_MEM_REF:
    5550            0 :     case TRANSLATION_UNIT_DECL:
    5551              :       /* We shouldn't meet these.  */
    5552            0 :       gcc_unreachable ();
    5553     13883233 :       break;
    5554              :     }
    5555     13883233 : }
    5556              : 
    5557              : /* Start tree read.  Allocate the receiving node.  */
    5558              : 
    5559              : tree
    5560     14057501 : trees_in::start (unsigned code)
    5561              : {
    5562     14057501 :   tree t = NULL_TREE;
    5563              : 
    5564     14057501 :   if (!code)
    5565     12736724 :     code = u ();
    5566              : 
    5567     14057501 :   switch (code)
    5568              :     {
    5569     12565835 :     default:
    5570     12565835 :       if (code >= MAX_TREE_CODES)
    5571              :         {
    5572            0 :         fail:
    5573            0 :           set_overrun ();
    5574            0 :           return NULL_TREE;
    5575              :         }
    5576     12565835 :       else if (TREE_CODE_CLASS (code) == tcc_vl_exp)
    5577              :         {
    5578       544757 :           unsigned ops = u ();
    5579       544757 :           t = build_vl_exp (tree_code (code), ops);
    5580              :         }
    5581              :       else
    5582     12021078 :         t = make_node (tree_code (code));
    5583              :       break;
    5584              : 
    5585       576712 :     case INTEGER_CST:
    5586       576712 :       {
    5587       576712 :         unsigned n = u ();
    5588       576712 :         unsigned e = u ();
    5589       576712 :         t = make_int_cst (n, e);
    5590              :       }
    5591       576712 :       break;
    5592              : 
    5593           18 :     case OMP_CLAUSE:
    5594           18 :       t = build_omp_clause (UNKNOWN_LOCATION, omp_clause_code (u ()));
    5595           18 :       break;
    5596              : 
    5597            9 :     CASE_OMP_SIMD_CODE:
    5598            9 :       if (!(state->extensions & SE_OPENMP_SIMD))
    5599            0 :         goto fail;
    5600            9 :       t = make_node (tree_code (code));
    5601            9 :       break;
    5602              : 
    5603            9 :     CASE_OMP_CODE:
    5604            9 :       if (!(state->extensions & SE_OPENMP))
    5605            0 :         goto fail;
    5606            9 :       t = make_node (tree_code (code));
    5607            9 :       break;
    5608              : 
    5609            6 :     CASE_OACC_CODE:
    5610            6 :       if (!(state->extensions & SE_OPENACC))
    5611            0 :         goto fail;
    5612            6 :       t = make_node (tree_code (code));
    5613            6 :       break;
    5614              : 
    5615        48296 :     case STRING_CST:
    5616        48296 :       {
    5617        48296 :         size_t l;
    5618        48296 :         const char *chars = str (&l);
    5619        48296 :         t = build_string (l, chars);
    5620              :       }
    5621        48296 :       break;
    5622              : 
    5623            9 :     case RAW_DATA_CST:
    5624            9 :       {
    5625            9 :         size_t l = u ();
    5626            9 :         if (l == 0)
    5627              :           {
    5628              :             /* Stream in RAW_DATA_CST with no owner as STRING_CST
    5629              :                which owns the data.  */
    5630            3 :             const char *chars = str (&l);
    5631            3 :             t = build_string (l, chars);
    5632              :           }
    5633              :         else
    5634              :           {
    5635            6 :             t = make_node (RAW_DATA_CST);
    5636            6 :             RAW_DATA_LENGTH (t) = l;
    5637              :           }
    5638              :       }
    5639            9 :       break;
    5640              : 
    5641           24 :     case VECTOR_CST:
    5642           24 :       {
    5643           24 :         unsigned log2_npats = u ();
    5644           24 :         unsigned elts_per = u ();
    5645           24 :         t = make_vector (log2_npats, elts_per);
    5646              :       }
    5647           24 :       break;
    5648              : 
    5649        85654 :     case TREE_BINFO:
    5650        85654 :       t = make_tree_binfo (u ());
    5651        85654 :       break;
    5652              : 
    5653       780929 :     case TREE_VEC:
    5654       780929 :       t = make_tree_vec (u ());
    5655       780929 :       break;
    5656              : 
    5657            0 :     case FIXED_CST:
    5658            0 :     case IDENTIFIER_NODE:
    5659            0 :     case SSA_NAME:
    5660            0 :     case TARGET_MEM_REF:
    5661            0 :     case TRANSLATION_UNIT_DECL:
    5662            0 :       goto fail;
    5663              :     }
    5664              : 
    5665              :   return t;
    5666              : }
    5667              : 
    5668              : /* The kinds of interface an importer could have for a decl.  */
    5669              : 
    5670              : enum class importer_interface {
    5671              :   unknown,        /* The definition may or may not need to be emitted.  */
    5672              :   external,       /* The definition can always be found in another TU.  */
    5673              :   internal,       /* The definition should be emitted in the importer's TU.  */
    5674              :   always_emit,    /* The definition must be emitted in the importer's TU,
    5675              :                      regardless of if it's used or not. */
    5676              : };
    5677              : 
    5678              : /* Returns what kind of interface an importer will have of DECL.  */
    5679              : 
    5680              : static importer_interface
    5681       516354 : get_importer_interface (tree decl)
    5682              : {
    5683              :   /* Internal linkage entities must be emitted in each importer if
    5684              :      there is a definition available.  */
    5685       516354 :   if (!TREE_PUBLIC (decl))
    5686              :     return importer_interface::internal;
    5687              : 
    5688              :   /* Other entities that aren't vague linkage are either not definitions
    5689              :      or will be publicly emitted in this TU, so importers can just refer
    5690              :      to an external definition.  */
    5691       261727 :   if (!vague_linkage_p (decl))
    5692              :     return importer_interface::external;
    5693              : 
    5694              :   /* For explicit instantiations, importers can always rely on there
    5695              :      being a definition in another TU, unless this is a definition
    5696              :      in a header module: in which case the importer will always need
    5697              :      to emit it.  */
    5698       256252 :   if (DECL_LANG_SPECIFIC (decl)
    5699       256252 :       && DECL_EXPLICIT_INSTANTIATION (decl))
    5700        19999 :     return (header_module_p () && !DECL_EXTERNAL (decl)
    5701        19999 :             ? importer_interface::always_emit
    5702              :             : importer_interface::external);
    5703              : 
    5704              :   /* A gnu_inline function is never emitted in any TU.  */
    5705       236253 :   if (TREE_CODE (decl) == FUNCTION_DECL
    5706       165568 :       && DECL_DECLARED_INLINE_P (decl)
    5707       396251 :       && lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl)))
    5708              :     return importer_interface::external;
    5709              : 
    5710              :   /* Everything else has vague linkage.  */
    5711              :   return importer_interface::unknown;
    5712              : }
    5713              : 
    5714              : /* The structure streamers access the raw fields, because the
    5715              :    alternative, of using the accessor macros can require using
    5716              :    different accessors for the same underlying field, depending on the
    5717              :    tree code.  That's both confusing and annoying.  */
    5718              : 
    5719              : /* Read & write the core boolean flags.  */
    5720              : 
    5721              : void
    5722     13905950 : trees_out::core_bools (tree t, bits_out& bits)
    5723              : {
    5724              : #define WB(X) (bits.b (X))
    5725              : /* Stream X if COND holds, and if !COND stream a dummy value so that the
    5726              :    overall number of bits streamed is independent of the runtime value
    5727              :    of COND, which allows the compiler to better optimize this function.  */
    5728              : #define WB_IF(COND, X) WB ((COND) ? (X) : false)
    5729     13905950 :   tree_code code = TREE_CODE (t);
    5730              : 
    5731     13905950 :   WB (t->base.side_effects_flag);
    5732     13905950 :   WB (t->base.constant_flag);
    5733     13905950 :   WB (t->base.addressable_flag);
    5734     13905950 :   WB (t->base.volatile_flag);
    5735     13905950 :   WB (t->base.readonly_flag);
    5736              :   /* base.asm_written_flag is a property of the current TU's use of
    5737              :      this decl.  */
    5738     13905950 :   WB (t->base.nowarning_flag);
    5739              :   /* base.visited read as zero (it's set for writer, because that's
    5740              :      how we mark nodes).  */
    5741              :   /* base.used_flag is not streamed.  Readers may set TREE_USED of
    5742              :      decls they use.  */
    5743     13905950 :   WB (t->base.nothrow_flag);
    5744     13905950 :   WB (t->base.static_flag);
    5745              :   /* This is TYPE_CACHED_VALUES_P for types.  */
    5746     13905950 :   WB_IF (TREE_CODE_CLASS (code) != tcc_type, t->base.public_flag);
    5747     13905950 :   WB (t->base.private_flag);
    5748     13905950 :   WB (t->base.protected_flag);
    5749     13905950 :   WB (t->base.deprecated_flag);
    5750     13905950 :   WB (t->base.default_def_flag);
    5751              : 
    5752     13905950 :   switch (code)
    5753              :     {
    5754              :     case CALL_EXPR:
    5755              :     case INTEGER_CST:
    5756              :     case SSA_NAME:
    5757              :     case TARGET_MEM_REF:
    5758              :     case TREE_VEC:
    5759              :       /* These use different base.u fields.  */
    5760              :       return;
    5761              : 
    5762     11991096 :     default:
    5763     11991096 :       WB (t->base.u.bits.lang_flag_0);
    5764     11991096 :       bool flag_1 = t->base.u.bits.lang_flag_1;
    5765     11991096 :       if (!flag_1)
    5766              :         ;
    5767       383776 :       else if (code == TEMPLATE_INFO)
    5768              :         /* This is TI_PENDING_TEMPLATE_FLAG, not relevant to reader.  */
    5769              :         flag_1 = false;
    5770       379107 :       else if (code == VAR_DECL)
    5771              :         {
    5772              :           /* This is DECL_INITIALIZED_P.  */
    5773        73261 :           if (TREE_CODE (DECL_CONTEXT (t)) != FUNCTION_DECL)
    5774              :             /* We'll set this when reading the definition.  */
    5775     11991096 :             flag_1 = false;
    5776              :         }
    5777     11991096 :       WB (flag_1);
    5778     11991096 :       WB (t->base.u.bits.lang_flag_2);
    5779     11991096 :       WB (t->base.u.bits.lang_flag_3);
    5780     11991096 :       WB (t->base.u.bits.lang_flag_4);
    5781     11991096 :       WB (t->base.u.bits.lang_flag_5);
    5782     11991096 :       WB (t->base.u.bits.lang_flag_6);
    5783     11991096 :       WB (t->base.u.bits.saturating_flag);
    5784     11991096 :       WB (t->base.u.bits.unsigned_flag);
    5785     11991096 :       WB (t->base.u.bits.packed_flag);
    5786     11991096 :       WB (t->base.u.bits.user_align);
    5787     11991096 :       WB (t->base.u.bits.nameless_flag);
    5788     11991096 :       WB (t->base.u.bits.atomic_flag);
    5789     11991096 :       WB (t->base.u.bits.unavailable_flag);
    5790     11991096 :       break;
    5791              :     }
    5792              : 
    5793     11991096 :   if (TREE_CODE_CLASS (code) == tcc_type)
    5794              :     {
    5795       576565 :       WB (t->type_common.no_force_blk_flag);
    5796       576565 :       WB (t->type_common.needs_constructing_flag);
    5797       576565 :       WB (t->type_common.transparent_aggr_flag);
    5798       576565 :       WB (t->type_common.restrict_flag);
    5799       576565 :       WB (t->type_common.string_flag);
    5800       576565 :       WB (t->type_common.lang_flag_0);
    5801       576565 :       WB (t->type_common.lang_flag_1);
    5802       576565 :       WB (t->type_common.lang_flag_2);
    5803       576565 :       WB (t->type_common.lang_flag_3);
    5804       576565 :       WB (t->type_common.lang_flag_4);
    5805       576565 :       WB (t->type_common.lang_flag_5);
    5806       576565 :       WB (t->type_common.lang_flag_6);
    5807       576565 :       WB (t->type_common.typeless_storage);
    5808              :     }
    5809              : 
    5810     11991096 :   if (TREE_CODE_CLASS (code) != tcc_declaration)
    5811              :     return;
    5812              : 
    5813      3075812 :   if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
    5814              :     {
    5815      3075812 :       WB (t->decl_common.nonlocal_flag);
    5816      3075812 :       WB (t->decl_common.virtual_flag);
    5817      3075812 :       WB (t->decl_common.ignored_flag);
    5818      3075812 :       WB (t->decl_common.abstract_flag);
    5819      3075812 :       WB (t->decl_common.artificial_flag);
    5820      3075812 :       WB (t->decl_common.preserve_flag);
    5821      3075812 :       WB (t->decl_common.debug_expr_is_from);
    5822      3075812 :       WB (t->decl_common.lang_flag_0);
    5823      3075812 :       WB (t->decl_common.lang_flag_1);
    5824      3075812 :       WB (t->decl_common.lang_flag_2);
    5825      3075812 :       WB (t->decl_common.lang_flag_3);
    5826      3075812 :       WB (t->decl_common.lang_flag_4);
    5827              : 
    5828      3075812 :       {
    5829              :         /* This is DECL_INTERFACE_KNOWN: We should redetermine whether
    5830              :            we need to import or export any vague-linkage entities on
    5831              :            stream-in.  */
    5832      3075812 :         bool interface_known = t->decl_common.lang_flag_5;
    5833      3075812 :         if (interface_known
    5834      3075812 :             && get_importer_interface (t) == importer_interface::unknown)
    5835              :           interface_known = false;
    5836      3075812 :         WB (interface_known);
    5837              :       }
    5838              : 
    5839      3075812 :       WB (t->decl_common.lang_flag_6);
    5840      3075812 :       WB (t->decl_common.lang_flag_7);
    5841      3075812 :       WB (t->decl_common.lang_flag_8);
    5842      3075812 :       WB (t->decl_common.decl_flag_0);
    5843              : 
    5844      3075812 :       {
    5845              :         /* DECL_EXTERNAL -> decl_flag_1
    5846              :              == it is defined elsewhere
    5847              :            DECL_NOT_REALLY_EXTERN -> base.not_really_extern
    5848              :              == that was a lie, it is here  */
    5849              : 
    5850      3075812 :         bool is_external = t->decl_common.decl_flag_1;
    5851              :         /* maybe_emit_vtables relies on vtables being marked as
    5852              :            DECL_EXTERNAL and DECL_NOT_REALLY_EXTERN before processing.  */
    5853      3075812 :         if (!is_external && VAR_P (t) && DECL_VTABLE_OR_VTT_P (t))
    5854              :           is_external = true;
    5855              :         /* Things we emit here might well be external from the POV of an
    5856              :            importer.  */
    5857      3075614 :         if (!is_external
    5858      2581500 :             && VAR_OR_FUNCTION_DECL_P (t)
    5859      3304425 :             && get_importer_interface (t) == importer_interface::external)
    5860              :           is_external = true;
    5861      3075812 :         WB (is_external);
    5862              :       }
    5863              : 
    5864      3075812 :       WB (t->decl_common.decl_flag_2);
    5865      3075812 :       WB (t->decl_common.decl_flag_3);
    5866      3075812 :       WB (t->decl_common.not_gimple_reg_flag);
    5867      3075812 :       WB (t->decl_common.decl_by_reference_flag);
    5868      3075812 :       WB (t->decl_common.decl_read_flag);
    5869      3075812 :       WB (t->decl_common.decl_nonshareable_flag);
    5870      3075812 :       WB (t->decl_common.decl_not_flexarray);
    5871              :     }
    5872              :   else
    5873              :     return;
    5874              : 
    5875      3075812 :   if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
    5876              :     {
    5877      1454344 :       WB (t->decl_with_vis.defer_output);
    5878      1454344 :       WB (t->decl_with_vis.hard_register);
    5879      1454344 :       WB (t->decl_with_vis.common_flag);
    5880      1454344 :       WB (t->decl_with_vis.in_text_section);
    5881      1454344 :       WB (t->decl_with_vis.in_constant_pool);
    5882      1454344 :       WB (t->decl_with_vis.dllimport_flag);
    5883      1454344 :       WB (t->decl_with_vis.weak_flag);
    5884      1454344 :       WB (t->decl_with_vis.seen_in_bind_expr);
    5885      1454344 :       WB (t->decl_with_vis.comdat_flag);
    5886      1454344 :       WB (t->decl_with_vis.visibility_specified);
    5887      1454344 :       WB (t->decl_with_vis.init_priority_p);
    5888      1454344 :       WB (t->decl_with_vis.shadowed_for_var_p);
    5889      1454344 :       WB (t->decl_with_vis.cxx_constructor);
    5890      1454344 :       WB (t->decl_with_vis.cxx_destructor);
    5891      1454344 :       WB (t->decl_with_vis.final);
    5892      1454344 :       WB (t->decl_with_vis.regdecl_flag);
    5893              :     }
    5894              :   else
    5895              :     return;
    5896              : 
    5897      1454344 :   if (CODE_CONTAINS_STRUCT (code, TS_FUNCTION_DECL))
    5898              :     {
    5899       424500 :       WB (t->function_decl.static_ctor_flag);
    5900       424500 :       WB (t->function_decl.static_dtor_flag);
    5901       424500 :       WB (t->function_decl.uninlinable);
    5902       424500 :       WB (t->function_decl.possibly_inlined);
    5903       424500 :       WB (t->function_decl.novops_flag);
    5904       424500 :       WB (t->function_decl.returns_twice_flag);
    5905       424500 :       WB (t->function_decl.malloc_flag);
    5906       424500 :       WB (t->function_decl.declared_inline_flag);
    5907       424500 :       WB (t->function_decl.no_inline_warning_flag);
    5908       424500 :       WB (t->function_decl.no_instrument_function_entry_exit);
    5909       424500 :       WB (t->function_decl.no_limit_stack);
    5910       424500 :       WB (t->function_decl.disregard_inline_limits);
    5911       424500 :       WB (t->function_decl.pure_flag);
    5912       424500 :       WB (t->function_decl.looping_const_or_pure_flag);
    5913              : 
    5914       424500 :       WB (t->function_decl.has_debug_args_flag);
    5915       424500 :       WB (t->function_decl.versioned_function);
    5916       424500 :       WB (t->function_decl.replaceable_operator);
    5917              : 
    5918              :       /* decl_type is a (misnamed) 2 bit discriminator.  */
    5919       424500 :       unsigned kind = (unsigned)t->function_decl.decl_type;
    5920       424500 :       WB ((kind >> 0) & 1);
    5921       424500 :       WB ((kind >> 1) & 1);
    5922              :     }
    5923              : #undef WB_IF
    5924              : #undef WB
    5925              : }
    5926              : 
    5927              : bool
    5928     14077627 : trees_in::core_bools (tree t, bits_in& bits)
    5929              : {
    5930              : #define RB(X) ((X) = bits.b ())
    5931              : /* See the comment for WB_IF in trees_out::core_bools.  */
    5932              : #define RB_IF(COND, X) ((COND) ? RB (X) : bits.b ())
    5933              : 
    5934     14077627 :   tree_code code = TREE_CODE (t);
    5935              : 
    5936     14077627 :   RB (t->base.side_effects_flag);
    5937     14077627 :   RB (t->base.constant_flag);
    5938     14077627 :   RB (t->base.addressable_flag);
    5939     14077627 :   RB (t->base.volatile_flag);
    5940     14077627 :   RB (t->base.readonly_flag);
    5941              :   /* base.asm_written_flag is not streamed.  */
    5942     14077627 :   RB (t->base.nowarning_flag);
    5943              :   /* base.visited is not streamed.  */
    5944              :   /* base.used_flag is not streamed.  */
    5945     14077627 :   RB (t->base.nothrow_flag);
    5946     14077627 :   RB (t->base.static_flag);
    5947     14077627 :   RB_IF (TREE_CODE_CLASS (code) != tcc_type, t->base.public_flag);
    5948     14077627 :   RB (t->base.private_flag);
    5949     14077627 :   RB (t->base.protected_flag);
    5950     14077627 :   RB (t->base.deprecated_flag);
    5951     14077627 :   RB (t->base.default_def_flag);
    5952              : 
    5953     14077627 :   switch (code)
    5954              :     {
    5955      1890391 :     case CALL_EXPR:
    5956      1890391 :     case INTEGER_CST:
    5957      1890391 :     case SSA_NAME:
    5958      1890391 :     case TARGET_MEM_REF:
    5959      1890391 :     case TREE_VEC:
    5960              :       /* These use different base.u fields.  */
    5961      1890391 :       goto done;
    5962              : 
    5963     12187236 :     default:
    5964     12187236 :       RB (t->base.u.bits.lang_flag_0);
    5965     12187236 :       RB (t->base.u.bits.lang_flag_1);
    5966     12187236 :       RB (t->base.u.bits.lang_flag_2);
    5967     12187236 :       RB (t->base.u.bits.lang_flag_3);
    5968     12187236 :       RB (t->base.u.bits.lang_flag_4);
    5969     12187236 :       RB (t->base.u.bits.lang_flag_5);
    5970     12187236 :       RB (t->base.u.bits.lang_flag_6);
    5971     12187236 :       RB (t->base.u.bits.saturating_flag);
    5972     12187236 :       RB (t->base.u.bits.unsigned_flag);
    5973     12187236 :       RB (t->base.u.bits.packed_flag);
    5974     12187236 :       RB (t->base.u.bits.user_align);
    5975     12187236 :       RB (t->base.u.bits.nameless_flag);
    5976     12187236 :       RB (t->base.u.bits.atomic_flag);
    5977     12187236 :       RB (t->base.u.bits.unavailable_flag);
    5978     12187236 :       break;
    5979              :     }
    5980              : 
    5981     12187236 :   if (TREE_CODE_CLASS (code) == tcc_type)
    5982              :     {
    5983       536648 :       RB (t->type_common.no_force_blk_flag);
    5984       536648 :       RB (t->type_common.needs_constructing_flag);
    5985       536648 :       RB (t->type_common.transparent_aggr_flag);
    5986       536648 :       RB (t->type_common.restrict_flag);
    5987       536648 :       RB (t->type_common.string_flag);
    5988       536648 :       RB (t->type_common.lang_flag_0);
    5989       536648 :       RB (t->type_common.lang_flag_1);
    5990       536648 :       RB (t->type_common.lang_flag_2);
    5991       536648 :       RB (t->type_common.lang_flag_3);
    5992       536648 :       RB (t->type_common.lang_flag_4);
    5993       536648 :       RB (t->type_common.lang_flag_5);
    5994       536648 :       RB (t->type_common.lang_flag_6);
    5995       536648 :       RB (t->type_common.typeless_storage);
    5996              :     }
    5997              : 
    5998     12187236 :   if (TREE_CODE_CLASS (code) != tcc_declaration)
    5999      9172203 :     goto done;
    6000              : 
    6001      3015033 :   if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
    6002              :     {
    6003      3015033 :       RB (t->decl_common.nonlocal_flag);
    6004      3015033 :       RB (t->decl_common.virtual_flag);
    6005      3015033 :       RB (t->decl_common.ignored_flag);
    6006      3015033 :       RB (t->decl_common.abstract_flag);
    6007      3015033 :       RB (t->decl_common.artificial_flag);
    6008      3015033 :       RB (t->decl_common.preserve_flag);
    6009      3015033 :       RB (t->decl_common.debug_expr_is_from);
    6010      3015033 :       RB (t->decl_common.lang_flag_0);
    6011      3015033 :       RB (t->decl_common.lang_flag_1);
    6012      3015033 :       RB (t->decl_common.lang_flag_2);
    6013      3015033 :       RB (t->decl_common.lang_flag_3);
    6014      3015033 :       RB (t->decl_common.lang_flag_4);
    6015      3015033 :       RB (t->decl_common.lang_flag_5);
    6016      3015033 :       RB (t->decl_common.lang_flag_6);
    6017      3015033 :       RB (t->decl_common.lang_flag_7);
    6018      3015033 :       RB (t->decl_common.lang_flag_8);
    6019      3015033 :       RB (t->decl_common.decl_flag_0);
    6020      3015033 :       RB (t->decl_common.decl_flag_1);
    6021      3015033 :       RB (t->decl_common.decl_flag_2);
    6022      3015033 :       RB (t->decl_common.decl_flag_3);
    6023      3015033 :       RB (t->decl_common.not_gimple_reg_flag);
    6024      3015033 :       RB (t->decl_common.decl_by_reference_flag);
    6025      3015033 :       RB (t->decl_common.decl_read_flag);
    6026      3015033 :       RB (t->decl_common.decl_nonshareable_flag);
    6027      3015033 :       RB (t->decl_common.decl_not_flexarray);
    6028              :     }
    6029              :   else
    6030            0 :     goto done;
    6031              : 
    6032      3015033 :   if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
    6033              :     {
    6034      1393999 :       RB (t->decl_with_vis.defer_output);
    6035      1393999 :       RB (t->decl_with_vis.hard_register);
    6036      1393999 :       RB (t->decl_with_vis.common_flag);
    6037      1393999 :       RB (t->decl_with_vis.in_text_section);
    6038      1393999 :       RB (t->decl_with_vis.in_constant_pool);
    6039      1393999 :       RB (t->decl_with_vis.dllimport_flag);
    6040      1393999 :       RB (t->decl_with_vis.weak_flag);
    6041      1393999 :       RB (t->decl_with_vis.seen_in_bind_expr);
    6042      1393999 :       RB (t->decl_with_vis.comdat_flag);
    6043      1393999 :       RB (t->decl_with_vis.visibility_specified);
    6044      1393999 :       RB (t->decl_with_vis.init_priority_p);
    6045      1393999 :       RB (t->decl_with_vis.shadowed_for_var_p);
    6046      1393999 :       RB (t->decl_with_vis.cxx_constructor);
    6047      1393999 :       RB (t->decl_with_vis.cxx_destructor);
    6048      1393999 :       RB (t->decl_with_vis.final);
    6049      1393999 :       RB (t->decl_with_vis.regdecl_flag);
    6050              :     }
    6051              :   else
    6052      1621034 :     goto done;
    6053              : 
    6054      1393999 :   if (CODE_CONTAINS_STRUCT (code, TS_FUNCTION_DECL))
    6055              :     {
    6056       425412 :       RB (t->function_decl.static_ctor_flag);
    6057       425412 :       RB (t->function_decl.static_dtor_flag);
    6058       425412 :       RB (t->function_decl.uninlinable);
    6059       425412 :       RB (t->function_decl.possibly_inlined);
    6060       425412 :       RB (t->function_decl.novops_flag);
    6061       425412 :       RB (t->function_decl.returns_twice_flag);
    6062       425412 :       RB (t->function_decl.malloc_flag);
    6063       425412 :       RB (t->function_decl.declared_inline_flag);
    6064       425412 :       RB (t->function_decl.no_inline_warning_flag);
    6065       425412 :       RB (t->function_decl.no_instrument_function_entry_exit);
    6066       425412 :       RB (t->function_decl.no_limit_stack);
    6067       425412 :       RB (t->function_decl.disregard_inline_limits);
    6068       425412 :       RB (t->function_decl.pure_flag);
    6069       425412 :       RB (t->function_decl.looping_const_or_pure_flag);
    6070              : 
    6071       425412 :       RB (t->function_decl.has_debug_args_flag);
    6072       425412 :       RB (t->function_decl.versioned_function);
    6073       425412 :       RB (t->function_decl.replaceable_operator);
    6074              : 
    6075              :       /* decl_type is a (misnamed) 2 bit discriminator.  */
    6076       425412 :       unsigned kind = 0;
    6077       425412 :       kind |= unsigned (bits.b ()) << 0;
    6078       425412 :       kind |= unsigned (bits.b ()) << 1;
    6079       425412 :       t->function_decl.decl_type = function_decl_type (kind);
    6080              :     }
    6081              : #undef RB_IF
    6082              : #undef RB
    6083       968587 : done:
    6084     14077627 :   return !get_overrun ();
    6085              : }
    6086              : 
    6087              : void
    6088      1967508 : trees_out::lang_decl_bools (tree t, bits_out& bits)
    6089              : {
    6090              : #define WB(X) (bits.b (X))
    6091      1967508 :   const struct lang_decl *lang = DECL_LANG_SPECIFIC (t);
    6092              : 
    6093      1967508 :   bits.bflush ();
    6094      1967508 :   WB (lang->u.base.language == lang_cplusplus);
    6095      1967508 :   WB ((lang->u.base.use_template >> 0) & 1);
    6096      1967508 :   WB ((lang->u.base.use_template >> 1) & 1);
    6097              :   /* Do not write lang->u.base.not_really_extern, importer will set
    6098              :      when reading the definition (if any).  */
    6099      1967508 :   WB (lang->u.base.initialized_in_class);
    6100              : 
    6101      1967508 :   WB (lang->u.base.threadprivate_or_deleted_p);
    6102      1967508 :   WB (lang->u.base.anticipated_p);
    6103      1967508 :   WB (lang->u.base.friend_or_tls);
    6104      1967508 :   WB (lang->u.base.unknown_bound_p);
    6105              :   /* Do not write lang->u.base.odr_used, importer will recalculate if
    6106              :      they do ODR use this decl.  */
    6107      1967508 :   WB (lang->u.base.concept_p);
    6108      1967508 :   WB (lang->u.base.var_declared_inline_p);
    6109      1967508 :   WB (lang->u.base.dependent_init_p);
    6110              : 
    6111              :   /* When building a header unit, everthing is marked as purview, (so
    6112              :      we know which decls to write).  But when we import them we do not
    6113              :      want to mark them as in module purview.  */
    6114      3855523 :   WB (lang->u.base.module_purview_p && !header_module_p ());
    6115      1967508 :   WB (lang->u.base.module_attach_p);
    6116              :   /* Importer will set module_import_p and module_entity_p themselves
    6117              :      as appropriate.  */
    6118      1967508 :   WB (lang->u.base.module_keyed_decls_p);
    6119              : 
    6120      1967508 :   WB (lang->u.base.omp_declare_mapper_p);
    6121              : 
    6122      1967508 :   switch (lang->u.base.selector)
    6123              :     {
    6124            0 :     default:
    6125            0 :       gcc_unreachable ();
    6126              : 
    6127       424500 :     case lds_fn:  /* lang_decl_fn.  */
    6128       424500 :       WB (lang->u.fn.global_ctor_p);
    6129       424500 :       WB (lang->u.fn.global_dtor_p);
    6130              : 
    6131       424500 :       WB (lang->u.fn.static_function);
    6132       424500 :       WB (lang->u.fn.pure_virtual);
    6133       424500 :       WB (lang->u.fn.defaulted_p);
    6134       424500 :       WB (lang->u.fn.has_in_charge_parm_p);
    6135       424500 :       WB (lang->u.fn.has_vtt_parm_p);
    6136              :       /* There shouldn't be a pending inline at this point.  */
    6137       424500 :       gcc_assert (!lang->u.fn.pending_inline_p);
    6138       424500 :       WB (lang->u.fn.nonconverting);
    6139       424500 :       WB (lang->u.fn.thunk_p);
    6140              : 
    6141       424500 :       WB (lang->u.fn.this_thunk_p);
    6142       424500 :       WB (lang->u.fn.omp_declare_reduction_p);
    6143       424500 :       WB (lang->u.fn.has_dependent_explicit_spec_p);
    6144       424500 :       WB (lang->u.fn.immediate_fn_p);
    6145       424500 :       WB (lang->u.fn.maybe_deleted);
    6146       424500 :       WB (lang->u.fn.coroutine_p);
    6147       424500 :       WB (lang->u.fn.implicit_constexpr);
    6148       424500 :       WB (lang->u.fn.escalated_p);
    6149       424500 :       WB (lang->u.fn.xobj_func);
    6150       424500 :       goto lds_min;
    6151              : 
    6152         1660 :     case lds_decomp:  /* lang_decl_decomp.  */
    6153              :       /* No bools.  */
    6154         1660 :       goto lds_min;
    6155              : 
    6156              :     case lds_min:  /* lang_decl_min.  */
    6157      1967508 :     lds_min:
    6158              :       /* No bools.  */
    6159              :       break;
    6160              : 
    6161              :     case lds_ns:  /* lang_decl_ns.  */
    6162              :       /* No bools.  */
    6163              :       break;
    6164              : 
    6165              :     case lds_parm:  /* lang_decl_parm.  */
    6166              :       /* No bools.  */
    6167              :       break;
    6168              :     }
    6169              : #undef WB
    6170      1967508 : }
    6171              : 
    6172              : bool
    6173      1910348 : trees_in::lang_decl_bools (tree t, bits_in& bits)
    6174              : {
    6175              : #define RB(X) ((X) = bits.b ())
    6176      1910348 :   struct lang_decl *lang = DECL_LANG_SPECIFIC (t);
    6177              : 
    6178      1910348 :   bits.bflush ();
    6179      1910348 :   lang->u.base.language = bits.b () ? lang_cplusplus : lang_c;
    6180      1910348 :   unsigned v;
    6181      1910348 :   v = bits.b () << 0;
    6182      1910348 :   v |= bits.b () << 1;
    6183      1910348 :   lang->u.base.use_template = v;
    6184              :   /* lang->u.base.not_really_extern is not streamed.  */
    6185      1910348 :   RB (lang->u.base.initialized_in_class);
    6186              : 
    6187      1910348 :   RB (lang->u.base.threadprivate_or_deleted_p);
    6188      1910348 :   RB (lang->u.base.anticipated_p);
    6189      1910348 :   RB (lang->u.base.friend_or_tls);
    6190      1910348 :   RB (lang->u.base.unknown_bound_p);
    6191              :   /* lang->u.base.odr_used is not streamed.  */
    6192      1910348 :   RB (lang->u.base.concept_p);
    6193      1910348 :   RB (lang->u.base.var_declared_inline_p);
    6194      1910348 :   RB (lang->u.base.dependent_init_p);
    6195              : 
    6196      1910348 :   RB (lang->u.base.module_purview_p);
    6197      1910348 :   RB (lang->u.base.module_attach_p);
    6198              :   /* module_import_p and module_entity_p are not streamed.  */
    6199      1910348 :   RB (lang->u.base.module_keyed_decls_p);
    6200              : 
    6201      1910348 :   RB (lang->u.base.omp_declare_mapper_p);
    6202              : 
    6203      1910348 :   switch (lang->u.base.selector)
    6204              :     {
    6205            0 :     default:
    6206            0 :       gcc_unreachable ();
    6207              : 
    6208       425412 :     case lds_fn:  /* lang_decl_fn.  */
    6209       425412 :       RB (lang->u.fn.global_ctor_p);
    6210       425412 :       RB (lang->u.fn.global_dtor_p);
    6211              : 
    6212       425412 :       RB (lang->u.fn.static_function);
    6213       425412 :       RB (lang->u.fn.pure_virtual);
    6214       425412 :       RB (lang->u.fn.defaulted_p);
    6215       425412 :       RB (lang->u.fn.has_in_charge_parm_p);
    6216       425412 :       RB (lang->u.fn.has_vtt_parm_p);
    6217              :       /* lang->u.f.n.pending_inline_p is not streamed.  */
    6218       425412 :       RB (lang->u.fn.nonconverting);
    6219       425412 :       RB (lang->u.fn.thunk_p);
    6220              : 
    6221       425412 :       RB (lang->u.fn.this_thunk_p);
    6222       425412 :       RB (lang->u.fn.omp_declare_reduction_p);
    6223       425412 :       RB (lang->u.fn.has_dependent_explicit_spec_p);
    6224       425412 :       RB (lang->u.fn.immediate_fn_p);
    6225       425412 :       RB (lang->u.fn.maybe_deleted);
    6226       425412 :       RB (lang->u.fn.coroutine_p);
    6227       425412 :       RB (lang->u.fn.implicit_constexpr);
    6228       425412 :       RB (lang->u.fn.escalated_p);
    6229       425412 :       RB (lang->u.fn.xobj_func);
    6230       425412 :       goto lds_min;
    6231              : 
    6232         1981 :     case lds_decomp:  /* lang_decl_decomp.  */
    6233              :       /* No bools.  */
    6234         1981 :       goto lds_min;
    6235              : 
    6236              :     case lds_min:  /* lang_decl_min.  */
    6237      1910348 :     lds_min:
    6238              :       /* No bools.  */
    6239              :       break;
    6240              : 
    6241              :     case lds_ns:  /* lang_decl_ns.  */
    6242              :       /* No bools.  */
    6243              :       break;
    6244              : 
    6245              :     case lds_parm:  /* lang_decl_parm.  */
    6246              :       /* No bools.  */
    6247              :       break;
    6248              :     }
    6249              : #undef RB
    6250      1910348 :   return !get_overrun ();
    6251              : }
    6252              : 
    6253              : void
    6254       154344 : trees_out::lang_type_bools (tree t, bits_out& bits)
    6255              : {
    6256              : #define WB(X) (bits.b (X))
    6257       154344 :   const struct lang_type *lang = TYPE_LANG_SPECIFIC (t);
    6258              : 
    6259       154344 :   bits.bflush ();
    6260       154344 :   WB (lang->has_type_conversion);
    6261       154344 :   WB (lang->has_copy_ctor);
    6262       154344 :   WB (lang->has_default_ctor);
    6263       154344 :   WB (lang->const_needs_init);
    6264       154344 :   WB (lang->ref_needs_init);
    6265       154344 :   WB (lang->has_const_copy_assign);
    6266       154344 :   WB ((lang->use_template >> 0) & 1);
    6267       154344 :   WB ((lang->use_template >> 1) & 1);
    6268              : 
    6269       154344 :   WB (lang->has_mutable);
    6270       154344 :   WB (lang->com_interface);
    6271       154344 :   WB (lang->non_pod_class);
    6272       154344 :   WB (lang->nearly_empty_p);
    6273       154344 :   WB (lang->user_align);
    6274       154344 :   WB (lang->has_copy_assign);
    6275       154344 :   WB (lang->has_new);
    6276       154344 :   WB (lang->has_array_new);
    6277              : 
    6278       154344 :   WB ((lang->gets_delete >> 0) & 1);
    6279       154344 :   WB ((lang->gets_delete >> 1) & 1);
    6280       154344 :   WB (lang->interface_only);
    6281       154344 :   WB (lang->interface_unknown);
    6282       154344 :   WB (lang->contains_empty_class_p);
    6283       154344 :   WB (lang->anon_aggr);
    6284       154344 :   WB (lang->non_zero_init);
    6285       154344 :   WB (lang->empty_p);
    6286              : 
    6287       154344 :   WB (lang->vec_new_uses_cookie);
    6288       154344 :   WB (lang->declared_class);
    6289       154344 :   WB (lang->diamond_shaped);
    6290       154344 :   WB (lang->repeated_base);
    6291       154344 :   gcc_checking_assert (!lang->being_defined);
    6292              :   // lang->debug_requested
    6293       154344 :   WB (lang->fields_readonly);
    6294       154344 :   WB (lang->ptrmemfunc_flag);
    6295              : 
    6296       154344 :   WB (lang->lazy_default_ctor);
    6297       154344 :   WB (lang->lazy_copy_ctor);
    6298       154344 :   WB (lang->lazy_copy_assign);
    6299       154344 :   WB (lang->lazy_destructor);
    6300       154344 :   WB (lang->has_const_copy_ctor);
    6301       154344 :   WB (lang->has_complex_copy_ctor);
    6302       154344 :   WB (lang->has_complex_copy_assign);
    6303       154344 :   WB (lang->non_aggregate);
    6304              : 
    6305       154344 :   WB (lang->has_complex_dflt);
    6306       154344 :   WB (lang->has_list_ctor);
    6307       154344 :   WB (lang->non_std_layout);
    6308       154344 :   WB (lang->is_literal);
    6309       154344 :   WB (lang->lazy_move_ctor);
    6310       154344 :   WB (lang->lazy_move_assign);
    6311       154344 :   WB (lang->has_complex_move_ctor);
    6312       154344 :   WB (lang->has_complex_move_assign);
    6313              : 
    6314       154344 :   WB (lang->has_constexpr_ctor);
    6315       154344 :   WB (lang->unique_obj_representations);
    6316       154344 :   WB (lang->unique_obj_representations_set);
    6317       154344 :   gcc_checking_assert (!lang->erroneous);
    6318       154344 :   WB (lang->non_pod_aggregate);
    6319       154344 :   WB (lang->non_aggregate_pod);
    6320              : #undef WB
    6321       154344 : }
    6322              : 
    6323              : bool
    6324       141656 : trees_in::lang_type_bools (tree t, bits_in& bits)
    6325              : {
    6326              : #define RB(X) ((X) = bits.b ())
    6327       141656 :   struct lang_type *lang = TYPE_LANG_SPECIFIC (t);
    6328              : 
    6329       141656 :   bits.bflush ();
    6330       141656 :   RB (lang->has_type_conversion);
    6331       141656 :   RB (lang->has_copy_ctor);
    6332       141656 :   RB (lang->has_default_ctor);
    6333       141656 :   RB (lang->const_needs_init);
    6334       141656 :   RB (lang->ref_needs_init);
    6335       141656 :   RB (lang->has_const_copy_assign);
    6336       141656 :   unsigned v;
    6337       141656 :   v = bits.b () << 0;
    6338       141656 :   v |= bits.b () << 1;
    6339       141656 :   lang->use_template = v;
    6340              : 
    6341       141656 :   RB (lang->has_mutable);
    6342       141656 :   RB (lang->com_interface);
    6343       141656 :   RB (lang->non_pod_class);
    6344       141656 :   RB (lang->nearly_empty_p);
    6345       141656 :   RB (lang->user_align);
    6346       141656 :   RB (lang->has_copy_assign);
    6347       141656 :   RB (lang->has_new);
    6348       141656 :   RB (lang->has_array_new);
    6349              : 
    6350       141656 :   v = bits.b () << 0;
    6351       141656 :   v |= bits.b () << 1;
    6352       141656 :   lang->gets_delete = v;
    6353       141656 :   RB (lang->interface_only);
    6354       141656 :   RB (lang->interface_unknown);
    6355       141656 :   RB (lang->contains_empty_class_p);
    6356       141656 :   RB (lang->anon_aggr);
    6357       141656 :   RB (lang->non_zero_init);
    6358       141656 :   RB (lang->empty_p);
    6359              : 
    6360       141656 :   RB (lang->vec_new_uses_cookie);
    6361       141656 :   RB (lang->declared_class);
    6362       141656 :   RB (lang->diamond_shaped);
    6363       141656 :   RB (lang->repeated_base);
    6364       141656 :   gcc_checking_assert (!lang->being_defined);
    6365       141656 :   gcc_checking_assert (!lang->debug_requested);
    6366       141656 :   RB (lang->fields_readonly);
    6367       141656 :   RB (lang->ptrmemfunc_flag);
    6368              : 
    6369       141656 :   RB (lang->lazy_default_ctor);
    6370       141656 :   RB (lang->lazy_copy_ctor);
    6371       141656 :   RB (lang->lazy_copy_assign);
    6372       141656 :   RB (lang->lazy_destructor);
    6373       141656 :   RB (lang->has_const_copy_ctor);
    6374       141656 :   RB (lang->has_complex_copy_ctor);
    6375       141656 :   RB (lang->has_complex_copy_assign);
    6376       141656 :   RB (lang->non_aggregate);
    6377              : 
    6378       141656 :   RB (lang->has_complex_dflt);
    6379       141656 :   RB (lang->has_list_ctor);
    6380       141656 :   RB (lang->non_std_layout);
    6381       141656 :   RB (lang->is_literal);
    6382       141656 :   RB (lang->lazy_move_ctor);
    6383       141656 :   RB (lang->lazy_move_assign);
    6384       141656 :   RB (lang->has_complex_move_ctor);
    6385       141656 :   RB (lang->has_complex_move_assign);
    6386              : 
    6387       141656 :   RB (lang->has_constexpr_ctor);
    6388       141656 :   RB (lang->unique_obj_representations);
    6389       141656 :   RB (lang->unique_obj_representations_set);
    6390       141656 :   gcc_checking_assert (!lang->erroneous);
    6391       141656 :   RB (lang->non_pod_aggregate);
    6392       141656 :   RB (lang->non_aggregate_pod);
    6393              : #undef RB
    6394       141656 :   return !get_overrun ();
    6395              : }
    6396              : 
    6397              : /* Read & write the core values and pointers.  */
    6398              : 
    6399              : void
    6400     35858856 : trees_out::core_vals (tree t)
    6401              : {
    6402              : #define WU(X) (u (X))
    6403              : #define WT(X) (tree_node (X))
    6404     35858856 :   tree_code code = TREE_CODE (t);
    6405              : 
    6406              :   /* First by shape of the tree.  */
    6407              : 
    6408     35858856 :   if (CODE_CONTAINS_STRUCT (code, TS_DECL_MINIMAL))
    6409              :     {
    6410              :       /* Write this early, for better log information.  */
    6411      8024552 :       WT (t->decl_minimal.name);
    6412      8024552 :       if (!DECL_TEMPLATE_PARM_P (t))
    6413      6213803 :         WT (t->decl_minimal.context);
    6414              : 
    6415      8024552 :       if (state)
    6416      6606106 :         state->write_location (*this, t->decl_minimal.locus);
    6417              : 
    6418      8024552 :       if (streaming_p ())
    6419      3075812 :         if (has_warning_spec (t))
    6420          631 :           u (get_warning_spec (t));
    6421              :     }
    6422              : 
    6423     35858856 :   if (CODE_CONTAINS_STRUCT (code, TS_TYPE_COMMON))
    6424              :     {
    6425              :       /* The only types we write also have TYPE_NON_COMMON.  */
    6426      1991077 :       gcc_checking_assert (CODE_CONTAINS_STRUCT (code, TS_TYPE_NON_COMMON));
    6427              : 
    6428              :       /* We only stream the main variant.  */
    6429      1991077 :       gcc_checking_assert (TYPE_MAIN_VARIANT (t) == t);
    6430              : 
    6431              :       /* Stream the name & context first, for better log information  */
    6432      1991077 :       WT (t->type_common.name);
    6433      1991077 :       WT (t->type_common.context);
    6434              : 
    6435              :       /* By construction we want to make sure we have the canonical
    6436              :          and main variants already in the type table, so emit them
    6437              :          now.  */
    6438      1991077 :       WT (t->type_common.main_variant);
    6439              : 
    6440      1991077 :       tree canonical = t->type_common.canonical;
    6441      1991077 :       if (canonical && DECL_TEMPLATE_PARM_P (TYPE_NAME (t)))
    6442              :         /* We do not want to wander into different templates.
    6443              :            Reconstructed on stream in.  */
    6444              :         canonical = t;
    6445      1991077 :       WT (canonical);
    6446              : 
    6447              :       /* type_common.next_variant is internally manipulated.  */
    6448              :       /* type_common.pointer_to, type_common.reference_to.  */
    6449              : 
    6450      1991077 :       if (streaming_p ())
    6451              :         {
    6452       553848 :           WU (t->type_common.precision);
    6453       553848 :           WU (t->type_common.contains_placeholder_bits);
    6454       553848 :           WU (t->type_common.mode);
    6455       553848 :           WU (t->type_common.align);
    6456              :         }
    6457              : 
    6458      1991077 :       if (!RECORD_OR_UNION_CODE_P (code))
    6459              :         {
    6460      1636523 :           WT (t->type_common.size);
    6461      1636523 :           WT (t->type_common.size_unit);
    6462              :         }
    6463      1991077 :       WT (t->type_common.attributes);
    6464              : 
    6465      1991077 :       WT (t->type_common.common.chain); /* TYPE_STUB_DECL.  */
    6466              :     }
    6467              : 
    6468     35858856 :   if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
    6469              :     {
    6470      8024552 :       if (streaming_p ())
    6471              :         {
    6472      3075812 :           WU (t->decl_common.mode);
    6473      3075812 :           WU (t->decl_common.off_align);
    6474      3075812 :           WU (t->decl_common.align);
    6475              :         }
    6476              : 
    6477              :       /* For templates these hold instantiation (partial and/or
    6478              :          specialization) information.  */
    6479      8024552 :       if (code != TEMPLATE_DECL)
    6480              :         {
    6481      7407638 :           WT (t->decl_common.size);
    6482      7407638 :           WT (t->decl_common.size_unit);
    6483              :         }
    6484              : 
    6485      8024552 :       WT (t->decl_common.attributes);
    6486              :       // FIXME: Does this introduce cross-decl links?  For instance
    6487              :       // from instantiation to the template.  If so, we'll need more
    6488              :       // deduplication logic.  I think we'll need to walk the blocks
    6489              :       // of the owning function_decl's abstract origin in tandem, to
    6490              :       // generate the locating data needed?
    6491      8024552 :       WT (t->decl_common.abstract_origin);
    6492              :     }
    6493              : 
    6494     35858856 :   if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
    6495              :     {
    6496      3803127 :       WT (t->decl_with_vis.assembler_name);
    6497      3803127 :       if (streaming_p ())
    6498      1454344 :         WU (t->decl_with_vis.visibility);
    6499              :     }
    6500              : 
    6501     35858856 :   if (CODE_CONTAINS_STRUCT (code, TS_TYPE_NON_COMMON))
    6502              :     {
    6503      1991077 :       if (code == ENUMERAL_TYPE)
    6504              :         {
    6505              :           /* These fields get set even for opaque enums that lack a
    6506              :              definition, so we stream them directly for each ENUMERAL_TYPE.
    6507              :              We stream TYPE_VALUES as part of the definition.  */
    6508         7914 :           WT (t->type_non_common.maxval);
    6509         7914 :           WT (t->type_non_common.minval);
    6510              :         }
    6511              :       /* Records and unions hold FIELDS, VFIELD & BINFO on these
    6512              :          things.  */
    6513      1983163 :       else if (!RECORD_OR_UNION_CODE_P (code))
    6514              :         {
    6515              :           // FIXME: These are from tpl_parm_value's 'type' writing.
    6516              :           // Perhaps it should just be doing them directly?
    6517      1628609 :           gcc_checking_assert (code == TEMPLATE_TYPE_PARM
    6518              :                                || code == TEMPLATE_TEMPLATE_PARM
    6519              :                                || code == BOUND_TEMPLATE_TEMPLATE_PARM);
    6520      1628609 :           gcc_checking_assert (!TYPE_CACHED_VALUES_P (t));
    6521      1628609 :           WT (t->type_non_common.values);
    6522      1628609 :           WT (t->type_non_common.maxval);
    6523      1628609 :           WT (t->type_non_common.minval);
    6524              :         }
    6525              : 
    6526      1991077 :       WT (t->type_non_common.lang_1);
    6527              :     }
    6528              : 
    6529     35858856 :   if (CODE_CONTAINS_STRUCT (code, TS_EXP))
    6530              :     {
    6531     10629882 :       if (state)
    6532     10408436 :         state->write_location (*this, t->exp.locus);
    6533              : 
    6534     10629882 :       if (streaming_p ())
    6535      5159675 :         if (has_warning_spec (t))
    6536       416607 :           u (get_warning_spec (t));
    6537              : 
    6538     10629882 :       bool vl = TREE_CODE_CLASS (code) == tcc_vl_exp;
    6539     10629882 :       unsigned limit = (vl ? VL_EXP_OPERAND_LENGTH (t)
    6540     10629882 :                         : TREE_OPERAND_LENGTH (t));
    6541     10629882 :       unsigned ix = unsigned (vl);
    6542     10629882 :       if (code == REQUIRES_EXPR)
    6543              :         {
    6544              :           /* The first operand of a REQUIRES_EXPR is a tree chain
    6545              :              of PARM_DECLs.  We need to stream this separately as
    6546              :              otherwise we would only stream the first one.  */
    6547        17965 :           chained_decls (REQUIRES_EXPR_PARMS (t));
    6548        17965 :           ++ix;
    6549              :         }
    6550     29395780 :       for (; ix != limit; ix++)
    6551     18765898 :         WT (TREE_OPERAND (t, ix));
    6552              :     }
    6553              :   else
    6554              :     /* The CODE_CONTAINS tables were inaccurate when I started.  */
    6555     25228974 :     gcc_checking_assert (TREE_CODE_CLASS (code) != tcc_expression
    6556              :                          && TREE_CODE_CLASS (code) != tcc_binary
    6557              :                          && TREE_CODE_CLASS (code) != tcc_unary
    6558              :                          && TREE_CODE_CLASS (code) != tcc_reference
    6559              :                          && TREE_CODE_CLASS (code) != tcc_comparison
    6560              :                          && TREE_CODE_CLASS (code) != tcc_statement
    6561              :                          && TREE_CODE_CLASS (code) != tcc_vl_exp);
    6562              : 
    6563              :   /* Then by CODE.  Special cases and/or 1:1 tree shape
    6564              :      correspondance. */
    6565     35858856 :   switch (code)
    6566              :     {
    6567              :     default:
    6568              :       break;
    6569              : 
    6570            0 :     case ARGUMENT_PACK_SELECT:  /* Transient during instantiation.  */
    6571            0 :     case DEFERRED_PARSE:        /* Expanded upon completion of
    6572              :                                    outermost class.  */
    6573            0 :     case IDENTIFIER_NODE:       /* Streamed specially.  */
    6574            0 :     case BINDING_VECTOR:                /* Only in namespace-scope symbol
    6575              :                                    table.  */
    6576            0 :     case SSA_NAME:
    6577            0 :     case TRANSLATION_UNIT_DECL: /* There is only one, it is a
    6578              :                                    global_tree.  */
    6579            0 :     case USERDEF_LITERAL:       /* Expanded during parsing.  */
    6580            0 :       gcc_unreachable (); /* Should never meet.  */
    6581              : 
    6582              :       /* Constants.  */
    6583           18 :     case COMPLEX_CST:
    6584           18 :       WT (TREE_REALPART (t));
    6585           18 :       WT (TREE_IMAGPART (t));
    6586           18 :       break;
    6587              : 
    6588            0 :     case FIXED_CST:
    6589            0 :       gcc_unreachable (); /* Not supported in C++.  */
    6590              : 
    6591      3396021 :     case INTEGER_CST:
    6592      3396021 :       if (streaming_p ())
    6593              :         {
    6594       620550 :           unsigned num = TREE_INT_CST_EXT_NUNITS (t);
    6595      1243027 :           for (unsigned ix = 0; ix != num; ix++)
    6596       622477 :             wu (TREE_INT_CST_ELT (t, ix));
    6597              :         }
    6598              :       break;
    6599              : 
    6600            0 :     case POLY_INT_CST:
    6601            0 :       if (streaming_p ())
    6602            0 :         for (unsigned ix = 0; ix != NUM_POLY_INT_COEFFS; ix++)
    6603            0 :           WT (POLY_INT_CST_COEFF (t, ix));
    6604              :       break;
    6605              : 
    6606        31625 :     case REAL_CST:
    6607        31625 :       if (streaming_p ())
    6608        15784 :         buf (TREE_REAL_CST_PTR (t), sizeof (real_value));
    6609              :       break;
    6610              : 
    6611              :     case STRING_CST:
    6612              :       /* Streamed during start.  */
    6613              :       break;
    6614              : 
    6615           36 :     case RAW_DATA_CST:
    6616           36 :       if (RAW_DATA_OWNER (t) == NULL_TREE)
    6617              :         break; /* Streamed as STRING_CST during start.  */
    6618           24 :       WT (RAW_DATA_OWNER (t));
    6619           24 :       if (streaming_p ())
    6620              :         {
    6621           12 :           if (TREE_CODE (RAW_DATA_OWNER (t)) == RAW_DATA_CST)
    6622            6 :             z (RAW_DATA_POINTER (t) - RAW_DATA_POINTER (RAW_DATA_OWNER (t)));
    6623            6 :           else if (TREE_CODE (RAW_DATA_OWNER (t)) == STRING_CST)
    6624            6 :             z (RAW_DATA_POINTER (t)
    6625            6 :                - TREE_STRING_POINTER (RAW_DATA_OWNER (t)));
    6626              :           else
    6627            0 :             gcc_unreachable ();
    6628              :         }
    6629              :       break;
    6630              : 
    6631           36 :     case VECTOR_CST:
    6632          102 :       for (unsigned ix = vector_cst_encoded_nelts (t); ix--;)
    6633           66 :         WT (VECTOR_CST_ENCODED_ELT (t, ix));
    6634              :       break;
    6635              : 
    6636              :       /* Decls.  */
    6637       424131 :     case VAR_DECL:
    6638       424131 :       if (DECL_CONTEXT (t)
    6639       424131 :           && TREE_CODE (DECL_CONTEXT (t)) != FUNCTION_DECL)
    6640              :         {
    6641       107253 :           if (DECL_HAS_VALUE_EXPR_P (t))
    6642           18 :             WT (DECL_VALUE_EXPR (t));
    6643              :           break;
    6644              :         }
    6645              :       /* FALLTHROUGH  */
    6646              : 
    6647      3610887 :     case RESULT_DECL:
    6648      3610887 :     case PARM_DECL:
    6649      3610887 :       if (DECL_HAS_VALUE_EXPR_P (t))
    6650        29376 :         WT (DECL_VALUE_EXPR (t));
    6651              :       /* FALLTHROUGH  */
    6652              : 
    6653      3768029 :     case CONST_DECL:
    6654      3768029 :     case IMPORTED_DECL:
    6655      3768029 :       WT (t->decl_common.initial);
    6656      3768029 :       break;
    6657              : 
    6658       109145 :     case FIELD_DECL:
    6659       109145 :       WT (t->field_decl.offset);
    6660       109145 :       WT (t->field_decl.bit_field_type);
    6661       109145 :       {
    6662       109145 :         auto ovr = make_temp_override (walking_bit_field_unit, true);
    6663       109145 :         WT (t->field_decl.qualifier); /* bitfield unit.  */
    6664       109145 :       }
    6665       109145 :       WT (t->field_decl.bit_offset);
    6666       109145 :       WT (t->field_decl.fcontext);
    6667       109145 :       WT (t->decl_common.initial);
    6668       109145 :       break;
    6669              : 
    6670        31843 :     case LABEL_DECL:
    6671        31843 :       if (streaming_p ())
    6672              :         {
    6673        15920 :           WU (t->label_decl.label_decl_uid);
    6674        15920 :           WU (t->label_decl.eh_landing_pad_nr);
    6675              :         }
    6676              :       break;
    6677              : 
    6678       849214 :     case FUNCTION_DECL:
    6679       849214 :       if (streaming_p ())
    6680              :         {
    6681              :           /* Builtins can be streamed by value when a header declares
    6682              :              them.  */
    6683       424500 :           WU (DECL_BUILT_IN_CLASS (t));
    6684       424500 :           if (DECL_BUILT_IN_CLASS (t) != NOT_BUILT_IN)
    6685         8320 :             WU (DECL_UNCHECKED_FUNCTION_CODE (t));
    6686              :         }
    6687              : 
    6688       849214 :       WT (t->function_decl.personality);
    6689              :       /* Rather than streaming target/optimize nodes, we should reconstruct
    6690              :          them on stream-in from any attributes applied to the function.  */
    6691       849214 :       if (streaming_p () && t->function_decl.function_specific_target)
    6692            0 :         warning_at (DECL_SOURCE_LOCATION (t), 0,
    6693              :                     "%<target%> attribute currently unsupported in modules");
    6694       849214 :       if (streaming_p () && t->function_decl.function_specific_optimization)
    6695            3 :         warning_at (DECL_SOURCE_LOCATION (t), 0,
    6696              :                     "%<optimize%> attribute currently unsupported in modules");
    6697       849214 :       WT (t->function_decl.vindex);
    6698              : 
    6699       849214 :       if (DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (t))
    6700         5518 :         WT (lookup_explicit_specifier (t));
    6701              :       break;
    6702              : 
    6703       100989 :     case USING_DECL:
    6704              :       /* USING_DECL_DECLS  */
    6705       100989 :       WT (t->decl_common.initial);
    6706              :       /* FALLTHROUGH  */
    6707              : 
    6708      2529515 :     case TYPE_DECL:
    6709              :       /* USING_DECL: USING_DECL_SCOPE  */
    6710              :       /* TYPE_DECL: DECL_ORIGINAL_TYPE */
    6711      2529515 :       WT (t->decl_non_common.result);
    6712      2529515 :       break;
    6713              : 
    6714              :       /* Miscellaneous common nodes.  */
    6715       540346 :     case BLOCK:
    6716       540346 :       if (state)
    6717              :         {
    6718       540346 :           state->write_location (*this, t->block.locus);
    6719       540346 :           state->write_location (*this, t->block.end_locus);
    6720              :         }
    6721              : 
    6722              :       /* DECL_LOCAL_DECL_P decls are first encountered here and
    6723              :          streamed by value.  */
    6724       807866 :       for (tree decls = t->block.vars; decls; decls = DECL_CHAIN (decls))
    6725              :         {
    6726       267520 :           if (VAR_OR_FUNCTION_DECL_P (decls)
    6727       267520 :               && DECL_LOCAL_DECL_P (decls))
    6728              :             {
    6729              :               /* Make sure this is the first encounter, and mark for
    6730              :                  walk-by-value.  */
    6731          258 :               gcc_checking_assert (!TREE_VISITED (decls)
    6732              :                                    && !DECL_TEMPLATE_INFO (decls));
    6733          258 :               mark_by_value (decls);
    6734              :             }
    6735       267520 :           tree_node (decls);
    6736              :         }
    6737       540346 :       tree_node (NULL_TREE);
    6738              : 
    6739              :       /* nonlocalized_vars is a middle-end thing.  */
    6740       540346 :       WT (t->block.subblocks);
    6741       540346 :       WT (t->block.supercontext);
    6742              :       // FIXME: As for decl's abstract_origin, does this introduce crosslinks?
    6743       540346 :       WT (t->block.abstract_origin);
    6744              :       /* fragment_origin, fragment_chain are middle-end things.  */
    6745       540346 :       WT (t->block.chain);
    6746              :       /* nonlocalized_vars, block_num & die are middle endy/debug
    6747              :          things.  */
    6748       540346 :       break;
    6749              : 
    6750      1068432 :     case CALL_EXPR:
    6751      1068432 :       if (streaming_p ())
    6752       511569 :         WU (t->base.u.ifn);
    6753              :       break;
    6754              : 
    6755              :     case CONSTRUCTOR:
    6756              :       // This must be streamed /after/ we've streamed the type,
    6757              :       // because it can directly refer to elements of the type. Eg,
    6758              :       // FIELD_DECLs of a RECORD_TYPE.
    6759              :       break;
    6760              : 
    6761           36 :     case OMP_CLAUSE:
    6762           36 :       {
    6763              :         /* The ompcode is serialized in start.  */
    6764           36 :         if (streaming_p ())
    6765           18 :           WU (t->omp_clause.subcode.map_kind);
    6766           36 :         if (state)
    6767           36 :           state->write_location (*this, t->omp_clause.locus);
    6768              : 
    6769           36 :         unsigned len = omp_clause_num_ops[OMP_CLAUSE_CODE (t)];
    6770          120 :         for (unsigned ix = 0; ix != len; ix++)
    6771           84 :           WT (t->omp_clause.ops[ix]);
    6772              :       }
    6773              :       break;
    6774              : 
    6775       416514 :     case STATEMENT_LIST:
    6776      1673197 :       for (tree stmt : tsi_range (t))
    6777      1256683 :         if (stmt)
    6778      1256683 :           WT (stmt);
    6779       416514 :       WT (NULL_TREE);
    6780       416514 :       break;
    6781              : 
    6782            0 :     case OPTIMIZATION_NODE:
    6783            0 :     case TARGET_OPTION_NODE:
    6784              :       // FIXME: Our representation for these two nodes is a cache of
    6785              :       // the resulting set of options.  Not a record of the options
    6786              :       // that got changed by a particular attribute or pragma.  Instead
    6787              :       // of recording that, we probably should just rebuild the options
    6788              :       // on stream-in from the function attributes.  This could introduce
    6789              :       // strangeness if the importer has some incompatible set of flags
    6790              :       // but we currently assume users "know what they're doing" in such
    6791              :       // a case anyway.
    6792            0 :       gcc_unreachable ();
    6793       205142 :       break;
    6794              : 
    6795       205142 :     case TREE_BINFO:
    6796       205142 :       {
    6797       205142 :         WT (t->binfo.common.chain);
    6798       205142 :         WT (t->binfo.offset);
    6799       205142 :         WT (t->binfo.inheritance);
    6800       205142 :         WT (t->binfo.vptr_field);
    6801              : 
    6802       205142 :         WT (t->binfo.vtable);
    6803       205142 :         WT (t->binfo.virtuals);
    6804       205142 :         WT (t->binfo.vtt_subvtt);
    6805       205142 :         WT (t->binfo.vtt_vptr);
    6806              : 
    6807       205142 :         tree_vec (BINFO_BASE_ACCESSES (t));
    6808       205142 :         unsigned num = vec_safe_length (BINFO_BASE_ACCESSES (t));
    6809       262522 :         for (unsigned ix = 0; ix != num; ix++)
    6810        57380 :           WT (BINFO_BASE_BINFO (t, ix));
    6811              :       }
    6812              :       break;
    6813              : 
    6814      2816112 :     case TREE_LIST:
    6815      2816112 :       WT (t->list.purpose);
    6816      2816112 :       WT (t->list.value);
    6817      2816112 :       WT (t->list.common.chain);
    6818      2816112 :       break;
    6819              : 
    6820      2363768 :     case TREE_VEC:
    6821      6491602 :       for (unsigned ix = TREE_VEC_LENGTH (t); ix--;)
    6822      4127834 :         WT (TREE_VEC_ELT (t, ix));
    6823              :       /* We stash NON_DEFAULT_TEMPLATE_ARGS_COUNT on TREE_CHAIN!  */
    6824      2363768 :       gcc_checking_assert (!t->type_common.common.chain
    6825              :                            || (TREE_CODE (t->type_common.common.chain)
    6826              :                                == INTEGER_CST));
    6827      2363768 :       WT (t->type_common.common.chain);
    6828      2363768 :       break;
    6829              : 
    6830              :       /* C++-specific nodes ...  */
    6831       193508 :     case BASELINK:
    6832       193508 :       WT (((lang_tree_node *)t)->baselink.binfo);
    6833       193508 :       WT (((lang_tree_node *)t)->baselink.functions);
    6834       193508 :       WT (((lang_tree_node *)t)->baselink.access_binfo);
    6835       193508 :       WT (((lang_tree_node *)t)->baselink.common.chain);
    6836       193508 :       break;
    6837              : 
    6838        84383 :     case CONSTRAINT_INFO:
    6839        84383 :       WT (((lang_tree_node *)t)->constraint_info.template_reqs);
    6840        84383 :       WT (((lang_tree_node *)t)->constraint_info.declarator_reqs);
    6841        84383 :       WT (((lang_tree_node *)t)->constraint_info.associated_constr);
    6842        84383 :       break;
    6843              : 
    6844        12534 :     case DEFERRED_NOEXCEPT:
    6845        12534 :       WT (((lang_tree_node *)t)->deferred_noexcept.pattern);
    6846        12534 :       WT (((lang_tree_node *)t)->deferred_noexcept.args);
    6847        12534 :       break;
    6848              : 
    6849        10958 :     case LAMBDA_EXPR:
    6850        10958 :       WT (((lang_tree_node *)t)->lambda_expression.capture_list);
    6851        10958 :       WT (((lang_tree_node *)t)->lambda_expression.this_capture);
    6852        10958 :       WT (((lang_tree_node *)t)->lambda_expression.extra_scope);
    6853        10958 :       WT (((lang_tree_node *)t)->lambda_expression.regen_info);
    6854        10958 :       WT (((lang_tree_node *)t)->lambda_expression.extra_args);
    6855              :       /* pending_proxies is a parse-time thing.  */
    6856        10958 :       gcc_assert (!((lang_tree_node *)t)->lambda_expression.pending_proxies);
    6857        10958 :       if (state)
    6858        10958 :         state->write_location
    6859        10958 :           (*this, ((lang_tree_node *)t)->lambda_expression.locus);
    6860        10958 :       if (streaming_p ())
    6861              :         {
    6862         3732 :           WU (((lang_tree_node *)t)->lambda_expression.default_capture_mode);
    6863         3732 :           WU (((lang_tree_node *)t)->lambda_expression.discriminator_scope);
    6864         3732 :           WU (((lang_tree_node *)t)->lambda_expression.discriminator_sig);
    6865              :         }
    6866              :       break;
    6867              : 
    6868      1776726 :     case OVERLOAD:
    6869      1776726 :       WT (((lang_tree_node *)t)->overload.function);
    6870      1776726 :       WT (t->common.chain);
    6871      1776726 :       break;
    6872              : 
    6873            0 :     case PTRMEM_CST:
    6874            0 :       WT (((lang_tree_node *)t)->ptrmem.member);
    6875            0 :       break;
    6876              : 
    6877        15237 :     case STATIC_ASSERT:
    6878        15237 :       WT (((lang_tree_node *)t)->static_assertion.condition);
    6879        15237 :       WT (((lang_tree_node *)t)->static_assertion.message);
    6880        15237 :       if (state)
    6881        15237 :         state->write_location
    6882        15237 :           (*this, ((lang_tree_node *)t)->static_assertion.location);
    6883              :       break;
    6884              : 
    6885       616914 :     case TEMPLATE_DECL:
    6886              :       /* Streamed with the template_decl node itself.  */
    6887       616914 :       gcc_checking_assert
    6888              :         (TREE_VISITED (((lang_tree_node *)t)->template_decl.arguments));
    6889       616914 :       gcc_checking_assert
    6890              :         (TREE_VISITED (((lang_tree_node *)t)->template_decl.result));
    6891       616914 :       if (DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (t))
    6892        11086 :         WT (DECL_CHAIN (t));
    6893              :       break;
    6894              : 
    6895      1385578 :     case TEMPLATE_INFO:
    6896      1385578 :       {
    6897      1385578 :         WT (((lang_tree_node *)t)->template_info.tmpl);
    6898      1385578 :         WT (((lang_tree_node *)t)->template_info.args);
    6899      1385578 :         WT (((lang_tree_node *)t)->template_info.partial);
    6900              : 
    6901      1385578 :         const auto *ac = (((lang_tree_node *)t)
    6902              :                           ->template_info.deferred_access_checks);
    6903      1385578 :         unsigned len = vec_safe_length (ac);
    6904      1385578 :         if (streaming_p ())
    6905       691085 :           u (len);
    6906      1385578 :         if (len)
    6907              :           {
    6908            0 :             for (unsigned ix = 0; ix != len; ix++)
    6909              :               {
    6910            0 :                 const auto &m = (*ac)[ix];
    6911            0 :                 WT (m.binfo);
    6912            0 :                 WT (m.decl);
    6913            0 :                 WT (m.diag_decl);
    6914            0 :                 if (state)
    6915            0 :                   state->write_location (*this, m.loc);
    6916              :               }
    6917              :           }
    6918              :       }
    6919              :       break;
    6920              : 
    6921      1746213 :     case TEMPLATE_PARM_INDEX:
    6922      1746213 :       if (streaming_p ())
    6923              :         {
    6924       395901 :           WU (((lang_tree_node *)t)->tpi.index);
    6925       395901 :           WU (((lang_tree_node *)t)->tpi.level);
    6926       395901 :           WU (((lang_tree_node *)t)->tpi.orig_level);
    6927              :         }
    6928      1746213 :       WT (((lang_tree_node *)t)->tpi.decl);
    6929              :       /* TEMPLATE_PARM_DESCENDANTS (AKA TREE_CHAIN) is an internal
    6930              :          cache, do not stream.  */
    6931      1746213 :       break;
    6932              : 
    6933        25180 :     case TRAIT_EXPR:
    6934        25180 :       WT (((lang_tree_node *)t)->trait_expression.type1);
    6935        25180 :       WT (((lang_tree_node *)t)->trait_expression.type2);
    6936        25180 :       if (streaming_p ())
    6937         9690 :         WU (((lang_tree_node *)t)->trait_expression.kind);
    6938              :       break;
    6939              : 
    6940            4 :     case TU_LOCAL_ENTITY:
    6941            4 :       WT (((lang_tree_node *)t)->tu_local_entity.name);
    6942            4 :       if (state)
    6943            4 :         state->write_location
    6944            4 :           (*this, ((lang_tree_node *)t)->tu_local_entity.loc);
    6945              :       break;
    6946              :     }
    6947              : 
    6948     35858856 :   if (CODE_CONTAINS_STRUCT (code, TS_TYPED))
    6949              :     {
    6950              :       /* We want to stream the type of a expression-like nodes /after/
    6951              :          we've streamed the operands.  The type often contains (bits
    6952              :          of the) types of the operands, and with things like decltype
    6953              :          and noexcept in play, we really want to stream the decls
    6954              :          defining the type before we try and stream the type on its
    6955              :          own.  Otherwise we can find ourselves trying to read in a
    6956              :          decl, when we're already partially reading in a component of
    6957              :          its type.  And that's bad.  */
    6958     33820774 :       tree type = t->typed.type;
    6959     33820774 :       unsigned prec = 0;
    6960              : 
    6961     33820774 :       switch (code)
    6962              :         {
    6963              :         default:
    6964              :           break;
    6965              : 
    6966              :         case TEMPLATE_DECL:
    6967              :           /* We fill in the template's type separately.  */
    6968     33820774 :           type = NULL_TREE;
    6969              :           break;
    6970              : 
    6971      2428526 :         case TYPE_DECL:
    6972      2428526 :           if (DECL_ORIGINAL_TYPE (t) && t == TYPE_NAME (type))
    6973              :             /* This is a typedef.  We set its type separately.  */
    6974              :             type = NULL_TREE;
    6975              :           break;
    6976              : 
    6977         7914 :         case ENUMERAL_TYPE:
    6978         7914 :           if (type && !ENUM_FIXED_UNDERLYING_TYPE_P (t))
    6979              :             {
    6980              :               /* Type is a restricted range integer type derived from the
    6981              :                  integer_types.  Find the right one.  */
    6982         4820 :               prec = TYPE_PRECISION (type);
    6983         4820 :               tree name = DECL_NAME (TYPE_NAME (type));
    6984              : 
    6985        62982 :               for (unsigned itk = itk_none; itk--;)
    6986        62982 :                 if (integer_types[itk]
    6987        62982 :                     && DECL_NAME (TYPE_NAME (integer_types[itk])) == name)
    6988              :                   {
    6989              :                     type = integer_types[itk];
    6990              :                     break;
    6991              :                   }
    6992         4820 :               gcc_assert (type != t->typed.type);
    6993              :             }
    6994              :           break;
    6995              :         }
    6996              : 
    6997     33820774 :       WT (type);
    6998     33820774 :       if (prec && streaming_p ())
    6999         2408 :         WU (prec);
    7000              :     }
    7001              : 
    7002     35858856 :   if (TREE_CODE (t) == CONSTRUCTOR)
    7003              :     {
    7004        96812 :       unsigned len = vec_safe_length (t->constructor.elts);
    7005        96812 :       if (streaming_p ())
    7006        47808 :         WU (len);
    7007        96812 :       if (len)
    7008       356855 :         for (unsigned ix = 0; ix != len; ix++)
    7009              :           {
    7010       304723 :             const constructor_elt &elt = (*t->constructor.elts)[ix];
    7011              : 
    7012       304723 :             WT (elt.index);
    7013       304723 :             WT (elt.value);
    7014              :           }
    7015              :     }
    7016              : 
    7017              : #undef WT
    7018              : #undef WU
    7019     35858856 : }
    7020              : 
    7021              : // Streaming in a reference to a decl can cause that decl to be
    7022              : // TREE_USED, which is the mark_used behaviour we need most of the
    7023              : // time.  The trees_in::unused can be incremented to inhibit this,
    7024              : // which is at least needed for vtables.
    7025              : 
    7026              : bool
    7027     14057501 : trees_in::core_vals (tree t)
    7028              : {
    7029              : #define RU(X) ((X) = u ())
    7030              : #define RUC(T,X) ((X) = T (u ()))
    7031              : #define RT(X) ((X) = tree_node ())
    7032              : #define RTU(X) ((X) = tree_node (true))
    7033     14057501 :   tree_code code = TREE_CODE (t);
    7034              : 
    7035              :   /* First by tree shape.  */
    7036     14057501 :   if (CODE_CONTAINS_STRUCT (code, TS_DECL_MINIMAL))
    7037              :     {
    7038      3015033 :       RT (t->decl_minimal.name);
    7039      3015033 :       if (!DECL_TEMPLATE_PARM_P (t))
    7040      2622833 :         RT (t->decl_minimal.context);
    7041              : 
    7042              :       /* Don't zap the locus just yet, we don't record it correctly
    7043              :          and thus lose all location information.  */
    7044      3015033 :       t->decl_minimal.locus = state->read_location (*this);
    7045      3015033 :       if (has_warning_spec (t))
    7046          610 :         put_warning_spec (t, u ());
    7047              :     }
    7048              : 
    7049     14057501 :   if (CODE_CONTAINS_STRUCT (code, TS_TYPE_COMMON))
    7050              :     {
    7051       516522 :       RT (t->type_common.name);
    7052       516522 :       RT (t->type_common.context);
    7053              : 
    7054       516522 :       RT (t->type_common.main_variant);
    7055       516522 :       RT (t->type_common.canonical);
    7056              : 
    7057              :       /* type_common.next_variant is internally manipulated.  */
    7058              :       /* type_common.pointer_to, type_common.reference_to.  */
    7059              : 
    7060       516522 :       RU (t->type_common.precision);
    7061       516522 :       RU (t->type_common.contains_placeholder_bits);
    7062       516522 :       RUC (machine_mode, t->type_common.mode);
    7063       516522 :       RU (t->type_common.align);
    7064              : 
    7065       516522 :       if (!RECORD_OR_UNION_CODE_P (code))
    7066              :         {
    7067       355851 :           RT (t->type_common.size);
    7068       355851 :           RT (t->type_common.size_unit);
    7069              :         }
    7070       516522 :       RT (t->type_common.attributes);
    7071              : 
    7072       516522 :       RT (t->type_common.common.chain); /* TYPE_STUB_DECL.  */
    7073              :     }
    7074              : 
    7075     14057501 :   if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
    7076              :     {
    7077      3015033 :       RUC (machine_mode, t->decl_common.mode);
    7078      3015033 :       RU (t->decl_common.off_align);
    7079      3015033 :       RU (t->decl_common.align);
    7080              : 
    7081      3015033 :       if (code != TEMPLATE_DECL)
    7082              :         {
    7083      2714351 :           RT (t->decl_common.size);
    7084      2714351 :           RT (t->decl_common.size_unit);
    7085              :         }
    7086              : 
    7087      3015033 :       RT (t->decl_common.attributes);
    7088      3015033 :       RT (t->decl_common.abstract_origin);
    7089              :     }
    7090              : 
    7091     14057501 :   if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
    7092              :     {
    7093      1393999 :       RT (t->decl_with_vis.assembler_name);
    7094      1393999 :       RUC (symbol_visibility, t->decl_with_vis.visibility);
    7095              :     }
    7096              : 
    7097     14057501 :   if (CODE_CONTAINS_STRUCT (code, TS_TYPE_NON_COMMON))
    7098              :     {
    7099       516522 :       if (code == ENUMERAL_TYPE)
    7100              :         {
    7101              :           /* These fields get set even for opaque enums that lack a
    7102              :              definition, so we stream them directly for each ENUMERAL_TYPE.
    7103              :              We stream TYPE_VALUES as part of the definition.  */
    7104         3005 :           RT (t->type_non_common.maxval);
    7105         3005 :           RT (t->type_non_common.minval);
    7106              :         }
    7107              :       /* Records and unions hold FIELDS, VFIELD & BINFO on these
    7108              :          things.  */
    7109       513517 :       else if (!RECORD_OR_UNION_CODE_P (code))
    7110              :         {
    7111              :           /* This is not clobbering TYPE_CACHED_VALUES, because this
    7112              :              is a type that doesn't have any.  */
    7113       352846 :           gcc_checking_assert (!TYPE_CACHED_VALUES_P (t));
    7114       352846 :           RT (t->type_non_common.values);
    7115       352846 :           RT (t->type_non_common.maxval);
    7116       352846 :           RT (t->type_non_common.minval);
    7117              :         }
    7118              : 
    7119       516522 :       RT (t->type_non_common.lang_1);
    7120              :     }
    7121              : 
    7122     14057501 :   if (CODE_CONTAINS_STRUCT (code, TS_EXP))
    7123              :     {
    7124      5477741 :       t->exp.locus = state->read_location (*this);
    7125      5477741 :       if (has_warning_spec (t))
    7126       436974 :         put_warning_spec (t, u ());
    7127              : 
    7128      5477741 :       bool vl = TREE_CODE_CLASS (code) == tcc_vl_exp;
    7129      5477741 :       unsigned limit = (vl ? VL_EXP_OPERAND_LENGTH (t)
    7130      5477741 :                         : TREE_OPERAND_LENGTH (t));
    7131      5477741 :       unsigned ix = unsigned (vl);
    7132      5477741 :       if (code == REQUIRES_EXPR)
    7133              :         {
    7134         6944 :           REQUIRES_EXPR_PARMS (t) = chained_decls ();
    7135         6944 :           ++ix;
    7136              :         }
    7137     15035223 :       for (; ix != limit; ix++)
    7138      9557482 :         RTU (TREE_OPERAND (t, ix));
    7139              :     }
    7140              : 
    7141              :   /* Then by CODE.  Special cases and/or 1:1 tree shape
    7142              :      correspondance. */
    7143     14057501 :   switch (code)
    7144              :     {
    7145              :     default:
    7146              :       break;
    7147              : 
    7148              :     case ARGUMENT_PACK_SELECT:
    7149              :     case DEFERRED_PARSE:
    7150              :     case IDENTIFIER_NODE:
    7151              :     case BINDING_VECTOR:
    7152              :     case SSA_NAME:
    7153              :     case TRANSLATION_UNIT_DECL:
    7154              :     case USERDEF_LITERAL:
    7155              :       return false; /* Should never meet.  */
    7156              : 
    7157              :       /* Constants.  */
    7158            9 :     case COMPLEX_CST:
    7159            9 :       RT (TREE_REALPART (t));
    7160            9 :       RT (TREE_IMAGPART (t));
    7161            9 :       break;
    7162              : 
    7163              :     case FIXED_CST:
    7164              :       /* Not suported in C++.  */
    7165              :       return false;
    7166              : 
    7167       576712 :     case INTEGER_CST:
    7168       576712 :       {
    7169       576712 :         unsigned num = TREE_INT_CST_EXT_NUNITS (t);
    7170      1155232 :         for (unsigned ix = 0; ix != num; ix++)
    7171       578520 :           TREE_INT_CST_ELT (t, ix) = wu ();
    7172              :       }
    7173              :       break;
    7174              : 
    7175              :     case POLY_INT_CST:
    7176            0 :       for (unsigned ix = 0; ix != NUM_POLY_INT_COEFFS; ix++)
    7177            0 :         RT (POLY_INT_CST_COEFF (t, ix));
    7178              :       break;
    7179              : 
    7180        16198 :     case REAL_CST:
    7181        16198 :       if (const void *bytes = buf (sizeof (real_value)))
    7182        16198 :         memcpy (TREE_REAL_CST_PTR (t), bytes, sizeof (real_value));
    7183              :       break;
    7184              : 
    7185              :     case STRING_CST:
    7186              :       /* Streamed during start.  */
    7187              :       break;
    7188              : 
    7189            6 :     case RAW_DATA_CST:
    7190            6 :       RT (RAW_DATA_OWNER (t));
    7191            6 :       gcc_assert (TREE_CODE (RAW_DATA_OWNER (t)) == STRING_CST
    7192              :                   && TREE_STRING_LENGTH (RAW_DATA_OWNER (t)));
    7193            6 :       RAW_DATA_POINTER (t) = TREE_STRING_POINTER (RAW_DATA_OWNER (t)) + z ();
    7194            6 :       break;
    7195              : 
    7196           24 :     case VECTOR_CST:
    7197           63 :       for (unsigned ix = vector_cst_encoded_nelts (t); ix--;)
    7198           39 :         RT (VECTOR_CST_ENCODED_ELT (t, ix));
    7199              :       break;
    7200              : 
    7201              :       /* Decls.  */
    7202       203714 :     case VAR_DECL:
    7203       203714 :       if (DECL_CONTEXT (t)
    7204       203714 :           && TREE_CODE (DECL_CONTEXT (t)) != FUNCTION_DECL)
    7205              :         {
    7206        40550 :           if (DECL_HAS_VALUE_EXPR_P (t))
    7207              :             {
    7208            9 :               tree val = tree_node ();
    7209            9 :               SET_DECL_VALUE_EXPR (t, val);
    7210              :             }
    7211              :           break;
    7212              :         }
    7213              :       /* FALLTHROUGH  */
    7214              : 
    7215      1366740 :     case RESULT_DECL:
    7216      1366740 :     case PARM_DECL:
    7217      1366740 :       if (DECL_HAS_VALUE_EXPR_P (t))
    7218              :         {
    7219              :           /* The DECL_VALUE hash table is a cache, thus if we're
    7220              :              reading a duplicate (which we end up discarding), the
    7221              :              value expr will also be cleaned up at the next gc.  */
    7222        12613 :           tree val = tree_node ();
    7223        12613 :           SET_DECL_VALUE_EXPR (t, val);
    7224              :         }
    7225              :       /* FALLTHROUGH  */
    7226              : 
    7227      1409185 :     case CONST_DECL:
    7228      1409185 :     case IMPORTED_DECL:
    7229      1409185 :       RT (t->decl_common.initial);
    7230      1409185 :       break;
    7231              : 
    7232        51801 :     case FIELD_DECL:
    7233        51801 :       RT (t->field_decl.offset);
    7234        51801 :       RT (t->field_decl.bit_field_type);
    7235        51801 :       RT (t->field_decl.qualifier);
    7236        51801 :       RT (t->field_decl.bit_offset);
    7237        51801 :       RT (t->field_decl.fcontext);
    7238        51801 :       RT (t->decl_common.initial);
    7239        51801 :       break;
    7240              : 
    7241        16723 :     case LABEL_DECL:
    7242        16723 :       RU (t->label_decl.label_decl_uid);
    7243        16723 :       RU (t->label_decl.eh_landing_pad_nr);
    7244        16723 :       break;
    7245              : 
    7246       425412 :     case FUNCTION_DECL:
    7247       425412 :       {
    7248       425412 :         unsigned bltin = u ();
    7249       425412 :         t->function_decl.built_in_class = built_in_class (bltin);
    7250       425412 :         if (bltin != NOT_BUILT_IN)
    7251              :           {
    7252         8234 :             bltin = u ();
    7253         8234 :             DECL_UNCHECKED_FUNCTION_CODE (t) = built_in_function (bltin);
    7254              :           }
    7255              : 
    7256       425412 :         RT (t->function_decl.personality);
    7257              :         /* These properties are not streamed, and should be reconstructed
    7258              :            from any function attributes.  */
    7259              :         // t->function_decl.function_specific_target);
    7260              :         // t->function_decl.function_specific_optimization);
    7261       425412 :         RT (t->function_decl.vindex);
    7262              : 
    7263       425412 :         if (DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (t))
    7264              :           {
    7265         3375 :             tree spec;
    7266         3375 :             RT (spec);
    7267         3375 :             store_explicit_specifier (t, spec);
    7268              :           }
    7269              :       }
    7270              :       break;
    7271              : 
    7272        49046 :     case USING_DECL:
    7273              :       /* USING_DECL_DECLS  */
    7274        49046 :       RT (t->decl_common.initial);
    7275              :       /* FALLTHROUGH  */
    7276              : 
    7277       764702 :     case TYPE_DECL:
    7278              :       /* USING_DECL: USING_DECL_SCOPE  */
    7279              :       /* TYPE_DECL: DECL_ORIGINAL_TYPE */
    7280       764702 :       RT (t->decl_non_common.result);
    7281       764702 :       break;
    7282              : 
    7283              :       /* Miscellaneous common nodes.  */
    7284       279382 :     case BLOCK:
    7285       279382 :       t->block.locus = state->read_location (*this);
    7286       279382 :       t->block.end_locus = state->read_location (*this);
    7287              : 
    7288       279382 :       for (tree *chain = &t->block.vars;;)
    7289       421032 :         if (tree decl = tree_node ())
    7290              :           {
    7291              :             /* For a deduplicated local type or enumerator, chain the
    7292              :                duplicate decl instead of the canonical in-TU decl.  Seeing
    7293              :                a duplicate here means the containing function whose body
    7294              :                we're streaming in is a duplicate too, so we'll end up
    7295              :                discarding this BLOCK (and the rest of the duplicate function
    7296              :                body) anyway.  */
    7297       141650 :             decl = maybe_duplicate (decl);
    7298              : 
    7299       141650 :             if (!DECL_P (decl))
    7300              :               {
    7301            0 :                 set_overrun ();
    7302            0 :                 break;
    7303              :               }
    7304              : 
    7305              :             /* If DECL_CHAIN is already set then this was a backreference to a
    7306              :                local type or enumerator from a previous read (PR c++/114630).
    7307              :                Let's copy the node so we can keep building the chain for ODR
    7308              :                checking later.  */
    7309       141650 :             if (DECL_CHAIN (decl))
    7310              :               {
    7311           12 :                 gcc_checking_assert (TREE_CODE (decl) == TYPE_DECL
    7312              :                                      && find_duplicate (DECL_CONTEXT (decl)));
    7313            6 :                 decl = copy_decl (decl);
    7314              :               }
    7315              : 
    7316       141650 :             *chain = decl;
    7317       141650 :             chain = &DECL_CHAIN (decl);
    7318              :           }
    7319              :         else
    7320       141650 :           break;
    7321              : 
    7322              :       /* nonlocalized_vars is middle-end.  */
    7323       279382 :       RT (t->block.subblocks);
    7324       279382 :       RT (t->block.supercontext);
    7325       279382 :       RT (t->block.abstract_origin);
    7326              :       /* fragment_origin, fragment_chain are middle-end.  */
    7327       279382 :       RT (t->block.chain);
    7328              :       /* nonlocalized_vars, block_num, die are middle endy/debug
    7329              :          things.  */
    7330       279382 :       break;
    7331              : 
    7332       532750 :     case CALL_EXPR:
    7333       532750 :       RUC (internal_fn, t->base.u.ifn);
    7334       532750 :       break;
    7335              : 
    7336              :     case CONSTRUCTOR:
    7337              :       // Streamed after the node's type.
    7338              :       break;
    7339              : 
    7340           18 :     case OMP_CLAUSE:
    7341           18 :       {
    7342           18 :         RU (t->omp_clause.subcode.map_kind);
    7343           18 :         t->omp_clause.locus = state->read_location (*this);
    7344              : 
    7345           18 :         unsigned len = omp_clause_num_ops[OMP_CLAUSE_CODE (t)];
    7346           66 :         for (unsigned ix = 0; ix != len; ix++)
    7347           48 :           RT (t->omp_clause.ops[ix]);
    7348              :       }
    7349              :       break;
    7350              : 
    7351       240450 :     case STATEMENT_LIST:
    7352       240450 :       {
    7353       240450 :         tree_stmt_iterator iter = tsi_start (t);
    7354       990468 :         for (tree stmt; RT (stmt);)
    7355              :           {
    7356       750018 :             if (TREE_CODE (stmt) == DEBUG_BEGIN_STMT
    7357       117012 :                 && !MAY_HAVE_DEBUG_MARKER_STMTS)
    7358            0 :               continue;
    7359       750018 :             tsi_link_after (&iter, stmt, TSI_CONTINUE_LINKING);
    7360              :           }
    7361              :       }
    7362       240450 :       break;
    7363              : 
    7364            0 :     case OPTIMIZATION_NODE:
    7365            0 :     case TARGET_OPTION_NODE:
    7366              :       /* Not implemented, see trees_out::core_vals.  */
    7367            0 :       gcc_unreachable ();
    7368        85654 :       break;
    7369              : 
    7370        85654 :     case TREE_BINFO:
    7371        85654 :       RT (t->binfo.common.chain);
    7372        85654 :       RT (t->binfo.offset);
    7373        85654 :       RT (t->binfo.inheritance);
    7374        85654 :       RT (t->binfo.vptr_field);
    7375              : 
    7376              :       /* Do not mark the vtables as USED in the address expressions
    7377              :          here.  */
    7378        85654 :       unused++;
    7379        85654 :       RT (t->binfo.vtable);
    7380        85654 :       RT (t->binfo.virtuals);
    7381        85654 :       RT (t->binfo.vtt_subvtt);
    7382        85654 :       RT (t->binfo.vtt_vptr);
    7383        85654 :       unused--;
    7384              : 
    7385        85654 :       BINFO_BASE_ACCESSES (t) = tree_vec ();
    7386        85654 :       if (!get_overrun ())
    7387              :         {
    7388        85654 :           unsigned num = vec_safe_length (BINFO_BASE_ACCESSES (t));
    7389       109152 :           for (unsigned ix = 0; ix != num; ix++)
    7390        23498 :             BINFO_BASE_APPEND (t, tree_node ());
    7391              :         }
    7392              :       break;
    7393              : 
    7394      1213994 :     case TREE_LIST:
    7395      1213994 :       RT (t->list.purpose);
    7396      1213994 :       RT (t->list.value);
    7397      1213994 :       RT (t->list.common.chain);
    7398      1213994 :       break;
    7399              : 
    7400       780929 :     case TREE_VEC:
    7401      2094168 :       for (unsigned ix = TREE_VEC_LENGTH (t); ix--;)
    7402      1313239 :         RT (TREE_VEC_ELT (t, ix));
    7403       780929 :       RT (t->type_common.common.chain);
    7404       780929 :       break;
    7405              : 
    7406              :       /* C++-specific nodes ...  */
    7407        97440 :     case BASELINK:
    7408        97440 :       RT (((lang_tree_node *)t)->baselink.binfo);
    7409        97440 :       RTU (((lang_tree_node *)t)->baselink.functions);
    7410        97440 :       RT (((lang_tree_node *)t)->baselink.access_binfo);
    7411        97440 :       RT (((lang_tree_node *)t)->baselink.common.chain);
    7412        97440 :       break;
    7413              : 
    7414        41822 :     case CONSTRAINT_INFO:
    7415        41822 :       RT (((lang_tree_node *)t)->constraint_info.template_reqs);
    7416        41822 :       RT (((lang_tree_node *)t)->constraint_info.declarator_reqs);
    7417        41822 :       RT (((lang_tree_node *)t)->constraint_info.associated_constr);
    7418        41822 :       break;
    7419              : 
    7420         6936 :     case DEFERRED_NOEXCEPT:
    7421         6936 :       RT (((lang_tree_node *)t)->deferred_noexcept.pattern);
    7422         6936 :       RT (((lang_tree_node *)t)->deferred_noexcept.args);
    7423         6936 :       break;
    7424              : 
    7425         3784 :     case LAMBDA_EXPR:
    7426         3784 :       RT (((lang_tree_node *)t)->lambda_expression.capture_list);
    7427         3784 :       RT (((lang_tree_node *)t)->lambda_expression.this_capture);
    7428         3784 :       RT (((lang_tree_node *)t)->lambda_expression.extra_scope);
    7429         3784 :       RT (((lang_tree_node *)t)->lambda_expression.regen_info);
    7430         3784 :       RT (((lang_tree_node *)t)->lambda_expression.extra_args);
    7431              :       /* lambda_expression.pending_proxies is NULL  */
    7432         3784 :       ((lang_tree_node *)t)->lambda_expression.locus
    7433         3784 :         = state->read_location (*this);
    7434         3784 :       RUC (cp_lambda_default_capture_mode_type,
    7435              :            ((lang_tree_node *)t)->lambda_expression.default_capture_mode);
    7436         3784 :       RU (((lang_tree_node *)t)->lambda_expression.discriminator_scope);
    7437         3784 :       RU (((lang_tree_node *)t)->lambda_expression.discriminator_sig);
    7438         3784 :       break;
    7439              : 
    7440       539970 :     case OVERLOAD:
    7441       539970 :       RT (((lang_tree_node *)t)->overload.function);
    7442       539970 :       RT (t->common.chain);
    7443       539970 :       break;
    7444              : 
    7445            0 :     case PTRMEM_CST:
    7446            0 :       RT (((lang_tree_node *)t)->ptrmem.member);
    7447            0 :       break;
    7448              : 
    7449         7362 :     case STATIC_ASSERT:
    7450         7362 :       RT (((lang_tree_node *)t)->static_assertion.condition);
    7451         7362 :       RT (((lang_tree_node *)t)->static_assertion.message);
    7452         7362 :       ((lang_tree_node *)t)->static_assertion.location
    7453         7362 :         = state->read_location (*this);
    7454         7362 :       break;
    7455              : 
    7456       300682 :     case TEMPLATE_DECL:
    7457              :       /* Streamed when reading the raw template decl itself.  */
    7458       300682 :       gcc_assert (((lang_tree_node *)t)->template_decl.arguments);
    7459       300682 :       gcc_assert (((lang_tree_node *)t)->template_decl.result);
    7460       300682 :       if (DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (t))
    7461         6471 :         RT (DECL_CHAIN (t));
    7462              :       break;
    7463              : 
    7464       675155 :     case TEMPLATE_INFO:
    7465       675155 :       RT (((lang_tree_node *)t)->template_info.tmpl);
    7466       675155 :       RT (((lang_tree_node *)t)->template_info.args);
    7467       675155 :       RT (((lang_tree_node *)t)->template_info.partial);
    7468       675155 :       if (unsigned len = u ())
    7469              :         {
    7470            0 :           auto &ac = (((lang_tree_node *)t)
    7471              :                       ->template_info.deferred_access_checks);
    7472            0 :           vec_alloc (ac, len);
    7473            0 :           for (unsigned ix = 0; ix != len; ix++)
    7474              :             {
    7475            0 :               deferred_access_check m;
    7476              : 
    7477            0 :               RT (m.binfo);
    7478            0 :               RT (m.decl);
    7479            0 :               RT (m.diag_decl);
    7480            0 :               m.loc = state->read_location (*this);
    7481            0 :               ac->quick_push (m);
    7482              :             }
    7483              :         }
    7484              :       break;
    7485              : 
    7486       375826 :     case TEMPLATE_PARM_INDEX:
    7487       375826 :       RU (((lang_tree_node *)t)->tpi.index);
    7488       375826 :       RU (((lang_tree_node *)t)->tpi.level);
    7489       375826 :       RU (((lang_tree_node *)t)->tpi.orig_level);
    7490       375826 :       RT (((lang_tree_node *)t)->tpi.decl);
    7491       375826 :       break;
    7492              : 
    7493         7757 :     case TRAIT_EXPR:
    7494         7757 :       RT (((lang_tree_node *)t)->trait_expression.type1);
    7495         7757 :       RT (((lang_tree_node *)t)->trait_expression.type2);
    7496         7757 :       RUC (cp_trait_kind, ((lang_tree_node *)t)->trait_expression.kind);
    7497         7757 :       break;
    7498              : 
    7499            2 :     case TU_LOCAL_ENTITY:
    7500            2 :       RT (((lang_tree_node *)t)->tu_local_entity.name);
    7501            2 :       ((lang_tree_node *)t)->tu_local_entity.loc
    7502            2 :         = state->read_location (*this);
    7503              :     }
    7504              : 
    7505     14057501 :   if (CODE_CONTAINS_STRUCT (code, TS_TYPED))
    7506              :     {
    7507     13046842 :       tree type = tree_node ();
    7508              : 
    7509     13046842 :       if (type && code == ENUMERAL_TYPE && !ENUM_FIXED_UNDERLYING_TYPE_P (t))
    7510              :         {
    7511         1676 :           unsigned precision = u ();
    7512              : 
    7513         1676 :           type = build_distinct_type_copy (type);
    7514         1676 :           TYPE_PRECISION (type) = precision;
    7515         3352 :           set_min_and_max_values_for_integral_type (type, precision,
    7516         1676 :                                                     TYPE_SIGN (type));
    7517              :         }
    7518              : 
    7519     13046842 :       if (code != TEMPLATE_DECL)
    7520     12746160 :         t->typed.type = type;
    7521              :     }
    7522              : 
    7523     14057501 :   if (TREE_CODE (t) == CONSTRUCTOR)
    7524        50476 :     if (unsigned len = u ())
    7525              :       {
    7526        30447 :         vec_alloc (t->constructor.elts, len);
    7527       205341 :         for (unsigned ix = 0; ix != len; ix++)
    7528              :           {
    7529       174894 :             constructor_elt elt;
    7530              : 
    7531       174894 :             RT (elt.index);
    7532       174894 :             RTU (elt.value);
    7533       174894 :             t->constructor.elts->quick_push (elt);
    7534              :           }
    7535              :       }
    7536              : 
    7537              : #undef RT
    7538              : #undef RM
    7539              : #undef RU
    7540     14057501 :   return !get_overrun ();
    7541              : }
    7542              : 
    7543              : void
    7544      4544644 : trees_out::lang_decl_vals (tree t)
    7545              : {
    7546      4544644 :   const struct lang_decl *lang = DECL_LANG_SPECIFIC (t);
    7547              : #define WU(X) (u (X))
    7548              : #define WT(X) (tree_node (X))
    7549              :   /* Module index already written.  */
    7550      4544644 :   switch (lang->u.base.selector)
    7551              :     {
    7552            0 :     default:
    7553            0 :       gcc_unreachable ();
    7554              : 
    7555       849214 :     case lds_fn:  /* lang_decl_fn.  */
    7556       849214 :       if (streaming_p ())
    7557              :         {
    7558       424500 :           if (DECL_NAME (t) && IDENTIFIER_OVL_OP_P (DECL_NAME (t)))
    7559        69821 :             WU (lang->u.fn.ovl_op_code);
    7560              :         }
    7561              : 
    7562      1075642 :       if (DECL_CLASS_SCOPE_P (t) || DECL_UNIQUE_FRIEND_P (t))
    7563       644538 :         WT (lang->u.fn.context);
    7564              : 
    7565       849214 :       if (lang->u.fn.thunk_p)
    7566              :         {
    7567              :           /* The thunked-to function.  */
    7568         1176 :           WT (lang->u.fn.befriending_classes);
    7569         1176 :           if (streaming_p ())
    7570          588 :             wi (lang->u.fn.u5.fixed_offset);
    7571              :         }
    7572       848038 :       else if (decl_tls_wrapper_p (t))
    7573              :         /* The wrapped variable.  */
    7574           18 :         WT (lang->u.fn.befriending_classes);
    7575              :       else
    7576       848020 :         WT (lang->u.fn.u5.cloned_function);
    7577              : 
    7578       849214 :       if (FNDECL_USED_AUTO (t))
    7579         2833 :         WT (lang->u.fn.u.saved_auto_return_type);
    7580              : 
    7581       849214 :       goto lds_min;
    7582              : 
    7583         3332 :     case lds_decomp:  /* lang_decl_decomp.  */
    7584         3332 :       WT (lang->u.decomp.base);
    7585         3332 :       goto lds_min;
    7586              : 
    7587      2680920 :     case lds_min:  /* lang_decl_min.  */
    7588      2680920 :     lds_min:
    7589      2680920 :       WT (lang->u.min.template_info);
    7590      2680920 :       {
    7591      2680920 :         tree access = lang->u.min.access;
    7592              : 
    7593              :         /* DECL_ACCESS needs to be maintained by the definition of the
    7594              :            (derived) class that changes the access.  The other users
    7595              :            of DECL_ACCESS need to write it here.  */
    7596       849214 :         if (!DECL_THUNK_P (t)
    7597      3528958 :             && (DECL_CONTEXT (t) && TYPE_P (DECL_CONTEXT (t))))
    7598              :           access = NULL_TREE;
    7599              : 
    7600      2680920 :         WT (access);
    7601              :       }
    7602              :       /* A friend template specialisation stashes its owning class on its
    7603              :          DECL_CHAIN; we need to reconstruct this, but it needs to happen
    7604              :          after we stream the template_info so readers can know this is such
    7605              :          an entity.  */
    7606      2680920 :       if (decl_specialization_friend_p (t))
    7607           90 :         WT (t->common.chain);
    7608              :       break;
    7609              : 
    7610              :     case lds_ns:  /* lang_decl_ns.  */
    7611              :       break;
    7612              : 
    7613      1863531 :     case lds_parm:  /* lang_decl_parm.  */
    7614      1863531 :       if (streaming_p ())
    7615              :         {
    7616       634862 :           WU (lang->u.parm.level);
    7617       634862 :           WU (lang->u.parm.index);
    7618              :         }
    7619              :       break;
    7620              :     }
    7621              : #undef WU
    7622              : #undef WT
    7623      4544644 : }
    7624              : 
    7625              : bool
    7626      1910348 : trees_in::lang_decl_vals (tree t)
    7627              : {
    7628      1910348 :   struct lang_decl *lang = DECL_LANG_SPECIFIC (t);
    7629              : #define RU(X) ((X) = u ())
    7630              : #define RT(X) ((X) = tree_node ())
    7631              : 
    7632              :   /* Module index already read.  */
    7633      1910348 :   switch (lang->u.base.selector)
    7634              :     {
    7635            0 :     default:
    7636            0 :       gcc_unreachable ();
    7637              : 
    7638       425412 :     case lds_fn:  /* lang_decl_fn.  */
    7639       425412 :       if (DECL_NAME (t) && IDENTIFIER_OVL_OP_P (DECL_NAME (t)))
    7640              :         {
    7641        73571 :           unsigned code = u ();
    7642              : 
    7643              :           /* Check consistency.  */
    7644        73571 :           if (code >= OVL_OP_MAX
    7645        73571 :               || (ovl_op_info[IDENTIFIER_ASSIGN_OP_P (DECL_NAME (t))][code]
    7646        73571 :                   .ovl_op_code) == OVL_OP_ERROR_MARK)
    7647            0 :             set_overrun ();
    7648              :           else
    7649        73571 :             lang->u.fn.ovl_op_code = code;
    7650              :         }
    7651              : 
    7652       528374 :       if (DECL_CLASS_SCOPE_P (t) || DECL_UNIQUE_FRIEND_P (t))
    7653       334765 :         RT (lang->u.fn.context);
    7654              : 
    7655       425412 :       if (lang->u.fn.thunk_p)
    7656              :         {
    7657          542 :           RT (lang->u.fn.befriending_classes);
    7658          542 :           lang->u.fn.u5.fixed_offset = wi ();
    7659              :         }
    7660       424870 :       else if (decl_tls_wrapper_p (t))
    7661           15 :         RT (lang->u.fn.befriending_classes);
    7662              :       else
    7663       424855 :         RT (lang->u.fn.u5.cloned_function);
    7664              : 
    7665       425412 :       if (FNDECL_USED_AUTO (t))
    7666         1410 :         RT (lang->u.fn.u.saved_auto_return_type);
    7667       425412 :       goto lds_min;
    7668              : 
    7669         1981 :     case lds_decomp:  /* lang_decl_decomp.  */
    7670         1981 :       RT (lang->u.decomp.base);
    7671         1981 :       goto lds_min;
    7672              : 
    7673      1286284 :     case lds_min:  /* lang_decl_min.  */
    7674      1286284 :     lds_min:
    7675      1286284 :       RT (lang->u.min.template_info);
    7676      1286284 :       RT (lang->u.min.access);
    7677      1286284 :       if (decl_specialization_friend_p (t))
    7678           49 :         RT (t->common.chain);
    7679              :       break;
    7680              : 
    7681              :     case lds_ns:  /* lang_decl_ns.  */
    7682              :       break;
    7683              : 
    7684       623929 :     case lds_parm:  /* lang_decl_parm.  */
    7685       623929 :       RU (lang->u.parm.level);
    7686       623929 :       RU (lang->u.parm.index);
    7687       623929 :       break;
    7688              :     }
    7689              : #undef RU
    7690              : #undef RT
    7691      1910348 :   return !get_overrun ();
    7692              : }
    7693              : 
    7694              : /* Most of the value contents of lang_type is streamed in
    7695              :    define_class.  */
    7696              : 
    7697              : void
    7698       308734 : trees_out::lang_type_vals (tree t)
    7699              : {
    7700       308734 :   const struct lang_type *lang = TYPE_LANG_SPECIFIC (t);
    7701              : #define WU(X) (u (X))
    7702              : #define WT(X) (tree_node (X))
    7703       308734 :   if (streaming_p ())
    7704       154344 :     WU (lang->align);
    7705              : #undef WU
    7706              : #undef WT
    7707       308734 : }
    7708              : 
    7709              : bool
    7710       141656 : trees_in::lang_type_vals (tree t)
    7711              : {
    7712       141656 :   struct lang_type *lang = TYPE_LANG_SPECIFIC (t);
    7713              : #define RU(X) ((X) = u ())
    7714              : #define RT(X) ((X) = tree_node ())
    7715       141656 :   RU (lang->align);
    7716              : #undef RU
    7717              : #undef RT
    7718       141656 :   return !get_overrun ();
    7719              : }
    7720              : 
    7721              : /* Write out the bools of T, including information about any
    7722              :    LANG_SPECIFIC information.  Including allocation of any lang
    7723              :    specific object.  */
    7724              : 
    7725              : void
    7726     13905950 : trees_out::tree_node_bools (tree t)
    7727              : {
    7728     13905950 :   gcc_checking_assert (streaming_p ());
    7729              : 
    7730              :   /* We should never stream a namespace.  */
    7731     13905950 :   gcc_checking_assert (TREE_CODE (t) != NAMESPACE_DECL
    7732              :                        || DECL_NAMESPACE_ALIAS (t));
    7733              : 
    7734     13905950 :   bits_out bits = stream_bits ();
    7735     13905950 :   core_bools (t, bits);
    7736              : 
    7737     13905950 :   switch (TREE_CODE_CLASS (TREE_CODE (t)))
    7738              :     {
    7739      3075812 :     case tcc_declaration:
    7740      3075812 :       {
    7741      3075812 :         bool specific = DECL_LANG_SPECIFIC (t) != NULL;
    7742      3075812 :         bits.b (specific);
    7743      3075812 :         if (specific && VAR_P (t))
    7744       310956 :           bits.b (DECL_DECOMPOSITION_P (t));
    7745      1967508 :         if (specific)
    7746      1967508 :           lang_decl_bools (t, bits);
    7747              :       }
    7748              :       break;
    7749              : 
    7750       576565 :     case tcc_type:
    7751       576565 :       {
    7752       576565 :         bool specific = (TYPE_MAIN_VARIANT (t) == t
    7753       576565 :                          && TYPE_LANG_SPECIFIC (t) != NULL);
    7754       576565 :         gcc_assert (TYPE_LANG_SPECIFIC (t)
    7755              :                     == TYPE_LANG_SPECIFIC (TYPE_MAIN_VARIANT (t)));
    7756              : 
    7757       576565 :         bits.b (specific);
    7758       576565 :         if (specific)
    7759       154344 :           lang_type_bools (t, bits);
    7760              :       }
    7761              :       break;
    7762              : 
    7763              :     default:
    7764              :       break;
    7765              :     }
    7766              : 
    7767     13905950 :   bits.bflush ();
    7768     13905950 : }
    7769              : 
    7770              : bool
    7771     14077627 : trees_in::tree_node_bools (tree t)
    7772              : {
    7773     14077627 :   bits_in bits = stream_bits ();
    7774     14077627 :   bool ok = core_bools (t, bits);
    7775              : 
    7776     14077627 :   if (ok)
    7777     14077627 :     switch (TREE_CODE_CLASS (TREE_CODE (t)))
    7778              :       {
    7779      3015033 :       case tcc_declaration:
    7780      3015033 :         if (bits.b ())
    7781              :           {
    7782      1910348 :             bool decomp = VAR_P (t) && bits.b ();
    7783              : 
    7784      1910348 :             ok = maybe_add_lang_decl_raw (t, decomp);
    7785      1910348 :             if (ok)
    7786      1910348 :               ok = lang_decl_bools (t, bits);
    7787              :           }
    7788              :         break;
    7789              : 
    7790       536648 :       case tcc_type:
    7791       536648 :         if (bits.b ())
    7792              :           {
    7793       141656 :             ok = maybe_add_lang_type_raw (t);
    7794       141656 :             if (ok)
    7795       141656 :               ok = lang_type_bools (t, bits);
    7796              :           }
    7797              :         break;
    7798              : 
    7799              :       default:
    7800              :         break;
    7801              :       }
    7802              : 
    7803     14077627 :   bits.bflush ();
    7804     14077627 :   if (!ok || get_overrun ())
    7805            0 :     return false;
    7806              : 
    7807              :   return true;
    7808     14077627 : }
    7809              : 
    7810              : 
    7811              : /* Write out the lang-specific vals of node T.  */
    7812              : 
    7813              : void
    7814     35858856 : trees_out::lang_vals (tree t)
    7815              : {
    7816     35858856 :   switch (TREE_CODE_CLASS (TREE_CODE (t)))
    7817              :     {
    7818      8024552 :     case tcc_declaration:
    7819      8024552 :       if (DECL_LANG_SPECIFIC (t))
    7820      4544644 :         lang_decl_vals (t);
    7821              :       break;
    7822              : 
    7823      1991077 :     case tcc_type:
    7824      1991077 :       if (TYPE_MAIN_VARIANT (t) == t && TYPE_LANG_SPECIFIC (t))
    7825       308734 :         lang_type_vals (t);
    7826              :       break;
    7827              : 
    7828              :     default:
    7829              :       break;
    7830              :     }
    7831     35858856 : }
    7832              : 
    7833              : bool
    7834     14057501 : trees_in::lang_vals (tree t)
    7835              : {
    7836     14057501 :   bool ok = true;
    7837              : 
    7838     14057501 :   switch (TREE_CODE_CLASS (TREE_CODE (t)))
    7839              :     {
    7840      3015033 :     case tcc_declaration:
    7841      3015033 :       if (DECL_LANG_SPECIFIC (t))
    7842      1910348 :         ok = lang_decl_vals (t);
    7843              :       break;
    7844              : 
    7845       516522 :     case tcc_type:
    7846       516522 :       if (TYPE_LANG_SPECIFIC (t))
    7847       141656 :         ok = lang_type_vals (t);
    7848              :       else
    7849       374866 :         TYPE_LANG_SPECIFIC (t) = TYPE_LANG_SPECIFIC (TYPE_MAIN_VARIANT (t));
    7850              :       break;
    7851              : 
    7852              :     default:
    7853              :       break;
    7854              :     }
    7855              : 
    7856     14057501 :   return ok;
    7857              : }
    7858              : 
    7859              : /* Write out the value fields of node T.  */
    7860              : 
    7861              : void
    7862     35858856 : trees_out::tree_node_vals (tree t)
    7863              : {
    7864     35858856 :   core_vals (t);
    7865     35858856 :   lang_vals (t);
    7866     35858856 : }
    7867              : 
    7868              : bool
    7869     14057501 : trees_in::tree_node_vals (tree t)
    7870              : {
    7871     14057501 :   bool ok = core_vals (t);
    7872     14057501 :   if (ok)
    7873     14057501 :     ok = lang_vals (t);
    7874              : 
    7875     14057501 :   return ok;
    7876              : }
    7877              : 
    7878              : 
    7879              : /* If T is a back reference, fixed reference or NULL, write out its
    7880              :    code and return WK_none.  Otherwise return WK_value if we must write
    7881              :    by value, or WK_normal otherwise.  */
    7882              : 
    7883              : walk_kind
    7884    237873987 : trees_out::ref_node (tree t)
    7885              : {
    7886    237873987 :   if (!t)
    7887              :     {
    7888     96528036 :       if (streaming_p ())
    7889              :         {
    7890              :           /* NULL_TREE -> tt_null.  */
    7891     36960984 :           null_count++;
    7892     36960984 :           i (tt_null);
    7893              :         }
    7894     96528036 :       return WK_none;
    7895              :     }
    7896              : 
    7897    141345951 :   if (!TREE_VISITED (t))
    7898              :     return WK_normal;
    7899              : 
    7900              :   /* An already-visited tree.  It must be in the map.  */
    7901     82900697 :   int val = get_tag (t);
    7902              : 
    7903     82900697 :   if (val == tag_value)
    7904              :     /* An entry we should walk into.  */
    7905              :     return WK_value;
    7906              : 
    7907     81563084 :   const char *kind;
    7908              : 
    7909     81563084 :   if (val <= tag_backref)
    7910              :     {
    7911              :       /* Back reference -> -ve number  */
    7912     62118973 :       if (streaming_p ())
    7913     29463657 :         i (val);
    7914              :       kind = "backref";
    7915              :     }
    7916     19444111 :   else if (val >= tag_fixed)
    7917              :     {
    7918              :       /* Fixed reference -> tt_fixed */
    7919     19444111 :       val -= tag_fixed;
    7920     19444111 :       if (streaming_p ())
    7921      6904677 :         i (tt_fixed), u (val);
    7922              :       kind = "fixed";
    7923              :     }
    7924              : 
    7925     81563084 :   if (streaming_p ())
    7926              :     {
    7927     36368334 :       back_ref_count++;
    7928     36368334 :       dump (dumper::TREE)
    7929        14184 :         && dump ("Wrote %s:%d %C:%N%S", kind, val, TREE_CODE (t), t, t);
    7930              :     }
    7931              :   return WK_none;
    7932              : }
    7933              : 
    7934              : tree
    7935     29513931 : trees_in::back_ref (int tag)
    7936              : {
    7937     29513931 :   tree res = NULL_TREE;
    7938              : 
    7939     29513931 :   if (tag < 0 && unsigned (~tag) < back_refs.length ())
    7940     29513931 :     res = back_refs[~tag];
    7941              : 
    7942     29513931 :   if (!res
    7943              :       /* Checking TREE_CODE is a dereference, so we know this is not a
    7944              :          wild pointer.  Checking the code provides evidence we've not
    7945              :          corrupted something.  */
    7946     29513931 :       || TREE_CODE (res) >= MAX_TREE_CODES)
    7947            0 :     set_overrun ();
    7948              :   else
    7949     29528471 :     dump (dumper::TREE) && dump ("Read backref:%d found %C:%N%S", tag,
    7950              :                                  TREE_CODE (res), res, res);
    7951     29513931 :   return res;
    7952              : }
    7953              : 
    7954              : unsigned
    7955      3194300 : trees_out::add_indirect_tpl_parms (tree parms)
    7956              : {
    7957      3194300 :   unsigned len = 0;
    7958      5981462 :   for (; parms; parms = TREE_CHAIN (parms), len++)
    7959              :     {
    7960      3378257 :       if (TREE_VISITED (parms))
    7961              :         break;
    7962              : 
    7963      2787162 :       int tag = insert (parms);
    7964      2787162 :       if (streaming_p ())
    7965      2787489 :         dump (dumper::TREE)
    7966          153 :           && dump ("Indirect:%d template's parameter %u %C:%N",
    7967          153 :                    tag, len, TREE_CODE (parms), parms);
    7968              :     }
    7969              : 
    7970      3194300 :   if (streaming_p ())
    7971       553780 :     u (len);
    7972              : 
    7973      3194300 :   return len;
    7974              : }
    7975              : 
    7976              : unsigned
    7977       528589 : trees_in::add_indirect_tpl_parms (tree parms)
    7978              : {
    7979       528589 :   unsigned len = u ();
    7980       947532 :   for (unsigned ix = 0; ix != len; parms = TREE_CHAIN (parms), ix++)
    7981              :     {
    7982       418943 :       int tag = insert (parms);
    7983       419408 :       dump (dumper::TREE)
    7984          159 :         && dump ("Indirect:%d template's parameter %u %C:%N",
    7985          159 :                  tag, ix, TREE_CODE (parms), parms);
    7986              :     }
    7987              : 
    7988       528589 :   return len;
    7989              : }
    7990              : 
    7991              : /* We've just found DECL by name.  Insert nodes that come with it, but
    7992              :    cannot be found by name, so we'll not accidentally walk into them.  */
    7993              : 
    7994              : void
    7995      7281138 : trees_out::add_indirects (tree decl)
    7996              : {
    7997      7281138 :   unsigned count = 0;
    7998              : 
    7999              :   // FIXME:OPTIMIZATION We'll eventually want default fn parms of
    8000              :   // templates and perhaps default template parms too.  The former can
    8001              :   // be referenced from instantiations (as they are lazily
    8002              :   // instantiated).  Also (deferred?) exception specifications of
    8003              :   // templates.  See the note about PARM_DECLs in trees_out::decl_node.
    8004      7281138 :   tree inner = decl;
    8005      7281138 :   if (TREE_CODE (decl) == TEMPLATE_DECL)
    8006              :     {
    8007      3194300 :       count += add_indirect_tpl_parms (DECL_TEMPLATE_PARMS (decl));
    8008              : 
    8009      3194300 :       inner = DECL_TEMPLATE_RESULT (decl);
    8010      3194300 :       int tag = insert (inner);
    8011      3194300 :       if (streaming_p ())
    8012       553780 :         dump (dumper::TREE)
    8013          219 :           && dump ("Indirect:%d template's result %C:%N",
    8014          219 :                    tag, TREE_CODE (inner), inner);
    8015      3194300 :       count++;
    8016              :     }
    8017              : 
    8018      7281138 :   if (TREE_CODE (inner) == TYPE_DECL)
    8019              :     {
    8020              :       /* Make sure the type is in the map too.  Otherwise we get
    8021              :          different RECORD_TYPEs for the same type, and things go
    8022              :          south.  */
    8023      3930636 :       tree type = TREE_TYPE (inner);
    8024      3930636 :       gcc_checking_assert (DECL_ORIGINAL_TYPE (inner)
    8025              :                            || TYPE_NAME (type) == inner);
    8026      3930636 :       int tag = insert (type);
    8027      3930636 :       if (streaming_p ())
    8028       456290 :         dump (dumper::TREE) && dump ("Indirect:%d decl's type %C:%N", tag,
    8029          362 :                                      TREE_CODE (type), type);
    8030      3930636 :       count++;
    8031              :     }
    8032              : 
    8033      7281138 :   if (streaming_p ())
    8034              :     {
    8035      1150296 :       u (count);
    8036      1150825 :       dump (dumper::TREE) && dump ("Inserted %u indirects", count);
    8037              :     }
    8038      7281138 : }
    8039              : 
    8040              : bool
    8041      1046281 : trees_in::add_indirects (tree decl)
    8042              : {
    8043      1046281 :   unsigned count = 0;
    8044              : 
    8045      1046281 :   tree inner = decl;
    8046      1046281 :   if (TREE_CODE (inner) == TEMPLATE_DECL)
    8047              :     {
    8048       528589 :       count += add_indirect_tpl_parms (DECL_TEMPLATE_PARMS (decl));
    8049              : 
    8050       528589 :       inner = DECL_TEMPLATE_RESULT (decl);
    8051       528589 :       int tag = insert (inner);
    8052       528589 :       dump (dumper::TREE)
    8053          228 :         && dump ("Indirect:%d templates's result %C:%N", tag,
    8054          228 :                  TREE_CODE (inner), inner);
    8055       528589 :       count++;
    8056              :     }
    8057              : 
    8058      1046281 :   if (TREE_CODE (inner) == TYPE_DECL)
    8059              :     {
    8060       394370 :       tree type = TREE_TYPE (inner);
    8061       394370 :       gcc_checking_assert (DECL_ORIGINAL_TYPE (inner)
    8062              :                            || TYPE_NAME (type) == inner);
    8063       394370 :       int tag = insert (type);
    8064       394370 :       dump (dumper::TREE)
    8065          362 :         && dump ("Indirect:%d decl's type %C:%N", tag, TREE_CODE (type), type);
    8066       394370 :       count++;
    8067              :     }
    8068              : 
    8069      1046886 :   dump (dumper::TREE) && dump ("Inserted %u indirects", count);
    8070      1046281 :   return count == u ();
    8071              : }
    8072              : 
    8073              : /* Stream a template parameter.  There are 4.5 kinds of parameter:
    8074              :    a) Template - TEMPLATE_DECL->TYPE_DECL->TEMPLATE_TEMPLATE_PARM
    8075              :         TEMPLATE_TYPE_PARM_INDEX TPI
    8076              :    b) Type - TYPE_DECL->TEMPLATE_TYPE_PARM TEMPLATE_TYPE_PARM_INDEX TPI
    8077              :    c.1) NonTYPE - PARM_DECL DECL_INITIAL TPI We meet this first
    8078              :    c.2) NonTYPE - CONST_DECL DECL_INITIAL Same TPI
    8079              :    d) BoundTemplate - TYPE_DECL->BOUND_TEMPLATE_TEMPLATE_PARM
    8080              :        TEMPLATE_TYPE_PARM_INDEX->TPI
    8081              :        TEMPLATE_TEMPLATE_PARM_INFO->TEMPLATE_INFO
    8082              : 
    8083              :    All of these point to a TEMPLATE_PARM_INDEX, and #B also has a TEMPLATE_INFO
    8084              : */
    8085              : 
    8086              : void
    8087      1810749 : trees_out::tpl_parm_value (tree parm)
    8088              : {
    8089      1810749 :   gcc_checking_assert (DECL_P (parm) && DECL_TEMPLATE_PARM_P (parm));
    8090              : 
    8091      1810749 :   int parm_tag = insert (parm);
    8092      1810749 :   if (streaming_p ())
    8093              :     {
    8094       412886 :       i (tt_tpl_parm);
    8095       412886 :       dump (dumper::TREE) && dump ("Writing template parm:%d %C:%N",
    8096          114 :                                    parm_tag, TREE_CODE (parm), parm);
    8097       412886 :       start (parm);
    8098       412886 :       tree_node_bools (parm);
    8099              :     }
    8100              : 
    8101      1810749 :   tree inner = parm;
    8102      1810749 :   if (TREE_CODE (inner) == TEMPLATE_DECL)
    8103              :     {
    8104         5582 :       inner = DECL_TEMPLATE_RESULT (inner);
    8105         5582 :       int inner_tag = insert (inner);
    8106         5582 :       if (streaming_p ())
    8107              :         {
    8108         1466 :           dump (dumper::TREE) && dump ("Writing inner template parm:%d %C:%N",
    8109            0 :                                        inner_tag, TREE_CODE (inner), inner);
    8110         1466 :           start (inner);
    8111         1466 :           tree_node_bools (inner);
    8112              :         }
    8113              :     }
    8114              : 
    8115      1810749 :   tree type = NULL_TREE;
    8116      1810749 :   if (TREE_CODE (inner) == TYPE_DECL)
    8117              :     {
    8118      1628609 :       type = TREE_TYPE (inner);
    8119      1628609 :       int type_tag = insert (type);
    8120      1628609 :       if (streaming_p ())
    8121              :         {
    8122       372651 :           dump (dumper::TREE) && dump ("Writing template parm type:%d %C:%N",
    8123          108 :                                        type_tag, TREE_CODE (type), type);
    8124       372651 :           start (type);
    8125       372651 :           tree_node_bools (type);
    8126              :         }
    8127              :     }
    8128              : 
    8129      1810749 :   if (inner != parm)
    8130              :     {
    8131              :       /* This is a template-template parameter.  */
    8132         5582 :       unsigned tpl_levels = 0;
    8133         5582 :       tpl_header (parm, &tpl_levels);
    8134         5582 :       tpl_parms_fini (parm, tpl_levels);
    8135              :     }
    8136              : 
    8137      1810749 :   tree_node_vals (parm);
    8138      1810749 :   if (inner != parm)
    8139         5582 :     tree_node_vals (inner);
    8140      1810749 :   if (type)
    8141              :     {
    8142      1628609 :       tree_node_vals (type);
    8143      1628609 :       if (DECL_NAME (inner) == auto_identifier
    8144      1628609 :           || DECL_NAME (inner) == decltype_auto_identifier)
    8145              :         {
    8146              :           /* Placeholder auto.  */
    8147        71005 :           tree_node (DECL_INITIAL (inner));
    8148        71005 :           tree_node (DECL_SIZE_UNIT (inner));
    8149              :         }
    8150              :     }
    8151              : 
    8152      1810749 :   if (streaming_p ())
    8153       412886 :     dump (dumper::TREE) && dump ("Wrote template parm:%d %C:%N",
    8154          114 :                                  parm_tag, TREE_CODE (parm), parm);
    8155      1810749 : }
    8156              : 
    8157              : tree
    8158       392200 : trees_in::tpl_parm_value ()
    8159              : {
    8160       392200 :   tree parm = start ();
    8161       392200 :   if (!parm || !tree_node_bools (parm))
    8162            0 :     return NULL_TREE;
    8163              : 
    8164       392200 :   int parm_tag = insert (parm);
    8165       392200 :   dump (dumper::TREE) && dump ("Reading template parm:%d %C:%N",
    8166          198 :                                parm_tag, TREE_CODE (parm), parm);
    8167              : 
    8168       392200 :   tree inner = parm;
    8169       392200 :   if (TREE_CODE (inner) == TEMPLATE_DECL)
    8170              :     {
    8171         1183 :       inner = start ();
    8172         1183 :       if (!inner || !tree_node_bools (inner))
    8173            0 :         return NULL_TREE;
    8174         1183 :       int inner_tag = insert (inner);
    8175         1183 :       dump (dumper::TREE) && dump ("Reading inner template parm:%d %C:%N",
    8176            0 :                                    inner_tag, TREE_CODE (inner), inner);
    8177         1183 :       DECL_TEMPLATE_RESULT (parm) = inner;
    8178              :     }
    8179              : 
    8180       392200 :   tree type = NULL_TREE;
    8181       392200 :   if (TREE_CODE (inner) == TYPE_DECL)
    8182              :     {
    8183       352846 :       type = start ();
    8184       352846 :       if (!type || !tree_node_bools (type))
    8185            0 :         return NULL_TREE;
    8186       352846 :       int type_tag = insert (type);
    8187       352846 :       dump (dumper::TREE) && dump ("Reading template parm type:%d %C:%N",
    8188          120 :                                    type_tag, TREE_CODE (type), type);
    8189              : 
    8190       352846 :       TREE_TYPE (inner) = TREE_TYPE (parm) = type;
    8191       352846 :       TYPE_NAME (type) = parm;
    8192              :     }
    8193              : 
    8194       392200 :   if (inner != parm)
    8195              :     {
    8196              :       /* A template template parameter.  */
    8197         1183 :       unsigned tpl_levels = 0;
    8198         1183 :       tpl_header (parm, &tpl_levels);
    8199         1183 :       tpl_parms_fini (parm, tpl_levels);
    8200              :     }
    8201              : 
    8202       392200 :   tree_node_vals (parm);
    8203       392200 :   if (inner != parm)
    8204         1183 :     tree_node_vals (inner);
    8205       392200 :   if (type)
    8206              :     {
    8207       352846 :       tree_node_vals (type);
    8208       352846 :       if (DECL_NAME (inner) == auto_identifier
    8209       352846 :           || DECL_NAME (inner) == decltype_auto_identifier)
    8210              :         {
    8211              :           /* Placeholder auto.  */
    8212        30734 :           DECL_INITIAL (inner) = tree_node ();
    8213        30734 :           DECL_SIZE_UNIT (inner) = tree_node ();
    8214              :         }
    8215       352846 :       if (TYPE_CANONICAL (type))
    8216              :         {
    8217       352846 :           gcc_checking_assert (TYPE_CANONICAL (type) == type);
    8218       352846 :           TYPE_CANONICAL (type) = canonical_type_parameter (type);
    8219              :         }
    8220              :     }
    8221              : 
    8222       392200 :   dump (dumper::TREE) && dump ("Read template parm:%d %C:%N",
    8223          198 :                                parm_tag, TREE_CODE (parm), parm);
    8224              : 
    8225              :   return parm;
    8226              : }
    8227              : 
    8228              : void
    8229      1161114 : trees_out::install_entity (tree decl, depset *dep)
    8230              : {
    8231      1161114 :   gcc_checking_assert (streaming_p ());
    8232              : 
    8233              :   /* Write the entity index, so we can insert it as soon as we
    8234              :      know this is new.  */
    8235      1161114 :   u (dep ? dep->cluster + 1 : 0);
    8236      1161114 :   if (CHECKING_P && dep)
    8237              :     {
    8238              :       /* Add it to the entity map, such that we can tell it is
    8239              :          part of us.  */
    8240       862589 :       bool existed;
    8241       862589 :       unsigned *slot = &entity_map->get_or_insert
    8242       862589 :         (DECL_UID (decl), &existed);
    8243       862589 :       if (existed)
    8244              :         /* If it existed, it should match.  */
    8245         1604 :         gcc_checking_assert (decl == (*entity_ary)[*slot]);
    8246       862589 :       *slot = ~dep->cluster;
    8247              :     }
    8248      1161114 : }
    8249              : 
    8250              : bool
    8251      1117422 : trees_in::install_entity (tree decl)
    8252              : {
    8253      1117422 :   unsigned entity_index = u ();
    8254      1117422 :   if (!entity_index)
    8255              :     return false;
    8256              : 
    8257       813201 :   if (entity_index > state->entity_num)
    8258              :     {
    8259            0 :       set_overrun ();
    8260            0 :       return false;
    8261              :     }
    8262              : 
    8263              :   /* Insert the real decl into the entity ary.  */
    8264       813201 :   unsigned ident = state->entity_lwm + entity_index - 1;
    8265       813201 :   (*entity_ary)[ident] = decl;
    8266              : 
    8267              :   /* And into the entity map, if it's not already there.  */
    8268       813201 :   tree not_tmpl = STRIP_TEMPLATE (decl);
    8269       813201 :   if (!DECL_LANG_SPECIFIC (not_tmpl)
    8270      1540894 :       || !DECL_MODULE_ENTITY_P (not_tmpl))
    8271              :     {
    8272              :       /* We don't want to use retrofit_lang_decl directly so that we aren't
    8273              :          affected by the language state when we load in.  */
    8274       812083 :       if (!DECL_LANG_SPECIFIC (not_tmpl))
    8275              :         {
    8276        85508 :           maybe_add_lang_decl_raw (not_tmpl, false);
    8277        85508 :           SET_DECL_LANGUAGE (not_tmpl, lang_cplusplus);
    8278              :         }
    8279       812083 :       DECL_MODULE_ENTITY_P (not_tmpl) = true;
    8280              : 
    8281              :       /* Insert into the entity hash (it cannot already be there).  */
    8282       812083 :       bool existed;
    8283       812083 :       unsigned &slot = entity_map->get_or_insert (DECL_UID (decl), &existed);
    8284       812083 :       gcc_checking_assert (!existed);
    8285       812083 :       slot = ident;
    8286              :     }
    8287              :   else
    8288              :     {
    8289         1118 :       unsigned *slot = entity_map->get (DECL_UID (decl));
    8290              : 
    8291              :       /* The entity must be in the entity map already.  However, DECL may
    8292              :          be the DECL_TEMPLATE_RESULT of an existing partial specialisation
    8293              :          if we matched it while streaming another instantiation; in this
    8294              :          case we already registered that TEMPLATE_DECL.  */
    8295         1118 :       if (!slot)
    8296              :         {
    8297            9 :           tree type = TREE_TYPE (decl);
    8298            9 :           gcc_checking_assert (TREE_CODE (decl) == TYPE_DECL
    8299              :                                && CLASS_TYPE_P (type)
    8300              :                                && CLASSTYPE_TEMPLATE_SPECIALIZATION (type));
    8301            9 :           slot = entity_map->get (DECL_UID (CLASSTYPE_TI_TEMPLATE (type)));
    8302              :         }
    8303            9 :       gcc_checking_assert (slot);
    8304              : 
    8305         1118 :       if (state->is_partition ())
    8306              :         {
    8307              :           /* The decl is already in the entity map, but we see it again now
    8308              :              from a partition: we want to overwrite if the original decl
    8309              :              wasn't also from a (possibly different) partition.  Otherwise,
    8310              :              for things like template instantiations, make_dependency might
    8311              :              not realise that this is also provided from a partition and
    8312              :              should be considered part of this module (and thus always
    8313              :              emitted into the primary interface's CMI).  */
    8314          411 :           module_state *imp = import_entity_module (*slot);
    8315          411 :           if (!imp->is_partition ())
    8316          285 :             *slot = ident;
    8317              :         }
    8318              :     }
    8319              : 
    8320              :   return true;
    8321              : }
    8322              : 
    8323              : static bool has_definition (tree decl);
    8324              : 
    8325              : /* DECL is a decl node that must be written by value.  DEP is the
    8326              :    decl's depset.  */
    8327              : 
    8328              : void
    8329      3197782 : trees_out::decl_value (tree decl, depset *dep)
    8330              : {
    8331              :   /* We should not be writing clones or template parms.  */
    8332      3197782 :   gcc_checking_assert (DECL_P (decl)
    8333              :                        && !DECL_CLONED_FUNCTION_P (decl)
    8334              :                        && !DECL_TEMPLATE_PARM_P (decl));
    8335              : 
    8336              :   /* We should never be writing non-typedef ptrmemfuncs by value.  */
    8337      3197782 :   gcc_checking_assert (TREE_CODE (decl) != TYPE_DECL
    8338              :                        || DECL_ORIGINAL_TYPE (decl)
    8339              :                        || !TYPE_PTRMEMFUNC_P (TREE_TYPE (decl)));
    8340              : 
    8341              :   /* There's no need to walk any of the contents of a known TU-local entity,
    8342              :      since importers should never see any of it regardless.  But make sure we
    8343              :      at least note its location so importers can use it for diagnostics.  */
    8344      3197782 :   if (dep && dep->is_tu_local ())
    8345              :     {
    8346          416 :       gcc_checking_assert (is_initial_scan ());
    8347          416 :       insert (decl, WK_value);
    8348          416 :       state->note_location (DECL_SOURCE_LOCATION (decl));
    8349          416 :       return;
    8350              :     }
    8351              : 
    8352      3197366 :   merge_kind mk = get_merge_kind (decl, dep);
    8353              : 
    8354      3197366 :   if (CHECKING_P)
    8355              :     {
    8356              :       /* Never start in the middle of a template.  */
    8357      3197366 :       int use_tpl = -1;
    8358      3197366 :       if (tree ti = node_template_info (decl, use_tpl))
    8359      1091939 :         gcc_checking_assert (TREE_CODE (TI_TEMPLATE (ti)) == OVERLOAD
    8360              :                              || TREE_CODE (TI_TEMPLATE (ti)) == FIELD_DECL
    8361              :                              || (DECL_TEMPLATE_RESULT (TI_TEMPLATE (ti))
    8362              :                                  != decl));
    8363              :     }
    8364              : 
    8365      3197366 :   if (streaming_p ())
    8366              :     {
    8367              :       /* A new node -> tt_decl.  */
    8368      1161114 :       decl_val_count++;
    8369      1161114 :       i (tt_decl);
    8370      1161114 :       u (mk);
    8371      1161114 :       start (decl);
    8372              : 
    8373      1161114 :       if (mk != MK_unique)
    8374              :         {
    8375       981687 :           bits_out bits = stream_bits ();
    8376       981687 :           if (!(mk & MK_template_mask) && !state->is_header ())
    8377              :             {
    8378              :               /* Tell the importer whether this is a global module entity,
    8379              :                  or a module entity.  */
    8380       102983 :               tree o = get_originating_module_decl (decl);
    8381       102983 :               bool is_attached = false;
    8382              : 
    8383       102983 :               tree not_tmpl = STRIP_TEMPLATE (o);
    8384       102983 :               if (DECL_LANG_SPECIFIC (not_tmpl)
    8385       166960 :                   && DECL_MODULE_ATTACH_P (not_tmpl))
    8386              :                 is_attached = true;
    8387              : 
    8388       102983 :               bits.b (is_attached);
    8389              :             }
    8390       981687 :           bits.b (dep && dep->has_defn ());
    8391       981687 :         }
    8392      1161114 :       tree_node_bools (decl);
    8393              :     }
    8394              : 
    8395      3197366 :   int tag = insert (decl, WK_value);
    8396      3197366 :   if (streaming_p ())
    8397      1161114 :     dump (dumper::TREE)
    8398          683 :       && dump ("Writing %s:%d %C:%N%S", merge_kind_name[mk], tag,
    8399          683 :                TREE_CODE (decl), decl, decl);
    8400              : 
    8401      3197366 :   tree inner = decl;
    8402      3197366 :   int inner_tag = 0;
    8403      3197366 :   if (TREE_CODE (decl) == TEMPLATE_DECL)
    8404              :     {
    8405       916975 :       inner = DECL_TEMPLATE_RESULT (decl);
    8406       916975 :       inner_tag = insert (inner, WK_value);
    8407              : 
    8408              :       /* On stream-in we assume that a template and its result will
    8409              :          have the same type.  */
    8410       916975 :       gcc_checking_assert (TREE_TYPE (decl) == TREE_TYPE (inner));
    8411              : 
    8412       916975 :       if (streaming_p ())
    8413              :         {
    8414       305643 :           int code = TREE_CODE (inner);
    8415       305643 :           u (code);
    8416       305643 :           start (inner, true);
    8417       305643 :           tree_node_bools (inner);
    8418       305643 :           dump (dumper::TREE)
    8419          132 :             && dump ("Writing %s:%d %C:%N%S", merge_kind_name[mk], inner_tag,
    8420          132 :                      TREE_CODE (inner), inner, inner);
    8421              :         }
    8422              :     }
    8423              : 
    8424      3197366 :   tree type = NULL_TREE;
    8425      3197366 :   int type_tag = 0;
    8426      3197366 :   tree stub_decl = NULL_TREE;
    8427      3197366 :   int stub_tag = 0;
    8428      3197366 :   if (TREE_CODE (inner) == TYPE_DECL)
    8429              :     {
    8430      1181010 :       type = TREE_TYPE (inner);
    8431      1181010 :       bool has_type = (type == TYPE_MAIN_VARIANT (type)
    8432      1181010 :                        && TYPE_NAME (type) == inner);
    8433              : 
    8434      1181010 :       if (streaming_p ())
    8435       398321 :         u (has_type ? TREE_CODE (type) : 0);
    8436              : 
    8437      1181010 :       if (has_type)
    8438              :         {
    8439       543623 :           type_tag = insert (type, WK_value);
    8440       543623 :           if (streaming_p ())
    8441              :             {
    8442       181183 :               start (type, true);
    8443       181183 :               tree_node_bools (type);
    8444       181183 :               dump (dumper::TREE)
    8445          155 :                 && dump ("Writing type:%d %C:%N", type_tag,
    8446          155 :                          TREE_CODE (type), type);
    8447              :             }
    8448              : 
    8449       543623 :           stub_decl = TYPE_STUB_DECL (type);
    8450       543623 :           bool has_stub = inner != stub_decl;
    8451       543623 :           if (streaming_p ())
    8452       181183 :             u (has_stub ? TREE_CODE (stub_decl) : 0);
    8453       543623 :           if (has_stub)
    8454              :             {
    8455         2217 :               stub_tag = insert (stub_decl);
    8456         2217 :               if (streaming_p ())
    8457              :                 {
    8458          738 :                   start (stub_decl, true);
    8459          738 :                   tree_node_bools (stub_decl);
    8460          738 :                   dump (dumper::TREE)
    8461            0 :                     && dump ("Writing stub_decl:%d %C:%N", stub_tag,
    8462            0 :                              TREE_CODE (stub_decl), stub_decl);
    8463              :                 }
    8464              :             }
    8465              :           else
    8466              :             stub_decl = NULL_TREE;
    8467              :         }
    8468              :       else
    8469              :         /* Regular typedef.  */
    8470              :         type = NULL_TREE;
    8471              :     }
    8472              : 
    8473              :   /* Stream the container, we want it correctly canonicalized before
    8474              :      we start emitting keys for this decl.  */
    8475      3197366 :   tree container = decl_container (decl);
    8476      3197366 :   unsigned tpl_levels = 0;
    8477              : 
    8478              :   /* Also tell the importer whether this is a temploid friend attached
    8479              :      to a different module (which has implications for merging), so that
    8480              :      importers can reconstruct this information on stream-in.  */
    8481      3197366 :   if (TREE_CODE (inner) == FUNCTION_DECL || TREE_CODE (inner) == TYPE_DECL)
    8482              :     {
    8483      2454016 :       tree* temploid_friend_slot = imported_temploid_friends->get (decl);
    8484      2454016 :       gcc_checking_assert (!temploid_friend_slot || *temploid_friend_slot);
    8485      2454016 :       tree_node (temploid_friend_slot ? *temploid_friend_slot : NULL_TREE);
    8486              :     }
    8487              : 
    8488      3197366 :   {
    8489      3197366 :     auto wmk = make_temp_override (dep_hash->writing_merge_key, true);
    8490      3197366 :     if (decl != inner)
    8491       916975 :       tpl_header (decl, &tpl_levels);
    8492      3197366 :     if (TREE_CODE (inner) == FUNCTION_DECL)
    8493      1273006 :       fn_parms_init (inner);
    8494              : 
    8495              :     /* Now write out the merging information, and then really
    8496              :        install the tag values.  */
    8497      3197366 :     key_mergeable (tag, mk, decl, inner, container, dep);
    8498              : 
    8499      3197366 :     if (streaming_p ())
    8500      3201247 :       dump (dumper::MERGE)
    8501         1089 :         && dump ("Wrote:%d's %s merge key %C:%N", tag,
    8502         1089 :                  merge_kind_name[mk], TREE_CODE (decl), decl);
    8503      3197366 :   }
    8504              : 
    8505      3197366 :   if (TREE_CODE (inner) == FUNCTION_DECL)
    8506      3197366 :     fn_parms_fini (inner);
    8507              : 
    8508      3197366 :   if (!is_key_order ())
    8509      2333378 :     tree_node_vals (decl);
    8510              : 
    8511      3197366 :   if (inner_tag)
    8512              :     {
    8513       916975 :       if (!is_key_order ())
    8514       611332 :         tree_node_vals (inner);
    8515       916975 :       tpl_parms_fini (decl, tpl_levels);
    8516              :     }
    8517              : 
    8518      3197366 :   if (type && !is_key_order ())
    8519              :     {
    8520       362440 :       tree_node_vals (type);
    8521       362440 :       if (stub_decl)
    8522         1479 :         tree_node_vals (stub_decl);
    8523              :     }
    8524              : 
    8525      3197366 :   if (!is_key_order ())
    8526              :     {
    8527      2333378 :       if (mk & MK_template_mask
    8528      1639776 :           || mk == MK_partial
    8529      1639776 :           || mk == MK_friend_spec)
    8530              :         {
    8531        29846 :           if (mk != MK_partial)
    8532              :             {
    8533              :               // FIXME: We should make use of the merge-key by
    8534              :               // exposing it outside of key_mergeable.  But this gets
    8535              :               // the job done.
    8536       693602 :               auto *entry = reinterpret_cast <spec_entry *> (dep->deps[0]);
    8537              : 
    8538       693602 :               if (streaming_p ())
    8539       346801 :                 u (get_mergeable_specialization_flags (mk & MK_tmpl_decl_mask,
    8540              :                                                        entry->tmpl, decl));
    8541       693602 :               tree_node (entry->tmpl);
    8542       693602 :               tree_node (entry->args);
    8543              :             }
    8544              :           else
    8545              :             {
    8546        29846 :               tree ti = get_template_info (inner);
    8547        29846 :               tree_node (TI_TEMPLATE (ti));
    8548        29846 :               tree_node (TI_ARGS (ti));
    8549              :             }
    8550              :         }
    8551      2333378 :       tree_node (get_constraints (decl));
    8552              :     }
    8553              : 
    8554      3197366 :   if (streaming_p ())
    8555              :     {
    8556              :       /* Do not stray outside this section.  */
    8557      1161114 :       gcc_checking_assert (!dep || dep->section == dep_hash->section);
    8558              : 
    8559              :       /* Write the entity index, so we can insert it as soon as we
    8560              :          know this is new.  */
    8561      1161114 :       install_entity (decl, dep);
    8562              :     }
    8563              : 
    8564      3197366 :   if (DECL_LANG_SPECIFIC (inner)
    8565      2907786 :       && DECL_MODULE_KEYED_DECLS_P (inner)
    8566      3198245 :       && streaming_p ())
    8567              :     {
    8568              :       /* Stream the keyed entities.  There may be keyed entities that we
    8569              :          choose not to stream, such as a lambda in a non-inline variable's
    8570              :          initializer, so don't build dependencies for them here; any deps
    8571              :          we need should be acquired during write_definition (possibly
    8572              :          indirectly).  */
    8573          286 :       auto *attach_vec = keyed_table->get (inner);
    8574          286 :       unsigned num = attach_vec->length ();
    8575          286 :       u (num);
    8576          604 :       for (unsigned ix = 0; ix != num; ix++)
    8577              :         {
    8578          318 :           tree attached = (*attach_vec)[ix];
    8579          318 :           if (attached)
    8580              :             {
    8581          318 :               tree ti = TYPE_TEMPLATE_INFO (TREE_TYPE (attached));
    8582          318 :               if (!dep_hash->find_dependency (attached)
    8583          318 :                   && !(ti && dep_hash->find_dependency (TI_TEMPLATE (ti))))
    8584              :                 attached = NULL_TREE;
    8585              :             }
    8586              : 
    8587          318 :           tree_node (attached);
    8588          360 :           dump (dumper::MERGE)
    8589           30 :             && dump ("Written %d[%u] attached decl %N", tag, ix, attached);
    8590              :         }
    8591              :     }
    8592              : 
    8593      3197366 :   bool is_typedef = false;
    8594      3197366 :   if (!type && TREE_CODE (inner) == TYPE_DECL)
    8595              :     {
    8596       637387 :       tree t = TREE_TYPE (inner);
    8597       637387 :       unsigned tdef_flags = 0;
    8598       637387 :       if (DECL_ORIGINAL_TYPE (inner)
    8599       637387 :           && TYPE_NAME (TREE_TYPE (inner)) == inner)
    8600              :         {
    8601       637387 :           tdef_flags |= 1;
    8602       637387 :           if (TYPE_STRUCTURAL_EQUALITY_P (t)
    8603       136624 :               && TYPE_DEPENDENT_P_VALID (t)
    8604       767879 :               && TYPE_DEPENDENT_P (t))
    8605              :             tdef_flags |= 2;
    8606              :         }
    8607       637387 :       if (streaming_p ())
    8608       217138 :         u (tdef_flags);
    8609              : 
    8610       637387 :       if (tdef_flags & 1)
    8611              :         {
    8612              :           /* A typedef type.  */
    8613       637387 :           int type_tag = insert (t);
    8614       637387 :           if (streaming_p ())
    8615       217138 :             dump (dumper::TREE)
    8616          206 :               && dump ("Cloned:%d %s %C:%N", type_tag,
    8617              :                        tdef_flags & 2 ? "depalias" : "typedef",
    8618          206 :                        TREE_CODE (t), t);
    8619              : 
    8620              :           is_typedef = true;
    8621              :         }
    8622              :     }
    8623              : 
    8624      3197366 :   if (streaming_p () && DECL_MAYBE_IN_CHARGE_CDTOR_P (decl))
    8625              :     {
    8626        90701 :       bool cloned_p
    8627        90701 :         = (DECL_CHAIN (decl) && DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl)));
    8628        63523 :       bool needs_vtt_parm_p
    8629        63523 :         = (cloned_p && CLASSTYPE_VBASECLASSES (DECL_CONTEXT (decl)));
    8630        63523 :       bool omit_inherited_parms_p
    8631        63523 :         = (cloned_p && DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl)
    8632        50394 :            && base_ctor_omit_inherited_parms (decl));
    8633        90701 :       unsigned flags = (int (cloned_p) << 0
    8634        90701 :                         | int (needs_vtt_parm_p) << 1
    8635        90701 :                         | int (omit_inherited_parms_p) << 2);
    8636        90701 :       u (flags);
    8637        90780 :       dump (dumper::TREE) && dump ("CDTOR %N is %scloned",
    8638              :                                    decl, cloned_p ? "" : "not ");
    8639              :     }
    8640              : 
    8641      3197366 :   if (streaming_p () && VAR_P (decl) && CP_DECL_THREAD_LOCAL_P (decl))
    8642          174 :     u (decl_tls_model (decl));
    8643              : 
    8644      3197366 :   if (streaming_p ())
    8645      1161114 :     dump (dumper::TREE) && dump ("Written decl:%d %C:%N", tag,
    8646          683 :                                  TREE_CODE (decl), decl);
    8647              : 
    8648      3197366 :   if (NAMESPACE_SCOPE_P (inner))
    8649      1911990 :     gcc_checking_assert (!dep == (VAR_OR_FUNCTION_DECL_P (inner)
    8650              :                                   && DECL_LOCAL_DECL_P (inner)));
    8651      2241245 :   else if ((TREE_CODE (inner) == TYPE_DECL
    8652       627204 :             && !is_typedef
    8653       120917 :             && TYPE_NAME (TREE_TYPE (inner)) == inner)
    8654      2747532 :            || TREE_CODE (inner) == FUNCTION_DECL)
    8655              :     {
    8656      1054443 :       bool write_defn = !dep && has_definition (decl);
    8657      1054443 :       if (streaming_p ())
    8658       351618 :         u (write_defn);
    8659      1054443 :       if (write_defn)
    8660            6 :         write_definition (decl);
    8661              :     }
    8662              : }
    8663              : 
    8664              : tree
    8665      1117422 : trees_in::decl_value ()
    8666              : {
    8667      1117422 :   int tag = 0;
    8668      1117422 :   bool is_attached = false;
    8669      1117422 :   bool has_defn = false;
    8670      1117422 :   unsigned mk_u = u ();
    8671      1117422 :   if (mk_u >= MK_hwm || !merge_kind_name[mk_u])
    8672              :     {
    8673            0 :       set_overrun ();
    8674            0 :       return NULL_TREE;
    8675              :     }
    8676              : 
    8677      1117422 :   unsigned saved_unused = unused;
    8678      1117422 :   unused = 0;
    8679              : 
    8680      1117422 :   merge_kind mk = merge_kind (mk_u);
    8681              : 
    8682      1117422 :   tree decl = start ();
    8683      1117422 :   if (decl)
    8684              :     {
    8685      1117422 :       if (mk != MK_unique)
    8686              :         {
    8687       925264 :           bits_in bits = stream_bits ();
    8688       925264 :           if (!(mk & MK_template_mask) && !state->is_header ())
    8689        46154 :             is_attached = bits.b ();
    8690              : 
    8691       925264 :           has_defn = bits.b ();
    8692       925264 :         }
    8693              : 
    8694      1117422 :       if (!tree_node_bools (decl))
    8695            0 :         decl = NULL_TREE;
    8696              :     }
    8697              : 
    8698              :   /* Insert into map.  */
    8699      1117422 :   tag = insert (decl);
    8700      1117422 :   if (decl)
    8701      1117422 :     dump (dumper::TREE)
    8702          964 :       && dump ("Reading:%d %C", tag, TREE_CODE (decl));
    8703              : 
    8704      1117422 :   tree inner = decl;
    8705      1117422 :   int inner_tag = 0;
    8706      1117422 :   if (decl && TREE_CODE (decl) == TEMPLATE_DECL)
    8707              :     {
    8708       299499 :       int code = u ();
    8709       299499 :       inner = start (code);
    8710       299499 :       if (inner && tree_node_bools (inner))
    8711       299499 :         DECL_TEMPLATE_RESULT (decl) = inner;
    8712              :       else
    8713            0 :         decl = NULL_TREE;
    8714              : 
    8715       299499 :       inner_tag = insert (inner);
    8716       299499 :       if (decl)
    8717       299499 :         dump (dumper::TREE)
    8718          204 :           && dump ("Reading:%d %C", inner_tag, TREE_CODE (inner));
    8719              :     }
    8720              : 
    8721      1117422 :   tree type = NULL_TREE;
    8722      1117422 :   int type_tag = 0;
    8723      1117422 :   tree stub_decl = NULL_TREE;
    8724      1117422 :   int stub_tag = 0;
    8725      1117422 :   if (decl && TREE_CODE (inner) == TYPE_DECL)
    8726              :     {
    8727       362373 :       if (unsigned type_code = u ())
    8728              :         {
    8729       163662 :           type = start (type_code);
    8730       163662 :           if (type && tree_node_bools (type))
    8731              :             {
    8732       163662 :               TREE_TYPE (inner) = type;
    8733       163662 :               TYPE_NAME (type) = inner;
    8734              :             }
    8735              :           else
    8736            0 :             decl = NULL_TREE;
    8737              : 
    8738       163662 :           type_tag = insert (type);
    8739       163662 :           if (decl)
    8740       163662 :             dump (dumper::TREE)
    8741          212 :               && dump ("Reading type:%d %C", type_tag, TREE_CODE (type));
    8742              : 
    8743       163662 :           if (unsigned stub_code = u ())
    8744              :             {
    8745          423 :               stub_decl = start (stub_code);
    8746          423 :               if (stub_decl && tree_node_bools (stub_decl))
    8747              :                 {
    8748          423 :                   TREE_TYPE (stub_decl) = type;
    8749          423 :                   TYPE_STUB_DECL (type) = stub_decl;
    8750              :                 }
    8751              :               else
    8752            0 :                 decl = NULL_TREE;
    8753              : 
    8754          423 :               stub_tag = insert (stub_decl);
    8755          423 :               if (decl)
    8756          423 :                 dump (dumper::TREE)
    8757            0 :                   && dump ("Reading stub_decl:%d %C", stub_tag,
    8758            0 :                            TREE_CODE (stub_decl));
    8759              :             }
    8760              :         }
    8761              :     }
    8762              : 
    8763      1117422 :   if (!decl)
    8764              :     {
    8765            0 :     bail:
    8766            0 :       if (inner_tag != 0)
    8767            0 :         back_refs[~inner_tag] = NULL_TREE;
    8768            0 :       if (type_tag != 0)
    8769            0 :         back_refs[~type_tag] = NULL_TREE;
    8770            0 :       if (stub_tag != 0)
    8771            0 :         back_refs[~stub_tag] = NULL_TREE;
    8772            0 :       if (tag != 0)
    8773            0 :         back_refs[~tag] = NULL_TREE;
    8774            0 :       set_overrun ();
    8775              :       /* Bail.  */
    8776            0 :       unused = saved_unused;
    8777            0 :       return NULL_TREE;
    8778              :     }
    8779              : 
    8780              :   /* Read the container, to ensure it's already been streamed in.  */
    8781      1117422 :   tree container = decl_container ();
    8782      1117422 :   unsigned tpl_levels = 0;
    8783              : 
    8784              :   /* If this is an imported temploid friend, get the owning decl its
    8785              :      attachment is determined by (or NULL_TREE otherwise).  */
    8786      1117422 :   tree temploid_friend = NULL_TREE;
    8787      1117422 :   if (TREE_CODE (inner) == FUNCTION_DECL || TREE_CODE (inner) == TYPE_DECL)
    8788       787785 :     temploid_friend = tree_node ();
    8789              : 
    8790              :   /* Figure out if this decl is already known about.  */
    8791      1117422 :   int parm_tag = 0;
    8792              : 
    8793      1117422 :   if (decl != inner)
    8794       299499 :     if (!tpl_header (decl, &tpl_levels))
    8795            0 :       goto bail;
    8796      1117422 :   if (TREE_CODE (inner) == FUNCTION_DECL)
    8797       425412 :     parm_tag = fn_parms_init (inner);
    8798              : 
    8799      1117422 :   tree existing = key_mergeable (tag, mk, decl, inner, type, container,
    8800              :                                  is_attached, temploid_friend);
    8801      1117422 :   tree existing_inner = existing;
    8802      1117422 :   if (existing)
    8803              :     {
    8804       434095 :       if (existing == error_mark_node)
    8805            0 :         goto bail;
    8806              : 
    8807       434095 :       if (TREE_CODE (STRIP_TEMPLATE (existing)) == TYPE_DECL)
    8808              :         {
    8809       161533 :           tree etype = TREE_TYPE (existing);
    8810       161533 :           if (TYPE_LANG_SPECIFIC (etype)
    8811       116587 :               && COMPLETE_TYPE_P (etype)
    8812       229473 :               && !CLASSTYPE_MEMBER_VEC (etype))
    8813              :             /* Give it a member vec, we're likely gonna be looking
    8814              :                inside it.  */
    8815        14121 :             set_class_bindings (etype, -1);
    8816              :         }
    8817              : 
    8818              :       /* Install the existing decl into the back ref array.  */
    8819       434095 :       register_duplicate (decl, existing);
    8820       434095 :       back_refs[~tag] = existing;
    8821       434095 :       if (inner_tag != 0)
    8822              :         {
    8823       145456 :           existing_inner = DECL_TEMPLATE_RESULT (existing);
    8824       145456 :           back_refs[~inner_tag] = existing_inner;
    8825              :         }
    8826              : 
    8827       434095 :       if (type_tag != 0)
    8828              :         {
    8829        77306 :           tree existing_type = TREE_TYPE (existing);
    8830        77306 :           back_refs[~type_tag] = existing_type;
    8831        77306 :           if (stub_tag != 0)
    8832          245 :             back_refs[~stub_tag] = TYPE_STUB_DECL (existing_type);
    8833              :         }
    8834              :     }
    8835              : 
    8836      1117422 :   if (parm_tag)
    8837       425412 :     fn_parms_fini (parm_tag, inner, existing_inner, has_defn);
    8838              : 
    8839      1117422 :   if (!tree_node_vals (decl))
    8840            0 :     goto bail;
    8841              : 
    8842      1117422 :   if (inner_tag)
    8843              :     {
    8844       299499 :       gcc_checking_assert (DECL_TEMPLATE_RESULT (decl) == inner);
    8845              : 
    8846       299499 :       if (!tree_node_vals (inner))
    8847            0 :         goto bail;
    8848              : 
    8849       299499 :       if (!tpl_parms_fini (decl, tpl_levels))
    8850            0 :         goto bail;
    8851              :     }
    8852              : 
    8853      1117422 :   if (type && (!tree_node_vals (type)
    8854       163662 :                || (stub_decl && !tree_node_vals (stub_decl))))
    8855            0 :     goto bail;
    8856              : 
    8857      1117422 :   spec_entry spec;
    8858      1117422 :   unsigned spec_flags = 0;
    8859      1117422 :   if (mk & MK_template_mask
    8860       771062 :       || mk == MK_partial
    8861       771062 :       || mk == MK_friend_spec)
    8862              :     {
    8863        10567 :       if (mk == MK_partial)
    8864              :         spec_flags = 2;
    8865              :       else
    8866       346360 :         spec_flags = u ();
    8867              : 
    8868       356927 :       spec.tmpl = tree_node ();
    8869       356927 :       spec.args = tree_node ();
    8870              :     }
    8871              :   /* Hold constraints on the spec field, for a short while.  */
    8872      1117422 :   spec.spec = tree_node ();
    8873              : 
    8874      1118386 :   dump (dumper::TREE) && dump ("Read:%d %C:%N", tag, TREE_CODE (decl), decl);
    8875              : 
    8876      1117422 :   existing = back_refs[~tag];
    8877      1117422 :   bool installed = install_entity (existing);
    8878      1117422 :   bool is_new = existing == decl;
    8879              : 
    8880      1117422 :   if (DECL_LANG_SPECIFIC (inner)
    8881      2106292 :       && DECL_MODULE_KEYED_DECLS_P (inner))
    8882              :     {
    8883              :       /* Read and maybe install the attached entities.  */
    8884          373 :       bool existed;
    8885          373 :       auto &set = keyed_table->get_or_insert (STRIP_TEMPLATE (existing),
    8886              :                                               &existed);
    8887          373 :       unsigned num = u ();
    8888          373 :       if (is_new == existed)
    8889            0 :         set_overrun ();
    8890          373 :       if (is_new)
    8891          240 :         set.reserve (num);
    8892          795 :       for (unsigned ix = 0; !get_overrun () && ix != num; ix++)
    8893              :         {
    8894          422 :           tree attached = tree_node ();
    8895          422 :           dump (dumper::MERGE)
    8896          105 :             && dump ("Read %d[%u] %s attached decl %N", tag, ix,
    8897              :                      is_new ? "new" : "matched", attached);
    8898          422 :           if (is_new)
    8899          270 :             set.quick_push (attached);
    8900          152 :           else if (set[ix] != attached)
    8901              :             {
    8902            3 :               if (!set[ix] || !attached)
    8903              :                 /* One import left a hole for a lambda dep we chose not
    8904              :                    to stream, but another import chose to stream that lambda.
    8905              :                    Let's not error here: hopefully we'll complain later in
    8906              :                    is_matching_decl about whatever caused us to make a
    8907              :                    different decision.  */
    8908              :                 ;
    8909              :               else
    8910            0 :                 set_overrun ();
    8911              :             }
    8912              :         }
    8913              :     }
    8914              : 
    8915              :   /* Regular typedefs will have a NULL TREE_TYPE at this point.  */
    8916      1117422 :   unsigned tdef_flags = 0;
    8917      1117422 :   bool is_typedef = false;
    8918      1117422 :   if (!type && TREE_CODE (inner) == TYPE_DECL)
    8919              :     {
    8920       198711 :       tdef_flags = u ();
    8921       198711 :       if (tdef_flags & 1)
    8922       198711 :         is_typedef = true;
    8923              :     }
    8924              : 
    8925      1117422 :   if (is_new)
    8926              :     {
    8927              :       /* A newly discovered node.  */
    8928       683327 :       if (TREE_CODE (decl) == FUNCTION_DECL && DECL_VIRTUAL_P (decl))
    8929              :         /* Mark this identifier as naming a virtual function --
    8930              :            lookup_overrides relies on this optimization.  */
    8931         5718 :         IDENTIFIER_VIRTUAL_P (DECL_NAME (decl)) = true;
    8932              : 
    8933       683327 :       if (installed)
    8934              :         {
    8935              :           /* Mark the entity as imported.  */
    8936       431726 :           retrofit_lang_decl (inner);
    8937       431726 :           DECL_MODULE_IMPORT_P (inner) = true;
    8938              :         }
    8939              : 
    8940       683327 :       if (temploid_friend)
    8941           40 :         imported_temploid_friends->put (decl, temploid_friend);
    8942              : 
    8943       683327 :       if (spec.spec)
    8944        29501 :         set_constraints (decl, spec.spec);
    8945              : 
    8946       683327 :       if (TREE_CODE (decl) == INTEGER_CST && !TREE_OVERFLOW (decl))
    8947              :         {
    8948            0 :           decl = cache_integer_cst (decl, true);
    8949            0 :           back_refs[~tag] = decl;
    8950              :         }
    8951              : 
    8952       683327 :       if (is_typedef)
    8953              :         {
    8954              :           /* Frob it to be ready for cloning.  */
    8955       114484 :           TREE_TYPE (inner) = DECL_ORIGINAL_TYPE (inner);
    8956       114484 :           DECL_ORIGINAL_TYPE (inner) = NULL_TREE;
    8957       114484 :           if (TREE_CODE (TREE_TYPE (inner)) != TU_LOCAL_ENTITY)
    8958              :             {
    8959       114481 :               set_underlying_type (inner);
    8960       114481 :               if (tdef_flags & 2)
    8961              :                 {
    8962              :                   /* Match instantiate_alias_template's handling.  */
    8963        28687 :                   tree type = TREE_TYPE (inner);
    8964        28687 :                   TYPE_DEPENDENT_P (type) = true;
    8965        28687 :                   TYPE_DEPENDENT_P_VALID (type) = true;
    8966        28687 :                   SET_TYPE_STRUCTURAL_EQUALITY (type);
    8967              :                 }
    8968              :             }
    8969              :         }
    8970              : 
    8971       683327 :       if (inner_tag)
    8972              :         /* Set the TEMPLATE_DECL's type.  */
    8973       154043 :         TREE_TYPE (decl) = TREE_TYPE (inner);
    8974              : 
    8975              :       /* Redetermine whether we need to import or export this declaration
    8976              :          for this TU.  But for extern templates we know we must import:
    8977              :          they'll be defined in a different TU.
    8978              :          FIXME: How do dllexport and dllimport interact across a module?
    8979              :          See also https://github.com/itanium-cxx-abi/cxx-abi/issues/170.
    8980              :          May have to revisit?  */
    8981       683327 :       if (type
    8982        86356 :           && CLASS_TYPE_P (type)
    8983        73900 :           && TYPE_LANG_SPECIFIC (type)
    8984       757227 :           && !(CLASSTYPE_EXPLICIT_INSTANTIATION (type)
    8985          681 :                && CLASSTYPE_INTERFACE_KNOWN (type)
    8986          681 :                && CLASSTYPE_INTERFACE_ONLY (type)))
    8987              :         {
    8988        73271 :           CLASSTYPE_INTERFACE_ONLY (type) = false;
    8989        73271 :           CLASSTYPE_INTERFACE_UNKNOWN (type) = true;
    8990              :         }
    8991              : 
    8992              :       /* Add to specialization tables now that constraints etc are
    8993              :          added.  */
    8994       683327 :       if (mk == MK_partial)
    8995              :         {
    8996         4433 :           bool is_type = TREE_CODE (inner) == TYPE_DECL;
    8997         4433 :           spec.spec = is_type ? type : inner;
    8998         4433 :           add_mergeable_specialization (!is_type, &spec, decl, spec_flags);
    8999              :         }
    9000       678894 :       else if (mk & MK_template_mask)
    9001              :         {
    9002       196526 :           bool is_type = !(mk & MK_tmpl_decl_mask);
    9003       196526 :           spec.spec = is_type ? type : mk & MK_tmpl_tmpl_mask ? inner : decl;
    9004       196526 :           add_mergeable_specialization (!is_type, &spec, decl, spec_flags);
    9005              :         }
    9006              : 
    9007       683327 :       if (NAMESPACE_SCOPE_P (decl)
    9008       143014 :           && (mk == MK_named || mk == MK_unique
    9009       143014 :               || mk == MK_enum || mk == MK_friend_spec)
    9010       747521 :           && !(VAR_OR_FUNCTION_DECL_P (decl) && DECL_LOCAL_DECL_P (decl)))
    9011        64099 :         add_module_namespace_decl (CP_DECL_CONTEXT (decl), decl);
    9012              : 
    9013       683327 :       if (DECL_ARTIFICIAL (decl)
    9014       188182 :           && TREE_CODE (decl) == FUNCTION_DECL
    9015        19647 :           && !DECL_TEMPLATE_INFO (decl)
    9016        19337 :           && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
    9017        19177 :           && TYPE_SIZE (DECL_CONTEXT (decl))
    9018       684779 :           && !DECL_THUNK_P (decl))
    9019              :         /* A new implicit member function, when the class is
    9020              :            complete.  This means the importee declared it, and
    9021              :            we must now add it to the class.  Note that implicit
    9022              :            member fns of template instantiations do not themselves
    9023              :            look like templates.  */
    9024          910 :         if (!install_implicit_member (inner))
    9025            0 :           set_overrun ();
    9026              : 
    9027              :       /* When importing a TLS wrapper from a header unit, we haven't
    9028              :          actually emitted its definition yet. Remember it so we can
    9029              :          do this later.  */
    9030       683327 :       if (state->is_header ()
    9031       683327 :           && decl_tls_wrapper_p (decl))
    9032            6 :         note_vague_linkage_fn (decl);
    9033              : 
    9034              :       /* Apply relevant attributes.
    9035              :          FIXME should probably use cplus_decl_attributes for this,
    9036              :          but it's not yet ready for modules.  */
    9037              : 
    9038       683327 :       if (VAR_OR_FUNCTION_DECL_P (inner))
    9039       410400 :         if (tree attr = lookup_attribute ("section", DECL_ATTRIBUTES (inner)))
    9040              :           {
    9041            6 :             tree section_name = TREE_VALUE (TREE_VALUE (attr));
    9042            6 :             set_decl_section_name (inner, TREE_STRING_POINTER (section_name));
    9043              :           }
    9044              : 
    9045              :       /* Setup aliases for the declaration.  */
    9046       683327 :       if (tree alias = lookup_attribute ("alias", DECL_ATTRIBUTES (decl)))
    9047              :         {
    9048            3 :           alias = TREE_VALUE (TREE_VALUE (alias));
    9049            3 :           alias = get_identifier (TREE_STRING_POINTER (alias));
    9050            3 :           assemble_alias (decl, alias);
    9051              :         }
    9052              :     }
    9053              :   else
    9054              :     {
    9055              :       /* DECL is the to-be-discarded decl.  Its internal pointers will
    9056              :          be to the EXISTING's structure.  Frob it to point to its
    9057              :          own other structures, so loading its definition will alter
    9058              :          it, and not the existing decl.  */
    9059       435833 :       dump (dumper::MERGE) && dump ("Deduping %N", existing);
    9060              : 
    9061       434095 :       if (inner_tag)
    9062       145456 :         DECL_TEMPLATE_RESULT (decl) = inner;
    9063              : 
    9064       434095 :       if (type)
    9065              :         {
    9066              :           /* Point at the to-be-discarded type & decl.  */
    9067        77306 :           TYPE_NAME (type) = inner;
    9068        77306 :           TREE_TYPE (inner) = type;
    9069              : 
    9070       154367 :           TYPE_STUB_DECL (type) = stub_decl ? stub_decl : inner;
    9071        77306 :           if (stub_decl)
    9072          245 :             TREE_TYPE (stub_decl) = type;
    9073              : 
    9074        77306 :           tree etype = TREE_TYPE (existing);
    9075              : 
    9076              :           /* Handle separate declarations with different attributes.  */
    9077        77306 :           tree &dattr = TYPE_ATTRIBUTES (type);
    9078        77306 :           tree &eattr = TYPE_ATTRIBUTES (etype);
    9079        77306 :           check_abi_tags (existing, decl, eattr, dattr);
    9080              :           // TODO: handle other conflicting type attributes
    9081        77306 :           eattr = merge_attributes (eattr, dattr);
    9082              : 
    9083              :           /* When merging a partial specialisation, the existing decl may have
    9084              :              had its TYPE_CANONICAL adjusted.  If so we should use structural
    9085              :              equality to ensure is_matching_decl doesn't get confused.  */
    9086        77306 :           if ((spec_flags & 2)
    9087        77306 :               && TYPE_CANONICAL (type) != TYPE_CANONICAL (etype))
    9088            3 :             SET_TYPE_STRUCTURAL_EQUALITY (type);
    9089              :         }
    9090              : 
    9091       434095 :       if (inner_tag)
    9092              :         /* Set the TEMPLATE_DECL's type.  */
    9093       145456 :         TREE_TYPE (decl) = TREE_TYPE (inner);
    9094              : 
    9095       434095 :       if (!is_matching_decl (existing, decl, is_typedef))
    9096           42 :         unmatched_duplicate (existing);
    9097              : 
    9098       434095 :       if (TREE_CODE (inner) == FUNCTION_DECL)
    9099              :         {
    9100       197964 :           tree e_inner = STRIP_TEMPLATE (existing);
    9101       197964 :           for (auto parm = DECL_ARGUMENTS (inner);
    9102       594148 :                parm; parm = DECL_CHAIN (parm))
    9103       396184 :             DECL_CONTEXT (parm) = e_inner;
    9104              :         }
    9105              : 
    9106              :       /* And our result is the existing node.  */
    9107       434095 :       decl = existing;
    9108              :     }
    9109              : 
    9110      1117422 :   if (mk == MK_friend_spec)
    9111              :     {
    9112            0 :       tree e = match_mergeable_specialization (true, &spec);
    9113            0 :       if (!e)
    9114              :         {
    9115            0 :           spec.spec = inner;
    9116            0 :           add_mergeable_specialization (true, &spec, decl, spec_flags);
    9117              :         }
    9118            0 :       else if (e != existing)
    9119            0 :         set_overrun ();
    9120              :     }
    9121              : 
    9122      1117422 :   if (is_typedef)
    9123              :     {
    9124              :       /* Insert the type into the array now.  */
    9125       198711 :       tag = insert (TREE_TYPE (decl));
    9126       198711 :       dump (dumper::TREE)
    9127          247 :         && dump ("Cloned:%d typedef %C:%N",
    9128          247 :                  tag, TREE_CODE (TREE_TYPE (decl)), TREE_TYPE (decl));
    9129              :     }
    9130              : 
    9131      1117422 :   unused = saved_unused;
    9132              : 
    9133      1117422 :   if (DECL_MAYBE_IN_CHARGE_CDTOR_P (decl))
    9134              :     {
    9135        94943 :       unsigned flags = u ();
    9136              : 
    9137        94943 :       if (is_new)
    9138              :         {
    9139        54668 :           bool cloned_p = flags & 1;
    9140        54768 :           dump (dumper::TREE) && dump ("CDTOR %N is %scloned",
    9141              :                                        decl, cloned_p ? "" : "not ");
    9142        54668 :           if (cloned_p)
    9143              :             {
    9144              :               /* Update the member vec, if there is one (we're in a different
    9145              :                  cluster to the class defn) and this isn't a primary template
    9146              :                  specialization (as in tsubst_function_decl).  */
    9147        39252 :               bool up = (CLASSTYPE_MEMBER_VEC (DECL_CONTEXT (decl))
    9148        39252 :                          && !primary_template_specialization_p (decl));
    9149        39252 :               build_cdtor_clones (decl, flags & 2, flags & 4, up);
    9150              :             }
    9151              :         }
    9152              :     }
    9153              : 
    9154      1117422 :   if (VAR_P (decl) && CP_DECL_THREAD_LOCAL_P (decl))
    9155              :     {
    9156          168 :       enum tls_model model = tls_model (u ());
    9157          168 :       if (is_new)
    9158          148 :         set_decl_tls_model (decl, model);
    9159              :     }
    9160              : 
    9161      1117422 :   if (!NAMESPACE_SCOPE_P (inner)
    9162       829930 :       && ((TREE_CODE (inner) == TYPE_DECL
    9163       194998 :            && !is_typedef
    9164        35174 :            && TYPE_NAME (TREE_TYPE (inner)) == inner)
    9165       794756 :           || TREE_CODE (inner) == FUNCTION_DECL)
    9166      1475049 :       && u ())
    9167            3 :     read_definition (decl);
    9168              : 
    9169              :   return decl;
    9170              : }
    9171              : 
    9172              : /* DECL is an unnameable member of CTX.  Return a suitable identifying
    9173              :    index.  */
    9174              : 
    9175              : static unsigned
    9176          967 : get_field_ident (tree ctx, tree decl)
    9177              : {
    9178          967 :   gcc_checking_assert (TREE_CODE (decl) == USING_DECL
    9179              :                        || !DECL_NAME (decl)
    9180              :                        || IDENTIFIER_ANON_P (DECL_NAME (decl)));
    9181              : 
    9182          967 :   unsigned ix = 0;
    9183          967 :   for (tree fields = TYPE_FIELDS (ctx);
    9184         7406 :        fields; fields = DECL_CHAIN (fields))
    9185              :     {
    9186         7406 :       if (fields == decl)
    9187          967 :         return ix;
    9188              : 
    9189         6439 :       if (DECL_CONTEXT (fields) == ctx
    9190         6439 :           && (TREE_CODE (fields) == USING_DECL
    9191         6428 :               || (TREE_CODE (fields) == FIELD_DECL
    9192           35 :                   && (!DECL_NAME (fields)
    9193           13 :                       || IDENTIFIER_ANON_P (DECL_NAME (fields))))))
    9194              :         /* Count this field.  */
    9195           25 :         ix++;
    9196              :     }
    9197            0 :   gcc_unreachable ();
    9198              : }
    9199              : 
    9200              : static tree
    9201          958 : lookup_field_ident (tree ctx, unsigned ix)
    9202              : {
    9203          958 :   for (tree fields = TYPE_FIELDS (ctx);
    9204         7252 :        fields; fields = DECL_CHAIN (fields))
    9205         7252 :     if (DECL_CONTEXT (fields) == ctx
    9206         7252 :         && (TREE_CODE (fields) == USING_DECL
    9207         7240 :             || (TREE_CODE (fields) == FIELD_DECL
    9208         1005 :                 && (!DECL_NAME (fields)
    9209           15 :                     || IDENTIFIER_ANON_P (DECL_NAME (fields))))))
    9210          996 :       if (!ix--)
    9211              :         return fields;
    9212              : 
    9213              :   return NULL_TREE;
    9214              : }
    9215              : 
    9216              : /* Reference DECL.  REF indicates the walk kind we are performing.
    9217              :    Return true if we should write this decl by value.  */
    9218              : 
    9219              : bool
    9220     10717384 : trees_out::decl_node (tree decl, walk_kind ref)
    9221              : {
    9222     10717384 :   gcc_checking_assert (DECL_P (decl) && !DECL_TEMPLATE_PARM_P (decl)
    9223              :                        && DECL_CONTEXT (decl));
    9224              : 
    9225     10717384 :   if (ref == WK_value)
    9226              :     {
    9227      1103031 :       depset *dep = dep_hash->find_dependency (decl);
    9228      1103031 :       decl_value (decl, dep);
    9229      1103031 :       return false;
    9230              :     }
    9231              : 
    9232      9614353 :   switch (TREE_CODE (decl))
    9233              :     {
    9234              :     default:
    9235              :       break;
    9236              : 
    9237      1010741 :     case FUNCTION_DECL:
    9238      1010741 :       gcc_checking_assert (!DECL_LOCAL_DECL_P (decl));
    9239              :       break;
    9240              : 
    9241              :     case RESULT_DECL:
    9242              :       /* Unlike PARM_DECLs, RESULT_DECLs are only generated and
    9243              :          referenced when we're inside the function itself.  */
    9244              :       return true;
    9245              : 
    9246       154745 :     case PARM_DECL:
    9247       154745 :       {
    9248       154745 :         if (streaming_p ())
    9249        68799 :           i (tt_parm);
    9250       154745 :         tree_node (DECL_CONTEXT (decl));
    9251              : 
    9252              :         /* That must have put this in the map.  */
    9253       154745 :         walk_kind ref = ref_node (decl);
    9254       154745 :         if (ref != WK_none)
    9255              :           // FIXME:OPTIMIZATION We can wander into bits of the
    9256              :           // template this was instantiated from, for instance
    9257              :           // deferred noexcept and default parms, or references
    9258              :           // to parms from earlier forward-decls (PR c++/119608).
    9259              :           //
    9260              :           // Currently we'll end up cloning those bits of tree.
    9261              :           // It would be nice to reference those specific nodes.
    9262              :           // I think putting those things in the map when we
    9263              :           // reference their template by name.
    9264              :           //
    9265              :           // See the note in add_indirects.
    9266              :           return true;
    9267              : 
    9268            0 :         if (streaming_p ())
    9269            0 :           dump (dumper::TREE)
    9270            0 :             && dump ("Wrote %s reference %N",
    9271            0 :                      TREE_CODE (decl) == PARM_DECL ? "parameter" : "result",
    9272              :                      decl);
    9273              :       }
    9274              :       return false;
    9275              : 
    9276              :     case IMPORTED_DECL:
    9277              :       /* This describes a USING_DECL to the ME's debug machinery.  It
    9278              :          originates from the fortran FE, and has nothing to do with
    9279              :          C++ modules.  */
    9280              :       return true;
    9281              : 
    9282              :     case LABEL_DECL:
    9283              :       return true;
    9284              : 
    9285        55637 :     case CONST_DECL:
    9286        55637 :       {
    9287              :         /* If I end up cloning enum decls, implementing C++20 using
    9288              :            E::v, this will need tweaking.   */
    9289        55637 :         if (streaming_p ())
    9290        12954 :           i (tt_enum_decl);
    9291        55637 :         tree ctx = DECL_CONTEXT (decl);
    9292        55637 :         gcc_checking_assert (TREE_CODE (ctx) == ENUMERAL_TYPE);
    9293        55637 :         tree_node (ctx);
    9294        55637 :         tree_node (DECL_NAME (decl));
    9295              : 
    9296        55637 :         int tag = insert (decl);
    9297        55637 :         if (streaming_p ())
    9298        12954 :           dump (dumper::TREE)
    9299           21 :             && dump ("Wrote enum decl:%d %C:%N", tag, TREE_CODE (decl), decl);
    9300              :         return false;
    9301              :       }
    9302        17662 :       break;
    9303              : 
    9304        17662 :     case USING_DECL:
    9305        17662 :       if (TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
    9306              :         break;
    9307              :       /* FALLTHROUGH  */
    9308              : 
    9309       128600 :     case FIELD_DECL:
    9310       128600 :       {
    9311       128600 :         if (streaming_p ())
    9312         7257 :           i (tt_data_member);
    9313              : 
    9314       128600 :         tree ctx = DECL_CONTEXT (decl);
    9315       128600 :         tree_node (ctx);
    9316              : 
    9317       128600 :         tree name = NULL_TREE;
    9318              : 
    9319       128600 :         if (TREE_CODE (decl) == USING_DECL)
    9320              :           ;
    9321              :         else
    9322              :           {
    9323       127646 :             name = DECL_NAME (decl);
    9324       247404 :             if (name && IDENTIFIER_ANON_P (name))
    9325              :               name = NULL_TREE;
    9326              :           }
    9327              : 
    9328       128600 :         tree_node (name);
    9329       128600 :         if (!name && streaming_p ())
    9330              :           {
    9331          967 :             unsigned ix = get_field_ident (ctx, decl);
    9332          967 :             u (ix);
    9333              :           }
    9334              : 
    9335       128600 :         int tag = insert (decl);
    9336       128600 :         if (streaming_p ())
    9337         7257 :           dump (dumper::TREE)
    9338           26 :             && dump ("Wrote member:%d %C:%N", tag, TREE_CODE (decl), decl);
    9339              :         return false;
    9340              :       }
    9341       421158 :       break;
    9342              : 
    9343       421158 :     case VAR_DECL:
    9344       421158 :       gcc_checking_assert (!DECL_LOCAL_DECL_P (decl));
    9345       421158 :       if (DECL_VTABLE_OR_VTT_P (decl))
    9346              :         {
    9347              :           /* VTT or VTABLE, they are all on the vtables list.  */
    9348         3192 :           tree ctx = CP_DECL_CONTEXT (decl);
    9349         3192 :           tree vtable = CLASSTYPE_VTABLES (ctx);
    9350         3225 :           for (unsigned ix = 0; ; vtable = DECL_CHAIN (vtable), ix++)
    9351         3225 :             if (vtable == decl)
    9352              :               {
    9353         3192 :                 gcc_checking_assert (DECL_VIRTUAL_P (decl));
    9354         3192 :                 if (streaming_p ())
    9355              :                   {
    9356           41 :                     u (tt_vtable);
    9357           41 :                     u (ix);
    9358           41 :                     dump (dumper::TREE)
    9359            0 :                       && dump ("Writing vtable %N[%u]", ctx, ix);
    9360              :                   }
    9361         3192 :                 tree_node (ctx);
    9362         3192 :                 return false;
    9363              :               }
    9364              :           gcc_unreachable ();
    9365              :         }
    9366              : 
    9367       417966 :       if (DECL_TINFO_P (decl))
    9368              :         {
    9369         6149 :         tinfo:
    9370              :           /* A typeinfo, tt_tinfo_typedef or tt_tinfo_var.  */
    9371        11326 :           bool is_var = VAR_P (decl);
    9372        11326 :           tree type = TREE_TYPE (decl);
    9373        11326 :           unsigned ix = get_pseudo_tinfo_index (type);
    9374        11326 :           if (streaming_p ())
    9375              :             {
    9376         7131 :               i (is_var ? tt_tinfo_var : tt_tinfo_typedef);
    9377         5054 :               u (ix);
    9378              :             }
    9379              : 
    9380        11326 :           if (is_var)
    9381              :             {
    9382              :               /* We also need the type it is for and mangled name, so
    9383              :                  the reader doesn't need to complete the type (which
    9384              :                  would break section ordering).  The type it is for is
    9385              :                  stashed on the name's TREE_TYPE.  */
    9386         6149 :               tree name = DECL_NAME (decl);
    9387         6149 :               tree_node (name);
    9388         6149 :               type = TREE_TYPE (name);
    9389         6149 :               tree_node (type);
    9390              :             }
    9391              : 
    9392        11326 :           int tag = insert (decl);
    9393        11326 :           if (streaming_p ())
    9394         5054 :             dump (dumper::TREE)
    9395           27 :               && dump ("Wrote tinfo_%s:%d %u %N", is_var ? "var" : "type",
    9396              :                        tag, ix, type);
    9397              : 
    9398        11326 :           if (!is_var)
    9399              :             {
    9400         5177 :               tag = insert (type);
    9401         5177 :               if (streaming_p ())
    9402         2077 :                 dump (dumper::TREE)
    9403            9 :                   && dump ("Wrote tinfo_type:%d %u %N", tag, ix, type);
    9404              :             }
    9405        11326 :           return false;
    9406              :         }
    9407              : 
    9408       411817 :       if (DECL_NTTP_OBJECT_P (decl))
    9409              :         {
    9410              :           /* A NTTP parm object.  */
    9411           33 :           if (streaming_p ())
    9412            7 :             i (tt_nttp_var);
    9413           33 :           tree_node (tparm_object_argument (decl));
    9414           33 :           tree_node (DECL_NAME (decl));
    9415           33 :           int tag = insert (decl);
    9416           33 :           if (streaming_p ())
    9417            7 :             dump (dumper::TREE)
    9418            0 :               && dump ("Wrote nttp object:%d %N", tag, DECL_NAME (decl));
    9419           33 :           return false;
    9420              :         }
    9421              : 
    9422              :       break;
    9423              : 
    9424      3363488 :     case TYPE_DECL:
    9425      3363488 :       if (DECL_TINFO_P (decl))
    9426         5177 :         goto tinfo;
    9427              :       break;
    9428              :     }
    9429              : 
    9430      8740289 :   if (DECL_THUNK_P (decl))
    9431              :     {
    9432              :       /* Thunks are similar to binfos -- write the thunked-to decl and
    9433              :          then thunk-specific key info.  */
    9434            0 :       if (streaming_p ())
    9435              :         {
    9436            0 :           i (tt_thunk);
    9437            0 :           i (THUNK_FIXED_OFFSET (decl));
    9438              :         }
    9439              : 
    9440              :       tree target = decl;
    9441            0 :       while (DECL_THUNK_P (target))
    9442            0 :         target = THUNK_TARGET (target);
    9443            0 :       tree_node (target);
    9444            0 :       tree_node (THUNK_VIRTUAL_OFFSET (decl));
    9445            0 :       int tag = insert (decl);
    9446            0 :       if (streaming_p ())
    9447            0 :         dump (dumper::TREE)
    9448            0 :           && dump ("Wrote:%d thunk %N to %N", tag, DECL_NAME (decl), target);
    9449            0 :       return false;
    9450              :     }
    9451              : 
    9452      8740289 :   if (DECL_CLONED_FUNCTION_P (decl))
    9453              :     {
    9454       293319 :       tree target = get_clone_target (decl);
    9455       293319 :       if (streaming_p ())
    9456       138704 :         i (tt_clone_ref);
    9457              : 
    9458       293319 :       tree_node (target);
    9459       293319 :       tree_node (DECL_NAME (decl));
    9460       293319 :       if (DECL_VIRTUAL_P (decl))
    9461        21591 :         tree_node (DECL_VINDEX (decl));
    9462       293319 :       int tag = insert (decl);
    9463       293319 :       if (streaming_p ())
    9464       138704 :         dump (dumper::TREE)
    9465          164 :           && dump ("Wrote:%d clone %N of %N", tag, DECL_NAME (decl), target);
    9466       293319 :       return false;
    9467              :     }
    9468              : 
    9469              :   /* Everything left should be a thing that is in the entity table.
    9470              :      Mostly things that can be defined outside of their (original
    9471              :      declaration) context.  */
    9472      8446970 :   gcc_checking_assert (TREE_CODE (decl) == TEMPLATE_DECL
    9473              :                        || VAR_P (decl)
    9474              :                        || TREE_CODE (decl) == FUNCTION_DECL
    9475              :                        || TREE_CODE (decl) == TYPE_DECL
    9476              :                        || TREE_CODE (decl) == USING_DECL
    9477              :                        || TREE_CODE (decl) == CONCEPT_DECL
    9478              :                        || TREE_CODE (decl) == NAMESPACE_DECL);
    9479              : 
    9480      8446970 :   int use_tpl = -1;
    9481      8446970 :   tree ti = node_template_info (decl, use_tpl);
    9482      8446970 :   tree tpl = NULL_TREE;
    9483              : 
    9484              :   /* If this is the TEMPLATE_DECL_RESULT of a TEMPLATE_DECL, get the
    9485              :      TEMPLATE_DECL.  Note TI_TEMPLATE is not a TEMPLATE_DECL for
    9486              :      (some) friends, so we need to check that.  */
    9487              :   // FIXME: Should local friend template specializations be by value?
    9488              :   // They don't get idents so we'll never know they're imported, but I
    9489              :   // think we can only reach them from the TU that defines the
    9490              :   // befriending class?
    9491      3131894 :   if (ti && TREE_CODE (TI_TEMPLATE (ti)) == TEMPLATE_DECL
    9492     11578813 :       && DECL_TEMPLATE_RESULT (TI_TEMPLATE (ti)) == decl)
    9493              :     {
    9494              :       tpl = TI_TEMPLATE (ti);
    9495       796876 :     partial_template:
    9496       796876 :       if (streaming_p ())
    9497              :         {
    9498         2531 :           i (tt_template);
    9499         2531 :           dump (dumper::TREE)
    9500            9 :             && dump ("Writing implicit template %C:%N%S",
    9501            9 :                      TREE_CODE (tpl), tpl, tpl);
    9502              :         }
    9503       796876 :       tree_node (tpl);
    9504              : 
    9505              :       /* Streaming TPL caused us to visit DECL and maybe its type,
    9506              :          if it wasn't TU-local.  */
    9507       796876 :       if (CHECKING_P && !has_tu_local_dep (tpl))
    9508              :         {
    9509       796849 :           gcc_checking_assert (TREE_VISITED (decl));
    9510       796849 :           if (DECL_IMPLICIT_TYPEDEF_P (decl))
    9511       412313 :             gcc_checking_assert (TREE_VISITED (TREE_TYPE (decl)));
    9512              :         }
    9513              :       return false;
    9514              :     }
    9515              : 
    9516      7737951 :   tree ctx = CP_DECL_CONTEXT (decl);
    9517      7737951 :   depset *dep = NULL;
    9518      7737951 :   if (streaming_p ())
    9519      1328613 :     dep = dep_hash->find_dependency (decl);
    9520      6409338 :   else if (TREE_CODE (ctx) != FUNCTION_DECL
    9521       238987 :            || TREE_CODE (decl) == TEMPLATE_DECL
    9522       216978 :            || DECL_IMPLICIT_TYPEDEF_P (decl)
    9523      6599717 :            || (DECL_LANG_SPECIFIC (decl)
    9524       138133 :                && DECL_MODULE_IMPORT_P (decl)))
    9525              :     {
    9526      6218959 :       auto kind = (TREE_CODE (decl) == NAMESPACE_DECL
    9527       520857 :                    && !DECL_NAMESPACE_ALIAS (decl)
    9528      6218959 :                    ? depset::EK_NAMESPACE : depset::EK_DECL);
    9529      6218959 :       dep = dep_hash->add_dependency (decl, kind);
    9530              :     }
    9531              : 
    9532      7547572 :   if (!dep || dep->is_tu_local ())
    9533              :     {
    9534              :       /* Some internal entity of context.  Do by value.  */
    9535       368956 :       decl_value (decl, dep);
    9536       368956 :       return false;
    9537              :     }
    9538              : 
    9539      7368995 :   if (dep->get_entity_kind () == depset::EK_REDIRECT)
    9540              :     {
    9541              :       /* The DECL_TEMPLATE_RESULT of a partial specialization.
    9542              :          Write the partial specialization's template.  */
    9543        87857 :       depset *redirect = dep->deps[0];
    9544        87857 :       gcc_checking_assert (redirect->get_entity_kind () == depset::EK_PARTIAL);
    9545        87857 :       tpl = redirect->get_entity ();
    9546        87857 :       goto partial_template;
    9547              :     }
    9548              : 
    9549      7281138 :   if (streaming_p ())
    9550              :     {
    9551              :       /* Locate the entity.  */
    9552      1150296 :       unsigned index = dep->cluster;
    9553      1150296 :       unsigned import = 0;
    9554              : 
    9555      1150296 :       if (dep->is_import ())
    9556         8396 :         import = dep->section;
    9557      1141900 :       else if (CHECKING_P)
    9558              :         /* It should be what we put there.  */
    9559      1141900 :         gcc_checking_assert (index == ~import_entity_index (decl));
    9560              : 
    9561              : #if CHECKING_P
    9562         8396 :       gcc_assert (!import || importedness >= 0);
    9563              : #endif
    9564      1150296 :       i (tt_entity);
    9565      1150296 :       u (import);
    9566      1150296 :       u (index);
    9567              :     }
    9568              : 
    9569      7281138 :   int tag = insert (decl);
    9570      7281138 :   if (streaming_p () && dump (dumper::TREE))
    9571              :     {
    9572          529 :       char const *kind = "import";
    9573          529 :       module_state *from = this_module ();
    9574          529 :       if (dep->is_import ())
    9575              :         /* Rediscover the unremapped index.  */
    9576           78 :         from = import_entity_module (import_entity_index (decl));
    9577              :       else
    9578              :         {
    9579          451 :           tree o = get_originating_module_decl (decl);
    9580          451 :           o = STRIP_TEMPLATE (o);
    9581          902 :           kind = (DECL_LANG_SPECIFIC (o) && DECL_MODULE_PURVIEW_P (o)
    9582          451 :                   ? "purview" : "GMF");
    9583              :         }
    9584          529 :       dump ("Wrote %s:%d %C:%N@%M", kind,
    9585          529 :             tag, TREE_CODE (decl), decl, from);
    9586              :     }
    9587              : 
    9588      7281138 :   add_indirects (decl);
    9589              : 
    9590      7281138 :   return false;
    9591              : }
    9592              : 
    9593              : void
    9594      8784636 : trees_out::type_node (tree type)
    9595              : {
    9596      8784636 :   gcc_assert (TYPE_P (type));
    9597              : 
    9598      8784636 :   tree root = (TYPE_NAME (type)
    9599      8784636 :                ? TREE_TYPE (TYPE_NAME (type)) : TYPE_MAIN_VARIANT (type));
    9600      8784636 :   gcc_checking_assert (root);
    9601              : 
    9602      8784636 :   if (type != root)
    9603              :     {
    9604      1989138 :       if (streaming_p ())
    9605       420074 :         i (tt_variant_type);
    9606      1989138 :       tree_node (root);
    9607              : 
    9608      1989138 :       int flags = -1;
    9609              : 
    9610      1989138 :       if (TREE_CODE (type) == FUNCTION_TYPE
    9611      1989138 :           || TREE_CODE (type) == METHOD_TYPE)
    9612              :         {
    9613       474969 :           int quals = type_memfn_quals (type);
    9614       474969 :           int rquals = type_memfn_rqual (type);
    9615       474969 :           tree raises = TYPE_RAISES_EXCEPTIONS (type);
    9616       474969 :           bool late = TYPE_HAS_LATE_RETURN_TYPE (type);
    9617              : 
    9618       474969 :           if (raises != TYPE_RAISES_EXCEPTIONS (root)
    9619        13771 :               || rquals != type_memfn_rqual (root)
    9620         9891 :               || quals != type_memfn_quals (root)
    9621       484842 :               || late != TYPE_HAS_LATE_RETURN_TYPE (root))
    9622       474969 :             flags = rquals | (int (late) << 2) | (quals << 3);
    9623              :         }
    9624              :       else
    9625              :         {
    9626      1514169 :           if (TYPE_USER_ALIGN (type))
    9627        14503 :             flags = TYPE_ALIGN_RAW (type);
    9628              :         }
    9629              : 
    9630      1989138 :       if (streaming_p ())
    9631       420074 :         i (flags);
    9632              : 
    9633      1989138 :       if (flags < 0)
    9634              :         ;
    9635       489472 :       else if (TREE_CODE (type) == FUNCTION_TYPE
    9636       489472 :                || TREE_CODE (type) == METHOD_TYPE)
    9637              :         {
    9638       474969 :           tree raises = TYPE_RAISES_EXCEPTIONS (type);
    9639       474969 :           if (raises == TYPE_RAISES_EXCEPTIONS (root))
    9640        13771 :             raises = error_mark_node;
    9641       474969 :           tree_node (raises);
    9642              :         }
    9643              : 
    9644              :       /* build_type_attribute_variant creates a new TYPE_MAIN_VARIANT, so
    9645              :          variants should all have the same set of attributes.  */
    9646      1989138 :       gcc_checking_assert (TYPE_ATTRIBUTES (type)
    9647              :                            == TYPE_ATTRIBUTES (TYPE_MAIN_VARIANT (type)));
    9648              : 
    9649      1989138 :       if (streaming_p ())
    9650              :         {
    9651              :           /* Qualifiers.  */
    9652       420074 :           int rquals = cp_type_quals (root);
    9653       420074 :           int quals = cp_type_quals (type);
    9654       420074 :           if (quals == rquals)
    9655       194530 :             quals = -1;
    9656       420074 :           i (quals);
    9657              :         }
    9658              : 
    9659      1989138 :       if (ref_node (type) != WK_none)
    9660              :         {
    9661      1989138 :           int tag = insert (type);
    9662      1989138 :           if (streaming_p ())
    9663              :             {
    9664       420074 :               i (0);
    9665       420074 :               dump (dumper::TREE)
    9666          203 :                 && dump ("Wrote:%d variant type %C", tag, TREE_CODE (type));
    9667              :             }
    9668              :         }
    9669      1989138 :       return;
    9670              :     }
    9671              : 
    9672      6795498 :   if (tree name = TYPE_NAME (type))
    9673      2664280 :     if ((TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name))
    9674      2086304 :         || DECL_TEMPLATE_PARM_P (name)
    9675      1412035 :         || TREE_CODE (type) == RECORD_TYPE
    9676       253221 :         || TREE_CODE (type) == UNION_TYPE
    9677      2911508 :         || TREE_CODE (type) == ENUMERAL_TYPE)
    9678              :       {
    9679      2486286 :         gcc_checking_assert (DECL_P (name));
    9680              : 
    9681              :         /* We can meet template parms that we didn't meet in the
    9682              :            tpl_parms walk, because we're referring to a derived type
    9683              :            that was previously constructed from equivalent template
    9684              :            parms. */
    9685      2486286 :         if (streaming_p ())
    9686              :           {
    9687       173440 :             i (tt_typedef_type);
    9688       173440 :             dump (dumper::TREE)
    9689           59 :               && dump ("Writing %stypedef %C:%N",
    9690           59 :                        DECL_IMPLICIT_TYPEDEF_P (name) ? "implicit " : "",
    9691           59 :                        TREE_CODE (name), name);
    9692              :           }
    9693      2486286 :         tree_node (name);
    9694      2486286 :         if (streaming_p ())
    9695       173440 :           dump (dumper::TREE) && dump ("Wrote typedef %C:%N%S",
    9696           59 :                                        TREE_CODE (name), name, name);
    9697              : 
    9698              :         /* We'll have either visited this type or have newly discovered
    9699              :            that it's TU-local; either way we won't need to visit it again.  */
    9700      2486286 :         gcc_checking_assert (TREE_VISITED (type) || has_tu_local_dep (name));
    9701      2486286 :         return;
    9702              :       }
    9703              : 
    9704      4309212 :   if (TYPE_PTRMEMFUNC_P (type))
    9705              :     {
    9706              :       /* This is a distinct type node, masquerading as a structure. */
    9707         4204 :       tree fn_type = TYPE_PTRMEMFUNC_FN_TYPE (type);
    9708         4204 :       if (streaming_p ())
    9709         1181 :         i (tt_ptrmem_type);
    9710         4204 :       tree_node (fn_type);
    9711         4204 :       int tag = insert (type);
    9712         4204 :       if (streaming_p ())
    9713         1184 :         dump (dumper::TREE) && dump ("Written:%d ptrmem type", tag);
    9714         4204 :       return;
    9715              :     }
    9716              : 
    9717      4305008 :   if (streaming_p ())
    9718              :     {
    9719      1382597 :       u (tt_derived_type);
    9720      1382597 :       u (TREE_CODE (type));
    9721              :     }
    9722              : 
    9723      4305008 :   tree_node (TREE_TYPE (type));
    9724      4305008 :   switch (TREE_CODE (type))
    9725              :     {
    9726            0 :     default:
    9727              :       /* We should never meet a type here that is indescribable in
    9728              :          terms of other types.  */
    9729            0 :       gcc_unreachable ();
    9730              : 
    9731        69083 :     case ARRAY_TYPE:
    9732        69083 :       tree_node (TYPE_DOMAIN (type));
    9733        69083 :       if (streaming_p ())
    9734              :         /* Dependent arrays are constructed with TYPE_DEPENENT_P
    9735              :            already set.  */
    9736        22416 :         u (TYPE_DEPENDENT_P (type));
    9737              :       break;
    9738              : 
    9739              :     case COMPLEX_TYPE:
    9740              :       /* No additional data.  */
    9741              :       break;
    9742              : 
    9743           12 :     case BOOLEAN_TYPE:
    9744              :       /* A non-standard boolean type.  */
    9745           12 :       if (streaming_p ())
    9746            6 :         u (TYPE_PRECISION (type));
    9747              :       break;
    9748              : 
    9749        63494 :     case INTEGER_TYPE:
    9750        63494 :       if (TREE_TYPE (type))
    9751              :         {
    9752              :           /* A range type (representing an array domain).  */
    9753        60505 :           tree_node (TYPE_MIN_VALUE (type));
    9754        60505 :           tree_node (TYPE_MAX_VALUE (type));
    9755              :         }
    9756              :       else
    9757              :         {
    9758              :           /* A new integral type (representing a bitfield).  */
    9759         2989 :           if (streaming_p ())
    9760              :             {
    9761          861 :               unsigned prec = TYPE_PRECISION (type);
    9762          861 :               bool unsigned_p = TYPE_UNSIGNED (type);
    9763              : 
    9764          861 :               u ((prec << 1) | unsigned_p);
    9765              :             }
    9766              :         }
    9767              :       break;
    9768              : 
    9769       947681 :     case METHOD_TYPE:
    9770       947681 :     case FUNCTION_TYPE:
    9771       947681 :       {
    9772       947681 :         gcc_checking_assert (type_memfn_rqual (type) == REF_QUAL_NONE);
    9773              : 
    9774       947681 :         tree arg_types = TYPE_ARG_TYPES (type);
    9775       947681 :         if (TREE_CODE (type) == METHOD_TYPE)
    9776              :           {
    9777       586840 :             tree_node (TREE_TYPE (TREE_VALUE (arg_types)));
    9778       586840 :             arg_types = TREE_CHAIN (arg_types);
    9779              :           }
    9780       947681 :         tree_node (arg_types);
    9781              :       }
    9782       947681 :       break;
    9783              : 
    9784         1300 :     case OFFSET_TYPE:
    9785         1300 :       tree_node (TYPE_OFFSET_BASETYPE (type));
    9786         1300 :       break;
    9787              : 
    9788              :     case POINTER_TYPE:
    9789              :       /* No additional data.  */
    9790              :       break;
    9791              : 
    9792       725536 :     case REFERENCE_TYPE:
    9793       725536 :       if (streaming_p ())
    9794       162415 :         u (TYPE_REF_IS_RVALUE (type));
    9795              :       break;
    9796              : 
    9797       858207 :     case DECLTYPE_TYPE:
    9798       858207 :     case TYPEOF_TYPE:
    9799       858207 :     case DEPENDENT_OPERATOR_TYPE:
    9800       858207 :       tree_node (TYPE_VALUES_RAW (type));
    9801       858207 :       if (TREE_CODE (type) == DECLTYPE_TYPE)
    9802              :         /* We stash a whole bunch of things into decltype's
    9803              :            flags.  */
    9804        66277 :         if (streaming_p ())
    9805        22717 :           tree_node_bools (type);
    9806              :       break;
    9807              : 
    9808         7509 :     case TRAIT_TYPE:
    9809         7509 :       tree_node (TRAIT_TYPE_KIND_RAW (type));
    9810         7509 :       tree_node (TRAIT_TYPE_TYPE1 (type));
    9811         7509 :       tree_node (TRAIT_TYPE_TYPE2 (type));
    9812         7509 :       break;
    9813              : 
    9814              :     case TYPE_ARGUMENT_PACK:
    9815              :       /* No additional data.  */
    9816              :       break;
    9817              : 
    9818       151126 :     case TYPE_PACK_EXPANSION:
    9819       151126 :       if (streaming_p ())
    9820        61816 :         u (PACK_EXPANSION_LOCAL_P (type));
    9821       302252 :       tree_node (PACK_EXPANSION_PARAMETER_PACKS (type));
    9822       151126 :       tree_node (PACK_EXPANSION_EXTRA_ARGS (type));
    9823       151126 :       break;
    9824              : 
    9825           31 :     case PACK_INDEX_TYPE:
    9826           31 :       tree_node (PACK_INDEX_PACK (type));
    9827           31 :       tree_node (PACK_INDEX_INDEX (type));
    9828           31 :       break;
    9829              : 
    9830       179841 :     case TYPENAME_TYPE:
    9831       179841 :       {
    9832       179841 :         tree_node (TYPE_CONTEXT (type));
    9833       179841 :         tree_node (DECL_NAME (TYPE_NAME (type)));
    9834       179841 :         tree_node (TYPENAME_TYPE_FULLNAME (type));
    9835       179841 :         if (streaming_p ())
    9836        63514 :           u (get_typename_tag (type));
    9837              :         }
    9838              :       break;
    9839              : 
    9840          228 :     case UNBOUND_CLASS_TEMPLATE:
    9841          228 :       {
    9842          228 :         tree decl = TYPE_NAME (type);
    9843          228 :         tree_node (DECL_CONTEXT (decl));
    9844          228 :         tree_node (DECL_NAME (decl));
    9845          228 :         tree_node (DECL_TEMPLATE_PARMS (decl));
    9846              :       }
    9847          228 :       break;
    9848              : 
    9849           42 :     case VECTOR_TYPE:
    9850           42 :       if (streaming_p ())
    9851              :         {
    9852           21 :           poly_uint64 nunits = TYPE_VECTOR_SUBPARTS (type);
    9853           42 :           for (unsigned ix = 0; ix != NUM_POLY_INT_COEFFS; ix++)
    9854           21 :             wu (nunits.coeffs[ix]);
    9855              :         }
    9856              :       break;
    9857              : 
    9858              :     case META_TYPE:
    9859              :       /* No additional data.  */
    9860              :       break;
    9861              : 
    9862            4 :     case SPLICE_SCOPE:
    9863            4 :       if (streaming_p ())
    9864            2 :         u (SPLICE_SCOPE_TYPE_P (type));
    9865            4 :       tree_node (SPLICE_SCOPE_EXPR (type));
    9866            4 :       break;
    9867              :     }
    9868              : 
    9869      4305008 :   tree_node (TYPE_ATTRIBUTES (type));
    9870              : 
    9871              :   /* We may have met the type during emitting the above.  */
    9872      4305008 :   if (ref_node (type) != WK_none)
    9873              :     {
    9874      3909043 :       int tag = insert (type);
    9875      3909043 :       if (streaming_p ())
    9876              :         {
    9877      1196828 :           i (0);
    9878      1196828 :           dump (dumper::TREE)
    9879          558 :             && dump ("Wrote:%d derived type %C", tag, TREE_CODE (type));
    9880              :         }
    9881              :     }
    9882              : 
    9883              :   return;
    9884              : }
    9885              : 
    9886              : /* T is (mostly*) a non-mergeable node that must be written by value.
    9887              :    The mergeable case is a BINFO, which are as-if DECLSs.   */
    9888              : 
    9889              : void
    9890     26540246 : trees_out::tree_value (tree t)
    9891              : {
    9892              :   /* We should never be writing a type by value.  tree_type should
    9893              :      have streamed it, or we're going via its TYPE_DECL.  */
    9894     26540246 :   gcc_checking_assert (!TYPE_P (t));
    9895              : 
    9896     26540246 :   if (DECL_P (t))
    9897              :     /* No template, type, var or function, except anonymous
    9898              :        non-context vars and types.  */
    9899       697019 :     gcc_checking_assert ((TREE_CODE (t) != TEMPLATE_DECL
    9900              :                           && (TREE_CODE (t) != TYPE_DECL
    9901              :                               || (DECL_ARTIFICIAL (t) && !DECL_CONTEXT (t)))
    9902              :                           && (TREE_CODE (t) != VAR_DECL
    9903              :                               || ((!DECL_NAME (t)
    9904              :                                    || IDENTIFIER_INTERNAL_P (DECL_NAME (t)))
    9905              :                                   && !DECL_CONTEXT (t)))
    9906              :                           && TREE_CODE (t) != FUNCTION_DECL));
    9907              : 
    9908     39993384 :   if (is_initial_scan () && EXPR_P (t))
    9909      5248761 :     dep_hash->add_dependent_adl_entities (t);
    9910              : 
    9911     26540246 :   if (streaming_p ())
    9912              :     {
    9913              :       /* A new node -> tt_node.  */
    9914     10592579 :       tree_val_count++;
    9915     10592579 :       i (tt_node);
    9916     10592579 :       start (t);
    9917     10592579 :       tree_node_bools (t);
    9918              :     }
    9919              : 
    9920     26540246 :   if (TREE_CODE (t) == TREE_BINFO)
    9921              :     /* Binfos are decl-like and need merging information.  */
    9922       205142 :     binfo_mergeable (t);
    9923              : 
    9924     26540246 :   int tag = insert (t, WK_value);
    9925     26540246 :   if (streaming_p ())
    9926     10592579 :     dump (dumper::TREE)
    9927         2823 :       && dump ("Writing tree:%d %C:%N", tag, TREE_CODE (t), t);
    9928              : 
    9929     26540246 :   int type_tag = 0;
    9930     26540246 :   tree type = NULL_TREE;
    9931     26540246 :   if (TREE_CODE (t) == TYPE_DECL)
    9932              :     {
    9933           28 :       type = TREE_TYPE (t);
    9934              : 
    9935              :       /* We only support a limited set of features for uncontexted types;
    9936              :          these are typically types created in the language-independent
    9937              :          parts of the frontend (such as ubsan).  */
    9938           28 :       gcc_checking_assert (RECORD_OR_UNION_TYPE_P (type)
    9939              :                            && TYPE_MAIN_VARIANT (type) == type
    9940              :                            && TYPE_NAME (type) == t
    9941              :                            && TYPE_STUB_DECL (type) == t
    9942              :                            && !TYPE_VFIELD (type)
    9943              :                            && !TYPE_BINFO (type)
    9944              :                            && !CLASS_TYPE_P (type));
    9945              : 
    9946           28 :       if (streaming_p ())
    9947              :         {
    9948           14 :           start (type);
    9949           14 :           tree_node_bools (type);
    9950              :         }
    9951              : 
    9952           28 :       type_tag = insert (type, WK_value);
    9953           28 :       if (streaming_p ())
    9954           14 :         dump (dumper::TREE)
    9955            0 :           && dump ("Writing type: %d %C:%N", type_tag,
    9956            0 :                    TREE_CODE (type), type);
    9957              :     }
    9958              : 
    9959     26540246 :   tree_node_vals (t);
    9960              : 
    9961     26540246 :   if (type)
    9962              :     {
    9963           28 :       tree_node_vals (type);
    9964           28 :       tree_node (TYPE_SIZE (type));
    9965           28 :       tree_node (TYPE_SIZE_UNIT (type));
    9966           28 :       chained_decls (TYPE_FIELDS (type));
    9967           28 :       if (streaming_p ())
    9968           14 :         dump (dumper::TREE)
    9969            0 :           && dump ("Written type:%d %C:%N", type_tag, TREE_CODE (type), type);
    9970              :     }
    9971              : 
    9972              :   /* For uncontexted VAR_DECLs we need to stream the definition so that
    9973              :      importers can recreate their value.  */
    9974     26540246 :   if (TREE_CODE (t) == VAR_DECL)
    9975              :     {
    9976          614 :       gcc_checking_assert (!DECL_NONTRIVIALLY_INITIALIZED_P (t));
    9977          614 :       tree_node (DECL_INITIAL (t));
    9978              :     }
    9979              : 
    9980     26540246 :   if (streaming_p ())
    9981     10595402 :     dump (dumper::TREE) && dump ("Written tree:%d %C:%N", tag, TREE_CODE (t), t);
    9982     26540246 : }
    9983              : 
    9984              : tree
    9985     10873059 : trees_in::tree_value ()
    9986              : {
    9987     10873059 :   tree t = start ();
    9988     10873059 :   if (!t || !tree_node_bools (t))
    9989            0 :     return NULL_TREE;
    9990              : 
    9991     10873059 :   tree existing = t;
    9992     10873059 :   if (TREE_CODE (t) == TREE_BINFO)
    9993              :     {
    9994        85654 :       tree type;
    9995        85654 :       unsigned ix = binfo_mergeable (&type);
    9996        85654 :       if (TYPE_BINFO (type))
    9997              :         {
    9998              :           /* We already have a definition, this must be a duplicate.  */
    9999        41316 :           dump (dumper::MERGE)
   10000          268 :             && dump ("Deduping binfo %N[%u]", type, ix);
   10001        41316 :           existing = TYPE_BINFO (type);
   10002        56606 :           while (existing && ix--)
   10003        15290 :             existing = TREE_CHAIN (existing);
   10004        41316 :           if (existing)
   10005        41316 :             register_duplicate (t, existing);
   10006              :           else
   10007              :             /* Error, mismatch -- diagnose in read_class_def's
   10008              :                checking.  */
   10009              :             existing = t;
   10010              :         }
   10011              :     }
   10012              : 
   10013              :   /* Insert into map.  */
   10014     10873059 :   int tag = insert (existing);
   10015     10873059 :   dump (dumper::TREE)
   10016         3677 :     && dump ("Reading tree:%d %C", tag, TREE_CODE (t));
   10017              : 
   10018     10873059 :   int type_tag = 0;
   10019     10873059 :   tree type = NULL_TREE;
   10020     10873059 :   if (TREE_CODE (t) == TYPE_DECL)
   10021              :     {
   10022           14 :       type = start ();
   10023           14 :       if (!type || !tree_node_bools (type))
   10024              :         t = NULL_TREE;
   10025              : 
   10026           14 :       type_tag = insert (type);
   10027           14 :       if (t)
   10028           14 :         dump (dumper::TREE)
   10029            0 :           && dump ("Reading type:%d %C", type_tag, TREE_CODE (type));
   10030              :     }
   10031              : 
   10032              :   if (!t)
   10033              :     {
   10034            0 : bail:
   10035            0 :       back_refs[~tag] = NULL_TREE;
   10036            0 :       if (type_tag)
   10037            0 :         back_refs[~type_tag] = NULL_TREE;
   10038            0 :       set_overrun ();
   10039            0 :       return NULL_TREE;
   10040              :     }
   10041              : 
   10042     10873059 :   if (!tree_node_vals (t))
   10043            0 :     goto bail;
   10044              : 
   10045     10873059 :   if (type)
   10046              :     {
   10047           14 :       if (!tree_node_vals (type))
   10048            0 :         goto bail;
   10049              : 
   10050           14 :       TYPE_SIZE (type) = tree_node ();
   10051           14 :       TYPE_SIZE_UNIT (type) = tree_node ();
   10052           14 :       TYPE_FIELDS (type) = chained_decls ();
   10053           14 :       if (get_overrun ())
   10054            0 :         goto bail;
   10055              : 
   10056           14 :       dump (dumper::TREE)
   10057            0 :         && dump ("Read type:%d %C:%N", type_tag, TREE_CODE (type), type);
   10058              :     }
   10059              : 
   10060     10873059 :   if (TREE_CODE (t) == VAR_DECL)
   10061              :     {
   10062          331 :       DECL_INITIAL (t) = tree_node ();
   10063          331 :       if (TREE_STATIC (t))
   10064            8 :         varpool_node::finalize_decl (t);
   10065              :     }
   10066              : 
   10067     10873059 :   if (TREE_CODE (t) == LAMBDA_EXPR
   10068     10873059 :       && CLASSTYPE_LAMBDA_EXPR (TREE_TYPE (t)))
   10069              :     {
   10070         1914 :       existing = CLASSTYPE_LAMBDA_EXPR (TREE_TYPE (t));
   10071         1914 :       back_refs[~tag] = existing;
   10072              :     }
   10073              : 
   10074     10876736 :   dump (dumper::TREE) && dump ("Read tree:%d %C:%N", tag, TREE_CODE (t), t);
   10075              : 
   10076     10873059 :   if (TREE_CODE (existing) == INTEGER_CST && !TREE_OVERFLOW (existing))
   10077              :     {
   10078       576712 :       existing = cache_integer_cst (t, true);
   10079       576712 :       back_refs[~tag] = existing;
   10080              :     }
   10081              : 
   10082              :   return existing;
   10083              : }
   10084              : 
   10085              : /* Whether DECL has a TU-local dependency in the hash.  */
   10086              : 
   10087              : bool
   10088       797237 : trees_out::has_tu_local_dep (tree decl) const
   10089              : {
   10090              :   /* Only the contexts of fields or enums remember that they're
   10091              :      TU-local.  */
   10092       797237 :   if (DECL_CONTEXT (decl)
   10093       797237 :       && (TREE_CODE (decl) == FIELD_DECL
   10094       797234 :           || TREE_CODE (decl) == CONST_DECL))
   10095            3 :     decl = TYPE_NAME (DECL_CONTEXT (decl));
   10096              : 
   10097       797237 :   depset *dep = dep_hash->find_dependency (decl);
   10098       797237 :   if (!dep)
   10099              :     {
   10100              :       /* This might be the DECL_TEMPLATE_RESULT of a TEMPLATE_DECL
   10101              :          which we found was TU-local and gave up early.  */
   10102        10486 :       int use_tpl = -1;
   10103        10486 :       if (tree ti = node_template_info (decl, use_tpl))
   10104         1773 :         dep = dep_hash->find_dependency (TI_TEMPLATE (ti));
   10105              :     }
   10106              : 
   10107       797237 :   return dep && dep->is_tu_local ();
   10108              : }
   10109              : 
   10110              : /* If T depends on a TU-local entity, return that decl.  */
   10111              : 
   10112              : tree
   10113          395 : trees_out::find_tu_local_decl (tree t)
   10114              : {
   10115              :   /* We need to have walked all deps first before we can check.  */
   10116          395 :   gcc_checking_assert (!is_initial_scan ());
   10117              : 
   10118          951 :   auto walker = [](tree *tp, int *walk_subtrees, void *data) -> tree
   10119              :     {
   10120          556 :       auto self = (trees_out *)data;
   10121              : 
   10122          556 :       tree decl = NULL_TREE;
   10123          556 :       if (TYPE_P (*tp))
   10124              :         {
   10125              :           /* A PMF type is a record type, which we otherwise wouldn't walk;
   10126              :              return whether the function type is TU-local.  */
   10127          370 :           if (TYPE_PTRMEMFUNC_P (*tp))
   10128              :             {
   10129            3 :               *walk_subtrees = 0;
   10130            3 :               return self->find_tu_local_decl (TYPE_PTRMEMFUNC_FN_TYPE (*tp));
   10131              :             }
   10132              :           else
   10133          367 :             decl = TYPE_MAIN_DECL (*tp);
   10134              :         }
   10135          186 :       else if (DECL_P (*tp))
   10136              :         decl = *tp;
   10137              : 
   10138          373 :       if (decl)
   10139              :         {
   10140              :           /* We found a DECL, this will tell us whether we're TU-local.  */
   10141           59 :           *walk_subtrees = 0;
   10142           59 :           return self->has_tu_local_dep (decl) ? decl : NULL_TREE;
   10143              :         }
   10144              :       return NULL_TREE;
   10145              :     };
   10146              : 
   10147              :   /* We need to walk without duplicates so that we step into the pointed-to
   10148              :      types of array types.  */
   10149          395 :   return cp_walk_tree_without_duplicates (&t, walker, this);
   10150              : }
   10151              : 
   10152              : /* Get the name for TU-local decl T to be used in diagnostics.  */
   10153              : 
   10154              : static tree
   10155          206 : name_for_tu_local_decl (tree t)
   10156              : {
   10157          206 :   int flags = (TFF_SCOPE | TFF_DECL_SPECIFIERS);
   10158          206 :   const char *str = decl_as_string (t, flags);
   10159          206 :   return get_identifier (str);
   10160              : }
   10161              : 
   10162              : /* Stream out tree node T.  We automatically create local back
   10163              :    references, which is essentially a single pass lisp
   10164              :    self-referential structure pretty-printer.  */
   10165              : 
   10166              : void
   10167    231003244 : trees_out::tree_node (tree t)
   10168              : {
   10169    231003244 :   dump.indent ();
   10170    231003244 :   walk_kind ref = ref_node (t);
   10171    231003244 :   if (ref == WK_none)
   10172    177273303 :     goto done;
   10173              : 
   10174              :   /* Find TU-local entities and intercept streaming to instead write a
   10175              :      placeholder value; this way we don't need to emit such decls.
   10176              :      We only need to do this when writing a definition of an entity
   10177              :      that we know names a TU-local entity.  */
   10178     62945579 :   if (!is_initial_scan () && writing_local_entities)
   10179              :     {
   10180          952 :       tree local_decl = NULL_TREE;
   10181          952 :       if (DECL_P (t) && has_tu_local_dep (t))
   10182              :         local_decl = t;
   10183              :       /* Consider a type to be TU-local if it refers to any TU-local decl,
   10184              :          no matter how deep.
   10185              : 
   10186              :          This worsens diagnostics slightly, as we often no longer point
   10187              :          directly to the at-fault entity when instantiating.  However, this
   10188              :          reduces the module size slightly and means that much less of pt.cc
   10189              :          needs to know about us.  */
   10190          848 :       else if (TYPE_P (t))
   10191          142 :         local_decl = find_tu_local_decl (t);
   10192          706 :       else if (EXPR_P (t))
   10193          250 :         local_decl = find_tu_local_decl (TREE_TYPE (t));
   10194              : 
   10195          496 :       if (local_decl)
   10196              :         {
   10197          158 :           int tag = insert (t, WK_value);
   10198          158 :           if (streaming_p ())
   10199              :             {
   10200          158 :               tu_local_count++;
   10201          158 :               i (tt_tu_local);
   10202          158 :               dump (dumper::TREE)
   10203            0 :                 && dump ("Writing TU-local entity:%d %C:%N",
   10204            0 :                          tag, TREE_CODE (t), t);
   10205              :             }
   10206          158 :           tree_node (name_for_tu_local_decl (local_decl));
   10207          158 :           if (state)
   10208          158 :             state->write_location (*this, DECL_SOURCE_LOCATION (local_decl));
   10209          158 :           goto done;
   10210              :         }
   10211              :     }
   10212              : 
   10213     53729783 :   if (ref != WK_normal)
   10214      1337613 :     goto skip_normal;
   10215              : 
   10216     52392170 :   if (TREE_CODE (t) == IDENTIFIER_NODE)
   10217              :     {
   10218              :       /* An identifier node -> tt_id, tt_conv_id, tt_anon_id, tt_lambda_id,
   10219              :          tt_internal_id.  */
   10220      6470578 :       int code = tt_id;
   10221      6470578 :       if (IDENTIFIER_ANON_P (t))
   10222        23679 :         code = IDENTIFIER_LAMBDA_P (t) ? tt_lambda_id : tt_anon_id;
   10223      6446899 :       else if (IDENTIFIER_INTERNAL_P (t))
   10224              :         code = tt_internal_id;
   10225      6446885 :       else if (IDENTIFIER_CONV_OP_P (t))
   10226         9677 :         code = tt_conv_id;
   10227              : 
   10228      6470578 :       if (streaming_p ())
   10229      1327247 :         i (code);
   10230              : 
   10231      6470578 :       if (code == tt_conv_id)
   10232              :         {
   10233         9677 :           tree type = TREE_TYPE (t);
   10234         9677 :           gcc_checking_assert (type || t == conv_op_identifier);
   10235         9677 :           tree_node (type);
   10236              :         }
   10237      6460901 :       else if (code == tt_id && streaming_p ())
   10238      1316698 :         str (IDENTIFIER_POINTER (t), IDENTIFIER_LENGTH (t));
   10239      5144203 :       else if (code == tt_internal_id && streaming_p ())
   10240            7 :         str (prefix_for_internal_label (t));
   10241              : 
   10242      6470578 :       int tag = insert (t);
   10243      6470578 :       if (streaming_p ())
   10244              :         {
   10245              :           /* We know the ordering of the 5 id tags.  */
   10246      1327247 :           static const char *const kinds[] =
   10247              :             {"", "conv_op ", "anon ", "lambda ", "internal "};
   10248      1327247 :           dump (dumper::TREE)
   10249         1074 :             && dump ("Written:%d %sidentifier:%N", tag,
   10250         1071 :                      kinds[code - tt_id],
   10251            3 :                      code == tt_conv_id ? TREE_TYPE (t) : t);
   10252              :         }
   10253      6470578 :       goto done;
   10254              :     }
   10255              : 
   10256     45921592 :   if (TREE_CODE (t) == TREE_BINFO)
   10257              :     {
   10258              :       /* A BINFO -> tt_binfo.
   10259              :          We must do this by reference.  We stream the binfo tree
   10260              :          itself when streaming its owning RECORD_TYPE.  That we got
   10261              :          here means the dominating type is not in this SCC.  */
   10262        53291 :       if (streaming_p ())
   10263         1567 :         i (tt_binfo);
   10264        53291 :       binfo_mergeable (t);
   10265        53291 :       gcc_checking_assert (!TREE_VISITED (t));
   10266        53291 :       int tag = insert (t);
   10267        53291 :       if (streaming_p ())
   10268         1567 :         dump (dumper::TREE) && dump ("Inserting binfo:%d %N", tag, t);
   10269        53291 :       goto done;
   10270              :     }
   10271              : 
   10272     45868301 :   if (TREE_CODE (t) == INTEGER_CST
   10273      3394756 :       && !TREE_OVERFLOW (t)
   10274     49263057 :       && TREE_CODE (TREE_TYPE (t)) == ENUMERAL_TYPE)
   10275              :     {
   10276              :       /* An integral constant of enumeral type.  See if it matches one
   10277              :          of the enumeration values.  */
   10278        29819 :       for (tree values = TYPE_VALUES (TREE_TYPE (t));
   10279       782175 :            values; values = TREE_CHAIN (values))
   10280              :         {
   10281       780531 :           tree decl = TREE_VALUE (values);
   10282       780531 :           if (tree_int_cst_equal (DECL_INITIAL (decl), t))
   10283              :             {
   10284        28175 :               if (streaming_p ())
   10285         8187 :                 u (tt_enum_value);
   10286        28175 :               tree_node (decl);
   10287        28217 :               dump (dumper::TREE) && dump ("Written enum value %N", decl);
   10288        28175 :               goto done;
   10289              :             }
   10290              :         }
   10291              :       /* It didn't match.  We'll write it a an explicit INTEGER_CST
   10292              :          node.  */
   10293              :     }
   10294              : 
   10295     45840126 :   if (TYPE_P (t))
   10296              :     {
   10297      8784636 :       type_node (t);
   10298      8784636 :       goto done;
   10299              :     }
   10300              : 
   10301     37055490 :   if (DECL_P (t))
   10302              :     {
   10303     11446845 :       if (DECL_TEMPLATE_PARM_P (t))
   10304              :         {
   10305      1810749 :           tpl_parm_value (t);
   10306      1810749 :           goto done;
   10307              :         }
   10308              : 
   10309      9636096 :       if (!DECL_CONTEXT (t))
   10310              :         {
   10311              :           /* There are a few cases of decls with no context.  We'll write
   10312              :              these by value, but first assert they are cases we expect.  */
   10313        21743 :           gcc_checking_assert (ref == WK_normal);
   10314        21743 :           switch (TREE_CODE (t))
   10315              :             {
   10316            0 :             default: gcc_unreachable ();
   10317              : 
   10318         7448 :             case LABEL_DECL:
   10319              :               /* CASE_LABEL_EXPRs contain uncontexted LABEL_DECLs.  */
   10320         7448 :               gcc_checking_assert (!DECL_NAME (t));
   10321              :               break;
   10322              : 
   10323          614 :             case VAR_DECL:
   10324              :               /* AGGR_INIT_EXPRs cons up anonymous uncontexted VAR_DECLs,
   10325              :                  and internal vars are created by sanitizers and
   10326              :                  __builtin_source_location.  */
   10327          614 :               gcc_checking_assert ((!DECL_NAME (t)
   10328              :                                     || IDENTIFIER_INTERNAL_P (DECL_NAME (t)))
   10329              :                                    && DECL_ARTIFICIAL (t));
   10330              :               break;
   10331              : 
   10332        13653 :             case PARM_DECL:
   10333              :               /* REQUIRES_EXPRs have a chain of uncontexted PARM_DECLS,
   10334              :                  and an implicit this parm in an NSDMI has no context.  */
   10335        13653 :               gcc_checking_assert (CONSTRAINT_VAR_P (t)
   10336              :                                    || DECL_NAME (t) == this_identifier);
   10337              :               break;
   10338              : 
   10339           28 :             case TYPE_DECL:
   10340              :               /* Some parts of the compiler need internal struct types;
   10341              :                  these types may not have an appropriate context to use.
   10342              :                  Walk the whole type (including its definition) by value.  */
   10343           28 :               gcc_checking_assert (DECL_ARTIFICIAL (t)
   10344              :                                    && TYPE_ARTIFICIAL (TREE_TYPE (t))
   10345              :                                    && RECORD_OR_UNION_TYPE_P (TREE_TYPE (t))
   10346              :                                    && !CLASS_TYPE_P (TREE_TYPE (t)));
   10347              :               break;
   10348              :             }
   10349        21743 :           mark_declaration (t, has_definition (t));
   10350        21743 :           goto by_value;
   10351              :         }
   10352              :     }
   10353              : 
   10354     25608645 :  skip_normal:
   10355     36560611 :   if (DECL_P (t) && !decl_node (t, ref))
   10356     10042108 :     goto done;
   10357              : 
   10358              :   /* Otherwise by value */
   10359     26540246 :  by_value:
   10360     26540246 :   tree_value (t);
   10361              : 
   10362    231003244 :  done:
   10363              :   /* And, breath out.  */
   10364    231003244 :   dump.outdent ();
   10365    231003244 : }
   10366              : 
   10367              : /* Stream in a tree node.  */
   10368              : 
   10369              : tree
   10370     88884117 : trees_in::tree_node (bool is_use)
   10371              : {
   10372     88884117 :   if (get_overrun ())
   10373              :     return NULL_TREE;
   10374              : 
   10375     88884117 :   dump.indent ();
   10376     88884117 :   int tag = i ();
   10377     88884117 :   tree res = NULL_TREE;
   10378     88884117 :   switch (tag)
   10379              :     {
   10380     29138234 :     default:
   10381              :       /* backref, pull it out of the map.  */
   10382     29138234 :       res = back_ref (tag);
   10383     29138234 :       break;
   10384              : 
   10385              :     case tt_null:
   10386              :       /* NULL_TREE.  */
   10387              :       break;
   10388              : 
   10389          158 :     case tt_tu_local:
   10390          158 :       {
   10391              :         /* A translation-unit-local entity.  */
   10392          158 :         res = make_node (TU_LOCAL_ENTITY);
   10393          158 :         int tag = insert (res);
   10394              : 
   10395          158 :         TU_LOCAL_ENTITY_NAME (res) = tree_node ();
   10396          158 :         TU_LOCAL_ENTITY_LOCATION (res) = state->read_location (*this);
   10397          158 :         dump (dumper::TREE) && dump ("Read TU-local entity:%d %N", tag, res);
   10398              :       }
   10399              :       break;
   10400              : 
   10401      6861220 :     case tt_fixed:
   10402              :       /* A fixed ref, find it in the fixed_ref array.   */
   10403      6861220 :       {
   10404      6861220 :         unsigned fix = u ();
   10405      6861220 :         if (fix < (*fixed_trees).length ())
   10406              :           {
   10407      6861220 :             res = (*fixed_trees)[fix];
   10408      6861220 :             dump (dumper::TREE) && dump ("Read fixed:%u %C:%N%S", fix,
   10409         5046 :                                          TREE_CODE (res), res, res);
   10410              :           }
   10411              : 
   10412      6856174 :         if (!res)
   10413            0 :           set_overrun ();
   10414              :       }
   10415              :       break;
   10416              : 
   10417        72907 :     case tt_parm:
   10418        72907 :       {
   10419        72907 :         tree fn = tree_node ();
   10420        72907 :         if (fn && TREE_CODE (fn) == FUNCTION_DECL)
   10421        72907 :           res = tree_node ();
   10422        72907 :         if (res)
   10423        72907 :           dump (dumper::TREE)
   10424           21 :             && dump ("Read %s reference %N",
   10425           21 :                      TREE_CODE (res) == PARM_DECL ? "parameter" : "result",
   10426              :                      res);
   10427              :       }
   10428              :       break;
   10429              : 
   10430     10873059 :     case tt_node:
   10431              :       /* A new node.  Stream it in.  */
   10432     10873059 :       res = tree_value ();
   10433     10873059 :       break;
   10434              : 
   10435      1117422 :     case tt_decl:
   10436              :       /* A new decl.  Stream it in.  */
   10437      1117422 :       res = decl_value ();
   10438      1117422 :       break;
   10439              : 
   10440       392200 :     case tt_tpl_parm:
   10441              :       /* A template parameter.  Stream it in.  */
   10442       392200 :       res = tpl_parm_value ();
   10443       392200 :       break;
   10444              : 
   10445      1161925 :     case tt_id:
   10446              :       /* An identifier node.  */
   10447      1161925 :       {
   10448      1161925 :         size_t l;
   10449      1161925 :         const char *chars = str (&l);
   10450      1161925 :         res = get_identifier_with_length (chars, l);
   10451      1161925 :         int tag = insert (res);
   10452      1161925 :         dump (dumper::TREE)
   10453         1488 :           && dump ("Read identifier:%d %N", tag, res);
   10454              :       }
   10455      1161925 :       break;
   10456              : 
   10457         3105 :     case tt_conv_id:
   10458              :       /* A conversion operator.  Get the type and recreate the
   10459              :          identifier.  */
   10460         3105 :       {
   10461         3105 :         tree type = tree_node ();
   10462         3105 :         if (!get_overrun ())
   10463              :           {
   10464         3105 :             res = type ? make_conv_op_name (type) : conv_op_identifier;
   10465         3105 :             int tag = insert (res);
   10466         3105 :             dump (dumper::TREE)
   10467           27 :               && dump ("Created conv_op:%d %S for %N", tag, res, type);
   10468              :           }
   10469              :       }
   10470              :       break;
   10471              : 
   10472         6201 :     case tt_anon_id:
   10473         6201 :     case tt_lambda_id:
   10474              :       /* An anonymous or lambda id.  */
   10475         6201 :       {
   10476         6201 :         res = make_anon_name ();
   10477         6201 :         if (tag == tt_lambda_id)
   10478         3615 :           IDENTIFIER_LAMBDA_P (res) = true;
   10479         6201 :         int tag = insert (res);
   10480         6204 :         dump (dumper::TREE)
   10481            3 :           && dump ("Read %s identifier:%d %N",
   10482            3 :                    IDENTIFIER_LAMBDA_P (res) ? "lambda" : "anon", tag, res);
   10483              :       }
   10484              :       break;
   10485              : 
   10486            8 :     case tt_internal_id:
   10487              :       /* An internal label.  */
   10488            8 :       {
   10489            8 :         const char *prefix = str ();
   10490            8 :         res = generate_internal_label (prefix);
   10491            8 :         int tag = insert (res);
   10492            8 :         dump (dumper::TREE)
   10493            1 :           && dump ("Read internal identifier:%d %N", tag, res);
   10494              :       }
   10495              :       break;
   10496              : 
   10497       176226 :     case tt_typedef_type:
   10498       176226 :       res = tree_node ();
   10499       176226 :       if (res)
   10500              :         {
   10501       176226 :           dump (dumper::TREE)
   10502           74 :             && dump ("Read %stypedef %C:%N",
   10503           74 :                      DECL_IMPLICIT_TYPEDEF_P (res) ? "implicit " : "",
   10504           74 :                      TREE_CODE (res), res);
   10505       176226 :           if (TREE_CODE (res) != TU_LOCAL_ENTITY)
   10506       176225 :             res = TREE_TYPE (res);
   10507              :         }
   10508              :       break;
   10509              : 
   10510      1366708 :     case tt_derived_type:
   10511              :       /* A type derived from some other type.  */
   10512      1366708 :       {
   10513      1366708 :         enum tree_code code = tree_code (u ());
   10514      1366708 :         res = tree_node ();
   10515              : 
   10516      1366708 :         switch (code)
   10517              :           {
   10518            0 :           default:
   10519            0 :             set_overrun ();
   10520            0 :             break;
   10521              : 
   10522        20225 :           case ARRAY_TYPE:
   10523        20225 :             {
   10524        20225 :               tree elt_type = res;
   10525        20225 :               tree domain = tree_node ();
   10526        20225 :               int dep = u ();
   10527        20225 :               if (!get_overrun ())
   10528              :                 {
   10529        20225 :                   res = build_cplus_array_type (elt_type, domain, dep);
   10530              :                   /* If we're an array of an incomplete imported type,
   10531              :                      save it for post-processing so that we can attempt
   10532              :                      to complete the type later if it will get a
   10533              :                      definition later in the cluster.  */
   10534        20225 :                   if (!dep
   10535        17553 :                       && !COMPLETE_TYPE_P (elt_type)
   10536           32 :                       && CLASS_TYPE_P (elt_type)
   10537           32 :                       && DECL_LANG_SPECIFIC (TYPE_NAME (elt_type))
   10538        20257 :                       && DECL_MODULE_IMPORT_P (TYPE_NAME (elt_type)))
   10539           32 :                     post_process_type (res);
   10540              :                 }
   10541              :             }
   10542              :             break;
   10543              : 
   10544          197 :           case COMPLEX_TYPE:
   10545          197 :             if (!get_overrun ())
   10546          197 :               res = build_complex_type (res);
   10547              :             break;
   10548              : 
   10549            9 :           case BOOLEAN_TYPE:
   10550            9 :             {
   10551            9 :               unsigned precision = u ();
   10552            9 :               if (!get_overrun ())
   10553            9 :                 res = build_nonstandard_boolean_type (precision);
   10554              :             }
   10555              :             break;
   10556              : 
   10557        18481 :           case INTEGER_TYPE:
   10558        18481 :             if (res)
   10559              :               {
   10560              :                 /* A range type (representing an array domain).  */
   10561        17603 :                 tree min = tree_node ();
   10562        17603 :                 tree max = tree_node ();
   10563              : 
   10564        17603 :                 if (!get_overrun ())
   10565        17603 :                   res = build_range_type (res, min, max);
   10566              :               }
   10567              :             else
   10568              :               {
   10569              :                 /* A new integral type (representing a bitfield).  */
   10570          878 :                 unsigned enc = u ();
   10571          878 :                 if (!get_overrun ())
   10572          878 :                   res = build_nonstandard_integer_type (enc >> 1, enc & 1);
   10573              :               }
   10574              :             break;
   10575              : 
   10576       386467 :           case FUNCTION_TYPE:
   10577       386467 :           case METHOD_TYPE:
   10578       386467 :             {
   10579       386467 :               tree klass =  code == METHOD_TYPE ? tree_node () : NULL_TREE;
   10580       386467 :               tree args = tree_node ();
   10581       386467 :               if (!get_overrun ())
   10582              :                 {
   10583       386467 :                   if (klass)
   10584       244286 :                     res = build_method_type_directly (klass, res, args);
   10585              :                   else
   10586       142181 :                     res = cp_build_function_type (res, args);
   10587              :                 }
   10588              :             }
   10589              :             break;
   10590              : 
   10591          251 :           case OFFSET_TYPE:
   10592          251 :             {
   10593          251 :               tree base = tree_node ();
   10594          251 :               if (!get_overrun ())
   10595          251 :                 res = build_offset_type (base, res);
   10596              :             }
   10597              :             break;
   10598              : 
   10599       187611 :           case POINTER_TYPE:
   10600       187611 :             if (!get_overrun ())
   10601       187611 :               res = build_pointer_type (res);
   10602              :             break;
   10603              : 
   10604       160328 :           case REFERENCE_TYPE:
   10605       160328 :             {
   10606       160328 :               bool rval = bool (u ());
   10607       160328 :               if (!get_overrun ())
   10608       160328 :                 res = cp_build_reference_type (res, rval);
   10609              :             }
   10610              :             break;
   10611              : 
   10612       411399 :           case DECLTYPE_TYPE:
   10613       411399 :           case TYPEOF_TYPE:
   10614       411399 :           case DEPENDENT_OPERATOR_TYPE:
   10615       411399 :             {
   10616       411399 :               tree expr = tree_node ();
   10617       411399 :               if (!get_overrun ())
   10618              :                 {
   10619       411399 :                   res = cxx_make_type (code);
   10620       411399 :                   TYPE_VALUES_RAW (res) = expr;
   10621       411399 :                   if (code == DECLTYPE_TYPE)
   10622        20126 :                     tree_node_bools (res);
   10623       411399 :                   SET_TYPE_STRUCTURAL_EQUALITY (res);
   10624              :                 }
   10625              :             }
   10626              :             break;
   10627              : 
   10628         2129 :           case TRAIT_TYPE:
   10629         2129 :             {
   10630         2129 :               tree kind = tree_node ();
   10631         2129 :               tree type1 = tree_node ();
   10632         2129 :               tree type2 = tree_node ();
   10633         2129 :               if (!get_overrun ())
   10634              :                 {
   10635         2129 :                   res = cxx_make_type (TRAIT_TYPE);
   10636         2129 :                   TRAIT_TYPE_KIND_RAW (res) = kind;
   10637         2129 :                   TRAIT_TYPE_TYPE1 (res) = type1;
   10638         2129 :                   TRAIT_TYPE_TYPE2 (res) = type2;
   10639         2129 :                   SET_TYPE_STRUCTURAL_EQUALITY (res);
   10640              :                 }
   10641              :             }
   10642              :             break;
   10643              : 
   10644        57284 :           case TYPE_ARGUMENT_PACK:
   10645        57284 :             if (!get_overrun ())
   10646              :               {
   10647        57284 :                 tree pack = cxx_make_type (TYPE_ARGUMENT_PACK);
   10648        57284 :                 ARGUMENT_PACK_ARGS (pack) = res;
   10649              :                 res = pack;
   10650              :               }
   10651              :             break;
   10652              : 
   10653        60222 :           case TYPE_PACK_EXPANSION:
   10654        60222 :             {
   10655        60222 :               bool local = u ();
   10656        60222 :               tree param_packs = tree_node ();
   10657        60222 :               tree extra_args = tree_node ();
   10658        60222 :               if (!get_overrun ())
   10659              :                 {
   10660        60222 :                   tree expn = cxx_make_type (TYPE_PACK_EXPANSION);
   10661        60222 :                   SET_TYPE_STRUCTURAL_EQUALITY (expn);
   10662        60222 :                   PACK_EXPANSION_PATTERN (expn) = res;
   10663       120444 :                   PACK_EXPANSION_PARAMETER_PACKS (expn) = param_packs;
   10664        60222 :                   PACK_EXPANSION_EXTRA_ARGS (expn) = extra_args;
   10665        60222 :                   PACK_EXPANSION_LOCAL_P (expn) = local;
   10666        60222 :                   res = expn;
   10667              :                 }
   10668              :             }
   10669              :             break;
   10670              : 
   10671           25 :           case PACK_INDEX_TYPE:
   10672           25 :             {
   10673           25 :               tree pack = tree_node ();
   10674           25 :               tree index = tree_node ();
   10675           25 :               if (!get_overrun ())
   10676           25 :                 res = make_pack_index (pack, index);
   10677              :             }
   10678              :             break;
   10679              : 
   10680        61970 :           case TYPENAME_TYPE:
   10681        61970 :             {
   10682        61970 :               tree ctx = tree_node ();
   10683        61970 :               tree name = tree_node ();
   10684        61970 :               tree fullname = tree_node ();
   10685        61970 :               enum tag_types tag_type = tag_types (u ());
   10686              : 
   10687        61970 :               if (!get_overrun ())
   10688        61970 :                 res = build_typename_type (ctx, name, fullname, tag_type);
   10689              :             }
   10690              :             break;
   10691              : 
   10692           52 :           case UNBOUND_CLASS_TEMPLATE:
   10693           52 :             {
   10694           52 :               tree ctx = tree_node ();
   10695           52 :               tree name = tree_node ();
   10696           52 :               tree parms = tree_node ();
   10697              : 
   10698           52 :               if (!get_overrun ())
   10699           52 :                 res = make_unbound_class_template_raw (ctx, name, parms);
   10700              :             }
   10701              :             break;
   10702              : 
   10703              :           case VECTOR_TYPE:
   10704              :             {
   10705              :               poly_uint64 nunits;
   10706           60 :               for (unsigned ix = 0; ix != NUM_POLY_INT_COEFFS; ix++)
   10707           30 :                 nunits.coeffs[ix] = wu ();
   10708           30 :               if (!get_overrun ())
   10709           30 :                 res = build_vector_type (res, nunits);
   10710              :             }
   10711              :             break;
   10712              : 
   10713           26 :           case META_TYPE:
   10714           26 :             if (!get_overrun ())
   10715           26 :               res = meta_info_type_node;
   10716              :             break;
   10717              : 
   10718            2 :           case SPLICE_SCOPE:
   10719            2 :             {
   10720            2 :               bool type = u ();
   10721            2 :               tree expr = tree_node ();
   10722              : 
   10723            2 :               if (!get_overrun ())
   10724            2 :                 res = make_splice_scope (expr, type);
   10725              :             }
   10726              :             break;
   10727              :           }
   10728              : 
   10729              :         /* In the exporting TU, a derived type with attributes was built by
   10730              :            build_type_attribute_variant as a distinct copy, with itself as
   10731              :            TYPE_MAIN_VARIANT.  We repeat that on import to get the version
   10732              :            without attributes as TYPE_CANONICAL.  */
   10733      1366708 :         if (tree attribs = tree_node ())
   10734        15603 :           res = cp_build_type_attribute_variant (res, attribs);
   10735              : 
   10736      1366708 :         int tag = i ();
   10737      1366708 :         if (!tag)
   10738              :           {
   10739      1171073 :             tag = insert (res);
   10740      1171073 :             if (res)
   10741      1171073 :               dump (dumper::TREE)
   10742          678 :                 && dump ("Created:%d derived type %C", tag, code);
   10743              :           }
   10744              :         else
   10745       195635 :           res = back_ref (tag);
   10746              :       }
   10747              :       break;
   10748              : 
   10749       397718 :     case tt_variant_type:
   10750              :       /* Variant of some type.  */
   10751       397718 :       {
   10752       397718 :         res = tree_node ();
   10753       397718 :         int flags = i ();
   10754       397718 :         if (get_overrun ())
   10755              :           ;
   10756       397718 :         else if (flags < 0)
   10757              :           /* No change.  */;
   10758       191275 :         else if (TREE_CODE (res) == FUNCTION_TYPE
   10759       191275 :                  || TREE_CODE (res) == METHOD_TYPE)
   10760              :           {
   10761       189732 :             cp_ref_qualifier rqual = cp_ref_qualifier (flags & 3);
   10762       189732 :             bool late = (flags >> 2) & 1;
   10763       189732 :             cp_cv_quals quals = cp_cv_quals (flags >> 3);
   10764              : 
   10765       189732 :             tree raises = tree_node ();
   10766       189732 :             if (raises == error_mark_node)
   10767         6872 :               raises = TYPE_RAISES_EXCEPTIONS (res);
   10768              : 
   10769       189732 :             res = build_cp_fntype_variant (res, rqual, raises, late);
   10770       189732 :             if (TREE_CODE (res) == FUNCTION_TYPE)
   10771        68148 :               res = apply_memfn_quals (res, quals, rqual);
   10772              :           }
   10773              :         else
   10774              :           {
   10775         1543 :             res = build_aligned_type (res, (1u << flags) >> 1);
   10776         1543 :             TYPE_USER_ALIGN (res) = true;
   10777              :           }
   10778              : 
   10779       397718 :         int quals = i ();
   10780       397718 :         if (quals >= 0 && !get_overrun ())
   10781       207592 :           res = cp_build_qualified_type (res, quals);
   10782              : 
   10783       397718 :         int tag = i ();
   10784       397718 :         if (!tag)
   10785              :           {
   10786       397718 :             tag = insert (res);
   10787       397718 :             if (res)
   10788       397718 :               dump (dumper::TREE)
   10789          292 :                 && dump ("Created:%d variant type %C", tag, TREE_CODE (res));
   10790              :           }
   10791              :         else
   10792            0 :           res = back_ref (tag);
   10793              :       }
   10794              :       break;
   10795              : 
   10796         4768 :     case tt_tinfo_var:
   10797         4768 :     case tt_tinfo_typedef:
   10798              :       /* A tinfo var or typedef.  */
   10799         4768 :       {
   10800         4768 :         bool is_var = tag == tt_tinfo_var;
   10801         4768 :         unsigned ix = u ();
   10802         4768 :         tree type = NULL_TREE;
   10803              : 
   10804         4768 :         if (is_var)
   10805              :           {
   10806         2874 :             tree name = tree_node ();
   10807         2874 :             type = tree_node ();
   10808              : 
   10809         2874 :             if (!get_overrun ())
   10810         2874 :               res = get_tinfo_decl_direct (type, name, int (ix));
   10811              :           }
   10812              :         else
   10813              :           {
   10814         1894 :             if (!get_overrun ())
   10815              :               {
   10816         1894 :                 type = get_pseudo_tinfo_type (ix);
   10817         1894 :                 res = TYPE_NAME (type);
   10818              :               }
   10819              :           }
   10820         4768 :         if (res)
   10821              :           {
   10822         4768 :             int tag = insert (res);
   10823         4768 :             dump (dumper::TREE)
   10824           36 :               && dump ("Created tinfo_%s:%d %S:%u for %N",
   10825              :                        is_var ? "var" : "decl", tag, res, ix, type);
   10826         4768 :             if (!is_var)
   10827              :               {
   10828         1894 :                 tag = insert (type);
   10829         1894 :                 dump (dumper::TREE)
   10830           12 :                   && dump ("Created tinfo_type:%d %u %N", tag, ix, type);
   10831              :               }
   10832              :           }
   10833              :       }
   10834              :       break;
   10835              : 
   10836         1055 :     case tt_ptrmem_type:
   10837              :       /* A pointer to member function.  */
   10838         1055 :       {
   10839         1055 :         tree type = tree_node ();
   10840         1055 :         if (type && TREE_CODE (type) == POINTER_TYPE
   10841         2110 :             && TREE_CODE (TREE_TYPE (type)) == METHOD_TYPE)
   10842              :           {
   10843         1055 :             res = build_ptrmemfunc_type (type);
   10844         1055 :             int tag = insert (res);
   10845         1058 :             dump (dumper::TREE) && dump ("Created:%d ptrmem type", tag);
   10846              :           }
   10847              :         else
   10848            0 :           set_overrun ();
   10849              :       }
   10850              :       break;
   10851              : 
   10852            6 :     case tt_nttp_var:
   10853              :       /* An NTTP object. */
   10854            6 :       {
   10855            6 :         tree init = tree_node ();
   10856            6 :         tree name = tree_node ();
   10857            6 :         if (!get_overrun ())
   10858              :           {
   10859              :             /* We don't want to check the initializer as that may require
   10860              :                name lookup, which could recursively start lazy loading.
   10861              :                Instead we know that INIT is already valid so we can just
   10862              :                apply that directly.  */
   10863            6 :             res = get_template_parm_object (init, name, /*check_init=*/false);
   10864            6 :             int tag = insert (res);
   10865            6 :             dump (dumper::TREE)
   10866            0 :               && dump ("Created nttp object:%d %N", tag, name);
   10867              :           }
   10868              :       }
   10869              :       break;
   10870              : 
   10871         7228 :     case tt_enum_value:
   10872              :       /* An enum const value.  */
   10873         7228 :       {
   10874         7228 :         if (tree decl = tree_node ())
   10875              :           {
   10876         7246 :             dump (dumper::TREE) && dump ("Read enum value %N", decl);
   10877         7228 :             res = DECL_INITIAL (decl);
   10878              :           }
   10879              : 
   10880         7228 :         if (!res)
   10881            0 :           set_overrun ();
   10882              :       }
   10883              :       break;
   10884              : 
   10885        13564 :     case tt_enum_decl:
   10886              :       /* An enum decl.  */
   10887        13564 :       {
   10888        13564 :         tree ctx = tree_node ();
   10889        13564 :         tree name = tree_node ();
   10890              : 
   10891        13564 :         if (!get_overrun ()
   10892        13564 :             && TREE_CODE (ctx) == ENUMERAL_TYPE)
   10893        13564 :           res = find_enum_member (ctx, name);
   10894              : 
   10895        13564 :         if (!res)
   10896            0 :           set_overrun ();
   10897              :         else
   10898              :           {
   10899        13564 :             int tag = insert (res);
   10900        13564 :             dump (dumper::TREE)
   10901           18 :               && dump ("Read enum decl:%d %C:%N", tag, TREE_CODE (res), res);
   10902              :           }
   10903              :       }
   10904              :       break;
   10905              : 
   10906         7587 :     case tt_data_member:
   10907              :       /* A data member.  */
   10908         7587 :       {
   10909         7587 :         tree ctx = tree_node ();
   10910         7587 :         tree name = tree_node ();
   10911              : 
   10912         7587 :         if (!get_overrun ()
   10913         7587 :             && RECORD_OR_UNION_TYPE_P (ctx))
   10914              :           {
   10915         7587 :             if (name)
   10916         6629 :               res = lookup_class_binding (ctx, name);
   10917              :             else
   10918          958 :               res = lookup_field_ident (ctx, u ());
   10919              : 
   10920         7587 :             if (!res
   10921         7587 :                 || (TREE_CODE (res) != FIELD_DECL
   10922         7587 :                     && TREE_CODE (res) != USING_DECL)
   10923        15174 :                 || DECL_CONTEXT (res) != ctx)
   10924              :               res = NULL_TREE;
   10925              :           }
   10926              : 
   10927         7587 :         if (!res)
   10928            0 :           set_overrun ();
   10929              :         else
   10930              :           {
   10931         7587 :             int tag = insert (res);
   10932         7587 :             dump (dumper::TREE)
   10933           26 :               && dump ("Read member:%d %C:%N", tag, TREE_CODE (res), res);
   10934              :           }
   10935              :       }
   10936              :       break;
   10937              : 
   10938         1661 :     case tt_binfo:
   10939              :       /* A BINFO.  Walk the tree of the dominating type.  */
   10940         1661 :       {
   10941         1661 :         tree type;
   10942         1661 :         unsigned ix = binfo_mergeable (&type);
   10943         1661 :         if (type)
   10944              :           {
   10945         1661 :             res = TYPE_BINFO (type);
   10946         1747 :             for (; ix && res; res = TREE_CHAIN (res))
   10947           86 :               ix--;
   10948         1661 :             if (!res)
   10949            0 :               set_overrun ();
   10950              :           }
   10951              : 
   10952         1661 :         if (get_overrun ())
   10953              :           break;
   10954              : 
   10955              :         /* Insert binfo into backreferences.  */
   10956         1661 :         tag = insert (res);
   10957         1661 :         dump (dumper::TREE) && dump ("Read binfo:%d %N", tag, res);
   10958              :       }
   10959         1661 :       break;
   10960              : 
   10961           80 :     case tt_vtable:
   10962           80 :       {
   10963           80 :         unsigned ix = u ();
   10964           80 :         tree ctx = tree_node ();
   10965           80 :         dump (dumper::TREE) && dump ("Reading vtable %N[%u]", ctx, ix);
   10966           80 :         if (TREE_CODE (ctx) == RECORD_TYPE && TYPE_LANG_SPECIFIC (ctx))
   10967           92 :           for (res = CLASSTYPE_VTABLES (ctx); res; res = DECL_CHAIN (res))
   10968           92 :             if (!ix--)
   10969              :               break;
   10970           80 :         if (!res)
   10971            0 :           set_overrun ();
   10972              :       }
   10973              :       break;
   10974              : 
   10975            0 :     case tt_thunk:
   10976            0 :       {
   10977            0 :         int fixed = i ();
   10978            0 :         tree target = tree_node ();
   10979            0 :         tree virt = tree_node ();
   10980              : 
   10981            0 :         for (tree thunk = DECL_THUNKS (target);
   10982            0 :              thunk; thunk = DECL_CHAIN (thunk))
   10983            0 :           if (THUNK_FIXED_OFFSET (thunk) == fixed
   10984            0 :               && !THUNK_VIRTUAL_OFFSET (thunk) == !virt
   10985            0 :               && (!virt
   10986            0 :                   || tree_int_cst_equal (virt, THUNK_VIRTUAL_OFFSET (thunk))))
   10987              :             {
   10988              :               res = thunk;
   10989              :               break;
   10990              :             }
   10991              : 
   10992            0 :         int tag = insert (res);
   10993            0 :         if (res)
   10994            0 :           dump (dumper::TREE)
   10995            0 :             && dump ("Read:%d thunk %N to %N", tag, DECL_NAME (res), target);
   10996              :         else
   10997            0 :           set_overrun ();
   10998              :       }
   10999              :       break;
   11000              : 
   11001       141651 :     case tt_clone_ref:
   11002       141651 :       {
   11003       141651 :         tree target = tree_node ();
   11004       141651 :         tree name = tree_node ();
   11005              : 
   11006       141651 :         if (DECL_P (target) && DECL_MAYBE_IN_CHARGE_CDTOR_P (target))
   11007              :           {
   11008       141651 :             tree clone;
   11009       220246 :             FOR_EVERY_CLONE (clone, target)
   11010       220246 :               if (DECL_NAME (clone) == name)
   11011              :                 {
   11012              :                   res = clone;
   11013              :                   break;
   11014              :                 }
   11015              :           }
   11016              : 
   11017              :         /* A clone might have a different vtable entry.  */
   11018       141651 :         if (res && DECL_VIRTUAL_P (res))
   11019         8067 :           DECL_VINDEX (res) = tree_node ();
   11020              : 
   11021       141651 :         if (!res)
   11022            0 :           set_overrun ();
   11023       141651 :         int tag = insert (res);
   11024       141651 :         if (res)
   11025       141651 :           dump (dumper::TREE)
   11026          230 :             && dump ("Read:%d clone %N of %N", tag, DECL_NAME (res), target);
   11027              :         else
   11028            0 :           set_overrun ();
   11029              :        }
   11030              :       break;
   11031              : 
   11032      1046281 :     case tt_entity:
   11033              :       /* Index into the entity table.  Perhaps not loaded yet!  */
   11034      1046281 :       {
   11035      1046281 :         unsigned origin = state->slurp->remap_module (u ());
   11036      1046281 :         unsigned ident = u ();
   11037      1046281 :         module_state *from = (*modules)[origin];
   11038              : 
   11039      1046281 :         if (!origin || ident >= from->entity_num)
   11040            0 :           set_overrun ();
   11041      1046281 :         if (!get_overrun ())
   11042              :           {
   11043      1046281 :             binding_slot *slot = &(*entity_ary)[from->entity_lwm + ident];
   11044      1046281 :             if (slot->is_lazy ())
   11045        44317 :               if (!from->lazy_load (ident, slot))
   11046            0 :                 set_overrun ();
   11047      1046281 :             res = *slot;
   11048              :           }
   11049              : 
   11050      1046281 :         if (res)
   11051              :           {
   11052      1046281 :             const char *kind = (origin != state->mod ? "Imported" : "Named");
   11053      1046281 :             int tag = insert (res);
   11054      1046281 :             dump (dumper::TREE)
   11055          605 :               && dump ("%s:%d %C:%N@%M", kind, tag, TREE_CODE (res),
   11056          605 :                        res, (*modules)[origin]);
   11057              : 
   11058      1046281 :             if (!add_indirects (res))
   11059              :               {
   11060            0 :                 set_overrun ();
   11061            0 :                 res = NULL_TREE;
   11062              :               }
   11063              :           }
   11064              :       }
   11065              :       break;
   11066              : 
   11067         2846 :     case tt_template:
   11068              :       /* A template.  */
   11069         2846 :       if (tree tpl = tree_node ())
   11070              :         {
   11071         2846 :           res = (TREE_CODE (tpl) == TU_LOCAL_ENTITY ?
   11072         2846 :                  tpl : DECL_TEMPLATE_RESULT (tpl));
   11073         2846 :           dump (dumper::TREE)
   11074            9 :             && dump ("Read template %C:%N", TREE_CODE (res), res);
   11075              :         }
   11076              :       break;
   11077              :     }
   11078              : 
   11079     88884117 :   if (is_use && !unused && res && DECL_P (res) && !TREE_USED (res))
   11080              :     {
   11081              :       /* Mark decl used as mark_used does -- we cannot call
   11082              :          mark_used in the middle of streaming, we only need a subset
   11083              :          of its functionality.   */
   11084       665766 :       TREE_USED (res) = true;
   11085              : 
   11086              :       /* And for structured bindings also the underlying decl.  */
   11087       665766 :       if (DECL_DECOMPOSITION_P (res) && !DECL_DECOMP_IS_BASE (res))
   11088         1304 :         TREE_USED (DECL_DECOMP_BASE (res)) = true;
   11089              : 
   11090       665766 :       if (DECL_CLONED_FUNCTION_P (res))
   11091         6487 :         TREE_USED (DECL_CLONED_FUNCTION (res)) = true;
   11092              :     }
   11093              : 
   11094     88884117 :   dump.outdent ();
   11095     88884117 :   return res;
   11096              : }
   11097              : 
   11098              : void
   11099      1522496 : trees_out::tpl_parms (tree parms, unsigned &tpl_levels)
   11100              : {
   11101      1522496 :   if (!parms)
   11102              :     return;
   11103              : 
   11104      1021791 :   if (TREE_VISITED (parms))
   11105              :     {
   11106       421852 :       ref_node (parms);
   11107       421852 :       return;
   11108              :     }
   11109              : 
   11110       599939 :   tpl_parms (TREE_CHAIN (parms), tpl_levels);
   11111              : 
   11112       599939 :   tree vec = TREE_VALUE (parms);
   11113       599939 :   unsigned len = TREE_VEC_LENGTH (vec);
   11114              :   /* Depth.  */
   11115       599939 :   int tag = insert (parms);
   11116       599939 :   if (streaming_p ())
   11117              :     {
   11118       161510 :       i (len + 1);
   11119       161576 :       dump (dumper::TREE)
   11120           66 :         && dump ("Writing template parms:%d level:%N length:%d",
   11121           66 :                  tag, TREE_PURPOSE (parms), len);
   11122              :     }
   11123       599939 :   tree_node (TREE_PURPOSE (parms));
   11124              : 
   11125      1641033 :   for (unsigned ix = 0; ix != len; ix++)
   11126              :     {
   11127      1041094 :       tree parm = TREE_VEC_ELT (vec, ix);
   11128      1041094 :       tree decl = TREE_VALUE (parm);
   11129              : 
   11130      1041094 :       gcc_checking_assert (DECL_TEMPLATE_PARM_P (decl));
   11131      1041094 :       if (CHECKING_P)
   11132      1041094 :         switch (TREE_CODE (decl))
   11133              :           {
   11134            0 :           default: gcc_unreachable ();
   11135              : 
   11136         3171 :           case TEMPLATE_DECL:
   11137         3171 :             gcc_assert ((TREE_CODE (TREE_TYPE (decl)) == TEMPLATE_TEMPLATE_PARM)
   11138              :                         && (TREE_CODE (DECL_TEMPLATE_RESULT (decl)) == TYPE_DECL)
   11139              :                         && (TYPE_NAME (TREE_TYPE (decl)) == decl));
   11140              :             break;
   11141              : 
   11142       973322 :           case TYPE_DECL:
   11143       973322 :             gcc_assert ((TREE_CODE (TREE_TYPE (decl)) == TEMPLATE_TYPE_PARM)
   11144              :                         && (TYPE_NAME (TREE_TYPE (decl)) == decl));
   11145              :             break;
   11146              : 
   11147        64601 :           case PARM_DECL:
   11148        64601 :             gcc_assert ((TREE_CODE (DECL_INITIAL (decl)) == TEMPLATE_PARM_INDEX)
   11149              :                         && (TREE_CODE (TEMPLATE_PARM_DECL (DECL_INITIAL (decl)))
   11150              :                             == CONST_DECL)
   11151              :                         && (DECL_TEMPLATE_PARM_P
   11152              :                             (TEMPLATE_PARM_DECL (DECL_INITIAL (decl)))));
   11153              :             break;
   11154              :           }
   11155              : 
   11156      1041094 :       tree_node (decl);
   11157      1041094 :       tree_node (TEMPLATE_PARM_CONSTRAINTS (parm));
   11158              :     }
   11159              : 
   11160       599939 :   tpl_levels++;
   11161              : }
   11162              : 
   11163              : tree
   11164       300682 : trees_in::tpl_parms (unsigned &tpl_levels)
   11165              : {
   11166       300682 :   tree parms = NULL_TREE;
   11167              : 
   11168       634306 :   while (int len = i ())
   11169              :     {
   11170       333624 :       if (len < 0)
   11171              :         {
   11172       180062 :           parms = back_ref (len);
   11173       180062 :           continue;
   11174              :         }
   11175              : 
   11176       153562 :       len -= 1;
   11177       153562 :       parms = tree_cons (NULL_TREE, NULL_TREE, parms);
   11178       153562 :       int tag = insert (parms);
   11179       153562 :       TREE_PURPOSE (parms) = tree_node ();
   11180              : 
   11181       153562 :       dump (dumper::TREE)
   11182          105 :         && dump ("Reading template parms:%d level:%N length:%d",
   11183          105 :                  tag, TREE_PURPOSE (parms), len);
   11184              : 
   11185       153562 :       tree vec = make_tree_vec (len);
   11186       404544 :       for (int ix = 0; ix != len; ix++)
   11187              :         {
   11188       250982 :           tree decl = tree_node ();
   11189       250982 :           if (!decl)
   11190              :             return NULL_TREE;
   11191              : 
   11192       250982 :           tree parm = build_tree_list (NULL, decl);
   11193       250982 :           TEMPLATE_PARM_CONSTRAINTS (parm) = tree_node ();
   11194              : 
   11195       250982 :           TREE_VEC_ELT (vec, ix) = parm;
   11196              :         }
   11197              : 
   11198       153562 :       TREE_VALUE (parms) = vec;
   11199       153562 :       tpl_levels++;
   11200              :     }
   11201              : 
   11202              :   return parms;
   11203              : }
   11204              : 
   11205              : void
   11206       922557 : trees_out::tpl_parms_fini (tree tmpl, unsigned tpl_levels)
   11207              : {
   11208       922557 :   for (tree parms = DECL_TEMPLATE_PARMS (tmpl);
   11209      1522496 :        tpl_levels--; parms = TREE_CHAIN (parms))
   11210              :     {
   11211       599939 :       tree vec = TREE_VALUE (parms);
   11212              : 
   11213       599939 :       tree_node (TREE_TYPE (vec));
   11214      1641033 :       for (unsigned ix = TREE_VEC_LENGTH (vec); ix--;)
   11215              :         {
   11216      1041094 :           tree parm = TREE_VEC_ELT (vec, ix);
   11217      1041094 :           tree dflt = TREE_PURPOSE (parm);
   11218      1041094 :           tree_node (dflt);
   11219              : 
   11220              :           /* Template template parameters need a context of their owning
   11221              :              template. This is quite tricky to infer correctly on stream-in
   11222              :              (see PR c++/98881) so we'll just provide it directly.  */
   11223      1041094 :           tree decl = TREE_VALUE (parm);
   11224      1041094 :           if (TREE_CODE (decl) == TEMPLATE_DECL)
   11225         3171 :             tree_node (DECL_CONTEXT (decl));
   11226              :         }
   11227              :     }
   11228       922557 : }
   11229              : 
   11230              : bool
   11231       300682 : trees_in::tpl_parms_fini (tree tmpl, unsigned tpl_levels)
   11232              : {
   11233       300682 :   for (tree parms = DECL_TEMPLATE_PARMS (tmpl);
   11234       454244 :        tpl_levels--; parms = TREE_CHAIN (parms))
   11235              :     {
   11236       153562 :       tree vec = TREE_VALUE (parms);
   11237              : 
   11238       153562 :       TREE_TYPE (vec) = tree_node ();
   11239       404544 :       for (unsigned ix = TREE_VEC_LENGTH (vec); ix--;)
   11240              :         {
   11241       250982 :           tree parm = TREE_VEC_ELT (vec, ix);
   11242       250982 :           tree dflt = tree_node ();
   11243       250982 :           TREE_PURPOSE (parm) = dflt;
   11244              : 
   11245       250982 :           tree decl = TREE_VALUE (parm);
   11246       250982 :           if (TREE_CODE (decl) == TEMPLATE_DECL)
   11247          806 :             DECL_CONTEXT (decl) = tree_node ();
   11248              : 
   11249       250982 :           if (get_overrun ())
   11250              :             return false;
   11251              :         }
   11252              :     }
   11253              :   return true;
   11254              : }
   11255              : 
   11256              : /* PARMS is a LIST, one node per level.
   11257              :    TREE_VALUE is a TREE_VEC of parm info for that level.
   11258              :    each ELT is a TREE_LIST
   11259              :    TREE_VALUE is PARM_DECL, TYPE_DECL or TEMPLATE_DECL
   11260              :    TREE_PURPOSE is the default value.  */
   11261              : 
   11262              : void
   11263       922557 : trees_out::tpl_header (tree tpl, unsigned *tpl_levels)
   11264              : {
   11265       922557 :   tree parms = DECL_TEMPLATE_PARMS (tpl);
   11266       922557 :   tpl_parms (parms, *tpl_levels);
   11267              : 
   11268              :   /* Mark end.  */
   11269       922557 :   if (streaming_p ())
   11270       307109 :     u (0);
   11271              : 
   11272       922557 :   if (*tpl_levels)
   11273       564532 :     tree_node (TEMPLATE_PARMS_CONSTRAINTS (parms));
   11274       922557 : }
   11275              : 
   11276              : bool
   11277       300682 : trees_in::tpl_header (tree tpl, unsigned *tpl_levels)
   11278              : {
   11279       300682 :   tree parms = tpl_parms (*tpl_levels);
   11280       300682 :   if (!parms)
   11281              :     return false;
   11282              : 
   11283       300682 :   DECL_TEMPLATE_PARMS (tpl) = parms;
   11284              : 
   11285       300682 :   if (*tpl_levels)
   11286       151570 :     TEMPLATE_PARMS_CONSTRAINTS (parms) = tree_node ();
   11287              : 
   11288              :   return true;
   11289              : }
   11290              : 
   11291              : /* Stream skeleton parm nodes, with their flags, type & parm indices.
   11292              :    All the parms will have consecutive tags.  */
   11293              : 
   11294              : void
   11295      1273006 : trees_out::fn_parms_init (tree fn)
   11296              : {
   11297              :   /* First init them.  */
   11298      1273006 :   int base_tag = ref_num - 1;
   11299      1273006 :   int ix = 0;
   11300      1273006 :   for (tree parm = DECL_ARGUMENTS (fn);
   11301      3838019 :        parm; parm = DECL_CHAIN (parm), ix++)
   11302              :     {
   11303      2565013 :       if (streaming_p ())
   11304              :         {
   11305       854959 :           start (parm);
   11306       854959 :           tree_node_bools (parm);
   11307              :         }
   11308      2565013 :       int tag = insert (parm);
   11309      2565013 :       gcc_checking_assert (base_tag - ix == tag);
   11310              :     }
   11311              :   /* Mark the end.  */
   11312      1273006 :   if (streaming_p ())
   11313       424500 :     u (0);
   11314              : 
   11315              :   /* Now stream their contents.  */
   11316      1273006 :   ix = 0;
   11317      1273006 :   for (tree parm = DECL_ARGUMENTS (fn);
   11318      3838019 :        parm; parm = DECL_CHAIN (parm), ix++)
   11319              :     {
   11320      2565013 :       if (streaming_p ())
   11321       854959 :         dump (dumper::TREE)
   11322          222 :           && dump ("Writing parm:%d %u (%N) of %N",
   11323              :                    base_tag - ix, ix, parm, fn);
   11324      2565013 :       tree_node_vals (parm);
   11325              :     }
   11326              : 
   11327      1273006 :   if (!streaming_p ())
   11328              :     {
   11329              :       /* We must walk contract specifiers so the dependency graph is
   11330              :          complete.  */
   11331       848506 :       tree contract = get_fn_contract_specifiers (fn);
   11332      1697012 :       for (; contract; contract = TREE_CHAIN (contract))
   11333            0 :         tree_node (contract);
   11334              :     }
   11335              : 
   11336              :   /* Write a reference to contracts pre/post functions, if any, to avoid
   11337              :      regenerating them in importers.  */
   11338      1273006 :   tree_node (DECL_PRE_FN (fn));
   11339      1273006 :   tree_node (DECL_POST_FN (fn));
   11340      1273006 : }
   11341              : 
   11342              : /* Build skeleton parm nodes, read their flags, type & parm indices.  */
   11343              : 
   11344              : int
   11345       425412 : trees_in::fn_parms_init (tree fn)
   11346              : {
   11347       425412 :   int base_tag = ~(int)back_refs.length ();
   11348              : 
   11349       425412 :   tree *parm_ptr = &DECL_ARGUMENTS (fn);
   11350       425412 :   int ix = 0;
   11351      1282605 :   for (; int code = u (); ix++)
   11352              :     {
   11353       857193 :       tree parm = start (code);
   11354       857193 :       if (!tree_node_bools (parm))
   11355              :         return 0;
   11356              : 
   11357       857193 :       int tag = insert (parm);
   11358       857193 :       gcc_checking_assert (base_tag - ix == tag);
   11359       857193 :       *parm_ptr = parm;
   11360       857193 :       parm_ptr = &DECL_CHAIN (parm);
   11361       857193 :     }
   11362              : 
   11363       425412 :   ix = 0;
   11364       425412 :   for (tree parm = DECL_ARGUMENTS (fn);
   11365      1282605 :        parm; parm = DECL_CHAIN (parm), ix++)
   11366              :     {
   11367       857193 :       dump (dumper::TREE)
   11368          362 :         && dump ("Reading parm:%d %u (%N) of %N",
   11369              :                  base_tag - ix, ix, parm, fn);
   11370       857193 :       if (!tree_node_vals (parm))
   11371              :         return 0;
   11372              : 
   11373              :       /* Apply relevant attributes.
   11374              :          FIXME should probably use cplus_decl_attributes for this,
   11375              :          but it's not yet ready for modules.  */
   11376              : 
   11377              :       /* TREE_USED is deliberately not streamed for most declarations,
   11378              :          but needs to be set if we have the [[maybe_unused]] attribute.  */
   11379       857193 :       if (lookup_attribute ("unused", DECL_ATTRIBUTES (parm))
   11380       857193 :           || lookup_attribute ("maybe_unused", DECL_ATTRIBUTES (parm)))
   11381              :         {
   11382         1921 :           TREE_USED (parm) = true;
   11383         1921 :           DECL_READ_P (parm) = true;
   11384              :         }
   11385              :     }
   11386              : 
   11387              :   /* Reload references to contract functions, if any.  */
   11388       425412 :   tree pre_fn = tree_node ();
   11389       425412 :   tree post_fn = tree_node ();
   11390       425412 :   set_contract_functions (fn, pre_fn, post_fn);
   11391              : 
   11392       425412 :   return base_tag;
   11393              : }
   11394              : 
   11395              : /* Read the remaining parm node data.  Replace with existing (if
   11396              :    non-null) in the map.  */
   11397              : 
   11398              : void
   11399       425412 : trees_in::fn_parms_fini (int tag, tree fn, tree existing, bool is_defn)
   11400              : {
   11401       623376 :   tree existing_parm = existing ? DECL_ARGUMENTS (existing) : NULL_TREE;
   11402       425412 :   tree parms = DECL_ARGUMENTS (fn);
   11403      1282605 :   for (tree parm = parms; parm; parm = DECL_CHAIN (parm))
   11404              :     {
   11405       857193 :       if (existing_parm)
   11406              :         {
   11407       581310 :           if (is_defn && !DECL_SAVED_TREE (existing))
   11408              :             {
   11409              :               /* If we're about to become the definition, set the
   11410              :                  names of the parms from us.  */
   11411        14952 :               DECL_NAME (existing_parm) = DECL_NAME (parm);
   11412        14952 :               DECL_SOURCE_LOCATION (existing_parm) = DECL_SOURCE_LOCATION (parm);
   11413              : 
   11414              :               /* And some other flags important for codegen are only set
   11415              :                  by the definition.  */
   11416        14952 :               TREE_ADDRESSABLE (existing_parm) = TREE_ADDRESSABLE (parm);
   11417        14952 :               DECL_BY_REFERENCE (existing_parm) = DECL_BY_REFERENCE (parm);
   11418        14952 :               DECL_NONLOCAL (existing_parm) = DECL_NONLOCAL (parm);
   11419        14952 :               DECL_ARG_TYPE (existing_parm) = DECL_ARG_TYPE (parm);
   11420              : 
   11421              :               /* Invisiref parms had their types adjusted by cp_genericize. */
   11422        14952 :               if (DECL_BY_REFERENCE (parm))
   11423              :                 {
   11424            6 :                   TREE_TYPE (existing_parm) = TREE_TYPE (parm);
   11425            6 :                   relayout_decl (existing_parm);
   11426              :                 }
   11427              :             }
   11428              : 
   11429       394049 :           back_refs[~tag] = existing_parm;
   11430       394049 :           existing_parm = DECL_CHAIN (existing_parm);
   11431              :         }
   11432       857193 :       tag--;
   11433              :     }
   11434       425412 : }
   11435              : 
   11436              : /* Encode into KEY the position of the local type (class or enum)
   11437              :    declaration DECL within FN.  The position is encoded as the
   11438              :    index of the innermost BLOCK (numbered in BFS order) along with
   11439              :    the index within its BLOCK_VARS list.  */
   11440              : 
   11441              : void
   11442        11853 : trees_out::key_local_type (merge_key& key, tree decl, tree fn)
   11443              : {
   11444        11853 :   auto_vec<tree, 4> blocks;
   11445        11853 :   blocks.quick_push (DECL_INITIAL (fn));
   11446        11853 :   unsigned block_ix = 0;
   11447        55323 :   while (block_ix != blocks.length ())
   11448              :     {
   11449        21735 :       tree block = blocks[block_ix];
   11450        21735 :       unsigned decl_ix = 0;
   11451        62472 :       for (tree var = BLOCK_VARS (block); var; var = DECL_CHAIN (var))
   11452              :         {
   11453        52590 :           if (TREE_CODE (var) != TYPE_DECL)
   11454        31821 :             continue;
   11455        20769 :           if (var == decl)
   11456              :             {
   11457        11853 :               key.index = (block_ix << 10) | decl_ix;
   11458        11853 :               return;
   11459              :             }
   11460         8916 :           ++decl_ix;
   11461              :         }
   11462        20565 :       for (tree sub = BLOCK_SUBBLOCKS (block); sub; sub = BLOCK_CHAIN (sub))
   11463        10683 :         blocks.safe_push (sub);
   11464         9882 :       ++block_ix;
   11465              :     }
   11466              : 
   11467              :   /* Not-found value.  */
   11468            0 :   key.index = 1023;
   11469        11853 : }
   11470              : 
   11471              : /* Look up the local type corresponding at the position encoded by
   11472              :    KEY within FN and named NAME.  */
   11473              : 
   11474              : tree
   11475         3892 : trees_in::key_local_type (const merge_key& key, tree fn, tree name)
   11476              : {
   11477         3892 :   if (!DECL_INITIAL (fn))
   11478              :     return NULL_TREE;
   11479              : 
   11480         1892 :   const unsigned block_pos = key.index >> 10;
   11481         1892 :   const unsigned decl_pos = key.index & 1023;
   11482              : 
   11483         1892 :   if (decl_pos == 1023)
   11484              :     return NULL_TREE;
   11485              : 
   11486         1892 :   auto_vec<tree, 4> blocks;
   11487         1892 :   blocks.quick_push (DECL_INITIAL (fn));
   11488         1892 :   unsigned block_ix = 0;
   11489         8742 :   while (block_ix != blocks.length ())
   11490              :     {
   11491         3425 :       tree block = blocks[block_ix];
   11492         3425 :       if (block_ix == block_pos)
   11493              :         {
   11494         1892 :           unsigned decl_ix = 0;
   11495         5138 :           for (tree var = BLOCK_VARS (block); var; var = DECL_CHAIN (var))
   11496              :             {
   11497         5138 :               if (TREE_CODE (var) != TYPE_DECL)
   11498         2268 :                 continue;
   11499              :               /* Prefer using the identifier as the key for more robustness
   11500              :                  to ODR violations, except for anonymous types since their
   11501              :                  compiler-generated identifiers aren't stable.  */
   11502         5740 :               if (IDENTIFIER_ANON_P (name)
   11503         2870 :                   ? decl_ix == decl_pos
   11504          323 :                   : DECL_NAME (var) == name)
   11505              :                 return var;
   11506          978 :               ++decl_ix;
   11507              :             }
   11508              :           return NULL_TREE;
   11509              :         }
   11510         3175 :       for (tree sub = BLOCK_SUBBLOCKS (block); sub; sub = BLOCK_CHAIN (sub))
   11511         1642 :         blocks.safe_push (sub);
   11512         1533 :       ++block_ix;
   11513              :     }
   11514              : 
   11515              :   return NULL_TREE;
   11516         1892 : }
   11517              : 
   11518              : /* DEP is the depset of some decl we're streaming by value.  Determine
   11519              :    the merging behaviour.  */
   11520              : 
   11521              : merge_kind
   11522      3197366 : trees_out::get_merge_kind (tree decl, depset *dep)
   11523              : {
   11524      3197366 :   if (!dep)
   11525              :     {
   11526       609138 :       if (VAR_OR_FUNCTION_DECL_P (decl))
   11527              :         {
   11528              :           /* Any var or function with template info should have DEP.  */
   11529       324668 :           gcc_checking_assert (!DECL_LANG_SPECIFIC (decl)
   11530              :                                || !DECL_TEMPLATE_INFO (decl));
   11531       324668 :           if (DECL_LOCAL_DECL_P (decl))
   11532              :             return MK_unique;
   11533              :         }
   11534              : 
   11535              :       /* Either unique, or some member of a class that cannot have an
   11536              :          out-of-class definition.  For instance a FIELD_DECL.  */
   11537       608880 :       tree ctx = CP_DECL_CONTEXT (decl);
   11538       608880 :       if (TREE_CODE (ctx) == FUNCTION_DECL)
   11539              :         {
   11540              :           /* USING_DECLs and NAMESPACE_DECLs cannot have DECL_TEMPLATE_INFO --
   11541              :              this isn't permitting them to have one.   */
   11542       368696 :           gcc_checking_assert (TREE_CODE (decl) == USING_DECL
   11543              :                                || TREE_CODE (decl) == NAMESPACE_DECL
   11544              :                                || !DECL_LANG_SPECIFIC (decl)
   11545              :                                || !DECL_TEMPLATE_INFO (decl));
   11546              : 
   11547              :           return MK_unique;
   11548              :         }
   11549              : 
   11550       240184 :       if (TREE_CODE (decl) == TEMPLATE_DECL
   11551       240184 :           && DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (decl))
   11552              :         return MK_local_friend;
   11553              : 
   11554       240184 :       gcc_checking_assert (TYPE_P (ctx));
   11555              : 
   11556              :       /* Internal-only types will not need to dedup their members.  */
   11557       240184 :       if (!DECL_CONTEXT (TYPE_NAME (ctx)))
   11558              :         return MK_unique;
   11559              : 
   11560       240128 :       if (TREE_CODE (decl) == USING_DECL)
   11561              :         return MK_field;
   11562              : 
   11563       155847 :       if (TREE_CODE (decl) == FIELD_DECL)
   11564              :         {
   11565       109089 :           if (DECL_NAME (decl))
   11566              :             {
   11567              :               /* Anonymous FIELD_DECLs have a NULL name.  */
   11568        86352 :               gcc_checking_assert (!IDENTIFIER_ANON_P (DECL_NAME (decl)));
   11569              :               return MK_named;
   11570              :             }
   11571              : 
   11572        22737 :           if (walking_bit_field_unit)
   11573              :             {
   11574              :               /* The underlying storage unit for a bitfield.  We do not
   11575              :                  need to dedup it, because it's only reachable through
   11576              :                  the bitfields it represents.  And those are deduped.  */
   11577              :               // FIXME: Is that assertion correct -- do we ever fish it
   11578              :               // out and put it in an expr?
   11579          394 :               gcc_checking_assert (!DECL_NAME (decl)
   11580              :                                    && !RECORD_OR_UNION_TYPE_P (TREE_TYPE (decl))
   11581              :                                    && !DECL_BIT_FIELD_REPRESENTATIVE (decl));
   11582          394 :               gcc_checking_assert ((TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
   11583              :                                     ? TREE_CODE (TREE_TYPE (TREE_TYPE (decl)))
   11584              :                                     : TREE_CODE (TREE_TYPE (decl)))
   11585              :                                    == INTEGER_TYPE);
   11586              :               return MK_unique;
   11587              :             }
   11588              : 
   11589              :           return MK_field;
   11590              :         }
   11591              : 
   11592        46758 :       if (TREE_CODE (decl) == CONST_DECL)
   11593              :         return MK_named;
   11594              : 
   11595         7294 :       if (TREE_CODE (decl) == VAR_DECL
   11596         7294 :           && DECL_VTABLE_OR_VTT_P (decl))
   11597              :         return MK_vtable;
   11598              : 
   11599         1176 :       if (DECL_THUNK_P (decl))
   11600              :         /* Thunks are unique-enough, because they're only referenced
   11601              :            from the vtable.  And that's either new (so we want the
   11602              :            thunks), or it's a duplicate (so it will be dropped).  */
   11603              :         return MK_unique;
   11604              : 
   11605              :       /* There should be no other cases.  */
   11606            0 :       gcc_unreachable ();
   11607              :     }
   11608              : 
   11609      2588228 :   gcc_checking_assert (TREE_CODE (decl) != FIELD_DECL
   11610              :                        && TREE_CODE (decl) != USING_DECL
   11611              :                        && TREE_CODE (decl) != CONST_DECL);
   11612              : 
   11613      2588228 :   if (is_key_order ())
   11614              :     {
   11615              :       /* When doing the mergeablilty graph, there's an indirection to
   11616              :          the actual depset.  */
   11617       862589 :       gcc_assert (dep->is_special ());
   11618       862589 :       dep = dep->deps[0];
   11619              :     }
   11620              : 
   11621      2588228 :   gcc_checking_assert (decl == dep->get_entity ());
   11622              : 
   11623      2588228 :   merge_kind mk = MK_named;
   11624      2588228 :   switch (dep->get_entity_kind ())
   11625              :     {
   11626            0 :     default:
   11627            0 :       gcc_unreachable ();
   11628              : 
   11629              :     case depset::EK_PARTIAL:
   11630              :       mk = MK_partial;
   11631              :       break;
   11632              : 
   11633      1503056 :     case depset::EK_DECL:
   11634      1503056 :       {
   11635      1503056 :         tree ctx = CP_DECL_CONTEXT (decl);
   11636              : 
   11637      1503056 :         switch (TREE_CODE (ctx))
   11638              :           {
   11639            0 :           default:
   11640            0 :             gcc_unreachable ();
   11641              : 
   11642        12117 :           case FUNCTION_DECL:
   11643        12117 :             gcc_checking_assert
   11644              :               (DECL_IMPLICIT_TYPEDEF_P (STRIP_TEMPLATE (decl)));
   11645              : 
   11646        12117 :             if (has_definition (ctx))
   11647              :               mk = MK_local_type;
   11648              :             else
   11649              :               /* We're not providing a definition of the context to key
   11650              :                  the local type into; use the keyed map instead.  */
   11651          880 :               mk = MK_keyed;
   11652              :             break;
   11653              : 
   11654      1490939 :           case RECORD_TYPE:
   11655      1490939 :           case UNION_TYPE:
   11656      1490939 :           case NAMESPACE_DECL:
   11657      1490939 :             if (DECL_NAME (decl) == as_base_identifier)
   11658              :               {
   11659              :                 mk = MK_as_base;
   11660              :                 break;
   11661              :               }
   11662              : 
   11663              :             /* A lambda may have a class as its context, even though it
   11664              :                isn't a member in the traditional sense; see the test
   11665              :                g++.dg/modules/lambda-6_a.C.  */
   11666      1874336 :             if (DECL_IMPLICIT_TYPEDEF_P (STRIP_TEMPLATE (decl))
   11667      1629980 :                 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
   11668              :               {
   11669          643 :                 if (get_keyed_decl_scope (decl))
   11670              :                   mk = MK_keyed;
   11671              :                 else
   11672              :                   /* Lambdas not attached to any mangling scope are TU-local
   11673              :                      and so cannot be deduplicated.  */
   11674       371084 :                   mk = MK_unique;
   11675              :                 break;
   11676              :               }
   11677              : 
   11678      1421614 :             if (TREE_CODE (decl) == TEMPLATE_DECL
   11679      1421614 :                 ? DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (decl)
   11680       637365 :                 : decl_specialization_friend_p (decl))
   11681              :               {
   11682              :                 mk = MK_local_friend;
   11683              :                 break;
   11684              :               }
   11685              : 
   11686      1404850 :             if (DECL_DECOMPOSITION_P (decl))
   11687              :               {
   11688              :                 mk = MK_unique;
   11689              :                 break;
   11690              :               }
   11691              : 
   11692      1404373 :             if (IDENTIFIER_ANON_P (DECL_NAME (decl)))
   11693              :               {
   11694        23003 :                 if (RECORD_OR_UNION_TYPE_P (ctx))
   11695              :                   mk = MK_field;
   11696         1123 :                 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
   11697         1123 :                          && UNSCOPED_ENUM_P (TREE_TYPE (decl))
   11698         2246 :                          && TYPE_VALUES (TREE_TYPE (decl)))
   11699              :                   /* Keyed by first enum value, and underlying type.  */
   11700              :                   mk = MK_enum;
   11701              :                 else
   11702              :                   /* No way to merge it, it is an ODR land-mine.  */
   11703              :                   mk = MK_unique;
   11704              :               }
   11705              :           }
   11706              :       }
   11707              :       break;
   11708              : 
   11709      1040403 :     case depset::EK_SPECIALIZATION:
   11710      1040403 :       {
   11711      1040403 :         gcc_checking_assert (dep->is_special ());
   11712              : 
   11713      1040403 :         if (TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
   11714              :           /* An block-scope classes of templates are themselves
   11715              :              templates.  */
   11716         4566 :           gcc_checking_assert (DECL_IMPLICIT_TYPEDEF_P (decl));
   11717              : 
   11718      1040403 :         if (dep->is_friend_spec ())
   11719              :           mk = MK_friend_spec;
   11720      1040403 :         else if (dep->is_type_spec ())
   11721              :           mk = MK_type_spec;
   11722              :         else
   11723       727641 :           mk = MK_decl_spec;
   11724              : 
   11725      1040403 :         if (TREE_CODE (decl) == TEMPLATE_DECL)
   11726              :           {
   11727        77499 :             spec_entry *entry = reinterpret_cast <spec_entry *> (dep->deps[0]);
   11728        77499 :             if (TREE_CODE (entry->spec) != TEMPLATE_DECL)
   11729         8073 :               mk = merge_kind (mk | MK_tmpl_tmpl_mask);
   11730              :           }
   11731              :       }
   11732              :       break;
   11733              :     }
   11734              : 
   11735              :   return mk;
   11736              : }
   11737              : 
   11738              : 
   11739              : /* The container of DECL -- not necessarily its context!  */
   11740              : 
   11741              : tree
   11742      3197366 : trees_out::decl_container (tree decl)
   11743              : {
   11744      3197366 :   int use_tpl;
   11745      3197366 :   tree tpl = NULL_TREE;
   11746      3197366 :   if (tree template_info = node_template_info (decl, use_tpl))
   11747      1091939 :     tpl = TI_TEMPLATE (template_info);
   11748      3197366 :   if (tpl == decl)
   11749            0 :     tpl = nullptr;
   11750              : 
   11751              :   /* Stream the template we're instantiated from.  */
   11752      3197366 :   tree_node (tpl);
   11753              : 
   11754      3197366 :   tree container = NULL_TREE;
   11755      3197366 :   if (TREE_CODE (decl) == TEMPLATE_DECL
   11756      3197366 :       ? DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (decl)
   11757      2280391 :       : decl_specialization_friend_p (decl))
   11758        16764 :     container = DECL_CHAIN (decl);
   11759              :   else
   11760      3180602 :     container = CP_DECL_CONTEXT (decl);
   11761              : 
   11762      3197366 :   if (TYPE_P (container))
   11763      1872558 :     container = TYPE_NAME (container);
   11764              : 
   11765      3197366 :   tree_node (container);
   11766              : 
   11767      3197366 :   return container;
   11768              : }
   11769              : 
   11770              : tree
   11771      1117422 : trees_in::decl_container ()
   11772              : {
   11773              :   /* The maybe-template.  */
   11774      1117422 :   (void)tree_node ();
   11775              : 
   11776      1117422 :   tree container = tree_node ();
   11777              : 
   11778      1117422 :   return container;
   11779              : }
   11780              : 
   11781              : /* Gets a 2-bit discriminator to distinguish coroutine actor or destroy
   11782              :    functions from a normal function.  */
   11783              : 
   11784              : static int
   11785       940164 : get_coroutine_discriminator (tree inner)
   11786              : {
   11787       940164 :   if (DECL_COROUTINE_P (inner))
   11788           60 :     if (tree ramp = DECL_RAMP_FN (inner))
   11789              :       {
   11790           18 :         if (DECL_ACTOR_FN (ramp) == inner)
   11791              :           return 1;
   11792            9 :         else if (DECL_DESTROY_FN (ramp) == inner)
   11793              :           return 2;
   11794              :         else
   11795            0 :           gcc_unreachable ();
   11796              :       }
   11797              :   return 0;
   11798              : }
   11799              : 
   11800              : /* Write out key information about a mergeable DEP.  Does not write
   11801              :    the contents of DEP itself.  The context has already been
   11802              :    written.  The container has already been streamed.  */
   11803              : 
   11804              : void
   11805      3197366 : trees_out::key_mergeable (int tag, merge_kind mk, tree decl, tree inner,
   11806              :                           tree container, depset *dep)
   11807              : {
   11808      3197366 :   if (dep && is_key_order ())
   11809              :     {
   11810       862589 :       gcc_checking_assert (dep->is_special ());
   11811       862589 :       dep = dep->deps[0];
   11812              :     }
   11813              : 
   11814      3197366 :   if (streaming_p ())
   11815      1161114 :     dump (dumper::MERGE)
   11816         1089 :       && dump ("Writing:%d's %s merge key (%s) %C:%N", tag, merge_kind_name[mk],
   11817          984 :                dep ? dep->entity_kind_name () : "contained",
   11818         1089 :                TREE_CODE (decl), decl);
   11819              : 
   11820              :   /* Now write the locating information. */
   11821      3197366 :   if (mk & MK_template_mask)
   11822              :     {
   11823              :       /* Specializations are located via their originating template,
   11824              :          and the set of template args they specialize.  */
   11825      1040403 :       gcc_checking_assert (dep && dep->is_special ());
   11826      1040403 :       spec_entry *entry = reinterpret_cast <spec_entry *> (dep->deps[0]);
   11827              : 
   11828      1040403 :       tree_node (entry->tmpl);
   11829      1040403 :       tree_node (entry->args);
   11830      1040403 :       if (mk & MK_tmpl_decl_mask)
   11831       727641 :         if (flag_concepts && TREE_CODE (inner) == VAR_DECL)
   11832              :           {
   11833              :             /* Variable template partial specializations might need
   11834              :                constraints (see spec_hasher::equal).  It's simpler to
   11835              :                write NULL when we don't need them.  */
   11836        16392 :             tree constraints = NULL_TREE;
   11837              : 
   11838        16392 :             if (uses_template_parms (entry->args))
   11839          417 :               constraints = get_constraints (inner);
   11840        16392 :             tree_node (constraints);
   11841              :           }
   11842              : 
   11843      1040403 :       if (CHECKING_P)
   11844              :         {
   11845              :           /* Make sure we can locate the decl.  */
   11846      1040403 :           tree existing = match_mergeable_specialization
   11847      1040403 :             (bool (mk & MK_tmpl_decl_mask), entry);
   11848              : 
   11849      1040403 :           gcc_assert (existing);
   11850      1040403 :           if (mk & MK_tmpl_decl_mask)
   11851              :             {
   11852       727641 :               if (mk & MK_tmpl_tmpl_mask)
   11853         6396 :                 existing = DECL_TI_TEMPLATE (existing);
   11854              :             }
   11855              :           else
   11856              :             {
   11857       312762 :               if (mk & MK_tmpl_tmpl_mask)
   11858         1677 :                 existing = CLASSTYPE_TI_TEMPLATE (existing);
   11859              :               else
   11860       311085 :                 existing = TYPE_NAME (existing);
   11861              :             }
   11862              : 
   11863              :           /* The walkabout should have found ourselves.  */
   11864      1040403 :           gcc_checking_assert (TREE_CODE (decl) == TYPE_DECL
   11865              :                                ? same_type_p (TREE_TYPE (decl),
   11866              :                                               TREE_TYPE (existing))
   11867              :                                : existing == decl);
   11868              :         }
   11869              :     }
   11870      2156963 :   else if (mk != MK_unique)
   11871              :     {
   11872      1785879 :       merge_key key;
   11873      1785879 :       tree name = DECL_NAME (decl);
   11874              : 
   11875      1785879 :       switch (mk)
   11876              :         {
   11877            0 :         default:
   11878            0 :           gcc_unreachable ();
   11879              : 
   11880      1507186 :         case MK_named:
   11881      1507186 :         case MK_friend_spec:
   11882      1507186 :           if (IDENTIFIER_CONV_OP_P (name))
   11883         5146 :             name = conv_op_identifier;
   11884              : 
   11885      1507186 :           if (TREE_CODE (inner) == FUNCTION_DECL)
   11886              :             {
   11887              :               /* Functions are distinguished by parameter types.  */
   11888       810082 :               tree fn_type = TREE_TYPE (inner);
   11889              : 
   11890       810082 :               key.ref_q = type_memfn_rqual (fn_type);
   11891       810082 :               key.coro_disc = get_coroutine_discriminator (inner);
   11892       810082 :               key.args = TYPE_ARG_TYPES (fn_type);
   11893              : 
   11894       810082 :               if (tree reqs = get_constraints (inner))
   11895              :                 {
   11896        57975 :                   if (cxx_dialect < cxx20)
   11897           48 :                     reqs = CI_ASSOCIATED_CONSTRAINTS (reqs);
   11898              :                   else
   11899       115902 :                     reqs = CI_DECLARATOR_REQS (reqs);
   11900        57975 :                   key.constraints = reqs;
   11901              :                 }
   11902              : 
   11903       810082 :               if (IDENTIFIER_CONV_OP_P (name)
   11904       810082 :                   || (decl != inner
   11905       426819 :                       && !(name == fun_identifier
   11906              :                            /* In case the user names something _FUN  */
   11907           84 :                            && LAMBDA_TYPE_P (DECL_CONTEXT (inner)))))
   11908              :                 /* And a function template, or conversion operator needs
   11909              :                    the return type.  Except for the _FUN thunk of a
   11910              :                    generic lambda, which has a recursive decl_type'd
   11911              :                    return type.  */
   11912              :                 // FIXME: What if the return type is a voldemort?
   11913       431923 :                 key.ret = fndecl_declared_return_type (inner);
   11914              :             }
   11915              :           break;
   11916              : 
   11917       128504 :         case MK_field:
   11918       128504 :           {
   11919       128504 :             unsigned ix = 0;
   11920       128504 :             if (TREE_CODE (inner) != FIELD_DECL)
   11921              :               name = NULL_TREE;
   11922              :             else
   11923        22343 :               gcc_checking_assert (!name || !IDENTIFIER_ANON_P (name));
   11924              : 
   11925       128504 :             for (tree field = TYPE_FIELDS (TREE_TYPE (container));
   11926      3143158 :                  ; field = DECL_CHAIN (field))
   11927              :               {
   11928      3271662 :                 tree finner = STRIP_TEMPLATE (field);
   11929      3271662 :                 if (TREE_CODE (finner) == TREE_CODE (inner))
   11930              :                   {
   11931      1106800 :                     if (finner == inner)
   11932              :                       break;
   11933       978296 :                     ix++;
   11934              :                   }
   11935      3143158 :               }
   11936       128504 :             key.index = ix;
   11937              :           }
   11938       128504 :           break;
   11939              : 
   11940         6118 :         case MK_vtable:
   11941         6118 :           {
   11942         6118 :             tree vtable = CLASSTYPE_VTABLES (TREE_TYPE (container));
   11943         7774 :             for (unsigned ix = 0; ; vtable = DECL_CHAIN (vtable), ix++)
   11944         7774 :               if (vtable == decl)
   11945              :                 {
   11946         6118 :                   key.index = ix;
   11947         6118 :                   break;
   11948              :                 }
   11949         6118 :             name = NULL_TREE;
   11950              :           }
   11951         6118 :           break;
   11952              : 
   11953        68682 :         case MK_as_base:
   11954        68682 :           gcc_checking_assert
   11955              :             (decl == TYPE_NAME (CLASSTYPE_AS_BASE (TREE_TYPE (container))));
   11956              :           break;
   11957              : 
   11958        16764 :         case MK_local_friend:
   11959        16764 :           {
   11960              :             /* Find by index on the class's DECL_LIST.  We set TREE_CHAIN to
   11961              :                point to the class in push_template_decl or grokfndecl.  */
   11962        16764 :             unsigned ix = 0;
   11963        16764 :             for (tree decls = CLASSTYPE_DECL_LIST (TREE_CHAIN (decl));
   11964       465870 :                  decls; decls = TREE_CHAIN (decls))
   11965       465870 :               if (!TREE_PURPOSE (decls))
   11966              :                 {
   11967        65544 :                   tree frnd = friend_from_decl_list (TREE_VALUE (decls));
   11968        65544 :                   if (frnd == decl)
   11969              :                     break;
   11970        48780 :                   ix++;
   11971              :                 }
   11972        16764 :             key.index = ix;
   11973        16764 :             name = NULL_TREE;
   11974              :           }
   11975        16764 :           break;
   11976              : 
   11977        11853 :         case MK_local_type:
   11978        11853 :           key_local_type (key, STRIP_TEMPLATE (decl), container);
   11979        11853 :           break;
   11980              : 
   11981         1123 :         case MK_enum:
   11982         1123 :           {
   11983              :             /* Anonymous enums are located by their first identifier,
   11984              :                and underlying type.  */
   11985         1123 :             tree type = TREE_TYPE (decl);
   11986              : 
   11987         1123 :             gcc_checking_assert (UNSCOPED_ENUM_P (type));
   11988              :             /* Using the type name drops the bit precision we might
   11989              :                have been using on the enum.  */
   11990         1123 :             key.ret = TYPE_NAME (ENUM_UNDERLYING_TYPE (type));
   11991         1123 :             if (tree values = TYPE_VALUES (type))
   11992         1123 :               name = DECL_NAME (TREE_VALUE (values));
   11993              :           }
   11994              :           break;
   11995              : 
   11996          880 :         case MK_keyed:
   11997          880 :           {
   11998          880 :             tree scope = get_keyed_decl_scope (inner);
   11999          880 :             gcc_checking_assert (scope);
   12000              : 
   12001          880 :             auto *root = keyed_table->get (scope);
   12002          880 :             unsigned ix = root->length ();
   12003              :             /* If we don't find it, we'll write a really big number
   12004              :                that the reader will ignore.  */
   12005          986 :             while (ix--)
   12006          986 :               if ((*root)[ix] == inner)
   12007              :                 break;
   12008              : 
   12009              :             /* Use the keyed-to decl as the 'name'.  */
   12010          880 :             name = scope;
   12011          880 :             key.index = ix;
   12012              :           }
   12013          880 :           break;
   12014              : 
   12015        44769 :         case MK_partial:
   12016        44769 :           {
   12017        44769 :             tree ti = get_template_info (inner);
   12018        44769 :             key.constraints = get_constraints (inner);
   12019        44769 :             key.ret = TI_TEMPLATE (ti);
   12020        44769 :             key.args = TI_ARGS (ti);
   12021              :           }
   12022        44769 :           break;
   12023              :         }
   12024              : 
   12025      1785879 :       tree_node (name);
   12026      1785879 :       if (streaming_p ())
   12027              :         {
   12028              :           /* Check we have enough bits for the index.  */
   12029       634886 :           gcc_checking_assert (key.index < (1u << (sizeof (unsigned) * 8 - 4)));
   12030              : 
   12031       634886 :           unsigned code = ((key.ref_q << 0)
   12032       634886 :                            | (key.coro_disc << 2)
   12033       634886 :                            | (key.index << 4));
   12034       634886 :           u (code);
   12035              :         }
   12036              : 
   12037      1785879 :       if (mk == MK_enum)
   12038         1123 :         tree_node (key.ret);
   12039      1784756 :       else if (mk == MK_partial
   12040      1739987 :                || (mk == MK_named && inner
   12041      1507186 :                    && TREE_CODE (inner) == FUNCTION_DECL))
   12042              :         {
   12043       854851 :           tree_node (key.ret);
   12044       854851 :           tree arg = key.args;
   12045       854851 :           if (mk == MK_named)
   12046      2337798 :             while (arg && arg != void_list_node)
   12047              :               {
   12048      1527716 :                 tree_node (TREE_VALUE (arg));
   12049      1527716 :                 arg = TREE_CHAIN (arg);
   12050              :               }
   12051       854851 :           tree_node (arg);
   12052       854851 :           tree_node (key.constraints);
   12053              :         }
   12054              :     }
   12055      3197366 : }
   12056              : 
   12057              : /* DECL is a new declaration that may be duplicated in OVL.  Use KEY
   12058              :    to find its clone, or NULL.  If DECL's DECL_NAME is NULL, this
   12059              :    has been found by a proxy.  It will be an enum type located by its
   12060              :    first member.
   12061              : 
   12062              :    We're conservative with matches, so ambiguous decls will be
   12063              :    registered as different, then lead to a lookup error if the two
   12064              :    modules are both visible.  Perhaps we want to do something similar
   12065              :    to duplicate decls to get ODR errors on loading?  We already have
   12066              :    some special casing for namespaces.  */
   12067              : 
   12068              : static tree
   12069       292555 : check_mergeable_decl (merge_kind mk, tree decl, tree ovl, merge_key const &key)
   12070              : {
   12071       292555 :   tree found = NULL_TREE;
   12072      1342035 :   for (ovl_iterator iter (ovl); !found && iter; ++iter)
   12073              :     {
   12074       645749 :       tree match = *iter;
   12075              : 
   12076       645749 :       tree d_inner = decl;
   12077       645749 :       tree m_inner = match;
   12078              : 
   12079       746632 :     again:
   12080       746632 :       if (TREE_CODE (d_inner) != TREE_CODE (m_inner))
   12081              :         {
   12082       110983 :           if (TREE_CODE (match) == NAMESPACE_DECL
   12083       110983 :               && !DECL_NAMESPACE_ALIAS (match))
   12084              :             /* Namespaces are never overloaded.  */
   12085              :             found = match;
   12086              : 
   12087       110983 :           continue;
   12088              :         }
   12089              : 
   12090       635649 :       switch (TREE_CODE (d_inner))
   12091              :         {
   12092       232329 :         case TEMPLATE_DECL:
   12093       232329 :           if (template_heads_equivalent_p (d_inner, m_inner))
   12094              :             {
   12095       100883 :               d_inner = DECL_TEMPLATE_RESULT (d_inner);
   12096       100883 :               m_inner = DECL_TEMPLATE_RESULT (m_inner);
   12097       100883 :               if (d_inner == error_mark_node
   12098       100883 :                   && TYPE_DECL_ALIAS_P (m_inner))
   12099              :                 {
   12100              :                   found = match;
   12101              :                   break;
   12102              :                 }
   12103       100883 :               goto again;
   12104              :             }
   12105              :           break;
   12106              : 
   12107       302989 :         case FUNCTION_DECL:
   12108       302989 :           if (tree m_type = TREE_TYPE (m_inner))
   12109       302989 :             if ((!key.ret
   12110       151578 :                  || same_type_p (key.ret, fndecl_declared_return_type (m_inner)))
   12111       279328 :                 && type_memfn_rqual (m_type) == key.ref_q
   12112       279062 :                 && compparms (key.args, TYPE_ARG_TYPES (m_type))
   12113       130082 :                 && get_coroutine_discriminator (m_inner) == key.coro_disc
   12114              :                 /* Reject if old is a "C" builtin and new is not "C".
   12115              :                    Matches decls_match behaviour.  */
   12116       130079 :                 && (!DECL_IS_UNDECLARED_BUILTIN (m_inner)
   12117         6862 :                     || !DECL_EXTERN_C_P (m_inner)
   12118         6659 :                     || DECL_EXTERN_C_P (d_inner))
   12119              :                 /* Reject if one is a different member of a
   12120              :                    guarded/pre/post fn set.  */
   12121       130064 :                 && (!flag_contracts
   12122       241196 :                     || (DECL_IS_PRE_FN_P (d_inner)
   12123       120598 :                         == DECL_IS_PRE_FN_P (m_inner)))
   12124       433053 :                 && (!flag_contracts
   12125       241196 :                     || (DECL_IS_POST_FN_P (d_inner)
   12126       120598 :                         == DECL_IS_POST_FN_P (m_inner))))
   12127              :               {
   12128       130064 :                 tree m_reqs = get_constraints (m_inner);
   12129       130064 :                 if (m_reqs)
   12130              :                   {
   12131         9651 :                     if (cxx_dialect < cxx20)
   12132            8 :                       m_reqs = CI_ASSOCIATED_CONSTRAINTS (m_reqs);
   12133              :                     else
   12134        19294 :                       m_reqs = CI_DECLARATOR_REQS (m_reqs);
   12135              :                   }
   12136              : 
   12137       130064 :                 if (cp_tree_equal (key.constraints, m_reqs))
   12138       169384 :                   found = match;
   12139              :               }
   12140              :           break;
   12141              : 
   12142        60936 :         case TYPE_DECL:
   12143       121872 :           if (DECL_IMPLICIT_TYPEDEF_P (d_inner)
   12144        60936 :               == DECL_IMPLICIT_TYPEDEF_P (m_inner))
   12145              :             {
   12146        60908 :               if (!IDENTIFIER_ANON_P (DECL_NAME (m_inner)))
   12147        60779 :                 return match;
   12148          129 :               else if (mk == MK_enum
   12149          129 :                        && (TYPE_NAME (ENUM_UNDERLYING_TYPE (TREE_TYPE (m_inner)))
   12150          129 :                            == key.ret))
   12151              :                 found = match;
   12152              :             }
   12153              :           break;
   12154              : 
   12155              :         default:
   12156              :           found = match;
   12157              :           break;
   12158              :         }
   12159              :     }
   12160              : 
   12161       231776 :   return found;
   12162              : }
   12163              : 
   12164              : /* DECL, INNER & TYPE are a skeleton set of nodes for a decl.  Only
   12165              :    the bools have been filled in.  Read its merging key and merge it.
   12166              :    Returns the existing decl if there is one.  */
   12167              : 
   12168              : tree
   12169      1117422 : trees_in::key_mergeable (int tag, merge_kind mk, tree decl, tree inner,
   12170              :                          tree type, tree container, bool is_attached,
   12171              :                          bool is_imported_temploid_friend)
   12172              : {
   12173      1117422 :   const char *kind = "new";
   12174      1117422 :   tree existing = NULL_TREE;
   12175              : 
   12176      1117422 :   if (mk & MK_template_mask)
   12177              :     {
   12178              :       // FIXME: We could stream the specialization hash?
   12179       346360 :       spec_entry spec;
   12180       346360 :       spec.tmpl = tree_node ();
   12181       346360 :       spec.args = tree_node ();
   12182              : 
   12183       346360 :       if (get_overrun ())
   12184            0 :         return error_mark_node;
   12185              : 
   12186       346360 :       DECL_NAME (decl) = DECL_NAME (spec.tmpl);
   12187       346360 :       DECL_CONTEXT (decl) = DECL_CONTEXT (spec.tmpl);
   12188       346360 :       DECL_NAME (inner) = DECL_NAME (decl);
   12189       346360 :       DECL_CONTEXT (inner) = DECL_CONTEXT (decl);
   12190              : 
   12191       346360 :       tree constr = NULL_TREE;
   12192       346360 :       bool is_decl = mk & MK_tmpl_decl_mask;
   12193       346360 :       if (is_decl)
   12194              :         {
   12195       247322 :           if (flag_concepts && TREE_CODE (inner) == VAR_DECL)
   12196              :             {
   12197         5098 :               constr = tree_node ();
   12198         5098 :               if (constr)
   12199            0 :                 set_constraints (inner, constr);
   12200              :             }
   12201       492175 :           spec.spec = (mk & MK_tmpl_tmpl_mask) ? inner : decl;
   12202              :         }
   12203              :       else
   12204        99038 :         spec.spec = type;
   12205       346360 :       existing = match_mergeable_specialization (is_decl, &spec);
   12206       346360 :       if (constr)
   12207              :         /* We'll add these back later, if this is the new decl.  */
   12208            0 :         remove_constraints (inner);
   12209              : 
   12210       346360 :       if (!existing)
   12211              :         ; /* We'll add to the table once read.  */
   12212       149834 :       else if (mk & MK_tmpl_decl_mask)
   12213              :         {
   12214              :           /* A declaration specialization.  */
   12215       103686 :           if (mk & MK_tmpl_tmpl_mask)
   12216         1053 :             existing = DECL_TI_TEMPLATE (existing);
   12217              :         }
   12218              :       else
   12219              :         {
   12220              :           /* A type specialization.  */
   12221        46148 :           if (mk & MK_tmpl_tmpl_mask)
   12222          241 :             existing = CLASSTYPE_TI_TEMPLATE (existing);
   12223              :           else
   12224        45907 :             existing = TYPE_NAME (existing);
   12225              :         }
   12226              :     }
   12227       771062 :   else if (mk == MK_unique)
   12228              :     kind = "unique";
   12229              :   else
   12230              :     {
   12231       578904 :       tree name = tree_node ();
   12232              : 
   12233       578904 :       merge_key key;
   12234       578904 :       unsigned code = u ();
   12235       578904 :       key.ref_q = cp_ref_qualifier ((code >> 0) & 3);
   12236       578904 :       key.coro_disc = (code >> 2) & 3;
   12237       578904 :       key.index = code >> 4;
   12238              : 
   12239       578904 :       if (mk == MK_enum)
   12240          232 :         key.ret = tree_node ();
   12241       578672 :       else if (mk == MK_partial
   12242       568105 :                || ((mk == MK_named || mk == MK_friend_spec)
   12243       481417 :                    && TREE_CODE (inner) == FUNCTION_DECL))
   12244              :         {
   12245       274968 :           key.ret = tree_node ();
   12246       274968 :           tree arg, *arg_ptr = &key.args;
   12247       274968 :           while ((arg = tree_node ())
   12248       774802 :                  && arg != void_list_node
   12249      1287961 :                  && mk != MK_partial)
   12250              :             {
   12251       501213 :               *arg_ptr = tree_cons (NULL_TREE, arg, NULL_TREE);
   12252       501213 :               arg_ptr = &TREE_CHAIN (*arg_ptr);
   12253              :             }
   12254       274968 :           *arg_ptr = arg;
   12255       274968 :           key.constraints = tree_node ();
   12256              :         }
   12257              : 
   12258       578904 :       if (get_overrun ())
   12259            0 :         return error_mark_node;
   12260              : 
   12261       578904 :       if (mk < MK_indirect_lwm)
   12262              :         {
   12263       567865 :           DECL_NAME (decl) = name;
   12264       567865 :           DECL_CONTEXT (decl) = FROB_CONTEXT (container);
   12265              :         }
   12266       578904 :       DECL_NAME (inner) = DECL_NAME (decl);
   12267       578904 :       DECL_CONTEXT (inner) = DECL_CONTEXT (decl);
   12268              : 
   12269       578904 :       if (mk == MK_partial)
   12270              :         {
   12271        10567 :           for (tree spec = DECL_TEMPLATE_SPECIALIZATIONS (key.ret);
   12272        54477 :                spec; spec = TREE_CHAIN (spec))
   12273              :             {
   12274        50044 :               tree tmpl = TREE_VALUE (spec);
   12275        50044 :               tree ti = get_template_info (tmpl);
   12276        50044 :               if (template_args_equal (key.args, TI_ARGS (ti))
   12277        56971 :                   && cp_tree_equal (key.constraints,
   12278              :                                     get_constraints
   12279         6927 :                                     (DECL_TEMPLATE_RESULT (tmpl))))
   12280              :                 {
   12281              :                   existing = tmpl;
   12282              :                   break;
   12283              :                 }
   12284              :             }
   12285              :         }
   12286       568337 :       else if (mk == MK_keyed
   12287          395 :                && DECL_LANG_SPECIFIC (name)
   12288       568732 :                && DECL_MODULE_KEYED_DECLS_P (name))
   12289              :         {
   12290          395 :           gcc_checking_assert (TREE_CODE (container) == NAMESPACE_DECL
   12291              :                                || TREE_CODE (container) == TYPE_DECL
   12292              :                                || TREE_CODE (container) == FUNCTION_DECL);
   12293          395 :           if (auto *set = keyed_table->get (name))
   12294       579053 :             if (key.index < set->length ())
   12295              :               {
   12296          149 :                 existing = (*set)[key.index];
   12297          149 :                 if (existing)
   12298              :                   {
   12299          149 :                     gcc_checking_assert
   12300              :                       (DECL_IMPLICIT_TYPEDEF_P (existing));
   12301          149 :                     if (inner != decl)
   12302           91 :                       existing
   12303           91 :                         = CLASSTYPE_TI_TEMPLATE (TREE_TYPE (existing));
   12304              :                   }
   12305              :               }
   12306              :         }
   12307              :       else
   12308       567942 :         switch (TREE_CODE (container))
   12309              :           {
   12310            0 :           default:
   12311            0 :             gcc_unreachable ();
   12312              : 
   12313       139948 :           case NAMESPACE_DECL:
   12314       139948 :             if (is_attached
   12315       139948 :                 && !is_imported_temploid_friend
   12316       139948 :                 && !(state->is_module () || state->is_partition ()))
   12317              :               kind = "unique";
   12318              :             else
   12319              :               {
   12320       137912 :                 gcc_checking_assert (mk == MK_named || mk == MK_enum);
   12321       137912 :                 tree mvec;
   12322       137912 :                 tree *vslot = mergeable_namespace_slots (container, name,
   12323              :                                                          is_attached, &mvec);
   12324       137912 :                 existing = check_mergeable_decl (mk, decl, *vslot, key);
   12325       137912 :                 if (!existing)
   12326        61895 :                   add_mergeable_namespace_entity (vslot, decl);
   12327              :                 else
   12328              :                   {
   12329              :                     /* Note that we now have duplicates to deal with in
   12330              :                        name lookup.  */
   12331        76017 :                     if (is_attached)
   12332           54 :                       BINDING_VECTOR_PARTITION_DUPS_P (mvec) = true;
   12333              :                     else
   12334        75963 :                       BINDING_VECTOR_GLOBAL_DUPS_P (mvec) = true;
   12335              :                   }
   12336              :               }
   12337       143840 :             break;
   12338              : 
   12339         3892 :           case FUNCTION_DECL:
   12340         3892 :             gcc_checking_assert (mk == MK_local_type);
   12341         3892 :             existing = key_local_type (key, container, name);
   12342         3892 :             if (existing && inner != decl)
   12343         3406 :               existing = TYPE_TI_TEMPLATE (TREE_TYPE (existing));
   12344              :             break;
   12345              : 
   12346       424102 :           case TYPE_DECL:
   12347       424102 :             gcc_checking_assert (!is_imported_temploid_friend);
   12348       424102 :             int use_tmpl = 0;
   12349         5721 :             if (is_attached && !(state->is_module () || state->is_partition ())
   12350              :                 /* Implicit or in-class defaulted member functions
   12351              :                    can come from anywhere.  */
   12352         4087 :                 && !(TREE_CODE (decl) == FUNCTION_DECL
   12353         1169 :                      && !DECL_THUNK_P (decl)
   12354         1169 :                      && DECL_DEFAULTED_FN (decl)
   12355          721 :                      && !DECL_DEFAULTED_OUTSIDE_CLASS_P (decl))
   12356              :                 /* As can members of template specialisations.  */
   12357       427468 :                 && !(node_template_info (container, use_tmpl)
   12358         1235 :                      && use_tmpl != 0))
   12359              :               kind = "unique";
   12360              :             else
   12361              :               {
   12362       420966 :                 tree ctx = TREE_TYPE (container);
   12363              : 
   12364              :                 /* For some reason templated enumeral types are not marked
   12365              :                    as COMPLETE_TYPE_P, even though they have members.
   12366              :                    This may well be a bug elsewhere.  */
   12367       420966 :                 if (TREE_CODE (ctx) == ENUMERAL_TYPE)
   12368        14665 :                   existing = find_enum_member (ctx, name);
   12369       406301 :                 else if (COMPLETE_TYPE_P (ctx))
   12370              :                   {
   12371       194256 :                     switch (mk)
   12372              :                       {
   12373            0 :                       default:
   12374            0 :                         gcc_unreachable ();
   12375              : 
   12376       155056 :                       case MK_named:
   12377       155056 :                         existing = lookup_class_binding (ctx, name);
   12378       155056 :                         if (existing)
   12379              :                           {
   12380       154643 :                             tree inner = decl;
   12381       154643 :                             if (TREE_CODE (inner) == TEMPLATE_DECL
   12382       154643 :                                 && !DECL_MEMBER_TEMPLATE_P (inner))
   12383        68848 :                               inner = DECL_TEMPLATE_RESULT (inner);
   12384              : 
   12385       154643 :                             existing = check_mergeable_decl
   12386       154643 :                               (mk, inner, existing, key);
   12387              : 
   12388       154643 :                             if (!existing && DECL_ALIAS_TEMPLATE_P (decl))
   12389              :                               {} // FIXME: Insert into specialization
   12390              :                             // tables, we'll need the arguments for that!
   12391              :                           }
   12392              :                         break;
   12393              : 
   12394        26765 :                       case MK_field:
   12395        26765 :                         {
   12396        26765 :                           unsigned ix = key.index;
   12397        26765 :                           for (tree field = TYPE_FIELDS (ctx);
   12398       892269 :                                field; field = DECL_CHAIN (field))
   12399              :                             {
   12400       892269 :                               tree finner = STRIP_TEMPLATE (field);
   12401       892269 :                               if (TREE_CODE (finner) == TREE_CODE (inner))
   12402       299424 :                                 if (!ix--)
   12403              :                                   {
   12404              :                                     existing = field;
   12405              :                                     break;
   12406              :                                   }
   12407              :                             }
   12408              :                         }
   12409              :                         break;
   12410              : 
   12411         1464 :                       case MK_vtable:
   12412         1464 :                         {
   12413         1464 :                           unsigned ix = key.index;
   12414         1464 :                           for (tree vtable = CLASSTYPE_VTABLES (ctx);
   12415         1803 :                                vtable; vtable = DECL_CHAIN (vtable))
   12416         1570 :                             if (!ix--)
   12417              :                               {
   12418              :                                 existing = vtable;
   12419              :                                 break;
   12420              :                               }
   12421              :                         }
   12422              :                         break;
   12423              : 
   12424         7975 :                       case MK_as_base:
   12425         7975 :                         {
   12426         7975 :                           tree as_base = CLASSTYPE_AS_BASE (ctx);
   12427         7975 :                           if (as_base && as_base != ctx)
   12428         7975 :                             existing = TYPE_NAME (as_base);
   12429              :                         }
   12430              :                         break;
   12431              : 
   12432         2996 :                       case MK_local_friend:
   12433         2996 :                         {
   12434         2996 :                           unsigned ix = key.index;
   12435         2996 :                           for (tree decls = CLASSTYPE_DECL_LIST (ctx);
   12436        84525 :                                decls; decls = TREE_CHAIN (decls))
   12437        84525 :                             if (!TREE_PURPOSE (decls) && !ix--)
   12438              :                               {
   12439         2996 :                                 existing
   12440         2996 :                                   = friend_from_decl_list (TREE_VALUE (decls));
   12441         2996 :                                 break;
   12442              :                               }
   12443              :                         }
   12444              :                         break;
   12445              :                       }
   12446              : 
   12447       194256 :                     if (existing && mk < MK_indirect_lwm && mk != MK_partial
   12448       190117 :                         && TREE_CODE (decl) == TEMPLATE_DECL
   12449       282024 :                         && !DECL_MEMBER_TEMPLATE_P (decl))
   12450              :                       {
   12451        70832 :                         tree ti;
   12452        70832 :                         if (DECL_IMPLICIT_TYPEDEF_P (existing))
   12453          906 :                           ti = TYPE_TEMPLATE_INFO (TREE_TYPE (existing));
   12454              :                         else
   12455        69926 :                           ti = DECL_TEMPLATE_INFO (existing);
   12456        70832 :                         existing = TI_TEMPLATE (ti);
   12457              :                       }
   12458              :                   }
   12459              :               }
   12460              :           }
   12461              :     }
   12462              : 
   12463      1117422 :   dump (dumper::MERGE)
   12464         3169 :     && dump ("Read:%d's %s merge key (%s) %C:%N", tag, merge_kind_name[mk],
   12465         3169 :              existing ? "matched" : kind, TREE_CODE (decl), decl);
   12466              : 
   12467              :   return existing;
   12468              : }
   12469              : 
   12470              : void
   12471       258433 : trees_out::binfo_mergeable (tree binfo)
   12472              : {
   12473       258433 :   tree dom = binfo;
   12474       328091 :   while (tree parent = BINFO_INHERITANCE_CHAIN (dom))
   12475              :     dom = parent;
   12476       258433 :   tree type = BINFO_TYPE (dom);
   12477       258433 :   gcc_checking_assert (TYPE_BINFO (type) == dom);
   12478       258433 :   tree_node (type);
   12479       258433 :   if (streaming_p ())
   12480              :     {
   12481              :       unsigned ix = 0;
   12482       140324 :       for (; dom != binfo; dom = TREE_CHAIN (dom))
   12483        36209 :         ix++;
   12484       104115 :       u (ix);
   12485              :     }
   12486       258433 : }
   12487              : 
   12488              : unsigned
   12489        87315 : trees_in::binfo_mergeable (tree *type)
   12490              : {
   12491        87315 :   *type = tree_node ();
   12492        87315 :   return u ();
   12493              : }
   12494              : 
   12495              : /* DECL is a just streamed declaration with attributes DATTR that should
   12496              :    have matching ABI tags as EXISTING's attributes EATTR.  Check that the
   12497              :    ABI tags match, and report an error if not.  */
   12498              : 
   12499              : void
   12500       295686 : trees_in::check_abi_tags (tree existing, tree decl, tree &eattr, tree &dattr)
   12501              : {
   12502       295686 :   tree etags = lookup_attribute ("abi_tag", eattr);
   12503       295686 :   tree dtags = lookup_attribute ("abi_tag", dattr);
   12504       295686 :   if ((etags == nullptr) != (dtags == nullptr)
   12505       295686 :       || (etags && !attribute_value_equal (etags, dtags)))
   12506              :     {
   12507           30 :       if (etags)
   12508           21 :         etags = TREE_VALUE (etags);
   12509           30 :       if (dtags)
   12510           24 :         dtags = TREE_VALUE (dtags);
   12511              : 
   12512              :       /* We only error if mangling wouldn't consider the tags equivalent.  */
   12513           30 :       if (!equal_abi_tags (etags, dtags))
   12514              :         {
   12515           21 :           auto_diagnostic_group d;
   12516           21 :           if (dtags)
   12517           15 :             error_at (DECL_SOURCE_LOCATION (decl),
   12518              :                       "mismatching abi tags for %qD with tags %qE",
   12519              :                       decl, dtags);
   12520              :           else
   12521            6 :             error_at (DECL_SOURCE_LOCATION (decl),
   12522              :                       "mismatching abi tags for %qD with no tags", decl);
   12523           21 :           if (etags)
   12524           15 :             inform (DECL_SOURCE_LOCATION (existing),
   12525              :                     "existing declaration here with tags %qE", etags);
   12526              :           else
   12527            6 :             inform (DECL_SOURCE_LOCATION (existing),
   12528              :                     "existing declaration here with no tags");
   12529           21 :         }
   12530              : 
   12531              :       /* Always use the existing abi_tags as the canonical set so that
   12532              :          later processing doesn't get confused.  */
   12533           30 :       if (dtags)
   12534           24 :         dattr = remove_attribute ("abi_tag", dattr);
   12535           30 :       if (etags)
   12536           21 :         duplicate_one_attribute (&dattr, eattr, "abi_tag");
   12537              :     }
   12538       295686 : }
   12539              : 
   12540              : /* DECL is a just streamed mergeable decl that should match EXISTING.  Check
   12541              :    it does and issue an appropriate diagnostic if not.  Merge any
   12542              :    bits from DECL to EXISTING.  This is stricter matching than
   12543              :    decls_match, because we can rely on ODR-sameness, and we cannot use
   12544              :    decls_match because it can cause instantiations of constraints.  */
   12545              : 
   12546              : bool
   12547       434095 : trees_in::is_matching_decl (tree existing, tree decl, bool is_typedef)
   12548              : {
   12549              :   // FIXME: We should probably do some duplicate decl-like stuff here
   12550              :   // (beware, default parms should be the same?)  Can we just call
   12551              :   // duplicate_decls and teach it how to handle the module-specific
   12552              :   // permitted/required duplications?
   12553              : 
   12554              :   // We know at this point that the decls have matched by key, so we
   12555              :   // can elide some of the checking
   12556       434095 :   gcc_checking_assert (TREE_CODE (existing) == TREE_CODE (decl));
   12557              : 
   12558       434095 :   tree d_inner = decl;
   12559       434095 :   tree e_inner = existing;
   12560       434095 :   if (TREE_CODE (decl) == TEMPLATE_DECL)
   12561              :     {
   12562       145456 :       d_inner = DECL_TEMPLATE_RESULT (d_inner);
   12563       145456 :       e_inner = DECL_TEMPLATE_RESULT (e_inner);
   12564       145456 :       gcc_checking_assert (TREE_CODE (e_inner) == TREE_CODE (d_inner));
   12565              :     }
   12566              : 
   12567              :   // FIXME: do more precise errors at point of mismatch
   12568       434095 :   const char *mismatch_msg = nullptr;
   12569              : 
   12570       434095 :   if (VAR_OR_FUNCTION_DECL_P (d_inner)
   12571       434095 :       && DECL_EXTERN_C_P (d_inner) != DECL_EXTERN_C_P (e_inner))
   12572              :     {
   12573            6 :       mismatch_msg = G_("conflicting language linkage for imported "
   12574              :                         "declaration %#qD");
   12575            6 :       goto mismatch;
   12576              :     }
   12577       434089 :   else if (TREE_CODE (d_inner) == FUNCTION_DECL)
   12578              :     {
   12579       197964 :       tree e_ret = fndecl_declared_return_type (existing);
   12580       197964 :       tree d_ret = fndecl_declared_return_type (decl);
   12581              : 
   12582        85361 :       if (decl != d_inner && DECL_NAME (d_inner) == fun_identifier
   12583       197988 :           && LAMBDA_TYPE_P (DECL_CONTEXT (d_inner)))
   12584              :         /* This has a recursive type that will compare different.  */;
   12585       197952 :       else if (!same_type_p (d_ret, e_ret))
   12586              :         {
   12587            9 :           mismatch_msg = G_("conflicting type for imported declaration %#qD");
   12588            9 :           goto mismatch;
   12589              :         }
   12590              : 
   12591       197955 :       tree e_type = TREE_TYPE (e_inner);
   12592       197955 :       tree d_type = TREE_TYPE (d_inner);
   12593              : 
   12594       197955 :       for (tree e_args = TYPE_ARG_TYPES (e_type),
   12595       197955 :              d_args = TYPE_ARG_TYPES (d_type);
   12596       341334 :            e_args != d_args && (e_args || d_args);
   12597       143379 :            e_args = TREE_CHAIN (e_args), d_args = TREE_CHAIN (d_args))
   12598              :         {
   12599       143379 :           if (!(e_args && d_args))
   12600              :             {
   12601            0 :               mismatch_msg = G_("conflicting argument list for imported "
   12602              :                                 "declaration %#qD");
   12603            0 :               goto mismatch;
   12604              :             }
   12605              : 
   12606       143379 :           if (!same_type_p (TREE_VALUE (d_args), TREE_VALUE (e_args)))
   12607              :             {
   12608            0 :               mismatch_msg = G_("conflicting argument types for imported "
   12609              :                                 "declaration %#qD");
   12610            0 :               goto mismatch;
   12611              :             }
   12612              :         }
   12613              : 
   12614              :       /* If EXISTING has an undeduced or uninstantiated exception
   12615              :          specification, but DECL does not, propagate the exception
   12616              :          specification.  Otherwise we end up asserting or trying to
   12617              :          instantiate it in the middle of loading.   */
   12618       197955 :       tree e_spec = TYPE_RAISES_EXCEPTIONS (e_type);
   12619       197955 :       tree d_spec = TYPE_RAISES_EXCEPTIONS (d_type);
   12620       290570 :       if (DECL_MAYBE_DELETED (e_inner) || DEFERRED_NOEXCEPT_SPEC_P (e_spec))
   12621              :         {
   12622        15742 :           if (!DEFERRED_NOEXCEPT_SPEC_P (d_spec)
   12623        46856 :               || (UNEVALUATED_NOEXCEPT_SPEC_P (e_spec)
   12624        12792 :                   && !UNEVALUATED_NOEXCEPT_SPEC_P (d_spec)))
   12625              :             {
   12626          260 :               dump (dumper::MERGE)
   12627            6 :                 && dump ("Propagating instantiated noexcept to %N", existing);
   12628          260 :               TREE_TYPE (existing) = d_type;
   12629              : 
   12630              :               /* Propagate to existing clones.  */
   12631          260 :               tree clone;
   12632          524 :               FOR_EACH_CLONE (clone, existing)
   12633              :                 {
   12634          264 :                   if (TREE_TYPE (clone) == e_type)
   12635          264 :                     TREE_TYPE (clone) = d_type;
   12636              :                   else
   12637            0 :                     TREE_TYPE (clone)
   12638            0 :                       = build_exception_variant (TREE_TYPE (clone), d_spec);
   12639              :                 }
   12640              :             }
   12641              :         }
   12642       182163 :       else if (!DECL_MAYBE_DELETED (d_inner)
   12643       260300 :                && !DEFERRED_NOEXCEPT_SPEC_P (d_spec)
   12644       364325 :                && !comp_except_specs (d_spec, e_spec, ce_type))
   12645              :         {
   12646         1264 :           mismatch_msg = G_("conflicting %<noexcept%> specifier for "
   12647              :                             "imported declaration %#qD");
   12648         1264 :           goto mismatch;
   12649              :         }
   12650              : 
   12651              :       /* Similarly if EXISTING has an undeduced return type, but DECL's
   12652              :          is already deduced.  */
   12653       196691 :       if (undeduced_auto_decl (existing) && !undeduced_auto_decl (decl))
   12654              :         {
   12655           13 :           dump (dumper::MERGE)
   12656            0 :             && dump ("Propagating deduced return type to %N", existing);
   12657           13 :           gcc_checking_assert (existing == e_inner);
   12658           13 :           FNDECL_USED_AUTO (existing) = true;
   12659           13 :           DECL_SAVED_AUTO_RETURN_TYPE (existing) = TREE_TYPE (e_type);
   12660           13 :           TREE_TYPE (existing) = change_return_type (TREE_TYPE (d_type), e_type);
   12661              :         }
   12662       196678 :       else if (type_uses_auto (d_ret)
   12663       196678 :                && !same_type_p (TREE_TYPE (d_type), TREE_TYPE (e_type)))
   12664              :         {
   12665            9 :           mismatch_msg = G_("conflicting deduced return type for "
   12666              :                             "imported declaration %#qD");
   12667            9 :           goto mismatch;
   12668              :         }
   12669              : 
   12670              :       /* Similarly if EXISTING has undeduced constexpr, but DECL's
   12671              :          is already deduced.  */
   12672       196682 :       if (DECL_DECLARED_CONSTEXPR_P (e_inner)
   12673       196682 :           == DECL_DECLARED_CONSTEXPR_P (d_inner))
   12674              :         /* Already matches.  */;
   12675            2 :       else if (DECL_DECLARED_CONSTEXPR_P (d_inner)
   12676            2 :                && (DECL_MAYBE_DELETED (e_inner)
   12677            0 :                    || decl_implicit_constexpr_p (d_inner)))
   12678              :         /* DECL was deduced, copy to EXISTING.  */
   12679              :         {
   12680            1 :           DECL_DECLARED_CONSTEXPR_P (e_inner) = true;
   12681            1 :           if (decl_implicit_constexpr_p (d_inner))
   12682            0 :             DECL_LANG_SPECIFIC (e_inner)->u.fn.implicit_constexpr = true;
   12683              :         }
   12684            1 :       else if (DECL_DECLARED_CONSTEXPR_P (e_inner)
   12685            1 :                && (DECL_MAYBE_DELETED (d_inner)
   12686            0 :                    || decl_implicit_constexpr_p (e_inner)))
   12687              :         /* EXISTING was deduced, leave it alone.  */;
   12688              :       else
   12689              :         {
   12690            0 :           mismatch_msg = G_("conflicting %<constexpr%> for imported "
   12691              :                             "declaration %#qD");
   12692            0 :           goto mismatch;
   12693              :         }
   12694              : 
   12695              :       /* Don't synthesize a defaulted function if we're importing one
   12696              :          we've already determined.  */
   12697       196682 :       if (!DECL_MAYBE_DELETED (d_inner))
   12698       196568 :         DECL_MAYBE_DELETED (e_inner) = false;
   12699              :     }
   12700       236125 :   else if (is_typedef)
   12701              :     {
   12702        84227 :       if (!DECL_ORIGINAL_TYPE (e_inner)
   12703        84227 :           || !same_type_p (DECL_ORIGINAL_TYPE (d_inner),
   12704              :                            DECL_ORIGINAL_TYPE (e_inner)))
   12705              :         {
   12706            3 :           mismatch_msg = G_("conflicting imported declaration %q#D");
   12707            3 :           goto mismatch;
   12708              :         }
   12709              :     }
   12710              :   /* Using cp_tree_equal because we can meet TYPE_ARGUMENT_PACKs
   12711              :      here. I suspect the entities that directly do that are things
   12712              :      that shouldn't go to duplicate_decls (FIELD_DECLs etc).   */
   12713       151898 :   else if (!cp_tree_equal (TREE_TYPE (decl), TREE_TYPE (existing)))
   12714              :     {
   12715              :       mismatch_msg = G_("conflicting type for imported declaration %#qD");
   12716         2013 :     mismatch:
   12717         2013 :       if (DECL_IS_UNDECLARED_BUILTIN (existing))
   12718              :         /* Just like duplicate_decls, presum the user knows what
   12719              :            they're doing in overriding a builtin.   */
   12720         1270 :         TREE_TYPE (existing) = TREE_TYPE (decl);
   12721          743 :       else if (decl_function_context (decl))
   12722              :         /* The type of a mergeable local entity (such as a function scope
   12723              :            capturing lambda's closure type fields) can depend on an
   12724              :            unmergeable local entity (such as a local variable), so type
   12725              :            equality isn't feasible in general for local entities.  */;
   12726              :       else
   12727              :         {
   12728           21 :           gcc_checking_assert (mismatch_msg);
   12729           21 :           auto_diagnostic_group d;
   12730           21 :           error_at (DECL_SOURCE_LOCATION (decl), mismatch_msg, decl);
   12731           21 :           inform (DECL_SOURCE_LOCATION (existing),
   12732              :                   "existing declaration %#qD", existing);
   12733           21 :           return false;
   12734           21 :         }
   12735              :     }
   12736              : 
   12737       434074 :   if (DECL_IS_UNDECLARED_BUILTIN (existing)
   12738       434074 :       && !DECL_IS_UNDECLARED_BUILTIN (decl))
   12739              :     {
   12740              :       /* We're matching a builtin that the user has yet to declare.
   12741              :          We are the one!  This is very much duplicate-decl
   12742              :          shenanigans. */
   12743         1529 :       DECL_SOURCE_LOCATION (existing) = DECL_SOURCE_LOCATION (decl);
   12744         1529 :       if (TREE_CODE (decl) != TYPE_DECL)
   12745              :         {
   12746              :           /* Propagate exceptions etc.  */
   12747         1510 :           TREE_TYPE (existing) = TREE_TYPE (decl);
   12748         1510 :           TREE_NOTHROW (existing) = TREE_NOTHROW (decl);
   12749              :         }
   12750              :       /* This is actually an import! */
   12751         1529 :       DECL_MODULE_IMPORT_P (existing) = true;
   12752              : 
   12753              :       /* Yay, sliced!  */
   12754         1529 :       existing->base = decl->base;
   12755              : 
   12756         1529 :       if (TREE_CODE (decl) == FUNCTION_DECL)
   12757              :         {
   12758              :           /* Ew :(  */
   12759         1510 :           memcpy (&existing->decl_common.size,
   12760              :                   &decl->decl_common.size,
   12761              :                   (offsetof (tree_decl_common, pt_uid)
   12762              :                    - offsetof (tree_decl_common, size)));
   12763         1510 :           auto bltin_class = DECL_BUILT_IN_CLASS (decl);
   12764         1510 :           existing->function_decl.built_in_class = bltin_class;
   12765         1510 :           auto fncode = DECL_UNCHECKED_FUNCTION_CODE (decl);
   12766         1510 :           DECL_UNCHECKED_FUNCTION_CODE (existing) = fncode;
   12767         1510 :           if (existing->function_decl.built_in_class == BUILT_IN_NORMAL)
   12768              :             {
   12769         1324 :               if (builtin_decl_explicit_p (built_in_function (fncode)))
   12770         1324 :                 switch (fncode)
   12771              :                   {
   12772            0 :                   case BUILT_IN_STPCPY:
   12773            0 :                     set_builtin_decl_implicit_p
   12774            0 :                       (built_in_function (fncode), true);
   12775            0 :                     break;
   12776         1324 :                   default:
   12777         1324 :                     set_builtin_decl_declared_p
   12778         1324 :                       (built_in_function (fncode), true);
   12779         1324 :                     break;
   12780              :                   }
   12781         1324 :               copy_attributes_to_builtin (decl);
   12782              :             }
   12783              :         }
   12784              :     }
   12785              : 
   12786       434074 :   if (VAR_OR_FUNCTION_DECL_P (decl)
   12787       434074 :       && DECL_TEMPLATE_INSTANTIATED (decl))
   12788              :     /* Don't instantiate again!  */
   12789         8903 :     DECL_TEMPLATE_INSTANTIATED (existing) = true;
   12790              : 
   12791       434074 :   if (TREE_CODE (d_inner) == FUNCTION_DECL
   12792       434074 :       && DECL_DECLARED_INLINE_P (d_inner))
   12793              :     {
   12794       160936 :       DECL_DECLARED_INLINE_P (e_inner) = true;
   12795       160936 :       if (!DECL_SAVED_TREE (e_inner)
   12796        79422 :           && lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (d_inner))
   12797       160946 :           && !lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (e_inner)))
   12798              :         {
   12799           30 :           DECL_INTERFACE_KNOWN (e_inner)
   12800           10 :             |= DECL_INTERFACE_KNOWN (d_inner);
   12801           10 :           DECL_DISREGARD_INLINE_LIMITS (e_inner)
   12802           10 :             |= DECL_DISREGARD_INLINE_LIMITS (d_inner);
   12803              :           // TODO: we will eventually want to merge all decl attributes
   12804           10 :           duplicate_one_attribute (&DECL_ATTRIBUTES (e_inner),
   12805           10 :                                    DECL_ATTRIBUTES (d_inner), "gnu_inline");
   12806              :         }
   12807              :     }
   12808       434074 :   if (!DECL_EXTERNAL (d_inner))
   12809       204397 :     DECL_EXTERNAL (e_inner) = false;
   12810              : 
   12811       434074 :   if (VAR_OR_FUNCTION_DECL_P (d_inner))
   12812       436760 :     check_abi_tags (existing, decl,
   12813       218380 :                     DECL_ATTRIBUTES (e_inner), DECL_ATTRIBUTES (d_inner));
   12814              : 
   12815       434074 :   if (TREE_CODE (decl) == TEMPLATE_DECL)
   12816              :     {
   12817              :       /* Merge default template arguments.  */
   12818       145454 :       tree d_parms = DECL_INNERMOST_TEMPLATE_PARMS (decl);
   12819       145454 :       tree e_parms = DECL_INNERMOST_TEMPLATE_PARMS (existing);
   12820       145454 :       gcc_checking_assert (TREE_VEC_LENGTH (d_parms)
   12821              :                            == TREE_VEC_LENGTH (e_parms));
   12822       417042 :       for (int i = 0; i < TREE_VEC_LENGTH (d_parms); ++i)
   12823              :         {
   12824       271597 :           tree d_default = TREE_PURPOSE (TREE_VEC_ELT (d_parms, i));
   12825       271597 :           tree& e_default = TREE_PURPOSE (TREE_VEC_ELT (e_parms, i));
   12826       271597 :           if (e_default == NULL_TREE)
   12827       230121 :             e_default = d_default;
   12828        41476 :           else if (d_default != NULL_TREE
   12829        41476 :                    && !cp_tree_equal (d_default, e_default))
   12830              :             {
   12831            9 :               auto_diagnostic_group d;
   12832            9 :               tree d_parm = TREE_VALUE (TREE_VEC_ELT (d_parms, i));
   12833            9 :               tree e_parm = TREE_VALUE (TREE_VEC_ELT (e_parms, i));
   12834            9 :               error_at (DECL_SOURCE_LOCATION (d_parm),
   12835              :                         "conflicting default argument for %#qD", d_parm);
   12836            9 :               inform (DECL_SOURCE_LOCATION (e_parm),
   12837              :                       "existing default declared here");
   12838            9 :               return false;
   12839            9 :             }
   12840              :         }
   12841              :     }
   12842              : 
   12843       434065 :   if (TREE_CODE (d_inner) == FUNCTION_DECL)
   12844              :     {
   12845              :       /* Merge default function arguments.  */
   12846       197955 :       tree d_parm = FUNCTION_FIRST_USER_PARMTYPE (d_inner);
   12847       197955 :       tree e_parm = FUNCTION_FIRST_USER_PARMTYPE (e_inner);
   12848       197955 :       int i = 0;
   12849       472198 :       for (; d_parm && d_parm != void_list_node;
   12850       274243 :            d_parm = TREE_CHAIN (d_parm), e_parm = TREE_CHAIN (e_parm), ++i)
   12851              :         {
   12852       274255 :           tree d_default = TREE_PURPOSE (d_parm);
   12853       274255 :           tree& e_default = TREE_PURPOSE (e_parm);
   12854       274255 :           if (e_default == NULL_TREE)
   12855       257679 :             e_default = d_default;
   12856        16576 :           else if (d_default != NULL_TREE
   12857        16576 :                    && !cp_tree_equal (d_default, e_default))
   12858              :             {
   12859           12 :               auto_diagnostic_group d;
   12860           12 :               error_at (get_fndecl_argument_location (d_inner, i),
   12861              :                         "conflicting default argument for parameter %P of %#qD",
   12862              :                         i, decl);
   12863           12 :               inform (get_fndecl_argument_location (e_inner, i),
   12864              :                       "existing default declared here");
   12865           12 :               return false;
   12866           12 :             }
   12867              :         }
   12868              :     }
   12869              : 
   12870              :   return true;
   12871              : }
   12872              : 
   12873              : /* FN is an implicit member function that we've discovered is new to
   12874              :    the class.  Add it to the TYPE_FIELDS chain and the method vector.
   12875              :    Reset the appropriate classtype lazy flag.   */
   12876              : 
   12877              : bool
   12878          910 : trees_in::install_implicit_member (tree fn)
   12879              : {
   12880          910 :   tree ctx = DECL_CONTEXT (fn);
   12881          910 :   tree name = DECL_NAME (fn);
   12882              :   /* We know these are synthesized, so the set of expected prototypes
   12883              :      is quite restricted.  We're not validating correctness, just
   12884              :      distinguishing beteeen the small set of possibilities.  */
   12885          910 :   tree parm_type = TREE_VALUE (FUNCTION_FIRST_USER_PARMTYPE (fn));
   12886          910 :   if (IDENTIFIER_CTOR_P (name))
   12887              :     {
   12888          626 :       if (CLASSTYPE_LAZY_DEFAULT_CTOR (ctx)
   12889          626 :           && VOID_TYPE_P (parm_type))
   12890          160 :         CLASSTYPE_LAZY_DEFAULT_CTOR (ctx) = false;
   12891          466 :       else if (!TYPE_REF_P (parm_type))
   12892              :         return false;
   12893          466 :       else if (CLASSTYPE_LAZY_COPY_CTOR (ctx)
   12894          466 :                && !TYPE_REF_IS_RVALUE (parm_type))
   12895          236 :         CLASSTYPE_LAZY_COPY_CTOR (ctx) = false;
   12896          230 :       else if (CLASSTYPE_LAZY_MOVE_CTOR (ctx))
   12897          230 :         CLASSTYPE_LAZY_MOVE_CTOR (ctx) = false;
   12898              :       else
   12899              :         return false;
   12900              :     }
   12901          284 :   else if (IDENTIFIER_DTOR_P (name))
   12902              :     {
   12903          222 :       if (CLASSTYPE_LAZY_DESTRUCTOR (ctx))
   12904          222 :         CLASSTYPE_LAZY_DESTRUCTOR (ctx) = false;
   12905              :       else
   12906              :         return false;
   12907          222 :       if (DECL_VIRTUAL_P (fn))
   12908              :         /* A virtual dtor should have been created when the class
   12909              :            became complete.  */
   12910              :         return false;
   12911              :     }
   12912           62 :   else if (name == assign_op_identifier)
   12913              :     {
   12914           62 :       if (!TYPE_REF_P (parm_type))
   12915              :         return false;
   12916           62 :       else if (CLASSTYPE_LAZY_COPY_ASSIGN (ctx)
   12917           62 :                && !TYPE_REF_IS_RVALUE (parm_type))
   12918           31 :         CLASSTYPE_LAZY_COPY_ASSIGN (ctx) = false;
   12919           31 :       else if (CLASSTYPE_LAZY_MOVE_ASSIGN (ctx))
   12920           31 :         CLASSTYPE_LAZY_MOVE_ASSIGN (ctx) = false;
   12921              :       else
   12922              :         return false;
   12923              :     }
   12924              :   else
   12925              :     return false;
   12926              : 
   12927          955 :   dump (dumper::MERGE) && dump ("Adding implicit member %N", fn);
   12928              : 
   12929          910 :   DECL_CHAIN (fn) = TYPE_FIELDS (ctx);
   12930          910 :   TYPE_FIELDS (ctx) = fn;
   12931              : 
   12932          910 :   add_method (ctx, fn, false);
   12933              : 
   12934              :     /* Propagate TYPE_FIELDS.  */
   12935          910 :   fixup_type_variants (ctx);
   12936              : 
   12937          910 :   return true;
   12938              : }
   12939              : 
   12940              : /* Return non-zero if DECL has a definition that would be interesting to
   12941              :    write out.  */
   12942              : 
   12943              : static bool
   12944      1509762 : has_definition (tree decl)
   12945              : {
   12946      1509768 :   bool is_tmpl = TREE_CODE (decl) == TEMPLATE_DECL;
   12947      1509768 :   if (is_tmpl)
   12948       327638 :     decl = DECL_TEMPLATE_RESULT (decl);
   12949              : 
   12950      1509768 :   switch (TREE_CODE (decl))
   12951              :     {
   12952              :     default:
   12953              :       break;
   12954              : 
   12955       552539 :     case FUNCTION_DECL:
   12956       552539 :       if (!DECL_SAVED_TREE (decl))
   12957              :         /* Not defined.  */
   12958              :         break;
   12959              : 
   12960       245097 :       if (DECL_DECLARED_INLINE_P (decl))
   12961              :         return true;
   12962              : 
   12963        19082 :       if (header_module_p ())
   12964              :         /* We always need to write definitions in header modules,
   12965              :            since there's no TU to emit them in otherwise.  */
   12966              :         return true;
   12967              : 
   12968         7126 :       if (DECL_TEMPLATE_INFO (decl))
   12969              :         {
   12970         5846 :           int use_tpl = DECL_USE_TEMPLATE (decl);
   12971              : 
   12972              :           // FIXME: Partial specializations have definitions too.
   12973         5846 :           if (use_tpl < 2)
   12974              :             return true;
   12975              :         }
   12976              : 
   12977              :       /* Coroutine transform functions always need to be emitted
   12978              :          into the importing TU if the ramp function will be.  */
   12979         1340 :       if (DECL_COROUTINE_P (decl))
   12980           12 :         if (tree ramp = DECL_RAMP_FN (decl))
   12981              :           return has_definition (ramp);
   12982              :       break;
   12983              : 
   12984       639397 :     case TYPE_DECL:
   12985       639397 :       {
   12986       639397 :         tree type = TREE_TYPE (decl);
   12987       639397 :         if (type == TYPE_MAIN_VARIANT (type)
   12988       295480 :             && decl == TYPE_NAME (type)
   12989       934877 :             && (TREE_CODE (type) == ENUMERAL_TYPE
   12990       295480 :                 ? TYPE_VALUES (type) : TYPE_FIELDS (type)))
   12991              :           return true;
   12992              :       }
   12993              :       break;
   12994              : 
   12995        85434 :     case VAR_DECL:
   12996              :       /* DECL_INITIALIZED_P might not be set on a dependent VAR_DECL.  */
   12997        85434 :       if (DECL_LANG_SPECIFIC (decl)
   12998        84739 :           && DECL_TEMPLATE_INFO (decl)
   12999       137149 :           && DECL_INITIAL (decl))
   13000              :         return true;
   13001              :       else
   13002              :         {
   13003        37727 :           if (!DECL_INITIALIZED_P (decl))
   13004              :             /* Not defined.  */
   13005              :             return false;
   13006              : 
   13007        31815 :           if (header_module_p ())
   13008              :             /* We always need to write definitions in header modules,
   13009              :                since there's no TU to emit them in otherwise.  */
   13010              :             return true;
   13011              : 
   13012         7144 :           if (decl_maybe_constant_var_p (decl))
   13013              :             /* We might need its constant value.  */
   13014              :             return true;
   13015              : 
   13016          406 :           if (vague_linkage_p (decl))
   13017              :             /* These are emitted as needed.  */
   13018              :             return true;
   13019              : 
   13020              :           return false;
   13021              :         }
   13022         6302 :       break;
   13023              : 
   13024         6302 :     case CONCEPT_DECL:
   13025         6302 :       if (DECL_INITIAL (decl))
   13026              :         return true;
   13027              : 
   13028              :       break;
   13029              :     }
   13030              : 
   13031              :   return false;
   13032              : }
   13033              : 
   13034              : uintptr_t *
   13035       598576 : trees_in::find_duplicate (tree existing)
   13036              : {
   13037       283212 :   if (!duplicates)
   13038              :     return NULL;
   13039              : 
   13040       374009 :   return duplicates->get (existing);
   13041              : }
   13042              : 
   13043              : /* We're starting to read a duplicate DECL.  EXISTING is the already
   13044              :    known node.  */
   13045              : 
   13046              : void
   13047       620867 : trees_in::register_duplicate (tree decl, tree existing)
   13048              : {
   13049       620867 :   if (!duplicates)
   13050       101362 :     duplicates = new duplicate_hash_map (40);
   13051              : 
   13052       620867 :   bool existed;
   13053       620867 :   uintptr_t &slot = duplicates->get_or_insert (existing, &existed);
   13054       620867 :   gcc_checking_assert (!existed);
   13055       620867 :   slot = reinterpret_cast<uintptr_t> (decl);
   13056              : 
   13057       620867 :   if (TREE_CODE (decl) == TEMPLATE_DECL)
   13058              :     /* Also register the DECL_TEMPLATE_RESULT as a duplicate so
   13059              :        that passing decl's _RESULT to maybe_duplicate naturally
   13060              :        gives us existing's _RESULT back.  */
   13061       290912 :     register_duplicate (DECL_TEMPLATE_RESULT (decl),
   13062       145456 :                         DECL_TEMPLATE_RESULT (existing));
   13063       620867 : }
   13064              : 
   13065              : /* We've read a definition of MAYBE_EXISTING.  If not a duplicate,
   13066              :    return MAYBE_EXISTING (into which the definition should be
   13067              :    installed).  Otherwise return NULL if already known bad, or the
   13068              :    duplicate we read (for ODR checking, or extracting additional merge
   13069              :    information).  */
   13070              : 
   13071              : tree
   13072       315364 : trees_in::odr_duplicate (tree maybe_existing, bool has_defn)
   13073              : {
   13074       315364 :   tree res = NULL_TREE;
   13075              : 
   13076       478431 :   if (uintptr_t *dup = find_duplicate (maybe_existing))
   13077              :     {
   13078       147120 :       if (!(*dup & 1))
   13079       147117 :         res = reinterpret_cast<tree> (*dup);
   13080              :     }
   13081              :   else
   13082              :     res = maybe_existing;
   13083              : 
   13084       315364 :   assert_definition (maybe_existing, res && !has_defn);
   13085              : 
   13086              :   // FIXME: We probably need to return the template, so that the
   13087              :   // template header can be checked?
   13088       315364 :   return res ? STRIP_TEMPLATE (res) : NULL_TREE;
   13089              : }
   13090              : 
   13091              : /* The following writer functions rely on the current behaviour of
   13092              :    depset::hash::add_dependency making the decl and defn depset nodes
   13093              :    depend on eachother.  That way we don't have to worry about seeding
   13094              :    the tree map with named decls that cannot be looked up by name (I.e
   13095              :    template and function parms).  We know the decl and definition will
   13096              :    be in the same cluster, which is what we want.  */
   13097              : 
   13098              : void
   13099       402747 : trees_out::write_function_def (tree decl)
   13100              : {
   13101       402747 :   tree_node (DECL_RESULT (decl));
   13102              : 
   13103       402747 :   {
   13104              :     /* The function body for a non-inline function or function template
   13105              :        is ignored for determining exposures.  This should only matter
   13106              :        for templates (we don't emit the bodies of non-inline functions
   13107              :        to begin with).  */
   13108       402747 :     auto ovr = dep_hash->ignore_exposure_if (!DECL_DECLARED_INLINE_P (decl));
   13109       402747 :     tree_node (DECL_INITIAL (decl));
   13110       402747 :     tree_node (DECL_SAVED_TREE (decl));
   13111       402747 :   }
   13112              : 
   13113       859142 :   tree_node (DECL_FRIEND_CONTEXT (decl));
   13114              : 
   13115       402747 :   constexpr_fundef *cexpr = retrieve_constexpr_fundef (decl);
   13116              : 
   13117       402747 :   if (streaming_p ())
   13118       201332 :     u (cexpr != nullptr);
   13119       402747 :   if (cexpr)
   13120              :     {
   13121        94771 :       chained_decls (cexpr->parms);
   13122        94771 :       tree_node (cexpr->result);
   13123        94771 :       tree_node (cexpr->body);
   13124              :     }
   13125              : 
   13126       402747 :   function* f = DECL_STRUCT_FUNCTION (decl);
   13127              : 
   13128       402747 :   if (streaming_p ())
   13129              :     {
   13130       201332 :       unsigned flags = 0;
   13131              : 
   13132              :       /* Whether the importer should emit this definition, if used.  */
   13133       201332 :       flags |= 1 * (DECL_NOT_REALLY_EXTERN (decl)
   13134       201332 :                     && (get_importer_interface (decl)
   13135              :                         != importer_interface::external));
   13136              : 
   13137              :       /* Make sure DECL_REALLY_EXTERN and DECL_INTERFACE_KNOWN are consistent
   13138              :          on non-templates or we'll crash later in import_export_decl.  */
   13139       132664 :       gcc_checking_assert (flags || DECL_INTERFACE_KNOWN (decl)
   13140              :                            || (DECL_LANG_SPECIFIC (decl)
   13141              :                                && DECL_LOCAL_DECL_P (decl)
   13142              :                                && DECL_OMP_DECLARE_REDUCTION_P (decl))
   13143              :                            || (DECL_LANG_SPECIFIC (decl)
   13144              :                                && DECL_TEMPLATE_INFO (decl)
   13145              :                                && uses_template_parms (DECL_TI_ARGS (decl))));
   13146              : 
   13147       201332 :       if (f)
   13148              :         {
   13149       200616 :           flags |= 2;
   13150              :           /* These flags are needed in tsubst_lambda_expr.  */
   13151       200616 :           flags |= 4 * f->language->returns_value;
   13152       200616 :           flags |= 8 * f->language->returns_null;
   13153       200616 :           flags |= 16 * f->language->returns_abnormally;
   13154       200616 :           flags |= 32 * f->language->infinite_loop;
   13155              :         }
   13156              : 
   13157       201332 :       u (flags);
   13158              :     }
   13159              : 
   13160       402747 :   if (state && f)
   13161              :     {
   13162       401315 :       state->write_location (*this, f->function_start_locus);
   13163       401315 :       state->write_location (*this, f->function_end_locus);
   13164              :     }
   13165              : 
   13166       402747 :   if (DECL_COROUTINE_P (decl))
   13167              :     {
   13168           32 :       tree ramp = DECL_RAMP_FN (decl);
   13169           32 :       tree_node (ramp);
   13170           32 :       if (!ramp)
   13171              :         {
   13172           20 :           tree_node (DECL_ACTOR_FN (decl));
   13173           20 :           tree_node (DECL_DESTROY_FN (decl));
   13174              :         }
   13175              :     }
   13176       402747 : }
   13177              : 
   13178              : void
   13179            0 : trees_out::mark_function_def (tree)
   13180              : {
   13181            0 : }
   13182              : 
   13183              : bool
   13184       205980 : trees_in::read_function_def (tree decl, tree maybe_template)
   13185              : {
   13186       206512 :   dump () && dump ("Reading function definition %N", decl);
   13187       205980 :   tree result = tree_node ();
   13188       205980 :   tree initial = tree_node ();
   13189       205980 :   tree saved = tree_node ();
   13190       205980 :   tree context = tree_node ();
   13191       205980 :   post_process_data pdata {};
   13192       205980 :   pdata.decl = maybe_template;
   13193              : 
   13194       205980 :   tree maybe_dup = odr_duplicate (maybe_template, DECL_SAVED_TREE (decl));
   13195       411957 :   bool installing = maybe_dup && !DECL_SAVED_TREE (decl);
   13196              : 
   13197       205980 :   constexpr_fundef cexpr;
   13198       205980 :   if (u ())
   13199              :     {
   13200        46297 :       cexpr.parms = chained_decls ();
   13201        46297 :       cexpr.result = tree_node ();
   13202        46297 :       cexpr.body = tree_node ();
   13203        46297 :       cexpr.decl = decl;
   13204              :     }
   13205              :   else
   13206       159683 :     cexpr.decl = NULL_TREE;
   13207              : 
   13208       205980 :   unsigned flags = u ();
   13209       205980 :   if (flags & 2)
   13210              :     {
   13211       205177 :       pdata.start_locus = state->read_location (*this);
   13212       205177 :       pdata.end_locus = state->read_location (*this);
   13213       205177 :       pdata.returns_value = flags & 4;
   13214       205177 :       pdata.returns_null = flags & 8;
   13215       205177 :       pdata.returns_abnormally = flags & 16;
   13216       205177 :       pdata.infinite_loop = flags & 32;
   13217              :     }
   13218              : 
   13219       205980 :   tree coro_actor = NULL_TREE;
   13220       205980 :   tree coro_destroy = NULL_TREE;
   13221       205980 :   tree coro_ramp = NULL_TREE;
   13222       205980 :   if (DECL_COROUTINE_P (decl))
   13223              :     {
   13224           18 :       coro_ramp = tree_node ();
   13225           18 :       if (!coro_ramp)
   13226              :         {
   13227           12 :           coro_actor = tree_node ();
   13228           12 :           coro_destroy = tree_node ();
   13229           12 :           if ((coro_actor == NULL_TREE) != (coro_destroy == NULL_TREE))
   13230            0 :             set_overrun ();
   13231              :         }
   13232              :     }
   13233              : 
   13234       205980 :   if (get_overrun ())
   13235              :     return NULL_TREE;
   13236              : 
   13237       205980 :   if (installing)
   13238              :     {
   13239       119693 :       DECL_NOT_REALLY_EXTERN (decl) = flags & 1;
   13240       119693 :       DECL_RESULT (decl) = result;
   13241       119693 :       DECL_INITIAL (decl) = initial;
   13242       119693 :       DECL_SAVED_TREE (decl) = saved;
   13243              : 
   13244       119693 :       if (context)
   13245         5370 :         SET_DECL_FRIEND_CONTEXT (decl, context);
   13246       119693 :       if (cexpr.decl)
   13247        32167 :         register_constexpr_fundef (cexpr);
   13248              : 
   13249       119693 :       if (coro_ramp)
   13250            6 :         coro_set_ramp_function (decl, coro_ramp);
   13251       119687 :       else if (coro_actor && coro_destroy)
   13252            3 :         coro_set_transform_functions (decl, coro_actor, coro_destroy);
   13253              : 
   13254       119693 :       if (DECL_LOCAL_DECL_P (decl))
   13255              :         /* Block-scope OMP UDRs aren't real functions, and don't need a
   13256              :            function structure to be allocated or to be expanded.  */
   13257            3 :         gcc_checking_assert (DECL_OMP_DECLARE_REDUCTION_P (decl));
   13258              :       else
   13259       119690 :         post_process (pdata);
   13260              :     }
   13261              :   else if (maybe_dup)
   13262              :     {
   13263              :       // FIXME:QOI Check matching defn
   13264              :     }
   13265              : 
   13266              :   return true;
   13267              : }
   13268              : 
   13269              : /* Also for CONCEPT_DECLs.  */
   13270              : 
   13271              : void
   13272       111568 : trees_out::write_var_def (tree decl)
   13273              : {
   13274              :   /* The initializer of a non-inline variable or variable template is
   13275              :      ignored for determining exposures.  */
   13276       111568 :   auto ovr = dep_hash->ignore_exposure_if (VAR_P (decl)
   13277       120917 :                                            && !DECL_INLINE_VAR_P (decl));
   13278              : 
   13279       111568 :   tree init = DECL_INITIAL (decl);
   13280       111568 :   tree_node (init);
   13281       111568 :   if (!init)
   13282              :     {
   13283         1360 :       tree dyn_init = NULL_TREE;
   13284              : 
   13285              :       /* We only need to write initializers in header modules.  */
   13286         2524 :       if (header_module_p () && DECL_NONTRIVIALLY_INITIALIZED_P (decl))
   13287              :         {
   13288          450 :           dyn_init = value_member (decl,
   13289          450 :                                    CP_DECL_THREAD_LOCAL_P (decl)
   13290              :                                    ? tls_aggregates : static_aggregates);
   13291          450 :           gcc_checking_assert (dyn_init);
   13292              :           /* Mark it so write_inits knows this is needed.  */
   13293          450 :           TREE_LANG_FLAG_0 (dyn_init) = true;
   13294          450 :           dyn_init = TREE_PURPOSE (dyn_init);
   13295              :         }
   13296         1360 :       tree_node (dyn_init);
   13297              :     }
   13298       111568 : }
   13299              : 
   13300              : void
   13301            0 : trees_out::mark_var_def (tree)
   13302              : {
   13303            0 : }
   13304              : 
   13305              : bool
   13306        42935 : trees_in::read_var_def (tree decl, tree maybe_template)
   13307              : {
   13308              :   /* Do not mark the virtual table entries as used.  */
   13309        42935 :   bool vtable = VAR_P (decl) && DECL_VTABLE_OR_VTT_P (decl);
   13310        42935 :   unused += vtable;
   13311        42935 :   tree init = tree_node ();
   13312        42935 :   tree dyn_init = init ? NULL_TREE : tree_node ();
   13313        42935 :   unused -= vtable;
   13314              : 
   13315        42935 :   if (get_overrun ())
   13316              :     return false;
   13317              : 
   13318        42935 :   bool initialized = (VAR_P (decl) ? bool (DECL_INITIALIZED_P (decl))
   13319        42935 :                       : bool (DECL_INITIAL (decl)));
   13320        42935 :   tree maybe_dup = odr_duplicate (maybe_template, initialized);
   13321        42935 :   bool installing = maybe_dup && !initialized;
   13322        42935 :   if (installing)
   13323              :     {
   13324        26065 :       DECL_INITIAL (decl) = init;
   13325        26065 :       if (DECL_EXTERNAL (decl))
   13326         3177 :         DECL_NOT_REALLY_EXTERN (decl) = true;
   13327        26065 :       if (VAR_P (decl))
   13328              :         {
   13329        22995 :           DECL_INITIALIZED_P (decl) = true;
   13330        22995 :           if (maybe_dup && DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (maybe_dup))
   13331        22591 :             DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
   13332        22995 :           tentative_decl_linkage (decl);
   13333        22995 :           if (DECL_EXPLICIT_INSTANTIATION (decl)
   13334        22995 :               && !DECL_EXTERNAL (decl))
   13335            9 :             setup_explicit_instantiation_definition_linkage (decl);
   13336              :           /* Class non-template static members are handled in read_class_def.
   13337              :              But still handle specialisations of member templates.  */
   13338        45990 :           if ((!DECL_CLASS_SCOPE_P (decl)
   13339        14906 :                || primary_template_specialization_p (decl))
   13340        31203 :               && (DECL_IMPLICIT_INSTANTIATION (decl)
   13341         8131 :                   || (DECL_EXPLICIT_INSTANTIATION (decl)
   13342           21 :                       && !DECL_EXTERNAL (decl))))
   13343           86 :             note_vague_linkage_variable (decl);
   13344              :         }
   13345        26065 :       if (!dyn_init)
   13346              :         ;
   13347          216 :       else if (CP_DECL_THREAD_LOCAL_P (decl))
   13348           96 :         tls_aggregates = tree_cons (dyn_init, decl, tls_aggregates);
   13349              :       else
   13350          120 :         static_aggregates = tree_cons (dyn_init, decl, static_aggregates);
   13351              :     }
   13352              :   else if (maybe_dup)
   13353              :     {
   13354              :       // FIXME:QOI Check matching defn
   13355              :     }
   13356              : 
   13357              :   return true;
   13358              : }
   13359              : 
   13360              : /* If MEMBER doesn't have an independent life outside the class,
   13361              :    return it (or its TEMPLATE_DECL).  Otherwise NULL.  */
   13362              : 
   13363              : static tree
   13364       200268 : member_owned_by_class (tree member)
   13365              : {
   13366       200268 :   gcc_assert (DECL_P (member));
   13367              : 
   13368              :   /* Clones are owned by their origin.  */
   13369       200268 :   if (DECL_CLONED_FUNCTION_P (member))
   13370              :     return NULL;
   13371              : 
   13372       200268 :   if (TREE_CODE (member) == FIELD_DECL)
   13373              :     /* FIELD_DECLS can have template info in some cases.  We always
   13374              :        want the FIELD_DECL though, as there's never a TEMPLATE_DECL
   13375              :        wrapping them.  */
   13376              :     return member;
   13377              : 
   13378        91517 :   int use_tpl = -1;
   13379        91517 :   if (tree ti = node_template_info (member, use_tpl))
   13380              :     {
   13381              :       // FIXME: Don't bail on things that CANNOT have their own
   13382              :       // template header.  No, make sure they're in the same cluster.
   13383            0 :       if (use_tpl > 0)
   13384              :         return NULL_TREE;
   13385              : 
   13386            0 :       if (DECL_TEMPLATE_RESULT (TI_TEMPLATE (ti)) == member)
   13387       200268 :         member = TI_TEMPLATE (ti);
   13388              :     }
   13389              :   return member;
   13390              : }
   13391              : 
   13392              : void
   13393       150366 : trees_out::write_class_def (tree defn)
   13394              : {
   13395       150366 :   gcc_assert (DECL_P (defn));
   13396       150366 :   if (streaming_p ())
   13397        75577 :     dump () && dump ("Writing class definition %N", defn);
   13398              : 
   13399       150366 :   tree type = TREE_TYPE (defn);
   13400       150366 :   tree_node (TYPE_SIZE (type));
   13401       150366 :   tree_node (TYPE_SIZE_UNIT (type));
   13402       150366 :   tree_node (TYPE_VFIELD (type));
   13403       150366 :   tree_node (TYPE_BINFO (type));
   13404              : 
   13405       150366 :   vec_chained_decls (TYPE_FIELDS (type));
   13406              : 
   13407              :   /* Every class but __as_base has a type-specific.  */
   13408       298266 :   gcc_checking_assert (!TYPE_LANG_SPECIFIC (type) == IS_FAKE_BASE_TYPE (type));
   13409              : 
   13410       150366 :   if (TYPE_LANG_SPECIFIC (type))
   13411              :     {
   13412       147900 :       {
   13413       147900 :         vec<tree, va_gc> *v = CLASSTYPE_MEMBER_VEC (type);
   13414       147900 :         if (!v)
   13415              :           {
   13416        38033 :             gcc_checking_assert (!streaming_p ());
   13417              :             /* Force a class vector.  */
   13418        38033 :             v = set_class_bindings (type, -1);
   13419        38033 :             gcc_checking_assert (v);
   13420              :           }
   13421              : 
   13422       147900 :         unsigned len = v->length ();
   13423       147900 :         if (streaming_p ())
   13424        73927 :           u (len);
   13425      1187508 :         for (unsigned ix = 0; ix != len; ix++)
   13426              :           {
   13427      1039608 :             tree m = (*v)[ix];
   13428      1039608 :             if (TREE_CODE (m) == TYPE_DECL
   13429       311445 :                 && DECL_ARTIFICIAL (m)
   13430      1199279 :                 && TYPE_STUB_DECL (TREE_TYPE (m)) == m)
   13431              :               /* This is a using-decl for a type, or an anonymous
   13432              :                  struct (maybe with a typedef name).  Write the type.  */
   13433        10868 :               m = TREE_TYPE (m);
   13434      1039608 :             tree_node (m);
   13435              :           }
   13436              :       }
   13437       147900 :       tree_node (CLASSTYPE_LAMBDA_EXPR (type));
   13438              : 
   13439              :       /* TYPE_CONTAINS_VPTR_P looks at the vbase vector, which the
   13440              :          reader won't know at this point.  */
   13441       147900 :       int has_vptr = TYPE_CONTAINS_VPTR_P (type);
   13442              : 
   13443       147900 :       if (streaming_p ())
   13444              :         {
   13445        73927 :           unsigned nvbases = vec_safe_length (CLASSTYPE_VBASECLASSES (type));
   13446        73927 :           u (nvbases);
   13447        73927 :           i (has_vptr);
   13448              :         }
   13449              : 
   13450       147900 :       if (has_vptr)
   13451              :         {
   13452         5256 :           tree_vec (CLASSTYPE_PURE_VIRTUALS (type));
   13453         5256 :           tree_pair_vec (CLASSTYPE_VCALL_INDICES (type));
   13454         5256 :           tree_node (CLASSTYPE_KEY_METHOD (type));
   13455              :         }
   13456              :     }
   13457              : 
   13458       150366 :   if (TYPE_LANG_SPECIFIC (type))
   13459              :     {
   13460       147900 :       tree_node (CLASSTYPE_PRIMARY_BINFO (type));
   13461              : 
   13462       147900 :       tree as_base = CLASSTYPE_AS_BASE (type);
   13463       147900 :       if (as_base)
   13464        76665 :         as_base = TYPE_NAME (as_base);
   13465       147900 :       tree_node (as_base);
   13466              : 
   13467              :       /* Write the vtables.  */
   13468       147900 :       tree vtables = CLASSTYPE_VTABLES (type);
   13469       147900 :       vec_chained_decls (vtables);
   13470       301918 :       for (; vtables; vtables = TREE_CHAIN (vtables))
   13471         6118 :         write_definition (vtables);
   13472              : 
   13473       147900 :       {
   13474              :         /* Friend declarations in class definitions are ignored when
   13475              :            determining exposures.  */
   13476       147900 :         auto ovr = dep_hash->ignore_exposure_if (true);
   13477              : 
   13478              :         /* Write the friend classes.  */
   13479       147900 :         tree_list (CLASSTYPE_FRIEND_CLASSES (type), false);
   13480              : 
   13481              :         /* Write the friend functions.  */
   13482       147900 :         for (tree friends = DECL_FRIENDLIST (defn);
   13483       168078 :              friends; friends = TREE_CHAIN (friends))
   13484              :           {
   13485        20178 :             tree_node (FRIEND_NAME (friends));
   13486        20178 :             tree_list (FRIEND_DECLS (friends), false);
   13487              :           }
   13488              :         /* End of friend fns.  */
   13489       147900 :         tree_node (NULL_TREE);
   13490       147900 :       }
   13491              : 
   13492              :       /* Write the decl list.  We don't need to ignore exposures of friend
   13493              :          decls here as any such decls should already have been added and
   13494              :          ignored above.  */
   13495       147900 :       tree_list (CLASSTYPE_DECL_LIST (type), true);
   13496              : 
   13497       147900 :       if (TYPE_CONTAINS_VPTR_P (type))
   13498              :         {
   13499              :           /* Write the thunks.  */
   13500         5256 :           for (tree decls = TYPE_FIELDS (type);
   13501       147578 :                decls; decls = DECL_CHAIN (decls))
   13502       142322 :             if (TREE_CODE (decls) == FUNCTION_DECL
   13503       103296 :                 && DECL_VIRTUAL_P (decls)
   13504       169386 :                 && DECL_THUNKS (decls))
   13505              :               {
   13506          888 :                 tree_node (decls);
   13507              :                 /* Thunks are always unique, so chaining is ok.  */
   13508          888 :                 chained_decls (DECL_THUNKS (decls));
   13509              :               }
   13510         5256 :           tree_node (NULL_TREE);
   13511              :         }
   13512              :     }
   13513       150366 : }
   13514              : 
   13515              : void
   13516       200268 : trees_out::mark_class_member (tree member, bool do_defn)
   13517              : {
   13518       200268 :   gcc_assert (DECL_P (member));
   13519              : 
   13520       200268 :   member = member_owned_by_class (member);
   13521       200268 :   if (member)
   13522       400536 :     mark_declaration (member, do_defn && has_definition (member));
   13523       200268 : }
   13524              : 
   13525              : void
   13526       150394 : trees_out::mark_class_def (tree defn)
   13527              : {
   13528       150394 :   gcc_assert (DECL_P (defn));
   13529       150394 :   tree type = TREE_TYPE (defn);
   13530              :   /* Mark the class members that are not type-decls and cannot have
   13531              :      independent definitions.  */
   13532      1619866 :   for (tree member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
   13533      1469472 :     if (TREE_CODE (member) == FIELD_DECL
   13534      1469472 :         || TREE_CODE (member) == USING_DECL
   13535              :         /* A cloned enum-decl from 'using enum unrelated;'   */
   13536      1469472 :         || (TREE_CODE (member) == CONST_DECL
   13537        14222 :             && DECL_CONTEXT (member) == type))
   13538              :       {
   13539       200268 :         mark_class_member (member);
   13540       200268 :         if (TREE_CODE (member) == FIELD_DECL)
   13541       108751 :           if (tree repr = DECL_BIT_FIELD_REPRESENTATIVE (member))
   13542              :             /* If we're marking a class template definition, then
   13543              :                this'll contain the width (as set by grokbitfield)
   13544              :                instead of a decl.  */
   13545         2242 :             if (DECL_P (repr))
   13546         1800 :               mark_declaration (repr, false);
   13547              :       }
   13548              : 
   13549              :   /* Mark the binfo hierarchy.  */
   13550       355536 :   for (tree child = TYPE_BINFO (type); child; child = TREE_CHAIN (child))
   13551       205142 :     mark_by_value (child);
   13552              : 
   13553       150394 :   if (TYPE_LANG_SPECIFIC (type))
   13554              :     {
   13555       147904 :       for (tree vtable = CLASSTYPE_VTABLES (type);
   13556       154022 :            vtable; vtable = TREE_CHAIN (vtable))
   13557         6118 :         mark_declaration (vtable, true);
   13558              : 
   13559       147904 :       if (TYPE_CONTAINS_VPTR_P (type))
   13560              :         /* Mark the thunks, they belong to the class definition,
   13561              :            /not/ the thunked-to function.  */
   13562         5256 :         for (tree decls = TYPE_FIELDS (type);
   13563       147578 :              decls; decls = DECL_CHAIN (decls))
   13564       142322 :           if (TREE_CODE (decls) == FUNCTION_DECL)
   13565       103296 :             for (tree thunks = DECL_THUNKS (decls);
   13566       104472 :                  thunks; thunks = DECL_CHAIN (thunks))
   13567         1176 :               mark_declaration (thunks, false);
   13568              :     }
   13569       150394 : }
   13570              : 
   13571              : /* Nop sorting, needed for resorting the member vec.  */
   13572              : 
   13573              : static void
   13574      9743900 : nop (void *, void *, void *)
   13575              : {
   13576      9743900 : }
   13577              : 
   13578              : bool
   13579        63522 : trees_in::read_class_def (tree defn, tree maybe_template)
   13580              : {
   13581        63522 :   gcc_assert (DECL_P (defn));
   13582        64149 :   dump () && dump ("Reading class definition %N", defn);
   13583        63522 :   tree type = TREE_TYPE (defn);
   13584        63522 :   tree size = tree_node ();
   13585        63522 :   tree size_unit = tree_node ();
   13586        63522 :   tree vfield = tree_node ();
   13587        63522 :   tree binfo = tree_node ();
   13588        63522 :   vec<tree, va_gc> *vbase_vec = NULL;
   13589        63522 :   vec<tree, va_gc> *member_vec = NULL;
   13590        63522 :   vec<tree, va_gc> *pure_virts = NULL;
   13591        63522 :   vec<tree_pair_s, va_gc> *vcall_indices = NULL;
   13592        63522 :   tree key_method = NULL_TREE;
   13593        63522 :   tree lambda = NULL_TREE;
   13594              : 
   13595              :   /* Read the fields.  */
   13596        63522 :   vec<tree, va_heap> *fields = vec_chained_decls ();
   13597              : 
   13598        63522 :   if (TYPE_LANG_SPECIFIC (type))
   13599              :     {
   13600        62220 :       if (unsigned len = u ())
   13601              :         {
   13602        62220 :           vec_alloc (member_vec, len);
   13603       554578 :           for (unsigned ix = 0; ix != len; ix++)
   13604              :             {
   13605       492358 :               tree m = tree_node ();
   13606       492358 :               if (get_overrun ())
   13607              :                 break;
   13608       492358 :               if (TYPE_P (m))
   13609         5008 :                 m = TYPE_STUB_DECL (m);
   13610       492358 :               member_vec->quick_push (m);
   13611              :             }
   13612              :         }
   13613        62220 :       lambda = tree_node ();
   13614              : 
   13615        62220 :       if (!get_overrun ())
   13616              :         {
   13617        62220 :           unsigned nvbases = u ();
   13618        62220 :           if (nvbases)
   13619              :             {
   13620          269 :               vec_alloc (vbase_vec, nvbases);
   13621         1232 :               for (tree child = binfo; child; child = TREE_CHAIN (child))
   13622          963 :                 if (BINFO_VIRTUAL_P (child))
   13623          269 :                   vbase_vec->quick_push (child);
   13624              :             }
   13625              :         }
   13626              : 
   13627        62220 :       if (!get_overrun ())
   13628              :         {
   13629        62220 :           int has_vptr = i ();
   13630        62220 :           if (has_vptr)
   13631              :             {
   13632         2497 :               pure_virts = tree_vec ();
   13633         2497 :               vcall_indices = tree_pair_vec ();
   13634         2497 :               key_method = tree_node ();
   13635              :             }
   13636              :         }
   13637              :     }
   13638              : 
   13639        63522 :   tree maybe_dup = odr_duplicate (maybe_template, TYPE_SIZE (type));
   13640        63522 :   bool installing = maybe_dup && !TYPE_SIZE (type);
   13641        33664 :   if (installing)
   13642              :     {
   13643        33664 :       if (maybe_dup != defn)
   13644              :         {
   13645              :           // FIXME: This is needed on other defns too, almost
   13646              :           // duplicate-decl like?  See is_matching_decl too.
   13647              :           /* Copy flags from the duplicate.  */
   13648          254 :           tree type_dup = TREE_TYPE (maybe_dup);
   13649              : 
   13650              :           /* Core pieces.  */
   13651          254 :           TYPE_MODE_RAW (type) = TYPE_MODE_RAW (type_dup);
   13652          254 :           TYPE_ALIGN_RAW (type) = TYPE_ALIGN_RAW (type_dup);
   13653          508 :           TYPE_WARN_IF_NOT_ALIGN_RAW (type)
   13654          254 :             = TYPE_WARN_IF_NOT_ALIGN_RAW (type_dup);
   13655          254 :           TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (type_dup);
   13656              : 
   13657          254 :           SET_DECL_MODE (defn, DECL_MODE (maybe_dup));
   13658          254 :           DECL_SIZE (defn) = DECL_SIZE (maybe_dup);
   13659          254 :           DECL_SIZE_UNIT (defn) = DECL_SIZE_UNIT (maybe_dup);
   13660          254 :           DECL_ALIGN_RAW (defn) = DECL_ALIGN_RAW (maybe_dup);
   13661          508 :           DECL_WARN_IF_NOT_ALIGN_RAW (defn)
   13662          254 :             = DECL_WARN_IF_NOT_ALIGN_RAW (maybe_dup);
   13663          254 :           DECL_USER_ALIGN (defn) = DECL_USER_ALIGN (maybe_dup);
   13664              : 
   13665          254 :           TYPE_TYPELESS_STORAGE (type) = TYPE_TYPELESS_STORAGE (type_dup);
   13666          254 :           TYPE_CXX_ODR_P (type) = TYPE_CXX_ODR_P (type_dup);
   13667          254 :           TYPE_NO_FORCE_BLK (type) = TYPE_NO_FORCE_BLK (type_dup);
   13668          254 :           TYPE_TRANSPARENT_AGGR (type) = TYPE_TRANSPARENT_AGGR (type_dup);
   13669          508 :           TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type)
   13670          254 :             = TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type_dup);
   13671              : 
   13672          254 :           TYPE_EMPTY_P (type) = TYPE_EMPTY_P (type_dup);
   13673          254 :           TREE_ADDRESSABLE (type) = TREE_ADDRESSABLE (type_dup);
   13674              : 
   13675              :           /* C++ pieces.  */
   13676          254 :           TYPE_POLYMORPHIC_P (type) = TYPE_POLYMORPHIC_P (type_dup);
   13677          254 :           CLASSTYPE_FINAL (type) = CLASSTYPE_FINAL (type_dup);
   13678              : 
   13679          508 :           TYPE_HAS_USER_CONSTRUCTOR (type)
   13680          254 :             = TYPE_HAS_USER_CONSTRUCTOR (type_dup);
   13681          508 :           TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
   13682          254 :             = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type_dup);
   13683          508 :           TYPE_NEEDS_CONSTRUCTING (type)
   13684          254 :             = TYPE_NEEDS_CONSTRUCTING (type_dup);
   13685              : 
   13686          254 :           if (auto ls = TYPE_LANG_SPECIFIC (type_dup))
   13687              :             {
   13688          254 :               if (TYPE_LANG_SPECIFIC (type))
   13689              :                 {
   13690          762 :                   CLASSTYPE_BEFRIENDING_CLASSES (type_dup)
   13691          254 :                     = CLASSTYPE_BEFRIENDING_CLASSES (type);
   13692          254 :                   if (!ANON_AGGR_TYPE_P (type))
   13693          762 :                     CLASSTYPE_TYPEINFO_VAR (type_dup)
   13694          254 :                       = CLASSTYPE_TYPEINFO_VAR (type);
   13695              :                 }
   13696         1151 :               for (tree v = type; v; v = TYPE_NEXT_VARIANT (v))
   13697          897 :                 TYPE_LANG_SPECIFIC (v) = ls;
   13698              :             }
   13699              :         }
   13700              : 
   13701        33664 :       TYPE_SIZE (type) = size;
   13702        33664 :       TYPE_SIZE_UNIT (type) = size_unit;
   13703              : 
   13704        33664 :       if (fields)
   13705              :         {
   13706        33664 :           tree *chain = &TYPE_FIELDS (type);
   13707        33664 :           unsigned len = fields->length ();
   13708       427651 :           for (unsigned ix = 0; ix != len; ix++)
   13709              :             {
   13710       393987 :               tree decl = (*fields)[ix];
   13711              : 
   13712       393987 :               if (!decl)
   13713              :                 {
   13714              :                   /* An anonymous struct with typedef name.  */
   13715            3 :                   tree tdef = (*fields)[ix+1];
   13716            3 :                   decl = TYPE_STUB_DECL (TREE_TYPE (tdef));
   13717            3 :                   gcc_checking_assert (IDENTIFIER_ANON_P (DECL_NAME (decl))
   13718              :                                        && decl != tdef);
   13719              :                 }
   13720              : 
   13721       711823 :               gcc_checking_assert (!*chain == !DECL_CLONED_FUNCTION_P (decl));
   13722       393987 :               *chain = decl;
   13723       393987 :               chain = &DECL_CHAIN (decl);
   13724              : 
   13725       393987 :               if (TREE_CODE (decl) == FIELD_DECL
   13726       393987 :                   && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
   13727              :                 {
   13728          261 :                   tree anon_type = TYPE_MAIN_VARIANT (TREE_TYPE (decl));
   13729          261 :                   if (DECL_NAME (defn) == as_base_identifier)
   13730              :                     /* ANON_AGGR_TYPE_FIELD should already point to the
   13731              :                        original FIELD_DECL; don't overwrite it to point
   13732              :                        to the as-base FIELD_DECL copy.  */
   13733           13 :                     gcc_checking_assert (ANON_AGGR_TYPE_FIELD (anon_type));
   13734              :                   else
   13735          248 :                     ANON_AGGR_TYPE_FIELD (anon_type) = decl;
   13736              :                 }
   13737              : 
   13738       393987 :               if (TREE_CODE (decl) == USING_DECL
   13739       393987 :                   && TREE_CODE (USING_DECL_SCOPE (decl)) == RECORD_TYPE)
   13740              :                 {
   13741              :                   /* Reconstruct DECL_ACCESS.  */
   13742        16391 :                   tree decls = USING_DECL_DECLS (decl);
   13743        16391 :                   tree access = declared_access (decl);
   13744              : 
   13745        19381 :                   for (ovl_iterator iter (decls); iter; ++iter)
   13746              :                     {
   13747         1925 :                       tree d = *iter;
   13748              : 
   13749         1925 :                       retrofit_lang_decl (d);
   13750         1925 :                       tree list = DECL_ACCESS (d);
   13751              : 
   13752         1925 :                       if (!purpose_member (type, list))
   13753         2313 :                         DECL_ACCESS (d) = tree_cons (type, access, list);
   13754              :                     }
   13755              :                 }
   13756              : 
   13757       393987 :               if (TREE_CODE (decl) == VAR_DECL
   13758        12389 :                   && TREE_CODE (maybe_template) != TEMPLATE_DECL)
   13759        10764 :                 note_vague_linkage_variable (decl);
   13760              :             }
   13761              :         }
   13762              : 
   13763        33664 :       TYPE_VFIELD (type) = vfield;
   13764        33664 :       TYPE_BINFO (type) = binfo;
   13765              : 
   13766        33664 :       if (TYPE_LANG_SPECIFIC (type))
   13767              :         {
   13768        32876 :           if (!TYPE_POLYMORPHIC_P (type))
   13769        31477 :             SET_CLASSTYPE_LAMBDA_EXPR (type, lambda);
   13770              :           else
   13771         1399 :             gcc_checking_assert (lambda == NULL_TREE);
   13772              : 
   13773        32876 :           CLASSTYPE_MEMBER_VEC (type) = member_vec;
   13774        32876 :           CLASSTYPE_PURE_VIRTUALS (type) = pure_virts;
   13775        32876 :           CLASSTYPE_VCALL_INDICES (type) = vcall_indices;
   13776              : 
   13777        32876 :           if (TYPE_POLYMORPHIC_P (type))
   13778         1399 :             SET_CLASSTYPE_KEY_METHOD (type, key_method);
   13779              :           else
   13780        31477 :             gcc_checking_assert (key_method == NULL_TREE);
   13781              : 
   13782        32876 :           CLASSTYPE_VBASECLASSES (type) = vbase_vec;
   13783              : 
   13784              :           /* Resort the member vector.  */
   13785        32876 :           resort_type_member_vec (member_vec, NULL, nop, NULL);
   13786              :         }
   13787              :     }
   13788              :   else if (maybe_dup)
   13789              :     {
   13790              :       // FIXME:QOI Check matching defn
   13791              :     }
   13792              : 
   13793        63522 :   if (TYPE_LANG_SPECIFIC (type))
   13794              :     {
   13795        62220 :       tree primary = tree_node ();
   13796        62220 :       tree as_base = tree_node ();
   13797              : 
   13798        62220 :       if (as_base)
   13799        32149 :         as_base = TREE_TYPE (as_base);
   13800              : 
   13801              :       /* Read the vtables.  */
   13802        62220 :       vec<tree, va_heap> *vtables = vec_chained_decls ();
   13803        62220 :       if (vtables)
   13804              :         {
   13805         2459 :           unsigned len = vtables->length ();
   13806         5379 :           for (unsigned ix = 0; ix != len; ix++)
   13807              :             {
   13808         2920 :               tree vtable = (*vtables)[ix];
   13809         2920 :               read_var_def (vtable, vtable);
   13810              :             }
   13811              :         }
   13812              : 
   13813        62220 :       tree friend_classes = tree_list (false);
   13814        62220 :       tree friend_functions = NULL_TREE;
   13815        62220 :       for (tree *chain = &friend_functions;
   13816        73432 :            tree name = tree_node (); chain = &TREE_CHAIN (*chain))
   13817              :         {
   13818        11212 :           tree val = tree_list (false);
   13819        11212 :           *chain = build_tree_list (name, val);
   13820        11212 :         }
   13821        62220 :       tree decl_list = tree_list (true);
   13822              : 
   13823        62220 :       if (installing)
   13824              :         {
   13825        32876 :           CLASSTYPE_PRIMARY_BINFO (type) = primary;
   13826        32876 :           CLASSTYPE_AS_BASE (type) = as_base;
   13827              : 
   13828        32876 :           if (vtables)
   13829              :             {
   13830         1411 :               if ((!CLASSTYPE_KEY_METHOD (type)
   13831              :                    /* Sneaky user may have defined it inline
   13832              :                       out-of-class.  */
   13833         1004 :                    || DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (type)))
   13834              :                   /* An imported non-template class attached to a module
   13835              :                      doesn't need to have its vtables emitted here.  */
   13836         1562 :                   && (CLASSTYPE_USE_TEMPLATE (type)
   13837          274 :                       || !DECL_MODULE_ATTACH_P (defn)))
   13838         1012 :                 vec_safe_push (keyed_classes, type);
   13839         1411 :               unsigned len = vtables->length ();
   13840         1411 :               tree *chain = &CLASSTYPE_VTABLES (type);
   13841         3100 :               for (unsigned ix = 0; ix != len; ix++)
   13842              :                 {
   13843         1689 :                   tree vtable = (*vtables)[ix];
   13844         1689 :                   gcc_checking_assert (!*chain);
   13845         1689 :                   *chain = vtable;
   13846         1689 :                   chain = &DECL_CHAIN (vtable);
   13847              :                 }
   13848              :             }
   13849        32876 :           CLASSTYPE_FRIEND_CLASSES (type) = friend_classes;
   13850        32876 :           DECL_FRIENDLIST (defn) = friend_functions;
   13851        32876 :           CLASSTYPE_DECL_LIST (type) = decl_list;
   13852              : 
   13853        35515 :           for (; friend_classes; friend_classes = TREE_CHAIN (friend_classes))
   13854              :             {
   13855         2639 :               tree f = TREE_VALUE (friend_classes);
   13856         2639 :               if (TREE_CODE (f) == TEMPLATE_DECL)
   13857         1077 :                 f = TREE_TYPE (f);
   13858              : 
   13859         2639 :               if (CLASS_TYPE_P (f))
   13860              :                 {
   13861         2601 :                   CLASSTYPE_BEFRIENDING_CLASSES (f)
   13862         5202 :                     = tree_cons (NULL_TREE, type,
   13863         2601 :                                  CLASSTYPE_BEFRIENDING_CLASSES (f));
   13864         2645 :                   dump () && dump ("Class %N befriending %C:%N",
   13865            6 :                                    type, TREE_CODE (f), f);
   13866              :                 }
   13867              :             }
   13868              : 
   13869        39242 :           for (; friend_functions;
   13870         6366 :                friend_functions = TREE_CHAIN (friend_functions))
   13871         6366 :             for (tree friend_decls = TREE_VALUE (friend_functions);
   13872        14554 :                  friend_decls; friend_decls = TREE_CHAIN (friend_decls))
   13873              :               {
   13874         8188 :                 tree f = TREE_VALUE (friend_decls);
   13875         8188 :                 if (TREE_CODE (f) == TU_LOCAL_ENTITY)
   13876           36 :                   continue;
   13877              : 
   13878         8152 :                 DECL_BEFRIENDING_CLASSES (f)
   13879         8152 :                   = tree_cons (NULL_TREE, type, DECL_BEFRIENDING_CLASSES (f));
   13880         8215 :                 dump () && dump ("Class %N befriending %C:%N",
   13881           27 :                                  type, TREE_CODE (f), f);
   13882              :               }
   13883              :         }
   13884              : 
   13885        62220 :       if (TYPE_CONTAINS_VPTR_P (type))
   13886              :         /* Read and install the thunks.  */
   13887         2911 :         while (tree vfunc = tree_node ())
   13888              :           {
   13889          414 :             tree thunks = chained_decls ();
   13890          414 :             if (installing)
   13891          246 :               SET_DECL_THUNKS (vfunc, thunks);
   13892              :           }
   13893              : 
   13894        62220 :       vec_free (vtables);
   13895              :     }
   13896              : 
   13897              :   /* Propagate to all variants.  */
   13898        63522 :   if (installing)
   13899        33664 :     fixup_type_variants (type);
   13900              : 
   13901              :   /* IS_FAKE_BASE_TYPE is inaccurate at this point, because if this is
   13902              :      the fake base, we've not hooked it into the containing class's
   13903              :      data structure yet.  Fortunately it has a unique name.  */
   13904        33664 :   if (installing
   13905        33664 :       && DECL_NAME (defn) != as_base_identifier
   13906        32876 :       && (!CLASSTYPE_TEMPLATE_INFO (type)
   13907        27771 :           || !uses_template_parms (TI_ARGS (CLASSTYPE_TEMPLATE_INFO (type)))))
   13908              :     /* Emit debug info.  It'd be nice to know if the interface TU
   13909              :        already emitted this.  */
   13910        18541 :     rest_of_type_compilation (type, !LOCAL_CLASS_P (type));
   13911              : 
   13912        63522 :   vec_free (fields);
   13913              : 
   13914        63522 :   return !get_overrun ();
   13915              : }
   13916              : 
   13917              : void
   13918         7728 : trees_out::write_enum_def (tree decl)
   13919              : {
   13920         7728 :   tree type = TREE_TYPE (decl);
   13921              : 
   13922         7728 :   tree_node (TYPE_VALUES (type));
   13923              :   /* Note that we stream TYPE_MIN/MAX_VALUE directly as part of the
   13924              :      ENUMERAL_TYPE.  */
   13925         7728 : }
   13926              : 
   13927              : void
   13928         7728 : trees_out::mark_enum_def (tree decl)
   13929              : {
   13930         7728 :   tree type = TREE_TYPE (decl);
   13931              : 
   13932        39956 :   for (tree values = TYPE_VALUES (type); values; values = TREE_CHAIN (values))
   13933              :     {
   13934        32228 :       tree cst = TREE_VALUE (values);
   13935        32228 :       mark_by_value (cst);
   13936              :       /* We must mark the init to avoid circularity in tt_enum_int.  */
   13937        32228 :       if (tree init = DECL_INITIAL (cst))
   13938        31972 :         if (TREE_CODE (init) == INTEGER_CST)
   13939        31336 :           mark_by_value (init);
   13940              :     }
   13941         7728 : }
   13942              : 
   13943              : bool
   13944         2927 : trees_in::read_enum_def (tree defn, tree maybe_template)
   13945              : {
   13946         2927 :   tree type = TREE_TYPE (defn);
   13947         2927 :   tree values = tree_node ();
   13948              : 
   13949         2927 :   if (get_overrun ())
   13950              :     return false;
   13951              : 
   13952         2927 :   tree maybe_dup = odr_duplicate (maybe_template, TYPE_VALUES (type));
   13953         5854 :   bool installing = maybe_dup && !TYPE_VALUES (type);
   13954              : 
   13955         2927 :   if (installing)
   13956              :     {
   13957         1408 :       TYPE_VALUES (type) = values;
   13958              :       /* Note that we stream TYPE_MIN/MAX_VALUE directly as part of the
   13959              :          ENUMERAL_TYPE.  */
   13960              : 
   13961         2284 :       rest_of_type_compilation (type, DECL_NAMESPACE_SCOPE_P (defn));
   13962              :     }
   13963         1519 :   else if (maybe_dup)
   13964              :     {
   13965         1519 :       tree known = TYPE_VALUES (type);
   13966         8472 :       for (; known && values;
   13967         6953 :            known = TREE_CHAIN (known), values = TREE_CHAIN (values))
   13968              :         {
   13969         6962 :           tree known_decl = TREE_VALUE (known);
   13970         6962 :           tree new_decl = TREE_VALUE (values);
   13971              : 
   13972         6962 :           if (DECL_NAME (known_decl) != DECL_NAME (new_decl))
   13973              :             break;
   13974              : 
   13975         6956 :           new_decl = maybe_duplicate (new_decl);
   13976              : 
   13977         6956 :           if (!cp_tree_equal (DECL_INITIAL (known_decl),
   13978         6956 :                               DECL_INITIAL (new_decl)))
   13979              :             break;
   13980              :         }
   13981              : 
   13982         1519 :       if (known || values)
   13983              :         {
   13984           12 :           auto_diagnostic_group d;
   13985           12 :           error_at (DECL_SOURCE_LOCATION (maybe_dup),
   13986              :                     "definition of %qD does not match", maybe_dup);
   13987           12 :           inform (DECL_SOURCE_LOCATION (defn),
   13988              :                   "existing definition %qD", defn);
   13989              : 
   13990           12 :           tree known_decl = NULL_TREE, new_decl = NULL_TREE;
   13991              : 
   13992           12 :           if (known)
   13993            9 :             known_decl = TREE_VALUE (known);
   13994           12 :           if (values)
   13995           12 :             new_decl = maybe_duplicate (TREE_VALUE (values));
   13996              : 
   13997           12 :           if (known_decl && new_decl)
   13998              :             {
   13999            9 :               inform (DECL_SOURCE_LOCATION (new_decl),
   14000              :                       "enumerator %qD does not match ...", new_decl);
   14001            9 :               inform (DECL_SOURCE_LOCATION (known_decl),
   14002              :                       "... this enumerator %qD", known_decl);
   14003              :             }
   14004            3 :           else if (known_decl || new_decl)
   14005              :             {
   14006            3 :               tree extra = known_decl ? known_decl : new_decl;
   14007            3 :               inform (DECL_SOURCE_LOCATION (extra),
   14008              :                       "additional enumerators beginning with %qD", extra);
   14009              :             }
   14010              :           else
   14011            0 :             inform (DECL_SOURCE_LOCATION (maybe_dup),
   14012              :                     "enumeration range differs");
   14013              : 
   14014              :           /* Mark it bad.  */
   14015           12 :           unmatched_duplicate (maybe_template);
   14016           12 :         }
   14017              :     }
   14018              : 
   14019              :   return true;
   14020              : }
   14021              : 
   14022              : /* Write out the body of DECL.  See above circularity note.  */
   14023              : 
   14024              : void
   14025       672409 : trees_out::write_definition (tree decl, bool refs_tu_local)
   14026              : {
   14027       672409 :   auto ovr = make_temp_override (writing_local_entities,
   14028       672409 :                                  writing_local_entities || refs_tu_local);
   14029              : 
   14030       672409 :   if (streaming_p ())
   14031              :     {
   14032       336114 :       assert_definition (decl);
   14033       336114 :       dump ()
   14034          949 :         && dump ("Writing definition %C:%N", TREE_CODE (decl), decl);
   14035              :     }
   14036              :   else
   14037       336295 :     dump (dumper::DEPEND)
   14038           96 :       && dump ("Depending definition %C:%N", TREE_CODE (decl), decl);
   14039              : 
   14040      1042698 :  again:
   14041      1042698 :   switch (TREE_CODE (decl))
   14042              :     {
   14043            0 :     default:
   14044            0 :       gcc_unreachable ();
   14045              : 
   14046       370289 :     case TEMPLATE_DECL:
   14047       370289 :       decl = DECL_TEMPLATE_RESULT (decl);
   14048       370289 :       goto again;
   14049              : 
   14050       402747 :     case FUNCTION_DECL:
   14051       402747 :       write_function_def (decl);
   14052       402747 :       break;
   14053              : 
   14054       158094 :     case TYPE_DECL:
   14055       158094 :       {
   14056       158094 :         tree type = TREE_TYPE (decl);
   14057       158094 :         gcc_assert (TYPE_MAIN_VARIANT (type) == type
   14058              :                     && TYPE_NAME (type) == decl);
   14059       158094 :         if (TREE_CODE (type) == ENUMERAL_TYPE)
   14060         7728 :           write_enum_def (decl);
   14061              :         else
   14062       150366 :           write_class_def (decl);
   14063              :       }
   14064              :       break;
   14065              : 
   14066       111568 :     case VAR_DECL:
   14067       111568 :     case CONCEPT_DECL:
   14068       111568 :       write_var_def (decl);
   14069       111568 :       break;
   14070              :     }
   14071       672409 : }
   14072              : 
   14073              : /* Mark a declaration for by-value walking.  If DO_DEFN is true, mark
   14074              :    its body too.  */
   14075              : 
   14076              : void
   14077      2819489 : trees_out::mark_declaration (tree decl, bool do_defn)
   14078              : {
   14079      2819489 :   mark_by_value (decl);
   14080              : 
   14081      2819489 :   if (TREE_CODE (decl) == TEMPLATE_DECL)
   14082       916998 :     decl = DECL_TEMPLATE_RESULT (decl);
   14083              : 
   14084      2819489 :   if (!do_defn)
   14085              :     return;
   14086              : 
   14087       672431 :   switch (TREE_CODE (decl))
   14088              :     {
   14089            0 :     default:
   14090            0 :       gcc_unreachable ();
   14091              : 
   14092              :     case FUNCTION_DECL:
   14093              :       mark_function_def (decl);
   14094              :       break;
   14095              : 
   14096       158122 :     case TYPE_DECL:
   14097       158122 :       {
   14098       158122 :         tree type = TREE_TYPE (decl);
   14099       158122 :         gcc_assert (TYPE_MAIN_VARIANT (type) == type
   14100              :                     && TYPE_NAME (type) == decl);
   14101       158122 :         if (TREE_CODE (type) == ENUMERAL_TYPE)
   14102         7728 :           mark_enum_def (decl);
   14103              :         else
   14104       150394 :           mark_class_def (decl);
   14105              :       }
   14106              :       break;
   14107              : 
   14108              :     case VAR_DECL:
   14109              :     case CONCEPT_DECL:
   14110              :       mark_var_def (decl);
   14111              :       break;
   14112              :     }
   14113              : }
   14114              : 
   14115              : /* Read in the body of DECL.  See above circularity note.  */
   14116              : 
   14117              : bool
   14118       312444 : trees_in::read_definition (tree decl)
   14119              : {
   14120       313738 :   dump () && dump ("Reading definition %C %N", TREE_CODE (decl), decl);
   14121              : 
   14122              :   tree maybe_template = decl;
   14123              : 
   14124       312444 :  again:
   14125       496958 :   switch (TREE_CODE (decl))
   14126              :     {
   14127              :     default:
   14128              :       break;
   14129              : 
   14130       184514 :     case TEMPLATE_DECL:
   14131       184514 :       decl = DECL_TEMPLATE_RESULT (decl);
   14132       184514 :       goto again;
   14133              : 
   14134       205980 :     case FUNCTION_DECL:
   14135       205980 :       return read_function_def (decl, maybe_template);
   14136              : 
   14137        66449 :     case TYPE_DECL:
   14138        66449 :       {
   14139        66449 :         tree type = TREE_TYPE (decl);
   14140        66449 :         gcc_assert (TYPE_MAIN_VARIANT (type) == type
   14141              :                     && TYPE_NAME (type) == decl);
   14142        66449 :         if (TREE_CODE (type) == ENUMERAL_TYPE)
   14143         2927 :           return read_enum_def (decl, maybe_template);
   14144              :         else
   14145        63522 :           return read_class_def (decl, maybe_template);
   14146              :       }
   14147        40015 :       break;
   14148              : 
   14149        40015 :     case VAR_DECL:
   14150        40015 :     case CONCEPT_DECL:
   14151        40015 :       return read_var_def (decl, maybe_template);
   14152              :     }
   14153              : 
   14154              :   return false;
   14155              : }
   14156              : 
   14157              : /* Lookup an maybe insert a slot for depset for KEY.  */
   14158              : 
   14159              : depset **
   14160     13511656 : depset::hash::entity_slot (tree entity, bool insert)
   14161              : {
   14162     13511656 :   traits::compare_type key (entity, NULL);
   14163     20418324 :   depset **slot = find_slot_with_hash (key, traits::hash (key),
   14164              :                                        insert ? INSERT : NO_INSERT);
   14165              : 
   14166     13511656 :   return slot;
   14167              : }
   14168              : 
   14169              : depset **
   14170       171197 : depset::hash::binding_slot (tree ctx, tree name, bool insert)
   14171              : {
   14172       171197 :   traits::compare_type key (ctx, name);
   14173       207864 :   depset **slot = find_slot_with_hash (key, traits::hash (key),
   14174              :                                        insert ? INSERT : NO_INSERT);
   14175              : 
   14176       171197 :   return slot;
   14177              : }
   14178              : 
   14179              : depset *
   14180      6579030 : depset::hash::find_dependency (tree decl)
   14181              : {
   14182      6579030 :   depset **slot = entity_slot (decl, false);
   14183              : 
   14184      6579030 :   return slot ? *slot : NULL;
   14185              : }
   14186              : 
   14187              : depset *
   14188        36667 : depset::hash::find_binding (tree ctx, tree name)
   14189              : {
   14190        36667 :   depset **slot = binding_slot (ctx, name, false);
   14191              : 
   14192        36667 :   return slot ? *slot : NULL;
   14193              : }
   14194              : 
   14195              : static bool is_tu_local_entity (tree decl, bool explain = false);
   14196              : static bool is_tu_local_value (tree decl, tree expr, bool explain = false);
   14197              : static bool has_tu_local_tmpl_arg (tree decl, tree args, bool explain);
   14198              : 
   14199              : /* Returns true if DECL is a TU-local entity, as defined by [basic.link].
   14200              :    If EXPLAIN is true, emit an informative note about why DECL is TU-local.  */
   14201              : 
   14202              : static bool
   14203      2824324 : is_tu_local_entity (tree decl, bool explain/*=false*/)
   14204              : {
   14205      2824324 :   gcc_checking_assert (DECL_P (decl));
   14206      2824324 :   location_t loc = DECL_SOURCE_LOCATION (decl);
   14207      2824324 :   tree type = TREE_TYPE (decl);
   14208              : 
   14209              :   /* Only types, functions, variables, and template (specialisations)
   14210              :      can be TU-local.  */
   14211      2824324 :   if (TREE_CODE (decl) != TYPE_DECL
   14212              :       && TREE_CODE (decl) != FUNCTION_DECL
   14213              :       && TREE_CODE (decl) != VAR_DECL
   14214              :       && TREE_CODE (decl) != TEMPLATE_DECL)
   14215              :     return false;
   14216              : 
   14217              :   /* An explicit type alias is not an entity; we don't want to stream
   14218              :      such aliases if they refer to TU-local entities, so propagate this
   14219              :      from the original type. The built-in declarations of 'int' and such
   14220              :      are never TU-local.  */
   14221      2821344 :   if (TREE_CODE (decl) == TYPE_DECL
   14222       872332 :       && !DECL_SELF_REFERENCE_P (decl)
   14223      3660310 :       && !DECL_IMPLICIT_TYPEDEF_P (decl))
   14224              :     {
   14225       461238 :       tree orig = DECL_ORIGINAL_TYPE (decl);
   14226       461238 :       if (orig && TYPE_NAME (orig))
   14227              :         {
   14228        92637 :           if (explain)
   14229           11 :             inform (loc, "%qD is an alias of TU-local type %qT", decl, orig);
   14230        92637 :           return is_tu_local_entity (TYPE_NAME (orig), explain);
   14231              :         }
   14232              :       else
   14233              :         return false;
   14234              :     }
   14235              : 
   14236              :   /* Check specializations first for slightly better explanations.  */
   14237      2360106 :   int use_tpl = -1;
   14238      2360106 :   tree ti = node_template_info (decl, use_tpl);
   14239      2840180 :   if (use_tpl > 0 && TREE_CODE (TI_TEMPLATE (ti)) == TEMPLATE_DECL)
   14240              :     {
   14241              :       /* A specialization of a TU-local template.  */
   14242       479952 :       tree tmpl = TI_TEMPLATE (ti);
   14243       479952 :       if (is_tu_local_entity (tmpl))
   14244              :         {
   14245           72 :           if (explain)
   14246              :             {
   14247           18 :               inform (loc, "%qD is a specialization of TU-local template %qD",
   14248              :                       decl, tmpl);
   14249           18 :               is_tu_local_entity (tmpl, /*explain=*/true);
   14250              :             }
   14251           72 :           return true;
   14252              :         }
   14253              : 
   14254              :       /* A specialization of a template with any TU-local template argument.  */
   14255       479880 :       if (has_tu_local_tmpl_arg (decl, TI_ARGS (ti), explain))
   14256              :         return true;
   14257              : 
   14258              :       /* FIXME A specialization of a template whose (possibly instantiated)
   14259              :          declaration is an exposure.  This should always be covered by the
   14260              :          above cases??  */
   14261              :     }
   14262              : 
   14263              :   /* A type, function, variable, or template with internal linkage.  */
   14264      2360002 :   linkage_kind kind = decl_linkage (decl);
   14265      2360002 :   if (kind == lk_internal
   14266              :       /* But although weakrefs are marked static, don't consider them
   14267              :          to be TU-local.  */
   14268      2360002 :       && !lookup_attribute ("weakref", DECL_ATTRIBUTES (decl)))
   14269              :     {
   14270          832 :       if (explain)
   14271          168 :         inform (loc, "%qD declared with internal linkage", decl);
   14272          832 :       return true;
   14273              :     }
   14274              : 
   14275              :   /* Does not have a name with linkage and is declared, or introduced by a
   14276              :      lambda-expression, within the definition of a TU-local entity.  */
   14277      2359170 :   if (kind == lk_none)
   14278              :     {
   14279       267039 :       tree ctx = CP_DECL_CONTEXT (decl);
   14280       336228 :       if (LAMBDA_TYPE_P (type))
   14281        42704 :         if (tree extra = LAMBDA_TYPE_EXTRA_SCOPE (type))
   14282       267039 :           ctx = extra;
   14283              : 
   14284       267039 :       if (TREE_CODE (ctx) == NAMESPACE_DECL)
   14285              :         {
   14286           31 :           if (!TREE_PUBLIC (ctx))
   14287              :             {
   14288            0 :               if (explain)
   14289            0 :                 inform (loc, "%qD has no linkage and is declared in an "
   14290              :                         "anonymous namespace", decl);
   14291            0 :               return true;
   14292              :             }
   14293              :         }
   14294       267008 :       else if (TYPE_P (ctx))
   14295              :         {
   14296        29436 :           tree ctx_decl = TYPE_MAIN_DECL (ctx);
   14297        29436 :           if (is_tu_local_entity (ctx_decl))
   14298              :             {
   14299            6 :               if (explain)
   14300              :                 {
   14301            0 :                   inform (loc, "%qD has no linkage and is declared within "
   14302              :                           "TU-local entity %qT", decl, ctx);
   14303            0 :                   is_tu_local_entity (ctx_decl, /*explain=*/true);
   14304              :                 }
   14305            6 :               return true;
   14306              :             }
   14307              :         }
   14308       237572 :       else if (is_tu_local_entity (ctx))
   14309              :         {
   14310           33 :           if (explain)
   14311              :             {
   14312            6 :               inform (loc, "%qD has no linkage and is declared within "
   14313              :                       "TU-local entity %qD", decl, ctx);
   14314            6 :               is_tu_local_entity (ctx, /*explain=*/true);
   14315              :             }
   14316           33 :           return true;
   14317              :         }
   14318              :     }
   14319              : 
   14320              :   /* A type with no name that is defined outside a class-specifier, function
   14321              :      body, or initializer; or is introduced by a defining-type-specifier that
   14322              :      is used to declare only TU-local entities.
   14323              : 
   14324              :      We consider types with names for linkage purposes as having names, since
   14325              :      these aren't really TU-local.  */
   14326      2359131 :   tree inner = STRIP_TEMPLATE (decl);
   14327       961654 :   if (inner
   14328      2359131 :       && TREE_CODE (inner) == TYPE_DECL
   14329      1561921 :       && TYPE_ANON_P (type)
   14330        23269 :       && !DECL_SELF_REFERENCE_P (inner)
   14331              :       /* An enum with an enumerator name for linkage.  */
   14332       983727 :       && !(UNSCOPED_ENUM_P (type) && TYPE_VALUES (type)))
   14333              :     {
   14334        20857 :       tree main_decl = TYPE_MAIN_DECL (type);
   14335        41417 :       if (LAMBDA_TYPE_P (type))
   14336              :         {
   14337              :           /* A lambda expression is, in practice, TU-local iff it has no
   14338              :              mangling scope.  This currently doesn't line up exactly with
   14339              :              the standard's definition due to some ABI issues, but it's
   14340              :              pretty close, and avoids other issues down the line.  */
   14341        41054 :           if (!LAMBDA_TYPE_EXTRA_SCOPE (type))
   14342              :             {
   14343            4 :               if (explain)
   14344            2 :                 inform (loc, "%qT has no name and cannot be differentiated "
   14345              :                         "from similar lambdas in other TUs", type);
   14346            4 :               return true;
   14347              :             }
   14348              :         }
   14349          660 :       else if (!DECL_CLASS_SCOPE_P (main_decl)
   14350          360 :                && !decl_function_context (main_decl))
   14351              :         {
   14352           30 :           if (explain)
   14353           12 :             inform (loc, "%qT has no name and is not defined within a class, "
   14354              :                     "function, or initializer", type);
   14355           30 :           return true;
   14356              :         }
   14357              : 
   14358              :       // FIXME introduced by a defining-type-specifier only declaring TU-local
   14359              :       // entities; does this refer to e.g. 'static struct {} a;"?  I can't
   14360              :       // think of any cases where this isn't covered by earlier cases.  */
   14361              :     }
   14362              : 
   14363              :   return false;
   14364              : }
   14365              : 
   14366              : /* Helper for is_tu_local_entity.  Returns true if one of the ARGS of
   14367              :    DECL is TU-local.  Emits an explanation if EXPLAIN is true.  */
   14368              : 
   14369              : static bool
   14370       543248 : has_tu_local_tmpl_arg (tree decl, tree args, bool explain)
   14371              : {
   14372       543248 :   if (!args || TREE_CODE (args) != TREE_VEC)
   14373              :     return false;
   14374              : 
   14375      1456963 :   for (tree a : tree_vec_range (args))
   14376              :     {
   14377       913747 :       if (TREE_CODE (a) == TREE_VEC)
   14378              :         {
   14379        63368 :           if (has_tu_local_tmpl_arg (decl, a, explain))
   14380           32 :             return true;
   14381              :         }
   14382              :       else if (!WILDCARD_TYPE_P (a))
   14383              :         {
   14384       774381 :           if (DECL_P (a) && is_tu_local_entity (a))
   14385              :             {
   14386            0 :               if (explain)
   14387              :                 {
   14388            0 :                   inform (DECL_SOURCE_LOCATION (decl),
   14389              :                           "%qD has TU-local template argument %qD",
   14390              :                           decl, a);
   14391            0 :                   is_tu_local_entity (a, /*explain=*/true);
   14392              :                 }
   14393            0 :               return true;
   14394              :             }
   14395              : 
   14396       774381 :           if (TYPE_P (a) && TYPE_NAME (a) && is_tu_local_entity (TYPE_NAME (a)))
   14397              :             {
   14398           17 :               if (explain)
   14399              :                 {
   14400            1 :                   inform (DECL_SOURCE_LOCATION (decl),
   14401              :                           "%qD has TU-local template argument %qT",
   14402              :                           decl, a);
   14403            1 :                   is_tu_local_entity (TYPE_NAME (a), /*explain=*/true);
   14404              :                 }
   14405           17 :               return true;
   14406              :             }
   14407              : 
   14408       774364 :           if (EXPR_P (a) && is_tu_local_value (decl, a, explain))
   14409              :             return true;
   14410              :         }
   14411              :     }
   14412              : 
   14413       543216 :   return false;
   14414              : }
   14415              : 
   14416              : /* Returns true if EXPR (part of the initializer for DECL) is a TU-local value
   14417              :    or object.  Emits an explanation if EXPLAIN is true.  */
   14418              : 
   14419              : static bool
   14420        49404 : is_tu_local_value (tree decl, tree expr, bool explain/*=false*/)
   14421              : {
   14422        49404 :   if (!expr)
   14423              :     return false;
   14424              : 
   14425        48283 :   tree e = expr;
   14426        48283 :   STRIP_ANY_LOCATION_WRAPPER (e);
   14427        48283 :   STRIP_NOPS (e);
   14428        48283 :   if (TREE_CODE (e) == TARGET_EXPR)
   14429            0 :     e = TARGET_EXPR_INITIAL (e);
   14430            0 :   if (!e)
   14431              :     return false;
   14432              : 
   14433              :   /* It is, or is a pointer to, a TU-local function or the object associated
   14434              :      with a TU-local variable.  */
   14435        48283 :   tree object = NULL_TREE;
   14436        48283 :   if (TREE_CODE (e) == ADDR_EXPR)
   14437         1892 :     object = TREE_OPERAND (e, 0);
   14438        46391 :   else if (TREE_CODE (e) == PTRMEM_CST)
   14439            0 :     object = PTRMEM_CST_MEMBER (e);
   14440        46391 :   else if (VAR_OR_FUNCTION_DECL_P (e))
   14441              :     object = e;
   14442              : 
   14443         1892 :   if (object
   14444         2262 :       && VAR_OR_FUNCTION_DECL_P (object)
   14445         2349 :       && is_tu_local_entity (object))
   14446              :     {
   14447           54 :       if (explain)
   14448              :         {
   14449              :           /* We've lost a lot of location information by the time we get here,
   14450              :              so let's just do our best effort.  */
   14451           18 :           auto loc = cp_expr_loc_or_loc (expr, DECL_SOURCE_LOCATION (decl));
   14452           18 :           if (VAR_P (object))
   14453            9 :             inform (loc, "%qD refers to TU-local object %qD", decl, object);
   14454              :           else
   14455            9 :             inform (loc, "%qD refers to TU-local function %qD", decl, object);
   14456           18 :           is_tu_local_entity (object, true);
   14457              :         }
   14458           54 :       return true;
   14459              :     }
   14460              : 
   14461              :   /* It is an object of class or array type and any of its subobjects or
   14462              :      any of the objects or functions to which its non-static data members
   14463              :      of reference type refer is TU-local and is usable in constant
   14464              :      expressions.  */
   14465        48229 :   if (TREE_CODE (e) == CONSTRUCTOR && AGGREGATE_TYPE_P (TREE_TYPE (e)))
   14466        21348 :     for (auto &f : CONSTRUCTOR_ELTS (e))
   14467        14914 :       if (is_tu_local_value (decl, f.value, explain))
   14468              :         return true;
   14469              : 
   14470              :   return false;
   14471              : }
   14472              : 
   14473              : /* Complains if DECL is a TU-local entity imported from a named module.
   14474              :    Returns TRUE if instantiation should fail.  */
   14475              : 
   14476              : bool
   14477  10165043924 : instantiating_tu_local_entity (tree decl)
   14478              : {
   14479  10165043924 :   if (!modules_p ())
   14480              :     return false;
   14481              : 
   14482     24881312 :   if (TREE_CODE (decl) == TU_LOCAL_ENTITY)
   14483              :     {
   14484           92 :       auto_diagnostic_group d;
   14485           92 :       error ("instantiation exposes TU-local entity %qD",
   14486           92 :              TU_LOCAL_ENTITY_NAME (decl));
   14487           92 :       inform (TU_LOCAL_ENTITY_LOCATION (decl), "declared here");
   14488           92 :       return true;
   14489           92 :     }
   14490              : 
   14491              :   /* Currently, only TU-local variables and functions, or possibly
   14492              :      templates thereof, will be emitted from named modules.  */
   14493     24881220 :   tree inner = STRIP_TEMPLATE (decl);
   14494     24881220 :   if (!VAR_OR_FUNCTION_DECL_P (inner))
   14495              :     return false;
   14496              : 
   14497              :   /* From this point we will only be emitting warnings; if we're not
   14498              :      warning about this case then there's no need to check further.  */
   14499      1013585 :   if (!warn_expose_global_module_tu_local
   14500      2027170 :       || !warning_enabled_at (DECL_SOURCE_LOCATION (decl),
   14501      1013585 :                               OPT_Wexpose_global_module_tu_local))
   14502         7896 :     return false;
   14503              : 
   14504      1005689 :   if (!is_tu_local_entity (decl))
   14505              :     return false;
   14506              : 
   14507           65 :   if (!DECL_LANG_SPECIFIC (inner)
   14508          120 :       || !DECL_MODULE_IMPORT_P (inner))
   14509              :     return false;
   14510              : 
   14511              :   /* Referencing TU-local entities from a header is generally OK.
   14512              :      We don't have an easy way to detect if this declaration came
   14513              :      from a header via a separate named module, but we can just
   14514              :      ignore that case for warning purposes.  */
   14515            9 :   unsigned index = import_entity_index (decl);
   14516            9 :   module_state *mod = import_entity_module (index);
   14517            9 :   if (mod->is_header ())
   14518              :     return false;
   14519              : 
   14520            9 :   auto_diagnostic_group d;
   14521            9 :   warning (OPT_Wexpose_global_module_tu_local,
   14522              :            "instantiation exposes TU-local entity %qD", decl);
   14523            9 :   inform (DECL_SOURCE_LOCATION (decl), "declared here");
   14524              : 
   14525              :   /* We treat TU-local entities from the GMF as not actually being
   14526              :      TU-local as an extension, so allow instantation to proceed.  */
   14527            9 :   return false;
   14528            9 : }
   14529              : 
   14530              : /* DECL is a newly discovered dependency.  Create the depset, if it
   14531              :    doesn't already exist.  Add it to the worklist if so.
   14532              : 
   14533              :    DECL will be an OVL_USING_P OVERLOAD, if it's from a binding that's
   14534              :    a using decl.
   14535              : 
   14536              :    We do not have to worry about adding the same dependency more than
   14537              :    once.  First it's harmless, but secondly the TREE_VISITED marking
   14538              :    prevents us wanting to do it anyway.  */
   14539              : 
   14540              : depset *
   14541      5742399 : depset::hash::make_dependency (tree decl, entity_kind ek)
   14542              : {
   14543              :   /* Make sure we're being told consistent information.  */
   14544     10693952 :   gcc_checking_assert ((ek == EK_NAMESPACE)
   14545              :                        == (TREE_CODE (decl) == NAMESPACE_DECL
   14546              :                            && !DECL_NAMESPACE_ALIAS (decl)));
   14547      5742399 :   gcc_checking_assert (ek != EK_BINDING && ek != EK_REDIRECT);
   14548      5742399 :   gcc_checking_assert (TREE_CODE (decl) != FIELD_DECL
   14549              :                        && (TREE_CODE (decl) != USING_DECL
   14550              :                            || TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL));
   14551      5742399 :   gcc_checking_assert (!is_key_order ());
   14552      5742399 :   if (ek == EK_USING)
   14553        21209 :     gcc_checking_assert (TREE_CODE (decl) == OVERLOAD);
   14554      5742399 :   if (ek == EK_TU_LOCAL)
   14555           93 :     gcc_checking_assert (DECL_DECLARES_FUNCTION_P (decl));
   14556              : 
   14557      5742399 :   if (TREE_CODE (decl) == TEMPLATE_DECL)
   14558              :     /* The template should have copied these from its result decl.  */
   14559      2100960 :     gcc_checking_assert (DECL_MODULE_EXPORT_P (decl)
   14560              :                          == DECL_MODULE_EXPORT_P (DECL_TEMPLATE_RESULT (decl)));
   14561              : 
   14562      5742399 :   depset **slot = entity_slot (decl, true);
   14563      5742399 :   depset *dep = *slot;
   14564      5742399 :   bool for_binding = ek == EK_FOR_BINDING;
   14565              : 
   14566      5742399 :   if (!dep)
   14567              :     {
   14568       532904 :       if ((DECL_IMPLICIT_TYPEDEF_P (decl)
   14569              :            /* ... not an enum, for instance.  */
   14570       265679 :            && RECORD_OR_UNION_TYPE_P (TREE_TYPE (decl))
   14571       261611 :            && TYPE_LANG_SPECIFIC (TREE_TYPE (decl))
   14572       238680 :            && CLASSTYPE_USE_TEMPLATE (TREE_TYPE (decl)) == 2)
   14573      1808343 :           || (VAR_P (decl)
   14574        74872 :               && DECL_LANG_SPECIFIC (decl)
   14575        74791 :               && DECL_USE_TEMPLATE (decl) == 2))
   14576              :         {
   14577              :           /* A partial or explicit specialization. Partial
   14578              :              specializations might not be in the hash table, because
   14579              :              there can be multiple differently-constrained variants.
   14580              : 
   14581              :              template<typename T> class silly;
   14582              :              template<typename T> requires true class silly {};
   14583              : 
   14584              :              We need to find them, insert their TEMPLATE_DECL in the
   14585              :              dep_hash, and then convert the dep we just found into a
   14586              :              redirect.  */
   14587              : 
   14588        37540 :           tree ti = get_template_info (decl);
   14589        37540 :           tree tmpl = TI_TEMPLATE (ti);
   14590        37540 :           tree partial = NULL_TREE;
   14591        37540 :           for (tree spec = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
   14592       125539 :                spec; spec = TREE_CHAIN (spec))
   14593       107749 :             if (DECL_TEMPLATE_RESULT (TREE_VALUE (spec)) == decl)
   14594              :               {
   14595              :                 partial = TREE_VALUE (spec);
   14596              :                 break;
   14597              :               }
   14598              : 
   14599        37540 :           if (partial)
   14600              :             {
   14601              :               /* Eagerly create an empty redirect.  The following
   14602              :                  make_dependency call could cause hash reallocation,
   14603              :                  and invalidate slot's value.  */
   14604        19750 :               depset *redirect = make_entity (decl, EK_REDIRECT);
   14605              : 
   14606              :               /* Redirects are never reached -- always snap to their target.  */
   14607        19750 :               redirect->set_flag_bit<DB_UNREACHED_BIT> ();
   14608              : 
   14609        19750 :               *slot = redirect;
   14610              : 
   14611        19750 :               depset *tmpl_dep = make_dependency (partial, EK_PARTIAL);
   14612        19750 :               gcc_checking_assert (tmpl_dep->get_entity_kind () == EK_PARTIAL);
   14613              : 
   14614        19750 :               redirect->deps.safe_push (tmpl_dep);
   14615              : 
   14616        19750 :               return redirect;
   14617              :             }
   14618              :         }
   14619              : 
   14620      1287229 :       bool has_def = ek != EK_USING && has_definition (decl);
   14621      1266020 :       if (ek > EK_BINDING)
   14622       143820 :         ek = EK_DECL;
   14623              : 
   14624              :       /* The only OVERLOADS we should see are USING decls from
   14625              :          bindings.  */
   14626      1287229 :       *slot = dep = make_entity (decl, ek, has_def);
   14627              : 
   14628      1287229 :       if (CHECKING_P && TREE_CODE (decl) == TEMPLATE_DECL)
   14629              :         /* The template_result should otherwise not be in the
   14630              :            table, or be an empty redirect (created above).  */
   14631       327638 :         if (auto *eslot = entity_slot (DECL_TEMPLATE_RESULT (decl), false))
   14632        19750 :           gcc_checking_assert ((*eslot)->get_entity_kind () == EK_REDIRECT
   14633              :                                && !(*eslot)->deps.length ());
   14634              : 
   14635      1287229 :       if (ignore_exposure)
   14636        22254 :         dep->set_flag_bit<DB_IGNORED_EXPOSURE_BIT> ();
   14637              : 
   14638      1287229 :       if (ek != EK_USING)
   14639              :         {
   14640      1266020 :           tree not_tmpl = STRIP_TEMPLATE (decl);
   14641      1266020 :           bool imported_from_module_p = false;
   14642              : 
   14643      1266020 :           if (DECL_LANG_SPECIFIC (not_tmpl)
   14644      2465144 :               && DECL_MODULE_IMPORT_P (not_tmpl))
   14645              :             {
   14646              :               /* Store the module number and index in cluster/section,
   14647              :                  so we don't have to look them up again.  */
   14648        60053 :               unsigned index = import_entity_index (decl);
   14649        60053 :               module_state *from = import_entity_module (index);
   14650              :               /* Remap will be zero for imports from partitions, which
   14651              :                  we want to treat as-if declared in this TU.  */
   14652        60053 :               if (from->remap)
   14653              :                 {
   14654        59320 :                   dep->cluster = index - from->entity_lwm;
   14655        59320 :                   dep->section = from->remap;
   14656        59320 :                   dep->set_flag_bit<DB_IMPORTED_BIT> ();
   14657              : 
   14658        59320 :                   if (!from->is_header ())
   14659      1266020 :                     imported_from_module_p = true;
   14660              :                 }
   14661              :             }
   14662              : 
   14663              :           /* Check for TU-local entities.  This is unnecessary in header
   14664              :              units because we can export internal-linkage decls, and
   14665              :              no declarations are exposures.  Similarly, if the decl was
   14666              :              imported from a non-header module we know it cannot have
   14667              :              been TU-local.  */
   14668      1266020 :           if (!header_module_p () && !imported_from_module_p)
   14669              :             {
   14670       394282 :               if (is_tu_local_entity (decl))
   14671          297 :                 dep->set_flag_bit<DB_TU_LOCAL_BIT> ();
   14672              : 
   14673       394282 :               if (VAR_P (decl)
   14674        23417 :                   && decl_maybe_constant_var_p (decl)
   14675       416874 :                   && is_tu_local_value (decl, DECL_INITIAL (decl)))
   14676              :                 {
   14677              :                   /* A potentially-constant variable initialized to a TU-local
   14678              :                      value is not usable in constant expressions within other
   14679              :                      translation units.  We can achieve this by simply not
   14680              :                      streaming the definition in such cases.  */
   14681           24 :                   dep->clear_flag_bit<DB_DEFN_BIT> ();
   14682              : 
   14683           24 :                   if (DECL_DECLARED_CONSTEXPR_P (decl)
   14684           39 :                       || DECL_INLINE_VAR_P (decl))
   14685              :                     /* A constexpr variable initialized to a TU-local value,
   14686              :                        or an inline value (PR c++/119996), is an exposure.
   14687              : 
   14688              :                        For simplicity, we don't support "non-strict" TU-local
   14689              :                        values: even if the TU-local entity we refer to in the
   14690              :                        initialiser is in the GMF, we still won't consider this
   14691              :                        valid in constant expressions in other TUs, and so
   14692              :                        complain accordingly.  */
   14693           15 :                     dep->set_flag_bit<DB_EXPOSE_PURVIEW_BIT> ();
   14694              :                 }
   14695              :             }
   14696              : 
   14697              :           /* A namespace-scope type may be declared in one module unit
   14698              :              and defined in another; make sure that we're found when
   14699              :              completing the class.  */
   14700      1266020 :           if (ek == EK_DECL
   14701       507873 :               && !dep->is_import ()
   14702       501430 :               && dep->has_defn ()
   14703       259231 :               && DECL_NAMESPACE_SCOPE_P (not_tmpl)
   14704        96228 :               && DECL_IMPLICIT_TYPEDEF_P (not_tmpl)
   14705              :               /* Anonymous types can't be forward-declared.  */
   14706      1292936 :               && !IDENTIFIER_ANON_P (DECL_NAME (not_tmpl)))
   14707        26450 :             dep->set_flag_bit<DB_IS_PENDING_BIT> ();
   14708              : 
   14709              :           /* Namespace-scope functions can be found by ADL by template
   14710              :              instantiations in this module.  We need to create bindings
   14711              :              for them so that name lookup recognises they exist, if they
   14712              :              won't be discarded.  add_binding_entity is too early to do
   14713              :              this for GM functions, because if nobody ends up using them
   14714              :              we'll have leftover bindings laying around, and it's tricky
   14715              :              to delete them and any namespaces they've implicitly created
   14716              :              deps on.  The downside is this means we don't pick up on
   14717              :              using-decls, but by [module.global.frag] p3.6 we don't have
   14718              :              to.  */
   14719      1266020 :           if (ek == EK_DECL
   14720      1266020 :               && !for_binding
   14721       364065 :               && !dep->is_import ()
   14722       357628 :               && !dep->is_tu_local ()
   14723       357524 :               && DECL_NAMESPACE_SCOPE_P (decl)
   14724        24652 :               && DECL_DECLARES_FUNCTION_P (decl)
   14725              :               /* Compiler-generated functions won't participate in ADL.  */
   14726        18897 :               && !DECL_ARTIFICIAL (decl)
   14727              :               /* A hidden friend doesn't need a binding.  */
   14728      1279596 :               && !(DECL_LANG_SPECIFIC (not_tmpl)
   14729        13576 :                    && DECL_UNIQUE_FRIEND_P (not_tmpl)))
   14730              :             {
   14731              :               /* This will only affect GM functions.  */
   14732         5520 :               gcc_checking_assert (!DECL_LANG_SPECIFIC (not_tmpl)
   14733              :                                    || !DECL_MODULE_PURVIEW_P (not_tmpl));
   14734              :               /* We shouldn't see any instantiations or specialisations.  */
   14735         2760 :               gcc_checking_assert (!DECL_LANG_SPECIFIC (decl)
   14736              :                                    || !DECL_USE_TEMPLATE (decl));
   14737              : 
   14738         2760 :               tree ns = CP_DECL_CONTEXT (decl);
   14739         2760 :               tree name = DECL_NAME (decl);
   14740         2760 :               depset *binding = find_binding (ns, name);
   14741         2760 :               if (!binding)
   14742              :                 {
   14743         1268 :                   binding = make_binding (ns, name);
   14744         1268 :                   add_namespace_context (binding, ns);
   14745              : 
   14746         1268 :                   depset **slot = binding_slot (ns, name, /*insert=*/true);
   14747         1268 :                   *slot = binding;
   14748              :                 }
   14749              : 
   14750         2760 :               binding->deps.safe_push (dep);
   14751         2760 :               dep->deps.safe_push (binding);
   14752         2795 :               dump (dumper::DEPEND)
   14753            9 :                 && dump ("Built ADL binding for %C:%N",
   14754            9 :                          TREE_CODE (decl), decl);
   14755              :             }
   14756              :         }
   14757              : 
   14758      1287229 :       if (!dep->is_import ())
   14759      1227909 :         worklist.safe_push (dep);
   14760              :     }
   14761      4435420 :   else if (!ignore_exposure)
   14762      4029600 :     dep->clear_flag_bit<DB_IGNORED_EXPOSURE_BIT> ();
   14763              : 
   14764      5722649 :   dump (dumper::DEPEND)
   14765        36425 :     && dump ("%s on %s %C:%N found",
   14766              :              ek == EK_REDIRECT ? "Redirect"
   14767        36425 :              : (for_binding || ek == EK_TU_LOCAL) ? "Binding"
   14768              :              : "Dependency",
   14769        36425 :              dep->entity_kind_name (), TREE_CODE (decl), decl);
   14770              : 
   14771      5722649 :   return dep;
   14772              : }
   14773              : 
   14774              : /* Whether REF is an exposure of a member type of SOURCE.
   14775              : 
   14776              :    This comes up with exposures of class-scope lambdas, that we currently
   14777              :    treat as TU-local due to ABI reasons.  In such a case the type of the
   14778              :    lambda will be exposed in two places, first by the class type it is in
   14779              :    the TYPE_FIELDS list of, and second by the actual member declaring that
   14780              :    lambda.  We only want the second case to warn.  */
   14781              : 
   14782              : static bool
   14783          247 : is_exposure_of_member_type (depset *source, depset *ref)
   14784              : {
   14785          247 :   gcc_checking_assert (source->refs_tu_local (/*strict=*/true)
   14786              :                        && ref->is_tu_local (/*strict=*/true));
   14787          247 :   tree source_entity = STRIP_TEMPLATE (source->get_entity ());
   14788          247 :   tree ref_entity = STRIP_TEMPLATE (ref->get_entity ());
   14789              : 
   14790          247 :   if (!source->is_tu_local (/*strict=*/true)
   14791          235 :       && source_entity
   14792          235 :       && ref_entity
   14793          235 :       && DECL_IMPLICIT_TYPEDEF_P (source_entity)
   14794            2 :       && DECL_IMPLICIT_TYPEDEF_P (ref_entity)
   14795            2 :       && DECL_CLASS_SCOPE_P (ref_entity)
   14796          249 :       && DECL_CONTEXT (ref_entity) == TREE_TYPE (source_entity))
   14797              :     {
   14798            4 :       gcc_checking_assert (LAMBDA_TYPE_P (TREE_TYPE (ref_entity)));
   14799              :       return true;
   14800              :     }
   14801              :   else
   14802              :     return false;
   14803              : }
   14804              : 
   14805              : /* DEP is a newly discovered dependency.  Append it to current's
   14806              :    depset.  */
   14807              : 
   14808              : void
   14809      4214506 : depset::hash::add_dependency (depset *dep)
   14810              : {
   14811      4214506 :   gcc_checking_assert (current && !is_key_order ());
   14812      4214506 :   current->deps.safe_push (dep);
   14813              : 
   14814      4214506 :   if (dep->is_tu_local (/*strict=*/true))
   14815              :     {
   14816          317 :       if (dep->is_tu_local ())
   14817          260 :         current->set_flag_bit<DB_REF_PURVIEW_BIT> ();
   14818              :       else
   14819           57 :         current->set_flag_bit<DB_REF_GLOBAL_BIT> ();
   14820              : 
   14821          317 :       if (!ignore_exposure && !is_exposure_of_member_type (current, dep))
   14822              :         {
   14823          127 :           if (dep->is_tu_local ())
   14824           91 :             current->set_flag_bit<DB_EXPOSE_PURVIEW_BIT> ();
   14825              :           else
   14826           36 :             current->set_flag_bit<DB_EXPOSE_GLOBAL_BIT> ();
   14827              :         }
   14828              :     }
   14829              : 
   14830      4214506 :   if (current->get_entity_kind () == EK_USING
   14831        21209 :       && DECL_IMPLICIT_TYPEDEF_P (dep->get_entity ())
   14832      4219401 :       && TREE_CODE (TREE_TYPE (dep->get_entity ())) == ENUMERAL_TYPE)
   14833              :     {
   14834              :       /* CURRENT is an unwrapped using-decl and DECL is an enum's
   14835              :          implicit typedef.  Is CURRENT a member of the enum?  */
   14836         4588 :       tree c_decl = OVL_FUNCTION (current->get_entity ());
   14837              : 
   14838         4588 :       if (TREE_CODE (c_decl) == CONST_DECL
   14839         9136 :           && (current->deps[0]->get_entity ()
   14840         4548 :               == CP_DECL_CONTEXT (dep->get_entity ())))
   14841              :         /* Make DECL depend on CURRENT.  */
   14842         4494 :         dep->deps.safe_push (current);
   14843              :     }
   14844              : 
   14845              :   /* If two dependencies recursively depend on each other existing within
   14846              :      their own merge keys, we must ensure that the first dep we saw while
   14847              :      walking is written first in this cluster.  See sort_cluster for more
   14848              :      details.  */
   14849      4214506 :   if (writing_merge_key)
   14850              :     {
   14851       741613 :       dep->set_flag_bit<DB_MAYBE_RECURSIVE_BIT> ();
   14852       741613 :       if (!current->is_maybe_recursive ())
   14853       700483 :         current->set_flag_bit<DB_ENTRY_BIT> ();
   14854              :     }
   14855              : 
   14856      4214506 :   if (dep->is_unreached ())
   14857              :     {
   14858              :       /* The dependency is reachable now.  */
   14859       312831 :       reached_unreached = true;
   14860       312831 :       dep->clear_flag_bit<DB_UNREACHED_BIT> ();
   14861       312831 :       dump (dumper::DEPEND)
   14862           30 :         && dump ("Reaching unreached %s %C:%N", dep->entity_kind_name (),
   14863           30 :                  TREE_CODE (dep->get_entity ()), dep->get_entity ());
   14864              :     }
   14865      4214506 : }
   14866              : 
   14867              : depset *
   14868      6218959 : depset::hash::add_dependency (tree decl, entity_kind ek)
   14869              : {
   14870      6218959 :   depset *dep;
   14871              : 
   14872      6218959 :   if (is_key_order ())
   14873              :     {
   14874      2009898 :       dep = find_dependency (decl);
   14875      2009898 :       if (dep)
   14876              :         {
   14877       918012 :           current->deps.safe_push (dep);
   14878       918012 :           dump (dumper::MERGE)
   14879          717 :             && dump ("Key dependency on %s %C:%N found",
   14880          717 :                      dep->entity_kind_name (), TREE_CODE (decl), decl);
   14881              :         }
   14882              :       else
   14883              :         {
   14884              :           /* It's not a mergeable decl, look for it in the original
   14885              :              table.  */
   14886      1091886 :           dep = chain->find_dependency (decl);
   14887      1091886 :           gcc_checking_assert (dep);
   14888              :         }
   14889              :     }
   14890              :   else
   14891              :     {
   14892      4209061 :       dep = make_dependency (decl, ek);
   14893      4209061 :       if (dep->get_entity_kind () != EK_REDIRECT)
   14894      4164059 :         add_dependency (dep);
   14895              :     }
   14896              : 
   14897      6218959 :   return dep;
   14898              : }
   14899              : 
   14900              : void
   14901       525009 : depset::hash::add_namespace_context (depset *dep, tree ns)
   14902              : {
   14903       525009 :   depset *ns_dep = make_dependency (ns, depset::EK_NAMESPACE);
   14904       525009 :   dep->deps.safe_push (ns_dep);
   14905              : 
   14906              :   /* Mark it as special if imported so we don't walk connect when
   14907              :      SCCing.  */
   14908       525009 :   if (!dep->is_binding () && ns_dep->is_import ())
   14909            0 :     dep->set_special ();
   14910       525009 : }
   14911              : 
   14912              : struct add_binding_data
   14913              : {
   14914              :   tree ns;
   14915              :   bitmap partitions;
   14916              :   depset *binding;
   14917              :   depset::hash *hash;
   14918              :   bool met_namespace;
   14919              : };
   14920              : 
   14921              : /* Return true if we are, or contain something that is exported.  */
   14922              : 
   14923              : bool
   14924      5599187 : depset::hash::add_binding_entity (tree decl, WMB_Flags flags, void *data_)
   14925              : {
   14926      5599187 :   auto data = static_cast <add_binding_data *> (data_);
   14927      5599187 :   decl = strip_using_decl (decl);
   14928              : 
   14929      5599187 :   if (!(TREE_CODE (decl) == NAMESPACE_DECL && !DECL_NAMESPACE_ALIAS (decl)))
   14930              :     {
   14931      5591285 :       tree inner = decl;
   14932              : 
   14933      5591285 :       if (TREE_CODE (inner) == CONST_DECL
   14934         5962 :           && TREE_CODE (DECL_CONTEXT (inner)) == ENUMERAL_TYPE
   14935              :           /* A using-decl could make a CONST_DECL purview for a non-purview
   14936              :              enumeration.  */
   14937      5597247 :           && (!DECL_LANG_SPECIFIC (inner) || !DECL_MODULE_PURVIEW_P (inner)))
   14938         5921 :         inner = TYPE_NAME (DECL_CONTEXT (inner));
   14939      5585364 :       else if (TREE_CODE (inner) == TEMPLATE_DECL)
   14940       103505 :         inner = DECL_TEMPLATE_RESULT (inner);
   14941              : 
   14942     11015103 :       if ((!DECL_LANG_SPECIFIC (inner) || !DECL_MODULE_PURVIEW_P (inner))
   14943     10850514 :           && !((flags & WMB_Using) && (flags & WMB_Purview)))
   14944              :         /* Ignore entities not within the module purview.  We'll need to
   14945              :            create bindings for any non-discarded function calls for ADL,
   14946              :            but it's simpler to handle that at the point of use rather
   14947              :            than trying to clear out bindings after the fact.  */
   14948              :         return false;
   14949              : 
   14950       170486 :       if ((flags & WMB_Hidden)
   14951         5401 :           && DECL_LANG_SPECIFIC (inner)
   14952       175887 :           && DECL_UNIQUE_FRIEND_P (inner))
   14953              :         /* Hidden friends will be found via ADL on the class type,
   14954              :            and so do not need to have bindings.  Anticipated builtin
   14955              :            functions and the hidden decl underlying a DECL_LOCAL_DECL_P
   14956              :            also don't need exporting, but we should create a binding
   14957              :            anyway so that we can have a common decl to match against.  */
   14958              :         return false;
   14959              : 
   14960       165173 :       bool internal_decl = false;
   14961       165173 :       if (!header_module_p () && is_tu_local_entity (decl))
   14962              :         {
   14963              :           /* A TU-local entity.  For ADL we still need to create bindings
   14964              :              for internal-linkage functions attached to a named module.  */
   14965          150 :           if (DECL_DECLARES_FUNCTION_P (inner)
   14966          105 :               && DECL_LANG_SPECIFIC (inner)
   14967          360 :               && DECL_MODULE_ATTACH_P (inner))
   14968              :             {
   14969           93 :               gcc_checking_assert (!DECL_MODULE_EXPORT_P (inner));
   14970              :               internal_decl = true;
   14971              :             }
   14972              :           else
   14973              :             return false;
   14974              :         }
   14975              : 
   14976       165011 :       if ((TREE_CODE (decl) == VAR_DECL
   14977       165011 :            || TREE_CODE (decl) == TYPE_DECL)
   14978       165011 :           && DECL_TINFO_P (decl))
   14979              :         /* Ignore TINFO things.  */
   14980              :         return false;
   14981              : 
   14982       165011 :       if (TREE_CODE (decl) == VAR_DECL && DECL_NTTP_OBJECT_P (decl))
   14983              :         /* Ignore NTTP objects.  */
   14984              :         return false;
   14985              : 
   14986       165011 :       if (deduction_guide_p (decl))
   14987              :         {
   14988              :           /* Ignore deduction guides, bindings for them will be created within
   14989              :              find_dependencies for their class template.  But still build a dep
   14990              :              for them so that we don't discard them.  */
   14991         1610 :           data->hash->make_dependency (decl, EK_FOR_BINDING);
   14992         1610 :           return false;
   14993              :         }
   14994              : 
   14995       163401 :       if (!(flags & WMB_Using) && CP_DECL_CONTEXT (decl) != data->ns)
   14996              :         {
   14997              :           /* An unscoped enum constant implicitly brought into the containing
   14998              :              namespace.  We treat this like a using-decl.  */
   14999         3806 :           gcc_checking_assert (TREE_CODE (decl) == CONST_DECL);
   15000              : 
   15001         3806 :           flags = WMB_Flags (flags | WMB_Using);
   15002         3806 :           if (DECL_MODULE_EXPORT_P (TYPE_NAME (TREE_TYPE (decl)))
   15003              :               /* A using-decl can make an enum constant exported for a
   15004              :                  non-exported enumeration.  */
   15005         3806 :               || (DECL_LANG_SPECIFIC (decl) && DECL_MODULE_EXPORT_P (decl)))
   15006         3575 :             flags = WMB_Flags (flags | WMB_Export);
   15007              :         }
   15008              : 
   15009       163401 :       if (!data->binding)
   15010              :         /* No binding to check.  */;
   15011        30807 :       else if (flags & WMB_Using)
   15012              :         {
   15013              :           /* Look in the binding to see if we already have this
   15014              :              using.  */
   15015        48906 :           for (unsigned ix = data->binding->deps.length (); --ix;)
   15016              :             {
   15017        39019 :               depset *d = data->binding->deps[ix];
   15018        78038 :               if (d->get_entity_kind () == EK_USING
   15019        39019 :                   && OVL_FUNCTION (d->get_entity ()) == decl)
   15020              :                 {
   15021            1 :                   if (!(flags & WMB_Hidden))
   15022            1 :                     d->clear_hidden_binding ();
   15023            1 :                   OVL_PURVIEW_P (d->get_entity ()) = true;
   15024            1 :                   if (flags & WMB_Export)
   15025            1 :                     OVL_EXPORT_P (d->get_entity ()) = true;
   15026            1 :                   return bool (flags & WMB_Export);
   15027              :                 }
   15028              :             }
   15029              :         }
   15030        25863 :       else if (flags & WMB_Dups)
   15031              :         {
   15032              :           /* Look in the binding to see if we already have this decl.  */
   15033           72 :           for (unsigned ix = data->binding->deps.length (); --ix;)
   15034              :             {
   15035           36 :               depset *d = data->binding->deps[ix];
   15036           36 :               if (d->get_entity () == decl)
   15037              :                 {
   15038           30 :                   if (!(flags & WMB_Hidden))
   15039           30 :                     d->clear_hidden_binding ();
   15040           30 :                   return false;
   15041              :                 }
   15042              :             }
   15043              :         }
   15044              : 
   15045              :       /* We're adding something.  */
   15046       163370 :       if (!data->binding)
   15047              :         {
   15048       132594 :           data->binding = make_binding (data->ns, DECL_NAME (decl));
   15049       132594 :           data->hash->add_namespace_context (data->binding, data->ns);
   15050              : 
   15051       132594 :           depset **slot = data->hash->binding_slot (data->ns,
   15052       132594 :                                                     DECL_NAME (decl), true);
   15053       132594 :           gcc_checking_assert (!*slot);
   15054       132594 :           *slot = data->binding;
   15055              :         }
   15056              : 
   15057              :       /* Make sure nobody left a tree visited lying about.  */
   15058       163370 :       gcc_checking_assert (!TREE_VISITED (decl));
   15059              : 
   15060       163370 :       if (flags & WMB_Using)
   15061              :         {
   15062        21209 :           decl = ovl_make (decl, NULL_TREE);
   15063        21209 :           OVL_USING_P (decl) = true;
   15064        21209 :           OVL_PURVIEW_P (decl) = true;
   15065        21209 :           if (flags & WMB_Export)
   15066        20340 :             OVL_EXPORT_P (decl) = true;
   15067              :         }
   15068              : 
   15069       163370 :       entity_kind ek = EK_FOR_BINDING;
   15070       163370 :       if (internal_decl)
   15071              :         ek = EK_TU_LOCAL;
   15072       163277 :       else if (flags & WMB_Using)
   15073        21209 :         ek = EK_USING;
   15074              : 
   15075       163370 :       depset *dep = data->hash->make_dependency (decl, ek);
   15076       163370 :       if (flags & WMB_Hidden)
   15077           88 :         dep->set_hidden_binding ();
   15078       163370 :       data->binding->deps.safe_push (dep);
   15079              :       /* Binding and contents are mutually dependent.  */
   15080       163370 :       dep->deps.safe_push (data->binding);
   15081              : 
   15082       163370 :       return (flags & WMB_Using
   15083       163370 :               ? flags & WMB_Export : DECL_MODULE_EXPORT_P (decl));
   15084              :     }
   15085         7902 :   else if (!data->met_namespace)
   15086              :     {
   15087              :       /* Namespace, walk exactly once.  */
   15088         7893 :       data->met_namespace = true;
   15089         7893 :       if (data->hash->add_namespace_entities (decl, data->partitions))
   15090              :         {
   15091              :           /* It contains an exported thing, so it is exported.  */
   15092         1567 :           gcc_checking_assert (DECL_MODULE_PURVIEW_P (decl));
   15093         1567 :           gcc_checking_assert (TREE_PUBLIC (decl) || header_module_p ());
   15094         1567 :           DECL_MODULE_EXPORT_P (decl) = true;
   15095              :         }
   15096              : 
   15097         7893 :       if (DECL_MODULE_PURVIEW_P (decl))
   15098              :         {
   15099         1931 :           data->hash->make_dependency (decl, depset::EK_NAMESPACE);
   15100              : 
   15101         1931 :           return DECL_MODULE_EXPORT_P (decl);
   15102              :         }
   15103              :     }
   15104              : 
   15105              :   return false;
   15106              : }
   15107              : 
   15108              : /* Recursively find all the namespace bindings of NS.  Add a depset
   15109              :    for every binding that contains an export or module-linkage entity.
   15110              :    Add a defining depset for every such decl that we need to write a
   15111              :    definition.  Such defining depsets depend on the binding depset.
   15112              :    Returns true if we contain something exported.  */
   15113              : 
   15114              : bool
   15115        10607 : depset::hash::add_namespace_entities (tree ns, bitmap partitions)
   15116              : {
   15117        11810 :   dump () && dump ("Looking for writables in %N", ns);
   15118        10607 :   dump.indent ();
   15119              : 
   15120        10607 :   unsigned count = 0;
   15121        10607 :   add_binding_data data;
   15122        10607 :   data.ns = ns;
   15123        10607 :   data.partitions = partitions;
   15124        10607 :   data.hash = this;
   15125              : 
   15126     14681069 :   for (tree binding : *DECL_NAMESPACE_BINDINGS (ns))
   15127              :     {
   15128      7335231 :       data.binding = nullptr;
   15129      7335231 :       data.met_namespace = false;
   15130      7335231 :       if (walk_module_binding (binding, partitions, add_binding_entity, &data))
   15131       124967 :         count++;
   15132              :     }
   15133              : 
   15134              :   /* Seed any using-directives so that we emit the relevant namespaces.  */
   15135        11168 :   for (tree udir : NAMESPACE_LEVEL (ns)->using_directives)
   15136          193 :     if (TREE_CODE (udir) == USING_DECL && DECL_MODULE_PURVIEW_P (udir))
   15137              :       {
   15138          166 :         make_dependency (USING_DECL_DECLS (udir), depset::EK_NAMESPACE);
   15139          166 :         if (DECL_MODULE_EXPORT_P (udir))
   15140           95 :           count++;
   15141              :       }
   15142              : 
   15143        10607 :   if (count)
   15144         3823 :     dump () && dump ("Found %u entries", count);
   15145        10607 :   dump.outdent ();
   15146              : 
   15147        10607 :   return count != 0;
   15148              : }
   15149              : 
   15150              : void
   15151          203 : depset::hash::add_partial_entities (vec<tree, va_gc> *partial_classes)
   15152              : {
   15153        16911 :   for (unsigned ix = 0; ix != partial_classes->length (); ix++)
   15154              :     {
   15155        16708 :       tree inner = (*partial_classes)[ix];
   15156              : 
   15157        16708 :       depset *dep = make_dependency (inner, depset::EK_DECL);
   15158              : 
   15159        16708 :       if (dep->get_entity_kind () == depset::EK_REDIRECT)
   15160              :         {
   15161        16708 :           dep = dep->deps[0];
   15162              :           /* We should have recorded the template as a partial
   15163              :              specialization.  */
   15164        16708 :           gcc_checking_assert (dep->get_entity_kind ()
   15165              :                                == depset::EK_PARTIAL);
   15166              : 
   15167              :           /* Only emit GM entities if reached.  */
   15168        16708 :           if (!DECL_LANG_SPECIFIC (inner)
   15169        28731 :               || !DECL_MODULE_PURVIEW_P (inner))
   15170         5305 :             dep->set_flag_bit<DB_UNREACHED_BIT> ();
   15171              :         }
   15172              :       else
   15173              :         {
   15174              :           /* It was an explicit specialization, not a partial one.
   15175              :              We should have already added this.  */
   15176            0 :           gcc_checking_assert (dep->get_entity_kind ()
   15177              :                                == depset::EK_SPECIALIZATION);
   15178            0 :           gcc_checking_assert (dep->is_special ());
   15179              :         }
   15180              :     }
   15181          203 : }
   15182              : 
   15183              : /* Add the members of imported classes that we defined in this TU.
   15184              :    This will also include lazily created implicit member function
   15185              :    declarations.  (All others will be definitions.)  */
   15186              : 
   15187              : void
   15188           12 : depset::hash::add_class_entities (vec<tree, va_gc> *class_members)
   15189              : {
   15190           24 :   for (unsigned ix = 0; ix != class_members->length (); ix++)
   15191              :     {
   15192           12 :       tree defn = (*class_members)[ix];
   15193           12 :       depset *dep = make_dependency (defn, EK_INNER_DECL);
   15194              : 
   15195           12 :       if (dep->get_entity_kind () == EK_REDIRECT)
   15196            0 :         dep = dep->deps[0];
   15197              : 
   15198              :       /* Only non-instantiations need marking as pendings.  */
   15199           24 :       if (dep->get_entity_kind () == EK_DECL)
   15200           12 :         dep->set_flag_bit <DB_IS_PENDING_BIT> ();
   15201              :     }
   15202           12 : }
   15203              : 
   15204              : /* Add any entities found via dependent ADL.  */
   15205              : 
   15206              : void
   15207      5248761 : depset::hash::add_dependent_adl_entities (tree expr)
   15208              : {
   15209      5248761 :   gcc_checking_assert (!is_key_order ());
   15210      5248761 :   if (TREE_CODE (current->get_entity ()) != TEMPLATE_DECL)
   15211      5102861 :     return;
   15212              : 
   15213      2899666 :   dep_adl_info info;
   15214      2899666 :   switch (TREE_CODE (expr))
   15215              :     {
   15216       320005 :     case CALL_EXPR:
   15217       320005 :       if (!KOENIG_LOOKUP_P (expr))
   15218              :         return;
   15219        19436 :       info.name = CALL_EXPR_FN (expr);
   15220        19436 :       if (!info.name)
   15221              :         return;
   15222        19356 :       if (TREE_CODE (info.name) == TEMPLATE_ID_EXPR)
   15223         2321 :         info.name = TREE_OPERAND (info.name, 0);
   15224        19356 :       if (TREE_CODE (info.name) == TU_LOCAL_ENTITY)
   15225              :         return;
   15226        28643 :       if (!identifier_p (info.name))
   15227        19098 :         info.name = OVL_NAME (info.name);
   15228        56623 :       for (int ix = 0; ix < call_expr_nargs (expr); ix++)
   15229        37268 :         vec_safe_push (info.args, CALL_EXPR_ARG (expr, ix));
   15230              :       break;
   15231              : 
   15232        26091 :     case LE_EXPR:
   15233        26091 :     case GE_EXPR:
   15234        26091 :     case LT_EXPR:
   15235        26091 :     case GT_EXPR:
   15236        26091 :       info.rewrite = SPACESHIP_EXPR;
   15237        26091 :       goto overloadable_expr;
   15238              : 
   15239        12454 :     case NE_EXPR:
   15240        12454 :       info.rewrite = EQ_EXPR;
   15241        12454 :       goto overloadable_expr;
   15242              : 
   15243        25917 :     case EQ_EXPR:
   15244              :       /* Not strictly a rewrite candidate, but we need to ensure
   15245              :          that lookup of a matching NE_EXPR can succeed if that
   15246              :          would inhibit a rewrite with reversed parameters.  */
   15247        25917 :       info.rewrite = NE_EXPR;
   15248        25917 :       goto overloadable_expr;
   15249              : 
   15250       197964 :     case COMPOUND_EXPR:
   15251       197964 :     case MEMBER_REF:
   15252       197964 :     case MULT_EXPR:
   15253       197964 :     case TRUNC_DIV_EXPR:
   15254       197964 :     case TRUNC_MOD_EXPR:
   15255       197964 :     case PLUS_EXPR:
   15256       197964 :     case MINUS_EXPR:
   15257       197964 :     case LSHIFT_EXPR:
   15258       197964 :     case RSHIFT_EXPR:
   15259       197964 :     case SPACESHIP_EXPR:
   15260       197964 :     case BIT_AND_EXPR:
   15261       197964 :     case BIT_XOR_EXPR:
   15262       197964 :     case BIT_IOR_EXPR:
   15263       197964 :     case TRUTH_ANDIF_EXPR:
   15264       197964 :     case TRUTH_ORIF_EXPR:
   15265       197964 :     overloadable_expr:
   15266       197964 :       info.name = ovl_op_identifier (TREE_CODE (expr));
   15267       197964 :       gcc_checking_assert (tree_operand_length (expr) == 2);
   15268       197964 :       vec_safe_push (info.args, TREE_OPERAND (expr, 0));
   15269       197964 :       vec_safe_push (info.args, TREE_OPERAND (expr, 1));
   15270       197964 :       break;
   15271              : 
   15272              :     default:
   15273              :       return;
   15274              :     }
   15275              : 
   15276              :   /* If all arguments are type-dependent we don't need to do
   15277              :      anything further, we won't find new entities.  */
   15278       363219 :   processing_template_decl_sentinel ptds;
   15279       217319 :   ++processing_template_decl;
   15280       217319 :   if (!any_type_dependent_arguments_p (info.args))
   15281        71419 :     return;
   15282              : 
   15283              :   /* We need to defer name lookup until after walking, otherwise
   15284              :      we get confused by stray TREE_VISITEDs.  */
   15285       145900 :   dep_adl_entity_list.safe_push (info);
   15286              : }
   15287              : 
   15288              : /* We add the partial & explicit specializations, and the explicit
   15289              :    instantiations.  */
   15290              : 
   15291              : static void
   15292       756009 : specialization_add (bool decl_p, spec_entry *entry, void *data_)
   15293              : {
   15294       756009 :   vec<spec_entry *> *data = reinterpret_cast <vec<spec_entry *> *> (data_);
   15295              : 
   15296       756009 :   if (!decl_p)
   15297              :     {
   15298              :       /* We exclusively use decls to locate things.  Make sure there's
   15299              :          no mismatch between the two specialization tables we keep.
   15300              :          pt.cc optimizes instantiation lookup using a complicated
   15301              :          heuristic.  We don't attempt to replicate that algorithm, but
   15302              :          observe its behaviour and reproduce it upon read back.  */
   15303              : 
   15304       229331 :        gcc_checking_assert (TREE_CODE (entry->spec) == ENUMERAL_TYPE
   15305              :                            || DECL_CLASS_TEMPLATE_P (entry->tmpl));
   15306              : 
   15307       229331 :        gcc_checking_assert (!match_mergeable_specialization (true, entry));
   15308              :     }
   15309       526678 :   else if (VAR_OR_FUNCTION_DECL_P (entry->spec))
   15310       259626 :     gcc_checking_assert (!DECL_LOCAL_DECL_P (entry->spec));
   15311              : 
   15312       756009 :   data->safe_push (entry);
   15313       756009 : }
   15314              : 
   15315              : /* Arbitrary stable comparison.  */
   15316              : 
   15317              : static int
   15318     45231274 : specialization_cmp (const void *a_, const void *b_)
   15319              : {
   15320     45231274 :   const spec_entry *ea = *reinterpret_cast<const spec_entry *const *> (a_);
   15321     45231274 :   const spec_entry *eb = *reinterpret_cast<const spec_entry *const *> (b_);
   15322              : 
   15323     45231274 :   if (ea == eb)
   15324              :     return 0;
   15325              : 
   15326     45231274 :   tree a = ea->spec;
   15327     45231274 :   tree b = eb->spec;
   15328     45231274 :   if (TYPE_P (a))
   15329              :     {
   15330     13017932 :       a = TYPE_NAME (a);
   15331     13017932 :       b = TYPE_NAME (b);
   15332              :     }
   15333              : 
   15334     45231274 :   if (a == b)
   15335              :     /* This can happen with friend specializations.  Just order by
   15336              :        entry address.  See note in depset_cmp.  */
   15337            0 :     return ea < eb ? -1 : +1;
   15338              : 
   15339     45231274 :   return DECL_UID (a) < DECL_UID (b) ? -1 : +1;
   15340              : }
   15341              : 
   15342              : /* We add all kinds of specialializations.  Implicit specializations
   15343              :    should only streamed and walked if they are reachable from
   15344              :    elsewhere.  Hence the UNREACHED flag.  This is making the
   15345              :    assumption that it is cheaper to reinstantiate them on demand
   15346              :    elsewhere, rather than stream them in when we instantiate their
   15347              :    general template.  Also, if we do stream them, we can only do that
   15348              :    if they are not internal (which they can become if they themselves
   15349              :    touch an internal entity?).  */
   15350              : 
   15351              : void
   15352         5428 : depset::hash::add_specializations (bool decl_p)
   15353              : {
   15354         5428 :   vec<spec_entry *> data;
   15355         5428 :   data.create (100);
   15356         5428 :   walk_specializations (decl_p, specialization_add, &data);
   15357         5428 :   data.qsort (specialization_cmp);
   15358       761437 :   while (data.length ())
   15359              :     {
   15360       756009 :       spec_entry *entry = data.pop ();
   15361       756009 :       tree spec = entry->spec;
   15362       756009 :       int use_tpl = 0;
   15363       756009 :       bool is_friend = false;
   15364              : 
   15365       756009 :       if (decl_p && DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (entry->tmpl))
   15366              :         /* A friend of a template.  This is keyed to the
   15367              :            instantiation.  */
   15368              :         is_friend = true;
   15369              : 
   15370       756009 :       if (decl_p)
   15371              :         {
   15372       526678 :           if (tree ti = DECL_TEMPLATE_INFO (spec))
   15373              :             {
   15374       526678 :               tree tmpl = TI_TEMPLATE (ti);
   15375              : 
   15376       526678 :               use_tpl = DECL_USE_TEMPLATE (spec);
   15377       526678 :               if (spec == DECL_TEMPLATE_RESULT (tmpl))
   15378              :                 {
   15379         3801 :                   spec = tmpl;
   15380         3801 :                   gcc_checking_assert (DECL_USE_TEMPLATE (spec) == use_tpl);
   15381              :                 }
   15382       522877 :               else if (is_friend)
   15383              :                 {
   15384         3414 :                   if (TI_TEMPLATE (ti) != entry->tmpl
   15385         3414 :                       || !template_args_equal (TI_ARGS (ti), entry->tmpl))
   15386         3414 :                     goto template_friend;
   15387              :                 }
   15388              :             }
   15389              :           else
   15390              :             {
   15391            0 :             template_friend:;
   15392         3414 :               gcc_checking_assert (is_friend);
   15393              :               /* This is a friend of a template class, but not the one
   15394              :                  that generated entry->spec itself (i.e. it's an
   15395              :                  equivalent clone).  We do not need to record
   15396              :                  this.  */
   15397         3414 :               continue;
   15398              :             }
   15399              :         }
   15400              :       else
   15401              :         {
   15402       229331 :           if (TREE_CODE (spec) == ENUMERAL_TYPE)
   15403              :             {
   15404         1176 :               tree ctx = DECL_CONTEXT (TYPE_NAME (spec));
   15405              : 
   15406         1176 :               if (TYPE_P (ctx))
   15407         1170 :                 use_tpl = CLASSTYPE_USE_TEMPLATE (ctx);
   15408              :               else
   15409            6 :                 use_tpl = DECL_USE_TEMPLATE (ctx);
   15410              :             }
   15411              :           else
   15412       228155 :             use_tpl = CLASSTYPE_USE_TEMPLATE (spec);
   15413              : 
   15414       229331 :           tree ti = TYPE_TEMPLATE_INFO (spec);
   15415       229331 :           tree tmpl = TI_TEMPLATE (ti);
   15416              : 
   15417       229331 :           spec = TYPE_NAME (spec);
   15418       229331 :           if (spec == DECL_TEMPLATE_RESULT (tmpl))
   15419              :             {
   15420         1077 :               spec = tmpl;
   15421         1077 :               use_tpl = DECL_USE_TEMPLATE (spec);
   15422              :             }
   15423              :         }
   15424              : 
   15425       752595 :       bool needs_reaching = false;
   15426       752595 :       if (use_tpl == 1)
   15427              :         /* Implicit instantiations only walked if we reach them.  */
   15428              :         needs_reaching = true;
   15429        65335 :       else if (!DECL_LANG_SPECIFIC (STRIP_TEMPLATE (spec))
   15430       120971 :                || !DECL_MODULE_PURVIEW_P (STRIP_TEMPLATE (spec)))
   15431              :         /* Likewise, GMF explicit or partial specializations.  */
   15432              :         needs_reaching = true;
   15433              : 
   15434              : #if false && CHECKING_P
   15435              :       /* The instantiation isn't always on
   15436              :          DECL_TEMPLATE_INSTANTIATIONS, */
   15437              :       // FIXME: we probably need to remember this information?
   15438              :       /* Verify the specialization is on the
   15439              :          DECL_TEMPLATE_INSTANTIATIONS of the template.  */
   15440              :       for (tree cons = DECL_TEMPLATE_INSTANTIATIONS (entry->tmpl);
   15441              :            cons; cons = TREE_CHAIN (cons))
   15442              :         if (TREE_VALUE (cons) == entry->spec)
   15443              :           {
   15444              :             gcc_assert (entry->args == TREE_PURPOSE (cons));
   15445              :             goto have_spec;
   15446              :           }
   15447              :       gcc_unreachable ();
   15448              :     have_spec:;
   15449              : #endif
   15450              : 
   15451              :       /* Make sure nobody left a tree visited lying about.  */
   15452       752595 :       gcc_checking_assert (!TREE_VISITED (spec));
   15453       752595 :       depset *dep = make_dependency (spec, depset::EK_SPECIALIZATION);
   15454       752595 :       if (dep->is_special ())
   15455            0 :         gcc_unreachable ();
   15456              :       else
   15457              :         {
   15458       752595 :           if (dep->get_entity_kind () == depset::EK_REDIRECT)
   15459        18992 :             dep = dep->deps[0];
   15460       733603 :           else if (dep->get_entity_kind () == depset::EK_SPECIALIZATION)
   15461              :             {
   15462       733603 :               dep->set_special ();
   15463       733603 :               dep->deps.safe_push (reinterpret_cast<depset *> (entry));
   15464       733603 :               if (!decl_p)
   15465       213381 :                 dep->set_flag_bit<DB_TYPE_SPEC_BIT> ();
   15466              :             }
   15467              : 
   15468       752595 :           if (needs_reaching)
   15469       708203 :             dep->set_flag_bit<DB_UNREACHED_BIT> ();
   15470       752595 :           if (is_friend)
   15471            0 :             dep->set_flag_bit<DB_FRIEND_SPEC_BIT> ();
   15472              :         }
   15473              :     }
   15474         5428 :   data.release ();
   15475         5428 : }
   15476              : 
   15477              : /* Add a depset into the mergeable hash.  */
   15478              : 
   15479              : void
   15480       862589 : depset::hash::add_mergeable (depset *mergeable)
   15481              : {
   15482       862589 :   gcc_checking_assert (is_key_order ());
   15483       862589 :   entity_kind ek = mergeable->get_entity_kind ();
   15484       862589 :   tree decl = mergeable->get_entity ();
   15485       862589 :   gcc_checking_assert (ek < EK_DIRECT_HWM);
   15486              : 
   15487       862589 :   depset **slot = entity_slot (decl, true);
   15488       862589 :   gcc_checking_assert (!*slot);
   15489       862589 :   depset *dep = make_entity (decl, ek);
   15490       862589 :   *slot = dep;
   15491              : 
   15492       862589 :   worklist.safe_push (dep);
   15493              : 
   15494              :   /* So we can locate the mergeable depset this depset refers to,
   15495              :      mark the first dep.  */
   15496       862589 :   dep->set_special ();
   15497       862589 :   dep->deps.safe_push (mergeable);
   15498       862589 : }
   15499              : 
   15500              : /* Find the innermost-namespace scope of DECL, and that
   15501              :    namespace-scope decl.  */
   15502              : 
   15503              : tree
   15504     28678234 : find_pending_key (tree decl, tree *decl_p = nullptr)
   15505              : {
   15506     28678234 :   tree ns = decl;
   15507     34274717 :   do
   15508              :     {
   15509     34274717 :       decl = ns;
   15510     34274717 :       ns = CP_DECL_CONTEXT (ns);
   15511     34274717 :       if (TYPE_P (ns))
   15512      3518177 :         ns = TYPE_NAME (ns);
   15513              :     }
   15514     34274717 :   while (TREE_CODE (ns) != NAMESPACE_DECL);
   15515              : 
   15516     28678234 :   if (decl_p)
   15517     28289996 :     *decl_p = decl;
   15518              : 
   15519     28678234 :   return ns;
   15520              : }
   15521              : 
   15522              : /* Creates bindings and dependencies for all deduction guides of
   15523              :    the given class template DECL as needed.  */
   15524              : 
   15525              : void
   15526        42267 : depset::hash::add_deduction_guides (tree decl)
   15527              : {
   15528              :   /* Alias templates never have deduction guides.  */
   15529        42267 :   if (DECL_ALIAS_TEMPLATE_P (decl))
   15530        41593 :     return;
   15531              : 
   15532              :   /* We don't need to do anything for class-scope deduction guides,
   15533              :      as they will be added as members anyway.  */
   15534        42267 :   if (!DECL_NAMESPACE_SCOPE_P (decl))
   15535              :     return;
   15536              : 
   15537        33907 :   tree ns = CP_DECL_CONTEXT (decl);
   15538        33907 :   tree name = dguide_name (decl);
   15539              : 
   15540              :   /* We always add all deduction guides with a given name at once,
   15541              :      so if there's already a binding there's nothing to do.  */
   15542        33907 :   if (find_binding (ns, name))
   15543              :     return;
   15544              : 
   15545        31991 :   tree guides = lookup_qualified_name (ns, name, LOOK_want::NORMAL,
   15546              :                                        /*complain=*/false);
   15547        31991 :   if (guides == error_mark_node)
   15548              :     return;
   15549              : 
   15550          674 :   depset *binding = nullptr;
   15551         3088 :   for (tree t : lkp_range (guides))
   15552              :     {
   15553         1740 :       gcc_checking_assert (!TREE_VISITED (t));
   15554         1740 :       depset *dep = make_dependency (t, EK_FOR_BINDING);
   15555              : 
   15556              :       /* We don't want to create bindings for imported deduction guides, as
   15557              :          this would potentially cause name lookup to return duplicates.  */
   15558         1740 :       if (dep->is_import ())
   15559            6 :         continue;
   15560              : 
   15561         1734 :       if (!binding)
   15562              :         {
   15563              :           /* We have bindings to add.  */
   15564          668 :           binding = make_binding (ns, name);
   15565          668 :           add_namespace_context (binding, ns);
   15566              : 
   15567          668 :           depset **slot = binding_slot (ns, name, /*insert=*/true);
   15568          668 :           *slot = binding;
   15569              :         }
   15570              : 
   15571         1734 :       binding->deps.safe_push (dep);
   15572         1734 :       dep->deps.safe_push (binding);
   15573         1734 :       dump (dumper::DEPEND)
   15574            0 :         && dump ("Built binding for deduction guide %C:%N",
   15575            0 :                  TREE_CODE (decl), decl);
   15576              :     }
   15577              : }
   15578              : 
   15579              : /* Iteratively find dependencies.  During the walk we may find more
   15580              :    entries on the same binding that need walking.  */
   15581              : 
   15582              : void
   15583       247628 : depset::hash::find_dependencies (module_state *module)
   15584              : {
   15585       247628 :   trees_out walker (NULL, module, *this);
   15586       247628 :   vec<depset *> unreached;
   15587       495256 :   unreached.create (worklist.length ());
   15588              : 
   15589         1062 :   for (;;)
   15590              :     {
   15591       248690 :       reached_unreached = false;
   15592      3698946 :       while (worklist.length ())
   15593              :         {
   15594      3450256 :           depset *item = worklist.pop ();
   15595              : 
   15596      3450256 :           gcc_checking_assert (!item->is_binding ());
   15597      3450256 :           if (item->is_unreached ())
   15598      1698458 :             unreached.quick_push (item);
   15599              :           else
   15600              :             {
   15601      1751798 :               current = item;
   15602      1751798 :               tree decl = current->get_entity ();
   15603      1751798 :               dump (is_key_order () ? dumper::MERGE : dumper::DEPEND)
   15604      1753112 :                 && dump ("Dependencies of %s %C:%N",
   15605         1314 :                          is_key_order () ? "key-order"
   15606         1314 :                          : current->entity_kind_name (), TREE_CODE (decl), decl);
   15607      1751798 :               dump.indent ();
   15608      1751798 :               walker.begin ();
   15609      1751798 :               if (current->get_entity_kind () == EK_USING)
   15610        21209 :                 walker.tree_node (OVL_FUNCTION (decl));
   15611      1730589 :               else if (current->get_entity_kind () == EK_TU_LOCAL)
   15612              :                 /* We only stream its name and location.  */
   15613           93 :                 module->note_location (DECL_SOURCE_LOCATION (decl));
   15614      1730496 :               else if (TREE_VISITED (decl))
   15615              :                 /* A global tree.  */;
   15616      1728036 :               else if (current->get_entity_kind () == EK_NAMESPACE)
   15617              :                 {
   15618         2241 :                   module->note_location (DECL_SOURCE_LOCATION (decl));
   15619         2241 :                   add_namespace_context (current, CP_DECL_CONTEXT (decl));
   15620              :                 }
   15621              :               else
   15622              :                 {
   15623      1725795 :                   walker.mark_declaration (decl, current->has_defn ());
   15624              : 
   15625      1725795 :                   if (!is_key_order ()
   15626      1725795 :                       && item->is_pending_entity ())
   15627              :                     {
   15628       388238 :                       tree ns = find_pending_key (decl, nullptr);
   15629       388238 :                       add_namespace_context (item, ns);
   15630              :                     }
   15631              : 
   15632      1725795 :                   auto ovr = make_temp_override
   15633      1725795 :                     (ignore_exposure, item->is_ignored_exposure_context ());
   15634      1725795 :                   walker.decl_value (decl, current);
   15635      1725795 :                   if (current->has_defn ())
   15636       333233 :                     walker.write_definition (decl, current->refs_tu_local ());
   15637      1725795 :                 }
   15638      1751798 :               walker.end ();
   15639              : 
   15640              :               /* If we see either a class template or a deduction guide, make
   15641              :                  sure to add all visible deduction guides.  We need to check
   15642              :                  both in case they have been added in separate modules, or
   15643              :                  one is in the GMF and would have otherwise been discarded.  */
   15644      1751798 :               if (!is_key_order ()
   15645      1751798 :                   && DECL_CLASS_TEMPLATE_P (decl))
   15646        40527 :                 add_deduction_guides (decl);
   15647      1751798 :               if (!is_key_order ()
   15648      1751798 :                   && deduction_guide_p (decl))
   15649         1740 :                 add_deduction_guides (TYPE_NAME (TREE_TYPE (TREE_TYPE (decl))));
   15650              : 
   15651              :               /* Handle dependent ADL for [module.global.frag] p3.3.  */
   15652      1751798 :               if (!is_key_order () && !dep_adl_entity_list.is_empty ())
   15653              :                 {
   15654        53640 :                   processing_template_decl_sentinel ptds;
   15655        53640 :                   ++processing_template_decl;
   15656       199540 :                   for (auto &info : dep_adl_entity_list)
   15657              :                     {
   15658       145900 :                       tree lookup = lookup_arg_dependent (info.name, NULL_TREE,
   15659              :                                                           info.args, true);
   15660       331436 :                       for (tree fn : lkp_range (lookup))
   15661        39636 :                         add_dependency (make_dependency (fn, EK_DECL));
   15662              : 
   15663       145900 :                       if (info.rewrite)
   15664              :                         {
   15665        44717 :                           tree rewrite_name = ovl_op_identifier (info.rewrite);
   15666        44717 :                           lookup = lookup_arg_dependent (rewrite_name, NULL_TREE,
   15667              :                                                          info.args, true);
   15668       100245 :                           for (tree fn : lkp_range (lookup))
   15669        10811 :                             add_dependency (make_dependency (fn, EK_DECL));
   15670              :                         }
   15671       145900 :                       release_tree_vector (info.args);
   15672              :                     }
   15673        53640 :                   dep_adl_entity_list.truncate (0);
   15674        53640 :                 }
   15675              : 
   15676      1751798 :               if (!is_key_order ()
   15677       889209 :                   && TREE_CODE (decl) == TEMPLATE_DECL
   15678      2057522 :                   && !DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (decl))
   15679              :                 {
   15680              :                   /* Mark all the explicit & partial specializations as
   15681              :                      reachable.  We search both specialization lists as some
   15682              :                      constrained partial specializations for class types are
   15683              :                      only found in DECL_TEMPLATE_SPECIALIZATIONS.  */
   15684       864064 :                   auto mark_reached = [this](tree spec)
   15685              :                     {
   15686       563901 :                       if (TYPE_P (spec))
   15687       167655 :                         spec = TYPE_NAME (spec);
   15688       563901 :                       int use_tpl;
   15689       563901 :                       node_template_info (spec, use_tpl);
   15690       563901 :                       if (use_tpl & 2)
   15691              :                         {
   15692        64034 :                           depset *spec_dep = find_dependency (spec);
   15693        64034 :                           if (spec_dep->get_entity_kind () == EK_REDIRECT)
   15694        14322 :                             spec_dep = spec_dep->deps[0];
   15695        64034 :                           if (spec_dep->is_unreached ())
   15696              :                             {
   15697         4266 :                               reached_unreached = true;
   15698         4266 :                               spec_dep->clear_flag_bit<DB_UNREACHED_BIT> ();
   15699         4266 :                               dump (dumper::DEPEND)
   15700            0 :                                 && dump ("Reaching unreached specialization"
   15701            0 :                                          " %C:%N", TREE_CODE (spec), spec);
   15702              :                             }
   15703              :                         }
   15704       864064 :                     };
   15705              : 
   15706       300163 :                   for (tree cons = DECL_TEMPLATE_INSTANTIATIONS (decl);
   15707       849136 :                        cons; cons = TREE_CHAIN (cons))
   15708       548973 :                     mark_reached (TREE_VALUE (cons));
   15709       300163 :                   for (tree cons = DECL_TEMPLATE_SPECIALIZATIONS (decl);
   15710       315091 :                        cons; cons = TREE_CHAIN (cons))
   15711        14928 :                     mark_reached (TREE_VALUE (cons));
   15712              :                 }
   15713              : 
   15714      1751798 :               dump.outdent ();
   15715      1751798 :               current = NULL;
   15716              :             }
   15717              :         }
   15718              : 
   15719       248690 :       if (!reached_unreached)
   15720              :         break;
   15721              : 
   15722              :       /* It's possible the we reached the unreached before we
   15723              :          processed it in the above loop, so we'll be doing this an
   15724              :          extra time.  However, to avoid that we have to do some
   15725              :          bit shuffling that also involves a scan of the list.
   15726              :          Swings & roundabouts I guess.  */
   15727         1062 :       std::swap (worklist, unreached);
   15728              :     }
   15729              : 
   15730       247628 :   unreached.release ();
   15731       247628 : }
   15732              : 
   15733              : /* Compare two entries of a single binding.  TYPE_DECL before
   15734              :    non-exported before exported.  */
   15735              : 
   15736              : static int
   15737       447028 : binding_cmp (const void *a_, const void *b_)
   15738              : {
   15739       447028 :   depset *a = *(depset *const *)a_;
   15740       447028 :   depset *b = *(depset *const *)b_;
   15741              : 
   15742       447028 :   tree a_ent = a->get_entity ();
   15743       447028 :   tree b_ent = b->get_entity ();
   15744       447028 :   gcc_checking_assert (a_ent != b_ent
   15745              :                        && !a->is_binding ()
   15746              :                        && !b->is_binding ());
   15747              : 
   15748              :   /* Implicit typedefs come first.  */
   15749       447028 :   bool a_implicit = DECL_IMPLICIT_TYPEDEF_P (a_ent);
   15750       447028 :   bool b_implicit = DECL_IMPLICIT_TYPEDEF_P (b_ent);
   15751       446902 :   if (a_implicit || b_implicit)
   15752              :     {
   15753              :       /* A binding with two implicit type decls?  That's unpossible!  */
   15754          252 :       gcc_checking_assert (!(a_implicit && b_implicit));
   15755          378 :       return a_implicit ? -1 : +1;  /* Implicit first.  */
   15756              :     }
   15757              : 
   15758              :   /* TU-local before non-TU-local.  */
   15759       446776 :   bool a_internal = a->get_entity_kind () == depset::EK_TU_LOCAL;
   15760       446776 :   bool b_internal = b->get_entity_kind () == depset::EK_TU_LOCAL;
   15761       446776 :   if (a_internal != b_internal)
   15762            0 :     return a_internal ? -1 : +1;  /* Internal first.  */
   15763              : 
   15764              :   /* Hidden before non-hidden.  */
   15765       446776 :   bool a_hidden = a->is_hidden ();
   15766       446776 :   bool b_hidden = b->is_hidden ();
   15767       446776 :   if (a_hidden != b_hidden)
   15768            0 :     return a_hidden ? -1 : +1;
   15769              : 
   15770       446776 :   bool a_using = a->get_entity_kind () == depset::EK_USING;
   15771       446776 :   bool a_export;
   15772       446776 :   if (a_using)
   15773              :     {
   15774        73851 :       a_export = OVL_EXPORT_P (a_ent);
   15775        73851 :       a_ent = OVL_FUNCTION (a_ent);
   15776              :     }
   15777       372925 :   else if (TREE_CODE (a_ent) == CONST_DECL
   15778            0 :            && DECL_LANG_SPECIFIC (a_ent)
   15779       372925 :            && DECL_MODULE_EXPORT_P (a_ent))
   15780              :     a_export = true;
   15781              :   else
   15782       372925 :     a_export = DECL_MODULE_EXPORT_P (TREE_CODE (a_ent) == CONST_DECL
   15783              :                                      ? TYPE_NAME (TREE_TYPE (a_ent))
   15784              :                                      : STRIP_TEMPLATE (a_ent));
   15785              : 
   15786       446776 :   bool b_using = b->get_entity_kind () == depset::EK_USING;
   15787       446776 :   bool b_export;
   15788       446776 :   if (b_using)
   15789              :     {
   15790        70129 :       b_export = OVL_EXPORT_P (b_ent);
   15791        70129 :       b_ent = OVL_FUNCTION (b_ent);
   15792              :     }
   15793       376647 :   else if (TREE_CODE (b_ent) == CONST_DECL
   15794            0 :            && DECL_LANG_SPECIFIC (b_ent)
   15795       376647 :            && DECL_MODULE_EXPORT_P (b_ent))
   15796              :     b_export = true;
   15797              :   else
   15798       376647 :     b_export = DECL_MODULE_EXPORT_P (TREE_CODE (b_ent) == CONST_DECL
   15799              :                                      ? TYPE_NAME (TREE_TYPE (b_ent))
   15800              :                                      : STRIP_TEMPLATE (b_ent));
   15801              : 
   15802              :   /* Non-exports before exports.  */
   15803       446776 :   if (a_export != b_export)
   15804         2652 :     return a_export ? +1 : -1;
   15805              : 
   15806              :   /* At this point we don't care, but want a stable sort.  */
   15807              : 
   15808       445086 :   if (a_using != b_using)
   15809              :     /* using first.  */
   15810        24593 :     return a_using? -1 : +1;
   15811              : 
   15812       427373 :   return DECL_UID (a_ent) < DECL_UID (b_ent) ? -1 : +1;
   15813              : }
   15814              : 
   15815              : /* True iff TMPL has an explicit instantiation definition.
   15816              : 
   15817              :    This is local to module.cc because register_specialization skips adding most
   15818              :    instantiations unless module_maybe_has_cmi_p.  */
   15819              : 
   15820              : static bool
   15821           76 : template_has_explicit_inst (tree tmpl)
   15822              : {
   15823           88 :   for (tree t = DECL_TEMPLATE_INSTANTIATIONS (tmpl); t; t = TREE_CHAIN (t))
   15824              :     {
   15825           24 :       tree spec = TREE_VALUE (t);
   15826           24 :       if (DECL_EXPLICIT_INSTANTIATION (spec)
   15827           24 :           && !DECL_REALLY_EXTERN (spec))
   15828              :         return true;
   15829              :     }
   15830              :   return false;
   15831              : }
   15832              : 
   15833              : /* Complain about DEP that exposes a TU-local entity.
   15834              : 
   15835              :    If STRICT, DEP only referenced entities from the GMF.  Returns TRUE
   15836              :    if we explained anything.  */
   15837              : 
   15838              : bool
   15839          127 : depset::hash::diagnose_bad_internal_ref (depset *dep, bool strict)
   15840              : {
   15841          127 :   tree decl = dep->get_entity ();
   15842              : 
   15843              :   /* Don't need to walk if we're not going to be emitting
   15844              :      any diagnostics anyway.  */
   15845          148 :   if (strict && !warning_enabled_at (DECL_SOURCE_LOCATION (decl),
   15846           21 :                                      OPT_Wexpose_global_module_tu_local))
   15847              :     return false;
   15848              : 
   15849          523 :   for (depset *rdep : dep->deps)
   15850          135 :     if (!rdep->is_binding () && rdep->is_tu_local (strict)
   15851          369 :         && !is_exposure_of_member_type (dep, rdep))
   15852              :       {
   15853              :         // FIXME:QOI Better location information?  We're
   15854              :         // losing, so it doesn't matter about efficiency.
   15855          118 :         tree exposed = rdep->get_entity ();
   15856          118 :         auto_diagnostic_group d;
   15857          118 :         if (strict)
   15858              :           {
   15859              :             /* Allow suppressing the warning from the point of declaration
   15860              :                of the otherwise-exposed decl, for cases we know that
   15861              :                exposures will never be 'bad'.  */
   15862           27 :             if (warning_enabled_at (DECL_SOURCE_LOCATION (exposed),
   15863           27 :                                     OPT_Wexpose_global_module_tu_local)
   15864           45 :                 && pedwarn (DECL_SOURCE_LOCATION (decl),
   15865           18 :                             OPT_Wexpose_global_module_tu_local,
   15866              :                             "%qD exposes TU-local entity %qD", decl, exposed))
   15867              :               {
   15868           18 :                 bool informed = is_tu_local_entity (exposed, /*explain=*/true);
   15869           18 :                 gcc_checking_assert (informed);
   15870              :                 return true;
   15871              :               }
   15872              :           }
   15873              :         else
   15874              :           {
   15875           91 :             error_at (DECL_SOURCE_LOCATION (decl),
   15876              :                       "%qD exposes TU-local entity %qD", decl, exposed);
   15877           91 :             bool informed = is_tu_local_entity (exposed, /*explain=*/true);
   15878           91 :             gcc_checking_assert (informed);
   15879           91 :             if (dep->is_tu_local (/*strict=*/true))
   15880            3 :               inform (DECL_SOURCE_LOCATION (decl),
   15881              :                       "%qD is also TU-local but has been exposed elsewhere",
   15882              :                       decl);
   15883           91 :             return true;
   15884              :           }
   15885          118 :       }
   15886              : 
   15887              :   return false;
   15888              : }
   15889              : 
   15890              : /* Warn about a template DEP that references a TU-local entity.
   15891              : 
   15892              :    If STRICT, DEP only referenced entities from the GMF.  Returns TRUE
   15893              :    if we explained anything.  */
   15894              : 
   15895              : bool
   15896           94 : depset::hash::diagnose_template_names_tu_local (depset *dep, bool strict)
   15897              : {
   15898           94 :   tree decl = dep->get_entity ();
   15899              : 
   15900              :   /* Don't bother walking if we know we won't be emitting anything.  */
   15901           94 :   if (!warning_enabled_at (DECL_SOURCE_LOCATION (decl),
   15902           94 :                            OPT_Wtemplate_names_tu_local)
   15903              :       /* Only warn strictly if users haven't silenced this warning here.  */
   15904          121 :       || (strict && !warning_enabled_at (DECL_SOURCE_LOCATION (decl),
   15905           27 :                                          OPT_Wexpose_global_module_tu_local)))
   15906            0 :     return false;
   15907              : 
   15908              :   /* Friend decls in a class body are ignored, but this is harmless:
   15909              :      it should not impact any consumers.  */
   15910           94 :   if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (decl)))
   15911              :     return false;
   15912              : 
   15913              :   /* We should now only be warning about templates.  */
   15914           76 :   gcc_checking_assert
   15915              :     (TREE_CODE (decl) == TEMPLATE_DECL
   15916              :      && VAR_OR_FUNCTION_DECL_P (DECL_TEMPLATE_RESULT (decl)));
   15917              : 
   15918              :   /* Don't warn if we've seen any explicit instantiation definitions,
   15919              :      the intent might be for importers to only use those.  */
   15920           76 :   if (template_has_explicit_inst (decl))
   15921              :     return false;
   15922              : 
   15923          268 :   for (depset *rdep : dep->deps)
   15924          134 :     if (!rdep->is_binding () && rdep->is_tu_local (strict))
   15925              :       {
   15926           67 :         tree ref = rdep->get_entity ();
   15927           67 :         auto_diagnostic_group d;
   15928           67 :         if (strict)
   15929              :           {
   15930           15 :             if (warning_enabled_at (DECL_SOURCE_LOCATION (ref),
   15931           15 :                                     OPT_Wexpose_global_module_tu_local)
   15932           21 :                 && warning_at (DECL_SOURCE_LOCATION (decl),
   15933            6 :                                OPT_Wtemplate_names_tu_local,
   15934              :                                "%qD refers to TU-local entity %qD, which may "
   15935              :                                "cause issues when instantiating in other TUs",
   15936              :                                decl, ref))
   15937              :               {
   15938            6 :                 is_tu_local_entity (ref, /*explain=*/true);
   15939            6 :                 return true;
   15940              :               }
   15941              :           }
   15942           52 :         else if (warning_at (DECL_SOURCE_LOCATION (decl),
   15943           52 :                              OPT_Wtemplate_names_tu_local,
   15944              :                              "%qD refers to TU-local entity %qD and cannot "
   15945              :                              "be instantiated in other TUs", decl, ref))
   15946              :           {
   15947           52 :             is_tu_local_entity (ref, /*explain=*/true);
   15948           52 :             return true;
   15949              :           }
   15950           67 :       }
   15951              : 
   15952              :   return false;
   15953              : }
   15954              : 
   15955              : /* Sort the bindings, issue errors about bad internal refs.  */
   15956              : 
   15957              : bool
   15958         2714 : depset::hash::finalize_dependencies ()
   15959              : {
   15960         2714 :   bool ok = true;
   15961      2885732 :   for (depset *dep : *this)
   15962              :     {
   15963      1441509 :       if (dep->is_binding ())
   15964              :         {
   15965              :           /* Keep the containing namespace dep first.  */
   15966       134530 :           gcc_checking_assert (dep->deps.length () > 1
   15967              :                                && (dep->deps[0]->get_entity_kind ()
   15968              :                                    == EK_NAMESPACE)
   15969              :                                && (dep->deps[0]->get_entity ()
   15970              :                                    == dep->get_entity ()));
   15971       134530 :           if (dep->deps.length () > 2)
   15972         9623 :             gcc_qsort (&dep->deps[1], dep->deps.length () - 1,
   15973              :                        sizeof (dep->deps[1]), binding_cmp);
   15974              : 
   15975              :           /* Bindings shouldn't refer to imported entities.  */
   15976       134530 :           if (CHECKING_P)
   15977       705984 :             for (depset *entity : dep->deps)
   15978       302394 :               gcc_checking_assert (!entity->is_import ());
   15979       134530 :           continue;
   15980       134530 :         }
   15981              : 
   15982              :       /* Otherwise, we'll check for bad internal refs.
   15983              :          Don't complain about any references from TU-local entities.  */
   15984      1306979 :       if (dep->is_tu_local ())
   15985          264 :         continue;
   15986              : 
   15987      1306715 :       if (dep->is_exposure ())
   15988              :         {
   15989          106 :           bool explained = diagnose_bad_internal_ref (dep);
   15990              : 
   15991              :           /* A TU-local variable will always be considered an exposure,
   15992              :              so we don't have to worry about strict-only handling.  */
   15993          106 :           tree decl = dep->get_entity ();
   15994          106 :           if (!explained
   15995           15 :               && VAR_P (decl)
   15996          121 :               && (DECL_DECLARED_CONSTEXPR_P (decl)
   15997            6 :                   || DECL_INLINE_VAR_P (decl)))
   15998              :             {
   15999           15 :               auto_diagnostic_group d;
   16000           15 :               if (DECL_DECLARED_CONSTEXPR_P (decl))
   16001            9 :                 error_at (DECL_SOURCE_LOCATION (decl),
   16002              :                           "%qD is declared %<constexpr%> and is initialized to "
   16003              :                           "a TU-local value", decl);
   16004              :               else
   16005              :                 {
   16006              :                   /* This can only occur with references.  */
   16007            6 :                   gcc_checking_assert (TYPE_REF_P (TREE_TYPE (decl)));
   16008            6 :                   error_at (DECL_SOURCE_LOCATION (decl),
   16009              :                             "%qD is a reference declared %<inline%> and is "
   16010              :                             "constant-initialized to a TU-local value", decl);
   16011              :                 }
   16012           15 :               bool informed = is_tu_local_value (decl, DECL_INITIAL (decl),
   16013              :                                                  /*explain=*/true);
   16014           15 :               gcc_checking_assert (informed);
   16015           15 :               explained = true;
   16016           15 :             }
   16017              : 
   16018              :           /* We should have emitted an error above, unless the warning was
   16019              :              silenced.  */
   16020          106 :           gcc_checking_assert (explained);
   16021          106 :           ok = false;
   16022          106 :           continue;
   16023          106 :         }
   16024              : 
   16025              :       /* In all other cases, we're just warning (rather than erroring).
   16026              :          We don't want to do too much warning, so let's just bail after
   16027              :          the first warning we successfully emit.  */
   16028      1306627 :       if (warn_expose_global_module_tu_local
   16029      1306609 :           && !dep->is_tu_local (/*strict=*/true)
   16030      1306579 :           && dep->is_exposure (/*strict=*/true)
   16031      1306630 :           && diagnose_bad_internal_ref (dep, /*strict=*/true))
   16032           18 :         continue;
   16033              : 
   16034      1306643 :       if (warn_template_names_tu_local
   16035        84664 :           && dep->refs_tu_local ()
   16036      1306658 :           && diagnose_template_names_tu_local (dep))
   16037           52 :         continue;
   16038              : 
   16039      1306539 :       if (warn_template_names_tu_local
   16040        84612 :           && warn_expose_global_module_tu_local
   16041        84612 :           && !dep->is_tu_local (/*strict=*/true)
   16042        84588 :           && dep->refs_tu_local (/*strict=*/true)
   16043           30 :           && !dep->is_exposure (/*strict=*/true)
   16044      1306566 :           && diagnose_template_names_tu_local (dep, /*strict=*/true))
   16045              :         continue;
   16046              :     }
   16047              : 
   16048         2714 :   return ok;
   16049              : }
   16050              : 
   16051              : /* Core of TARJAN's algorithm to find Strongly Connected Components
   16052              :    within a graph.  See https://en.wikipedia.org/wiki/
   16053              :    Tarjan%27s_strongly_connected_components_algorithm for details.
   16054              : 
   16055              :    We use depset::section as lowlink.  Completed nodes have
   16056              :    depset::cluster containing the cluster number, with the top
   16057              :    bit set.
   16058              : 
   16059              :    A useful property is that the output vector is a reverse
   16060              :    topological sort of the resulting DAG.  In our case that means
   16061              :    dependent SCCs are found before their dependers.  We make use of
   16062              :    that property.  */
   16063              : 
   16064              : void
   16065      1885308 : depset::tarjan::connect (depset *v)
   16066              : {
   16067      1885308 :   gcc_checking_assert (v->is_binding ()
   16068              :                        || !(v->is_tu_local ()
   16069              :                             || v->is_unreached ()
   16070              :                             || v->is_import ()));
   16071              : 
   16072      1885308 :   v->cluster = v->section = ++index;
   16073      1885308 :   stack.safe_push (v);
   16074              : 
   16075              :   /* Walk all our dependencies, ignore a first marked slot  */
   16076     15755015 :   for (unsigned ix = v->is_special (); ix != v->deps.length (); ix++)
   16077              :     {
   16078      5996156 :       depset *dep = v->deps[ix];
   16079              : 
   16080      5996156 :       if (dep->is_binding ()
   16081     11824753 :           || !(dep->is_import () || dep->is_tu_local ()))
   16082              :         {
   16083      5986536 :           unsigned lwm = dep->cluster;
   16084              : 
   16085      5986536 :           if (!dep->cluster)
   16086              :             {
   16087              :               /* A new node.  Connect it.  */
   16088      1039960 :               connect (dep);
   16089      1039960 :               lwm = dep->section;
   16090              :             }
   16091              : 
   16092      5986536 :           if (dep->section && v->section > lwm)
   16093       947054 :             v->section = lwm;
   16094              :         }
   16095              :     }
   16096              : 
   16097      1885308 :   if (v->section == v->cluster)
   16098              :     {
   16099              :       /* Root of a new SCC.  Push all the members onto the result list. */
   16100              :       unsigned num = v->cluster;
   16101      1885308 :       depset *p;
   16102      1885308 :       do
   16103              :         {
   16104      1885308 :           p = stack.pop ();
   16105      1885308 :           p->cluster = num;
   16106      1885308 :           p->section = 0;
   16107      1885308 :           result.quick_push (p);
   16108              :         }
   16109      1885308 :       while (p != v);
   16110              :     }
   16111      1885308 : }
   16112              : 
   16113              : /* Compare two depsets.  The specific ordering is unimportant, we're
   16114              :    just trying to get consistency.  */
   16115              : 
   16116              : static int
   16117     87877678 : depset_cmp (const void *a_, const void *b_)
   16118              : {
   16119     87877678 :   depset *a = *(depset *const *)a_;
   16120     87877678 :   depset *b = *(depset *const *)b_;
   16121              : 
   16122     87877678 :   depset::entity_kind a_kind = a->get_entity_kind ();
   16123     87877678 :   depset::entity_kind b_kind = b->get_entity_kind ();
   16124              : 
   16125     87877678 :   if  (a_kind != b_kind)
   16126              :     /* Different entity kinds, order by that.  */
   16127      4572609 :     return a_kind < b_kind ? -1 : +1;
   16128              : 
   16129     84643956 :   tree a_decl = a->get_entity ();
   16130     84643956 :   tree b_decl = b->get_entity ();
   16131     84643956 :   if (a_kind == depset::EK_USING)
   16132              :     {
   16133              :       /* If one is a using, the other must be too.  */
   16134      1190716 :       a_decl = OVL_FUNCTION (a_decl);
   16135      1190716 :       b_decl = OVL_FUNCTION (b_decl);
   16136              :     }
   16137              : 
   16138     84643956 :   if (a_decl != b_decl)
   16139              :     /* Different entities, order by their UID.  */
   16140     78032594 :     return DECL_UID (a_decl) < DECL_UID (b_decl) ? -1 : +1;
   16141              : 
   16142      6611362 :   if (a_kind == depset::EK_BINDING)
   16143              :     {
   16144              :       /* Both are bindings.  Order by identifier hash.  */
   16145      6608249 :       gcc_checking_assert (a->get_name () != b->get_name ());
   16146      6608249 :       hashval_t ah = IDENTIFIER_HASH_VALUE (a->get_name ());
   16147      6608249 :       hashval_t bh = IDENTIFIER_HASH_VALUE (b->get_name ());
   16148      9845681 :       return (ah == bh ? 0 : ah < bh ? -1 : +1);
   16149              :     }
   16150              : 
   16151              :   /* They are the same decl.  This can happen with two using decls
   16152              :      pointing to the same target.  The best we can aim for is
   16153              :      consistently telling qsort how to order them.  Hopefully we'll
   16154              :      never have to debug a case that depends on this.  Oh, who am I
   16155              :      kidding?  Good luck.  */
   16156         3113 :   gcc_checking_assert (a_kind == depset::EK_USING);
   16157              : 
   16158              :   /* Order by depset address.  Not the best, but it is something.  */
   16159         3113 :   return a < b ? -1 : +1;
   16160              : }
   16161              : 
   16162              : /* Sort the clusters in SCC such that those that depend on one another
   16163              :    are placed later.   */
   16164              : 
   16165              : // FIXME: I am not convinced this is needed and, if needed,
   16166              : // sufficient.  We emit the decls in this order but that emission
   16167              : // could walk into later decls (from the body of the decl, or default
   16168              : // arg-like things).  Why doesn't that walk do the right thing?  And
   16169              : // if it DTRT why do we need to sort here -- won't things naturally
   16170              : // work?  I think part of the issue is that when we're going to refer
   16171              : // to an entity by name, and that entity is in the same cluster as us,
   16172              : // we need to actually walk that entity, if we've not already walked
   16173              : // it.
   16174              : static void
   16175       244914 : sort_cluster (depset::hash *original, depset *scc[], unsigned size)
   16176              : {
   16177       244914 :   depset::hash table (size, original);
   16178              : 
   16179       244914 :   dump.indent ();
   16180              : 
   16181              :   /* Place bindings last, usings before that.  It's not strictly
   16182              :      necessary, but it does make things neater.  Says Mr OCD.  */
   16183              :   unsigned bind_lwm = size;
   16184              :   unsigned use_lwm = size;
   16185      1262982 :   for (unsigned ix = 0; ix != use_lwm;)
   16186              :     {
   16187      1018068 :       depset *dep = scc[ix];
   16188      1018068 :       switch (dep->get_entity_kind ())
   16189              :         {
   16190       134273 :         case depset::EK_BINDING:
   16191              :           /* Move to end.  No increment.  Notice this could be moving
   16192              :              a using decl, which we'll then move again.  */
   16193       134273 :           if (--bind_lwm != ix)
   16194              :             {
   16195        71947 :               scc[ix] = scc[bind_lwm];
   16196        71947 :               scc[bind_lwm] = dep;
   16197              :             }
   16198       134273 :           if (use_lwm > bind_lwm)
   16199              :             {
   16200       112697 :               use_lwm--;
   16201       112697 :               break;
   16202              :             }
   16203              :           /* We must have copied a using or TU-local, so move it too.  */
   16204        21576 :           dep = scc[ix];
   16205        21576 :           gcc_checking_assert
   16206              :             (dep->get_entity_kind () == depset::EK_USING
   16207              :              || dep->get_entity_kind () == depset::EK_TU_LOCAL);
   16208              :           /* FALLTHROUGH  */
   16209              : 
   16210        42782 :         case depset::EK_USING:
   16211        42782 :         case depset::EK_TU_LOCAL:
   16212        42782 :           if (--use_lwm != ix)
   16213              :             {
   16214        29562 :               scc[ix] = scc[use_lwm];
   16215        29562 :               scc[use_lwm] = dep;
   16216              :             }
   16217              :           break;
   16218              : 
   16219       862589 :         case depset::EK_DECL:
   16220       862589 :         case depset::EK_SPECIALIZATION:
   16221       862589 :         case depset::EK_PARTIAL:
   16222       862589 :           table.add_mergeable (dep);
   16223       862589 :           ix++;
   16224       862589 :           break;
   16225              : 
   16226            0 :         default:
   16227            0 :           gcc_unreachable ();
   16228              :         }
   16229              :     }
   16230              : 
   16231       244914 :   gcc_checking_assert (use_lwm <= bind_lwm);
   16232       245199 :   dump (dumper::MERGE) && dump ("Ordering %u/%u depsets", use_lwm, size);
   16233              : 
   16234       244914 :   table.find_dependencies (nullptr);
   16235              : 
   16236       244914 :   auto_vec<depset *> order = table.connect ();
   16237       489828 :   gcc_checking_assert (order.length () == use_lwm);
   16238              : 
   16239              :   /* Now rewrite entries [0,lwm), in the dependency order we
   16240              :      discovered.  Usually each entity is in its own cluster.  Rarely,
   16241              :      we can get multi-entity clusters, in which case all but one must
   16242              :      only be reached from within the cluster.  This happens for
   16243              :      something like:
   16244              : 
   16245              :      template<typename T>
   16246              :      auto Foo (const T &arg) -> TPL<decltype (arg)>;
   16247              : 
   16248              :      The instantiation of TPL will be in the specialization table, and
   16249              :      refer to Foo via arg.  But we can only get to that specialization
   16250              :      from Foo's declaration, so we only need to treat Foo as mergable
   16251              :      (We'll do structural comparison of TPL<decltype (arg)>).
   16252              : 
   16253              :      We approximate finding the single cluster entry dep by checking for
   16254              :      entities recursively depending on a dep first seen when streaming
   16255              :      its own merge key; the first dep we see in such a cluster should be
   16256              :      the first one streamed.  */
   16257              :   unsigned entry_pos = ~0u;
   16258              :   unsigned cluster = ~0u;
   16259      2215006 :   for (unsigned ix = 0; ix != order.length (); ix++)
   16260              :     {
   16261       862589 :       gcc_checking_assert (order[ix]->is_special ());
   16262       862589 :       bool tight = order[ix]->cluster == cluster;
   16263       862589 :       depset *dep = order[ix]->deps[0];
   16264       863573 :       dump (dumper::MERGE)
   16265         1965 :         && dump ("Mergeable %u is %N%s%s", ix, dep->get_entity (),
   16266          984 :                  tight ? " (tight)" : "", dep->is_entry () ? " (entry)" : "");
   16267       862589 :       scc[ix] = dep;
   16268       862589 :       if (tight)
   16269              :         {
   16270          100 :           gcc_checking_assert (dep->is_maybe_recursive ());
   16271          100 :           if (dep->is_entry ())
   16272              :             {
   16273              :               /* There should only be one entry dep in a cluster.  */
   16274            6 :               gcc_checking_assert (!scc[entry_pos]->is_entry ());
   16275            6 :               gcc_checking_assert (scc[entry_pos]->is_maybe_recursive ());
   16276            6 :               scc[ix] = scc[entry_pos];
   16277            6 :               scc[entry_pos] = dep;
   16278              :             }
   16279              :         }
   16280              :       else
   16281              :         entry_pos = ix;
   16282       862589 :       cluster = order[ix]->cluster;
   16283              :     }
   16284              : 
   16285       245199 :   dump (dumper::MERGE) && dump ("Ordered %u keys", order.length ());
   16286       244914 :   dump.outdent ();
   16287       244914 : }
   16288              : 
   16289              : /* Reduce graph to SCCS clusters.  SCCS will be populated with the
   16290              :    depsets in dependency order.  Each depset's CLUSTER field contains
   16291              :    its cluster number.  Each SCC has a unique cluster number, and are
   16292              :    contiguous in SCCS. Cluster numbers are otherwise arbitrary.  */
   16293              : 
   16294              : vec<depset *>
   16295       247599 : depset::hash::connect ()
   16296              : {
   16297       247599 :   tarjan connector (size ());
   16298       247599 :   vec<depset *> deps;
   16299       247599 :   deps.create (size ());
   16300      4853943 :   for (depset *item : *this)
   16301              :     {
   16302      2303172 :       entity_kind kind = item->get_entity_kind ();
   16303      2168899 :       if (kind == EK_BINDING
   16304      2168899 :           || !(kind == EK_REDIRECT
   16305      2149149 :                || item->is_tu_local ()
   16306      2149018 :                || item->is_unreached ()
   16307      1757668 :                || item->is_import ()))
   16308      1885308 :         deps.quick_push (item);
   16309              :     }
   16310              : 
   16311              :   /* Iteration over the hash table is an unspecified ordering.  While
   16312              :      that has advantages, it causes 2 problems.  Firstly repeatable
   16313              :      builds are tricky.  Secondly creating testcases that check
   16314              :      dependencies are correct by making sure a bad ordering would
   16315              :      happen if that was wrong.  */
   16316      1340546 :   deps.qsort (depset_cmp);
   16317              : 
   16318      2132907 :   while (deps.length ())
   16319              :     {
   16320      1885308 :       depset *v = deps.pop ();
   16321      1885308 :       dump (dumper::CLUSTER) &&
   16322         1800 :         (v->is_binding ()
   16323          210 :          ? dump ("Connecting binding %P", v->get_entity (), v->get_name ())
   16324         1590 :          : dump ("Connecting %s %s %C:%N",
   16325         1590 :                  is_key_order () ? "key-order"
   16326          870 :                  : !v->has_defn () ? "declaration" : "definition",
   16327         1590 :                  v->entity_kind_name (), TREE_CODE (v->get_entity ()),
   16328              :                  v->get_entity ()));
   16329      1885308 :       if (!v->cluster)
   16330       845348 :         connector.connect (v);
   16331              :     }
   16332              : 
   16333       247599 :   deps.release ();
   16334       495198 :   return connector.result;
   16335       247599 : }
   16336              : 
   16337              : /* Initialize location spans.  */
   16338              : 
   16339              : void
   16340         4814 : loc_spans::init (const line_maps *lmaps, const line_map_ordinary *map)
   16341              : {
   16342         4814 :   gcc_checking_assert (!init_p ());
   16343         4814 :   spans = new vec<span> ();
   16344         4814 :   spans->reserve (20);
   16345              : 
   16346         4814 :   span interval;
   16347         4814 :   interval.ordinary.first = 0;
   16348         4814 :   interval.macro.second = MAX_LOCATION_T + 1;
   16349         4814 :   interval.ordinary_delta = interval.macro_delta = 0;
   16350              : 
   16351              :   /* A span for reserved fixed locs.  */
   16352         4814 :   interval.ordinary.second
   16353         4814 :     = MAP_START_LOCATION (LINEMAPS_ORDINARY_MAP_AT (line_table, 0));
   16354         4814 :   interval.macro.first = interval.macro.second;
   16355         4814 :   dump (dumper::LOCATION)
   16356           42 :     && dump ("Fixed span %u ordinary:[%K,%K) macro:[%K,%K)", spans->length (),
   16357              :              interval.ordinary.first, interval.ordinary.second,
   16358              :              interval.macro.first, interval.macro.second);
   16359         4814 :   spans->quick_push (interval);
   16360              : 
   16361              :   /* A span for command line & forced headers.  */
   16362         4814 :   interval.ordinary.first = interval.ordinary.second;
   16363         4814 :   interval.macro.second = interval.macro.first;
   16364         4814 :   if (map)
   16365              :     {
   16366         4808 :       interval.ordinary.second = map->start_location;
   16367         4808 :       interval.macro.first = LINEMAPS_MACRO_LOWEST_LOCATION (lmaps);
   16368              :     }
   16369         4814 :   dump (dumper::LOCATION)
   16370           21 :     && dump ("Pre span %u ordinary:[%K,%K) macro:[%K,%K)", spans->length (),
   16371              :              interval.ordinary.first, interval.ordinary.second,
   16372              :              interval.macro.first, interval.macro.second);
   16373         4814 :   spans->quick_push (interval);
   16374              : 
   16375              :   /* Start an interval for the main file.  */
   16376         4814 :   interval.ordinary.first = interval.ordinary.second;
   16377         4814 :   interval.macro.second = interval.macro.first;
   16378         4814 :   dump (dumper::LOCATION)
   16379           21 :     && dump ("Main span %u ordinary:[%K,*) macro:[*,%K)", spans->length (),
   16380              :              interval.ordinary.first, interval.macro.second);
   16381         4814 :   spans->quick_push (interval);
   16382         4814 : }
   16383              : 
   16384              : /* Reopen the span, if we want the about-to-be-inserted set of maps to
   16385              :    be propagated in our own location table.  I.e. we are the primary
   16386              :    interface and we're importing a partition.  */
   16387              : 
   16388              : bool
   16389         3003 : loc_spans::maybe_propagate (module_state *import, location_t hwm)
   16390              : {
   16391         3003 :   bool opened = (module_interface_p () && !module_partition_p ()
   16392         3445 :                  && import->is_partition ());
   16393          163 :   if (opened)
   16394          163 :     open (hwm);
   16395         3003 :   return opened;
   16396              : }
   16397              : 
   16398              : /* Open a new linemap interval.  The just-created ordinary map is the
   16399              :    first map of the interval.  */
   16400              : 
   16401              : void
   16402         1048 : loc_spans::open (location_t hwm)
   16403              : {
   16404         1048 :   span interval;
   16405         1048 :   interval.ordinary.first = interval.ordinary.second = hwm;
   16406         2096 :   interval.macro.first = interval.macro.second
   16407         1048 :     = LINEMAPS_MACRO_LOWEST_LOCATION (line_table);
   16408         1048 :   interval.ordinary_delta = interval.macro_delta = 0;
   16409         1048 :   dump (dumper::LOCATION)
   16410            0 :     && dump ("Opening span %u ordinary:[%K,... macro:...,%K)",
   16411            0 :              spans->length (), interval.ordinary.first,
   16412              :              interval.macro.second);
   16413         1048 :   if (spans->length ())
   16414              :     {
   16415              :       /* No overlapping!  */
   16416         1048 :       auto &last = spans->last ();
   16417         1048 :       gcc_checking_assert (interval.ordinary.first >= last.ordinary.second);
   16418         1048 :       gcc_checking_assert (interval.macro.second <= last.macro.first);
   16419              :     }
   16420         1048 :   spans->safe_push (interval);
   16421         1048 : }
   16422              : 
   16423              : /* Close out the current linemap interval.  The last maps are within
   16424              :    the interval.  */
   16425              : 
   16426              : void
   16427         5859 : loc_spans::close ()
   16428              : {
   16429         5859 :   span &interval = spans->last ();
   16430              : 
   16431         5859 :   interval.ordinary.second
   16432         5859 :     = ((line_table->highest_location
   16433         5859 :         + (loc_one << line_table->default_range_bits))
   16434         5859 :        & ~((loc_one << line_table->default_range_bits) - 1));
   16435         5859 :   interval.macro.first = LINEMAPS_MACRO_LOWEST_LOCATION (line_table);
   16436         5859 :   dump (dumper::LOCATION)
   16437           21 :     && dump ("Closing span %u ordinary:[%K,%K) macro:[%K,%K)",
   16438           21 :              spans->length () - 1,
   16439              :              interval.ordinary.first,interval.ordinary.second,
   16440              :              interval.macro.first, interval.macro.second);
   16441         5859 : }
   16442              : 
   16443              : /* Given an ordinary location LOC, return the lmap_interval it resides
   16444              :    in.  NULL if it is not in an interval.  */
   16445              : 
   16446              : const loc_spans::span *
   16447     27045759 : loc_spans::ordinary (location_t loc)
   16448              : {
   16449     27045759 :   unsigned len = spans->length ();
   16450     53964623 :   unsigned pos = 0;
   16451     53976825 :   while (len)
   16452              :     {
   16453     53959604 :       unsigned half = len / 2;
   16454     53959604 :       const span &probe = (*spans)[pos + half];
   16455     53959604 :       if (loc < probe.ordinary.first)
   16456              :         len = half;
   16457     53947402 :       else if (loc < probe.ordinary.second)
   16458              :         return &probe;
   16459              :       else
   16460              :         {
   16461     26918864 :           pos += half + 1;
   16462     26918864 :           len = len - (half + 1);
   16463              :         }
   16464              :     }
   16465              :   return NULL;
   16466              : }
   16467              : 
   16468              : /* Likewise, given a macro location LOC, return the lmap interval it
   16469              :    resides in.   */
   16470              : 
   16471              : const loc_spans::span *
   16472      2345667 : loc_spans::macro (location_t loc)
   16473              : {
   16474      2345667 :   unsigned len = spans->length ();
   16475      4688236 :   unsigned pos = 0;
   16476      4688344 :   while (len)
   16477              :     {
   16478      4688224 :       unsigned half = len / 2;
   16479      4688224 :       const span &probe = (*spans)[pos + half];
   16480      4688224 :       if (loc >= probe.macro.second)
   16481              :         len = half;
   16482      4688116 :       else if (loc >= probe.macro.first)
   16483              :         return &probe;
   16484              :       else
   16485              :         {
   16486      2342569 :           pos += half + 1;
   16487      2342569 :           len = len - (half + 1);
   16488              :         }
   16489              :     }
   16490              :   return NULL;
   16491              : }
   16492              : 
   16493              : /* Return the ordinary location closest to FROM.  */
   16494              : 
   16495              : static location_t
   16496         6735 : ordinary_loc_of (line_maps *lmaps, location_t from)
   16497              : {
   16498        13473 :   while (!IS_ORDINARY_LOC (from))
   16499              :     {
   16500            3 :       if (IS_ADHOC_LOC (from))
   16501            3 :         from = get_location_from_adhoc_loc (lmaps, from);
   16502            3 :       if (from >= LINEMAPS_MACRO_LOWEST_LOCATION (lmaps))
   16503              :         {
   16504              :           /* Find the ordinary location nearest FROM.  */
   16505            0 :           const line_map *map = linemap_lookup (lmaps, from);
   16506            0 :           const line_map_macro *mac_map = linemap_check_macro (map);
   16507            0 :           from = mac_map->get_expansion_point_location ();
   16508              :         }
   16509              :     }
   16510         6735 :   return from;
   16511              : }
   16512              : 
   16513              : static module_state **
   16514        12064 : get_module_slot (tree name, module_state *parent, bool partition, bool insert)
   16515              : {
   16516        12064 :   module_state_hash::compare_type ct (name, uintptr_t (parent) | partition);
   16517        12064 :   hashval_t hv = module_state_hash::hash (ct);
   16518              : 
   16519        12064 :   return modules_hash->find_slot_with_hash (ct, hv, insert ? INSERT : NO_INSERT);
   16520              : }
   16521              : 
   16522              : static module_state *
   16523       112817 : get_primary (module_state *parent)
   16524              : {
   16525       116729 :   while (parent->is_partition ())
   16526          631 :     parent = parent->parent;
   16527              : 
   16528       116098 :   if (!parent->name)
   16529              :     // Implementation unit has null name
   16530        57956 :     parent = parent->parent;
   16531              : 
   16532       111674 :   return parent;
   16533              : }
   16534              : 
   16535              : /* Find or create module NAME & PARENT in the hash table.  */
   16536              : 
   16537              : module_state *
   16538        12064 : get_module (tree name, module_state *parent, bool partition)
   16539              : {
   16540              :   /* We might be given an empty NAME if preprocessing fails to handle
   16541              :      a header-name token.  */
   16542        12064 :   if (name && TREE_CODE (name) == STRING_CST
   16543        14890 :       && TREE_STRING_LENGTH (name) == 0)
   16544              :     return nullptr;
   16545              : 
   16546        12064 :   if (partition)
   16547              :     {
   16548         1021 :       if (!parent)
   16549          214 :         parent = get_primary (this_module ());
   16550              : 
   16551         1021 :       if (!parent->is_partition () && !parent->flatname)
   16552          241 :         parent->set_flatname ();
   16553              :     }
   16554              : 
   16555        12064 :   module_state **slot = get_module_slot (name, parent, partition, true);
   16556        12064 :   module_state *state = *slot;
   16557        12064 :   if (!state)
   16558              :     {
   16559         6541 :       state = (new (ggc_alloc<module_state> ())
   16560         6541 :                module_state (name, parent, partition));
   16561         6541 :       *slot = state;
   16562              :     }
   16563              :   return state;
   16564              : }
   16565              : 
   16566              : /* Process string name PTR into a module_state.  */
   16567              : 
   16568              : static module_state *
   16569          447 : get_module (const char *ptr)
   16570              : {
   16571              :   /* On DOS based file systems, there is an ambiguity with A:B which can be
   16572              :      interpreted as a module Module:Partition or Drive:PATH.  Interpret strings
   16573              :      which clearly starts as pathnames as header-names and everything else is
   16574              :      treated as a (possibly malformed) named moduled.  */
   16575          447 :   if (IS_DIR_SEPARATOR (ptr[ptr[0] == '.']) // ./FOO or /FOO
   16576              : #if HAVE_DOS_BASED_FILE_SYSTEM
   16577              :       || (HAS_DRIVE_SPEC (ptr) && IS_DIR_SEPARATOR (ptr[2])) // A:/FOO
   16578              : #endif
   16579              :       || false)
   16580              :     /* A header name.  */
   16581          111 :     return get_module (build_string (strlen (ptr), ptr));
   16582              : 
   16583              :   bool partition = false;
   16584              :   module_state *mod = NULL;
   16585              : 
   16586         1273 :   for (const char *probe = ptr;; probe++)
   16587         1609 :     if (!*probe || *probe == '.' || *probe == ':')
   16588              :       {
   16589          420 :         if (probe == ptr)
   16590              :           return NULL;
   16591              : 
   16592          420 :         mod = get_module (get_identifier_with_length (ptr, probe - ptr),
   16593              :                           mod, partition);
   16594          420 :         ptr = probe;
   16595          420 :         if (*ptr == ':')
   16596              :           {
   16597           81 :             if (partition)
   16598              :               return NULL;
   16599              :             partition = true;
   16600              :           }
   16601              : 
   16602          420 :         if (!*ptr++)
   16603              :           break;
   16604              :       }
   16605         1189 :     else if (!(ISALPHA (*probe) || *probe == '_'
   16606           18 :                || (probe != ptr && ISDIGIT (*probe))))
   16607              :       return NULL;
   16608              : 
   16609              :   return mod;
   16610              : }
   16611              : 
   16612              : /* Create a new mapper connecting to OPTION.  */
   16613              : 
   16614              : module_client *
   16615         4814 : make_mapper (location_t loc, class mkdeps *deps)
   16616              : {
   16617         4814 :   timevar_start (TV_MODULE_MAPPER);
   16618         4814 :   const char *option = module_mapper_name;
   16619         4814 :   if (!option)
   16620         4772 :     option = getenv ("CXX_MODULE_MAPPER");
   16621              : 
   16622         9628 :   mapper = module_client::open_module_client
   16623         4814 :     (loc, option, deps, &set_cmi_repo,
   16624         4814 :      (save_decoded_options[0].opt_index == OPT_SPECIAL_program_name)
   16625         4814 :      && save_decoded_options[0].arg != progname
   16626              :      ? save_decoded_options[0].arg : nullptr);
   16627              : 
   16628         4814 :   timevar_stop (TV_MODULE_MAPPER);
   16629              : 
   16630         4814 :   return mapper;
   16631              : }
   16632              : 
   16633              : static unsigned lazy_snum;
   16634              : 
   16635              : static bool
   16636        10946 : recursive_lazy (unsigned snum = ~0u)
   16637              : {
   16638        10946 :   if (lazy_snum)
   16639              :     {
   16640            0 :       error_at (input_location, "recursive lazy load");
   16641            0 :       return true;
   16642              :     }
   16643              : 
   16644        10946 :   lazy_snum = snum;
   16645        10946 :   return false;
   16646              : }
   16647              : 
   16648              : /* If THIS has an interface dependency on itself, report an error and
   16649              :    return false.  */
   16650              : 
   16651              : bool
   16652         2856 : module_state::check_circular_import (location_t from)
   16653              : {
   16654         2856 :   if (this == this_module ())
   16655              :     {
   16656              :       /* Cannot import the current module.  */
   16657            9 :       auto_diagnostic_group d;
   16658            9 :       error_at (from, "module %qs depends on itself", get_flatname ());
   16659            9 :       if (!header_module_p ())
   16660            6 :         inform (loc, "module %qs declared here", get_flatname ());
   16661            9 :       return false;
   16662            9 :     }
   16663              :   return true;
   16664              : }
   16665              : 
   16666              : /* Module name substitutions.  */
   16667              : static vec<module_state *,va_heap> substs;
   16668              : 
   16669              : void
   16670         8827 : module_state::mangle (bool include_partition)
   16671              : {
   16672         8827 :   if (subst)
   16673          386 :     mangle_module_substitution (subst);
   16674              :   else
   16675              :     {
   16676         8441 :       if (parent)
   16677          838 :         parent->mangle (include_partition);
   16678         8441 :       if (include_partition || !is_partition ())
   16679              :         {
   16680              :           // Partitions are significant for global initializer
   16681              :           // functions
   16682         8253 :           bool partition = is_partition () && !parent->is_partition ();
   16683         8253 :           subst = mangle_module_component (name, partition);
   16684         8253 :           substs.safe_push (this);
   16685              :         }
   16686              :     }
   16687         8827 : }
   16688              : 
   16689              : void
   16690         7989 : mangle_module (int mod, bool include_partition)
   16691              : {
   16692         7989 :   module_state *imp = (*modules)[mod];
   16693              : 
   16694         7989 :   gcc_checking_assert (!imp->is_header ());
   16695              : 
   16696         7989 :   if (!imp->name)
   16697              :     /* Set when importing the primary module interface.  */
   16698          223 :     imp = imp->parent;
   16699              : 
   16700              :   /* Ensure this is actually a module unit.  */
   16701          223 :   gcc_checking_assert (imp);
   16702              : 
   16703         7989 :   imp->mangle (include_partition);
   16704         7989 : }
   16705              : 
   16706              : /* Clean up substitutions.  */
   16707              : void
   16708         7557 : mangle_module_fini ()
   16709              : {
   16710        15810 :   while (substs.length ())
   16711         8253 :     substs.pop ()->subst = 0;
   16712         7557 : }
   16713              : 
   16714              : /* Announce WHAT about the module.  */
   16715              : 
   16716              : void
   16717        12399 : module_state::announce (const char *what) const
   16718              : {
   16719        12399 :   if (noisy_p ())
   16720              :     {
   16721            0 :       fprintf (stderr, " %s:%s", what, get_flatname ());
   16722            0 :       fflush (stderr);
   16723              :     }
   16724        12399 : }
   16725              : 
   16726              : /* A human-readable README section.  The contents of this section to
   16727              :    not contribute to the CRC, so the contents can change per
   16728              :    compilation.  That allows us to embed CWD, hostname, build time and
   16729              :    what not.  It is a STRTAB that may be extracted with:
   16730              :      readelf -pgnu.c++.README $(module).gcm */
   16731              : 
   16732              : void
   16733         2685 : module_state::write_readme (elf_out *to, cpp_reader *reader, const char *dialect)
   16734              : {
   16735         2685 :   bytes_out readme (to);
   16736              : 
   16737         2685 :   readme.begin (false);
   16738              : 
   16739         2685 :   readme.printf ("GNU C++ %s",
   16740         2685 :                  is_header () ? "header unit"
   16741         1802 :                  : !is_partition () ? "primary interface"
   16742          187 :                  : is_interface () ? "interface partition"
   16743              :                  : "internal partition");
   16744              : 
   16745              :   /* Compiler's version.  */
   16746         2685 :   readme.printf ("compiler: %s", version_string);
   16747              : 
   16748              :   /* Module format version.  */
   16749         2685 :   verstr_t string;
   16750         2685 :   version2string (MODULE_VERSION, string);
   16751         2685 :   readme.printf ("version: %s", string);
   16752              : 
   16753              :   /* Module information.  */
   16754         2685 :   readme.printf ("module: %s", get_flatname ());
   16755         2685 :   readme.printf ("source: %s", main_input_filename);
   16756         2685 :   readme.printf ("dialect: %s", dialect);
   16757         2685 :   if (extensions)
   16758           30 :     readme.printf ("extensions: %s%s%s",
   16759              :                    extensions & SE_OPENMP ? "-fopenmp"
   16760            6 :                    : extensions & SE_OPENMP_SIMD ? "-fopenmp-simd" : "",
   16761              :                    (extensions & SE_OPENACC)
   16762            3 :                    && (extensions & (SE_OPENMP | SE_OPENMP_SIMD))
   16763              :                    ? " " : "",
   16764           12 :                    extensions & SE_OPENACC ? "-fopenacc" : "");
   16765              : 
   16766              :   /* The following fields could be expected to change between
   16767              :      otherwise identical compilations.  Consider a distributed build
   16768              :      system.  We should have a way of overriding that.  */
   16769         2685 :   if (char *cwd = getcwd (NULL, 0))
   16770              :     {
   16771         2685 :       readme.printf ("cwd: %s", cwd);
   16772         2685 :       free (cwd);
   16773              :     }
   16774         5370 :   readme.printf ("repository: %s", cmi_repo ? cmi_repo : ".");
   16775              : #if NETWORKING
   16776              :   {
   16777              :     char hostname[64];
   16778              :     if (!gethostname (hostname, sizeof (hostname)))
   16779              :       readme.printf ("host: %s", hostname);
   16780              :   }
   16781              : #endif
   16782         2685 :   {
   16783              :     /* This of course will change!  */
   16784         2685 :     time_t stampy;
   16785         2685 :     auto kind = cpp_get_date (reader, &stampy);
   16786         2685 :     if (kind != CPP_time_kind::UNKNOWN)
   16787              :       {
   16788         2685 :         struct tm *time;
   16789              : 
   16790         2685 :         time = gmtime (&stampy);
   16791         2685 :         readme.print_time ("build", time, "UTC");
   16792              : 
   16793         2685 :         if (kind == CPP_time_kind::DYNAMIC)
   16794              :           {
   16795         2685 :             time = localtime (&stampy);
   16796         2685 :             readme.print_time ("local", time,
   16797              : #if defined (__USE_MISC) || defined (__USE_BSD) /* Is there a better way?  */
   16798              :                                time->tm_zone
   16799              : #else
   16800              :                                ""
   16801              : #endif
   16802              :                                );
   16803              :           }
   16804              :       }
   16805              :   }
   16806              : 
   16807              :   /* Its direct imports.  */
   16808         3338 :   for (unsigned ix = 1; ix < modules->length (); ix++)
   16809              :     {
   16810          653 :       module_state *state = (*modules)[ix];
   16811              : 
   16812          653 :       if (state->is_direct ())
   16813          962 :         readme.printf ("%s: %s %s", state->exported_p ? "export" : "import",
   16814              :                        state->get_flatname (), state->filename);
   16815              :     }
   16816              : 
   16817         2685 :   readme.end (to, to->name (MOD_SNAME_PFX ".README"), NULL);
   16818         2685 : }
   16819              : 
   16820              : /* Sort environment var names in reverse order.  */
   16821              : 
   16822              : static int
   16823            0 : env_var_cmp (const void *a_, const void *b_)
   16824              : {
   16825            0 :   const unsigned char *a = *(const unsigned char *const *)a_;
   16826            0 :   const unsigned char *b = *(const unsigned char *const *)b_;
   16827              : 
   16828            0 :   for (unsigned ix = 0; ; ix++)
   16829              :     {
   16830            0 :       bool a_end = !a[ix] || a[ix] == '=';
   16831            0 :       if (a[ix] == b[ix])
   16832              :         {
   16833            0 :           if (a_end)
   16834              :             break;
   16835              :         }
   16836              :       else
   16837              :         {
   16838            0 :           bool b_end = !b[ix] || b[ix] == '=';
   16839              : 
   16840            0 :           if (!a_end && !b_end)
   16841            0 :             return a[ix] < b[ix] ? +1 : -1;
   16842            0 :           if (a_end && b_end)
   16843              :             break;
   16844            0 :           return a_end ? +1 : -1;
   16845              :         }
   16846            0 :     }
   16847              : 
   16848              :   return 0;
   16849              : }
   16850              : 
   16851              : /* Write the environment. It is a STRTAB that may be extracted with:
   16852              :      readelf -pgnu.c++.ENV $(module).gcm */
   16853              : 
   16854              : void
   16855            0 : module_state::write_env (elf_out *to)
   16856              : {
   16857            0 :   vec<const char *> vars;
   16858            0 :   vars.create (20);
   16859              : 
   16860            0 :   extern char **environ;
   16861            0 :   while (const char *var = environ[vars.length ()])
   16862            0 :     vars.safe_push (var);
   16863            0 :   vars.qsort (env_var_cmp);
   16864              : 
   16865            0 :   bytes_out env (to);
   16866            0 :   env.begin (false);
   16867            0 :   while (vars.length ())
   16868            0 :     env.printf ("%s", vars.pop ());
   16869            0 :   env.end (to, to->name (MOD_SNAME_PFX ".ENV"), NULL);
   16870              : 
   16871            0 :   vars.release ();
   16872            0 : }
   16873              : 
   16874              : /* Write the direct or indirect imports.
   16875              :    u:N
   16876              :    {
   16877              :      u:index
   16878              :      s:name
   16879              :      u32:crc
   16880              :      s:filename (direct)
   16881              :      u:exported (direct)
   16882              :    } imports[N]
   16883              :  */
   16884              : 
   16885              : void
   16886          894 : module_state::write_imports (bytes_out &sec, bool direct)
   16887              : {
   16888          894 :   unsigned count = 0;
   16889              : 
   16890         1892 :   for (unsigned ix = 1; ix < modules->length (); ix++)
   16891              :     {
   16892          998 :       module_state *imp = (*modules)[ix];
   16893              : 
   16894          998 :       if (imp->remap && imp->is_direct () == direct)
   16895          478 :         count++;
   16896              :     }
   16897              : 
   16898          894 :   gcc_assert (!direct || count);
   16899              : 
   16900          894 :   sec.u (count);
   16901         1892 :   for (unsigned ix = 1; ix < modules->length (); ix++)
   16902              :     {
   16903          998 :       module_state *imp = (*modules)[ix];
   16904              : 
   16905          998 :       if (imp->remap && imp->is_direct () == direct)
   16906              :         {
   16907          640 :           dump () && dump ("Writing %simport:%u->%u %M (crc=%x)",
   16908              :                            !direct ? "indirect "
   16909           81 :                            : imp->exported_p ? "exported " : "",
   16910              :                            ix, imp->remap, imp, imp->crc);
   16911          478 :           sec.u (imp->remap);
   16912          478 :           sec.str (imp->get_flatname ());
   16913          478 :           sec.u32 (imp->crc);
   16914          478 :           if (direct)
   16915              :             {
   16916          469 :               write_location (sec, imp->imported_from ());
   16917          469 :               sec.str (imp->filename);
   16918          469 :               int exportedness = 0;
   16919          469 :               if (imp->exported_p)
   16920              :                 exportedness = +1;
   16921          277 :               else if (!imp->is_purview_direct ())
   16922           13 :                 exportedness = -1;
   16923          469 :               sec.i (exportedness);
   16924              :             }
   16925              :         }
   16926              :     }
   16927          894 : }
   16928              : 
   16929              : /* READER, LMAPS  != NULL == direct imports,
   16930              :    == NUL == indirect imports.  */
   16931              : 
   16932              : unsigned
   16933          766 : module_state::read_imports (bytes_in &sec, cpp_reader *reader, line_maps *lmaps)
   16934              : {
   16935          766 :   unsigned count = sec.u ();
   16936          766 :   unsigned loaded = 0;
   16937              : 
   16938         1937 :   while (count--)
   16939              :     {
   16940          405 :       unsigned ix = sec.u ();
   16941          405 :       if (ix >= slurp->remap->length () || !ix || (*slurp->remap)[ix])
   16942              :         {
   16943            0 :           sec.set_overrun ();
   16944            0 :           break;
   16945              :         }
   16946              : 
   16947          405 :       const char *name = sec.str (NULL);
   16948          405 :       module_state *imp = get_module (name);
   16949          405 :       unsigned crc = sec.u32 ();
   16950          405 :       int exportedness = 0;
   16951              : 
   16952              :       /* If the import is a partition, it must be the same primary
   16953              :          module as this TU.  */
   16954          405 :       if (imp && imp->is_partition () &&
   16955              :           (!named_module_p ()
   16956          135 :            || (get_primary (this_module ()) != get_primary (imp))))
   16957              :         imp = NULL;
   16958              : 
   16959          405 :       if (!imp)
   16960            0 :         sec.set_overrun ();
   16961          405 :       if (sec.get_overrun ())
   16962              :         break;
   16963              : 
   16964          405 :       if (lmaps)
   16965              :         {
   16966              :           /* A direct import, maybe load it.  */
   16967          401 :           location_t floc = read_location (sec);
   16968          401 :           const char *fname = sec.str (NULL);
   16969          401 :           exportedness = sec.i ();
   16970              : 
   16971          401 :           if (sec.get_overrun ())
   16972              :             break;
   16973              : 
   16974          401 :           if (!imp->check_circular_import (floc))
   16975            3 :             continue;
   16976              : 
   16977          398 :           if (imp->loadedness == ML_NONE)
   16978              :             {
   16979          314 :               imp->loc = floc;
   16980          314 :               imp->crc = crc;
   16981          314 :               if (!imp->get_flatname ())
   16982          271 :                 imp->set_flatname ();
   16983              : 
   16984          314 :               unsigned n = dump.push (imp);
   16985              : 
   16986          314 :               if (!imp->filename && fname)
   16987          271 :                 imp->filename = xstrdup (fname);
   16988              : 
   16989          314 :               if (imp->is_partition ())
   16990           33 :                 dump () && dump ("Importing elided partition %M", imp);
   16991              : 
   16992          314 :               if (!imp->do_import (reader, false))
   16993            3 :                 imp = NULL;
   16994          314 :               dump.pop (n);
   16995          314 :               if (!imp)
   16996            3 :                 continue;
   16997              :             }
   16998              : 
   16999          395 :           if (is_partition ())
   17000              :             {
   17001           66 :               if (!imp->is_direct () && !imp->is_partition_direct ())
   17002              :                 {
   17003           30 :                   imp->directness = MD_PARTITION_DIRECT;
   17004           30 :                   linemap_module_reparent (line_table, imp->loc, floc);
   17005              :                 }
   17006           66 :               if (exportedness > 0)
   17007            6 :                 imp->exported_p = true;
   17008              :             }
   17009              :         }
   17010              :       else
   17011              :         {
   17012              :           /* An indirect import, find it, it should already be here.  */
   17013            4 :           if (imp->loadedness == ML_NONE)
   17014              :             {
   17015            0 :               error_at (loc, "indirect import %qs is not already loaded", name);
   17016            0 :               continue;
   17017              :             }
   17018              :         }
   17019              : 
   17020          399 :       if (imp->crc != crc)
   17021            0 :         error_at (loc, "import %qs has CRC mismatch", imp->get_flatname ());
   17022              : 
   17023          399 :       (*slurp->remap)[ix] = (imp->mod << 1) | (lmaps != NULL);
   17024              : 
   17025          399 :       if (lmaps && exportedness >= 0)
   17026          381 :         set_import (imp, bool (exportedness));
   17027          579 :       dump () && dump ("Found %simport:%u %M->%u", !lmaps ? "indirect "
   17028           90 :                        : exportedness > 0 ? "exported "
   17029           51 :                        : exportedness < 0 ? "gmf" : "", ix, imp,
   17030              :                        imp->mod);
   17031          399 :       loaded++;
   17032              :     }
   17033              : 
   17034          766 :   return loaded;
   17035              : }
   17036              : 
   17037              : /* Write the import table to MOD_SNAME_PFX.imp.  */
   17038              : 
   17039              : void
   17040          447 : module_state::write_imports (elf_out *to, unsigned *crc_ptr)
   17041              : {
   17042          525 :   dump () && dump ("Writing imports");
   17043          447 :   dump.indent ();
   17044              : 
   17045          447 :   bytes_out sec (to);
   17046          447 :   sec.begin ();
   17047              : 
   17048          447 :   write_imports (sec, true);
   17049          447 :   write_imports (sec, false);
   17050              : 
   17051          447 :   sec.end (to, to->name (MOD_SNAME_PFX ".imp"), crc_ptr);
   17052          447 :   dump.outdent ();
   17053          447 : }
   17054              : 
   17055              : bool
   17056          383 : module_state::read_imports (cpp_reader *reader, line_maps *lmaps)
   17057              : {
   17058          383 :   bytes_in sec;
   17059              : 
   17060          383 :   if (!sec.begin (loc, from (), MOD_SNAME_PFX ".imp"))
   17061              :     return false;
   17062              : 
   17063          470 :   dump () && dump ("Reading %u imports", slurp->remap->length () - 1);
   17064          383 :   dump.indent ();
   17065              : 
   17066              :   /* Read the imports.  */
   17067          383 :   unsigned direct = read_imports (sec, reader, lmaps);
   17068          383 :   unsigned indirect = read_imports (sec, NULL, NULL);
   17069          383 :   if (direct + indirect + 1 != slurp->remap->length ())
   17070            6 :     from ()->set_error (elf::E_BAD_IMPORT);
   17071              : 
   17072          383 :   dump.outdent ();
   17073          383 :   if (!sec.end (from ()))
   17074              :     return false;
   17075              :   return true;
   17076          383 : }
   17077              : 
   17078              : /* We're the primary module interface, but have partitions.  Document
   17079              :    them so that non-partition module implementation units know which
   17080              :    have already been loaded.  */
   17081              : 
   17082              : void
   17083          127 : module_state::write_partitions (elf_out *to, unsigned count, unsigned *crc_ptr)
   17084              : {
   17085          148 :   dump () && dump ("Writing %u elided partitions", count);
   17086          127 :   dump.indent ();
   17087              : 
   17088          127 :   bytes_out sec (to);
   17089          127 :   sec.begin ();
   17090              : 
   17091          326 :   for (unsigned ix = 1; ix != modules->length (); ix++)
   17092              :     {
   17093          199 :       module_state *imp = (*modules)[ix];
   17094          199 :       if (imp->is_partition ())
   17095              :         {
   17096          208 :           dump () && dump ("Writing elided partition %M (crc=%x)",
   17097              :                            imp, imp->crc);
   17098          175 :           sec.str (imp->get_flatname ());
   17099          175 :           sec.u32 (imp->crc);
   17100          341 :           write_location (sec, imp->is_direct ()
   17101          166 :                           ? imp->imported_from () : UNKNOWN_LOCATION);
   17102          175 :           sec.str (imp->filename);
   17103              :         }
   17104              :     }
   17105              : 
   17106          127 :   sec.end (to, to->name (MOD_SNAME_PFX ".prt"), crc_ptr);
   17107          127 :   dump.outdent ();
   17108          127 : }
   17109              : 
   17110              : bool
   17111           24 : module_state::read_partitions (unsigned count)
   17112              : {
   17113           24 :   bytes_in sec;
   17114           24 :   if (!sec.begin (loc, from (), MOD_SNAME_PFX ".prt"))
   17115              :     return false;
   17116              : 
   17117           30 :   dump () && dump ("Reading %u elided partitions", count);
   17118           24 :   dump.indent ();
   17119              : 
   17120           60 :   while (count--)
   17121              :     {
   17122           36 :       const char *name = sec.str (NULL);
   17123           36 :       unsigned crc = sec.u32 ();
   17124           36 :       location_t floc = read_location (sec);
   17125           36 :       const char *fname = sec.str (NULL);
   17126              : 
   17127           36 :       if (sec.get_overrun ())
   17128              :         break;
   17129              : 
   17130           45 :       dump () && dump ("Reading elided partition %s (crc=%x)", name, crc);
   17131              : 
   17132           36 :       module_state *imp = get_module (name);
   17133           36 :       if (!imp  /* Partition should be ...  */
   17134           36 :           || !imp->is_partition () /* a partition ...  */
   17135           36 :           || imp->loadedness != ML_NONE  /* that is not yet loaded ...  */
   17136           72 :           || get_primary (imp) != this) /* whose primary is this.  */
   17137              :         {
   17138            0 :           sec.set_overrun ();
   17139            0 :           break;
   17140              :         }
   17141              : 
   17142           36 :       if (!imp->has_location ())
   17143           30 :         imp->loc = floc;
   17144           36 :       imp->crc = crc;
   17145           36 :       if (!imp->filename && fname[0])
   17146           30 :         imp->filename = xstrdup (fname);
   17147              :     }
   17148              : 
   17149           24 :   dump.outdent ();
   17150           24 :   if (!sec.end (from ()))
   17151              :     return false;
   17152              :   return true;
   17153           24 : }
   17154              : 
   17155              : /* Data for config reading and writing.  */
   17156              : struct module_state_config {
   17157              :   const char *dialect_str = get_dialect ();
   17158              :   line_map_uint_t ordinary_locs = 0;
   17159              :   line_map_uint_t macro_locs = 0;
   17160              :   unsigned num_imports = 0;
   17161              :   unsigned num_partitions = 0;
   17162              :   unsigned num_entities = 0;
   17163              :   unsigned loc_range_bits = 0;
   17164              :   unsigned active_init = 0;
   17165              : 
   17166        96437 :   static void release ()
   17167              :   {
   17168        96437 :     XDELETEVEC (dialect);
   17169        96437 :     dialect = NULL;
   17170              :   }
   17171              : 
   17172              : private:
   17173              :   static const char *get_dialect ();
   17174              :   static char *dialect;
   17175              : };
   17176              : 
   17177              : char *module_state_config::dialect;
   17178              : 
   17179              : /* Generate a string of the significant compilation options.
   17180              :    Generally assume the user knows what they're doing, in the same way
   17181              :    that object files can be mixed.  */
   17182              : 
   17183              : const char *
   17184         5820 : module_state_config::get_dialect ()
   17185              : {
   17186         5820 :   if (!dialect)
   17187         9206 :     dialect = concat (get_cxx_dialect_name (cxx_dialect),
   17188              :                       /* C++ implies these, only show if disabled.  */
   17189         4603 :                       flag_exceptions ? "" : "/no-exceptions",
   17190         4603 :                       flag_rtti ? "" : "/no-rtti",
   17191         4603 :                       flag_new_inheriting_ctors ? "" : "/old-inheriting-ctors",
   17192              :                       /* C++ 20 implies concepts and coroutines.  */
   17193         1454 :                       cxx_dialect < cxx20 && flag_concepts ? "/concepts" : "",
   17194         4603 :                       (cxx_dialect < cxx20 && flag_coroutines
   17195              :                        ? "/coroutines" : ""),
   17196         4603 :                       flag_module_implicit_inline ? "/implicit-inline" : "",
   17197         4603 :                       flag_contracts ? "/contracts" : "",
   17198              :                       NULL);
   17199              : 
   17200         5820 :   return dialect;
   17201              : }
   17202              : 
   17203              : /* Contents of a cluster.  */
   17204              : enum cluster_tag {
   17205              :   ct_decl,      /* A decl.  */
   17206              :   ct_defn,      /* A definition.  */
   17207              :   ct_bind,      /* A binding.  */
   17208              :   ct_hwm
   17209              : };
   17210              : 
   17211              : /* Binding modifiers.  */
   17212              : enum ct_bind_flags
   17213              : {
   17214              :   cbf_export = 0x1,     /* An exported decl.  */
   17215              :   cbf_hidden = 0x2,     /* A hidden (friend) decl.  */
   17216              :   cbf_using = 0x4,      /* A using decl.  */
   17217              :   cbf_internal = 0x8,   /* A TU-local decl.  */
   17218              : };
   17219              : 
   17220              : /* DEP belongs to a different cluster, seed it to prevent
   17221              :    unfortunately timed duplicate import.  */
   17222              : // FIXME: QOI For inter-cluster references we could just only pick
   17223              : // one entity from an earlier cluster.  Even better track
   17224              : // dependencies between earlier clusters
   17225              : 
   17226              : void
   17227      5663705 : module_state::intercluster_seed (trees_out &sec, unsigned index_hwm, depset *dep)
   17228              : {
   17229      5663705 :   if (dep->is_tu_local ())
   17230              :     /* We only stream placeholders for TU-local entities anyway.  */;
   17231      5663496 :   else if (dep->is_import () || dep->cluster < index_hwm)
   17232              :     {
   17233      2699101 :       tree ent = dep->get_entity ();
   17234      2699101 :       if (!TREE_VISITED (ent))
   17235              :         {
   17236      1148781 :           sec.tree_node (ent);
   17237      1149231 :           dump (dumper::CLUSTER)
   17238          450 :             && dump ("Seeded %s %N",
   17239          450 :                      dep->is_import () ? "import" : "intercluster", ent);
   17240              :         }
   17241              :     }
   17242      5663705 : }
   17243              : 
   17244              : /* Write the cluster of depsets in SCC[0-SIZE).
   17245              :    dep->section -> section number
   17246              :    dep->cluster -> entity number
   17247              :  */
   17248              : 
   17249              : unsigned
   17250       244914 : module_state::write_cluster (elf_out *to, depset *scc[], unsigned size,
   17251              :                              depset::hash &table, unsigned *counts,
   17252              :                              unsigned *crc_ptr)
   17253              : {
   17254       246445 :   dump () && dump ("Writing section:%u %u depsets", table.section, size);
   17255       244914 :   dump.indent ();
   17256              : 
   17257       244914 :   trees_out sec (to, this, table, table.section);
   17258       244914 :   sec.begin ();
   17259       244914 :   unsigned index_lwm = counts[MSC_entities];
   17260              : 
   17261              :   /* Determine entity numbers, mark for writing.   */
   17262       245223 :   dump (dumper::CLUSTER) && dump ("Cluster members:") && (dump.indent (), true);
   17263      1262982 :   for (unsigned ix = 0; ix != size; ix++)
   17264              :     {
   17265      1018068 :       depset *b = scc[ix];
   17266              : 
   17267      1018068 :       switch (b->get_entity_kind ())
   17268              :         {
   17269            0 :         default:
   17270            0 :           gcc_unreachable ();
   17271              : 
   17272       134273 :         case depset::EK_BINDING:
   17273       134273 :           {
   17274       134273 :             dump (dumper::CLUSTER)
   17275          210 :               && dump ("[%u]=%s %P", ix, b->entity_kind_name (),
   17276              :                        b->get_entity (), b->get_name ());
   17277       134273 :             depset *ns_dep = b->deps[0];
   17278       134273 :             gcc_checking_assert (ns_dep->get_entity_kind ()
   17279              :                                  == depset::EK_NAMESPACE
   17280              :                                  && ns_dep->get_entity () == b->get_entity ());
   17281       301880 :             for (unsigned jx = b->deps.length (); --jx;)
   17282              :               {
   17283       167607 :                 depset *dep = b->deps[jx];
   17284              :                 // We could be declaring something that is also a
   17285              :                 // (merged) import
   17286       188861 :                 gcc_checking_assert (dep->is_import ()
   17287              :                                      || TREE_VISITED (dep->get_entity ())
   17288              :                                      || (dep->get_entity_kind ()
   17289              :                                          == depset::EK_USING)
   17290              :                                      || (dep->get_entity_kind ()
   17291              :                                          == depset::EK_TU_LOCAL));
   17292              :               }
   17293              :           }
   17294              :           break;
   17295              : 
   17296       862589 :         case depset::EK_DECL:
   17297       862589 :         case depset::EK_SPECIALIZATION:
   17298       862589 :         case depset::EK_PARTIAL:
   17299       862589 :           b->cluster = counts[MSC_entities]++;
   17300       862589 :           sec.mark_declaration (b->get_entity (), b->has_defn ());
   17301              :           /* FALLTHROUGH  */
   17302              : 
   17303       883795 :         case depset::EK_USING:
   17304       883795 :         case depset::EK_TU_LOCAL:
   17305      1767590 :           gcc_checking_assert (!b->is_import ()
   17306              :                                && !b->is_unreached ());
   17307      1021233 :           dump (dumper::CLUSTER)
   17308         1161 :             && dump ("[%u]=%s %s %N", ix, b->entity_kind_name (),
   17309          729 :                      b->has_defn () ? "definition" : "declaration",
   17310              :                      b->get_entity ());
   17311              :           break;
   17312              :         }
   17313              :     }
   17314       245223 :   dump (dumper::CLUSTER) && (dump.outdent (), true);
   17315              : 
   17316              :   /* Ensure every out-of-cluster decl is referenced before we start
   17317              :      streaming.  We must do both imports *and* earlier clusters,
   17318              :      because the latter could reach into the former and cause a
   17319              :      duplicate loop.   */
   17320       244914 :   sec.set_importing (+1);
   17321      1262982 :   for (unsigned ix = 0; ix != size; ix++)
   17322              :     {
   17323      1018068 :       depset *b = scc[ix];
   17324     12182502 :       for (unsigned jx = b->is_special (); jx != b->deps.length (); jx++)
   17325              :         {
   17326      5075924 :           depset *dep = b->deps[jx];
   17327              : 
   17328      5075924 :           if (dep->is_binding ())
   17329              :             {
   17330       922899 :               for (unsigned ix = dep->deps.length (); --ix;)
   17331              :                 {
   17332       587781 :                   depset *bind = dep->deps[ix];
   17333       587781 :                   if (bind->get_entity_kind () == depset::EK_USING)
   17334        98082 :                     bind = bind->deps[1];
   17335              : 
   17336       587781 :                   intercluster_seed (sec, index_lwm, bind);
   17337              :                 }
   17338              :               /* Also check the namespace itself.  */
   17339       167559 :               dep = dep->deps[0];
   17340              :             }
   17341              : 
   17342      5075924 :           intercluster_seed (sec, index_lwm, dep);
   17343              :         }
   17344              :     }
   17345       244914 :   sec.tree_node (NULL_TREE);
   17346              :   /* We're done importing now.  */
   17347       244914 :   sec.set_importing (-1);
   17348              : 
   17349              :   /* Write non-definitions.  */
   17350      1262982 :   for (unsigned ix = 0; ix != size; ix++)
   17351              :     {
   17352      1018068 :       depset *b = scc[ix];
   17353      1018068 :       tree decl = b->get_entity ();
   17354      1018068 :       switch (b->get_entity_kind ())
   17355              :         {
   17356            0 :         default:
   17357            0 :           gcc_unreachable ();
   17358       134273 :           break;
   17359              : 
   17360       134273 :         case depset::EK_BINDING:
   17361       134273 :           {
   17362       134273 :             gcc_assert (TREE_CODE (decl) == NAMESPACE_DECL);
   17363       135468 :             dump () && dump ("Depset:%u binding %C:%P", ix, TREE_CODE (decl),
   17364              :                              decl, b->get_name ());
   17365       134273 :             sec.u (ct_bind);
   17366       134273 :             sec.tree_node (decl);
   17367       134273 :             sec.tree_node (b->get_name ());
   17368              : 
   17369              :             /* Write in reverse order, so reading will see the exports
   17370              :                first, thus building the overload chain will be
   17371              :                optimized.  */
   17372       436153 :             for (unsigned jx = b->deps.length (); --jx;)
   17373              :               {
   17374       167607 :                 depset *dep = b->deps[jx];
   17375       167607 :                 tree bound = dep->get_entity ();
   17376       167607 :                 unsigned flags = 0;
   17377       167607 :                 if (dep->get_entity_kind () == depset::EK_TU_LOCAL)
   17378              :                   flags |= cbf_internal;
   17379       167559 :                 else if (dep->get_entity_kind () == depset::EK_USING)
   17380              :                   {
   17381        21206 :                     tree ovl = bound;
   17382        21206 :                     bound = OVL_FUNCTION (bound);
   17383        21206 :                     if (!(TREE_CODE (bound) == CONST_DECL
   17384         4545 :                           && UNSCOPED_ENUM_P (TREE_TYPE (bound))
   17385         3839 :                           && decl == TYPE_NAME (TREE_TYPE (bound))))
   17386              :                       /* An unscoped enumerator in its enumeration's
   17387              :                          scope is not a using.  */
   17388              :                       flags |= cbf_using;
   17389        21206 :                     if (OVL_EXPORT_P (ovl))
   17390        20340 :                       flags |= cbf_export;
   17391              :                   }
   17392              :                 else
   17393              :                   {
   17394              :                     /* An implicit typedef must be at one.  */
   17395       146353 :                     gcc_assert (!DECL_IMPLICIT_TYPEDEF_P (bound) || jx == 1);
   17396       146353 :                     if (dep->is_hidden ())
   17397              :                       flags |= cbf_hidden;
   17398       146265 :                     else if (DECL_MODULE_EXPORT_P (STRIP_TEMPLATE (bound)))
   17399       132549 :                       flags |= cbf_export;
   17400              :                   }
   17401              : 
   17402       167607 :                 gcc_checking_assert (DECL_P (bound));
   17403              : 
   17404       167607 :                 sec.i (flags);
   17405       167607 :                 if (flags & cbf_internal)
   17406              :                   {
   17407           48 :                     sec.tree_node (name_for_tu_local_decl (bound));
   17408           48 :                     write_location (sec, DECL_SOURCE_LOCATION (bound));
   17409              :                   }
   17410              :                 else
   17411       167559 :                   sec.tree_node (bound);
   17412              :               }
   17413              : 
   17414              :             /* Terminate the list.  */
   17415       134273 :             sec.i (-1);
   17416              :           }
   17417       134273 :           break;
   17418              : 
   17419        21206 :         case depset::EK_USING:
   17420        21206 :         case depset::EK_TU_LOCAL:
   17421        21233 :           dump () && dump ("Depset:%u %s %C:%N", ix, b->entity_kind_name (),
   17422           27 :                            TREE_CODE (decl), decl);
   17423              :           break;
   17424              : 
   17425       862589 :         case depset::EK_SPECIALIZATION:
   17426       862589 :         case depset::EK_PARTIAL:
   17427       862589 :         case depset::EK_DECL:
   17428       865727 :           dump () && dump ("Depset:%u %s entity:%u %C:%N", ix,
   17429              :                            b->entity_kind_name (), b->cluster,
   17430         3138 :                            TREE_CODE (decl), decl);
   17431              : 
   17432       862589 :           sec.u (ct_decl);
   17433       862589 :           sec.tree_node (decl);
   17434              : 
   17435      1021206 :           dump () && dump ("Wrote declaration entity:%u %C:%N",
   17436         3138 :                            b->cluster, TREE_CODE (decl), decl);
   17437              :           break;
   17438              :         }
   17439              :     }
   17440              : 
   17441              :   depset *namer = NULL;
   17442              : 
   17443              :   /* Write out definitions  */
   17444      1262982 :   for (unsigned ix = 0; ix != size; ix++)
   17445              :     {
   17446      1018068 :       depset *b = scc[ix];
   17447      1018068 :       tree decl = b->get_entity ();
   17448      1018068 :       switch (b->get_entity_kind ())
   17449              :         {
   17450              :         default:
   17451              :           break;
   17452              : 
   17453       862589 :         case depset::EK_SPECIALIZATION:
   17454       862589 :         case depset::EK_PARTIAL:
   17455       862589 :         case depset::EK_DECL:
   17456       862589 :           if (!namer)
   17457       233272 :             namer = b;
   17458              : 
   17459       862589 :           if (b->has_defn ())
   17460              :             {
   17461       333052 :               sec.u (ct_defn);
   17462       333052 :               sec.tree_node (decl);
   17463       333992 :               dump () && dump ("Writing definition %N", decl);
   17464       333052 :               sec.write_definition (decl, b->refs_tu_local ());
   17465              : 
   17466       333052 :               if (!namer->has_defn ())
   17467      1018068 :                 namer = b;
   17468              :             }
   17469              :           break;
   17470              :         }
   17471              :     }
   17472              : 
   17473              :   /* We don't find the section by name.  Use depset's decl's name for
   17474              :      human friendliness.  */
   17475       244914 :   unsigned name = 0;
   17476       244914 :   tree naming_decl = NULL_TREE;
   17477       244914 :   if (namer)
   17478              :     {
   17479       233272 :       naming_decl = namer->get_entity ();
   17480       233272 :       if (namer->get_entity_kind () == depset::EK_USING)
   17481              :         /* This unfortunately names the section from the target of the
   17482              :            using decl.  But the name is only a guide, so Do Not Care.  */
   17483            0 :         naming_decl = OVL_FUNCTION (naming_decl);
   17484       233272 :       if (DECL_IMPLICIT_TYPEDEF_P (naming_decl))
   17485              :         /* Lose any anonymousness.  */
   17486        79473 :         naming_decl = TYPE_NAME (TREE_TYPE (naming_decl));
   17487       233272 :       name = to->qualified_name (naming_decl, namer->has_defn ());
   17488              :     }
   17489              : 
   17490       244914 :   unsigned bytes = sec.pos;
   17491       244914 :   unsigned snum = sec.end (to, name, crc_ptr);
   17492              : 
   17493      1262982 :   for (unsigned ix = size; ix--;)
   17494      1018068 :     gcc_checking_assert (scc[ix]->section == snum);
   17495              : 
   17496       244914 :   dump.outdent ();
   17497       246445 :   dump () && dump ("Wrote section:%u named-by:%N", table.section, naming_decl);
   17498              : 
   17499       244914 :   return bytes;
   17500       244914 : }
   17501              : 
   17502              : /* Read a cluster from section SNUM.  */
   17503              : 
   17504              : bool
   17505       198340 : module_state::read_cluster (unsigned snum)
   17506              : {
   17507       198340 :   trees_in sec (this);
   17508              : 
   17509       198340 :   if (!sec.begin (loc, from (), snum))
   17510              :     return false;
   17511              : 
   17512       199570 :   dump () && dump ("Reading section:%u", snum);
   17513       198340 :   dump.indent ();
   17514              : 
   17515              :   /* We care about structural equality.  */
   17516       198340 :   comparing_dependent_aliases++;
   17517              : 
   17518              :   /* First seed the imports.  */
   17519      1242803 :   while (tree import = sec.tree_node ())
   17520      2287452 :     dump (dumper::CLUSTER) && dump ("Seeded import %N", import);
   17521              : 
   17522      1428362 :   while (!sec.get_overrun () && sec.more_p ())
   17523              :     {
   17524      1230022 :       unsigned ct = sec.u ();
   17525      1230022 :       switch (ct)
   17526              :         {
   17527            0 :         default:
   17528            0 :           sec.set_overrun ();
   17529            0 :           break;
   17530              : 
   17531       104380 :         case ct_bind:
   17532              :           /* A set of namespace bindings.  */
   17533       104380 :           {
   17534       104380 :             tree ns = sec.tree_node ();
   17535       104380 :             tree name = sec.tree_node ();
   17536       104380 :             tree decls = NULL_TREE;
   17537       104380 :             tree visible = NULL_TREE;
   17538       104380 :             tree internal = NULL_TREE;
   17539       104380 :             tree type = NULL_TREE;
   17540       104380 :             bool dedup = false;
   17541       104380 :             bool global_p = is_header ();
   17542              : 
   17543              :             /* We rely on the bindings being in the reverse order of
   17544              :                the resulting overload set.  */
   17545       240384 :             for (;;)
   17546              :               {
   17547       240384 :                 int flags = sec.i ();
   17548       240384 :                 if (flags < 0)
   17549              :                   break;
   17550              : 
   17551       136004 :                 if ((flags & cbf_hidden)
   17552           62 :                     && (flags & (cbf_using | cbf_export)))
   17553            0 :                   sec.set_overrun ();
   17554       136004 :                 if ((flags & cbf_internal)
   17555           15 :                     && flags != cbf_internal)
   17556            0 :                   sec.set_overrun ();
   17557              : 
   17558            0 :                 if (flags & cbf_internal)
   17559              :                   {
   17560           15 :                     tree name = sec.tree_node ();
   17561           15 :                     location_t loc = read_location (sec);
   17562           15 :                     if (sec.get_overrun ())
   17563              :                       break;
   17564              : 
   17565           15 :                     tree decl = make_node (TU_LOCAL_ENTITY);
   17566           15 :                     TU_LOCAL_ENTITY_NAME (decl) = name;
   17567           15 :                     TU_LOCAL_ENTITY_LOCATION (decl) = loc;
   17568           15 :                     internal = tree_cons (NULL_TREE, decl, internal);
   17569           15 :                     continue;
   17570           15 :                   }
   17571              : 
   17572       135989 :                 tree decl = sec.tree_node ();
   17573       135989 :                 if (sec.get_overrun ())
   17574              :                   break;
   17575              : 
   17576       135989 :                 if (!global_p)
   17577              :                   {
   17578              :                     /* Check if the decl could require GM merging.  */
   17579         4856 :                     tree orig = get_originating_module_decl (decl);
   17580         4856 :                     tree inner = STRIP_TEMPLATE (orig);
   17581         4856 :                     if (!DECL_LANG_SPECIFIC (inner)
   17582         9712 :                         || !DECL_MODULE_ATTACH_P (inner))
   17583              :                       global_p = true;
   17584              :                   }
   17585              : 
   17586       135989 :                 if (decls && TREE_CODE (decl) == TYPE_DECL)
   17587              :                   {
   17588              :                     /* Stat hack.  */
   17589           51 :                     if (type || !DECL_IMPLICIT_TYPEDEF_P (decl))
   17590            0 :                       sec.set_overrun ();
   17591              : 
   17592           51 :                     if (flags & cbf_using)
   17593              :                       {
   17594            3 :                         type = build_lang_decl_loc (UNKNOWN_LOCATION,
   17595              :                                                     USING_DECL,
   17596            3 :                                                     DECL_NAME (decl),
   17597              :                                                     NULL_TREE);
   17598            3 :                         USING_DECL_DECLS (type) = decl;
   17599            3 :                         USING_DECL_SCOPE (type) = CP_DECL_CONTEXT (decl);
   17600            3 :                         DECL_CONTEXT (type) = ns;
   17601              : 
   17602            3 :                         DECL_MODULE_PURVIEW_P (type) = true;
   17603            3 :                         if (flags & cbf_export)
   17604            3 :                           DECL_MODULE_EXPORT_P (type) = true;
   17605              :                       }
   17606              :                     else
   17607              :                       type = decl;
   17608              :                   }
   17609              :                 else
   17610              :                   {
   17611       135938 :                     if ((flags & cbf_using) &&
   17612        13278 :                         !DECL_DECLARES_FUNCTION_P (decl))
   17613              :                       {
   17614              :                         /* We should only see a single non-function using-decl
   17615              :                            for a binding; more than that would clash.  */
   17616         4585 :                         if (decls)
   17617            0 :                           sec.set_overrun ();
   17618              : 
   17619              :                         /* FIXME: Propagate the location of the using-decl
   17620              :                            for use in diagnostics.  */
   17621         4585 :                         decls = build_lang_decl_loc (UNKNOWN_LOCATION,
   17622              :                                                      USING_DECL,
   17623         4585 :                                                      DECL_NAME (decl),
   17624              :                                                      NULL_TREE);
   17625         4585 :                         USING_DECL_DECLS (decls) = decl;
   17626              :                         /* We don't currently record the actual scope of the
   17627              :                            using-declaration, but this approximation should
   17628              :                            generally be good enough.  */
   17629         4585 :                         USING_DECL_SCOPE (decls) = CP_DECL_CONTEXT (decl);
   17630         4585 :                         DECL_CONTEXT (decls) = ns;
   17631              : 
   17632         4585 :                         DECL_MODULE_PURVIEW_P (decls) = true;
   17633         4585 :                         if (flags & cbf_export)
   17634         4573 :                           DECL_MODULE_EXPORT_P (decls) = true;
   17635              :                       }
   17636       131353 :                     else if (decls
   17637        99780 :                              || (flags & (cbf_hidden | cbf_using))
   17638       225883 :                              || DECL_FUNCTION_TEMPLATE_P (decl))
   17639              :                       {
   17640        51949 :                         decls = ovl_make (decl, decls);
   17641        51949 :                         if (flags & cbf_using)
   17642              :                           {
   17643         8693 :                             dedup = true;
   17644         8693 :                             OVL_USING_P (decls) = true;
   17645         8693 :                             OVL_PURVIEW_P (decls) = true;
   17646         8693 :                             if (flags & cbf_export)
   17647         8678 :                               OVL_EXPORT_P (decls) = true;
   17648              :                           }
   17649              : 
   17650        51949 :                         if (flags & cbf_hidden)
   17651           62 :                           OVL_HIDDEN_P (decls) = true;
   17652        51887 :                         else if (dedup)
   17653         8924 :                           OVL_DEDUP_P (decls) = true;
   17654              :                       }
   17655              :                     else
   17656              :                       decls = decl;
   17657              : 
   17658       135926 :                     if (flags & cbf_export
   17659       135938 :                         || (!(flags & cbf_hidden)
   17660         3476 :                             && (is_module () || is_partition ())))
   17661              :                       visible = decls;
   17662              :                   }
   17663              :               }
   17664              : 
   17665       104380 :             if (!decls && !internal)
   17666            0 :               sec.set_overrun ();
   17667              : 
   17668       104380 :             if (sec.get_overrun ())
   17669              :               break; /* Bail.  */
   17670              : 
   17671       105244 :             dump () && dump ("Binding of %P", ns, name);
   17672       104380 :             if (!set_module_binding (ns, name, mod, global_p,
   17673       104380 :                                      is_module () || is_partition (),
   17674              :                                      decls, type, visible, internal))
   17675            0 :               sec.set_overrun ();
   17676              :           }
   17677              :           break;
   17678              : 
   17679       813201 :         case ct_decl:
   17680              :           /* A decl.  */
   17681       813201 :           {
   17682       813201 :             tree decl = sec.tree_node ();
   17683      2245222 :             dump () && dump ("Read declaration of %N", decl);
   17684              :           }
   17685              :           break;
   17686              : 
   17687       312441 :         case ct_defn:
   17688       312441 :           {
   17689       312441 :             tree decl = sec.tree_node ();
   17690       313735 :             dump () && dump ("Reading definition of %N", decl);
   17691       312441 :             sec.read_definition (decl);
   17692              :           }
   17693       312441 :           break;
   17694              :         }
   17695              :     }
   17696              : 
   17697              :   /* When lazy loading is in effect, we can be in the middle of
   17698              :      parsing or instantiating a function.  Save it away.
   17699              :      push_function_context does too much work.   */
   17700       198340 :   tree old_cfd = current_function_decl;
   17701       198340 :   struct function *old_cfun = cfun;
   17702       359302 :   for (const post_process_data& pdata : sec.post_process ())
   17703              :     {
   17704       119690 :       tree decl = pdata.decl;
   17705              : 
   17706       119690 :       bool abstract = false;
   17707       119690 :       if (TREE_CODE (decl) == TEMPLATE_DECL)
   17708              :         {
   17709        72846 :           abstract = true;
   17710        72846 :           decl = DECL_TEMPLATE_RESULT (decl);
   17711              :         }
   17712              : 
   17713       119690 :       current_function_decl = decl;
   17714       119690 :       allocate_struct_function (decl, abstract);
   17715       119690 :       cfun->language = ggc_cleared_alloc<language_function> ();
   17716       119690 :       cfun->language->base.x_stmt_tree.stmts_are_full_exprs_p = 1;
   17717       119690 :       cfun->function_start_locus = pdata.start_locus;
   17718       119690 :       cfun->function_end_locus = pdata.end_locus;
   17719       119690 :       cfun->language->returns_value = pdata.returns_value;
   17720       119690 :       cfun->language->returns_null = pdata.returns_null;
   17721       119690 :       cfun->language->returns_abnormally = pdata.returns_abnormally;
   17722       119690 :       cfun->language->infinite_loop = pdata.infinite_loop;
   17723       119690 :       cfun->coroutine_component = DECL_COROUTINE_P (decl);
   17724              : 
   17725              :       /* Make sure we emit explicit instantiations.
   17726              :          FIXME do we want to do this in expand_or_defer_fn instead?  */
   17727       119690 :       if (DECL_EXPLICIT_INSTANTIATION (decl)
   17728       119690 :           && !DECL_EXTERNAL (decl))
   17729           27 :         setup_explicit_instantiation_definition_linkage (decl);
   17730              : 
   17731       119690 :       if (abstract)
   17732              :         ;
   17733        46844 :       else if (DECL_MAYBE_IN_CHARGE_CDTOR_P (decl))
   17734         8102 :         vec_safe_push (post_load_decls, decl);
   17735              :       else
   17736              :         {
   17737        38742 :           bool aggr = aggregate_value_p (DECL_RESULT (decl), decl);
   17738              : #ifdef PCC_STATIC_STRUCT_RETURN
   17739              :           cfun->returns_pcc_struct = aggr;
   17740              : #endif
   17741        38742 :           cfun->returns_struct = aggr;
   17742        38742 :           expand_or_defer_fn (decl);
   17743              : 
   17744              :           /* If we first see this function after at_eof, it doesn't get
   17745              :              note_vague_linkage_fn from tentative_decl_linkage, so the loop in
   17746              :              c_parse_final_cleanups won't consider it.  But with DECL_COMDAT we
   17747              :              can just clear DECL_EXTERNAL and let cgraph decide.
   17748              :              FIXME handle this outside module.cc after GCC 15.  */
   17749         4238 :           if (at_eof && DECL_COMDAT (decl) && DECL_EXTERNAL (decl)
   17750        40400 :               && DECL_NOT_REALLY_EXTERN (decl))
   17751         1618 :             DECL_EXTERNAL (decl) = false;
   17752              :         }
   17753              : 
   17754              :     }
   17755       198408 :   for (const tree& type : sec.post_process_type ())
   17756              :     {
   17757              :       /* Attempt to complete an array type now in case its element type
   17758              :          had a definition streamed later in the cluster.  */
   17759           32 :       gcc_checking_assert (TREE_CODE (type) == ARRAY_TYPE);
   17760           32 :       complete_type (type);
   17761              :     }
   17762       198340 :   set_cfun (old_cfun);
   17763       198340 :   current_function_decl = old_cfd;
   17764       198340 :   comparing_dependent_aliases--;
   17765              : 
   17766       198340 :   dump.outdent ();
   17767       199570 :   dump () && dump ("Read section:%u", snum);
   17768              : 
   17769       198340 :   loaded_clusters++;
   17770              : 
   17771       198340 :   if (!sec.end (from ()))
   17772              :     return false;
   17773              : 
   17774              :   return true;
   17775       198340 : }
   17776              : 
   17777              : void
   17778       136795 : module_state::write_namespace (bytes_out &sec, depset *dep)
   17779              : {
   17780       136795 :   unsigned ns_num = dep->cluster;
   17781       136795 :   unsigned ns_import = 0;
   17782              : 
   17783       136795 :   if (dep->is_import ())
   17784            0 :     ns_import = dep->section;
   17785       136795 :   else if (dep->get_entity () != global_namespace)
   17786        85956 :     ns_num++;
   17787              : 
   17788       136795 :   sec.u (ns_import);
   17789       136795 :   sec.u (ns_num);
   17790       136795 : }
   17791              : 
   17792              : tree
   17793       185298 : module_state::read_namespace (bytes_in &sec)
   17794              : {
   17795       185298 :   unsigned ns_import = sec.u ();
   17796       185298 :   unsigned ns_num = sec.u ();
   17797       185298 :   tree ns = NULL_TREE;
   17798              : 
   17799       185298 :   if (ns_import || ns_num)
   17800              :     {
   17801       116121 :       if (!ns_import)
   17802       116121 :         ns_num--;
   17803              : 
   17804       116121 :       if (unsigned origin = slurp->remap_module (ns_import))
   17805              :         {
   17806       116121 :           module_state *from = (*modules)[origin];
   17807       116121 :           if (ns_num < from->entity_num)
   17808              :             {
   17809       116121 :               binding_slot &slot = (*entity_ary)[from->entity_lwm + ns_num];
   17810              : 
   17811       116121 :               if (!slot.is_lazy ())
   17812       116121 :                 ns = slot;
   17813              :             }
   17814              :         }
   17815              :       else
   17816            0 :         sec.set_overrun ();
   17817              :     }
   17818              :   else
   17819        69177 :     ns = global_namespace;
   17820              : 
   17821       185298 :   return ns;
   17822              : }
   17823              : 
   17824              : /* SPACES is a sorted vector of namespaces.  Write out the namespaces
   17825              :    to MOD_SNAME_PFX.nms section.   */
   17826              : 
   17827              : void
   17828          567 : module_state::write_namespaces (elf_out *to, vec<depset *> spaces,
   17829              :                                 unsigned num, unsigned *crc_p)
   17830              : {
   17831          625 :   dump () && dump ("Writing namespaces");
   17832          567 :   dump.indent ();
   17833              : 
   17834          567 :   bytes_out sec (to);
   17835          567 :   sec.begin ();
   17836              : 
   17837         2787 :   for (unsigned ix = 0; ix != num; ix++)
   17838              :     {
   17839         2220 :       depset *b = spaces[ix];
   17840         2220 :       tree ns = b->get_entity ();
   17841              : 
   17842              :       /* This could be an anonymous namespace even for a named module,
   17843              :          since we can still emit no-linkage decls.  */
   17844         2220 :       gcc_checking_assert (TREE_CODE (ns) == NAMESPACE_DECL);
   17845              : 
   17846         2220 :       unsigned flags = 0;
   17847         2220 :       if (TREE_PUBLIC (ns))
   17848         2162 :         flags |= 1;
   17849         2220 :       if (DECL_NAMESPACE_INLINE_P (ns))
   17850          413 :         flags |= 2;
   17851         2220 :       if (DECL_MODULE_PURVIEW_P (ns))
   17852         1916 :         flags |= 4;
   17853         2220 :       if (DECL_MODULE_EXPORT_P (ns))
   17854         1696 :         flags |= 8;
   17855         2220 :       if (TREE_DEPRECATED (ns))
   17856           12 :         flags |= 16;
   17857              : 
   17858         2360 :       dump () && dump ("Writing namespace:%u %N%s%s%s%s",
   17859              :                        b->cluster, ns,
   17860          140 :                        flags & 1 ? ", public" : "",
   17861          140 :                        flags & 2 ? ", inline" : "",
   17862          140 :                        flags & 4 ? ", purview" : "",
   17863          140 :                        flags & 8 ? ", export" : "",
   17864          140 :                        flags & 16 ? ", deprecated" : "");
   17865         2220 :       sec.u (b->cluster);
   17866         2220 :       sec.u (to->name (DECL_NAME (ns)));
   17867         2220 :       write_namespace (sec, b->deps[0]);
   17868              : 
   17869         2220 :       sec.u (flags);
   17870         2220 :       write_location (sec, DECL_SOURCE_LOCATION (ns));
   17871              : 
   17872         2220 :       if (DECL_NAMESPACE_INLINE_P (ns))
   17873              :         {
   17874          413 :           if (tree attr = lookup_attribute ("abi_tag", DECL_ATTRIBUTES (ns)))
   17875              :             {
   17876          115 :               tree tags = TREE_VALUE (attr);
   17877          115 :               sec.u (list_length (tags));
   17878          233 :               for (tree tag = tags; tag; tag = TREE_CHAIN (tag))
   17879          118 :                 sec.str (TREE_STRING_POINTER (TREE_VALUE (tag)));
   17880              :             }
   17881              :           else
   17882          298 :             sec.u (0);
   17883              :         }
   17884              :     }
   17885              : 
   17886          567 :   sec.end (to, to->name (MOD_SNAME_PFX ".nms"), crc_p);
   17887          567 :   dump.outdent ();
   17888          567 : }
   17889              : 
   17890              : /* Read the namespace hierarchy from MOD_SNAME_PFX.namespace.  Fill in
   17891              :    SPACES from that data.  */
   17892              : 
   17893              : bool
   17894          587 : module_state::read_namespaces (unsigned num)
   17895              : {
   17896          587 :   bytes_in sec;
   17897              : 
   17898          587 :   if (!sec.begin (loc, from (), MOD_SNAME_PFX ".nms"))
   17899              :     return false;
   17900              : 
   17901          657 :   dump () && dump ("Reading namespaces");
   17902          587 :   dump.indent ();
   17903              : 
   17904         3253 :   for (unsigned ix = 0; ix != num; ix++)
   17905              :     {
   17906         2666 :       unsigned entity_index = sec.u ();
   17907         2666 :       unsigned name = sec.u ();
   17908              : 
   17909         2666 :       tree parent = read_namespace (sec);
   17910              : 
   17911              :       /* See comment in write_namespace about why not bits.  */
   17912         2666 :       unsigned flags = sec.u ();
   17913         2666 :       location_t src_loc = read_location (sec);
   17914         2666 :       unsigned tags_count = (flags & 2) ? sec.u () : 0;
   17915              : 
   17916         2666 :       if (entity_index >= entity_num
   17917         2666 :           || !parent
   17918         2666 :           || (flags & 0xc) == 0x8)
   17919            0 :         sec.set_overrun ();
   17920              : 
   17921              :       tree tags = NULL_TREE;
   17922         2802 :       while (tags_count--)
   17923              :         {
   17924          136 :           size_t len;
   17925          136 :           const char *str = sec.str (&len);
   17926          136 :           tags = tree_cons (NULL_TREE, build_string (len + 1, str), tags);
   17927          136 :           tags = nreverse (tags);
   17928              :         }
   17929              : 
   17930         2666 :       if (sec.get_overrun ())
   17931              :         break;
   17932              : 
   17933         5274 :       tree id = name ? get_identifier (from ()->name (name)) : NULL_TREE;
   17934              : 
   17935         2860 :       dump () && dump ("Read namespace:%u %P%s%s%s%s",
   17936              :                        entity_index, parent, id,
   17937           97 :                        flags & 1 ? ", public" : "",
   17938              :                        flags & 2 ? ", inline" : "",
   17939           97 :                        flags & 4 ? ", purview" : "",
   17940           97 :                        flags & 8 ? ", export" : "",
   17941           97 :                        flags & 16 ? ", deprecated" : "");
   17942         2666 :       bool visible_p = ((flags & 8)
   17943         2666 :                         || ((flags & 1)
   17944          437 :                             && (flags & 4)
   17945          102 :                             && (is_partition () || is_module ())));
   17946         2666 :       tree inner = add_imported_namespace (parent, id, src_loc, mod,
   17947              :                                            bool (flags & 2), visible_p);
   17948         2666 :       if (!inner)
   17949              :         {
   17950            0 :           sec.set_overrun ();
   17951            0 :           break;
   17952              :         }
   17953              : 
   17954         2666 :       if (is_partition ())
   17955              :         {
   17956           48 :           if (flags & 4)
   17957           42 :             DECL_MODULE_PURVIEW_P (inner) = true;
   17958           48 :           if (flags & 8)
   17959           27 :             DECL_MODULE_EXPORT_P (inner) = true;
   17960              :         }
   17961              : 
   17962         2666 :       if (flags & 16)
   17963           17 :         TREE_DEPRECATED (inner) = true;
   17964              : 
   17965         2666 :       if (tags)
   17966          133 :         DECL_ATTRIBUTES (inner)
   17967          266 :           = tree_cons (get_identifier ("abi_tag"), tags, DECL_ATTRIBUTES (inner));
   17968              : 
   17969              :       /* Install the namespace.  */
   17970         2666 :       (*entity_ary)[entity_lwm + entity_index] = inner;
   17971         2666 :       if (DECL_MODULE_IMPORT_P (inner))
   17972              :         {
   17973            0 :           bool existed;
   17974            0 :           unsigned *slot = &entity_map->get_or_insert
   17975            0 :             (DECL_UID (inner), &existed);
   17976            0 :           if (existed)
   17977              :             /* If it existed, it should match.  */
   17978            0 :             gcc_checking_assert (inner == (*entity_ary)[*slot]);
   17979              :           else
   17980            0 :             *slot = entity_lwm + entity_index;
   17981              :         }
   17982              :     }
   17983              : 
   17984          587 :   dump.outdent ();
   17985          587 :   if (!sec.end (from ()))
   17986              :     return false;
   17987              :   return true;
   17988          587 : }
   17989              : 
   17990              : unsigned
   17991          567 : module_state::write_using_directives (elf_out *to, depset::hash &table,
   17992              :                                       vec<depset *> spaces, unsigned *crc_p)
   17993              : {
   17994          625 :   dump () && dump ("Writing using-directives");
   17995          567 :   dump.indent ();
   17996              : 
   17997          567 :   bytes_out sec (to);
   17998          567 :   sec.begin ();
   17999              : 
   18000          567 :   unsigned num = 0;
   18001         3354 :   auto emit_one_ns = [&](depset *parent_dep)
   18002              :     {
   18003         2787 :       tree parent = parent_dep->get_entity ();
   18004         3282 :       for (auto udir : NAMESPACE_LEVEL (parent)->using_directives)
   18005              :         {
   18006          169 :           if (TREE_CODE (udir) != USING_DECL || !DECL_MODULE_PURVIEW_P (udir))
   18007           12 :             continue;
   18008          157 :           bool exported = DECL_MODULE_EXPORT_P (udir);
   18009          157 :           tree target = USING_DECL_DECLS (udir);
   18010          157 :           depset *target_dep = table.find_dependency (target);
   18011              : 
   18012              :           /* An using-directive imported from a different module might not
   18013              :              have been walked earlier (PR c++/122915).  But importers will
   18014              :              be able to just refer to the decl in that module unless it was
   18015              :              a partition anyway, so we don't have anything to do here.  */
   18016          157 :           if (!target_dep)
   18017              :             {
   18018            6 :               gcc_checking_assert (DECL_MODULE_IMPORT_P (udir));
   18019            6 :               continue;
   18020              :             }
   18021              : 
   18022          175 :           dump () && dump ("Writing using-directive in %N for %N",
   18023              :                            parent, target);
   18024          151 :           sec.u (exported);
   18025          151 :           write_namespace (sec, parent_dep);
   18026          151 :           write_namespace (sec, target_dep);
   18027          151 :           ++num;
   18028              :         }
   18029         3354 :     };
   18030              : 
   18031          567 :   emit_one_ns (table.find_dependency (global_namespace));
   18032         3921 :   for (depset *parent_dep : spaces)
   18033         2220 :     emit_one_ns (parent_dep);
   18034              : 
   18035          567 :   sec.end (to, to->name (MOD_SNAME_PFX ".udi"), crc_p);
   18036          567 :   dump.outdent ();
   18037              : 
   18038          567 :   return num;
   18039          567 : }
   18040              : 
   18041              : bool
   18042          155 : module_state::read_using_directives (unsigned num)
   18043              : {
   18044          155 :   if (!bitmap_bit_p (this_module ()->imports, mod))
   18045              :     {
   18046           12 :       dump () && dump ("Ignoring using-directives because module %M "
   18047              :                        "is not visible in this TU", this);
   18048            9 :       return true;
   18049              :     }
   18050              : 
   18051          146 :   bytes_in sec;
   18052              : 
   18053          146 :   if (!sec.begin (loc, from (), MOD_SNAME_PFX ".udi"))
   18054              :     return false;
   18055              : 
   18056          158 :   dump () && dump ("Reading using-directives");
   18057          146 :   dump.indent ();
   18058              : 
   18059          320 :   for (unsigned ix = 0; ix != num; ++ix)
   18060              :     {
   18061          174 :       bool exported = sec.u ();
   18062          174 :       tree parent = read_namespace (sec);
   18063          174 :       tree target = read_namespace (sec);
   18064          174 :       if (sec.get_overrun ())
   18065              :         break;
   18066              : 
   18067          186 :       dump () && dump ("Read using-directive in %N for %N", parent, target);
   18068          174 :       if (exported || is_module () || is_partition ())
   18069          147 :         add_imported_using_namespace (parent, target);
   18070              :     }
   18071              : 
   18072          146 :   dump.outdent ();
   18073          146 :   if (!sec.end (from ()))
   18074              :     return false;
   18075              :   return true;
   18076          146 : }
   18077              : 
   18078              : /* Write the binding TABLE to MOD_SNAME_PFX.bnd   */
   18079              : 
   18080              : unsigned
   18081         2685 : module_state::write_bindings (elf_out *to, vec<depset *> sccs, unsigned *crc_p)
   18082              : {
   18083         2982 :   dump () && dump ("Writing binding table");
   18084         2685 :   dump.indent ();
   18085              : 
   18086         2685 :   unsigned num = 0;
   18087         2685 :   bytes_out sec (to);
   18088         2685 :   sec.begin ();
   18089              : 
   18090      2050808 :   for (unsigned ix = 0; ix != sccs.length (); ix++)
   18091              :     {
   18092      1022719 :       depset *b = sccs[ix];
   18093      1022719 :       if (b->is_binding ())
   18094              :         {
   18095       134273 :           tree ns = b->get_entity ();
   18096       135468 :           dump () && dump ("Bindings %P section:%u", ns, b->get_name (),
   18097              :                            b->section);
   18098       134273 :           sec.u (to->name (b->get_name ()));
   18099       134273 :           write_namespace (sec, b->deps[0]);
   18100       134273 :           sec.u (b->section);
   18101       134273 :           num++;
   18102              :         }
   18103              :     }
   18104              : 
   18105         2685 :   sec.end (to, to->name (MOD_SNAME_PFX ".bnd"), crc_p);
   18106         2685 :   dump.outdent ();
   18107              : 
   18108         2685 :   return num;
   18109         2685 : }
   18110              : 
   18111              : /* Read the binding table from MOD_SNAME_PFX.bind.  */
   18112              : 
   18113              : bool
   18114         2881 : module_state::read_bindings (unsigned num, unsigned lwm, unsigned hwm)
   18115              : {
   18116         2881 :   bytes_in sec;
   18117              : 
   18118         2881 :   if (!sec.begin (loc, from (), MOD_SNAME_PFX ".bnd"))
   18119              :     return false;
   18120              : 
   18121         3401 :   dump () && dump ("Reading binding table");
   18122         2881 :   dump.indent ();
   18123       185165 :   for (; !sec.get_overrun () && num--;)
   18124              :     {
   18125       182284 :       const char *name = from ()->name (sec.u ());
   18126       182284 :       tree ns = read_namespace (sec);
   18127       182284 :       unsigned snum = sec.u ();
   18128              : 
   18129       182284 :       if (!ns || !name || (snum - lwm) >= (hwm - lwm))
   18130            0 :         sec.set_overrun ();
   18131       182284 :       if (!sec.get_overrun ())
   18132              :         {
   18133       182284 :           tree id = get_identifier (name);
   18134       183786 :           dump () && dump ("Bindings %P section:%u", ns, id, snum);
   18135       182284 :           if (mod && !import_module_binding (ns, id, mod, snum))
   18136              :             break;
   18137              :         }
   18138              :     }
   18139              : 
   18140         2881 :   dump.outdent ();
   18141         2881 :   if (!sec.end (from ()))
   18142              :     return false;
   18143              :   return true;
   18144         2881 : }
   18145              : 
   18146              : /* Write the entity table to MOD_SNAME_PFX.ent
   18147              : 
   18148              :    Each entry is a section number.  */
   18149              : 
   18150              : void
   18151         2425 : module_state::write_entities (elf_out *to, vec<depset *> depsets,
   18152              :                               unsigned count, unsigned *crc_p)
   18153              : {
   18154         2669 :   dump () && dump ("Writing entities");
   18155         2425 :   dump.indent ();
   18156              : 
   18157         2425 :   bytes_out sec (to);
   18158         2425 :   sec.begin ();
   18159              : 
   18160         2425 :   unsigned current = 0;
   18161      1025123 :   for (unsigned ix = 0; ix < depsets.length (); ix++)
   18162              :     {
   18163      1022698 :       depset *d = depsets[ix];
   18164              : 
   18165      1889932 :       switch (d->get_entity_kind ())
   18166              :         {
   18167              :         default:
   18168              :           break;
   18169              : 
   18170         4645 :         case depset::EK_NAMESPACE:
   18171         4645 :           if (!d->is_import () && d->get_entity () != global_namespace)
   18172              :             {
   18173         2220 :               gcc_checking_assert (d->cluster == current);
   18174         2220 :               current++;
   18175         2220 :               sec.u (0);
   18176              :             }
   18177              :           break;
   18178              : 
   18179       862589 :         case depset::EK_DECL:
   18180       862589 :         case depset::EK_SPECIALIZATION:
   18181       862589 :         case depset::EK_PARTIAL:
   18182      1725178 :           gcc_checking_assert (!d->is_unreached ()
   18183              :                                && !d->is_import ()
   18184              :                                && d->cluster == current
   18185              :                                && d->section);
   18186       862589 :           current++;
   18187       862589 :           sec.u (d->section);
   18188       862589 :           break;
   18189              :         }
   18190              :     }
   18191         2425 :   gcc_assert (count == current);
   18192         2425 :   sec.end (to, to->name (MOD_SNAME_PFX ".ent"), crc_p);
   18193         2425 :   dump.outdent ();
   18194         2425 : }
   18195              : 
   18196              : bool
   18197         2655 : module_state::read_entities (unsigned count, unsigned lwm, unsigned hwm)
   18198              : {
   18199         2655 :   trees_in sec (this);
   18200              : 
   18201         2655 :   if (!sec.begin (loc, from (), MOD_SNAME_PFX ".ent"))
   18202              :     return false;
   18203              : 
   18204         3121 :   dump () && dump ("Reading entities");
   18205         2655 :   dump.indent ();
   18206              : 
   18207      1170033 :   for (binding_slot *slot = entity_ary->begin () + entity_lwm; count--; slot++)
   18208              :     {
   18209      1167378 :       unsigned snum = sec.u ();
   18210      1167378 :       if (snum && (snum - lwm) >= (hwm - lwm))
   18211            0 :         sec.set_overrun ();
   18212      1167378 :       if (sec.get_overrun ())
   18213              :         break;
   18214              : 
   18215      1167378 :       if (snum)
   18216      1164712 :         slot->set_lazy (snum << 2);
   18217              :     }
   18218              : 
   18219         2655 :   dump.outdent ();
   18220         2655 :   if (!sec.end (from ()))
   18221              :     return false;
   18222              :   return true;
   18223         2655 : }
   18224              : 
   18225              : /* Write the pending table to MOD_SNAME_PFX.pnd
   18226              : 
   18227              :    The pending table holds information about clusters that need to be
   18228              :    loaded because they contain information about something that is not
   18229              :    found by namespace-scope lookup.
   18230              : 
   18231              :    The three cases are:
   18232              : 
   18233              :    (a) Template (maybe-partial) specializations that we have
   18234              :    instantiated or defined.  When an importer needs to instantiate
   18235              :    that template, they /must have/ the partial, explicit & extern
   18236              :    specializations available.  If they have the other specializations
   18237              :    available, they'll have less work to do.  Thus, when we're about to
   18238              :    instantiate FOO, we have to be able to ask 'are there any
   18239              :    specialization of FOO in our imports?'.
   18240              : 
   18241              :    (b) (Maybe-implicit) member functions definitions.  A class could
   18242              :    be defined in one header, and an inline member defined in a
   18243              :    different header (this occurs in the STL).  Similarly, like the
   18244              :    specialization case, an implicit member function could have been
   18245              :    'instantiated' in one module, and it'd be nice to not have to
   18246              :    reinstantiate it in another.
   18247              : 
   18248              :    (c) Classes completed elsewhere.  A class could be declared in one
   18249              :    header and defined in another.  We need to know to load the class
   18250              :    definition before looking in it.  It does highlight an issue --
   18251              :    there could be an intermediate import between the outermost containing
   18252              :    namespace-scope class and the innermost being-defined class.  This is
   18253              :    actually possible with all of these cases, so be aware -- we're not
   18254              :    just talking of one level of import to get to the innermost namespace.
   18255              : 
   18256              :    This gets complicated fast, it took me multiple attempts to even
   18257              :    get something remotely working.  Partially because I focussed on
   18258              :    optimizing what I think turns out to be a smaller problem, given
   18259              :    the known need to do the more general case *anyway*.  I document
   18260              :    the smaller problem, because it does appear to be the natural way
   18261              :    to do it.  It's trap!
   18262              : 
   18263              :    **** THE TRAP
   18264              : 
   18265              :    Let's refer to the primary template or the containing class as the
   18266              :    KEY.  And the specialization or member as the PENDING-ENTITY.  (To
   18267              :    avoid having to say those mouthfuls all the time.)
   18268              : 
   18269              :    In either case, we have an entity and we need some way of mapping
   18270              :    that to a set of entities that need to be loaded before we can
   18271              :    proceed with whatever processing of the entity we were going to do.
   18272              : 
   18273              :    We need to link the key to the pending-entity in some way.  Given a
   18274              :    key, tell me the pending-entities I need to have loaded.  However
   18275              :    we tie the key to the pending-entity must not rely on the key being
   18276              :    loaded -- that'd defeat the lazy loading scheme.
   18277              : 
   18278              :    As the key will be an import in we know its entity number (either
   18279              :    because we imported it, or we're writing it out too).  Thus we can
   18280              :    generate a map of key-indices to pending-entities.  The
   18281              :    pending-entity indices will be into our span of the entity table,
   18282              :    and thus allow them to be lazily loaded.  The key index will be
   18283              :    into another slot of the entity table.  Notice that this checking
   18284              :    could be expensive, we don't want to iterate over a bunch of
   18285              :    pending-entity indices (across multiple imports), every time we're
   18286              :    about do to the thing with the key.  We need to quickly determine
   18287              :    'definitely nothing needed'.
   18288              : 
   18289              :    That's almost good enough, except that key indices are not unique
   18290              :    in a couple of cases :( Specifically the Global Module or a module
   18291              :    partition can result in multiple modules assigning an entity index
   18292              :    for the key.  The decl-merging on loading will detect that so we
   18293              :    only have one Key loaded, and in the entity hash it'll indicate the
   18294              :    entity index of first load.  Which might be different to how we
   18295              :    know it.  Notice this is restricted to GM entities or this-module
   18296              :    entities.  Foreign imports cannot have this.
   18297              : 
   18298              :    We can simply resolve this in the direction of how this module
   18299              :    referred to the key to how the importer knows it.  Look in the
   18300              :    entity table slot that we nominate, maybe lazy load it, and then
   18301              :    lookup the resultant entity in the entity hash to learn how the
   18302              :    importer knows it.
   18303              : 
   18304              :    But we need to go in the other direction :( Given the key, find all
   18305              :    the index-aliases of that key.  We can partially solve that by
   18306              :    adding an alias hash table.  Whenever we load a merged decl, add or
   18307              :    augment a mapping from the entity (or its entity-index) to the
   18308              :    newly-discovered index.  Then when we look for pending entities of
   18309              :    a key, we also iterate over this aliases this mapping provides.
   18310              : 
   18311              :    But that requires the alias to be loaded.  And that's not
   18312              :    necessarily true.
   18313              : 
   18314              :    *** THE SIMPLER WAY
   18315              : 
   18316              :    The remaining fixed thing we have is the innermost namespace
   18317              :    containing the ultimate namespace-scope container of the key and
   18318              :    the name of that container (which might be the key itself).  I.e. a
   18319              :    namespace-decl/identifier/module tuple.  Let's call this the
   18320              :    top-key.  We'll discover that the module is not important here,
   18321              :    because of cross-module possibilities mentioned in case #c above.
   18322              :    We can't markup namespace-binding slots.  The best we can do is
   18323              :    mark the binding vector with 'there's something here', and have
   18324              :    another map from namespace/identifier pairs to a vector of pending
   18325              :    entity indices.
   18326              : 
   18327              :    Maintain a pending-entity map.  This is keyed by top-key, and
   18328              :    maps to a vector of pending-entity indices.  On the binding vector
   18329              :    have flags saying whether the pending-name-entity map has contents.
   18330              :    (We might want to further extend the key to be GM-vs-Partition and
   18331              :    specialization-vs-member, but let's not get ahead of ourselves.)
   18332              : 
   18333              :    For every key-like entity, find the outermost namespace-scope
   18334              :    name.  Use that to lookup in the pending-entity map and then make
   18335              :    sure the specified entities are loaded.
   18336              : 
   18337              :    An optimization might be to have a flag in each key-entity saying
   18338              :    that its top key might be in the entity table.  It's not clear to
   18339              :    me how to set that flag cheaply -- cheaper than just looking.
   18340              : 
   18341              :    FIXME: It'd be nice to have a bit in decls to tell us whether to
   18342              :    even try this.  We can have a 'already done' flag, that we set when
   18343              :    we've done KLASS's lazy pendings.  When we import a module that
   18344              :    registers pendings on the same top-key as KLASS we need to clear
   18345              :    the flag.  A recursive walk of the top-key clearing the bit will
   18346              :    suffice.  Plus we only need to recurse on classes that have the bit
   18347              :    set.  (That means we need to set the bit on parents of KLASS here,
   18348              :    don't forget.)  However, first: correctness, second: efficiency.  */
   18349              : 
   18350              : unsigned
   18351         2685 : module_state::write_pendings (elf_out *to, vec<depset *> depsets,
   18352              :                               depset::hash &table, unsigned *crc_p)
   18353              : {
   18354         2982 :   dump () && dump ("Writing pending-entities");
   18355         2685 :   dump.indent ();
   18356              : 
   18357         2685 :   trees_out sec (to, this, table);
   18358         2685 :   sec.begin ();
   18359              : 
   18360         2685 :   unsigned count = 0;
   18361         2685 :   tree cache_ns = NULL_TREE;
   18362         2685 :   tree cache_id = NULL_TREE;
   18363         2685 :   unsigned cache_section = ~0;
   18364      1025404 :   for (unsigned ix = 0; ix < depsets.length (); ix++)
   18365              :     {
   18366      1022719 :       depset *d = depsets[ix];
   18367              : 
   18368      1022719 :       if (d->is_binding ())
   18369       634551 :         continue;
   18370              : 
   18371       888446 :       if (d->is_import ())
   18372            0 :         continue;
   18373              : 
   18374       888446 :       if (!d->is_pending_entity ())
   18375       500278 :         continue;
   18376              : 
   18377       388168 :       tree key_decl = nullptr;
   18378       388168 :       tree key_ns = find_pending_key (d->get_entity (), &key_decl);
   18379       388168 :       tree key_name = DECL_NAME (key_decl);
   18380              : 
   18381       388168 :       if (IDENTIFIER_ANON_P (key_name))
   18382              :         {
   18383            6 :           gcc_checking_assert (IDENTIFIER_LAMBDA_P (key_name));
   18384           12 :           if (tree attached = LAMBDA_TYPE_EXTRA_SCOPE (TREE_TYPE (key_decl)))
   18385            6 :             key_name = DECL_NAME (attached);
   18386              :           else
   18387              :             {
   18388              :               /* There's nothing to attach it to.  Must
   18389              :                  always reinstantiate.  */
   18390            0 :               dump ()
   18391            0 :                 && dump ("Unattached lambda %N[%u] section:%u",
   18392            0 :                          d->get_entity_kind () == depset::EK_DECL
   18393              :                          ? "Member" : "Specialization", d->get_entity (),
   18394              :                          d->cluster, d->section);
   18395            0 :               continue;
   18396              :             }
   18397              :         }
   18398              : 
   18399       388168 :       char const *also = "";
   18400       388168 :       if (d->section == cache_section
   18401       248387 :           && key_ns == cache_ns
   18402       248387 :           && key_name == cache_id)
   18403              :         /* Same section & key as previous, no need to repeat ourselves.  */
   18404              :         also = "also ";
   18405              :       else
   18406              :         {
   18407       181361 :           cache_ns = key_ns;
   18408       181361 :           cache_id = key_name;
   18409       181361 :           cache_section = d->section;
   18410       181361 :           gcc_checking_assert (table.find_dependency (cache_ns));
   18411       181361 :           sec.tree_node (cache_ns);
   18412       181361 :           sec.tree_node (cache_id);
   18413       181361 :           sec.u (d->cluster);
   18414       181361 :           count++;
   18415              :         }
   18416       389618 :       dump () && dump ("Pending %s %N entity:%u section:%u %skeyed to %P",
   18417          725 :                        d->get_entity_kind () == depset::EK_DECL
   18418              :                        ? "member" : "specialization", d->get_entity (),
   18419              :                        d->cluster, cache_section, also, cache_ns, cache_id);
   18420              :       }
   18421         2685 :   sec.end (to, to->name (MOD_SNAME_PFX ".pnd"), crc_p);
   18422         2685 :   dump.outdent ();
   18423              : 
   18424         2685 :   return count;
   18425         2685 : }
   18426              : 
   18427              : bool
   18428         1525 : module_state::read_pendings (unsigned count)
   18429              : {
   18430         1525 :   trees_in sec (this);
   18431              : 
   18432         1525 :   if (!sec.begin (loc, from (), MOD_SNAME_PFX ".pnd"))
   18433              :     return false;
   18434              : 
   18435         1838 :   dump () && dump ("Reading %u pendings", count);
   18436         1525 :   dump.indent ();
   18437              : 
   18438       242608 :   for (unsigned ix = 0; ix != count; ix++)
   18439              :     {
   18440       241083 :       pending_key key;
   18441       241083 :       unsigned index;
   18442              : 
   18443       241083 :       key.ns = sec.tree_node ();
   18444       241083 :       key.id = sec.tree_node ();
   18445       241083 :       index = sec.u ();
   18446              : 
   18447       241083 :       if (!key.ns || !key.id
   18448       241083 :           || !(TREE_CODE (key.ns) == NAMESPACE_DECL
   18449       241083 :                && !DECL_NAMESPACE_ALIAS (key.ns))
   18450       241083 :           || !identifier_p (key.id)
   18451       482166 :           || index >= entity_num)
   18452            0 :         sec.set_overrun ();
   18453              : 
   18454       241083 :       if (sec.get_overrun ())
   18455              :         break;
   18456              : 
   18457       241858 :       dump () && dump ("Pending:%u keyed to %P", index, key.ns, key.id);
   18458              : 
   18459       241083 :       index += entity_lwm;
   18460       241083 :       auto &vec = pending_table->get_or_insert (key);
   18461       241083 :       vec.safe_push (index);
   18462              :     }
   18463              : 
   18464         1525 :   dump.outdent ();
   18465         1525 :   if (!sec.end (from ()))
   18466              :     return false;
   18467              :   return true;
   18468         1525 : }
   18469              : 
   18470              : /* Read & write locations.  */
   18471              : enum loc_kind {
   18472              :   LK_ORDINARY,
   18473              :   LK_MACRO,
   18474              :   LK_IMPORT_ORDINARY,
   18475              :   LK_IMPORT_MACRO,
   18476              :   LK_ADHOC,
   18477              :   LK_RESERVED,
   18478              : };
   18479              : 
   18480              : static const module_state *
   18481         8604 : module_for_ordinary_loc (location_t loc)
   18482              : {
   18483         8604 :   unsigned pos = 0;
   18484        17208 :   unsigned len = ool->length () - pos;
   18485              : 
   18486         8607 :   while (len)
   18487              :     {
   18488         8607 :       unsigned half = len / 2;
   18489         8607 :       module_state *probe = (*ool)[pos + half];
   18490         8607 :       if (loc < probe->ordinary_locs.first)
   18491              :         len = half;
   18492         8604 :       else if (loc < probe->ordinary_locs.first + probe->ordinary_locs.second)
   18493              :         return probe;
   18494              :       else
   18495              :         {
   18496            0 :           pos += half + 1;
   18497            0 :           len = len - (half + 1);
   18498              :         }
   18499              :     }
   18500              : 
   18501              :   return nullptr;
   18502              : }
   18503              : 
   18504              : static const module_state *
   18505           60 : module_for_macro_loc (location_t loc)
   18506              : {
   18507           60 :   unsigned pos = 1;
   18508           60 :   unsigned len = modules->length () - pos;
   18509              : 
   18510           60 :   while (len)
   18511              :     {
   18512           60 :       unsigned half = len / 2;
   18513           60 :       module_state *probe = (*modules)[pos + half];
   18514           60 :       if (loc < probe->macro_locs.first)
   18515              :         {
   18516            0 :           pos += half + 1;
   18517            0 :           len = len - (half + 1);
   18518              :         }
   18519           60 :       else if (loc >= probe->macro_locs.first + probe->macro_locs.second)
   18520              :         len = half;
   18521              :       else
   18522              :         return probe;
   18523              :     }
   18524              : 
   18525              :   return NULL;
   18526              : }
   18527              : 
   18528              : location_t
   18529         1277 : module_state::imported_from () const
   18530              : {
   18531         1277 :   location_t from = loc;
   18532         1277 :   line_map_ordinary const *fmap
   18533         1277 :     = linemap_check_ordinary (linemap_lookup (line_table, from));
   18534              : 
   18535         1277 :   if (MAP_MODULE_P (fmap))
   18536         1277 :     from = linemap_included_from (fmap);
   18537              : 
   18538         1277 :   return from;
   18539              : }
   18540              : 
   18541              : /* Note that LOC will need writing.  This allows us to prune locations
   18542              :    that are not needed.  */
   18543              : 
   18544              : bool
   18545     18756247 : module_state::note_location (location_t loc)
   18546              : {
   18547     18756247 :   bool added = false;
   18548     18756247 :   if (!macro_loc_table && !ord_loc_table)
   18549              :     ;
   18550     18756247 :   else if (loc < RESERVED_LOCATION_COUNT)
   18551              :     ;
   18552     17065457 :   else if (IS_ADHOC_LOC (loc))
   18553              :     {
   18554      2063228 :       location_t locus = get_location_from_adhoc_loc (line_table, loc);
   18555      2063228 :       note_location (locus);
   18556      2063228 :       source_range range = get_range_from_loc (line_table, loc);
   18557      2063228 :       if (range.m_start != locus)
   18558      1997022 :         note_location (range.m_start);
   18559      2063228 :       note_location (range.m_finish);
   18560              :     }
   18561     15002229 :   else if (loc >= LINEMAPS_MACRO_LOWEST_LOCATION (line_table))
   18562              :     {
   18563      1175475 :       if (spans.macro (loc))
   18564              :         {
   18565      1175415 :           const line_map *map = linemap_lookup (line_table, loc);
   18566      1175415 :           const line_map_macro *mac_map = linemap_check_macro (map);
   18567      1175415 :           hashval_t hv = macro_loc_traits::hash (mac_map);
   18568      1175415 :           macro_loc_info *slot
   18569      1175415 :             = macro_loc_table->find_slot_with_hash (mac_map, hv, INSERT);
   18570      1175415 :           if (!slot->src)
   18571              :             {
   18572       134294 :               slot->src = mac_map;
   18573       134294 :               slot->remap = 0;
   18574              :               // Expansion locations could themselves be from a
   18575              :               // macro, we need to note them all.
   18576       134294 :               note_location (mac_map->m_expansion);
   18577       134294 :               gcc_checking_assert (mac_map->n_tokens);
   18578       134294 :               location_t tloc = UNKNOWN_LOCATION;
   18579      4805402 :               for (unsigned ix = mac_map->n_tokens * 2; ix--;)
   18580      4671108 :                 if (mac_map->macro_locations[ix] != tloc)
   18581              :                   {
   18582      2474476 :                     tloc = mac_map->macro_locations[ix];
   18583      2474476 :                     note_location (tloc);
   18584              :                   }
   18585              :               added = true;
   18586              :             }
   18587              :         }
   18588              :     }
   18589     13826754 :   else if (IS_ORDINARY_LOC (loc))
   18590              :     {
   18591     13826754 :       if (spans.ordinary (loc))
   18592              :         {
   18593     13818137 :           const line_map *map = linemap_lookup (line_table, loc);
   18594     13818137 :           const line_map_ordinary *ord_map = linemap_check_ordinary (map);
   18595     13818137 :           ord_loc_info lkup;
   18596     13818137 :           lkup.src = ord_map;
   18597     13818137 :           lkup.span = loc_one << ord_map->m_column_and_range_bits;
   18598     13818137 :           lkup.offset = (loc - MAP_START_LOCATION (ord_map)) & ~(lkup.span - 1);
   18599     13818137 :           lkup.remap = 0;
   18600     13818137 :           ord_loc_info *slot = (ord_loc_table->find_slot_with_hash
   18601     13818137 :                                 (lkup, ord_loc_traits::hash (lkup), INSERT));
   18602     13818137 :           if (!slot->src)
   18603              :             {
   18604      1657306 :               *slot = lkup;
   18605      1657306 :               added = true;
   18606              :             }
   18607              :         }
   18608              :     }
   18609              :   else
   18610            0 :     gcc_unreachable ();
   18611     18756247 :   return added;
   18612              : }
   18613              : 
   18614              : /* If we're not streaming, record that we need location LOC.
   18615              :    Otherwise stream it.  */
   18616              : 
   18617              : void
   18618     27895577 : module_state::write_location (bytes_out &sec, location_t loc)
   18619              : {
   18620     27895577 :   if (!sec.streaming_p ())
   18621              :     {
   18622      9754909 :       note_location (loc);
   18623      9754909 :       return;
   18624              :     }
   18625              : 
   18626     18140668 :   if (loc < RESERVED_LOCATION_COUNT)
   18627              :     {
   18628      1732486 :       dump (dumper::LOCATION) && dump ("Reserved location %K", loc);
   18629      1732468 :       sec.loc (LK_RESERVED + loc);
   18630              :     }
   18631     16408200 :   else if (IS_ADHOC_LOC (loc))
   18632              :     {
   18633      2019006 :       dump (dumper::LOCATION) && dump ("Adhoc location");
   18634      2019003 :       sec.u (LK_ADHOC);
   18635      2019003 :       location_t locus = get_location_from_adhoc_loc (line_table, loc);
   18636      2019003 :       write_location (sec, locus);
   18637      2019003 :       source_range range = get_range_from_loc (line_table, loc);
   18638      2019003 :       if (range.m_start == locus)
   18639              :         /* Compress.  */
   18640        62458 :         range.m_start = UNKNOWN_LOCATION;
   18641      2019003 :       write_location (sec, range.m_start);
   18642      2019003 :       write_location (sec, range.m_finish);
   18643      2019003 :       unsigned discriminator = get_discriminator_from_adhoc_loc (line_table, loc);
   18644      2019003 :       sec.u (discriminator);
   18645              :     }
   18646     14389197 :   else if (loc >= LINEMAPS_MACRO_LOWEST_LOCATION (line_table))
   18647              :     {
   18648      1170192 :       const macro_loc_info *info = nullptr;
   18649      1170192 :       line_map_uint_t offset = 0;
   18650      1170192 :       if (unsigned hwm = macro_loc_remap->length ())
   18651              :         {
   18652      1170141 :           info = macro_loc_remap->begin ();
   18653     17411975 :           while (hwm != 1)
   18654              :             {
   18655     15071693 :               unsigned mid = hwm / 2;
   18656     15071693 :               if (MAP_START_LOCATION (info[mid].src) <= loc)
   18657              :                 {
   18658      7619313 :                   info += mid;
   18659      7619313 :                   hwm -= mid;
   18660              :                 }
   18661              :               else
   18662              :                 hwm = mid;
   18663              :             }
   18664      1170141 :           offset = loc - MAP_START_LOCATION (info->src);
   18665      1170141 :           if (offset > info->src->n_tokens)
   18666            9 :             info = nullptr;
   18667              :         }
   18668              : 
   18669      1170192 :       gcc_checking_assert (bool (info) == bool (spans.macro (loc)));
   18670              : 
   18671      1170192 :       if (info)
   18672              :         {
   18673      1170132 :           offset += info->remap;
   18674      1170132 :           sec.u (LK_MACRO);
   18675      1170132 :           sec.loc (offset);
   18676      1170132 :           dump (dumper::LOCATION)
   18677            9 :             && dump ("Macro location %K output %K", loc, offset);
   18678              :         }
   18679           60 :       else if (const module_state *import = module_for_macro_loc (loc))
   18680              :         {
   18681           60 :           auto off = loc - import->macro_locs.first;
   18682           60 :           sec.u (LK_IMPORT_MACRO);
   18683           60 :           sec.u (import->remap);
   18684           60 :           sec.loc (off);
   18685           60 :           dump (dumper::LOCATION)
   18686            0 :             && dump ("Imported macro location %K output %u:%K",
   18687            0 :                      loc, import->remap, off);
   18688              :         }
   18689              :       else
   18690            0 :         gcc_unreachable ();
   18691              :     }
   18692     13219005 :   else if (IS_ORDINARY_LOC (loc))
   18693              :     {
   18694              :       /* If we ran out of locations for imported decls, this location could
   18695              :          be a module unit's location.  In that case, remap the location
   18696              :          to be where we imported the module from.  */
   18697     13219005 :       if (spans.locations_exhausted_p () || CHECKING_P)
   18698              :         {
   18699     13219005 :           const line_map_ordinary *map
   18700     13219005 :             = linemap_check_ordinary (linemap_lookup (line_table, loc));
   18701     13219005 :           if (MAP_MODULE_P (map) && loc == MAP_START_LOCATION (map))
   18702              :             {
   18703            0 :               gcc_checking_assert (spans.locations_exhausted_p ());
   18704            0 :               write_location (sec, linemap_included_from (map));
   18705            0 :               return;
   18706              :             }
   18707              :         }
   18708              : 
   18709     13219005 :       const ord_loc_info *info = nullptr;
   18710     13219005 :       line_map_uint_t offset = 0;
   18711     13219005 :       if (line_map_uint_t hwm = ord_loc_remap->length ())
   18712              :         {
   18713     13219005 :           info = ord_loc_remap->begin ();
   18714    193695609 :           while (hwm != 1)
   18715              :             {
   18716    167257599 :               auto mid = hwm / 2;
   18717    167257599 :               if (MAP_START_LOCATION (info[mid].src) + info[mid].offset <= loc)
   18718              :                 {
   18719     87693790 :                   info += mid;
   18720     87693790 :                   hwm -= mid;
   18721              :                 }
   18722              :               else
   18723              :                 hwm = mid;
   18724              :             }
   18725     13219005 :           offset = loc - MAP_START_LOCATION (info->src) - info->offset;
   18726     13219005 :           if (offset > info->span)
   18727         8604 :             info = nullptr;
   18728              :         }
   18729              : 
   18730     13219005 :       gcc_checking_assert (bool (info) == bool (spans.ordinary (loc)));
   18731              : 
   18732     13219005 :       if (info)
   18733              :         {
   18734     13210401 :           offset += info->remap;
   18735     13210401 :           sec.u (LK_ORDINARY);
   18736     13210401 :           sec.loc (offset);
   18737              : 
   18738     13210401 :           dump (dumper::LOCATION)
   18739           78 :             && dump ("Ordinary location %K output %K", loc, offset);
   18740              :         }
   18741         8604 :       else if (const module_state *import = module_for_ordinary_loc (loc))
   18742              :         {
   18743         8604 :           auto off = loc - import->ordinary_locs.first;
   18744         8604 :           sec.u (LK_IMPORT_ORDINARY);
   18745         8604 :           sec.u (import->remap);
   18746         8604 :           sec.loc (off);
   18747         8604 :           dump (dumper::LOCATION)
   18748            0 :             && dump ("Imported ordinary location %K output %u:%K",
   18749            0 :                      loc, import->remap, off);
   18750              :         }
   18751              :       else
   18752            0 :         gcc_unreachable ();
   18753              :     }
   18754              :   else
   18755            0 :     gcc_unreachable ();
   18756              : }
   18757              : 
   18758              : location_t
   18759     19553967 : module_state::read_location (bytes_in &sec) const
   18760              : {
   18761     19553967 :   location_t locus = UNKNOWN_LOCATION;
   18762     19553967 :   unsigned kind = sec.u ();
   18763     19553967 :   switch (kind)
   18764              :      {
   18765      1827578 :     default:
   18766      1827578 :       {
   18767      1827578 :         if (kind < LK_RESERVED + RESERVED_LOCATION_COUNT)
   18768      1827578 :           locus = location_t (kind - LK_RESERVED);
   18769              :         else
   18770            0 :           sec.set_overrun ();
   18771      1827578 :         dump (dumper::LOCATION)
   18772            0 :           && dump ("Reserved location %K", locus);
   18773              :       }
   18774              :       break;
   18775              : 
   18776      2085527 :      case LK_ADHOC:
   18777      2085527 :       {
   18778      2085527 :         dump (dumper::LOCATION) && dump ("Adhoc location");
   18779      2085527 :         locus = read_location (sec);
   18780      2085527 :         source_range range;
   18781      2085527 :         range.m_start = read_location (sec);
   18782      2085527 :         if (range.m_start == UNKNOWN_LOCATION)
   18783        62991 :           range.m_start = locus;
   18784      2085527 :         range.m_finish = read_location (sec);
   18785      2085527 :         unsigned discriminator = sec.u ();
   18786      2085527 :         if (locus != loc && range.m_start != loc && range.m_finish != loc)
   18787      2085527 :           locus = line_table->get_or_create_combined_loc (locus, range,
   18788              :                                                           nullptr, discriminator);
   18789              :       }
   18790              :       break;
   18791              : 
   18792      1479333 :     case LK_MACRO:
   18793      1479333 :       {
   18794      1479333 :         auto off = sec.loc ();
   18795              : 
   18796      1479333 :         if (macro_locs.second)
   18797              :           {
   18798      1479333 :             if (off < macro_locs.second)
   18799      1479333 :               locus = off + macro_locs.first;
   18800              :             else
   18801            0 :               sec.set_overrun ();
   18802              :           }
   18803              :         else
   18804            0 :           locus = loc;
   18805      1479333 :         dump (dumper::LOCATION)
   18806            0 :           && dump ("Macro %K becoming %K", off, locus);
   18807              :       }
   18808              :       break;
   18809              : 
   18810     14156611 :     case LK_ORDINARY:
   18811     14156611 :       {
   18812     14156611 :         auto off = sec.loc ();
   18813     14156611 :         if (ordinary_locs.second)
   18814              :           {
   18815     14156611 :             if (off < ordinary_locs.second)
   18816     14156611 :               locus = off + ordinary_locs.first;
   18817              :             else
   18818            0 :               sec.set_overrun ();
   18819              :           }
   18820              :         else
   18821            0 :           locus = loc;
   18822              : 
   18823     14156611 :         dump (dumper::LOCATION)
   18824            0 :           && dump ("Ordinary location %K becoming %K", off, locus);
   18825              :       }
   18826              :       break;
   18827              : 
   18828         4918 :      case LK_IMPORT_MACRO:
   18829         4918 :      case LK_IMPORT_ORDINARY:
   18830         4918 :        {
   18831         4918 :          unsigned mod = sec.u ();
   18832         4918 :          location_t off = sec.loc ();
   18833         4918 :          const module_state *import = NULL;
   18834              : 
   18835         4918 :          if (!mod && !slurp->remap)
   18836              :            /* This is an early read of a partition location during the
   18837              :               read of our ordinary location map.  */
   18838              :            import = this;
   18839              :          else
   18840              :            {
   18841         4918 :              mod = slurp->remap_module (mod);
   18842         4918 :              if (!mod)
   18843            0 :                sec.set_overrun ();
   18844              :              else
   18845         4918 :                import = (*modules)[mod];
   18846              :            }
   18847              : 
   18848         4918 :          if (import)
   18849              :            {
   18850         4918 :              if (kind == LK_IMPORT_MACRO)
   18851              :                {
   18852           69 :                  if (!import->macro_locs.second)
   18853            0 :                    locus = import->loc;
   18854           69 :                  else if (off < import->macro_locs.second)
   18855           69 :                    locus = off + import->macro_locs.first;
   18856              :                  else
   18857            0 :                    sec.set_overrun ();
   18858              :                }
   18859              :              else
   18860              :                {
   18861         4849 :                  if (!import->ordinary_locs.second)
   18862            0 :                    locus = import->loc;
   18863         4849 :                  else if (off < import->ordinary_locs.second)
   18864         4849 :                    locus = import->ordinary_locs.first + off;
   18865              :                  else
   18866            0 :                    sec.set_overrun ();
   18867              :                }
   18868              :            }
   18869              :        }
   18870              :        break;
   18871              :     }
   18872              : 
   18873     19553967 :   return locus;
   18874              : }
   18875              : 
   18876              : /* Allocate hash tables to record needed locations.  */
   18877              : 
   18878              : void
   18879         2714 : module_state::write_init_maps ()
   18880              : {
   18881         2714 :   macro_loc_table = new hash_table<macro_loc_traits> (EXPERIMENT (1, 400));
   18882         2714 :   ord_loc_table = new hash_table<ord_loc_traits> (EXPERIMENT (1, 400));
   18883         2714 : }
   18884              : 
   18885              : /* Prepare the span adjustments.  We prune unneeded locations -- at
   18886              :    this point every needed location must have been seen by
   18887              :    note_location.  */
   18888              : 
   18889              : range_t
   18890         2685 : module_state::write_prepare_maps (module_state_config *cfg, bool has_partitions)
   18891              : {
   18892         2982 :   dump () && dump ("Preparing locations");
   18893         2685 :   dump.indent ();
   18894              : 
   18895         2982 :   dump () && dump ("Reserved locations [%K,%K) macro [%K,%K)",
   18896          297 :                    spans[loc_spans::SPAN_RESERVED].ordinary.first,
   18897          297 :                    spans[loc_spans::SPAN_RESERVED].ordinary.second,
   18898          297 :                    spans[loc_spans::SPAN_RESERVED].macro.first,
   18899          297 :                    spans[loc_spans::SPAN_RESERVED].macro.second);
   18900              : 
   18901         2685 :   range_t info {0, 0};
   18902              : 
   18903              :   // Sort the noted lines.
   18904         2685 :   vec_alloc (ord_loc_remap, ord_loc_table->size ());
   18905         2685 :   for (auto iter = ord_loc_table->begin (), end = ord_loc_table->end ();
   18906      3294279 :        iter != end; ++iter)
   18907      1645797 :     ord_loc_remap->quick_push (*iter);
   18908         2685 :   ord_loc_remap->qsort (&ord_loc_info::compare);
   18909              : 
   18910              :   // Note included-from maps.
   18911         2685 :   bool added = false;
   18912         2685 :   const line_map_ordinary *current = nullptr;
   18913      1653852 :   for (auto iter = ord_loc_remap->begin (), end = ord_loc_remap->end ();
   18914      1648482 :        iter != end; ++iter)
   18915      1645797 :     if (iter->src != current)
   18916              :       {
   18917        28566 :         current = iter->src;
   18918        11027 :         for (auto probe = current;
   18919        28566 :              auto from = linemap_included_from (probe);
   18920        11027 :              probe = linemap_check_ordinary (linemap_lookup (line_table, from)))
   18921              :           {
   18922        25543 :             if (has_partitions)
   18923              :               {
   18924              :                 // Partition locations need to elide their module map
   18925              :                 // entry.
   18926          211 :                 probe
   18927          211 :                   = linemap_check_ordinary (linemap_lookup (line_table, from));
   18928          211 :                 if (MAP_MODULE_P (probe))
   18929          181 :                   from = linemap_included_from (probe);
   18930              :               }
   18931              : 
   18932        25543 :             if (!note_location (from))
   18933              :               break;
   18934        11027 :             added = true;
   18935        11027 :           }
   18936              :       }
   18937         2685 :   if (added)
   18938              :     {
   18939              :       // Reconstruct the line array as we added items to the hash table.
   18940          470 :       vec_free (ord_loc_remap);
   18941          470 :       vec_alloc (ord_loc_remap, ord_loc_table->size ());
   18942          470 :       for (auto iter = ord_loc_table->begin (), end = ord_loc_table->end ();
   18943      3290620 :            iter != end; ++iter)
   18944      1645075 :         ord_loc_remap->quick_push (*iter);
   18945          470 :       ord_loc_remap->qsort (&ord_loc_info::compare);
   18946              :     }
   18947         2685 :   delete ord_loc_table;
   18948         2685 :   ord_loc_table = nullptr;
   18949              : 
   18950              :   // Merge (sufficiently) adjacent spans, and calculate remapping.
   18951         2685 :   constexpr line_map_uint_t adjacency = 2; // Allow 2 missing lines.
   18952         5370 :   auto begin = ord_loc_remap->begin (), end = ord_loc_remap->end ();
   18953         2685 :   auto dst = begin;
   18954         2685 :   line_map_uint_t offset = 0;
   18955         2685 :   unsigned range_bits = 0;
   18956         2685 :   ord_loc_info *base = nullptr;
   18957      1659509 :   for (auto iter = begin; iter != end; ++iter)
   18958              :     {
   18959      1656824 :       if (base && iter->src == base->src)
   18960              :         {
   18961      3085250 :           if (base->offset + base->span +
   18962      1632632 :               ((adjacency << base->src->m_column_and_range_bits)
   18963              :                // If there are few c&r bits, allow further separation.
   18964      1632632 :                | (adjacency << 4))
   18965      1632632 :               >= iter->offset)
   18966              :             {
   18967              :               // Merge.
   18968      1452618 :               offset -= base->span;
   18969      1452618 :               base->span = iter->offset + iter->span - base->offset;
   18970      1452618 :               offset += base->span;
   18971      1452618 :               continue;
   18972              :             }
   18973              :         }
   18974        24192 :       else if (range_bits < iter->src->m_range_bits)
   18975         2589 :         range_bits = iter->src->m_range_bits;
   18976              : 
   18977       204206 :       offset += ((loc_one << iter->src->m_range_bits) - 1);
   18978       204206 :       offset &= ~((loc_one << iter->src->m_range_bits) - 1);
   18979       204206 :       iter->remap = offset;
   18980       204206 :       offset += iter->span;
   18981       204206 :       base = dst;
   18982       204206 :       *dst++ = *iter;
   18983              :     }
   18984         2685 :   ord_loc_remap->truncate (dst - begin);
   18985              : 
   18986         2685 :   info.first = ord_loc_remap->length ();
   18987         2685 :   cfg->ordinary_locs = offset;
   18988         2685 :   cfg->loc_range_bits = range_bits;
   18989         2982 :   dump () && dump ("Ordinary maps:%K locs:%K range_bits:%u",
   18990              :                    info.first,
   18991              :                    cfg->ordinary_locs,
   18992              :                    cfg->loc_range_bits);
   18993              : 
   18994              :   // Remap the macro locations.
   18995         2685 :   vec_alloc (macro_loc_remap, macro_loc_table->size ());
   18996         2685 :   for (auto iter = macro_loc_table->begin (), end = macro_loc_table->end ();
   18997       271273 :        iter != end; ++iter)
   18998       134294 :     macro_loc_remap->quick_push (*iter);
   18999         2685 :   delete macro_loc_table;
   19000         2685 :   macro_loc_table = nullptr;
   19001              : 
   19002         2685 :   macro_loc_remap->qsort (&macro_loc_info::compare);
   19003         2685 :   offset = 0;
   19004         8055 :   for (auto iter = macro_loc_remap->begin (), end = macro_loc_remap->end ();
   19005       136979 :        iter != end; ++iter)
   19006              :     {
   19007       134294 :       auto mac = iter->src;
   19008       134294 :       iter->remap = offset;
   19009       134294 :       offset += mac->n_tokens;
   19010              :     }
   19011         2685 :   info.second = macro_loc_remap->length ();
   19012         2685 :   cfg->macro_locs = offset;
   19013              : 
   19014         2982 :   dump () && dump ("Macro maps:%K locs:%K", info.second, cfg->macro_locs);
   19015              : 
   19016         2685 :   dump.outdent ();
   19017              : 
   19018              :   // If we have no ordinary locs, we must also have no macro locs.
   19019         2685 :   gcc_checking_assert (cfg->ordinary_locs || !cfg->macro_locs);
   19020              : 
   19021         2685 :   return info;
   19022              : }
   19023              : 
   19024              : bool
   19025         2932 : module_state::read_prepare_maps (const module_state_config *cfg)
   19026              : {
   19027         2932 :   location_t ordinary = line_table->highest_location + 1;
   19028         2932 :   ordinary += cfg->ordinary_locs;
   19029              : 
   19030         2932 :   location_t macro = LINEMAPS_MACRO_LOWEST_LOCATION (line_table);
   19031         2932 :   macro -= cfg->macro_locs;
   19032              : 
   19033         2932 :   if (ordinary < LINE_MAP_MAX_LOCATION_WITH_COLS
   19034         2932 :       && macro >= LINE_MAP_MAX_LOCATION)
   19035              :     /* OK, we have enough locations.  */
   19036              :     return true;
   19037              : 
   19038            0 :   ordinary_locs.first = ordinary_locs.second = 0;
   19039            0 :   macro_locs.first = macro_locs.second = 0;
   19040              : 
   19041            0 :   spans.report_location_exhaustion (loc);
   19042              : 
   19043              :   return false;
   19044              : }
   19045              : 
   19046              : /* Write & read the location maps. Not called if there are no
   19047              :    locations.   */
   19048              : 
   19049              : void
   19050         2589 : module_state::write_ordinary_maps (elf_out *to, range_t &info,
   19051              :                                    bool has_partitions, unsigned *crc_p)
   19052              : {
   19053         2864 :   dump () && dump ("Writing ordinary location maps");
   19054         2589 :   dump.indent ();
   19055              : 
   19056         2589 :   vec<const char *> filenames;
   19057         2589 :   filenames.create (20);
   19058              : 
   19059              :   /* Determine the unique filenames.  */
   19060         2589 :   const line_map_ordinary *current = nullptr;
   19061       211973 :   for (auto iter = ord_loc_remap->begin (), end = ord_loc_remap->end ();
   19062       206795 :        iter != end; ++iter)
   19063       204206 :     if (iter->src != current)
   19064              :       {
   19065        24192 :         current = iter->src;
   19066        24192 :         const char *fname = ORDINARY_MAP_FILE_NAME (iter->src);
   19067              : 
   19068              :         /* We should never find a module linemap in an interval.  */
   19069        24192 :         gcc_checking_assert (!MAP_MODULE_P (iter->src));
   19070              : 
   19071              :         /* We expect very few filenames, so just an array.
   19072              :            (Not true when headers are still in play :()  */
   19073      1557702 :         for (unsigned jx = filenames.length (); jx--;)
   19074              :           {
   19075      1520675 :             const char *name = filenames[jx];
   19076      1520675 :             if (0 == strcmp (name, fname))
   19077              :               {
   19078              :                 /* Reset the linemap's name, because for things like
   19079              :                    preprocessed input we could have multiple instances
   19080              :                    of the same name, and we'd rather not percolate
   19081              :                    that.  */
   19082        11357 :                 const_cast<line_map_ordinary *> (iter->src)->to_file = name;
   19083        11357 :                 fname = NULL;
   19084        11357 :                 break;
   19085              :               }
   19086              :           }
   19087        24192 :         if (fname)
   19088        12835 :           filenames.safe_push (fname);
   19089              :       }
   19090              : 
   19091         2589 :   bytes_out sec (to);
   19092         2589 :   sec.begin ();
   19093              : 
   19094              :   /* Write the filenames.  */
   19095         2589 :   unsigned len = filenames.length ();
   19096         2589 :   sec.u (len);
   19097         2864 :   dump () && dump ("%u source file names", len);
   19098        15424 :   for (unsigned ix = 0; ix != len; ix++)
   19099              :     {
   19100        12835 :       const char *fname = filenames[ix];
   19101        12850 :       dump (dumper::LOCATION) && dump ("Source file[%u]=%s", ix, fname);
   19102        12835 :       sec.str (fname);
   19103              :     }
   19104              : 
   19105         2589 :   sec.loc (info.first); /* Num maps.  */
   19106         2589 :   const ord_loc_info *base = nullptr;
   19107       211973 :   for (auto iter = ord_loc_remap->begin (), end = ord_loc_remap->end ();
   19108       206795 :        iter != end; ++iter)
   19109              :     {
   19110       204206 :       dump (dumper::LOCATION)
   19111           36 :         && dump ("Span:%K ordinary [%K+%K,+%K)->[%K,+%K)",
   19112           36 :                  (location_t) (iter - ord_loc_remap->begin ()),
   19113           18 :                  MAP_START_LOCATION (iter->src),
   19114              :                  iter->offset, iter->span, iter->remap,
   19115              :                  iter->span);
   19116              : 
   19117       204206 :       if (!base || iter->src != base->src)
   19118        24192 :         base = iter;
   19119       204206 :       sec.loc (iter->offset - base->offset);
   19120       204206 :       if (base == iter)
   19121              :         {
   19122        24192 :           sec.u (iter->src->sysp);
   19123        24192 :           sec.u (iter->src->m_range_bits);
   19124        24192 :           sec.u (iter->src->m_column_and_range_bits - iter->src->m_range_bits);
   19125              : 
   19126        24192 :           const char *fname = ORDINARY_MAP_FILE_NAME (iter->src);
   19127      4876812 :           for (unsigned ix = 0; ix != filenames.length (); ix++)
   19128      2438406 :             if (filenames[ix] == fname)
   19129              :               {
   19130        24192 :                 sec.u (ix);
   19131        24192 :                 break;
   19132              :               }
   19133        24192 :           unsigned line = ORDINARY_MAP_STARTING_LINE_NUMBER (iter->src);
   19134        24192 :           line += iter->offset >> iter->src->m_column_and_range_bits;
   19135        24192 :           sec.u (line);
   19136              :         }
   19137       204206 :       sec.loc (iter->remap);
   19138       204206 :       if (base == iter)
   19139              :         {
   19140              :           /* Write the included from location, which means reading it
   19141              :              while reading in the ordinary maps.  So we'd better not
   19142              :              be getting ahead of ourselves.  */
   19143        24192 :           location_t from = linemap_included_from (iter->src);
   19144        24192 :           gcc_checking_assert (from < MAP_START_LOCATION (iter->src));
   19145        24192 :           if (from != UNKNOWN_LOCATION && has_partitions)
   19146              :             {
   19147              :               /* A partition's span will have a from pointing at a
   19148              :                  MODULE_INC.  Find that map's from.  */
   19149          205 :               line_map_ordinary const *fmap
   19150          205 :                 = linemap_check_ordinary (linemap_lookup (line_table, from));
   19151          205 :               if (MAP_MODULE_P (fmap))
   19152          175 :                 from = linemap_included_from (fmap);
   19153              :             }
   19154        24192 :           write_location (sec, from);
   19155              :         }
   19156              :     }
   19157              : 
   19158         2589 :   filenames.release ();
   19159              : 
   19160         2589 :   sec.end (to, to->name (MOD_SNAME_PFX ".olm"), crc_p);
   19161         2589 :   dump.outdent ();
   19162         2589 : }
   19163              : 
   19164              : /* Return the prefix to use for dumping a #pragma diagnostic change to DK.  */
   19165              : 
   19166              : static const char *
   19167          992 : dk_string (enum diagnostics::kind dk)
   19168              : {
   19169          992 :   gcc_assert (dk > diagnostics::kind::unspecified
   19170              :               && dk < diagnostics::kind::last_diagnostic_kind);
   19171          992 :   if (dk == diagnostics::kind::ignored)
   19172              :     /* diagnostics/kinds.def has an empty string for ignored.  */
   19173              :     return "ignored: ";
   19174              :   else
   19175            0 :     return diagnostics::get_text_for_kind (dk);
   19176              : }
   19177              : 
   19178              : /* Dump one #pragma GCC diagnostic entry.  */
   19179              : 
   19180              : static bool
   19181         2020 : dump_dc_change (unsigned index, unsigned opt, enum diagnostics::kind dk)
   19182              : {
   19183         2020 :   if (dk == diagnostics::kind::pop)
   19184         1028 :     return dump (" Index %u: pop from %d", index, opt);
   19185              :   else
   19186          992 :     return dump (" Index %u: %s%s", index, dk_string (dk),
   19187         1984 :                  cl_options[opt].opt_text);
   19188              : }
   19189              : 
   19190              : /* Write out any #pragma GCC diagnostic info to the .dgc section.  */
   19191              : 
   19192              : void
   19193         5274 : module_state::write_diagnostic_classification (elf_out *to,
   19194              :                                                diagnostics::context *dc,
   19195              :                                                unsigned *crc_p)
   19196              : {
   19197         5274 :   auto &changes = dc->get_classification_history ();
   19198              : 
   19199         5274 :   bytes_out sec (to);
   19200         5274 :   if (sec.streaming_p ())
   19201              :     {
   19202         2589 :       sec.begin ();
   19203         2864 :       dump () && dump ("Writing diagnostic change locations");
   19204         2589 :       dump.indent ();
   19205              :     }
   19206              : 
   19207         5274 :   unsigned len = changes.length ();
   19208              : 
   19209              :   /* We don't want to write out any entries that came from one of our imports.
   19210              :      But then we need to adjust the total, and change diagnostics::kind::pop
   19211              :      targets to match the index in our actual output.  So remember how many
   19212              :      lines we had skipped at each step, where -1 means this line itself
   19213              :      is skipped.  */
   19214         5274 :   int skips = 0;
   19215         5274 :   auto_vec<int> skips_at (len);
   19216         5274 :   skips_at.safe_grow (len);
   19217              : 
   19218        53782 :   for (unsigned i = 0; i < len; ++i)
   19219              :     {
   19220        48508 :       const auto &c = changes[i];
   19221        48508 :       skips_at[i] = skips;
   19222        48508 :       if (linemap_location_from_module_p (line_table, c.location))
   19223              :         {
   19224        10226 :           ++skips;
   19225        10226 :           skips_at[i] = -1;
   19226        10226 :           continue;
   19227              :         }
   19228              :     }
   19229              : 
   19230         5274 :   if (sec.streaming_p ())
   19231              :     {
   19232         2589 :       sec.u (len - skips);
   19233         2864 :       dump () && dump ("Diagnostic changes: %u", len - skips);
   19234              :     }
   19235              : 
   19236        53782 :   for (unsigned i = 0; i < len; ++i)
   19237              :     {
   19238        48508 :       if (skips_at[i] == -1)
   19239        10226 :         continue;
   19240              : 
   19241        38282 :       const auto &c = changes[i];
   19242        38282 :       write_location (sec, c.location);
   19243        38282 :       if (sec.streaming_p ())
   19244              :         {
   19245        19141 :           unsigned opt = c.option;
   19246        19141 :           if (c.kind == diagnostics::kind::pop)
   19247         9753 :             opt -= skips_at[opt];
   19248        19141 :           sec.u (opt);
   19249        19141 :           sec.u (static_cast<unsigned> (c.kind));
   19250        50461 :           dump () && dump_dc_change (i - skips_at[i], opt, c.kind);
   19251              :         }
   19252              :     }
   19253              : 
   19254         5274 :   if (sec.streaming_p ())
   19255              :     {
   19256         2589 :       sec.end (to, to->name (MOD_SNAME_PFX ".dgc"), crc_p);
   19257         2589 :       dump.outdent ();
   19258              :     }
   19259         5274 : }
   19260              : 
   19261              : /* Read any #pragma GCC diagnostic info from the .dgc section.  */
   19262              : 
   19263              : bool
   19264         2874 : module_state::read_diagnostic_classification (diagnostics::context *dc)
   19265              : {
   19266         2874 :   bytes_in sec;
   19267              : 
   19268         2874 :   if (!sec.begin (loc, from (), MOD_SNAME_PFX ".dgc"))
   19269              :     return false;
   19270              : 
   19271         3382 :   dump () && dump ("Reading diagnostic change locations");
   19272         2874 :   dump.indent ();
   19273              : 
   19274         2874 :   unsigned len = sec.u ();
   19275         3382 :   dump () && dump ("Diagnostic changes: %u", len);
   19276              : 
   19277         2874 :   auto &changes = dc->get_classification_history ();
   19278         2874 :   int offset = changes.length ();
   19279         2874 :   changes.reserve (len + 1);
   19280        24898 :   for (unsigned i = 0; i < len; ++i)
   19281              :     {
   19282        22024 :       location_t loc = read_location (sec);
   19283        22024 :       int opt = sec.u ();
   19284        22024 :       enum diagnostics::kind kind = (enum diagnostics::kind) sec.u ();
   19285        22024 :       if (kind == diagnostics::kind::pop)
   19286              :         /* For a pop, opt is the 'changes' index to return to.  */
   19287        11267 :         opt += offset;
   19288        22024 :       changes.quick_push ({ loc, opt, kind });
   19289        22091 :       dump () && dump_dc_change (changes.length () - 1, opt, kind);
   19290              :     }
   19291              : 
   19292              :   /* Did the import pop all its diagnostic changes?  */
   19293         2874 :   bool last_was_reset = (len == 0);
   19294         2874 :   if (len)
   19295          224 :     for (int i = changes.length () - 1; ; --i)
   19296              :       {
   19297         9679 :         gcc_checking_assert (i >= offset);
   19298              : 
   19299         9679 :         const auto &c = changes[i];
   19300         9679 :         if (c.kind != diagnostics::kind::pop)
   19301              :           break;
   19302         9670 :         else if (c.option == offset)
   19303              :           {
   19304              :             last_was_reset = true;
   19305              :             break;
   19306              :           }
   19307              :         else
   19308              :           /* As in update_effective_level_from_pragmas, the loop will decrement
   19309              :              i so we actually jump to c.option - 1.  */
   19310         9567 :           i = c.option;
   19311         9567 :       }
   19312         2874 :   if (!last_was_reset)
   19313              :     {
   19314              :       /* It didn't, so add a pop at its last location to avoid affecting later
   19315              :          imports.  */
   19316            9 :       location_t last_loc = ordinary_locs.first + ordinary_locs.second - 1;
   19317            9 :       changes.quick_push ({ last_loc, offset, diagnostics::kind::pop });
   19318           15 :       dump () && dump (" Adding final pop from index %d", offset);
   19319              :     }
   19320              : 
   19321         2874 :   dump.outdent ();
   19322         2874 :   if (!sec.end (from ()))
   19323              :     return false;
   19324              : 
   19325              :   return true;
   19326         2874 : }
   19327              : 
   19328              : void
   19329          118 : module_state::write_macro_maps (elf_out *to, range_t &info, unsigned *crc_p)
   19330              : {
   19331          130 :   dump () && dump ("Writing macro location maps");
   19332          118 :   dump.indent ();
   19333              : 
   19334          118 :   bytes_out sec (to);
   19335          118 :   sec.begin ();
   19336              : 
   19337          130 :   dump () && dump ("Macro maps:%K", info.second);
   19338          118 :   sec.loc (info.second);
   19339              : 
   19340          118 :   line_map_uint_t macro_num = 0;
   19341          236 :   for (auto iter = macro_loc_remap->end (), begin = macro_loc_remap->begin ();
   19342       134412 :        iter-- != begin;)
   19343              :     {
   19344       134294 :       auto mac = iter->src;
   19345       134294 :       sec.loc (iter->remap);
   19346       134294 :       sec.u (mac->n_tokens);
   19347       134294 :       sec.cpp_node (mac->macro);
   19348       134294 :       write_location (sec, mac->m_expansion);
   19349       134294 :       const location_t *locs = mac->macro_locations;
   19350              :       /* There are lots of identical runs.  */
   19351       134294 :       location_t prev = UNKNOWN_LOCATION;
   19352       134294 :       unsigned count = 0;
   19353       134294 :       unsigned runs = 0;
   19354      4805402 :       for (unsigned jx = mac->n_tokens * 2; jx--;)
   19355              :         {
   19356      4671108 :           location_t tok_loc = locs[jx];
   19357      4671108 :           if (tok_loc == prev)
   19358              :             {
   19359      2196632 :               count++;
   19360      2196632 :               continue;
   19361              :             }
   19362      2474476 :           runs++;
   19363      2474476 :           sec.u (count);
   19364      2474476 :           count = 1;
   19365      2474476 :           prev = tok_loc;
   19366      2474476 :           write_location (sec, tok_loc);
   19367              :         }
   19368       134294 :       sec.u (count);
   19369       134294 :       dump (dumper::LOCATION)
   19370            9 :         && dump ("Macro:%K %I %u/%u*2 locations [%K,%K)->%K",
   19371            9 :                  macro_num, identifier (mac->macro),
   19372              :                  runs, mac->n_tokens,
   19373              :                  MAP_START_LOCATION (mac),
   19374            9 :                  MAP_START_LOCATION (mac) + mac->n_tokens,
   19375              :                  iter->remap);
   19376       134294 :       macro_num++;
   19377              :     }
   19378          118 :   gcc_assert (macro_num == info.second);
   19379              : 
   19380          118 :   sec.end (to, to->name (MOD_SNAME_PFX ".mlm"), crc_p);
   19381          118 :   dump.outdent ();
   19382          118 : }
   19383              : 
   19384              : bool
   19385         2874 : module_state::read_ordinary_maps (line_map_uint_t num_ord_locs,
   19386              :                                   unsigned range_bits)
   19387              : {
   19388         2874 :   bytes_in sec;
   19389              : 
   19390         2874 :   if (!sec.begin (loc, from (), MOD_SNAME_PFX ".olm"))
   19391              :     return false;
   19392         3382 :   dump () && dump ("Reading ordinary location maps");
   19393         2874 :   dump.indent ();
   19394              : 
   19395              :   /* Read the filename table.  */
   19396         2874 :   unsigned len = sec.u ();
   19397         3382 :   dump () && dump ("%u source file names", len);
   19398         2874 :   vec<const char *> filenames;
   19399         2874 :   filenames.create (len);
   19400        18474 :   for (unsigned ix = 0; ix != len; ix++)
   19401              :     {
   19402        15600 :       size_t l;
   19403        15600 :       const char *buf = sec.str (&l);
   19404        15600 :       char *fname = XNEWVEC (char, l + 1);
   19405        15600 :       memcpy (fname, buf, l + 1);
   19406        15600 :       dump (dumper::LOCATION) && dump ("Source file[%u]=%s", ix, fname);
   19407              :       /* We leak these names into the line-map table.  But it
   19408              :          doesn't own them.  */
   19409        15600 :       filenames.quick_push (fname);
   19410              :     }
   19411              : 
   19412         2874 :   line_map_uint_t num_ordinary = sec.loc ();
   19413         3382 :   dump () && dump ("Ordinary maps:%K, range_bits:%u",
   19414              :                    num_ordinary, range_bits);
   19415              : 
   19416         2874 :   location_t offset = line_table->highest_location + 1;
   19417         2874 :   offset += ((loc_one << range_bits) - 1);
   19418         2874 :   offset &= ~((loc_one << range_bits) - 1);
   19419         2874 :   ordinary_locs.first = offset;
   19420              : 
   19421         2874 :   bool propagated = spans.maybe_propagate (this, offset);
   19422         2874 :   line_map_ordinary *maps = static_cast<line_map_ordinary *>
   19423         2874 :     (line_map_new_raw (line_table, false, num_ordinary));
   19424              : 
   19425         2874 :   const line_map_ordinary *base = nullptr;
   19426       274507 :   for (line_map_uint_t ix = 0; ix != num_ordinary && !sec.get_overrun (); ix++)
   19427              :     {
   19428       271633 :       line_map_ordinary *map = &maps[ix];
   19429              : 
   19430       271633 :       location_t offset = sec.loc ();
   19431       271633 :       if (!offset)
   19432              :         {
   19433        30326 :           map->reason = LC_RENAME;
   19434        30326 :           map->sysp = sec.u ();
   19435        30326 :           map->m_range_bits = sec.u ();
   19436        30326 :           map->m_column_and_range_bits = sec.u () + map->m_range_bits;
   19437        30326 :           unsigned fnum = sec.u ();
   19438        60652 :           map->to_file = (fnum < filenames.length () ? filenames[fnum] : "");
   19439        30326 :           map->to_line = sec.u ();
   19440        30326 :           base = map;
   19441              :         }
   19442              :       else
   19443              :         {
   19444       241307 :           *map = *base;
   19445       241307 :           map->to_line += offset >> map->m_column_and_range_bits;
   19446              :         }
   19447       271633 :       location_t remap = sec.loc ();
   19448       271633 :       map->start_location = remap + ordinary_locs.first;
   19449       271633 :       if (base == map)
   19450              :         {
   19451              :           /* Root the outermost map at our location.  */
   19452        30326 :           ordinary_locs.second = remap;
   19453        30326 :           location_t from = read_location (sec);
   19454        30326 :           map->included_from = from != UNKNOWN_LOCATION ? from : loc;
   19455              :         }
   19456              :     }
   19457              : 
   19458         2874 :   ordinary_locs.second = num_ord_locs;
   19459              :   /* highest_location is the one handed out, not the next one to
   19460              :      hand out.  */
   19461         2874 :   line_table->highest_location = ordinary_locs.first + ordinary_locs.second - 1;
   19462              : 
   19463         2874 :   if (line_table->highest_location >= LINE_MAP_MAX_LOCATION_WITH_COLS)
   19464              :     /* We shouldn't run out of locations, as we checked before
   19465              :        starting.  */
   19466            0 :     sec.set_overrun ();
   19467         3382 :   dump () && dump ("Ordinary location [%K,+%K)",
   19468              :                    ordinary_locs.first, ordinary_locs.second);
   19469              : 
   19470         2874 :   if (propagated)
   19471          160 :     spans.close ();
   19472              : 
   19473         2874 :   filenames.release ();
   19474              : 
   19475         2874 :   dump.outdent ();
   19476         2874 :   if (!sec.end (from ()))
   19477              :     return false;
   19478              : 
   19479              :   return true;
   19480         2874 : }
   19481              : 
   19482              : bool
   19483          129 : module_state::read_macro_maps (line_map_uint_t num_macro_locs)
   19484              : {
   19485          129 :   bytes_in sec;
   19486              : 
   19487          129 :   if (!sec.begin (loc, from (), MOD_SNAME_PFX ".mlm"))
   19488              :     return false;
   19489          138 :   dump () && dump ("Reading macro location maps");
   19490          129 :   dump.indent ();
   19491              : 
   19492          129 :   line_map_uint_t num_macros = sec.loc ();
   19493          138 :   dump () && dump ("Macro maps:%K locs:%K",
   19494              :                    num_macros, num_macro_locs);
   19495              : 
   19496          258 :   bool propagated = spans.maybe_propagate (this,
   19497          129 :                                            line_table->highest_location + 1);
   19498              : 
   19499          129 :   location_t offset = LINEMAPS_MACRO_LOWEST_LOCATION (line_table);
   19500          129 :   macro_locs.second = num_macro_locs;
   19501          129 :   macro_locs.first = offset - num_macro_locs;
   19502              : 
   19503          138 :   dump () && dump ("Macro loc delta %K", offset);
   19504          138 :   dump () && dump ("Macro locations [%K,%K)",
   19505              :                    macro_locs.first, macro_locs.second);
   19506              : 
   19507       192657 :   for (line_map_uint_t ix = 0; ix != num_macros && !sec.get_overrun (); ix++)
   19508              :     {
   19509       192528 :       location_t offset = sec.loc ();
   19510       192528 :       unsigned n_tokens = sec.u ();
   19511       192528 :       cpp_hashnode *node = sec.cpp_node ();
   19512       192528 :       location_t exp_loc = read_location (sec);
   19513              : 
   19514       192528 :       const line_map_macro *macro
   19515       192528 :         = linemap_enter_macro (line_table, node, exp_loc, n_tokens);
   19516       192528 :       if (!macro)
   19517              :         /* We shouldn't run out of locations, as we checked that we
   19518              :            had enough before starting.  */
   19519              :         break;
   19520       192528 :       gcc_checking_assert (MAP_START_LOCATION (macro)
   19521              :                            == offset + macro_locs.first);
   19522              : 
   19523       192528 :       location_t *locs = macro->macro_locations;
   19524       192528 :       location_t tok_loc = UNKNOWN_LOCATION;
   19525       192528 :       unsigned count = sec.u ();
   19526       192528 :       unsigned runs = 0;
   19527      6939268 :       for (unsigned jx = macro->n_tokens * 2; jx-- && !sec.get_overrun ();)
   19528              :         {
   19529     10321022 :           while (!count-- && !sec.get_overrun ())
   19530              :             {
   19531      3574282 :               runs++;
   19532      3574282 :               tok_loc = read_location (sec);
   19533      3574282 :               count = sec.u ();
   19534              :             }
   19535      6746740 :           locs[jx] = tok_loc;
   19536              :         }
   19537       192528 :       if (count)
   19538            0 :         sec.set_overrun ();
   19539       192558 :       dump (dumper::LOCATION)
   19540            0 :         && dump ("Macro:%K %I %u/%u*2 locations [%K,%K)",
   19541              :                  ix, identifier (node), runs, n_tokens,
   19542              :                  MAP_START_LOCATION (macro),
   19543            0 :                  MAP_START_LOCATION (macro) + n_tokens);
   19544              :     }
   19545              : 
   19546          138 :   dump () && dump ("Macro location lwm:%K", macro_locs.first);
   19547          129 :   if (propagated)
   19548            3 :     spans.close ();
   19549              : 
   19550          129 :   dump.outdent ();
   19551          129 :   if (!sec.end (from ()))
   19552              :     return false;
   19553              : 
   19554              :   return true;
   19555          129 : }
   19556              : 
   19557              : /* Serialize the definition of MACRO.  */
   19558              : 
   19559              : void
   19560        73663 : module_state::write_define (bytes_out &sec, const cpp_macro *macro)
   19561              : {
   19562        73663 :   sec.u (macro->count);
   19563              : 
   19564        73663 :   bytes_out::bits_out bits = sec.stream_bits ();
   19565        73663 :   bits.b (macro->fun_like);
   19566        73663 :   bits.b (macro->variadic);
   19567        73663 :   bits.b (macro->syshdr);
   19568        73663 :   bits.bflush ();
   19569              : 
   19570        73663 :   write_location (sec, macro->line);
   19571        73663 :   if (macro->fun_like)
   19572              :     {
   19573         9574 :       sec.u (macro->paramc);
   19574         9574 :       const cpp_hashnode *const *parms = macro->parm.params;
   19575        24117 :       for (unsigned ix = 0; ix != macro->paramc; ix++)
   19576        14543 :         sec.cpp_node (parms[ix]);
   19577              :     }
   19578              : 
   19579              :   unsigned len = 0;
   19580       240143 :   for (unsigned ix = 0; ix != macro->count; ix++)
   19581              :     {
   19582       166480 :       const cpp_token *token = &macro->exp.tokens[ix];
   19583       166480 :       write_location (sec, token->src_loc);
   19584       166480 :       sec.u (token->type);
   19585       166480 :       sec.u (token->flags);
   19586       166480 :       switch (cpp_token_val_index (token))
   19587              :         {
   19588            0 :         default:
   19589            0 :           gcc_unreachable ();
   19590              : 
   19591        12815 :         case CPP_TOKEN_FLD_ARG_NO:
   19592              :           /* An argument reference.  */
   19593        12815 :           sec.u (token->val.macro_arg.arg_no);
   19594        12815 :           sec.cpp_node (token->val.macro_arg.spelling);
   19595        12815 :           break;
   19596              : 
   19597        32960 :         case CPP_TOKEN_FLD_NODE:
   19598              :           /* An identifier.  */
   19599        32960 :           sec.cpp_node (token->val.node.node);
   19600        32960 :           if (token->val.node.spelling == token->val.node.node)
   19601              :             /* The spelling will usually be the same.  so optimize
   19602              :                that.  */
   19603        32960 :             sec.str (NULL, 0);
   19604              :           else
   19605            0 :             sec.cpp_node (token->val.node.spelling);
   19606              :           break;
   19607              : 
   19608              :         case CPP_TOKEN_FLD_NONE:
   19609              :           break;
   19610              : 
   19611        52311 :         case CPP_TOKEN_FLD_STR:
   19612              :           /* A string, number or comment.  Not always NUL terminated,
   19613              :              we stream out in a single contatenation with embedded
   19614              :              NULs as that's a safe default.  */
   19615        52311 :           len += token->val.str.len + 1;
   19616        52311 :           sec.u (token->val.str.len);
   19617        52311 :           break;
   19618              : 
   19619            0 :         case CPP_TOKEN_FLD_SOURCE:
   19620            0 :         case CPP_TOKEN_FLD_TOKEN_NO:
   19621            0 :         case CPP_TOKEN_FLD_PRAGMA:
   19622              :           /* These do not occur inside a macro itself.  */
   19623            0 :           gcc_unreachable ();
   19624              :         }
   19625              :     }
   19626              : 
   19627        73663 :   if (len)
   19628              :     {
   19629        48562 :       char *ptr = reinterpret_cast<char *> (sec.buf (len));
   19630        48562 :       len = 0;
   19631       147713 :       for (unsigned ix = 0; ix != macro->count; ix++)
   19632              :         {
   19633        99151 :           const cpp_token *token = &macro->exp.tokens[ix];
   19634        99151 :           if (cpp_token_val_index (token) == CPP_TOKEN_FLD_STR)
   19635              :             {
   19636        52311 :               memcpy (ptr + len, token->val.str.text,
   19637        52311 :                       token->val.str.len);
   19638        52311 :               len += token->val.str.len;
   19639        52311 :               ptr[len++] = 0;
   19640              :             }
   19641              :         }
   19642              :     }
   19643        73663 : }
   19644              : 
   19645              : /* Read a macro definition.  */
   19646              : 
   19647              : cpp_macro *
   19648          645 : module_state::read_define (bytes_in &sec, cpp_reader *reader) const
   19649              : {
   19650          645 :   unsigned count = sec.u ();
   19651              :   /* We rely on knowing cpp_reader's hash table is ident_hash, and
   19652              :      its subobject allocator is stringpool_ggc_alloc and that is just
   19653              :      a wrapper for ggc_alloc_atomic.  */
   19654          645 :   cpp_macro *macro
   19655         1290 :     = (cpp_macro *)ggc_alloc_atomic (sizeof (cpp_macro)
   19656          645 :                                      + sizeof (cpp_token) * (count - !!count));
   19657          645 :   memset (macro, 0, sizeof (cpp_macro) + sizeof (cpp_token) * (count - !!count));
   19658              : 
   19659          645 :   macro->count = count;
   19660          645 :   macro->kind = cmk_macro;
   19661          645 :   macro->imported_p = true;
   19662              : 
   19663          645 :   bytes_in::bits_in bits = sec.stream_bits ();
   19664          645 :   macro->fun_like = bits.b ();
   19665          645 :   macro->variadic = bits.b ();
   19666          645 :   macro->syshdr = bits.b ();
   19667          645 :   bits.bflush ();
   19668              : 
   19669          645 :   macro->line = read_location (sec);
   19670              : 
   19671          645 :   if (macro->fun_like)
   19672              :     {
   19673           79 :       unsigned paramc = sec.u ();
   19674           79 :       cpp_hashnode **params
   19675           79 :         = (cpp_hashnode **)ggc_alloc_atomic (sizeof (cpp_hashnode *) * paramc);
   19676           79 :       macro->paramc = paramc;
   19677           79 :       macro->parm.params = params;
   19678          169 :       for (unsigned ix = 0; ix != paramc; ix++)
   19679           90 :         params[ix] = sec.cpp_node ();
   19680              :     }
   19681              : 
   19682              :   unsigned len = 0;
   19683         1880 :   for (unsigned ix = 0; ix != count && !sec.get_overrun (); ix++)
   19684              :     {
   19685         1235 :       cpp_token *token = &macro->exp.tokens[ix];
   19686         1235 :       token->src_loc = read_location (sec);
   19687         1235 :       token->type = cpp_ttype (sec.u ());
   19688         1235 :       token->flags = sec.u ();
   19689         1235 :       switch (cpp_token_val_index (token))
   19690              :         {
   19691            0 :         default:
   19692            0 :           sec.set_overrun ();
   19693            0 :           break;
   19694              : 
   19695           73 :         case CPP_TOKEN_FLD_ARG_NO:
   19696              :           /* An argument reference.  */
   19697           73 :           {
   19698           73 :             unsigned arg_no = sec.u ();
   19699           73 :             if (arg_no - 1 >= macro->paramc)
   19700            0 :               sec.set_overrun ();
   19701           73 :             token->val.macro_arg.arg_no = arg_no;
   19702           73 :             token->val.macro_arg.spelling = sec.cpp_node ();
   19703              :           }
   19704           73 :           break;
   19705              : 
   19706          263 :         case CPP_TOKEN_FLD_NODE:
   19707              :           /* An identifier.  */
   19708          263 :           token->val.node.node = sec.cpp_node ();
   19709          263 :           token->val.node.spelling = sec.cpp_node ();
   19710          263 :           if (!token->val.node.spelling)
   19711          263 :             token->val.node.spelling = token->val.node.node;
   19712              :           break;
   19713              : 
   19714              :         case CPP_TOKEN_FLD_NONE:
   19715              :           break;
   19716              : 
   19717          474 :         case CPP_TOKEN_FLD_STR:
   19718              :           /* A string, number or comment.  */
   19719          474 :           token->val.str.len = sec.u ();
   19720          474 :           len += token->val.str.len + 1;
   19721          474 :           break;
   19722              :         }
   19723              :     }
   19724              : 
   19725          645 :   if (len)
   19726          472 :     if (const char *ptr = reinterpret_cast<const char *> (sec.buf (len)))
   19727              :       {
   19728              :         /* There should be a final NUL.  */
   19729          472 :         if (ptr[len-1])
   19730            0 :           sec.set_overrun ();
   19731              :         /* cpp_alloc_token_string will add a final NUL.  */
   19732          472 :         const unsigned char *buf
   19733          472 :           = cpp_alloc_token_string (reader, (const unsigned char *)ptr, len - 1);
   19734          472 :         len = 0;
   19735         1257 :         for (unsigned ix = 0; ix != count && !sec.get_overrun (); ix++)
   19736              :           {
   19737          785 :             cpp_token *token = &macro->exp.tokens[ix];
   19738          785 :             if (cpp_token_val_index (token) == CPP_TOKEN_FLD_STR)
   19739              :               {
   19740          474 :                 token->val.str.text = buf + len;
   19741          474 :                 len += token->val.str.len;
   19742          474 :                 if (buf[len++])
   19743            0 :                   sec.set_overrun ();
   19744              :               }
   19745              :           }
   19746              :       }
   19747              : 
   19748          645 :   if (sec.get_overrun ())
   19749            0 :     return NULL;
   19750              :   return macro;
   19751          645 : }
   19752              : 
   19753              : /* Exported macro data.  */
   19754              : struct GTY(()) macro_export {
   19755              :   cpp_macro *def;
   19756              :   location_t undef_loc;
   19757              : 
   19758       103959 :   macro_export ()
   19759       103959 :     :def (NULL), undef_loc (UNKNOWN_LOCATION)
   19760              :   {
   19761              :   }
   19762              : };
   19763              : 
   19764              : /* Imported macro data.  */
   19765              : class macro_import {
   19766              : public:
   19767              :   struct slot {
   19768              : #if defined (WORDS_BIGENDIAN) && SIZEOF_VOID_P == 8
   19769              :     int offset;
   19770              : #endif
   19771              :     /* We need to ensure we don't use the LSB for representation, as
   19772              :        that's the union discriminator below.  */
   19773              :     unsigned bits;
   19774              : 
   19775              : #if !(defined (WORDS_BIGENDIAN) && SIZEOF_VOID_P == 8)
   19776              :     int offset;
   19777              : #endif
   19778              : 
   19779              :   public:
   19780              :     enum Layout {
   19781              :       L_DEF = 1,
   19782              :       L_UNDEF = 2,
   19783              :       L_BOTH = 3,
   19784              :       L_MODULE_SHIFT = 2
   19785              :     };
   19786              : 
   19787              :   public:
   19788              :     /* Not a regular ctor, because we put it in a union, and that's
   19789              :        not allowed in C++ 98.  */
   19790       185571 :     static slot ctor (unsigned module, unsigned defness)
   19791              :     {
   19792       185571 :       gcc_checking_assert (defness);
   19793       185571 :       slot s;
   19794       185571 :       s.bits = defness | (module << L_MODULE_SHIFT);
   19795       185571 :       s.offset = -1;
   19796       185571 :       return s;
   19797              :     }
   19798              : 
   19799              :   public:
   19800       149820 :     unsigned get_defness () const
   19801              :     {
   19802       149820 :       return bits & L_BOTH;
   19803              :     }
   19804       106027 :     unsigned get_module () const
   19805              :     {
   19806       106027 :       return bits >> L_MODULE_SHIFT;
   19807              :     }
   19808           12 :     void become_undef ()
   19809              :     {
   19810           12 :       bits &= ~unsigned (L_DEF);
   19811           12 :       bits |= unsigned (L_UNDEF);
   19812              :     }
   19813              :   };
   19814              : 
   19815              : private:
   19816              :   typedef vec<slot, va_heap, vl_embed> ary_t;
   19817              :   union either {
   19818              :     /* Discriminated by bits 0|1 != 0.  The expected case is that
   19819              :        there will be exactly one slot per macro, hence the effort of
   19820              :        packing that.  */
   19821              :     ary_t *ary;
   19822              :     slot single;
   19823              :   } u;
   19824              : 
   19825              : public:
   19826       151121 :   macro_import ()
   19827       151121 :   {
   19828       151121 :     u.ary = NULL;
   19829              :   }
   19830              : 
   19831              : private:
   19832      8112539 :   bool single_p () const
   19833              :   {
   19834      8112539 :     return u.single.bits & slot::L_BOTH;
   19835              :   }
   19836      8263669 :   bool occupied_p () const
   19837              :   {
   19838      8263669 :     return u.ary != NULL;
   19839              :   }
   19840              : 
   19841              : public:
   19842         1893 :   unsigned length () const
   19843              :   {
   19844         1893 :     gcc_checking_assert (occupied_p ());
   19845         1893 :     return single_p () ? 1 : u.ary->length ();
   19846              :   }
   19847      7972888 :   slot &operator[] (unsigned ix)
   19848              :   {
   19849      7972888 :     gcc_checking_assert (occupied_p ());
   19850      7972888 :     if (single_p ())
   19851              :       {
   19852      7886285 :         gcc_checking_assert (!ix);
   19853      7886285 :         return u.single;
   19854              :       }
   19855              :     else
   19856        86603 :       return (*u.ary)[ix];
   19857              :   }
   19858              : 
   19859              : public:
   19860              :   slot &exported ();
   19861              :   slot &append (unsigned module, unsigned defness);
   19862              : };
   19863              : 
   19864              : /* O is a new import to append to the list for.  If we're an empty
   19865              :    set, initialize us.  */
   19866              : 
   19867              : macro_import::slot &
   19868       185571 : macro_import::append (unsigned module, unsigned defness)
   19869              : {
   19870       185571 :   if (!occupied_p ())
   19871              :     {
   19872       151121 :       u.single = slot::ctor (module, defness);
   19873       151121 :       return u.single;
   19874              :     }
   19875              :   else
   19876              :     {
   19877        34450 :       bool single = single_p ();
   19878        34450 :       ary_t *m = single ? NULL : u.ary;
   19879        34450 :       vec_safe_reserve (m, 1 + single);
   19880        34450 :       if (single)
   19881        34447 :         m->quick_push (u.single);
   19882        34450 :       u.ary = m;
   19883        34450 :       return *u.ary->quick_push (slot::ctor (module, defness));
   19884              :     }
   19885              : }
   19886              : 
   19887              : /* We're going to export something.  Make sure the first import slot
   19888              :    is us.  */
   19889              : 
   19890              : macro_import::slot &
   19891       103317 : macro_import::exported ()
   19892              : {
   19893       103317 :   if (occupied_p () && !(*this)[0].get_module ())
   19894              :     {
   19895            9 :       slot &res = (*this)[0];
   19896            9 :       res.bits |= slot::L_DEF;
   19897            9 :       return res;
   19898              :     }
   19899              : 
   19900       103308 :   slot *a = &append (0, slot::L_DEF);
   19901       103308 :   if (!single_p ())
   19902              :     {
   19903        30193 :       slot &f = (*this)[0];
   19904        30193 :       std::swap (f, *a);
   19905        30193 :       a = &f;
   19906              :     }
   19907              :   return *a;
   19908              : }
   19909              : 
   19910              : /* The import (&exported) macros.  cpp_hasnode's deferred field
   19911              :    indexes this array (offset by 1, so zero means 'not present'.  */
   19912              : 
   19913              : static vec<macro_import, va_heap, vl_embed> *macro_imports;
   19914              : 
   19915              : /* The exported macros.  A macro_import slot's zeroth element's offset
   19916              :    indexes this array.  If the zeroth slot is not for module zero,
   19917              :    there is no export.  */
   19918              : 
   19919              : static GTY(()) vec<macro_export, va_gc> *macro_exports;
   19920              : 
   19921              : /* The reachable set of header imports from this TU.  */
   19922              : 
   19923              : static GTY(()) bitmap headers;
   19924              : 
   19925              : /* Get the (possibly empty) macro imports for NODE.  */
   19926              : 
   19927              : static macro_import &
   19928       155387 : get_macro_imports (cpp_hashnode *node)
   19929              : {
   19930       155387 :   if (node->deferred)
   19931         4266 :     return (*macro_imports)[node->deferred - 1];
   19932              : 
   19933       151121 :   vec_safe_reserve (macro_imports, 1);
   19934       151121 :   node->deferred = macro_imports->length () + 1;
   19935       151121 :   return *vec_safe_push (macro_imports, macro_import ());
   19936              : }
   19937              : 
   19938              : /* Get the macro export for export EXP of NODE.  */
   19939              : 
   19940              : static macro_export &
   19941       103317 : get_macro_export (macro_import::slot &slot)
   19942              : {
   19943       103317 :   if (slot.offset >= 0)
   19944            9 :     return (*macro_exports)[slot.offset];
   19945              : 
   19946       103308 :   vec_safe_reserve (macro_exports, 1);
   19947       103308 :   slot.offset = macro_exports->length ();
   19948       103308 :   return *macro_exports->quick_push (macro_export ());
   19949              : }
   19950              : 
   19951              : /* If NODE is an exportable macro, add it to the export set.  */
   19952              : 
   19953              : static int
   19954      3862440 : maybe_add_macro (cpp_reader *, cpp_hashnode *node, void *data_)
   19955              : {
   19956      3862440 :   bool exporting = false;
   19957              : 
   19958      3862440 :   if (cpp_user_macro_p (node))
   19959       496217 :     if (cpp_macro *macro = node->value.macro)
   19960              :       /* Ignore imported, builtins, command line and forced header macros.  */
   19961       495781 :       if (!macro->imported_p
   19962       495781 :           && !macro->lazy && macro->line >= spans.main_start ())
   19963              :         {
   19964        73124 :           gcc_checking_assert (macro->kind == cmk_macro);
   19965              :           /* I don't want to deal with this corner case, that I suspect is
   19966              :              a devil's advocate reading of the standard.  */
   19967        73124 :           gcc_checking_assert (!macro->extra_tokens);
   19968              : 
   19969        73124 :           macro_import::slot &slot = get_macro_imports (node).exported ();
   19970        73124 :           macro_export &exp = get_macro_export (slot);
   19971        73124 :           exp.def = macro;
   19972        73124 :           exporting = true;
   19973              :         }
   19974              : 
   19975      3789316 :   if (!exporting && node->deferred)
   19976              :     {
   19977          577 :       macro_import &imports = (*macro_imports)[node->deferred - 1];
   19978          577 :       macro_import::slot &slot = imports[0];
   19979          577 :       if (!slot.get_module ())
   19980              :         {
   19981          546 :           gcc_checking_assert (slot.get_defness ());
   19982              :           exporting = true;
   19983              :         }
   19984              :     }
   19985              : 
   19986        73124 :   if (exporting)
   19987        73670 :     static_cast<vec<cpp_hashnode *> *> (data_)->safe_push (node);
   19988              : 
   19989      3862440 :   return 1; /* Don't stop.  */
   19990              : }
   19991              : 
   19992              : /* Order cpp_hashnodes A_ and B_ by their exported macro locations.  */
   19993              : 
   19994              : static int
   19995      3844354 : macro_loc_cmp (const void *a_, const void *b_)
   19996              : {
   19997      3844354 :   const cpp_hashnode *node_a = *(const cpp_hashnode *const *)a_;
   19998      3844354 :   macro_import &import_a = (*macro_imports)[node_a->deferred - 1];
   19999      3844354 :   const macro_export &export_a = (*macro_exports)[import_a[0].offset];
   20000      3844354 :   location_t loc_a = export_a.def ? export_a.def->line : export_a.undef_loc;
   20001              : 
   20002      3844354 :   const cpp_hashnode *node_b = *(const cpp_hashnode *const *)b_;
   20003      3844354 :   macro_import &import_b = (*macro_imports)[node_b->deferred - 1];
   20004      3844354 :   const macro_export &export_b = (*macro_exports)[import_b[0].offset];
   20005      3844354 :   location_t loc_b = export_b.def ? export_b.def->line : export_b.undef_loc;
   20006              : 
   20007      3844354 :   if (loc_a < loc_b)
   20008              :     return +1;
   20009      1975057 :   else if (loc_a > loc_b)
   20010              :     return -1;
   20011              :   else
   20012            0 :     return 0;
   20013              : }
   20014              : 
   20015              : /* Gather the macro definitions and undefinitions that we will need to
   20016              :    write out.   */
   20017              : 
   20018              : vec<cpp_hashnode *> *
   20019          883 : module_state::prepare_macros (cpp_reader *reader)
   20020              : {
   20021          883 :   vec<cpp_hashnode *> *macros;
   20022          883 :   vec_alloc (macros, 100);
   20023              : 
   20024          883 :   cpp_forall_identifiers (reader, maybe_add_macro, macros);
   20025              : 
   20026          907 :   dump (dumper::MACRO) && dump ("No more than %u macros", macros->length ());
   20027              : 
   20028          883 :   macros->qsort (macro_loc_cmp);
   20029              : 
   20030              :   // Note the locations.
   20031        75436 :   for (unsigned ix = macros->length (); ix--;)
   20032              :     {
   20033        73670 :       cpp_hashnode *node = (*macros)[ix];
   20034        73670 :       macro_import::slot &slot = (*macro_imports)[node->deferred - 1][0];
   20035        73670 :       macro_export &mac = (*macro_exports)[slot.offset];
   20036              : 
   20037        73670 :       if (IDENTIFIER_KEYWORD_P (identifier (node)))
   20038            1 :         continue;
   20039              : 
   20040        73669 :       if (mac.undef_loc != UNKNOWN_LOCATION)
   20041           12 :         note_location (mac.undef_loc);
   20042        73669 :       if (mac.def)
   20043              :         {
   20044        73663 :           note_location (mac.def->line);
   20045       240143 :           for (unsigned ix = 0; ix != mac.def->count; ix++)
   20046       166480 :             note_location (mac.def->exp.tokens[ix].src_loc);
   20047              :         }
   20048              :     }
   20049              : 
   20050          883 :   return macros;
   20051              : }
   20052              : 
   20053              : /* Write out the exported defines.  This is two sections, one
   20054              :    containing the definitions, the other a table of node names.  */
   20055              : 
   20056              : unsigned
   20057          883 : module_state::write_macros (elf_out *to, vec<cpp_hashnode *> *macros,
   20058              :                             unsigned *crc_p)
   20059              : {
   20060          950 :   dump () && dump ("Writing macros");
   20061          883 :   dump.indent ();
   20062              : 
   20063              :   /* Write the defs */
   20064          883 :   bytes_out sec (to);
   20065          883 :   sec.begin ();
   20066              : 
   20067          883 :   unsigned count = 0;
   20068        75436 :   for (unsigned ix = macros->length (); ix--;)
   20069              :     {
   20070        73670 :       cpp_hashnode *node = (*macros)[ix];
   20071        73670 :       macro_import::slot &slot = (*macro_imports)[node->deferred - 1][0];
   20072        73670 :       gcc_assert (!slot.get_module () && slot.get_defness ());
   20073              : 
   20074        73670 :       macro_export &mac = (*macro_exports)[slot.offset];
   20075        73670 :       gcc_assert (!!(slot.get_defness () & macro_import::slot::L_UNDEF)
   20076              :                   == (mac.undef_loc != UNKNOWN_LOCATION)
   20077              :                   && !!(slot.get_defness () & macro_import::slot::L_DEF)
   20078              :                   == (mac.def != NULL));
   20079              : 
   20080        73670 :       if (IDENTIFIER_KEYWORD_P (identifier (node)))
   20081              :         {
   20082            1 :           warning_at (mac.def->line, 0,
   20083              :                       "not exporting %<#define %E%> as it is a keyword",
   20084              :                       identifier (node));
   20085            1 :           slot.offset = 0;
   20086            1 :           continue;
   20087              :         }
   20088              : 
   20089        73669 :       count++;
   20090        73669 :       slot.offset = sec.pos;
   20091        73669 :       dump (dumper::MACRO)
   20092           24 :         && dump ("Writing macro %s%s%s %I at %u",
   20093           24 :                  slot.get_defness () & macro_import::slot::L_UNDEF
   20094              :                  ? "#undef" : "",
   20095           24 :                  slot.get_defness () == macro_import::slot::L_BOTH
   20096              :                  ? " & " : "",
   20097           24 :                  slot.get_defness () & macro_import::slot::L_DEF
   20098              :                  ? "#define" : "",
   20099              :                  identifier (node), slot.offset);
   20100        73669 :       if (mac.undef_loc != UNKNOWN_LOCATION)
   20101           12 :         write_location (sec, mac.undef_loc);
   20102        73669 :       if (mac.def)
   20103        73663 :         write_define (sec, mac.def);
   20104              :     }
   20105          883 :   if (count)
   20106              :     // We may have ended on a tokenless macro with a very short
   20107              :     // location, that will cause problems reading its bit flags.
   20108          144 :     sec.u (0);
   20109          883 :   sec.end (to, to->name (MOD_SNAME_PFX ".def"), crc_p);
   20110              : 
   20111          883 :   if (count)
   20112              :     {
   20113              :       /* Write the table.  */
   20114          144 :       bytes_out sec (to);
   20115          144 :       sec.begin ();
   20116          144 :       sec.u (count);
   20117              : 
   20118        73957 :       for (unsigned ix = macros->length (); ix--;)
   20119              :         {
   20120        73669 :           const cpp_hashnode *node = (*macros)[ix];
   20121        73669 :           macro_import::slot &slot = (*macro_imports)[node->deferred - 1][0];
   20122              : 
   20123        73669 :           if (slot.offset)
   20124              :             {
   20125        73669 :               sec.cpp_node (node);
   20126        73669 :               sec.u (slot.get_defness ());
   20127        73669 :               sec.u (slot.offset);
   20128              :             }
   20129              :         }
   20130          144 :       sec.end (to, to->name (MOD_SNAME_PFX ".mac"), crc_p);
   20131          144 :     }
   20132              : 
   20133          883 :   dump.outdent ();
   20134          883 :   return count;
   20135          883 : }
   20136              : 
   20137              : bool
   20138          915 : module_state::read_macros ()
   20139              : {
   20140              :   /* Get the def section.  */
   20141          915 :   if (!slurp->macro_defs.begin (loc, from (), MOD_SNAME_PFX ".def"))
   20142              :     return false;
   20143              : 
   20144              :   /* Get the tbl section, if there are defs. */
   20145          915 :   if (slurp->macro_defs.more_p ()
   20146          915 :       && !slurp->macro_tbl.begin (loc, from (), MOD_SNAME_PFX ".mac"))
   20147              :     return false;
   20148              : 
   20149              :   return true;
   20150              : }
   20151              : 
   20152              : /* Install the macro name table.  */
   20153              : 
   20154              : void
   20155          921 : module_state::install_macros ()
   20156              : {
   20157          921 :   bytes_in &sec = slurp->macro_tbl;
   20158          921 :   if (!sec.size)
   20159              :     return;
   20160              : 
   20161          203 :   dump () && dump ("Reading macro table %M", this);
   20162          181 :   dump.indent ();
   20163              : 
   20164          181 :   unsigned count = sec.u ();
   20165          203 :   dump () && dump ("%u macros", count);
   20166        82444 :   while (count--)
   20167              :     {
   20168        82263 :       cpp_hashnode *node = sec.cpp_node ();
   20169        82263 :       macro_import &imp = get_macro_imports (node);
   20170        82263 :       unsigned flags = sec.u () & macro_import::slot::L_BOTH;
   20171        82263 :       if (!flags)
   20172            0 :         sec.set_overrun ();
   20173              : 
   20174        82263 :       if (sec.get_overrun ())
   20175              :         break;
   20176              : 
   20177        82263 :       macro_import::slot &slot = imp.append (mod, flags);
   20178        82263 :       slot.offset = sec.u ();
   20179              : 
   20180        82263 :       dump (dumper::MACRO)
   20181           84 :         && dump ("Read %s macro %s%s%s %I at %u",
   20182           30 :                  imp.length () > 1 ? "add" : "new",
   20183           27 :                  flags & macro_import::slot::L_UNDEF ? "#undef" : "",
   20184              :                  flags == macro_import::slot::L_BOTH ? " & " : "",
   20185           30 :                  flags & macro_import::slot::L_DEF ? "#define" : "",
   20186              :                  identifier (node), slot.offset);
   20187              : 
   20188              :       /* We'll leak an imported definition's TOKEN_FLD_STR's data
   20189              :          here.  But that only happens when we've had to resolve the
   20190              :          deferred macro before this import -- why are you doing
   20191              :          that?  */
   20192        82263 :       if (cpp_macro *cur = cpp_set_deferred_macro (node))
   20193        30181 :         if (!cur->imported_p)
   20194              :           {
   20195        30181 :             macro_import::slot &slot = imp.exported ();
   20196        30181 :             macro_export &exp = get_macro_export (slot);
   20197        30181 :             exp.def = cur;
   20198       112625 :             dump (dumper::MACRO)
   20199            0 :               && dump ("Saving current #define %I", identifier (node));
   20200              :           }
   20201              :     }
   20202              : 
   20203              :   /* We're now done with the table.  */
   20204          181 :   elf_in::release (slurp->from, sec);
   20205              : 
   20206          181 :   dump.outdent ();
   20207              : }
   20208              : 
   20209              : /* Import the transitive macros.  */
   20210              : 
   20211              : void
   20212          879 : module_state::import_macros ()
   20213              : {
   20214          879 :   bitmap_ior_into (headers, slurp->headers);
   20215              : 
   20216          879 :   bitmap_iterator bititer;
   20217          879 :   unsigned bitnum;
   20218         1800 :   EXECUTE_IF_SET_IN_BITMAP (slurp->headers, 0, bitnum, bititer)
   20219          921 :     (*modules)[bitnum]->install_macros ();
   20220          879 : }
   20221              : 
   20222              : /* NODE is being undefined at LOC.  Record it in the export table, if
   20223              :    necessary.  */
   20224              : 
   20225              : void
   20226       259233 : module_state::undef_macro (cpp_reader *, location_t loc, cpp_hashnode *node)
   20227              : {
   20228       259233 :   if (!node->deferred)
   20229              :     /* The macro is not imported, so our undef is irrelevant.  */
   20230              :     return;
   20231              : 
   20232           12 :   unsigned n = dump.push (NULL);
   20233              : 
   20234           12 :   macro_import::slot &slot = (*macro_imports)[node->deferred - 1].exported ();
   20235           12 :   macro_export &exp = get_macro_export (slot);
   20236              : 
   20237           12 :   exp.undef_loc = loc;
   20238           12 :   slot.become_undef ();
   20239           12 :   exp.def = NULL;
   20240              : 
   20241           18 :   dump (dumper::MACRO) && dump ("Recording macro #undef %I", identifier (node));
   20242              : 
   20243           12 :   dump.pop (n);
   20244              : }
   20245              : 
   20246              : /* NODE is a deferred macro node.  Determine the definition and return
   20247              :    it, with NULL if undefined.  May issue diagnostics.
   20248              : 
   20249              :    This can leak memory, when merging declarations -- the string
   20250              :    contents (TOKEN_FLD_STR) of each definition are allocated in
   20251              :    unreclaimable cpp objstack.  Only one will win.  However, I do not
   20252              :    expect this to be common -- mostly macros have a single point of
   20253              :    definition.  Perhaps we could restore the objstack to its position
   20254              :    after the first imported definition (if that wins)?  The macros
   20255              :    themselves are GC'd.  */
   20256              : 
   20257              : cpp_macro *
   20258          621 : module_state::deferred_macro (cpp_reader *reader, location_t loc,
   20259              :                               cpp_hashnode *node)
   20260              : {
   20261          621 :   macro_import &imports = (*macro_imports)[node->deferred - 1];
   20262              : 
   20263          621 :   unsigned n = dump.push (NULL);
   20264          627 :   dump (dumper::MACRO) && dump ("Deferred macro %I", identifier (node));
   20265              : 
   20266          621 :   bitmap visible (BITMAP_GGC_ALLOC ());
   20267              : 
   20268          621 :   if (!((imports[0].get_defness () & macro_import::slot::L_UNDEF)
   20269            0 :         && !imports[0].get_module ()))
   20270              :     {
   20271              :       /* Calculate the set of visible header imports.  */
   20272          621 :       bitmap_copy (visible, headers);
   20273         1410 :       for (unsigned ix = imports.length (); ix--;)
   20274              :         {
   20275          789 :           const macro_import::slot &slot = imports[ix];
   20276          789 :           unsigned mod = slot.get_module ();
   20277          789 :           if ((slot.get_defness () & macro_import::slot::L_UNDEF)
   20278          789 :               && bitmap_bit_p (visible, mod))
   20279              :             {
   20280           12 :               bitmap arg = mod ? (*modules)[mod]->slurp->headers : headers;
   20281           12 :               bitmap_and_compl_into (visible, arg);
   20282           12 :               bitmap_set_bit (visible, mod);
   20283              :             }
   20284              :         }
   20285              :     }
   20286          621 :   bitmap_set_bit (visible, 0);
   20287              : 
   20288              :   /* Now find the macros that are still visible.  */
   20289          621 :   bool failed = false;
   20290          621 :   cpp_macro *def = NULL;
   20291          621 :   vec<macro_export> defs;
   20292          621 :   defs.create (imports.length ());
   20293         1410 :   for (unsigned ix = imports.length (); ix--;)
   20294              :     {
   20295          789 :       const macro_import::slot &slot = imports[ix];
   20296          789 :       unsigned mod = slot.get_module ();
   20297          789 :       if (bitmap_bit_p (visible, mod))
   20298              :         {
   20299          777 :           macro_export *pushed = NULL;
   20300          777 :           if (mod)
   20301              :             {
   20302          651 :               const module_state *imp = (*modules)[mod];
   20303          651 :               bytes_in &sec = imp->slurp->macro_defs;
   20304          651 :               if (!sec.get_overrun ())
   20305              :                 {
   20306          651 :                   dump (dumper::MACRO)
   20307            6 :                     && dump ("Reading macro %s%s%s %I module %M at %u",
   20308            6 :                              slot.get_defness () & macro_import::slot::L_UNDEF
   20309              :                              ? "#undef" : "",
   20310            6 :                              slot.get_defness () == macro_import::slot::L_BOTH
   20311              :                              ? " & " : "",
   20312            6 :                              slot.get_defness () & macro_import::slot::L_DEF
   20313              :                              ? "#define" : "",
   20314            6 :                              identifier (node), imp, slot.offset);
   20315          651 :                   sec.random_access (slot.offset);
   20316              : 
   20317          651 :                   macro_export exp;
   20318          651 :                   if (slot.get_defness () & macro_import::slot::L_UNDEF)
   20319           12 :                     exp.undef_loc = imp->read_location (sec);
   20320          651 :                   if (slot.get_defness () & macro_import::slot::L_DEF)
   20321          645 :                     exp.def = imp->read_define (sec, reader);
   20322          651 :                   if (sec.get_overrun ())
   20323            0 :                     error_at (loc, "macro definitions of %qE corrupted",
   20324            0 :                               imp->name);
   20325              :                   else
   20326          651 :                     pushed = defs.quick_push (exp);
   20327              :                 }
   20328              :             }
   20329              :           else
   20330          126 :             pushed = defs.quick_push ((*macro_exports)[slot.offset]);
   20331          777 :           if (pushed && pushed->def)
   20332              :             {
   20333          771 :               if (!def)
   20334              :                 def = pushed->def;
   20335          153 :               else if (cpp_compare_macros (def, pushed->def))
   20336          789 :                 failed = true;
   20337              :             }
   20338              :         }
   20339              :     }
   20340              : 
   20341          621 :   if (failed)
   20342              :     {
   20343              :       /* If LOC is the first loc, this is the end of file check, which
   20344              :          is a warning.  */
   20345           15 :       auto_diagnostic_group d;
   20346           15 :       if (loc == MAP_START_LOCATION (LINEMAPS_ORDINARY_MAP_AT (line_table, 0)))
   20347            9 :         warning_at (loc, OPT_Winvalid_imported_macros,
   20348              :                     "inconsistent imported macro definition %qE",
   20349              :                     identifier (node));
   20350              :       else
   20351            6 :         error_at (loc, "inconsistent imported macro definition %qE",
   20352              :                   identifier (node));
   20353           60 :       for (unsigned ix = defs.length (); ix--;)
   20354              :         {
   20355           30 :           macro_export &exp = defs[ix];
   20356           30 :           if (exp.undef_loc)
   20357            0 :             inform (exp.undef_loc, "%<#undef %E%>", identifier (node));
   20358           30 :           if (exp.def)
   20359           30 :             inform (exp.def->line, "%<#define %s%>",
   20360              :                     cpp_macro_definition (reader, node, exp.def));
   20361              :         }
   20362           15 :       def = NULL;
   20363           15 :     }
   20364              : 
   20365          621 :   defs.release ();
   20366              : 
   20367          621 :   dump.pop (n);
   20368              : 
   20369          621 :   return def;
   20370              : }
   20371              : 
   20372              : /* Stream the static aggregates.  Sadly some headers (ahem:
   20373              :    iostream) contain static vars, and rely on them to run global
   20374              :    ctors.  */
   20375              : unsigned
   20376          883 : module_state::write_inits (elf_out *to, depset::hash &table, unsigned *crc_ptr)
   20377              : {
   20378          883 :   if (!static_aggregates && !tls_aggregates)
   20379              :     return 0;
   20380              : 
   20381           45 :   dump () && dump ("Writing initializers");
   20382           45 :   dump.indent ();
   20383              : 
   20384           45 :   static_aggregates = nreverse (static_aggregates);
   20385           45 :   tls_aggregates = nreverse (tls_aggregates);
   20386              : 
   20387           45 :   unsigned count = 0;
   20388           45 :   trees_out sec (to, this, table, ~0u);
   20389           45 :   sec.begin ();
   20390              : 
   20391           45 :   tree list = static_aggregates;
   20392          135 :   for (int passes = 0; passes != 2; passes++)
   20393              :     {
   20394          258 :       for (tree init = list; init; init = TREE_CHAIN (init))
   20395          168 :         if (TREE_LANG_FLAG_0 (init))
   20396              :           {
   20397          144 :             if (STATIC_INIT_DECOMP_BASE_P (init))
   20398              :               {
   20399              :                 /* Ensure that in the returned result chain if the
   20400              :                    STATIC_INIT_DECOMP_*BASE_P flags are set, there is
   20401              :                    always one or more STATIC_INIT_DECOMP_BASE_P TREE_LIST
   20402              :                    followed by one or more STATIC_INIT_DECOMP_NONBASE_P.  */
   20403           21 :                 int phase = 0;
   20404           21 :                 tree last = NULL_TREE;
   20405           21 :                 for (tree init2 = TREE_CHAIN (init);
   20406          126 :                      init2; init2 = TREE_CHAIN (init2))
   20407              :                   {
   20408          147 :                     if (phase == 0 && STATIC_INIT_DECOMP_BASE_P (init2))
   20409              :                       ;
   20410          126 :                     else if (phase == 0
   20411          147 :                              && STATIC_INIT_DECOMP_NONBASE_P (init2))
   20412              :                       {
   20413          120 :                         phase = TREE_LANG_FLAG_0 (init2) ? 2 : 1;
   20414              :                         last = init2;
   20415              :                       }
   20416          105 :                     else if (IN_RANGE (phase, 1, 2)
   20417          210 :                              && STATIC_INIT_DECOMP_NONBASE_P (init2))
   20418              :                       {
   20419           84 :                         if (TREE_LANG_FLAG_0 (init2))
   20420           81 :                           phase = 2;
   20421              :                         last = init2;
   20422              :                       }
   20423              :                     else
   20424              :                       break;
   20425              :                   }
   20426           21 :                 if (phase == 2)
   20427              :                   {
   20428              :                     /* In that case, add markers about it so that the
   20429              :                        STATIC_INIT_DECOMP_BASE_P and
   20430              :                        STATIC_INIT_DECOMP_NONBASE_P flags can be restored.  */
   20431           21 :                     sec.tree_node (build_int_cst (integer_type_node,
   20432           21 :                                                   2 * passes + 1));
   20433           21 :                     phase = 1;
   20434          147 :                     for (tree init2 = init; init2 != TREE_CHAIN (last);
   20435          126 :                          init2 = TREE_CHAIN (init2))
   20436          126 :                       if (TREE_LANG_FLAG_0 (init2))
   20437              :                         {
   20438          102 :                           tree decl = TREE_VALUE (init2);
   20439          102 :                           if (phase == 1
   20440          102 :                               && STATIC_INIT_DECOMP_NONBASE_P (init2))
   20441              :                             {
   20442           21 :                               sec.tree_node (build_int_cst (integer_type_node,
   20443           21 :                                                             2 * passes + 2));
   20444           21 :                               phase = 2;
   20445              :                             }
   20446          102 :                           dump ("Initializer:%u for %N", count, decl);
   20447          102 :                           sec.tree_node (decl);
   20448          102 :                           ++count;
   20449              :                         }
   20450           21 :                     sec.tree_node (integer_zero_node);
   20451           21 :                     init = last;
   20452           21 :                     continue;
   20453           21 :                   }
   20454              :               }
   20455              : 
   20456          123 :             tree decl = TREE_VALUE (init);
   20457              : 
   20458          123 :             dump ("Initializer:%u for %N", count, decl);
   20459          123 :             sec.tree_node (decl);
   20460          123 :             ++count;
   20461              :           }
   20462              : 
   20463           90 :       list = tls_aggregates;
   20464              :     }
   20465              : 
   20466           45 :   sec.end (to, to->name (MOD_SNAME_PFX ".ini"), crc_ptr);
   20467           45 :   dump.outdent ();
   20468              : 
   20469           45 :   return count;
   20470           45 : }
   20471              : 
   20472              : /* We have to defer some post-load processing until we've completed
   20473              :    reading, because they can cause more reading.  */
   20474              : 
   20475              : static void
   20476        11507 : post_load_processing ()
   20477              : {
   20478              :   /* We mustn't cause a GC, our caller should have arranged for that
   20479              :      not to happen.  */
   20480        11507 :   gcc_checking_assert (function_depth);
   20481              : 
   20482        11507 :   if (!post_load_decls)
   20483              :     return;
   20484              : 
   20485         6988 :   tree old_cfd = current_function_decl;
   20486         6988 :   struct function *old_cfun = cfun;
   20487        15090 :   while (post_load_decls->length ())
   20488              :     {
   20489         8102 :       tree decl = post_load_decls->pop ();
   20490              : 
   20491         8157 :       dump () && dump ("Post-load processing of %N", decl);
   20492              : 
   20493         8102 :       gcc_checking_assert (DECL_MAYBE_IN_CHARGE_CDTOR_P (decl));
   20494         8102 :       expand_or_defer_fn (decl);
   20495              :       /* As in module_state::read_cluster.  */
   20496          873 :       if (at_eof && DECL_COMDAT (decl) && DECL_EXTERNAL (decl)
   20497         8369 :           && DECL_NOT_REALLY_EXTERN (decl))
   20498          226 :         DECL_EXTERNAL (decl) = false;
   20499              :     }
   20500              : 
   20501         6988 :   set_cfun (old_cfun);
   20502         6988 :   current_function_decl = old_cfd;
   20503              : }
   20504              : 
   20505              : bool
   20506           45 : module_state::read_inits (unsigned count)
   20507              : {
   20508           45 :   trees_in sec (this);
   20509           45 :   if (!sec.begin (loc, from (), from ()->find (MOD_SNAME_PFX ".ini")))
   20510              :     return false;
   20511           57 :   dump () && dump ("Reading %u initializers", count);
   20512           45 :   dump.indent ();
   20513              : 
   20514           45 :   lazy_snum = ~0u;
   20515           45 :   int decomp_phase = 0;
   20516           45 :   tree *aggrp = NULL;
   20517          270 :   for (unsigned ix = 0; ix != count; ix++)
   20518              :     {
   20519          225 :       tree last = NULL_TREE;
   20520          225 :       if (decomp_phase)
   20521          102 :         last = *aggrp;
   20522              :       /* Merely referencing the decl causes its initializer to be read
   20523              :          and added to the correct list.  */
   20524          225 :       tree decl = sec.tree_node ();
   20525              :       /* module_state::write_inits can add special INTEGER_CST markers in
   20526              :          between the decls.  1 means STATIC_INIT_DECOMP_BASE_P entries
   20527              :          follow in static_aggregates, 2 means STATIC_INIT_DECOMP_NONBASE_P
   20528              :          entries follow in static_aggregates, 3 means
   20529              :          STATIC_INIT_DECOMP_BASE_P entries follow in tls_aggregates,
   20530              :          4 means STATIC_INIT_DECOMP_NONBASE_P follow in tls_aggregates,
   20531              :          0 means end of STATIC_INIT_DECOMP_{,NON}BASE_P sequence.  */
   20532          225 :       if (tree_fits_shwi_p (decl))
   20533              :         {
   20534           63 :           if (sec.get_overrun ())
   20535              :             break;
   20536           63 :           decomp_phase = tree_to_shwi (decl);
   20537           63 :           if (decomp_phase)
   20538              :             {
   20539           42 :               aggrp = decomp_phase > 2 ? &tls_aggregates : &static_aggregates;
   20540              :               last = *aggrp;
   20541              :             }
   20542           63 :           decl = sec.tree_node ();
   20543              :         }
   20544              : 
   20545          225 :       if (sec.get_overrun ())
   20546              :         break;
   20547          225 :       if (decl)
   20548          225 :         dump ("Initializer:%u for %N", ix, decl);
   20549          225 :       if (decomp_phase)
   20550              :         {
   20551          102 :           tree init = *aggrp;
   20552          102 :           gcc_assert (TREE_VALUE (init) == decl && TREE_CHAIN (init) == last);
   20553          102 :           if ((decomp_phase & 1) != 0)
   20554           21 :             STATIC_INIT_DECOMP_BASE_P (init) = 1;
   20555              :           else
   20556           81 :             STATIC_INIT_DECOMP_NONBASE_P (init) = 1;
   20557              :         }
   20558              :     }
   20559           45 :   if (decomp_phase && !sec.get_overrun ())
   20560              :     {
   20561            0 :       tree decl = sec.tree_node ();
   20562            0 :       gcc_assert (integer_zerop (decl));
   20563              :     }
   20564           45 :   lazy_snum = 0;
   20565           45 :   post_load_processing ();
   20566           45 :   dump.outdent ();
   20567           45 :   if (!sec.end (from ()))
   20568              :     return false;
   20569              :   return true;
   20570           45 : }
   20571              : 
   20572              : void
   20573         2685 : module_state::write_counts (elf_out *to, unsigned counts[MSC_HWM],
   20574              :                             unsigned *crc_ptr)
   20575              : {
   20576         2685 :   bytes_out cfg (to);
   20577              : 
   20578         2685 :   cfg.begin ();
   20579              : 
   20580        26850 :   for (unsigned ix = MSC_HWM; ix--;)
   20581        24165 :     cfg.u (counts[ix]);
   20582              : 
   20583         2685 :   if (dump ())
   20584              :     {
   20585          297 :       dump ("Cluster sections are [%u,%u)",
   20586              :             counts[MSC_sec_lwm], counts[MSC_sec_hwm]);
   20587          297 :       dump ("Bindings %u", counts[MSC_bindings]);
   20588          297 :       dump ("Pendings %u", counts[MSC_pendings]);
   20589          297 :       dump ("Entities %u", counts[MSC_entities]);
   20590          297 :       dump ("Namespaces %u", counts[MSC_namespaces]);
   20591          297 :       dump ("Using-directives %u", counts[MSC_using_directives]);
   20592          297 :       dump ("Macros %u", counts[MSC_macros]);
   20593          297 :       dump ("Initializers %u", counts[MSC_inits]);
   20594              :     }
   20595              : 
   20596         2685 :   cfg.end (to, to->name (MOD_SNAME_PFX ".cnt"), crc_ptr);
   20597         2685 : }
   20598              : 
   20599              : bool
   20600         2881 : module_state::read_counts (unsigned counts[MSC_HWM])
   20601              : {
   20602         2881 :   bytes_in cfg;
   20603              : 
   20604         2881 :   if (!cfg.begin (loc, from (), MOD_SNAME_PFX ".cnt"))
   20605              :     return false;
   20606              : 
   20607        28810 :   for (unsigned ix = MSC_HWM; ix--;)
   20608        25929 :     counts[ix] = cfg.u ();
   20609              : 
   20610         2881 :   if (dump ())
   20611              :     {
   20612          520 :       dump ("Declaration sections are [%u,%u)",
   20613              :             counts[MSC_sec_lwm], counts[MSC_sec_hwm]);
   20614          520 :       dump ("Bindings %u", counts[MSC_bindings]);
   20615          520 :       dump ("Pendings %u", counts[MSC_pendings]);
   20616          520 :       dump ("Entities %u", counts[MSC_entities]);
   20617          520 :       dump ("Namespaces %u", counts[MSC_namespaces]);
   20618          520 :       dump ("Using-directives %u", counts[MSC_using_directives]);
   20619          520 :       dump ("Macros %u", counts[MSC_macros]);
   20620          520 :       dump ("Initializers %u", counts[MSC_inits]);
   20621              :     }
   20622              : 
   20623         2881 :   return cfg.end (from ());
   20624         2881 : }
   20625              : 
   20626              : /* Tool configuration:  MOD_SNAME_PFX .config
   20627              : 
   20628              :    This is data that confirms current state (or fails).  */
   20629              : 
   20630              : void
   20631         2685 : module_state::write_config (elf_out *to, module_state_config &config,
   20632              :                             unsigned inner_crc)
   20633              : {
   20634         2685 :   bytes_out cfg (to);
   20635              : 
   20636         2685 :   cfg.begin ();
   20637              : 
   20638              :   /* Write version and inner crc as u32 values, for easier
   20639              :      debug inspection.  */
   20640         2982 :   dump () && dump ("Writing version=%V, inner_crc=%x",
   20641              :                    MODULE_VERSION, inner_crc);
   20642         2685 :   cfg.u32 (unsigned (MODULE_VERSION));
   20643         2685 :   cfg.u32 (inner_crc);
   20644              : 
   20645         2685 :   cfg.u (to->name (is_header () ? "" : get_flatname ()));
   20646              : 
   20647              :   /* Configuration. */
   20648         2982 :   dump () && dump ("Writing target='%s', host='%s'",
   20649              :                    TARGET_MACHINE, HOST_MACHINE);
   20650         2685 :   unsigned target = to->name (TARGET_MACHINE);
   20651         2685 :   unsigned host = (!strcmp (TARGET_MACHINE, HOST_MACHINE)
   20652              :                    ? target : to->name (HOST_MACHINE));
   20653         2685 :   cfg.u (target);
   20654         2685 :   cfg.u (host);
   20655              : 
   20656         2685 :   cfg.str (config.dialect_str);
   20657         2685 :   cfg.u (extensions);
   20658              : 
   20659              :   /* Global tree information.  We write the globals crc separately,
   20660              :      rather than mix it directly into the overall crc, as it is used
   20661              :      to ensure data match between instances of the compiler, not
   20662              :      integrity of the file.  */
   20663         2982 :   dump () && dump ("Writing globals=%u, crc=%x",
   20664              :                    fixed_trees->length (), global_crc);
   20665         2685 :   cfg.u (fixed_trees->length ());
   20666         2685 :   cfg.u32 (global_crc);
   20667              : 
   20668         2685 :   if (is_partition ())
   20669          187 :     cfg.u (is_interface ());
   20670              : 
   20671         2685 :   cfg.u (config.num_imports);
   20672         2685 :   cfg.u (config.num_partitions);
   20673         2685 :   cfg.u (config.num_entities);
   20674              : 
   20675         2685 :   cfg.loc (config.ordinary_locs);
   20676         2685 :   cfg.loc (config.macro_locs);
   20677         2685 :   cfg.u (config.loc_range_bits);
   20678              : 
   20679         2685 :   cfg.u (config.active_init);
   20680              : 
   20681              :   /* Now generate CRC, we'll have incorporated the inner CRC because
   20682              :      of its serialization above.  */
   20683         2685 :   cfg.end (to, to->name (MOD_SNAME_PFX ".cfg"), &crc);
   20684         2982 :   dump () && dump ("Writing CRC=%x", crc);
   20685         2685 : }
   20686              : 
   20687              : void
   20688           40 : module_state::note_cmi_name ()
   20689              : {
   20690           40 :   if (!cmi_noted_p && filename)
   20691              :     {
   20692           40 :       cmi_noted_p = true;
   20693           40 :       inform (loc, "compiled module file is %qs",
   20694              :               maybe_add_cmi_prefix (filename));
   20695              :     }
   20696           40 : }
   20697              : 
   20698              : bool
   20699         2991 : module_state::read_config (module_state_config &config, bool complain)
   20700              : {
   20701         2991 :   bytes_in cfg;
   20702              : 
   20703         2991 :   if (!cfg.begin (loc, from (), MOD_SNAME_PFX ".cfg"))
   20704              :     return false;
   20705              : 
   20706              :   /* Check version.  */
   20707         2991 :   unsigned my_ver = MODULE_VERSION;
   20708         2991 :   unsigned their_ver = cfg.u32 ();
   20709         3514 :   dump () && dump  (my_ver == their_ver ? "Version %V"
   20710              :                     : "Expecting %V found %V", my_ver, their_ver);
   20711         2991 :   if (their_ver != my_ver)
   20712              :     {
   20713              :       /* The compiler versions differ.  Close enough? */
   20714            0 :       verstr_t my_string, their_string;
   20715              : 
   20716            0 :       version2string (my_ver, my_string);
   20717            0 :       version2string (their_ver, their_string);
   20718              : 
   20719              :       /* Reject when either is non-experimental or when experimental
   20720              :          major versions differ.  */
   20721            0 :       auto_diagnostic_group d;
   20722            0 :       bool reject_p = ((!IS_EXPERIMENTAL (my_ver)
   20723              :                         || !IS_EXPERIMENTAL (their_ver)
   20724            0 :                         || MODULE_MAJOR (my_ver) != MODULE_MAJOR (their_ver))
   20725              :                        /* The 'I know what I'm doing' switch.  */
   20726            0 :                        && !flag_module_version_ignore);
   20727            0 :       bool inform_p = true;
   20728            0 :       if (!complain)
   20729              :         inform_p = false;
   20730            0 :       else if (reject_p)
   20731              :         {
   20732            0 :           cfg.set_overrun ();
   20733            0 :           error_at (loc, "compiled module is %sversion %s",
   20734              :                     IS_EXPERIMENTAL (their_ver) ? "experimental " : "",
   20735              :                     their_string);
   20736              :         }
   20737              :       else
   20738            0 :         inform_p = warning_at (loc, 0, "compiled module is %sversion %s",
   20739              :                              IS_EXPERIMENTAL (their_ver) ? "experimental " : "",
   20740              :                              their_string);
   20741              : 
   20742            0 :       if (inform_p)
   20743              :         {
   20744            0 :           inform (loc, "compiler is %sversion %s%s%s",
   20745              :                   IS_EXPERIMENTAL (my_ver) ? "experimental " : "",
   20746              :                   my_string,
   20747            0 :                   reject_p ? "" : flag_module_version_ignore
   20748            0 :                   ? ", be it on your own head!" : ", close enough?",
   20749              :                   reject_p ? "" : " \xc2\xaf\\_(\xe3\x83\x84)_/\xc2\xaf");
   20750            0 :           note_cmi_name ();
   20751              :         }
   20752              : 
   20753            0 :       if (reject_p)
   20754            0 :         goto done;
   20755            0 :     }
   20756              : 
   20757              :   /*  We wrote the inner crc merely to merge it, so simply read it
   20758              :       back and forget it.  */
   20759         2991 :   cfg.u32 ();
   20760              : 
   20761              :   /* Check module name.  */
   20762         2991 :   {
   20763         2991 :     const char *their_name = from ()->name (cfg.u ());
   20764         2991 :     const char *our_name = "";
   20765              : 
   20766         2991 :     if (!is_header ())
   20767         1974 :       our_name = get_flatname ();
   20768              : 
   20769              :     /* Header units can be aliased, so name checking is
   20770              :        inappropriate.  */
   20771         2991 :     if (0 != strcmp (their_name, our_name))
   20772              :       {
   20773            0 :         error_at (loc,
   20774            0 :                   their_name[0] && our_name[0] ? G_("module %qs found")
   20775              :                   : their_name[0]
   20776              :                   ? G_("header module expected, module %qs found")
   20777              :                   : G_("module %qs expected, header module found"),
   20778            0 :                   their_name[0] ? their_name : our_name);
   20779            0 :         cfg.set_overrun ();
   20780            0 :         goto done;
   20781              :       }
   20782              :   }
   20783              : 
   20784              :   /* Check the CRC after the above sanity checks, so that the user is
   20785              :      clued in.  */
   20786         2991 :   {
   20787         2991 :     unsigned e_crc = crc;
   20788         2991 :     crc = cfg.get_crc ();
   20789         3514 :     dump () && dump ("Reading CRC=%x", crc);
   20790              :     /* When not complaining we haven't set directness yet, so ignore the
   20791              :        mismatch. */
   20792         2991 :     if (complain && !is_direct () && crc != e_crc)
   20793              :       {
   20794            3 :         error_at (loc, "module %qs CRC mismatch", get_flatname ());
   20795            3 :         cfg.set_overrun ();
   20796            3 :         goto done;
   20797              :       }
   20798              :   }
   20799              : 
   20800              :   /* Check target & host.  */
   20801         2988 :   {
   20802         2988 :     const char *their_target = from ()->name (cfg.u ());
   20803         2988 :     const char *their_host = from ()->name (cfg.u ());
   20804         3511 :     dump () && dump ("Read target='%s', host='%s'", their_target, their_host);
   20805         2988 :     if (strcmp (their_target, TARGET_MACHINE)
   20806         2988 :         || strcmp (their_host, HOST_MACHINE))
   20807              :       {
   20808            0 :         error_at (loc, "target & host is %qs:%qs, expected %qs:%qs",
   20809              :                   their_target, TARGET_MACHINE, their_host, HOST_MACHINE);
   20810            0 :         cfg.set_overrun ();
   20811            0 :         goto done;
   20812              :       }
   20813              :   }
   20814              : 
   20815              :   /* Check compilation dialect.  This must match.  */
   20816         2988 :   {
   20817         2988 :     const char *their_dialect = cfg.str ();
   20818         2988 :     if (strcmp (their_dialect, config.dialect_str))
   20819              :       {
   20820            1 :         if (complain)
   20821            1 :           error_at (loc, "language dialect differs %qs, expected %qs",
   20822              :                     their_dialect, config.dialect_str);
   20823            1 :         cfg.set_overrun ();
   20824            1 :         goto done;
   20825              :       }
   20826              :   }
   20827              : 
   20828              :   /* Check for extensions.  If they set any, we must have them set
   20829              :      too.  */
   20830         2987 :   {
   20831         2987 :     unsigned ext = cfg.u ();
   20832         2987 :     unsigned allowed = (flag_openmp ? SE_OPENMP | SE_OPENMP_SIMD : 0);
   20833         2987 :     if (flag_openmp_simd)
   20834            3 :       allowed |= SE_OPENMP_SIMD;
   20835         2987 :     if (flag_openacc)
   20836            3 :       allowed |= SE_OPENACC;
   20837              : 
   20838         2987 :     if (unsigned bad = ext & ~allowed)
   20839              :       {
   20840            9 :         if (bad & SE_OPENMP)
   20841            3 :           error_at (loc, "module contains OpenMP, use %<-fopenmp%> to enable");
   20842            6 :         else if (bad & SE_OPENMP_SIMD)
   20843            3 :           error_at (loc, "module contains OpenMP, use %<-fopenmp%> or "
   20844              :                          "%<-fopenmp-simd%> to enable");
   20845            9 :         if (bad & SE_OPENACC)
   20846            3 :           error_at (loc, "module contains OpenACC, use %<-fopenacc%> to "
   20847              :                          "enable");
   20848            9 :         cfg.set_overrun ();
   20849            9 :         goto done;
   20850              :       }
   20851         2978 :     extensions = ext;
   20852              :   }
   20853              : 
   20854              :   /* Check global trees.  */
   20855         2978 :   {
   20856         2978 :     unsigned their_fixed_length = cfg.u ();
   20857         2978 :     unsigned their_fixed_crc = cfg.u32 ();
   20858         3501 :     dump () && dump ("Read globals=%u, crc=%x",
   20859              :                      their_fixed_length, their_fixed_crc);
   20860         2978 :     if (!flag_preprocess_only
   20861         2978 :         && (their_fixed_length != fixed_trees->length ()
   20862         2924 :             || their_fixed_crc != global_crc))
   20863              :       {
   20864            0 :         error_at (loc, "fixed tree mismatch");
   20865            0 :         cfg.set_overrun ();
   20866            0 :         goto done;
   20867              :       }
   20868              :   }
   20869              : 
   20870              :   /* All non-partitions are interfaces.  */
   20871         2978 :   interface_p = !is_partition () || cfg.u ();
   20872              : 
   20873         2978 :   config.num_imports = cfg.u ();
   20874         2978 :   config.num_partitions = cfg.u ();
   20875         2978 :   config.num_entities = cfg.u ();
   20876              : 
   20877         2978 :   config.ordinary_locs = cfg.loc ();
   20878         2978 :   config.macro_locs = cfg.loc ();
   20879         2978 :   config.loc_range_bits = cfg.u ();
   20880              : 
   20881         2978 :   config.active_init = cfg.u ();
   20882              : 
   20883         2991 :  done:
   20884         2991 :   return cfg.end (from ());
   20885         2991 : }
   20886              : 
   20887              : /* Comparator for ordering the Ordered Ordinary Location array.  */
   20888              : 
   20889              : static int
   20890          124 : ool_cmp (const void *a_, const void *b_)
   20891              : {
   20892          124 :   auto *a = *static_cast<const module_state *const *> (a_);
   20893          124 :   auto *b = *static_cast<const module_state *const *> (b_);
   20894          124 :   if (a == b)
   20895              :     return 0;
   20896          124 :   else if (a->ordinary_locs.first < b->ordinary_locs.first)
   20897              :     return -1;
   20898              :   else
   20899           52 :     return +1;
   20900              : }
   20901              : 
   20902              : /* Use ELROND format to record the following sections:
   20903              :      qualified-names        : binding value(s)
   20904              :      MOD_SNAME_PFX.README   : human readable, strings
   20905              :      MOD_SNAME_PFX.ENV      : environment strings, strings
   20906              :      MOD_SNAME_PFX.nms      : namespace hierarchy
   20907              :      MOD_SNAME_PFX.udi      : namespace using-directives
   20908              :      MOD_SNAME_PFX.bnd      : binding table
   20909              :      MOD_SNAME_PFX.spc      : specialization table
   20910              :      MOD_SNAME_PFX.imp      : import table
   20911              :      MOD_SNAME_PFX.ent      : entity table
   20912              :      MOD_SNAME_PFX.prt      : partitions table
   20913              :      MOD_SNAME_PFX.olm      : ordinary line maps
   20914              :      MOD_SNAME_PFX.mlm      : macro line maps
   20915              :      MOD_SNAME_PFX.def      : macro definitions
   20916              :      MOD_SNAME_PFX.mac      : macro index
   20917              :      MOD_SNAME_PFX.ini      : inits
   20918              :      MOD_SNAME_PFX.cnt      : counts
   20919              :      MOD_SNAME_PFX.cfg      : config data
   20920              : */
   20921              : 
   20922              : bool
   20923         2714 : module_state::write_begin (elf_out *to, cpp_reader *reader,
   20924              :                            module_state_config &config, unsigned &crc)
   20925              : {
   20926              :   /* Figure out remapped module numbers, which might elide
   20927              :      partitions.  */
   20928         2714 :   bitmap partitions = NULL;
   20929         2714 :   if (!is_header () && !is_partition ())
   20930         1644 :     partitions = BITMAP_GGC_ALLOC ();
   20931         2714 :   write_init_maps ();
   20932              : 
   20933         2714 :   unsigned mod_hwm = 1;
   20934         3374 :   for (unsigned ix = 1; ix != modules->length (); ix++)
   20935              :     {
   20936          660 :       module_state *imp = (*modules)[ix];
   20937              : 
   20938              :       /* Promote any non-partition direct import from a partition, unless
   20939              :          we're a partition.  */
   20940          603 :       if (!is_partition () && !imp->is_partition ()
   20941         1088 :           && imp->is_partition_direct ())
   20942           12 :         imp->directness = MD_PURVIEW_DIRECT;
   20943              : 
   20944              :       /* Write any import that is not a partition, unless we're a
   20945              :          partition.  */
   20946          660 :       if (!partitions || !imp->is_partition ())
   20947          485 :         imp->remap = mod_hwm++;
   20948              :       else
   20949              :         {
   20950          208 :           dump () && dump ("Partition %M %u", imp, ix);
   20951          175 :           bitmap_set_bit (partitions, ix);
   20952          175 :           imp->remap = 0;
   20953              :           /* All interface partitions must be exported.  */
   20954          175 :           if (imp->is_interface () && !bitmap_bit_p (exports, imp->mod))
   20955              :             {
   20956            3 :               error_at (imp->loc, "interface partition is not exported");
   20957            3 :               bitmap_set_bit (exports, imp->mod);
   20958              :             }
   20959              : 
   20960              :           /* All the partition entities should have been loaded when
   20961              :              loading the partition.  */
   20962              :           if (CHECKING_P)
   20963         1194 :             for (unsigned jx = 0; jx != imp->entity_num; jx++)
   20964              :               {
   20965         1019 :                 binding_slot *slot = &(*entity_ary)[imp->entity_lwm + jx];
   20966         1019 :                 gcc_checking_assert (!slot->is_lazy ());
   20967              :               }
   20968              :         }
   20969              : 
   20970          660 :       if (imp->is_direct () && (imp->remap || imp->is_partition ()))
   20971          642 :         note_location (imp->imported_from ());
   20972              :     }
   20973              : 
   20974         2714 :   if (partitions && bitmap_empty_p (partitions))
   20975              :     /* No partitions present.  */
   20976              :     partitions = nullptr;
   20977              : 
   20978              :   /* Find the set of decls we must write out.  */
   20979         2714 :   depset::hash table (DECL_NAMESPACE_BINDINGS (global_namespace)->size () * 8);
   20980              :   /* Add the specializations before the writables, so that we can
   20981              :      detect injected friend specializations.  */
   20982         2714 :   table.add_specializations (true);
   20983         2714 :   table.add_specializations (false);
   20984         2714 :   if (partial_specializations)
   20985              :     {
   20986          203 :       table.add_partial_entities (partial_specializations);
   20987          203 :       partial_specializations = NULL;
   20988              :     }
   20989         2714 :   table.add_namespace_entities (global_namespace, partitions);
   20990         2714 :   if (class_members)
   20991              :     {
   20992           12 :       table.add_class_entities (class_members);
   20993           12 :       class_members = NULL;
   20994              :     }
   20995              : 
   20996              :   /* Now join everything up.  */
   20997         2714 :   table.find_dependencies (this);
   20998              : 
   20999         2714 :   if (!table.finalize_dependencies ())
   21000              :     return false;
   21001              : 
   21002              : #if CHECKING_P
   21003              :   /* We're done verifying at-most once reading, reset to verify
   21004              :      at-most once writing.  */
   21005         2685 :   note_defs = note_defs_table_t::create_ggc (1000);
   21006              : #endif
   21007              : 
   21008              :   /* Determine Strongly Connected Components.  This will also strip any
   21009              :      unnecessary dependencies on imported or TU-local entities.  */
   21010         2685 :   vec<depset *> sccs = table.connect ();
   21011              : 
   21012         2685 :   vec_alloc (ool, modules->length ());
   21013         3338 :   for (unsigned ix = modules->length (); --ix;)
   21014              :     {
   21015          653 :       auto *import = (*modules)[ix];
   21016          653 :       if (import->loadedness > ML_NONE
   21017          653 :           && !(partitions && bitmap_bit_p (partitions, import->mod)))
   21018          478 :         ool->quick_push (import);
   21019              :     }
   21020         2685 :   ool->qsort (ool_cmp);
   21021              : 
   21022         2685 :   write_diagnostic_classification (nullptr, global_dc, nullptr);
   21023              : 
   21024         2685 :   vec<cpp_hashnode *> *macros = nullptr;
   21025         2685 :   if (is_header ())
   21026          883 :     macros = prepare_macros (reader);
   21027              : 
   21028         2685 :   config.num_imports = mod_hwm;
   21029         2685 :   config.num_partitions = modules->length () - mod_hwm;
   21030         2685 :   auto map_info = write_prepare_maps (&config, bool (config.num_partitions));
   21031         2685 :   unsigned counts[MSC_HWM];
   21032         2685 :   memset (counts, 0, sizeof (counts));
   21033              : 
   21034              :   /* depset::cluster is the cluster number,
   21035              :      depset::section is unspecified scratch value.
   21036              : 
   21037              :      The following loops make use of the tarjan property that
   21038              :      dependencies will be earlier in the SCCS array.  */
   21039              : 
   21040              :   /* This first loop determines the number of depsets in each SCC, and
   21041              :      also the number of namespaces we're dealing with.  During the
   21042              :      loop, the meaning of a couple of depset fields now change:
   21043              : 
   21044              :      depset::cluster -> size_of cluster, if first of cluster & !namespace
   21045              :      depset::section -> section number of cluster (if !namespace). */
   21046              : 
   21047         2685 :   unsigned n_spaces = 0;
   21048         2685 :   counts[MSC_sec_lwm] = counts[MSC_sec_hwm] = to->get_section_limit ();
   21049       252250 :   for (unsigned size, ix = 0; ix < sccs.length (); ix += size)
   21050              :     {
   21051       249565 :       depset **base = &sccs[ix];
   21052              : 
   21053       470562 :       if (base[0]->get_entity_kind () == depset::EK_NAMESPACE)
   21054              :         {
   21055         4651 :           n_spaces++;
   21056         4651 :           size = 1;
   21057              :         }
   21058              :       else
   21059              :         {
   21060              :           /* Count the members in this cluster.  */
   21061      1018068 :           for (size = 1; ix + size < sccs.length (); size++)
   21062      1015714 :             if (base[size]->cluster != base[0]->cluster)
   21063              :               break;
   21064              : 
   21065      1262982 :           for (unsigned jx = 0; jx != size; jx++)
   21066              :             {
   21067              :               /* Set the section number.  */
   21068      1018068 :               base[jx]->cluster = ~(~0u >> 1); /* A bad value.  */
   21069      1018068 :               base[jx]->section = counts[MSC_sec_hwm];
   21070              :             }
   21071              : 
   21072              :           /* Save the size in the first member's cluster slot.  */
   21073       244914 :           base[0]->cluster = size;
   21074              : 
   21075       244914 :           counts[MSC_sec_hwm]++;
   21076              :         }
   21077              :     }
   21078              : 
   21079              :   /* Write the clusters.  Namespace decls are put in the spaces array.
   21080              :      The meaning of depset::cluster changes to provide the
   21081              :      unnamed-decl count of the depset's decl (and remains zero for
   21082              :      non-decls and non-unnamed).  */
   21083         2685 :   unsigned bytes = 0;
   21084         2685 :   vec<depset *> spaces;
   21085         2685 :   spaces.create (n_spaces);
   21086              : 
   21087       252250 :   for (unsigned size, ix = 0; ix < sccs.length (); ix += size)
   21088              :     {
   21089       249565 :       depset **base = &sccs[ix];
   21090              : 
   21091       249565 :       if (base[0]->get_entity_kind () == depset::EK_NAMESPACE)
   21092              :         {
   21093         4651 :           tree decl = base[0]->get_entity ();
   21094         4651 :           if (decl == global_namespace)
   21095         2431 :             base[0]->cluster = 0;
   21096         2220 :           else if (!base[0]->is_import ())
   21097              :             {
   21098         2220 :               base[0]->cluster = counts[MSC_entities]++;
   21099         2220 :               spaces.quick_push (base[0]);
   21100         2220 :               counts[MSC_namespaces]++;
   21101         2220 :               if (CHECKING_P)
   21102              :                 {
   21103              :                   /* Add it to the entity map, such that we can tell it is
   21104              :                      part of us.  */
   21105         2220 :                   bool existed;
   21106         2220 :                   unsigned *slot = &entity_map->get_or_insert
   21107         2220 :                     (DECL_UID (decl), &existed);
   21108         2220 :                   if (existed)
   21109              :                     /* It must have come from a partition.  */
   21110            0 :                     gcc_checking_assert
   21111              :                       (import_entity_module (*slot)->is_partition ());
   21112         2220 :                   *slot = ~base[0]->cluster;
   21113              :                 }
   21114       251815 :               dump (dumper::CLUSTER) && dump ("Cluster namespace %N", decl);
   21115              :             }
   21116              :           size = 1;
   21117              :         }
   21118              :       else
   21119              :         {
   21120       244914 :           size = base[0]->cluster;
   21121              : 
   21122              :           /* Cluster is now used to number entities.  */
   21123       244914 :           base[0]->cluster = ~(~0u >> 1); /* A bad value.  */
   21124              : 
   21125       244914 :           sort_cluster (&table, base, size);
   21126              : 
   21127              :           /* Record the section for consistency checking during stream
   21128              :              out -- we don't want to start writing decls in different
   21129              :              sections.  */
   21130       244914 :           table.section = base[0]->section;
   21131       244914 :           bytes += write_cluster (to, base, size, table, counts, &crc);
   21132       244914 :           table.section = 0;
   21133              :         }
   21134              :     }
   21135              : 
   21136              :   /* depset::cluster - entity number (on entities)
   21137              :      depset::section - cluster number  */
   21138              :   /* We'd better have written as many sections and found as many
   21139              :      namespaces as we predicted.  */
   21140         5370 :   gcc_assert (counts[MSC_sec_hwm] == to->get_section_limit ()
   21141              :               && spaces.length () == counts[MSC_namespaces]);
   21142              : 
   21143              :   /* Write the entitites.  None happens if we contain namespaces or
   21144              :      nothing. */
   21145         2685 :   config.num_entities = counts[MSC_entities];
   21146         2685 :   if (counts[MSC_entities])
   21147         2425 :     write_entities (to, sccs, counts[MSC_entities], &crc);
   21148              : 
   21149              :   /* Write the namespaces.  */
   21150         2685 :   if (counts[MSC_namespaces])
   21151          567 :     write_namespaces (to, spaces, counts[MSC_namespaces], &crc);
   21152              : 
   21153              :   /* Write any using-directives.  */
   21154         2685 :   if (counts[MSC_namespaces])
   21155          567 :     counts[MSC_using_directives]
   21156          567 :       = write_using_directives (to, table, spaces, &crc);
   21157              : 
   21158              :   /* Write the bindings themselves.  */
   21159         2685 :   counts[MSC_bindings] = write_bindings (to, sccs, &crc);
   21160              : 
   21161              :   /* Write the unnamed.  */
   21162         2685 :   counts[MSC_pendings] = write_pendings (to, sccs, table, &crc);
   21163              : 
   21164              :   /* Write the import table.  */
   21165         2685 :   if (config.num_imports > 1)
   21166          447 :     write_imports (to, &crc);
   21167              : 
   21168              :   /* Write elided partition table.  */
   21169         2685 :   if (config.num_partitions)
   21170          127 :     write_partitions (to, config.num_partitions, &crc);
   21171              : 
   21172              :   /* Write the line maps.  */
   21173         2685 :   if (config.ordinary_locs)
   21174              :     {
   21175         2589 :       write_ordinary_maps (to, map_info, bool (config.num_partitions), &crc);
   21176         2589 :       write_diagnostic_classification (to, global_dc, &crc);
   21177              :     }
   21178         2685 :   if (config.macro_locs)
   21179          118 :     write_macro_maps (to, map_info, &crc);
   21180              : 
   21181         2685 :   if (is_header ())
   21182              :     {
   21183          883 :       counts[MSC_macros] = write_macros (to, macros, &crc);
   21184          883 :       counts[MSC_inits] = write_inits (to, table, &crc);
   21185          883 :       vec_free (macros);
   21186              :     }
   21187              : 
   21188         2685 :   unsigned clusters = counts[MSC_sec_hwm] - counts[MSC_sec_lwm];
   21189         2685 :   dump () && dump ("Wrote %u clusters, average %u bytes/cluster",
   21190          297 :                    clusters, (bytes + clusters / 2) / (clusters + !clusters));
   21191         2685 :   trees_out::instrument ();
   21192              : 
   21193         2685 :   write_counts (to, counts, &crc);
   21194              : 
   21195         2685 :   spaces.release ();
   21196         2685 :   sccs.release ();
   21197              : 
   21198         2685 :   vec_free (macro_loc_remap);
   21199         2685 :   vec_free (ord_loc_remap);
   21200         2685 :   vec_free (ool);
   21201              : 
   21202              :   // FIXME:QOI:  Have a command line switch to control more detailed
   21203              :   // information (which might leak data you do not want to leak).
   21204              :   // Perhaps (some of) the write_readme contents should also be
   21205              :   // so-controlled.
   21206         2685 :   if (false)
   21207              :     write_env (to);
   21208              : 
   21209         2685 :   return true;
   21210         2714 : }
   21211              : 
   21212              : // Finish module writing after we've emitted all dynamic initializers.
   21213              : 
   21214              : void
   21215         2685 : module_state::write_end (elf_out *to, cpp_reader *reader,
   21216              :                          module_state_config &config, unsigned &crc)
   21217              : {
   21218              :   /* And finish up.  */
   21219         2685 :   write_config (to, config, crc);
   21220              : 
   21221              :   /* Human-readable info.  */
   21222         2685 :   write_readme (to, reader, config.dialect_str);
   21223              : 
   21224         2982 :   dump () && dump ("Wrote %u sections", to->get_section_limit ());
   21225         2685 : }
   21226              : 
   21227              : /* Initial read of a CMI.  Checks config, loads up imports and line
   21228              :    maps.  */
   21229              : 
   21230              : bool
   21231         2945 : module_state::read_initial (cpp_reader *reader)
   21232              : {
   21233         2945 :   module_state_config config;
   21234         2945 :   bool ok = true;
   21235              : 
   21236         2945 :   if (ok && !read_config (config))
   21237              :     ok = false;
   21238              : 
   21239         2932 :   bool have_locs = ok && read_prepare_maps (&config);
   21240              : 
   21241              :   /* Ordinary maps before the imports.  */
   21242         2932 :   if (!(have_locs && config.ordinary_locs))
   21243           71 :     ordinary_locs.first = line_table->highest_location + 1;
   21244         2874 :   else if (!read_ordinary_maps (config.ordinary_locs, config.loc_range_bits))
   21245              :     ok = false;
   21246              : 
   21247              :   /* Allocate the REMAP vector.  */
   21248         2945 :   slurp->alloc_remap (config.num_imports);
   21249              : 
   21250         2945 :   if (ok)
   21251              :     {
   21252              :       /* Read the import table.  Decrement current to stop this CMI
   21253              :          from being evicted during the import. */
   21254         2932 :       slurp->current--;
   21255         2932 :       if (config.num_imports > 1 && !read_imports (reader, line_table))
   21256              :         ok = false;
   21257         2932 :       slurp->current++;
   21258              :     }
   21259              : 
   21260              :   /* Read the elided partition table, if we're the primary partition.  */
   21261         2932 :   if (ok && config.num_partitions && is_module ()
   21262         2956 :       && !read_partitions (config.num_partitions))
   21263              :     ok = false;
   21264              : 
   21265              :   /* Determine the module's number.  */
   21266         2945 :   gcc_checking_assert (mod == MODULE_UNKNOWN);
   21267         2945 :   gcc_checking_assert (this != this_module ());
   21268              : 
   21269         2945 :   {
   21270              :     /* Allocate space in the entities array now -- that array must be
   21271              :        monotonically in step with the modules array.  */
   21272         2945 :     entity_lwm = vec_safe_length (entity_ary);
   21273         2945 :     entity_num = config.num_entities;
   21274         2945 :     gcc_checking_assert (modules->length () == 1
   21275              :                          || modules->last ()->entity_lwm <= entity_lwm);
   21276         2945 :     vec_safe_reserve (entity_ary, config.num_entities);
   21277              : 
   21278         2945 :     binding_slot slot;
   21279         2945 :     slot.u.binding = NULL_TREE;
   21280      1170344 :     for (unsigned count = config.num_entities; count--;)
   21281      1167399 :       entity_ary->quick_push (slot);
   21282              :   }
   21283              : 
   21284              :   /* We'll run out of other resources before we run out of module
   21285              :      indices.  */
   21286         2945 :   mod = modules->length ();
   21287         2945 :   vec_safe_push (modules, this);
   21288              : 
   21289              :   /* We always import and export ourselves. */
   21290         2945 :   bitmap_set_bit (imports, mod);
   21291         2945 :   bitmap_set_bit (exports, mod);
   21292              : 
   21293         2945 :   if (ok)
   21294         2932 :     (*slurp->remap)[0] = mod << 1;
   21295         3465 :   dump () && dump ("Assigning %M module number %u", this, mod);
   21296              : 
   21297              :   /* We should not have been frozen during the importing done by
   21298              :      read_config.  */
   21299         2945 :   gcc_assert (!from ()->is_frozen ());
   21300              : 
   21301              :   /* Macro maps after the imports.  */
   21302         2945 :   if (!(ok && have_locs && config.macro_locs))
   21303         2816 :     macro_locs.first = LINEMAPS_MACRO_LOWEST_LOCATION (line_table);
   21304          129 :   else if (!read_macro_maps (config.macro_locs))
   21305              :     ok = false;
   21306              : 
   21307              :   /* Diagnostic classification streaming needs to come after reading
   21308              :      macro maps to handle _Pragmas in macros.  */
   21309         2932 :   if (ok && have_locs && config.ordinary_locs
   21310         5819 :       && !read_diagnostic_classification (global_dc))
   21311              :     ok = false;
   21312              : 
   21313              :   /* Note whether there's an active initializer.  */
   21314         2945 :   active_init_p = !is_header () && bool (config.active_init);
   21315              : 
   21316         2945 :   gcc_assert (slurp->current == ~0u);
   21317         2945 :   return ok;
   21318              : }
   21319              : 
   21320              : /* Read a preprocessor state.  */
   21321              : 
   21322              : bool
   21323          921 : module_state::read_preprocessor (bool outermost)
   21324              : {
   21325          921 :   gcc_checking_assert (is_header () && slurp
   21326              :                        && slurp->remap_module (0) == mod);
   21327              : 
   21328          921 :   if (loadedness == ML_PREPROCESSOR)
   21329            6 :     return !(from () && from ()->get_error ());
   21330              : 
   21331          915 :   bool ok = true;
   21332              : 
   21333              :   /* Read direct header imports.  */
   21334          915 :   unsigned len = slurp->remap->length ();
   21335          957 :   for (unsigned ix = 1; ok && ix != len; ix++)
   21336              :     {
   21337           42 :       unsigned map = (*slurp->remap)[ix];
   21338           42 :       if (map & 1)
   21339              :         {
   21340           42 :           module_state *import = (*modules)[map >> 1];
   21341           42 :           if (import->is_header ())
   21342              :             {
   21343           42 :               ok = import->read_preprocessor (false);
   21344           42 :               bitmap_ior_into (slurp->headers, import->slurp->headers);
   21345              :             }
   21346              :         }
   21347              :     }
   21348              : 
   21349              :   /* Record as a direct header.  */
   21350          915 :   if (ok)
   21351          915 :     bitmap_set_bit (slurp->headers, mod);
   21352              : 
   21353          915 :   if (ok && !read_macros ())
   21354              :     ok = false;
   21355              : 
   21356          915 :   loadedness = ML_PREPROCESSOR;
   21357          915 :   announce ("macros");
   21358              : 
   21359          915 :   if (flag_preprocess_only)
   21360              :     /* We're done with the string table.  */
   21361           39 :     from ()->release ();
   21362              : 
   21363          915 :   return check_read (outermost, ok);
   21364              : }
   21365              : 
   21366              : /* Read language state.  */
   21367              : 
   21368              : bool
   21369         2965 : module_state::read_language (bool outermost)
   21370              : {
   21371         2965 :   gcc_checking_assert (!lazy_snum);
   21372              : 
   21373         2965 :   if (loadedness == ML_LANGUAGE)
   21374           84 :     return !(slurp && from () && from ()->get_error ());
   21375              : 
   21376         2881 :   gcc_checking_assert (slurp && slurp->current == ~0u
   21377              :                        && slurp->remap_module (0) == mod);
   21378              : 
   21379         2881 :   bool ok = true;
   21380              : 
   21381              :   /* Read direct imports.  */
   21382         2881 :   unsigned len = slurp->remap->length ();
   21383         3280 :   for (unsigned ix = 1; ok && ix != len; ix++)
   21384              :     {
   21385          399 :       unsigned map = (*slurp->remap)[ix];
   21386          399 :       if (map & 1)
   21387              :         {
   21388          395 :           module_state *import = (*modules)[map >> 1];
   21389          395 :           if (!import->read_language (false))
   21390          399 :             ok = false;
   21391              :         }
   21392              :     }
   21393              : 
   21394         2881 :   unsigned counts[MSC_HWM];
   21395              : 
   21396         2881 :   if (ok && !read_counts (counts))
   21397              :     ok = false;
   21398              : 
   21399         2881 :   function_depth++; /* Prevent unexpected GCs.  */
   21400              : 
   21401         2881 :   if (ok && counts[MSC_entities] != entity_num)
   21402              :     ok = false;
   21403         2881 :   if (ok && counts[MSC_entities]
   21404         2655 :       && !read_entities (counts[MSC_entities],
   21405              :                          counts[MSC_sec_lwm], counts[MSC_sec_hwm]))
   21406              :     ok = false;
   21407              : 
   21408              :   /* Read the namespace hierarchy.  */
   21409         2881 :   if (ok && counts[MSC_namespaces]
   21410         3468 :       && !read_namespaces (counts[MSC_namespaces]))
   21411              :     ok = false;
   21412              : 
   21413              :   /* Read any using-directives.  */
   21414         2881 :   if (ok && counts[MSC_using_directives]
   21415         3036 :       && !read_using_directives (counts[MSC_using_directives]))
   21416              :     ok = false;
   21417              : 
   21418         2881 :   if (ok && !read_bindings (counts[MSC_bindings],
   21419              :                             counts[MSC_sec_lwm], counts[MSC_sec_hwm]))
   21420              :     ok = false;
   21421              : 
   21422              :   /* And unnamed.  */
   21423         2881 :   if (ok && counts[MSC_pendings] && !read_pendings (counts[MSC_pendings]))
   21424              :     ok = false;
   21425              : 
   21426         2881 :   if (ok)
   21427              :     {
   21428         2881 :       slurp->remaining = counts[MSC_sec_hwm] - counts[MSC_sec_lwm];
   21429         2881 :       available_clusters += counts[MSC_sec_hwm] - counts[MSC_sec_lwm];
   21430              :     }
   21431              : 
   21432         2881 :   if (!flag_module_lazy
   21433         2881 :       || (is_partition ()
   21434          226 :           && module_interface_p ()
   21435          199 :           && !module_partition_p ()))
   21436              :     {
   21437              :       /* Read the sections in forward order, so that dependencies are read
   21438              :          first.  See note about tarjan_connect.  */
   21439          516 :       ggc_collect ();
   21440              : 
   21441          516 :       lazy_snum = ~0u;
   21442              : 
   21443          516 :       unsigned hwm = counts[MSC_sec_hwm];
   21444       143173 :       for (unsigned ix = counts[MSC_sec_lwm]; ok && ix != hwm; ix++)
   21445       142657 :         if (!load_section (ix, NULL))
   21446              :           {
   21447              :             ok = false;
   21448              :             break;
   21449              :           }
   21450          516 :       lazy_snum = 0;
   21451          516 :       post_load_processing ();
   21452              : 
   21453          516 :       ggc_collect ();
   21454              : 
   21455          516 :       if (ok && CHECKING_P)
   21456       513593 :         for (unsigned ix = 0; ix != entity_num; ix++)
   21457       513077 :           gcc_assert (!(*entity_ary)[ix + entity_lwm].is_lazy ());
   21458              :     }
   21459              : 
   21460              :   // If the import is a header-unit, we need to register initializers
   21461              :   // of any static objects it contains (looking at you _Ioinit).
   21462              :   // Notice, the ordering of these initializers will be that of a
   21463              :   // dynamic initializer at this point in the current TU.  (Other
   21464              :   // instances of these objects in other TUs will be initialized as
   21465              :   // part of that TU's global initializers.)
   21466         2881 :   if (ok && counts[MSC_inits] && !read_inits (counts[MSC_inits]))
   21467              :     ok = false;
   21468              : 
   21469         2881 :   function_depth--;
   21470              : 
   21471         3222 :   announce (flag_module_lazy ? "lazy" : "imported");
   21472         2881 :   loadedness = ML_LANGUAGE;
   21473              : 
   21474         2881 :   gcc_assert (slurp->current == ~0u);
   21475              : 
   21476              :   /* We're done with the string table.  */
   21477         2881 :   from ()->release ();
   21478              : 
   21479         2881 :   return check_read (outermost, ok);
   21480              : }
   21481              : 
   21482              : bool
   21483       198340 : module_state::maybe_defrost ()
   21484              : {
   21485       198340 :   bool ok = true;
   21486       198340 :   if (from ()->is_frozen ())
   21487              :     {
   21488            9 :       if (lazy_open >= lazy_limit)
   21489            3 :         freeze_an_elf ();
   21490           18 :       dump () && dump ("Defrosting '%s'", filename);
   21491            9 :       ok = from ()->defrost (maybe_add_cmi_prefix (filename));
   21492            9 :       lazy_open++;
   21493              :     }
   21494              : 
   21495       198340 :   return ok;
   21496              : }
   21497              : 
   21498              : /* Load section SNUM, dealing with laziness.  It doesn't matter if we
   21499              :    have multiple concurrent loads, because we do not use TREE_VISITED
   21500              :    when reading back in.  */
   21501              : 
   21502              : bool
   21503       198340 : module_state::load_section (unsigned snum, binding_slot *mslot)
   21504              : {
   21505       198340 :   if (from ()->get_error ())
   21506              :     return false;
   21507              : 
   21508       198340 :   if (snum >= slurp->current)
   21509            0 :     from ()->set_error (elf::E_BAD_LAZY);
   21510       198340 :   else if (maybe_defrost ())
   21511              :     {
   21512       198340 :       unsigned old_current = slurp->current;
   21513       198340 :       slurp->current = snum;
   21514       198340 :       slurp->lru = 0;  /* Do not swap out.  */
   21515       198340 :       slurp->remaining--;
   21516       198340 :       read_cluster (snum);
   21517       198340 :       slurp->lru = ++lazy_lru;
   21518       198340 :       slurp->current = old_current;
   21519              :     }
   21520              : 
   21521       198340 :   if (mslot && mslot->is_lazy ())
   21522              :     {
   21523              :       /* Oops, the section didn't set this slot.  */
   21524            0 :       from ()->set_error (elf::E_BAD_DATA);
   21525            0 :       *mslot = NULL_TREE;
   21526              :     }
   21527              : 
   21528       198340 :   bool ok = !from ()->get_error ();
   21529       198340 :   if (!ok)
   21530              :     {
   21531            0 :       error_at (loc, "failed to read compiled module cluster %u: %s",
   21532            0 :                 snum, from ()->get_error (filename));
   21533            0 :       note_cmi_name ();
   21534              :     }
   21535              : 
   21536       198340 :   maybe_completed_reading ();
   21537              : 
   21538       198340 :   return ok;
   21539              : }
   21540              : 
   21541              : void
   21542       205065 : module_state::maybe_completed_reading ()
   21543              : {
   21544       205065 :   if (loadedness == ML_LANGUAGE && slurp->current == ~0u && !slurp->remaining)
   21545              :     {
   21546         2373 :       lazy_open--;
   21547              :       /* We no longer need the macros, all tokenizing has been done.  */
   21548         2373 :       slurp->release_macros ();
   21549              : 
   21550         2373 :       from ()->end ();
   21551         2373 :       slurp->close ();
   21552         2373 :       slurped ();
   21553              :     }
   21554       205065 : }
   21555              : 
   21556              : /* After a reading operation, make sure things are still ok.  If not,
   21557              :    emit an error and clean up.  */
   21558              : 
   21559              : bool
   21560         6762 : module_state::check_read (bool outermost, bool ok)
   21561              : {
   21562         6762 :   gcc_checking_assert (!outermost || slurp->current == ~0u);
   21563              : 
   21564         6762 :   if (!ok)
   21565           34 :     from ()->set_error ();
   21566              : 
   21567         6762 :   if (int e = from ()->get_error ())
   21568              :     {
   21569           40 :       auto_diagnostic_group d;
   21570           40 :       error_at (loc, "failed to read compiled module: %s",
   21571           40 :                 from ()->get_error (filename));
   21572           40 :       note_cmi_name ();
   21573              : 
   21574           40 :       if (e == EMFILE
   21575           40 :           || e == ENFILE
   21576              : #if MAPPED_READING
   21577           40 :           || e == ENOMEM
   21578              : #endif
   21579              :           || false)
   21580            0 :         inform (loc, "consider using %<-fno-module-lazy%>,"
   21581              :                 " increasing %<-param-lazy-modules=%u%> value,"
   21582              :                 " or increasing the per-process file descriptor limit",
   21583              :                 param_lazy_modules);
   21584           40 :       else if (e == ENOENT)
   21585           21 :         inform (loc, "imports must be built before being imported");
   21586              : 
   21587           40 :       if (outermost)
   21588           37 :         fatal_error (loc, "returning to the gate for a mechanical issue");
   21589              : 
   21590            3 :       ok = false;
   21591            3 :     }
   21592              : 
   21593         6725 :   maybe_completed_reading ();
   21594              : 
   21595         6725 :   return ok;
   21596              : }
   21597              : 
   21598              : /* Return the IDENTIFIER_NODE naming module IX.  This is the name
   21599              :    including dots.  */
   21600              : 
   21601              : char const *
   21602          408 : module_name (unsigned ix, bool header_ok)
   21603              : {
   21604          408 :   if (modules)
   21605              :     {
   21606          408 :       module_state *imp = (*modules)[ix];
   21607              : 
   21608          408 :       if (ix && !imp->name)
   21609            0 :         imp = imp->parent;
   21610              : 
   21611          408 :       if (header_ok || !imp->is_header ())
   21612          408 :         return imp->get_flatname ();
   21613              :     }
   21614              : 
   21615              :   return NULL;
   21616              : }
   21617              : 
   21618              : /* Return the bitmap describing what modules are imported.  Remember,
   21619              :    we always import ourselves.  */
   21620              : 
   21621              : bitmap
   21622       109779 : get_import_bitmap ()
   21623              : {
   21624       109779 :   return this_module ()->imports;
   21625              : }
   21626              : 
   21627              : /* Get the original decl for an instantiation at TINST, or NULL_TREE
   21628              :    if we're not an instantiation.  */
   21629              : 
   21630              : static tree
   21631       170036 : orig_decl_for_instantiation (tinst_level *tinst)
   21632              : {
   21633       170036 :   if (!tinst || TREE_CODE (tinst->tldcl) == TEMPLATE_FOR_STMT)
   21634              :     return NULL_TREE;
   21635              : 
   21636        81992 :   tree decl = tinst->tldcl;
   21637        81992 :   if (TREE_CODE (decl) == TREE_LIST)
   21638            0 :     decl = TREE_PURPOSE (decl);
   21639        81992 :   if (TYPE_P (decl))
   21640        11896 :     decl = TYPE_NAME (decl);
   21641              :   return decl;
   21642              : }
   21643              : 
   21644              : /* Return the visible imports and path of instantiation for an
   21645              :    instantiation at TINST.  If TINST is nullptr, we're not in an
   21646              :    instantiation, and thus will return the visible imports of the
   21647              :    current TU (and NULL *PATH_MAP_P).   We cache the information on
   21648              :    the tinst level itself.  */
   21649              : 
   21650              : static bitmap
   21651       117751 : path_of_instantiation (tinst_level *tinst, bitmap *path_map_p)
   21652              : {
   21653       117751 :   gcc_checking_assert (modules_p ());
   21654              : 
   21655       117751 :   tree decl = orig_decl_for_instantiation (tinst);
   21656       117751 :   if (!decl)
   21657              :     {
   21658        52545 :       gcc_assert (!tinst || !tinst->next);
   21659              :       /* Not inside an instantiation, just the regular case.  */
   21660        52545 :       *path_map_p = nullptr;
   21661        52545 :       return get_import_bitmap ();
   21662              :     }
   21663              : 
   21664        65206 :   if (!tinst->path)
   21665              :     {
   21666              :       /* Calculate.  */
   21667        19513 :       bitmap visible = path_of_instantiation (tinst->next, path_map_p);
   21668        19513 :       bitmap path_map = *path_map_p;
   21669              : 
   21670        19513 :       if (!path_map)
   21671              :         {
   21672         2489 :           path_map = BITMAP_GGC_ALLOC ();
   21673         2489 :           bitmap_set_bit (path_map, 0);
   21674              :         }
   21675              : 
   21676        19513 :       if (unsigned mod = get_originating_module (decl))
   21677         3850 :         if (!bitmap_bit_p (path_map, mod))
   21678              :           {
   21679              :             /* This is brand new information!  */
   21680          170 :             bitmap new_path = BITMAP_GGC_ALLOC ();
   21681          170 :             bitmap_copy (new_path, path_map);
   21682          170 :             bitmap_set_bit (new_path, mod);
   21683          170 :             path_map = new_path;
   21684              : 
   21685          170 :             bitmap imports = (*modules)[mod]->imports;
   21686          170 :             if (bitmap_intersect_compl_p (imports, visible))
   21687              :               {
   21688              :                 /* IMPORTS contains additional modules to VISIBLE.  */
   21689           32 :                 bitmap new_visible = BITMAP_GGC_ALLOC ();
   21690              : 
   21691           32 :                 bitmap_ior (new_visible, visible, imports);
   21692           32 :                 visible = new_visible;
   21693              :               }
   21694              :           }
   21695              : 
   21696        19513 :       tinst->path = path_map;
   21697        19513 :       tinst->visible = visible;
   21698              :     }
   21699              : 
   21700        65206 :   *path_map_p = tinst->path;
   21701        65206 :   return tinst->visible;
   21702              : }
   21703              : 
   21704              : /* Return the bitmap describing what modules are visible along the
   21705              :    path of instantiation.  If we're not an instantiation, this will be
   21706              :    the visible imports of the TU.  *PATH_MAP_P is filled in with the
   21707              :    modules owning the instantiation path -- we see the module-linkage
   21708              :    entities of those modules.  */
   21709              : 
   21710              : bitmap
   21711     42639102 : visible_instantiation_path (bitmap *path_map_p)
   21712              : {
   21713     42639102 :   if (!modules_p ())
   21714              :     return NULL;
   21715              : 
   21716        98238 :   return path_of_instantiation (current_instantiation (), path_map_p);
   21717              : }
   21718              : 
   21719              : /* Returns the bitmap describing what modules were visible from the
   21720              :    module that the current instantiation originated from.  If we're
   21721              :    not an instantiation, returns NULL.  *MODULE_P is filled in with
   21722              :    the originating module of the definition for this instantiation.  */
   21723              : 
   21724              : bitmap
   21725        52285 : visible_from_instantiation_origination (unsigned *module_p)
   21726              : {
   21727        52285 :   if (!modules_p ())
   21728              :     return NULL;
   21729              : 
   21730        52285 :   tree decl = orig_decl_for_instantiation (current_instantiation ());
   21731        52285 :   if (!decl)
   21732              :     return NULL;
   21733              : 
   21734        16786 :   *module_p = get_originating_module (decl);
   21735        16786 :   return (*modules)[*module_p]->imports;
   21736              : }
   21737              : 
   21738              : /* We've just directly imported IMPORT.  Update our import/export
   21739              :    bitmaps.  IS_EXPORT is true if we're reexporting the OTHER.  */
   21740              : 
   21741              : void
   21742         3032 : module_state::set_import (module_state const *import, bool is_export)
   21743              : {
   21744         3032 :   gcc_checking_assert (this != import);
   21745              : 
   21746              :   /* We see IMPORT's exports (which includes IMPORT).  If IMPORT is
   21747              :      the primary interface or a partition we'll see its imports.  */
   21748         3032 :   bitmap_ior_into (imports, import->is_module () || import->is_partition ()
   21749              :                    ? import->imports : import->exports);
   21750              : 
   21751         3032 :   if (is_export)
   21752              :     /* We'll export OTHER's exports.  */
   21753          469 :     bitmap_ior_into (exports, import->exports);
   21754         3032 : }
   21755              : 
   21756              : /* Return the declaring entity of DECL.  That is the decl determining
   21757              :    how to decorate DECL with module information.  Returns NULL_TREE if
   21758              :    it's the global module.  */
   21759              : 
   21760              : tree
   21761    175651918 : get_originating_module_decl (tree decl)
   21762              : {
   21763              :   /* An enumeration constant.  */
   21764    175651918 :   if (TREE_CODE (decl) == CONST_DECL
   21765         4686 :       && DECL_CONTEXT (decl)
   21766    175656604 :       && (TREE_CODE (DECL_CONTEXT (decl)) == ENUMERAL_TYPE))
   21767         4167 :     decl = TYPE_NAME (DECL_CONTEXT (decl));
   21768    175647751 :   else if (TREE_CODE (decl) == FIELD_DECL
   21769    175639016 :            || TREE_CODE (decl) == USING_DECL
   21770    351280042 :            || CONST_DECL_USING_P (decl))
   21771              :     {
   21772        15979 :       decl = DECL_CONTEXT (decl);
   21773        15979 :       if (TREE_CODE (decl) != FUNCTION_DECL)
   21774        15979 :         decl = TYPE_NAME (decl);
   21775              :     }
   21776              : 
   21777    175651918 :   gcc_checking_assert (TREE_CODE (decl) == TEMPLATE_DECL
   21778              :                        || TREE_CODE (decl) == FUNCTION_DECL
   21779              :                        || TREE_CODE (decl) == TYPE_DECL
   21780              :                        || TREE_CODE (decl) == VAR_DECL
   21781              :                        || TREE_CODE (decl) == CONCEPT_DECL
   21782              :                        || TREE_CODE (decl) == NAMESPACE_DECL);
   21783              : 
   21784    177139768 :   for (;;)
   21785              :     {
   21786              :       /* Uninstantiated template friends are owned by the befriending
   21787              :          class -- not their context.  */
   21788    176395843 :       if (TREE_CODE (decl) == TEMPLATE_DECL
   21789    176395843 :           && DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (decl))
   21790         6463 :         decl = TYPE_NAME (DECL_CHAIN (decl));
   21791              : 
   21792              :       /* An imported temploid friend is attached to the same module the
   21793              :          befriending class was.  */
   21794    176395843 :       if (imported_temploid_friends)
   21795      2635142 :         if (tree *slot = imported_temploid_friends->get (decl))
   21796          897 :           decl = *slot;
   21797              : 
   21798    176395843 :       int use;
   21799    176395843 :       if (tree ti = node_template_info (decl, use))
   21800              :         {
   21801       614677 :           decl = TI_TEMPLATE (ti);
   21802       614677 :           if (TREE_CODE (decl) != TEMPLATE_DECL)
   21803              :             {
   21804              :               /* A friend template specialization.  */
   21805           28 :               gcc_checking_assert (OVL_P (decl));
   21806           28 :               return global_namespace;
   21807              :             }
   21808              :         }
   21809              :       else
   21810              :         {
   21811    175781166 :           tree ctx = CP_DECL_CONTEXT (decl);
   21812    175781166 :           if (TREE_CODE (ctx) == NAMESPACE_DECL)
   21813              :             break;
   21814              : 
   21815       129276 :           if (TYPE_P (ctx))
   21816              :             {
   21817       119715 :               ctx = TYPE_NAME (ctx);
   21818       119715 :               if (!ctx)
   21819              :                 {
   21820              :                   /* Some kind of internal type.  */
   21821            0 :                   gcc_checking_assert (DECL_ARTIFICIAL (decl));
   21822            0 :                   return global_namespace;
   21823              :                 }
   21824              :             }
   21825       129276 :           decl = ctx;
   21826              :         }
   21827       743925 :     }
   21828              : 
   21829    175651890 :   return decl;
   21830              : }
   21831              : 
   21832              : /* If DECL is imported, return which module imported it, or 0 for the current
   21833              :    module.  Except that if GLOBAL_M1, return -1 for decls attached to the
   21834              :    global module.  */
   21835              : 
   21836              : int
   21837      1150670 : get_originating_module (tree decl, bool global_m1)
   21838              : {
   21839      1150670 :   tree owner = get_originating_module_decl (decl);
   21840      1150670 :   tree not_tmpl = STRIP_TEMPLATE (owner);
   21841              : 
   21842      1150670 :   if (!DECL_LANG_SPECIFIC (not_tmpl))
   21843       249437 :     return global_m1 ? -1 : 0;
   21844              : 
   21845      1747098 :   if (global_m1 && !DECL_MODULE_ATTACH_P (not_tmpl))
   21846              :     return -1;
   21847              : 
   21848       114814 :   int mod = !DECL_MODULE_IMPORT_P (not_tmpl) ? 0 : get_importing_module (owner);
   21849       114814 :   gcc_checking_assert (!global_m1 || !(*modules)[mod]->is_header ());
   21850              :   return mod;
   21851              : }
   21852              : 
   21853              : /* DECL is imported, return which module imported it.
   21854              :    If FLEXIBLE, return -1 if not found, otherwise checking ICE.  */
   21855              : 
   21856              : unsigned
   21857        15869 : get_importing_module (tree decl, bool flexible)
   21858              : {
   21859        15869 :   unsigned index = import_entity_index (decl, flexible);
   21860        15869 :   if (index == ~(~0u >> 1))
   21861              :     return -1;
   21862        15869 :   module_state *module = import_entity_module (index);
   21863              : 
   21864        15869 :   return module->mod;
   21865              : }
   21866              : 
   21867              : /* Is it permissible to redeclare OLDDECL with NEWDECL.
   21868              : 
   21869              :    If NEWDECL is NULL, assumes that OLDDECL will be redeclared using
   21870              :    the current scope's module and attachment.  */
   21871              : 
   21872              : bool
   21873       159136 : module_may_redeclare (tree olddecl, tree newdecl)
   21874              : {
   21875       159136 :   tree decl = olddecl;
   21876       212316 :   for (;;)
   21877              :     {
   21878       185726 :       tree ctx = CP_DECL_CONTEXT (decl);
   21879       185726 :       if (TREE_CODE (ctx) == NAMESPACE_DECL)
   21880              :         // Found the namespace-scope decl.
   21881              :         break;
   21882        27735 :       if (!CLASS_TYPE_P (ctx))
   21883              :         // We've met a non-class scope.  Such a thing is not
   21884              :         // reopenable, so we must be ok.
   21885              :         return true;
   21886        26590 :       decl = TYPE_NAME (ctx);
   21887        26590 :     }
   21888              : 
   21889       157991 :   int use_tpl = 0;
   21890       157991 :   if (node_template_info (STRIP_TEMPLATE (decl), use_tpl) && use_tpl)
   21891              :     // Specializations of any kind can be redeclared anywhere.
   21892              :     // FIXME: Should we be checking this in more places on the scope chain?
   21893              :     return true;
   21894              : 
   21895       111497 :   module_state *old_mod = get_primary (this_module ());
   21896       111497 :   module_state *new_mod = old_mod;
   21897              : 
   21898       111497 :   tree old_origin = get_originating_module_decl (decl);
   21899       111497 :   tree old_inner = STRIP_TEMPLATE (old_origin);
   21900       111497 :   bool olddecl_attached_p = (DECL_LANG_SPECIFIC (old_inner)
   21901       178309 :                              && DECL_MODULE_ATTACH_P (old_inner));
   21902       178309 :   if (DECL_LANG_SPECIFIC (old_inner) && DECL_MODULE_IMPORT_P (old_inner))
   21903              :     {
   21904          841 :       unsigned index = import_entity_index (old_origin);
   21905          841 :       old_mod = get_primary (import_entity_module (index));
   21906              :     }
   21907              : 
   21908       111497 :   bool newdecl_attached_p = module_attach_p ();
   21909       111497 :   if (newdecl)
   21910              :     {
   21911        26984 :       tree new_origin = get_originating_module_decl (newdecl);
   21912        26984 :       tree new_inner = STRIP_TEMPLATE (new_origin);
   21913        26984 :       newdecl_attached_p = (DECL_LANG_SPECIFIC (new_inner)
   21914        52710 :                             && DECL_MODULE_ATTACH_P (new_inner));
   21915        52710 :       if (DECL_LANG_SPECIFIC (new_inner) && DECL_MODULE_IMPORT_P (new_inner))
   21916              :         {
   21917          175 :           unsigned index = import_entity_index (new_origin);
   21918          175 :           new_mod = get_primary (import_entity_module (index));
   21919              :         }
   21920              :     }
   21921              : 
   21922              :   /* Module attachment needs to match.  */
   21923       111497 :   if (olddecl_attached_p == newdecl_attached_p)
   21924              :     {
   21925       111353 :       if (!olddecl_attached_p)
   21926              :         /* Both are GM entities, OK.  */
   21927              :         return true;
   21928              : 
   21929         1395 :       if (new_mod == old_mod)
   21930              :         /* Both attached to same named module, OK.  */
   21931              :         return true;
   21932              :     }
   21933              : 
   21934              :   /* Attached to different modules, error.  */
   21935          171 :   decl = newdecl ? newdecl : olddecl;
   21936          171 :   location_t loc = newdecl ? DECL_SOURCE_LOCATION (newdecl) : input_location;
   21937          171 :   if (DECL_IS_UNDECLARED_BUILTIN (olddecl))
   21938              :     {
   21939            3 :       if (newdecl_attached_p)
   21940            3 :         error_at (loc, "declaring %qD in module %qs conflicts with builtin "
   21941              :                   "in global module", decl, new_mod->get_flatname ());
   21942              :       else
   21943            0 :         error_at (loc, "declaration %qD conflicts with builtin", decl);
   21944              :     }
   21945          333 :   else if (DECL_LANG_SPECIFIC (old_inner) && DECL_MODULE_IMPORT_P (old_inner))
   21946              :     {
   21947          153 :       auto_diagnostic_group d;
   21948          153 :       if (newdecl_attached_p)
   21949           75 :         error_at (loc, "redeclaring %qD in module %qs conflicts with import",
   21950              :                   decl, new_mod->get_flatname ());
   21951              :       else
   21952           78 :         error_at (loc, "redeclaring %qD in global module conflicts with import",
   21953              :                   decl);
   21954              : 
   21955          153 :       if (olddecl_attached_p)
   21956          105 :         inform (DECL_SOURCE_LOCATION (olddecl),
   21957              :                 "import declared attached to module %qs",
   21958              :                 old_mod->get_flatname ());
   21959              :       else
   21960           48 :         inform (DECL_SOURCE_LOCATION (olddecl),
   21961              :                 "import declared in global module");
   21962          153 :     }
   21963              :   else
   21964              :     {
   21965           15 :       auto_diagnostic_group d;
   21966           15 :       if (newdecl_attached_p)
   21967           15 :         error_at (loc, "conflicting declaration of %qD in module %qs",
   21968              :                   decl, new_mod->get_flatname ());
   21969              :       else
   21970            0 :         error_at (loc, "conflicting declaration of %qD in global module",
   21971              :                   decl);
   21972              : 
   21973           15 :       if (olddecl_attached_p)
   21974            0 :         inform (DECL_SOURCE_LOCATION (olddecl),
   21975              :                 "previously declared in module %qs",
   21976              :                 old_mod->get_flatname ());
   21977              :       else
   21978           15 :         inform (DECL_SOURCE_LOCATION (olddecl),
   21979              :                 "previously declared in global module");
   21980           15 :     }
   21981              :   return false;
   21982              : }
   21983              : 
   21984              : /* DECL is being created by this TU.  Record it came from here.  We
   21985              :    record module purview, so we can see if partial or explicit
   21986              :    specialization needs to be written out, even though its purviewness
   21987              :    comes from the most general template.  */
   21988              : 
   21989              : void
   21990    997645011 : set_instantiating_module (tree decl)
   21991              : {
   21992    997645011 :   gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
   21993              :               || VAR_P (decl)
   21994              :               || TREE_CODE (decl) == TYPE_DECL
   21995              :               || TREE_CODE (decl) == CONCEPT_DECL
   21996              :               || TREE_CODE (decl) == TEMPLATE_DECL
   21997              :               || TREE_CODE (decl) == CONST_DECL
   21998              :               || (TREE_CODE (decl) == NAMESPACE_DECL
   21999              :                   && DECL_NAMESPACE_ALIAS (decl)));
   22000              : 
   22001    997645011 :   if (!modules_p ())
   22002              :     return;
   22003              : 
   22004      2830274 :   decl = STRIP_TEMPLATE (decl);
   22005              : 
   22006      2830274 :   if (!DECL_LANG_SPECIFIC (decl) && module_purview_p ())
   22007       359480 :     retrofit_lang_decl (decl);
   22008              : 
   22009      2830274 :   if (DECL_LANG_SPECIFIC (decl))
   22010              :     {
   22011      2270642 :       DECL_MODULE_PURVIEW_P (decl) = module_purview_p ();
   22012              :       /* If this was imported, we'll still be in the entity_hash.  */
   22013      2270642 :       DECL_MODULE_IMPORT_P (decl) = false;
   22014              :     }
   22015              : }
   22016              : 
   22017              : /* If DECL is a class member, whose class is not defined in this TU
   22018              :    (it was imported), remember this decl.  */
   22019              : 
   22020              : void
   22021       118236 : set_defining_module (tree decl)
   22022              : {
   22023       118236 :   gcc_checking_assert (!DECL_LANG_SPECIFIC (decl)
   22024              :                        || !DECL_MODULE_IMPORT_P (decl));
   22025              : 
   22026       118236 :   if (module_maybe_has_cmi_p ())
   22027              :     {
   22028              :       /* We need to track all declarations within a module, not just those
   22029              :          in the module purview, because we don't necessarily know yet if
   22030              :          this module will require a CMI while in the global fragment.  */
   22031        79178 :       tree ctx = DECL_CONTEXT (decl);
   22032        79178 :       if (ctx
   22033        79178 :           && (TREE_CODE (ctx) == RECORD_TYPE || TREE_CODE (ctx) == UNION_TYPE)
   22034        14687 :           && DECL_LANG_SPECIFIC (TYPE_NAME (ctx))
   22035        90364 :           && DECL_MODULE_IMPORT_P (TYPE_NAME (ctx)))
   22036              :         {
   22037              :           /* This entity's context is from an import.  We may need to
   22038              :              record this entity to make sure we emit it in the CMI.
   22039              :              Template specializations are in the template hash tables,
   22040              :              so we don't need to record them here as well.  */
   22041           39 :           int use_tpl = -1;
   22042           39 :           tree ti = node_template_info (decl, use_tpl);
   22043           39 :           if (use_tpl <= 0)
   22044              :             {
   22045           39 :               if (ti)
   22046              :                 {
   22047           21 :                   gcc_checking_assert (!use_tpl);
   22048              :                   /* Get to the TEMPLATE_DECL.  */
   22049           21 :                   decl = TI_TEMPLATE (ti);
   22050              :                 }
   22051              : 
   22052              :               /* Record it on the class_members list.  */
   22053           39 :               vec_safe_push (class_members, decl);
   22054              :             }
   22055              :         }
   22056              :     }
   22057       118236 : }
   22058              : 
   22059              : /* Also remember DECL if it's a newly declared class template partial
   22060              :    specialization, because these are not necessarily added to the
   22061              :    instantiation tables.  */
   22062              : 
   22063              : void
   22064      7835265 : set_defining_module_for_partial_spec (tree decl)
   22065              : {
   22066      7835265 :   if (module_maybe_has_cmi_p ()
   22067        19753 :       && DECL_IMPLICIT_TYPEDEF_P (decl)
   22068      7851976 :       && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
   22069        16711 :     vec_safe_push (partial_specializations, decl);
   22070      7835265 : }
   22071              : 
   22072              : void
   22073    296689516 : set_originating_module (tree decl, bool friend_p ATTRIBUTE_UNUSED)
   22074              : {
   22075    296689516 :   set_instantiating_module (decl);
   22076              : 
   22077    296689516 :   if (!DECL_NAMESPACE_SCOPE_P (decl))
   22078              :     return;
   22079              : 
   22080    180449259 :   gcc_checking_assert (friend_p || decl == get_originating_module_decl (decl));
   22081              : 
   22082    180449259 :   if (module_attach_p ())
   22083              :     {
   22084         4565 :       retrofit_lang_decl (decl);
   22085         4565 :       DECL_MODULE_ATTACH_P (decl) = true;
   22086              :     }
   22087              : 
   22088              :   /* It is ill-formed to export a declaration with internal linkage.  However,
   22089              :      at the point this function is called we don't yet always know whether this
   22090              :      declaration has internal linkage; instead we defer this check for callers
   22091              :      to do once visibility has been determined.  */
   22092    180449259 :   if (module_exporting_p ())
   22093       225864 :     DECL_MODULE_EXPORT_P (decl) = true;
   22094              : }
   22095              : 
   22096              : /* Checks whether DECL within a module unit has valid linkage for its kind.
   22097              :    Must be called after visibility for DECL has been finalised.  */
   22098              : 
   22099              : void
   22100    371836130 : check_module_decl_linkage (tree decl)
   22101              : {
   22102    371836130 :   if (!module_has_cmi_p ())
   22103              :     return;
   22104              : 
   22105              :   /* A header unit shall not contain a definition of a non-inline function
   22106              :      or variable (not template) whose name has external linkage.  */
   22107       520991 :   if (header_module_p ()
   22108       470705 :       && !processing_template_decl
   22109       155791 :       && ((TREE_CODE (decl) == FUNCTION_DECL
   22110        92103 :            && !DECL_DECLARED_INLINE_P (decl))
   22111       120503 :           || (TREE_CODE (decl) == VAR_DECL
   22112        36231 :               && !DECL_INLINE_VAR_P (decl)))
   22113        44403 :       && decl_defined_p (decl)
   22114         4397 :       && !(DECL_LANG_SPECIFIC (decl)
   22115         4397 :            && DECL_TEMPLATE_INSTANTIATION (decl))
   22116       525388 :       && decl_linkage (decl) == lk_external)
   22117           60 :     error_at (DECL_SOURCE_LOCATION (decl),
   22118              :               "external linkage definition of %qD in header module must "
   22119              :               "be declared %<inline%>", decl);
   22120              : 
   22121              :   /* An internal-linkage declaration cannot be generally be exported.
   22122              :      But it's OK to export any declaration from a header unit, including
   22123              :      internal linkage declarations.  */
   22124       520991 :   if (!header_module_p () && DECL_MODULE_EXPORT_P (decl))
   22125              :     {
   22126              :       /* Let's additionally treat any exported declaration within an
   22127              :          internal namespace as exporting a declaration with internal
   22128              :          linkage, as this would also implicitly export the internal
   22129              :          linkage namespace.  */
   22130         2125 :       if (decl_internal_context_p (decl))
   22131              :         {
   22132           50 :           error_at (DECL_SOURCE_LOCATION (decl),
   22133              :                     "exporting declaration %qD declared in unnamed namespace",
   22134              :                     decl);
   22135           50 :           DECL_MODULE_EXPORT_P (decl) = false;
   22136              :         }
   22137         2075 :       else if (decl_linkage (decl) == lk_internal)
   22138              :         {
   22139           17 :           error_at (DECL_SOURCE_LOCATION (decl),
   22140              :                     "exporting declaration %qD with internal linkage", decl);
   22141           17 :           DECL_MODULE_EXPORT_P (decl) = false;
   22142              :         }
   22143              :     }
   22144              : }
   22145              : 
   22146              : /* Given a scope CTX, find the scope we want to attach the key to,
   22147              :    or NULL if no key scope is required.  */
   22148              : 
   22149              : static tree
   22150         2030 : adjust_key_scope (tree ctx)
   22151              : {
   22152              :   /* For members, key it to the containing type to handle deduplication
   22153              :      correctly.  For fields, this is necessary as FIELD_DECLs have no
   22154              :      dep and so would only be streamed after the lambda type, defeating
   22155              :      our ability to merge them.
   22156              : 
   22157              :      Other class-scope key decls might depend on the type of the lambda
   22158              :      but be within the same cluster; we need to ensure that we never
   22159              :      first see the key decl while streaming the lambda type as merging
   22160              :      would then fail when comparing the partially-streamed lambda type
   22161              :      of the key decl with the existing (PR c++/122310).
   22162              : 
   22163              :      Perhaps sort_cluster can be adjusted to handle this better, but
   22164              :      this is a simple workaround (and might down on the number of
   22165              :      entries in keyed_table as a bonus).  */
   22166         3260 :   while (!DECL_NAMESPACE_SCOPE_P (ctx))
   22167         2460 :     if (DECL_CLASS_SCOPE_P (ctx))
   22168         1116 :       ctx = TYPE_NAME (DECL_CONTEXT (ctx));
   22169              :     else
   22170          114 :       ctx = DECL_CONTEXT (ctx);
   22171              : 
   22172         2030 :   return ctx;
   22173              : }
   22174              : 
   22175              : /* DECL is keyed to CTX for odr purposes.  */
   22176              : 
   22177              : void
   22178      2023019 : maybe_key_decl (tree ctx, tree decl)
   22179              : {
   22180      2023019 :   if (!modules_p ())
   22181              :     return;
   22182              : 
   22183              :   /* We only need to deal here with decls attached to var, field,
   22184              :      parm, type, function, or concept decls.  */
   22185         8597 :   if (TREE_CODE (ctx) != VAR_DECL
   22186         8597 :       && TREE_CODE (ctx) != FIELD_DECL
   22187              :       && TREE_CODE (ctx) != PARM_DECL
   22188              :       && TREE_CODE (ctx) != TYPE_DECL
   22189              :       && TREE_CODE (ctx) != FUNCTION_DECL
   22190              :       && TREE_CODE (ctx) != CONCEPT_DECL)
   22191              :     return;
   22192              : 
   22193        17191 :   gcc_checking_assert (LAMBDA_TYPE_P (TREE_TYPE (decl))
   22194              :                        || TREE_CODE (ctx) == FUNCTION_DECL);
   22195              : 
   22196              :   /* We don't need to use the keyed map for functions with definitions,
   22197              :      as we can instead use the MK_local_type handling for streaming.  */
   22198         8597 :   if (TREE_CODE (ctx) == FUNCTION_DECL
   22199         8597 :       && (has_definition (ctx)
   22200              :           /* If we won't be streaming this definition there's also no
   22201              :              need to record the key, as it will not be useful for merging
   22202              :              (this function is non-inline and so a matching declaration
   22203              :              will always be an ODR violation anyway).  */
   22204          120 :           || !module_maybe_has_cmi_p ()))
   22205              :     return;
   22206              : 
   22207          534 :   ctx = adjust_key_scope (ctx);
   22208              : 
   22209          534 :   if (!keyed_table)
   22210          152 :     keyed_table = new keyed_map_t (EXPERIMENT (1, 400));
   22211              : 
   22212          534 :   auto &vec = keyed_table->get_or_insert (ctx);
   22213          534 :   if (!vec.length ())
   22214              :     {
   22215          486 :       retrofit_lang_decl (ctx);
   22216          486 :       DECL_MODULE_KEYED_DECLS_P (ctx) = true;
   22217              :     }
   22218          534 :   if (CHECKING_P)
   22219          684 :     for (tree t : vec)
   22220           54 :       gcc_checking_assert (t != decl);
   22221              : 
   22222          534 :   vec.safe_push (decl);
   22223              : }
   22224              : 
   22225              : /* Find the scope that the local type or lambda DECL is keyed to, if any.  */
   22226              : 
   22227              : static tree
   22228         1523 : get_keyed_decl_scope (tree decl)
   22229              : {
   22230         1523 :   gcc_checking_assert (DECL_IMPLICIT_TYPEDEF_P (STRIP_TEMPLATE (decl)));
   22231              : 
   22232         4569 :   tree scope = (LAMBDA_TYPE_P (TREE_TYPE (decl))
   22233         2818 :                 ? LAMBDA_TYPE_EXTRA_SCOPE (TREE_TYPE (decl))
   22234         1523 :                 : CP_DECL_CONTEXT (decl));
   22235         1523 :   if (!scope)
   22236              :     return NULL_TREE;
   22237              : 
   22238         1496 :   gcc_checking_assert (TREE_CODE (scope) == VAR_DECL
   22239              :                        || TREE_CODE (scope) == FIELD_DECL
   22240              :                        || TREE_CODE (scope) == PARM_DECL
   22241              :                        || TREE_CODE (scope) == TYPE_DECL
   22242              :                        || (TREE_CODE (scope) == FUNCTION_DECL
   22243              :                            && !has_definition (scope))
   22244              :                        || TREE_CODE (scope) == CONCEPT_DECL);
   22245              : 
   22246         1496 :   scope = adjust_key_scope (scope);
   22247              : 
   22248         2992 :   gcc_checking_assert (scope
   22249              :                        && DECL_LANG_SPECIFIC (scope)
   22250              :                        && DECL_MODULE_KEYED_DECLS_P (scope));
   22251              :   return scope;
   22252              : }
   22253              : 
   22254              : /* DECL is an instantiated friend that should be attached to the same
   22255              :    module that ORIG is.  */
   22256              : 
   22257              : void
   22258      2650178 : propagate_defining_module (tree decl, tree orig)
   22259              : {
   22260      2650178 :   if (!modules_p ())
   22261              :     return;
   22262              : 
   22263         5387 :   tree not_tmpl = STRIP_TEMPLATE (orig);
   22264        10753 :   if (DECL_LANG_SPECIFIC (not_tmpl) && DECL_MODULE_ATTACH_P (not_tmpl))
   22265              :     {
   22266          126 :       tree inner = STRIP_TEMPLATE (decl);
   22267          126 :       retrofit_lang_decl (inner);
   22268          126 :       DECL_MODULE_ATTACH_P (inner) = true;
   22269              :     }
   22270              : 
   22271        10753 :   if (DECL_LANG_SPECIFIC (not_tmpl) && DECL_MODULE_IMPORT_P (not_tmpl))
   22272              :     {
   22273          389 :       bool exists = imported_temploid_friends->put (decl, orig);
   22274              : 
   22275              :       /* We should only be called if lookup for an existing decl
   22276              :          failed, in which case there shouldn't already be an entry
   22277              :          in the map.  */
   22278          389 :       gcc_assert (!exists);
   22279              :     }
   22280              : }
   22281              : 
   22282              : /* NEWDECL matched with OLDDECL, transfer defining module information
   22283              :    onto OLDDECL.  We've already validated attachment matches.  */
   22284              : 
   22285              : void
   22286     19095431 : transfer_defining_module (tree olddecl, tree newdecl)
   22287              : {
   22288     19095431 :   if (!modules_p ())
   22289              :     return;
   22290              : 
   22291        45101 :   tree old_inner = STRIP_TEMPLATE (olddecl);
   22292        45101 :   tree new_inner = STRIP_TEMPLATE (newdecl);
   22293              : 
   22294        45101 :   if (DECL_LANG_SPECIFIC (new_inner))
   22295              :     {
   22296        45013 :       gcc_checking_assert (DECL_LANG_SPECIFIC (old_inner));
   22297        45013 :       if (DECL_MODULE_PURVIEW_P (new_inner))
   22298        22309 :         DECL_MODULE_PURVIEW_P (old_inner) = true;
   22299        45013 :       if (!DECL_MODULE_IMPORT_P (new_inner))
   22300        45013 :         DECL_MODULE_IMPORT_P (old_inner) = false;
   22301              :     }
   22302              : 
   22303        45101 :   if (tree *p = imported_temploid_friends->get (newdecl))
   22304              :     {
   22305           70 :       tree orig = *p;
   22306           70 :       tree &slot = imported_temploid_friends->get_or_insert (olddecl);
   22307           70 :       if (!slot)
   22308           47 :         slot = orig;
   22309           23 :       else if (slot != orig)
   22310              :         /* This can happen when multiple classes declare the same
   22311              :            friend function (e.g. g++.dg/modules/tpl-friend-4);
   22312              :            make sure we at least attach to the same module.  */
   22313            3 :         gcc_checking_assert (get_originating_module (slot)
   22314              :                              == get_originating_module (orig));
   22315              :     }
   22316              : }
   22317              : 
   22318              : /* DECL is being freed, clear data we don't need anymore.  */
   22319              : 
   22320              : void
   22321        32389 : remove_defining_module (tree decl)
   22322              : {
   22323        32389 :   if (!modules_p ())
   22324              :     return;
   22325              : 
   22326        32389 :   if (imported_temploid_friends)
   22327        32389 :     imported_temploid_friends->remove (decl);
   22328              : }
   22329              : 
   22330              : /* Create the flat name string.  It is simplest to have it handy.  */
   22331              : 
   22332              : void
   22333         6231 : module_state::set_flatname ()
   22334              : {
   22335         6231 :   gcc_checking_assert (!flatname);
   22336         6231 :   if (parent)
   22337              :     {
   22338          654 :       auto_vec<tree,5> ids;
   22339          654 :       size_t len = 0;
   22340          654 :       char const *primary = NULL;
   22341          654 :       size_t pfx_len = 0;
   22342              : 
   22343          654 :       for (module_state *probe = this;
   22344         1609 :            probe;
   22345          955 :            probe = probe->parent)
   22346         1443 :         if (is_partition () && !probe->is_partition ())
   22347              :           {
   22348          488 :             primary = probe->get_flatname ();
   22349          488 :             pfx_len = strlen (primary);
   22350          488 :             break;
   22351              :           }
   22352              :         else
   22353              :           {
   22354          955 :             ids.safe_push (probe->name);
   22355          955 :             len += IDENTIFIER_LENGTH (probe->name) + 1;
   22356              :           }
   22357              : 
   22358          654 :       char *flat = XNEWVEC (char, pfx_len + len + is_partition ());
   22359          654 :       flatname = flat;
   22360              : 
   22361          654 :       if (primary)
   22362              :         {
   22363          488 :           memcpy (flat, primary, pfx_len);
   22364          488 :           flat += pfx_len;
   22365          488 :           *flat++ = ':';
   22366              :         }
   22367              : 
   22368         1609 :       for (unsigned len = 0; ids.length ();)
   22369              :         {
   22370          955 :           if (len)
   22371          301 :             flat[len++] = '.';
   22372          955 :           tree elt = ids.pop ();
   22373          955 :           unsigned l = IDENTIFIER_LENGTH (elt);
   22374          955 :           memcpy (flat + len, IDENTIFIER_POINTER (elt), l + 1);
   22375          955 :           len += l;
   22376              :         }
   22377          654 :     }
   22378         5577 :   else if (is_header ())
   22379         1881 :     flatname = TREE_STRING_POINTER (name);
   22380              :   else
   22381         3696 :     flatname = IDENTIFIER_POINTER (name);
   22382         6231 : }
   22383              : 
   22384              : /* Open the GCM file and prepare to read.  Return whether that was
   22385              :    successful.  */
   22386              : 
   22387              : bool
   22388         3015 : module_state::open_slurp (cpp_reader *reader)
   22389              : {
   22390         3015 :   if (slurp)
   22391              :     return true;
   22392              : 
   22393         2969 :   if (lazy_open >= lazy_limit)
   22394            9 :     freeze_an_elf ();
   22395              : 
   22396         2969 :   int fd = -1;
   22397         2969 :   int e = ENOENT;
   22398         2969 :   if (filename)
   22399              :     {
   22400         2969 :       const char *file = maybe_add_cmi_prefix (filename);
   22401         3489 :       dump () && dump ("CMI is %s", file);
   22402         2969 :       if (note_module_cmi_yes || inform_cmi_p)
   22403           12 :         inform (loc, "reading CMI %qs", file);
   22404              :       /* Add the CMI file to the dependency tracking. */
   22405         2969 :       if (cpp_get_deps (reader))
   22406           15 :         deps_add_dep (cpp_get_deps (reader), file);
   22407         2969 :       fd = open (file, O_RDONLY | O_CLOEXEC | O_BINARY);
   22408         2969 :       e = errno;
   22409              :     }
   22410              : 
   22411         2969 :   gcc_checking_assert (!slurp);
   22412         5914 :   slurp = new slurping (new elf_in (fd, e));
   22413              : 
   22414         2969 :   bool ok = from ()->begin (loc);
   22415         2969 :   if (ok)
   22416              :     {
   22417         2945 :       lazy_open++;
   22418         2945 :       slurp->lru = ++lazy_lru;
   22419              :     }
   22420              :   return ok;
   22421              : }
   22422              : 
   22423              : /* Return whether importing this GCM would work without an error in
   22424              :    read_config.  */
   22425              : 
   22426              : bool
   22427           52 : module_state::check_importable (cpp_reader *reader)
   22428              : {
   22429           52 :   if (loadedness > ML_CONFIG)
   22430              :     return true;
   22431           49 :   if (!open_slurp (reader))
   22432              :     return false;
   22433           46 :   module_state_config config;
   22434           46 :   return read_config (config, /*complain*/false);
   22435              : }
   22436              : 
   22437              : /* Read the CMI file for a module.  */
   22438              : 
   22439              : bool
   22440         2966 : module_state::do_import (cpp_reader *reader, bool outermost)
   22441              : {
   22442         2966 :   gcc_assert (global_namespace == current_scope () && loadedness == ML_NONE);
   22443              : 
   22444              :   /* If this TU is a partition of the module we're importing,
   22445              :      that module is the primary module interface.  */
   22446         2966 :   if (this_module ()->is_partition ()
   22447         3017 :       && this == get_primary (this_module ()))
   22448            9 :     module_p = true;
   22449              : 
   22450         2966 :   loc = linemap_module_loc (line_table, loc, get_flatname ());
   22451              : 
   22452         2966 :   bool ok = open_slurp (reader);
   22453         2966 :   if (!from ()->get_error ())
   22454              :     {
   22455         2945 :       announce ("importing");
   22456         2945 :       loadedness = ML_CONFIG;
   22457         2945 :       ok = read_initial (reader);
   22458              :     }
   22459              : 
   22460         2966 :   gcc_assert (slurp->current == ~0u);
   22461              : 
   22462         2966 :   return check_read (outermost, ok);
   22463              : }
   22464              : 
   22465              : /* Attempt to increase the file descriptor limit.  */
   22466              : 
   22467              : static bool
   22468         4820 : try_increase_lazy (unsigned want)
   22469              : {
   22470         4820 :   gcc_checking_assert (lazy_open >= lazy_limit);
   22471              : 
   22472              :   /* If we're increasing, saturate at hard limit.  */
   22473         4820 :   if (want > lazy_hard_limit && lazy_limit < lazy_hard_limit)
   22474         4820 :     want = lazy_hard_limit;
   22475              : 
   22476              : #if HAVE_SETRLIMIT
   22477         4820 :   if ((!lazy_limit || !param_lazy_modules)
   22478         4808 :       && lazy_hard_limit
   22479         4808 :       && want <= lazy_hard_limit)
   22480              :     {
   22481         4808 :       struct rlimit rlimit;
   22482         4808 :       rlimit.rlim_cur = want + LAZY_HEADROOM;
   22483         4808 :       rlimit.rlim_max = lazy_hard_limit + LAZY_HEADROOM;
   22484         4808 :       if (!setrlimit (RLIMIT_NOFILE, &rlimit))
   22485         4808 :         lazy_limit = want;
   22486              :     }
   22487              : #endif
   22488              : 
   22489         4820 :   return lazy_open < lazy_limit;
   22490              : }
   22491              : 
   22492              : /* Pick a victim module to freeze its reader.  */
   22493              : 
   22494              : void
   22495           12 : module_state::freeze_an_elf ()
   22496              : {
   22497           12 :   if (try_increase_lazy (lazy_open * 2))
   22498              :     return;
   22499              : 
   22500           12 :   module_state *victim = NULL;
   22501           12 :   for (unsigned ix = modules->length (); ix--;)
   22502              :     {
   22503           30 :       module_state *candidate = (*modules)[ix];
   22504           30 :       if (candidate && candidate->slurp && candidate->slurp->lru
   22505           60 :           && candidate->from ()->is_freezable ()
   22506           39 :           && (!victim || victim->slurp->lru > candidate->slurp->lru))
   22507              :         victim = candidate;
   22508              :     }
   22509              : 
   22510           12 :   if (victim)
   22511              :     {
   22512           18 :       dump () && dump ("Freezing '%s'", victim->filename);
   22513            9 :       if (victim->slurp->macro_defs.size)
   22514              :         /* Save the macro definitions to a buffer.  */
   22515            0 :         victim->from ()->preserve (victim->slurp->macro_defs);
   22516            9 :       if (victim->slurp->macro_tbl.size)
   22517              :         /* Save the macro definitions to a buffer.  */
   22518            0 :         victim->from ()->preserve (victim->slurp->macro_tbl);
   22519            9 :       victim->from ()->freeze ();
   22520            9 :       lazy_open--;
   22521              :     }
   22522              :   else
   22523            3 :     dump () && dump ("No module available for freezing");
   22524              : }
   22525              : 
   22526              : /* Load the lazy slot *MSLOT, INDEX'th slot of the module.  */
   22527              : 
   22528              : bool
   22529        50746 : module_state::lazy_load (unsigned index, binding_slot *mslot)
   22530              : {
   22531        50746 :   unsigned n = dump.push (this);
   22532              : 
   22533        50746 :   gcc_checking_assert (function_depth);
   22534              : 
   22535        50746 :   unsigned cookie = mslot->get_lazy ();
   22536        50746 :   unsigned snum = cookie >> 2;
   22537        51114 :   dump () && dump ("Loading entity %M[%u] section:%u", this, index, snum);
   22538              : 
   22539        50746 :   bool ok = load_section (snum, mslot);
   22540              : 
   22541        50746 :   dump.pop (n);
   22542              : 
   22543        50746 :   return ok;
   22544              : }
   22545              : 
   22546              : /* Load MOD's binding for NS::ID into *MSLOT.  *MSLOT contains the
   22547              :    lazy cookie.  OUTER is true if this is the outermost lazy, (used
   22548              :    for diagnostics).  */
   22549              : 
   22550              : void
   22551         4937 : lazy_load_binding (unsigned mod, tree ns, tree id, binding_slot *mslot)
   22552              : {
   22553         4937 :   int count = errorcount + warningcount;
   22554              : 
   22555         4937 :   bool timer_running = timevar_cond_start (TV_MODULE_IMPORT);
   22556              : 
   22557              :   /* Make sure lazy loading from a template context behaves as if
   22558              :      from a non-template context.  */
   22559         4937 :   processing_template_decl_sentinel ptds;
   22560              : 
   22561              :   /* Stop GC happening, even in outermost loads (because our caller
   22562              :      could well be building up a lookup set).  */
   22563         4937 :   function_depth++;
   22564              : 
   22565         4937 :   gcc_checking_assert (mod);
   22566         4937 :   module_state *module = (*modules)[mod];
   22567         4937 :   unsigned n = dump.push (module);
   22568              : 
   22569         4937 :   unsigned snum = mslot->get_lazy ();
   22570         5250 :   dump () && dump ("Lazily binding %P@%N section:%u", ns, id,
   22571              :                    module->name, snum);
   22572              : 
   22573         4937 :   bool ok = !recursive_lazy (snum);
   22574         4937 :   if (ok)
   22575              :     {
   22576         4937 :       ok = module->load_section (snum, mslot);
   22577         4937 :       lazy_snum = 0;
   22578         4937 :       post_load_processing ();
   22579              :     }
   22580              : 
   22581         4937 :   dump.pop (n);
   22582              : 
   22583         4937 :   function_depth--;
   22584              : 
   22585         4937 :   timevar_cond_stop (TV_MODULE_IMPORT, timer_running);
   22586              : 
   22587         4937 :   if (!ok)
   22588            0 :     fatal_error (input_location,
   22589            0 :                  module->is_header ()
   22590              :                  ? G_("failed to load binding %<%E%s%E%>")
   22591              :                  : G_("failed to load binding %<%E%s%E@%s%>"),
   22592            0 :                  ns, &"::"[ns == global_namespace ? 2 : 0], id,
   22593              :                  module->get_flatname ());
   22594              : 
   22595         4937 :   if (count != errorcount + warningcount)
   22596           27 :     inform (input_location,
   22597           27 :             module->is_header ()
   22598              :             ? G_("during load of binding %<%E%s%E%>")
   22599              :             : G_("during load of binding %<%E%s%E@%s%>"),
   22600           27 :             ns, &"::"[ns == global_namespace ? 2 : 0], id,
   22601              :             module->get_flatname ());
   22602         4937 : }
   22603              : 
   22604              : /* Load any pending entities keyed to NS and NAME.
   22605              :    Used to find pending types if we don't yet have a decl built.  */
   22606              : 
   22607              : void
   22608     27976436 : lazy_load_pendings (tree ns, tree name)
   22609              : {
   22610              :   /* Make sure lazy loading from a template context behaves as if
   22611              :      from a non-template context.  */
   22612     27976436 :   processing_template_decl_sentinel ptds;
   22613              : 
   22614     27976436 :   pending_key key;
   22615     27976436 :   key.ns = ns;
   22616     27976436 :   key.id = name;
   22617              : 
   22618     27976436 :   auto *pending_vec = pending_table ? pending_table->get (key) : nullptr;
   22619     27971624 :   if (!pending_vec)
   22620     27970427 :     return;
   22621              : 
   22622         6009 :   int count = errorcount + warningcount;
   22623              : 
   22624         6009 :   bool timer_running = timevar_cond_start (TV_MODULE_IMPORT);
   22625         6009 :   bool ok = !recursive_lazy ();
   22626         6009 :   if (ok)
   22627              :     {
   22628         6009 :       function_depth++; /* Prevent GC */
   22629         6009 :       unsigned n = dump.push (NULL);
   22630         6475 :       dump () && dump ("Reading %u pending entities keyed to %P",
   22631              :                        pending_vec->length (), key.ns, key.id);
   22632         6009 :       for (unsigned ix = pending_vec->length (); ix--;)
   22633              :         {
   22634        68371 :           unsigned index = (*pending_vec)[ix];
   22635        68371 :           binding_slot *slot = &(*entity_ary)[index];
   22636              : 
   22637        68371 :           if (slot->is_lazy ())
   22638              :             {
   22639         6429 :               module_state *import = import_entity_module (index);
   22640         6429 :               if (!import->lazy_load (index - import->entity_lwm, slot))
   22641        68371 :                 ok = false;
   22642              :             }
   22643       136322 :           else if (dump ())
   22644              :             {
   22645          338 :               module_state *import = import_entity_module (index);
   22646          338 :               dump () && dump ("Entity %M[%u] already loaded",
   22647          338 :                                import, index - import->entity_lwm);
   22648              :             }
   22649              :         }
   22650              : 
   22651         6009 :       pending_table->remove (key);
   22652         6009 :       dump.pop (n);
   22653         6009 :       lazy_snum = 0;
   22654         6009 :       post_load_processing ();
   22655         6009 :       function_depth--;
   22656              :     }
   22657              : 
   22658         6009 :   timevar_cond_stop (TV_MODULE_IMPORT, timer_running);
   22659              : 
   22660         6009 :   if (!ok)
   22661            0 :     fatal_error (input_location, "failed to load pendings for %<%E%s%E%>",
   22662            0 :                  key.ns, &"::"[key.ns == global_namespace ? 2 : 0], key.id);
   22663              : 
   22664         6009 :   if (count != errorcount + warningcount)
   22665            0 :     inform (input_location, "during load of pendings for %<%E%s%E%>",
   22666            0 :             key.ns, &"::"[key.ns == global_namespace ? 2 : 0], key.id);
   22667     27976436 : }
   22668              : 
   22669              : /* Load any pending entities keyed to the top-key of DECL.  */
   22670              : 
   22671              : void
   22672     27901828 : lazy_load_pendings (tree decl)
   22673              : {
   22674     27901828 :   tree key_decl;
   22675     27901828 :   tree ns = find_pending_key (decl, &key_decl);
   22676     27901828 :   return lazy_load_pendings (ns, DECL_NAME (key_decl));
   22677              : }
   22678              : 
   22679              : static void
   22680         2688 : direct_import (module_state *import, cpp_reader *reader)
   22681              : {
   22682         2688 :   timevar_start (TV_MODULE_IMPORT);
   22683         2688 :   unsigned n = dump.push (import);
   22684              : 
   22685         2688 :   gcc_checking_assert (import->is_direct () && import->has_location ());
   22686         2688 :   if (import->loadedness == ML_NONE)
   22687         1763 :     if (!import->do_import (reader, true))
   22688            0 :       gcc_unreachable ();
   22689              : 
   22690         2651 :   this_module ()->set_import (import, import->exported_p);
   22691              : 
   22692         2651 :   if (import->loadedness < ML_LANGUAGE)
   22693              :     {
   22694         2570 :       if (!keyed_table)
   22695         2258 :         keyed_table = new keyed_map_t (EXPERIMENT (1, 400));
   22696         2570 :       import->read_language (true);
   22697              :     }
   22698              : 
   22699         2651 :   dump.pop (n);
   22700         2651 :   timevar_stop (TV_MODULE_IMPORT);
   22701         2651 : }
   22702              : 
   22703              : /* Import module IMPORT.  */
   22704              : 
   22705              : void
   22706         2458 : import_module (module_state *import, location_t from_loc, bool exporting_p,
   22707              :                tree, cpp_reader *reader)
   22708              : {
   22709              :   /* A non-partition implementation unit has no name.  */
   22710         2458 :   if (!this_module ()->name && this_module ()->parent == import)
   22711              :     {
   22712            3 :       auto_diagnostic_group d;
   22713            3 :       error_at (from_loc, "import of %qs within its own implementation unit",
   22714              :                 import->get_flatname());
   22715            3 :       inform (import->loc, "module declared here");
   22716            3 :       return;
   22717            3 :     }
   22718              : 
   22719         2455 :   if (!import->check_circular_import (from_loc))
   22720              :     return;
   22721              : 
   22722         2449 :   if (!import->is_header () && current_lang_depth ())
   22723              :     /* Only header units should appear inside language
   22724              :        specifications.  The std doesn't specify this, but I think
   22725              :        that's an error in resolving US 033, because language linkage
   22726              :        is also our escape clause to getting things into the global
   22727              :        module, so we don't want to confuse things by having to think
   22728              :        about whether 'extern "C++" { import foo; }' puts foo's
   22729              :        contents into the global module all of a sudden.  */
   22730            6 :     warning (0, "import of named module %qs inside language-linkage block",
   22731              :              import->get_flatname ());
   22732              : 
   22733         2449 :   if (exporting_p || module_exporting_p ())
   22734          314 :     import->exported_p = true;
   22735              : 
   22736         2449 :   if (import->loadedness != ML_NONE)
   22737              :     {
   22738          922 :       from_loc = ordinary_loc_of (line_table, from_loc);
   22739          922 :       linemap_module_reparent (line_table, import->loc, from_loc);
   22740              :     }
   22741              : 
   22742         2449 :   gcc_checking_assert (import->is_direct () && import->has_location ());
   22743              : 
   22744         2449 :   direct_import (import, reader);
   22745              : }
   22746              : 
   22747              : /* Declare the name of the current module to be NAME.  EXPORTING_p is
   22748              :    true if this TU is the exporting module unit.  */
   22749              : 
   22750              : void
   22751         3080 : declare_module (module_state *module, location_t from_loc, bool exporting_p,
   22752              :                 tree, cpp_reader *reader)
   22753              : {
   22754         3080 :   gcc_assert (global_namespace == current_scope ());
   22755              : 
   22756         3080 :   module_state *current = this_module ();
   22757         3080 :   if (module_purview_p () || module->loadedness > ML_CONFIG)
   22758              :     {
   22759            6 :       auto_diagnostic_group d;
   22760           12 :       error_at (from_loc, module_purview_p ()
   22761              :                 ? G_("module already declared")
   22762              :                 : G_("module already imported"));
   22763            6 :       if (module_purview_p ())
   22764            0 :         module = current;
   22765           12 :       inform (module->loc, module_purview_p ()
   22766              :               ? G_("module %qs declared here")
   22767              :               : G_("module %qs imported here"),
   22768              :               module->get_flatname ());
   22769            6 :       return;
   22770            6 :     }
   22771              : 
   22772         3074 :   gcc_checking_assert (module->is_module ());
   22773         3074 :   gcc_checking_assert (module->is_direct () && module->has_location ());
   22774              : 
   22775              :   /* Yer a module, 'arry.  */
   22776         3074 :   module_kind = module->is_header () ? MK_HEADER : MK_NAMED | MK_ATTACH;
   22777              : 
   22778              :   // Even in header units, we consider the decls to be purview
   22779         3074 :   module_kind |= MK_PURVIEW;
   22780              : 
   22781         3074 :   if (module->is_partition ())
   22782          196 :     module_kind |= MK_PARTITION;
   22783         3074 :   if (exporting_p)
   22784              :     {
   22785         2770 :       module->interface_p = true;
   22786         2770 :       module_kind |= MK_INTERFACE;
   22787              :     }
   22788              : 
   22789         3074 :   if (module_has_cmi_p ())
   22790              :     {
   22791              :       /* Copy the importing information we may have already done.  We
   22792              :          do not need to separate out the imports that only happen in
   22793              :          the GMF, inspite of what the literal wording of the std
   22794              :          might imply.  See p2191, the core list had a discussion
   22795              :          where the module implementors agreed that the GMF of a named
   22796              :          module is invisible to importers.  */
   22797         2835 :       module->imports = current->imports;
   22798              : 
   22799         2835 :       module->mod = 0;
   22800         2835 :       (*modules)[0] = module;
   22801              :     }
   22802              :   else
   22803              :     {
   22804          239 :       module->interface_p = true;
   22805          239 :       current->parent = module; /* So mangler knows module identity. */
   22806          239 :       direct_import (module, reader);
   22807              :     }
   22808              : }
   22809              : 
   22810              : /* Return true IFF we must emit a module global initializer function
   22811              :    (which will be called by importers' init code).  */
   22812              : 
   22813              : bool
   22814       103712 : module_global_init_needed ()
   22815              : {
   22816       103712 :   return module_has_cmi_p () && !header_module_p ();
   22817              : }
   22818              : 
   22819              : /* Calculate which, if any, import initializers need calling.  */
   22820              : 
   22821              : bool
   22822        96437 : module_determine_import_inits ()
   22823              : {
   22824        96437 :   if (!modules || header_module_p ())
   22825              :     return false;
   22826              : 
   22827              :   /* Prune active_init_p.  We need the same bitmap allocation
   22828              :      scheme as for the imports member.  */
   22829         3735 :   function_depth++; /* Disable GC.  */
   22830         3735 :   bitmap covered_imports (BITMAP_GGC_ALLOC ());
   22831              : 
   22832         3735 :   bool any = false;
   22833              : 
   22834              :   /* Because indirect imports are before their direct import, and
   22835              :      we're scanning the array backwards, we only need one pass!  */
   22836         6517 :   for (unsigned ix = modules->length (); --ix;)
   22837              :     {
   22838         2782 :       module_state *import = (*modules)[ix];
   22839              : 
   22840         2782 :       if (!import->active_init_p)
   22841              :         ;
   22842           52 :       else if (bitmap_bit_p (covered_imports, ix))
   22843            6 :         import->active_init_p = false;
   22844              :       else
   22845              :         {
   22846              :           /* Everything this imports is therefore handled by its
   22847              :              initializer, so doesn't need initializing by us.  */
   22848           46 :           bitmap_ior_into (covered_imports, import->imports);
   22849           46 :           any = true;
   22850              :         }
   22851              :     }
   22852         3735 :   function_depth--;
   22853              : 
   22854         3735 :   return any;
   22855              : }
   22856              : 
   22857              : /* Emit calls to each direct import's global initializer.  Including
   22858              :    direct imports of directly imported header units.  The initializers
   22859              :    of (static) entities in header units will be called by their
   22860              :    importing modules (for the instance contained within that), or by
   22861              :    the current TU (for the instances we've brought in).  Of course
   22862              :    such header unit behaviour is evil, but iostream went through that
   22863              :    door some time ago.  */
   22864              : 
   22865              : void
   22866           46 : module_add_import_initializers ()
   22867              : {
   22868           46 :   if (!modules || header_module_p ())
   22869            0 :     return;
   22870              : 
   22871           46 :   tree fntype = build_function_type (void_type_node, void_list_node);
   22872           46 :   releasing_vec args;  // There are no args
   22873              : 
   22874          104 :   for (unsigned ix = modules->length (); --ix;)
   22875              :     {
   22876           58 :       module_state *import = (*modules)[ix];
   22877           58 :       if (import->active_init_p)
   22878              :         {
   22879           46 :           tree name = mangle_module_global_init (ix);
   22880           46 :           tree fndecl = build_lang_decl (FUNCTION_DECL, name, fntype);
   22881              : 
   22882           46 :           DECL_CONTEXT (fndecl) = FROB_CONTEXT (global_namespace);
   22883           46 :           SET_DECL_ASSEMBLER_NAME (fndecl, name);
   22884           46 :           TREE_PUBLIC (fndecl) = true;
   22885           46 :           determine_visibility (fndecl);
   22886              : 
   22887           46 :           tree call = cp_build_function_call_vec (fndecl, &args,
   22888              :                                                   tf_warning_or_error);
   22889           46 :           finish_expr_stmt (call);
   22890              :         }
   22891              :     }
   22892           46 : }
   22893              : 
   22894              : /* NAME & LEN are a preprocessed header name, possibly including the
   22895              :    surrounding "" or <> characters.  Return the raw string name of the
   22896              :    module to which it refers.  This will be an absolute path, or begin
   22897              :    with ./, so it is immediately distinguishable from a (non-header
   22898              :    unit) module name.  If READER is non-null, ask the preprocessor to
   22899              :    locate the header to which it refers using the appropriate include
   22900              :    path.  Note that we do never do \ processing of the string, as that
   22901              :    matches the preprocessor's behaviour.  */
   22902              : 
   22903              : static const char *
   22904        20353 : canonicalize_header_name (cpp_reader *reader, location_t loc, bool unquoted,
   22905              :                           const char *str, size_t &len_r)
   22906              : {
   22907        20353 :   size_t len = len_r;
   22908        20353 :   static char *buf = 0;
   22909        20353 :   static size_t alloc = 0;
   22910              : 
   22911        20353 :   if (!unquoted)
   22912              :     {
   22913            4 :       gcc_checking_assert (len >= 2
   22914              :                            && ((reader && str[0] == '<' && str[len-1] == '>')
   22915              :                                || (str[0] == '"' && str[len-1] == '"')));
   22916            4 :       str += 1;
   22917            4 :       len -= 2;
   22918              :     }
   22919              : 
   22920        20353 :   if (reader)
   22921              :     {
   22922            4 :       gcc_assert (!unquoted);
   22923              : 
   22924            4 :       if (len >= alloc)
   22925              :         {
   22926            4 :           alloc = len + 1;
   22927            4 :           buf = XRESIZEVEC (char, buf, alloc);
   22928              :         }
   22929            4 :       memcpy (buf, str, len);
   22930            4 :       buf[len] = 0;
   22931              : 
   22932            8 :       if (const char *hdr
   22933            4 :           = cpp_probe_header_unit (reader, buf, str[-1] == '<', loc))
   22934              :         {
   22935            4 :           len = strlen (hdr);
   22936            4 :           str = hdr;
   22937              :         }
   22938              :       else
   22939            0 :         str = buf;
   22940              :     }
   22941              : 
   22942        20353 :   if (!(str[0] == '.' ? IS_DIR_SEPARATOR (str[1]) : IS_ABSOLUTE_PATH (str)))
   22943              :     {
   22944              :       /* Prepend './'  */
   22945            9 :       if (len + 3 > alloc)
   22946              :         {
   22947            9 :           alloc = len + 3;
   22948            9 :           buf = XRESIZEVEC (char, buf, alloc);
   22949              :         }
   22950              : 
   22951            9 :       buf[0] = '.';
   22952            9 :       buf[1] = DIR_SEPARATOR;
   22953            9 :       memmove (buf + 2, str, len);
   22954            9 :       len += 2;
   22955            9 :       buf[len] = 0;
   22956            9 :       str = buf;
   22957              :     }
   22958              : 
   22959        20353 :   len_r = len;
   22960        20353 :   return str;
   22961              : }
   22962              : 
   22963              : /* Set the CMI name from a cody packet.  Issue an error if
   22964              :    ill-formed.  */
   22965              : 
   22966         5656 : void module_state::set_filename (const Cody::Packet &packet)
   22967              : {
   22968         5656 :   if (packet.GetCode () == Cody::Client::PC_PATHNAME)
   22969              :     {
   22970              :       /* If we've seen this import before we better have the same CMI.  */
   22971         5653 :       const std::string &path = packet.GetString ();
   22972         5653 :       if (!filename)
   22973         5650 :         filename = xstrdup (packet.GetString ().c_str ());
   22974            3 :       else if (filename != path)
   22975            0 :         error_at (loc, "mismatching compiled module interface: "
   22976              :                   "had %qs, got %qs", filename, path.c_str ());
   22977              :     }
   22978              :   else
   22979              :     {
   22980            3 :       gcc_checking_assert (packet.GetCode () == Cody::Client::PC_ERROR);
   22981            3 :       fatal_error (loc, "unknown compiled module interface: %s",
   22982            3 :                    packet.GetString ().c_str ());
   22983              :     }
   22984         5653 : }
   22985              : 
   22986              : /* The list of importable headers from C++ Table 24.  */
   22987              : 
   22988              : static const char *
   22989              : importable_headers[] =
   22990              :   {
   22991              :     "algorithm", "any", "array", "atomic",
   22992              :     "barrier", "bit", "bitset",
   22993              :     "charconv", "chrono", "compare", "complex", "concepts",
   22994              :     "condition_variable", "contracts", "coroutine",
   22995              :     "debugging", "deque",
   22996              :     "exception", "execution", "expected",
   22997              :     "filesystem", "flat_map", "flat_set", "format", "forward_list",
   22998              :     "fstream", "functional", "future",
   22999              :     "generator",
   23000              :     "hazard_pointer", "hive",
   23001              :     "initializer_list", "inplace_vector", "iomanip", "ios", "iosfwd",
   23002              :     "iostream", "istream", "iterator",
   23003              :     "latch", "limits", "linalg", "list", "locale",
   23004              :     "map", "mdspan", "memory", "memory_resource", "meta", "mutex",
   23005              :     "new", "numbers", "numeric",
   23006              :     "optional", "ostream",
   23007              :     "print",
   23008              :     "queue",
   23009              :     "random", "ranges", "ratio", "rcu", "regex",
   23010              :     "scoped_allocator", "semaphore", "set", "shared_mutex", "simd",
   23011              :     "source_location", "span", "spanstream", "sstream", "stack", "stacktrace",
   23012              :     "stdexcept", "stdfloat", "stop_token", "streambuf", "string",
   23013              :     "string_view", "syncstream", "system_error",
   23014              :     "text_encoding", "thread", "tuple", "type_traits", "typeindex", "typeinfo",
   23015              :     "unordered_map", "unordered_set",
   23016              :     "utility",
   23017              :     "valarray", "variant", "vector", "version"
   23018              :   };
   23019              : 
   23020              : /* True iff <name> is listed as an importable standard header.  */
   23021              : 
   23022              : static bool
   23023        18249 : is_importable_header (const char *name)
   23024              : {
   23025        18249 :   unsigned lo = 0;
   23026        18249 :   unsigned hi = ARRAY_SIZE (importable_headers);
   23027       138204 :   while (hi > lo)
   23028              :     {
   23029       122100 :       unsigned mid = (lo + hi)/2;
   23030       122100 :       int cmp = strcmp (name, importable_headers[mid]);
   23031       122100 :       if (cmp > 0)
   23032        28414 :         lo = mid + 1;
   23033        93686 :       else if (cmp < 0)
   23034              :         hi = mid;
   23035              :       else
   23036              :         return true;
   23037              :     }
   23038              :   return false;
   23039              : }
   23040              : 
   23041              : /* Figure out whether to treat HEADER as an include or an import.  */
   23042              : 
   23043              : static char *
   23044        19442 : maybe_translate_include (cpp_reader *reader, line_maps *lmaps, location_t loc,
   23045              :                          _cpp_file *file, bool angle, const char **alternate)
   23046              : {
   23047        19442 :   if (!modules_p ())
   23048              :     {
   23049              :       /* Turn off.  */
   23050            0 :       cpp_get_callbacks (reader)->translate_include = NULL;
   23051            0 :       return nullptr;
   23052              :     }
   23053              : 
   23054        19442 :   const char *path = _cpp_get_file_path (file);
   23055              : 
   23056        19442 :   dump.push (NULL);
   23057              : 
   23058        20936 :   dump () && dump ("Checking include translation '%s'", path);
   23059        19442 :   auto *mapper = get_mapper (cpp_main_loc (reader), cpp_get_deps (reader));
   23060              : 
   23061        19442 :   size_t len = strlen (path);
   23062        19442 :   path = canonicalize_header_name (NULL, loc, true, path, len);
   23063        19442 :   auto packet = mapper->IncludeTranslate (path, Cody::Flags::None, len);
   23064              : 
   23065        19442 :   enum class xlate_kind {
   23066              :     unknown, text, import, invalid
   23067        19442 :   } translate = xlate_kind::unknown;
   23068              : 
   23069        19442 :   if (packet.GetCode () == Cody::Client::PC_BOOL)
   23070        19393 :     translate = packet.GetInteger () ? xlate_kind::text : xlate_kind::unknown;
   23071           49 :   else if (packet.GetCode () == Cody::Client::PC_PATHNAME)
   23072              :     {
   23073              :       /* Record the CMI name for when we do the import.
   23074              :          We may already know about this import, but libcpp doesn't yet.  */
   23075           49 :       module_state *import = get_module (build_string (len, path));
   23076           49 :       import->set_filename (packet);
   23077           49 :       if (import->check_importable (reader))
   23078              :         translate = xlate_kind::import;
   23079              :       else
   23080            0 :         translate = xlate_kind::invalid;
   23081              :     }
   23082              :   else
   23083              :     {
   23084            0 :       gcc_checking_assert (packet.GetCode () == Cody::Client::PC_ERROR);
   23085            0 :       error_at (loc, "cannot determine %<#include%> translation of %s: %s",
   23086            0 :                 path, packet.GetString ().c_str ());
   23087              :     }
   23088              : 
   23089        19442 :   bool note = (translate == xlate_kind::invalid);
   23090        19442 :   if (note_include_translate_yes && translate == xlate_kind::import)
   23091              :     note = true;
   23092        19437 :   else if (note_include_translate_no && translate == xlate_kind::unknown)
   23093              :     note = true;
   23094        19434 :   else if (note_includes)
   23095              :     /* We do not expect the note_includes vector to be large, so O(N)
   23096              :        iteration.  */
   23097          420 :     for (unsigned ix = note_includes->length (); !note && ix--;)
   23098          210 :       if (!strcmp ((*note_includes)[ix], path))
   23099            1 :         note = true;
   23100              : 
   23101              :   /* Maybe try importing a different header instead.  */
   23102        19442 :   if (alternate && translate == xlate_kind::unknown)
   23103              :     {
   23104        19016 :       const char *fname = _cpp_get_file_name (file);
   23105              :       /* Redirect importable <name> to <bits/stdc++.h>.  */
   23106              :       /* ??? Generalize to use a .json.  */
   23107        19016 :       expanded_location eloc = expand_location (loc);
   23108        18249 :       if (angle && is_importable_header (fname)
   23109              :           /* Exclude <version> which often goes with import std.  */
   23110         2145 :           && strcmp (fname, "version") != 0
   23111              :           /* Don't redirect #includes between headers under the same include
   23112              :              path directory (i.e. between library headers); if the import
   23113              :              brings in the current file we then get redefinition errors.  */
   23114         2136 :           && !strstr (eloc.file, _cpp_get_file_dir (file)->name)
   23115              :           /* ??? These are needed when running a toolchain from the build
   23116              :              directory, because libsupc++ headers aren't linked into
   23117              :              libstdc++-v3/include with the other headers.  */
   23118          802 :           && !strstr (eloc.file, "libstdc++-v3/include")
   23119        19461 :           && !strstr (eloc.file, "libsupc++"))
   23120          378 :         *alternate = "bits/stdc++.h";
   23121              :     }
   23122              : 
   23123        19442 :   if (note)
   23124           12 :     inform (loc, translate == xlate_kind::import
   23125              :             ? G_("include %qs translated to import")
   23126              :             : translate == xlate_kind::invalid
   23127            3 :             ? G_("import of %qs failed, falling back to include")
   23128              :             : G_("include %qs processed textually"), path);
   23129              : 
   23130        22427 :   dump () && dump (translate == xlate_kind::import
   23131              :                    ? "Translating include to import"
   23132              :                    : "Keeping include as include");
   23133        19442 :   dump.pop (0);
   23134              : 
   23135        19442 :   if (translate != xlate_kind::import)
   23136              :     return nullptr;
   23137              : 
   23138              :   /* Create the translation text.  */
   23139           49 :   loc = ordinary_loc_of (lmaps, loc);
   23140           49 :   const line_map_ordinary *map
   23141           49 :     = linemap_check_ordinary (linemap_lookup (lmaps, loc));
   23142           49 :   unsigned col = SOURCE_COLUMN (map, loc);
   23143           49 :   col -= (col != 0); /* Columns are 1-based.  */
   23144              : 
   23145           49 :   unsigned alloc = len + col + 60;
   23146           49 :   char *res = XNEWVEC (char, alloc);
   23147              : 
   23148           49 :   strcpy (res, "__import");
   23149           49 :   unsigned actual = 8;
   23150           49 :   if (col > actual)
   23151              :     {
   23152              :       /* Pad out so the filename appears at the same position.  */
   23153           46 :       memset (res + actual, ' ', col - actual);
   23154           46 :       actual = col;
   23155              :     }
   23156              :   /* No need to encode characters, that's not how header names are
   23157              :      handled.  */
   23158           49 :   actual += snprintf (res + actual, alloc - actual,
   23159              :                       "\"%s\" [[__translated]];\n", path);
   23160           49 :   gcc_checking_assert (actual < alloc);
   23161              : 
   23162              :   /* cpplib will delete the buffer.  */
   23163              :   return res;
   23164        19442 : }
   23165              : 
   23166              : static void
   23167          907 : begin_header_unit (cpp_reader *reader)
   23168              : {
   23169              :   /* Set the module header name from the main_input_filename.  */
   23170          907 :   const char *main = main_input_filename;
   23171          907 :   size_t len = strlen (main);
   23172          907 :   main = canonicalize_header_name (NULL, 0, true, main, len);
   23173          907 :   module_state *module = get_module (build_string (len, main));
   23174              : 
   23175          907 :   preprocess_module (module, cpp_main_loc (reader), false, false, true, reader);
   23176          907 : }
   23177              : 
   23178              : /* We've just properly entered the main source file.  I.e. after the
   23179              :    command line, builtins and forced headers.  Record the line map and
   23180              :    location of this map.  Note we may be called more than once.  The
   23181              :    first call sticks.  */
   23182              : 
   23183              : void
   23184        98309 : module_begin_main_file (cpp_reader *reader, line_maps *lmaps,
   23185              :                        const line_map_ordinary *map)
   23186              : {
   23187        98309 :   gcc_checking_assert (lmaps == line_table);
   23188        98309 :   if (modules_p () && !spans.init_p ())
   23189              :     {
   23190         4808 :       unsigned n = dump.push (NULL);
   23191         4808 :       spans.init (lmaps, map);
   23192         4808 :       dump.pop (n);
   23193         4808 :       if (flag_header_unit && !cpp_get_options (reader)->preprocessed)
   23194              :         {
   23195              :           /* Tell the preprocessor this is an include file.  */
   23196          898 :           cpp_retrofit_as_include (reader);
   23197          898 :           begin_header_unit (reader);
   23198              :         }
   23199              :     }
   23200        98309 : }
   23201              : 
   23202              : /* Process the pending_import queue, making sure we know the
   23203              :    filenames.   */
   23204              : 
   23205              : static void
   23206         5699 : name_pending_imports (cpp_reader *reader)
   23207              : {
   23208         5699 :   auto *mapper = get_mapper (cpp_main_loc (reader), cpp_get_deps (reader));
   23209              : 
   23210         5699 :   if (!vec_safe_length (pending_imports))
   23211              :     /* Not doing anything.  */
   23212              :     return;
   23213              : 
   23214         4875 :   timevar_start (TV_MODULE_MAPPER);
   23215              : 
   23216         4875 :   auto n = dump.push (NULL);
   23217         5488 :   dump () && dump ("Resolving direct import names");
   23218         4875 :   bool want_deps = (bool (mapper->get_flags () & Cody::Flags::NameOnly)
   23219         4875 :                     || cpp_get_deps (reader));
   23220         4875 :   bool any = false;
   23221              : 
   23222        10666 :   for (unsigned ix = 0; ix != pending_imports->length (); ix++)
   23223              :     {
   23224         5791 :       module_state *module = (*pending_imports)[ix];
   23225         5791 :       gcc_checking_assert (module->is_direct ());
   23226         5791 :       if (!module->filename && !module->visited_p)
   23227              :         {
   23228         5679 :           bool export_p = (module->is_module ()
   23229         5679 :                            && (module->is_partition ()
   23230         2956 :                                || module->is_exported ()));
   23231              : 
   23232         5679 :           Cody::Flags flags = Cody::Flags::None;
   23233         5679 :           if (flag_preprocess_only
   23234         5679 :               && !(module->is_header () && !export_p))
   23235              :             {
   23236          141 :               if (!want_deps)
   23237           72 :                 continue;
   23238              :               flags = Cody::Flags::NameOnly;
   23239              :             }
   23240              : 
   23241         5607 :           if (!any)
   23242              :             {
   23243         4801 :               any = true;
   23244         4801 :               mapper->Cork ();
   23245              :             }
   23246         5607 :           if (export_p)
   23247         2889 :             mapper->ModuleExport (module->get_flatname (), flags);
   23248              :           else
   23249         2718 :             mapper->ModuleImport (module->get_flatname (), flags);
   23250         5607 :           module->visited_p = true;
   23251              :         }
   23252              :     }
   23253              : 
   23254         4875 :   if (any)
   23255              :     {
   23256         4801 :       auto response = mapper->Uncork ();
   23257         4801 :       auto r_iter = response.begin ();
   23258        10500 :       for (unsigned ix = 0; ix != pending_imports->length (); ix++)
   23259              :         {
   23260         5702 :           module_state *module = (*pending_imports)[ix];
   23261         5702 :           if (module->visited_p)
   23262              :             {
   23263         5607 :               module->visited_p = false;
   23264         5607 :               gcc_checking_assert (!module->filename);
   23265              : 
   23266         5607 :               module->set_filename (*r_iter);
   23267         5604 :               ++r_iter;
   23268              :             }
   23269              :         }
   23270         4798 :     }
   23271              : 
   23272         4872 :   dump.pop (n);
   23273              : 
   23274         4872 :   timevar_stop (TV_MODULE_MAPPER);
   23275              : }
   23276              : 
   23277              : /* We've just lexed a module-specific control line for MODULE.  Mark
   23278              :    the module as a direct import, and possibly load up its macro
   23279              :    state.  Returns the primary module, if this is a module
   23280              :    declaration.  */
   23281              : /* Perhaps we should offer a preprocessing mode where we read the
   23282              :    directives from the header unit, rather than require the header's
   23283              :    CMI.  */
   23284              : 
   23285              : module_state *
   23286         5811 : preprocess_module (module_state *module, location_t from_loc,
   23287              :                    bool in_purview, bool is_import, bool is_export,
   23288              :                    cpp_reader *reader)
   23289              : {
   23290         5811 :   if (!is_import)
   23291              :     {
   23292         3236 :       if (in_purview || module->loc)
   23293              :         {
   23294              :           /* We've already seen a module declaration.  If only preprocessing
   23295              :              then we won't complain in declare_module, so complain here.  */
   23296           42 :           if (flag_preprocess_only)
   23297            6 :             error_at (from_loc,
   23298              :                       in_purview
   23299              :                       ? G_("module already declared")
   23300              :                       : G_("module already imported"));
   23301              :           /* Always pretend this was an import to aid error recovery.  */
   23302              :           is_import = true;
   23303              :         }
   23304              :       else
   23305              :         {
   23306              :           /* Record it is the module.  */
   23307         3194 :           module->module_p = true;
   23308         3194 :           if (is_export)
   23309              :             {
   23310         2872 :               module->exported_p = true;
   23311         2872 :               module->interface_p = true;
   23312              :             }
   23313              :         }
   23314              :     }
   23315              : 
   23316         5811 :   if (module->directness < MD_DIRECT + in_purview)
   23317              :     {
   23318              :       /* Mark as a direct import.  */
   23319         5764 :       module->directness = module_directness (MD_DIRECT + in_purview);
   23320              : 
   23321              :       /* Set the location to be most informative for users.  */
   23322         5764 :       from_loc = ordinary_loc_of (line_table, from_loc);
   23323         5764 :       if (module->loadedness != ML_NONE)
   23324            6 :         linemap_module_reparent (line_table, module->loc, from_loc);
   23325              :       else
   23326              :         {
   23327              :           /* Don't overwrite the location if we're importing ourselves
   23328              :              after already having seen a module-declaration.  */
   23329         5758 :           if (!(is_import && module->is_module ()))
   23330         5728 :             module->loc = from_loc;
   23331         5758 :           if (!module->flatname)
   23332         5719 :             module->set_flatname ();
   23333              :         }
   23334              :     }
   23335              : 
   23336         5811 :   auto desired = ML_CONFIG;
   23337         5811 :   if (is_import
   23338         2617 :       && module->is_header ()
   23339         6716 :       && (!cpp_get_options (reader)->preprocessed
   23340            3 :           || cpp_get_options (reader)->directives_only))
   23341              :     /* We need preprocessor state now.  */
   23342              :     desired = ML_PREPROCESSOR;
   23343              : 
   23344         5811 :   if (!is_import || module->loadedness < desired)
   23345              :     {
   23346         5791 :       vec_safe_push (pending_imports, module);
   23347              : 
   23348         5791 :       if (desired == ML_PREPROCESSOR)
   23349              :         {
   23350          885 :           unsigned n = dump.push (NULL);
   23351              : 
   23352         1084 :           dump () && dump ("Reading %M preprocessor state", module);
   23353          885 :           name_pending_imports (reader);
   23354              : 
   23355              :           /* Preserve the state of the line-map.  */
   23356          885 :           auto pre_hwm = LINEMAPS_ORDINARY_USED (line_table);
   23357              : 
   23358              :           /* We only need to close the span, if we're going to emit a
   23359              :              CMI.  But that's a little tricky -- our token scanner
   23360              :              needs to be smarter -- and this isn't much state.
   23361              :              Remember, we've not parsed anything at this point, so
   23362              :              our module state flags are inadequate.  */
   23363          885 :           spans.maybe_init ();
   23364          885 :           spans.close ();
   23365              : 
   23366          885 :           timevar_start (TV_MODULE_IMPORT);
   23367              : 
   23368              :           /* Load the config of each pending import -- we must assign
   23369              :              module numbers monotonically.  */
   23370         1958 :           for (unsigned ix = 0; ix != pending_imports->length (); ix++)
   23371              :             {
   23372         1073 :               auto *import = (*pending_imports)[ix];
   23373         1250 :               if (!(import->is_module ()
   23374          177 :                     && (import->is_partition () || import->is_exported ()))
   23375          902 :                   && import->loadedness == ML_NONE
   23376         1974 :                   && (!flag_preprocess_only
   23377           51 :                       || (import->is_header ()
   23378              :                           /* Allow a missing/unimportable GCM with -MG.
   23379              :                              FIXME We should also try falling back to #include
   23380              :                              before giving up entirely.  */
   23381           42 :                           && (!cpp_get_options (reader)->deps.missing_files
   23382            3 :                               || import->check_importable (reader)))))
   23383              :                 {
   23384          889 :                   unsigned n = dump.push (import);
   23385          889 :                   import->do_import (reader, true);
   23386          889 :                   dump.pop (n);
   23387              :                 }
   23388              :             }
   23389          885 :           vec_free (pending_imports);
   23390              : 
   23391              :           /* Restore the line-map state.  */
   23392          885 :           spans.open (linemap_module_restore (line_table, pre_hwm));
   23393              : 
   23394              :           /* Now read the preprocessor state of this particular
   23395              :              import.  */
   23396          885 :           if (module->loadedness == ML_CONFIG
   23397          885 :               && module->read_preprocessor (true))
   23398          879 :             module->import_macros ();
   23399              : 
   23400          885 :           timevar_stop (TV_MODULE_IMPORT);
   23401              : 
   23402          885 :           dump.pop (n);
   23403              :         }
   23404              :     }
   23405              : 
   23406         5811 :   return is_import ? NULL : get_primary (module);
   23407              : }
   23408              : 
   23409              : /* We've completed phase-4 translation.  Emit any dependency
   23410              :    information for the not-yet-loaded direct imports, and fill in
   23411              :    their file names.  We'll have already loaded up the direct header
   23412              :    unit wavefront.  */
   23413              : 
   23414              : void
   23415         4814 : preprocessed_module (cpp_reader *reader)
   23416              : {
   23417         4814 :   unsigned n = dump.push (NULL);
   23418              : 
   23419         5391 :   dump () && dump ("Completed phase-4 (tokenization) processing");
   23420              : 
   23421         4814 :   name_pending_imports (reader);
   23422         4811 :   vec_free (pending_imports);
   23423              : 
   23424         4811 :   spans.maybe_init ();
   23425         4811 :   spans.close ();
   23426              : 
   23427         4811 :   using iterator = hash_table<module_state_hash>::iterator;
   23428         4811 :   if (mkdeps *deps = cpp_get_deps (reader))
   23429              :     {
   23430              :       /* Walk the module hash, informing the dependency machinery.  */
   23431           57 :       iterator end = modules_hash->end ();
   23432          342 :       for (iterator iter = modules_hash->begin (); iter != end; ++iter)
   23433              :         {
   23434          114 :           module_state *module = *iter;
   23435              : 
   23436          114 :           if (module->is_direct ())
   23437              :             {
   23438           90 :               if (module->is_module ()
   23439           90 :                   && (module->is_interface () || module->is_partition ()))
   23440           36 :                 deps_add_module_target (deps, module->get_flatname (),
   23441           36 :                                         maybe_add_cmi_prefix (module->filename),
   23442           36 :                                         module->is_header (),
   23443           36 :                                         module->is_exported ());
   23444              :               else
   23445           54 :                 deps_add_module_dep (deps, module->get_flatname ());
   23446              :             }
   23447              :         }
   23448              :     }
   23449              : 
   23450         4811 :   if (flag_header_unit && !flag_preprocess_only)
   23451              :     {
   23452              :       /* Find the main module -- remember, it's not yet in the module
   23453              :          array.  */
   23454          895 :       iterator end = modules_hash->end ();
   23455         1898 :       for (iterator iter = modules_hash->begin (); iter != end; ++iter)
   23456              :         {
   23457          949 :           module_state *module = *iter;
   23458          949 :           if (module->is_module ())
   23459              :             {
   23460          895 :               declare_module (module, cpp_main_loc (reader), true, NULL, reader);
   23461          895 :               module_kind |= MK_EXPORTING;
   23462          895 :               break;
   23463              :             }
   23464              :         }
   23465              :     }
   23466              : 
   23467         4811 :   dump.pop (n);
   23468         4811 : }
   23469              : 
   23470              : /* VAL is a global tree, add it to the global vec if it is
   23471              :    interesting.  Add some of its targets, if they too are
   23472              :    interesting.  We do not add identifiers, as they can be re-found
   23473              :    via the identifier hash table.  There is a cost to the number of
   23474              :    global trees.  */
   23475              : 
   23476              : static int
   23477      2983164 : maybe_add_global (tree val, unsigned &crc)
   23478              : {
   23479      2983164 :   int v = 0;
   23480              : 
   23481      2983164 :   if (val && !(identifier_p (val) || TREE_VISITED (val)))
   23482              :     {
   23483       917958 :       TREE_VISITED (val) = true;
   23484       917958 :       crc = crc32_unsigned (crc, fixed_trees->length ());
   23485       917958 :       vec_safe_push (fixed_trees, val);
   23486       917958 :       v++;
   23487              : 
   23488       917958 :       if (CODE_CONTAINS_STRUCT (TREE_CODE (val), TS_TYPED))
   23489       917958 :         v += maybe_add_global (TREE_TYPE (val), crc);
   23490       917958 :       if (CODE_CONTAINS_STRUCT (TREE_CODE (val), TS_TYPE_COMMON))
   23491       601750 :         v += maybe_add_global (TYPE_NAME (val), crc);
   23492              :     }
   23493              : 
   23494      2983164 :   return v;
   23495              : }
   23496              : 
   23497              : /* Initialize module state.  Create the hash table, determine the
   23498              :    global trees.  Create the module for current TU.  */
   23499              : 
   23500              : void
   23501         4814 : init_modules (cpp_reader *reader)
   23502              : {
   23503              :   /* PCH should not be reachable because of lang-specs, but the
   23504              :      user could have overriden that.  */
   23505         4814 :   if (pch_file)
   23506            0 :     fatal_error (input_location,
   23507              :                  "C++ modules are incompatible with precompiled headers");
   23508              : 
   23509         4814 :   if (cpp_get_options (reader)->traditional)
   23510            0 :     fatal_error (input_location,
   23511              :                  "C++ modules are incompatible with traditional preprocessing");
   23512              : 
   23513              :   /* :: is always exported.  */
   23514         4814 :   DECL_MODULE_EXPORT_P (global_namespace) = true;
   23515              : 
   23516         4814 :   modules_hash = hash_table<module_state_hash>::create_ggc (31);
   23517         4814 :   vec_safe_reserve (modules, 20);
   23518              : 
   23519              :   /* Create module for current TU.  */
   23520         4814 :   module_state *current
   23521         4814 :     = new (ggc_alloc<module_state> ()) module_state (NULL_TREE, NULL, false);
   23522         4814 :   current->mod = 0;
   23523         4814 :   bitmap_set_bit (current->imports, 0);
   23524         4814 :   modules->quick_push (current);
   23525              : 
   23526         4814 :   gcc_checking_assert (!fixed_trees);
   23527              : 
   23528         4814 :   headers = BITMAP_GGC_ALLOC ();
   23529              : 
   23530         4814 :   if (note_includes)
   23531              :     /* Canonicalize header names.  */
   23532            2 :     for (unsigned ix = 0; ix != note_includes->length (); ix++)
   23533              :       {
   23534            1 :         const char *hdr = (*note_includes)[ix];
   23535            1 :         size_t len = strlen (hdr);
   23536              : 
   23537            1 :         bool system = hdr[0] == '<';
   23538            1 :         bool user = hdr[0] == '"';
   23539            1 :         bool delimed = system || user;
   23540              : 
   23541            1 :         if (len <= (delimed ? 2 : 0)
   23542            1 :             || (delimed && hdr[len-1] != (system ? '>' : '"')))
   23543            0 :           error ("invalid header name %qs", hdr);
   23544              : 
   23545            1 :         hdr = canonicalize_header_name (delimed ? reader : NULL,
   23546              :                                         0, !delimed, hdr, len);
   23547            1 :         char *path = XNEWVEC (char, len + 1);
   23548            1 :         memcpy (path, hdr, len);
   23549            1 :         path[len] = 0;
   23550              : 
   23551            1 :         (*note_includes)[ix] = path;
   23552              :       }
   23553              : 
   23554         4814 :   if (note_cmis)
   23555              :     /* Canonicalize & mark module names.  */
   23556           12 :     for (unsigned ix = 0; ix != note_cmis->length (); ix++)
   23557              :       {
   23558            6 :         const char *name = (*note_cmis)[ix];
   23559            6 :         size_t len = strlen (name);
   23560              : 
   23561            6 :         bool is_system = name[0] == '<';
   23562            6 :         bool is_user = name[0] == '"';
   23563            6 :         bool is_pathname = false;
   23564            6 :         if (!(is_system || is_user))
   23565           12 :           for (unsigned ix = len; !is_pathname && ix--;)
   23566            9 :             is_pathname = IS_DIR_SEPARATOR (name[ix]);
   23567            6 :         if (is_system || is_user || is_pathname)
   23568              :           {
   23569            3 :             if (len <= (is_pathname ? 0 : 2)
   23570            3 :                 || (!is_pathname && name[len-1] != (is_system ? '>' : '"')))
   23571              :               {
   23572            0 :                 error ("invalid header name %qs", name);
   23573            0 :                 continue;
   23574              :               }
   23575              :             else
   23576            3 :               name = canonicalize_header_name (is_pathname ? nullptr : reader,
   23577              :                                                0, is_pathname, name, len);
   23578              :           }
   23579            6 :         if (auto module = get_module (name))
   23580            6 :           module->inform_cmi_p = 1;
   23581              :         else
   23582            0 :           error ("invalid module name %qs", name);
   23583              :       }
   23584              : 
   23585         4814 :   dump.push (NULL);
   23586              : 
   23587              :   /* Determine lazy handle bound.  */
   23588         4814 :   {
   23589         4814 :     unsigned limit = 1000;
   23590              : #if HAVE_GETRLIMIT
   23591         4814 :     struct rlimit rlimit;
   23592         4814 :     if (!getrlimit (RLIMIT_NOFILE, &rlimit))
   23593              :       {
   23594         4814 :         lazy_hard_limit = (rlimit.rlim_max < 1000000
   23595         4814 :                            ? unsigned (rlimit.rlim_max) : 1000000);
   23596         4814 :         lazy_hard_limit = (lazy_hard_limit > LAZY_HEADROOM
   23597         4814 :                            ? lazy_hard_limit - LAZY_HEADROOM : 0);
   23598         4814 :         if (rlimit.rlim_cur < limit)
   23599            0 :           limit = unsigned (rlimit.rlim_cur);
   23600              :       }
   23601              : #endif
   23602         4814 :     limit = limit > LAZY_HEADROOM ? limit - LAZY_HEADROOM : 1;
   23603              : 
   23604         4814 :     if (unsigned parm = param_lazy_modules)
   23605              :       {
   23606         4814 :         if (parm <= limit || !lazy_hard_limit || !try_increase_lazy (parm))
   23607            6 :           lazy_limit = parm;
   23608              :       }
   23609              :     else
   23610            0 :       lazy_limit = limit;
   23611              :   }
   23612              : 
   23613         4814 :   if (dump ())
   23614              :     {
   23615          577 :       verstr_t ver;
   23616          577 :       version2string (MODULE_VERSION, ver);
   23617          577 :       dump ("Source: %s", main_input_filename);
   23618          577 :       dump ("Compiler: %s", version_string);
   23619          577 :       dump ("Modules: %s", ver);
   23620          577 :       dump ("Checking: %s",
   23621              : #if CHECKING_P
   23622              :             "checking"
   23623              : #elif ENABLE_ASSERT_CHECKING
   23624              :             "asserting"
   23625              : #else
   23626              :             "release"
   23627              : #endif
   23628              :             );
   23629          577 :       dump ("Compiled by: "
   23630              : #ifdef __GNUC__
   23631              :             "GCC %d.%d, %s", __GNUC__, __GNUC_MINOR__,
   23632              : #ifdef __OPTIMIZE__
   23633              :             "optimizing"
   23634              : #else
   23635              :             "not optimizing"
   23636              : #endif
   23637              : #else
   23638              :             "not GCC"
   23639              : #endif
   23640              :             );
   23641          577 :       dump ("Reading: %s", MAPPED_READING ? "mmap" : "fileio");
   23642          577 :       dump ("Writing: %s", MAPPED_WRITING ? "mmap" : "fileio");
   23643          577 :       dump ("Lazy limit: %u", lazy_limit);
   23644          577 :       dump ("Lazy hard limit: %u", lazy_hard_limit);
   23645          577 :       dump ("");
   23646              :     }
   23647              : 
   23648              :   /* Construct the global tree array.  This is an array of unique
   23649              :      global trees (& types).  Do this now, rather than lazily, as
   23650              :      some global trees are lazily created and we don't want that to
   23651              :      mess with our syndrome of fixed trees.  */
   23652         4814 :   unsigned crc = 0;
   23653         4814 :   vec_alloc (fixed_trees, 250);
   23654              : 
   23655         5391 :   dump () && dump ("+Creating globals");
   23656              :   /* Insert the TRANSLATION_UNIT_DECL.  */
   23657         4814 :   TREE_VISITED (DECL_CONTEXT (global_namespace)) = true;
   23658         4814 :   fixed_trees->quick_push (DECL_CONTEXT (global_namespace));
   23659        28884 :   for (unsigned jx = 0; global_tree_arys[jx].first; jx++)
   23660              :     {
   23661        24070 :       const tree *ptr = global_tree_arys[jx].first;
   23662        24070 :       unsigned limit = global_tree_arys[jx].second;
   23663              : 
   23664      1473084 :       for (unsigned ix = 0; ix != limit; ix++, ptr++)
   23665              :         {
   23666      1449014 :           !(ix & 31) && dump ("") && dump ("+\t%u:%u:", jx, ix);
   23667      1449014 :           unsigned v = maybe_add_global (*ptr, crc);
   23668      1622691 :           dump () && dump ("+%u", v);
   23669              :         }
   23670              :     }
   23671              :   /* OS- and machine-specific types are dynamically registered at
   23672              :      runtime, so cannot be part of global_tree_arys.  */
   23673         4814 :   registered_builtin_types && dump ("") && dump ("+\tB:");
   23674        19256 :   for (tree t = registered_builtin_types; t; t = TREE_CHAIN (t))
   23675              :     {
   23676        14442 :       unsigned v = maybe_add_global (TREE_VALUE (t), crc);
   23677        16173 :       dump () && dump ("+%u", v);
   23678              :     }
   23679         4814 :   global_crc = crc32_unsigned (crc, fixed_trees->length ());
   23680         4814 :   dump ("") && dump ("Created %u unique globals, crc=%x",
   23681              :                      fixed_trees->length (), global_crc);
   23682       927586 :   for (unsigned ix = fixed_trees->length (); ix--;)
   23683       922772 :     TREE_VISITED ((*fixed_trees)[ix]) = false;
   23684              : 
   23685         4814 :   dump.pop (0);
   23686              : 
   23687         4814 :   if (!flag_module_lazy)
   23688              :     /* Get the mapper now, if we're not being lazy.  */
   23689          296 :     get_mapper (cpp_main_loc (reader), cpp_get_deps (reader));
   23690              : 
   23691         4814 :   if (!flag_preprocess_only)
   23692              :     {
   23693         4670 :       pending_table = new pending_map_t (EXPERIMENT (1, 400));
   23694         4670 :       entity_map = new entity_map_t (EXPERIMENT (1, 400));
   23695         4670 :       vec_safe_reserve (entity_ary, EXPERIMENT (1, 400));
   23696         4670 :       imported_temploid_friends
   23697         4670 :         = decl_tree_cache_map::create_ggc (EXPERIMENT (1, 400));
   23698              :     }
   23699              : 
   23700              : #if CHECKING_P
   23701         4814 :   note_defs = note_defs_table_t::create_ggc (1000);
   23702              : #endif
   23703              : 
   23704         4814 :   if (flag_header_unit && cpp_get_options (reader)->preprocessed)
   23705            9 :     begin_header_unit (reader);
   23706              : 
   23707              :   /* Collect here to make sure things are tagged correctly (when
   23708              :      aggressively GC'd).  */
   23709         4814 :   ggc_collect ();
   23710         4814 : }
   23711              : 
   23712              : /* If NODE is a deferred macro, load it.  */
   23713              : 
   23714              : static int
   23715        82925 : load_macros (cpp_reader *reader, cpp_hashnode *node, void *)
   23716              : {
   23717        82925 :   location_t main_loc
   23718        82925 :     = MAP_START_LOCATION (LINEMAPS_ORDINARY_MAP_AT (line_table, 0));
   23719              : 
   23720        82925 :   if (cpp_user_macro_p (node)
   23721        82925 :       && !node->value.macro)
   23722              :     {
   23723           72 :       cpp_macro *macro = cpp_get_deferred_macro (reader, node, main_loc);
   23724           72 :       dump () && dump ("Loaded macro #%s %I",
   23725              :                        macro ? "define" : "undef", identifier (node));
   23726              :     }
   23727              : 
   23728        82925 :   return 1;
   23729              : }
   23730              : 
   23731              : /* At the end of tokenizing, we no longer need the macro tables of
   23732              :    imports.  But the user might have requested some checking.  */
   23733              : 
   23734              : void
   23735        96644 : maybe_check_all_macros (cpp_reader *reader)
   23736              : {
   23737        96644 :   if (!warn_imported_macros)
   23738              :     return;
   23739              : 
   23740              :   /* Force loading of any remaining deferred macros.  This will
   23741              :      produce diagnostics if they are ill-formed.  */
   23742           21 :   unsigned n = dump.push (NULL);
   23743           21 :   cpp_forall_identifiers (reader, load_macros, NULL);
   23744           21 :   dump.pop (n);
   23745              : }
   23746              : 
   23747              : // State propagated from finish_module_processing to fini_modules
   23748              : 
   23749              : struct module_processing_cookie
   23750              : {
   23751              :   elf_out out;
   23752              :   module_state_config config;
   23753              :   char *cmi_name;
   23754              :   char *tmp_name;
   23755              :   unsigned crc;
   23756              :   bool began;
   23757              : 
   23758         2829 :   module_processing_cookie (char *cmi, char *tmp, int fd, int e)
   23759         2829 :     : out (fd, e), cmi_name (cmi), tmp_name (tmp), crc (0), began (false)
   23760              :   {
   23761              :   }
   23762         2829 :   ~module_processing_cookie ()
   23763              :   {
   23764         2829 :     XDELETEVEC (tmp_name);
   23765         2829 :     XDELETEVEC (cmi_name);
   23766         2829 :   }
   23767              : };
   23768              : 
   23769              : /* Write the CMI, if we're a module interface.  */
   23770              : 
   23771              : void *
   23772        96437 : finish_module_processing (cpp_reader *reader)
   23773              : {
   23774        96437 :   module_processing_cookie *cookie = nullptr;
   23775              : 
   23776        96437 :   if (header_module_p ())
   23777          895 :     module_kind &= ~MK_EXPORTING;
   23778              : 
   23779        96437 :   if (!modules || !this_module ()->name)
   23780              :     {
   23781        93605 :       if (flag_module_only)
   23782            6 :         warning (0, "%<-fmodule-only%> used for non-interface");
   23783              :     }
   23784         2832 :   else if (!flag_syntax_only)
   23785              :     {
   23786         2829 :       int fd = -1;
   23787         2829 :       int e = -1;
   23788              : 
   23789         2829 :       timevar_start (TV_MODULE_EXPORT);
   23790              : 
   23791              :       /* Force a valid but empty line map at the end.  This simplifies
   23792              :          the line table preparation and writing logic.  */
   23793         2829 :       linemap_add (line_table, LC_ENTER, false, "", 0);
   23794              : 
   23795              :       /* We write to a tmpname, and then atomically rename.  */
   23796         2829 :       char *cmi_name = NULL;
   23797         2829 :       char *tmp_name = NULL;
   23798         2829 :       module_state *state = this_module ();
   23799              : 
   23800         2829 :       unsigned n = dump.push (state);
   23801         2829 :       state->announce ("creating");
   23802         2829 :       if (state->filename)
   23803              :         {
   23804         2829 :           size_t len = 0;
   23805         2829 :           cmi_name = xstrdup (maybe_add_cmi_prefix (state->filename, &len));
   23806         2829 :           tmp_name = XNEWVEC (char, len + 3);
   23807         2829 :           memcpy (tmp_name, cmi_name, len);
   23808         2829 :           strcpy (&tmp_name[len], "~");
   23809              : 
   23810         2829 :           if (!errorcount)
   23811           29 :             for (unsigned again = 2; ; again--)
   23812              :               {
   23813         2749 :                 fd = open (tmp_name,
   23814              :                            O_RDWR | O_CREAT | O_TRUNC | O_CLOEXEC | O_BINARY,
   23815              :                            S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH);
   23816         2749 :                 e = errno;
   23817         2749 :                 if (fd >= 0 || !again || e != ENOENT)
   23818              :                   break;
   23819           29 :                 create_dirs (tmp_name);
   23820              :               }
   23821         2829 :           if (note_module_cmi_yes || state->inform_cmi_p)
   23822            3 :             inform (state->loc, "writing CMI %qs", cmi_name);
   23823         3126 :           dump () && dump ("CMI is %s", cmi_name);
   23824              :         }
   23825              : 
   23826         2829 :       cookie = new module_processing_cookie (cmi_name, tmp_name, fd, e);
   23827              : 
   23828         2829 :       if (errorcount)
   23829              :         /* Don't write the module if we have reported errors.  */;
   23830         2720 :       else if (erroneous_templates
   23831         2720 :                && !erroneous_templates->is_empty ())
   23832              :         {
   23833              :           /* Don't write the module if it contains an erroneous template.
   23834              :              Also emit notes about where errors occurred in case
   23835              :              -Wno-template-body was passed.  */
   23836            6 :           auto_diagnostic_group d;
   23837            6 :           error_at (state->loc, "not writing module %qs due to errors "
   23838              :                     "in template bodies", state->get_flatname ());
   23839            6 :           if (!warn_template_body)
   23840            3 :             inform (state->loc, "enable %<-Wtemplate-body%> for more details");
   23841           12 :           for (auto e : *erroneous_templates)
   23842            6 :             inform (e.second, "first error in %qD appeared here", e.first);
   23843            6 :         }
   23844         2714 :       else if (cookie->out.begin ())
   23845              :         {
   23846              :           /* So crashes finger-point the module decl.  */
   23847         2714 :           iloc_sentinel ils = state->loc;
   23848         2714 :           if (state->write_begin (&cookie->out, reader, cookie->config,
   23849         2714 :                                   cookie->crc))
   23850         2685 :             cookie->began = true;
   23851         2714 :         }
   23852              : 
   23853         2829 :       dump.pop (n);
   23854         2829 :       timevar_stop (TV_MODULE_EXPORT);
   23855              : 
   23856         2829 :       ggc_collect ();
   23857              :     }
   23858              : 
   23859        96437 :   if (modules)
   23860              :     {
   23861         4630 :       unsigned n = dump.push (NULL);
   23862         5207 :       dump () && dump ("Imported %u modules", modules->length () - 1);
   23863         5207 :       dump () && dump ("Containing %u clusters", available_clusters);
   23864         4630 :       dump () && dump ("Loaded %u clusters (%u%%)", loaded_clusters,
   23865          577 :                        (loaded_clusters * 100 + available_clusters / 2) /
   23866          577 :                        (available_clusters + !available_clusters));
   23867         4630 :       dump.pop (n);
   23868              :     }
   23869              : 
   23870        96437 :   return cookie;
   23871              : }
   23872              : 
   23873              : // Do the final emission of a module.  At this point we know whether
   23874              : // the module static initializer is a NOP or not.
   23875              : 
   23876              : static void
   23877         2829 : late_finish_module (cpp_reader *reader,  module_processing_cookie *cookie,
   23878              :                     bool init_fn_non_empty)
   23879              : {
   23880         2829 :   timevar_start (TV_MODULE_EXPORT);
   23881              : 
   23882         2829 :   module_state *state = this_module ();
   23883         2829 :   unsigned n = dump.push (state);
   23884         2829 :   state->announce ("finishing");
   23885              : 
   23886         2829 :   cookie->config.active_init = init_fn_non_empty;
   23887         2829 :   if (cookie->began)
   23888         2685 :     state->write_end (&cookie->out, reader, cookie->config, cookie->crc);
   23889              : 
   23890         2829 :   if (cookie->out.end () && cookie->cmi_name)
   23891              :     {
   23892              :       /* Some OS's do not replace NEWNAME if it already exists.
   23893              :          This'll have a race condition in erroneous concurrent
   23894              :          builds.  */
   23895         2720 :       unlink (cookie->cmi_name);
   23896         2720 :       if (rename (cookie->tmp_name, cookie->cmi_name))
   23897              :         {
   23898            0 :           dump () && dump ("Rename ('%s','%s') errno=%u",
   23899            0 :                            cookie->tmp_name, cookie->cmi_name, errno);
   23900            0 :           cookie->out.set_error (errno);
   23901              :         }
   23902              :     }
   23903              : 
   23904         2829 :   if (cookie->out.get_error () && cookie->began)
   23905              :     {
   23906            0 :       error_at (state->loc, "failed to write compiled module: %s",
   23907            0 :                 cookie->out.get_error (state->filename));
   23908            0 :       state->note_cmi_name ();
   23909              :     }
   23910              : 
   23911         2829 :   if (!errorcount)
   23912              :     {
   23913         2679 :       auto *mapper = get_mapper (cpp_main_loc (reader), cpp_get_deps (reader));
   23914         2679 :       mapper->ModuleCompiled (state->get_flatname ());
   23915              :     }
   23916          150 :   else if (cookie->cmi_name)
   23917              :     {
   23918              :       /* We failed, attempt to erase all evidence we even tried.  */
   23919          150 :       unlink (cookie->tmp_name);
   23920          150 :       unlink (cookie->cmi_name);
   23921              :     }
   23922              : 
   23923         2829 :   delete cookie;
   23924         2829 :   dump.pop (n);
   23925         2829 :   timevar_stop (TV_MODULE_EXPORT);
   23926         2829 : }
   23927              : 
   23928              : void
   23929        96437 : fini_modules (cpp_reader *reader, void *cookie, bool has_inits)
   23930              : {
   23931        96437 :   if (cookie)
   23932         2829 :     late_finish_module (reader,
   23933              :                         static_cast<module_processing_cookie *> (cookie),
   23934              :                         has_inits);
   23935              : 
   23936              :   /* We're done with the macro tables now.  */
   23937        96437 :   vec_free (macro_exports);
   23938        96437 :   vec_free (macro_imports);
   23939        96437 :   headers = NULL;
   23940              : 
   23941              :   /* We're now done with everything but the module names.  */
   23942        96437 :   set_cmi_repo (NULL);
   23943        96437 :   if (mapper)
   23944              :     {
   23945         4630 :       timevar_start (TV_MODULE_MAPPER);
   23946         4630 :       module_client::close_module_client (0, mapper);
   23947         4630 :       mapper = nullptr;
   23948         4630 :       timevar_stop (TV_MODULE_MAPPER);
   23949              :     }
   23950        96437 :   module_state_config::release ();
   23951              : 
   23952              : #if CHECKING_P
   23953        96437 :   note_defs = NULL;
   23954              : #endif
   23955              : 
   23956        96437 :   if (modules)
   23957         7517 :     for (unsigned ix = modules->length (); --ix;)
   23958         2887 :       if (module_state *state = (*modules)[ix])
   23959         2887 :         state->release ();
   23960              : 
   23961              :   /* No need to lookup modules anymore.  */
   23962        96437 :   modules_hash = NULL;
   23963              : 
   23964              :   /* Or entity array.  We still need the entity map to find import numbers.  */
   23965        96437 :   vec_free (entity_ary);
   23966        96437 :   entity_ary = NULL;
   23967              : 
   23968              :   /* Or remember any pending entities.  */
   23969       101067 :   delete pending_table;
   23970        96437 :   pending_table = NULL;
   23971              : 
   23972              :   /* Or any keys -- Let it go!  */
   23973        98847 :   delete keyed_table;
   23974        96437 :   keyed_table = NULL;
   23975              : 
   23976              :   /* Allow a GC, we've possibly made much data unreachable.  */
   23977        96437 :   ggc_collect ();
   23978        96437 : }
   23979              : 
   23980              : /* If CODE is a module option, handle it & return true.  Otherwise
   23981              :    return false.  For unknown reasons I cannot get the option
   23982              :    generation machinery to set fmodule-mapper or -fmodule-header to
   23983              :    make a string type option variable.  */
   23984              : 
   23985              : bool
   23986      1910302 : handle_module_option (unsigned code, const char *str, int)
   23987              : {
   23988      1910302 :   auto hdr = CMS_header;
   23989              : 
   23990      1910302 :   switch (opt_code (code))
   23991              :     {
   23992           42 :     case OPT_fmodule_mapper_:
   23993           42 :       module_mapper_name = str;
   23994           42 :       return true;
   23995              : 
   23996           12 :     case OPT_fmodule_header_:
   23997           12 :       {
   23998           12 :         if (!strcmp (str, "user"))
   23999              :           hdr = CMS_user;
   24000           12 :         else if (!strcmp (str, "system"))
   24001              :           hdr = CMS_system;
   24002              :         else
   24003            0 :           error ("unknown header kind %qs", str);
   24004              :       }
   24005              :       /* Fallthrough.  */
   24006              : 
   24007          910 :     case OPT_fmodule_header:
   24008          910 :       flag_header_unit = hdr;
   24009          910 :       flag_modules = 1;
   24010          910 :       return true;
   24011              : 
   24012            1 :     case OPT_flang_info_include_translate_:
   24013            1 :       vec_safe_push (note_includes, str);
   24014            1 :       return true;
   24015              : 
   24016            6 :     case OPT_flang_info_module_cmi_:
   24017            6 :       vec_safe_push (note_cmis, str);
   24018            6 :       return true;
   24019              : 
   24020              :     default:
   24021              :       return false;
   24022              :     }
   24023              : }
   24024              : 
   24025              : /* Set preprocessor callbacks and options for modules.  */
   24026              : 
   24027              : void
   24028        98118 : module_preprocess_options (cpp_reader *reader)
   24029              : {
   24030        98118 :   gcc_checking_assert (!lang_hooks.preprocess_undef);
   24031        98118 :   if (modules_p ())
   24032              :     {
   24033         4814 :       auto *cb = cpp_get_callbacks (reader);
   24034              : 
   24035         4814 :       cb->translate_include = maybe_translate_include;
   24036         4814 :       cb->user_deferred_macro = module_state::deferred_macro;
   24037         4814 :       if (flag_header_unit)
   24038              :         {
   24039              :           /* If the preprocessor hook is already in use, that
   24040              :              implementation will call the undef langhook.  */
   24041          907 :           if (cb->undef)
   24042            0 :             lang_hooks.preprocess_undef = module_state::undef_macro;
   24043              :           else
   24044          907 :             cb->undef = module_state::undef_macro;
   24045              :         }
   24046         4814 :       auto *opt = cpp_get_options (reader);
   24047         4814 :       opt->module_directives = true;
   24048         4814 :       if (flag_no_output)
   24049           18 :         opt->directives_only = true;
   24050         4814 :       if (opt->main_search == CMS_none)
   24051         4812 :         opt->main_search = cpp_main_search (flag_header_unit);
   24052              :     }
   24053        98118 : }
   24054              : 
   24055              : #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.