LCOV - code coverage report
Current view: top level - gcc/cp - module.cc (source / functions) Hit Total Coverage
Test: gcc.info Lines: 8898 9437 94.3 %
Date: 2023-07-19 08:18:47 Functions: 397 418 95.0 %

          Line data    Source code
       1             : /* C++ modules.  Experimental!
       2             :    Copyright (C) 2017-2023 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 - data streamer
     157             :    bytes_in : bytes - scalar reader
     158             :    bytes_out : bytes - scalar writer
     159             : 
     160             :    elf - ELROND format
     161             :    elf_in : elf - ELROND reader
     162             :    elf_out : elf - ELROND writer
     163             : 
     164             :    trees_in : bytes_in - tree reader
     165             :    trees_out : bytes_out - tree writer
     166             : 
     167             :    depset - dependency set
     168             :    depset::hash - hash table of depsets
     169             :    depset::tarjan - SCC determinator
     170             : 
     171             :    uidset<T> - set T's related to a UID
     172             :    uidset<T>::hash hash table of uidset<T>
     173             : 
     174             :    loc_spans - location map data
     175             : 
     176             :    module_state - module object
     177             : 
     178             :    slurping - data needed during loading
     179             : 
     180             :    macro_import - imported macro data
     181             :    macro_export - exported macro data
     182             : 
     183             :    The ELROND objects use mmap, for both reading and writing.  If mmap
     184             :    is unavailable, fileno IO is used to read and write blocks of data.
     185             : 
     186             :    The mapper object uses fileno IO to communicate with the server or
     187             :    program.   */
     188             : 
     189             : /* In expermental (trunk) sources, MODULE_VERSION is a #define passed
     190             :    in from the Makefile.  It records the modification date of the
     191             :    source directory -- that's the only way to stay sane.  In release
     192             :    sources, we (plan to) use the compiler's major.minor versioning.
     193             :    While the format might not change between at minor versions, it
     194             :    seems simplest to tie the two together.  There's no concept of
     195             :    inter-version compatibility.  */
     196             : #define IS_EXPERIMENTAL(V) ((V) >= (1U << 20))
     197             : #define MODULE_MAJOR(V) ((V) / 10000)
     198             : #define MODULE_MINOR(V) ((V) % 10000)
     199             : #define EXPERIMENT(A,B) (IS_EXPERIMENTAL (MODULE_VERSION) ? (A) : (B))
     200             : #ifndef MODULE_VERSION
     201             : #include "bversion.h"
     202             : #define MODULE_VERSION (BUILDING_GCC_MAJOR * 10000U + BUILDING_GCC_MINOR)
     203             : #elif !IS_EXPERIMENTAL (MODULE_VERSION)
     204             : #error "This is not the version I was looking for."
     205             : #endif
     206             : 
     207             : #define _DEFAULT_SOURCE 1 /* To get TZ field of struct tm, if available.  */
     208             : #include "config.h"
     209             : #define INCLUDE_MEMORY
     210             : #define INCLUDE_STRING
     211             : #define INCLUDE_VECTOR
     212             : #include "system.h"
     213             : #include "coretypes.h"
     214             : #include "cp-tree.h"
     215             : #include "timevar.h"
     216             : #include "stringpool.h"
     217             : #include "dumpfile.h"
     218             : #include "bitmap.h"
     219             : #include "cgraph.h"
     220             : #include "tree-iterator.h"
     221             : #include "cpplib.h"
     222             : #include "mkdeps.h"
     223             : #include "incpath.h"
     224             : #include "libiberty.h"
     225             : #include "stor-layout.h"
     226             : #include "version.h"
     227             : #include "tree-diagnostic.h"
     228             : #include "toplev.h"
     229             : #include "opts.h"
     230             : #include "attribs.h"
     231             : #include "intl.h"
     232             : #include "langhooks.h"
     233             : /* This TU doesn't need or want to see the networking.  */
     234             : #define CODY_NETWORKING 0
     235             : #include "mapper-client.h"
     236             : 
     237             : #if 0 // 1 for testing no mmap
     238             : #define MAPPED_READING 0
     239             : #define MAPPED_WRITING 0
     240             : #else
     241             : #if HAVE_MMAP_FILE && _POSIX_MAPPED_FILES > 0
     242             : /* mmap, munmap.  */
     243             : #define MAPPED_READING 1
     244             : #if HAVE_SYSCONF && defined (_SC_PAGE_SIZE)
     245             : /* msync, sysconf (_SC_PAGE_SIZE), ftruncate  */
     246             : /* posix_fallocate used if available.  */
     247             : #define MAPPED_WRITING 1
     248             : #else
     249             : #define MAPPED_WRITING 0
     250             : #endif
     251             : #else
     252             : #define MAPPED_READING 0
     253             : #define MAPPED_WRITING 0
     254             : #endif
     255             : #endif
     256             : 
     257             : /* Some open(2) flag differences, what a colourful world it is!  */
     258             : #if defined (O_CLOEXEC)
     259             : // OK
     260             : #elif defined (_O_NOINHERIT)
     261             : /* Windows' _O_NOINHERIT matches O_CLOEXEC flag */
     262             : #define O_CLOEXEC _O_NOINHERIT
     263             : #else
     264             : #define O_CLOEXEC 0
     265             : #endif
     266             : #if defined (O_BINARY)
     267             : // Ok?
     268             : #elif defined (_O_BINARY)
     269             : /* Windows' open(2) call defaults to text!  */
     270             : #define O_BINARY _O_BINARY
     271             : #else
     272             : #define O_BINARY 0
     273             : #endif
     274             : 
     275      389121 : static inline cpp_hashnode *cpp_node (tree id)
     276             : {
     277      389121 :   return CPP_HASHNODE (GCC_IDENT_TO_HT_IDENT (id));
     278             : }
     279             : 
     280      193226 : static inline tree identifier (const cpp_hashnode *node)
     281             : {
     282             :   /* HT_NODE() expands to node->ident that HT_IDENT_TO_GCC_IDENT()
     283             :      then subtracts a nonzero constant, deriving a pointer to
     284             :      a different member than ident.  That's strictly undefined
     285             :      and detected by -Warray-bounds.  Suppress it.  See PR 101372.  */
     286      193226 : #pragma GCC diagnostic push
     287      193226 : #pragma GCC diagnostic ignored "-Warray-bounds"
     288      193226 :   return HT_IDENT_TO_GCC_IDENT (HT_NODE (const_cast<cpp_hashnode *> (node)));
     289      193226 : #pragma GCC diagnostic pop
     290             : }
     291             : 
     292             : /* Id for dumping module information.  */
     293             : int module_dump_id;
     294             : 
     295             : /* We have a special module owner.  */
     296             : #define MODULE_UNKNOWN (~0U)    /* Not yet known.  */
     297             : 
     298             : /* Prefix for section names.  */
     299             : #define MOD_SNAME_PFX ".gnu.c++"
     300             : 
     301             : /* Format a version for user consumption.  */
     302             : 
     303             : typedef char verstr_t[32];
     304             : static void
     305        2806 : version2string (unsigned version, verstr_t &out)
     306             : {
     307        2806 :   unsigned major = MODULE_MAJOR (version);
     308        2806 :   unsigned minor = MODULE_MINOR (version);
     309             : 
     310        2806 :   if (IS_EXPERIMENTAL (version))
     311        2806 :     sprintf (out, "%04u/%02u/%02u-%02u:%02u%s",
     312        2806 :              2000 + major / 10000, (major / 100) % 100, (major % 100),
     313             :              minor / 100, minor % 100,
     314             :              EXPERIMENT ("", " (experimental)"));
     315             :   else
     316           0 :     sprintf (out, "%u.%u", major, minor);
     317        2806 : }
     318             : 
     319             : /* Include files to note translation for.  */
     320             : static vec<const char *, va_heap, vl_embed> *note_includes;
     321             : 
     322             : /* Modules to note CMI pathames.  */
     323             : static vec<const char *, va_heap, vl_embed> *note_cmis;
     324             : 
     325             : /* Traits to hash an arbitrary pointer.  Entries are not deletable,
     326             :    and removal is a noop (removal needed upon destruction).  */
     327             : template <typename T>
     328             : struct nodel_ptr_hash : pointer_hash<T>, typed_noop_remove <T *> {
     329             :   /* Nothing is deletable.  Everything is insertable.  */
     330             :   static bool is_deleted (T *) { return false; }
     331             :   static void mark_deleted (T *) { gcc_unreachable (); }
     332             : };
     333             : 
     334             : /* Map from pointer to signed integer.   */
     335             : typedef simple_hashmap_traits<nodel_ptr_hash<void>, int> ptr_int_traits;
     336             : typedef hash_map<void *,signed,ptr_int_traits> ptr_int_hash_map;
     337             : 
     338             : /********************************************************************/
     339             : /* Basic streaming & ELF.  Serialization is usually via mmap.  For
     340             :    writing we slide a buffer over the output file, syncing it
     341             :    approproiately.  For reading we simply map the whole file (as a
     342             :    file-backed read-only map -- it's just address space, leaving the
     343             :    OS pager to deal with getting the data to us).  Some buffers need
     344             :    to be more conventional malloc'd contents.   */
     345             : 
     346             : /* Variable length buffer.  */
     347             : 
     348             : class data {
     349             : public:
     350        1717 :   class allocator {
     351             :   public:
     352             :     /* Tools tend to moan if the dtor's not virtual.  */
     353       91017 :     virtual ~allocator () {}
     354             : 
     355             :   public:
     356             :     void grow (data &obj, unsigned needed, bool exact);
     357             :     void shrink (data &obj);
     358             : 
     359             :   public:
     360             :     virtual char *grow (char *ptr, unsigned needed);
     361             :     virtual void shrink (char *ptr);
     362             :   };
     363             : 
     364             : public:
     365             :   char *buffer;         /* Buffer being transferred.  */
     366             :   /* Although size_t would be the usual size, we know we never get
     367             :      more than 4GB of buffer -- because that's the limit of the
     368             :      encapsulation format.  And if you need bigger imports, you're
     369             :      doing it wrong.  */
     370             :   unsigned size;        /* Allocated size of buffer.  */
     371             :   unsigned pos;         /* Position in buffer.  */
     372             : 
     373             : public:
     374      967215 :   data ()
     375      967215 :     :buffer (NULL), size (0), pos (0)
     376             :   {
     377             :   }
     378      975966 :   ~data ()
     379             :   {
     380             :     /* Make sure the derived and/or using class know what they're
     381             :        doing.  */
     382      975966 :     gcc_checking_assert (!buffer);
     383      975966 :   }
     384             : 
     385             : protected:
     386   624796714 :   char *use (unsigned count)
     387             :   {
     388   624796714 :     if (size < pos + count)
     389             :       return NULL;
     390   624796714 :     char *res = &buffer[pos];
     391   624796714 :     pos += count;
     392   285932419 :     return res;
     393             :   }
     394             : 
     395             : public:
     396    47459559 :   void unuse (unsigned count)
     397             :   {
     398    47459559 :     pos -= count;
     399       17006 :   }
     400             : 
     401             : public:
     402             :   static allocator simple_memory;
     403             : };
     404             : 
     405             : /* The simple data allocator.  */
     406             : data::allocator data::simple_memory;
     407             : 
     408             : /* Grow buffer to at least size NEEDED.  */
     409             : 
     410             : void
     411      745549 : data::allocator::grow (data &obj, unsigned needed, bool exact)
     412             : {
     413      745549 :   gcc_checking_assert (needed ? needed > obj.size : !obj.size);
     414      745549 :   if (!needed)
     415             :     /* Pick a default size.  */
     416      318316 :     needed = EXPERIMENT (100, 1000);
     417             : 
     418      745549 :   if (!exact)
     419      740569 :     needed *= 2;
     420      745549 :   obj.buffer = grow (obj.buffer, needed);
     421      745549 :   if (obj.buffer)
     422      745549 :     obj.size = needed;
     423             :   else
     424           0 :     obj.pos = obj.size = 0;
     425      745549 : }
     426             : 
     427             : /* Free a buffer.  */
     428             : 
     429             : void
     430      326901 : data::allocator::shrink (data &obj)
     431             : {
     432           0 :   shrink (obj.buffer);
     433      326901 :   obj.buffer = NULL;
     434      326901 :   obj.size = 0;
     435           0 : }
     436             : 
     437             : char *
     438        6911 : data::allocator::grow (char *ptr, unsigned needed)
     439             : {
     440        6911 :   return XRESIZEVAR (char, ptr, needed);
     441             : }
     442             : 
     443             : void
     444        8585 : data::allocator::shrink (char *ptr)
     445             : {
     446        8585 :   XDELETEVEC (ptr);
     447        8585 : }
     448             : 
     449             : /* Byte streamer base.   Buffer with read/write position and smarts
     450             :    for single bits.  */
     451             : 
     452             : class bytes : public data {
     453             : public:
     454             :   typedef data parent;
     455             : 
     456             : protected:
     457             :   uint32_t bit_val;     /* Bit buffer.  */
     458             :   unsigned bit_pos;     /* Next bit in bit buffer.  */
     459             : 
     460             : public:
     461      963624 :   bytes ()
     462      963624 :     :parent (), bit_val (0), bit_pos (0)
     463             :   {}
     464      965376 :   ~bytes () 
     465             :   {
     466      965376 :   }
     467             : 
     468             : protected:
     469             :   unsigned calc_crc (unsigned) const;
     470             : 
     471             : protected:
     472             :   /* Finish bit packet.  Rewind the bytes not used.  */
     473    47425575 :   unsigned bit_flush ()
     474             :   {
     475    47425575 :     gcc_assert (bit_pos);
     476    47425575 :     unsigned bytes = (bit_pos + 7) / 8;
     477    47425575 :     unuse (4 - bytes);
     478    47425575 :     bit_pos = 0;
     479    47425575 :     bit_val = 0;
     480    47425575 :     return bytes;
     481             :   }
     482             : };
     483             : 
     484             : /* Calculate the crc32 of the buffer.  Note the CRC is stored in the
     485             :    first 4 bytes, so don't include them.  */
     486             : 
     487             : unsigned
     488      653891 : bytes::calc_crc (unsigned l) const
     489             : {
     490      653891 :   unsigned crc = 0;
     491   932688701 :   for (size_t ix = 4; ix < l; ix++)
     492   932034810 :     crc = crc32_byte (crc, buffer[ix]);
     493      653891 :   return crc;
     494             : }
     495             : 
     496             : class elf_in;
     497             : 
     498             : /* Byte stream reader.  */
     499             : 
     500             : class bytes_in : public bytes {
     501             :   typedef bytes parent;
     502             : 
     503             : protected:
     504             :   bool overrun;  /* Sticky read-too-much flag.  */
     505             : 
     506             : public:
     507      341580 :   bytes_in ()
     508      341580 :     : parent (), overrun (false)
     509             :   {
     510             :   }
     511      343332 :   ~bytes_in ()
     512             :   {
     513        7960 :   }
     514             : 
     515             : public:
     516             :   /* Begin reading a named section.  */
     517             :   bool begin (location_t loc, elf_in *src, const char *name);
     518             :   /* Begin reading a numbered section with optional name.  */
     519             :   bool begin (location_t loc, elf_in *src, unsigned, const char * = NULL);
     520             :   /* Complete reading a buffer.  Propagate errors and return true on
     521             :      success.  */
     522             :   bool end (elf_in *src);
     523             :   /* Return true if there is unread data.  */
     524     2450531 :   bool more_p () const
     525             :   {
     526     2450531 :     return pos != size;
     527             :   }
     528             : 
     529             : public:
     530             :   /* Start reading at OFFSET.  */
     531         530 :   void random_access (unsigned offset)
     532             :   {
     533         530 :     if (offset > size)
     534         530 :       set_overrun ();
     535         530 :     pos = offset;
     536         530 :     bit_pos = bit_val = 0;
     537             :   }
     538             : 
     539             : public:
     540     2190677 :   void align (unsigned boundary)
     541             :   {
     542     2190677 :     if (unsigned pad = pos & (boundary - 1))
     543     4253138 :       read (boundary - pad);
     544             :   }
     545             : 
     546             : public:
     547   338864295 :   const char *read (unsigned count)
     548             :   {
     549     2062461 :     char *ptr = use (count);
     550   338864295 :     if (!ptr)
     551   458286125 :       set_overrun ();
     552   277699328 :     return ptr;
     553             :   }
     554             : 
     555             : public:
     556             :   bool check_crc () const;
     557             :   /* We store the CRC in the first 4 bytes, using host endianness.  */
     558      342411 :   unsigned get_crc () const
     559             :   {
     560      342411 :     return *(const unsigned *)&buffer[0];
     561             :   }
     562             : 
     563             : public:
     564             :   /* Manipulate the overrun flag.  */
     565   201336923 :   bool get_overrun () const
     566             :   {
     567   201336923 :     return overrun;
     568             :   }
     569          14 :   void set_overrun ()
     570             :   {
     571          14 :     overrun = true;
     572           0 :   }
     573             : 
     574             : public:
     575             :   unsigned u32 ();      /* Read uncompressed integer.  */
     576             : 
     577             : public:
     578             :   bool b ();            /* Read a bool.  */
     579             :   void bflush ();       /* Completed a block of bools.  */
     580             : 
     581             : private:
     582             :   void bfill ();        /* Get the next block of bools.  */
     583             : 
     584             : public:
     585             :   int c ();             /* Read a char.  */
     586             :   int i ();             /* Read a signed int.  */
     587             :   unsigned u ();        /* Read an unsigned int.  */
     588             :   size_t z ();          /* Read a size_t.  */
     589             :   HOST_WIDE_INT wi ();  /* Read a HOST_WIDE_INT.  */
     590             :   unsigned HOST_WIDE_INT wu (); /* Read an unsigned HOST_WIDE_INT.  */
     591             :   const char *str (size_t * = NULL); /* Read a string.  */
     592             :   const void *buf (size_t); /* Read a fixed-length buffer.  */
     593             :   cpp_hashnode *cpp_node (); /* Read a cpp node.  */
     594             : };
     595             : 
     596             : /* Verify the buffer's CRC is correct.  */
     597             : 
     598             : bool
     599      340555 : bytes_in::check_crc () const
     600             : {
     601      340555 :   if (size < 4)
     602             :     return false;
     603             : 
     604      340555 :   unsigned c_crc = calc_crc (size);
     605      340555 :   if (c_crc != get_crc ())
     606             :     return false;
     607             : 
     608             :   return true;
     609             : }
     610             : 
     611             : class elf_out;
     612             : 
     613             : /* Byte stream writer.  */
     614             : 
     615             : class bytes_out : public bytes {
     616             :   typedef bytes parent;
     617             : 
     618             : public:
     619             :   allocator *memory;    /* Obtainer of memory.  */
     620             :   
     621             : public:
     622      622044 :   bytes_out (allocator *memory)
     623      622044 :     : parent (), memory (memory)
     624             :   {
     625             :   }
     626      622044 :   ~bytes_out ()
     627             :   {
     628     1244070 :   }
     629             : 
     630             : public:
     631   610160999 :   bool streaming_p () const
     632             :   {
     633   610160999 :     return memory != NULL;
     634             :   }
     635             : 
     636             : public:
     637             :   void set_crc (unsigned *crc_ptr);
     638             : 
     639             : public:
     640             :   /* Begin writing, maybe reserve space for CRC.  */
     641             :   void begin (bool need_crc = true);
     642             :   /* Finish writing.  Spill to section by number.  */
     643             :   unsigned end (elf_out *, unsigned, unsigned *crc_ptr = NULL);
     644             : 
     645             : public:
     646     1998407 :   void align (unsigned boundary)
     647             :   {
     648     1998407 :     if (unsigned pad = pos & (boundary - 1))
     649     1886252 :       write (boundary - pad);
     650     1998407 :   }
     651             :   
     652             : public:
     653   285932419 :   char *write (unsigned count, bool exact = false)
     654             :   {
     655   285932419 :     if (size < pos + count)
     656      418662 :       memory->grow (*this, pos + count, exact);
     657   285932419 :     return use (count);
     658             :   }
     659             : 
     660             : public:
     661             :   void u32 (unsigned);  /* Write uncompressed integer.  */
     662             : 
     663             : public:
     664             :   void b (bool);        /* Write bool.  */
     665             :   void bflush ();       /* Finish block of bools.  */
     666             : 
     667             : public:
     668             :   void c (unsigned char); /* Write unsigned char.  */
     669             :   void i (int);         /* Write signed int.  */
     670             :   void u (unsigned);    /* Write unsigned int.  */
     671             :   void z (size_t s);    /* Write size_t.  */
     672             :   void wi (HOST_WIDE_INT); /* Write HOST_WIDE_INT.  */
     673             :   void wu (unsigned HOST_WIDE_INT);  /* Write unsigned HOST_WIDE_INT.  */
     674       15944 :   void str (const char *ptr)
     675             :   {
     676       15944 :     str (ptr, strlen (ptr));
     677       15944 :   }
     678      364618 :   void cpp_node (const cpp_hashnode *node)
     679             :   {
     680      364618 :     str ((const char *)NODE_NAME (node), NODE_LEN (node));
     681       16289 :   }
     682             :   void str (const char *, size_t);  /* Write string of known length.  */
     683             :   void buf (const void *, size_t);  /* Write fixed length buffer.  */
     684             :   void *buf (size_t); /* Create a writable buffer */
     685             : 
     686             : public:
     687             :   /* Format a NUL-terminated raw string.  */
     688             :   void printf (const char *, ...) ATTRIBUTE_PRINTF_2;
     689             :   void print_time (const char *, const tm *, const char *);
     690             : 
     691             : public:
     692             :   /* Dump instrumentation.  */
     693             :   static void instrument ();
     694             : 
     695             : protected:
     696             :   /* Instrumentation.  */
     697             :   static unsigned spans[4];
     698             :   static unsigned lengths[4];
     699             :   static int is_set;
     700             : };
     701             : 
     702             : /* Instrumentation.  */
     703             : unsigned bytes_out::spans[4];
     704             : unsigned bytes_out::lengths[4];
     705             : int bytes_out::is_set = -1;
     706             : 
     707             : /* If CRC_PTR non-null, set the CRC of the buffer.  Mix the CRC into
     708             :    that pointed to by CRC_PTR.  */
     709             : 
     710             : void
     711      314996 : bytes_out::set_crc (unsigned *crc_ptr)
     712             : {
     713      314996 :   if (crc_ptr)
     714             :     {
     715      313336 :       gcc_checking_assert (pos >= 4);
     716             : 
     717      313336 :       unsigned crc = calc_crc (pos);
     718      313336 :       unsigned accum = *crc_ptr;
     719             :       /* Only mix the existing *CRC_PTR if it is non-zero.  */
     720      313336 :       accum = accum ? crc32_unsigned (accum, crc) : crc;
     721      313336 :       *crc_ptr = accum;
     722             : 
     723             :       /* Buffer will be sufficiently aligned.  */
     724      313336 :       *(unsigned *)buffer = crc;
     725             :     }
     726      314996 : }
     727             : 
     728             : /* Finish a set of bools.  */
     729             : 
     730             : void
     731    14925143 : bytes_out::bflush ()
     732             : {
     733    14925143 :   if (bit_pos)
     734             :     {
     735    14155027 :       u32 (bit_val);
     736    14155027 :       lengths[2] += bit_flush ();
     737             :     }
     738    14925143 :   spans[2]++;
     739    14925143 :   is_set = -1;
     740    14925143 : }
     741             : 
     742             : void
     743    17500127 : bytes_in::bflush ()
     744             : {
     745           0 :   if (bit_pos)
     746    16590681 :     bit_flush ();
     747           0 : }
     748             : 
     749             : /* When reading, we don't know how many bools we'll read in.  So read
     750             :    4 bytes-worth, and then rewind when flushing if we didn't need them
     751             :    all.  You can't have a block of bools closer than 4 bytes to the
     752             :    end of the buffer.  */
     753             : 
     754             : void
     755    25546930 : bytes_in::bfill ()
     756             : {
     757           0 :   bit_val = u32 ();
     758    25546930 : }
     759             : 
     760             : /* Bools are packed into bytes.  You cannot mix bools and non-bools.
     761             :    You must call bflush before emitting another type.  So batch your
     762             :    bools.
     763             : 
     764             :    It may be worth optimizing for most bools being zero.  Some kind of
     765             :    run-length encoding?  */
     766             : 
     767             : void
     768   543268452 : bytes_out::b (bool x)
     769             : {
     770   543268452 :   if (is_set != x)
     771             :     {
     772    55157124 :       is_set = x;
     773    55157124 :       spans[x]++;
     774             :     }
     775   543268452 :   lengths[x]++;
     776   543268452 :   bit_val |= unsigned (x) << bit_pos++;
     777   543268452 :   if (bit_pos == 32)
     778             :     {
     779     7723618 :       u32 (bit_val);
     780     7723618 :       lengths[2] += bit_flush ();
     781             :     }
     782   543268452 : }
     783             : 
     784             : bool
     785   638177128 : bytes_in::b ()
     786             : {
     787   638177128 :   if (!bit_pos)
     788    25546930 :     bfill ();
     789   638177128 :   bool v = (bit_val >> bit_pos++) & 1;
     790   638177128 :   if (bit_pos == 32)
     791     8956249 :     bit_flush ();
     792   638177128 :   return v;
     793             : }
     794             : 
     795             : /* Exactly 4 bytes.  Used internally for bool packing and a few other
     796             :    places.  We can't simply use uint32_t because (a) alignment and
     797             :    (b) we need little-endian for the bool streaming rewinding to make
     798             :    sense.  */
     799             : 
     800             : void
     801    21884035 : bytes_out::u32 (unsigned val)
     802             : {
     803    21884035 :   if (char *ptr = write (4))
     804             :     {
     805    21884035 :       ptr[0] = val;
     806    21884035 :       ptr[1] = val >> 8;
     807    21884035 :       ptr[2] = val >> 16;
     808    21884035 :       ptr[3] = val >> 24;
     809             :     }
     810    21884035 : }
     811             : 
     812             : unsigned
     813    25552766 : bytes_in::u32 ()
     814             : {
     815    25552766 :   unsigned val = 0;
     816    25552766 :   if (const char *ptr = read (4))
     817             :     {
     818    25552766 :       val |= (unsigned char)ptr[0];
     819    25552766 :       val |= (unsigned char)ptr[1] << 8;
     820    25552766 :       val |= (unsigned char)ptr[2] << 16;
     821    25552766 :       val |= (unsigned char)ptr[3] << 24;
     822             :     }
     823             : 
     824    25552766 :   return val;
     825             : }
     826             : 
     827             : /* Chars are unsigned and written as single bytes. */
     828             : 
     829             : void
     830           0 : bytes_out::c (unsigned char v)
     831             : {
     832           0 :   if (char *ptr = write (1))
     833           0 :     *ptr = v;
     834           0 : }
     835             : 
     836             : int
     837           0 : bytes_in::c ()
     838             : {
     839           0 :   int v = 0;
     840           0 :   if (const char *ptr = read (1))
     841           0 :     v = (unsigned char)ptr[0];
     842           0 :   return v;
     843             : }
     844             : 
     845             : /* Ints 7-bit as a byte. Otherwise a 3bit count of following bytes in
     846             :    big-endian form.  4 bits are in the first byte.  */
     847             : 
     848             : void
     849   101370354 : bytes_out::i (int v)
     850             : {
     851   101370354 :   if (char *ptr = write (1))
     852             :     {
     853   101370354 :       if (v <= 0x3f && v >= -0x40)
     854    80311896 :         *ptr = v & 0x7f;
     855             :       else
     856             :         {
     857    21058458 :           unsigned bytes = 0;
     858    21058458 :           int probe;
     859    21058458 :           if (v >= 0)
     860           0 :             for (probe = v >> 8; probe > 0x7; probe >>= 8)
     861           0 :               bytes++;
     862             :           else
     863    33065253 :             for (probe = v >> 8; probe < -0x8; probe >>= 8)
     864    12006795 :               bytes++;
     865    21058458 :           *ptr = 0x80 | bytes << 4 | (probe & 0xf);
     866    21058458 :           if ((ptr = write (++bytes)))
     867    54123711 :             for (; bytes--; v >>= 8)
     868    33065253 :               ptr[bytes] = v & 0xff;
     869             :         }
     870             :     }
     871   101370354 : }
     872             : 
     873             : int
     874   120921801 : bytes_in::i ()
     875             : {
     876   120921801 :   int v = 0;
     877   120921801 :   if (const char *ptr = read (1))
     878             :     {
     879   120921801 :       v = *ptr & 0xff;
     880   120921801 :       if (v & 0x80)
     881             :         {
     882    25691668 :           unsigned bytes = (v >> 4) & 0x7;
     883    25691668 :           v &= 0xf;
     884    25691668 :           if (v & 0x8)
     885    25691668 :             v |= -1 ^ 0x7;
     886             :           /* unsigned necessary due to left shifts of -ve values.  */
     887    25691668 :           unsigned uv = unsigned (v);
     888    25691668 :           if ((ptr = read (++bytes)))
     889    66704431 :             while (bytes--)
     890    41012763 :               uv = (uv << 8) | (*ptr++ & 0xff);
     891    25691668 :           v = int (uv);
     892             :         }
     893    95230133 :       else if (v & 0x40)
     894    13886949 :         v |= -1 ^ 0x3f;
     895             :     }
     896             : 
     897   120921801 :   return v;
     898             : }
     899             : 
     900             : void
     901   107820691 : bytes_out::u (unsigned v)
     902             : {
     903   107820691 :   if (char *ptr = write (1))
     904             :     {
     905   107820691 :       if (v <= 0x7f)
     906    80606535 :         *ptr = v;
     907             :       else
     908             :         {
     909    27214156 :           unsigned bytes = 0;
     910    27214156 :           unsigned probe;
     911    63432800 :           for (probe = v >> 8; probe > 0xf; probe >>= 8)
     912    36218644 :             bytes++;
     913    27214156 :           *ptr = 0x80 | bytes << 4 | probe;
     914    27214156 :           if ((ptr = write (++bytes)))
     915    90646956 :             for (; bytes--; v >>= 8)
     916    63432800 :               ptr[bytes] = v & 0xff;
     917             :         }
     918             :     }
     919   107820691 : }
     920             : 
     921             : unsigned
     922   126247096 : bytes_in::u ()
     923             : {
     924   126247096 :   unsigned v = 0;
     925             : 
     926   126247096 :   if (const char *ptr = read (1))
     927             :     {
     928   126247096 :       v = *ptr & 0xff;
     929   126247096 :       if (v & 0x80)
     930             :         {
     931    33222128 :           unsigned bytes = (v >> 4) & 0x7;
     932    33222128 :           v &= 0xf;
     933    33222128 :           if ((ptr = read (++bytes)))
     934   111277882 :             while (bytes--)
     935    78055754 :               v = (v << 8) | (*ptr++ & 0xff);
     936             :         }
     937             :     }
     938             : 
     939   126247096 :   return v;
     940             : }
     941             : 
     942             : void
     943     2506433 : bytes_out::wi (HOST_WIDE_INT v)
     944             : {
     945     2506433 :   if (char *ptr = write (1))
     946             :     {
     947     2506433 :       if (v <= 0x3f && v >= -0x40)
     948     2346784 :         *ptr = v & 0x7f;
     949             :       else
     950             :         {
     951      159649 :           unsigned bytes = 0;
     952      159649 :           HOST_WIDE_INT probe;
     953      159649 :           if (v >= 0)
     954      184661 :             for (probe = v >> 8; probe > 0x7; probe >>= 8)
     955       27349 :               bytes++;
     956             :           else
     957        7894 :             for (probe = v >> 8; probe < -0x8; probe >>= 8)
     958        5557 :               bytes++;
     959      159649 :           *ptr = 0x80 | bytes << 4 | (probe & 0xf);
     960      159649 :           if ((ptr = write (++bytes)))
     961      352204 :             for (; bytes--; v >>= 8)
     962      192555 :               ptr[bytes] = v & 0xff;
     963             :         }
     964             :     }
     965     2506433 : }
     966             : 
     967             : HOST_WIDE_INT
     968     2786988 : bytes_in::wi ()
     969             : {
     970     2786988 :   HOST_WIDE_INT v = 0;
     971     2786988 :   if (const char *ptr = read (1))
     972             :     {
     973     2786988 :       v = *ptr & 0xff;
     974     2786988 :       if (v & 0x80)
     975             :         {
     976      188710 :           unsigned bytes = (v >> 4) & 0x7;
     977      188710 :           v &= 0xf;
     978      188710 :           if (v & 0x8)
     979        2908 :             v |= -1 ^ 0x7;
     980             :           /* unsigned necessary due to left shifts of -ve values.  */
     981      188710 :           unsigned HOST_WIDE_INT uv = (unsigned HOST_WIDE_INT) v;
     982      188710 :           if ((ptr = read (++bytes)))
     983      413807 :             while (bytes--)
     984      225097 :               uv = (uv << 8) | (*ptr++ & 0xff);
     985      188710 :           v = (HOST_WIDE_INT) uv;
     986             :         }
     987     2598278 :       else if (v & 0x40)
     988       13021 :         v |= -1 ^ 0x3f;
     989             :     }
     990             : 
     991     2786988 :   return v;
     992             : }
     993             : 
     994             : /* unsigned wide ints are just written as signed wide ints.  */
     995             : 
     996             : inline void
     997     2505469 : bytes_out::wu (unsigned HOST_WIDE_INT v)
     998             : {
     999          21 :   wi ((HOST_WIDE_INT) v);
    1000             : }
    1001             : 
    1002             : inline unsigned HOST_WIDE_INT
    1003     2785822 : bytes_in::wu ()
    1004             : {
    1005     4945196 :   return (unsigned HOST_WIDE_INT) wi ();
    1006             : }
    1007             : 
    1008             : /* size_t written as unsigned or unsigned wide int.  */
    1009             : 
    1010             : inline void
    1011     1967484 : bytes_out::z (size_t s)
    1012             : {
    1013     1967484 :   if (sizeof (s) == sizeof (unsigned))
    1014             :     u (s);
    1015             :   else
    1016     3886579 :     wu (s);
    1017             : }
    1018             : 
    1019             : inline size_t
    1020     2159374 : bytes_in::z ()
    1021             : {
    1022     2159374 :   if (sizeof (size_t) == sizeof (unsigned))
    1023             :     return u ();
    1024             :   else
    1025     4318748 :     return wu ();
    1026             : }
    1027             : 
    1028             : /* Buffer simply memcpied.  */
    1029             : void *
    1030     1998407 : bytes_out::buf (size_t len)
    1031             : {
    1032     1998407 :   align (sizeof (void *) * 2);
    1033     1998407 :   return write (len);
    1034             : }
    1035             : 
    1036             : void
    1037     1940403 : bytes_out::buf (const void *src, size_t len)
    1038             : {
    1039     1940403 :   if (void *ptr = buf (len))
    1040     1940403 :     memcpy (ptr, src, len);
    1041     1940403 : }
    1042             : 
    1043             : const void *
    1044     2190677 : bytes_in::buf (size_t len)
    1045             : {
    1046     2190677 :   align (sizeof (void *) * 2);
    1047     2190677 :   const char *ptr = read (len);
    1048             : 
    1049     2190677 :   return ptr;
    1050             : }
    1051             : 
    1052             : /* strings as an size_t length, followed by the buffer.  Make sure
    1053             :    there's a NUL terminator on read.  */
    1054             : 
    1055             : void
    1056     1967484 : bytes_out::str (const char *string, size_t len)
    1057             : {
    1058     1919095 :   z (len);
    1059     1919095 :   if (len)
    1060             :     {
    1061     1919095 :       gcc_checking_assert (!string[len]);
    1062     1919095 :       buf (string, len + 1);
    1063             :     }
    1064       48389 : }
    1065             : 
    1066             : const char *
    1067     2159374 : bytes_in::str (size_t *len_p)
    1068             : {
    1069     2159374 :   size_t len = z ();
    1070             : 
    1071             :   /* We're about to trust some user data.  */
    1072     2159374 :   if (overrun)
    1073           0 :     len = 0;
    1074     2159374 :   if (len_p)
    1075     2156971 :     *len_p = len;
    1076     2159374 :   const char *str = NULL;
    1077     2159374 :   if (len)
    1078             :     {
    1079     2159071 :       str = reinterpret_cast<const char *> (buf (len + 1));
    1080     2159071 :       if (!str || str[len])
    1081             :         {
    1082           0 :           set_overrun ();
    1083           0 :           str = NULL;
    1084             :         }
    1085             :     }
    1086           0 :   return str ? str : "";
    1087             : }
    1088             : 
    1089             : cpp_hashnode *
    1090      389424 : bytes_in::cpp_node ()
    1091             : {
    1092      389424 :   size_t len;
    1093      389424 :   const char *s = str (&len);
    1094      389424 :   if (!len)
    1095             :     return NULL;
    1096      389121 :   return ::cpp_node (get_identifier_with_length (s, len));
    1097             : }
    1098             : 
    1099             : /* Format a string directly to the buffer, including a terminating
    1100             :    NUL.  Intended for human consumption.  */
    1101             : 
    1102             : void
    1103       17006 : bytes_out::printf (const char *format, ...)
    1104             : {
    1105       17006 :   va_list args;
    1106             :   /* Exercise buffer expansion.  */
    1107       17006 :   size_t len = EXPERIMENT (10, 500);
    1108             : 
    1109       33984 :   while (char *ptr = write (len))
    1110             :     {
    1111       33984 :       va_start (args, format);
    1112       33984 :       size_t actual = vsnprintf (ptr, len, format, args) + 1;
    1113       33984 :       va_end (args);
    1114       33984 :       if (actual <= len)
    1115             :         {
    1116       17006 :           unuse (len - actual);
    1117             :           break;
    1118             :         }
    1119       16978 :       unuse (len);
    1120       16978 :       len = actual;
    1121       16978 :     }
    1122       17006 : }
    1123             : 
    1124             : void
    1125        3320 : bytes_out::print_time (const char *kind, const tm *time, const char *tz)
    1126             : {
    1127        3320 :   printf ("%stime: %4u/%02u/%02u %02u:%02u:%02u %s",
    1128        3320 :           kind, time->tm_year + 1900, time->tm_mon + 1, time->tm_mday,
    1129        3320 :           time->tm_hour, time->tm_min, time->tm_sec, tz);
    1130        3320 : }
    1131             : 
    1132             : /* Encapsulated Lazy Records Of Named Declarations.
    1133             :    Header: Stunningly Elf32_Ehdr-like
    1134             :    Sections: Sectional data
    1135             :      [1-N) : User data sections
    1136             :      N .strtab  : strings, stunningly ELF STRTAB-like
    1137             :    Index: Section table, stunningly ELF32_Shdr-like.   */
    1138             : 
    1139             : class elf {
    1140             : protected:
    1141             :   /* Constants used within the format.  */
    1142             :   enum private_constants {
    1143             :     /* File kind. */
    1144             :     ET_NONE = 0,
    1145             :     EM_NONE = 0,
    1146             :     OSABI_NONE = 0,
    1147             : 
    1148             :     /* File format. */
    1149             :     EV_CURRENT = 1,
    1150             :     CLASS32 = 1,
    1151             :     DATA2LSB = 1,
    1152             :     DATA2MSB = 2,
    1153             : 
    1154             :     /* Section numbering.  */
    1155             :     SHN_UNDEF = 0,
    1156             :     SHN_LORESERVE = 0xff00,
    1157             :     SHN_XINDEX = 0xffff,
    1158             : 
    1159             :     /* Section types.  */
    1160             :     SHT_NONE = 0,       /* No contents.  */
    1161             :     SHT_PROGBITS = 1, /* Random bytes.  */
    1162             :     SHT_STRTAB = 3,     /* A string table.  */
    1163             : 
    1164             :     /* Section flags.  */
    1165             :     SHF_NONE = 0x00,    /* Nothing.  */
    1166             :     SHF_STRINGS = 0x20,  /* NUL-Terminated strings.  */
    1167             : 
    1168             :     /* I really hope we do not get CMI files larger than 4GB.  */
    1169             :     MY_CLASS = CLASS32,
    1170             :     /* It is host endianness that is relevant.  */
    1171             :     MY_ENDIAN = DATA2LSB
    1172             : #ifdef WORDS_BIGENDIAN
    1173             :     ^ DATA2LSB ^ DATA2MSB
    1174             : #endif
    1175             :   };
    1176             : 
    1177             : public:
    1178             :   /* Constants visible to users.  */
    1179             :   enum public_constants {
    1180             :     /* Special error codes.  Breaking layering a bit.  */
    1181             :     E_BAD_DATA = -1,  /* Random unexpected data errors.  */
    1182             :     E_BAD_LAZY = -2,  /* Badly ordered laziness.  */
    1183             :     E_BAD_IMPORT = -3 /* A nested import failed.  */
    1184             :   };
    1185             : 
    1186             : protected:
    1187             :   /* File identification.  On-disk representation.  */
    1188             :   struct ident {
    1189             :     uint8_t magic[4];   /* 0x7f, 'E', 'L', 'F' */
    1190             :     uint8_t klass;      /* 4:CLASS32 */
    1191             :     uint8_t data;       /* 5:DATA2[LM]SB */
    1192             :     uint8_t version;    /* 6:EV_CURRENT  */
    1193             :     uint8_t osabi;      /* 7:OSABI_NONE */
    1194             :     uint8_t abiver;     /* 8: 0 */
    1195             :     uint8_t pad[7];     /* 9-15 */
    1196             :   };
    1197             :   /* File header.  On-disk representation.  */
    1198             :   struct header {
    1199             :     struct ident ident;
    1200             :     uint16_t type;      /* ET_NONE */
    1201             :     uint16_t machine;   /* EM_NONE */
    1202             :     uint32_t version;   /* EV_CURRENT */
    1203             :     uint32_t entry;     /* 0 */
    1204             :     uint32_t phoff;     /* 0 */
    1205             :     uint32_t shoff;     /* Section Header Offset in file */
    1206             :     uint32_t flags; 
    1207             :     uint16_t ehsize;    /* ELROND Header SIZE -- sizeof (header) */
    1208             :     uint16_t phentsize; /* 0 */
    1209             :     uint16_t phnum;     /* 0 */
    1210             :     uint16_t shentsize; /* Section Header SIZE -- sizeof (section) */
    1211             :     uint16_t shnum;     /* Section Header NUM */
    1212             :     uint16_t shstrndx;  /* Section Header STRing iNDeX */
    1213             :   };
    1214             :   /* File section.  On-disk representation.  */
    1215             :   struct section {
    1216             :     uint32_t name;      /* String table offset.  */
    1217             :     uint32_t type;      /* SHT_* */
    1218             :     uint32_t flags;     /* SHF_* */
    1219             :     uint32_t addr;      /* 0 */
    1220             :     uint32_t offset;    /* OFFSET in file */
    1221             :     uint32_t size;      /* SIZE of section */
    1222             :     uint32_t link;      /* 0 */
    1223             :     uint32_t info;      /* 0 */
    1224             :     uint32_t addralign; /* 0 */
    1225             :     uint32_t entsize;   /* ENTry SIZE, usually 0 */
    1226             :   };
    1227             : 
    1228             : protected:
    1229             :   data hdr;     /* The header.  */
    1230             :   data sectab;  /* The section table.  */
    1231             :   data strtab;  /* String table.  */
    1232             :   int fd;       /* File descriptor we're reading or writing.  */
    1233             :   int err;      /* Sticky error code.  */
    1234             : 
    1235             : public:
    1236             :   /* Construct from STREAM.  E is errno if STREAM NULL.  */
    1237        3591 :   elf (int fd, int e)
    1238        7182 :     :hdr (), sectab (), strtab (), fd (fd), err (fd >= 0 ? 0 : e)
    1239             :   {}
    1240        3530 :   ~elf ()
    1241             :   {
    1242        3530 :     gcc_checking_assert (fd < 0 && !hdr.buffer
    1243             :                          && !sectab.buffer && !strtab.buffer);
    1244        3530 :   }
    1245             : 
    1246             : public:
    1247             :   /* Return the error, if we have an error.  */
    1248      679225 :   int get_error () const
    1249             :   {
    1250      679225 :     return err;
    1251             :   }
    1252             :   /* Set the error, unless it's already been set.  */
    1253          23 :   void set_error (int e = E_BAD_DATA)
    1254             :   {
    1255          23 :     if (!err)
    1256          16 :       err = e;
    1257           0 :   }
    1258             :   /* Get an error string.  */
    1259             :   const char *get_error (const char *) const;
    1260             : 
    1261             : public:
    1262             :   /* Begin reading/writing file.  Return false on error.  */
    1263        3516 :   bool begin () const
    1264             :   {
    1265        3516 :     return !get_error ();
    1266             :   }
    1267             :   /* Finish reading/writing file.  Return false on error.  */
    1268             :   bool end ();
    1269             : };
    1270             : 
    1271             : /* Return error string.  */
    1272             : 
    1273             : const char *
    1274          34 : elf::get_error (const char *name) const
    1275             : {
    1276          34 :   if (!name)
    1277             :     return "Unknown CMI mapping";
    1278             : 
    1279          34 :   switch (err)
    1280             :     {
    1281           0 :     case 0:
    1282           0 :       gcc_unreachable ();
    1283             :     case E_BAD_DATA:
    1284             :       return "Bad file data";
    1285           6 :     case E_BAD_IMPORT:
    1286           6 :       return "Bad import dependency";
    1287           0 :     case E_BAD_LAZY:
    1288           0 :       return "Bad lazy ordering";
    1289          18 :     default:
    1290          18 :       return xstrerror (err);
    1291             :     }
    1292             : }
    1293             : 
    1294             : /* Finish file, return true if there's an error.  */
    1295             : 
    1296             : bool
    1297        5052 : elf::end ()
    1298             : {
    1299             :   /* Close the stream and free the section table.  */
    1300        5052 :   if (fd >= 0 && close (fd))
    1301           0 :     set_error (errno);
    1302        5052 :   fd = -1;
    1303             : 
    1304        5052 :   return !get_error ();
    1305             : }
    1306             : 
    1307             : /* ELROND reader.  */
    1308             : 
    1309             : class elf_in : public elf {
    1310             :   typedef elf parent;
    1311             : 
    1312             : private:
    1313             :   /* For freezing & defrosting.  */
    1314             : #if !defined (HOST_LACKS_INODE_NUMBERS)
    1315             :   dev_t device;
    1316             :   ino_t inode;
    1317             : #endif
    1318             : 
    1319             : public:
    1320        1874 :   elf_in (int fd, int e)
    1321        3748 :     :parent (fd, e)
    1322             :   {
    1323             :   }
    1324        1813 :   ~elf_in ()
    1325             :   {
    1326        1813 :   }
    1327             : 
    1328             : public:
    1329      331546 :   bool is_frozen () const
    1330             :   {
    1331          18 :     return fd < 0 && hdr.pos;
    1332             :   }
    1333          18 :   bool is_freezable () const
    1334             :   {
    1335           9 :     return fd >= 0 && hdr.pos;
    1336             :   }
    1337             :   void freeze ();
    1338             :   bool defrost (const char *);
    1339             : 
    1340             :   /* If BYTES is in the mmapped area, allocate a new buffer for it.  */
    1341           0 :   void preserve (bytes_in &bytes ATTRIBUTE_UNUSED)
    1342             :   {
    1343             : #if MAPPED_READING
    1344           0 :     if (hdr.buffer && bytes.buffer >= hdr.buffer
    1345           0 :         && bytes.buffer < hdr.buffer + hdr.pos)
    1346             :       {
    1347           0 :         char *buf = bytes.buffer;
    1348           0 :         bytes.buffer = data::simple_memory.grow (NULL, bytes.size);
    1349           0 :         memcpy (bytes.buffer, buf, bytes.size);
    1350             :       }
    1351             : #endif
    1352           0 :   }
    1353             :   /* If BYTES is not in SELF's mmapped area, free it.  SELF might be
    1354             :      NULL. */
    1355         819 :   static void release (elf_in *self ATTRIBUTE_UNUSED, bytes_in &bytes)
    1356             :   {
    1357             : #if MAPPED_READING
    1358         819 :     if (!(self && self->hdr.buffer && bytes.buffer >= self->hdr.buffer
    1359         819 :           && bytes.buffer < self->hdr.buffer + self->hdr.pos))
    1360             : #endif
    1361           0 :       data::simple_memory.shrink (bytes.buffer);
    1362         819 :     bytes.buffer = NULL;
    1363         819 :     bytes.size = 0;
    1364         819 :   }
    1365             : 
    1366             : public:
    1367      344267 :   static void grow (data &data, unsigned needed)
    1368             :   {
    1369      344267 :     gcc_checking_assert (!data.buffer);
    1370             : #if !MAPPED_READING
    1371             :     data.buffer = XNEWVEC (char, needed);
    1372             : #endif
    1373      344267 :     data.size = needed;
    1374      344267 :   }
    1375      348213 :   static void shrink (data &data)
    1376             :   {
    1377             : #if !MAPPED_READING
    1378             :     XDELETEVEC (data.buffer);
    1379             : #endif
    1380      348213 :     data.buffer = NULL;
    1381      348213 :     data.size = 0;
    1382           0 :   }
    1383             : 
    1384             : public:
    1385      342411 :   const section *get_section (unsigned s) const
    1386             :   {
    1387      342411 :     if (s * sizeof (section) < sectab.size)
    1388      342411 :       return reinterpret_cast<const section *>
    1389      342411 :         (&sectab.buffer[s * sizeof (section)]);
    1390             :     else
    1391             :       return NULL;
    1392             :   }
    1393             :   unsigned get_section_limit () const
    1394             :   {
    1395             :     return sectab.size / sizeof (section);
    1396             :   }
    1397             : 
    1398             : protected:
    1399             :   const char *read (data *, unsigned, unsigned);
    1400             : 
    1401             : public:
    1402             :   /* Read section by number.  */
    1403      342411 :   bool read (data *d, const section *s)
    1404             :   {
    1405      342411 :     return s && read (d, s->offset, s->size);
    1406             :   }
    1407             : 
    1408             :   /* Find section by name.  */
    1409             :   unsigned find (const char *name);
    1410             :   /* Find section by index.  */
    1411             :   const section *find (unsigned snum, unsigned type = SHT_PROGBITS);
    1412             : 
    1413             : public:
    1414             :   /* Release the string table, when we're done with it.  */
    1415        5172 :   void release ()
    1416             :   {
    1417        5172 :     shrink (strtab);
    1418          30 :   }
    1419             : 
    1420             : public:
    1421             :   bool begin (location_t);
    1422        3335 :   bool end ()
    1423             :   {
    1424        3335 :     release ();
    1425             : #if MAPPED_READING
    1426        3335 :     if (hdr.buffer)
    1427        1813 :       munmap (hdr.buffer, hdr.pos);
    1428        3335 :     hdr.buffer = NULL;
    1429             : #endif
    1430        3335 :     shrink (sectab);
    1431             : 
    1432        3335 :     return parent::end ();
    1433             :   }
    1434             : 
    1435             : public:
    1436             :   /* Return string name at OFFSET.  Checks OFFSET range.  Always
    1437             :      returns non-NULL.  We know offset 0 is an empty string.  */
    1438      308077 :   const char *name (unsigned offset)
    1439             :   {
    1440      616154 :     return &strtab.buffer[offset < strtab.size ? offset : 0];
    1441             :   }
    1442             : };
    1443             : 
    1444             : /* ELROND writer.  */
    1445             : 
    1446             : class elf_out : public elf, public data::allocator {
    1447             :   typedef elf parent;
    1448             :   /* Desired section alignment on disk.  */
    1449             :   static const int SECTION_ALIGN = 16;
    1450             : 
    1451             : private:
    1452             :   ptr_int_hash_map identtab;    /* Map of IDENTIFIERS to strtab offsets. */
    1453             :   unsigned pos;                 /* Write position in file.  */
    1454             : #if MAPPED_WRITING
    1455             :   unsigned offset;              /* Offset of the mapping.  */
    1456             :   unsigned extent;              /* Length of mapping.  */
    1457             :   unsigned page_size;           /* System page size.  */
    1458             : #endif
    1459             : 
    1460             : public:
    1461        1717 :   elf_out (int fd, int e)
    1462        3377 :     :parent (fd, e), identtab (500), pos (0)
    1463             :   {
    1464             : #if MAPPED_WRITING
    1465        1717 :     offset = extent = 0;
    1466        1717 :     page_size = sysconf (_SC_PAGE_SIZE);
    1467        1717 :     if (page_size < SECTION_ALIGN)
    1468             :       /* Something really strange.  */
    1469           0 :       set_error (EINVAL);
    1470             : #endif
    1471        1717 :   }
    1472        1717 :   ~elf_out ()
    1473        1717 :   {
    1474        1717 :     data::simple_memory.shrink (hdr);
    1475        1717 :     data::simple_memory.shrink (sectab);
    1476        1717 :     data::simple_memory.shrink (strtab);
    1477        1717 :   }
    1478             : 
    1479             : #if MAPPED_WRITING
    1480             : private:
    1481             :   void create_mapping (unsigned ext, bool extending = true);
    1482             :   void remove_mapping ();
    1483             : #endif
    1484             : 
    1485             : protected:
    1486             :   using allocator::grow;
    1487             :   char *grow (char *, unsigned needed) final override;
    1488             : #if MAPPED_WRITING
    1489             :   using allocator::shrink;
    1490             :   void shrink (char *) final override;
    1491             : #endif
    1492             : 
    1493             : public:
    1494        3539 :   unsigned get_section_limit () const
    1495             :   {
    1496        3539 :     return sectab.pos / sizeof (section);
    1497             :   }
    1498             : 
    1499             : protected:
    1500             :   unsigned add (unsigned type, unsigned name = 0,
    1501             :                 unsigned off = 0, unsigned size = 0, unsigned flags = SHF_NONE);
    1502             :   unsigned write (const data &);
    1503             : #if MAPPED_WRITING
    1504             :   unsigned write (const bytes_out &);
    1505             : #endif
    1506             : 
    1507             : public:
    1508             :   /* IDENTIFIER to strtab offset.  */
    1509             :   unsigned name (tree ident);
    1510             :   /* String literal to strtab offset.  */
    1511             :   unsigned name (const char *n);
    1512             :   /* Qualified name of DECL to strtab offset.  */
    1513             :   unsigned qualified_name (tree decl, bool is_defn);
    1514             : 
    1515             : private:
    1516             :   unsigned strtab_write (const char *s, unsigned l);
    1517             :   void strtab_write (tree decl, int);
    1518             : 
    1519             : public:
    1520             :   /* Add a section with contents or strings.  */
    1521             :   unsigned add (const bytes_out &, bool string_p, unsigned name);
    1522             : 
    1523             : public:
    1524             :   /* Begin and end writing.  */
    1525             :   bool begin ();
    1526             :   bool end ();
    1527             : };
    1528             : 
    1529             : /* Begin reading section NAME (of type PROGBITS) from SOURCE.
    1530             :    Data always checked for CRC.  */
    1531             : 
    1532             : bool
    1533       10865 : bytes_in::begin (location_t loc, elf_in *source, const char *name)
    1534             : {
    1535       10865 :   unsigned snum = source->find (name);
    1536             : 
    1537       10865 :   return begin (loc, source, snum, name);
    1538             : }
    1539             : 
    1540             : /* Begin reading section numbered SNUM with NAME (may be NULL).  */
    1541             : 
    1542             : bool
    1543      340555 : bytes_in::begin (location_t loc, elf_in *source, unsigned snum, const char *name)
    1544             : {
    1545      340555 :   if (!source->read (this, source->find (snum))
    1546      340555 :       || !size || !check_crc ())
    1547             :     {
    1548           0 :       source->set_error (elf::E_BAD_DATA);
    1549           0 :       source->shrink (*this);
    1550           0 :       if (name)
    1551           0 :         error_at (loc, "section %qs is missing or corrupted", name);
    1552             :       else
    1553           0 :         error_at (loc, "section #%u is missing or corrupted", snum);
    1554           0 :       return false;
    1555             :     }
    1556      340555 :   pos = 4;
    1557      340555 :   return true;
    1558             : }
    1559             : 
    1560             : /* Finish reading a section.  */
    1561             : 
    1562             : bool
    1563      339706 : bytes_in::end (elf_in *src)
    1564             : {
    1565      339706 :   if (more_p ())
    1566           7 :     set_overrun ();
    1567      339706 :   if (overrun)
    1568           7 :     src->set_error ();
    1569             : 
    1570      339706 :   src->shrink (*this);
    1571             : 
    1572      339706 :   return !overrun;
    1573             : }
    1574             : 
    1575             : /* Begin writing buffer.  */
    1576             : 
    1577             : void
    1578      314996 : bytes_out::begin (bool need_crc)
    1579             : {
    1580           0 :   if (need_crc)
    1581           0 :     pos = 4;
    1582           0 :   memory->grow (*this, 0, false);
    1583      303743 : }
    1584             : 
    1585             : /* Finish writing buffer.  Stream out to SINK as named section NAME.
    1586             :    Return section number or 0 on failure.  If CRC_PTR is true, crc
    1587             :    the data.  Otherwise it is a string section.  */
    1588             : 
    1589             : unsigned
    1590      314996 : bytes_out::end (elf_out *sink, unsigned name, unsigned *crc_ptr)
    1591             : {
    1592      314996 :   lengths[3] += pos;
    1593      314996 :   spans[3]++;
    1594             : 
    1595      314996 :   set_crc (crc_ptr);
    1596      314996 :   unsigned sec_num = sink->add (*this, !crc_ptr, name);
    1597      314996 :   memory->shrink (*this);
    1598             : 
    1599      314996 :   return sec_num;
    1600             : }
    1601             : 
    1602             : /* Close and open the file, without destroying it.  */
    1603             : 
    1604             : void
    1605           9 : elf_in::freeze ()
    1606             : {
    1607           9 :   gcc_checking_assert (!is_frozen ());
    1608             : #if MAPPED_READING
    1609           9 :   if (munmap (hdr.buffer, hdr.pos) < 0)
    1610           0 :     set_error (errno);
    1611             : #endif
    1612           9 :   if (close (fd) < 0)
    1613           0 :     set_error (errno);
    1614           9 :   fd = -1;
    1615           9 : }
    1616             : 
    1617             : bool
    1618           9 : elf_in::defrost (const char *name)
    1619             : {
    1620           9 :   gcc_checking_assert (is_frozen ());
    1621           9 :   struct stat stat;
    1622             : 
    1623           9 :   fd = open (name, O_RDONLY | O_CLOEXEC | O_BINARY);
    1624           9 :   if (fd < 0 || fstat (fd, &stat) < 0)
    1625           0 :     set_error (errno);
    1626             :   else
    1627             :     {
    1628           9 :       bool ok = hdr.pos == unsigned (stat.st_size);
    1629             : #ifndef HOST_LACKS_INODE_NUMBERS
    1630           9 :       if (device != stat.st_dev
    1631           9 :           || inode != stat.st_ino)
    1632             :         ok = false;
    1633             : #endif
    1634           9 :       if (!ok)
    1635           0 :         set_error (EMFILE);
    1636             : #if MAPPED_READING
    1637           9 :       if (ok)
    1638             :         {
    1639           9 :           char *mapping = reinterpret_cast<char *>
    1640           9 :             (mmap (NULL, hdr.pos, PROT_READ, MAP_SHARED, fd, 0));
    1641           9 :           if (mapping == MAP_FAILED)
    1642           0 :           fail:
    1643           0 :               set_error (errno);
    1644             :           else
    1645             :             {
    1646           9 :               if (madvise (mapping, hdr.pos, MADV_RANDOM))
    1647           0 :                 goto fail;
    1648             : 
    1649             :               /* These buffers are never NULL in this case.  */
    1650           9 :               strtab.buffer = mapping + strtab.pos;
    1651           9 :               sectab.buffer = mapping + sectab.pos;
    1652           9 :               hdr.buffer = mapping;
    1653             :             }
    1654             :         }
    1655             : #endif
    1656             :     }
    1657             : 
    1658           9 :   return !get_error ();
    1659             : }
    1660             : 
    1661             : /* Read at current position into BUFFER.  Return true on success.  */
    1662             : 
    1663             : const char *
    1664      344267 : elf_in::read (data *data, unsigned pos, unsigned length)
    1665             : {
    1666             : #if MAPPED_READING
    1667      344267 :   if (pos + length > hdr.pos)
    1668             :     {
    1669           0 :       set_error (EINVAL);
    1670           0 :       return NULL;
    1671             :     }
    1672             : #else
    1673             :   if (pos != ~0u && lseek (fd, pos, SEEK_SET) < 0)
    1674             :     {
    1675             :       set_error (errno);
    1676             :       return NULL;
    1677             :     }
    1678             : #endif
    1679      344267 :   grow (*data, length);
    1680             : #if MAPPED_READING  
    1681      344267 :   data->buffer = hdr.buffer + pos;
    1682             : #else
    1683             :   if (::read (fd, data->buffer, data->size) != ssize_t (length))
    1684             :     {
    1685             :       set_error (errno);
    1686             :       shrink (*data);
    1687             :       return NULL;
    1688             :     }
    1689             : #endif
    1690             : 
    1691      344267 :   return data->buffer;
    1692             : }
    1693             : 
    1694             : /* Read section SNUM of TYPE.  Return section pointer or NULL on error.  */
    1695             : 
    1696             : const elf::section *
    1697      342411 : elf_in::find (unsigned snum, unsigned type)
    1698             : {
    1699      342411 :   const section *sec = get_section (snum);
    1700      342411 :   if (!snum || !sec || sec->type != type)
    1701           0 :     return NULL;
    1702             :   return sec;
    1703             : }
    1704             : 
    1705             : /* Find a section NAME and TYPE.  Return section number, or zero on
    1706             :    failure.  */
    1707             : 
    1708             : unsigned
    1709       10883 : elf_in::find (const char *sname)
    1710             : {
    1711       63842 :   for (unsigned pos = sectab.size; pos -= sizeof (section); )
    1712             :     {
    1713       63842 :       const section *sec
    1714       63842 :         = reinterpret_cast<const section *> (&sectab.buffer[pos]);
    1715             : 
    1716      127684 :       if (0 == strcmp (sname, name (sec->name)))
    1717       10883 :         return pos / sizeof (section);
    1718             :     }
    1719             : 
    1720             :   return 0;
    1721             : }
    1722             : 
    1723             : /* Begin reading file.  Verify header.  Pull in section and string
    1724             :    tables.  Return true on success.  */
    1725             : 
    1726             : bool
    1727        1856 : elf_in::begin (location_t loc)
    1728             : {
    1729        1856 :   if (!parent::begin ())
    1730             :     return false;
    1731             : 
    1732        1856 :   struct stat stat;
    1733        1856 :   unsigned size = 0;
    1734        1856 :   if (!fstat (fd, &stat))
    1735             :     {
    1736             : #if !defined (HOST_LACKS_INODE_NUMBERS)
    1737        1856 :       device = stat.st_dev;
    1738        1856 :       inode = stat.st_ino;
    1739             : #endif
    1740             :       /* Never generate files > 4GB, check we've not been given one.  */
    1741        1856 :       if (stat.st_size == unsigned (stat.st_size))
    1742        1856 :         size = unsigned (stat.st_size);
    1743             :     }
    1744             : 
    1745             : #if MAPPED_READING
    1746             :   /* MAP_SHARED so that the file is backing store.  If someone else
    1747             :      concurrently writes it, they're wrong.  */
    1748        1856 :   void *mapping = mmap (NULL, size, PROT_READ, MAP_SHARED, fd, 0);
    1749        1856 :   if (mapping == MAP_FAILED)
    1750             :     {
    1751           0 :     fail:
    1752           0 :       set_error (errno);
    1753           0 :       return false;
    1754             :     }
    1755             :   /* We'll be hopping over this randomly.  Some systems declare the
    1756             :      first parm as char *, and other declare it as void *.  */
    1757        1856 :   if (madvise (reinterpret_cast <char *> (mapping), size, MADV_RANDOM))
    1758           0 :     goto fail;
    1759             : 
    1760        1856 :   hdr.buffer = (char *)mapping;
    1761             : #else
    1762             :   read (&hdr, 0, sizeof (header));
    1763             : #endif
    1764        1856 :   hdr.pos = size; /* Record size of the file.  */
    1765             : 
    1766        1856 :   const header *h = reinterpret_cast<const header *> (hdr.buffer);
    1767        1856 :   if (!h)
    1768             :     return false;
    1769             : 
    1770        1856 :   if (h->ident.magic[0] != 0x7f
    1771             :       || h->ident.magic[1] != 'E'
    1772             :       || h->ident.magic[2] != 'L'
    1773        1856 :       || h->ident.magic[3] != 'F')
    1774             :     {
    1775           0 :       error_at (loc, "not Encapsulated Lazy Records of Named Declarations");
    1776           0 :     failed:
    1777           0 :       shrink (hdr);
    1778           0 :       return false;
    1779             :     }
    1780             : 
    1781             :   /* We expect a particular format -- the ELF is not intended to be
    1782             :      distributable.  */
    1783        1856 :   if (h->ident.klass != MY_CLASS
    1784             :       || h->ident.data != MY_ENDIAN
    1785        1856 :       || h->ident.version != EV_CURRENT
    1786             :       || h->type != ET_NONE
    1787        1856 :       || h->machine != EM_NONE
    1788        1856 :       || h->ident.osabi != OSABI_NONE)
    1789             :     {
    1790           0 :       error_at (loc, "unexpected encapsulation format or type");
    1791           0 :       goto failed;
    1792             :     }
    1793             : 
    1794        1856 :   int e = -1;
    1795        1856 :   if (!h->shoff || h->shentsize != sizeof (section))
    1796             :     {
    1797           0 :     malformed:
    1798           0 :       set_error (e);
    1799           0 :       error_at (loc, "encapsulation is malformed");
    1800           0 :       goto failed;
    1801             :     }
    1802             : 
    1803        1856 :   unsigned strndx = h->shstrndx;
    1804        1856 :   unsigned shnum = h->shnum;
    1805        1856 :   if (shnum == SHN_XINDEX)
    1806             :     {
    1807           0 :       if (!read (&sectab, h->shoff, sizeof (section)))
    1808             :         {
    1809           0 :         section_table_fail:
    1810           0 :           e = errno;
    1811           0 :           goto malformed;
    1812             :         }
    1813           0 :       shnum = get_section (0)->size;
    1814             :       /* Freeing does mean we'll re-read it in the case we're not
    1815             :          mapping, but this is going to be rare.  */
    1816           0 :       shrink (sectab);
    1817             :     }
    1818             : 
    1819        1856 :   if (!shnum)
    1820           0 :     goto malformed;
    1821             : 
    1822        1856 :   if (!read (&sectab, h->shoff, shnum * sizeof (section)))
    1823           0 :     goto section_table_fail;
    1824             : 
    1825        1856 :   if (strndx == SHN_XINDEX)
    1826           0 :     strndx = get_section (0)->link;
    1827             : 
    1828        1856 :   if (!read (&strtab, find (strndx, SHT_STRTAB)))
    1829           0 :     goto malformed;
    1830             : 
    1831             :   /* The string table should be at least one byte, with NUL chars
    1832             :      at either end.  */
    1833        1856 :   if (!(strtab.size && !strtab.buffer[0]
    1834        1856 :         && !strtab.buffer[strtab.size - 1]))
    1835           0 :     goto malformed;
    1836             : 
    1837             : #if MAPPED_READING
    1838             :   /* Record the offsets of the section and string tables.  */
    1839        1856 :   sectab.pos = h->shoff;
    1840        1856 :   strtab.pos = shnum * sizeof (section);
    1841             : #else
    1842             :   shrink (hdr);
    1843             : #endif
    1844             : 
    1845        1856 :   return true;
    1846             : }
    1847             : 
    1848             : /* Create a new mapping.  */
    1849             : 
    1850             : #if MAPPED_WRITING
    1851             : void
    1852        2433 : elf_out::create_mapping (unsigned ext, bool extending)
    1853             : {
    1854             : #ifndef HAVE_POSIX_FALLOCATE
    1855             : #define posix_fallocate(fd,off,len) ftruncate (fd, off + len)
    1856             : #endif
    1857        2433 :   void *mapping = MAP_FAILED;
    1858        2433 :   if (extending && ext < 1024 * 1024)
    1859             :     {
    1860        2163 :       if (!posix_fallocate (fd, offset, ext * 2))
    1861        2163 :         mapping = mmap (NULL, ext * 2, PROT_READ | PROT_WRITE,
    1862        2163 :                         MAP_SHARED, fd, offset);
    1863        2163 :       if (mapping != MAP_FAILED)
    1864             :         ext *= 2;
    1865             :     }
    1866        2163 :   if (mapping == MAP_FAILED)
    1867             :     {
    1868         270 :       if (!extending || !posix_fallocate (fd, offset, ext))
    1869         270 :         mapping = mmap (NULL, ext, PROT_READ | PROT_WRITE,
    1870         270 :                         MAP_SHARED, fd, offset);
    1871         270 :       if (mapping == MAP_FAILED)
    1872             :         {
    1873           0 :           set_error (errno);
    1874             :           mapping = NULL;
    1875             :           ext = 0;
    1876             :         }
    1877             :     }
    1878             : #undef posix_fallocate
    1879        2433 :   hdr.buffer = (char *)mapping;
    1880        2433 :   extent = ext;
    1881        2433 : }
    1882             : #endif
    1883             : 
    1884             : /* Flush out the current mapping.  */
    1885             : 
    1886             : #if MAPPED_WRITING
    1887             : void
    1888        2433 : elf_out::remove_mapping ()
    1889             : {
    1890        2433 :   if (hdr.buffer)
    1891             :     {
    1892             :       /* MS_ASYNC dtrt with the removed mapping, including a
    1893             :          subsequent overlapping remap.  */
    1894        2433 :       if (msync (hdr.buffer, extent, MS_ASYNC)
    1895        2433 :           || munmap (hdr.buffer, extent))
    1896             :         /* We're somewhat screwed at this point.  */
    1897           0 :         set_error (errno);
    1898             :     }
    1899             : 
    1900        2433 :   hdr.buffer = NULL;
    1901        2433 : }
    1902             : #endif
    1903             : 
    1904             : /* Grow a mapping of PTR to be NEEDED bytes long.  This gets
    1905             :    interesting if the new size grows the EXTENT.  */
    1906             : 
    1907             : char *
    1908      738638 : elf_out::grow (char *data, unsigned needed)
    1909             : {
    1910      738638 :   if (!data)
    1911             :     {
    1912             :       /* First allocation, check we're aligned.  */
    1913      318316 :       gcc_checking_assert (!(pos & (SECTION_ALIGN - 1)));
    1914             : #if MAPPED_WRITING
    1915      318316 :       data = hdr.buffer + (pos - offset);
    1916             : #endif
    1917             :     }
    1918             : 
    1919             : #if MAPPED_WRITING
    1920      738638 :   unsigned off = data - hdr.buffer;
    1921      738638 :   if (off + needed > extent)
    1922             :     {
    1923             :       /* We need to grow the mapping.  */
    1924         668 :       unsigned lwm = off & ~(page_size - 1);
    1925         668 :       unsigned hwm = (off + needed + page_size - 1) & ~(page_size - 1);
    1926             : 
    1927         668 :       gcc_checking_assert (hwm > extent);
    1928             : 
    1929         668 :       remove_mapping ();
    1930             : 
    1931         668 :       offset += lwm;
    1932         668 :       create_mapping (extent < hwm - lwm ? hwm - lwm : extent);
    1933             : 
    1934         668 :       data = hdr.buffer + (off - lwm);
    1935             :     }
    1936             : #else
    1937             :   data = allocator::grow (data, needed);
    1938             : #endif
    1939             : 
    1940      738638 :   return data;
    1941             : }
    1942             : 
    1943             : #if MAPPED_WRITING
    1944             : /* Shrinking is a NOP.  */
    1945             : void
    1946      314996 : elf_out::shrink (char *)
    1947             : {
    1948      314996 : }
    1949             : #endif
    1950             : 
    1951             : /* Write S of length L to the strtab buffer.  L must include the ending
    1952             :    NUL, if that's what you want.  */
    1953             : 
    1954             : unsigned
    1955     1405197 : elf_out::strtab_write (const char *s, unsigned l)
    1956             : {
    1957     1405197 :   if (strtab.pos + l > strtab.size)
    1958         932 :     data::simple_memory.grow (strtab, strtab.pos + l, false);
    1959     1405197 :   memcpy (strtab.buffer + strtab.pos, s, l);
    1960     1405197 :   unsigned res = strtab.pos;
    1961     1405197 :   strtab.pos += l;
    1962     1405197 :   return res;
    1963             : }
    1964             : 
    1965             : /* Write qualified name of decl.  INNER >0 if this is a definition, <0
    1966             :    if this is a qualifier of an outer name.  */
    1967             : 
    1968             : void
    1969      558629 : elf_out::strtab_write (tree decl, int inner)
    1970             : {
    1971      558629 :   tree ctx = CP_DECL_CONTEXT (decl);
    1972      558629 :   if (TYPE_P (ctx))
    1973        5078 :     ctx = TYPE_NAME (ctx);
    1974      558629 :   if (ctx != global_namespace)
    1975      267798 :     strtab_write (ctx, -1);
    1976             : 
    1977      558629 :   tree name = DECL_NAME (decl);
    1978      558629 :   if (!name)
    1979           0 :     name = DECL_ASSEMBLER_NAME_RAW (decl);
    1980      558629 :   strtab_write (IDENTIFIER_POINTER (name), IDENTIFIER_LENGTH (name));
    1981             : 
    1982      558629 :   if (inner)
    1983      384101 :     strtab_write (&"::{}"[inner+1], 2);
    1984      558629 : }
    1985             : 
    1986             : /* Map IDENTIFIER IDENT to strtab offset.  Inserts into strtab if not
    1987             :    already there.  */
    1988             : 
    1989             : unsigned
    1990      169279 : elf_out::name (tree ident)
    1991             : {
    1992      169279 :   unsigned res = 0;
    1993      169279 :   if (ident)
    1994             :     {
    1995      169279 :       bool existed;
    1996      169279 :       int *slot = &identtab.get_or_insert (ident, &existed);
    1997      169279 :       if (!existed)
    1998      304136 :         *slot = strtab_write (IDENTIFIER_POINTER (ident),
    1999      152068 :                               IDENTIFIER_LENGTH (ident) + 1);
    2000      169279 :       res = *slot;
    2001             :     }
    2002      169279 :   return res;
    2003             : }
    2004             : 
    2005             : /* Map LITERAL to strtab offset.  Does not detect duplicates and
    2006             :    expects LITERAL to remain live until strtab is written out.  */
    2007             : 
    2008             : unsigned
    2009       19568 : elf_out::name (const char *literal)
    2010             : {
    2011       19568 :   return strtab_write (literal, strlen (literal) + 1);
    2012             : }
    2013             : 
    2014             : /* Map a DECL's qualified name to strtab offset.  Does not detect
    2015             :    duplicates.  */
    2016             : 
    2017             : unsigned
    2018      290831 : elf_out::qualified_name (tree decl, bool is_defn)
    2019             : {
    2020      290831 :   gcc_checking_assert (DECL_P (decl) && decl != global_namespace);
    2021      290831 :   unsigned result = strtab.pos;
    2022             : 
    2023      290831 :   strtab_write (decl, is_defn);
    2024      290831 :   strtab_write ("", 1);
    2025             : 
    2026      290831 :   return result;
    2027             : }
    2028             : 
    2029             : /* Add section to file.  Return section number.  TYPE & NAME identify
    2030             :    the section.  OFF and SIZE identify the file location of its
    2031             :    data.  FLAGS contains additional info.  */
    2032             : 
    2033             : unsigned
    2034      318316 : elf_out::add (unsigned type, unsigned name, unsigned off, unsigned size,
    2035             :               unsigned flags)
    2036             : {
    2037      318316 :   gcc_checking_assert (!(off & (SECTION_ALIGN - 1)));
    2038      318316 :   if (sectab.pos + sizeof (section) > sectab.size)
    2039        2659 :     data::simple_memory.grow (sectab, sectab.pos + sizeof (section), false);
    2040      318316 :   section *sec = reinterpret_cast<section *> (sectab.buffer + sectab.pos);
    2041      318316 :   memset (sec, 0, sizeof (section));
    2042      318316 :   sec->type = type;
    2043      318316 :   sec->flags = flags;
    2044      318316 :   sec->name = name;
    2045      318316 :   sec->offset = off;
    2046      318316 :   sec->size = size;
    2047      318316 :   if (flags & SHF_STRINGS)
    2048        3320 :     sec->entsize = 1;
    2049             : 
    2050      318316 :   unsigned res = sectab.pos;
    2051      318316 :   sectab.pos += sizeof (section);
    2052      318316 :   return res / sizeof (section);
    2053             : }
    2054             : 
    2055             : /* Pad to the next alignment boundary, then write BUFFER to disk.
    2056             :    Return the position of the start of the write, or zero on failure.   */
    2057             : 
    2058             : unsigned
    2059        6637 : elf_out::write (const data &buffer)
    2060             : {
    2061             : #if MAPPED_WRITING
    2062             :   /* HDR is always mapped.  */
    2063        6637 :   if (&buffer != &hdr)
    2064             :     {
    2065        3320 :       bytes_out out (this);
    2066        3320 :       grow (out, buffer.pos, true);
    2067        3320 :       if (out.buffer)
    2068        3320 :         memcpy (out.buffer, buffer.buffer, buffer.pos);
    2069        3320 :       shrink (out);
    2070        3320 :     }
    2071             :   else
    2072             :     /* We should have been aligned during the first allocation.  */
    2073        3317 :     gcc_checking_assert (!(pos & (SECTION_ALIGN - 1)));
    2074             : #else
    2075             :   if (::write (fd, buffer.buffer, buffer.pos) != ssize_t (buffer.pos))
    2076             :     {
    2077             :       set_error (errno);
    2078             :       return 0;
    2079             :     }
    2080             : #endif
    2081        6637 :   unsigned res = pos;
    2082        6637 :   pos += buffer.pos;
    2083             : 
    2084        6637 :   if (unsigned padding = -pos & (SECTION_ALIGN - 1))
    2085             :     {
    2086             : #if !MAPPED_WRITING
    2087             :       /* Align the section on disk, should help the necessary copies.
    2088             :          fseeking to extend is non-portable.  */
    2089             :       static char zero[SECTION_ALIGN];
    2090             :       if (::write (fd, &zero, padding) != ssize_t (padding))
    2091             :         set_error (errno);
    2092             : #endif
    2093        5742 :       pos += padding;
    2094             :     }
    2095        6637 :   return res;
    2096             : }
    2097             : 
    2098             : /* Write a streaming buffer.  It must be using us as an allocator.  */
    2099             : 
    2100             : #if MAPPED_WRITING
    2101             : unsigned
    2102      314996 : elf_out::write (const bytes_out &buf)
    2103             : {
    2104      314996 :   gcc_checking_assert (buf.memory == this);
    2105             :   /* A directly mapped buffer.  */
    2106      314996 :   gcc_checking_assert (buf.buffer - hdr.buffer >= 0
    2107             :                        && buf.buffer - hdr.buffer + buf.size <= extent);
    2108      314996 :   unsigned res = pos;
    2109      314996 :   pos += buf.pos;
    2110             : 
    2111             :   /* Align up.  We're not going to advance into the next page. */
    2112      314996 :   pos += -pos & (SECTION_ALIGN - 1);
    2113             : 
    2114      314996 :   return res;
    2115             : }
    2116             : #endif
    2117             : 
    2118             : /* Write data and add section.  STRING_P is true for a string
    2119             :    section, false for PROGBITS.  NAME identifies the section (0 is the
    2120             :    empty name).  DATA is the contents.  Return section number or 0 on
    2121             :    failure (0 is the undef section).  */
    2122             : 
    2123             : unsigned
    2124      314996 : elf_out::add (const bytes_out &data, bool string_p, unsigned name)
    2125             : {
    2126      314996 :   unsigned off = write (data);
    2127             : 
    2128      629992 :   return add (string_p ? SHT_STRTAB : SHT_PROGBITS, name,
    2129      314996 :               off, data.pos, string_p ? SHF_STRINGS : SHF_NONE);
    2130             : }
    2131             : 
    2132             : /* Begin writing the file.  Initialize the section table and write an
    2133             :    empty header.  Return false on failure.  */
    2134             : 
    2135             : bool
    2136        1660 : elf_out::begin ()
    2137             : {
    2138        1660 :   if (!parent::begin ())
    2139             :     return false;
    2140             : 
    2141             :   /* Let the allocators pick a default.  */
    2142        1660 :   data::simple_memory.grow (strtab, 0, false);
    2143        1660 :   data::simple_memory.grow (sectab, 0, false);
    2144             : 
    2145             :   /* The string table starts with an empty string.  */
    2146        1660 :   name ("");
    2147             : 
    2148             :   /* Create the UNDEF section.  */
    2149        1660 :   add (SHT_NONE);
    2150             : 
    2151             : #if MAPPED_WRITING
    2152             :   /* Start a mapping.  */
    2153        1660 :   create_mapping (EXPERIMENT (page_size,
    2154             :                               (32767 + page_size) & ~(page_size - 1)));
    2155        1660 :   if (!hdr.buffer)
    2156             :     return false;
    2157             : #endif
    2158             : 
    2159             :   /* Write an empty header.  */
    2160        1660 :   grow (hdr, sizeof (header), true);
    2161        1660 :   header *h = reinterpret_cast<header *> (hdr.buffer);
    2162        1660 :   memset (h, 0, sizeof (header));
    2163        1660 :   hdr.pos = hdr.size;
    2164        1660 :   write (hdr);
    2165        1660 :   return !get_error ();
    2166             : }
    2167             : 
    2168             : /* Finish writing the file.  Write out the string & section tables.
    2169             :    Fill in the header.  Return true on error.  */
    2170             : 
    2171             : bool
    2172        1717 : elf_out::end ()
    2173             : {
    2174        1717 :   if (fd >= 0)
    2175             :     {
    2176             :       /* Write the string table.  */
    2177        1660 :       unsigned strnam = name (".strtab");
    2178        1660 :       unsigned stroff = write (strtab);
    2179        1660 :       unsigned strndx = add (SHT_STRTAB, strnam, stroff, strtab.pos,
    2180             :                              SHF_STRINGS);
    2181             : 
    2182             :       /* Store escape values in section[0].  */
    2183        1660 :       if (strndx >= SHN_LORESERVE)
    2184             :         {
    2185           0 :           reinterpret_cast<section *> (sectab.buffer)->link = strndx;
    2186           0 :           strndx = SHN_XINDEX;
    2187             :         }
    2188        1660 :       unsigned shnum = sectab.pos / sizeof (section);
    2189        1660 :       if (shnum >= SHN_LORESERVE)
    2190             :         {
    2191           0 :           reinterpret_cast<section *> (sectab.buffer)->size = shnum;
    2192           0 :           shnum = SHN_XINDEX;
    2193             :         }
    2194             : 
    2195        1660 :       unsigned shoff = write (sectab);
    2196             : 
    2197             : #if MAPPED_WRITING
    2198        1660 :       if (offset)
    2199             :         {
    2200         105 :           remove_mapping ();
    2201         105 :           offset = 0;
    2202         105 :           create_mapping ((sizeof (header) + page_size - 1) & ~(page_size - 1),
    2203             :                           false);
    2204             :         }
    2205        1660 :       unsigned length = pos;
    2206             : #else
    2207             :       if (lseek (fd, 0, SEEK_SET) < 0)
    2208             :         set_error (errno);
    2209             : #endif
    2210             :       /* Write header.  */
    2211        1660 :       if (!get_error ())
    2212             :         {
    2213             :           /* Write the correct header now.  */
    2214        1657 :           header *h = reinterpret_cast<header *> (hdr.buffer);
    2215        1657 :           h->ident.magic[0] = 0x7f;
    2216        1657 :           h->ident.magic[1] = 'E';   /* Elrond */
    2217        1657 :           h->ident.magic[2] = 'L';   /* is an */
    2218        1657 :           h->ident.magic[3] = 'F';   /* elf.  */
    2219        1657 :           h->ident.klass = MY_CLASS;
    2220        1657 :           h->ident.data =  MY_ENDIAN;
    2221        1657 :           h->ident.version = EV_CURRENT;
    2222        1657 :           h->ident.osabi = OSABI_NONE;
    2223        1657 :           h->type = ET_NONE;
    2224        1657 :           h->machine = EM_NONE;
    2225        1657 :           h->version = EV_CURRENT;
    2226        1657 :           h->shoff = shoff;
    2227        1657 :           h->ehsize = sizeof (header);
    2228        1657 :           h->shentsize = sizeof (section);
    2229        1657 :           h->shnum = shnum;
    2230        1657 :           h->shstrndx = strndx;
    2231             : 
    2232        1657 :           pos = 0;
    2233        1657 :           write (hdr);
    2234             :         }
    2235             : 
    2236             : #if MAPPED_WRITING
    2237        1660 :       remove_mapping ();
    2238        1660 :       if (ftruncate (fd, length))
    2239           0 :         set_error (errno);
    2240             : #endif
    2241             :     }
    2242             : 
    2243        1717 :   data::simple_memory.shrink (sectab);
    2244        1717 :   data::simple_memory.shrink (strtab);
    2245             : 
    2246        1717 :   return parent::end ();
    2247             : }
    2248             : 
    2249             : /********************************************************************/
    2250             : 
    2251             : /* A dependency set.  This is used during stream out to determine the
    2252             :    connectivity of the graph.  Every namespace-scope declaration that
    2253             :    needs writing has a depset.  The depset is filled with the (depsets
    2254             :    of) declarations within this module that it references.  For a
    2255             :    declaration that'll generally be named types.  For definitions
    2256             :    it'll also be declarations in the body.
    2257             : 
    2258             :    From that we can convert the graph to a DAG, via determining the
    2259             :    Strongly Connected Clusters.  Each cluster is streamed
    2260             :    independently, and thus we achieve lazy loading.
    2261             : 
    2262             :    Other decls that get a depset are namespaces themselves and
    2263             :    unnameable declarations.   */
    2264             : 
    2265             : class depset {
    2266             : private:
    2267             :   tree entity;  /* Entity, or containing namespace.  */
    2268             :   uintptr_t discriminator;  /* Flags or identifier.  */
    2269             : 
    2270             : public:
    2271             :   /* The kinds of entity the depset could describe.  The ordering is
    2272             :      significant, see entity_kind_name.  */
    2273             :   enum entity_kind
    2274             :   {
    2275             :     EK_DECL,            /* A decl.  */
    2276             :     EK_SPECIALIZATION,  /* A specialization.  */
    2277             :     EK_PARTIAL,         /* A partial specialization.  */
    2278             :     EK_USING,           /* A using declaration (at namespace scope).  */
    2279             :     EK_NAMESPACE,       /* A namespace.  */
    2280             :     EK_REDIRECT,        /* Redirect to a template_decl.  */
    2281             :     EK_EXPLICIT_HWM,  
    2282             :     EK_BINDING = EK_EXPLICIT_HWM, /* Implicitly encoded.  */
    2283             :     EK_FOR_BINDING,     /* A decl being inserted for a binding.  */
    2284             :     EK_INNER_DECL,      /* A decl defined outside of its imported
    2285             :                            context.  */
    2286             :     EK_DIRECT_HWM = EK_PARTIAL + 1,
    2287             : 
    2288             :     EK_BITS = 3         /* Only need to encode below EK_EXPLICIT_HWM.  */
    2289             :   };
    2290             : 
    2291             : private:
    2292             :   /* Placement of bit fields in discriminator.  */
    2293             :   enum disc_bits 
    2294             :   {
    2295             :     DB_ZERO_BIT, /* Set to disambiguate identifier from flags  */
    2296             :     DB_SPECIAL_BIT, /* First dep slot is special.  */
    2297             :     DB_KIND_BIT, /* Kind of the entity.  */
    2298             :     DB_KIND_BITS = EK_BITS,
    2299             :     DB_DEFN_BIT = DB_KIND_BIT + DB_KIND_BITS,
    2300             :     DB_IS_MEMBER_BIT,           /* Is an out-of-class member.  */
    2301             :     DB_IS_INTERNAL_BIT,         /* It is an (erroneous)
    2302             :                                    internal-linkage entity.  */
    2303             :     DB_REFS_INTERNAL_BIT,       /* Refers to an internal-linkage
    2304             :                                    entity. */
    2305             :     DB_IMPORTED_BIT,            /* An imported entity.  */
    2306             :     DB_UNREACHED_BIT,           /* A yet-to-be reached entity.  */
    2307             :     DB_HIDDEN_BIT,              /* A hidden binding.  */
    2308             :     /* The following bits are not independent, but enumerating them is
    2309             :        awkward.  */
    2310             :     DB_ALIAS_TMPL_INST_BIT,     /* An alias template instantiation. */
    2311             :     DB_ALIAS_SPEC_BIT,          /* Specialization of an alias template
    2312             :                                    (in both spec tables).  */
    2313             :     DB_TYPE_SPEC_BIT,           /* Specialization in the type table.
    2314             :                                    */
    2315             :     DB_FRIEND_SPEC_BIT,         /* An instantiated template friend.  */
    2316             :   };
    2317             : 
    2318             : public:
    2319             :   /* The first slot is special for EK_SPECIALIZATIONS it is a
    2320             :      spec_entry pointer.  It is not relevant for the SCC
    2321             :      determination.  */
    2322             :   vec<depset *> deps;  /* Depsets we reference.  */
    2323             : 
    2324             : public:
    2325             :   unsigned cluster; /* Strongly connected cluster, later entity number  */
    2326             :   unsigned section; /* Section written to.  */
    2327             :   /* During SCC construction, section is lowlink, until the depset is
    2328             :      removed from the stack.  See Tarjan algorithm for details.  */
    2329             : 
    2330             : private:
    2331             :   /* Construction via factories.  Destruction via hash traits.  */
    2332             :   depset (tree entity);
    2333             :   ~depset ();
    2334             : 
    2335             : public:
    2336             :   static depset *make_binding (tree, tree);
    2337             :   static depset *make_entity (tree, entity_kind, bool = false);
    2338             :   /* Late setting a binding name -- /then/ insert into hash!  */
    2339             :   inline void set_binding_name (tree name)
    2340             :   {
    2341             :     gcc_checking_assert (!get_name ());
    2342             :     discriminator = reinterpret_cast<uintptr_t> (name);
    2343             :   }
    2344             : 
    2345             : private:
    2346     2369945 :   template<unsigned I> void set_flag_bit ()
    2347             :   {
    2348           0 :     gcc_checking_assert (I < 2 || !is_binding ());
    2349     2369945 :     discriminator |= 1u << I;
    2350      778840 :   }
    2351      374332 :   template<unsigned I> void clear_flag_bit ()
    2352             :   {
    2353           0 :     gcc_checking_assert (I < 2 || !is_binding ());
    2354      374332 :     discriminator &= ~(1u << I);
    2355      374332 :   }
    2356   417500035 :   template<unsigned I> bool get_flag_bit () const
    2357             :   {
    2358           0 :     gcc_checking_assert (I < 2 || !is_binding ());
    2359   475412693 :     return bool ((discriminator >> I) & 1);
    2360             :   }
    2361             :   
    2362             : public:
    2363   408038651 :   bool is_binding () const
    2364             :   {
    2365    59065830 :     return !get_flag_bit<DB_ZERO_BIT> ();
    2366             :   }
    2367   254474002 :   entity_kind get_entity_kind () const
    2368             :   {
    2369       22229 :     if (is_binding ())
    2370             :       return EK_BINDING;
    2371   190309307 :     return entity_kind ((discriminator >> DB_KIND_BIT) & ((1u << EK_BITS) - 1));
    2372             :   }
    2373             :   const char *entity_kind_name () const;
    2374             : 
    2375             : public:
    2376     7777416 :   bool has_defn () const
    2377             :   {
    2378         765 :     return get_flag_bit<DB_DEFN_BIT> ();
    2379             :   }
    2380             : 
    2381             : public:
    2382             :   /* This class-member is defined here, but the class was imported.  */
    2383     1155398 :   bool is_member () const
    2384             :   {
    2385     1155398 :     gcc_checking_assert (get_entity_kind () == EK_DECL);
    2386     1155398 :     return get_flag_bit<DB_IS_MEMBER_BIT> ();
    2387             :   }
    2388             : public:
    2389     4684579 :   bool is_internal () const
    2390             :   {
    2391     4684579 :     return get_flag_bit<DB_IS_INTERNAL_BIT> ();
    2392             :   }
    2393     1225937 :   bool refs_internal () const
    2394             :   {
    2395     1225937 :     return get_flag_bit<DB_REFS_INTERNAL_BIT> ();
    2396             :   }
    2397    24048641 :   bool is_import () const
    2398             :   {
    2399     7665259 :     return get_flag_bit<DB_IMPORTED_BIT> ();
    2400             :   }
    2401    14378493 :   bool is_unreached () const
    2402             :   {
    2403     1040078 :     return get_flag_bit<DB_UNREACHED_BIT> ();
    2404             :   }
    2405           0 :   bool is_alias_tmpl_inst () const
    2406             :   {
    2407           0 :     return get_flag_bit<DB_ALIAS_TMPL_INST_BIT> ();
    2408             :   }
    2409      867603 :   bool is_alias () const
    2410             :   {
    2411      867603 :     return get_flag_bit<DB_ALIAS_SPEC_BIT> ();
    2412             :   }
    2413     1232702 :   bool is_hidden () const
    2414             :   {
    2415     1232702 :     return get_flag_bit<DB_HIDDEN_BIT> ();
    2416             :   }
    2417     1286425 :   bool is_type_spec () const
    2418             :   {
    2419       30961 :     return get_flag_bit<DB_TYPE_SPEC_BIT> ();
    2420             :   }
    2421     1255464 :   bool is_friend_spec () const
    2422             :   {
    2423     1255464 :     return get_flag_bit<DB_FRIEND_SPEC_BIT> ();
    2424             :   }
    2425             : 
    2426             : public:
    2427             :   /* We set these bit outside of depset.  */
    2428        4517 :   void set_hidden_binding ()
    2429             :   {
    2430        4517 :     set_flag_bit<DB_HIDDEN_BIT> ();
    2431        4517 :   }
    2432           9 :   void clear_hidden_binding ()
    2433             :   {
    2434           9 :     clear_flag_bit<DB_HIDDEN_BIT> ();
    2435           9 :   }
    2436             : 
    2437             : public:
    2438     9461384 :   bool is_special () const
    2439             :   {
    2440     9461384 :     return get_flag_bit<DB_SPECIAL_BIT> ();
    2441             :   }
    2442     1591105 :   void set_special ()
    2443             :   {
    2444     1591105 :     set_flag_bit<DB_SPECIAL_BIT> ();
    2445          45 :   }
    2446             : 
    2447             : public:
    2448   115255353 :   tree get_entity () const
    2449             :   {
    2450   115255353 :     return entity;
    2451             :   }
    2452    22404573 :   tree get_name () const
    2453             :   {
    2454    22404573 :     gcc_checking_assert (is_binding ());
    2455    22404573 :     return reinterpret_cast <tree> (discriminator);
    2456             :   }
    2457             : 
    2458             : public:
    2459             :   /* Traits for a hash table of pointers to bindings.  */
    2460             :   struct traits {
    2461             :     /* Each entry is a pointer to a depset. */
    2462             :     typedef depset *value_type;
    2463             :     /* We lookup by container:maybe-identifier pair.  */
    2464             :     typedef std::pair<tree,tree> compare_type;
    2465             : 
    2466             :     static const bool empty_zero_p = true;
    2467             : 
    2468             :     /* hash and equality for compare_type.  */
    2469    14369631 :     inline static hashval_t hash (const compare_type &p)
    2470             :     {
    2471    14369631 :       hashval_t h = pointer_hash<tree_node>::hash (p.first);
    2472    14369631 :       if (p.second)
    2473             :         {
    2474      167250 :           hashval_t nh = IDENTIFIER_HASH_VALUE (p.second);
    2475      167250 :           h = iterative_hash_hashval_t (h, nh);
    2476             :         }
    2477    14369631 :       return h;
    2478             :     }
    2479    58582026 :     inline static bool equal (const value_type b, const compare_type &p)
    2480             :     {
    2481    58582026 :       if (b->entity != p.first)
    2482             :         return false;
    2483             : 
    2484    10190504 :       if (p.second)
    2485       16561 :         return b->discriminator == reinterpret_cast<uintptr_t> (p.second);
    2486             :       else
    2487    10173943 :         return !b->is_binding ();
    2488             :     }
    2489             : 
    2490             :     /* (re)hasher for a binding itself.  */
    2491    43503565 :     inline static hashval_t hash (const value_type b)
    2492             :     {
    2493    43503565 :       hashval_t h = pointer_hash<tree_node>::hash (b->entity);
    2494    43503565 :       if (b->is_binding ())
    2495             :         {
    2496     5014425 :           hashval_t nh = IDENTIFIER_HASH_VALUE (b->get_name ());
    2497     5014425 :           h = iterative_hash_hashval_t (h, nh);
    2498             :         }
    2499    43503565 :       return h;
    2500             :     }
    2501             : 
    2502             :     /* Empty via NULL.  */
    2503           0 :     static inline void mark_empty (value_type &p) {p = NULL;}
    2504             :     static inline bool is_empty (value_type p) {return !p;}
    2505             : 
    2506             :     /* Nothing is deletable.  Everything is insertable.  */
    2507             :     static bool is_deleted (value_type) { return false; }
    2508             :     static void mark_deleted (value_type) { gcc_unreachable (); }
    2509             : 
    2510             :     /* We own the entities in the hash table.  */
    2511     2411036 :     static void remove (value_type p)
    2512             :     {
    2513     2411036 :       delete (p);
    2514     2411036 :     }
    2515             :   };
    2516             : 
    2517             : public:
    2518             :   class hash : public hash_table<traits> {
    2519             :     typedef traits::compare_type key_t;
    2520             :     typedef hash_table<traits> parent;
    2521             : 
    2522             :   public:
    2523             :     vec<depset *> worklist;  /* Worklist of decls to walk.  */
    2524             :     hash *chain;             /* Original table.  */
    2525             :     depset *current;         /* Current depset being depended.  */
    2526             :     unsigned section;        /* When writing out, the section.  */
    2527             :     bool sneakoscope;        /* Detecting dark magic (of a voldemort).  */
    2528             :     bool reached_unreached;  /* We reached an unreached entity.  */
    2529             : 
    2530             :   public:
    2531      303728 :     hash (size_t size, hash *c = NULL)
    2532      607456 :       : parent (size), chain (c), current (NULL), section (0),
    2533      303728 :         sneakoscope (false), reached_unreached (false)
    2534             :     {
    2535      303728 :       worklist.create (size);
    2536      303728 :     }
    2537      303728 :     ~hash ()
    2538             :     {
    2539      303728 :       worklist.release ();
    2540      303728 :     }
    2541             : 
    2542             :   public:
    2543    40454715 :     bool is_key_order () const
    2544             :     {
    2545    40454715 :       return chain != NULL;
    2546             :     }
    2547             : 
    2548             :   private:
    2549             :     depset **entity_slot (tree entity, bool = true);
    2550             :     depset **binding_slot (tree ctx, tree name, bool = true);
    2551             :     depset *maybe_add_declaration (tree decl);
    2552             : 
    2553             :   public:
    2554             :     depset *find_dependency (tree entity);
    2555             :     depset *find_binding (tree ctx, tree name);
    2556             :     depset *make_dependency (tree decl, entity_kind);
    2557             :     void add_dependency (depset *);
    2558             : 
    2559             :   public:
    2560             :     void add_mergeable (depset *);
    2561             :     depset *add_dependency (tree decl, entity_kind);
    2562             :     void add_namespace_context (depset *, tree ns);
    2563             : 
    2564             :   private:
    2565             :     static bool add_binding_entity (tree, WMB_Flags, void *);
    2566             : 
    2567             :   public:
    2568             :     bool add_namespace_entities (tree ns, bitmap partitions);
    2569             :     void add_specializations (bool decl_p);
    2570             :     void add_partial_entities (vec<tree, va_gc> *);
    2571             :     void add_class_entities (vec<tree, va_gc> *);
    2572             : 
    2573             :   public:    
    2574             :     void find_dependencies (module_state *);
    2575             :     bool finalize_dependencies ();
    2576             :     vec<depset *> connect ();
    2577             :   };
    2578             : 
    2579             : public:
    2580             :   struct tarjan {
    2581             :     vec<depset *> result;
    2582             :     vec<depset *> stack;
    2583             :     unsigned index;
    2584             : 
    2585      303725 :     tarjan (unsigned size)
    2586      303725 :       : index (0)
    2587             :     {
    2588      303725 :       result.create (size);
    2589      303725 :       stack.create (50);
    2590      303725 :     }
    2591      303725 :     ~tarjan () 
    2592             :     {
    2593      303725 :       gcc_assert (!stack.length ());
    2594      303725 :       stack.release ();
    2595      303725 :     }
    2596             : 
    2597             :   public:
    2598             :     void connect (depset *);
    2599             :   };
    2600             : };
    2601             : 
    2602             : inline
    2603     2411036 : depset::depset (tree entity)
    2604     2411036 :   :entity (entity), discriminator (0), cluster (0), section (0)
    2605             : {
    2606     2411036 :   deps.create (0);
    2607             : }
    2608             : 
    2609             : inline
    2610     2411036 : depset::~depset ()
    2611             : {
    2612     2411036 :   deps.release ();
    2613     2411036 : }
    2614             : 
    2615             : const char *
    2616      193842 : depset::entity_kind_name () const
    2617             : {
    2618             :   /* Same order as entity_kind.  */
    2619      193842 :   static const char *const names[] = 
    2620             :     {"decl", "specialization", "partial", "using",
    2621             :      "namespace", "redirect", "binding"};
    2622      193842 :   entity_kind kind = get_entity_kind ();
    2623      193647 :   gcc_checking_assert (kind < ARRAY_SIZE (names));
    2624      193842 :   return names[kind];
    2625             : }
    2626             : 
    2627             : /* Create a depset for a namespace binding NS::NAME.  */
    2628             : 
    2629      167250 : depset *depset::make_binding (tree ns, tree name)
    2630             : {
    2631      167250 :   depset *binding = new depset (ns);
    2632             : 
    2633      167250 :   binding->discriminator = reinterpret_cast <uintptr_t> (name);
    2634             : 
    2635      167250 :   return binding;
    2636             : }
    2637             : 
    2638     2243786 : depset *depset::make_entity (tree entity, entity_kind ek, bool is_defn)
    2639             : {
    2640     2243786 :   depset *r = new depset (entity);
    2641             : 
    2642     2243786 :   r->discriminator = ((1 << DB_ZERO_BIT)
    2643     2219328 :                       | (ek << DB_KIND_BIT)
    2644     2219328 :                       | is_defn << DB_DEFN_BIT);
    2645             : 
    2646     2243786 :   return r;
    2647             : }
    2648             : 
    2649             : class pending_key
    2650             : {
    2651             : public:
    2652             :   tree ns;
    2653             :   tree id;
    2654             : };
    2655             : 
    2656             : template<>
    2657             : struct default_hash_traits<pending_key>
    2658             : {
    2659             :   using value_type = pending_key;
    2660             : 
    2661             :   static const bool empty_zero_p = false;
    2662    32421392 :   static hashval_t hash (const value_type &k)
    2663             :   {
    2664    32421392 :     hashval_t h = IDENTIFIER_HASH_VALUE (k.id);
    2665    32421392 :     h = iterative_hash_hashval_t (DECL_UID (k.ns), h);
    2666             : 
    2667    32421392 :     return h;
    2668             :   }
    2669     6047580 :   static bool equal (const value_type &k, const value_type &l)
    2670             :   {
    2671     6047580 :     return k.ns == l.ns && k.id == l.id;
    2672             :   }
    2673      177876 :   static void mark_empty (value_type &k)
    2674             :   {
    2675      177876 :     k.ns = k.id = NULL_TREE;
    2676             :   }
    2677        3201 :   static void mark_deleted (value_type &k)
    2678             :   {
    2679        3201 :     k.ns = NULL_TREE;
    2680        3201 :     gcc_checking_assert (k.id);
    2681        3201 :   }
    2682   259649806 :   static bool is_empty (const value_type &k)
    2683             :   {
    2684   259605811 :     return k.ns == NULL_TREE && k.id == NULL_TREE;
    2685             :   }
    2686     7846066 :   static bool is_deleted (const value_type &k)
    2687             :   {
    2688     7846066 :     return k.ns == NULL_TREE && k.id != NULL_TREE;
    2689             :   }
    2690             :   static void remove (value_type &)
    2691             :   {
    2692             :   }
    2693             : };
    2694             : 
    2695             : typedef hash_map<pending_key, auto_vec<unsigned>> pending_map_t;
    2696             : 
    2697             : /* Not-loaded entities that are keyed to a namespace-scope
    2698             :    identifier.  See module_state::write_pendings for details.  */
    2699             : pending_map_t *pending_table;
    2700             : 
    2701             : /* Decls that need some post processing once a batch of lazy loads has
    2702             :    completed.  */
    2703             : vec<tree, va_heap, vl_embed> *post_load_decls;
    2704             : 
    2705             : /* Some entities are keyed to another entitity for ODR purposes.
    2706             :    For example, at namespace scope, 'inline auto var = []{};', that
    2707             :    lambda is keyed to 'var', and follows its ODRness.  */
    2708             : typedef hash_map<tree, auto_vec<tree>> keyed_map_t;
    2709             : static keyed_map_t *keyed_table;
    2710             : 
    2711             : /********************************************************************/
    2712             : /* Tree streaming.   The tree streaming is very specific to the tree
    2713             :    structures themselves.  A tag indicates the kind of tree being
    2714             :    streamed.  -ve tags indicate backreferences to already-streamed
    2715             :    trees.  Backreferences are auto-numbered.  */
    2716             : 
    2717             : /* Tree tags.  */
    2718             : enum tree_tag {
    2719             :   tt_null,              /* NULL_TREE.  */
    2720             :   tt_fixed,             /* Fixed vector index.  */
    2721             : 
    2722             :   tt_node,              /* By-value node.  */
    2723             :   tt_decl,              /* By-value mergeable decl.  */
    2724             :   tt_tpl_parm,          /* Template parm.  */
    2725             : 
    2726             :   /* The ordering of the following 4 is relied upon in
    2727             :      trees_out::tree_node.  */
    2728             :   tt_id,                /* Identifier node.  */
    2729             :   tt_conv_id,           /* Conversion operator name.  */
    2730             :   tt_anon_id,           /* Anonymous name.  */
    2731             :   tt_lambda_id,         /* Lambda name.  */
    2732             : 
    2733             :   tt_typedef_type,      /* A (possibly implicit) typedefed type.  */
    2734             :   tt_derived_type,      /* A type derived from another type.  */
    2735             :   tt_variant_type,      /* A variant of another type.  */
    2736             : 
    2737             :   tt_tinfo_var,         /* Typeinfo object. */
    2738             :   tt_tinfo_typedef,     /* Typeinfo typedef.  */
    2739             :   tt_ptrmem_type,       /* Pointer to member type.  */
    2740             :   tt_nttp_var,          /* NTTP_OBJECT VAR_DECL.  */
    2741             : 
    2742             :   tt_parm,              /* Function parameter or result.  */
    2743             :   tt_enum_value,        /* An enum value.  */
    2744             :   tt_enum_decl,         /* An enum decl.  */
    2745             :   tt_data_member,       /* Data member/using-decl.  */
    2746             : 
    2747             :   tt_binfo,             /* A BINFO.  */
    2748             :   tt_vtable,            /* A vtable.  */
    2749             :   tt_thunk,             /* A thunk.  */
    2750             :   tt_clone_ref,
    2751             : 
    2752             :   tt_entity,            /* A extra-cluster entity.  */
    2753             : 
    2754             :   tt_template,          /* The TEMPLATE_RESULT of a template.  */
    2755             : };
    2756             : 
    2757             : enum walk_kind {
    2758             :   WK_none,      /* No walk to do (a back- or fixed-ref happened).  */
    2759             :   WK_normal,    /* Normal walk (by-name if possible).  */
    2760             : 
    2761             :   WK_value,     /* By-value walk.  */
    2762             : };
    2763             : 
    2764             : enum merge_kind
    2765             : {
    2766             :   MK_unique,    /* Known unique.  */
    2767             :   MK_named,     /* Found by CTX, NAME + maybe_arg types etc.  */
    2768             :   MK_field,     /* Found by CTX and index on TYPE_FIELDS  */
    2769             :   MK_vtable,    /* Found by CTX and index on TYPE_VTABLES  */
    2770             :   MK_as_base,   /* Found by CTX.  */
    2771             : 
    2772             :   MK_partial,
    2773             : 
    2774             :   MK_enum,      /* Found by CTX, & 1stMemberNAME.  */
    2775             :   MK_keyed,     /* Found by key & index.  */
    2776             : 
    2777             :   MK_friend_spec,  /* Like named, but has a tmpl & args too.  */
    2778             :   MK_local_friend, /* Found by CTX, index.  */
    2779             : 
    2780             :   MK_indirect_lwm = MK_enum,
    2781             :   
    2782             :   /* Template specialization kinds below. These are all found via
    2783             :      primary template and specialization args.  */
    2784             :   MK_template_mask = 0x10,  /* A template specialization.  */
    2785             : 
    2786             :   MK_tmpl_decl_mask = 0x4, /* In decl table.  */
    2787             :   MK_tmpl_alias_mask = 0x2, /* Also in type table  */
    2788             : 
    2789             :   MK_tmpl_tmpl_mask = 0x1, /* We want TEMPLATE_DECL.  */
    2790             : 
    2791             :   MK_type_spec = MK_template_mask,
    2792             :   MK_decl_spec = MK_template_mask | MK_tmpl_decl_mask,
    2793             :   MK_alias_spec = MK_decl_spec | MK_tmpl_alias_mask,
    2794             : 
    2795             :   MK_hwm = 0x20
    2796             : };
    2797             : /* This is more than a debugging array.  NULLs are used to determine
    2798             :    an invalid merge_kind number.  */
    2799             : static char const *const merge_kind_name[MK_hwm] =
    2800             :   {
    2801             :     "unique", "named", "field", "vtable",       /* 0...3  */
    2802             :     "asbase", "partial", "enum", "attached",    /* 4...7  */
    2803             : 
    2804             :     "friend spec", "local friend", NULL, NULL,  /* 8...11 */
    2805             :     NULL, NULL, NULL, NULL,
    2806             : 
    2807             :     "type spec", "type tmpl spec",  /* 16,17 type (template).  */
    2808             :     NULL, NULL,
    2809             : 
    2810             :     "decl spec", "decl tmpl spec",  /* 20,21 decl (template).  */
    2811             :     "alias spec", "alias tmpl spec",        /* 22,23 alias (template). */
    2812             :     NULL, NULL, NULL, NULL,
    2813             :     NULL, NULL, NULL, NULL,
    2814             :   };
    2815             : 
    2816             : /* Mergeable entity location data.  */
    2817             : struct merge_key {
    2818             :   cp_ref_qualifier ref_q : 2;
    2819             :   unsigned index;
    2820             : 
    2821             :   tree ret;  /* Return type, if appropriate.  */
    2822             :   tree args; /* Arg types, if appropriate.  */
    2823             : 
    2824             :   tree constraints;  /* Constraints.  */
    2825             : 
    2826     2882142 :   merge_key ()
    2827     2882142 :     :ref_q (REF_QUAL_NONE), index (0),
    2828     2882142 :      ret (NULL_TREE), args (NULL_TREE),
    2829     2882142 :      constraints (NULL_TREE)
    2830             :   {
    2831             :   }
    2832             : };
    2833             : 
    2834             : /* Hashmap of merged duplicates.  Usually decls, but can contain
    2835             :    BINFOs.  */
    2836             : typedef hash_map<tree,uintptr_t,
    2837             :                  simple_hashmap_traits<nodel_ptr_hash<tree_node>,uintptr_t> >
    2838             : duplicate_hash_map;
    2839             : 
    2840             : /* Tree stream reader.  Note that reading a stream doesn't mark the
    2841             :    read trees with TREE_VISITED.  Thus it's quite safe to have
    2842             :    multiple concurrent readers.  Which is good, because lazy
    2843             :    loading. */
    2844             : class trees_in : public bytes_in {
    2845             :   typedef bytes_in parent;
    2846             : 
    2847             : private:
    2848             :   module_state *state;          /* Module being imported.  */
    2849             :   vec<tree> back_refs;            /* Back references.  */
    2850             :   duplicate_hash_map *duplicates;       /* Map from existings to duplicate.  */
    2851             :   vec<tree> post_decls;           /* Decls to post process.  */
    2852             :   unsigned unused;              /* Inhibit any interior TREE_USED
    2853             :                                    marking.  */
    2854             : 
    2855             : public:
    2856             :   trees_in (module_state *);
    2857             :   ~trees_in ();
    2858             : 
    2859             : public:
    2860             :   int insert (tree);
    2861             :   tree back_ref (int);
    2862             : 
    2863             : private:
    2864             :   tree start (unsigned = 0);
    2865             : 
    2866             : public:
    2867             :   /* Needed for binfo writing  */
    2868             :   bool core_bools (tree);
    2869             : 
    2870             : private:
    2871             :   /* Stream tree_core, lang_decl_specific and lang_type_specific
    2872             :      bits.  */
    2873             :   bool core_vals (tree);
    2874             :   bool lang_type_bools (tree);
    2875             :   bool lang_type_vals (tree);
    2876             :   bool lang_decl_bools (tree);
    2877             :   bool lang_decl_vals (tree);
    2878             :   bool lang_vals (tree);
    2879             :   bool tree_node_bools (tree);
    2880             :   bool tree_node_vals (tree);
    2881             :   tree tree_value ();
    2882             :   tree decl_value ();
    2883             :   tree tpl_parm_value ();
    2884             : 
    2885             : private:
    2886             :   tree chained_decls ();  /* Follow DECL_CHAIN.  */
    2887             :   vec<tree, va_heap> *vec_chained_decls ();
    2888             :   vec<tree, va_gc> *tree_vec (); /* vec of tree.  */
    2889             :   vec<tree_pair_s, va_gc> *tree_pair_vec (); /* vec of tree_pair.  */
    2890             :   tree tree_list (bool has_purpose);
    2891             : 
    2892             : public:
    2893             :   /* Read a tree node.  */
    2894             :   tree tree_node (bool is_use = false);
    2895             : 
    2896             : private:
    2897             :   bool install_entity (tree decl);
    2898             :   tree tpl_parms (unsigned &tpl_levels);
    2899             :   bool tpl_parms_fini (tree decl, unsigned tpl_levels);
    2900             :   bool tpl_header (tree decl, unsigned *tpl_levels);
    2901             :   int fn_parms_init (tree);
    2902             :   void fn_parms_fini (int tag, tree fn, tree existing, bool has_defn);
    2903             :   unsigned add_indirect_tpl_parms (tree);
    2904             : public:
    2905             :   bool add_indirects (tree);
    2906             : 
    2907             : public:
    2908             :   /* Serialize various definitions. */
    2909             :   bool read_definition (tree decl);
    2910             :   
    2911             : private:
    2912             :   bool is_matching_decl (tree existing, tree decl, bool is_typedef);
    2913             :   static bool install_implicit_member (tree decl);
    2914             :   bool read_function_def (tree decl, tree maybe_template);
    2915             :   bool read_var_def (tree decl, tree maybe_template);
    2916             :   bool read_class_def (tree decl, tree maybe_template);
    2917             :   bool read_enum_def (tree decl, tree maybe_template);
    2918             : 
    2919             : public:
    2920             :   tree decl_container ();
    2921             :   tree key_mergeable (int tag, merge_kind, tree decl, tree inner, tree type,
    2922             :                       tree container, bool is_attached);
    2923             :   unsigned binfo_mergeable (tree *);
    2924             : 
    2925             : private:
    2926             :   uintptr_t *find_duplicate (tree existing);
    2927             :   void register_duplicate (tree decl, tree existing);
    2928             :   /* Mark as an already diagnosed bad duplicate.  */
    2929          21 :   void unmatched_duplicate (tree existing)
    2930             :   {
    2931          42 :     *find_duplicate (existing) |= 1;
    2932          21 :   }
    2933             : 
    2934             : public:
    2935      220221 :   bool is_duplicate (tree decl)
    2936             :   {
    2937      440442 :     return find_duplicate (decl) != NULL;
    2938             :   }
    2939       11674 :   tree maybe_duplicate (tree decl)
    2940             :   {
    2941       11674 :     if (uintptr_t *dup = find_duplicate (decl))
    2942       11665 :       return reinterpret_cast<tree> (*dup & ~uintptr_t (1));
    2943             :     return decl;
    2944             :   }
    2945             :   tree odr_duplicate (tree decl, bool has_defn);
    2946             : 
    2947             : public:
    2948             :   /* Return the next decl to postprocess, or NULL.  */
    2949      457376 :   tree post_process ()
    2950             :   {
    2951      457376 :     return post_decls.length () ? post_decls.pop () : NULL_TREE;
    2952             :   }
    2953             : private:
    2954             :   /* Register DECL for postprocessing.  */
    2955      127704 :   void post_process (tree decl)
    2956             :   {
    2957      127704 :     post_decls.safe_push (decl);
    2958             :   }
    2959             : 
    2960             : private:
    2961             :   void assert_definition (tree, bool installing);
    2962             : };
    2963             : 
    2964      331746 : trees_in::trees_in (module_state *state)
    2965      331746 :   :parent (), state (state), unused (0)
    2966             : {
    2967      331746 :   duplicates = NULL;
    2968      331746 :   back_refs.create (500);
    2969      331746 :   post_decls.create (0);
    2970      331746 : }
    2971             : 
    2972      331746 : trees_in::~trees_in ()
    2973             : {
    2974      522144 :   delete (duplicates);
    2975      331746 :   back_refs.release ();
    2976      331746 :   post_decls.release ();
    2977      331746 : }
    2978             : 
    2979             : /* Tree stream writer.  */
    2980             : class trees_out : public bytes_out {
    2981             :   typedef bytes_out parent;
    2982             : 
    2983             : private:
    2984             :   module_state *state;          /* The module we are writing.  */
    2985             :   ptr_int_hash_map tree_map;    /* Trees to references */
    2986             :   depset::hash *dep_hash;       /* Dependency table.  */
    2987             :   int ref_num;                  /* Back reference number.  */
    2988             :   unsigned section;
    2989             : #if CHECKING_P
    2990             :   int importedness;             /* Checker that imports not occurring
    2991             :                                    inappropriately.  +ve imports ok,
    2992             :                                    -ve imports not ok.  */
    2993             : #endif
    2994             : 
    2995             : public:
    2996             :   trees_out (allocator *, module_state *, depset::hash &deps, unsigned sec = 0);
    2997             :   ~trees_out ();
    2998             : 
    2999             : private:
    3000             :   void mark_trees ();
    3001             :   void unmark_trees ();
    3002             : 
    3003             : public:
    3004             :   /* Hey, let's ignore the well known STL iterator idiom.  */
    3005             :   void begin ();
    3006             :   unsigned end (elf_out *sink, unsigned name, unsigned *crc_ptr);
    3007             :   void end ();
    3008             : 
    3009             : public:
    3010             :   enum tags 
    3011             :   {
    3012             :     tag_backref = -1,   /* Upper bound on the backrefs.  */
    3013             :     tag_value = 0,      /* Write by value.  */
    3014             :     tag_fixed           /* Lower bound on the fixed trees.  */
    3015             :   };
    3016             : 
    3017             : public:
    3018    19315226 :   bool is_key_order () const
    3019             :   {
    3020    19315226 :     return dep_hash->is_key_order ();
    3021             :   }
    3022             : 
    3023             : public:
    3024             :   int insert (tree, walk_kind = WK_normal);
    3025             : 
    3026             : private:
    3027             :   void start (tree, bool = false);
    3028             : 
    3029             : private:
    3030             :   walk_kind ref_node (tree);
    3031             : public:
    3032             :   int get_tag (tree);
    3033      604136 :   void set_importing (int i ATTRIBUTE_UNUSED)
    3034             :   {
    3035             : #if CHECKING_P
    3036      604136 :     importedness = i;
    3037             : #endif
    3038             :   }
    3039             : 
    3040             : private:
    3041             :   void core_bools (tree);
    3042             :   void core_vals (tree);
    3043             :   void lang_type_bools (tree);
    3044             :   void lang_type_vals (tree);
    3045             :   void lang_decl_bools (tree);
    3046             :   void lang_decl_vals (tree);
    3047             :   void lang_vals (tree);
    3048             :   void tree_node_bools (tree);
    3049             :   void tree_node_vals (tree);
    3050             : 
    3051             : private:
    3052             :   void chained_decls (tree);
    3053             :   void vec_chained_decls (tree);
    3054             :   void tree_vec (vec<tree, va_gc> *);
    3055             :   void tree_pair_vec (vec<tree_pair_s, va_gc> *);
    3056             :   void tree_list (tree, bool has_purpose);
    3057             : 
    3058             : public:
    3059             :   /* Mark a node for by-value walking.  */
    3060             :   void mark_by_value (tree);
    3061             : 
    3062             : public:
    3063             :   void tree_node (tree);
    3064             : 
    3065             : private:
    3066             :   void install_entity (tree decl, depset *);
    3067             :   void tpl_parms (tree parms, unsigned &tpl_levels);
    3068             :   void tpl_parms_fini (tree decl, unsigned tpl_levels);
    3069             :   void fn_parms_fini (tree) {}
    3070             :   unsigned add_indirect_tpl_parms (tree);
    3071             : public:
    3072             :   void add_indirects (tree);
    3073             :   void fn_parms_init (tree);
    3074             :   void tpl_header (tree decl, unsigned *tpl_levels);
    3075             : 
    3076             : public:
    3077             :   merge_kind get_merge_kind (tree decl, depset *maybe_dep);
    3078             :   tree decl_container (tree decl);
    3079             :   void key_mergeable (int tag, merge_kind, tree decl, tree inner,
    3080             :                       tree container, depset *maybe_dep);
    3081             :   void binfo_mergeable (tree binfo);
    3082             : 
    3083             : private:
    3084             :   bool decl_node (tree, walk_kind ref);
    3085             :   void type_node (tree);
    3086             :   void tree_value (tree);
    3087             :   void tpl_parm_value (tree);
    3088             : 
    3089             : public:
    3090             :   void decl_value (tree, depset *);
    3091             : 
    3092             : public:
    3093             :   /* Serialize various definitions. */
    3094             :   void write_definition (tree decl);
    3095             :   void mark_declaration (tree decl, bool do_defn);
    3096             : 
    3097             : private:
    3098             :   void mark_function_def (tree decl);
    3099             :   void mark_var_def (tree decl);
    3100             :   void mark_class_def (tree decl);
    3101             :   void mark_enum_def (tree decl);
    3102             :   void mark_class_member (tree decl, bool do_defn = true);
    3103             :   void mark_binfos (tree type);
    3104             : 
    3105             : private:
    3106             :   void write_var_def (tree decl);
    3107             :   void write_function_def (tree decl);
    3108             :   void write_class_def (tree decl);
    3109             :   void write_enum_def (tree decl);
    3110             : 
    3111             : private:
    3112             :   static void assert_definition (tree);
    3113             : 
    3114             : public:
    3115             :   static void instrument ();
    3116             : 
    3117             : private:
    3118             :   /* Tree instrumentation. */
    3119             :   static unsigned tree_val_count;
    3120             :   static unsigned decl_val_count;
    3121             :   static unsigned back_ref_count;
    3122             :   static unsigned null_count;
    3123             : };
    3124             : 
    3125             : /* Instrumentation counters.  */
    3126             : unsigned trees_out::tree_val_count;
    3127             : unsigned trees_out::decl_val_count;
    3128             : unsigned trees_out::back_ref_count;
    3129             : unsigned trees_out::null_count;
    3130             : 
    3131      607471 : trees_out::trees_out (allocator *mem, module_state *state, depset::hash &deps,
    3132      607471 :                       unsigned section)
    3133     1214942 :   :parent (mem), state (state), tree_map (500),
    3134      607471 :    dep_hash (&deps), ref_num (0), section (section)
    3135             : {
    3136             : #if CHECKING_P
    3137      607471 :   importedness = 0;
    3138             : #endif
    3139      607471 : }
    3140             : 
    3141      607471 : trees_out::~trees_out ()
    3142             : {
    3143      607471 : }
    3144             : 
    3145             : /********************************************************************/
    3146             : /* Location.  We're aware of the line-map concept and reproduce it
    3147             :    here.  Each imported module allocates a contiguous span of ordinary
    3148             :    maps, and of macro maps.  adhoc maps are serialized by contents,
    3149             :    not pre-allocated.   The scattered linemaps of a module are
    3150             :    coalesced when writing.  */
    3151             : 
    3152             : 
    3153             : /* I use half-open [first,second) ranges.  */
    3154             : typedef std::pair<unsigned,unsigned> range_t;
    3155             : 
    3156             : /* A range of locations.  */
    3157             : typedef std::pair<location_t,location_t> loc_range_t;
    3158             : 
    3159             : /* Spans of the line maps that are occupied by this TU.  I.e. not
    3160             :    within imports.  Only extended when in an interface unit.
    3161             :    Interval zero corresponds to the forced header linemap(s).  This
    3162             :    is a singleton object.  */
    3163             : 
    3164             : class loc_spans {
    3165             : public:
    3166             :   /* An interval of line maps.  The line maps here represent a contiguous
    3167             :      non-imported range.  */
    3168       12998 :   struct span {
    3169             :     loc_range_t ordinary;       /* Ordinary map location range. */
    3170             :     loc_range_t macro;          /* Macro map location range.  */
    3171             :     int ordinary_delta; /* Add to ordinary loc to get serialized loc.  */
    3172             :     int macro_delta;    /* Likewise for macro loc.  */
    3173             :   };
    3174             : 
    3175             : private:
    3176             :   vec<span> *spans;
    3177             : 
    3178             : public:
    3179             :   loc_spans ()
    3180             :     /* Do not preallocate spans, as that causes
    3181             :        --enable-detailed-mem-stats problems.  */
    3182             :     : spans (nullptr)
    3183             :   {
    3184             :   }
    3185       89300 :   ~loc_spans ()
    3186             :   {
    3187       89300 :     delete spans;
    3188       89300 :   }
    3189             : 
    3190             : public:
    3191         225 :   span &operator[] (unsigned ix)
    3192             :   {
    3193         450 :     return (*spans)[ix];
    3194             :   }
    3195             :   unsigned length () const
    3196             :   {
    3197             :     return spans->length ();
    3198             :   }
    3199             : 
    3200             : public:
    3201       33618 :   bool init_p () const
    3202             :   {
    3203       33618 :     return spans != nullptr;
    3204             :   }
    3205             :   /* Initializer.  */
    3206             :   void init (const line_maps *lmaps, const line_map_ordinary *map);
    3207             : 
    3208             :   /* Slightly skewed preprocessed files can cause us to miss an
    3209             :      initialization in some places.  Fallback initializer.  */
    3210        3536 :   void maybe_init ()
    3211             :   {
    3212        3536 :     if (!init_p ())
    3213           3 :       init (line_table, nullptr);
    3214        3536 :   }
    3215             : 
    3216             : public:
    3217             :   enum {
    3218             :     SPAN_RESERVED = 0,  /* Reserved (fixed) locations.  */
    3219             :     SPAN_FIRST = 1,     /* LWM of locations to stream  */
    3220             :     SPAN_MAIN = 2       /* Main file and onwards.  */
    3221             :   };
    3222             : 
    3223             : public:
    3224      360884 :   location_t main_start () const
    3225             :   {
    3226      360884 :     return (*spans)[SPAN_MAIN].ordinary.first;
    3227             :   }
    3228             : 
    3229             : public:
    3230             :   void open (location_t);
    3231             :   void close ();
    3232             : 
    3233             : public:
    3234             :   /* Propagate imported linemaps to us, if needed.  */
    3235             :   bool maybe_propagate (module_state *import, location_t loc);
    3236             : 
    3237             : public:
    3238             :   const span *ordinary (location_t);
    3239             :   const span *macro (location_t);
    3240             : };
    3241             : 
    3242             : static loc_spans spans;
    3243             : 
    3244             : /* Information about ordinary locations we stream out.  */
    3245             : struct ord_loc_info
    3246             : {
    3247             :   const line_map_ordinary *src; // line map we're based on
    3248             :   unsigned offset;      // offset to this line
    3249             :   unsigned span;        // number of locs we span
    3250             :   unsigned remap;       // serialization
    3251             : 
    3252   240563873 :   static int compare (const void *a_, const void *b_)
    3253             :   {
    3254   240563873 :     auto *a = static_cast<const ord_loc_info *> (a_);
    3255   240563873 :     auto *b = static_cast<const ord_loc_info *> (b_);
    3256             : 
    3257   240563873 :     if (a->src != b->src)
    3258    56084562 :       return a->src < b->src ? -1 : +1;
    3259             : 
    3260             :     // Ensure no overlap
    3261   202665692 :     gcc_checking_assert (a->offset + a->span <= b->offset
    3262             :                          || b->offset + b->span <= a->offset);
    3263             : 
    3264   202665692 :     gcc_checking_assert (a->offset != b->offset);
    3265   202665692 :     return a->offset < b->offset ? -1 : +1;
    3266             :   }
    3267             : };
    3268             : struct ord_loc_traits
    3269             : {
    3270             :   typedef ord_loc_info value_type;
    3271             :   typedef value_type compare_type;
    3272             : 
    3273             :   static const bool empty_zero_p = false;
    3274             : 
    3275   100391767 :   static hashval_t hash (const value_type &v)
    3276             :   {
    3277    85386092 :     auto h = pointer_hash<const line_map_ordinary>::hash (v.src);
    3278   100391767 :     return iterative_hash_hashval_t (v.offset, h);
    3279             :   }
    3280   110342109 :   static bool equal (const value_type &v, const compare_type p)
    3281             :   {
    3282   110342109 :     return v.src == p.src && v.offset == p.offset;
    3283             :   }
    3284             : 
    3285     7124416 :   static void mark_empty (value_type &v)
    3286             :   {
    3287     7124416 :     v.src = nullptr;
    3288             :   }
    3289   319967047 :   static bool is_empty (value_type &v)
    3290             :   {
    3291   319967047 :     return !v.src;
    3292             :   }
    3293             : 
    3294             :   static bool is_deleted (value_type &) { return false; }
    3295             :   static void mark_deleted (value_type &) { gcc_unreachable (); }
    3296             :   
    3297             :   static void remove (value_type &) {}
    3298             : };
    3299             : /* Table keyed by ord_loc_info, used for noting.  */
    3300             : static  hash_table<ord_loc_traits> *ord_loc_table;
    3301             : /* Sorted vector, used for writing.  */
    3302             : static vec<ord_loc_info> *ord_loc_remap;
    3303             : 
    3304             : /* Information about macro locations we stream out.  */
    3305             : struct macro_loc_info
    3306             : {
    3307             :   const line_map_macro *src;    // original expansion
    3308             :   unsigned remap;         // serialization
    3309             : 
    3310    10799961 :   static int compare (const void *a_, const void *b_)
    3311             :   {
    3312    10799961 :     auto *a = static_cast<const macro_loc_info *> (a_);
    3313    10799961 :     auto *b = static_cast<const macro_loc_info *> (b_);
    3314             : 
    3315    10799961 :     gcc_checking_assert (MAP_START_LOCATION (a->src)
    3316             :                          != MAP_START_LOCATION (b->src));
    3317    10799961 :     if (MAP_START_LOCATION (a->src) < MAP_START_LOCATION (b->src))
    3318             :       return -1;
    3319             :     else
    3320     5207126 :       return +1;
    3321             :   }
    3322             : };
    3323             : struct macro_loc_traits
    3324             : {
    3325             :   typedef macro_loc_info value_type;
    3326             :   typedef const line_map_macro *compare_type;
    3327             : 
    3328             :   static const bool empty_zero_p = false;
    3329             : 
    3330    10156736 :   static hashval_t hash (compare_type p)
    3331             :   {
    3332    10156736 :     return pointer_hash<const line_map_macro>::hash (p);
    3333             :   }
    3334     8602628 :   static hashval_t hash (const value_type &v)
    3335             :   {
    3336     8602628 :     return hash (v.src);
    3337             :   }
    3338             :   static bool equal (const value_type &v, const compare_type p)
    3339             :   {
    3340             :     return v.src == p;
    3341             :   }
    3342             : 
    3343      594022 :   static void mark_empty (value_type &v)
    3344             :   {
    3345      594022 :     v.src = nullptr;
    3346             :   }
    3347    31508753 :   static bool is_empty (value_type &v)
    3348             :   {
    3349    31508753 :     return !v.src;
    3350             :   }
    3351             : 
    3352             :   static bool is_deleted (value_type &) { return false; }
    3353             :   static void mark_deleted (value_type &) { gcc_unreachable (); }
    3354             :   
    3355             :   static void remove (value_type &) {}
    3356             : };
    3357             : /* Table keyed by line_map_macro, used for noting.  */
    3358             : static  hash_table<macro_loc_traits> *macro_loc_table;
    3359             : /* Sorted vector, used for writing.  */
    3360             : static vec<macro_loc_info> *macro_loc_remap;
    3361             : 
    3362             : /* Indirection to allow bsearching imports by ordinary location.  */
    3363             : static vec<module_state *> *ool;
    3364             : 
    3365             : /********************************************************************/
    3366             : /* Data needed by a module during the process of loading.  */
    3367             : struct GTY(()) slurping {
    3368             : 
    3369             :   /* Remap import's module numbering to our numbering.  Values are
    3370             :      shifted by 1.  Bit0 encodes if the import is direct.  */
    3371             :   vec<unsigned, va_heap, vl_embed> *
    3372             :     GTY((skip)) remap;                  /* Module owner remapping.  */
    3373             : 
    3374             :   elf_in *GTY((skip)) from;             /* The elf loader.  */
    3375             : 
    3376             :   /* This map is only for header imports themselves -- the global
    3377             :      headers bitmap hold it for the current TU.  */
    3378             :   bitmap headers;       /* Transitive set of direct imports, including
    3379             :                            self.  Used for macro visibility and
    3380             :                            priority.  */
    3381             : 
    3382             :   /* These objects point into the mmapped area, unless we're not doing
    3383             :      that, or we got frozen or closed.  In those cases they point to
    3384             :      buffers we own.  */
    3385             :   bytes_in macro_defs;  /* Macro definitions.  */
    3386             :   bytes_in macro_tbl;   /* Macro table.  */
    3387             : 
    3388             :   /* Location remapping.  first->ordinary, second->macro.  */
    3389             :   range_t GTY((skip)) loc_deltas;
    3390             : 
    3391             :   unsigned current;     /* Section currently being loaded.  */
    3392             :   unsigned remaining;   /* Number of lazy sections yet to read.  */
    3393             :   unsigned lru;         /* An LRU counter.  */
    3394             : 
    3395             :  public:
    3396             :   slurping (elf_in *);
    3397             :   ~slurping ();
    3398             : 
    3399             :  public:
    3400             :   /* Close the ELF file, if it's open.  */
    3401        3335 :   void close ()
    3402             :   {
    3403        3335 :     if (from)
    3404             :       {
    3405        1813 :         from->end ();
    3406        3626 :         delete from;
    3407        1813 :         from = NULL;
    3408             :       }
    3409        3335 :   }
    3410             : 
    3411             :  public:
    3412             :   void release_macros ();
    3413             : 
    3414             :  public:
    3415        1856 :   void alloc_remap (unsigned size)
    3416             :   {
    3417        1856 :     gcc_assert (!remap);
    3418        1856 :     vec_safe_reserve (remap, size);
    3419        3962 :     for (unsigned ix = size; ix--;)
    3420        2106 :       remap->quick_push (0);
    3421        1856 :   }
    3422     1570590 :   unsigned remap_module (unsigned owner)
    3423             :   {
    3424     1570590 :     if (owner < remap->length ())
    3425     1570590 :       return (*remap)[owner] >> 1;
    3426             :     return 0;
    3427             :   }
    3428             : 
    3429             :  public:
    3430             :   /* GC allocation.  But we must explicitly delete it.   */
    3431        1874 :   static void *operator new (size_t x)
    3432             :   {
    3433        3748 :     return ggc_alloc_atomic (x);
    3434             :   }
    3435        1813 :   static void operator delete (void *p)
    3436             :   {
    3437        1813 :     ggc_free (p);
    3438        1813 :   }
    3439             : };
    3440             : 
    3441        1874 : slurping::slurping (elf_in *from)
    3442        1874 :   : remap (NULL), from (from),
    3443        1874 :     headers (BITMAP_GGC_ALLOC ()), macro_defs (), macro_tbl (),
    3444        1874 :     loc_deltas (0, 0),
    3445        1874 :     current (~0u), remaining (0), lru (0)
    3446             : {
    3447        1874 : }
    3448             : 
    3449        1813 : slurping::~slurping ()
    3450             : {
    3451        1813 :   vec_free (remap);
    3452        1813 :   remap = NULL;
    3453        1813 :   release_macros ();
    3454        1813 :   close ();
    3455        1813 : }
    3456             : 
    3457        3335 : void slurping::release_macros ()
    3458             : {
    3459        3335 :   if (macro_defs.size)
    3460         644 :     elf_in::release (from, macro_defs);
    3461        3335 :   if (macro_tbl.size)
    3462           0 :     elf_in::release (from, macro_tbl);
    3463        3335 : }
    3464             : 
    3465             : /* Flags for extensions that end up being streamed.  */
    3466             : 
    3467             : enum streamed_extensions {
    3468             :   SE_OPENMP = 1 << 0,
    3469             :   SE_BITS = 1
    3470             : };
    3471             : 
    3472             : /* Counter indices.  */
    3473             : enum module_state_counts
    3474             : {
    3475             :   MSC_sec_lwm,
    3476             :   MSC_sec_hwm,
    3477             :   MSC_pendings,
    3478             :   MSC_entities,
    3479             :   MSC_namespaces,
    3480             :   MSC_bindings,
    3481             :   MSC_macros,
    3482             :   MSC_inits,
    3483             :   MSC_HWM
    3484             : };
    3485             : 
    3486             : /********************************************************************/
    3487             : struct module_state_config;
    3488             : 
    3489             : /* Increasing levels of loadedness.  */
    3490             : enum module_loadedness {
    3491             :   ML_NONE,              /* Not loaded.  */
    3492             :   ML_CONFIG,            /* Config loaed.  */
    3493             :   ML_PREPROCESSOR,      /* Preprocessor loaded.  */
    3494             :   ML_LANGUAGE,          /* Language loaded.  */
    3495             : };
    3496             : 
    3497             : /* Increasing levels of directness (toplevel) of import.  */
    3498             : enum module_directness {
    3499             :   MD_NONE,              /* Not direct.  */
    3500             :   MD_PARTITION_DIRECT,  /* Direct import of a partition.  */
    3501             :   MD_DIRECT,            /* Direct import.  */
    3502             :   MD_PURVIEW_DIRECT,    /* direct import in purview.  */
    3503             : };
    3504             : 
    3505             : /* State of a particular module. */
    3506             : 
    3507             : class GTY((chain_next ("%h.parent"), for_user)) module_state {
    3508             :  public:
    3509             :   /* We always import & export ourselves.  */
    3510             :   bitmap imports;       /* Transitive modules we're importing.  */
    3511             :   bitmap exports;       /* Subset of that, that we're exporting.  */
    3512             : 
    3513             :   module_state *parent;
    3514             :   tree name;            /* Name of the module.  */
    3515             : 
    3516             :   slurping *slurp;      /* Data for loading.  */
    3517             : 
    3518             :   const char *flatname; /* Flatname of module.  */
    3519             :   char *filename;       /* CMI Filename */
    3520             : 
    3521             :   /* Indices into the entity_ary.  */
    3522             :   unsigned entity_lwm;
    3523             :   unsigned entity_num;
    3524             : 
    3525             :   /* Location ranges for this module.  adhoc-locs are decomposed, so
    3526             :      don't have a range.  */
    3527             :   loc_range_t GTY((skip)) ordinary_locs;
    3528             :   loc_range_t GTY((skip)) macro_locs; // [lwm,num)
    3529             : 
    3530             :   /* LOC is first set too the importing location.  When initially
    3531             :      loaded it refers to a module loc whose parent is the importing
    3532             :      location.  */
    3533             :   location_t loc;       /* Location referring to module itself.  */
    3534             :   unsigned crc;         /* CRC we saw reading it in. */
    3535             : 
    3536             :   unsigned mod;         /* Module owner number.  */
    3537             :   unsigned remap;       /* Remapping during writing.  */
    3538             : 
    3539             :   unsigned short subst; /* Mangle subst if !0.  */
    3540             : 
    3541             :   /* How loaded this module is.  */
    3542             :   enum module_loadedness loadedness : 2;
    3543             : 
    3544             :   bool module_p : 1;    /* /The/ module of this TU.  */
    3545             :   bool header_p : 1;    /* Is a header unit.  */
    3546             :   bool interface_p : 1; /* An interface.  */
    3547             :   bool partition_p : 1; /* A partition.  */
    3548             : 
    3549             :   /* How directly this module is imported.  */
    3550             :   enum module_directness directness : 2;
    3551             : 
    3552             :   bool exported_p : 1;  /* directness != MD_NONE && exported.  */
    3553             :   bool cmi_noted_p : 1; /* We've told the user about the CMI, don't
    3554             :                            do it again  */
    3555             :   bool active_init_p : 1; /* This module's global initializer needs
    3556             :                            calling.  */
    3557             :   bool inform_cmi_p : 1; /* Inform of a read/write.  */
    3558             :   bool visited_p : 1;    /* A walk-once flag. */
    3559             :   /* Record extensions emitted or permitted.  */
    3560             :   unsigned extensions : SE_BITS;
    3561             :   /* 14 bits used, 2 bits remain  */
    3562             : 
    3563             :  public:
    3564             :   module_state (tree name, module_state *, bool);
    3565             :   ~module_state ();
    3566             : 
    3567             :  public:
    3568        1874 :   void release ()
    3569             :   {
    3570        1874 :     imports = exports = NULL;
    3571        1874 :     slurped ();
    3572        1813 :   }
    3573        3396 :   void slurped ()
    3574             :   {
    3575        3396 :     delete slurp;
    3576        3396 :     slurp = NULL;
    3577        3396 :   }
    3578     1925046 :   elf_in *from () const
    3579             :   {
    3580     1925046 :     return slurp->from;
    3581             :   }
    3582             : 
    3583             :  public:
    3584             :   /* Kind of this module.  */
    3585        8735 :   bool is_module () const
    3586             :   {
    3587        8735 :     return module_p;
    3588             :   }
    3589     1779589 :   bool is_header () const
    3590             :   {
    3591     1779589 :     return header_p;
    3592             :   }
    3593         279 :   bool is_interface () const
    3594             :   {
    3595         279 :     return interface_p;
    3596             :   }
    3597       41209 :   bool is_partition () const
    3598             :   {
    3599       41209 :     return partition_p;
    3600             :   }
    3601             : 
    3602             :   /* How this module is used in the current TU.  */
    3603             :   bool is_exported () const
    3604             :   {
    3605             :     return exported_p;
    3606             :   }
    3607       12762 :   bool is_direct () const
    3608             :   {
    3609       12762 :     return directness >= MD_DIRECT;
    3610             :   }
    3611         195 :   bool is_purview_direct () const
    3612             :   {
    3613         195 :     return directness == MD_PURVIEW_DIRECT;
    3614             :   }
    3615         287 :   bool is_partition_direct () const
    3616             :   {
    3617         287 :     return directness == MD_PARTITION_DIRECT;
    3618             :   }
    3619             : 
    3620             :  public:
    3621             :   /* Is this a real module?  */
    3622       11380 :   bool has_location () const
    3623             :   {
    3624       11380 :     return loc != UNKNOWN_LOCATION;
    3625             :   }
    3626             : 
    3627             :  public:
    3628             :   bool check_not_purview (location_t loc);
    3629             : 
    3630             :  public:
    3631             :   void mangle (bool include_partition);
    3632             : 
    3633             :  public:
    3634             :   void set_import (module_state const *, bool is_export);
    3635             :   void announce (const char *) const;
    3636             : 
    3637             :  public:
    3638             :   /* Read and write module.  */
    3639             :   void write_begin (elf_out *to, cpp_reader *,
    3640             :                     module_state_config &, unsigned &crc);
    3641             :   void write_end (elf_out *to, cpp_reader *,
    3642             :                   module_state_config &, unsigned &crc);
    3643             :   bool read_initial (cpp_reader *);
    3644             :   bool read_preprocessor (bool);
    3645             :   bool read_language (bool);
    3646             : 
    3647             :  public:
    3648             :   /* Read a section.  */
    3649             :   bool load_section (unsigned snum, binding_slot *mslot);
    3650             :   /* Lazily read a section.  */
    3651             :   bool lazy_load (unsigned index, binding_slot *mslot);
    3652             : 
    3653             :  public:
    3654             :   /* Juggle a limited number of file numbers.  */
    3655             :   static void freeze_an_elf ();
    3656             :   bool maybe_defrost ();
    3657             : 
    3658             :  public:
    3659             :   void maybe_completed_reading ();
    3660             :   bool check_read (bool outermost, bool ok);
    3661             : 
    3662             :  private:
    3663             :   /* The README, for human consumption.  */
    3664             :   void write_readme (elf_out *to, cpp_reader *, const char *dialect);
    3665             :   void write_env (elf_out *to);
    3666             : 
    3667             :  private:
    3668             :   /* Import tables. */
    3669             :   void write_imports (bytes_out &cfg, bool direct);
    3670             :   unsigned read_imports (bytes_in &cfg, cpp_reader *, line_maps *maps);
    3671             : 
    3672             :  private:
    3673             :   void write_imports (elf_out *to, unsigned *crc_ptr);
    3674             :   bool read_imports (cpp_reader *, line_maps *);
    3675             : 
    3676             :  private:
    3677             :   void write_partitions (elf_out *to, unsigned, unsigned *crc_ptr);
    3678             :   bool read_partitions (unsigned);
    3679             : 
    3680             :  private:
    3681             :   void write_config (elf_out *to, struct module_state_config &, unsigned crc);
    3682             :   bool read_config (struct module_state_config &);
    3683             :   static void write_counts (elf_out *to, unsigned [MSC_HWM], unsigned *crc_ptr);
    3684             :   bool read_counts (unsigned *);
    3685             : 
    3686             :  public:
    3687             :   void note_cmi_name ();
    3688             : 
    3689             :  private:
    3690             :   static unsigned write_bindings (elf_out *to, vec<depset *> depsets,
    3691             :                                   unsigned *crc_ptr);
    3692             :   bool read_bindings (unsigned count, unsigned lwm, unsigned hwm);
    3693             : 
    3694             :   static void write_namespace (bytes_out &sec, depset *ns_dep);
    3695             :   tree read_namespace (bytes_in &sec);
    3696             : 
    3697             :   void write_namespaces (elf_out *to, vec<depset *> spaces,
    3698             :                          unsigned, unsigned *crc_ptr);
    3699             :   bool read_namespaces (unsigned);
    3700             : 
    3701             :   void intercluster_seed (trees_out &sec, unsigned index, depset *dep);
    3702             :   unsigned write_cluster (elf_out *to, depset *depsets[], unsigned size,
    3703             :                           depset::hash &, unsigned *counts, unsigned *crc_ptr);
    3704             :   bool read_cluster (unsigned snum);
    3705             : 
    3706             :  private:
    3707             :   unsigned write_inits (elf_out *to, depset::hash &, unsigned *crc_ptr);
    3708             :   bool read_inits (unsigned count);
    3709             : 
    3710             :  private:
    3711             :   unsigned write_pendings (elf_out *to, vec<depset *> depsets,
    3712             :                            depset::hash &, unsigned *crc_ptr);
    3713             :   bool read_pendings (unsigned count);
    3714             : 
    3715             :  private:
    3716             :   void write_entities (elf_out *to, vec<depset *> depsets,
    3717             :                        unsigned count, unsigned *crc_ptr);
    3718             :   bool read_entities (unsigned count, unsigned lwm, unsigned hwm);
    3719             : 
    3720             :  private:
    3721             :   void write_init_maps ();
    3722             :   range_t write_prepare_maps (module_state_config *, bool);
    3723             :   bool read_prepare_maps (const module_state_config *);
    3724             : 
    3725             :   void write_ordinary_maps (elf_out *to, range_t &,
    3726             :                             bool, unsigned *crc_ptr);
    3727             :   bool read_ordinary_maps (unsigned, unsigned);
    3728             :   void write_macro_maps (elf_out *to, range_t &, unsigned *crc_ptr);
    3729             :   bool read_macro_maps (unsigned);
    3730             : 
    3731             :  private:
    3732             :   void write_define (bytes_out &, const cpp_macro *);
    3733             :   cpp_macro *read_define (bytes_in &, cpp_reader *) const;
    3734             :   vec<cpp_hashnode *> *prepare_macros (cpp_reader *);
    3735             :   unsigned write_macros (elf_out *to, vec<cpp_hashnode *> *, unsigned *crc_ptr);
    3736             :   bool read_macros ();
    3737             :   void install_macros ();
    3738             : 
    3739             :  public:
    3740             :   void import_macros ();
    3741             : 
    3742             :  public:
    3743             :   static void undef_macro (cpp_reader *, location_t, cpp_hashnode *);
    3744             :   static cpp_macro *deferred_macro (cpp_reader *, location_t, cpp_hashnode *);
    3745             : 
    3746             :  public:
    3747             :   static bool note_location (location_t);
    3748             :   static void write_location (bytes_out &, location_t);
    3749             :   location_t read_location (bytes_in &) const;
    3750             : 
    3751             :  public:
    3752             :   void set_flatname ();
    3753       44487 :   const char *get_flatname () const
    3754             :   {
    3755       44487 :     return flatname;
    3756             :   }
    3757             :   location_t imported_from () const;
    3758             : 
    3759             :  public:
    3760             :   void set_filename (const Cody::Packet &);
    3761             :   bool do_import (cpp_reader *, bool outermost);
    3762             : };
    3763             : 
    3764             : /* Hash module state by name.  This cannot be a member of
    3765             :    module_state, because of GTY restrictions.  We never delete from
    3766             :    the hash table, but ggc_ptr_hash doesn't support that
    3767             :    simplification.  */
    3768             : 
    3769             : struct module_state_hash : ggc_ptr_hash<module_state> {
    3770             :   typedef std::pair<tree,uintptr_t> compare_type; /* {name,parent} */
    3771             : 
    3772             :   static inline hashval_t hash (const value_type m);
    3773             :   static inline hashval_t hash (const compare_type &n);
    3774             :   static inline bool equal (const value_type existing,
    3775             :                             const compare_type &candidate);
    3776             : };
    3777             : 
    3778        6801 : module_state::module_state (tree name, module_state *parent, bool partition)
    3779        6801 :   : imports (BITMAP_GGC_ALLOC ()), exports (BITMAP_GGC_ALLOC ()),
    3780        6801 :     parent (parent), name (name), slurp (NULL),
    3781        6801 :     flatname (NULL), filename (NULL),
    3782        6801 :     entity_lwm (~0u >> 1), entity_num (0),
    3783        6801 :     ordinary_locs (0, 0), macro_locs (0, 0),
    3784        6801 :     loc (UNKNOWN_LOCATION),
    3785        6801 :     crc (0), mod (MODULE_UNKNOWN), remap (0), subst (0)
    3786             : {
    3787        6801 :   loadedness = ML_NONE;
    3788             : 
    3789        6801 :   module_p = header_p = interface_p = partition_p = false;
    3790             : 
    3791        6801 :   directness = MD_NONE;
    3792        6801 :   exported_p = false;
    3793             : 
    3794        6801 :   cmi_noted_p = false;
    3795        6801 :   active_init_p = false;
    3796             : 
    3797        6801 :   partition_p = partition;
    3798             : 
    3799        6801 :   inform_cmi_p = false;
    3800        6801 :   visited_p = false;
    3801             : 
    3802        6801 :   extensions = 0;
    3803        6801 :   if (name && TREE_CODE (name) == STRING_CST)
    3804             :     {
    3805        1383 :       header_p = true;
    3806             : 
    3807        1383 :       const char *string = TREE_STRING_POINTER (name);
    3808        1383 :       gcc_checking_assert (string[0] == '.'
    3809             :                            ? IS_DIR_SEPARATOR (string[1])
    3810             :                            : IS_ABSOLUTE_PATH (string));
    3811             :     }
    3812             : 
    3813        6801 :   gcc_checking_assert (!(parent && header_p));
    3814        6801 : }
    3815             : 
    3816          61 : module_state::~module_state ()
    3817             : {
    3818          61 :   release ();
    3819          61 : }
    3820             : 
    3821             : /* Hash module state.  */
    3822             : static hashval_t
    3823        9355 : module_name_hash (const_tree name)
    3824             : {
    3825        9355 :   if (TREE_CODE (name) == STRING_CST)
    3826        2565 :     return htab_hash_string (TREE_STRING_POINTER (name));
    3827             :   else
    3828        6790 :     return IDENTIFIER_HASH_VALUE (name);
    3829             : }
    3830             : 
    3831             : hashval_t
    3832        2138 : module_state_hash::hash (const value_type m)
    3833             : {
    3834        2138 :   hashval_t ph = pointer_hash<void>::hash
    3835        2138 :     (reinterpret_cast<void *> (reinterpret_cast<uintptr_t> (m->parent)
    3836        2138 :                                | m->is_partition ()));
    3837        2138 :   hashval_t nh = module_name_hash (m->name);
    3838        2138 :   return iterative_hash_hashval_t (ph, nh);
    3839             : }
    3840             : 
    3841             : /* Hash a name.  */
    3842             : hashval_t
    3843        7217 : module_state_hash::hash (const compare_type &c)
    3844             : {
    3845        7217 :   hashval_t ph = pointer_hash<void>::hash (reinterpret_cast<void *> (c.second));
    3846        7217 :   hashval_t nh = module_name_hash (c.first);
    3847             : 
    3848        7217 :   return iterative_hash_hashval_t (ph, nh);
    3849             : }
    3850             : 
    3851             : bool
    3852        4736 : module_state_hash::equal (const value_type existing,
    3853             :                           const compare_type &candidate)
    3854             : {
    3855        4736 :   uintptr_t ep = (reinterpret_cast<uintptr_t> (existing->parent)
    3856        4736 :                   | existing->is_partition ());
    3857        4736 :   if (ep != candidate.second)
    3858             :     return false;
    3859             : 
    3860             :   /* Identifier comparison is by pointer.  If the string_csts happen
    3861             :      to be the same object, then they're equal too.  */
    3862        4040 :   if (existing->name == candidate.first)
    3863             :     return true;
    3864             : 
    3865             :   /* If neither are string csts, they can't be equal.  */
    3866         835 :   if (TREE_CODE (candidate.first) != STRING_CST
    3867         350 :       || TREE_CODE (existing->name) != STRING_CST)
    3868             :     return false;
    3869             : 
    3870             :   /* String equality.  */
    3871         289 :   if (TREE_STRING_LENGTH (existing->name)
    3872         289 :       == TREE_STRING_LENGTH (candidate.first)
    3873         289 :       && !memcmp (TREE_STRING_POINTER (existing->name),
    3874         289 :                   TREE_STRING_POINTER (candidate.first),
    3875         289 :                   TREE_STRING_LENGTH (existing->name)))
    3876          93 :     return true;
    3877             : 
    3878             :   return false;
    3879             : }
    3880             : 
    3881             : /********************************************************************/
    3882             : /* Global state */
    3883             : 
    3884             : /* Mapper name.  */
    3885             : static const char *module_mapper_name;
    3886             : 
    3887             : /* Deferred import queue (FIFO).  */
    3888             : static vec<module_state *, va_heap, vl_embed> *pending_imports;
    3889             : 
    3890             : /* CMI repository path and workspace.  */
    3891             : static char *cmi_repo;
    3892             : static size_t cmi_repo_length;
    3893             : static char *cmi_path;
    3894             : static size_t cmi_path_alloc;
    3895             : 
    3896             : /* Count of available and loaded clusters.  */
    3897             : static unsigned available_clusters;
    3898             : static unsigned loaded_clusters;
    3899             : 
    3900             : /* What the current TU is.  */
    3901             : unsigned module_kind;
    3902             : 
    3903             : /* Global trees.  */
    3904             : static const std::pair<tree *, unsigned> global_tree_arys[] =
    3905             :   {
    3906             :     std::pair<tree *, unsigned> (sizetype_tab, stk_type_kind_last),
    3907             :     std::pair<tree *, unsigned> (integer_types, itk_none),
    3908             :     std::pair<tree *, unsigned> (global_trees, TI_MODULE_HWM),
    3909             :     std::pair<tree *, unsigned> (c_global_trees, CTI_MODULE_HWM),
    3910             :     std::pair<tree *, unsigned> (cp_global_trees, CPTI_MODULE_HWM),
    3911             :     std::pair<tree *, unsigned> (NULL, 0)
    3912             :   };
    3913             : static GTY(()) vec<tree, va_gc> *fixed_trees;
    3914             : static unsigned global_crc;
    3915             : 
    3916             : /* Lazy loading can open many files concurrently, there are
    3917             :    per-process limits on that.  We pay attention to the process limit,
    3918             :    and attempt to increase it when we run out.  Otherwise we use an
    3919             :    LRU scheme to figure out who to flush.  Note that if the import
    3920             :    graph /depth/ exceeds lazy_limit, we'll exceed the limit.  */
    3921             : static unsigned lazy_lru;  /* LRU counter.  */
    3922             : static unsigned lazy_open; /* Number of open modules */
    3923             : static unsigned lazy_limit; /* Current limit of open modules.  */
    3924             : static unsigned lazy_hard_limit; /* Hard limit on open modules.  */
    3925             : /* Account for source, assembler and dump files & directory searches.
    3926             :    We don't keep the source file's open, so we don't have to account
    3927             :    for #include depth.  I think dump files are opened and closed per
    3928             :    pass, but ICBW.  */
    3929             : #define LAZY_HEADROOM 15 /* File descriptor headroom.  */
    3930             : 
    3931             : /* Vector of module state.  Indexed by OWNER.  Has at least 2 slots.  */
    3932             : static GTY(()) vec<module_state *, va_gc> *modules;
    3933             : 
    3934             : /* Hash of module state, findable by {name, parent}. */
    3935             : static GTY(()) hash_table<module_state_hash> *modules_hash;
    3936             : 
    3937             : /* Map of imported entities.  We map DECL_UID to index of entity
    3938             :    vector.  */
    3939             : typedef hash_map<unsigned/*UID*/, unsigned/*index*/,
    3940             :                  simple_hashmap_traits<int_hash<unsigned,0>, unsigned>
    3941             :                  > entity_map_t;
    3942             : static entity_map_t *entity_map;
    3943             : /* Doesn't need GTYing, because any tree referenced here is also
    3944             :    findable by, symbol table, specialization table, return type of
    3945             :    reachable function.  */
    3946             : static vec<binding_slot, va_heap, vl_embed> *entity_ary;
    3947             : 
    3948             : /* Members entities of imported classes that are defined in this TU.
    3949             :    These are where the entity's context is not from the current TU.
    3950             :    We need to emit the definition (but not the enclosing class).
    3951             : 
    3952             :    We could find these by walking ALL the imported classes that we
    3953             :    could provide a member definition.  But that's expensive,
    3954             :    especially when you consider lazy implicit member declarations,
    3955             :    which could be ANY imported class.  */
    3956             : static GTY(()) vec<tree, va_gc> *class_members;
    3957             : 
    3958             : /* The same problem exists for class template partial
    3959             :    specializations.  Now that we have constraints, the invariant of
    3960             :    expecting them in the instantiation table no longer holds.  One of
    3961             :    the constrained partial specializations will be there, but the
    3962             :    others not so much.  It's not even an unconstrained partial
    3963             :    spacialization in the table :(  so any partial template declaration
    3964             :    is added to this list too.  */
    3965             : static GTY(()) vec<tree, va_gc> *partial_specializations;
    3966             : 
    3967             : /********************************************************************/
    3968             : 
    3969             : /* Our module mapper (created lazily).  */
    3970             : module_client *mapper;
    3971             : 
    3972             : static module_client *make_mapper (location_t loc);
    3973       26872 : inline module_client *get_mapper (location_t loc)
    3974             : {
    3975       26872 :   auto *res = mapper;
    3976         234 :   if (!res)
    3977        2882 :     res = make_mapper (loc);
    3978       26872 :   return res;
    3979             : }
    3980             : 
    3981             : /********************************************************************/
    3982             : static tree
    3983      318962 : get_clone_target (tree decl)
    3984             : {
    3985      318962 :   tree target;
    3986             : 
    3987      318962 :   if (TREE_CODE (decl) == TEMPLATE_DECL)
    3988             :     {
    3989       31948 :       tree res_orig = DECL_CLONED_FUNCTION (DECL_TEMPLATE_RESULT (decl));
    3990             :       
    3991       31948 :       target = DECL_TI_TEMPLATE (res_orig);
    3992             :     }
    3993             :   else
    3994      287014 :     target = DECL_CLONED_FUNCTION (decl);
    3995             : 
    3996      318962 :   gcc_checking_assert (DECL_MAYBE_IN_CHARGE_CDTOR_P (target));
    3997             : 
    3998      318962 :   return target;
    3999             : }
    4000             : 
    4001             : /* Like FOR_EACH_CLONE, but will walk cloned templates.  */
    4002             : #define FOR_EVERY_CLONE(CLONE, FN)                      \
    4003             :   if (!DECL_MAYBE_IN_CHARGE_CDTOR_P (FN));              \
    4004             :   else                                                  \
    4005             :     for (CLONE = DECL_CHAIN (FN);                       \
    4006             :          CLONE && DECL_CLONED_FUNCTION_P (CLONE);       \
    4007             :          CLONE = DECL_CHAIN (CLONE))
    4008             : 
    4009             : /* It'd be nice if USE_TEMPLATE was a field of template_info
    4010             :    (a) it'd solve the enum case dealt with below,
    4011             :    (b) both class templates and decl templates would store this in the
    4012             :    same place
    4013             :    (c) this function wouldn't need the by-ref arg, which is annoying.  */
    4014             : 
    4015             : static tree
    4016   127434152 : node_template_info (tree decl, int &use)
    4017             : {
    4018   127434152 :   tree ti = NULL_TREE;
    4019   127434152 :   int use_tpl = -1;
    4020   127434152 :   if (DECL_IMPLICIT_TYPEDEF_P (decl))
    4021             :     {
    4022    15225154 :       tree type = TREE_TYPE (decl);
    4023             : 
    4024    15225154 :       ti = TYPE_TEMPLATE_INFO (type);
    4025    15225154 :       if (ti)
    4026             :         {
    4027     4043656 :           if (TYPE_LANG_SPECIFIC (type))
    4028     4037482 :             use_tpl = CLASSTYPE_USE_TEMPLATE (type);
    4029             :           else
    4030             :             {
    4031             :               /* An enum, where we don't explicitly encode use_tpl.
    4032             :                  If the containing context (a type or a function), is
    4033             :                  an ({im,ex}plicit) instantiation, then this is too.
    4034             :                  If it's a partial or explicit specialization, then
    4035             :                  this is not!.  */
    4036        6174 :               tree ctx = CP_DECL_CONTEXT (decl);
    4037        6174 :               if (TYPE_P (ctx))
    4038        6128 :                 ctx = TYPE_NAME (ctx);
    4039        6174 :               node_template_info (ctx, use);
    4040        6174 :               use_tpl = use != 2 ? use : 0;
    4041             :             }
    4042             :         }
    4043             :     }
    4044   112208998 :   else if (DECL_LANG_SPECIFIC (decl)
    4045   112208998 :            && (VAR_P (decl)
    4046    63942218 :                || TREE_CODE (decl) == TYPE_DECL
    4047             :                || TREE_CODE (decl) == FUNCTION_DECL
    4048             :                || TREE_CODE (decl) == FIELD_DECL
    4049             :                || TREE_CODE (decl) == CONCEPT_DECL
    4050             :                || TREE_CODE (decl) == TEMPLATE_DECL))
    4051             :     {
    4052    62273181 :       use_tpl = DECL_USE_TEMPLATE (decl);
    4053    62273181 :       ti = DECL_TEMPLATE_INFO (decl);
    4054             :     }
    4055             : 
    4056   127434152 :   use = use_tpl;
    4057   127434152 :   return ti;
    4058             : }
    4059             : 
    4060             : /* Find the index in entity_ary for an imported DECL.  It should
    4061             :    always be there, but bugs can cause it to be missing, and that can
    4062             :    crash the crash reporting -- let's not do that!  When streaming
    4063             :    out we place entities from this module there too -- with negated
    4064             :    indices.  */
    4065             : 
    4066             : static unsigned
    4067     1290598 : import_entity_index (tree decl, bool null_ok = false)
    4068             : {
    4069     1290598 :   if (unsigned *slot = entity_map->get (DECL_UID (decl)))
    4070     1290583 :     return *slot;
    4071             : 
    4072          15 :   gcc_checking_assert (null_ok);
    4073             :   return ~(~0u >> 1);
    4074             : }
    4075             : 
    4076             : /* Find the module for an imported entity at INDEX in the entity ary.
    4077             :    There must be one.  */
    4078             : 
    4079             : static module_state *
    4080       34887 : import_entity_module (unsigned index)
    4081             : {
    4082       34887 :   if (index > ~(~0u >> 1))
    4083             :     /* This is an index for an exported entity.  */
    4084          15 :     return (*modules)[0];
    4085             : 
    4086             :   /* Do not include the current TU (not an off-by-one error).  */
    4087       34872 :   unsigned pos = 1;
    4088       34872 :   unsigned len = modules->length () - pos;
    4089       79985 :   while (len)
    4090             :     {
    4091       45113 :       unsigned half = len / 2;
    4092       45113 :       module_state *probe = (*modules)[pos + half];
    4093       45113 :       if (index < probe->entity_lwm)
    4094             :         len = half;
    4095       34969 :       else if (index < probe->entity_lwm + probe->entity_num)
    4096       34872 :         return probe;
    4097             :       else
    4098             :         {
    4099          97 :           pos += half + 1;
    4100          97 :           len = len - (half + 1);
    4101             :         }
    4102             :     }
    4103           0 :   gcc_unreachable ();
    4104             : }
    4105             : 
    4106             : 
    4107             : /********************************************************************/
    4108             : /* A dumping machinery.  */
    4109             : 
    4110             : class dumper {
    4111             : public:
    4112             :   enum {
    4113             :     LOCATION = TDF_LINENO,  /* -lineno:Source location streaming.  */
    4114             :     DEPEND = TDF_GRAPH, /* -graph:Dependency graph construction.  */
    4115             :     CLUSTER = TDF_BLOCKS,   /* -blocks:Clusters.  */
    4116             :     TREE = TDF_UID,     /* -uid:Tree streaming.  */
    4117             :     MERGE = TDF_ALIAS,  /* -alias:Mergeable Entities.  */
    4118             :     ELF = TDF_ASMNAME,  /* -asmname:Elf data.  */
    4119             :     MACRO = TDF_VOPS    /* -vops:Macros.  */
    4120             :   };
    4121             : 
    4122             : private:
    4123             :   struct impl {
    4124             :     typedef vec<module_state *, va_heap, vl_embed> stack_t;
    4125             : 
    4126             :     FILE *stream;       /* Dump stream.  */
    4127             :     unsigned indent;    /* Local indentation.  */
    4128             :     bool bol;           /* Beginning of line.  */
    4129             :     stack_t stack;      /* Trailing array of module_state.  */
    4130             : 
    4131             :     bool nested_name (tree);  /* Dump a name following DECL_CONTEXT.  */
    4132             :   };
    4133             : 
    4134             : public:
    4135             :   /* The dumper.  */
    4136             :   impl *dumps;
    4137             :   dump_flags_t flags;
    4138             : 
    4139             : public:
    4140             :   /* Push/pop module state dumping.  */
    4141             :   unsigned push (module_state *);
    4142             :   void pop (unsigned);
    4143             : 
    4144             : public:
    4145             :   /* Change local indentation.  */
    4146   374941737 :   void indent ()
    4147             :   {
    4148         282 :     if (dumps)
    4149     6128732 :       dumps->indent++;
    4150             :   }
    4151   374941737 :   void outdent ()
    4152             :   {
    4153   374941737 :     if (dumps)
    4154             :       {
    4155     6128732 :         gcc_checking_assert (dumps->indent);
    4156     6128732 :         dumps->indent--;
    4157             :       }
    4158   374941737 :   }
    4159             : 
    4160             : public:
    4161             :   /* Is dump enabled?.  */
    4162   245366107 :   bool operator () (int mask = 0)
    4163             :   {
    4164     6773302 :     if (!dumps || !dumps->stream)
    4165             :       return false;
    4166     2721335 :     if (mask && !(mask & flags))
    4167       25357 :       return false;
    4168             :     return true;
    4169             :   }
    4170             :   /* Dump some information.  */
    4171             :   bool operator () (const char *, ...);
    4172             : };
    4173             : 
    4174             : /* The dumper.  */
    4175             : static dumper dump = {0, dump_flags_t (0)};
    4176             : 
    4177             : /* Push to dumping M.  Return previous indentation level.  */
    4178             : 
    4179             : unsigned
    4180       65381 : dumper::push (module_state *m)
    4181             : {
    4182       65381 :   FILE *stream = NULL;
    4183       65381 :   if (!dumps || !dumps->stack.length ())
    4184             :     {
    4185       64334 :       stream = dump_begin (module_dump_id, &flags);
    4186       64334 :       if (!stream)
    4187             :         return 0;
    4188             :     }
    4189             : 
    4190        5192 :   if (!dumps || !dumps->stack.space (1))
    4191             :     {
    4192             :       /* Create or extend the dump implementor.  */
    4193        1005 :       unsigned current = dumps ? dumps->stack.length () : 0;
    4194         528 :       unsigned count = current ? current * 2 : EXPERIMENT (1, 20);
    4195        1005 :       size_t alloc = (offsetof (impl, stack)
    4196        1005 :                       + impl::stack_t::embedded_size (count));
    4197        1005 :       dumps = XRESIZEVAR (impl, dumps, alloc);
    4198        1005 :       dumps->stack.embedded_init (count, current);
    4199             :     }
    4200        5192 :   if (stream)
    4201        4145 :     dumps->stream = stream;
    4202             : 
    4203        5192 :   unsigned n = dumps->indent;
    4204        5192 :   dumps->indent = 0;
    4205        5192 :   dumps->bol = true;
    4206        5192 :   dumps->stack.quick_push (m);
    4207        5192 :   if (m)
    4208             :     {
    4209        1650 :       module_state *from = NULL;
    4210             : 
    4211        1650 :       if (dumps->stack.length () > 1)
    4212         525 :         from = dumps->stack[dumps->stack.length () - 2];
    4213             :       else
    4214        1125 :         dump ("");
    4215        3000 :       dump (from ? "Starting module %M (from %M)"
    4216             :             : "Starting module %M", m, from);
    4217             :     }
    4218             : 
    4219             :   return n;
    4220             : }
    4221             : 
    4222             : /* Pop from dumping.  Restore indentation to N.  */
    4223             : 
    4224       65353 : void dumper::pop (unsigned n)
    4225             : {
    4226       65353 :   if (!dumps)
    4227             :     return;
    4228             : 
    4229       10384 :   gcc_checking_assert (dump () && !dumps->indent);
    4230        5192 :   if (module_state *m = dumps->stack[dumps->stack.length () - 1])
    4231             :     {
    4232        1650 :       module_state *from = (dumps->stack.length () > 1
    4233        1650 :                             ? dumps->stack[dumps->stack.length () - 2] : NULL);
    4234        1875 :       dump (from ? "Finishing module %M (returning to %M)"
    4235             :             : "Finishing module %M", m, from);
    4236             :     }
    4237        5192 :   dumps->stack.pop ();
    4238        5192 :   dumps->indent = n;
    4239        5192 :   if (!dumps->stack.length ())
    4240             :     {
    4241        4145 :       dump_end (module_dump_id, dumps->stream);
    4242        4145 :       dumps->stream = NULL;
    4243             :     }
    4244             : }
    4245             : 
    4246             : /* Dump a nested name for arbitrary tree T.  Sometimes it won't have a
    4247             :    name.  */
    4248             : 
    4249             : bool
    4250     1613394 : dumper::impl::nested_name (tree t)
    4251             : {
    4252     1613394 :   tree ti = NULL_TREE;
    4253     1613394 :   int origin = -1;
    4254     1613394 :   tree name = NULL_TREE;
    4255             : 
    4256     1613394 :   if (t && TREE_CODE (t) == TREE_BINFO)
    4257         378 :     t = BINFO_TYPE (t);
    4258             : 
    4259     1613373 :   if (t && TYPE_P (t))
    4260      776262 :     t = TYPE_NAME (t);
    4261             : 
    4262     1613382 :   if (t && DECL_P (t))
    4263             :     {
    4264     1495741 :       if (t == global_namespace || DECL_TEMPLATE_PARM_P (t))
    4265             :         ;
    4266     1350802 :       else if (tree ctx = DECL_CONTEXT (t))
    4267     1071371 :         if (TREE_CODE (ctx) == TRANSLATION_UNIT_DECL
    4268     1071371 :             || nested_name (ctx))
    4269     1071371 :           fputs ("::", stream);
    4270             : 
    4271     1495741 :       int use_tpl;
    4272     1495741 :       ti = node_template_info (t, use_tpl);
    4273      409787 :       if (ti && TREE_CODE (TI_TEMPLATE (ti)) == TEMPLATE_DECL
    4274     1905474 :           && (DECL_TEMPLATE_RESULT (TI_TEMPLATE (ti)) == t))
    4275             :         t = TI_TEMPLATE (ti);
    4276     1495741 :       tree not_tmpl = t;
    4277     1495741 :       if (TREE_CODE (t) == TEMPLATE_DECL)
    4278             :         {
    4279      153991 :           fputs ("template ", stream);
    4280      153991 :           not_tmpl = DECL_TEMPLATE_RESULT (t);
    4281             :         }
    4282             : 
    4283      153991 :       if (not_tmpl
    4284     1495701 :           && DECL_P (not_tmpl)
    4285     1495701 :           && DECL_LANG_SPECIFIC (not_tmpl)
    4286      876654 :           && DECL_MODULE_IMPORT_P (not_tmpl))
    4287             :         {
    4288             :           /* We need to be careful here, so as to not explode on
    4289             :              inconsistent data -- we're probably debugging, because
    4290             :              Something Is Wrong.  */
    4291       25989 :           unsigned index = import_entity_index (t, true);
    4292       25989 :           if (!(index & ~(~0u >> 1)))
    4293       25476 :             origin = import_entity_module (index)->mod;
    4294         513 :           else if (index > ~(~0u >> 1))
    4295             :             /* An imported partition member that we're emitting.  */
    4296             :             origin = 0;
    4297             :           else
    4298          15 :             origin = -2;
    4299             :         }
    4300             : 
    4301     1495741 :       name = DECL_NAME (t) ? DECL_NAME (t)
    4302        7660 :         : HAS_DECL_ASSEMBLER_NAME_P (t) ? DECL_ASSEMBLER_NAME_RAW (t)
    4303             :         : NULL_TREE;
    4304     1495741 :     }
    4305             :   else
    4306             :     name = t;
    4307             : 
    4308     1613394 :   if (name)
    4309     1559381 :     switch (TREE_CODE (name))
    4310             :       {
    4311       25272 :       default:
    4312       25272 :         fputs ("#unnamed#", stream);
    4313       25272 :         break;
    4314             : 
    4315     1510542 :       case IDENTIFIER_NODE:
    4316     1510542 :         fwrite (IDENTIFIER_POINTER (name), 1, IDENTIFIER_LENGTH (name), stream);
    4317     1510542 :         break;
    4318             : 
    4319       23507 :       case INTEGER_CST:
    4320       23507 :         print_hex (wi::to_wide (name), stream);
    4321       23507 :         break;
    4322             : 
    4323          60 :       case STRING_CST:
    4324             :         /* If TREE_TYPE is NULL, this is a raw string.  */
    4325         120 :         fwrite (TREE_STRING_POINTER (name), 1,
    4326          60 :                 TREE_STRING_LENGTH (name) - (TREE_TYPE (name) != NULL_TREE),
    4327             :                 stream);
    4328          60 :         break;
    4329             :       }
    4330             :   else
    4331       54013 :     fputs ("#null#", stream);
    4332             : 
    4333     1613394 :   if (origin >= 0)
    4334             :     {
    4335       25974 :       const module_state *module = (*modules)[origin];
    4336       51948 :       fprintf (stream, "@%s:%d", !module ? "" : !module->name ? "(unnamed)"
    4337       25974 :                : module->get_flatname (), origin);
    4338             :     }
    4339     1587420 :   else if (origin == -2)
    4340          15 :     fprintf (stream, "@???");
    4341             : 
    4342     1613394 :   if (ti)
    4343             :     {
    4344      409787 :       tree args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (ti));
    4345      409787 :       fputs ("<", stream);
    4346      409787 :       if (args)
    4347     1051269 :         for (int ix = 0; ix != TREE_VEC_LENGTH (args); ix++)
    4348             :           {
    4349      641482 :             if (ix)
    4350      231707 :               fputs (",", stream);
    4351      641482 :             nested_name (TREE_VEC_ELT (args, ix));
    4352             :           }
    4353      409787 :       fputs (">", stream);
    4354             :     }
    4355             : 
    4356     1613394 :   return true;
    4357             : }
    4358             : 
    4359             : /* Formatted dumping.  FORMAT begins with '+' do not emit a trailing
    4360             :    new line.  (Normally it is appended.)
    4361             :    Escapes:
    4362             :       %C - tree_code
    4363             :       %I - identifier
    4364             :       %M - module_state
    4365             :       %N - name -- DECL_NAME
    4366             :       %P - context:name pair
    4367             :       %R - unsigned:unsigned ratio
    4368             :       %S - symbol -- DECL_ASSEMBLER_NAME
    4369             :       %U - long unsigned
    4370             :       %V - version
    4371             :       --- the following are printf-like, but without its flexibility
    4372             :       %d - decimal int
    4373             :       %p - pointer
    4374             :       %s - string
    4375             :       %u - unsigned int
    4376             :       %x - hex int
    4377             : 
    4378             :   We do not implement the printf modifiers.  */
    4379             : 
    4380             : bool
    4381      568043 : dumper::operator () (const char *format, ...)
    4382             : {
    4383      568043 :   if (!(*this) ())
    4384             :     return false;
    4385             : 
    4386      535319 :   bool no_nl = format[0] == '+';
    4387      535319 :   format += no_nl;
    4388             : 
    4389      535319 :   if (dumps->bol)
    4390             :     {
    4391             :       /* Module import indent.  */
    4392      386495 :       if (unsigned depth = dumps->stack.length () - 1)
    4393             :         {
    4394       18714 :           const char *prefix = ">>>>";
    4395       37428 :           fprintf (dumps->stream, (depth <= strlen (prefix)
    4396       18714 :                                    ? &prefix[strlen (prefix) - depth]
    4397             :                                    : ">.%d.>"), depth);
    4398             :         }
    4399             : 
    4400             :       /* Local indent.  */
    4401      386495 :       if (unsigned indent = dumps->indent)
    4402             :         {
    4403      290683 :           const char *prefix = "      ";
    4404      551007 :           fprintf (dumps->stream, (indent <= strlen (prefix)
    4405      260324 :                                    ? &prefix[strlen (prefix) - indent]
    4406             :                                    : "  .%d.  "), indent);
    4407             :         }
    4408      386495 :       dumps->bol = false;
    4409             :     }
    4410             : 
    4411      535319 :   va_list args;
    4412      535319 :   va_start (args, format);
    4413     1958751 :   while (const char *esc = strchr (format, '%'))
    4414             :     {
    4415     1423432 :       fwrite (format, 1, (size_t)(esc - format), dumps->stream);
    4416     1423432 :       format = ++esc;
    4417     1423432 :       switch (*format++)
    4418             :         {
    4419           0 :         default:
    4420           0 :           gcc_unreachable ();
    4421             : 
    4422         477 :         case '%':
    4423         477 :           fputc ('%', dumps->stream);
    4424         477 :           break;
    4425             : 
    4426      294317 :         case 'C': /* Code */
    4427      294317 :           {
    4428      294317 :             tree_code code = (tree_code)va_arg (args, unsigned);
    4429      294317 :             fputs (get_tree_code_name (code), dumps->stream);
    4430             :           }
    4431      294317 :           break;
    4432             : 
    4433          81 :         case 'I': /* Identifier.  */
    4434          81 :           {
    4435          81 :             tree t = va_arg (args, tree);
    4436          81 :             dumps->nested_name (t);
    4437             :           }
    4438          81 :           break;
    4439             : 
    4440        6237 :         case 'M': /* Module. */
    4441        6237 :           {
    4442        6237 :             const char *str = "(none)";
    4443        6237 :             if (module_state *m = va_arg (args, module_state *))
    4444             :               {
    4445        6237 :                 if (!m->has_location ())
    4446             :                   str = "(detached)";
    4447             :                 else
    4448        6237 :                   str = m->get_flatname ();
    4449             :               }
    4450        6237 :             fputs (str, dumps->stream);
    4451             :           }
    4452        6237 :           break;
    4453             : 
    4454      328297 :         case 'N': /* Name.  */
    4455      328297 :           {
    4456      328297 :             tree t = va_arg (args, tree);
    4457      657047 :             while (t && TREE_CODE (t) == OVERLOAD)
    4458         453 :               t = OVL_FUNCTION (t);
    4459      328297 :             fputc ('\'', dumps->stream);
    4460      328297 :             dumps->nested_name (t);
    4461      328297 :             fputc ('\'', dumps->stream);
    4462             :           }
    4463      328297 :           break;
    4464             : 
    4465       15609 :         case 'P': /* Pair.  */
    4466       15609 :           {
    4467       15609 :             tree ctx = va_arg (args, tree);
    4468       15609 :             tree name = va_arg (args, tree);
    4469       15609 :             fputc ('\'', dumps->stream);
    4470       15609 :             dumps->nested_name (ctx);
    4471       15609 :             if (ctx && ctx != global_namespace)
    4472       10612 :               fputs ("::", dumps->stream);
    4473       15609 :             dumps->nested_name (name);
    4474       15609 :             fputc ('\'', dumps->stream);
    4475             :           }
    4476       15609 :           break;
    4477             : 
    4478         675 :         case 'R': /* Ratio */
    4479         675 :           {
    4480         675 :             unsigned a = va_arg (args, unsigned);
    4481         675 :             unsigned b = va_arg (args, unsigned);
    4482         675 :             fprintf (dumps->stream, "%.1f", (float) a / (b + !b));
    4483             :           }
    4484         675 :           break;
    4485             : 
    4486       33222 :         case 'S': /* Symbol name */
    4487       33222 :           {
    4488       33222 :             tree t = va_arg (args, tree);
    4489       33222 :             if (t && TYPE_P (t))
    4490       12066 :               t = TYPE_NAME (t);
    4491       31794 :             if (t && HAS_DECL_ASSEMBLER_NAME_P (t)
    4492       30534 :                 && DECL_ASSEMBLER_NAME_SET_P (t))
    4493             :               {
    4494         171 :                 fputc ('(', dumps->stream);
    4495         171 :                 fputs (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (t)),
    4496         171 :                        dumps->stream);
    4497         171 :                 fputc (')', dumps->stream);
    4498             :               }
    4499             :           }
    4500             :           break;
    4501             : 
    4502           0 :         case 'U': /* long unsigned.  */
    4503           0 :           {
    4504           0 :             unsigned long u = va_arg (args, unsigned long);
    4505           0 :             fprintf (dumps->stream, "%lu", u);
    4506             :           }
    4507           0 :           break;
    4508             : 
    4509         669 :         case 'V': /* Verson.  */
    4510         669 :           {
    4511         669 :             unsigned v = va_arg (args, unsigned);
    4512         669 :             verstr_t string;
    4513             : 
    4514         669 :             version2string (v, string);
    4515         669 :             fputs (string, dumps->stream);
    4516             :           }
    4517         669 :           break;
    4518             : 
    4519           0 :         case 'c': /* Character.  */
    4520           0 :           {
    4521           0 :             int c = va_arg (args, int);
    4522           0 :             fputc (c, dumps->stream);
    4523             :           }
    4524           0 :           break;
    4525             : 
    4526       57253 :         case 'd': /* Decimal Int.  */
    4527       57253 :           {
    4528       57253 :             int d = va_arg (args, int);
    4529       57253 :             fprintf (dumps->stream, "%d", d);
    4530             :           }
    4531       57253 :           break;
    4532             : 
    4533           0 :         case 'p': /* Pointer. */
    4534           0 :           {
    4535           0 :             void *p = va_arg (args, void *);
    4536           0 :             fprintf (dumps->stream, "%p", p);
    4537             :           }
    4538           0 :           break;
    4539             : 
    4540      383856 :         case 's': /* String. */
    4541      383856 :           {
    4542      383856 :             const char *s = va_arg (args, char *);
    4543      383856 :             gcc_checking_assert (s);
    4544      383856 :             fputs (s, dumps->stream);
    4545             :           }
    4546      383856 :           break;
    4547             : 
    4548      300603 :         case 'u': /* Unsigned.  */
    4549      300603 :           {
    4550      300603 :             unsigned u = va_arg (args, unsigned);
    4551      300603 :             fprintf (dumps->stream, "%u", u);
    4552             :           }
    4553      300603 :           break;
    4554             : 
    4555        2136 :         case 'x': /* Hex. */
    4556        2136 :           {
    4557        2136 :             unsigned x = va_arg (args, unsigned);
    4558        2136 :             fprintf (dumps->stream, "%x", x);
    4559             :           }
    4560        2136 :           break;
    4561             :         }
    4562             :     }
    4563      535319 :   fputs (format, dumps->stream);
    4564      535319 :   va_end (args);
    4565      535319 :   if (!no_nl)
    4566             :     {
    4567      386495 :       dumps->bol = true;
    4568      386495 :       fputc ('\n', dumps->stream);
    4569             :     }
    4570             :   return true;
    4571             : }
    4572             : 
    4573             : struct note_def_cache_hasher : ggc_cache_ptr_hash<tree_node>
    4574             : {
    4575      242621 :   static int keep_cache_entry (tree t)
    4576             :   {
    4577      242621 :     if (!CHECKING_P)
    4578             :       /* GTY is unfortunately not clever enough to conditionalize
    4579             :          this.  */
    4580             :       gcc_unreachable ();
    4581             : 
    4582      242621 :     if (ggc_marked_p (t))
    4583             :       return -1;
    4584             : 
    4585        2232 :     unsigned n = dump.push (NULL);
    4586             :     /* This might or might not be an error.  We should note its
    4587             :        dropping whichever.  */
    4588        2232 :     dump () && dump ("Dropping %N from note_defs table", t);
    4589        2232 :     dump.pop (n);
    4590             : 
    4591        2232 :     return 0;
    4592             :   }
    4593             : };
    4594             : 
    4595             : /* We should stream each definition at most once.
    4596             :    This needs to be a cache because there are cases where a definition
    4597             :    ends up being not retained, and we need to drop those so we don't
    4598             :    get confused if memory is reallocated.  */
    4599             : typedef hash_table<note_def_cache_hasher> note_defs_table_t;
    4600             : static GTY((cache)) note_defs_table_t *note_defs;
    4601             : 
    4602             : void
    4603      425157 : trees_in::assert_definition (tree decl ATTRIBUTE_UNUSED,
    4604             :                              bool installing ATTRIBUTE_UNUSED)
    4605             : {
    4606             : #if CHECKING_P
    4607      425157 :   tree *slot = note_defs->find_slot (decl, installing ? INSERT : NO_INSERT);
    4608      425157 :   tree not_tmpl = STRIP_TEMPLATE (decl);
    4609      425157 :   if (installing)
    4610             :     {
    4611             :       /* We must be inserting for the first time.  */
    4612      204936 :       gcc_assert (!*slot);
    4613      204936 :       *slot = decl;
    4614             :     }
    4615             :   else
    4616             :     /* If this is not the mergeable entity, it should not be in the
    4617             :        table.  If it is a non-global-module mergeable entity, it
    4618             :        should be in the table.  Global module entities could have been
    4619             :        defined textually in the current TU and so might or might not
    4620             :        be present.  */
    4621      220221 :     gcc_assert (!is_duplicate (decl)
    4622             :                 ? !slot
    4623             :                 : (slot
    4624             :                    || !DECL_LANG_SPECIFIC (not_tmpl)
    4625             :                    || !DECL_MODULE_PURVIEW_P (not_tmpl)
    4626             :                    || (!DECL_MODULE_IMPORT_P (not_tmpl)
    4627             :                        && header_module_p ())));
    4628             : 
    4629      425157 :   if (not_tmpl != decl)
    4630      253747 :     gcc_assert (!note_defs->find_slot (not_tmpl, NO_INSERT));
    4631             : #endif
    4632      425157 : }
    4633             : 
    4634             : void
    4635      366930 : trees_out::assert_definition (tree decl ATTRIBUTE_UNUSED)
    4636             : {
    4637             : #if CHECKING_P
    4638      366930 :   tree *slot = note_defs->find_slot (decl, INSERT);
    4639      366930 :   gcc_assert (!*slot);
    4640      366930 :   *slot = decl;
    4641      366930 :   if (TREE_CODE (decl) == TEMPLATE_DECL)
    4642      220361 :     gcc_assert (!note_defs->find_slot (DECL_TEMPLATE_RESULT (decl), NO_INSERT));
    4643             : #endif
    4644      366930 : }
    4645             : 
    4646             : /********************************************************************/
    4647             : static bool
    4648        7771 : noisy_p ()
    4649             : {
    4650           0 :   if (quiet_flag)
    4651             :     return false;
    4652             : 
    4653           0 :   pp_needs_newline (global_dc->printer) = true;
    4654           0 :   diagnostic_set_last_function (global_dc, (diagnostic_info *) NULL);
    4655             : 
    4656           0 :   return true;
    4657             : }
    4658             : 
    4659             : /* Set the cmi repo.  Strip trailing '/', '.' becomes NULL.  */
    4660             : 
    4661             : static void
    4662       91198 : set_cmi_repo (const char *r)
    4663             : {
    4664       91198 :   XDELETEVEC (cmi_repo);
    4665       91198 :   XDELETEVEC (cmi_path);
    4666       91198 :   cmi_path_alloc = 0;
    4667             : 
    4668       91198 :   cmi_repo = NULL;
    4669       91198 :   cmi_repo_length = 0;
    4670             : 
    4671       91198 :   if (!r || !r[0])
    4672             :     return;
    4673             : 
    4674        2879 :   size_t len = strlen (r);
    4675        2879 :   cmi_repo = XNEWVEC (char, len + 1);
    4676        2879 :   memcpy (cmi_repo, r, len + 1);
    4677             :   
    4678        2879 :   if (len > 1 && IS_DIR_SEPARATOR (cmi_repo[len-1]))
    4679        2879 :     len--;
    4680        2879 :   if (len == 1 && cmi_repo[0] == '.')
    4681          15 :     len--;
    4682        2879 :   cmi_repo[len] = 0;
    4683        2879 :   cmi_repo_length = len;
    4684             : }
    4685             : 
    4686             : /* TO is a repo-relative name.  Provide one that we may use from where
    4687             :    we are.  */
    4688             : 
    4689             : static const char *
    4690        3643 : maybe_add_cmi_prefix (const char *to, size_t *len_p = NULL)
    4691             : {
    4692        3643 :   size_t len = len_p || cmi_repo_length ? strlen (to) : 0;
    4693             : 
    4694        3643 :   if (cmi_repo_length && !IS_ABSOLUTE_PATH (to))
    4695             :     {
    4696        3625 :       if (cmi_path_alloc < cmi_repo_length + len + 2)
    4697             :         {
    4698        2870 :           XDELETEVEC (cmi_path);
    4699        2870 :           cmi_path_alloc = cmi_repo_length + len * 2 + 2;
    4700        2870 :           cmi_path = XNEWVEC (char, cmi_path_alloc);
    4701             : 
    4702        2870 :           memcpy (cmi_path, cmi_repo, cmi_repo_length);
    4703        2870 :           cmi_path[cmi_repo_length] = DIR_SEPARATOR;
    4704             :         }
    4705             : 
    4706        3625 :       memcpy (&cmi_path[cmi_repo_length + 1], to, len + 1);
    4707        3625 :       len += cmi_repo_length + 1;
    4708        3625 :       to = cmi_path;
    4709             :     }
    4710             : 
    4711        3643 :   if (len_p)
    4712        1714 :     *len_p = len;
    4713             : 
    4714        3643 :   return to;
    4715             : }
    4716             : 
    4717             : /* Try and create the directories of PATH.  */
    4718             : 
    4719             : static void
    4720           6 : create_dirs (char *path)
    4721             : {
    4722             :   /* Try and create the missing directories.  */
    4723         344 :   for (char *base = path; *base; base++)
    4724         338 :     if (IS_DIR_SEPARATOR (*base))
    4725             :       {
    4726          29 :         char sep = *base;
    4727          29 :         *base = 0;
    4728          29 :         int failed = mkdir (path, S_IRWXU | S_IRWXG | S_IRWXO);
    4729          31 :         dump () && dump ("Mkdir ('%s') errno:=%u", path, failed ? errno : 0);
    4730          29 :         *base = sep;
    4731          29 :         if (failed
    4732             :             /* Maybe racing with another creator (of a *different*
    4733             :                module).  */
    4734          11 :             && errno != EEXIST)
    4735             :           break;
    4736             :       }
    4737           6 : }
    4738             : 
    4739             : /* Given a CLASSTYPE_DECL_LIST VALUE get the template friend decl,
    4740             :    if that's what this is.  */
    4741             : 
    4742             : static tree
    4743       53138 : friend_from_decl_list (tree frnd)
    4744             : {
    4745       53138 :   tree res = frnd;
    4746             : 
    4747       53138 :   if (TREE_CODE (frnd) != TEMPLATE_DECL)
    4748             :     {
    4749       24955 :       tree tmpl = NULL_TREE;
    4750       24955 :       if (TYPE_P (frnd))
    4751             :         {
    4752        5037 :           res = TYPE_NAME (frnd);
    4753        5010 :           if (CLASS_TYPE_P (frnd)
    4754       10047 :               && CLASSTYPE_TEMPLATE_INFO (frnd))
    4755        5010 :             tmpl = CLASSTYPE_TI_TEMPLATE (frnd);
    4756             :         }
    4757       19918 :       else if (DECL_TEMPLATE_INFO (frnd))
    4758             :         {
    4759       19918 :           tmpl = DECL_TI_TEMPLATE (frnd);
    4760       19918 :           if (TREE_CODE (tmpl) != TEMPLATE_DECL)
    4761             :             tmpl = NULL_TREE;
    4762             :         }
    4763             : 
    4764       29254 :       if (tmpl && DECL_TEMPLATE_RESULT (tmpl) == res)
    4765             :         res = tmpl;
    4766             :     }
    4767             : 
    4768       53138 :   return res;
    4769             : }
    4770             : 
    4771             : static tree
    4772       28330 : find_enum_member (tree ctx, tree name)
    4773             : {
    4774       28330 :   for (tree values = TYPE_VALUES (ctx);
    4775      435650 :        values; values = TREE_CHAIN (values))
    4776      427603 :     if (DECL_NAME (TREE_VALUE (values)) == name)
    4777             :       return TREE_VALUE (values);
    4778             : 
    4779             :   return NULL_TREE;
    4780             : }
    4781             : 
    4782             : /********************************************************************/
    4783             : /* Instrumentation gathered writing bytes.  */
    4784             : 
    4785             : void
    4786         225 : bytes_out::instrument ()
    4787             : {
    4788         225 :   dump ("Wrote %u bytes in %u blocks", lengths[3], spans[3]);
    4789         225 :   dump ("Wrote %u bits in %u bytes", lengths[0] + lengths[1], lengths[2]);
    4790         675 :   for (unsigned ix = 0; ix < 2; ix++)
    4791         675 :     dump ("  %u %s spans of %R bits", spans[ix],
    4792             :           ix ? "one" : "zero", lengths[ix], spans[ix]);
    4793         225 :   dump ("  %u blocks with %R bits padding", spans[2],
    4794         225 :         lengths[2] * 8 - (lengths[0] + lengths[1]), spans[2]);
    4795         225 : }
    4796             : 
    4797             : /* Instrumentation gathered writing trees.  */
    4798             : void
    4799        1657 : trees_out::instrument ()
    4800             : {
    4801        1657 :   if (dump (""))
    4802             :     {
    4803         225 :       bytes_out::instrument ();
    4804         225 :       dump ("Wrote:");
    4805         225 :       dump ("  %u decl trees", decl_val_count);
    4806         225 :       dump ("  %u other trees", tree_val_count);
    4807         225 :       dump ("  %u back references", back_ref_count);
    4808         225 :       dump ("  %u null trees", null_count);
    4809             :     }
    4810        1657 : }
    4811             : 
    4812             : /* Setup and teardown for a tree walk.  */
    4813             : 
    4814             : void
    4815     2365197 : trees_out::begin ()
    4816             : {
    4817     2365197 :   gcc_assert (!streaming_p () || !tree_map.elements ());
    4818             : 
    4819     2365197 :   mark_trees ();
    4820     2365197 :   if (streaming_p ())
    4821      303743 :     parent::begin ();
    4822     2365197 : }
    4823             : 
    4824             : unsigned
    4825      303743 : trees_out::end (elf_out *sink, unsigned name, unsigned *crc_ptr)
    4826             : {
    4827      303743 :   gcc_checking_assert (streaming_p ());
    4828             : 
    4829      303743 :   unmark_trees ();
    4830      303743 :   return parent::end (sink, name, crc_ptr);
    4831             : }
    4832             : 
    4833             : void
    4834     2061454 : trees_out::end ()
    4835             : {
    4836     2061454 :   gcc_assert (!streaming_p ());
    4837             : 
    4838     2061454 :   unmark_trees ();
    4839             :   /* Do not parent::end -- we weren't streaming.  */
    4840     2061454 : }
    4841             : 
    4842             : void
    4843     2365197 : trees_out::mark_trees ()
    4844             : {
    4845     2365197 :   if (size_t size = tree_map.elements ())
    4846             :     {
    4847             :       /* This isn't our first rodeo, destroy and recreate the
    4848             :          tree_map.  I'm a bad bad man.  Use the previous size as a
    4849             :          guess for the next one (so not all bad).  */
    4850     1769143 :       tree_map.~ptr_int_hash_map ();
    4851     1769143 :       new (&tree_map) ptr_int_hash_map (size);
    4852             :     }
    4853             : 
    4854             :   /* Install the fixed trees, with +ve references.  */
    4855     2365197 :   unsigned limit = fixed_trees->length ();
    4856   455848729 :   for (unsigned ix = 0; ix != limit; ix++)
    4857             :     {
    4858   453483532 :       tree val = (*fixed_trees)[ix];
    4859   453483532 :       bool existed = tree_map.put (val, ix + tag_fixed);
    4860   453483532 :       gcc_checking_assert (!TREE_VISITED (val) && !existed);
    4861   453483532 :       TREE_VISITED (val) = true;
    4862             :     }
    4863             : 
    4864     2365197 :   ref_num = 0;
    4865     2365197 : }
    4866             : 
    4867             : /* Unmark the trees we encountered  */
    4868             : 
    4869             : void
    4870     2365197 : trees_out::unmark_trees ()
    4871             : {
    4872     2365197 :   ptr_int_hash_map::iterator end (tree_map.end ());
    4873   531905374 :   for (ptr_int_hash_map::iterator iter (tree_map.begin ()); iter != end; ++iter)
    4874             :     {
    4875   529540177 :       tree node = reinterpret_cast<tree> ((*iter).first);
    4876   529540177 :       int ref = (*iter).second;
    4877             :       /* We should have visited the node, and converted its mergeable
    4878             :          reference to a regular reference.  */
    4879   529540177 :       gcc_checking_assert (TREE_VISITED (node)
    4880             :                            && (ref <= tag_backref || ref >= tag_fixed));
    4881   529540177 :       TREE_VISITED (node) = false;
    4882             :     }
    4883     2365197 : }
    4884             : 
    4885             : /* Mark DECL for by-value walking.  We do this by inserting it into
    4886             :    the tree map with a reference of zero.  May be called multiple
    4887             :    times on the same node.  */
    4888             : 
    4889             : void
    4890     3622340 : trees_out::mark_by_value (tree decl)
    4891             : {
    4892     3622340 :   gcc_checking_assert (DECL_P (decl)
    4893             :                        /* Enum consts are INTEGER_CSTS.  */
    4894             :                        || TREE_CODE (decl) == INTEGER_CST
    4895             :                        || TREE_CODE (decl) == TREE_BINFO);
    4896             : 
    4897     3622340 :   if (TREE_VISITED (decl))
    4898             :     /* Must already be forced or fixed.  */
    4899        2794 :     gcc_checking_assert (*tree_map.get (decl) >= tag_value);
    4900             :   else
    4901             :     {
    4902     3619546 :       bool existed = tree_map.put (decl, tag_value);
    4903     3619546 :       gcc_checking_assert (!existed);
    4904     3619546 :       TREE_VISITED (decl) = true;
    4905             :     }
    4906     3622340 : }
    4907             : 
    4908             : int
    4909    92442314 : trees_out::get_tag (tree t)
    4910             : {
    4911    92442314 :   gcc_checking_assert (TREE_VISITED (t));
    4912    92442314 :   return *tree_map.get (t);
    4913             : }
    4914             : 
    4915             : /* Insert T into the map, return its tag number.    */
    4916             : 
    4917             : int
    4918    76056645 : trees_out::insert (tree t, walk_kind walk)
    4919             : {
    4920    76056645 :   gcc_checking_assert (walk != WK_normal || !TREE_VISITED (t));
    4921    76056645 :   int tag = --ref_num;
    4922    76056645 :   bool existed;
    4923    76056645 :   int &slot = tree_map.get_or_insert (t, &existed);
    4924    76056645 :   gcc_checking_assert (TREE_VISITED (t) == existed
    4925             :                        && (!existed
    4926             :                            || (walk == WK_value && slot == tag_value)));
    4927    76056645 :   TREE_VISITED (t) = true;
    4928    76056645 :   slot = tag;
    4929             : 
    4930    76056645 :   return tag;
    4931             : }
    4932             : 
    4933             : /* Insert T into the backreference array.  Return its back reference
    4934             :    number.  */
    4935             : 
    4936             : int
    4937    25282348 : trees_in::insert (tree t)
    4938             : {
    4939    25282348 :   gcc_checking_assert (t || get_overrun ());
    4940    25282348 :   back_refs.safe_push (t);
    4941    25282348 :   return -(int)back_refs.length ();
    4942             : }
    4943             : 
    4944             : /* A chained set of decls.  */
    4945             : 
    4946             : void
    4947      661272 : trees_out::chained_decls (tree decls)
    4948             : {
    4949     1058650 :   for (; decls; decls = DECL_CHAIN (decls))
    4950             :     {
    4951      397378 :       if (VAR_OR_FUNCTION_DECL_P (decls)
    4952      397378 :           && DECL_LOCAL_DECL_P (decls))
    4953             :         {
    4954             :           /* Make sure this is the first encounter, and mark for
    4955             :              walk-by-value.  */
    4956         248 :           gcc_checking_assert (!TREE_VISITED (decls)
    4957             :                                && !DECL_TEMPLATE_INFO (decls));
    4958         248 :           mark_by_value (decls);
    4959             :         }
    4960      397378 :       tree_node (decls);
    4961             :     }
    4962      661272 :   tree_node (NULL_TREE);
    4963      661272 : }
    4964             : 
    4965             : tree
    4966      399245 : trees_in::chained_decls ()
    4967             : {
    4968      399245 :   tree decls = NULL_TREE;
    4969      399245 :   for (tree *chain = &decls;;)
    4970      647237 :     if (tree decl = tree_node ())
    4971             :       {
    4972      247992 :         if (!DECL_P (decl) || DECL_CHAIN (decl))
    4973             :           {
    4974           0 :             set_overrun ();
    4975             :             break;
    4976             :           }
    4977      247992 :         *chain = decl;
    4978      247992 :         chain = &DECL_CHAIN (decl);
    4979             :       }
    4980             :     else
    4981      247992 :       break;
    4982             : 
    4983      399245 :   return decls;
    4984             : }
    4985             : 
    4986             : /* A vector of decls following DECL_CHAIN.  */
    4987             : 
    4988             : void
    4989      372932 : trees_out::vec_chained_decls (tree decls)
    4990             : {
    4991      372932 :   if (streaming_p ())
    4992             :     {
    4993             :       unsigned len = 0;
    4994             : 
    4995     1027088 :       for (tree decl = decls; decl; decl = DECL_CHAIN (decl))
    4996      842374 :         len++;
    4997      184714 :       u (len);
    4998             :     }
    4999             : 
    5000     2072282 :   for (tree decl = decls; decl; decl = DECL_CHAIN (decl))
    5001             :     {
    5002      401572 :       if (DECL_IMPLICIT_TYPEDEF_P (decl)
    5003     1712778 :           && TYPE_NAME (TREE_TYPE (decl)) != decl)
    5004             :         /* An anonynmous struct with a typedef name.  An odd thing to
    5005             :            write.  */
    5006          12 :         tree_node (NULL_TREE);
    5007             :       else
    5008     1699338 :         tree_node (decl);
    5009             :     }
    5010      372932 : }
    5011             : 
    5012             : vec<tree, va_heap> *
    5013      193730 : trees_in::vec_chained_decls ()
    5014             : {
    5015      193730 :   vec<tree, va_heap> *v = NULL;
    5016             : 
    5017      193730 :   if (unsigned len = u ())
    5018             :     {
    5019      101652 :       vec_alloc (v, len);
    5020             : 
    5021     1082996 :       for (unsigned ix = 0; ix < len; ix++)
    5022             :         {
    5023      981344 :           tree decl = tree_node ();
    5024      981344 :           if (decl && !DECL_P (decl))
    5025             :             {
    5026           0 :               set_overrun ();
    5027           0 :               break;
    5028             :             }
    5029      981344 :           v->quick_push (decl);
    5030             :         }
    5031             : 
    5032      101652 :       if (get_overrun ())
    5033             :         {
    5034           0 :           vec_free (v);
    5035           0 :           v = NULL;
    5036             :         }
    5037             :     }
    5038             : 
    5039      193730 :   return v;
    5040             : }
    5041             : 
    5042             : /* A vector of trees.  */
    5043             : 
    5044             : void
    5045      273885 : trees_out::tree_vec (vec<tree, va_gc> *v)
    5046             : {
    5047      273885 :   unsigned len = vec_safe_length (v);
    5048      273885 :   if (streaming_p ())
    5049      136941 :     u (len);
    5050      360861 :   for (unsigned ix = 0; ix != len; ix++)
    5051       86976 :     tree_node ((*v)[ix]);
    5052      273885 : }
    5053             : 
    5054             : vec<tree, va_gc> *
    5055      144868 : trees_in::tree_vec ()
    5056             : {
    5057      144868 :   vec<tree, va_gc> *v = NULL;
    5058      144868 :   if (unsigned len = u ())
    5059             :     {
    5060       42822 :       vec_alloc (v, len);
    5061       89637 :       for (unsigned ix = 0; ix != len; ix++)
    5062       46815 :         v->quick_push (tree_node ());
    5063             :     }
    5064      144868 :   return v;
    5065             : }
    5066             : 
    5067             : /* A vector of tree pairs.  */
    5068             : 
    5069             : void
    5070        6528 : trees_out::tree_pair_vec (vec<tree_pair_s, va_gc> *v)
    5071             : {
    5072        6528 :   unsigned len = vec_safe_length (v);
    5073        6528 :   if (streaming_p ())
    5074        3264 :     u (len);
    5075        6528 :   if (len)
    5076       35332 :     for (unsigned ix = 0; ix != len; ix++)
    5077             :       {
    5078       28950 :         tree_pair_s const &s = (*v)[ix];
    5079       28950 :         tree_node (s.purpose);
    5080       28950 :         tree_node (s.value);
    5081             :       }
    5082        6528 : }
    5083             : 
    5084             : vec<tree_pair_s, va_gc> *
    5085        3950 : trees_in::tree_pair_vec ()
    5086             : {
    5087        3950 :   vec<tree_pair_s, va_gc> *v = NULL;
    5088        3950 :   if (unsigned len = u ())
    5089             :     {
    5090        3879 :       vec_alloc (v, len);
    5091       21820 :       for (unsigned ix = 0; ix != len; ix++)
    5092             :         {
    5093       17941 :           tree_pair_s s;
    5094       17941 :           s.purpose = tree_node ();
    5095       17941 :           s.value = tree_node ();
    5096       17941 :           v->quick_push (s);
    5097             :       }
    5098             :     }
    5099        3950 :   return v;
    5100             : }
    5101             : 
    5102             : void
    5103      388290 : trees_out::tree_list (tree list, bool has_purpose)
    5104             : {
    5105     1572152 :   for (; list; list = TREE_CHAIN (list))
    5106             :     {
    5107     1183862 :       gcc_checking_assert (TREE_VALUE (list));
    5108     1183862 :       tree_node (TREE_VALUE (list));
    5109     1183862 :       if (has_purpose)
    5110     1153168 :         tree_node (TREE_PURPOSE (list));
    5111             :     }
    5112      388290 :   tree_node (NULL_TREE);
    5113      388290 : }
    5114             : 
    5115             : tree
    5116      202342 : trees_in::tree_list (bool has_purpose)
    5117             : {
    5118      202342 :   tree res = NULL_TREE;
    5119             : 
    5120      890317 :   for (tree *chain = &res; tree value = tree_node ();
    5121     1375950 :        chain = &TREE_CHAIN (*chain))
    5122             :     {
    5123      687975 :       tree purpose = has_purpose ? tree_node () : NULL_TREE;
    5124      687975 :       *chain = build_tree_list (purpose, value);
    5125      687975 :     }
    5126             : 
    5127      202342 :   return res;
    5128             : }
    5129             : /* Start tree write.  Write information to allocate the receiving
    5130             :    node.  */
    5131             : 
    5132             : void
    5133    14808878 : trees_out::start (tree t, bool code_streamed)
    5134             : {
    5135    14808878 :   if (TYPE_P (t))
    5136             :     {
    5137      651303 :       enum tree_code code = TREE_CODE (t);
    5138      651303 :       gcc_checking_assert (TYPE_MAIN_VARIANT (t) == t);
    5139             :       /* All these types are TYPE_NON_COMMON.  */
    5140      651303 :       gcc_checking_assert (code == RECORD_TYPE
    5141             :                            || code == UNION_TYPE
    5142             :                            || code == ENUMERAL_TYPE
    5143             :                            || code == TEMPLATE_TYPE_PARM
    5144             :                            || code == TEMPLATE_TEMPLATE_PARM
    5145             :                            || code == BOUND_TEMPLATE_TEMPLATE_PARM);
    5146             :     }
    5147             : 
    5148    14808878 :   if (!code_streamed)
    5149    14217810 :     u (TREE_CODE (t));
    5150             : 
    5151    14808878 :   switch (TREE_CODE (t))
    5152             :     {
    5153    13225331 :     default:
    5154    13225331 :       if (VL_EXP_CLASS_P (t))
    5155      531926 :         u (VL_EXP_OPERAND_LENGTH (t));
    5156             :       break;
    5157             : 
    5158      535834 :     case INTEGER_CST:
    5159      535834 :       u (TREE_INT_CST_NUNITS (t));
    5160      535834 :       u (TREE_INT_CST_EXT_NUNITS (t));
    5161      535834 :       u (TREE_INT_CST_OFFSET_NUNITS (t));
    5162      535834 :       break;
    5163             : 
    5164           3 :     case OMP_CLAUSE:
    5165           3 :       state->extensions |= SE_OPENMP;
    5166           3 :       u (OMP_CLAUSE_CODE (t));
    5167           3 :       break;
    5168             : 
    5169       22266 :     case STRING_CST:
    5170       22266 :       str (TREE_STRING_POINTER (t), TREE_STRING_LENGTH (t));
    5171       22266 :       break;
    5172             : 
    5173          18 :     case VECTOR_CST:
    5174          18 :       u (VECTOR_CST_LOG2_NPATTERNS (t));
    5175          18 :       u (VECTOR_CST_NELTS_PER_PATTERN (t));
    5176          18 :       break;
    5177             : 
    5178      133677 :     case TREE_BINFO:
    5179      133677 :       u (BINFO_N_BASE_BINFOS (t));
    5180      133677 :       break;
    5181             : 
    5182      891749 :     case TREE_VEC:
    5183      891749 :       u (TREE_VEC_LENGTH (t));
    5184      891749 :       break;
    5185             : 
    5186           0 :     case FIXED_CST:
    5187           0 :     case POLY_INT_CST:
    5188           0 :       gcc_unreachable (); /* Not supported in C++.  */
    5189           0 :       break;
    5190             : 
    5191           0 :     case IDENTIFIER_NODE:
    5192           0 :     case SSA_NAME:
    5193           0 :     case TARGET_MEM_REF:
    5194           0 :     case TRANSLATION_UNIT_DECL:
    5195             :       /* We shouldn't meet these.  */
    5196           0 :       gcc_unreachable ();
    5197    14808878 :       break;
    5198             :     }
    5199    14808878 : }
    5200             : 
    5201             : /* Start tree read.  Allocate the receiving node.  */
    5202             : 
    5203             : tree
    5204    17475493 : trees_in::start (unsigned code)
    5205             : {
    5206    17475493 :   tree t = NULL_TREE;
    5207             : 
    5208    17475493 :   if (!code)
    5209    15573618 :     code = u ();
    5210             : 
    5211    17475493 :   switch (code)
    5212             :     {
    5213    15682565 :     default:
    5214    15682565 :       if (code >= MAX_TREE_CODES)
    5215             :         {
    5216           0 :         fail:
    5217           0 :           set_overrun ();
    5218           0 :           return NULL_TREE;
    5219             :         }
    5220    15682565 :       else if (TREE_CODE_CLASS (code) == tcc_vl_exp)
    5221             :         {
    5222      636849 :           unsigned ops = u ();
    5223      636849 :           t = build_vl_exp (tree_code (code), ops);
    5224             :         }
    5225             :       else
    5226    15045716 :         t = make_node (tree_code (code));
    5227             :       break;
    5228             : 
    5229      624076 :     case INTEGER_CST:
    5230      624076 :       {
    5231      624076 :         unsigned n = u ();
    5232      624076 :         unsigned e = u ();
    5233      624076 :         t = make_int_cst (n, e);
    5234      624076 :         TREE_INT_CST_OFFSET_NUNITS(t) = u ();
    5235             :       }
    5236      624076 :       break;
    5237             : 
    5238           3 :     case OMP_CLAUSE:
    5239           3 :       {
    5240           3 :         if (!(state->extensions & SE_OPENMP))
    5241           0 :           goto fail;
    5242             : 
    5243           3 :         unsigned omp_code = u ();
    5244           3 :         t = build_omp_clause (UNKNOWN_LOCATION, omp_clause_code (omp_code));
    5245             :       }
    5246           3 :       break;
    5247             : 
    5248       27189 :     case STRING_CST:
    5249       27189 :       {
    5250       27189 :         size_t l;
    5251       27189 :         const char *chars = str (&l);
    5252       27189 :         t = build_string (l, chars);
    5253             :       }
    5254       27189 :       break;
    5255             : 
    5256          24 :     case VECTOR_CST:
    5257          24 :       {
    5258          24 :         unsigned log2_npats = u ();
    5259          24 :         unsigned elts_per = u ();
    5260          24 :         t = make_vector (log2_npats, elts_per);
    5261             :       }
    5262          24 :       break;
    5263             : 
    5264      140918 :     case TREE_BINFO:
    5265      140918 :       t = make_tree_binfo (u ());
    5266      140918 :       break;
    5267             : 
    5268     1000718 :     case TREE_VEC:
    5269     1000718 :       t = make_tree_vec (u ());
    5270     1000718 :       break;
    5271             : 
    5272           0 :     case FIXED_CST:
    5273           0 :     case IDENTIFIER_NODE:
    5274           0 :     case POLY_INT_CST:
    5275           0 :     case SSA_NAME:
    5276           0 :     case TARGET_MEM_REF:
    5277           0 :     case TRANSLATION_UNIT_DECL:
    5278           0 :       goto fail;
    5279             :     }
    5280             : 
    5281             :   return t;
    5282             : }
    5283             : 
    5284             : /* The structure streamers access the raw fields, because the
    5285             :    alternative, of using the accessor macros can require using
    5286             :    different accessors for the same underlying field, depending on the
    5287             :    tree code.  That's both confusing and annoying.  */
    5288             : 
    5289             : /* Read & write the core boolean flags.  */
    5290             : 
    5291             : void
    5292    14828573 : trees_out::core_bools (tree t)
    5293             : {
    5294             : #define WB(X) (b (X))
    5295    14828573 :   tree_code code = TREE_CODE (t);
    5296             : 
    5297    14828573 :   WB (t->base.side_effects_flag);
    5298    14828573 :   WB (t->base.constant_flag);
    5299    14828573 :   WB (t->base.addressable_flag);
    5300    14828573 :   WB (t->base.volatile_flag);
    5301    14828573 :   WB (t->base.readonly_flag);
    5302             :   /* base.asm_written_flag is a property of the current TU's use of
    5303             :      this decl.  */
    5304    14828573 :   WB (t->base.nowarning_flag);
    5305             :   /* base.visited read as zero (it's set for writer, because that's
    5306             :      how we mark nodes).  */
    5307             :   /* base.used_flag is not streamed.  Readers may set TREE_USED of
    5308             :      decls they use.  */
    5309    14828573 :   WB (t->base.nothrow_flag);
    5310    14828573 :   WB (t->base.static_flag);
    5311    14828573 :   if (TREE_CODE_CLASS (code) != tcc_type)
    5312             :     /* This is TYPE_CACHED_VALUES_P for types.  */
    5313    14157575 :     WB (t->base.public_flag);
    5314    14828573 :   WB (t->base.private_flag);
    5315    14828573 :   WB (t->base.protected_flag);
    5316    14828573 :   WB (t->base.deprecated_flag);
    5317    14828573 :   WB (t->base.default_def_flag);
    5318             : 
    5319    14828573 :   switch (code)
    5320             :     {
    5321             :     case CALL_EXPR:
    5322             :     case INTEGER_CST:
    5323             :     case SSA_NAME:
    5324             :     case TARGET_MEM_REF:
    5325             :     case TREE_VEC:
    5326             :       /* These use different base.u fields.  */
    5327             :       break;
    5328             : 
    5329    12881220 :     default:
    5330    12881220 :       WB (t->base.u.bits.lang_flag_0);
    5331    12881220 :       bool flag_1 = t->base.u.bits.lang_flag_1;
    5332    12881220 :       if (!flag_1)
    5333             :         ;
    5334      362092 :       else if (code == TEMPLATE_INFO)
    5335             :         /* This is TI_PENDING_TEMPLATE_FLAG, not relevant to reader.  */
    5336             :         flag_1 = false;
    5337      358614 :       else if (code == VAR_DECL)
    5338             :         {
    5339             :           /* This is DECL_INITIALIZED_P.  */
    5340       62311 :           if (TREE_CODE (DECL_CONTEXT (t)) != FUNCTION_DECL)
    5341             :             /* We'll set this when reading the definition.  */
    5342    12881220 :             flag_1 = false;
    5343             :         }
    5344    12881220 :       WB (flag_1);
    5345    12881220 :       WB (t->base.u.bits.lang_flag_2);
    5346    12881220 :       WB (t->base.u.bits.lang_flag_3);
    5347    12881220 :       WB (t->base.u.bits.lang_flag_4);
    5348    12881220 :       WB (t->base.u.bits.lang_flag_5);
    5349    12881220 :       WB (t->base.u.bits.lang_flag_6);
    5350    12881220 :       WB (t->base.u.bits.saturating_flag);
    5351    12881220 :       WB (t->base.u.bits.unsigned_flag);
    5352    12881220 :       WB (t->base.u.bits.packed_flag);
    5353    12881220 :       WB (t->base.u.bits.user_align);
    5354    12881220 :       WB (t->base.u.bits.nameless_flag);
    5355    12881220 :       WB (t->base.u.bits.atomic_flag);
    5356    12881220 :       break;
    5357             :     }
    5358             : 
    5359    14828573 :   if (CODE_CONTAINS_STRUCT (code, TS_TYPE_COMMON))
    5360             :     {
    5361      670998 :       WB (t->type_common.no_force_blk_flag);
    5362      670998 :       WB (t->type_common.needs_constructing_flag);
    5363      670998 :       WB (t->type_common.transparent_aggr_flag);
    5364      670998 :       WB (t->type_common.restrict_flag);
    5365      670998 :       WB (t->type_common.string_flag);
    5366      670998 :       WB (t->type_common.lang_flag_0);
    5367      670998 :       WB (t->type_common.lang_flag_1);
    5368      670998 :       WB (t->type_common.lang_flag_2);
    5369      670998 :       WB (t->type_common.lang_flag_3);
    5370      670998 :       WB (t->type_common.lang_flag_4);
    5371      670998 :       WB (t->type_common.lang_flag_5);
    5372      670998 :       WB (t->type_common.lang_flag_6);
    5373      670998 :       WB (t->type_common.typeless_storage);
    5374             :     }
    5375             : 
    5376    14828573 :   if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
    5377             :     {
    5378     3559547 :       WB (t->decl_common.nonlocal_flag);
    5379     3559547 :       WB (t->decl_common.virtual_flag);
    5380     3559547 :       WB (t->decl_common.ignored_flag);
    5381     3559547 :       WB (t->decl_common.abstract_flag);
    5382     3559547 :       WB (t->decl_common.artificial_flag);
    5383     3559547 :       WB (t->decl_common.preserve_flag);
    5384     3559547 :       WB (t->decl_common.debug_expr_is_from);
    5385     3559547 :       WB (t->decl_common.lang_flag_0);
    5386     3559547 :       WB (t->decl_common.lang_flag_1);
    5387     3559547 :       WB (t->decl_common.lang_flag_2);
    5388     3559547 :       WB (t->decl_common.lang_flag_3);
    5389     3559547 :       WB (t->decl_common.lang_flag_4);
    5390     3559547 :       WB (t->decl_common.lang_flag_5);
    5391     3559547 :       WB (t->decl_common.lang_flag_6);
    5392     3559547 :       WB (t->decl_common.lang_flag_7);
    5393     3559547 :       WB (t->decl_common.lang_flag_8);
    5394     3559547 :       WB (t->decl_common.decl_flag_0);
    5395             : 
    5396     3559547 :       {
    5397             :         /* DECL_EXTERNAL -> decl_flag_1
    5398             :              == it is defined elsewhere
    5399             :            DECL_NOT_REALLY_EXTERN -> base.not_really_extern
    5400             :              == that was a lie, it is here  */
    5401             : 
    5402     3559547 :         bool is_external = t->decl_common.decl_flag_1;
    5403     3559547 :         if (!is_external)
    5404             :           /* decl_flag_1 is DECL_EXTERNAL. Things we emit here, might
    5405             :              well be external from the POV of an importer.  */
    5406             :           // FIXME: Do we need to know if this is a TEMPLATE_RESULT --
    5407             :           // a flag from the caller?
    5408     2969774 :           switch (code)
    5409             :             {
    5410             :             default:
    5411             :               break;
    5412             : 
    5413      182350 :             case VAR_DECL:
    5414      182350 :               if (TREE_PUBLIC (t)
    5415       32656 :                   && !(TREE_STATIC (t)
    5416       32656 :                        && DECL_FUNCTION_SCOPE_P (t)
    5417         115 :                        && DECL_DECLARED_INLINE_P (DECL_CONTEXT (t)))
    5418      214969 :                   && !DECL_VAR_DECLARED_INLINE_P (t))
    5419             :                 is_external = true;
    5420             :               break;
    5421             : 
    5422       30600 :             case FUNCTION_DECL:
    5423       30600 :               if (TREE_PUBLIC (t)
    5424       59810 :                   && !DECL_DECLARED_INLINE_P (t))
    5425             :                 is_external = true;
    5426             :               break;
    5427             :             }
    5428     3559547 :         WB (is_external);
    5429             :       }
    5430             : 
    5431     3559547 :       WB (t->decl_common.decl_flag_2);
    5432     3559547 :       WB (t->decl_common.decl_flag_3);
    5433     3559547 :       WB (t->decl_common.not_gimple_reg_flag);
    5434     3559547 :       WB (t->decl_common.decl_by_reference_flag);
    5435     3559547 :       WB (t->decl_common.decl_read_flag);
    5436     3559547 :       WB (t->decl_common.decl_nonshareable_flag);
    5437     3559547 :       WB (t->decl_common.decl_not_flexarray);
    5438             :     }
    5439             : 
    5440    14828573 :   if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
    5441             :     {
    5442     1667191 :       WB (t->decl_with_vis.defer_output);
    5443     1667191 :       WB (t->decl_with_vis.hard_register);
    5444     1667191 :       WB (t->decl_with_vis.common_flag);
    5445     1667191 :       WB (t->decl_with_vis.in_text_section);
    5446     1667191 :       WB (t->decl_with_vis.in_constant_pool);
    5447     1667191 :       WB (t->decl_with_vis.dllimport_flag);
    5448     1667191 :       WB (t->decl_with_vis.weak_flag);
    5449     1667191 :       WB (t->decl_with_vis.seen_in_bind_expr);
    5450     1667191 :       WB (t->decl_with_vis.comdat_flag);
    5451     1667191 :       WB (t->decl_with_vis.visibility_specified);
    5452     1667191 :       WB (t->decl_with_vis.init_priority_p);
    5453     1667191 :       WB (t->decl_with_vis.shadowed_for_var_p);
    5454     1667191 :       WB (t->decl_with_vis.cxx_constructor);
    5455     1667191 :       WB (t->decl_with_vis.cxx_destructor);
    5456     1667191 :       WB (t->decl_with_vis.final);
    5457     1667191 :       WB (t->decl_with_vis.regdecl_flag);
    5458             :     }
    5459             : 
    5460    14828573 :   if (CODE_CONTAINS_STRUCT (code, TS_FUNCTION_DECL))
    5461             :     {
    5462      497424 :       WB (t->function_decl.static_ctor_flag);
    5463      497424 :       WB (t->function_decl.static_dtor_flag);
    5464      497424 :       WB (t->function_decl.uninlinable);
    5465      497424 :       WB (t->function_decl.possibly_inlined);
    5466      497424 :       WB (t->function_decl.novops_flag);
    5467      497424 :       WB (t->function_decl.returns_twice_flag);
    5468      497424 :       WB (t->function_decl.malloc_flag);
    5469      497424 :       WB (t->function_decl.declared_inline_flag);
    5470      497424 :       WB (t->function_decl.no_inline_warning_flag);
    5471      497424 :       WB (t->function_decl.no_instrument_function_entry_exit);
    5472      497424 :       WB (t->function_decl.no_limit_stack);
    5473      497424 :       WB (t->function_decl.disregard_inline_limits);
    5474      497424 :       WB (t->function_decl.pure_flag);
    5475      497424 :       WB (t->function_decl.looping_const_or_pure_flag);
    5476             : 
    5477      497424 :       WB (t->function_decl.has_debug_args_flag);
    5478      497424 :       WB (t->function_decl.versioned_function);
    5479             : 
    5480             :       /* decl_type is a (misnamed) 2 bit discriminator.  */
    5481      497424 :       unsigned kind = t->function_decl.decl_type;
    5482      497424 :       WB ((kind >> 0) & 1);
    5483      497424 :       WB ((kind >> 1) & 1);
    5484             :     }
    5485             : #undef WB
    5486    14828573 : }
    5487             : 
    5488             : bool
    5489    17499603 : trees_in::core_bools (tree t)
    5490             : {
    5491             : #define RB(X) ((X) = b ())
    5492    17499603 :   tree_code code = TREE_CODE (t);
    5493             : 
    5494    17499603 :   RB (t->base.side_effects_flag);
    5495    17499603 :   RB (t->base.constant_flag);
    5496    17499603 :   RB (t->base.addressable_flag);
    5497    17499603 :   RB (t->base.volatile_flag);
    5498    17499603 :   RB (t->base.readonly_flag);
    5499             :   /* base.asm_written_flag is not streamed.  */
    5500    17499603 :   RB (t->base.nowarning_flag);
    5501             :   /* base.visited is not streamed.  */
    5502             :   /* base.used_flag is not streamed.  */
    5503    17499603 :   RB (t->base.nothrow_flag);
    5504    17499603 :   RB (t->base.static_flag);
    5505    17499603 :   if (TREE_CODE_CLASS (code) != tcc_type)
    5506    16751391 :     RB (t->base.public_flag);
    5507    17499603 :   RB (t->base.private_flag);
    5508    17499603 :   RB (t->base.protected_flag);
    5509    17499603 :   RB (t->base.deprecated_flag);
    5510    17499603 :   RB (t->base.default_def_flag);
    5511             : 
    5512    17499603 :   switch (code)
    5513             :     {
    5514             :     case CALL_EXPR:
    5515             :     case INTEGER_CST:
    5516             :     case SSA_NAME:
    5517             :     case TARGET_MEM_REF:
    5518             :     case TREE_VEC:
    5519             :       /* These use different base.u fields.  */
    5520             :       break;
    5521             : 
    5522    15251813 :     default:
    5523    15251813 :       RB (t->base.u.bits.lang_flag_0);
    5524    15251813 :       RB (t->base.u.bits.lang_flag_1);
    5525    15251813 :       RB (t->base.u.bits.lang_flag_2);
    5526    15251813 :       RB (t->base.u.bits.lang_flag_3);
    5527    15251813 :       RB (t->base.u.bits.lang_flag_4);
    5528    15251813 :       RB (t->base.u.bits.lang_flag_5);
    5529    15251813 :       RB (t->base.u.bits.lang_flag_6);
    5530    15251813 :       RB (t->base.u.bits.saturating_flag);
    5531    15251813 :       RB (t->base.u.bits.unsigned_flag);
    5532    15251813 :       RB (t->base.u.bits.packed_flag);
    5533    15251813 :       RB (t->base.u.bits.user_align);
    5534    15251813 :       RB (t->base.u.bits.nameless_flag);
    5535    15251813 :       RB (t->base.u.bits.atomic_flag);
    5536    15251813 :       break;
    5537             :     }
    5538             : 
    5539    17499603 :   if (CODE_CONTAINS_STRUCT (code, TS_TYPE_COMMON))
    5540             :     {
    5541      748212 :       RB (t->type_common.no_force_blk_flag);
    5542      748212 :       RB (t->type_common.needs_constructing_flag);
    5543      748212 :       RB (t->type_common.transparent_aggr_flag);
    5544      748212 :       RB (t->type_common.restrict_flag);
    5545      748212 :       RB (t->type_common.string_flag);
    5546      748212 :       RB (t->type_common.lang_flag_0);
    5547      748212 :       RB (t->type_common.lang_flag_1);
    5548      748212 :       RB (t->type_common.lang_flag_2);
    5549      748212 :       RB (t->type_common.lang_flag_3);
    5550      748212 :       RB (t->type_common.lang_flag_4);
    5551      748212 :       RB (t->type_common.lang_flag_5);
    5552      748212 :       RB (t->type_common.lang_flag_6);
    5553      748212 :       RB (t->type_common.typeless_storage);
    5554             :     }
    5555             : 
    5556    17499603 :   if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
    5557             :     {
    5558     4152298 :       RB (t->decl_common.nonlocal_flag);
    5559     4152298 :       RB (t->decl_common.virtual_flag);
    5560     4152298 :       RB (t->decl_common.ignored_flag);
    5561     4152298 :       RB (t->decl_common.abstract_flag);
    5562     4152298 :       RB (t->decl_common.artificial_flag);
    5563     4152298 :       RB (t->decl_common.preserve_flag);
    5564     4152298 :       RB (t->decl_common.debug_expr_is_from);
    5565     4152298 :       RB (t->decl_common.lang_flag_0);
    5566     4152298 :       RB (t->decl_common.lang_flag_1);
    5567     4152298 :       RB (t->decl_common.lang_flag_2);
    5568     4152298 :       RB (t->decl_common.lang_flag_3);
    5569     4152298 :       RB (t->decl_common.lang_flag_4);
    5570     4152298 :       RB (t->decl_common.lang_flag_5);
    5571     4152298 :       RB (t->decl_common.lang_flag_6);
    5572     4152298 :       RB (t->decl_common.lang_flag_7);
    5573     4152298 :       RB (t->decl_common.lang_flag_8);
    5574     4152298 :       RB (t->decl_common.decl_flag_0);
    5575     4152298 :       RB (t->decl_common.decl_flag_1);
    5576     4152298 :       RB (t->decl_common.decl_flag_2);
    5577     4152298 :       RB (t->decl_common.decl_flag_3);
    5578     4152298 :       RB (t->decl_common.not_gimple_reg_flag);
    5579     4152298 :       RB (t->decl_common.decl_by_reference_flag);
    5580     4152298 :       RB (t->decl_common.decl_read_flag);
    5581     4152298 :       RB (t->decl_common.decl_nonshareable_flag);
    5582     4152298 :       RB (t->decl_common.decl_not_flexarray);
    5583             :     }
    5584             : 
    5585    17499603 :   if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
    5586             :     {
    5587     1921835 :       RB (t->decl_with_vis.defer_output);
    5588     1921835 :       RB (t->decl_with_vis.hard_register);
    5589     1921835 :       RB (t->decl_with_vis.common_flag);
    5590     1921835 :       RB (t->decl_with_vis.in_text_section);
    5591     1921835 :       RB (t->decl_with_vis.in_constant_pool);
    5592     1921835 :       RB (t->decl_with_vis.dllimport_flag);
    5593     1921835 :       RB (t->decl_with_vis.weak_flag);
    5594     1921835 :       RB (t->decl_with_vis.seen_in_bind_expr);
    5595     1921835 :       RB (t->decl_with_vis.comdat_flag);
    5596     1921835 :       RB (t->decl_with_vis.visibility_specified);
    5597     1921835 :       RB (t->decl_with_vis.init_priority_p);
    5598     1921835 :       RB (t->decl_with_vis.shadowed_for_var_p);
    5599     1921835 :       RB (t->decl_with_vis.cxx_constructor);
    5600     1921835 :       RB (t->decl_with_vis.cxx_destructor);
    5601     1921835 :       RB (t->decl_with_vis.final);
    5602     1921835 :       RB (t->decl_with_vis.regdecl_flag);
    5603             :     }
    5604             : 
    5605    17499603 :   if (CODE_CONTAINS_STRUCT (code, TS_FUNCTION_DECL))
    5606             :     {
    5607      600440 :       RB (t->function_decl.static_ctor_flag);
    5608      600440 :       RB (t->function_decl.static_dtor_flag);
    5609      600440 :       RB (t->function_decl.uninlinable);
    5610      600440 :       RB (t->function_decl.possibly_inlined);
    5611      600440 :       RB (t->function_decl.novops_flag);
    5612      600440 :       RB (t->function_decl.returns_twice_flag);
    5613      600440 :       RB (t->function_decl.malloc_flag);
    5614      600440 :       RB (t->function_decl.declared_inline_flag);
    5615      600440 :       RB (t->function_decl.no_inline_warning_flag);
    5616      600440 :       RB (t->function_decl.no_instrument_function_entry_exit);
    5617      600440 :       RB (t->function_decl.no_limit_stack);
    5618      600440 :       RB (t->function_decl.disregard_inline_limits);
    5619      600440 :       RB (t->function_decl.pure_flag);
    5620      600440 :       RB (t->function_decl.looping_const_or_pure_flag);
    5621             :       
    5622      600440 :       RB (t->function_decl.has_debug_args_flag);
    5623      600440 :       RB (t->function_decl.versioned_function);
    5624             : 
    5625             :       /* decl_type is a (misnamed) 2 bit discriminator.  */
    5626      600440 :       unsigned kind = 0;
    5627      600440 :       kind |= unsigned (b ()) << 0;
    5628      600440 :       kind |= unsigned (b ()) << 1;
    5629      600440 :       t->function_decl.decl_type = function_decl_type (kind);
    5630             :     }
    5631             : #undef RB
    5632    17499603 :   return !get_overrun ();
    5633             : }
    5634             : 
    5635             : void
    5636     2317638 : trees_out::lang_decl_bools (tree t)
    5637             : {
    5638             : #define WB(X) (b (X))
    5639     2317638 :   const struct lang_decl *lang = DECL_LANG_SPECIFIC (t);
    5640             : 
    5641     2317638 :   WB (lang->u.base.language == lang_cplusplus);
    5642     2317638 :   WB ((lang->u.base.use_template >> 0) & 1);
    5643     2317638 :   WB ((lang->u.base.use_template >> 1) & 1);
    5644             :   /* Do not write lang->u.base.not_really_extern, importer will set
    5645             :      when reading the definition (if any).  */
    5646     2317638 :   WB (lang->u.base.initialized_in_class);
    5647     2317638 :   WB (lang->u.base.threadprivate_or_deleted_p);
    5648             :   /* Do not write lang->u.base.anticipated_p, it is a property of the
    5649             :      current TU.  */
    5650     2317638 :   WB (lang->u.base.friend_or_tls);
    5651     2317638 :   WB (lang->u.base.unknown_bound_p);
    5652             :   /* Do not write lang->u.base.odr_used, importer will recalculate if
    5653             :      they do ODR use this decl.  */
    5654     2317638 :   WB (lang->u.base.concept_p);
    5655     2317638 :   WB (lang->u.base.var_declared_inline_p);
    5656     2317638 :   WB (lang->u.base.dependent_init_p);
    5657             :   /* When building a header unit, everthing is marked as purview, (so
    5658             :      we know which decls to write).  But when we import them we do not
    5659             :      want to mark them as in module purview.  */
    5660     4627745 :   WB (lang->u.base.module_purview_p && !header_module_p ());
    5661     2317638 :   WB (lang->u.base.module_attach_p);
    5662     2317638 :   if (VAR_OR_FUNCTION_DECL_P (t))
    5663      650731 :     WB (lang->u.base.module_keyed_decls_p);
    5664     2317638 :   switch (lang->u.base.selector)
    5665             :     {
    5666           0 :     default:
    5667           0 :       gcc_unreachable ();
    5668             : 
    5669      497424 :     case lds_fn:  /* lang_decl_fn.  */
    5670      497424 :       WB (lang->u.fn.global_ctor_p);
    5671      497424 :       WB (lang->u.fn.global_dtor_p);
    5672      497424 :       WB (lang->u.fn.static_function);
    5673      497424 :       WB (lang->u.fn.pure_virtual);
    5674      497424 :       WB (lang->u.fn.defaulted_p);
    5675      497424 :       WB (lang->u.fn.has_in_charge_parm_p);
    5676      497424 :       WB (lang->u.fn.has_vtt_parm_p);
    5677             :       /* There shouldn't be a pending inline at this point.  */
    5678      497424 :       gcc_assert (!lang->u.fn.pending_inline_p);
    5679      497424 :       WB (lang->u.fn.nonconverting);
    5680      497424 :       WB (lang->u.fn.thunk_p);
    5681      497424 :       WB (lang->u.fn.this_thunk_p);
    5682             :       /* Do not stream lang->u.hidden_friend_p, it is a property of
    5683             :          the TU.  */
    5684      497424 :       WB (lang->u.fn.omp_declare_reduction_p);
    5685      497424 :       WB (lang->u.fn.has_dependent_explicit_spec_p);
    5686      497424 :       WB (lang->u.fn.immediate_fn_p);
    5687      497424 :       WB (lang->u.fn.maybe_deleted);
    5688      497424 :       goto lds_min;
    5689             : 
    5690         553 :     case lds_decomp:  /* lang_decl_decomp.  */
    5691             :       /* No bools.  */
    5692         553 :       goto lds_min;
    5693             : 
    5694             :     case lds_min:  /* lang_decl_min.  */
    5695     2317638 :     lds_min:
    5696             :       /* No bools.  */
    5697             :       break;
    5698             : 
    5699             :     case lds_ns:  /* lang_decl_ns.  */
    5700             :       /* No bools.  */
    5701             :       break;
    5702             : 
    5703             :     case lds_parm:  /* lang_decl_parm.  */
    5704             :       /* No bools.  */
    5705             :       break;
    5706             :     }
    5707             : #undef WB
    5708     2317638 : }
    5709             : 
    5710             : bool
    5711     2709977 : trees_in::lang_decl_bools (tree t)
    5712             : {
    5713             : #define RB(X) ((X) = b ())
    5714     2709977 :   struct lang_decl *lang = DECL_LANG_SPECIFIC (t);
    5715             : 
    5716     2709977 :   lang->u.base.language = b () ? lang_cplusplus : lang_c;
    5717     2709977 :   unsigned v;
    5718     2709977 :   v = b () << 0;
    5719     2709977 :   v |= b () << 1;
    5720     2709977 :   lang->u.base.use_template = v;
    5721             :   /* lang->u.base.not_really_extern is not streamed.  */
    5722     2709977 :   RB (lang->u.base.initialized_in_class);
    5723     2709977 :   RB (lang->u.base.threadprivate_or_deleted_p);
    5724             :   /* lang->u.base.anticipated_p is not streamed.  */
    5725     2709977 :   RB (lang->u.base.friend_or_tls);
    5726     2709977 :   RB (lang->u.base.unknown_bound_p);
    5727             :   /* lang->u.base.odr_used is not streamed.  */
    5728     2709977 :   RB (lang->u.base.concept_p);
    5729     2709977 :   RB (lang->u.base.var_declared_inline_p);
    5730     2709977 :   RB (lang->u.base.dependent_init_p);
    5731     2709977 :   RB (lang->u.base.module_purview_p);
    5732     2709977 :   RB (lang->u.base.module_attach_p);
    5733     2709977 :   if (VAR_OR_FUNCTION_DECL_P (t))
    5734      799746 :     RB (lang->u.base.module_keyed_decls_p);
    5735     2709977 :   switch (lang->u.base.selector)
    5736             :     {
    5737           0 :     default:
    5738           0 :       gcc_unreachable ();
    5739             : 
    5740      600440 :     case lds_fn:  /* lang_decl_fn.  */
    5741      600440 :       RB (lang->u.fn.global_ctor_p);
    5742      600440 :       RB (lang->u.fn.global_dtor_p);
    5743      600440 :       RB (lang->u.fn.static_function);
    5744      600440 :       RB (lang->u.fn.pure_virtual);
    5745      600440 :       RB (lang->u.fn.defaulted_p);
    5746      600440 :       RB (lang->u.fn.has_in_charge_parm_p);
    5747      600440 :       RB (lang->u.fn.has_vtt_parm_p);
    5748      600440 :       RB (lang->u.fn.nonconverting);
    5749      600440 :       RB (lang->u.fn.thunk_p);
    5750      600440 :       RB (lang->u.fn.this_thunk_p);
    5751             :       /* lang->u.fn.hidden_friend_p is not streamed.  */
    5752      600440 :       RB (lang->u.fn.omp_declare_reduction_p);
    5753      600440 :       RB (lang->u.fn.has_dependent_explicit_spec_p);
    5754      600440 :       RB (lang->u.fn.immediate_fn_p);
    5755      600440 :       RB (lang->u.fn.maybe_deleted);
    5756      600440 :       goto lds_min;
    5757             : 
    5758         781 :     case lds_decomp:  /* lang_decl_decomp.  */
    5759             :       /* No bools.  */
    5760         781 :       goto lds_min;
    5761             : 
    5762             :     case lds_min:  /* lang_decl_min.  */
    5763     2709977 :     lds_min:
    5764             :       /* No bools.  */
    5765             :       break;
    5766             : 
    5767             :     case lds_ns:  /* lang_decl_ns.  */
    5768             :       /* No bools.  */
    5769             :       break;
    5770             : 
    5771             :     case lds_parm:  /* lang_decl_parm.  */
    5772             :       /* No bools.  */
    5773             :       break;
    5774             :     }
    5775             : #undef RB
    5776     2709977 :   return !get_overrun ();
    5777             : }
    5778             : 
    5779             : void
    5780      192966 : trees_out::lang_type_bools (tree t)
    5781             : {
    5782             : #define WB(X) (b (X))
    5783      192966 :   const struct lang_type *lang = TYPE_LANG_SPECIFIC (t);
    5784             : 
    5785      192966 :   WB (lang->has_type_conversion);
    5786      192966 :   WB (lang->has_copy_ctor);
    5787      192966 :   WB (lang->has_default_ctor);
    5788      192966 :   WB (lang->const_needs_init);
    5789      192966 :   WB (lang->ref_needs_init);
    5790      192966 :   WB (lang->has_const_copy_assign);
    5791      192966 :   WB ((lang->use_template >> 0) & 1);
    5792      192966 :   WB ((lang->use_template >> 1) & 1);
    5793             : 
    5794      192966 :   WB (lang->has_mutable);
    5795      192966 :   WB (lang->com_interface);
    5796      192966 :   WB (lang->non_pod_class);
    5797      192966 :   WB (lang->nearly_empty_p);
    5798      192966 :   WB (lang->user_align);
    5799      192966 :   WB (lang->has_copy_assign);
    5800      192966 :   WB (lang->has_new);
    5801      192966 :   WB (lang->has_array_new);
    5802             : 
    5803      192966 :   WB ((lang->gets_delete >> 0) & 1);
    5804      192966 :   WB ((lang->gets_delete >> 1) & 1);
    5805             :   // Interfaceness is recalculated upon reading.  May have to revisit?
    5806             :   // How do dllexport and dllimport interact across a module?
    5807             :   // lang->interface_only
    5808             :   // lang->interface_unknown
    5809      192966 :   WB (lang->contains_empty_class_p);
    5810      192966 :   WB (lang->anon_aggr);
    5811      192966 :   WB (lang->non_zero_init);
    5812      192966 :   WB (lang->empty_p);
    5813             : 
    5814      192966 :   WB (lang->vec_new_uses_cookie);
    5815      192966 :   WB (lang->declared_class);
    5816      192966 :   WB (lang->diamond_shaped);
    5817      192966 :   WB (lang->repeated_base);
    5818      192966 :   gcc_assert (!lang->being_defined);
    5819             :   // lang->debug_requested
    5820      192966 :   WB (lang->fields_readonly);
    5821      192966 :   WB (lang->ptrmemfunc_flag);
    5822             : 
    5823      192966 :   WB (lang->lazy_default_ctor);
    5824      192966 :   WB (lang->lazy_copy_ctor);
    5825      192966 :   WB (lang->lazy_copy_assign);
    5826      192966 :   WB (lang->lazy_destructor);
    5827      192966 :   WB (lang->has_const_copy_ctor);
    5828      192966 :   WB (lang->has_complex_copy_ctor);
    5829      192966 :   WB (lang->has_complex_copy_assign);
    5830      192966 :   WB (lang->non_aggregate);
    5831             : 
    5832      192966 :   WB (lang->has_complex_dflt);
    5833      192966 :   WB (lang->has_list_ctor);
    5834      192966 :   WB (lang->non_std_layout);
    5835      192966 :   WB (lang->is_literal);
    5836      192966 :   WB (lang->lazy_move_ctor);
    5837      192966 :   WB (lang->lazy_move_assign);
    5838      192966 :   WB (lang->has_complex_move_ctor);
    5839      192966 :   WB (lang->has_complex_move_assign);
    5840             : 
    5841      192966 :   WB (lang->has_constexpr_ctor);
    5842      192966 :   WB (lang->unique_obj_representations);
    5843      192966 :   WB (lang->unique_obj_representations_set);
    5844             : #undef WB
    5845      192966 : }
    5846             : 
    5847             : bool
    5848      210508 : trees_in::lang_type_bools (tree t)
    5849             : {
    5850             : #define RB(X) ((X) = b ())
    5851      210508 :   struct lang_type *lang = TYPE_LANG_SPECIFIC (t);
    5852             : 
    5853      210508 :   RB (lang->has_type_conversion);
    5854      210508 :   RB (lang->has_copy_ctor);
    5855      210508 :   RB (lang->has_default_ctor);
    5856      210508 :   RB (lang->const_needs_init);
    5857      210508 :   RB (lang->ref_needs_init);
    5858      210508 :   RB (lang->has_const_copy_assign);
    5859      210508 :   unsigned v;
    5860      210508 :   v = b () << 0;
    5861      210508 :   v |= b () << 1;
    5862      210508 :   lang->use_template = v;
    5863             : 
    5864      210508 :   RB (lang->has_mutable);
    5865      210508 :   RB (lang->com_interface);
    5866      210508 :   RB (lang->non_pod_class);
    5867      210508 :   RB (lang->nearly_empty_p);
    5868      210508 :   RB (lang->user_align);
    5869      210508 :   RB (lang->has_copy_assign);
    5870      210508 :   RB (lang->has_new);
    5871      210508 :   RB (lang->has_array_new);
    5872             : 
    5873      210508 :   v = b () << 0;
    5874      210508 :   v |= b () << 1;
    5875      210508 :   lang->gets_delete = v;
    5876             :   // lang->interface_only
    5877             :   // lang->interface_unknown
    5878      210508 :   lang->interface_unknown = true; // Redetermine interface
    5879      210508 :   RB (lang->contains_empty_class_p);
    5880      210508 :   RB (lang->anon_aggr);
    5881      210508 :   RB (lang->non_zero_init);
    5882      210508 :   RB (lang->empty_p);
    5883             : 
    5884      210508 :   RB (lang->vec_new_uses_cookie);
    5885      210508 :   RB (lang->declared_class);
    5886      210508 :   RB (lang->diamond_shaped);
    5887      210508 :   RB (lang->repeated_base);
    5888      210508 :   gcc_assert (!lang->being_defined);
    5889      210508 :   gcc_assert (!lang->debug_requested);
    5890      210508 :   RB (lang->fields_readonly);
    5891      210508 :   RB (lang->ptrmemfunc_flag);
    5892             : 
    5893      210508 :   RB (lang->lazy_default_ctor);
    5894      210508 :   RB (lang->lazy_copy_ctor);
    5895      210508 :   RB (lang->lazy_copy_assign);
    5896      210508 :   RB (lang->lazy_destructor);
    5897      210508 :   RB (lang->has_const_copy_ctor);
    5898      210508 :   RB (lang->has_complex_copy_ctor);
    5899      210508 :   RB (lang->has_complex_copy_assign);
    5900      210508 :   RB (lang->non_aggregate);
    5901             : 
    5902      210508 :   RB (lang->has_complex_dflt);
    5903      210508 :   RB (lang->has_list_ctor);
    5904      210508 :   RB (lang->non_std_layout);
    5905      210508 :   RB (lang->is_literal);
    5906      210508 :   RB (lang->lazy_move_ctor);
    5907      210508 :   RB (lang->lazy_move_assign);
    5908      210508 :   RB (lang->has_complex_move_ctor);
    5909      210508 :   RB (lang->has_complex_move_assign);
    5910             : 
    5911      210508 :   RB (lang->has_constexpr_ctor);
    5912      210508 :   RB (lang->unique_obj_representations);
    5913      210508 :   RB (lang->unique_obj_representations_set);
    5914             : #undef RB
    5915      210508 :   return !get_overrun ();
    5916             : }
    5917             : 
    5918             : /* Read & write the core values and pointers.  */
    5919             : 
    5920             : void
    5921    38741626 : trees_out::core_vals (tree t)
    5922             : {
    5923             : #define WU(X) (u (X))
    5924             : #define WT(X) (tree_node (X))
    5925    38741626 :   tree_code code = TREE_CODE (t);
    5926             : 
    5927             :   /* First by shape of the tree.  */
    5928             : 
    5929    38741626 :   if (CODE_CONTAINS_STRUCT (code, TS_DECL_MINIMAL))
    5930             :     {
    5931             :       /* Write this early, for better log information.  */
    5932     9273397 :       WT (t->decl_minimal.name);
    5933     9273397 :       if (!DECL_TEMPLATE_PARM_P (t))
    5934     7138719 :         WT (t->decl_minimal.context);
    5935             : 
    5936     9273397 :       if (state)
    5937     7577619 :         state->write_location (*this, t->decl_minimal.locus);
    5938             :     }
    5939             : 
    5940    38741626 :   if (CODE_CONTAINS_STRUCT (code, TS_TYPE_COMMON))
    5941             :     {
    5942             :       /* The only types we write also have TYPE_NON_COMMON.  */
    5943     2350722 :       gcc_checking_assert (CODE_CONTAINS_STRUCT (code, TS_TYPE_NON_COMMON));
    5944             : 
    5945             :       /* We only stream the main variant.  */
    5946     2350722 :       gcc_checking_assert (TYPE_MAIN_VARIANT (t) == t);
    5947             : 
    5948             :       /* Stream the name & context first, for better log information  */
    5949     2350722 :       WT (t->type_common.name);
    5950     2350722 :       WT (t->type_common.context);
    5951             : 
    5952             :       /* By construction we want to make sure we have the canonical
    5953             :          and main variants already in the type table, so emit them
    5954             :          now.  */
    5955     2350722 :       WT (t->type_common.main_variant);
    5956             : 
    5957     2350722 :       tree canonical = t->type_common.canonical;
    5958     2350722 :       if (canonical && DECL_TEMPLATE_PARM_P (TYPE_NAME (t)))
    5959             :         /* We do not want to wander into different templates.
    5960             :            Reconstructed on stream in.  */
    5961             :         canonical = t;
    5962     2350722 :       WT (canonical);
    5963             : 
    5964             :       /* type_common.next_variant is internally manipulated.  */
    5965             :       /* type_common.pointer_to, type_common.reference_to.  */
    5966             : 
    5967     2350722 :       if (streaming_p ())
    5968             :         {
    5969      651303 :           WU (t->type_common.precision);
    5970      651303 :           WU (t->type_common.contains_placeholder_bits);
    5971      651303 :           WU (t->type_common.mode);
    5972      651303 :           WU (t->type_common.align);
    5973             :         }
    5974             : 
    5975     2350722 :       if (!RECORD_OR_UNION_CODE_P (code))
    5976             :         {
    5977     1912947 :           WT (t->type_common.size);
    5978     1912947 :           WT (t->type_common.size_unit);
    5979             :         }
    5980     2350722 :       WT (t->type_common.attributes);
    5981             : 
    5982     2350722 :       WT (t->type_common.common.chain); /* TYPE_STUB_DECL.  */
    5983             :     }
    5984             : 
    5985    38741626 :   if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
    5986             :     {
    5987     9273397 :       if (streaming_p ())
    5988             :         {
    5989     3559547 :           WU (t->decl_common.mode);
    5990     3559547 :           WU (t->decl_common.off_align);
    5991     3559547 :           WU (t->decl_common.align);
    5992             :         }
    5993             : 
    5994             :       /* For templates these hold instantiation (partial and/or
    5995             :          specialization) information.  */
    5996     9273397 :       if (code != TEMPLATE_DECL)
    5997             :         {
    5998     8534014 :           WT (t->decl_common.size);
    5999     8534014 :           WT (t->decl_common.size_unit);
    6000             :         }
    6001             : 
    6002     9273397 :       WT (t->decl_common.attributes);
    6003             :       // FIXME: Does this introduce cross-decl links?  For instance
    6004             :       // from instantiation to the template.  If so, we'll need more
    6005             :       // deduplication logic.  I think we'll need to walk the blocks
    6006             :       // of the owning function_decl's abstract origin in tandem, to
    6007             :       // generate the locating data needed?
    6008     9273397 :       WT (t->decl_common.abstract_origin);
    6009             :     }
    6010             : 
    6011    38741626 :   if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
    6012             :     {
    6013     4389890 :       WT (t->decl_with_vis.assembler_name);
    6014     4389890 :       if (streaming_p ())
    6015     1667191 :         WU (t->decl_with_vis.visibility);
    6016             :     }
    6017             : 
    6018    38741626 :   if (CODE_CONTAINS_STRUCT (code, TS_TYPE_NON_COMMON))
    6019             :     {
    6020     2350722 :       if (code == ENUMERAL_TYPE)
    6021             :         {
    6022             :           /* These fields get set even for opaque enums that lack a
    6023             :              definition, so we stream them directly for each ENUMERAL_TYPE.
    6024             :              We stream TYPE_VALUES as part of the definition.  */
    6025        9804 :           WT (t->type_non_common.maxval);
    6026        9804 :           WT (t->type_non_common.minval);
    6027             :         }
    6028             :       /* Records and unions hold FIELDS, VFIELD & BINFO on these
    6029             :          things.  */
    6030     2340918 :       else if (!RECORD_OR_UNION_CODE_P (code))
    6031             :         {
    6032             :           // FIXME: These are from tpl_parm_value's 'type' writing.
    6033             :           // Perhaps it should just be doing them directly?
    6034     1903143 :           gcc_checking_assert (code == TEMPLATE_TYPE_PARM
    6035             :                                || code == TEMPLATE_TEMPLATE_PARM
    6036             :                                || code == BOUND_TEMPLATE_TEMPLATE_PARM);
    6037     1903143 :           gcc_checking_assert (!TYPE_CACHED_VALUES_P (t));
    6038     1903143 :           WT (t->type_non_common.values);
    6039     1903143 :           WT (t->type_non_common.maxval);
    6040     1903143 :           WT (t->type_non_common.minval);
    6041             :         }
    6042             : 
    6043     2350722 :       WT (t->type_non_common.lang_1);
    6044             :     }
    6045             : 
    6046    38741626 :   if (CODE_CONTAINS_STRUCT (code, TS_EXP))
    6047             :     {
    6048    10504832 :       if (state)
    6049    10320476 :         state->write_location (*this, t->exp.locus);
    6050             : 
    6051             :       /* Walk in forward order, as (for instance) REQUIRES_EXPR has a
    6052             :          bunch of unscoped parms on its first operand.  It's safer to
    6053             :          create those in order.  */
    6054    10504832 :       bool vl = TREE_CODE_CLASS (code) == tcc_vl_exp;
    6055    38434291 :       for (unsigned limit = (vl ? VL_EXP_OPERAND_LENGTH (t)
    6056     9403434 :                              : TREE_OPERAND_LENGTH (t)),
    6057    29030857 :              ix = unsigned (vl); ix != limit; ix++)
    6058    18526025 :         WT (TREE_OPERAND (t, ix));
    6059             :     }
    6060             :   else
    6061             :     /* The CODE_CONTAINS tables were inaccurate when I started.  */
    6062    28236794 :     gcc_checking_assert (TREE_CODE_CLASS (code) != tcc_expression
    6063             :                          && TREE_CODE_CLASS (code) != tcc_binary
    6064             :                          && TREE_CODE_CLASS (code) != tcc_unary
    6065             :                          && TREE_CODE_CLASS (code) != tcc_reference
    6066             :                          && TREE_CODE_CLASS (code) != tcc_comparison
    6067             :                          && TREE_CODE_CLASS (code) != tcc_statement
    6068             :                          && TREE_CODE_CLASS (code) != tcc_vl_exp);
    6069             : 
    6070             :   /* Then by CODE.  Special cases and/or 1:1 tree shape
    6071             :      correspondance. */
    6072    38741626 :   switch (code)
    6073             :     {
    6074             :     default:
    6075             :       break;
    6076             : 
    6077           0 :     case ARGUMENT_PACK_SELECT:  /* Transient during instantiation.  */
    6078           0 :     case DEFERRED_PARSE:        /* Expanded upon completion of
    6079             :                                    outermost class.  */
    6080           0 :     case IDENTIFIER_NODE:       /* Streamed specially.  */
    6081           0 :     case BINDING_VECTOR:                /* Only in namespace-scope symbol
    6082             :                                    table.  */
    6083           0 :     case SSA_NAME:
    6084           0 :     case TRANSLATION_UNIT_DECL: /* There is only one, it is a
    6085             :                                    global_tree.  */
    6086           0 :     case USERDEF_LITERAL:       /* Expanded during parsing.  */
    6087           0 :       gcc_unreachable (); /* Should never meet.  */
    6088             : 
    6089             :       /* Constants.  */
    6090          18 :     case COMPLEX_CST:
    6091          18 :       WT (TREE_REALPART (t));
    6092          18 :       WT (TREE_IMAGPART (t));
    6093          18 :       break;
    6094             : 
    6095           0 :     case FIXED_CST:
    6096           0 :       gcc_unreachable (); /* Not supported in C++.  */
    6097             : 
    6098     3557227 :     case INTEGER_CST:
    6099     3557227 :       if (streaming_p ())
    6100             :         {
    6101      535834 :           unsigned num = TREE_INT_CST_EXT_NUNITS (t);
    6102     1073798 :           for (unsigned ix = 0; ix != num; ix++)
    6103      537964 :             wu (TREE_INT_CST_ELT (t, ix));
    6104             :         }
    6105             :       break;
    6106             : 
    6107           0 :     case POLY_INT_CST:
    6108           0 :       gcc_unreachable (); /* Not supported in C++.  */
    6109             : 
    6110       42706 :     case REAL_CST:
    6111       42706 :       if (streaming_p ())
    6112       21308 :         buf (TREE_REAL_CST_PTR (t), sizeof (real_value));
    6113             :       break;
    6114             : 
    6115             :     case STRING_CST:
    6116             :       /* Streamed during start.  */
    6117             :       break;
    6118             : 
    6119          36 :     case VECTOR_CST:
    6120         102 :       for (unsigned ix = vector_cst_encoded_nelts (t); ix--;)
    6121          66 :         WT (VECTOR_CST_ENCODED_ELT (t, ix));
    6122             :       break;
    6123             : 
    6124             :       /* Decls.  */
    6125      392498 :     case VAR_DECL:
    6126      392498 :       if (DECL_CONTEXT (t)
    6127      392498 :           && TREE_CODE (DECL_CONTEXT (t)) != FUNCTION_DECL)
    6128             :         break;
    6129             :       /* FALLTHROUGH  */
    6130             : 
    6131     4106427 :     case RESULT_DECL:
    6132     4106427 :     case PARM_DECL:
    6133     4106427 :       if (DECL_HAS_VALUE_EXPR_P (t))
    6134       13536 :         WT (DECL_VALUE_EXPR (t));
    6135             :       /* FALLTHROUGH  */
    6136             : 
    6137     4288226 :     case CONST_DECL:
    6138     4288226 :     case IMPORTED_DECL:
    6139     4288226 :       WT (t->decl_common.initial);
    6140     4288226 :       break;
    6141             : 
    6142      118628 :     case FIELD_DECL:
    6143      118628 :       WT (t->field_decl.offset);
    6144      118628 :       WT (t->field_decl.bit_field_type);
    6145      118628 :       WT (t->field_decl.qualifier); /* bitfield unit.  */
    6146      118628 :       WT (t->field_decl.bit_offset);
    6147      118628 :       WT (t->field_decl.fcontext);
    6148      118628 :       WT (t->decl_common.initial);
    6149      118628 :       break;
    6150             : 
    6151       29362 :     case LABEL_DECL:
    6152       29362 :       if (streaming_p ())
    6153             :         {
    6154       14681 :           WU (t->label_decl.label_decl_uid);
    6155       14681 :           WU (t->label_decl.eh_landing_pad_nr);
    6156             :         }
    6157             :       break;
    6158             : 
    6159      994857 :     case FUNCTION_DECL:
    6160      994857 :       if (streaming_p ())
    6161             :         {
    6162             :           /* Builtins can be streamed by value when a header declares
    6163             :              them.  */
    6164      497424 :           WU (DECL_BUILT_IN_CLASS (t));
    6165      497424 :           if (DECL_BUILT_IN_CLASS (t) != NOT_BUILT_IN)
    6166       11427 :             WU (DECL_UNCHECKED_FUNCTION_CODE (t));
    6167             :         }
    6168             : 
    6169      994857 :       WT (t->function_decl.personality);
    6170      994857 :       WT (t->function_decl.function_specific_target);
    6171      994857 :       WT (t->function_decl.function_specific_optimization);
    6172      994857 :       WT (t->function_decl.vindex);
    6173             : 
    6174      994857 :       if (DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (t))
    6175        2220 :         WT (lookup_explicit_specifier (t));
    6176             :       break;
    6177             : 
    6178      105770 :     case USING_DECL:
    6179             :       /* USING_DECL_DECLS  */
    6180      105770 :       WT (t->decl_common.initial);
    6181             :       /* FALLTHROUGH  */
    6182             : 
    6183     3002387 :     case TYPE_DECL:
    6184             :       /* USING_DECL: USING_DECL_SCOPE  */
    6185             :       /* TYPE_DECL: DECL_ORIGINAL_TYPE */
    6186     3002387 :       WT (t->decl_non_common.result);
    6187     3002387 :       break;
    6188             : 
    6189             :       /* Miscellaneous common nodes.  */
    6190      580050 :     case BLOCK:
    6191      580050 :       if (state)
    6192             :         {
    6193      580050 :           state->write_location (*this, t->block.locus);
    6194      580050 :           state->write_location (*this, t->block.end_locus);
    6195             :         }
    6196             : 
    6197             :       /* DECL_LOCAL_DECL_P decls are first encountered here and
    6198             :          streamed by value.  */
    6199      580050 :       chained_decls (t->block.vars);
    6200             :       /* nonlocalized_vars is a middle-end thing.  */
    6201      580050 :       WT (t->block.subblocks);
    6202      580050 :       WT (t->block.supercontext);
    6203             :       // FIXME: As for decl's abstract_origin, does this introduce crosslinks?
    6204      580050 :       WT (t->block.abstract_origin);
    6205             :       /* fragment_origin, fragment_chain are middle-end things.  */
    6206      580050 :       WT (t->block.chain);
    6207             :       /* nonlocalized_vars, block_num & die are middle endy/debug
    6208             :          things.  */
    6209      580050 :       break;
    6210             : 
    6211     1077086 :     case CALL_EXPR:
    6212     1077086 :       if (streaming_p ())
    6213      519770 :         WU (t->base.u.ifn);
    6214             :       break;
    6215             : 
    6216       83316 :     case CONSTRUCTOR:
    6217       83316 :       {
    6218       83316 :         unsigned len = vec_safe_length (t->constructor.elts);
    6219       83316 :         if (streaming_p ())
    6220       41120 :           WU (len);
    6221       83316 :         if (len)
    6222      126964 :           for (unsigned ix = 0; ix != len; ix++)
    6223             :             {
    6224      102340 :               const constructor_elt &elt = (*t->constructor.elts)[ix];
    6225             : 
    6226      102340 :               WT (elt.index);
    6227      102340 :               WT (elt.value);
    6228             :             }
    6229             :       }
    6230             :       break;
    6231             : 
    6232           6 :     case OMP_CLAUSE:
    6233           6 :       {
    6234             :         /* The ompcode is serialized in start.  */
    6235           6 :         if (streaming_p ())
    6236           3 :           WU (t->omp_clause.subcode.map_kind);
    6237           6 :         if (state)
    6238           6 :           state->write_location (*this, t->omp_clause.locus);
    6239             : 
    6240           6 :         unsigned len = omp_clause_num_ops[OMP_CLAUSE_CODE (t)];
    6241          18 :         for (unsigned ix = 0; ix != len; ix++)
    6242          12 :           WT (t->omp_clause.ops[ix]);
    6243             :       }
    6244             :       break;
    6245             : 
    6246      379534 :     case STATEMENT_LIST:
    6247     1490228 :       for (tree stmt : tsi_range (t))
    6248     1110694 :         if (stmt)
    6249     1110694 :           WT (stmt);
    6250      379534 :       WT (NULL_TREE);
    6251      379534 :       break;
    6252             : 
    6253           0 :     case OPTIMIZATION_NODE:
    6254           0 :     case TARGET_OPTION_NODE:
    6255             :       // FIXME: Our representation for these two nodes is a cache of
    6256             :       // the resulting set of options.  Not a record of the options
    6257             :       // that got changed by a particular attribute or pragma.  Should
    6258             :       // we record that, or should we record the diff from the command
    6259             :       // line options?  The latter seems the right behaviour, but is
    6260             :       // (a) harder, and I guess could introduce strangeness if the
    6261             :       // importer has set some incompatible set of optimization flags?
    6262           0 :       gcc_unreachable ();
    6263      267357 :       break;
    6264             : 
    6265      267357 :     case TREE_BINFO:
    6266      267357 :       {
    6267      267357 :         WT (t->binfo.common.chain);
    6268      267357 :         WT (t->binfo.offset);
    6269      267357 :         WT (t->binfo.inheritance);
    6270      267357 :         WT (t->binfo.vptr_field);
    6271             : 
    6272      267357 :         WT (t->binfo.vtable);
    6273      267357 :         WT (t->binfo.virtuals);
    6274      267357 :         WT (t->binfo.vtt_subvtt);
    6275      267357 :         WT (t->binfo.vtt_vptr);
    6276             : 
    6277      267357 :         tree_vec (BINFO_BASE_ACCESSES (t));
    6278      267357 :         unsigned num = vec_safe_length (BINFO_BASE_ACCESSES (t));
    6279      351605 :         for (unsigned ix = 0; ix != num; ix++)
    6280       84248 :           WT (BINFO_BASE_BINFO (t, ix));
    6281             :       }
    6282             :       break;
    6283             : 
    6284     3142129 :     case TREE_LIST:
    6285     3142129 :       WT (t->list.purpose);
    6286     3142129 :       WT (t->list.value);
    6287     3142129 :       WT (t->list.common.chain);
    6288     3142129 :       break;
    6289             : 
    6290     2670996 :     case TREE_VEC:
    6291     7559650 :       for (unsigned ix = TREE_VEC_LENGTH (t); ix--;)
    6292     4888654 :         WT (TREE_VEC_ELT (t, ix));
    6293             :       /* We stash NON_DEFAULT_TEMPLATE_ARGS_COUNT on TREE_CHAIN!  */
    6294     2670996 :       gcc_checking_assert (!t->type_common.common.chain
    6295             :                            || (TREE_CODE (t->type_common.common.chain)
    6296             :                                == INTEGER_CST));
    6297     2670996 :       WT (t->type_common.common.chain);
    6298     2670996 :       break;
    6299             : 
    6300             :       /* C++-specific nodes ...  */
    6301      187802 :     case BASELINK:
    6302      187802 :       WT (((lang_tree_node *)t)->baselink.binfo);
    6303      187802 :       WT (((lang_tree_node *)t)->baselink.functions);
    6304      187802 :       WT (((lang_tree_node *)t)->baselink.access_binfo);
    6305      187802 :       break;
    6306             : 
    6307       43268 :     case CONSTRAINT_INFO:
    6308       43268 :       WT (((lang_tree_node *)t)->constraint_info.template_reqs);
    6309       43268 :       WT (((lang_tree_node *)t)->constraint_info.declarator_reqs);
    6310       43268 :       WT (((lang_tree_node *)t)->constraint_info.associated_constr);
    6311       43268 :       break;
    6312             : 
    6313       12102 :     case DEFERRED_NOEXCEPT:
    6314       12102 :       WT (((lang_tree_node *)t)->deferred_noexcept.pattern);
    6315       12102 :       WT (((lang_tree_node *)t)->deferred_noexcept.args);
    6316       12102 :       break;
    6317             : 
    6318        7196 :     case LAMBDA_EXPR:
    6319        7196 :       WT (((lang_tree_node *)t)->lambda_expression.capture_list);
    6320        7196 :       WT (((lang_tree_node *)t)->lambda_expression.this_capture);
    6321        7196 :       WT (((lang_tree_node *)t)->lambda_expression.extra_scope);
    6322             :       /* pending_proxies is a parse-time thing.  */
    6323        7196 :       gcc_assert (!((lang_tree_node *)t)->lambda_expression.pending_proxies);
    6324        7196 :       if (state)
    6325        7196 :         state->write_location
    6326        7196 :           (*this, ((lang_tree_node *)t)->lambda_expression.locus);
    6327        7196 :       if (streaming_p ())
    6328             :         {
    6329        2328 :           WU (((lang_tree_node *)t)->lambda_expression.default_capture_mode);
    6330        2328 :           WU (((lang_tree_node *)t)->lambda_expression.discriminator_scope);
    6331        2328 :           WU (((lang_tree_node *)t)->lambda_expression.discriminator_sig);
    6332             :         }
    6333             :       break;
    6334             : 
    6335     1836124 :     case OVERLOAD:
    6336     1836124 :       WT (((lang_tree_node *)t)->overload.function);
    6337     1836124 :       WT (t->common.chain);
    6338     1836124 :       break;
    6339             :       
    6340           0 :     case PTRMEM_CST:
    6341           0 :       WT (((lang_tree_node *)t)->ptrmem.member);
    6342           0 :       break;
    6343             : 
    6344       15460 :     case STATIC_ASSERT:
    6345       15460 :       WT (((lang_tree_node *)t)->static_assertion.condition);
    6346       15460 :       WT (((lang_tree_node *)t)->static_assertion.message);
    6347       15460 :       if (state)
    6348       15460 :         state->write_location
    6349       15460 :           (*this, ((lang_tree_node *)t)->static_assertion.location);
    6350             :       break;
    6351             : 
    6352      739383 :     case TEMPLATE_DECL:
    6353             :       /* Streamed with the template_decl node itself.  */
    6354      739383 :       gcc_checking_assert
    6355             :         (TREE_VISITED (((lang_tree_node *)t)->template_decl.arguments));
    6356      739383 :       gcc_checking_assert
    6357             :         (TREE_VISITED (((lang_tree_node *)t)->template_decl.result)
    6358             :          || dep_hash->find_dependency (t)->is_alias_tmpl_inst ());
    6359      739383 :       if (DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (t))
    6360        9070 :         WT (DECL_CHAIN (t));
    6361             :       break;
    6362             : 
    6363     1679392 :     case TEMPLATE_INFO:
    6364     1679392 :       {
    6365     1679392 :         WT (((lang_tree_node *)t)->template_info.tmpl);
    6366     1679392 :         WT (((lang_tree_node *)t)->template_info.args);
    6367     1679392 :         WT (((lang_tree_node *)t)->template_info.partial);
    6368             : 
    6369     1679392 :         const auto *ac = (((lang_tree_node *)t)
    6370             :                           ->template_info.deferred_access_checks);
    6371     1679392 :         unsigned len = vec_safe_length (ac);
    6372     1679392 :         if (streaming_p ())
    6373      837407 :           u (len);
    6374     1679392 :         if (len)
    6375             :           {
    6376           0 :             for (unsigned ix = 0; ix != len; ix++)
    6377             :               {
    6378           0 :                 const auto &m = (*ac)[ix];
    6379           0 :                 WT (m.binfo);
    6380           0 :                 WT (m.decl);
    6381           0 :                 WT (m.diag_decl);
    6382           0 :                 if (state)
    6383           0 :                   state->write_location (*this, m.loc);
    6384             :               }
    6385             :           }
    6386             :       }
    6387             :       break;
    6388             : 
    6389     2049912 :     case TEMPLATE_PARM_INDEX:
    6390     2049912 :       if (streaming_p ())
    6391             :         {
    6392      460612 :           WU (((lang_tree_node *)t)->tpi.index);
    6393      460612 :           WU (((lang_tree_node *)t)->tpi.level);
    6394      460612 :           WU (((lang_tree_node *)t)->tpi.orig_level);
    6395             :         }
    6396     2049912 :       WT (((lang_tree_node *)t)->tpi.decl);
    6397             :       /* TEMPLATE_PARM_DESCENDANTS (AKA TREE_CHAIN) is an internal
    6398             :          cache, do not stream.  */
    6399     2049912 :       break;
    6400             :       
    6401       24815 :     case TRAIT_EXPR:
    6402       24815 :       WT (((lang_tree_node *)t)->trait_expression.type1);
    6403       24815 :       WT (((lang_tree_node *)t)->trait_expression.type2);
    6404       24815 :       if (streaming_p ())
    6405        9567 :         WU (((lang_tree_node *)t)->trait_expression.kind);
    6406             :       break;
    6407             :     }
    6408             : 
    6409    38741626 :   if (CODE_CONTAINS_STRUCT (code, TS_TYPED))
    6410             :     {
    6411             :       /* We want to stream the type of a expression-like nodes /after/
    6412             :          we've streamed the operands.  The type often contains (bits
    6413             :          of the) types of the operands, and with things like decltype
    6414             :          and noexcept in play, we really want to stream the decls
    6415             :          defining the type before we try and stream the type on its
    6416             :          own.  Otherwise we can find ourselves trying to read in a
    6417             :          decl, when we're already partially reading in a component of
    6418             :          its type.  And that's bad.  */
    6419    36411354 :       tree type = t->typed.type;
    6420    36411354 :       unsigned prec = 0;
    6421             : 
    6422    36411354 :       switch (code)
    6423             :         {
    6424             :         default:
    6425             :           break;
    6426             : 
    6427             :         case TEMPLATE_DECL:
    6428             :           /* We fill in the template's type separately.  */
    6429    36411354 :           type = NULL_TREE;
    6430             :           break;
    6431             : 
    6432     2896617 :         case TYPE_DECL:
    6433     2896617 :           if (DECL_ORIGINAL_TYPE (t) && t == TYPE_NAME (type))
    6434             :             /* This is a typedef.  We set its type separately.  */
    6435             :             type = NULL_TREE;
    6436             :           break;
    6437             : 
    6438        9804 :         case ENUMERAL_TYPE:
    6439        9804 :           if (type && !ENUM_FIXED_UNDERLYING_TYPE_P (t))
    6440             :             {
    6441             :               /* Type is a restricted range integer type derived from the
    6442             :                  integer_types.  Find the right one.  */
    6443        6556 :               prec = TYPE_PRECISION (type);
    6444        6556 :               tree name = DECL_NAME (TYPE_NAME (type));
    6445             : 
    6446       85606 :               for (unsigned itk = itk_none; itk--;)
    6447       85606 :                 if (integer_types[itk]
    6448       85606 :                     && DECL_NAME (TYPE_NAME (integer_types[itk])) == name)
    6449             :                   {
    6450             :                     type = integer_types[itk];
    6451             :                     break;
    6452             :                   }
    6453        6556 :               gcc_assert (type != t->typed.type);
    6454             :             }
    6455             :           break;
    6456             :         }
    6457             : 
    6458    36411354 :       WT (type);
    6459    36411354 :       if (prec && streaming_p ())
    6460        3278 :         WU (prec);
    6461             :     }
    6462             : 
    6463             : #undef WT
    6464             : #undef WU
    6465    38741626 : }
    6466             : 
    6467             : // Streaming in a reference to a decl can cause that decl to be
    6468             : // TREE_USED, which is the mark_used behaviour we need most of the
    6469             : // time.  The trees_in::unused can be incremented to inhibit this,
    6470             : // which is at least needed for vtables.
    6471             : 
    6472             : bool
    6473    17475493 : trees_in::core_vals (tree t)
    6474             : {
    6475             : #define RU(X) ((X) = u ())
    6476             : #define RUC(T,X) ((X) = T (u ()))
    6477             : #define RT(X) ((X) = tree_node ())
    6478             : #define RTU(X) ((X) = tree_node (true))
    6479    17475493 :   tree_code code = TREE_CODE (t);
    6480             : 
    6481             :   /* First by tree shape.  */
    6482    17475493 :   if (CODE_CONTAINS_STRUCT (code, TS_DECL_MINIMAL))
    6483             :     {
    6484     4152298 :       RT (t->decl_minimal.name);
    6485     4152298 :       if (!DECL_TEMPLATE_PARM_P (t))
    6486     3603817 :         RT (t->decl_minimal.context);
    6487             : 
    6488             :       /* Don't zap the locus just yet, we don't record it correctly
    6489             :          and thus lose all location information.  */
    6490     4152298 :       t->decl_minimal.locus = state->read_location (*this);
    6491             :     }
    6492             : 
    6493    17475493 :   if (CODE_CONTAINS_STRUCT (code, TS_TYPE_COMMON))
    6494             :     {
    6495      724102 :       RT (t->type_common.name);
    6496      724102 :       RT (t->type_common.context);
    6497             : 
    6498      724102 :       RT (t->type_common.main_variant);
    6499      724102 :       RT (t->type_common.canonical);
    6500             : 
    6501             :       /* type_common.next_variant is internally manipulated.  */
    6502             :       /* type_common.pointer_to, type_common.reference_to.  */
    6503             : 
    6504      724102 :       RU (t->type_common.precision);
    6505      724102 :       RU (t->type_common.contains_placeholder_bits);
    6506      724102 :       RUC (machine_mode, t->type_common.mode);
    6507      724102 :       RU (t->type_common.align);
    6508             : 
    6509      724102 :       if (!RECORD_OR_UNION_CODE_P (code))
    6510             :         {
    6511      487546 :           RT (t->type_common.size);
    6512      487546 :           RT (t->type_common.size_unit);
    6513             :         }
    6514      724102 :       RT (t->type_common.attributes);
    6515             : 
    6516      724102 :       RT (t->type_common.common.chain); /* TYPE_STUB_DECL.  */
    6517             :     }
    6518             : 
    6519    17475493 :   if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
    6520             :     {
    6521     4152298 :       RUC (machine_mode, t->decl_common.mode);
    6522     4152298 :       RU (t->decl_common.off_align);
    6523     4152298 :       RU (t->decl_common.align);
    6524             : 
    6525     4152298 :       if (code != TEMPLATE_DECL)
    6526             :         {
    6527     3734986 :           RT (t->decl_common.size);
    6528     3734986 :           RT (t->decl_common.size_unit);
    6529             :         }
    6530             : 
    6531     4152298 :       RT (t->decl_common.attributes);
    6532     4152298 :       RT (t->decl_common.abstract_origin);
    6533             :     }
    6534             : 
    6535    17475493 :   if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
    6536             :     {
    6537     1921835 :       RT (t->decl_with_vis.assembler_name);
    6538     1921835 :       RUC (symbol_visibility, t->decl_with_vis.visibility);
    6539             :     }
    6540             : 
    6541    17475493 :   if (CODE_CONTAINS_STRUCT (code, TS_TYPE_NON_COMMON))
    6542             :     {
    6543      724102 :       if (code == ENUMERAL_TYPE)
    6544             :         {
    6545             :           /* These fields get set even for opaque enums that lack a
    6546             :              definition, so we stream them directly for each ENUMERAL_TYPE.
    6547             :              We stream TYPE_VALUES as part of the definition.  */
    6548        4695 :           RT (t->type_non_common.maxval);
    6549        4695 :           RT (t->type_non_common.minval);
    6550             :         }
    6551             :       /* Records and unions hold FIELDS, VFIELD & BINFO on these
    6552             :          things.  */
    6553      719407 :       else if (!RECORD_OR_UNION_CODE_P (code))
    6554             :         {
    6555             :           /* This is not clobbering TYPE_CACHED_VALUES, because this
    6556             :              is a type that doesn't have any.  */
    6557      482851 :           gcc_checking_assert (!TYPE_CACHED_VALUES_P (t));
    6558      482851 :           RT (t->type_non_common.values);
    6559      482851 :           RT (t->type_non_common.maxval);
    6560      482851 :           RT (t->type_non_common.minval);
    6561             :         }
    6562             : 
    6563      724102 :       RT (t->type_non_common.lang_1);
    6564             :     }
    6565             : 
    6566    17475493 :   if (CODE_CONTAINS_STRUCT (code, TS_EXP))
    6567             :     {
    6568     6211022 :       t->exp.locus = state->read_location (*this);
    6569             : 
    6570     6211022 :       bool vl = TREE_CODE_CLASS (code) == tcc_vl_exp;
    6571    11785195 :       for (unsigned limit = (vl ? VL_EXP_OPERAND_LENGTH (t)
    6572     5574173 :                              : TREE_OPERAND_LENGTH (t)),
    6573    17153815 :              ix = unsigned (vl); ix != limit; ix++)
    6574    10942793 :         RTU (TREE_OPERAND (t, ix));
    6575             :     }
    6576             : 
    6577             :   /* Then by CODE.  Special cases and/or 1:1 tree shape
    6578             :      correspondance. */
    6579    17475493 :   switch (code)
    6580             :     {
    6581             :     default:
    6582             :       break;
    6583             : 
    6584             :     case ARGUMENT_PACK_SELECT:
    6585             :     case DEFERRED_PARSE:
    6586             :     case IDENTIFIER_NODE:
    6587             :     case BINDING_VECTOR:
    6588             :     case SSA_NAME:
    6589             :     case TRANSLATION_UNIT_DECL:
    6590             :     case USERDEF_LITERAL:
    6591             :       return false; /* Should never meet.  */
    6592             : 
    6593             :       /* Constants.  */
    6594           9 :     case COMPLEX_CST:
    6595           9 :       RT (TREE_REALPART (t));
    6596           9 :       RT (TREE_IMAGPART (t));
    6597           9 :       break;
    6598             : 
    6599             :     case FIXED_CST:
    6600             :       /* Not suported in C++.  */
    6601             :       return false;
    6602             : 
    6603      624076 :     case INTEGER_CST:
    6604      624076 :       {
    6605      624076 :         unsigned num = TREE_INT_CST_EXT_NUNITS (t);
    6606     1250494 :         for (unsigned ix = 0; ix != num; ix++)
    6607      626418 :           TREE_INT_CST_ELT (t, ix) = wu ();
    6608             :       }
    6609             :       break;
    6610             : 
    6611             :     case POLY_INT_CST:
    6612             :       /* Not suported in C++.  */
    6613             :       return false;
    6614             : 
    6615       31334 :     case REAL_CST:
    6616       31334 :       if (const void *bytes = buf (sizeof (real_value)))
    6617       31334 :         memcpy (TREE_REAL_CST_PTR (t), bytes, sizeof (real_value));
    6618             :       break;
    6619             : 
    6620             :     case STRING_CST:
    6621             :       /* Streamed during start.  */
    6622             :       break;
    6623             : 
    6624          24 :     case VECTOR_CST:
    6625          63 :       for (unsigned ix = vector_cst_encoded_nelts (t); ix--;)
    6626          39 :         RT (VECTOR_CST_ENCODED_ELT (t, ix));
    6627             :       break;
    6628             : 
    6629             :       /* Decls.  */
    6630      243645 :     case VAR_DECL:
    6631      243645 :       if (DECL_CONTEXT (t)
    6632      243645 :           && TREE_CODE (DECL_CONTEXT (t)) != FUNCTION_DECL)
    6633             :         break;
    6634             :       /* FALLTHROUGH  */
    6635             : 
    6636     1851624 :     case RESULT_DECL:
    6637     1851624 :     case PARM_DECL:
    6638     1851624 :       if (DECL_HAS_VALUE_EXPR_P (t))
    6639             :         {
    6640             :           /* The DECL_VALUE hash table is a cache, thus if we're
    6641             :              reading a duplicate (which we end up discarding), the
    6642             :              value expr will also be cleaned up at the next gc.  */
    6643        8276 :           tree val = tree_node ();
    6644        8276 :           SET_DECL_VALUE_EXPR (t, val);
    6645             :         }
    6646             :       /* FALLTHROUGH  */
    6647             : 
    6648     1908627 :     case CONST_DECL:
    6649     1908627 :     case IMPORTED_DECL:
    6650     1908627 :       RT (t->decl_common.initial);
    6651     1908627 :       break;
    6652             : 
    6653       71369 :     case FIELD_DECL:
    6654       71369 :       RT (t->field_decl.offset);
    6655       71369 :       RT (t->field_decl.bit_field_type);
    6656       71369 :       RT (t->field_decl.qualifier);
    6657       71369 :       RT (t->field_decl.bit_offset);
    6658       71369 :       RT (t->field_decl.fcontext);
    6659       71369 :       RT (t->decl_common.initial);
    6660       71369 :       break;
    6661             : 
    6662       17530 :     case LABEL_DECL:
    6663       17530 :       RU (t->label_decl.label_decl_uid);
    6664       17530 :       RU (t->label_decl.eh_landing_pad_nr);
    6665       17530 :       break;
    6666             :   
    6667      600440 :     case FUNCTION_DECL:
    6668      600440 :       {
    6669      600440 :         unsigned bltin = u ();
    6670      600440 :         t->function_decl.built_in_class = built_in_class (bltin);
    6671      600440 :         if (bltin != NOT_BUILT_IN)
    6672             :           {
    6673       15176 :             bltin = u ();
    6674       15176 :             DECL_UNCHECKED_FUNCTION_CODE (t) = built_in_function (bltin);
    6675             :           }
    6676             : 
    6677      600440 :         RT (t->function_decl.personality);
    6678      600440 :         RT (t->function_decl.function_specific_target);
    6679      600440 :         RT (t->function_decl.function_specific_optimization);
    6680      600440 :         RT (t->function_decl.vindex);
    6681             : 
    6682      600440 :         if (DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (t))
    6683             :           {
    6684        1319 :             tree spec;
    6685        1319 :             RT (spec);
    6686        1319 :             store_explicit_specifier (t, spec);
    6687             :           }
    6688             :       }
    6689             :       break;
    6690             : 
    6691       55488 :     case USING_DECL:
    6692             :       /* USING_DECL_DECLS  */
    6693       55488 :       RT (t->decl_common.initial);
    6694             :       /* FALLTHROUGH  */
    6695             : 
    6696     1077628 :     case TYPE_DECL:
    6697             :       /* USING_DECL: USING_DECL_SCOPE  */
    6698             :       /* TYPE_DECL: DECL_ORIGINAL_TYPE */
    6699     1077628 :       RT (t->decl_non_common.result);
    6700     1077628 :       break;
    6701             : 
    6702             :       /* Miscellaneous common nodes.  */
    6703      351168 :     case BLOCK:
    6704      351168 :       t->block.locus = state->read_location (*this);
    6705      351168 :       t->block.end_locus = state->read_location (*this);
    6706      351168 :       t->block.vars = chained_decls ();
    6707             :       /* nonlocalized_vars is middle-end.  */
    6708      351168 :       RT (t->block.subblocks);
    6709      351168 :       RT (t->block.supercontext);
    6710      351168 :       RT (t->block.abstract_origin);
    6711             :       /* fragment_origin, fragment_chain are middle-end.  */
    6712      351168 :       RT (t->block.chain);
    6713             :       /* nonlocalized_vars, block_num, die are middle endy/debug
    6714             :          things.  */
    6715      351168 :       break;
    6716             : 
    6717      622996 :     case CALL_EXPR:
    6718      622996 :       RUC (internal_fn, t->base.u.ifn);
    6719      622996 :       break;
    6720             : 
    6721       49238 :     case CONSTRUCTOR:
    6722       49238 :       if (unsigned len = u ())
    6723             :         {
    6724       15897 :           vec_alloc (t->constructor.elts, len);
    6725       82361 :           for (unsigned ix = 0; ix != len; ix++)
    6726             :             {
    6727       66464 :               constructor_elt elt;
    6728             : 
    6729       66464 :               RT (elt.index);
    6730       66464 :               RTU (elt.value);
    6731       66464 :               t->constructor.elts->quick_push (elt);
    6732             :             }
    6733             :         }
    6734             :       break;
    6735             : 
    6736           3 :     case OMP_CLAUSE:
    6737           3 :       {
    6738           3 :         RU (t->omp_clause.subcode.map_kind);
    6739           3 :         t->omp_clause.locus = state->read_location (*this);
    6740             : 
    6741           3 :         unsigned len = omp_clause_num_ops[OMP_CLAUSE_CODE (t)];
    6742           9 :         for (unsigned ix = 0; ix != len; ix++)
    6743           6 :           RT (t->omp_clause.ops[ix]);
    6744             :       }
    6745             :       break;
    6746             : 
    6747      229582 :     case STATEMENT_LIST:
    6748      229582 :       {
    6749      229582 :         tree_stmt_iterator iter = tsi_start (t);
    6750      916752 :         for (tree stmt; RT (stmt);)
    6751      687170 :           tsi_link_after (&iter, stmt, TSI_CONTINUE_LINKING);
    6752             :       }
    6753      229582 :       break;
    6754             : 
    6755           0 :     case OPTIMIZATION_NODE:
    6756           0 :     case TARGET_OPTION_NODE:
    6757             :       /* Not yet implemented, see trees_out::core_vals.  */
    6758           0 :       gcc_unreachable ();
    6759      140918 :       break;
    6760             : 
    6761      140918 :     case TREE_BINFO:
    6762      140918 :       RT (t->binfo.common.chain);
    6763      140918 :       RT (t->binfo.offset);
    6764      140918 :       RT (t->binfo.inheritance);
    6765      140918 :       RT (t->binfo.vptr_field);
    6766             : 
    6767             :       /* Do not mark the vtables as USED in the address expressions
    6768             :          here.  */
    6769      140918 :       unused++;
    6770      140918 :       RT (t->binfo.vtable);
    6771      140918 :       RT (t->binfo.virtuals);
    6772      140918 :       RT (t->binfo.vtt_subvtt);
    6773      140918 :       RT (t->binfo.vtt_vptr);
    6774      140918 :       unused--;
    6775             : 
    6776      140918 :       BINFO_BASE_ACCESSES (t) = tree_vec ();
    6777      140918 :       if (!get_overrun ())
    6778             :         {
    6779      140918 :           unsigned num = vec_safe_length (BINFO_BASE_ACCESSES (t));
    6780      186018 :           for (unsigned ix = 0; ix != num; ix++)
    6781       45100 :             BINFO_BASE_APPEND (t, tree_node ());
    6782             :         }
    6783             :       break;
    6784             : 
    6785     1635970 :     case TREE_LIST:
    6786     1635970 :       RT (t->list.purpose);
    6787     1635970 :       RT (t->list.value);
    6788     1635970 :       RT (t->list.common.chain);
    6789     1635970 :       break;
    6790             : 
    6791     1000718 :     case TREE_VEC:
    6792     2782336 :       for (unsigned ix = TREE_VEC_LENGTH (t); ix--;)
    6793     1781618 :         RT (TREE_VEC_ELT (t, ix));
    6794     1000718 :       RT (t->type_common.common.chain);
    6795     1000718 :       break;
    6796             : 
    6797             :       /* C++-specific nodes ...  */
    6798      105687 :     case BASELINK:
    6799      105687 :       RT (((lang_tree_node *)t)->baselink.binfo);
    6800      105687 :       RTU (((lang_tree_node *)t)->baselink.functions);
    6801      105687 :       RT (((lang_tree_node *)t)->baselink.access_binfo);
    6802      105687 :       break;
    6803             : 
    6804       20627 :     case CONSTRAINT_INFO:
    6805       20627 :       RT (((lang_tree_node *)t)->constraint_info.template_reqs);
    6806       20627 :       RT (((lang_tree_node *)t)->constraint_info.declarator_reqs);
    6807       20627 :       RT (((lang_tree_node *)t)->constraint_info.associated_constr);
    6808       20627 :       break;
    6809             : 
    6810        6845 :     case DEFERRED_NOEXCEPT:
    6811        6845 :       RT (((lang_tree_node *)t)->deferred_noexcept.pattern);
    6812        6845 :       RT (((lang_tree_node *)t)->deferred_noexcept.args);
    6813        6845 :       break;
    6814             : 
    6815        3426 :     case LAMBDA_EXPR:
    6816        3426 :       RT (((lang_tree_node *)t)->lambda_expression.capture_list);
    6817        3426 :       RT (((lang_tree_node *)t)->lambda_expression.this_capture);
    6818        3426 :       RT (((lang_tree_node *)t)->lambda_expression.extra_scope);
    6819             :       /* lambda_expression.pending_proxies is NULL  */
    6820        3426 :       ((lang_tree_node *)t)->lambda_expression.locus
    6821        3426 :         = state->read_location (*this);
    6822        3426 :       RUC (cp_lambda_default_capture_mode_type,
    6823             :            ((lang_tree_node *)t)->lambda_expression.default_capture_mode);
    6824        3426 :       RU (((lang_tree_node *)t)->lambda_expression.discriminator_scope);
    6825        3426 :       RU (((lang_tree_node *)t)->lambda_expression.discriminator_sig);
    6826        3426 :       break;
    6827             : 
    6828      681478 :     case OVERLOAD:
    6829      681478 :       RT (((lang_tree_node *)t)->overload.function);
    6830      681478 :       RT (t->common.chain);
    6831      681478 :       break;
    6832             : 
    6833           0 :     case PTRMEM_CST:
    6834           0 :       RT (((lang_tree_node *)t)->ptrmem.member);
    6835           0 :       break;
    6836             : 
    6837        9057 :     case STATIC_ASSERT:
    6838        9057 :       RT (((lang_tree_node *)t)->static_assertion.condition);
    6839        9057 :       RT (((lang_tree_node *)t)->static_assertion.message);
    6840        9057 :       ((lang_tree_node *)t)->static_assertion.location
    6841        9057 :         = state->read_location (*this);
    6842        9057 :       break;
    6843             : 
    6844      417312 :     case TEMPLATE_DECL:
    6845             :       /* Streamed when reading the raw template decl itself.  */
    6846      417312 :       gcc_assert (((lang_tree_node *)t)->template_decl.arguments);
    6847      417312 :       gcc_assert (((lang_tree_node *)t)->template_decl.result);
    6848      417312 :       if (DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (t))
    6849        5381 :         RT (DECL_CHAIN (t));
    6850             :       break;
    6851             : 
    6852      950684 :     case TEMPLATE_INFO:
    6853      950684 :       RT (((lang_tree_node *)t)->template_info.tmpl);
    6854      950684 :       RT (((lang_tree_node *)t)->template_info.args);
    6855      950684 :       RT (((lang_tree_node *)t)->template_info.partial);
    6856      950684 :       if (unsigned len = u ())
    6857             :         {
    6858           0 :           auto &ac = (((lang_tree_node *)t)
    6859             :                       ->template_info.deferred_access_checks);
    6860           0 :           vec_alloc (ac, len);
    6861           0 :           for (unsigned ix = 0; ix != len; ix++)
    6862             :             {
    6863           0 :               deferred_access_check m;
    6864             : 
    6865           0 :               RT (m.binfo);
    6866           0 :               RT (m.decl);
    6867           0 :               RT (m.diag_decl);
    6868           0 :               m.loc = state->read_location (*this);
    6869           0 :               ac->quick_push (m);
    6870             :             }
    6871             :         }
    6872             :       break;
    6873             : 
    6874      520038 :     case TEMPLATE_PARM_INDEX:
    6875      520038 :       RU (((lang_tree_node *)t)->tpi.index);
    6876      520038 :       RU (((lang_tree_node *)t)->tpi.level);
    6877      520038 :       RU (((lang_tree_node *)t)->tpi.orig_level);
    6878      520038 :       RT (((lang_tree_node *)t)->tpi.decl);
    6879      520038 :       break;
    6880             : 
    6881        9430 :     case TRAIT_EXPR:
    6882        9430 :       RT (((lang_tree_node *)t)->trait_expression.type1);
    6883        9430 :       RT (((lang_tree_node *)t)->trait_expression.type2);
    6884        9430 :       RUC (cp_trait_kind, ((lang_tree_node *)t)->trait_expression.kind);
    6885        9430 :       break;
    6886             :     }
    6887             : 
    6888    17475493 :   if (CODE_CONTAINS_STRUCT (code, TS_TYPED))
    6889             :     {
    6890    16137112 :       tree type = tree_node ();
    6891             : 
    6892    16137112 :       if (type && code == ENUMERAL_TYPE && !ENUM_FIXED_UNDERLYING_TYPE_P (t))
    6893             :         {
    6894        3047 :           unsigned precision = u ();
    6895             : 
    6896        3047 :           type = build_distinct_type_copy (type);
    6897        3047 :           TYPE_PRECISION (type) = precision;
    6898        6094 :           set_min_and_max_values_for_integral_type (type, precision,
    6899        3047 :                                                     TYPE_SIGN (type));
    6900             :         }
    6901             : 
    6902    16137112 :       if (code != TEMPLATE_DECL)
    6903    15719800 :         t->typed.type = type;
    6904             :     }
    6905             : 
    6906             : #undef RT
    6907             : #undef RM
    6908             : #undef RU
    6909    17475493 :   return !get_overrun ();
    6910             : }
    6911             : 
    6912             : void
    6913     5310194 : trees_out::lang_decl_vals (tree t)
    6914             : {
    6915     5310194 :   const struct lang_decl *lang = DECL_LANG_SPECIFIC (t);
    6916             : #define WU(X) (u (X))
    6917             : #define WT(X) (tree_node (X))
    6918             :   /* Module index already written.  */
    6919     5310194 :   switch (lang->u.base.selector)
    6920             :     {
    6921           0 :     default:
    6922           0 :       gcc_unreachable ();
    6923             : 
    6924      994857 :     case lds_fn:  /* lang_decl_fn.  */
    6925      994857 :       if (streaming_p ())
    6926             :         {
    6927      497424 :           if (DECL_NAME (t) && IDENTIFIER_OVL_OP_P (DECL_NAME (t)))
    6928       82934 :             WU (lang->u.fn.ovl_op_code);
    6929             :         }
    6930             : 
    6931      994857 :       if (DECL_CLASS_SCOPE_P (t))
    6932      711806 :         WT (lang->u.fn.context);
    6933             : 
    6934      994857 :       if (lang->u.fn.thunk_p)
    6935             :         {
    6936             :           /* The thunked-to function.  */
    6937        1928 :           WT (lang->u.fn.befriending_classes);
    6938        1928 :           if (streaming_p ())
    6939         964 :             wi (lang->u.fn.u5.fixed_offset);
    6940             :         }
    6941             :       else
    6942      992929 :         WT (lang->u.fn.u5.cloned_function);
    6943             : 
    6944      994857 :       if (FNDECL_USED_AUTO (t))
    6945         496 :         WT (lang->u.fn.u.saved_auto_return_type);
    6946             : 
    6947      994857 :       goto lds_min;
    6948             : 
    6949        1124 :     case lds_decomp:  /* lang_decl_decomp.  */
    6950        1124 :       WT (lang->u.decomp.base);
    6951        1124 :       goto lds_min;
    6952             : 
    6953     3128747 :     case lds_min:  /* lang_decl_min.  */
    6954     3128747 :     lds_min:
    6955     3128747 :       WT (lang->u.min.template_info);
    6956     3128747 :       {
    6957     3128747 :         tree access = lang->u.min.access;
    6958             : 
    6959             :         /* DECL_ACCESS needs to be maintained by the definition of the
    6960             :            (derived) class that changes the access.  The other users
    6961             :            of DECL_ACCESS need to write it here.  */
    6962      994857 :         if (!DECL_THUNK_P (t)
    6963     4121676 :             && (DECL_CONTEXT (t) && TYPE_P (DECL_CONTEXT (t))))
    6964             :           access = NULL_TREE;
    6965             : 
    6966     3128747 :         WT (access);
    6967             :       }
    6968     3128747 :       break;
    6969             : 
    6970             :     case lds_ns:  /* lang_decl_ns.  */
    6971             :       break;
    6972             : 
    6973     2181321 :     case lds_parm:  /* lang_decl_parm.  */
    6974     2181321 :       if (streaming_p ())
    6975             :         {
    6976      760184 :           WU (lang->u.parm.level);
    6977      760184 :           WU (lang->u.parm.index);
    6978             :         }
    6979             :       break;
    6980             :     }
    6981             : #undef WU
    6982             : #undef WT
    6983     5310194 : }
    6984             : 
    6985             : bool
    6986     2709977 : trees_in::lang_decl_vals (tree t)
    6987             : {
    6988     2709977 :   struct lang_decl *lang = DECL_LANG_SPECIFIC (t);
    6989             : #define RU(X) ((X) = u ())
    6990             : #define RT(X) ((X) = tree_node ())
    6991             : 
    6992             :   /* Module index already read.  */
    6993     2709977 :   switch (lang->u.base.selector)
    6994             :     {
    6995           0 :     default:
    6996           0 :       gcc_unreachable ();
    6997             : 
    6998      600440 :     case lds_fn:  /* lang_decl_fn.  */
    6999      600440 :       if (DECL_NAME (t) && IDENTIFIER_OVL_OP_P (DECL_NAME (t)))
    7000             :         {
    7001      100013 :           unsigned code = u ();
    7002             : 
    7003             :           /* Check consistency.  */
    7004      100013 :           if (code >= OVL_OP_MAX
    7005      100013 :               || (ovl_op_info[IDENTIFIER_ASSIGN_OP_P (DECL_NAME (t))][code]
    7006      100013 :                   .ovl_op_code) == OVL_OP_ERROR_MARK)
    7007           0 :             set_overrun ();
    7008             :           else
    7009      100013 :             lang->u.fn.ovl_op_code = code;
    7010             :         }
    7011             : 
    7012      600440 :       if (DECL_CLASS_SCOPE_P (t))
    7013      426306 :         RT (lang->u.fn.context);
    7014             : 
    7015      600440 :       if (lang->u.fn.thunk_p)
    7016             :         {
    7017        1166 :           RT (lang->u.fn.befriending_classes);
    7018        1166 :           lang->u.fn.u5.fixed_offset = wi ();
    7019             :         }
    7020             :       else
    7021      599274 :         RT (lang->u.fn.u5.cloned_function);
    7022             : 
    7023      600440 :       if (FNDECL_USED_AUTO (t))
    7024         357 :         RT (lang->u.fn.u.saved_auto_return_type);
    7025      600440 :       goto lds_min;
    7026             : 
    7027         781 :     case lds_decomp:  /* lang_decl_decomp.  */
    7028         781 :       RT (lang->u.decomp.base);
    7029         781 :       goto lds_min;
    7030             : 
    7031     1811070 :     case lds_min:  /* lang_decl_min.  */
    7032     1811070 :     lds_min:
    7033     1811070 :       RT (lang->u.min.template_info);
    7034     1811070 :       RT (lang->u.min.access);
    7035     1811070 :       break;
    7036             : 
    7037             :     case lds_ns:  /* lang_decl_ns.  */
    7038             :       break;
    7039             : 
    7040      898802 :     case lds_parm:  /* lang_decl_parm.  */
    7041      898802 :       RU (lang->u.parm.level);
    7042      898802 :       RU (lang->u.parm.index);
    7043      898802 :       break;
    7044             :     }
    7045             : #undef RU
    7046             : #undef RT
    7047     2709977 :   return !get_overrun ();
    7048             : }
    7049             : 
    7050             : /* Most of the value contents of lang_type is streamed in
    7051             :    define_class.  */
    7052             : 
    7053             : void
    7054      387684 : trees_out::lang_type_vals (tree t)
    7055             : {
    7056      387684 :   const struct lang_type *lang = TYPE_LANG_SPECIFIC (t);
    7057             : #define WU(X) (u (X))
    7058             : #define WT(X) (tree_node (X))
    7059      387684 :   if (streaming_p ())
    7060      192966 :     WU (lang->align);
    7061             : #undef WU
    7062             : #undef WT
    7063      387684 : }
    7064             : 
    7065             : bool
    7066      210508 : trees_in::lang_type_vals (tree t)
    7067             : {
    7068      210508 :   struct lang_type *lang = TYPE_LANG_SPECIFIC (t);
    7069             : #define RU(X) ((X) = u ())
    7070             : #define RT(X) ((X) = tree_node ())
    7071      210508 :   RU (lang->align);
    7072             : #undef RU
    7073             : #undef RT
    7074      210508 :   return !get_overrun ();
    7075             : }
    7076             : 
    7077             : /* Write out the bools of T, including information about any
    7078             :    LANG_SPECIFIC information.  Including allocation of any lang
    7079             :    specific object.  */
    7080             : 
    7081             : void
    7082    14828573 : trees_out::tree_node_bools (tree t)
    7083             : {
    7084    14828573 :   gcc_checking_assert (streaming_p ());
    7085             : 
    7086             :   /* We should never stream a namespace.  */
    7087    14828573 :   gcc_checking_assert (TREE_CODE (t) != NAMESPACE_DECL
    7088             :                        || DECL_NAMESPACE_ALIAS (t));
    7089             : 
    7090    14828573 :   core_bools (t);
    7091             : 
    7092    14828573 :   switch (TREE_CODE_CLASS (TREE_CODE (t)))
    7093             :     {
    7094     3559547 :     case tcc_declaration:
    7095     3559547 :       {
    7096     3559547 :         bool specific = DECL_LANG_SPECIFIC (t) != NULL;
    7097     3559547 :         b (specific);
    7098     3559547 :         if (specific && VAR_P (t))
    7099      306061 :           b (DECL_DECOMPOSITION_P (t));
    7100     2317638 :         if (specific)
    7101     2317638 :           lang_decl_bools (t);
    7102             :       }
    7103             :       break;
    7104             : 
    7105      670998 :     case tcc_type:
    7106      670998 :       {
    7107      670998 :         bool specific = (TYPE_MAIN_VARIANT (t) == t
    7108      670998 :                          && TYPE_LANG_SPECIFIC (t) != NULL);
    7109      670998 :         gcc_assert (TYPE_LANG_SPECIFIC (t)
    7110             :                     == TYPE_LANG_SPECIFIC (TYPE_MAIN_VARIANT (t)));
    7111             : 
    7112      670998 :         b (specific);
    7113      670998 :         if (specific)
    7114      192966 :           lang_type_bools (t);
    7115             :       }
    7116             :       break;
    7117             : 
    7118             :     default:
    7119             :       break;
    7120             :     }
    7121             : 
    7122    14828573 :   bflush ();
    7123    14828573 : }
    7124             : 
    7125             : bool
    7126    17499603 : trees_in::tree_node_bools (tree t)
    7127             : {
    7128    17499603 :   bool ok = core_bools (t);
    7129             : 
    7130    17499603 :   if (ok)
    7131    17499603 :     switch (TREE_CODE_CLASS (TREE_CODE (t)))
    7132             :       {
    7133     4152298 :       case tcc_declaration:
    7134     4152298 :         if (b ())
    7135             :           {
    7136     2709977 :             bool decomp = VAR_P (t) && b ();
    7137             : 
    7138     2709977 :             ok = maybe_add_lang_decl_raw (t, decomp);
    7139     2709977 :             if (ok)
    7140     2709977 :               ok = lang_decl_bools (t);
    7141             :         }
    7142             :         break;
    7143             : 
    7144      748212 :       case tcc_type:
    7145      748212 :         if (b ())
    7146             :           {
    7147      210508 :             ok = maybe_add_lang_type_raw (t);
    7148      210508 :             if (ok)
    7149      210508 :               ok = lang_type_bools (t);
    7150             :           }
    7151             :         break;
    7152             : 
    7153             :       default:
    7154             :         break;
    7155             :       }
    7156             : 
    7157    17499603 :   bflush ();
    7158    17499603 :   if (!ok || get_overrun ())
    7159           0 :     return false;
    7160             : 
    7161             :   return true;
    7162             : }
    7163             : 
    7164             : 
    7165             : /* Write out the lang-specifc vals of node T.  */
    7166             : 
    7167             : void
    7168    38741626 : trees_out::lang_vals (tree t)
    7169             : {
    7170    38741626 :   switch (TREE_CODE_CLASS (TREE_CODE (t)))
    7171             :     {
    7172     9273397 :     case tcc_declaration:
    7173     9273397 :       if (DECL_LANG_SPECIFIC (t))
    7174     5310194 :         lang_decl_vals (t);
    7175             :       break;
    7176             : 
    7177     2350722 :     case tcc_type:
    7178     2350722 :       if (TYPE_MAIN_VARIANT (t) == t && TYPE_LANG_SPECIFIC (t))
    7179      387684 :         lang_type_vals (t);
    7180             :       break;
    7181             : 
    7182             :     default:
    7183             :       break;
    7184             :     }
    7185    38741626 : }
    7186             : 
    7187             : bool
    7188    17475493 : trees_in::lang_vals (tree t)
    7189             : {
    7190    17475493 :   bool ok = true;
    7191             : 
    7192    17475493 :   switch (TREE_CODE_CLASS (TREE_CODE (t)))
    7193             :     {
    7194     4152298 :     case tcc_declaration:
    7195     4152298 :       if (DECL_LANG_SPECIFIC (t))
    7196     2709977 :         ok = lang_decl_vals (t);
    7197             :       break;
    7198             : 
    7199      724102 :     case tcc_type:
    7200      724102 :       if (TYPE_LANG_SPECIFIC (t))
    7201      210508 :         ok = lang_type_vals (t);
    7202             :       else
    7203      513594 :         TYPE_LANG_SPECIFIC (t) = TYPE_LANG_SPECIFIC (TYPE_MAIN_VARIANT (t));
    7204             :       break;
    7205             : 
    7206             :     default:
    7207             :       break;
    7208             :     }
    7209             : 
    7210    17475493 :   return ok;
    7211             : }
    7212             : 
    7213             : /* Write out the value fields of node T.  */
    7214             : 
    7215             : void
    7216    38741626 : trees_out::tree_node_vals (tree t)
    7217             : {
    7218    38741626 :   core_vals (t);
    7219    38741626 :   lang_vals (t);
    7220    38741626 : }
    7221             : 
    7222             : bool
    7223    17475493 : trees_in::tree_node_vals (tree t)
    7224             : {
    7225    17475493 :   bool ok = core_vals (t);
    7226    17475493 :   if (ok)
    7227    17475493 :     ok = lang_vals (t);
    7228             : 
    7229    17475493 :   return ok;
    7230             : }
    7231             : 
    7232             : 
    7233             : /* If T is a back reference, fixed reference or NULL, write out its
    7234             :    code and return WK_none.  Otherwise return WK_value if we must write
    7235             :    by value, or WK_normal otherwise.  */
    7236             : 
    7237             : walk_kind
    7238   263599220 : trees_out::ref_node (tree t)
    7239             : {
    7240   263599220 :   if (!t)
    7241             :     {
    7242   106887069 :       if (streaming_p ())
    7243             :         {
    7244             :           /* NULL_TREE -> tt_null.  */
    7245    41016392 :           null_count++;
    7246    41016392 :           i (tt_null);
    7247             :         }
    7248   106887069 :       return WK_none;
    7249             :     }
    7250             : 
    7251   156712151 :   if (!TREE_VISITED (t))
    7252             :     return WK_normal;
    7253             : 
    7254             :   /* An already-visited tree.  It must be in the map.  */
    7255    92442314 :   int val = get_tag (t);
    7256             : 
    7257    92442314 :   if (val == tag_value)
    7258             :     /* An entry we should walk into.  */
    7259             :     return WK_value;
    7260             : 
    7261    90858484 :   const char *kind;
    7262             : 
    7263    90858484 :   if (val <= tag_backref)
    7264             :     {
    7265             :       /* Back reference -> -ve number  */
    7266    69820538 :       if (streaming_p ())
    7267    33128117 :         i (val);
    7268             :       kind = "backref";
    7269             :     }
    7270    21037946 :   else if (val >= tag_fixed)
    7271             :     {
    7272             :       /* Fixed reference -> tt_fixed */
    7273    21037946 :       val -= tag_fixed;
    7274    21037946 :       if (streaming_p ())
    7275     7360136 :         i (tt_fixed), u (val);
    7276             :       kind = "fixed";
    7277             :     }
    7278             : 
    7279    90858484 :   if (streaming_p ())
    7280             :     {
    7281    40488253 :       back_ref_count++;
    7282    40488253 :       dump (dumper::TREE)
    7283       13350 :         && dump ("Wrote %s:%d %C:%N%S", kind, val, TREE_CODE (t), t, t);
    7284             :     }
    7285             :   return WK_none;
    7286             : }
    7287             : 
    7288             : tree
    7289    38825153 : trees_in::back_ref (int tag)
    7290             : {
    7291    38825153 :   tree res = NULL_TREE;
    7292             : 
    7293    38825153 :   if (tag < 0 && unsigned (~tag) < back_refs.length ())
    7294    38825153 :     res = back_refs[~tag];
    7295             : 
    7296    38825153 :   if (!res
    7297             :       /* Checking TREE_CODE is a dereference, so we know this is not a
    7298             :          wild pointer.  Checking the code provides evidence we've not
    7299             :          corrupted something.  */
    7300    38825153 :       || TREE_CODE (res) >= MAX_TREE_CODES)
    7301           0 :     set_overrun ();
    7302             :   else
    7303    38839154 :     dump (dumper::TREE) && dump ("Read backref:%d found %C:%N%S", tag,
    7304             :                                  TREE_CODE (res), res, res);
    7305    38825153 :   return res;
    7306             : }
    7307             : 
    7308             : unsigned
    7309     3472018 : trees_out::add_indirect_tpl_parms (tree parms)
    7310             : {
    7311     3472018 :   unsigned len = 0;
    7312     6431767 :   for (; parms; parms = TREE_CHAIN (parms), len++)
    7313             :     {
    7314     3635353 :       if (TREE_VISITED (parms))
    7315             :         break;
    7316             : 
    7317     2959749 :       int tag = insert (parms);
    7318     2959749 :       if (streaming_p ())
    7319     2965862 :         dump (dumper::TREE)
    7320         156 :           && dump ("Indirect:%d template's parameter %u %C:%N",
    7321         156 :                    tag, len, TREE_CODE (parms), parms);
    7322             :     }
    7323             : 
    7324     3472018 :   if (streaming_p ())
    7325      572649 :     u (len);
    7326             : 
    7327     3472018 :   return len;
    7328             : }
    7329             : 
    7330             : unsigned
    7331      669964 : trees_in::add_indirect_tpl_parms (tree parms)
    7332             : {
    7333      669964 :   unsigned len = u ();
    7334     1193751 :   for (unsigned ix = 0; ix != len; parms = TREE_CHAIN (parms), ix++)
    7335             :     {
    7336      523787 :       int tag = insert (parms);
    7337      524198 :       dump (dumper::TREE)
    7338         162 :         && dump ("Indirect:%d template's parameter %u %C:%N",
    7339         162 :                  tag, ix, TREE_CODE (parms), parms);
    7340             :     }
    7341             : 
    7342      669964 :   return len;
    7343             : }
    7344             : 
    7345             : /* We've just found DECL by name.  Insert nodes that come with it, but
    7346             :    cannot be found by name, so we'll not accidentally walk into them.  */
    7347             : 
    7348             : void
    7349     8268210 : trees_out::add_indirects (tree decl)
    7350             : {
    7351     8268210 :   unsigned count = 0;
    7352             : 
    7353             :   // FIXME:OPTIMIZATION We'll eventually want default fn parms of
    7354             :   // templates and perhaps default template parms too.  The former can
    7355             :   // be referenced from instantiations (as they are lazily
    7356             :   // instantiated).  Also (deferred?) exception specifications of
    7357             :   // templates.  See the note about PARM_DECLs in trees_out::decl_node.
    7358     8268210 :   tree inner = decl;
    7359     8268210 :   if (TREE_CODE (decl) == TEMPLATE_DECL)
    7360             :     {
    7361     3472018 :       count += add_indirect_tpl_parms (DECL_TEMPLATE_PARMS (decl));
    7362             : 
    7363     3472018 :       inner = DECL_TEMPLATE_RESULT (decl);
    7364     3472018 :       int tag = insert (inner);
    7365     3472018 :       if (streaming_p ())
    7366      572649 :         dump (dumper::TREE)
    7367         222 :           && dump ("Indirect:%d template's result %C:%N",
    7368         222 :                    tag, TREE_CODE (inner), inner);
    7369     3472018 :       count++;
    7370             :     }
    7371             : 
    7372     8268210 :   if (TREE_CODE (inner) == TYPE_DECL)
    7373             :     {
    7374             :       /* Make sure the type is in the map too.  Otherwise we get
    7375             :          different RECORD_TYPEs for the same type, and things go
    7376             :          south.  */
    7377     4750912 :       tree type = TREE_TYPE (inner);
    7378     4750912 :       gcc_checking_assert (DECL_ORIGINAL_TYPE (inner)
    7379             :                            || TYPE_NAME (type) == inner);
    7380     4750912 :       int tag = insert (type);
    7381     4750912 :       if (streaming_p ())
    7382      544900 :         dump (dumper::TREE) && dump ("Indirect:%d decl's type %C:%N", tag,
    7383         363 :                                      TREE_CODE (type), type);
    7384     4750912 :       count++;
    7385             :     }
    7386             : 
    7387     8268210 :   if (streaming_p ())
    7388             :     {
    7389     1256807 :       u (count);
    7390     1257320 :       dump (dumper::TREE) && dump ("Inserted %u indirects", count);
    7391             :     }
    7392     8268210 : }
    7393             : 
    7394             : bool
    7395     1431776 : trees_in::add_indirects (tree decl)
    7396             : {
    7397     1431776 :   unsigned count = 0;
    7398             :             
    7399     1431776 :   tree inner = decl;
    7400     1431776 :   if (TREE_CODE (inner) == TEMPLATE_DECL)
    7401             :     {
    7402      669964 :       count += add_indirect_tpl_parms (DECL_TEMPLATE_PARMS (decl));
    7403             : 
    7404      669964 :       inner = DECL_TEMPLATE_RESULT (decl);
    7405      669964 :       int tag = insert (inner);
    7406      669964 :       dump (dumper::TREE)
    7407         231 :         && dump ("Indirect:%d templates's result %C:%N", tag,
    7408         231 :                  TREE_CODE (inner), inner);
    7409      669964 :       count++;
    7410             :     }
    7411             : 
    7412     1431776 :   if (TREE_CODE (inner) == TYPE_DECL)
    7413             :     {
    7414      588963 :       tree type = TREE_TYPE (inner);
    7415      588963 :       gcc_checking_assert (DECL_ORIGINAL_TYPE (inner)
    7416             :                            || TYPE_NAME (type) == inner);
    7417      588963 :       int tag = insert (type);
    7418      588963 :       dump (dumper::TREE)
    7419         363 :         && dump ("Indirect:%d decl's type %C:%N", tag, TREE_CODE (type), type);
    7420      588963 :       count++;
    7421             :     }
    7422             : 
    7423     1432352 :   dump (dumper::TREE) && dump ("Inserted %u indirects", count);
    7424     1431776 :   return count == u ();
    7425             : }
    7426             : 
    7427             : /* Stream a template parameter.  There are 4.5 kinds of parameter:
    7428             :    a) Template - TEMPLATE_DECL->TYPE_DECL->TEMPLATE_TEMPLATE_PARM
    7429             :         TEMPLATE_TYPE_PARM_INDEX TPI
    7430             :    b) Type - TYPE_DECL->TEMPLATE_TYPE_PARM TEMPLATE_TYPE_PARM_INDEX TPI
    7431             :    c.1) NonTYPE - PARM_DECL DECL_INITIAL TPI We meet this first
    7432             :    c.2) NonTYPE - CONST_DECL DECL_INITIAL Same TPI
    7433             :    d) BoundTemplate - TYPE_DECL->BOUND_TEMPLATE_TEMPLATE_PARM
    7434             :        TEMPLATE_TYPE_PARM_INDEX->TPI
    7435             :        TEMPLATE_TEMPLATE_PARM_INFO->TEMPLATE_INFO
    7436             : 
    7437             :    All of these point to a TEMPLATE_PARM_INDEX, and #B also has a TEMPLATE_INFO
    7438             : */
    7439             : 
    7440             : void
    7441     2134678 : trees_out::tpl_parm_value (tree parm)
    7442             : {
    7443     2134678 :   gcc_checking_assert (DECL_P (parm) && DECL_TEMPLATE_PARM_P (parm));
    7444             : 
    7445     2134678 :   int parm_tag = insert (parm);
    7446     2134678 :   if (streaming_p ())
    7447             :     {
    7448      485556 :       i (tt_tpl_parm);
    7449      485556 :       dump (dumper::TREE) && dump ("Writing template parm:%d %C:%N",
    7450         117 :                                    parm_tag, TREE_CODE (parm), parm);
    7451      485556 :       start (parm);
    7452      485556 :       tree_node_bools (parm);
    7453             :     }
    7454             : 
    7455     2134678 :   tree inner = parm;
    7456     2134678 :   if (TREE_CODE (inner) == TEMPLATE_DECL)
    7457             :     {
    7458        5003 :       inner = DECL_TEMPLATE_RESULT (inner);
    7459        5003 :       int inner_tag = insert (inner);
    7460        5003 :       if (streaming_p ())
    7461             :         {
    7462        1290 :           dump (dumper::TREE) && dump ("Writing inner template parm:%d %C:%N",
    7463           0 :                                        inner_tag, TREE_CODE (inner), inner);
    7464        1290 :           start (inner);
    7465        1290 :           tree_node_bools (inner);
    7466             :         }
    7467             :     }
    7468             : 
    7469     2134678 :   tree type = NULL_TREE;
    7470     2134678 :   if (TREE_CODE (inner) == TYPE_DECL)
    7471             :     {
    7472     1903143 :       type = TREE_TYPE (inner);
    7473     1903143 :       int type_tag = insert (type);
    7474     1903143 :       if (streaming_p ())
    7475             :         {
    7476      428391 :           dump (dumper::TREE) && dump ("Writing template parm type:%d %C:%N",
    7477         111 :                                        type_tag, TREE_CODE (type), type);
    7478      428391 :           start (type);
    7479      428391 :           tree_node_bools (type);
    7480             :         }
    7481             :     }
    7482             : 
    7483     2134678 :   if (inner != parm)
    7484             :     {
    7485             :       /* This is a template-template parameter.  */
    7486        5003 :       unsigned tpl_levels = 0;
    7487        5003 :       tpl_header (parm, &tpl_levels);
    7488        5003 :       tpl_parms_fini (parm, tpl_levels);
    7489             :     }
    7490             : 
    7491     2134678 :   tree_node_vals (parm);
    7492     2134678 :   if (inner != parm)
    7493        5003 :     tree_node_vals (inner);
    7494     2134678 :   if (type)
    7495             :     {
    7496     1903143 :       tree_node_vals (type);
    7497     1903143 :       if (DECL_NAME (inner) == auto_identifier
    7498     1903143 :           || DECL_NAME (inner) == decltype_auto_identifier)
    7499             :         {
    7500             :           /* Placeholder auto.  */
    7501       47415 :           tree_node (DECL_INITIAL (inner));
    7502       47415 :           tree_node (DECL_SIZE_UNIT (inner));
    7503             :         }
    7504             :     }
    7505             : 
    7506     2134678 :   if (streaming_p ())
    7507      485556 :     dump (dumper::TREE) && dump ("Wrote template parm:%d %C:%N",
    7508         117 :                                  parm_tag, TREE_CODE (parm), parm);
    7509     2134678 : }
    7510             : 
    7511             : tree
    7512      548481 : trees_in::tpl_parm_value ()
    7513             : {
    7514      548481 :   tree parm = start ();
    7515      548481 :   if (!parm || !tree_node_bools (parm))
    7516           0 :     return NULL_TREE;
    7517             : 
    7518      548481 :   int parm_tag = insert (parm);
    7519      548481 :   dump (dumper::TREE) && dump ("Reading template parm:%d %C:%N",
    7520         201 :                                parm_tag, TREE_CODE (parm), parm);
    7521             : 
    7522      548481 :   tree inner = parm;
    7523      548481 :   if (TREE_CODE (inner) == TEMPLATE_DECL)
    7524             :     {
    7525        1078 :       inner = start ();
    7526        1078 :       if (!inner || !tree_node_bools (inner))
    7527           0 :         return NULL_TREE;
    7528        1078 :       int inner_tag = insert (inner);
    7529        1078 :       dump (dumper::TREE) && dump ("Reading inner template parm:%d %C:%N",
    7530           0 :                                    inner_tag, TREE_CODE (inner), inner);
    7531        1078 :       DECL_TEMPLATE_RESULT (parm) = inner;
    7532             :     }
    7533             : 
    7534      548481 :   tree type = NULL_TREE;
    7535      548481 :   if (TREE_CODE (inner) == TYPE_DECL)
    7536             :     {
    7537      482851 :       type = start ();
    7538      482851 :       if (!type || !tree_node_bools (type))
    7539           0 :         return NULL_TREE;
    7540      482851 :       int type_tag = insert (type);
    7541      482851 :       dump (dumper::TREE) && dump ("Reading template parm type:%d %C:%N",
    7542         123 :                                    type_tag, TREE_CODE (type), type);
    7543             : 
    7544      482851 :       TREE_TYPE (inner) = TREE_TYPE (parm) = type;
    7545      482851 :       TYPE_NAME (type) = parm;
    7546             :     }
    7547             : 
    7548      548481 :   if (inner != parm)
    7549             :     {
    7550             :       /* A template template parameter.  */
    7551        1078 :       unsigned tpl_levels = 0;
    7552        1078 :       tpl_header (parm, &tpl_levels);
    7553        1078 :       tpl_parms_fini (parm, tpl_levels);
    7554             :     }
    7555             : 
    7556      548481 :   tree_node_vals (parm);
    7557      548481 :   if (inner != parm)
    7558        1078 :     tree_node_vals (inner);
    7559      548481 :   if (type)
    7560             :     {
    7561      482851 :       tree_node_vals (type);
    7562      482851 :       if (DECL_NAME (inner) == auto_identifier
    7563      482851 :           || DECL_NAME (inner) == decltype_auto_identifier)
    7564             :         {
    7565             :           /* Placeholder auto.  */
    7566       24160 :           DECL_INITIAL (inner) = tree_node ();
    7567       24160 :           DECL_SIZE_UNIT (inner) = tree_node ();
    7568             :         }
    7569      482851 :       if (TYPE_CANONICAL (type))
    7570             :         {
    7571      482851 :           gcc_checking_assert (TYPE_CANONICAL (type) == type);
    7572      482851 :           TYPE_CANONICAL (type) = canonical_type_parameter (type);
    7573             :         }
    7574             :     }
    7575             : 
    7576      548481 :   dump (dumper::TREE) && dump ("Read template parm:%d %C:%N",
    7577         201 :                                parm_tag, TREE_CODE (parm), parm);
    7578             : 
    7579             :   return parm;
    7580             : }
    7581             : 
    7582             : void
    7583     1320456 : trees_out::install_entity (tree decl, depset *dep)
    7584             : {
    7585     1320456 :   gcc_checking_assert (streaming_p ());
    7586             :   
    7587             :   /* Write the entity index, so we can insert it as soon as we
    7588             :      know this is new.  */
    7589     1320456 :   u (dep ? dep->cluster + 1 : 0);
    7590     1320456 :   if (CHECKING_P && dep)
    7591             :     {
    7592             :       /* Add it to the entity map, such that we can tell it is
    7593             :          part of us.  */
    7594     1017849 :       bool existed;
    7595     1017849 :       unsigned *slot = &entity_map->get_or_insert
    7596     1017849 :         (DECL_UID (decl), &existed);
    7597     1017849 :       if (existed)
    7598             :         /* If it existed, it should match.  */
    7599         377 :         gcc_checking_assert (decl == (*entity_ary)[*slot]);
    7600     1017849 :       *slot = ~dep->cluster;
    7601             :     }
    7602     1320456 : }
    7603             : 
    7604             : bool
    7605     1534819 : trees_in::install_entity (tree decl)
    7606             : {
    7607     1534819 :   unsigned entity_index = u ();
    7608     1534819 :   if (!entity_index)
    7609             :     return false;
    7610             : 
    7611     1167227 :   if (entity_index > state->entity_num)
    7612             :     {
    7613           0 :       set_overrun ();
    7614           0 :       return false;
    7615             :     }
    7616             : 
    7617             :   /* Insert the real decl into the entity ary.  */
    7618     1167227 :   unsigned ident = state->entity_lwm + entity_index - 1;
    7619     1167227 :   (*entity_ary)[ident] = decl;
    7620             : 
    7621             :   /* And into the entity map, if it's not already there.  */
    7622     1167227 :   tree not_tmpl = STRIP_TEMPLATE (decl);
    7623     1167227 :   if (!DECL_LANG_SPECIFIC (not_tmpl)
    7624     2184442 :       || !DECL_MODULE_ENTITY_P (not_tmpl))
    7625             :     {
    7626     1166859 :       retrofit_lang_decl (not_tmpl);
    7627     1166859 :       DECL_MODULE_ENTITY_P (not_tmpl) = true;
    7628             : 
    7629             :       /* Insert into the entity hash (it cannot already be there).  */
    7630     1166859 :       bool existed;
    7631     1166859 :       unsigned &slot = entity_map->get_or_insert (DECL_UID (decl), &existed);
    7632     1166859 :       gcc_checking_assert (!existed);
    7633     1166859 :       slot = ident;
    7634             :     }
    7635             : 
    7636             :   return true;
    7637             : }
    7638             : 
    7639             : static bool has_definition (tree decl);
    7640             : 
    7641             : /* DECL is a decl node that must be written by value.  DEP is the
    7642             :    decl's depset.  */
    7643             : 
    7644             : void
    7645     3669401 : trees_out::decl_value (tree decl, depset *dep)
    7646             : {
    7647             :   /* We should not be writing clones or template parms.  */
    7648     3669401 :   gcc_checking_assert (DECL_P (decl)
    7649             :                        && !DECL_CLONED_FUNCTION_P (decl)
    7650             :                        && !DECL_TEMPLATE_PARM_P (decl));
    7651             : 
    7652             :   /* We should never be writing non-typedef ptrmemfuncs by value.  */
    7653     3669401 :   gcc_checking_assert (TREE_CODE (decl) != TYPE_DECL
    7654             :                        || DECL_ORIGINAL_TYPE (decl)
    7655             :                        || !TYPE_PTRMEMFUNC_P (TREE_TYPE (decl)));
    7656             : 
    7657     3669401 :   merge_kind mk = get_merge_kind (decl, dep);
    7658             : 
    7659     3669401 :   if (CHECKING_P)
    7660             :     {
    7661             :       /* Never start in the middle of a template.  */
    7662     3669401 :       int use_tpl = -1;
    7663     3669401 :       if (tree ti = node_template_info (decl, use_tpl))
    7664     1327315 :         gcc_checking_assert (TREE_CODE (TI_TEMPLATE (ti)) == OVERLOAD
    7665             :                              || TREE_CODE (TI_TEMPLATE (ti)) == FIELD_DECL
    7666             :                              || (DECL_TEMPLATE_RESULT (TI_TEMPLATE (ti))
    7667             :                                  != decl));
    7668             :     }
    7669             : 
    7670     3669401 :   if (streaming_p ())
    7671             :     {
    7672             :       /* A new node -> tt_decl.  */
    7673     1320456 :       decl_val_count++;
    7674     1320456 :       i (tt_decl);
    7675     1320456 :       u (mk);
    7676     1320456 :       start (decl);
    7677             : 
    7678     1320456 :       if (mk != MK_unique)
    7679             :         {
    7680     1146783 :           if (!(mk & MK_template_mask) && !state->is_header ())
    7681             :             {
    7682             :               /* Tell the importer whether this is a global module entity,
    7683             :                  or a module entity.  This bool merges into the next block
    7684             :                  of bools.  Sneaky.  */
    7685       55734 :               tree o = get_originating_module_decl (decl);
    7686       55734 :               bool is_attached = false;
    7687             : 
    7688       55734 :               tree not_tmpl = STRIP_TEMPLATE (o);
    7689       55734 :               if (DECL_LANG_SPECIFIC (not_tmpl)
    7690       66354 :                   && DECL_MODULE_ATTACH_P (not_tmpl))
    7691             :                 is_attached = true;
    7692             : 
    7693       55734 :               b (is_attached);
    7694             :             }
    7695     1146783 :           b (dep && dep->has_defn ());
    7696             :         }
    7697     1320456 :       tree_node_bools (decl);
    7698             :     }
    7699             : 
    7700     3669401 :   int tag = insert (decl, WK_value);
    7701     3669401 :   if (streaming_p ())
    7702     1320456 :     dump (dumper::TREE)
    7703         639 :       && dump ("Writing %s:%d %C:%N%S", merge_kind_name[mk], tag,
    7704         639 :                TREE_CODE (decl), decl, decl);
    7705             : 
    7706     3669401 :   tree inner = decl;
    7707     3669401 :   int inner_tag = 0;
    7708     3669401 :   if (TREE_CODE (decl) == TEMPLATE_DECL)
    7709             :     {
    7710     1101570 :       inner = DECL_TEMPLATE_RESULT (decl);
    7711     1101570 :       inner_tag = insert (inner, WK_value);
    7712             : 
    7713     1101570 :       if (streaming_p ())
    7714             :         {
    7715      367190 :           int code = TREE_CODE (inner);
    7716      367190 :           u (code);
    7717      367190 :           start (inner, true);
    7718      367190 :           tree_node_bools (inner);
    7719      367190 :           dump (dumper::TREE)
    7720         132 :             && dump ("Writing %s:%d %C:%N%S", merge_kind_name[mk], inner_tag,
    7721         132 :                      TREE_CODE (inner), inner, inner);
    7722             :         }
    7723             :     }
    7724             : 
    7725     3669401 :   tree type = NULL_TREE;
    7726     3669401 :   int type_tag = 0;
    7727     3669401 :   tree stub_decl = NULL_TREE;
    7728     3669401 :   int stub_tag = 0;
    7729     3669401 :   if (TREE_CODE (inner) == TYPE_DECL)
    7730             :     {
    7731     1467410 :       type = TREE_TYPE (inner);
    7732     1467410 :       bool has_type = (type == TYPE_MAIN_VARIANT (type)
    7733     1467410 :                        && TYPE_NAME (type) == inner);
    7734             : 
    7735     1467410 :       if (streaming_p ())
    7736      493990 :         u (has_type ? TREE_CODE (type) : 0);
    7737             : 
    7738     1467410 :       if (has_type)
    7739             :         {
    7740      670491 :           type_tag = insert (type, WK_value);
    7741      670491 :           if (streaming_p ())
    7742             :             {
    7743      222912 :               start (type, true);
    7744      222912 :               tree_node_bools (type);
    7745      222912 :               dump (dumper::TREE)
    7746         153 :                 && dump ("Writing type:%d %C:%N", type_tag,
    7747         153 :                          TREE_CODE (type), type);
    7748             :             }
    7749             : 
    7750      670491 :           stub_decl = TYPE_STUB_DECL (type);
    7751      670491 :           bool has_stub = inner != stub_decl;
    7752      670491 :           if (streaming_p ())
    7753      222912 :             u (has_stub ? TREE_CODE (stub_decl) : 0);
    7754      670491 :           if (has_stub)
    7755             :             {
    7756        2898 :               stub_tag = insert (stub_decl);
    7757        2898 :               if (streaming_p ())
    7758             :                 {
    7759         966 :                   start (stub_decl, true);
    7760         966 :                   tree_node_bools (stub_decl);
    7761         966 :                   dump (dumper::TREE)
    7762           0 :                     && dump ("Writing stub_decl:%d %C:%N", stub_tag,
    7763           0 :                              TREE_CODE (stub_decl), stub_decl);
    7764             :                 }
    7765             :             }
    7766             :           else
    7767             :             stub_decl = NULL_TREE;
    7768             :         }
    7769             :       else
    7770             :         /* Regular typedef.  */
    7771             :         type = NULL_TREE;
    7772             :     }
    7773             : 
    7774             :   /* Stream the container, we want it correctly canonicalized before
    7775             :      we start emitting keys for this decl.  */
    7776     3669401 :   tree container = decl_container (decl);
    7777             : 
    7778     3669401 :   unsigned tpl_levels = 0;
    7779     3669401 :   if (decl != inner)
    7780     1101570 :     tpl_header (decl, &tpl_levels);
    7781     3669401 :   if (TREE_CODE (inner) == FUNCTION_DECL)
    7782     1491202 :     fn_parms_init (inner);
    7783             : 
    7784             :   /* Now write out the merging information, and then really
    7785             :      install the tag values.  */
    7786     3669401 :   key_mergeable (tag, mk, decl, inner, container, dep);
    7787             : 
    7788     3669401 :   if (streaming_p ())
    7789     1320456 :     dump (dumper::MERGE)
    7790         723 :       && dump ("Wrote:%d's %s merge key %C:%N", tag,
    7791         723 :                merge_kind_name[mk], TREE_CODE (decl), decl);
    7792             : 
    7793     3669401 :   if (TREE_CODE (inner) == FUNCTION_DECL)
    7794             :     fn_parms_fini (inner);
    7795             : 
    7796     3669401 :   if (!is_key_order ())
    7797     2650059 :     tree_node_vals (decl);
    7798             : 
    7799     3669401 :   if (inner_tag)
    7800             :     {
    7801     1101570 :       if (!is_key_order ())
    7802      734380 :         tree_node_vals (inner);
    7803     1101570 :       tpl_parms_fini (decl, tpl_levels);
    7804             :     }
    7805             : 
    7806     3669401 :   if (type && !is_key_order ())
    7807             :     {
    7808      447579 :       tree_node_vals (type);
    7809      447579 :       if (stub_decl)
    7810        1932 :         tree_node_vals (stub_decl);
    7811             :     }
    7812             : 
    7813     3669401 :   if (!is_key_order ())
    7814             :     {
    7815     2650059 :       if (mk & MK_template_mask
    7816     1813083 :           || mk == MK_partial
    7817     1813083 :           || mk == MK_friend_spec)
    7818             :         {
    7819       43342 :           if (mk != MK_partial)
    7820             :             {
    7821             :               // FIXME: We should make use of the merge-key by
    7822             :               // exposing it outside of key_mergeable.  But this gets
    7823             :               // the job done.
    7824      836976 :               auto *entry = reinterpret_cast <spec_entry *> (dep->deps[0]);
    7825             : 
    7826      836976 :               if (streaming_p ())
    7827      418488 :                 u (get_mergeable_specialization_flags (entry->tmpl, decl));
    7828      836976 :               tree_node (entry->tmpl);
    7829      836976 :               tree_node (entry->args);
    7830             :             }
    7831             :           else
    7832             :             {
    7833       43342 :               tree ti = get_template_info (inner);
    7834       43342 :               tree_node (TI_TEMPLATE (ti));
    7835       43342 :               tree_node (TI_ARGS (ti));
    7836             :             }
    7837             :         }
    7838     2650059 :       tree_node (get_constraints (decl));
    7839             :     }
    7840             : 
    7841     3669401 :   if (streaming_p ())
    7842             :     {
    7843             :       /* Do not stray outside this section.  */
    7844     1320456 :       gcc_checking_assert (!dep || dep->section == dep_hash->section);
    7845             : 
    7846             :       /* Write the entity index, so we can insert it as soon as we
    7847             :          know this is new.  */
    7848     1320456 :       install_entity (decl, dep);
    7849             :     }
    7850             : 
    7851     3669401 :   if (VAR_OR_FUNCTION_DECL_P (inner)
    7852     1924530 :       && DECL_LANG_SPECIFIC (inner)
    7853     1844488 :       && DECL_MODULE_KEYED_DECLS_P (inner)
    7854     3669464 :       && !is_key_order ())
    7855             :     {
    7856             :       /* Stream the keyed entities.  */
    7857          42 :       auto *attach_vec = keyed_table->get (inner);
    7858          42 :       unsigned num = attach_vec->length ();
    7859          42 :       if (streaming_p ())
    7860          21 :         u (num);
    7861          84 :       for (unsigned ix = 0; ix != num; ix++)
    7862             :         {
    7863          42 :           tree attached = (*attach_vec)[ix];
    7864          42 :           tree_node (attached);
    7865          42 :           if (streaming_p ())
    7866          57 :             dump (dumper::MERGE)
    7867          15 :               && dump ("Written %d[%u] attached decl %N", tag, ix, attached);
    7868             :         }
    7869             :     }
    7870             : 
    7871     3669401 :   bool is_typedef = false;
    7872     3669401 :   if (!type && TREE_CODE (inner) == TYPE_DECL)
    7873             :     {
    7874      796919 :       tree t = TREE_TYPE (inner);
    7875      796919 :       unsigned tdef_flags = 0;
    7876      796919 :       if (DECL_ORIGINAL_TYPE (inner)
    7877      796919 :           && TYPE_NAME (TREE_TYPE (inner)) == inner)
    7878             :         {
    7879      793853 :           tdef_flags |= 1;
    7880      793853 :           if (TYPE_STRUCTURAL_EQUALITY_P (t)
    7881      157177 :               && TYPE_DEPENDENT_P_VALID (t)
    7882      902292 :               && TYPE_DEPENDENT_P (t))
    7883             :             tdef_flags |= 2;
    7884             :         }
    7885      796919 :       if (streaming_p ())
    7886      271078 :         u (tdef_flags);
    7887             : 
    7888      796919 :       if (tdef_flags & 1)
    7889             :         {
    7890             :           /* A typedef type.  */
    7891      793853 :           int type_tag = insert (t);
    7892      793853 :           if (streaming_p ())
    7893      269545 :             dump (dumper::TREE)
    7894         198 :               && dump ("Cloned:%d %s %C:%N", type_tag,
    7895         198 :                        tdef_flags & 2 ? "depalias" : "typedef",
    7896         198 :                        TREE_CODE (t), t);
    7897             : 
    7898             :           is_typedef = true;
    7899             :         }
    7900             :     }
    7901             : 
    7902     3669401 :   if (streaming_p () && DECL_MAYBE_IN_CHARGE_CDTOR_P (decl))
    7903             :     {
    7904       98737 :       bool cloned_p
    7905       98737 :         = (DECL_CHAIN (decl) && DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl)));
    7906       67988 :       bool needs_vtt_parm_p
    7907       67988 :         = (cloned_p && CLASSTYPE_VBASECLASSES (DECL_CONTEXT (decl)));
    7908       67988 :       bool omit_inherited_parms_p
    7909       67988 :         = (cloned_p && DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl)
    7910       54272 :            && base_ctor_omit_inherited_parms (decl));
    7911       98737 :       unsigned flags = (int (cloned_p) << 0
    7912       98737 :                         | int (needs_vtt_parm_p) << 1
    7913       98737 :                         | int (omit_inherited_parms_p) << 2);
    7914       98737 :       u (flags);
    7915       98812 :       dump (dumper::TREE) && dump ("CDTOR %N is %scloned",
    7916             :                                    decl, cloned_p ? "" : "not ");
    7917             :     }
    7918             : 
    7919     3669401 :   if (streaming_p ())
    7920     1320456 :     dump (dumper::TREE) && dump ("Written decl:%d %C:%N", tag,
    7921         639 :                                  TREE_CODE (decl), decl);
    7922             : 
    7923     3669401 :   if (NAMESPACE_SCOPE_P (inner))
    7924     2338274 :     gcc_checking_assert (!dep == (VAR_OR_FUNCTION_DECL_P (inner)
    7925             :                                   && DECL_LOCAL_DECL_P (inner)));
    7926     2500140 :   else if ((TREE_CODE (inner) == TYPE_DECL
    7927      782156 :             && !is_typedef
    7928      134184 :             && TYPE_NAME (TREE_TYPE (inner)) == inner)
    7929     3151178 :            || TREE_CODE (inner) == FUNCTION_DECL)
    7930             :     {
    7931     1197863 :       bool write_defn = !dep && has_definition (decl);
    7932     1197863 :       if (streaming_p ())
    7933      399025 :         u (write_defn);
    7934     1197863 :       if (write_defn)
    7935        1749 :         write_definition (decl);
    7936             :     }
    7937     3669401 : }
    7938             : 
    7939             : tree
    7940     1534819 : trees_in::decl_value ()
    7941             : {
    7942     1534819 :   int tag = 0;
    7943     1534819 :   bool is_attached = false;
    7944     1534819 :   bool has_defn = false;
    7945     1534819 :   unsigned mk_u = u ();
    7946     1534819 :   if (mk_u >= MK_hwm || !merge_kind_name[mk_u])
    7947             :     {
    7948           0 :       set_overrun ();
    7949           0 :       return NULL_TREE;
    7950             :     }
    7951             : 
    7952     1534819 :   unsigned saved_unused = unused;
    7953     1534819 :   unused = 0;
    7954             :   
    7955     1534819 :   merge_kind mk = merge_kind (mk_u);
    7956             : 
    7957     1534819 :   tree decl = start ();
    7958     1534819 :   if (decl)
    7959             :     {
    7960     1534819 :       if (mk != MK_unique)
    7961             :         {
    7962     1312735 :           if (!(mk & MK_template_mask) && !state->is_header ())
    7963             :             /* See note in trees_out about where this bool is sequenced.  */
    7964       31817 :             is_attached = b ();
    7965             : 
    7966     1312735 :           has_defn = b ();
    7967             :         }
    7968             : 
    7969     1534819 :       if (!tree_node_bools (decl))
    7970           0 :         decl = NULL_TREE;
    7971             :     }
    7972             :   
    7973             :   /* Insert into map.  */
    7974     1534819 :   tag = insert (decl);
    7975     1534819 :   if (decl)
    7976     1534819 :     dump (dumper::TREE)
    7977         939 :       && dump ("Reading:%d %C", tag, TREE_CODE (decl));
    7978             : 
    7979     1534819 :   tree inner = decl;
    7980     1534819 :   int inner_tag = 0;
    7981     1534819 :   if (decl && TREE_CODE (decl) == TEMPLATE_DECL)
    7982             :     {
    7983      416234 :       int code = u ();
    7984      416234 :       inner = start (code);
    7985      416234 :       if (inner && tree_node_bools (inner))
    7986      416234 :         DECL_TEMPLATE_RESULT (decl) = inner;
    7987             :       else
    7988             :         decl = NULL_TREE;
    7989             : 
    7990      416234 :       inner_tag = insert (inner);
    7991      416234 :       if (decl)
    7992      416234 :         dump (dumper::TREE)
    7993         204 :           && dump ("Reading:%d %C", inner_tag, TREE_CODE (inner));
    7994             :     }
    7995             : 
    7996     1534819 :   tree type = NULL_TREE;
    7997     1534819 :   int type_tag = 0;
    7998     1534819 :   tree stub_decl = NULL_TREE;
    7999     1534819 :   int stub_tag = 0;
    8000     1534819 :   if (decl && TREE_CODE (inner) == TYPE_DECL)
    8001             :     {
    8002      538367 :       if (unsigned type_code = u ())
    8003             :         {
    8004      241251 :           type = start (type_code);
    8005      241251 :           if (type && tree_node_bools (type))
    8006             :             {
    8007      241251 :               TREE_TYPE (inner) = type;
    8008      241251 :               TYPE_NAME (type) = inner;
    8009             :             }
    8010             :           else
    8011             :             decl = NULL_TREE;
    8012             : 
    8013      241251 :           type_tag = insert (type);
    8014      241251 :           if (decl)
    8015      241251 :             dump (dumper::TREE)
    8016         210 :               && dump ("Reading type:%d %C", type_tag, TREE_CODE (type));
    8017             : 
    8018      241251 :           if (unsigned stub_code = u ())
    8019             :             {
    8020         922 :               stub_decl = start (stub_code);
    8021         922 :               if (stub_decl && tree_node_bools (stub_decl))
    8022             :                 {
    8023         922 :                   TREE_TYPE (stub_decl) = type;
    8024         922 :                   TYPE_STUB_DECL (type) = stub_decl;
    8025             :                 }
    8026             :               else
    8027             :                 decl = NULL_TREE;
    8028             : 
    8029         922 :               stub_tag = insert (stub_decl);
    8030         922 :               if (decl)
    8031         922 :                 dump (dumper::TREE)
    8032           0 :                   && dump ("Reading stub_decl:%d %C", stub_tag,
    8033           0 :                            TREE_CODE (stub_decl));
    8034             :             }
    8035             :         }
    8036             :     }
    8037             : 
    8038     1534819 :   if (!decl)
    8039             :     {
    8040           0 :     bail:
    8041           0 :       if (inner_tag != 0)
    8042           0 :         back_refs[~inner_tag] = NULL_TREE;
    8043           0 :       if (type_tag != 0)
    8044           0 :         back_refs[~type_tag] = NULL_TREE;
    8045           0 :       if (stub_tag != 0)
    8046           0 :         back_refs[~stub_tag] = NULL_TREE;
    8047           0 :       if (tag != 0)
    8048           0 :         back_refs[~tag] = NULL_TREE;
    8049           0 :       set_overrun ();
    8050             :       /* Bail.  */
    8051           0 :       unused = saved_unused;
    8052           0 :       return NULL_TREE;
    8053             :     }
    8054             : 
    8055             :   /* Read the container, to ensure it's already been streamed in.  */
    8056     1534819 :   tree container = decl_container ();
    8057     1534819 :   unsigned tpl_levels = 0;
    8058             : 
    8059             :   /* Figure out if this decl is already known about.  */
    8060     1534819 :   int parm_tag = 0;
    8061             : 
    8062     1534819 :   if (decl != inner)
    8063      416234 :     if (!tpl_header (decl, &tpl_levels))
    8064           0 :       goto bail;
    8065     1534819 :   if (TREE_CODE (inner) == FUNCTION_DECL)
    8066      600440 :     parm_tag = fn_parms_init (inner);
    8067             : 
    8068     1534819 :   tree existing = key_mergeable (tag, mk, decl, inner, type, container,
    8069     1534819 :                                  is_attached);
    8070     1534819 :   tree existing_inner = existing;
    8071     1534819 :   if (existing)
    8072             :     {
    8073      747479 :       if (existing == error_mark_node)
    8074           0 :         goto bail;
    8075             : 
    8076      747479 :       if (TREE_CODE (STRIP_TEMPLATE (existing)) == TYPE_DECL)
    8077             :         {
    8078      287192 :           tree etype = TREE_TYPE (existing);
    8079      287192 :           if (TYPE_LANG_SPECIFIC (etype)
    8080      208517 :               && COMPLETE_TYPE_P (etype)
    8081      409708 :               && !CLASSTYPE_MEMBER_VEC (etype))
    8082             :             /* Give it a member vec, we're likely gonna be looking
    8083             :                inside it.  */
    8084       28203 :             set_class_bindings (etype, -1);
    8085             :         }
    8086             : 
    8087             :       /* Install the existing decl into the back ref array.  */
    8088      747479 :       register_duplicate (decl, existing);
    8089      747479 :       back_refs[~tag] = existing;
    8090      747479 :       if (inner_tag != 0)
    8091             :         {
    8092      242569 :           existing_inner = DECL_TEMPLATE_RESULT (existing);
    8093      242569 :           back_refs[~inner_tag] = existing_inner;
    8094             :         }
    8095             : 
    8096      747479 :       if (type_tag != 0)
    8097             :         {
    8098      136186 :           tree existing_type = TREE_TYPE (existing);
    8099      136186 :           back_refs[~type_tag] = existing_type;
    8100      136186 :           if (stub_tag != 0)
    8101         552 :             back_refs[~stub_tag] = TYPE_STUB_DECL (existing_type);
    8102             :         }
    8103             :     }
    8104             : 
    8105     1534819 :   if (parm_tag)
    8106      600440 :     fn_parms_fini (parm_tag, inner, existing_inner, has_defn);
    8107             : 
    8108     1534819 :   if (!tree_node_vals (decl))
    8109           0 :     goto bail;
    8110             : 
    8111     1534819 :   if (inner_tag)
    8112             :     {
    8113      416234 :       gcc_checking_assert (DECL_TEMPLATE_RESULT (decl) == inner);
    8114             : 
    8115      416234 :       if (!tree_node_vals (inner))
    8116           0 :         goto bail;
    8117             : 
    8118      416234 :       if (!tpl_parms_fini (decl, tpl_levels))
    8119           0 :         goto bail;
    8120             :     }
    8121             : 
    8122     1534819 :   if (type && (!tree_node_vals (type)
    8123      241251 :                || (stub_decl && !tree_node_vals (stub_decl))))
    8124           0 :     goto bail;
    8125             : 
    8126     1534819 :   spec_entry spec;
    8127     1534819 :   unsigned spec_flags = 0;
    8128     1534819 :   if (mk & MK_template_mask
    8129     1051037 :       || mk == MK_partial
    8130     1051037 :       || mk == MK_friend_spec)
    8131             :     {
    8132       20917 :       if (mk == MK_partial)
    8133             :         spec_flags = 2;
    8134             :       else
    8135      483782 :         spec_flags = u ();
    8136             : 
    8137      504699 :       spec.tmpl = tree_node ();
    8138      504699 :       spec.args = tree_node ();
    8139             :     }
    8140             :   /* Hold constraints on the spec field, for a short while.  */
    8141     1534819 :   spec.spec = tree_node ();
    8142             : 
    8143     1535758 :   dump (dumper::TREE) && dump ("Read:%d %C:%N", tag, TREE_CODE (decl), decl);
    8144             : 
    8145     1534819 :   existing = back_refs[~tag];
    8146     1534819 :   bool installed = install_entity (existing);
    8147     1534819 :   bool is_new = existing == decl;
    8148             : 
    8149     1534819 :   if (VAR_OR_FUNCTION_DECL_P (inner)
    8150      843899 :       && DECL_LANG_SPECIFIC (inner)
    8151     2334568 :       && DECL_MODULE_KEYED_DECLS_P (inner))
    8152             :     {
    8153             :       /* Read and maybe install the attached entities.  */
    8154          33 :       bool existed;
    8155          33 :       auto &set = keyed_table->get_or_insert (STRIP_TEMPLATE (existing),
    8156             :                                               &existed);
    8157          33 :       unsigned num = u ();
    8158          33 :       if (is_new == existed)
    8159           0 :         set_overrun ();
    8160          33 :       if (is_new)
    8161          12 :         set.reserve (num);
    8162          66 :       for (unsigned ix = 0; !get_overrun () && ix != num; ix++)
    8163             :         {
    8164          33 :           tree attached = tree_node ();
    8165          33 :           dump (dumper::MERGE)
    8166          54 :             && dump ("Read %d[%u] %s attached decl %N", tag, ix,
    8167             :                      is_new ? "new" : "matched", attached);
    8168          33 :           if (is_new)
    8169          12 :             set.quick_push (attached);
    8170          21 :           else if (set[ix] != attached)
    8171          33 :             set_overrun ();
    8172             :         }
    8173             :     }
    8174             : 
    8175             :   /* Regular typedefs will have a NULL TREE_TYPE at this point.  */
    8176     1534819 :   unsigned tdef_flags = 0;
    8177     1534819 :   bool is_typedef = false;
    8178     1534819 :   if (!type && TREE_CODE (inner) == TYPE_DECL)
    8179             :     {
    8180      297116 :       tdef_flags = u ();
    8181      297116 :       if (tdef_flags & 1)
    8182      294897 :         is_typedef = true;
    8183             :     }
    8184             : 
    8185     1534819 :   if (is_new)
    8186             :     {
    8187             :       /* A newly discovered node.  */
    8188      787340 :       if (TREE_CODE (decl) == FUNCTION_DECL && DECL_VIRTUAL_P (decl))
    8189             :         /* Mark this identifier as naming a virtual function --
    8190             :            lookup_overrides relies on this optimization.  */
    8191        7158 :         IDENTIFIER_VIRTUAL_P (DECL_NAME (decl)) = true;
    8192             : 
    8193      787340 :       if (installed)
    8194             :         {
    8195             :           /* Mark the entity as imported.  */
    8196      500458 :           retrofit_lang_decl (inner);
    8197      500458 :           DECL_MODULE_IMPORT_P (inner) = true;
    8198             :         }
    8199             : 
    8200      787340 :       if (spec.spec)
    8201       11286 :         set_constraints (decl, spec.spec);
    8202             : 
    8203      787340 :       if (TREE_CODE (decl) == INTEGER_CST && !TREE_OVERFLOW (decl))
    8204             :         {
    8205           0 :           decl = cache_integer_cst (decl, true);
    8206           0 :           back_refs[~tag] = decl;
    8207             :         }
    8208             : 
    8209      787340 :       if (is_typedef)
    8210             :         {
    8211             :           /* Frob it to be ready for cloning.  */
    8212      143891 :           TREE_TYPE (inner) = DECL_ORIGINAL_TYPE (inner);
    8213      143891 :           DECL_ORIGINAL_TYPE (inner) = NULL_TREE;
    8214      143891 :           set_underlying_type (inner);
    8215      143891 :           if (tdef_flags & 2)
    8216             :             {
    8217             :               /* Match instantiate_alias_template's handling.  */
    8218       25397 :               tree type = TREE_TYPE (inner);
    8219       25397 :               TYPE_DEPENDENT_P (type) = true;
    8220       25397 :               TYPE_DEPENDENT_P_VALID (type) = true;
    8221       25397 :               SET_TYPE_STRUCTURAL_EQUALITY (type);
    8222             :             }
    8223             :         }
    8224             : 
    8225      787340 :       if (inner_tag)
    8226             :         /* Set the TEMPLATE_DECL's type.  */
    8227      173665 :         TREE_TYPE (decl) = TREE_TYPE (inner);
    8228             : 
    8229             :       /* Add to specialization tables now that constraints etc are
    8230             :          added.  */
    8231      787340 :       if (mk == MK_partial)
    8232             :         {
    8233        8006 :           bool is_type = TREE_CODE (inner) == TYPE_DECL;
    8234        8006 :           spec.spec = is_type ? type : inner;
    8235        8006 :           add_mergeable_specialization (!is_type, false,
    8236             :                                         &spec, decl, spec_flags);
    8237             :         }
    8238      779334 :       else if (mk & MK_template_mask)
    8239             :         {
    8240      219779 :           bool is_type = !(mk & MK_tmpl_decl_mask);
    8241      219779 :           spec.spec = is_type ? type : mk & MK_tmpl_tmpl_mask ? inner : decl;
    8242      219779 :           add_mergeable_specialization (!is_type,
    8243      219779 :                                         !is_type && mk & MK_tmpl_alias_mask,
    8244             :                                         &spec, decl, spec_flags);
    8245             :         }
    8246             : 
    8247      787340 :       if (NAMESPACE_SCOPE_P (decl)
    8248      177845 :           && (mk == MK_named || mk == MK_unique
    8249      177845 :               || mk == MK_enum || mk == MK_friend_spec)
    8250      874472 :           && !(VAR_OR_FUNCTION_DECL_P (decl) && DECL_LOCAL_DECL_P (decl)))
    8251       87028 :         add_module_namespace_decl (CP_DECL_CONTEXT (decl), decl);
    8252             : 
    8253      787340 :       if (DECL_ARTIFICIAL (decl)
    8254      200596 :           && TREE_CODE (decl) == FUNCTION_DECL
    8255       17611 :           && !DECL_TEMPLATE_INFO (decl)
    8256       17599 :           && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
    8257       17475 :           && TYPE_SIZE (DECL_CONTEXT (decl))
    8258      789735 :           && !DECL_THUNK_P (decl))
    8259             :         /* A new implicit member function, when the class is
    8260             :            complete.  This means the importee declared it, and
    8261             :            we must now add it to the class.  Note that implicit
    8262             :            member fns of template instantiations do not themselves
    8263             :            look like templates.  */
    8264        1229 :         if (!install_implicit_member (inner))
    8265           0 :           set_overrun ();
    8266             :     }
    8267             :   else
    8268             :     {
    8269             :       /* DECL is the to-be-discarded decl.  Its internal pointers will
    8270             :          be to the EXISTING's structure.  Frob it to point to its
    8271             :          own other structures, so loading its definition will alter
    8272             :          it, and not the existing decl.  */
    8273      748809 :       dump (dumper::MERGE) && dump ("Deduping %N", existing);
    8274             : 
    8275      747479 :       if (inner_tag)
    8276      242569 :         DECL_TEMPLATE_RESULT (decl) = inner;
    8277             : 
    8278      747479 :       if (type)
    8279             :         {
    8280             :           /* Point at the to-be-discarded type & decl.  */
    8281      136186 :           TYPE_NAME (type) = inner;
    8282      136186 :           TREE_TYPE (inner) = type;
    8283             : 
    8284      271820 :           TYPE_STUB_DECL (type) = stub_decl ? stub_decl : inner;
    8285      136186 :           if (stub_decl)
    8286         552 :             TREE_TYPE (stub_decl) = type;
    8287             :         }
    8288             : 
    8289      747479 :       if (inner_tag)
    8290             :         /* Set the TEMPLATE_DECL's type.  */
    8291      242569 :         TREE_TYPE (decl) = TREE_TYPE (inner);
    8292             : 
    8293      747479 :       if (!is_matching_decl (existing, decl, is_typedef))
    8294           9 :         unmatched_duplicate (existing);
    8295             : 
    8296      747479 :       if (TREE_CODE (inner) == FUNCTION_DECL)
    8297             :         {
    8298      348619 :           tree e_inner = STRIP_TEMPLATE (existing);
    8299      348619 :           for (auto parm = DECL_ARGUMENTS (inner);
    8300     1067405 :                parm; parm = DECL_CHAIN (parm))
    8301      718786 :             DECL_CONTEXT (parm) = e_inner;
    8302             :         }
    8303             : 
    8304             :       /* And our result is the existing node.  */
    8305             :       decl = existing;
    8306             :     }
    8307             : 
    8308     1534819 :   if (mk == MK_friend_spec)
    8309             :     {
    8310           0 :       tree e = match_mergeable_specialization (true, &spec);
    8311           0 :       if (!e)
    8312             :         {
    8313           0 :           spec.spec = inner;
    8314           0 :           add_mergeable_specialization (true, false, &spec, decl, spec_flags);
    8315             :         }
    8316           0 :       else if (e != existing)
    8317           0 :         set_overrun ();
    8318             :     }
    8319             : 
    8320     1534819 :   if (is_typedef)
    8321             :     {
    8322             :       /* Insert the type into the array now.  */
    8323      294897 :       tag = insert (TREE_TYPE (decl));
    8324      294897 :       dump (dumper::TREE)
    8325         243 :         && dump ("Cloned:%d typedef %C:%N",
    8326         243 :                  tag, TREE_CODE (TREE_TYPE (decl)), TREE_TYPE (decl));
    8327             :     }
    8328             : 
    8329     1534819 :   unused = saved_unused;
    8330             : 
    8331     1534819 :   if (DECL_MAYBE_IN_CHARGE_CDTOR_P (decl))
    8332             :     {
    8333      120459 :       unsigned flags = u ();
    8334             : 
    8335      120459 :       if (is_new)
    8336             :         {
    8337       53345 :           bool cloned_p = flags & 1;
    8338       53441 :           dump (dumper::TREE) && dump ("CDTOR %N is %scloned",
    8339             :                                        decl, cloned_p ? "" : "not ");
    8340       53345 :           if (cloned_p)
    8341       76262 :             build_cdtor_clones (decl, flags & 2, flags & 4,
    8342             :                                 /* Update the member vec, if there is
    8343             :                                    one (we're in a different cluster
    8344             :                                    to the class defn).  */
    8345       38131 :                                 CLASSTYPE_MEMBER_VEC (DECL_CONTEXT (decl)));
    8346             :         }
    8347             :     }
    8348             : 
    8349     1534819 :   if (!NAMESPACE_SCOPE_P (inner)
    8350     1093549 :       && ((TREE_CODE (inner) == TYPE_DECL
    8351      290779 :            && !is_typedef
    8352       47957 :            && TYPE_NAME (TREE_TYPE (inner)) == inner)
    8353     1047811 :           || TREE_CODE (inner) == FUNCTION_DECL)
    8354     2006863 :       && u ())
    8355           0 :     read_definition (decl);
    8356             : 
    8357             :   return decl;
    8358             : }
    8359             : 
    8360             : /* DECL is an unnameable member of CTX.  Return a suitable identifying
    8361             :    index.  */
    8362             : 
    8363             : static unsigned
    8364        1020 : get_field_ident (tree ctx, tree decl)
    8365             : {
    8366        1020 :   gcc_checking_assert (TREE_CODE (decl) == USING_DECL
    8367             :                        || !DECL_NAME (decl)
    8368             :                        || IDENTIFIER_ANON_P (DECL_NAME (decl)));
    8369             : 
    8370        1020 :   unsigned ix = 0;
    8371        1020 :   for (tree fields = TYPE_FIELDS (ctx);
    8372        8592 :        fields; fields = DECL_CHAIN (fields))
    8373             :     {
    8374        8592 :       if (fields == decl)
    8375        1020 :         return ix;
    8376             : 
    8377        7572 :       if (DECL_CONTEXT (fields) == ctx
    8378        7572 :           && (TREE_CODE (fields) == USING_DECL
    8379        7566 :               || (TREE_CODE (fields) == FIELD_DECL
    8380          18 :                   && (!DECL_NAME (fields)
    8381           0 :                       || IDENTIFIER_ANON_P (DECL_NAME (fields))))))
    8382             :         /* Count this field.  */
    8383          18 :         ix++;
    8384             :     }
    8385           0 :   gcc_unreachable ();
    8386             : }
    8387             : 
    8388             : static tree
    8389        1251 : lookup_field_ident (tree ctx, unsigned ix)
    8390             : {
    8391        1251 :   for (tree fields = TYPE_FIELDS (ctx);
    8392        9510 :        fields; fields = DECL_CHAIN (fields))
    8393        9510 :     if (DECL_CONTEXT (fields) == ctx
    8394        9510 :         && (TREE_CODE (fields) == USING_DECL
    8395        9504 :             || (TREE_CODE (fields) == FIELD_DECL
    8396        1278 :                 && (!DECL_NAME (fields)
    8397           0 :                     || IDENTIFIER_ANON_P (DECL_NAME (fields))))))
    8398        1278 :       if (!ix--)
    8399        1251 :         return fields;
    8400             : 
    8401             :   return NULL_TREE;
    8402             : }
    8403             : 
    8404             : /* Reference DECL.  REF indicates the walk kind we are performing.
    8405             :    Return true if we should write this decl by value.  */
    8406             : 
    8407             : bool
    8408    12154614 : trees_out::decl_node (tree decl, walk_kind ref)
    8409             : {
    8410    12154614 :   gcc_checking_assert (DECL_P (decl) && !DECL_TEMPLATE_PARM_P (decl)
    8411             :                        && DECL_CONTEXT (decl));
    8412             : 
    8413    12154614 :   if (ref == WK_value)
    8414             :     {
    8415     1283711 :       depset *dep = dep_hash->find_dependency (decl);
    8416     1283711 :       decl_value (decl, dep);
    8417     1283711 :       return false;
    8418             :     }
    8419             : 
    8420    10870903 :   switch (TREE_CODE (decl))
    8421             :     {
    8422             :     default:
    8423             :       break;
    8424             : 
    8425     1089922 :     case FUNCTION_DECL:
    8426     1089922 :       gcc_checking_assert (!DECL_LOCAL_DECL_P (decl));
    8427             :       break;
    8428             : 
    8429             :     case RESULT_DECL:
    8430             :       /* Unlike PARM_DECLs, RESULT_DECLs are only generated and
    8431             :          referenced when we're inside the function itself.  */
    8432             :       return true;
    8433             : 
    8434      247657 :     case PARM_DECL:
    8435      247657 :       {
    8436      247657 :         if (streaming_p ())
    8437       61688 :           i (tt_parm);
    8438      247657 :         tree_node (DECL_CONTEXT (decl));
    8439      247657 :         if (streaming_p ())
    8440             :           {
    8441             :             /* That must have put this in the map.  */
    8442       61688 :             walk_kind ref = ref_node (decl);
    8443       61688 :             if (ref != WK_none)
    8444             :               // FIXME:OPTIMIZATION We can wander into bits of the
    8445             :               // template this was instantiated from.  For instance
    8446             :               // deferred noexcept and default parms.  Currently we'll
    8447             :               // end up cloning those bits of tree.  It would be nice
    8448             :               // to reference those specific nodes.  I think putting
    8449             :               // those things in the map when we reference their
    8450             :               // template by name.  See the note in add_indirects.
    8451             :               return true;
    8452             : 
    8453           0 :             dump (dumper::TREE)
    8454           0 :               && dump ("Wrote %s reference %N",
    8455           0 :                        TREE_CODE (decl) == PARM_DECL ? "parameter" : "result",
    8456             :                        decl);
    8457             :           }
    8458             :       }
    8459             :       return false;
    8460             : 
    8461             :     case IMPORTED_DECL:
    8462             :       /* This describes a USING_DECL to the ME's debug machinery.  It
    8463             :          originates from the fortran FE, and has nothing to do with
    8464             :          C++ modules.  */
    8465             :       return true;
    8466             : 
    8467             :     case LABEL_DECL:
    8468             :       return true;
    8469             : 
    8470       42427 :     case CONST_DECL:
    8471       42427 :       {
    8472             :         /* If I end up cloning enum decls, implementing C++20 using
    8473             :            E::v, this will need tweaking.   */
    8474       42427 :         if (streaming_p ())
    8475        6579 :           i (tt_enum_decl);
    8476       42427 :         tree ctx = DECL_CONTEXT (decl);
    8477       42427 :         gcc_checking_assert (TREE_CODE (ctx) == ENUMERAL_TYPE);
    8478       42427 :         tree_node (ctx);
    8479       42427 :         tree_node (DECL_NAME (decl));
    8480             : 
    8481       42427 :         int tag = insert (decl);
    8482       42427 :         if (streaming_p ())
    8483        6579 :           dump (dumper::TREE)
    8484          21 :             && dump ("Wrote enum decl:%d %C:%N", tag, TREE_CODE (decl), decl);
    8485             :         return false;
    8486             :       }
    8487        4677 :       break;
    8488             : 
    8489        4677 :     case USING_DECL:
    8490        4677 :       if (TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
    8491             :         break;
    8492             :       /* FALLTHROUGH  */
    8493             : 
    8494      124790 :     case FIELD_DECL:
    8495      124790 :       {
    8496      124790 :         if (streaming_p ())
    8497        5792 :           i (tt_data_member);
    8498             : 
    8499      124790 :         tree ctx = DECL_CONTEXT (decl);
    8500      124790 :         tree_node (ctx);
    8501             : 
    8502      124790 :         tree name = NULL_TREE;
    8503             : 
    8504      124790 :         if (TREE_CODE (decl) == USING_DECL)
    8505             :           ;
    8506             :         else
    8507             :           {
    8508      123729 :             name = DECL_NAME (decl);
    8509      240397 :             if (name && IDENTIFIER_ANON_P (name))
    8510             :               name = NULL_TREE;
    8511             :           }
    8512             : 
    8513      124790 :         tree_node (name);
    8514      124790 :         if (!name && streaming_p ())
    8515             :           {
    8516        1020 :             unsigned ix = get_field_ident (ctx, decl);
    8517        1020 :             u (ix);
    8518             :           }
    8519             : 
    8520      124790 :         int tag = insert (decl);
    8521      124790 :         if (streaming_p ())
    8522        5792 :           dump (dumper::TREE)
    8523          21 :             && dump ("Wrote member:%d %C:%N", tag, TREE_CODE (decl), decl);
    8524             :         return false;
    8525             :       }
    8526      399152 :       break;
    8527             : 
    8528      399152 :     case VAR_DECL:
    8529      399152 :       gcc_checking_assert (!DECL_LOCAL_DECL_P (decl));
    8530      399152 :       if (DECL_VTABLE_OR_VTT_P (decl))
    8531             :         {
    8532             :           /* VTT or VTABLE, they are all on the vtables list.  */
    8533        1399 :           tree ctx = CP_DECL_CONTEXT (decl);
    8534        1399 :           tree vtable = CLASSTYPE_VTABLES (ctx);
    8535        1399 :           for (unsigned ix = 0; ; vtable = DECL_CHAIN (vtable), ix++)
    8536        1399 :             if (vtable == decl)
    8537             :               {
    8538        1399 :                 gcc_checking_assert (DECL_VIRTUAL_P (decl));
    8539        1399 :                 if (streaming_p ())
    8540             :                   {
    8541           9 :                     u (tt_vtable);
    8542           9 :                     u (ix);
    8543           9 :                     dump (dumper::TREE)
    8544           0 :                       && dump ("Writing vtable %N[%u]", ctx, ix);
    8545             :                   }
    8546        1399 :                 tree_node (ctx);
    8547        1399 :                 return false;
    8548             :               }
    8549             :           gcc_unreachable ();
    8550             :         }
    8551             : 
    8552      397753 :       if (DECL_TINFO_P (decl))
    8553             :         {
    8554        7305 :         tinfo:
    8555             :           /* A typeinfo, tt_tinfo_typedef or tt_tinfo_var.  */
    8556       13326 :           bool is_var = VAR_P (decl);
    8557       13326 :           tree type = TREE_TYPE (decl);
    8558       13326 :           unsigned ix = get_pseudo_tinfo_index (type);
    8559       13326 :           if (streaming_p ())
    8560             :             {
    8561        8353 :               i (is_var ? tt_tinfo_var : tt_tinfo_typedef);
    8562        5957 :               u (ix);
    8563             :             }
    8564             : 
    8565       13326 :           if (is_var)
    8566             :             {
    8567             :               /* We also need the type it is for and mangled name, so
    8568             :                  the reader doesn't need to complete the type (which
    8569             :                  would break section ordering).  The type it is for is
    8570             :                  stashed on the name's TREE_TYPE.  */
    8571        7305 :               tree name = DECL_NAME (decl);
    8572        7305 :               tree_node (name);
    8573        7305 :               type = TREE_TYPE (name);
    8574        7305 :               tree_node (type);
    8575             :             }
    8576             : 
    8577       13326 :           int tag = insert (decl);
    8578       13326 :           if (streaming_p ())
    8579        5957 :             dump (dumper::TREE)
    8580          27 :               && dump ("Wrote tinfo_%s:%d %u %N", is_var ? "var" : "type",
    8581             :                        tag, ix, type);
    8582             : 
    8583       13326 :           if (!is_var)
    8584             :             {
    8585        6021 :               tag = insert (type);
    8586        6021 :               if (streaming_p ())
    8587        2396 :                 dump (dumper::TREE)
    8588           9 :                   && dump ("Wrote tinfo_type:%d %u %N", tag, ix, type);
    8589             :             }
    8590       13326 :           return false;
    8591             :         }
    8592             : 
    8593      390448 :       if (DECL_NTTP_OBJECT_P (decl))
    8594             :         {
    8595             :           /* A NTTP parm object.  */
    8596          10 :           if (streaming_p ())
    8597           2 :             i (tt_nttp_var);
    8598          10 :           tree_node (tparm_object_argument (decl));
    8599          10 :           tree_node (DECL_NAME (decl));
    8600          10 :           int tag = insert (decl);
    8601          10 :           if (streaming_p ())
    8602           2 :             dump (dumper::TREE)
    8603           0 :               && dump ("Wrote nttp object:%d %N", tag, DECL_NAME (decl));
    8604          10 :           return false;
    8605             :         }
    8606             : 
    8607             :       break;
    8608             : 
    8609     4049927 :     case TYPE_DECL:
    8610     4049927 :       if (DECL_TINFO_P (decl))
    8611        6021 :         goto tinfo;
    8612             :       break;
    8613             :     }
    8614             : 
    8615     9890920 :   if (DECL_THUNK_P (decl))
    8616             :     {
    8617             :       /* Thunks are similar to binfos -- write the thunked-to decl and
    8618             :          then thunk-specific key info.  */
    8619           0 :       if (streaming_p ())
    8620             :         {
    8621           0 :           i (tt_thunk);
    8622           0 :           i (THUNK_FIXED_OFFSET (decl));
    8623             :         }
    8624             : 
    8625             :       tree target = decl;
    8626           0 :       while (DECL_THUNK_P (target))
    8627           0 :         target = THUNK_TARGET (target);
    8628           0 :       tree_node (target);
    8629           0 :       tree_node (THUNK_VIRTUAL_OFFSET (decl));
    8630           0 :       int tag = insert (decl);
    8631           0 :       if (streaming_p ())
    8632           0 :         dump (dumper::TREE)
    8633           0 :           && dump ("Wrote:%d thunk %N to %N", tag, DECL_NAME (decl), target);
    8634           0 :       return false;
    8635             :     }
    8636             : 
    8637     9890920 :   if (DECL_CLONED_FUNCTION_P (decl))
    8638             :     {
    8639      318962 :       tree target = get_clone_target (decl);
    8640      318962 :       if (streaming_p ())
    8641      148457 :         i (tt_clone_ref);
    8642             : 
    8643      318962 :       tree_node (target);
    8644      318962 :       tree_node (DECL_NAME (decl));
    8645      318962 :       int tag = insert (decl);
    8646      318962 :       if (streaming_p ())
    8647      148457 :         dump (dumper::TREE)
    8648         156 :           && dump ("Wrote:%d clone %N of %N", tag, DECL_NAME (decl), target);
    8649      318962 :       return false;
    8650             :     }
    8651             : 
    8652             :   /* Everything left should be a thing that is in the entity table.
    8653             :      Mostly things that can be defined outside of their (original
    8654             :      declaration) context.  */
    8655     9571958 :   gcc_checking_assert (TREE_CODE (decl) == TEMPLATE_DECL
    8656             :                        || VAR_P (decl)
    8657             :                        || TREE_CODE (decl) == FUNCTION_DECL
    8658             :                        || TREE_CODE (decl) == TYPE_DECL
    8659             :                        || TREE_CODE (decl) == USING_DECL
    8660             :                        || TREE_CODE (decl) == CONCEPT_DECL
    8661             :                        || TREE_CODE (decl) == NAMESPACE_DECL);
    8662             : 
    8663     9571958 :   int use_tpl = -1;
    8664     9571958 :   tree ti = node_template_info (decl, use_tpl);
    8665     9571958 :   tree tpl = NULL_TREE;
    8666             : 
    8667             :   /* If this is the TEMPLATE_DECL_RESULT of a TEMPLATE_DECL, get the
    8668             :      TEMPLATE_DECL.  Note TI_TEMPLATE is not a TEMPLATE_DECL for
    8669             :      (some) friends, so we need to check that.  */
    8670             :   // FIXME: Should local friend template specializations be by value?
    8671             :   // They don't get idents so we'll never know they're imported, but I
    8672             :   // think we can only reach them from the TU that defines the
    8673             :   // befriending class?
    8674     3769407 :   if (ti && TREE_CODE (TI_TEMPLATE (ti)) == TEMPLATE_DECL
    8675    13341324 :       && DECL_TEMPLATE_RESULT (TI_TEMPLATE (ti)) == decl)
    8676             :     {
    8677             :       tpl = TI_TEMPLATE (ti);
    8678      953774 :     partial_template:
    8679      953774 :       if (streaming_p ())
    8680             :         {
    8681        2638 :           i (tt_template);
    8682        2638 :           dump (dumper::TREE)
    8683           9 :             && dump ("Writing implicit template %C:%N%S",
    8684           9 :                      TREE_CODE (tpl), tpl, tpl);
    8685             :         }
    8686      953774 :       tree_node (tpl);
    8687             : 
    8688             :       /* Streaming TPL caused us to visit DECL and maybe its type.  */
    8689      953774 :       gcc_checking_assert (TREE_VISITED (decl));
    8690      953774 :       if (DECL_IMPLICIT_TYPEDEF_P (decl))
    8691      487175 :         gcc_checking_assert (TREE_VISITED (TREE_TYPE (decl)));
    8692             :       return false;
    8693             :     }
    8694             : 
    8695     8736484 :   tree ctx = CP_DECL_CONTEXT (decl);
    8696     8736484 :   depset *dep = NULL;
    8697     8736484 :   if (streaming_p ())
    8698     1426483 :     dep = dep_hash->find_dependency (decl);
    8699     7310001 :   else if (TREE_CODE (ctx) != FUNCTION_DECL
    8700      215059 :            || TREE_CODE (decl) == TEMPLATE_DECL
    8701      194676 :            || (dep_hash->sneakoscope && DECL_IMPLICIT_TYPEDEF_P (decl))
    8702     7490299 :            || (DECL_LANG_SPECIFIC (decl)
    8703      139756 :                && DECL_MODULE_IMPORT_P (decl)))
    8704             :     {
    8705     7129703 :       auto kind = (TREE_CODE (decl) == NAMESPACE_DECL
    8706      626839 :                    && !DECL_NAMESPACE_ALIAS (decl)
    8707     7129703 :                    ? depset::EK_NAMESPACE : depset::EK_DECL);
    8708     7129703 :       dep = dep_hash->add_dependency (decl, kind);
    8709             :     }
    8710             : 
    8711     8556186 :   if (!dep)
    8712             :     {
    8713             :       /* Some internal entity of context.  Do by value.  */
    8714      349974 :       decl_value (decl, NULL);
    8715      349974 :       return false;
    8716             :     }
    8717             : 
    8718     8386510 :   if (dep->get_entity_kind () == depset::EK_REDIRECT)
    8719             :     {
    8720             :       /* The DECL_TEMPLATE_RESULT of a partial specialization.
    8721             :          Write the partial specialization's template.  */
    8722      118300 :       depset *redirect = dep->deps[0];
    8723      118300 :       gcc_checking_assert (redirect->get_entity_kind () == depset::EK_PARTIAL);
    8724      118300 :       tpl = redirect->get_entity ();
    8725      118300 :       goto partial_template;
    8726             :     }
    8727             : 
    8728     8268210 :   if (streaming_p ())
    8729             :     {
    8730             :       /* Locate the entity.  */
    8731     1256807 :       unsigned index = dep->cluster;
    8732     1256807 :       unsigned import = 0;
    8733             : 
    8734     1256807 :       if (dep->is_import ())
    8735         659 :         import = dep->section;
    8736     1256148 :       else if (CHECKING_P)
    8737             :         /* It should be what we put there.  */
    8738     1256148 :         gcc_checking_assert (index == ~import_entity_index (decl));
    8739             : 
    8740             : #if CHECKING_P
    8741         659 :       gcc_assert (!import || importedness >= 0);
    8742             : #endif
    8743     1256807 :       i (tt_entity);
    8744     1256807 :       u (import);
    8745     1256807 :       u (index);
    8746             :     }
    8747             : 
    8748     8268210 :   int tag = insert (decl);
    8749     8268210 :   if (streaming_p () && dump (dumper::TREE))
    8750             :     {
    8751         513 :       char const *kind = "import";
    8752         513 :       module_state *from = (*modules)[0];
    8753         513 :       if (dep->is_import ())
    8754             :         /* Rediscover the unremapped index.  */
    8755         105 :         from = import_entity_module (import_entity_index (decl));
    8756             :       else
    8757             :         {
    8758         408 :           tree o = get_originating_module_decl (decl);
    8759         408 :           o = STRIP_TEMPLATE (o);
    8760         816 :           kind = (DECL_LANG_SPECIFIC (o) && DECL_MODULE_PURVIEW_P (o)
    8761         408 :                   ? "purview" : "GMF");
    8762             :         }
    8763         513 :       dump ("Wrote %s:%d %C:%N@%M", kind,
    8764         513 :             tag, TREE_CODE (decl), decl, from);
    8765             :     }
    8766             : 
    8767     8268210 :   add_indirects (decl);
    8768             : 
    8769     8268210 :   return false;
    8770             : }
    8771             : 
    8772             : void
    8773    10085933 : trees_out::type_node (tree type)
    8774             : {
    8775    10085933 :   gcc_assert (TYPE_P (type));
    8776             : 
    8777    10085933 :   tree root = (TYPE_NAME (type)
    8778    10085933 :                ? TREE_TYPE (TYPE_NAME (type)) : TYPE_MAIN_VARIANT (type));
    8779             : 
    8780    10085933 :   if (type != root)
    8781             :     {
    8782     2252011 :       if (streaming_p ())
    8783      476691 :         i (tt_variant_type);
    8784     2252011 :       tree_node (root);
    8785             : 
    8786     2252011 :       int flags = -1;
    8787             : 
    8788     2252011 :       if (TREE_CODE (type) == FUNCTION_TYPE
    8789     2252011 :           || TREE_CODE (type) == METHOD_TYPE)
    8790             :         {
    8791      554129 :           int quals = type_memfn_quals (type);
    8792      554129 :           int rquals = type_memfn_rqual (type);
    8793      554129 :           tree raises = TYPE_RAISES_EXCEPTIONS (type);
    8794      554129 :           bool late = TYPE_HAS_LATE_RETURN_TYPE (type);
    8795             : 
    8796      554129 :           if (raises != TYPE_RAISES_EXCEPTIONS (root)
    8797       14931 :               || rquals != type_memfn_rqual (root)
    8798       11071 :               || quals != type_memfn_quals (root)
    8799      564912 :               || late != TYPE_HAS_LATE_RETURN_TYPE (root))
    8800      554129 :             flags = rquals | (int (late) << 2) | (quals << 3);
    8801             :         }
    8802             :       else
    8803             :         {
    8804     1697882 :           if (TYPE_USER_ALIGN (type))
    8805       17957 :             flags = TYPE_ALIGN_RAW (type);
    8806             :         }
    8807             : 
    8808     2252011 :       if (streaming_p ())
    8809      476691 :         i (flags);
    8810             : 
    8811     2252011 :       if (flags < 0)
    8812             :         ;
    8813      572086 :       else if (TREE_CODE (type) == FUNCTION_TYPE
    8814      572086 :                || TREE_CODE (type) == METHOD_TYPE)
    8815             :         {
    8816      554129 :           tree raises = TYPE_RAISES_EXCEPTIONS (type);
    8817      554129 :           if (raises == TYPE_RAISES_EXCEPTIONS (root))
    8818       14931 :             raises = error_mark_node;
    8819      554129 :           tree_node (raises);
    8820             :         }
    8821             : 
    8822     2252011 :       tree_node (TYPE_ATTRIBUTES (type));
    8823             : 
    8824     2252011 :       if (streaming_p ())
    8825             :         {
    8826             :           /* Qualifiers.  */
    8827      476691 :           int rquals = cp_type_quals (root);
    8828      476691 :           int quals = cp_type_quals (type);
    8829      476691 :           if (quals == rquals)
    8830      235586 :             quals = -1;
    8831      476691 :           i (quals);
    8832             :         }
    8833             : 
    8834     2252011 :       if (ref_node (type) != WK_none)
    8835             :         {
    8836     2252011 :           int tag = insert (type);
    8837     2252011 :           if (streaming_p ())
    8838             :             {
    8839      476691 :               i (0);
    8840      476691 :               dump (dumper::TREE)
    8841         198 :                 && dump ("Wrote:%d variant type %C", tag, TREE_CODE (type));
    8842             :             }
    8843             :         }
    8844     2252011 :       return;
    8845             :     }
    8846             : 
    8847     7833922 :   if (tree name = TYPE_NAME (type))
    8848     3211257 :     if ((TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name))
    8849     2501588 :         || DECL_TEMPLATE_PARM_P (name)
    8850     1736817 :         || TREE_CODE (type) == RECORD_TYPE
    8851      299661 :         || TREE_CODE (type) == UNION_TYPE
    8852     3502282 :         || TREE_CODE (type) == ENUMERAL_TYPE)
    8853             :       {
    8854             :         /* We can meet template parms that we didn't meet in the
    8855             :            tpl_parms walk, because we're referring to a derived type
    8856             :            that was previously constructed from equivalent template
    8857             :            parms. */
    8858     2996246 :         if (streaming_p ())
    8859             :           {
    8860      190855 :             i (tt_typedef_type);
    8861      190855 :             dump (dumper::TREE)
    8862          60 :               && dump ("Writing %stypedef %C:%N",
    8863          60 :                        DECL_IMPLICIT_TYPEDEF_P (name) ? "implicit " : "",
    8864          60 :                        TREE_CODE (name), name);
    8865             :           }
    8866     2996246 :         tree_node (name);
    8867     2996246 :         if (streaming_p ())
    8868      190855 :           dump (dumper::TREE) && dump ("Wrote typedef %C:%N%S",
    8869          60 :                                        TREE_CODE (name), name, name);
    8870     2996246 :         gcc_checking_assert (TREE_VISITED (type));
    8871             :         return;
    8872             :       }
    8873             : 
    8874     4837676 :   if (TYPE_PTRMEMFUNC_P (type))
    8875             :     {
    8876             :       /* This is a distinct type node, masquerading as a structure. */
    8877        9408 :       tree fn_type = TYPE_PTRMEMFUNC_FN_TYPE (type);
    8878        9408 :       if (streaming_p ())
    8879        2854 :         i (tt_ptrmem_type);
    8880        9408 :       tree_node (fn_type);
    8881        9408 :       int tag = insert (type);
    8882        9408 :       if (streaming_p ())
    8883        2857 :         dump (dumper::TREE) && dump ("Written:%d ptrmem type", tag);
    8884        9408 :       return;
    8885             :     }
    8886             : 
    8887     4828268 :   if (streaming_p ())
    8888             :     {
    8889     1552164 :       u (tt_derived_type);
    8890     1552164 :       u (TREE_CODE (type));
    8891             :     }
    8892             : 
    8893     4828268 :   tree_node (TREE_TYPE (type));
    8894     4828268 :   switch (TREE_CODE (type))
    8895             :     {
    8896           0 :     default:
    8897             :       /* We should never meet a type here that is indescribable in
    8898             :          terms of other types.  */
    8899           0 :       gcc_unreachable ();
    8900             : 
    8901       66542 :     case ARRAY_TYPE:
    8902       66542 :       tree_node (TYPE_DOMAIN (type));
    8903       66542 :       if (streaming_p ())
    8904             :         /* Dependent arrays are constructed with TYPE_DEPENENT_P
    8905             :            already set.  */
    8906       20851 :         u (TYPE_DEPENDENT_P (type));
    8907             :       break;
    8908             : 
    8909             :     case COMPLEX_TYPE:
    8910             :       /* No additional data.  */
    8911             :       break;
    8912             : 
    8913          12 :     case BOOLEAN_TYPE:
    8914             :       /* A non-standard boolean type.  */
    8915          12 :       if (streaming_p ())
    8916           6 :         u (TYPE_PRECISION (type));
    8917             :       break;
    8918             : 
    8919       63004 :     case INTEGER_TYPE:
    8920       63004 :       if (TREE_TYPE (type))
    8921             :         {
    8922             :           /* A range type (representing an array domain).  */
    8923       60987 :           tree_node (TYPE_MIN_VALUE (type));
    8924       60987 :           tree_node (TYPE_MAX_VALUE (type));
    8925             :         }
    8926             :       else
    8927             :         {
    8928             :           /* A new integral type (representing a bitfield).  */
    8929        2017 :           if (streaming_p ())
    8930             :             {
    8931         478 :               unsigned prec = TYPE_PRECISION (type);
    8932         478 :               bool unsigned_p = TYPE_UNSIGNED (type);
    8933             : 
    8934         478 :               u ((prec << 1) | unsigned_p);
    8935             :             }
    8936             :         }
    8937             :       break;
    8938             : 
    8939     1107710 :     case METHOD_TYPE:
    8940     1107710 :     case FUNCTION_TYPE:
    8941     1107710 :       {
    8942     1107710 :         gcc_checking_assert (type_memfn_rqual (type) == REF_QUAL_NONE);
    8943             : 
    8944     1107710 :         tree arg_types = TYPE_ARG_TYPES (type);
    8945     1107710 :         if (TREE_CODE (type) == METHOD_TYPE)
    8946             :           {
    8947      671161 :             tree_node (TREE_TYPE (TREE_VALUE (arg_types)));
    8948      671161 :             arg_types = TREE_CHAIN (arg_types);
    8949             :           }
    8950     1107710 :         tree_node (arg_types);
    8951             :       }
    8952     1107710 :       break;
    8953             : 
    8954        2370 :     case OFFSET_TYPE:
    8955        2370 :       tree_node (TYPE_OFFSET_BASETYPE (type));
    8956        2370 :       break;
    8957             : 
    8958             :     case POINTER_TYPE:
    8959             :       /* No additional data.  */
    8960             :       break;
    8961             : 
    8962      807154 :     case REFERENCE_TYPE:
    8963      807154 :       if (streaming_p ())
    8964      175974 :         u (TYPE_REF_IS_RVALUE (type));
    8965             :       break;
    8966             : 
    8967      922783 :     case DECLTYPE_TYPE:
    8968      922783 :     case TYPEOF_TYPE:
    8969      922783 :     case DEPENDENT_OPERATOR_TYPE:
    8970      922783 :       tree_node (TYPE_VALUES_RAW (type));
    8971      922783 :       if (TREE_CODE (type) == DECLTYPE_TYPE)
    8972             :         /* We stash a whole bunch of things into decltype's
    8973             :            flags.  */
    8974       57907 :         if (streaming_p ())
    8975       19695 :           tree_node_bools (type);
    8976             :       break;
    8977             : 
    8978         548 :     case TRAIT_TYPE:
    8979         548 :       tree_node (TRAIT_TYPE_KIND_RAW (type));
    8980         548 :       tree_node (TRAIT_TYPE_TYPE1 (type));
    8981         548 :       tree_node (TRAIT_TYPE_TYPE2 (type));
    8982         548 :       break;
    8983             : 
    8984             :     case TYPE_ARGUMENT_PACK:
    8985             :       /* No additional data.  */
    8986             :       break;
    8987             : 
    8988      154415 :     case TYPE_PACK_EXPANSION:
    8989      154415 :       if (streaming_p ())
    8990       63506 :         u (PACK_EXPANSION_LOCAL_P (type));
    8991      308830 :       tree_node (PACK_EXPANSION_PARAMETER_PACKS (type));
    8992      154415 :       tree_node (PACK_EXPANSION_EXTRA_ARGS (type));
    8993      154415 :       break;
    8994             : 
    8995      217122 :     case TYPENAME_TYPE:
    8996      217122 :       {
    8997      217122 :         tree_node (TYPE_CONTEXT (type));
    8998      217122 :         tree_node (DECL_NAME (TYPE_NAME (type)));
    8999      217122 :         tree_node (TYPENAME_TYPE_FULLNAME (type));
    9000      217122 :         if (streaming_p ())
    9001             :           {
    9002       78556 :             enum tag_types tag_type = none_type;
    9003       78556 :             if (TYPENAME_IS_ENUM_P (type))
    9004             :               tag_type = enum_type;
    9005       78556 :             else if (TYPENAME_IS_CLASS_P (type))
    9006           0 :               tag_type = class_type;
    9007       78556 :             u (int (tag_type));
    9008             :           }
    9009             :         }
    9010             :       break;
    9011             : 
    9012         273 :     case UNBOUND_CLASS_TEMPLATE:
    9013         273 :       {
    9014         273 :         tree decl = TYPE_NAME (type);
    9015         273 :         tree_node (DECL_CONTEXT (decl));
    9016         273 :         tree_node (DECL_NAME (decl));
    9017         273 :         tree_node (DECL_TEMPLATE_PARMS (decl));
    9018             :       }
    9019         273 :       break;
    9020             : 
    9021          42 :     case VECTOR_TYPE:
    9022          42 :       if (streaming_p ())
    9023             :         {
    9024          21 :           poly_uint64 nunits = TYPE_VECTOR_SUBPARTS (type);
    9025             :           /* to_constant asserts that only coeff[0] is of interest.  */
    9026          21 :           wu (static_cast<unsigned HOST_WIDE_INT> (nunits.to_constant ()));
    9027             :         }
    9028             :       break;
    9029             :     }
    9030             : 
    9031             :   /* We may have met the type during emitting the above.  */
    9032     4828268 :   if (ref_node (type) != WK_none)
    9033             :     {
    9034     4395830 :       int tag = insert (type);
    9035     4395830 :       if (streaming_p ())
    9036             :         {
    9037     1346476 :           i (0);
    9038     1346476 :           dump (dumper::TREE)
    9039         543 :             && dump ("Wrote:%d derived type %C", tag, TREE_CODE (type));
    9040             :         }
    9041             :     }
    9042             : 
    9043             :   return;
    9044             : }
    9045             : 
    9046             : /* T is (mostly*) a non-mergeable node that must be written by value.
    9047             :    The mergeable case is a BINFO, which are as-if DECLSs.   */
    9048             : 
    9049             : void
    9050    27746334 : trees_out::tree_value (tree t)
    9051             : {
    9052             :   /* We should never be writing a type by value.  tree_type should
    9053             :      have streamed it, or we're going via its TYPE_DECL.  */
    9054    27746334 :   gcc_checking_assert (!TYPE_P (t));
    9055             : 
    9056    27746334 :   if (DECL_P (t))
    9057             :     /* No template, type, var or function, except anonymous
    9058             :        non-context vars.  */
    9059      628827 :     gcc_checking_assert ((TREE_CODE (t) != TEMPLATE_DECL
    9060             :                           && TREE_CODE (t) != TYPE_DECL
    9061             :                           && (TREE_CODE (t) != VAR_DECL
    9062             :                               || (!DECL_NAME (t) && !DECL_CONTEXT (t)))
    9063             :                           && TREE_CODE (t) != FUNCTION_DECL));
    9064             : 
    9065    27746334 :   if (streaming_p ())
    9066             :     {
    9067             :       /* A new node -> tt_node.  */
    9068    10942602 :       tree_val_count++;
    9069    10942602 :       i (tt_node);
    9070    10942602 :       start (t);
    9071    10942602 :       tree_node_bools (t);
    9072             :     }
    9073             : 
    9074    27746334 :   if  (TREE_CODE (t) == TREE_BINFO)
    9075             :     /* Binfos are decl-like and need merging information.  */
    9076      267357 :     binfo_mergeable (t);
    9077             : 
    9078    27746334 :   int tag = insert (t, WK_value);
    9079    27746334 :   if (streaming_p ())
    9080    10942602 :     dump (dumper::TREE)
    9081        2520 :       && dump ("Writing tree:%d %C:%N", tag, TREE_CODE (t), t);
    9082             : 
    9083    27746334 :   tree_node_vals (t);
    9084             : 
    9085    27746334 :   if (streaming_p ())
    9086    10945122 :     dump (dumper::TREE) && dump ("Written tree:%d %C:%N", tag, TREE_CODE (t), t);
    9087    27746334 : }
    9088             : 
    9089             : tree
    9090    13006389 : trees_in::tree_value ()
    9091             : {
    9092    13006389 :   tree t = start ();
    9093    13006389 :   if (!t || !tree_node_bools (t))
    9094           0 :     return NULL_TREE;
    9095             : 
    9096    13006389 :   tree existing = t;
    9097    13006389 :   if (TREE_CODE (t) == TREE_BINFO)
    9098             :     {
    9099      140918 :       tree type;
    9100      140918 :       unsigned ix = binfo_mergeable (&type);
    9101      140918 :       if (TYPE_BINFO (type))
    9102             :         {
    9103             :           /* We already have a definition, this must be a duplicate.  */
    9104       78448 :           dump (dumper::MERGE)
    9105         214 :             && dump ("Deduping binfo %N[%u]", type, ix);
    9106       78448 :           existing = TYPE_BINFO (type);
    9107      117748 :           while (existing && ix--)
    9108       39300 :             existing = TREE_CHAIN (existing);
    9109       78448 :           if (existing)
    9110       78448 :             register_duplicate (t, existing);
    9111             :           else
    9112             :             /* Error, mismatch -- diagnose in read_class_def's
    9113             :                checking.  */
    9114             :             existing = t;
    9115             :         }
    9116             :     }
    9117             : 
    9118             :   /* Insert into map.  */
    9119    13006389 :   int tag = insert (existing);
    9120    13006389 :   dump (dumper::TREE)
    9121        3411 :     && dump ("Reading tree:%d %C", tag, TREE_CODE (t));
    9122             : 
    9123    13006389 :   if (!tree_node_vals (t))
    9124             :     {
    9125           0 :       back_refs[~tag] = NULL_TREE;
    9126           0 :       set_overrun ();
    9127             :       /* Bail.  */
    9128           0 :       return NULL_TREE;
    9129             :     }
    9130             : 
    9131    13009800 :   dump (dumper::TREE) && dump ("Read tree:%d %C:%N", tag, TREE_CODE (t), t);
    9132             : 
    9133    13006389 :   if (TREE_CODE (existing) == INTEGER_CST && !TREE_OVERFLOW (existing))
    9134             :     {
    9135      624076 :       existing = cache_integer_cst (t, true);
    9136      624076 :       back_refs[~tag] = existing;
    9137             :     }
    9138             : 
    9139             :   return existing;
    9140             : }
    9141             : 
    9142             : /* Stream out tree node T.  We automatically create local back
    9143             :    references, which is essentially a single pass lisp
    9144             :    self-referential structure pretty-printer.  */
    9145             : 
    9146             : void
    9147   255931455 : trees_out::tree_node (tree t)
    9148             : {
    9149   255931455 :   dump.indent ();
    9150   255931455 :   walk_kind ref = ref_node (t);
    9151   255931455 :   if (ref == WK_none)
    9152   196787317 :     goto done;
    9153             : 
    9154    59144138 :   if (ref != WK_normal)
    9155     1583830 :     goto skip_normal;
    9156             : 
    9157    57560308 :   if (TREE_CODE (t) == IDENTIFIER_NODE)
    9158             :     {
    9159             :       /* An identifier node -> tt_id, tt_conv_id, tt_anon_id, tt_lambda_id.  */
    9160     7557681 :       int code = tt_id;
    9161     7557681 :       if (IDENTIFIER_ANON_P (t))
    9162       19493 :         code = IDENTIFIER_LAMBDA_P (t) ? tt_lambda_id : tt_anon_id;
    9163     7538188 :       else if (IDENTIFIER_CONV_OP_P (t))
    9164        9089 :         code = tt_conv_id;
    9165             : 
    9166     7557681 :       if (streaming_p ())
    9167     1526538 :         i (code);
    9168             : 
    9169     7557681 :       if (code == tt_conv_id)
    9170             :         {
    9171        9089 :           tree type = TREE_TYPE (t);
    9172        9089 :           gcc_checking_assert (type || t == conv_op_identifier);
    9173        9089 :           tree_node (type);
    9174             :         }
    9175     7548592 :       else if (code == tt_id && streaming_p ())
    9176     1516267 :         str (IDENTIFIER_POINTER (t), IDENTIFIER_LENGTH (t));
    9177             : 
    9178     7557681 :       int tag = insert (t);
    9179     7557681 :       if (streaming_p ())
    9180             :         {
    9181             :           /* We know the ordering of the 4 id tags.  */
    9182     1526538 :           static const char *const kinds[] = 
    9183             :             {"", "conv_op ", "anon ", "lambda "};
    9184     1526538 :           dump (dumper::TREE)
    9185        1005 :             && dump ("Written:%d %sidentifier:%N", tag,
    9186        1002 :                      kinds[code - tt_id],
    9187           3 :                      code == tt_conv_id ? TREE_TYPE (t) : t);
    9188             :         }
    9189     7557681 :       goto done;
    9190             :     }
    9191             : 
    9192    50002627 :   if (TREE_CODE (t) == TREE_BINFO)
    9193             :     {
    9194             :       /* A BINFO -> tt_binfo.
    9195             :          We must do this by reference.  We stream the binfo tree
    9196             :          itself when streaming its owning RECORD_TYPE.  That we got
    9197             :          here means the dominating type is not in this SCC.  */
    9198       55739 :       if (streaming_p ())
    9199        1248 :         i (tt_binfo);
    9200       55739 :       binfo_mergeable (t);
    9201       55739 :       gcc_checking_assert (!TREE_VISITED (t));
    9202       55739 :       int tag = insert (t);
    9203       55739 :       if (streaming_p ())
    9204        1248 :         dump (dumper::TREE) && dump ("Inserting binfo:%d %N", tag, t);
    9205       55739 :       goto done;
    9206             :     }
    9207             : 
    9208    49946888 :   if (TREE_CODE (t) == INTEGER_CST
    9209     3545686 :       && !TREE_OVERFLOW (t)
    9210    53492574 :       && TREE_CODE (TREE_TYPE (t)) == ENUMERAL_TYPE)
    9211             :     {
    9212             :       /* An integral constant of enumeral type.  See if it matches one
    9213             :          of the enumeration values.  */
    9214       22327 :       for (tree values = TYPE_VALUES (TREE_TYPE (t));
    9215      161428 :            values; values = TREE_CHAIN (values))
    9216             :         {
    9217      160322 :           tree decl = TREE_VALUE (values);
    9218      160322 :           if (tree_int_cst_equal (DECL_INITIAL (decl), t))
    9219             :             {
    9220       21221 :               if (streaming_p ())
    9221        5912 :                 u (tt_enum_value);
    9222       21221 :               tree_node (decl);
    9223       21263 :               dump (dumper::TREE) && dump ("Written enum value %N", decl);
    9224       21221 :               goto done;
    9225             :             }
    9226             :         }
    9227             :       /* It didn't match.  We'll write it a an explicit INTEGER_CST
    9228             :          node.  */
    9229             :     }
    9230             : 
    9231    49925667 :   if (TYPE_P (t))
    9232             :     {
    9233    10085933 :       type_node (t);
    9234    10085933 :       goto done;
    9235             :     }
    9236             : 
    9237    39839734 :   if (DECL_P (t))
    9238             :     {
    9239    13022346 :       if (DECL_TEMPLATE_PARM_P (t))
    9240             :         {
    9241     2134678 :           tpl_parm_value (t);
    9242     2134678 :           goto done;
    9243             :         }
    9244             : 
    9245    10887668 :       if (!DECL_CONTEXT (t))
    9246             :         {
    9247             :           /* There are a few cases of decls with no context.  We'll write
    9248             :              these by value, but first assert they are cases we expect.  */
    9249       16765 :           gcc_checking_assert (ref == WK_normal);
    9250       16765 :           switch (TREE_CODE (t))
    9251             :             {
    9252           0 :             default: gcc_unreachable ();
    9253             : 
    9254        4996 :             case LABEL_DECL:
    9255             :               /* CASE_LABEL_EXPRs contain uncontexted LABEL_DECLs.  */
    9256        4996 :               gcc_checking_assert (!DECL_NAME (t));
    9257             :               break;
    9258             : 
    9259         312 :             case VAR_DECL:
    9260             :               /* AGGR_INIT_EXPRs cons up anonymous uncontexted VAR_DECLs.  */
    9261         312 :               gcc_checking_assert (!DECL_NAME (t)
    9262             :                                    && DECL_ARTIFICIAL (t));
    9263             :               break;
    9264             : 
    9265             :             case PARM_DECL:
    9266             :               /* REQUIRES_EXPRs have a tree list of uncontexted
    9267             :                  PARM_DECLS.  It'd be nice if they had a
    9268             :                  distinguishing flag to double check.  */
    9269             :               break;
    9270             :             }
    9271       16765 :           goto by_value;
    9272             :         }
    9273             :     }
    9274             : 
    9275    26817388 :  skip_normal:
    9276    39272121 :   if (DECL_P (t) && !decl_node (t, ref))
    9277    11542552 :     goto done;
    9278             : 
    9279             :   /* Otherwise by value */
    9280    27746334 :  by_value:
    9281    27746334 :   tree_value (t);
    9282             : 
    9283   255931455 :  done:
    9284             :   /* And, breath out.  */
    9285   255931455 :   dump.outdent ();
    9286   255931455 : }
    9287             : 
    9288             : /* Stream in a tree node.  */
    9289             : 
    9290             : tree
    9291   115991613 : trees_in::tree_node (bool is_use)
    9292             : {
    9293   115991613 :   if (get_overrun ())
    9294             :     return NULL_TREE;
    9295             : 
    9296   115991613 :   dump.indent ();
    9297   115991613 :   int tag = i ();
    9298   115991613 :   tree res = NULL_TREE;
    9299   115991613 :   switch (tag)
    9300             :     {
    9301    38318543 :     default:
    9302             :       /* backref, pull it out of the map.  */
    9303    38318543 :       res = back_ref (tag);
    9304    38318543 :       break;
    9305             : 
    9306             :     case tt_null:
    9307             :       /* NULL_TREE.  */
    9308             :       break;
    9309             : 
    9310     8783649 :     case tt_fixed:
    9311             :       /* A fixed ref, find it in the fixed_ref array.   */
    9312     8783649 :       {
    9313     8783649 :         unsigned fix = u ();
    9314     8783649 :         if (fix < (*fixed_trees).length ())
    9315             :           {
    9316     8783649 :             res = (*fixed_trees)[fix];
    9317     8783649 :             dump (dumper::TREE) && dump ("Read fixed:%u %C:%N%S", fix,
    9318        4980 :                                          TREE_CODE (res), res, res);
    9319             :           }
    9320             : 
    9321     8778669 :         if (!res)
    9322           0 :           set_overrun ();
    9323             :       }
    9324             :       break;
    9325             : 
    9326       68984 :     case tt_parm:
    9327       68984 :       {
    9328       68984 :         tree fn = tree_node ();
    9329       68984 :         if (fn && TREE_CODE (fn) == FUNCTION_DECL)
    9330       68984 :           res = tree_node ();
    9331       68984 :         if (res)
    9332       68984 :           dump (dumper::TREE)
    9333          15 :             && dump ("Read %s reference %N",
    9334          15 :                      TREE_CODE (res) == PARM_DECL ? "parameter" : "result",
    9335             :                      res);
    9336             :       }
    9337             :       break;
    9338             : 
    9339    13006389 :     case tt_node:
    9340             :       /* A new node.  Stream it in.  */
    9341    13006389 :       res = tree_value ();
    9342    13006389 :       break;
    9343             : 
    9344     1534819 :     case tt_decl:
    9345             :       /* A new decl.  Stream it in.  */
    9346     1534819 :       res = decl_value ();
    9347     1534819 :       break;
    9348             : 
    9349      548481 :     case tt_tpl_parm:
    9350             :       /* A template parameter.  Stream it in.  */
    9351      548481 :       res = tpl_parm_value ();
    9352      548481 :       break;
    9353             : 
    9354     1723142 :     case tt_id:
    9355             :       /* An identifier node.  */
    9356     1723142 :       {
    9357     1723142 :         size_t l;
    9358     1723142 :         const char *chars = str (&l);
    9359     1723142 :         res = get_identifier_with_length (chars, l);
    9360     1723142 :         int tag = insert (res);
    9361     1723142 :         dump (dumper::TREE)
    9362        1395 :           && dump ("Read identifier:%d %N", tag, res);
    9363             :       }
    9364     1723142 :       break;
    9365             : 
    9366        3305 :     case tt_conv_id:
    9367             :       /* A conversion operator.  Get the type and recreate the
    9368             :          identifier.  */
    9369        3305 :       {
    9370        3305 :         tree type = tree_node ();
    9371        3305 :         if (!get_overrun ())
    9372             :           {
    9373        3305 :             res = type ? make_conv_op_name (type) : conv_op_identifier;
    9374        3305 :             int tag = insert (res);
    9375        3305 :             dump (dumper::TREE)
    9376          27 :               && dump ("Created conv_op:%d %S for %N", tag, res, type);
    9377             :           }
    9378             :       }
    9379             :       break;
    9380             : 
    9381        8232 :     case tt_anon_id:
    9382        8232 :     case tt_lambda_id:
    9383             :       /* An anonymous or lambda id.  */
    9384        8232 :       {
    9385        8232 :         res = make_anon_name ();
    9386        8232 :         if (tag == tt_lambda_id)
    9387        3426 :           IDENTIFIER_LAMBDA_P (res) = true;
    9388        8232 :         int tag = insert (res);
    9389        8235 :         dump (dumper::TREE)
    9390           3 :           && dump ("Read %s identifier:%d %N",
    9391           3 :                    IDENTIFIER_LAMBDA_P (res) ? "lambda" : "anon", tag, res);
    9392             :       }
    9393             :       break;
    9394             : 
    9395      223486 :     case tt_typedef_type:
    9396      223486 :       res = tree_node ();
    9397      223486 :       if (res)
    9398             :         {
    9399      223486 :           dump (dumper::TREE)
    9400          75 :             && dump ("Read %stypedef %C:%N",
    9401          75 :                      DECL_IMPLICIT_TYPEDEF_P (res) ? "implicit " : "",
    9402          75 :                      TREE_CODE (res), res);
    9403      223486 :           res = TREE_TYPE (res);
    9404             :         }
    9405             :       break;
    9406             : 
    9407     1839307 :     case tt_derived_type:
    9408             :       /* A type derived from some other type.  */
    9409     1839307 :       {
    9410     1839307 :         enum tree_code code = tree_code (u ());
    9411     1839307 :         res = tree_node ();
    9412             : 
    9413     1839307 :         switch (code)
    9414             :           {
    9415           0 :           default:
    9416           0 :             set_overrun ();
    9417             :             break;
    9418             : 
    9419       23455 :           case ARRAY_TYPE:
    9420       23455 :             {
    9421       23455 :               tree domain = tree_node ();
    9422       23455 :               int dep = u ();
    9423       23455 :               if (!get_overrun ())
    9424       23455 :                 res = build_cplus_array_type (res, domain, dep);
    9425             :             }
    9426             :             break;
    9427             : 
    9428          84 :           case COMPLEX_TYPE:
    9429          84 :             if (!get_overrun ())
    9430          84 :               res = build_complex_type (res);
    9431             :             break;
    9432             : 
    9433           9 :           case BOOLEAN_TYPE:
    9434           9 :             {
    9435           9 :               unsigned precision = u ();
    9436           9 :               if (!get_overrun ())
    9437           9 :                 res = build_nonstandard_boolean_type (precision);
    9438             :             }
    9439             :             break;
    9440             : 
    9441       21284 :           case INTEGER_TYPE:
    9442       21284 :             if (res)
    9443             :               {
    9444             :                 /* A range type (representing an array domain).  */
    9445       20724 :                 tree min = tree_node ();
    9446       20724 :                 tree max = tree_node ();
    9447             : 
    9448       20724 :                 if (!get_overrun ())
    9449       20724 :                   res = build_range_type (res, min, max);
    9450             :               }
    9451             :             else
    9452             :               {
    9453             :                 /* A new integral type (representing a bitfield).  */
    9454         560 :                 unsigned enc = u ();
    9455         560 :                 if (!get_overrun ())
    9456         560 :                   res = build_nonstandard_integer_type (enc >> 1, enc & 1);
    9457             :               }
    9458             :             break;
    9459             : 
    9460      561606 :           case FUNCTION_TYPE:
    9461      561606 :           case METHOD_TYPE:
    9462      561606 :             {
    9463      561606 :               tree klass =  code == METHOD_TYPE ? tree_node () : NULL_TREE;
    9464      561606 :               tree args = tree_node ();
    9465      561606 :               if (!get_overrun ())
    9466             :                 {
    9467      561606 :                   if (klass)
    9468      332069 :                     res = build_method_type_directly (klass, res, args);
    9469             :                   else
    9470      229537 :                     res = build_function_type (res, args);
    9471             :                 }
    9472             :             }
    9473             :             break;
    9474             : 
    9475         669 :           case OFFSET_TYPE:
    9476         669 :             {
    9477         669 :               tree base = tree_node ();
    9478         669 :               if (!get_overrun ())
    9479         669 :                 res = build_offset_type (base, res);
    9480             :             }
    9481             :             break;
    9482             : 
    9483      253943 :           case POINTER_TYPE:
    9484      253943 :             if (!get_overrun ())
    9485      253943 :               res = build_pointer_type (res);
    9486             :             break;
    9487             : 
    9488      201942 :           case REFERENCE_TYPE:
    9489      201942 :             {
    9490      201942 :               bool rval = bool (u ());
    9491      201942 :               if (!get_overrun ())
    9492      201942 :                 res = cp_build_reference_type (res, rval);
    9493             :             }
    9494             :             break;
    9495             : 
    9496      544257 :           case DECLTYPE_TYPE:
    9497      544257 :           case TYPEOF_TYPE:
    9498      544257 :           case DEPENDENT_OPERATOR_TYPE:
    9499      544257 :             {
    9500      544257 :               tree expr = tree_node ();
    9501      544257 :               if (!get_overrun ())
    9502             :                 {
    9503      544257 :                   res = cxx_make_type (code);
    9504      544257 :                   TYPE_VALUES_RAW (res) = expr;
    9505      544257 :                   if (code == DECLTYPE_TYPE)
    9506       24110 :                     tree_node_bools (res);
    9507      544257 :                   SET_TYPE_STRUCTURAL_EQUALITY (res);
    9508             :                 }
    9509             :             }
    9510             :             break;
    9511             : 
    9512         264 :           case TRAIT_TYPE:
    9513         264 :             {
    9514         264 :               tree kind = tree_node ();
    9515         264 :               tree type1 = tree_node ();
    9516         264 :               tree type2 = tree_node ();
    9517         264 :               if (!get_overrun ())
    9518             :                 {
    9519         264 :                   res = cxx_make_type (TRAIT_TYPE);
    9520         264 :                   TRAIT_TYPE_KIND_RAW (res) = kind;
    9521         264 :                   TRAIT_TYPE_TYPE1 (res) = type1;
    9522         264 :                   TRAIT_TYPE_TYPE2 (res) = type2;
    9523         264 :                   SET_TYPE_STRUCTURAL_EQUALITY (res);
    9524             :                 }
    9525             :             }
    9526             :             break;
    9527             : 
    9528       77365 :           case TYPE_ARGUMENT_PACK:
    9529       77365 :             if (!get_overrun ())
    9530             :               {
    9531       77365 :                 tree pack = cxx_make_type (TYPE_ARGUMENT_PACK);
    9532       77365 :                 ARGUMENT_PACK_ARGS (pack) = res;
    9533             :                 res = pack;
    9534             :               }
    9535             :             break;
    9536             : 
    9537       66967 :           case TYPE_PACK_EXPANSION:
    9538       66967 :             {
    9539       66967 :               bool local = u ();
    9540       66967 :               tree param_packs = tree_node ();
    9541       66967 :               tree extra_args = tree_node ();
    9542       66967 :               if (!get_overrun ())
    9543             :                 {
    9544       66967 :                   tree expn = cxx_make_type (TYPE_PACK_EXPANSION);
    9545       66967 :                   SET_TYPE_STRUCTURAL_EQUALITY (expn);
    9546       66967 :                   PACK_EXPANSION_PATTERN (expn) = res;
    9547      133934 :                   PACK_EXPANSION_PARAMETER_PACKS (expn) = param_packs;
    9548       66967 :                   PACK_EXPANSION_EXTRA_ARGS (expn) = extra_args;
    9549       66967 :                   PACK_EXPANSION_LOCAL_P (expn) = local;
    9550       66967 :                   res = expn;
    9551             :                 }
    9552             :             }
    9553             :             break;
    9554             : 
    9555       87369 :           case TYPENAME_TYPE:
    9556       87369 :             {
    9557       87369 :               tree ctx = tree_node ();
    9558       87369 :               tree name = tree_node ();
    9559       87369 :               tree fullname = tree_node ();
    9560       87369 :               enum tag_types tag_type = tag_types (u ());
    9561             : 
    9562       87369 :               if (!get_overrun ())
    9563       87369 :                 res = build_typename_type (ctx, name, fullname, tag_type);
    9564             :             }
    9565             :             break;
    9566             : 
    9567          63 :           case UNBOUND_CLASS_TEMPLATE:
    9568          63 :             {
    9569          63 :               tree ctx = tree_node ();
    9570          63 :               tree name = tree_node ();
    9571          63 :               tree parms = tree_node ();
    9572             : 
    9573          63 :               if (!get_overrun ())
    9574          63 :                 res = make_unbound_class_template_raw (ctx, name, parms);
    9575             :             }
    9576             :             break;
    9577             : 
    9578          30 :           case VECTOR_TYPE:
    9579          30 :             {
    9580          30 :               unsigned HOST_WIDE_INT nunits = wu ();
    9581          30 :               if (!get_overrun ())
    9582          30 :                 res = build_vector_type (res, static_cast<poly_int64> (nunits));
    9583             :             }
    9584             :             break;
    9585             :           }
    9586             : 
    9587     1839307 :         int tag = i ();
    9588     1839307 :         if (!tag)
    9589             :           {
    9590     1579235 :             tag = insert (res);
    9591     1579235 :             if (res)
    9592     1579235 :               dump (dumper::TREE)
    9593         663 :                 && dump ("Created:%d derived type %C", tag, code);
    9594             :           }
    9595             :         else
    9596      260072 :           res = back_ref (tag);
    9597             :       }
    9598             :       break;
    9599             : 
    9600      562528 :     case tt_variant_type:
    9601             :       /* Variant of some type.  */
    9602      562528 :       {
    9603      562528 :         res = tree_node ();
    9604      562528 :         int flags = i ();
    9605      562528 :         if (get_overrun ())
    9606             :           ;
    9607      562528 :         else if (flags < 0)
    9608             :           /* No change.  */;
    9609      287830 :         else if (TREE_CODE (res) == FUNCTION_TYPE
    9610      287830 :                  || TREE_CODE (res) == METHOD_TYPE)
    9611             :           {
    9612      285306 :             cp_ref_qualifier rqual = cp_ref_qualifier (flags & 3);
    9613      285306 :             bool late = (flags >> 2) & 1;
    9614      285306 :             cp_cv_quals quals = cp_cv_quals (flags >> 3);
    9615             : 
    9616      285306 :             tree raises = tree_node ();
    9617      285306 :             if (raises == error_mark_node)
    9618        8997 :               raises = TYPE_RAISES_EXCEPTIONS (res);
    9619             : 
    9620      285306 :             res = build_cp_fntype_variant (res, rqual, raises, late);
    9621      285306 :             if (TREE_CODE (res) == FUNCTION_TYPE)
    9622      115647 :               res = apply_memfn_quals (res, quals, rqual);
    9623             :           }
    9624             :         else
    9625             :           {
    9626        2524 :             res = build_aligned_type (res, (1u << flags) >> 1);
    9627        2524 :             TYPE_USER_ALIGN (res) = true;
    9628             :           }
    9629             : 
    9630      562528 :         if (tree attribs = tree_node ())
    9631       19405 :           res = cp_build_type_attribute_variant (res, attribs);
    9632             : 
    9633      562528 :         int quals = i ();
    9634      562528 :         if (quals >= 0 && !get_overrun ())
    9635      276719 :           res = cp_build_qualified_type (res, quals);
    9636             : 
    9637      562528 :         int tag = i ();
    9638      562528 :         if (!tag)
    9639             :           {
    9640      562528 :             tag = insert (res);
    9641      562528 :             if (res)
    9642      562528 :               dump (dumper::TREE)
    9643         288 :                 && dump ("Created:%d variant type %C", tag, TREE_CODE (res));
    9644             :           }
    9645             :         else
    9646           0 :           res = back_ref (tag);
    9647             :       }
    9648             :       break;
    9649             : 
    9650        7457 :     case tt_tinfo_var:
    9651        7457 :     case tt_tinfo_typedef:
    9652             :       /* A tinfo var or typedef.  */
    9653        7457 :       {
    9654        7457 :         bool is_var = tag == tt_tinfo_var;
    9655        7457 :         unsigned ix = u ();
    9656        7457 :         tree type = NULL_TREE;
    9657             : 
    9658        7457 :         if (is_var)
    9659             :           {
    9660        4444 :             tree name = tree_node ();
    9661        4444 :             type = tree_node ();
    9662             : 
    9663        4444 :             if (!get_overrun ())
    9664        4444 :               res = get_tinfo_decl_direct (type, name, int (ix));
    9665             :           }
    9666             :         else
    9667             :           {
    9668        3013 :             if (!get_overrun ())
    9669             :               {
    9670        3013 :                 type = get_pseudo_tinfo_type (ix);
    9671        3013 :                 res = TYPE_NAME (type);
    9672             :               }
    9673             :           }
    9674        7457 :         if (res)
    9675             :           {
    9676        7457 :             int tag = insert (res);
    9677        7457 :             dump (dumper::TREE)
    9678          36 :               && dump ("Created tinfo_%s:%d %S:%u for %N",
    9679             :                        is_var ? "var" : "decl", tag, res, ix, type);
    9680        7457 :             if (!is_var)
    9681             :               {
    9682        3013 :                 tag = insert (type);
    9683        3013 :                 dump (dumper::TREE)
    9684          12 :                   && dump ("Created tinfo_type:%d %u %N", tag, ix, type);
    9685             :               }
    9686             :           }
    9687             :       }
    9688             :       break;
    9689             : 
    9690        2672 :     case tt_ptrmem_type:
    9691             :       /* A pointer to member function.  */
    9692        2672 :       {
    9693        2672 :         tree type = tree_node ();
    9694        2672 :         if (type && TREE_CODE (type) == POINTER_TYPE
    9695        5344 :             && TREE_CODE (TREE_TYPE (type)) == METHOD_TYPE)
    9696             :           {
    9697        2672 :             res = build_ptrmemfunc_type (type);
    9698        2672 :             int tag = insert (res);
    9699        2675 :             dump (dumper::TREE) && dump ("Created:%d ptrmem type", tag);
    9700             :           }
    9701             :         else
    9702           0 :           set_overrun ();
    9703             :       }
    9704             :       break;
    9705             : 
    9706           2 :     case tt_nttp_var:
    9707             :       /* An NTTP object. */
    9708           2 :       {
    9709           2 :         tree init = tree_node ();
    9710           2 :         tree name = tree_node ();
    9711           2 :         if (!get_overrun ())
    9712             :           {
    9713           2 :             res = get_template_parm_object (init, name);
    9714           2 :             int tag = insert (res);
    9715           2 :             dump (dumper::TREE)
    9716           0 :               && dump ("Created nttp object:%d %N", tag, name);
    9717             :           }
    9718             :       }
    9719             :       break;
    9720             : 
    9721        7923 :     case tt_enum_value:
    9722             :       /* An enum const value.  */
    9723        7923 :       {
    9724        7923 :         if (tree decl = tree_node ())
    9725             :           {
    9726        7941 :             dump (dumper::TREE) && dump ("Read enum value %N", decl);
    9727        7923 :             res = DECL_INITIAL (decl);
    9728             :           }
    9729             : 
    9730        7923 :         if (!res)
    9731           0 :           set_overrun ();
    9732             :       }
    9733             :       break;
    9734             : 
    9735        8621 :     case tt_enum_decl:
    9736             :       /* An enum decl.  */
    9737        8621 :       {
    9738        8621 :         tree ctx = tree_node ();
    9739        8621 :         tree name = tree_node ();
    9740             : 
    9741        8621 :         if (!get_overrun ()
    9742        8621 :             && TREE_CODE (ctx) == ENUMERAL_TYPE)
    9743        8621 :           res = find_enum_member (ctx, name);
    9744             : 
    9745        8621 :         if (!res)
    9746           0 :           set_overrun ();
    9747             :         else
    9748             :           {
    9749        8621 :             int tag = insert (res);
    9750        8621 :             dump (dumper::TREE)
    9751          18 :               && dump ("Read enum decl:%d %C:%N", tag, TREE_CODE (res), res);
    9752             :           }
    9753             :       }
    9754             :       break;
    9755             : 
    9756        7928 :     case tt_data_member:
    9757             :       /* A data member.  */
    9758        7928 :       {
    9759        7928 :         tree ctx = tree_node ();
    9760        7928 :         tree name = tree_node ();
    9761             : 
    9762        7928 :         if (!get_overrun ()
    9763        7928 :             && RECORD_OR_UNION_TYPE_P (ctx))
    9764             :           {
    9765        7928 :             if (name)
    9766        6677 :               res = lookup_class_binding (ctx, name);
    9767             :             else
    9768        1251 :               res = lookup_field_ident (ctx, u ());
    9769             : 
    9770        7928 :             if (!res
    9771        7928 :                 || TREE_CODE (res) != FIELD_DECL
    9772       15856 :                 || DECL_CONTEXT (res) != ctx)
    9773             :               res = NULL_TREE;
    9774             :           }
    9775             : 
    9776        7928 :         if (!res)
    9777           0 :           set_overrun ();
    9778             :         else
    9779             :           {
    9780        7928 :             int tag = insert (res);
    9781        7928 :             dump (dumper::TREE)
    9782          21 :               && dump ("Read member:%d %C:%N", tag, TREE_CODE (res), res);
    9783             :           }
    9784             :       }
    9785             :       break;
    9786             : 
    9787        1774 :     case tt_binfo:
    9788             :       /* A BINFO.  Walk the tree of the dominating type.  */
    9789        1774 :       {
    9790        1774 :         tree type;
    9791        1774 :         unsigned ix = binfo_mergeable (&type);
    9792        1774 :         if (type)
    9793             :           {
    9794        1774 :             res = TYPE_BINFO (type);
    9795        1841 :             for (; ix && res; res = TREE_CHAIN (res))
    9796          67 :               ix--;
    9797        1774 :             if (!res)
    9798           0 :               set_overrun ();
    9799             :           }
    9800             : 
    9801        1774 :         if (get_overrun ())
    9802             :           break;
    9803             : 
    9804             :         /* Insert binfo into backreferences.  */
    9805        1774 :         tag = insert (res);
    9806        1774 :         dump (dumper::TREE) && dump ("Read binfo:%d %N", tag, res);
    9807             :       }
    9808        1774 :       break;
    9809             : 
    9810          15 :     case tt_vtable:
    9811          15 :       {
    9812          15 :         unsigned ix = u ();
    9813          15 :         tree ctx = tree_node ();
    9814          15 :         dump (dumper::TREE) && dump ("Reading vtable %N[%u]", ctx, ix);
    9815          15 :         if (TREE_CODE (ctx) == RECORD_TYPE && TYPE_LANG_SPECIFIC (ctx))
    9816          15 :           for (res = CLASSTYPE_VTABLES (ctx); res; res = DECL_CHAIN (res))
    9817          15 :             if (!ix--)
    9818             :               break;
    9819          15 :         if (!res)
    9820           0 :           set_overrun ();
    9821             :       }
    9822             :       break;
    9823             : 
    9824           0 :     case tt_thunk:
    9825           0 :       {
    9826           0 :         int fixed = i ();
    9827           0 :         tree target = tree_node ();
    9828           0 :         tree virt = tree_node ();
    9829             : 
    9830           0 :         for (tree thunk = DECL_THUNKS (target);
    9831           0 :              thunk; thunk = DECL_CHAIN (thunk))
    9832           0 :           if (THUNK_FIXED_OFFSET (thunk) == fixed
    9833           0 :               && !THUNK_VIRTUAL_OFFSET (thunk) == !virt
    9834           0 :               && (!virt
    9835           0 :                   || tree_int_cst_equal (virt, THUNK_VIRTUAL_OFFSET (thunk))))
    9836             :             {
    9837             :               res = thunk;
    9838             :               break;
    9839             :             }
    9840             : 
    9841           0 :         int tag = insert (res);
    9842           0 :         if (res)
    9843           0 :           dump (dumper::TREE)
    9844           0 :             && dump ("Read:%d thunk %N to %N", tag, DECL_NAME (res), target);
    9845             :         else
    9846           0 :           set_overrun ();
    9847             :       }
    9848             :       break;
    9849             : 
    9850      181144 :     case tt_clone_ref:
    9851      181144 :       {
    9852      181144 :         tree target = tree_node ();
    9853      181144 :         tree name = tree_node ();
    9854             : 
    9855      181144 :         if (DECL_P (target) && DECL_MAYBE_IN_CHARGE_CDTOR_P (target))
    9856             :           {
    9857      181144 :             tree clone;
    9858      285798 :             FOR_EVERY_CLONE (clone, target)
    9859      285798 :               if (DECL_NAME (clone) == name)
    9860             :                 {
    9861             :                   res = clone;
    9862             :                   break;
    9863             :                 }
    9864             :           }
    9865             : 
    9866      181144 :         if (!res)
    9867           0 :           set_overrun ();
    9868      181144 :         int tag = insert (res);
    9869      181144 :         if (res)
    9870      181144 :           dump (dumper::TREE)
    9871         222 :             && dump ("Read:%d clone %N of %N", tag, DECL_NAME (res), target);
    9872             :         else
    9873           0 :           set_overrun ();
    9874             :        }
    9875             :       break;
    9876             : 
    9877     1431776 :     case tt_entity:
    9878             :       /* Index into the entity table.  Perhaps not loaded yet!  */
    9879     1431776 :       {
    9880     1431776 :         unsigned origin = state->slurp->remap_module (u ());
    9881     1431776 :         unsigned ident = u ();
    9882     1431776 :         module_state *from = (*modules)[origin];
    9883             : 
    9884     1431776 :         if (!origin || ident >= from->entity_num)
    9885           0 :           set_overrun ();
    9886     1431776 :         if (!get_overrun ())
    9887             :           {
    9888     1431776 :             binding_slot *slot = &(*entity_ary)[from->entity_lwm + ident];
    9889     1431776 :             if (slot->is_lazy ())
    9890       14521 :               if (!from->lazy_load (ident, slot))
    9891           0 :                 set_overrun ();
    9892     1431776 :             res = *slot;
    9893             :           }
    9894             : 
    9895     1431776 :         if (res)
    9896             :           {
    9897     1431776 :             const char *kind = (origin != state->mod ? "Imported" : "Named");
    9898     1431776 :             int tag = insert (res);
    9899     1431776 :             dump (dumper::TREE)
    9900         576 :               && dump ("%s:%d %C:%N@%M", kind, tag, TREE_CODE (res),
    9901         576 :                        res, (*modules)[origin]);
    9902             : 
    9903     1431776 :             if (!add_indirects (res))
    9904             :               {
    9905           0 :                 set_overrun ();
    9906           0 :                 res = NULL_TREE;
    9907             :               }
    9908             :           }
    9909             :       }
    9910             :       break;
    9911             : 
    9912        3216 :     case tt_template:
    9913             :       /* A template.  */
    9914        3216 :       if (tree tpl = tree_node ())
    9915             :         {
    9916        3216 :           res = DECL_TEMPLATE_RESULT (tpl);
    9917        3216 :           dump (dumper::TREE)
    9918           9 :             && dump ("Read template %C:%N", TREE_CODE (res), res);
    9919             :         }
    9920             :       break;
    9921             :     }
    9922             : 
    9923   115991613 :   if (is_use && !unused && res && DECL_P (res) && !TREE_USED (res))
    9924             :     {
    9925             :       /* Mark decl used as mark_used does -- we cannot call
    9926             :          mark_used in the middle of streaming, we only need a subset
    9927             :          of its functionality.   */
    9928      741183 :       TREE_USED (res) = true;
    9929             : 
    9930             :       /* And for structured bindings also the underlying decl.  */
    9931      741183 :       if (DECL_DECOMPOSITION_P (res) && DECL_DECOMP_BASE (res))
    9932         500 :         TREE_USED (DECL_DECOMP_BASE (res)) = true;
    9933             : 
    9934      741183 :       if (DECL_CLONED_FUNCTION_P (res))
    9935        7271 :         TREE_USED (DECL_CLONED_FUNCTION (res)) = true;
    9936             :     }
    9937             : 
    9938   115991613 :   dump.outdent ();
    9939   115991613 :   return res;
    9940             : }
    9941             : 
    9942             : void
    9943     1790235 : trees_out::tpl_parms (tree parms, unsigned &tpl_levels)
    9944             : {
    9945     1790235 :   if (!parms)
    9946             :     return;
    9947             : 
    9948     1209460 :   if (TREE_VISITED (parms))
    9949             :     {
    9950      525798 :       ref_node (parms);
    9951      525798 :       return;
    9952             :     }
    9953             : 
    9954      683662 :   tpl_parms (TREE_CHAIN (parms), tpl_levels);
    9955             : 
    9956      683662 :   tree vec = TREE_VALUE (parms);
    9957      683662 :   unsigned len = TREE_VEC_LENGTH (vec);
    9958             :   /* Depth.  */
    9959      683662 :   int tag = insert (parms);
    9960      683662 :   if (streaming_p ())
    9961             :     {
    9962      188292 :       i (len + 1);
    9963      188358 :       dump (dumper::TREE)
    9964          66 :         && dump ("Writing template parms:%d level:%N length:%d",
    9965          66 :                  tag, TREE_PURPOSE (parms), len);
    9966             :     }
    9967      683662 :   tree_node (TREE_PURPOSE (parms));
    9968             : 
    9969     1934331 :   for (unsigned ix = 0; ix != len; ix++)
    9970             :     {
    9971     1250669 :       tree parm = TREE_VEC_ELT (vec, ix);
    9972     1250669 :       tree decl = TREE_VALUE (parm);
    9973             : 
    9974     1250669 :       gcc_checking_assert (DECL_TEMPLATE_PARM_P (decl));
    9975     1250669 :       if (CHECKING_P)
    9976     1250669 :         switch (TREE_CODE (decl))
    9977             :           {
    9978           0 :           default: gcc_unreachable ();
    9979             : 
    9980        2892 :           case TEMPLATE_DECL:
    9981        2892 :             gcc_assert ((TREE_CODE (TREE_TYPE (decl)) == TEMPLATE_TEMPLATE_PARM)
    9982             :                         && (TREE_CODE (DECL_TEMPLATE_RESULT (decl)) == TYPE_DECL)
    9983             :                         && (TYPE_NAME (TREE_TYPE (decl)) == decl));
    9984             :             break;
    9985             : 
    9986     1162995 :           case TYPE_DECL:
    9987     1162995 :             gcc_assert ((TREE_CODE (TREE_TYPE (decl)) == TEMPLATE_TYPE_PARM)
    9988             :                         && (TYPE_NAME (TREE_TYPE (decl)) == decl));
    9989             :             break;
    9990             : 
    9991       84782 :           case PARM_DECL:
    9992       84782 :             gcc_assert ((TREE_CODE (DECL_INITIAL (decl)) == TEMPLATE_PARM_INDEX)
    9993             :                         && (TREE_CODE (TEMPLATE_PARM_DECL (DECL_INITIAL (decl)))
    9994             :                             == CONST_DECL)
    9995             :                         && (DECL_TEMPLATE_PARM_P
    9996             :                             (TEMPLATE_PARM_DECL (DECL_INITIAL (decl)))));
    9997             :             break;
    9998             :           }
    9999             : 
   10000     1250669 :       tree_node (decl);
   10001     1250669 :       tree_node (TEMPLATE_PARM_CONSTRAINTS (parm));
   10002             :     }
   10003             : 
   10004      683662 :   tpl_levels++;
   10005             : }
   10006             : 
   10007             : tree
   10008      417312 : trees_in::tpl_parms (unsigned &tpl_levels)
   10009             : {
   10010      417312 :   tree parms = NULL_TREE;
   10011             : 
   10012      872265 :   while (int len = i ())
   10013             :     {
   10014      454953 :       if (len < 0)
   10015             :         {
   10016      246538 :           parms = back_ref (len);
   10017      246538 :           continue;
   10018             :         }
   10019             : 
   10020      208415 :       len -= 1;
   10021      208415 :       parms = tree_cons (NULL_TREE, NULL_TREE, parms);
   10022      208415 :       int tag = insert (parms);
   10023      208415 :       TREE_PURPOSE (parms) = tree_node ();
   10024             : 
   10025      208415 :       dump (dumper::TREE)
   10026         105 :         && dump ("Reading template parms:%d level:%N length:%d",
   10027         105 :                  tag, TREE_PURPOSE (parms), len);
   10028             : 
   10029      208415 :       tree vec = make_tree_vec (len);
   10030      575216 :       for (int ix = 0; ix != len; ix++)
   10031             :         {
   10032      366801 :           tree decl = tree_node ();
   10033      366801 :           if (!decl)
   10034             :             return NULL_TREE;
   10035             : 
   10036      366801 :           tree parm = build_tree_list (NULL, decl);
   10037      366801 :           TEMPLATE_PARM_CONSTRAINTS (parm) = tree_node ();
   10038             : 
   10039      366801 :           TREE_VEC_ELT (vec, ix) = parm;
   10040             :         }
   10041             : 
   10042      208415 :       TREE_VALUE (parms) = vec;
   10043      208415 :       tpl_levels++;
   10044             :     }
   10045             : 
   10046             :   return parms;
   10047             : }
   10048             : 
   10049             : void
   10050     1106573 : trees_out::tpl_parms_fini (tree tmpl, unsigned tpl_levels)
   10051             : {
   10052     1106573 :   for (tree parms = DECL_TEMPLATE_PARMS (tmpl);
   10053     1790235 :        tpl_levels--; parms = TREE_CHAIN (parms))
   10054             :     {
   10055      683662 :       tree vec = TREE_VALUE (parms);
   10056             : 
   10057      683662 :       tree_node (TREE_TYPE (vec));
   10058     1934331 :       for (unsigned ix = TREE_VEC_LENGTH (vec); ix--;)
   10059             :         {
   10060     1250669 :           tree parm = TREE_VEC_ELT (vec, ix);
   10061     1250669 :           tree dflt = TREE_PURPOSE (parm);
   10062     1250669 :           tree_node (dflt);
   10063             : 
   10064     1250669 :           if (streaming_p ())
   10065             :             {
   10066      325740 :               tree decl = TREE_VALUE (parm);
   10067      325740 :               if (TREE_CODE (decl) == TEMPLATE_DECL)
   10068             :                 {
   10069         964 :                   tree ctx = DECL_CONTEXT (decl);
   10070         964 :                   tree inner = DECL_TEMPLATE_RESULT (decl);
   10071         964 :                   tree tpi = (TREE_CODE (inner) == TYPE_DECL
   10072         964 :                               ? TEMPLATE_TYPE_PARM_INDEX (TREE_TYPE (decl))
   10073           0 :                               : DECL_INITIAL (inner));
   10074         964 :                   bool original = (TEMPLATE_PARM_LEVEL (tpi)
   10075         964 :                                    == TEMPLATE_PARM_ORIG_LEVEL (tpi));
   10076             :                   /* Original template template parms have a context
   10077             :                      of their owning template.  Reduced ones do not.  */
   10078         964 :                   gcc_checking_assert (original ? ctx == tmpl : !ctx);
   10079             :                 }
   10080             :             }
   10081             :         }
   10082             :     }
   10083     1106573 : }
   10084             : 
   10085             : bool
   10086      417312 : trees_in::tpl_parms_fini (tree tmpl, unsigned tpl_levels)
   10087             : {
   10088      417312 :   for (tree parms = DECL_TEMPLATE_PARMS (tmpl);
   10089      625727 :        tpl_levels--; parms = TREE_CHAIN (parms))
   10090             :     {
   10091      208415 :       tree vec = TREE_VALUE (parms);
   10092             : 
   10093      208415 :       TREE_TYPE (vec) = tree_node ();
   10094      575216 :       for (unsigned ix = TREE_VEC_LENGTH (vec); ix--;)
   10095             :         {
   10096      366801 :           tree parm = TREE_VEC_ELT (vec, ix);
   10097      366801 :           tree dflt = tree_node ();
   10098      366801 :           if (get_overrun ())
   10099             :             return false;
   10100      366801 :           TREE_PURPOSE (parm) = dflt;
   10101             : 
   10102      366801 :           tree decl = TREE_VALUE (parm);
   10103      366801 :           if (TREE_CODE (decl) == TEMPLATE_DECL)
   10104             :             {
   10105         814 :               tree inner = DECL_TEMPLATE_RESULT (decl);
   10106         814 :               tree tpi = (TREE_CODE (inner) == TYPE_DECL
   10107         814 :                           ? TEMPLATE_TYPE_PARM_INDEX (TREE_TYPE (decl))
   10108           0 :                           : DECL_INITIAL (inner));
   10109         814 :               bool original = (TEMPLATE_PARM_LEVEL (tpi)
   10110         814 :                                == TEMPLATE_PARM_ORIG_LEVEL (tpi));
   10111             :               /* Original template template parms have a context
   10112             :                  of their owning template.  Reduced ones do not.  */
   10113         814 :               if (original)
   10114         811 :                 DECL_CONTEXT (decl) = tmpl;
   10115             :             }
   10116             :         }
   10117             :     }
   10118             :   return true;
   10119             : }
   10120             : 
   10121             : /* PARMS is a LIST, one node per level.
   10122             :    TREE_VALUE is a TREE_VEC of parm info for that level.
   10123             :    each ELT is a TREE_LIST
   10124             :    TREE_VALUE is PARM_DECL, TYPE_DECL or TEMPLATE_DECL
   10125             :    TREE_PURPOSE is the default value.  */
   10126             : 
   10127             : void
   10128     1106573 : trees_out::tpl_header (tree tpl, unsigned *tpl_levels)
   10129             : {
   10130     1106573 :   tree parms = DECL_TEMPLATE_PARMS (tpl);
   10131     1106573 :   tpl_parms (parms, *tpl_levels);
   10132             : 
   10133             :   /* Mark end.  */
   10134     1106573 :   if (streaming_p ())
   10135      368480 :     u (0);
   10136             : 
   10137     1106573 :   if (*tpl_levels)
   10138      653946 :     tree_node (TEMPLATE_PARMS_CONSTRAINTS (parms));
   10139     1106573 : }
   10140             : 
   10141             : bool
   10142      417312 : trees_in::tpl_header (tree tpl, unsigned *tpl_levels)
   10143             : {
   10144      417312 :   tree parms = tpl_parms (*tpl_levels);
   10145      417312 :   if (!parms)
   10146             :     return false;
   10147             : 
   10148      417312 :   DECL_TEMPLATE_PARMS (tpl) = parms;
   10149             : 
   10150      417312 :   if (*tpl_levels)
   10151      206629 :     TEMPLATE_PARMS_CONSTRAINTS (parms) = tree_node ();
   10152             : 
   10153             :   return true;
   10154             : }
   10155             : 
   10156             : /* Stream skeleton parm nodes, with their flags, type & parm indices.
   10157             :    All the parms will have consecutive tags.  */
   10158             : 
   10159             : void
   10160     1491202 : trees_out::fn_parms_init (tree fn)
   10161             : {
   10162             :   /* First init them.  */
   10163     1491202 :   int base_tag = ref_num - 1;
   10164     1491202 :   int ix = 0;
   10165     1491202 :   for (tree parm = DECL_ARGUMENTS (fn);
   10166     4609720 :        parm; parm = DECL_CHAIN (parm), ix++)
   10167             :     {
   10168     3118518 :       if (streaming_p ())
   10169             :         {
   10170     1039515 :           start (parm);
   10171     1039515 :           tree_node_bools (parm);
   10172             :         }
   10173     3118518 :       int tag = insert (parm);
   10174     3118518 :       gcc_checking_assert (base_tag - ix == tag);
   10175             :     }
   10176             :   /* Mark the end.  */
   10177     1491202 :   if (streaming_p ())
   10178      497424 :     u (0);
   10179             : 
   10180             :   /* Now stream their contents.  */
   10181     1491202 :   ix = 0;
   10182     1491202 :   for (tree parm = DECL_ARGUMENTS (fn);
   10183     4609720 :        parm; parm = DECL_CHAIN (parm), ix++)
   10184             :     {
   10185     3118518 :       if (streaming_p ())
   10186     1039515 :         dump (dumper::TREE)
   10187         225 :           && dump ("Writing parm:%d %u (%N) of %N",
   10188             :                    base_tag - ix, ix, parm, fn);
   10189     3118518 :       tree_node_vals (parm);
   10190             :     }
   10191             : 
   10192     1491202 :   if (!streaming_p ())
   10193             :     {
   10194             :       /* We must walk contract attrs so the dependency graph is complete. */
   10195      993778 :       for (tree contract = DECL_CONTRACTS (fn);
   10196      993892 :           contract;
   10197         114 :           contract = CONTRACT_CHAIN (contract))
   10198         114 :         tree_node (contract);
   10199             :     }
   10200             : 
   10201             :   /* Write a reference to contracts pre/post functions, if any, to avoid
   10202             :      regenerating them in importers.  */
   10203     1491202 :   tree_node (DECL_PRE_FN (fn));
   10204     1491202 :   tree_node (DECL_POST_FN (fn));
   10205     1491202 : }
   10206             : 
   10207             : /* Build skeleton parm nodes, read their flags, type & parm indices.  */
   10208             : 
   10209             : int
   10210      600440 : trees_in::fn_parms_init (tree fn)
   10211             : {
   10212      600440 :   int base_tag = ~(int)back_refs.length ();
   10213             : 
   10214      600440 :   tree *parm_ptr = &DECL_ARGUMENTS (fn);
   10215      600440 :   int ix = 0;
   10216     1843908 :   for (; int code = u (); ix++)
   10217             :     {
   10218     1243468 :       tree parm = start (code);
   10219     1243468 :       if (!tree_node_bools (parm))
   10220             :         return 0;
   10221             : 
   10222     1243468 :       int tag = insert (parm);
   10223     1243468 :       gcc_checking_assert (base_tag - ix == tag);
   10224     1243468 :       *parm_ptr = parm;
   10225     1243468 :       parm_ptr = &DECL_CHAIN (parm);
   10226     1243468 :     }
   10227             : 
   10228      600440 :   ix = 0;
   10229      600440 :   for (tree parm = DECL_ARGUMENTS (fn);
   10230     1843908 :        parm; parm = DECL_CHAIN (parm), ix++)
   10231             :     {
   10232     1243468 :       dump (dumper::TREE)
   10233         375 :         && dump ("Reading parm:%d %u (%N) of %N",
   10234             :                  base_tag - ix, ix, parm, fn);
   10235     1243468 :       if (!tree_node_vals (parm))
   10236             :         return 0;
   10237             :     }
   10238             : 
   10239             :   /* Reload references to contract functions, if any.  */
   10240      600440 :   tree pre_fn = tree_node ();
   10241      600440 :   tree post_fn = tree_node ();
   10242      600440 :   set_contract_functions (fn, pre_fn, post_fn);
   10243             : 
   10244      600440 :   return base_tag;
   10245             : }
   10246             : 
   10247             : /* Read the remaining parm node data.  Replace with existing (if
   10248             :    non-null) in the map.  */
   10249             : 
   10250             : void
   10251      600440 : trees_in::fn_parms_fini (int tag, tree fn, tree existing, bool is_defn)
   10252             : {
   10253      949059 :   tree existing_parm = existing ? DECL_ARGUMENTS (existing) : NULL_TREE;
   10254      600440 :   tree parms = DECL_ARGUMENTS (fn);
   10255      600440 :   unsigned ix = 0;
   10256     1843908 :   for (tree parm = parms; parm; parm = DECL_CHAIN (parm), ix++)
   10257             :     {
   10258     1243468 :       if (existing_parm)
   10259             :         {
   10260     1031566 :           if (is_defn && !DECL_SAVED_TREE (existing))
   10261             :             {
   10262             :               /* If we're about to become the definition, set the
   10263             :                  names of the parms from us.  */
   10264       22722 :               DECL_NAME (existing_parm) = DECL_NAME (parm);
   10265       22722 :               DECL_SOURCE_LOCATION (existing_parm) = DECL_SOURCE_LOCATION (parm);
   10266             :             }
   10267             : 
   10268      715168 :           back_refs[~tag] = existing_parm;
   10269      715168 :           existing_parm = DECL_CHAIN (existing_parm);
   10270             :         }
   10271     1243468 :       tag--;
   10272             :     }
   10273      600440 : }
   10274             : 
   10275             : /* DEP is the depset of some decl we're streaming by value.  Determine
   10276             :    the merging behaviour.  */
   10277             : 
   10278             : merge_kind
   10279     3669401 : trees_out::get_merge_kind (tree decl, depset *dep)
   10280             : {
   10281     3669401 :   if (!dep)
   10282             :     {
   10283      615836 :       if (VAR_OR_FUNCTION_DECL_P (decl))
   10284             :         {
   10285             :           /* Any var or function with template info should have DEP.  */
   10286      314628 :           gcc_checking_assert (!DECL_LANG_SPECIFIC (decl)
   10287             :                                || !DECL_TEMPLATE_INFO (decl));
   10288      314628 :           if (DECL_LOCAL_DECL_P (decl))
   10289             :             return MK_unique;
   10290             :         }
   10291             : 
   10292             :       /* Either unique, or some member of a class that cannot have an
   10293             :          out-of-class definition.  For instance a FIELD_DECL.  */
   10294      615588 :       tree ctx = CP_DECL_CONTEXT (decl);
   10295      615588 :       if (TREE_CODE (ctx) == FUNCTION_DECL)
   10296             :         {
   10297             :           /* USING_DECLs and NAMESPACE_DECLs cannot have DECL_TEMPLATE_INFO --
   10298             :              this isn't permitting them to have one.   */
   10299      349974 :           gcc_checking_assert (TREE_CODE (decl) == USING_DECL
   10300             :                                || TREE_CODE (decl) == NAMESPACE_DECL
   10301             :                                || !DECL_LANG_SPECIFIC (decl)
   10302             :                                || !DECL_TEMPLATE_INFO (decl));
   10303             : 
   10304             :           return MK_unique;
   10305             :         }
   10306             : 
   10307      265614 :       if (TREE_CODE (decl) == TEMPLATE_DECL
   10308      265614 :           && DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (decl))
   10309             :         return MK_local_friend;
   10310             : 
   10311      265614 :       gcc_checking_assert (TYPE_P (ctx));
   10312      265614 :       if (TREE_CODE (decl) == USING_DECL)
   10313             :         return MK_field;
   10314             : 
   10315      163460 :       if (TREE_CODE (decl) == FIELD_DECL)
   10316             :         {
   10317      118628 :           if (DECL_NAME (decl))
   10318             :             {
   10319             :               /* Anonymous FIELD_DECLs have a NULL name.  */
   10320       88974 :               gcc_checking_assert (!IDENTIFIER_ANON_P (DECL_NAME (decl)));
   10321             :               return MK_named;
   10322             :             }
   10323             : 
   10324       29654 :           if (!DECL_NAME (decl)
   10325       29654 :               && !RECORD_OR_UNION_TYPE_P (TREE_TYPE (decl))
   10326       30796 :               && !DECL_BIT_FIELD_REPRESENTATIVE (decl))
   10327             :             {
   10328             :               /* The underlying storage unit for a bitfield.  We do not
   10329             :                  need to dedup it, because it's only reachable through
   10330             :                  the bitfields it represents.  And those are deduped.  */
   10331             :               // FIXME: Is that assertion correct -- do we ever fish it
   10332             :               // out and put it in an expr?
   10333         258 :               gcc_checking_assert ((TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
   10334             :                                     ? TREE_CODE (TREE_TYPE (TREE_TYPE (decl)))
   10335             :                                     : TREE_CODE (TREE_TYPE (decl)))
   10336             :                                    == INTEGER_TYPE);
   10337             :               return MK_unique;
   10338             :             }
   10339             : 
   10340             :           return MK_field;
   10341             :         }
   10342             : 
   10343       44832 :       if (TREE_CODE (decl) == CONST_DECL)
   10344             :         return MK_named;
   10345             : 
   10346        9824 :       if (TREE_CODE (decl) == VAR_DECL
   10347        9824 :           && DECL_VTABLE_OR_VTT_P (decl))
   10348             :         return MK_vtable;
   10349             : 
   10350        1928 :       if (DECL_THUNK_P (decl))
   10351             :         /* Thunks are unique-enough, because they're only referenced
   10352             :            from the vtable.  And that's either new (so we want the
   10353             :            thunks), or it's a duplicate (so it will be dropped).  */
   10354             :         return MK_unique;
   10355             : 
   10356             :       /* There should be no other cases.  */
   10357           0 :       gcc_unreachable ();
   10358             :     }
   10359             : 
   10360     3053565 :   gcc_checking_assert (TREE_CODE (decl) != FIELD_DECL
   10361             :                        && TREE_CODE (decl) != USING_DECL
   10362             :                        && TREE_CODE (decl) != CONST_DECL);
   10363             : 
   10364     3053565 :   if (is_key_order ())
   10365             :     {
   10366             :       /* When doing the mergeablilty graph, there's an indirection to
   10367             :          the actual depset.  */
   10368     1017849 :       gcc_assert (dep->is_special ());
   10369     1017849 :       dep = dep->deps[0];
   10370             :     }
   10371             : 
   10372     3053565 :   gcc_checking_assert (decl == dep->get_entity ());
   10373             : 
   10374     3053565 :   merge_kind mk = MK_named;
   10375     3053565 :   switch (dep->get_entity_kind ())
   10376             :     {
   10377           0 :     default:
   10378           0 :       gcc_unreachable ();
   10379             : 
   10380             :     case depset::EK_PARTIAL:
   10381             :       mk = MK_partial;
   10382             :       break;
   10383             : 
   10384     1733088 :     case depset::EK_DECL:
   10385     1733088 :       {
   10386     1733088 :         tree ctx = CP_DECL_CONTEXT (decl);
   10387             : 
   10388     1733088 :         switch (TREE_CODE (ctx))
   10389             :           {
   10390           0 :           default:
   10391           0 :             gcc_unreachable ();
   10392             : 
   10393        8340 :           case FUNCTION_DECL:
   10394             :             // FIXME: This can occur for (a) voldemorty TYPE_DECLS
   10395             :             // (which are returned from a function), or (b)
   10396             :             // block-scope class definitions in template functions.
   10397             :             // These are as unique as the containing function.  While
   10398             :             // on read-back we can discover if the CTX was a
   10399             :             // duplicate, we don't have a mechanism to get from the
   10400             :             // existing CTX to the existing version of this decl.
   10401        8340 :             gcc_checking_assert
   10402             :               (DECL_IMPLICIT_TYPEDEF_P (STRIP_TEMPLATE (decl)));
   10403             : 
   10404             :             mk = MK_unique;
   10405             :             break;
   10406             : 
   10407     1106316 :           case RECORD_TYPE:
   10408     1106316 :           case UNION_TYPE:
   10409     1106316 :             if (DECL_NAME (decl) == as_base_identifier)
   10410             :               mk = MK_as_base;
   10411     1031181 :             else if (IDENTIFIER_ANON_P (DECL_NAME (decl)))
   10412      153099 :               mk = MK_field;
   10413             :             break;
   10414             : 
   10415      618432 :           case NAMESPACE_DECL:
   10416      796578 :             if (DECL_IMPLICIT_TYPEDEF_P (STRIP_TEMPLATE (decl))
   10417      843960 :                 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
   10418         126 :               if (tree scope
   10419          63 :                   = LAMBDA_EXPR_EXTRA_SCOPE (CLASSTYPE_LAMBDA_EXPR
   10420             :                                              (TREE_TYPE (decl))))
   10421          63 :                 if (TREE_CODE (scope) == VAR_DECL
   10422          63 :                     && DECL_MODULE_KEYED_DECLS_P (scope))
   10423             :                   {
   10424             :                     mk = MK_keyed;
   10425             :                     break;
   10426             :                   }
   10427             : 
   10428      618369 :             if (TREE_CODE (decl) == TEMPLATE_DECL
   10429      618369 :                 && DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (decl))
   10430             :               mk = MK_local_friend;
   10431      604800 :             else if (IDENTIFIER_ANON_P (DECL_NAME (decl)))
   10432             :               {
   10433        1632 :                 if (DECL_IMPLICIT_TYPEDEF_P (decl)
   10434        1632 :                     && UNSCOPED_ENUM_P (TREE_TYPE (decl))
   10435        3264 :                     && TYPE_VALUES (TREE_TYPE (decl)))
   10436             :                   /* Keyed by first enum value, and underlying type.  */
   10437             :                   mk = MK_enum;
   10438             :                 else
   10439             :                   /* No way to merge it, it is an ODR land-mine.  */
   10440             :                   mk = MK_unique;
   10441             :               }
   10442             :           }
   10443             :       }
   10444             :       break;
   10445             : 
   10446     1255464 :     case depset::EK_SPECIALIZATION:
   10447     1255464 :       {
   10448     1255464 :         gcc_checking_assert (dep->is_special ());
   10449             : 
   10450     1255464 :         if (TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
   10451             :           /* An block-scope classes of templates are themselves
   10452             :              templates.  */
   10453        4806 :           gcc_checking_assert (DECL_IMPLICIT_TYPEDEF_P (decl));
   10454             : 
   10455     1255464 :         if (dep->is_friend_spec ())
   10456             :           mk = MK_friend_spec;
   10457     1255464 :         else if (dep->is_type_spec ())
   10458             :           mk = MK_type_spec;
   10459      867603 :         else if (dep->is_alias ())
   10460             :           mk = MK_alias_spec;
   10461             :         else
   10462      804306 :           mk = MK_decl_spec;
   10463             : 
   10464     1255464 :         if (TREE_CODE (decl) == TEMPLATE_DECL)
   10465             :           {
   10466       76896 :             spec_entry *entry = reinterpret_cast <spec_entry *> (dep->deps[0]);
   10467       76896 :             if (TREE_CODE (entry->spec) != TEMPLATE_DECL)
   10468       11031 :               mk = merge_kind (mk | MK_tmpl_tmpl_mask);
   10469             :           }
   10470             :       }
   10471             :       break;
   10472             :     }
   10473             : 
   10474             :   return mk;
   10475             : }
   10476             : 
   10477             : 
   10478             : /* The container of DECL -- not necessarily its context!  */
   10479             : 
   10480             : tree
   10481     3669401 : trees_out::decl_container (tree decl)
   10482             : {
   10483     3669401 :   int use_tpl;
   10484     3669401 :   tree tpl = NULL_TREE;
   10485     3669401 :   if (tree template_info = node_template_info (decl, use_tpl))
   10486     1327315 :     tpl = TI_TEMPLATE (template_info);
   10487     3669401 :   if (tpl == decl)
   10488           0 :     tpl = nullptr;
   10489             : 
   10490             :   /* Stream the template we're instantiated from.  */
   10491     3669401 :   tree_node (tpl);
   10492             : 
   10493     3669401 :   tree container = NULL_TREE;
   10494     3669401 :   if (TREE_CODE (decl) == TEMPLATE_DECL
   10495     3669401 :       && DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (decl))
   10496       13605 :     container = DECL_CHAIN (decl);
   10497             :   else
   10498     3655796 :     container = CP_DECL_CONTEXT (decl);
   10499             : 
   10500     3669401 :   if (TYPE_P (container))
   10501     2150589 :     container = TYPE_NAME (container);
   10502             : 
   10503     3669401 :   tree_node (container);
   10504             : 
   10505     3669401 :   return container;
   10506             : }
   10507             : 
   10508             : tree
   10509     1534819 : trees_in::decl_container ()
   10510             : {
   10511             :   /* The maybe-template.  */
   10512     1534819 :   (void)tree_node ();
   10513             : 
   10514     1534819 :   tree container = tree_node ();
   10515             : 
   10516     1534819 :   return container;
   10517             : }
   10518             : 
   10519             : /* Write out key information about a mergeable DEP.  Does not write
   10520             :    the contents of DEP itself.  The context has already been
   10521             :    written.  The container has already been streamed.  */
   10522             : 
   10523             : void
   10524     3669401 : trees_out::key_mergeable (int tag, merge_kind mk, tree decl, tree inner,
   10525             :                           tree container, depset *dep)
   10526             : {
   10527     3669401 :   if (dep && is_key_order ())
   10528             :     {
   10529     1017849 :       gcc_checking_assert (dep->is_special ());
   10530     1017849 :       dep = dep->deps[0];
   10531             :     }
   10532             : 
   10533     3669401 :   if (streaming_p ())
   10534     1320456 :     dump (dumper::MERGE)
   10535         723 :       && dump ("Writing:%d's %s merge key (%s) %C:%N", tag, merge_kind_name[mk],
   10536         672 :                dep ? dep->entity_kind_name () : "contained",
   10537         723 :                TREE_CODE (decl), decl);
   10538             : 
   10539             :   /* Now write the locating information. */
   10540     3669401 :   if (mk & MK_template_mask)
   10541             :     {
   10542             :       /* Specializations are located via their originating template,
   10543             :          and the set of template args they specialize.  */
   10544     1255464 :       gcc_checking_assert (dep && dep->is_special ());
   10545     1255464 :       spec_entry *entry = reinterpret_cast <spec_entry *> (dep->deps[0]);
   10546             : 
   10547     1255464 :       tree_node (entry->tmpl);
   10548     1255464 :       tree_node (entry->args);
   10549     1255464 :       if (mk & MK_tmpl_decl_mask)
   10550      867603 :         if (flag_concepts && TREE_CODE (inner) == VAR_DECL)
   10551             :           {
   10552             :             /* Variable template partial specializations might need
   10553             :                constraints (see spec_hasher::equal).  It's simpler to
   10554             :                write NULL when we don't need them.  */
   10555       13488 :             tree constraints = NULL_TREE;
   10556             : 
   10557       13488 :             if (uses_template_parms (entry->args))
   10558         144 :               constraints = get_constraints (inner);
   10559       13488 :             tree_node (constraints);
   10560             :           }
   10561             : 
   10562     1255464 :       if (CHECKING_P)
   10563             :         {
   10564             :           /* Make sure we can locate the decl.  */
   10565     1255464 :           tree existing = match_mergeable_specialization
   10566     1255464 :             (bool (mk & MK_tmpl_decl_mask), entry);
   10567             : 
   10568     1255464 :           gcc_assert (existing);
   10569     1255464 :           if (mk & MK_tmpl_decl_mask)
   10570             :             {
   10571      867603 :               if (mk & MK_tmpl_alias_mask)
   10572             :                 /* It should be in both tables.  */
   10573       63297 :                 gcc_checking_assert
   10574             :                   (same_type_p (match_mergeable_specialization (false, entry),
   10575             :                                 TREE_TYPE (existing)));
   10576      867603 :               if (mk & MK_tmpl_tmpl_mask)
   10577        9129 :                 existing = DECL_TI_TEMPLATE (existing);
   10578             :             }
   10579             :           else
   10580             :             {
   10581      387861 :               if (mk & MK_tmpl_tmpl_mask)
   10582        1902 :                 existing = CLASSTYPE_TI_TEMPLATE (existing);
   10583             :               else
   10584      385959 :                 existing = TYPE_NAME (existing);
   10585             :             }
   10586             : 
   10587             :           /* The walkabout should have found ourselves.  */
   10588     1255464 :           gcc_checking_assert (TREE_CODE (decl) == TYPE_DECL
   10589             :                                ? same_type_p (TREE_TYPE (decl),
   10590             :                                               TREE_TYPE (existing))
   10591             :                                : existing == decl);
   10592             :         }
   10593             :     }
   10594     2413937 :   else if (mk != MK_unique)
   10595             :     {
   10596     2053189 :       merge_key key;
   10597     2053189 :       tree name = DECL_NAME (decl);
   10598             : 
   10599     2053189 :       switch (mk)
   10600             :         {
   10601           0 :         default:
   10602           0 :           gcc_unreachable ();
   10603             : 
   10604     1736782 :         case MK_named:
   10605     1736782 :         case MK_friend_spec:
   10606     1736782 :           if (IDENTIFIER_CONV_OP_P (name))
   10607        4725 :             name = conv_op_identifier;
   10608             : 
   10609     1736782 :           if (TREE_CODE (inner) == FUNCTION_DECL)
   10610             :             {
   10611             :               /* Functions are distinguished by parameter types.  */
   10612      950517 :               tree fn_type = TREE_TYPE (inner);
   10613             : 
   10614      950517 :               key.ref_q = type_memfn_rqual (fn_type);
   10615      950517 :               key.args = TYPE_ARG_TYPES (fn_type);
   10616             : 
   10617      950517 :               if (tree reqs = get_constraints (inner))
   10618             :                 {
   10619       34596 :                   if (cxx_dialect < cxx20)
   10620          42 :                     reqs = CI_ASSOCIATED_CONSTRAINTS (reqs);
   10621             :                   else
   10622       69150 :                     reqs = CI_DECLARATOR_REQS (reqs);
   10623             :                   key.constraints = reqs;
   10624             :                 }
   10625             : 
   10626      950517 :               if (IDENTIFIER_CONV_OP_P (name)
   10627      950517 :                   || (decl != inner
   10628      502713 :                       && !(name == fun_identifier
   10629             :                            /* In case the user names something _FUN  */
   10630          36 :                            && LAMBDA_TYPE_P (DECL_CONTEXT (inner)))))
   10631             :                 /* And a function template, or conversion operator needs
   10632             :                    the return type.  Except for the _FUN thunk of a
   10633             :                    generic lambda, which has a recursive decl_type'd
   10634             :                    return type.  */
   10635             :                 // FIXME: What if the return type is a voldemort?
   10636      507420 :                 key.ret = fndecl_declared_return_type (inner);
   10637             :             }
   10638             :           break;
   10639             : 
   10640      153099 :         case MK_field:
   10641      153099 :           {
   10642      153099 :             unsigned ix = 0;
   10643      153099 :             if (TREE_CODE (inner) != FIELD_DECL)
   10644             :               name = NULL_TREE;
   10645             :             else
   10646       29396 :               gcc_checking_assert (!name || !IDENTIFIER_ANON_P (name));
   10647             : 
   10648      153099 :             for (tree field = TYPE_FIELDS (TREE_TYPE (container));
   10649      308220 :                  ; field = DECL_CHAIN (field))
   10650             :               {
   10651      461319 :                 tree finner = STRIP_TEMPLATE (field);
   10652      461319 :                 if (TREE_CODE (finner) == TREE_CODE (inner))
   10653             :                   {
   10654      211115 :                     if (finner == inner)
   10655             :                       break;
   10656       58016 :                     ix++;
   10657             :                   }
   10658      308220 :               }
   10659             :             key.index = ix;
   10660             :           }
   10661             :           break;
   10662             : 
   10663        7896 :         case MK_vtable:
   10664        7896 :           {
   10665        7896 :             tree vtable = CLASSTYPE_VTABLES (TREE_TYPE (container));
   10666       10578 :             for (unsigned ix = 0; ; vtable = DECL_CHAIN (vtable), ix++)
   10667       10578 :               if (vtable == decl)
   10668             :                 {
   10669             :                   key.index = ix;
   10670             :                   break;
   10671             :                 }
   10672             :             name = NULL_TREE;
   10673             :           }
   10674             :           break;
   10675             : 
   10676       75135 :         case MK_as_base:
   10677       75135 :           gcc_checking_assert
   10678             :             (decl == TYPE_NAME (CLASSTYPE_AS_BASE (TREE_TYPE (container))));
   10679             :           break;
   10680             : 
   10681       13569 :         case MK_local_friend:
   10682       13569 :           {
   10683             :             /* Find by index on the class's DECL_LIST  */
   10684       13569 :             unsigned ix = 0;
   10685       13569 :             for (tree decls = CLASSTYPE_DECL_LIST (TREE_CHAIN (decl));
   10686      375402 :                  decls; decls = TREE_CHAIN (decls))
   10687      375402 :               if (!TREE_PURPOSE (decls))
   10688             :                 {
   10689       49833 :                   tree frnd = friend_from_decl_list (TREE_VALUE (decls));
   10690       49833 :                   if (frnd == decl)
   10691             :                     break;
   10692       36264 :                   ix++;
   10693             :                 }
   10694             :             key.index = ix;
   10695             :             name = NULL_TREE;
   10696             :           }
   10697             :           break;
   10698             : 
   10699        1632 :         case MK_enum:
   10700        1632 :           {
   10701             :             /* Anonymous enums are located by their first identifier,
   10702             :                and underlying type.  */
   10703        1632 :             tree type = TREE_TYPE (decl);
   10704             : 
   10705        1632 :             gcc_checking_assert (UNSCOPED_ENUM_P (type));
   10706             :             /* Using the type name drops the bit precision we might
   10707             :                have been using on the enum.  */
   10708        1632 :             key.ret = TYPE_NAME (ENUM_UNDERLYING_TYPE (type));
   10709        1632 :             if (tree values = TYPE_VALUES (type))
   10710        1632 :               name = DECL_NAME (TREE_VALUE (values));
   10711             :           }
   10712             :           break;
   10713             : 
   10714          63 :         case MK_keyed:
   10715          63 :           {
   10716         126 :             gcc_checking_assert (LAMBDA_TYPE_P (TREE_TYPE (inner)));
   10717          63 :             tree scope = LAMBDA_EXPR_EXTRA_SCOPE (CLASSTYPE_LAMBDA_EXPR
   10718             :                                                   (TREE_TYPE (inner)));
   10719          63 :             gcc_checking_assert (TREE_CODE (scope) == VAR_DECL);
   10720          63 :             auto *root = keyed_table->get (scope);
   10721          63 :             unsigned ix = root->length ();
   10722             :             /* If we don't find it, we'll write a really big number
   10723             :                that the reader will ignore.  */
   10724          63 :             while (ix--)
   10725          63 :               if ((*root)[ix] == inner)
   10726             :                 break;
   10727             : 
   10728             :             /* Use the keyed-to decl as the 'name'.  */
   10729          63 :             name = scope;
   10730          63 :             key.index = ix;
   10731             :           }
   10732          63 :           break;
   10733             : 
   10734       65013 :         case MK_partial:
   10735       65013 :           {
   10736       65013 :             tree ti = get_template_info (inner);
   10737       65013 :             key.constraints = get_constraints (inner);
   10738       65013 :             key.ret = TI_TEMPLATE (ti);
   10739       65013 :             key.args = TI_ARGS (ti);
   10740             :           }
   10741       65013 :           break;
   10742             :         }
   10743             : 
   10744     2053189 :       tree_node (name);
   10745     2053189 :       if (streaming_p ())
   10746             :         {
   10747      728295 :           unsigned code = (key.ref_q << 0) | (key.index << 2);
   10748      728295 :           u (code);
   10749             :         }
   10750             : 
   10751     2053189 :       if (mk == MK_enum)
   10752        1632 :         tree_node (key.ret);
   10753     2051557 :       else if (mk == MK_partial
   10754     1986544 :                || (mk == MK_named && inner
   10755     1736782 :                    && TREE_CODE (inner) == FUNCTION_DECL))
   10756             :         {
   10757     1015530 :           tree_node (key.ret);
   10758     1015530 :           tree arg = key.args;
   10759     1015530 :           if (mk == MK_named)
   10760     2828130 :             while (arg && arg != void_list_node)
   10761             :               {
   10762     1877613 :                 tree_node (TREE_VALUE (arg));
   10763     1877613 :                 arg = TREE_CHAIN (arg);
   10764             :               }
   10765     1015530 :           tree_node (arg);
   10766     1015530 :           tree_node (key.constraints);
   10767             :         }
   10768             :     }
   10769     3669401 : }
   10770             : 
   10771             : /* DECL is a new declaration that may be duplicated in OVL.  Use RET &
   10772             :    ARGS to find its clone, or NULL.  If DECL's DECL_NAME is NULL, this
   10773             :    has been found by a proxy.  It will be an enum type located by its
   10774             :    first member.
   10775             : 
   10776             :    We're conservative with matches, so ambiguous decls will be
   10777             :    registered as different, then lead to a lookup error if the two
   10778             :    modules are both visible.  Perhaps we want to do something similar
   10779             :    to duplicate decls to get ODR errors on loading?  We already have
   10780             :    some special casing for namespaces.  */
   10781             : 
   10782             : static tree
   10783      483781 : check_mergeable_decl (merge_kind mk, tree decl, tree ovl, merge_key const &key)
   10784             : {
   10785      483781 :   tree found = NULL_TREE;
   10786     2108099 :   for (ovl_iterator iter (ovl); !found && iter; ++iter)
   10787             :     {
   10788      923381 :       tree match = *iter;
   10789             : 
   10790      923381 :       tree d_inner = decl;
   10791      923381 :       tree m_inner = match;
   10792             : 
   10793     1086059 :     again:
   10794     1086059 :       if (TREE_CODE (d_inner) != TREE_CODE (m_inner))
   10795             :         {
   10796      135816 :           if (TREE_CODE (match) == NAMESPACE_DECL
   10797      135816 :               && !DECL_NAMESPACE_ALIAS (match))
   10798             :             /* Namespaces are never overloaded.  */
   10799             :             found = match;
   10800             : 
   10801      135816 :           continue;
   10802             :         }
   10803             : 
   10804      950243 :       switch (TREE_CODE (d_inner))
   10805             :         {
   10806      328408 :         case TEMPLATE_DECL:
   10807      328408 :           if (template_heads_equivalent_p (d_inner, m_inner))
   10808             :             {
   10809      162678 :               d_inner = DECL_TEMPLATE_RESULT (d_inner);
   10810      162678 :               m_inner = DECL_TEMPLATE_RESULT (m_inner);
   10811      162678 :               if (d_inner == error_mark_node
   10812      162678 :                   && TYPE_DECL_ALIAS_P (m_inner))
   10813             :                 {
   10814             :                   found = match;
   10815             :                   break;
   10816             :                 }
   10817      162678 :               goto again;
   10818             :             }
   10819             :           break;
   10820             : 
   10821      456789 :         case FUNCTION_DECL:
   10822      456789 :           if (tree m_type = TREE_TYPE (m_inner))
   10823      456789 :             if ((!key.ret
   10824      250118 :                  || same_type_p (key.ret, fndecl_declared_return_type (m_inner)))
   10825      421417 :                 && type_memfn_rqual (m_type) == key.ref_q
   10826      421217 :                 && compparms (key.args, TYPE_ARG_TYPES (m_type))
   10827             :                 /* Reject if old is a "C" builtin and new is not "C".
   10828             :                    Matches decls_match behaviour.  */
   10829      232358 :                 && (!DECL_IS_UNDECLARED_BUILTIN (m_inner)
   10830       11165 :                     || !DECL_EXTERN_C_P (m_inner)
   10831       10925 :                     || DECL_EXTERN_C_P (d_inner))
   10832             :                 /* Reject if one is a different member of a
   10833             :                    guarded/pre/post fn set.  */
   10834      232343 :                 && (!flag_contracts
   10835         302 :                     || (DECL_IS_PRE_FN_P (d_inner)
   10836         151 :                         == DECL_IS_PRE_FN_P (m_inner)))
   10837      689132 :                 && (!flag_contracts
   10838         302 :                     || (DECL_IS_POST_FN_P (d_inner)
   10839         151 :                         == DECL_IS_POST_FN_P (m_inner))))
   10840             :               {
   10841      232343 :                 tree m_reqs = get_constraints (m_inner);
   10842      232343 :                 if (m_reqs)
   10843             :                   {
   10844        7154 :                     if (cxx_dialect < cxx20)
   10845          10 :                       m_reqs = CI_ASSOCIATED_CONSTRAINTS (m_reqs);
   10846             :                     else
   10847       14298 :                       m_reqs = CI_DECLARATOR_REQS (m_reqs);
   10848             :                   }
   10849             : 
   10850      232343 :                 if (cp_tree_equal (key.constraints, m_reqs))
   10851      285916 :                   found = match;
   10852             :               }
   10853             :           break;
   10854             : 
   10855      111635 :         case TYPE_DECL:
   10856      223270 :           if (DECL_IMPLICIT_TYPEDEF_P (d_inner)
   10857      111635 :               == DECL_IMPLICIT_TYPEDEF_P (m_inner))
   10858             :             {
   10859      111577 :               if (!IDENTIFIER_ANON_P (DECL_NAME (m_inner)))
   10860      111222 :                 return match;
   10861         355 :               else if (mk == MK_enum
   10862         355 :                        && (TYPE_NAME (ENUM_UNDERLYING_TYPE (TREE_TYPE (m_inner)))
   10863         355 :                            == key.ret))
   10864             :                 found = match;
   10865             :             }
   10866             :           break;
   10867             : 
   10868             :         default:
   10869             :           found = match;
   10870             :           break;
   10871             :         }
   10872             :     }
   10873             : 
   10874      372559 :   return found;
   10875             : }
   10876             : 
   10877             : /* DECL, INNER & TYPE are a skeleton set of nodes for a decl.  Only
   10878             :    the bools have been filled in.  Read its merging key and merge it.
   10879             :    Returns the existing decl if there is one.  */
   10880             : 
   10881             : tree
   10882     1534819 : trees_in::key_mergeable (int tag, merge_kind mk, tree decl, tree inner,
   10883             :                          tree type, tree container, bool is_attached)
   10884             : {
   10885     1534819 :   const char *kind = "new";
   10886     1534819 :   tree existing = NULL_TREE;
   10887             : 
   10888     1534819 :   if (mk & MK_template_mask)
   10889             :     {
   10890             :       // FIXME: We could stream the specialization hash?
   10891      483782 :       spec_entry spec;
   10892      483782 :       spec.tmpl = tree_node ();
   10893      483782 :       spec.args = tree_node ();
   10894             : 
   10895      483782 :       if (get_overrun ())
   10896           0 :         return error_mark_node;
   10897             : 
   10898      483782 :       DECL_NAME (decl) = DECL_NAME (spec.tmpl);
   10899      483782 :       DECL_CONTEXT (decl) = DECL_CONTEXT (spec.tmpl);
   10900      483782 :       DECL_NAME (inner) = DECL_NAME (decl);
   10901      483782 :       DECL_CONTEXT (inner) = DECL_CONTEXT (decl);
   10902             : 
   10903      483782 :       tree constr = NULL_TREE;
   10904      483782 :       bool is_decl = mk & MK_tmpl_decl_mask;
   10905      483782 :       if (is_decl)
   10906             :         {
   10907      339136 :           if (flag_concepts && TREE_CODE (inner) == VAR_DECL)
   10908             :             {
   10909        5229 :               constr = tree_node ();
   10910        5229 :               if (constr)
   10911           0 :                 set_constraints (inner, constr);
   10912             :             }
   10913      674393 :           spec.spec = (mk & MK_tmpl_tmpl_mask) ? inner : decl;
   10914             :         }
   10915             :       else
   10916      144646 :         spec.spec = type;
   10917      483782 :       existing = match_mergeable_specialization (is_decl, &spec);
   10918      483782 :       if (constr)
   10919             :         /* We'll add these back later, if this is the new decl.  */
   10920           0 :         remove_constraints (inner);
   10921             : 
   10922      483782 :       if (!existing)
   10923             :         ; /* We'll add to the table once read.  */
   10924      264003 :       else if (mk & MK_tmpl_decl_mask)
   10925             :         {
   10926             :           /* A declaration specialization.  */
   10927      181159 :           if (mk & MK_tmpl_tmpl_mask)
   10928        2136 :             existing = DECL_TI_TEMPLATE (existing);
   10929             :         }
   10930             :       else
   10931             :         {
   10932             :           /* A type specialization.  */
   10933       82844 :           if (mk & MK_tmpl_tmpl_mask)
   10934         464 :             existing = CLASSTYPE_TI_TEMPLATE (existing);
   10935             :           else
   10936       82380 :             existing = TYPE_NAME (existing);
   10937             :         }
   10938             :     }
   10939     1051037 :   else if (mk == MK_unique)
   10940             :     kind = "unique";
   10941             :   else
   10942             :     {
   10943      828953 :       tree name = tree_node ();
   10944             : 
   10945      828953 :       merge_key key;
   10946      828953 :       unsigned code = u ();
   10947      828953 :       key.ref_q = cp_ref_qualifier ((code >> 0) & 3);
   10948      828953 :       key.index = code >> 2;
   10949             : 
   10950      828953 :       if (mk == MK_enum)
   10951         593 :         key.ret = tree_node ();
   10952      828360 :       else if (mk == MK_partial
   10953      807443 :                || ((mk == MK_named || mk == MK_friend_spec)
   10954      693191 :                    && TREE_CODE (inner) == FUNCTION_DECL))
   10955             :         {
   10956      405713 :           key.ret = tree_node ();
   10957      405713 :           tree arg, *arg_ptr = &key.args;
   10958      405713 :           while ((arg = tree_node ())
   10959     1159280 :                  && arg != void_list_node
   10960     1939942 :                  && mk != MK_partial)
   10961             :             {
   10962      756656 :               *arg_ptr = tree_cons (NULL_TREE, arg, NULL_TREE);
   10963      756656 :               arg_ptr = &TREE_CHAIN (*arg_ptr);
   10964             :             }
   10965      405713 :           *arg_ptr = arg;
   10966      405713 :           key.constraints = tree_node ();
   10967             :         }
   10968             : 
   10969      828953 :       if (get_overrun ())
   10970           0 :         return error_mark_node;
   10971             : 
   10972      828953 :       if (mk < MK_indirect_lwm)
   10973             :         {
   10974      822964 :           DECL_NAME (decl) = name;
   10975      822964 :           DECL_CONTEXT (decl) = FROB_CONTEXT (container);
   10976             :         }
   10977      828953 :       DECL_NAME (inner) = DECL_NAME (decl);
   10978      828953 :       DECL_CONTEXT (inner) = DECL_CONTEXT (decl);
   10979             : 
   10980      828953 :       if (mk == MK_partial)
   10981             :         {
   10982       20917 :           for (tree spec = DECL_TEMPLATE_SPECIALIZATIONS (key.ret);
   10983       97470 :                spec; spec = TREE_CHAIN (spec))
   10984             :             {
   10985       89464 :               tree tmpl = TREE_VALUE (spec);
   10986       89464 :               tree ti = get_template_info (tmpl);
   10987       89464 :               if (template_args_equal (key.args, TI_ARGS (ti))
   10988      103210 :                   && cp_tree_equal (key.constraints,
   10989             :                                     get_constraints
   10990       13746 :                                     (DECL_TEMPLATE_RESULT (tmpl))))
   10991             :                 {
   10992             :                   existing = tmpl;
   10993             :                   break;
   10994             :                 }
   10995             :             }
   10996             :         }
   10997             :       else
   10998      808036 :         switch (TREE_CODE (container))
   10999             :           {
   11000           0 :           default:
   11001           0 :             gcc_unreachable ();
   11002             : 
   11003      232412 :           case NAMESPACE_DECL:
   11004      232412 :             if (mk == MK_keyed)
   11005             :               {
   11006          33 :                 if (DECL_LANG_SPECIFIC (name)
   11007          33 :                     && VAR_OR_FUNCTION_DECL_P (name)
   11008          66 :                     && DECL_MODULE_KEYED_DECLS_P (name))
   11009          33 :                   if (auto *set = keyed_table->get (name))
   11010      828977 :                     if (key.index < set->length ())
   11011             :                       {
   11012          21 :                         existing = (*set)[key.index];
   11013          21 :                         if (existing)
   11014             :                           {
   11015          21 :                             gcc_checking_assert
   11016             :                               (DECL_IMPLICIT_TYPEDEF_P (existing));
   11017          21 :                             if (inner != decl)
   11018           6 :                               existing
   11019           6 :                                 = CLASSTYPE_TI_TEMPLATE (TREE_TYPE (existing));
   11020             :                           }
   11021             :                       }
   11022             :               }
   11023      232379 :             else if (is_attached
   11024      232379 :                      && !(state->is_module () || state->is_partition ()))
   11025             :               kind = "unique";
   11026             :             else
   11027             :               {
   11028      231263 :                 gcc_checking_assert (mk == MK_named || mk == MK_enum);
   11029      231263 :                 tree mvec;
   11030      231263 :                 tree *vslot = mergeable_namespace_slots (container, name,
   11031             :                                                          is_attached, &mvec);
   11032      231263 :                 existing = check_mergeable_decl (mk, decl, *vslot, key);
   11033      231263 :                 if (!existing)
   11034       85912 :                   add_mergeable_namespace_entity (vslot, decl);
   11035             :                 else
   11036             :                   {
   11037             :                     /* Note that we now have duplicates to deal with in
   11038             :                        name lookup.  */
   11039      145351 :                     if (is_attached)
   11040          15 :                       BINDING_VECTOR_PARTITION_DUPS_P (mvec) = true;
   11041             :                     else
   11042      145336 :                       BINDING_VECTOR_GLOBAL_DUPS_P (mvec) = true;
   11043             :                   }
   11044             :               }
   11045             :             break;
   11046             : 
   11047             :           case FUNCTION_DECL:
   11048             :             // FIXME: What about a voldemort? how do we find what it
   11049             :             // duplicates? Do we have to number vmorts relative to
   11050             :             // their containing function?  But how would that work
   11051             :             // when matching an in-TU declaration?
   11052             :             kind = "unique";
   11053             :             break;
   11054             : 
   11055      575624 :           case TYPE_DECL:
   11056        2579 :             if (is_attached && !(state->is_module () || state->is_partition ())
   11057             :                 /* Implicit member functions can come from
   11058             :                    anywhere.  */
   11059      577528 :                 && !(DECL_ARTIFICIAL (decl)
   11060        1040 :                      && TREE_CODE (decl) == FUNCTION_DECL
   11061         289 :                      && !DECL_THUNK_P (decl)))
   11062             :               kind = "unique";
   11063             :             else
   11064             :               {
   11065      574009 :                 tree ctx = TREE_TYPE (container);
   11066             : 
   11067             :                 /* For some reason templated enumeral types are not marked
   11068             :                    as COMPLETE_TYPE_P, even though they have members.
   11069             :                    This may well be a bug elsewhere.  */
   11070      574009 :                 if (TREE_CODE (ctx) == ENUMERAL_TYPE)
   11071       19709 :                   existing = find_enum_member (ctx, name);
   11072      554300 :                 else if (COMPLETE_TYPE_P (ctx))
   11073             :                   {
   11074      315166 :                     switch (mk)
   11075             :                       {
   11076           0 :                       default:
   11077           0 :                         gcc_unreachable ();
   11078             : 
   11079      253028 :                       case MK_named:
   11080      253028 :                         existing = lookup_class_binding (ctx, name);
   11081      253028 :                         if (existing)
   11082             :                           {
   11083      252518 :                             tree inner = decl;
   11084      252518 :                             if (TREE_CODE (inner) == TEMPLATE_DECL
   11085      252518 :                                 && !DECL_MEMBER_TEMPLATE_P (inner))
   11086      119823 :                               inner = DECL_TEMPLATE_RESULT (inner);
   11087             : 
   11088      252518 :                             existing = check_mergeable_decl
   11089      252518 :                               (mk, inner, existing, key);
   11090             :                             
   11091      252518 :                             if (!existing && DECL_ALIAS_TEMPLATE_P (decl))
   11092             :                               {} // FIXME: Insert into specialization
   11093             :                             // tables, we'll need the arguments for that!
   11094             :                           }
   11095             :                         break;
   11096             : 
   11097       41990 :                       case MK_field:
   11098       41990 :                         {
   11099       41990 :                           unsigned ix = key.index;
   11100       41990 :                           for (tree field = TYPE_FIELDS (ctx);
   11101      116934 :                                field; field = DECL_CHAIN (field))
   11102             :                             {
   11103      116934 :                               tree finner = STRIP_TEMPLATE (field);
   11104      116934 :                               if (TREE_CODE (finner) == TREE_CODE (inner))
   11105       58327 :                                 if (!ix--)
   11106             :                                   {
   11107             :                                     existing = field;
   11108             :                                     break;
   11109             :                                   }
   11110             :                             }
   11111             :                         }
   11112             :                         break;
   11113             : 
   11114        3138 :                       case MK_vtable:
   11115        3138 :                         {
   11116        3138 :                           unsigned ix = key.index;
   11117        3138 :                           for (tree vtable = CLASSTYPE_VTABLES (ctx);
   11118        4149 :                                vtable; vtable = DECL_CHAIN (vtable))
   11119        3755 :                             if (!ix--)
   11120             :                               {
   11121             :                                 existing = vtable;
   11122             :                                 break;
   11123             :                               }
   11124             :                         }
   11125             :                         break;
   11126             : 
   11127       13705 :                       case MK_as_base:
   11128       13705 :                         {
   11129       13705 :                           tree as_base = CLASSTYPE_AS_BASE (ctx);
   11130       13705 :                           if (as_base && as_base != ctx)
   11131       13705 :                             existing = TYPE_NAME (as_base);
   11132             :                         }
   11133             :                         break;
   11134             : 
   11135        3305 :                       case MK_local_friend:
   11136        3305 :                         {
   11137        3305 :                           unsigned ix = key.index;
   11138        3305 :                           for (tree decls = CLASSTYPE_DECL_LIST (ctx);
   11139       92158 :                                decls; decls = TREE_CHAIN (decls))
   11140       92158 :                             if (!TREE_PURPOSE (decls) && !ix--)
   11141             :                               {
   11142        3305 :                                 existing
   11143        3305 :                                   = friend_from_decl_list (TREE_VALUE (decls));
   11144        3305 :                                 break;
   11145             :                               }
   11146             :                         }
   11147             :                         break;
   11148             :                       }
   11149             : 
   11150      315166 :                     if (existing && mk < MK_indirect_lwm && mk != MK_partial
   11151      310226 :                         && TREE_CODE (decl) == TEMPLATE_DECL
   11152      462633 :                         && !DECL_MEMBER_TEMPLATE_P (decl))
   11153             :                       {
   11154      120631 :                         tree ti;
   11155      120631 :                         if (DECL_IMPLICIT_TYPEDEF_P (existing))
   11156        1507 :                           ti = TYPE_TEMPLATE_INFO (TREE_TYPE (existing));
   11157             :                         else
   11158      119124 :                           ti = DECL_TEMPLATE_INFO (existing);
   11159      120631 :                         existing = TI_TEMPLATE (ti);
   11160             :                       }
   11161             :                   }
   11162             :               }
   11163             :           }
   11164             :     }
   11165             : 
   11166     1534819 :   dump (dumper::MERGE)
   11167        2245 :     && dump ("Read:%d's %s merge key (%s) %C:%N", tag, merge_kind_name[mk],
   11168        2245 :              existing ? "matched" : kind, TREE_CODE (decl), decl);
   11169             : 
   11170             :   return existing;
   11171             : }
   11172             : 
   11173             : void
   11174      323096 : trees_out::binfo_mergeable (tree binfo)
   11175             : {
   11176      323096 :   tree dom = binfo;
   11177      422951 :   while (tree parent = BINFO_INHERITANCE_CHAIN (dom))
   11178             :     dom = parent;
   11179      323096 :   tree type = BINFO_TYPE (dom);
   11180      323096 :   gcc_checking_assert (TYPE_BINFO (type) == dom);
   11181      323096 :   tree_node (type);
   11182      323096 :   if (streaming_p ())
   11183             :     {
   11184             :       unsigned ix = 0;
   11185      191877 :       for (; dom != binfo; dom = TREE_CHAIN (dom))
   11186       56952 :         ix++;
   11187      134925 :       u (ix);
   11188             :     }
   11189      323096 : }
   11190             : 
   11191             : unsigned
   11192      142692 : trees_in::binfo_mergeable (tree *type)
   11193             : {
   11194      142692 :   *type = tree_node ();
   11195      142692 :   return u ();
   11196             : }
   11197             : 
   11198             : /* DECL is a just streamed mergeable decl that should match EXISTING.  Check
   11199             :    it does and issue an appropriate diagnostic if not.  Merge any
   11200             :    bits from DECL to EXISTING.  This is stricter matching than
   11201             :    decls_match, because we can rely on ODR-sameness, and we cannot use
   11202             :    decls_match because it can cause instantiations of constraints.  */
   11203             : 
   11204             : bool
   11205      747479 : trees_in::is_matching_decl (tree existing, tree decl, bool is_typedef)
   11206             : {
   11207             :   // FIXME: We should probably do some duplicate decl-like stuff here
   11208             :   // (beware, default parms should be the same?)  Can we just call
   11209             :   // duplicate_decls and teach it how to handle the module-specific
   11210             :   // permitted/required duplications?
   11211             : 
   11212             :   // We know at this point that the decls have matched by key, so we
   11213             :   // can elide some of the checking
   11214      747479 :   gcc_checking_assert (TREE_CODE (existing) == TREE_CODE (decl));
   11215             : 
   11216      747479 :   tree d_inner = decl;
   11217      747479 :   tree e_inner = existing;
   11218      747479 :   if (TREE_CODE (decl) == TEMPLATE_DECL)
   11219             :     {
   11220      242569 :       d_inner = DECL_TEMPLATE_RESULT (d_inner);
   11221      242569 :       e_inner = DECL_TEMPLATE_RESULT (e_inner);
   11222      242569 :       gcc_checking_assert (TREE_CODE (e_inner) == TREE_CODE (d_inner));
   11223             :     }
   11224             : 
   11225      747479 :   if (TREE_CODE (d_inner) == FUNCTION_DECL)
   11226             :     {
   11227      348619 :       tree e_ret = fndecl_declared_return_type (existing);
   11228      348619 :       tree d_ret = fndecl_declared_return_type (decl);
   11229             : 
   11230      139977 :       if (decl != d_inner && DECL_NAME (d_inner) == fun_identifier
   11231      348631 :           && LAMBDA_TYPE_P (DECL_CONTEXT (d_inner)))
   11232             :         /* This has a recursive type that will compare different.  */;
   11233      348613 :       else if (!same_type_p (d_ret, e_ret))
   11234           6 :         goto mismatch;
   11235             : 
   11236      348613 :       tree e_type = TREE_TYPE (e_inner);
   11237      348613 :       tree d_type = TREE_TYPE (d_inner);
   11238             : 
   11239      348613 :       if (DECL_EXTERN_C_P (d_inner) != DECL_EXTERN_C_P (e_inner))
   11240           0 :         goto mismatch;
   11241             : 
   11242      348613 :       for (tree e_args = TYPE_ARG_TYPES (e_type),
   11243      348613 :              d_args = TYPE_ARG_TYPES (d_type);
   11244      589730 :            e_args != d_args && (e_args || d_args);
   11245      241117 :            e_args = TREE_CHAIN (e_args), d_args = TREE_CHAIN (d_args))
   11246             :         {
   11247      241117 :           if (!(e_args && d_args))
   11248           0 :             goto mismatch;
   11249             : 
   11250      241117 :           if (!same_type_p (TREE_VALUE (d_args), TREE_VALUE (e_args)))
   11251           0 :             goto mismatch;
   11252             : 
   11253             :           // FIXME: Check default values
   11254             :         }
   11255             : 
   11256             :       /* If EXISTING has an undeduced or uninstantiated exception
   11257             :          specification, but DECL does not, propagate the exception
   11258             :          specification.  Otherwise we end up asserting or trying to
   11259             :          instantiate it in the middle of loading.   */
   11260      348613 :       tree e_spec = TYPE_RAISES_EXCEPTIONS (e_type);
   11261      348613 :       tree d_spec = TYPE_RAISES_EXCEPTIONS (d_type);
   11262      514355 :       if (DEFERRED_NOEXCEPT_SPEC_P (e_spec))
   11263             :         {
   11264       25432 :           if (!DEFERRED_NOEXCEPT_SPEC_P (d_spec)
   11265       50705 :               || (UNEVALUATED_NOEXCEPT_SPEC_P (e_spec)
   11266       20913 :                   && !UNEVALUATED_NOEXCEPT_SPEC_P (d_spec)))
   11267             :             {
   11268         159 :               dump (dumper::MERGE)
   11269           3 :                 && dump ("Propagating instantiated noexcept to %N", existing);
   11270         159 :               TREE_TYPE (existing) = d_type;
   11271             : 
   11272             :               /* Propagate to existing clones.  */
   11273         159 :               tree clone;
   11274         477 :               FOR_EACH_CLONE (clone, existing)
   11275             :                 {
   11276         318 :                   if (TREE_TYPE (clone) == e_type)
   11277         234 :                     TREE_TYPE (clone) = d_type;
   11278             :                   else
   11279          84 :                     TREE_TYPE (clone)
   11280         168 :                       = build_exception_variant (TREE_TYPE (clone), d_spec);
   11281             :                 }
   11282             :             }
   11283             :         }
   11284      142467 :       else if (!DEFERRED_NOEXCEPT_SPEC_P (d_spec)
   11285      465648 :                && !comp_except_specs (d_spec, e_spec, ce_type))
   11286        2235 :         goto mismatch;
   11287             :     }
   11288      398860 :   else if (is_typedef)
   11289             :     {
   11290      151006 :       if (!DECL_ORIGINAL_TYPE (e_inner)
   11291      151006 :           || !same_type_p (DECL_ORIGINAL_TYPE (d_inner),
   11292             :                            DECL_ORIGINAL_TYPE (e_inner)))
   11293           0 :         goto mismatch;
   11294             :     }
   11295             :   /* Using cp_tree_equal because we can meet TYPE_ARGUMENT_PACKs
   11296             :      here. I suspect the entities that directly do that are things
   11297             :      that shouldn't go to duplicate_decls (FIELD_DECLs etc).   */
   11298      247854 :   else if (!cp_tree_equal (TREE_TYPE (decl), TREE_TYPE (existing)))
   11299             :     {
   11300           6 :     mismatch:
   11301        2247 :       if (DECL_IS_UNDECLARED_BUILTIN (existing))
   11302             :         /* Just like duplicate_decls, presum the user knows what
   11303             :            they're doing in overriding a builtin.   */
   11304        2238 :         TREE_TYPE (existing) = TREE_TYPE (decl);
   11305             :       else
   11306             :         {
   11307             :           // FIXME:QOI Might be template specialization from a module,
   11308             :           // not necessarily global module
   11309           9 :           error_at (DECL_SOURCE_LOCATION (decl),
   11310             :                     "conflicting global module declaration %#qD", decl);
   11311           9 :           inform (DECL_SOURCE_LOCATION (existing),
   11312             :                   "existing declaration %#qD", existing);
   11313           9 :           return false;
   11314             :         }
   11315             :     }
   11316             : 
   11317      747470 :   if (DECL_IS_UNDECLARED_BUILTIN (existing)
   11318      747470 :       && !DECL_IS_UNDECLARED_BUILTIN (decl))
   11319             :     {
   11320             :       /* We're matching a builtin that the user has yet to declare.
   11321             :          We are the one!  This is very much duplicate-decl
   11322             :          shenanigans. */
   11323        2609 :       DECL_SOURCE_LOCATION (existing) = DECL_SOURCE_LOCATION (decl);
   11324        2609 :       if (TREE_CODE (decl) != TYPE_DECL)
   11325             :         {
   11326             :           /* Propagate exceptions etc.  */
   11327        2589 :           TREE_TYPE (existing) = TREE_TYPE (decl);
   11328        2589 :           TREE_NOTHROW (existing) = TREE_NOTHROW (decl);
   11329             :         }
   11330             :       /* This is actually an import! */
   11331        2609 :       DECL_MODULE_IMPORT_P (existing) = true;
   11332             : 
   11333             :       /* Yay, sliced!  */
   11334        2609 :       existing->base = decl->base;
   11335             : 
   11336        2609 :       if (TREE_CODE (decl) == FUNCTION_DECL)
   11337             :         {
   11338             :           /* Ew :(  */
   11339        2589 :           memcpy (&existing->decl_common.size,
   11340             :                   &decl->decl_common.size,
   11341             :                   (offsetof (tree_decl_common, pt_uid)
   11342             :                    - offsetof (tree_decl_common, size)));
   11343        2589 :           auto bltin_class = DECL_BUILT_IN_CLASS (decl);
   11344        2589 :           existing->function_decl.built_in_class = bltin_class;
   11345        2589 :           auto fncode = DECL_UNCHECKED_FUNCTION_CODE (decl);
   11346        2589 :           DECL_UNCHECKED_FUNCTION_CODE (existing) = fncode;
   11347        2589 :           if (existing->function_decl.built_in_class == BUILT_IN_NORMAL)
   11348             :             {
   11349        2355 :               if (builtin_decl_explicit_p (built_in_function (fncode)))
   11350        2355 :                 switch (fncode)
   11351             :                   {
   11352           0 :                   case BUILT_IN_STPCPY:
   11353           0 :                     set_builtin_decl_implicit_p
   11354           0 :                       (built_in_function (fncode), true);
   11355           0 :                     break;
   11356        2355 :                   default:
   11357        2355 :                     set_builtin_decl_declared_p
   11358        2355 :                       (built_in_function (fncode), true);
   11359        2355 :                     break;
   11360             :                   }
   11361        2355 :               copy_attributes_to_builtin (decl);
   11362             :             }
   11363             :         }
   11364             :     }
   11365             : 
   11366      747470 :   if (VAR_OR_FUNCTION_DECL_P (decl)
   11367      747470 :       && DECL_TEMPLATE_INSTANTIATED (decl))
   11368             :     /* Don't instantiate again!  */
   11369       12733 :     DECL_TEMPLATE_INSTANTIATED (existing) = true;
   11370             : 
   11371      747470 :   if (TREE_CODE (d_inner) == FUNCTION_DECL
   11372      747470 :       && DECL_DECLARED_INLINE_P (d_inner))
   11373      260771 :     DECL_DECLARED_INLINE_P (e_inner) = true;
   11374      747470 :   if (!DECL_EXTERNAL (d_inner))
   11375      321739 :     DECL_EXTERNAL (e_inner) = false;
   11376             : 
   11377             :   // FIXME: Check default tmpl and fn parms here
   11378             : 
   11379             :   return true;
   11380             : }
   11381             : 
   11382             : /* FN is an implicit member function that we've discovered is new to
   11383             :    the class.  Add it to the TYPE_FIELDS chain and the method vector.
   11384             :    Reset the appropriate classtype lazy flag.   */
   11385             : 
   11386             : bool
   11387        1229 : trees_in::install_implicit_member (tree fn)
   11388             : {
   11389        1229 :   tree ctx = DECL_CONTEXT (fn);
   11390        1229 :   tree name = DECL_NAME (fn);
   11391             :   /* We know these are synthesized, so the set of expected prototypes
   11392             :      is quite restricted.  We're not validating correctness, just
   11393             :      distinguishing beteeen the small set of possibilities.  */
   11394        1229 :   tree parm_type = TREE_VALUE (FUNCTION_FIRST_USER_PARMTYPE (fn));
   11395        1229 :   if (IDENTIFIER_CTOR_P (name))
   11396             :     {
   11397         877 :       if (CLASSTYPE_LAZY_DEFAULT_CTOR (ctx)
   11398         877 :           && VOID_TYPE_P (parm_type))
   11399         161 :         CLASSTYPE_LAZY_DEFAULT_CTOR (ctx) = false;
   11400         716 :       else if (!TYPE_REF_P (parm_type))
   11401             :         return false;
   11402         716 :       else if (CLASSTYPE_LAZY_COPY_CTOR (ctx)
   11403         716 :                && !TYPE_REF_IS_RVALUE (parm_type))
   11404         374 :         CLASSTYPE_LAZY_COPY_CTOR (ctx) = false;
   11405         342 :       else if (CLASSTYPE_LAZY_MOVE_CTOR (ctx))
   11406         342 :         CLASSTYPE_LAZY_MOVE_CTOR (ctx) = false;
   11407             :       else
   11408             :         return false;
   11409             :     }
   11410         352 :   else if (IDENTIFIER_DTOR_P (name))
   11411             :     {
   11412         322 :       if (CLASSTYPE_LAZY_DESTRUCTOR (ctx))
   11413         322 :         CLASSTYPE_LAZY_DESTRUCTOR (ctx) = false;
   11414             :       else
   11415             :         return false;
   11416         322 :       if (DECL_VIRTUAL_P (fn))
   11417             :         /* A virtual dtor should have been created when the class
   11418             :            became complete.  */
   11419             :         return false;
   11420             :     }
   11421          30 :   else if (name == assign_op_identifier)
   11422             :     {
   11423          30 :       if (!TYPE_REF_P (parm_type))
   11424             :         return false;
   11425          30 :       else if (CLASSTYPE_LAZY_COPY_ASSIGN (ctx)
   11426          30 :                && !TYPE_REF_IS_RVALUE (parm_type))
   11427          15 :         CLASSTYPE_LAZY_COPY_ASSIGN (ctx) = false;
   11428          15 :       else if (CLASSTYPE_LAZY_MOVE_ASSIGN (ctx))
   11429          15 :         CLASSTYPE_LAZY_MOVE_ASSIGN (ctx) = false;
   11430             :       else
   11431             :         return false;
   11432             :     }
   11433             :   else
   11434             :     return false;
   11435             : 
   11436        1259 :   dump (dumper::MERGE) && dump ("Adding implicit member %N", fn);
   11437             : 
   11438        1229 :   DECL_CHAIN (fn) = TYPE_FIELDS (ctx);
   11439        1229 :   TYPE_FIELDS (ctx) = fn;
   11440             : 
   11441        1229 :   add_method (ctx, fn, false);
   11442             : 
   11443             :     /* Propagate TYPE_FIELDS.  */
   11444        1229 :   fixup_type_variants (ctx);
   11445             : 
   11446        1229 :   return true;
   11447             : }
   11448             : 
   11449             : /* Return non-zero if DECL has a definition that would be interesting to
   11450             :    write out.  */
   11451             : 
   11452             : static bool
   11453     1403457 : has_definition (tree decl)
   11454             : {
   11455     1403457 :   bool is_tmpl = TREE_CODE (decl) == TEMPLATE_DECL;
   11456     1403457 :   if (is_tmpl)
   11457      371628 :     decl = DECL_TEMPLATE_RESULT (decl);
   11458             : 
   11459     1403457 :   switch (TREE_CODE (decl))
   11460             :     {
   11461             :     default:
   11462             :       break;
   11463             : 
   11464      527389 :     case FUNCTION_DECL:
   11465      527389 :       if (!DECL_SAVED_TREE (decl))
   11466             :         /* Not defined.  */
   11467             :         break;
   11468             : 
   11469      227274 :       if (DECL_DECLARED_INLINE_P (decl))
   11470             :         return true;
   11471             : 
   11472       16802 :       if (DECL_THIS_STATIC (decl)
   11473       16802 :           && (header_module_p ()
   11474          33 :               || (!DECL_LANG_SPECIFIC (decl) || !DECL_MODULE_PURVIEW_P (decl))))
   11475             :         /* GM static function.  */
   11476             :         return true;
   11477             : 
   11478       16643 :       if (DECL_TEMPLATE_INFO (decl))
   11479             :         {
   11480       16309 :           int use_tpl = DECL_USE_TEMPLATE (decl);
   11481             : 
   11482             :           // FIXME: Partial specializations have definitions too.
   11483       16309 :           if (use_tpl < 2)
   11484       16285 :             return true;
   11485             :         }
   11486             :       break;
   11487             : 
   11488      596668 :     case TYPE_DECL:
   11489      596668 :       {
   11490      596668 :         tree type = TREE_TYPE (decl);
   11491      596668 :         if (type == TYPE_MAIN_VARIANT (type)
   11492      277688 :             && decl == TYPE_NAME (type)
   11493      874356 :             && (TREE_CODE (type) == ENUMERAL_TYPE
   11494      277688 :                 ? TYPE_VALUES (type) : TYPE_FIELDS (type)))
   11495      135527 :           return true;
   11496             :       }
   11497             :       break;
   11498             : 
   11499       49347 :     case VAR_DECL:
   11500       49347 :       if (DECL_LANG_SPECIFIC (decl)
   11501       49347 :           && DECL_TEMPLATE_INFO (decl))
   11502       27348 :         return DECL_INITIAL (decl);
   11503             :       else
   11504             :         {
   11505       21999 :           if (!DECL_INITIALIZED_P (decl))
   11506             :             return false;
   11507             : 
   11508       20256 :           if (header_module_p ()
   11509       20256 :               || (!DECL_LANG_SPECIFIC (decl) || !DECL_MODULE_PURVIEW_P (decl)))
   11510             :             /* GM static variable.  */
   11511             :             return true;
   11512             : 
   11513         111 :           if (!TREE_CONSTANT (decl))
   11514             :             return false;
   11515             : 
   11516          29 :           return true;
   11517             :         }
   11518        5971 :       break;
   11519             : 
   11520        5971 :     case CONCEPT_DECL:
   11521        5971 :       if (DECL_INITIAL (decl))
   11522        5971 :         return true;
   11523             : 
   11524             :       break;
   11525             :     }
   11526             : 
   11527             :   return false;
   11528             : }
   11529             : 
   11530             : uintptr_t *
   11531      657073 : trees_in::find_duplicate (tree existing)
   11532             : {
   11533      231916 :   if (!duplicates)
   11534             :     return NULL;
   11535             : 
   11536      484689 :   return duplicates->get (existing);
   11537             : }
   11538             : 
   11539             : /* We're starting to read a duplicate DECL.  EXISTING is the already
   11540             :    known node.  */
   11541             : 
   11542             : void
   11543      825927 : trees_in::register_duplicate (tree decl, tree existing)
   11544             : {
   11545      825927 :   if (!duplicates)
   11546      190398 :     duplicates = new duplicate_hash_map (40);
   11547             : 
   11548      825927 :   bool existed;
   11549      825927 :   uintptr_t &slot = duplicates->get_or_insert (existing, &existed);
   11550      825927 :   gcc_checking_assert (!existed);
   11551      825927 :   slot = reinterpret_cast<uintptr_t> (decl);
   11552      825927 : }
   11553             : 
   11554             : /* We've read a definition of MAYBE_EXISTING.  If not a duplicate,
   11555             :    return MAYBE_EXISTING (into which the definition should be
   11556             :    installed).  Otherwise return NULL if already known bad, or the
   11557             :    duplicate we read (for ODR checking, or extracting additional merge
   11558             :    information).  */
   11559             : 
   11560             : tree
   11561      425157 : trees_in::odr_duplicate (tree maybe_existing, bool has_defn)
   11562             : {
   11563      425157 :   tree res = NULL_TREE;
   11564             : 
   11565      677930 :   if (uintptr_t *dup = find_duplicate (maybe_existing))
   11566             :     {
   11567      239007 :       if (!(*dup & 1))
   11568      239007 :         res = reinterpret_cast<tree> (*dup);
   11569             :     }
   11570             :   else
   11571             :     res = maybe_existing;
   11572             : 
   11573      425157 :   assert_definition (maybe_existing, res && !has_defn);
   11574             : 
   11575             :   // FIXME: We probably need to return the template, so that the
   11576             :   // template header can be checked?
   11577      425157 :   return res ? STRIP_TEMPLATE (res) : NULL_TREE;
   11578             : }
   11579             : 
   11580             : /* The following writer functions rely on the current behaviour of
   11581             :    depset::hash::add_dependency making the decl and defn depset nodes
   11582             :    depend on eachother.  That way we don't have to worry about seeding
   11583             :    the tree map with named decls that cannot be looked up by name (I.e
   11584             :    template and function parms).  We know the decl and definition will
   11585             :    be in the same cluster, which is what we want.  */
   11586             : 
   11587             : void
   11588      447166 : trees_out::write_function_def (tree decl)
   11589             : {
   11590      447166 :   tree_node (DECL_RESULT (decl));
   11591      447166 :   tree_node (DECL_INITIAL (decl));
   11592      447166 :   tree_node (DECL_SAVED_TREE (decl));
   11593      956222 :   tree_node (DECL_FRIEND_CONTEXT (decl));
   11594             : 
   11595      447166 :   constexpr_fundef *cexpr = retrieve_constexpr_fundef (decl);
   11596             : 
   11597      447166 :   if (streaming_p ())
   11598      223583 :     u (cexpr != nullptr);
   11599      447166 :   if (cexpr)
   11600             :     {
   11601       79770 :       chained_decls (cexpr->parms);
   11602       79770 :       tree_node (cexpr->result);
   11603       79770 :       tree_node (cexpr->body);
   11604             :     }
   11605             : 
   11606      447166 :   if (streaming_p ())
   11607             :     {
   11608      223583 :       unsigned flags = 0;
   11609             : 
   11610      223583 :       if (DECL_NOT_REALLY_EXTERN (decl))
   11611       72130 :         flags |= 1;
   11612             : 
   11613      223583 :       u (flags);
   11614             :     }
   11615      447166 : }
   11616             : 
   11617             : void
   11618           0 : trees_out::mark_function_def (tree)
   11619             : {
   11620           0 : }
   11621             : 
   11622             : bool
   11623      268713 : trees_in::read_function_def (tree decl, tree maybe_template)
   11624             : {
   11625      269091 :   dump () && dump ("Reading function definition %N", decl);
   11626      268713 :   tree result = tree_node ();
   11627      268713 :   tree initial = tree_node ();
   11628      268713 :   tree saved = tree_node ();
   11629      268713 :   tree context = tree_node ();
   11630      268713 :   constexpr_fundef cexpr;
   11631             : 
   11632      268713 :   tree maybe_dup = odr_duplicate (maybe_template, DECL_SAVED_TREE (decl));
   11633      537426 :   bool installing = maybe_dup && !DECL_SAVED_TREE (decl);
   11634             : 
   11635      268713 :   if (u ())
   11636             :     {
   11637       47195 :       cexpr.parms = chained_decls ();
   11638       47195 :       cexpr.result = tree_node ();
   11639       47195 :       cexpr.body = tree_node ();
   11640       47195 :       cexpr.decl = decl;
   11641             :     }
   11642             :   else
   11643      221518 :     cexpr.decl = NULL_TREE;
   11644             : 
   11645      268713 :   unsigned flags = u ();
   11646             : 
   11647      268713 :   if (get_overrun ())
   11648             :     return NULL_TREE;
   11649             : 
   11650      268713 :   if (installing)
   11651             :     {
   11652      127704 :       DECL_NOT_REALLY_EXTERN (decl) = flags & 1;
   11653      127704 :       DECL_RESULT (decl) = result;
   11654      127704 :       DECL_INITIAL (decl) = initial;
   11655      127704 :       DECL_SAVED_TREE (decl) = saved;
   11656      127704 :       if (maybe_dup)
   11657      127704 :         DECL_ARGUMENTS (decl) = DECL_ARGUMENTS (maybe_dup);
   11658             : 
   11659      127704 :       if (context)
   11660        3603 :         SET_DECL_FRIEND_CONTEXT (decl, context);
   11661      127704 :       if (cexpr.decl)
   11662       29756 :         register_constexpr_fundef (cexpr);
   11663      127704 :       post_process (maybe_template);
   11664             :     }
   11665             :   else if (maybe_dup)
   11666             :     {
   11667             :       // FIXME:QOI Check matching defn
   11668             :     }
   11669             :   
   11670             :   return true;
   11671             : }
   11672             : 
   11673             : /* Also for CONCEPT_DECLs.  */
   11674             : 
   11675             : void
   11676       91028 : trees_out::write_var_def (tree decl)
   11677             : {
   11678       91028 :   tree init = DECL_INITIAL (decl);
   11679       91028 :   tree_node (init);
   11680       91028 :   if (!init)
   11681             :     {
   11682         826 :       tree dyn_init = NULL_TREE;
   11683             : 
   11684         826 :       if (DECL_NONTRIVIALLY_INITIALIZED_P (decl))
   11685             :         {
   11686          36 :           dyn_init = value_member (decl,
   11687          36 :                                    CP_DECL_THREAD_LOCAL_P (decl)
   11688             :                                    ? tls_aggregates : static_aggregates);
   11689          36 :           gcc_checking_assert (dyn_init);
   11690             :           /* Mark it so write_inits knows this is needed.  */
   11691          36 :           TREE_LANG_FLAG_0 (dyn_init) = true;
   11692          36 :           dyn_init = TREE_PURPOSE (dyn_init);
   11693             :         }
   11694         826 :       tree_node (dyn_init);
   11695             :     }
   11696       91028 : }
   11697             : 
   11698             : void
   11699           0 : trees_out::mark_var_def (tree)
   11700             : {
   11701           0 : }
   11702             : 
   11703             : bool
   11704       54087 : trees_in::read_var_def (tree decl, tree maybe_template)
   11705             : {
   11706             :   /* Do not mark the virtual table entries as used.  */
   11707       54087 :   bool vtable = VAR_P (decl) && DECL_VTABLE_OR_VTT_P (decl);
   11708       54087 :   unused += vtable;
   11709       54087 :   tree init = tree_node ();
   11710       54087 :   tree dyn_init = init ? NULL_TREE : tree_node ();
   11711       54087 :   unused -= vtable;
   11712             : 
   11713       54087 :   if (get_overrun ())
   11714             :     return false;
   11715             : 
   11716       54087 :   bool initialized = (VAR_P (decl) ? bool (DECL_INITIALIZED_P (decl))
   11717        5792 :                       : bool (DECL_INITIAL (decl)));
   11718       54087 :   tree maybe_dup = odr_duplicate (maybe_template, initialized);
   11719       54087 :   bool installing = maybe_dup && !initialized;
   11720       54087 :   if (installing)
   11721             :     {
   11722       30500 :       if (DECL_EXTERNAL (decl))
   11723       15566 :         DECL_NOT_REALLY_EXTERN (decl) = true;
   11724       30500 :       if (VAR_P (decl))
   11725             :         {
   11726       27792 :           DECL_INITIALIZED_P (decl) = true;
   11727       27792 :           if (maybe_dup && DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (maybe_dup))
   11728       27640 :             DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
   11729             :         }
   11730       30500 :       DECL_INITIAL (decl) = init;
   11731       30500 :       if (!dyn_init)
   11732             :         ;
   11733          12 :       else if (CP_DECL_THREAD_LOCAL_P (decl))
   11734           0 :         tls_aggregates = tree_cons (dyn_init, decl, tls_aggregates);
   11735             :       else
   11736          12 :         static_aggregates = tree_cons (dyn_init, decl, static_aggregates);
   11737             :     }
   11738             :   else if (maybe_dup)
   11739             :     {
   11740             :       // FIXME:QOI Check matching defn
   11741             :     }
   11742             : 
   11743             :   return true;
   11744             : }
   11745             : 
   11746             : /* If MEMBER doesn't have an independent life outside the class,
   11747             :    return it (or its TEMPLATE_DECL).  Otherwise NULL.  */
   11748             : 
   11749             : static tree
   11750      220530 : member_owned_by_class (tree member)
   11751             : {
   11752      220530 :   gcc_assert (DECL_P (member));
   11753             : 
   11754             :   /* Clones are owned by their origin.  */
   11755      220530 :   if (DECL_CLONED_FUNCTION_P (member))
   11756             :     return NULL;
   11757             : 
   11758      220530 :   if (TREE_CODE (member) == FIELD_DECL)
   11759             :     /* FIELD_DECLS can have template info in some cases.  We always
   11760             :        want the FIELD_DECL though, as there's never a TEMPLATE_DECL
   11761             :        wrapping them.  */
   11762             :     return member;
   11763             : 
   11764      102160 :   int use_tpl = -1;
   11765      102160 :   if (tree ti = node_template_info (member, use_tpl))
   11766             :     {
   11767             :       // FIXME: Don't bail on things that CANNOT have their own
   11768             :       // template header.  No, make sure they're in the same cluster.
   11769           0 :       if (use_tpl > 0)
   11770             :         return NULL_TREE;
   11771             : 
   11772           0 :       if (DECL_TEMPLATE_RESULT (TI_TEMPLATE (ti)) == member)
   11773      220530 :         member = TI_TEMPLATE (ti);
   11774             :     }
   11775             :   return member;
   11776             : }
   11777             : 
   11778             : void
   11779      187842 : trees_out::write_class_def (tree defn)
   11780             : {
   11781      187842 :   gcc_assert (DECL_P (defn));
   11782      187842 :   if (streaming_p ())
   11783       95347 :     dump () && dump ("Writing class definition %N", defn);
   11784             : 
   11785      187842 :   tree type = TREE_TYPE (defn);
   11786      187842 :   tree_node (TYPE_SIZE (type));
   11787      187842 :   tree_node (TYPE_SIZE_UNIT (type));
   11788      187842 :   tree_node (TYPE_VFIELD (type));
   11789      187842 :   tree_node (TYPE_BINFO (type));
   11790             : 
   11791      187842 :   vec_chained_decls (TYPE_FIELDS (type));
   11792             : 
   11793             :   /* Every class but __as_base has a type-specific.  */
   11794      372932 :   gcc_checking_assert (!TYPE_LANG_SPECIFIC (type) == IS_FAKE_BASE_TYPE (type));
   11795             : 
   11796      187842 :   if (TYPE_LANG_SPECIFIC (type))
   11797             :     {
   11798      185090 :       {
   11799      185090 :         vec<tree, va_gc> *v = CLASSTYPE_MEMBER_VEC (type);
   11800      185090 :         if (!v)
   11801             :           {
   11802       52298 :             gcc_checking_assert (!streaming_p ());
   11803             :             /* Force a class vector.  */
   11804       52298 :             v = set_class_bindings (type, -1);
   11805       52298 :             gcc_checking_assert (v);
   11806             :           }
   11807             : 
   11808      185090 :         unsigned len = v->length ();
   11809      185090 :         if (streaming_p ())
   11810       91669 :           u (len);
   11811     1384630 :         for (unsigned ix = 0; ix != len; ix++)
   11812             :           {
   11813     1199540 :             tree m = (*v)[ix];
   11814     1199540 :             if (TREE_CODE (m) == TYPE_DECL
   11815      401628 :                 && DECL_ARTIFICIAL (m)
   11816     1398114 :                 && TYPE_STUB_DECL (TREE_TYPE (m)) == m)
   11817             :               /* This is a using-decl for a type, or an anonymous
   11818             :                  struct (maybe with a typedef name).  Write the type.  */
   11819       12650 :               m = TREE_TYPE (m);
   11820     1199540 :             tree_node (m);
   11821             :           }
   11822             :       }
   11823      185090 :       tree_node (CLASSTYPE_LAMBDA_EXPR (type));
   11824             : 
   11825             :       /* TYPE_CONTAINS_VPTR_P looks at the vbase vector, which the
   11826             :          reader won't know at this point.  */
   11827      185090 :       int has_vptr = TYPE_CONTAINS_VPTR_P (type);
   11828             : 
   11829      185090 :       if (streaming_p ())
   11830             :         {
   11831       91669 :           unsigned nvbases = vec_safe_length (CLASSTYPE_VBASECLASSES (type));
   11832       91669 :           u (nvbases);
   11833       91669 :           i (has_vptr);
   11834             :         }
   11835             : 
   11836      185090 :       if (has_vptr)
   11837             :         {
   11838        6528 :           tree_vec (CLASSTYPE_PURE_VIRTUALS (type));
   11839        6528 :           tree_pair_vec (CLASSTYPE_VCALL_INDICES (type));
   11840        6528 :           tree_node (CLASSTYPE_KEY_METHOD (type));
   11841             :         }
   11842             :     }
   11843             : 
   11844      187842 :   if (TYPE_LANG_SPECIFIC (type))
   11845             :     {
   11846      185090 :       tree_node (CLASSTYPE_PRIMARY_BINFO (type));
   11847             : 
   11848      185090 :       tree as_base = CLASSTYPE_AS_BASE (type);
   11849      185090 :       if (as_base)
   11850       89042 :         as_base = TYPE_NAME (as_base);
   11851      185090 :       tree_node (as_base);
   11852             : 
   11853             :       /* Write the vtables.  */
   11854      185090 :       tree vtables = CLASSTYPE_VTABLES (type);
   11855      185090 :       vec_chained_decls (vtables);
   11856      378076 :       for (; vtables; vtables = TREE_CHAIN (vtables))
   11857        7896 :         write_definition (vtables);
   11858             : 
   11859             :       /* Write the friend classes.  */
   11860      185090 :       tree_list (CLASSTYPE_FRIEND_CLASSES (type), false);
   11861             : 
   11862             :       /* Write the friend functions.  */
   11863      185090 :       for (tree friends = DECL_FRIENDLIST (defn);
   11864      203200 :            friends; friends = TREE_CHAIN (friends))
   11865             :         {
   11866             :           /* Name of these friends.  */
   11867       18110 :           tree_node (TREE_PURPOSE (friends));
   11868       18110 :           tree_list (TREE_VALUE (friends), false);
   11869             :         }
   11870             :       /* End of friend fns.  */
   11871      185090 :       tree_node (NULL_TREE);
   11872             : 
   11873             :       /* Write the decl list.  */
   11874      185090 :       tree_list (CLASSTYPE_DECL_LIST (type), true);
   11875             : 
   11876      185090 :       if (TYPE_CONTAINS_VPTR_P (type))
   11877             :         {
   11878             :           /* Write the thunks.  */
   11879        6528 :           for (tree decls = TYPE_FIELDS (type);
   11880      196672 :                decls; decls = DECL_CHAIN (decls))
   11881      190144 :             if (TREE_CODE (decls) == FUNCTION_DECL
   11882      139460 :                 && DECL_VIRTUAL_P (decls)
   11883      225470 :                 && DECL_THUNKS (decls))
   11884             :               {
   11885        1452 :                 tree_node (decls);
   11886             :                 /* Thunks are always unique, so chaining is ok.  */
   11887        1452 :                 chained_decls (DECL_THUNKS (decls));
   11888             :               }
   11889        6528 :           tree_node (NULL_TREE);
   11890             :         }
   11891             :     }
   11892      187842 : }
   11893             : 
   11894             : void
   11895      220530 : trees_out::mark_class_member (tree member, bool do_defn)
   11896             : {
   11897      220530 :   gcc_assert (DECL_P (member));
   11898             : 
   11899      220530 :   member = member_owned_by_class (member);
   11900      220530 :   if (member)
   11901      220530 :     mark_declaration (member, do_defn && has_definition (member));
   11902      220530 : }
   11903             : 
   11904             : void
   11905      186093 : trees_out::mark_class_def (tree defn)
   11906             : {
   11907      186093 :   gcc_assert (DECL_P (defn));
   11908      186093 :   tree type = TREE_TYPE (defn);
   11909             :   /* Mark the class members that are not type-decls and cannot have
   11910             :      independent definitions.  */
   11911     1862948 :   for (tree member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
   11912     1676855 :     if (TREE_CODE (member) == FIELD_DECL
   11913     1676855 :         || TREE_CODE (member) == USING_DECL
   11914             :         /* A cloned enum-decl from 'using enum unrelated;'   */
   11915     1676855 :         || (TREE_CODE (member) == CONST_DECL
   11916        9184 :             && DECL_CONTEXT (member) == type))
   11917             :       {
   11918      220530 :         mark_class_member (member);
   11919      220530 :         if (TREE_CODE (member) == FIELD_DECL)
   11920      118370 :           if (tree repr = DECL_BIT_FIELD_REPRESENTATIVE (member))
   11921             :             /* If we're marking a class template definition, then
   11922             :                this'll contain the width (as set by grokbitfield)
   11923             :                instead of a decl.  */
   11924        1656 :             if (DECL_P (repr))
   11925        1564 :               mark_declaration (repr, false);
   11926             :       }
   11927             : 
   11928             :   /* Mark the binfo hierarchy.  */
   11929      453450 :   for (tree child = TYPE_BINFO (type); child; child = TREE_CHAIN (child))
   11930      267357 :     mark_by_value (child);
   11931             : 
   11932      186093 :   if (TYPE_LANG_SPECIFIC (type))
   11933             :     {
   11934      183341 :       for (tree vtable = CLASSTYPE_VTABLES (type);
   11935      191237 :            vtable; vtable = TREE_CHAIN (vtable))
   11936        7896 :         mark_declaration (vtable, true);
   11937             : 
   11938      183341 :       if (TYPE_CONTAINS_VPTR_P (type))
   11939             :         /* Mark the thunks, they belong to the class definition,
   11940             :            /not/ the thunked-to function.  */
   11941        6528 :         for (tree decls = TYPE_FIELDS (type);
   11942      196672 :              decls; decls = DECL_CHAIN (decls))
   11943      190144 :           if (TREE_CODE (decls) == FUNCTION_DECL)
   11944      139460 :             for (tree thunks = DECL_THUNKS (decls);
   11945      141388 :                  thunks; thunks = DECL_CHAIN (thunks))
   11946        1928 :               mark_declaration (thunks, false);
   11947             :     }
   11948      186093 : }
   11949             : 
   11950             : /* Nop sorting, needed for resorting the member vec.  */
   11951             : 
   11952             : static void
   11953    10307332 : nop (void *, void *, void *)
   11954             : {
   11955    10307332 : }
   11956             : 
   11957             : bool
   11958       97770 : trees_in::read_class_def (tree defn, tree maybe_template)
   11959             : {
   11960       97770 :   gcc_assert (DECL_P (defn));
   11961       98236 :   dump () && dump ("Reading class definition %N", defn);
   11962       97770 :   tree type = TREE_TYPE (defn);
   11963       97770 :   tree size = tree_node ();
   11964       97770 :   tree size_unit = tree_node ();
   11965       97770 :   tree vfield = tree_node ();
   11966       97770 :   tree binfo = tree_node ();
   11967       97770 :   vec<tree, va_gc> *vbase_vec = NULL;
   11968       97770 :   vec<tree, va_gc> *member_vec = NULL;
   11969       97770 :   vec<tree, va_gc> *pure_virts = NULL;
   11970       97770 :   vec<tree_pair_s, va_gc> *vcall_indices = NULL;
   11971       97770 :   tree key_method = NULL_TREE;
   11972       97770 :   tree lambda = NULL_TREE;
   11973             : 
   11974             :   /* Read the fields.  */
   11975       97770 :   vec<tree, va_heap> *fields = vec_chained_decls ();
   11976             : 
   11977       97770 :   if (TYPE_LANG_SPECIFIC (type))
   11978             :     {
   11979       95960 :       if (unsigned len = u ())
   11980             :         {
   11981       95960 :           vec_alloc (member_vec, len);
   11982      875262 :           for (unsigned ix = 0; ix != len; ix++)
   11983             :             {
   11984      683342 :               tree m = tree_node ();
   11985      683342 :               if (get_overrun ())
   11986             :                 break;
   11987      683342 :               if (TYPE_P (m))
   11988        6499 :                 m = TYPE_STUB_DECL (m);
   11989      683342 :               member_vec->quick_push (m);
   11990             :             }
   11991             :         }
   11992       95960 :       lambda = tree_node ();
   11993             : 
   11994       95960 :       if (!get_overrun ())
   11995             :         {
   11996       95960 :           unsigned nvbases = u ();
   11997       95960 :           if (nvbases)
   11998             :             {
   11999         524 :               vec_alloc (vbase_vec, nvbases);
   12000        2513 :               for (tree child = binfo; child; child = TREE_CHAIN (child))
   12001        1989 :                 if (BINFO_VIRTUAL_P (child))
   12002         524 :                   vbase_vec->quick_push (child);
   12003             :             }
   12004             :         }
   12005             : 
   12006       95960 :       if (!get_overrun ())
   12007             :         {
   12008       95960 :           int has_vptr = i ();
   12009       95960 :           if (has_vptr)
   12010             :             {
   12011        3950 :               pure_virts = tree_vec ();
   12012        3950 :               vcall_indices = tree_pair_vec ();
   12013        3950 :               key_method = tree_node ();
   12014             :             }
   12015             :         }
   12016             :     }
   12017             : 
   12018       97770 :   tree maybe_dup = odr_duplicate (maybe_template, TYPE_SIZE (type));
   12019       97770 :   bool installing = maybe_dup && !TYPE_SIZE (type);
   12020       44845 :   if (installing)
   12021             :     {
   12022       44845 :       if (DECL_EXTERNAL (defn) && TYPE_LANG_SPECIFIC (type))
   12023             :         {
   12024             :           /* We don't deal with not-really-extern, because, for a
   12025             :              module you want the import to be the interface, and for a
   12026             :              header-unit, you're doing it wrong.  */
   12027        2350 :           CLASSTYPE_INTERFACE_UNKNOWN (type) = false;
   12028        2350 :           CLASSTYPE_INTERFACE_ONLY (type) = true;
   12029             :         }
   12030             : 
   12031       44845 :       if (maybe_dup != defn)
   12032             :         {
   12033             :           // FIXME: This is needed on other defns too, almost
   12034             :           // duplicate-decl like?  See is_matching_decl too.
   12035             :           /* Copy flags from the duplicate.  */
   12036         327 :           tree type_dup = TREE_TYPE (maybe_dup);
   12037             : 
   12038             :           /* Core pieces.  */
   12039         327 :           TYPE_MODE_RAW (type) = TYPE_MODE_RAW (type_dup);
   12040         327 :           SET_DECL_MODE (defn, DECL_MODE (maybe_dup));
   12041         327 :           TREE_ADDRESSABLE (type) = TREE_ADDRESSABLE (type_dup);
   12042         327 :           DECL_SIZE (defn) = DECL_SIZE (maybe_dup);
   12043         327 :           DECL_SIZE_UNIT (defn) = DECL_SIZE_UNIT (maybe_dup);
   12044         327 :           DECL_ALIGN_RAW (defn) = DECL_ALIGN_RAW (maybe_dup);
   12045         654 :           DECL_WARN_IF_NOT_ALIGN_RAW (defn)
   12046         327 :             = DECL_WARN_IF_NOT_ALIGN_RAW (maybe_dup);
   12047         327 :           DECL_USER_ALIGN (defn) = DECL_USER_ALIGN (maybe_dup);
   12048             : 
   12049             :           /* C++ pieces.  */
   12050         327 :           TYPE_POLYMORPHIC_P (type) = TYPE_POLYMORPHIC_P (type_dup);
   12051         654 :           TYPE_HAS_USER_CONSTRUCTOR (type)
   12052         327 :             = TYPE_HAS_USER_CONSTRUCTOR (type_dup);
   12053         654 :           TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
   12054         327 :             = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type_dup);
   12055             : 
   12056         327 :           if (auto ls = TYPE_LANG_SPECIFIC (type_dup))
   12057             :             {
   12058         327 :               if (TYPE_LANG_SPECIFIC (type))
   12059             :                 {
   12060         981 :                   CLASSTYPE_BEFRIENDING_CLASSES (type_dup)
   12061         327 :                     = CLASSTYPE_BEFRIENDING_CLASSES (type);
   12062         327 :                   if (!ANON_AGGR_TYPE_P (type))
   12063         981 :                     CLASSTYPE_TYPEINFO_VAR (type_dup)
   12064         327 :                       = CLASSTYPE_TYPEINFO_VAR (type);
   12065             :                 }
   12066        1709 :               for (tree v = type; v; v = TYPE_NEXT_VARIANT (v))
   12067        1382 :                 TYPE_LANG_SPECIFIC (v) = ls;
   12068             :             }
   12069             :         }
   12070             : 
   12071       44845 :       TYPE_SIZE (type) = size;
   12072       44845 :       TYPE_SIZE_UNIT (type) = size_unit;
   12073             : 
   12074       44845 :       if (fields)
   12075             :         {
   12076       44845 :           tree *chain = &TYPE_FIELDS (type);
   12077       44845 :           unsigned len = fields->length ();
   12078      476111 :           for (unsigned ix = 0; ix != len; ix++)
   12079             :             {
   12080      431266 :               tree decl = (*fields)[ix];
   12081             : 
   12082      431266 :               if (!decl)
   12083             :                 {
   12084             :                   /* An anonymous struct with typedef name.  */
   12085           3 :                   tree tdef = (*fields)[ix+1];
   12086           3 :                   decl = TYPE_STUB_DECL (TREE_TYPE (tdef));
   12087           3 :                   gcc_checking_assert (IDENTIFIER_ANON_P (DECL_NAME (decl))
   12088             :                                        && decl != tdef);
   12089             :                 }
   12090             : 
   12091      788873 :               gcc_checking_assert (!*chain == !DECL_CLONED_FUNCTION_P (decl));
   12092      431266 :               *chain = decl;
   12093      431266 :               chain = &DECL_CHAIN (decl);
   12094             : 
   12095      431266 :               if (TREE_CODE (decl) == FIELD_DECL
   12096      431266 :                   && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
   12097         180 :                 ANON_AGGR_TYPE_FIELD
   12098         180 :                   (TYPE_MAIN_VARIANT (TREE_TYPE (decl))) = decl;
   12099             : 
   12100      431266 :               if (TREE_CODE (decl) == USING_DECL
   12101      431266 :                   && TREE_CODE (USING_DECL_SCOPE (decl)) == RECORD_TYPE)
   12102             :                 {
   12103             :                   /* Reconstruct DECL_ACCESS.  */
   12104       23604 :                   tree decls = USING_DECL_DECLS (decl);
   12105       23604 :                   tree access = declared_access (decl);
   12106             : 
   12107       27426 :                   for (ovl_iterator iter (decls); iter; ++iter)
   12108             :                     {
   12109        1911 :                       tree d = *iter;
   12110             : 
   12111        1911 :                       retrofit_lang_decl (d);
   12112        1911 :                       tree list = DECL_ACCESS (d);
   12113             : 
   12114        1911 :                       if (!purpose_member (type, list))
   12115        2236 :                         DECL_ACCESS (d) = tree_cons (type, access, list);
   12116             :                     }
   12117             :                 }
   12118             :             }
   12119             :         }
   12120             : 
   12121       44845 :       TYPE_VFIELD (type) = vfield;
   12122       44845 :       TYPE_BINFO (type) = binfo;
   12123             : 
   12124       44845 :       if (TYPE_LANG_SPECIFIC (type))
   12125             :         {
   12126       44057 :           CLASSTYPE_LAMBDA_EXPR (type) = lambda;
   12127             : 
   12128       44057 :           CLASSTYPE_MEMBER_VEC (type) = member_vec;
   12129       44057 :           CLASSTYPE_PURE_VIRTUALS (type) = pure_virts;
   12130       44057 :           CLASSTYPE_VCALL_INDICES (type) = vcall_indices;
   12131             : 
   12132       44057 :           CLASSTYPE_KEY_METHOD (type) = key_method;
   12133             : 
   12134       44057 :           CLASSTYPE_VBASECLASSES (type) = vbase_vec;
   12135             : 
   12136             :           /* Resort the member vector.  */
   12137       44057 :           resort_type_member_vec (member_vec, NULL, nop, NULL);
   12138             :         }
   12139             :     }
   12140             :   else if (maybe_dup)
   12141             :     {
   12142             :       // FIXME:QOI Check matching defn
   12143             :     }
   12144             : 
   12145       97770 :   if (TYPE_LANG_SPECIFIC (type))
   12146             :     {
   12147       95960 :       tree primary = tree_node ();
   12148       95960 :       tree as_base = tree_node ();
   12149             : 
   12150       95960 :       if (as_base)
   12151       46336 :         as_base = TREE_TYPE (as_base);
   12152             : 
   12153             :       /* Read the vtables.  */
   12154       95960 :       vec<tree, va_heap> *vtables = vec_chained_decls ();
   12155       95960 :       if (vtables)
   12156             :         {
   12157        3882 :           unsigned len = vtables->length ();
   12158        8732 :           for (unsigned ix = 0; ix != len; ix++)
   12159             :             {
   12160        4850 :               tree vtable = (*vtables)[ix];
   12161        4850 :               read_var_def (vtable, vtable);
   12162             :             }
   12163             :         }
   12164             : 
   12165       95960 :       tree friend_classes = tree_list (false);
   12166       95960 :       tree friend_functions = NULL_TREE;
   12167       95960 :       for (tree *chain = &friend_functions;
   12168      106382 :            tree name = tree_node (); chain = &TREE_CHAIN (*chain))
   12169             :         {
   12170       10422 :           tree val = tree_list (false);
   12171       10422 :           *chain = build_tree_list (name, val);
   12172       10422 :         }
   12173       95960 :       tree decl_list = tree_list (true);
   12174             : 
   12175       95960 :       if (installing)
   12176             :         {
   12177       44057 :           CLASSTYPE_PRIMARY_BINFO (type) = primary;
   12178       44057 :           CLASSTYPE_AS_BASE (type) = as_base;
   12179             : 
   12180       44057 :           if (vtables)
   12181             :             {
   12182        1679 :               if (!CLASSTYPE_KEY_METHOD (type)
   12183             :                   /* Sneaky user may have defined it inline
   12184             :                      out-of-class.  */
   12185        1679 :                   || DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (type)))
   12186        1055 :                 vec_safe_push (keyed_classes, type);
   12187        1679 :               unsigned len = vtables->length ();
   12188        1679 :               tree *chain = &CLASSTYPE_VTABLES (type);
   12189        3785 :               for (unsigned ix = 0; ix != len; ix++)
   12190             :                 {
   12191        2106 :                   tree vtable = (*vtables)[ix];
   12192        2106 :                   gcc_checking_assert (!*chain);
   12193        2106 :                   *chain = vtable;
   12194        2106 :                   chain = &DECL_CHAIN (vtable);
   12195             :                 }
   12196             :             }
   12197       44057 :           CLASSTYPE_FRIEND_CLASSES (type) = friend_classes;
   12198       44057 :           DECL_FRIENDLIST (defn) = friend_functions;
   12199       44057 :           CLASSTYPE_DECL_LIST (type) = decl_list;
   12200             : 
   12201       46192 :           for (; friend_classes; friend_classes = TREE_CHAIN (friend_classes))
   12202             :             {
   12203        2135 :               tree f = TREE_VALUE (friend_classes);
   12204             : 
   12205        2135 :               if (CLASS_TYPE_P (f))
   12206             :                 {
   12207        1378 :                   CLASSTYPE_BEFRIENDING_CLASSES (f)
   12208        2756 :                     = tree_cons (NULL_TREE, type,
   12209        1378 :                                  CLASSTYPE_BEFRIENDING_CLASSES (f));
   12210        2141 :                   dump () && dump ("Class %N befriending %C:%N",
   12211           6 :                                    type, TREE_CODE (f), f);
   12212             :                 }
   12213             :             }
   12214             : 
   12215       48639 :           for (; friend_functions;
   12216        4582 :                friend_functions = TREE_CHAIN (friend_functions))
   12217        4582 :             for (tree friend_decls = TREE_VALUE (friend_functions);
   12218       10215 :                  friend_decls; friend_decls = TREE_CHAIN (friend_decls))
   12219             :               {
   12220        5633 :                 tree f = TREE_VALUE (friend_decls);
   12221             :                 
   12222        5633 :                 DECL_BEFRIENDING_CLASSES (f)
   12223        5633 :                   = tree_cons (NULL_TREE, type, DECL_BEFRIENDING_CLASSES (f));
   12224        5660 :                 dump () && dump ("Class %N befriending %C:%N",
   12225          27 :                                  type, TREE_CODE (f), f);
   12226             :               }
   12227             :         }
   12228             : 
   12229       95960 :       if (TYPE_CONTAINS_VPTR_P (type))
   12230             :         /* Read and install the thunks.  */
   12231        4832 :         while (tree vfunc = tree_node ())
   12232             :           {
   12233         882 :             tree thunks = chained_decls ();
   12234         882 :             if (installing)
   12235         396 :               SET_DECL_THUNKS (vfunc, thunks);
   12236             :           }
   12237             : 
   12238       95960 :       vec_free (vtables);
   12239             :     }
   12240             : 
   12241             :   /* Propagate to all variants.  */
   12242       97770 :   if (installing)
   12243       44845 :     fixup_type_variants (type);
   12244             : 
   12245             :   /* IS_FAKE_BASE_TYPE is inaccurate at this point, because if this is
   12246             :      the fake base, we've not hooked it into the containing class's
   12247             :      data structure yet.  Fortunately it has a unique name.  */
   12248       44845 :   if (installing
   12249       44845 :       && DECL_NAME (defn) != as_base_identifier
   12250       44057 :       && (!CLASSTYPE_TEMPLATE_INFO (type)
   12251       38934 :           || !uses_template_parms (TI_ARGS (CLASSTYPE_TEMPLATE_INFO (type)))))
   12252             :     /* Emit debug info.  It'd be nice to know if the interface TU
   12253             :        already emitted this.  */
   12254       21863 :     rest_of_type_compilation (type, !LOCAL_CLASS_P (type));
   12255             : 
   12256       97770 :   vec_free (fields);
   12257             : 
   12258       97770 :   return !get_overrun ();
   12259             : }
   12260             : 
   12261             : void
   12262        9576 : trees_out::write_enum_def (tree decl)
   12263             : {
   12264        9576 :   tree type = TREE_TYPE (decl);
   12265             : 
   12266        9576 :   tree_node (TYPE_VALUES (type));
   12267             :   /* Note that we stream TYPE_MIN/MAX_VALUE directly as part of the
   12268             :      ENUMERAL_TYPE.  */
   12269        9576 : }
   12270             : 
   12271             : void
   12272        9576 : trees_out::mark_enum_def (tree decl)
   12273             : {
   12274        9576 :   tree type = TREE_TYPE (decl);
   12275             : 
   12276       44578 :   for (tree values = TYPE_VALUES (type); values; values = TREE_CHAIN (values))
   12277             :     {
   12278       35002 :       tree cst = TREE_VALUE (values);
   12279       35002 :       mark_by_value (cst);
   12280             :       /* We must mark the init to avoid circularity in tt_enum_int.  */
   12281       35002 :       if (tree init = DECL_INITIAL (cst))
   12282       34790 :         if (TREE_CODE (init) == INTEGER_CST)
   12283       34250 :           mark_by_value (init);
   12284             :     }
   12285        9576 : }
   12286             : 
   12287             : bool
   12288        4587 : trees_in::read_enum_def (tree defn, tree maybe_template)
   12289             : {
   12290        4587 :   tree type = TREE_TYPE (defn);
   12291        4587 :   tree values = tree_node ();
   12292             : 
   12293        4587 :   if (get_overrun ())
   12294             :     return false;
   12295             : 
   12296        4587 :   tree maybe_dup = odr_duplicate (maybe_template, TYPE_VALUES (type));
   12297        9174 :   bool installing = maybe_dup && !TYPE_VALUES (type);
   12298             : 
   12299        4587 :   if (installing)
   12300             :     {
   12301        1887 :       TYPE_VALUES (type) = values;
   12302             :       /* Note that we stream TYPE_MIN/MAX_VALUE directly as part of the
   12303             :          ENUMERAL_TYPE.  */
   12304             : 
   12305        3162 :       rest_of_type_compilation (type, DECL_NAMESPACE_SCOPE_P (defn));
   12306             :     }
   12307        2700 :   else if (maybe_dup)
   12308             :     {
   12309        2700 :       tree known = TYPE_VALUES (type);
   12310       14359 :       for (; known && values;
   12311       11659 :            known = TREE_CHAIN (known), values = TREE_CHAIN (values))
   12312             :         {
   12313       11668 :           tree known_decl = TREE_VALUE (known);
   12314       11668 :           tree new_decl = TREE_VALUE (values);
   12315             : 
   12316       11668 :           if (DECL_NAME (known_decl) != DECL_NAME (new_decl))
   12317             :             break;
   12318             :               
   12319       11662 :           new_decl = maybe_duplicate (new_decl);
   12320             : 
   12321       11662 :           if (!cp_tree_equal (DECL_INITIAL (known_decl),
   12322       11662 :                               DECL_INITIAL (new_decl)))
   12323             :             break;
   12324             :         }
   12325             : 
   12326        2700 :       if (known || values)
   12327             :         {
   12328          12 :           error_at (DECL_SOURCE_LOCATION (maybe_dup),
   12329             :                     "definition of %qD does not match", maybe_dup);
   12330          12 :           inform (DECL_SOURCE_LOCATION (defn),
   12331             :                   "existing definition %qD", defn);
   12332             : 
   12333          12 :           tree known_decl = NULL_TREE, new_decl = NULL_TREE;
   12334             : 
   12335          12 :           if (known)
   12336           9 :             known_decl = TREE_VALUE (known);
   12337          12 :           if (values)
   12338          12 :             new_decl = maybe_duplicate (TREE_VALUE (values));
   12339             : 
   12340          12 :           if (known_decl && new_decl)
   12341             :             {
   12342           9 :               inform (DECL_SOURCE_LOCATION (new_decl),
   12343             :                       "... this enumerator %qD", new_decl);
   12344           9 :               inform (DECL_SOURCE_LOCATION (known_decl),
   12345             :                       "enumerator %qD does not match ...", known_decl);
   12346             :             }
   12347           3 :           else if (known_decl || new_decl)
   12348             :             {
   12349           3 :               tree extra = known_decl ? known_decl : new_decl;
   12350           3 :               inform (DECL_SOURCE_LOCATION (extra),
   12351             :                       "additional enumerators beginning with %qD", extra);
   12352             :             }
   12353             :           else
   12354           0 :             inform (DECL_SOURCE_LOCATION (maybe_dup),
   12355             :                     "enumeration range differs");
   12356             : 
   12357             :           /* Mark it bad.  */
   12358          12 :           unmatched_duplicate (maybe_template);
   12359             :         }
   12360             :     }
   12361             : 
   12362             :   return true;
   12363             : }
   12364             : 
   12365             : /* Write out the body of DECL.  See above circularity note.  */
   12366             : 
   12367             : void
   12368      735612 : trees_out::write_definition (tree decl)
   12369             : {
   12370      735612 :   if (streaming_p ())
   12371             :     {
   12372      366930 :       assert_definition (decl);
   12373      366930 :       dump ()
   12374        8657 :         && dump ("Writing definition %C:%N", TREE_CODE (decl), decl);
   12375             :     }
   12376             :   else
   12377      368682 :     dump (dumper::DEPEND)
   12378        8204 :       && dump ("Depending definition %C:%N", TREE_CODE (decl), decl);
   12379             : 
   12380     1176334 :  again:
   12381     1176334 :   switch (TREE_CODE (decl))
   12382             :     {
   12383           0 :     default:
   12384           0 :       gcc_unreachable ();
   12385             : 
   12386      440722 :     case TEMPLATE_DECL:
   12387      440722 :       decl = DECL_TEMPLATE_RESULT (decl);
   12388      440722 :       goto again;
   12389             : 
   12390      447166 :     case FUNCTION_DECL:
   12391      447166 :       write_function_def (decl);
   12392      447166 :       break;
   12393             : 
   12394      197418 :     case TYPE_DECL:
   12395      197418 :       {
   12396      197418 :         tree type = TREE_TYPE (decl);
   12397      197418 :         gcc_assert (TYPE_MAIN_VARIANT (type) == type
   12398             :                     && TYPE_NAME (type) == decl);
   12399      197418 :         if (TREE_CODE (type) == ENUMERAL_TYPE)
   12400        9576 :           write_enum_def (decl);
   12401             :         else
   12402      187842 :           write_class_def (decl);
   12403             :       }
   12404             :       break;
   12405             : 
   12406       91028 :     case VAR_DECL:
   12407       91028 :     case CONCEPT_DECL:
   12408       91028 :       write_var_def (decl);
   12409       91028 :       break;
   12410             :     }
   12411      735612 : }
   12412             : 
   12413             : /* Mark a declaration for by-value walking.  If DO_DEFN is true, mark
   12414             :    its body too.  */
   12415             : 
   12416             : void
   12417     3285483 : trees_out::mark_declaration (tree decl, bool do_defn)
   12418             : {
   12419     3285483 :   mark_by_value (decl);
   12420             : 
   12421     3285483 :   if (TREE_CODE (decl) == TEMPLATE_DECL)
   12422     1101570 :     decl = DECL_TEMPLATE_RESULT (decl);
   12423             : 
   12424     3285483 :   if (!do_defn)
   12425             :     return;
   12426             : 
   12427      733863 :   switch (TREE_CODE (decl))
   12428             :     {
   12429           0 :     default:
   12430           0 :       gcc_unreachable ();
   12431             : 
   12432             :     case FUNCTION_DECL:
   12433             :       mark_function_def (decl);
   12434             :       break;
   12435             : 
   12436      195669 :     case TYPE_DECL:
   12437      195669 :       {
   12438      195669 :         tree type = TREE_TYPE (decl);
   12439      195669 :         gcc_assert (TYPE_MAIN_VARIANT (type) == type
   12440             :                     && TYPE_NAME (type) == decl);
   12441      195669 :         if (TREE_CODE (type) == ENUMERAL_TYPE)
   12442        9576 :           mark_enum_def (decl);
   12443             :         else
   12444      186093 :           mark_class_def (decl);
   12445             :       }
   12446             :       break;
   12447             : 
   12448             :     case VAR_DECL:
   12449             :     case CONCEPT_DECL:
   12450             :       mark_var_def (decl);
   12451             :       break;
   12452             :     }
   12453             : }
   12454             : 
   12455             : /* Read in the body of DECL.  See above circularity note.  */
   12456             : 
   12457             : bool
   12458      420307 : trees_in::read_definition (tree decl)
   12459             : {
   12460      421265 :   dump () && dump ("Reading definition %C %N", TREE_CODE (decl), decl);
   12461             : 
   12462             :   tree maybe_template = decl;
   12463             : 
   12464      420307 :  again:
   12465      674054 :   switch (TREE_CODE (decl))
   12466             :     {
   12467             :     default:
   12468             :       break;
   12469             : 
   12470      253747 :     case TEMPLATE_DECL:
   12471      253747 :       decl = DECL_TEMPLATE_RESULT (decl);
   12472      253747 :       goto again;
   12473             : 
   12474      268713 :     case FUNCTION_DECL:
   12475      268713 :       return read_function_def (decl, maybe_template);
   12476             : 
   12477      102357 :     case TYPE_DECL:
   12478      102357 :       {
   12479      102357 :         tree type = TREE_TYPE (decl);
   12480      102357 :         gcc_assert (TYPE_MAIN_VARIANT (type) == type
   12481             :                     && TYPE_NAME (type) == decl);
   12482      102357 :         if (TREE_CODE (type) == ENUMERAL_TYPE)
   12483        4587 :           return read_enum_def (decl, maybe_template);
   12484             :         else
   12485       97770 :           return read_class_def (decl, maybe_template);
   12486             :       }
   12487       49237 :       break;
   12488             : 
   12489       49237 :     case VAR_DECL:
   12490       49237 :     case CONCEPT_DECL:
   12491       49237 :       return read_var_def (decl, maybe_template);
   12492             :     }
   12493             : 
   12494             :   return false;
   12495             : }
   12496             : 
   12497             : /* Lookup an maybe insert a slot for depset for KEY.  */
   12498             : 
   12499             : depset **
   12500    14202381 : depset::hash::entity_slot (tree entity, bool insert)
   12501             : {
   12502    14202381 :   traits::compare_type key (entity, NULL);
   12503    21142380 :   depset **slot = find_slot_with_hash (key, traits::hash (key),
   12504             :                                        insert ? INSERT : NO_INSERT);
   12505             : 
   12506    14202381 :   return slot;
   12507             : }
   12508             : 
   12509             : depset **
   12510      167250 : depset::hash::binding_slot (tree ctx, tree name, bool insert)
   12511             : {
   12512      167250 :   traits::compare_type key (ctx, name);
   12513      167250 :   depset **slot = find_slot_with_hash (key, traits::hash (key),
   12514             :                                        insert ? INSERT : NO_INSERT);
   12515             : 
   12516      167250 :   return slot;
   12517             : }
   12518             : 
   12519             : depset *
   12520     6571972 : depset::hash::find_dependency (tree decl)
   12521             : {
   12522     6571972 :   depset **slot = entity_slot (decl, false);
   12523             : 
   12524     6571972 :   return slot ? *slot : NULL;
   12525             : }
   12526             : 
   12527             : depset *
   12528           0 : depset::hash::find_binding (tree ctx, tree name)
   12529             : {
   12530           0 :   depset **slot = binding_slot (ctx, name, false);
   12531             : 
   12532           0 :   return slot ? *slot : NULL;
   12533             : }
   12534             : 
   12535             : /* DECL is a newly discovered dependency.  Create the depset, if it
   12536             :    doesn't already exist.  Add it to the worklist if so.
   12537             : 
   12538             :    DECL will be an OVL_USING_P OVERLOAD, if it's from a binding that's
   12539             :    a using decl.
   12540             : 
   12541             :    We do not have to worry about adding the same dependency more than
   12542             :    once.  First it's harmless, but secondly the TREE_VISITED marking
   12543             :    prevents us wanting to do it anyway.  */
   12544             : 
   12545             : depset *
   12546     6244533 : depset::hash::make_dependency (tree decl, entity_kind ek)
   12547             : {
   12548             :   /* Make sure we're being told consistent information.  */
   12549    11561580 :   gcc_checking_assert ((ek == EK_NAMESPACE)
   12550             :                        == (TREE_CODE (decl) == NAMESPACE_DECL
   12551             :                            && !DECL_NAMESPACE_ALIAS (decl)));
   12552     6244533 :   gcc_checking_assert (ek != EK_BINDING && ek != EK_REDIRECT);
   12553     6244533 :   gcc_checking_assert (TREE_CODE (decl) != FIELD_DECL
   12554             :                        && (TREE_CODE (decl) != USING_DECL
   12555             :                            || TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL));
   12556     6244533 :   gcc_checking_assert (!is_key_order ());
   12557     6244533 :   if (ek == EK_USING)
   12558       22229 :     gcc_checking_assert (TREE_CODE (decl) == OVERLOAD);
   12559             : 
   12560     6244533 :   if (TREE_CODE (decl) == TEMPLATE_DECL)
   12561             :     /* The template should have copied these from its result decl.  */
   12562     2219930 :     gcc_checking_assert (DECL_MODULE_EXPORT_P (decl)
   12563             :                          == DECL_MODULE_EXPORT_P (DECL_TEMPLATE_RESULT (decl)));
   12564             : 
   12565     6244533 :   depset **slot = entity_slot (decl, true);
   12566     6244533 :   depset *dep = *slot;
   12567     6244533 :   bool for_binding = ek == EK_FOR_BINDING;
   12568             : 
   12569     6244533 :   if (!dep)
   12570             :     {
   12571      459573 :       if ((DECL_IMPLICIT_TYPEDEF_P (decl)
   12572             :            /* ... not an enum, for instance.  */
   12573      240897 :            && RECORD_OR_UNION_TYPE_P (TREE_TYPE (decl))
   12574      236024 :            && TYPE_LANG_SPECIFIC (TREE_TYPE (decl))
   12575      210962 :            && CLASSTYPE_USE_TEMPLATE (TREE_TYPE (decl)) == 2)
   12576     1647950 :           || (VAR_P (decl)
   12577       38856 :               && DECL_LANG_SPECIFIC (decl)
   12578       38838 :               && DECL_USE_TEMPLATE (decl) == 2))
   12579             :         {
   12580             :           /* A partial or explicit specialization. Partial
   12581             :              specializations might not be in the hash table, because
   12582             :              there can be multiple differently-constrained variants.
   12583             : 
   12584             :              template<typename T> class silly;
   12585             :              template<typename T> requires true class silly {};
   12586             : 
   12587             :              We need to find them, insert their TEMPLATE_DECL in the
   12588             :              dep_hash, and then convert the dep we just found into a
   12589             :              redirect.  */
   12590             : 
   12591       41529 :           tree ti = get_template_info (decl);
   12592       41529 :           tree tmpl = TI_TEMPLATE (ti);
   12593       41529 :           tree partial = NULL_TREE;
   12594       41529 :           for (tree spec = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
   12595      144479 :                spec; spec = TREE_CHAIN (spec))
   12596      127408 :             if (DECL_TEMPLATE_RESULT (TREE_VALUE (spec)) == decl)
   12597             :               {
   12598             :                 partial = TREE_VALUE (spec);
   12599             :                 break;
   12600             :               }
   12601             : 
   12602       41529 :           if (partial)
   12603             :             {
   12604             :               /* Eagerly create an empty redirect.  The following
   12605             :                  make_dependency call could cause hash reallocation,
   12606             :                  and invalidate slot's value.  */
   12607       24458 :               depset *redirect = make_entity (decl, EK_REDIRECT);
   12608             : 
   12609             :               /* Redirects are never reached -- always snap to their target.  */
   12610       24458 :               redirect->set_flag_bit<DB_UNREACHED_BIT> ();
   12611             : 
   12612       24458 :               *slot = redirect;
   12613             : 
   12614       24458 :               depset *tmpl_dep = make_dependency (partial, EK_PARTIAL);
   12615       24458 :               gcc_checking_assert (tmpl_dep->get_entity_kind () == EK_PARTIAL);
   12616             : 
   12617       24458 :               redirect->deps.safe_push (tmpl_dep);
   12618             : 
   12619       24458 :               return redirect;
   12620             :             }
   12621             :         }
   12622             : 
   12623     1201479 :       bool has_def = ek != EK_USING && has_definition (decl);
   12624     1179250 :       if (ek > EK_BINDING)
   12625      184708 :         ek = EK_DECL;
   12626             : 
   12627             :       /* The only OVERLOADS we should see are USING decls from
   12628             :          bindings.  */
   12629     1201479 :       *slot = dep = make_entity (decl, ek, has_def);
   12630             : 
   12631     1201479 :       if (TREE_CODE (decl) == TEMPLATE_DECL)
   12632             :         {
   12633      371628 :           if (DECL_ALIAS_TEMPLATE_P (decl) && DECL_TEMPLATE_INFO (decl))
   12634        3601 :             dep->set_flag_bit<DB_ALIAS_TMPL_INST_BIT> ();
   12635      368027 :           else if (CHECKING_P)
   12636             :             /* The template_result should otherwise not be in the
   12637             :                table, or be an empty redirect (created above).  */
   12638      368027 :             if (auto *eslot = entity_slot (DECL_TEMPLATE_RESULT (decl), false))
   12639       24458 :               gcc_checking_assert ((*eslot)->get_entity_kind () == EK_REDIRECT
   12640             :                                    && !(*eslot)->deps.length ());
   12641             :         }
   12642             : 
   12643     1201479 :       if (ek != EK_USING)
   12644             :         {
   12645     1179250 :           tree not_tmpl = STRIP_TEMPLATE (decl);
   12646             : 
   12647     1179250 :           if (DECL_LANG_SPECIFIC (not_tmpl)
   12648     2323018 :               && DECL_MODULE_IMPORT_P (not_tmpl))
   12649             :             {
   12650             :               /* Store the module number and index in cluster/section,
   12651             :                  so we don't have to look them up again.  */
   12652        3885 :               unsigned index = import_entity_index (decl);
   12653        3885 :               module_state *from = import_entity_module (index);
   12654             :               /* Remap will be zero for imports from partitions, which
   12655             :                  we want to treat as-if declared in this TU.  */
   12656        3885 :               if (from->remap)
   12657             :                 {
   12658        3738 :                   dep->cluster = index - from->entity_lwm;
   12659        3738 :                   dep->section = from->remap;
   12660        3738 :                   dep->set_flag_bit<DB_IMPORTED_BIT> ();
   12661             :                 }
   12662             :             }
   12663             : 
   12664     1179250 :           if (ek == EK_DECL
   12665      578045 :               && !dep->is_import ()
   12666      577708 :               && TREE_CODE (CP_DECL_CONTEXT (decl)) == NAMESPACE_DECL
   12667     1385402 :               && !(TREE_CODE (decl) == TEMPLATE_DECL
   12668       95487 :                    && DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (decl)))
   12669             :             {
   12670      201629 :               tree ctx = CP_DECL_CONTEXT (decl);
   12671             : 
   12672      201629 :               if (!TREE_PUBLIC (ctx))
   12673             :                 /* Member of internal namespace.  */
   12674           3 :                 dep->set_flag_bit<DB_IS_INTERNAL_BIT> ();
   12675      201626 :               else if (VAR_OR_FUNCTION_DECL_P (not_tmpl)
   12676      201626 :                        && DECL_THIS_STATIC (not_tmpl))
   12677             :                 {
   12678             :                   /* An internal decl.  This is ok in a GM entity.  */
   12679        1508 :                   if (!(header_module_p ()
   12680           3 :                         || !DECL_LANG_SPECIFIC (not_tmpl)
   12681           3 :                         || !DECL_MODULE_PURVIEW_P (not_tmpl)))
   12682           3 :                     dep->set_flag_bit<DB_IS_INTERNAL_BIT> ();
   12683             :                 }
   12684             :             }
   12685             :         }
   12686             : 
   12687     1201479 :       if (!dep->is_import ())
   12688     1197741 :         worklist.safe_push (dep);
   12689             :     }
   12690             : 
   12691     6220075 :   dump (dumper::DEPEND)
   12692      135164 :     && dump ("%s on %s %C:%N found",
   12693             :              ek == EK_REDIRECT ? "Redirect"
   12694      135164 :              : for_binding ? "Binding" : "Dependency",
   12695      135164 :              dep->entity_kind_name (), TREE_CODE (decl), decl);
   12696             : 
   12697     6220075 :   return dep;
   12698             : }
   12699             : 
   12700             : /* DEP is a newly discovered dependency.  Append it to current's
   12701             :    depset.  */
   12702             : 
   12703             : void
   12704     4684555 : depset::hash::add_dependency (depset *dep)
   12705             : {
   12706     4684555 :   gcc_checking_assert (current && !is_key_order ());
   12707     4684555 :   current->deps.safe_push (dep);
   12708             : 
   12709     4684555 :   if (dep->is_internal () && !current->is_internal ())
   12710          12 :     current->set_flag_bit<DB_REFS_INTERNAL_BIT> ();
   12711             : 
   12712     4684555 :   if (current->get_entity_kind () == EK_USING
   12713       22229 :       && DECL_IMPLICIT_TYPEDEF_P (dep->get_entity ())
   12714     4692867 :       && TREE_CODE (TREE_TYPE (dep->get_entity ())) == ENUMERAL_TYPE)
   12715             :     {
   12716             :       /* CURRENT is an unwrapped using-decl and DECL is an enum's
   12717             :          implicit typedef.  Is CURRENT a member of the enum?  */
   12718        8157 :       tree c_decl = OVL_FUNCTION (current->get_entity ());
   12719             : 
   12720        8157 :       if (TREE_CODE (c_decl) == CONST_DECL
   12721       16296 :           && (current->deps[0]->get_entity ()
   12722        8139 :               == CP_DECL_CONTEXT (dep->get_entity ())))
   12723             :         /* Make DECL depend on CURRENT.  */
   12724        8082 :         dep->deps.safe_push (current);
   12725             :     }
   12726             : 
   12727     4684555 :   if (dep->is_unreached ())
   12728             :     {
   12729             :       /* The dependency is reachable now.  */
   12730      371089 :       reached_unreached = true;
   12731      371089 :       dep->clear_flag_bit<DB_UNREACHED_BIT> ();
   12732      371089 :       dump (dumper::DEPEND)
   12733        9080 :         && dump ("Reaching unreached %s %C:%N", dep->entity_kind_name (),
   12734        9080 :                  TREE_CODE (dep->get_entity ()), dep->get_entity ());
   12735             :     }
   12736     4684555 : }
   12737             : 
   12738             : depset *
   12739     7129703 : depset::hash::add_dependency (tree decl, entity_kind ek)
   12740             : {
   12741     7129703 :   depset *dep;
   12742             : 
   12743     7129703 :   if (is_key_order ())
   12744             :     {
   12745     2384710 :       dep = find_dependency (decl);
   12746     2384710 :       if (dep)
   12747             :         {
   12748     1138425 :           current->deps.safe_push (dep);
   12749     1138425 :           dump (dumper::MERGE)
   12750         456 :             && dump ("Key dependency on %s %C:%N found",
   12751         456 :                      dep->entity_kind_name (), TREE_CODE (decl), decl);
   12752             :         }
   12753             :       else
   12754             :         {
   12755             :           /* It's not a mergeable decl, look for it in the original
   12756             :              table.  */
   12757     1246285 :           dep = chain->find_dependency (decl);
   12758     1246285 :           gcc_checking_assert (dep);
   12759             :         }
   12760             :     }
   12761             :   else
   12762             :     {
   12763     4744993 :       dep = make_dependency (decl, ek);
   12764     4744993 :       if (dep->get_entity_kind () != EK_REDIRECT)
   12765     4684555 :         add_dependency (dep);
   12766             :     }
   12767             : 
   12768     7129703 :   return dep;
   12769             : }
   12770             : 
   12771             : void
   12772      609459 : depset::hash::add_namespace_context (depset *dep, tree ns)
   12773             : {
   12774      609459 :   depset *ns_dep = make_dependency (ns, depset::EK_NAMESPACE);
   12775      609459 :   dep->deps.safe_push (ns_dep);
   12776             : 
   12777             :   /* Mark it as special if imported so we don't walk connect when
   12778             :      SCCing.  */
   12779      609459 :   if (!dep->is_binding () && ns_dep->is_import ())
   12780          45 :     dep->set_special ();
   12781      609459 : }
   12782             : 
   12783             : struct add_binding_data
   12784             : {
   12785             :   tree ns;
   12786             :   bitmap partitions;
   12787             :   depset *binding;
   12788             :   depset::hash *hash;
   12789             :   bool met_namespace;
   12790             : };
   12791             : 
   12792             : /* Return true if we are, or contain something that is exported.  */
   12793             : 
   12794             : bool
   12795     3314696 : depset::hash::add_binding_entity (tree decl, WMB_Flags flags, void *data_)
   12796             : {
   12797     3314696 :   auto data = static_cast <add_binding_data *> (data_);
   12798             : 
   12799     3314696 :   if (!(TREE_CODE (decl) == NAMESPACE_DECL && !DECL_NAMESPACE_ALIAS (decl)))
   12800             :     {
   12801     3309230 :       tree inner = decl;
   12802             : 
   12803     3309230 :       if (TREE_CODE (inner) == CONST_DECL
   12804     3309230 :           && TREE_CODE (DECL_CONTEXT (inner)) == ENUMERAL_TYPE)
   12805        8799 :         inner = TYPE_NAME (DECL_CONTEXT (inner));
   12806     3300431 :       else if (TREE_CODE (inner) == TEMPLATE_DECL)
   12807      106670 :         inner = DECL_TEMPLATE_RESULT (inner);
   12808             : 
   12809     6512001 :       if (!DECL_LANG_SPECIFIC (inner) || !DECL_MODULE_PURVIEW_P (inner))
   12810             :         /* Ignore global module fragment entities.  */
   12811             :         return false;
   12812             : 
   12813      206964 :       if (VAR_OR_FUNCTION_DECL_P (inner)
   12814      206964 :           && DECL_THIS_STATIC (inner))
   12815             :         {
   12816        1535 :           if (!header_module_p ())
   12817             :             /* Ignore internal-linkage entitites.  */
   12818             :             return false;
   12819             :         }
   12820             : 
   12821      206934 :       if ((TREE_CODE (decl) == VAR_DECL
   12822      206934 :            || TREE_CODE (decl) == TYPE_DECL)
   12823      206934 :           && DECL_TINFO_P (decl))
   12824             :         /* Ignore TINFO things.  */
   12825             :         return false;
   12826             : 
   12827      206934 :       if (TREE_CODE (decl) == VAR_DECL && DECL_NTTP_OBJECT_P (decl))
   12828             :         /* Ignore NTTP objects.  */
   12829             :         return false;
   12830             : 
   12831      206934 :       if (!(flags & WMB_Using) && CP_DECL_CONTEXT (decl) != data->ns)
   12832             :         {
   12833             :           /* A using that lost its wrapper or an unscoped enum
   12834             :              constant.  */
   12835        9689 :           flags = WMB_Flags (flags | WMB_Using);
   12836        9689 :           if (DECL_MODULE_EXPORT_P (TREE_CODE (decl) == CONST_DECL
   12837             :                                     ? TYPE_NAME (TREE_TYPE (decl))
   12838             :                                     : STRIP_TEMPLATE (decl)))
   12839        9686 :             flags = WMB_Flags (flags | WMB_Export);
   12840             :         }
   12841             : 
   12842      206934 :       if (!data->binding)
   12843             :         /* No binding to check.  */;
   12844       39684 :       else if (flags & WMB_Using)
   12845             :         {
   12846             :           /* Look in the binding to see if we already have this
   12847             :              using.  */
   12848       17550 :           for (unsigned ix = data->binding->deps.length (); --ix;)
   12849             :             {
   12850       11834 :               depset *d = data->binding->deps[ix];
   12851       23668 :               if (d->get_entity_kind () == EK_USING
   12852       11834 :                   && OVL_FUNCTION (d->get_entity ()) == decl)
   12853             :                 {
   12854           0 :                   if (!(flags & WMB_Hidden))
   12855           0 :                     d->clear_hidden_binding ();
   12856           0 :                   if (flags & WMB_Export)
   12857           0 :                     OVL_EXPORT_P (d->get_entity ()) = true;
   12858           0 :                   return bool (flags & WMB_Export);
   12859             :                 }
   12860             :             }
   12861             :         }
   12862       36826 :       else if (flags & WMB_Dups)
   12863             :         {
   12864             :           /* Look in the binding to see if we already have this decl.  */
   12865          18 :           for (unsigned ix = data->binding->deps.length (); --ix;)
   12866             :             {
   12867           9 :               depset *d = data->binding->deps[ix];
   12868           9 :               if (d->get_entity () == decl)
   12869             :                 {
   12870           9 :                   if (!(flags & WMB_Hidden))
   12871           9 :                     d->clear_hidden_binding ();
   12872           9 :                   return false;
   12873             :                 }
   12874             :             }
   12875             :         }
   12876             : 
   12877             :       /* We're adding something.  */
   12878      206925 :       if (!data->binding)
   12879             :         {
   12880      167250 :           data->binding = make_binding (data->ns, DECL_NAME (decl));
   12881      167250 :           data->hash->add_namespace_context (data->binding, data->ns);
   12882             : 
   12883      167250 :           depset **slot = data->hash->binding_slot (data->ns,
   12884      167250 :                                                     DECL_NAME (decl), true);
   12885      167250 :           gcc_checking_assert (!*slot);
   12886      167250 :           *slot = data->binding;
   12887             :         }
   12888             : 
   12889             :       /* Make sure nobody left a tree visited lying about.  */
   12890      206925 :       gcc_checking_assert (!TREE_VISITED (decl));
   12891             : 
   12892      206925 :       if (flags & WMB_Using)
   12893             :         {
   12894       22229 :           decl = ovl_make (decl, NULL_TREE);
   12895       22229 :           if (flags & WMB_Export)
   12896       22220 :             OVL_EXPORT_P (decl) = true;
   12897             :         }
   12898             : 
   12899      206925 :       depset *dep = data->hash->make_dependency
   12900      391621 :         (decl, flags & WMB_Using ? EK_USING : EK_FOR_BINDING);
   12901      206925 :       if (flags & WMB_Hidden)
   12902        4517 :         dep->set_hidden_binding ();
   12903      206925 :       data->binding->deps.safe_push (dep);
   12904             :       /* Binding and contents are mutually dependent.  */
   12905      206925 :       dep->deps.safe_push (data->binding);
   12906             : 
   12907      206925 :       return (flags & WMB_Using
   12908      206925 :               ? flags & WMB_Export : DECL_MODULE_EXPORT_P (decl));
   12909             :     }
   12910        5466 :   else if (DECL_NAME (decl) && !data->met_namespace)
   12911             :     {
   12912             :       /* Namespace, walk exactly once.  */
   12913        5448 :       gcc_checking_assert (TREE_PUBLIC (decl));
   12914        5448 :       data->met_namespace = true;
   12915        5448 :       if (data->hash->add_namespace_entities (decl, data->partitions))
   12916             :         {
   12917             :           /* It contains an exported thing, so it is exported.  */
   12918        1590 :           gcc_checking_assert (DECL_MODULE_PURVIEW_P (decl));
   12919        1590 :           DECL_MODULE_EXPORT_P (decl) = true;
   12920             :         }
   12921             : 
   12922        5448 :       if (DECL_MODULE_PURVIEW_P (decl))
   12923             :         {
   12924        1782 :           data->hash->make_dependency (decl, depset::EK_NAMESPACE);
   12925             : 
   12926        1782 :           return DECL_MODULE_EXPORT_P (decl);
   12927             :         }
   12928             :     }
   12929             : 
   12930             :   return false;
   12931             : }
   12932             : 
   12933             : /* Recursively find all the namespace bindings of NS.  Add a depset
   12934             :    for every binding that contains an export or module-linkage entity.
   12935             :    Add a defining depset for every such decl that we need to write a
   12936             :    definition.  Such defining depsets depend on the binding depset.
   12937             :    Returns true if we contain something exported.  */
   12938             : 
   12939             : bool
   12940        7108 : depset::hash::add_namespace_entities (tree ns, bitmap partitions)
   12941             : {
   12942        7906 :   dump () && dump ("Looking for writables in %N", ns);
   12943        7108 :   dump.indent ();
   12944             : 
   12945        7108 :   unsigned count = 0;
   12946        7108 :   add_binding_data data;
   12947        7108 :   data.ns = ns;
   12948        7108 :   data.partitions = partitions;
   12949        7108 :   data.hash = this;
   12950             : 
   12951        7108 :   hash_table<named_decl_hash>::iterator end
   12952        7108 :     (DECL_NAMESPACE_BINDINGS (ns)->end ());
   12953     4356467 :   for (hash_table<named_decl_hash>::iterator iter
   12954     4363575 :          (DECL_NAMESPACE_BINDINGS (ns)->begin ()); iter != end; ++iter)
   12955             :     {
   12956     4349359 :       data.binding = nullptr;
   12957     4349359 :       data.met_namespace = false;
   12958     4349359 :       if (walk_module_binding (*iter, partitions, add_binding_entity, &data))
   12959      168413 :         count++;
   12960             :     }
   12961             : 
   12962        7108 :   if (count)
   12963        3041 :     dump () && dump ("Found %u entries", count);
   12964        7108 :   dump.outdent ();
   12965             : 
   12966        7108 :   return count != 0;
   12967             : }
   12968             : 
   12969             : void
   12970          98 : depset::hash::add_partial_entities (vec<tree, va_gc> *partial_classes)
   12971             : {
   12972       28845 :   for (unsigned ix = 0; ix != partial_classes->length (); ix++)
   12973             :     {
   12974       28747 :       tree inner = (*partial_classes)[ix];
   12975             : 
   12976       28747 :       depset *dep = make_dependency (inner, depset::EK_DECL);
   12977             : 
   12978       28747 :       if (dep->get_entity_kind () == depset::EK_REDIRECT)
   12979             :         /* We should have recorded the template as a partial
   12980             :            specialization.  */
   12981       17830 :         gcc_checking_assert (dep->deps[0]->get_entity_kind ()
   12982             :                              == depset::EK_PARTIAL);
   12983             :       else
   12984             :         /* It was an explicit specialization, not a partial one.  */
   12985       10917 :         gcc_checking_assert (dep->get_entity_kind ()
   12986             :                              == depset::EK_SPECIALIZATION);
   12987             :     }
   12988          98 : }
   12989             : 
   12990             : /* Add the members of imported classes that we defined in this TU.
   12991             :    This will also include lazily created implicit member function
   12992             :    declarations.  (All others will be definitions.)  */
   12993             : 
   12994             : void
   12995          12 : depset::hash::add_class_entities (vec<tree, va_gc> *class_members)
   12996             : {
   12997          24 :   for (unsigned ix = 0; ix != class_members->length (); ix++)
   12998             :     {
   12999          12 :       tree defn = (*class_members)[ix];
   13000          12 :       depset *dep = make_dependency (defn, EK_INNER_DECL);
   13001             : 
   13002          12 :       if (dep->get_entity_kind () == EK_REDIRECT)
   13003           0 :         dep = dep->deps[0];
   13004             : 
   13005             :       /* Only non-instantiations need marking as members.  */
   13006          24 :       if (dep->get_entity_kind () == EK_DECL)
   13007          12 :         dep->set_flag_bit <DB_IS_MEMBER_BIT> ();
   13008             :     }
   13009          12 : }
   13010             : 
   13011             : /* We add the partial & explicit specializations, and the explicit
   13012             :    instantiations.  */
   13013             : 
   13014             : static void
   13015      630195 : specialization_add (bool decl_p, spec_entry *entry, void *data_)
   13016             : {
   13017      630195 :   vec<spec_entry *> *data = reinterpret_cast <vec<spec_entry *> *> (data_);
   13018             : 
   13019      630195 :   if (!decl_p)
   13020             :     {
   13021             :       /* We exclusively use decls to locate things.  Make sure there's
   13022             :          no mismatch between the two specialization tables we keep.
   13023             :          pt.cc optimizes instantiation lookup using a complicated
   13024             :          heuristic.  We don't attempt to replicate that algorithm, but
   13025             :          observe its behaviour and reproduce it upon read back.  */
   13026             : 
   13027      232243 :        gcc_checking_assert (DECL_ALIAS_TEMPLATE_P (entry->tmpl)
   13028             :                            || TREE_CODE (entry->spec) == ENUMERAL_TYPE
   13029             :                            || DECL_CLASS_TEMPLATE_P (entry->tmpl));
   13030             : 
   13031             :        /* Only alias templates can appear in both tables (and
   13032             :           if they're in the type table they must also be in the decl
   13033             :           table).  */
   13034      433525 :        gcc_checking_assert
   13035             :          (!match_mergeable_specialization (true, entry)
   13036             :           == !DECL_ALIAS_TEMPLATE_P (entry->tmpl));
   13037             :     }
   13038      397952 :   else if (VAR_OR_FUNCTION_DECL_P (entry->spec))
   13039      199463 :     gcc_checking_assert (!DECL_LOCAL_DECL_P (entry->spec));
   13040             : 
   13041      630195 :   data->safe_push (entry);
   13042      630195 : }
   13043             : 
   13044             : /* Arbitrary stable comparison.  */
   13045             : 
   13046             : static int
   13047    36564572 : specialization_cmp (const void *a_, const void *b_)
   13048             : {
   13049    36564572 :   const spec_entry *ea = *reinterpret_cast<const spec_entry *const *> (a_);
   13050    36564572 :   const spec_entry *eb = *reinterpret_cast<const spec_entry *const *> (b_);
   13051             : 
   13052    36564572 :   if (ea == eb)
   13053             :     return 0;
   13054             : 
   13055    36564572 :   tree a = ea->spec;
   13056    36564572 :   tree b = eb->spec;
   13057    36564572 :   if (TYPE_P (a))
   13058             :     {
   13059    13019685 :       a = TYPE_NAME (a);
   13060    13019685 :       b = TYPE_NAME (b);
   13061             :     }
   13062             : 
   13063    36564572 :   if (a == b)
   13064             :     /* This can happen with friend specializations.  Just order by
   13065             :        entry address.  See note in depset_cmp.  */
   13066         202 :     return ea < eb ? -1 : +1;
   13067             : 
   13068    36564420 :   return DECL_UID (a) < DECL_UID (b) ? -1 : +1;
   13069             : }
   13070             : 
   13071             : /* We add all kinds of specialializations.  Implicit specializations
   13072             :    should only streamed and walked if they are reachable from
   13073             :    elsewhere.  Hence the UNREACHED flag.  This is making the
   13074             :    assumption that it is cheaper to reinstantiate them on demand
   13075             :    elsewhere, rather than stream them in when we instantiate their
   13076             :    general template.  Also, if we do stream them, we can only do that
   13077             :    if they are not internal (which they can become if they themselves
   13078             :    touch an internal entity?).  */
   13079             : 
   13080             : void
   13081        3320 : depset::hash::add_specializations (bool decl_p)
   13082             : {
   13083        3320 :   vec<spec_entry *> data;
   13084        3320 :   data.create (100);
   13085        3320 :   walk_specializations (decl_p, specialization_add, &data);
   13086        3320 :   data.qsort (specialization_cmp);
   13087      633515 :   while (data.length ())
   13088             :     {
   13089      630195 :       spec_entry *entry = data.pop ();
   13090      630195 :       tree spec = entry->spec;
   13091      630195 :       int use_tpl = 0;
   13092      630195 :       bool is_alias = false;
   13093      630195 :       bool is_friend = false;
   13094             : 
   13095      630195 :       if (decl_p && DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (entry->tmpl))
   13096             :         /* A friend of a template.  This is keyed to the
   13097             :            instantiation.  */
   13098             :         is_friend = true;
   13099             : 
   13100      630195 :       if (!decl_p && DECL_ALIAS_TEMPLATE_P (entry->tmpl))
   13101             :         {
   13102       30961 :           spec = TYPE_NAME (spec);
   13103       30961 :           is_alias = true;
   13104             :         }
   13105             : 
   13106      630195 :       if (decl_p || is_alias)
   13107             :         {
   13108      428913 :           if (tree ti = DECL_TEMPLATE_INFO (spec))
   13109             :             {
   13110      428913 :               tree tmpl = TI_TEMPLATE (ti);
   13111             : 
   13112      428913 :               use_tpl = DECL_USE_TEMPLATE (spec);
   13113      428913 :               if (spec == DECL_TEMPLATE_RESULT (tmpl))
   13114             :                 {
   13115        3639 :                   spec = tmpl;
   13116        3639 :                   gcc_checking_assert (DECL_USE_TEMPLATE (spec) == use_tpl);
   13117             :                 }
   13118      425274 :               else if (is_friend)
   13119             :                 {
   13120        2038 :                   if (TI_TEMPLATE (ti) != entry->tmpl
   13121        2038 :                       || !template_args_equal (TI_ARGS (ti), entry->tmpl))
   13122        2038 :                     goto template_friend;
   13123             :                 }
   13124             :             }
   13125             :           else
   13126             :             {
   13127           0 :             template_friend:;
   13128        2038 :               gcc_checking_assert (is_friend);
   13129             :               /* This is a friend of a template class, but not the one
   13130             :                  that generated entry->spec itself (i.e. it's an
   13131             :                  equivalent clone).  We do not need to record
   13132             :                  this.  */
   13133        2038 :               continue;
   13134             :             }
   13135             :         }
   13136             :       else
   13137             :         {
   13138      201282 :           if (TREE_CODE (spec) == ENUMERAL_TYPE)
   13139             :             {
   13140        1280 :               tree ctx = DECL_CONTEXT (TYPE_NAME (spec));
   13141             : 
   13142        1280 :               if (TYPE_P (ctx))
   13143        1280 :                 use_tpl = CLASSTYPE_USE_TEMPLATE (ctx);
   13144             :               else
   13145           0 :                 use_tpl = DECL_USE_TEMPLATE (ctx);
   13146             :             }
   13147             :           else
   13148      200002 :             use_tpl = CLASSTYPE_USE_TEMPLATE (spec);
   13149             : 
   13150      201282 :           tree ti = TYPE_TEMPLATE_INFO (spec);
   13151      201282 :           tree tmpl = TI_TEMPLATE (ti);
   13152             : 
   13153      201282 :           spec = TYPE_NAME (spec);
   13154      201282 :           if (spec == DECL_TEMPLATE_RESULT (tmpl))
   13155             :             {
   13156         688 :               spec = tmpl;
   13157         688 :               use_tpl = DECL_USE_TEMPLATE (spec);
   13158             :             }
   13159             :         }
   13160             : 
   13161      628157 :       bool needs_reaching = false;
   13162      628157 :       if (use_tpl == 1)
   13163             :         /* Implicit instantiations only walked if we reach them.  */
   13164             :         needs_reaching = true;
   13165       78841 :       else if (!DECL_LANG_SPECIFIC (spec)
   13166      149534 :                || !DECL_MODULE_PURVIEW_P (STRIP_TEMPLATE (spec)))
   13167             :         /* Likewise, GMF explicit or partial specializations.  */
   13168             :         needs_reaching = true;
   13169             : 
   13170             : #if false && CHECKING_P
   13171             :       /* The instantiation isn't always on
   13172             :          DECL_TEMPLATE_INSTANTIATIONS, */
   13173             :       // FIXME: we probably need to remember this information?
   13174             :       /* Verify the specialization is on the
   13175             :          DECL_TEMPLATE_INSTANTIATIONS of the template.  */
   13176             :       for (tree cons = DECL_TEMPLATE_INSTANTIATIONS (entry->tmpl);
   13177             :            cons; cons = TREE_CHAIN (cons))
   13178             :         if (TREE_VALUE (cons) == entry->spec)
   13179             :           {
   13180             :             gcc_assert (entry->args == TREE_PURPOSE (cons));
   13181             :             goto have_spec;
   13182             :           }
   13183             :       gcc_unreachable ();
   13184             :     have_spec:;
   13185             : #endif
   13186             : 
   13187             :       /* Make sure nobody left a tree visited lying about.  */
   13188      628157 :       gcc_checking_assert (!TREE_VISITED (spec));
   13189      628157 :       depset *dep = make_dependency (spec, depset::EK_SPECIALIZATION);
   13190      628157 :       if (dep->is_special ())
   13191             :         {
   13192             :           /* An already located specialization, this must be the TYPE
   13193             :              corresponding to an alias_decl we found in the decl
   13194             :              table.  */
   13195       30961 :           spec_entry *other = reinterpret_cast <spec_entry *> (dep->deps[0]);
   13196       61922 :           gcc_checking_assert (!decl_p && is_alias && !dep->is_type_spec ());
   13197       30961 :           gcc_checking_assert (other->tmpl == entry->tmpl
   13198             :                                && template_args_equal (other->args, entry->args)
   13199             :                                && TREE_TYPE (other->spec) == entry->spec);
   13200       30961 :           dep->set_flag_bit<DB_ALIAS_SPEC_BIT> ();
   13201             :         }
   13202             :       else
   13203             :         {
   13204      597196 :           gcc_checking_assert (decl_p || !is_alias);
   13205      597196 :           if (dep->get_entity_kind () == depset::EK_REDIRECT)
   13206       23985 :             dep = dep->deps[0];
   13207      573211 :           else if (dep->get_entity_kind () == depset::EK_SPECIALIZATION)
   13208             :             {
   13209      573211 :               dep->set_special ();
   13210      573211 :               dep->deps.safe_push (reinterpret_cast<depset *> (entry));
   13211      573211 :               if (!decl_p)
   13212      179730 :                 dep->set_flag_bit<DB_TYPE_SPEC_BIT> ();
   13213             :             }
   13214             : 
   13215      597196 :           if (needs_reaching)
   13216      531805 :             dep->set_flag_bit<DB_UNREACHED_BIT> ();
   13217      597196 :           if (is_friend)
   13218           0 :             dep->set_flag_bit<DB_FRIEND_SPEC_BIT> ();
   13219             :         }
   13220             :     }
   13221        3320 :   data.release ();
   13222        3320 : }
   13223             : 
   13224             : /* Add a depset into the mergeable hash.  */
   13225             : 
   13226             : void
   13227     1017849 : depset::hash::add_mergeable (depset *mergeable)
   13228             : {
   13229     1017849 :   gcc_checking_assert (is_key_order ());
   13230     1017849 :   entity_kind ek = mergeable->get_entity_kind ();
   13231     1017849 :   tree decl = mergeable->get_entity ();
   13232     1017849 :   gcc_checking_assert (ek < EK_DIRECT_HWM);
   13233             : 
   13234     1017849 :   depset **slot = entity_slot (decl, true);
   13235     1017849 :   gcc_checking_assert (!*slot);
   13236     1017849 :   depset *dep = make_entity (decl, ek);
   13237     1017849 :   *slot = dep;
   13238             : 
   13239     1017849 :   worklist.safe_push (dep);
   13240             : 
   13241             :   /* So we can locate the mergeable depset this depset refers to,
   13242             :      mark the first dep.  */
   13243     1017849 :   dep->set_special ();
   13244     1017849 :   dep->deps.safe_push (mergeable);
   13245     1017849 : }
   13246             : 
   13247             : /* Find the innermost-namespace scope of DECL, and that
   13248             :    namespace-scope decl.  */
   13249             : 
   13250             : tree
   13251    28085822 : find_pending_key (tree decl, tree *decl_p = nullptr)
   13252             : {
   13253    28085822 :   tree ns = decl;
   13254    33253639 :   do
   13255             :     {
   13256    33253639 :       decl = ns;
   13257    33253639 :       ns = CP_DECL_CONTEXT (ns);
   13258    33253639 :       if (TYPE_P (ns))
   13259     3141551 :         ns = TYPE_NAME (ns);
   13260             :     }
   13261    33253639 :   while (TREE_CODE (ns) != NAMESPACE_DECL);
   13262             : 
   13263    28085822 :   if (decl_p)
   13264    27645651 :     *decl_p = decl;
   13265             : 
   13266    28085822 :   return ns;
   13267             : }
   13268             : 
   13269             : /* Iteratively find dependencies.  During the walk we may find more
   13270             :    entries on the same binding that need walking.  */
   13271             : 
   13272             : void
   13273      303728 : depset::hash::find_dependencies (module_state *module)
   13274             : {
   13275      303728 :   trees_out walker (NULL, module, *this);
   13276      303728 :   vec<depset *> unreached;
   13277      607456 :   unreached.create (worklist.length ());
   13278             : 
   13279      304452 :   for (;;)
   13280             :     {
   13281      304452 :       reached_unreached = false;
   13282     3622215 :       while (worklist.length ())
   13283             :         {
   13284     3317763 :           depset *item = worklist.pop ();
   13285             : 
   13286     3317763 :           gcc_checking_assert (!item->is_binding ());
   13287     3317763 :           if (item->is_unreached ())
   13288     3317763 :             unreached.quick_push (item);
   13289             :           else
   13290             :             {
   13291     2061454 :               current = item;
   13292     2061454 :               tree decl = current->get_entity ();
   13293     2061454 :               dump (is_key_order () ? dumper::MERGE : dumper::DEPEND)
   13294     2084366 :                 && dump ("Dependencies of %s %C:%N",
   13295       22912 :                          is_key_order () ? "key-order"
   13296       22912 :                          : current->entity_kind_name (), TREE_CODE (decl), decl);
   13297     2061454 :               dump.indent ();
   13298     2061454 :               walker.begin ();
   13299     2061454 :               if (current->get_entity_kind () == EK_USING)
   13300       22229 :                 walker.tree_node (OVL_FUNCTION (decl));
   13301     2039225 :               else if (TREE_VISITED (decl))
   13302             :                 /* A global tree.  */;
   13303     2037754 :               else if (item->get_entity_kind () == EK_NAMESPACE)
   13304             :                 {
   13305        2038 :                   module->note_location (DECL_SOURCE_LOCATION (decl));
   13306        2038 :                   add_namespace_context (current, CP_DECL_CONTEXT (decl));
   13307             :                 }
   13308             :               else
   13309             :                 {
   13310     2035716 :                   walker.mark_declaration (decl, current->has_defn ());
   13311             : 
   13312     2035716 :                   if (!walker.is_key_order ()
   13313     2035716 :                       && (item->get_entity_kind () == EK_SPECIALIZATION
   13314      599379 :                           || item->get_entity_kind () == EK_PARTIAL
   13315      577708 :                           || (item->get_entity_kind () == EK_DECL
   13316      577708 :                               && item->is_member ())))
   13317             :                     {
   13318      440171 :                       tree ns = find_pending_key (decl, nullptr);
   13319      440171 :                       add_namespace_context (item, ns);
   13320             :                     }
   13321             : 
   13322             :                   // FIXME: Perhaps p1815 makes this redundant? Or at
   13323             :                   // least simplifies it.  Voldemort types are only
   13324             :                   // ever emissable when containing (inline) function
   13325             :                   // definition is emitted?
   13326             :                   /* Turn the Sneakoscope on when depending the decl.  */
   13327     2035716 :                   sneakoscope = true;
   13328     2035716 :                   walker.decl_value (decl, current);
   13329     2035716 :                   sneakoscope = false;
   13330     2035716 :                   if (current->has_defn ())
   13331      362985 :                     walker.write_definition (decl);
   13332             :                 }
   13333     2061454 :               walker.end ();
   13334             : 
   13335     2061454 :               if (!walker.is_key_order ()
   13336     1043605 :                   && TREE_CODE (decl) == TEMPLATE_DECL
   13337     2428644 :                   && !DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (decl))
   13338             :                 /* Mark all the explicit & partial specializations as
   13339             :                    reachable.  */
   13340      362655 :                 for (tree cons = DECL_TEMPLATE_INSTANTIATIONS (decl);
   13341      597091 :                      cons; cons = TREE_CHAIN (cons))
   13342             :                   {
   13343      234436 :                     tree spec = TREE_VALUE (cons);
   13344      234436 :                     if (TYPE_P (spec))
   13345      215623 :                       spec = TYPE_NAME (spec);
   13346      234436 :                     int use_tpl;
   13347      234436 :                     node_template_info (spec, use_tpl);
   13348      234436 :                     if (use_tpl & 2)
   13349             :                       {
   13350       37514 :                         depset *spec_dep = find_dependency (spec);
   13351       37514 :                         if (spec_dep->get_entity_kind () == EK_REDIRECT)
   13352       21195 :                           spec_dep = spec_dep->deps[0];
   13353       37514 :                         if (spec_dep->is_unreached ())
   13354             :                           {
   13355        3234 :                             reached_unreached = true;
   13356        3234 :                             spec_dep->clear_flag_bit<DB_UNREACHED_BIT> ();
   13357      235304 :                             dump (dumper::DEPEND)
   13358         868 :                               && dump ("Reaching unreached specialization"
   13359         868 :                                        " %C:%N", TREE_CODE (spec), spec);
   13360             :                           }
   13361             :                       }
   13362             :                   }
   13363             : 
   13364     2061454 :               dump.outdent ();
   13365     2061454 :               current = NULL;
   13366             :             }
   13367             :         }
   13368             : 
   13369      304452 :       if (!reached_unreached)
   13370             :         break;
   13371             : 
   13372             :       /* It's possible the we reached the unreached before we
   13373             :          processed it in the above loop, so we'll be doing this an
   13374             :          extra time.  However, to avoid that we have to do some
   13375             :          bit shuffling that also involves a scan of the list.
   13376             :          Swings & roundabouts I guess.  */
   13377      304452 :       std::swap (worklist, unreached);
   13378             :     }
   13379             : 
   13380      303728 :   unreached.release ();
   13381      303728 : }
   13382             : 
   13383             : /* Compare two entries of a single binding.  TYPE_DECL before
   13384             :    non-exported before exported.  */
   13385             : 
   13386             : static int
   13387      524290 : binding_cmp (const void *a_, const void *b_)
   13388             : {
   13389      524290 :   depset *a = *(depset *const *)a_;
   13390      524290 :   depset *b = *(depset *const *)b_;
   13391             : 
   13392      524290 :   tree a_ent = a->get_entity ();
   13393      524290 :   tree b_ent = b->get_entity ();
   13394      524290 :   gcc_checking_assert (a_ent != b_ent
   13395             :                        && !a->is_binding ()
   13396             :                        && !b->is_binding ());
   13397             : 
   13398             :   /* Implicit typedefs come first.  */
   13399      524290 :   bool a_implicit = DECL_IMPLICIT_TYPEDEF_P (a_ent);
   13400      524290 :   bool b_implicit = DECL_IMPLICIT_TYPEDEF_P (b_ent);
   13401      524290 :   if (a_implicit || b_implicit)
   13402             :     {
   13403             :       /* A binding with two implicit type decls?  That's unpossible!  */
   13404         284 :       gcc_checking_assert (!(a_implicit && b_implicit));
   13405         426 :       return a_implicit ? -1 : +1;  /* Implicit first.  */
   13406             :     }
   13407             : 
   13408             :   /* Hidden before non-hidden.  */
   13409      524006 :   bool a_hidden = a->is_hidden ();
   13410      524006 :   bool b_hidden = b->is_hidden ();
   13411      524006 :   if (a_hidden != b_hidden)
   13412       58204 :     return a_hidden ? -1 : +1;
   13413             : 
   13414      485904 :   bool a_using = a->get_entity_kind () == depset::EK_USING;
   13415      485904 :   bool a_export;
   13416      485904 :   if (a_using)
   13417             :     {
   13418       25304 :       a_export = OVL_EXPORT_P (a_ent);
   13419       25304 :       a_ent = OVL_FUNCTION (a_ent);
   13420             :     }
   13421             :   else
   13422      460600 :     a_export = DECL_MODULE_EXPORT_P (TREE_CODE (a_ent) == CONST_DECL
   13423             :                                      ? TYPE_NAME (TREE_TYPE (a_ent))
   13424             :                                      : STRIP_TEMPLATE (a_ent));
   13425             :   
   13426      485904 :   bool b_using = b->get_entity_kind () == depset::EK_USING;
   13427      485904 :   bool b_export;
   13428      485904 :   if (b_using)
   13429             :     {
   13430       20122 :       b_export = OVL_EXPORT_P (b_ent);
   13431       20122 :       b_ent = OVL_FUNCTION (b_ent);
   13432             :     }
   13433             :   else
   13434      465782 :     b_export = DECL_MODULE_EXPORT_P (TREE_CODE (b_ent) == CONST_DECL
   13435             :                                      ? TYPE_NAME (TREE_TYPE (b_ent))
   13436             :                                      : STRIP_TEMPLATE (b_ent));
   13437             : 
   13438             :   /* Non-exports before exports.  */
   13439      485904 :   if (a_export != b_export)
   13440          72 :     return a_export ? +1 : -1;
   13441             : 
   13442             :   /* At this point we don't care, but want a stable sort.  */
   13443             : 
   13444      485859 :   if (a_using != b_using)
   13445             :     /* using first.  */
   13446       26704 :     return a_using? -1 : +1;
   13447             : 
   13448      466329 :   return DECL_UID (a_ent) < DECL_UID (b_ent) ? -1 : +1;
   13449             : }
   13450             : 
   13451             : /* Sort the bindings, issue errors about bad internal refs.  */
   13452             : 
   13453             : bool
   13454        1660 : depset::hash::finalize_dependencies ()
   13455             : {
   13456        1660 :   bool ok = true;
   13457        1660 :   depset::hash::iterator end (this->end ());
   13458     2788034 :   for (depset::hash::iterator iter (begin ()); iter != end; ++iter)
   13459             :     {
   13460     1393187 :       depset *dep = *iter;
   13461     1393187 :       if (dep->is_binding ())
   13462             :         {
   13463             :           /* Keep the containing namespace dep first.  */
   13464      167250 :           gcc_checking_assert (dep->deps.length () > 1
   13465             :                                && (dep->deps[0]->get_entity_kind ()
   13466             :                                    == EK_NAMESPACE)
   13467             :                                && (dep->deps[0]->get_entity ()
   13468             :                                    == dep->get_entity ()));
   13469      167250 :           if (dep->deps.length () > 2)
   13470       12554 :             gcc_qsort (&dep->deps[1], dep->deps.length () - 1,
   13471             :                        sizeof (dep->deps[1]), binding_cmp);
   13472             :         }
   13473     1225937 :       else if (dep->refs_internal ())
   13474             :         {
   13475          24 :           for (unsigned ix = dep->deps.length (); ix--;)
   13476             :             {
   13477          12 :               depset *rdep = dep->deps[ix];
   13478          12 :               if (rdep->is_internal ())
   13479             :                 {
   13480             :                   // FIXME:QOI Better location information?  We're
   13481             :                   // losing, so it doesn't matter about efficiency
   13482          12 :                   tree decl = dep->get_entity ();
   13483          12 :                   error_at (DECL_SOURCE_LOCATION (decl),
   13484             :                             "%q#D references internal linkage entity %q#D",
   13485             :                             decl, rdep->get_entity ());
   13486          12 :                   break;
   13487             :                 }
   13488             :             }
   13489             :           ok = false;
   13490             :         }
   13491             :     }
   13492             : 
   13493        1660 :   return ok;
   13494             : }
   13495             : 
   13496             : /* Core of TARJAN's algorithm to find Strongly Connected Components
   13497             :    within a graph.  See https://en.wikipedia.org/wiki/
   13498             :    Tarjan%27s_strongly_connected_components_algorithm for details.
   13499             : 
   13500             :    We use depset::section as lowlink.  Completed nodes have
   13501             :    depset::cluster containing the cluster number, with the top
   13502             :    bit set.
   13503             : 
   13504             :    A useful property is that the output vector is a reverse
   13505             :    topological sort of the resulting DAG.  In our case that means
   13506             :    dependent SCCs are found before their dependers.  We make use of
   13507             :    that property.  */
   13508             : 
   13509             : void
   13510     2228674 : depset::tarjan::connect (depset *v)
   13511             : {
   13512     2228674 :   gcc_checking_assert (v->is_binding ()
   13513             :                        || !(v->is_unreached () || v->is_import ()));
   13514             : 
   13515     2228674 :   v->cluster = v->section = ++index;
   13516     2228674 :   stack.safe_push (v);
   13517             : 
   13518             :   /* Walk all our dependencies, ignore a first marked slot  */
   13519    18157860 :   for (unsigned ix = v->is_special (); ix != v->deps.length (); ix++)
   13520             :     {
   13521     6854329 :       depset *dep = v->deps[ix];
   13522             : 
   13523    13501739 :       if (dep->is_binding () || !dep->is_import ())
   13524             :         {
   13525     6853595 :           unsigned lwm = dep->cluster;
   13526             : 
   13527     6853595 :           if (!dep->cluster)
   13528             :             {
   13529             :               /* A new node.  Connect it.  */
   13530     1228476 :               connect (dep);
   13531     1228476 :               lwm = dep->section;
   13532             :             }
   13533             : 
   13534     6853595 :           if (dep->section && v->section > lwm)
   13535     1105705 :             v->section = lwm;
   13536             :         }
   13537             :     }
   13538             : 
   13539     2228674 :   if (v->section == v->cluster)
   13540             :     {
   13541             :       /* Root of a new SCC.  Push all the members onto the result list. */
   13542             :       unsigned num = v->cluster;
   13543     2228674 :       depset *p;
   13544     2228674 :       do
   13545             :         {
   13546     2228674 :           p = stack.pop ();
   13547     2228674 :           p->cluster = num;
   13548     2228674 :           p->section = 0;
   13549     2228674 :           result.quick_push (p);
   13550             :         }
   13551     2228674 :       while (p != v);
   13552             :     }
   13553     2228674 : }
   13554             : 
   13555             : /* Compare two depsets.  The specific ordering is unimportant, we're
   13556             :    just trying to get consistency.  */
   13557             : 
   13558             : static int
   13559   104161124 : depset_cmp (const void *a_, const void *b_)
   13560             : {
   13561   104161124 :   depset *a = *(depset *const *)a_;
   13562   104161124 :   depset *b = *(depset *const *)b_;
   13563             : 
   13564   104161124 :   depset::entity_kind a_kind = a->get_entity_kind ();
   13565   104161124 :   depset::entity_kind b_kind = b->get_entity_kind ();
   13566             : 
   13567   104161124 :   if  (a_kind != b_kind)
   13568             :     /* Different entity kinds, order by that.  */
   13569     5146816 :     return a_kind < b_kind ? -1 : +1;
   13570             :   
   13571   100483499 :   tree a_decl = a->get_entity ();
   13572   100483499 :   tree b_decl = b->get_entity ();
   13573   100483499 :   if (a_kind == depset::EK_USING)
   13574             :     {
   13575             :       /* If one is a using, the other must be too.  */
   13576     1246682 :       a_decl = OVL_FUNCTION (a_decl);
   13577     1246682 :       b_decl = OVL_FUNCTION (b_decl);
   13578             :     }
   13579             : 
   13580   100483499 :   if (a_decl != b_decl)
   13581             :     /* Different entities, order by their UID.  */
   13582    91951731 :     return DECL_UID (a_decl) < DECL_UID (b_decl) ? -1 : +1;
   13583             : 
   13584     8531768 :   if (a_kind == depset::EK_BINDING)
   13585             :     {
   13586             :       /* Both are bindings.  Order by identifier hash.  */
   13587     8526648 :       gcc_checking_assert (a->get_name () != b->get_name ());
   13588     8526648 :       return (IDENTIFIER_HASH_VALUE (a->get_name ())
   13589     8526648 :               < IDENTIFIER_HASH_VALUE (b->get_name ())
   13590     8526648 :               ? -1 : +1);
   13591             :     }
   13592             : 
   13593             :   /* They are the same decl.  This can happen with two using decls
   13594             :      pointing to the same target.  The best we can aim for is
   13595             :      consistently telling qsort how to order them.  Hopefully we'll
   13596             :      never have to debug a case that depends on this.  Oh, who am I
   13597             :      kidding?  Good luck.  */
   13598        5120 :   gcc_checking_assert (a_kind == depset::EK_USING);
   13599             : 
   13600             :   /* Order by depset address.  Not the best, but it is something.  */
   13601        5120 :   return a < b ? -1 : +1;
   13602             : }
   13603             : 
   13604             : /* Sort the clusters in SCC such that those that depend on one another
   13605             :    are placed later.   */
   13606             : 
   13607             : // FIXME: I am not convinced this is needed and, if needed,
   13608             : // sufficient.  We emit the decls in this order but that emission
   13609             : // could walk into later decls (from the body of the decl, or default
   13610             : // arg-like things).  Why doesn't that walk do the right thing?  And
   13611             : // if it DTRT why do we need to sort here -- won't things naturally
   13612             : // work?  I think part of the issue is that when we're going to refer
   13613             : // to an entity by name, and that entity is in the same cluster as us,
   13614             : // we need to actually walk that entity, if we've not already walked
   13615             : // it.
   13616             : static void
   13617      302068 : sort_cluster (depset::hash *original, depset *scc[], unsigned size)
   13618             : {
   13619      302068 :   depset::hash table (size, original);
   13620             : 
   13621      302068 :   dump.indent ();
   13622             : 
   13623             :   /* Place bindings last, usings before that.  It's not strictly
   13624             :      necessary, but it does make things neater.  Says Mr OCD.  */
   13625             :   unsigned bind_lwm = size;
   13626             :   unsigned use_lwm = size;
   13627     1509390 :   for (unsigned ix = 0; ix != use_lwm;)
   13628             :     {
   13629     1207322 :       depset *dep = scc[ix];
   13630     1207322 :       switch (dep->get_entity_kind ())
   13631             :         {
   13632      167244 :         case depset::EK_BINDING:
   13633             :           /* Move to end.  No increment.  Notice this could be moving
   13634             :              a using decl, which we'll then move again.  */
   13635      167244 :           if (--bind_lwm != ix)
   13636             :             {
   13637       85973 :               scc[ix] = scc[bind_lwm];
   13638       85973 :               scc[bind_lwm] = dep;
   13639             :             }
   13640      167244 :           if (use_lwm > bind_lwm)
   13641             :             {
   13642      137822 :               use_lwm--;
   13643      137822 :               break;
   13644             :             }
   13645             :           /* We must have copied a using, so move it too.  */
   13646       29422 :           dep = scc[ix];
   13647       29422 :           gcc_checking_assert (dep->get_entity_kind () == depset::EK_USING);
   13648             :           /* FALLTHROUGH  */
   13649             : 
   13650       51651 :         case depset::EK_USING:
   13651       51651 :           if (--use_lwm != ix)
   13652             :             {
   13653       38134 :               scc[ix] = scc[use_lwm];
   13654       38134 :               scc[use_lwm] = dep;
   13655             :             }
   13656             :           break;
   13657             : 
   13658     1017849 :         case depset::EK_DECL:
   13659     1017849 :         case depset::EK_SPECIALIZATION:
   13660     1017849 :         case depset::EK_PARTIAL:
   13661     1017849 :           table.add_mergeable (dep);
   13662     1017849 :           ix++;
   13663     1017849 :           break;
   13664             : 
   13665           0 :         default:
   13666           0 :           gcc_unreachable ();
   13667             :         }
   13668             :     }
   13669             : 
   13670      302068 :   gcc_checking_assert (use_lwm <= bind_lwm);
   13671      302296 :   dump (dumper::MERGE) && dump ("Ordering %u/%u depsets", use_lwm, size);
   13672             : 
   13673      302068 :   table.find_dependencies (nullptr);
   13674             : 
   13675      302068 :   vec<depset *> order = table.connect ();
   13676      604136 :   gcc_checking_assert (order.length () == use_lwm);
   13677             : 
   13678             :   /* Now rewrite entries [0,lwm), in the dependency order we
   13679             :      discovered.  Usually each entity is in its own cluster.  Rarely,
   13680             :      we can get multi-entity clusters, in which case all but one must
   13681             :      only be reached from within the cluster.  This happens for
   13682             :      something like:
   13683             : 
   13684             :      template<typename T>
   13685             :      auto Foo (const T &arg) -> TPL<decltype (arg)>;
   13686             : 
   13687             :      The instantiation of TPL will be in the specialization table, and
   13688             :      refer to Foo via arg.  But we can only get to that specialization
   13689             :      from Foo's declaration, so we only need to treat Foo as mergable
   13690             :      (We'll do structural comparison of TPL<decltype (arg)>).
   13691             : 
   13692             :      Finding the single cluster entry dep is very tricky and
   13693             :      expensive.  Let's just not do that.  It's harmless in this case
   13694             :      anyway. */
   13695             :   unsigned pos = 0;
   13696             :   unsigned cluster = ~0u;
   13697     2639834 :   for (unsigned ix = 0; ix != order.length (); ix++)
   13698             :     {
   13699     1017849 :       gcc_checking_assert (order[ix]->is_special ());
   13700     1017849 :       depset *dep = order[ix]->deps[0];
   13701     1017849 :       scc[pos++] = dep;
   13702     1018521 :       dump (dumper::MERGE)
   13703         672 :         && dump ("Mergeable %u is %N%s", ix, dep->get_entity (),
   13704         672 :                  order[ix]->cluster == cluster ? " (tight)" : "");
   13705     1017849 :       cluster = order[ix]->cluster;
   13706             :     }
   13707             : 
   13708      302068 :   gcc_checking_assert (pos == use_lwm);
   13709             : 
   13710      302068 :   order.release ();
   13711      302296 :   dump (dumper::MERGE) && dump ("Ordered %u keys", pos);
   13712      302068 :   dump.outdent ();
   13713      302068 : }
   13714             : 
   13715             : /* Reduce graph to SCCS clusters.  SCCS will be populated with the
   13716             :    depsets in dependency order.  Each depset's CLUSTER field contains
   13717             :    its cluster number.  Each SCC has a unique cluster number, and are
   13718             :    contiguous in SCCS. Cluster numbers are otherwise arbitrary.  */
   13719             : 
   13720             : vec<depset *>
   13721      303725 : depset::hash::connect ()
   13722             : {
   13723      303725 :   tarjan connector (size ());
   13724      303725 :   vec<depset *> deps;
   13725      303725 :   deps.create (size ());
   13726      303725 :   iterator end (this->end ());
   13727     2714731 :   for (iterator iter (begin ()); iter != end; ++iter)
   13728             :     {
   13729     2411006 :       depset *item = *iter;
   13730             : 
   13731     2411006 :       entity_kind kind = item->get_entity_kind ();
   13732     2243762 :       if (kind == EK_BINDING
   13733     2243762 :           || !(kind == EK_REDIRECT
   13734     2219304 :                || item->is_unreached ()
   13735     2061822 :                || item->is_import ()))
   13736     2411006 :         deps.quick_push (item);
   13737             :     }
   13738             : 
   13739             :   /* Iteration over the hash table is an unspecified ordering.  While
   13740             :      that has advantages, it causes 2 problems.  Firstly repeatable
   13741             :      builds are tricky.  Secondly creating testcases that check
   13742             :      dependencies are correct by making sure a bad ordering would
   13743             :      happen if that was wrong.  */
   13744      303725 :   deps.qsort (depset_cmp);
   13745             : 
   13746     2532399 :   while (deps.length ())
   13747             :     {
   13748     2228674 :       depset *v = deps.pop ();
   13749     2228674 :       dump (dumper::CLUSTER) &&
   13750        1590 :         (v->is_binding ()
   13751         195 :          ? dump ("Connecting binding %P", v->get_entity (), v->get_name ())
   13752        1395 :          : dump ("Connecting %s %s %C:%N",
   13753        1395 :                  is_key_order () ? "key-order"
   13754         765 :                  : !v->has_defn () ? "declaration" : "definition",
   13755        1395 :                  v->entity_kind_name (), TREE_CODE (v->get_entity ()),
   13756             :                  v->get_entity ()));
   13757     2228674 :       if (!v->cluster)
   13758     1000198 :         connector.connect (v);
   13759             :     }
   13760             : 
   13761      303725 :   deps.release ();
   13762      607450 :   return connector.result;
   13763      303725 : }
   13764             : 
   13765             : /* Initialize location spans.  */
   13766             : 
   13767             : void
   13768        2882 : loc_spans::init (const line_maps *lmaps, const line_map_ordinary *map)
   13769             : {
   13770        2882 :   gcc_checking_assert (!init_p ());
   13771        2882 :   spans = new vec<span> ();
   13772        2882 :   spans->reserve (20);
   13773             : 
   13774        2882 :   span interval;
   13775        2882 :   interval.ordinary.first = 0;
   13776        2882 :   interval.macro.second = MAX_LOCATION_T + 1;
   13777        2882 :   interval.ordinary_delta = interval.macro_delta = 0;
   13778             : 
   13779             :   /* A span for reserved fixed locs.  */
   13780        2882 :   interval.ordinary.second
   13781        2882 :     = MAP_START_LOCATION (LINEMAPS_ORDINARY_MAP_AT (line_table, 0));
   13782        2882 :   interval.macro.first = interval.macro.second;
   13783        2882 :   dump (dumper::LOCATION)
   13784          42 :     && dump ("Fixed span %u ordinary:[%u,%u) macro:[%u,%u)", spans->length (),
   13785             :              interval.ordinary.first, interval.ordinary.second,
   13786             :              interval.macro.first, interval.macro.second);
   13787        2882 :   spans->quick_push (interval);
   13788             : 
   13789             :   /* A span for command line & forced headers.  */
   13790        2882 :   interval.ordinary.first = interval.ordinary.second;
   13791        2882 :   interval.macro.second = interval.macro.first;
   13792        2882 :   if (map)
   13793             :     {
   13794        2879 :       interval.ordinary.second = map->start_location;
   13795        5728 :       interval.macro.first = LINEMAPS_MACRO_LOWEST_LOCATION (lmaps);
   13796             :     }
   13797        2882 :   dump (dumper::LOCATION)
   13798          21 :     && dump ("Pre span %u ordinary:[%u,%u) macro:[%u,%u)", spans->length (),
   13799             :              interval.ordinary.first, interval.ordinary.second,
   13800             :              interval.macro.first, interval.macro.second);
   13801        2882 :   spans->quick_push (interval);
   13802             :   
   13803             :   /* Start an interval for the main file.  */
   13804        2882 :   interval.ordinary.first = interval.ordinary.second;
   13805        2882 :   interval.macro.second = interval.macro.first;
   13806        2882 :   dump (dumper::LOCATION)
   13807          21 :     && dump ("Main span %u ordinary:[%u,*) macro:[*,%u)", spans->length (),
   13808             :              interval.ordinary.first, interval.macro.second);
   13809        2882 :   spans->quick_push (interval);
   13810        2882 : }
   13811             : 
   13812             : /* Reopen the span, if we want the about-to-be-inserted set of maps to
   13813             :    be propagated in our own location table.  I.e. we are the primary
   13814             :    interface and we're importing a partition.  */
   13815             : 
   13816             : bool
   13817        1918 : loc_spans::maybe_propagate (module_state *import, location_t hwm)
   13818             : {
   13819        1918 :   bool opened = (module_interface_p () && !module_partition_p ()
   13820        2169 :                  && import->is_partition ());
   13821          81 :   if (opened)
   13822          81 :     open (hwm);
   13823        1918 :   return opened;
   13824             : }
   13825             : 
   13826             : /* Open a new linemap interval.  The just-created ordinary map is the
   13827             :    first map of the interval.  */
   13828             : 
   13829             : void
   13830         735 : loc_spans::open (location_t hwm)
   13831             : {
   13832         735 :   span interval;
   13833         735 :   interval.ordinary.first = interval.ordinary.second = hwm;
   13834        1470 :   interval.macro.first = interval.macro.second
   13835         735 :     = LINEMAPS_MACRO_LOWEST_LOCATION (line_table);
   13836         735 :   interval.ordinary_delta = interval.macro_delta = 0;
   13837         735 :   dump (dumper::LOCATION)
   13838           0 :     && dump ("Opening span %u ordinary:[%u,... macro:...,%u)",
   13839           0 :              spans->length (), interval.ordinary.first,
   13840             :              interval.macro.second);
   13841         735 :   if (spans->length ())
   13842             :     {
   13843             :       /* No overlapping!  */
   13844         735 :       auto &last = spans->last ();
   13845         735 :       gcc_checking_assert (interval.ordinary.first >= last.ordinary.second);
   13846         735 :       gcc_checking_assert (interval.macro.second <= last.macro.first);
   13847             :     }
   13848         735 :   spans->safe_push (interval);
   13849         735 : }
   13850             : 
   13851             : /* Close out the current linemap interval.  The last maps are within
   13852             :    the interval.  */
   13853             : 
   13854             : void
   13855        3617 : loc_spans::close ()
   13856             : {
   13857        3617 :   span &interval = spans->last ();
   13858             : 
   13859        3617 :   interval.ordinary.second
   13860        3617 :     = ((line_table->highest_location + (1 << line_table->default_range_bits))
   13861        3617 :        & ~((1u << line_table->default_range_bits) - 1));
   13862        3617 :   interval.macro.first = LINEMAPS_MACRO_LOWEST_LOCATION (line_table);
   13863        3617 :   dump (dumper::LOCATION)
   13864          21 :     && dump ("Closing span %u ordinary:[%u,%u) macro:[%u,%u)",
   13865          21 :              spans->length () - 1,
   13866             :              interval.ordinary.first,interval.ordinary.second,
   13867             :              interval.macro.first, interval.macro.second);
   13868        3617 : }
   13869             : 
   13870             : /* Given an ordinary location LOC, return the lmap_interval it resides
   13871             :    in.  NULL if it is not in an interval.  */
   13872             : 
   13873             : const loc_spans::span *
   13874    29562978 : loc_spans::ordinary (location_t loc)
   13875             : {
   13876    29562978 :   unsigned len = spans->length ();
   13877    58762193 :   unsigned pos = 0;
   13878    58770007 :   while (len)
   13879             :     {
   13880    58761607 :       unsigned half = len / 2;
   13881    58761607 :       const span &probe = (*spans)[pos + half];
   13882    58761607 :       if (loc < probe.ordinary.first)
   13883             :         len = half;
   13884    58753793 :       else if (loc < probe.ordinary.second)
   13885    29554578 :         return &probe;
   13886             :       else
   13887             :         {
   13888    29199215 :           pos += half + 1;
   13889    29199215 :           len = len - (half + 1);
   13890             :         }
   13891             :     }
   13892             :   return NULL;
   13893             : }
   13894             : 
   13895             : /* Likewise, given a macro location LOC, return the lmap interval it
   13896             :    resides in.   */
   13897             : 
   13898             : const loc_spans::span *
   13899     3094272 : loc_spans::macro (location_t loc)
   13900             : {
   13901     3094272 :   unsigned len = spans->length ();
   13902     6174331 :   unsigned pos = 0;
   13903     6174331 :   while (len)
   13904             :     {
   13905     6174319 :       unsigned half = len / 2;
   13906     6174319 :       const span &probe = (*spans)[pos + half];
   13907     6174319 :       if (loc >= probe.macro.second)
   13908             :         len = half;
   13909     6174319 :       else if (loc >= probe.macro.first)
   13910     3094260 :         return &probe;
   13911             :       else
   13912             :         {
   13913     3080059 :           pos += half + 1;
   13914     3080059 :           len = len - (half + 1);
   13915             :         }
   13916             :     }
   13917             :   return NULL;
   13918             : }
   13919             : 
   13920             : /* Return the ordinary location closest to FROM.  */
   13921             : 
   13922             : static location_t
   13923        4220 : ordinary_loc_of (line_maps *lmaps, location_t from)
   13924             : {
   13925        8443 :   while (!IS_ORDINARY_LOC (from))
   13926             :     {
   13927           3 :       if (IS_ADHOC_LOC (from))
   13928           3 :         from = get_location_from_adhoc_loc (lmaps, from);
   13929           6 :       if (from >= LINEMAPS_MACRO_LOWEST_LOCATION (lmaps))
   13930             :         {
   13931             :           /* Find the ordinary location nearest FROM.  */
   13932           0 :           const line_map *map = linemap_lookup (lmaps, from);
   13933           0 :           const line_map_macro *mac_map = linemap_check_macro (map);
   13934           0 :           from = MACRO_MAP_EXPANSION_POINT_LOCATION (mac_map);
   13935             :         }
   13936             :     }
   13937        4220 :   return from;
   13938             : }
   13939             : 
   13940             : static module_state **
   13941        7217 : get_module_slot (tree name, module_state *parent, bool partition, bool insert)
   13942             : {
   13943        7217 :   module_state_hash::compare_type ct (name, uintptr_t (parent) | partition);
   13944        7217 :   hashval_t hv = module_state_hash::hash (ct);
   13945             : 
   13946        7217 :   return modules_hash->find_slot_with_hash (ct, hv, insert ? INSERT : NO_INSERT);
   13947             : }
   13948             : 
   13949             : static module_state *
   13950         291 : get_primary (module_state *parent)
   13951             : {
   13952        2542 :   while (parent->is_partition ())
   13953         180 :     parent = parent->parent;
   13954             : 
   13955        2362 :   if (!parent->name)
   13956             :     // Implementation unit has null name
   13957         111 :     parent = parent->parent;
   13958             : 
   13959         306 :   return parent;
   13960             : }
   13961             : 
   13962             : /* Find or create module NAME & PARENT in the hash table.  */
   13963             : 
   13964             : module_state *
   13965        7217 : get_module (tree name, module_state *parent, bool partition)
   13966             : {
   13967        7217 :   if (partition)
   13968             :     {
   13969         468 :       if (!parent)
   13970         111 :         parent = get_primary ((*modules)[0]);
   13971             : 
   13972         468 :       if (!parent->is_partition () && !parent->flatname)
   13973          93 :         parent->set_flatname ();
   13974             :     }
   13975             : 
   13976        7217 :   module_state **slot = get_module_slot (name, parent, partition, true);
   13977        7217 :   module_state *state = *slot;
   13978        7217 :   if (!state)
   13979             :     {
   13980        3919 :       state = (new (ggc_alloc<module_state> ())
   13981        3919 :                module_state (name, parent, partition));
   13982        3919 :       *slot = state;
   13983             :     }
   13984        7217 :   return state;
   13985             : }
   13986             : 
   13987             : /* Process string name PTR into a module_state.  */
   13988             : 
   13989             : static module_state *
   13990         281 : get_module (const char *ptr)
   13991             : {
   13992             :   /* On DOS based file systems, there is an ambiguity with A:B which can be
   13993             :      interpreted as a module Module:Partition or Drive:PATH.  Interpret strings
   13994             :      which clearly starts as pathnames as header-names and everything else is
   13995             :      treated as a (possibly malformed) named moduled.  */
   13996         281 :   if (IS_DIR_SEPARATOR (ptr[ptr[0] == '.']) // ./FOO or /FOO
   13997             : #if HAVE_DOS_BASED_FILE_SYSTEM
   13998             :       || (HAS_DRIVE_SPEC (ptr) && IS_DIR_SEPARATOR (ptr[2])) // A:/FOO
   13999             : #endif
   14000             :       || false)
   14001             :     /* A header name.  */
   14002          88 :     return get_module (build_string (strlen (ptr), ptr));
   14003             : 
   14004             :   bool partition = false;
   14005             :   module_state *mod = NULL;
   14006             : 
   14007         903 :   for (const char *probe = ptr;; probe++)
   14008        1096 :     if (!*probe || *probe == '.' || *probe == ':')
   14009             :       {
   14010         247 :         if (probe == ptr)
   14011             :           return NULL;
   14012             : 
   14013         247 :         mod = get_module (get_identifier_with_length (ptr, probe - ptr),
   14014             :                           mod, partition);
   14015         247 :         ptr = probe;
   14016         247 :         if (*ptr == ':')
   14017             :           {
   14018          54 :             if (partition)
   14019             :               return NULL;
   14020             :             partition = true;
   14021             :           }
   14022             : 
   14023         247 :         if (!*ptr++)
   14024             :           break;
   14025             :       }
   14026         849 :     else if (!(ISALPHA (*probe) || *probe == '_'
   14027          12 :                || (probe != ptr && ISDIGIT (*probe))))
   14028             :       return NULL;
   14029             : 
   14030             :   return mod;
   14031             : }
   14032             : 
   14033             : /* Create a new mapper connecting to OPTION.  */
   14034             : 
   14035             : module_client *
   14036        2882 : make_mapper (location_t loc)
   14037             : {
   14038        2882 :   timevar_start (TV_MODULE_MAPPER);
   14039        2882 :   const char *option = module_mapper_name;
   14040        2882 :   if (!option)
   14041        2846 :     option = getenv ("CXX_MODULE_MAPPER");
   14042             : 
   14043        5764 :   mapper = module_client::open_module_client
   14044        2882 :     (loc, option, &set_cmi_repo,
   14045        2882 :      (save_decoded_options[0].opt_index == OPT_SPECIAL_program_name)
   14046        2882 :      && save_decoded_options[0].arg != progname
   14047             :      ? save_decoded_options[0].arg : nullptr);
   14048             : 
   14049        2882 :   timevar_stop (TV_MODULE_MAPPER);
   14050             : 
   14051        2882 :   return mapper;
   14052             : }
   14053             : 
   14054             : static unsigned lazy_snum;
   14055             : 
   14056             : static bool
   14057        4746 : recursive_lazy (unsigned snum = ~0u)
   14058             : {
   14059        4746 :   if (lazy_snum)
   14060             :     {
   14061           0 :       error_at (input_location, "recursive lazy load");
   14062           0 :       return true;
   14063             :     }
   14064             : 
   14065        4746 :   lazy_snum = snum;
   14066        4746 :   return false;
   14067             : }
   14068             : 
   14069             : /* If THIS is the current purview, issue an import error and return false.  */
   14070             : 
   14071             : bool
   14072        1787 : module_state::check_not_purview (location_t from)
   14073             : {
   14074        1787 :   module_state *imp = (*modules)[0];
   14075        1787 :   if (imp && !imp->name)
   14076        1330 :     imp = imp->parent;
   14077        1787 :   if (imp == this)
   14078             :     {
   14079             :       /* Cannot import the current module.  */
   14080          12 :       error_at (from, "cannot import module in its own purview");
   14081          12 :       inform (loc, "module %qs declared here", get_flatname ());
   14082          12 :       return false;
   14083             :     }
   14084             :   return true;
   14085             : }
   14086             : 
   14087             : /* Module name substitutions.  */
   14088             : static vec<module_state *,va_heap> substs;
   14089             : 
   14090             : void
   14091        4495 : module_state::mangle (bool include_partition)
   14092             : {
   14093        4495 :   if (subst)
   14094         101 :     mangle_module_substitution (subst);
   14095             :   else
   14096             :     {
   14097        4394 :       if (parent)
   14098         432 :         parent->mangle (include_partition);
   14099        4394 :       if (include_partition || !is_partition ()) 
   14100             :         {
   14101             :           // Partitions are significant for global initializer
   14102             :           // functions
   14103        4349 :           bool partition = is_partition () && !parent->is_partition ();
   14104        4349 :           subst = mangle_module_component (name, partition);
   14105        4349 :           substs.safe_push (this);
   14106             :         }
   14107             :     }
   14108        4495 : }
   14109             : 
   14110             : void
   14111        4063 : mangle_module (int mod, bool include_partition)
   14112             : {
   14113        4063 :   module_state *imp = (*modules)[mod];
   14114             : 
   14115        4063 :   gcc_checking_assert (!imp->is_header ());
   14116             : 
   14117        4063 :   if (!imp->name)
   14118             :     /* Set when importing the primary module interface.  */
   14119         189 :     imp = imp->parent;
   14120             : 
   14121        4063 :   imp->mangle (include_partition);
   14122        4063 : }
   14123             : 
   14124             : /* Clean up substitutions.  */
   14125             : void
   14126        3932 : mangle_module_fini ()
   14127             : {
   14128        8281 :   while (substs.length ())
   14129        4349 :     substs.pop ()->subst = 0;
   14130        3932 : }
   14131             : 
   14132             : /* Announce WHAT about the module.  */
   14133             : 
   14134             : void
   14135        7771 : module_state::announce (const char *what) const
   14136             : {
   14137        7771 :   if (noisy_p ())
   14138             :     {
   14139           0 :       fprintf (stderr, " %s:%s", what, get_flatname ());
   14140           0 :       fflush (stderr);
   14141             :     }
   14142        7771 : }
   14143             : 
   14144             : /* A human-readable README section.  The contents of this section to
   14145             :    not contribute to the CRC, so the contents can change per
   14146             :    compilation.  That allows us to embed CWD, hostname, build time and
   14147             :    what not.  It is a STRTAB that may be extracted with:
   14148             :      readelf -pgnu.c++.README $(module).gcm */
   14149             : 
   14150             : void
   14151        1660 : module_state::write_readme (elf_out *to, cpp_reader *reader, const char *dialect)
   14152             : {
   14153        1660 :   bytes_out readme (to);
   14154             : 
   14155        1660 :   readme.begin (false);
   14156             : 
   14157        1660 :   readme.printf ("GNU C++ %s",
   14158        1660 :                  is_header () ? "header unit"
   14159        1017 :                  : !is_partition () ? "primary interface"
   14160          87 :                  : is_interface () ? "interface partition"
   14161             :                  : "internal partition");
   14162             : 
   14163             :   /* Compiler's version.  */
   14164        1660 :   readme.printf ("compiler: %s", version_string);
   14165             : 
   14166             :   /* Module format version.  */
   14167        1660 :   verstr_t string;
   14168        1660 :   version2string (MODULE_VERSION, string);
   14169        1660 :   readme.printf ("version: %s", string);
   14170             : 
   14171             :   /* Module information.  */
   14172        1660 :   readme.printf ("module: %s", get_flatname ());
   14173        1660 :   readme.printf ("source: %s", main_input_filename);
   14174        1660 :   readme.printf ("dialect: %s", dialect);
   14175        1660 :   if (extensions)
   14176           3 :     readme.printf ("extensions: %s",
   14177           3 :                    extensions & SE_OPENMP ? "-fopenmp" : "");
   14178             : 
   14179             :   /* The following fields could be expected to change between
   14180             :      otherwise identical compilations.  Consider a distributed build
   14181             :      system.  We should have a way of overriding that.  */
   14182        1660 :   if (char *cwd = getcwd (NULL, 0))
   14183             :     {
   14184        1660 :       readme.printf ("cwd: %s", cwd);
   14185        1660 :       free (cwd);
   14186             :     }
   14187        3320 :   readme.printf ("repository: %s", cmi_repo ? cmi_repo : ".");
   14188             : #if NETWORKING
   14189             :   {
   14190             :     char hostname[64];
   14191             :     if (!gethostname (hostname, sizeof (hostname)))
   14192             :       readme.printf ("host: %s", hostname);
   14193             :   }
   14194             : #endif
   14195        1660 :   {
   14196             :     /* This of course will change!  */
   14197        1660 :     time_t stampy;
   14198        1660 :     auto kind = cpp_get_date (reader, &stampy);
   14199        1660 :     if (kind != CPP_time_kind::UNKNOWN)
   14200             :       {
   14201        1660 :         struct tm *time;
   14202             : 
   14203        1660 :         time = gmtime (&stampy);
   14204        1660 :         readme.print_time ("build", time, "UTC");
   14205             : 
   14206        1660 :         if (kind == CPP_time_kind::DYNAMIC)
   14207             :           {
   14208        1660 :             time = localtime (&stampy);
   14209        1660 :             readme.print_time ("local", time,
   14210             : #if defined (__USE_MISC) || defined (__USE_BSD) /* Is there a better way?  */
   14211             :                                time->tm_zone
   14212             : #else
   14213             :                                ""
   14214             : #endif
   14215             :                                );
   14216             :           }
   14217             :       }
   14218             :   }
   14219             : 
   14220             :   /* Its direct imports.  */
   14221        2070 :   for (unsigned ix = 1; ix < modules->length (); ix++)
   14222             :     {
   14223         410 :       module_state *state = (*modules)[ix];
   14224             : 
   14225         410 :       if (state->is_direct ())
   14226         613 :         readme.printf ("%s: %s %s", state->exported_p ? "export" : "import",
   14227             :                        state->get_flatname (), state->filename);
   14228             :     }
   14229             : 
   14230        1660 :   readme.end (to, to->name (MOD_SNAME_PFX ".README"), NULL);
   14231        1660 : }
   14232             : 
   14233             : /* Sort environment var names in reverse order.  */
   14234             : 
   14235             : static int
   14236           0 : env_var_cmp (const void *a_, const void *b_)
   14237             : {
   14238           0 :   const unsigned char *a = *(const unsigned char *const *)a_;
   14239           0 :   const unsigned char *b = *(const unsigned char *const *)b_;
   14240             : 
   14241           0 :   for (unsigned ix = 0; ; ix++)
   14242             :     {
   14243           0 :       bool a_end = !a[ix] || a[ix] == '=';
   14244           0 :       if (a[ix] == b[ix])
   14245             :         {
   14246           0 :           if (a_end)
   14247             :             break;
   14248             :         }
   14249             :       else
   14250             :         {
   14251           0 :           bool b_end = !b[ix] || b[ix] == '=';
   14252             : 
   14253           0 :           if (!a_end && !b_end)
   14254           0 :             return a[ix] < b[ix] ? +1 : -1;
   14255           0 :           if (a_end && b_end)
   14256             :             break;
   14257           0 :           return a_end ? +1 : -1;
   14258             :         }
   14259           0 :     }
   14260             : 
   14261             :   return 0;
   14262             : }
   14263             : 
   14264             : /* Write the environment. It is a STRTAB that may be extracted with:
   14265             :      readelf -pgnu.c++.ENV $(module).gcm */
   14266             : 
   14267             : void
   14268           0 : module_state::write_env (elf_out *to)
   14269             : {
   14270           0 :   vec<const char *> vars;
   14271           0 :   vars.create (20);
   14272             : 
   14273           0 :   extern char **environ;
   14274           0 :   while (const char *var = environ[vars.length ()])
   14275           0 :     vars.safe_push (var);
   14276           0 :   vars.qsort (env_var_cmp);
   14277             : 
   14278           0 :   bytes_out env (to);
   14279           0 :   env.begin (false);
   14280           0 :   while (vars.length ())
   14281           0 :     env.printf ("%s", vars.pop ());
   14282           0 :   env.end (to, to->name (MOD_SNAME_PFX ".ENV"), NULL);
   14283             : 
   14284           0 :   vars.release ();
   14285           0 : }
   14286             : 
   14287             : /* Write the direct or indirect imports.
   14288             :    u:N
   14289             :    {
   14290             :      u:index
   14291             :      s:name
   14292             :      u32:crc
   14293             :      s:filename (direct)
   14294             :      u:exported (direct)
   14295             :    } imports[N]
   14296             :  */
   14297             : 
   14298             : void
   14299         594 : module_state::write_imports (bytes_out &sec, bool direct)
   14300             : {
   14301         594 :   unsigned count = 0;
   14302             : 
   14303        1246 :   for (unsigned ix = 1; ix < modules->length (); ix++)
   14304             :     {
   14305         652 :       module_state *imp = (*modules)[ix];
   14306             : 
   14307         652 :       if (imp->remap && imp->is_direct () == direct)
   14308         317 :         count++;
   14309             :     }
   14310             : 
   14311         594 :   gcc_assert (!direct || count);
   14312             : 
   14313         594 :   sec.u (count);
   14314        1246 :   for (unsigned ix = 1; ix < modules->length (); ix++)
   14315             :     {
   14316         652 :       module_state *imp = (*modules)[ix];
   14317             : 
   14318         652 :       if (imp->remap && imp->is_direct () == direct)
   14319             :         {
   14320         437 :           dump () && dump ("Writing %simport:%u->%u %M (crc=%x)",
   14321             :                            !direct ? "indirect "
   14322          60 :                            : imp->exported_p ? "exported " : "",
   14323             :                            ix, imp->remap, imp, imp->crc);
   14324         317 :           sec.u (imp->remap);
   14325         317 :           sec.str (imp->get_flatname ());
   14326         317 :           sec.u32 (imp->crc);
   14327         317 :           if (direct)
   14328             :             {
   14329         316 :               write_location (sec, imp->imported_from ());
   14330         316 :               sec.str (imp->filename);
   14331         316 :               int exportedness = 0;
   14332         316 :               if (imp->exported_p)
   14333             :                 exportedness = +1;
   14334         195 :               else if (!imp->is_purview_direct ())
   14335          12 :                 exportedness = -1;
   14336         316 :               sec.i (exportedness);
   14337             :             }
   14338             :         }
   14339             :     }
   14340         594 : }
   14341             : 
   14342             : /* READER, LMAPS  != NULL == direct imports,
   14343             :    == NUL == indirect imports.  */
   14344             : 
   14345             : unsigned
   14346         484 : module_state::read_imports (bytes_in &sec, cpp_reader *reader, line_maps *lmaps)
   14347             : {
   14348         484 :   unsigned count = sec.u ();
   14349         484 :   unsigned loaded = 0;
   14350             : 
   14351        1225 :   while (count--)
   14352             :     {
   14353         257 :       unsigned ix = sec.u ();
   14354         257 :       if (ix >= slurp->remap->length () || !ix || (*slurp->remap)[ix])
   14355             :         {
   14356           0 :           sec.set_overrun ();
   14357             :           break;
   14358             :         }
   14359             : 
   14360         257 :       const char *name = sec.str (NULL);
   14361         257 :       module_state *imp = get_module (name);
   14362         257 :       unsigned crc = sec.u32 ();
   14363         257 :       int exportedness = 0;
   14364             : 
   14365             :       /* If the import is a partition, it must be the same primary
   14366             :          module as this TU.  */
   14367         257 :       if (imp && imp->is_partition () &&
   14368             :           (!named_module_p ()
   14369         108 :            || (get_primary ((*modules)[0]) != get_primary (imp))))
   14370             :         imp = NULL;
   14371             : 
   14372         257 :       if (!imp)
   14373           0 :         sec.set_overrun ();
   14374         257 :       if (sec.get_overrun ())
   14375             :         break;
   14376             : 
   14377         257 :       if (lmaps)
   14378             :         {
   14379             :           /* A direct import, maybe load it.  */
   14380         257 :           location_t floc = read_location (sec);
   14381         257 :           const char *fname = sec.str (NULL);
   14382         257 :           exportedness = sec.i ();
   14383             : 
   14384         257 :           if (sec.get_overrun ())
   14385             :             break;
   14386             : 
   14387         257 :           if (!imp->check_not_purview (loc))
   14388           3 :             continue;
   14389             : 
   14390         254 :           if (imp->loadedness == ML_NONE)
   14391             :             {
   14392         201 :               imp->loc = floc;
   14393         201 :               imp->crc = crc;
   14394         201 :               if (!imp->get_flatname ())
   14395         171 :                 imp->set_flatname ();
   14396             : 
   14397         201 :               unsigned n = dump.push (imp);
   14398             : 
   14399         201 :               if (!imp->filename && fname)
   14400         168 :                 imp->filename = xstrdup (fname);
   14401             : 
   14402         201 :               if (imp->is_partition ())
   14403          27 :                 dump () && dump ("Importing elided partition %M", imp);
   14404             : 
   14405         201 :               if (!imp->do_import (reader, false))
   14406           3 :                 imp = NULL;
   14407         201 :               dump.pop (n);
   14408         201 :               if (!imp)
   14409           3 :                 continue;
   14410             :             }
   14411             : 
   14412         251 :           if (is_partition ())
   14413             :             {
   14414          42 :               if (!imp->is_direct ())
   14415          18 :                 imp->directness = MD_PARTITION_DIRECT;
   14416          42 :               if (exportedness > 0)
   14417           3 :                 imp->exported_p = true;
   14418             :             }
   14419             :         }
   14420             :       else
   14421             :         {
   14422             :           /* An indirect import, find it, it should already be here.  */
   14423           0 :           if (imp->loadedness == ML_NONE)
   14424             :             {
   14425           0 :               error_at (loc, "indirect import %qs is not already loaded", name);
   14426           0 :               continue;
   14427             :             }
   14428             :         }
   14429             : 
   14430         251 :       if (imp->crc != crc)
   14431           0 :         error_at (loc, "import %qs has CRC mismatch", imp->get_flatname ());
   14432             : 
   14433         251 :       (*slurp->remap)[ix] = (imp->mod << 1) | (lmaps != NULL);
   14434             : 
   14435         251 :       if (lmaps && exportedness >= 0)
   14436         239 :         set_import (imp, bool (exportedness));
   14437         389 :       dump () && dump ("Found %simport:%u %M->%u", !lmaps ? "indirect "
   14438          69 :                        : exportedness > 0 ? "exported "
   14439          36 :                        : exportedness < 0 ? "gmf" : "", ix, imp,
   14440             :                        imp->mod);
   14441         251 :       loaded++;
   14442             :     }
   14443             : 
   14444         484 :   return loaded;
   14445             : }
   14446             : 
   14447             : /* Write the import table to MOD_SNAME_PFX.imp.  */
   14448             : 
   14449             : void
   14450         297 : module_state::write_imports (elf_out *to, unsigned *crc_ptr)
   14451             : {
   14452         354 :   dump () && dump ("Writing imports");
   14453         297 :   dump.indent ();
   14454             : 
   14455         297 :   bytes_out sec (to);
   14456         297 :   sec.begin ();
   14457             : 
   14458         297 :   write_imports (sec, true);
   14459         297 :   write_imports (sec, false);
   14460             : 
   14461         297 :   sec.end (to, to->name (MOD_SNAME_PFX ".imp"), crc_ptr);
   14462         297 :   dump.outdent ();
   14463         297 : }
   14464             : 
   14465             : bool
   14466         242 : module_state::read_imports (cpp_reader *reader, line_maps *lmaps)
   14467             : {
   14468         242 :   bytes_in sec;
   14469             : 
   14470         242 :   if (!sec.begin (loc, from (), MOD_SNAME_PFX ".imp"))
   14471             :     return false;
   14472             : 
   14473         308 :   dump () && dump ("Reading %u imports", slurp->remap->length () - 1);
   14474         242 :   dump.indent ();
   14475             : 
   14476             :   /* Read the imports.  */
   14477         242 :   unsigned direct = read_imports (sec, reader, lmaps);
   14478         242 :   unsigned indirect = read_imports (sec, NULL, NULL);
   14479         242 :   if (direct + indirect + 1 != slurp->remap->length ())
   14480           6 :     from ()->set_error (elf::E_BAD_IMPORT);
   14481             : 
   14482         242 :   dump.outdent ();
   14483         242 :   if (!sec.end (from ()))
   14484             :     return false;
   14485             :   return true;
   14486         242 : }
   14487             : 
   14488             : /* We're the primary module interface, but have partitions.  Document
   14489             :    them so that non-partition module implementation units know which
   14490             :    have already been loaded.  */
   14491             : 
   14492             : void
   14493          63 : module_state::write_partitions (elf_out *to, unsigned count, unsigned *crc_ptr)
   14494             : {
   14495          81 :   dump () && dump ("Writing %u elided partitions", count);
   14496          63 :   dump.indent ();
   14497             : 
   14498          63 :   bytes_out sec (to);
   14499          63 :   sec.begin ();
   14500             : 
   14501         165 :   for (unsigned ix = 1; ix != modules->length (); ix++)
   14502             :     {
   14503         102 :       module_state *imp = (*modules)[ix];
   14504         102 :       if (imp->is_partition ())
   14505             :         {
   14506         123 :           dump () && dump ("Writing elided partition %M (crc=%x)",
   14507             :                            imp, imp->crc);
   14508          93 :           sec.str (imp->get_flatname ());
   14509          93 :           sec.u32 (imp->crc);
   14510         180 :           write_location (sec, imp->is_direct ()
   14511          87 :                           ? imp->imported_from () : UNKNOWN_LOCATION);
   14512          93 :           sec.str (imp->filename);
   14513             :         }
   14514             :     }
   14515             : 
   14516          63 :   sec.end (to, to->name (MOD_SNAME_PFX ".prt"), crc_ptr);
   14517          63 :   dump.outdent ();
   14518          63 : }
   14519             : 
   14520             : bool
   14521          12 : module_state::read_partitions (unsigned count)
   14522             : {
   14523          12 :   bytes_in sec;
   14524          12 :   if (!sec.begin (loc, from (), MOD_SNAME_PFX ".prt"))
   14525             :     return false;
   14526             : 
   14527          15 :   dump () && dump ("Reading %u elided partitions", count);
   14528          12 :   dump.indent ();
   14529             : 
   14530          30 :   while (count--)
   14531             :     {
   14532          18 :       const char *name = sec.str (NULL);
   14533          18 :       unsigned crc = sec.u32 ();
   14534          18 :       location_t floc = read_location (sec);
   14535          18 :       const char *fname = sec.str (NULL);
   14536             : 
   14537          18 :       if (sec.get_overrun ())
   14538             :         break;
   14539             : 
   14540          24 :       dump () && dump ("Reading elided partition %s (crc=%x)", name, crc);
   14541             : 
   14542          18 :       module_state *imp = get_module (name);
   14543          18 :       if (!imp  /* Partition should be ...  */
   14544          18 :           || !imp->is_partition () /* a partition ...  */
   14545          18 :           || imp->loadedness != ML_NONE  /* that is not yet loaded ...  */
   14546          36 :           || get_primary (imp) != this) /* whose primary is this.  */
   14547             :         {
   14548           0 :           sec.set_overrun ();
   14549             :           break;
   14550             :         }
   14551             : 
   14552          18 :       if (!imp->has_location ())
   14553          15 :         imp->loc = floc;
   14554          18 :       imp->crc = crc;
   14555          18 :       if (!imp->filename && fname[0])
   14556          15 :         imp->filename = xstrdup (fname);
   14557             :     }
   14558             : 
   14559          12 :   dump.outdent ();
   14560          12 :   if (!sec.end (from ()))
   14561             :     return false;
   14562             :   return true;
   14563          12 : }
   14564             : 
   14565             : /* Data for config reading and writing.  */
   14566             : struct module_state_config {
   14567             :   const char *dialect_str;
   14568             :   unsigned num_imports;
   14569             :   unsigned num_partitions;
   14570             :   unsigned num_entities;
   14571             :   unsigned ordinary_locs;
   14572             :   unsigned macro_locs;
   14573             :   unsigned loc_range_bits;
   14574             :   unsigned active_init;
   14575             : 
   14576             : public:
   14577        3573 :   module_state_config ()
   14578        3573 :     :dialect_str (get_dialect ()),
   14579        3573 :      num_imports (0), num_partitions (0), num_entities (0),
   14580        3573 :      ordinary_locs (0), macro_locs (0), loc_range_bits (0),
   14581        3573 :      active_init (0)
   14582             :   {
   14583        3573 :   }
   14584             : 
   14585       88316 :   static void release ()
   14586             :   {
   14587       88316 :     XDELETEVEC (dialect);
   14588       88316 :     dialect = NULL;
   14589             :   }
   14590             : 
   14591             : private:
   14592             :   static const char *get_dialect ();
   14593             :   static char *dialect;
   14594             : };
   14595             : 
   14596             : char *module_state_config::dialect;
   14597             : 
   14598             : /* Generate a string of the significant compilation options.
   14599             :    Generally assume the user knows what they're doing, in the same way
   14600             :    that object files can be mixed.  */
   14601             : 
   14602             : const char *
   14603        3573 : module_state_config::get_dialect ()
   14604             : {
   14605        3573 :   if (!dialect)
   14606        2816 :     dialect = concat (get_cxx_dialect_name (cxx_dialect),
   14607             :                       /* C++ implies these, only show if disabled.  */
   14608        2816 :                       flag_exceptions ? "" : "/no-exceptions",
   14609        2816 :                       flag_rtti ? "" : "/no-rtti",
   14610        2816 :                       flag_new_inheriting_ctors ? "" : "/old-inheriting-ctors",
   14611             :                       /* C++ 20 implies concepts.  */
   14612        2816 :                       cxx_dialect < cxx20 && flag_concepts ? "/concepts" : "",
   14613        2816 :                       flag_coroutines ? "/coroutines" : "",
   14614        2816 :                       flag_module_implicit_inline ? "/implicit-inline" : "",
   14615        2816 :                       flag_contracts ? "/contracts" : "",
   14616             :                       NULL);
   14617             : 
   14618        3573 :   return dialect;
   14619             : }
   14620             : 
   14621             : /* Contents of a cluster.  */
   14622             : enum cluster_tag {
   14623             :   ct_decl,      /* A decl.  */
   14624             :   ct_defn,      /* A definition.  */
   14625             :   ct_bind,      /* A binding.  */
   14626             :   ct_hwm
   14627             : };
   14628             : 
   14629             : /* Binding modifiers.  */
   14630             : enum ct_bind_flags
   14631             : {
   14632             :   cbf_export = 0x1,     /* An exported decl.  */
   14633             :   cbf_hidden = 0x2,     /* A hidden (friend) decl.  */
   14634             :   cbf_using = 0x4,      /* A using decl.  */
   14635             :   cbf_wrapped = 0x8,    /* ... that is wrapped.  */
   14636             : };
   14637             : 
   14638             : /* DEP belongs to a different cluster, seed it to prevent
   14639             :    unfortunately timed duplicate import.  */
   14640             : // FIXME: QOI For inter-cluster references we could just only pick
   14641             : // one entity from an earlier cluster.  Even better track
   14642             : // dependencies between earlier clusters
   14643             : 
   14644             : void
   14645     6315292 : module_state::intercluster_seed (trees_out &sec, unsigned index_hwm, depset *dep)
   14646             : {
   14647     6315292 :   if (dep->is_import ()
   14648     6315292 :       || dep->cluster < index_hwm)
   14649             :     {
   14650     2672905 :       tree ent = dep->get_entity ();
   14651     2672905 :       if (!TREE_VISITED (ent))
   14652             :         {
   14653     1255923 :           sec.tree_node (ent);
   14654     1256313 :           dump (dumper::CLUSTER)
   14655         390 :             && dump ("Seeded %s %N",
   14656         390 :                      dep->is_import () ? "import" : "intercluster", ent);
   14657             :         }
   14658             :     }
   14659     6315292 : }
   14660             : 
   14661             : /* Write the cluster of depsets in SCC[0-SIZE).
   14662             :    dep->section -> section number
   14663             :    dep->cluster -> entity number
   14664             :  */
   14665             : 
   14666             : unsigned
   14667      302068 : module_state::write_cluster (elf_out *to, depset *scc[], unsigned size,
   14668             :                              depset::hash &table, unsigned *counts,
   14669             :                              unsigned *crc_ptr)
   14670             : {
   14671      308225 :   dump () && dump ("Writing section:%u %u depsets", table.section, size);
   14672      302068 :   dump.indent ();
   14673             : 
   14674      302068 :   trees_out sec (to, this, table, table.section);
   14675      302068 :   sec.begin ();
   14676      302068 :   unsigned index_lwm = counts[MSC_entities];
   14677             : 
   14678             :   /* Determine entity numbers, mark for writing.   */
   14679      302350 :   dump (dumper::CLUSTER) && dump ("Cluster members:") && (dump.indent (), true);
   14680     1509390 :   for (unsigned ix = 0; ix != size; ix++)
   14681             :     {
   14682     1207322 :       depset *b = scc[ix];
   14683             : 
   14684     1207322 :       switch (b->get_entity_kind ())
   14685             :         {
   14686           0 :         default:
   14687           0 :           gcc_unreachable ();
   14688             : 
   14689      167244 :         case depset::EK_BINDING:
   14690      167244 :           {
   14691      167244 :             dump (dumper::CLUSTER)
   14692         195 :               && dump ("[%u]=%s %P", ix, b->entity_kind_name (),
   14693             :                        b->get_entity (), b->get_name ());
   14694      167244 :             depset *ns_dep = b->deps[0];
   14695      167244 :             gcc_checking_assert (ns_dep->get_entity_kind ()
   14696             :                                  == depset::EK_NAMESPACE
   14697             :                                  && ns_dep->get_entity () == b->get_entity ());
   14698      374163 :             for (unsigned jx = b->deps.length (); --jx;)
   14699             :               {
   14700      206919 :                 depset *dep = b->deps[jx];
   14701             :                 // We could be declaring something that is also a
   14702             :                 // (merged) import
   14703      229148 :                 gcc_checking_assert (dep->is_import ()
   14704             :                                      || TREE_VISITED (dep->get_entity ())
   14705             :                                      || (dep->get_entity_kind ()
   14706             :                                          == depset::EK_USING));
   14707             :               }
   14708             :           }
   14709             :           break;
   14710             : 
   14711     1017849 :         case depset::EK_DECL:
   14712     1017849 :         case depset::EK_SPECIALIZATION:
   14713     1017849 :         case depset::EK_PARTIAL:
   14714     1017849 :           b->cluster = counts[MSC_entities]++;
   14715     1017849 :           sec.mark_declaration (b->get_entity (), b->has_defn ());
   14716             :           /* FALLTHROUGH  */
   14717             : 
   14718     1040078 :         case depset::EK_USING:
   14719     2080156 :           gcc_checking_assert (!b->is_import ()
   14720             :                                && !b->is_unreached ());
   14721     1231323 :           dump (dumper::CLUSTER)
   14722        1278 :             && dump ("[%u]=%s %s %N", ix, b->entity_kind_name (),
   14723         639 :                      b->has_defn () ? "definition" : "declaration",
   14724             :                      b->get_entity ());
   14725             :           break;
   14726             :         }
   14727             :     }
   14728      302350 :   dump (dumper::CLUSTER) && (dump.outdent (), true);
   14729             : 
   14730             :   /* Ensure every out-of-cluster decl is referenced before we start
   14731             :      streaming.  We must do both imports *and* earlier clusters,
   14732             :      because the latter could reach into the former and cause a
   14733             :      duplicate loop.   */
   14734      302068 :   sec.set_importing (+1);
   14735     1509390 :   for (unsigned ix = 0; ix != size; ix++)
   14736             :     {
   14737     1207322 :       depset *b = scc[ix];
   14738     6753947 :       for (unsigned jx = (b->get_entity_kind () == depset::EK_BINDING
   14739     1040078 :                           || b->is_special ()) ? 1 : 0;
   14740    13501216 :            jx != b->deps.length (); jx++)
   14741             :         {
   14742     5546625 :           depset *dep = b->deps[jx];
   14743             : 
   14744     5546625 :           if (dep->is_binding ())
   14745             :             {
   14746     1182505 :               for (unsigned ix = dep->deps.length (); --ix;)
   14747             :                 {
   14748      768667 :                   depset *bind = dep->deps[ix];
   14749      768667 :                   if (bind->get_entity_kind () == depset::EK_USING)
   14750       40201 :                     bind = bind->deps[1];
   14751             : 
   14752      768667 :                   intercluster_seed (sec, index_lwm, bind);
   14753             :                 }
   14754             :               /* Also check the namespace itself.  */
   14755      206919 :               dep = dep->deps[0];
   14756             :             }
   14757             : 
   14758     5546625 :           intercluster_seed (sec, index_lwm, dep);
   14759             :         }
   14760             :     }
   14761      302068 :   sec.tree_node (NULL_TREE);
   14762             :   /* We're done importing now.  */
   14763      302068 :   sec.set_importing (-1);
   14764             : 
   14765             :   /* Write non-definitions.  */
   14766     1509390 :   for (unsigned ix = 0; ix != size; ix++)
   14767             :     {
   14768     1207322 :       depset *b = scc[ix];
   14769     1207322 :       tree decl = b->get_entity ();
   14770     1207322 :       switch (b->get_entity_kind ())
   14771             :         {
   14772           0 :         default:
   14773           0 :           gcc_unreachable ();
   14774      167244 :           break;
   14775             : 
   14776      167244 :         case depset::EK_BINDING:
   14777      167244 :           {
   14778      167244 :             gcc_assert (TREE_CODE (decl) == NAMESPACE_DECL);
   14779      168231 :             dump () && dump ("Depset:%u binding %C:%P", ix, TREE_CODE (decl),
   14780             :                              decl, b->get_name ());
   14781      167244 :             sec.u (ct_bind);
   14782      167244 :             sec.tree_node (decl);
   14783      167244 :             sec.tree_node (b->get_name ());
   14784             : 
   14785             :             /* Write in reverse order, so reading will see the exports
   14786             :                first, thus building the overload chain will be
   14787             :                optimized.  */
   14788      541407 :             for (unsigned jx = b->deps.length (); --jx;)
   14789             :               {
   14790      206919 :                 depset *dep = b->deps[jx];
   14791      206919 :                 tree bound = dep->get_entity ();
   14792      206919 :                 unsigned flags = 0;
   14793      206919 :                 if (dep->get_entity_kind () == depset::EK_USING)
   14794             :                   {
   14795       22229 :                     tree ovl = bound;
   14796       22229 :                     bound = OVL_FUNCTION (bound);
   14797       22229 :                     if (!(TREE_CODE (bound) == CONST_DECL
   14798        8139 :                           && UNSCOPED_ENUM_P (TREE_TYPE (bound))
   14799        8139 :                           && decl == TYPE_NAME (TREE_TYPE (bound))))
   14800             :                       {
   14801             :                         /* An unscope enumerator in its enumeration's
   14802             :                            scope is not a using.  */
   14803       22229 :                         flags |= cbf_using;
   14804       22229 :                         if (OVL_USING_P (ovl))
   14805           0 :                           flags |= cbf_wrapped;
   14806             :                       }
   14807       22229 :                     if (OVL_EXPORT_P (ovl))
   14808       22220 :                       flags |= cbf_export;
   14809             :                   }
   14810             :                 else
   14811             :                   {
   14812             :                     /* An implicit typedef must be at one.  */
   14813      184690 :                     gcc_assert (!DECL_IMPLICIT_TYPEDEF_P (bound) || jx == 1);
   14814      184690 :                     if (dep->is_hidden ())
   14815             :                       flags |= cbf_hidden;
   14816      180173 :                     else if (DECL_MODULE_EXPORT_P (STRIP_TEMPLATE (bound)))
   14817      179607 :                       flags |= cbf_export;
   14818             :                   }
   14819             : 
   14820      206919 :                 gcc_checking_assert (DECL_P (bound));
   14821             : 
   14822      206919 :                 sec.i (flags);
   14823      206919 :                 sec.tree_node (bound);
   14824             :               }
   14825             : 
   14826             :             /* Terminate the list.  */
   14827      167244 :             sec.i (-1);
   14828             :           }
   14829      167244 :           break;
   14830             : 
   14831       22229 :         case depset::EK_USING:
   14832       22250 :           dump () && dump ("Depset:%u %s %C:%N", ix, b->entity_kind_name (),
   14833          21 :                            TREE_CODE (decl), decl);
   14834             :           break;
   14835             : 
   14836     1017849 :         case depset::EK_SPECIALIZATION:
   14837     1017849 :         case depset::EK_PARTIAL:
   14838     1017849 :         case depset::EK_DECL:
   14839     1041829 :           dump () && dump ("Depset:%u %s entity:%u %C:%N", ix,
   14840             :                            b->entity_kind_name (), b->cluster,
   14841       23980 :                            TREE_CODE (decl), decl);
   14842             : 
   14843     1017849 :           sec.u (ct_decl);
   14844     1017849 :           sec.tree_node (decl);
   14845             : 
   14846     1231302 :           dump () && dump ("Wrote declaration entity:%u %C:%N",
   14847       23980 :                            b->cluster, TREE_CODE (decl), decl);
   14848             :           break;
   14849             :         }
   14850             :     }
   14851             : 
   14852             :   depset *namer = NULL;
   14853             : 
   14854             :   /* Write out definitions  */
   14855     1509390 :   for (unsigned ix = 0; ix != size; ix++)
   14856             :     {
   14857     1207322 :       depset *b = scc[ix];
   14858     1207322 :       tree decl = b->get_entity ();
   14859     1207322 :       switch (b->get_entity_kind ())
   14860             :         {
   14861             :         default:
   14862             :           break;
   14863             : 
   14864     1017849 :         case depset::EK_SPECIALIZATION:
   14865     1017849 :         case depset::EK_PARTIAL:
   14866     1017849 :         case depset::EK_DECL:
   14867     1017849 :           if (!namer)
   14868      290831 :             namer = b;
   14869             : 
   14870     1017849 :           if (b->has_defn ())
   14871             :             {
   14872      362982 :               sec.u (ct_defn);
   14873      362982 :               sec.tree_node (decl);
   14874      371507 :               dump () && dump ("Writing definition %N", decl);
   14875      362982 :               sec.write_definition (decl);
   14876             : 
   14877      362982 :               if (!namer->has_defn ())
   14878     1207322 :                 namer = b;
   14879             :             }
   14880             :           break;
   14881             :         }
   14882             :     }
   14883             : 
   14884             :   /* We don't find the section by name.  Use depset's decl's name for
   14885             :      human friendliness.  */
   14886      302068 :   unsigned name = 0;
   14887      302068 :   tree naming_decl = NULL_TREE;
   14888      302068 :   if (namer)
   14889             :     {
   14890      290831 :       naming_decl = namer->get_entity ();
   14891      290831 :       if (namer->get_entity_kind () == depset::EK_USING)
   14892             :         /* This unfortunately names the section from the target of the
   14893             :            using decl.  But the name is only a guide, so Do Not Care.  */
   14894           0 :         naming_decl = OVL_FUNCTION (naming_decl);
   14895      290831 :       if (DECL_IMPLICIT_TYPEDEF_P (naming_decl))
   14896             :         /* Lose any anonymousness.  */
   14897       98237 :         naming_decl = TYPE_NAME (TREE_TYPE (naming_decl));
   14898      290831 :       name = to->qualified_name (naming_decl, namer->has_defn ());
   14899             :     }
   14900             : 
   14901      302068 :   unsigned bytes = sec.pos;
   14902      302068 :   unsigned snum = sec.end (to, name, crc_ptr);
   14903             : 
   14904     1509390 :   for (unsigned ix = size; ix--;)
   14905     1207322 :     gcc_checking_assert (scc[ix]->section == snum);
   14906             : 
   14907      302068 :   dump.outdent ();
   14908      308225 :   dump () && dump ("Wrote section:%u named-by:%N", table.section, naming_decl);
   14909             : 
   14910      302068 :   return bytes;
   14911      302068 : }
   14912             : 
   14913             : /* Read a cluster from section SNUM.  */
   14914             : 
   14915             : bool
   14916      329672 : module_state::read_cluster (unsigned snum)
   14917             : {
   14918      329672 :   trees_in sec (this);
   14919             : 
   14920      329672 :   if (!sec.begin (loc, from (), snum))
   14921             :     return false;
   14922             : 
   14923      330710 :   dump () && dump ("Reading section:%u", snum);
   14924      329672 :   dump.indent ();
   14925             : 
   14926             :   /* We care about structural equality.  */
   14927      329672 :   comparing_dependent_aliases++;
   14928             : 
   14929             :   /* First seed the imports.  */
   14930     1760270 :   while (tree import = sec.tree_node ())
   14931     3191033 :     dump (dumper::CLUSTER) && dump ("Seeded import %N", import);
   14932             : 
   14933     2110151 :   while (!sec.get_overrun () && sec.more_p ())
   14934             :     {
   14935     1780479 :       unsigned ct = sec.u ();
   14936     1780479 :       switch (ct)
   14937             :         {
   14938           0 :         default:
   14939           0 :           sec.set_overrun ();
   14940             :           break;
   14941             : 
   14942      192945 :         case ct_bind:
   14943             :           /* A set of namespace bindings.  */
   14944      192945 :           {
   14945      192945 :             tree ns = sec.tree_node ();
   14946      192945 :             tree name = sec.tree_node ();
   14947      192945 :             tree decls = NULL_TREE;
   14948      192945 :             tree visible = NULL_TREE;
   14949      192945 :             tree type = NULL_TREE;
   14950      192945 :             bool dedup = false;
   14951             : 
   14952             :             /* We rely on the bindings being in the reverse order of
   14953             :                the resulting overload set.  */
   14954      434815 :             for (;;)
   14955             :               {
   14956      869630 :                 int flags = sec.i ();
   14957      434815 :                 if (flags < 0)
   14958             :                   break;
   14959             : 
   14960      241870 :                 if ((flags & cbf_hidden)
   14961        5171 :                     && (flags & (cbf_using | cbf_export)))
   14962           0 :                   sec.set_overrun ();
   14963             : 
   14964      241870 :                 tree decl = sec.tree_node ();
   14965      241870 :                 if (sec.get_overrun ())
   14966             :                   break;
   14967             : 
   14968      241870 :                 if (decls && TREE_CODE (decl) == TYPE_DECL)
   14969             :                   {
   14970             :                     /* Stat hack.  */
   14971          81 :                     if (type || !DECL_IMPLICIT_TYPEDEF_P (decl))
   14972      434815 :                       sec.set_overrun ();
   14973             :                     type = decl;
   14974             :                   }
   14975             :                 else
   14976             :                   {
   14977             :                     if (decls
   14978      192945 :                         || (flags & (cbf_hidden | cbf_wrapped))
   14979      258345 :                         || DECL_FUNCTION_TEMPLATE_P (decl))
   14980             :                       {
   14981       73506 :                         decls = ovl_make (decl, decls);
   14982       73506 :                         if (flags & cbf_using)
   14983             :                           {
   14984        3720 :                             dedup = true;
   14985        3720 :                             OVL_USING_P (decls) = true;
   14986        3720 :                             if (flags & cbf_export)
   14987        3720 :                               OVL_EXPORT_P (decls) = true;
   14988             :                           }
   14989             : 
   14990       73506 :                         if (flags & cbf_hidden)
   14991        5171 :                           OVL_HIDDEN_P (decls) = true;
   14992       68335 :                         else if (dedup)
   14993        3720 :                           OVL_DEDUP_P (decls) = true;
   14994             :                       }
   14995             :                     else
   14996             :                       decls = decl;
   14997             : 
   14998      241789 :                     if (flags & cbf_export
   14999      241789 :                         || (!(flags & cbf_hidden)
   15000         409 :                             && (is_module () || is_partition ())))
   15001             :                       visible = decls;
   15002             :                   }
   15003             :               }
   15004             : 
   15005      192945 :             if (!decls)
   15006           0 :               sec.set_overrun ();
   15007             : 
   15008      192945 :             if (sec.get_overrun ())
   15009             :               break; /* Bail.  */
   15010             : 
   15011      193714 :             dump () && dump ("Binding of %P", ns, name);
   15012      192945 :             if (!set_module_binding (ns, name, mod,
   15013      192945 :                                      is_header () ? -1
   15014        1522 :                                      : is_module () || is_partition () ? 1
   15015             :                                      : 0,
   15016             :                                      decls, type, visible))
   15017           0 :               sec.set_overrun ();
   15018             :           }
   15019             :           break;
   15020             : 
   15021     1167227 :         case ct_decl:
   15022             :           /* A decl.  */
   15023     1167227 :           {
   15024     1167227 :             tree decl = sec.tree_node ();
   15025     3280032 :             dump () && dump ("Read declaration of %N", decl);
   15026             :           }
   15027             :           break;
   15028             : 
   15029      420307 :         case ct_defn:
   15030      420307 :           {
   15031      420307 :             tree decl = sec.tree_node ();
   15032      421265 :             dump () && dump ("Reading definition of %N", decl);
   15033      420307 :             sec.read_definition (decl);
   15034             :           }
   15035      420307 :           break;
   15036             :         }
   15037             :     }
   15038             : 
   15039             :   /* When lazy loading is in effect, we can be in the middle of
   15040             :      parsing or instantiating a function.  Save it away.
   15041             :      push_function_context does too much work.   */
   15042      329672 :   tree old_cfd = current_function_decl;
   15043      329672 :   struct function *old_cfun = cfun;
   15044      457376 :   while (tree decl = sec.post_process ())
   15045             :     {
   15046      127704 :       bool abstract = false;
   15047      127704 :       if (TREE_CODE (decl) == TEMPLATE_DECL)
   15048             :         {
   15049       75353 :           abstract = true;
   15050       75353 :           decl = DECL_TEMPLATE_RESULT (decl);
   15051             :         }
   15052             : 
   15053      127704 :       current_function_decl = decl;
   15054      127704 :       allocate_struct_function (decl, abstract);
   15055      127704 :       cfun->language = ggc_cleared_alloc<language_function> ();
   15056      127704 :       cfun->language->base.x_stmt_tree.stmts_are_full_exprs_p = 1;
   15057             : 
   15058      127704 :       if (abstract)
   15059             :         ;
   15060       52351 :       else if (DECL_ABSTRACT_P (decl))
   15061        9325 :         vec_safe_push (post_load_decls, decl);
   15062             :       else
   15063             :         {
   15064       43026 :           bool aggr = aggregate_value_p (DECL_RESULT (decl), decl);
   15065             : #ifdef PCC_STATIC_STRUCT_RETURN
   15066             :           cfun->returns_pcc_struct = aggr;
   15067             : #endif
   15068       43026 :           cfun->returns_struct = aggr;
   15069             : 
   15070       43026 :           if (DECL_COMDAT (decl))
   15071             :             // FIXME: Comdat grouping?
   15072       42448 :             comdat_linkage (decl);
   15073       43026 :           note_vague_linkage_fn (decl);
   15074       43026 :           cgraph_node::finalize_function (decl, true);
   15075             :         }
   15076             : 
   15077      127704 :     }
   15078             :   /* Look, function.cc's interface to cfun does too much for us, we
   15079             :      just need to restore the old value.  I do not want to go
   15080             :      redesigning that API right now.  */
   15081             : #undef cfun
   15082      329672 :   cfun = old_cfun;
   15083      329672 :   current_function_decl = old_cfd;
   15084      329672 :   comparing_dependent_aliases--;
   15085             : 
   15086      329672 :   dump.outdent ();
   15087      330710 :   dump () && dump ("Read section:%u", snum);
   15088             : 
   15089      329672 :   loaded_clusters++;
   15090             : 
   15091      329672 :   if (!sec.end (from ()))
   15092             :     return false;
   15093             : 
   15094             :   return true;
   15095      329672 : }
   15096             : 
   15097             : void
   15098      169279 : module_state::write_namespace (bytes_out &sec, depset *dep)
   15099             : {
   15100      169279 :   unsigned ns_num = dep->cluster;
   15101      169279 :   unsigned ns_import = 0;
   15102             : 
   15103      169279 :   if (dep->is_import ())
   15104          12 :     ns_import = dep->section;
   15105      169267 :   else if (dep->get_entity () != global_namespace)
   15106       98495 :     ns_num++;
   15107             : 
   15108      169279 :   sec.u (ns_import);
   15109      169279 :   sec.u (ns_num);
   15110      169279 : }
   15111             : 
   15112             : tree
   15113      238673 : module_state::read_namespace (bytes_in &sec)
   15114             : {
   15115      238673 :   unsigned ns_import = sec.u ();
   15116      238673 :   unsigned ns_num = sec.u ();
   15117      238673 :   tree ns = NULL_TREE;
   15118             : 
   15119      238673 :   if (ns_import || ns_num)
   15120             :     {
   15121      135787 :       if (!ns_import)
   15122      135781 :         ns_num--;
   15123             : 
   15124      135787 :       if (unsigned origin = slurp->remap_module (ns_import))
   15125             :         {
   15126      135787 :           module_state *from = (*modules)[origin];
   15127      135787 :           if (ns_num < from->entity_num)
   15128             :             {
   15129      135787 :               binding_slot &slot = (*entity_ary)[from->entity_lwm + ns_num];
   15130             : 
   15131      135787 :               if (!slot.is_lazy ())
   15132      135787 :                 ns = slot;
   15133             :             }
   15134             :         }
   15135             :       else
   15136           0 :         sec.set_overrun ();
   15137             :     }
   15138             :   else
   15139      102886 :     ns = global_namespace;
   15140             : 
   15141      238673 :   return ns;
   15142             : }
   15143             : 
   15144             : /* SPACES is a sorted vector of namespaces.  Write out the namespaces
   15145             :    to MOD_SNAME_PFX.nms section.   */
   15146             : 
   15147             : void
   15148         295 : module_state::write_namespaces (elf_out *to, vec<depset *> spaces,
   15149             :                                 unsigned num, unsigned *crc_p)
   15150             : {
   15151         328 :   dump () && dump ("Writing namespaces");
   15152         295 :   dump.indent ();
   15153             : 
   15154         295 :   bytes_out sec (to);
   15155         295 :   sec.begin ();
   15156             : 
   15157        2330 :   for (unsigned ix = 0; ix != num; ix++)
   15158             :     {
   15159        2035 :       depset *b = spaces[ix];
   15160        2035 :       tree ns = b->get_entity ();
   15161             : 
   15162        2035 :       gcc_checking_assert (TREE_CODE (ns) == NAMESPACE_DECL);
   15163             :       /* P1815 may have something to say about this.  */
   15164        2035 :       gcc_checking_assert (TREE_PUBLIC (ns));
   15165             : 
   15166        2035 :       unsigned flags = 0;
   15167        2035 :       if (TREE_PUBLIC (ns))
   15168        2035 :         flags |= 1;
   15169        2035 :       if (DECL_NAMESPACE_INLINE_P (ns))
   15170         431 :         flags |= 2;
   15171        2035 :       if (DECL_MODULE_PURVIEW_P (ns))
   15172        1770 :         flags |= 4;
   15173        2035 :       if (DECL_MODULE_EXPORT_P (ns))
   15174        1711 :         flags |= 8;
   15175             : 
   15176        2134 :       dump () && dump ("Writing namespace:%u %N%s%s%s%s",
   15177             :                        b->cluster, ns,
   15178             :                        flags & 1 ? ", public" : "", 
   15179          99 :                        flags & 2 ? ", inline" : "",
   15180          99 :                        flags & 4 ? ", purview" : "",
   15181          99 :                        flags & 8 ? ", export" : "");
   15182        2035 :       sec.u (b->cluster);
   15183        2035 :       sec.u (to->name (DECL_NAME (ns)));
   15184        2035 :       write_namespace (sec, b->deps[0]);
   15185             : 
   15186        2035 :       sec.u (flags);
   15187        2035 :       write_location (sec, DECL_SOURCE_LOCATION (ns));
   15188             :     }
   15189             : 
   15190         295 :   sec.end (to, to->name (MOD_SNAME_PFX ".nms"), crc_p);
   15191         295 :   dump.outdent ();
   15192         295 : }
   15193             : 
   15194             : /* Read the namespace hierarchy from MOD_SNAME_PFX.namespace.  Fill in
   15195             :    SPACES from that data.  */
   15196             : 
   15197             : bool
   15198         318 : module_state::read_namespaces (unsigned num)
   15199             : {
   15200         318 :   bytes_in sec;
   15201             : 
   15202         318 :   if (!sec.begin (loc, from (), MOD_SNAME_PFX ".nms"))
   15203             :     return false;
   15204             : 
   15205         363 :   dump () && dump ("Reading namespaces");
   15206         318 :   dump.indent ();
   15207             : 
   15208        2978 :   for (unsigned ix = 0; ix != num; ix++)
   15209             :     {
   15210        2660 :       unsigned entity_index = sec.u ();
   15211        2660 :       unsigned name = sec.u ();
   15212             : 
   15213        2660 :       tree parent = read_namespace (sec);
   15214             : 
   15215             :       /* See comment in write_namespace about why not bits.  */
   15216        2660 :       unsigned flags = sec.u ();
   15217        2660 :       location_t src_loc = read_location (sec);
   15218             : 
   15219        2660 :       if (entity_index >= entity_num
   15220        2660 :           || !parent
   15221        2660 :           || (flags & 0xc) == 0x8)
   15222           0 :         sec.set_overrun ();
   15223        2660 :       if (sec.get_overrun ())
   15224             :         break;
   15225             : 
   15226        5320 :       tree id = name ? get_identifier (from ()->name (name)) : NULL_TREE;
   15227             : 
   15228        2708 :       dump () && dump ("Read namespace:%u %P%s%s%s%s",
   15229             :                        entity_index, parent, id,
   15230          48 :                        flags & 1 ? ", public" : "", 
   15231          48 :                        flags & 2 ? ", inline" : "",
   15232          48 :                        flags & 4 ? ", purview" : "",
   15233          48 :                        flags & 8 ? ", export" : "");
   15234        2660 :       bool visible_p = ((flags & 8)
   15235        2660 :                         || ((flags & 1)
   15236         401 :                             && (flags & 4)
   15237          41 :                             && (is_partition () || is_module ())));
   15238        5320 :       tree inner = add_imported_namespace (parent, id, src_loc, mod,
   15239        2660 :                                            bool (flags & 2), visible_p);
   15240        2660 :       if (!inner)
   15241             :         {
   15242           0 :           sec.set_overrun ();
   15243             :           break;
   15244             :         }
   15245             : 
   15246        2660 :       if (is_partition ())
   15247             :         {
   15248          12 :           if (flags & 4)
   15249          12 :             DECL_MODULE_PURVIEW_P (inner) = true;
   15250          12 :           if (flags & 8)
   15251          12 :             DECL_MODULE_EXPORT_P (inner) = true;
   15252             :         }
   15253             : 
   15254             :       /* Install the namespace.  */
   15255        2660 :       (*entity_ary)[entity_lwm + entity_index] = inner;
   15256        2660 :       if (DECL_MODULE_IMPORT_P (inner))
   15257             :         {
   15258        1501 :           bool existed;
   15259        1501 :           unsigned *slot = &entity_map->get_or_insert
   15260        1501 :             (DECL_UID (inner), &existed);
   15261        1501 :           if (existed)
   15262             :             /* If it existed, it should match.  */
   15263          75 :             gcc_checking_assert (inner == (*entity_ary)[*slot]);
   15264             :           else
   15265        1426 :             *slot = entity_lwm + entity_index;
   15266             :         }
   15267             :     }
   15268         318 :   dump.outdent ();
   15269         318 :   if (!sec.end (from ()))
   15270             :     return false;
   15271             :   return true;
   15272         318 : }
   15273             : 
   15274             : /* Write the binding TABLE to MOD_SNAME_PFX.bnd   */
   15275             : 
   15276             : unsigned
   15277        1657 : module_state::write_bindings (elf_out *to, vec<depset *> sccs, unsigned *crc_p)
   15278             : {
   15279        1882 :   dump () && dump ("Writing binding table");
   15280        1657 :   dump.indent ();
   15281             : 
   15282        1657 :   unsigned num = 0;
   15283        1657 :   bytes_out sec (to);
   15284        1657 :   sec.begin ();
   15285             : 
   15286     2424964 :   for (unsigned ix = 0; ix != sccs.length (); ix++)
   15287             :     {
   15288     1210825 :       depset *b = sccs[ix];
   15289     1210825 :       if (b->is_binding ())
   15290             :         {
   15291      167244 :           tree ns = b->get_entity ();
   15292      168231 :           dump () && dump ("Bindings %P section:%u", ns, b->get_name (),
   15293             :                            b->section);
   15294      167244 :           sec.u (to->name (b->get_name ()));
   15295      167244 :           write_namespace (sec, b->deps[0]);
   15296      167244 :           sec.u (b->section);
   15297      167244 :           num++;
   15298             :         }
   15299             :     }
   15300             : 
   15301        1657 :   sec.end (to, to->name (MOD_SNAME_PFX ".bnd"), crc_p);
   15302        1657 :   dump.outdent ();
   15303             : 
   15304        1657 :   return num;
   15305        1657 : }
   15306             : 
   15307             : /* Read the binding table from MOD_SNAME_PFX.bind.  */
   15308             : 
   15309             : bool
   15310        1807 : module_state::read_bindings (unsigned num, unsigned lwm, unsigned hwm)
   15311             : {
   15312        1807 :   bytes_in sec;
   15313             : 
   15314        1807 :   if (!sec.begin (loc, from (), MOD_SNAME_PFX ".bnd"))
   15315             :     return false;
   15316             : 
   15317        2251 :   dump () && dump ("Reading binding table");
   15318        1807 :   dump.indent ();
   15319      237820 :   for (; !sec.get_overrun () && num--;)
   15320             :     {
   15321      236013 :       const char *name = from ()->name (sec.u ());
   15322      236013 :       tree ns = read_namespace (sec);
   15323      236013 :       unsigned snum = sec.u ();
   15324             : 
   15325      236013 :       if (!ns || !name || (snum - lwm) >= (hwm - lwm))
   15326           0 :         sec.set_overrun ();
   15327      236013 :       if (!sec.get_overrun ())
   15328             :         {
   15329      236013 :           tree id = get_identifier (name);
   15330      237385 :           dump () && dump ("Bindings %P section:%u", ns, id, snum);
   15331      236013 :           if (mod && !import_module_binding (ns, id, mod, snum))
   15332             :             break;
   15333             :         }
   15334             :     }
   15335             : 
   15336        1807 :   dump.outdent ();
   15337        1807 :   if (!sec.end (from ()))
   15338             :     return false;
   15339             :   return true;
   15340        1807 : }
   15341             : 
   15342             : /* Write the entity table to MOD_SNAME_PFX.ent
   15343             : 
   15344             :    Each entry is a section number.  */
   15345             : 
   15346             : void
   15347        1474 : module_state::write_entities (elf_out *to, vec<depset *> depsets,
   15348             :                               unsigned count, unsigned *crc_p)
   15349             : {
   15350        1663 :   dump () && dump ("Writing entities");
   15351        1474 :   dump.indent ();
   15352             : 
   15353        1474 :   bytes_out sec (to);
   15354        1474 :   sec.begin ();
   15355             : 
   15356        1474 :   unsigned current = 0;
   15357     1212293 :   for (unsigned ix = 0; ix < depsets.length (); ix++)
   15358             :     {
   15359     1210819 :       depset *d = depsets[ix];
   15360             : 
   15361     2232171 :       switch (d->get_entity_kind ())
   15362             :         {
   15363             :         default:
   15364             :           break;
   15365             : 
   15366        3503 :         case depset::EK_NAMESPACE:
   15367        3503 :           if (!d->is_import () && d->get_entity () != global_namespace)
   15368             :             {
   15369        2035 :               gcc_checking_assert (d->cluster == current);
   15370        2035 :               current++;
   15371        2035 :               sec.u (0);
   15372             :             }
   15373             :           break;
   15374             : 
   15375     1017849 :         case depset::EK_DECL:
   15376     1017849 :         case depset::EK_SPECIALIZATION:
   15377     1017849 :         case depset::EK_PARTIAL:
   15378     2035698 :           gcc_checking_assert (!d->is_unreached ()
   15379             :                                && !d->is_import ()
   15380             :                                && d->cluster == current
   15381             :                                && d->section);
   15382     1017849 :           current++;
   15383     1017849 :           sec.u (d->section);
   15384     1017849 :           break;
   15385             :         }
   15386             :     }
   15387        1474 :   gcc_assert (count == current);
   15388        1474 :   sec.end (to, to->name (MOD_SNAME_PFX ".ent"), crc_p);
   15389        1474 :   dump.outdent ();
   15390        1474 : }
   15391             : 
   15392             : bool
   15393        1618 : module_state::read_entities (unsigned count, unsigned lwm, unsigned hwm)
   15394             : {
   15395        1618 :   trees_in sec (this);
   15396             : 
   15397        1618 :   if (!sec.begin (loc, from (), MOD_SNAME_PFX ".ent"))
   15398             :     return false;
   15399             : 
   15400        2014 :   dump () && dump ("Reading entities");
   15401        1618 :   dump.indent ();
   15402             : 
   15403     1435268 :   for (binding_slot *slot = entity_ary->begin () + entity_lwm; count--; slot++)
   15404             :     {
   15405     1433650 :       unsigned snum = sec.u ();
   15406     1433650 :       if (snum && (snum - lwm) >= (hwm - lwm))
   15407           0 :         sec.set_overrun ();
   15408     1433650 :       if (sec.get_overrun ())
   15409             :         break;
   15410             : 
   15411     1433650 :       if (snum)
   15412     1430990 :         slot->set_lazy (snum << 2);
   15413             :     }
   15414             : 
   15415        1618 :   dump.outdent ();
   15416        1618 :   if (!sec.end (from ()))
   15417             :     return false;
   15418             :   return true;
   15419        1618 : }
   15420             : 
   15421             : /* Write the pending table to MOD_SNAME_PFX.pnd
   15422             : 
   15423             :    The pending table holds information about clusters that need to be
   15424             :    loaded because they contain information about something that is not
   15425             :    found by namespace-scope lookup.
   15426             : 
   15427             :    The three cases are:
   15428             : 
   15429             :    (a) Template (maybe-partial) specializations that we have
   15430             :    instantiated or defined.  When an importer needs to instantiate
   15431             :    that template, they /must have/ the partial, explicit & extern
   15432             :    specializations available.  If they have the other specializations
   15433             :    available, they'll have less work to do.  Thus, when we're about to
   15434             :    instantiate FOO, we have to be able to ask 'are there any
   15435             :    specialization of FOO in our imports?'.
   15436             : 
   15437             :    (b) (Maybe-implicit) member functions definitions.  A class could
   15438             :    be defined in one header, and an inline member defined in a
   15439             :    different header (this occurs in the STL).  Similarly, like the
   15440             :    specialization case, an implicit member function could have been
   15441             :    'instantiated' in one module, and it'd be nice to not have to
   15442             :    reinstantiate it in another.
   15443             : 
   15444             :    (c) A member classes completed elsewhere.  A member class could be
   15445             :    declared in one header and defined in another.  We need to know to
   15446             :    load the class definition before looking in it.  This turns out to
   15447             :    be a specific case of #b, so we can treat these the same.  But it
   15448             :    does highlight an issue -- there could be an intermediate import
   15449             :    between the outermost containing namespace-scope class and the
   15450             :    innermost being-defined member class.  This is actually possible
   15451             :    with all of these cases, so be aware -- we're not just talking of
   15452             :    one level of import to get to the innermost namespace.
   15453             : 
   15454             :    This gets complicated fast, it took me multiple attempts to even
   15455             :    get something remotely working.  Partially because I focussed on
   15456             :    optimizing what I think turns out to be a smaller problem, given
   15457             :    the known need to do the more general case *anyway*.  I document
   15458             :    the smaller problem, because it does appear to be the natural way
   15459             :    to do it.  It's trap!
   15460             : 
   15461             :    **** THE TRAP
   15462             : 
   15463             :    Let's refer to the primary template or the containing class as the
   15464             :    KEY.  And the specialization or member as the PENDING-ENTITY.  (To
   15465             :    avoid having to say those mouthfuls all the time.)
   15466             : 
   15467             :    In either case, we have an entity and we need some way of mapping
   15468             :    that to a set of entities that need to be loaded before we can
   15469             :    proceed with whatever processing of the entity we were going to do.
   15470             : 
   15471             :    We need to link the key to the pending-entity in some way.  Given a
   15472             :    key, tell me the pending-entities I need to have loaded.  However
   15473             :    we tie the key to the pending-entity must not rely on the key being
   15474             :    loaded -- that'd defeat the lazy loading scheme.
   15475             : 
   15476             :    As the key will be an import in we know its entity number (either
   15477             :    because we imported it, or we're writing it out too).  Thus we can
   15478             :    generate a map of key-indices to pending-entities.  The
   15479             :    pending-entity indices will be into our span of the entity table,
   15480             :    and thus allow them to be lazily loaded.  The key index will be
   15481             :    into another slot of the entity table.  Notice that this checking
   15482             :    could be expensive, we don't want to iterate over a bunch of
   15483             :    pending-entity indices (across multiple imports), every time we're
   15484             :    about do to the thing with the key.  We need to quickly determine
   15485             :    'definitely nothing needed'.
   15486             : 
   15487             :    That's almost good enough, except that key indices are not unique
   15488             :    in a couple of cases :( Specifically the Global Module or a module
   15489             :    partition can result in multiple modules assigning an entity index
   15490             :    for the key.  The decl-merging on loading will detect that so we
   15491             :    only have one Key loaded, and in the entity hash it'll indicate the
   15492             :    entity index of first load.  Which might be different to how we
   15493             :    know it.  Notice this is restricted to GM entities or this-module
   15494             :    entities.  Foreign imports cannot have this.
   15495             : 
   15496             :    We can simply resolve this in the direction of how this module
   15497             :    referred to the key to how the importer knows it.  Look in the
   15498             :    entity table slot that we nominate, maybe lazy load it, and then
   15499             :    lookup the resultant entity in the entity hash to learn how the
   15500             :    importer knows it.
   15501             : 
   15502             :    But we need to go in the other direction :( Given the key, find all
   15503             :    the index-aliases of that key.  We can partially solve that by
   15504             :    adding an alias hash table.  Whenever we load a merged decl, add or
   15505             :    augment a mapping from the entity (or its entity-index) to the
   15506             :    newly-discovered index.  Then when we look for pending entities of
   15507             :    a key, we also iterate over this aliases this mapping provides.
   15508             : 
   15509             :    But that requires the alias to be loaded.  And that's not
   15510             :    necessarily true.
   15511             : 
   15512             :    *** THE SIMPLER WAY
   15513             : 
   15514             :    The remaining fixed thing we have is the innermost namespace
   15515             :    containing the ultimate namespace-scope container of the key and
   15516             :    the name of that container (which might be the key itself).  I.e. a
   15517             :    namespace-decl/identifier/module tuple.  Let's call this the
   15518             :    top-key.  We'll discover that the module is not important here,
   15519             :    because of cross-module possibilities mentioned in case #c above.
   15520             :    We can't markup namespace-binding slots.  The best we can do is
   15521             :    mark the binding vector with 'there's something here', and have
   15522             :    another map from namespace/identifier pairs to a vector of pending
   15523             :    entity indices.
   15524             : 
   15525             :    Maintain a pending-entity map.  This is keyed by top-key, and
   15526             :    maps to a vector of pending-entity indices.  On the binding vector
   15527             :    have flags saying whether the pending-name-entity map has contents.
   15528             :    (We might want to further extend the key to be GM-vs-Partition and
   15529             :    specialization-vs-member, but let's not get ahead of ourselves.)
   15530             : 
   15531             :    For every key-like entity, find the outermost namespace-scope
   15532             :    name.  Use that to lookup in the pending-entity map and then make
   15533             :    sure the specified entities are loaded.
   15534             : 
   15535             :    An optimization might be to have a flag in each key-entity saying
   15536             :    that its top key might be in the entity table.  It's not clear to
   15537             :    me how to set that flag cheaply -- cheaper than just looking.
   15538             : 
   15539             :    FIXME: It'd be nice to have a bit in decls to tell us whether to
   15540             :    even try this.  We can have a 'already done' flag, that we set when
   15541             :    we've done KLASS's lazy pendings.  When we import a module that
   15542             :    registers pendings on the same top-key as KLASS we need to clear
   15543             :    the flag.  A recursive walk of the top-key clearing the bit will
   15544             :    suffice.  Plus we only need to recurse on classes that have the bit
   15545             :    set.  (That means we need to set the bit on parents of KLASS here,
   15546             :    don't forget.)  However, first: correctness, second: efficiency.  */
   15547             : 
   15548             : unsigned
   15549        1657 : module_state::write_pendings (elf_out *to, vec<depset *> depsets,
   15550             :                               depset::hash &table, unsigned *crc_p)
   15551             : {
   15552        1882 :   dump () && dump ("Writing pending-entities");
   15553        1657 :   dump.indent ();
   15554             : 
   15555        1657 :   trees_out sec (to, this, table);
   15556        1657 :   sec.begin ();
   15557             : 
   15558        1657 :   unsigned count = 0;
   15559        1657 :   tree cache_ns = NULL_TREE;
   15560        1657 :   tree cache_id = NULL_TREE;
   15561        1657 :   unsigned cache_section = ~0;
   15562     1212482 :   for (unsigned ix = 0; ix < depsets.length (); ix++)
   15563             :     {
   15564     1210825 :       depset *d = depsets[ix];
   15565             : 
   15566     1210825 :       if (d->is_binding ())
   15567      770654 :         continue;
   15568             : 
   15569     1043581 :       if (d->is_import ())
   15570           0 :         continue;
   15571             : 
   15572     1043581 :       if (!(d->get_entity_kind () == depset::EK_SPECIALIZATION
   15573      625093 :             || d->get_entity_kind () == depset::EK_PARTIAL
   15574      603422 :             || (d->get_entity_kind () == depset::EK_DECL && d->is_member ())))
   15575      603410 :         continue;
   15576             : 
   15577      440171 :       tree key_decl = nullptr;
   15578      440171 :       tree key_ns = find_pending_key (d->get_entity (), &key_decl);
   15579      440171 :       tree key_name = DECL_NAME (key_decl);
   15580             : 
   15581      440171 :       if (IDENTIFIER_ANON_P (key_name))
   15582             :         {
   15583           6 :           gcc_checking_assert (IDENTIFIER_LAMBDA_P (key_name));
   15584           6 :           if (tree attached = LAMBDA_TYPE_EXTRA_SCOPE (TREE_TYPE (key_decl)))
   15585           6 :             key_name = DECL_NAME (attached);
   15586             :           else
   15587             :             {
   15588             :               /* There's nothing to attach it to.  Must
   15589             :                  always reinstantiate.  */
   15590           0 :               dump ()
   15591           0 :                 && dump ("Unattached lambda %N[%u] section:%u",
   15592           0 :                          d->get_entity_kind () == depset::EK_DECL
   15593             :                          ? "Member" : "Specialization", d->get_entity (),
   15594             :                          d->cluster, d->section);
   15595           0 :               continue;
   15596             :             }
   15597             :         }
   15598             : 
   15599      440171 :       char const *also = "";
   15600      440171 :       if (d->section == cache_section
   15601      295679 :           && key_ns == cache_ns
   15602      295679 :           && key_name == cache_id)
   15603             :         /* Same section & key as previous, no need to repeat ourselves.  */
   15604             :         also = "also ";
   15605             :       else
   15606             :         {
   15607      193269 :           cache_ns = key_ns;
   15608      193269 :           cache_id = key_name;
   15609      193269 :           cache_section = d->section;
   15610      193269 :           gcc_checking_assert (table.find_dependency (cache_ns));
   15611      193269 :           sec.tree_node (cache_ns);
   15612      193269 :           sec.tree_node (cache_id);
   15613      193269 :           sec.u (d->cluster);
   15614      193269 :           count++;
   15615             :         }
   15616      460867 :       dump () && dump ("Pending %s %N entity:%u section:%u %skeyed to %P",
   15617       10348 :                        d->get_entity_kind () == depset::EK_DECL
   15618             :                        ? "member" : "specialization", d->get_entity (),
   15619             :                        d->cluster, cache_section, also, cache_ns, cache_id);
   15620             :       }
   15621        1657 :   sec.end (to, to->name (MOD_SNAME_PFX ".pnd"), crc_p);
   15622        1657 :   dump.outdent ();
   15623             : 
   15624        1657 :   return count;
   15625        1657 : }
   15626             : 
   15627             : bool
   15628         438 : module_state::read_pendings (unsigned count)
   15629             : {
   15630         438 :   trees_in sec (this);
   15631             : 
   15632         438 :   if (!sec.begin (loc, from (), MOD_SNAME_PFX ".pnd"))
   15633             :     return false;
   15634             : 
   15635         579 :   dump () && dump ("Reading %u pendings", count);
   15636         438 :   dump.indent ();
   15637             : 
   15638      268868 :   for (unsigned ix = 0; ix != count; ix++)
   15639             :     {
   15640      268430 :       pending_key key;
   15641      268430 :       unsigned index;
   15642             : 
   15643      268430 :       key.ns = sec.tree_node ();
   15644      268430 :       key.id = sec.tree_node ();
   15645      268430 :       index = sec.u ();
   15646             : 
   15647      268430 :       if (!key.ns || !key.id
   15648      268430 :           || !(TREE_CODE (key.ns) == NAMESPACE_DECL
   15649      268430 :                && !DECL_NAMESPACE_ALIAS (key.ns))
   15650      268430 :           || !identifier_p (key.id)
   15651      536860 :           || index >= entity_num)
   15652           0 :         sec.set_overrun ();
   15653             : 
   15654      268430 :       if (sec.get_overrun ())
   15655             :         break;
   15656             : 
   15657      268765 :       dump () && dump ("Pending:%u keyed to %P", index, key.ns, key.id);
   15658             : 
   15659      268430 :       index += entity_lwm;
   15660      268430 :       auto &vec = pending_table->get_or_insert (key);
   15661      268430 :       vec.safe_push (index);
   15662             :     }
   15663             : 
   15664         438 :   dump.outdent ();
   15665         438 :   if (!sec.end (from ()))
   15666             :     return false;
   15667             :   return true;
   15668         438 : }
   15669             : 
   15670             : /* Read & write locations.  */
   15671             : enum loc_kind {
   15672             :   LK_ORDINARY,
   15673             :   LK_MACRO,
   15674             :   LK_IMPORT_ORDINARY,
   15675             :   LK_IMPORT_MACRO,
   15676             :   LK_ADHOC,
   15677             :   LK_RESERVED,
   15678             : };
   15679             : 
   15680             : static const module_state *
   15681        4201 : module_for_ordinary_loc (location_t loc)
   15682             : {
   15683        4201 :   unsigned pos = 0;
   15684        8402 :   unsigned len = ool->length () - pos;
   15685             : 
   15686        4202 :   while (len)
   15687             :     {
   15688        4202 :       unsigned half = len / 2;
   15689        4202 :       module_state *probe = (*ool)[pos + half];
   15690        4202 :       if (loc < probe->ordinary_locs.first)
   15691             :         len = half;
   15692        4201 :       else if (loc < probe->ordinary_locs.first + probe->ordinary_locs.second)
   15693        4201 :         return probe;
   15694             :       else
   15695             :         {
   15696           0 :           pos += half + 1;
   15697           0 :           len = len - (half + 1);
   15698             :         }
   15699             :     }
   15700             : 
   15701             :   return nullptr;
   15702             : }
   15703             : 
   15704             : static const module_state *
   15705           6 : module_for_macro_loc (location_t loc)
   15706             : {
   15707           6 :   unsigned pos = 1;
   15708           6 :   unsigned len = modules->length () - pos;
   15709             : 
   15710           6 :   while (len)
   15711             :     {
   15712           6 :       unsigned half = len / 2;
   15713           6 :       module_state *probe = (*modules)[pos + half];
   15714           6 :       if (loc < probe->macro_locs.first)
   15715             :         {
   15716           0 :           pos += half + 1;
   15717           0 :           len = len - (half + 1);
   15718             :         }
   15719           6 :       else if (loc >= probe->macro_locs.first + probe->macro_locs.second)
   15720             :         len = half;
   15721             :       else
   15722           6 :         return probe;
   15723             :     }
   15724             : 
   15725             :   return NULL;
   15726             : }
   15727             : 
   15728             : location_t
   15729         806 : module_state::imported_from () const
   15730             : {
   15731         806 :   location_t from = loc;
   15732         806 :   line_map_ordinary const *fmap
   15733         806 :     = linemap_check_ordinary (linemap_lookup (line_table, from));
   15734             : 
   15735         806 :   if (MAP_MODULE_P (fmap))
   15736         806 :     from = linemap_included_from (fmap);
   15737             : 
   15738         806 :   return from;
   15739             : }
   15740             : 
   15741             : /* Note that LOC will need writing.  This allows us to prune locations
   15742             :    that are not needed.  */
   15743             : 
   15744             : bool
   15745    20463635 : module_state::note_location (location_t loc)
   15746             : {
   15747    20463635 :   bool added = false;
   15748    20463635 :   if (!macro_loc_table && !ord_loc_table)
   15749             :     ;
   15750    20463635 :   else if (loc < RESERVED_LOCATION_COUNT)
   15751             :     ;
   15752    18849276 :   else if (IS_ADHOC_LOC (loc))
   15753             :     {
   15754     2285288 :       location_t locus = get_location_from_adhoc_loc (line_table, loc);
   15755     2285288 :       note_location (locus);
   15756     2285288 :       source_range range = get_range_from_loc (line_table, loc);
   15757     2285288 :       if (range.m_start != locus)
   15758     2059156 :         note_location (range.m_start);
   15759     2285288 :       note_location (range.m_finish);
   15760             :     }
   15761    33127895 :   else if (loc >= LINEMAPS_MACRO_LOWEST_LOCATION (line_table))
   15762             :     {
   15763     1554114 :       if (spans.macro (loc))
   15764             :         {
   15765     1554108 :           const line_map *map = linemap_lookup (line_table, loc);
   15766     1554108 :           const line_map_macro *mac_map = linemap_check_macro (map);
   15767     1554108 :           hashval_t hv = macro_loc_traits::hash (mac_map);
   15768     1554108 :           macro_loc_info *slot
   15769     1554108 :             = macro_loc_table->find_slot_with_hash (mac_map, hv, INSERT);
   15770     1554108 :           if (!slot->src)
   15771             :             {
   15772      184777 :               slot->src = mac_map;
   15773      184777 :               slot->remap = 0;
   15774             :               // Expansion locations could themselves be from a
   15775             :               // macro, we need to note them all.
   15776      184777 :               note_location (mac_map->expansion);
   15777      184777 :               gcc_checking_assert (mac_map->n_tokens);
   15778      184777 :               location_t tloc = UNKNOWN_LOCATION;
   15779     6755281 :               for (unsigned ix = mac_map->n_tokens * 2; ix--;)
   15780     6570504 :                 if (mac_map->macro_locations[ix] != tloc)
   15781             :                   {
   15782     3498129 :                     tloc = mac_map->macro_locations[ix];
   15783     3498129 :                     note_location (tloc);
   15784             :                   }
   15785             :               added = true;
   15786             :             }
   15787             :         }                                      
   15788             :     }
   15789    15009874 :   else if (IS_ORDINARY_LOC (loc))
   15790             :     {
   15791    15009874 :       if (spans.ordinary (loc))
   15792             :         {
   15793    15005675 :           const line_map *map = linemap_lookup (line_table, loc);
   15794    15005675 :           const line_map_ordinary *ord_map = linemap_check_ordinary (map);
   15795    15005675 :           ord_loc_info lkup;
   15796    15005675 :           lkup.src = ord_map;
   15797    15005675 :           lkup.span = 1 << ord_map->m_column_and_range_bits;
   15798    15005675 :           lkup.offset = (loc - MAP_START_LOCATION (ord_map)) & ~(lkup.span - 1);
   15799    15005675 :           lkup.remap = 0;
   15800    15005675 :           ord_loc_info *slot = (ord_loc_table->find_slot_with_hash
   15801    15005675 :                                 (lkup, ord_loc_traits::hash (lkup), INSERT));
   15802    15005675 :           if (!slot->src)
   15803             :             {
   15804     1834845 :               *slot = lkup;
   15805     1834845 :               added = true;
   15806             :             }
   15807             :         }
   15808             :     }
   15809             :   else
   15810           0 :     gcc_unreachable ();
   15811    20463635 :   return added;
   15812             : }
   15813             : 
   15814             : /* If we're not streaming, record that we need location LOC.
   15815             :    Otherwise stream it.  */
   15816             : 
   15817             : void
   15818    30023716 : module_state::write_location (bytes_out &sec, location_t loc)
   15819             : {
   15820    30023716 :   if (!sec.streaming_p ())
   15821             :     {
   15822     9803345 :       note_location (loc);
   15823     9803345 :       return;
   15824             :     }
   15825             : 
   15826    20220371 :   if (loc < RESERVED_LOCATION_COUNT)
   15827             :     {
   15828     1822501 :       dump (dumper::LOCATION) && dump ("Reserved location %u", unsigned (loc));
   15829     1822483 :       sec.u (LK_RESERVED + loc);
   15830             :     }
   15831    18397888 :   else if (IS_ADHOC_LOC (loc))
   15832             :     {
   15833     2304629 :       dump (dumper::LOCATION) && dump ("Adhoc location");
   15834     2304626 :       sec.u (LK_ADHOC);
   15835     2304626 :       location_t locus = get_location_from_adhoc_loc (line_table, loc);
   15836     2304626 :       write_location (sec, locus);
   15837     2304626 :       source_range range = get_range_from_loc (line_table, loc);
   15838     2304626 :       if (range.m_start == locus)
   15839             :         /* Compress.  */
   15840      219834 :         range.m_start = UNKNOWN_LOCATION;
   15841     2304626 :       write_location (sec, range.m_start);
   15842     2304626 :       write_location (sec, range.m_finish);
   15843     2304626 :       unsigned discriminator = get_discriminator_from_adhoc_loc (line_table, loc);
   15844     2304626 :       sec.u (discriminator);
   15845             :     }
   15846    32186443 :   else if (loc >= LINEMAPS_MACRO_LOWEST_LOCATION (line_table))
   15847             :     {
   15848     1540158 :       const macro_loc_info *info = nullptr;
   15849     1540158 :       unsigned offset = 0;
   15850     1540158 :       if (unsigned hwm = macro_loc_remap->length ())
   15851             :         {
   15852     1540152 :           info = macro_loc_remap->begin ();
   15853    22956149 :           while (hwm != 1)
   15854             :             {
   15855    19875845 :               unsigned mid = hwm / 2;
   15856    19875845 :               if (MAP_START_LOCATION (info[mid].src) <= loc)
   15857             :                 {
   15858    10110476 :                   info += mid;
   15859    10110476 :                   hwm -= mid;
   15860             :                 }
   15861             :               else
   15862             :                 hwm = mid;
   15863             :             }
   15864     1540152 :           offset = loc - MAP_START_LOCATION (info->src);
   15865     1540152 :           if (offset > info->src->n_tokens)
   15866           0 :             info = nullptr;
   15867             :         }
   15868             : 
   15869     1540158 :       gcc_checking_assert (bool (info) == bool (spans.macro (loc)));
   15870             : 
   15871     1540158 :       if (info)
   15872             :         {
   15873     1540152 :           offset += info->remap;
   15874     1540152 :           sec.u (LK_MACRO);
   15875     1540152 :           sec.u (offset);
   15876     1540152 :           dump (dumper::LOCATION)
   15877           9 :             && dump ("Macro location %u output %u", loc, offset);
   15878             :         }
   15879           6 :       else if (const module_state *import = module_for_macro_loc (loc))
   15880             :         {
   15881           6 :           unsigned off = loc - import->macro_locs.first;
   15882           6 :           sec.u (LK_IMPORT_MACRO);
   15883           6 :           sec.u (import->remap);
   15884           6 :           sec.u (off);
   15885           6 :           dump (dumper::LOCATION)
   15886           0 :             && dump ("Imported macro location %u output %u:%u",
   15887           0 :                      loc, import->remap, off);
   15888             :         }
   15889             :       else
   15890           0 :         gcc_unreachable ();
   15891             :     }
   15892    14553104 :   else if (IS_ORDINARY_LOC (loc))
   15893             :     {
   15894    14553104 :       const ord_loc_info *info = nullptr;
   15895    14553104 :       unsigned offset = 0;
   15896    14553104 :       if (unsigned hwm = ord_loc_remap->length ())
   15897             :         {
   15898    14553104 :           info = ord_loc_remap->begin ();
   15899   209487478 :           while (hwm != 1)
   15900             :             {
   15901   180381270 :               unsigned mid = hwm / 2;
   15902   180381270 :               if (MAP_START_LOCATION (info[mid].src) + info[mid].offset <= loc)
   15903             :                 {
   15904    94002216 :                   info += mid;
   15905    94002216 :                   hwm -= mid;
   15906             :                 }
   15907             :               else
   15908             :                 hwm = mid;
   15909             :             }
   15910    14553104 :           offset = loc - MAP_START_LOCATION (info->src) - info->offset;
   15911    14553104 :           if (offset > info->span)
   15912        4201 :             info = nullptr;
   15913             :         }
   15914             : 
   15915    14553104 :       gcc_checking_assert (bool (info) == bool (spans.ordinary (loc)));
   15916             : 
   15917    14553104 :       if (info)
   15918             :         {
   15919    14548903 :           offset += info->remap;
   15920    14548903 :           sec.u (LK_ORDINARY);
   15921    14548903 :           sec.u (offset);
   15922             : 
   15923    14548903 :           dump (dumper::LOCATION)
   15924          78 :             && dump ("Ordinary location %u output %u", loc, offset);
   15925             :         }
   15926        4201 :       else if (const module_state *import = module_for_ordinary_loc (loc))
   15927             :         {
   15928        4201 :           unsigned off = loc - import->ordinary_locs.first;
   15929        4201 :           sec.u (LK_IMPORT_ORDINARY);
   15930        4201 :           sec.u (import->remap);
   15931        4201 :           sec.u (off);
   15932        4201 :           dump (dumper::LOCATION)
   15933           0 :             && dump ("Imported ordinary location %u output %u:%u",
   15934           0 :                      import->remap, import->remap, off);
   15935             :         }
   15936             :       else
   15937           0 :         gcc_unreachable ();
   15938             :     }
   15939             :   else
   15940           0 :     gcc_unreachable ();
   15941             : }
   15942             : 
   15943             : location_t
   15944    24583544 : module_state::read_location (bytes_in &sec) const
   15945             : {
   15946    24583544 :   location_t locus = UNKNOWN_LOCATION;
   15947    24583544 :   unsigned kind = sec.u ();
   15948    24583544 :   switch (kind)
   15949             :      {
   15950     2182972 :     default:
   15951     2182972 :       {
   15952     2182972 :         if (kind < LK_RESERVED + RESERVED_LOCATION_COUNT)
   15953     2182972 :           locus = location_t (kind - LK_RESERVED);
   15954             :         else
   15955           0 :           sec.set_overrun ();
   15956     2182972 :         dump (dumper::LOCATION)
   15957           0 :           && dump ("Reserved location %u", unsigned (locus));
   15958             :       }
   15959             :       break;
   15960             : 
   15961     2749268 :      case LK_ADHOC:
   15962     2749268 :       {
   15963     2749268 :         dump (dumper::LOCATION) && dump ("Adhoc location");
   15964     2749268 :         locus = read_location (sec);
   15965     2749268 :         source_range range;
   15966     2749268 :         range.m_start = read_location (sec);
   15967     2749268 :         if (range.m_start == UNKNOWN_LOCATION)
   15968      245874 :           range.m_start = locus;
   15969     2749268 :         range.m_finish = read_location (sec);
   15970     2749268 :         unsigned discriminator = sec.u ();
   15971     2749268 :         if (locus != loc && range.m_start != loc && range.m_finish != loc)
   15972     2454317 :           locus = get_combined_adhoc_loc (line_table, locus, range, NULL, discriminator);
   15973             :       }
   15974             :       break;
   15975             : 
   15976     2170038 :     case LK_MACRO:
   15977     2170038 :       {
   15978     2170038 :         unsigned off = sec.u ();
   15979             : 
   15980     2170038 :         if (macro_locs.second)
   15981             :           {
   15982     2073783 :             if (off < macro_locs.second)
   15983     2073783 :               locus = off + macro_locs.first;
   15984             :             else
   15985           0 :               sec.set_overrun ();
   15986             :           }
   15987             :         else
   15988       96255 :           locus = loc;
   15989     2170038 :         dump (dumper::LOCATION)
   15990           0 :           && dump ("Macro %u becoming %u", off, locus);
   15991             :       }
   15992             :       break;
   15993             : 
   15994    17480726 :     case LK_ORDINARY:
   15995    17480726 :       {
   15996    17480726 :         unsigned off = sec.u ();
   15997    17480726 :         if (ordinary_locs.second)
   15998             :           {
   15999    16025034 :             if (off < ordinary_locs.second)
   16000    16025034 :               locus = off + ordinary_locs.first;
   16001             :             else
   16002           0 :               sec.set_overrun ();
   16003             :           }
   16004             :         else
   16005     1455692 :           locus = loc;
   16006             : 
   16007    17480726 :         dump (dumper::LOCATION)
   16008           0 :           && dump ("Ordinary location %u becoming %u", off, locus);
   16009             :       }
   16010             :       break;
   16011             : 
   16012         540 :      case LK_IMPORT_MACRO:
   16013         540 :      case LK_IMPORT_ORDINARY:
   16014         540 :        {
   16015         540 :          unsigned mod = sec.u ();
   16016         540 :          unsigned off = sec.u ();
   16017         540 :          const module_state *import = NULL;
   16018             : 
   16019         540 :          if (!mod && !slurp->remap)
   16020             :            /* This is an early read of a partition location during the
   16021             :               read of our ordinary location map.  */
   16022             :            import = this;
   16023             :          else
   16024             :            {
   16025         540 :              mod = slurp->remap_module (mod);
   16026         540 :              if (!mod)
   16027        1080 :                sec.set_overrun ();
   16028             :              else
   16029         540 :                import = (*modules)[mod];
   16030             :            }
   16031             : 
   16032         540 :          if (import)
   16033             :            {
   16034         540 :              if (kind == LK_IMPORT_MACRO)
   16035             :                {
   16036           6 :                  if (!import->macro_locs.second)
   16037           0 :                    locus = import->loc;
   16038           6 :                  else if (off < import->macro_locs.second)
   16039           6 :                    locus = off + import->macro_locs.first;
   16040             :                  else
   16041           0 :                    sec.set_overrun ();
   16042             :                }
   16043             :              else
   16044             :                {
   16045         534 :                  if (!import->ordinary_locs.second)
   16046           0 :                    locus = import->loc;
   16047         534 :                  else if (off < import->ordinary_locs.second)
   16048         534 :                    locus = import->ordinary_locs.first + off;
   16049             :                  else
   16050           0 :                    sec.set_overrun ();
   16051             :                }
   16052             :            }
   16053             :        }
   16054             :        break;
   16055             :     }
   16056             : 
   16057    24583544 :   return locus;
   16058             : }
   16059             : 
   16060             : /* Allocate hash tables to record needed locations.  */
   16061             : 
   16062             : void
   16063        1660 : module_state::write_init_maps ()
   16064             : {
   16065        1660 :   macro_loc_table = new hash_table<macro_loc_traits> (EXPERIMENT (1, 400));
   16066        1660 :   ord_loc_table = new hash_table<ord_loc_traits> (EXPERIMENT (1, 400));
   16067        1660 : }
   16068             : 
   16069             : /* Prepare the span adjustments.  We prune unneeded locations -- at
   16070             :    this point every needed location must have been seen by
   16071             :    note_location.  */
   16072             : 
   16073             : range_t
   16074        1657 : module_state::write_prepare_maps (module_state_config *cfg, bool has_partitions)
   16075             : {
   16076        1882 :   dump () && dump ("Preparing locations");
   16077        1657 :   dump.indent ();
   16078             : 
   16079        1882 :   dump () && dump ("Reserved locations [%u,%u) macro [%u,%u)",
   16080         225 :                    spans[loc_spans::SPAN_RESERVED].ordinary.first,
   16081         225 :                    spans[loc_spans::SPAN_RESERVED].ordinary.second,
   16082         225 :                    spans[loc_spans::SPAN_RESERVED].macro.first,
   16083         225 :                    spans[loc_spans::SPAN_RESERVED].macro.second);
   16084             : 
   16085        1657 :   range_t info {0, 0};
   16086             : 
   16087             :   // Sort the noted lines.
   16088        1657 :   vec_alloc (ord_loc_remap, ord_loc_table->size ());
   16089        1657 :   for (auto iter = ord_loc_table->begin (), end = ord_loc_table->end ();
   16090     1824204 :        iter != end; ++iter)
   16091     1822547 :     ord_loc_remap->quick_push (*iter);
   16092        1657 :   ord_loc_remap->qsort (&ord_loc_info::compare);
   16093             : 
   16094             :   // Note included-from maps.
   16095        1657 :   bool added = false;
   16096        1657 :   const line_map_ordinary *current = nullptr;
   16097     1827518 :   for (auto iter = ord_loc_remap->begin (), end = ord_loc_remap->end ();
   16098     1824204 :        iter != end; ++iter)
   16099     1822547 :     if (iter->src != current)
   16100             :       {
   16101       32450 :         current = iter->src;
   16102       24560 :         for (auto probe = current;
   16103       32450 :              auto from = linemap_included_from (probe);
   16104       12280 :              probe = linemap_check_ordinary (linemap_lookup (line_table, from)))
   16105             :           {
   16106       30555 :             if (has_partitions)
   16107             :               {
   16108             :                 // Partition locations need to elide their module map
   16109             :                 // entry.
   16110          81 :                 probe
   16111          81 :                   = linemap_check_ordinary (linemap_lookup (line_table, from));
   16112          81 :                 if (MAP_MODULE_P (probe))
   16113          69 :                   from = linemap_included_from (probe);
   16114             :               }
   16115             : 
   16116       30555 :             if (!note_location (from))
   16117             :               break;
   16118       12280 :             added = true;
   16119       12280 :           }
   16120             :       }
   16121        1657 :   if (added)
   16122             :     {
   16123             :       // Reconstruct the line array as we added items to the hash table.
   16124         347 :       vec_free (ord_loc_remap);
   16125         347 :       vec_alloc (ord_loc_remap, ord_loc_table->size ());
   16126         347 :       for (auto iter = ord_loc_table->begin (), end = ord_loc_table->end ();
   16127     1829935 :            iter != end; ++iter)
   16128     1829588 :         ord_loc_remap->quick_push (*iter);
   16129         347 :       ord_loc_remap->qsort (&ord_loc_info::compare);
   16130             :     }
   16131        1657 :   delete ord_loc_table;
   16132        1657 :   ord_loc_table = nullptr;
   16133             : 
   16134             :   // Merge (sufficiently) adjacent spans, and calculate remapping.
   16135        1657 :   constexpr unsigned adjacency = 2; // Allow 2 missing lines.
   16136        3314 :   auto begin = ord_loc_remap->begin (), end = ord_loc_remap->end ();
   16137        1657 :   auto dst = begin;
   16138        1657 :   unsigned offset = 0, range_bits = 0;
   16139        1657 :   ord_loc_info *base = nullptr;
   16140     1836484 :   for (auto iter = begin; iter != end; ++iter)
   16141             :     {    
   16142     1834827 :       if (base && iter->src == base->src)
   16143             :         {
   16144     3400445 :           if (base->offset + base->span +
   16145     1805852 :               ((adjacency << base->src->m_column_and_range_bits)
   16146             :                // If there are few c&r bits, allow further separation.
   16147     1805852 :                | (adjacency << 4))
   16148     1805852 :               >= iter->offset)
   16149             :             {
   16150             :               // Merge.
   16151     1594593 :               offset -= base->span;
   16152     1594593 :               base->span = iter->offset + iter->span - base->offset;
   16153     1594593 :               offset += base->span;
   16154     1594593 :               continue;
   16155             :             }
   16156             :         }
   16157       28975 :       else if (range_bits < iter->src->m_range_bits)
   16158        1597 :         range_bits = iter->src->m_range_bits;
   16159             : 
   16160      240234 :       offset += ((1u << iter->src->m_range_bits) - 1);
   16161      240234 :       offset &= ~((1u << iter->src->m_range_bits) - 1);
   16162      240234 :       iter->remap = offset;
   16163      240234 :       offset += iter->span;
   16164      240234 :       base = dst;
   16165      240234 :       *dst++ = *iter;
   16166             :     }
   16167        1657 :   ord_loc_remap->truncate (dst - begin);
   16168             : 
   16169        1657 :   info.first = ord_loc_remap->length ();
   16170        1657 :   cfg->ordinary_locs = offset;
   16171        1657 :   cfg->loc_range_bits = range_bits;
   16172        1882 :   dump () && dump ("Ordinary maps:%u locs:%u range_bits:%u",
   16173             :                    info.first, cfg->ordinary_locs,
   16174             :                    cfg->loc_range_bits);
   16175             : 
   16176             :   // Remap the macro locations.
   16177        1657 :   vec_alloc (macro_loc_remap, macro_loc_table->size ());
   16178        1657 :   for (auto iter = macro_loc_table->begin (), end = macro_loc_table->end ();
   16179      186434 :        iter != end; ++iter)
   16180      184777 :     macro_loc_remap->quick_push (*iter);
   16181        1657 :   delete macro_loc_table;
   16182        1657 :   macro_loc_table = nullptr;
   16183             : 
   16184        1657 :   macro_loc_remap->qsort (&macro_loc_info::compare);
   16185        1657 :   offset = 0;
   16186        4971 :   for (auto iter = macro_loc_remap->begin (), end = macro_loc_remap->end ();
   16187      186434 :        iter != end; ++iter)
   16188             :     {
   16189      184777 :       auto mac = iter->src;
   16190      184777 :       iter->remap = offset;
   16191      184777 :       offset += mac->n_tokens;
   16192             :     }
   16193        1657 :   info.second = macro_loc_remap->length ();
   16194        1657 :   cfg->macro_locs = offset;
   16195             : 
   16196        1882 :   dump () && dump ("Macro maps:%u locs:%u", info.second, cfg->macro_locs);
   16197             : 
   16198        1657 :   dump.outdent ();
   16199             : 
   16200             :   // If we have no ordinary locs, we must also have no macro locs.
   16201        1657 :   gcc_checking_assert (cfg->ordinary_locs || !cfg->macro_locs);
   16202             : 
   16203        1657 :   return info;
   16204             : }
   16205             : 
   16206             : bool
   16207        1849 : module_state::read_prepare_maps (const module_state_config *cfg)
   16208             : {
   16209        1849 :   location_t ordinary = line_table->highest_location + 1;
   16210        1849 :   ordinary += cfg->ordinary_locs;
   16211             : 
   16212        1849 :   location_t macro = LINEMAPS_MACRO_LOWEST_LOCATION (line_table);
   16213        1849 :   macro -= cfg->macro_locs;
   16214             : 
   16215        1849 :   if (ordinary < LINE_MAP_MAX_LOCATION_WITH_COLS
   16216        1849 :       && macro >= LINE_MAP_MAX_LOCATION)
   16217             :     /* OK, we have enough locations.  */
   16218             :     return true;
   16219             : 
   16220           2 :   ordinary_locs.first = ordinary_locs.second = 0;
   16221           2 :   macro_locs.first = macro_locs.second = 0;
   16222             : 
   16223           2 :   static bool informed = false;
   16224           2 :   if (!informed)
   16225             :     {
   16226             :       /* Just give the notice once.  */
   16227           2 :       informed = true;
   16228           2 :       inform (loc, "unable to represent further imported source locations");
   16229             :     }
   16230             : 
   16231             :   return false;
   16232             : }
   16233             : 
   16234             : /* Write & read the location maps. Not called if there are no
   16235             :    locations.   */
   16236             : 
   16237             : void
   16238        1597 : module_state::write_ordinary_maps (elf_out *to, range_t &info,
   16239             :                                    bool has_partitions, unsigned *crc_p)
   16240             : {
   16241        1810 :   dump () && dump ("Writing ordinary location maps");
   16242        1597 :   dump.indent ();
   16243             : 
   16244        1597 :   vec<const char *> filenames;
   16245        1597 :   filenames.create (20);
   16246             : 
   16247             :   /* Determine the unique filenames.  */
   16248        1597 :   const line_map_ordinary *current = nullptr;
   16249      245025 :   for (auto iter = ord_loc_remap->begin (), end = ord_loc_remap->end ();
   16250      241831 :        iter != end; ++iter)
   16251      240234 :     if (iter->src != current)
   16252             :       {
   16253       28975 :         current = iter->src;
   16254       28975 :         const char *fname = ORDINARY_MAP_FILE_NAME (iter->src);
   16255             : 
   16256             :         /* We should never find a module linemap in an interval.  */
   16257       28975 :         gcc_checking_assert (!MAP_MODULE_P (iter->src));
   16258             : 
   16259             :         /* We expect very few filenames, so just an array.
   16260             :            (Not true when headers are still in play :()  */
   16261     1632301 :         for (unsigned jx = filenames.length (); jx--;)
   16262             :           {
   16263     1589861 :             const char *name = filenames[jx];
   16264     1589861 :             if (0 == strcmp (name, fname))
   16265             :               {
   16266             :                 /* Reset the linemap's name, because for things like
   16267             :                    preprocessed input we could have multiple instances
   16268             :                    of the same name, and we'd rather not percolate
   16269             :                    that.  */
   16270       15510 :                 const_cast<line_map_ordinary *> (iter->src)->to_file = name;
   16271       15510 :                 fname = NULL;
   16272       15510 :                 break;
   16273             :               }
   16274             :           }
   16275       28975 :         if (fname)
   16276       13465 :           filenames.safe_push (fname);
   16277             :       }
   16278             : 
   16279        1597 :   bytes_out sec (to);
   16280        1597 :   sec.begin ();
   16281             : 
   16282             :   /* Write the filenames.  */
   16283        1597 :   unsigned len = filenames.length ();
   16284        1597 :   sec.u (len);
   16285        1810 :   dump () && dump ("%u source file names", len);
   16286       15062 :   for (unsigned ix = 0; ix != len; ix++)
   16287             :     {
   16288       13465 :       const char *fname = filenames[ix];
   16289       13480 :       dump (dumper::LOCATION) && dump ("Source file[%u]=%s", ix, fname);
   16290       13465 :       sec.str (fname);
   16291             :     }
   16292             : 
   16293        1597 :   sec.u (info.first);   /* Num maps.  */
   16294        1597 :   const ord_loc_info *base = nullptr;
   16295      245025 :   for (auto iter = ord_loc_remap->begin (), end = ord_loc_remap->end ();
   16296      241831 :        iter != end; ++iter)
   16297             :     {
   16298      240234 :       dump (dumper::LOCATION)
   16299          36 :         && dump ("Span:%u ordinary [%u+%u,+%u)->[%u,+%u)",
   16300          36 :                  iter - ord_loc_remap->begin (),
   16301          18 :                  MAP_START_LOCATION (iter->src), iter->offset, iter->span,
   16302             :                  iter->remap, iter->span);
   16303             : 
   16304      240234 :       if (!base || iter->src != base->src)
   16305       28975 :         base = iter;
   16306      240234 :       sec.u (iter->offset - base->offset);
   16307      240234 :       if (base == iter)
   16308             :         {
   16309       28975 :           sec.u (iter->src->sysp);
   16310       28975 :           sec.u (iter->src->m_range_bits);
   16311       28975 :           sec.u (iter->src->m_column_and_range_bits - iter->src->m_range_bits);
   16312             : 
   16313       28975 :           const char *fname = ORDINARY_MAP_FILE_NAME (iter->src);
   16314     6249774 :           for (unsigned ix = 0; ix != filenames.length (); ix++)
   16315     3124887 :             if (filenames[ix] == fname)
   16316             :               {
   16317       28975 :                 sec.u (ix);
   16318       28975 :                 break;
   16319             :               }
   16320       28975 :           unsigned line = ORDINARY_MAP_STARTING_LINE_NUMBER (iter->src);
   16321       28975 :           line += iter->offset >> iter->src->m_column_and_range_bits;
   16322       28975 :           sec.u (line);
   16323             :         }
   16324      240234 :       sec.u (iter->remap);
   16325      240234 :       if (base == iter)
   16326             :         {
   16327             :           /* Write the included from location, which means reading it
   16328             :              while reading in the ordinary maps.  So we'd better not
   16329             :              be getting ahead of ourselves.  */
   16330       28975 :           location_t from = linemap_included_from (iter->src);
   16331       28975 :           gcc_checking_assert (from < MAP_START_LOCATION (iter->src));
   16332       28975 :           if (from != UNKNOWN_LOCATION && has_partitions)
   16333             :             {
   16334             :               /* A partition's span will have a from pointing at a
   16335             :                  MODULE_INC.  Find that map's from.  */
   16336          78 :               line_map_ordinary const *fmap
   16337          78 :                 = linemap_check_ordinary (linemap_lookup (line_table, from));
   16338          78 :               if (MAP_MODULE_P (fmap))
   16339          66 :                 from = linemap_included_from (fmap);
   16340             :             }
   16341       28975 :           write_location (sec, from);
   16342             :         }
   16343             :     }
   16344             : 
   16345        1597 :   filenames.release ();
   16346             : 
   16347        1597 :   sec.end (to, to->name (MOD_SNAME_PFX ".olm"), crc_p);
   16348        1597 :   dump.outdent ();
   16349        1597 : }
   16350             : 
   16351             : void
   16352         113 : module_state::write_macro_maps (elf_out *to, range_t &info, unsigned *crc_p)
   16353             : {
   16354         128 :   dump () && dump ("Writing macro location maps");
   16355         113 :   dump.indent ();
   16356             : 
   16357         113 :   bytes_out sec (to);
   16358         113 :   sec.begin ();
   16359             : 
   16360         128 :   dump () && dump ("Macro maps:%u", info.second);
   16361         113 :   sec.u (info.second);
   16362             : 
   16363         113 :   unsigned macro_num = 0;
   16364         339 :   for (auto iter = macro_loc_remap->end (), begin = macro_loc_remap->begin ();
   16365      184890 :        iter-- != begin;)
   16366             :     {
   16367      184777 :       auto mac = iter->src;
   16368      184777 :       sec.u (iter->remap);
   16369      184777 :       sec.u (mac->n_tokens);
   16370      184777 :       sec.cpp_node (mac->macro);
   16371      184777 :       write_location (sec, mac->expansion);
   16372      184777 :       const location_t *locs = mac->macro_locations;
   16373             :       /* There are lots of identical runs.  */
   16374      184777 :       location_t prev = UNKNOWN_LOCATION;
   16375      184777 :       unsigned count = 0;
   16376      184777 :       unsigned runs = 0;
   16377     6755281 :       for (unsigned jx = mac->n_tokens * 2; jx--;)
   16378             :         {
   16379     6570504 :           location_t tok_loc = locs[jx];
   16380     6570504 :           if (tok_loc == prev)
   16381             :             {
   16382     3072375 :               count++;
   16383     3072375 :               continue;
   16384             :             }
   16385     3498129 :           runs++;
   16386     3498129 :           sec.u (count);
   16387     3498129 :           count = 1;
   16388     3498129 :           prev = tok_loc;
   16389     3498129 :           write_location (sec, tok_loc);
   16390             :         }
   16391      184777 :       sec.u (count);
   16392      184777 :       dump (dumper::LOCATION)
   16393           9 :         && dump ("Macro:%u %I %u/%u*2 locations [%u,%u)->%u",
   16394           9 :                  macro_num, identifier (mac->macro),
   16395             :                  runs, mac->n_tokens,
   16396             :                  MAP_START_LOCATION (mac),
   16397           9 :                  MAP_START_LOCATION (mac) + mac->n_tokens,
   16398             :                  iter->remap);
   16399      184777 :       macro_num++;
   16400             :     }
   16401         113 :   gcc_assert (macro_num == info.second);
   16402             : 
   16403         113 :   sec.end (to, to->name (MOD_SNAME_PFX ".mlm"), crc_p);
   16404         113 :   dump.outdent ();
   16405         113 : }
   16406             : 
   16407             : bool
   16408        1787 : module_state::read_ordinary_maps (unsigned num_ord_locs, unsigned range_bits)
   16409             : {
   16410        1787 :   bytes_in sec;
   16411             : 
   16412        1787 :   if (!sec.begin (loc, from (), MOD_SNAME_PFX ".olm"))
   16413             :     return false;
   16414        2219 :   dump () && dump ("Reading ordinary location maps");
   16415        1787 :   dump.indent ();
   16416             : 
   16417             :   /* Read the filename table.  */
   16418        1787 :   unsigned len = sec.u ();
   16419        2219 :   dump () && dump ("%u source file names", len);
   16420        1787 :   vec<const char *> filenames;
   16421        1787 :   filenames.create (len);
   16422       19003 :   for (unsigned ix = 0; ix != len; ix++)
   16423             :     {
   16424       17216 :       size_t l;
   16425       17216 :       const char *buf = sec.str (&l);
   16426       17216 :       char *fname = XNEWVEC (char, l + 1);
   16427       17216 :       memcpy (fname, buf, l + 1);
   16428       17216 :       dump (dumper::LOCATION) && dump ("Source file[%u]=%s", ix, fname);
   16429             :       /* We leak these names into the line-map table.  But it
   16430             :          doesn't own them.  */
   16431       17216 :       filenames.quick_push (fname);
   16432             :     }
   16433             : 
   16434        1787 :   unsigned num_ordinary = sec.u ();
   16435        2219 :   dump () && dump ("Ordinary maps:%u, range_bits:%u", num_ordinary, range_bits);
   16436             : 
   16437        1787 :   location_t offset = line_table->highest_location + 1;
   16438        1787 :   offset += ((1u << range_bits) - 1);
   16439        1787 :   offset &= ~((1u << range_bits) - 1);
   16440        1787 :   ordinary_locs.first = offset;
   16441             : 
   16442        1787 :   bool propagated = spans.maybe_propagate (this, offset);
   16443        1787 :   line_map_ordinary *maps = static_cast<line_map_ordinary *>
   16444        1787 :     (line_map_new_raw (line_table, false, num_ordinary));
   16445             : 
   16446        1787 :   const line_map_ordinary *base = nullptr;
   16447      316741 :   for (unsigned ix = 0; ix != num_ordinary && !sec.get_overrun (); ix++)
   16448             :     {
   16449      314954 :       line_map_ordinary *map = &maps[ix];
   16450             : 
   16451      314954 :       unsigned offset = sec.u ();
   16452      314954 :       if (!offset)
   16453             :         {
   16454       37497 :           map->reason = LC_RENAME;
   16455       37497 :           map->sysp = sec.u ();
   16456       37497 :           map->m_range_bits = sec.u ();
   16457       37497 :           map->m_column_and_range_bits = sec.u () + map->m_range_bits;
   16458       37497 :           unsigned fnum = sec.u ();
   16459       74994 :           map->to_file = (fnum < filenames.length () ? filenames[fnum] : "");
   16460       37497 :           map->to_line = sec.u ();
   16461       37497 :           base = map;
   16462             :         }
   16463             :       else
   16464             :         {
   16465      277457 :           *map = *base;
   16466      277457 :           map->to_line += offset >> map->m_column_and_range_bits;
   16467             :         }
   16468      314954 :       unsigned remap = sec.u ();
   16469      314954 :       map->start_location = remap + ordinary_locs.first;
   16470      314954 :       if (base == map)
   16471             :         {
   16472             :           /* Root the outermost map at our location.  */
   16473       37497 :           ordinary_locs.second = remap;
   16474       37497 :           location_t from = read_location (sec);
   16475       37497 :           map->included_from = from != UNKNOWN_LOCATION ? from : loc;
   16476             :         }
   16477             :     }
   16478             : 
   16479        1787 :   ordinary_locs.second = num_ord_locs;
   16480             :   /* highest_location is the one handed out, not the next one to
   16481             :      hand out.  */
   16482        1787 :   line_table->highest_location = ordinary_locs.first + ordinary_locs.second - 1;
   16483             : 
   16484        1787 :   if (line_table->highest_location >= LINE_MAP_MAX_LOCATION_WITH_COLS)
   16485             :     /* We shouldn't run out of locations, as we checked before
   16486             :        starting.  */
   16487           0 :     sec.set_overrun ();
   16488        2219 :   dump () && dump ("Ordinary location [%u,+%u)",
   16489             :                    ordinary_locs.first, ordinary_locs.second);
   16490             : 
   16491        1787 :   if (propagated)
   16492          78 :     spans.close ();
   16493             : 
   16494        1787 :   filenames.release ();
   16495             :   
   16496        1787 :   dump.outdent ();
   16497        1787 :   if (!sec.end (from ()))
   16498             :     return false;
   16499             : 
   16500             :   return true;
   16501        1787 : }
   16502             : 
   16503             : bool
   16504         131 : module_state::read_macro_maps (unsigned num_macro_locs)
   16505             : {
   16506         131 :   bytes_in sec;
   16507             : 
   16508         131 :   if (!sec.begin (loc, from (), MOD_SNAME_PFX ".mlm"))
   16509             :     return false;
   16510         137 :   dump () && dump ("Reading macro location maps");
   16511         131 :   dump.indent ();
   16512             : 
   16513         131 :   unsigned num_macros = sec.u ();
   16514         137 :   dump () && dump ("Macro maps:%u locs:%u", num_macros, num_macro_locs);
   16515             : 
   16516         262 :   bool propagated = spans.maybe_propagate (this,
   16517         131 :                                            line_table->highest_location + 1);
   16518             : 
   16519         131 :   location_t offset = LINEMAPS_MACRO_LOWEST_LOCATION (line_table);
   16520         131 :   macro_locs.second = num_macro_locs;
   16521         131 :   macro_locs.first = offset - num_macro_locs;
   16522             : 
   16523         137 :   dump () && dump ("Macro loc delta %d", offset);
   16524         137 :   dump () && dump ("Macro locations [%u,%u)",
   16525             :                    macro_locs.first, macro_locs.second);
   16526             : 
   16527      263819 :   for (unsigned ix = 0; ix != num_macros && !sec.get_overrun (); ix++)
   16528             :     {
   16529      263688 :       unsigned offset = sec.u ();
   16530      263688 :       unsigned n_tokens = sec.u ();
   16531      263688 :       cpp_hashnode *node = sec.cpp_node ();
   16532      263688 :       location_t exp_loc = read_location (sec);
   16533             : 
   16534      263688 :       const line_map_macro *macro
   16535      263688 :         = linemap_enter_macro (line_table, node, exp_loc, n_tokens);
   16536      263688 :       if (!macro)
   16537             :         /* We shouldn't run out of locations, as we checked that we
   16538             :            had enough before starting.  */
   16539             :         break;
   16540      263688 :       gcc_checking_assert (MAP_START_LOCATION (macro)
   16541             :                            == offset + macro_locs.first);
   16542             : 
   16543      263688 :       location_t *locs = macro->macro_locations;
   16544      263688 :       location_t tok_loc = UNKNOWN_LOCATION;
   16545      263688 :       unsigned count = sec.u ();
   16546      263688 :       unsigned runs = 0;
   16547     9548714 :       for (unsigned jx = macro->n_tokens * 2; jx-- && !sec.get_overrun ();)
   16548             :         {
   16549    14236795 :           while (!count-- && !sec.get_overrun ())
   16550             :             {
   16551     4951769 :               runs++;
   16552     4951769 :               tok_loc = read_location (sec);
   16553     4951769 :               count = sec.u ();
   16554             :             }
   16555     9285026 :           locs[jx] = tok_loc;
   16556             :         }
   16557      263688 :       if (count)
   16558           0 :         sec.set_overrun ();
   16559      263715 :       dump (dumper::LOCATION)
   16560           0 :         && dump ("Macro:%u %I %u/%u*2 locations [%u,%u)",
   16561             :                  ix, identifier (node), runs, n_tokens,
   16562             :                  MAP_START_LOCATION (macro),
   16563           0 :                  MAP_START_LOCATION (macro) + n_tokens);
   16564             :     }
   16565             : 
   16566         137 :   dump () && dump ("Macro location lwm:%u", macro_locs.first);
   16567         131 :   if (propagated)
   16568           3 :     spans.close ();
   16569             : 
   16570         131 :   dump.outdent ();
   16571         131 :   if (!sec.end (from ()))
   16572             :     return false;
   16573             : 
   16574             :   return true;
   16575         131 : }
   16576             : 
   16577             : /* Serialize the definition of MACRO.  */
   16578             : 
   16579             : void
   16580       96570 : module_state::write_define (bytes_out &sec, const cpp_macro *macro)
   16581             : {
   16582       96570 :   sec.u (macro->count);
   16583             : 
   16584       96570 :   sec.b (macro->fun_like);
   16585       96570 :   sec.b (macro->variadic);
   16586       96570 :   sec.b (macro->syshdr);
   16587       96570 :   sec.bflush ();
   16588             : 
   16589       96570 :   write_location (sec, macro->line);
   16590       96570 :   if (macro->fun_like)
   16591             :     {
   16592       11912 :       sec.u (macro->paramc);
   16593       11912 :       const cpp_hashnode *const *parms = macro->parm.params;
   16594       30499 :       for (unsigned ix = 0; ix != macro->paramc; ix++)
   16595       18587 :         sec.cpp_node (parms[ix]);
   16596             :     }
   16597             : 
   16598             :   unsigned len = 0;
   16599      314644 :   for (unsigned ix = 0; ix != macro->count; ix++)
   16600             :     {
   16601      218074 :       const cpp_token *token = &macro->exp.tokens[ix];
   16602      218074 :       write_location (sec, token->src_loc);
   16603      218074 :       sec.u (token->type);
   16604      218074 :       sec.u (token->flags);
   16605      218074 :       switch (cpp_token_val_index (token))
   16606             :         {
   16607           0 :         default:
   16608           0 :           gcc_unreachable ();
   16609             : 
   16610       16289 :         case CPP_TOKEN_FLD_ARG_NO:
   16611             :           /* An argument reference.  */
   16612       16289 :           sec.u (token->val.macro_arg.arg_no);
   16613       16289 :           sec.cpp_node (token->val.macro_arg.spelling);
   16614             :           break;
   16615             : 
   16616       48389 :         case CPP_TOKEN_FLD_NODE:
   16617             :           /* An identifier.  */
   16618       48389 :           sec.cpp_node (token->val.node.node);
   16619       48389 :           if (token->val.node.spelling == token->val.node.node)
   16620             :             /* The spelling will usually be the same.  so optimize
   16621             :                that.  */
   16622       48389 :             sec.str (NULL, 0);
   16623             :           else
   16624      218074 :             sec.cpp_node (token->val.node.spelling);
   16625             :           break;
   16626             : 
   16627             :         case CPP_TOKEN_FLD_NONE:
   16628             :           break;
   16629             : 
   16630       63312 :         case CPP_TOKEN_FLD_STR:
   16631             :           /* A string, number or comment.  Not always NUL terminated,
   16632             :              we stream out in a single contatenation with embedded
   16633             :              NULs as that's a safe default.  */
   16634       63312 :           len += token->val.str.len + 1;
   16635       63312 :           sec.u (token->val.str.len);
   16636       63312 :           break;
   16637             : 
   16638           0 :         case CPP_TOKEN_FLD_SOURCE:
   16639           0 :         case CPP_TOKEN_FLD_TOKEN_NO:
   16640           0 :         case CPP_TOKEN_FLD_PRAGMA:
   16641             :           /* These do not occur inside a macro itself.  */
   16642           0 :           gcc_unreachable ();
   16643             :         }
   16644             :     }
   16645             : 
   16646       96570 :   if (len)
   16647             :     {
   16648       58004 :       char *ptr = reinterpret_cast<char *> (sec.buf (len));
   16649       58004 :       len = 0;
   16650      187242 :       for (unsigned ix = 0; ix != macro->count; ix++)
   16651             :         {
   16652      129238 :           const cpp_token *token = &macro->exp.tokens[ix];
   16653      129238 :           if (cpp_token_val_index (token) == CPP_TOKEN_FLD_STR)
   16654             :             {
   16655       63312 :               memcpy (ptr + len, token->val.str.text,
   16656       63312 :                       token->val.str.len);
   16657       63312 :               len += token->val.str.len;
   16658       63312 :               ptr[len++] = 0;
   16659             :             }
   16660             :         }
   16661             :     }
   16662       96570 : }
   16663             : 
   16664             : /* Read a macro definition.  */
   16665             : 
   16666             : cpp_macro *
   16667         524 : module_state::read_define (bytes_in &sec, cpp_reader *reader) const
   16668             : {
   16669         524 :   unsigned count = sec.u ();
   16670             :   /* We rely on knowing cpp_reader's hash table is ident_hash, and
   16671             :      its subobject allocator is stringpool_ggc_alloc and that is just
   16672             :      a wrapper for ggc_alloc_atomic.  */
   16673         524 :   cpp_macro *macro
   16674        1048 :     = (cpp_macro *)ggc_alloc_atomic (sizeof (cpp_macro)
   16675         524 :                                      + sizeof (cpp_token) * (count - !!count));
   16676         524 :   memset (macro, 0, sizeof (cpp_macro) + sizeof (cpp_token) * (count - !!count));
   16677             : 
   16678         524 :   macro->count = count;
   16679         524 :   macro->kind = cmk_macro;
   16680         524 :   macro->imported_p = true;
   16681             : 
   16682         524 :   macro->fun_like = sec.b ();
   16683         524 :   macro->variadic = sec.b ();
   16684         524 :   macro->syshdr = sec.b ();
   16685         524 :   sec.bflush ();
   16686             : 
   16687         524 :   macro->line = read_location (sec);
   16688             : 
   16689         524 :   if (macro->fun_like)
   16690             :     {
   16691         104 :       unsigned paramc = sec.u ();
   16692         104 :       cpp_hashnode **params
   16693         104 :         = (cpp_hashnode **)ggc_alloc_atomic (sizeof (cpp_hashnode *) * paramc);
   16694         104 :       macro->paramc = paramc;
   16695         104 :       macro->parm.params = params;
   16696         235 :       for (unsigned ix = 0; ix != paramc; ix++)
   16697         131 :         params[ix] = sec.cpp_node ();
   16698             :     }
   16699             : 
   16700             :   unsigned len = 0;
   16701        1697 :   for (unsigned ix = 0; ix != count && !sec.get_overrun (); ix++)
   16702             :     {
   16703        1173 :       cpp_token *token = &macro->exp.tokens[ix];
   16704        1173 :       token->src_loc = read_location (sec);
   16705        1173 :       token->type = cpp_ttype (sec.u ());
   16706        1173 :       token->flags = sec.u ();
   16707        1173 :       switch (cpp_token_val_index (token))
   16708             :         {
   16709           0 :         default:
   16710        1173 :           sec.set_overrun ();
   16711             :           break;
   16712             : 
   16713         109 :         case CPP_TOKEN_FLD_ARG_NO:
   16714             :           /* An argument reference.  */
   16715         109 :           {
   16716         109 :             unsigned arg_no = sec.u ();
   16717         109 :             if (arg_no - 1 >= macro->paramc)
   16718           0 :               sec.set_overrun ();
   16719         109 :             token->val.macro_arg.arg_no = arg_no;
   16720         109 :             token->val.macro_arg.spelling = sec.cpp_node ();
   16721             :           }
   16722         109 :           break;
   16723             : 
   16724         303 :         case CPP_TOKEN_FLD_NODE:
   16725             :           /* An identifier.  */
   16726         303 :           token->val.node.node = sec.cpp_node ();
   16727         303 :           token->val.node.spelling = sec.cpp_node ();
   16728         303 :           if (!token->val.node.spelling)
   16729         303 :             token->val.node.spelling = token->val.node.node;
   16730             :           break;
   16731             : 
   16732             :         case CPP_TOKEN_FLD_NONE:
   16733             :           break;
   16734             : 
   16735         277 :         case CPP_TOKEN_FLD_STR:
   16736             :           /* A string, number or comment.  */
   16737         277 :           token->val.str.len = sec.u ();
   16738         277 :           len += token->val.str.len + 1;
   16739         277 :           break;
   16740             :         }
   16741             :     }
   16742             : 
   16743         524 :   if (len)
   16744         272 :     if (const char *ptr = reinterpret_cast<const char *> (sec.buf (len)))
   16745             :       {
   16746             :         /* There should be a final NUL.  */
   16747         272 :         if (ptr[len-1])
   16748           0 :           sec.set_overrun ();
   16749             :         /* cpp_alloc_token_string will add a final NUL.  */
   16750         272 :         const unsigned char *buf
   16751         272 :           = cpp_alloc_token_string (reader, (const unsigned char *)ptr, len - 1);
   16752         272 :         len = 0;
   16753         732 :         for (unsigned ix = 0; ix != count && !sec.get_overrun (); ix++)
   16754             :           {
   16755         460 :             cpp_token *token = &macro->exp.tokens[ix];
   16756         460 :             if (cpp_token_val_index (token) == CPP_TOKEN_FLD_STR)
   16757             :               {
   16758         277 :                 token->val.str.text = buf + len;
   16759         277 :                 len += token->val.str.len;
   16760         277 :                 if (buf[len++])
   16761         460 :                   sec.set_overrun ();
   16762             :               }
   16763             :           }
   16764             :       }
   16765             : 
   16766         524 :   if (sec.get_overrun ())
   16767           0 :     return NULL;
   16768             :   return macro;
   16769             : }
   16770             : 
   16771             : /* Exported macro data.  */
   16772             : struct GTY(()) macro_export {
   16773             :   cpp_macro *def;
   16774             :   location_t undef_loc;
   16775             : 
   16776      159716 :   macro_export ()
   16777      159716 :     :def (NULL), undef_loc (UNKNOWN_LOCATION)
   16778             :   {
   16779             :   }
   16780             : };
   16781             : 
   16782             : /* Imported macro data.  */
   16783             : class macro_import {
   16784             : public:
   16785             :   struct slot {
   16786             : #if defined (WORDS_BIGENDIAN) && SIZEOF_VOID_P == 8
   16787             :     int offset;
   16788             : #endif
   16789             :     /* We need to ensure we don't use the LSB for representation, as
   16790             :        that's the union discriminator below.  */
   16791             :     unsigned bits;
   16792             : 
   16793             : #if !(defined (WORDS_BIGENDIAN) && SIZEOF_VOID_P == 8)
   16794             :     int offset;
   16795             : #endif
   16796             : 
   16797             :   public:
   16798             :     enum Layout {
   16799             :       L_DEF = 1,
   16800             :       L_UNDEF = 2,
   16801             :       L_BOTH = 3,
   16802             :       L_MODULE_SHIFT = 2
   16803             :     };
   16804             : 
   16805             :   public:
   16806             :     /* Not a regular ctor, because we put it in a union, and that's
   16807             :        not allowed in C++ 98.  */
   16808      284076 :     static slot ctor (unsigned module, unsigned defness)
   16809             :     {
   16810      284076 :       gcc_checking_assert (defness);
   16811      284076 :       slot s;
   16812      284076 :       s.bits = defness | (module << L_MODULE_SHIFT);
   16813      284076 :       s.offset = -1;
   16814      284076 :       return s;
   16815             :     }
   16816             : 
   16817             :   public:
   16818      196283 :     unsigned get_defness () const
   16819             :     {
   16820      196283 :       return bits & L_BOTH;
   16821             :     }
   16822      164143 :     unsigned get_module () const
   16823             :     {
   16824      164143 :       return bits >> L_MODULE_SHIFT;
   16825             :     }
   16826          12 :     void become_undef ()
   16827             :     {
   16828          12 :       bits &= ~unsigned (L_DEF);
   16829          12 :       bits |= unsigned (L_UNDEF);
   16830             :     }
   16831             :   };
   16832             : 
   16833             : private:
   16834             :   typedef vec<slot, va_heap, vl_embed> ary_t;
   16835             :   union either {
   16836             :     /* Discriminated by bits 0|1 != 0.  The expected case is that
   16837             :        there will be exactly one slot per macro, hence the effort of
   16838             :        packing that.  */
   16839             :     ary_t *ary;
   16840             :     slot single;
   16841             :   } u;
   16842             : 
   16843             : public:
   16844      216198 :   macro_import ()
   16845      216198 :   {
   16846      216198 :     u.ary = NULL;
   16847             :   }
   16848             : 
   16849             : private:
   16850    10851185 :   bool single_p () const
   16851             :   {
   16852    10851185 :     return u.single.bits & slot::L_BOTH;
   16853             :   }
   16854    11067389 :   bool occupied_p () const
   16855             :   {
   16856    11067389 :     return u.ary != NULL;
   16857             :   }
   16858             : 
   16859             : public:
   16860        1530 :   unsigned length () const
   16861             :   {
   16862        1530 :     gcc_checking_assert (occupied_p ());
   16863        1530 :     return single_p () ? 1 : u.ary->length ();
   16864             :   }
   16865    10622591 :   slot &operator[] (unsigned ix)
   16866             :   {
   16867    10622591 :     gcc_checking_assert (occupied_p ());
   16868    10622591 :     if (single_p ())
   16869             :       {
   16870    10397933 :         gcc_checking_assert (!ix);
   16871    10397933 :         return u.single;
   16872             :       }
   16873             :     else
   16874      224658 :       return (*u.ary)[ix];
   16875             :   }
   16876             : 
   16877             : public:
   16878             :   slot &exported ();
   16879             :   slot &append (unsigned module, unsigned defness);
   16880             : };
   16881             : 
   16882             : /* O is a new import to append to the list for.  If we're an empty
   16883             :    set, initialize us.  */
   16884             : 
   16885             : macro_import::slot &
   16886      284076 : macro_import::append (unsigned module, unsigned defness)
   16887             : {
   16888      284076 :   if (!occupied_p ())
   16889             :     {
   16890      216198 :       u.single = slot::ctor (module, defness);
   16891      216198 :       return u.single;
   16892             :     }
   16893             :   else
   16894             :     {
   16895       67878 :       bool single = single_p ();
   16896       67878 :       ary_t *m = single ? NULL : u.ary;
   16897       67878 :       vec_safe_reserve (m, 1 + single);
   16898       67878 :       if (single)
   16899       67875 :         m->quick_push (u.single);
   16900       67878 :       u.ary = m;
   16901       67878 :       return *u.ary->quick_push (slot::ctor (module, defness));
   16902             :     }
   16903             : }
   16904             : 
   16905             : /* We're going to export something.  Make sure the first import slot
   16906             :    is us.  */
   16907             : 
   16908             : macro_import::slot &
   16909      159192 : macro_import::exported ()
   16910             : {
   16911      159192 :   if (occupied_p () && !(*this)[0].get_module ())
   16912             :     {
   16913           6 :       slot &res = (*this)[0];
   16914           6 :       res.bits |= slot::L_DEF;
   16915           6 :       return res;
   16916             :     }
   16917             : 
   16918      159186 :   slot *a = &append (0, slot::L_DEF);
   16919      159186 :   if (!single_p ())
   16920             :     {
   16921       64179 :       slot &f = (*this)[0];
   16922       64179 :       std::swap (f, *a);
   16923       64179 :       a = &f;
   16924             :     }
   16925             :   return *a;
   16926             : }
   16927             : 
   16928             : /* The import (&exported) macros.  cpp_hasnode's deferred field
   16929             :    indexes this array (offset by 1, so zero means 'not present'.  */
   16930             : 
   16931             : static vec<macro_import, va_heap, vl_embed> *macro_imports;
   16932             : 
   16933             : /* The exported macros.  A macro_import slot's zeroth element's offset
   16934             :    indexes this array.  If the zeroth slot is not for module zero,
   16935             :    there is no export.  */
   16936             : 
   16937             : static GTY(()) vec<macro_export, va_gc> *macro_exports;
   16938             : 
   16939             : /* The reachable set of header imports from this TU.  */
   16940             : 
   16941             : static GTY(()) bitmap headers;
   16942             : 
   16943             : /* Get the (possibly empty) macro imports for NODE.  */
   16944             : 
   16945             : static macro_import &
   16946      219903 : get_macro_imports (cpp_hashnode *node)
   16947             : {
   16948      219903 :   if (node->deferred)
   16949        3705 :     return (*macro_imports)[node->deferred - 1];
   16950             : 
   16951      216198 :   vec_safe_reserve (macro_imports, 1);
   16952      216198 :   node->deferred = macro_imports->length () + 1;
   16953      216198 :   return *vec_safe_push (macro_imports, macro_import ());
   16954             : }
   16955             : 
   16956             : /* Get the macro export for export EXP of NODE.  */
   16957             : 
   16958             : static macro_export &
   16959      159192 : get_macro_export (macro_import::slot &slot)
   16960             : {
   16961      159192 :   if (slot.offset >= 0)
   16962           6 :     return (*macro_exports)[slot.offset];
   16963             : 
   16964      159186 :   vec_safe_reserve (macro_exports, 1);
   16965      159186 :   slot.offset = macro_exports->length ();
   16966      159186 :   return *macro_exports->quick_push (macro_export ());
   16967             : }
   16968             : 
   16969             : /* If NODE is an exportable macro, add it to the export set.  */
   16970             : 
   16971             : static int
   16972     2808459 : maybe_add_macro (cpp_reader *, cpp_hashnode *node, void *data_)
   16973             : {
   16974     2808459 :   bool exporting = false;
   16975             : 
   16976     2808459 :   if (cpp_user_macro_p (node))
   16977      393731 :     if (cpp_macro *macro = node->value.macro)
   16978             :       /* Ignore imported, builtins, command line and forced header macros.  */
   16979      392502 :       if (!macro->imported_p
   16980      392502 :           && !macro->lazy && macro->line >= spans.main_start ())
   16981             :         {
   16982       95013 :           gcc_checking_assert (macro->kind == cmk_macro);
   16983             :           /* I don't want to deal with this corner case, that I suspect is
   16984             :              a devil's advocate reading of the standard.  */
   16985       95013 :           gcc_checking_assert (!macro->extra_tokens);
   16986             : 
   16987       95013 :           macro_import::slot &slot = get_macro_imports (node).exported ();
   16988       95013 :           macro_export &exp = get_macro_export (slot);
   16989       95013 :           exp.def = macro;
   16990       95013 :           exporting = true;
   16991             :         }
   16992             : 
   16993     2713446 :   if (!exporting && node->deferred)
   16994             :     {
   16995        1597 :       macro_import &imports = (*macro_imports)[node->deferred - 1];
   16996        1597 :       macro_import::slot &slot = imports[0];
   16997        1597 :       if (!slot.get_module ())
   16998             :         {
   16999        1564 :           gcc_checking_assert (slot.get_defness ());
   17000             :           exporting = true;
   17001             :         }
   17002             :     }
   17003             : 
   17004       95013 :   if (exporting)
   17005       96577 :     static_cast<vec<cpp_hashnode *> *> (data_)->safe_push (node);
   17006             : 
   17007     2808459 :   return 1; /* Don't stop.  */
   17008             : }
   17009             : 
   17010             : /* Order cpp_hashnodes A_ and B_ by their exported macro locations.  */
   17011             : 
   17012             : static int
   17013     5100308 : macro_loc_cmp (const void *a_, const void *b_)
   17014             : {
   17015     5100308 :   const cpp_hashnode *node_a = *(const cpp_hashnode *const *)a_;
   17016     5100308 :   macro_import &import_a = (*macro_imports)[node_a->deferred - 1];
   17017     5100308 :   const macro_export &export_a = (*macro_exports)[import_a[0].offset];
   17018     5100308 :   location_t loc_a = export_a.def ? export_a.def->line : export_a.undef_loc;
   17019             : 
   17020     5100308 :   const cpp_hashnode *node_b = *(const cpp_hashnode *const *)b_;
   17021     5100308 :   macro_import &import_b = (*macro_imports)[node_b->deferred - 1];
   17022     5100308 :   const macro_export &export_b = (*macro_exports)[import_b[0].offset];
   17023     5100308 :   location_t loc_b = export_b.def ? export_b.def->line : export_b.undef_loc;
   17024             : 
   17025     5100308 :   if (loc_a < loc_b)
   17026             :     return +1;
   17027     2619458 :   else if (loc_a > loc_b)
   17028             :     return -1;
   17029             :   else
   17030           0 :     return 0;
   17031             : }
   17032             : 
   17033             : /* Gather the macro definitions and undefinitions that we will need to
   17034             :    write out.   */
   17035             : 
   17036             : vec<cpp_hashnode *> *
   17037         643 : module_state::prepare_macros (cpp_reader *reader)
   17038             : {
   17039         643 :   vec<cpp_hashnode *> *macros;
   17040         643 :   vec_alloc (macros, 100);
   17041             : 
   17042         643 :   cpp_forall_identifiers (reader, maybe_add_macro, macros);
   17043             : 
   17044         667 :   dump (dumper::MACRO) && dump ("No more than %u macros", macros->length ());
   17045             : 
   17046         643 :   macros->qsort (macro_loc_cmp);
   17047             : 
   17048             :   // Note the locations.
   17049       97863 :   for (unsigned ix = macros->length (); ix--;)
   17050             :     {
   17051       96577 :       cpp_hashnode *node = (*macros)[ix];
   17052       96577 :       macro_import::slot &slot = (*macro_imports)[node->deferred - 1][0];
   17053       96577 :       macro_export &mac = (*macro_exports)[slot.offset];
   17054             : 
   17055       96577 :       if (IDENTIFIER_KEYWORD_P (identifier (node)))
   17056           1 :         continue;
   17057             : 
   17058       96576 :       if (mac.undef_loc != UNKNOWN_LOCATION)
   17059          12 :         note_location (mac.undef_loc);
   17060       96576 :       if (mac.def)
   17061             :         {
   17062       96570 :           note_location (mac.def->line);
   17063      314644 :           for (unsigned ix = 0; ix != mac.def->count; ix++)
   17064      218074 :             note_location (mac.def->exp.tokens[ix].src_loc);
   17065             :         }
   17066             :     }
   17067             : 
   17068         643 :   return macros;
   17069             : }
   17070             : 
   17071             : /* Write out the exported defines.  This is two sections, one
   17072             :    containing the definitions, the other a table of node names.  */
   17073             : 
   17074             : unsigned
   17075         643 : module_state::write_macros (elf_out *to, vec<cpp_hashnode *> *macros,
   17076             :                             unsigned *crc_p)
   17077             : {
   17078         700 :   dump () && dump ("Writing macros");
   17079         643 :   dump.indent ();
   17080             : 
   17081             :   /* Write the defs */
   17082         643 :   bytes_out sec (to);
   17083         643 :   sec.begin ();
   17084             : 
   17085         643 :   unsigned count = 0;
   17086       97863 :   for (unsigned ix = macros->length (); ix--;)
   17087             :     {
   17088       96577 :       cpp_hashnode *node = (*macros)[ix];
   17089       96577 :       macro_import::slot &slot = (*macro_imports)[node->deferred - 1][0];
   17090       96577 :       gcc_assert (!slot.get_module () && slot.get_defness ());
   17091             : 
   17092       96577 :       macro_export &mac = (*macro_exports)[slot.offset];
   17093       96577 :       gcc_assert (!!(slot.get_defness () & macro_import::slot::L_UNDEF)
   17094             :                   == (mac.undef_loc != UNKNOWN_LOCATION)
   17095             :                   && !!(slot.get_defness () & macro_import::slot::L_DEF)
   17096             :                   == (mac.def != NULL));
   17097             : 
   17098       96577 :       if (IDENTIFIER_KEYWORD_P (identifier (node)))
   17099             :         {
   17100           1 :           warning_at (mac.def->line, 0,
   17101             :                       "not exporting %<#define %E%> as it is a keyword",
   17102             :                       identifier (node));
   17103           1 :           slot.offset = 0;
   17104           1 :           continue;
   17105             :         }
   17106             : 
   17107       96576 :       count++;
   17108       96576 :       slot.offset = sec.pos;
   17109       96576 :       dump (dumper::MACRO)
   17110          24 :         && dump ("Writing macro %s%s%s %I at %u",
   17111          24 :                  slot.get_defness () & macro_import::slot::L_UNDEF
   17112             :                  ? "#undef" : "",
   17113          24 :                  slot.get_defness () == macro_import::slot::L_BOTH
   17114             :                  ? " & " : "",
   17115          24 :                  slot.get_defness () & macro_import::slot::L_DEF
   17116             :                  ? "#define" : "",
   17117             :                  identifier (node), slot.offset);
   17118       96576 :       if (mac.undef_loc != UNKNOWN_LOCATION)
   17119          12 :         write_location (sec, mac.undef_loc);
   17120       96576 :       if (mac.def)
   17121       96570 :         write_define (sec, mac.def);
   17122             :     }
   17123         643 :   if (count)
   17124             :     // We may have ended on a tokenless macro with a very short
   17125             :     // location, that will cause problems reading its bit flags.
   17126         137 :     sec.u (0);
   17127         643 :   sec.end (to, to->name (MOD_SNAME_PFX ".def"), crc_p);
   17128             : 
   17129         643 :   if (count)
   17130             :     {
   17131             :       /* Write the table.  */
   17132         137 :       bytes_out sec (to);
   17133         137 :       sec.begin ();
   17134         137 :       sec.u (count);
   17135             : 
   17136       96850 :       for (unsigned ix = macros->length (); ix--;)
   17137             :         {
   17138       96576 :           const cpp_hashnode *node = (*macros)[ix];
   17139       96576 :           macro_import::slot &slot = (*macro_imports)[node->deferred - 1][0];
   17140             : 
   17141       96576 :           if (slot.offset)
   17142             :             {
   17143       96576 :               sec.cpp_node (node);
   17144       96576 :               sec.u (slot.get_defness ());
   17145       96576 :               sec.u (slot.offset);
   17146             :             }
   17147             :         }
   17148         137 :       sec.end (to, to->name (MOD_SNAME_PFX ".mac"), crc_p);
   17149         137 :     }
   17150             : 
   17151         643 :   dump.outdent ();
   17152         643 :   return count;
   17153         643 : }
   17154             : 
   17155             : bool
   17156         674 : module_state::read_macros ()
   17157             : {
   17158             :   /* Get the def section.  */
   17159         674 :   if (!slurp->macro_defs.begin (loc, from (), MOD_SNAME_PFX ".def"))
   17160             :     return false;
   17161             : 
   17162             :   /* Get the tbl section, if there are defs. */
   17163         674 :   if (slurp->macro_defs.more_p ()
   17164         674 :       && !slurp->macro_tbl.begin (loc, from (), MOD_SNAME_PFX ".mac"))
   17165             :     return false;
   17166             : 
   17167             :   return true;
   17168             : }
   17169             : 
   17170             : /* Install the macro name table.  */
   17171             : 
   17172             : void
   17173         680 : module_state::install_macros ()
   17174             : {
   17175         680 :   bytes_in &sec = slurp->macro_tbl;
   17176         680 :   if (!sec.size)
   17177             :     return;
   17178             : 
   17179         196 :   dump () && dump ("Reading macro table %M", this);
   17180         175 :   dump.indent ();
   17181             : 
   17182         175 :   unsigned count = sec.u ();
   17183         196 :   dump () && dump ("%u macros", count);
   17184      125065 :   while (count--)
   17185             :     {
   17186      124890 :       cpp_hashnode *node = sec.cpp_node ();
   17187      124890 :       macro_import &imp = get_macro_imports (node);
   17188      124890 :       unsigned flags = sec.u () & macro_import::slot::L_BOTH;
   17189      124890 :       if (!flags)
   17190           0 :         sec.set_overrun ();
   17191             : 
   17192      124890 :       if (sec.get_overrun ())
   17193             :         break;
   17194             : 
   17195      124890 :       macro_import::slot &slot = imp.append (mod, flags);
   17196      124890 :       slot.offset = sec.u ();
   17197             : 
   17198      124890 :       dump (dumper::MACRO)
   17199          84 :         && dump ("Read %s macro %s%s%s %I at %u",
   17200          30 :                  imp.length () > 1 ? "add" : "new",
   17201          30 :                  flags & macro_import::slot::L_UNDEF ? "#undef" : "",
   17202             :                  flags == macro_import::slot::L_BOTH ? " & " : "",
   17203          30 :                  flags & macro_import::slot::L_DEF ? "#define" : "",
   17204             :                  identifier (node), slot.offset);
   17205             : 
   17206             :       /* We'll leak an imported definition's TOKEN_FLD_STR's data
   17207             :          here.  But that only happens when we've had to resolve the
   17208             :          deferred macro before this import -- why are you doing
   17209             :          that?  */
   17210      124890 :       if (cpp_macro *cur = cpp_set_deferred_macro (node))
   17211       64167 :         if (!cur->imported_p)
   17212             :           {
   17213       64167 :             macro_import::slot &slot = imp.exported ();
   17214       64167 :             macro_export &exp = get_macro_export (slot);
   17215       64167 :             exp.def = cur;
   17216      189232 :             dump (dumper::MACRO)
   17217           0 :               && dump ("Saving current #define %I", identifier (node));
   17218             :           }
   17219             :     }
   17220             : 
   17221             :   /* We're now done with the table.  */
   17222         175 :   elf_in::release (slurp->from, sec);
   17223             : 
   17224         175 :   dump.outdent ();
   17225             : }
   17226             : 
   17227             : /* Import the transitive macros.  */
   17228             : 
   17229             : void
   17230         651 : module_state::import_macros ()
   17231             : {
   17232         651 :   bitmap_ior_into (headers, slurp->headers);
   17233             : 
   17234         651 :   bitmap_iterator bititer;
   17235         651 :   unsigned bitnum;
   17236        1331 :   EXECUTE_IF_SET_IN_BITMAP (slurp->headers, 0, bitnum, bititer)
   17237         680 :     (*modules)[bitnum]->install_macros ();
   17238         651 : }
   17239             : 
   17240             : /* NODE is being undefined at LOC.  Record it in the export table, if
   17241             :    necessary.  */
   17242             : 
   17243             : void
   17244       21866 : module_state::undef_macro (cpp_reader *, location_t loc, cpp_hashnode *node)
   17245             : {
   17246       21866 :   if (!node->deferred)
   17247             :     /* The macro is not imported, so our undef is irrelevant.  */
   17248             :     return;
   17249             : 
   17250          12 :   unsigned n = dump.push (NULL);
   17251             : 
   17252          12 :   macro_import::slot &slot = (*macro_imports)[node->deferred - 1].exported ();
   17253          12 :   macro_export &exp = get_macro_export (slot);
   17254             : 
   17255          12 :   exp.undef_loc = loc;
   17256          12 :   slot.become_undef ();
   17257          12 :   exp.def = NULL;
   17258             : 
   17259          18 :   dump (dumper::MACRO) && dump ("Recording macro #undef %I", identifier (node));
   17260             : 
   17261          12 :   dump.pop (n);
   17262             : }
   17263             : 
   17264             : /* NODE is a deferred macro node.  Determine the definition and return
   17265             :    it, with NULL if undefined.  May issue diagnostics.
   17266             : 
   17267             :    This can leak memory, when merging declarations -- the string
   17268             :    contents (TOKEN_FLD_STR) of each definition are allocated in
   17269             :    unreclaimable cpp objstack.  Only one will win.  However, I do not
   17270             :    expect this to be common -- mostly macros have a single point of
   17271             :    definition.  Perhaps we could restore the objstack to its position
   17272             :    after the first imported definition (if that wins)?  The macros
   17273             :    themselves are GC'd.  */
   17274             : 
   17275             : cpp_macro *
   17276         500 : module_state::deferred_macro (cpp_reader *reader, location_t loc,
   17277             :                               cpp_hashnode *node)
   17278             : {
   17279         500 :   macro_import &imports = (*macro_imports)[node->deferred - 1];
   17280             : 
   17281         500 :   unsigned n = dump.push (NULL);
   17282         506 :   dump (dumper::MACRO) && dump ("Deferred macro %I", identifier (node));
   17283             : 
   17284         500 :   bitmap visible (BITMAP_GGC_ALLOC ());
   17285             : 
   17286         500 :   if (!((imports[0].get_defness () & macro_import::slot::L_UNDEF)
   17287           0 :         && !imports[0].get_module ()))
   17288             :     {
   17289             :       /* Calculate the set of visible header imports.  */
   17290         500 :       bitmap_copy (visible, headers);
   17291        1392 :       for (unsigned ix = imports.length (); ix--;)
   17292             :         {
   17293         892 :           const macro_import::slot &slot = imports[ix];
   17294         892 :           unsigned mod = slot.get_module ();
   17295         892 :           if ((slot.get_defness () & macro_import::slot::L_UNDEF)
   17296         892 :               && bitmap_bit_p (visible, mod))
   17297             :             {
   17298          12 :               bitmap arg = mod ? (*modules)[mod]->slurp->headers : headers;
   17299          12 :               bitmap_and_compl_into (visible, arg);
   17300          12 :               bitmap_set_bit (visible, mod);
   17301             :             }
   17302             :         }
   17303             :     }
   17304         500 :   bitmap_set_bit (visible, 0);
   17305             : 
   17306             :   /* Now find the macros that are still visible.  */
   17307         500 :   bool failed = false;
   17308         500 :   cpp_macro *def = NULL;
   17309         500 :   vec<macro_export> defs;
   17310         500 :   defs.create (imports.length ());
   17311        1392 :   for (unsigned ix = imports.length (); ix--;)
   17312             :     {
   17313         892 :       const macro_import::slot &slot = imports[ix];
   17314         892 :       unsigned mod = slot.get_module ();
   17315         892 :       if (bitmap_bit_p (visible, mod))
   17316             :         {
   17317         880 :           macro_export *pushed = NULL;
   17318         880 :           if (mod)
   17319             :             {
   17320         530 :               const module_state *imp = (*modules)[mod];
   17321         530 :               bytes_in &sec = imp->slurp->macro_defs;
   17322         530 :               if (!sec.get_overrun ())
   17323             :                 {
   17324         530 :                   dump (dumper::MACRO)
   17325           6 :                     && dump ("Reading macro %s%s%s %I module %M at %u",
   17326           6 :                              slot.get_defness () & macro_import::slot::L_UNDEF
   17327             :                              ? "#undef" : "",
   17328           6 :                              slot.get_defness () == macro_import::slot::L_BOTH
   17329             :                              ? " & " : "",
   17330           6 :                              slot.get_defness () & macro_import::slot::L_DEF
   17331             :                              ? "#define" : "",
   17332           6 :                              identifier (node), imp, slot.offset);
   17333         530 :                   sec.random_access (slot.offset);
   17334             : 
   17335         530 :                   macro_export exp;
   17336         530 :                   if (slot.get_defness () & macro_import::slot::L_UNDEF)
   17337          12 :                     exp.undef_loc = imp->read_location (sec);
   17338         530 :                   if (slot.get_defness () & macro_import::slot::L_DEF)
   17339         524 :                     exp.def = imp->read_define (sec, reader);
   17340         530 :                   if (sec.get_overrun ())
   17341           0 :                     error_at (loc, "macro definitions of %qE corrupted",
   17342           0 :                               imp->name);
   17343             :                   else
   17344         530 :                     pushed = defs.quick_push (exp);
   17345             :                 }
   17346             :             }
   17347             :           else
   17348         350 :             pushed = defs.quick_push ((*macro_exports)[slot.offset]);
   17349         880 :           if (pushed && pushed->def)
   17350             :             {
   17351         874 :               if (!def)
   17352             :                 def = pushed->def;
   17353         377 :               else if (cpp_compare_macros (def, pushed->def))
   17354         892 :                 failed = true;
   17355             :             }
   17356             :         }
   17357             :     }
   17358             : 
   17359         500 :   if (failed)
   17360             :     {
   17361             :       /* If LOC is the first loc, this is the end of file check, which
   17362             :          is a warning.  */
   17363          15 :       if (loc == MAP_START_LOCATION (LINEMAPS_ORDINARY_MAP_AT (line_table, 0)))
   17364           9 :         warning_at (loc, OPT_Winvalid_imported_macros,
   17365             :                     "inconsistent imported macro definition %qE",
   17366             :                     identifier (node));
   17367             :       else
   17368           6 :         error_at (loc, "inconsistent imported macro definition %qE",
   17369             :                   identifier (node));
   17370          60 :       for (unsigned ix = defs.length (); ix--;)
   17371             :         {
   17372          30 :           macro_export &exp = defs[ix];
   17373          30 :           if (exp.undef_loc)
   17374           0 :             inform (exp.undef_loc, "%<#undef %E%>", identifier (node));
   17375          30 :           if (exp.def)
   17376          30 :             inform (exp.def->line, "%<#define %s%>",
   17377             :                     cpp_macro_definition (reader, node, exp.def));
   17378             :         }
   17379             :       def = NULL;
   17380             :     }
   17381             : 
   17382         500 :   defs.release ();
   17383             : 
   17384         500 :   dump.pop (n);
   17385             : 
   17386         500 :   return def;
   17387             : }
   17388             : 
   17389             : /* Stream the static aggregates.  Sadly some headers (ahem:
   17390             :    iostream) contain static vars, and rely on them to run global
   17391             :    ctors.  */
   17392             : unsigned
   17393         643 : module_state::write_inits (elf_out *to, depset::hash &table, unsigned *crc_ptr)
   17394             : {
   17395         643 :   if (!static_aggregates && !tls_aggregates)
   17396             :     return 0;
   17397             : 
   17398          18 :   dump () && dump ("Writing initializers");
   17399          18 :   dump.indent ();
   17400             : 
   17401          18 :   static_aggregates = nreverse (static_aggregates);
   17402          18 :   tls_aggregates = nreverse (tls_aggregates);
   17403             : 
   17404          18 :   unsigned count = 0;
   17405          18 :   trees_out sec (to, this, table, ~0u);
   17406          18 :   sec.begin ();
   17407             : 
   17408          18 :   tree list = static_aggregates;
   17409          54 :   for (int passes = 0; passes != 2; passes++)
   17410             :     {
   17411          54 :       for (tree init = list; init; init = TREE_CHAIN (init), count++)
   17412          18 :         if (TREE_LANG_FLAG_0 (init))
   17413             :           {
   17414          18 :             tree decl = TREE_VALUE (init);
   17415             : 
   17416          18 :             dump ("Initializer:%u for %N", count, decl);
   17417          18 :             sec.tree_node (decl);
   17418             :           }
   17419             : 
   17420          36 :       list = tls_aggregates;
   17421             :     }
   17422             :   
   17423          18 :   sec.end (to, to->name (MOD_SNAME_PFX ".ini"), crc_ptr);
   17424          18 :   dump.outdent ();
   17425             : 
   17426          18 :   return count;
   17427         643 : }
   17428             : 
   17429             : /* We have to defer some post-load processing until we've completed
   17430             :    reading, because they can cause more reading.  */
   17431             : 
   17432             : static void
   17433        5121 : post_load_processing ()
   17434             : {
   17435             :   /* We mustn't cause a GC, our caller should have arranged for that
   17436             :      not to happen.  */
   17437        5121 :   gcc_checking_assert (function_depth);
   17438             : 
   17439        5121 :   if (!post_load_decls)
   17440             :     return;
   17441             : 
   17442        3091 :   tree old_cfd = current_function_decl;
   17443        3091 :   struct function *old_cfun = cfun;
   17444       12416 :   while (post_load_decls->length ())
   17445             :     {
   17446        9325 :       tree decl = post_load_decls->pop ();
   17447             : 
   17448        9367 :       dump () && dump ("Post-load processing of %N", decl);
   17449             : 
   17450        9325 :       gcc_checking_assert (DECL_ABSTRACT_P (decl));
   17451             :       /* Cloning can cause loading -- specifically operator delete for
   17452             :          the deleting dtor.  */
   17453        9325 :       maybe_clone_body (decl);
   17454             :     }
   17455             : 
   17456        3091 :   cfun = old_cfun;
   17457        3091 :   current_function_decl = old_cfd;
   17458             : }
   17459             : 
   17460             : bool
   17461          18 : module_state::read_inits (unsigned count)
   17462             : {
   17463          18 :   trees_in sec (this);
   17464          18 :   if (!sec.begin (loc, from (), from ()->find (MOD_SNAME_PFX ".ini")))
   17465             :     return false;
   17466          27 :   dump () && dump ("Reading %u initializers", count);
   17467          18 :   dump.indent ();
   17468             : 
   17469          18 :   lazy_snum = ~0u;
   17470          36 :   for (unsigned ix = 0; ix != count; ix++)
   17471             :     {
   17472             :       /* Merely referencing the decl causes its initializer to be read
   17473             :          and added to the correct list.  */
   17474          18 :       tree decl = sec.tree_node ();
   17475             : 
   17476          18 :       if (sec.get_overrun ())
   17477             :         break;
   17478          18 :       if (decl)
   17479          18 :         dump ("Initializer:%u for %N", count, decl);
   17480             :     }
   17481          18 :   lazy_snum = 0;
   17482          18 :   post_load_processing ();
   17483          18 :   dump.outdent ();
   17484          18 :   if (!sec.end (from ()))
   17485             :     return false;  
   17486             :   return true;
   17487          18 : }
   17488             : 
   17489             : void
   17490        1657 : module_state::write_counts (elf_out *to, unsigned counts[MSC_HWM],
   17491             :                             unsigned *crc_ptr)
   17492             : {
   17493        1657 :   bytes_out cfg (to);
   17494             : 
   17495        1657 :   cfg.begin ();
   17496             : 
   17497       14913 :   for (unsigned ix = MSC_HWM; ix--;)
   17498       13256 :     cfg.u (counts[ix]);
   17499             : 
   17500        1657 :   if (dump ())
   17501             :     {
   17502         225 :       dump ("Cluster sections are [%u,%u)",
   17503             :             counts[MSC_sec_lwm], counts[MSC_sec_hwm]);
   17504         225 :       dump ("Bindings %u", counts[MSC_bindings]);
   17505         225 :       dump ("Pendings %u", counts[MSC_pendings]);
   17506         225 :       dump ("Entities %u", counts[MSC_entities]);
   17507         225 :       dump ("Namespaces %u", counts[MSC_namespaces]);
   17508         225 :       dump ("Macros %u", counts[MSC_macros]);
   17509         225 :       dump ("Initializers %u", counts[MSC_inits]);
   17510             :     }
   17511             : 
   17512        1657 :   cfg.end (to, to->name (MOD_SNAME_PFX ".cnt"), crc_ptr);
   17513        1657 : }
   17514             : 
   17515             : bool
   17516        1807 : module_state::read_counts (unsigned counts[MSC_HWM])
   17517             : {
   17518        1807 :   bytes_in cfg;
   17519             : 
   17520        1807 :   if (!cfg.begin (loc, from (), MOD_SNAME_PFX ".cnt"))
   17521             :     return false;
   17522             : 
   17523       16263 :   for (unsigned ix = MSC_HWM; ix--;)
   17524       14456 :     counts[ix] = cfg.u ();
   17525             : 
   17526        1807 :   if (dump ())
   17527             :     {
   17528         444 :       dump ("Declaration sections are [%u,%u)",
   17529             :             counts[MSC_sec_lwm], counts[MSC_sec_hwm]);
   17530         444 :       dump ("Bindings %u", counts[MSC_bindings]);
   17531         444 :       dump ("Pendings %u", counts[MSC_pendings]);
   17532         444 :       dump ("Entities %u", counts[MSC_entities]);
   17533         444 :       dump ("Namespaces %u", counts[MSC_namespaces]);
   17534         444 :       dump ("Macros %u", counts[MSC_macros]);
   17535         444 :       dump ("Initializers %u", counts[MSC_inits]);
   17536             :     }
   17537             : 
   17538        1807 :   return cfg.end (from ());
   17539        1807 : }
   17540             : 
   17541             : /* Tool configuration:  MOD_SNAME_PFX .config
   17542             : 
   17543             :    This is data that confirms current state (or fails).  */
   17544             : 
   17545             : void
   17546        1660 : module_state::write_config (elf_out *to, module_state_config &config,
   17547             :                             unsigned inner_crc)
   17548             : {
   17549        1660 :   bytes_out cfg (to);
   17550             : 
   17551        1660 :   cfg.begin ();
   17552             : 
   17553             :   /* Write version and inner crc as u32 values, for easier
   17554             :      debug inspection.  */
   17555        1885 :   dump () && dump ("Writing version=%V, inner_crc=%x",
   17556             :                    MODULE_VERSION, inner_crc);
   17557        1660 :   cfg.u32 (unsigned (MODULE_VERSION));
   17558        1660 :   cfg.u32 (inner_crc);
   17559             : 
   17560        1660 :   cfg.u (to->name (is_header () ? "" : get_flatname ()));
   17561             : 
   17562             :   /* Configuration. */
   17563        1885 :   dump () && dump ("Writing target='%s', host='%s'",
   17564             :                    TARGET_MACHINE, HOST_MACHINE);
   17565        1660 :   unsigned target = to->name (TARGET_MACHINE);
   17566        1660 :   unsigned host = (!strcmp (TARGET_MACHINE, HOST_MACHINE)
   17567             :                    ? target : to->name (HOST_MACHINE));
   17568        1660 :   cfg.u (target);
   17569        1660 :   cfg.u (host);
   17570             : 
   17571        1660 :   cfg.str (config.dialect_str);
   17572        1660 :   cfg.u (extensions);
   17573             : 
   17574             :   /* Global tree information.  We write the globals crc separately,
   17575             :      rather than mix it directly into the overall crc, as it is used
   17576             :      to ensure data match between instances of the compiler, not
   17577             :      integrity of the file.  */
   17578        1885 :   dump () && dump ("Writing globals=%u, crc=%x",
   17579             :                    fixed_trees->length (), global_crc);
   17580        1660 :   cfg.u (fixed_trees->length ());
   17581        1660 :   cfg.u32 (global_crc);
   17582             : 
   17583        1660 :   if (is_partition ())
   17584          87 :     cfg.u (is_interface ());
   17585             : 
   17586        1660 :   cfg.u (config.num_imports);
   17587        1660 :   cfg.u (config.num_partitions);
   17588        1660 :   cfg.u (config.num_entities);
   17589             : 
   17590        1660 :   cfg.u (config.ordinary_locs);
   17591        1660 :   cfg.u (config.macro_locs);
   17592        1660 :   cfg.u (config.loc_range_bits);
   17593             : 
   17594        1660 :   cfg.u (config.active_init);
   17595             : 
   17596             :   /* Now generate CRC, we'll have incorporated the inner CRC because
   17597             :      of its serialization above.  */
   17598        1660 :   cfg.end (to, to->name (MOD_SNAME_PFX ".cfg"), &crc);
   17599        1885 :   dump () && dump ("Writing CRC=%x", crc);
   17600        1660 : }
   17601             : 
   17602             : void
   17603          34 : module_state::note_cmi_name ()
   17604             : {
   17605          34 :   if (!cmi_noted_p && filename)
   17606             :     {
   17607          34 :       cmi_noted_p = true;
   17608          34 :       inform (loc, "compiled module file is %qs",
   17609             :               maybe_add_cmi_prefix (filename));
   17610             :     }
   17611          34 : }
   17612             : 
   17613             : bool
   17614        1856 : module_state::read_config (module_state_config &config)
   17615             : {
   17616        1856 :   bytes_in cfg;
   17617             : 
   17618        1856 :   if (!cfg.begin (loc, from (), MOD_SNAME_PFX ".cfg"))
   17619             :     return false;
   17620             : 
   17621             :   /* Check version.  */
   17622        1856 :   unsigned my_ver = MODULE_VERSION;
   17623        1856 :   unsigned their_ver = cfg.u32 ();
   17624        2300 :   dump () && dump  (my_ver == their_ver ? "Version %V"
   17625             :                     : "Expecting %V found %V", my_ver, their_ver);
   17626        1856 :   if (their_ver != my_ver)
   17627             :     {
   17628             :       /* The compiler versions differ.  Close enough? */
   17629           0 :       verstr_t my_string, their_string;
   17630             : 
   17631           0 :       version2string (my_ver, my_string);
   17632           0 :       version2string (their_ver, their_string);
   17633             : 
   17634             :       /* Reject when either is non-experimental or when experimental
   17635             :          major versions differ.  */
   17636           0 :       bool reject_p = ((!IS_EXPERIMENTAL (my_ver)
   17637             :                         || !IS_EXPERIMENTAL (their_ver)
   17638           0 :                         || MODULE_MAJOR (my_ver) != MODULE_MAJOR (their_ver))
   17639             :                        /* The 'I know what I'm doing' switch.  */
   17640           0 :                        && !flag_module_version_ignore);
   17641           0 :       bool inform_p = true;
   17642           0 :       if (reject_p)
   17643             :         {
   17644           0 :           cfg.set_overrun ();
   17645           0 :           error_at (loc, "compiled module is %sversion %s",
   17646             :                     IS_EXPERIMENTAL (their_ver) ? "experimental " : "",
   17647             :                     their_string);
   17648             :         }
   17649             :       else
   17650           0 :         inform_p = warning_at (loc, 0, "compiled module is %sversion %s",
   17651             :                              IS_EXPERIMENTAL (their_ver) ? "experimental " : "",
   17652             :                              their_string);
   17653             : 
   17654           0 :       if (inform_p)
   17655             :         {
   17656           0 :           inform (loc, "compiler is %sversion %s%s%s",
   17657             :                   IS_EXPERIMENTAL (my_ver) ? "experimental " : "",
   17658             :                   my_string,
   17659           0 :                   reject_p ? "" : flag_module_version_ignore
   17660           0 :                   ? ", be it on your own head!" : ", close enough?",
   17661             :                   reject_p ? "" : " \xc2\xaf\\_(\xe3\x83\x84)_/\xc2\xaf");
   17662           0 :           note_cmi_name ();
   17663             :         }
   17664             : 
   17665           0 :       if (reject_p)
   17666           0 :         goto done;
   17667             :     }
   17668             : 
   17669             :   /*  We wrote the inner crc merely to merge it, so simply read it
   17670             :       back and forget it.  */
   17671        1856 :   cfg.u32 ();
   17672             : 
   17673             :   /* Check module name.  */
   17674        1856 :   {
   17675        1856 :     const char *their_name = from ()->name (cfg.u ());
   17676        1856 :     const char *our_name = "";
   17677             : 
   17678        1856 :     if (!is_header ())
   17679        1134 :       our_name = get_flatname ();
   17680             : 
   17681             :     /* Header units can be aliased, so name checking is
   17682             :        inappropriate.  */
   17683        1856 :     if (0 != strcmp (their_name, our_name))
   17684             :       {
   17685           0 :         error_at (loc,
   17686           0 :                   their_name[0] && our_name[0] ? G_("module %qs found")
   17687             :                   : their_name[0]
   17688             :                   ? G_("header module expected, module %qs found")
   17689             :                   : G_("module %qs expected, header module found"),
   17690           0 :                   their_name[0] ? their_name : our_name);
   17691           0 :         cfg.set_overrun ();
   17692           0 :         goto done;
   17693             :       }
   17694             :   }
   17695             : 
   17696             :   /* Check the CRC after the above sanity checks, so that the user is
   17697             :      clued in.  */
   17698        1856 :   {
   17699        1856 :     unsigned e_crc = crc;
   17700        1856 :     crc = cfg.get_crc ();
   17701        2300 :     dump () && dump ("Reading CRC=%x", crc);
   17702        1856 :     if (!is_direct () && crc != e_crc)
   17703             :       {
   17704           3 :         error_at (loc, "module %qs CRC mismatch", get_flatname ());
   17705           3 :         cfg.set_overrun ();
   17706           3 :         goto done;
   17707             :       }
   17708             :   }
   17709             : 
   17710             :   /* Check target & host.  */
   17711        1853 :   {
   17712        1853 :     const char *their_target = from ()->name (cfg.u ());
   17713        1853 :     const char *their_host = from ()->name (cfg.u ());
   17714        2297 :     dump () && dump ("Read target='%s', host='%s'", their_target, their_host);
   17715        1853 :     if (strcmp (their_target, TARGET_MACHINE)
   17716        1853 :         || strcmp (their_host, HOST_MACHINE))
   17717             :       {
   17718           0 :         error_at (loc, "target & host is %qs:%qs, expected %qs:%qs",
   17719             :                   their_target, TARGET_MACHINE, their_host, HOST_MACHINE);
   17720           0 :         cfg.set_overrun ();
   17721           0 :         goto done;
   17722             :       }
   17723             :   }
   17724             : 
   17725             :   /* Check compilation dialect.  This must match.  */
   17726        1853 :   {
   17727        1853 :     const char *their_dialect = cfg.str ();
   17728        1853 :     if (strcmp (their_dialect, config.dialect_str))
   17729             :       {
   17730           1 :         error_at (loc, "language dialect differs %qs, expected %qs",
   17731             :                   their_dialect, config.dialect_str);
   17732           1 :         cfg.set_overrun ();
   17733           1 :         goto done;
   17734             :       }
   17735             :   }
   17736             : 
   17737             :   /* Check for extensions.  If they set any, we must have them set
   17738             :      too.  */
   17739        1852 :   {
   17740        1852 :     unsigned ext = cfg.u ();
   17741        1852 :     unsigned allowed = (flag_openmp ? SE_OPENMP : 0);
   17742             : 
   17743        1852 :     if (unsigned bad = ext & ~allowed)
   17744             :       {
   17745           3 :         if (bad & SE_OPENMP)
   17746           3 :           error_at (loc, "module contains OpenMP, use %<-fopenmp%> to enable");
   17747           3 :         cfg.set_overrun ();
   17748           3 :         goto done;
   17749             :       }
   17750        1849 :     extensions = ext;
   17751             :   }
   17752             : 
   17753             :   /* Check global trees.  */
   17754        1849 :   {
   17755        1849 :     unsigned their_fixed_length = cfg.u ();
   17756        1849 :     unsigned their_fixed_crc = cfg.u32 ();
   17757        2293 :     dump () && dump ("Read globals=%u, crc=%x",
   17758             :                      their_fixed_length, their_fixed_crc);
   17759        1849 :     if (!flag_preprocess_only
   17760        1849 :         && (their_fixed_length != fixed_trees->length ()
   17761        1819 :             || their_fixed_crc != global_crc))
   17762             :       {
   17763           0 :         error_at (loc, "fixed tree mismatch");
   17764           0 :         cfg.set_overrun ();
   17765           0 :         goto done;
   17766             :       }
   17767             :   }
   17768             : 
   17769             :   /* All non-partitions are interfaces.  */
   17770        1849 :   interface_p = !is_partition () || cfg.u ();
   17771             : 
   17772        1849 :   config.num_imports = cfg.u ();
   17773        1849 :   config.num_partitions = cfg.u ();
   17774        1849 :   config.num_entities = cfg.u ();
   17775             : 
   17776        1849 :   config.ordinary_locs = cfg.u ();
   17777        1849 :   config.macro_locs = cfg.u ();
   17778        1849 :   config.loc_range_bits = cfg.u ();
   17779             : 
   17780        1849 :   config.active_init = cfg.u ();
   17781             : 
   17782        1856 :  done:
   17783        1856 :   return cfg.end (from ());
   17784        1856 : }
   17785             : 
   17786             : /* Comparator for ordering the Ordered Ordinary Location array.  */
   17787             : 
   17788             : static int
   17789          80 : ool_cmp (const void *a_, const void *b_)
   17790             : {
   17791          80 :   auto *a = *static_cast<const module_state *const *> (a_);
   17792          80 :   auto *b = *static_cast<const module_state *const *> (b_);
   17793          80 :   if (a == b)
   17794             :     return 0;
   17795          80 :   else if (a->ordinary_locs.first < b->ordinary_locs.first)
   17796             :     return -1;
   17797             :   else
   17798          27 :     return +1;
   17799             : }
   17800             : 
   17801             : /* Use ELROND format to record the following sections:
   17802             :      qualified-names        : binding value(s)
   17803             :      MOD_SNAME_PFX.README   : human readable, strings
   17804             :      MOD_SNAME_PFX.ENV      : environment strings, strings
   17805             :      MOD_SNAME_PFX.nms      : namespace hierarchy
   17806             :      MOD_SNAME_PFX.bnd      : binding table
   17807             :      MOD_SNAME_PFX.spc      : specialization table
   17808             :      MOD_SNAME_PFX.imp      : import table
   17809             :      MOD_SNAME_PFX.ent      : entity table
   17810             :      MOD_SNAME_PFX.prt      : partitions table
   17811             :      MOD_SNAME_PFX.olm      : ordinary line maps
   17812             :      MOD_SNAME_PFX.mlm      : macro line maps
   17813             :      MOD_SNAME_PFX.def      : macro definitions
   17814             :      MOD_SNAME_PFX.mac      : macro index
   17815             :      MOD_SNAME_PFX.ini      : inits
   17816             :      MOD_SNAME_PFX.cnt      : counts
   17817             :      MOD_SNAME_PFX.cfg      : config data
   17818             : */
   17819             : 
   17820             : void
   17821        1660 : module_state::write_begin (elf_out *to, cpp_reader *reader,
   17822             :                            module_state_config &config, unsigned &crc)
   17823             : {
   17824             :   /* Figure out remapped module numbers, which might elide
   17825             :      partitions.  */
   17826        1660 :   bitmap partitions = NULL;
   17827        1660 :   if (!is_header () && !is_partition ())
   17828         930 :     partitions = BITMAP_GGC_ALLOC ();
   17829        1660 :   write_init_maps ();
   17830             : 
   17831        1660 :   unsigned mod_hwm = 1;
   17832        2070 :   for (unsigned ix = 1; ix != modules->length (); ix++)
   17833             :     {
   17834         410 :       module_state *imp = (*modules)[ix];
   17835             : 
   17836             :       /* Promote any non-partition direct import from a partition, unless
   17837             :          we're a partition.  */
   17838         380 :       if (!is_partition () && !imp->is_partition ()
   17839         697 :           && imp->is_partition_direct ())
   17840           6 :         imp->directness = MD_PURVIEW_DIRECT;
   17841             : 
   17842             :       /* Write any import that is not a partition, unless we're a
   17843             :          partition.  */
   17844         410 :       if (!partitions || !imp->is_partition ())
   17845         317 :         imp->remap = mod_hwm++;
   17846             :       else
   17847             :         {
   17848         123 :           dump () && dump ("Partition %M %u", imp, ix);
   17849          93 :           bitmap_set_bit (partitions, ix);
   17850          93 :           imp->remap = 0;
   17851             :           /* All interface partitions must be exported.  */
   17852          93 :           if (imp->is_interface () && !bitmap_bit_p (exports, imp->mod))
   17853             :             {
   17854           3 :               error_at (imp->loc, "interface partition is not exported");
   17855           3 :               bitmap_set_bit (exports, imp->mod);
   17856             :             }
   17857             : 
   17858             :           /* All the partition entities should have been loaded when
   17859             :              loading the partition.  */
   17860             :           if (CHECKING_P)
   17861         348 :             for (unsigned jx = 0; jx != imp->entity_num; jx++)
   17862             :               {
   17863         255 :                 binding_slot *slot = &(*entity_ary)[imp->entity_lwm + jx];
   17864         255 :                 gcc_checking_assert (!slot->is_lazy ());
   17865             :               }
   17866             :         }
   17867             : 
   17868         410 :       if (imp->is_direct () && (imp->remap || imp->is_partition ()))
   17869         403 :         note_location (imp->imported_from ());
   17870             :     }
   17871             : 
   17872        1660 :   if (partitions && bitmap_empty_p (partitions))
   17873             :     /* No partitions present.  */
   17874             :     partitions = nullptr;
   17875             : 
   17876             :   /* Find the set of decls we must write out.  */
   17877        1660 :   depset::hash table (DECL_NAMESPACE_BINDINGS (global_namespace)->size () * 8);
   17878             :   /* Add the specializations before the writables, so that we can
   17879             :      detect injected friend specializations.  */
   17880        1660 :   table.add_specializations (true);
   17881        1660 :   table.add_specializations (false);
   17882        1660 :   if (partial_specializations)
   17883             :     {
   17884          98 :       table.add_partial_entities (partial_specializations);
   17885          98 :       partial_specializations = NULL;
   17886             :     }
   17887        1660 :   table.add_namespace_entities (global_namespace, partitions);
   17888        1660 :   if (class_members)
   17889             :     {
   17890          12 :       table.add_class_entities (class_members);
   17891          12 :       class_members = NULL;
   17892             :     }
   17893             : 
   17894             :   /* Now join everything up.  */
   17895        1660 :   table.find_dependencies (this);
   17896             : 
   17897        1660 :   if (!table.finalize_dependencies ())
   17898             :     {
   17899           3 :       to->set_error ();
   17900           3 :       return;
   17901             :     }
   17902             : 
   17903             : #if CHECKING_P
   17904             :   /* We're done verifying at-most once reading, reset to verify
   17905             :      at-most once writing.  */
   17906        1657 :   note_defs = note_defs_table_t::create_ggc (1000);
   17907             : #endif
   17908             : 
   17909             :   /* Determine Strongy Connected Components.  */
   17910        1657 :   vec<depset *> sccs = table.connect ();
   17911             : 
   17912        1657 :   vec_alloc (ool, modules->length ());
   17913        2067 :   for (unsigned ix = modules->length (); --ix;)
   17914             :     {
   17915         410 :       auto *import = (*modules)[ix];
   17916         410 :       if (import->loadedness > ML_NONE
   17917         410 :           && !(partitions && bitmap_bit_p (partitions, import->mod)))
   17918         410 :         ool->quick_push (import);
   17919             :     }
   17920        1657 :   ool->qsort (ool_cmp);
   17921             : 
   17922        1657 :   vec<cpp_hashnode *> *macros = nullptr;
   17923        1657 :   if (is_header ())
   17924         643 :     macros = prepare_macros (reader);
   17925             : 
   17926        1657 :   config.num_imports = mod_hwm;
   17927        1657 :   config.num_partitions = modules->length () - mod_hwm;
   17928        1657 :   auto map_info = write_prepare_maps (&config, bool (config.num_partitions));
   17929        1657 :   unsigned counts[MSC_HWM];
   17930        1657 :   memset (counts, 0, sizeof (counts));
   17931             : 
   17932             :   /* depset::cluster is the cluster number,
   17933             :      depset::section is unspecified scratch value.
   17934             : 
   17935             :      The following loops make use of the tarjan property that
   17936             :      dependencies will be earlier in the SCCS array.  */
   17937             : 
   17938             :   /* This first loop determines the number of depsets in each SCC, and
   17939             :      also the number of namespaces we're dealing with.  During the
   17940             :      loop, the meaning of a couple of depset fields now change:
   17941             : 
   17942             :      depset::cluster -> size_of cluster, if first of cluster & !namespace
   17943             :      depset::section -> section number of cluster (if !namespace). */
   17944             : 
   17945        1657 :   unsigned n_spaces = 0;
   17946        1657 :   counts[MSC_sec_lwm] = counts[MSC_sec_hwm] = to->get_section_limit ();
   17947      307228 :   for (unsigned size, ix = 0; ix < sccs.length (); ix += size)
   17948             :     {
   17949      305571 :       depset **base = &sccs[ix];
   17950             : 
   17951      578408 :       if (base[0]->get_entity_kind () == depset::EK_NAMESPACE)
   17952             :         {
   17953        3503 :           n_spaces++;
   17954        3503 :           size = 1;
   17955             :         }
   17956             :       else
   17957             :         {
   17958             :           /* Count the members in this cluster.  */
   17959     1207322 :           for (size = 1; ix + size < sccs.length (); size++)
   17960     1205878 :             if (base[size]->cluster != base[0]->cluster)
   17961             :               break;
   17962             : 
   17963     1509390 :           for (unsigned jx = 0; jx != size; jx++)
   17964             :             {
   17965             :               /* Set the section number.  */
   17966     1207322 :               base[jx]->cluster = ~(~0u >> 1); /* A bad value.  */
   17967     1207322 :               base[jx]->section = counts[MSC_sec_hwm];
   17968             :             }
   17969             : 
   17970             :           /* Save the size in the first member's cluster slot.  */
   17971      302068 :           base[0]->cluster = size;
   17972             : 
   17973      302068 :           counts[MSC_sec_hwm]++;
   17974             :         }
   17975             :     }
   17976             : 
   17977             :   /* Write the clusters.  Namespace decls are put in the spaces array.
   17978             :      The meaning of depset::cluster changes to provide the
   17979             :      unnamed-decl count of the depset's decl (and remains zero for
   17980             :      non-decls and non-unnamed).  */
   17981        1657 :   unsigned bytes = 0;
   17982        1657 :   vec<depset *> spaces;
   17983        1657 :   spaces.create (n_spaces);
   17984             : 
   17985      307228 :   for (unsigned size, ix = 0; ix < sccs.length (); ix += size)
   17986             :     {
   17987      305571 :       depset **base = &sccs[ix];
   17988             : 
   17989      305571 :       if (base[0]->get_entity_kind () == depset::EK_NAMESPACE)
   17990             :         {
   17991        3503 :           tree decl = base[0]->get_entity ();
   17992        3503 :           if (decl == global_namespace)
   17993        1468 :             base[0]->cluster = 0;
   17994        2035 :           else if (!base[0]->is_import ())
   17995             :             {
   17996        2035 :               base[0]->cluster = counts[MSC_entities]++;
   17997        2035 :               spaces.quick_push (base[0]);
   17998        2035 :               counts[MSC_namespaces]++;
   17999        2035 :               if (CHECKING_P)
   18000             :                 {
   18001             :                   /* Add it to the entity map, such that we can tell it is
   18002             :                      part of us.  */
   18003        2035 :                   bool existed;
   18004        2035 :                   unsigned *slot = &entity_map->get_or_insert
   18005        2035 :                     (DECL_UID (decl), &existed);
   18006        2035 :                   if (existed)
   18007             :                     /* It must have come from a partition.  */
   18008           9 :                     gcc_checking_assert
   18009             :                       (import_entity_module (*slot)->is_partition ());
   18010        2035 :                   *slot = ~base[0]->cluster;
   18011             :                 }
   18012      307627 :               dump (dumper::CLUSTER) && dump ("Cluster namespace %N", decl);
   18013             :             }
   18014             :           size = 1;
   18015             :         }
   18016             :       else
   18017             :         {
   18018      302068 :           size = base[0]->cluster;
   18019             : 
   18020             :           /* Cluster is now used to number entities.  */
   18021      302068 :           base[0]->cluster = ~(~0u >> 1); /* A bad value.  */
   18022             : 
   18023      302068 :           sort_cluster (&table, base, size);
   18024             : 
   18025             :           /* Record the section for consistency checking during stream
   18026             :              out -- we don't want to start writing decls in different
   18027             :              sections.  */
   18028      302068 :           table.section = base[0]->section;
   18029      302068 :           bytes += write_cluster (to, base, size, table, counts, &crc);
   18030      302068 :           table.section = 0;
   18031             :         }
   18032             :     }
   18033             : 
   18034             :   /* depset::cluster - entity number (on entities)
   18035             :      depset::section - cluster number  */
   18036             :   /* We'd better have written as many sections and found as many
   18037             :      namespaces as we predicted.  */
   18038        3314 :   gcc_assert (counts[MSC_sec_hwm] == to->get_section_limit ()
   18039             :               && spaces.length () == counts[MSC_namespaces]);
   18040             : 
   18041             :   /* Write the entitites.  None happens if we contain namespaces or
   18042             :      nothing. */
   18043        1657 :   config.num_entities = counts[MSC_entities];
   18044        1657 :   if (counts[MSC_entities])
   18045        1474 :     write_entities (to, sccs, counts[MSC_entities], &crc);
   18046             : 
   18047             :   /* Write the namespaces.  */
   18048        1657 :   if (counts[MSC_namespaces])
   18049         295 :     write_namespaces (to, spaces, counts[MSC_namespaces], &crc);
   18050             : 
   18051             :   /* Write the bindings themselves.  */
   18052        1657 :   counts[MSC_bindings] = write_bindings (to, sccs, &crc);
   18053             : 
   18054             :   /* Write the unnamed.  */
   18055        1657 :   counts[MSC_pendings] = write_pendings (to, sccs, table, &crc);
   18056             : 
   18057             :   /* Write the import table.  */
   18058        1657 :   if (config.num_imports > 1)
   18059         297 :     write_imports (to, &crc);
   18060             : 
   18061             :   /* Write elided partition table.  */
   18062        1657 :   if (config.num_partitions)
   18063          63 :     write_partitions (to, config.num_partitions, &crc);
   18064             : 
   18065             :   /* Write the line maps.  */
   18066        1657 :   if (config.ordinary_locs)
   18067        1597 :     write_ordinary_maps (to, map_info, bool (config.num_partitions), &crc);
   18068        1657 :   if (config.macro_locs)
   18069         113 :     write_macro_maps (to, map_info, &crc);
   18070             : 
   18071        1657 :   if (is_header ())
   18072             :     {
   18073         643 :       counts[MSC_macros] = write_macros (to, macros, &crc);
   18074         643 :       counts[MSC_inits] = write_inits (to, table, &crc);
   18075         643 :       vec_free (macros);
   18076             :     }
   18077             : 
   18078        1657 :   unsigned clusters = counts[MSC_sec_hwm] - counts[MSC_sec_lwm];
   18079        1657 :   dump () && dump ("Wrote %u clusters, average %u bytes/cluster",
   18080         225 :                    clusters, (bytes + clusters / 2) / (clusters + !clusters));
   18081        1657 :   trees_out::instrument ();
   18082             : 
   18083        1657 :   write_counts (to, counts, &crc);
   18084             : 
   18085        1657 :   spaces.release ();
   18086        1657 :   sccs.release ();
   18087             : 
   18088        1657 :   vec_free (macro_loc_remap);
   18089        1657 :   vec_free (ord_loc_remap);
   18090        1657 :   vec_free (ool);
   18091             : 
   18092             :   // FIXME:QOI:  Have a command line switch to control more detailed
   18093             :   // information (which might leak data you do not want to leak).
   18094             :   // Perhaps (some of) the write_readme contents should also be
   18095             :   // so-controlled.
   18096        1657 :   if (false)
   18097             :     write_env (to);
   18098        1660 : }
   18099             : 
   18100             : // Finish module writing after we've emitted all dynamic initializers. 
   18101             : 
   18102             : void
   18103        1660 : module_state::write_end (elf_out *to, cpp_reader *reader,
   18104             :                          module_state_config &config, unsigned &crc)
   18105             : {
   18106             :   /* And finish up.  */
   18107        1660 :   write_config (to, config, crc);
   18108             : 
   18109             :   /* Human-readable info.  */
   18110        1660 :   write_readme (to, reader, config.dialect_str);
   18111             : 
   18112        1885 :   dump () && dump ("Wrote %u sections", to->get_section_limit ());
   18113        1660 : }
   18114             : 
   18115             : /* Initial read of a CMI.  Checks config, loads up imports and line
   18116             :    maps.  */
   18117             : 
   18118             : bool
   18119        1856 : module_state::read_initial (cpp_reader *reader)
   18120             : {
   18121        1856 :   module_state_config config;
   18122        1856 :   bool ok = true;
   18123             : 
   18124        1856 :   if (ok && !from ()->begin (loc))
   18125             :     ok = false;
   18126             : 
   18127        1856 :   if (ok && !read_config (config))
   18128             :     ok = false;
   18129             : 
   18130        1849 :   bool have_locs = ok && read_prepare_maps (&config);
   18131             : 
   18132             :   /* Ordinary maps before the imports.  */
   18133        1847 :   if (!(have_locs && config.ordinary_locs))
   18134          69 :     ordinary_locs.first = line_table->highest_location + 1;
   18135        1787 :   else if (!read_ordinary_maps (config.ordinary_locs, config.loc_range_bits))
   18136             :     ok = false;
   18137             : 
   18138             :   /* Allocate the REMAP vector.  */
   18139        1856 :   slurp->alloc_remap (config.num_imports);
   18140             : 
   18141        1856 :   if (ok)
   18142             :     {
   18143             :       /* Read the import table.  Decrement current to stop this CMI
   18144             :          from being evicted during the import. */
   18145        1849 :       slurp->current--;
   18146        1849 :       if (config.num_imports > 1 && !read_imports (reader, line_table))
   18147             :         ok = false;
   18148        1849 :       slurp->current++;
   18149             :     }
   18150             : 
   18151             :   /* Read the elided partition table, if we're the primary partition.  */
   18152        1849 :   if (ok && config.num_partitions && is_module ()
   18153        1861 :       && !read_partitions (config.num_partitions))
   18154             :     ok = false;
   18155             : 
   18156             :   /* Determine the module's number.  */
   18157        1856 :   gcc_checking_assert (mod == MODULE_UNKNOWN);
   18158        1856 :   gcc_checking_assert (this != (*modules)[0]);
   18159             : 
   18160        1856 :   {
   18161             :     /* Allocate space in the entities array now -- that array must be
   18162             :        monotonically in step with the modules array.  */
   18163        1856 :     entity_lwm = vec_safe_length (entity_ary);
   18164        1856 :     entity_num = config.num_entities;
   18165        1856 :     gcc_checking_assert (modules->length () == 1
   18166             :                          || modules->last ()->entity_lwm <= entity_lwm);
   18167        1856 :     vec_safe_reserve (entity_ary, config.num_entities);
   18168             : 
   18169        1856 :     binding_slot slot;
   18170        1856 :     slot.u.binding = NULL_TREE;
   18171     1435527 :     for (unsigned count = config.num_entities; count--;)
   18172     1433671 :       entity_ary->quick_push (slot);
   18173             :   }
   18174             : 
   18175             :   /* We'll run out of other resources before we run out of module
   18176             :      indices.  */
   18177        1856 :   mod = modules->length ();
   18178        1856 :   vec_safe_push (modules, this);
   18179             : 
   18180             :   /* We always import and export ourselves. */
   18181        1856 :   bitmap_set_bit (imports, mod);
   18182        1856 :   bitmap_set_bit (exports, mod);
   18183             : 
   18184        1856 :   if (ok)
   18185        1849 :     (*slurp->remap)[0] = mod << 1;
   18186        2300 :   dump () && dump ("Assigning %M module number %u", this, mod);
   18187             : 
   18188             :   /* We should not have been frozen during the importing done by
   18189             :      read_config.  */
   18190        1856 :   gcc_assert (!from ()->is_frozen ());
   18191             : 
   18192             :   /* Macro maps after the imports.  */
   18193        1856 :   if (!(ok && have_locs && config.macro_locs))
   18194        3447 :     macro_locs.first = LINEMAPS_MACRO_LOWEST_LOCATION (line_table);
   18195         131 :   else if (!read_macro_maps (config.macro_locs))
   18196             :     ok = false;
   18197             : 
   18198             :   /* Note whether there's an active initializer.  */
   18199        1856 :   active_init_p = !is_header () && bool (config.active_init);
   18200             : 
   18201        1856 :   gcc_assert (slurp->current == ~0u);
   18202        1856 :   return ok;
   18203             : }
   18204             : 
   18205             : /* Read a preprocessor state.  */
   18206             : 
   18207             : bool
   18208         680 : module_state::read_preprocessor (bool outermost)
   18209             : {
   18210         680 :   gcc_checking_assert (is_header () && slurp
   18211             :                        && slurp->remap_module (0) == mod);
   18212             : 
   18213         680 :   if (loadedness == ML_PREPROCESSOR)
   18214           6 :     return !(from () && from ()->get_error ());
   18215             : 
   18216         674 :   bool ok = true;
   18217             : 
   18218             :   /* Read direct header imports.  */
   18219         674 :   unsigned len = slurp->remap->length ();
   18220         703 :   for (unsigned ix = 1; ok && ix != len; ix++)
   18221             :     {
   18222          29 :       unsigned map = (*slurp->remap)[ix];
   18223          29 :       if (map & 1)
   18224             :         {
   18225          29 :           module_state *import = (*modules)[map >> 1];
   18226          29 :           if (import->is_header ())
   18227             :             {
   18228          29 :               ok = import->read_preprocessor (false);
   18229          29 :               bitmap_ior_into (slurp->headers, import->slurp->headers);
   18230             :             }
   18231             :         }
   18232             :     }
   18233             : 
   18234             :   /* Record as a direct header.  */
   18235         674 :   if (ok)
   18236         674 :     bitmap_set_bit (slurp->headers, mod);
   18237             : 
   18238         674 :   if (ok && !read_macros ())
   18239             :     ok = false;
   18240             : 
   18241         674 :   loadedness = ML_PREPROCESSOR;
   18242         674 :   announce ("macros");
   18243             : 
   18244         674 :   if (flag_preprocess_only)
   18245             :     /* We're done with the string table.  */
   18246          30 :     from ()->release ();
   18247             : 
   18248         674 :   return check_read (outermost, ok);
   18249             : }
   18250             : 
   18251             : /* Read language state.  */
   18252             : 
   18253             : bool
   18254        1860 : module_state::read_language (bool outermost)
   18255             : {
   18256        1860 :   gcc_checking_assert (!lazy_snum);
   18257             : 
   18258        1860 :   if (loadedness == ML_LANGUAGE)
   18259          53 :     return !(slurp && from () && from ()->get_error ());
   18260             : 
   18261        1807 :   gcc_checking_assert (slurp && slurp->current == ~0u
   18262             :                        && slurp->remap_module (0) == mod);
   18263             : 
   18264        1807 :   bool ok = true;
   18265             : 
   18266             :   /* Read direct imports.  */
   18267        1807 :   unsigned len = slurp->remap->length ();
   18268        2058 :   for (unsigned ix = 1; ok && ix != len; ix++)
   18269             :     {
   18270         251 :       unsigned map = (*slurp->remap)[ix];
   18271         251 :       if (map & 1)
   18272             :         {
   18273         251 :           module_state *import = (*modules)[map >> 1];
   18274         251 :           if (!import->read_language (false))
   18275         251 :             ok = false;
   18276             :         }
   18277             :     }
   18278             : 
   18279        1807 :   unsigned counts[MSC_HWM];
   18280             : 
   18281        1807 :   if (ok && !read_counts (counts))
   18282             :     ok = false;
   18283             : 
   18284        1807 :   function_depth++; /* Prevent unexpected GCs.  */
   18285             : 
   18286        1807 :   if (ok && counts[MSC_entities] != entity_num)
   18287             :     ok = false;
   18288        1807 :   if (ok && counts[MSC_entities]
   18289        1618 :       && !read_entities (counts[MSC_entities],
   18290             :                          counts[MSC_sec_lwm], counts[MSC_sec_hwm]))
   18291             :     ok = false;
   18292             : 
   18293             :   /* Read the namespace hierarchy. */
   18294        1807 :   if (ok && counts[MSC_namespaces]
   18295        2125 :       && !read_namespaces (counts[MSC_namespaces]))
   18296             :     ok = false;
   18297             : 
   18298        1807 :   if (ok && !read_bindings (counts[MSC_bindings],
   18299             :                             counts[MSC_sec_lwm], counts[MSC_sec_hwm]))
   18300             :     ok = false;
   18301             : 
   18302             :   /* And unnamed.  */
   18303        1807 :   if (ok && counts[MSC_pendings] && !read_pendings (counts[MSC_pendings]))
   18304             :     ok = false;
   18305             : 
   18306        1807 :   if (ok)
   18307             :     {
   18308        1807 :       slurp->remaining = counts[MSC_sec_hwm] - counts[MSC_sec_lwm];
   18309        1807 :       available_clusters += counts[MSC_sec_hwm] - counts[MSC_sec_lwm];
   18310             :     }
   18311             : 
   18312        1807 :   if (!flag_module_lazy
   18313        1807 :       || (is_partition ()
   18314         126 :           && module_interface_p ()
   18315         111 :           && !module_partition_p ()))
   18316             :     {
   18317             :       /* Read the sections in forward order, so that dependencies are read
   18318             :          first.  See note about tarjan_connect.  */
   18319         357 :       ggc_collect ();
   18320             : 
   18321         357 :       lazy_snum = ~0u;
   18322             : 
   18323         357 :       unsigned hwm = counts[MSC_sec_hwm];
   18324      313139 :       for (unsigned ix = counts[MSC_sec_lwm]; ok && ix != hwm; ix++)
   18325      312782 :         if (!load_section (ix, NULL))
   18326             :           {
   18327             :             ok = false;
   18328             :             break;
   18329             :           }
   18330         357 :       lazy_snum = 0;
   18331         357 :       post_load_processing ();
   18332             : 
   18333         357 :       ggc_collect ();
   18334             : 
   18335         357 :       if (ok && CHECKING_P)
   18336     1103155 :         for (unsigned ix = 0; ix != entity_num; ix++)
   18337     1102798 :           gcc_assert (!(*entity_ary)[ix + entity_lwm].is_lazy ());
   18338             :     }
   18339             : 
   18340             :   // If the import is a header-unit, we need to register initializers
   18341             :   // of any static objects it contains (looking at you _Ioinit).
   18342             :   // Notice, the ordering of these initializers will be that of a
   18343             :   // dynamic initializer at this point in the current TU.  (Other
   18344             :   // instances of these objects in other TUs will be initialized as
   18345             :   // part of that TU's global initializers.)
   18346        1807 :   if (ok && counts[MSC_inits] && !read_inits (counts[MSC_inits]))
   18347             :     ok = false;
   18348             : 
   18349        1807 :   function_depth--;
   18350             :   
   18351        2071 :   announce (flag_module_lazy ? "lazy" : "imported");
   18352        1807 :   loadedness = ML_LANGUAGE;
   18353             : 
   18354        1807 :   gcc_assert (slurp->current == ~0u);
   18355             : 
   18356             :   /* We're done with the string table.  */
   18357        1807 :   from ()->release ();
   18358             : 
   18359        1807 :   return check_read (outermost, ok);
   18360             : }
   18361             : 
   18362             : bool
   18363      329672 : module_state::maybe_defrost ()
   18364             : {
   18365      329672 :   bool ok = true;
   18366      329672 :   if (from ()->is_frozen ())
   18367             :     {
   18368           9 :       if (lazy_open >= lazy_limit)
   18369           3 :         freeze_an_elf ();
   18370          18 :       dump () && dump ("Defrosting '%s'", filename);
   18371           9 :       ok = from ()->defrost (maybe_add_cmi_prefix (filename));
   18372           9 :       lazy_open++;
   18373             :     }
   18374             : 
   18375      329672 :   return ok;
   18376             : }
   18377             : 
   18378             : /* Load section SNUM, dealing with laziness.  It doesn't matter if we
   18379             :    have multiple concurrent loads, because we do not use TREE_VISITED
   18380             :    when reading back in.  */
   18381             : 
   18382             : bool
   18383      329672 : module_state::load_section (unsigned snum, binding_slot *mslot)
   18384             : {
   18385      329672 :   if (from ()->get_error ())
   18386             :     return false;
   18387             : 
   18388      329672 :   if (snum >= slurp->current)
   18389           0 :     from ()->set_error (elf::E_BAD_LAZY);
   18390      329672 :   else if (maybe_defrost ())
   18391             :     {
   18392      329672 :       unsigned old_current = slurp->current;
   18393      329672 :       slurp->current = snum;
   18394      329672 :       slurp->lru = 0;  /* Do not swap out.  */
   18395      329672 :       slurp->remaining--;
   18396      329672 :       read_cluster (snum);
   18397      329672 :       slurp->lru = ++lazy_lru;
   18398      329672 :       slurp->current = old_current;
   18399             :     }
   18400             :   
   18401      329672 :   if (mslot && mslot->is_lazy ())
   18402             :     {
   18403             :       /* Oops, the section didn't set this slot.  */
   18404           0 :       from ()->set_error (elf::E_BAD_DATA);
   18405           0 :       *mslot = NULL_TREE;
   18406             :     }
   18407             : 
   18408      329672 :   bool ok = !from ()->get_error ();
   18409      329672 :   if (!ok)
   18410             :     {
   18411           0 :       error_at (loc, "failed to read compiled module cluster %u: %s",
   18412           0 :                 snum, from ()->get_error (filename));
   18413           0 :       note_cmi_name ();
   18414             :     }
   18415             : 
   18416      329672 :   maybe_completed_reading ();
   18417             : 
   18418      329672 :   return ok;
   18419             : }
   18420             : 
   18421             : void
   18422      333999 : module_state::maybe_completed_reading ()
   18423             : {
   18424      333999 :   if (loadedness == ML_LANGUAGE && slurp->current == ~0u && !slurp->remaining)
   18425             :     {
   18426        1522 :       lazy_open--;
   18427             :       /* We no longer need the macros, all tokenizing has been done.  */
   18428        1522 :       slurp->release_macros ();
   18429             : 
   18430        1522 :       from ()->end ();
   18431        1522 :       slurp->close ();
   18432        1522 :       slurped ();
   18433             :     }
   18434      333999 : }
   18435             : 
   18436             : /* After a reading operation, make sure things are still ok.  If not,
   18437             :    emit an error and clean up.  */
   18438             : 
   18439             : bool
   18440        4355 : module_state::check_read (bool outermost, bool ok)
   18441             : {
   18442        4355 :   gcc_checking_assert (!outermost || slurp->current == ~0u);
   18443             : 
   18444        4355 :   if (!ok)
   18445           7 :     from ()->set_error ();
   18446             : 
   18447        4355 :   if (int e = from ()->get_error ())
   18448             :     {
   18449          31 :       error_at (loc, "failed to read compiled module: %s",
   18450          31 :                 from ()->get_error (filename));
   18451          31 :       note_cmi_name ();
   18452             : 
   18453          31 :       if (e == EMFILE
   18454          31 :           || e == ENFILE
   18455             : #if MAPPED_READING
   18456          31 :           || e == ENOMEM
   18457             : #endif
   18458             :           || false)
   18459           0 :         inform (loc, "consider using %<-fno-module-lazy%>,"
   18460             :                 " increasing %<-param-lazy-modules=%u%> value,"
   18461             :                 " or increasing the per-process file descriptor limit",
   18462             :                 param_lazy_modules);
   18463          31 :       else if (e == ENOENT)
   18464          18 :         inform (loc, "imports must be built before being imported");
   18465             : 
   18466          31 :       if (outermost)
   18467          28 :         fatal_error (loc, "returning to the gate for a mechanical issue");
   18468             : 
   18469             :       ok = false;
   18470             :     }
   18471             : 
   18472        4327 :   maybe_completed_reading ();
   18473             : 
   18474        4327 :   return ok;
   18475             : }
   18476             : 
   18477             : /* Return the IDENTIFIER_NODE naming module IX.  This is the name
   18478             :    including dots.  */
   18479             : 
   18480             : char const *
   18481         210 : module_name (unsigned ix, bool header_ok)
   18482             : {
   18483         210 :   if (modules)
   18484             :     {
   18485         210 :       module_state *imp = (*modules)[ix];
   18486             : 
   18487         210 :       if (ix && !imp->name)
   18488           0 :         imp = imp->parent;
   18489             : 
   18490         210 :       if (header_ok || !imp->is_header ())
   18491         150 :         return imp->get_flatname ();
   18492             :     }
   18493             : 
   18494             :   return NULL;
   18495             : }
   18496             : 
   18497             : /* Return the bitmap describing what modules are imported.  Remember,
   18498             :    we always import ourselves.  */
   18499             : 
   18500             : bitmap
   18501       68963 : get_import_bitmap ()
   18502             : {
   18503       68963 :   return (*modules)[0]->imports;
   18504             : }
   18505             : 
   18506             : /* Return the visible imports and path of instantiation for an
   18507             :    instantiation at TINST.  If TINST is nullptr, we're not in an
   18508             :    instantiation, and thus will return the visible imports of the
   18509             :    current TU (and NULL *PATH_MAP_P).   We cache the information on
   18510             :    the tinst level itself.  */
   18511             : 
   18512             : static bitmap
   18513       69728 : path_of_instantiation (tinst_level *tinst,  bitmap *path_map_p)
   18514             : {
   18515       69728 :   gcc_checking_assert (modules_p ());
   18516             : 
   18517       69728 :   if (!tinst)
   18518             :     {
   18519             :       /* Not inside an instantiation, just the regular case.  */
   18520       49718 :       *path_map_p = nullptr;
   18521       49718 :       return get_import_bitmap ();
   18522             :     }
   18523             : 
   18524       20010 :   if (!tinst->path)
   18525             :     {
   18526             :       /* Calculate.  */
   18527        6634 :       bitmap visible = path_of_instantiation (tinst->next, path_map_p);
   18528        6634 :       bitmap path_map = *path_map_p;
   18529             : 
   18530        6634 :       if (!path_map)
   18531             :         {
   18532        2135 :           path_map = BITMAP_GGC_ALLOC ();
   18533        2135 :           bitmap_set_bit (path_map, 0);
   18534             :         }
   18535             : 
   18536        6634 :       tree decl = tinst->tldcl;
   18537        6634 :       if (TREE_CODE (decl) == TREE_LIST)
   18538           0 :         decl = TREE_PURPOSE (decl);
   18539        6634 :       if (TYPE_P (decl))
   18540         826 :         decl = TYPE_NAME (decl);
   18541             : 
   18542        6634 :       if (unsigned mod = get_originating_module (decl))
   18543         618 :         if (!bitmap_bit_p (path_map, mod))
   18544             :           {
   18545             :             /* This is brand new information!  */
   18546         105 :             bitmap new_path = BITMAP_GGC_ALLOC ();
   18547         105 :             bitmap_copy (new_path, path_map);
   18548         105 :             bitmap_set_bit (new_path, mod);
   18549         105 :             path_map = new_path;
   18550             : 
   18551         105 :             bitmap imports = (*modules)[mod]->imports;
   18552         105 :             if (bitmap_intersect_compl_p (imports, visible))
   18553             :               {
   18554             :                 /* IMPORTS contains additional modules to VISIBLE.  */
   18555           6 :                 bitmap new_visible = BITMAP_GGC_ALLOC ();
   18556             : 
   18557           6 :                 bitmap_ior (new_visible, visible, imports);
   18558           6 :                 visible = new_visible;
   18559             :               }
   18560             :           }
   18561             : 
   18562        6634 :       tinst->path = path_map;
   18563        6634 :       tinst->visible = visible;
   18564             :     }
   18565             : 
   18566       20010 :   *path_map_p = tinst->path;
   18567       20010 :   return tinst->visible;
   18568             : }
   18569             : 
   18570             : /* Return the bitmap describing what modules are visible along the
   18571             :    path of instantiation.  If we're not an instantiation, this will be
   18572             :    the visible imports of the TU.  *PATH_MAP_P is filled in with the
   18573             :    modules owning the instantiation path -- we see the module-linkage
   18574             :    entities of those modules.  */
   18575             : 
   18576             : bitmap
   18577     9338320 : visible_instantiation_path (bitmap *path_map_p)
   18578             : {
   18579     9338320 :   if (!modules_p ())
   18580             :     return NULL;
   18581             : 
   18582       63094 :   return path_of_instantiation (current_instantiation (), path_map_p);
   18583             : }
   18584             : 
   18585             : /* We've just directly imported IMPORT.  Update our import/export
   18586             :    bitmaps.  IS_EXPORT is true if we're reexporting the OTHER.  */
   18587             : 
   18588             : void
   18589        1912 : module_state::set_import (module_state const *import, bool is_export)
   18590             : {
   18591        1912 :   gcc_checking_assert (this != import);
   18592             : 
   18593             :   /* We see IMPORT's exports (which includes IMPORT).  If IMPORT is
   18594             :      the primary interface or a partition we'll see its imports.  */
   18595        1912 :   bitmap_ior_into (imports, import->is_module () || import->is_partition ()
   18596             :                    ? import->imports : import->exports);
   18597             : 
   18598        1912 :   if (is_export)
   18599             :     /* We'll export OTHER's exports.  */
   18600         291 :     bitmap_ior_into (exports, import->exports);
   18601        1912 : }
   18602             : 
   18603             : /* Return the declaring entity of DECL.  That is the decl determining
   18604             :    how to decorate DECL with module information.  Returns NULL_TREE if
   18605             :    it's the global module.  */
   18606             : 
   18607             : tree
   18608   108259220 : get_originating_module_decl (tree decl)
   18609             : {
   18610             :   /* An enumeration constant.  */
   18611   108259220 :   if (TREE_CODE (decl) == CONST_DECL
   18612        8078 :       && DECL_CONTEXT (decl)
   18613   108267298 :       && (TREE_CODE (DECL_CONTEXT (decl)) == ENUMERAL_TYPE))
   18614        8078 :     decl = TYPE_NAME (DECL_CONTEXT (decl));
   18615   108251142 :   else if (TREE_CODE (decl) == FIELD_DECL
   18616   108247701 :            || TREE_CODE (decl) == USING_DECL)
   18617             :     {
   18618        7854 :       decl = DECL_CONTEXT (decl);
   18619        7854 :       if (TREE_CODE (decl) != FUNCTION_DECL)
   18620        7854 :         decl = TYPE_NAME (decl);
   18621             :     }
   18622             : 
   18623   108259220 :   gcc_checking_assert (TREE_CODE (decl) == TEMPLATE_DECL
   18624             :                        || TREE_CODE (decl) == FUNCTION_DECL
   18625             :                        || TREE_CODE (decl) == TYPE_DECL
   18626             :                        || TREE_CODE (decl) == VAR_DECL
   18627             :                        || TREE_CODE (decl) == CONCEPT_DECL
   18628             :                        || TREE_CODE (decl) == NAMESPACE_DECL);
   18629             : 
   18630   108788474 :   for (;;)
   18631             :     {
   18632             :       /* Uninstantiated template friends are owned by the befriending
   18633             :          class -- not their context.  */
   18634   108523847 :       if (TREE_CODE (decl) == TEMPLATE_DECL
   18635   108523847 :           && DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (decl))
   18636        1553 :         decl = TYPE_NAME (DECL_CHAIN (decl));
   18637             : 
   18638   108523847 :       int use;
   18639   108523847 :       if (tree ti = node_template_info (decl, use))
   18640             :         {
   18641      223597 :           decl = TI_TEMPLATE (ti);
   18642      223597 :           if (TREE_CODE (decl) != TEMPLATE_DECL)
   18643             :             {
   18644             :               /* A friend template specialization.  */
   18645           9 :               gcc_checking_assert (OVL_P (decl));
   18646           9 :               return global_namespace;
   18647             :             }
   18648             :         }
   18649             :       else
   18650             :         {
   18651   108300250 :           tree ctx = CP_DECL_CONTEXT (decl);
   18652   108300250 :           if (TREE_CODE (ctx) == NAMESPACE_DECL)
   18653             :             break;
   18654             : 
   18655       41039 :           if (TYPE_P (ctx))
   18656             :             {
   18657       40630 :               ctx = TYPE_NAME (ctx);
   18658       40630 :               if (!ctx)
   18659             :                 {
   18660             :                   /* Some kind of internal type.  */
   18661           0 :                   gcc_checking_assert (DECL_ARTIFICIAL (decl));
   18662           0 :                   return global_namespace;
   18663             :                 }
   18664             :             }
   18665             :           decl = ctx;
   18666             :         }
   18667      264627 :     }
   18668             : 
   18669   108259211 :   return decl;
   18670             : }
   18671             : 
   18672             : int
   18673      649914 : get_originating_module (tree decl, bool for_mangle)
   18674             : {
   18675      649914 :   tree owner = get_originating_module_decl (decl);
   18676      649914 :   tree not_tmpl = STRIP_TEMPLATE (owner);
   18677             : 
   18678      649914 :   if (!DECL_LANG_SPECIFIC (not_tmpl))
   18679      134080 :     return for_mangle ? -1 : 0;
   18680             : 
   18681      977626 :   if (for_mangle && !DECL_MODULE_ATTACH_P (not_tmpl))
   18682             :     return -1;
   18683             : 
   18684      138208 :   int mod = !DECL_MODULE_IMPORT_P (not_tmpl) ? 0 : get_importing_module (owner);
   18685      138208 :   gcc_checking_assert (!for_mangle || !(*modules)[mod]->is_header ());
   18686             :   return mod;
   18687             : }
   18688             : 
   18689             : unsigned
   18690        4354 : get_importing_module (tree decl, bool flexible)
   18691             : {
   18692        4354 :   unsigned index = import_entity_index (decl, flexible);
   18693        4354 :   if (index == ~(~0u >> 1))
   18694             :     return -1;
   18695        4354 :   module_state *module = import_entity_module (index);
   18696             : 
   18697        4354 :   return module->mod;
   18698             : }
   18699             : 
   18700             : /* Is it permissible to redeclare DECL.  */
   18701             : 
   18702             : bool
   18703      166531 : module_may_redeclare (tree decl)
   18704             : {
   18705      185085 :   for (;;)
   18706             :     {
   18707      175808 :       tree ctx = CP_DECL_CONTEXT (decl);
   18708      175808 :       if (TREE_CODE (ctx) == NAMESPACE_DECL)
   18709             :         // Found the namespace-scope decl.
   18710             :         break;
   18711       14903 :       if (!CLASS_TYPE_P (ctx))
   18712             :         // We've met a non-class scope.  Such a thing is not
   18713             :         // reopenable, so we must be ok.
   18714             :         return true;
   18715        9277 :       decl = TYPE_NAME (ctx);
   18716        9277 :     }
   18717             : 
   18718      160905 :   tree not_tmpl = STRIP_TEMPLATE (decl);
   18719             : 
   18720      160905 :   int use_tpl = 0;
   18721      160905 :   if (node_template_info (not_tmpl, use_tpl) && use_tpl)
   18722             :     // Specializations of any kind can be redeclared anywhere.
   18723             :     // FIXME: Should we be checking this in more places on the scope chain?
   18724             :     return true;
   18725             : 
   18726      167060 :   if (!DECL_LANG_SPECIFIC (not_tmpl) || !DECL_MODULE_ATTACH_P (not_tmpl))
   18727             :     // Decl is attached to global module.  Current scope needs to be too.
   18728      102341 :     return !module_attach_p ();
   18729             : 
   18730         641 :   module_state *me = (*modules)[0];
   18731         641 :   module_state *them = me;
   18732             : 
   18733         641 :   if (DECL_LANG_SPECIFIC (not_tmpl) && DECL_MODULE_IMPORT_P (not_tmpl))
   18734             :     {
   18735             :       /* We can be given the TEMPLATE_RESULT.  We want the
   18736             :          TEMPLATE_DECL.  */
   18737         117 :       int use_tpl = -1;
   18738         117 :       if (tree ti = node_template_info (decl, use_tpl))
   18739             :         {
   18740           9 :           tree tmpl = TI_TEMPLATE (ti);
   18741           9 :           if (use_tpl == 2)
   18742             :             {
   18743             :               /* A partial specialization.  Find that specialization's
   18744             :                  template_decl.  */
   18745           0 :               for (tree list = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
   18746           0 :                    list; list = TREE_CHAIN (list))
   18747           0 :                 if (DECL_TEMPLATE_RESULT (TREE_VALUE (list)) == decl)
   18748             :                   {
   18749             :                     decl = TREE_VALUE (list);
   18750             :                     break;
   18751             :                 }
   18752             :             }
   18753           9 :           else if (DECL_TEMPLATE_RESULT (tmpl) == decl)
   18754         117 :             decl = tmpl;
   18755             :         }
   18756         117 :       unsigned index = import_entity_index (decl);
   18757         117 :       them = import_entity_module (index);
   18758             :     }
   18759             : 
   18760             :   // Decl is attached to named module.  Current scope needs to be
   18761             :   // attaching to the same module.
   18762         641 :   if (!module_attach_p ())
   18763             :     return false;
   18764             : 
   18765             :   // Both attached to named module.
   18766         632 :   if (me == them)
   18767             :     return true;
   18768             : 
   18769         324 :   return me && get_primary (them) == get_primary (me);
   18770             : }
   18771             : 
   18772             : /* DECL is being created by this TU.  Record it came from here.  We
   18773             :    record module purview, so we can see if partial or explicit
   18774             :    specialization needs to be written out, even though its purviewness
   18775             :    comes from the most general template.  */
   18776             : 
   18777             : void
   18778   526145241 : set_instantiating_module (tree decl)
   18779             : {
   18780   526145241 :   gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
   18781             :               || VAR_P (decl)
   18782             :               || TREE_CODE (decl) == TYPE_DECL
   18783             :               || TREE_CODE (decl) == CONCEPT_DECL
   18784             :               || TREE_CODE (decl) == TEMPLATE_DECL
   18785             :               || (TREE_CODE (decl) == NAMESPACE_DECL
   18786             :                   && DECL_NAMESPACE_ALIAS (decl)));
   18787             : 
   18788   526145241 :   if (!modules_p ())
   18789             :     return;
   18790             : 
   18791     2811010 :   decl = STRIP_TEMPLATE (decl);
   18792             : 
   18793     2811010 :   if (!DECL_LANG_SPECIFIC (decl) && module_purview_p ())
   18794      395430 :     retrofit_lang_decl (decl);
   18795             : 
   18796     2811010 :   if (DECL_LANG_SPECIFIC (decl))
   18797             :     {
   18798     2316310 :       DECL_MODULE_PURVIEW_P (decl) = module_purview_p ();
   18799             :       /* If this was imported, we'll still be in the entity_hash.  */
   18800     2316310 :       DECL_MODULE_IMPORT_P (decl) = false;
   18801             :     }
   18802             : }
   18803             : 
   18804             : /* If DECL is a class member, whose class is not defined in this TU
   18805             :    (it was imported), remember this decl.  */
   18806             : 
   18807             : void
   18808      150493 : set_defining_module (tree decl)
   18809             : {
   18810      150493 :   gcc_checking_assert (!DECL_LANG_SPECIFIC (decl)
   18811             :                        || !DECL_MODULE_IMPORT_P (decl));
   18812             : 
   18813      150493 :   if (module_has_cmi_p ())
   18814             :     {
   18815       75319 :       tree ctx = DECL_CONTEXT (decl);
   18816       75319 :       if (ctx
   18817       75319 :           && (TREE_CODE (ctx) == RECORD_TYPE || TREE_CODE (ctx) == UNION_TYPE)
   18818       14653 :           && DECL_LANG_SPECIFIC (TYPE_NAME (ctx))
   18819       89972 :           && DECL_MODULE_IMPORT_P (TYPE_NAME (ctx)))
   18820             :         {
   18821             :           /* This entity's context is from an import.  We may need to
   18822             :              record this entity to make sure we emit it in the CMI.
   18823             :              Template specializations are in the template hash tables,
   18824             :              so we don't need to record them here as well.  */
   18825          12 :           int use_tpl = -1;
   18826          12 :           tree ti = node_template_info (decl, use_tpl);
   18827          12 :           if (use_tpl <= 0)
   18828             :             {
   18829          12 :               if (ti)
   18830             :                 {
   18831           3 :                   gcc_checking_assert (!use_tpl);
   18832             :                   /* Get to the TEMPLATE_DECL.  */
   18833           3 :                   decl = TI_TEMPLATE (ti);
   18834             :                 }
   18835             : 
   18836             :               /* Record it on the class_members list.  */
   18837          12 :               vec_safe_push (class_members, decl);
   18838             :             }
   18839             :         }
   18840       65160 :       else if (DECL_IMPLICIT_TYPEDEF_P (decl)
   18841      140467 :                && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
   18842             :         /* This is a partial or explicit specialization.  */
   18843       28747 :         vec_safe_push (partial_specializations, decl);
   18844             :     }
   18845      150493 : }
   18846             : 
   18847             : void
   18848   177327307 : set_originating_module (tree decl, bool friend_p ATTRIBUTE_UNUSED)
   18849             : {
   18850   177327307 :   set_instantiating_module (decl);
   18851             : 
   18852   177327307 :   if (!DECL_NAMESPACE_SCOPE_P (decl))
   18853             :     return;
   18854             : 
   18855   109547460 :   gcc_checking_assert (friend_p || decl == get_originating_module_decl (decl));
   18856             : 
   18857   109547460 :   if (module_attach_p ())
   18858             :     {
   18859        2107 :       retrofit_lang_decl (decl);
   18860        2107 :       DECL_MODULE_ATTACH_P (decl) = true;
   18861             :     }
   18862             : 
   18863   109547460 :   if (!module_exporting_p ())
   18864             :     return;
   18865             : 
   18866             :   // FIXME: Check ill-formed linkage
   18867      291381 :   DECL_MODULE_EXPORT_P (decl) = true;
   18868             : }
   18869             : 
   18870             : /* DECL is keyed to CTX for odr purposes.  */
   18871             : 
   18872             : void
   18873      247961 : maybe_key_decl (tree ctx, tree decl)
   18874             : {
   18875      247961 :   if (!modules_p ())
   18876             :     return;
   18877             : 
   18878             :   // FIXME: For now just deal with lambdas attached to var decls.
   18879             :   // This might be sufficient?
   18880        4808 :   if (TREE_CODE (ctx) != VAR_DECL)
   18881             :     return;
   18882             : 
   18883          42 :   gcc_checking_assert (DECL_NAMESPACE_SCOPE_P (ctx));
   18884             : 
   18885          42 :  if (!keyed_table)
   18886          30 :     keyed_table = new keyed_map_t (EXPERIMENT (1, 400));
   18887             : 
   18888          42 :  auto &vec = keyed_table->get_or_insert (ctx);
   18889          42 :  if (!vec.length ())
   18890             :    {
   18891          42 :      retrofit_lang_decl (ctx);
   18892          42 :      DECL_MODULE_KEYED_DECLS_P (ctx) = true;
   18893             :    }
   18894          42 :  vec.safe_push (decl);
   18895             : }
   18896             : 
   18897             : /* Create the flat name string.  It is simplest to have it handy.  */
   18898             : 
   18899             : void
   18900        3750 : module_state::set_flatname ()
   18901             : {
   18902        3750 :   gcc_checking_assert (!flatname);
   18903        3750 :   if (parent)
   18904             :     {
   18905         287 :       auto_vec<tree,5> ids;
   18906         287 :       size_t len = 0;
   18907         287 :       char const *primary = NULL;
   18908         287 :       size_t pfx_len = 0;
   18909             : 
   18910         287 :       for (module_state *probe = this;
   18911         732 :            probe;
   18912         445 :            probe = probe->parent)
   18913         664 :         if (is_partition () && !probe->is_partition ())
   18914             :           {
   18915         219 :             primary = probe->get_flatname ();
   18916         219 :             pfx_len = strlen (primary);
   18917         219 :             break;
   18918             :           }
   18919             :         else
   18920             :           {
   18921         445 :             ids.safe_push (probe->name);
   18922         445 :             len += IDENTIFIER_LENGTH (probe->name) + 1;
   18923             :           }
   18924             : 
   18925         287 :       char *flat = XNEWVEC (char, pfx_len + len + is_partition ());
   18926         287 :       flatname = flat;
   18927             : 
   18928         287 :       if (primary)
   18929             :         {
   18930         219 :           memcpy (flat, primary, pfx_len);
   18931         219 :           flat += pfx_len;
   18932         219 :           *flat++ = ':';
   18933             :         }
   18934             : 
   18935         732 :       for (unsigned len = 0; ids.length ();)
   18936             :         {
   18937         445 :           if (len)
   18938         158 :             flat[len++] = '.';
   18939         445 :           tree elt = ids.pop ();
   18940         445 :           unsigned l = IDENTIFIER_LENGTH (elt);
   18941         445 :           memcpy (flat + len, IDENTIFIER_POINTER (elt), l + 1);
   18942         445 :           len += l;
   18943             :         }
   18944         287 :     }
   18945        3463 :   else if (is_header ())
   18946        1383 :     flatname = TREE_STRING_POINTER (name);
   18947             :   else
   18948        2080 :     flatname = IDENTIFIER_POINTER (name);
   18949        3750 : }
   18950             : 
   18951             : /* Read the CMI file for a module.  */
   18952             : 
   18953             : bool
   18954        1874 : module_state::do_import (cpp_reader *reader, bool outermost)
   18955             : {
   18956        1874 :   gcc_assert (global_namespace == current_scope () && loadedness == ML_NONE);
   18957             : 
   18958        1874 :   loc = linemap_module_loc (line_table, loc, get_flatname ());
   18959             : 
   18960        1874 :   if (lazy_open >= lazy_limit)
   18961           9 :     freeze_an_elf ();
   18962             : 
   18963        1874 :   int fd = -1;
   18964        1874 :   int e = ENOENT;
   18965        1874 :   if (filename)
   18966             :     {
   18967        1874 :       const char *file = maybe_add_cmi_prefix (filename);
   18968        2318 :       dump () && dump ("CMI is %s", file);
   18969        1874 :       if (note_module_cmi_yes || inform_cmi_p)
   18970          12 :         inform (loc, "reading CMI %qs", file);
   18971        1874 :       fd = open (file, O_RDONLY | O_CLOEXEC | O_BINARY);
   18972        1874 :       e = errno;
   18973             :     }
   18974             : 
   18975        1874 :   gcc_checking_assert (!slurp);
   18976        3730 :   slurp = new slurping (new elf_in (fd, e));
   18977             : 
   18978        1874 :   bool ok = true;
   18979        1874 :   if (!from ()->get_error ())
   18980             :     {
   18981        1856 :       announce ("importing");
   18982        1856 :       loadedness = ML_CONFIG;
   18983        1856 :       lazy_open++;
   18984        1856 :       ok = read_initial (reader);
   18985        1856 :       slurp->lru = ++lazy_lru;
   18986             :     }
   18987             : 
   18988        1874 :   gcc_assert (slurp->current == ~0u);
   18989             : 
   18990        1874 :   return check_read (outermost, ok);
   18991             : }
   18992             : 
   18993             : /* Attempt to increase the file descriptor limit.  */
   18994             : 
   18995             : static bool
   18996        2888 : try_increase_lazy (unsigned want)
   18997             : {
   18998        2888 :   gcc_checking_assert (lazy_open >= lazy_limit);
   18999             : 
   19000             :   /* If we're increasing, saturate at hard limit.  */
   19001        2888 :   if (want > lazy_hard_limit && lazy_limit < lazy_hard_limit)
   19002        2888 :     want = lazy_hard_limit;
   19003             : 
   19004             : #if HAVE_SETRLIMIT
   19005        2888 :   if ((!lazy_limit || !param_lazy_modules)
   19006        2876 :       && lazy_hard_limit
   19007        2876 :       && want <= lazy_hard_limit)
   19008             :     {
   19009        2876 :       struct rlimit rlimit;
   19010        2876 :       rlimit.rlim_cur = want + LAZY_HEADROOM;
   19011        2876 :       rlimit.rlim_max = lazy_hard_limit + LAZY_HEADROOM;
   19012        2876 :       if (!setrlimit (RLIMIT_NOFILE, &rlimit))
   19013        2876 :         lazy_limit = want;
   19014             :     }
   19015             : #endif
   19016             : 
   19017        2888 :   return lazy_open < lazy_limit;
   19018             : }
   19019             : 
   19020             : /* Pick a victim module to freeze its reader.  */
   19021             : 
   19022             : void
   19023          12 : module_state::freeze_an_elf ()
   19024             : {
   19025          12 :   if (try_increase_lazy (lazy_open * 2))
   19026             :     return;
   19027             : 
   19028          12 :   module_state *victim = NULL;
   19029          12 :   for (unsigned ix = modules->length (); ix--;)
   19030             :     {
   19031          30 :       module_state *candidate = (*modules)[ix];
   19032          30 :       if (candidate && candidate->slurp && candidate->slurp->lru
   19033          60 :           && candidate->from ()->is_freezable ()
   19034          39 :           && (!victim || victim->slurp->lru > candidate->slurp->lru))
   19035             :         victim = candidate;
   19036             :     }
   19037             : 
   19038          12 :   if (victim)
   19039             :     {
   19040          18 :       dump () && dump ("Freezing '%s'", victim->filename);
   19041           9 :       if (victim->slurp->macro_defs.size)
   19042             :         /* Save the macro definitions to a buffer.  */
   19043           0 :         victim->from ()->preserve (victim->slurp->macro_defs);
   19044           9 :       if (victim->slurp->macro_tbl.size)
   19045             :         /* Save the macro definitions to a buffer.  */
   19046           0 :         victim->from ()->preserve (victim->slurp->macro_tbl);
   19047           9 :       victim->from ()->freeze ();
   19048           9 :       lazy_open--;
   19049             :     }
   19050             :   else
   19051           3 :     dump () && dump ("No module available for freezing");
   19052             : }
   19053             : 
   19054             : /* Load the lazy slot *MSLOT, INDEX'th slot of the module.  */
   19055             : 
   19056             : bool
   19057       15345 : module_state::lazy_load (unsigned index, binding_slot *mslot)
   19058             : {
   19059       15345 :   unsigned n = dump.push (this);
   19060             : 
   19061       15345 :   gcc_checking_assert (function_depth);
   19062             : 
   19063       15345 :   unsigned cookie = mslot->get_lazy ();
   19064       15345 :   unsigned snum = cookie >> 2;
   19065       15624 :   dump () && dump ("Loading entity %M[%u] section:%u", this, index, snum);
   19066             : 
   19067       15345 :   bool ok = load_section (snum, mslot);
   19068             :  
   19069       15345 :   dump.pop (n);
   19070             : 
   19071       15345 :   return ok;
   19072             : }
   19073             : 
   19074             : /* Load MOD's binding for NS::ID into *MSLOT.  *MSLOT contains the
   19075             :    lazy cookie.  OUTER is true if this is the outermost lazy, (used
   19076             :    for diagnostics).  */
   19077             : 
   19078             : void
   19079        1545 : lazy_load_binding (unsigned mod, tree ns, tree id, binding_slot *mslot)
   19080             : {
   19081        1545 :   int count = errorcount + warningcount;
   19082             : 
   19083        1545 :   timevar_start (TV_MODULE_IMPORT);
   19084             : 
   19085             :   /* Make sure lazy loading from a template context behaves as if
   19086             :      from a non-template context.  */
   19087        1545 :   processing_template_decl_sentinel ptds;
   19088             : 
   19089             :   /* Stop GC happening, even in outermost loads (because our caller
   19090             :      could well be building up a lookup set).  */
   19091        1545 :   function_depth++;
   19092             : 
   19093        1545 :   gcc_checking_assert (mod);
   19094        1545 :   module_state *module = (*modules)[mod];
   19095        1545 :   unsigned n = dump.push (module);
   19096             : 
   19097        1545 :   unsigned snum = mslot->get_lazy ();
   19098        1824 :   dump () && dump ("Lazily binding %P@%N section:%u", ns, id,
   19099             :                    module->name, snum);
   19100             : 
   19101        1545 :   bool ok = !recursive_lazy (snum);
   19102        1545 :   if (ok)
   19103             :     {
   19104        1545 :       ok = module->load_section (snum, mslot);
   19105        1545 :       lazy_snum = 0;
   19106        1545 :       post_load_processing ();
   19107             :     }
   19108             : 
   19109        1545 :   dump.pop (n);
   19110             : 
   19111        1545 :   function_depth--;
   19112             : 
   19113        1545 :   timevar_stop (TV_MODULE_IMPORT);
   19114             : 
   19115        1545 :   if (!ok)
   19116           0 :     fatal_error (input_location,
   19117           0 :                  module->is_header ()
   19118             :                  ? G_("failed to load binding %<%E%s%E%>")
   19119             :                  : G_("failed to load binding %<%E%s%E@%s%>"),
   19120           0 :                  ns, &"::"[ns == global_namespace ? 2 : 0], id,
   19121             :                  module->get_flatname ());
   19122             : 
   19123        1545 :   if (count != errorcount + warningcount)
   19124          21 :     inform (input_location,
   19125          21 :             module->is_header ()
   19126             :             ? G_("during load of binding %<%E%s%E%>")
   19127             :             : G_("during load of binding %<%E%s%E@%s%>"),
   19128          21 :             ns, &"::"[ns == global_namespace ? 2 : 0], id,
   19129             :             module->get_flatname ());
   19130        1545 : }
   19131             : 
   19132             : /* Load any pending entities keyed to the top-key of DECL.  */
   19133             : 
   19134             : void
   19135    27205480 : lazy_load_pendings (tree decl)
   19136             : {
   19137             :   /* Make sure lazy loading from a template context behaves as if
   19138             :      from a non-template context.  */
   19139    27205480 :   processing_template_decl_sentinel ptds;
   19140             : 
   19141    27205480 :   tree key_decl;
   19142    27205480 :   pending_key key;
   19143    27205480 :   key.ns = find_pending_key (decl, &key_decl);
   19144    27205480 :   key.id = DECL_NAME (key_decl);
   19145             : 
   19146    27205480 :   auto *pending_vec = pending_table ? pending_table->get (key) : nullptr;
   19147    27203429 :   if (!pending_vec)
   19148    27202279 :     return;
   19149             : 
   19150        3201 :   int count = errorcount + warningcount;
   19151             : 
   19152        3201 :   timevar_start (TV_MODULE_IMPORT);
   19153        3201 :   bool ok = !recursive_lazy ();
   19154        3201 :   if (ok)
   19155             :     {
   19156        3201 :       function_depth++; /* Prevent GC */
   19157        3201 :       unsigned n = dump.push (NULL);
   19158        3389 :       dump () && dump ("Reading %u pending entities keyed to %P",
   19159             :                        pending_vec->length (), key.ns, key.id);
   19160        3201 :       for (unsigned ix = pending_vec->length (); ix--;)
   19161             :         {
   19162       53184 :           unsigned index = (*pending_vec)[ix];
   19163       53184 :           binding_slot *slot = &(*entity_ary)[index];
   19164             : 
   19165       53184 :           if (slot->is_lazy ())
   19166             :             {
   19167         824 :               module_state *import = import_entity_module (index);
   19168         824 :               if (!import->lazy_load (index - import->entity_lwm, slot))
   19169       53184 :                 ok = false;
   19170             :             }
   19171       52360 :           else if (dump ())
   19172             :             {
   19173         117 :               module_state *import = import_entity_module (index);
   19174       56502 :               dump () && dump ("Entity %M[%u] already loaded",
   19175         117 :                                import, index - import->entity_lwm);
   19176             :             }
   19177             :         }
   19178             : 
   19179        3201 :       pending_table->remove (key);
   19180        3201 :       dump.pop (n);
   19181        3201 :       lazy_snum = 0;
   19182        3201 :       post_load_processing ();
   19183        3201 :       function_depth--;
   19184             :     }
   19185             : 
   19186        3201 :   timevar_stop (TV_MODULE_IMPORT);
   19187             : 
   19188        3201 :   if (!ok)
   19189           0 :     fatal_error (input_location, "failed to load pendings for %<%E%s%E%>",
   19190           0 :                  key.ns, &"::"[key.ns == global_namespace ? 2 : 0], key.id);
   19191             : 
   19192        3201 :   if (count != errorcount + warningcount)
   19193           0 :     inform (input_location, "during load of pendings for %<%E%s%E%>",
   19194           0 :             key.ns, &"::"[key.ns == global_namespace ? 2 : 0], key.id);
   19195    27205480 : }
   19196             : 
   19197             : static void
   19198        1701 : direct_import (module_state *import, cpp_reader *reader)
   19199             : {
   19200        1701 :   timevar_start (TV_MODULE_IMPORT);
   19201        1701 :   unsigned n = dump.push (import);
   19202             : 
   19203        1701 :   gcc_checking_assert (import->is_direct () && import->has_location ());
   19204        1701 :   if (import->loadedness == ML_NONE)
   19205        1013 :     if (!import->do_import (reader, true))
   19206           0 :       gcc_unreachable ();
   19207             : 
   19208        1673 :   if (import->loadedness < ML_LANGUAGE)
   19209             :     {
   19210        1609 :       if (!keyed_table)
   19211        1407 :         keyed_table = new keyed_map_t (EXPERIMENT (1, 400));
   19212        1609 :       import->read_language (true);
   19213             :     }
   19214             : 
   19215        1673 :   (*modules)[0]->set_import (import, import->exported_p);
   19216             : 
   19217        1673 :   dump.pop (n);
   19218        1673 :   timevar_stop (TV_MODULE_IMPORT);
   19219        1673 : }
   19220             : 
   19221             : /* Import module IMPORT.  */
   19222             : 
   19223             : void
   19224        1530 : import_module (module_state *import, location_t from_loc, bool exporting_p,
   19225             :                tree, cpp_reader *reader)
   19226             : {
   19227        1530 :   if (!import->check_not_purview (from_loc))
   19228             :     return;
   19229             : 
   19230        1521 :   if (!import->is_header () && current_lang_depth ())
   19231             :     /* Only header units should appear inside language
   19232             :        specifications.  The std doesn't specify this, but I think
   19233             :        that's an error in resolving US 033, because language linkage
   19234             :        is also our escape clause to getting things into the global
   19235             :        module, so we don't want to confuse things by having to think
   19236             :        about whether 'extern "C++" { import foo; }' puts foo's
   19237             :        contents into the global module all of a sudden.  */
   19238           6 :     warning (0, "import of named module %qs inside language-linkage block",
   19239             :              import->get_flatname ());
   19240             : 
   19241        1521 :   if (exporting_p || module_exporting_p ())
   19242         202 :     import->exported_p = true;
   19243             : 
   19244        1521 :   if (import->loadedness != ML_NONE)
   19245             :     {
   19246         685 :       from_loc = ordinary_loc_of (line_table, from_loc);
   19247         685 :       linemap_module_reparent (line_table, import->loc, from_loc);
   19248             :     }
   19249        1521 :   gcc_checking_assert (!import->module_p);
   19250        1521 :   gcc_checking_assert (import->is_direct () && import->has_location ());
   19251             : 
   19252        1521 :   direct_import (import, reader);
   19253             : }
   19254             : 
   19255             : /* Declare the name of the current module to be NAME.  EXPORTING_p is
   19256             :    true if this TU is the exporting module unit.  */
   19257             : 
   19258             : void
   19259        1909 : declare_module (module_state *module, location_t from_loc, bool exporting_p,
   19260             :                 tree, cpp_reader *reader)
   19261             : {
   19262        1909 :   gcc_assert (global_namespace == current_scope ());
   19263             : 
   19264        1909 :   module_state *current = (*modules)[0];
   19265        1909 :   if (module_purview_p () || module->loadedness > ML_CONFIG)
   19266             :     {
   19267          12 :       error_at (from_loc, module_purview_p ()
   19268             :                 ? G_("module already declared")
   19269             :                 : G_("module already imported"));
   19270           6 :       if (module_purview_p ())
   19271           0 :         module = current;
   19272           6 :       inform (module->loc, module_purview_p ()
   19273             :               ? G_("module %qs declared here")
   19274             :               : G_("module %qs imported here"),
   19275             :               module->get_flatname ());
   19276           6 :       return;
   19277             :     }
   19278             : 
   19279        1903 :   gcc_checking_assert (module->module_p);
   19280        1903 :   gcc_checking_assert (module->is_direct () && module->has_location ());
   19281             : 
   19282             :   /* Yer a module, 'arry.  */
   19283        1903 :   module_kind = module->is_header () ? MK_HEADER : MK_NAMED | MK_ATTACH;
   19284             : 
   19285             :   // Even in header units, we consider the decls to be purview
   19286        1903 :   module_kind |= MK_PURVIEW;
   19287             : 
   19288        1903 :   if (module->is_partition ())
   19289          87 :     module_kind |= MK_PARTITION;
   19290        1903 :   if (exporting_p)
   19291             :     {
   19292        1705 :       module->interface_p = true;
   19293        1705 :       module_kind |= MK_INTERFACE;
   19294             :     }
   19295             : 
   19296        1903 :   if (module_has_cmi_p ())
   19297             :     {
   19298             :       /* Copy the importing information we may have already done.  We
   19299             :          do not need to separate out the imports that only happen in
   19300             :          the GMF, inspite of what the literal wording of the std
   19301             :          might imply.  See p2191, the core list had a discussion
   19302             :          where the module implementors agreed that the GMF of a named
   19303             :          module is invisible to importers.  */
   19304        1723 :       module->imports = current->imports;
   19305             : 
   19306        1723 :       module->mod = 0;
   19307        1723 :       (*modules)[0] = module;
   19308             :     }
   19309             :   else
   19310             :     {
   19311         180 :       module->interface_p = true;
   19312         180 :       current->parent = module; /* So mangler knows module identity. */
   19313         180 :       direct_import (module, reader);
   19314             :     }
   19315             : }
   19316             : 
   19317             : /* Return true IFF we must emit a module global initializer function
   19318             :    (which will be called by importers' init code).  */
   19319             : 
   19320             : bool
   19321       94285 : module_global_init_needed ()
   19322             : {
   19323       94285 :   return module_has_cmi_p () && !header_module_p ();
   19324             : }
   19325             : 
   19326             : /* Calculate which, if any, import initializers need calling.  */
   19327             : 
   19328             : bool
   19329       88316 : module_determine_import_inits ()
   19330             : {
   19331       88316 :   if (!modules || header_module_p ())
   19332             :     return false;
   19333             : 
   19334             :   /* Prune active_init_p.  We need the same bitmap allocation
   19335             :      scheme as for the imports member.  */
   19336        2172 :   function_depth++; /* Disable GC.  */
   19337        2172 :   bitmap covered_imports (BITMAP_GGC_ALLOC ());
   19338             : 
   19339        2172 :   bool any = false;
   19340             : 
   19341             :   /* Because indirect imports are before their direct import, and
   19342             :      we're scanning the array backwards, we only need one pass!  */
   19343        3908 :   for (unsigned ix = modules->length (); --ix;)
   19344             :     {
   19345        1736 :       module_state *import = (*modules)[ix];
   19346             : 
   19347        1736 :       if (!import->active_init_p)
   19348             :         ;
   19349          24 :       else if (bitmap_bit_p (covered_imports, ix))
   19350           6 :         import->active_init_p = false;
   19351             :       else
   19352             :         {
   19353             :           /* Everything this imports is therefore handled by its
   19354             :              initializer, so doesn't need initializing by us.  */
   19355          18 :           bitmap_ior_into (covered_imports, import->imports);
   19356          18 :           any = true;
   19357             :         }
   19358             :     }
   19359        2172 :   function_depth--;
   19360             : 
   19361        2172 :   return any;
   19362             : }
   19363             : 
   19364             : /* Emit calls to each direct import's global initializer.  Including
   19365             :    direct imports of directly imported header units.  The initializers
   19366             :    of (static) entities in header units will be called by their
   19367             :    importing modules (for the instance contained within that), or by
   19368             :    the current TU (for the instances we've brought in).  Of course
   19369             :    such header unit behaviour is evil, but iostream went through that
   19370             :    door some time ago.  */
   19371             : 
   19372             : void
   19373          18 : module_add_import_initializers ()
   19374             : {
   19375          18 :   if (!modules || header_module_p ())
   19376           0 :     return;
   19377             : 
   19378          18 :   tree fntype = build_function_type (void_type_node, void_list_node);
   19379          18 :   releasing_vec args;  // There are no args
   19380             : 
   19381          45 :   for (unsigned ix = modules->length (); --ix;)
   19382             :     {
   19383          27 :       module_state *import = (*modules)[ix];
   19384          27 :       if (import->active_init_p)
   19385             :         {
   19386          18 :           tree name = mangle_module_global_init (ix);
   19387          18 :           tree fndecl = build_lang_decl (FUNCTION_DECL, name, fntype);
   19388             : 
   19389          18 :           DECL_CONTEXT (fndecl) = FROB_CONTEXT (global_namespace);
   19390          18 :           SET_DECL_ASSEMBLER_NAME (fndecl, name);
   19391          18 :           TREE_PUBLIC (fndecl) = true;
   19392          18 :           determine_visibility (fndecl);
   19393             : 
   19394          18 :           tree call = cp_build_function_call_vec (fndecl, &args,
   19395             :                                                   tf_warning_or_error);
   19396          18 :           finish_expr_stmt (call);
   19397             :         }
   19398             :     }
   19399          18 : }
   19400             : 
   19401             : /* NAME & LEN are a preprocessed header name, possibly including the
   19402             :    surrounding "" or <> characters.  Return the raw string name of the
   19403             :    module to which it refers.  This will be an absolute path, or begin
   19404             :    with ./, so it is immediately distinguishable from a (non-header
   19405             :    unit) module name.  If READER is non-null, ask the preprocessor to
   19406             :    locate the header to which it refers using the appropriate include
   19407             :    path.  Note that we do never do \ processing of the string, as that
   19408             :    matches the preprocessor's behaviour.  */
   19409             : 
   19410             : static const char *
   19411       22115 : canonicalize_header_name (cpp_reader *reader, location_t loc, bool unquoted,
   19412             :                           const char *str, size_t &len_r)
   19413             : {
   19414       22115 :   size_t len = len_r;
   19415       22115 :   static char *buf = 0;
   19416       22115 :   static size_t alloc = 0;
   19417             : 
   19418       22115 :   if (!unquoted)
   19419             :     {
   19420           6 :       gcc_checking_assert (len >= 2
   19421             :                            && ((reader && str[0] == '<' && str[len-1] == '>')
   19422             :                                || (str[0] == '"' && str[len-1] == '"')));
   19423           6 :       str += 1;
   19424           6 :       len -= 2;
   19425             :     }
   19426             : 
   19427       22115 :   if (reader)
   19428             :     {
   19429           6 :       gcc_assert (!unquoted);
   19430             : 
   19431           6 :       if (len >= alloc)
   19432             :         {
   19433           6 :           alloc = len + 1;
   19434           6 :           buf = XRESIZEVEC (char, buf, alloc);
   19435             :         }
   19436           6 :       memcpy (buf, str, len);
   19437           6 :       buf[len] = 0;
   19438             : 
   19439          12 :       if (const char *hdr
   19440           6 :           = cpp_probe_header_unit (reader, buf, str[-1] == '<', loc))
   19441             :         {
   19442           6 :           len = strlen (hdr);
   19443           6 :           str = hdr;
   19444             :         }
   19445             :       else
   19446           0 :         str = buf;
   19447             :     }
   19448             : 
   19449       22115 :   if (!(str[0] == '.' ? IS_DIR_SEPARATOR (str[1]) : IS_ABSOLUTE_PATH (str)))
   19450             :     {
   19451             :       /* Prepend './'  */
   19452           9 :       if (len + 3 > alloc)
   19453             :         {
   19454           9 :           alloc = len + 3;
   19455           9 :           buf = XRESIZEVEC (char, buf, alloc);
   19456             :         }
   19457             : 
   19458           9 :       buf[0] = '.';
   19459           9 :       buf[1] = DIR_SEPARATOR;
   19460           9 :       memmove (buf + 2, str, len);
   19461           9 :       len += 2;
   19462           9 :       buf[len] = 0;
   19463           9 :       str = buf;
   19464             :     }
   19465             : 
   19466       22115 :   len_r = len;
   19467       22115 :   return str;
   19468             : }
   19469             : 
   19470             : /* Set the CMI name from a cody packet.  Issue an error if
   19471             :    ill-formed.  */
   19472             : 
   19473        3462 : void module_state::set_filename (const Cody::Packet &packet)
   19474             : {
   19475        3462 :   gcc_checking_assert (!filename);
   19476        3462 :   if (packet.GetCode () == Cody::Client::PC_PATHNAME)
   19477        3459 :     filename = xstrdup (packet.GetString ().c_str ());
   19478             :   else
   19479             :     {
   19480           3 :       gcc_checking_assert (packet.GetCode () == Cody::Client::PC_ERROR);
   19481           3 :       error_at (loc, "unknown Compiled Module Interface: %s",
   19482           3 :                 packet.GetString ().c_str ());
   19483             :     }
   19484        3462 : }
   19485             : 
   19486             : /* Figure out whether to treat HEADER as an include or an import.  */
   19487             : 
   19488             : static char *
   19489       24297 : maybe_translate_include (cpp_reader *reader, line_maps *lmaps, location_t loc,
   19490             :                          const char *path)
   19491             : {
   19492       24297 :   if (!modules_p ())
   19493             :     {
   19494             :       /* Turn off.  */
   19495           0 :       cpp_get_callbacks (reader)->translate_include = NULL;
   19496           0 :       return nullptr;
   19497             :     }
   19498             : 
   19499       24297 :   if (!spans.init_p ())
   19500             :     /* Before the main file, don't divert.  */
   19501             :     return nullptr;
   19502             : 
   19503       21448 :   dump.push (NULL);
   19504             : 
   19505       22274 :   dump () && dump ("Checking include translation '%s'", path);
   19506       21448 :   auto *mapper = get_mapper (cpp_main_loc (reader));
   19507             : 
   19508       21448 :   size_t len = strlen (path);
   19509       21448 :   path = canonicalize_header_name (NULL, loc, true, path, len);
   19510       21448 :   auto packet = mapper->IncludeTranslate (path, Cody::Flags::None, len);
   19511       21448 :   int xlate = false;
   19512       21448 :   if (packet.GetCode () == Cody::Client::PC_BOOL)
   19513       21420 :     xlate = -int (packet.GetInteger ());
   19514          28 :   else if (packet.GetCode () == Cody::Client::PC_PATHNAME)
   19515             :     {
   19516             :       /* Record the CMI name for when we do the import.  */
   19517          28 :       module_state *import = get_module (build_string (len, path));
   19518          28 :       import->set_filename (packet);
   19519          28 :       xlate = +1;
   19520             :     }
   19521             :   else
   19522             :     {
   19523           0 :       gcc_checking_assert (packet.GetCode () == Cody::Client::PC_ERROR);
   19524           0 :       error_at (loc, "cannot determine %<#include%> translation of %s: %s",
   19525           0 :                 path, packet.GetString ().c_str ());
   19526             :     }
   19527             : 
   19528       21448 :   bool note = false;
   19529       21448 :   if (note_include_translate_yes && xlate > 1)
   19530             :     note = true;
   19531       21448 :   else if (note_include_translate_no && xlate == 0)
   19532             :     note = true;
   19533       21448 :   else if (note_includes)
   19534             :     /* We do not expect the note_includes vector to be large, so O(N)
   19535             :        iteration.  */
   19536        1124 :     for (unsigned ix = note_includes->length (); !note && ix--;)
   19537         562 :       if (!strcmp ((*note_includes)[ix], path))
   19538           3 :         note = true;
   19539             : 
   19540         562 :   if (note)
   19541           3 :     inform (loc, xlate
   19542             :             ? G_("include %qs translated to import")
   19543             :             : G_("include %qs processed textually") , path);
   19544             : 
   19545       23100 :   dump () && dump (xlate ? "Translating include to import"
   19546             :                    : "Keeping include as include");
   19547       21448 :   dump.pop (0);
   19548             : 
   19549       21448 :   if (!(xlate > 0))
   19550             :     return nullptr;
   19551             :   
   19552             :   /* Create the translation text.  */
   19553          28 :   loc = ordinary_loc_of (lmaps, loc);
   19554          28 :   const line_map_ordinary *map
   19555          28 :     = linemap_check_ordinary (linemap_lookup (lmaps, loc));
   19556          28 :   unsigned col = SOURCE_COLUMN (map, loc);
   19557          28 :   col -= (col != 0); /* Columns are 1-based.  */
   19558             : 
   19559          28 :   unsigned alloc = len + col + 60;
   19560          28 :   char *res = XNEWVEC (char, alloc);
   19561             : 
   19562          28 :   strcpy (res, "__import");
   19563          28 :   unsigned actual = 8;
   19564          28 :   if (col > actual)
   19565             :     {
   19566             :       /* Pad out so the filename appears at the same position.  */
   19567          28 :       memset (res + actual, ' ', col - actual);
   19568          28 :       actual = col;
   19569             :     }
   19570             :   /* No need to encode characters, that's not how header names are
   19571             :      handled.  */
   19572          28 :   actual += snprintf (res + actual, alloc - actual,
   19573             :                       "\"%s\" [[__translated]];\n", path);
   19574          28 :   gcc_checking_assert (actual < alloc);
   19575             : 
   19576             :   /* cpplib will delete the buffer.  */
   19577             :   return res;
   19578       24297 : }
   19579             : 
   19580             : static void
   19581         661 : begin_header_unit (cpp_reader *reader)
   19582             : {
   19583             :   /* Set the module header name from the main_input_filename.  */
   19584         661 :   const char *main = main_input_filename;
   19585         661 :   size_t len = strlen (main);
   19586         661 :   main = canonicalize_header_name (NULL, 0, true, main, len);
   19587         661 :   module_state *module = get_module (build_string (len, main));
   19588             : 
   19589         661 :   preprocess_module (module, cpp_main_loc (reader), false, false, true, reader);
   19590         661 : }
   19591             : 
   19592             : /* We've just properly entered the main source file.  I.e. after the
   19593             :    command line, builtins and forced headers.  Record the line map and
   19594             :    location of this map.  Note we may be called more than once.  The
   19595             :    first call sticks.  */
   19596             : 
   19597             : void
   19598       89507 : module_begin_main_file (cpp_reader *reader, line_maps *lmaps,
   19599             :                        const line_map_ordinary *map)
   19600             : {
   19601       89507 :   gcc_checking_assert (lmaps == line_table);
   19602       89507 :   if (modules_p () && !spans.init_p ())
   19603             :     {
   19604        2879 :       unsigned n = dump.push (NULL);
   19605        2879 :       spans.init (lmaps, map);
   19606        2879 :       dump.pop (n);
   19607        2879 :       if (flag_header_unit && !cpp_get_options (reader)->preprocessed)
   19608             :         {
   19609             :           /* Tell the preprocessor this is an include file.  */
   19610         652 :           cpp_retrofit_as_include (reader);
   19611         652 :           begin_header_unit (reader);
   19612             :         }
   19613             :     }
   19614       89507 : }
   19615             : 
   19616             : /* Process the pending_import queue, making sure we know the
   19617             :    filenames.   */
   19618             : 
   19619             : static void
   19620        3536 : name_pending_imports (cpp_reader *reader)
   19621             : {
   19622        3536 :   auto *mapper = get_mapper (cpp_main_loc (reader));
   19623             : 
   19624        3536 :   if (!vec_safe_length (pending_imports))
   19625             :     /* Not doing anything.  */
   19626             :     return;
   19627             : 
   19628        2960 :   timevar_start (TV_MODULE_MAPPER);
   19629             : 
   19630        2960 :   auto n = dump.push (NULL);
   19631        3473 :   dump () && dump ("Resolving direct import names");
   19632        2960 :   bool want_deps = (bool (mapper->get_flags () & Cody::Flags::NameOnly)
   19633        2960 :                     || cpp_get_deps (reader));
   19634        2960 :   bool any = false;
   19635             : 
   19636        6494 :   for (unsigned ix = 0; ix != pending_imports->length (); ix++)
   19637             :     {
   19638        3534 :       module_state *module = (*pending_imports)[ix];
   19639        3534 :       gcc_checking_assert (module->is_direct ());
   19640        3534 :       if (!module->filename && !module->visited_p)
   19641             :         {
   19642        3458 :           bool export_p = (module->module_p
   19643        3458 :                            && (module->is_partition () || module->exported_p));
   19644             : 
   19645        3458 :           Cody::Flags flags = Cody::Flags::None;
   19646        3458 :           if (flag_preprocess_only
   19647        3458 :               && !(module->is_header () && !export_p))
   19648             :             {
   19649          30 :               if (!want_deps)
   19650          24 :                 continue;
   19651             :               flags = Cody::Flags::NameOnly;
   19652             :             }
   19653             : 
   19654        3434 :           if (!any)
   19655             :             {
   19656        2941 :               any = true;
   19657        2941 :               mapper->Cork ();
   19658             :             }
   19659        3434 :           if (export_p)
   19660        1732 :             mapper->ModuleExport (module->get_flatname (), flags);
   19661             :           else
   19662        1702 :             mapper->ModuleImport (module->get_flatname (), flags);
   19663        3434 :           module->visited_p = true;
   19664             :         }
   19665             :     }
   19666             : 
   19667        2960 :   if (any)
   19668             :     {
   19669        2941 :       auto response = mapper->Uncork ();
   19670        2941 :       auto r_iter = response.begin ();
   19671        6444 :       for (unsigned ix = 0; ix != pending_imports->length (); ix++)
   19672             :         {
   19673        3503 :           module_state *module = (*pending_imports)[ix];
   19674        3503 :           if (module->visited_p)
   19675             :             {
   19676        3434 :               module->visited_p = false;
   19677        3434 :               gcc_checking_assert (!module->filename);
   19678             : 
   19679        3434 :               module->set_filename (*r_iter);
   19680        3503 :               ++r_iter;
   19681             :             }
   19682             :         }
   19683        2941 :     }
   19684             : 
   19685        2960 :   dump.pop (n);
   19686             : 
   19687        2960 :   timevar_stop (TV_MODULE_MAPPER);
   19688             : }
   19689             : 
   19690             : /* We've just lexed a module-specific control line for MODULE.  Mark
   19691             :    the module as a direct import, and possibly load up its macro
   19692             :    state.  Returns the primary module, if this is a module
   19693             :    declaration.  */
   19694             : /* Perhaps we should offer a preprocessing mode where we read the
   19695             :    directives from the header unit, rather than require the header's
   19696             :    CMI.  */
   19697             : 
   19698             : module_state *
   19699        3550 : preprocess_module (module_state *module, location_t from_loc,
   19700             :                    bool in_purview, bool is_import, bool is_export,
   19701             :                    cpp_reader *reader)
   19702             : {
   19703        3550 :   if (!is_import)
   19704             :     {
   19705        1957 :       if (module->loc)
   19706             :         /* It's already been mentioned, so ignore its module-ness.  */
   19707             :         is_import = true;
   19708             :       else
   19709             :         {
   19710             :           /* Record it is the module.  */
   19711        1945 :           module->module_p = true;
   19712        1945 :           if (is_export)
   19713             :             {
   19714        1720 :               module->exported_p = true;
   19715        1720 :               module->interface_p = true;
   19716             :             }
   19717             :         }
   19718             :     }
   19719             : 
   19720        3550 :   if (module->directness < MD_DIRECT + in_purview)
   19721             :     {
   19722             :       /* Mark as a direct import.  */
   19723        3507 :       module->directness = module_directness (MD_DIRECT + in_purview);
   19724             : 
   19725             :       /* Set the location to be most informative for users.  */
   19726        3507 :       from_loc = ordinary_loc_of (line_table, from_loc);
   19727        3507 :       if (module->loadedness != ML_NONE)
   19728           3 :         linemap_module_reparent (line_table, module->loc, from_loc);
   19729             :       else
   19730             :         {
   19731        3504 :           module->loc = from_loc;
   19732        3504 :           if (!module->flatname)
   19733        3486 :             module->set_flatname ();
   19734             :         }
   19735             :     }
   19736             : 
   19737        3550 :   auto desired = ML_CONFIG;
   19738        3550 :   if (is_import
   19739        1605 :       && module->is_header ()
   19740        4220 :       && (!cpp_get_options (reader)->preprocessed
   19741           3 :           || cpp_get_options (reader)->directives_only))
   19742             :     /* We need preprocessor state now.  */
   19743             :     desired = ML_PREPROCESSOR;
   19744             : 
   19745        3550 :   if (!is_import || module->loadedness < desired)
   19746             :     {
   19747        3534 :       vec_safe_push (pending_imports, module);
   19748             : 
   19749        3534 :       if (desired == ML_PREPROCESSOR)
   19750             :         {
   19751         654 :           unsigned n = dump.push (NULL);
   19752             : 
   19753         840 :           dump () && dump ("Reading %M preprocessor state", module);
   19754         654 :           name_pending_imports (reader);
   19755             : 
   19756             :           /* Preserve the state of the line-map.  */
   19757         654 :           unsigned pre_hwm = LINEMAPS_ORDINARY_USED (line_table);
   19758             : 
   19759             :           /* We only need to close the span, if we're going to emit a
   19760             :              CMI.  But that's a little tricky -- our token scanner
   19761             :              needs to be smarter -- and this isn't much state.
   19762             :              Remember, we've not parsed anything at this point, so
   19763             :              our module state flags are inadequate.  */
   19764         654 :           spans.maybe_init ();
   19765         654 :           spans.close ();
   19766             : 
   19767         654 :           timevar_start (TV_MODULE_IMPORT);
   19768             : 
   19769             :           /* Load the config of each pending import -- we must assign
   19770             :              module numbers monotonically.  */
   19771        1465 :           for (unsigned ix = 0; ix != pending_imports->length (); ix++)
   19772             :             {
   19773         811 :               auto *import = (*pending_imports)[ix];
   19774         959 :               if (!(import->module_p
   19775         148 :                     && (import->is_partition () || import->exported_p))
   19776         669 :                   && import->loadedness == ML_NONE
   19777        1480 :                   && (import->is_header () || !flag_preprocess_only))
   19778             :                 {
   19779         660 :                   unsigned n = dump.push (import);
   19780         660 :                   import->do_import (reader, true);
   19781         660 :                   dump.pop (n);
   19782             :                 }
   19783             :             }
   19784         654 :           vec_free (pending_imports);
   19785             : 
   19786             :           /* Restore the line-map state.  */
   19787         654 :           spans.open (linemap_module_restore (line_table, pre_hwm));
   19788             : 
   19789             :           /* Now read the preprocessor state of this particular
   19790             :              import.  */
   19791         654 :           if (module->loadedness == ML_CONFIG
   19792         654 :               && module->read_preprocessor (true))
   19793         651 :             module->import_macros ();
   19794             : 
   19795         654 :           timevar_stop (TV_MODULE_IMPORT);
   19796             : 
   19797         654 :           dump.pop (n);
   19798             :         }
   19799             :     }
   19800             : 
   19801        3550 :   return is_import ? NULL : get_primary (module);
   19802             : }
   19803             : 
   19804             : /* We've completed phase-4 translation.  Emit any dependency
   19805             :    information for the not-yet-loaded direct imports, and fill in
   19806             :    their file names.  We'll have already loaded up the direct header
   19807             :    unit wavefront.  */
   19808             : 
   19809             : void
   19810        2882 : preprocessed_module (cpp_reader *reader)
   19811             : {
   19812        2882 :   unsigned n = dump.push (NULL);
   19813             : 
   19814        3359 :   dump () && dump ("Completed phase-4 (tokenization) processing");
   19815             : 
   19816        2882 :   name_pending_imports (reader);
   19817        2882 :   vec_free (pending_imports);
   19818             : 
   19819        2882 :   spans.maybe_init ();
   19820        2882 :   spans.close ();
   19821             : 
   19822        2882 :   using iterator = hash_table<module_state_hash>::iterator;
   19823        2882 :   if (mkdeps *deps = cpp_get_deps (reader))
   19824             :     {
   19825             :       /* Walk the module hash, informing the dependency machinery.  */
   19826          12 :       iterator end = modules_hash->end ();
   19827          72 :       for (iterator iter = modules_hash->begin (); iter != end; ++iter)
   19828             :         {
   19829          24 :           module_state *module = *iter;
   19830             : 
   19831          24 :           if (module->is_direct ())
   19832             :             {
   19833          15 :               if (module->is_module ()
   19834          15 :                   && (module->is_interface () || module->is_partition ()))
   19835          12 :                 deps_add_module_target (deps, module->get_flatname (),
   19836          12 :                                         maybe_add_cmi_prefix (module->filename),
   19837          12 :                                         module->is_header());
   19838             :               else
   19839           3 :                 deps_add_module_dep (deps, module->get_flatname ());
   19840             :             }
   19841             :         }
   19842             :     }
   19843             : 
   19844        2882 :   if (flag_header_unit && !flag_preprocess_only)
   19845             :     {
   19846             :       /* Find the main module -- remember, it's not yet in the module
   19847             :          array.  */
   19848         652 :       iterator end = modules_hash->end ();
   19849        1386 :       for (iterator iter = modules_hash->begin (); iter != end; ++iter)
   19850             :         {
   19851         693 :           module_state *module = *iter;
   19852         693 :           if (module->is_module ())
   19853             :             {
   19854         652 :               declare_module (module, cpp_main_loc (reader), true, NULL, reader);
   19855         652 :               module_kind |= MK_EXPORTING;
   19856         652 :               break;
   19857             :             }
   19858             :         }
   19859             :     }
   19860             : 
   19861        2882 :   dump.pop (n);
   19862        2882 : }
   19863             : 
   19864             : /* VAL is a global tree, add it to the global vec if it is
   19865             :    interesting.  Add some of its targets, if they too are
   19866             :    interesting.  We do not add identifiers, as they can be re-found
   19867             :    via the identifier hash table.  There is a cost to the number of
   19868             :    global trees.  */
   19869             : 
   19870             : static int
   19871     1768600 : maybe_add_global (tree val, unsigned &crc)
   19872             : {
   19873     1768600 :   int v = 0;
   19874             : 
   19875     1768600 :   if (val && !(identifier_p (val) || TREE_VISITED (val)))
   19876             :     {
   19877      549514 :       TREE_VISITED (val) = true;
   19878      549514 :       crc = crc32_unsigned (crc, fixed_trees->length ());
   19879      549514 :       vec_safe_push (fixed_trees, val);
   19880      549514 :       v++;
   19881             : 
   19882      549514 :       if (CODE_CONTAINS_STRUCT (TREE_CODE (val), TS_TYPED))
   19883      549514 :         v += maybe_add_global (TREE_TYPE (val), crc);
   19884      549514 :       if (CODE_CONTAINS_STRUCT (TREE_CODE (val), TS_TYPE_COMMON))
   19885      357368 :         v += maybe_add_global (TYPE_NAME (val), crc);
   19886             :     }
   19887             : 
   19888     1768600 :   return v;
   19889             : }
   19890             : 
   19891             : /* Initialize module state.  Create the hash table, determine the
   19892             :    global trees.  Create the module for current TU.  */
   19893             : 
   19894             : void
   19895        2882 : init_modules (cpp_reader *reader)
   19896             : {
   19897             :   /* PCH should not be reachable because of lang-specs, but the
   19898             :      user could have overriden that.  */
   19899        2882 :   if (pch_file)
   19900           0 :     fatal_error (input_location,
   19901             :                  "C++ modules are incompatible with precompiled headers");
   19902             : 
   19903        2882 :   if (cpp_get_options (reader)->traditional)
   19904           0 :     fatal_error (input_location,
   19905             :                  "C++ modules are incompatible with traditional preprocessing");
   19906             : 
   19907        2882 :   if (flag_preprocess_only)
   19908             :     {
   19909          30 :       cpp_options *cpp_opts = cpp_get_options (reader);
   19910          30 :       if (flag_no_output
   19911          30 :           || (cpp_opts->deps.style != DEPS_NONE
   19912           6 :               && !cpp_opts->deps.need_preprocessor_output))
   19913             :         {
   19914           0 :           warning (0, flag_dump_macros == 'M'
   19915             :                    ? G_("macro debug output may be incomplete with modules")
   19916             :                    : G_("module dependencies require preprocessing"));
   19917           0 :           if (cpp_opts->deps.style != DEPS_NONE)
   19918           0 :             inform (input_location, "you should use the %<-%s%> option",
   19919             :                     cpp_opts->deps.style == DEPS_SYSTEM ? "MD" : "MMD");
   19920             :         }
   19921             :     }
   19922             : 
   19923             :   /* :: is always exported.  */
   19924        2882 :   DECL_MODULE_EXPORT_P (global_namespace) = true;
   19925             : 
   19926        2882 :   modules_hash = hash_table<module_state_hash>::create_ggc (31);
   19927        2882 :   vec_safe_reserve (modules, 20);
   19928             : 
   19929             :   /* Create module for current TU.  */
   19930        2882 :   module_state *current
   19931        2882 :     = new (ggc_alloc<module_state> ()) module_state (NULL_TREE, NULL, false);
   19932        2882 :   current->mod = 0;
   19933        2882 :   bitmap_set_bit (current->imports, 0);
   19934        2882 :   modules->quick_push (current);
   19935             : 
   19936        2882 :   gcc_checking_assert (!fixed_trees);
   19937             : 
   19938        2882 :   headers = BITMAP_GGC_ALLOC ();
   19939             : 
   19940        2882 :   if (note_includes)
   19941             :     /* Canonicalize header names.  */
   19942           6 :     for (unsigned ix = 0; ix != note_includes->length (); ix++)
   19943             :       {
   19944           3 :         const char *hdr = (*note_includes)[ix];
   19945           3 :         size_t len = strlen (hdr);
   19946             : 
   19947           3 :         bool system = hdr[0] == '<';
   19948           3 :         bool user = hdr[0] == '"';
   19949           3 :         bool delimed = system || user;
   19950             : 
   19951           3 :         if (len <= (delimed ? 2 : 0)
   19952           3 :             || (delimed && hdr[len-1] != (system ? '>' : '"')))
   19953           0 :           error ("invalid header name %qs", hdr);
   19954             : 
   19955           3 :         hdr = canonicalize_header_name (delimed ? reader : NULL,
   19956           3 :                                         0, !delimed, hdr, len);
   19957           3 :         char *path = XNEWVEC (char, len + 1);
   19958           3 :         memcpy (path, hdr, len);
   19959           3 :         path[len] = 0;
   19960             : 
   19961           3 :         (*note_includes)[ix] = path;
   19962             :       }
   19963             : 
   19964        2882 :   if (note_cmis)
   19965             :     /* Canonicalize & mark module names.  */
   19966          12 :     for (unsigned ix = 0; ix != note_cmis->length (); ix++)
   19967             :       {
   19968           6 :         const char *name = (*note_cmis)[ix];
   19969           6 :         size_t len = strlen (name);
   19970             : 
   19971           6 :         bool is_system = name[0] == '<';
   19972           6 :         bool is_user = name[0] == '"';
   19973           6 :         bool is_pathname = false;
   19974           6 :         if (!(is_system || is_user))
   19975          12 :           for (unsigned ix = len; !is_pathname && ix--;)
   19976           9 :             is_pathname = IS_DIR_SEPARATOR (name[ix]);
   19977           6 :         if (is_system || is_user || is_pathname)
   19978             :           {
   19979           3 :             if (len <= (is_pathname ? 0 : 2)
   19980           3 :                 || (!is_pathname && name[len-1] != (is_system ? '>' : '"')))
   19981             :               {
   19982           0 :                 error ("invalid header name %qs", name);
   19983           0 :                 continue;
   19984             :               }
   19985             :             else
   19986           3 :               name = canonicalize_header_name (is_pathname ? nullptr : reader,
   19987             :                                                0, is_pathname, name, len);
   19988             :           }
   19989           6 :         if (auto module = get_module (name))
   19990           6 :           module->inform_cmi_p = 1;
   19991             :         else
   19992           0 :           error ("invalid module name %qs", name);
   19993             :       }
   19994             : 
   19995        2882 :   dump.push (NULL);
   19996             : 
   19997             :   /* Determine lazy handle bound.  */
   19998        2882 :   {
   19999        2882 :     unsigned limit = 1000;
   20000             : #if HAVE_GETRLIMIT
   20001        2882 :     struct rlimit rlimit;
   20002        2882 :     if (!getrlimit (RLIMIT_NOFILE, &rlimit))
   20003             :       {
   20004        2882 :         lazy_hard_limit = (rlimit.rlim_max < 1000000
   20005        2882 :                            ? unsigned (rlimit.rlim_max) : 1000000);
   20006        2882 :         lazy_hard_limit = (lazy_hard_limit > LAZY_HEADROOM
   20007        2882 :                            ? lazy_hard_limit - LAZY_HEADROOM : 0);
   20008        2882 :         if (rlimit.rlim_cur < limit)
   20009           0 :           limit = unsigned (rlimit.rlim_cur);
   20010             :       }
   20011             : #endif
   20012        2882 :     limit = limit > LAZY_HEADROOM ? limit - LAZY_HEADROOM : 1;
   20013             : 
   20014        2882 :     if (unsigned parm = param_lazy_modules)
   20015             :       {
   20016        2882 :         if (parm <= limit || !lazy_hard_limit || !try_increase_lazy (parm))
   20017           6 :           lazy_limit = parm;
   20018             :       }
   20019             :     else
   20020           0 :       lazy_limit = limit;
   20021             :   }
   20022             : 
   20023        2882 :   if (dump ())
   20024             :     {
   20025         477 :       verstr_t ver;
   20026         477 :       version2string (MODULE_VERSION, ver);
   20027         477 :       dump ("Source: %s", main_input_filename);
   20028         477 :       dump ("Compiler: %s", version_string);
   20029         477 :       dump ("Modules: %s", ver);
   20030         477 :       dump ("Checking: %s",
   20031             : #if CHECKING_P
   20032             :             "checking"
   20033             : #elif ENABLE_ASSERT_CHECKING
   20034             :             "asserting"
   20035             : #else
   20036             :             "release"
   20037             : #endif
   20038             :             );
   20039         477 :       dump ("Compiled by: "
   20040             : #ifdef __GNUC__
   20041             :             "GCC %d.%d, %s", __GNUC__, __GNUC_MINOR__,
   20042             : #ifdef __OPTIMIZE__
   20043             :             "optimizing"
   20044             : #else
   20045             :             "not optimizing"
   20046             : #endif
   20047             : #else
   20048             :             "not GCC"
   20049             : #endif
   20050             :             );
   20051         477 :       dump ("Reading: %s", MAPPED_READING ? "mmap" : "fileio");
   20052         477 :       dump ("Writing: %s", MAPPED_WRITING ? "mmap" : "fileio");
   20053         477 :       dump ("Lazy limit: %u", lazy_limit);
   20054         477 :       dump ("Lazy hard limit: %u", lazy_hard_limit);
   20055         477 :       dump ("");
   20056             :     }
   20057             : 
   20058             :   /* Construct the global tree array.  This is an array of unique
   20059             :      global trees (& types).  Do this now, rather than lazily, as
   20060             :      some global trees are lazily created and we don't want that to
   20061             :      mess with our syndrome of fixed trees.  */
   20062        2882 :   unsigned crc = 0;
   20063        2882 :   vec_alloc (fixed_trees, 200);
   20064             : 
   20065        3359 :   dump () && dump ("+Creating globals");
   20066             :   /* Insert the TRANSLATION_UNIT_DECL.  */
   20067        2882 :   TREE_VISITED (DECL_CONTEXT (global_namespace)) = true;
   20068        2882 :   fixed_trees->quick_push (DECL_CONTEXT (global_namespace));
   20069       17292 :   for (unsigned jx = 0; global_tree_arys[jx].first; jx++)
   20070             :     {
   20071       14410 :       const tree *ptr = global_tree_arys[jx].first;
   20072       14410 :       unsigned limit = global_tree_arys[jx].second;
   20073             : 
   20074      876128 :       for (unsigned ix = 0; ix != limit; ix++, ptr++)
   20075             :         {
   20076      861718 :           !(ix & 31) && dump ("") && dump ("+\t%u:%u:", jx, ix);
   20077      861718 :           unsigned v = maybe_add_global (*ptr, crc);
   20078     1004341 :           dump () && dump ("+%u", v);
   20079             :         }
   20080             :     }
   20081        2882 :   global_crc = crc32_unsigned (crc, fixed_trees->length ());
   20082        2882 :   dump ("") && dump ("Created %u unique globals, crc=%x",
   20083             :                      fixed_trees->length (), global_crc);
   20084      555278 :   for (unsigned ix = fixed_trees->length (); ix--;)
   20085      552396 :     TREE_VISITED ((*fixed_trees)[ix]) = false;
   20086             : 
   20087        2882 :   dump.pop (0);
   20088             : 
   20089        2882 :   if (!flag_module_lazy)
   20090             :     /* Get the mapper now, if we're not being lazy.  */
   20091         234 :     get_mapper (cpp_main_loc (reader));
   20092             : 
   20093        2882 :   if (!flag_preprocess_only)
   20094             :     {
   20095        2852 :       pending_table = new pending_map_t (EXPERIMENT (1, 400));
   20096        2852 :       entity_map = new entity_map_t (EXPERIMENT (1, 400));
   20097        2852 :       vec_safe_reserve (entity_ary, EXPERIMENT (1, 400));
   20098             :     }
   20099             : 
   20100             : #if CHECKING_P
   20101        2882 :   note_defs = note_defs_table_t::create_ggc (1000);
   20102             : #endif
   20103             : 
   20104        2882 :   if (flag_header_unit && cpp_get_options (reader)->preprocessed)
   20105           9 :     begin_header_unit (reader);
   20106             : 
   20107             :   /* Collect here to make sure things are tagged correctly (when
   20108             :      aggressively GC'd).  */
   20109        2882 :   ggc_collect ();
   20110        2882 : }
   20111             : 
   20112             : /* If NODE is a deferred macro, load it.  */
   20113             : 
   20114             : static int
   20115       77178 : load_macros (cpp_reader *reader, cpp_hashnode *node, void *)
   20116             : {
   20117       77178 :   location_t main_loc
   20118       77178 :     = MAP_START_LOCATION (LINEMAPS_ORDINARY_MAP_AT (line_table, 0));
   20119             : 
   20120       77178 :   if (cpp_user_macro_p (node)
   20121       77178 :       && !node->value.macro)
   20122             :     {
   20123          72 :       cpp_macro *macro = cpp_get_deferred_macro (reader, node, main_loc);
   20124          72 :       dump () && dump ("Loaded macro #%s %I",
   20125             :                        macro ? "define" : "undef", identifier (node));
   20126             :     }
   20127             : 
   20128       77178 :   return 1;
   20129             : }
   20130             : 
   20131             : /* At the end of tokenizing, we no longer need the macro tables of
   20132             :    imports.  But the user might have requested some checking.  */
   20133             : 
   20134             : void
   20135       88493 : maybe_check_all_macros (cpp_reader *reader)
   20136             : {
   20137       88493 :   if (!warn_imported_macros)
   20138             :     return;
   20139             : 
   20140             :   /* Force loading of any remaining deferred macros.  This will
   20141             :      produce diagnostics if they are ill-formed.  */
   20142          21 :   unsigned n = dump.push (NULL);
   20143          21 :   cpp_forall_identifiers (reader, load_macros, NULL);
   20144          21 :   dump.pop (n);
   20145             : }
   20146             : 
   20147             : // State propagated from finish_module_processing to fini_modules
   20148             : 
   20149             : struct module_processing_cookie
   20150             : {
   20151             :   elf_out out;
   20152             :   module_state_config config;
   20153             :   char *cmi_name;
   20154             :   char *tmp_name;
   20155             :   unsigned crc;
   20156             :   bool began;
   20157             : 
   20158        1717 :   module_processing_cookie (char *cmi, char *tmp, int fd, int e)
   20159        1717 :     : out (fd, e), cmi_name (cmi), tmp_name (tmp), crc (0), began (false)
   20160             :   {
   20161             :   }
   20162        1717 :   ~module_processing_cookie ()
   20163             :   {
   20164        1717 :     XDELETEVEC (tmp_name);
   20165        1717 :     XDELETEVEC (cmi_name);
   20166        1717 :   }
   20167             : };
   20168             : 
   20169             : /* Write the CMI, if we're a module interface.  */
   20170             : 
   20171             : void *
   20172       88316 : finish_module_processing (cpp_reader *reader)
   20173             : {
   20174       88316 :   module_processing_cookie *cookie = nullptr;
   20175             : 
   20176       88316 :   if (header_module_p ())
   20177         652 :     module_kind &= ~MK_EXPORTING;
   20178             : 
   20179       88316 :   if (!modules || !(*modules)[0]->name)
   20180             :     {
   20181       86596 :       if (flag_module_only)
   20182           6 :         warning (0, "%<-fmodule-only%> used for non-interface");
   20183             :     }
   20184        1720 :   else if (!flag_syntax_only)
   20185             :     {
   20186        1717 :       int fd = -1;
   20187        1717 :       int e = -1;
   20188             : 
   20189        1717 :       timevar_start (TV_MODULE_EXPORT);
   20190             : 
   20191             :       /* Force a valid but empty line map at the end.  This simplifies
   20192             :          the line table preparation and writing logic.  */
   20193        1717 :       linemap_add (line_table, LC_ENTER, false, "", 0);
   20194             : 
   20195             :       /* We write to a tmpname, and then atomically rename.  */
   20196        1717 :       char *cmi_name = NULL;
   20197        1717 :       char *tmp_name = NULL;
   20198        1717 :       module_state *state = (*modules)[0];
   20199             : 
   20200        1717 :       unsigned n = dump.push (state);
   20201        1717 :       state->announce ("creating");
   20202        1717 :       if (state->filename)
   20203             :         {
   20204        1714 :           size_t len = 0;
   20205        1714 :           cmi_name = xstrdup (maybe_add_cmi_prefix (state->filename, &len));
   20206        1714 :           tmp_name = XNEWVEC (char, len + 3);
   20207        1714 :           memcpy (tmp_name, cmi_name, len);
   20208        1714 :           strcpy (&tmp_name[len], "~");
   20209             : 
   20210        1714 :           if (!errorcount)
   20211           6 :             for (unsigned again = 2; ; again--)
   20212             :               {
   20213        1666 :                 fd = open (tmp_name,
   20214             :                            O_RDWR | O_CREAT | O_TRUNC | O_CLOEXEC | O_BINARY,
   20215             :                            S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH);
   20216        1666 :                 e = errno;
   20217        1666 :                 if (fd >= 0 || !again || e != ENOENT)
   20218             :                   break;
   20219           6 :                 create_dirs (tmp_name);
   20220             :               }
   20221        1714 :           if (note_module_cmi_yes || state->inform_cmi_p)
   20222           3 :             inform (state->loc, "writing CMI %qs", cmi_name);
   20223        1939 :           dump () && dump ("CMI is %s", cmi_name);
   20224             :         }
   20225             : 
   20226        1717 :       cookie = new module_processing_cookie (cmi_name, tmp_name, fd, e);
   20227             : 
   20228        1717 :       if (errorcount)
   20229          57 :         warning_at (state->loc, 0, "not writing module %qs due to errors",
   20230             :                     state->get_flatname ());
   20231        1660 :       else if (cookie->out.begin ())
   20232             :         {
   20233        1660 :           cookie->began = true;
   20234        1660 :           auto loc = input_location;
   20235             :           /* So crashes finger-point the module decl.  */
   20236        1660 :           input_location = state->loc;
   20237        1660 :           state->write_begin (&cookie->out, reader, cookie->config, cookie->crc);
   20238        1660 :           input_location = loc;
   20239             :         }
   20240             : 
   20241        1717 :       dump.pop (n);
   20242        1717 :       timevar_stop (TV_MODULE_EXPORT);
   20243             : 
   20244        1717 :       ggc_collect ();
   20245             :     }
   20246             : 
   20247       88316 :   if (modules)
   20248             :     {
   20249        2824 :       unsigned n = dump.push (NULL);
   20250        3301 :       dump () && dump ("Imported %u modules", modules->length () - 1);
   20251        3301 :       dump () && dump ("Containing %u clusters", available_clusters);
   20252        2824 :       dump () && dump ("Loaded %u clusters (%u%%)", loaded_clusters,
   20253         477 :                        (loaded_clusters * 100 + available_clusters / 2) /
   20254         477 :                        (available_clusters + !available_clusters));
   20255        2824 :       dump.pop (n);
   20256             :     }
   20257             : 
   20258       88316 :   return cookie;
   20259             : }
   20260             : 
   20261             : // Do the final emission of a module.  At this point we know whether
   20262             : // the module static initializer is a NOP or not.
   20263             : 
   20264             : static void
   20265        1717 : late_finish_module (cpp_reader *reader,  module_processing_cookie *cookie,
   20266             :                     bool init_fn_non_empty)
   20267             : {
   20268        1717 :   timevar_start (TV_MODULE_EXPORT);
   20269             : 
   20270        1717 :   module_state *state = (*modules)[0];
   20271        1717 :   unsigned n = dump.push (state);
   20272        1717 :   state->announce ("finishing");
   20273             : 
   20274        1717 :   cookie->config.active_init = init_fn_non_empty;
   20275        1717 :   if (cookie->began)
   20276        1660 :     state->write_end (&cookie->out, reader, cookie->config, cookie->crc);
   20277             : 
   20278        1717 :   if (cookie->out.end () && cookie->cmi_name)
   20279             :     {
   20280             :       /* Some OS's do not replace NEWNAME if it already exists.
   20281             :          This'll have a race condition in erroneous concurrent
   20282             :          builds.  */
   20283        1657 :       unlink (cookie->cmi_name);
   20284        1657 :       if (rename (cookie->tmp_name, cookie->cmi_name))
   20285             :         {
   20286           0 :           dump () && dump ("Rename ('%s','%s') errno=%u",
   20287           0 :                            cookie->tmp_name, cookie->cmi_name, errno);
   20288           0 :           cookie->out.set_error (errno);
   20289             :         }
   20290             :     }
   20291             : 
   20292        1717 :   if (cookie->out.get_error () && cookie->began)
   20293             :     {
   20294           3 :       error_at (state->loc, "failed to write compiled module: %s",
   20295           3 :                 cookie->out.get_error (state->filename));
   20296           3 :       state->note_cmi_name ();
   20297             :     }
   20298             : 
   20299        1717 :   if (!errorcount)
   20300             :     {
   20301        1654 :       auto *mapper = get_mapper (cpp_main_loc (reader));
   20302        1654 :       mapper->ModuleCompiled (state->get_flatname ());
   20303             :     }
   20304          63 :   else if (cookie->cmi_name)
   20305             :     {
   20306             :       /* We failed, attempt to erase all evidence we even tried.  */
   20307          60 :       unlink (cookie->tmp_name);
   20308          60 :       unlink (cookie->cmi_name);
   20309             :     }
   20310             : 
   20311        1717 :   delete cookie;
   20312        1717 :   dump.pop (n);
   20313        1717 :   timevar_stop (TV_MODULE_EXPORT);
   20314        1717 : }
   20315             : 
   20316             : void
   20317       88316 : fini_modules (cpp_reader *reader, void *cookie, bool has_inits)
   20318             : {
   20319       88316 :   if (cookie)
   20320        1717 :     late_finish_module (reader,
   20321             :                         static_cast<module_processing_cookie *> (cookie),
   20322             :                         has_inits);
   20323             : 
   20324             :   /* We're done with the macro tables now.  */
   20325       88316 :   vec_free (macro_exports);
   20326       88316 :   vec_free (macro_imports);
   20327       88316 :   headers = NULL;
   20328             : 
   20329             :   /* We're now done with everything but the module names.  */
   20330       88316 :   set_cmi_repo (NULL);
   20331       88316 :   if (mapper)
   20332             :     {
   20333        2824 :       timevar_start (TV_MODULE_MAPPER);
   20334        2824 :       module_client::close_module_client (0, mapper);
   20335        2824 :       mapper = nullptr;
   20336        2824 :       timevar_stop (TV_MODULE_MAPPER);
   20337             :     }
   20338       88316 :   module_state_config::release ();
   20339             : 
   20340             : #if CHECKING_P
   20341       88316 :   note_defs = NULL;
   20342             : #endif
   20343             : 
   20344       88316 :   if (modules)
   20345        2824 :     for (unsigned ix = modules->length (); --ix;)
   20346        1813 :       if (module_state *state = (*modules)[ix])
   20347        6450 :         state->release ();
   20348             : 
   20349             :   /* No need to lookup modules anymore.  */
   20350       88316 :   modules_hash = NULL;
   20351             : 
   20352             :   /* Or entity array.  We still need the entity map to find import numbers.  */
   20353       88316 :   vec_free (entity_ary);
   20354       88316 :   entity_ary = NULL;
   20355             : 
   20356             :   /* Or remember any pending entities.  */
   20357       91140 :   delete pending_table;
   20358       88316 :   pending_table = NULL;
   20359             : 
   20360             :   /* Or any keys -- Let it go!  */
   20361       89753 :   delete keyed_table;
   20362       88316 :   keyed_table = NULL;
   20363             : 
   20364             :   /* Allow a GC, we've possibly made much data unreachable.  */
   20365       88316 :   ggc_collect ();
   20366       88316 : }
   20367             : 
   20368             : /* If CODE is a module option, handle it & return true.  Otherwise
   20369             :    return false.  For unknown reasons I cannot get the option
   20370             :    generation machinery to set fmodule-mapper or -fmodule-header to
   20371             :    make a string type option variable.  */
   20372             : 
   20373             : bool
   20374     1760189 : handle_module_option (unsigned code, const char *str, int)
   20375             : {
   20376     1760189 :   auto hdr = CMS_header;
   20377             : 
   20378     1760189 :   switch (opt_code (code))
   20379             :     {
   20380          36 :     case OPT_fmodule_mapper_:
   20381          36 :       module_mapper_name = str;
   20382          36 :       return true;
   20383             : 
   20384          15 :     case OPT_fmodule_header_:
   20385          15 :       {
   20386          15 :         if (!strcmp (str, "user"))
   20387             :           hdr = CMS_user;
   20388          15 :         else if (!strcmp (str, "system"))
   20389             :           hdr = CMS_system;
   20390             :         else
   20391           0 :           error ("unknown header kind %qs", str);
   20392             :       }
   20393             :       /* Fallthrough.  */
   20394             : 
   20395         664 :     case OPT_fmodule_header:
   20396         664 :       flag_header_unit = hdr;
   20397         664 :       flag_modules = 1;
   20398         664 :       return true;
   20399             : 
   20400           3 :     case OPT_flang_info_include_translate_:
   20401           3 :       vec_safe_push (note_includes, str);
   20402           3 :       return true;
   20403             : 
   20404           6 :     case OPT_flang_info_module_cmi_:
   20405           6 :       vec_safe_push (note_cmis, str);
   20406           6 :       return true;
   20407             : 
   20408             :     default:
   20409             :       return false;
   20410             :     }
   20411             : }
   20412             : 
   20413             : /* Set preprocessor callbacks and options for modules.  */
   20414             : 
   20415             : void
   20416       89300 : module_preprocess_options (cpp_reader *reader)
   20417             : {
   20418       89300 :   gcc_checking_assert (!lang_hooks.preprocess_undef);
   20419       89300 :   if (modules_p ())
   20420             :     {
   20421        2882 :       auto *cb = cpp_get_callbacks (reader);
   20422             :       
   20423        2882 :       cb->translate_include = maybe_translate_include;
   20424        2882 :       cb->user_deferred_macro = module_state::deferred_macro;
   20425        2882 :       if (flag_header_unit)
   20426             :         {
   20427             :           /* If the preprocessor hook is already in use, that
   20428             :              implementation will call the undef langhook.  */
   20429         661 :           if (cb->undef)
   20430           0 :             lang_hooks.preprocess_undef = module_state::undef_macro;
   20431             :           else
   20432         661 :             cb->undef = module_state::undef_macro;
   20433             :         }
   20434        2882 :       auto *opt = cpp_get_options (reader);
   20435        2882 :       opt->module_directives = true;
   20436        2882 :       opt->main_search = cpp_main_search (flag_header_unit);
   20437             :     }
   20438       89300 : }
   20439             : 
   20440             : #include "gt-cp-module.h"

Generated by: LCOV version 1.16