Branch data Line data Source code
1 : : /* C++ modules. Experimental!
2 : : Copyright (C) 2017-2025 Free Software Foundation, Inc.
3 : : Written by Nathan Sidwell <nathan@acm.org> while at FaceBook
4 : :
5 : : This file is part of GCC.
6 : :
7 : : GCC is free software; you can redistribute it and/or modify it
8 : : under the terms of the GNU General Public License as published by
9 : : the Free Software Foundation; either version 3, or (at your option)
10 : : any later version.
11 : :
12 : : GCC is distributed in the hope that it will be useful, but
13 : : WITHOUT ANY WARRANTY; without even the implied warranty of
14 : : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 : : General Public License for more details.
16 : :
17 : : You should have received a copy of the GNU General Public License
18 : : along with GCC; see the file COPYING3. If not see
19 : : <http://www.gnu.org/licenses/>. */
20 : :
21 : : /* Comments in this file have a non-negligible chance of being wrong
22 : : or at least inaccurate. Due to (a) my misunderstanding, (b)
23 : : ambiguities that I have interpretted differently to original intent
24 : : (c) changes in the specification, (d) my poor wording, (e) source
25 : : changes. */
26 : :
27 : : /* (Incomplete) Design Notes
28 : :
29 : : A hash table contains all module names. Imported modules are
30 : : present in a modules array, which by construction places an
31 : : import's dependencies before the import itself. The single
32 : : exception is the current TU, which always occupies slot zero (even
33 : : when it is not a module).
34 : :
35 : : Imported decls occupy an entity_ary, an array of binding_slots, indexed
36 : : by importing module and index within that module. A flat index is
37 : : used, as each module reserves a contiguous range of indices.
38 : : Initially each slot indicates the CMI section containing the
39 : : streamed decl. When the decl is imported it will point to the decl
40 : : itself.
41 : :
42 : : Additionally each imported decl is mapped in the entity_map via its
43 : : DECL_UID to the flat index in the entity_ary. Thus we can locate
44 : : the index for any imported decl by using this map and then
45 : : de-flattening the index via a binary seach of the module vector.
46 : : Cross-module references are by (remapped) module number and
47 : : module-local index.
48 : :
49 : : Each importable DECL contains several flags. The simple set are
50 : : DECL_MODULE_EXPORT_P, DECL_MODULE_PURVIEW_P, DECL_MODULE_ATTACH_P
51 : : and DECL_MODULE_IMPORT_P. The first indicates whether it is
52 : : exported, the second whether it is in module or header-unit
53 : : purview. The third indicates it is attached to the named module in
54 : : whose purview it resides and the fourth indicates whether it was an
55 : : import into this TU or not. DECL_MODULE_ATTACH_P will be false for
56 : : all decls in a header-unit, and for those in a named module inside
57 : : a linkage declaration.
58 : :
59 : : The more detailed flags are DECL_MODULE_PARTITION_P,
60 : : DECL_MODULE_ENTITY_P. The first is set in a primary interface unit
61 : : on decls that were read from module partitions (these will have
62 : : DECL_MODULE_IMPORT_P set too). Such decls will be streamed out to
63 : : the primary's CMI. DECL_MODULE_ENTITY_P is set when an entity is
64 : : imported, even if it matched a non-imported entity. Such a decl
65 : : will not have DECL_MODULE_IMPORT_P set, even though it has an entry
66 : : in the entity map and array.
67 : :
68 : : Header units are module-like.
69 : :
70 : : For namespace-scope lookup, the decls for a particular module are
71 : : held located in a sparse array hanging off the binding of the name.
72 : : This is partitioned into two: a few fixed slots at the start
73 : : followed by the sparse slots afterwards. By construction we only
74 : : need to append new slots to the end -- there is never a need to
75 : : insert in the middle. The fixed slots are MODULE_SLOT_CURRENT for
76 : : the current TU (regardless of whether it is a module or not),
77 : : MODULE_SLOT_GLOBAL and MODULE_SLOT_PARTITION. These latter two
78 : : slots are used for merging entities across the global module and
79 : : module partitions respectively. MODULE_SLOT_PARTITION is only
80 : : present in a module. Neither of those two slots is searched during
81 : : name lookup -- they are internal use only. This vector is created
82 : : lazily once we require it, if there is only a declaration from the
83 : : current TU, a regular binding is present. It is converted on
84 : : demand.
85 : :
86 : : OPTIMIZATION: Outside of the current TU, we only need ADL to work.
87 : : We could optimize regular lookup for the current TU by glomming all
88 : : the visible decls on its slot. Perhaps wait until design is a
89 : : little more settled though.
90 : :
91 : : There is only one instance of each extern-linkage namespace. It
92 : : appears in every module slot that makes it visible. It also
93 : : appears in MODULE_SLOT_GLOBAL. (It is an ODR violation if they
94 : : collide with some other global module entity.) We also have an
95 : : optimization that shares the slot for adjacent modules that declare
96 : : the same such namespace.
97 : :
98 : : A module interface compilation produces a Compiled Module Interface
99 : : (CMI). The format used is Encapsulated Lazy Records Of Numbered
100 : : Declarations, which is essentially ELF's section encapsulation. (As
101 : : all good nerds are aware, Elrond is half Elf.) Some sections are
102 : : named, and contain information about the module as a whole (indices
103 : : etc), and other sections are referenced by number. Although I
104 : : don't defend against actively hostile CMIs, there is some
105 : : checksumming involved to verify data integrity. When dumping out
106 : : an interface, we generate a graph of all the
107 : : independently-redeclarable DECLS that are needed, and the decls
108 : : they reference. From that we determine the strongly connected
109 : : components (SCC) within this TU. Each SCC is dumped to a separate
110 : : numbered section of the CMI. We generate a binding table section,
111 : : mapping each namespace&name to a defining section. This allows
112 : : lazy loading.
113 : :
114 : : Lazy loading employs mmap to map a read-only image of the CMI.
115 : : It thus only occupies address space and is paged in on demand,
116 : : backed by the CMI file itself. If mmap is unavailable, regular
117 : : FILEIO is used. Also, there's a bespoke ELF reader/writer here,
118 : : which implements just the section table and sections (including
119 : : string sections) of a 32-bit ELF in host byte-order. You can of
120 : : course inspect it with readelf. I figured 32-bit is sufficient,
121 : : for a single module. I detect running out of section numbers, but
122 : : do not implement the ELF overflow mechanism. At least you'll get
123 : : an error if that happens.
124 : :
125 : : We do not separate declarations and definitions. My guess is that
126 : : if you refer to the declaration, you'll also need the definition
127 : : (template body, inline function, class definition etc). But this
128 : : does mean we can get larger SCCs than if we separated them. It is
129 : : unclear whether this is a win or not.
130 : :
131 : : Notice that we embed section indices into the contents of other
132 : : sections. Thus random manipulation of the CMI file by ELF tools
133 : : may well break it. The kosher way would probably be to introduce
134 : : indirection via section symbols, but that would require defining a
135 : : relocation type.
136 : :
137 : : Notice that lazy loading of one module's decls can cause lazy
138 : : loading of other decls in the same or another module. Clearly we
139 : : want to avoid loops. In a correct program there can be no loops in
140 : : the module dependency graph, and the above-mentioned SCC algorithm
141 : : places all intra-module circular dependencies in the same SCC. It
142 : : also orders the SCCs wrt each other, so dependent SCCs come first.
143 : : As we load dependent modules first, we know there can be no
144 : : reference to a higher-numbered module, and because we write out
145 : : dependent SCCs first, likewise for SCCs within the module. This
146 : : allows us to immediately detect broken references. When loading,
147 : : we must ensure the rest of the compiler doesn't cause some
148 : : unconnected load to occur (for instance, instantiate a template).
149 : :
150 : : Classes used:
151 : :
152 : : dumper - logger
153 : :
154 : : data - buffer
155 : :
156 : : bytes_in : data - scalar reader
157 : : bytes_out : data - scalar writer
158 : :
159 : : bytes_in::bits_in - bit stream reader
160 : : bytes_out::bits_out - bit stream writer
161 : :
162 : : elf - ELROND format
163 : : elf_in : elf - ELROND reader
164 : : elf_out : elf - ELROND writer
165 : :
166 : : trees_in : bytes_in - tree reader
167 : : trees_out : bytes_out - tree writer
168 : :
169 : : depset - dependency set
170 : : depset::hash - hash table of depsets
171 : : depset::tarjan - SCC determinator
172 : :
173 : : uidset<T> - set T's related to a UID
174 : : uidset<T>::hash hash table of uidset<T>
175 : :
176 : : loc_spans - location map data
177 : :
178 : : module_state - module object
179 : :
180 : : slurping - data needed during loading
181 : :
182 : : macro_import - imported macro data
183 : : macro_export - exported macro data
184 : :
185 : : The ELROND objects use mmap, for both reading and writing. If mmap
186 : : is unavailable, fileno IO is used to read and write blocks of data.
187 : :
188 : : The mapper object uses fileno IO to communicate with the server or
189 : : program. */
190 : :
191 : : /* In expermental (trunk) sources, MODULE_VERSION is a #define passed
192 : : in from the Makefile. It records the modification date of the
193 : : source directory -- that's the only way to stay sane. In release
194 : : sources, we (plan to) use the compiler's major.minor versioning.
195 : : While the format might not change between at minor versions, it
196 : : seems simplest to tie the two together. There's no concept of
197 : : inter-version compatibility. */
198 : : #define IS_EXPERIMENTAL(V) ((V) >= (1U << 20))
199 : : #define MODULE_MAJOR(V) ((V) / 10000)
200 : : #define MODULE_MINOR(V) ((V) % 10000)
201 : : #define EXPERIMENT(A,B) (IS_EXPERIMENTAL (MODULE_VERSION) ? (A) : (B))
202 : : #ifndef MODULE_VERSION
203 : : #include "bversion.h"
204 : : #define MODULE_VERSION (BUILDING_GCC_MAJOR * 10000U + BUILDING_GCC_MINOR)
205 : : #elif !IS_EXPERIMENTAL (MODULE_VERSION)
206 : : #error "This is not the version I was looking for."
207 : : #endif
208 : :
209 : : #define _DEFAULT_SOURCE 1 /* To get TZ field of struct tm, if available. */
210 : : #include "config.h"
211 : : #define INCLUDE_STRING
212 : : #define INCLUDE_VECTOR
213 : : #include "system.h"
214 : : #include "coretypes.h"
215 : : #include "cp-tree.h"
216 : : #include "timevar.h"
217 : : #include "stringpool.h"
218 : : #include "dumpfile.h"
219 : : #include "bitmap.h"
220 : : #include "cgraph.h"
221 : : #include "varasm.h"
222 : : #include "tree-iterator.h"
223 : : #include "cpplib.h"
224 : : #include "mkdeps.h"
225 : : #include "incpath.h"
226 : : #include "libiberty.h"
227 : : #include "stor-layout.h"
228 : : #include "version.h"
229 : : #include "tree-diagnostic.h"
230 : : #include "toplev.h"
231 : : #include "opts.h"
232 : : #include "attribs.h"
233 : : #include "intl.h"
234 : : #include "langhooks.h"
235 : : /* This TU doesn't need or want to see the networking. */
236 : : #define CODY_NETWORKING 0
237 : : #include "mapper-client.h"
238 : : #include <zlib.h> // for crc32, crc32_combine
239 : :
240 : : #if 0 // 1 for testing no mmap
241 : : #define MAPPED_READING 0
242 : : #define MAPPED_WRITING 0
243 : : #else
244 : : #if HAVE_MMAP_FILE && HAVE_MUNMAP && HAVE_MSYNC
245 : : /* mmap, munmap, msync. */
246 : : #define MAPPED_READING 1
247 : : #if HAVE_SYSCONF && defined (_SC_PAGE_SIZE)
248 : : /* sysconf (_SC_PAGE_SIZE), ftruncate */
249 : : /* posix_fallocate used if available. */
250 : : #define MAPPED_WRITING 1
251 : : #else
252 : : #define MAPPED_WRITING 0
253 : : #endif
254 : : #else
255 : : #define MAPPED_READING 0
256 : : #define MAPPED_WRITING 0
257 : : #endif
258 : : #endif
259 : :
260 : : /* Some open(2) flag differences, what a colourful world it is! */
261 : : #if defined (O_CLOEXEC)
262 : : // OK
263 : : #elif defined (_O_NOINHERIT)
264 : : /* Windows' _O_NOINHERIT matches O_CLOEXEC flag */
265 : : #define O_CLOEXEC _O_NOINHERIT
266 : : #else
267 : : #define O_CLOEXEC 0
268 : : #endif
269 : : #if defined (O_BINARY)
270 : : // Ok?
271 : : #elif defined (_O_BINARY)
272 : : /* Windows' open(2) call defaults to text! */
273 : : #define O_BINARY _O_BINARY
274 : : #else
275 : : #define O_BINARY 0
276 : : #endif
277 : :
278 : 219514 : static inline cpp_hashnode *cpp_node (tree id)
279 : : {
280 : 219514 : return CPP_HASHNODE (GCC_IDENT_TO_HT_IDENT (id));
281 : : }
282 : :
283 : 141982 : static inline tree identifier (const cpp_hashnode *node)
284 : : {
285 : : /* HT_NODE() expands to node->ident that HT_IDENT_TO_GCC_IDENT()
286 : : then subtracts a nonzero constant, deriving a pointer to
287 : : a different member than ident. That's strictly undefined
288 : : and detected by -Warray-bounds. Suppress it. See PR 101372. */
289 : 141982 : #pragma GCC diagnostic push
290 : 141982 : #pragma GCC diagnostic ignored "-Warray-bounds"
291 : 141982 : return HT_IDENT_TO_GCC_IDENT (HT_NODE (const_cast<cpp_hashnode *> (node)));
292 : 141982 : #pragma GCC diagnostic pop
293 : : }
294 : :
295 : : /* Id for dumping module information. */
296 : : int module_dump_id;
297 : :
298 : : /* We have a special module owner. */
299 : : #define MODULE_UNKNOWN (~0U) /* Not yet known. */
300 : :
301 : : /* Prefix for section names. */
302 : : #define MOD_SNAME_PFX ".gnu.c++"
303 : :
304 : : /* Format a version for user consumption. */
305 : :
306 : : typedef char verstr_t[32];
307 : : static void
308 : 3519 : version2string (unsigned version, verstr_t &out)
309 : : {
310 : 3519 : unsigned major = MODULE_MAJOR (version);
311 : 3519 : unsigned minor = MODULE_MINOR (version);
312 : :
313 : 3519 : if (IS_EXPERIMENTAL (version))
314 : 3519 : sprintf (out, "%04u/%02u/%02u-%02u:%02u%s",
315 : 3519 : 2000 + major / 10000, (major / 100) % 100, (major % 100),
316 : : minor / 100, minor % 100,
317 : : EXPERIMENT ("", " (experimental)"));
318 : : else
319 : 0 : sprintf (out, "%u.%u", major, minor);
320 : 3519 : }
321 : :
322 : : /* Include files to note translation for. */
323 : : static vec<const char *, va_heap, vl_embed> *note_includes;
324 : :
325 : : /* Modules to note CMI pathames. */
326 : : static vec<const char *, va_heap, vl_embed> *note_cmis;
327 : :
328 : : /* Traits to hash an arbitrary pointer. Entries are not deletable,
329 : : and removal is a noop (removal needed upon destruction). */
330 : : template <typename T>
331 : : struct nodel_ptr_hash : pointer_hash<T>, typed_noop_remove <T *> {
332 : : /* Nothing is deletable. Everything is insertable. */
333 : : static bool is_deleted (T *) { return false; }
334 : : static void mark_deleted (T *) { gcc_unreachable (); }
335 : : };
336 : :
337 : : /* Map from pointer to signed integer. */
338 : : typedef simple_hashmap_traits<nodel_ptr_hash<void>, int> ptr_int_traits;
339 : : typedef hash_map<void *,signed,ptr_int_traits> ptr_int_hash_map;
340 : :
341 : : /********************************************************************/
342 : : /* Basic streaming & ELF. Serialization is usually via mmap. For
343 : : writing we slide a buffer over the output file, syncing it
344 : : approproiately. For reading we simply map the whole file (as a
345 : : file-backed read-only map -- it's just address space, leaving the
346 : : OS pager to deal with getting the data to us). Some buffers need
347 : : to be more conventional malloc'd contents. */
348 : :
349 : : /* Variable length buffer. */
350 : :
351 : : namespace {
352 : :
353 : : constexpr line_map_uint_t loc_one = 1;
354 : :
355 : : class data {
356 : : public:
357 : 2436 : class allocator {
358 : : public:
359 : : /* Tools tend to moan if the dtor's not virtual. */
360 : 96685 : virtual ~allocator () {}
361 : :
362 : : public:
363 : : void grow (data &obj, unsigned needed, bool exact);
364 : : void shrink (data &obj);
365 : :
366 : : public:
367 : : virtual char *grow (char *ptr, unsigned needed);
368 : : virtual void shrink (char *ptr);
369 : : };
370 : :
371 : : public:
372 : : char *buffer; /* Buffer being transferred. */
373 : : /* Although size_t would be the usual size, we know we never get
374 : : more than 4GB of buffer -- because that's the limit of the
375 : : encapsulation format. And if you need bigger imports, you're
376 : : doing it wrong. */
377 : : unsigned size; /* Allocated size of buffer. */
378 : : unsigned pos; /* Position in buffer. */
379 : :
380 : : public:
381 : 652399 : data ()
382 : 652399 : :buffer (NULL), size (0), pos (0)
383 : : {
384 : : }
385 : 664511 : ~data ()
386 : : {
387 : : /* Make sure the derived and/or using class know what they're
388 : : doing. */
389 : 664511 : gcc_checking_assert (!buffer);
390 : 664511 : }
391 : :
392 : : protected:
393 : 413366596 : char *use (unsigned count)
394 : : {
395 : 413366596 : if (size < pos + count)
396 : : return NULL;
397 : 413366596 : char *res = &buffer[pos];
398 : 413366596 : pos += count;
399 : 222471640 : return res;
400 : : }
401 : :
402 : : unsigned calc_crc (unsigned) const;
403 : :
404 : : public:
405 : 31319144 : void unuse (unsigned count)
406 : : {
407 : 31319144 : pos -= count;
408 : 23549 : }
409 : :
410 : : public:
411 : : static allocator simple_memory;
412 : : };
413 : : } // anon namespace
414 : :
415 : : /* The simple data allocator. */
416 : : data::allocator data::simple_memory;
417 : :
418 : : /* Grow buffer to at least size NEEDED. */
419 : :
420 : : void
421 : 574422 : data::allocator::grow (data &obj, unsigned needed, bool exact)
422 : : {
423 : 574422 : gcc_checking_assert (needed ? needed > obj.size : !obj.size);
424 : 574422 : if (!needed)
425 : : /* Pick a default size. */
426 : 243502 : needed = EXPERIMENT (100, 1000);
427 : :
428 : 574422 : if (!exact)
429 : 567429 : needed *= 2;
430 : 574422 : obj.buffer = grow (obj.buffer, needed);
431 : 574422 : if (obj.buffer)
432 : 574422 : obj.size = needed;
433 : : else
434 : 0 : obj.pos = obj.size = 0;
435 : 574422 : }
436 : :
437 : : /* Free a buffer. */
438 : :
439 : : void
440 : 255694 : data::allocator::shrink (data &obj)
441 : : {
442 : 0 : shrink (obj.buffer);
443 : 255694 : obj.buffer = NULL;
444 : 255694 : obj.size = 0;
445 : 0 : }
446 : :
447 : : char *
448 : 9108 : data::allocator::grow (char *ptr, unsigned needed)
449 : : {
450 : 9108 : return XRESIZEVAR (char, ptr, needed);
451 : : }
452 : :
453 : : void
454 : 12180 : data::allocator::shrink (char *ptr)
455 : : {
456 : 12180 : XDELETEVEC (ptr);
457 : 12180 : }
458 : :
459 : : /* Calculate the crc32 of the buffer. Note the CRC is stored in the
460 : : first 4 bytes, so don't include them. */
461 : :
462 : : unsigned
463 : 415589 : data::calc_crc (unsigned l) const
464 : : {
465 : 415589 : return crc32 (0, (unsigned char *)buffer + 4, l - 4);
466 : : }
467 : :
468 : : class elf_in;
469 : :
470 : : /* Byte stream reader. */
471 : :
472 : : namespace {
473 : : class bytes_in : public data {
474 : : typedef data parent;
475 : :
476 : : protected:
477 : : bool overrun; /* Sticky read-too-much flag. */
478 : :
479 : : public:
480 : 180541 : bytes_in ()
481 : 180541 : : parent (), overrun (false)
482 : : {
483 : : }
484 : 182929 : ~bytes_in ()
485 : : {
486 : 10764 : }
487 : :
488 : : public:
489 : : /* Begin reading a named section. */
490 : : bool begin (location_t loc, elf_in *src, const char *name);
491 : : /* Begin reading a numbered section with optional name. */
492 : : bool begin (location_t loc, elf_in *src, unsigned, const char * = NULL);
493 : : /* Complete reading a buffer. Propagate errors and return true on
494 : : success. */
495 : : bool end (elf_in *src);
496 : : /* Return true if there is unread data. */
497 : 1269523 : bool more_p () const
498 : : {
499 : 1269523 : return pos != size;
500 : : }
501 : :
502 : : public:
503 : : /* Start reading at OFFSET. */
504 : 320 : void random_access (unsigned offset)
505 : : {
506 : 320 : if (offset > size)
507 : 0 : set_overrun ();
508 : 320 : pos = offset;
509 : : }
510 : :
511 : : public:
512 : 1169811 : void align (unsigned boundary)
513 : : {
514 : 1169811 : if (unsigned pad = pos & (boundary - 1))
515 : 2263186 : read (boundary - pad);
516 : : }
517 : :
518 : : public:
519 : 190894956 : const char *read (unsigned count)
520 : : {
521 : 1093375 : char *ptr = use (count);
522 : 190894956 : if (!ptr)
523 : 0 : set_overrun ();
524 : 155847719 : return ptr;
525 : : }
526 : :
527 : : public:
528 : : bool check_crc () const;
529 : : /* We store the CRC in the first 4 bytes, using host endianness. */
530 : 181565 : unsigned get_crc () const
531 : : {
532 : 181565 : return *(const unsigned *)&buffer[0];
533 : : }
534 : :
535 : : public:
536 : : /* Manipulate the overrun flag. */
537 : 111852934 : bool get_overrun () const
538 : : {
539 : 111852934 : return overrun;
540 : : }
541 : 26 : void set_overrun ()
542 : : {
543 : 26 : overrun = true;
544 : 0 : }
545 : :
546 : : public:
547 : : unsigned u32 (); /* Read uncompressed integer. */
548 : :
549 : : public:
550 : : int c () ATTRIBUTE_UNUSED; /* Read a char. */
551 : : int i (); /* Read a signed int. */
552 : : unsigned u (); /* Read an unsigned int. */
553 : : size_t z (); /* Read a size_t. */
554 : : location_t loc (); /* Read a location_t. */
555 : : HOST_WIDE_INT wi (); /* Read a HOST_WIDE_INT. */
556 : : unsigned HOST_WIDE_INT wu (); /* Read an unsigned HOST_WIDE_INT. */
557 : : const char *str (size_t * = NULL); /* Read a string. */
558 : : const void *buf (size_t); /* Read a fixed-length buffer. */
559 : : cpp_hashnode *cpp_node (); /* Read a cpp node. */
560 : :
561 : : struct bits_in;
562 : : bits_in stream_bits ();
563 : : };
564 : : } // anon namespace
565 : :
566 : : /* Verify the buffer's CRC is correct. */
567 : :
568 : : bool
569 : 179043 : bytes_in::check_crc () const
570 : : {
571 : 179043 : if (size < 4)
572 : : return false;
573 : :
574 : 179043 : unsigned c_crc = calc_crc (size);
575 : 179043 : if (c_crc != get_crc ())
576 : : return false;
577 : :
578 : : return true;
579 : : }
580 : :
581 : : class elf_out;
582 : :
583 : : /* Byte stream writer. */
584 : :
585 : : namespace {
586 : : class bytes_out : public data {
587 : : typedef data parent;
588 : :
589 : : public:
590 : : allocator *memory; /* Obtainer of memory. */
591 : :
592 : : public:
593 : 466882 : bytes_out (allocator *memory)
594 : 466882 : : parent (), memory (memory)
595 : : {
596 : : }
597 : 466882 : ~bytes_out ()
598 : : {
599 : 487011 : }
600 : :
601 : : public:
602 : 534984896 : bool streaming_p () const
603 : : {
604 : 534984896 : return memory != NULL;
605 : : }
606 : :
607 : : public:
608 : : void set_crc (unsigned *crc_ptr);
609 : :
610 : : public:
611 : : /* Begin writing, maybe reserve space for CRC. */
612 : : void begin (bool need_crc = true);
613 : : /* Finish writing. Spill to section by number. */
614 : : unsigned end (elf_out *, unsigned, unsigned *crc_ptr = NULL);
615 : :
616 : : public:
617 : 1478875 : void align (unsigned boundary)
618 : : {
619 : 1478875 : if (unsigned pad = pos & (boundary - 1))
620 : 1381624 : write (boundary - pad);
621 : 1478875 : }
622 : :
623 : : public:
624 : 222471640 : char *write (unsigned count, bool exact = false)
625 : : {
626 : 222471640 : if (size < pos + count)
627 : 319473 : memory->grow (*this, pos + count, exact);
628 : 222471640 : return use (count);
629 : : }
630 : :
631 : : public:
632 : : void u32 (unsigned); /* Write uncompressed integer. */
633 : :
634 : : public:
635 : : void c (unsigned char) ATTRIBUTE_UNUSED; /* Write unsigned char. */
636 : : void i (int); /* Write signed int. */
637 : : void u (unsigned); /* Write unsigned int. */
638 : : void z (size_t s); /* Write size_t. */
639 : : void loc (location_t); /* Write location_t. */
640 : : void wi (HOST_WIDE_INT); /* Write HOST_WIDE_INT. */
641 : : void wu (unsigned HOST_WIDE_INT); /* Write unsigned HOST_WIDE_INT. */
642 : 14583 : void str (const char *ptr)
643 : : {
644 : 14583 : str (ptr, strlen (ptr));
645 : 14583 : }
646 : 240062 : void cpp_node (const cpp_hashnode *node)
647 : : {
648 : 240062 : str ((const char *)NODE_NAME (node), NODE_LEN (node));
649 : 11248 : }
650 : : void str (const char *, size_t); /* Write string of known length. */
651 : : void buf (const void *, size_t); /* Write fixed length buffer. */
652 : : void *buf (size_t); /* Create a writable buffer */
653 : :
654 : : struct bits_out;
655 : : bits_out stream_bits ();
656 : :
657 : : public:
658 : : /* Format a NUL-terminated raw string. */
659 : : void printf (const char *, ...) ATTRIBUTE_PRINTF_2;
660 : : void print_time (const char *, const tm *, const char *);
661 : :
662 : : public:
663 : : /* Dump instrumentation. */
664 : : static void instrument ();
665 : :
666 : : protected:
667 : : /* Instrumentation. */
668 : : static unsigned spans[4];
669 : : static unsigned lengths[4];
670 : : };
671 : : } // anon namespace
672 : :
673 : : /* Finish bit packet. Rewind the bytes not used. */
674 : :
675 : : static unsigned
676 : 31272308 : bit_flush (data& bits, uint32_t& bit_val, unsigned& bit_pos)
677 : : {
678 : 31272308 : gcc_assert (bit_pos);
679 : 31272308 : unsigned bytes = (bit_pos + 7) / 8;
680 : 31272308 : bits.unuse (4 - bytes);
681 : 31272308 : bit_pos = 0;
682 : 31272308 : bit_val = 0;
683 : 31272308 : return bytes;
684 : : }
685 : :
686 : : /* Bit stream reader (RAII-enabled). Bools are packed into bytes. You
687 : : cannot mix bools and non-bools. Use bflush to flush the current stream
688 : : of bools on demand. Upon destruction bflush is called.
689 : :
690 : : When reading, we don't know how many bools we'll read in. So read
691 : : 4 bytes-worth, and then rewind when flushing if we didn't need them
692 : : all. You can't have a block of bools closer than 4 bytes to the
693 : : end of the buffer.
694 : :
695 : : Both bits_in and bits_out maintain the necessary state for bit packing,
696 : : and since these objects are locally constructed the compiler can more
697 : : easily track their state across consecutive reads/writes and optimize
698 : : away redundant buffering checks. */
699 : :
700 : : struct bytes_in::bits_in {
701 : : bytes_in& in;
702 : : uint32_t bit_val = 0;
703 : : unsigned bit_pos = 0;
704 : :
705 : 10641926 : bits_in (bytes_in& in)
706 : 10641926 : : in (in)
707 : : { }
708 : :
709 : 10641926 : ~bits_in ()
710 : : {
711 : 9953446 : bflush ();
712 : 10641926 : }
713 : :
714 : : bits_in(bits_in&&) = default;
715 : : bits_in(const bits_in&) = delete;
716 : : bits_in& operator=(const bits_in&) = delete;
717 : :
718 : : /* Completed a block of bools. */
719 : 22115828 : void bflush ()
720 : : {
721 : 22115828 : if (bit_pos)
722 : 12162382 : bit_flush (in, bit_val, bit_pos);
723 : 22115828 : }
724 : :
725 : : /* Read one bit. */
726 : 364654523 : bool b ()
727 : : {
728 : 364654523 : if (!bit_pos)
729 : 15901318 : bit_val = in.u32 ();
730 : 364654523 : bool x = (bit_val >> bit_pos) & 1;
731 : 364654523 : bit_pos = (bit_pos + 1) % 32;
732 : 364654523 : return x;
733 : : }
734 : : };
735 : :
736 : : /* Factory function for bits_in. */
737 : :
738 : : bytes_in::bits_in
739 : 10641926 : bytes_in::stream_bits ()
740 : : {
741 : 10641926 : return bits_in (*this);
742 : : }
743 : :
744 : : /* Bit stream writer (RAII-enabled), counterpart to bits_in. */
745 : :
746 : : struct bytes_out::bits_out {
747 : : bytes_out& out;
748 : : uint32_t bit_val = 0;
749 : : unsigned bit_pos = 0;
750 : : char is_set = -1;
751 : :
752 : 12689801 : bits_out (bytes_out& out)
753 : 12689801 : : out (out)
754 : : { }
755 : :
756 : 12689801 : ~bits_out ()
757 : : {
758 : 70948 : bflush ();
759 : : }
760 : :
761 : : bits_out(bits_out&&) = default;
762 : : bits_out(const bits_out&) = delete;
763 : : bits_out& operator=(const bits_out&) = delete;
764 : :
765 : : /* Completed a block of bools. */
766 : 26374478 : void bflush ()
767 : : {
768 : 26374478 : if (bit_pos)
769 : : {
770 : 14539014 : out.u32 (bit_val);
771 : 14539014 : out.lengths[2] += bit_flush (out, bit_val, bit_pos);
772 : : }
773 : 26374478 : out.spans[2]++;
774 : 26374478 : is_set = -1;
775 : 26374478 : }
776 : :
777 : : /* Write one bit.
778 : :
779 : : It may be worth optimizing for most bools being zero. Some kind of
780 : : run-length encoding? */
781 : 434398414 : void b (bool x)
782 : : {
783 : 434398414 : if (is_set != x)
784 : : {
785 : 46041773 : is_set = x;
786 : 46041773 : out.spans[x]++;
787 : : }
788 : 434398414 : out.lengths[x]++;
789 : 434398414 : bit_val |= unsigned (x) << bit_pos++;
790 : 434398414 : if (bit_pos == 32)
791 : : {
792 : 4570912 : out.u32 (bit_val);
793 : 4570912 : out.lengths[2] += bit_flush (out, bit_val, bit_pos);
794 : : }
795 : 434398414 : }
796 : : };
797 : :
798 : : /* Factory function for bits_out. */
799 : :
800 : : bytes_out::bits_out
801 : 12689801 : bytes_out::stream_bits ()
802 : : {
803 : 12689801 : return bits_out (*this);
804 : : }
805 : :
806 : : /* Instrumentation. */
807 : : unsigned bytes_out::spans[4];
808 : : unsigned bytes_out::lengths[4];
809 : :
810 : : /* If CRC_PTR non-null, set the CRC of the buffer. Mix the CRC into
811 : : that pointed to by CRC_PTR. */
812 : :
813 : : void
814 : 238848 : bytes_out::set_crc (unsigned *crc_ptr)
815 : : {
816 : 238848 : if (crc_ptr)
817 : : {
818 : 236546 : gcc_checking_assert (pos >= 4);
819 : :
820 : 236546 : unsigned crc = calc_crc (pos);
821 : 236546 : unsigned accum = *crc_ptr;
822 : : /* Only mix the existing *CRC_PTR if it is non-zero. */
823 : 236546 : accum = accum ? crc32_combine (accum, crc, pos - 4) : crc;
824 : 236546 : *crc_ptr = accum;
825 : :
826 : : /* Buffer will be sufficiently aligned. */
827 : 236546 : *(unsigned *)buffer = crc;
828 : : }
829 : 238848 : }
830 : :
831 : : /* Exactly 4 bytes. Used internally for bool packing and a few other
832 : : places. We can't simply use uint32_t because (a) alignment and
833 : : (b) we need little-endian for the bool streaming rewinding to make
834 : : sense. */
835 : :
836 : : void
837 : 19117366 : bytes_out::u32 (unsigned val)
838 : : {
839 : 19117366 : if (char *ptr = write (4))
840 : : {
841 : 19117366 : ptr[0] = val;
842 : 19117366 : ptr[1] = val >> 8;
843 : 19117366 : ptr[2] = val >> 16;
844 : 19117366 : ptr[3] = val >> 24;
845 : : }
846 : 19117366 : }
847 : :
848 : : unsigned
849 : 15909219 : bytes_in::u32 ()
850 : : {
851 : 15909219 : unsigned val = 0;
852 : 15909219 : if (const char *ptr = read (4))
853 : : {
854 : 15909219 : val |= (unsigned char)ptr[0];
855 : 15909219 : val |= (unsigned char)ptr[1] << 8;
856 : 15909219 : val |= (unsigned char)ptr[2] << 16;
857 : 15909219 : val |= (unsigned char)ptr[3] << 24;
858 : : }
859 : :
860 : 15909219 : return val;
861 : : }
862 : :
863 : : /* Chars are unsigned and written as single bytes. */
864 : :
865 : : void
866 : 0 : bytes_out::c (unsigned char v)
867 : : {
868 : 0 : if (char *ptr = write (1))
869 : 0 : *ptr = v;
870 : 0 : }
871 : :
872 : : int
873 : 0 : bytes_in::c ()
874 : : {
875 : 0 : int v = 0;
876 : 0 : if (const char *ptr = read (1))
877 : 0 : v = (unsigned char)ptr[0];
878 : 0 : return v;
879 : : }
880 : :
881 : : /* Ints 7-bit as a byte. Otherwise a 3bit count of following bytes in
882 : : big-endian form. 4 bits are in the first byte. */
883 : :
884 : : void
885 : 79325817 : bytes_out::i (int v)
886 : : {
887 : 79325817 : if (char *ptr = write (1))
888 : : {
889 : 79325817 : if (v <= 0x3f && v >= -0x40)
890 : 63072019 : *ptr = v & 0x7f;
891 : : else
892 : : {
893 : 16253798 : unsigned bytes = 0;
894 : 16253798 : int probe;
895 : 16253798 : if (v >= 0)
896 : 0 : for (probe = v >> 8; probe > 0x7; probe >>= 8)
897 : 0 : bytes++;
898 : : else
899 : 25037579 : for (probe = v >> 8; probe < -0x8; probe >>= 8)
900 : 8783781 : bytes++;
901 : 16253798 : *ptr = 0x80 | bytes << 4 | (probe & 0xf);
902 : 16253798 : if ((ptr = write (++bytes)))
903 : 41291377 : for (; bytes--; v >>= 8)
904 : 25037579 : ptr[bytes] = v & 0xff;
905 : : }
906 : : }
907 : 79325817 : }
908 : :
909 : : int
910 : 67345699 : bytes_in::i ()
911 : : {
912 : 67345699 : int v = 0;
913 : 67345699 : if (const char *ptr = read (1))
914 : : {
915 : 67345699 : v = *ptr & 0xff;
916 : 67345699 : if (v & 0x80)
917 : : {
918 : 14478241 : unsigned bytes = (v >> 4) & 0x7;
919 : 14478241 : v &= 0xf;
920 : 14478241 : if (v & 0x8)
921 : 14478241 : v |= -1 ^ 0x7;
922 : : /* unsigned necessary due to left shifts of -ve values. */
923 : 14478241 : unsigned uv = unsigned (v);
924 : 14478241 : if ((ptr = read (++bytes)))
925 : 37341220 : while (bytes--)
926 : 22862979 : uv = (uv << 8) | (*ptr++ & 0xff);
927 : 14478241 : v = int (uv);
928 : : }
929 : 52867458 : else if (v & 0x40)
930 : 7237381 : v |= -1 ^ 0x3f;
931 : : }
932 : :
933 : 67345699 : return v;
934 : : }
935 : :
936 : : void
937 : 67459422 : bytes_out::u (unsigned v)
938 : : {
939 : 67459422 : if (char *ptr = write (1))
940 : : {
941 : 67459422 : if (v <= 0x7f)
942 : 58795948 : *ptr = v;
943 : : else
944 : : {
945 : 8663474 : unsigned bytes = 0;
946 : 8663474 : unsigned probe;
947 : 10184717 : for (probe = v >> 8; probe > 0xf; probe >>= 8)
948 : 1521243 : bytes++;
949 : 8663474 : *ptr = 0x80 | bytes << 4 | probe;
950 : 8663474 : if ((ptr = write (++bytes)))
951 : 18848191 : for (; bytes--; v >>= 8)
952 : 10184717 : ptr[bytes] = v & 0xff;
953 : : }
954 : : }
955 : 67459422 : }
956 : :
957 : : unsigned
958 : 58229751 : bytes_in::u ()
959 : : {
960 : 58229751 : unsigned v = 0;
961 : :
962 : 58229751 : if (const char *ptr = read (1))
963 : : {
964 : 58229751 : v = *ptr & 0xff;
965 : 58229751 : if (v & 0x80)
966 : : {
967 : 7701467 : unsigned bytes = (v >> 4) & 0x7;
968 : 7701467 : v &= 0xf;
969 : 7701467 : if ((ptr = read (++bytes)))
970 : 16870104 : while (bytes--)
971 : 9168637 : v = (v << 8) | (*ptr++ & 0xff);
972 : : }
973 : : }
974 : :
975 : 58229751 : return v;
976 : : }
977 : :
978 : : void
979 : 15980876 : bytes_out::wi (HOST_WIDE_INT v)
980 : : {
981 : 15980876 : if (char *ptr = write (1))
982 : : {
983 : 15980876 : if (v <= 0x3f && v >= -0x40)
984 : 3217324 : *ptr = v & 0x7f;
985 : : else
986 : : {
987 : 12763552 : unsigned bytes = 0;
988 : 12763552 : HOST_WIDE_INT probe;
989 : 12763552 : if (v >= 0)
990 : 45855468 : for (probe = v >> 8; probe > 0x7; probe >>= 8)
991 : 33093634 : bytes++;
992 : : else
993 : 5704 : for (probe = v >> 8; probe < -0x8; probe >>= 8)
994 : 3986 : bytes++;
995 : 12763552 : *ptr = 0x80 | bytes << 4 | (probe & 0xf);
996 : 12763552 : if ((ptr = write (++bytes)))
997 : 58624724 : for (; bytes--; v >>= 8)
998 : 45861172 : ptr[bytes] = v & 0xff;
999 : : }
1000 : : }
1001 : 15980876 : }
1002 : :
1003 : : HOST_WIDE_INT
1004 : 13193239 : bytes_in::wi ()
1005 : : {
1006 : 13193239 : HOST_WIDE_INT v = 0;
1007 : 13193239 : if (const char *ptr = read (1))
1008 : : {
1009 : 13193239 : v = *ptr & 0xff;
1010 : 13193239 : if (v & 0x80)
1011 : : {
1012 : 11774154 : unsigned bytes = (v >> 4) & 0x7;
1013 : 11774154 : v &= 0xf;
1014 : 11774154 : if (v & 0x8)
1015 : 1430 : v |= -1 ^ 0x7;
1016 : : /* unsigned necessary due to left shifts of -ve values. */
1017 : 11774154 : unsigned HOST_WIDE_INT uv = (unsigned HOST_WIDE_INT) v;
1018 : 11774154 : if ((ptr = read (++bytes)))
1019 : 53930754 : while (bytes--)
1020 : 42156600 : uv = (uv << 8) | (*ptr++ & 0xff);
1021 : 11774154 : v = (HOST_WIDE_INT) uv;
1022 : : }
1023 : 1419085 : else if (v & 0x40)
1024 : 6556 : v |= -1 ^ 0x3f;
1025 : : }
1026 : :
1027 : 13193239 : return v;
1028 : : }
1029 : :
1030 : : /* unsigned wide ints are just written as signed wide ints. */
1031 : :
1032 : : inline void
1033 : 15980312 : bytes_out::wu (unsigned HOST_WIDE_INT v)
1034 : : {
1035 : 15980312 : wi ((HOST_WIDE_INT) v);
1036 : : }
1037 : :
1038 : : inline unsigned HOST_WIDE_INT
1039 : 13192749 : bytes_in::wu ()
1040 : : {
1041 : 25956504 : return (unsigned HOST_WIDE_INT) wi ();
1042 : : }
1043 : :
1044 : : /* size_t written as unsigned or unsigned wide int. */
1045 : :
1046 : : inline void
1047 : 1454809 : bytes_out::z (size_t s)
1048 : : {
1049 : 1454809 : if (sizeof (s) == sizeof (unsigned))
1050 : : u (s);
1051 : : else
1052 : 2876458 : wu (s);
1053 : 12 : }
1054 : :
1055 : : inline size_t
1056 : 1156396 : bytes_in::z ()
1057 : : {
1058 : 1156396 : if (sizeof (size_t) == sizeof (unsigned))
1059 : : return u ();
1060 : : else
1061 : 2312792 : return wu ();
1062 : : }
1063 : :
1064 : : /* location_t written as 32- or 64-bit as needed. */
1065 : :
1066 : 14000994 : inline void bytes_out::loc (location_t l)
1067 : : {
1068 : 14000994 : if (sizeof (location_t) > sizeof (unsigned))
1069 : 26565179 : wu (l);
1070 : : else
1071 : : u (l);
1072 : 1434507 : }
1073 : :
1074 : 11609838 : inline location_t bytes_in::loc ()
1075 : : {
1076 : 11609838 : if (sizeof (location_t) > sizeof (unsigned))
1077 : 23217167 : return wu ();
1078 : : else
1079 : : return u ();
1080 : : }
1081 : :
1082 : : /* Buffer simply memcpied. */
1083 : : void *
1084 : 1478875 : bytes_out::buf (size_t len)
1085 : : {
1086 : 1478875 : align (sizeof (void *) * 2);
1087 : 1478875 : return write (len);
1088 : : }
1089 : :
1090 : : void
1091 : 1434003 : bytes_out::buf (const void *src, size_t len)
1092 : : {
1093 : 1434003 : if (void *ptr = buf (len))
1094 : 1434003 : memcpy (ptr, src, len);
1095 : 1434003 : }
1096 : :
1097 : : const void *
1098 : 1169811 : bytes_in::buf (size_t len)
1099 : : {
1100 : 1169811 : align (sizeof (void *) * 2);
1101 : 1169811 : const char *ptr = read (len);
1102 : :
1103 : 1169811 : return ptr;
1104 : : }
1105 : :
1106 : : /* strings as an size_t length, followed by the buffer. Make sure
1107 : : there's a NUL terminator on read. */
1108 : :
1109 : : void
1110 : 1454791 : bytes_out::str (const char *string, size_t len)
1111 : : {
1112 : 1421643 : z (len);
1113 : 1421643 : if (len)
1114 : : {
1115 : 1421643 : gcc_checking_assert (!string[len]);
1116 : 1421643 : buf (string, len + 1);
1117 : : }
1118 : 33148 : }
1119 : :
1120 : : const char *
1121 : 1156390 : bytes_in::str (size_t *len_p)
1122 : : {
1123 : 1156390 : size_t len = z ();
1124 : :
1125 : : /* We're about to trust some user data. */
1126 : 1156390 : if (overrun)
1127 : 0 : len = 0;
1128 : 1156390 : if (len_p)
1129 : 1153175 : *len_p = len;
1130 : 1156390 : const char *str = NULL;
1131 : 1156390 : if (len)
1132 : : {
1133 : 1156193 : str = reinterpret_cast<const char *> (buf (len + 1));
1134 : 1156193 : if (!str || str[len])
1135 : : {
1136 : 0 : set_overrun ();
1137 : 0 : str = NULL;
1138 : : }
1139 : : }
1140 : 0 : return str ? str : "";
1141 : : }
1142 : :
1143 : : cpp_hashnode *
1144 : 219711 : bytes_in::cpp_node ()
1145 : : {
1146 : 219711 : size_t len;
1147 : 219711 : const char *s = str (&len);
1148 : 219711 : if (!len)
1149 : : return NULL;
1150 : 219514 : return ::cpp_node (get_identifier_with_length (s, len));
1151 : : }
1152 : :
1153 : : /* Format a string directly to the buffer, including a terminating
1154 : : NUL. Intended for human consumption. */
1155 : :
1156 : : void
1157 : 23549 : bytes_out::printf (const char *format, ...)
1158 : : {
1159 : 23549 : va_list args;
1160 : : /* Exercise buffer expansion. */
1161 : 23549 : size_t len = EXPERIMENT (10, 500);
1162 : :
1163 : 46836 : while (char *ptr = write (len))
1164 : : {
1165 : 46836 : va_start (args, format);
1166 : 46836 : size_t actual = vsnprintf (ptr, len, format, args) + 1;
1167 : 46836 : va_end (args);
1168 : 46836 : if (actual <= len)
1169 : : {
1170 : 23549 : unuse (len - actual);
1171 : 23549 : break;
1172 : : }
1173 : 23287 : unuse (len);
1174 : 23287 : len = actual;
1175 : 23287 : }
1176 : 23549 : }
1177 : :
1178 : : void
1179 : 4604 : bytes_out::print_time (const char *kind, const tm *time, const char *tz)
1180 : : {
1181 : 4604 : printf ("%stime: %4u/%02u/%02u %02u:%02u:%02u %s",
1182 : 4604 : kind, time->tm_year + 1900, time->tm_mon + 1, time->tm_mday,
1183 : 4604 : time->tm_hour, time->tm_min, time->tm_sec, tz);
1184 : 4604 : }
1185 : :
1186 : : /* Encapsulated Lazy Records Of Named Declarations.
1187 : : Header: Stunningly Elf32_Ehdr-like
1188 : : Sections: Sectional data
1189 : : [1-N) : User data sections
1190 : : N .strtab : strings, stunningly ELF STRTAB-like
1191 : : Index: Section table, stunningly ELF32_Shdr-like. */
1192 : :
1193 : : class elf {
1194 : : protected:
1195 : : /* Constants used within the format. */
1196 : : enum private_constants {
1197 : : /* File kind. */
1198 : : ET_NONE = 0,
1199 : : EM_NONE = 0,
1200 : : OSABI_NONE = 0,
1201 : :
1202 : : /* File format. */
1203 : : EV_CURRENT = 1,
1204 : : CLASS32 = 1,
1205 : : DATA2LSB = 1,
1206 : : DATA2MSB = 2,
1207 : :
1208 : : /* Section numbering. */
1209 : : SHN_UNDEF = 0,
1210 : : SHN_LORESERVE = 0xff00,
1211 : : SHN_XINDEX = 0xffff,
1212 : :
1213 : : /* Section types. */
1214 : : SHT_NONE = 0, /* No contents. */
1215 : : SHT_PROGBITS = 1, /* Random bytes. */
1216 : : SHT_STRTAB = 3, /* A string table. */
1217 : :
1218 : : /* Section flags. */
1219 : : SHF_NONE = 0x00, /* Nothing. */
1220 : : SHF_STRINGS = 0x20, /* NUL-Terminated strings. */
1221 : :
1222 : : /* I really hope we do not get CMI files larger than 4GB. */
1223 : : MY_CLASS = CLASS32,
1224 : : /* It is host endianness that is relevant. */
1225 : : MY_ENDIAN = DATA2LSB
1226 : : #ifdef WORDS_BIGENDIAN
1227 : : ^ DATA2LSB ^ DATA2MSB
1228 : : #endif
1229 : : };
1230 : :
1231 : : public:
1232 : : /* Constants visible to users. */
1233 : : enum public_constants {
1234 : : /* Special error codes. Breaking layering a bit. */
1235 : : E_BAD_DATA = -1, /* Random unexpected data errors. */
1236 : : E_BAD_LAZY = -2, /* Badly ordered laziness. */
1237 : : E_BAD_IMPORT = -3 /* A nested import failed. */
1238 : : };
1239 : :
1240 : : protected:
1241 : : /* File identification. On-disk representation. */
1242 : : struct ident {
1243 : : uint8_t magic[4]; /* 0x7f, 'E', 'L', 'F' */
1244 : : uint8_t klass; /* 4:CLASS32 */
1245 : : uint8_t data; /* 5:DATA2[LM]SB */
1246 : : uint8_t version; /* 6:EV_CURRENT */
1247 : : uint8_t osabi; /* 7:OSABI_NONE */
1248 : : uint8_t abiver; /* 8: 0 */
1249 : : uint8_t pad[7]; /* 9-15 */
1250 : : };
1251 : : /* File header. On-disk representation. */
1252 : : struct header {
1253 : : struct ident ident;
1254 : : uint16_t type; /* ET_NONE */
1255 : : uint16_t machine; /* EM_NONE */
1256 : : uint32_t version; /* EV_CURRENT */
1257 : : uint32_t entry; /* 0 */
1258 : : uint32_t phoff; /* 0 */
1259 : : uint32_t shoff; /* Section Header Offset in file */
1260 : : uint32_t flags;
1261 : : uint16_t ehsize; /* ELROND Header SIZE -- sizeof (header) */
1262 : : uint16_t phentsize; /* 0 */
1263 : : uint16_t phnum; /* 0 */
1264 : : uint16_t shentsize; /* Section Header SIZE -- sizeof (section) */
1265 : : uint16_t shnum; /* Section Header NUM */
1266 : : uint16_t shstrndx; /* Section Header STRing iNDeX */
1267 : : };
1268 : : /* File section. On-disk representation. */
1269 : : struct section {
1270 : : uint32_t name; /* String table offset. */
1271 : : uint32_t type; /* SHT_* */
1272 : : uint32_t flags; /* SHF_* */
1273 : : uint32_t addr; /* 0 */
1274 : : uint32_t offset; /* OFFSET in file */
1275 : : uint32_t size; /* SIZE of section */
1276 : : uint32_t link; /* 0 */
1277 : : uint32_t info; /* 0 */
1278 : : uint32_t addralign; /* 0 */
1279 : : uint32_t entsize; /* ENTry SIZE, usually 0 */
1280 : : };
1281 : :
1282 : : protected:
1283 : : data hdr; /* The header. */
1284 : : data sectab; /* The section table. */
1285 : : data strtab; /* String table. */
1286 : : int fd; /* File descriptor we're reading or writing. */
1287 : : int err; /* Sticky error code. */
1288 : :
1289 : : public:
1290 : : /* Construct from STREAM. E is errno if STREAM NULL. */
1291 : 4976 : elf (int fd, int e)
1292 : 9952 : :hdr (), sectab (), strtab (), fd (fd), err (fd >= 0 ? 0 : e)
1293 : : {}
1294 : 4900 : ~elf ()
1295 : : {
1296 : 4900 : gcc_checking_assert (fd < 0 && !hdr.buffer
1297 : : && !sectab.buffer && !strtab.buffer);
1298 : 4900 : }
1299 : :
1300 : : public:
1301 : : /* Return the error, if we have an error. */
1302 : 354664 : int get_error () const
1303 : : {
1304 : 354664 : return err;
1305 : : }
1306 : : /* Set the error, unless it's already been set. */
1307 : 32 : void set_error (int e = E_BAD_DATA)
1308 : : {
1309 : 32 : if (!err)
1310 : 19 : err = e;
1311 : 0 : }
1312 : : /* Get an error string. */
1313 : : const char *get_error (const char *) const;
1314 : :
1315 : : public:
1316 : : /* Begin reading/writing file. Return false on error. */
1317 : 4849 : bool begin () const
1318 : : {
1319 : 4849 : return !get_error ();
1320 : : }
1321 : : /* Finish reading/writing file. Return false on error. */
1322 : : bool end ();
1323 : : };
1324 : :
1325 : : /* Return error string. */
1326 : :
1327 : : const char *
1328 : 37 : elf::get_error (const char *name) const
1329 : : {
1330 : 37 : if (!name)
1331 : : return "Unknown CMI mapping";
1332 : :
1333 : 37 : switch (err)
1334 : : {
1335 : 0 : case 0:
1336 : 0 : gcc_unreachable ();
1337 : : case E_BAD_DATA:
1338 : : return "Bad file data";
1339 : 6 : case E_BAD_IMPORT:
1340 : 6 : return "Bad import dependency";
1341 : 0 : case E_BAD_LAZY:
1342 : 0 : return "Bad lazy ordering";
1343 : 18 : default:
1344 : 18 : return xstrerror (err);
1345 : : }
1346 : : }
1347 : :
1348 : : /* Finish file, return true if there's an error. */
1349 : :
1350 : : bool
1351 : 6953 : elf::end ()
1352 : : {
1353 : : /* Close the stream and free the section table. */
1354 : 6953 : if (fd >= 0 && close (fd))
1355 : 0 : set_error (errno);
1356 : 6953 : fd = -1;
1357 : :
1358 : 6953 : return !get_error ();
1359 : : }
1360 : :
1361 : : /* ELROND reader. */
1362 : :
1363 : : class elf_in : public elf {
1364 : : typedef elf parent;
1365 : :
1366 : : private:
1367 : : /* For freezing & defrosting. */
1368 : : #if !defined (HOST_LACKS_INODE_NUMBERS)
1369 : : dev_t device;
1370 : : ino_t inode;
1371 : : #endif
1372 : :
1373 : : public:
1374 : 2540 : elf_in (int fd, int e)
1375 : 5080 : :parent (fd, e)
1376 : : {
1377 : : }
1378 : 2464 : ~elf_in ()
1379 : : {
1380 : 2464 : }
1381 : :
1382 : : public:
1383 : 166192 : bool is_frozen () const
1384 : : {
1385 : 18 : return fd < 0 && hdr.pos;
1386 : : }
1387 : 18 : bool is_freezable () const
1388 : : {
1389 : 9 : return fd >= 0 && hdr.pos;
1390 : : }
1391 : : void freeze ();
1392 : : bool defrost (const char *);
1393 : :
1394 : : /* If BYTES is in the mmapped area, allocate a new buffer for it. */
1395 : 0 : void preserve (bytes_in &bytes ATTRIBUTE_UNUSED)
1396 : : {
1397 : : #if MAPPED_READING
1398 : 0 : if (hdr.buffer && bytes.buffer >= hdr.buffer
1399 : 0 : && bytes.buffer < hdr.buffer + hdr.pos)
1400 : : {
1401 : 0 : char *buf = bytes.buffer;
1402 : 0 : bytes.buffer = data::simple_memory.grow (NULL, bytes.size);
1403 : 0 : memcpy (bytes.buffer, buf, bytes.size);
1404 : : }
1405 : : #endif
1406 : 0 : }
1407 : : /* If BYTES is not in SELF's mmapped area, free it. SELF might be
1408 : : NULL. */
1409 : 1003 : static void release (elf_in *self ATTRIBUTE_UNUSED, bytes_in &bytes)
1410 : : {
1411 : : #if MAPPED_READING
1412 : 1003 : if (!(self && self->hdr.buffer && bytes.buffer >= self->hdr.buffer
1413 : 1003 : && bytes.buffer < self->hdr.buffer + self->hdr.pos))
1414 : : #endif
1415 : 0 : data::simple_memory.shrink (bytes.buffer);
1416 : 1003 : bytes.buffer = NULL;
1417 : 1003 : bytes.size = 0;
1418 : 1003 : }
1419 : :
1420 : : public:
1421 : 184087 : static void grow (data &data, unsigned needed)
1422 : : {
1423 : 184087 : gcc_checking_assert (!data.buffer);
1424 : : #if !MAPPED_READING
1425 : : data.buffer = XNEWVEC (char, needed);
1426 : : #endif
1427 : 184087 : data.size = needed;
1428 : 184087 : }
1429 : 189532 : static void shrink (data &data)
1430 : : {
1431 : : #if !MAPPED_READING
1432 : : XDELETEVEC (data.buffer);
1433 : : #endif
1434 : 189532 : data.buffer = NULL;
1435 : 189532 : data.size = 0;
1436 : 0 : }
1437 : :
1438 : : public:
1439 : 181565 : const section *get_section (unsigned s) const
1440 : : {
1441 : 181565 : if (s * sizeof (section) < sectab.size)
1442 : 181565 : return reinterpret_cast<const section *>
1443 : 181565 : (§ab.buffer[s * sizeof (section)]);
1444 : : else
1445 : : return NULL;
1446 : : }
1447 : : unsigned get_section_limit () const
1448 : : {
1449 : : return sectab.size / sizeof (section);
1450 : : }
1451 : :
1452 : : protected:
1453 : : const char *read (data *, unsigned, unsigned);
1454 : :
1455 : : public:
1456 : : /* Read section by number. */
1457 : 181565 : bool read (data *d, const section *s)
1458 : : {
1459 : 181565 : return s && read (d, s->offset, s->size);
1460 : : }
1461 : :
1462 : : /* Find section by name. */
1463 : : unsigned find (const char *name);
1464 : : /* Find section by index. */
1465 : : const section *find (unsigned snum, unsigned type = SHT_PROGBITS);
1466 : :
1467 : : public:
1468 : : /* Release the string table, when we're done with it. */
1469 : 7014 : void release ()
1470 : : {
1471 : 7014 : shrink (strtab);
1472 : 39 : }
1473 : :
1474 : : public:
1475 : : bool begin (location_t);
1476 : 4517 : bool end ()
1477 : : {
1478 : 4517 : release ();
1479 : : #if MAPPED_READING
1480 : 4517 : if (hdr.buffer)
1481 : 2464 : munmap (hdr.buffer, hdr.pos);
1482 : 4517 : hdr.buffer = NULL;
1483 : : #endif
1484 : 4517 : shrink (sectab);
1485 : :
1486 : 4517 : return parent::end ();
1487 : : }
1488 : :
1489 : : public:
1490 : : /* Return string name at OFFSET. Checks OFFSET range. Always
1491 : : returns non-NULL. We know offset 0 is an empty string. */
1492 : 242793 : const char *name (unsigned offset)
1493 : : {
1494 : 485586 : return &strtab.buffer[offset < strtab.size ? offset : 0];
1495 : : }
1496 : : };
1497 : :
1498 : : /* ELROND writer. */
1499 : :
1500 : : class elf_out : public elf, public data::allocator {
1501 : : typedef elf parent;
1502 : : /* Desired section alignment on disk. */
1503 : : static const int SECTION_ALIGN = 16;
1504 : :
1505 : : private:
1506 : : ptr_int_hash_map identtab; /* Map of IDENTIFIERS to strtab offsets. */
1507 : : unsigned pos; /* Write position in file. */
1508 : : #if MAPPED_WRITING
1509 : : unsigned offset; /* Offset of the mapping. */
1510 : : unsigned extent; /* Length of mapping. */
1511 : : unsigned page_size; /* System page size. */
1512 : : #endif
1513 : :
1514 : : public:
1515 : 2436 : elf_out (int fd, int e)
1516 : 4769 : :parent (fd, e), identtab (500), pos (0)
1517 : : {
1518 : : #if MAPPED_WRITING
1519 : 2436 : offset = extent = 0;
1520 : 2436 : page_size = sysconf (_SC_PAGE_SIZE);
1521 : 2436 : if (page_size < SECTION_ALIGN)
1522 : : /* Something really strange. */
1523 : 0 : set_error (EINVAL);
1524 : : #endif
1525 : 2436 : }
1526 : 2436 : ~elf_out ()
1527 : 2436 : {
1528 : 2436 : data::simple_memory.shrink (hdr);
1529 : 2436 : data::simple_memory.shrink (sectab);
1530 : 2436 : data::simple_memory.shrink (strtab);
1531 : 2436 : }
1532 : :
1533 : : #if MAPPED_WRITING
1534 : : private:
1535 : : void create_mapping (unsigned ext, bool extending = true);
1536 : : void remove_mapping ();
1537 : : #endif
1538 : :
1539 : : protected:
1540 : : using allocator::grow;
1541 : : char *grow (char *, unsigned needed) final override;
1542 : : #if MAPPED_WRITING
1543 : : using allocator::shrink;
1544 : : void shrink (char *) final override;
1545 : : #endif
1546 : :
1547 : : public:
1548 : 4852 : unsigned get_section_limit () const
1549 : : {
1550 : 4852 : return sectab.pos / sizeof (section);
1551 : : }
1552 : :
1553 : : protected:
1554 : : unsigned add (unsigned type, unsigned name = 0,
1555 : : unsigned off = 0, unsigned size = 0, unsigned flags = SHF_NONE);
1556 : : unsigned write (const data &);
1557 : : #if MAPPED_WRITING
1558 : : unsigned write (const bytes_out &);
1559 : : #endif
1560 : :
1561 : : public:
1562 : : /* IDENTIFIER to strtab offset. */
1563 : : unsigned name (tree ident);
1564 : : /* String literal to strtab offset. */
1565 : : unsigned name (const char *n);
1566 : : /* Qualified name of DECL to strtab offset. */
1567 : : unsigned qualified_name (tree decl, bool is_defn);
1568 : :
1569 : : private:
1570 : : unsigned strtab_write (const char *s, unsigned l);
1571 : : void strtab_write (tree decl, int);
1572 : :
1573 : : public:
1574 : : /* Add a section with contents or strings. */
1575 : : unsigned add (const bytes_out &, bool string_p, unsigned name);
1576 : :
1577 : : public:
1578 : : /* Begin and end writing. */
1579 : : bool begin ();
1580 : : bool end ();
1581 : : };
1582 : :
1583 : : /* Begin reading section NAME (of type PROGBITS) from SOURCE.
1584 : : Data always checked for CRC. */
1585 : :
1586 : : bool
1587 : 15349 : bytes_in::begin (location_t loc, elf_in *source, const char *name)
1588 : : {
1589 : 15349 : unsigned snum = source->find (name);
1590 : :
1591 : 15349 : return begin (loc, source, snum, name);
1592 : : }
1593 : :
1594 : : /* Begin reading section numbered SNUM with NAME (may be NULL). */
1595 : :
1596 : : bool
1597 : 179043 : bytes_in::begin (location_t loc, elf_in *source, unsigned snum, const char *name)
1598 : : {
1599 : 179043 : if (!source->read (this, source->find (snum))
1600 : 179043 : || !size || !check_crc ())
1601 : : {
1602 : 0 : source->set_error (elf::E_BAD_DATA);
1603 : 0 : source->shrink (*this);
1604 : 0 : if (name)
1605 : 0 : error_at (loc, "section %qs is missing or corrupted", name);
1606 : : else
1607 : 0 : error_at (loc, "section #%u is missing or corrupted", snum);
1608 : 0 : return false;
1609 : : }
1610 : 179043 : pos = 4;
1611 : 179043 : return true;
1612 : : }
1613 : :
1614 : : /* Finish reading a section. */
1615 : :
1616 : : bool
1617 : 178001 : bytes_in::end (elf_in *src)
1618 : : {
1619 : 178001 : if (more_p ())
1620 : 13 : set_overrun ();
1621 : 178001 : if (overrun)
1622 : 13 : src->set_error ();
1623 : :
1624 : 178001 : src->shrink (*this);
1625 : :
1626 : 178001 : return !overrun;
1627 : : }
1628 : :
1629 : : /* Begin writing buffer. */
1630 : :
1631 : : void
1632 : 238848 : bytes_out::begin (bool need_crc)
1633 : : {
1634 : 0 : if (need_crc)
1635 : 0 : pos = 4;
1636 : 0 : memory->grow (*this, 0, false);
1637 : 223385 : }
1638 : :
1639 : : /* Finish writing buffer. Stream out to SINK as named section NAME.
1640 : : Return section number or 0 on failure. If CRC_PTR is true, crc
1641 : : the data. Otherwise it is a string section. */
1642 : :
1643 : : unsigned
1644 : 238848 : bytes_out::end (elf_out *sink, unsigned name, unsigned *crc_ptr)
1645 : : {
1646 : 238848 : lengths[3] += pos;
1647 : 238848 : spans[3]++;
1648 : :
1649 : 238848 : set_crc (crc_ptr);
1650 : 238848 : unsigned sec_num = sink->add (*this, !crc_ptr, name);
1651 : 238848 : memory->shrink (*this);
1652 : :
1653 : 238848 : return sec_num;
1654 : : }
1655 : :
1656 : : /* Close and open the file, without destroying it. */
1657 : :
1658 : : void
1659 : 9 : elf_in::freeze ()
1660 : : {
1661 : 9 : gcc_checking_assert (!is_frozen ());
1662 : : #if MAPPED_READING
1663 : 9 : if (munmap (hdr.buffer, hdr.pos) < 0)
1664 : 0 : set_error (errno);
1665 : : #endif
1666 : 9 : if (close (fd) < 0)
1667 : 0 : set_error (errno);
1668 : 9 : fd = -1;
1669 : 9 : }
1670 : :
1671 : : bool
1672 : 9 : elf_in::defrost (const char *name)
1673 : : {
1674 : 9 : gcc_checking_assert (is_frozen ());
1675 : 9 : struct stat stat;
1676 : :
1677 : 9 : fd = open (name, O_RDONLY | O_CLOEXEC | O_BINARY);
1678 : 9 : if (fd < 0 || fstat (fd, &stat) < 0)
1679 : 0 : set_error (errno);
1680 : : else
1681 : : {
1682 : 9 : bool ok = hdr.pos == unsigned (stat.st_size);
1683 : : #ifndef HOST_LACKS_INODE_NUMBERS
1684 : 9 : if (device != stat.st_dev
1685 : 9 : || inode != stat.st_ino)
1686 : : ok = false;
1687 : : #endif
1688 : 9 : if (!ok)
1689 : 0 : set_error (EMFILE);
1690 : : #if MAPPED_READING
1691 : 0 : if (ok)
1692 : : {
1693 : 9 : char *mapping = reinterpret_cast<char *>
1694 : 9 : (mmap (NULL, hdr.pos, PROT_READ, MAP_SHARED, fd, 0));
1695 : 9 : if (mapping == MAP_FAILED)
1696 : 0 : fail:
1697 : 0 : set_error (errno);
1698 : : else
1699 : : {
1700 : 9 : if (madvise (mapping, hdr.pos, MADV_RANDOM))
1701 : 0 : goto fail;
1702 : :
1703 : : /* These buffers are never NULL in this case. */
1704 : 9 : strtab.buffer = mapping + strtab.pos;
1705 : 9 : sectab.buffer = mapping + sectab.pos;
1706 : 9 : hdr.buffer = mapping;
1707 : : }
1708 : : }
1709 : : #endif
1710 : : }
1711 : :
1712 : 9 : return !get_error ();
1713 : : }
1714 : :
1715 : : /* Read at current position into BUFFER. Return true on success. */
1716 : :
1717 : : const char *
1718 : 184087 : elf_in::read (data *data, unsigned pos, unsigned length)
1719 : : {
1720 : : #if MAPPED_READING
1721 : 184087 : if (pos + length > hdr.pos)
1722 : : {
1723 : 0 : set_error (EINVAL);
1724 : 0 : return NULL;
1725 : : }
1726 : : #else
1727 : : if (pos != ~0u && lseek (fd, pos, SEEK_SET) < 0)
1728 : : {
1729 : : set_error (errno);
1730 : : return NULL;
1731 : : }
1732 : : #endif
1733 : 184087 : grow (*data, length);
1734 : : #if MAPPED_READING
1735 : 184087 : data->buffer = hdr.buffer + pos;
1736 : : #else
1737 : : if (::read (fd, data->buffer, data->size) != ssize_t (length))
1738 : : {
1739 : : set_error (errno);
1740 : : shrink (*data);
1741 : : return NULL;
1742 : : }
1743 : : #endif
1744 : :
1745 : 184087 : return data->buffer;
1746 : : }
1747 : :
1748 : : /* Read section SNUM of TYPE. Return section pointer or NULL on error. */
1749 : :
1750 : : const elf::section *
1751 : 181565 : elf_in::find (unsigned snum, unsigned type)
1752 : : {
1753 : 181565 : const section *sec = get_section (snum);
1754 : 181565 : if (!snum || !sec || sec->type != type)
1755 : 0 : return NULL;
1756 : : return sec;
1757 : : }
1758 : :
1759 : : /* Find a section NAME and TYPE. Return section number, or zero on
1760 : : failure. */
1761 : :
1762 : : unsigned
1763 : 15391 : elf_in::find (const char *sname)
1764 : : {
1765 : 90188 : for (unsigned pos = sectab.size; pos -= sizeof (section); )
1766 : : {
1767 : 90188 : const section *sec
1768 : 90188 : = reinterpret_cast<const section *> (§ab.buffer[pos]);
1769 : :
1770 : 180376 : if (0 == strcmp (sname, name (sec->name)))
1771 : 15391 : return pos / sizeof (section);
1772 : : }
1773 : :
1774 : : return 0;
1775 : : }
1776 : :
1777 : : /* Begin reading file. Verify header. Pull in section and string
1778 : : tables. Return true on success. */
1779 : :
1780 : : bool
1781 : 2522 : elf_in::begin (location_t loc)
1782 : : {
1783 : 2522 : if (!parent::begin ())
1784 : : return false;
1785 : :
1786 : 2522 : struct stat stat;
1787 : 2522 : unsigned size = 0;
1788 : 2522 : if (!fstat (fd, &stat))
1789 : : {
1790 : : #if !defined (HOST_LACKS_INODE_NUMBERS)
1791 : 2522 : device = stat.st_dev;
1792 : 2522 : inode = stat.st_ino;
1793 : : #endif
1794 : : /* Never generate files > 4GB, check we've not been given one. */
1795 : 2522 : if (stat.st_size == unsigned (stat.st_size))
1796 : 2522 : size = unsigned (stat.st_size);
1797 : : }
1798 : :
1799 : : #if MAPPED_READING
1800 : : /* MAP_SHARED so that the file is backing store. If someone else
1801 : : concurrently writes it, they're wrong. */
1802 : 2522 : void *mapping = mmap (NULL, size, PROT_READ, MAP_SHARED, fd, 0);
1803 : 2522 : if (mapping == MAP_FAILED)
1804 : : {
1805 : 0 : fail:
1806 : 0 : set_error (errno);
1807 : 0 : return false;
1808 : : }
1809 : : /* We'll be hopping over this randomly. Some systems declare the
1810 : : first parm as char *, and other declare it as void *. */
1811 : 2522 : if (madvise (reinterpret_cast <char *> (mapping), size, MADV_RANDOM))
1812 : 0 : goto fail;
1813 : :
1814 : 2522 : hdr.buffer = (char *)mapping;
1815 : : #else
1816 : : read (&hdr, 0, sizeof (header));
1817 : : #endif
1818 : 2522 : hdr.pos = size; /* Record size of the file. */
1819 : :
1820 : 2522 : const header *h = reinterpret_cast<const header *> (hdr.buffer);
1821 : 2522 : if (!h)
1822 : : return false;
1823 : :
1824 : 2522 : if (h->ident.magic[0] != 0x7f
1825 : : || h->ident.magic[1] != 'E'
1826 : : || h->ident.magic[2] != 'L'
1827 : 2522 : || h->ident.magic[3] != 'F')
1828 : : {
1829 : 0 : error_at (loc, "not Encapsulated Lazy Records of Named Declarations");
1830 : 0 : failed:
1831 : 0 : shrink (hdr);
1832 : 0 : return false;
1833 : : }
1834 : :
1835 : : /* We expect a particular format -- the ELF is not intended to be
1836 : : distributable. */
1837 : 2522 : if (h->ident.klass != MY_CLASS
1838 : : || h->ident.data != MY_ENDIAN
1839 : 2522 : || h->ident.version != EV_CURRENT
1840 : : || h->type != ET_NONE
1841 : 2522 : || h->machine != EM_NONE
1842 : 2522 : || h->ident.osabi != OSABI_NONE)
1843 : : {
1844 : 0 : error_at (loc, "unexpected encapsulation format or type");
1845 : 0 : goto failed;
1846 : : }
1847 : :
1848 : 2522 : int e = -1;
1849 : 2522 : if (!h->shoff || h->shentsize != sizeof (section))
1850 : : {
1851 : 0 : malformed:
1852 : 0 : set_error (e);
1853 : 0 : error_at (loc, "encapsulation is malformed");
1854 : 0 : goto failed;
1855 : : }
1856 : :
1857 : 2522 : unsigned strndx = h->shstrndx;
1858 : 2522 : unsigned shnum = h->shnum;
1859 : 2522 : if (shnum == SHN_XINDEX)
1860 : : {
1861 : 0 : if (!read (§ab, h->shoff, sizeof (section)))
1862 : : {
1863 : 0 : section_table_fail:
1864 : 0 : e = errno;
1865 : 0 : goto malformed;
1866 : : }
1867 : 0 : shnum = get_section (0)->size;
1868 : : /* Freeing does mean we'll re-read it in the case we're not
1869 : : mapping, but this is going to be rare. */
1870 : 0 : shrink (sectab);
1871 : : }
1872 : :
1873 : 2522 : if (!shnum)
1874 : 0 : goto malformed;
1875 : :
1876 : 2522 : if (!read (§ab, h->shoff, shnum * sizeof (section)))
1877 : 0 : goto section_table_fail;
1878 : :
1879 : 2522 : if (strndx == SHN_XINDEX)
1880 : 0 : strndx = get_section (0)->link;
1881 : :
1882 : 2522 : if (!read (&strtab, find (strndx, SHT_STRTAB)))
1883 : 0 : goto malformed;
1884 : :
1885 : : /* The string table should be at least one byte, with NUL chars
1886 : : at either end. */
1887 : 2522 : if (!(strtab.size && !strtab.buffer[0]
1888 : 2522 : && !strtab.buffer[strtab.size - 1]))
1889 : 0 : goto malformed;
1890 : :
1891 : : #if MAPPED_READING
1892 : : /* Record the offsets of the section and string tables. */
1893 : 2522 : sectab.pos = h->shoff;
1894 : 2522 : strtab.pos = shnum * sizeof (section);
1895 : : #else
1896 : : shrink (hdr);
1897 : : #endif
1898 : :
1899 : 2522 : return true;
1900 : : }
1901 : :
1902 : : /* Create a new mapping. */
1903 : :
1904 : : #if MAPPED_WRITING
1905 : : void
1906 : 3026 : elf_out::create_mapping (unsigned ext, bool extending)
1907 : : {
1908 : : /* A wrapper around posix_fallocate, falling back to ftruncate
1909 : : if the underlying filesystem does not support the operation. */
1910 : 5933 : auto allocate = [](int fd, off_t offset, off_t length)
1911 : : {
1912 : : #ifdef HAVE_POSIX_FALLOCATE
1913 : 2907 : int result = posix_fallocate (fd, offset, length);
1914 : 2907 : if (result != EINVAL)
1915 : 2907 : return result == 0;
1916 : : /* Not supported by the underlying filesystem, fallback to ftruncate. */
1917 : : #endif
1918 : 0 : return ftruncate (fd, offset + length) == 0;
1919 : : };
1920 : :
1921 : 3026 : void *mapping = MAP_FAILED;
1922 : 3026 : if (extending && ext < 1024 * 1024)
1923 : : {
1924 : 2797 : if (allocate (fd, offset, ext * 2))
1925 : 2797 : mapping = mmap (NULL, ext * 2, PROT_READ | PROT_WRITE,
1926 : 2797 : MAP_SHARED, fd, offset);
1927 : 2797 : if (mapping != MAP_FAILED)
1928 : : ext *= 2;
1929 : : }
1930 : : if (mapping == MAP_FAILED)
1931 : : {
1932 : 229 : if (!extending || allocate (fd, offset, ext))
1933 : 229 : mapping = mmap (NULL, ext, PROT_READ | PROT_WRITE,
1934 : 229 : MAP_SHARED, fd, offset);
1935 : 229 : if (mapping == MAP_FAILED)
1936 : : {
1937 : 0 : set_error (errno);
1938 : : mapping = NULL;
1939 : : ext = 0;
1940 : : }
1941 : : }
1942 : 3026 : hdr.buffer = (char *)mapping;
1943 : 3026 : extent = ext;
1944 : 3026 : }
1945 : : #endif
1946 : :
1947 : : /* Flush out the current mapping. */
1948 : :
1949 : : #if MAPPED_WRITING
1950 : : void
1951 : 3032 : elf_out::remove_mapping ()
1952 : : {
1953 : 3032 : if (hdr.buffer)
1954 : : {
1955 : : /* MS_ASYNC dtrt with the removed mapping, including a
1956 : : subsequent overlapping remap. */
1957 : 3026 : if (msync (hdr.buffer, extent, MS_ASYNC)
1958 : 3026 : || munmap (hdr.buffer, extent))
1959 : : /* We're somewhat screwed at this point. */
1960 : 0 : set_error (errno);
1961 : : }
1962 : :
1963 : 3032 : hdr.buffer = NULL;
1964 : 3032 : }
1965 : : #endif
1966 : :
1967 : : /* Grow a mapping of PTR to be NEEDED bytes long. This gets
1968 : : interesting if the new size grows the EXTENT. */
1969 : :
1970 : : char *
1971 : 565314 : elf_out::grow (char *data, unsigned needed)
1972 : : {
1973 : 565314 : if (!data)
1974 : : {
1975 : : /* First allocation, check we're aligned. */
1976 : 243514 : gcc_checking_assert (!(pos & (SECTION_ALIGN - 1)));
1977 : : #if MAPPED_WRITING
1978 : 243514 : data = hdr.buffer + (pos - offset);
1979 : : #endif
1980 : : }
1981 : :
1982 : : #if MAPPED_WRITING
1983 : 565314 : unsigned off = data - hdr.buffer;
1984 : 565314 : if (off + needed > extent)
1985 : : {
1986 : : /* We need to grow the mapping. */
1987 : 580 : unsigned lwm = off & ~(page_size - 1);
1988 : 580 : unsigned hwm = (off + needed + page_size - 1) & ~(page_size - 1);
1989 : :
1990 : 580 : gcc_checking_assert (hwm > extent);
1991 : :
1992 : 580 : remove_mapping ();
1993 : :
1994 : 580 : offset += lwm;
1995 : 580 : create_mapping (extent < hwm - lwm ? hwm - lwm : extent);
1996 : :
1997 : 580 : data = hdr.buffer + (off - lwm);
1998 : : }
1999 : : #else
2000 : : data = allocator::grow (data, needed);
2001 : : #endif
2002 : :
2003 : 565314 : return data;
2004 : : }
2005 : :
2006 : : #if MAPPED_WRITING
2007 : : /* Shrinking is a NOP. */
2008 : : void
2009 : 243514 : elf_out::shrink (char *)
2010 : : {
2011 : 243514 : }
2012 : : #endif
2013 : :
2014 : : /* Write S of length L to the strtab buffer. L must include the ending
2015 : : NUL, if that's what you want. */
2016 : :
2017 : : unsigned
2018 : 1062961 : elf_out::strtab_write (const char *s, unsigned l)
2019 : : {
2020 : 1062961 : if (strtab.pos + l > strtab.size)
2021 : 924 : data::simple_memory.grow (strtab, strtab.pos + l, false);
2022 : 1062961 : memcpy (strtab.buffer + strtab.pos, s, l);
2023 : 1062961 : unsigned res = strtab.pos;
2024 : 1062961 : strtab.pos += l;
2025 : 1062961 : return res;
2026 : : }
2027 : :
2028 : : /* Write qualified name of decl. INNER >0 if this is a definition, <0
2029 : : if this is a qualifier of an outer name. */
2030 : :
2031 : : void
2032 : 419017 : elf_out::strtab_write (tree decl, int inner)
2033 : : {
2034 : 419017 : tree ctx = CP_DECL_CONTEXT (decl);
2035 : 419017 : if (TYPE_P (ctx))
2036 : 4442 : ctx = TYPE_NAME (ctx);
2037 : 419017 : if (ctx != global_namespace)
2038 : 205698 : strtab_write (ctx, -1);
2039 : :
2040 : 419017 : tree name = DECL_NAME (decl);
2041 : 419017 : if (!name)
2042 : 301 : name = DECL_ASSEMBLER_NAME_RAW (decl);
2043 : 419017 : strtab_write (IDENTIFIER_POINTER (name), IDENTIFIER_LENGTH (name));
2044 : :
2045 : 419017 : if (inner)
2046 : 295236 : strtab_write (&"::{}"[inner+1], 2);
2047 : 419017 : }
2048 : :
2049 : : /* Map IDENTIFIER IDENT to strtab offset. Inserts into strtab if not
2050 : : already there. */
2051 : :
2052 : : unsigned
2053 : 120294 : elf_out::name (tree ident)
2054 : : {
2055 : 120294 : unsigned res = 0;
2056 : 120294 : if (ident)
2057 : : {
2058 : 120256 : bool existed;
2059 : 120256 : int *slot = &identtab.get_or_insert (ident, &existed);
2060 : 120256 : if (!existed)
2061 : 216636 : *slot = strtab_write (IDENTIFIER_POINTER (ident),
2062 : 108318 : IDENTIFIER_LENGTH (ident) + 1);
2063 : 120256 : res = *slot;
2064 : : }
2065 : 120294 : return res;
2066 : : }
2067 : :
2068 : : /* Map LITERAL to strtab offset. Does not detect duplicates and
2069 : : expects LITERAL to remain live until strtab is written out. */
2070 : :
2071 : : unsigned
2072 : 27071 : elf_out::name (const char *literal)
2073 : : {
2074 : 27071 : return strtab_write (literal, strlen (literal) + 1);
2075 : : }
2076 : :
2077 : : /* Map a DECL's qualified name to strtab offset. Does not detect
2078 : : duplicates. */
2079 : :
2080 : : unsigned
2081 : 213319 : elf_out::qualified_name (tree decl, bool is_defn)
2082 : : {
2083 : 213319 : gcc_checking_assert (DECL_P (decl) && decl != global_namespace);
2084 : 213319 : unsigned result = strtab.pos;
2085 : :
2086 : 213319 : strtab_write (decl, is_defn);
2087 : 213319 : strtab_write ("", 1);
2088 : :
2089 : 213319 : return result;
2090 : : }
2091 : :
2092 : : /* Add section to file. Return section number. TYPE & NAME identify
2093 : : the section. OFF and SIZE identify the file location of its
2094 : : data. FLAGS contains additional info. */
2095 : :
2096 : : unsigned
2097 : 243508 : elf_out::add (unsigned type, unsigned name, unsigned off, unsigned size,
2098 : : unsigned flags)
2099 : : {
2100 : 243508 : gcc_checking_assert (!(off & (SECTION_ALIGN - 1)));
2101 : 243508 : if (sectab.pos + sizeof (section) > sectab.size)
2102 : 3530 : data::simple_memory.grow (sectab, sectab.pos + sizeof (section), false);
2103 : 243508 : section *sec = reinterpret_cast<section *> (sectab.buffer + sectab.pos);
2104 : 243508 : memset (sec, 0, sizeof (section));
2105 : 243508 : sec->type = type;
2106 : 243508 : sec->flags = flags;
2107 : 243508 : sec->name = name;
2108 : 243508 : sec->offset = off;
2109 : 243508 : sec->size = size;
2110 : 243508 : if (flags & SHF_STRINGS)
2111 : 4635 : sec->entsize = 1;
2112 : :
2113 : 243508 : unsigned res = sectab.pos;
2114 : 243508 : sectab.pos += sizeof (section);
2115 : 243508 : return res / sizeof (section);
2116 : : }
2117 : :
2118 : : /* Pad to the next alignment boundary, then write BUFFER to disk.
2119 : : Return the position of the start of the write, or zero on failure. */
2120 : :
2121 : : unsigned
2122 : 9326 : elf_out::write (const data &buffer)
2123 : : {
2124 : : #if MAPPED_WRITING
2125 : : /* HDR is always mapped. */
2126 : 9326 : if (&buffer != &hdr)
2127 : : {
2128 : 4666 : bytes_out out (this);
2129 : 4666 : grow (out, buffer.pos, true);
2130 : 4666 : if (out.buffer)
2131 : 4666 : memcpy (out.buffer, buffer.buffer, buffer.pos);
2132 : 4666 : shrink (out);
2133 : 4666 : }
2134 : : else
2135 : : /* We should have been aligned during the first allocation. */
2136 : 4660 : gcc_checking_assert (!(pos & (SECTION_ALIGN - 1)));
2137 : : #else
2138 : : if (::write (fd, buffer.buffer, buffer.pos) != ssize_t (buffer.pos))
2139 : : {
2140 : : set_error (errno);
2141 : : return 0;
2142 : : }
2143 : : #endif
2144 : 9326 : unsigned res = pos;
2145 : 9326 : pos += buffer.pos;
2146 : :
2147 : 9326 : if (unsigned padding = -pos & (SECTION_ALIGN - 1))
2148 : : {
2149 : : #if !MAPPED_WRITING
2150 : : /* Align the section on disk, should help the necessary copies.
2151 : : fseeking to extend is non-portable. */
2152 : : static char zero[SECTION_ALIGN];
2153 : : if (::write (fd, &zero, padding) != ssize_t (padding))
2154 : : set_error (errno);
2155 : : #endif
2156 : 8120 : pos += padding;
2157 : : }
2158 : 9326 : return res;
2159 : : }
2160 : :
2161 : : /* Write a streaming buffer. It must be using us as an allocator. */
2162 : :
2163 : : #if MAPPED_WRITING
2164 : : unsigned
2165 : 238848 : elf_out::write (const bytes_out &buf)
2166 : : {
2167 : 238848 : gcc_checking_assert (buf.memory == this);
2168 : : /* A directly mapped buffer. */
2169 : 238848 : gcc_checking_assert (buf.buffer - hdr.buffer >= 0
2170 : : && buf.buffer - hdr.buffer + buf.size <= extent);
2171 : 238848 : unsigned res = pos;
2172 : 238848 : pos += buf.pos;
2173 : :
2174 : : /* Align up. We're not going to advance into the next page. */
2175 : 238848 : pos += -pos & (SECTION_ALIGN - 1);
2176 : :
2177 : 238848 : return res;
2178 : : }
2179 : : #endif
2180 : :
2181 : : /* Write data and add section. STRING_P is true for a string
2182 : : section, false for PROGBITS. NAME identifies the section (0 is the
2183 : : empty name). DATA is the contents. Return section number or 0 on
2184 : : failure (0 is the undef section). */
2185 : :
2186 : : unsigned
2187 : 238848 : elf_out::add (const bytes_out &data, bool string_p, unsigned name)
2188 : : {
2189 : 238848 : unsigned off = write (data);
2190 : :
2191 : 477696 : return add (string_p ? SHT_STRTAB : SHT_PROGBITS, name,
2192 : 238848 : off, data.pos, string_p ? SHF_STRINGS : SHF_NONE);
2193 : : }
2194 : :
2195 : : /* Begin writing the file. Initialize the section table and write an
2196 : : empty header. Return false on failure. */
2197 : :
2198 : : bool
2199 : 2327 : elf_out::begin ()
2200 : : {
2201 : 2327 : if (!parent::begin ())
2202 : : return false;
2203 : :
2204 : : /* Let the allocators pick a default. */
2205 : 2327 : data::simple_memory.grow (strtab, 0, false);
2206 : 2327 : data::simple_memory.grow (sectab, 0, false);
2207 : :
2208 : : /* The string table starts with an empty string. */
2209 : 2327 : name ("");
2210 : :
2211 : : /* Create the UNDEF section. */
2212 : 2327 : add (SHT_NONE);
2213 : :
2214 : : #if MAPPED_WRITING
2215 : : /* Start a mapping. */
2216 : 2327 : create_mapping (EXPERIMENT (page_size,
2217 : : (32767 + page_size) & ~(page_size - 1)));
2218 : 2327 : if (!hdr.buffer)
2219 : : return false;
2220 : : #endif
2221 : :
2222 : : /* Write an empty header. */
2223 : 2327 : grow (hdr, sizeof (header), true);
2224 : 2327 : header *h = reinterpret_cast<header *> (hdr.buffer);
2225 : 2327 : memset (h, 0, sizeof (header));
2226 : 2327 : hdr.pos = hdr.size;
2227 : 2327 : write (hdr);
2228 : 2327 : return !get_error ();
2229 : : }
2230 : :
2231 : : /* Finish writing the file. Write out the string & section tables.
2232 : : Fill in the header. Return true on error. */
2233 : :
2234 : : bool
2235 : 2436 : elf_out::end ()
2236 : : {
2237 : 2436 : if (fd >= 0)
2238 : : {
2239 : : /* Write the string table. */
2240 : 2333 : unsigned strnam = name (".strtab");
2241 : 2333 : unsigned stroff = write (strtab);
2242 : 2333 : unsigned strndx = add (SHT_STRTAB, strnam, stroff, strtab.pos,
2243 : : SHF_STRINGS);
2244 : :
2245 : : /* Store escape values in section[0]. */
2246 : 2333 : if (strndx >= SHN_LORESERVE)
2247 : : {
2248 : 0 : reinterpret_cast<section *> (sectab.buffer)->link = strndx;
2249 : 0 : strndx = SHN_XINDEX;
2250 : : }
2251 : 2333 : unsigned shnum = sectab.pos / sizeof (section);
2252 : 2333 : if (shnum >= SHN_LORESERVE)
2253 : : {
2254 : 0 : reinterpret_cast<section *> (sectab.buffer)->size = shnum;
2255 : 0 : shnum = SHN_XINDEX;
2256 : : }
2257 : :
2258 : 2333 : unsigned shoff = write (sectab);
2259 : :
2260 : : #if MAPPED_WRITING
2261 : 2333 : if (offset)
2262 : : {
2263 : 119 : remove_mapping ();
2264 : 119 : offset = 0;
2265 : 119 : create_mapping ((sizeof (header) + page_size - 1) & ~(page_size - 1),
2266 : : false);
2267 : : }
2268 : 2333 : unsigned length = pos;
2269 : : #else
2270 : : if (lseek (fd, 0, SEEK_SET) < 0)
2271 : : set_error (errno);
2272 : : #endif
2273 : : /* Write header. */
2274 : 2333 : if (!get_error ())
2275 : : {
2276 : : /* Write the correct header now. */
2277 : 2333 : header *h = reinterpret_cast<header *> (hdr.buffer);
2278 : 2333 : h->ident.magic[0] = 0x7f;
2279 : 2333 : h->ident.magic[1] = 'E'; /* Elrond */
2280 : 2333 : h->ident.magic[2] = 'L'; /* is an */
2281 : 2333 : h->ident.magic[3] = 'F'; /* elf. */
2282 : 2333 : h->ident.klass = MY_CLASS;
2283 : 2333 : h->ident.data = MY_ENDIAN;
2284 : 2333 : h->ident.version = EV_CURRENT;
2285 : 2333 : h->ident.osabi = OSABI_NONE;
2286 : 2333 : h->type = ET_NONE;
2287 : 2333 : h->machine = EM_NONE;
2288 : 2333 : h->version = EV_CURRENT;
2289 : 2333 : h->shoff = shoff;
2290 : 2333 : h->ehsize = sizeof (header);
2291 : 2333 : h->shentsize = sizeof (section);
2292 : 2333 : h->shnum = shnum;
2293 : 2333 : h->shstrndx = strndx;
2294 : :
2295 : 2333 : pos = 0;
2296 : 2333 : write (hdr);
2297 : : }
2298 : :
2299 : : #if MAPPED_WRITING
2300 : 2333 : remove_mapping ();
2301 : 2333 : if (ftruncate (fd, length))
2302 : 0 : set_error (errno);
2303 : : #endif
2304 : : }
2305 : :
2306 : 2436 : data::simple_memory.shrink (sectab);
2307 : 2436 : data::simple_memory.shrink (strtab);
2308 : :
2309 : 2436 : return parent::end ();
2310 : : }
2311 : :
2312 : : /********************************************************************/
2313 : :
2314 : : /* A dependency set. This is used during stream out to determine the
2315 : : connectivity of the graph. Every namespace-scope declaration that
2316 : : needs writing has a depset. The depset is filled with the (depsets
2317 : : of) declarations within this module that it references. For a
2318 : : declaration that'll generally be named types. For definitions
2319 : : it'll also be declarations in the body.
2320 : :
2321 : : From that we can convert the graph to a DAG, via determining the
2322 : : Strongly Connected Clusters. Each cluster is streamed
2323 : : independently, and thus we achieve lazy loading.
2324 : :
2325 : : Other decls that get a depset are namespaces themselves and
2326 : : unnameable declarations. */
2327 : :
2328 : : class depset {
2329 : : private:
2330 : : tree entity; /* Entity, or containing namespace. */
2331 : : uintptr_t discriminator; /* Flags or identifier. */
2332 : :
2333 : : public:
2334 : : /* The kinds of entity the depset could describe. The ordering is
2335 : : significant, see entity_kind_name. */
2336 : : enum entity_kind
2337 : : {
2338 : : EK_DECL, /* A decl. */
2339 : : EK_SPECIALIZATION, /* A specialization. */
2340 : : EK_PARTIAL, /* A partial specialization. */
2341 : : EK_USING, /* A using declaration (at namespace scope). */
2342 : : EK_NAMESPACE, /* A namespace. */
2343 : : EK_REDIRECT, /* Redirect to a template_decl. */
2344 : : EK_EXPLICIT_HWM,
2345 : : EK_BINDING = EK_EXPLICIT_HWM, /* Implicitly encoded. */
2346 : : EK_FOR_BINDING, /* A decl being inserted for a binding. */
2347 : : EK_INNER_DECL, /* A decl defined outside of its imported
2348 : : context. */
2349 : : EK_DIRECT_HWM = EK_PARTIAL + 1,
2350 : :
2351 : : EK_BITS = 3 /* Only need to encode below EK_EXPLICIT_HWM. */
2352 : : };
2353 : :
2354 : : private:
2355 : : /* Placement of bit fields in discriminator. */
2356 : : enum disc_bits
2357 : : {
2358 : : DB_ZERO_BIT, /* Set to disambiguate identifier from flags */
2359 : : DB_SPECIAL_BIT, /* First dep slot is special. */
2360 : : DB_KIND_BIT, /* Kind of the entity. */
2361 : : DB_KIND_BITS = EK_BITS,
2362 : : DB_DEFN_BIT = DB_KIND_BIT + DB_KIND_BITS,
2363 : : DB_IS_PENDING_BIT, /* Is a maybe-pending entity. */
2364 : : DB_TU_LOCAL_BIT, /* It is a TU-local entity. */
2365 : : DB_REFS_TU_LOCAL_BIT, /* Refers to a TU-local entity (but is not
2366 : : necessarily an exposure.) */
2367 : : DB_EXPOSURE_BIT, /* Exposes a TU-local entity. */
2368 : : DB_IMPORTED_BIT, /* An imported entity. */
2369 : : DB_UNREACHED_BIT, /* A yet-to-be reached entity. */
2370 : : DB_MAYBE_RECURSIVE_BIT, /* An entity maybe in a recursive cluster. */
2371 : : DB_ENTRY_BIT, /* The first reached recursive dep. */
2372 : : DB_HIDDEN_BIT, /* A hidden binding. */
2373 : : /* The following bits are not independent, but enumerating them is
2374 : : awkward. */
2375 : : DB_TYPE_SPEC_BIT, /* Specialization in the type table. */
2376 : : DB_FRIEND_SPEC_BIT, /* An instantiated template friend. */
2377 : : };
2378 : :
2379 : : public:
2380 : : /* The first slot is special for EK_SPECIALIZATIONS it is a
2381 : : spec_entry pointer. It is not relevant for the SCC
2382 : : determination. */
2383 : : vec<depset *> deps; /* Depsets we reference. */
2384 : :
2385 : : public:
2386 : : unsigned cluster; /* Strongly connected cluster, later entity number */
2387 : : unsigned section; /* Section written to. */
2388 : : /* During SCC construction, section is lowlink, until the depset is
2389 : : removed from the stack. See Tarjan algorithm for details. */
2390 : :
2391 : : private:
2392 : : /* Construction via factories. Destruction via hash traits. */
2393 : : depset (tree entity);
2394 : : ~depset ();
2395 : :
2396 : : public:
2397 : : static depset *make_binding (tree, tree);
2398 : : static depset *make_entity (tree, entity_kind, bool = false);
2399 : : /* Late setting a binding name -- /then/ insert into hash! */
2400 : : inline void set_binding_name (tree name)
2401 : : {
2402 : : gcc_checking_assert (!get_name ());
2403 : : discriminator = reinterpret_cast<uintptr_t> (name);
2404 : : }
2405 : :
2406 : : private:
2407 : 3251008 : template<unsigned I> void set_flag_bit ()
2408 : : {
2409 : 0 : gcc_checking_assert (I < 2 || !is_binding ());
2410 : 3251008 : discriminator |= 1u << I;
2411 : 1958891 : }
2412 : 280427 : template<unsigned I> void clear_flag_bit ()
2413 : : {
2414 : 0 : gcc_checking_assert (I < 2 || !is_binding ());
2415 : 280427 : discriminator &= ~(1u << I);
2416 : 280427 : }
2417 : 341790115 : template<unsigned I> bool get_flag_bit () const
2418 : : {
2419 : 0 : gcc_checking_assert (I < 2 || !is_binding ());
2420 : 411480763 : return bool ((discriminator >> I) & 1);
2421 : : }
2422 : :
2423 : : public:
2424 : 334598937 : bool is_binding () const
2425 : : {
2426 : 71929966 : return !get_flag_bit<DB_ZERO_BIT> ();
2427 : : }
2428 : 185203602 : entity_kind get_entity_kind () const
2429 : : {
2430 : 15508 : if (is_binding ())
2431 : : return EK_BINDING;
2432 : 139888835 : return entity_kind ((discriminator >> DB_KIND_BIT) & ((1u << EK_BITS) - 1));
2433 : : }
2434 : : const char *entity_kind_name () const;
2435 : :
2436 : : public:
2437 : 6206583 : bool has_defn () const
2438 : : {
2439 : : /* Never consider TU-local entities as having definitions, since
2440 : : we will never be accessing them from importers anyway. */
2441 : 8180737 : return get_flag_bit<DB_DEFN_BIT> () && !is_tu_local ();
2442 : : }
2443 : :
2444 : : public:
2445 : : /* This entity might be found other than by namespace-scope lookup;
2446 : : see module_state::write_pendings for more details. */
2447 : 1528269 : bool is_pending_entity () const
2448 : : {
2449 : 2443897 : return (get_entity_kind () == EK_SPECIALIZATION
2450 : 915628 : || get_entity_kind () == EK_PARTIAL
2451 : 2413389 : || (get_entity_kind () == EK_DECL
2452 : 865683 : && get_flag_bit<DB_IS_PENDING_BIT> ()));
2453 : : }
2454 : : public:
2455 : 27837065 : bool is_tu_local () const
2456 : : {
2457 : 16185838 : return get_flag_bit<DB_TU_LOCAL_BIT> ();
2458 : : }
2459 : 637248 : bool refs_tu_local () const
2460 : : {
2461 : 79367 : return get_flag_bit<DB_REFS_TU_LOCAL_BIT> ();
2462 : : }
2463 : 1029842 : bool is_exposure () const
2464 : : {
2465 : 1029842 : return get_flag_bit<DB_EXPOSURE_BIT> ();
2466 : : }
2467 : 18476727 : bool is_import () const
2468 : : {
2469 : 5732349 : return get_flag_bit<DB_IMPORTED_BIT> ();
2470 : : }
2471 : 11260480 : bool is_unreached () const
2472 : : {
2473 : 769656 : return get_flag_bit<DB_UNREACHED_BIT> ();
2474 : : }
2475 : 909193 : bool is_hidden () const
2476 : : {
2477 : 909193 : return get_flag_bit<DB_HIDDEN_BIT> ();
2478 : : }
2479 : 629217 : bool is_maybe_recursive () const
2480 : : {
2481 : 629217 : return get_flag_bit<DB_MAYBE_RECURSIVE_BIT> ();
2482 : : }
2483 : 822 : bool is_entry () const
2484 : : {
2485 : 822 : return get_flag_bit<DB_ENTRY_BIT> ();
2486 : : }
2487 : 918894 : bool is_type_spec () const
2488 : : {
2489 : 918894 : return get_flag_bit<DB_TYPE_SPEC_BIT> ();
2490 : : }
2491 : 918894 : bool is_friend_spec () const
2492 : : {
2493 : 918894 : return get_flag_bit<DB_FRIEND_SPEC_BIT> ();
2494 : : }
2495 : :
2496 : : public:
2497 : : /* We set these bit outside of depset. */
2498 : 3650 : void set_hidden_binding ()
2499 : : {
2500 : 3650 : set_flag_bit<DB_HIDDEN_BIT> ();
2501 : 3650 : }
2502 : 30 : void clear_hidden_binding ()
2503 : : {
2504 : 30 : clear_flag_bit<DB_HIDDEN_BIT> ();
2505 : 30 : }
2506 : :
2507 : : public:
2508 : 7191178 : bool is_special () const
2509 : : {
2510 : 7191178 : return get_flag_bit<DB_SPECIAL_BIT> ();
2511 : : }
2512 : 1292117 : void set_special ()
2513 : : {
2514 : 1292117 : set_flag_bit<DB_SPECIAL_BIT> ();
2515 : 0 : }
2516 : :
2517 : : public:
2518 : 84483945 : tree get_entity () const
2519 : : {
2520 : 84483945 : return entity;
2521 : : }
2522 : 15312838 : tree get_name () const
2523 : : {
2524 : 15312838 : gcc_checking_assert (is_binding ());
2525 : 15312838 : return reinterpret_cast <tree> (discriminator);
2526 : : }
2527 : :
2528 : : public:
2529 : : /* Traits for a hash table of pointers to bindings. */
2530 : : struct traits {
2531 : : /* Each entry is a pointer to a depset. */
2532 : : typedef depset *value_type;
2533 : : /* We lookup by container:maybe-identifier pair. */
2534 : : typedef std::pair<tree,tree> compare_type;
2535 : :
2536 : : static const bool empty_zero_p = true;
2537 : :
2538 : : /* hash and equality for compare_type. */
2539 : 11606971 : inline static hashval_t hash (const compare_type &p)
2540 : : {
2541 : 11606971 : hashval_t h = pointer_hash<tree_node>::hash (p.first);
2542 : 11606971 : if (p.second)
2543 : : {
2544 : 150455 : hashval_t nh = IDENTIFIER_HASH_VALUE (p.second);
2545 : 150455 : h = iterative_hash_hashval_t (h, nh);
2546 : : }
2547 : 11606971 : return h;
2548 : : }
2549 : 51293936 : inline static bool equal (const value_type b, const compare_type &p)
2550 : : {
2551 : 51293936 : if (b->entity != p.first)
2552 : : return false;
2553 : :
2554 : 8275653 : if (p.second)
2555 : 13420 : return b->discriminator == reinterpret_cast<uintptr_t> (p.second);
2556 : : else
2557 : 8262233 : return !b->is_binding ();
2558 : : }
2559 : :
2560 : : /* (re)hasher for a binding itself. */
2561 : 38856937 : inline static hashval_t hash (const value_type b)
2562 : : {
2563 : 38856937 : hashval_t h = pointer_hash<tree_node>::hash (b->entity);
2564 : 38856937 : if (b->is_binding ())
2565 : : {
2566 : 3342742 : hashval_t nh = IDENTIFIER_HASH_VALUE (b->get_name ());
2567 : 3342742 : h = iterative_hash_hashval_t (h, nh);
2568 : : }
2569 : 38856937 : return h;
2570 : : }
2571 : :
2572 : : /* Empty via NULL. */
2573 : 0 : static inline void mark_empty (value_type &p) {p = NULL;}
2574 : : static inline bool is_empty (value_type p) {return !p;}
2575 : :
2576 : : /* Nothing is deletable. Everything is insertable. */
2577 : : static bool is_deleted (value_type) { return false; }
2578 : : static void mark_deleted (value_type) { gcc_unreachable (); }
2579 : :
2580 : : /* We own the entities in the hash table. */
2581 : 1902592 : static void remove (value_type p)
2582 : : {
2583 : 1902592 : delete (p);
2584 : 1902592 : }
2585 : : };
2586 : :
2587 : : public:
2588 : : class hash : public hash_table<traits> {
2589 : : typedef traits::compare_type key_t;
2590 : : typedef hash_table<traits> parent;
2591 : :
2592 : : public:
2593 : : vec<depset *> worklist; /* Worklist of decls to walk. */
2594 : : hash *chain; /* Original table. */
2595 : : depset *current; /* Current depset being depended. */
2596 : : unsigned section; /* When writing out, the section. */
2597 : : bool reached_unreached; /* We reached an unreached entity. */
2598 : : bool writing_merge_key; /* We're writing merge key information. */
2599 : : bool ignore_tu_local; /* In a context where referencing a TU-local
2600 : : entity is not an exposure. */
2601 : :
2602 : : public:
2603 : 223368 : hash (size_t size, hash *c = NULL)
2604 : 446736 : : parent (size), chain (c), current (NULL), section (0),
2605 : 223368 : reached_unreached (false), writing_merge_key (false),
2606 : 223368 : ignore_tu_local (false)
2607 : : {
2608 : 223368 : worklist.create (size);
2609 : 223368 : }
2610 : 223368 : ~hash ()
2611 : : {
2612 : 2327 : worklist.release ();
2613 : 223368 : }
2614 : :
2615 : : public:
2616 : 63282254 : bool is_key_order () const
2617 : : {
2618 : 63282254 : return chain != NULL;
2619 : : }
2620 : :
2621 : : private:
2622 : : depset **entity_slot (tree entity, bool = true);
2623 : : depset **binding_slot (tree ctx, tree name, bool = true);
2624 : : depset *maybe_add_declaration (tree decl);
2625 : :
2626 : : public:
2627 : : depset *find_dependency (tree entity);
2628 : : depset *find_binding (tree ctx, tree name);
2629 : : depset *make_dependency (tree decl, entity_kind);
2630 : : void add_dependency (depset *);
2631 : :
2632 : : public:
2633 : : void add_mergeable (depset *);
2634 : : depset *add_dependency (tree decl, entity_kind);
2635 : : void add_namespace_context (depset *, tree ns);
2636 : :
2637 : : private:
2638 : : bool has_tu_local_tmpl_arg (tree decl, tree args, bool explain);
2639 : : bool is_tu_local_entity (tree decl, bool explain = false);
2640 : : bool is_tu_local_value (tree decl, tree expr, bool explain = false);
2641 : :
2642 : : private:
2643 : : static bool add_binding_entity (tree, WMB_Flags, void *);
2644 : :
2645 : : public:
2646 : : bool add_namespace_entities (tree ns, bitmap partitions);
2647 : : void add_specializations (bool decl_p);
2648 : : void add_partial_entities (vec<tree, va_gc> *);
2649 : : void add_class_entities (vec<tree, va_gc> *);
2650 : :
2651 : : private:
2652 : : void add_deduction_guides (tree decl);
2653 : :
2654 : : public:
2655 : : void find_dependencies (module_state *);
2656 : : bool finalize_dependencies ();
2657 : : vec<depset *> connect ();
2658 : : };
2659 : :
2660 : : public:
2661 : : struct tarjan {
2662 : : vec<depset *> result;
2663 : : vec<depset *> stack;
2664 : : unsigned index;
2665 : :
2666 : 223343 : tarjan (unsigned size)
2667 : 223343 : : index (0)
2668 : : {
2669 : 223343 : result.create (size);
2670 : 223343 : stack.create (50);
2671 : 223343 : }
2672 : 223343 : ~tarjan ()
2673 : : {
2674 : 223343 : gcc_assert (!stack.length ());
2675 : 223343 : stack.release ();
2676 : 223343 : }
2677 : :
2678 : : public:
2679 : : void connect (depset *);
2680 : : };
2681 : : };
2682 : :
2683 : : inline
2684 : 1902592 : depset::depset (tree entity)
2685 : 1902592 : :entity (entity), discriminator (0), cluster (0), section (0)
2686 : : {
2687 : 1902592 : deps.create (0);
2688 : : }
2689 : :
2690 : : inline
2691 : 1902592 : depset::~depset ()
2692 : : {
2693 : 1902592 : deps.release ();
2694 : 1902592 : }
2695 : :
2696 : : const char *
2697 : 39311 : depset::entity_kind_name () const
2698 : : {
2699 : : /* Same order as entity_kind. */
2700 : 39311 : static const char *const names[] =
2701 : : {"decl", "specialization", "partial", "using",
2702 : : "namespace", "redirect", "binding"};
2703 : 39311 : entity_kind kind = get_entity_kind ();
2704 : 39116 : gcc_checking_assert (kind < ARRAY_SIZE (names));
2705 : 39311 : return names[kind];
2706 : : }
2707 : :
2708 : : /* Create a depset for a namespace binding NS::NAME. */
2709 : :
2710 : 118602 : depset *depset::make_binding (tree ns, tree name)
2711 : : {
2712 : 118602 : depset *binding = new depset (ns);
2713 : :
2714 : 118602 : binding->discriminator = reinterpret_cast <uintptr_t> (name);
2715 : :
2716 : 118602 : return binding;
2717 : : }
2718 : :
2719 : 1783990 : depset *depset::make_entity (tree entity, entity_kind ek, bool is_defn)
2720 : : {
2721 : 1783990 : depset *r = new depset (entity);
2722 : :
2723 : 1783990 : r->discriminator = ((1 << DB_ZERO_BIT)
2724 : 1783990 : | (ek << DB_KIND_BIT)
2725 : 1783990 : | is_defn << DB_DEFN_BIT);
2726 : :
2727 : 1783990 : return r;
2728 : : }
2729 : :
2730 : : class pending_key
2731 : : {
2732 : : public:
2733 : : tree ns;
2734 : : tree id;
2735 : : };
2736 : :
2737 : : template<>
2738 : : struct default_hash_traits<pending_key>
2739 : : {
2740 : : using value_type = pending_key;
2741 : :
2742 : : static const bool empty_zero_p = false;
2743 : 31325383 : static hashval_t hash (const value_type &k)
2744 : : {
2745 : 31325383 : hashval_t h = IDENTIFIER_HASH_VALUE (k.id);
2746 : 31325383 : h = iterative_hash_hashval_t (DECL_UID (k.ns), h);
2747 : :
2748 : 31325383 : return h;
2749 : : }
2750 : 10009942 : static bool equal (const value_type &k, const value_type &l)
2751 : : {
2752 : 10009942 : return k.ns == l.ns && k.id == l.id;
2753 : : }
2754 : 174364 : static void mark_empty (value_type &k)
2755 : : {
2756 : 174364 : k.ns = k.id = NULL_TREE;
2757 : : }
2758 : 4724 : static void mark_deleted (value_type &k)
2759 : : {
2760 : 4724 : k.ns = NULL_TREE;
2761 : 4724 : gcc_checking_assert (k.id);
2762 : 4724 : }
2763 : 236087622 : static bool is_empty (const value_type &k)
2764 : : {
2765 : 236047824 : return k.ns == NULL_TREE && k.id == NULL_TREE;
2766 : : }
2767 : 14790537 : static bool is_deleted (const value_type &k)
2768 : : {
2769 : 14790537 : return k.ns == NULL_TREE && k.id != NULL_TREE;
2770 : : }
2771 : : static void remove (value_type &)
2772 : : {
2773 : : }
2774 : : };
2775 : :
2776 : : typedef hash_map<pending_key, auto_vec<unsigned>> pending_map_t;
2777 : :
2778 : : /* Not-loaded entities that are keyed to a namespace-scope
2779 : : identifier. See module_state::write_pendings for details. */
2780 : : pending_map_t *pending_table;
2781 : :
2782 : : /* Decls that need some post processing once a batch of lazy loads has
2783 : : completed. */
2784 : : vec<tree, va_heap, vl_embed> *post_load_decls;
2785 : :
2786 : : /* Some entities are keyed to another entitity for ODR purposes.
2787 : : For example, at namespace scope, 'inline auto var = []{};', that
2788 : : lambda is keyed to 'var', and follows its ODRness. */
2789 : : typedef hash_map<tree, auto_vec<tree>> keyed_map_t;
2790 : : static keyed_map_t *keyed_table;
2791 : :
2792 : : /* Instantiations of temploid friends imported from another module
2793 : : need to be attached to the same module as the temploid. This maps
2794 : : these decls to the temploid they are instantiated from, as there is
2795 : : no other easy way to get this information. */
2796 : : static GTY((cache)) decl_tree_cache_map *imported_temploid_friends;
2797 : :
2798 : : /********************************************************************/
2799 : : /* Tree streaming. The tree streaming is very specific to the tree
2800 : : structures themselves. A tag indicates the kind of tree being
2801 : : streamed. -ve tags indicate backreferences to already-streamed
2802 : : trees. Backreferences are auto-numbered. */
2803 : :
2804 : : /* Tree tags. */
2805 : : enum tree_tag {
2806 : : tt_null, /* NULL_TREE. */
2807 : : tt_tu_local, /* A TU-local entity. */
2808 : : tt_fixed, /* Fixed vector index. */
2809 : :
2810 : : tt_node, /* By-value node. */
2811 : : tt_decl, /* By-value mergeable decl. */
2812 : : tt_tpl_parm, /* Template parm. */
2813 : :
2814 : : /* The ordering of the following 4 is relied upon in
2815 : : trees_out::tree_node. */
2816 : : tt_id, /* Identifier node. */
2817 : : tt_conv_id, /* Conversion operator name. */
2818 : : tt_anon_id, /* Anonymous name. */
2819 : : tt_lambda_id, /* Lambda name. */
2820 : :
2821 : : tt_typedef_type, /* A (possibly implicit) typedefed type. */
2822 : : tt_derived_type, /* A type derived from another type. */
2823 : : tt_variant_type, /* A variant of another type. */
2824 : :
2825 : : tt_tinfo_var, /* Typeinfo object. */
2826 : : tt_tinfo_typedef, /* Typeinfo typedef. */
2827 : : tt_ptrmem_type, /* Pointer to member type. */
2828 : : tt_nttp_var, /* NTTP_OBJECT VAR_DECL. */
2829 : :
2830 : : tt_parm, /* Function parameter or result. */
2831 : : tt_enum_value, /* An enum value. */
2832 : : tt_enum_decl, /* An enum decl. */
2833 : : tt_data_member, /* Data member/using-decl. */
2834 : :
2835 : : tt_binfo, /* A BINFO. */
2836 : : tt_vtable, /* A vtable. */
2837 : : tt_thunk, /* A thunk. */
2838 : : tt_clone_ref,
2839 : :
2840 : : tt_entity, /* A extra-cluster entity. */
2841 : :
2842 : : tt_template, /* The TEMPLATE_RESULT of a template. */
2843 : : };
2844 : :
2845 : : enum walk_kind {
2846 : : WK_none, /* No walk to do (a back- or fixed-ref happened). */
2847 : : WK_normal, /* Normal walk (by-name if possible). */
2848 : :
2849 : : WK_value, /* By-value walk. */
2850 : : };
2851 : :
2852 : : enum merge_kind
2853 : : {
2854 : : MK_unique, /* Known unique. */
2855 : : MK_named, /* Found by CTX, NAME + maybe_arg types etc. */
2856 : : MK_field, /* Found by CTX and index on TYPE_FIELDS */
2857 : : MK_vtable, /* Found by CTX and index on TYPE_VTABLES */
2858 : : MK_as_base, /* Found by CTX. */
2859 : :
2860 : : MK_partial,
2861 : :
2862 : : MK_enum, /* Found by CTX, & 1stMemberNAME. */
2863 : : MK_keyed, /* Found by key & index. */
2864 : : MK_local_type, /* Found by CTX, index. */
2865 : :
2866 : : MK_friend_spec, /* Like named, but has a tmpl & args too. */
2867 : : MK_local_friend, /* Found by CTX, index. */
2868 : :
2869 : : MK_indirect_lwm = MK_enum,
2870 : :
2871 : : /* Template specialization kinds below. These are all found via
2872 : : primary template and specialization args. */
2873 : : MK_template_mask = 0x10, /* A template specialization. */
2874 : :
2875 : : MK_tmpl_decl_mask = 0x4, /* In decl table. */
2876 : :
2877 : : MK_tmpl_tmpl_mask = 0x1, /* We want TEMPLATE_DECL. */
2878 : :
2879 : : MK_type_spec = MK_template_mask,
2880 : : MK_decl_spec = MK_template_mask | MK_tmpl_decl_mask,
2881 : :
2882 : : MK_hwm = 0x20
2883 : : };
2884 : : /* This is more than a debugging array. NULLs are used to determine
2885 : : an invalid merge_kind number. */
2886 : : static char const *const merge_kind_name[MK_hwm] =
2887 : : {
2888 : : "unique", "named", "field", "vtable", /* 0...3 */
2889 : : "asbase", "partial", "enum", "attached", /* 4...7 */
2890 : :
2891 : : "local type", "friend spec", "local friend", NULL, /* 8...11 */
2892 : : NULL, NULL, NULL, NULL,
2893 : :
2894 : : "type spec", "type tmpl spec", /* 16,17 type (template). */
2895 : : NULL, NULL,
2896 : :
2897 : : "decl spec", "decl tmpl spec", /* 20,21 decl (template). */
2898 : : NULL, NULL,
2899 : : NULL, NULL, NULL, NULL,
2900 : : NULL, NULL, NULL, NULL,
2901 : : };
2902 : :
2903 : : /* Mergeable entity location data. */
2904 : : struct merge_key {
2905 : : cp_ref_qualifier ref_q : 2;
2906 : : unsigned index;
2907 : :
2908 : : tree ret; /* Return type, if appropriate. */
2909 : : tree args; /* Arg types, if appropriate. */
2910 : :
2911 : : tree constraints; /* Constraints. */
2912 : :
2913 : 1978011 : merge_key ()
2914 : 1978011 : :ref_q (REF_QUAL_NONE), index (0),
2915 : 1978011 : ret (NULL_TREE), args (NULL_TREE),
2916 : 1978011 : constraints (NULL_TREE)
2917 : : {
2918 : : }
2919 : : };
2920 : :
2921 : : /* Hashmap of merged duplicates. Usually decls, but can contain
2922 : : BINFOs. */
2923 : : typedef hash_map<tree,uintptr_t,
2924 : : simple_hashmap_traits<nodel_ptr_hash<tree_node>,uintptr_t> >
2925 : : duplicate_hash_map;
2926 : :
2927 : : /* Data needed for post-processing. */
2928 : : struct post_process_data {
2929 : : tree decl;
2930 : : location_t start_locus;
2931 : : location_t end_locus;
2932 : : bool returns_value;
2933 : : bool returns_null;
2934 : : bool returns_abnormally;
2935 : : bool infinite_loop;
2936 : : };
2937 : :
2938 : : /* Tree stream reader. Note that reading a stream doesn't mark the
2939 : : read trees with TREE_VISITED. Thus it's quite safe to have
2940 : : multiple concurrent readers. Which is good, because lazy
2941 : : loading.
2942 : :
2943 : : It's important that trees_in/out have internal linkage so that the
2944 : : compiler knows core_bools, lang_type_bools and lang_decl_bools have
2945 : : only a single caller (tree_node_bools) and inlines them appropriately. */
2946 : : namespace {
2947 : : class trees_in : public bytes_in {
2948 : : typedef bytes_in parent;
2949 : :
2950 : : private:
2951 : : module_state *state; /* Module being imported. */
2952 : : vec<tree> back_refs; /* Back references. */
2953 : : duplicate_hash_map *duplicates; /* Map from existings to duplicate. */
2954 : : vec<post_process_data> post_decls; /* Decls to post process. */
2955 : : unsigned unused; /* Inhibit any interior TREE_USED
2956 : : marking. */
2957 : :
2958 : : public:
2959 : : trees_in (module_state *);
2960 : : ~trees_in ();
2961 : :
2962 : : public:
2963 : : int insert (tree);
2964 : : tree back_ref (int);
2965 : :
2966 : : private:
2967 : : tree start (unsigned = 0);
2968 : :
2969 : : public:
2970 : : /* Needed for binfo writing */
2971 : : bool core_bools (tree, bits_in&);
2972 : :
2973 : : private:
2974 : : /* Stream tree_core, lang_decl_specific and lang_type_specific
2975 : : bits. */
2976 : : bool core_vals (tree);
2977 : : bool lang_type_bools (tree, bits_in&);
2978 : : bool lang_type_vals (tree);
2979 : : bool lang_decl_bools (tree, bits_in&);
2980 : : bool lang_decl_vals (tree);
2981 : : bool lang_vals (tree);
2982 : : bool tree_node_bools (tree);
2983 : : bool tree_node_vals (tree);
2984 : : tree tree_value ();
2985 : : tree decl_value ();
2986 : : tree tpl_parm_value ();
2987 : :
2988 : : private:
2989 : : tree chained_decls (); /* Follow DECL_CHAIN. */
2990 : : vec<tree, va_heap> *vec_chained_decls ();
2991 : : vec<tree, va_gc> *tree_vec (); /* vec of tree. */
2992 : : vec<tree_pair_s, va_gc> *tree_pair_vec (); /* vec of tree_pair. */
2993 : : tree tree_list (bool has_purpose);
2994 : :
2995 : : public:
2996 : : /* Read a tree node. */
2997 : : tree tree_node (bool is_use = false);
2998 : :
2999 : : private:
3000 : : bool install_entity (tree decl);
3001 : : tree tpl_parms (unsigned &tpl_levels);
3002 : : bool tpl_parms_fini (tree decl, unsigned tpl_levels);
3003 : : bool tpl_header (tree decl, unsigned *tpl_levels);
3004 : : int fn_parms_init (tree);
3005 : : void fn_parms_fini (int tag, tree fn, tree existing, bool has_defn);
3006 : : unsigned add_indirect_tpl_parms (tree);
3007 : : public:
3008 : : bool add_indirects (tree);
3009 : :
3010 : : public:
3011 : : /* Serialize various definitions. */
3012 : : bool read_definition (tree decl);
3013 : :
3014 : : private:
3015 : : void check_abi_tags (tree existing, tree decl, tree &eattr, tree &dattr);
3016 : : bool is_matching_decl (tree existing, tree decl, bool is_typedef);
3017 : : static bool install_implicit_member (tree decl);
3018 : : bool read_function_def (tree decl, tree maybe_template);
3019 : : bool read_var_def (tree decl, tree maybe_template);
3020 : : bool read_class_def (tree decl, tree maybe_template);
3021 : : bool read_enum_def (tree decl, tree maybe_template);
3022 : :
3023 : : public:
3024 : : tree decl_container ();
3025 : : tree key_mergeable (int tag, merge_kind, tree decl, tree inner, tree type,
3026 : : tree container, bool is_attached,
3027 : : bool is_imported_temploid_friend);
3028 : : unsigned binfo_mergeable (tree *);
3029 : :
3030 : : private:
3031 : : tree key_local_type (const merge_key&, tree, tree);
3032 : : uintptr_t *find_duplicate (tree existing);
3033 : : void register_duplicate (tree decl, tree existing);
3034 : : /* Mark as an already diagnosed bad duplicate. */
3035 : 48 : void unmatched_duplicate (tree existing)
3036 : : {
3037 : 96 : *find_duplicate (existing) |= 1;
3038 : 48 : }
3039 : :
3040 : : public:
3041 : 121085 : bool is_duplicate (tree decl)
3042 : : {
3043 : 242170 : return find_duplicate (decl) != NULL;
3044 : : }
3045 : 107650 : tree maybe_duplicate (tree decl)
3046 : : {
3047 : 107650 : if (uintptr_t *dup = find_duplicate (decl))
3048 : 7683 : return reinterpret_cast<tree> (*dup & ~uintptr_t (1));
3049 : : return decl;
3050 : : }
3051 : : tree odr_duplicate (tree decl, bool has_defn);
3052 : :
3053 : : public:
3054 : : /* Return the decls to postprocess. */
3055 : : const vec<post_process_data>& post_process ()
3056 : : {
3057 : : return post_decls;
3058 : : }
3059 : : private:
3060 : : /* Register DATA for postprocessing. */
3061 : 69047 : void post_process (post_process_data data)
3062 : : {
3063 : 69047 : post_decls.safe_push (data);
3064 : : }
3065 : :
3066 : : private:
3067 : : void assert_definition (tree, bool installing);
3068 : : };
3069 : : } // anon namespace
3070 : :
3071 : 167237 : trees_in::trees_in (module_state *state)
3072 : 167237 : :parent (), state (state), unused (0)
3073 : : {
3074 : 167237 : duplicates = NULL;
3075 : 167237 : back_refs.create (500);
3076 : 167237 : post_decls.create (0);
3077 : 167237 : }
3078 : :
3079 : 167237 : trees_in::~trees_in ()
3080 : : {
3081 : 261556 : delete (duplicates);
3082 : 167237 : back_refs.release ();
3083 : 167237 : post_decls.release ();
3084 : 167237 : }
3085 : :
3086 : : /* Tree stream writer. */
3087 : : namespace {
3088 : : class trees_out : public bytes_out {
3089 : : typedef bytes_out parent;
3090 : :
3091 : : private:
3092 : : module_state *state; /* The module we are writing. */
3093 : : ptr_int_hash_map tree_map; /* Trees to references */
3094 : : depset::hash *dep_hash; /* Dependency table. */
3095 : : int ref_num; /* Back reference number. */
3096 : : unsigned section;
3097 : : bool writing_local_entities; /* Whether we might walk into a TU-local
3098 : : entity we need to emit placeholders for. */
3099 : : #if CHECKING_P
3100 : : int importedness; /* Checker that imports not occurring
3101 : : inappropriately. +ve imports ok,
3102 : : -ve imports not ok. */
3103 : : #endif
3104 : :
3105 : : public:
3106 : : trees_out (allocator *, module_state *, depset::hash &deps, unsigned sec = 0);
3107 : : ~trees_out ();
3108 : :
3109 : : private:
3110 : : void mark_trees ();
3111 : : void unmark_trees ();
3112 : :
3113 : : public:
3114 : : /* Hey, let's ignore the well known STL iterator idiom. */
3115 : : void begin ();
3116 : : unsigned end (elf_out *sink, unsigned name, unsigned *crc_ptr);
3117 : : void end ();
3118 : :
3119 : : public:
3120 : : enum tags
3121 : : {
3122 : : tag_backref = -1, /* Upper bound on the backrefs. */
3123 : : tag_value = 0, /* Write by value. */
3124 : : tag_fixed /* Lower bound on the fixed trees. */
3125 : : };
3126 : :
3127 : : public:
3128 : : /* The walk is used for three similar purposes:
3129 : :
3130 : : 1. The initial scan for dependencies.
3131 : : 2. Once dependencies have been found, ordering them.
3132 : : 3. Writing dependencies to file (streaming_p).
3133 : :
3134 : : For cases where it matters, these accessers can be used to determine
3135 : : which state we're in. */
3136 : 45653067 : bool is_initial_scan () const
3137 : : {
3138 : 77129313 : return !streaming_p () && !is_key_order ();
3139 : : }
3140 : 42844174 : bool is_key_order () const
3141 : : {
3142 : 31476246 : return dep_hash->is_key_order ();
3143 : : }
3144 : :
3145 : : public:
3146 : : int insert (tree, walk_kind = WK_normal);
3147 : :
3148 : : private:
3149 : : void start (tree, bool = false);
3150 : :
3151 : : private:
3152 : : walk_kind ref_node (tree);
3153 : : public:
3154 : : int get_tag (tree);
3155 : 442082 : void set_importing (int i ATTRIBUTE_UNUSED)
3156 : : {
3157 : : #if CHECKING_P
3158 : 442082 : importedness = i;
3159 : : #endif
3160 : : }
3161 : :
3162 : : private:
3163 : : void core_bools (tree, bits_out&);
3164 : : void core_vals (tree);
3165 : : void lang_type_bools (tree, bits_out&);
3166 : : void lang_type_vals (tree);
3167 : : void lang_decl_bools (tree, bits_out&);
3168 : : void lang_decl_vals (tree);
3169 : : void lang_vals (tree);
3170 : : void tree_node_bools (tree);
3171 : : void tree_node_vals (tree);
3172 : :
3173 : : private:
3174 : : void chained_decls (tree);
3175 : : void vec_chained_decls (tree);
3176 : : void tree_vec (vec<tree, va_gc> *);
3177 : : void tree_pair_vec (vec<tree_pair_s, va_gc> *);
3178 : : void tree_list (tree, bool has_purpose);
3179 : :
3180 : : private:
3181 : : bool has_tu_local_dep (tree) const;
3182 : : tree find_tu_local_decl (tree);
3183 : :
3184 : : public:
3185 : : /* Mark a node for by-value walking. */
3186 : : void mark_by_value (tree);
3187 : :
3188 : : public:
3189 : : void tree_node (tree);
3190 : :
3191 : : private:
3192 : : void install_entity (tree decl, depset *);
3193 : : void tpl_parms (tree parms, unsigned &tpl_levels);
3194 : : void tpl_parms_fini (tree decl, unsigned tpl_levels);
3195 : : void fn_parms_fini (tree) {}
3196 : : unsigned add_indirect_tpl_parms (tree);
3197 : : public:
3198 : : void add_indirects (tree);
3199 : : void fn_parms_init (tree);
3200 : : void tpl_header (tree decl, unsigned *tpl_levels);
3201 : :
3202 : : public:
3203 : : merge_kind get_merge_kind (tree decl, depset *maybe_dep);
3204 : : tree decl_container (tree decl);
3205 : : void key_mergeable (int tag, merge_kind, tree decl, tree inner,
3206 : : tree container, depset *maybe_dep);
3207 : : void binfo_mergeable (tree binfo);
3208 : :
3209 : : private:
3210 : : void key_local_type (merge_key&, tree, tree);
3211 : : bool decl_node (tree, walk_kind ref);
3212 : : void type_node (tree);
3213 : : void tree_value (tree);
3214 : : void tpl_parm_value (tree);
3215 : :
3216 : : public:
3217 : : void decl_value (tree, depset *);
3218 : :
3219 : : public:
3220 : : /* Serialize various definitions. */
3221 : : void write_definition (tree decl, bool refs_tu_local = false);
3222 : : void mark_declaration (tree decl, bool do_defn);
3223 : :
3224 : : private:
3225 : : void mark_function_def (tree decl);
3226 : : void mark_var_def (tree decl);
3227 : : void mark_class_def (tree decl);
3228 : : void mark_enum_def (tree decl);
3229 : : void mark_class_member (tree decl, bool do_defn = true);
3230 : : void mark_binfos (tree type);
3231 : :
3232 : : private:
3233 : : void write_var_def (tree decl);
3234 : : void write_function_def (tree decl);
3235 : : void write_class_def (tree decl);
3236 : : void write_enum_def (tree decl);
3237 : :
3238 : : private:
3239 : : static void assert_definition (tree);
3240 : :
3241 : : public:
3242 : : static void instrument ();
3243 : :
3244 : : private:
3245 : : /* Tree instrumentation. */
3246 : : static unsigned tree_val_count;
3247 : : static unsigned decl_val_count;
3248 : : static unsigned back_ref_count;
3249 : : static unsigned tu_local_count;
3250 : : static unsigned null_count;
3251 : : };
3252 : : } // anon namespace
3253 : :
3254 : : /* Instrumentation counters. */
3255 : : unsigned trees_out::tree_val_count;
3256 : : unsigned trees_out::decl_val_count;
3257 : : unsigned trees_out::back_ref_count;
3258 : : unsigned trees_out::tu_local_count;
3259 : : unsigned trees_out::null_count;
3260 : :
3261 : 446753 : trees_out::trees_out (allocator *mem, module_state *state, depset::hash &deps,
3262 : 446753 : unsigned section)
3263 : 893506 : :parent (mem), state (state), tree_map (500),
3264 : 446753 : dep_hash (&deps), ref_num (0), section (section),
3265 : 446753 : writing_local_entities (false)
3266 : : {
3267 : : #if CHECKING_P
3268 : 446753 : importedness = 0;
3269 : : #endif
3270 : 446753 : }
3271 : :
3272 : 446753 : trees_out::~trees_out ()
3273 : : {
3274 : 446753 : }
3275 : :
3276 : : /********************************************************************/
3277 : : /* Location. We're aware of the line-map concept and reproduce it
3278 : : here. Each imported module allocates a contiguous span of ordinary
3279 : : maps, and of macro maps. adhoc maps are serialized by contents,
3280 : : not pre-allocated. The scattered linemaps of a module are
3281 : : coalesced when writing. */
3282 : :
3283 : :
3284 : : /* I use half-open [first,second) ranges. */
3285 : : typedef std::pair<line_map_uint_t,line_map_uint_t> range_t;
3286 : :
3287 : : /* A range of locations. */
3288 : : typedef std::pair<location_t,location_t> loc_range_t;
3289 : :
3290 : : /* Spans of the line maps that are occupied by this TU. I.e. not
3291 : : within imports. Only extended when in an interface unit.
3292 : : Interval zero corresponds to the forced header linemap(s). This
3293 : : is a singleton object. */
3294 : :
3295 : : class loc_spans {
3296 : : public:
3297 : : /* An interval of line maps. The line maps here represent a contiguous
3298 : : non-imported range. */
3299 : 5127 : struct span {
3300 : : loc_range_t ordinary; /* Ordinary map location range. */
3301 : : loc_range_t macro; /* Macro map location range. */
3302 : : /* Add to locs to get serialized loc. */
3303 : : location_diff_t ordinary_delta;
3304 : : location_diff_t macro_delta;
3305 : : };
3306 : :
3307 : : private:
3308 : : vec<span> *spans;
3309 : : bool locs_exhausted_p;
3310 : :
3311 : : public:
3312 : : loc_spans ()
3313 : : /* Do not preallocate spans, as that causes
3314 : : --enable-detailed-mem-stats problems. */
3315 : : : spans (nullptr), locs_exhausted_p (false)
3316 : : {
3317 : : }
3318 : 94249 : ~loc_spans ()
3319 : : {
3320 : 94249 : delete spans;
3321 : 94249 : }
3322 : :
3323 : : public:
3324 : 248 : span &operator[] (unsigned ix)
3325 : : {
3326 : 496 : return (*spans)[ix];
3327 : : }
3328 : : unsigned length () const
3329 : : {
3330 : : return spans->length ();
3331 : : }
3332 : :
3333 : : public:
3334 : 13321 : bool init_p () const
3335 : : {
3336 : 13321 : return spans != nullptr;
3337 : : }
3338 : : /* Initializer. */
3339 : : void init (const line_maps *lmaps, const line_map_ordinary *map);
3340 : :
3341 : : /* Slightly skewed preprocessed files can cause us to miss an
3342 : : initialization in some places. Fallback initializer. */
3343 : 4990 : void maybe_init ()
3344 : : {
3345 : 4990 : if (!init_p ())
3346 : 6 : init (line_table, nullptr);
3347 : 4990 : }
3348 : :
3349 : : public:
3350 : : enum {
3351 : : SPAN_RESERVED = 0, /* Reserved (fixed) locations. */
3352 : : SPAN_FIRST = 1, /* LWM of locations to stream */
3353 : : SPAN_MAIN = 2 /* Main file and onwards. */
3354 : : };
3355 : :
3356 : : public:
3357 : 427512 : location_t main_start () const
3358 : : {
3359 : 427512 : return (*spans)[SPAN_MAIN].ordinary.first;
3360 : : }
3361 : :
3362 : : public:
3363 : : void open (location_t);
3364 : : void close ();
3365 : :
3366 : : public:
3367 : : /* Propagate imported linemaps to us, if needed. */
3368 : : bool maybe_propagate (module_state *import, location_t loc);
3369 : :
3370 : : public:
3371 : : /* Whether we can no longer represent new imported locations. */
3372 : 0 : bool locations_exhausted_p () const
3373 : : {
3374 : 0 : return locs_exhausted_p;
3375 : : }
3376 : 0 : void report_location_exhaustion (location_t loc)
3377 : : {
3378 : 0 : if (!locs_exhausted_p)
3379 : : {
3380 : : /* Just give the notice once. */
3381 : 0 : locs_exhausted_p = true;
3382 : 0 : inform (loc, "unable to represent further imported source locations");
3383 : : }
3384 : : }
3385 : :
3386 : : public:
3387 : : const span *ordinary (location_t);
3388 : : const span *macro (location_t);
3389 : : };
3390 : :
3391 : : static loc_spans spans;
3392 : :
3393 : : /* Information about ordinary locations we stream out. */
3394 : : struct ord_loc_info
3395 : : {
3396 : : const line_map_ordinary *src; // line map we're based on
3397 : : line_map_uint_t offset; // offset to this line
3398 : : line_map_uint_t span; // number of locs we span
3399 : : line_map_uint_t remap; // serialization
3400 : :
3401 : 186805571 : static int compare (const void *a_, const void *b_)
3402 : : {
3403 : 186805571 : auto *a = static_cast<const ord_loc_info *> (a_);
3404 : 186805571 : auto *b = static_cast<const ord_loc_info *> (b_);
3405 : :
3406 : 186805571 : if (a->src != b->src)
3407 : 42276992 : return a->src < b->src ? -1 : +1;
3408 : :
3409 : : // Ensure no overlap
3410 : 158232331 : gcc_checking_assert (a->offset + a->span <= b->offset
3411 : : || b->offset + b->span <= a->offset);
3412 : :
3413 : 158232331 : gcc_checking_assert (a->offset != b->offset);
3414 : 158232331 : return a->offset < b->offset ? -1 : +1;
3415 : : }
3416 : : };
3417 : : struct ord_loc_traits
3418 : : {
3419 : : typedef ord_loc_info value_type;
3420 : : typedef value_type compare_type;
3421 : :
3422 : : static const bool empty_zero_p = false;
3423 : :
3424 : 77400985 : static hashval_t hash (const value_type &v)
3425 : : {
3426 : 65710224 : auto h = pointer_hash<const line_map_ordinary>::hash (v.src);
3427 : 77400985 : return iterative_hash_hashval_t (v.offset, h);
3428 : : }
3429 : 85319443 : static bool equal (const value_type &v, const compare_type p)
3430 : : {
3431 : 85319443 : return v.src == p.src && v.offset == p.offset;
3432 : : }
3433 : :
3434 : 5643537 : static void mark_empty (value_type &v)
3435 : : {
3436 : 5643537 : v.src = nullptr;
3437 : : }
3438 : 248896793 : static bool is_empty (value_type &v)
3439 : : {
3440 : 248896793 : return !v.src;
3441 : : }
3442 : :
3443 : : static bool is_deleted (value_type &) { return false; }
3444 : : static void mark_deleted (value_type &) { gcc_unreachable (); }
3445 : :
3446 : 1421585 : static void remove (value_type &) {}
3447 : : };
3448 : : /* Table keyed by ord_loc_info, used for noting. */
3449 : : static hash_table<ord_loc_traits> *ord_loc_table;
3450 : : /* Sorted vector, used for writing. */
3451 : : static vec<ord_loc_info> *ord_loc_remap;
3452 : :
3453 : : /* Information about macro locations we stream out. */
3454 : : struct macro_loc_info
3455 : : {
3456 : : const line_map_macro *src; // original expansion
3457 : : line_map_uint_t remap; // serialization
3458 : :
3459 : 6468158 : static int compare (const void *a_, const void *b_)
3460 : : {
3461 : 6468158 : auto *a = static_cast<const macro_loc_info *> (a_);
3462 : 6468158 : auto *b = static_cast<const macro_loc_info *> (b_);
3463 : :
3464 : 6468158 : gcc_checking_assert (MAP_START_LOCATION (a->src)
3465 : : != MAP_START_LOCATION (b->src));
3466 : 6468158 : if (MAP_START_LOCATION (a->src) < MAP_START_LOCATION (b->src))
3467 : : return -1;
3468 : : else
3469 : 3118211 : return +1;
3470 : : }
3471 : : };
3472 : : struct macro_loc_traits
3473 : : {
3474 : : typedef macro_loc_info value_type;
3475 : : typedef const line_map_macro *compare_type;
3476 : :
3477 : : static const bool empty_zero_p = false;
3478 : :
3479 : 6611787 : static hashval_t hash (compare_type p)
3480 : : {
3481 : 6611787 : return pointer_hash<const line_map_macro>::hash (p);
3482 : : }
3483 : 5679393 : static hashval_t hash (const value_type &v)
3484 : : {
3485 : 5679393 : return hash (v.src);
3486 : : }
3487 : : static bool equal (const value_type &v, const compare_type p)
3488 : : {
3489 : : return v.src == p;
3490 : : }
3491 : :
3492 : 472499 : static void mark_empty (value_type &v)
3493 : : {
3494 : 472499 : v.src = nullptr;
3495 : : }
3496 : 20165739 : static bool is_empty (value_type &v)
3497 : : {
3498 : 20165739 : return !v.src;
3499 : : }
3500 : :
3501 : : static bool is_deleted (value_type &) { return false; }
3502 : : static void mark_deleted (value_type &) { gcc_unreachable (); }
3503 : :
3504 : 111647 : static void remove (value_type &) {}
3505 : : };
3506 : : /* Table keyed by line_map_macro, used for noting. */
3507 : : static hash_table<macro_loc_traits> *macro_loc_table;
3508 : : /* Sorted vector, used for writing. */
3509 : : static vec<macro_loc_info> *macro_loc_remap;
3510 : :
3511 : : /* Indirection to allow bsearching imports by ordinary location. */
3512 : : static vec<module_state *> *ool;
3513 : :
3514 : : /********************************************************************/
3515 : : /* Data needed by a module during the process of loading. */
3516 : : struct GTY(()) slurping {
3517 : :
3518 : : /* Remap import's module numbering to our numbering. Values are
3519 : : shifted by 1. Bit0 encodes if the import is direct. */
3520 : : vec<unsigned, va_heap, vl_embed> *
3521 : : GTY((skip)) remap; /* Module owner remapping. */
3522 : :
3523 : : elf_in *GTY((skip)) from; /* The elf loader. */
3524 : :
3525 : : /* This map is only for header imports themselves -- the global
3526 : : headers bitmap hold it for the current TU. */
3527 : : bitmap headers; /* Transitive set of direct imports, including
3528 : : self. Used for macro visibility and
3529 : : priority. */
3530 : :
3531 : : /* These objects point into the mmapped area, unless we're not doing
3532 : : that, or we got frozen or closed. In those cases they point to
3533 : : buffers we own. */
3534 : : bytes_in macro_defs; /* Macro definitions. */
3535 : : bytes_in macro_tbl; /* Macro table. */
3536 : :
3537 : : /* Location remapping. first->ordinary, second->macro. */
3538 : : range_t GTY((skip)) loc_deltas;
3539 : :
3540 : : unsigned current; /* Section currently being loaded. */
3541 : : unsigned remaining; /* Number of lazy sections yet to read. */
3542 : : unsigned lru; /* An LRU counter. */
3543 : :
3544 : : public:
3545 : : slurping (elf_in *);
3546 : : ~slurping ();
3547 : :
3548 : : public:
3549 : : /* Close the ELF file, if it's open. */
3550 : 4517 : void close ()
3551 : : {
3552 : 4517 : if (from)
3553 : : {
3554 : 2464 : from->end ();
3555 : 4928 : delete from;
3556 : 2464 : from = NULL;
3557 : : }
3558 : 4517 : }
3559 : :
3560 : : public:
3561 : : void release_macros ();
3562 : :
3563 : : public:
3564 : 2522 : void alloc_remap (unsigned size)
3565 : : {
3566 : 2522 : gcc_assert (!remap);
3567 : 2522 : vec_safe_reserve (remap, size);
3568 : 5352 : for (unsigned ix = size; ix--;)
3569 : 2830 : remap->quick_push (0);
3570 : 2522 : }
3571 : 856430 : unsigned remap_module (unsigned owner)
3572 : : {
3573 : 856430 : if (owner < remap->length ())
3574 : 856430 : return (*remap)[owner] >> 1;
3575 : : return 0;
3576 : : }
3577 : :
3578 : : public:
3579 : : /* GC allocation. But we must explicitly delete it. */
3580 : 2540 : static void *operator new (size_t x)
3581 : : {
3582 : 5080 : return ggc_alloc_atomic (x);
3583 : : }
3584 : 2464 : static void operator delete (void *p)
3585 : : {
3586 : 2464 : ggc_free (p);
3587 : 2464 : }
3588 : : };
3589 : :
3590 : 2540 : slurping::slurping (elf_in *from)
3591 : 2540 : : remap (NULL), from (from),
3592 : 2540 : headers (BITMAP_GGC_ALLOC ()), macro_defs (), macro_tbl (),
3593 : 2540 : loc_deltas (0, 0),
3594 : 2540 : current (~0u), remaining (0), lru (0)
3595 : : {
3596 : 2540 : }
3597 : :
3598 : 2464 : slurping::~slurping ()
3599 : : {
3600 : 2464 : vec_free (remap);
3601 : 2464 : remap = NULL;
3602 : 2464 : release_macros ();
3603 : 2464 : close ();
3604 : 2464 : }
3605 : :
3606 : 4517 : void slurping::release_macros ()
3607 : : {
3608 : 4517 : if (macro_defs.size)
3609 : 832 : elf_in::release (from, macro_defs);
3610 : 4517 : if (macro_tbl.size)
3611 : 0 : elf_in::release (from, macro_tbl);
3612 : 4517 : }
3613 : :
3614 : : /* Flags for extensions that end up being streamed. */
3615 : :
3616 : : enum streamed_extensions {
3617 : : SE_OPENMP_SIMD = 1 << 0,
3618 : : SE_OPENMP = 1 << 1,
3619 : : SE_OPENACC = 1 << 2,
3620 : : SE_BITS = 3
3621 : : };
3622 : :
3623 : : /* Counter indices. */
3624 : : enum module_state_counts
3625 : : {
3626 : : MSC_sec_lwm,
3627 : : MSC_sec_hwm,
3628 : : MSC_pendings,
3629 : : MSC_entities,
3630 : : MSC_namespaces,
3631 : : MSC_bindings,
3632 : : MSC_macros,
3633 : : MSC_inits,
3634 : : MSC_HWM
3635 : : };
3636 : :
3637 : : /********************************************************************/
3638 : : struct module_state_config;
3639 : :
3640 : : /* Increasing levels of loadedness. */
3641 : : enum module_loadedness {
3642 : : ML_NONE, /* Not loaded. */
3643 : : ML_CONFIG, /* Config loaed. */
3644 : : ML_PREPROCESSOR, /* Preprocessor loaded. */
3645 : : ML_LANGUAGE, /* Language loaded. */
3646 : : };
3647 : :
3648 : : /* Increasing levels of directness (toplevel) of import. */
3649 : : enum module_directness {
3650 : : MD_NONE, /* Not direct. */
3651 : : MD_PARTITION_DIRECT, /* Direct import of a partition. */
3652 : : MD_DIRECT, /* Direct import. */
3653 : : MD_PURVIEW_DIRECT, /* direct import in purview. */
3654 : : };
3655 : :
3656 : : /* State of a particular module. */
3657 : :
3658 : : class GTY((chain_next ("%h.parent"), for_user)) module_state {
3659 : : public:
3660 : : /* We always import & export ourselves. */
3661 : : bitmap imports; /* Transitive modules we're importing. */
3662 : : bitmap exports; /* Subset of that, that we're exporting. */
3663 : :
3664 : : module_state *parent;
3665 : : tree name; /* Name of the module. */
3666 : :
3667 : : slurping *slurp; /* Data for loading. */
3668 : :
3669 : : const char *flatname; /* Flatname of module. */
3670 : : char *filename; /* CMI Filename */
3671 : :
3672 : : /* Indices into the entity_ary. */
3673 : : unsigned entity_lwm;
3674 : : unsigned entity_num;
3675 : :
3676 : : /* Location ranges for this module. adhoc-locs are decomposed, so
3677 : : don't have a range. */
3678 : : loc_range_t GTY((skip)) ordinary_locs;
3679 : : loc_range_t GTY((skip)) macro_locs; // [lwm,num)
3680 : :
3681 : : /* LOC is first set too the importing location. When initially
3682 : : loaded it refers to a module loc whose parent is the importing
3683 : : location. */
3684 : : location_t loc; /* Location referring to module itself. */
3685 : : unsigned crc; /* CRC we saw reading it in. */
3686 : :
3687 : : unsigned mod; /* Module owner number. */
3688 : : unsigned remap; /* Remapping during writing. */
3689 : :
3690 : : unsigned short subst; /* Mangle subst if !0. */
3691 : :
3692 : : /* How loaded this module is. */
3693 : : enum module_loadedness loadedness : 2;
3694 : :
3695 : : bool module_p : 1; /* /The/ module of this TU. */
3696 : : bool header_p : 1; /* Is a header unit. */
3697 : : bool interface_p : 1; /* An interface. */
3698 : : bool partition_p : 1; /* A partition. */
3699 : :
3700 : : /* How directly this module is imported. */
3701 : : enum module_directness directness : 2;
3702 : :
3703 : : bool exported_p : 1; /* directness != MD_NONE && exported. */
3704 : : bool cmi_noted_p : 1; /* We've told the user about the CMI, don't
3705 : : do it again */
3706 : : bool active_init_p : 1; /* This module's global initializer needs
3707 : : calling. */
3708 : : bool inform_cmi_p : 1; /* Inform of a read/write. */
3709 : : bool visited_p : 1; /* A walk-once flag. */
3710 : : /* Record extensions emitted or permitted. */
3711 : : unsigned extensions : SE_BITS;
3712 : : /* 14 bits used, 2 bits remain */
3713 : :
3714 : : public:
3715 : : module_state (tree name, module_state *, bool);
3716 : : ~module_state ();
3717 : :
3718 : : public:
3719 : 2514 : void release ()
3720 : : {
3721 : 2514 : imports = exports = NULL;
3722 : 2514 : slurped ();
3723 : 2464 : }
3724 : 4567 : void slurped ()
3725 : : {
3726 : 4567 : delete slurp;
3727 : 4567 : slurp = NULL;
3728 : 4567 : }
3729 : 1016267 : elf_in *from () const
3730 : : {
3731 : 1016267 : return slurp->from;
3732 : : }
3733 : :
3734 : : public:
3735 : : /* Kind of this module. */
3736 : 99210 : bool is_module () const
3737 : : {
3738 : 99210 : return module_p;
3739 : : }
3740 : 1534249 : bool is_header () const
3741 : : {
3742 : 1534249 : return header_p;
3743 : : }
3744 : 486 : bool is_interface () const
3745 : : {
3746 : 486 : return interface_p;
3747 : : }
3748 : 146520 : bool is_partition () const
3749 : : {
3750 : 146520 : return partition_p;
3751 : : }
3752 : :
3753 : : /* How this module is used in the current TU. */
3754 : 33 : bool is_exported () const
3755 : : {
3756 : 33 : return exported_p;
3757 : : }
3758 : 17515 : bool is_direct () const
3759 : : {
3760 : 17515 : return directness >= MD_DIRECT;
3761 : : }
3762 : 226 : bool is_purview_direct () const
3763 : : {
3764 : 226 : return directness == MD_PURVIEW_DIRECT;
3765 : : }
3766 : 349 : bool is_partition_direct () const
3767 : : {
3768 : 349 : return directness == MD_PARTITION_DIRECT;
3769 : : }
3770 : :
3771 : : public:
3772 : : /* Is this a real module? */
3773 : 13825 : bool has_location () const
3774 : : {
3775 : 13825 : return loc != UNKNOWN_LOCATION;
3776 : : }
3777 : :
3778 : : public:
3779 : : bool check_not_purview (location_t loc);
3780 : :
3781 : : public:
3782 : : void mangle (bool include_partition);
3783 : :
3784 : : public:
3785 : : void set_import (module_state const *, bool is_export);
3786 : : void announce (const char *) const;
3787 : :
3788 : : public:
3789 : : /* Read and write module. */
3790 : : bool write_begin (elf_out *to, cpp_reader *,
3791 : : module_state_config &, unsigned &crc);
3792 : : void write_end (elf_out *to, cpp_reader *,
3793 : : module_state_config &, unsigned &crc);
3794 : : bool read_initial (cpp_reader *);
3795 : : bool read_preprocessor (bool);
3796 : : bool read_language (bool);
3797 : :
3798 : : public:
3799 : : /* Read a section. */
3800 : : bool load_section (unsigned snum, binding_slot *mslot);
3801 : : /* Lazily read a section. */
3802 : : bool lazy_load (unsigned index, binding_slot *mslot);
3803 : :
3804 : : public:
3805 : : /* Juggle a limited number of file numbers. */
3806 : : static void freeze_an_elf ();
3807 : : bool maybe_defrost ();
3808 : :
3809 : : public:
3810 : : void maybe_completed_reading ();
3811 : : bool check_read (bool outermost, bool ok);
3812 : :
3813 : : private:
3814 : : /* The README, for human consumption. */
3815 : : void write_readme (elf_out *to, cpp_reader *, const char *dialect);
3816 : : void write_env (elf_out *to);
3817 : :
3818 : : private:
3819 : : /* Import tables. */
3820 : : void write_imports (bytes_out &cfg, bool direct);
3821 : : unsigned read_imports (bytes_in &cfg, cpp_reader *, line_maps *maps);
3822 : :
3823 : : private:
3824 : : void write_imports (elf_out *to, unsigned *crc_ptr);
3825 : : bool read_imports (cpp_reader *, line_maps *);
3826 : :
3827 : : private:
3828 : : void write_partitions (elf_out *to, unsigned, unsigned *crc_ptr);
3829 : : bool read_partitions (unsigned);
3830 : :
3831 : : private:
3832 : : void write_config (elf_out *to, struct module_state_config &, unsigned crc);
3833 : : bool read_config (struct module_state_config &);
3834 : : static void write_counts (elf_out *to, unsigned [MSC_HWM], unsigned *crc_ptr);
3835 : : bool read_counts (unsigned *);
3836 : :
3837 : : public:
3838 : : void note_cmi_name ();
3839 : :
3840 : : private:
3841 : : static unsigned write_bindings (elf_out *to, vec<depset *> depsets,
3842 : : unsigned *crc_ptr);
3843 : : bool read_bindings (unsigned count, unsigned lwm, unsigned hwm);
3844 : :
3845 : : static void write_namespace (bytes_out &sec, depset *ns_dep);
3846 : : tree read_namespace (bytes_in &sec);
3847 : :
3848 : : void write_namespaces (elf_out *to, vec<depset *> spaces,
3849 : : unsigned, unsigned *crc_ptr);
3850 : : bool read_namespaces (unsigned);
3851 : :
3852 : : void intercluster_seed (trees_out &sec, unsigned index, depset *dep);
3853 : : unsigned write_cluster (elf_out *to, depset *depsets[], unsigned size,
3854 : : depset::hash &, unsigned *counts, unsigned *crc_ptr);
3855 : : bool read_cluster (unsigned snum);
3856 : :
3857 : : private:
3858 : : unsigned write_inits (elf_out *to, depset::hash &, unsigned *crc_ptr);
3859 : : bool read_inits (unsigned count);
3860 : :
3861 : : private:
3862 : : unsigned write_pendings (elf_out *to, vec<depset *> depsets,
3863 : : depset::hash &, unsigned *crc_ptr);
3864 : : bool read_pendings (unsigned count);
3865 : :
3866 : : private:
3867 : : void write_entities (elf_out *to, vec<depset *> depsets,
3868 : : unsigned count, unsigned *crc_ptr);
3869 : : bool read_entities (unsigned count, unsigned lwm, unsigned hwm);
3870 : :
3871 : : private:
3872 : : void write_init_maps ();
3873 : : range_t write_prepare_maps (module_state_config *, bool);
3874 : : bool read_prepare_maps (const module_state_config *);
3875 : :
3876 : : void write_ordinary_maps (elf_out *to, range_t &,
3877 : : bool, unsigned *crc_ptr);
3878 : : bool read_ordinary_maps (line_map_uint_t, unsigned);
3879 : : void write_macro_maps (elf_out *to, range_t &, unsigned *crc_ptr);
3880 : : bool read_macro_maps (line_map_uint_t);
3881 : :
3882 : : private:
3883 : : void write_define (bytes_out &, const cpp_macro *);
3884 : : cpp_macro *read_define (bytes_in &, cpp_reader *) const;
3885 : : vec<cpp_hashnode *> *prepare_macros (cpp_reader *);
3886 : : unsigned write_macros (elf_out *to, vec<cpp_hashnode *> *, unsigned *crc_ptr);
3887 : : bool read_macros ();
3888 : : void install_macros ();
3889 : :
3890 : : public:
3891 : : void import_macros ();
3892 : :
3893 : : public:
3894 : : static void undef_macro (cpp_reader *, location_t, cpp_hashnode *);
3895 : : static cpp_macro *deferred_macro (cpp_reader *, location_t, cpp_hashnode *);
3896 : :
3897 : : public:
3898 : : static bool note_location (location_t);
3899 : : static void write_location (bytes_out &, location_t);
3900 : : location_t read_location (bytes_in &) const;
3901 : :
3902 : : public:
3903 : : void set_flatname ();
3904 : 40501 : const char *get_flatname () const
3905 : : {
3906 : 40501 : return flatname;
3907 : : }
3908 : : location_t imported_from () const;
3909 : :
3910 : : public:
3911 : : void set_filename (const Cody::Packet &);
3912 : : bool do_import (cpp_reader *, bool outermost);
3913 : : };
3914 : :
3915 : : /* Hash module state by name. This cannot be a member of
3916 : : module_state, because of GTY restrictions. We never delete from
3917 : : the hash table, but ggc_ptr_hash doesn't support that
3918 : : simplification. */
3919 : :
3920 : : struct module_state_hash : ggc_ptr_hash<module_state> {
3921 : : typedef std::pair<tree,uintptr_t> compare_type; /* {name,parent} */
3922 : :
3923 : : static inline hashval_t hash (const value_type m);
3924 : : static inline hashval_t hash (const compare_type &n);
3925 : : static inline bool equal (const value_type existing,
3926 : : const compare_type &candidate);
3927 : : };
3928 : :
3929 : 9806 : module_state::module_state (tree name, module_state *parent, bool partition)
3930 : 9806 : : imports (BITMAP_GGC_ALLOC ()), exports (BITMAP_GGC_ALLOC ()),
3931 : 9806 : parent (parent), name (name), slurp (NULL),
3932 : 9806 : flatname (NULL), filename (NULL),
3933 : 9806 : entity_lwm (~0u >> 1), entity_num (0),
3934 : 9806 : ordinary_locs (0, 0), macro_locs (0, 0),
3935 : 9806 : loc (UNKNOWN_LOCATION),
3936 : 9806 : crc (0), mod (MODULE_UNKNOWN), remap (0), subst (0)
3937 : : {
3938 : 9806 : loadedness = ML_NONE;
3939 : :
3940 : 9806 : module_p = header_p = interface_p = partition_p = false;
3941 : :
3942 : 9806 : directness = MD_NONE;
3943 : 9806 : exported_p = false;
3944 : :
3945 : 9806 : cmi_noted_p = false;
3946 : 9806 : active_init_p = false;
3947 : :
3948 : 9806 : partition_p = partition;
3949 : :
3950 : 9806 : inform_cmi_p = false;
3951 : 9806 : visited_p = false;
3952 : :
3953 : 9806 : extensions = 0;
3954 : 9806 : if (name && TREE_CODE (name) == STRING_CST)
3955 : : {
3956 : 1787 : header_p = true;
3957 : :
3958 : 1787 : const char *string = TREE_STRING_POINTER (name);
3959 : 1787 : gcc_checking_assert (string[0] == '.'
3960 : : ? IS_DIR_SEPARATOR (string[1])
3961 : : : IS_ABSOLUTE_PATH (string));
3962 : : }
3963 : :
3964 : 9806 : gcc_checking_assert (!(parent && header_p));
3965 : 9806 : }
3966 : :
3967 : 50 : module_state::~module_state ()
3968 : : {
3969 : 50 : release ();
3970 : 50 : }
3971 : :
3972 : : /* Hash module state. */
3973 : : static hashval_t
3974 : 13975 : module_name_hash (const_tree name)
3975 : : {
3976 : 13975 : if (TREE_CODE (name) == STRING_CST)
3977 : 3299 : return htab_hash_string (TREE_STRING_POINTER (name));
3978 : : else
3979 : 10676 : return IDENTIFIER_HASH_VALUE (name);
3980 : : }
3981 : :
3982 : : hashval_t
3983 : 3663 : module_state_hash::hash (const value_type m)
3984 : : {
3985 : 3663 : hashval_t ph = pointer_hash<void>::hash
3986 : 3663 : (reinterpret_cast<void *> (reinterpret_cast<uintptr_t> (m->parent)
3987 : 3663 : | m->is_partition ()));
3988 : 3663 : hashval_t nh = module_name_hash (m->name);
3989 : 3663 : return iterative_hash_hashval_t (ph, nh);
3990 : : }
3991 : :
3992 : : /* Hash a name. */
3993 : : hashval_t
3994 : 10312 : module_state_hash::hash (const compare_type &c)
3995 : : {
3996 : 10312 : hashval_t ph = pointer_hash<void>::hash (reinterpret_cast<void *> (c.second));
3997 : 10312 : hashval_t nh = module_name_hash (c.first);
3998 : :
3999 : 10312 : return iterative_hash_hashval_t (ph, nh);
4000 : : }
4001 : :
4002 : : bool
4003 : 6902 : module_state_hash::equal (const value_type existing,
4004 : : const compare_type &candidate)
4005 : : {
4006 : 6902 : uintptr_t ep = (reinterpret_cast<uintptr_t> (existing->parent)
4007 : 6902 : | existing->is_partition ());
4008 : 6902 : if (ep != candidate.second)
4009 : : return false;
4010 : :
4011 : : /* Identifier comparison is by pointer. If the string_csts happen
4012 : : to be the same object, then they're equal too. */
4013 : 5731 : if (existing->name == candidate.first)
4014 : : return true;
4015 : :
4016 : : /* If neither are string csts, they can't be equal. */
4017 : 1201 : if (TREE_CODE (candidate.first) != STRING_CST
4018 : 481 : || TREE_CODE (existing->name) != STRING_CST)
4019 : : return false;
4020 : :
4021 : : /* String equality. */
4022 : 417 : if (TREE_STRING_LENGTH (existing->name)
4023 : 417 : == TREE_STRING_LENGTH (candidate.first)
4024 : 417 : && !memcmp (TREE_STRING_POINTER (existing->name),
4025 : 414 : TREE_STRING_POINTER (candidate.first),
4026 : 414 : TREE_STRING_LENGTH (existing->name)))
4027 : 131 : return true;
4028 : :
4029 : : return false;
4030 : : }
4031 : :
4032 : : /********************************************************************/
4033 : : /* Global state */
4034 : :
4035 : : /* Mapper name. */
4036 : : static const char *module_mapper_name;
4037 : :
4038 : : /* Deferred import queue (FIFO). */
4039 : : static vec<module_state *, va_heap, vl_embed> *pending_imports;
4040 : :
4041 : : /* CMI repository path and workspace. */
4042 : : static char *cmi_repo;
4043 : : static size_t cmi_repo_length;
4044 : : static char *cmi_path;
4045 : : static size_t cmi_path_alloc;
4046 : :
4047 : : /* Count of available and loaded clusters. */
4048 : : static unsigned available_clusters;
4049 : : static unsigned loaded_clusters;
4050 : :
4051 : : /* What the current TU is. */
4052 : : unsigned module_kind;
4053 : :
4054 : : /* Global trees. */
4055 : : static const std::pair<tree *, unsigned> global_tree_arys[] =
4056 : : {
4057 : : std::pair<tree *, unsigned> (sizetype_tab, stk_type_kind_last),
4058 : : std::pair<tree *, unsigned> (integer_types, itk_none),
4059 : : std::pair<tree *, unsigned> (global_trees, TI_MODULE_HWM),
4060 : : std::pair<tree *, unsigned> (c_global_trees, CTI_MODULE_HWM),
4061 : : std::pair<tree *, unsigned> (cp_global_trees, CPTI_MODULE_HWM),
4062 : : std::pair<tree *, unsigned> (NULL, 0)
4063 : : };
4064 : : static GTY(()) vec<tree, va_gc> *fixed_trees;
4065 : : static unsigned global_crc;
4066 : :
4067 : : /* Lazy loading can open many files concurrently, there are
4068 : : per-process limits on that. We pay attention to the process limit,
4069 : : and attempt to increase it when we run out. Otherwise we use an
4070 : : LRU scheme to figure out who to flush. Note that if the import
4071 : : graph /depth/ exceeds lazy_limit, we'll exceed the limit. */
4072 : : static unsigned lazy_lru; /* LRU counter. */
4073 : : static unsigned lazy_open; /* Number of open modules */
4074 : : static unsigned lazy_limit; /* Current limit of open modules. */
4075 : : static unsigned lazy_hard_limit; /* Hard limit on open modules. */
4076 : : /* Account for source, assembler and dump files & directory searches.
4077 : : We don't keep the source file's open, so we don't have to account
4078 : : for #include depth. I think dump files are opened and closed per
4079 : : pass, but ICBW. */
4080 : : #define LAZY_HEADROOM 15 /* File descriptor headroom. */
4081 : :
4082 : : /* Vector of module state. Indexed by OWNER. Index 0 is reserved for the
4083 : : current TU; imports start at 1. */
4084 : : static GTY(()) vec<module_state *, va_gc> *modules;
4085 : :
4086 : : /* Hash of module state, findable by {name, parent}. */
4087 : : static GTY(()) hash_table<module_state_hash> *modules_hash;
4088 : :
4089 : : /* Map of imported entities. We map DECL_UID to index of entity
4090 : : vector. */
4091 : : typedef hash_map<unsigned/*UID*/, unsigned/*index*/,
4092 : : simple_hashmap_traits<int_hash<unsigned,0>, unsigned>
4093 : : > entity_map_t;
4094 : : static entity_map_t *entity_map;
4095 : : /* Doesn't need GTYing, because any tree referenced here is also
4096 : : findable by, symbol table, specialization table, return type of
4097 : : reachable function. */
4098 : : static vec<binding_slot, va_heap, vl_embed> *entity_ary;
4099 : :
4100 : : /* Members entities of imported classes that are defined in this TU.
4101 : : These are where the entity's context is not from the current TU.
4102 : : We need to emit the definition (but not the enclosing class).
4103 : :
4104 : : We could find these by walking ALL the imported classes that we
4105 : : could provide a member definition. But that's expensive,
4106 : : especially when you consider lazy implicit member declarations,
4107 : : which could be ANY imported class. */
4108 : : static GTY(()) vec<tree, va_gc> *class_members;
4109 : :
4110 : : /* The same problem exists for class template partial
4111 : : specializations. Now that we have constraints, the invariant of
4112 : : expecting them in the instantiation table no longer holds. One of
4113 : : the constrained partial specializations will be there, but the
4114 : : others not so much. It's not even an unconstrained partial
4115 : : spacialization in the table :( so any partial template declaration
4116 : : is added to this list too. */
4117 : : static GTY(()) vec<tree, va_gc> *partial_specializations;
4118 : :
4119 : : /********************************************************************/
4120 : :
4121 : : /* Our module mapper (created lazily). */
4122 : : module_client *mapper;
4123 : :
4124 : : static module_client *make_mapper (location_t loc, class mkdeps *deps);
4125 : 29618 : inline module_client *get_mapper (location_t loc, class mkdeps *deps)
4126 : : {
4127 : 29618 : auto *res = mapper;
4128 : 270 : if (!res)
4129 : 4155 : res = make_mapper (loc, deps);
4130 : 29618 : return res;
4131 : : }
4132 : :
4133 : : /********************************************************************/
4134 : : static tree
4135 : 247206 : get_clone_target (tree decl)
4136 : : {
4137 : 247206 : tree target;
4138 : :
4139 : 247206 : if (TREE_CODE (decl) == TEMPLATE_DECL)
4140 : : {
4141 : 25956 : tree res_orig = DECL_CLONED_FUNCTION (DECL_TEMPLATE_RESULT (decl));
4142 : :
4143 : 25956 : target = DECL_TI_TEMPLATE (res_orig);
4144 : : }
4145 : : else
4146 : 221250 : target = DECL_CLONED_FUNCTION (decl);
4147 : :
4148 : 247206 : gcc_checking_assert (DECL_MAYBE_IN_CHARGE_CDTOR_P (target));
4149 : :
4150 : 247206 : return target;
4151 : : }
4152 : :
4153 : : /* Like FOR_EACH_CLONE, but will walk cloned templates. */
4154 : : #define FOR_EVERY_CLONE(CLONE, FN) \
4155 : : if (!DECL_MAYBE_IN_CHARGE_CDTOR_P (FN)); \
4156 : : else \
4157 : : for (CLONE = DECL_CHAIN (FN); \
4158 : : CLONE && DECL_CLONED_FUNCTION_P (CLONE); \
4159 : : CLONE = DECL_CHAIN (CLONE))
4160 : :
4161 : : /* It'd be nice if USE_TEMPLATE was a field of template_info
4162 : : (a) it'd solve the enum case dealt with below,
4163 : : (b) both class templates and decl templates would store this in the
4164 : : same place
4165 : : (c) this function wouldn't need the by-ref arg, which is annoying. */
4166 : :
4167 : : static tree
4168 : 171864324 : node_template_info (tree decl, int &use)
4169 : : {
4170 : 171864324 : tree ti = NULL_TREE;
4171 : 171864324 : int use_tpl = -1;
4172 : 171864324 : if (DECL_IMPLICIT_TYPEDEF_P (decl))
4173 : : {
4174 : 18406368 : tree type = TREE_TYPE (decl);
4175 : :
4176 : 18406368 : ti = TYPE_TEMPLATE_INFO (type);
4177 : 18406368 : if (ti)
4178 : : {
4179 : 3177274 : if (TYPE_LANG_SPECIFIC (type))
4180 : 3171933 : use_tpl = CLASSTYPE_USE_TEMPLATE (type);
4181 : : else
4182 : : {
4183 : : /* An enum, where we don't explicitly encode use_tpl.
4184 : : If the containing context (a type or a function), is
4185 : : an ({im,ex}plicit) instantiation, then this is too.
4186 : : If it's a partial or explicit specialization, then
4187 : : this is not!. */
4188 : 5341 : tree ctx = CP_DECL_CONTEXT (decl);
4189 : 5341 : if (TYPE_P (ctx))
4190 : 5231 : ctx = TYPE_NAME (ctx);
4191 : 5341 : node_template_info (ctx, use);
4192 : 5341 : use_tpl = use != 2 ? use : 0;
4193 : : }
4194 : : }
4195 : : }
4196 : 153457956 : else if (DECL_LANG_SPECIFIC (decl)
4197 : 153457956 : && (VAR_P (decl)
4198 : : || TREE_CODE (decl) == TYPE_DECL
4199 : : || TREE_CODE (decl) == FUNCTION_DECL
4200 : : || TREE_CODE (decl) == FIELD_DECL
4201 : : || TREE_CODE (decl) == CONCEPT_DECL
4202 : : || TREE_CODE (decl) == TEMPLATE_DECL))
4203 : : {
4204 : 81088621 : use_tpl = DECL_USE_TEMPLATE (decl);
4205 : 81088621 : ti = DECL_TEMPLATE_INFO (decl);
4206 : : }
4207 : :
4208 : 171864324 : use = use_tpl;
4209 : 171864324 : return ti;
4210 : : }
4211 : :
4212 : : /* Find the index in entity_ary for an imported DECL. It should
4213 : : always be there, but bugs can cause it to be missing, and that can
4214 : : crash the crash reporting -- let's not do that! When streaming
4215 : : out we place entities from this module there too -- with negated
4216 : : indices. */
4217 : :
4218 : : static unsigned
4219 : 1003406 : import_entity_index (tree decl, bool null_ok = false)
4220 : : {
4221 : 1003406 : if (unsigned *slot = entity_map->get (DECL_UID (decl)))
4222 : 1003361 : return *slot;
4223 : :
4224 : 45 : gcc_checking_assert (null_ok);
4225 : : return ~(~0u >> 1);
4226 : : }
4227 : :
4228 : : /* Find the module for an imported entity at INDEX in the entity ary.
4229 : : There must be one. */
4230 : :
4231 : : static module_state *
4232 : 36730 : import_entity_module (unsigned index)
4233 : : {
4234 : 36730 : if (index > ~(~0u >> 1))
4235 : : /* This is an index for an exported entity. */
4236 : 18 : return (*modules)[0];
4237 : :
4238 : : /* Do not include the current TU (not an off-by-one error). */
4239 : 36712 : unsigned pos = 1;
4240 : 36712 : unsigned len = modules->length () - pos;
4241 : 80602 : while (len)
4242 : : {
4243 : 43890 : unsigned half = len / 2;
4244 : 43890 : module_state *probe = (*modules)[pos + half];
4245 : 43890 : if (index < probe->entity_lwm)
4246 : : len = half;
4247 : 36830 : else if (index < probe->entity_lwm + probe->entity_num)
4248 : : return probe;
4249 : : else
4250 : : {
4251 : 118 : pos += half + 1;
4252 : 118 : len = len - (half + 1);
4253 : : }
4254 : : }
4255 : 0 : gcc_unreachable ();
4256 : : }
4257 : :
4258 : :
4259 : : /********************************************************************/
4260 : : /* A dumping machinery. */
4261 : :
4262 : : class dumper {
4263 : : public:
4264 : : enum {
4265 : : LOCATION = TDF_LINENO, /* -lineno:Source location streaming. */
4266 : : DEPEND = TDF_GRAPH, /* -graph:Dependency graph construction. */
4267 : : CLUSTER = TDF_BLOCKS, /* -blocks:Clusters. */
4268 : : TREE = TDF_UID, /* -uid:Tree streaming. */
4269 : : MERGE = TDF_ALIAS, /* -alias:Mergeable Entities. */
4270 : : ELF = TDF_ASMNAME, /* -asmname:Elf data. */
4271 : : MACRO = TDF_VOPS /* -vops:Macros. */
4272 : : };
4273 : :
4274 : : private:
4275 : : struct impl {
4276 : : typedef vec<module_state *, va_heap, vl_embed> stack_t;
4277 : :
4278 : : FILE *stream; /* Dump stream. */
4279 : : unsigned indent; /* Local indentation. */
4280 : : bool bol; /* Beginning of line. */
4281 : : stack_t stack; /* Trailing array of module_state. */
4282 : :
4283 : : bool nested_name (tree); /* Dump a name following DECL_CONTEXT. */
4284 : : };
4285 : :
4286 : : public:
4287 : : /* The dumper. */
4288 : : impl *dumps;
4289 : : dump_flags_t flags;
4290 : :
4291 : : public:
4292 : : /* Push/pop module state dumping. */
4293 : : unsigned push (module_state *);
4294 : : void pop (unsigned);
4295 : :
4296 : : public:
4297 : : /* Change local indentation. */
4298 : 266767996 : void indent ()
4299 : : {
4300 : 282 : if (dumps)
4301 : 492535 : dumps->indent++;
4302 : : }
4303 : 266767996 : void outdent ()
4304 : : {
4305 : 266767996 : if (dumps)
4306 : : {
4307 : 492535 : gcc_checking_assert (dumps->indent);
4308 : 492535 : dumps->indent--;
4309 : : }
4310 : 266767996 : }
4311 : :
4312 : : public:
4313 : : /* Is dump enabled?. */
4314 : 163283989 : bool operator () (int mask = 0)
4315 : : {
4316 : 3390108 : if (!dumps || !dumps->stream)
4317 : : return false;
4318 : 364195 : if (mask && !(mask & flags))
4319 : 3582 : return false;
4320 : : return true;
4321 : : }
4322 : : /* Dump some information. */
4323 : : bool operator () (const char *, ...);
4324 : : };
4325 : :
4326 : : /* The dumper. */
4327 : : static dumper dump = {0, dump_flags_t (0)};
4328 : :
4329 : : /* Push to dumping M. Return previous indentation level. */
4330 : :
4331 : : unsigned
4332 : 86196 : dumper::push (module_state *m)
4333 : : {
4334 : 86196 : FILE *stream = NULL;
4335 : 86196 : if (!dumps || !dumps->stack.length ())
4336 : : {
4337 : 85101 : stream = dump_begin (module_dump_id, &flags);
4338 : 85101 : if (!stream)
4339 : : return 0;
4340 : : }
4341 : :
4342 : 6609 : if (!dumps || !dumps->stack.space (1))
4343 : : {
4344 : : /* Create or extend the dump implementor. */
4345 : 1070 : unsigned current = dumps ? dumps->stack.length () : 0;
4346 : 562 : unsigned count = current ? current * 2 : EXPERIMENT (1, 20);
4347 : 1070 : size_t alloc = (offsetof (impl, stack)
4348 : 1070 : + impl::stack_t::embedded_size (count));
4349 : 1070 : dumps = XRESIZEVAR (impl, dumps, alloc);
4350 : 1070 : dumps->stack.embedded_init (count, current);
4351 : : }
4352 : 6609 : if (stream)
4353 : 5514 : dumps->stream = stream;
4354 : :
4355 : 6609 : unsigned n = dumps->indent;
4356 : 6609 : dumps->indent = 0;
4357 : 6609 : dumps->bol = true;
4358 : 6609 : dumps->stack.quick_push (m);
4359 : 6609 : if (m)
4360 : : {
4361 : 1751 : module_state *from = NULL;
4362 : :
4363 : 1751 : if (dumps->stack.length () > 1)
4364 : 551 : from = dumps->stack[dumps->stack.length () - 2];
4365 : : else
4366 : 1200 : dump ("");
4367 : 3187 : dump (from ? "Starting module %M (from %M)"
4368 : : : "Starting module %M", m, from);
4369 : : }
4370 : :
4371 : : return n;
4372 : : }
4373 : :
4374 : : /* Pop from dumping. Restore indentation to N. */
4375 : :
4376 : 86156 : void dumper::pop (unsigned n)
4377 : : {
4378 : 86156 : if (!dumps)
4379 : : return;
4380 : :
4381 : 13218 : gcc_checking_assert (dump () && !dumps->indent);
4382 : 6609 : if (module_state *m = dumps->stack[dumps->stack.length () - 1])
4383 : : {
4384 : 1751 : module_state *from = (dumps->stack.length () > 1
4385 : 1751 : ? dumps->stack[dumps->stack.length () - 2] : NULL);
4386 : 1987 : dump (from ? "Finishing module %M (returning to %M)"
4387 : : : "Finishing module %M", m, from);
4388 : : }
4389 : 6609 : dumps->stack.pop ();
4390 : 6609 : dumps->indent = n;
4391 : 6609 : if (!dumps->stack.length ())
4392 : : {
4393 : 5514 : dump_end (module_dump_id, dumps->stream);
4394 : 5514 : dumps->stream = NULL;
4395 : : }
4396 : : }
4397 : :
4398 : : /* Dump a nested name for arbitrary tree T. Sometimes it won't have a
4399 : : name. */
4400 : :
4401 : : bool
4402 : 461888 : dumper::impl::nested_name (tree t)
4403 : : {
4404 : 461888 : tree ti = NULL_TREE;
4405 : 461888 : int origin = -1;
4406 : 461888 : tree name = NULL_TREE;
4407 : :
4408 : 461888 : if (t && TREE_CODE (t) == TU_LOCAL_ENTITY)
4409 : 0 : t = TU_LOCAL_ENTITY_NAME (t);
4410 : :
4411 : 461864 : if (t && TREE_CODE (t) == TREE_BINFO)
4412 : 378 : t = BINFO_TYPE (t);
4413 : :
4414 : 461888 : if (t && TYPE_P (t))
4415 : 231119 : t = TYPE_NAME (t);
4416 : :
4417 : 461852 : if (t && DECL_P (t))
4418 : : {
4419 : 390813 : if (t == global_namespace || DECL_TEMPLATE_PARM_P (t))
4420 : : ;
4421 : 360215 : else if (tree ctx = DECL_CONTEXT (t))
4422 : 275032 : if (TREE_CODE (ctx) == TRANSLATION_UNIT_DECL
4423 : 275032 : || nested_name (ctx))
4424 : 275032 : fputs ("::", stream);
4425 : :
4426 : 390813 : int use_tpl;
4427 : 390813 : ti = node_template_info (t, use_tpl);
4428 : 127055 : if (ti && TREE_CODE (TI_TEMPLATE (ti)) == TEMPLATE_DECL
4429 : 517826 : && (DECL_TEMPLATE_RESULT (TI_TEMPLATE (ti)) == t))
4430 : : t = TI_TEMPLATE (ti);
4431 : 390813 : tree not_tmpl = t;
4432 : 390813 : if (TREE_CODE (t) == TEMPLATE_DECL)
4433 : : {
4434 : 21870 : fputs ("template ", stream);
4435 : 21870 : not_tmpl = DECL_TEMPLATE_RESULT (t);
4436 : : }
4437 : :
4438 : 21870 : if (not_tmpl
4439 : 390809 : && DECL_P (not_tmpl)
4440 : 390809 : && DECL_LANG_SPECIFIC (not_tmpl)
4441 : 227172 : && DECL_MODULE_IMPORT_P (not_tmpl))
4442 : : {
4443 : : /* We need to be careful here, so as to not explode on
4444 : : inconsistent data -- we're probably debugging, because
4445 : : Something Is Wrong. */
4446 : 16620 : unsigned index = import_entity_index (t, true);
4447 : 16620 : if (!(index & ~(~0u >> 1)))
4448 : 16062 : origin = import_entity_module (index)->mod;
4449 : 558 : else if (index > ~(~0u >> 1))
4450 : : /* An imported partition member that we're emitting. */
4451 : : origin = 0;
4452 : : else
4453 : 45 : origin = -2;
4454 : : }
4455 : :
4456 : 390813 : name = DECL_NAME (t) ? DECL_NAME (t)
4457 : 3985 : : HAS_DECL_ASSEMBLER_NAME_P (t) ? DECL_ASSEMBLER_NAME_RAW (t)
4458 : : : NULL_TREE;
4459 : : }
4460 : : else
4461 : : name = t;
4462 : :
4463 : 390813 : if (name)
4464 : 431864 : switch (TREE_CODE (name))
4465 : : {
4466 : 12451 : default:
4467 : 12451 : fputs ("#unnamed#", stream);
4468 : 12451 : break;
4469 : :
4470 : 400112 : case IDENTIFIER_NODE:
4471 : 400112 : fwrite (IDENTIFIER_POINTER (name), 1, IDENTIFIER_LENGTH (name), stream);
4472 : 400112 : break;
4473 : :
4474 : 19226 : case INTEGER_CST:
4475 : 19226 : print_hex (wi::to_wide (name), stream);
4476 : 19226 : break;
4477 : :
4478 : 75 : case STRING_CST:
4479 : : /* If TREE_TYPE is NULL, this is a raw string. */
4480 : 150 : fwrite (TREE_STRING_POINTER (name), 1,
4481 : 75 : TREE_STRING_LENGTH (name) - (TREE_TYPE (name) != NULL_TREE),
4482 : : stream);
4483 : 75 : break;
4484 : : }
4485 : : else
4486 : 30024 : fputs ("#null#", stream);
4487 : :
4488 : 461888 : if (origin >= 0)
4489 : : {
4490 : 16575 : const module_state *module = (*modules)[origin];
4491 : 33150 : fprintf (stream, "@%s:%d", !module ? "" : !module->name ? "(unnamed)"
4492 : 16575 : : module->get_flatname (), origin);
4493 : : }
4494 : 445313 : else if (origin == -2)
4495 : 45 : fprintf (stream, "@???");
4496 : :
4497 : 461888 : if (ti)
4498 : : {
4499 : 127055 : tree args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (ti));
4500 : 127055 : fputs ("<", stream);
4501 : 127055 : if (args)
4502 : 319226 : for (int ix = 0; ix != TREE_VEC_LENGTH (args); ix++)
4503 : : {
4504 : 192171 : if (ix)
4505 : 65116 : fputs (",", stream);
4506 : 192171 : nested_name (TREE_VEC_ELT (args, ix));
4507 : : }
4508 : 127055 : fputs (">", stream);
4509 : : }
4510 : :
4511 : 461888 : return true;
4512 : : }
4513 : :
4514 : : /* Formatted dumping. FORMAT begins with '+' do not emit a trailing
4515 : : new line. (Normally it is appended.)
4516 : : Escapes:
4517 : : %C - tree_code
4518 : : %I - identifier
4519 : : %K - location_t or line_map_uint_t
4520 : : %M - module_state
4521 : : %N - name -- DECL_NAME
4522 : : %P - context:name pair
4523 : : %R - unsigned:unsigned ratio
4524 : : %S - symbol -- DECL_ASSEMBLER_NAME
4525 : : %U - long unsigned
4526 : : %V - version
4527 : : --- the following are printf-like, but without its flexibility
4528 : : %d - decimal int
4529 : : %p - pointer
4530 : : %s - string
4531 : : %u - unsigned int
4532 : : %x - hex int
4533 : :
4534 : : We do not implement the printf modifiers. */
4535 : :
4536 : : bool
4537 : 384357 : dumper::operator () (const char *format, ...)
4538 : : {
4539 : 384357 : if (!(*this) ())
4540 : : return false;
4541 : :
4542 : 330872 : bool no_nl = format[0] == '+';
4543 : 330872 : format += no_nl;
4544 : :
4545 : 330872 : if (dumps->bol)
4546 : : {
4547 : : /* Module import indent. */
4548 : 169836 : if (unsigned depth = dumps->stack.length () - 1)
4549 : : {
4550 : 18829 : const char *prefix = ">>>>";
4551 : 37658 : fprintf (dumps->stream, (depth <= strlen (prefix)
4552 : 18829 : ? &prefix[strlen (prefix) - depth]
4553 : : : ">.%d.>"), depth);
4554 : : }
4555 : :
4556 : : /* Local indent. */
4557 : 169836 : if (unsigned indent = dumps->indent)
4558 : : {
4559 : 89890 : const char *prefix = " ";
4560 : 176362 : fprintf (dumps->stream, (indent <= strlen (prefix)
4561 : 86472 : ? &prefix[strlen (prefix) - indent]
4562 : : : " .%d. "), indent);
4563 : : }
4564 : 169836 : dumps->bol = false;
4565 : : }
4566 : :
4567 : 330872 : va_list args;
4568 : 330872 : va_start (args, format);
4569 : 974800 : while (const char *esc = strchr (format, '%'))
4570 : : {
4571 : 643928 : fwrite (format, 1, (size_t)(esc - format), dumps->stream);
4572 : 643928 : format = ++esc;
4573 : 643928 : switch (*format++)
4574 : : {
4575 : 0 : default:
4576 : 0 : gcc_unreachable ();
4577 : :
4578 : 508 : case '%':
4579 : 508 : fputc ('%', dumps->stream);
4580 : 508 : break;
4581 : :
4582 : 100528 : case 'C': /* Code */
4583 : 100528 : {
4584 : 100528 : tree_code code = (tree_code)va_arg (args, unsigned);
4585 : 100528 : fputs (get_tree_code_name (code), dumps->stream);
4586 : : }
4587 : 100528 : break;
4588 : :
4589 : 81 : case 'I': /* Identifier. */
4590 : 81 : {
4591 : 81 : tree t = va_arg (args, tree);
4592 : 81 : dumps->nested_name (t);
4593 : : }
4594 : 81 : break;
4595 : :
4596 : 4009 : case 'K': /* location_t, either 32- or 64-bit. */
4597 : 4009 : {
4598 : 4009 : unsigned long long u = va_arg (args, location_t);
4599 : 4009 : fprintf (dumps->stream, "%llu", u);
4600 : : }
4601 : 4009 : break;
4602 : :
4603 : 6726 : case 'M': /* Module. */
4604 : 6726 : {
4605 : 6726 : const char *str = "(none)";
4606 : 6726 : if (module_state *m = va_arg (args, module_state *))
4607 : : {
4608 : 6726 : if (!m->has_location ())
4609 : : str = "(detached)";
4610 : : else
4611 : 6726 : str = m->get_flatname ();
4612 : : }
4613 : 6726 : fputs (str, dumps->stream);
4614 : : }
4615 : 6726 : break;
4616 : :
4617 : 110345 : case 'N': /* Name. */
4618 : 110345 : {
4619 : 110345 : tree t = va_arg (args, tree);
4620 : 221146 : while (t && TREE_CODE (t) == OVERLOAD)
4621 : 456 : t = OVL_FUNCTION (t);
4622 : 110345 : fputc ('\'', dumps->stream);
4623 : 110345 : dumps->nested_name (t);
4624 : 110345 : fputc ('\'', dumps->stream);
4625 : : }
4626 : 110345 : break;
4627 : :
4628 : 6399 : case 'P': /* Pair. */
4629 : 6399 : {
4630 : 6399 : tree ctx = va_arg (args, tree);
4631 : 6399 : tree name = va_arg (args, tree);
4632 : 6399 : fputc ('\'', dumps->stream);
4633 : 6399 : dumps->nested_name (ctx);
4634 : 6399 : if (ctx && ctx != global_namespace)
4635 : 720 : fputs ("::", dumps->stream);
4636 : 6399 : dumps->nested_name (name);
4637 : 6399 : fputc ('\'', dumps->stream);
4638 : : }
4639 : 6399 : break;
4640 : :
4641 : 744 : case 'R': /* Ratio */
4642 : 744 : {
4643 : 744 : unsigned a = va_arg (args, unsigned);
4644 : 744 : unsigned b = va_arg (args, unsigned);
4645 : 744 : fprintf (dumps->stream, "%.1f", (float) a / (b + !b));
4646 : : }
4647 : 744 : break;
4648 : :
4649 : 32850 : case 'S': /* Symbol name */
4650 : 32850 : {
4651 : 32850 : tree t = va_arg (args, tree);
4652 : 32850 : if (t && TYPE_P (t))
4653 : 11772 : t = TYPE_NAME (t);
4654 : 31416 : if (t && HAS_DECL_ASSEMBLER_NAME_P (t)
4655 : 29985 : && DECL_ASSEMBLER_NAME_SET_P (t))
4656 : : {
4657 : 165 : fputc ('(', dumps->stream);
4658 : 165 : fputs (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (t)),
4659 : 165 : dumps->stream);
4660 : 165 : fputc (')', dumps->stream);
4661 : : }
4662 : : }
4663 : : break;
4664 : :
4665 : 0 : case 'U': /* long unsigned. */
4666 : 0 : {
4667 : 0 : unsigned long u = va_arg (args, unsigned long);
4668 : 0 : fprintf (dumps->stream, "%lu", u);
4669 : : }
4670 : 0 : break;
4671 : :
4672 : 709 : case 'V': /* Version. */
4673 : 709 : {
4674 : 709 : unsigned v = va_arg (args, unsigned);
4675 : 709 : verstr_t string;
4676 : :
4677 : 709 : version2string (v, string);
4678 : 709 : fputs (string, dumps->stream);
4679 : : }
4680 : 709 : break;
4681 : :
4682 : 0 : case 'c': /* Character. */
4683 : 0 : {
4684 : 0 : int c = va_arg (args, int);
4685 : 0 : fputc (c, dumps->stream);
4686 : : }
4687 : 0 : break;
4688 : :
4689 : 57035 : case 'd': /* Decimal Int. */
4690 : 57035 : {
4691 : 57035 : int d = va_arg (args, int);
4692 : 57035 : fprintf (dumps->stream, "%d", d);
4693 : : }
4694 : 57035 : break;
4695 : :
4696 : 0 : case 'p': /* Pointer. */
4697 : 0 : {
4698 : 0 : void *p = va_arg (args, void *);
4699 : 0 : fprintf (dumps->stream, "%p", p);
4700 : : }
4701 : 0 : break;
4702 : :
4703 : 110496 : case 's': /* String. */
4704 : 110496 : {
4705 : 110496 : const char *s = va_arg (args, char *);
4706 : 110496 : gcc_checking_assert (s);
4707 : 110496 : fputs (s, dumps->stream);
4708 : : }
4709 : 110496 : break;
4710 : :
4711 : 211222 : case 'u': /* Unsigned. */
4712 : 211222 : {
4713 : 211222 : unsigned u = va_arg (args, unsigned);
4714 : 211222 : fprintf (dumps->stream, "%u", u);
4715 : : }
4716 : 211222 : break;
4717 : :
4718 : 2276 : case 'x': /* Hex. */
4719 : 2276 : {
4720 : 2276 : unsigned x = va_arg (args, unsigned);
4721 : 2276 : fprintf (dumps->stream, "%x", x);
4722 : : }
4723 : 2276 : break;
4724 : : }
4725 : : }
4726 : 330872 : fputs (format, dumps->stream);
4727 : 330872 : va_end (args);
4728 : 330872 : if (!no_nl)
4729 : : {
4730 : 169836 : dumps->bol = true;
4731 : 169836 : fputc ('\n', dumps->stream);
4732 : : }
4733 : : return true;
4734 : : }
4735 : :
4736 : : struct note_def_cache_hasher : ggc_cache_ptr_hash<tree_node>
4737 : : {
4738 : 147570 : static int keep_cache_entry (tree t)
4739 : : {
4740 : 147570 : if (!CHECKING_P)
4741 : : /* GTY is unfortunately not clever enough to conditionalize
4742 : : this. */
4743 : : gcc_unreachable ();
4744 : :
4745 : 147570 : if (ggc_marked_p (t))
4746 : : return -1;
4747 : :
4748 : 0 : unsigned n = dump.push (NULL);
4749 : : /* This might or might not be an error. We should note its
4750 : : dropping whichever. */
4751 : 0 : dump () && dump ("Dropping %N from note_defs table", t);
4752 : 0 : dump.pop (n);
4753 : :
4754 : 0 : return 0;
4755 : : }
4756 : : };
4757 : :
4758 : : /* We should stream each definition at most once.
4759 : : This needs to be a cache because there are cases where a definition
4760 : : ends up being not retained, and we need to drop those so we don't
4761 : : get confused if memory is reallocated. */
4762 : : typedef hash_table<note_def_cache_hasher> note_defs_table_t;
4763 : : static GTY((cache)) note_defs_table_t *note_defs;
4764 : :
4765 : : void
4766 : 230683 : trees_in::assert_definition (tree decl ATTRIBUTE_UNUSED,
4767 : : bool installing ATTRIBUTE_UNUSED)
4768 : : {
4769 : : #if CHECKING_P
4770 : 230683 : tree *slot = note_defs->find_slot (decl, installing ? INSERT : NO_INSERT);
4771 : 230683 : tree not_tmpl = STRIP_TEMPLATE (decl);
4772 : 230683 : if (installing)
4773 : : {
4774 : : /* We must be inserting for the first time. */
4775 : 109598 : gcc_assert (!*slot);
4776 : 109598 : *slot = decl;
4777 : : }
4778 : : else
4779 : : /* If this is not the mergeable entity, it should not be in the
4780 : : table. If it is a non-global-module mergeable entity, it
4781 : : should be in the table. Global module entities could have been
4782 : : defined textually in the current TU and so might or might not
4783 : : be present. */
4784 : 121085 : gcc_assert (!is_duplicate (decl)
4785 : : ? !slot
4786 : : : (slot
4787 : : || !DECL_LANG_SPECIFIC (not_tmpl)
4788 : : || !DECL_MODULE_PURVIEW_P (not_tmpl)
4789 : : || (!DECL_MODULE_IMPORT_P (not_tmpl)
4790 : : && header_module_p ())));
4791 : :
4792 : 230683 : if (not_tmpl != decl)
4793 : 136171 : gcc_assert (!note_defs->find_slot (not_tmpl, NO_INSERT));
4794 : : #endif
4795 : 230683 : }
4796 : :
4797 : : void
4798 : 281586 : trees_out::assert_definition (tree decl ATTRIBUTE_UNUSED)
4799 : : {
4800 : : #if CHECKING_P
4801 : 281586 : tree *slot = note_defs->find_slot (decl, INSERT);
4802 : 281586 : gcc_assert (!*slot);
4803 : 281586 : *slot = decl;
4804 : 281586 : if (TREE_CODE (decl) == TEMPLATE_DECL)
4805 : 163921 : gcc_assert (!note_defs->find_slot (DECL_TEMPLATE_RESULT (decl), NO_INSERT));
4806 : : #endif
4807 : 281586 : }
4808 : :
4809 : : /********************************************************************/
4810 : : static bool
4811 : 10723 : noisy_p ()
4812 : : {
4813 : 0 : if (quiet_flag)
4814 : : return false;
4815 : :
4816 : 0 : pp_needs_newline (global_dc->get_reference_printer ()) = true;
4817 : 0 : diagnostic_set_last_function (global_dc, (diagnostic_info *) NULL);
4818 : :
4819 : 0 : return true;
4820 : : }
4821 : :
4822 : : /* Set the cmi repo. Strip trailing '/', '.' becomes NULL. */
4823 : :
4824 : : static void
4825 : 96791 : set_cmi_repo (const char *r)
4826 : : {
4827 : 96791 : XDELETEVEC (cmi_repo);
4828 : 96791 : XDELETEVEC (cmi_path);
4829 : 96791 : cmi_path_alloc = 0;
4830 : :
4831 : 96791 : cmi_repo = NULL;
4832 : 96791 : cmi_repo_length = 0;
4833 : :
4834 : 96791 : if (!r || !r[0])
4835 : : return;
4836 : :
4837 : 4152 : size_t len = strlen (r);
4838 : 4152 : cmi_repo = XNEWVEC (char, len + 1);
4839 : 4152 : memcpy (cmi_repo, r, len + 1);
4840 : :
4841 : 4152 : if (len > 1 && IS_DIR_SEPARATOR (cmi_repo[len-1]))
4842 : 4152 : len--;
4843 : 4152 : if (len == 1 && cmi_repo[0] == '.')
4844 : 21 : len--;
4845 : 4152 : cmi_repo[len] = 0;
4846 : 4152 : cmi_repo_length = len;
4847 : : }
4848 : :
4849 : : /* TO is a repo-relative name. Provide one that we may use from where
4850 : : we are. */
4851 : :
4852 : : static const char *
4853 : 5055 : maybe_add_cmi_prefix (const char *to, size_t *len_p = NULL)
4854 : : {
4855 : 5055 : size_t len = len_p || cmi_repo_length ? strlen (to) : 0;
4856 : :
4857 : 5055 : if (cmi_repo_length && !IS_ABSOLUTE_PATH (to))
4858 : : {
4859 : 5034 : if (cmi_path_alloc < cmi_repo_length + len + 2)
4860 : : {
4861 : 4068 : XDELETEVEC (cmi_path);
4862 : 4068 : cmi_path_alloc = cmi_repo_length + len * 2 + 2;
4863 : 4068 : cmi_path = XNEWVEC (char, cmi_path_alloc);
4864 : :
4865 : 4068 : memcpy (cmi_path, cmi_repo, cmi_repo_length);
4866 : 4068 : cmi_path[cmi_repo_length] = DIR_SEPARATOR;
4867 : : }
4868 : :
4869 : 5034 : memcpy (&cmi_path[cmi_repo_length + 1], to, len + 1);
4870 : 5034 : len += cmi_repo_length + 1;
4871 : 5034 : to = cmi_path;
4872 : : }
4873 : :
4874 : 5055 : if (len_p)
4875 : 2436 : *len_p = len;
4876 : :
4877 : 5055 : return to;
4878 : : }
4879 : :
4880 : : /* Try and create the directories of PATH. */
4881 : :
4882 : : static void
4883 : 41 : create_dirs (char *path)
4884 : : {
4885 : : /* Try and create the missing directories. */
4886 : 2538 : for (char *base = path; *base; base++)
4887 : 2497 : if (IS_DIR_SEPARATOR (*base))
4888 : : {
4889 : 249 : char sep = *base;
4890 : 249 : *base = 0;
4891 : 249 : int failed = mkdir (path, S_IRWXU | S_IRWXG | S_IRWXO);
4892 : 255 : dump () && dump ("Mkdir ('%s') errno:=%u", path, failed ? errno : 0);
4893 : 249 : *base = sep;
4894 : 249 : if (failed
4895 : : /* Maybe racing with another creator (of a *different*
4896 : : module). */
4897 : 49 : && errno != EEXIST)
4898 : : break;
4899 : : }
4900 : 41 : }
4901 : :
4902 : : /* Given a CLASSTYPE_DECL_LIST VALUE get the template friend decl,
4903 : : if that's what this is. */
4904 : :
4905 : : static tree
4906 : 50288 : friend_from_decl_list (tree frnd)
4907 : : {
4908 : 50288 : tree res = frnd;
4909 : :
4910 : 50288 : if (TREE_CODE (frnd) != TEMPLATE_DECL)
4911 : : {
4912 : 28027 : tree tmpl = NULL_TREE;
4913 : 28027 : if (TYPE_P (frnd))
4914 : : {
4915 : 4740 : res = TYPE_NAME (frnd);
4916 : 4656 : if (CLASS_TYPE_P (frnd)
4917 : 9396 : && CLASSTYPE_TEMPLATE_INFO (frnd))
4918 : 4647 : tmpl = CLASSTYPE_TI_TEMPLATE (frnd);
4919 : : }
4920 : 23287 : else if (DECL_TEMPLATE_INFO (frnd))
4921 : : {
4922 : 23287 : tmpl = DECL_TI_TEMPLATE (frnd);
4923 : 23287 : if (TREE_CODE (tmpl) != TEMPLATE_DECL)
4924 : : tmpl = NULL_TREE;
4925 : : }
4926 : :
4927 : 32185 : if (tmpl && DECL_TEMPLATE_RESULT (tmpl) == res)
4928 : : res = tmpl;
4929 : : }
4930 : :
4931 : 50288 : return res;
4932 : : }
4933 : :
4934 : : static tree
4935 : 15356 : find_enum_member (tree ctx, tree name)
4936 : : {
4937 : 15356 : for (tree values = TYPE_VALUES (ctx);
4938 : 228906 : values; values = TREE_CHAIN (values))
4939 : 224856 : if (DECL_NAME (TREE_VALUE (values)) == name)
4940 : : return TREE_VALUE (values);
4941 : :
4942 : : return NULL_TREE;
4943 : : }
4944 : :
4945 : : /********************************************************************/
4946 : : /* Instrumentation gathered writing bytes. */
4947 : :
4948 : : void
4949 : 248 : bytes_out::instrument ()
4950 : : {
4951 : 248 : dump ("Wrote %u bytes in %u blocks", lengths[3], spans[3]);
4952 : 248 : dump ("Wrote %u bits in %u bytes", lengths[0] + lengths[1], lengths[2]);
4953 : 744 : for (unsigned ix = 0; ix < 2; ix++)
4954 : 744 : dump (" %u %s spans of %R bits", spans[ix],
4955 : : ix ? "one" : "zero", lengths[ix], spans[ix]);
4956 : 248 : dump (" %u blocks with %R bits padding", spans[2],
4957 : 248 : lengths[2] * 8 - (lengths[0] + lengths[1]), spans[2]);
4958 : 248 : }
4959 : :
4960 : : /* Instrumentation gathered writing trees. */
4961 : : void
4962 : 2302 : trees_out::instrument ()
4963 : : {
4964 : 2302 : if (dump (""))
4965 : : {
4966 : 248 : bytes_out::instrument ();
4967 : 248 : dump ("Wrote:");
4968 : 248 : dump (" %u decl trees", decl_val_count);
4969 : 248 : dump (" %u other trees", tree_val_count);
4970 : 248 : dump (" %u back references", back_ref_count);
4971 : 248 : dump (" %u TU-local entities", tu_local_count);
4972 : 248 : dump (" %u null trees", null_count);
4973 : : }
4974 : 2302 : }
4975 : :
4976 : : /* Setup and teardown for a tree walk. */
4977 : :
4978 : : void
4979 : 1751700 : trees_out::begin ()
4980 : : {
4981 : 1751700 : gcc_assert (!streaming_p () || !tree_map.elements ());
4982 : :
4983 : 1751700 : mark_trees ();
4984 : 1751700 : if (streaming_p ())
4985 : 223385 : parent::begin ();
4986 : 1751700 : }
4987 : :
4988 : : unsigned
4989 : 223385 : trees_out::end (elf_out *sink, unsigned name, unsigned *crc_ptr)
4990 : : {
4991 : 223385 : gcc_checking_assert (streaming_p ());
4992 : :
4993 : 223385 : unmark_trees ();
4994 : 223385 : return parent::end (sink, name, crc_ptr);
4995 : : }
4996 : :
4997 : : void
4998 : 1528315 : trees_out::end ()
4999 : : {
5000 : 1528315 : gcc_assert (!streaming_p ());
5001 : :
5002 : 1528315 : unmark_trees ();
5003 : : /* Do not parent::end -- we weren't streaming. */
5004 : 1528315 : }
5005 : :
5006 : : void
5007 : 1751700 : trees_out::mark_trees ()
5008 : : {
5009 : 1751700 : if (size_t size = tree_map.elements ())
5010 : : {
5011 : : /* This isn't our first rodeo, destroy and recreate the
5012 : : tree_map. I'm a bad bad man. Use the previous size as a
5013 : : guess for the next one (so not all bad). */
5014 : 1312905 : tree_map.~ptr_int_hash_map ();
5015 : 1312905 : new (&tree_map) ptr_int_hash_map (size);
5016 : : }
5017 : :
5018 : : /* Install the fixed trees, with +ve references. */
5019 : 1751700 : unsigned limit = fixed_trees->length ();
5020 : 337871057 : for (unsigned ix = 0; ix != limit; ix++)
5021 : : {
5022 : 336119357 : tree val = (*fixed_trees)[ix];
5023 : 336119357 : bool existed = tree_map.put (val, ix + tag_fixed);
5024 : 336119357 : gcc_checking_assert (!TREE_VISITED (val) && !existed);
5025 : 336119357 : TREE_VISITED (val) = true;
5026 : : }
5027 : :
5028 : 1751700 : ref_num = 0;
5029 : 1751700 : }
5030 : :
5031 : : /* Unmark the trees we encountered */
5032 : :
5033 : : void
5034 : 1751700 : trees_out::unmark_trees ()
5035 : : {
5036 : 1751700 : ptr_int_hash_map::iterator end (tree_map.end ());
5037 : 396333361 : for (ptr_int_hash_map::iterator iter (tree_map.begin ()); iter != end; ++iter)
5038 : : {
5039 : 394581661 : tree node = reinterpret_cast<tree> ((*iter).first);
5040 : 394581661 : int ref = (*iter).second;
5041 : : /* We should have visited the node, and converted its mergeable
5042 : : reference to a regular reference. */
5043 : 394581661 : gcc_checking_assert (TREE_VISITED (node)
5044 : : && (ref <= tag_backref || ref >= tag_fixed));
5045 : 394581661 : TREE_VISITED (node) = false;
5046 : : }
5047 : 1751700 : }
5048 : :
5049 : : /* Mark DECL for by-value walking. We do this by inserting it into
5050 : : the tree map with a reference of zero. May be called multiple
5051 : : times on the same node. */
5052 : :
5053 : : void
5054 : 2687580 : trees_out::mark_by_value (tree decl)
5055 : : {
5056 : 2687580 : gcc_checking_assert (DECL_P (decl)
5057 : : /* Enum consts are INTEGER_CSTS. */
5058 : : || TREE_CODE (decl) == INTEGER_CST
5059 : : || TREE_CODE (decl) == TREE_BINFO);
5060 : :
5061 : 2687580 : if (TREE_VISITED (decl))
5062 : : /* Must already be forced or fixed. */
5063 : 2226 : gcc_checking_assert (*tree_map.get (decl) >= tag_value);
5064 : : else
5065 : : {
5066 : 2685354 : bool existed = tree_map.put (decl, tag_value);
5067 : 2685354 : gcc_checking_assert (!existed);
5068 : 2685354 : TREE_VISITED (decl) = true;
5069 : : }
5070 : 2687580 : }
5071 : :
5072 : : int
5073 : 71172319 : trees_out::get_tag (tree t)
5074 : : {
5075 : 71172319 : gcc_checking_assert (TREE_VISITED (t));
5076 : 71172319 : return *tree_map.get (t);
5077 : : }
5078 : :
5079 : : /* Insert T into the map, return its tag number. */
5080 : :
5081 : : int
5082 : 58462304 : trees_out::insert (tree t, walk_kind walk)
5083 : : {
5084 : 58462304 : gcc_checking_assert (walk != WK_normal || !TREE_VISITED (t));
5085 : 58462304 : int tag = --ref_num;
5086 : 58462304 : bool existed;
5087 : 58462304 : int &slot = tree_map.get_or_insert (t, &existed);
5088 : 58462304 : gcc_checking_assert (TREE_VISITED (t) == existed
5089 : : && (!existed
5090 : : || (walk == WK_value && slot == tag_value)));
5091 : 58462304 : TREE_VISITED (t) = true;
5092 : 58462304 : slot = tag;
5093 : :
5094 : 58462304 : return tag;
5095 : : }
5096 : :
5097 : : /* Insert T into the backreference array. Return its back reference
5098 : : number. */
5099 : :
5100 : : int
5101 : 14083384 : trees_in::insert (tree t)
5102 : : {
5103 : 14083384 : gcc_checking_assert (t || get_overrun ());
5104 : 14083384 : back_refs.safe_push (t);
5105 : 14083384 : return -(int)back_refs.length ();
5106 : : }
5107 : :
5108 : : /* A chained set of decls. */
5109 : :
5110 : : void
5111 : 73612 : trees_out::chained_decls (tree decls)
5112 : : {
5113 : 186694 : for (; decls; decls = DECL_CHAIN (decls))
5114 : 113082 : tree_node (decls);
5115 : 73612 : tree_node (NULL_TREE);
5116 : 73612 : }
5117 : :
5118 : : tree
5119 : 31870 : trees_in::chained_decls ()
5120 : : {
5121 : 31870 : tree decls = NULL_TREE;
5122 : 31870 : for (tree *chain = &decls;;)
5123 : 80598 : if (tree decl = tree_node ())
5124 : : {
5125 : 48728 : if (!DECL_P (decl) || DECL_CHAIN (decl))
5126 : : {
5127 : 0 : set_overrun ();
5128 : 0 : break;
5129 : : }
5130 : 48728 : *chain = decl;
5131 : 48728 : chain = &DECL_CHAIN (decl);
5132 : : }
5133 : : else
5134 : 48728 : break;
5135 : :
5136 : 31870 : return decls;
5137 : : }
5138 : :
5139 : : /* A vector of decls following DECL_CHAIN. */
5140 : :
5141 : : void
5142 : 280102 : trees_out::vec_chained_decls (tree decls)
5143 : : {
5144 : 280102 : if (streaming_p ())
5145 : : {
5146 : : unsigned len = 0;
5147 : :
5148 : 772094 : for (tree decl = decls; decl; decl = DECL_CHAIN (decl))
5149 : 632083 : len++;
5150 : 140011 : u (len);
5151 : : }
5152 : :
5153 : 1544594 : for (tree decl = decls; decl; decl = DECL_CHAIN (decl))
5154 : : {
5155 : 291223 : if (DECL_IMPLICIT_TYPEDEF_P (decl)
5156 : 1275063 : && TYPE_NAME (TREE_TYPE (decl)) != decl)
5157 : : /* An anonynmous struct with a typedef name. An odd thing to
5158 : : write. */
5159 : 8 : tree_node (NULL_TREE);
5160 : : else
5161 : 1264484 : tree_node (decl);
5162 : : }
5163 : 280102 : }
5164 : :
5165 : : vec<tree, va_heap> *
5166 : 100972 : trees_in::vec_chained_decls ()
5167 : : {
5168 : 100972 : vec<tree, va_heap> *v = NULL;
5169 : :
5170 : 100972 : if (unsigned len = u ())
5171 : : {
5172 : 53010 : vec_alloc (v, len);
5173 : :
5174 : 575462 : for (unsigned ix = 0; ix < len; ix++)
5175 : : {
5176 : 522452 : tree decl = tree_node ();
5177 : 522452 : if (decl && !DECL_P (decl))
5178 : : {
5179 : 0 : set_overrun ();
5180 : 0 : break;
5181 : : }
5182 : 522452 : v->quick_push (decl);
5183 : : }
5184 : :
5185 : 53010 : if (get_overrun ())
5186 : : {
5187 : 0 : vec_free (v);
5188 : 0 : v = NULL;
5189 : : }
5190 : : }
5191 : :
5192 : 100972 : return v;
5193 : : }
5194 : :
5195 : : /* A vector of trees. */
5196 : :
5197 : : void
5198 : 200878 : trees_out::tree_vec (vec<tree, va_gc> *v)
5199 : : {
5200 : 200878 : unsigned len = vec_safe_length (v);
5201 : 200878 : if (streaming_p ())
5202 : 100419 : u (len);
5203 : 260284 : for (unsigned ix = 0; ix != len; ix++)
5204 : 59406 : tree_node ((*v)[ix]);
5205 : 200878 : }
5206 : :
5207 : : vec<tree, va_gc> *
5208 : 72401 : trees_in::tree_vec ()
5209 : : {
5210 : 72401 : vec<tree, va_gc> *v = NULL;
5211 : 72401 : if (unsigned len = u ())
5212 : : {
5213 : 19551 : vec_alloc (v, len);
5214 : 40982 : for (unsigned ix = 0; ix != len; ix++)
5215 : 21431 : v->quick_push (tree_node ());
5216 : : }
5217 : 72401 : return v;
5218 : : }
5219 : :
5220 : : /* A vector of tree pairs. */
5221 : :
5222 : : void
5223 : 4760 : trees_out::tree_pair_vec (vec<tree_pair_s, va_gc> *v)
5224 : : {
5225 : 4760 : unsigned len = vec_safe_length (v);
5226 : 4760 : if (streaming_p ())
5227 : 2380 : u (len);
5228 : 4760 : if (len)
5229 : 25300 : for (unsigned ix = 0; ix != len; ix++)
5230 : : {
5231 : 20648 : tree_pair_s const &s = (*v)[ix];
5232 : 20648 : tree_node (s.purpose);
5233 : 20648 : tree_node (s.value);
5234 : : }
5235 : 4760 : }
5236 : :
5237 : : vec<tree_pair_s, va_gc> *
5238 : 2057 : trees_in::tree_pair_vec ()
5239 : : {
5240 : 2057 : vec<tree_pair_s, va_gc> *v = NULL;
5241 : 2057 : if (unsigned len = u ())
5242 : : {
5243 : 2011 : vec_alloc (v, len);
5244 : 11109 : for (unsigned ix = 0; ix != len; ix++)
5245 : : {
5246 : 9098 : tree_pair_s s;
5247 : 9098 : s.purpose = tree_node ();
5248 : 9098 : s.value = tree_node ();
5249 : 9098 : v->quick_push (s);
5250 : : }
5251 : : }
5252 : 2057 : return v;
5253 : : }
5254 : :
5255 : : void
5256 : 293028 : trees_out::tree_list (tree list, bool has_purpose)
5257 : : {
5258 : 1168249 : for (; list; list = TREE_CHAIN (list))
5259 : : {
5260 : 875221 : gcc_checking_assert (TREE_VALUE (list));
5261 : 875221 : tree_node (TREE_VALUE (list));
5262 : 875221 : if (has_purpose)
5263 : 849115 : tree_node (TREE_PURPOSE (list));
5264 : : }
5265 : 293028 : tree_node (NULL_TREE);
5266 : 293028 : }
5267 : :
5268 : : tree
5269 : 106897 : trees_in::tree_list (bool has_purpose)
5270 : : {
5271 : 106897 : tree res = NULL_TREE;
5272 : :
5273 : 472594 : for (tree *chain = &res; tree value = tree_node ();
5274 : 731394 : chain = &TREE_CHAIN (*chain))
5275 : : {
5276 : 365697 : tree purpose = has_purpose ? tree_node () : NULL_TREE;
5277 : 365697 : *chain = build_tree_list (purpose, value);
5278 : 365697 : }
5279 : :
5280 : 106897 : return res;
5281 : : }
5282 : :
5283 : : #define CASE_OMP_SIMD_CODE \
5284 : : case OMP_SIMD: \
5285 : : case OMP_STRUCTURED_BLOCK: \
5286 : : case OMP_LOOP: \
5287 : : case OMP_ORDERED: \
5288 : : case OMP_TILE: \
5289 : : case OMP_UNROLL
5290 : : #define CASE_OMP_CODE \
5291 : : case OMP_PARALLEL: \
5292 : : case OMP_TASK: \
5293 : : case OMP_FOR: \
5294 : : case OMP_DISTRIBUTE: \
5295 : : case OMP_TASKLOOP: \
5296 : : case OMP_TEAMS: \
5297 : : case OMP_TARGET_DATA: \
5298 : : case OMP_TARGET: \
5299 : : case OMP_SECTIONS: \
5300 : : case OMP_CRITICAL: \
5301 : : case OMP_SINGLE: \
5302 : : case OMP_SCOPE: \
5303 : : case OMP_TASKGROUP: \
5304 : : case OMP_MASKED: \
5305 : : case OMP_DISPATCH: \
5306 : : case OMP_INTEROP: \
5307 : : case OMP_MASTER: \
5308 : : case OMP_TARGET_UPDATE: \
5309 : : case OMP_TARGET_ENTER_DATA: \
5310 : : case OMP_TARGET_EXIT_DATA: \
5311 : : case OMP_METADIRECTIVE: \
5312 : : case OMP_ATOMIC: \
5313 : : case OMP_ATOMIC_READ: \
5314 : : case OMP_ATOMIC_CAPTURE_OLD: \
5315 : : case OMP_ATOMIC_CAPTURE_NEW
5316 : : #define CASE_OACC_CODE \
5317 : : case OACC_PARALLEL: \
5318 : : case OACC_KERNELS: \
5319 : : case OACC_SERIAL: \
5320 : : case OACC_DATA: \
5321 : : case OACC_HOST_DATA: \
5322 : : case OACC_LOOP: \
5323 : : case OACC_CACHE: \
5324 : : case OACC_DECLARE: \
5325 : : case OACC_ENTER_DATA: \
5326 : : case OACC_EXIT_DATA: \
5327 : : case OACC_UPDATE
5328 : :
5329 : : /* Start tree write. Write information to allocate the receiving
5330 : : node. */
5331 : :
5332 : : void
5333 : 11746798 : trees_out::start (tree t, bool code_streamed)
5334 : : {
5335 : 11746798 : if (TYPE_P (t))
5336 : : {
5337 : 492588 : enum tree_code code = TREE_CODE (t);
5338 : 492588 : gcc_checking_assert (TYPE_MAIN_VARIANT (t) == t);
5339 : : /* All these types are TYPE_NON_COMMON. */
5340 : 492588 : gcc_checking_assert (code == RECORD_TYPE
5341 : : || code == UNION_TYPE
5342 : : || code == ENUMERAL_TYPE
5343 : : || code == TEMPLATE_TYPE_PARM
5344 : : || code == TEMPLATE_TEMPLATE_PARM
5345 : : || code == BOUND_TEMPLATE_TEMPLATE_PARM);
5346 : : }
5347 : :
5348 : 11746798 : if (!code_streamed)
5349 : 11305572 : u (TREE_CODE (t));
5350 : :
5351 : 11746798 : switch (TREE_CODE (t))
5352 : : {
5353 : 10418845 : default:
5354 : 10418845 : if (VL_EXP_CLASS_P (t))
5355 : 438908 : u (VL_EXP_OPERAND_LENGTH (t));
5356 : : break;
5357 : :
5358 : 523034 : case INTEGER_CST:
5359 : 523034 : u (TREE_INT_CST_NUNITS (t));
5360 : 523034 : u (TREE_INT_CST_EXT_NUNITS (t));
5361 : 523034 : break;
5362 : :
5363 : 12 : case OMP_CLAUSE:
5364 : 12 : u (OMP_CLAUSE_CODE (t));
5365 : 12 : break;
5366 : :
5367 : 6 : CASE_OMP_SIMD_CODE:
5368 : 6 : state->extensions |= SE_OPENMP_SIMD;
5369 : 6 : break;
5370 : :
5371 : 3 : CASE_OMP_CODE:
5372 : 3 : state->extensions |= SE_OPENMP;
5373 : 3 : break;
5374 : :
5375 : 6 : CASE_OACC_CODE:
5376 : 6 : state->extensions |= SE_OPENACC;
5377 : 6 : break;
5378 : :
5379 : 26993 : case STRING_CST:
5380 : 26993 : str (TREE_STRING_POINTER (t), TREE_STRING_LENGTH (t));
5381 : 26993 : break;
5382 : :
5383 : 18 : case RAW_DATA_CST:
5384 : 18 : if (RAW_DATA_OWNER (t) == NULL_TREE)
5385 : : {
5386 : : /* Stream RAW_DATA_CST with no owner (i.e. data pointing
5387 : : into libcpp buffers) as something we can stream in as
5388 : : STRING_CST which owns the data. */
5389 : 6 : u (0);
5390 : : /* Can't use str (RAW_DATA_POINTER (t), RAW_DATA_LENGTH (t));
5391 : : here as there isn't a null termination after it. */
5392 : 6 : z (RAW_DATA_LENGTH (t));
5393 : 6 : if (RAW_DATA_LENGTH (t))
5394 : 6 : if (void *ptr = buf (RAW_DATA_LENGTH (t) + 1))
5395 : : {
5396 : 6 : memcpy (ptr, RAW_DATA_POINTER (t), RAW_DATA_LENGTH (t));
5397 : 6 : ((char *) ptr)[RAW_DATA_LENGTH (t)] = '\0';
5398 : : }
5399 : : }
5400 : : else
5401 : : {
5402 : 12 : gcc_assert (RAW_DATA_LENGTH (t));
5403 : 12 : u (RAW_DATA_LENGTH (t));
5404 : : }
5405 : : break;
5406 : :
5407 : 18 : case VECTOR_CST:
5408 : 18 : u (VECTOR_CST_LOG2_NPATTERNS (t));
5409 : 18 : u (VECTOR_CST_NELTS_PER_PATTERN (t));
5410 : 18 : break;
5411 : :
5412 : 98039 : case TREE_BINFO:
5413 : 98039 : u (BINFO_N_BASE_BINFOS (t));
5414 : 98039 : break;
5415 : :
5416 : 679824 : case TREE_VEC:
5417 : 679824 : u (TREE_VEC_LENGTH (t));
5418 : 679824 : break;
5419 : :
5420 : 0 : case FIXED_CST:
5421 : 0 : gcc_unreachable (); /* Not supported in C++. */
5422 : 0 : break;
5423 : :
5424 : 0 : case IDENTIFIER_NODE:
5425 : 0 : case SSA_NAME:
5426 : 0 : case TARGET_MEM_REF:
5427 : 0 : case TRANSLATION_UNIT_DECL:
5428 : : /* We shouldn't meet these. */
5429 : 0 : gcc_unreachable ();
5430 : 11746798 : break;
5431 : : }
5432 : 11746798 : }
5433 : :
5434 : : /* Start tree read. Allocate the receiving node. */
5435 : :
5436 : : tree
5437 : 9939000 : trees_in::start (unsigned code)
5438 : : {
5439 : 9939000 : tree t = NULL_TREE;
5440 : :
5441 : 9939000 : if (!code)
5442 : 8954982 : code = u ();
5443 : :
5444 : 9939000 : switch (code)
5445 : : {
5446 : 8865364 : default:
5447 : 8865364 : if (code >= MAX_TREE_CODES)
5448 : : {
5449 : 0 : fail:
5450 : 0 : set_overrun ();
5451 : 0 : return NULL_TREE;
5452 : : }
5453 : 8865364 : else if (TREE_CODE_CLASS (code) == tcc_vl_exp)
5454 : : {
5455 : 381305 : unsigned ops = u ();
5456 : 381305 : t = build_vl_exp (tree_code (code), ops);
5457 : : }
5458 : : else
5459 : 8484059 : t = make_node (tree_code (code));
5460 : : break;
5461 : :
5462 : 425234 : case INTEGER_CST:
5463 : 425234 : {
5464 : 425234 : unsigned n = u ();
5465 : 425234 : unsigned e = u ();
5466 : 425234 : t = make_int_cst (n, e);
5467 : : }
5468 : 425234 : break;
5469 : :
5470 : 12 : case OMP_CLAUSE:
5471 : 12 : t = build_omp_clause (UNKNOWN_LOCATION, omp_clause_code (u ()));
5472 : 12 : break;
5473 : :
5474 : 9 : CASE_OMP_SIMD_CODE:
5475 : 9 : if (!(state->extensions & SE_OPENMP_SIMD))
5476 : 0 : goto fail;
5477 : 9 : t = make_node (tree_code (code));
5478 : 9 : break;
5479 : :
5480 : 3 : CASE_OMP_CODE:
5481 : 3 : if (!(state->extensions & SE_OPENMP))
5482 : 0 : goto fail;
5483 : 3 : t = make_node (tree_code (code));
5484 : 3 : break;
5485 : :
5486 : 6 : CASE_OACC_CODE:
5487 : 6 : if (!(state->extensions & SE_OPENACC))
5488 : 0 : goto fail;
5489 : 6 : t = make_node (tree_code (code));
5490 : 6 : break;
5491 : :
5492 : 23615 : case STRING_CST:
5493 : 23615 : {
5494 : 23615 : size_t l;
5495 : 23615 : const char *chars = str (&l);
5496 : 23615 : t = build_string (l, chars);
5497 : : }
5498 : 23615 : break;
5499 : :
5500 : 9 : case RAW_DATA_CST:
5501 : 9 : {
5502 : 9 : size_t l = u ();
5503 : 9 : if (l == 0)
5504 : : {
5505 : : /* Stream in RAW_DATA_CST with no owner as STRING_CST
5506 : : which owns the data. */
5507 : 3 : const char *chars = str (&l);
5508 : 3 : t = build_string (l, chars);
5509 : : }
5510 : : else
5511 : : {
5512 : 6 : t = make_node (RAW_DATA_CST);
5513 : 6 : RAW_DATA_LENGTH (t) = l;
5514 : : }
5515 : : }
5516 : 9 : break;
5517 : :
5518 : 24 : case VECTOR_CST:
5519 : 24 : {
5520 : 24 : unsigned log2_npats = u ();
5521 : 24 : unsigned elts_per = u ();
5522 : 24 : t = make_vector (log2_npats, elts_per);
5523 : : }
5524 : 24 : break;
5525 : :
5526 : 70344 : case TREE_BINFO:
5527 : 70344 : t = make_tree_binfo (u ());
5528 : 70344 : break;
5529 : :
5530 : 554380 : case TREE_VEC:
5531 : 554380 : t = make_tree_vec (u ());
5532 : 554380 : break;
5533 : :
5534 : 0 : case FIXED_CST:
5535 : 0 : case IDENTIFIER_NODE:
5536 : 0 : case SSA_NAME:
5537 : 0 : case TARGET_MEM_REF:
5538 : 0 : case TRANSLATION_UNIT_DECL:
5539 : 0 : goto fail;
5540 : : }
5541 : :
5542 : : return t;
5543 : : }
5544 : :
5545 : : /* The kinds of interface an importer could have for a decl. */
5546 : :
5547 : : enum class importer_interface {
5548 : : unknown, /* The definition may or may not need to be emitted. */
5549 : : always_import, /* The definition can always be found in another TU. */
5550 : : always_emit, /* The definition must be emitted in the importer's TU. */
5551 : : };
5552 : :
5553 : : /* Returns what kind of interface an importer will have of DECL. */
5554 : :
5555 : : static importer_interface
5556 : 356312 : get_importer_interface (tree decl)
5557 : : {
5558 : : /* Internal linkage entities must be emitted in each importer if
5559 : : there is a definition available. */
5560 : 356312 : if (!TREE_PUBLIC (decl))
5561 : : return importer_interface::always_emit;
5562 : :
5563 : : /* Entities that aren't vague linkage are either not definitions or
5564 : : will be emitted in this TU, so importers can just refer to an
5565 : : external definition. */
5566 : 144414 : if (!vague_linkage_p (decl))
5567 : : return importer_interface::always_import;
5568 : :
5569 : : /* For explicit instantiations, importers can always rely on there
5570 : : being a definition in another TU, unless this is a definition
5571 : : in a header module: in which case the importer will always need
5572 : : to emit it. */
5573 : 140033 : if (DECL_LANG_SPECIFIC (decl)
5574 : 140033 : && DECL_EXPLICIT_INSTANTIATION (decl))
5575 : 18858 : return (header_module_p () && !DECL_EXTERNAL (decl)
5576 : 18858 : ? importer_interface::always_emit
5577 : : : importer_interface::always_import);
5578 : :
5579 : : /* A gnu_inline function is never emitted in any TU. */
5580 : 121175 : if (TREE_CODE (decl) == FUNCTION_DECL
5581 : 81787 : && DECL_DECLARED_INLINE_P (decl)
5582 : 199428 : && lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl)))
5583 : : return importer_interface::always_import;
5584 : :
5585 : : /* Everything else has vague linkage. */
5586 : : return importer_interface::unknown;
5587 : : }
5588 : :
5589 : : /* The structure streamers access the raw fields, because the
5590 : : alternative, of using the accessor macros can require using
5591 : : different accessors for the same underlying field, depending on the
5592 : : tree code. That's both confusing and annoying. */
5593 : :
5594 : : /* Read & write the core boolean flags. */
5595 : :
5596 : : void
5597 : 11764516 : trees_out::core_bools (tree t, bits_out& bits)
5598 : : {
5599 : : #define WB(X) (bits.b (X))
5600 : : /* Stream X if COND holds, and if !COND stream a dummy value so that the
5601 : : overall number of bits streamed is independent of the runtime value
5602 : : of COND, which allows the compiler to better optimize this function. */
5603 : : #define WB_IF(COND, X) WB ((COND) ? (X) : false)
5604 : 11764516 : tree_code code = TREE_CODE (t);
5605 : :
5606 : 11764516 : WB (t->base.side_effects_flag);
5607 : 11764516 : WB (t->base.constant_flag);
5608 : 11764516 : WB (t->base.addressable_flag);
5609 : 11764516 : WB (t->base.volatile_flag);
5610 : 11764516 : WB (t->base.readonly_flag);
5611 : : /* base.asm_written_flag is a property of the current TU's use of
5612 : : this decl. */
5613 : 11764516 : WB (t->base.nowarning_flag);
5614 : : /* base.visited read as zero (it's set for writer, because that's
5615 : : how we mark nodes). */
5616 : : /* base.used_flag is not streamed. Readers may set TREE_USED of
5617 : : decls they use. */
5618 : 11764516 : WB (t->base.nothrow_flag);
5619 : 11764516 : WB (t->base.static_flag);
5620 : : /* This is TYPE_CACHED_VALUES_P for types. */
5621 : 11764516 : WB_IF (TREE_CODE_CLASS (code) != tcc_type, t->base.public_flag);
5622 : 11764516 : WB (t->base.private_flag);
5623 : 11764516 : WB (t->base.protected_flag);
5624 : 11764516 : WB (t->base.deprecated_flag);
5625 : 11764516 : WB (t->base.default_def_flag);
5626 : :
5627 : 11764516 : switch (code)
5628 : : {
5629 : : case CALL_EXPR:
5630 : : case INTEGER_CST:
5631 : : case SSA_NAME:
5632 : : case TARGET_MEM_REF:
5633 : : case TREE_VEC:
5634 : : /* These use different base.u fields. */
5635 : : return;
5636 : :
5637 : 10132828 : default:
5638 : 10132828 : WB (t->base.u.bits.lang_flag_0);
5639 : 10132828 : bool flag_1 = t->base.u.bits.lang_flag_1;
5640 : 10132828 : if (!flag_1)
5641 : : ;
5642 : 302429 : else if (code == TEMPLATE_INFO)
5643 : : /* This is TI_PENDING_TEMPLATE_FLAG, not relevant to reader. */
5644 : : flag_1 = false;
5645 : 299077 : else if (code == VAR_DECL)
5646 : : {
5647 : : /* This is DECL_INITIALIZED_P. */
5648 : 50720 : if (TREE_CODE (DECL_CONTEXT (t)) != FUNCTION_DECL)
5649 : : /* We'll set this when reading the definition. */
5650 : 10132828 : flag_1 = false;
5651 : : }
5652 : 10132828 : WB (flag_1);
5653 : 10132828 : WB (t->base.u.bits.lang_flag_2);
5654 : 10132828 : WB (t->base.u.bits.lang_flag_3);
5655 : 10132828 : WB (t->base.u.bits.lang_flag_4);
5656 : 10132828 : WB (t->base.u.bits.lang_flag_5);
5657 : 10132828 : WB (t->base.u.bits.lang_flag_6);
5658 : 10132828 : WB (t->base.u.bits.saturating_flag);
5659 : 10132828 : WB (t->base.u.bits.unsigned_flag);
5660 : 10132828 : WB (t->base.u.bits.packed_flag);
5661 : 10132828 : WB (t->base.u.bits.user_align);
5662 : 10132828 : WB (t->base.u.bits.nameless_flag);
5663 : 10132828 : WB (t->base.u.bits.atomic_flag);
5664 : 10132828 : WB (t->base.u.bits.unavailable_flag);
5665 : 10132828 : break;
5666 : : }
5667 : :
5668 : 10132828 : if (TREE_CODE_CLASS (code) == tcc_type)
5669 : : {
5670 : 510306 : WB (t->type_common.no_force_blk_flag);
5671 : 510306 : WB (t->type_common.needs_constructing_flag);
5672 : 510306 : WB (t->type_common.transparent_aggr_flag);
5673 : 510306 : WB (t->type_common.restrict_flag);
5674 : 510306 : WB (t->type_common.string_flag);
5675 : 510306 : WB (t->type_common.lang_flag_0);
5676 : 510306 : WB (t->type_common.lang_flag_1);
5677 : 510306 : WB (t->type_common.lang_flag_2);
5678 : 510306 : WB (t->type_common.lang_flag_3);
5679 : 510306 : WB (t->type_common.lang_flag_4);
5680 : 510306 : WB (t->type_common.lang_flag_5);
5681 : 510306 : WB (t->type_common.lang_flag_6);
5682 : 510306 : WB (t->type_common.typeless_storage);
5683 : : }
5684 : :
5685 : 10132828 : if (TREE_CODE_CLASS (code) != tcc_declaration)
5686 : : return;
5687 : :
5688 : 2659800 : if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
5689 : : {
5690 : 2659800 : WB (t->decl_common.nonlocal_flag);
5691 : 2659800 : WB (t->decl_common.virtual_flag);
5692 : 2659800 : WB (t->decl_common.ignored_flag);
5693 : 2659800 : WB (t->decl_common.abstract_flag);
5694 : 2659800 : WB (t->decl_common.artificial_flag);
5695 : 2659800 : WB (t->decl_common.preserve_flag);
5696 : 2659800 : WB (t->decl_common.debug_expr_is_from);
5697 : 2659800 : WB (t->decl_common.lang_flag_0);
5698 : 2659800 : WB (t->decl_common.lang_flag_1);
5699 : 2659800 : WB (t->decl_common.lang_flag_2);
5700 : 2659800 : WB (t->decl_common.lang_flag_3);
5701 : 2659800 : WB (t->decl_common.lang_flag_4);
5702 : :
5703 : 2659800 : {
5704 : : /* This is DECL_INTERFACE_KNOWN: We should redetermine whether
5705 : : we need to import or export any vague-linkage entities on
5706 : : stream-in. */
5707 : 2659800 : bool interface_known = t->decl_common.lang_flag_5;
5708 : 2659800 : if (interface_known
5709 : 2659800 : && get_importer_interface (t) == importer_interface::unknown)
5710 : : interface_known = false;
5711 : 2659800 : WB (interface_known);
5712 : : }
5713 : :
5714 : 2659800 : WB (t->decl_common.lang_flag_6);
5715 : 2659800 : WB (t->decl_common.lang_flag_7);
5716 : 2659800 : WB (t->decl_common.lang_flag_8);
5717 : 2659800 : WB (t->decl_common.decl_flag_0);
5718 : :
5719 : 2659800 : {
5720 : : /* DECL_EXTERNAL -> decl_flag_1
5721 : : == it is defined elsewhere
5722 : : DECL_NOT_REALLY_EXTERN -> base.not_really_extern
5723 : : == that was a lie, it is here */
5724 : :
5725 : 2659800 : bool is_external = t->decl_common.decl_flag_1;
5726 : 2659800 : if (!is_external)
5727 : : /* decl_flag_1 is DECL_EXTERNAL. Things we emit here, might
5728 : : well be external from the POV of an importer. */
5729 : : // FIXME: Do we need to know if this is a TEMPLATE_RESULT --
5730 : : // a flag from the caller?
5731 : 2233524 : switch (code)
5732 : : {
5733 : : default:
5734 : : break;
5735 : :
5736 : 155351 : case VAR_DECL:
5737 : 155351 : if (TREE_PUBLIC (t)
5738 : 182958 : && DECL_VTABLE_OR_VTT_P (t))
5739 : : /* We handle vtable linkage specially. */
5740 : : is_external = true;
5741 : 182183 : gcc_fallthrough ();
5742 : 182183 : case FUNCTION_DECL:
5743 : 182183 : if (get_importer_interface (t)
5744 : : == importer_interface::always_import)
5745 : 427452 : is_external = true;
5746 : : break;
5747 : : }
5748 : 2659800 : WB (is_external);
5749 : : }
5750 : :
5751 : 2659800 : WB (t->decl_common.decl_flag_2);
5752 : 2659800 : WB (t->decl_common.decl_flag_3);
5753 : 2659800 : WB (t->decl_common.not_gimple_reg_flag);
5754 : 2659800 : WB (t->decl_common.decl_by_reference_flag);
5755 : 2659800 : WB (t->decl_common.decl_read_flag);
5756 : 2659800 : WB (t->decl_common.decl_nonshareable_flag);
5757 : 2659800 : WB (t->decl_common.decl_not_flexarray);
5758 : : }
5759 : : else
5760 : : return;
5761 : :
5762 : 2659800 : if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
5763 : : {
5764 : 1259031 : WB (t->decl_with_vis.defer_output);
5765 : 1259031 : WB (t->decl_with_vis.hard_register);
5766 : 1259031 : WB (t->decl_with_vis.common_flag);
5767 : 1259031 : WB (t->decl_with_vis.in_text_section);
5768 : 1259031 : WB (t->decl_with_vis.in_constant_pool);
5769 : 1259031 : WB (t->decl_with_vis.dllimport_flag);
5770 : 1259031 : WB (t->decl_with_vis.weak_flag);
5771 : 1259031 : WB (t->decl_with_vis.seen_in_bind_expr);
5772 : 1259031 : WB (t->decl_with_vis.comdat_flag);
5773 : 1259031 : WB (t->decl_with_vis.visibility_specified);
5774 : 1259031 : WB (t->decl_with_vis.init_priority_p);
5775 : 1259031 : WB (t->decl_with_vis.shadowed_for_var_p);
5776 : 1259031 : WB (t->decl_with_vis.cxx_constructor);
5777 : 1259031 : WB (t->decl_with_vis.cxx_destructor);
5778 : 1259031 : WB (t->decl_with_vis.final);
5779 : 1259031 : WB (t->decl_with_vis.regdecl_flag);
5780 : : }
5781 : : else
5782 : : return;
5783 : :
5784 : 1259031 : if (CODE_CONTAINS_STRUCT (code, TS_FUNCTION_DECL))
5785 : : {
5786 : 363559 : WB (t->function_decl.static_ctor_flag);
5787 : 363559 : WB (t->function_decl.static_dtor_flag);
5788 : 363559 : WB (t->function_decl.uninlinable);
5789 : 363559 : WB (t->function_decl.possibly_inlined);
5790 : 363559 : WB (t->function_decl.novops_flag);
5791 : 363559 : WB (t->function_decl.returns_twice_flag);
5792 : 363559 : WB (t->function_decl.malloc_flag);
5793 : 363559 : WB (t->function_decl.declared_inline_flag);
5794 : 363559 : WB (t->function_decl.no_inline_warning_flag);
5795 : 363559 : WB (t->function_decl.no_instrument_function_entry_exit);
5796 : 363559 : WB (t->function_decl.no_limit_stack);
5797 : 363559 : WB (t->function_decl.disregard_inline_limits);
5798 : 363559 : WB (t->function_decl.pure_flag);
5799 : 363559 : WB (t->function_decl.looping_const_or_pure_flag);
5800 : :
5801 : 363559 : WB (t->function_decl.has_debug_args_flag);
5802 : 363559 : WB (t->function_decl.versioned_function);
5803 : 363559 : WB (t->function_decl.replaceable_operator);
5804 : :
5805 : : /* decl_type is a (misnamed) 2 bit discriminator. */
5806 : 363559 : unsigned kind = (unsigned)t->function_decl.decl_type;
5807 : 363559 : WB ((kind >> 0) & 1);
5808 : 363559 : WB ((kind >> 1) & 1);
5809 : : }
5810 : : #undef WB_IF
5811 : : #undef WB
5812 : : }
5813 : :
5814 : : bool
5815 : 9953132 : trees_in::core_bools (tree t, bits_in& bits)
5816 : : {
5817 : : #define RB(X) ((X) = bits.b ())
5818 : : /* See the comment for WB_IF in trees_out::core_bools. */
5819 : : #define RB_IF(COND, X) ((COND) ? RB (X) : bits.b ())
5820 : :
5821 : 9953132 : tree_code code = TREE_CODE (t);
5822 : :
5823 : 9953132 : RB (t->base.side_effects_flag);
5824 : 9953132 : RB (t->base.constant_flag);
5825 : 9953132 : RB (t->base.addressable_flag);
5826 : 9953132 : RB (t->base.volatile_flag);
5827 : 9953132 : RB (t->base.readonly_flag);
5828 : : /* base.asm_written_flag is not streamed. */
5829 : 9953132 : RB (t->base.nowarning_flag);
5830 : : /* base.visited is not streamed. */
5831 : : /* base.used_flag is not streamed. */
5832 : 9953132 : RB (t->base.nothrow_flag);
5833 : 9953132 : RB (t->base.static_flag);
5834 : 9953132 : RB_IF (TREE_CODE_CLASS (code) != tcc_type, t->base.public_flag);
5835 : 9953132 : RB (t->base.private_flag);
5836 : 9953132 : RB (t->base.protected_flag);
5837 : 9953132 : RB (t->base.deprecated_flag);
5838 : 9953132 : RB (t->base.default_def_flag);
5839 : :
5840 : 9953132 : switch (code)
5841 : : {
5842 : 1352362 : case CALL_EXPR:
5843 : 1352362 : case INTEGER_CST:
5844 : 1352362 : case SSA_NAME:
5845 : 1352362 : case TARGET_MEM_REF:
5846 : 1352362 : case TREE_VEC:
5847 : : /* These use different base.u fields. */
5848 : 1352362 : goto done;
5849 : :
5850 : 8600770 : default:
5851 : 8600770 : RB (t->base.u.bits.lang_flag_0);
5852 : 8600770 : RB (t->base.u.bits.lang_flag_1);
5853 : 8600770 : RB (t->base.u.bits.lang_flag_2);
5854 : 8600770 : RB (t->base.u.bits.lang_flag_3);
5855 : 8600770 : RB (t->base.u.bits.lang_flag_4);
5856 : 8600770 : RB (t->base.u.bits.lang_flag_5);
5857 : 8600770 : RB (t->base.u.bits.lang_flag_6);
5858 : 8600770 : RB (t->base.u.bits.saturating_flag);
5859 : 8600770 : RB (t->base.u.bits.unsigned_flag);
5860 : 8600770 : RB (t->base.u.bits.packed_flag);
5861 : 8600770 : RB (t->base.u.bits.user_align);
5862 : 8600770 : RB (t->base.u.bits.nameless_flag);
5863 : 8600770 : RB (t->base.u.bits.atomic_flag);
5864 : 8600770 : RB (t->base.u.bits.unavailable_flag);
5865 : 8600770 : break;
5866 : : }
5867 : :
5868 : 8600770 : if (TREE_CODE_CLASS (code) == tcc_type)
5869 : : {
5870 : 398712 : RB (t->type_common.no_force_blk_flag);
5871 : 398712 : RB (t->type_common.needs_constructing_flag);
5872 : 398712 : RB (t->type_common.transparent_aggr_flag);
5873 : 398712 : RB (t->type_common.restrict_flag);
5874 : 398712 : RB (t->type_common.string_flag);
5875 : 398712 : RB (t->type_common.lang_flag_0);
5876 : 398712 : RB (t->type_common.lang_flag_1);
5877 : 398712 : RB (t->type_common.lang_flag_2);
5878 : 398712 : RB (t->type_common.lang_flag_3);
5879 : 398712 : RB (t->type_common.lang_flag_4);
5880 : 398712 : RB (t->type_common.lang_flag_5);
5881 : 398712 : RB (t->type_common.lang_flag_6);
5882 : 398712 : RB (t->type_common.typeless_storage);
5883 : : }
5884 : :
5885 : 8600770 : if (TREE_CODE_CLASS (code) != tcc_declaration)
5886 : 6392612 : goto done;
5887 : :
5888 : 2208158 : if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
5889 : : {
5890 : 2208158 : RB (t->decl_common.nonlocal_flag);
5891 : 2208158 : RB (t->decl_common.virtual_flag);
5892 : 2208158 : RB (t->decl_common.ignored_flag);
5893 : 2208158 : RB (t->decl_common.abstract_flag);
5894 : 2208158 : RB (t->decl_common.artificial_flag);
5895 : 2208158 : RB (t->decl_common.preserve_flag);
5896 : 2208158 : RB (t->decl_common.debug_expr_is_from);
5897 : 2208158 : RB (t->decl_common.lang_flag_0);
5898 : 2208158 : RB (t->decl_common.lang_flag_1);
5899 : 2208158 : RB (t->decl_common.lang_flag_2);
5900 : 2208158 : RB (t->decl_common.lang_flag_3);
5901 : 2208158 : RB (t->decl_common.lang_flag_4);
5902 : 2208158 : RB (t->decl_common.lang_flag_5);
5903 : 2208158 : RB (t->decl_common.lang_flag_6);
5904 : 2208158 : RB (t->decl_common.lang_flag_7);
5905 : 2208158 : RB (t->decl_common.lang_flag_8);
5906 : 2208158 : RB (t->decl_common.decl_flag_0);
5907 : 2208158 : RB (t->decl_common.decl_flag_1);
5908 : 2208158 : RB (t->decl_common.decl_flag_2);
5909 : 2208158 : RB (t->decl_common.decl_flag_3);
5910 : 2208158 : RB (t->decl_common.not_gimple_reg_flag);
5911 : 2208158 : RB (t->decl_common.decl_by_reference_flag);
5912 : 2208158 : RB (t->decl_common.decl_read_flag);
5913 : 2208158 : RB (t->decl_common.decl_nonshareable_flag);
5914 : 2208158 : RB (t->decl_common.decl_not_flexarray);
5915 : : }
5916 : : else
5917 : 0 : goto done;
5918 : :
5919 : 2208158 : if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
5920 : : {
5921 : 1023284 : RB (t->decl_with_vis.defer_output);
5922 : 1023284 : RB (t->decl_with_vis.hard_register);
5923 : 1023284 : RB (t->decl_with_vis.common_flag);
5924 : 1023284 : RB (t->decl_with_vis.in_text_section);
5925 : 1023284 : RB (t->decl_with_vis.in_constant_pool);
5926 : 1023284 : RB (t->decl_with_vis.dllimport_flag);
5927 : 1023284 : RB (t->decl_with_vis.weak_flag);
5928 : 1023284 : RB (t->decl_with_vis.seen_in_bind_expr);
5929 : 1023284 : RB (t->decl_with_vis.comdat_flag);
5930 : 1023284 : RB (t->decl_with_vis.visibility_specified);
5931 : 1023284 : RB (t->decl_with_vis.init_priority_p);
5932 : 1023284 : RB (t->decl_with_vis.shadowed_for_var_p);
5933 : 1023284 : RB (t->decl_with_vis.cxx_constructor);
5934 : 1023284 : RB (t->decl_with_vis.cxx_destructor);
5935 : 1023284 : RB (t->decl_with_vis.final);
5936 : 1023284 : RB (t->decl_with_vis.regdecl_flag);
5937 : : }
5938 : : else
5939 : 1184874 : goto done;
5940 : :
5941 : 1023284 : if (CODE_CONTAINS_STRUCT (code, TS_FUNCTION_DECL))
5942 : : {
5943 : 313287 : RB (t->function_decl.static_ctor_flag);
5944 : 313287 : RB (t->function_decl.static_dtor_flag);
5945 : 313287 : RB (t->function_decl.uninlinable);
5946 : 313287 : RB (t->function_decl.possibly_inlined);
5947 : 313287 : RB (t->function_decl.novops_flag);
5948 : 313287 : RB (t->function_decl.returns_twice_flag);
5949 : 313287 : RB (t->function_decl.malloc_flag);
5950 : 313287 : RB (t->function_decl.declared_inline_flag);
5951 : 313287 : RB (t->function_decl.no_inline_warning_flag);
5952 : 313287 : RB (t->function_decl.no_instrument_function_entry_exit);
5953 : 313287 : RB (t->function_decl.no_limit_stack);
5954 : 313287 : RB (t->function_decl.disregard_inline_limits);
5955 : 313287 : RB (t->function_decl.pure_flag);
5956 : 313287 : RB (t->function_decl.looping_const_or_pure_flag);
5957 : :
5958 : 313287 : RB (t->function_decl.has_debug_args_flag);
5959 : 313287 : RB (t->function_decl.versioned_function);
5960 : 313287 : RB (t->function_decl.replaceable_operator);
5961 : :
5962 : : /* decl_type is a (misnamed) 2 bit discriminator. */
5963 : 313287 : unsigned kind = 0;
5964 : 313287 : kind |= unsigned (bits.b ()) << 0;
5965 : 313287 : kind |= unsigned (bits.b ()) << 1;
5966 : 313287 : t->function_decl.decl_type = function_decl_type (kind);
5967 : : }
5968 : : #undef RB_IF
5969 : : #undef RB
5970 : 709997 : done:
5971 : 9953132 : return !get_overrun ();
5972 : : }
5973 : :
5974 : : void
5975 : 1707438 : trees_out::lang_decl_bools (tree t, bits_out& bits)
5976 : : {
5977 : : #define WB(X) (bits.b (X))
5978 : 1707438 : const struct lang_decl *lang = DECL_LANG_SPECIFIC (t);
5979 : :
5980 : 1707438 : bits.bflush ();
5981 : 1707438 : WB (lang->u.base.language == lang_cplusplus);
5982 : 1707438 : WB ((lang->u.base.use_template >> 0) & 1);
5983 : 1707438 : WB ((lang->u.base.use_template >> 1) & 1);
5984 : : /* Do not write lang->u.base.not_really_extern, importer will set
5985 : : when reading the definition (if any). */
5986 : 1707438 : WB (lang->u.base.initialized_in_class);
5987 : 1707438 : WB (lang->u.base.threadprivate_or_deleted_p);
5988 : : /* Do not write lang->u.base.anticipated_p, it is a property of the
5989 : : current TU. */
5990 : 1707438 : WB (lang->u.base.friend_or_tls);
5991 : 1707438 : WB (lang->u.base.unknown_bound_p);
5992 : : /* Do not write lang->u.base.odr_used, importer will recalculate if
5993 : : they do ODR use this decl. */
5994 : 1707438 : WB (lang->u.base.concept_p);
5995 : 1707438 : WB (lang->u.base.var_declared_inline_p);
5996 : 1707438 : WB (lang->u.base.dependent_init_p);
5997 : : /* When building a header unit, everthing is marked as purview, (so
5998 : : we know which decls to write). But when we import them we do not
5999 : : want to mark them as in module purview. */
6000 : 3341298 : WB (lang->u.base.module_purview_p && !header_module_p ());
6001 : 1707438 : WB (lang->u.base.module_attach_p);
6002 : 1707438 : WB (lang->u.base.module_keyed_decls_p);
6003 : 1707438 : switch (lang->u.base.selector)
6004 : : {
6005 : 0 : default:
6006 : 0 : gcc_unreachable ();
6007 : :
6008 : 363559 : case lds_fn: /* lang_decl_fn. */
6009 : 363559 : WB (lang->u.fn.global_ctor_p);
6010 : 363559 : WB (lang->u.fn.global_dtor_p);
6011 : 363559 : WB (lang->u.fn.static_function);
6012 : 363559 : WB (lang->u.fn.pure_virtual);
6013 : 363559 : WB (lang->u.fn.defaulted_p);
6014 : 363559 : WB (lang->u.fn.has_in_charge_parm_p);
6015 : 363559 : WB (lang->u.fn.has_vtt_parm_p);
6016 : : /* There shouldn't be a pending inline at this point. */
6017 : 363559 : gcc_assert (!lang->u.fn.pending_inline_p);
6018 : 363559 : WB (lang->u.fn.nonconverting);
6019 : 363559 : WB (lang->u.fn.thunk_p);
6020 : 363559 : WB (lang->u.fn.this_thunk_p);
6021 : : /* Do not stream lang->u.hidden_friend_p, it is a property of
6022 : : the TU. */
6023 : 363559 : WB (lang->u.fn.omp_declare_reduction_p);
6024 : 363559 : WB (lang->u.fn.has_dependent_explicit_spec_p);
6025 : 363559 : WB (lang->u.fn.immediate_fn_p);
6026 : 363559 : WB (lang->u.fn.maybe_deleted);
6027 : : /* We do not stream lang->u.fn.implicit_constexpr. */
6028 : 363559 : WB (lang->u.fn.escalated_p);
6029 : 363559 : WB (lang->u.fn.xobj_func);
6030 : 363559 : goto lds_min;
6031 : :
6032 : 1220 : case lds_decomp: /* lang_decl_decomp. */
6033 : : /* No bools. */
6034 : 1220 : goto lds_min;
6035 : :
6036 : : case lds_min: /* lang_decl_min. */
6037 : 1707438 : lds_min:
6038 : : /* No bools. */
6039 : : break;
6040 : :
6041 : : case lds_ns: /* lang_decl_ns. */
6042 : : /* No bools. */
6043 : : break;
6044 : :
6045 : : case lds_parm: /* lang_decl_parm. */
6046 : : /* No bools. */
6047 : : break;
6048 : : }
6049 : : #undef WB
6050 : 1707438 : }
6051 : :
6052 : : bool
6053 : 1411674 : trees_in::lang_decl_bools (tree t, bits_in& bits)
6054 : : {
6055 : : #define RB(X) ((X) = bits.b ())
6056 : 1411674 : struct lang_decl *lang = DECL_LANG_SPECIFIC (t);
6057 : :
6058 : 1411674 : bits.bflush ();
6059 : 1411674 : lang->u.base.language = bits.b () ? lang_cplusplus : lang_c;
6060 : 1411674 : unsigned v;
6061 : 1411674 : v = bits.b () << 0;
6062 : 1411674 : v |= bits.b () << 1;
6063 : 1411674 : lang->u.base.use_template = v;
6064 : : /* lang->u.base.not_really_extern is not streamed. */
6065 : 1411674 : RB (lang->u.base.initialized_in_class);
6066 : 1411674 : RB (lang->u.base.threadprivate_or_deleted_p);
6067 : : /* lang->u.base.anticipated_p is not streamed. */
6068 : 1411674 : RB (lang->u.base.friend_or_tls);
6069 : 1411674 : RB (lang->u.base.unknown_bound_p);
6070 : : /* lang->u.base.odr_used is not streamed. */
6071 : 1411674 : RB (lang->u.base.concept_p);
6072 : 1411674 : RB (lang->u.base.var_declared_inline_p);
6073 : 1411674 : RB (lang->u.base.dependent_init_p);
6074 : 1411674 : RB (lang->u.base.module_purview_p);
6075 : 1411674 : RB (lang->u.base.module_attach_p);
6076 : 1411674 : RB (lang->u.base.module_keyed_decls_p);
6077 : 1411674 : switch (lang->u.base.selector)
6078 : : {
6079 : 0 : default:
6080 : 0 : gcc_unreachable ();
6081 : :
6082 : 313287 : case lds_fn: /* lang_decl_fn. */
6083 : 313287 : RB (lang->u.fn.global_ctor_p);
6084 : 313287 : RB (lang->u.fn.global_dtor_p);
6085 : 313287 : RB (lang->u.fn.static_function);
6086 : 313287 : RB (lang->u.fn.pure_virtual);
6087 : 313287 : RB (lang->u.fn.defaulted_p);
6088 : 313287 : RB (lang->u.fn.has_in_charge_parm_p);
6089 : 313287 : RB (lang->u.fn.has_vtt_parm_p);
6090 : 313287 : RB (lang->u.fn.nonconverting);
6091 : 313287 : RB (lang->u.fn.thunk_p);
6092 : 313287 : RB (lang->u.fn.this_thunk_p);
6093 : : /* lang->u.fn.hidden_friend_p is not streamed. */
6094 : 313287 : RB (lang->u.fn.omp_declare_reduction_p);
6095 : 313287 : RB (lang->u.fn.has_dependent_explicit_spec_p);
6096 : 313287 : RB (lang->u.fn.immediate_fn_p);
6097 : 313287 : RB (lang->u.fn.maybe_deleted);
6098 : : /* We do not stream lang->u.fn.implicit_constexpr. */
6099 : 313287 : RB (lang->u.fn.escalated_p);
6100 : 313287 : RB (lang->u.fn.xobj_func);
6101 : 313287 : goto lds_min;
6102 : :
6103 : 1154 : case lds_decomp: /* lang_decl_decomp. */
6104 : : /* No bools. */
6105 : 1154 : goto lds_min;
6106 : :
6107 : : case lds_min: /* lang_decl_min. */
6108 : 1411674 : lds_min:
6109 : : /* No bools. */
6110 : : break;
6111 : :
6112 : : case lds_ns: /* lang_decl_ns. */
6113 : : /* No bools. */
6114 : : break;
6115 : :
6116 : : case lds_parm: /* lang_decl_parm. */
6117 : : /* No bools. */
6118 : : break;
6119 : : }
6120 : : #undef RB
6121 : 1411674 : return !get_overrun ();
6122 : : }
6123 : :
6124 : : void
6125 : 141775 : trees_out::lang_type_bools (tree t, bits_out& bits)
6126 : : {
6127 : : #define WB(X) (bits.b (X))
6128 : 141775 : const struct lang_type *lang = TYPE_LANG_SPECIFIC (t);
6129 : :
6130 : 141775 : bits.bflush ();
6131 : 141775 : WB (lang->has_type_conversion);
6132 : 141775 : WB (lang->has_copy_ctor);
6133 : 141775 : WB (lang->has_default_ctor);
6134 : 141775 : WB (lang->const_needs_init);
6135 : 141775 : WB (lang->ref_needs_init);
6136 : 141775 : WB (lang->has_const_copy_assign);
6137 : 141775 : WB ((lang->use_template >> 0) & 1);
6138 : 141775 : WB ((lang->use_template >> 1) & 1);
6139 : :
6140 : 141775 : WB (lang->has_mutable);
6141 : 141775 : WB (lang->com_interface);
6142 : 141775 : WB (lang->non_pod_class);
6143 : 141775 : WB (lang->nearly_empty_p);
6144 : 141775 : WB (lang->user_align);
6145 : 141775 : WB (lang->has_copy_assign);
6146 : 141775 : WB (lang->has_new);
6147 : 141775 : WB (lang->has_array_new);
6148 : :
6149 : 141775 : WB ((lang->gets_delete >> 0) & 1);
6150 : 141775 : WB ((lang->gets_delete >> 1) & 1);
6151 : 141775 : WB (lang->interface_only);
6152 : 141775 : WB (lang->interface_unknown);
6153 : 141775 : WB (lang->contains_empty_class_p);
6154 : 141775 : WB (lang->anon_aggr);
6155 : 141775 : WB (lang->non_zero_init);
6156 : 141775 : WB (lang->empty_p);
6157 : :
6158 : 141775 : WB (lang->vec_new_uses_cookie);
6159 : 141775 : WB (lang->declared_class);
6160 : 141775 : WB (lang->diamond_shaped);
6161 : 141775 : WB (lang->repeated_base);
6162 : 141775 : gcc_assert (!lang->being_defined);
6163 : : // lang->debug_requested
6164 : 141775 : WB (lang->fields_readonly);
6165 : 141775 : WB (lang->ptrmemfunc_flag);
6166 : :
6167 : 141775 : WB (lang->lazy_default_ctor);
6168 : 141775 : WB (lang->lazy_copy_ctor);
6169 : 141775 : WB (lang->lazy_copy_assign);
6170 : 141775 : WB (lang->lazy_destructor);
6171 : 141775 : WB (lang->has_const_copy_ctor);
6172 : 141775 : WB (lang->has_complex_copy_ctor);
6173 : 141775 : WB (lang->has_complex_copy_assign);
6174 : 141775 : WB (lang->non_aggregate);
6175 : :
6176 : 141775 : WB (lang->has_complex_dflt);
6177 : 141775 : WB (lang->has_list_ctor);
6178 : 141775 : WB (lang->non_std_layout);
6179 : 141775 : WB (lang->is_literal);
6180 : 141775 : WB (lang->lazy_move_ctor);
6181 : 141775 : WB (lang->lazy_move_assign);
6182 : 141775 : WB (lang->has_complex_move_ctor);
6183 : 141775 : WB (lang->has_complex_move_assign);
6184 : :
6185 : 141775 : WB (lang->has_constexpr_ctor);
6186 : 141775 : WB (lang->unique_obj_representations);
6187 : 141775 : WB (lang->unique_obj_representations_set);
6188 : : #undef WB
6189 : 141775 : }
6190 : :
6191 : : bool
6192 : 108782 : trees_in::lang_type_bools (tree t, bits_in& bits)
6193 : : {
6194 : : #define RB(X) ((X) = bits.b ())
6195 : 108782 : struct lang_type *lang = TYPE_LANG_SPECIFIC (t);
6196 : :
6197 : 108782 : bits.bflush ();
6198 : 108782 : RB (lang->has_type_conversion);
6199 : 108782 : RB (lang->has_copy_ctor);
6200 : 108782 : RB (lang->has_default_ctor);
6201 : 108782 : RB (lang->const_needs_init);
6202 : 108782 : RB (lang->ref_needs_init);
6203 : 108782 : RB (lang->has_const_copy_assign);
6204 : 108782 : unsigned v;
6205 : 108782 : v = bits.b () << 0;
6206 : 108782 : v |= bits.b () << 1;
6207 : 108782 : lang->use_template = v;
6208 : :
6209 : 108782 : RB (lang->has_mutable);
6210 : 108782 : RB (lang->com_interface);
6211 : 108782 : RB (lang->non_pod_class);
6212 : 108782 : RB (lang->nearly_empty_p);
6213 : 108782 : RB (lang->user_align);
6214 : 108782 : RB (lang->has_copy_assign);
6215 : 108782 : RB (lang->has_new);
6216 : 108782 : RB (lang->has_array_new);
6217 : :
6218 : 108782 : v = bits.b () << 0;
6219 : 108782 : v |= bits.b () << 1;
6220 : 108782 : lang->gets_delete = v;
6221 : 108782 : RB (lang->interface_only);
6222 : 108782 : RB (lang->interface_unknown);
6223 : 108782 : RB (lang->contains_empty_class_p);
6224 : 108782 : RB (lang->anon_aggr);
6225 : 108782 : RB (lang->non_zero_init);
6226 : 108782 : RB (lang->empty_p);
6227 : :
6228 : 108782 : RB (lang->vec_new_uses_cookie);
6229 : 108782 : RB (lang->declared_class);
6230 : 108782 : RB (lang->diamond_shaped);
6231 : 108782 : RB (lang->repeated_base);
6232 : 108782 : gcc_assert (!lang->being_defined);
6233 : 108782 : gcc_assert (!lang->debug_requested);
6234 : 108782 : RB (lang->fields_readonly);
6235 : 108782 : RB (lang->ptrmemfunc_flag);
6236 : :
6237 : 108782 : RB (lang->lazy_default_ctor);
6238 : 108782 : RB (lang->lazy_copy_ctor);
6239 : 108782 : RB (lang->lazy_copy_assign);
6240 : 108782 : RB (lang->lazy_destructor);
6241 : 108782 : RB (lang->has_const_copy_ctor);
6242 : 108782 : RB (lang->has_complex_copy_ctor);
6243 : 108782 : RB (lang->has_complex_copy_assign);
6244 : 108782 : RB (lang->non_aggregate);
6245 : :
6246 : 108782 : RB (lang->has_complex_dflt);
6247 : 108782 : RB (lang->has_list_ctor);
6248 : 108782 : RB (lang->non_std_layout);
6249 : 108782 : RB (lang->is_literal);
6250 : 108782 : RB (lang->lazy_move_ctor);
6251 : 108782 : RB (lang->lazy_move_assign);
6252 : 108782 : RB (lang->has_complex_move_ctor);
6253 : 108782 : RB (lang->has_complex_move_assign);
6254 : :
6255 : 108782 : RB (lang->has_constexpr_ctor);
6256 : 108782 : RB (lang->unique_obj_representations);
6257 : 108782 : RB (lang->unique_obj_representations_set);
6258 : : #undef RB
6259 : 108782 : return !get_overrun ();
6260 : : }
6261 : :
6262 : : /* Read & write the core values and pointers. */
6263 : :
6264 : : void
6265 : 30456137 : trees_out::core_vals (tree t)
6266 : : {
6267 : : #define WU(X) (u (X))
6268 : : #define WT(X) (tree_node (X))
6269 : 30456137 : tree_code code = TREE_CODE (t);
6270 : :
6271 : : /* First by shape of the tree. */
6272 : :
6273 : 30456137 : if (CODE_CONTAINS_STRUCT (code, TS_DECL_MINIMAL))
6274 : : {
6275 : : /* Write this early, for better log information. */
6276 : 6954683 : WT (t->decl_minimal.name);
6277 : 6954683 : if (!DECL_TEMPLATE_PARM_P (t))
6278 : 5364482 : WT (t->decl_minimal.context);
6279 : :
6280 : 6954683 : if (state)
6281 : 5718912 : state->write_location (*this, t->decl_minimal.locus);
6282 : :
6283 : 6954683 : if (streaming_p ())
6284 : 2659800 : if (has_warning_spec (t))
6285 : 516 : u (get_warning_spec (t));
6286 : : }
6287 : :
6288 : 30456137 : if (CODE_CONTAINS_STRUCT (code, TS_TYPE_COMMON))
6289 : : {
6290 : : /* The only types we write also have TYPE_NON_COMMON. */
6291 : 1768059 : gcc_checking_assert (CODE_CONTAINS_STRUCT (code, TS_TYPE_NON_COMMON));
6292 : :
6293 : : /* We only stream the main variant. */
6294 : 1768059 : gcc_checking_assert (TYPE_MAIN_VARIANT (t) == t);
6295 : :
6296 : : /* Stream the name & context first, for better log information */
6297 : 1768059 : WT (t->type_common.name);
6298 : 1768059 : WT (t->type_common.context);
6299 : :
6300 : : /* By construction we want to make sure we have the canonical
6301 : : and main variants already in the type table, so emit them
6302 : : now. */
6303 : 1768059 : WT (t->type_common.main_variant);
6304 : :
6305 : 1768059 : tree canonical = t->type_common.canonical;
6306 : 1768059 : if (canonical && DECL_TEMPLATE_PARM_P (TYPE_NAME (t)))
6307 : : /* We do not want to wander into different templates.
6308 : : Reconstructed on stream in. */
6309 : : canonical = t;
6310 : 1768059 : WT (canonical);
6311 : :
6312 : : /* type_common.next_variant is internally manipulated. */
6313 : : /* type_common.pointer_to, type_common.reference_to. */
6314 : :
6315 : 1768059 : if (streaming_p ())
6316 : : {
6317 : 492588 : WU (t->type_common.precision);
6318 : 492588 : WU (t->type_common.contains_placeholder_bits);
6319 : 492588 : WU (t->type_common.mode);
6320 : 492588 : WU (t->type_common.align);
6321 : : }
6322 : :
6323 : 1768059 : if (!RECORD_OR_UNION_CODE_P (code))
6324 : : {
6325 : 1443643 : WT (t->type_common.size);
6326 : 1443643 : WT (t->type_common.size_unit);
6327 : : }
6328 : 1768059 : WT (t->type_common.attributes);
6329 : :
6330 : 1768059 : WT (t->type_common.common.chain); /* TYPE_STUB_DECL. */
6331 : : }
6332 : :
6333 : 30456137 : if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
6334 : : {
6335 : 6954683 : if (streaming_p ())
6336 : : {
6337 : 2659800 : WU (t->decl_common.mode);
6338 : 2659800 : WU (t->decl_common.off_align);
6339 : 2659800 : WU (t->decl_common.align);
6340 : : }
6341 : :
6342 : : /* For templates these hold instantiation (partial and/or
6343 : : specialization) information. */
6344 : 6954683 : if (code != TEMPLATE_DECL)
6345 : : {
6346 : 6400976 : WT (t->decl_common.size);
6347 : 6400976 : WT (t->decl_common.size_unit);
6348 : : }
6349 : :
6350 : 6954683 : WT (t->decl_common.attributes);
6351 : : // FIXME: Does this introduce cross-decl links? For instance
6352 : : // from instantiation to the template. If so, we'll need more
6353 : : // deduplication logic. I think we'll need to walk the blocks
6354 : : // of the owning function_decl's abstract origin in tandem, to
6355 : : // generate the locating data needed?
6356 : 6954683 : WT (t->decl_common.abstract_origin);
6357 : : }
6358 : :
6359 : 30456137 : if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
6360 : : {
6361 : 3310146 : WT (t->decl_with_vis.assembler_name);
6362 : 3310146 : if (streaming_p ())
6363 : 1259031 : WU (t->decl_with_vis.visibility);
6364 : : }
6365 : :
6366 : 30456137 : if (CODE_CONTAINS_STRUCT (code, TS_TYPE_NON_COMMON))
6367 : : {
6368 : 1768059 : if (code == ENUMERAL_TYPE)
6369 : : {
6370 : : /* These fields get set even for opaque enums that lack a
6371 : : definition, so we stream them directly for each ENUMERAL_TYPE.
6372 : : We stream TYPE_VALUES as part of the definition. */
6373 : 7757 : WT (t->type_non_common.maxval);
6374 : 7757 : WT (t->type_non_common.minval);
6375 : : }
6376 : : /* Records and unions hold FIELDS, VFIELD & BINFO on these
6377 : : things. */
6378 : 1760302 : else if (!RECORD_OR_UNION_CODE_P (code))
6379 : : {
6380 : : // FIXME: These are from tpl_parm_value's 'type' writing.
6381 : : // Perhaps it should just be doing them directly?
6382 : 1435886 : gcc_checking_assert (code == TEMPLATE_TYPE_PARM
6383 : : || code == TEMPLATE_TEMPLATE_PARM
6384 : : || code == BOUND_TEMPLATE_TEMPLATE_PARM);
6385 : 1435886 : gcc_checking_assert (!TYPE_CACHED_VALUES_P (t));
6386 : 1435886 : WT (t->type_non_common.values);
6387 : 1435886 : WT (t->type_non_common.maxval);
6388 : 1435886 : WT (t->type_non_common.minval);
6389 : : }
6390 : :
6391 : 1768059 : WT (t->type_non_common.lang_1);
6392 : : }
6393 : :
6394 : 30456137 : if (CODE_CONTAINS_STRUCT (code, TS_EXP))
6395 : : {
6396 : 8789081 : if (state)
6397 : 8610185 : state->write_location (*this, t->exp.locus);
6398 : :
6399 : 8789081 : if (streaming_p ())
6400 : 4266498 : if (has_warning_spec (t))
6401 : 347644 : u (get_warning_spec (t));
6402 : :
6403 : : /* Walk in forward order, as (for instance) REQUIRES_EXPR has a
6404 : : bunch of unscoped parms on its first operand. It's safer to
6405 : : create those in order. */
6406 : 8789081 : bool vl = TREE_CODE_CLASS (code) == tcc_vl_exp;
6407 : 32313076 : for (unsigned limit = (vl ? VL_EXP_OPERAND_LENGTH (t)
6408 : 7872328 : : TREE_OPERAND_LENGTH (t)),
6409 : 24440748 : ix = unsigned (vl); ix != limit; ix++)
6410 : 15651667 : WT (TREE_OPERAND (t, ix));
6411 : : }
6412 : : else
6413 : : /* The CODE_CONTAINS tables were inaccurate when I started. */
6414 : 21667056 : gcc_checking_assert (TREE_CODE_CLASS (code) != tcc_expression
6415 : : && TREE_CODE_CLASS (code) != tcc_binary
6416 : : && TREE_CODE_CLASS (code) != tcc_unary
6417 : : && TREE_CODE_CLASS (code) != tcc_reference
6418 : : && TREE_CODE_CLASS (code) != tcc_comparison
6419 : : && TREE_CODE_CLASS (code) != tcc_statement
6420 : : && TREE_CODE_CLASS (code) != tcc_vl_exp);
6421 : :
6422 : : /* Then by CODE. Special cases and/or 1:1 tree shape
6423 : : correspondance. */
6424 : 30456137 : switch (code)
6425 : : {
6426 : : default:
6427 : : break;
6428 : :
6429 : 0 : case ARGUMENT_PACK_SELECT: /* Transient during instantiation. */
6430 : 0 : case DEFERRED_PARSE: /* Expanded upon completion of
6431 : : outermost class. */
6432 : 0 : case IDENTIFIER_NODE: /* Streamed specially. */
6433 : 0 : case BINDING_VECTOR: /* Only in namespace-scope symbol
6434 : : table. */
6435 : 0 : case SSA_NAME:
6436 : 0 : case TRANSLATION_UNIT_DECL: /* There is only one, it is a
6437 : : global_tree. */
6438 : 0 : case USERDEF_LITERAL: /* Expanded during parsing. */
6439 : 0 : gcc_unreachable (); /* Should never meet. */
6440 : :
6441 : : /* Constants. */
6442 : 18 : case COMPLEX_CST:
6443 : 18 : WT (TREE_REALPART (t));
6444 : 18 : WT (TREE_IMAGPART (t));
6445 : 18 : break;
6446 : :
6447 : 0 : case FIXED_CST:
6448 : 0 : gcc_unreachable (); /* Not supported in C++. */
6449 : :
6450 : 2895863 : case INTEGER_CST:
6451 : 2895863 : if (streaming_p ())
6452 : : {
6453 : 523034 : unsigned num = TREE_INT_CST_EXT_NUNITS (t);
6454 : 1047522 : for (unsigned ix = 0; ix != num; ix++)
6455 : 524488 : wu (TREE_INT_CST_ELT (t, ix));
6456 : : }
6457 : : break;
6458 : :
6459 : 0 : case POLY_INT_CST:
6460 : 0 : if (streaming_p ())
6461 : 0 : for (unsigned ix = 0; ix != NUM_POLY_INT_COEFFS; ix++)
6462 : 0 : WT (POLY_INT_CST_COEFF (t, ix));
6463 : : break;
6464 : :
6465 : 24768 : case REAL_CST:
6466 : 24768 : if (streaming_p ())
6467 : 12360 : buf (TREE_REAL_CST_PTR (t), sizeof (real_value));
6468 : : break;
6469 : :
6470 : : case STRING_CST:
6471 : : /* Streamed during start. */
6472 : : break;
6473 : :
6474 : 36 : case RAW_DATA_CST:
6475 : 36 : if (RAW_DATA_OWNER (t) == NULL_TREE)
6476 : : break; /* Streamed as STRING_CST during start. */
6477 : 24 : WT (RAW_DATA_OWNER (t));
6478 : 24 : if (streaming_p ())
6479 : : {
6480 : 12 : if (TREE_CODE (RAW_DATA_OWNER (t)) == RAW_DATA_CST)
6481 : 6 : z (RAW_DATA_POINTER (t) - RAW_DATA_POINTER (RAW_DATA_OWNER (t)));
6482 : 6 : else if (TREE_CODE (RAW_DATA_OWNER (t)) == STRING_CST)
6483 : 6 : z (RAW_DATA_POINTER (t)
6484 : 6 : - TREE_STRING_POINTER (RAW_DATA_OWNER (t)));
6485 : : else
6486 : 0 : gcc_unreachable ();
6487 : : }
6488 : : break;
6489 : :
6490 : 36 : case VECTOR_CST:
6491 : 102 : for (unsigned ix = vector_cst_encoded_nelts (t); ix--;)
6492 : 66 : WT (VECTOR_CST_ENCODED_ELT (t, ix));
6493 : : break;
6494 : :
6495 : : /* Decls. */
6496 : 333834 : case VAR_DECL:
6497 : 333834 : if (DECL_CONTEXT (t)
6498 : 333834 : && TREE_CODE (DECL_CONTEXT (t)) != FUNCTION_DECL)
6499 : : {
6500 : 71182 : if (DECL_HAS_VALUE_EXPR_P (t))
6501 : 18 : WT (DECL_VALUE_EXPR (t));
6502 : : break;
6503 : : }
6504 : : /* FALLTHROUGH */
6505 : :
6506 : 3095638 : case RESULT_DECL:
6507 : 3095638 : case PARM_DECL:
6508 : 3095638 : if (DECL_HAS_VALUE_EXPR_P (t))
6509 : 23881 : WT (DECL_VALUE_EXPR (t));
6510 : : /* FALLTHROUGH */
6511 : :
6512 : 3219877 : case CONST_DECL:
6513 : 3219877 : case IMPORTED_DECL:
6514 : 3219877 : WT (t->decl_common.initial);
6515 : 3219877 : break;
6516 : :
6517 : 95631 : case FIELD_DECL:
6518 : 95631 : WT (t->field_decl.offset);
6519 : 95631 : WT (t->field_decl.bit_field_type);
6520 : 95631 : WT (t->field_decl.qualifier); /* bitfield unit. */
6521 : 95631 : WT (t->field_decl.bit_offset);
6522 : 95631 : WT (t->field_decl.fcontext);
6523 : 95631 : WT (t->decl_common.initial);
6524 : 95631 : break;
6525 : :
6526 : 26494 : case LABEL_DECL:
6527 : 26494 : if (streaming_p ())
6528 : : {
6529 : 13247 : WU (t->label_decl.label_decl_uid);
6530 : 13247 : WU (t->label_decl.eh_landing_pad_nr);
6531 : : }
6532 : : break;
6533 : :
6534 : 727285 : case FUNCTION_DECL:
6535 : 727285 : if (streaming_p ())
6536 : : {
6537 : : /* Builtins can be streamed by value when a header declares
6538 : : them. */
6539 : 363559 : WU (DECL_BUILT_IN_CLASS (t));
6540 : 363559 : if (DECL_BUILT_IN_CLASS (t) != NOT_BUILT_IN)
6541 : 7100 : WU (DECL_UNCHECKED_FUNCTION_CODE (t));
6542 : : }
6543 : :
6544 : 727285 : WT (t->function_decl.personality);
6545 : 727285 : WT (t->function_decl.function_specific_target);
6546 : 727285 : WT (t->function_decl.function_specific_optimization);
6547 : 727285 : WT (t->function_decl.vindex);
6548 : :
6549 : 727285 : if (DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (t))
6550 : 4274 : WT (lookup_explicit_specifier (t));
6551 : : break;
6552 : :
6553 : 77647 : case USING_DECL:
6554 : : /* USING_DECL_DECLS */
6555 : 77647 : WT (t->decl_common.initial);
6556 : : /* FALLTHROUGH */
6557 : :
6558 : 2248774 : case TYPE_DECL:
6559 : : /* USING_DECL: USING_DECL_SCOPE */
6560 : : /* TYPE_DECL: DECL_ORIGINAL_TYPE */
6561 : 2248774 : WT (t->decl_non_common.result);
6562 : 2248774 : break;
6563 : :
6564 : : /* Miscellaneous common nodes. */
6565 : 455202 : case BLOCK:
6566 : 455202 : if (state)
6567 : : {
6568 : 455202 : state->write_location (*this, t->block.locus);
6569 : 455202 : state->write_location (*this, t->block.end_locus);
6570 : : }
6571 : :
6572 : : /* DECL_LOCAL_DECL_P decls are first encountered here and
6573 : : streamed by value. */
6574 : 678124 : for (tree decls = t->block.vars; decls; decls = DECL_CHAIN (decls))
6575 : : {
6576 : 222922 : if (VAR_OR_FUNCTION_DECL_P (decls)
6577 : 222922 : && DECL_LOCAL_DECL_P (decls))
6578 : : {
6579 : : /* Make sure this is the first encounter, and mark for
6580 : : walk-by-value. */
6581 : 220 : gcc_checking_assert (!TREE_VISITED (decls)
6582 : : && !DECL_TEMPLATE_INFO (decls));
6583 : 220 : mark_by_value (decls);
6584 : : }
6585 : 222922 : tree_node (decls);
6586 : : }
6587 : 455202 : tree_node (NULL_TREE);
6588 : :
6589 : : /* nonlocalized_vars is a middle-end thing. */
6590 : 455202 : WT (t->block.subblocks);
6591 : 455202 : WT (t->block.supercontext);
6592 : : // FIXME: As for decl's abstract_origin, does this introduce crosslinks?
6593 : 455202 : WT (t->block.abstract_origin);
6594 : : /* fragment_origin, fragment_chain are middle-end things. */
6595 : 455202 : WT (t->block.chain);
6596 : : /* nonlocalized_vars, block_num & die are middle endy/debug
6597 : : things. */
6598 : 455202 : break;
6599 : :
6600 : 896597 : case CALL_EXPR:
6601 : 896597 : if (streaming_p ())
6602 : 428830 : WU (t->base.u.ifn);
6603 : : break;
6604 : :
6605 : : case CONSTRUCTOR:
6606 : : // This must be streamed /after/ we've streamed the type,
6607 : : // because it can directly refer to elements of the type. Eg,
6608 : : // FIELD_DECLs of a RECORD_TYPE.
6609 : : break;
6610 : :
6611 : 24 : case OMP_CLAUSE:
6612 : 24 : {
6613 : : /* The ompcode is serialized in start. */
6614 : 24 : if (streaming_p ())
6615 : 12 : WU (t->omp_clause.subcode.map_kind);
6616 : 24 : if (state)
6617 : 24 : state->write_location (*this, t->omp_clause.locus);
6618 : :
6619 : 24 : unsigned len = omp_clause_num_ops[OMP_CLAUSE_CODE (t)];
6620 : 78 : for (unsigned ix = 0; ix != len; ix++)
6621 : 54 : WT (t->omp_clause.ops[ix]);
6622 : : }
6623 : : break;
6624 : :
6625 : 319805 : case STATEMENT_LIST:
6626 : 1253459 : for (tree stmt : tsi_range (t))
6627 : 933654 : if (stmt)
6628 : 933654 : WT (stmt);
6629 : 319805 : WT (NULL_TREE);
6630 : 319805 : break;
6631 : :
6632 : 0 : case OPTIMIZATION_NODE:
6633 : 0 : case TARGET_OPTION_NODE:
6634 : : // FIXME: Our representation for these two nodes is a cache of
6635 : : // the resulting set of options. Not a record of the options
6636 : : // that got changed by a particular attribute or pragma. Should
6637 : : // we record that, or should we record the diff from the command
6638 : : // line options? The latter seems the right behaviour, but is
6639 : : // (a) harder, and I guess could introduce strangeness if the
6640 : : // importer has set some incompatible set of optimization flags?
6641 : 0 : gcc_unreachable ();
6642 : 196118 : break;
6643 : :
6644 : 196118 : case TREE_BINFO:
6645 : 196118 : {
6646 : 196118 : WT (t->binfo.common.chain);
6647 : 196118 : WT (t->binfo.offset);
6648 : 196118 : WT (t->binfo.inheritance);
6649 : 196118 : WT (t->binfo.vptr_field);
6650 : :
6651 : 196118 : WT (t->binfo.vtable);
6652 : 196118 : WT (t->binfo.virtuals);
6653 : 196118 : WT (t->binfo.vtt_subvtt);
6654 : 196118 : WT (t->binfo.vtt_vptr);
6655 : :
6656 : 196118 : tree_vec (BINFO_BASE_ACCESSES (t));
6657 : 196118 : unsigned num = vec_safe_length (BINFO_BASE_ACCESSES (t));
6658 : 253384 : for (unsigned ix = 0; ix != num; ix++)
6659 : 57266 : WT (BINFO_BASE_BINFO (t, ix));
6660 : : }
6661 : : break;
6662 : :
6663 : 2395340 : case TREE_LIST:
6664 : 2395340 : WT (t->list.purpose);
6665 : 2395340 : WT (t->list.value);
6666 : 2395340 : WT (t->list.common.chain);
6667 : 2395340 : break;
6668 : :
6669 : 2055017 : case TREE_VEC:
6670 : 5645923 : for (unsigned ix = TREE_VEC_LENGTH (t); ix--;)
6671 : 3590906 : WT (TREE_VEC_ELT (t, ix));
6672 : : /* We stash NON_DEFAULT_TEMPLATE_ARGS_COUNT on TREE_CHAIN! */
6673 : 2055017 : gcc_checking_assert (!t->type_common.common.chain
6674 : : || (TREE_CODE (t->type_common.common.chain)
6675 : : == INTEGER_CST));
6676 : 2055017 : WT (t->type_common.common.chain);
6677 : 2055017 : break;
6678 : :
6679 : : /* C++-specific nodes ... */
6680 : 160022 : case BASELINK:
6681 : 160022 : WT (((lang_tree_node *)t)->baselink.binfo);
6682 : 160022 : WT (((lang_tree_node *)t)->baselink.functions);
6683 : 160022 : WT (((lang_tree_node *)t)->baselink.access_binfo);
6684 : 160022 : break;
6685 : :
6686 : 64715 : case CONSTRAINT_INFO:
6687 : 64715 : WT (((lang_tree_node *)t)->constraint_info.template_reqs);
6688 : 64715 : WT (((lang_tree_node *)t)->constraint_info.declarator_reqs);
6689 : 64715 : WT (((lang_tree_node *)t)->constraint_info.associated_constr);
6690 : 64715 : break;
6691 : :
6692 : 10616 : case DEFERRED_NOEXCEPT:
6693 : 10616 : WT (((lang_tree_node *)t)->deferred_noexcept.pattern);
6694 : 10616 : WT (((lang_tree_node *)t)->deferred_noexcept.args);
6695 : 10616 : break;
6696 : :
6697 : 8153 : case LAMBDA_EXPR:
6698 : 8153 : WT (((lang_tree_node *)t)->lambda_expression.capture_list);
6699 : 8153 : WT (((lang_tree_node *)t)->lambda_expression.this_capture);
6700 : 8153 : WT (((lang_tree_node *)t)->lambda_expression.extra_scope);
6701 : 8153 : WT (((lang_tree_node *)t)->lambda_expression.regen_info);
6702 : 8153 : WT (((lang_tree_node *)t)->lambda_expression.extra_args);
6703 : : /* pending_proxies is a parse-time thing. */
6704 : 8153 : gcc_assert (!((lang_tree_node *)t)->lambda_expression.pending_proxies);
6705 : 8153 : if (state)
6706 : 8153 : state->write_location
6707 : 8153 : (*this, ((lang_tree_node *)t)->lambda_expression.locus);
6708 : 8153 : if (streaming_p ())
6709 : : {
6710 : 2754 : WU (((lang_tree_node *)t)->lambda_expression.default_capture_mode);
6711 : 2754 : WU (((lang_tree_node *)t)->lambda_expression.discriminator_scope);
6712 : 2754 : WU (((lang_tree_node *)t)->lambda_expression.discriminator_sig);
6713 : : }
6714 : : break;
6715 : :
6716 : 1414567 : case OVERLOAD:
6717 : 1414567 : WT (((lang_tree_node *)t)->overload.function);
6718 : 1414567 : WT (t->common.chain);
6719 : 1414567 : break;
6720 : :
6721 : 0 : case PTRMEM_CST:
6722 : 0 : WT (((lang_tree_node *)t)->ptrmem.member);
6723 : 0 : break;
6724 : :
6725 : 12302 : case STATIC_ASSERT:
6726 : 12302 : WT (((lang_tree_node *)t)->static_assertion.condition);
6727 : 12302 : WT (((lang_tree_node *)t)->static_assertion.message);
6728 : 12302 : if (state)
6729 : 12302 : state->write_location
6730 : 12302 : (*this, ((lang_tree_node *)t)->static_assertion.location);
6731 : : break;
6732 : :
6733 : 553707 : case TEMPLATE_DECL:
6734 : : /* Streamed with the template_decl node itself. */
6735 : 553707 : gcc_checking_assert
6736 : : (TREE_VISITED (((lang_tree_node *)t)->template_decl.arguments));
6737 : 553707 : gcc_checking_assert
6738 : : (TREE_VISITED (((lang_tree_node *)t)->template_decl.result));
6739 : 553707 : if (DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (t))
6740 : 8324 : WT (DECL_CHAIN (t));
6741 : : break;
6742 : :
6743 : 1239862 : case TEMPLATE_INFO:
6744 : 1239862 : {
6745 : 1239862 : WT (((lang_tree_node *)t)->template_info.tmpl);
6746 : 1239862 : WT (((lang_tree_node *)t)->template_info.args);
6747 : 1239862 : WT (((lang_tree_node *)t)->template_info.partial);
6748 : :
6749 : 1239862 : const auto *ac = (((lang_tree_node *)t)
6750 : : ->template_info.deferred_access_checks);
6751 : 1239862 : unsigned len = vec_safe_length (ac);
6752 : 1239862 : if (streaming_p ())
6753 : 618503 : u (len);
6754 : 1239862 : if (len)
6755 : : {
6756 : 0 : for (unsigned ix = 0; ix != len; ix++)
6757 : : {
6758 : 0 : const auto &m = (*ac)[ix];
6759 : 0 : WT (m.binfo);
6760 : 0 : WT (m.decl);
6761 : 0 : WT (m.diag_decl);
6762 : 0 : if (state)
6763 : 0 : state->write_location (*this, m.loc);
6764 : : }
6765 : : }
6766 : : }
6767 : : break;
6768 : :
6769 : 1534430 : case TEMPLATE_PARM_INDEX:
6770 : 1534430 : if (streaming_p ())
6771 : : {
6772 : 346512 : WU (((lang_tree_node *)t)->tpi.index);
6773 : 346512 : WU (((lang_tree_node *)t)->tpi.level);
6774 : 346512 : WU (((lang_tree_node *)t)->tpi.orig_level);
6775 : : }
6776 : 1534430 : WT (((lang_tree_node *)t)->tpi.decl);
6777 : : /* TEMPLATE_PARM_DESCENDANTS (AKA TREE_CHAIN) is an internal
6778 : : cache, do not stream. */
6779 : 1534430 : break;
6780 : :
6781 : 23556 : case TRAIT_EXPR:
6782 : 23556 : WT (((lang_tree_node *)t)->trait_expression.type1);
6783 : 23556 : WT (((lang_tree_node *)t)->trait_expression.type2);
6784 : 23556 : if (streaming_p ())
6785 : 9066 : WU (((lang_tree_node *)t)->trait_expression.kind);
6786 : : break;
6787 : : }
6788 : :
6789 : 30456137 : if (CODE_CONTAINS_STRUCT (code, TS_TYPED))
6790 : : {
6791 : : /* We want to stream the type of a expression-like nodes /after/
6792 : : we've streamed the operands. The type often contains (bits
6793 : : of the) types of the operands, and with things like decltype
6794 : : and noexcept in play, we really want to stream the decls
6795 : : defining the type before we try and stream the type on its
6796 : : own. Otherwise we can find ourselves trying to read in a
6797 : : decl, when we're already partially reading in a component of
6798 : : its type. And that's bad. */
6799 : 28673440 : tree type = t->typed.type;
6800 : 28673440 : unsigned prec = 0;
6801 : :
6802 : 28673440 : switch (code)
6803 : : {
6804 : : default:
6805 : : break;
6806 : :
6807 : : case TEMPLATE_DECL:
6808 : : /* We fill in the template's type separately. */
6809 : 28673440 : type = NULL_TREE;
6810 : : break;
6811 : :
6812 : 2171127 : case TYPE_DECL:
6813 : 2171127 : if (DECL_ORIGINAL_TYPE (t) && t == TYPE_NAME (type))
6814 : : /* This is a typedef. We set its type separately. */
6815 : : type = NULL_TREE;
6816 : : break;
6817 : :
6818 : 7757 : case ENUMERAL_TYPE:
6819 : 7757 : if (type && !ENUM_FIXED_UNDERLYING_TYPE_P (t))
6820 : : {
6821 : : /* Type is a restricted range integer type derived from the
6822 : : integer_types. Find the right one. */
6823 : 5135 : prec = TYPE_PRECISION (type);
6824 : 5135 : tree name = DECL_NAME (TYPE_NAME (type));
6825 : :
6826 : 66987 : for (unsigned itk = itk_none; itk--;)
6827 : 66987 : if (integer_types[itk]
6828 : 66987 : && DECL_NAME (TYPE_NAME (integer_types[itk])) == name)
6829 : : {
6830 : : type = integer_types[itk];
6831 : : break;
6832 : : }
6833 : 5135 : gcc_assert (type != t->typed.type);
6834 : : }
6835 : : break;
6836 : : }
6837 : :
6838 : 28673440 : WT (type);
6839 : 28673440 : if (prec && streaming_p ())
6840 : 2566 : WU (prec);
6841 : : }
6842 : :
6843 : 30456137 : if (TREE_CODE (t) == CONSTRUCTOR)
6844 : : {
6845 : 74175 : unsigned len = vec_safe_length (t->constructor.elts);
6846 : 74175 : if (streaming_p ())
6847 : 36377 : WU (len);
6848 : 74175 : if (len)
6849 : 210974 : for (unsigned ix = 0; ix != len; ix++)
6850 : : {
6851 : 188685 : const constructor_elt &elt = (*t->constructor.elts)[ix];
6852 : :
6853 : 188685 : WT (elt.index);
6854 : 188685 : WT (elt.value);
6855 : : }
6856 : : }
6857 : :
6858 : : #undef WT
6859 : : #undef WU
6860 : 30456137 : }
6861 : :
6862 : : // Streaming in a reference to a decl can cause that decl to be
6863 : : // TREE_USED, which is the mark_used behaviour we need most of the
6864 : : // time. The trees_in::unused can be incremented to inhibit this,
6865 : : // which is at least needed for vtables.
6866 : :
6867 : : bool
6868 : 9939000 : trees_in::core_vals (tree t)
6869 : : {
6870 : : #define RU(X) ((X) = u ())
6871 : : #define RUC(T,X) ((X) = T (u ()))
6872 : : #define RT(X) ((X) = tree_node ())
6873 : : #define RTU(X) ((X) = tree_node (true))
6874 : 9939000 : tree_code code = TREE_CODE (t);
6875 : :
6876 : : /* First by tree shape. */
6877 : 9939000 : if (CODE_CONTAINS_STRUCT (code, TS_DECL_MINIMAL))
6878 : : {
6879 : 2208158 : RT (t->decl_minimal.name);
6880 : 2208158 : if (!DECL_TEMPLATE_PARM_P (t))
6881 : 1920514 : RT (t->decl_minimal.context);
6882 : :
6883 : : /* Don't zap the locus just yet, we don't record it correctly
6884 : : and thus lose all location information. */
6885 : 2208158 : t->decl_minimal.locus = state->read_location (*this);
6886 : 2208158 : if (has_warning_spec (t))
6887 : 407 : put_warning_spec (t, u ());
6888 : : }
6889 : :
6890 : 9939000 : if (CODE_CONTAINS_STRUCT (code, TS_TYPE_COMMON))
6891 : : {
6892 : 384580 : RT (t->type_common.name);
6893 : 384580 : RT (t->type_common.context);
6894 : :
6895 : 384580 : RT (t->type_common.main_variant);
6896 : 384580 : RT (t->type_common.canonical);
6897 : :
6898 : : /* type_common.next_variant is internally manipulated. */
6899 : : /* type_common.pointer_to, type_common.reference_to. */
6900 : :
6901 : 384580 : RU (t->type_common.precision);
6902 : 384580 : RU (t->type_common.contains_placeholder_bits);
6903 : 384580 : RUC (machine_mode, t->type_common.mode);
6904 : 384580 : RU (t->type_common.align);
6905 : :
6906 : 384580 : if (!RECORD_OR_UNION_CODE_P (code))
6907 : : {
6908 : 261372 : RT (t->type_common.size);
6909 : 261372 : RT (t->type_common.size_unit);
6910 : : }
6911 : 384580 : RT (t->type_common.attributes);
6912 : :
6913 : 384580 : RT (t->type_common.common.chain); /* TYPE_STUB_DECL. */
6914 : : }
6915 : :
6916 : 9939000 : if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
6917 : : {
6918 : 2208158 : RUC (machine_mode, t->decl_common.mode);
6919 : 2208158 : RU (t->decl_common.off_align);
6920 : 2208158 : RU (t->decl_common.align);
6921 : :
6922 : 2208158 : if (code != TEMPLATE_DECL)
6923 : : {
6924 : 1984060 : RT (t->decl_common.size);
6925 : 1984060 : RT (t->decl_common.size_unit);
6926 : : }
6927 : :
6928 : 2208158 : RT (t->decl_common.attributes);
6929 : 2208158 : RT (t->decl_common.abstract_origin);
6930 : : }
6931 : :
6932 : 9939000 : if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
6933 : : {
6934 : 1023284 : RT (t->decl_with_vis.assembler_name);
6935 : 1023284 : RUC (symbol_visibility, t->decl_with_vis.visibility);
6936 : : }
6937 : :
6938 : 9939000 : if (CODE_CONTAINS_STRUCT (code, TS_TYPE_NON_COMMON))
6939 : : {
6940 : 384580 : if (code == ENUMERAL_TYPE)
6941 : : {
6942 : : /* These fields get set even for opaque enums that lack a
6943 : : definition, so we stream them directly for each ENUMERAL_TYPE.
6944 : : We stream TYPE_VALUES as part of the definition. */
6945 : 2502 : RT (t->type_non_common.maxval);
6946 : 2502 : RT (t->type_non_common.minval);
6947 : : }
6948 : : /* Records and unions hold FIELDS, VFIELD & BINFO on these
6949 : : things. */
6950 : 382078 : else if (!RECORD_OR_UNION_CODE_P (code))
6951 : : {
6952 : : /* This is not clobbering TYPE_CACHED_VALUES, because this
6953 : : is a type that doesn't have any. */
6954 : 258870 : gcc_checking_assert (!TYPE_CACHED_VALUES_P (t));
6955 : 258870 : RT (t->type_non_common.values);
6956 : 258870 : RT (t->type_non_common.maxval);
6957 : 258870 : RT (t->type_non_common.minval);
6958 : : }
6959 : :
6960 : 384580 : RT (t->type_non_common.lang_1);
6961 : : }
6962 : :
6963 : 9939000 : if (CODE_CONTAINS_STRUCT (code, TS_EXP))
6964 : : {
6965 : 3750151 : t->exp.locus = state->read_location (*this);
6966 : 3750151 : if (has_warning_spec (t))
6967 : 305618 : put_warning_spec (t, u ());
6968 : :
6969 : 3750151 : bool vl = TREE_CODE_CLASS (code) == tcc_vl_exp;
6970 : 7118997 : for (unsigned limit = (vl ? VL_EXP_OPERAND_LENGTH (t)
6971 : 3368846 : : TREE_OPERAND_LENGTH (t)),
6972 : 10419876 : ix = unsigned (vl); ix != limit; ix++)
6973 : 6669725 : RTU (TREE_OPERAND (t, ix));
6974 : : }
6975 : :
6976 : : /* Then by CODE. Special cases and/or 1:1 tree shape
6977 : : correspondance. */
6978 : 9939000 : switch (code)
6979 : : {
6980 : : default:
6981 : : break;
6982 : :
6983 : : case ARGUMENT_PACK_SELECT:
6984 : : case DEFERRED_PARSE:
6985 : : case IDENTIFIER_NODE:
6986 : : case BINDING_VECTOR:
6987 : : case SSA_NAME:
6988 : : case TRANSLATION_UNIT_DECL:
6989 : : case USERDEF_LITERAL:
6990 : : return false; /* Should never meet. */
6991 : :
6992 : : /* Constants. */
6993 : 9 : case COMPLEX_CST:
6994 : 9 : RT (TREE_REALPART (t));
6995 : 9 : RT (TREE_IMAGPART (t));
6996 : 9 : break;
6997 : :
6998 : : case FIXED_CST:
6999 : : /* Not suported in C++. */
7000 : : return false;
7001 : :
7002 : 425234 : case INTEGER_CST:
7003 : 425234 : {
7004 : 425234 : unsigned num = TREE_INT_CST_EXT_NUNITS (t);
7005 : 851719 : for (unsigned ix = 0; ix != num; ix++)
7006 : 426485 : TREE_INT_CST_ELT (t, ix) = wu ();
7007 : : }
7008 : : break;
7009 : :
7010 : : case POLY_INT_CST:
7011 : 0 : for (unsigned ix = 0; ix != NUM_POLY_INT_COEFFS; ix++)
7012 : 0 : RT (POLY_INT_CST_COEFF (t, ix));
7013 : : break;
7014 : :
7015 : 13444 : case REAL_CST:
7016 : 13444 : if (const void *bytes = buf (sizeof (real_value)))
7017 : 13444 : memcpy (TREE_REAL_CST_PTR (t), bytes, sizeof (real_value));
7018 : : break;
7019 : :
7020 : : case STRING_CST:
7021 : : /* Streamed during start. */
7022 : : break;
7023 : :
7024 : 6 : case RAW_DATA_CST:
7025 : 6 : RT (RAW_DATA_OWNER (t));
7026 : 6 : gcc_assert (TREE_CODE (RAW_DATA_OWNER (t)) == STRING_CST
7027 : : && TREE_STRING_LENGTH (RAW_DATA_OWNER (t)));
7028 : 6 : RAW_DATA_POINTER (t) = TREE_STRING_POINTER (RAW_DATA_OWNER (t)) + z ();
7029 : 6 : break;
7030 : :
7031 : 24 : case VECTOR_CST:
7032 : 63 : for (unsigned ix = vector_cst_encoded_nelts (t); ix--;)
7033 : 39 : RT (VECTOR_CST_ENCODED_ELT (t, ix));
7034 : : break;
7035 : :
7036 : : /* Decls. */
7037 : 143642 : case VAR_DECL:
7038 : 143642 : if (DECL_CONTEXT (t)
7039 : 143642 : && TREE_CODE (DECL_CONTEXT (t)) != FUNCTION_DECL)
7040 : : {
7041 : 28428 : if (DECL_HAS_VALUE_EXPR_P (t))
7042 : : {
7043 : 9 : tree val = tree_node ();
7044 : 9 : SET_DECL_VALUE_EXPR (t, val);
7045 : : }
7046 : : break;
7047 : : }
7048 : : /* FALLTHROUGH */
7049 : :
7050 : 994304 : case RESULT_DECL:
7051 : 994304 : case PARM_DECL:
7052 : 994304 : if (DECL_HAS_VALUE_EXPR_P (t))
7053 : : {
7054 : : /* The DECL_VALUE hash table is a cache, thus if we're
7055 : : reading a duplicate (which we end up discarding), the
7056 : : value expr will also be cleaned up at the next gc. */
7057 : 9319 : tree val = tree_node ();
7058 : 9319 : SET_DECL_VALUE_EXPR (t, val);
7059 : : }
7060 : : /* FALLTHROUGH */
7061 : :
7062 : 1020988 : case CONST_DECL:
7063 : 1020988 : case IMPORTED_DECL:
7064 : 1020988 : RT (t->decl_common.initial);
7065 : 1020988 : break;
7066 : :
7067 : 39253 : case FIELD_DECL:
7068 : 39253 : RT (t->field_decl.offset);
7069 : 39253 : RT (t->field_decl.bit_field_type);
7070 : 39253 : RT (t->field_decl.qualifier);
7071 : 39253 : RT (t->field_decl.bit_offset);
7072 : 39253 : RT (t->field_decl.fcontext);
7073 : 39253 : RT (t->decl_common.initial);
7074 : 39253 : break;
7075 : :
7076 : 11157 : case LABEL_DECL:
7077 : 11157 : RU (t->label_decl.label_decl_uid);
7078 : 11157 : RU (t->label_decl.eh_landing_pad_nr);
7079 : 11157 : break;
7080 : :
7081 : 313287 : case FUNCTION_DECL:
7082 : 313287 : {
7083 : 313287 : unsigned bltin = u ();
7084 : 313287 : t->function_decl.built_in_class = built_in_class (bltin);
7085 : 313287 : if (bltin != NOT_BUILT_IN)
7086 : : {
7087 : 6462 : bltin = u ();
7088 : 6462 : DECL_UNCHECKED_FUNCTION_CODE (t) = built_in_function (bltin);
7089 : : }
7090 : :
7091 : 313287 : RT (t->function_decl.personality);
7092 : 313287 : RT (t->function_decl.function_specific_target);
7093 : 313287 : RT (t->function_decl.function_specific_optimization);
7094 : 313287 : RT (t->function_decl.vindex);
7095 : :
7096 : 313287 : if (DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (t))
7097 : : {
7098 : 2009 : tree spec;
7099 : 2009 : RT (spec);
7100 : 2009 : store_explicit_specifier (t, spec);
7101 : : }
7102 : : }
7103 : : break;
7104 : :
7105 : 28368 : case USING_DECL:
7106 : : /* USING_DECL_DECLS */
7107 : 28368 : RT (t->decl_common.initial);
7108 : : /* FALLTHROUGH */
7109 : :
7110 : 566220 : case TYPE_DECL:
7111 : : /* USING_DECL: USING_DECL_SCOPE */
7112 : : /* TYPE_DECL: DECL_ORIGINAL_TYPE */
7113 : 566220 : RT (t->decl_non_common.result);
7114 : 566220 : break;
7115 : :
7116 : : /* Miscellaneous common nodes. */
7117 : 199344 : case BLOCK:
7118 : 199344 : t->block.locus = state->read_location (*this);
7119 : 199344 : t->block.end_locus = state->read_location (*this);
7120 : :
7121 : 199344 : for (tree *chain = &t->block.vars;;)
7122 : 301028 : if (tree decl = tree_node ())
7123 : : {
7124 : : /* For a deduplicated local type or enumerator, chain the
7125 : : duplicate decl instead of the canonical in-TU decl. Seeing
7126 : : a duplicate here means the containing function whose body
7127 : : we're streaming in is a duplicate too, so we'll end up
7128 : : discarding this BLOCK (and the rest of the duplicate function
7129 : : body) anyway. */
7130 : 101684 : decl = maybe_duplicate (decl);
7131 : :
7132 : 101684 : if (!DECL_P (decl))
7133 : : {
7134 : 0 : set_overrun ();
7135 : 0 : break;
7136 : : }
7137 : :
7138 : : /* If DECL_CHAIN is already set then this was a backreference to a
7139 : : local type or enumerator from a previous read (PR c++/114630).
7140 : : Let's copy the node so we can keep building the chain for ODR
7141 : : checking later. */
7142 : 101684 : if (DECL_CHAIN (decl))
7143 : : {
7144 : 12 : gcc_checking_assert (TREE_CODE (decl) == TYPE_DECL
7145 : : && find_duplicate (DECL_CONTEXT (decl)));
7146 : 6 : decl = copy_decl (decl);
7147 : : }
7148 : :
7149 : 101684 : *chain = decl;
7150 : 101684 : chain = &DECL_CHAIN (decl);
7151 : : }
7152 : : else
7153 : 101684 : break;
7154 : :
7155 : : /* nonlocalized_vars is middle-end. */
7156 : 199344 : RT (t->block.subblocks);
7157 : 199344 : RT (t->block.supercontext);
7158 : 199344 : RT (t->block.abstract_origin);
7159 : : /* fragment_origin, fragment_chain are middle-end. */
7160 : 199344 : RT (t->block.chain);
7161 : : /* nonlocalized_vars, block_num, die are middle endy/debug
7162 : : things. */
7163 : 199344 : break;
7164 : :
7165 : 372748 : case CALL_EXPR:
7166 : 372748 : RUC (internal_fn, t->base.u.ifn);
7167 : 372748 : break;
7168 : :
7169 : : case CONSTRUCTOR:
7170 : : // Streamed after the node's type.
7171 : : break;
7172 : :
7173 : 12 : case OMP_CLAUSE:
7174 : 12 : {
7175 : 12 : RU (t->omp_clause.subcode.map_kind);
7176 : 12 : t->omp_clause.locus = state->read_location (*this);
7177 : :
7178 : 12 : unsigned len = omp_clause_num_ops[OMP_CLAUSE_CODE (t)];
7179 : 45 : for (unsigned ix = 0; ix != len; ix++)
7180 : 33 : RT (t->omp_clause.ops[ix]);
7181 : : }
7182 : : break;
7183 : :
7184 : 139563 : case STATEMENT_LIST:
7185 : 139563 : {
7186 : 139563 : tree_stmt_iterator iter = tsi_start (t);
7187 : 553355 : for (tree stmt; RT (stmt);)
7188 : : {
7189 : 413792 : if (TREE_CODE (stmt) == DEBUG_BEGIN_STMT
7190 : 0 : && !MAY_HAVE_DEBUG_MARKER_STMTS)
7191 : 0 : continue;
7192 : 413792 : tsi_link_after (&iter, stmt, TSI_CONTINUE_LINKING);
7193 : : }
7194 : : }
7195 : 139563 : break;
7196 : :
7197 : 0 : case OPTIMIZATION_NODE:
7198 : 0 : case TARGET_OPTION_NODE:
7199 : : /* Not yet implemented, see trees_out::core_vals. */
7200 : 0 : gcc_unreachable ();
7201 : 70344 : break;
7202 : :
7203 : 70344 : case TREE_BINFO:
7204 : 70344 : RT (t->binfo.common.chain);
7205 : 70344 : RT (t->binfo.offset);
7206 : 70344 : RT (t->binfo.inheritance);
7207 : 70344 : RT (t->binfo.vptr_field);
7208 : :
7209 : : /* Do not mark the vtables as USED in the address expressions
7210 : : here. */
7211 : 70344 : unused++;
7212 : 70344 : RT (t->binfo.vtable);
7213 : 70344 : RT (t->binfo.virtuals);
7214 : 70344 : RT (t->binfo.vtt_subvtt);
7215 : 70344 : RT (t->binfo.vtt_vptr);
7216 : 70344 : unused--;
7217 : :
7218 : 70344 : BINFO_BASE_ACCESSES (t) = tree_vec ();
7219 : 70344 : if (!get_overrun ())
7220 : : {
7221 : 70344 : unsigned num = vec_safe_length (BINFO_BASE_ACCESSES (t));
7222 : 90760 : for (unsigned ix = 0; ix != num; ix++)
7223 : 20416 : BINFO_BASE_APPEND (t, tree_node ());
7224 : : }
7225 : : break;
7226 : :
7227 : 880331 : case TREE_LIST:
7228 : 880331 : RT (t->list.purpose);
7229 : 880331 : RT (t->list.value);
7230 : 880331 : RT (t->list.common.chain);
7231 : 880331 : break;
7232 : :
7233 : 554380 : case TREE_VEC:
7234 : 1490118 : for (unsigned ix = TREE_VEC_LENGTH (t); ix--;)
7235 : 935738 : RT (TREE_VEC_ELT (t, ix));
7236 : 554380 : RT (t->type_common.common.chain);
7237 : 554380 : break;
7238 : :
7239 : : /* C++-specific nodes ... */
7240 : 65659 : case BASELINK:
7241 : 65659 : RT (((lang_tree_node *)t)->baselink.binfo);
7242 : 65659 : RTU (((lang_tree_node *)t)->baselink.functions);
7243 : 65659 : RT (((lang_tree_node *)t)->baselink.access_binfo);
7244 : 65659 : break;
7245 : :
7246 : 24944 : case CONSTRAINT_INFO:
7247 : 24944 : RT (((lang_tree_node *)t)->constraint_info.template_reqs);
7248 : 24944 : RT (((lang_tree_node *)t)->constraint_info.declarator_reqs);
7249 : 24944 : RT (((lang_tree_node *)t)->constraint_info.associated_constr);
7250 : 24944 : break;
7251 : :
7252 : 4546 : case DEFERRED_NOEXCEPT:
7253 : 4546 : RT (((lang_tree_node *)t)->deferred_noexcept.pattern);
7254 : 4546 : RT (((lang_tree_node *)t)->deferred_noexcept.args);
7255 : 4546 : break;
7256 : :
7257 : 2456 : case LAMBDA_EXPR:
7258 : 2456 : RT (((lang_tree_node *)t)->lambda_expression.capture_list);
7259 : 2456 : RT (((lang_tree_node *)t)->lambda_expression.this_capture);
7260 : 2456 : RT (((lang_tree_node *)t)->lambda_expression.extra_scope);
7261 : 2456 : RT (((lang_tree_node *)t)->lambda_expression.regen_info);
7262 : 2456 : RT (((lang_tree_node *)t)->lambda_expression.extra_args);
7263 : : /* lambda_expression.pending_proxies is NULL */
7264 : 2456 : ((lang_tree_node *)t)->lambda_expression.locus
7265 : 2456 : = state->read_location (*this);
7266 : 2456 : RUC (cp_lambda_default_capture_mode_type,
7267 : : ((lang_tree_node *)t)->lambda_expression.default_capture_mode);
7268 : 2456 : RU (((lang_tree_node *)t)->lambda_expression.discriminator_scope);
7269 : 2456 : RU (((lang_tree_node *)t)->lambda_expression.discriminator_sig);
7270 : 2456 : break;
7271 : :
7272 : 373116 : case OVERLOAD:
7273 : 373116 : RT (((lang_tree_node *)t)->overload.function);
7274 : 373116 : RT (t->common.chain);
7275 : 373116 : break;
7276 : :
7277 : 0 : case PTRMEM_CST:
7278 : 0 : RT (((lang_tree_node *)t)->ptrmem.member);
7279 : 0 : break;
7280 : :
7281 : 5016 : case STATIC_ASSERT:
7282 : 5016 : RT (((lang_tree_node *)t)->static_assertion.condition);
7283 : 5016 : RT (((lang_tree_node *)t)->static_assertion.message);
7284 : 5016 : ((lang_tree_node *)t)->static_assertion.location
7285 : 5016 : = state->read_location (*this);
7286 : 5016 : break;
7287 : :
7288 : 224098 : case TEMPLATE_DECL:
7289 : : /* Streamed when reading the raw template decl itself. */
7290 : 224098 : gcc_assert (((lang_tree_node *)t)->template_decl.arguments);
7291 : 224098 : gcc_assert (((lang_tree_node *)t)->template_decl.result);
7292 : 224098 : if (DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (t))
7293 : 3970 : RT (DECL_CHAIN (t));
7294 : : break;
7295 : :
7296 : 501935 : case TEMPLATE_INFO:
7297 : 501935 : RT (((lang_tree_node *)t)->template_info.tmpl);
7298 : 501935 : RT (((lang_tree_node *)t)->template_info.args);
7299 : 501935 : RT (((lang_tree_node *)t)->template_info.partial);
7300 : 501935 : if (unsigned len = u ())
7301 : : {
7302 : 0 : auto &ac = (((lang_tree_node *)t)
7303 : : ->template_info.deferred_access_checks);
7304 : 0 : vec_alloc (ac, len);
7305 : 0 : for (unsigned ix = 0; ix != len; ix++)
7306 : : {
7307 : 0 : deferred_access_check m;
7308 : :
7309 : 0 : RT (m.binfo);
7310 : 0 : RT (m.decl);
7311 : 0 : RT (m.diag_decl);
7312 : 0 : m.loc = state->read_location (*this);
7313 : 0 : ac->quick_push (m);
7314 : : }
7315 : : }
7316 : : break;
7317 : :
7318 : 275412 : case TEMPLATE_PARM_INDEX:
7319 : 275412 : RU (((lang_tree_node *)t)->tpi.index);
7320 : 275412 : RU (((lang_tree_node *)t)->tpi.level);
7321 : 275412 : RU (((lang_tree_node *)t)->tpi.orig_level);
7322 : 275412 : RT (((lang_tree_node *)t)->tpi.decl);
7323 : 275412 : break;
7324 : :
7325 : 6390 : case TRAIT_EXPR:
7326 : 6390 : RT (((lang_tree_node *)t)->trait_expression.type1);
7327 : 6390 : RT (((lang_tree_node *)t)->trait_expression.type2);
7328 : 6390 : RUC (cp_trait_kind, ((lang_tree_node *)t)->trait_expression.kind);
7329 : 6390 : break;
7330 : : }
7331 : :
7332 : 9939000 : if (CODE_CONTAINS_STRUCT (code, TS_TYPED))
7333 : : {
7334 : 9203215 : tree type = tree_node ();
7335 : :
7336 : 9203215 : if (type && code == ENUMERAL_TYPE && !ENUM_FIXED_UNDERLYING_TYPE_P (t))
7337 : : {
7338 : 1536 : unsigned precision = u ();
7339 : :
7340 : 1536 : type = build_distinct_type_copy (type);
7341 : 1536 : TYPE_PRECISION (type) = precision;
7342 : 3072 : set_min_and_max_values_for_integral_type (type, precision,
7343 : 1536 : TYPE_SIGN (type));
7344 : : }
7345 : :
7346 : 9203215 : if (code != TEMPLATE_DECL)
7347 : 8979117 : t->typed.type = type;
7348 : : }
7349 : :
7350 : 9939000 : if (TREE_CODE (t) == CONSTRUCTOR)
7351 : 30328 : if (unsigned len = u ())
7352 : : {
7353 : 9721 : vec_alloc (t->constructor.elts, len);
7354 : 96190 : for (unsigned ix = 0; ix != len; ix++)
7355 : : {
7356 : 86469 : constructor_elt elt;
7357 : :
7358 : 86469 : RT (elt.index);
7359 : 86469 : RTU (elt.value);
7360 : 86469 : t->constructor.elts->quick_push (elt);
7361 : : }
7362 : : }
7363 : :
7364 : : #undef RT
7365 : : #undef RM
7366 : : #undef RU
7367 : 9939000 : return !get_overrun ();
7368 : : }
7369 : :
7370 : : void
7371 : 3944263 : trees_out::lang_decl_vals (tree t)
7372 : : {
7373 : 3944263 : const struct lang_decl *lang = DECL_LANG_SPECIFIC (t);
7374 : : #define WU(X) (u (X))
7375 : : #define WT(X) (tree_node (X))
7376 : : /* Module index already written. */
7377 : 3944263 : switch (lang->u.base.selector)
7378 : : {
7379 : 0 : default:
7380 : 0 : gcc_unreachable ();
7381 : :
7382 : 727285 : case lds_fn: /* lang_decl_fn. */
7383 : 727285 : if (streaming_p ())
7384 : : {
7385 : 363559 : if (DECL_NAME (t) && IDENTIFIER_OVL_OP_P (DECL_NAME (t)))
7386 : 59983 : WU (lang->u.fn.ovl_op_code);
7387 : : }
7388 : :
7389 : 727285 : if (DECL_CLASS_SCOPE_P (t))
7390 : 531370 : WT (lang->u.fn.context);
7391 : :
7392 : 727285 : if (lang->u.fn.thunk_p)
7393 : : {
7394 : : /* The thunked-to function. */
7395 : 1128 : WT (lang->u.fn.befriending_classes);
7396 : 1128 : if (streaming_p ())
7397 : 564 : wi (lang->u.fn.u5.fixed_offset);
7398 : : }
7399 : 726157 : else if (decl_tls_wrapper_p (t))
7400 : : /* The wrapped variable. */
7401 : 6 : WT (lang->u.fn.befriending_classes);
7402 : : else
7403 : 726151 : WT (lang->u.fn.u5.cloned_function);
7404 : :
7405 : 727285 : if (FNDECL_USED_AUTO (t))
7406 : 2122 : WT (lang->u.fn.u.saved_auto_return_type);
7407 : :
7408 : 727285 : goto lds_min;
7409 : :
7410 : 2450 : case lds_decomp: /* lang_decl_decomp. */
7411 : 2450 : WT (lang->u.decomp.base);
7412 : 2450 : goto lds_min;
7413 : :
7414 : 2322848 : case lds_min: /* lang_decl_min. */
7415 : 2322848 : lds_min:
7416 : 2322848 : WT (lang->u.min.template_info);
7417 : 2322848 : {
7418 : 2322848 : tree access = lang->u.min.access;
7419 : :
7420 : : /* DECL_ACCESS needs to be maintained by the definition of the
7421 : : (derived) class that changes the access. The other users
7422 : : of DECL_ACCESS need to write it here. */
7423 : 727285 : if (!DECL_THUNK_P (t)
7424 : 3049005 : && (DECL_CONTEXT (t) && TYPE_P (DECL_CONTEXT (t))))
7425 : : access = NULL_TREE;
7426 : :
7427 : 2322848 : WT (access);
7428 : : }
7429 : 2322848 : break;
7430 : :
7431 : : case lds_ns: /* lang_decl_ns. */
7432 : : break;
7433 : :
7434 : 1621242 : case lds_parm: /* lang_decl_parm. */
7435 : 1621242 : if (streaming_p ())
7436 : : {
7437 : 552871 : WU (lang->u.parm.level);
7438 : 552871 : WU (lang->u.parm.index);
7439 : : }
7440 : : break;
7441 : : }
7442 : : #undef WU
7443 : : #undef WT
7444 : 3944263 : }
7445 : :
7446 : : bool
7447 : 1411674 : trees_in::lang_decl_vals (tree t)
7448 : : {
7449 : 1411674 : struct lang_decl *lang = DECL_LANG_SPECIFIC (t);
7450 : : #define RU(X) ((X) = u ())
7451 : : #define RT(X) ((X) = tree_node ())
7452 : :
7453 : : /* Module index already read. */
7454 : 1411674 : switch (lang->u.base.selector)
7455 : : {
7456 : 0 : default:
7457 : 0 : gcc_unreachable ();
7458 : :
7459 : 313287 : case lds_fn: /* lang_decl_fn. */
7460 : 313287 : if (DECL_NAME (t) && IDENTIFIER_OVL_OP_P (DECL_NAME (t)))
7461 : : {
7462 : 53052 : unsigned code = u ();
7463 : :
7464 : : /* Check consistency. */
7465 : 53052 : if (code >= OVL_OP_MAX
7466 : 53052 : || (ovl_op_info[IDENTIFIER_ASSIGN_OP_P (DECL_NAME (t))][code]
7467 : 53052 : .ovl_op_code) == OVL_OP_ERROR_MARK)
7468 : 0 : set_overrun ();
7469 : : else
7470 : 53052 : lang->u.fn.ovl_op_code = code;
7471 : : }
7472 : :
7473 : 313287 : if (DECL_CLASS_SCOPE_P (t))
7474 : 231339 : RT (lang->u.fn.context);
7475 : :
7476 : 313287 : if (lang->u.fn.thunk_p)
7477 : : {
7478 : 490 : RT (lang->u.fn.befriending_classes);
7479 : 490 : lang->u.fn.u5.fixed_offset = wi ();
7480 : : }
7481 : 312797 : else if (decl_tls_wrapper_p (t))
7482 : 6 : RT (lang->u.fn.befriending_classes);
7483 : : else
7484 : 312791 : RT (lang->u.fn.u5.cloned_function);
7485 : :
7486 : 313287 : if (FNDECL_USED_AUTO (t))
7487 : 901 : RT (lang->u.fn.u.saved_auto_return_type);
7488 : 313287 : goto lds_min;
7489 : :
7490 : 1154 : case lds_decomp: /* lang_decl_decomp. */
7491 : 1154 : RT (lang->u.decomp.base);
7492 : 1154 : goto lds_min;
7493 : :
7494 : 945768 : case lds_min: /* lang_decl_min. */
7495 : 945768 : lds_min:
7496 : 945768 : RT (lang->u.min.template_info);
7497 : 945768 : RT (lang->u.min.access);
7498 : 945768 : break;
7499 : :
7500 : : case lds_ns: /* lang_decl_ns. */
7501 : : break;
7502 : :
7503 : 465806 : case lds_parm: /* lang_decl_parm. */
7504 : 465806 : RU (lang->u.parm.level);
7505 : 465806 : RU (lang->u.parm.index);
7506 : 465806 : break;
7507 : : }
7508 : : #undef RU
7509 : : #undef RT
7510 : 1411674 : return !get_overrun ();
7511 : : }
7512 : :
7513 : : /* Most of the value contents of lang_type is streamed in
7514 : : define_class. */
7515 : :
7516 : : void
7517 : 283590 : trees_out::lang_type_vals (tree t)
7518 : : {
7519 : 283590 : const struct lang_type *lang = TYPE_LANG_SPECIFIC (t);
7520 : : #define WU(X) (u (X))
7521 : : #define WT(X) (tree_node (X))
7522 : 283590 : if (streaming_p ())
7523 : 141775 : WU (lang->align);
7524 : : #undef WU
7525 : : #undef WT
7526 : 283590 : }
7527 : :
7528 : : bool
7529 : 108782 : trees_in::lang_type_vals (tree t)
7530 : : {
7531 : 108782 : struct lang_type *lang = TYPE_LANG_SPECIFIC (t);
7532 : : #define RU(X) ((X) = u ())
7533 : : #define RT(X) ((X) = tree_node ())
7534 : 108782 : RU (lang->align);
7535 : : #undef RU
7536 : : #undef RT
7537 : 108782 : return !get_overrun ();
7538 : : }
7539 : :
7540 : : /* Write out the bools of T, including information about any
7541 : : LANG_SPECIFIC information. Including allocation of any lang
7542 : : specific object. */
7543 : :
7544 : : void
7545 : 11764516 : trees_out::tree_node_bools (tree t)
7546 : : {
7547 : 11764516 : gcc_checking_assert (streaming_p ());
7548 : :
7549 : : /* We should never stream a namespace. */
7550 : 11764516 : gcc_checking_assert (TREE_CODE (t) != NAMESPACE_DECL
7551 : : || DECL_NAMESPACE_ALIAS (t));
7552 : :
7553 : 11764516 : bits_out bits = stream_bits ();
7554 : 11764516 : core_bools (t, bits);
7555 : :
7556 : 11764516 : switch (TREE_CODE_CLASS (TREE_CODE (t)))
7557 : : {
7558 : 2659800 : case tcc_declaration:
7559 : 2659800 : {
7560 : 2659800 : bool specific = DECL_LANG_SPECIFIC (t) != NULL;
7561 : 2659800 : bits.b (specific);
7562 : 2659800 : if (specific && VAR_P (t))
7563 : 238132 : bits.b (DECL_DECOMPOSITION_P (t));
7564 : 1707438 : if (specific)
7565 : 1707438 : lang_decl_bools (t, bits);
7566 : : }
7567 : : break;
7568 : :
7569 : 510306 : case tcc_type:
7570 : 510306 : {
7571 : 510306 : bool specific = (TYPE_MAIN_VARIANT (t) == t
7572 : 510306 : && TYPE_LANG_SPECIFIC (t) != NULL);
7573 : 510306 : gcc_assert (TYPE_LANG_SPECIFIC (t)
7574 : : == TYPE_LANG_SPECIFIC (TYPE_MAIN_VARIANT (t)));
7575 : :
7576 : 510306 : bits.b (specific);
7577 : 510306 : if (specific)
7578 : 141775 : lang_type_bools (t, bits);
7579 : : }
7580 : : break;
7581 : :
7582 : : default:
7583 : : break;
7584 : : }
7585 : :
7586 : 11764516 : bits.bflush ();
7587 : 11764516 : }
7588 : :
7589 : : bool
7590 : 9953132 : trees_in::tree_node_bools (tree t)
7591 : : {
7592 : 9953132 : bits_in bits = stream_bits ();
7593 : 9953132 : bool ok = core_bools (t, bits);
7594 : :
7595 : 9953132 : if (ok)
7596 : 9953132 : switch (TREE_CODE_CLASS (TREE_CODE (t)))
7597 : : {
7598 : 2208158 : case tcc_declaration:
7599 : 2208158 : if (bits.b ())
7600 : : {
7601 : 1411674 : bool decomp = VAR_P (t) && bits.b ();
7602 : :
7603 : 1411674 : ok = maybe_add_lang_decl_raw (t, decomp);
7604 : 1411674 : if (ok)
7605 : 1411674 : ok = lang_decl_bools (t, bits);
7606 : : }
7607 : : break;
7608 : :
7609 : 398712 : case tcc_type:
7610 : 398712 : if (bits.b ())
7611 : : {
7612 : 108782 : ok = maybe_add_lang_type_raw (t);
7613 : 108782 : if (ok)
7614 : 108782 : ok = lang_type_bools (t, bits);
7615 : : }
7616 : : break;
7617 : :
7618 : : default:
7619 : : break;
7620 : : }
7621 : :
7622 : 9953132 : bits.bflush ();
7623 : 9953132 : if (!ok || get_overrun ())
7624 : 0 : return false;
7625 : :
7626 : : return true;
7627 : 9953132 : }
7628 : :
7629 : :
7630 : : /* Write out the lang-specifc vals of node T. */
7631 : :
7632 : : void
7633 : 30456137 : trees_out::lang_vals (tree t)
7634 : : {
7635 : 30456137 : switch (TREE_CODE_CLASS (TREE_CODE (t)))
7636 : : {
7637 : 6954683 : case tcc_declaration:
7638 : 6954683 : if (DECL_LANG_SPECIFIC (t))
7639 : 3944263 : lang_decl_vals (t);
7640 : : break;
7641 : :
7642 : 1768059 : case tcc_type:
7643 : 1768059 : if (TYPE_MAIN_VARIANT (t) == t && TYPE_LANG_SPECIFIC (t))
7644 : 283590 : lang_type_vals (t);
7645 : : break;
7646 : :
7647 : : default:
7648 : : break;
7649 : : }
7650 : 30456137 : }
7651 : :
7652 : : bool
7653 : 9939000 : trees_in::lang_vals (tree t)
7654 : : {
7655 : 9939000 : bool ok = true;
7656 : :
7657 : 9939000 : switch (TREE_CODE_CLASS (TREE_CODE (t)))
7658 : : {
7659 : 2208158 : case tcc_declaration:
7660 : 2208158 : if (DECL_LANG_SPECIFIC (t))
7661 : 1411674 : ok = lang_decl_vals (t);
7662 : : break;
7663 : :
7664 : 384580 : case tcc_type:
7665 : 384580 : if (TYPE_LANG_SPECIFIC (t))
7666 : 108782 : ok = lang_type_vals (t);
7667 : : else
7668 : 275798 : TYPE_LANG_SPECIFIC (t) = TYPE_LANG_SPECIFIC (TYPE_MAIN_VARIANT (t));
7669 : : break;
7670 : :
7671 : : default:
7672 : : break;
7673 : : }
7674 : :
7675 : 9939000 : return ok;
7676 : : }
7677 : :
7678 : : /* Write out the value fields of node T. */
7679 : :
7680 : : void
7681 : 30456137 : trees_out::tree_node_vals (tree t)
7682 : : {
7683 : 30456137 : core_vals (t);
7684 : 30456137 : lang_vals (t);
7685 : 30456137 : }
7686 : :
7687 : : bool
7688 : 9939000 : trees_in::tree_node_vals (tree t)
7689 : : {
7690 : 9939000 : bool ok = core_vals (t);
7691 : 9939000 : if (ok)
7692 : 9939000 : ok = lang_vals (t);
7693 : :
7694 : 9939000 : return ok;
7695 : : }
7696 : :
7697 : :
7698 : : /* If T is a back reference, fixed reference or NULL, write out its
7699 : : code and return WK_none. Otherwise return WK_value if we must write
7700 : : by value, or WK_normal otherwise. */
7701 : :
7702 : : walk_kind
7703 : 205716993 : trees_out::ref_node (tree t)
7704 : : {
7705 : 205716993 : if (!t)
7706 : : {
7707 : 84902326 : if (streaming_p ())
7708 : : {
7709 : : /* NULL_TREE -> tt_null. */
7710 : 32562290 : null_count++;
7711 : 32562290 : i (tt_null);
7712 : : }
7713 : 84902326 : return WK_none;
7714 : : }
7715 : :
7716 : 120814667 : if (!TREE_VISITED (t))
7717 : : return WK_normal;
7718 : :
7719 : : /* An already-visited tree. It must be in the map. */
7720 : 71172319 : int val = get_tag (t);
7721 : :
7722 : 71172319 : if (val == tag_value)
7723 : : /* An entry we should walk into. */
7724 : : return WK_value;
7725 : :
7726 : 69995797 : const char *kind;
7727 : :
7728 : 69995797 : if (val <= tag_backref)
7729 : : {
7730 : : /* Back reference -> -ve number */
7731 : 53662569 : if (streaming_p ())
7732 : 25371221 : i (val);
7733 : : kind = "backref";
7734 : : }
7735 : 16333228 : else if (val >= tag_fixed)
7736 : : {
7737 : : /* Fixed reference -> tt_fixed */
7738 : 16333228 : val -= tag_fixed;
7739 : 16333228 : if (streaming_p ())
7740 : 5770589 : i (tt_fixed), u (val);
7741 : : kind = "fixed";
7742 : : }
7743 : :
7744 : 69995797 : if (streaming_p ())
7745 : : {
7746 : 31141810 : back_ref_count++;
7747 : 31141810 : dump (dumper::TREE)
7748 : 13182 : && dump ("Wrote %s:%d %C:%N%S", kind, val, TREE_CODE (t), t, t);
7749 : : }
7750 : : return WK_none;
7751 : : }
7752 : :
7753 : : tree
7754 : 21335185 : trees_in::back_ref (int tag)
7755 : : {
7756 : 21335185 : tree res = NULL_TREE;
7757 : :
7758 : 21335185 : if (tag < 0 && unsigned (~tag) < back_refs.length ())
7759 : 21335185 : res = back_refs[~tag];
7760 : :
7761 : 21335185 : if (!res
7762 : : /* Checking TREE_CODE is a dereference, so we know this is not a
7763 : : wild pointer. Checking the code provides evidence we've not
7764 : : corrupted something. */
7765 : 21335185 : || TREE_CODE (res) >= MAX_TREE_CODES)
7766 : 0 : set_overrun ();
7767 : : else
7768 : 21349111 : dump (dumper::TREE) && dump ("Read backref:%d found %C:%N%S", tag,
7769 : : TREE_CODE (res), res, res);
7770 : 21335185 : return res;
7771 : : }
7772 : :
7773 : : unsigned
7774 : 2662891 : trees_out::add_indirect_tpl_parms (tree parms)
7775 : : {
7776 : 2662891 : unsigned len = 0;
7777 : 4970332 : for (; parms; parms = TREE_CHAIN (parms), len++)
7778 : : {
7779 : 2810736 : if (TREE_VISITED (parms))
7780 : : break;
7781 : :
7782 : 2307441 : int tag = insert (parms);
7783 : 2307441 : if (streaming_p ())
7784 : 2307720 : dump (dumper::TREE)
7785 : 153 : && dump ("Indirect:%d template's parameter %u %C:%N",
7786 : 153 : tag, len, TREE_CODE (parms), parms);
7787 : : }
7788 : :
7789 : 2662891 : if (streaming_p ())
7790 : 448828 : u (len);
7791 : :
7792 : 2662891 : return len;
7793 : : }
7794 : :
7795 : : unsigned
7796 : 365687 : trees_in::add_indirect_tpl_parms (tree parms)
7797 : : {
7798 : 365687 : unsigned len = u ();
7799 : 655759 : for (unsigned ix = 0; ix != len; parms = TREE_CHAIN (parms), ix++)
7800 : : {
7801 : 290072 : int tag = insert (parms);
7802 : 290502 : dump (dumper::TREE)
7803 : 159 : && dump ("Indirect:%d template's parameter %u %C:%N",
7804 : 159 : tag, ix, TREE_CODE (parms), parms);
7805 : : }
7806 : :
7807 : 365687 : return len;
7808 : : }
7809 : :
7810 : : /* We've just found DECL by name. Insert nodes that come with it, but
7811 : : cannot be found by name, so we'll not accidentally walk into them. */
7812 : :
7813 : : void
7814 : 6189771 : trees_out::add_indirects (tree decl)
7815 : : {
7816 : 6189771 : unsigned count = 0;
7817 : :
7818 : : // FIXME:OPTIMIZATION We'll eventually want default fn parms of
7819 : : // templates and perhaps default template parms too. The former can
7820 : : // be referenced from instantiations (as they are lazily
7821 : : // instantiated). Also (deferred?) exception specifications of
7822 : : // templates. See the note about PARM_DECLs in trees_out::decl_node.
7823 : 6189771 : tree inner = decl;
7824 : 6189771 : if (TREE_CODE (decl) == TEMPLATE_DECL)
7825 : : {
7826 : 2662891 : count += add_indirect_tpl_parms (DECL_TEMPLATE_PARMS (decl));
7827 : :
7828 : 2662891 : inner = DECL_TEMPLATE_RESULT (decl);
7829 : 2662891 : int tag = insert (inner);
7830 : 2662891 : if (streaming_p ())
7831 : 448828 : dump (dumper::TREE)
7832 : 219 : && dump ("Indirect:%d template's result %C:%N",
7833 : 219 : tag, TREE_CODE (inner), inner);
7834 : 2662891 : count++;
7835 : : }
7836 : :
7837 : 6189771 : if (TREE_CODE (inner) == TYPE_DECL)
7838 : : {
7839 : : /* Make sure the type is in the map too. Otherwise we get
7840 : : different RECORD_TYPEs for the same type, and things go
7841 : : south. */
7842 : 3438126 : tree type = TREE_TYPE (inner);
7843 : 3438126 : gcc_checking_assert (DECL_ORIGINAL_TYPE (inner)
7844 : : || TYPE_NAME (type) == inner);
7845 : 3438126 : int tag = insert (type);
7846 : 3438126 : if (streaming_p ())
7847 : 403348 : dump (dumper::TREE) && dump ("Indirect:%d decl's type %C:%N", tag,
7848 : 360 : TREE_CODE (type), type);
7849 : 3438126 : count++;
7850 : : }
7851 : :
7852 : 6189771 : if (streaming_p ())
7853 : : {
7854 : 969773 : u (count);
7855 : 970286 : dump (dumper::TREE) && dump ("Inserted %u indirects", count);
7856 : : }
7857 : 6189771 : }
7858 : :
7859 : : bool
7860 : 765213 : trees_in::add_indirects (tree decl)
7861 : : {
7862 : 765213 : unsigned count = 0;
7863 : :
7864 : 765213 : tree inner = decl;
7865 : 765213 : if (TREE_CODE (inner) == TEMPLATE_DECL)
7866 : : {
7867 : 365687 : count += add_indirect_tpl_parms (DECL_TEMPLATE_PARMS (decl));
7868 : :
7869 : 365687 : inner = DECL_TEMPLATE_RESULT (decl);
7870 : 365687 : int tag = insert (inner);
7871 : 365687 : dump (dumper::TREE)
7872 : 228 : && dump ("Indirect:%d templates's result %C:%N", tag,
7873 : 228 : TREE_CODE (inner), inner);
7874 : 365687 : count++;
7875 : : }
7876 : :
7877 : 765213 : if (TREE_CODE (inner) == TYPE_DECL)
7878 : : {
7879 : 298891 : tree type = TREE_TYPE (inner);
7880 : 298891 : gcc_checking_assert (DECL_ORIGINAL_TYPE (inner)
7881 : : || TYPE_NAME (type) == inner);
7882 : 298891 : int tag = insert (type);
7883 : 298891 : dump (dumper::TREE)
7884 : 360 : && dump ("Indirect:%d decl's type %C:%N", tag, TREE_CODE (type), type);
7885 : 298891 : count++;
7886 : : }
7887 : :
7888 : 765813 : dump (dumper::TREE) && dump ("Inserted %u indirects", count);
7889 : 765213 : return count == u ();
7890 : : }
7891 : :
7892 : : /* Stream a template parameter. There are 4.5 kinds of parameter:
7893 : : a) Template - TEMPLATE_DECL->TYPE_DECL->TEMPLATE_TEMPLATE_PARM
7894 : : TEMPLATE_TYPE_PARM_INDEX TPI
7895 : : b) Type - TYPE_DECL->TEMPLATE_TYPE_PARM TEMPLATE_TYPE_PARM_INDEX TPI
7896 : : c.1) NonTYPE - PARM_DECL DECL_INITIAL TPI We meet this first
7897 : : c.2) NonTYPE - CONST_DECL DECL_INITIAL Same TPI
7898 : : d) BoundTemplate - TYPE_DECL->BOUND_TEMPLATE_TEMPLATE_PARM
7899 : : TEMPLATE_TYPE_PARM_INDEX->TPI
7900 : : TEMPLATE_TEMPLATE_PARM_INFO->TEMPLATE_INFO
7901 : :
7902 : : All of these point to a TEMPLATE_PARM_INDEX, and #B also has a TEMPLATE_INFO
7903 : : */
7904 : :
7905 : : void
7906 : 1590201 : trees_out::tpl_parm_value (tree parm)
7907 : : {
7908 : 1590201 : gcc_checking_assert (DECL_P (parm) && DECL_TEMPLATE_PARM_P (parm));
7909 : :
7910 : 1590201 : int parm_tag = insert (parm);
7911 : 1590201 : if (streaming_p ())
7912 : : {
7913 : 361540 : i (tt_tpl_parm);
7914 : 361540 : dump (dumper::TREE) && dump ("Writing template parm:%d %C:%N",
7915 : 114 : parm_tag, TREE_CODE (parm), parm);
7916 : 361540 : start (parm);
7917 : 361540 : tree_node_bools (parm);
7918 : : }
7919 : :
7920 : 1590201 : tree inner = parm;
7921 : 1590201 : if (TREE_CODE (inner) == TEMPLATE_DECL)
7922 : : {
7923 : 4786 : inner = DECL_TEMPLATE_RESULT (inner);
7924 : 4786 : int inner_tag = insert (inner);
7925 : 4786 : if (streaming_p ())
7926 : : {
7927 : 1234 : dump (dumper::TREE) && dump ("Writing inner template parm:%d %C:%N",
7928 : 0 : inner_tag, TREE_CODE (inner), inner);
7929 : 1234 : start (inner);
7930 : 1234 : tree_node_bools (inner);
7931 : : }
7932 : : }
7933 : :
7934 : 1590201 : tree type = NULL_TREE;
7935 : 1590201 : if (TREE_CODE (inner) == TYPE_DECL)
7936 : : {
7937 : 1435886 : type = TREE_TYPE (inner);
7938 : 1435886 : int type_tag = insert (type);
7939 : 1435886 : if (streaming_p ())
7940 : : {
7941 : 326532 : dump (dumper::TREE) && dump ("Writing template parm type:%d %C:%N",
7942 : 108 : type_tag, TREE_CODE (type), type);
7943 : 326532 : start (type);
7944 : 326532 : tree_node_bools (type);
7945 : : }
7946 : : }
7947 : :
7948 : 1590201 : if (inner != parm)
7949 : : {
7950 : : /* This is a template-template parameter. */
7951 : 4786 : unsigned tpl_levels = 0;
7952 : 4786 : tpl_header (parm, &tpl_levels);
7953 : 4786 : tpl_parms_fini (parm, tpl_levels);
7954 : : }
7955 : :
7956 : 1590201 : tree_node_vals (parm);
7957 : 1590201 : if (inner != parm)
7958 : 4786 : tree_node_vals (inner);
7959 : 1590201 : if (type)
7960 : : {
7961 : 1435886 : tree_node_vals (type);
7962 : 1435886 : if (DECL_NAME (inner) == auto_identifier
7963 : 1435886 : || DECL_NAME (inner) == decltype_auto_identifier)
7964 : : {
7965 : : /* Placeholder auto. */
7966 : 53575 : tree_node (DECL_INITIAL (inner));
7967 : 53575 : tree_node (DECL_SIZE_UNIT (inner));
7968 : : }
7969 : : }
7970 : :
7971 : 1590201 : if (streaming_p ())
7972 : 361540 : dump (dumper::TREE) && dump ("Wrote template parm:%d %C:%N",
7973 : 114 : parm_tag, TREE_CODE (parm), parm);
7974 : 1590201 : }
7975 : :
7976 : : tree
7977 : 287644 : trees_in::tpl_parm_value ()
7978 : : {
7979 : 287644 : tree parm = start ();
7980 : 287644 : if (!parm || !tree_node_bools (parm))
7981 : 0 : return NULL_TREE;
7982 : :
7983 : 287644 : int parm_tag = insert (parm);
7984 : 287644 : dump (dumper::TREE) && dump ("Reading template parm:%d %C:%N",
7985 : 198 : parm_tag, TREE_CODE (parm), parm);
7986 : :
7987 : 287644 : tree inner = parm;
7988 : 287644 : if (TREE_CODE (inner) == TEMPLATE_DECL)
7989 : : {
7990 : 824 : inner = start ();
7991 : 824 : if (!inner || !tree_node_bools (inner))
7992 : 0 : return NULL_TREE;
7993 : 824 : int inner_tag = insert (inner);
7994 : 824 : dump (dumper::TREE) && dump ("Reading inner template parm:%d %C:%N",
7995 : 0 : inner_tag, TREE_CODE (inner), inner);
7996 : 824 : DECL_TEMPLATE_RESULT (parm) = inner;
7997 : : }
7998 : :
7999 : 287644 : tree type = NULL_TREE;
8000 : 287644 : if (TREE_CODE (inner) == TYPE_DECL)
8001 : : {
8002 : 258870 : type = start ();
8003 : 258870 : if (!type || !tree_node_bools (type))
8004 : 0 : return NULL_TREE;
8005 : 258870 : int type_tag = insert (type);
8006 : 258870 : dump (dumper::TREE) && dump ("Reading template parm type:%d %C:%N",
8007 : 120 : type_tag, TREE_CODE (type), type);
8008 : :
8009 : 258870 : TREE_TYPE (inner) = TREE_TYPE (parm) = type;
8010 : 258870 : TYPE_NAME (type) = parm;
8011 : : }
8012 : :
8013 : 287644 : if (inner != parm)
8014 : : {
8015 : : /* A template template parameter. */
8016 : 824 : unsigned tpl_levels = 0;
8017 : 824 : tpl_header (parm, &tpl_levels);
8018 : 824 : tpl_parms_fini (parm, tpl_levels);
8019 : : }
8020 : :
8021 : 287644 : tree_node_vals (parm);
8022 : 287644 : if (inner != parm)
8023 : 824 : tree_node_vals (inner);
8024 : 287644 : if (type)
8025 : : {
8026 : 258870 : tree_node_vals (type);
8027 : 258870 : if (DECL_NAME (inner) == auto_identifier
8028 : 258870 : || DECL_NAME (inner) == decltype_auto_identifier)
8029 : : {
8030 : : /* Placeholder auto. */
8031 : 19768 : DECL_INITIAL (inner) = tree_node ();
8032 : 19768 : DECL_SIZE_UNIT (inner) = tree_node ();
8033 : : }
8034 : 258870 : if (TYPE_CANONICAL (type))
8035 : : {
8036 : 258870 : gcc_checking_assert (TYPE_CANONICAL (type) == type);
8037 : 258870 : TYPE_CANONICAL (type) = canonical_type_parameter (type);
8038 : : }
8039 : : }
8040 : :
8041 : 287644 : dump (dumper::TREE) && dump ("Read template parm:%d %C:%N",
8042 : 198 : parm_tag, TREE_CODE (parm), parm);
8043 : :
8044 : : return parm;
8045 : : }
8046 : :
8047 : : void
8048 : 997864 : trees_out::install_entity (tree decl, depset *dep)
8049 : : {
8050 : 997864 : gcc_checking_assert (streaming_p ());
8051 : :
8052 : : /* Write the entity index, so we can insert it as soon as we
8053 : : know this is new. */
8054 : 997864 : u (dep ? dep->cluster + 1 : 0);
8055 : 997864 : if (CHECKING_P && dep)
8056 : : {
8057 : : /* Add it to the entity map, such that we can tell it is
8058 : : part of us. */
8059 : 754148 : bool existed;
8060 : 754148 : unsigned *slot = &entity_map->get_or_insert
8061 : 754148 : (DECL_UID (decl), &existed);
8062 : 754148 : if (existed)
8063 : : /* If it existed, it should match. */
8064 : 761 : gcc_checking_assert (decl == (*entity_ary)[*slot]);
8065 : 754148 : *slot = ~dep->cluster;
8066 : : }
8067 : 997864 : }
8068 : :
8069 : : bool
8070 : 817691 : trees_in::install_entity (tree decl)
8071 : : {
8072 : 817691 : unsigned entity_index = u ();
8073 : 817691 : if (!entity_index)
8074 : : return false;
8075 : :
8076 : 609950 : if (entity_index > state->entity_num)
8077 : : {
8078 : 0 : set_overrun ();
8079 : 0 : return false;
8080 : : }
8081 : :
8082 : : /* Insert the real decl into the entity ary. */
8083 : 609950 : unsigned ident = state->entity_lwm + entity_index - 1;
8084 : 609950 : (*entity_ary)[ident] = decl;
8085 : :
8086 : : /* And into the entity map, if it's not already there. */
8087 : 609950 : tree not_tmpl = STRIP_TEMPLATE (decl);
8088 : 609950 : if (!DECL_LANG_SPECIFIC (not_tmpl)
8089 : 1137056 : || !DECL_MODULE_ENTITY_P (not_tmpl))
8090 : : {
8091 : 609360 : retrofit_lang_decl (not_tmpl);
8092 : 609360 : DECL_MODULE_ENTITY_P (not_tmpl) = true;
8093 : :
8094 : : /* Insert into the entity hash (it cannot already be there). */
8095 : 609360 : bool existed;
8096 : 609360 : unsigned &slot = entity_map->get_or_insert (DECL_UID (decl), &existed);
8097 : 609360 : gcc_checking_assert (!existed);
8098 : 609360 : slot = ident;
8099 : : }
8100 : 590 : else if (state->is_partition ())
8101 : : {
8102 : : /* The decl is already in the entity map, but we see it again now from a
8103 : : partition: we want to overwrite if the original decl wasn't also from
8104 : : a (possibly different) partition. Otherwise, for things like template
8105 : : instantiations, make_dependency might not realise that this is also
8106 : : provided from a partition and should be considered part of this module
8107 : : (and thus always emitted into the primary interface's CMI). */
8108 : 123 : unsigned *slot = entity_map->get (DECL_UID (decl));
8109 : 123 : module_state *imp = import_entity_module (*slot);
8110 : 123 : if (!imp->is_partition ())
8111 : 24 : *slot = ident;
8112 : : }
8113 : :
8114 : : return true;
8115 : : }
8116 : :
8117 : : static bool has_definition (tree decl);
8118 : :
8119 : : /* DECL is a decl node that must be written by value. DEP is the
8120 : : decl's depset. */
8121 : :
8122 : : void
8123 : 2760692 : trees_out::decl_value (tree decl, depset *dep)
8124 : : {
8125 : : /* We should not be writing clones or template parms. */
8126 : 2760692 : gcc_checking_assert (DECL_P (decl)
8127 : : && !DECL_CLONED_FUNCTION_P (decl)
8128 : : && !DECL_TEMPLATE_PARM_P (decl));
8129 : :
8130 : : /* We should never be writing non-typedef ptrmemfuncs by value. */
8131 : 2760692 : gcc_checking_assert (TREE_CODE (decl) != TYPE_DECL
8132 : : || DECL_ORIGINAL_TYPE (decl)
8133 : : || !TYPE_PTRMEMFUNC_P (TREE_TYPE (decl)));
8134 : :
8135 : : /* There's no need to walk any of the contents of a known TU-local entity,
8136 : : since importers should never see any of it regardless. But make sure we
8137 : : at least note its location so importers can use it for diagnostics. */
8138 : 5023917 : if (dep && dep->is_tu_local ())
8139 : : {
8140 : 419 : gcc_checking_assert (is_initial_scan ());
8141 : 419 : insert (decl, WK_value);
8142 : 419 : state->note_location (DECL_SOURCE_LOCATION (decl));
8143 : 419 : return;
8144 : : }
8145 : :
8146 : 2760273 : merge_kind mk = get_merge_kind (decl, dep);
8147 : :
8148 : 2760273 : if (CHECKING_P)
8149 : : {
8150 : : /* Never start in the middle of a template. */
8151 : 2760273 : int use_tpl = -1;
8152 : 2760273 : if (tree ti = node_template_info (decl, use_tpl))
8153 : 968864 : gcc_checking_assert (TREE_CODE (TI_TEMPLATE (ti)) == OVERLOAD
8154 : : || TREE_CODE (TI_TEMPLATE (ti)) == FIELD_DECL
8155 : : || (DECL_TEMPLATE_RESULT (TI_TEMPLATE (ti))
8156 : : != decl));
8157 : : }
8158 : :
8159 : 2760273 : if (streaming_p ())
8160 : : {
8161 : : /* A new node -> tt_decl. */
8162 : 997864 : decl_val_count++;
8163 : 997864 : i (tt_decl);
8164 : 997864 : u (mk);
8165 : 997864 : start (decl);
8166 : :
8167 : 997864 : if (mk != MK_unique)
8168 : : {
8169 : 854337 : bits_out bits = stream_bits ();
8170 : 854337 : if (!(mk & MK_template_mask) && !state->is_header ())
8171 : : {
8172 : : /* Tell the importer whether this is a global module entity,
8173 : : or a module entity. */
8174 : 101443 : tree o = get_originating_module_decl (decl);
8175 : 101443 : bool is_attached = false;
8176 : :
8177 : 101443 : tree not_tmpl = STRIP_TEMPLATE (o);
8178 : 101443 : if (DECL_LANG_SPECIFIC (not_tmpl)
8179 : 158699 : && DECL_MODULE_ATTACH_P (not_tmpl))
8180 : : is_attached = true;
8181 : :
8182 : 101443 : bits.b (is_attached);
8183 : : }
8184 : 854337 : bits.b (dep && dep->has_defn ());
8185 : 854337 : }
8186 : 997864 : tree_node_bools (decl);
8187 : : }
8188 : :
8189 : 2760273 : int tag = insert (decl, WK_value);
8190 : 2760273 : if (streaming_p ())
8191 : 997864 : dump (dumper::TREE)
8192 : 642 : && dump ("Writing %s:%d %C:%N%S", merge_kind_name[mk], tag,
8193 : 642 : TREE_CODE (decl), decl, decl);
8194 : :
8195 : 2760273 : tree inner = decl;
8196 : 2760273 : int inner_tag = 0;
8197 : 2760273 : if (TREE_CODE (decl) == TEMPLATE_DECL)
8198 : : {
8199 : 823363 : inner = DECL_TEMPLATE_RESULT (decl);
8200 : 823363 : inner_tag = insert (inner, WK_value);
8201 : :
8202 : 823363 : if (streaming_p ())
8203 : : {
8204 : 274442 : int code = TREE_CODE (inner);
8205 : 274442 : u (code);
8206 : 274442 : start (inner, true);
8207 : 274442 : tree_node_bools (inner);
8208 : 274442 : dump (dumper::TREE)
8209 : 132 : && dump ("Writing %s:%d %C:%N%S", merge_kind_name[mk], inner_tag,
8210 : 132 : TREE_CODE (inner), inner, inner);
8211 : : }
8212 : : }
8213 : :
8214 : 2760273 : tree type = NULL_TREE;
8215 : 2760273 : int type_tag = 0;
8216 : 2760273 : tree stub_decl = NULL_TREE;
8217 : 2760273 : int stub_tag = 0;
8218 : 2760273 : if (TREE_CODE (inner) == TYPE_DECL)
8219 : : {
8220 : 1086907 : type = TREE_TYPE (inner);
8221 : 1086907 : bool has_type = (type == TYPE_MAIN_VARIANT (type)
8222 : 1086907 : && TYPE_NAME (type) == inner);
8223 : :
8224 : 1086907 : if (streaming_p ())
8225 : 366201 : u (has_type ? TREE_CODE (type) : 0);
8226 : :
8227 : 1086907 : if (has_type)
8228 : : {
8229 : 498229 : type_tag = insert (type, WK_value);
8230 : 498229 : if (streaming_p ())
8231 : : {
8232 : 166056 : start (type, true);
8233 : 166056 : tree_node_bools (type);
8234 : 166056 : dump (dumper::TREE)
8235 : 153 : && dump ("Writing type:%d %C:%N", type_tag,
8236 : 153 : TREE_CODE (type), type);
8237 : : }
8238 : :
8239 : 498229 : stub_decl = TYPE_STUB_DECL (type);
8240 : 498229 : bool has_stub = inner != stub_decl;
8241 : 498229 : if (streaming_p ())
8242 : 166056 : u (has_stub ? TREE_CODE (stub_decl) : 0);
8243 : 498229 : if (has_stub)
8244 : : {
8245 : 2187 : stub_tag = insert (stub_decl);
8246 : 2187 : if (streaming_p ())
8247 : : {
8248 : 728 : start (stub_decl, true);
8249 : 728 : tree_node_bools (stub_decl);
8250 : 728 : dump (dumper::TREE)
8251 : 0 : && dump ("Writing stub_decl:%d %C:%N", stub_tag,
8252 : 0 : TREE_CODE (stub_decl), stub_decl);
8253 : : }
8254 : : }
8255 : : else
8256 : : stub_decl = NULL_TREE;
8257 : : }
8258 : : else
8259 : : /* Regular typedef. */
8260 : : type = NULL_TREE;
8261 : : }
8262 : :
8263 : : /* Stream the container, we want it correctly canonicalized before
8264 : : we start emitting keys for this decl. */
8265 : 2760273 : tree container = decl_container (decl);
8266 : 2760273 : unsigned tpl_levels = 0;
8267 : :
8268 : : /* Also tell the importer whether this is a temploid friend attached
8269 : : to a different module (which has implications for merging), so that
8270 : : importers can reconstruct this information on stream-in. */
8271 : 2760273 : if (TREE_CODE (inner) == FUNCTION_DECL || TREE_CODE (inner) == TYPE_DECL)
8272 : : {
8273 : 2177086 : tree* temploid_friend_slot = imported_temploid_friends->get (decl);
8274 : 2177086 : gcc_checking_assert (!temploid_friend_slot || *temploid_friend_slot);
8275 : 2177086 : tree_node (temploid_friend_slot ? *temploid_friend_slot : NULL_TREE);
8276 : : }
8277 : :
8278 : 2760273 : {
8279 : 2760273 : auto wmk = make_temp_override (dep_hash->writing_merge_key, true);
8280 : 2760273 : if (decl != inner)
8281 : 823363 : tpl_header (decl, &tpl_levels);
8282 : 2760273 : if (TREE_CODE (inner) == FUNCTION_DECL)
8283 : 1090179 : fn_parms_init (inner);
8284 : :
8285 : : /* Now write out the merging information, and then really
8286 : : install the tag values. */
8287 : 2760273 : key_mergeable (tag, mk, decl, inner, container, dep);
8288 : :
8289 : 2760273 : if (streaming_p ())
8290 : 2762799 : dump (dumper::MERGE)
8291 : 798 : && dump ("Wrote:%d's %s merge key %C:%N", tag,
8292 : 798 : merge_kind_name[mk], TREE_CODE (decl), decl);
8293 : 2760273 : }
8294 : :
8295 : 2760273 : if (TREE_CODE (inner) == FUNCTION_DECL)
8296 : 2760273 : fn_parms_fini (inner);
8297 : :
8298 : 2760273 : if (!is_key_order ())
8299 : 2005007 : tree_node_vals (decl);
8300 : :
8301 : 2760273 : if (inner_tag)
8302 : : {
8303 : 823363 : if (!is_key_order ())
8304 : 548921 : tree_node_vals (inner);
8305 : 823363 : tpl_parms_fini (decl, tpl_levels);
8306 : : }
8307 : :
8308 : 2760273 : if (type && !is_key_order ())
8309 : : {
8310 : 332173 : tree_node_vals (type);
8311 : 332173 : if (stub_decl)
8312 : 1459 : tree_node_vals (stub_decl);
8313 : : }
8314 : :
8315 : 2760273 : if (!is_key_order ())
8316 : : {
8317 : 2005007 : if (mk & MK_template_mask
8318 : 1392411 : || mk == MK_partial
8319 : 1392411 : || mk == MK_friend_spec)
8320 : : {
8321 : 30508 : if (mk != MK_partial)
8322 : : {
8323 : : // FIXME: We should make use of the merge-key by
8324 : : // exposing it outside of key_mergeable. But this gets
8325 : : // the job done.
8326 : 612596 : auto *entry = reinterpret_cast <spec_entry *> (dep->deps[0]);
8327 : :
8328 : 612596 : if (streaming_p ())
8329 : 306298 : u (get_mergeable_specialization_flags (mk & MK_tmpl_decl_mask,
8330 : : entry->tmpl, decl));
8331 : 612596 : tree_node (entry->tmpl);
8332 : 612596 : tree_node (entry->args);
8333 : : }
8334 : : else
8335 : : {
8336 : 30508 : tree ti = get_template_info (inner);
8337 : 30508 : tree_node (TI_TEMPLATE (ti));
8338 : 30508 : tree_node (TI_ARGS (ti));
8339 : : }
8340 : : }
8341 : 2005007 : tree_node (get_constraints (decl));
8342 : : }
8343 : :
8344 : 2760273 : if (streaming_p ())
8345 : : {
8346 : : /* Do not stray outside this section. */
8347 : 997864 : gcc_checking_assert (!dep || dep->section == dep_hash->section);
8348 : :
8349 : : /* Write the entity index, so we can insert it as soon as we
8350 : : know this is new. */
8351 : 997864 : install_entity (decl, dep);
8352 : : }
8353 : :
8354 : 2760273 : if (DECL_LANG_SPECIFIC (inner)
8355 : 2500581 : && DECL_MODULE_KEYED_DECLS_P (inner)
8356 : 2760451 : && !is_key_order ())
8357 : : {
8358 : : /* Stream the keyed entities. */
8359 : 122 : auto *attach_vec = keyed_table->get (inner);
8360 : 122 : unsigned num = attach_vec->length ();
8361 : 122 : if (streaming_p ())
8362 : 56 : u (num);
8363 : 256 : for (unsigned ix = 0; ix != num; ix++)
8364 : : {
8365 : 134 : tree attached = (*attach_vec)[ix];
8366 : 134 : tree_node (attached);
8367 : 134 : if (streaming_p ())
8368 : 149 : dump (dumper::MERGE)
8369 : 15 : && dump ("Written %d[%u] attached decl %N", tag, ix, attached);
8370 : : }
8371 : : }
8372 : :
8373 : 2760273 : bool is_typedef = false;
8374 : 2760273 : if (!type && TREE_CODE (inner) == TYPE_DECL)
8375 : : {
8376 : 588678 : tree t = TREE_TYPE (inner);
8377 : 588678 : unsigned tdef_flags = 0;
8378 : 588678 : if (DECL_ORIGINAL_TYPE (inner)
8379 : 588678 : && TYPE_NAME (TREE_TYPE (inner)) == inner)
8380 : : {
8381 : 588678 : tdef_flags |= 1;
8382 : 588678 : if (TYPE_STRUCTURAL_EQUALITY_P (t)
8383 : 125463 : && TYPE_DEPENDENT_P_VALID (t)
8384 : 707322 : && TYPE_DEPENDENT_P (t))
8385 : : tdef_flags |= 2;
8386 : : }
8387 : 588678 : if (streaming_p ())
8388 : 200145 : u (tdef_flags);
8389 : :
8390 : 588678 : if (tdef_flags & 1)
8391 : : {
8392 : : /* A typedef type. */
8393 : 588678 : int type_tag = insert (t);
8394 : 588678 : if (streaming_p ())
8395 : 200145 : dump (dumper::TREE)
8396 : 198 : && dump ("Cloned:%d %s %C:%N", type_tag,
8397 : : tdef_flags & 2 ? "depalias" : "typedef",
8398 : 198 : TREE_CODE (t), t);
8399 : :
8400 : : is_typedef = true;
8401 : : }
8402 : : }
8403 : :
8404 : 2760273 : if (streaming_p () && DECL_MAYBE_IN_CHARGE_CDTOR_P (decl))
8405 : : {
8406 : 77061 : bool cloned_p
8407 : 77061 : = (DECL_CHAIN (decl) && DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl)));
8408 : 53867 : bool needs_vtt_parm_p
8409 : 53867 : = (cloned_p && CLASSTYPE_VBASECLASSES (DECL_CONTEXT (decl)));
8410 : 53867 : bool omit_inherited_parms_p
8411 : 53867 : = (cloned_p && DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl)
8412 : 42556 : && base_ctor_omit_inherited_parms (decl));
8413 : 77061 : unsigned flags = (int (cloned_p) << 0
8414 : 77061 : | int (needs_vtt_parm_p) << 1
8415 : 77061 : | int (omit_inherited_parms_p) << 2);
8416 : 77061 : u (flags);
8417 : 77136 : dump (dumper::TREE) && dump ("CDTOR %N is %scloned",
8418 : : decl, cloned_p ? "" : "not ");
8419 : : }
8420 : :
8421 : 2760273 : if (streaming_p () && VAR_P (decl) && CP_DECL_THREAD_LOCAL_P (decl))
8422 : 112 : u (decl_tls_model (decl));
8423 : :
8424 : 2760273 : if (streaming_p ())
8425 : 997864 : dump (dumper::TREE) && dump ("Written decl:%d %C:%N", tag,
8426 : 642 : TREE_CODE (decl), decl);
8427 : :
8428 : 2760273 : if (NAMESPACE_SCOPE_P (inner))
8429 : 1712206 : gcc_checking_assert (!dep == (VAR_OR_FUNCTION_DECL_P (inner)
8430 : : && DECL_LOCAL_DECL_P (inner)));
8431 : 1904060 : else if ((TREE_CODE (inner) == TYPE_DECL
8432 : 578999 : && !is_typedef
8433 : 108818 : && TYPE_NAME (TREE_TYPE (inner)) == inner)
8434 : 2374241 : || TREE_CODE (inner) == FUNCTION_DECL)
8435 : : {
8436 : 905256 : bool write_defn = !dep && has_definition (decl);
8437 : 905256 : if (streaming_p ())
8438 : 301895 : u (write_defn);
8439 : 905256 : if (write_defn)
8440 : 0 : write_definition (decl);
8441 : : }
8442 : : }
8443 : :
8444 : : tree
8445 : 817691 : trees_in::decl_value ()
8446 : : {
8447 : 817691 : int tag = 0;
8448 : 817691 : bool is_attached = false;
8449 : 817691 : bool has_defn = false;
8450 : 817691 : unsigned mk_u = u ();
8451 : 817691 : if (mk_u >= MK_hwm || !merge_kind_name[mk_u])
8452 : : {
8453 : 0 : set_overrun ();
8454 : 0 : return NULL_TREE;
8455 : : }
8456 : :
8457 : 817691 : unsigned saved_unused = unused;
8458 : 817691 : unused = 0;
8459 : :
8460 : 817691 : merge_kind mk = merge_kind (mk_u);
8461 : :
8462 : 817691 : tree decl = start ();
8463 : 817691 : if (decl)
8464 : : {
8465 : 817691 : if (mk != MK_unique)
8466 : : {
8467 : 688480 : bits_in bits = stream_bits ();
8468 : 688480 : if (!(mk & MK_template_mask) && !state->is_header ())
8469 : 43510 : is_attached = bits.b ();
8470 : :
8471 : 688480 : has_defn = bits.b ();
8472 : 688480 : }
8473 : :
8474 : 817691 : if (!tree_node_bools (decl))
8475 : 0 : decl = NULL_TREE;
8476 : : }
8477 : :
8478 : : /* Insert into map. */
8479 : 817691 : tag = insert (decl);
8480 : 817691 : if (decl)
8481 : 817691 : dump (dumper::TREE)
8482 : 936 : && dump ("Reading:%d %C", tag, TREE_CODE (decl));
8483 : :
8484 : 817691 : tree inner = decl;
8485 : 817691 : int inner_tag = 0;
8486 : 817691 : if (decl && TREE_CODE (decl) == TEMPLATE_DECL)
8487 : : {
8488 : 223274 : int code = u ();
8489 : 223274 : inner = start (code);
8490 : 223274 : if (inner && tree_node_bools (inner))
8491 : 223274 : DECL_TEMPLATE_RESULT (decl) = inner;
8492 : : else
8493 : 0 : decl = NULL_TREE;
8494 : :
8495 : 223274 : inner_tag = insert (inner);
8496 : 223274 : if (decl)
8497 : 223274 : dump (dumper::TREE)
8498 : 204 : && dump ("Reading:%d %C", inner_tag, TREE_CODE (inner));
8499 : : }
8500 : :
8501 : 817691 : tree type = NULL_TREE;
8502 : 817691 : int type_tag = 0;
8503 : 817691 : tree stub_decl = NULL_TREE;
8504 : 817691 : int stub_tag = 0;
8505 : 817691 : if (decl && TREE_CODE (inner) == TYPE_DECL)
8506 : : {
8507 : 278562 : if (unsigned type_code = u ())
8508 : : {
8509 : 125710 : type = start (type_code);
8510 : 125710 : if (type && tree_node_bools (type))
8511 : : {
8512 : 125710 : TREE_TYPE (inner) = type;
8513 : 125710 : TYPE_NAME (type) = inner;
8514 : : }
8515 : : else
8516 : 0 : decl = NULL_TREE;
8517 : :
8518 : 125710 : type_tag = insert (type);
8519 : 125710 : if (decl)
8520 : 125710 : dump (dumper::TREE)
8521 : 210 : && dump ("Reading type:%d %C", type_tag, TREE_CODE (type));
8522 : :
8523 : 125710 : if (unsigned stub_code = u ())
8524 : : {
8525 : 420 : stub_decl = start (stub_code);
8526 : 420 : if (stub_decl && tree_node_bools (stub_decl))
8527 : : {
8528 : 420 : TREE_TYPE (stub_decl) = type;
8529 : 420 : TYPE_STUB_DECL (type) = stub_decl;
8530 : : }
8531 : : else
8532 : 0 : decl = NULL_TREE;
8533 : :
8534 : 420 : stub_tag = insert (stub_decl);
8535 : 420 : if (decl)
8536 : 420 : dump (dumper::TREE)
8537 : 0 : && dump ("Reading stub_decl:%d %C", stub_tag,
8538 : 0 : TREE_CODE (stub_decl));
8539 : : }
8540 : : }
8541 : : }
8542 : :
8543 : 817691 : if (!decl)
8544 : : {
8545 : 0 : bail:
8546 : 0 : if (inner_tag != 0)
8547 : 0 : back_refs[~inner_tag] = NULL_TREE;
8548 : 0 : if (type_tag != 0)
8549 : 0 : back_refs[~type_tag] = NULL_TREE;
8550 : 0 : if (stub_tag != 0)
8551 : 0 : back_refs[~stub_tag] = NULL_TREE;
8552 : 0 : if (tag != 0)
8553 : 0 : back_refs[~tag] = NULL_TREE;
8554 : 0 : set_overrun ();
8555 : : /* Bail. */
8556 : 0 : unused = saved_unused;
8557 : 0 : return NULL_TREE;
8558 : : }
8559 : :
8560 : : /* Read the container, to ensure it's already been streamed in. */
8561 : 817691 : tree container = decl_container ();
8562 : 817691 : unsigned tpl_levels = 0;
8563 : :
8564 : : /* If this is an imported temploid friend, get the owning decl its
8565 : : attachment is determined by (or NULL_TREE otherwise). */
8566 : 817691 : tree temploid_friend = NULL_TREE;
8567 : 817691 : if (TREE_CODE (inner) == FUNCTION_DECL || TREE_CODE (inner) == TYPE_DECL)
8568 : 591849 : temploid_friend = tree_node ();
8569 : :
8570 : : /* Figure out if this decl is already known about. */
8571 : 817691 : int parm_tag = 0;
8572 : :
8573 : 817691 : if (decl != inner)
8574 : 223274 : if (!tpl_header (decl, &tpl_levels))
8575 : 0 : goto bail;
8576 : 817691 : if (TREE_CODE (inner) == FUNCTION_DECL)
8577 : 313287 : parm_tag = fn_parms_init (inner);
8578 : :
8579 : 817691 : tree existing = key_mergeable (tag, mk, decl, inner, type, container,
8580 : 817691 : is_attached, temploid_friend);
8581 : 817691 : tree existing_inner = existing;
8582 : 817691 : if (existing)
8583 : : {
8584 : 398806 : if (existing == error_mark_node)
8585 : 0 : goto bail;
8586 : :
8587 : 398806 : if (TREE_CODE (STRIP_TEMPLATE (existing)) == TYPE_DECL)
8588 : : {
8589 : 153718 : tree etype = TREE_TYPE (existing);
8590 : 153718 : if (TYPE_LANG_SPECIFIC (etype)
8591 : 111257 : && COMPLETE_TYPE_P (etype)
8592 : 219374 : && !CLASSTYPE_MEMBER_VEC (etype))
8593 : : /* Give it a member vec, we're likely gonna be looking
8594 : : inside it. */
8595 : 14075 : set_class_bindings (etype, -1);
8596 : : }
8597 : :
8598 : : /* Install the existing decl into the back ref array. */
8599 : 398806 : register_duplicate (decl, existing);
8600 : 398806 : back_refs[~tag] = existing;
8601 : 398806 : if (inner_tag != 0)
8602 : : {
8603 : 135780 : existing_inner = DECL_TEMPLATE_RESULT (existing);
8604 : 135780 : back_refs[~inner_tag] = existing_inner;
8605 : : }
8606 : :
8607 : 398806 : if (type_tag != 0)
8608 : : {
8609 : 73086 : tree existing_type = TREE_TYPE (existing);
8610 : 73086 : back_refs[~type_tag] = existing_type;
8611 : 73086 : if (stub_tag != 0)
8612 : 258 : back_refs[~stub_tag] = TYPE_STUB_DECL (existing_type);
8613 : : }
8614 : : }
8615 : :
8616 : 817691 : if (parm_tag)
8617 : 313287 : fn_parms_fini (parm_tag, inner, existing_inner, has_defn);
8618 : :
8619 : 817691 : if (!tree_node_vals (decl))
8620 : 0 : goto bail;
8621 : :
8622 : 817691 : if (inner_tag)
8623 : : {
8624 : 223274 : gcc_checking_assert (DECL_TEMPLATE_RESULT (decl) == inner);
8625 : :
8626 : 223274 : if (!tree_node_vals (inner))
8627 : 0 : goto bail;
8628 : :
8629 : 223274 : if (!tpl_parms_fini (decl, tpl_levels))
8630 : 0 : goto bail;
8631 : : }
8632 : :
8633 : 817691 : if (type && (!tree_node_vals (type)
8634 : 125710 : || (stub_decl && !tree_node_vals (stub_decl))))
8635 : 0 : goto bail;
8636 : :
8637 : 817691 : spec_entry spec;
8638 : 817691 : unsigned spec_flags = 0;
8639 : 817691 : if (mk & MK_template_mask
8640 : 563049 : || mk == MK_partial
8641 : 563049 : || mk == MK_friend_spec)
8642 : : {
8643 : 9331 : if (mk == MK_partial)
8644 : : spec_flags = 2;
8645 : : else
8646 : 254642 : spec_flags = u ();
8647 : :
8648 : 263973 : spec.tmpl = tree_node ();
8649 : 263973 : spec.args = tree_node ();
8650 : : }
8651 : : /* Hold constraints on the spec field, for a short while. */
8652 : 817691 : spec.spec = tree_node ();
8653 : :
8654 : 818627 : dump (dumper::TREE) && dump ("Read:%d %C:%N", tag, TREE_CODE (decl), decl);
8655 : :
8656 : 817691 : existing = back_refs[~tag];
8657 : 817691 : bool installed = install_entity (existing);
8658 : 817691 : bool is_new = existing == decl;
8659 : :
8660 : 817691 : if (DECL_LANG_SPECIFIC (inner)
8661 : 1544433 : && DECL_MODULE_KEYED_DECLS_P (inner))
8662 : : {
8663 : : /* Read and maybe install the attached entities. */
8664 : 79 : bool existed;
8665 : 79 : auto &set = keyed_table->get_or_insert (STRIP_TEMPLATE (existing),
8666 : : &existed);
8667 : 79 : unsigned num = u ();
8668 : 79 : if (is_new == existed)
8669 : 0 : set_overrun ();
8670 : 79 : if (is_new)
8671 : 34 : set.reserve (num);
8672 : 170 : for (unsigned ix = 0; !get_overrun () && ix != num; ix++)
8673 : : {
8674 : 91 : tree attached = tree_node ();
8675 : 91 : dump (dumper::MERGE)
8676 : 58 : && dump ("Read %d[%u] %s attached decl %N", tag, ix,
8677 : : is_new ? "new" : "matched", attached);
8678 : 91 : if (is_new)
8679 : 40 : set.quick_push (attached);
8680 : 51 : else if (set[ix] != attached)
8681 : 0 : set_overrun ();
8682 : : }
8683 : : }
8684 : :
8685 : : /* Regular typedefs will have a NULL TREE_TYPE at this point. */
8686 : 817691 : unsigned tdef_flags = 0;
8687 : 817691 : bool is_typedef = false;
8688 : 817691 : if (!type && TREE_CODE (inner) == TYPE_DECL)
8689 : : {
8690 : 152852 : tdef_flags = u ();
8691 : 152852 : if (tdef_flags & 1)
8692 : 152852 : is_typedef = true;
8693 : : }
8694 : :
8695 : 817691 : if (is_new)
8696 : : {
8697 : : /* A newly discovered node. */
8698 : 418885 : if (TREE_CODE (decl) == FUNCTION_DECL && DECL_VIRTUAL_P (decl))
8699 : : /* Mark this identifier as naming a virtual function --
8700 : : lookup_overrides relies on this optimization. */
8701 : 4013 : IDENTIFIER_VIRTUAL_P (DECL_NAME (decl)) = true;
8702 : :
8703 : 418885 : if (installed)
8704 : : {
8705 : : /* Mark the entity as imported. */
8706 : 257114 : retrofit_lang_decl (inner);
8707 : 257114 : DECL_MODULE_IMPORT_P (inner) = true;
8708 : : }
8709 : :
8710 : 418885 : if (temploid_friend)
8711 : 22 : imported_temploid_friends->put (decl, temploid_friend);
8712 : :
8713 : 418885 : if (spec.spec)
8714 : 14628 : set_constraints (decl, spec.spec);
8715 : :
8716 : 418885 : if (TREE_CODE (decl) == INTEGER_CST && !TREE_OVERFLOW (decl))
8717 : : {
8718 : 0 : decl = cache_integer_cst (decl, true);
8719 : 0 : back_refs[~tag] = decl;
8720 : : }
8721 : :
8722 : 418885 : if (is_typedef)
8723 : : {
8724 : : /* Frob it to be ready for cloning. */
8725 : 72220 : TREE_TYPE (inner) = DECL_ORIGINAL_TYPE (inner);
8726 : 72220 : DECL_ORIGINAL_TYPE (inner) = NULL_TREE;
8727 : 72220 : if (TREE_CODE (TREE_TYPE (inner)) != TU_LOCAL_ENTITY)
8728 : : {
8729 : 72217 : set_underlying_type (inner);
8730 : 72217 : if (tdef_flags & 2)
8731 : : {
8732 : : /* Match instantiate_alias_template's handling. */
8733 : 17394 : tree type = TREE_TYPE (inner);
8734 : 17394 : TYPE_DEPENDENT_P (type) = true;
8735 : 17394 : TYPE_DEPENDENT_P_VALID (type) = true;
8736 : 17394 : SET_TYPE_STRUCTURAL_EQUALITY (type);
8737 : : }
8738 : : }
8739 : : }
8740 : :
8741 : 418885 : if (inner_tag)
8742 : : /* Set the TEMPLATE_DECL's type. */
8743 : 87494 : TREE_TYPE (decl) = TREE_TYPE (inner);
8744 : :
8745 : : /* Redetermine whether we need to import or export this declaration
8746 : : for this TU. But for extern templates we know we must import:
8747 : : they'll be defined in a different TU.
8748 : : FIXME: How do dllexport and dllimport interact across a module?
8749 : : See also https://github.com/itanium-cxx-abi/cxx-abi/issues/170.
8750 : : May have to revisit? */
8751 : 418885 : if (type
8752 : 52624 : && CLASS_TYPE_P (type)
8753 : 44561 : && TYPE_LANG_SPECIFIC (type)
8754 : 463446 : && !(CLASSTYPE_EXPLICIT_INSTANTIATION (type)
8755 : 514 : && CLASSTYPE_INTERFACE_KNOWN (type)
8756 : 514 : && CLASSTYPE_INTERFACE_ONLY (type)))
8757 : : {
8758 : 44087 : CLASSTYPE_INTERFACE_ONLY (type) = false;
8759 : 44087 : CLASSTYPE_INTERFACE_UNKNOWN (type) = true;
8760 : : }
8761 : :
8762 : : /* Add to specialization tables now that constraints etc are
8763 : : added. */
8764 : 418885 : if (mk == MK_partial)
8765 : : {
8766 : 3321 : bool is_type = TREE_CODE (inner) == TYPE_DECL;
8767 : 3321 : spec.spec = is_type ? type : inner;
8768 : 3321 : add_mergeable_specialization (!is_type, &spec, decl, spec_flags);
8769 : : }
8770 : 415564 : else if (mk & MK_template_mask)
8771 : : {
8772 : 114053 : bool is_type = !(mk & MK_tmpl_decl_mask);
8773 : 114053 : spec.spec = is_type ? type : mk & MK_tmpl_tmpl_mask ? inner : decl;
8774 : 114053 : add_mergeable_specialization (!is_type, &spec, decl, spec_flags);
8775 : : }
8776 : :
8777 : 418885 : if (NAMESPACE_SCOPE_P (decl)
8778 : 90482 : && (mk == MK_named || mk == MK_unique
8779 : 90482 : || mk == MK_enum || mk == MK_friend_spec)
8780 : 463093 : && !(VAR_OR_FUNCTION_DECL_P (decl) && DECL_LOCAL_DECL_P (decl)))
8781 : 44138 : add_module_namespace_decl (CP_DECL_CONTEXT (decl), decl);
8782 : :
8783 : 418885 : if (DECL_ARTIFICIAL (decl)
8784 : 117923 : && TREE_CODE (decl) == FUNCTION_DECL
8785 : 11562 : && !DECL_TEMPLATE_INFO (decl)
8786 : 11362 : && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
8787 : 11244 : && TYPE_SIZE (DECL_CONTEXT (decl))
8788 : 420123 : && !DECL_THUNK_P (decl))
8789 : : /* A new implicit member function, when the class is
8790 : : complete. This means the importee declared it, and
8791 : : we must now add it to the class. Note that implicit
8792 : : member fns of template instantiations do not themselves
8793 : : look like templates. */
8794 : 748 : if (!install_implicit_member (inner))
8795 : 0 : set_overrun ();
8796 : :
8797 : : /* When importing a TLS wrapper from a header unit, we haven't
8798 : : actually emitted its definition yet. Remember it so we can
8799 : : do this later. */
8800 : 418885 : if (state->is_header ()
8801 : 418885 : && decl_tls_wrapper_p (decl))
8802 : 6 : note_vague_linkage_fn (decl);
8803 : :
8804 : : /* Setup aliases for the declaration. */
8805 : 418885 : if (tree alias = lookup_attribute ("alias", DECL_ATTRIBUTES (decl)))
8806 : : {
8807 : 3 : alias = TREE_VALUE (TREE_VALUE (alias));
8808 : 3 : alias = get_identifier (TREE_STRING_POINTER (alias));
8809 : 3 : assemble_alias (decl, alias);
8810 : : }
8811 : : }
8812 : : else
8813 : : {
8814 : : /* DECL is the to-be-discarded decl. Its internal pointers will
8815 : : be to the EXISTING's structure. Frob it to point to its
8816 : : own other structures, so loading its definition will alter
8817 : : it, and not the existing decl. */
8818 : 400220 : dump (dumper::MERGE) && dump ("Deduping %N", existing);
8819 : :
8820 : 398806 : if (inner_tag)
8821 : 135780 : DECL_TEMPLATE_RESULT (decl) = inner;
8822 : :
8823 : 398806 : if (type)
8824 : : {
8825 : : /* Point at the to-be-discarded type & decl. */
8826 : 73086 : TYPE_NAME (type) = inner;
8827 : 73086 : TREE_TYPE (inner) = type;
8828 : :
8829 : 145914 : TYPE_STUB_DECL (type) = stub_decl ? stub_decl : inner;
8830 : 73086 : if (stub_decl)
8831 : 258 : TREE_TYPE (stub_decl) = type;
8832 : :
8833 : 73086 : tree etype = TREE_TYPE (existing);
8834 : :
8835 : : /* Handle separate declarations with different attributes. */
8836 : 73086 : tree &dattr = TYPE_ATTRIBUTES (type);
8837 : 73086 : tree &eattr = TYPE_ATTRIBUTES (etype);
8838 : 73086 : check_abi_tags (existing, decl, eattr, dattr);
8839 : : // TODO: handle other conflicting type attributes
8840 : 73086 : eattr = merge_attributes (eattr, dattr);
8841 : :
8842 : : /* When merging a partial specialisation, the existing decl may have
8843 : : had its TYPE_CANONICAL adjusted. If so we should use structural
8844 : : equality to ensure is_matching_decl doesn't get confused. */
8845 : 73086 : if ((spec_flags & 2)
8846 : 73086 : && TYPE_CANONICAL (type) != TYPE_CANONICAL (etype))
8847 : 3 : SET_TYPE_STRUCTURAL_EQUALITY (type);
8848 : : }
8849 : :
8850 : 398806 : if (inner_tag)
8851 : : /* Set the TEMPLATE_DECL's type. */
8852 : 135780 : TREE_TYPE (decl) = TREE_TYPE (inner);
8853 : :
8854 : 398806 : if (!is_matching_decl (existing, decl, is_typedef))
8855 : 36 : unmatched_duplicate (existing);
8856 : :
8857 : 398806 : if (TREE_CODE (inner) == FUNCTION_DECL)
8858 : : {
8859 : 181932 : tree e_inner = STRIP_TEMPLATE (existing);
8860 : 181932 : for (auto parm = DECL_ARGUMENTS (inner);
8861 : 549364 : parm; parm = DECL_CHAIN (parm))
8862 : 367432 : DECL_CONTEXT (parm) = e_inner;
8863 : : }
8864 : :
8865 : : /* And our result is the existing node. */
8866 : 398806 : decl = existing;
8867 : : }
8868 : :
8869 : 817691 : if (mk == MK_friend_spec)
8870 : : {
8871 : 0 : tree e = match_mergeable_specialization (true, &spec);
8872 : 0 : if (!e)
8873 : : {
8874 : 0 : spec.spec = inner;
8875 : 0 : add_mergeable_specialization (true, &spec, decl, spec_flags);
8876 : : }
8877 : 0 : else if (e != existing)
8878 : 0 : set_overrun ();
8879 : : }
8880 : :
8881 : 817691 : if (is_typedef)
8882 : : {
8883 : : /* Insert the type into the array now. */
8884 : 152852 : tag = insert (TREE_TYPE (decl));
8885 : 152852 : dump (dumper::TREE)
8886 : 243 : && dump ("Cloned:%d typedef %C:%N",
8887 : 243 : tag, TREE_CODE (TREE_TYPE (decl)), TREE_TYPE (decl));
8888 : : }
8889 : :
8890 : 817691 : unused = saved_unused;
8891 : :
8892 : 817691 : if (DECL_MAYBE_IN_CHARGE_CDTOR_P (decl))
8893 : : {
8894 : 67043 : unsigned flags = u ();
8895 : :
8896 : 67043 : if (is_new)
8897 : : {
8898 : 29608 : bool cloned_p = flags & 1;
8899 : 29704 : dump (dumper::TREE) && dump ("CDTOR %N is %scloned",
8900 : : decl, cloned_p ? "" : "not ");
8901 : 29608 : if (cloned_p)
8902 : 43566 : build_cdtor_clones (decl, flags & 2, flags & 4,
8903 : : /* Update the member vec, if there is
8904 : : one (we're in a different cluster
8905 : : to the class defn). */
8906 : 21783 : CLASSTYPE_MEMBER_VEC (DECL_CONTEXT (decl)));
8907 : : }
8908 : : }
8909 : :
8910 : 817691 : if (VAR_P (decl) && CP_DECL_THREAD_LOCAL_P (decl))
8911 : : {
8912 : 101 : enum tls_model model = tls_model (u ());
8913 : 101 : if (is_new)
8914 : 93 : set_decl_tls_model (decl, model);
8915 : : }
8916 : :
8917 : 817691 : if (!NAMESPACE_SCOPE_P (inner)
8918 : 592921 : && ((TREE_CODE (inner) == TYPE_DECL
8919 : 149196 : && !is_typedef
8920 : 26261 : && TYPE_NAME (TREE_TYPE (inner)) == inner)
8921 : 566660 : || TREE_CODE (inner) == FUNCTION_DECL)
8922 : 1075291 : && u ())
8923 : 0 : read_definition (decl);
8924 : :
8925 : : return decl;
8926 : : }
8927 : :
8928 : : /* DECL is an unnameable member of CTX. Return a suitable identifying
8929 : : index. */
8930 : :
8931 : : static unsigned
8932 : 843 : get_field_ident (tree ctx, tree decl)
8933 : : {
8934 : 843 : gcc_checking_assert (TREE_CODE (decl) == USING_DECL
8935 : : || !DECL_NAME (decl)
8936 : : || IDENTIFIER_ANON_P (DECL_NAME (decl)));
8937 : :
8938 : 843 : unsigned ix = 0;
8939 : 843 : for (tree fields = TYPE_FIELDS (ctx);
8940 : 7178 : fields; fields = DECL_CHAIN (fields))
8941 : : {
8942 : 7178 : if (fields == decl)
8943 : 843 : return ix;
8944 : :
8945 : 6335 : if (DECL_CONTEXT (fields) == ctx
8946 : 6335 : && (TREE_CODE (fields) == USING_DECL
8947 : 6327 : || (TREE_CODE (fields) == FIELD_DECL
8948 : 19 : && (!DECL_NAME (fields)
8949 : 7 : || IDENTIFIER_ANON_P (DECL_NAME (fields))))))
8950 : : /* Count this field. */
8951 : 12 : ix++;
8952 : : }
8953 : 0 : gcc_unreachable ();
8954 : : }
8955 : :
8956 : : static tree
8957 : 717 : lookup_field_ident (tree ctx, unsigned ix)
8958 : : {
8959 : 717 : for (tree fields = TYPE_FIELDS (ctx);
8960 : 5815 : fields; fields = DECL_CHAIN (fields))
8961 : 5815 : if (DECL_CONTEXT (fields) == ctx
8962 : 5815 : && (TREE_CODE (fields) == USING_DECL
8963 : 5809 : || (TREE_CODE (fields) == FIELD_DECL
8964 : 732 : && (!DECL_NAME (fields)
8965 : 3 : || IDENTIFIER_ANON_P (DECL_NAME (fields))))))
8966 : 729 : if (!ix--)
8967 : : return fields;
8968 : :
8969 : : return NULL_TREE;
8970 : : }
8971 : :
8972 : : /* Reference DECL. REF indicates the walk kind we are performing.
8973 : : Return true if we should write this decl by value. */
8974 : :
8975 : : bool
8976 : 9106503 : trees_out::decl_node (tree decl, walk_kind ref)
8977 : : {
8978 : 9106503 : gcc_checking_assert (DECL_P (decl) && !DECL_TEMPLATE_PARM_P (decl)
8979 : : && DECL_CONTEXT (decl));
8980 : :
8981 : 9106503 : if (ref == WK_value)
8982 : : {
8983 : 956523 : depset *dep = dep_hash->find_dependency (decl);
8984 : 956523 : decl_value (decl, dep);
8985 : 956523 : return false;
8986 : : }
8987 : :
8988 : 8149980 : switch (TREE_CODE (decl))
8989 : : {
8990 : : default:
8991 : : break;
8992 : :
8993 : 849718 : case FUNCTION_DECL:
8994 : 849718 : gcc_checking_assert (!DECL_LOCAL_DECL_P (decl));
8995 : : break;
8996 : :
8997 : : case RESULT_DECL:
8998 : : /* Unlike PARM_DECLs, RESULT_DECLs are only generated and
8999 : : referenced when we're inside the function itself. */
9000 : : return true;
9001 : :
9002 : 128430 : case PARM_DECL:
9003 : 128430 : {
9004 : 128430 : if (streaming_p ())
9005 : 57761 : i (tt_parm);
9006 : 128430 : tree_node (DECL_CONTEXT (decl));
9007 : :
9008 : : /* That must have put this in the map. */
9009 : 128430 : walk_kind ref = ref_node (decl);
9010 : 128430 : if (ref != WK_none)
9011 : : // FIXME:OPTIMIZATION We can wander into bits of the
9012 : : // template this was instantiated from, for instance
9013 : : // deferred noexcept and default parms, or references
9014 : : // to parms from earlier forward-decls (PR c++/119608).
9015 : : //
9016 : : // Currently we'll end up cloning those bits of tree.
9017 : : // It would be nice to reference those specific nodes.
9018 : : // I think putting those things in the map when we
9019 : : // reference their template by name.
9020 : : //
9021 : : // See the note in add_indirects.
9022 : : return true;
9023 : :
9024 : 0 : if (streaming_p ())
9025 : 0 : dump (dumper::TREE)
9026 : 0 : && dump ("Wrote %s reference %N",
9027 : 0 : TREE_CODE (decl) == PARM_DECL ? "parameter" : "result",
9028 : : decl);
9029 : : }
9030 : : return false;
9031 : :
9032 : : case IMPORTED_DECL:
9033 : : /* This describes a USING_DECL to the ME's debug machinery. It
9034 : : originates from the fortran FE, and has nothing to do with
9035 : : C++ modules. */
9036 : : return true;
9037 : :
9038 : : case LABEL_DECL:
9039 : : return true;
9040 : :
9041 : 33025 : case CONST_DECL:
9042 : 33025 : {
9043 : : /* If I end up cloning enum decls, implementing C++20 using
9044 : : E::v, this will need tweaking. */
9045 : 33025 : if (streaming_p ())
9046 : 5995 : i (tt_enum_decl);
9047 : 33025 : tree ctx = DECL_CONTEXT (decl);
9048 : 33025 : gcc_checking_assert (TREE_CODE (ctx) == ENUMERAL_TYPE);
9049 : 33025 : tree_node (ctx);
9050 : 33025 : tree_node (DECL_NAME (decl));
9051 : :
9052 : 33025 : int tag = insert (decl);
9053 : 33025 : if (streaming_p ())
9054 : 5995 : dump (dumper::TREE)
9055 : 21 : && dump ("Wrote enum decl:%d %C:%N", tag, TREE_CODE (decl), decl);
9056 : : return false;
9057 : : }
9058 : 4133 : break;
9059 : :
9060 : 4133 : case USING_DECL:
9061 : 4133 : if (TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
9062 : : break;
9063 : : /* FALLTHROUGH */
9064 : :
9065 : 107115 : case FIELD_DECL:
9066 : 107115 : {
9067 : 107115 : if (streaming_p ())
9068 : 5631 : i (tt_data_member);
9069 : :
9070 : 107115 : tree ctx = DECL_CONTEXT (decl);
9071 : 107115 : tree_node (ctx);
9072 : :
9073 : 107115 : tree name = NULL_TREE;
9074 : :
9075 : 107115 : if (TREE_CODE (decl) == USING_DECL)
9076 : : ;
9077 : : else
9078 : : {
9079 : 106424 : name = DECL_NAME (decl);
9080 : 206995 : if (name && IDENTIFIER_ANON_P (name))
9081 : : name = NULL_TREE;
9082 : : }
9083 : :
9084 : 107115 : tree_node (name);
9085 : 107115 : if (!name && streaming_p ())
9086 : : {
9087 : 843 : unsigned ix = get_field_ident (ctx, decl);
9088 : 843 : u (ix);
9089 : : }
9090 : :
9091 : 107115 : int tag = insert (decl);
9092 : 107115 : if (streaming_p ())
9093 : 5631 : dump (dumper::TREE)
9094 : 21 : && dump ("Wrote member:%d %C:%N", tag, TREE_CODE (decl), decl);
9095 : : return false;
9096 : : }
9097 : 338993 : break;
9098 : :
9099 : 338993 : case VAR_DECL:
9100 : 338993 : gcc_checking_assert (!DECL_LOCAL_DECL_P (decl));
9101 : 338993 : if (DECL_VTABLE_OR_VTT_P (decl))
9102 : : {
9103 : : /* VTT or VTABLE, they are all on the vtables list. */
9104 : 1327 : tree ctx = CP_DECL_CONTEXT (decl);
9105 : 1327 : tree vtable = CLASSTYPE_VTABLES (ctx);
9106 : 1339 : for (unsigned ix = 0; ; vtable = DECL_CHAIN (vtable), ix++)
9107 : 1339 : if (vtable == decl)
9108 : : {
9109 : 1327 : gcc_checking_assert (DECL_VIRTUAL_P (decl));
9110 : 1327 : if (streaming_p ())
9111 : : {
9112 : 8 : u (tt_vtable);
9113 : 8 : u (ix);
9114 : 8 : dump (dumper::TREE)
9115 : 0 : && dump ("Writing vtable %N[%u]", ctx, ix);
9116 : : }
9117 : 1327 : tree_node (ctx);
9118 : 1327 : return false;
9119 : : }
9120 : : gcc_unreachable ();
9121 : : }
9122 : :
9123 : 337666 : if (DECL_TINFO_P (decl))
9124 : : {
9125 : 5246 : tinfo:
9126 : : /* A typeinfo, tt_tinfo_typedef or tt_tinfo_var. */
9127 : 9627 : bool is_var = VAR_P (decl);
9128 : 9627 : tree type = TREE_TYPE (decl);
9129 : 9627 : unsigned ix = get_pseudo_tinfo_index (type);
9130 : 9627 : if (streaming_p ())
9131 : : {
9132 : 6106 : i (is_var ? tt_tinfo_var : tt_tinfo_typedef);
9133 : 4336 : u (ix);
9134 : : }
9135 : :
9136 : 9627 : if (is_var)
9137 : : {
9138 : : /* We also need the type it is for and mangled name, so
9139 : : the reader doesn't need to complete the type (which
9140 : : would break section ordering). The type it is for is
9141 : : stashed on the name's TREE_TYPE. */
9142 : 5246 : tree name = DECL_NAME (decl);
9143 : 5246 : tree_node (name);
9144 : 5246 : type = TREE_TYPE (name);
9145 : 5246 : tree_node (type);
9146 : : }
9147 : :
9148 : 9627 : int tag = insert (decl);
9149 : 9627 : if (streaming_p ())
9150 : 4336 : dump (dumper::TREE)
9151 : 27 : && dump ("Wrote tinfo_%s:%d %u %N", is_var ? "var" : "type",
9152 : : tag, ix, type);
9153 : :
9154 : 9627 : if (!is_var)
9155 : : {
9156 : 4381 : tag = insert (type);
9157 : 4381 : if (streaming_p ())
9158 : 1770 : dump (dumper::TREE)
9159 : 9 : && dump ("Wrote tinfo_type:%d %u %N", tag, ix, type);
9160 : : }
9161 : 9627 : return false;
9162 : : }
9163 : :
9164 : 332420 : if (DECL_NTTP_OBJECT_P (decl))
9165 : : {
9166 : : /* A NTTP parm object. */
9167 : 20 : if (streaming_p ())
9168 : 4 : i (tt_nttp_var);
9169 : 20 : tree_node (tparm_object_argument (decl));
9170 : 20 : tree_node (DECL_NAME (decl));
9171 : 20 : int tag = insert (decl);
9172 : 20 : if (streaming_p ())
9173 : 4 : dump (dumper::TREE)
9174 : 0 : && dump ("Wrote nttp object:%d %N", tag, DECL_NAME (decl));
9175 : 20 : return false;
9176 : : }
9177 : :
9178 : : break;
9179 : :
9180 : 2926687 : case TYPE_DECL:
9181 : 2926687 : if (DECL_TINFO_P (decl))
9182 : 4381 : goto tinfo;
9183 : : break;
9184 : : }
9185 : :
9186 : 7438167 : if (DECL_THUNK_P (decl))
9187 : : {
9188 : : /* Thunks are similar to binfos -- write the thunked-to decl and
9189 : : then thunk-specific key info. */
9190 : 0 : if (streaming_p ())
9191 : : {
9192 : 0 : i (tt_thunk);
9193 : 0 : i (THUNK_FIXED_OFFSET (decl));
9194 : : }
9195 : :
9196 : : tree target = decl;
9197 : 0 : while (DECL_THUNK_P (target))
9198 : 0 : target = THUNK_TARGET (target);
9199 : 0 : tree_node (target);
9200 : 0 : tree_node (THUNK_VIRTUAL_OFFSET (decl));
9201 : 0 : int tag = insert (decl);
9202 : 0 : if (streaming_p ())
9203 : 0 : dump (dumper::TREE)
9204 : 0 : && dump ("Wrote:%d thunk %N to %N", tag, DECL_NAME (decl), target);
9205 : 0 : return false;
9206 : : }
9207 : :
9208 : 7438167 : if (DECL_CLONED_FUNCTION_P (decl))
9209 : : {
9210 : 247206 : tree target = get_clone_target (decl);
9211 : 247206 : if (streaming_p ())
9212 : 117059 : i (tt_clone_ref);
9213 : :
9214 : 247206 : tree_node (target);
9215 : 247206 : tree_node (DECL_NAME (decl));
9216 : 247206 : if (DECL_VIRTUAL_P (decl))
9217 : 18943 : tree_node (DECL_VINDEX (decl));
9218 : 247206 : int tag = insert (decl);
9219 : 247206 : if (streaming_p ())
9220 : 117059 : dump (dumper::TREE)
9221 : 156 : && dump ("Wrote:%d clone %N of %N", tag, DECL_NAME (decl), target);
9222 : 247206 : return false;
9223 : : }
9224 : :
9225 : : /* Everything left should be a thing that is in the entity table.
9226 : : Mostly things that can be defined outside of their (original
9227 : : declaration) context. */
9228 : 7190961 : gcc_checking_assert (TREE_CODE (decl) == TEMPLATE_DECL
9229 : : || VAR_P (decl)
9230 : : || TREE_CODE (decl) == FUNCTION_DECL
9231 : : || TREE_CODE (decl) == TYPE_DECL
9232 : : || TREE_CODE (decl) == USING_DECL
9233 : : || TREE_CODE (decl) == CONCEPT_DECL
9234 : : || TREE_CODE (decl) == NAMESPACE_DECL);
9235 : :
9236 : 7190961 : int use_tpl = -1;
9237 : 7190961 : tree ti = node_template_info (decl, use_tpl);
9238 : 7190961 : tree tpl = NULL_TREE;
9239 : :
9240 : : /* If this is the TEMPLATE_DECL_RESULT of a TEMPLATE_DECL, get the
9241 : : TEMPLATE_DECL. Note TI_TEMPLATE is not a TEMPLATE_DECL for
9242 : : (some) friends, so we need to check that. */
9243 : : // FIXME: Should local friend template specializations be by value?
9244 : : // They don't get idents so we'll never know they're imported, but I
9245 : : // think we can only reach them from the TU that defines the
9246 : : // befriending class?
9247 : 2730018 : if (ti && TREE_CODE (TI_TEMPLATE (ti)) == TEMPLATE_DECL
9248 : 9920948 : && DECL_TEMPLATE_RESULT (TI_TEMPLATE (ti)) == decl)
9249 : : {
9250 : : tpl = TI_TEMPLATE (ti);
9251 : 705853 : partial_template:
9252 : 705853 : if (streaming_p ())
9253 : : {
9254 : 2011 : i (tt_template);
9255 : 2011 : dump (dumper::TREE)
9256 : 9 : && dump ("Writing implicit template %C:%N%S",
9257 : 9 : TREE_CODE (tpl), tpl, tpl);
9258 : : }
9259 : 705853 : tree_node (tpl);
9260 : :
9261 : : /* Streaming TPL caused us to visit DECL and maybe its type,
9262 : : if it wasn't TU-local. */
9263 : 705853 : if (CHECKING_P && !has_tu_local_dep (tpl))
9264 : : {
9265 : 705826 : gcc_checking_assert (TREE_VISITED (decl));
9266 : 705826 : if (DECL_IMPLICIT_TYPEDEF_P (decl))
9267 : 367516 : gcc_checking_assert (TREE_VISITED (TREE_TYPE (decl)));
9268 : : }
9269 : : return false;
9270 : : }
9271 : :
9272 : 6567642 : tree ctx = CP_DECL_CONTEXT (decl);
9273 : 6567642 : depset *dep = NULL;
9274 : 6567642 : if (streaming_p ())
9275 : 1112314 : dep = dep_hash->find_dependency (decl);
9276 : 5455328 : else if (TREE_CODE (ctx) != FUNCTION_DECL
9277 : 191571 : || TREE_CODE (decl) == TEMPLATE_DECL
9278 : 173836 : || DECL_IMPLICIT_TYPEDEF_P (decl)
9279 : 5607879 : || (DECL_LANG_SPECIFIC (decl)
9280 : 108993 : && DECL_MODULE_IMPORT_P (decl)))
9281 : : {
9282 : 5302777 : auto kind = (TREE_CODE (decl) == NAMESPACE_DECL
9283 : 466372 : && !DECL_NAMESPACE_ALIAS (decl)
9284 : 5302777 : ? depset::EK_NAMESPACE : depset::EK_DECL);
9285 : 5302777 : dep = dep_hash->add_dependency (decl, kind);
9286 : : }
9287 : :
9288 : 12687641 : if (!dep || dep->is_tu_local ())
9289 : : {
9290 : : /* Some internal entity of context. Do by value. */
9291 : 295337 : decl_value (decl, dep);
9292 : 295337 : return false;
9293 : : }
9294 : :
9295 : 6272305 : if (dep->get_entity_kind () == depset::EK_REDIRECT)
9296 : : {
9297 : : /* The DECL_TEMPLATE_RESULT of a partial specialization.
9298 : : Write the partial specialization's template. */
9299 : 82534 : depset *redirect = dep->deps[0];
9300 : 82534 : gcc_checking_assert (redirect->get_entity_kind () == depset::EK_PARTIAL);
9301 : 82534 : tpl = redirect->get_entity ();
9302 : 82534 : goto partial_template;
9303 : : }
9304 : :
9305 : 6189771 : if (streaming_p ())
9306 : : {
9307 : : /* Locate the entity. */
9308 : 969773 : unsigned index = dep->cluster;
9309 : 969773 : unsigned import = 0;
9310 : :
9311 : 969773 : if (dep->is_import ())
9312 : 687 : import = dep->section;
9313 : 969086 : else if (CHECKING_P)
9314 : : /* It should be what we put there. */
9315 : 969086 : gcc_checking_assert (index == ~import_entity_index (decl));
9316 : :
9317 : : #if CHECKING_P
9318 : 687 : gcc_assert (!import || importedness >= 0);
9319 : : #endif
9320 : 969773 : i (tt_entity);
9321 : 969773 : u (import);
9322 : 969773 : u (index);
9323 : : }
9324 : :
9325 : 6189771 : int tag = insert (decl);
9326 : 6189771 : if (streaming_p () && dump (dumper::TREE))
9327 : : {
9328 : 513 : char const *kind = "import";
9329 : 513 : module_state *from = (*modules)[0];
9330 : 513 : if (dep->is_import ())
9331 : : /* Rediscover the unremapped index. */
9332 : 78 : from = import_entity_module (import_entity_index (decl));
9333 : : else
9334 : : {
9335 : 435 : tree o = get_originating_module_decl (decl);
9336 : 435 : o = STRIP_TEMPLATE (o);
9337 : 870 : kind = (DECL_LANG_SPECIFIC (o) && DECL_MODULE_PURVIEW_P (o)
9338 : 435 : ? "purview" : "GMF");
9339 : : }
9340 : 513 : dump ("Wrote %s:%d %C:%N@%M", kind,
9341 : 513 : tag, TREE_CODE (decl), decl, from);
9342 : : }
9343 : :
9344 : 6189771 : add_indirects (decl);
9345 : :
9346 : 6189771 : return false;
9347 : : }
9348 : :
9349 : : void
9350 : 7539383 : trees_out::type_node (tree type)
9351 : : {
9352 : 7539383 : gcc_assert (TYPE_P (type));
9353 : :
9354 : 7539383 : tree root = (TYPE_NAME (type)
9355 : 7539383 : ? TREE_TYPE (TYPE_NAME (type)) : TYPE_MAIN_VARIANT (type));
9356 : :
9357 : 7539383 : if (type != root)
9358 : : {
9359 : 1695791 : if (streaming_p ())
9360 : 356124 : i (tt_variant_type);
9361 : 1695791 : tree_node (root);
9362 : :
9363 : 1695791 : int flags = -1;
9364 : :
9365 : 1695791 : if (TREE_CODE (type) == FUNCTION_TYPE
9366 : 1695791 : || TREE_CODE (type) == METHOD_TYPE)
9367 : : {
9368 : 402437 : int quals = type_memfn_quals (type);
9369 : 402437 : int rquals = type_memfn_rqual (type);
9370 : 402437 : tree raises = TYPE_RAISES_EXCEPTIONS (type);
9371 : 402437 : bool late = TYPE_HAS_LATE_RETURN_TYPE (type);
9372 : :
9373 : 402437 : if (raises != TYPE_RAISES_EXCEPTIONS (root)
9374 : 11997 : || rquals != type_memfn_rqual (root)
9375 : 8011 : || quals != type_memfn_quals (root)
9376 : 410322 : || late != TYPE_HAS_LATE_RETURN_TYPE (root))
9377 : 402437 : flags = rquals | (int (late) << 2) | (quals << 3);
9378 : : }
9379 : : else
9380 : : {
9381 : 1293354 : if (TYPE_USER_ALIGN (type))
9382 : 11382 : flags = TYPE_ALIGN_RAW (type);
9383 : : }
9384 : :
9385 : 1695791 : if (streaming_p ())
9386 : 356124 : i (flags);
9387 : :
9388 : 1695791 : if (flags < 0)
9389 : : ;
9390 : 413819 : else if (TREE_CODE (type) == FUNCTION_TYPE
9391 : 413819 : || TREE_CODE (type) == METHOD_TYPE)
9392 : : {
9393 : 402437 : tree raises = TYPE_RAISES_EXCEPTIONS (type);
9394 : 402437 : if (raises == TYPE_RAISES_EXCEPTIONS (root))
9395 : 11997 : raises = error_mark_node;
9396 : 402437 : tree_node (raises);
9397 : : }
9398 : :
9399 : : /* build_type_attribute_variant creates a new TYPE_MAIN_VARIANT, so
9400 : : variants should all have the same set of attributes. */
9401 : 1695791 : gcc_checking_assert (TYPE_ATTRIBUTES (type)
9402 : : == TYPE_ATTRIBUTES (TYPE_MAIN_VARIANT (type)));
9403 : :
9404 : 1695791 : if (streaming_p ())
9405 : : {
9406 : : /* Qualifiers. */
9407 : 356124 : int rquals = cp_type_quals (root);
9408 : 356124 : int quals = cp_type_quals (type);
9409 : 356124 : if (quals == rquals)
9410 : 165393 : quals = -1;
9411 : 356124 : i (quals);
9412 : : }
9413 : :
9414 : 1695791 : if (ref_node (type) != WK_none)
9415 : : {
9416 : 1695791 : int tag = insert (type);
9417 : 1695791 : if (streaming_p ())
9418 : : {
9419 : 356124 : i (0);
9420 : 356124 : dump (dumper::TREE)
9421 : 183 : && dump ("Wrote:%d variant type %C", tag, TREE_CODE (type));
9422 : : }
9423 : : }
9424 : 1695791 : return;
9425 : : }
9426 : :
9427 : 5843592 : if (tree name = TYPE_NAME (type))
9428 : 2319991 : if ((TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name))
9429 : 1809484 : || DECL_TEMPLATE_PARM_P (name)
9430 : 1217983 : || TREE_CODE (type) == RECORD_TYPE
9431 : 214569 : || TREE_CODE (type) == UNION_TYPE
9432 : 2529036 : || TREE_CODE (type) == ENUMERAL_TYPE)
9433 : : {
9434 : : /* We can meet template parms that we didn't meet in the
9435 : : tpl_parms walk, because we're referring to a derived type
9436 : : that was previously constructed from equivalent template
9437 : : parms. */
9438 : 2166787 : if (streaming_p ())
9439 : : {
9440 : 149724 : i (tt_typedef_type);
9441 : 149724 : dump (dumper::TREE)
9442 : 57 : && dump ("Writing %stypedef %C:%N",
9443 : 57 : DECL_IMPLICIT_TYPEDEF_P (name) ? "implicit " : "",
9444 : 57 : TREE_CODE (name), name);
9445 : : }
9446 : 2166787 : tree_node (name);
9447 : 2166787 : if (streaming_p ())
9448 : 149724 : dump (dumper::TREE) && dump ("Wrote typedef %C:%N%S",
9449 : 57 : TREE_CODE (name), name, name);
9450 : :
9451 : : /* We'll have either visited this type or have newly discovered
9452 : : that it's TU-local; either way we won't need to visit it again. */
9453 : 2166787 : gcc_checking_assert (TREE_VISITED (type) || has_tu_local_dep (name));
9454 : 2166787 : return;
9455 : : }
9456 : :
9457 : 3676805 : if (TYPE_PTRMEMFUNC_P (type))
9458 : : {
9459 : : /* This is a distinct type node, masquerading as a structure. */
9460 : 6579 : tree fn_type = TYPE_PTRMEMFUNC_FN_TYPE (type);
9461 : 6579 : if (streaming_p ())
9462 : 1997 : i (tt_ptrmem_type);
9463 : 6579 : tree_node (fn_type);
9464 : 6579 : int tag = insert (type);
9465 : 6579 : if (streaming_p ())
9466 : 2000 : dump (dumper::TREE) && dump ("Written:%d ptrmem type", tag);
9467 : 6579 : return;
9468 : : }
9469 : :
9470 : 3670226 : if (streaming_p ())
9471 : : {
9472 : 1172750 : u (tt_derived_type);
9473 : 1172750 : u (TREE_CODE (type));
9474 : : }
9475 : :
9476 : 3670226 : tree_node (TREE_TYPE (type));
9477 : 3670226 : switch (TREE_CODE (type))
9478 : : {
9479 : 0 : default:
9480 : : /* We should never meet a type here that is indescribable in
9481 : : terms of other types. */
9482 : 0 : gcc_unreachable ();
9483 : :
9484 : 58543 : case ARRAY_TYPE:
9485 : 58543 : tree_node (TYPE_DOMAIN (type));
9486 : 58543 : if (streaming_p ())
9487 : : /* Dependent arrays are constructed with TYPE_DEPENENT_P
9488 : : already set. */
9489 : 18781 : u (TYPE_DEPENDENT_P (type));
9490 : : break;
9491 : :
9492 : : case COMPLEX_TYPE:
9493 : : /* No additional data. */
9494 : : break;
9495 : :
9496 : 12 : case BOOLEAN_TYPE:
9497 : : /* A non-standard boolean type. */
9498 : 12 : if (streaming_p ())
9499 : 6 : u (TYPE_PRECISION (type));
9500 : : break;
9501 : :
9502 : 53844 : case INTEGER_TYPE:
9503 : 53844 : if (TREE_TYPE (type))
9504 : : {
9505 : : /* A range type (representing an array domain). */
9506 : 51284 : tree_node (TYPE_MIN_VALUE (type));
9507 : 51284 : tree_node (TYPE_MAX_VALUE (type));
9508 : : }
9509 : : else
9510 : : {
9511 : : /* A new integral type (representing a bitfield). */
9512 : 2560 : if (streaming_p ())
9513 : : {
9514 : 609 : unsigned prec = TYPE_PRECISION (type);
9515 : 609 : bool unsigned_p = TYPE_UNSIGNED (type);
9516 : :
9517 : 609 : u ((prec << 1) | unsigned_p);
9518 : : }
9519 : : }
9520 : : break;
9521 : :
9522 : 812759 : case METHOD_TYPE:
9523 : 812759 : case FUNCTION_TYPE:
9524 : 812759 : {
9525 : 812759 : gcc_checking_assert (type_memfn_rqual (type) == REF_QUAL_NONE);
9526 : :
9527 : 812759 : tree arg_types = TYPE_ARG_TYPES (type);
9528 : 812759 : if (TREE_CODE (type) == METHOD_TYPE)
9529 : : {
9530 : 502308 : tree_node (TREE_TYPE (TREE_VALUE (arg_types)));
9531 : 502308 : arg_types = TREE_CHAIN (arg_types);
9532 : : }
9533 : 812759 : tree_node (arg_types);
9534 : : }
9535 : 812759 : break;
9536 : :
9537 : 1282 : case OFFSET_TYPE:
9538 : 1282 : tree_node (TYPE_OFFSET_BASETYPE (type));
9539 : 1282 : break;
9540 : :
9541 : : case POINTER_TYPE:
9542 : : /* No additional data. */
9543 : : break;
9544 : :
9545 : 622795 : case REFERENCE_TYPE:
9546 : 622795 : if (streaming_p ())
9547 : 137998 : u (TYPE_REF_IS_RVALUE (type));
9548 : : break;
9549 : :
9550 : 707320 : case DECLTYPE_TYPE:
9551 : 707320 : case TYPEOF_TYPE:
9552 : 707320 : case DEPENDENT_OPERATOR_TYPE:
9553 : 707320 : tree_node (TYPE_VALUES_RAW (type));
9554 : 707320 : if (TREE_CODE (type) == DECLTYPE_TYPE)
9555 : : /* We stash a whole bunch of things into decltype's
9556 : : flags. */
9557 : 51602 : if (streaming_p ())
9558 : 17718 : tree_node_bools (type);
9559 : : break;
9560 : :
9561 : 7815 : case TRAIT_TYPE:
9562 : 7815 : tree_node (TRAIT_TYPE_KIND_RAW (type));
9563 : 7815 : tree_node (TRAIT_TYPE_TYPE1 (type));
9564 : 7815 : tree_node (TRAIT_TYPE_TYPE2 (type));
9565 : 7815 : break;
9566 : :
9567 : : case TYPE_ARGUMENT_PACK:
9568 : : /* No additional data. */
9569 : : break;
9570 : :
9571 : 128503 : case TYPE_PACK_EXPANSION:
9572 : 128503 : if (streaming_p ())
9573 : 52150 : u (PACK_EXPANSION_LOCAL_P (type));
9574 : 257006 : tree_node (PACK_EXPANSION_PARAMETER_PACKS (type));
9575 : 128503 : tree_node (PACK_EXPANSION_EXTRA_ARGS (type));
9576 : 128503 : break;
9577 : :
9578 : 2 : case PACK_INDEX_TYPE:
9579 : 2 : tree_node (PACK_INDEX_PACK (type));
9580 : 2 : tree_node (PACK_INDEX_INDEX (type));
9581 : 2 : break;
9582 : :
9583 : 155218 : case TYPENAME_TYPE:
9584 : 155218 : {
9585 : 155218 : tree_node (TYPE_CONTEXT (type));
9586 : 155218 : tree_node (DECL_NAME (TYPE_NAME (type)));
9587 : 155218 : tree_node (TYPENAME_TYPE_FULLNAME (type));
9588 : 155218 : if (streaming_p ())
9589 : : {
9590 : 55545 : enum tag_types tag_type = none_type;
9591 : 55545 : if (TYPENAME_IS_ENUM_P (type))
9592 : : tag_type = enum_type;
9593 : 55545 : else if (TYPENAME_IS_CLASS_P (type))
9594 : 0 : tag_type = class_type;
9595 : 55545 : u (int (tag_type));
9596 : : }
9597 : : }
9598 : : break;
9599 : :
9600 : 246 : case UNBOUND_CLASS_TEMPLATE:
9601 : 246 : {
9602 : 246 : tree decl = TYPE_NAME (type);
9603 : 246 : tree_node (DECL_CONTEXT (decl));
9604 : 246 : tree_node (DECL_NAME (decl));
9605 : 246 : tree_node (DECL_TEMPLATE_PARMS (decl));
9606 : : }
9607 : 246 : break;
9608 : :
9609 : 42 : case VECTOR_TYPE:
9610 : 42 : if (streaming_p ())
9611 : : {
9612 : 21 : poly_uint64 nunits = TYPE_VECTOR_SUBPARTS (type);
9613 : 42 : for (unsigned ix = 0; ix != NUM_POLY_INT_COEFFS; ix++)
9614 : 21 : wu (nunits.coeffs[ix]);
9615 : : }
9616 : : break;
9617 : : }
9618 : :
9619 : 3670226 : tree_node (TYPE_ATTRIBUTES (type));
9620 : :
9621 : : /* We may have met the type during emitting the above. */
9622 : 3670226 : if (ref_node (type) != WK_none)
9623 : : {
9624 : 3342367 : int tag = insert (type);
9625 : 3342367 : if (streaming_p ())
9626 : : {
9627 : 1018409 : i (0);
9628 : 1018409 : dump (dumper::TREE)
9629 : 525 : && dump ("Wrote:%d derived type %C", tag, TREE_CODE (type));
9630 : : }
9631 : : }
9632 : :
9633 : : return;
9634 : : }
9635 : :
9636 : : /* T is (mostly*) a non-mergeable node that must be written by value.
9637 : : The mergeable case is a BINFO, which are as-if DECLSs. */
9638 : :
9639 : : void
9640 : 22312066 : trees_out::tree_value (tree t)
9641 : : {
9642 : : /* We should never be writing a type by value. tree_type should
9643 : : have streamed it, or we're going via its TYPE_DECL. */
9644 : 22312066 : gcc_checking_assert (!TYPE_P (t));
9645 : :
9646 : 22312066 : if (DECL_P (t))
9647 : : /* No template, type, var or function, except anonymous
9648 : : non-context vars. */
9649 : 578671 : gcc_checking_assert ((TREE_CODE (t) != TEMPLATE_DECL
9650 : : && TREE_CODE (t) != TYPE_DECL
9651 : : && (TREE_CODE (t) != VAR_DECL
9652 : : || (!DECL_NAME (t) && !DECL_CONTEXT (t)))
9653 : : && TREE_CODE (t) != FUNCTION_DECL));
9654 : :
9655 : 22312066 : if (streaming_p ())
9656 : : {
9657 : : /* A new node -> tt_node. */
9658 : 8876557 : tree_val_count++;
9659 : 8876557 : i (tt_node);
9660 : 8876557 : start (t);
9661 : 8876557 : tree_node_bools (t);
9662 : : }
9663 : :
9664 : 22312066 : if (TREE_CODE (t) == TREE_BINFO)
9665 : : /* Binfos are decl-like and need merging information. */
9666 : 196118 : binfo_mergeable (t);
9667 : :
9668 : 22312066 : int tag = insert (t, WK_value);
9669 : 22312066 : if (streaming_p ())
9670 : 8876557 : dump (dumper::TREE)
9671 : 2457 : && dump ("Writing tree:%d %C:%N", tag, TREE_CODE (t), t);
9672 : :
9673 : 22312066 : tree_node_vals (t);
9674 : :
9675 : 22312066 : if (streaming_p ())
9676 : 8879014 : dump (dumper::TREE) && dump ("Written tree:%d %C:%N", tag, TREE_CODE (t), t);
9677 : 22312066 : }
9678 : :
9679 : : tree
9680 : 7589953 : trees_in::tree_value ()
9681 : : {
9682 : 7589953 : tree t = start ();
9683 : 7589953 : if (!t || !tree_node_bools (t))
9684 : 0 : return NULL_TREE;
9685 : :
9686 : 7589953 : tree existing = t;
9687 : 7589953 : if (TREE_CODE (t) == TREE_BINFO)
9688 : : {
9689 : 70344 : tree type;
9690 : 70344 : unsigned ix = binfo_mergeable (&type);
9691 : 70344 : if (TYPE_BINFO (type))
9692 : : {
9693 : : /* We already have a definition, this must be a duplicate. */
9694 : 40264 : dump (dumper::MERGE)
9695 : 229 : && dump ("Deduping binfo %N[%u]", type, ix);
9696 : 40264 : existing = TYPE_BINFO (type);
9697 : 55364 : while (existing && ix--)
9698 : 15100 : existing = TREE_CHAIN (existing);
9699 : 40264 : if (existing)
9700 : 40264 : register_duplicate (t, existing);
9701 : : else
9702 : : /* Error, mismatch -- diagnose in read_class_def's
9703 : : checking. */
9704 : : existing = t;
9705 : : }
9706 : : }
9707 : :
9708 : : /* Insert into map. */
9709 : 7589953 : int tag = insert (existing);
9710 : 7589953 : dump (dumper::TREE)
9711 : 3351 : && dump ("Reading tree:%d %C", tag, TREE_CODE (t));
9712 : :
9713 : 7589953 : if (!tree_node_vals (t))
9714 : : {
9715 : 0 : back_refs[~tag] = NULL_TREE;
9716 : 0 : set_overrun ();
9717 : : /* Bail. */
9718 : 0 : return NULL_TREE;
9719 : : }
9720 : :
9721 : 7589953 : if (TREE_CODE (t) == LAMBDA_EXPR
9722 : 7589953 : && CLASSTYPE_LAMBDA_EXPR (TREE_TYPE (t)))
9723 : : {
9724 : 1571 : existing = CLASSTYPE_LAMBDA_EXPR (TREE_TYPE (t));
9725 : 1571 : back_refs[~tag] = existing;
9726 : : }
9727 : :
9728 : 7593304 : dump (dumper::TREE) && dump ("Read tree:%d %C:%N", tag, TREE_CODE (t), t);
9729 : :
9730 : 7589953 : if (TREE_CODE (existing) == INTEGER_CST && !TREE_OVERFLOW (existing))
9731 : : {
9732 : 425234 : existing = cache_integer_cst (t, true);
9733 : 425234 : back_refs[~tag] = existing;
9734 : : }
9735 : :
9736 : : return existing;
9737 : : }
9738 : :
9739 : : /* Whether DECL has a TU-local dependency in the hash. */
9740 : :
9741 : : bool
9742 : 706187 : trees_out::has_tu_local_dep (tree decl) const
9743 : : {
9744 : : /* Only the contexts of fields or enums remember that they're
9745 : : TU-local. */
9746 : 706187 : if (DECL_CONTEXT (decl)
9747 : 706187 : && (TREE_CODE (decl) == FIELD_DECL
9748 : 706184 : || TREE_CODE (decl) == CONST_DECL))
9749 : 3 : decl = TYPE_NAME (DECL_CONTEXT (decl));
9750 : :
9751 : 706187 : depset *dep = dep_hash->find_dependency (decl);
9752 : 706187 : if (!dep)
9753 : : {
9754 : : /* This might be the DECL_TEMPLATE_RESULT of a TEMPLATE_DECL
9755 : : which we found was TU-local and gave up early. */
9756 : 8204 : int use_tpl = -1;
9757 : 8204 : if (tree ti = node_template_info (decl, use_tpl))
9758 : 1697 : dep = dep_hash->find_dependency (TI_TEMPLATE (ti));
9759 : : }
9760 : :
9761 : 706202 : return dep && dep->is_tu_local ();
9762 : : }
9763 : :
9764 : : /* If T depends on a TU-local entity, return that decl. */
9765 : :
9766 : : tree
9767 : 366 : trees_out::find_tu_local_decl (tree t)
9768 : : {
9769 : : /* We need to have walked all deps first before we can check. */
9770 : 366 : gcc_checking_assert (!is_initial_scan ());
9771 : :
9772 : 894 : auto walker = [](tree *tp, int *walk_subtrees, void *data) -> tree
9773 : : {
9774 : 528 : auto self = (trees_out *)data;
9775 : :
9776 : 528 : tree decl = NULL_TREE;
9777 : 528 : if (TYPE_P (*tp))
9778 : : {
9779 : : /* A PMF type is a record type, which we otherwise wouldn't walk;
9780 : : return whether the function type is TU-local. */
9781 : 354 : if (TYPE_PTRMEMFUNC_P (*tp))
9782 : : {
9783 : 3 : *walk_subtrees = 0;
9784 : 3 : return self->find_tu_local_decl (TYPE_PTRMEMFUNC_FN_TYPE (*tp));
9785 : : }
9786 : : else
9787 : 351 : decl = TYPE_MAIN_DECL (*tp);
9788 : : }
9789 : 174 : else if (DECL_P (*tp))
9790 : : decl = *tp;
9791 : :
9792 : 357 : if (decl)
9793 : : {
9794 : : /* We found a DECL, this will tell us whether we're TU-local. */
9795 : 57 : *walk_subtrees = 0;
9796 : 57 : return self->has_tu_local_dep (decl) ? decl : NULL_TREE;
9797 : : }
9798 : : return NULL_TREE;
9799 : : };
9800 : :
9801 : : /* We need to walk without duplicates so that we step into the pointed-to
9802 : : types of array types. */
9803 : 366 : return cp_walk_tree_without_duplicates (&t, walker, this);
9804 : : }
9805 : :
9806 : : /* Stream out tree node T. We automatically create local back
9807 : : references, which is essentially a single pass lisp
9808 : : self-referential structure pretty-printer. */
9809 : :
9810 : : void
9811 : 199839088 : trees_out::tree_node (tree t)
9812 : : {
9813 : 199839088 : dump.indent ();
9814 : 199839088 : walk_kind ref = ref_node (t);
9815 : 199839088 : if (ref == WK_none)
9816 : 154186806 : goto done;
9817 : :
9818 : : /* Find TU-local entities and intercept streaming to instead write a
9819 : : placeholder value; this way we don't need to emit such decls.
9820 : : We only need to do this when writing a definition of an entity
9821 : : that we know names a TU-local entity. */
9822 : 53625214 : if (!is_initial_scan () && writing_local_entities)
9823 : : {
9824 : 846 : tree local_decl = NULL_TREE;
9825 : 846 : if (DECL_P (t) && has_tu_local_dep (t))
9826 : : local_decl = t;
9827 : : /* Consider a type to be TU-local if it refers to any TU-local decl,
9828 : : no matter how deep.
9829 : :
9830 : : This worsens diagnostics slightly, as we often no longer point
9831 : : directly to the at-fault entity when instantiating. However, this
9832 : : reduces the module size slightly and means that much less of pt.cc
9833 : : needs to know about us. */
9834 : 750 : else if (TYPE_P (t))
9835 : 141 : local_decl = find_tu_local_decl (t);
9836 : 609 : else if (EXPR_P (t))
9837 : 222 : local_decl = find_tu_local_decl (TREE_TYPE (t));
9838 : :
9839 : 459 : if (local_decl)
9840 : : {
9841 : 150 : int tag = insert (t, WK_value);
9842 : 150 : if (streaming_p ())
9843 : : {
9844 : 150 : tu_local_count++;
9845 : 150 : i (tt_tu_local);
9846 : 150 : dump (dumper::TREE)
9847 : 0 : && dump ("Writing TU-local entity:%d %C:%N",
9848 : 0 : tag, TREE_CODE (t), t);
9849 : : }
9850 : : /* TODO: Get a more descriptive name? */
9851 : 150 : tree_node (DECL_NAME (local_decl));
9852 : 150 : if (state)
9853 : 150 : state->write_location (*this, DECL_SOURCE_LOCATION (local_decl));
9854 : 150 : goto done;
9855 : : }
9856 : : }
9857 : :
9858 : 45652132 : if (ref != WK_normal)
9859 : 1176522 : goto skip_normal;
9860 : :
9861 : 44475610 : if (TREE_CODE (t) == IDENTIFIER_NODE)
9862 : : {
9863 : : /* An identifier node -> tt_id, tt_conv_id, tt_anon_id, tt_lambda_id. */
9864 : 5604772 : int code = tt_id;
9865 : 5604772 : if (IDENTIFIER_ANON_P (t))
9866 : 20121 : code = IDENTIFIER_LAMBDA_P (t) ? tt_lambda_id : tt_anon_id;
9867 : 5584651 : else if (IDENTIFIER_CONV_OP_P (t))
9868 : 8087 : code = tt_conv_id;
9869 : :
9870 : 5604772 : if (streaming_p ())
9871 : 1149144 : i (code);
9872 : :
9873 : 5604772 : if (code == tt_conv_id)
9874 : : {
9875 : 8087 : tree type = TREE_TYPE (t);
9876 : 8087 : gcc_checking_assert (type || t == conv_op_identifier);
9877 : 8087 : tree_node (type);
9878 : : }
9879 : 5596685 : else if (code == tt_id && streaming_p ())
9880 : 1140005 : str (IDENTIFIER_POINTER (t), IDENTIFIER_LENGTH (t));
9881 : :
9882 : 5604772 : int tag = insert (t);
9883 : 5604772 : if (streaming_p ())
9884 : : {
9885 : : /* We know the ordering of the 4 id tags. */
9886 : 1149144 : static const char *const kinds[] =
9887 : : {"", "conv_op ", "anon ", "lambda "};
9888 : 1149144 : dump (dumper::TREE)
9889 : 1008 : && dump ("Written:%d %sidentifier:%N", tag,
9890 : 1005 : kinds[code - tt_id],
9891 : 3 : code == tt_conv_id ? TREE_TYPE (t) : t);
9892 : : }
9893 : 5604772 : goto done;
9894 : : }
9895 : :
9896 : 38870838 : if (TREE_CODE (t) == TREE_BINFO)
9897 : : {
9898 : : /* A BINFO -> tt_binfo.
9899 : : We must do this by reference. We stream the binfo tree
9900 : : itself when streaming its owning RECORD_TYPE. That we got
9901 : : here means the dominating type is not in this SCC. */
9902 : 44157 : if (streaming_p ())
9903 : 1119 : i (tt_binfo);
9904 : 44157 : binfo_mergeable (t);
9905 : 44157 : gcc_checking_assert (!TREE_VISITED (t));
9906 : 44157 : int tag = insert (t);
9907 : 44157 : if (streaming_p ())
9908 : 1119 : dump (dumper::TREE) && dump ("Inserting binfo:%d %N", tag, t);
9909 : 44157 : goto done;
9910 : : }
9911 : :
9912 : 38826681 : if (TREE_CODE (t) == INTEGER_CST
9913 : 2887731 : && !TREE_OVERFLOW (t)
9914 : 41714412 : && TREE_CODE (TREE_TYPE (t)) == ENUMERAL_TYPE)
9915 : : {
9916 : : /* An integral constant of enumeral type. See if it matches one
9917 : : of the enumeration values. */
9918 : 16937 : for (tree values = TYPE_VALUES (TREE_TYPE (t));
9919 : 165879 : values; values = TREE_CHAIN (values))
9920 : : {
9921 : 164691 : tree decl = TREE_VALUE (values);
9922 : 164691 : if (tree_int_cst_equal (DECL_INITIAL (decl), t))
9923 : : {
9924 : 15749 : if (streaming_p ())
9925 : 4669 : u (tt_enum_value);
9926 : 15749 : tree_node (decl);
9927 : 15791 : dump (dumper::TREE) && dump ("Written enum value %N", decl);
9928 : 15749 : goto done;
9929 : : }
9930 : : }
9931 : : /* It didn't match. We'll write it a an explicit INTEGER_CST
9932 : : node. */
9933 : : }
9934 : :
9935 : 38810932 : if (TYPE_P (t))
9936 : : {
9937 : 7539383 : type_node (t);
9938 : 7539383 : goto done;
9939 : : }
9940 : :
9941 : 31271549 : if (DECL_P (t))
9942 : : {
9943 : 9758153 : if (DECL_TEMPLATE_PARM_P (t))
9944 : : {
9945 : 1590201 : tpl_parm_value (t);
9946 : 1590201 : goto done;
9947 : : }
9948 : :
9949 : 8167952 : if (!DECL_CONTEXT (t))
9950 : : {
9951 : : /* There are a few cases of decls with no context. We'll write
9952 : : these by value, but first assert they are cases we expect. */
9953 : 17972 : gcc_checking_assert (ref == WK_normal);
9954 : 17972 : switch (TREE_CODE (t))
9955 : : {
9956 : 0 : default: gcc_unreachable ();
9957 : :
9958 : 5786 : case LABEL_DECL:
9959 : : /* CASE_LABEL_EXPRs contain uncontexted LABEL_DECLs. */
9960 : 5786 : gcc_checking_assert (!DECL_NAME (t));
9961 : : break;
9962 : :
9963 : 520 : case VAR_DECL:
9964 : : /* AGGR_INIT_EXPRs cons up anonymous uncontexted VAR_DECLs. */
9965 : 520 : gcc_checking_assert (!DECL_NAME (t)
9966 : : && DECL_ARTIFICIAL (t));
9967 : : break;
9968 : :
9969 : : case PARM_DECL:
9970 : : /* REQUIRES_EXPRs have a tree list of uncontexted
9971 : : PARM_DECLS. It'd be nice if they had a
9972 : : distinguishing flag to double check. */
9973 : : break;
9974 : : }
9975 : 17972 : goto by_value;
9976 : : }
9977 : : }
9978 : :
9979 : 21513396 : skip_normal:
9980 : 30839898 : if (DECL_P (t) && !decl_node (t, ref))
9981 : 8545804 : goto done;
9982 : :
9983 : : /* Otherwise by value */
9984 : 22312066 : by_value:
9985 : 22312066 : tree_value (t);
9986 : :
9987 : 199839088 : done:
9988 : : /* And, breath out. */
9989 : 199839088 : dump.outdent ();
9990 : 199839088 : }
9991 : :
9992 : : /* Stream in a tree node. */
9993 : :
9994 : : tree
9995 : 64762912 : trees_in::tree_node (bool is_use)
9996 : : {
9997 : 64762912 : if (get_overrun ())
9998 : : return NULL_TREE;
9999 : :
10000 : 64762912 : dump.indent ();
10001 : 64762912 : int tag = i ();
10002 : 64762912 : tree res = NULL_TREE;
10003 : 64762912 : switch (tag)
10004 : : {
10005 : 21063173 : default:
10006 : : /* backref, pull it out of the map. */
10007 : 21063173 : res = back_ref (tag);
10008 : 21063173 : break;
10009 : :
10010 : : case tt_null:
10011 : : /* NULL_TREE. */
10012 : : break;
10013 : :
10014 : 150 : case tt_tu_local:
10015 : 150 : {
10016 : : /* A translation-unit-local entity. */
10017 : 150 : res = make_node (TU_LOCAL_ENTITY);
10018 : 150 : int tag = insert (res);
10019 : :
10020 : 150 : TU_LOCAL_ENTITY_NAME (res) = tree_node ();
10021 : 150 : TU_LOCAL_ENTITY_LOCATION (res) = state->read_location (*this);
10022 : 150 : dump (dumper::TREE) && dump ("Read TU-local entity:%d %N", tag, res);
10023 : : }
10024 : : break;
10025 : :
10026 : 4888870 : case tt_fixed:
10027 : : /* A fixed ref, find it in the fixed_ref array. */
10028 : 4888870 : {
10029 : 4888870 : unsigned fix = u ();
10030 : 4888870 : if (fix < (*fixed_trees).length ())
10031 : : {
10032 : 4888870 : res = (*fixed_trees)[fix];
10033 : 4888870 : dump (dumper::TREE) && dump ("Read fixed:%u %C:%N%S", fix,
10034 : 4851 : TREE_CODE (res), res, res);
10035 : : }
10036 : :
10037 : 4884019 : if (!res)
10038 : 0 : set_overrun ();
10039 : : }
10040 : : break;
10041 : :
10042 : 49693 : case tt_parm:
10043 : 49693 : {
10044 : 49693 : tree fn = tree_node ();
10045 : 49693 : if (fn && TREE_CODE (fn) == FUNCTION_DECL)
10046 : 49693 : res = tree_node ();
10047 : 49693 : if (res)
10048 : 49693 : dump (dumper::TREE)
10049 : 15 : && dump ("Read %s reference %N",
10050 : 15 : TREE_CODE (res) == PARM_DECL ? "parameter" : "result",
10051 : : res);
10052 : : }
10053 : : break;
10054 : :
10055 : 7589953 : case tt_node:
10056 : : /* A new node. Stream it in. */
10057 : 7589953 : res = tree_value ();
10058 : 7589953 : break;
10059 : :
10060 : 817691 : case tt_decl:
10061 : : /* A new decl. Stream it in. */
10062 : 817691 : res = decl_value ();
10063 : 817691 : break;
10064 : :
10065 : 287644 : case tt_tpl_parm:
10066 : : /* A template parameter. Stream it in. */
10067 : 287644 : res = tpl_parm_value ();
10068 : 287644 : break;
10069 : :
10070 : 896468 : case tt_id:
10071 : : /* An identifier node. */
10072 : 896468 : {
10073 : 896468 : size_t l;
10074 : 896468 : const char *chars = str (&l);
10075 : 896468 : res = get_identifier_with_length (chars, l);
10076 : 896468 : int tag = insert (res);
10077 : 896468 : dump (dumper::TREE)
10078 : 1428 : && dump ("Read identifier:%d %N", tag, res);
10079 : : }
10080 : 896468 : break;
10081 : :
10082 : 2281 : case tt_conv_id:
10083 : : /* A conversion operator. Get the type and recreate the
10084 : : identifier. */
10085 : 2281 : {
10086 : 2281 : tree type = tree_node ();
10087 : 2281 : if (!get_overrun ())
10088 : : {
10089 : 2281 : res = type ? make_conv_op_name (type) : conv_op_identifier;
10090 : 2281 : int tag = insert (res);
10091 : 2281 : dump (dumper::TREE)
10092 : 27 : && dump ("Created conv_op:%d %S for %N", tag, res, type);
10093 : : }
10094 : : }
10095 : : break;
10096 : :
10097 : 4731 : case tt_anon_id:
10098 : 4731 : case tt_lambda_id:
10099 : : /* An anonymous or lambda id. */
10100 : 4731 : {
10101 : 4731 : res = make_anon_name ();
10102 : 4731 : if (tag == tt_lambda_id)
10103 : 2358 : IDENTIFIER_LAMBDA_P (res) = true;
10104 : 4731 : int tag = insert (res);
10105 : 4734 : dump (dumper::TREE)
10106 : 3 : && dump ("Read %s identifier:%d %N",
10107 : 3 : IDENTIFIER_LAMBDA_P (res) ? "lambda" : "anon", tag, res);
10108 : : }
10109 : : break;
10110 : :
10111 : 125695 : case tt_typedef_type:
10112 : 125695 : res = tree_node ();
10113 : 125695 : if (res)
10114 : : {
10115 : 125695 : dump (dumper::TREE)
10116 : 72 : && dump ("Read %stypedef %C:%N",
10117 : 72 : DECL_IMPLICIT_TYPEDEF_P (res) ? "implicit " : "",
10118 : 72 : TREE_CODE (res), res);
10119 : 125695 : res = TREE_TYPE (res);
10120 : : }
10121 : : break;
10122 : :
10123 : 983283 : case tt_derived_type:
10124 : : /* A type derived from some other type. */
10125 : 983283 : {
10126 : 983283 : enum tree_code code = tree_code (u ());
10127 : 983283 : res = tree_node ();
10128 : :
10129 : 983283 : switch (code)
10130 : : {
10131 : 0 : default:
10132 : 0 : set_overrun ();
10133 : 0 : break;
10134 : :
10135 : 15262 : case ARRAY_TYPE:
10136 : 15262 : {
10137 : 15262 : tree domain = tree_node ();
10138 : 15262 : int dep = u ();
10139 : 15262 : if (!get_overrun ())
10140 : 15262 : res = build_cplus_array_type (res, domain, dep);
10141 : : }
10142 : : break;
10143 : :
10144 : 87 : case COMPLEX_TYPE:
10145 : 87 : if (!get_overrun ())
10146 : 87 : res = build_complex_type (res);
10147 : : break;
10148 : :
10149 : 9 : case BOOLEAN_TYPE:
10150 : 9 : {
10151 : 9 : unsigned precision = u ();
10152 : 9 : if (!get_overrun ())
10153 : 9 : res = build_nonstandard_boolean_type (precision);
10154 : : }
10155 : : break;
10156 : :
10157 : 13990 : case INTEGER_TYPE:
10158 : 13990 : if (res)
10159 : : {
10160 : : /* A range type (representing an array domain). */
10161 : 13463 : tree min = tree_node ();
10162 : 13463 : tree max = tree_node ();
10163 : :
10164 : 13463 : if (!get_overrun ())
10165 : 13463 : res = build_range_type (res, min, max);
10166 : : }
10167 : : else
10168 : : {
10169 : : /* A new integral type (representing a bitfield). */
10170 : 527 : unsigned enc = u ();
10171 : 527 : if (!get_overrun ())
10172 : 527 : res = build_nonstandard_integer_type (enc >> 1, enc & 1);
10173 : : }
10174 : : break;
10175 : :
10176 : 284739 : case FUNCTION_TYPE:
10177 : 284739 : case METHOD_TYPE:
10178 : 284739 : {
10179 : 284739 : tree klass = code == METHOD_TYPE ? tree_node () : NULL_TREE;
10180 : 284739 : tree args = tree_node ();
10181 : 284739 : if (!get_overrun ())
10182 : : {
10183 : 284739 : if (klass)
10184 : 174533 : res = build_method_type_directly (klass, res, args);
10185 : : else
10186 : 110206 : res = build_function_type (res, args);
10187 : : }
10188 : : }
10189 : : break;
10190 : :
10191 : 231 : case OFFSET_TYPE:
10192 : 231 : {
10193 : 231 : tree base = tree_node ();
10194 : 231 : if (!get_overrun ())
10195 : 231 : res = build_offset_type (base, res);
10196 : : }
10197 : : break;
10198 : :
10199 : 138099 : case POINTER_TYPE:
10200 : 138099 : if (!get_overrun ())
10201 : 138099 : res = build_pointer_type (res);
10202 : : break;
10203 : :
10204 : 113011 : case REFERENCE_TYPE:
10205 : 113011 : {
10206 : 113011 : bool rval = bool (u ());
10207 : 113011 : if (!get_overrun ())
10208 : 113011 : res = cp_build_reference_type (res, rval);
10209 : : }
10210 : : break;
10211 : :
10212 : 292251 : case DECLTYPE_TYPE:
10213 : 292251 : case TYPEOF_TYPE:
10214 : 292251 : case DEPENDENT_OPERATOR_TYPE:
10215 : 292251 : {
10216 : 292251 : tree expr = tree_node ();
10217 : 292251 : if (!get_overrun ())
10218 : : {
10219 : 292251 : res = cxx_make_type (code);
10220 : 292251 : TYPE_VALUES_RAW (res) = expr;
10221 : 292251 : if (code == DECLTYPE_TYPE)
10222 : 14132 : tree_node_bools (res);
10223 : 292251 : SET_TYPE_STRUCTURAL_EQUALITY (res);
10224 : : }
10225 : : }
10226 : : break;
10227 : :
10228 : 2045 : case TRAIT_TYPE:
10229 : 2045 : {
10230 : 2045 : tree kind = tree_node ();
10231 : 2045 : tree type1 = tree_node ();
10232 : 2045 : tree type2 = tree_node ();
10233 : 2045 : if (!get_overrun ())
10234 : : {
10235 : 2045 : res = cxx_make_type (TRAIT_TYPE);
10236 : 2045 : TRAIT_TYPE_KIND_RAW (res) = kind;
10237 : 2045 : TRAIT_TYPE_TYPE1 (res) = type1;
10238 : 2045 : TRAIT_TYPE_TYPE2 (res) = type2;
10239 : 2045 : SET_TYPE_STRUCTURAL_EQUALITY (res);
10240 : : }
10241 : : }
10242 : : break;
10243 : :
10244 : 39557 : case TYPE_ARGUMENT_PACK:
10245 : 39557 : if (!get_overrun ())
10246 : : {
10247 : 39557 : tree pack = cxx_make_type (TYPE_ARGUMENT_PACK);
10248 : 39557 : ARGUMENT_PACK_ARGS (pack) = res;
10249 : : res = pack;
10250 : : }
10251 : : break;
10252 : :
10253 : 39258 : case TYPE_PACK_EXPANSION:
10254 : 39258 : {
10255 : 39258 : bool local = u ();
10256 : 39258 : tree param_packs = tree_node ();
10257 : 39258 : tree extra_args = tree_node ();
10258 : 39258 : if (!get_overrun ())
10259 : : {
10260 : 39258 : tree expn = cxx_make_type (TYPE_PACK_EXPANSION);
10261 : 39258 : SET_TYPE_STRUCTURAL_EQUALITY (expn);
10262 : 39258 : PACK_EXPANSION_PATTERN (expn) = res;
10263 : 78516 : PACK_EXPANSION_PARAMETER_PACKS (expn) = param_packs;
10264 : 39258 : PACK_EXPANSION_EXTRA_ARGS (expn) = extra_args;
10265 : 39258 : PACK_EXPANSION_LOCAL_P (expn) = local;
10266 : 39258 : res = expn;
10267 : : }
10268 : : }
10269 : : break;
10270 : :
10271 : 1 : case PACK_INDEX_TYPE:
10272 : 1 : {
10273 : 1 : tree pack = tree_node ();
10274 : 1 : tree index = tree_node ();
10275 : 1 : if (!get_overrun ())
10276 : 1 : res = make_pack_index (pack, index);
10277 : : }
10278 : : break;
10279 : :
10280 : 44669 : case TYPENAME_TYPE:
10281 : 44669 : {
10282 : 44669 : tree ctx = tree_node ();
10283 : 44669 : tree name = tree_node ();
10284 : 44669 : tree fullname = tree_node ();
10285 : 44669 : enum tag_types tag_type = tag_types (u ());
10286 : :
10287 : 44669 : if (!get_overrun ())
10288 : 44669 : res = build_typename_type (ctx, name, fullname, tag_type);
10289 : : }
10290 : : break;
10291 : :
10292 : 44 : case UNBOUND_CLASS_TEMPLATE:
10293 : 44 : {
10294 : 44 : tree ctx = tree_node ();
10295 : 44 : tree name = tree_node ();
10296 : 44 : tree parms = tree_node ();
10297 : :
10298 : 44 : if (!get_overrun ())
10299 : 44 : res = make_unbound_class_template_raw (ctx, name, parms);
10300 : : }
10301 : : break;
10302 : :
10303 : : case VECTOR_TYPE:
10304 : : {
10305 : : poly_uint64 nunits;
10306 : 60 : for (unsigned ix = 0; ix != NUM_POLY_INT_COEFFS; ix++)
10307 : 30 : nunits.coeffs[ix] = wu ();
10308 : 30 : if (!get_overrun ())
10309 : 30 : res = build_vector_type (res, nunits);
10310 : : }
10311 : : break;
10312 : : }
10313 : :
10314 : : /* In the exporting TU, a derived type with attributes was built by
10315 : : build_type_attribute_variant as a distinct copy, with itself as
10316 : : TYPE_MAIN_VARIANT. We repeat that on import to get the version
10317 : : without attributes as TYPE_CANONICAL. */
10318 : 983283 : if (tree attribs = tree_node ())
10319 : 13523 : res = cp_build_type_attribute_variant (res, attribs);
10320 : :
10321 : 983283 : int tag = i ();
10322 : 983283 : if (!tag)
10323 : : {
10324 : 844225 : tag = insert (res);
10325 : 844225 : if (res)
10326 : 844225 : dump (dumper::TREE)
10327 : 654 : && dump ("Created:%d derived type %C", tag, code);
10328 : : }
10329 : : else
10330 : 139058 : res = back_ref (tag);
10331 : : }
10332 : : break;
10333 : :
10334 : 292475 : case tt_variant_type:
10335 : : /* Variant of some type. */
10336 : 292475 : {
10337 : 292475 : res = tree_node ();
10338 : 292475 : int flags = i ();
10339 : 292475 : if (get_overrun ())
10340 : : ;
10341 : 292475 : else if (flags < 0)
10342 : : /* No change. */;
10343 : 141609 : else if (TREE_CODE (res) == FUNCTION_TYPE
10344 : 141609 : || TREE_CODE (res) == METHOD_TYPE)
10345 : : {
10346 : 140437 : cp_ref_qualifier rqual = cp_ref_qualifier (flags & 3);
10347 : 140437 : bool late = (flags >> 2) & 1;
10348 : 140437 : cp_cv_quals quals = cp_cv_quals (flags >> 3);
10349 : :
10350 : 140437 : tree raises = tree_node ();
10351 : 140437 : if (raises == error_mark_node)
10352 : 4952 : raises = TYPE_RAISES_EXCEPTIONS (res);
10353 : :
10354 : 140437 : res = build_cp_fntype_variant (res, rqual, raises, late);
10355 : 140437 : if (TREE_CODE (res) == FUNCTION_TYPE)
10356 : 52868 : res = apply_memfn_quals (res, quals, rqual);
10357 : : }
10358 : : else
10359 : : {
10360 : 1172 : res = build_aligned_type (res, (1u << flags) >> 1);
10361 : 1172 : TYPE_USER_ALIGN (res) = true;
10362 : : }
10363 : :
10364 : 292475 : int quals = i ();
10365 : 292475 : if (quals >= 0 && !get_overrun ())
10366 : 151720 : res = cp_build_qualified_type (res, quals);
10367 : :
10368 : 292475 : int tag = i ();
10369 : 292475 : if (!tag)
10370 : : {
10371 : 292475 : tag = insert (res);
10372 : 292475 : if (res)
10373 : 292475 : dump (dumper::TREE)
10374 : 276 : && dump ("Created:%d variant type %C", tag, TREE_CODE (res));
10375 : : }
10376 : : else
10377 : 0 : res = back_ref (tag);
10378 : : }
10379 : : break;
10380 : :
10381 : 3764 : case tt_tinfo_var:
10382 : 3764 : case tt_tinfo_typedef:
10383 : : /* A tinfo var or typedef. */
10384 : 3764 : {
10385 : 3764 : bool is_var = tag == tt_tinfo_var;
10386 : 3764 : unsigned ix = u ();
10387 : 3764 : tree type = NULL_TREE;
10388 : :
10389 : 3764 : if (is_var)
10390 : : {
10391 : 2254 : tree name = tree_node ();
10392 : 2254 : type = tree_node ();
10393 : :
10394 : 2254 : if (!get_overrun ())
10395 : 2254 : res = get_tinfo_decl_direct (type, name, int (ix));
10396 : : }
10397 : : else
10398 : : {
10399 : 1510 : if (!get_overrun ())
10400 : : {
10401 : 1510 : type = get_pseudo_tinfo_type (ix);
10402 : 1510 : res = TYPE_NAME (type);
10403 : : }
10404 : : }
10405 : 3764 : if (res)
10406 : : {
10407 : 3764 : int tag = insert (res);
10408 : 3764 : dump (dumper::TREE)
10409 : 36 : && dump ("Created tinfo_%s:%d %S:%u for %N",
10410 : : is_var ? "var" : "decl", tag, res, ix, type);
10411 : 3764 : if (!is_var)
10412 : : {
10413 : 1510 : tag = insert (type);
10414 : 1510 : dump (dumper::TREE)
10415 : 12 : && dump ("Created tinfo_type:%d %u %N", tag, ix, type);
10416 : : }
10417 : : }
10418 : : }
10419 : : break;
10420 : :
10421 : 1097 : case tt_ptrmem_type:
10422 : : /* A pointer to member function. */
10423 : 1097 : {
10424 : 1097 : tree type = tree_node ();
10425 : 1097 : if (type && TREE_CODE (type) == POINTER_TYPE
10426 : 2194 : && TREE_CODE (TREE_TYPE (type)) == METHOD_TYPE)
10427 : : {
10428 : 1097 : res = build_ptrmemfunc_type (type);
10429 : 1097 : int tag = insert (res);
10430 : 1100 : dump (dumper::TREE) && dump ("Created:%d ptrmem type", tag);
10431 : : }
10432 : : else
10433 : 0 : set_overrun ();
10434 : : }
10435 : : break;
10436 : :
10437 : 3 : case tt_nttp_var:
10438 : : /* An NTTP object. */
10439 : 3 : {
10440 : 3 : tree init = tree_node ();
10441 : 3 : tree name = tree_node ();
10442 : 3 : if (!get_overrun ())
10443 : : {
10444 : : /* We don't want to check the initializer as that may require
10445 : : name lookup, which could recursively start lazy loading.
10446 : : Instead we know that INIT is already valid so we can just
10447 : : apply that directly. */
10448 : 3 : res = get_template_parm_object (init, name, /*check_init=*/false);
10449 : 3 : int tag = insert (res);
10450 : 3 : dump (dumper::TREE)
10451 : 0 : && dump ("Created nttp object:%d %N", tag, name);
10452 : : }
10453 : : }
10454 : : break;
10455 : :
10456 : 4068 : case tt_enum_value:
10457 : : /* An enum const value. */
10458 : 4068 : {
10459 : 4068 : if (tree decl = tree_node ())
10460 : : {
10461 : 4086 : dump (dumper::TREE) && dump ("Read enum value %N", decl);
10462 : 4068 : res = DECL_INITIAL (decl);
10463 : : }
10464 : :
10465 : 4068 : if (!res)
10466 : 0 : set_overrun ();
10467 : : }
10468 : : break;
10469 : :
10470 : 5352 : case tt_enum_decl:
10471 : : /* An enum decl. */
10472 : 5352 : {
10473 : 5352 : tree ctx = tree_node ();
10474 : 5352 : tree name = tree_node ();
10475 : :
10476 : 5352 : if (!get_overrun ()
10477 : 5352 : && TREE_CODE (ctx) == ENUMERAL_TYPE)
10478 : 5352 : res = find_enum_member (ctx, name);
10479 : :
10480 : 5352 : if (!res)
10481 : 0 : set_overrun ();
10482 : : else
10483 : : {
10484 : 5352 : int tag = insert (res);
10485 : 5352 : dump (dumper::TREE)
10486 : 18 : && dump ("Read enum decl:%d %C:%N", tag, TREE_CODE (res), res);
10487 : : }
10488 : : }
10489 : : break;
10490 : :
10491 : 5127 : case tt_data_member:
10492 : : /* A data member. */
10493 : 5127 : {
10494 : 5127 : tree ctx = tree_node ();
10495 : 5127 : tree name = tree_node ();
10496 : :
10497 : 5127 : if (!get_overrun ()
10498 : 5127 : && RECORD_OR_UNION_TYPE_P (ctx))
10499 : : {
10500 : 5127 : if (name)
10501 : 4410 : res = lookup_class_binding (ctx, name);
10502 : : else
10503 : 717 : res = lookup_field_ident (ctx, u ());
10504 : :
10505 : 5127 : if (!res
10506 : 5127 : || TREE_CODE (res) != FIELD_DECL
10507 : 10254 : || DECL_CONTEXT (res) != ctx)
10508 : : res = NULL_TREE;
10509 : : }
10510 : :
10511 : 5127 : if (!res)
10512 : 0 : set_overrun ();
10513 : : else
10514 : : {
10515 : 5127 : int tag = insert (res);
10516 : 5127 : dump (dumper::TREE)
10517 : 21 : && dump ("Read member:%d %C:%N", tag, TREE_CODE (res), res);
10518 : : }
10519 : : }
10520 : : break;
10521 : :
10522 : 1027 : case tt_binfo:
10523 : : /* A BINFO. Walk the tree of the dominating type. */
10524 : 1027 : {
10525 : 1027 : tree type;
10526 : 1027 : unsigned ix = binfo_mergeable (&type);
10527 : 1027 : if (type)
10528 : : {
10529 : 1027 : res = TYPE_BINFO (type);
10530 : 1108 : for (; ix && res; res = TREE_CHAIN (res))
10531 : 81 : ix--;
10532 : 1027 : if (!res)
10533 : 0 : set_overrun ();
10534 : : }
10535 : :
10536 : 1027 : if (get_overrun ())
10537 : : break;
10538 : :
10539 : : /* Insert binfo into backreferences. */
10540 : 1027 : tag = insert (res);
10541 : 1027 : dump (dumper::TREE) && dump ("Read binfo:%d %N", tag, res);
10542 : : }
10543 : 1027 : break;
10544 : :
10545 : 9 : case tt_vtable:
10546 : 9 : {
10547 : 9 : unsigned ix = u ();
10548 : 9 : tree ctx = tree_node ();
10549 : 9 : dump (dumper::TREE) && dump ("Reading vtable %N[%u]", ctx, ix);
10550 : 9 : if (TREE_CODE (ctx) == RECORD_TYPE && TYPE_LANG_SPECIFIC (ctx))
10551 : 9 : for (res = CLASSTYPE_VTABLES (ctx); res; res = DECL_CHAIN (res))
10552 : 9 : if (!ix--)
10553 : : break;
10554 : 9 : if (!res)
10555 : 0 : set_overrun ();
10556 : : }
10557 : : break;
10558 : :
10559 : 0 : case tt_thunk:
10560 : 0 : {
10561 : 0 : int fixed = i ();
10562 : 0 : tree target = tree_node ();
10563 : 0 : tree virt = tree_node ();
10564 : :
10565 : 0 : for (tree thunk = DECL_THUNKS (target);
10566 : 0 : thunk; thunk = DECL_CHAIN (thunk))
10567 : 0 : if (THUNK_FIXED_OFFSET (thunk) == fixed
10568 : 0 : && !THUNK_VIRTUAL_OFFSET (thunk) == !virt
10569 : 0 : && (!virt
10570 : 0 : || tree_int_cst_equal (virt, THUNK_VIRTUAL_OFFSET (thunk))))
10571 : : {
10572 : : res = thunk;
10573 : : break;
10574 : : }
10575 : :
10576 : 0 : int tag = insert (res);
10577 : 0 : if (res)
10578 : 0 : dump (dumper::TREE)
10579 : 0 : && dump ("Read:%d thunk %N to %N", tag, DECL_NAME (res), target);
10580 : : else
10581 : 0 : set_overrun ();
10582 : : }
10583 : : break;
10584 : :
10585 : 100351 : case tt_clone_ref:
10586 : 100351 : {
10587 : 100351 : tree target = tree_node ();
10588 : 100351 : tree name = tree_node ();
10589 : :
10590 : 100351 : if (DECL_P (target) && DECL_MAYBE_IN_CHARGE_CDTOR_P (target))
10591 : : {
10592 : 100351 : tree clone;
10593 : 157039 : FOR_EVERY_CLONE (clone, target)
10594 : 157039 : if (DECL_NAME (clone) == name)
10595 : : {
10596 : : res = clone;
10597 : : break;
10598 : : }
10599 : : }
10600 : :
10601 : : /* A clone might have a different vtable entry. */
10602 : 100351 : if (res && DECL_VIRTUAL_P (res))
10603 : 6624 : DECL_VINDEX (res) = tree_node ();
10604 : :
10605 : 100351 : if (!res)
10606 : 0 : set_overrun ();
10607 : 100351 : int tag = insert (res);
10608 : 100351 : if (res)
10609 : 100351 : dump (dumper::TREE)
10610 : 222 : && dump ("Read:%d clone %N of %N", tag, DECL_NAME (res), target);
10611 : : else
10612 : 0 : set_overrun ();
10613 : : }
10614 : : break;
10615 : :
10616 : 765213 : case tt_entity:
10617 : : /* Index into the entity table. Perhaps not loaded yet! */
10618 : 765213 : {
10619 : 765213 : unsigned origin = state->slurp->remap_module (u ());
10620 : 765213 : unsigned ident = u ();
10621 : 765213 : module_state *from = (*modules)[origin];
10622 : :
10623 : 765213 : if (!origin || ident >= from->entity_num)
10624 : 0 : set_overrun ();
10625 : 765213 : if (!get_overrun ())
10626 : : {
10627 : 765213 : binding_slot *slot = &(*entity_ary)[from->entity_lwm + ident];
10628 : 765213 : if (slot->is_lazy ())
10629 : 24265 : if (!from->lazy_load (ident, slot))
10630 : 0 : set_overrun ();
10631 : 765213 : res = *slot;
10632 : : }
10633 : :
10634 : 765213 : if (res)
10635 : : {
10636 : 765213 : const char *kind = (origin != state->mod ? "Imported" : "Named");
10637 : 765213 : int tag = insert (res);
10638 : 765213 : dump (dumper::TREE)
10639 : 600 : && dump ("%s:%d %C:%N@%M", kind, tag, TREE_CODE (res),
10640 : 600 : res, (*modules)[origin]);
10641 : :
10642 : 765213 : if (!add_indirects (res))
10643 : : {
10644 : 0 : set_overrun ();
10645 : 0 : res = NULL_TREE;
10646 : : }
10647 : : }
10648 : : }
10649 : : break;
10650 : :
10651 : 1814 : case tt_template:
10652 : : /* A template. */
10653 : 1814 : if (tree tpl = tree_node ())
10654 : : {
10655 : 1814 : res = (TREE_CODE (tpl) == TU_LOCAL_ENTITY ?
10656 : 1814 : tpl : DECL_TEMPLATE_RESULT (tpl));
10657 : 1814 : dump (dumper::TREE)
10658 : 9 : && dump ("Read template %C:%N", TREE_CODE (res), res);
10659 : : }
10660 : : break;
10661 : : }
10662 : :
10663 : 64762912 : if (is_use && !unused && res && DECL_P (res) && !TREE_USED (res))
10664 : : {
10665 : : /* Mark decl used as mark_used does -- we cannot call
10666 : : mark_used in the middle of streaming, we only need a subset
10667 : : of its functionality. */
10668 : 437258 : TREE_USED (res) = true;
10669 : :
10670 : : /* And for structured bindings also the underlying decl. */
10671 : 437258 : if (DECL_DECOMPOSITION_P (res) && !DECL_DECOMP_IS_BASE (res))
10672 : 758 : TREE_USED (DECL_DECOMP_BASE (res)) = true;
10673 : :
10674 : 437258 : if (DECL_CLONED_FUNCTION_P (res))
10675 : 4170 : TREE_USED (DECL_CLONED_FUNCTION (res)) = true;
10676 : : }
10677 : :
10678 : 64762912 : dump.outdent ();
10679 : 64762912 : return res;
10680 : : }
10681 : :
10682 : : void
10683 : 1355308 : trees_out::tpl_parms (tree parms, unsigned &tpl_levels)
10684 : : {
10685 : 1355308 : if (!parms)
10686 : : return;
10687 : :
10688 : 910617 : if (TREE_VISITED (parms))
10689 : : {
10690 : 383458 : ref_node (parms);
10691 : 383458 : return;
10692 : : }
10693 : :
10694 : 527159 : tpl_parms (TREE_CHAIN (parms), tpl_levels);
10695 : :
10696 : 527159 : tree vec = TREE_VALUE (parms);
10697 : 527159 : unsigned len = TREE_VEC_LENGTH (vec);
10698 : : /* Depth. */
10699 : 527159 : int tag = insert (parms);
10700 : 527159 : if (streaming_p ())
10701 : : {
10702 : 143134 : i (len + 1);
10703 : 143200 : dump (dumper::TREE)
10704 : 66 : && dump ("Writing template parms:%d level:%N length:%d",
10705 : 66 : tag, TREE_PURPOSE (parms), len);
10706 : : }
10707 : 527159 : tree_node (TREE_PURPOSE (parms));
10708 : :
10709 : 1448390 : for (unsigned ix = 0; ix != len; ix++)
10710 : : {
10711 : 921231 : tree parm = TREE_VEC_ELT (vec, ix);
10712 : 921231 : tree decl = TREE_VALUE (parm);
10713 : :
10714 : 921231 : gcc_checking_assert (DECL_TEMPLATE_PARM_P (decl));
10715 : 921231 : if (CHECKING_P)
10716 : 921231 : switch (TREE_CODE (decl))
10717 : : {
10718 : 0 : default: gcc_unreachable ();
10719 : :
10720 : 2757 : case TEMPLATE_DECL:
10721 : 2757 : gcc_assert ((TREE_CODE (TREE_TYPE (decl)) == TEMPLATE_TEMPLATE_PARM)
10722 : : && (TREE_CODE (DECL_TEMPLATE_RESULT (decl)) == TYPE_DECL)
10723 : : && (TYPE_NAME (TREE_TYPE (decl)) == decl));
10724 : : break;
10725 : :
10726 : 862673 : case TYPE_DECL:
10727 : 862673 : gcc_assert ((TREE_CODE (TREE_TYPE (decl)) == TEMPLATE_TYPE_PARM)
10728 : : && (TYPE_NAME (TREE_TYPE (decl)) == decl));
10729 : : break;
10730 : :
10731 : 55801 : case PARM_DECL:
10732 : 55801 : gcc_assert ((TREE_CODE (DECL_INITIAL (decl)) == TEMPLATE_PARM_INDEX)
10733 : : && (TREE_CODE (TEMPLATE_PARM_DECL (DECL_INITIAL (decl)))
10734 : : == CONST_DECL)
10735 : : && (DECL_TEMPLATE_PARM_P
10736 : : (TEMPLATE_PARM_DECL (DECL_INITIAL (decl)))));
10737 : : break;
10738 : : }
10739 : :
10740 : 921231 : tree_node (decl);
10741 : 921231 : tree_node (TEMPLATE_PARM_CONSTRAINTS (parm));
10742 : : }
10743 : :
10744 : 527159 : tpl_levels++;
10745 : : }
10746 : :
10747 : : tree
10748 : 224098 : trees_in::tpl_parms (unsigned &tpl_levels)
10749 : : {
10750 : 224098 : tree parms = NULL_TREE;
10751 : :
10752 : 470160 : while (int len = i ())
10753 : : {
10754 : 246062 : if (len < 0)
10755 : : {
10756 : 132954 : parms = back_ref (len);
10757 : 132954 : continue;
10758 : : }
10759 : :
10760 : 113108 : len -= 1;
10761 : 113108 : parms = tree_cons (NULL_TREE, NULL_TREE, parms);
10762 : 113108 : int tag = insert (parms);
10763 : 113108 : TREE_PURPOSE (parms) = tree_node ();
10764 : :
10765 : 113108 : dump (dumper::TREE)
10766 : 105 : && dump ("Reading template parms:%d level:%N length:%d",
10767 : 105 : tag, TREE_PURPOSE (parms), len);
10768 : :
10769 : 113108 : tree vec = make_tree_vec (len);
10770 : 300874 : for (int ix = 0; ix != len; ix++)
10771 : : {
10772 : 187766 : tree decl = tree_node ();
10773 : 187766 : if (!decl)
10774 : : return NULL_TREE;
10775 : :
10776 : 187766 : tree parm = build_tree_list (NULL, decl);
10777 : 187766 : TEMPLATE_PARM_CONSTRAINTS (parm) = tree_node ();
10778 : :
10779 : 187766 : TREE_VEC_ELT (vec, ix) = parm;
10780 : : }
10781 : :
10782 : 113108 : TREE_VALUE (parms) = vec;
10783 : 113108 : tpl_levels++;
10784 : : }
10785 : :
10786 : : return parms;
10787 : : }
10788 : :
10789 : : void
10790 : 828149 : trees_out::tpl_parms_fini (tree tmpl, unsigned tpl_levels)
10791 : : {
10792 : 828149 : for (tree parms = DECL_TEMPLATE_PARMS (tmpl);
10793 : 1355308 : tpl_levels--; parms = TREE_CHAIN (parms))
10794 : : {
10795 : 527159 : tree vec = TREE_VALUE (parms);
10796 : :
10797 : 527159 : tree_node (TREE_TYPE (vec));
10798 : 1448390 : for (unsigned ix = TREE_VEC_LENGTH (vec); ix--;)
10799 : : {
10800 : 921231 : tree parm = TREE_VEC_ELT (vec, ix);
10801 : 921231 : tree dflt = TREE_PURPOSE (parm);
10802 : 921231 : tree_node (dflt);
10803 : :
10804 : : /* Template template parameters need a context of their owning
10805 : : template. This is quite tricky to infer correctly on stream-in
10806 : : (see PR c++/98881) so we'll just provide it directly. */
10807 : 921231 : tree decl = TREE_VALUE (parm);
10808 : 921231 : if (TREE_CODE (decl) == TEMPLATE_DECL)
10809 : 2757 : tree_node (DECL_CONTEXT (decl));
10810 : : }
10811 : : }
10812 : 828149 : }
10813 : :
10814 : : bool
10815 : 224098 : trees_in::tpl_parms_fini (tree tmpl, unsigned tpl_levels)
10816 : : {
10817 : 224098 : for (tree parms = DECL_TEMPLATE_PARMS (tmpl);
10818 : 337206 : tpl_levels--; parms = TREE_CHAIN (parms))
10819 : : {
10820 : 113108 : tree vec = TREE_VALUE (parms);
10821 : :
10822 : 113108 : TREE_TYPE (vec) = tree_node ();
10823 : 300874 : for (unsigned ix = TREE_VEC_LENGTH (vec); ix--;)
10824 : : {
10825 : 187766 : tree parm = TREE_VEC_ELT (vec, ix);
10826 : 187766 : tree dflt = tree_node ();
10827 : 187766 : TREE_PURPOSE (parm) = dflt;
10828 : :
10829 : 187766 : tree decl = TREE_VALUE (parm);
10830 : 187766 : if (TREE_CODE (decl) == TEMPLATE_DECL)
10831 : 612 : DECL_CONTEXT (decl) = tree_node ();
10832 : :
10833 : 187766 : if (get_overrun ())
10834 : : return false;
10835 : : }
10836 : : }
10837 : : return true;
10838 : : }
10839 : :
10840 : : /* PARMS is a LIST, one node per level.
10841 : : TREE_VALUE is a TREE_VEC of parm info for that level.
10842 : : each ELT is a TREE_LIST
10843 : : TREE_VALUE is PARM_DECL, TYPE_DECL or TEMPLATE_DECL
10844 : : TREE_PURPOSE is the default value. */
10845 : :
10846 : : void
10847 : 828149 : trees_out::tpl_header (tree tpl, unsigned *tpl_levels)
10848 : : {
10849 : 828149 : tree parms = DECL_TEMPLATE_PARMS (tpl);
10850 : 828149 : tpl_parms (parms, *tpl_levels);
10851 : :
10852 : : /* Mark end. */
10853 : 828149 : if (streaming_p ())
10854 : 275676 : u (0);
10855 : :
10856 : 828149 : if (*tpl_levels)
10857 : 497977 : tree_node (TEMPLATE_PARMS_CONSTRAINTS (parms));
10858 : 828149 : }
10859 : :
10860 : : bool
10861 : 224098 : trees_in::tpl_header (tree tpl, unsigned *tpl_levels)
10862 : : {
10863 : 224098 : tree parms = tpl_parms (*tpl_levels);
10864 : 224098 : if (!parms)
10865 : : return false;
10866 : :
10867 : 224098 : DECL_TEMPLATE_PARMS (tpl) = parms;
10868 : :
10869 : 224098 : if (*tpl_levels)
10870 : 111734 : TEMPLATE_PARMS_CONSTRAINTS (parms) = tree_node ();
10871 : :
10872 : : return true;
10873 : : }
10874 : :
10875 : : /* Stream skeleton parm nodes, with their flags, type & parm indices.
10876 : : All the parms will have consecutive tags. */
10877 : :
10878 : : void
10879 : 1090179 : trees_out::fn_parms_init (tree fn)
10880 : : {
10881 : : /* First init them. */
10882 : 1090179 : int base_tag = ref_num - 1;
10883 : 1090179 : int ix = 0;
10884 : 1090179 : for (tree parm = DECL_ARGUMENTS (fn);
10885 : 3315817 : parm; parm = DECL_CHAIN (parm), ix++)
10886 : : {
10887 : 2225638 : if (streaming_p ())
10888 : : {
10889 : 741845 : start (parm);
10890 : 741845 : tree_node_bools (parm);
10891 : : }
10892 : 2225638 : int tag = insert (parm);
10893 : 2225638 : gcc_checking_assert (base_tag - ix == tag);
10894 : : }
10895 : : /* Mark the end. */
10896 : 1090179 : if (streaming_p ())
10897 : 363559 : u (0);
10898 : :
10899 : : /* Now stream their contents. */
10900 : 1090179 : ix = 0;
10901 : 1090179 : for (tree parm = DECL_ARGUMENTS (fn);
10902 : 3315817 : parm; parm = DECL_CHAIN (parm), ix++)
10903 : : {
10904 : 2225638 : if (streaming_p ())
10905 : 741845 : dump (dumper::TREE)
10906 : 207 : && dump ("Writing parm:%d %u (%N) of %N",
10907 : : base_tag - ix, ix, parm, fn);
10908 : 2225638 : tree_node_vals (parm);
10909 : : }
10910 : :
10911 : 1090179 : if (!streaming_p ())
10912 : : {
10913 : : /* We must walk contract attrs so the dependency graph is complete. */
10914 : 726620 : for (tree contract = DECL_CONTRACTS (fn);
10915 : 726728 : contract;
10916 : 108 : contract = CONTRACT_CHAIN (contract))
10917 : 108 : tree_node (contract);
10918 : : }
10919 : :
10920 : : /* Write a reference to contracts pre/post functions, if any, to avoid
10921 : : regenerating them in importers. */
10922 : 1090179 : tree_node (DECL_PRE_FN (fn));
10923 : 1090179 : tree_node (DECL_POST_FN (fn));
10924 : 1090179 : }
10925 : :
10926 : : /* Build skeleton parm nodes, read their flags, type & parm indices. */
10927 : :
10928 : : int
10929 : 313287 : trees_in::fn_parms_init (tree fn)
10930 : : {
10931 : 313287 : int base_tag = ~(int)back_refs.length ();
10932 : :
10933 : 313287 : tree *parm_ptr = &DECL_ARGUMENTS (fn);
10934 : 313287 : int ix = 0;
10935 : 947901 : for (; int code = u (); ix++)
10936 : : {
10937 : 634614 : tree parm = start (code);
10938 : 634614 : if (!tree_node_bools (parm))
10939 : : return 0;
10940 : :
10941 : 634614 : int tag = insert (parm);
10942 : 634614 : gcc_checking_assert (base_tag - ix == tag);
10943 : 634614 : *parm_ptr = parm;
10944 : 634614 : parm_ptr = &DECL_CHAIN (parm);
10945 : 634614 : }
10946 : :
10947 : 313287 : ix = 0;
10948 : 313287 : for (tree parm = DECL_ARGUMENTS (fn);
10949 : 947901 : parm; parm = DECL_CHAIN (parm), ix++)
10950 : : {
10951 : 634614 : dump (dumper::TREE)
10952 : 351 : && dump ("Reading parm:%d %u (%N) of %N",
10953 : : base_tag - ix, ix, parm, fn);
10954 : 634614 : if (!tree_node_vals (parm))
10955 : : return 0;
10956 : : }
10957 : :
10958 : : /* Reload references to contract functions, if any. */
10959 : 313287 : tree pre_fn = tree_node ();
10960 : 313287 : tree post_fn = tree_node ();
10961 : 313287 : set_contract_functions (fn, pre_fn, post_fn);
10962 : :
10963 : 313287 : return base_tag;
10964 : : }
10965 : :
10966 : : /* Read the remaining parm node data. Replace with existing (if
10967 : : non-null) in the map. */
10968 : :
10969 : : void
10970 : 313287 : trees_in::fn_parms_fini (int tag, tree fn, tree existing, bool is_defn)
10971 : : {
10972 : 495219 : tree existing_parm = existing ? DECL_ARGUMENTS (existing) : NULL_TREE;
10973 : 313287 : tree parms = DECL_ARGUMENTS (fn);
10974 : 313287 : unsigned ix = 0;
10975 : 947901 : for (tree parm = parms; parm; parm = DECL_CHAIN (parm), ix++)
10976 : : {
10977 : 634614 : if (existing_parm)
10978 : : {
10979 : 538042 : if (is_defn && !DECL_SAVED_TREE (existing))
10980 : : {
10981 : : /* If we're about to become the definition, set the
10982 : : names of the parms from us. */
10983 : 13274 : DECL_NAME (existing_parm) = DECL_NAME (parm);
10984 : 13274 : DECL_SOURCE_LOCATION (existing_parm) = DECL_SOURCE_LOCATION (parm);
10985 : : }
10986 : :
10987 : 366121 : back_refs[~tag] = existing_parm;
10988 : 366121 : existing_parm = DECL_CHAIN (existing_parm);
10989 : : }
10990 : 634614 : tag--;
10991 : : }
10992 : 313287 : }
10993 : :
10994 : : /* Encode into KEY the position of the local type (class or enum)
10995 : : declaration DECL within FN. The position is encoded as the
10996 : : index of the innermost BLOCK (numbered in BFS order) along with
10997 : : the index within its BLOCK_VARS list. */
10998 : :
10999 : : void
11000 : 9447 : trees_out::key_local_type (merge_key& key, tree decl, tree fn)
11001 : : {
11002 : 9447 : auto_vec<tree, 4> blocks;
11003 : 9447 : blocks.quick_push (DECL_INITIAL (fn));
11004 : 9447 : unsigned block_ix = 0;
11005 : 43599 : while (block_ix != blocks.length ())
11006 : : {
11007 : 17076 : tree block = blocks[block_ix];
11008 : 17076 : unsigned decl_ix = 0;
11009 : 49962 : for (tree var = BLOCK_VARS (block); var; var = DECL_CHAIN (var))
11010 : : {
11011 : 42333 : if (TREE_CODE (var) != TYPE_DECL)
11012 : 25638 : continue;
11013 : 16695 : if (var == decl)
11014 : : {
11015 : 9447 : key.index = (block_ix << 10) | decl_ix;
11016 : 9447 : return;
11017 : : }
11018 : 7248 : ++decl_ix;
11019 : : }
11020 : 15951 : for (tree sub = BLOCK_SUBBLOCKS (block); sub; sub = BLOCK_CHAIN (sub))
11021 : 8322 : blocks.safe_push (sub);
11022 : 7629 : ++block_ix;
11023 : : }
11024 : :
11025 : : /* Not-found value. */
11026 : 0 : key.index = 1023;
11027 : 9447 : }
11028 : :
11029 : : /* Look up the local type corresponding at the position encoded by
11030 : : KEY within FN and named NAME. */
11031 : :
11032 : : tree
11033 : 2749 : trees_in::key_local_type (const merge_key& key, tree fn, tree name)
11034 : : {
11035 : 2749 : if (!DECL_INITIAL (fn))
11036 : : return NULL_TREE;
11037 : :
11038 : 1680 : const unsigned block_pos = key.index >> 10;
11039 : 1680 : const unsigned decl_pos = key.index & 1023;
11040 : :
11041 : 1680 : if (decl_pos == 1023)
11042 : : return NULL_TREE;
11043 : :
11044 : 1680 : auto_vec<tree, 4> blocks;
11045 : 1680 : blocks.quick_push (DECL_INITIAL (fn));
11046 : 1680 : unsigned block_ix = 0;
11047 : 7822 : while (block_ix != blocks.length ())
11048 : : {
11049 : 3071 : tree block = blocks[block_ix];
11050 : 3071 : if (block_ix == block_pos)
11051 : : {
11052 : 1680 : unsigned decl_ix = 0;
11053 : 4602 : for (tree var = BLOCK_VARS (block); var; var = DECL_CHAIN (var))
11054 : : {
11055 : 4602 : if (TREE_CODE (var) != TYPE_DECL)
11056 : 2013 : continue;
11057 : : /* Prefer using the identifier as the key for more robustness
11058 : : to ODR violations, except for anonymous types since their
11059 : : compiler-generated identifiers aren't stable. */
11060 : 5178 : if (IDENTIFIER_ANON_P (name)
11061 : 2589 : ? decl_ix == decl_pos
11062 : 302 : : DECL_NAME (var) == name)
11063 : : return var;
11064 : 909 : ++decl_ix;
11065 : : }
11066 : : return NULL_TREE;
11067 : : }
11068 : 2892 : for (tree sub = BLOCK_SUBBLOCKS (block); sub; sub = BLOCK_CHAIN (sub))
11069 : 1501 : blocks.safe_push (sub);
11070 : 1391 : ++block_ix;
11071 : : }
11072 : :
11073 : : return NULL_TREE;
11074 : 1680 : }
11075 : :
11076 : : /* DEP is the depset of some decl we're streaming by value. Determine
11077 : : the merging behaviour. */
11078 : :
11079 : : merge_kind
11080 : 2760273 : trees_out::get_merge_kind (tree decl, depset *dep)
11081 : : {
11082 : 2760273 : if (!dep)
11083 : : {
11084 : 497467 : if (VAR_OR_FUNCTION_DECL_P (decl))
11085 : : {
11086 : : /* Any var or function with template info should have DEP. */
11087 : 269752 : gcc_checking_assert (!DECL_LANG_SPECIFIC (decl)
11088 : : || !DECL_TEMPLATE_INFO (decl));
11089 : 269752 : if (DECL_LOCAL_DECL_P (decl))
11090 : : return MK_unique;
11091 : : }
11092 : :
11093 : : /* Either unique, or some member of a class that cannot have an
11094 : : out-of-class definition. For instance a FIELD_DECL. */
11095 : 497247 : tree ctx = CP_DECL_CONTEXT (decl);
11096 : 497247 : if (TREE_CODE (ctx) == FUNCTION_DECL)
11097 : : {
11098 : : /* USING_DECLs and NAMESPACE_DECLs cannot have DECL_TEMPLATE_INFO --
11099 : : this isn't permitting them to have one. */
11100 : 295092 : gcc_checking_assert (TREE_CODE (decl) == USING_DECL
11101 : : || TREE_CODE (decl) == NAMESPACE_DECL
11102 : : || !DECL_LANG_SPECIFIC (decl)
11103 : : || !DECL_TEMPLATE_INFO (decl));
11104 : :
11105 : : return MK_unique;
11106 : : }
11107 : :
11108 : 202155 : if (TREE_CODE (decl) == TEMPLATE_DECL
11109 : 202155 : && DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (decl))
11110 : : return MK_local_friend;
11111 : :
11112 : 202155 : gcc_checking_assert (TYPE_P (ctx));
11113 : 202155 : if (TREE_CODE (decl) == USING_DECL)
11114 : : return MK_field;
11115 : :
11116 : 127950 : if (TREE_CODE (decl) == FIELD_DECL)
11117 : : {
11118 : 95631 : if (DECL_NAME (decl))
11119 : : {
11120 : : /* Anonymous FIELD_DECLs have a NULL name. */
11121 : 74108 : gcc_checking_assert (!IDENTIFIER_ANON_P (DECL_NAME (decl)));
11122 : : return MK_named;
11123 : : }
11124 : :
11125 : 21523 : if (!DECL_NAME (decl)
11126 : 21523 : && !RECORD_OR_UNION_TYPE_P (TREE_TYPE (decl))
11127 : 22417 : && !DECL_BIT_FIELD_REPRESENTATIVE (decl))
11128 : : {
11129 : : /* The underlying storage unit for a bitfield. We do not
11130 : : need to dedup it, because it's only reachable through
11131 : : the bitfields it represents. And those are deduped. */
11132 : : // FIXME: Is that assertion correct -- do we ever fish it
11133 : : // out and put it in an expr?
11134 : 340 : gcc_checking_assert ((TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
11135 : : ? TREE_CODE (TREE_TYPE (TREE_TYPE (decl)))
11136 : : : TREE_CODE (TREE_TYPE (decl)))
11137 : : == INTEGER_TYPE);
11138 : : return MK_unique;
11139 : : }
11140 : :
11141 : : return MK_field;
11142 : : }
11143 : :
11144 : 32319 : if (TREE_CODE (decl) == CONST_DECL)
11145 : : return MK_named;
11146 : :
11147 : 6704 : if (TREE_CODE (decl) == VAR_DECL
11148 : 6704 : && DECL_VTABLE_OR_VTT_P (decl))
11149 : : return MK_vtable;
11150 : :
11151 : 1128 : if (DECL_THUNK_P (decl))
11152 : : /* Thunks are unique-enough, because they're only referenced
11153 : : from the vtable. And that's either new (so we want the
11154 : : thunks), or it's a duplicate (so it will be dropped). */
11155 : : return MK_unique;
11156 : :
11157 : : /* There should be no other cases. */
11158 : 0 : gcc_unreachable ();
11159 : : }
11160 : :
11161 : 2262806 : gcc_checking_assert (TREE_CODE (decl) != FIELD_DECL
11162 : : && TREE_CODE (decl) != USING_DECL
11163 : : && TREE_CODE (decl) != CONST_DECL);
11164 : :
11165 : 2262806 : if (is_key_order ())
11166 : : {
11167 : : /* When doing the mergeablilty graph, there's an indirection to
11168 : : the actual depset. */
11169 : 754148 : gcc_assert (dep->is_special ());
11170 : 754148 : dep = dep->deps[0];
11171 : : }
11172 : :
11173 : 2262806 : gcc_checking_assert (decl == dep->get_entity ());
11174 : :
11175 : 2262806 : merge_kind mk = MK_named;
11176 : 2262806 : switch (dep->get_entity_kind ())
11177 : : {
11178 : 0 : default:
11179 : 0 : gcc_unreachable ();
11180 : :
11181 : : case depset::EK_PARTIAL:
11182 : : mk = MK_partial;
11183 : : break;
11184 : :
11185 : 1298150 : case depset::EK_DECL:
11186 : 1298150 : {
11187 : 1298150 : tree ctx = CP_DECL_CONTEXT (decl);
11188 : :
11189 : 1298150 : switch (TREE_CODE (ctx))
11190 : : {
11191 : 0 : default:
11192 : 0 : gcc_unreachable ();
11193 : :
11194 : 9447 : case FUNCTION_DECL:
11195 : 9447 : gcc_checking_assert
11196 : : (DECL_IMPLICIT_TYPEDEF_P (STRIP_TEMPLATE (decl)));
11197 : :
11198 : : mk = MK_local_type;
11199 : : break;
11200 : :
11201 : 1288703 : case RECORD_TYPE:
11202 : 1288703 : case UNION_TYPE:
11203 : 1288703 : case NAMESPACE_DECL:
11204 : 1288703 : if (DECL_NAME (decl) == as_base_identifier)
11205 : : {
11206 : : mk = MK_as_base;
11207 : : break;
11208 : : }
11209 : :
11210 : : /* A lambda may have a class as its context, even though it
11211 : : isn't a member in the traditional sense; see the test
11212 : : g++.dg/modules/lambda-6_a.C. */
11213 : 1657213 : if (DECL_IMPLICIT_TYPEDEF_P (STRIP_TEMPLATE (decl))
11214 : 1422982 : && LAMBDA_TYPE_P (TREE_TYPE (decl)))
11215 : : {
11216 : 232 : if (tree scope = LAMBDA_TYPE_EXTRA_SCOPE (TREE_TYPE (decl)))
11217 : : {
11218 : : /* Lambdas attached to fields are keyed to its class. */
11219 : 214 : if (TREE_CODE (scope) == FIELD_DECL)
11220 : 9 : scope = TYPE_NAME (DECL_CONTEXT (scope));
11221 : 214 : if (DECL_LANG_SPECIFIC (scope)
11222 : 428 : && DECL_MODULE_KEYED_DECLS_P (scope))
11223 : : {
11224 : : mk = MK_keyed;
11225 : : break;
11226 : : }
11227 : : }
11228 : : /* Lambdas not attached to any mangling scope are TU-local. */
11229 : : mk = MK_unique;
11230 : : break;
11231 : : }
11232 : :
11233 : 1227241 : if (TREE_CODE (decl) == TEMPLATE_DECL
11234 : 1227241 : && DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (decl))
11235 : : {
11236 : : mk = MK_local_friend;
11237 : : break;
11238 : : }
11239 : :
11240 : 1214755 : if (DECL_DECOMPOSITION_P (decl))
11241 : : {
11242 : : mk = MK_unique;
11243 : : break;
11244 : : }
11245 : :
11246 : 1214347 : if (IDENTIFIER_ANON_P (DECL_NAME (decl)))
11247 : : {
11248 : 20281 : if (RECORD_OR_UNION_TYPE_P (ctx))
11249 : : mk = MK_field;
11250 : 1065 : else if (DECL_IMPLICIT_TYPEDEF_P (decl)
11251 : 1065 : && UNSCOPED_ENUM_P (TREE_TYPE (decl))
11252 : 2130 : && TYPE_VALUES (TREE_TYPE (decl)))
11253 : : /* Keyed by first enum value, and underlying type. */
11254 : : mk = MK_enum;
11255 : : else
11256 : : /* No way to merge it, it is an ODR land-mine. */
11257 : : mk = MK_unique;
11258 : : }
11259 : : }
11260 : : }
11261 : : break;
11262 : :
11263 : 918894 : case depset::EK_SPECIALIZATION:
11264 : 918894 : {
11265 : 918894 : gcc_checking_assert (dep->is_special ());
11266 : :
11267 : 918894 : if (TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
11268 : : /* An block-scope classes of templates are themselves
11269 : : templates. */
11270 : 4050 : gcc_checking_assert (DECL_IMPLICIT_TYPEDEF_P (decl));
11271 : :
11272 : 918894 : if (dep->is_friend_spec ())
11273 : : mk = MK_friend_spec;
11274 : 918894 : else if (dep->is_type_spec ())
11275 : : mk = MK_type_spec;
11276 : : else
11277 : 637308 : mk = MK_decl_spec;
11278 : :
11279 : 918894 : if (TREE_CODE (decl) == TEMPLATE_DECL)
11280 : : {
11281 : 62307 : spec_entry *entry = reinterpret_cast <spec_entry *> (dep->deps[0]);
11282 : 62307 : if (TREE_CODE (entry->spec) != TEMPLATE_DECL)
11283 : 6792 : mk = merge_kind (mk | MK_tmpl_tmpl_mask);
11284 : : }
11285 : : }
11286 : : break;
11287 : : }
11288 : :
11289 : : return mk;
11290 : : }
11291 : :
11292 : :
11293 : : /* The container of DECL -- not necessarily its context! */
11294 : :
11295 : : tree
11296 : 2760273 : trees_out::decl_container (tree decl)
11297 : : {
11298 : 2760273 : int use_tpl;
11299 : 2760273 : tree tpl = NULL_TREE;
11300 : 2760273 : if (tree template_info = node_template_info (decl, use_tpl))
11301 : 968864 : tpl = TI_TEMPLATE (template_info);
11302 : 2760273 : if (tpl == decl)
11303 : 0 : tpl = nullptr;
11304 : :
11305 : : /* Stream the template we're instantiated from. */
11306 : 2760273 : tree_node (tpl);
11307 : :
11308 : 2760273 : tree container = NULL_TREE;
11309 : 2760273 : if (TREE_CODE (decl) == TEMPLATE_DECL
11310 : 2760273 : && DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (decl))
11311 : 12486 : container = DECL_CHAIN (decl);
11312 : : else
11313 : 2747787 : container = CP_DECL_CONTEXT (decl);
11314 : :
11315 : 2760273 : if (TYPE_P (container))
11316 : 1607903 : container = TYPE_NAME (container);
11317 : :
11318 : 2760273 : tree_node (container);
11319 : :
11320 : 2760273 : return container;
11321 : : }
11322 : :
11323 : : tree
11324 : 817691 : trees_in::decl_container ()
11325 : : {
11326 : : /* The maybe-template. */
11327 : 817691 : (void)tree_node ();
11328 : :
11329 : 817691 : tree container = tree_node ();
11330 : :
11331 : 817691 : return container;
11332 : : }
11333 : :
11334 : : /* Write out key information about a mergeable DEP. Does not write
11335 : : the contents of DEP itself. The context has already been
11336 : : written. The container has already been streamed. */
11337 : :
11338 : : void
11339 : 2760273 : trees_out::key_mergeable (int tag, merge_kind mk, tree decl, tree inner,
11340 : : tree container, depset *dep)
11341 : : {
11342 : 2760273 : if (dep && is_key_order ())
11343 : : {
11344 : 754148 : gcc_checking_assert (dep->is_special ());
11345 : 754148 : dep = dep->deps[0];
11346 : : }
11347 : :
11348 : 2760273 : if (streaming_p ())
11349 : 997864 : dump (dumper::MERGE)
11350 : 798 : && dump ("Writing:%d's %s merge key (%s) %C:%N", tag, merge_kind_name[mk],
11351 : 723 : dep ? dep->entity_kind_name () : "contained",
11352 : 798 : TREE_CODE (decl), decl);
11353 : :
11354 : : /* Now write the locating information. */
11355 : 2760273 : if (mk & MK_template_mask)
11356 : : {
11357 : : /* Specializations are located via their originating template,
11358 : : and the set of template args they specialize. */
11359 : 918894 : gcc_checking_assert (dep && dep->is_special ());
11360 : 918894 : spec_entry *entry = reinterpret_cast <spec_entry *> (dep->deps[0]);
11361 : :
11362 : 918894 : tree_node (entry->tmpl);
11363 : 918894 : tree_node (entry->args);
11364 : 918894 : if (mk & MK_tmpl_decl_mask)
11365 : 637308 : if (flag_concepts && TREE_CODE (inner) == VAR_DECL)
11366 : : {
11367 : : /* Variable template partial specializations might need
11368 : : constraints (see spec_hasher::equal). It's simpler to
11369 : : write NULL when we don't need them. */
11370 : 13317 : tree constraints = NULL_TREE;
11371 : :
11372 : 13317 : if (uses_template_parms (entry->args))
11373 : 294 : constraints = get_constraints (inner);
11374 : 13317 : tree_node (constraints);
11375 : : }
11376 : :
11377 : 918894 : if (CHECKING_P)
11378 : : {
11379 : : /* Make sure we can locate the decl. */
11380 : 918894 : tree existing = match_mergeable_specialization
11381 : 918894 : (bool (mk & MK_tmpl_decl_mask), entry);
11382 : :
11383 : 918894 : gcc_assert (existing);
11384 : 918894 : if (mk & MK_tmpl_decl_mask)
11385 : : {
11386 : 637308 : if (mk & MK_tmpl_tmpl_mask)
11387 : 5376 : existing = DECL_TI_TEMPLATE (existing);
11388 : : }
11389 : : else
11390 : : {
11391 : 281586 : if (mk & MK_tmpl_tmpl_mask)
11392 : 1416 : existing = CLASSTYPE_TI_TEMPLATE (existing);
11393 : : else
11394 : 280170 : existing = TYPE_NAME (existing);
11395 : : }
11396 : :
11397 : : /* The walkabout should have found ourselves. */
11398 : 918894 : gcc_checking_assert (TREE_CODE (decl) == TYPE_DECL
11399 : : ? same_type_p (TREE_TYPE (decl),
11400 : : TREE_TYPE (existing))
11401 : : : existing == decl);
11402 : : }
11403 : : }
11404 : 1841379 : else if (mk != MK_unique)
11405 : : {
11406 : 1544173 : merge_key key;
11407 : 1544173 : tree name = DECL_NAME (decl);
11408 : :
11409 : 1544173 : switch (mk)
11410 : : {
11411 : 0 : default:
11412 : 0 : gcc_unreachable ();
11413 : :
11414 : 1293789 : case MK_named:
11415 : 1293789 : case MK_friend_spec:
11416 : 1293789 : if (IDENTIFIER_CONV_OP_P (name))
11417 : 4267 : name = conv_op_identifier;
11418 : :
11419 : 1293789 : if (TREE_CODE (inner) == FUNCTION_DECL)
11420 : : {
11421 : : /* Functions are distinguished by parameter types. */
11422 : 692012 : tree fn_type = TREE_TYPE (inner);
11423 : :
11424 : 692012 : key.ref_q = type_memfn_rqual (fn_type);
11425 : 692012 : key.args = TYPE_ARG_TYPES (fn_type);
11426 : :
11427 : 692012 : if (tree reqs = get_constraints (inner))
11428 : : {
11429 : 44241 : if (cxx_dialect < cxx20)
11430 : 42 : reqs = CI_ASSOCIATED_CONSTRAINTS (reqs);
11431 : : else
11432 : 88440 : reqs = CI_DECLARATOR_REQS (reqs);
11433 : 44241 : key.constraints = reqs;
11434 : : }
11435 : :
11436 : 692012 : if (IDENTIFIER_CONV_OP_P (name)
11437 : 692012 : || (decl != inner
11438 : 369477 : && !(name == fun_identifier
11439 : : /* In case the user names something _FUN */
11440 : 60 : && LAMBDA_TYPE_P (DECL_CONTEXT (inner)))))
11441 : : /* And a function template, or conversion operator needs
11442 : : the return type. Except for the _FUN thunk of a
11443 : : generic lambda, which has a recursive decl_type'd
11444 : : return type. */
11445 : : // FIXME: What if the return type is a voldemort?
11446 : 373714 : key.ret = fndecl_declared_return_type (inner);
11447 : : }
11448 : : break;
11449 : :
11450 : 114604 : case MK_field:
11451 : 114604 : {
11452 : 114604 : unsigned ix = 0;
11453 : 114604 : if (TREE_CODE (inner) != FIELD_DECL)
11454 : : name = NULL_TREE;
11455 : : else
11456 : 21183 : gcc_checking_assert (!name || !IDENTIFIER_ANON_P (name));
11457 : :
11458 : 114604 : for (tree field = TYPE_FIELDS (TREE_TYPE (container));
11459 : 236991 : ; field = DECL_CHAIN (field))
11460 : : {
11461 : 351595 : tree finner = STRIP_TEMPLATE (field);
11462 : 351595 : if (TREE_CODE (finner) == TREE_CODE (inner))
11463 : : {
11464 : 153844 : if (finner == inner)
11465 : : break;
11466 : 39240 : ix++;
11467 : : }
11468 : 236991 : }
11469 : 114604 : key.index = ix;
11470 : : }
11471 : 114604 : break;
11472 : :
11473 : 5576 : case MK_vtable:
11474 : 5576 : {
11475 : 5576 : tree vtable = CLASSTYPE_VTABLES (TREE_TYPE (container));
11476 : 7136 : for (unsigned ix = 0; ; vtable = DECL_CHAIN (vtable), ix++)
11477 : 7136 : if (vtable == decl)
11478 : : {
11479 : 5576 : key.index = ix;
11480 : 5576 : break;
11481 : : }
11482 : 5576 : name = NULL_TREE;
11483 : : }
11484 : 5576 : break;
11485 : :
11486 : 61230 : case MK_as_base:
11487 : 61230 : gcc_checking_assert
11488 : : (decl == TYPE_NAME (CLASSTYPE_AS_BASE (TREE_TYPE (container))));
11489 : : break;
11490 : :
11491 : 12486 : case MK_local_friend:
11492 : 12486 : {
11493 : : /* Find by index on the class's DECL_LIST */
11494 : 12486 : unsigned ix = 0;
11495 : 12486 : for (tree decls = CLASSTYPE_DECL_LIST (TREE_CHAIN (decl));
11496 : 343011 : decls; decls = TREE_CHAIN (decls))
11497 : 343011 : if (!TREE_PURPOSE (decls))
11498 : : {
11499 : 47823 : tree frnd = friend_from_decl_list (TREE_VALUE (decls));
11500 : 47823 : if (frnd == decl)
11501 : : break;
11502 : 35337 : ix++;
11503 : : }
11504 : 12486 : key.index = ix;
11505 : 12486 : name = NULL_TREE;
11506 : : }
11507 : 12486 : break;
11508 : :
11509 : 9447 : case MK_local_type:
11510 : 9447 : key_local_type (key, STRIP_TEMPLATE (decl), container);
11511 : 9447 : break;
11512 : :
11513 : 1065 : case MK_enum:
11514 : 1065 : {
11515 : : /* Anonymous enums are located by their first identifier,
11516 : : and underlying type. */
11517 : 1065 : tree type = TREE_TYPE (decl);
11518 : :
11519 : 1065 : gcc_checking_assert (UNSCOPED_ENUM_P (type));
11520 : : /* Using the type name drops the bit precision we might
11521 : : have been using on the enum. */
11522 : 1065 : key.ret = TYPE_NAME (ENUM_UNDERLYING_TYPE (type));
11523 : 1065 : if (tree values = TYPE_VALUES (type))
11524 : 1065 : name = DECL_NAME (TREE_VALUE (values));
11525 : : }
11526 : : break;
11527 : :
11528 : 214 : case MK_keyed:
11529 : 214 : {
11530 : 428 : gcc_checking_assert (LAMBDA_TYPE_P (TREE_TYPE (inner)));
11531 : 214 : tree scope = LAMBDA_TYPE_EXTRA_SCOPE (TREE_TYPE (inner));
11532 : 214 : gcc_checking_assert (TREE_CODE (scope) == VAR_DECL
11533 : : || TREE_CODE (scope) == FIELD_DECL
11534 : : || TREE_CODE (scope) == PARM_DECL
11535 : : || TREE_CODE (scope) == TYPE_DECL
11536 : : || TREE_CODE (scope) == CONCEPT_DECL);
11537 : : /* Lambdas attached to fields are keyed to the class. */
11538 : 214 : if (TREE_CODE (scope) == FIELD_DECL)
11539 : 9 : scope = TYPE_NAME (DECL_CONTEXT (scope));
11540 : 214 : auto *root = keyed_table->get (scope);
11541 : 214 : unsigned ix = root->length ();
11542 : : /* If we don't find it, we'll write a really big number
11543 : : that the reader will ignore. */
11544 : 232 : while (ix--)
11545 : 232 : if ((*root)[ix] == inner)
11546 : : break;
11547 : :
11548 : : /* Use the keyed-to decl as the 'name'. */
11549 : 214 : name = scope;
11550 : 214 : key.index = ix;
11551 : : }
11552 : 214 : break;
11553 : :
11554 : 45762 : case MK_partial:
11555 : 45762 : {
11556 : 45762 : tree ti = get_template_info (inner);
11557 : 45762 : key.constraints = get_constraints (inner);
11558 : 45762 : key.ret = TI_TEMPLATE (ti);
11559 : 45762 : key.args = TI_ARGS (ti);
11560 : : }
11561 : 45762 : break;
11562 : : }
11563 : :
11564 : 1544173 : tree_node (name);
11565 : 1544173 : if (streaming_p ())
11566 : : {
11567 : 548039 : unsigned code = (key.ref_q << 0) | (key.index << 2);
11568 : 548039 : u (code);
11569 : : }
11570 : :
11571 : 1544173 : if (mk == MK_enum)
11572 : 1065 : tree_node (key.ret);
11573 : 1543108 : else if (mk == MK_partial
11574 : 1497346 : || (mk == MK_named && inner
11575 : 1293789 : && TREE_CODE (inner) == FUNCTION_DECL))
11576 : : {
11577 : 737774 : tree_node (key.ret);
11578 : 737774 : tree arg = key.args;
11579 : 737774 : if (mk == MK_named)
11580 : 2020450 : while (arg && arg != void_list_node)
11581 : : {
11582 : 1328438 : tree_node (TREE_VALUE (arg));
11583 : 1328438 : arg = TREE_CHAIN (arg);
11584 : : }
11585 : 737774 : tree_node (arg);
11586 : 737774 : tree_node (key.constraints);
11587 : : }
11588 : : }
11589 : 2760273 : }
11590 : :
11591 : : /* DECL is a new declaration that may be duplicated in OVL. Use RET &
11592 : : ARGS to find its clone, or NULL. If DECL's DECL_NAME is NULL, this
11593 : : has been found by a proxy. It will be an enum type located by its
11594 : : first member.
11595 : :
11596 : : We're conservative with matches, so ambiguous decls will be
11597 : : registered as different, then lead to a lookup error if the two
11598 : : modules are both visible. Perhaps we want to do something similar
11599 : : to duplicate decls to get ODR errors on loading? We already have
11600 : : some special casing for namespaces. */
11601 : :
11602 : : static tree
11603 : 252408 : check_mergeable_decl (merge_kind mk, tree decl, tree ovl, merge_key const &key)
11604 : : {
11605 : 252408 : tree found = NULL_TREE;
11606 : 1017230 : for (ovl_iterator iter (ovl); !found && iter; ++iter)
11607 : : {
11608 : 492788 : tree match = *iter;
11609 : :
11610 : 492788 : tree d_inner = decl;
11611 : 492788 : tree m_inner = match;
11612 : :
11613 : 573117 : again:
11614 : 573117 : if (TREE_CODE (d_inner) != TREE_CODE (m_inner))
11615 : : {
11616 : 73348 : if (TREE_CODE (match) == NAMESPACE_DECL
11617 : 73348 : && !DECL_NAMESPACE_ALIAS (match))
11618 : : /* Namespaces are never overloaded. */
11619 : : found = match;
11620 : :
11621 : 73348 : continue;
11622 : : }
11623 : :
11624 : 499769 : switch (TREE_CODE (d_inner))
11625 : : {
11626 : 164217 : case TEMPLATE_DECL:
11627 : 164217 : if (template_heads_equivalent_p (d_inner, m_inner))
11628 : : {
11629 : 80329 : d_inner = DECL_TEMPLATE_RESULT (d_inner);
11630 : 80329 : m_inner = DECL_TEMPLATE_RESULT (m_inner);
11631 : 80329 : if (d_inner == error_mark_node
11632 : 80329 : && TYPE_DECL_ALIAS_P (m_inner))
11633 : : {
11634 : : found = match;
11635 : : break;
11636 : : }
11637 : 80329 : goto again;
11638 : : }
11639 : : break;
11640 : :
11641 : 245125 : case FUNCTION_DECL:
11642 : 245125 : if (tree m_type = TREE_TYPE (m_inner))
11643 : 245125 : if ((!key.ret
11644 : 128199 : || same_type_p (key.ret, fndecl_declared_return_type (m_inner)))
11645 : 228589 : && type_memfn_rqual (m_type) == key.ref_q
11646 : 228337 : && compparms (key.args, TYPE_ARG_TYPES (m_type))
11647 : : /* Reject if old is a "C" builtin and new is not "C".
11648 : : Matches decls_match behaviour. */
11649 : 119392 : && (!DECL_IS_UNDECLARED_BUILTIN (m_inner)
11650 : 5039 : || !DECL_EXTERN_C_P (m_inner)
11651 : 4913 : || DECL_EXTERN_C_P (d_inner))
11652 : : /* Reject if one is a different member of a
11653 : : guarded/pre/post fn set. */
11654 : 119387 : && (!flag_contracts
11655 : 346 : || (DECL_IS_PRE_FN_P (d_inner)
11656 : 173 : == DECL_IS_PRE_FN_P (m_inner)))
11657 : 364512 : && (!flag_contracts
11658 : 346 : || (DECL_IS_POST_FN_P (d_inner)
11659 : 173 : == DECL_IS_POST_FN_P (m_inner))))
11660 : : {
11661 : 119387 : tree m_reqs = get_constraints (m_inner);
11662 : 119387 : if (m_reqs)
11663 : : {
11664 : 7790 : if (cxx_dialect < cxx20)
11665 : 8 : m_reqs = CI_ASSOCIATED_CONSTRAINTS (m_reqs);
11666 : : else
11667 : 15572 : m_reqs = CI_DECLARATOR_REQS (m_reqs);
11668 : : }
11669 : :
11670 : 119387 : if (cp_tree_equal (key.constraints, m_reqs))
11671 : 151422 : found = match;
11672 : : }
11673 : : break;
11674 : :
11675 : 58347 : case TYPE_DECL:
11676 : 116694 : if (DECL_IMPLICIT_TYPEDEF_P (d_inner)
11677 : 58347 : == DECL_IMPLICIT_TYPEDEF_P (m_inner))
11678 : : {
11679 : 58321 : if (!IDENTIFIER_ANON_P (DECL_NAME (m_inner)))
11680 : 58181 : return match;
11681 : 140 : else if (mk == MK_enum
11682 : 140 : && (TYPE_NAME (ENUM_UNDERLYING_TYPE (TREE_TYPE (m_inner)))
11683 : 140 : == key.ret))
11684 : : found = match;
11685 : : }
11686 : : break;
11687 : :
11688 : : default:
11689 : : found = match;
11690 : : break;
11691 : : }
11692 : : }
11693 : :
11694 : 194227 : return found;
11695 : : }
11696 : :
11697 : : /* DECL, INNER & TYPE are a skeleton set of nodes for a decl. Only
11698 : : the bools have been filled in. Read its merging key and merge it.
11699 : : Returns the existing decl if there is one. */
11700 : :
11701 : : tree
11702 : 817691 : trees_in::key_mergeable (int tag, merge_kind mk, tree decl, tree inner,
11703 : : tree type, tree container, bool is_attached,
11704 : : bool is_imported_temploid_friend)
11705 : : {
11706 : 817691 : const char *kind = "new";
11707 : 817691 : tree existing = NULL_TREE;
11708 : :
11709 : 817691 : if (mk & MK_template_mask)
11710 : : {
11711 : : // FIXME: We could stream the specialization hash?
11712 : 254642 : spec_entry spec;
11713 : 254642 : spec.tmpl = tree_node ();
11714 : 254642 : spec.args = tree_node ();
11715 : :
11716 : 254642 : if (get_overrun ())
11717 : 0 : return error_mark_node;
11718 : :
11719 : 254642 : DECL_NAME (decl) = DECL_NAME (spec.tmpl);
11720 : 254642 : DECL_CONTEXT (decl) = DECL_CONTEXT (spec.tmpl);
11721 : 254642 : DECL_NAME (inner) = DECL_NAME (decl);
11722 : 254642 : DECL_CONTEXT (inner) = DECL_CONTEXT (decl);
11723 : :
11724 : 254642 : tree constr = NULL_TREE;
11725 : 254642 : bool is_decl = mk & MK_tmpl_decl_mask;
11726 : 254642 : if (is_decl)
11727 : : {
11728 : 180656 : if (flag_concepts && TREE_CODE (inner) == VAR_DECL)
11729 : : {
11730 : 3975 : constr = tree_node ();
11731 : 3975 : if (constr)
11732 : 0 : set_constraints (inner, constr);
11733 : : }
11734 : 359674 : spec.spec = (mk & MK_tmpl_tmpl_mask) ? inner : decl;
11735 : : }
11736 : : else
11737 : 73986 : spec.spec = type;
11738 : 254642 : existing = match_mergeable_specialization (is_decl, &spec);
11739 : 254642 : if (constr)
11740 : : /* We'll add these back later, if this is the new decl. */
11741 : 0 : remove_constraints (inner);
11742 : :
11743 : 254642 : if (!existing)
11744 : : ; /* We'll add to the table once read. */
11745 : 140589 : else if (mk & MK_tmpl_decl_mask)
11746 : : {
11747 : : /* A declaration specialization. */
11748 : 97179 : if (mk & MK_tmpl_tmpl_mask)
11749 : 995 : existing = DECL_TI_TEMPLATE (existing);
11750 : : }
11751 : : else
11752 : : {
11753 : : /* A type specialization. */
11754 : 43410 : if (mk & MK_tmpl_tmpl_mask)
11755 : 225 : existing = CLASSTYPE_TI_TEMPLATE (existing);
11756 : : else
11757 : 43185 : existing = TYPE_NAME (existing);
11758 : : }
11759 : : }
11760 : 563049 : else if (mk == MK_unique)
11761 : : kind = "unique";
11762 : : else
11763 : : {
11764 : 433838 : tree name = tree_node ();
11765 : :
11766 : 433838 : merge_key key;
11767 : 433838 : unsigned code = u ();
11768 : 433838 : key.ref_q = cp_ref_qualifier ((code >> 0) & 3);
11769 : 433838 : key.index = code >> 2;
11770 : :
11771 : 433838 : if (mk == MK_enum)
11772 : 239 : key.ret = tree_node ();
11773 : 433599 : else if (mk == MK_partial
11774 : 424268 : || ((mk == MK_named || mk == MK_friend_spec)
11775 : 360923 : && TREE_CODE (inner) == FUNCTION_DECL))
11776 : : {
11777 : 206733 : key.ret = tree_node ();
11778 : 206733 : tree arg, *arg_ptr = &key.args;
11779 : 206733 : while ((arg = tree_node ())
11780 : 581787 : && arg != void_list_node
11781 : 968912 : && mk != MK_partial)
11782 : : {
11783 : 376424 : *arg_ptr = tree_cons (NULL_TREE, arg, NULL_TREE);
11784 : 376424 : arg_ptr = &TREE_CHAIN (*arg_ptr);
11785 : : }
11786 : 206733 : *arg_ptr = arg;
11787 : 206733 : key.constraints = tree_node ();
11788 : : }
11789 : :
11790 : 433838 : if (get_overrun ())
11791 : 0 : return error_mark_node;
11792 : :
11793 : 433838 : if (mk < MK_indirect_lwm)
11794 : : {
11795 : 426777 : DECL_NAME (decl) = name;
11796 : 426777 : DECL_CONTEXT (decl) = FROB_CONTEXT (container);
11797 : : }
11798 : 433838 : DECL_NAME (inner) = DECL_NAME (decl);
11799 : 433838 : DECL_CONTEXT (inner) = DECL_CONTEXT (decl);
11800 : :
11801 : 433838 : if (mk == MK_partial)
11802 : : {
11803 : 9331 : for (tree spec = DECL_TEMPLATE_SPECIALIZATIONS (key.ret);
11804 : 48253 : spec; spec = TREE_CHAIN (spec))
11805 : : {
11806 : 44932 : tree tmpl = TREE_VALUE (spec);
11807 : 44932 : tree ti = get_template_info (tmpl);
11808 : 44932 : if (template_args_equal (key.args, TI_ARGS (ti))
11809 : 51534 : && cp_tree_equal (key.constraints,
11810 : : get_constraints
11811 : 6602 : (DECL_TEMPLATE_RESULT (tmpl))))
11812 : : {
11813 : : existing = tmpl;
11814 : : break;
11815 : : }
11816 : : }
11817 : : }
11818 : 424507 : else if (mk == MK_keyed
11819 : 103 : && DECL_LANG_SPECIFIC (name)
11820 : 424610 : && DECL_MODULE_KEYED_DECLS_P (name))
11821 : : {
11822 : 103 : gcc_checking_assert (TREE_CODE (container) == NAMESPACE_DECL
11823 : : || TREE_CODE (container) == TYPE_DECL);
11824 : 103 : if (auto *set = keyed_table->get (name))
11825 : 433895 : if (key.index < set->length ())
11826 : : {
11827 : 57 : existing = (*set)[key.index];
11828 : 57 : if (existing)
11829 : : {
11830 : 57 : gcc_checking_assert
11831 : : (DECL_IMPLICIT_TYPEDEF_P (existing));
11832 : 57 : if (inner != decl)
11833 : 23 : existing
11834 : 23 : = CLASSTYPE_TI_TEMPLATE (TREE_TYPE (existing));
11835 : : }
11836 : : }
11837 : : }
11838 : : else
11839 : 424404 : switch (TREE_CODE (container))
11840 : : {
11841 : 0 : default:
11842 : 0 : gcc_unreachable ();
11843 : :
11844 : 114744 : case NAMESPACE_DECL:
11845 : 114744 : if (is_attached
11846 : 114744 : && !is_imported_temploid_friend
11847 : 114744 : && !(state->is_module () || state->is_partition ()))
11848 : : kind = "unique";
11849 : : else
11850 : : {
11851 : 113132 : gcc_checking_assert (mk == MK_named || mk == MK_enum);
11852 : 113132 : tree mvec;
11853 : 113132 : tree *vslot = mergeable_namespace_slots (container, name,
11854 : : is_attached, &mvec);
11855 : 113132 : existing = check_mergeable_decl (mk, decl, *vslot, key);
11856 : 113132 : if (!existing)
11857 : 42384 : add_mergeable_namespace_entity (vslot, decl);
11858 : : else
11859 : : {
11860 : : /* Note that we now have duplicates to deal with in
11861 : : name lookup. */
11862 : 70748 : if (is_attached)
11863 : 33 : BINDING_VECTOR_PARTITION_DUPS_P (mvec) = true;
11864 : : else
11865 : 70715 : BINDING_VECTOR_GLOBAL_DUPS_P (mvec) = true;
11866 : : }
11867 : : }
11868 : : break;
11869 : :
11870 : 2749 : case FUNCTION_DECL:
11871 : 2749 : gcc_checking_assert (mk == MK_local_type);
11872 : 2749 : existing = key_local_type (key, container, name);
11873 : 2749 : if (existing && inner != decl)
11874 : 3092 : existing = TYPE_TI_TEMPLATE (TREE_TYPE (existing));
11875 : : break;
11876 : :
11877 : 306911 : case TYPE_DECL:
11878 : 306911 : gcc_checking_assert (!is_imported_temploid_friend);
11879 : 3862 : if (is_attached && !(state->is_module () || state->is_partition ())
11880 : : /* Implicit member functions can come from
11881 : : anywhere. */
11882 : 309898 : && !(DECL_ARTIFICIAL (decl)
11883 : 1746 : && TREE_CODE (decl) == FUNCTION_DECL
11884 : 559 : && !DECL_THUNK_P (decl)))
11885 : : kind = "unique";
11886 : : else
11887 : : {
11888 : 304483 : tree ctx = TREE_TYPE (container);
11889 : :
11890 : : /* For some reason templated enumeral types are not marked
11891 : : as COMPLETE_TYPE_P, even though they have members.
11892 : : This may well be a bug elsewhere. */
11893 : 304483 : if (TREE_CODE (ctx) == ENUMERAL_TYPE)
11894 : 10004 : existing = find_enum_member (ctx, name);
11895 : 294479 : else if (COMPLETE_TYPE_P (ctx))
11896 : : {
11897 : 174681 : switch (mk)
11898 : : {
11899 : 0 : default:
11900 : 0 : gcc_unreachable ();
11901 : :
11902 : 139603 : case MK_named:
11903 : 139603 : existing = lookup_class_binding (ctx, name);
11904 : 139603 : if (existing)
11905 : : {
11906 : 139276 : tree inner = decl;
11907 : 139276 : if (TREE_CODE (inner) == TEMPLATE_DECL
11908 : 139276 : && !DECL_MEMBER_TEMPLATE_P (inner))
11909 : 65490 : inner = DECL_TEMPLATE_RESULT (inner);
11910 : :
11911 : 139276 : existing = check_mergeable_decl
11912 : 139276 : (mk, inner, existing, key);
11913 : :
11914 : 139276 : if (!existing && DECL_ALIAS_TEMPLATE_P (decl))
11915 : : {} // FIXME: Insert into specialization
11916 : : // tables, we'll need the arguments for that!
11917 : : }
11918 : : break;
11919 : :
11920 : 23847 : case MK_field:
11921 : 23847 : {
11922 : 23847 : unsigned ix = key.index;
11923 : 23847 : for (tree field = TYPE_FIELDS (ctx);
11924 : 67732 : field; field = DECL_CHAIN (field))
11925 : : {
11926 : 67732 : tree finner = STRIP_TEMPLATE (field);
11927 : 67732 : if (TREE_CODE (finner) == TREE_CODE (inner))
11928 : 31010 : if (!ix--)
11929 : : {
11930 : : existing = field;
11931 : : break;
11932 : : }
11933 : : }
11934 : : }
11935 : : break;
11936 : :
11937 : 1384 : case MK_vtable:
11938 : 1384 : {
11939 : 1384 : unsigned ix = key.index;
11940 : 1384 : for (tree vtable = CLASSTYPE_VTABLES (ctx);
11941 : 1761 : vtable; vtable = DECL_CHAIN (vtable))
11942 : 1596 : if (!ix--)
11943 : : {
11944 : : existing = vtable;
11945 : : break;
11946 : : }
11947 : : }
11948 : : break;
11949 : :
11950 : 7382 : case MK_as_base:
11951 : 7382 : {
11952 : 7382 : tree as_base = CLASSTYPE_AS_BASE (ctx);
11953 : 7382 : if (as_base && as_base != ctx)
11954 : 7382 : existing = TYPE_NAME (as_base);
11955 : : }
11956 : : break;
11957 : :
11958 : 2465 : case MK_local_friend:
11959 : 2465 : {
11960 : 2465 : unsigned ix = key.index;
11961 : 2465 : for (tree decls = CLASSTYPE_DECL_LIST (ctx);
11962 : 68788 : decls; decls = TREE_CHAIN (decls))
11963 : 68788 : if (!TREE_PURPOSE (decls) && !ix--)
11964 : : {
11965 : 2465 : existing
11966 : 2465 : = friend_from_decl_list (TREE_VALUE (decls));
11967 : 2465 : break;
11968 : : }
11969 : : }
11970 : : break;
11971 : : }
11972 : :
11973 : 174681 : if (existing && mk < MK_indirect_lwm && mk != MK_partial
11974 : 171303 : && TREE_CODE (decl) == TEMPLATE_DECL
11975 : 257381 : && !DECL_MEMBER_TEMPLATE_P (decl))
11976 : : {
11977 : 67215 : tree ti;
11978 : 67215 : if (DECL_IMPLICIT_TYPEDEF_P (existing))
11979 : 841 : ti = TYPE_TEMPLATE_INFO (TREE_TYPE (existing));
11980 : : else
11981 : 66374 : ti = DECL_TEMPLATE_INFO (existing);
11982 : 67215 : existing = TI_TEMPLATE (ti);
11983 : : }
11984 : : }
11985 : : }
11986 : : }
11987 : : }
11988 : :
11989 : 817691 : dump (dumper::MERGE)
11990 : 2380 : && dump ("Read:%d's %s merge key (%s) %C:%N", tag, merge_kind_name[mk],
11991 : 2380 : existing ? "matched" : kind, TREE_CODE (decl), decl);
11992 : :
11993 : : return existing;
11994 : : }
11995 : :
11996 : : void
11997 : 240275 : trees_out::binfo_mergeable (tree binfo)
11998 : : {
11999 : 240275 : tree dom = binfo;
12000 : 307527 : while (tree parent = BINFO_INHERITANCE_CHAIN (dom))
12001 : : dom = parent;
12002 : 240275 : tree type = BINFO_TYPE (dom);
12003 : 240275 : gcc_checking_assert (TYPE_BINFO (type) == dom);
12004 : 240275 : tree_node (type);
12005 : 240275 : if (streaming_p ())
12006 : : {
12007 : : unsigned ix = 0;
12008 : 134126 : for (; dom != binfo; dom = TREE_CHAIN (dom))
12009 : 34968 : ix++;
12010 : 99158 : u (ix);
12011 : : }
12012 : 240275 : }
12013 : :
12014 : : unsigned
12015 : 71371 : trees_in::binfo_mergeable (tree *type)
12016 : : {
12017 : 71371 : *type = tree_node ();
12018 : 71371 : return u ();
12019 : : }
12020 : :
12021 : : /* DECL is a just streamed declaration with attributes DATTR that should
12022 : : have matching ABI tags as EXISTING's attributes EATTR. Check that the
12023 : : ABI tags match, and report an error if not. */
12024 : :
12025 : : void
12026 : 270902 : trees_in::check_abi_tags (tree existing, tree decl, tree &eattr, tree &dattr)
12027 : : {
12028 : 270902 : tree etags = lookup_attribute ("abi_tag", eattr);
12029 : 270902 : tree dtags = lookup_attribute ("abi_tag", dattr);
12030 : 270902 : if ((etags == nullptr) != (dtags == nullptr)
12031 : 270902 : || (etags && !attribute_value_equal (etags, dtags)))
12032 : : {
12033 : 30 : if (etags)
12034 : 21 : etags = TREE_VALUE (etags);
12035 : 30 : if (dtags)
12036 : 24 : dtags = TREE_VALUE (dtags);
12037 : :
12038 : : /* We only error if mangling wouldn't consider the tags equivalent. */
12039 : 30 : if (!equal_abi_tags (etags, dtags))
12040 : : {
12041 : 21 : auto_diagnostic_group d;
12042 : 21 : if (dtags)
12043 : 15 : error_at (DECL_SOURCE_LOCATION (decl),
12044 : : "mismatching abi tags for %qD with tags %qE",
12045 : : decl, dtags);
12046 : : else
12047 : 6 : error_at (DECL_SOURCE_LOCATION (decl),
12048 : : "mismatching abi tags for %qD with no tags", decl);
12049 : 21 : if (etags)
12050 : 15 : inform (DECL_SOURCE_LOCATION (existing),
12051 : : "existing declaration here with tags %qE", etags);
12052 : : else
12053 : 6 : inform (DECL_SOURCE_LOCATION (existing),
12054 : : "existing declaration here with no tags");
12055 : 21 : }
12056 : :
12057 : : /* Always use the existing abi_tags as the canonical set so that
12058 : : later processing doesn't get confused. */
12059 : 30 : if (dtags)
12060 : 24 : dattr = remove_attribute ("abi_tag", dattr);
12061 : 30 : if (etags)
12062 : 21 : duplicate_one_attribute (&dattr, eattr, "abi_tag");
12063 : : }
12064 : 270902 : }
12065 : :
12066 : : /* DECL is a just streamed mergeable decl that should match EXISTING. Check
12067 : : it does and issue an appropriate diagnostic if not. Merge any
12068 : : bits from DECL to EXISTING. This is stricter matching than
12069 : : decls_match, because we can rely on ODR-sameness, and we cannot use
12070 : : decls_match because it can cause instantiations of constraints. */
12071 : :
12072 : : bool
12073 : 398806 : trees_in::is_matching_decl (tree existing, tree decl, bool is_typedef)
12074 : : {
12075 : : // FIXME: We should probably do some duplicate decl-like stuff here
12076 : : // (beware, default parms should be the same?) Can we just call
12077 : : // duplicate_decls and teach it how to handle the module-specific
12078 : : // permitted/required duplications?
12079 : :
12080 : : // We know at this point that the decls have matched by key, so we
12081 : : // can elide some of the checking
12082 : 398806 : gcc_checking_assert (TREE_CODE (existing) == TREE_CODE (decl));
12083 : :
12084 : 398806 : tree d_inner = decl;
12085 : 398806 : tree e_inner = existing;
12086 : 398806 : if (TREE_CODE (decl) == TEMPLATE_DECL)
12087 : : {
12088 : 135780 : d_inner = DECL_TEMPLATE_RESULT (d_inner);
12089 : 135780 : e_inner = DECL_TEMPLATE_RESULT (e_inner);
12090 : 135780 : gcc_checking_assert (TREE_CODE (e_inner) == TREE_CODE (d_inner));
12091 : : }
12092 : :
12093 : : // FIXME: do more precise errors at point of mismatch
12094 : 398806 : const char *mismatch_msg = nullptr;
12095 : 398806 : if (TREE_CODE (d_inner) == FUNCTION_DECL)
12096 : : {
12097 : 181932 : tree e_ret = fndecl_declared_return_type (existing);
12098 : 181932 : tree d_ret = fndecl_declared_return_type (decl);
12099 : :
12100 : 78635 : if (decl != d_inner && DECL_NAME (d_inner) == fun_identifier
12101 : 181948 : && LAMBDA_TYPE_P (DECL_CONTEXT (d_inner)))
12102 : : /* This has a recursive type that will compare different. */;
12103 : 181924 : else if (!same_type_p (d_ret, e_ret))
12104 : : {
12105 : 5 : mismatch_msg = G_("conflicting type for imported declaration %#qD");
12106 : 5 : goto mismatch;
12107 : : }
12108 : :
12109 : 181927 : tree e_type = TREE_TYPE (e_inner);
12110 : 181927 : tree d_type = TREE_TYPE (d_inner);
12111 : :
12112 : 181927 : if (DECL_EXTERN_C_P (d_inner) != DECL_EXTERN_C_P (e_inner))
12113 : : {
12114 : 0 : mismatch_msg = G_("conflicting language linkage for imported "
12115 : : "declaration %#qD");
12116 : 0 : goto mismatch;
12117 : : }
12118 : :
12119 : 181927 : for (tree e_args = TYPE_ARG_TYPES (e_type),
12120 : 181927 : d_args = TYPE_ARG_TYPES (d_type);
12121 : 311072 : e_args != d_args && (e_args || d_args);
12122 : 129145 : e_args = TREE_CHAIN (e_args), d_args = TREE_CHAIN (d_args))
12123 : : {
12124 : 129145 : if (!(e_args && d_args))
12125 : : {
12126 : 0 : mismatch_msg = G_("conflicting argument list for imported "
12127 : : "declaration %#qD");
12128 : 0 : goto mismatch;
12129 : : }
12130 : :
12131 : 129145 : if (!same_type_p (TREE_VALUE (d_args), TREE_VALUE (e_args)))
12132 : : {
12133 : 0 : mismatch_msg = G_("conflicting argument types for imported "
12134 : : "declaration %#qD");
12135 : 0 : goto mismatch;
12136 : : }
12137 : : }
12138 : :
12139 : : /* If EXISTING has an undeduced or uninstantiated exception
12140 : : specification, but DECL does not, propagate the exception
12141 : : specification. Otherwise we end up asserting or trying to
12142 : : instantiate it in the middle of loading. */
12143 : 181927 : tree e_spec = TYPE_RAISES_EXCEPTIONS (e_type);
12144 : 181927 : tree d_spec = TYPE_RAISES_EXCEPTIONS (d_type);
12145 : 266479 : if (DECL_MAYBE_DELETED (e_inner) || DEFERRED_NOEXCEPT_SPEC_P (e_spec))
12146 : : {
12147 : 14529 : if (!DEFERRED_NOEXCEPT_SPEC_P (d_spec)
12148 : 43208 : || (UNEVALUATED_NOEXCEPT_SPEC_P (e_spec)
12149 : 11833 : && !UNEVALUATED_NOEXCEPT_SPEC_P (d_spec)))
12150 : : {
12151 : 254 : dump (dumper::MERGE)
12152 : 3 : && dump ("Propagating instantiated noexcept to %N", existing);
12153 : 254 : TREE_TYPE (existing) = d_type;
12154 : :
12155 : : /* Propagate to existing clones. */
12156 : 254 : tree clone;
12157 : 542 : FOR_EACH_CLONE (clone, existing)
12158 : : {
12159 : 288 : if (TREE_TYPE (clone) == e_type)
12160 : 288 : TREE_TYPE (clone) = d_type;
12161 : : else
12162 : 0 : TREE_TYPE (clone)
12163 : 0 : = build_exception_variant (TREE_TYPE (clone), d_spec);
12164 : : }
12165 : : }
12166 : : }
12167 : 167355 : else if (!DECL_MAYBE_DELETED (d_inner)
12168 : 238157 : && !DEFERRED_NOEXCEPT_SPEC_P (d_spec)
12169 : 334709 : && !comp_except_specs (d_spec, e_spec, ce_type))
12170 : : {
12171 : 761 : mismatch_msg = G_("conflicting %<noexcept%> specifier for "
12172 : : "imported declaration %#qD");
12173 : 761 : goto mismatch;
12174 : : }
12175 : :
12176 : : /* Similarly if EXISTING has an undeduced return type, but DECL's
12177 : : is already deduced. */
12178 : 181166 : if (undeduced_auto_decl (existing) && !undeduced_auto_decl (decl))
12179 : : {
12180 : 13 : dump (dumper::MERGE)
12181 : 0 : && dump ("Propagating deduced return type to %N", existing);
12182 : 13 : FNDECL_USED_AUTO (e_inner) = true;
12183 : 13 : DECL_SAVED_AUTO_RETURN_TYPE (existing) = TREE_TYPE (e_type);
12184 : 13 : TREE_TYPE (existing) = change_return_type (TREE_TYPE (d_type), e_type);
12185 : : }
12186 : 181153 : else if (type_uses_auto (d_ret)
12187 : 181153 : && !same_type_p (TREE_TYPE (d_type), TREE_TYPE (e_type)))
12188 : : {
12189 : 3 : mismatch_msg = G_("conflicting deduced return type for "
12190 : : "imported declaration %#qD");
12191 : 3 : goto mismatch;
12192 : : }
12193 : :
12194 : : /* Similarly if EXISTING has undeduced constexpr, but DECL's
12195 : : is already deduced. */
12196 : 181266 : if (DECL_MAYBE_DELETED (e_inner) && !DECL_MAYBE_DELETED (d_inner)
12197 : 181164 : && DECL_DECLARED_CONSTEXPR_P (d_inner))
12198 : 1 : DECL_DECLARED_CONSTEXPR_P (e_inner) = true;
12199 : 181162 : else if (!DECL_MAYBE_DELETED (e_inner) && DECL_MAYBE_DELETED (d_inner))
12200 : : /* Nothing to do. */;
12201 : 181161 : else if (DECL_DECLARED_CONSTEXPR_P (e_inner)
12202 : 181161 : != DECL_DECLARED_CONSTEXPR_P (d_inner))
12203 : : {
12204 : 0 : mismatch_msg = G_("conflicting %<constexpr%> for imported "
12205 : : "declaration %#qD");
12206 : 0 : goto mismatch;
12207 : : }
12208 : :
12209 : : /* Don't synthesize a defaulted function if we're importing one
12210 : : we've already determined. */
12211 : 181163 : if (!DECL_MAYBE_DELETED (d_inner))
12212 : 181060 : DECL_MAYBE_DELETED (e_inner) = false;
12213 : : }
12214 : 216874 : else if (is_typedef)
12215 : : {
12216 : 80632 : if (!DECL_ORIGINAL_TYPE (e_inner)
12217 : 80632 : || !same_type_p (DECL_ORIGINAL_TYPE (d_inner),
12218 : : DECL_ORIGINAL_TYPE (e_inner)))
12219 : : {
12220 : 3 : mismatch_msg = G_("conflicting imported declaration %q#D");
12221 : 3 : goto mismatch;
12222 : : }
12223 : : }
12224 : : /* Using cp_tree_equal because we can meet TYPE_ARGUMENT_PACKs
12225 : : here. I suspect the entities that directly do that are things
12226 : : that shouldn't go to duplicate_decls (FIELD_DECLs etc). */
12227 : 136242 : else if (!cp_tree_equal (TREE_TYPE (decl), TREE_TYPE (existing)))
12228 : : {
12229 : : mismatch_msg = G_("conflicting type for imported declaration %#qD");
12230 : 1435 : mismatch:
12231 : 1435 : if (DECL_IS_UNDECLARED_BUILTIN (existing))
12232 : : /* Just like duplicate_decls, presum the user knows what
12233 : : they're doing in overriding a builtin. */
12234 : 763 : TREE_TYPE (existing) = TREE_TYPE (decl);
12235 : 672 : else if (decl_function_context (decl))
12236 : : /* The type of a mergeable local entity (such as a function scope
12237 : : capturing lambda's closure type fields) can depend on an
12238 : : unmergeable local entity (such as a local variable), so type
12239 : : equality isn't feasible in general for local entities. */;
12240 : : else
12241 : : {
12242 : 15 : gcc_checking_assert (mismatch_msg);
12243 : 15 : auto_diagnostic_group d;
12244 : 15 : error_at (DECL_SOURCE_LOCATION (decl), mismatch_msg, decl);
12245 : 15 : inform (DECL_SOURCE_LOCATION (existing),
12246 : : "existing declaration %#qD", existing);
12247 : 15 : return false;
12248 : 15 : }
12249 : : }
12250 : :
12251 : 398791 : if (DECL_IS_UNDECLARED_BUILTIN (existing)
12252 : 398791 : && !DECL_IS_UNDECLARED_BUILTIN (decl))
12253 : : {
12254 : : /* We're matching a builtin that the user has yet to declare.
12255 : : We are the one! This is very much duplicate-decl
12256 : : shenanigans. */
12257 : 946 : DECL_SOURCE_LOCATION (existing) = DECL_SOURCE_LOCATION (decl);
12258 : 946 : if (TREE_CODE (decl) != TYPE_DECL)
12259 : : {
12260 : : /* Propagate exceptions etc. */
12261 : 935 : TREE_TYPE (existing) = TREE_TYPE (decl);
12262 : 935 : TREE_NOTHROW (existing) = TREE_NOTHROW (decl);
12263 : : }
12264 : : /* This is actually an import! */
12265 : 946 : DECL_MODULE_IMPORT_P (existing) = true;
12266 : :
12267 : : /* Yay, sliced! */
12268 : 946 : existing->base = decl->base;
12269 : :
12270 : 946 : if (TREE_CODE (decl) == FUNCTION_DECL)
12271 : : {
12272 : : /* Ew :( */
12273 : 935 : memcpy (&existing->decl_common.size,
12274 : : &decl->decl_common.size,
12275 : : (offsetof (tree_decl_common, pt_uid)
12276 : : - offsetof (tree_decl_common, size)));
12277 : 935 : auto bltin_class = DECL_BUILT_IN_CLASS (decl);
12278 : 935 : existing->function_decl.built_in_class = bltin_class;
12279 : 935 : auto fncode = DECL_UNCHECKED_FUNCTION_CODE (decl);
12280 : 935 : DECL_UNCHECKED_FUNCTION_CODE (existing) = fncode;
12281 : 935 : if (existing->function_decl.built_in_class == BUILT_IN_NORMAL)
12282 : : {
12283 : 815 : if (builtin_decl_explicit_p (built_in_function (fncode)))
12284 : 815 : switch (fncode)
12285 : : {
12286 : 0 : case BUILT_IN_STPCPY:
12287 : 0 : set_builtin_decl_implicit_p
12288 : 0 : (built_in_function (fncode), true);
12289 : 0 : break;
12290 : 815 : default:
12291 : 815 : set_builtin_decl_declared_p
12292 : 815 : (built_in_function (fncode), true);
12293 : 815 : break;
12294 : : }
12295 : 815 : copy_attributes_to_builtin (decl);
12296 : : }
12297 : : }
12298 : : }
12299 : :
12300 : 398791 : if (VAR_OR_FUNCTION_DECL_P (decl)
12301 : 398791 : && DECL_TEMPLATE_INSTANTIATED (decl))
12302 : : /* Don't instantiate again! */
12303 : 7941 : DECL_TEMPLATE_INSTANTIATED (existing) = true;
12304 : :
12305 : 398791 : if (TREE_CODE (d_inner) == FUNCTION_DECL
12306 : 398791 : && DECL_DECLARED_INLINE_P (d_inner))
12307 : : {
12308 : 146656 : DECL_DECLARED_INLINE_P (e_inner) = true;
12309 : 146656 : if (!DECL_SAVED_TREE (e_inner)
12310 : 73181 : && lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (d_inner))
12311 : 146662 : && !lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (e_inner)))
12312 : : {
12313 : 18 : DECL_INTERFACE_KNOWN (e_inner)
12314 : 6 : |= DECL_INTERFACE_KNOWN (d_inner);
12315 : 6 : DECL_DISREGARD_INLINE_LIMITS (e_inner)
12316 : 6 : |= DECL_DISREGARD_INLINE_LIMITS (d_inner);
12317 : : // TODO: we will eventually want to merge all decl attributes
12318 : 6 : duplicate_one_attribute (&DECL_ATTRIBUTES (e_inner),
12319 : 6 : DECL_ATTRIBUTES (d_inner), "gnu_inline");
12320 : : }
12321 : : }
12322 : 398791 : if (!DECL_EXTERNAL (d_inner))
12323 : 185795 : DECL_EXTERNAL (e_inner) = false;
12324 : :
12325 : 398791 : if (VAR_OR_FUNCTION_DECL_P (d_inner))
12326 : 395632 : check_abi_tags (existing, decl,
12327 : 197816 : DECL_ATTRIBUTES (e_inner), DECL_ATTRIBUTES (d_inner));
12328 : :
12329 : 398791 : if (TREE_CODE (decl) == TEMPLATE_DECL)
12330 : : {
12331 : : /* Merge default template arguments. */
12332 : 135778 : tree d_parms = DECL_INNERMOST_TEMPLATE_PARMS (decl);
12333 : 135778 : tree e_parms = DECL_INNERMOST_TEMPLATE_PARMS (existing);
12334 : 135778 : gcc_checking_assert (TREE_VEC_LENGTH (d_parms)
12335 : : == TREE_VEC_LENGTH (e_parms));
12336 : 391854 : for (int i = 0; i < TREE_VEC_LENGTH (d_parms); ++i)
12337 : : {
12338 : 256085 : tree d_default = TREE_PURPOSE (TREE_VEC_ELT (d_parms, i));
12339 : 256085 : tree& e_default = TREE_PURPOSE (TREE_VEC_ELT (e_parms, i));
12340 : 256085 : if (e_default == NULL_TREE)
12341 : 216944 : e_default = d_default;
12342 : 39141 : else if (d_default != NULL_TREE
12343 : 39141 : && !cp_tree_equal (d_default, e_default))
12344 : : {
12345 : 9 : auto_diagnostic_group d;
12346 : 9 : tree d_parm = TREE_VALUE (TREE_VEC_ELT (d_parms, i));
12347 : 9 : tree e_parm = TREE_VALUE (TREE_VEC_ELT (e_parms, i));
12348 : 9 : error_at (DECL_SOURCE_LOCATION (d_parm),
12349 : : "conflicting default argument for %#qD", d_parm);
12350 : 9 : inform (DECL_SOURCE_LOCATION (e_parm),
12351 : : "existing default declared here");
12352 : 9 : return false;
12353 : 9 : }
12354 : : }
12355 : : }
12356 : :
12357 : 398782 : if (TREE_CODE (d_inner) == FUNCTION_DECL)
12358 : : {
12359 : : /* Merge default function arguments. */
12360 : 181923 : tree d_parm = FUNCTION_FIRST_USER_PARMTYPE (d_inner);
12361 : 181923 : tree e_parm = FUNCTION_FIRST_USER_PARMTYPE (e_inner);
12362 : 181923 : int i = 0;
12363 : 434800 : for (; d_parm && d_parm != void_list_node;
12364 : 252877 : d_parm = TREE_CHAIN (d_parm), e_parm = TREE_CHAIN (e_parm), ++i)
12365 : : {
12366 : 252889 : tree d_default = TREE_PURPOSE (d_parm);
12367 : 252889 : tree& e_default = TREE_PURPOSE (e_parm);
12368 : 252889 : if (e_default == NULL_TREE)
12369 : 238279 : e_default = d_default;
12370 : 14610 : else if (d_default != NULL_TREE
12371 : 14610 : && !cp_tree_equal (d_default, e_default))
12372 : : {
12373 : 12 : auto_diagnostic_group d;
12374 : 12 : error_at (get_fndecl_argument_location (d_inner, i),
12375 : : "conflicting default argument for parameter %P of %#qD",
12376 : : i, decl);
12377 : 12 : inform (get_fndecl_argument_location (e_inner, i),
12378 : : "existing default declared here");
12379 : 12 : return false;
12380 : 12 : }
12381 : : }
12382 : : }
12383 : :
12384 : : return true;
12385 : : }
12386 : :
12387 : : /* FN is an implicit member function that we've discovered is new to
12388 : : the class. Add it to the TYPE_FIELDS chain and the method vector.
12389 : : Reset the appropriate classtype lazy flag. */
12390 : :
12391 : : bool
12392 : 748 : trees_in::install_implicit_member (tree fn)
12393 : : {
12394 : 748 : tree ctx = DECL_CONTEXT (fn);
12395 : 748 : tree name = DECL_NAME (fn);
12396 : : /* We know these are synthesized, so the set of expected prototypes
12397 : : is quite restricted. We're not validating correctness, just
12398 : : distinguishing beteeen the small set of possibilities. */
12399 : 748 : tree parm_type = TREE_VALUE (FUNCTION_FIRST_USER_PARMTYPE (fn));
12400 : 748 : if (IDENTIFIER_CTOR_P (name))
12401 : : {
12402 : 507 : if (CLASSTYPE_LAZY_DEFAULT_CTOR (ctx)
12403 : 507 : && VOID_TYPE_P (parm_type))
12404 : 113 : CLASSTYPE_LAZY_DEFAULT_CTOR (ctx) = false;
12405 : 394 : else if (!TYPE_REF_P (parm_type))
12406 : : return false;
12407 : 394 : else if (CLASSTYPE_LAZY_COPY_CTOR (ctx)
12408 : 394 : && !TYPE_REF_IS_RVALUE (parm_type))
12409 : 206 : CLASSTYPE_LAZY_COPY_CTOR (ctx) = false;
12410 : 188 : else if (CLASSTYPE_LAZY_MOVE_CTOR (ctx))
12411 : 188 : CLASSTYPE_LAZY_MOVE_CTOR (ctx) = false;
12412 : : else
12413 : : return false;
12414 : : }
12415 : 241 : else if (IDENTIFIER_DTOR_P (name))
12416 : : {
12417 : 187 : if (CLASSTYPE_LAZY_DESTRUCTOR (ctx))
12418 : 187 : CLASSTYPE_LAZY_DESTRUCTOR (ctx) = false;
12419 : : else
12420 : : return false;
12421 : 187 : if (DECL_VIRTUAL_P (fn))
12422 : : /* A virtual dtor should have been created when the class
12423 : : became complete. */
12424 : : return false;
12425 : : }
12426 : 54 : else if (name == assign_op_identifier)
12427 : : {
12428 : 54 : if (!TYPE_REF_P (parm_type))
12429 : : return false;
12430 : 54 : else if (CLASSTYPE_LAZY_COPY_ASSIGN (ctx)
12431 : 54 : && !TYPE_REF_IS_RVALUE (parm_type))
12432 : 27 : CLASSTYPE_LAZY_COPY_ASSIGN (ctx) = false;
12433 : 27 : else if (CLASSTYPE_LAZY_MOVE_ASSIGN (ctx))
12434 : 27 : CLASSTYPE_LAZY_MOVE_ASSIGN (ctx) = false;
12435 : : else
12436 : : return false;
12437 : : }
12438 : : else
12439 : : return false;
12440 : :
12441 : 778 : dump (dumper::MERGE) && dump ("Adding implicit member %N", fn);
12442 : :
12443 : 748 : DECL_CHAIN (fn) = TYPE_FIELDS (ctx);
12444 : 748 : TYPE_FIELDS (ctx) = fn;
12445 : :
12446 : 748 : add_method (ctx, fn, false);
12447 : :
12448 : : /* Propagate TYPE_FIELDS. */
12449 : 748 : fixup_type_variants (ctx);
12450 : :
12451 : 748 : return true;
12452 : : }
12453 : :
12454 : : /* Return non-zero if DECL has a definition that would be interesting to
12455 : : write out. */
12456 : :
12457 : : static bool
12458 : 1165551 : has_definition (tree decl)
12459 : : {
12460 : 1165551 : bool is_tmpl = TREE_CODE (decl) == TEMPLATE_DECL;
12461 : 1165551 : if (is_tmpl)
12462 : 285012 : decl = DECL_TEMPLATE_RESULT (decl);
12463 : :
12464 : 1165551 : switch (TREE_CODE (decl))
12465 : : {
12466 : : default:
12467 : : break;
12468 : :
12469 : 422871 : case FUNCTION_DECL:
12470 : 422871 : if (!DECL_SAVED_TREE (decl))
12471 : : /* Not defined. */
12472 : : break;
12473 : :
12474 : 181241 : if (DECL_DECLARED_INLINE_P (decl))
12475 : : return true;
12476 : :
12477 : 11148 : if (header_module_p ())
12478 : : /* We always need to write definitions in header modules,
12479 : : since there's no TU to emit them in otherwise. */
12480 : : return true;
12481 : :
12482 : 3199 : if (DECL_TEMPLATE_INFO (decl))
12483 : : {
12484 : 2601 : int use_tpl = DECL_USE_TEMPLATE (decl);
12485 : :
12486 : : // FIXME: Partial specializations have definitions too.
12487 : 2601 : if (use_tpl < 2)
12488 : : return true;
12489 : : }
12490 : : break;
12491 : :
12492 : 511679 : case TYPE_DECL:
12493 : 511679 : {
12494 : 511679 : tree type = TREE_TYPE (decl);
12495 : 511679 : if (type == TYPE_MAIN_VARIANT (type)
12496 : 240173 : && decl == TYPE_NAME (type)
12497 : 751852 : && (TREE_CODE (type) == ENUMERAL_TYPE
12498 : 240173 : ? TYPE_VALUES (type) : TYPE_FIELDS (type)))
12499 : : return true;
12500 : : }
12501 : : break;
12502 : :
12503 : 51718 : case VAR_DECL:
12504 : : /* DECL_INITIALIZED_P might not be set on a dependent VAR_DECL. */
12505 : 51718 : if (DECL_LANG_SPECIFIC (decl)
12506 : 51644 : && DECL_TEMPLATE_INFO (decl)
12507 : 85749 : && DECL_INITIAL (decl))
12508 : : return true;
12509 : : else
12510 : : {
12511 : 21054 : if (!DECL_INITIALIZED_P (decl))
12512 : : /* Not defined. */
12513 : : return false;
12514 : :
12515 : 16473 : if (header_module_p ())
12516 : : /* We always need to write definitions in header modules,
12517 : : since there's no TU to emit them in otherwise. */
12518 : : return true;
12519 : :
12520 : 2696 : if (decl_maybe_constant_var_p (decl))
12521 : : /* We might need its constant value. */
12522 : : return true;
12523 : :
12524 : 317 : if (vague_linkage_p (decl))
12525 : : /* These are emitted as needed. */
12526 : : return true;
12527 : :
12528 : : return false;
12529 : : }
12530 : 5781 : break;
12531 : :
12532 : 5781 : case CONCEPT_DECL:
12533 : 5781 : if (DECL_INITIAL (decl))
12534 : : return true;
12535 : :
12536 : : break;
12537 : : }
12538 : :
12539 : : return false;
12540 : : }
12541 : :
12542 : : uintptr_t *
12543 : 459472 : trees_in::find_duplicate (tree existing)
12544 : : {
12545 : 228789 : if (!duplicates)
12546 : : return NULL;
12547 : :
12548 : 325597 : return duplicates->get (existing);
12549 : : }
12550 : :
12551 : : /* We're starting to read a duplicate DECL. EXISTING is the already
12552 : : known node. */
12553 : :
12554 : : void
12555 : 574850 : trees_in::register_duplicate (tree decl, tree existing)
12556 : : {
12557 : 574850 : if (!duplicates)
12558 : 94319 : duplicates = new duplicate_hash_map (40);
12559 : :
12560 : 574850 : bool existed;
12561 : 574850 : uintptr_t &slot = duplicates->get_or_insert (existing, &existed);
12562 : 574850 : gcc_checking_assert (!existed);
12563 : 574850 : slot = reinterpret_cast<uintptr_t> (decl);
12564 : :
12565 : 574850 : if (TREE_CODE (decl) == TEMPLATE_DECL)
12566 : : /* Also register the DECL_TEMPLATE_RESULT as a duplicate so
12567 : : that passing decl's _RESULT to maybe_duplicate naturally
12568 : : gives us existing's _RESULT back. */
12569 : 271560 : register_duplicate (DECL_TEMPLATE_RESULT (decl),
12570 : 135780 : DECL_TEMPLATE_RESULT (existing));
12571 : 574850 : }
12572 : :
12573 : : /* We've read a definition of MAYBE_EXISTING. If not a duplicate,
12574 : : return MAYBE_EXISTING (into which the definition should be
12575 : : installed). Otherwise return NULL if already known bad, or the
12576 : : duplicate we read (for ODR checking, or extracting additional merge
12577 : : information). */
12578 : :
12579 : : tree
12580 : 230683 : trees_in::odr_duplicate (tree maybe_existing, bool has_defn)
12581 : : {
12582 : 230683 : tree res = NULL_TREE;
12583 : :
12584 : 369799 : if (uintptr_t *dup = find_duplicate (maybe_existing))
12585 : : {
12586 : 132190 : if (!(*dup & 1))
12587 : 132187 : res = reinterpret_cast<tree> (*dup);
12588 : : }
12589 : : else
12590 : : res = maybe_existing;
12591 : :
12592 : 230683 : assert_definition (maybe_existing, res && !has_defn);
12593 : :
12594 : : // FIXME: We probably need to return the template, so that the
12595 : : // template header can be checked?
12596 : 230683 : return res ? STRIP_TEMPLATE (res) : NULL_TREE;
12597 : : }
12598 : :
12599 : : /* The following writer functions rely on the current behaviour of
12600 : : depset::hash::add_dependency making the decl and defn depset nodes
12601 : : depend on eachother. That way we don't have to worry about seeding
12602 : : the tree map with named decls that cannot be looked up by name (I.e
12603 : : template and function parms). We know the decl and definition will
12604 : : be in the same cluster, which is what we want. */
12605 : :
12606 : : void
12607 : 339459 : trees_out::write_function_def (tree decl)
12608 : : {
12609 : 339459 : tree_node (DECL_RESULT (decl));
12610 : :
12611 : 339459 : {
12612 : : /* The function body for a non-inline function or function template
12613 : : is ignored for determining exposures. This should only matter
12614 : : for templates (we don't emit the bodies of non-inline functions
12615 : : to begin with). */
12616 : 339459 : auto ovr = make_temp_override (dep_hash->ignore_tu_local,
12617 : 339459 : !DECL_DECLARED_INLINE_P (decl));
12618 : 339459 : tree_node (DECL_INITIAL (decl));
12619 : 339459 : tree_node (DECL_SAVED_TREE (decl));
12620 : 339459 : }
12621 : :
12622 : 724398 : tree_node (DECL_FRIEND_CONTEXT (decl));
12623 : :
12624 : 339459 : constexpr_fundef *cexpr = retrieve_constexpr_fundef (decl);
12625 : :
12626 : 339459 : if (streaming_p ())
12627 : 169698 : u (cexpr != nullptr);
12628 : 339459 : if (cexpr)
12629 : : {
12630 : 72760 : chained_decls (cexpr->parms);
12631 : 72760 : tree_node (cexpr->result);
12632 : 72760 : tree_node (cexpr->body);
12633 : : }
12634 : :
12635 : 339459 : function* f = DECL_STRUCT_FUNCTION (decl);
12636 : :
12637 : 339459 : if (streaming_p ())
12638 : : {
12639 : 169698 : unsigned flags = 0;
12640 : :
12641 : 169698 : flags |= 1 * DECL_NOT_REALLY_EXTERN (decl);
12642 : 169698 : if (f)
12643 : : {
12644 : 169339 : flags |= 2;
12645 : : /* These flags are needed in tsubst_lambda_expr. */
12646 : 169339 : flags |= 4 * f->language->returns_value;
12647 : 169339 : flags |= 8 * f->language->returns_null;
12648 : 169339 : flags |= 16 * f->language->returns_abnormally;
12649 : 169339 : flags |= 32 * f->language->infinite_loop;
12650 : : }
12651 : :
12652 : 169698 : u (flags);
12653 : : }
12654 : :
12655 : 339459 : if (state && f)
12656 : : {
12657 : 338741 : state->write_location (*this, f->function_start_locus);
12658 : 338741 : state->write_location (*this, f->function_end_locus);
12659 : : }
12660 : 339459 : }
12661 : :
12662 : : void
12663 : 0 : trees_out::mark_function_def (tree)
12664 : : {
12665 : 0 : }
12666 : :
12667 : : bool
12668 : 147213 : trees_in::read_function_def (tree decl, tree maybe_template)
12669 : : {
12670 : 147619 : dump () && dump ("Reading function definition %N", decl);
12671 : 147213 : tree result = tree_node ();
12672 : 147213 : tree initial = tree_node ();
12673 : 147213 : tree saved = tree_node ();
12674 : 147213 : tree context = tree_node ();
12675 : 147213 : constexpr_fundef cexpr;
12676 : 147213 : post_process_data pdata {};
12677 : 147213 : pdata.decl = maybe_template;
12678 : :
12679 : 147213 : tree maybe_dup = odr_duplicate (maybe_template, DECL_SAVED_TREE (decl));
12680 : 294423 : bool installing = maybe_dup && !DECL_SAVED_TREE (decl);
12681 : :
12682 : 147213 : if (u ())
12683 : : {
12684 : 31494 : cexpr.parms = chained_decls ();
12685 : 31494 : cexpr.result = tree_node ();
12686 : 31494 : cexpr.body = tree_node ();
12687 : 31494 : cexpr.decl = decl;
12688 : : }
12689 : : else
12690 : 115719 : cexpr.decl = NULL_TREE;
12691 : :
12692 : 147213 : unsigned flags = u ();
12693 : :
12694 : 147213 : if (flags & 2)
12695 : : {
12696 : 146904 : pdata.start_locus = state->read_location (*this);
12697 : 146904 : pdata.end_locus = state->read_location (*this);
12698 : 146904 : pdata.returns_value = flags & 4;
12699 : 146904 : pdata.returns_null = flags & 8;
12700 : 146904 : pdata.returns_abnormally = flags & 16;
12701 : 146904 : pdata.infinite_loop = flags & 32;
12702 : : }
12703 : :
12704 : 147213 : if (get_overrun ())
12705 : : return NULL_TREE;
12706 : :
12707 : 147213 : if (installing)
12708 : : {
12709 : 69047 : DECL_NOT_REALLY_EXTERN (decl) = flags & 1;
12710 : 69047 : DECL_RESULT (decl) = result;
12711 : 69047 : DECL_INITIAL (decl) = initial;
12712 : 69047 : DECL_SAVED_TREE (decl) = saved;
12713 : :
12714 : 69047 : if (context)
12715 : 2625 : SET_DECL_FRIEND_CONTEXT (decl, context);
12716 : 69047 : if (cexpr.decl)
12717 : 20446 : register_constexpr_fundef (cexpr);
12718 : 69047 : post_process (pdata);
12719 : : }
12720 : : else if (maybe_dup)
12721 : : {
12722 : : // FIXME:QOI Check matching defn
12723 : : }
12724 : :
12725 : : return true;
12726 : : }
12727 : :
12728 : : /* Also for CONCEPT_DECLs. */
12729 : :
12730 : : void
12731 : 75142 : trees_out::write_var_def (tree decl)
12732 : : {
12733 : : /* The initializer of a non-inline variable or variable template is
12734 : : ignored for determining exposures. */
12735 : 75142 : auto ovr = make_temp_override (dep_hash->ignore_tu_local,
12736 : 75142 : VAR_P (decl) && !DECL_INLINE_VAR_P (decl));
12737 : :
12738 : 75142 : tree init = DECL_INITIAL (decl);
12739 : 75142 : tree_node (init);
12740 : 75142 : if (!init)
12741 : : {
12742 : 1065 : tree dyn_init = NULL_TREE;
12743 : :
12744 : : /* We only need to write initializers in header modules. */
12745 : 1985 : if (header_module_p () && DECL_NONTRIVIALLY_INITIALIZED_P (decl))
12746 : : {
12747 : 384 : dyn_init = value_member (decl,
12748 : 384 : CP_DECL_THREAD_LOCAL_P (decl)
12749 : : ? tls_aggregates : static_aggregates);
12750 : 384 : gcc_checking_assert (dyn_init);
12751 : : /* Mark it so write_inits knows this is needed. */
12752 : 384 : TREE_LANG_FLAG_0 (dyn_init) = true;
12753 : 384 : dyn_init = TREE_PURPOSE (dyn_init);
12754 : : }
12755 : 1065 : tree_node (dyn_init);
12756 : : }
12757 : 75142 : }
12758 : :
12759 : : void
12760 : 0 : trees_out::mark_var_def (tree)
12761 : : {
12762 : 0 : }
12763 : :
12764 : : bool
12765 : 30043 : trees_in::read_var_def (tree decl, tree maybe_template)
12766 : : {
12767 : : /* Do not mark the virtual table entries as used. */
12768 : 30043 : bool vtable = VAR_P (decl) && DECL_VTABLE_OR_VTT_P (decl);
12769 : 30043 : unused += vtable;
12770 : 30043 : tree init = tree_node ();
12771 : 30043 : tree dyn_init = init ? NULL_TREE : tree_node ();
12772 : 30043 : unused -= vtable;
12773 : :
12774 : 30043 : if (get_overrun ())
12775 : : return false;
12776 : :
12777 : 30043 : bool initialized = (VAR_P (decl) ? bool (DECL_INITIALIZED_P (decl))
12778 : 4627 : : bool (DECL_INITIAL (decl)));
12779 : 30043 : tree maybe_dup = odr_duplicate (maybe_template, initialized);
12780 : 30043 : bool installing = maybe_dup && !initialized;
12781 : 30043 : if (installing)
12782 : : {
12783 : 17274 : DECL_INITIAL (decl) = init;
12784 : 17274 : if (DECL_EXTERNAL (decl))
12785 : 2368 : DECL_NOT_REALLY_EXTERN (decl) = true;
12786 : 17274 : if (VAR_P (decl))
12787 : : {
12788 : 15154 : DECL_INITIALIZED_P (decl) = true;
12789 : 15154 : if (maybe_dup && DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (maybe_dup))
12790 : 14853 : DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
12791 : 15154 : tentative_decl_linkage (decl);
12792 : 15154 : if (DECL_EXPLICIT_INSTANTIATION (decl)
12793 : 15154 : && !DECL_EXTERNAL (decl))
12794 : 9 : setup_explicit_instantiation_definition_linkage (decl);
12795 : 15154 : if (DECL_IMPLICIT_INSTANTIATION (decl)
12796 : 14344 : || (DECL_EXPLICIT_INSTANTIATION (decl)
12797 : 18 : && !DECL_EXTERNAL (decl))
12798 : 29489 : || (DECL_CLASS_SCOPE_P (decl)
12799 : 8341 : && !DECL_VTABLE_OR_VTT_P (decl)
12800 : 7122 : && !DECL_TEMPLATE_INFO (decl)))
12801 : 5279 : note_vague_linkage_variable (decl);
12802 : : }
12803 : 17274 : if (!dyn_init)
12804 : : ;
12805 : 184 : else if (CP_DECL_THREAD_LOCAL_P (decl))
12806 : 64 : tls_aggregates = tree_cons (dyn_init, decl, tls_aggregates);
12807 : : else
12808 : 120 : static_aggregates = tree_cons (dyn_init, decl, static_aggregates);
12809 : : }
12810 : : else if (maybe_dup)
12811 : : {
12812 : : // FIXME:QOI Check matching defn
12813 : : }
12814 : :
12815 : : return true;
12816 : : }
12817 : :
12818 : : /* If MEMBER doesn't have an independent life outside the class,
12819 : : return it (or its TEMPLATE_DECL). Otherwise NULL. */
12820 : :
12821 : : static tree
12822 : 169506 : member_owned_by_class (tree member)
12823 : : {
12824 : 169506 : gcc_assert (DECL_P (member));
12825 : :
12826 : : /* Clones are owned by their origin. */
12827 : 169506 : if (DECL_CLONED_FUNCTION_P (member))
12828 : : return NULL;
12829 : :
12830 : 169506 : if (TREE_CODE (member) == FIELD_DECL)
12831 : : /* FIELD_DECLS can have template info in some cases. We always
12832 : : want the FIELD_DECL though, as there's never a TEMPLATE_DECL
12833 : : wrapping them. */
12834 : : return member;
12835 : :
12836 : 74215 : int use_tpl = -1;
12837 : 74215 : if (tree ti = node_template_info (member, use_tpl))
12838 : : {
12839 : : // FIXME: Don't bail on things that CANNOT have their own
12840 : : // template header. No, make sure they're in the same cluster.
12841 : 0 : if (use_tpl > 0)
12842 : : return NULL_TREE;
12843 : :
12844 : 0 : if (DECL_TEMPLATE_RESULT (TI_TEMPLATE (ti)) == member)
12845 : 169506 : member = TI_TEMPLATE (ti);
12846 : : }
12847 : : return member;
12848 : : }
12849 : :
12850 : : void
12851 : 141118 : trees_out::write_class_def (tree defn)
12852 : : {
12853 : 141118 : gcc_assert (DECL_P (defn));
12854 : 141118 : if (streaming_p ())
12855 : 70809 : dump () && dump ("Writing class definition %N", defn);
12856 : :
12857 : 141118 : tree type = TREE_TYPE (defn);
12858 : 141118 : tree_node (TYPE_SIZE (type));
12859 : 141118 : tree_node (TYPE_SIZE_UNIT (type));
12860 : 141118 : tree_node (TYPE_VFIELD (type));
12861 : 141118 : tree_node (TYPE_BINFO (type));
12862 : :
12863 : 141118 : vec_chained_decls (TYPE_FIELDS (type));
12864 : :
12865 : : /* Every class but __as_base has a type-specific. */
12866 : 280102 : gcc_checking_assert (!TYPE_LANG_SPECIFIC (type) == IS_FAKE_BASE_TYPE (type));
12867 : :
12868 : 141118 : if (TYPE_LANG_SPECIFIC (type))
12869 : : {
12870 : 138984 : {
12871 : 138984 : vec<tree, va_gc> *v = CLASSTYPE_MEMBER_VEC (type);
12872 : 138984 : if (!v)
12873 : : {
12874 : 38726 : gcc_checking_assert (!streaming_p ());
12875 : : /* Force a class vector. */
12876 : 38726 : v = set_class_bindings (type, -1);
12877 : 38726 : gcc_checking_assert (v);
12878 : : }
12879 : :
12880 : 138984 : unsigned len = v->length ();
12881 : 138984 : if (streaming_p ())
12882 : 69472 : u (len);
12883 : 1036040 : for (unsigned ix = 0; ix != len; ix++)
12884 : : {
12885 : 897056 : tree m = (*v)[ix];
12886 : 897056 : if (TREE_CODE (m) == TYPE_DECL
12887 : 291259 : && DECL_ARTIFICIAL (m)
12888 : 1046641 : && TYPE_STUB_DECL (TREE_TYPE (m)) == m)
12889 : : /* This is a using-decl for a type, or an anonymous
12890 : : struct (maybe with a typedef name). Write the type. */
12891 : 9864 : m = TREE_TYPE (m);
12892 : 897056 : tree_node (m);
12893 : : }
12894 : : }
12895 : 138984 : tree_node (CLASSTYPE_LAMBDA_EXPR (type));
12896 : :
12897 : : /* TYPE_CONTAINS_VPTR_P looks at the vbase vector, which the
12898 : : reader won't know at this point. */
12899 : 138984 : int has_vptr = TYPE_CONTAINS_VPTR_P (type);
12900 : :
12901 : 138984 : if (streaming_p ())
12902 : : {
12903 : 69472 : unsigned nvbases = vec_safe_length (CLASSTYPE_VBASECLASSES (type));
12904 : 69472 : u (nvbases);
12905 : 69472 : i (has_vptr);
12906 : : }
12907 : :
12908 : 138984 : if (has_vptr)
12909 : : {
12910 : 4760 : tree_vec (CLASSTYPE_PURE_VIRTUALS (type));
12911 : 4760 : tree_pair_vec (CLASSTYPE_VCALL_INDICES (type));
12912 : 4760 : tree_node (CLASSTYPE_KEY_METHOD (type));
12913 : : }
12914 : : }
12915 : :
12916 : 141118 : if (TYPE_LANG_SPECIFIC (type))
12917 : : {
12918 : 138984 : tree_node (CLASSTYPE_PRIMARY_BINFO (type));
12919 : :
12920 : 138984 : tree as_base = CLASSTYPE_AS_BASE (type);
12921 : 138984 : if (as_base)
12922 : 69285 : as_base = TYPE_NAME (as_base);
12923 : 138984 : tree_node (as_base);
12924 : :
12925 : : /* Write the vtables. */
12926 : 138984 : tree vtables = CLASSTYPE_VTABLES (type);
12927 : 138984 : vec_chained_decls (vtables);
12928 : 283544 : for (; vtables; vtables = TREE_CHAIN (vtables))
12929 : 5576 : write_definition (vtables);
12930 : :
12931 : 138984 : {
12932 : : /* Friend declarations in class definitions are ignored when
12933 : : determining exposures. */
12934 : 138984 : auto ovr = make_temp_override (dep_hash->ignore_tu_local, true);
12935 : :
12936 : : /* Write the friend classes. */
12937 : 138984 : tree_list (CLASSTYPE_FRIEND_CLASSES (type), false);
12938 : :
12939 : : /* Write the friend functions. */
12940 : 138984 : for (tree friends = DECL_FRIENDLIST (defn);
12941 : 154044 : friends; friends = TREE_CHAIN (friends))
12942 : : {
12943 : 15060 : tree_node (FRIEND_NAME (friends));
12944 : 15060 : tree_list (FRIEND_DECLS (friends), false);
12945 : : }
12946 : : /* End of friend fns. */
12947 : 138984 : tree_node (NULL_TREE);
12948 : 138984 : }
12949 : :
12950 : : /* Write the decl list. We don't need to ignore exposures of friend
12951 : : decls here as any such decls should already have been added and
12952 : : ignored above. */
12953 : 138984 : tree_list (CLASSTYPE_DECL_LIST (type), true);
12954 : :
12955 : 138984 : if (TYPE_CONTAINS_VPTR_P (type))
12956 : : {
12957 : : /* Write the thunks. */
12958 : 4760 : for (tree decls = TYPE_FIELDS (type);
12959 : 134488 : decls; decls = DECL_CHAIN (decls))
12960 : 129728 : if (TREE_CODE (decls) == FUNCTION_DECL
12961 : 94824 : && DECL_VIRTUAL_P (decls)
12962 : 154124 : && DECL_THUNKS (decls))
12963 : : {
12964 : 852 : tree_node (decls);
12965 : : /* Thunks are always unique, so chaining is ok. */
12966 : 852 : chained_decls (DECL_THUNKS (decls));
12967 : : }
12968 : 4760 : tree_node (NULL_TREE);
12969 : : }
12970 : : }
12971 : 141118 : }
12972 : :
12973 : : void
12974 : 169506 : trees_out::mark_class_member (tree member, bool do_defn)
12975 : : {
12976 : 169506 : gcc_assert (DECL_P (member));
12977 : :
12978 : 169506 : member = member_owned_by_class (member);
12979 : 169506 : if (member)
12980 : 339012 : mark_declaration (member, do_defn && has_definition (member));
12981 : 169506 : }
12982 : :
12983 : : void
12984 : 141118 : trees_out::mark_class_def (tree defn)
12985 : : {
12986 : 141118 : gcc_assert (DECL_P (defn));
12987 : 141118 : tree type = TREE_TYPE (defn);
12988 : : /* Mark the class members that are not type-decls and cannot have
12989 : : independent definitions. */
12990 : 1400034 : for (tree member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
12991 : 1258916 : if (TREE_CODE (member) == FIELD_DECL
12992 : 1258916 : || TREE_CODE (member) == USING_DECL
12993 : : /* A cloned enum-decl from 'using enum unrelated;' */
12994 : 1258916 : || (TREE_CODE (member) == CONST_DECL
12995 : 7088 : && DECL_CONTEXT (member) == type))
12996 : : {
12997 : 169506 : mark_class_member (member);
12998 : 169506 : if (TREE_CODE (member) == FIELD_DECL)
12999 : 95291 : if (tree repr = DECL_BIT_FIELD_REPRESENTATIVE (member))
13000 : : /* If we're marking a class template definition, then
13001 : : this'll contain the width (as set by grokbitfield)
13002 : : instead of a decl. */
13003 : 1902 : if (DECL_P (repr))
13004 : 1610 : mark_declaration (repr, false);
13005 : : }
13006 : :
13007 : : /* Mark the binfo hierarchy. */
13008 : 337236 : for (tree child = TYPE_BINFO (type); child; child = TREE_CHAIN (child))
13009 : 196118 : mark_by_value (child);
13010 : :
13011 : 141118 : if (TYPE_LANG_SPECIFIC (type))
13012 : : {
13013 : 138984 : for (tree vtable = CLASSTYPE_VTABLES (type);
13014 : 144560 : vtable; vtable = TREE_CHAIN (vtable))
13015 : 5576 : mark_declaration (vtable, true);
13016 : :
13017 : 138984 : if (TYPE_CONTAINS_VPTR_P (type))
13018 : : /* Mark the thunks, they belong to the class definition,
13019 : : /not/ the thunked-to function. */
13020 : 4760 : for (tree decls = TYPE_FIELDS (type);
13021 : 134488 : decls; decls = DECL_CHAIN (decls))
13022 : 129728 : if (TREE_CODE (decls) == FUNCTION_DECL)
13023 : 94824 : for (tree thunks = DECL_THUNKS (decls);
13024 : 95952 : thunks; thunks = DECL_CHAIN (thunks))
13025 : 1128 : mark_declaration (thunks, false);
13026 : : }
13027 : 141118 : }
13028 : :
13029 : : /* Nop sorting, needed for resorting the member vec. */
13030 : :
13031 : : static void
13032 : 5346790 : nop (void *, void *, void *)
13033 : : {
13034 : 5346790 : }
13035 : :
13036 : : bool
13037 : 50987 : trees_in::read_class_def (tree defn, tree maybe_template)
13038 : : {
13039 : 50987 : gcc_assert (DECL_P (defn));
13040 : 51480 : dump () && dump ("Reading class definition %N", defn);
13041 : 50987 : tree type = TREE_TYPE (defn);
13042 : 50987 : tree size = tree_node ();
13043 : 50987 : tree size_unit = tree_node ();
13044 : 50987 : tree vfield = tree_node ();
13045 : 50987 : tree binfo = tree_node ();
13046 : 50987 : vec<tree, va_gc> *vbase_vec = NULL;
13047 : 50987 : vec<tree, va_gc> *member_vec = NULL;
13048 : 50987 : vec<tree, va_gc> *pure_virts = NULL;
13049 : 50987 : vec<tree_pair_s, va_gc> *vcall_indices = NULL;
13050 : 50987 : tree key_method = NULL_TREE;
13051 : 50987 : tree lambda = NULL_TREE;
13052 : :
13053 : : /* Read the fields. */
13054 : 50987 : vec<tree, va_heap> *fields = vec_chained_decls ();
13055 : :
13056 : 50987 : if (TYPE_LANG_SPECIFIC (type))
13057 : : {
13058 : 49985 : if (unsigned len = u ())
13059 : : {
13060 : 49985 : vec_alloc (member_vec, len);
13061 : 412902 : for (unsigned ix = 0; ix != len; ix++)
13062 : : {
13063 : 362917 : tree m = tree_node ();
13064 : 362917 : if (get_overrun ())
13065 : : break;
13066 : 362917 : if (TYPE_P (m))
13067 : 3497 : m = TYPE_STUB_DECL (m);
13068 : 362917 : member_vec->quick_push (m);
13069 : : }
13070 : : }
13071 : 49985 : lambda = tree_node ();
13072 : :
13073 : 49985 : if (!get_overrun ())
13074 : : {
13075 : 49985 : unsigned nvbases = u ();
13076 : 49985 : if (nvbases)
13077 : : {
13078 : 246 : vec_alloc (vbase_vec, nvbases);
13079 : 1117 : for (tree child = binfo; child; child = TREE_CHAIN (child))
13080 : 871 : if (BINFO_VIRTUAL_P (child))
13081 : 246 : vbase_vec->quick_push (child);
13082 : : }
13083 : : }
13084 : :
13085 : 49985 : if (!get_overrun ())
13086 : : {
13087 : 49985 : int has_vptr = i ();
13088 : 49985 : if (has_vptr)
13089 : : {
13090 : 2057 : pure_virts = tree_vec ();
13091 : 2057 : vcall_indices = tree_pair_vec ();
13092 : 2057 : key_method = tree_node ();
13093 : : }
13094 : : }
13095 : : }
13096 : :
13097 : 50987 : tree maybe_dup = odr_duplicate (maybe_template, TYPE_SIZE (type));
13098 : 50987 : bool installing = maybe_dup && !TYPE_SIZE (type);
13099 : 22271 : if (installing)
13100 : : {
13101 : 22271 : if (maybe_dup != defn)
13102 : : {
13103 : : // FIXME: This is needed on other defns too, almost
13104 : : // duplicate-decl like? See is_matching_decl too.
13105 : : /* Copy flags from the duplicate. */
13106 : 171 : tree type_dup = TREE_TYPE (maybe_dup);
13107 : :
13108 : : /* Core pieces. */
13109 : 171 : TYPE_MODE_RAW (type) = TYPE_MODE_RAW (type_dup);
13110 : 171 : TYPE_ALIGN_RAW (type) = TYPE_ALIGN_RAW (type_dup);
13111 : 342 : TYPE_WARN_IF_NOT_ALIGN_RAW (type)
13112 : 171 : = TYPE_WARN_IF_NOT_ALIGN_RAW (type_dup);
13113 : 171 : TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (type_dup);
13114 : :
13115 : 171 : SET_DECL_MODE (defn, DECL_MODE (maybe_dup));
13116 : 171 : DECL_SIZE (defn) = DECL_SIZE (maybe_dup);
13117 : 171 : DECL_SIZE_UNIT (defn) = DECL_SIZE_UNIT (maybe_dup);
13118 : 171 : DECL_ALIGN_RAW (defn) = DECL_ALIGN_RAW (maybe_dup);
13119 : 342 : DECL_WARN_IF_NOT_ALIGN_RAW (defn)
13120 : 171 : = DECL_WARN_IF_NOT_ALIGN_RAW (maybe_dup);
13121 : 171 : DECL_USER_ALIGN (defn) = DECL_USER_ALIGN (maybe_dup);
13122 : :
13123 : 171 : TYPE_TYPELESS_STORAGE (type) = TYPE_TYPELESS_STORAGE (type_dup);
13124 : 171 : TYPE_CXX_ODR_P (type) = TYPE_CXX_ODR_P (type_dup);
13125 : 171 : TYPE_NO_FORCE_BLK (type) = TYPE_NO_FORCE_BLK (type_dup);
13126 : 171 : TYPE_TRANSPARENT_AGGR (type) = TYPE_TRANSPARENT_AGGR (type_dup);
13127 : 342 : TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type)
13128 : 171 : = TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type_dup);
13129 : :
13130 : 171 : TYPE_EMPTY_P (type) = TYPE_EMPTY_P (type_dup);
13131 : 171 : TREE_ADDRESSABLE (type) = TREE_ADDRESSABLE (type_dup);
13132 : :
13133 : : /* C++ pieces. */
13134 : 171 : TYPE_POLYMORPHIC_P (type) = TYPE_POLYMORPHIC_P (type_dup);
13135 : 171 : CLASSTYPE_FINAL (type) = CLASSTYPE_FINAL (type_dup);
13136 : :
13137 : 342 : TYPE_HAS_USER_CONSTRUCTOR (type)
13138 : 171 : = TYPE_HAS_USER_CONSTRUCTOR (type_dup);
13139 : 342 : TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
13140 : 171 : = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type_dup);
13141 : 342 : TYPE_NEEDS_CONSTRUCTING (type)
13142 : 171 : = TYPE_NEEDS_CONSTRUCTING (type_dup);
13143 : :
13144 : 171 : if (auto ls = TYPE_LANG_SPECIFIC (type_dup))
13145 : : {
13146 : 171 : if (TYPE_LANG_SPECIFIC (type))
13147 : : {
13148 : 513 : CLASSTYPE_BEFRIENDING_CLASSES (type_dup)
13149 : 171 : = CLASSTYPE_BEFRIENDING_CLASSES (type);
13150 : 171 : if (!ANON_AGGR_TYPE_P (type))
13151 : 513 : CLASSTYPE_TYPEINFO_VAR (type_dup)
13152 : 171 : = CLASSTYPE_TYPEINFO_VAR (type);
13153 : : }
13154 : 804 : for (tree v = type; v; v = TYPE_NEXT_VARIANT (v))
13155 : 633 : TYPE_LANG_SPECIFIC (v) = ls;
13156 : : }
13157 : : }
13158 : :
13159 : 22271 : TYPE_SIZE (type) = size;
13160 : 22271 : TYPE_SIZE_UNIT (type) = size_unit;
13161 : :
13162 : 22271 : if (fields)
13163 : : {
13164 : 22271 : tree *chain = &TYPE_FIELDS (type);
13165 : 22271 : unsigned len = fields->length ();
13166 : 245219 : for (unsigned ix = 0; ix != len; ix++)
13167 : : {
13168 : 222948 : tree decl = (*fields)[ix];
13169 : :
13170 : 222948 : if (!decl)
13171 : : {
13172 : : /* An anonymous struct with typedef name. */
13173 : 3 : tree tdef = (*fields)[ix+1];
13174 : 3 : decl = TYPE_STUB_DECL (TREE_TYPE (tdef));
13175 : 3 : gcc_checking_assert (IDENTIFIER_ANON_P (DECL_NAME (decl))
13176 : : && decl != tdef);
13177 : : }
13178 : :
13179 : 404088 : gcc_checking_assert (!*chain == !DECL_CLONED_FUNCTION_P (decl));
13180 : 222948 : *chain = decl;
13181 : 222948 : chain = &DECL_CHAIN (decl);
13182 : :
13183 : 222948 : if (TREE_CODE (decl) == FIELD_DECL
13184 : 222948 : && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
13185 : : {
13186 : 146 : tree anon_type = TYPE_MAIN_VARIANT (TREE_TYPE (decl));
13187 : 146 : if (DECL_NAME (defn) == as_base_identifier)
13188 : : /* ANON_AGGR_TYPE_FIELD should already point to the
13189 : : original FIELD_DECL; don't overwrite it to point
13190 : : to the as-base FIELD_DECL copy. */
13191 : 10 : gcc_checking_assert (ANON_AGGR_TYPE_FIELD (anon_type));
13192 : : else
13193 : 136 : ANON_AGGR_TYPE_FIELD (anon_type) = decl;
13194 : : }
13195 : :
13196 : 222948 : if (TREE_CODE (decl) == USING_DECL
13197 : 222948 : && TREE_CODE (USING_DECL_SCOPE (decl)) == RECORD_TYPE)
13198 : : {
13199 : : /* Reconstruct DECL_ACCESS. */
13200 : 10436 : tree decls = USING_DECL_DECLS (decl);
13201 : 10436 : tree access = declared_access (decl);
13202 : :
13203 : 11944 : for (ovl_iterator iter (decls); iter; ++iter)
13204 : : {
13205 : 962 : tree d = *iter;
13206 : :
13207 : 962 : retrofit_lang_decl (d);
13208 : 962 : tree list = DECL_ACCESS (d);
13209 : :
13210 : 962 : if (!purpose_member (type, list))
13211 : 1135 : DECL_ACCESS (d) = tree_cons (type, access, list);
13212 : : }
13213 : : }
13214 : : }
13215 : : }
13216 : :
13217 : 22271 : TYPE_VFIELD (type) = vfield;
13218 : 22271 : TYPE_BINFO (type) = binfo;
13219 : :
13220 : 22271 : if (TYPE_LANG_SPECIFIC (type))
13221 : : {
13222 : 21772 : CLASSTYPE_LAMBDA_EXPR (type) = lambda;
13223 : :
13224 : 21772 : CLASSTYPE_MEMBER_VEC (type) = member_vec;
13225 : 21772 : CLASSTYPE_PURE_VIRTUALS (type) = pure_virts;
13226 : 21772 : CLASSTYPE_VCALL_INDICES (type) = vcall_indices;
13227 : :
13228 : 21772 : CLASSTYPE_KEY_METHOD (type) = key_method;
13229 : :
13230 : 21772 : CLASSTYPE_VBASECLASSES (type) = vbase_vec;
13231 : :
13232 : : /* Resort the member vector. */
13233 : 21772 : resort_type_member_vec (member_vec, NULL, nop, NULL);
13234 : : }
13235 : : }
13236 : : else if (maybe_dup)
13237 : : {
13238 : : // FIXME:QOI Check matching defn
13239 : : }
13240 : :
13241 : 50987 : if (TYPE_LANG_SPECIFIC (type))
13242 : : {
13243 : 49985 : tree primary = tree_node ();
13244 : 49985 : tree as_base = tree_node ();
13245 : :
13246 : 49985 : if (as_base)
13247 : 25049 : as_base = TREE_TYPE (as_base);
13248 : :
13249 : : /* Read the vtables. */
13250 : 49985 : vec<tree, va_heap> *vtables = vec_chained_decls ();
13251 : 49985 : if (vtables)
13252 : : {
13253 : 2023 : unsigned len = vtables->length ();
13254 : 4461 : for (unsigned ix = 0; ix != len; ix++)
13255 : : {
13256 : 2438 : tree vtable = (*vtables)[ix];
13257 : 2438 : read_var_def (vtable, vtable);
13258 : : }
13259 : : }
13260 : :
13261 : 49985 : tree friend_classes = tree_list (false);
13262 : 49985 : tree friend_functions = NULL_TREE;
13263 : 49985 : for (tree *chain = &friend_functions;
13264 : 56912 : tree name = tree_node (); chain = &TREE_CHAIN (*chain))
13265 : : {
13266 : 6927 : tree val = tree_list (false);
13267 : 6927 : *chain = build_tree_list (name, val);
13268 : 6927 : }
13269 : 49985 : tree decl_list = tree_list (true);
13270 : :
13271 : 49985 : if (installing)
13272 : : {
13273 : 21772 : CLASSTYPE_PRIMARY_BINFO (type) = primary;
13274 : 21772 : CLASSTYPE_AS_BASE (type) = as_base;
13275 : :
13276 : 21772 : if (vtables)
13277 : : {
13278 : 1009 : if ((!CLASSTYPE_KEY_METHOD (type)
13279 : : /* Sneaky user may have defined it inline
13280 : : out-of-class. */
13281 : 338 : || DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (type)))
13282 : : /* An imported non-template class attached to a module
13283 : : doesn't need to have its vtables emitted here. */
13284 : 1015 : && (CLASSTYPE_USE_TEMPLATE (type)
13285 : 78 : || !DECL_MODULE_ATTACH_P (defn)))
13286 : 638 : vec_safe_push (keyed_classes, type);
13287 : 1009 : unsigned len = vtables->length ();
13288 : 1009 : tree *chain = &CLASSTYPE_VTABLES (type);
13289 : 2228 : for (unsigned ix = 0; ix != len; ix++)
13290 : : {
13291 : 1219 : tree vtable = (*vtables)[ix];
13292 : 1219 : gcc_checking_assert (!*chain);
13293 : 1219 : *chain = vtable;
13294 : 1219 : chain = &DECL_CHAIN (vtable);
13295 : : }
13296 : : }
13297 : 21772 : CLASSTYPE_FRIEND_CLASSES (type) = friend_classes;
13298 : 21772 : DECL_FRIENDLIST (defn) = friend_functions;
13299 : 21772 : CLASSTYPE_DECL_LIST (type) = decl_list;
13300 : :
13301 : 23233 : for (; friend_classes; friend_classes = TREE_CHAIN (friend_classes))
13302 : : {
13303 : 1461 : tree f = TREE_VALUE (friend_classes);
13304 : 1461 : if (TREE_CODE (f) == TEMPLATE_DECL)
13305 : 556 : f = TREE_TYPE (f);
13306 : :
13307 : 1461 : if (CLASS_TYPE_P (f))
13308 : : {
13309 : 1429 : CLASSTYPE_BEFRIENDING_CLASSES (f)
13310 : 2858 : = tree_cons (NULL_TREE, type,
13311 : 1429 : CLASSTYPE_BEFRIENDING_CLASSES (f));
13312 : 1467 : dump () && dump ("Class %N befriending %C:%N",
13313 : 6 : type, TREE_CODE (f), f);
13314 : : }
13315 : : }
13316 : :
13317 : 24890 : for (; friend_functions;
13318 : 3118 : friend_functions = TREE_CHAIN (friend_functions))
13319 : 3118 : for (tree friend_decls = TREE_VALUE (friend_functions);
13320 : 7007 : friend_decls; friend_decls = TREE_CHAIN (friend_decls))
13321 : : {
13322 : 3889 : tree f = TREE_VALUE (friend_decls);
13323 : 3889 : if (TREE_CODE (f) == TU_LOCAL_ENTITY)
13324 : 36 : continue;
13325 : :
13326 : 3853 : DECL_BEFRIENDING_CLASSES (f)
13327 : 3853 : = tree_cons (NULL_TREE, type, DECL_BEFRIENDING_CLASSES (f));
13328 : 3916 : dump () && dump ("Class %N befriending %C:%N",
13329 : 27 : type, TREE_CODE (f), f);
13330 : : }
13331 : : }
13332 : :
13333 : 49985 : if (TYPE_CONTAINS_VPTR_P (type))
13334 : : /* Read and install the thunks. */
13335 : 2433 : while (tree vfunc = tree_node ())
13336 : : {
13337 : 376 : tree thunks = chained_decls ();
13338 : 376 : if (installing)
13339 : 186 : SET_DECL_THUNKS (vfunc, thunks);
13340 : : }
13341 : :
13342 : 49985 : vec_free (vtables);
13343 : : }
13344 : :
13345 : : /* Propagate to all variants. */
13346 : 50987 : if (installing)
13347 : 22271 : fixup_type_variants (type);
13348 : :
13349 : : /* IS_FAKE_BASE_TYPE is inaccurate at this point, because if this is
13350 : : the fake base, we've not hooked it into the containing class's
13351 : : data structure yet. Fortunately it has a unique name. */
13352 : 22271 : if (installing
13353 : 22271 : && DECL_NAME (defn) != as_base_identifier
13354 : 21772 : && (!CLASSTYPE_TEMPLATE_INFO (type)
13355 : 18503 : || !uses_template_parms (TI_ARGS (CLASSTYPE_TEMPLATE_INFO (type)))))
13356 : : /* Emit debug info. It'd be nice to know if the interface TU
13357 : : already emitted this. */
13358 : 12084 : rest_of_type_compilation (type, !LOCAL_CLASS_P (type));
13359 : :
13360 : 50987 : vec_free (fields);
13361 : :
13362 : 50987 : return !get_overrun ();
13363 : : }
13364 : :
13365 : : void
13366 : 7589 : trees_out::write_enum_def (tree decl)
13367 : : {
13368 : 7589 : tree type = TREE_TYPE (decl);
13369 : :
13370 : 7589 : tree_node (TYPE_VALUES (type));
13371 : : /* Note that we stream TYPE_MIN/MAX_VALUE directly as part of the
13372 : : ENUMERAL_TYPE. */
13373 : 7589 : }
13374 : :
13375 : : void
13376 : 7589 : trees_out::mark_enum_def (tree decl)
13377 : : {
13378 : 7589 : tree type = TREE_TYPE (decl);
13379 : :
13380 : 33194 : for (tree values = TYPE_VALUES (type); values; values = TREE_CHAIN (values))
13381 : : {
13382 : 25605 : tree cst = TREE_VALUE (values);
13383 : 25605 : mark_by_value (cst);
13384 : : /* We must mark the init to avoid circularity in tt_enum_int. */
13385 : 25605 : if (tree init = DECL_INITIAL (cst))
13386 : 25397 : if (TREE_CODE (init) == INTEGER_CST)
13387 : 24837 : mark_by_value (init);
13388 : : }
13389 : 7589 : }
13390 : :
13391 : : bool
13392 : 2440 : trees_in::read_enum_def (tree defn, tree maybe_template)
13393 : : {
13394 : 2440 : tree type = TREE_TYPE (defn);
13395 : 2440 : tree values = tree_node ();
13396 : :
13397 : 2440 : if (get_overrun ())
13398 : : return false;
13399 : :
13400 : 2440 : tree maybe_dup = odr_duplicate (maybe_template, TYPE_VALUES (type));
13401 : 4880 : bool installing = maybe_dup && !TYPE_VALUES (type);
13402 : :
13403 : 2440 : if (installing)
13404 : : {
13405 : 1006 : TYPE_VALUES (type) = values;
13406 : : /* Note that we stream TYPE_MIN/MAX_VALUE directly as part of the
13407 : : ENUMERAL_TYPE. */
13408 : :
13409 : 1646 : rest_of_type_compilation (type, DECL_NAMESPACE_SCOPE_P (defn));
13410 : : }
13411 : 1434 : else if (maybe_dup)
13412 : : {
13413 : 1434 : tree known = TYPE_VALUES (type);
13414 : 7385 : for (; known && values;
13415 : 5951 : known = TREE_CHAIN (known), values = TREE_CHAIN (values))
13416 : : {
13417 : 5960 : tree known_decl = TREE_VALUE (known);
13418 : 5960 : tree new_decl = TREE_VALUE (values);
13419 : :
13420 : 5960 : if (DECL_NAME (known_decl) != DECL_NAME (new_decl))
13421 : : break;
13422 : :
13423 : 5954 : new_decl = maybe_duplicate (new_decl);
13424 : :
13425 : 5954 : if (!cp_tree_equal (DECL_INITIAL (known_decl),
13426 : 5954 : DECL_INITIAL (new_decl)))
13427 : : break;
13428 : : }
13429 : :
13430 : 1434 : if (known || values)
13431 : : {
13432 : 12 : auto_diagnostic_group d;
13433 : 12 : error_at (DECL_SOURCE_LOCATION (maybe_dup),
13434 : : "definition of %qD does not match", maybe_dup);
13435 : 12 : inform (DECL_SOURCE_LOCATION (defn),
13436 : : "existing definition %qD", defn);
13437 : :
13438 : 12 : tree known_decl = NULL_TREE, new_decl = NULL_TREE;
13439 : :
13440 : 12 : if (known)
13441 : 9 : known_decl = TREE_VALUE (known);
13442 : 12 : if (values)
13443 : 12 : new_decl = maybe_duplicate (TREE_VALUE (values));
13444 : :
13445 : 12 : if (known_decl && new_decl)
13446 : : {
13447 : 9 : inform (DECL_SOURCE_LOCATION (new_decl),
13448 : : "enumerator %qD does not match ...", new_decl);
13449 : 9 : inform (DECL_SOURCE_LOCATION (known_decl),
13450 : : "... this enumerator %qD", known_decl);
13451 : : }
13452 : 3 : else if (known_decl || new_decl)
13453 : : {
13454 : 3 : tree extra = known_decl ? known_decl : new_decl;
13455 : 3 : inform (DECL_SOURCE_LOCATION (extra),
13456 : : "additional enumerators beginning with %qD", extra);
13457 : : }
13458 : : else
13459 : 0 : inform (DECL_SOURCE_LOCATION (maybe_dup),
13460 : : "enumeration range differs");
13461 : :
13462 : : /* Mark it bad. */
13463 : 12 : unmatched_duplicate (maybe_template);
13464 : 12 : }
13465 : : }
13466 : :
13467 : : return true;
13468 : : }
13469 : :
13470 : : /* Write out the body of DECL. See above circularity note. */
13471 : :
13472 : : void
13473 : 563308 : trees_out::write_definition (tree decl, bool refs_tu_local)
13474 : : {
13475 : 563308 : auto ovr = make_temp_override (writing_local_entities,
13476 : 563308 : writing_local_entities || refs_tu_local);
13477 : :
13478 : 563308 : if (streaming_p ())
13479 : : {
13480 : 281586 : assert_definition (decl);
13481 : 281586 : dump ()
13482 : 573 : && dump ("Writing definition %C:%N", TREE_CODE (decl), decl);
13483 : : }
13484 : : else
13485 : 281722 : dump (dumper::DEPEND)
13486 : 75 : && dump ("Depending definition %C:%N", TREE_CODE (decl), decl);
13487 : :
13488 : 891180 : again:
13489 : 891180 : switch (TREE_CODE (decl))
13490 : : {
13491 : 0 : default:
13492 : 0 : gcc_unreachable ();
13493 : :
13494 : 327872 : case TEMPLATE_DECL:
13495 : 327872 : decl = DECL_TEMPLATE_RESULT (decl);
13496 : 327872 : goto again;
13497 : :
13498 : 339459 : case FUNCTION_DECL:
13499 : 339459 : write_function_def (decl);
13500 : 339459 : break;
13501 : :
13502 : 148707 : case TYPE_DECL:
13503 : 148707 : {
13504 : 148707 : tree type = TREE_TYPE (decl);
13505 : 148707 : gcc_assert (TYPE_MAIN_VARIANT (type) == type
13506 : : && TYPE_NAME (type) == decl);
13507 : 148707 : if (TREE_CODE (type) == ENUMERAL_TYPE)
13508 : 7589 : write_enum_def (decl);
13509 : : else
13510 : 141118 : write_class_def (decl);
13511 : : }
13512 : : break;
13513 : :
13514 : 75142 : case VAR_DECL:
13515 : 75142 : case CONCEPT_DECL:
13516 : 75142 : write_var_def (decl);
13517 : 75142 : break;
13518 : : }
13519 : 563308 : }
13520 : :
13521 : : /* Mark a declaration for by-value walking. If DO_DEFN is true, mark
13522 : : its body too. */
13523 : :
13524 : : void
13525 : 2440800 : trees_out::mark_declaration (tree decl, bool do_defn)
13526 : : {
13527 : 2440800 : mark_by_value (decl);
13528 : :
13529 : 2440800 : if (TREE_CODE (decl) == TEMPLATE_DECL)
13530 : 823392 : decl = DECL_TEMPLATE_RESULT (decl);
13531 : :
13532 : 2440800 : if (!do_defn)
13533 : : return;
13534 : :
13535 : 563308 : switch (TREE_CODE (decl))
13536 : : {
13537 : 0 : default:
13538 : 0 : gcc_unreachable ();
13539 : :
13540 : : case FUNCTION_DECL:
13541 : : mark_function_def (decl);
13542 : : break;
13543 : :
13544 : 148707 : case TYPE_DECL:
13545 : 148707 : {
13546 : 148707 : tree type = TREE_TYPE (decl);
13547 : 148707 : gcc_assert (TYPE_MAIN_VARIANT (type) == type
13548 : : && TYPE_NAME (type) == decl);
13549 : 148707 : if (TREE_CODE (type) == ENUMERAL_TYPE)
13550 : 7589 : mark_enum_def (decl);
13551 : : else
13552 : 141118 : mark_class_def (decl);
13553 : : }
13554 : : break;
13555 : :
13556 : : case VAR_DECL:
13557 : : case CONCEPT_DECL:
13558 : : mark_var_def (decl);
13559 : : break;
13560 : : }
13561 : : }
13562 : :
13563 : : /* Read in the body of DECL. See above circularity note. */
13564 : :
13565 : : bool
13566 : 228245 : trees_in::read_definition (tree decl)
13567 : : {
13568 : 229260 : dump () && dump ("Reading definition %C %N", TREE_CODE (decl), decl);
13569 : :
13570 : : tree maybe_template = decl;
13571 : :
13572 : 228245 : again:
13573 : 364416 : switch (TREE_CODE (decl))
13574 : : {
13575 : : default:
13576 : : break;
13577 : :
13578 : 136171 : case TEMPLATE_DECL:
13579 : 136171 : decl = DECL_TEMPLATE_RESULT (decl);
13580 : 136171 : goto again;
13581 : :
13582 : 147213 : case FUNCTION_DECL:
13583 : 147213 : return read_function_def (decl, maybe_template);
13584 : :
13585 : 53427 : case TYPE_DECL:
13586 : 53427 : {
13587 : 53427 : tree type = TREE_TYPE (decl);
13588 : 53427 : gcc_assert (TYPE_MAIN_VARIANT (type) == type
13589 : : && TYPE_NAME (type) == decl);
13590 : 53427 : if (TREE_CODE (type) == ENUMERAL_TYPE)
13591 : 2440 : return read_enum_def (decl, maybe_template);
13592 : : else
13593 : 50987 : return read_class_def (decl, maybe_template);
13594 : : }
13595 : 27605 : break;
13596 : :
13597 : 27605 : case VAR_DECL:
13598 : 27605 : case CONCEPT_DECL:
13599 : 27605 : return read_var_def (decl, maybe_template);
13600 : : }
13601 : :
13602 : : return false;
13603 : : }
13604 : :
13605 : : /* Lookup an maybe insert a slot for depset for KEY. */
13606 : :
13607 : : depset **
13608 : 11456516 : depset::hash::entity_slot (tree entity, bool insert)
13609 : : {
13610 : 11456516 : traits::compare_type key (entity, NULL);
13611 : 17389848 : depset **slot = find_slot_with_hash (key, traits::hash (key),
13612 : : insert ? INSERT : NO_INSERT);
13613 : :
13614 : 11456516 : return slot;
13615 : : }
13616 : :
13617 : : depset **
13618 : 150455 : depset::hash::binding_slot (tree ctx, tree name, bool insert)
13619 : : {
13620 : 150455 : traits::compare_type key (ctx, name);
13621 : 182308 : depset **slot = find_slot_with_hash (key, traits::hash (key),
13622 : : insert ? INSERT : NO_INSERT);
13623 : :
13624 : 150455 : return slot;
13625 : : }
13626 : :
13627 : : depset *
13628 : 5648320 : depset::hash::find_dependency (tree decl)
13629 : : {
13630 : 5648320 : depset **slot = entity_slot (decl, false);
13631 : :
13632 : 5648320 : return slot ? *slot : NULL;
13633 : : }
13634 : :
13635 : : depset *
13636 : 31853 : depset::hash::find_binding (tree ctx, tree name)
13637 : : {
13638 : 31853 : depset **slot = binding_slot (ctx, name, false);
13639 : :
13640 : 31853 : return slot ? *slot : NULL;
13641 : : }
13642 : :
13643 : : /* Returns true if DECL is a TU-local entity, as defined by [basic.link].
13644 : : If EXPLAIN is true, emit an informative note about why DECL is TU-local. */
13645 : :
13646 : : bool
13647 : 1055859 : depset::hash::is_tu_local_entity (tree decl, bool explain/*=false*/)
13648 : : {
13649 : 1055859 : gcc_checking_assert (DECL_P (decl));
13650 : 1055859 : location_t loc = DECL_SOURCE_LOCATION (decl);
13651 : 1055859 : tree type = TREE_TYPE (decl);
13652 : :
13653 : : /* Only types, functions, variables, and template (specialisations)
13654 : : can be TU-local. */
13655 : 1055859 : if (TREE_CODE (decl) != TYPE_DECL
13656 : 1055859 : && TREE_CODE (decl) != FUNCTION_DECL
13657 : 394102 : && TREE_CODE (decl) != VAR_DECL
13658 : 380445 : && TREE_CODE (decl) != TEMPLATE_DECL)
13659 : : return false;
13660 : :
13661 : : /* An explicit type alias is not an entity; we don't want to stream
13662 : : such aliases if they refer to TU-local entities, so propagate this
13663 : : from the original type. The built-in declarations of 'int' and such
13664 : : are never TU-local. */
13665 : 1053112 : if (TREE_CODE (decl) == TYPE_DECL
13666 : 569263 : && !DECL_SELF_REFERENCE_P (decl)
13667 : 1599289 : && !DECL_IMPLICIT_TYPEDEF_P (decl))
13668 : : {
13669 : 310521 : tree orig = DECL_ORIGINAL_TYPE (decl);
13670 : 310521 : if (orig && TYPE_NAME (orig))
13671 : : {
13672 : 62080 : if (explain)
13673 : 11 : inform (loc, "%qD is an alias of TU-local type %qT", decl, orig);
13674 : 62080 : return is_tu_local_entity (TYPE_NAME (orig), explain);
13675 : : }
13676 : : else
13677 : : return false;
13678 : : }
13679 : :
13680 : : /* Check specializations first for slightly better explanations. */
13681 : 742591 : int use_tpl = -1;
13682 : 742591 : tree ti = node_template_info (decl, use_tpl);
13683 : 1050486 : if (use_tpl > 0 && TREE_CODE (TI_TEMPLATE (ti)) == TEMPLATE_DECL)
13684 : : {
13685 : : /* A specialization of a TU-local template. */
13686 : 307882 : tree tmpl = TI_TEMPLATE (ti);
13687 : 307882 : if (is_tu_local_entity (tmpl))
13688 : : {
13689 : 42 : if (explain)
13690 : : {
13691 : 9 : inform (loc, "%qD is a specialization of TU-local template %qD",
13692 : : decl, tmpl);
13693 : 9 : is_tu_local_entity (tmpl, /*explain=*/true);
13694 : : }
13695 : 42 : return true;
13696 : : }
13697 : :
13698 : : /* A specialization of a template with any TU-local template argument. */
13699 : 307840 : if (has_tu_local_tmpl_arg (decl, TI_ARGS (ti), explain))
13700 : : return true;
13701 : :
13702 : : /* FIXME A specialization of a template whose (possibly instantiated)
13703 : : declaration is an exposure. This should always be covered by the
13704 : : above cases?? */
13705 : : }
13706 : :
13707 : : /* A type, function, variable, or template with internal linkage. */
13708 : 742522 : linkage_kind kind = decl_linkage (decl);
13709 : 742522 : if (kind == lk_internal
13710 : : /* But although weakrefs are marked static, don't consider them
13711 : : to be TU-local. */
13712 : 742522 : && !lookup_attribute ("weakref", DECL_ATTRIBUTES (decl)))
13713 : : {
13714 : 522 : if (explain)
13715 : 111 : inform (loc, "%qD declared with internal linkage", decl);
13716 : 522 : return true;
13717 : : }
13718 : :
13719 : : /* Does not have a name with linkage and is declared, or introduced by a
13720 : : lambda-expression, within the definition of a TU-local entity. */
13721 : 742000 : if (kind == lk_none)
13722 : : {
13723 : 16125 : tree ctx = CP_DECL_CONTEXT (decl);
13724 : 27836 : if (LAMBDA_TYPE_P (type))
13725 : 3504 : if (tree extra = LAMBDA_TYPE_EXTRA_SCOPE (type))
13726 : 16125 : ctx = extra;
13727 : :
13728 : 16125 : if (TREE_CODE (ctx) == NAMESPACE_DECL)
13729 : : {
13730 : 65 : if (!TREE_PUBLIC (ctx))
13731 : : {
13732 : 0 : if (explain)
13733 : 0 : inform (loc, "%qD has no linkage and is declared in an "
13734 : : "anonymous namespace", decl);
13735 : 0 : return true;
13736 : : }
13737 : : }
13738 : 16060 : else if (TYPE_P (ctx))
13739 : : {
13740 : 5599 : tree ctx_decl = TYPE_MAIN_DECL (ctx);
13741 : 5599 : if (is_tu_local_entity (ctx_decl))
13742 : : {
13743 : 0 : if (explain)
13744 : : {
13745 : 0 : inform (loc, "%qD has no linkage and is declared within "
13746 : : "TU-local entity %qT", decl, ctx);
13747 : 0 : is_tu_local_entity (ctx_decl, /*explain=*/true);
13748 : : }
13749 : 0 : return true;
13750 : : }
13751 : : }
13752 : 10461 : else if (is_tu_local_entity (ctx))
13753 : : {
13754 : 27 : if (explain)
13755 : : {
13756 : 9 : inform (loc, "%qD has no linkage and is declared within "
13757 : : "TU-local entity %qD", decl, ctx);
13758 : 9 : is_tu_local_entity (ctx, /*explain=*/true);
13759 : : }
13760 : 27 : return true;
13761 : : }
13762 : : }
13763 : :
13764 : : /* A type with no name that is defined outside a class-specifier, function
13765 : : body, or initializer; or is introduced by a defining-type-specifier that
13766 : : is used to declare only TU-local entities.
13767 : :
13768 : : We consider types with names for linkage purposes as having names, since
13769 : : these aren't really TU-local. */
13770 : 741973 : tree inner = STRIP_TEMPLATE (decl);
13771 : 377599 : if (inner
13772 : 741973 : && TREE_CODE (inner) == TYPE_DECL
13773 : 1025799 : && TYPE_ANON_P (type)
13774 : 5363 : && !DECL_SELF_REFERENCE_P (inner)
13775 : : /* An enum with an enumerator name for linkage. */
13776 : 382205 : && !(UNSCOPED_ENUM_P (type) && TYPE_VALUES (type)))
13777 : : {
13778 : 3207 : tree main_decl = TYPE_MAIN_DECL (type);
13779 : 6198 : if (LAMBDA_TYPE_P (type))
13780 : : {
13781 : : /* A lambda expression is, in practice, TU-local iff it has no
13782 : : mangling scope. This currently doesn't line up exactly with
13783 : : the standard's definition due to some ABI issues, but it's
13784 : : pretty close, and avoids other issues down the line. */
13785 : 2960 : if (!LAMBDA_TYPE_EXTRA_SCOPE (type))
13786 : : {
13787 : 14 : if (explain)
13788 : 6 : inform (loc, "%qT has no name and cannot be differentiated "
13789 : : "from similar lambdas in other TUs", type);
13790 : 14 : return true;
13791 : : }
13792 : : }
13793 : 494 : else if (!DECL_CLASS_SCOPE_P (main_decl)
13794 : 277 : && !decl_function_context (main_decl))
13795 : : {
13796 : 30 : if (explain)
13797 : 12 : inform (loc, "%qT has no name and is not defined within a class, "
13798 : : "function, or initializer", type);
13799 : 30 : return true;
13800 : : }
13801 : :
13802 : : // FIXME introduced by a defining-type-specifier only declaring TU-local
13803 : : // entities; does this refer to e.g. 'static struct {} a;"? I can't
13804 : : // think of any cases where this isn't covered by earlier cases. */
13805 : : }
13806 : :
13807 : : return false;
13808 : : }
13809 : :
13810 : : /* Helper for is_tu_local_entity. Returns true if one of the ARGS of
13811 : : DECL is TU-local. Emits an explanation if EXPLAIN is true. */
13812 : :
13813 : : bool
13814 : 344546 : depset::hash::has_tu_local_tmpl_arg (tree decl, tree args, bool explain)
13815 : : {
13816 : 344546 : if (!args || TREE_CODE (args) != TREE_VEC)
13817 : : return false;
13818 : :
13819 : 923439 : for (tree a : tree_vec_range (args))
13820 : : {
13821 : 578920 : if (TREE_CODE (a) == TREE_VEC)
13822 : : {
13823 : 36706 : if (has_tu_local_tmpl_arg (decl, a, explain))
13824 : 27 : return true;
13825 : : }
13826 : 542214 : else if (!WILDCARD_TYPE_P (a))
13827 : : {
13828 : 490178 : if (DECL_P (a) && is_tu_local_entity (a))
13829 : : {
13830 : 0 : if (explain)
13831 : : {
13832 : 0 : inform (DECL_SOURCE_LOCATION (decl),
13833 : : "%qD has TU-local template argument %qD",
13834 : : decl, a);
13835 : 0 : is_tu_local_entity (a, /*explain=*/true);
13836 : : }
13837 : 0 : return true;
13838 : : }
13839 : :
13840 : 490178 : if (TYPE_P (a) && TYPE_NAME (a) && is_tu_local_entity (TYPE_NAME (a)))
13841 : : {
13842 : 12 : if (explain)
13843 : : {
13844 : 0 : inform (DECL_SOURCE_LOCATION (decl),
13845 : : "%qD has TU-local template argument %qT",
13846 : : decl, a);
13847 : 0 : is_tu_local_entity (TYPE_NAME (a), /*explain=*/true);
13848 : : }
13849 : 12 : return true;
13850 : : }
13851 : :
13852 : 490166 : if (EXPR_P (a) && is_tu_local_value (decl, a, explain))
13853 : : return true;
13854 : : }
13855 : : }
13856 : :
13857 : 344519 : return false;
13858 : : }
13859 : :
13860 : : /* Returns true if EXPR (part of the initializer for DECL) is a TU-local value
13861 : : or object. Emits an explanation if EXPLAIN is true. */
13862 : :
13863 : : bool
13864 : 28193 : depset::hash::is_tu_local_value (tree decl, tree expr, bool explain)
13865 : : {
13866 : 28193 : if (!expr)
13867 : : return false;
13868 : :
13869 : 27181 : tree e = expr;
13870 : 27181 : STRIP_ANY_LOCATION_WRAPPER (e);
13871 : 27181 : STRIP_NOPS (e);
13872 : 27181 : if (TREE_CODE (e) == TARGET_EXPR)
13873 : 0 : e = TARGET_EXPR_INITIAL (e);
13874 : 0 : if (!e)
13875 : : return false;
13876 : :
13877 : : /* It is, or is a pointer to, a TU-local function or the object associated
13878 : : with a TU-local variable. */
13879 : 27181 : tree object = NULL_TREE;
13880 : 27181 : if (TREE_CODE (e) == ADDR_EXPR)
13881 : 69 : object = TREE_OPERAND (e, 0);
13882 : 27112 : else if (TREE_CODE (e) == PTRMEM_CST)
13883 : 0 : object = PTRMEM_CST_MEMBER (e);
13884 : 27112 : else if (VAR_OR_FUNCTION_DECL_P (e))
13885 : : object = e;
13886 : :
13887 : 69 : if (object
13888 : 345 : && VAR_OR_FUNCTION_DECL_P (object)
13889 : 414 : && is_tu_local_entity (object))
13890 : : {
13891 : 39 : if (explain)
13892 : : {
13893 : : /* We've lost a lot of location information by the time we get here,
13894 : : so let's just do our best effort. */
13895 : 12 : auto loc = cp_expr_loc_or_loc (expr, DECL_SOURCE_LOCATION (decl));
13896 : 12 : if (VAR_P (object))
13897 : 6 : inform (loc, "%qD refers to TU-local object %qD", decl, object);
13898 : : else
13899 : 6 : inform (loc, "%qD refers to TU-local function %qD", decl, object);
13900 : 12 : is_tu_local_entity (object, true);
13901 : : }
13902 : 39 : return true;
13903 : : }
13904 : :
13905 : : /* It is an object of class or array type and any of its subobjects or
13906 : : any of the objects or functions to which its non-static data members
13907 : : of reference type refer is TU-local and is usable in constant
13908 : : expressions. */
13909 : 27142 : if (TREE_CODE (e) == CONSTRUCTOR && AGGREGATE_TYPE_P (TREE_TYPE (e)))
13910 : 9453 : for (auto &f : CONSTRUCTOR_ELTS (e))
13911 : 8486 : if (is_tu_local_value (decl, f.value, explain))
13912 : : return true;
13913 : :
13914 : : return false;
13915 : : }
13916 : :
13917 : : /* DECL is a newly discovered dependency. Create the depset, if it
13918 : : doesn't already exist. Add it to the worklist if so.
13919 : :
13920 : : DECL will be an OVL_USING_P OVERLOAD, if it's from a binding that's
13921 : : a using decl.
13922 : :
13923 : : We do not have to worry about adding the same dependency more than
13924 : : once. First it's harmless, but secondly the TREE_VISITED marking
13925 : : prevents us wanting to do it anyway. */
13926 : :
13927 : : depset *
13928 : 4769036 : depset::hash::make_dependency (tree decl, entity_kind ek)
13929 : : {
13930 : : /* Make sure we're being told consistent information. */
13931 : 8833732 : gcc_checking_assert ((ek == EK_NAMESPACE)
13932 : : == (TREE_CODE (decl) == NAMESPACE_DECL
13933 : : && !DECL_NAMESPACE_ALIAS (decl)));
13934 : 4769036 : gcc_checking_assert (ek != EK_BINDING && ek != EK_REDIRECT);
13935 : 4769036 : gcc_checking_assert (TREE_CODE (decl) != FIELD_DECL
13936 : : && (TREE_CODE (decl) != USING_DECL
13937 : : || TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL));
13938 : 4769036 : gcc_checking_assert (!is_key_order ());
13939 : 4769036 : if (ek == EK_USING)
13940 : 15511 : gcc_checking_assert (TREE_CODE (decl) == OVERLOAD);
13941 : :
13942 : 4769036 : if (TREE_CODE (decl) == TEMPLATE_DECL)
13943 : : /* The template should have copied these from its result decl. */
13944 : 1702348 : gcc_checking_assert (DECL_MODULE_EXPORT_P (decl)
13945 : : == DECL_MODULE_EXPORT_P (DECL_TEMPLATE_RESULT (decl)));
13946 : :
13947 : 4769036 : depset **slot = entity_slot (decl, true);
13948 : 4769036 : depset *dep = *slot;
13949 : 4769036 : bool for_binding = ek == EK_FOR_BINDING;
13950 : :
13951 : 4769036 : if (!dep)
13952 : : {
13953 : 411458 : if ((DECL_IMPLICIT_TYPEDEF_P (decl)
13954 : : /* ... not an enum, for instance. */
13955 : 213050 : && RECORD_OR_UNION_TYPE_P (TREE_TYPE (decl))
13956 : 209026 : && TYPE_LANG_SPECIFIC (TREE_TYPE (decl))
13957 : 188586 : && CLASSTYPE_USE_TEMPLATE (TREE_TYPE (decl)) == 2)
13958 : 1411180 : || (VAR_P (decl)
13959 : 43250 : && DECL_LANG_SPECIFIC (decl)
13960 : 43176 : && DECL_USE_TEMPLATE (decl) == 2))
13961 : : {
13962 : : /* A partial or explicit specialization. Partial
13963 : : specializations might not be in the hash table, because
13964 : : there can be multiple differently-constrained variants.
13965 : :
13966 : : template<typename T> class silly;
13967 : : template<typename T> requires true class silly {};
13968 : :
13969 : : We need to find them, insert their TEMPLATE_DECL in the
13970 : : dep_hash, and then convert the dep we just found into a
13971 : : redirect. */
13972 : :
13973 : 34292 : tree ti = get_template_info (decl);
13974 : 34292 : tree tmpl = TI_TEMPLATE (ti);
13975 : 34292 : tree partial = NULL_TREE;
13976 : 34292 : for (tree spec = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
13977 : 129653 : spec; spec = TREE_CHAIN (spec))
13978 : 114775 : if (DECL_TEMPLATE_RESULT (TREE_VALUE (spec)) == decl)
13979 : : {
13980 : : partial = TREE_VALUE (spec);
13981 : : break;
13982 : : }
13983 : :
13984 : 34292 : if (partial)
13985 : : {
13986 : : /* Eagerly create an empty redirect. The following
13987 : : make_dependency call could cause hash reallocation,
13988 : : and invalidate slot's value. */
13989 : 19414 : depset *redirect = make_entity (decl, EK_REDIRECT);
13990 : :
13991 : : /* Redirects are never reached -- always snap to their target. */
13992 : 19414 : redirect->set_flag_bit<DB_UNREACHED_BIT> ();
13993 : :
13994 : 19414 : *slot = redirect;
13995 : :
13996 : 19414 : depset *tmpl_dep = make_dependency (partial, EK_PARTIAL);
13997 : 19414 : gcc_checking_assert (tmpl_dep->get_entity_kind () == EK_PARTIAL);
13998 : :
13999 : 19414 : redirect->deps.safe_push (tmpl_dep);
14000 : :
14001 : 19414 : return redirect;
14002 : : }
14003 : : }
14004 : :
14005 : 1010428 : bool has_def = ek != EK_USING && has_definition (decl);
14006 : 994917 : if (ek > EK_BINDING)
14007 : 131405 : ek = EK_DECL;
14008 : :
14009 : : /* The only OVERLOADS we should see are USING decls from
14010 : : bindings. */
14011 : 1010428 : *slot = dep = make_entity (decl, ek, has_def);
14012 : :
14013 : 1010428 : if (CHECKING_P && TREE_CODE (decl) == TEMPLATE_DECL)
14014 : : /* The template_result should otherwise not be in the
14015 : : table, or be an empty redirect (created above). */
14016 : 285012 : if (auto *eslot = entity_slot (DECL_TEMPLATE_RESULT (decl), false))
14017 : 19414 : gcc_checking_assert ((*eslot)->get_entity_kind () == EK_REDIRECT
14018 : : && !(*eslot)->deps.length ());
14019 : :
14020 : 1010428 : if (ek != EK_USING)
14021 : : {
14022 : 994917 : tree not_tmpl = STRIP_TEMPLATE (decl);
14023 : 994917 : bool imported_from_module_p = false;
14024 : :
14025 : 994917 : if (DECL_LANG_SPECIFIC (not_tmpl)
14026 : 1929539 : && DECL_MODULE_IMPORT_P (not_tmpl))
14027 : : {
14028 : : /* Store the module number and index in cluster/section,
14029 : : so we don't have to look them up again. */
14030 : 2152 : unsigned index = import_entity_index (decl);
14031 : 2152 : module_state *from = import_entity_module (index);
14032 : : /* Remap will be zero for imports from partitions, which
14033 : : we want to treat as-if declared in this TU. */
14034 : 2152 : if (from->remap)
14035 : : {
14036 : 1755 : dep->cluster = index - from->entity_lwm;
14037 : 1755 : dep->section = from->remap;
14038 : 1755 : dep->set_flag_bit<DB_IMPORTED_BIT> ();
14039 : :
14040 : 1755 : if (!from->is_header ())
14041 : 994917 : imported_from_module_p = true;
14042 : : }
14043 : : }
14044 : :
14045 : : /* Check for TU-local entities. This is unnecessary in header
14046 : : units because we can export internal-linkage decls, and
14047 : : no declarations are exposures. Similarly, if the decl was
14048 : : imported from a non-header module we know it cannot have
14049 : : been TU-local. */
14050 : 994917 : if (!header_module_p () && !imported_from_module_p)
14051 : : {
14052 : 286243 : if (is_tu_local_entity (decl))
14053 : 192 : dep->set_flag_bit<DB_TU_LOCAL_BIT> ();
14054 : :
14055 : 286243 : if (VAR_P (decl)
14056 : 12473 : && decl_maybe_constant_var_p (decl)
14057 : 298089 : && is_tu_local_value (decl, DECL_INITIAL (decl)))
14058 : : {
14059 : : /* A potentially-constant variable initialized to a TU-local
14060 : : value is not usable in constant expressions within other
14061 : : translation units. We can achieve this by simply not
14062 : : streaming the definition in such cases. */
14063 : 15 : dep->clear_flag_bit<DB_DEFN_BIT> ();
14064 : :
14065 : 15 : if (DECL_DECLARED_CONSTEXPR_P (decl))
14066 : : /* Also, a constexpr variable initialized to a TU-local
14067 : : value is an exposure. */
14068 : 9 : dep->set_flag_bit<DB_EXPOSURE_BIT> ();
14069 : : }
14070 : : }
14071 : :
14072 : : /* A namespace-scope type may be declared in one module unit
14073 : : and defined in another; make sure that we're found when
14074 : : completing the class. */
14075 : 994917 : if (ek == EK_DECL
14076 : 433562 : && !dep->is_import ()
14077 : 433087 : && dep->has_defn ()
14078 : 211457 : && DECL_NAMESPACE_SCOPE_P (not_tmpl)
14079 : 85658 : && DECL_IMPLICIT_TYPEDEF_P (not_tmpl)
14080 : : /* Anonymous types can't be forward-declared. */
14081 : 1020269 : && !IDENTIFIER_ANON_P (DECL_NAME (not_tmpl)))
14082 : 24940 : dep->set_flag_bit<DB_IS_PENDING_BIT> ();
14083 : : }
14084 : :
14085 : 1010428 : if (!dep->is_import ())
14086 : 1008673 : worklist.safe_push (dep);
14087 : : }
14088 : :
14089 : 4749622 : dump (dumper::DEPEND)
14090 : 33479 : && dump ("%s on %s %C:%N found",
14091 : : ek == EK_REDIRECT ? "Redirect"
14092 : 33479 : : for_binding ? "Binding" : "Dependency",
14093 : 33479 : dep->entity_kind_name (), TREE_CODE (decl), decl);
14094 : :
14095 : 4749622 : return dep;
14096 : : }
14097 : :
14098 : : /* Whether REF is an exposure of a member type of SOURCE.
14099 : :
14100 : : This comes up with exposures of class-scope lambdas, that we currently
14101 : : treat as TU-local due to ABI reasons. In such a case the type of the
14102 : : lambda will be exposed in two places, first by the class type it is in
14103 : : the TYPE_FIELDS list of, and second by the actual member declaring that
14104 : : lambda. We only want the second case to warn. */
14105 : :
14106 : : static bool
14107 : 149 : is_exposure_of_member_type (depset *source, depset *ref)
14108 : : {
14109 : 298 : gcc_checking_assert (source->refs_tu_local () && ref->is_tu_local ());
14110 : 149 : tree source_entity = STRIP_TEMPLATE (source->get_entity ());
14111 : 149 : tree ref_entity = STRIP_TEMPLATE (ref->get_entity ());
14112 : :
14113 : 149 : if (source_entity
14114 : 149 : && ref_entity
14115 : 149 : && DECL_IMPLICIT_TYPEDEF_P (source_entity)
14116 : 2 : && DECL_IMPLICIT_TYPEDEF_P (ref_entity)
14117 : 2 : && DECL_CLASS_SCOPE_P (ref_entity)
14118 : 151 : && DECL_CONTEXT (ref_entity) == TREE_TYPE (source_entity))
14119 : : {
14120 : 4 : gcc_checking_assert (LAMBDA_TYPE_P (TREE_TYPE (ref_entity)));
14121 : : return true;
14122 : : }
14123 : : else
14124 : : return false;
14125 : : }
14126 : :
14127 : : /* DEP is a newly discovered dependency. Append it to current's
14128 : : depset. */
14129 : :
14130 : : void
14131 : 3516938 : depset::hash::add_dependency (depset *dep)
14132 : : {
14133 : 3516938 : gcc_checking_assert (current && !is_key_order ());
14134 : 3516938 : current->deps.safe_push (dep);
14135 : :
14136 : 3516938 : if (dep->is_tu_local ())
14137 : : {
14138 : 245 : current->set_flag_bit<DB_REFS_TU_LOCAL_BIT> ();
14139 : 245 : if (!ignore_tu_local && !is_exposure_of_member_type (current, dep))
14140 : 75 : current->set_flag_bit<DB_EXPOSURE_BIT> ();
14141 : : }
14142 : :
14143 : 3516938 : if (current->get_entity_kind () == EK_USING
14144 : 15511 : && DECL_IMPLICIT_TYPEDEF_P (dep->get_entity ())
14145 : 3522861 : && TREE_CODE (TREE_TYPE (dep->get_entity ())) == ENUMERAL_TYPE)
14146 : : {
14147 : : /* CURRENT is an unwrapped using-decl and DECL is an enum's
14148 : : implicit typedef. Is CURRENT a member of the enum? */
14149 : 5800 : tree c_decl = OVL_FUNCTION (current->get_entity ());
14150 : :
14151 : 5800 : if (TREE_CODE (c_decl) == CONST_DECL
14152 : 11589 : && (current->deps[0]->get_entity ()
14153 : 5789 : == CP_DECL_CONTEXT (dep->get_entity ())))
14154 : : /* Make DECL depend on CURRENT. */
14155 : 5744 : dep->deps.safe_push (current);
14156 : : }
14157 : :
14158 : : /* If two dependencies recursively depend on each other existing within
14159 : : their own merge keys, we must ensure that the first dep we saw while
14160 : : walking is written first in this cluster. See sort_cluster for more
14161 : : details. */
14162 : 3516938 : if (writing_merge_key)
14163 : : {
14164 : 629118 : dep->set_flag_bit<DB_MAYBE_RECURSIVE_BIT> ();
14165 : 629118 : if (!current->is_maybe_recursive ())
14166 : 594288 : current->set_flag_bit<DB_ENTRY_BIT> ();
14167 : : }
14168 : :
14169 : 3516938 : if (dep->is_unreached ())
14170 : : {
14171 : : /* The dependency is reachable now. */
14172 : 275180 : reached_unreached = true;
14173 : 275180 : dep->clear_flag_bit<DB_UNREACHED_BIT> ();
14174 : 275180 : dump (dumper::DEPEND)
14175 : 24 : && dump ("Reaching unreached %s %C:%N", dep->entity_kind_name (),
14176 : 24 : TREE_CODE (dep->get_entity ()), dep->get_entity ());
14177 : : }
14178 : 3516938 : }
14179 : :
14180 : : depset *
14181 : 5302777 : depset::hash::add_dependency (tree decl, entity_kind ek)
14182 : : {
14183 : 5302777 : depset *dep;
14184 : :
14185 : 5302777 : if (is_key_order ())
14186 : : {
14187 : 1743613 : dep = find_dependency (decl);
14188 : 1743613 : if (dep)
14189 : : {
14190 : 842563 : current->deps.safe_push (dep);
14191 : 842563 : dump (dumper::MERGE)
14192 : 471 : && dump ("Key dependency on %s %C:%N found",
14193 : 471 : dep->entity_kind_name (), TREE_CODE (decl), decl);
14194 : : }
14195 : : else
14196 : : {
14197 : : /* It's not a mergeable decl, look for it in the original
14198 : : table. */
14199 : 901050 : dep = chain->find_dependency (decl);
14200 : 901050 : gcc_checking_assert (dep);
14201 : : }
14202 : : }
14203 : : else
14204 : : {
14205 : 3559164 : dep = make_dependency (decl, ek);
14206 : 3559164 : if (dep->get_entity_kind () != EK_REDIRECT)
14207 : 3516938 : add_dependency (dep);
14208 : : }
14209 : :
14210 : 5302777 : return dep;
14211 : : }
14212 : :
14213 : : void
14214 : 467032 : depset::hash::add_namespace_context (depset *dep, tree ns)
14215 : : {
14216 : 467032 : depset *ns_dep = make_dependency (ns, depset::EK_NAMESPACE);
14217 : 467032 : dep->deps.safe_push (ns_dep);
14218 : :
14219 : : /* Mark it as special if imported so we don't walk connect when
14220 : : SCCing. */
14221 : 467032 : if (!dep->is_binding () && ns_dep->is_import ())
14222 : 0 : dep->set_special ();
14223 : 467032 : }
14224 : :
14225 : : struct add_binding_data
14226 : : {
14227 : : tree ns;
14228 : : bitmap partitions;
14229 : : depset *binding;
14230 : : depset::hash *hash;
14231 : : bool met_namespace;
14232 : : };
14233 : :
14234 : : /* Return true if we are, or contain something that is exported. */
14235 : :
14236 : : bool
14237 : 4655247 : depset::hash::add_binding_entity (tree decl, WMB_Flags flags, void *data_)
14238 : : {
14239 : 4655247 : auto data = static_cast <add_binding_data *> (data_);
14240 : 4655247 : decl = strip_using_decl (decl);
14241 : :
14242 : 4655247 : if (!(TREE_CODE (decl) == NAMESPACE_DECL && !DECL_NAMESPACE_ALIAS (decl)))
14243 : : {
14244 : 4648507 : tree inner = decl;
14245 : :
14246 : 4648507 : if (TREE_CODE (inner) == CONST_DECL
14247 : 7210 : && TREE_CODE (DECL_CONTEXT (inner)) == ENUMERAL_TYPE
14248 : : /* A using-decl could make a CONST_DECL purview for a non-purview
14249 : : enumeration. */
14250 : 4655717 : && (!DECL_LANG_SPECIFIC (inner) || !DECL_MODULE_PURVIEW_P (inner)))
14251 : 7207 : inner = TYPE_NAME (DECL_CONTEXT (inner));
14252 : 4641300 : else if (TREE_CODE (inner) == TEMPLATE_DECL)
14253 : 92314 : inner = DECL_TEMPLATE_RESULT (inner);
14254 : :
14255 : 9148646 : if ((!DECL_LANG_SPECIFIC (inner) || !DECL_MODULE_PURVIEW_P (inner))
14256 : 9001727 : && !((flags & WMB_Using) && (flags & WMB_Purview)))
14257 : : /* Ignore entities not within the module purview. */
14258 : : return false;
14259 : :
14260 : 147009 : if (!header_module_p () && data->hash->is_tu_local_entity (decl))
14261 : : /* Ignore TU-local entitites. */
14262 : : return false;
14263 : :
14264 : 146809 : if ((TREE_CODE (decl) == VAR_DECL
14265 : 146809 : || TREE_CODE (decl) == TYPE_DECL)
14266 : 146809 : && DECL_TINFO_P (decl))
14267 : : /* Ignore TINFO things. */
14268 : : return false;
14269 : :
14270 : 146809 : if (TREE_CODE (decl) == VAR_DECL && DECL_NTTP_OBJECT_P (decl))
14271 : : /* Ignore NTTP objects. */
14272 : : return false;
14273 : :
14274 : 146809 : if (deduction_guide_p (decl))
14275 : : {
14276 : : /* Ignore deduction guides, bindings for them will be created within
14277 : : find_dependencies for their class template. But still build a dep
14278 : : for them so that we don't discard them. */
14279 : 1269 : data->hash->make_dependency (decl, EK_FOR_BINDING);
14280 : 1269 : return false;
14281 : : }
14282 : :
14283 : 145540 : if (!(flags & WMB_Using) && CP_DECL_CONTEXT (decl) != data->ns)
14284 : : {
14285 : : /* An unscoped enum constant implicitly brought into the containing
14286 : : namespace. We treat this like a using-decl. */
14287 : 5744 : gcc_checking_assert (TREE_CODE (decl) == CONST_DECL);
14288 : :
14289 : 5744 : flags = WMB_Flags (flags | WMB_Using);
14290 : 5744 : if (DECL_MODULE_EXPORT_P (TYPE_NAME (TREE_TYPE (decl)))
14291 : : /* A using-decl can make an enum constant exported for a
14292 : : non-exported enumeration. */
14293 : 5744 : || (DECL_LANG_SPECIFIC (decl) && DECL_MODULE_EXPORT_P (decl)))
14294 : 5161 : flags = WMB_Flags (flags | WMB_Export);
14295 : : }
14296 : :
14297 : 145540 : if (!data->binding)
14298 : : /* No binding to check. */;
14299 : 27500 : else if (flags & WMB_Using)
14300 : : {
14301 : : /* Look in the binding to see if we already have this
14302 : : using. */
14303 : 18220 : for (unsigned ix = data->binding->deps.length (); --ix;)
14304 : : {
14305 : 14204 : depset *d = data->binding->deps[ix];
14306 : 28408 : if (d->get_entity_kind () == EK_USING
14307 : 14204 : && OVL_FUNCTION (d->get_entity ()) == decl)
14308 : : {
14309 : 0 : if (!(flags & WMB_Hidden))
14310 : 0 : d->clear_hidden_binding ();
14311 : 0 : OVL_PURVIEW_P (d->get_entity ()) = true;
14312 : 0 : if (flags & WMB_Export)
14313 : 0 : OVL_EXPORT_P (d->get_entity ()) = true;
14314 : 0 : return bool (flags & WMB_Export);
14315 : : }
14316 : : }
14317 : : }
14318 : 25492 : else if (flags & WMB_Dups)
14319 : : {
14320 : : /* Look in the binding to see if we already have this decl. */
14321 : 78 : for (unsigned ix = data->binding->deps.length (); --ix;)
14322 : : {
14323 : 39 : depset *d = data->binding->deps[ix];
14324 : 39 : if (d->get_entity () == decl)
14325 : : {
14326 : 33 : if (!(flags & WMB_Hidden))
14327 : 30 : d->clear_hidden_binding ();
14328 : 33 : return false;
14329 : : }
14330 : : }
14331 : : }
14332 : :
14333 : : /* We're adding something. */
14334 : 145507 : if (!data->binding)
14335 : : {
14336 : 118040 : data->binding = make_binding (data->ns, DECL_NAME (decl));
14337 : 118040 : data->hash->add_namespace_context (data->binding, data->ns);
14338 : :
14339 : 118040 : depset **slot = data->hash->binding_slot (data->ns,
14340 : 118040 : DECL_NAME (decl), true);
14341 : 118040 : gcc_checking_assert (!*slot);
14342 : 118040 : *slot = data->binding;
14343 : : }
14344 : :
14345 : : /* Make sure nobody left a tree visited lying about. */
14346 : 145507 : gcc_checking_assert (!TREE_VISITED (decl));
14347 : :
14348 : 145507 : if (flags & WMB_Using)
14349 : : {
14350 : 15511 : decl = ovl_make (decl, NULL_TREE);
14351 : 15511 : OVL_USING_P (decl) = true;
14352 : 15511 : OVL_PURVIEW_P (decl) = true;
14353 : 15511 : if (flags & WMB_Export)
14354 : 14348 : OVL_EXPORT_P (decl) = true;
14355 : : }
14356 : :
14357 : 145507 : depset *dep = data->hash->make_dependency
14358 : 275503 : (decl, flags & WMB_Using ? EK_USING : EK_FOR_BINDING);
14359 : 145507 : if (flags & WMB_Hidden)
14360 : 3650 : dep->set_hidden_binding ();
14361 : 145507 : data->binding->deps.safe_push (dep);
14362 : : /* Binding and contents are mutually dependent. */
14363 : 145507 : dep->deps.safe_push (data->binding);
14364 : :
14365 : 145507 : return (flags & WMB_Using
14366 : 145507 : ? flags & WMB_Export : DECL_MODULE_EXPORT_P (decl));
14367 : : }
14368 : 6740 : else if (!data->met_namespace)
14369 : : {
14370 : : /* Namespace, walk exactly once. */
14371 : 6731 : data->met_namespace = true;
14372 : 6731 : if (data->hash->add_namespace_entities (decl, data->partitions))
14373 : : {
14374 : : /* It contains an exported thing, so it is exported. */
14375 : 1281 : gcc_checking_assert (DECL_MODULE_PURVIEW_P (decl));
14376 : 1281 : gcc_checking_assert (TREE_PUBLIC (decl) || header_module_p ());
14377 : 1281 : DECL_MODULE_EXPORT_P (decl) = true;
14378 : : }
14379 : :
14380 : 6731 : if (DECL_MODULE_PURVIEW_P (decl))
14381 : : {
14382 : 1556 : data->hash->make_dependency (decl, depset::EK_NAMESPACE);
14383 : :
14384 : 1556 : return DECL_MODULE_EXPORT_P (decl);
14385 : : }
14386 : : }
14387 : :
14388 : : return false;
14389 : : }
14390 : :
14391 : : /* Recursively find all the namespace bindings of NS. Add a depset
14392 : : for every binding that contains an export or module-linkage entity.
14393 : : Add a defining depset for every such decl that we need to write a
14394 : : definition. Such defining depsets depend on the binding depset.
14395 : : Returns true if we contain something exported. */
14396 : :
14397 : : bool
14398 : 9058 : depset::hash::add_namespace_entities (tree ns, bitmap partitions)
14399 : : {
14400 : 10002 : dump () && dump ("Looking for writables in %N", ns);
14401 : 9058 : dump.indent ();
14402 : :
14403 : 9058 : unsigned count = 0;
14404 : 9058 : add_binding_data data;
14405 : 9058 : data.ns = ns;
14406 : 9058 : data.partitions = partitions;
14407 : 9058 : data.hash = this;
14408 : :
14409 : 9058 : hash_table<named_decl_hash>::iterator end
14410 : 9058 : (DECL_NAMESPACE_BINDINGS (ns)->end ());
14411 : 6142028 : for (hash_table<named_decl_hash>::iterator iter
14412 : 12284056 : (DECL_NAMESPACE_BINDINGS (ns)->begin ()); iter != end; ++iter)
14413 : : {
14414 : 6132970 : data.binding = nullptr;
14415 : 6132970 : data.met_namespace = false;
14416 : 6132970 : if (walk_module_binding (*iter, partitions, add_binding_entity, &data))
14417 : 110357 : count++;
14418 : : }
14419 : :
14420 : 9058 : if (count)
14421 : 3233 : dump () && dump ("Found %u entries", count);
14422 : 9058 : dump.outdent ();
14423 : :
14424 : 9058 : return count != 0;
14425 : : }
14426 : :
14427 : : void
14428 : 181 : depset::hash::add_partial_entities (vec<tree, va_gc> *partial_classes)
14429 : : {
14430 : 17171 : for (unsigned ix = 0; ix != partial_classes->length (); ix++)
14431 : : {
14432 : 16990 : tree inner = (*partial_classes)[ix];
14433 : :
14434 : 16990 : depset *dep = make_dependency (inner, depset::EK_DECL);
14435 : :
14436 : 16990 : if (dep->get_entity_kind () == depset::EK_REDIRECT)
14437 : : {
14438 : 16990 : dep = dep->deps[0];
14439 : : /* We should have recorded the template as a partial
14440 : : specialization. */
14441 : 16990 : gcc_checking_assert (dep->get_entity_kind ()
14442 : : == depset::EK_PARTIAL);
14443 : :
14444 : : /* Only emit GM entities if reached. */
14445 : 16990 : if (!DECL_LANG_SPECIFIC (inner)
14446 : 28696 : || !DECL_MODULE_PURVIEW_P (inner))
14447 : 5320 : dep->set_flag_bit<DB_UNREACHED_BIT> ();
14448 : : }
14449 : : else
14450 : : {
14451 : : /* It was an explicit specialization, not a partial one.
14452 : : We should have already added this. */
14453 : 0 : gcc_checking_assert (dep->get_entity_kind ()
14454 : : == depset::EK_SPECIALIZATION);
14455 : 0 : gcc_checking_assert (dep->is_special ());
14456 : : }
14457 : : }
14458 : 181 : }
14459 : :
14460 : : /* Add the members of imported classes that we defined in this TU.
14461 : : This will also include lazily created implicit member function
14462 : : declarations. (All others will be definitions.) */
14463 : :
14464 : : void
14465 : 12 : depset::hash::add_class_entities (vec<tree, va_gc> *class_members)
14466 : : {
14467 : 24 : for (unsigned ix = 0; ix != class_members->length (); ix++)
14468 : : {
14469 : 12 : tree defn = (*class_members)[ix];
14470 : 12 : depset *dep = make_dependency (defn, EK_INNER_DECL);
14471 : :
14472 : 12 : if (dep->get_entity_kind () == EK_REDIRECT)
14473 : 0 : dep = dep->deps[0];
14474 : :
14475 : : /* Only non-instantiations need marking as pendings. */
14476 : 24 : if (dep->get_entity_kind () == EK_DECL)
14477 : 12 : dep->set_flag_bit <DB_IS_PENDING_BIT> ();
14478 : : }
14479 : 12 : }
14480 : :
14481 : : /* We add the partial & explicit specializations, and the explicit
14482 : : instantiations. */
14483 : :
14484 : : static void
14485 : 558703 : specialization_add (bool decl_p, spec_entry *entry, void *data_)
14486 : : {
14487 : 558703 : vec<spec_entry *> *data = reinterpret_cast <vec<spec_entry *> *> (data_);
14488 : :
14489 : 558703 : if (!decl_p)
14490 : : {
14491 : : /* We exclusively use decls to locate things. Make sure there's
14492 : : no mismatch between the two specialization tables we keep.
14493 : : pt.cc optimizes instantiation lookup using a complicated
14494 : : heuristic. We don't attempt to replicate that algorithm, but
14495 : : observe its behaviour and reproduce it upon read back. */
14496 : :
14497 : 180329 : gcc_checking_assert (TREE_CODE (entry->spec) == ENUMERAL_TYPE
14498 : : || DECL_CLASS_TEMPLATE_P (entry->tmpl));
14499 : :
14500 : 180329 : gcc_checking_assert (!match_mergeable_specialization (true, entry));
14501 : : }
14502 : 378374 : else if (VAR_OR_FUNCTION_DECL_P (entry->spec))
14503 : 190175 : gcc_checking_assert (!DECL_LOCAL_DECL_P (entry->spec));
14504 : :
14505 : 558703 : data->safe_push (entry);
14506 : 558703 : }
14507 : :
14508 : : /* Arbitrary stable comparison. */
14509 : :
14510 : : static int
14511 : 32829944 : specialization_cmp (const void *a_, const void *b_)
14512 : : {
14513 : 32829944 : const spec_entry *ea = *reinterpret_cast<const spec_entry *const *> (a_);
14514 : 32829944 : const spec_entry *eb = *reinterpret_cast<const spec_entry *const *> (b_);
14515 : :
14516 : 32829944 : if (ea == eb)
14517 : : return 0;
14518 : :
14519 : 32829944 : tree a = ea->spec;
14520 : 32829944 : tree b = eb->spec;
14521 : 32829944 : if (TYPE_P (a))
14522 : : {
14523 : 10075472 : a = TYPE_NAME (a);
14524 : 10075472 : b = TYPE_NAME (b);
14525 : : }
14526 : :
14527 : 32829944 : if (a == b)
14528 : : /* This can happen with friend specializations. Just order by
14529 : : entry address. See note in depset_cmp. */
14530 : 166 : return ea < eb ? -1 : +1;
14531 : :
14532 : 32829826 : return DECL_UID (a) < DECL_UID (b) ? -1 : +1;
14533 : : }
14534 : :
14535 : : /* We add all kinds of specialializations. Implicit specializations
14536 : : should only streamed and walked if they are reachable from
14537 : : elsewhere. Hence the UNREACHED flag. This is making the
14538 : : assumption that it is cheaper to reinstantiate them on demand
14539 : : elsewhere, rather than stream them in when we instantiate their
14540 : : general template. Also, if we do stream them, we can only do that
14541 : : if they are not internal (which they can become if they themselves
14542 : : touch an internal entity?). */
14543 : :
14544 : : void
14545 : 4654 : depset::hash::add_specializations (bool decl_p)
14546 : : {
14547 : 4654 : vec<spec_entry *> data;
14548 : 4654 : data.create (100);
14549 : 4654 : walk_specializations (decl_p, specialization_add, &data);
14550 : 4654 : data.qsort (specialization_cmp);
14551 : 563357 : while (data.length ())
14552 : : {
14553 : 558703 : spec_entry *entry = data.pop ();
14554 : 558703 : tree spec = entry->spec;
14555 : 558703 : int use_tpl = 0;
14556 : 558703 : bool is_friend = false;
14557 : :
14558 : 558703 : if (decl_p && DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (entry->tmpl))
14559 : : /* A friend of a template. This is keyed to the
14560 : : instantiation. */
14561 : : is_friend = true;
14562 : :
14563 : 558703 : if (decl_p)
14564 : : {
14565 : 378374 : if (tree ti = DECL_TEMPLATE_INFO (spec))
14566 : : {
14567 : 378374 : tree tmpl = TI_TEMPLATE (ti);
14568 : :
14569 : 378374 : use_tpl = DECL_USE_TEMPLATE (spec);
14570 : 378374 : if (spec == DECL_TEMPLATE_RESULT (tmpl))
14571 : : {
14572 : 2719 : spec = tmpl;
14573 : 2719 : gcc_checking_assert (DECL_USE_TEMPLATE (spec) == use_tpl);
14574 : : }
14575 : 375655 : else if (is_friend)
14576 : : {
14577 : 2008 : if (TI_TEMPLATE (ti) != entry->tmpl
14578 : 2008 : || !template_args_equal (TI_ARGS (ti), entry->tmpl))
14579 : 2008 : goto template_friend;
14580 : : }
14581 : : }
14582 : : else
14583 : : {
14584 : 0 : template_friend:;
14585 : 2008 : gcc_checking_assert (is_friend);
14586 : : /* This is a friend of a template class, but not the one
14587 : : that generated entry->spec itself (i.e. it's an
14588 : : equivalent clone). We do not need to record
14589 : : this. */
14590 : 2008 : continue;
14591 : : }
14592 : : }
14593 : : else
14594 : : {
14595 : 180329 : if (TREE_CODE (spec) == ENUMERAL_TYPE)
14596 : : {
14597 : 1132 : tree ctx = DECL_CONTEXT (TYPE_NAME (spec));
14598 : :
14599 : 1132 : if (TYPE_P (ctx))
14600 : 1126 : use_tpl = CLASSTYPE_USE_TEMPLATE (ctx);
14601 : : else
14602 : 6 : use_tpl = DECL_USE_TEMPLATE (ctx);
14603 : : }
14604 : : else
14605 : 179197 : use_tpl = CLASSTYPE_USE_TEMPLATE (spec);
14606 : :
14607 : 180329 : tree ti = TYPE_TEMPLATE_INFO (spec);
14608 : 180329 : tree tmpl = TI_TEMPLATE (ti);
14609 : :
14610 : 180329 : spec = TYPE_NAME (spec);
14611 : 180329 : if (spec == DECL_TEMPLATE_RESULT (tmpl))
14612 : : {
14613 : 760 : spec = tmpl;
14614 : 760 : use_tpl = DECL_USE_TEMPLATE (spec);
14615 : : }
14616 : : }
14617 : :
14618 : 556695 : bool needs_reaching = false;
14619 : 556695 : if (use_tpl == 1)
14620 : : /* Implicit instantiations only walked if we reach them. */
14621 : : needs_reaching = true;
14622 : 58427 : else if (!DECL_LANG_SPECIFIC (STRIP_TEMPLATE (spec))
14623 : 106586 : || !DECL_MODULE_PURVIEW_P (STRIP_TEMPLATE (spec)))
14624 : : /* Likewise, GMF explicit or partial specializations. */
14625 : : needs_reaching = true;
14626 : :
14627 : : #if false && CHECKING_P
14628 : : /* The instantiation isn't always on
14629 : : DECL_TEMPLATE_INSTANTIATIONS, */
14630 : : // FIXME: we probably need to remember this information?
14631 : : /* Verify the specialization is on the
14632 : : DECL_TEMPLATE_INSTANTIATIONS of the template. */
14633 : : for (tree cons = DECL_TEMPLATE_INSTANTIATIONS (entry->tmpl);
14634 : : cons; cons = TREE_CHAIN (cons))
14635 : : if (TREE_VALUE (cons) == entry->spec)
14636 : : {
14637 : : gcc_assert (entry->args == TREE_PURPOSE (cons));
14638 : : goto have_spec;
14639 : : }
14640 : : gcc_unreachable ();
14641 : : have_spec:;
14642 : : #endif
14643 : :
14644 : : /* Make sure nobody left a tree visited lying about. */
14645 : 556695 : gcc_checking_assert (!TREE_VISITED (spec));
14646 : 556695 : depset *dep = make_dependency (spec, depset::EK_SPECIALIZATION);
14647 : 556695 : if (dep->is_special ())
14648 : 0 : gcc_unreachable ();
14649 : : else
14650 : : {
14651 : 556695 : if (dep->get_entity_kind () == depset::EK_REDIRECT)
14652 : 18726 : dep = dep->deps[0];
14653 : 537969 : else if (dep->get_entity_kind () == depset::EK_SPECIALIZATION)
14654 : : {
14655 : 537969 : dep->set_special ();
14656 : 537969 : dep->deps.safe_push (reinterpret_cast<depset *> (entry));
14657 : 537969 : if (!decl_p)
14658 : 164027 : dep->set_flag_bit<DB_TYPE_SPEC_BIT> ();
14659 : : }
14660 : :
14661 : 556695 : if (needs_reaching)
14662 : 515846 : dep->set_flag_bit<DB_UNREACHED_BIT> ();
14663 : 556695 : if (is_friend)
14664 : 0 : dep->set_flag_bit<DB_FRIEND_SPEC_BIT> ();
14665 : : }
14666 : : }
14667 : 4654 : data.release ();
14668 : 4654 : }
14669 : :
14670 : : /* Add a depset into the mergeable hash. */
14671 : :
14672 : : void
14673 : 754148 : depset::hash::add_mergeable (depset *mergeable)
14674 : : {
14675 : 754148 : gcc_checking_assert (is_key_order ());
14676 : 754148 : entity_kind ek = mergeable->get_entity_kind ();
14677 : 754148 : tree decl = mergeable->get_entity ();
14678 : 754148 : gcc_checking_assert (ek < EK_DIRECT_HWM);
14679 : :
14680 : 754148 : depset **slot = entity_slot (decl, true);
14681 : 754148 : gcc_checking_assert (!*slot);
14682 : 754148 : depset *dep = make_entity (decl, ek);
14683 : 754148 : *slot = dep;
14684 : :
14685 : 754148 : worklist.safe_push (dep);
14686 : :
14687 : : /* So we can locate the mergeable depset this depset refers to,
14688 : : mark the first dep. */
14689 : 754148 : dep->set_special ();
14690 : 754148 : dep->deps.safe_push (mergeable);
14691 : 754148 : }
14692 : :
14693 : : /* Find the innermost-namespace scope of DECL, and that
14694 : : namespace-scope decl. */
14695 : :
14696 : : tree
14697 : 24011853 : find_pending_key (tree decl, tree *decl_p = nullptr)
14698 : : {
14699 : 24011853 : tree ns = decl;
14700 : 28615329 : do
14701 : : {
14702 : 28615329 : decl = ns;
14703 : 28615329 : ns = CP_DECL_CONTEXT (ns);
14704 : 28615329 : if (TYPE_P (ns))
14705 : 2906984 : ns = TYPE_NAME (ns);
14706 : : }
14707 : 28615329 : while (TREE_CODE (ns) != NAMESPACE_DECL);
14708 : :
14709 : 24011853 : if (decl_p)
14710 : 23665304 : *decl_p = decl;
14711 : :
14712 : 24011853 : return ns;
14713 : : }
14714 : :
14715 : : /* Creates bindings and dependencies for all deduction guides of
14716 : : the given class template DECL as needed. */
14717 : :
14718 : : void
14719 : 39342 : depset::hash::add_deduction_guides (tree decl)
14720 : : {
14721 : : /* Alias templates never have deduction guides. */
14722 : 39342 : if (DECL_ALIAS_TEMPLATE_P (decl))
14723 : 38774 : return;
14724 : :
14725 : : /* We don't need to do anything for class-scope deduction guides,
14726 : : as they will be added as members anyway. */
14727 : 39342 : if (!DECL_NAMESPACE_SCOPE_P (decl))
14728 : : return;
14729 : :
14730 : 31853 : tree ns = CP_DECL_CONTEXT (decl);
14731 : 31853 : tree name = dguide_name (decl);
14732 : :
14733 : : /* We always add all deduction guides with a given name at once,
14734 : : so if there's already a binding there's nothing to do. */
14735 : 31853 : if (find_binding (ns, name))
14736 : : return;
14737 : :
14738 : 31748 : tree guides = lookup_qualified_name (ns, name, LOOK_want::NORMAL,
14739 : : /*complain=*/false);
14740 : 31748 : if (guides == error_mark_node)
14741 : : return;
14742 : :
14743 : 568 : depset *binding = nullptr;
14744 : 1965 : for (tree t : lkp_range (guides))
14745 : : {
14746 : 1397 : gcc_checking_assert (!TREE_VISITED (t));
14747 : 1397 : depset *dep = make_dependency (t, EK_FOR_BINDING);
14748 : :
14749 : : /* We don't want to create bindings for imported deduction guides, as
14750 : : this would potentially cause name lookup to return duplicates. */
14751 : 1397 : if (dep->is_import ())
14752 : 6 : continue;
14753 : :
14754 : 1391 : if (!binding)
14755 : : {
14756 : : /* We have bindings to add. */
14757 : 562 : binding = make_binding (ns, name);
14758 : 562 : add_namespace_context (binding, ns);
14759 : :
14760 : 562 : depset **slot = binding_slot (ns, name, /*insert=*/true);
14761 : 562 : *slot = binding;
14762 : : }
14763 : :
14764 : 1391 : binding->deps.safe_push (dep);
14765 : 1391 : dep->deps.safe_push (binding);
14766 : : }
14767 : : }
14768 : :
14769 : : /* Iteratively find dependencies. During the walk we may find more
14770 : : entries on the same binding that need walking. */
14771 : :
14772 : : void
14773 : 223368 : depset::hash::find_dependencies (module_state *module)
14774 : : {
14775 : 223368 : trees_out walker (NULL, module, *this);
14776 : 223368 : vec<depset *> unreached;
14777 : 446736 : unreached.create (worklist.length ());
14778 : :
14779 : 959 : for (;;)
14780 : : {
14781 : 224327 : reached_unreached = false;
14782 : 3090652 : while (worklist.length ())
14783 : : {
14784 : 2866325 : depset *item = worklist.pop ();
14785 : :
14786 : 2866325 : gcc_checking_assert (!item->is_binding ());
14787 : 2866325 : if (item->is_unreached ())
14788 : 1338010 : unreached.quick_push (item);
14789 : : else
14790 : : {
14791 : 1528315 : current = item;
14792 : 1528315 : tree decl = current->get_entity ();
14793 : 1528315 : dump (is_key_order () ? dumper::MERGE : dumper::DEPEND)
14794 : 1529269 : && dump ("Dependencies of %s %C:%N",
14795 : 954 : is_key_order () ? "key-order"
14796 : 954 : : current->entity_kind_name (), TREE_CODE (decl), decl);
14797 : 1528315 : dump.indent ();
14798 : 1528315 : walker.begin ();
14799 : 1528315 : if (current->get_entity_kind () == EK_USING)
14800 : 15511 : walker.tree_node (OVL_FUNCTION (decl));
14801 : 1512804 : else if (TREE_VISITED (decl))
14802 : : /* A global tree. */;
14803 : 1510713 : else if (item->get_entity_kind () == EK_NAMESPACE)
14804 : : {
14805 : 1881 : module->note_location (DECL_SOURCE_LOCATION (decl));
14806 : 1881 : add_namespace_context (current, CP_DECL_CONTEXT (decl));
14807 : : }
14808 : : else
14809 : : {
14810 : 1508832 : walker.mark_declaration (decl, current->has_defn ());
14811 : :
14812 : 1508832 : if (!is_key_order ()
14813 : 1508832 : && item->is_pending_entity ())
14814 : : {
14815 : 346549 : tree ns = find_pending_key (decl, nullptr);
14816 : 346549 : add_namespace_context (item, ns);
14817 : : }
14818 : :
14819 : 1508832 : walker.decl_value (decl, current);
14820 : 1508832 : if (current->has_defn ())
14821 : 278934 : walker.write_definition (decl, current->refs_tu_local ());
14822 : : }
14823 : 1528315 : walker.end ();
14824 : :
14825 : 1528315 : if (!is_key_order ()
14826 : 1528315 : && DECL_CLASS_TEMPLATE_P (decl))
14827 : 39342 : add_deduction_guides (decl);
14828 : :
14829 : 1528315 : if (!is_key_order ()
14830 : 774167 : && TREE_CODE (decl) == TEMPLATE_DECL
14831 : 1802823 : && !DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (decl))
14832 : : {
14833 : : /* Mark all the explicit & partial specializations as
14834 : : reachable. We search both specialization lists as some
14835 : : constrained partial specializations for class types are
14836 : : only found in DECL_TEMPLATE_SPECIALIZATIONS. */
14837 : 743728 : auto mark_reached = [this](tree spec)
14838 : : {
14839 : 473400 : if (TYPE_P (spec))
14840 : 145771 : spec = TYPE_NAME (spec);
14841 : 473400 : int use_tpl;
14842 : 473400 : node_template_info (spec, use_tpl);
14843 : 473400 : if (use_tpl & 2)
14844 : : {
14845 : 61704 : depset *spec_dep = find_dependency (spec);
14846 : 61704 : if (spec_dep->get_entity_kind () == EK_REDIRECT)
14847 : 14651 : spec_dep = spec_dep->deps[0];
14848 : 61704 : if (spec_dep->is_unreached ())
14849 : : {
14850 : 5202 : reached_unreached = true;
14851 : 5202 : spec_dep->clear_flag_bit<DB_UNREACHED_BIT> ();
14852 : 5202 : dump (dumper::DEPEND)
14853 : 0 : && dump ("Reaching unreached specialization"
14854 : 0 : " %C:%N", TREE_CODE (spec), spec);
14855 : : }
14856 : : }
14857 : 743728 : };
14858 : :
14859 : 270328 : for (tree cons = DECL_TEMPLATE_INSTANTIATIONS (decl);
14860 : 728469 : cons; cons = TREE_CHAIN (cons))
14861 : 458141 : mark_reached (TREE_VALUE (cons));
14862 : 270328 : for (tree cons = DECL_TEMPLATE_SPECIALIZATIONS (decl);
14863 : 285587 : cons; cons = TREE_CHAIN (cons))
14864 : 15259 : mark_reached (TREE_VALUE (cons));
14865 : : }
14866 : :
14867 : 1528315 : dump.outdent ();
14868 : 1528315 : current = NULL;
14869 : : }
14870 : : }
14871 : :
14872 : 224327 : if (!reached_unreached)
14873 : : break;
14874 : :
14875 : : /* It's possible the we reached the unreached before we
14876 : : processed it in the above loop, so we'll be doing this an
14877 : : extra time. However, to avoid that we have to do some
14878 : : bit shuffling that also involves a scan of the list.
14879 : : Swings & roundabouts I guess. */
14880 : 959 : std::swap (worklist, unreached);
14881 : : }
14882 : :
14883 : 223368 : unreached.release ();
14884 : 223368 : }
14885 : :
14886 : : /* Compare two entries of a single binding. TYPE_DECL before
14887 : : non-exported before exported. */
14888 : :
14889 : : static int
14890 : 389191 : binding_cmp (const void *a_, const void *b_)
14891 : : {
14892 : 389191 : depset *a = *(depset *const *)a_;
14893 : 389191 : depset *b = *(depset *const *)b_;
14894 : :
14895 : 389191 : tree a_ent = a->get_entity ();
14896 : 389191 : tree b_ent = b->get_entity ();
14897 : 389191 : gcc_checking_assert (a_ent != b_ent
14898 : : && !a->is_binding ()
14899 : : && !b->is_binding ());
14900 : :
14901 : : /* Implicit typedefs come first. */
14902 : 389191 : bool a_implicit = DECL_IMPLICIT_TYPEDEF_P (a_ent);
14903 : 389191 : bool b_implicit = DECL_IMPLICIT_TYPEDEF_P (b_ent);
14904 : 389089 : if (a_implicit || b_implicit)
14905 : : {
14906 : : /* A binding with two implicit type decls? That's unpossible! */
14907 : 204 : gcc_checking_assert (!(a_implicit && b_implicit));
14908 : 306 : return a_implicit ? -1 : +1; /* Implicit first. */
14909 : : }
14910 : :
14911 : : /* Hidden before non-hidden. */
14912 : 388987 : bool a_hidden = a->is_hidden ();
14913 : 388987 : bool b_hidden = b->is_hidden ();
14914 : 388987 : if (a_hidden != b_hidden)
14915 : 39963 : return a_hidden ? -1 : +1;
14916 : :
14917 : 362794 : bool a_using = a->get_entity_kind () == depset::EK_USING;
14918 : 362794 : bool a_export;
14919 : 362794 : if (a_using)
14920 : : {
14921 : 26752 : a_export = OVL_EXPORT_P (a_ent);
14922 : 26752 : a_ent = OVL_FUNCTION (a_ent);
14923 : : }
14924 : 336042 : else if (TREE_CODE (a_ent) == CONST_DECL
14925 : 0 : && DECL_LANG_SPECIFIC (a_ent)
14926 : 336042 : && DECL_MODULE_EXPORT_P (a_ent))
14927 : : a_export = true;
14928 : : else
14929 : 336042 : a_export = DECL_MODULE_EXPORT_P (TREE_CODE (a_ent) == CONST_DECL
14930 : : ? TYPE_NAME (TREE_TYPE (a_ent))
14931 : : : STRIP_TEMPLATE (a_ent));
14932 : :
14933 : 362794 : bool b_using = b->get_entity_kind () == depset::EK_USING;
14934 : 362794 : bool b_export;
14935 : 362794 : if (b_using)
14936 : : {
14937 : 23160 : b_export = OVL_EXPORT_P (b_ent);
14938 : 23160 : b_ent = OVL_FUNCTION (b_ent);
14939 : : }
14940 : 339634 : else if (TREE_CODE (b_ent) == CONST_DECL
14941 : 0 : && DECL_LANG_SPECIFIC (b_ent)
14942 : 339634 : && DECL_MODULE_EXPORT_P (b_ent))
14943 : : b_export = true;
14944 : : else
14945 : 339634 : b_export = DECL_MODULE_EXPORT_P (TREE_CODE (b_ent) == CONST_DECL
14946 : : ? TYPE_NAME (TREE_TYPE (b_ent))
14947 : : : STRIP_TEMPLATE (b_ent));
14948 : :
14949 : : /* Non-exports before exports. */
14950 : 362794 : if (a_export != b_export)
14951 : 72 : return a_export ? +1 : -1;
14952 : :
14953 : : /* At this point we don't care, but want a stable sort. */
14954 : :
14955 : 362749 : if (a_using != b_using)
14956 : : /* using first. */
14957 : 19747 : return a_using? -1 : +1;
14958 : :
14959 : 348387 : return DECL_UID (a_ent) < DECL_UID (b_ent) ? -1 : +1;
14960 : : }
14961 : :
14962 : : /* True iff TMPL has an explicit instantiation definition.
14963 : :
14964 : : This is local to module.cc because register_specialization skips adding most
14965 : : instantiations unless module_maybe_has_cmi_p. */
14966 : :
14967 : : static bool
14968 : 54 : template_has_explicit_inst (tree tmpl)
14969 : : {
14970 : 60 : for (tree t = DECL_TEMPLATE_INSTANTIATIONS (tmpl); t; t = TREE_CHAIN (t))
14971 : : {
14972 : 12 : tree spec = TREE_VALUE (t);
14973 : 12 : if (DECL_EXPLICIT_INSTANTIATION (spec)
14974 : 12 : && !DECL_REALLY_EXTERN (spec))
14975 : : return true;
14976 : : }
14977 : : return false;
14978 : : }
14979 : :
14980 : : /* Sort the bindings, issue errors about bad internal refs. */
14981 : :
14982 : : bool
14983 : 2327 : depset::hash::finalize_dependencies ()
14984 : : {
14985 : 2327 : bool ok = true;
14986 : 2299215 : for (depset *dep : *this)
14987 : : {
14988 : 1148444 : if (dep->is_binding ())
14989 : : {
14990 : : /* Keep the containing namespace dep first. */
14991 : 118602 : gcc_checking_assert (dep->deps.length () > 1
14992 : : && (dep->deps[0]->get_entity_kind ()
14993 : : == EK_NAMESPACE)
14994 : : && (dep->deps[0]->get_entity ()
14995 : : == dep->get_entity ()));
14996 : 118602 : if (dep->deps.length () > 2)
14997 : 8321 : gcc_qsort (&dep->deps[1], dep->deps.length () - 1,
14998 : : sizeof (dep->deps[1]), binding_cmp);
14999 : :
15000 : : /* Bindings shouldn't refer to imported entities. */
15001 : 118602 : if (CHECKING_P)
15002 : 621306 : for (depset *entity : dep->deps)
15003 : 265500 : gcc_checking_assert (!entity->is_import ());
15004 : : }
15005 : 1029923 : else if (dep->is_exposure () && !dep->is_tu_local ())
15006 : : {
15007 : 81 : ok = false;
15008 : 81 : bool explained = false;
15009 : 81 : tree decl = dep->get_entity ();
15010 : :
15011 : 330 : for (depset *rdep : dep->deps)
15012 : 159 : if (!rdep->is_binding ()
15013 : 86 : && rdep->is_tu_local ()
15014 : 231 : && !is_exposure_of_member_type (dep, rdep))
15015 : : {
15016 : : // FIXME:QOI Better location information? We're
15017 : : // losing, so it doesn't matter about efficiency
15018 : 72 : tree exposed = rdep->get_entity ();
15019 : 72 : auto_diagnostic_group d;
15020 : 72 : error_at (DECL_SOURCE_LOCATION (decl),
15021 : : "%qD exposes TU-local entity %qD", decl, exposed);
15022 : 72 : bool informed = is_tu_local_entity (exposed, /*explain=*/true);
15023 : 72 : gcc_checking_assert (informed);
15024 : 72 : explained = true;
15025 : 72 : break;
15026 : 72 : }
15027 : :
15028 : 81 : if (!explained && VAR_P (decl) && DECL_DECLARED_CONSTEXPR_P (decl))
15029 : : {
15030 : 9 : auto_diagnostic_group d;
15031 : 9 : error_at (DECL_SOURCE_LOCATION (decl),
15032 : : "%qD is declared %<constexpr%> and is initialized to "
15033 : : "a TU-local value", decl);
15034 : 9 : bool informed = is_tu_local_value (decl, DECL_INITIAL (decl),
15035 : : /*explain=*/true);
15036 : 9 : gcc_checking_assert (informed);
15037 : 9 : explained = true;
15038 : 9 : }
15039 : :
15040 : : /* We should have emitted an error above. */
15041 : 81 : gcc_checking_assert (explained);
15042 : : }
15043 : 1029761 : else if (warn_template_names_tu_local
15044 : 1109191 : && dep->refs_tu_local () && !dep->is_tu_local ())
15045 : : {
15046 : 63 : tree decl = dep->get_entity ();
15047 : :
15048 : : /* Friend decls in a class body are ignored, but this is harmless:
15049 : : it should not impact any consumers. */
15050 : 63 : if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (decl)))
15051 : 9 : continue;
15052 : :
15053 : : /* We should now only be warning about templates. */
15054 : 54 : gcc_checking_assert
15055 : : (TREE_CODE (decl) == TEMPLATE_DECL
15056 : : && VAR_OR_FUNCTION_DECL_P (DECL_TEMPLATE_RESULT (decl)));
15057 : :
15058 : : /* Don't warn if we've seen any explicit instantiation definitions,
15059 : : the intent might be for importers to only use those. */
15060 : 54 : if (template_has_explicit_inst (decl))
15061 : 6 : continue;
15062 : :
15063 : 195 : for (depset *rdep : dep->deps)
15064 : 153 : if (!rdep->is_binding () && rdep->is_tu_local ())
15065 : : {
15066 : 48 : tree ref = rdep->get_entity ();
15067 : 48 : auto_diagnostic_group d;
15068 : 48 : if (warning_at (DECL_SOURCE_LOCATION (decl),
15069 : : OPT_Wtemplate_names_tu_local,
15070 : : "%qD refers to TU-local entity %qD and cannot "
15071 : : "be instantiated in other TUs", decl, ref))
15072 : 48 : is_tu_local_entity (ref, /*explain=*/true);
15073 : 48 : break;
15074 : 48 : }
15075 : : }
15076 : : }
15077 : :
15078 : 2327 : return ok;
15079 : : }
15080 : :
15081 : : /* Core of TARJAN's algorithm to find Strongly Connected Components
15082 : : within a graph. See https://en.wikipedia.org/wiki/
15083 : : Tarjan%27s_strongly_connected_components_algorithm for details.
15084 : :
15085 : : We use depset::section as lowlink. Completed nodes have
15086 : : depset::cluster containing the cluster number, with the top
15087 : : bit set.
15088 : :
15089 : : A useful property is that the output vector is a reverse
15090 : : topological sort of the resulting DAG. In our case that means
15091 : : dependent SCCs are found before their dependers. We make use of
15092 : : that property. */
15093 : :
15094 : : void
15095 : 1646164 : depset::tarjan::connect (depset *v)
15096 : : {
15097 : 1646164 : gcc_checking_assert (v->is_binding ()
15098 : : || !(v->is_tu_local ()
15099 : : || v->is_unreached ()
15100 : : || v->is_import ()));
15101 : :
15102 : 1646164 : v->cluster = v->section = ++index;
15103 : 1646164 : stack.safe_push (v);
15104 : :
15105 : : /* Walk all our dependencies, ignore a first marked slot */
15106 : 13535565 : for (unsigned ix = v->is_special (); ix != v->deps.length (); ix++)
15107 : : {
15108 : 5124928 : depset *dep = v->deps[ix];
15109 : :
15110 : 5124928 : if (dep->is_binding ()
15111 : 15080607 : || !(dep->is_import () || dep->is_tu_local ()))
15112 : : {
15113 : 5124052 : unsigned lwm = dep->cluster;
15114 : :
15115 : 5124052 : if (!dep->cluster)
15116 : : {
15117 : : /* A new node. Connect it. */
15118 : 908937 : connect (dep);
15119 : 908937 : lwm = dep->section;
15120 : : }
15121 : :
15122 : 5124052 : if (dep->section && v->section > lwm)
15123 : 812962 : v->section = lwm;
15124 : : }
15125 : : }
15126 : :
15127 : 1646164 : if (v->section == v->cluster)
15128 : : {
15129 : : /* Root of a new SCC. Push all the members onto the result list. */
15130 : : unsigned num = v->cluster;
15131 : 1646164 : depset *p;
15132 : 1646164 : do
15133 : : {
15134 : 1646164 : p = stack.pop ();
15135 : 1646164 : p->cluster = num;
15136 : 1646164 : p->section = 0;
15137 : 1646164 : result.quick_push (p);
15138 : : }
15139 : 1646164 : while (p != v);
15140 : : }
15141 : 1646164 : }
15142 : :
15143 : : /* Compare two depsets. The specific ordering is unimportant, we're
15144 : : just trying to get consistency. */
15145 : :
15146 : : static int
15147 : 76194254 : depset_cmp (const void *a_, const void *b_)
15148 : : {
15149 : 76194254 : depset *a = *(depset *const *)a_;
15150 : 76194254 : depset *b = *(depset *const *)b_;
15151 : :
15152 : 76194254 : depset::entity_kind a_kind = a->get_entity_kind ();
15153 : 76194254 : depset::entity_kind b_kind = b->get_entity_kind ();
15154 : :
15155 : 76194254 : if (a_kind != b_kind)
15156 : : /* Different entity kinds, order by that. */
15157 : 3967566 : return a_kind < b_kind ? -1 : +1;
15158 : :
15159 : 73374208 : tree a_decl = a->get_entity ();
15160 : 73374208 : tree b_decl = b->get_entity ();
15161 : 73374208 : if (a_kind == depset::EK_USING)
15162 : : {
15163 : : /* If one is a using, the other must be too. */
15164 : 867859 : a_decl = OVL_FUNCTION (a_decl);
15165 : 867859 : b_decl = OVL_FUNCTION (b_decl);
15166 : : }
15167 : :
15168 : 73374208 : if (a_decl != b_decl)
15169 : : /* Different entities, order by their UID. */
15170 : 67505884 : return DECL_UID (a_decl) < DECL_UID (b_decl) ? -1 : +1;
15171 : :
15172 : 5868324 : if (a_kind == depset::EK_BINDING)
15173 : : {
15174 : : /* Both are bindings. Order by identifier hash. */
15175 : 5865402 : gcc_checking_assert (a->get_name () != b->get_name ());
15176 : 5865402 : hashval_t ah = IDENTIFIER_HASH_VALUE (a->get_name ());
15177 : 5865402 : hashval_t bh = IDENTIFIER_HASH_VALUE (b->get_name ());
15178 : 8737637 : return (ah == bh ? 0 : ah < bh ? -1 : +1);
15179 : : }
15180 : :
15181 : : /* They are the same decl. This can happen with two using decls
15182 : : pointing to the same target. The best we can aim for is
15183 : : consistently telling qsort how to order them. Hopefully we'll
15184 : : never have to debug a case that depends on this. Oh, who am I
15185 : : kidding? Good luck. */
15186 : 2922 : gcc_checking_assert (a_kind == depset::EK_USING);
15187 : :
15188 : : /* Order by depset address. Not the best, but it is something. */
15189 : 2922 : return a < b ? -1 : +1;
15190 : : }
15191 : :
15192 : : /* Sort the clusters in SCC such that those that depend on one another
15193 : : are placed later. */
15194 : :
15195 : : // FIXME: I am not convinced this is needed and, if needed,
15196 : : // sufficient. We emit the decls in this order but that emission
15197 : : // could walk into later decls (from the body of the decl, or default
15198 : : // arg-like things). Why doesn't that walk do the right thing? And
15199 : : // if it DTRT why do we need to sort here -- won't things naturally
15200 : : // work? I think part of the issue is that when we're going to refer
15201 : : // to an entity by name, and that entity is in the same cluster as us,
15202 : : // we need to actually walk that entity, if we've not already walked
15203 : : // it.
15204 : : static void
15205 : 221041 : sort_cluster (depset::hash *original, depset *scc[], unsigned size)
15206 : : {
15207 : 221041 : depset::hash table (size, original);
15208 : :
15209 : 221041 : dump.indent ();
15210 : :
15211 : : /* Place bindings last, usings before that. It's not strictly
15212 : : necessary, but it does make things neater. Says Mr OCD. */
15213 : : unsigned bind_lwm = size;
15214 : : unsigned use_lwm = size;
15215 : 1109128 : for (unsigned ix = 0; ix != use_lwm;)
15216 : : {
15217 : 888087 : depset *dep = scc[ix];
15218 : 888087 : switch (dep->get_entity_kind ())
15219 : : {
15220 : 118431 : case depset::EK_BINDING:
15221 : : /* Move to end. No increment. Notice this could be moving
15222 : : a using decl, which we'll then move again. */
15223 : 118431 : if (--bind_lwm != ix)
15224 : : {
15225 : 62713 : scc[ix] = scc[bind_lwm];
15226 : 62713 : scc[bind_lwm] = dep;
15227 : : }
15228 : 118431 : if (use_lwm > bind_lwm)
15229 : : {
15230 : 98886 : use_lwm--;
15231 : 98886 : break;
15232 : : }
15233 : : /* We must have copied a using, so move it too. */
15234 : 19545 : dep = scc[ix];
15235 : 19545 : gcc_checking_assert (dep->get_entity_kind () == depset::EK_USING);
15236 : : /* FALLTHROUGH */
15237 : :
15238 : 35053 : case depset::EK_USING:
15239 : 35053 : if (--use_lwm != ix)
15240 : : {
15241 : 25929 : scc[ix] = scc[use_lwm];
15242 : 25929 : scc[use_lwm] = dep;
15243 : : }
15244 : : break;
15245 : :
15246 : 754148 : case depset::EK_DECL:
15247 : 754148 : case depset::EK_SPECIALIZATION:
15248 : 754148 : case depset::EK_PARTIAL:
15249 : 754148 : table.add_mergeable (dep);
15250 : 754148 : ix++;
15251 : 754148 : break;
15252 : :
15253 : 0 : default:
15254 : 0 : gcc_unreachable ();
15255 : : }
15256 : : }
15257 : :
15258 : 221041 : gcc_checking_assert (use_lwm <= bind_lwm);
15259 : 221305 : dump (dumper::MERGE) && dump ("Ordering %u/%u depsets", use_lwm, size);
15260 : :
15261 : 221041 : table.find_dependencies (nullptr);
15262 : :
15263 : 442082 : auto_vec<depset *> order = table.connect ();
15264 : 442082 : gcc_checking_assert (order.length () == use_lwm);
15265 : :
15266 : : /* Now rewrite entries [0,lwm), in the dependency order we
15267 : : discovered. Usually each entity is in its own cluster. Rarely,
15268 : : we can get multi-entity clusters, in which case all but one must
15269 : : only be reached from within the cluster. This happens for
15270 : : something like:
15271 : :
15272 : : template<typename T>
15273 : : auto Foo (const T &arg) -> TPL<decltype (arg)>;
15274 : :
15275 : : The instantiation of TPL will be in the specialization table, and
15276 : : refer to Foo via arg. But we can only get to that specialization
15277 : : from Foo's declaration, so we only need to treat Foo as mergable
15278 : : (We'll do structural comparison of TPL<decltype (arg)>).
15279 : :
15280 : : We approximate finding the single cluster entry dep by checking for
15281 : : entities recursively depending on a dep first seen when streaming
15282 : : its own merge key; the first dep we see in such a cluster should be
15283 : : the first one streamed. */
15284 : : unsigned entry_pos = ~0u;
15285 : : unsigned cluster = ~0u;
15286 : 1950378 : for (unsigned ix = 0; ix != order.length (); ix++)
15287 : : {
15288 : 754148 : gcc_checking_assert (order[ix]->is_special ());
15289 : 754148 : bool tight = order[ix]->cluster == cluster;
15290 : 754148 : depset *dep = order[ix]->deps[0];
15291 : 754871 : dump (dumper::MERGE)
15292 : 1443 : && dump ("Mergeable %u is %N%s%s", ix, dep->get_entity (),
15293 : 723 : tight ? " (tight)" : "", dep->is_entry () ? " (entry)" : "");
15294 : 754148 : scc[ix] = dep;
15295 : 754148 : if (tight)
15296 : : {
15297 : 93 : gcc_checking_assert (dep->is_maybe_recursive ());
15298 : 93 : if (dep->is_entry ())
15299 : : {
15300 : : /* There should only be one entry dep in a cluster. */
15301 : 6 : gcc_checking_assert (!scc[entry_pos]->is_entry ());
15302 : 6 : gcc_checking_assert (scc[entry_pos]->is_maybe_recursive ());
15303 : 6 : scc[ix] = scc[entry_pos];
15304 : 6 : scc[entry_pos] = dep;
15305 : : }
15306 : : }
15307 : : else
15308 : : entry_pos = ix;
15309 : 754148 : cluster = order[ix]->cluster;
15310 : : }
15311 : :
15312 : 221305 : dump (dumper::MERGE) && dump ("Ordered %u keys", order.length ());
15313 : 221041 : dump.outdent ();
15314 : 221041 : }
15315 : :
15316 : : /* Reduce graph to SCCS clusters. SCCS will be populated with the
15317 : : depsets in dependency order. Each depset's CLUSTER field contains
15318 : : its cluster number. Each SCC has a unique cluster number, and are
15319 : : contiguous in SCCS. Cluster numbers are otherwise arbitrary. */
15320 : :
15321 : : vec<depset *>
15322 : 223343 : depset::hash::connect ()
15323 : : {
15324 : 223343 : tarjan connector (size ());
15325 : 223343 : vec<depset *> deps;
15326 : 223343 : deps.create (size ());
15327 : 4027171 : for (depset *item : *this)
15328 : : {
15329 : 1901914 : entity_kind kind = item->get_entity_kind ();
15330 : 1783483 : if (kind == EK_BINDING
15331 : 1783483 : || !(kind == EK_REDIRECT
15332 : 1764069 : || item->is_tu_local ()
15333 : 1763976 : || item->is_unreached ()
15334 : 1528267 : || item->is_import ()))
15335 : 1646164 : deps.quick_push (item);
15336 : : }
15337 : :
15338 : : /* Iteration over the hash table is an unspecified ordering. While
15339 : : that has advantages, it causes 2 problems. Firstly repeatable
15340 : : builds are tricky. Secondly creating testcases that check
15341 : : dependencies are correct by making sure a bad ordering would
15342 : : happen if that was wrong. */
15343 : 1183913 : deps.qsort (depset_cmp);
15344 : :
15345 : 1869507 : while (deps.length ())
15346 : : {
15347 : 1646164 : depset *v = deps.pop ();
15348 : 1646164 : dump (dumper::CLUSTER) &&
15349 : 1599 : (v->is_binding ()
15350 : 195 : ? dump ("Connecting binding %P", v->get_entity (), v->get_name ())
15351 : 1404 : : dump ("Connecting %s %s %C:%N",
15352 : 1404 : is_key_order () ? "key-order"
15353 : 774 : : !v->has_defn () ? "declaration" : "definition",
15354 : 1404 : v->entity_kind_name (), TREE_CODE (v->get_entity ()),
15355 : : v->get_entity ()));
15356 : 1646164 : if (!v->cluster)
15357 : 737227 : connector.connect (v);
15358 : : }
15359 : :
15360 : 223343 : deps.release ();
15361 : 446686 : return connector.result;
15362 : 223343 : }
15363 : :
15364 : : /* Initialize location spans. */
15365 : :
15366 : : void
15367 : 4155 : loc_spans::init (const line_maps *lmaps, const line_map_ordinary *map)
15368 : : {
15369 : 4155 : gcc_checking_assert (!init_p ());
15370 : 4155 : spans = new vec<span> ();
15371 : 4155 : spans->reserve (20);
15372 : :
15373 : 4155 : span interval;
15374 : 4155 : interval.ordinary.first = 0;
15375 : 4155 : interval.macro.second = MAX_LOCATION_T + 1;
15376 : 4155 : interval.ordinary_delta = interval.macro_delta = 0;
15377 : :
15378 : : /* A span for reserved fixed locs. */
15379 : 4155 : interval.ordinary.second
15380 : 4155 : = MAP_START_LOCATION (LINEMAPS_ORDINARY_MAP_AT (line_table, 0));
15381 : 4155 : interval.macro.first = interval.macro.second;
15382 : 4155 : dump (dumper::LOCATION)
15383 : 42 : && dump ("Fixed span %u ordinary:[%K,%K) macro:[%K,%K)", spans->length (),
15384 : : interval.ordinary.first, interval.ordinary.second,
15385 : : interval.macro.first, interval.macro.second);
15386 : 4155 : spans->quick_push (interval);
15387 : :
15388 : : /* A span for command line & forced headers. */
15389 : 4155 : interval.ordinary.first = interval.ordinary.second;
15390 : 4155 : interval.macro.second = interval.macro.first;
15391 : 4155 : if (map)
15392 : : {
15393 : 4149 : interval.ordinary.second = map->start_location;
15394 : 4149 : interval.macro.first = LINEMAPS_MACRO_LOWEST_LOCATION (lmaps);
15395 : : }
15396 : 4155 : dump (dumper::LOCATION)
15397 : 21 : && dump ("Pre span %u ordinary:[%K,%K) macro:[%K,%K)", spans->length (),
15398 : : interval.ordinary.first, interval.ordinary.second,
15399 : : interval.macro.first, interval.macro.second);
15400 : 4155 : spans->quick_push (interval);
15401 : :
15402 : : /* Start an interval for the main file. */
15403 : 4155 : interval.ordinary.first = interval.ordinary.second;
15404 : 4155 : interval.macro.second = interval.macro.first;
15405 : 4155 : dump (dumper::LOCATION)
15406 : 21 : && dump ("Main span %u ordinary:[%K,*) macro:[*,%K)", spans->length (),
15407 : : interval.ordinary.first, interval.macro.second);
15408 : 4155 : spans->quick_push (interval);
15409 : 4155 : }
15410 : :
15411 : : /* Reopen the span, if we want the about-to-be-inserted set of maps to
15412 : : be propagated in our own location table. I.e. we are the primary
15413 : : interface and we're importing a partition. */
15414 : :
15415 : : bool
15416 : 2567 : loc_spans::maybe_propagate (module_state *import, location_t hwm)
15417 : : {
15418 : 2567 : bool opened = (module_interface_p () && !module_partition_p ()
15419 : 2896 : && import->is_partition ());
15420 : 134 : if (opened)
15421 : 134 : open (hwm);
15422 : 2567 : return opened;
15423 : : }
15424 : :
15425 : : /* Open a new linemap interval. The just-created ordinary map is the
15426 : : first map of the interval. */
15427 : :
15428 : : void
15429 : 972 : loc_spans::open (location_t hwm)
15430 : : {
15431 : 972 : span interval;
15432 : 972 : interval.ordinary.first = interval.ordinary.second = hwm;
15433 : 1944 : interval.macro.first = interval.macro.second
15434 : 972 : = LINEMAPS_MACRO_LOWEST_LOCATION (line_table);
15435 : 972 : interval.ordinary_delta = interval.macro_delta = 0;
15436 : 972 : dump (dumper::LOCATION)
15437 : 0 : && dump ("Opening span %u ordinary:[%K,... macro:...,%K)",
15438 : 0 : spans->length (), interval.ordinary.first,
15439 : : interval.macro.second);
15440 : 972 : if (spans->length ())
15441 : : {
15442 : : /* No overlapping! */
15443 : 972 : auto &last = spans->last ();
15444 : 972 : gcc_checking_assert (interval.ordinary.first >= last.ordinary.second);
15445 : 972 : gcc_checking_assert (interval.macro.second <= last.macro.first);
15446 : : }
15447 : 972 : spans->safe_push (interval);
15448 : 972 : }
15449 : :
15450 : : /* Close out the current linemap interval. The last maps are within
15451 : : the interval. */
15452 : :
15453 : : void
15454 : 5124 : loc_spans::close ()
15455 : : {
15456 : 5124 : span &interval = spans->last ();
15457 : :
15458 : 5124 : interval.ordinary.second
15459 : 5124 : = ((line_table->highest_location
15460 : 5124 : + (loc_one << line_table->default_range_bits))
15461 : 5124 : & ~((loc_one << line_table->default_range_bits) - 1));
15462 : 5124 : interval.macro.first = LINEMAPS_MACRO_LOWEST_LOCATION (line_table);
15463 : 5124 : dump (dumper::LOCATION)
15464 : 21 : && dump ("Closing span %u ordinary:[%K,%K) macro:[%K,%K)",
15465 : 21 : spans->length () - 1,
15466 : : interval.ordinary.first,interval.ordinary.second,
15467 : : interval.macro.first, interval.macro.second);
15468 : 5124 : }
15469 : :
15470 : : /* Given an ordinary location LOC, return the lmap_interval it resides
15471 : : in. NULL if it is not in an interval. */
15472 : :
15473 : : const loc_spans::span *
15474 : 22861496 : loc_spans::ordinary (location_t loc)
15475 : : {
15476 : 22861496 : unsigned len = spans->length ();
15477 : 45583051 : unsigned pos = 0;
15478 : 45588323 : while (len)
15479 : : {
15480 : 45582184 : unsigned half = len / 2;
15481 : 45582184 : const span &probe = (*spans)[pos + half];
15482 : 45582184 : if (loc < probe.ordinary.first)
15483 : : len = half;
15484 : 45576912 : else if (loc < probe.ordinary.second)
15485 : : return &probe;
15486 : : else
15487 : : {
15488 : 22721555 : pos += half + 1;
15489 : 22721555 : len = len - (half + 1);
15490 : : }
15491 : : }
15492 : : return NULL;
15493 : : }
15494 : :
15495 : : /* Likewise, given a macro location LOC, return the lmap interval it
15496 : : resides in. */
15497 : :
15498 : : const loc_spans::span *
15499 : 1852842 : loc_spans::macro (location_t loc)
15500 : : {
15501 : 1852842 : unsigned len = spans->length ();
15502 : 3700622 : unsigned pos = 0;
15503 : 3700622 : while (len)
15504 : : {
15505 : 3700610 : unsigned half = len / 2;
15506 : 3700610 : const span &probe = (*spans)[pos + half];
15507 : 3700610 : if (loc >= probe.macro.second)
15508 : : len = half;
15509 : 3700610 : else if (loc >= probe.macro.first)
15510 : : return &probe;
15511 : : else
15512 : : {
15513 : 1847780 : pos += half + 1;
15514 : 1847780 : len = len - (half + 1);
15515 : : }
15516 : : }
15517 : : return NULL;
15518 : : }
15519 : :
15520 : : /* Return the ordinary location closest to FROM. */
15521 : :
15522 : : static location_t
15523 : 5894 : ordinary_loc_of (line_maps *lmaps, location_t from)
15524 : : {
15525 : 11791 : while (!IS_ORDINARY_LOC (from))
15526 : : {
15527 : 3 : if (IS_ADHOC_LOC (from))
15528 : 3 : from = get_location_from_adhoc_loc (lmaps, from);
15529 : 3 : if (from >= LINEMAPS_MACRO_LOWEST_LOCATION (lmaps))
15530 : : {
15531 : : /* Find the ordinary location nearest FROM. */
15532 : 0 : const line_map *map = linemap_lookup (lmaps, from);
15533 : 0 : const line_map_macro *mac_map = linemap_check_macro (map);
15534 : 0 : from = mac_map->get_expansion_point_location ();
15535 : : }
15536 : : }
15537 : 5894 : return from;
15538 : : }
15539 : :
15540 : : static module_state **
15541 : 10312 : get_module_slot (tree name, module_state *parent, bool partition, bool insert)
15542 : : {
15543 : 10312 : module_state_hash::compare_type ct (name, uintptr_t (parent) | partition);
15544 : 10312 : hashval_t hv = module_state_hash::hash (ct);
15545 : :
15546 : 10312 : return modules_hash->find_slot_with_hash (ct, hv, insert ? INSERT : NO_INSERT);
15547 : : }
15548 : :
15549 : : static module_state *
15550 : 380 : get_primary (module_state *parent)
15551 : : {
15552 : 3643 : while (parent->is_partition ())
15553 : 338 : parent = parent->parent;
15554 : :
15555 : 3305 : if (!parent->name)
15556 : : // Implementation unit has null name
15557 : 126 : parent = parent->parent;
15558 : :
15559 : 357 : return parent;
15560 : : }
15561 : :
15562 : : /* Find or create module NAME & PARENT in the hash table. */
15563 : :
15564 : : module_state *
15565 : 10312 : get_module (tree name, module_state *parent, bool partition)
15566 : : {
15567 : : /* We might be given an empty NAME if preprocessing fails to handle
15568 : : a header-name token. */
15569 : 10312 : if (name && TREE_CODE (name) == STRING_CST
15570 : 12994 : && TREE_STRING_LENGTH (name) == 0)
15571 : : return nullptr;
15572 : :
15573 : 10312 : if (partition)
15574 : : {
15575 : 839 : if (!parent)
15576 : 173 : parent = get_primary ((*modules)[0]);
15577 : :
15578 : 839 : if (!parent->is_partition () && !parent->flatname)
15579 : 200 : parent->set_flatname ();
15580 : : }
15581 : :
15582 : 10312 : module_state **slot = get_module_slot (name, parent, partition, true);
15583 : 10312 : module_state *state = *slot;
15584 : 10312 : if (!state)
15585 : : {
15586 : 5651 : state = (new (ggc_alloc<module_state> ())
15587 : 5651 : module_state (name, parent, partition));
15588 : 5651 : *slot = state;
15589 : : }
15590 : : return state;
15591 : : }
15592 : :
15593 : : /* Process string name PTR into a module_state. */
15594 : :
15595 : : static module_state *
15596 : 354 : get_module (const char *ptr)
15597 : : {
15598 : : /* On DOS based file systems, there is an ambiguity with A:B which can be
15599 : : interpreted as a module Module:Partition or Drive:PATH. Interpret strings
15600 : : which clearly starts as pathnames as header-names and everything else is
15601 : : treated as a (possibly malformed) named moduled. */
15602 : 354 : if (IS_DIR_SEPARATOR (ptr[ptr[0] == '.']) // ./FOO or /FOO
15603 : : #if HAVE_DOS_BASED_FILE_SYSTEM
15604 : : || (HAS_DRIVE_SPEC (ptr) && IS_DIR_SEPARATOR (ptr[2])) // A:/FOO
15605 : : #endif
15606 : : || false)
15607 : : /* A header name. */
15608 : 107 : return get_module (build_string (strlen (ptr), ptr));
15609 : :
15610 : : bool partition = false;
15611 : : module_state *mod = NULL;
15612 : :
15613 : 1038 : for (const char *probe = ptr;; probe++)
15614 : 1285 : if (!*probe || *probe == '.' || *probe == ':')
15615 : : {
15616 : 316 : if (probe == ptr)
15617 : : return NULL;
15618 : :
15619 : 316 : mod = get_module (get_identifier_with_length (ptr, probe - ptr),
15620 : : mod, partition);
15621 : 316 : ptr = probe;
15622 : 316 : if (*ptr == ':')
15623 : : {
15624 : 69 : if (partition)
15625 : : return NULL;
15626 : : partition = true;
15627 : : }
15628 : :
15629 : 316 : if (!*ptr++)
15630 : : break;
15631 : : }
15632 : 969 : else if (!(ISALPHA (*probe) || *probe == '_'
15633 : 12 : || (probe != ptr && ISDIGIT (*probe))))
15634 : : return NULL;
15635 : :
15636 : : return mod;
15637 : : }
15638 : :
15639 : : /* Create a new mapper connecting to OPTION. */
15640 : :
15641 : : module_client *
15642 : 4155 : make_mapper (location_t loc, class mkdeps *deps)
15643 : : {
15644 : 4155 : timevar_start (TV_MODULE_MAPPER);
15645 : 4155 : const char *option = module_mapper_name;
15646 : 4155 : if (!option)
15647 : 4113 : option = getenv ("CXX_MODULE_MAPPER");
15648 : :
15649 : 8310 : mapper = module_client::open_module_client
15650 : 4155 : (loc, option, deps, &set_cmi_repo,
15651 : 4155 : (save_decoded_options[0].opt_index == OPT_SPECIAL_program_name)
15652 : 4155 : && save_decoded_options[0].arg != progname
15653 : : ? save_decoded_options[0].arg : nullptr);
15654 : :
15655 : 4155 : timevar_stop (TV_MODULE_MAPPER);
15656 : :
15657 : 4155 : return mapper;
15658 : : }
15659 : :
15660 : : static unsigned lazy_snum;
15661 : :
15662 : : static bool
15663 : 7178 : recursive_lazy (unsigned snum = ~0u)
15664 : : {
15665 : 7178 : if (lazy_snum)
15666 : : {
15667 : 0 : error_at (input_location, "recursive lazy load");
15668 : 0 : return true;
15669 : : }
15670 : :
15671 : 7178 : lazy_snum = snum;
15672 : 7178 : return false;
15673 : : }
15674 : :
15675 : : /* If THIS is the current purview, issue an import error and return false. */
15676 : :
15677 : : bool
15678 : 2435 : module_state::check_not_purview (location_t from)
15679 : : {
15680 : 2435 : module_state *imp = (*modules)[0];
15681 : 2435 : if (imp && !imp->name)
15682 : 1849 : imp = imp->parent;
15683 : 2435 : if (imp == this)
15684 : : {
15685 : : /* Cannot import the current module. */
15686 : 12 : auto_diagnostic_group d;
15687 : 12 : error_at (from, "cannot import module in its own purview");
15688 : 12 : inform (loc, "module %qs declared here", get_flatname ());
15689 : 12 : return false;
15690 : 12 : }
15691 : : return true;
15692 : : }
15693 : :
15694 : : /* Module name substitutions. */
15695 : : static vec<module_state *,va_heap> substs;
15696 : :
15697 : : void
15698 : 6836 : module_state::mangle (bool include_partition)
15699 : : {
15700 : 6836 : if (subst)
15701 : 167 : mangle_module_substitution (subst);
15702 : : else
15703 : : {
15704 : 6669 : if (parent)
15705 : 678 : parent->mangle (include_partition);
15706 : 6669 : if (include_partition || !is_partition ())
15707 : : {
15708 : : // Partitions are significant for global initializer
15709 : : // functions
15710 : 6587 : bool partition = is_partition () && !parent->is_partition ();
15711 : 6587 : subst = mangle_module_component (name, partition);
15712 : 6587 : substs.safe_push (this);
15713 : : }
15714 : : }
15715 : 6836 : }
15716 : :
15717 : : void
15718 : 6158 : mangle_module (int mod, bool include_partition)
15719 : : {
15720 : 6158 : module_state *imp = (*modules)[mod];
15721 : :
15722 : 6158 : gcc_checking_assert (!imp->is_header ());
15723 : :
15724 : 6158 : if (!imp->name)
15725 : : /* Set when importing the primary module interface. */
15726 : 222 : imp = imp->parent;
15727 : :
15728 : 6158 : imp->mangle (include_partition);
15729 : 6158 : }
15730 : :
15731 : : /* Clean up substitutions. */
15732 : : void
15733 : 5955 : mangle_module_fini ()
15734 : : {
15735 : 12542 : while (substs.length ())
15736 : 6587 : substs.pop ()->subst = 0;
15737 : 5955 : }
15738 : :
15739 : : /* Announce WHAT about the module. */
15740 : :
15741 : : void
15742 : 10723 : module_state::announce (const char *what) const
15743 : : {
15744 : 10723 : if (noisy_p ())
15745 : : {
15746 : 0 : fprintf (stderr, " %s:%s", what, get_flatname ());
15747 : 0 : fflush (stderr);
15748 : : }
15749 : 10723 : }
15750 : :
15751 : : /* A human-readable README section. The contents of this section to
15752 : : not contribute to the CRC, so the contents can change per
15753 : : compilation. That allows us to embed CWD, hostname, build time and
15754 : : what not. It is a STRTAB that may be extracted with:
15755 : : readelf -pgnu.c++.README $(module).gcm */
15756 : :
15757 : : void
15758 : 2302 : module_state::write_readme (elf_out *to, cpp_reader *reader, const char *dialect)
15759 : : {
15760 : 2302 : bytes_out readme (to);
15761 : :
15762 : 2302 : readme.begin (false);
15763 : :
15764 : 2302 : readme.printf ("GNU C++ %s",
15765 : 2302 : is_header () ? "header unit"
15766 : 1464 : : !is_partition () ? "primary interface"
15767 : 152 : : is_interface () ? "interface partition"
15768 : : : "internal partition");
15769 : :
15770 : : /* Compiler's version. */
15771 : 2302 : readme.printf ("compiler: %s", version_string);
15772 : :
15773 : : /* Module format version. */
15774 : 2302 : verstr_t string;
15775 : 2302 : version2string (MODULE_VERSION, string);
15776 : 2302 : readme.printf ("version: %s", string);
15777 : :
15778 : : /* Module information. */
15779 : 2302 : readme.printf ("module: %s", get_flatname ());
15780 : 2302 : readme.printf ("source: %s", main_input_filename);
15781 : 2302 : readme.printf ("dialect: %s", dialect);
15782 : 2302 : if (extensions)
15783 : 24 : readme.printf ("extensions: %s%s%s",
15784 : : extensions & SE_OPENMP ? "-fopenmp"
15785 : 6 : : extensions & SE_OPENMP_SIMD ? "-fopenmp-simd" : "",
15786 : : (extensions & SE_OPENACC)
15787 : 3 : && (extensions & (SE_OPENMP | SE_OPENMP_SIMD))
15788 : : ? " " : "",
15789 : 9 : extensions & SE_OPENACC ? "-fopenacc" : "");
15790 : :
15791 : : /* The following fields could be expected to change between
15792 : : otherwise identical compilations. Consider a distributed build
15793 : : system. We should have a way of overriding that. */
15794 : 2302 : if (char *cwd = getcwd (NULL, 0))
15795 : : {
15796 : 2302 : readme.printf ("cwd: %s", cwd);
15797 : 2302 : free (cwd);
15798 : : }
15799 : 4604 : readme.printf ("repository: %s", cmi_repo ? cmi_repo : ".");
15800 : : #if NETWORKING
15801 : : {
15802 : : char hostname[64];
15803 : : if (!gethostname (hostname, sizeof (hostname)))
15804 : : readme.printf ("host: %s", hostname);
15805 : : }
15806 : : #endif
15807 : 2302 : {
15808 : : /* This of course will change! */
15809 : 2302 : time_t stampy;
15810 : 2302 : auto kind = cpp_get_date (reader, &stampy);
15811 : 2302 : if (kind != CPP_time_kind::UNKNOWN)
15812 : : {
15813 : 2302 : struct tm *time;
15814 : :
15815 : 2302 : time = gmtime (&stampy);
15816 : 2302 : readme.print_time ("build", time, "UTC");
15817 : :
15818 : 2302 : if (kind == CPP_time_kind::DYNAMIC)
15819 : : {
15820 : 2302 : time = localtime (&stampy);
15821 : 2302 : readme.print_time ("local", time,
15822 : : #if defined (__USE_MISC) || defined (__USE_BSD) /* Is there a better way? */
15823 : : time->tm_zone
15824 : : #else
15825 : : ""
15826 : : #endif
15827 : : );
15828 : : }
15829 : : }
15830 : : }
15831 : :
15832 : : /* Its direct imports. */
15833 : 2836 : for (unsigned ix = 1; ix < modules->length (); ix++)
15834 : : {
15835 : 534 : module_state *state = (*modules)[ix];
15836 : :
15837 : 534 : if (state->is_direct ())
15838 : 781 : readme.printf ("%s: %s %s", state->exported_p ? "export" : "import",
15839 : : state->get_flatname (), state->filename);
15840 : : }
15841 : :
15842 : 2302 : readme.end (to, to->name (MOD_SNAME_PFX ".README"), NULL);
15843 : 2302 : }
15844 : :
15845 : : /* Sort environment var names in reverse order. */
15846 : :
15847 : : static int
15848 : 0 : env_var_cmp (const void *a_, const void *b_)
15849 : : {
15850 : 0 : const unsigned char *a = *(const unsigned char *const *)a_;
15851 : 0 : const unsigned char *b = *(const unsigned char *const *)b_;
15852 : :
15853 : 0 : for (unsigned ix = 0; ; ix++)
15854 : : {
15855 : 0 : bool a_end = !a[ix] || a[ix] == '=';
15856 : 0 : if (a[ix] == b[ix])
15857 : : {
15858 : 0 : if (a_end)
15859 : : break;
15860 : : }
15861 : : else
15862 : : {
15863 : 0 : bool b_end = !b[ix] || b[ix] == '=';
15864 : :
15865 : 0 : if (!a_end && !b_end)
15866 : 0 : return a[ix] < b[ix] ? +1 : -1;
15867 : 0 : if (a_end && b_end)
15868 : : break;
15869 : 0 : return a_end ? +1 : -1;
15870 : : }
15871 : 0 : }
15872 : :
15873 : : return 0;
15874 : : }
15875 : :
15876 : : /* Write the environment. It is a STRTAB that may be extracted with:
15877 : : readelf -pgnu.c++.ENV $(module).gcm */
15878 : :
15879 : : void
15880 : 0 : module_state::write_env (elf_out *to)
15881 : : {
15882 : 0 : vec<const char *> vars;
15883 : 0 : vars.create (20);
15884 : :
15885 : 0 : extern char **environ;
15886 : 0 : while (const char *var = environ[vars.length ()])
15887 : 0 : vars.safe_push (var);
15888 : 0 : vars.qsort (env_var_cmp);
15889 : :
15890 : 0 : bytes_out env (to);
15891 : 0 : env.begin (false);
15892 : 0 : while (vars.length ())
15893 : 0 : env.printf ("%s", vars.pop ());
15894 : 0 : env.end (to, to->name (MOD_SNAME_PFX ".ENV"), NULL);
15895 : :
15896 : 0 : vars.release ();
15897 : 0 : }
15898 : :
15899 : : /* Write the direct or indirect imports.
15900 : : u:N
15901 : : {
15902 : : u:index
15903 : : s:name
15904 : : u32:crc
15905 : : s:filename (direct)
15906 : : u:exported (direct)
15907 : : } imports[N]
15908 : : */
15909 : :
15910 : : void
15911 : 728 : module_state::write_imports (bytes_out &sec, bool direct)
15912 : : {
15913 : 728 : unsigned count = 0;
15914 : :
15915 : 1534 : for (unsigned ix = 1; ix < modules->length (); ix++)
15916 : : {
15917 : 806 : module_state *imp = (*modules)[ix];
15918 : :
15919 : 806 : if (imp->remap && imp->is_direct () == direct)
15920 : 388 : count++;
15921 : : }
15922 : :
15923 : 728 : gcc_assert (!direct || count);
15924 : :
15925 : 728 : sec.u (count);
15926 : 1534 : for (unsigned ix = 1; ix < modules->length (); ix++)
15927 : : {
15928 : 806 : module_state *imp = (*modules)[ix];
15929 : :
15930 : 806 : if (imp->remap && imp->is_direct () == direct)
15931 : : {
15932 : 520 : dump () && dump ("Writing %simport:%u->%u %M (crc=%x)",
15933 : : !direct ? "indirect "
15934 : 66 : : imp->exported_p ? "exported " : "",
15935 : : ix, imp->remap, imp, imp->crc);
15936 : 388 : sec.u (imp->remap);
15937 : 388 : sec.str (imp->get_flatname ());
15938 : 388 : sec.u32 (imp->crc);
15939 : 388 : if (direct)
15940 : : {
15941 : 383 : write_location (sec, imp->imported_from ());
15942 : 383 : sec.str (imp->filename);
15943 : 383 : int exportedness = 0;
15944 : 383 : if (imp->exported_p)
15945 : : exportedness = +1;
15946 : 226 : else if (!imp->is_purview_direct ())
15947 : 12 : exportedness = -1;
15948 : 383 : sec.i (exportedness);
15949 : : }
15950 : : }
15951 : : }
15952 : 728 : }
15953 : :
15954 : : /* READER, LMAPS != NULL == direct imports,
15955 : : == NUL == indirect imports. */
15956 : :
15957 : : unsigned
15958 : 612 : module_state::read_imports (bytes_in &sec, cpp_reader *reader, line_maps *lmaps)
15959 : : {
15960 : 612 : unsigned count = sec.u ();
15961 : 612 : unsigned loaded = 0;
15962 : :
15963 : 1545 : while (count--)
15964 : : {
15965 : 321 : unsigned ix = sec.u ();
15966 : 321 : if (ix >= slurp->remap->length () || !ix || (*slurp->remap)[ix])
15967 : : {
15968 : 0 : sec.set_overrun ();
15969 : 0 : break;
15970 : : }
15971 : :
15972 : 321 : const char *name = sec.str (NULL);
15973 : 321 : module_state *imp = get_module (name);
15974 : 321 : unsigned crc = sec.u32 ();
15975 : 321 : int exportedness = 0;
15976 : :
15977 : : /* If the import is a partition, it must be the same primary
15978 : : module as this TU. */
15979 : 321 : if (imp && imp->is_partition () &&
15980 : : (!named_module_p ()
15981 : 126 : || (get_primary ((*modules)[0]) != get_primary (imp))))
15982 : : imp = NULL;
15983 : :
15984 : 321 : if (!imp)
15985 : 0 : sec.set_overrun ();
15986 : 321 : if (sec.get_overrun ())
15987 : : break;
15988 : :
15989 : 321 : if (lmaps)
15990 : : {
15991 : : /* A direct import, maybe load it. */
15992 : 321 : location_t floc = read_location (sec);
15993 : 321 : const char *fname = sec.str (NULL);
15994 : 321 : exportedness = sec.i ();
15995 : :
15996 : 321 : if (sec.get_overrun ())
15997 : : break;
15998 : :
15999 : 321 : if (!imp->check_not_purview (loc))
16000 : 3 : continue;
16001 : :
16002 : 318 : if (imp->loadedness == ML_NONE)
16003 : : {
16004 : 253 : imp->loc = floc;
16005 : 253 : imp->crc = crc;
16006 : 253 : if (!imp->get_flatname ())
16007 : 217 : imp->set_flatname ();
16008 : :
16009 : 253 : unsigned n = dump.push (imp);
16010 : :
16011 : 253 : if (!imp->filename && fname)
16012 : 214 : imp->filename = xstrdup (fname);
16013 : :
16014 : 253 : if (imp->is_partition ())
16015 : 30 : dump () && dump ("Importing elided partition %M", imp);
16016 : :
16017 : 253 : if (!imp->do_import (reader, false))
16018 : 3 : imp = NULL;
16019 : 253 : dump.pop (n);
16020 : 253 : if (!imp)
16021 : 3 : continue;
16022 : : }
16023 : :
16024 : 315 : if (is_partition ())
16025 : : {
16026 : 54 : if (!imp->is_direct ())
16027 : 27 : imp->directness = MD_PARTITION_DIRECT;
16028 : 54 : if (exportedness > 0)
16029 : 3 : imp->exported_p = true;
16030 : : }
16031 : : }
16032 : : else
16033 : : {
16034 : : /* An indirect import, find it, it should already be here. */
16035 : 0 : if (imp->loadedness == ML_NONE)
16036 : : {
16037 : 0 : error_at (loc, "indirect import %qs is not already loaded", name);
16038 : 0 : continue;
16039 : : }
16040 : : }
16041 : :
16042 : 315 : if (imp->crc != crc)
16043 : 0 : error_at (loc, "import %qs has CRC mismatch", imp->get_flatname ());
16044 : :
16045 : 315 : (*slurp->remap)[ix] = (imp->mod << 1) | (lmaps != NULL);
16046 : :
16047 : 315 : if (lmaps && exportedness >= 0)
16048 : 303 : set_import (imp, bool (exportedness));
16049 : 459 : dump () && dump ("Found %simport:%u %M->%u", !lmaps ? "indirect "
16050 : 72 : : exportedness > 0 ? "exported "
16051 : 39 : : exportedness < 0 ? "gmf" : "", ix, imp,
16052 : : imp->mod);
16053 : 315 : loaded++;
16054 : : }
16055 : :
16056 : 612 : return loaded;
16057 : : }
16058 : :
16059 : : /* Write the import table to MOD_SNAME_PFX.imp. */
16060 : :
16061 : : void
16062 : 364 : module_state::write_imports (elf_out *to, unsigned *crc_ptr)
16063 : : {
16064 : 427 : dump () && dump ("Writing imports");
16065 : 364 : dump.indent ();
16066 : :
16067 : 364 : bytes_out sec (to);
16068 : 364 : sec.begin ();
16069 : :
16070 : 364 : write_imports (sec, true);
16071 : 364 : write_imports (sec, false);
16072 : :
16073 : 364 : sec.end (to, to->name (MOD_SNAME_PFX ".imp"), crc_ptr);
16074 : 364 : dump.outdent ();
16075 : 364 : }
16076 : :
16077 : : bool
16078 : 306 : module_state::read_imports (cpp_reader *reader, line_maps *lmaps)
16079 : : {
16080 : 306 : bytes_in sec;
16081 : :
16082 : 306 : if (!sec.begin (loc, from (), MOD_SNAME_PFX ".imp"))
16083 : : return false;
16084 : :
16085 : 375 : dump () && dump ("Reading %u imports", slurp->remap->length () - 1);
16086 : 306 : dump.indent ();
16087 : :
16088 : : /* Read the imports. */
16089 : 306 : unsigned direct = read_imports (sec, reader, lmaps);
16090 : 306 : unsigned indirect = read_imports (sec, NULL, NULL);
16091 : 306 : if (direct + indirect + 1 != slurp->remap->length ())
16092 : 6 : from ()->set_error (elf::E_BAD_IMPORT);
16093 : :
16094 : 306 : dump.outdent ();
16095 : 306 : if (!sec.end (from ()))
16096 : : return false;
16097 : : return true;
16098 : 306 : }
16099 : :
16100 : : /* We're the primary module interface, but have partitions. Document
16101 : : them so that non-partition module implementation units know which
16102 : : have already been loaded. */
16103 : :
16104 : : void
16105 : 104 : module_state::write_partitions (elf_out *to, unsigned count, unsigned *crc_ptr)
16106 : : {
16107 : 122 : dump () && dump ("Writing %u elided partitions", count);
16108 : 104 : dump.indent ();
16109 : :
16110 : 104 : bytes_out sec (to);
16111 : 104 : sec.begin ();
16112 : :
16113 : 265 : for (unsigned ix = 1; ix != modules->length (); ix++)
16114 : : {
16115 : 161 : module_state *imp = (*modules)[ix];
16116 : 161 : if (imp->is_partition ())
16117 : : {
16118 : 176 : dump () && dump ("Writing elided partition %M (crc=%x)",
16119 : : imp, imp->crc);
16120 : 146 : sec.str (imp->get_flatname ());
16121 : 146 : sec.u32 (imp->crc);
16122 : 283 : write_location (sec, imp->is_direct ()
16123 : 137 : ? imp->imported_from () : UNKNOWN_LOCATION);
16124 : 146 : sec.str (imp->filename);
16125 : : }
16126 : : }
16127 : :
16128 : 104 : sec.end (to, to->name (MOD_SNAME_PFX ".prt"), crc_ptr);
16129 : 104 : dump.outdent ();
16130 : 104 : }
16131 : :
16132 : : bool
16133 : 15 : module_state::read_partitions (unsigned count)
16134 : : {
16135 : 15 : bytes_in sec;
16136 : 15 : if (!sec.begin (loc, from (), MOD_SNAME_PFX ".prt"))
16137 : : return false;
16138 : :
16139 : 18 : dump () && dump ("Reading %u elided partitions", count);
16140 : 15 : dump.indent ();
16141 : :
16142 : 42 : while (count--)
16143 : : {
16144 : 27 : const char *name = sec.str (NULL);
16145 : 27 : unsigned crc = sec.u32 ();
16146 : 27 : location_t floc = read_location (sec);
16147 : 27 : const char *fname = sec.str (NULL);
16148 : :
16149 : 27 : if (sec.get_overrun ())
16150 : : break;
16151 : :
16152 : 33 : dump () && dump ("Reading elided partition %s (crc=%x)", name, crc);
16153 : :
16154 : 27 : module_state *imp = get_module (name);
16155 : 27 : if (!imp /* Partition should be ... */
16156 : 27 : || !imp->is_partition () /* a partition ... */
16157 : 27 : || imp->loadedness != ML_NONE /* that is not yet loaded ... */
16158 : 54 : || get_primary (imp) != this) /* whose primary is this. */
16159 : : {
16160 : 0 : sec.set_overrun ();
16161 : 0 : break;
16162 : : }
16163 : :
16164 : 27 : if (!imp->has_location ())
16165 : 24 : imp->loc = floc;
16166 : 27 : imp->crc = crc;
16167 : 27 : if (!imp->filename && fname[0])
16168 : 24 : imp->filename = xstrdup (fname);
16169 : : }
16170 : :
16171 : 15 : dump.outdent ();
16172 : 15 : if (!sec.end (from ()))
16173 : : return false;
16174 : : return true;
16175 : 15 : }
16176 : :
16177 : : /* Data for config reading and writing. */
16178 : : struct module_state_config {
16179 : : const char *dialect_str = get_dialect ();
16180 : : line_map_uint_t ordinary_locs = 0;
16181 : : line_map_uint_t macro_locs = 0;
16182 : : unsigned num_imports = 0;
16183 : : unsigned num_partitions = 0;
16184 : : unsigned num_entities = 0;
16185 : : unsigned loc_range_bits = 0;
16186 : : unsigned active_init = 0;
16187 : :
16188 : 92636 : static void release ()
16189 : : {
16190 : 92636 : XDELETEVEC (dialect);
16191 : 92636 : dialect = NULL;
16192 : : }
16193 : :
16194 : : private:
16195 : : static const char *get_dialect ();
16196 : : static char *dialect;
16197 : : };
16198 : :
16199 : : char *module_state_config::dialect;
16200 : :
16201 : : /* Generate a string of the significant compilation options.
16202 : : Generally assume the user knows what they're doing, in the same way
16203 : : that object files can be mixed. */
16204 : :
16205 : : const char *
16206 : 4958 : module_state_config::get_dialect ()
16207 : : {
16208 : 4958 : if (!dialect)
16209 : 7974 : dialect = concat (get_cxx_dialect_name (cxx_dialect),
16210 : : /* C++ implies these, only show if disabled. */
16211 : 3987 : flag_exceptions ? "" : "/no-exceptions",
16212 : 3987 : flag_rtti ? "" : "/no-rtti",
16213 : 3987 : flag_new_inheriting_ctors ? "" : "/old-inheriting-ctors",
16214 : : /* C++ 20 implies concepts and coroutines. */
16215 : 1286 : cxx_dialect < cxx20 && flag_concepts ? "/concepts" : "",
16216 : 3987 : (cxx_dialect < cxx20 && flag_coroutines
16217 : : ? "/coroutines" : ""),
16218 : 3987 : flag_module_implicit_inline ? "/implicit-inline" : "",
16219 : 3987 : flag_contracts ? "/contracts" : "",
16220 : : NULL);
16221 : :
16222 : 4958 : return dialect;
16223 : : }
16224 : :
16225 : : /* Contents of a cluster. */
16226 : : enum cluster_tag {
16227 : : ct_decl, /* A decl. */
16228 : : ct_defn, /* A definition. */
16229 : : ct_bind, /* A binding. */
16230 : : ct_hwm
16231 : : };
16232 : :
16233 : : /* Binding modifiers. */
16234 : : enum ct_bind_flags
16235 : : {
16236 : : cbf_export = 0x1, /* An exported decl. */
16237 : : cbf_hidden = 0x2, /* A hidden (friend) decl. */
16238 : : cbf_using = 0x4, /* A using decl. */
16239 : : };
16240 : :
16241 : : /* DEP belongs to a different cluster, seed it to prevent
16242 : : unfortunately timed duplicate import. */
16243 : : // FIXME: QOI For inter-cluster references we could just only pick
16244 : : // one entity from an earlier cluster. Even better track
16245 : : // dependencies between earlier clusters
16246 : :
16247 : : void
16248 : 4842487 : module_state::intercluster_seed (trees_out &sec, unsigned index_hwm, depset *dep)
16249 : : {
16250 : 4842487 : if (dep->is_tu_local ())
16251 : : /* We only stream placeholders for TU-local entities anyway. */;
16252 : 4842334 : else if (dep->is_import () || dep->cluster < index_hwm)
16253 : : {
16254 : 2182909 : tree ent = dep->get_entity ();
16255 : 2182909 : if (!TREE_VISITED (ent))
16256 : : {
16257 : 968419 : sec.tree_node (ent);
16258 : 968809 : dump (dumper::CLUSTER)
16259 : 390 : && dump ("Seeded %s %N",
16260 : 390 : dep->is_import () ? "import" : "intercluster", ent);
16261 : : }
16262 : : }
16263 : 4842487 : }
16264 : :
16265 : : /* Write the cluster of depsets in SCC[0-SIZE).
16266 : : dep->section -> section number
16267 : : dep->cluster -> entity number
16268 : : */
16269 : :
16270 : : unsigned
16271 : 221041 : module_state::write_cluster (elf_out *to, depset *scc[], unsigned size,
16272 : : depset::hash &table, unsigned *counts,
16273 : : unsigned *crc_ptr)
16274 : : {
16275 : 222283 : dump () && dump ("Writing section:%u %u depsets", table.section, size);
16276 : 221041 : dump.indent ();
16277 : :
16278 : 221041 : trees_out sec (to, this, table, table.section);
16279 : 221041 : sec.begin ();
16280 : 221041 : unsigned index_lwm = counts[MSC_entities];
16281 : :
16282 : : /* Determine entity numbers, mark for writing. */
16283 : 221323 : dump (dumper::CLUSTER) && dump ("Cluster members:") && (dump.indent (), true);
16284 : 1109128 : for (unsigned ix = 0; ix != size; ix++)
16285 : : {
16286 : 888087 : depset *b = scc[ix];
16287 : :
16288 : 888087 : switch (b->get_entity_kind ())
16289 : : {
16290 : 0 : default:
16291 : 0 : gcc_unreachable ();
16292 : :
16293 : 118431 : case depset::EK_BINDING:
16294 : 118431 : {
16295 : 118431 : dump (dumper::CLUSTER)
16296 : 195 : && dump ("[%u]=%s %P", ix, b->entity_kind_name (),
16297 : : b->get_entity (), b->get_name ());
16298 : 118431 : depset *ns_dep = b->deps[0];
16299 : 118431 : gcc_checking_assert (ns_dep->get_entity_kind ()
16300 : : == depset::EK_NAMESPACE
16301 : : && ns_dep->get_entity () == b->get_entity ());
16302 : 265158 : for (unsigned jx = b->deps.length (); --jx;)
16303 : : {
16304 : 146727 : depset *dep = b->deps[jx];
16305 : : // We could be declaring something that is also a
16306 : : // (merged) import
16307 : 162235 : gcc_checking_assert (dep->is_import ()
16308 : : || TREE_VISITED (dep->get_entity ())
16309 : : || (dep->get_entity_kind ()
16310 : : == depset::EK_USING));
16311 : : }
16312 : : }
16313 : : break;
16314 : :
16315 : 754148 : case depset::EK_DECL:
16316 : 754148 : case depset::EK_SPECIALIZATION:
16317 : 754148 : case depset::EK_PARTIAL:
16318 : 754148 : b->cluster = counts[MSC_entities]++;
16319 : 754148 : sec.mark_declaration (b->get_entity (), b->has_defn ());
16320 : : /* FALLTHROUGH */
16321 : :
16322 : 769656 : case depset::EK_USING:
16323 : 1539312 : gcc_checking_assert (!b->is_import ()
16324 : : && !b->is_unreached ());
16325 : 890232 : dump (dumper::CLUSTER)
16326 : 1014 : && dump ("[%u]=%s %s %N", ix, b->entity_kind_name (),
16327 : 639 : b->has_defn () ? "definition" : "declaration",
16328 : : b->get_entity ());
16329 : : break;
16330 : : }
16331 : : }
16332 : 221323 : dump (dumper::CLUSTER) && (dump.outdent (), true);
16333 : :
16334 : : /* Ensure every out-of-cluster decl is referenced before we start
16335 : : streaming. We must do both imports *and* earlier clusters,
16336 : : because the latter could reach into the former and cause a
16337 : : duplicate loop. */
16338 : 221041 : sec.set_importing (+1);
16339 : 1109128 : for (unsigned ix = 0; ix != size; ix++)
16340 : : {
16341 : 888087 : depset *b = scc[ix];
16342 : 10332625 : for (unsigned jx = b->is_special (); jx != b->deps.length (); jx++)
16343 : : {
16344 : 4280502 : depset *dep = b->deps[jx];
16345 : :
16346 : 4280502 : if (dep->is_binding ())
16347 : : {
16348 : 855439 : for (unsigned ix = dep->deps.length (); --ix;)
16349 : : {
16350 : 561985 : depset *bind = dep->deps[ix];
16351 : 561985 : if (bind->get_entity_kind () == depset::EK_USING)
16352 : 40204 : bind = bind->deps[1];
16353 : :
16354 : 561985 : intercluster_seed (sec, index_lwm, bind);
16355 : : }
16356 : : /* Also check the namespace itself. */
16357 : 146727 : dep = dep->deps[0];
16358 : : }
16359 : :
16360 : 4280502 : intercluster_seed (sec, index_lwm, dep);
16361 : : }
16362 : : }
16363 : 221041 : sec.tree_node (NULL_TREE);
16364 : : /* We're done importing now. */
16365 : 221041 : sec.set_importing (-1);
16366 : :
16367 : : /* Write non-definitions. */
16368 : 1109128 : for (unsigned ix = 0; ix != size; ix++)
16369 : : {
16370 : 888087 : depset *b = scc[ix];
16371 : 888087 : tree decl = b->get_entity ();
16372 : 888087 : switch (b->get_entity_kind ())
16373 : : {
16374 : 0 : default:
16375 : 0 : gcc_unreachable ();
16376 : 118431 : break;
16377 : :
16378 : 118431 : case depset::EK_BINDING:
16379 : 118431 : {
16380 : 118431 : gcc_assert (TREE_CODE (decl) == NAMESPACE_DECL);
16381 : 119451 : dump () && dump ("Depset:%u binding %C:%P", ix, TREE_CODE (decl),
16382 : : decl, b->get_name ());
16383 : 118431 : sec.u (ct_bind);
16384 : 118431 : sec.tree_node (decl);
16385 : 118431 : sec.tree_node (b->get_name ());
16386 : :
16387 : : /* Write in reverse order, so reading will see the exports
16388 : : first, thus building the overload chain will be
16389 : : optimized. */
16390 : 383589 : for (unsigned jx = b->deps.length (); --jx;)
16391 : : {
16392 : 146727 : depset *dep = b->deps[jx];
16393 : 146727 : tree bound = dep->get_entity ();
16394 : 146727 : unsigned flags = 0;
16395 : 146727 : if (dep->get_entity_kind () == depset::EK_USING)
16396 : : {
16397 : 15508 : tree ovl = bound;
16398 : 15508 : bound = OVL_FUNCTION (bound);
16399 : 15508 : if (!(TREE_CODE (bound) == CONST_DECL
16400 : 5786 : && UNSCOPED_ENUM_P (TREE_TYPE (bound))
16401 : 5774 : && decl == TYPE_NAME (TREE_TYPE (bound))))
16402 : : /* An unscoped enumerator in its enumeration's
16403 : : scope is not a using. */
16404 : : flags |= cbf_using;
16405 : 15508 : if (OVL_EXPORT_P (ovl))
16406 : 14348 : flags |= cbf_export;
16407 : : }
16408 : : else
16409 : : {
16410 : : /* An implicit typedef must be at one. */
16411 : 131219 : gcc_assert (!DECL_IMPLICIT_TYPEDEF_P (bound) || jx == 1);
16412 : 131219 : if (dep->is_hidden ())
16413 : : flags |= cbf_hidden;
16414 : 127569 : else if (DECL_MODULE_EXPORT_P (STRIP_TEMPLATE (bound)))
16415 : 116984 : flags |= cbf_export;
16416 : : }
16417 : :
16418 : 146727 : gcc_checking_assert (DECL_P (bound));
16419 : :
16420 : 146727 : sec.i (flags);
16421 : 146727 : sec.tree_node (bound);
16422 : : }
16423 : :
16424 : : /* Terminate the list. */
16425 : 118431 : sec.i (-1);
16426 : : }
16427 : 118431 : break;
16428 : :
16429 : 15508 : case depset::EK_USING:
16430 : 15532 : dump () && dump ("Depset:%u %s %C:%N", ix, b->entity_kind_name (),
16431 : 24 : TREE_CODE (decl), decl);
16432 : : break;
16433 : :
16434 : 754148 : case depset::EK_SPECIALIZATION:
16435 : 754148 : case depset::EK_PARTIAL:
16436 : 754148 : case depset::EK_DECL:
16437 : 756269 : dump () && dump ("Depset:%u %s entity:%u %C:%N", ix,
16438 : : b->entity_kind_name (), b->cluster,
16439 : 2121 : TREE_CODE (decl), decl);
16440 : :
16441 : 754148 : sec.u (ct_decl);
16442 : 754148 : sec.tree_node (decl);
16443 : :
16444 : 890208 : dump () && dump ("Wrote declaration entity:%u %C:%N",
16445 : 2121 : b->cluster, TREE_CODE (decl), decl);
16446 : : break;
16447 : : }
16448 : : }
16449 : :
16450 : : depset *namer = NULL;
16451 : :
16452 : : /* Write out definitions */
16453 : 1109128 : for (unsigned ix = 0; ix != size; ix++)
16454 : : {
16455 : 888087 : depset *b = scc[ix];
16456 : 888087 : tree decl = b->get_entity ();
16457 : 888087 : switch (b->get_entity_kind ())
16458 : : {
16459 : : default:
16460 : : break;
16461 : :
16462 : 754148 : case depset::EK_SPECIALIZATION:
16463 : 754148 : case depset::EK_PARTIAL:
16464 : 754148 : case depset::EK_DECL:
16465 : 754148 : if (!namer)
16466 : 213319 : namer = b;
16467 : :
16468 : 754148 : if (b->has_defn ())
16469 : : {
16470 : 278798 : sec.u (ct_defn);
16471 : 278798 : sec.tree_node (decl);
16472 : 279362 : dump () && dump ("Writing definition %N", decl);
16473 : 278798 : sec.write_definition (decl, b->refs_tu_local ());
16474 : :
16475 : 278798 : if (!namer->has_defn ())
16476 : 888087 : namer = b;
16477 : : }
16478 : : break;
16479 : : }
16480 : : }
16481 : :
16482 : : /* We don't find the section by name. Use depset's decl's name for
16483 : : human friendliness. */
16484 : 221041 : unsigned name = 0;
16485 : 221041 : tree naming_decl = NULL_TREE;
16486 : 221041 : if (namer)
16487 : : {
16488 : 213319 : naming_decl = namer->get_entity ();
16489 : 213319 : if (namer->get_entity_kind () == depset::EK_USING)
16490 : : /* This unfortunately names the section from the target of the
16491 : : using decl. But the name is only a guide, so Do Not Care. */
16492 : 0 : naming_decl = OVL_FUNCTION (naming_decl);
16493 : 213319 : if (DECL_IMPLICIT_TYPEDEF_P (naming_decl))
16494 : : /* Lose any anonymousness. */
16495 : 72502 : naming_decl = TYPE_NAME (TREE_TYPE (naming_decl));
16496 : 213319 : name = to->qualified_name (naming_decl, namer->has_defn ());
16497 : : }
16498 : :
16499 : 221041 : unsigned bytes = sec.pos;
16500 : 221041 : unsigned snum = sec.end (to, name, crc_ptr);
16501 : :
16502 : 1109128 : for (unsigned ix = size; ix--;)
16503 : 888087 : gcc_checking_assert (scc[ix]->section == snum);
16504 : :
16505 : 221041 : dump.outdent ();
16506 : 222283 : dump () && dump ("Wrote section:%u named-by:%N", table.section, naming_decl);
16507 : :
16508 : 221041 : return bytes;
16509 : 221041 : }
16510 : :
16511 : : /* Read a cluster from section SNUM. */
16512 : :
16513 : : bool
16514 : 163652 : module_state::read_cluster (unsigned snum)
16515 : : {
16516 : 163652 : trees_in sec (this);
16517 : :
16518 : 163652 : if (!sec.begin (loc, from (), snum))
16519 : : return false;
16520 : :
16521 : 164740 : dump () && dump ("Reading section:%u", snum);
16522 : 163652 : dump.indent ();
16523 : :
16524 : : /* We care about structural equality. */
16525 : 163652 : comparing_dependent_aliases++;
16526 : :
16527 : : /* First seed the imports. */
16528 : 927309 : while (tree import = sec.tree_node ())
16529 : 1691131 : dump (dumper::CLUSTER) && dump ("Seeded import %N", import);
16530 : :
16531 : 1090651 : while (!sec.get_overrun () && sec.more_p ())
16532 : : {
16533 : 926999 : unsigned ct = sec.u ();
16534 : 926999 : switch (ct)
16535 : : {
16536 : 0 : default:
16537 : 0 : sec.set_overrun ();
16538 : 0 : break;
16539 : :
16540 : 88804 : case ct_bind:
16541 : : /* A set of namespace bindings. */
16542 : 88804 : {
16543 : 88804 : tree ns = sec.tree_node ();
16544 : 88804 : tree name = sec.tree_node ();
16545 : 88804 : tree decls = NULL_TREE;
16546 : 88804 : tree visible = NULL_TREE;
16547 : 88804 : tree type = NULL_TREE;
16548 : 88804 : bool dedup = false;
16549 : 88804 : bool global_p = is_header ();
16550 : :
16551 : : /* We rely on the bindings being in the reverse order of
16552 : : the resulting overload set. */
16553 : 201613 : for (;;)
16554 : : {
16555 : 201613 : int flags = sec.i ();
16556 : 201613 : if (flags < 0)
16557 : : break;
16558 : :
16559 : 112809 : if ((flags & cbf_hidden)
16560 : 3042 : && (flags & (cbf_using | cbf_export)))
16561 : 0 : sec.set_overrun ();
16562 : :
16563 : 112809 : tree decl = sec.tree_node ();
16564 : 112809 : if (sec.get_overrun ())
16565 : : break;
16566 : :
16567 : 112809 : if (!global_p)
16568 : : {
16569 : : /* Check if the decl could require GM merging. */
16570 : 2528 : tree orig = get_originating_module_decl (decl);
16571 : 2528 : tree inner = STRIP_TEMPLATE (orig);
16572 : 2528 : if (!DECL_LANG_SPECIFIC (inner)
16573 : 5056 : || !DECL_MODULE_ATTACH_P (inner))
16574 : : global_p = true;
16575 : : }
16576 : :
16577 : 112809 : if (decls && TREE_CODE (decl) == TYPE_DECL)
16578 : : {
16579 : : /* Stat hack. */
16580 : 45 : if (type || !DECL_IMPLICIT_TYPEDEF_P (decl))
16581 : 0 : sec.set_overrun ();
16582 : :
16583 : 45 : if (flags & cbf_using)
16584 : : {
16585 : 3 : type = build_lang_decl_loc (UNKNOWN_LOCATION,
16586 : : USING_DECL,
16587 : 3 : DECL_NAME (decl),
16588 : : NULL_TREE);
16589 : 3 : USING_DECL_DECLS (type) = decl;
16590 : 3 : USING_DECL_SCOPE (type) = CP_DECL_CONTEXT (decl);
16591 : 3 : DECL_CONTEXT (type) = ns;
16592 : :
16593 : 3 : DECL_MODULE_PURVIEW_P (type) = true;
16594 : 3 : if (flags & cbf_export)
16595 : 3 : DECL_MODULE_EXPORT_P (type) = true;
16596 : : }
16597 : : else
16598 : : type = decl;
16599 : : }
16600 : : else
16601 : : {
16602 : 112764 : if ((flags & cbf_using) &&
16603 : 12076 : !DECL_DECLARES_FUNCTION_P (decl))
16604 : : {
16605 : : /* We should only see a single non-function using-decl
16606 : : for a binding; more than that would clash. */
16607 : 6115 : if (decls)
16608 : 0 : sec.set_overrun ();
16609 : :
16610 : : /* FIXME: Propagate the location of the using-decl
16611 : : for use in diagnostics. */
16612 : 6115 : decls = build_lang_decl_loc (UNKNOWN_LOCATION,
16613 : : USING_DECL,
16614 : 6115 : DECL_NAME (decl),
16615 : : NULL_TREE);
16616 : 6115 : USING_DECL_DECLS (decls) = decl;
16617 : : /* We don't currently record the actual scope of the
16618 : : using-declaration, but this approximation should
16619 : : generally be good enough. */
16620 : 6115 : USING_DECL_SCOPE (decls) = CP_DECL_CONTEXT (decl);
16621 : 6115 : DECL_CONTEXT (decls) = ns;
16622 : :
16623 : 6115 : DECL_MODULE_PURVIEW_P (decls) = true;
16624 : 6115 : if (flags & cbf_export)
16625 : 6109 : DECL_MODULE_EXPORT_P (decls) = true;
16626 : : }
16627 : 106649 : else if (decls
16628 : 82689 : || (flags & (cbf_hidden | cbf_using))
16629 : 184525 : || DECL_FUNCTION_TEMPLATE_P (decl))
16630 : : {
16631 : 39904 : decls = ovl_make (decl, decls);
16632 : 39904 : if (flags & cbf_using)
16633 : : {
16634 : 5961 : dedup = true;
16635 : 5961 : OVL_USING_P (decls) = true;
16636 : 5961 : OVL_PURVIEW_P (decls) = true;
16637 : 5961 : if (flags & cbf_export)
16638 : 5946 : OVL_EXPORT_P (decls) = true;
16639 : : }
16640 : :
16641 : 39904 : if (flags & cbf_hidden)
16642 : 3042 : OVL_HIDDEN_P (decls) = true;
16643 : 36862 : else if (dedup)
16644 : 5961 : OVL_DEDUP_P (decls) = true;
16645 : : }
16646 : : else
16647 : : decls = decl;
16648 : :
16649 : 112758 : if (flags & cbf_export
16650 : 112764 : || (!(flags & cbf_hidden)
16651 : 684 : && (is_module () || is_partition ())))
16652 : : visible = decls;
16653 : : }
16654 : : }
16655 : :
16656 : 88804 : if (!decls)
16657 : 0 : sec.set_overrun ();
16658 : :
16659 : 88804 : if (sec.get_overrun ())
16660 : : break; /* Bail. */
16661 : :
16662 : 89603 : dump () && dump ("Binding of %P", ns, name);
16663 : 88804 : if (!set_module_binding (ns, name, mod, global_p,
16664 : 88804 : is_module () || is_partition (),
16665 : : decls, type, visible))
16666 : 0 : sec.set_overrun ();
16667 : : }
16668 : : break;
16669 : :
16670 : 609950 : case ct_decl:
16671 : : /* A decl. */
16672 : 609950 : {
16673 : 609950 : tree decl = sec.tree_node ();
16674 : 1703376 : dump () && dump ("Read declaration of %N", decl);
16675 : : }
16676 : : break;
16677 : :
16678 : 228245 : case ct_defn:
16679 : 228245 : {
16680 : 228245 : tree decl = sec.tree_node ();
16681 : 229260 : dump () && dump ("Reading definition of %N", decl);
16682 : 228245 : sec.read_definition (decl);
16683 : : }
16684 : 228245 : break;
16685 : : }
16686 : : }
16687 : :
16688 : : /* When lazy loading is in effect, we can be in the middle of
16689 : : parsing or instantiating a function. Save it away.
16690 : : push_function_context does too much work. */
16691 : 163652 : tree old_cfd = current_function_decl;
16692 : 163652 : struct function *old_cfun = cfun;
16693 : 261243 : for (const post_process_data& pdata : sec.post_process ())
16694 : : {
16695 : 69047 : tree decl = pdata.decl;
16696 : :
16697 : 69047 : bool abstract = false;
16698 : 69047 : if (TREE_CODE (decl) == TEMPLATE_DECL)
16699 : : {
16700 : 38231 : abstract = true;
16701 : 38231 : decl = DECL_TEMPLATE_RESULT (decl);
16702 : : }
16703 : :
16704 : 69047 : current_function_decl = decl;
16705 : 69047 : allocate_struct_function (decl, abstract);
16706 : 69047 : cfun->language = ggc_cleared_alloc<language_function> ();
16707 : 69047 : cfun->language->base.x_stmt_tree.stmts_are_full_exprs_p = 1;
16708 : 69047 : cfun->function_start_locus = pdata.start_locus;
16709 : 69047 : cfun->function_end_locus = pdata.end_locus;
16710 : 69047 : cfun->language->returns_value = pdata.returns_value;
16711 : 69047 : cfun->language->returns_null = pdata.returns_null;
16712 : 69047 : cfun->language->returns_abnormally = pdata.returns_abnormally;
16713 : 69047 : cfun->language->infinite_loop = pdata.infinite_loop;
16714 : :
16715 : : /* Make sure we emit explicit instantiations.
16716 : : FIXME do we want to do this in expand_or_defer_fn instead? */
16717 : 69047 : if (DECL_EXPLICIT_INSTANTIATION (decl)
16718 : 69047 : && !DECL_EXTERNAL (decl))
16719 : 12 : setup_explicit_instantiation_definition_linkage (decl);
16720 : :
16721 : 69047 : if (abstract)
16722 : : ;
16723 : 30816 : else if (DECL_MAYBE_IN_CHARGE_CDTOR_P (decl))
16724 : 5301 : vec_safe_push (post_load_decls, decl);
16725 : : else
16726 : : {
16727 : 25515 : bool aggr = aggregate_value_p (DECL_RESULT (decl), decl);
16728 : : #ifdef PCC_STATIC_STRUCT_RETURN
16729 : : cfun->returns_pcc_struct = aggr;
16730 : : #endif
16731 : 25515 : cfun->returns_struct = aggr;
16732 : 25515 : expand_or_defer_fn (decl);
16733 : :
16734 : : /* If we first see this function after at_eof, it doesn't get
16735 : : note_vague_linkage_fn from tentative_decl_linkage, so the loop in
16736 : : c_parse_final_cleanups won't consider it. But with DECL_COMDAT we
16737 : : can just clear DECL_EXTERNAL and let cgraph decide.
16738 : : FIXME handle this outside module.cc after GCC 15. */
16739 : 2782 : if (at_eof && DECL_COMDAT (decl) && DECL_EXTERNAL (decl)
16740 : 26225 : && DECL_NOT_REALLY_EXTERN (decl))
16741 : 678 : DECL_EXTERNAL (decl) = false;
16742 : : }
16743 : :
16744 : : }
16745 : : /* Look, function.cc's interface to cfun does too much for us, we
16746 : : just need to restore the old value. I do not want to go
16747 : : redesigning that API right now. */
16748 : : #undef cfun
16749 : 163652 : cfun = old_cfun;
16750 : 163652 : current_function_decl = old_cfd;
16751 : 163652 : comparing_dependent_aliases--;
16752 : :
16753 : 163652 : dump.outdent ();
16754 : 164740 : dump () && dump ("Read section:%u", snum);
16755 : :
16756 : 163652 : loaded_clusters++;
16757 : :
16758 : 163652 : if (!sec.end (from ()))
16759 : : return false;
16760 : :
16761 : : return true;
16762 : 163652 : }
16763 : :
16764 : : void
16765 : 120294 : module_state::write_namespace (bytes_out &sec, depset *dep)
16766 : : {
16767 : 120294 : unsigned ns_num = dep->cluster;
16768 : 120294 : unsigned ns_import = 0;
16769 : :
16770 : 120294 : if (dep->is_import ())
16771 : 0 : ns_import = dep->section;
16772 : 120294 : else if (dep->get_entity () != global_namespace)
16773 : 73059 : ns_num++;
16774 : :
16775 : 120294 : sec.u (ns_import);
16776 : 120294 : sec.u (ns_num);
16777 : 120294 : }
16778 : :
16779 : : tree
16780 : 145089 : module_state::read_namespace (bytes_in &sec)
16781 : : {
16782 : 145089 : unsigned ns_import = sec.u ();
16783 : 145089 : unsigned ns_num = sec.u ();
16784 : 145089 : tree ns = NULL_TREE;
16785 : :
16786 : 145089 : if (ns_import || ns_num)
16787 : : {
16788 : 86859 : if (!ns_import)
16789 : 86859 : ns_num--;
16790 : :
16791 : 86859 : if (unsigned origin = slurp->remap_module (ns_import))
16792 : : {
16793 : 86859 : module_state *from = (*modules)[origin];
16794 : 86859 : if (ns_num < from->entity_num)
16795 : : {
16796 : 86859 : binding_slot &slot = (*entity_ary)[from->entity_lwm + ns_num];
16797 : :
16798 : 86859 : if (!slot.is_lazy ())
16799 : 86859 : ns = slot;
16800 : : }
16801 : : }
16802 : : else
16803 : 0 : sec.set_overrun ();
16804 : : }
16805 : : else
16806 : 58230 : ns = global_namespace;
16807 : :
16808 : 145089 : return ns;
16809 : : }
16810 : :
16811 : : /* SPACES is a sorted vector of namespaces. Write out the namespaces
16812 : : to MOD_SNAME_PFX.nms section. */
16813 : :
16814 : : void
16815 : 436 : module_state::write_namespaces (elf_out *to, vec<depset *> spaces,
16816 : : unsigned num, unsigned *crc_p)
16817 : : {
16818 : 472 : dump () && dump ("Writing namespaces");
16819 : 436 : dump.indent ();
16820 : :
16821 : 436 : bytes_out sec (to);
16822 : 436 : sec.begin ();
16823 : :
16824 : 2299 : for (unsigned ix = 0; ix != num; ix++)
16825 : : {
16826 : 1863 : depset *b = spaces[ix];
16827 : 1863 : tree ns = b->get_entity ();
16828 : :
16829 : : /* This could be an anonymous namespace even for a named module,
16830 : : since we can still emit no-linkage decls. */
16831 : 1863 : gcc_checking_assert (TREE_CODE (ns) == NAMESPACE_DECL);
16832 : :
16833 : 1863 : unsigned flags = 0;
16834 : 1863 : if (TREE_PUBLIC (ns))
16835 : 1819 : flags |= 1;
16836 : 1863 : if (DECL_NAMESPACE_INLINE_P (ns))
16837 : 345 : flags |= 2;
16838 : 1863 : if (DECL_MODULE_PURVIEW_P (ns))
16839 : 1538 : flags |= 4;
16840 : 1863 : if (DECL_MODULE_EXPORT_P (ns))
16841 : 1394 : flags |= 8;
16842 : :
16843 : 1929 : dump () && dump ("Writing namespace:%u %N%s%s%s%s",
16844 : : b->cluster, ns,
16845 : 66 : flags & 1 ? ", public" : "",
16846 : 66 : flags & 2 ? ", inline" : "",
16847 : 66 : flags & 4 ? ", purview" : "",
16848 : 66 : flags & 8 ? ", export" : "");
16849 : 1863 : sec.u (b->cluster);
16850 : 1863 : sec.u (to->name (DECL_NAME (ns)));
16851 : 1863 : write_namespace (sec, b->deps[0]);
16852 : :
16853 : 1863 : sec.u (flags);
16854 : 1863 : write_location (sec, DECL_SOURCE_LOCATION (ns));
16855 : :
16856 : 1863 : if (DECL_NAMESPACE_INLINE_P (ns))
16857 : : {
16858 : 345 : if (tree attr = lookup_attribute ("abi_tag", DECL_ATTRIBUTES (ns)))
16859 : : {
16860 : 102 : tree tags = TREE_VALUE (attr);
16861 : 102 : sec.u (list_length (tags));
16862 : 207 : for (tree tag = tags; tag; tag = TREE_CHAIN (tag))
16863 : 105 : sec.str (TREE_STRING_POINTER (TREE_VALUE (tag)));
16864 : : }
16865 : : else
16866 : 243 : sec.u (0);
16867 : : }
16868 : : }
16869 : :
16870 : 436 : sec.end (to, to->name (MOD_SNAME_PFX ".nms"), crc_p);
16871 : 436 : dump.outdent ();
16872 : 436 : }
16873 : :
16874 : : /* Read the namespace hierarchy from MOD_SNAME_PFX.namespace. Fill in
16875 : : SPACES from that data. */
16876 : :
16877 : : bool
16878 : 438 : module_state::read_namespaces (unsigned num)
16879 : : {
16880 : 438 : bytes_in sec;
16881 : :
16882 : 438 : if (!sec.begin (loc, from (), MOD_SNAME_PFX ".nms"))
16883 : : return false;
16884 : :
16885 : 486 : dump () && dump ("Reading namespaces");
16886 : 438 : dump.indent ();
16887 : :
16888 : 2553 : for (unsigned ix = 0; ix != num; ix++)
16889 : : {
16890 : 2115 : unsigned entity_index = sec.u ();
16891 : 2115 : unsigned name = sec.u ();
16892 : :
16893 : 2115 : tree parent = read_namespace (sec);
16894 : :
16895 : : /* See comment in write_namespace about why not bits. */
16896 : 2115 : unsigned flags = sec.u ();
16897 : 2115 : location_t src_loc = read_location (sec);
16898 : 2115 : unsigned tags_count = (flags & 2) ? sec.u () : 0;
16899 : :
16900 : 2115 : if (entity_index >= entity_num
16901 : 2115 : || !parent
16902 : 2115 : || (flags & 0xc) == 0x8)
16903 : 0 : sec.set_overrun ();
16904 : :
16905 : : tree tags = NULL_TREE;
16906 : 2228 : while (tags_count--)
16907 : : {
16908 : 113 : size_t len;
16909 : 113 : const char *str = sec.str (&len);
16910 : 113 : tags = tree_cons (NULL_TREE, build_string (len + 1, str), tags);
16911 : 113 : tags = nreverse (tags);
16912 : : }
16913 : :
16914 : 2115 : if (sec.get_overrun ())
16915 : : break;
16916 : :
16917 : 4186 : tree id = name ? get_identifier (from ()->name (name)) : NULL_TREE;
16918 : :
16919 : 2235 : dump () && dump ("Read namespace:%u %P%s%s%s%s",
16920 : : entity_index, parent, id,
16921 : 60 : flags & 1 ? ", public" : "",
16922 : : flags & 2 ? ", inline" : "",
16923 : 60 : flags & 4 ? ", purview" : "",
16924 : 60 : flags & 8 ? ", export" : "");
16925 : 2115 : bool visible_p = ((flags & 8)
16926 : 2115 : || ((flags & 1)
16927 : 474 : && (flags & 4)
16928 : 41 : && (is_partition () || is_module ())));
16929 : 2115 : tree inner = add_imported_namespace (parent, id, src_loc, mod,
16930 : : bool (flags & 2), visible_p);
16931 : 2115 : if (!inner)
16932 : : {
16933 : 0 : sec.set_overrun ();
16934 : 0 : break;
16935 : : }
16936 : :
16937 : 2115 : if (is_partition ())
16938 : : {
16939 : 28 : if (flags & 4)
16940 : 23 : DECL_MODULE_PURVIEW_P (inner) = true;
16941 : 28 : if (flags & 8)
16942 : 23 : DECL_MODULE_EXPORT_P (inner) = true;
16943 : : }
16944 : :
16945 : 2115 : if (tags)
16946 : 110 : DECL_ATTRIBUTES (inner)
16947 : 220 : = tree_cons (get_identifier ("abi_tag"), tags, DECL_ATTRIBUTES (inner));
16948 : :
16949 : : /* Install the namespace. */
16950 : 2115 : (*entity_ary)[entity_lwm + entity_index] = inner;
16951 : 2115 : if (DECL_MODULE_IMPORT_P (inner))
16952 : : {
16953 : 0 : bool existed;
16954 : 0 : unsigned *slot = &entity_map->get_or_insert
16955 : 0 : (DECL_UID (inner), &existed);
16956 : 0 : if (existed)
16957 : : /* If it existed, it should match. */
16958 : 0 : gcc_checking_assert (inner == (*entity_ary)[*slot]);
16959 : : else
16960 : 0 : *slot = entity_lwm + entity_index;
16961 : : }
16962 : : }
16963 : 438 : dump.outdent ();
16964 : 438 : if (!sec.end (from ()))
16965 : : return false;
16966 : : return true;
16967 : 438 : }
16968 : :
16969 : : /* Write the binding TABLE to MOD_SNAME_PFX.bnd */
16970 : :
16971 : : unsigned
16972 : 2302 : module_state::write_bindings (elf_out *to, vec<depset *> sccs, unsigned *crc_p)
16973 : : {
16974 : 2550 : dump () && dump ("Writing binding table");
16975 : 2302 : dump.indent ();
16976 : :
16977 : 2302 : unsigned num = 0;
16978 : 2302 : bytes_out sec (to);
16979 : 2302 : sec.begin ();
16980 : :
16981 : 1788636 : for (unsigned ix = 0; ix != sccs.length (); ix++)
16982 : : {
16983 : 892016 : depset *b = sccs[ix];
16984 : 892016 : if (b->is_binding ())
16985 : : {
16986 : 118431 : tree ns = b->get_entity ();
16987 : 119451 : dump () && dump ("Bindings %P section:%u", ns, b->get_name (),
16988 : : b->section);
16989 : 118431 : sec.u (to->name (b->get_name ()));
16990 : 118431 : write_namespace (sec, b->deps[0]);
16991 : 118431 : sec.u (b->section);
16992 : 118431 : num++;
16993 : : }
16994 : : }
16995 : :
16996 : 2302 : sec.end (to, to->name (MOD_SNAME_PFX ".bnd"), crc_p);
16997 : 2302 : dump.outdent ();
16998 : :
16999 : 2302 : return num;
17000 : 2302 : }
17001 : :
17002 : : /* Read the binding table from MOD_SNAME_PFX.bind. */
17003 : :
17004 : : bool
17005 : 2458 : module_state::read_bindings (unsigned num, unsigned lwm, unsigned hwm)
17006 : : {
17007 : 2458 : bytes_in sec;
17008 : :
17009 : 2458 : if (!sec.begin (loc, from (), MOD_SNAME_PFX ".bnd"))
17010 : : return false;
17011 : :
17012 : 2919 : dump () && dump ("Reading binding table");
17013 : 2458 : dump.indent ();
17014 : 145432 : for (; !sec.get_overrun () && num--;)
17015 : : {
17016 : 142974 : const char *name = from ()->name (sec.u ());
17017 : 142974 : tree ns = read_namespace (sec);
17018 : 142974 : unsigned snum = sec.u ();
17019 : :
17020 : 142974 : if (!ns || !name || (snum - lwm) >= (hwm - lwm))
17021 : 0 : sec.set_overrun ();
17022 : 142974 : if (!sec.get_overrun ())
17023 : : {
17024 : 142974 : tree id = get_identifier (name);
17025 : 144376 : dump () && dump ("Bindings %P section:%u", ns, id, snum);
17026 : 142974 : if (mod && !import_module_binding (ns, id, mod, snum))
17027 : : break;
17028 : : }
17029 : : }
17030 : :
17031 : 2458 : dump.outdent ();
17032 : 2458 : if (!sec.end (from ()))
17033 : : return false;
17034 : : return true;
17035 : 2458 : }
17036 : :
17037 : : /* Write the entity table to MOD_SNAME_PFX.ent
17038 : :
17039 : : Each entry is a section number. */
17040 : :
17041 : : void
17042 : 2063 : module_state::write_entities (elf_out *to, vec<depset *> depsets,
17043 : : unsigned count, unsigned *crc_p)
17044 : : {
17045 : 2267 : dump () && dump ("Writing entities");
17046 : 2063 : dump.indent ();
17047 : :
17048 : 2063 : bytes_out sec (to);
17049 : 2063 : sec.begin ();
17050 : :
17051 : 2063 : unsigned current = 0;
17052 : 894064 : for (unsigned ix = 0; ix < depsets.length (); ix++)
17053 : : {
17054 : 892001 : depset *d = depsets[ix];
17055 : :
17056 : 1650075 : switch (d->get_entity_kind ())
17057 : : {
17058 : : default:
17059 : : break;
17060 : :
17061 : 3926 : case depset::EK_NAMESPACE:
17062 : 3926 : if (!d->is_import () && d->get_entity () != global_namespace)
17063 : : {
17064 : 1863 : gcc_checking_assert (d->cluster == current);
17065 : 1863 : current++;
17066 : 1863 : sec.u (0);
17067 : : }
17068 : : break;
17069 : :
17070 : 754148 : case depset::EK_DECL:
17071 : 754148 : case depset::EK_SPECIALIZATION:
17072 : 754148 : case depset::EK_PARTIAL:
17073 : 1508296 : gcc_checking_assert (!d->is_unreached ()
17074 : : && !d->is_import ()
17075 : : && d->cluster == current
17076 : : && d->section);
17077 : 754148 : current++;
17078 : 754148 : sec.u (d->section);
17079 : 754148 : break;
17080 : : }
17081 : : }
17082 : 2063 : gcc_assert (count == current);
17083 : 2063 : sec.end (to, to->name (MOD_SNAME_PFX ".ent"), crc_p);
17084 : 2063 : dump.outdent ();
17085 : 2063 : }
17086 : :
17087 : : bool
17088 : 2256 : module_state::read_entities (unsigned count, unsigned lwm, unsigned hwm)
17089 : : {
17090 : 2256 : trees_in sec (this);
17091 : :
17092 : 2256 : if (!sec.begin (loc, from (), MOD_SNAME_PFX ".ent"))
17093 : : return false;
17094 : :
17095 : 2669 : dump () && dump ("Reading entities");
17096 : 2256 : dump.indent ();
17097 : :
17098 : 912155 : for (binding_slot *slot = entity_ary->begin () + entity_lwm; count--; slot++)
17099 : : {
17100 : 909899 : unsigned snum = sec.u ();
17101 : 909899 : if (snum && (snum - lwm) >= (hwm - lwm))
17102 : 0 : sec.set_overrun ();
17103 : 909899 : if (sec.get_overrun ())
17104 : : break;
17105 : :
17106 : 909899 : if (snum)
17107 : 907784 : slot->set_lazy (snum << 2);
17108 : : }
17109 : :
17110 : 2256 : dump.outdent ();
17111 : 2256 : if (!sec.end (from ()))
17112 : : return false;
17113 : : return true;
17114 : 2256 : }
17115 : :
17116 : : /* Write the pending table to MOD_SNAME_PFX.pnd
17117 : :
17118 : : The pending table holds information about clusters that need to be
17119 : : loaded because they contain information about something that is not
17120 : : found by namespace-scope lookup.
17121 : :
17122 : : The three cases are:
17123 : :
17124 : : (a) Template (maybe-partial) specializations that we have
17125 : : instantiated or defined. When an importer needs to instantiate
17126 : : that template, they /must have/ the partial, explicit & extern
17127 : : specializations available. If they have the other specializations
17128 : : available, they'll have less work to do. Thus, when we're about to
17129 : : instantiate FOO, we have to be able to ask 'are there any
17130 : : specialization of FOO in our imports?'.
17131 : :
17132 : : (b) (Maybe-implicit) member functions definitions. A class could
17133 : : be defined in one header, and an inline member defined in a
17134 : : different header (this occurs in the STL). Similarly, like the
17135 : : specialization case, an implicit member function could have been
17136 : : 'instantiated' in one module, and it'd be nice to not have to
17137 : : reinstantiate it in another.
17138 : :
17139 : : (c) Classes completed elsewhere. A class could be declared in one
17140 : : header and defined in another. We need to know to load the class
17141 : : definition before looking in it. It does highlight an issue --
17142 : : there could be an intermediate import between the outermost containing
17143 : : namespace-scope class and the innermost being-defined class. This is
17144 : : actually possible with all of these cases, so be aware -- we're not
17145 : : just talking of one level of import to get to the innermost namespace.
17146 : :
17147 : : This gets complicated fast, it took me multiple attempts to even
17148 : : get something remotely working. Partially because I focussed on
17149 : : optimizing what I think turns out to be a smaller problem, given
17150 : : the known need to do the more general case *anyway*. I document
17151 : : the smaller problem, because it does appear to be the natural way
17152 : : to do it. It's trap!
17153 : :
17154 : : **** THE TRAP
17155 : :
17156 : : Let's refer to the primary template or the containing class as the
17157 : : KEY. And the specialization or member as the PENDING-ENTITY. (To
17158 : : avoid having to say those mouthfuls all the time.)
17159 : :
17160 : : In either case, we have an entity and we need some way of mapping
17161 : : that to a set of entities that need to be loaded before we can
17162 : : proceed with whatever processing of the entity we were going to do.
17163 : :
17164 : : We need to link the key to the pending-entity in some way. Given a
17165 : : key, tell me the pending-entities I need to have loaded. However
17166 : : we tie the key to the pending-entity must not rely on the key being
17167 : : loaded -- that'd defeat the lazy loading scheme.
17168 : :
17169 : : As the key will be an import in we know its entity number (either
17170 : : because we imported it, or we're writing it out too). Thus we can
17171 : : generate a map of key-indices to pending-entities. The
17172 : : pending-entity indices will be into our span of the entity table,
17173 : : and thus allow them to be lazily loaded. The key index will be
17174 : : into another slot of the entity table. Notice that this checking
17175 : : could be expensive, we don't want to iterate over a bunch of
17176 : : pending-entity indices (across multiple imports), every time we're
17177 : : about do to the thing with the key. We need to quickly determine
17178 : : 'definitely nothing needed'.
17179 : :
17180 : : That's almost good enough, except that key indices are not unique
17181 : : in a couple of cases :( Specifically the Global Module or a module
17182 : : partition can result in multiple modules assigning an entity index
17183 : : for the key. The decl-merging on loading will detect that so we
17184 : : only have one Key loaded, and in the entity hash it'll indicate the
17185 : : entity index of first load. Which might be different to how we
17186 : : know it. Notice this is restricted to GM entities or this-module
17187 : : entities. Foreign imports cannot have this.
17188 : :
17189 : : We can simply resolve this in the direction of how this module
17190 : : referred to the key to how the importer knows it. Look in the
17191 : : entity table slot that we nominate, maybe lazy load it, and then
17192 : : lookup the resultant entity in the entity hash to learn how the
17193 : : importer knows it.
17194 : :
17195 : : But we need to go in the other direction :( Given the key, find all
17196 : : the index-aliases of that key. We can partially solve that by
17197 : : adding an alias hash table. Whenever we load a merged decl, add or
17198 : : augment a mapping from the entity (or its entity-index) to the
17199 : : newly-discovered index. Then when we look for pending entities of
17200 : : a key, we also iterate over this aliases this mapping provides.
17201 : :
17202 : : But that requires the alias to be loaded. And that's not
17203 : : necessarily true.
17204 : :
17205 : : *** THE SIMPLER WAY
17206 : :
17207 : : The remaining fixed thing we have is the innermost namespace
17208 : : containing the ultimate namespace-scope container of the key and
17209 : : the name of that container (which might be the key itself). I.e. a
17210 : : namespace-decl/identifier/module tuple. Let's call this the
17211 : : top-key. We'll discover that the module is not important here,
17212 : : because of cross-module possibilities mentioned in case #c above.
17213 : : We can't markup namespace-binding slots. The best we can do is
17214 : : mark the binding vector with 'there's something here', and have
17215 : : another map from namespace/identifier pairs to a vector of pending
17216 : : entity indices.
17217 : :
17218 : : Maintain a pending-entity map. This is keyed by top-key, and
17219 : : maps to a vector of pending-entity indices. On the binding vector
17220 : : have flags saying whether the pending-name-entity map has contents.
17221 : : (We might want to further extend the key to be GM-vs-Partition and
17222 : : specialization-vs-member, but let's not get ahead of ourselves.)
17223 : :
17224 : : For every key-like entity, find the outermost namespace-scope
17225 : : name. Use that to lookup in the pending-entity map and then make
17226 : : sure the specified entities are loaded.
17227 : :
17228 : : An optimization might be to have a flag in each key-entity saying
17229 : : that its top key might be in the entity table. It's not clear to
17230 : : me how to set that flag cheaply -- cheaper than just looking.
17231 : :
17232 : : FIXME: It'd be nice to have a bit in decls to tell us whether to
17233 : : even try this. We can have a 'already done' flag, that we set when
17234 : : we've done KLASS's lazy pendings. When we import a module that
17235 : : registers pendings on the same top-key as KLASS we need to clear
17236 : : the flag. A recursive walk of the top-key clearing the bit will
17237 : : suffice. Plus we only need to recurse on classes that have the bit
17238 : : set. (That means we need to set the bit on parents of KLASS here,
17239 : : don't forget.) However, first: correctness, second: efficiency. */
17240 : :
17241 : : unsigned
17242 : 2302 : module_state::write_pendings (elf_out *to, vec<depset *> depsets,
17243 : : depset::hash &table, unsigned *crc_p)
17244 : : {
17245 : 2550 : dump () && dump ("Writing pending-entities");
17246 : 2302 : dump.indent ();
17247 : :
17248 : 2302 : trees_out sec (to, this, table);
17249 : 2302 : sec.begin ();
17250 : :
17251 : 2302 : unsigned count = 0;
17252 : 2302 : tree cache_ns = NULL_TREE;
17253 : 2302 : tree cache_id = NULL_TREE;
17254 : 2302 : unsigned cache_section = ~0;
17255 : 894318 : for (unsigned ix = 0; ix < depsets.length (); ix++)
17256 : : {
17257 : 892016 : depset *d = depsets[ix];
17258 : :
17259 : 892016 : if (d->is_binding ())
17260 : 545530 : continue;
17261 : :
17262 : 773585 : if (d->is_import ())
17263 : 0 : continue;
17264 : :
17265 : 773585 : if (!d->is_pending_entity ())
17266 : 427099 : continue;
17267 : :
17268 : 346486 : tree key_decl = nullptr;
17269 : 346486 : tree key_ns = find_pending_key (d->get_entity (), &key_decl);
17270 : 346486 : tree key_name = DECL_NAME (key_decl);
17271 : :
17272 : 346486 : if (IDENTIFIER_ANON_P (key_name))
17273 : : {
17274 : 6 : gcc_checking_assert (IDENTIFIER_LAMBDA_P (key_name));
17275 : 6 : if (tree attached = LAMBDA_TYPE_EXTRA_SCOPE (TREE_TYPE (key_decl)))
17276 : 6 : key_name = DECL_NAME (attached);
17277 : : else
17278 : : {
17279 : : /* There's nothing to attach it to. Must
17280 : : always reinstantiate. */
17281 : 0 : dump ()
17282 : 0 : && dump ("Unattached lambda %N[%u] section:%u",
17283 : 0 : d->get_entity_kind () == depset::EK_DECL
17284 : : ? "Member" : "Specialization", d->get_entity (),
17285 : : d->cluster, d->section);
17286 : 0 : continue;
17287 : : }
17288 : : }
17289 : :
17290 : 346486 : char const *also = "";
17291 : 346486 : if (d->section == cache_section
17292 : 218227 : && key_ns == cache_ns
17293 : 218227 : && key_name == cache_id)
17294 : : /* Same section & key as previous, no need to repeat ourselves. */
17295 : : also = "also ";
17296 : : else
17297 : : {
17298 : 165232 : cache_ns = key_ns;
17299 : 165232 : cache_id = key_name;
17300 : 165232 : cache_section = d->section;
17301 : 165232 : gcc_checking_assert (table.find_dependency (cache_ns));
17302 : 165232 : sec.tree_node (cache_ns);
17303 : 165232 : sec.tree_node (cache_id);
17304 : 165232 : sec.u (d->cluster);
17305 : 165232 : count++;
17306 : : }
17307 : 347548 : dump () && dump ("Pending %s %N entity:%u section:%u %skeyed to %P",
17308 : 531 : d->get_entity_kind () == depset::EK_DECL
17309 : : ? "member" : "specialization", d->get_entity (),
17310 : : d->cluster, cache_section, also, cache_ns, cache_id);
17311 : : }
17312 : 2302 : sec.end (to, to->name (MOD_SNAME_PFX ".pnd"), crc_p);
17313 : 2302 : dump.outdent ();
17314 : :
17315 : 2302 : return count;
17316 : 2302 : }
17317 : :
17318 : : bool
17319 : 1287 : module_state::read_pendings (unsigned count)
17320 : : {
17321 : 1287 : trees_in sec (this);
17322 : :
17323 : 1287 : if (!sec.begin (loc, from (), MOD_SNAME_PFX ".pnd"))
17324 : : return false;
17325 : :
17326 : 1568 : dump () && dump ("Reading %u pendings", count);
17327 : 1287 : dump.indent ();
17328 : :
17329 : 197833 : for (unsigned ix = 0; ix != count; ix++)
17330 : : {
17331 : 196546 : pending_key key;
17332 : 196546 : unsigned index;
17333 : :
17334 : 196546 : key.ns = sec.tree_node ();
17335 : 196546 : key.id = sec.tree_node ();
17336 : 196546 : index = sec.u ();
17337 : :
17338 : 196546 : if (!key.ns || !key.id
17339 : 196546 : || !(TREE_CODE (key.ns) == NAMESPACE_DECL
17340 : 196546 : && !DECL_NAMESPACE_ALIAS (key.ns))
17341 : 196546 : || !identifier_p (key.id)
17342 : 393092 : || index >= entity_num)
17343 : 0 : sec.set_overrun ();
17344 : :
17345 : 196546 : if (sec.get_overrun ())
17346 : : break;
17347 : :
17348 : 197224 : dump () && dump ("Pending:%u keyed to %P", index, key.ns, key.id);
17349 : :
17350 : 196546 : index += entity_lwm;
17351 : 196546 : auto &vec = pending_table->get_or_insert (key);
17352 : 196546 : vec.safe_push (index);
17353 : : }
17354 : :
17355 : 1287 : dump.outdent ();
17356 : 1287 : if (!sec.end (from ()))
17357 : : return false;
17358 : : return true;
17359 : 1287 : }
17360 : :
17361 : : /* Read & write locations. */
17362 : : enum loc_kind {
17363 : : LK_ORDINARY,
17364 : : LK_MACRO,
17365 : : LK_IMPORT_ORDINARY,
17366 : : LK_IMPORT_MACRO,
17367 : : LK_ADHOC,
17368 : : LK_RESERVED,
17369 : : };
17370 : :
17371 : : static const module_state *
17372 : 3068 : module_for_ordinary_loc (location_t loc)
17373 : : {
17374 : 3068 : unsigned pos = 0;
17375 : 6136 : unsigned len = ool->length () - pos;
17376 : :
17377 : 3071 : while (len)
17378 : : {
17379 : 3071 : unsigned half = len / 2;
17380 : 3071 : module_state *probe = (*ool)[pos + half];
17381 : 3071 : if (loc < probe->ordinary_locs.first)
17382 : : len = half;
17383 : 3068 : else if (loc < probe->ordinary_locs.first + probe->ordinary_locs.second)
17384 : : return probe;
17385 : : else
17386 : : {
17387 : 0 : pos += half + 1;
17388 : 0 : len = len - (half + 1);
17389 : : }
17390 : : }
17391 : :
17392 : : return nullptr;
17393 : : }
17394 : :
17395 : : static const module_state *
17396 : 6 : module_for_macro_loc (location_t loc)
17397 : : {
17398 : 6 : unsigned pos = 1;
17399 : 6 : unsigned len = modules->length () - pos;
17400 : :
17401 : 6 : while (len)
17402 : : {
17403 : 6 : unsigned half = len / 2;
17404 : 6 : module_state *probe = (*modules)[pos + half];
17405 : 6 : if (loc < probe->macro_locs.first)
17406 : : {
17407 : 0 : pos += half + 1;
17408 : 0 : len = len - (half + 1);
17409 : : }
17410 : 6 : else if (loc >= probe->macro_locs.first + probe->macro_locs.second)
17411 : : len = half;
17412 : : else
17413 : : return probe;
17414 : : }
17415 : :
17416 : : return NULL;
17417 : : }
17418 : :
17419 : : location_t
17420 : 1043 : module_state::imported_from () const
17421 : : {
17422 : 1043 : location_t from = loc;
17423 : 1043 : line_map_ordinary const *fmap
17424 : 1043 : = linemap_check_ordinary (linemap_lookup (line_table, from));
17425 : :
17426 : 1043 : if (MAP_MODULE_P (fmap))
17427 : 1043 : from = linemap_included_from (fmap);
17428 : :
17429 : 1043 : return from;
17430 : : }
17431 : :
17432 : : /* Note that LOC will need writing. This allows us to prune locations
17433 : : that are not needed. */
17434 : :
17435 : : bool
17436 : 15782813 : module_state::note_location (location_t loc)
17437 : : {
17438 : 15782813 : bool added = false;
17439 : 15782813 : if (!macro_loc_table && !ord_loc_table)
17440 : : ;
17441 : 15782813 : else if (loc < RESERVED_LOCATION_COUNT)
17442 : : ;
17443 : 14385649 : else if (IS_ADHOC_LOC (loc))
17444 : : {
17445 : 1759417 : location_t locus = get_location_from_adhoc_loc (line_table, loc);
17446 : 1759417 : note_location (locus);
17447 : 1759417 : source_range range = get_range_from_loc (line_table, loc);
17448 : 1759417 : if (range.m_start != locus)
17449 : 1702621 : note_location (range.m_start);
17450 : 1759417 : note_location (range.m_finish);
17451 : : }
17452 : 12626232 : else if (loc >= LINEMAPS_MACRO_LOWEST_LOCATION (line_table))
17453 : : {
17454 : 932400 : if (spans.macro (loc))
17455 : : {
17456 : 932394 : const line_map *map = linemap_lookup (line_table, loc);
17457 : 932394 : const line_map_macro *mac_map = linemap_check_macro (map);
17458 : 932394 : hashval_t hv = macro_loc_traits::hash (mac_map);
17459 : 932394 : macro_loc_info *slot
17460 : 932394 : = macro_loc_table->find_slot_with_hash (mac_map, hv, INSERT);
17461 : 932394 : if (!slot->src)
17462 : : {
17463 : 111647 : slot->src = mac_map;
17464 : 111647 : slot->remap = 0;
17465 : : // Expansion locations could themselves be from a
17466 : : // macro, we need to note them all.
17467 : 111647 : note_location (mac_map->m_expansion);
17468 : 111647 : gcc_checking_assert (mac_map->n_tokens);
17469 : 111647 : location_t tloc = UNKNOWN_LOCATION;
17470 : 3895409 : for (unsigned ix = mac_map->n_tokens * 2; ix--;)
17471 : 3783762 : if (mac_map->macro_locations[ix] != tloc)
17472 : : {
17473 : 1991779 : tloc = mac_map->macro_locations[ix];
17474 : 1991779 : note_location (tloc);
17475 : : }
17476 : : added = true;
17477 : : }
17478 : : }
17479 : : }
17480 : 11693832 : else if (IS_ORDINARY_LOC (loc))
17481 : : {
17482 : 11693832 : if (spans.ordinary (loc))
17483 : : {
17484 : 11690761 : const line_map *map = linemap_lookup (line_table, loc);
17485 : 11690761 : const line_map_ordinary *ord_map = linemap_check_ordinary (map);
17486 : 11690761 : ord_loc_info lkup;
17487 : 11690761 : lkup.src = ord_map;
17488 : 11690761 : lkup.span = loc_one << ord_map->m_column_and_range_bits;
17489 : 11690761 : lkup.offset = (loc - MAP_START_LOCATION (ord_map)) & ~(lkup.span - 1);
17490 : 11690761 : lkup.remap = 0;
17491 : 11690761 : ord_loc_info *slot = (ord_loc_table->find_slot_with_hash
17492 : 11690761 : (lkup, ord_loc_traits::hash (lkup), INSERT));
17493 : 11690761 : if (!slot->src)
17494 : : {
17495 : 1421973 : *slot = lkup;
17496 : 1421973 : added = true;
17497 : : }
17498 : : }
17499 : : }
17500 : : else
17501 : 0 : gcc_unreachable ();
17502 : 15782813 : return added;
17503 : : }
17504 : :
17505 : : /* If we're not streaming, record that we need location LOC.
17506 : : Otherwise stream it. */
17507 : :
17508 : : void
17509 : 23452253 : module_state::write_location (bytes_out &sec, location_t loc)
17510 : : {
17511 : 23452253 : if (!sec.streaming_p ())
17512 : : {
17513 : 8208448 : note_location (loc);
17514 : 8208448 : return;
17515 : : }
17516 : :
17517 : 15243805 : if (loc < RESERVED_LOCATION_COUNT)
17518 : : {
17519 : 1434525 : dump (dumper::LOCATION) && dump ("Reserved location %K", loc);
17520 : 1434507 : sec.loc (LK_RESERVED + loc);
17521 : : }
17522 : 13809298 : else if (IS_ADHOC_LOC (loc))
17523 : : {
17524 : 1721195 : dump (dumper::LOCATION) && dump ("Adhoc location");
17525 : 1721192 : sec.u (LK_ADHOC);
17526 : 1721192 : location_t locus = get_location_from_adhoc_loc (line_table, loc);
17527 : 1721192 : write_location (sec, locus);
17528 : 1721192 : source_range range = get_range_from_loc (line_table, loc);
17529 : 1721192 : if (range.m_start == locus)
17530 : : /* Compress. */
17531 : 53610 : range.m_start = UNKNOWN_LOCATION;
17532 : 1721192 : write_location (sec, range.m_start);
17533 : 1721192 : write_location (sec, range.m_finish);
17534 : 1721192 : unsigned discriminator = get_discriminator_from_adhoc_loc (line_table, loc);
17535 : 1721192 : sec.u (discriminator);
17536 : : }
17537 : 12088106 : else if (loc >= LINEMAPS_MACRO_LOWEST_LOCATION (line_table))
17538 : : {
17539 : 920442 : const macro_loc_info *info = nullptr;
17540 : 920442 : line_map_uint_t offset = 0;
17541 : 920442 : if (unsigned hwm = macro_loc_remap->length ())
17542 : : {
17543 : 920436 : info = macro_loc_remap->begin ();
17544 : 13583370 : while (hwm != 1)
17545 : : {
17546 : 11742498 : unsigned mid = hwm / 2;
17547 : 11742498 : if (MAP_START_LOCATION (info[mid].src) <= loc)
17548 : : {
17549 : 5989977 : info += mid;
17550 : 5989977 : hwm -= mid;
17551 : : }
17552 : : else
17553 : : hwm = mid;
17554 : : }
17555 : 920436 : offset = loc - MAP_START_LOCATION (info->src);
17556 : 920436 : if (offset > info->src->n_tokens)
17557 : 0 : info = nullptr;
17558 : : }
17559 : :
17560 : 920442 : gcc_checking_assert (bool (info) == bool (spans.macro (loc)));
17561 : :
17562 : 920442 : if (info)
17563 : : {
17564 : 920436 : offset += info->remap;
17565 : 920436 : sec.u (LK_MACRO);
17566 : 920436 : sec.loc (offset);
17567 : 920436 : dump (dumper::LOCATION)
17568 : 9 : && dump ("Macro location %K output %K", loc, offset);
17569 : : }
17570 : 6 : else if (const module_state *import = module_for_macro_loc (loc))
17571 : : {
17572 : 6 : auto off = loc - import->macro_locs.first;
17573 : 6 : sec.u (LK_IMPORT_MACRO);
17574 : 6 : sec.u (import->remap);
17575 : 6 : sec.loc (off);
17576 : 6 : dump (dumper::LOCATION)
17577 : 0 : && dump ("Imported macro location %K output %u:%K",
17578 : 0 : loc, import->remap, off);
17579 : : }
17580 : : else
17581 : 0 : gcc_unreachable ();
17582 : : }
17583 : 11167664 : else if (IS_ORDINARY_LOC (loc))
17584 : : {
17585 : : /* If we ran out of locations for imported decls, this location could
17586 : : be a module unit's location. In that case, remap the location
17587 : : to be where we imported the module from. */
17588 : 11167664 : if (spans.locations_exhausted_p () || CHECKING_P)
17589 : : {
17590 : 11167664 : const line_map_ordinary *map
17591 : 11167664 : = linemap_check_ordinary (linemap_lookup (line_table, loc));
17592 : 11167664 : if (MAP_MODULE_P (map) && loc == MAP_START_LOCATION (map))
17593 : : {
17594 : 0 : gcc_checking_assert (spans.locations_exhausted_p ());
17595 : 0 : write_location (sec, linemap_included_from (map));
17596 : 0 : return;
17597 : : }
17598 : : }
17599 : :
17600 : 11167664 : const ord_loc_info *info = nullptr;
17601 : 11167664 : line_map_uint_t offset = 0;
17602 : 11167664 : if (line_map_uint_t hwm = ord_loc_remap->length ())
17603 : : {
17604 : 11167664 : info = ord_loc_remap->begin ();
17605 : 161763654 : while (hwm != 1)
17606 : : {
17607 : 139428326 : auto mid = hwm / 2;
17608 : 139428326 : if (MAP_START_LOCATION (info[mid].src) + info[mid].offset <= loc)
17609 : : {
17610 : 73953067 : info += mid;
17611 : 73953067 : hwm -= mid;
17612 : : }
17613 : : else
17614 : : hwm = mid;
17615 : : }
17616 : 11167664 : offset = loc - MAP_START_LOCATION (info->src) - info->offset;
17617 : 11167664 : if (offset > info->span)
17618 : 3068 : info = nullptr;
17619 : : }
17620 : :
17621 : 11167664 : gcc_checking_assert (bool (info) == bool (spans.ordinary (loc)));
17622 : :
17623 : 11167664 : if (info)
17624 : : {
17625 : 11164596 : offset += info->remap;
17626 : 11164596 : sec.u (LK_ORDINARY);
17627 : 11164596 : sec.loc (offset);
17628 : :
17629 : 11164596 : dump (dumper::LOCATION)
17630 : 78 : && dump ("Ordinary location %K output %K", loc, offset);
17631 : : }
17632 : 3068 : else if (const module_state *import = module_for_ordinary_loc (loc))
17633 : : {
17634 : 3068 : auto off = loc - import->ordinary_locs.first;
17635 : 3068 : sec.u (LK_IMPORT_ORDINARY);
17636 : 3068 : sec.u (import->remap);
17637 : 3068 : sec.loc (off);
17638 : 3068 : dump (dumper::LOCATION)
17639 : 0 : && dump ("Imported ordinary location %K output %u:%K",
17640 : 0 : loc, import->remap, off);
17641 : : }
17642 : : else
17643 : 0 : gcc_unreachable ();
17644 : : }
17645 : : else
17646 : 0 : gcc_unreachable ();
17647 : : }
17648 : :
17649 : : location_t
17650 : 13777743 : module_state::read_location (bytes_in &sec) const
17651 : : {
17652 : 13777743 : location_t locus = UNKNOWN_LOCATION;
17653 : 13777743 : unsigned kind = sec.u ();
17654 : 13777743 : switch (kind)
17655 : : {
17656 : 1266439 : default:
17657 : 1266439 : {
17658 : 1266439 : if (kind < LK_RESERVED + RESERVED_LOCATION_COUNT)
17659 : 1266439 : locus = location_t (kind - LK_RESERVED);
17660 : : else
17661 : 0 : sec.set_overrun ();
17662 : 1266439 : dump (dumper::LOCATION)
17663 : 0 : && dump ("Reserved location %K", locus);
17664 : : }
17665 : : break;
17666 : :
17667 : 1491960 : case LK_ADHOC:
17668 : 1491960 : {
17669 : 1491960 : dump (dumper::LOCATION) && dump ("Adhoc location");
17670 : 1491960 : locus = read_location (sec);
17671 : 1491960 : source_range range;
17672 : 1491960 : range.m_start = read_location (sec);
17673 : 1491960 : if (range.m_start == UNKNOWN_LOCATION)
17674 : 43303 : range.m_start = locus;
17675 : 1491960 : range.m_finish = read_location (sec);
17676 : 1491960 : unsigned discriminator = sec.u ();
17677 : 1491960 : if (locus != loc && range.m_start != loc && range.m_finish != loc)
17678 : 1491960 : locus = line_table->get_or_create_combined_loc (locus, range,
17679 : : nullptr, discriminator);
17680 : : }
17681 : : break;
17682 : :
17683 : 1001471 : case LK_MACRO:
17684 : 1001471 : {
17685 : 1001471 : auto off = sec.loc ();
17686 : :
17687 : 1001471 : if (macro_locs.second)
17688 : : {
17689 : 1001471 : if (off < macro_locs.second)
17690 : 1001471 : locus = off + macro_locs.first;
17691 : : else
17692 : 0 : sec.set_overrun ();
17693 : : }
17694 : : else
17695 : 0 : locus = loc;
17696 : 1001471 : dump (dumper::LOCATION)
17697 : 0 : && dump ("Macro %K becoming %K", off, locus);
17698 : : }
17699 : : break;
17700 : :
17701 : 10016850 : case LK_ORDINARY:
17702 : 10016850 : {
17703 : 10016850 : auto off = sec.loc ();
17704 : 10016850 : if (ordinary_locs.second)
17705 : : {
17706 : 10016850 : if (off < ordinary_locs.second)
17707 : 10016850 : locus = off + ordinary_locs.first;
17708 : : else
17709 : 0 : sec.set_overrun ();
17710 : : }
17711 : : else
17712 : 0 : locus = loc;
17713 : :
17714 : 10016850 : dump (dumper::LOCATION)
17715 : 0 : && dump ("Ordinary location %K becoming %K", off, locus);
17716 : : }
17717 : : break;
17718 : :
17719 : 1023 : case LK_IMPORT_MACRO:
17720 : 1023 : case LK_IMPORT_ORDINARY:
17721 : 1023 : {
17722 : 1023 : unsigned mod = sec.u ();
17723 : 1023 : location_t off = sec.loc ();
17724 : 1023 : const module_state *import = NULL;
17725 : :
17726 : 1023 : if (!mod && !slurp->remap)
17727 : : /* This is an early read of a partition location during the
17728 : : read of our ordinary location map. */
17729 : : import = this;
17730 : : else
17731 : : {
17732 : 1023 : mod = slurp->remap_module (mod);
17733 : 1023 : if (!mod)
17734 : 0 : sec.set_overrun ();
17735 : : else
17736 : 1023 : import = (*modules)[mod];
17737 : : }
17738 : :
17739 : 1023 : if (import)
17740 : : {
17741 : 1023 : if (kind == LK_IMPORT_MACRO)
17742 : : {
17743 : 6 : if (!import->macro_locs.second)
17744 : 0 : locus = import->loc;
17745 : 6 : else if (off < import->macro_locs.second)
17746 : 6 : locus = off + import->macro_locs.first;
17747 : : else
17748 : 0 : sec.set_overrun ();
17749 : : }
17750 : : else
17751 : : {
17752 : 1017 : if (!import->ordinary_locs.second)
17753 : 0 : locus = import->loc;
17754 : 1017 : else if (off < import->ordinary_locs.second)
17755 : 1017 : locus = import->ordinary_locs.first + off;
17756 : : else
17757 : 0 : sec.set_overrun ();
17758 : : }
17759 : : }
17760 : : }
17761 : : break;
17762 : : }
17763 : :
17764 : 13777743 : return locus;
17765 : : }
17766 : :
17767 : : /* Allocate hash tables to record needed locations. */
17768 : :
17769 : : void
17770 : 2327 : module_state::write_init_maps ()
17771 : : {
17772 : 2327 : macro_loc_table = new hash_table<macro_loc_traits> (EXPERIMENT (1, 400));
17773 : 2327 : ord_loc_table = new hash_table<ord_loc_traits> (EXPERIMENT (1, 400));
17774 : 2327 : }
17775 : :
17776 : : /* Prepare the span adjustments. We prune unneeded locations -- at
17777 : : this point every needed location must have been seen by
17778 : : note_location. */
17779 : :
17780 : : range_t
17781 : 2302 : module_state::write_prepare_maps (module_state_config *cfg, bool has_partitions)
17782 : : {
17783 : 2550 : dump () && dump ("Preparing locations");
17784 : 2302 : dump.indent ();
17785 : :
17786 : 2550 : dump () && dump ("Reserved locations [%K,%K) macro [%K,%K)",
17787 : 248 : spans[loc_spans::SPAN_RESERVED].ordinary.first,
17788 : 248 : spans[loc_spans::SPAN_RESERVED].ordinary.second,
17789 : 248 : spans[loc_spans::SPAN_RESERVED].macro.first,
17790 : 248 : spans[loc_spans::SPAN_RESERVED].macro.second);
17791 : :
17792 : 2302 : range_t info {0, 0};
17793 : :
17794 : : // Sort the noted lines.
17795 : 2302 : vec_alloc (ord_loc_remap, ord_loc_table->size ());
17796 : 2302 : for (auto iter = ord_loc_table->begin (), end = ord_loc_table->end ();
17797 : 2826368 : iter != end; ++iter)
17798 : 1412033 : ord_loc_remap->quick_push (*iter);
17799 : 2302 : ord_loc_remap->qsort (&ord_loc_info::compare);
17800 : :
17801 : : // Note included-from maps.
17802 : 2302 : bool added = false;
17803 : 2302 : const line_map_ordinary *current = nullptr;
17804 : 1418939 : for (auto iter = ord_loc_remap->begin (), end = ord_loc_remap->end ();
17805 : 1414335 : iter != end; ++iter)
17806 : 1412033 : if (iter->src != current)
17807 : : {
17808 : 24677 : current = iter->src;
17809 : 19104 : for (auto probe = current;
17810 : 24677 : auto from = linemap_included_from (probe);
17811 : 9552 : probe = linemap_check_ordinary (linemap_lookup (line_table, from)))
17812 : : {
17813 : 22133 : if (has_partitions)
17814 : : {
17815 : : // Partition locations need to elide their module map
17816 : : // entry.
17817 : 149 : probe
17818 : 149 : = linemap_check_ordinary (linemap_lookup (line_table, from));
17819 : 149 : if (MAP_MODULE_P (probe))
17820 : 128 : from = linemap_included_from (probe);
17821 : : }
17822 : :
17823 : 22133 : if (!note_location (from))
17824 : : break;
17825 : 9552 : added = true;
17826 : 9552 : }
17827 : : }
17828 : 2302 : if (added)
17829 : : {
17830 : : // Reconstruct the line array as we added items to the hash table.
17831 : 417 : vec_free (ord_loc_remap);
17832 : 417 : vec_alloc (ord_loc_remap, ord_loc_table->size ());
17833 : 417 : for (auto iter = ord_loc_table->begin (), end = ord_loc_table->end ();
17834 : 2825235 : iter != end; ++iter)
17835 : 1412409 : ord_loc_remap->quick_push (*iter);
17836 : 417 : ord_loc_remap->qsort (&ord_loc_info::compare);
17837 : : }
17838 : 2302 : delete ord_loc_table;
17839 : 2302 : ord_loc_table = nullptr;
17840 : :
17841 : : // Merge (sufficiently) adjacent spans, and calculate remapping.
17842 : 2302 : constexpr line_map_uint_t adjacency = 2; // Allow 2 missing lines.
17843 : 4604 : auto begin = ord_loc_remap->begin (), end = ord_loc_remap->end ();
17844 : 2302 : auto dst = begin;
17845 : 2302 : line_map_uint_t offset = 0;
17846 : 2302 : unsigned range_bits = 0;
17847 : 2302 : ord_loc_info *base = nullptr;
17848 : 1423887 : for (auto iter = begin; iter != end; ++iter)
17849 : : {
17850 : 1421585 : if (base && iter->src == base->src)
17851 : : {
17852 : 2642543 : if (base->offset + base->span +
17853 : 1400866 : ((adjacency << base->src->m_column_and_range_bits)
17854 : : // If there are few c&r bits, allow further separation.
17855 : 1400866 : | (adjacency << 4))
17856 : 1400866 : >= iter->offset)
17857 : : {
17858 : : // Merge.
17859 : 1241677 : offset -= base->span;
17860 : 1241677 : base->span = iter->offset + iter->span - base->offset;
17861 : 1241677 : offset += base->span;
17862 : 1241677 : continue;
17863 : : }
17864 : : }
17865 : 20719 : else if (range_bits < iter->src->m_range_bits)
17866 : 2202 : range_bits = iter->src->m_range_bits;
17867 : :
17868 : 179908 : offset += ((loc_one << iter->src->m_range_bits) - 1);
17869 : 179908 : offset &= ~((loc_one << iter->src->m_range_bits) - 1);
17870 : 179908 : iter->remap = offset;
17871 : 179908 : offset += iter->span;
17872 : 179908 : base = dst;
17873 : 179908 : *dst++ = *iter;
17874 : : }
17875 : 2302 : ord_loc_remap->truncate (dst - begin);
17876 : :
17877 : 2302 : info.first = ord_loc_remap->length ();
17878 : 2302 : cfg->ordinary_locs = offset;
17879 : 2302 : cfg->loc_range_bits = range_bits;
17880 : 2550 : dump () && dump ("Ordinary maps:%K locs:%K range_bits:%u",
17881 : : info.first,
17882 : : cfg->ordinary_locs,
17883 : : cfg->loc_range_bits);
17884 : :
17885 : : // Remap the macro locations.
17886 : 2302 : vec_alloc (macro_loc_remap, macro_loc_table->size ());
17887 : 2302 : for (auto iter = macro_loc_table->begin (), end = macro_loc_table->end ();
17888 : 225596 : iter != end; ++iter)
17889 : 111647 : macro_loc_remap->quick_push (*iter);
17890 : 2302 : delete macro_loc_table;
17891 : 2302 : macro_loc_table = nullptr;
17892 : :
17893 : 2302 : macro_loc_remap->qsort (¯o_loc_info::compare);
17894 : 2302 : offset = 0;
17895 : 6906 : for (auto iter = macro_loc_remap->begin (), end = macro_loc_remap->end ();
17896 : 113949 : iter != end; ++iter)
17897 : : {
17898 : 111647 : auto mac = iter->src;
17899 : 111647 : iter->remap = offset;
17900 : 111647 : offset += mac->n_tokens;
17901 : : }
17902 : 2302 : info.second = macro_loc_remap->length ();
17903 : 2302 : cfg->macro_locs = offset;
17904 : :
17905 : 2550 : dump () && dump ("Macro maps:%K locs:%K", info.second, cfg->macro_locs);
17906 : :
17907 : 2302 : dump.outdent ();
17908 : :
17909 : : // If we have no ordinary locs, we must also have no macro locs.
17910 : 2302 : gcc_checking_assert (cfg->ordinary_locs || !cfg->macro_locs);
17911 : :
17912 : 2302 : return info;
17913 : : }
17914 : :
17915 : : bool
17916 : 2509 : module_state::read_prepare_maps (const module_state_config *cfg)
17917 : : {
17918 : 2509 : location_t ordinary = line_table->highest_location + 1;
17919 : 2509 : ordinary += cfg->ordinary_locs;
17920 : :
17921 : 2509 : location_t macro = LINEMAPS_MACRO_LOWEST_LOCATION (line_table);
17922 : 2509 : macro -= cfg->macro_locs;
17923 : :
17924 : 2509 : if (ordinary < LINE_MAP_MAX_LOCATION_WITH_COLS
17925 : 2509 : && macro >= LINE_MAP_MAX_LOCATION)
17926 : : /* OK, we have enough locations. */
17927 : : return true;
17928 : :
17929 : 0 : ordinary_locs.first = ordinary_locs.second = 0;
17930 : 0 : macro_locs.first = macro_locs.second = 0;
17931 : :
17932 : 0 : spans.report_location_exhaustion (loc);
17933 : :
17934 : : return false;
17935 : : }
17936 : :
17937 : : /* Write & read the location maps. Not called if there are no
17938 : : locations. */
17939 : :
17940 : : void
17941 : 2202 : module_state::write_ordinary_maps (elf_out *to, range_t &info,
17942 : : bool has_partitions, unsigned *crc_p)
17943 : : {
17944 : 2430 : dump () && dump ("Writing ordinary location maps");
17945 : 2202 : dump.indent ();
17946 : :
17947 : 2202 : vec<const char *> filenames;
17948 : 2202 : filenames.create (20);
17949 : :
17950 : : /* Determine the unique filenames. */
17951 : 2202 : const line_map_ordinary *current = nullptr;
17952 : 186514 : for (auto iter = ord_loc_remap->begin (), end = ord_loc_remap->end ();
17953 : 182110 : iter != end; ++iter)
17954 : 179908 : if (iter->src != current)
17955 : : {
17956 : 20719 : current = iter->src;
17957 : 20719 : const char *fname = ORDINARY_MAP_FILE_NAME (iter->src);
17958 : :
17959 : : /* We should never find a module linemap in an interval. */
17960 : 20719 : gcc_checking_assert (!MAP_MODULE_P (iter->src));
17961 : :
17962 : : /* We expect very few filenames, so just an array.
17963 : : (Not true when headers are still in play :() */
17964 : 1298189 : for (unsigned jx = filenames.length (); jx--;)
17965 : : {
17966 : 1266357 : const char *name = filenames[jx];
17967 : 1266357 : if (0 == strcmp (name, fname))
17968 : : {
17969 : : /* Reset the linemap's name, because for things like
17970 : : preprocessed input we could have multiple instances
17971 : : of the same name, and we'd rather not percolate
17972 : : that. */
17973 : 9606 : const_cast<line_map_ordinary *> (iter->src)->to_file = name;
17974 : 9606 : fname = NULL;
17975 : 9606 : break;
17976 : : }
17977 : : }
17978 : 20719 : if (fname)
17979 : 11113 : filenames.safe_push (fname);
17980 : : }
17981 : :
17982 : 2202 : bytes_out sec (to);
17983 : 2202 : sec.begin ();
17984 : :
17985 : : /* Write the filenames. */
17986 : 2202 : unsigned len = filenames.length ();
17987 : 2202 : sec.u (len);
17988 : 2430 : dump () && dump ("%u source file names", len);
17989 : 13315 : for (unsigned ix = 0; ix != len; ix++)
17990 : : {
17991 : 11113 : const char *fname = filenames[ix];
17992 : 11128 : dump (dumper::LOCATION) && dump ("Source file[%u]=%s", ix, fname);
17993 : 11113 : sec.str (fname);
17994 : : }
17995 : :
17996 : 2202 : sec.loc (info.first); /* Num maps. */
17997 : 2202 : const ord_loc_info *base = nullptr;
17998 : 186514 : for (auto iter = ord_loc_remap->begin (), end = ord_loc_remap->end ();
17999 : 182110 : iter != end; ++iter)
18000 : : {
18001 : 179908 : dump (dumper::LOCATION)
18002 : 36 : && dump ("Span:%K ordinary [%K+%K,+%K)->[%K,+%K)",
18003 : 36 : (location_t) (iter - ord_loc_remap->begin ()),
18004 : 18 : MAP_START_LOCATION (iter->src),
18005 : : iter->offset, iter->span, iter->remap,
18006 : : iter->span);
18007 : :
18008 : 179908 : if (!base || iter->src != base->src)
18009 : 20719 : base = iter;
18010 : 179908 : sec.loc (iter->offset - base->offset);
18011 : 179908 : if (base == iter)
18012 : : {
18013 : 20719 : sec.u (iter->src->sysp);
18014 : 20719 : sec.u (iter->src->m_range_bits);
18015 : 20719 : sec.u (iter->src->m_column_and_range_bits - iter->src->m_range_bits);
18016 : :
18017 : 20719 : const char *fname = ORDINARY_MAP_FILE_NAME (iter->src);
18018 : 4021534 : for (unsigned ix = 0; ix != filenames.length (); ix++)
18019 : 2010767 : if (filenames[ix] == fname)
18020 : : {
18021 : 20719 : sec.u (ix);
18022 : 20719 : break;
18023 : : }
18024 : 20719 : unsigned line = ORDINARY_MAP_STARTING_LINE_NUMBER (iter->src);
18025 : 20719 : line += iter->offset >> iter->src->m_column_and_range_bits;
18026 : 20719 : sec.u (line);
18027 : : }
18028 : 179908 : sec.loc (iter->remap);
18029 : 179908 : if (base == iter)
18030 : : {
18031 : : /* Write the included from location, which means reading it
18032 : : while reading in the ordinary maps. So we'd better not
18033 : : be getting ahead of ourselves. */
18034 : 20719 : location_t from = linemap_included_from (iter->src);
18035 : 20719 : gcc_checking_assert (from < MAP_START_LOCATION (iter->src));
18036 : 20719 : if (from != UNKNOWN_LOCATION && has_partitions)
18037 : : {
18038 : : /* A partition's span will have a from pointing at a
18039 : : MODULE_INC. Find that map's from. */
18040 : 143 : line_map_ordinary const *fmap
18041 : 143 : = linemap_check_ordinary (linemap_lookup (line_table, from));
18042 : 143 : if (MAP_MODULE_P (fmap))
18043 : 122 : from = linemap_included_from (fmap);
18044 : : }
18045 : 20719 : write_location (sec, from);
18046 : : }
18047 : : }
18048 : :
18049 : 2202 : filenames.release ();
18050 : :
18051 : 2202 : sec.end (to, to->name (MOD_SNAME_PFX ".olm"), crc_p);
18052 : 2202 : dump.outdent ();
18053 : 2202 : }
18054 : :
18055 : : void
18056 : 112 : module_state::write_macro_maps (elf_out *to, range_t &info, unsigned *crc_p)
18057 : : {
18058 : 124 : dump () && dump ("Writing macro location maps");
18059 : 112 : dump.indent ();
18060 : :
18061 : 112 : bytes_out sec (to);
18062 : 112 : sec.begin ();
18063 : :
18064 : 124 : dump () && dump ("Macro maps:%K", info.second);
18065 : 112 : sec.loc (info.second);
18066 : :
18067 : 112 : line_map_uint_t macro_num = 0;
18068 : 224 : for (auto iter = macro_loc_remap->end (), begin = macro_loc_remap->begin ();
18069 : 111759 : iter-- != begin;)
18070 : : {
18071 : 111647 : auto mac = iter->src;
18072 : 111647 : sec.loc (iter->remap);
18073 : 111647 : sec.u (mac->n_tokens);
18074 : 111647 : sec.cpp_node (mac->macro);
18075 : 111647 : write_location (sec, mac->m_expansion);
18076 : 111647 : const location_t *locs = mac->macro_locations;
18077 : : /* There are lots of identical runs. */
18078 : 111647 : location_t prev = UNKNOWN_LOCATION;
18079 : 111647 : unsigned count = 0;
18080 : 111647 : unsigned runs = 0;
18081 : 3895409 : for (unsigned jx = mac->n_tokens * 2; jx--;)
18082 : : {
18083 : 3783762 : location_t tok_loc = locs[jx];
18084 : 3783762 : if (tok_loc == prev)
18085 : : {
18086 : 1791983 : count++;
18087 : 1791983 : continue;
18088 : : }
18089 : 1991779 : runs++;
18090 : 1991779 : sec.u (count);
18091 : 1991779 : count = 1;
18092 : 1991779 : prev = tok_loc;
18093 : 1991779 : write_location (sec, tok_loc);
18094 : : }
18095 : 111647 : sec.u (count);
18096 : 111647 : dump (dumper::LOCATION)
18097 : 9 : && dump ("Macro:%K %I %u/%u*2 locations [%K,%K)->%K",
18098 : 9 : macro_num, identifier (mac->macro),
18099 : : runs, mac->n_tokens,
18100 : : MAP_START_LOCATION (mac),
18101 : 9 : MAP_START_LOCATION (mac) + mac->n_tokens,
18102 : : iter->remap);
18103 : 111647 : macro_num++;
18104 : : }
18105 : 112 : gcc_assert (macro_num == info.second);
18106 : :
18107 : 112 : sec.end (to, to->name (MOD_SNAME_PFX ".mlm"), crc_p);
18108 : 112 : dump.outdent ();
18109 : 112 : }
18110 : :
18111 : : bool
18112 : 2445 : module_state::read_ordinary_maps (line_map_uint_t num_ord_locs,
18113 : : unsigned range_bits)
18114 : : {
18115 : 2445 : bytes_in sec;
18116 : :
18117 : 2445 : if (!sec.begin (loc, from (), MOD_SNAME_PFX ".olm"))
18118 : : return false;
18119 : 2894 : dump () && dump ("Reading ordinary location maps");
18120 : 2445 : dump.indent ();
18121 : :
18122 : : /* Read the filename table. */
18123 : 2445 : unsigned len = sec.u ();
18124 : 2894 : dump () && dump ("%u source file names", len);
18125 : 2445 : vec<const char *> filenames;
18126 : 2445 : filenames.create (len);
18127 : 15710 : for (unsigned ix = 0; ix != len; ix++)
18128 : : {
18129 : 13265 : size_t l;
18130 : 13265 : const char *buf = sec.str (&l);
18131 : 13265 : char *fname = XNEWVEC (char, l + 1);
18132 : 13265 : memcpy (fname, buf, l + 1);
18133 : 13265 : dump (dumper::LOCATION) && dump ("Source file[%u]=%s", ix, fname);
18134 : : /* We leak these names into the line-map table. But it
18135 : : doesn't own them. */
18136 : 13265 : filenames.quick_push (fname);
18137 : : }
18138 : :
18139 : 2445 : line_map_uint_t num_ordinary = sec.loc ();
18140 : 2894 : dump () && dump ("Ordinary maps:%K, range_bits:%u",
18141 : : num_ordinary, range_bits);
18142 : :
18143 : 2445 : location_t offset = line_table->highest_location + 1;
18144 : 2445 : offset += ((loc_one << range_bits) - 1);
18145 : 2445 : offset &= ~((loc_one << range_bits) - 1);
18146 : 2445 : ordinary_locs.first = offset;
18147 : :
18148 : 2445 : bool propagated = spans.maybe_propagate (this, offset);
18149 : 2445 : line_map_ordinary *maps = static_cast<line_map_ordinary *>
18150 : 2445 : (line_map_new_raw (line_table, false, num_ordinary));
18151 : :
18152 : 2445 : const line_map_ordinary *base = nullptr;
18153 : 223450 : for (line_map_uint_t ix = 0; ix != num_ordinary && !sec.get_overrun (); ix++)
18154 : : {
18155 : 221005 : line_map_ordinary *map = &maps[ix];
18156 : :
18157 : 221005 : location_t offset = sec.loc ();
18158 : 221005 : if (!offset)
18159 : : {
18160 : 25032 : map->reason = LC_RENAME;
18161 : 25032 : map->sysp = sec.u ();
18162 : 25032 : map->m_range_bits = sec.u ();
18163 : 25032 : map->m_column_and_range_bits = sec.u () + map->m_range_bits;
18164 : 25032 : unsigned fnum = sec.u ();
18165 : 50064 : map->to_file = (fnum < filenames.length () ? filenames[fnum] : "");
18166 : 25032 : map->to_line = sec.u ();
18167 : 25032 : base = map;
18168 : : }
18169 : : else
18170 : : {
18171 : 195973 : *map = *base;
18172 : 195973 : map->to_line += offset >> map->m_column_and_range_bits;
18173 : : }
18174 : 221005 : location_t remap = sec.loc ();
18175 : 221005 : map->start_location = remap + ordinary_locs.first;
18176 : 221005 : if (base == map)
18177 : : {
18178 : : /* Root the outermost map at our location. */
18179 : 25032 : ordinary_locs.second = remap;
18180 : 25032 : location_t from = read_location (sec);
18181 : 25032 : map->included_from = from != UNKNOWN_LOCATION ? from : loc;
18182 : : }
18183 : : }
18184 : :
18185 : 2445 : ordinary_locs.second = num_ord_locs;
18186 : : /* highest_location is the one handed out, not the next one to
18187 : : hand out. */
18188 : 2445 : line_table->highest_location = ordinary_locs.first + ordinary_locs.second - 1;
18189 : :
18190 : 2445 : if (line_table->highest_location >= LINE_MAP_MAX_LOCATION_WITH_COLS)
18191 : : /* We shouldn't run out of locations, as we checked before
18192 : : starting. */
18193 : 0 : sec.set_overrun ();
18194 : 2894 : dump () && dump ("Ordinary location [%K,+%K)",
18195 : : ordinary_locs.first, ordinary_locs.second);
18196 : :
18197 : 2445 : if (propagated)
18198 : 131 : spans.close ();
18199 : :
18200 : 2445 : filenames.release ();
18201 : :
18202 : 2445 : dump.outdent ();
18203 : 2445 : if (!sec.end (from ()))
18204 : : return false;
18205 : :
18206 : : return true;
18207 : 2445 : }
18208 : :
18209 : : bool
18210 : 122 : module_state::read_macro_maps (line_map_uint_t num_macro_locs)
18211 : : {
18212 : 122 : bytes_in sec;
18213 : :
18214 : 122 : if (!sec.begin (loc, from (), MOD_SNAME_PFX ".mlm"))
18215 : : return false;
18216 : 128 : dump () && dump ("Reading macro location maps");
18217 : 122 : dump.indent ();
18218 : :
18219 : 122 : line_map_uint_t num_macros = sec.loc ();
18220 : 128 : dump () && dump ("Macro maps:%K locs:%K",
18221 : : num_macros, num_macro_locs);
18222 : :
18223 : 244 : bool propagated = spans.maybe_propagate (this,
18224 : 122 : line_table->highest_location + 1);
18225 : :
18226 : 122 : location_t offset = LINEMAPS_MACRO_LOWEST_LOCATION (line_table);
18227 : 122 : macro_locs.second = num_macro_locs;
18228 : 122 : macro_locs.first = offset - num_macro_locs;
18229 : :
18230 : 128 : dump () && dump ("Macro loc delta %K", offset);
18231 : 128 : dump () && dump ("Macro locations [%K,%K)",
18232 : : macro_locs.first, macro_locs.second);
18233 : :
18234 : 141021 : for (line_map_uint_t ix = 0; ix != num_macros && !sec.get_overrun (); ix++)
18235 : : {
18236 : 140899 : location_t offset = sec.loc ();
18237 : 140899 : unsigned n_tokens = sec.u ();
18238 : 140899 : cpp_hashnode *node = sec.cpp_node ();
18239 : 140899 : location_t exp_loc = read_location (sec);
18240 : :
18241 : 140899 : const line_map_macro *macro
18242 : 140899 : = linemap_enter_macro (line_table, node, exp_loc, n_tokens);
18243 : 140899 : if (!macro)
18244 : : /* We shouldn't run out of locations, as we checked that we
18245 : : had enough before starting. */
18246 : : break;
18247 : 140899 : gcc_checking_assert (MAP_START_LOCATION (macro)
18248 : : == offset + macro_locs.first);
18249 : :
18250 : 140899 : location_t *locs = macro->macro_locations;
18251 : 140899 : location_t tok_loc = UNKNOWN_LOCATION;
18252 : 140899 : unsigned count = sec.u ();
18253 : 140899 : unsigned runs = 0;
18254 : 4834725 : for (unsigned jx = macro->n_tokens * 2; jx-- && !sec.get_overrun ();)
18255 : : {
18256 : 7167782 : while (!count-- && !sec.get_overrun ())
18257 : : {
18258 : 2473956 : runs++;
18259 : 2473956 : tok_loc = read_location (sec);
18260 : 2473956 : count = sec.u ();
18261 : : }
18262 : 4693826 : locs[jx] = tok_loc;
18263 : : }
18264 : 140899 : if (count)
18265 : 0 : sec.set_overrun ();
18266 : 140926 : dump (dumper::LOCATION)
18267 : 0 : && dump ("Macro:%K %I %u/%u*2 locations [%K,%K)",
18268 : : ix, identifier (node), runs, n_tokens,
18269 : : MAP_START_LOCATION (macro),
18270 : 0 : MAP_START_LOCATION (macro) + n_tokens);
18271 : : }
18272 : :
18273 : 128 : dump () && dump ("Macro location lwm:%K", macro_locs.first);
18274 : 122 : if (propagated)
18275 : 3 : spans.close ();
18276 : :
18277 : 122 : dump.outdent ();
18278 : 122 : if (!sec.end (from ()))
18279 : : return false;
18280 : :
18281 : : return true;
18282 : 122 : }
18283 : :
18284 : : /* Serialize the definition of MACRO. */
18285 : :
18286 : : void
18287 : 70948 : module_state::write_define (bytes_out &sec, const cpp_macro *macro)
18288 : : {
18289 : 70948 : sec.u (macro->count);
18290 : :
18291 : 70948 : bytes_out::bits_out bits = sec.stream_bits ();
18292 : 70948 : bits.b (macro->fun_like);
18293 : 70948 : bits.b (macro->variadic);
18294 : 70948 : bits.b (macro->syshdr);
18295 : 70948 : bits.bflush ();
18296 : :
18297 : 70948 : write_location (sec, macro->line);
18298 : 70948 : if (macro->fun_like)
18299 : : {
18300 : 8555 : sec.u (macro->paramc);
18301 : 8555 : const cpp_hashnode *const *parms = macro->parm.params;
18302 : 21620 : for (unsigned ix = 0; ix != macro->paramc; ix++)
18303 : 13065 : sec.cpp_node (parms[ix]);
18304 : : }
18305 : :
18306 : : unsigned len = 0;
18307 : 224516 : for (unsigned ix = 0; ix != macro->count; ix++)
18308 : : {
18309 : 153568 : const cpp_token *token = ¯o->exp.tokens[ix];
18310 : 153568 : write_location (sec, token->src_loc);
18311 : 153568 : sec.u (token->type);
18312 : 153568 : sec.u (token->flags);
18313 : 153568 : switch (cpp_token_val_index (token))
18314 : : {
18315 : 0 : default:
18316 : 0 : gcc_unreachable ();
18317 : :
18318 : 11248 : case CPP_TOKEN_FLD_ARG_NO:
18319 : : /* An argument reference. */
18320 : 11248 : sec.u (token->val.macro_arg.arg_no);
18321 : 11248 : sec.cpp_node (token->val.macro_arg.spelling);
18322 : 11248 : break;
18323 : :
18324 : 33148 : case CPP_TOKEN_FLD_NODE:
18325 : : /* An identifier. */
18326 : 33148 : sec.cpp_node (token->val.node.node);
18327 : 33148 : if (token->val.node.spelling == token->val.node.node)
18328 : : /* The spelling will usually be the same. so optimize
18329 : : that. */
18330 : 33148 : sec.str (NULL, 0);
18331 : : else
18332 : 0 : sec.cpp_node (token->val.node.spelling);
18333 : : break;
18334 : :
18335 : : case CPP_TOKEN_FLD_NONE:
18336 : : break;
18337 : :
18338 : 48223 : case CPP_TOKEN_FLD_STR:
18339 : : /* A string, number or comment. Not always NUL terminated,
18340 : : we stream out in a single contatenation with embedded
18341 : : NULs as that's a safe default. */
18342 : 48223 : len += token->val.str.len + 1;
18343 : 48223 : sec.u (token->val.str.len);
18344 : 48223 : break;
18345 : :
18346 : 0 : case CPP_TOKEN_FLD_SOURCE:
18347 : 0 : case CPP_TOKEN_FLD_TOKEN_NO:
18348 : 0 : case CPP_TOKEN_FLD_PRAGMA:
18349 : : /* These do not occur inside a macro itself. */
18350 : 0 : gcc_unreachable ();
18351 : : }
18352 : : }
18353 : :
18354 : 70948 : if (len)
18355 : : {
18356 : 44866 : char *ptr = reinterpret_cast<char *> (sec.buf (len));
18357 : 44866 : len = 0;
18358 : 135522 : for (unsigned ix = 0; ix != macro->count; ix++)
18359 : : {
18360 : 90656 : const cpp_token *token = ¯o->exp.tokens[ix];
18361 : 90656 : if (cpp_token_val_index (token) == CPP_TOKEN_FLD_STR)
18362 : : {
18363 : 48223 : memcpy (ptr + len, token->val.str.text,
18364 : 48223 : token->val.str.len);
18365 : 48223 : len += token->val.str.len;
18366 : 48223 : ptr[len++] = 0;
18367 : : }
18368 : : }
18369 : : }
18370 : 70948 : }
18371 : :
18372 : : /* Read a macro definition. */
18373 : :
18374 : : cpp_macro *
18375 : 314 : module_state::read_define (bytes_in &sec, cpp_reader *reader) const
18376 : : {
18377 : 314 : unsigned count = sec.u ();
18378 : : /* We rely on knowing cpp_reader's hash table is ident_hash, and
18379 : : its subobject allocator is stringpool_ggc_alloc and that is just
18380 : : a wrapper for ggc_alloc_atomic. */
18381 : 314 : cpp_macro *macro
18382 : 628 : = (cpp_macro *)ggc_alloc_atomic (sizeof (cpp_macro)
18383 : 314 : + sizeof (cpp_token) * (count - !!count));
18384 : 314 : memset (macro, 0, sizeof (cpp_macro) + sizeof (cpp_token) * (count - !!count));
18385 : :
18386 : 314 : macro->count = count;
18387 : 314 : macro->kind = cmk_macro;
18388 : 314 : macro->imported_p = true;
18389 : :
18390 : 314 : bytes_in::bits_in bits = sec.stream_bits ();
18391 : 314 : macro->fun_like = bits.b ();
18392 : 314 : macro->variadic = bits.b ();
18393 : 314 : macro->syshdr = bits.b ();
18394 : 314 : bits.bflush ();
18395 : :
18396 : 314 : macro->line = read_location (sec);
18397 : :
18398 : 314 : if (macro->fun_like)
18399 : : {
18400 : 63 : unsigned paramc = sec.u ();
18401 : 63 : cpp_hashnode **params
18402 : 63 : = (cpp_hashnode **)ggc_alloc_atomic (sizeof (cpp_hashnode *) * paramc);
18403 : 63 : macro->paramc = paramc;
18404 : 63 : macro->parm.params = params;
18405 : 135 : for (unsigned ix = 0; ix != paramc; ix++)
18406 : 72 : params[ix] = sec.cpp_node ();
18407 : : }
18408 : :
18409 : : unsigned len = 0;
18410 : 1062 : for (unsigned ix = 0; ix != count && !sec.get_overrun (); ix++)
18411 : : {
18412 : 748 : cpp_token *token = ¯o->exp.tokens[ix];
18413 : 748 : token->src_loc = read_location (sec);
18414 : 748 : token->type = cpp_ttype (sec.u ());
18415 : 748 : token->flags = sec.u ();
18416 : 748 : switch (cpp_token_val_index (token))
18417 : : {
18418 : 0 : default:
18419 : 0 : sec.set_overrun ();
18420 : 0 : break;
18421 : :
18422 : 58 : case CPP_TOKEN_FLD_ARG_NO:
18423 : : /* An argument reference. */
18424 : 58 : {
18425 : 58 : unsigned arg_no = sec.u ();
18426 : 58 : if (arg_no - 1 >= macro->paramc)
18427 : 0 : sec.set_overrun ();
18428 : 58 : token->val.macro_arg.arg_no = arg_no;
18429 : 58 : token->val.macro_arg.spelling = sec.cpp_node ();
18430 : : }
18431 : 58 : break;
18432 : :
18433 : 197 : case CPP_TOKEN_FLD_NODE:
18434 : : /* An identifier. */
18435 : 197 : token->val.node.node = sec.cpp_node ();
18436 : 197 : token->val.node.spelling = sec.cpp_node ();
18437 : 197 : if (!token->val.node.spelling)
18438 : 197 : token->val.node.spelling = token->val.node.node;
18439 : : break;
18440 : :
18441 : : case CPP_TOKEN_FLD_NONE:
18442 : : break;
18443 : :
18444 : 175 : case CPP_TOKEN_FLD_STR:
18445 : : /* A string, number or comment. */
18446 : 175 : token->val.str.len = sec.u ();
18447 : 175 : len += token->val.str.len + 1;
18448 : 175 : break;
18449 : : }
18450 : : }
18451 : :
18452 : 314 : if (len)
18453 : 174 : if (const char *ptr = reinterpret_cast<const char *> (sec.buf (len)))
18454 : : {
18455 : : /* There should be a final NUL. */
18456 : 174 : if (ptr[len-1])
18457 : 0 : sec.set_overrun ();
18458 : : /* cpp_alloc_token_string will add a final NUL. */
18459 : 174 : const unsigned char *buf
18460 : 174 : = cpp_alloc_token_string (reader, (const unsigned char *)ptr, len - 1);
18461 : 174 : len = 0;
18462 : 617 : for (unsigned ix = 0; ix != count && !sec.get_overrun (); ix++)
18463 : : {
18464 : 443 : cpp_token *token = ¯o->exp.tokens[ix];
18465 : 443 : if (cpp_token_val_index (token) == CPP_TOKEN_FLD_STR)
18466 : : {
18467 : 175 : token->val.str.text = buf + len;
18468 : 175 : len += token->val.str.len;
18469 : 175 : if (buf[len++])
18470 : 0 : sec.set_overrun ();
18471 : : }
18472 : : }
18473 : : }
18474 : :
18475 : 314 : if (sec.get_overrun ())
18476 : 0 : return NULL;
18477 : : return macro;
18478 : 314 : }
18479 : :
18480 : : /* Exported macro data. */
18481 : : struct GTY(()) macro_export {
18482 : : cpp_macro *def;
18483 : : location_t undef_loc;
18484 : :
18485 : 106409 : macro_export ()
18486 : 106409 : :def (NULL), undef_loc (UNKNOWN_LOCATION)
18487 : : {
18488 : : }
18489 : : };
18490 : :
18491 : : /* Imported macro data. */
18492 : : class macro_import {
18493 : : public:
18494 : : struct slot {
18495 : : #if defined (WORDS_BIGENDIAN) && SIZEOF_VOID_P == 8
18496 : : int offset;
18497 : : #endif
18498 : : /* We need to ensure we don't use the LSB for representation, as
18499 : : that's the union discriminator below. */
18500 : : unsigned bits;
18501 : :
18502 : : #if !(defined (WORDS_BIGENDIAN) && SIZEOF_VOID_P == 8)
18503 : : int offset;
18504 : : #endif
18505 : :
18506 : : public:
18507 : : enum Layout {
18508 : : L_DEF = 1,
18509 : : L_UNDEF = 2,
18510 : : L_BOTH = 3,
18511 : : L_MODULE_SHIFT = 2
18512 : : };
18513 : :
18514 : : public:
18515 : : /* Not a regular ctor, because we put it in a union, and that's
18516 : : not allowed in C++ 98. */
18517 : 184377 : static slot ctor (unsigned module, unsigned defness)
18518 : : {
18519 : 184377 : gcc_checking_assert (defness);
18520 : 184377 : slot s;
18521 : 184377 : s.bits = defness | (module << L_MODULE_SHIFT);
18522 : 184377 : s.offset = -1;
18523 : 184377 : return s;
18524 : : }
18525 : :
18526 : : public:
18527 : 143401 : unsigned get_defness () const
18528 : : {
18529 : 143401 : return bits & L_BOTH;
18530 : : }
18531 : 108138 : unsigned get_module () const
18532 : : {
18533 : 108138 : return bits >> L_MODULE_SHIFT;
18534 : : }
18535 : 12 : void become_undef ()
18536 : : {
18537 : 12 : bits &= ~unsigned (L_DEF);
18538 : 12 : bits |= unsigned (L_UNDEF);
18539 : : }
18540 : : };
18541 : :
18542 : : private:
18543 : : typedef vec<slot, va_heap, vl_embed> ary_t;
18544 : : union either {
18545 : : /* Discriminated by bits 0|1 != 0. The expected case is that
18546 : : there will be exactly one slot per macro, hence the effort of
18547 : : packing that. */
18548 : : ary_t *ary;
18549 : : slot single;
18550 : : } u;
18551 : :
18552 : : public:
18553 : 144544 : macro_import ()
18554 : 144544 : {
18555 : 144544 : u.ary = NULL;
18556 : : }
18557 : :
18558 : : private:
18559 : 7914779 : bool single_p () const
18560 : : {
18561 : 7914779 : return u.single.bits & slot::L_BOTH;
18562 : : }
18563 : 8059329 : bool occupied_p () const
18564 : : {
18565 : 8059329 : return u.ary != NULL;
18566 : : }
18567 : :
18568 : : public:
18569 : 900 : unsigned length () const
18570 : : {
18571 : 900 : gcc_checking_assert (occupied_p ());
18572 : 900 : return single_p () ? 1 : u.ary->length ();
18573 : : }
18574 : 7767957 : slot &operator[] (unsigned ix)
18575 : : {
18576 : 7767957 : gcc_checking_assert (occupied_p ());
18577 : 7767957 : if (single_p ())
18578 : : {
18579 : 7675827 : gcc_checking_assert (!ix);
18580 : 7675827 : return u.single;
18581 : : }
18582 : : else
18583 : 92130 : return (*u.ary)[ix];
18584 : : }
18585 : :
18586 : : public:
18587 : : slot &exported ();
18588 : : slot &append (unsigned module, unsigned defness);
18589 : : };
18590 : :
18591 : : /* O is a new import to append to the list for. If we're an empty
18592 : : set, initialize us. */
18593 : :
18594 : : macro_import::slot &
18595 : 184377 : macro_import::append (unsigned module, unsigned defness)
18596 : : {
18597 : 184377 : if (!occupied_p ())
18598 : : {
18599 : 144544 : u.single = slot::ctor (module, defness);
18600 : 144544 : return u.single;
18601 : : }
18602 : : else
18603 : : {
18604 : 39833 : bool single = single_p ();
18605 : 39833 : ary_t *m = single ? NULL : u.ary;
18606 : 39833 : vec_safe_reserve (m, 1 + single);
18607 : 39833 : if (single)
18608 : 39830 : m->quick_push (u.single);
18609 : 39833 : u.ary = m;
18610 : 39833 : return *u.ary->quick_push (slot::ctor (module, defness));
18611 : : }
18612 : : }
18613 : :
18614 : : /* We're going to export something. Make sure the first import slot
18615 : : is us. */
18616 : :
18617 : : macro_import::slot &
18618 : 106095 : macro_import::exported ()
18619 : : {
18620 : 106095 : if (occupied_p () && !(*this)[0].get_module ())
18621 : : {
18622 : 6 : slot &res = (*this)[0];
18623 : 6 : res.bits |= slot::L_DEF;
18624 : 6 : return res;
18625 : : }
18626 : :
18627 : 106089 : slot *a = &append (0, slot::L_DEF);
18628 : 106089 : if (!single_p ())
18629 : : {
18630 : 35690 : slot &f = (*this)[0];
18631 : 35690 : std::swap (f, *a);
18632 : 35690 : a = &f;
18633 : : }
18634 : : return *a;
18635 : : }
18636 : :
18637 : : /* The import (&exported) macros. cpp_hasnode's deferred field
18638 : : indexes this array (offset by 1, so zero means 'not present'. */
18639 : :
18640 : : static vec<macro_import, va_heap, vl_embed> *macro_imports;
18641 : :
18642 : : /* The exported macros. A macro_import slot's zeroth element's offset
18643 : : indexes this array. If the zeroth slot is not for module zero,
18644 : : there is no export. */
18645 : :
18646 : : static GTY(()) vec<macro_export, va_gc> *macro_exports;
18647 : :
18648 : : /* The reachable set of header imports from this TU. */
18649 : :
18650 : : static GTY(()) bitmap headers;
18651 : :
18652 : : /* Get the (possibly empty) macro imports for NODE. */
18653 : :
18654 : : static macro_import &
18655 : 148693 : get_macro_imports (cpp_hashnode *node)
18656 : : {
18657 : 148693 : if (node->deferred)
18658 : 4149 : return (*macro_imports)[node->deferred - 1];
18659 : :
18660 : 144544 : vec_safe_reserve (macro_imports, 1);
18661 : 144544 : node->deferred = macro_imports->length () + 1;
18662 : 144544 : return *vec_safe_push (macro_imports, macro_import ());
18663 : : }
18664 : :
18665 : : /* Get the macro export for export EXP of NODE. */
18666 : :
18667 : : static macro_export &
18668 : 106095 : get_macro_export (macro_import::slot &slot)
18669 : : {
18670 : 106095 : if (slot.offset >= 0)
18671 : 6 : return (*macro_exports)[slot.offset];
18672 : :
18673 : 106089 : vec_safe_reserve (macro_exports, 1);
18674 : 106089 : slot.offset = macro_exports->length ();
18675 : 106089 : return *macro_exports->quick_push (macro_export ());
18676 : : }
18677 : :
18678 : : /* If NODE is an exportable macro, add it to the export set. */
18679 : :
18680 : : static int
18681 : 3513669 : maybe_add_macro (cpp_reader *, cpp_hashnode *node, void *data_)
18682 : : {
18683 : 3513669 : bool exporting = false;
18684 : :
18685 : 3513669 : if (cpp_user_macro_p (node))
18686 : 469105 : if (cpp_macro *macro = node->value.macro)
18687 : : /* Ignore imported, builtins, command line and forced header macros. */
18688 : 468663 : if (!macro->imported_p
18689 : 468663 : && !macro->lazy && macro->line >= spans.main_start ())
18690 : : {
18691 : 70405 : gcc_checking_assert (macro->kind == cmk_macro);
18692 : : /* I don't want to deal with this corner case, that I suspect is
18693 : : a devil's advocate reading of the standard. */
18694 : 70405 : gcc_checking_assert (!macro->extra_tokens);
18695 : :
18696 : 70405 : macro_import::slot &slot = get_macro_imports (node).exported ();
18697 : 70405 : macro_export &exp = get_macro_export (slot);
18698 : 70405 : exp.def = macro;
18699 : 70405 : exporting = true;
18700 : : }
18701 : :
18702 : 3443264 : if (!exporting && node->deferred)
18703 : : {
18704 : 581 : macro_import &imports = (*macro_imports)[node->deferred - 1];
18705 : 581 : macro_import::slot &slot = imports[0];
18706 : 581 : if (!slot.get_module ())
18707 : : {
18708 : 550 : gcc_checking_assert (slot.get_defness ());
18709 : : exporting = true;
18710 : : }
18711 : : }
18712 : :
18713 : 70405 : if (exporting)
18714 : 70955 : static_cast<vec<cpp_hashnode *> *> (data_)->safe_push (node);
18715 : :
18716 : 3513669 : return 1; /* Don't stop. */
18717 : : }
18718 : :
18719 : : /* Order cpp_hashnodes A_ and B_ by their exported macro locations. */
18720 : :
18721 : : static int
18722 : 3740965 : macro_loc_cmp (const void *a_, const void *b_)
18723 : : {
18724 : 3740965 : const cpp_hashnode *node_a = *(const cpp_hashnode *const *)a_;
18725 : 3740965 : macro_import &import_a = (*macro_imports)[node_a->deferred - 1];
18726 : 3740965 : const macro_export &export_a = (*macro_exports)[import_a[0].offset];
18727 : 3740965 : location_t loc_a = export_a.def ? export_a.def->line : export_a.undef_loc;
18728 : :
18729 : 3740965 : const cpp_hashnode *node_b = *(const cpp_hashnode *const *)b_;
18730 : 3740965 : macro_import &import_b = (*macro_imports)[node_b->deferred - 1];
18731 : 3740965 : const macro_export &export_b = (*macro_exports)[import_b[0].offset];
18732 : 3740965 : location_t loc_b = export_b.def ? export_b.def->line : export_b.undef_loc;
18733 : :
18734 : 3740965 : if (loc_a < loc_b)
18735 : : return +1;
18736 : 1922007 : else if (loc_a > loc_b)
18737 : : return -1;
18738 : : else
18739 : 0 : return 0;
18740 : : }
18741 : :
18742 : : /* Gather the macro definitions and undefinitions that we will need to
18743 : : write out. */
18744 : :
18745 : : vec<cpp_hashnode *> *
18746 : 838 : module_state::prepare_macros (cpp_reader *reader)
18747 : : {
18748 : 838 : vec<cpp_hashnode *> *macros;
18749 : 838 : vec_alloc (macros, 100);
18750 : :
18751 : 838 : cpp_forall_identifiers (reader, maybe_add_macro, macros);
18752 : :
18753 : 862 : dump (dumper::MACRO) && dump ("No more than %u macros", macros->length ());
18754 : :
18755 : 838 : macros->qsort (macro_loc_cmp);
18756 : :
18757 : : // Note the locations.
18758 : 72631 : for (unsigned ix = macros->length (); ix--;)
18759 : : {
18760 : 70955 : cpp_hashnode *node = (*macros)[ix];
18761 : 70955 : macro_import::slot &slot = (*macro_imports)[node->deferred - 1][0];
18762 : 70955 : macro_export &mac = (*macro_exports)[slot.offset];
18763 : :
18764 : 70955 : if (IDENTIFIER_KEYWORD_P (identifier (node)))
18765 : 1 : continue;
18766 : :
18767 : 70954 : if (mac.undef_loc != UNKNOWN_LOCATION)
18768 : 12 : note_location (mac.undef_loc);
18769 : 70954 : if (mac.def)
18770 : : {
18771 : 70948 : note_location (mac.def->line);
18772 : 224516 : for (unsigned ix = 0; ix != mac.def->count; ix++)
18773 : 153568 : note_location (mac.def->exp.tokens[ix].src_loc);
18774 : : }
18775 : : }
18776 : :
18777 : 838 : return macros;
18778 : : }
18779 : :
18780 : : /* Write out the exported defines. This is two sections, one
18781 : : containing the definitions, the other a table of node names. */
18782 : :
18783 : : unsigned
18784 : 838 : module_state::write_macros (elf_out *to, vec<cpp_hashnode *> *macros,
18785 : : unsigned *crc_p)
18786 : : {
18787 : 904 : dump () && dump ("Writing macros");
18788 : 838 : dump.indent ();
18789 : :
18790 : : /* Write the defs */
18791 : 838 : bytes_out sec (to);
18792 : 838 : sec.begin ();
18793 : :
18794 : 838 : unsigned count = 0;
18795 : 72631 : for (unsigned ix = macros->length (); ix--;)
18796 : : {
18797 : 70955 : cpp_hashnode *node = (*macros)[ix];
18798 : 70955 : macro_import::slot &slot = (*macro_imports)[node->deferred - 1][0];
18799 : 70955 : gcc_assert (!slot.get_module () && slot.get_defness ());
18800 : :
18801 : 70955 : macro_export &mac = (*macro_exports)[slot.offset];
18802 : 70955 : gcc_assert (!!(slot.get_defness () & macro_import::slot::L_UNDEF)
18803 : : == (mac.undef_loc != UNKNOWN_LOCATION)
18804 : : && !!(slot.get_defness () & macro_import::slot::L_DEF)
18805 : : == (mac.def != NULL));
18806 : :
18807 : 70955 : if (IDENTIFIER_KEYWORD_P (identifier (node)))
18808 : : {
18809 : 1 : warning_at (mac.def->line, 0,
18810 : : "not exporting %<#define %E%> as it is a keyword",
18811 : : identifier (node));
18812 : 1 : slot.offset = 0;
18813 : 1 : continue;
18814 : : }
18815 : :
18816 : 70954 : count++;
18817 : 70954 : slot.offset = sec.pos;
18818 : 70954 : dump (dumper::MACRO)
18819 : 24 : && dump ("Writing macro %s%s%s %I at %u",
18820 : 24 : slot.get_defness () & macro_import::slot::L_UNDEF
18821 : : ? "#undef" : "",
18822 : 24 : slot.get_defness () == macro_import::slot::L_BOTH
18823 : : ? " & " : "",
18824 : 24 : slot.get_defness () & macro_import::slot::L_DEF
18825 : : ? "#define" : "",
18826 : : identifier (node), slot.offset);
18827 : 70954 : if (mac.undef_loc != UNKNOWN_LOCATION)
18828 : 12 : write_location (sec, mac.undef_loc);
18829 : 70954 : if (mac.def)
18830 : 70948 : write_define (sec, mac.def);
18831 : : }
18832 : 838 : if (count)
18833 : : // We may have ended on a tokenless macro with a very short
18834 : : // location, that will cause problems reading its bit flags.
18835 : 136 : sec.u (0);
18836 : 838 : sec.end (to, to->name (MOD_SNAME_PFX ".def"), crc_p);
18837 : :
18838 : 838 : if (count)
18839 : : {
18840 : : /* Write the table. */
18841 : 136 : bytes_out sec (to);
18842 : 136 : sec.begin ();
18843 : 136 : sec.u (count);
18844 : :
18845 : 71226 : for (unsigned ix = macros->length (); ix--;)
18846 : : {
18847 : 70954 : const cpp_hashnode *node = (*macros)[ix];
18848 : 70954 : macro_import::slot &slot = (*macro_imports)[node->deferred - 1][0];
18849 : :
18850 : 70954 : if (slot.offset)
18851 : : {
18852 : 70954 : sec.cpp_node (node);
18853 : 70954 : sec.u (slot.get_defness ());
18854 : 70954 : sec.u (slot.offset);
18855 : : }
18856 : : }
18857 : 136 : sec.end (to, to->name (MOD_SNAME_PFX ".mac"), crc_p);
18858 : 136 : }
18859 : :
18860 : 838 : dump.outdent ();
18861 : 838 : return count;
18862 : 838 : }
18863 : :
18864 : : bool
18865 : 871 : module_state::read_macros ()
18866 : : {
18867 : : /* Get the def section. */
18868 : 871 : if (!slurp->macro_defs.begin (loc, from (), MOD_SNAME_PFX ".def"))
18869 : : return false;
18870 : :
18871 : : /* Get the tbl section, if there are defs. */
18872 : 871 : if (slurp->macro_defs.more_p ()
18873 : 871 : && !slurp->macro_tbl.begin (loc, from (), MOD_SNAME_PFX ".mac"))
18874 : : return false;
18875 : :
18876 : : return true;
18877 : : }
18878 : :
18879 : : /* Install the macro name table. */
18880 : :
18881 : : void
18882 : 877 : module_state::install_macros ()
18883 : : {
18884 : 877 : bytes_in &sec = slurp->macro_tbl;
18885 : 877 : if (!sec.size)
18886 : : return;
18887 : :
18888 : 192 : dump () && dump ("Reading macro table %M", this);
18889 : 171 : dump.indent ();
18890 : :
18891 : 171 : unsigned count = sec.u ();
18892 : 192 : dump () && dump ("%u macros", count);
18893 : 78459 : while (count--)
18894 : : {
18895 : 78288 : cpp_hashnode *node = sec.cpp_node ();
18896 : 78288 : macro_import &imp = get_macro_imports (node);
18897 : 78288 : unsigned flags = sec.u () & macro_import::slot::L_BOTH;
18898 : 78288 : if (!flags)
18899 : 0 : sec.set_overrun ();
18900 : :
18901 : 78288 : if (sec.get_overrun ())
18902 : : break;
18903 : :
18904 : 78288 : macro_import::slot &slot = imp.append (mod, flags);
18905 : 78288 : slot.offset = sec.u ();
18906 : :
18907 : 78288 : dump (dumper::MACRO)
18908 : 84 : && dump ("Read %s macro %s%s%s %I at %u",
18909 : 30 : imp.length () > 1 ? "add" : "new",
18910 : 27 : flags & macro_import::slot::L_UNDEF ? "#undef" : "",
18911 : : flags == macro_import::slot::L_BOTH ? " & " : "",
18912 : 30 : flags & macro_import::slot::L_DEF ? "#define" : "",
18913 : : identifier (node), slot.offset);
18914 : :
18915 : : /* We'll leak an imported definition's TOKEN_FLD_STR's data
18916 : : here. But that only happens when we've had to resolve the
18917 : : deferred macro before this import -- why are you doing
18918 : : that? */
18919 : 78288 : if (cpp_macro *cur = cpp_set_deferred_macro (node))
18920 : 35678 : if (!cur->imported_p)
18921 : : {
18922 : 35678 : macro_import::slot &slot = imp.exported ();
18923 : 35678 : macro_export &exp = get_macro_export (slot);
18924 : 35678 : exp.def = cur;
18925 : 114137 : dump (dumper::MACRO)
18926 : 0 : && dump ("Saving current #define %I", identifier (node));
18927 : : }
18928 : : }
18929 : :
18930 : : /* We're now done with the table. */
18931 : 171 : elf_in::release (slurp->from, sec);
18932 : :
18933 : 171 : dump.outdent ();
18934 : : }
18935 : :
18936 : : /* Import the transitive macros. */
18937 : :
18938 : : void
18939 : 835 : module_state::import_macros ()
18940 : : {
18941 : 835 : bitmap_ior_into (headers, slurp->headers);
18942 : :
18943 : 835 : bitmap_iterator bititer;
18944 : 835 : unsigned bitnum;
18945 : 1712 : EXECUTE_IF_SET_IN_BITMAP (slurp->headers, 0, bitnum, bititer)
18946 : 877 : (*modules)[bitnum]->install_macros ();
18947 : 835 : }
18948 : :
18949 : : /* NODE is being undefined at LOC. Record it in the export table, if
18950 : : necessary. */
18951 : :
18952 : : void
18953 : 173480 : module_state::undef_macro (cpp_reader *, location_t loc, cpp_hashnode *node)
18954 : : {
18955 : 173480 : if (!node->deferred)
18956 : : /* The macro is not imported, so our undef is irrelevant. */
18957 : : return;
18958 : :
18959 : 12 : unsigned n = dump.push (NULL);
18960 : :
18961 : 12 : macro_import::slot &slot = (*macro_imports)[node->deferred - 1].exported ();
18962 : 12 : macro_export &exp = get_macro_export (slot);
18963 : :
18964 : 12 : exp.undef_loc = loc;
18965 : 12 : slot.become_undef ();
18966 : 12 : exp.def = NULL;
18967 : :
18968 : 18 : dump (dumper::MACRO) && dump ("Recording macro #undef %I", identifier (node));
18969 : :
18970 : 12 : dump.pop (n);
18971 : : }
18972 : :
18973 : : /* NODE is a deferred macro node. Determine the definition and return
18974 : : it, with NULL if undefined. May issue diagnostics.
18975 : :
18976 : : This can leak memory, when merging declarations -- the string
18977 : : contents (TOKEN_FLD_STR) of each definition are allocated in
18978 : : unreclaimable cpp objstack. Only one will win. However, I do not
18979 : : expect this to be common -- mostly macros have a single point of
18980 : : definition. Perhaps we could restore the objstack to its position
18981 : : after the first imported definition (if that wins)? The macros
18982 : : themselves are GC'd. */
18983 : :
18984 : : cpp_macro *
18985 : 290 : module_state::deferred_macro (cpp_reader *reader, location_t loc,
18986 : : cpp_hashnode *node)
18987 : : {
18988 : 290 : macro_import &imports = (*macro_imports)[node->deferred - 1];
18989 : :
18990 : 290 : unsigned n = dump.push (NULL);
18991 : 296 : dump (dumper::MACRO) && dump ("Deferred macro %I", identifier (node));
18992 : :
18993 : 290 : bitmap visible (BITMAP_GGC_ALLOC ());
18994 : :
18995 : 290 : if (!((imports[0].get_defness () & macro_import::slot::L_UNDEF)
18996 : 0 : && !imports[0].get_module ()))
18997 : : {
18998 : : /* Calculate the set of visible header imports. */
18999 : 290 : bitmap_copy (visible, headers);
19000 : 743 : for (unsigned ix = imports.length (); ix--;)
19001 : : {
19002 : 453 : const macro_import::slot &slot = imports[ix];
19003 : 453 : unsigned mod = slot.get_module ();
19004 : 453 : if ((slot.get_defness () & macro_import::slot::L_UNDEF)
19005 : 453 : && bitmap_bit_p (visible, mod))
19006 : : {
19007 : 12 : bitmap arg = mod ? (*modules)[mod]->slurp->headers : headers;
19008 : 12 : bitmap_and_compl_into (visible, arg);
19009 : 12 : bitmap_set_bit (visible, mod);
19010 : : }
19011 : : }
19012 : : }
19013 : 290 : bitmap_set_bit (visible, 0);
19014 : :
19015 : : /* Now find the macros that are still visible. */
19016 : 290 : bool failed = false;
19017 : 290 : cpp_macro *def = NULL;
19018 : 290 : vec<macro_export> defs;
19019 : 290 : defs.create (imports.length ());
19020 : 743 : for (unsigned ix = imports.length (); ix--;)
19021 : : {
19022 : 453 : const macro_import::slot &slot = imports[ix];
19023 : 453 : unsigned mod = slot.get_module ();
19024 : 453 : if (bitmap_bit_p (visible, mod))
19025 : : {
19026 : 441 : macro_export *pushed = NULL;
19027 : 441 : if (mod)
19028 : : {
19029 : 320 : const module_state *imp = (*modules)[mod];
19030 : 320 : bytes_in &sec = imp->slurp->macro_defs;
19031 : 320 : if (!sec.get_overrun ())
19032 : : {
19033 : 320 : dump (dumper::MACRO)
19034 : 6 : && dump ("Reading macro %s%s%s %I module %M at %u",
19035 : 6 : slot.get_defness () & macro_import::slot::L_UNDEF
19036 : : ? "#undef" : "",
19037 : 6 : slot.get_defness () == macro_import::slot::L_BOTH
19038 : : ? " & " : "",
19039 : 6 : slot.get_defness () & macro_import::slot::L_DEF
19040 : : ? "#define" : "",
19041 : 6 : identifier (node), imp, slot.offset);
19042 : 320 : sec.random_access (slot.offset);
19043 : :
19044 : 320 : macro_export exp;
19045 : 320 : if (slot.get_defness () & macro_import::slot::L_UNDEF)
19046 : 12 : exp.undef_loc = imp->read_location (sec);
19047 : 320 : if (slot.get_defness () & macro_import::slot::L_DEF)
19048 : 314 : exp.def = imp->read_define (sec, reader);
19049 : 320 : if (sec.get_overrun ())
19050 : 0 : error_at (loc, "macro definitions of %qE corrupted",
19051 : 0 : imp->name);
19052 : : else
19053 : 320 : pushed = defs.quick_push (exp);
19054 : : }
19055 : : }
19056 : : else
19057 : 121 : pushed = defs.quick_push ((*macro_exports)[slot.offset]);
19058 : 441 : if (pushed && pushed->def)
19059 : : {
19060 : 435 : if (!def)
19061 : : def = pushed->def;
19062 : 148 : else if (cpp_compare_macros (def, pushed->def))
19063 : 453 : failed = true;
19064 : : }
19065 : : }
19066 : : }
19067 : :
19068 : 290 : if (failed)
19069 : : {
19070 : : /* If LOC is the first loc, this is the end of file check, which
19071 : : is a warning. */
19072 : 15 : auto_diagnostic_group d;
19073 : 15 : if (loc == MAP_START_LOCATION (LINEMAPS_ORDINARY_MAP_AT (line_table, 0)))
19074 : 9 : warning_at (loc, OPT_Winvalid_imported_macros,
19075 : : "inconsistent imported macro definition %qE",
19076 : : identifier (node));
19077 : : else
19078 : 6 : error_at (loc, "inconsistent imported macro definition %qE",
19079 : : identifier (node));
19080 : 60 : for (unsigned ix = defs.length (); ix--;)
19081 : : {
19082 : 30 : macro_export &exp = defs[ix];
19083 : 30 : if (exp.undef_loc)
19084 : 0 : inform (exp.undef_loc, "%<#undef %E%>", identifier (node));
19085 : 30 : if (exp.def)
19086 : 30 : inform (exp.def->line, "%<#define %s%>",
19087 : : cpp_macro_definition (reader, node, exp.def));
19088 : : }
19089 : 15 : def = NULL;
19090 : 15 : }
19091 : :
19092 : 290 : defs.release ();
19093 : :
19094 : 290 : dump.pop (n);
19095 : :
19096 : 290 : return def;
19097 : : }
19098 : :
19099 : : /* Stream the static aggregates. Sadly some headers (ahem:
19100 : : iostream) contain static vars, and rely on them to run global
19101 : : ctors. */
19102 : : unsigned
19103 : 838 : module_state::write_inits (elf_out *to, depset::hash &table, unsigned *crc_ptr)
19104 : : {
19105 : 838 : if (!static_aggregates && !tls_aggregates)
19106 : : return 0;
19107 : :
19108 : 42 : dump () && dump ("Writing initializers");
19109 : 42 : dump.indent ();
19110 : :
19111 : 42 : static_aggregates = nreverse (static_aggregates);
19112 : 42 : tls_aggregates = nreverse (tls_aggregates);
19113 : :
19114 : 42 : unsigned count = 0;
19115 : 42 : trees_out sec (to, this, table, ~0u);
19116 : 42 : sec.begin ();
19117 : :
19118 : 42 : tree list = static_aggregates;
19119 : 126 : for (int passes = 0; passes != 2; passes++)
19120 : : {
19121 : 230 : for (tree init = list; init; init = TREE_CHAIN (init))
19122 : 146 : if (TREE_LANG_FLAG_0 (init))
19123 : : {
19124 : 126 : if (STATIC_INIT_DECOMP_BASE_P (init))
19125 : : {
19126 : : /* Ensure that in the returned result chain if the
19127 : : STATIC_INIT_DECOMP_*BASE_P flags are set, there is
19128 : : always one or more STATIC_INIT_DECOMP_BASE_P TREE_LIST
19129 : : followed by one or more STATIC_INIT_DECOMP_NONBASE_P. */
19130 : 17 : int phase = 0;
19131 : 17 : tree last = NULL_TREE;
19132 : 17 : for (tree init2 = TREE_CHAIN (init);
19133 : 103 : init2; init2 = TREE_CHAIN (init2))
19134 : : {
19135 : 120 : if (phase == 0 && STATIC_INIT_DECOMP_BASE_P (init2))
19136 : : ;
19137 : 103 : else if (phase == 0
19138 : 120 : && STATIC_INIT_DECOMP_NONBASE_P (init2))
19139 : : {
19140 : 17 : phase = TREE_LANG_FLAG_0 (init2) ? 2 : 1;
19141 : : last = init2;
19142 : : }
19143 : 86 : else if (IN_RANGE (phase, 1, 2)
19144 : 172 : && STATIC_INIT_DECOMP_NONBASE_P (init2))
19145 : : {
19146 : 69 : if (TREE_LANG_FLAG_0 (init2))
19147 : 66 : phase = 2;
19148 : : last = init2;
19149 : : }
19150 : : else
19151 : : break;
19152 : : }
19153 : 17 : if (phase == 2)
19154 : : {
19155 : : /* In that case, add markers about it so that the
19156 : : STATIC_INIT_DECOMP_BASE_P and
19157 : : STATIC_INIT_DECOMP_NONBASE_P flags can be restored. */
19158 : 17 : sec.tree_node (build_int_cst (integer_type_node,
19159 : 17 : 2 * passes + 1));
19160 : 17 : phase = 1;
19161 : 120 : for (tree init2 = init; init2 != TREE_CHAIN (last);
19162 : 103 : init2 = TREE_CHAIN (init2))
19163 : 103 : if (TREE_LANG_FLAG_0 (init2))
19164 : : {
19165 : 83 : tree decl = TREE_VALUE (init2);
19166 : 83 : if (phase == 1
19167 : 83 : && STATIC_INIT_DECOMP_NONBASE_P (init2))
19168 : : {
19169 : 17 : sec.tree_node (build_int_cst (integer_type_node,
19170 : 17 : 2 * passes + 2));
19171 : 17 : phase = 2;
19172 : : }
19173 : 83 : dump ("Initializer:%u for %N", count, decl);
19174 : 83 : sec.tree_node (decl);
19175 : 83 : ++count;
19176 : : }
19177 : 17 : sec.tree_node (integer_zero_node);
19178 : 17 : init = last;
19179 : 17 : continue;
19180 : 17 : }
19181 : : }
19182 : :
19183 : 109 : tree decl = TREE_VALUE (init);
19184 : :
19185 : 109 : dump ("Initializer:%u for %N", count, decl);
19186 : 109 : sec.tree_node (decl);
19187 : 109 : ++count;
19188 : : }
19189 : :
19190 : 84 : list = tls_aggregates;
19191 : : }
19192 : :
19193 : 42 : sec.end (to, to->name (MOD_SNAME_PFX ".ini"), crc_ptr);
19194 : 42 : dump.outdent ();
19195 : :
19196 : 42 : return count;
19197 : 42 : }
19198 : :
19199 : : /* We have to defer some post-load processing until we've completed
19200 : : reading, because they can cause more reading. */
19201 : :
19202 : : static void
19203 : 7678 : post_load_processing ()
19204 : : {
19205 : : /* We mustn't cause a GC, our caller should have arranged for that
19206 : : not to happen. */
19207 : 7678 : gcc_checking_assert (function_depth);
19208 : :
19209 : 7678 : if (!post_load_decls)
19210 : : return;
19211 : :
19212 : 3941 : tree old_cfd = current_function_decl;
19213 : 3941 : struct function *old_cfun = cfun;
19214 : 9242 : while (post_load_decls->length ())
19215 : : {
19216 : 5301 : tree decl = post_load_decls->pop ();
19217 : :
19218 : 5345 : dump () && dump ("Post-load processing of %N", decl);
19219 : :
19220 : 5301 : gcc_checking_assert (DECL_MAYBE_IN_CHARGE_CDTOR_P (decl));
19221 : 5301 : expand_or_defer_fn (decl);
19222 : : /* As in module_state::read_cluster. */
19223 : 522 : if (at_eof && DECL_COMDAT (decl) && DECL_EXTERNAL (decl)
19224 : 5331 : && DECL_NOT_REALLY_EXTERN (decl))
19225 : 8 : DECL_EXTERNAL (decl) = false;
19226 : : }
19227 : :
19228 : 3941 : cfun = old_cfun;
19229 : 3941 : current_function_decl = old_cfd;
19230 : : }
19231 : :
19232 : : bool
19233 : 42 : module_state::read_inits (unsigned count)
19234 : : {
19235 : 42 : trees_in sec (this);
19236 : 42 : if (!sec.begin (loc, from (), from ()->find (MOD_SNAME_PFX ".ini")))
19237 : : return false;
19238 : 53 : dump () && dump ("Reading %u initializers", count);
19239 : 42 : dump.indent ();
19240 : :
19241 : 42 : lazy_snum = ~0u;
19242 : 42 : int decomp_phase = 0;
19243 : 42 : tree *aggrp = NULL;
19244 : 234 : for (unsigned ix = 0; ix != count; ix++)
19245 : : {
19246 : 192 : tree last = NULL_TREE;
19247 : 192 : if (decomp_phase)
19248 : 83 : last = *aggrp;
19249 : : /* Merely referencing the decl causes its initializer to be read
19250 : : and added to the correct list. */
19251 : 192 : tree decl = sec.tree_node ();
19252 : : /* module_state::write_inits can add special INTEGER_CST markers in
19253 : : between the decls. 1 means STATIC_INIT_DECOMP_BASE_P entries
19254 : : follow in static_aggregates, 2 means STATIC_INIT_DECOMP_NONBASE_P
19255 : : entries follow in static_aggregates, 3 means
19256 : : STATIC_INIT_DECOMP_BASE_P entries follow in tls_aggregates,
19257 : : 4 means STATIC_INIT_DECOMP_NONBASE_P follow in tls_aggregates,
19258 : : 0 means end of STATIC_INIT_DECOMP_{,NON}BASE_P sequence. */
19259 : 192 : if (tree_fits_shwi_p (decl))
19260 : : {
19261 : 51 : if (sec.get_overrun ())
19262 : : break;
19263 : 51 : decomp_phase = tree_to_shwi (decl);
19264 : 51 : if (decomp_phase)
19265 : : {
19266 : 34 : aggrp = decomp_phase > 2 ? &tls_aggregates : &static_aggregates;
19267 : : last = *aggrp;
19268 : : }
19269 : 51 : decl = sec.tree_node ();
19270 : : }
19271 : :
19272 : 192 : if (sec.get_overrun ())
19273 : : break;
19274 : 192 : if (decl)
19275 : 192 : dump ("Initializer:%u for %N", ix, decl);
19276 : 192 : if (decomp_phase)
19277 : : {
19278 : 83 : tree init = *aggrp;
19279 : 83 : gcc_assert (TREE_VALUE (init) == decl && TREE_CHAIN (init) == last);
19280 : 83 : if ((decomp_phase & 1) != 0)
19281 : 17 : STATIC_INIT_DECOMP_BASE_P (init) = 1;
19282 : : else
19283 : 66 : STATIC_INIT_DECOMP_NONBASE_P (init) = 1;
19284 : : }
19285 : : }
19286 : 42 : if (decomp_phase && !sec.get_overrun ())
19287 : : {
19288 : 0 : tree decl = sec.tree_node ();
19289 : 0 : gcc_assert (integer_zerop (decl));
19290 : : }
19291 : 42 : lazy_snum = 0;
19292 : 42 : post_load_processing ();
19293 : 42 : dump.outdent ();
19294 : 42 : if (!sec.end (from ()))
19295 : : return false;
19296 : : return true;
19297 : 42 : }
19298 : :
19299 : : void
19300 : 2302 : module_state::write_counts (elf_out *to, unsigned counts[MSC_HWM],
19301 : : unsigned *crc_ptr)
19302 : : {
19303 : 2302 : bytes_out cfg (to);
19304 : :
19305 : 2302 : cfg.begin ();
19306 : :
19307 : 20718 : for (unsigned ix = MSC_HWM; ix--;)
19308 : 18416 : cfg.u (counts[ix]);
19309 : :
19310 : 2302 : if (dump ())
19311 : : {
19312 : 248 : dump ("Cluster sections are [%u,%u)",
19313 : : counts[MSC_sec_lwm], counts[MSC_sec_hwm]);
19314 : 248 : dump ("Bindings %u", counts[MSC_bindings]);
19315 : 248 : dump ("Pendings %u", counts[MSC_pendings]);
19316 : 248 : dump ("Entities %u", counts[MSC_entities]);
19317 : 248 : dump ("Namespaces %u", counts[MSC_namespaces]);
19318 : 248 : dump ("Macros %u", counts[MSC_macros]);
19319 : 248 : dump ("Initializers %u", counts[MSC_inits]);
19320 : : }
19321 : :
19322 : 2302 : cfg.end (to, to->name (MOD_SNAME_PFX ".cnt"), crc_ptr);
19323 : 2302 : }
19324 : :
19325 : : bool
19326 : 2458 : module_state::read_counts (unsigned counts[MSC_HWM])
19327 : : {
19328 : 2458 : bytes_in cfg;
19329 : :
19330 : 2458 : if (!cfg.begin (loc, from (), MOD_SNAME_PFX ".cnt"))
19331 : : return false;
19332 : :
19333 : 22122 : for (unsigned ix = MSC_HWM; ix--;)
19334 : 19664 : counts[ix] = cfg.u ();
19335 : :
19336 : 2458 : if (dump ())
19337 : : {
19338 : 461 : dump ("Declaration sections are [%u,%u)",
19339 : : counts[MSC_sec_lwm], counts[MSC_sec_hwm]);
19340 : 461 : dump ("Bindings %u", counts[MSC_bindings]);
19341 : 461 : dump ("Pendings %u", counts[MSC_pendings]);
19342 : 461 : dump ("Entities %u", counts[MSC_entities]);
19343 : 461 : dump ("Namespaces %u", counts[MSC_namespaces]);
19344 : 461 : dump ("Macros %u", counts[MSC_macros]);
19345 : 461 : dump ("Initializers %u", counts[MSC_inits]);
19346 : : }
19347 : :
19348 : 2458 : return cfg.end (from ());
19349 : 2458 : }
19350 : :
19351 : : /* Tool configuration: MOD_SNAME_PFX .config
19352 : :
19353 : : This is data that confirms current state (or fails). */
19354 : :
19355 : : void
19356 : 2302 : module_state::write_config (elf_out *to, module_state_config &config,
19357 : : unsigned inner_crc)
19358 : : {
19359 : 2302 : bytes_out cfg (to);
19360 : :
19361 : 2302 : cfg.begin ();
19362 : :
19363 : : /* Write version and inner crc as u32 values, for easier
19364 : : debug inspection. */
19365 : 2550 : dump () && dump ("Writing version=%V, inner_crc=%x",
19366 : : MODULE_VERSION, inner_crc);
19367 : 2302 : cfg.u32 (unsigned (MODULE_VERSION));
19368 : 2302 : cfg.u32 (inner_crc);
19369 : :
19370 : 2302 : cfg.u (to->name (is_header () ? "" : get_flatname ()));
19371 : :
19372 : : /* Configuration. */
19373 : 2550 : dump () && dump ("Writing target='%s', host='%s'",
19374 : : TARGET_MACHINE, HOST_MACHINE);
19375 : 2302 : unsigned target = to->name (TARGET_MACHINE);
19376 : 2302 : unsigned host = (!strcmp (TARGET_MACHINE, HOST_MACHINE)
19377 : : ? target : to->name (HOST_MACHINE));
19378 : 2302 : cfg.u (target);
19379 : 2302 : cfg.u (host);
19380 : :
19381 : 2302 : cfg.str (config.dialect_str);
19382 : 2302 : cfg.u (extensions);
19383 : :
19384 : : /* Global tree information. We write the globals crc separately,
19385 : : rather than mix it directly into the overall crc, as it is used
19386 : : to ensure data match between instances of the compiler, not
19387 : : integrity of the file. */
19388 : 2550 : dump () && dump ("Writing globals=%u, crc=%x",
19389 : : fixed_trees->length (), global_crc);
19390 : 2302 : cfg.u (fixed_trees->length ());
19391 : 2302 : cfg.u32 (global_crc);
19392 : :
19393 : 2302 : if (is_partition ())
19394 : 152 : cfg.u (is_interface ());
19395 : :
19396 : 2302 : cfg.u (config.num_imports);
19397 : 2302 : cfg.u (config.num_partitions);
19398 : 2302 : cfg.u (config.num_entities);
19399 : :
19400 : 2302 : cfg.loc (config.ordinary_locs);
19401 : 2302 : cfg.loc (config.macro_locs);
19402 : 2302 : cfg.u (config.loc_range_bits);
19403 : :
19404 : 2302 : cfg.u (config.active_init);
19405 : :
19406 : : /* Now generate CRC, we'll have incorporated the inner CRC because
19407 : : of its serialization above. */
19408 : 2302 : cfg.end (to, to->name (MOD_SNAME_PFX ".cfg"), &crc);
19409 : 2550 : dump () && dump ("Writing CRC=%x", crc);
19410 : 2302 : }
19411 : :
19412 : : void
19413 : 37 : module_state::note_cmi_name ()
19414 : : {
19415 : 37 : if (!cmi_noted_p && filename)
19416 : : {
19417 : 37 : cmi_noted_p = true;
19418 : 37 : inform (loc, "compiled module file is %qs",
19419 : : maybe_add_cmi_prefix (filename));
19420 : : }
19421 : 37 : }
19422 : :
19423 : : bool
19424 : 2522 : module_state::read_config (module_state_config &config)
19425 : : {
19426 : 2522 : bytes_in cfg;
19427 : :
19428 : 2522 : if (!cfg.begin (loc, from (), MOD_SNAME_PFX ".cfg"))
19429 : : return false;
19430 : :
19431 : : /* Check version. */
19432 : 2522 : unsigned my_ver = MODULE_VERSION;
19433 : 2522 : unsigned their_ver = cfg.u32 ();
19434 : 2983 : dump () && dump (my_ver == their_ver ? "Version %V"
19435 : : : "Expecting %V found %V", my_ver, their_ver);
19436 : 2522 : if (their_ver != my_ver)
19437 : : {
19438 : : /* The compiler versions differ. Close enough? */
19439 : 0 : verstr_t my_string, their_string;
19440 : :
19441 : 0 : version2string (my_ver, my_string);
19442 : 0 : version2string (their_ver, their_string);
19443 : :
19444 : : /* Reject when either is non-experimental or when experimental
19445 : : major versions differ. */
19446 : 0 : auto_diagnostic_group d;
19447 : 0 : bool reject_p = ((!IS_EXPERIMENTAL (my_ver)
19448 : : || !IS_EXPERIMENTAL (their_ver)
19449 : 0 : || MODULE_MAJOR (my_ver) != MODULE_MAJOR (their_ver))
19450 : : /* The 'I know what I'm doing' switch. */
19451 : 0 : && !flag_module_version_ignore);
19452 : 0 : bool inform_p = true;
19453 : 0 : if (reject_p)
19454 : : {
19455 : 0 : cfg.set_overrun ();
19456 : 0 : error_at (loc, "compiled module is %sversion %s",
19457 : : IS_EXPERIMENTAL (their_ver) ? "experimental " : "",
19458 : : their_string);
19459 : : }
19460 : : else
19461 : 0 : inform_p = warning_at (loc, 0, "compiled module is %sversion %s",
19462 : : IS_EXPERIMENTAL (their_ver) ? "experimental " : "",
19463 : : their_string);
19464 : :
19465 : 0 : if (inform_p)
19466 : : {
19467 : 0 : inform (loc, "compiler is %sversion %s%s%s",
19468 : : IS_EXPERIMENTAL (my_ver) ? "experimental " : "",
19469 : : my_string,
19470 : 0 : reject_p ? "" : flag_module_version_ignore
19471 : 0 : ? ", be it on your own head!" : ", close enough?",
19472 : : reject_p ? "" : " \xc2\xaf\\_(\xe3\x83\x84)_/\xc2\xaf");
19473 : 0 : note_cmi_name ();
19474 : : }
19475 : :
19476 : 0 : if (reject_p)
19477 : 0 : goto done;
19478 : 0 : }
19479 : :
19480 : : /* We wrote the inner crc merely to merge it, so simply read it
19481 : : back and forget it. */
19482 : 2522 : cfg.u32 ();
19483 : :
19484 : : /* Check module name. */
19485 : 2522 : {
19486 : 2522 : const char *their_name = from ()->name (cfg.u ());
19487 : 2522 : const char *our_name = "";
19488 : :
19489 : 2522 : if (!is_header ())
19490 : 1597 : our_name = get_flatname ();
19491 : :
19492 : : /* Header units can be aliased, so name checking is
19493 : : inappropriate. */
19494 : 2522 : if (0 != strcmp (their_name, our_name))
19495 : : {
19496 : 0 : error_at (loc,
19497 : 0 : their_name[0] && our_name[0] ? G_("module %qs found")
19498 : : : their_name[0]
19499 : : ? G_("header module expected, module %qs found")
19500 : : : G_("module %qs expected, header module found"),
19501 : 0 : their_name[0] ? their_name : our_name);
19502 : 0 : cfg.set_overrun ();
19503 : 0 : goto done;
19504 : : }
19505 : : }
19506 : :
19507 : : /* Check the CRC after the above sanity checks, so that the user is
19508 : : clued in. */
19509 : 2522 : {
19510 : 2522 : unsigned e_crc = crc;
19511 : 2522 : crc = cfg.get_crc ();
19512 : 2983 : dump () && dump ("Reading CRC=%x", crc);
19513 : 2522 : if (!is_direct () && crc != e_crc)
19514 : : {
19515 : 3 : error_at (loc, "module %qs CRC mismatch", get_flatname ());
19516 : 3 : cfg.set_overrun ();
19517 : 3 : goto done;
19518 : : }
19519 : : }
19520 : :
19521 : : /* Check target & host. */
19522 : 2519 : {
19523 : 2519 : const char *their_target = from ()->name (cfg.u ());
19524 : 2519 : const char *their_host = from ()->name (cfg.u ());
19525 : 2980 : dump () && dump ("Read target='%s', host='%s'", their_target, their_host);
19526 : 2519 : if (strcmp (their_target, TARGET_MACHINE)
19527 : 2519 : || strcmp (their_host, HOST_MACHINE))
19528 : : {
19529 : 0 : error_at (loc, "target & host is %qs:%qs, expected %qs:%qs",
19530 : : their_target, TARGET_MACHINE, their_host, HOST_MACHINE);
19531 : 0 : cfg.set_overrun ();
19532 : 0 : goto done;
19533 : : }
19534 : : }
19535 : :
19536 : : /* Check compilation dialect. This must match. */
19537 : 2519 : {
19538 : 2519 : const char *their_dialect = cfg.str ();
19539 : 2519 : if (strcmp (their_dialect, config.dialect_str))
19540 : : {
19541 : 1 : error_at (loc, "language dialect differs %qs, expected %qs",
19542 : : their_dialect, config.dialect_str);
19543 : 1 : cfg.set_overrun ();
19544 : 1 : goto done;
19545 : : }
19546 : : }
19547 : :
19548 : : /* Check for extensions. If they set any, we must have them set
19549 : : too. */
19550 : 2518 : {
19551 : 2518 : unsigned ext = cfg.u ();
19552 : 2518 : unsigned allowed = (flag_openmp ? SE_OPENMP | SE_OPENMP_SIMD : 0);
19553 : 2518 : if (flag_openmp_simd)
19554 : 3 : allowed |= SE_OPENMP_SIMD;
19555 : 2518 : if (flag_openacc)
19556 : 3 : allowed |= SE_OPENACC;
19557 : :
19558 : 2518 : if (unsigned bad = ext & ~allowed)
19559 : : {
19560 : 9 : if (bad & SE_OPENMP)
19561 : 3 : error_at (loc, "module contains OpenMP, use %<-fopenmp%> to enable");
19562 : 6 : else if (bad & SE_OPENMP_SIMD)
19563 : 3 : error_at (loc, "module contains OpenMP, use %<-fopenmp%> or "
19564 : : "%<-fopenmp-simd%> to enable");
19565 : 9 : if (bad & SE_OPENACC)
19566 : 3 : error_at (loc, "module contains OpenACC, use %<-fopenacc%> to "
19567 : : "enable");
19568 : 9 : cfg.set_overrun ();
19569 : 9 : goto done;
19570 : : }
19571 : 2509 : extensions = ext;
19572 : : }
19573 : :
19574 : : /* Check global trees. */
19575 : 2509 : {
19576 : 2509 : unsigned their_fixed_length = cfg.u ();
19577 : 2509 : unsigned their_fixed_crc = cfg.u32 ();
19578 : 2970 : dump () && dump ("Read globals=%u, crc=%x",
19579 : : their_fixed_length, their_fixed_crc);
19580 : 2509 : if (!flag_preprocess_only
19581 : 2509 : && (their_fixed_length != fixed_trees->length ()
19582 : 2470 : || their_fixed_crc != global_crc))
19583 : : {
19584 : 0 : error_at (loc, "fixed tree mismatch");
19585 : 0 : cfg.set_overrun ();
19586 : 0 : goto done;
19587 : : }
19588 : : }
19589 : :
19590 : : /* All non-partitions are interfaces. */
19591 : 2509 : interface_p = !is_partition () || cfg.u ();
19592 : :
19593 : 2509 : config.num_imports = cfg.u ();
19594 : 2509 : config.num_partitions = cfg.u ();
19595 : 2509 : config.num_entities = cfg.u ();
19596 : :
19597 : 2509 : config.ordinary_locs = cfg.loc ();
19598 : 2509 : config.macro_locs = cfg.loc ();
19599 : 2509 : config.loc_range_bits = cfg.u ();
19600 : :
19601 : 2509 : config.active_init = cfg.u ();
19602 : :
19603 : 2522 : done:
19604 : 2522 : return cfg.end (from ());
19605 : 2522 : }
19606 : :
19607 : : /* Comparator for ordering the Ordered Ordinary Location array. */
19608 : :
19609 : : static int
19610 : 96 : ool_cmp (const void *a_, const void *b_)
19611 : : {
19612 : 96 : auto *a = *static_cast<const module_state *const *> (a_);
19613 : 96 : auto *b = *static_cast<const module_state *const *> (b_);
19614 : 96 : if (a == b)
19615 : : return 0;
19616 : 96 : else if (a->ordinary_locs.first < b->ordinary_locs.first)
19617 : : return -1;
19618 : : else
19619 : 38 : return +1;
19620 : : }
19621 : :
19622 : : /* Use ELROND format to record the following sections:
19623 : : qualified-names : binding value(s)
19624 : : MOD_SNAME_PFX.README : human readable, strings
19625 : : MOD_SNAME_PFX.ENV : environment strings, strings
19626 : : MOD_SNAME_PFX.nms : namespace hierarchy
19627 : : MOD_SNAME_PFX.bnd : binding table
19628 : : MOD_SNAME_PFX.spc : specialization table
19629 : : MOD_SNAME_PFX.imp : import table
19630 : : MOD_SNAME_PFX.ent : entity table
19631 : : MOD_SNAME_PFX.prt : partitions table
19632 : : MOD_SNAME_PFX.olm : ordinary line maps
19633 : : MOD_SNAME_PFX.mlm : macro line maps
19634 : : MOD_SNAME_PFX.def : macro definitions
19635 : : MOD_SNAME_PFX.mac : macro index
19636 : : MOD_SNAME_PFX.ini : inits
19637 : : MOD_SNAME_PFX.cnt : counts
19638 : : MOD_SNAME_PFX.cfg : config data
19639 : : */
19640 : :
19641 : : bool
19642 : 2327 : module_state::write_begin (elf_out *to, cpp_reader *reader,
19643 : : module_state_config &config, unsigned &crc)
19644 : : {
19645 : : /* Figure out remapped module numbers, which might elide
19646 : : partitions. */
19647 : 2327 : bitmap partitions = NULL;
19648 : 2327 : if (!is_header () && !is_partition ())
19649 : 1337 : partitions = BITMAP_GGC_ALLOC ();
19650 : 2327 : write_init_maps ();
19651 : :
19652 : 2327 : unsigned mod_hwm = 1;
19653 : 2864 : for (unsigned ix = 1; ix != modules->length (); ix++)
19654 : : {
19655 : 537 : module_state *imp = (*modules)[ix];
19656 : :
19657 : : /* Promote any non-partition direct import from a partition, unless
19658 : : we're a partition. */
19659 : 495 : if (!is_partition () && !imp->is_partition ()
19660 : 886 : && imp->is_partition_direct ())
19661 : 9 : imp->directness = MD_PURVIEW_DIRECT;
19662 : :
19663 : : /* Write any import that is not a partition, unless we're a
19664 : : partition. */
19665 : 537 : if (!partitions || !imp->is_partition ())
19666 : 391 : imp->remap = mod_hwm++;
19667 : : else
19668 : : {
19669 : 176 : dump () && dump ("Partition %M %u", imp, ix);
19670 : 146 : bitmap_set_bit (partitions, ix);
19671 : 146 : imp->remap = 0;
19672 : : /* All interface partitions must be exported. */
19673 : 146 : if (imp->is_interface () && !bitmap_bit_p (exports, imp->mod))
19674 : : {
19675 : 3 : error_at (imp->loc, "interface partition is not exported");
19676 : 3 : bitmap_set_bit (exports, imp->mod);
19677 : : }
19678 : :
19679 : : /* All the partition entities should have been loaded when
19680 : : loading the partition. */
19681 : : if (CHECKING_P)
19682 : 785 : for (unsigned jx = 0; jx != imp->entity_num; jx++)
19683 : : {
19684 : 639 : binding_slot *slot = &(*entity_ary)[imp->entity_lwm + jx];
19685 : 639 : gcc_checking_assert (!slot->is_lazy ());
19686 : : }
19687 : : }
19688 : :
19689 : 537 : if (imp->is_direct () && (imp->remap || imp->is_partition ()))
19690 : 523 : note_location (imp->imported_from ());
19691 : : }
19692 : :
19693 : 2327 : if (partitions && bitmap_empty_p (partitions))
19694 : : /* No partitions present. */
19695 : : partitions = nullptr;
19696 : :
19697 : : /* Find the set of decls we must write out. */
19698 : 2327 : depset::hash table (DECL_NAMESPACE_BINDINGS (global_namespace)->size () * 8);
19699 : : /* Add the specializations before the writables, so that we can
19700 : : detect injected friend specializations. */
19701 : 2327 : table.add_specializations (true);
19702 : 2327 : table.add_specializations (false);
19703 : 2327 : if (partial_specializations)
19704 : : {
19705 : 181 : table.add_partial_entities (partial_specializations);
19706 : 181 : partial_specializations = NULL;
19707 : : }
19708 : 2327 : table.add_namespace_entities (global_namespace, partitions);
19709 : 2327 : if (class_members)
19710 : : {
19711 : 12 : table.add_class_entities (class_members);
19712 : 12 : class_members = NULL;
19713 : : }
19714 : :
19715 : : /* Now join everything up. */
19716 : 2327 : table.find_dependencies (this);
19717 : :
19718 : 2327 : if (!table.finalize_dependencies ())
19719 : : return false;
19720 : :
19721 : : #if CHECKING_P
19722 : : /* We're done verifying at-most once reading, reset to verify
19723 : : at-most once writing. */
19724 : 2302 : note_defs = note_defs_table_t::create_ggc (1000);
19725 : : #endif
19726 : :
19727 : : /* Determine Strongly Connected Components. This will also strip any
19728 : : unnecessary dependencies on imported or TU-local entities. */
19729 : 2302 : vec<depset *> sccs = table.connect ();
19730 : :
19731 : 2302 : vec_alloc (ool, modules->length ());
19732 : 2836 : for (unsigned ix = modules->length (); --ix;)
19733 : : {
19734 : 534 : auto *import = (*modules)[ix];
19735 : 534 : if (import->loadedness > ML_NONE
19736 : 534 : && !(partitions && bitmap_bit_p (partitions, import->mod)))
19737 : 388 : ool->quick_push (import);
19738 : : }
19739 : 2302 : ool->qsort (ool_cmp);
19740 : :
19741 : 2302 : vec<cpp_hashnode *> *macros = nullptr;
19742 : 2302 : if (is_header ())
19743 : 838 : macros = prepare_macros (reader);
19744 : :
19745 : 2302 : config.num_imports = mod_hwm;
19746 : 2302 : config.num_partitions = modules->length () - mod_hwm;
19747 : 2302 : auto map_info = write_prepare_maps (&config, bool (config.num_partitions));
19748 : 2302 : unsigned counts[MSC_HWM];
19749 : 2302 : memset (counts, 0, sizeof (counts));
19750 : :
19751 : : /* depset::cluster is the cluster number,
19752 : : depset::section is unspecified scratch value.
19753 : :
19754 : : The following loops make use of the tarjan property that
19755 : : dependencies will be earlier in the SCCS array. */
19756 : :
19757 : : /* This first loop determines the number of depsets in each SCC, and
19758 : : also the number of namespaces we're dealing with. During the
19759 : : loop, the meaning of a couple of depset fields now change:
19760 : :
19761 : : depset::cluster -> size_of cluster, if first of cluster & !namespace
19762 : : depset::section -> section number of cluster (if !namespace). */
19763 : :
19764 : 2302 : unsigned n_spaces = 0;
19765 : 2302 : counts[MSC_sec_lwm] = counts[MSC_sec_hwm] = to->get_section_limit ();
19766 : 227272 : for (unsigned size, ix = 0; ix < sccs.length (); ix += size)
19767 : : {
19768 : 224970 : depset **base = &sccs[ix];
19769 : :
19770 : 425239 : if (base[0]->get_entity_kind () == depset::EK_NAMESPACE)
19771 : : {
19772 : 3929 : n_spaces++;
19773 : 3929 : size = 1;
19774 : : }
19775 : : else
19776 : : {
19777 : : /* Count the members in this cluster. */
19778 : 888087 : for (size = 1; ix + size < sccs.length (); size++)
19779 : 886070 : if (base[size]->cluster != base[0]->cluster)
19780 : : break;
19781 : :
19782 : 1109128 : for (unsigned jx = 0; jx != size; jx++)
19783 : : {
19784 : : /* Set the section number. */
19785 : 888087 : base[jx]->cluster = ~(~0u >> 1); /* A bad value. */
19786 : 888087 : base[jx]->section = counts[MSC_sec_hwm];
19787 : : }
19788 : :
19789 : : /* Save the size in the first member's cluster slot. */
19790 : 221041 : base[0]->cluster = size;
19791 : :
19792 : 221041 : counts[MSC_sec_hwm]++;
19793 : : }
19794 : : }
19795 : :
19796 : : /* Write the clusters. Namespace decls are put in the spaces array.
19797 : : The meaning of depset::cluster changes to provide the
19798 : : unnamed-decl count of the depset's decl (and remains zero for
19799 : : non-decls and non-unnamed). */
19800 : 2302 : unsigned bytes = 0;
19801 : 2302 : vec<depset *> spaces;
19802 : 2302 : spaces.create (n_spaces);
19803 : :
19804 : 227272 : for (unsigned size, ix = 0; ix < sccs.length (); ix += size)
19805 : : {
19806 : 224970 : depset **base = &sccs[ix];
19807 : :
19808 : 224970 : if (base[0]->get_entity_kind () == depset::EK_NAMESPACE)
19809 : : {
19810 : 3929 : tree decl = base[0]->get_entity ();
19811 : 3929 : if (decl == global_namespace)
19812 : 2066 : base[0]->cluster = 0;
19813 : 1863 : else if (!base[0]->is_import ())
19814 : : {
19815 : 1863 : base[0]->cluster = counts[MSC_entities]++;
19816 : 1863 : spaces.quick_push (base[0]);
19817 : 1863 : counts[MSC_namespaces]++;
19818 : 1863 : if (CHECKING_P)
19819 : : {
19820 : : /* Add it to the entity map, such that we can tell it is
19821 : : part of us. */
19822 : 1863 : bool existed;
19823 : 1863 : unsigned *slot = &entity_map->get_or_insert
19824 : 1863 : (DECL_UID (decl), &existed);
19825 : 1863 : if (existed)
19826 : : /* It must have come from a partition. */
19827 : 0 : gcc_checking_assert
19828 : : (import_entity_module (*slot)->is_partition ());
19829 : 1863 : *slot = ~base[0]->cluster;
19830 : : }
19831 : 226863 : dump (dumper::CLUSTER) && dump ("Cluster namespace %N", decl);
19832 : : }
19833 : : size = 1;
19834 : : }
19835 : : else
19836 : : {
19837 : 221041 : size = base[0]->cluster;
19838 : :
19839 : : /* Cluster is now used to number entities. */
19840 : 221041 : base[0]->cluster = ~(~0u >> 1); /* A bad value. */
19841 : :
19842 : 221041 : sort_cluster (&table, base, size);
19843 : :
19844 : : /* Record the section for consistency checking during stream
19845 : : out -- we don't want to start writing decls in different
19846 : : sections. */
19847 : 221041 : table.section = base[0]->section;
19848 : 221041 : bytes += write_cluster (to, base, size, table, counts, &crc);
19849 : 221041 : table.section = 0;
19850 : : }
19851 : : }
19852 : :
19853 : : /* depset::cluster - entity number (on entities)
19854 : : depset::section - cluster number */
19855 : : /* We'd better have written as many sections and found as many
19856 : : namespaces as we predicted. */
19857 : 4604 : gcc_assert (counts[MSC_sec_hwm] == to->get_section_limit ()
19858 : : && spaces.length () == counts[MSC_namespaces]);
19859 : :
19860 : : /* Write the entitites. None happens if we contain namespaces or
19861 : : nothing. */
19862 : 2302 : config.num_entities = counts[MSC_entities];
19863 : 2302 : if (counts[MSC_entities])
19864 : 2063 : write_entities (to, sccs, counts[MSC_entities], &crc);
19865 : :
19866 : : /* Write the namespaces. */
19867 : 2302 : if (counts[MSC_namespaces])
19868 : 436 : write_namespaces (to, spaces, counts[MSC_namespaces], &crc);
19869 : :
19870 : : /* Write the bindings themselves. */
19871 : 2302 : counts[MSC_bindings] = write_bindings (to, sccs, &crc);
19872 : :
19873 : : /* Write the unnamed. */
19874 : 2302 : counts[MSC_pendings] = write_pendings (to, sccs, table, &crc);
19875 : :
19876 : : /* Write the import table. */
19877 : 2302 : if (config.num_imports > 1)
19878 : 364 : write_imports (to, &crc);
19879 : :
19880 : : /* Write elided partition table. */
19881 : 2302 : if (config.num_partitions)
19882 : 104 : write_partitions (to, config.num_partitions, &crc);
19883 : :
19884 : : /* Write the line maps. */
19885 : 2302 : if (config.ordinary_locs)
19886 : 2202 : write_ordinary_maps (to, map_info, bool (config.num_partitions), &crc);
19887 : 2302 : if (config.macro_locs)
19888 : 112 : write_macro_maps (to, map_info, &crc);
19889 : :
19890 : 2302 : if (is_header ())
19891 : : {
19892 : 838 : counts[MSC_macros] = write_macros (to, macros, &crc);
19893 : 838 : counts[MSC_inits] = write_inits (to, table, &crc);
19894 : 838 : vec_free (macros);
19895 : : }
19896 : :
19897 : 2302 : unsigned clusters = counts[MSC_sec_hwm] - counts[MSC_sec_lwm];
19898 : 2302 : dump () && dump ("Wrote %u clusters, average %u bytes/cluster",
19899 : 248 : clusters, (bytes + clusters / 2) / (clusters + !clusters));
19900 : 2302 : trees_out::instrument ();
19901 : :
19902 : 2302 : write_counts (to, counts, &crc);
19903 : :
19904 : 2302 : spaces.release ();
19905 : 2302 : sccs.release ();
19906 : :
19907 : 2302 : vec_free (macro_loc_remap);
19908 : 2302 : vec_free (ord_loc_remap);
19909 : 2302 : vec_free (ool);
19910 : :
19911 : : // FIXME:QOI: Have a command line switch to control more detailed
19912 : : // information (which might leak data you do not want to leak).
19913 : : // Perhaps (some of) the write_readme contents should also be
19914 : : // so-controlled.
19915 : 2302 : if (false)
19916 : : write_env (to);
19917 : :
19918 : 2302 : return true;
19919 : 2327 : }
19920 : :
19921 : : // Finish module writing after we've emitted all dynamic initializers.
19922 : :
19923 : : void
19924 : 2302 : module_state::write_end (elf_out *to, cpp_reader *reader,
19925 : : module_state_config &config, unsigned &crc)
19926 : : {
19927 : : /* And finish up. */
19928 : 2302 : write_config (to, config, crc);
19929 : :
19930 : : /* Human-readable info. */
19931 : 2302 : write_readme (to, reader, config.dialect_str);
19932 : :
19933 : 2550 : dump () && dump ("Wrote %u sections", to->get_section_limit ());
19934 : 2302 : }
19935 : :
19936 : : /* Initial read of a CMI. Checks config, loads up imports and line
19937 : : maps. */
19938 : :
19939 : : bool
19940 : 2522 : module_state::read_initial (cpp_reader *reader)
19941 : : {
19942 : 2522 : module_state_config config;
19943 : 2522 : bool ok = true;
19944 : :
19945 : 2522 : if (ok && !from ()->begin (loc))
19946 : : ok = false;
19947 : :
19948 : 2522 : if (ok && !read_config (config))
19949 : : ok = false;
19950 : :
19951 : 2509 : bool have_locs = ok && read_prepare_maps (&config);
19952 : :
19953 : : /* Ordinary maps before the imports. */
19954 : 2509 : if (!(have_locs && config.ordinary_locs))
19955 : 77 : ordinary_locs.first = line_table->highest_location + 1;
19956 : 2445 : else if (!read_ordinary_maps (config.ordinary_locs, config.loc_range_bits))
19957 : : ok = false;
19958 : :
19959 : : /* Allocate the REMAP vector. */
19960 : 2522 : slurp->alloc_remap (config.num_imports);
19961 : :
19962 : 2522 : if (ok)
19963 : : {
19964 : : /* Read the import table. Decrement current to stop this CMI
19965 : : from being evicted during the import. */
19966 : 2509 : slurp->current--;
19967 : 2509 : if (config.num_imports > 1 && !read_imports (reader, line_table))
19968 : : ok = false;
19969 : 2509 : slurp->current++;
19970 : : }
19971 : :
19972 : : /* Read the elided partition table, if we're the primary partition. */
19973 : 2509 : if (ok && config.num_partitions && is_module ()
19974 : 2524 : && !read_partitions (config.num_partitions))
19975 : : ok = false;
19976 : :
19977 : : /* Determine the module's number. */
19978 : 2522 : gcc_checking_assert (mod == MODULE_UNKNOWN);
19979 : 2522 : gcc_checking_assert (this != (*modules)[0]);
19980 : :
19981 : 2522 : {
19982 : : /* Allocate space in the entities array now -- that array must be
19983 : : monotonically in step with the modules array. */
19984 : 2522 : entity_lwm = vec_safe_length (entity_ary);
19985 : 2522 : entity_num = config.num_entities;
19986 : 2522 : gcc_checking_assert (modules->length () == 1
19987 : : || modules->last ()->entity_lwm <= entity_lwm);
19988 : 2522 : vec_safe_reserve (entity_ary, config.num_entities);
19989 : :
19990 : 2522 : binding_slot slot;
19991 : 2522 : slot.u.binding = NULL_TREE;
19992 : 912442 : for (unsigned count = config.num_entities; count--;)
19993 : 909920 : entity_ary->quick_push (slot);
19994 : : }
19995 : :
19996 : : /* We'll run out of other resources before we run out of module
19997 : : indices. */
19998 : 2522 : mod = modules->length ();
19999 : 2522 : vec_safe_push (modules, this);
20000 : :
20001 : : /* We always import and export ourselves. */
20002 : 2522 : bitmap_set_bit (imports, mod);
20003 : 2522 : bitmap_set_bit (exports, mod);
20004 : :
20005 : 2522 : if (ok)
20006 : 2509 : (*slurp->remap)[0] = mod << 1;
20007 : 2983 : dump () && dump ("Assigning %M module number %u", this, mod);
20008 : :
20009 : : /* We should not have been frozen during the importing done by
20010 : : read_config. */
20011 : 2522 : gcc_assert (!from ()->is_frozen ());
20012 : :
20013 : : /* Macro maps after the imports. */
20014 : 2522 : if (!(ok && have_locs && config.macro_locs))
20015 : 2400 : macro_locs.first = LINEMAPS_MACRO_LOWEST_LOCATION (line_table);
20016 : 122 : else if (!read_macro_maps (config.macro_locs))
20017 : : ok = false;
20018 : :
20019 : : /* Note whether there's an active initializer. */
20020 : 2522 : active_init_p = !is_header () && bool (config.active_init);
20021 : :
20022 : 2522 : gcc_assert (slurp->current == ~0u);
20023 : 2522 : return ok;
20024 : : }
20025 : :
20026 : : /* Read a preprocessor state. */
20027 : :
20028 : : bool
20029 : 877 : module_state::read_preprocessor (bool outermost)
20030 : : {
20031 : 877 : gcc_checking_assert (is_header () && slurp
20032 : : && slurp->remap_module (0) == mod);
20033 : :
20034 : 877 : if (loadedness == ML_PREPROCESSOR)
20035 : 6 : return !(from () && from ()->get_error ());
20036 : :
20037 : 871 : bool ok = true;
20038 : :
20039 : : /* Read direct header imports. */
20040 : 871 : unsigned len = slurp->remap->length ();
20041 : 913 : for (unsigned ix = 1; ok && ix != len; ix++)
20042 : : {
20043 : 42 : unsigned map = (*slurp->remap)[ix];
20044 : 42 : if (map & 1)
20045 : : {
20046 : 42 : module_state *import = (*modules)[map >> 1];
20047 : 42 : if (import->is_header ())
20048 : : {
20049 : 42 : ok = import->read_preprocessor (false);
20050 : 42 : bitmap_ior_into (slurp->headers, import->slurp->headers);
20051 : : }
20052 : : }
20053 : : }
20054 : :
20055 : : /* Record as a direct header. */
20056 : 871 : if (ok)
20057 : 871 : bitmap_set_bit (slurp->headers, mod);
20058 : :
20059 : 871 : if (ok && !read_macros ())
20060 : : ok = false;
20061 : :
20062 : 871 : loadedness = ML_PREPROCESSOR;
20063 : 871 : announce ("macros");
20064 : :
20065 : 871 : if (flag_preprocess_only)
20066 : : /* We're done with the string table. */
20067 : 39 : from ()->release ();
20068 : :
20069 : 871 : return check_read (outermost, ok);
20070 : : }
20071 : :
20072 : : /* Read language state. */
20073 : :
20074 : : bool
20075 : 2523 : module_state::read_language (bool outermost)
20076 : : {
20077 : 2523 : gcc_checking_assert (!lazy_snum);
20078 : :
20079 : 2523 : if (loadedness == ML_LANGUAGE)
20080 : 65 : return !(slurp && from () && from ()->get_error ());
20081 : :
20082 : 2458 : gcc_checking_assert (slurp && slurp->current == ~0u
20083 : : && slurp->remap_module (0) == mod);
20084 : :
20085 : 2458 : bool ok = true;
20086 : :
20087 : : /* Read direct imports. */
20088 : 2458 : unsigned len = slurp->remap->length ();
20089 : 2773 : for (unsigned ix = 1; ok && ix != len; ix++)
20090 : : {
20091 : 315 : unsigned map = (*slurp->remap)[ix];
20092 : 315 : if (map & 1)
20093 : : {
20094 : 315 : module_state *import = (*modules)[map >> 1];
20095 : 315 : if (!import->read_language (false))
20096 : 315 : ok = false;
20097 : : }
20098 : : }
20099 : :
20100 : 2458 : unsigned counts[MSC_HWM];
20101 : :
20102 : 2458 : if (ok && !read_counts (counts))
20103 : : ok = false;
20104 : :
20105 : 2458 : function_depth++; /* Prevent unexpected GCs. */
20106 : :
20107 : 2458 : if (ok && counts[MSC_entities] != entity_num)
20108 : : ok = false;
20109 : 2458 : if (ok && counts[MSC_entities]
20110 : 2256 : && !read_entities (counts[MSC_entities],
20111 : : counts[MSC_sec_lwm], counts[MSC_sec_hwm]))
20112 : : ok = false;
20113 : :
20114 : : /* Read the namespace hierarchy. */
20115 : 2458 : if (ok && counts[MSC_namespaces]
20116 : 2896 : && !read_namespaces (counts[MSC_namespaces]))
20117 : : ok = false;
20118 : :
20119 : 2458 : if (ok && !read_bindings (counts[MSC_bindings],
20120 : : counts[MSC_sec_lwm], counts[MSC_sec_hwm]))
20121 : : ok = false;
20122 : :
20123 : : /* And unnamed. */
20124 : 2458 : if (ok && counts[MSC_pendings] && !read_pendings (counts[MSC_pendings]))
20125 : : ok = false;
20126 : :
20127 : 2458 : if (ok)
20128 : : {
20129 : 2458 : slurp->remaining = counts[MSC_sec_hwm] - counts[MSC_sec_lwm];
20130 : 2458 : available_clusters += counts[MSC_sec_hwm] - counts[MSC_sec_lwm];
20131 : : }
20132 : :
20133 : 2458 : if (!flag_module_lazy
20134 : 2458 : || (is_partition ()
20135 : 185 : && module_interface_p ()
20136 : 170 : && !module_partition_p ()))
20137 : : {
20138 : : /* Read the sections in forward order, so that dependencies are read
20139 : : first. See note about tarjan_connect. */
20140 : 458 : ggc_collect ();
20141 : :
20142 : 458 : lazy_snum = ~0u;
20143 : :
20144 : 458 : unsigned hwm = counts[MSC_sec_hwm];
20145 : 134847 : for (unsigned ix = counts[MSC_sec_lwm]; ok && ix != hwm; ix++)
20146 : 134389 : if (!load_section (ix, NULL))
20147 : : {
20148 : : ok = false;
20149 : : break;
20150 : : }
20151 : 458 : lazy_snum = 0;
20152 : 458 : post_load_processing ();
20153 : :
20154 : 458 : ggc_collect ();
20155 : :
20156 : 458 : if (ok && CHECKING_P)
20157 : 478275 : for (unsigned ix = 0; ix != entity_num; ix++)
20158 : 477817 : gcc_assert (!(*entity_ary)[ix + entity_lwm].is_lazy ());
20159 : : }
20160 : :
20161 : : // If the import is a header-unit, we need to register initializers
20162 : : // of any static objects it contains (looking at you _Ioinit).
20163 : : // Notice, the ordering of these initializers will be that of a
20164 : : // dynamic initializer at this point in the current TU. (Other
20165 : : // instances of these objects in other TUs will be initialized as
20166 : : // part of that TU's global initializers.)
20167 : 2458 : if (ok && counts[MSC_inits] && !read_inits (counts[MSC_inits]))
20168 : : ok = false;
20169 : :
20170 : 2458 : function_depth--;
20171 : :
20172 : 2770 : announce (flag_module_lazy ? "lazy" : "imported");
20173 : 2458 : loadedness = ML_LANGUAGE;
20174 : :
20175 : 2458 : gcc_assert (slurp->current == ~0u);
20176 : :
20177 : : /* We're done with the string table. */
20178 : 2458 : from ()->release ();
20179 : :
20180 : 2458 : return check_read (outermost, ok);
20181 : : }
20182 : :
20183 : : bool
20184 : 163652 : module_state::maybe_defrost ()
20185 : : {
20186 : 163652 : bool ok = true;
20187 : 163652 : if (from ()->is_frozen ())
20188 : : {
20189 : 9 : if (lazy_open >= lazy_limit)
20190 : 3 : freeze_an_elf ();
20191 : 18 : dump () && dump ("Defrosting '%s'", filename);
20192 : 9 : ok = from ()->defrost (maybe_add_cmi_prefix (filename));
20193 : 9 : lazy_open++;
20194 : : }
20195 : :
20196 : 163652 : return ok;
20197 : : }
20198 : :
20199 : : /* Load section SNUM, dealing with laziness. It doesn't matter if we
20200 : : have multiple concurrent loads, because we do not use TREE_VISITED
20201 : : when reading back in. */
20202 : :
20203 : : bool
20204 : 163652 : module_state::load_section (unsigned snum, binding_slot *mslot)
20205 : : {
20206 : 163652 : if (from ()->get_error ())
20207 : : return false;
20208 : :
20209 : 163652 : if (snum >= slurp->current)
20210 : 0 : from ()->set_error (elf::E_BAD_LAZY);
20211 : 163652 : else if (maybe_defrost ())
20212 : : {
20213 : 163652 : unsigned old_current = slurp->current;
20214 : 163652 : slurp->current = snum;
20215 : 163652 : slurp->lru = 0; /* Do not swap out. */
20216 : 163652 : slurp->remaining--;
20217 : 163652 : read_cluster (snum);
20218 : 163652 : slurp->lru = ++lazy_lru;
20219 : 163652 : slurp->current = old_current;
20220 : : }
20221 : :
20222 : 163652 : if (mslot && mslot->is_lazy ())
20223 : : {
20224 : : /* Oops, the section didn't set this slot. */
20225 : 0 : from ()->set_error (elf::E_BAD_DATA);
20226 : 0 : *mslot = NULL_TREE;
20227 : : }
20228 : :
20229 : 163652 : bool ok = !from ()->get_error ();
20230 : 163652 : if (!ok)
20231 : : {
20232 : 0 : error_at (loc, "failed to read compiled module cluster %u: %s",
20233 : 0 : snum, from ()->get_error (filename));
20234 : 0 : note_cmi_name ();
20235 : : }
20236 : :
20237 : 163652 : maybe_completed_reading ();
20238 : :
20239 : 163652 : return ok;
20240 : : }
20241 : :
20242 : : void
20243 : 169487 : module_state::maybe_completed_reading ()
20244 : : {
20245 : 169487 : if (loadedness == ML_LANGUAGE && slurp->current == ~0u && !slurp->remaining)
20246 : : {
20247 : 2053 : lazy_open--;
20248 : : /* We no longer need the macros, all tokenizing has been done. */
20249 : 2053 : slurp->release_macros ();
20250 : :
20251 : 2053 : from ()->end ();
20252 : 2053 : slurp->close ();
20253 : 2053 : slurped ();
20254 : : }
20255 : 169487 : }
20256 : :
20257 : : /* After a reading operation, make sure things are still ok. If not,
20258 : : emit an error and clean up. */
20259 : :
20260 : : bool
20261 : 5869 : module_state::check_read (bool outermost, bool ok)
20262 : : {
20263 : 5869 : gcc_checking_assert (!outermost || slurp->current == ~0u);
20264 : :
20265 : 5869 : if (!ok)
20266 : 13 : from ()->set_error ();
20267 : :
20268 : 5869 : if (int e = from ()->get_error ())
20269 : : {
20270 : 37 : auto_diagnostic_group d;
20271 : 37 : error_at (loc, "failed to read compiled module: %s",
20272 : 37 : from ()->get_error (filename));
20273 : 37 : note_cmi_name ();
20274 : :
20275 : 37 : if (e == EMFILE
20276 : 37 : || e == ENFILE
20277 : : #if MAPPED_READING
20278 : 37 : || e == ENOMEM
20279 : : #endif
20280 : : || false)
20281 : 0 : inform (loc, "consider using %<-fno-module-lazy%>,"
20282 : : " increasing %<-param-lazy-modules=%u%> value,"
20283 : : " or increasing the per-process file descriptor limit",
20284 : : param_lazy_modules);
20285 : 37 : else if (e == ENOENT)
20286 : 18 : inform (loc, "imports must be built before being imported");
20287 : :
20288 : 37 : if (outermost)
20289 : 34 : fatal_error (loc, "returning to the gate for a mechanical issue");
20290 : :
20291 : 3 : ok = false;
20292 : 3 : }
20293 : :
20294 : 5835 : maybe_completed_reading ();
20295 : :
20296 : 5835 : return ok;
20297 : : }
20298 : :
20299 : : /* Return the IDENTIFIER_NODE naming module IX. This is the name
20300 : : including dots. */
20301 : :
20302 : : char const *
20303 : 270 : module_name (unsigned ix, bool header_ok)
20304 : : {
20305 : 270 : if (modules)
20306 : : {
20307 : 270 : module_state *imp = (*modules)[ix];
20308 : :
20309 : 270 : if (ix && !imp->name)
20310 : 0 : imp = imp->parent;
20311 : :
20312 : 270 : if (header_ok || !imp->is_header ())
20313 : 270 : return imp->get_flatname ();
20314 : : }
20315 : :
20316 : : return NULL;
20317 : : }
20318 : :
20319 : : /* Return the bitmap describing what modules are imported. Remember,
20320 : : we always import ourselves. */
20321 : :
20322 : : bitmap
20323 : 68317 : get_import_bitmap ()
20324 : : {
20325 : 68317 : return (*modules)[0]->imports;
20326 : : }
20327 : :
20328 : : /* Return the visible imports and path of instantiation for an
20329 : : instantiation at TINST. If TINST is nullptr, we're not in an
20330 : : instantiation, and thus will return the visible imports of the
20331 : : current TU (and NULL *PATH_MAP_P). We cache the information on
20332 : : the tinst level itself. */
20333 : :
20334 : : static bitmap
20335 : 90051 : path_of_instantiation (tinst_level *tinst, bitmap *path_map_p)
20336 : : {
20337 : 90051 : gcc_checking_assert (modules_p ());
20338 : :
20339 : 90051 : if (!tinst)
20340 : : {
20341 : : /* Not inside an instantiation, just the regular case. */
20342 : 41260 : *path_map_p = nullptr;
20343 : 41260 : return get_import_bitmap ();
20344 : : }
20345 : :
20346 : 48791 : if (!tinst->path)
20347 : : {
20348 : : /* Calculate. */
20349 : 13461 : bitmap visible = path_of_instantiation (tinst->next, path_map_p);
20350 : 13461 : bitmap path_map = *path_map_p;
20351 : :
20352 : 13461 : if (!path_map)
20353 : : {
20354 : 2084 : path_map = BITMAP_GGC_ALLOC ();
20355 : 2084 : bitmap_set_bit (path_map, 0);
20356 : : }
20357 : :
20358 : 13461 : tree decl = tinst->tldcl;
20359 : 13461 : if (TREE_CODE (decl) == TREE_LIST)
20360 : 0 : decl = TREE_PURPOSE (decl);
20361 : 13461 : if (TYPE_P (decl))
20362 : 1626 : decl = TYPE_NAME (decl);
20363 : :
20364 : 13461 : if (unsigned mod = get_originating_module (decl))
20365 : 1663 : if (!bitmap_bit_p (path_map, mod))
20366 : : {
20367 : : /* This is brand new information! */
20368 : 73 : bitmap new_path = BITMAP_GGC_ALLOC ();
20369 : 73 : bitmap_copy (new_path, path_map);
20370 : 73 : bitmap_set_bit (new_path, mod);
20371 : 73 : path_map = new_path;
20372 : :
20373 : 73 : bitmap imports = (*modules)[mod]->imports;
20374 : 73 : if (bitmap_intersect_compl_p (imports, visible))
20375 : : {
20376 : : /* IMPORTS contains additional modules to VISIBLE. */
20377 : 6 : bitmap new_visible = BITMAP_GGC_ALLOC ();
20378 : :
20379 : 6 : bitmap_ior (new_visible, visible, imports);
20380 : 6 : visible = new_visible;
20381 : : }
20382 : : }
20383 : :
20384 : 13461 : tinst->path = path_map;
20385 : 13461 : tinst->visible = visible;
20386 : : }
20387 : :
20388 : 48791 : *path_map_p = tinst->path;
20389 : 48791 : return tinst->visible;
20390 : : }
20391 : :
20392 : : /* Return the bitmap describing what modules are visible along the
20393 : : path of instantiation. If we're not an instantiation, this will be
20394 : : the visible imports of the TU. *PATH_MAP_P is filled in with the
20395 : : modules owning the instantiation path -- we see the module-linkage
20396 : : entities of those modules. */
20397 : :
20398 : : bitmap
20399 : 19649212 : visible_instantiation_path (bitmap *path_map_p)
20400 : : {
20401 : 19649212 : if (!modules_p ())
20402 : : return NULL;
20403 : :
20404 : 76590 : return path_of_instantiation (current_instantiation (), path_map_p);
20405 : : }
20406 : :
20407 : : /* We've just directly imported IMPORT. Update our import/export
20408 : : bitmaps. IS_EXPORT is true if we're reexporting the OTHER. */
20409 : :
20410 : : void
20411 : 2584 : module_state::set_import (module_state const *import, bool is_export)
20412 : : {
20413 : 2584 : gcc_checking_assert (this != import);
20414 : :
20415 : : /* We see IMPORT's exports (which includes IMPORT). If IMPORT is
20416 : : the primary interface or a partition we'll see its imports. */
20417 : 2584 : bitmap_ior_into (imports, import->is_module () || import->is_partition ()
20418 : : ? import->imports : import->exports);
20419 : :
20420 : 2584 : if (is_export)
20421 : : /* We'll export OTHER's exports. */
20422 : 385 : bitmap_ior_into (exports, import->exports);
20423 : 2584 : }
20424 : :
20425 : : /* Return the declaring entity of DECL. That is the decl determining
20426 : : how to decorate DECL with module information. Returns NULL_TREE if
20427 : : it's the global module. */
20428 : :
20429 : : tree
20430 : 156869051 : get_originating_module_decl (tree decl)
20431 : : {
20432 : : /* An enumeration constant. */
20433 : 156869051 : if (TREE_CODE (decl) == CONST_DECL
20434 : 5859 : && DECL_CONTEXT (decl)
20435 : 156874910 : && (TREE_CODE (DECL_CONTEXT (decl)) == ENUMERAL_TYPE))
20436 : 5857 : decl = TYPE_NAME (DECL_CONTEXT (decl));
20437 : 156863194 : else if (TREE_CODE (decl) == FIELD_DECL
20438 : 156854947 : || TREE_CODE (decl) == USING_DECL
20439 : 313711247 : || CONST_DECL_USING_P (decl))
20440 : : {
20441 : 15143 : decl = DECL_CONTEXT (decl);
20442 : 15143 : if (TREE_CODE (decl) != FUNCTION_DECL)
20443 : 15143 : decl = TYPE_NAME (decl);
20444 : : }
20445 : :
20446 : 156869051 : gcc_checking_assert (TREE_CODE (decl) == TEMPLATE_DECL
20447 : : || TREE_CODE (decl) == FUNCTION_DECL
20448 : : || TREE_CODE (decl) == TYPE_DECL
20449 : : || TREE_CODE (decl) == VAR_DECL
20450 : : || TREE_CODE (decl) == CONCEPT_DECL
20451 : : || TREE_CODE (decl) == NAMESPACE_DECL);
20452 : :
20453 : 157788551 : for (;;)
20454 : : {
20455 : : /* Uninstantiated template friends are owned by the befriending
20456 : : class -- not their context. */
20457 : 157328801 : if (TREE_CODE (decl) == TEMPLATE_DECL
20458 : 157328801 : && DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (decl))
20459 : 4939 : decl = TYPE_NAME (DECL_CHAIN (decl));
20460 : :
20461 : : /* An imported temploid friend is attached to the same module the
20462 : : befriending class was. */
20463 : 157328801 : if (imported_temploid_friends)
20464 : 1831878 : if (tree *slot = imported_temploid_friends->get (decl))
20465 : 305 : decl = *slot;
20466 : :
20467 : 157328801 : int use;
20468 : 157328801 : if (tree ti = node_template_info (decl, use))
20469 : : {
20470 : 378158 : decl = TI_TEMPLATE (ti);
20471 : 378158 : if (TREE_CODE (decl) != TEMPLATE_DECL)
20472 : : {
20473 : : /* A friend template specialization. */
20474 : 7 : gcc_checking_assert (OVL_P (decl));
20475 : 7 : return global_namespace;
20476 : : }
20477 : : }
20478 : : else
20479 : : {
20480 : 156950643 : tree ctx = CP_DECL_CONTEXT (decl);
20481 : 156950643 : if (TREE_CODE (ctx) == NAMESPACE_DECL)
20482 : : break;
20483 : :
20484 : 81599 : if (TYPE_P (ctx))
20485 : : {
20486 : 75691 : ctx = TYPE_NAME (ctx);
20487 : 75691 : if (!ctx)
20488 : : {
20489 : : /* Some kind of internal type. */
20490 : 0 : gcc_checking_assert (DECL_ARTIFICIAL (decl));
20491 : 0 : return global_namespace;
20492 : : }
20493 : : }
20494 : 81599 : decl = ctx;
20495 : : }
20496 : 459750 : }
20497 : :
20498 : 156869044 : return decl;
20499 : : }
20500 : :
20501 : : /* If DECL is imported, return which module imported it, or 0 for the current
20502 : : module. Except that if GLOBAL_M1, return -1 for decls attached to the
20503 : : global module. */
20504 : :
20505 : : int
20506 : 637524 : get_originating_module (tree decl, bool global_m1)
20507 : : {
20508 : 637524 : tree owner = get_originating_module_decl (decl);
20509 : 637524 : tree not_tmpl = STRIP_TEMPLATE (owner);
20510 : :
20511 : 637524 : if (!DECL_LANG_SPECIFIC (not_tmpl))
20512 : 148444 : return global_m1 ? -1 : 0;
20513 : :
20514 : 949457 : if (global_m1 && !DECL_MODULE_ATTACH_P (not_tmpl))
20515 : : return -1;
20516 : :
20517 : 84471 : int mod = !DECL_MODULE_IMPORT_P (not_tmpl) ? 0 : get_importing_module (owner);
20518 : 84471 : gcc_checking_assert (!global_m1 || !(*modules)[mod]->is_header ());
20519 : : return mod;
20520 : : }
20521 : :
20522 : : /* DECL is imported, return which module imported it.
20523 : : If FLEXIBLE, return -1 if not found, otherwise checking ICE. */
20524 : :
20525 : : unsigned
20526 : 14935 : get_importing_module (tree decl, bool flexible)
20527 : : {
20528 : 14935 : unsigned index = import_entity_index (decl, flexible);
20529 : 14935 : if (index == ~(~0u >> 1))
20530 : : return -1;
20531 : 14935 : module_state *module = import_entity_module (index);
20532 : :
20533 : 14935 : return module->mod;
20534 : : }
20535 : :
20536 : : /* Is it permissible to redeclare OLDDECL with NEWDECL.
20537 : :
20538 : : If NEWDECL is NULL, assumes that OLDDECL will be redeclared using
20539 : : the current scope's module and attachment. */
20540 : :
20541 : : bool
20542 : 136126 : module_may_redeclare (tree olddecl, tree newdecl)
20543 : : {
20544 : 136126 : tree decl = olddecl;
20545 : 151086 : for (;;)
20546 : : {
20547 : 143606 : tree ctx = CP_DECL_CONTEXT (decl);
20548 : 143606 : if (TREE_CODE (ctx) == NAMESPACE_DECL)
20549 : : // Found the namespace-scope decl.
20550 : : break;
20551 : 14166 : if (!CLASS_TYPE_P (ctx))
20552 : : // We've met a non-class scope. Such a thing is not
20553 : : // reopenable, so we must be ok.
20554 : : return true;
20555 : 7480 : decl = TYPE_NAME (ctx);
20556 : 7480 : }
20557 : :
20558 : 129440 : int use_tpl = 0;
20559 : 129440 : if (node_template_info (STRIP_TEMPLATE (decl), use_tpl) && use_tpl)
20560 : : // Specializations of any kind can be redeclared anywhere.
20561 : : // FIXME: Should we be checking this in more places on the scope chain?
20562 : : return true;
20563 : :
20564 : 86464 : module_state *old_mod = (*modules)[0];
20565 : 86464 : module_state *new_mod = old_mod;
20566 : :
20567 : 86464 : tree old_origin = get_originating_module_decl (decl);
20568 : 86464 : tree old_inner = STRIP_TEMPLATE (old_origin);
20569 : 86464 : bool olddecl_attached_p = (DECL_LANG_SPECIFIC (old_inner)
20570 : 138344 : && DECL_MODULE_ATTACH_P (old_inner));
20571 : 138344 : if (DECL_LANG_SPECIFIC (old_inner) && DECL_MODULE_IMPORT_P (old_inner))
20572 : : {
20573 : 402 : unsigned index = import_entity_index (old_origin);
20574 : 402 : old_mod = import_entity_module (index);
20575 : : }
20576 : :
20577 : 86464 : bool newdecl_attached_p = module_attach_p ();
20578 : 86464 : if (newdecl)
20579 : : {
20580 : 23553 : tree new_origin = get_originating_module_decl (newdecl);
20581 : 23553 : tree new_inner = STRIP_TEMPLATE (new_origin);
20582 : 23553 : newdecl_attached_p = (DECL_LANG_SPECIFIC (new_inner)
20583 : 45948 : && DECL_MODULE_ATTACH_P (new_inner));
20584 : 45948 : if (DECL_LANG_SPECIFIC (new_inner) && DECL_MODULE_IMPORT_P (new_inner))
20585 : : {
20586 : 133 : unsigned index = import_entity_index (new_origin);
20587 : 133 : new_mod = import_entity_module (index);
20588 : : }
20589 : : }
20590 : :
20591 : : /* Module attachment needs to match. */
20592 : 86464 : if (olddecl_attached_p == newdecl_attached_p)
20593 : : {
20594 : 86422 : if (!olddecl_attached_p)
20595 : : /* Both are GM entities, OK. */
20596 : : return true;
20597 : :
20598 : 1072 : if (new_mod == old_mod
20599 : 1318 : || (new_mod && get_primary (new_mod) == get_primary (old_mod)))
20600 : : /* Both attached to same named module, OK. */
20601 : : return true;
20602 : : }
20603 : :
20604 : : /* Attached to different modules, error. */
20605 : 45 : decl = newdecl ? newdecl : olddecl;
20606 : 45 : location_t loc = newdecl ? DECL_SOURCE_LOCATION (newdecl) : input_location;
20607 : 45 : if (DECL_IS_UNDECLARED_BUILTIN (olddecl))
20608 : : {
20609 : 3 : if (newdecl_attached_p)
20610 : 3 : error_at (loc, "declaring %qD in module %qs conflicts with builtin "
20611 : : "in global module", decl, new_mod->get_flatname ());
20612 : : else
20613 : 0 : error_at (loc, "declaration %qD conflicts with builtin", decl);
20614 : : }
20615 : 81 : else if (DECL_LANG_SPECIFIC (old_inner) && DECL_MODULE_IMPORT_P (old_inner))
20616 : : {
20617 : 27 : auto_diagnostic_group d;
20618 : 27 : if (newdecl_attached_p)
20619 : 3 : error_at (loc, "redeclaring %qD in module %qs conflicts with import",
20620 : : decl, new_mod->get_flatname ());
20621 : : else
20622 : 24 : error_at (loc, "redeclaring %qD in global module conflicts with import",
20623 : : decl);
20624 : :
20625 : 27 : if (olddecl_attached_p)
20626 : 27 : inform (DECL_SOURCE_LOCATION (olddecl),
20627 : : "import declared attached to module %qs",
20628 : : old_mod->get_flatname ());
20629 : : else
20630 : 0 : inform (DECL_SOURCE_LOCATION (olddecl),
20631 : : "import declared in global module");
20632 : 27 : }
20633 : : else
20634 : : {
20635 : 15 : auto_diagnostic_group d;
20636 : 15 : if (newdecl_attached_p)
20637 : 15 : error_at (loc, "conflicting declaration of %qD in module %qs",
20638 : : decl, new_mod->get_flatname ());
20639 : : else
20640 : 0 : error_at (loc, "conflicting declaration of %qD in global module",
20641 : : decl);
20642 : :
20643 : 15 : if (olddecl_attached_p)
20644 : 0 : inform (DECL_SOURCE_LOCATION (olddecl),
20645 : : "previously declared in module %qs",
20646 : : old_mod->get_flatname ());
20647 : : else
20648 : 15 : inform (DECL_SOURCE_LOCATION (olddecl),
20649 : : "previously declared in global module");
20650 : 15 : }
20651 : : return false;
20652 : : }
20653 : :
20654 : : /* DECL is being created by this TU. Record it came from here. We
20655 : : record module purview, so we can see if partial or explicit
20656 : : specialization needs to be written out, even though its purviewness
20657 : : comes from the most general template. */
20658 : :
20659 : : void
20660 : 743090459 : set_instantiating_module (tree decl)
20661 : : {
20662 : 743090459 : gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
20663 : : || VAR_P (decl)
20664 : : || TREE_CODE (decl) == TYPE_DECL
20665 : : || TREE_CODE (decl) == CONCEPT_DECL
20666 : : || TREE_CODE (decl) == TEMPLATE_DECL
20667 : : || TREE_CODE (decl) == CONST_DECL
20668 : : || (TREE_CODE (decl) == NAMESPACE_DECL
20669 : : && DECL_NAMESPACE_ALIAS (decl)));
20670 : :
20671 : 743090459 : if (!modules_p ())
20672 : : return;
20673 : :
20674 : 2348386 : decl = STRIP_TEMPLATE (decl);
20675 : :
20676 : 2348386 : if (!DECL_LANG_SPECIFIC (decl) && module_purview_p ())
20677 : 298602 : retrofit_lang_decl (decl);
20678 : :
20679 : 2348386 : if (DECL_LANG_SPECIFIC (decl))
20680 : : {
20681 : 1857617 : DECL_MODULE_PURVIEW_P (decl) = module_purview_p ();
20682 : : /* If this was imported, we'll still be in the entity_hash. */
20683 : 1857617 : DECL_MODULE_IMPORT_P (decl) = false;
20684 : : }
20685 : : }
20686 : :
20687 : : /* If DECL is a class member, whose class is not defined in this TU
20688 : : (it was imported), remember this decl. */
20689 : :
20690 : : void
20691 : 116812 : set_defining_module (tree decl)
20692 : : {
20693 : 116812 : gcc_checking_assert (!DECL_LANG_SPECIFIC (decl)
20694 : : || !DECL_MODULE_IMPORT_P (decl));
20695 : :
20696 : 116812 : if (module_maybe_has_cmi_p ())
20697 : : {
20698 : : /* We need to track all declarations within a module, not just those
20699 : : in the module purview, because we don't necessarily know yet if
20700 : : this module will require a CMI while in the global fragment. */
20701 : 78514 : tree ctx = DECL_CONTEXT (decl);
20702 : 78514 : if (ctx
20703 : 78514 : && (TREE_CODE (ctx) == RECORD_TYPE || TREE_CODE (ctx) == UNION_TYPE)
20704 : 13308 : && DECL_LANG_SPECIFIC (TYPE_NAME (ctx))
20705 : 88457 : && DECL_MODULE_IMPORT_P (TYPE_NAME (ctx)))
20706 : : {
20707 : : /* This entity's context is from an import. We may need to
20708 : : record this entity to make sure we emit it in the CMI.
20709 : : Template specializations are in the template hash tables,
20710 : : so we don't need to record them here as well. */
20711 : 12 : int use_tpl = -1;
20712 : 12 : tree ti = node_template_info (decl, use_tpl);
20713 : 12 : if (use_tpl <= 0)
20714 : : {
20715 : 12 : if (ti)
20716 : : {
20717 : 3 : gcc_checking_assert (!use_tpl);
20718 : : /* Get to the TEMPLATE_DECL. */
20719 : 3 : decl = TI_TEMPLATE (ti);
20720 : : }
20721 : :
20722 : : /* Record it on the class_members list. */
20723 : 12 : vec_safe_push (class_members, decl);
20724 : : }
20725 : : }
20726 : : }
20727 : 116812 : }
20728 : :
20729 : : /* Also remember DECL if it's a newly declared class template partial
20730 : : specialization, because these are not necessarily added to the
20731 : : instantiation tables. */
20732 : :
20733 : : void
20734 : 7013295 : set_defining_module_for_partial_spec (tree decl)
20735 : : {
20736 : 7013295 : if (module_maybe_has_cmi_p ()
20737 : 19441 : && DECL_IMPLICIT_TYPEDEF_P (decl)
20738 : 7030312 : && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
20739 : 17017 : vec_safe_push (partial_specializations, decl);
20740 : 7013295 : }
20741 : :
20742 : : void
20743 : 261228049 : set_originating_module (tree decl, bool friend_p ATTRIBUTE_UNUSED)
20744 : : {
20745 : 261228049 : set_instantiating_module (decl);
20746 : :
20747 : 261228049 : if (!DECL_NAMESPACE_SCOPE_P (decl))
20748 : : return;
20749 : :
20750 : 159680522 : gcc_checking_assert (friend_p || decl == get_originating_module_decl (decl));
20751 : :
20752 : 159680522 : if (module_attach_p ())
20753 : : {
20754 : 3683 : retrofit_lang_decl (decl);
20755 : 3683 : DECL_MODULE_ATTACH_P (decl) = true;
20756 : : }
20757 : :
20758 : : /* It is ill-formed to export a declaration with internal linkage. However,
20759 : : at the point this function is called we don't yet always know whether this
20760 : : declaration has internal linkage; instead we defer this check for callers
20761 : : to do once visibility has been determined. */
20762 : 159680522 : if (module_exporting_p ())
20763 : 192304 : DECL_MODULE_EXPORT_P (decl) = true;
20764 : : }
20765 : :
20766 : : /* Checks whether DECL within a module unit has valid linkage for its kind.
20767 : : Must be called after visibility for DECL has been finalised. */
20768 : :
20769 : : void
20770 : 321101214 : check_module_decl_linkage (tree decl)
20771 : : {
20772 : 321101214 : if (!module_has_cmi_p ())
20773 : : return;
20774 : :
20775 : : /* A header unit shall not contain a definition of a non-inline function
20776 : : or variable (not template) whose name has external linkage. */
20777 : 429779 : if (header_module_p ()
20778 : 383636 : && !processing_template_decl
20779 : 119725 : && ((TREE_CODE (decl) == FUNCTION_DECL
20780 : 72066 : && !DECL_DECLARED_INLINE_P (decl))
20781 : 88353 : || (TREE_CODE (decl) == VAR_DECL
20782 : 22999 : && !DECL_INLINE_VAR_P (decl)))
20783 : 39331 : && decl_defined_p (decl)
20784 : 3495 : && !(DECL_LANG_SPECIFIC (decl)
20785 : 3495 : && DECL_TEMPLATE_INSTANTIATION (decl))
20786 : 433274 : && decl_linkage (decl) == lk_external)
20787 : 60 : error_at (DECL_SOURCE_LOCATION (decl),
20788 : : "external linkage definition of %qD in header module must "
20789 : : "be declared %<inline%>", decl);
20790 : :
20791 : : /* An internal-linkage declaration cannot be generally be exported.
20792 : : But it's OK to export any declaration from a header unit, including
20793 : : internal linkage declarations. */
20794 : 429779 : if (!header_module_p () && DECL_MODULE_EXPORT_P (decl))
20795 : : {
20796 : : /* Let's additionally treat any exported declaration within an
20797 : : internal namespace as exporting a declaration with internal
20798 : : linkage, as this would also implicitly export the internal
20799 : : linkage namespace. */
20800 : 1726 : if (decl_internal_context_p (decl))
20801 : : {
20802 : 50 : error_at (DECL_SOURCE_LOCATION (decl),
20803 : : "exporting declaration %qD declared in unnamed namespace",
20804 : : decl);
20805 : 50 : DECL_MODULE_EXPORT_P (decl) = false;
20806 : : }
20807 : 1676 : else if (decl_linkage (decl) == lk_internal)
20808 : : {
20809 : 17 : error_at (DECL_SOURCE_LOCATION (decl),
20810 : : "exporting declaration %qD with internal linkage", decl);
20811 : 17 : DECL_MODULE_EXPORT_P (decl) = false;
20812 : : }
20813 : : }
20814 : : }
20815 : :
20816 : : /* DECL is keyed to CTX for odr purposes. */
20817 : :
20818 : : void
20819 : 807471 : maybe_key_decl (tree ctx, tree decl)
20820 : : {
20821 : 807471 : if (!modules_p ())
20822 : : return;
20823 : :
20824 : : /* We only need to deal with lambdas attached to var, field,
20825 : : parm, type, or concept decls. */
20826 : 5851 : if (TREE_CODE (ctx) != VAR_DECL
20827 : 5851 : && TREE_CODE (ctx) != FIELD_DECL
20828 : 5779 : && TREE_CODE (ctx) != PARM_DECL
20829 : 5767 : && TREE_CODE (ctx) != TYPE_DECL
20830 : 5739 : && TREE_CODE (ctx) != CONCEPT_DECL)
20831 : : return;
20832 : :
20833 : : /* For fields, key it to the containing type to handle deduplication
20834 : : correctly. */
20835 : 149 : if (TREE_CODE (ctx) == FIELD_DECL)
20836 : 6 : ctx = TYPE_NAME (DECL_CONTEXT (ctx));
20837 : :
20838 : 149 : if (!keyed_table)
20839 : 74 : keyed_table = new keyed_map_t (EXPERIMENT (1, 400));
20840 : :
20841 : 149 : auto &vec = keyed_table->get_or_insert (ctx);
20842 : 149 : if (!vec.length ())
20843 : : {
20844 : 135 : retrofit_lang_decl (ctx);
20845 : 135 : DECL_MODULE_KEYED_DECLS_P (ctx) = true;
20846 : : }
20847 : 149 : vec.safe_push (decl);
20848 : : }
20849 : :
20850 : : /* DECL is an instantiated friend that should be attached to the same
20851 : : module that ORIG is. */
20852 : :
20853 : : void
20854 : 1766409 : propagate_defining_module (tree decl, tree orig)
20855 : : {
20856 : 1766409 : if (!modules_p ())
20857 : : return;
20858 : :
20859 : 3365 : tree not_tmpl = STRIP_TEMPLATE (orig);
20860 : 6711 : if (DECL_LANG_SPECIFIC (not_tmpl) && DECL_MODULE_ATTACH_P (not_tmpl))
20861 : : {
20862 : 99 : tree inner = STRIP_TEMPLATE (decl);
20863 : 99 : retrofit_lang_decl (inner);
20864 : 99 : DECL_MODULE_ATTACH_P (inner) = true;
20865 : : }
20866 : :
20867 : 6711 : if (DECL_LANG_SPECIFIC (not_tmpl) && DECL_MODULE_IMPORT_P (not_tmpl))
20868 : : {
20869 : 160 : bool exists = imported_temploid_friends->put (decl, orig);
20870 : :
20871 : : /* We should only be called if lookup for an existing decl
20872 : : failed, in which case there shouldn't already be an entry
20873 : : in the map. */
20874 : 160 : gcc_assert (!exists);
20875 : : }
20876 : : }
20877 : :
20878 : : /* DECL is being freed, clear data we don't need anymore. */
20879 : :
20880 : : void
20881 : 28332 : remove_defining_module (tree decl)
20882 : : {
20883 : 28332 : if (!modules_p ())
20884 : : return;
20885 : :
20886 : 28332 : if (imported_temploid_friends)
20887 : 28332 : imported_temploid_friends->remove (decl);
20888 : : }
20889 : :
20890 : : /* Create the flat name string. It is simplest to have it handy. */
20891 : :
20892 : : void
20893 : 5362 : module_state::set_flatname ()
20894 : : {
20895 : 5362 : gcc_checking_assert (!flatname);
20896 : 5362 : if (parent)
20897 : : {
20898 : 549 : auto_vec<tree,5> ids;
20899 : 549 : size_t len = 0;
20900 : 549 : char const *primary = NULL;
20901 : 549 : size_t pfx_len = 0;
20902 : :
20903 : 549 : for (module_state *probe = this;
20904 : 1376 : probe;
20905 : 827 : probe = probe->parent)
20906 : 1230 : if (is_partition () && !probe->is_partition ())
20907 : : {
20908 : 403 : primary = probe->get_flatname ();
20909 : 403 : pfx_len = strlen (primary);
20910 : 403 : break;
20911 : : }
20912 : : else
20913 : : {
20914 : 827 : ids.safe_push (probe->name);
20915 : 827 : len += IDENTIFIER_LENGTH (probe->name) + 1;
20916 : : }
20917 : :
20918 : 549 : char *flat = XNEWVEC (char, pfx_len + len + is_partition ());
20919 : 549 : flatname = flat;
20920 : :
20921 : 549 : if (primary)
20922 : : {
20923 : 403 : memcpy (flat, primary, pfx_len);
20924 : 403 : flat += pfx_len;
20925 : 403 : *flat++ = ':';
20926 : : }
20927 : :
20928 : 1376 : for (unsigned len = 0; ids.length ();)
20929 : : {
20930 : 827 : if (len)
20931 : 278 : flat[len++] = '.';
20932 : 827 : tree elt = ids.pop ();
20933 : 827 : unsigned l = IDENTIFIER_LENGTH (elt);
20934 : 827 : memcpy (flat + len, IDENTIFIER_POINTER (elt), l + 1);
20935 : 827 : len += l;
20936 : : }
20937 : 549 : }
20938 : 4813 : else if (is_header ())
20939 : 1787 : flatname = TREE_STRING_POINTER (name);
20940 : : else
20941 : 3026 : flatname = IDENTIFIER_POINTER (name);
20942 : 5362 : }
20943 : :
20944 : : /* Read the CMI file for a module. */
20945 : :
20946 : : bool
20947 : 2540 : module_state::do_import (cpp_reader *reader, bool outermost)
20948 : : {
20949 : 2540 : gcc_assert (global_namespace == current_scope () && loadedness == ML_NONE);
20950 : :
20951 : 2540 : loc = linemap_module_loc (line_table, loc, get_flatname ());
20952 : :
20953 : 2540 : if (lazy_open >= lazy_limit)
20954 : 9 : freeze_an_elf ();
20955 : :
20956 : 2540 : int fd = -1;
20957 : 2540 : int e = ENOENT;
20958 : 2540 : if (filename)
20959 : : {
20960 : 2540 : const char *file = maybe_add_cmi_prefix (filename);
20961 : 3001 : dump () && dump ("CMI is %s", file);
20962 : 2540 : if (note_module_cmi_yes || inform_cmi_p)
20963 : 12 : inform (loc, "reading CMI %qs", file);
20964 : : /* Add the CMI file to the dependency tracking. */
20965 : 2540 : if (cpp_get_deps (reader))
20966 : 12 : deps_add_dep (cpp_get_deps (reader), file);
20967 : 2540 : fd = open (file, O_RDONLY | O_CLOEXEC | O_BINARY);
20968 : 2540 : e = errno;
20969 : : }
20970 : :
20971 : 2540 : gcc_checking_assert (!slurp);
20972 : 5062 : slurp = new slurping (new elf_in (fd, e));
20973 : :
20974 : 2540 : bool ok = true;
20975 : 2540 : if (!from ()->get_error ())
20976 : : {
20977 : 2522 : announce ("importing");
20978 : 2522 : loadedness = ML_CONFIG;
20979 : 2522 : lazy_open++;
20980 : 2522 : ok = read_initial (reader);
20981 : 2522 : slurp->lru = ++lazy_lru;
20982 : : }
20983 : :
20984 : 2540 : gcc_assert (slurp->current == ~0u);
20985 : :
20986 : 2540 : return check_read (outermost, ok);
20987 : : }
20988 : :
20989 : : /* Attempt to increase the file descriptor limit. */
20990 : :
20991 : : static bool
20992 : 4161 : try_increase_lazy (unsigned want)
20993 : : {
20994 : 4161 : gcc_checking_assert (lazy_open >= lazy_limit);
20995 : :
20996 : : /* If we're increasing, saturate at hard limit. */
20997 : 4161 : if (want > lazy_hard_limit && lazy_limit < lazy_hard_limit)
20998 : 4161 : want = lazy_hard_limit;
20999 : :
21000 : : #if HAVE_SETRLIMIT
21001 : 4161 : if ((!lazy_limit || !param_lazy_modules)
21002 : 4149 : && lazy_hard_limit
21003 : 4149 : && want <= lazy_hard_limit)
21004 : : {
21005 : 4149 : struct rlimit rlimit;
21006 : 4149 : rlimit.rlim_cur = want + LAZY_HEADROOM;
21007 : 4149 : rlimit.rlim_max = lazy_hard_limit + LAZY_HEADROOM;
21008 : 4149 : if (!setrlimit (RLIMIT_NOFILE, &rlimit))
21009 : 4149 : lazy_limit = want;
21010 : : }
21011 : : #endif
21012 : :
21013 : 4161 : return lazy_open < lazy_limit;
21014 : : }
21015 : :
21016 : : /* Pick a victim module to freeze its reader. */
21017 : :
21018 : : void
21019 : 12 : module_state::freeze_an_elf ()
21020 : : {
21021 : 12 : if (try_increase_lazy (lazy_open * 2))
21022 : : return;
21023 : :
21024 : 12 : module_state *victim = NULL;
21025 : 12 : for (unsigned ix = modules->length (); ix--;)
21026 : : {
21027 : 30 : module_state *candidate = (*modules)[ix];
21028 : 30 : if (candidate && candidate->slurp && candidate->slurp->lru
21029 : 60 : && candidate->from ()->is_freezable ()
21030 : 39 : && (!victim || victim->slurp->lru > candidate->slurp->lru))
21031 : : victim = candidate;
21032 : : }
21033 : :
21034 : 12 : if (victim)
21035 : : {
21036 : 18 : dump () && dump ("Freezing '%s'", victim->filename);
21037 : 9 : if (victim->slurp->macro_defs.size)
21038 : : /* Save the macro definitions to a buffer. */
21039 : 0 : victim->from ()->preserve (victim->slurp->macro_defs);
21040 : 9 : if (victim->slurp->macro_tbl.size)
21041 : : /* Save the macro definitions to a buffer. */
21042 : 0 : victim->from ()->preserve (victim->slurp->macro_tbl);
21043 : 9 : victim->from ()->freeze ();
21044 : 9 : lazy_open--;
21045 : : }
21046 : : else
21047 : 3 : dump () && dump ("No module available for freezing");
21048 : : }
21049 : :
21050 : : /* Load the lazy slot *MSLOT, INDEX'th slot of the module. */
21051 : :
21052 : : bool
21053 : 26809 : module_state::lazy_load (unsigned index, binding_slot *mslot)
21054 : : {
21055 : 26809 : unsigned n = dump.push (this);
21056 : :
21057 : 26809 : gcc_checking_assert (function_depth);
21058 : :
21059 : 26809 : unsigned cookie = mslot->get_lazy ();
21060 : 26809 : unsigned snum = cookie >> 2;
21061 : 27100 : dump () && dump ("Loading entity %M[%u] section:%u", this, index, snum);
21062 : :
21063 : 26809 : bool ok = load_section (snum, mslot);
21064 : :
21065 : 26809 : dump.pop (n);
21066 : :
21067 : 26809 : return ok;
21068 : : }
21069 : :
21070 : : /* Load MOD's binding for NS::ID into *MSLOT. *MSLOT contains the
21071 : : lazy cookie. OUTER is true if this is the outermost lazy, (used
21072 : : for diagnostics). */
21073 : :
21074 : : void
21075 : 2454 : lazy_load_binding (unsigned mod, tree ns, tree id, binding_slot *mslot)
21076 : : {
21077 : 2454 : int count = errorcount + warningcount;
21078 : :
21079 : 2454 : bool timer_running = timevar_cond_start (TV_MODULE_IMPORT);
21080 : :
21081 : : /* Make sure lazy loading from a template context behaves as if
21082 : : from a non-template context. */
21083 : 2454 : processing_template_decl_sentinel ptds;
21084 : :
21085 : : /* Stop GC happening, even in outermost loads (because our caller
21086 : : could well be building up a lookup set). */
21087 : 2454 : function_depth++;
21088 : :
21089 : 2454 : gcc_checking_assert (mod);
21090 : 2454 : module_state *module = (*modules)[mod];
21091 : 2454 : unsigned n = dump.push (module);
21092 : :
21093 : 2454 : unsigned snum = mslot->get_lazy ();
21094 : 2748 : dump () && dump ("Lazily binding %P@%N section:%u", ns, id,
21095 : : module->name, snum);
21096 : :
21097 : 2454 : bool ok = !recursive_lazy (snum);
21098 : 2454 : if (ok)
21099 : : {
21100 : 2454 : ok = module->load_section (snum, mslot);
21101 : 2454 : lazy_snum = 0;
21102 : 2454 : post_load_processing ();
21103 : : }
21104 : :
21105 : 2454 : dump.pop (n);
21106 : :
21107 : 2454 : function_depth--;
21108 : :
21109 : 2454 : timevar_cond_stop (TV_MODULE_IMPORT, timer_running);
21110 : :
21111 : 2454 : if (!ok)
21112 : 0 : fatal_error (input_location,
21113 : 0 : module->is_header ()
21114 : : ? G_("failed to load binding %<%E%s%E%>")
21115 : : : G_("failed to load binding %<%E%s%E@%s%>"),
21116 : 0 : ns, &"::"[ns == global_namespace ? 2 : 0], id,
21117 : : module->get_flatname ());
21118 : :
21119 : 2454 : if (count != errorcount + warningcount)
21120 : 21 : inform (input_location,
21121 : 21 : module->is_header ()
21122 : : ? G_("during load of binding %<%E%s%E%>")
21123 : : : G_("during load of binding %<%E%s%E@%s%>"),
21124 : 21 : ns, &"::"[ns == global_namespace ? 2 : 0], id,
21125 : : module->get_flatname ());
21126 : 2454 : }
21127 : :
21128 : : /* Load any pending entities keyed to the top-key of DECL. */
21129 : :
21130 : : void
21131 : 23318818 : lazy_load_pendings (tree decl)
21132 : : {
21133 : : /* Make sure lazy loading from a template context behaves as if
21134 : : from a non-template context. */
21135 : 23318818 : processing_template_decl_sentinel ptds;
21136 : :
21137 : 23318818 : tree key_decl;
21138 : 23318818 : pending_key key;
21139 : 23318818 : key.ns = find_pending_key (decl, &key_decl);
21140 : 23318818 : key.id = DECL_NAME (key_decl);
21141 : :
21142 : 23318818 : auto *pending_vec = pending_table ? pending_table->get (key) : nullptr;
21143 : 23318818 : if (!pending_vec)
21144 : 23314094 : return;
21145 : :
21146 : 4724 : int count = errorcount + warningcount;
21147 : :
21148 : 4724 : bool timer_running = timevar_cond_start (TV_MODULE_IMPORT);
21149 : 4724 : bool ok = !recursive_lazy ();
21150 : 4724 : if (ok)
21151 : : {
21152 : 4724 : function_depth++; /* Prevent GC */
21153 : 4724 : unsigned n = dump.push (NULL);
21154 : 5134 : dump () && dump ("Reading %u pending entities keyed to %P",
21155 : : pending_vec->length (), key.ns, key.id);
21156 : 4724 : for (unsigned ix = pending_vec->length (); ix--;)
21157 : : {
21158 : 49518 : unsigned index = (*pending_vec)[ix];
21159 : 49518 : binding_slot *slot = &(*entity_ary)[index];
21160 : :
21161 : 49518 : if (slot->is_lazy ())
21162 : : {
21163 : 2544 : module_state *import = import_entity_module (index);
21164 : 2544 : if (!import->lazy_load (index - import->entity_lwm, slot))
21165 : 49518 : ok = false;
21166 : : }
21167 : 46974 : else if (dump ())
21168 : : {
21169 : 301 : module_state *import = import_entity_module (index);
21170 : 54543 : dump () && dump ("Entity %M[%u] already loaded",
21171 : 301 : import, index - import->entity_lwm);
21172 : : }
21173 : : }
21174 : :
21175 : 4724 : pending_table->remove (key);
21176 : 4724 : dump.pop (n);
21177 : 4724 : lazy_snum = 0;
21178 : 4724 : post_load_processing ();
21179 : 4724 : function_depth--;
21180 : : }
21181 : :
21182 : 4724 : timevar_cond_stop (TV_MODULE_IMPORT, timer_running);
21183 : :
21184 : 4724 : if (!ok)
21185 : 0 : fatal_error (input_location, "failed to load pendings for %<%E%s%E%>",
21186 : 0 : key.ns, &"::"[key.ns == global_namespace ? 2 : 0], key.id);
21187 : :
21188 : 4724 : if (count != errorcount + warningcount)
21189 : 0 : inform (input_location, "during load of pendings for %<%E%s%E%>",
21190 : 0 : key.ns, &"::"[key.ns == global_namespace ? 2 : 0], key.id);
21191 : 23318818 : }
21192 : :
21193 : : static void
21194 : 2315 : direct_import (module_state *import, cpp_reader *reader)
21195 : : {
21196 : 2315 : timevar_start (TV_MODULE_IMPORT);
21197 : 2315 : unsigned n = dump.push (import);
21198 : :
21199 : 2315 : gcc_checking_assert (import->is_direct () && import->has_location ());
21200 : 2315 : if (import->loadedness == ML_NONE)
21201 : 1443 : if (!import->do_import (reader, true))
21202 : 0 : gcc_unreachable ();
21203 : :
21204 : 2281 : if (import->loadedness < ML_LANGUAGE)
21205 : : {
21206 : 2208 : if (!keyed_table)
21207 : 1936 : keyed_table = new keyed_map_t (EXPERIMENT (1, 400));
21208 : 2208 : import->read_language (true);
21209 : : }
21210 : :
21211 : 2281 : (*modules)[0]->set_import (import, import->exported_p);
21212 : :
21213 : 2281 : dump.pop (n);
21214 : 2281 : timevar_stop (TV_MODULE_IMPORT);
21215 : 2281 : }
21216 : :
21217 : : /* Import module IMPORT. */
21218 : :
21219 : : void
21220 : 2114 : import_module (module_state *import, location_t from_loc, bool exporting_p,
21221 : : tree, cpp_reader *reader)
21222 : : {
21223 : 2114 : if (!import->check_not_purview (from_loc))
21224 : : return;
21225 : :
21226 : 2105 : if (!import->is_header () && current_lang_depth ())
21227 : : /* Only header units should appear inside language
21228 : : specifications. The std doesn't specify this, but I think
21229 : : that's an error in resolving US 033, because language linkage
21230 : : is also our escape clause to getting things into the global
21231 : : module, so we don't want to confuse things by having to think
21232 : : about whether 'extern "C++" { import foo; }' puts foo's
21233 : : contents into the global module all of a sudden. */
21234 : 6 : warning (0, "import of named module %qs inside language-linkage block",
21235 : : import->get_flatname ());
21236 : :
21237 : 2105 : if (exporting_p || module_exporting_p ())
21238 : 268 : import->exported_p = true;
21239 : :
21240 : 2105 : if (import->loadedness != ML_NONE)
21241 : : {
21242 : 869 : from_loc = ordinary_loc_of (line_table, from_loc);
21243 : 869 : linemap_module_reparent (line_table, import->loc, from_loc);
21244 : : }
21245 : 2105 : gcc_checking_assert (!import->module_p);
21246 : 2105 : gcc_checking_assert (import->is_direct () && import->has_location ());
21247 : :
21248 : 2105 : direct_import (import, reader);
21249 : : }
21250 : :
21251 : : /* Declare the name of the current module to be NAME. EXPORTING_p is
21252 : : true if this TU is the exporting module unit. */
21253 : :
21254 : : void
21255 : 2658 : declare_module (module_state *module, location_t from_loc, bool exporting_p,
21256 : : tree, cpp_reader *reader)
21257 : : {
21258 : 2658 : gcc_assert (global_namespace == current_scope ());
21259 : :
21260 : 2658 : module_state *current = (*modules)[0];
21261 : 2658 : if (module_purview_p () || module->loadedness > ML_CONFIG)
21262 : : {
21263 : 6 : auto_diagnostic_group d;
21264 : 12 : error_at (from_loc, module_purview_p ()
21265 : : ? G_("module already declared")
21266 : : : G_("module already imported"));
21267 : 6 : if (module_purview_p ())
21268 : 0 : module = current;
21269 : 12 : inform (module->loc, module_purview_p ()
21270 : : ? G_("module %qs declared here")
21271 : : : G_("module %qs imported here"),
21272 : : module->get_flatname ());
21273 : 6 : return;
21274 : 6 : }
21275 : :
21276 : 2652 : gcc_checking_assert (module->module_p);
21277 : 2652 : gcc_checking_assert (module->is_direct () && module->has_location ());
21278 : :
21279 : : /* Yer a module, 'arry. */
21280 : 2652 : module_kind = module->is_header () ? MK_HEADER : MK_NAMED | MK_ATTACH;
21281 : :
21282 : : // Even in header units, we consider the decls to be purview
21283 : 2652 : module_kind |= MK_PURVIEW;
21284 : :
21285 : 2652 : if (module->is_partition ())
21286 : 158 : module_kind |= MK_PARTITION;
21287 : 2652 : if (exporting_p)
21288 : : {
21289 : 2401 : module->interface_p = true;
21290 : 2401 : module_kind |= MK_INTERFACE;
21291 : : }
21292 : :
21293 : 2652 : if (module_has_cmi_p ())
21294 : : {
21295 : : /* Copy the importing information we may have already done. We
21296 : : do not need to separate out the imports that only happen in
21297 : : the GMF, inspite of what the literal wording of the std
21298 : : might imply. See p2191, the core list had a discussion
21299 : : where the module implementors agreed that the GMF of a named
21300 : : module is invisible to importers. */
21301 : 2442 : module->imports = current->imports;
21302 : :
21303 : 2442 : module->mod = 0;
21304 : 2442 : (*modules)[0] = module;
21305 : : }
21306 : : else
21307 : : {
21308 : 210 : module->interface_p = true;
21309 : 210 : current->parent = module; /* So mangler knows module identity. */
21310 : 210 : direct_import (module, reader);
21311 : : }
21312 : : }
21313 : :
21314 : : /* Return true IFF we must emit a module global initializer function
21315 : : (which will be called by importers' init code). */
21316 : :
21317 : : bool
21318 : 99369 : module_global_init_needed ()
21319 : : {
21320 : 99369 : return module_has_cmi_p () && !header_module_p ();
21321 : : }
21322 : :
21323 : : /* Calculate which, if any, import initializers need calling. */
21324 : :
21325 : : bool
21326 : 92636 : module_determine_import_inits ()
21327 : : {
21328 : 92636 : if (!modules || header_module_p ())
21329 : : return false;
21330 : :
21331 : : /* Prune active_init_p. We need the same bitmap allocation
21332 : : scheme as for the imports member. */
21333 : 3157 : function_depth++; /* Disable GC. */
21334 : 3157 : bitmap covered_imports (BITMAP_GGC_ALLOC ());
21335 : :
21336 : 3157 : bool any = false;
21337 : :
21338 : : /* Because indirect imports are before their direct import, and
21339 : : we're scanning the array backwards, we only need one pass! */
21340 : 5516 : for (unsigned ix = modules->length (); --ix;)
21341 : : {
21342 : 2359 : module_state *import = (*modules)[ix];
21343 : :
21344 : 2359 : if (!import->active_init_p)
21345 : : ;
21346 : 43 : else if (bitmap_bit_p (covered_imports, ix))
21347 : 6 : import->active_init_p = false;
21348 : : else
21349 : : {
21350 : : /* Everything this imports is therefore handled by its
21351 : : initializer, so doesn't need initializing by us. */
21352 : 37 : bitmap_ior_into (covered_imports, import->imports);
21353 : 37 : any = true;
21354 : : }
21355 : : }
21356 : 3157 : function_depth--;
21357 : :
21358 : 3157 : return any;
21359 : : }
21360 : :
21361 : : /* Emit calls to each direct import's global initializer. Including
21362 : : direct imports of directly imported header units. The initializers
21363 : : of (static) entities in header units will be called by their
21364 : : importing modules (for the instance contained within that), or by
21365 : : the current TU (for the instances we've brought in). Of course
21366 : : such header unit behaviour is evil, but iostream went through that
21367 : : door some time ago. */
21368 : :
21369 : : void
21370 : 37 : module_add_import_initializers ()
21371 : : {
21372 : 37 : if (!modules || header_module_p ())
21373 : 0 : return;
21374 : :
21375 : 37 : tree fntype = build_function_type (void_type_node, void_list_node);
21376 : 37 : releasing_vec args; // There are no args
21377 : :
21378 : 83 : for (unsigned ix = modules->length (); --ix;)
21379 : : {
21380 : 46 : module_state *import = (*modules)[ix];
21381 : 46 : if (import->active_init_p)
21382 : : {
21383 : 37 : tree name = mangle_module_global_init (ix);
21384 : 37 : tree fndecl = build_lang_decl (FUNCTION_DECL, name, fntype);
21385 : :
21386 : 37 : DECL_CONTEXT (fndecl) = FROB_CONTEXT (global_namespace);
21387 : 37 : SET_DECL_ASSEMBLER_NAME (fndecl, name);
21388 : 37 : TREE_PUBLIC (fndecl) = true;
21389 : 37 : determine_visibility (fndecl);
21390 : :
21391 : 37 : tree call = cp_build_function_call_vec (fndecl, &args,
21392 : : tf_warning_or_error);
21393 : 37 : finish_expr_stmt (call);
21394 : : }
21395 : : }
21396 : 37 : }
21397 : :
21398 : : /* NAME & LEN are a preprocessed header name, possibly including the
21399 : : surrounding "" or <> characters. Return the raw string name of the
21400 : : module to which it refers. This will be an absolute path, or begin
21401 : : with ./, so it is immediately distinguishable from a (non-header
21402 : : unit) module name. If READER is non-null, ask the preprocessor to
21403 : : locate the header to which it refers using the appropriate include
21404 : : path. Note that we do never do \ processing of the string, as that
21405 : : matches the preprocessor's behaviour. */
21406 : :
21407 : : static const char *
21408 : 22922 : canonicalize_header_name (cpp_reader *reader, location_t loc, bool unquoted,
21409 : : const char *str, size_t &len_r)
21410 : : {
21411 : 22922 : size_t len = len_r;
21412 : 22922 : static char *buf = 0;
21413 : 22922 : static size_t alloc = 0;
21414 : :
21415 : 22922 : if (!unquoted)
21416 : : {
21417 : 4 : gcc_checking_assert (len >= 2
21418 : : && ((reader && str[0] == '<' && str[len-1] == '>')
21419 : : || (str[0] == '"' && str[len-1] == '"')));
21420 : 4 : str += 1;
21421 : 4 : len -= 2;
21422 : : }
21423 : :
21424 : 22922 : if (reader)
21425 : : {
21426 : 4 : gcc_assert (!unquoted);
21427 : :
21428 : 4 : if (len >= alloc)
21429 : : {
21430 : 4 : alloc = len + 1;
21431 : 4 : buf = XRESIZEVEC (char, buf, alloc);
21432 : : }
21433 : 4 : memcpy (buf, str, len);
21434 : 4 : buf[len] = 0;
21435 : :
21436 : 8 : if (const char *hdr
21437 : 4 : = cpp_probe_header_unit (reader, buf, str[-1] == '<', loc))
21438 : : {
21439 : 4 : len = strlen (hdr);
21440 : 4 : str = hdr;
21441 : : }
21442 : : else
21443 : 0 : str = buf;
21444 : : }
21445 : :
21446 : 22922 : if (!(str[0] == '.' ? IS_DIR_SEPARATOR (str[1]) : IS_ABSOLUTE_PATH (str)))
21447 : : {
21448 : : /* Prepend './' */
21449 : 9 : if (len + 3 > alloc)
21450 : : {
21451 : 9 : alloc = len + 3;
21452 : 9 : buf = XRESIZEVEC (char, buf, alloc);
21453 : : }
21454 : :
21455 : 9 : buf[0] = '.';
21456 : 9 : buf[1] = DIR_SEPARATOR;
21457 : 9 : memmove (buf + 2, str, len);
21458 : 9 : len += 2;
21459 : 9 : buf[len] = 0;
21460 : 9 : str = buf;
21461 : : }
21462 : :
21463 : 22922 : len_r = len;
21464 : 22922 : return str;
21465 : : }
21466 : :
21467 : : /* Set the CMI name from a cody packet. Issue an error if
21468 : : ill-formed. */
21469 : :
21470 : 4882 : void module_state::set_filename (const Cody::Packet &packet)
21471 : : {
21472 : 4882 : if (packet.GetCode () == Cody::Client::PC_PATHNAME)
21473 : : {
21474 : : /* If we've seen this import before we better have the same CMI. */
21475 : 4879 : const std::string &path = packet.GetString ();
21476 : 4879 : if (!filename)
21477 : 4876 : filename = xstrdup (packet.GetString ().c_str ());
21478 : 3 : else if (filename != path)
21479 : 0 : error_at (loc, "mismatching compiled module interface: "
21480 : : "had %qs, got %qs", filename, path.c_str ());
21481 : : }
21482 : : else
21483 : : {
21484 : 3 : gcc_checking_assert (packet.GetCode () == Cody::Client::PC_ERROR);
21485 : 3 : fatal_error (loc, "unknown compiled module interface: %s",
21486 : 3 : packet.GetString ().c_str ());
21487 : : }
21488 : 4879 : }
21489 : :
21490 : : /* Figure out whether to treat HEADER as an include or an import. */
21491 : :
21492 : : static char *
21493 : 22056 : maybe_translate_include (cpp_reader *reader, line_maps *lmaps, location_t loc,
21494 : : const char *path)
21495 : : {
21496 : 22056 : if (!modules_p ())
21497 : : {
21498 : : /* Turn off. */
21499 : 0 : cpp_get_callbacks (reader)->translate_include = NULL;
21500 : 0 : return nullptr;
21501 : : }
21502 : :
21503 : 22056 : dump.push (NULL);
21504 : :
21505 : 23924 : dump () && dump ("Checking include translation '%s'", path);
21506 : 22056 : auto *mapper = get_mapper (cpp_main_loc (reader), cpp_get_deps (reader));
21507 : :
21508 : 22056 : size_t len = strlen (path);
21509 : 22056 : path = canonicalize_header_name (NULL, loc, true, path, len);
21510 : 22056 : auto packet = mapper->IncludeTranslate (path, Cody::Flags::None, len);
21511 : :
21512 : 22056 : enum class xlate_kind {
21513 : : unknown, text, import,
21514 : 22056 : } translate = xlate_kind::unknown;
21515 : :
21516 : 22056 : if (packet.GetCode () == Cody::Client::PC_BOOL)
21517 : 22009 : translate = packet.GetInteger () ? xlate_kind::text : xlate_kind::unknown;
21518 : 47 : else if (packet.GetCode () == Cody::Client::PC_PATHNAME)
21519 : : {
21520 : : /* Record the CMI name for when we do the import.
21521 : : We may already know about this import, but libcpp doesn't yet. */
21522 : 47 : module_state *import = get_module (build_string (len, path));
21523 : 47 : import->set_filename (packet);
21524 : 47 : translate = xlate_kind::import;
21525 : : }
21526 : : else
21527 : : {
21528 : 0 : gcc_checking_assert (packet.GetCode () == Cody::Client::PC_ERROR);
21529 : 0 : error_at (loc, "cannot determine %<#include%> translation of %s: %s",
21530 : 0 : path, packet.GetString ().c_str ());
21531 : : }
21532 : :
21533 : 22056 : bool note = false;
21534 : 22056 : if (note_include_translate_yes && translate == xlate_kind::import)
21535 : : note = true;
21536 : 22053 : else if (note_include_translate_no && translate == xlate_kind::unknown)
21537 : : note = true;
21538 : 22047 : else if (note_includes)
21539 : : /* We do not expect the note_includes vector to be large, so O(N)
21540 : : iteration. */
21541 : 410 : for (unsigned ix = note_includes->length (); !note && ix--;)
21542 : 205 : if (!strcmp ((*note_includes)[ix], path))
21543 : 1 : note = true;
21544 : :
21545 : 205 : if (note)
21546 : 16 : inform (loc, translate == xlate_kind::import
21547 : : ? G_("include %qs translated to import")
21548 : : : G_("include %qs processed textually"), path);
21549 : :
21550 : 25789 : dump () && dump (translate == xlate_kind::import
21551 : : ? "Translating include to import"
21552 : : : "Keeping include as include");
21553 : 22056 : dump.pop (0);
21554 : :
21555 : 22056 : if (translate != xlate_kind::import)
21556 : : return nullptr;
21557 : :
21558 : : /* Create the translation text. */
21559 : 47 : loc = ordinary_loc_of (lmaps, loc);
21560 : 47 : const line_map_ordinary *map
21561 : 47 : = linemap_check_ordinary (linemap_lookup (lmaps, loc));
21562 : 47 : unsigned col = SOURCE_COLUMN (map, loc);
21563 : 47 : col -= (col != 0); /* Columns are 1-based. */
21564 : :
21565 : 47 : unsigned alloc = len + col + 60;
21566 : 47 : char *res = XNEWVEC (char, alloc);
21567 : :
21568 : 47 : strcpy (res, "__import");
21569 : 47 : unsigned actual = 8;
21570 : 47 : if (col > actual)
21571 : : {
21572 : : /* Pad out so the filename appears at the same position. */
21573 : 44 : memset (res + actual, ' ', col - actual);
21574 : 44 : actual = col;
21575 : : }
21576 : : /* No need to encode characters, that's not how header names are
21577 : : handled. */
21578 : 47 : actual += snprintf (res + actual, alloc - actual,
21579 : : "\"%s\" [[__translated]];\n", path);
21580 : 47 : gcc_checking_assert (actual < alloc);
21581 : :
21582 : : /* cpplib will delete the buffer. */
21583 : : return res;
21584 : 22056 : }
21585 : :
21586 : : static void
21587 : 862 : begin_header_unit (cpp_reader *reader)
21588 : : {
21589 : : /* Set the module header name from the main_input_filename. */
21590 : 862 : const char *main = main_input_filename;
21591 : 862 : size_t len = strlen (main);
21592 : 862 : main = canonicalize_header_name (NULL, 0, true, main, len);
21593 : 862 : module_state *module = get_module (build_string (len, main));
21594 : :
21595 : 862 : preprocess_module (module, cpp_main_loc (reader), false, false, true, reader);
21596 : 862 : }
21597 : :
21598 : : /* We've just properly entered the main source file. I.e. after the
21599 : : command line, builtins and forced headers. Record the line map and
21600 : : location of this map. Note we may be called more than once. The
21601 : : first call sticks. */
21602 : :
21603 : : void
21604 : 94442 : module_begin_main_file (cpp_reader *reader, line_maps *lmaps,
21605 : : const line_map_ordinary *map)
21606 : : {
21607 : 94442 : gcc_checking_assert (lmaps == line_table);
21608 : 94442 : if (modules_p () && !spans.init_p ())
21609 : : {
21610 : 4149 : unsigned n = dump.push (NULL);
21611 : 4149 : spans.init (lmaps, map);
21612 : 4149 : dump.pop (n);
21613 : 4149 : if (flag_header_unit && !cpp_get_options (reader)->preprocessed)
21614 : : {
21615 : : /* Tell the preprocessor this is an include file. */
21616 : 853 : cpp_retrofit_as_include (reader);
21617 : 853 : begin_header_unit (reader);
21618 : : }
21619 : : }
21620 : 94442 : }
21621 : :
21622 : : /* Process the pending_import queue, making sure we know the
21623 : : filenames. */
21624 : :
21625 : : static void
21626 : 4993 : name_pending_imports (cpp_reader *reader)
21627 : : {
21628 : 4993 : auto *mapper = get_mapper (cpp_main_loc (reader), cpp_get_deps (reader));
21629 : :
21630 : 4993 : if (!vec_safe_length (pending_imports))
21631 : : /* Not doing anything. */
21632 : : return;
21633 : :
21634 : 4242 : timevar_start (TV_MODULE_MAPPER);
21635 : :
21636 : 4242 : auto n = dump.push (NULL);
21637 : 4786 : dump () && dump ("Resolving direct import names");
21638 : 4242 : bool want_deps = (bool (mapper->get_flags () & Cody::Flags::NameOnly)
21639 : 4242 : || cpp_get_deps (reader));
21640 : 4242 : bool any = false;
21641 : :
21642 : 9244 : for (unsigned ix = 0; ix != pending_imports->length (); ix++)
21643 : : {
21644 : 5002 : module_state *module = (*pending_imports)[ix];
21645 : 5002 : gcc_checking_assert (module->is_direct ());
21646 : 5002 : if (!module->filename && !module->visited_p)
21647 : : {
21648 : 4901 : bool export_p = (module->module_p
21649 : 4901 : && (module->is_partition () || module->exported_p));
21650 : :
21651 : 4901 : Cody::Flags flags = Cody::Flags::None;
21652 : 4901 : if (flag_preprocess_only
21653 : 4901 : && !(module->is_header () && !export_p))
21654 : : {
21655 : 129 : if (!want_deps)
21656 : 66 : continue;
21657 : : flags = Cody::Flags::NameOnly;
21658 : : }
21659 : :
21660 : 4835 : if (!any)
21661 : : {
21662 : 4172 : any = true;
21663 : 4172 : mapper->Cork ();
21664 : : }
21665 : 4835 : if (export_p)
21666 : 2487 : mapper->ModuleExport (module->get_flatname (), flags);
21667 : : else
21668 : 2348 : mapper->ModuleImport (module->get_flatname (), flags);
21669 : 4835 : module->visited_p = true;
21670 : : }
21671 : : }
21672 : :
21673 : 4242 : if (any)
21674 : : {
21675 : 4172 : auto response = mapper->Uncork ();
21676 : 4172 : auto r_iter = response.begin ();
21677 : 9089 : for (unsigned ix = 0; ix != pending_imports->length (); ix++)
21678 : : {
21679 : 4920 : module_state *module = (*pending_imports)[ix];
21680 : 4920 : if (module->visited_p)
21681 : : {
21682 : 4835 : module->visited_p = false;
21683 : 4835 : gcc_checking_assert (!module->filename);
21684 : :
21685 : 4835 : module->set_filename (*r_iter);
21686 : 4832 : ++r_iter;
21687 : : }
21688 : : }
21689 : 4169 : }
21690 : :
21691 : 4239 : dump.pop (n);
21692 : :
21693 : 4239 : timevar_stop (TV_MODULE_MAPPER);
21694 : : }
21695 : :
21696 : : /* We've just lexed a module-specific control line for MODULE. Mark
21697 : : the module as a direct import, and possibly load up its macro
21698 : : state. Returns the primary module, if this is a module
21699 : : declaration. */
21700 : : /* Perhaps we should offer a preprocessing mode where we read the
21701 : : directives from the header unit, rather than require the header's
21702 : : CMI. */
21703 : :
21704 : : module_state *
21705 : 5021 : preprocess_module (module_state *module, location_t from_loc,
21706 : : bool in_purview, bool is_import, bool is_export,
21707 : : cpp_reader *reader)
21708 : : {
21709 : 5021 : if (!is_import)
21710 : : {
21711 : 2796 : if (module->loc)
21712 : : /* It's already been mentioned, so ignore its module-ness. */
21713 : : is_import = true;
21714 : : else
21715 : : {
21716 : : /* Record it is the module. */
21717 : 2775 : module->module_p = true;
21718 : 2775 : if (is_export)
21719 : : {
21720 : 2491 : module->exported_p = true;
21721 : 2491 : module->interface_p = true;
21722 : : }
21723 : : }
21724 : : }
21725 : :
21726 : 5021 : if (module->directness < MD_DIRECT + in_purview)
21727 : : {
21728 : : /* Mark as a direct import. */
21729 : 4978 : module->directness = module_directness (MD_DIRECT + in_purview);
21730 : :
21731 : : /* Set the location to be most informative for users. */
21732 : 4978 : from_loc = ordinary_loc_of (line_table, from_loc);
21733 : 4978 : if (module->loadedness != ML_NONE)
21734 : 6 : linemap_module_reparent (line_table, module->loc, from_loc);
21735 : : else
21736 : : {
21737 : 4972 : module->loc = from_loc;
21738 : 4972 : if (!module->flatname)
21739 : 4945 : module->set_flatname ();
21740 : : }
21741 : : }
21742 : :
21743 : 5021 : auto desired = ML_CONFIG;
21744 : 5021 : if (is_import
21745 : 2246 : && module->is_header ()
21746 : 5878 : && (!cpp_get_options (reader)->preprocessed
21747 : 3 : || cpp_get_options (reader)->directives_only))
21748 : : /* We need preprocessor state now. */
21749 : : desired = ML_PREPROCESSOR;
21750 : :
21751 : 5021 : if (!is_import || module->loadedness < desired)
21752 : : {
21753 : 5002 : vec_safe_push (pending_imports, module);
21754 : :
21755 : 5002 : if (desired == ML_PREPROCESSOR)
21756 : : {
21757 : 838 : unsigned n = dump.push (NULL);
21758 : :
21759 : 1035 : dump () && dump ("Reading %M preprocessor state", module);
21760 : 838 : name_pending_imports (reader);
21761 : :
21762 : : /* Preserve the state of the line-map. */
21763 : 838 : auto pre_hwm = LINEMAPS_ORDINARY_USED (line_table);
21764 : :
21765 : : /* We only need to close the span, if we're going to emit a
21766 : : CMI. But that's a little tricky -- our token scanner
21767 : : needs to be smarter -- and this isn't much state.
21768 : : Remember, we've not parsed anything at this point, so
21769 : : our module state flags are inadequate. */
21770 : 838 : spans.maybe_init ();
21771 : 838 : spans.close ();
21772 : :
21773 : 838 : timevar_start (TV_MODULE_IMPORT);
21774 : :
21775 : : /* Load the config of each pending import -- we must assign
21776 : : module numbers monotonically. */
21777 : 1860 : for (unsigned ix = 0; ix != pending_imports->length (); ix++)
21778 : : {
21779 : 1022 : auto *import = (*pending_imports)[ix];
21780 : 1197 : if (!(import->module_p
21781 : 175 : && (import->is_partition () || import->exported_p))
21782 : 853 : && import->loadedness == ML_NONE
21783 : 1875 : && (import->is_header () || !flag_preprocess_only))
21784 : : {
21785 : 844 : unsigned n = dump.push (import);
21786 : 844 : import->do_import (reader, true);
21787 : 844 : dump.pop (n);
21788 : : }
21789 : : }
21790 : 838 : vec_free (pending_imports);
21791 : :
21792 : : /* Restore the line-map state. */
21793 : 838 : spans.open (linemap_module_restore (line_table, pre_hwm));
21794 : :
21795 : : /* Now read the preprocessor state of this particular
21796 : : import. */
21797 : 838 : if (module->loadedness == ML_CONFIG
21798 : 838 : && module->read_preprocessor (true))
21799 : 835 : module->import_macros ();
21800 : :
21801 : 838 : timevar_stop (TV_MODULE_IMPORT);
21802 : :
21803 : 838 : dump.pop (n);
21804 : : }
21805 : : }
21806 : :
21807 : 5021 : return is_import ? NULL : get_primary (module);
21808 : : }
21809 : :
21810 : : /* We've completed phase-4 translation. Emit any dependency
21811 : : information for the not-yet-loaded direct imports, and fill in
21812 : : their file names. We'll have already loaded up the direct header
21813 : : unit wavefront. */
21814 : :
21815 : : void
21816 : 4155 : preprocessed_module (cpp_reader *reader)
21817 : : {
21818 : 4155 : unsigned n = dump.push (NULL);
21819 : :
21820 : 4663 : dump () && dump ("Completed phase-4 (tokenization) processing");
21821 : :
21822 : 4155 : name_pending_imports (reader);
21823 : 4152 : vec_free (pending_imports);
21824 : :
21825 : 4152 : spans.maybe_init ();
21826 : 4152 : spans.close ();
21827 : :
21828 : 4152 : using iterator = hash_table<module_state_hash>::iterator;
21829 : 4152 : if (mkdeps *deps = cpp_get_deps (reader))
21830 : : {
21831 : : /* Walk the module hash, informing the dependency machinery. */
21832 : 51 : iterator end = modules_hash->end ();
21833 : 294 : for (iterator iter = modules_hash->begin (); iter != end; ++iter)
21834 : : {
21835 : 96 : module_state *module = *iter;
21836 : :
21837 : 96 : if (module->is_direct ())
21838 : : {
21839 : 81 : if (module->is_module ()
21840 : 81 : && (module->is_interface () || module->is_partition ()))
21841 : 33 : deps_add_module_target (deps, module->get_flatname (),
21842 : 33 : maybe_add_cmi_prefix (module->filename),
21843 : 33 : module->is_header (),
21844 : 33 : module->is_exported ());
21845 : : else
21846 : 48 : deps_add_module_dep (deps, module->get_flatname ());
21847 : : }
21848 : : }
21849 : : }
21850 : :
21851 : 4152 : if (flag_header_unit && !flag_preprocess_only)
21852 : : {
21853 : : /* Find the main module -- remember, it's not yet in the module
21854 : : array. */
21855 : 850 : iterator end = modules_hash->end ();
21856 : 1808 : for (iterator iter = modules_hash->begin (); iter != end; ++iter)
21857 : : {
21858 : 904 : module_state *module = *iter;
21859 : 904 : if (module->is_module ())
21860 : : {
21861 : 850 : declare_module (module, cpp_main_loc (reader), true, NULL, reader);
21862 : 850 : module_kind |= MK_EXPORTING;
21863 : 850 : break;
21864 : : }
21865 : : }
21866 : : }
21867 : :
21868 : 4152 : dump.pop (n);
21869 : 4152 : }
21870 : :
21871 : : /* VAL is a global tree, add it to the global vec if it is
21872 : : interesting. Add some of its targets, if they too are
21873 : : interesting. We do not add identifiers, as they can be re-found
21874 : : via the identifier hash table. There is a cost to the number of
21875 : : global trees. */
21876 : :
21877 : : static int
21878 : 2570603 : maybe_add_global (tree val, unsigned &crc)
21879 : : {
21880 : 2570603 : int v = 0;
21881 : :
21882 : 2570603 : if (val && !(identifier_p (val) || TREE_VISITED (val)))
21883 : : {
21884 : 792263 : TREE_VISITED (val) = true;
21885 : 792263 : crc = crc32_unsigned (crc, fixed_trees->length ());
21886 : 792263 : vec_safe_push (fixed_trees, val);
21887 : 792263 : v++;
21888 : :
21889 : 792263 : if (CODE_CONTAINS_STRUCT (TREE_CODE (val), TS_TYPED))
21890 : 792263 : v += maybe_add_global (TREE_TYPE (val), crc);
21891 : 792263 : if (CODE_CONTAINS_STRUCT (TREE_CODE (val), TS_TYPE_COMMON))
21892 : 519375 : v += maybe_add_global (TYPE_NAME (val), crc);
21893 : : }
21894 : :
21895 : 2570603 : return v;
21896 : : }
21897 : :
21898 : : /* Initialize module state. Create the hash table, determine the
21899 : : global trees. Create the module for current TU. */
21900 : :
21901 : : void
21902 : 4155 : init_modules (cpp_reader *reader)
21903 : : {
21904 : : /* PCH should not be reachable because of lang-specs, but the
21905 : : user could have overriden that. */
21906 : 4155 : if (pch_file)
21907 : 0 : fatal_error (input_location,
21908 : : "C++ modules are incompatible with precompiled headers");
21909 : :
21910 : 4155 : if (cpp_get_options (reader)->traditional)
21911 : 0 : fatal_error (input_location,
21912 : : "C++ modules are incompatible with traditional preprocessing");
21913 : :
21914 : : /* :: is always exported. */
21915 : 4155 : DECL_MODULE_EXPORT_P (global_namespace) = true;
21916 : :
21917 : 4155 : modules_hash = hash_table<module_state_hash>::create_ggc (31);
21918 : 4155 : vec_safe_reserve (modules, 20);
21919 : :
21920 : : /* Create module for current TU. */
21921 : 4155 : module_state *current
21922 : 4155 : = new (ggc_alloc<module_state> ()) module_state (NULL_TREE, NULL, false);
21923 : 4155 : current->mod = 0;
21924 : 4155 : bitmap_set_bit (current->imports, 0);
21925 : 4155 : modules->quick_push (current);
21926 : :
21927 : 4155 : gcc_checking_assert (!fixed_trees);
21928 : :
21929 : 4155 : headers = BITMAP_GGC_ALLOC ();
21930 : :
21931 : 4155 : if (note_includes)
21932 : : /* Canonicalize header names. */
21933 : 2 : for (unsigned ix = 0; ix != note_includes->length (); ix++)
21934 : : {
21935 : 1 : const char *hdr = (*note_includes)[ix];
21936 : 1 : size_t len = strlen (hdr);
21937 : :
21938 : 1 : bool system = hdr[0] == '<';
21939 : 1 : bool user = hdr[0] == '"';
21940 : 1 : bool delimed = system || user;
21941 : :
21942 : 1 : if (len <= (delimed ? 2 : 0)
21943 : 1 : || (delimed && hdr[len-1] != (system ? '>' : '"')))
21944 : 0 : error ("invalid header name %qs", hdr);
21945 : :
21946 : 1 : hdr = canonicalize_header_name (delimed ? reader : NULL,
21947 : : 0, !delimed, hdr, len);
21948 : 1 : char *path = XNEWVEC (char, len + 1);
21949 : 1 : memcpy (path, hdr, len);
21950 : 1 : path[len] = 0;
21951 : :
21952 : 1 : (*note_includes)[ix] = path;
21953 : : }
21954 : :
21955 : 4155 : if (note_cmis)
21956 : : /* Canonicalize & mark module names. */
21957 : 12 : for (unsigned ix = 0; ix != note_cmis->length (); ix++)
21958 : : {
21959 : 6 : const char *name = (*note_cmis)[ix];
21960 : 6 : size_t len = strlen (name);
21961 : :
21962 : 6 : bool is_system = name[0] == '<';
21963 : 6 : bool is_user = name[0] == '"';
21964 : 6 : bool is_pathname = false;
21965 : 6 : if (!(is_system || is_user))
21966 : 12 : for (unsigned ix = len; !is_pathname && ix--;)
21967 : 9 : is_pathname = IS_DIR_SEPARATOR (name[ix]);
21968 : 6 : if (is_system || is_user || is_pathname)
21969 : : {
21970 : 3 : if (len <= (is_pathname ? 0 : 2)
21971 : 3 : || (!is_pathname && name[len-1] != (is_system ? '>' : '"')))
21972 : : {
21973 : 0 : error ("invalid header name %qs", name);
21974 : 0 : continue;
21975 : : }
21976 : : else
21977 : 3 : name = canonicalize_header_name (is_pathname ? nullptr : reader,
21978 : : 0, is_pathname, name, len);
21979 : : }
21980 : 6 : if (auto module = get_module (name))
21981 : 6 : module->inform_cmi_p = 1;
21982 : : else
21983 : 0 : error ("invalid module name %qs", name);
21984 : : }
21985 : :
21986 : 4155 : dump.push (NULL);
21987 : :
21988 : : /* Determine lazy handle bound. */
21989 : 4155 : {
21990 : 4155 : unsigned limit = 1000;
21991 : : #if HAVE_GETRLIMIT
21992 : 4155 : struct rlimit rlimit;
21993 : 4155 : if (!getrlimit (RLIMIT_NOFILE, &rlimit))
21994 : : {
21995 : 4155 : lazy_hard_limit = (rlimit.rlim_max < 1000000
21996 : 4155 : ? unsigned (rlimit.rlim_max) : 1000000);
21997 : 4155 : lazy_hard_limit = (lazy_hard_limit > LAZY_HEADROOM
21998 : 4155 : ? lazy_hard_limit - LAZY_HEADROOM : 0);
21999 : 4155 : if (rlimit.rlim_cur < limit)
22000 : 0 : limit = unsigned (rlimit.rlim_cur);
22001 : : }
22002 : : #endif
22003 : 4155 : limit = limit > LAZY_HEADROOM ? limit - LAZY_HEADROOM : 1;
22004 : :
22005 : 4155 : if (unsigned parm = param_lazy_modules)
22006 : : {
22007 : 4155 : if (parm <= limit || !lazy_hard_limit || !try_increase_lazy (parm))
22008 : 6 : lazy_limit = parm;
22009 : : }
22010 : : else
22011 : 0 : lazy_limit = limit;
22012 : : }
22013 : :
22014 : 4155 : if (dump ())
22015 : : {
22016 : 508 : verstr_t ver;
22017 : 508 : version2string (MODULE_VERSION, ver);
22018 : 508 : dump ("Source: %s", main_input_filename);
22019 : 508 : dump ("Compiler: %s", version_string);
22020 : 508 : dump ("Modules: %s", ver);
22021 : 508 : dump ("Checking: %s",
22022 : : #if CHECKING_P
22023 : : "checking"
22024 : : #elif ENABLE_ASSERT_CHECKING
22025 : : "asserting"
22026 : : #else
22027 : : "release"
22028 : : #endif
22029 : : );
22030 : 508 : dump ("Compiled by: "
22031 : : #ifdef __GNUC__
22032 : : "GCC %d.%d, %s", __GNUC__, __GNUC_MINOR__,
22033 : : #ifdef __OPTIMIZE__
22034 : : "optimizing"
22035 : : #else
22036 : : "not optimizing"
22037 : : #endif
22038 : : #else
22039 : : "not GCC"
22040 : : #endif
22041 : : );
22042 : 508 : dump ("Reading: %s", MAPPED_READING ? "mmap" : "fileio");
22043 : 508 : dump ("Writing: %s", MAPPED_WRITING ? "mmap" : "fileio");
22044 : 508 : dump ("Lazy limit: %u", lazy_limit);
22045 : 508 : dump ("Lazy hard limit: %u", lazy_hard_limit);
22046 : 508 : dump ("");
22047 : : }
22048 : :
22049 : : /* Construct the global tree array. This is an array of unique
22050 : : global trees (& types). Do this now, rather than lazily, as
22051 : : some global trees are lazily created and we don't want that to
22052 : : mess with our syndrome of fixed trees. */
22053 : 4155 : unsigned crc = 0;
22054 : 4155 : vec_alloc (fixed_trees, 250);
22055 : :
22056 : 4663 : dump () && dump ("+Creating globals");
22057 : : /* Insert the TRANSLATION_UNIT_DECL. */
22058 : 4155 : TREE_VISITED (DECL_CONTEXT (global_namespace)) = true;
22059 : 4155 : fixed_trees->quick_push (DECL_CONTEXT (global_namespace));
22060 : 24930 : for (unsigned jx = 0; global_tree_arys[jx].first; jx++)
22061 : : {
22062 : 20775 : const tree *ptr = global_tree_arys[jx].first;
22063 : 20775 : unsigned limit = global_tree_arys[jx].second;
22064 : :
22065 : 1267275 : for (unsigned ix = 0; ix != limit; ix++, ptr++)
22066 : : {
22067 : 1246500 : !(ix & 31) && dump ("") && dump ("+\t%u:%u:", jx, ix);
22068 : 1246500 : unsigned v = maybe_add_global (*ptr, crc);
22069 : 1398900 : dump () && dump ("+%u", v);
22070 : : }
22071 : : }
22072 : : /* OS- and machine-specific types are dynamically registered at
22073 : : runtime, so cannot be part of global_tree_arys. */
22074 : 4155 : registered_builtin_types && dump ("") && dump ("+\tB:");
22075 : 16620 : for (tree t = registered_builtin_types; t; t = TREE_CHAIN (t))
22076 : : {
22077 : 12465 : unsigned v = maybe_add_global (TREE_VALUE (t), crc);
22078 : 13989 : dump () && dump ("+%u", v);
22079 : : }
22080 : 4155 : global_crc = crc32_unsigned (crc, fixed_trees->length ());
22081 : 4155 : dump ("") && dump ("Created %u unique globals, crc=%x",
22082 : : fixed_trees->length (), global_crc);
22083 : 800573 : for (unsigned ix = fixed_trees->length (); ix--;)
22084 : 796418 : TREE_VISITED ((*fixed_trees)[ix]) = false;
22085 : :
22086 : 4155 : dump.pop (0);
22087 : :
22088 : 4155 : if (!flag_module_lazy)
22089 : : /* Get the mapper now, if we're not being lazy. */
22090 : 270 : get_mapper (cpp_main_loc (reader), cpp_get_deps (reader));
22091 : :
22092 : 4155 : if (!flag_preprocess_only)
22093 : : {
22094 : 4044 : pending_table = new pending_map_t (EXPERIMENT (1, 400));
22095 : 4044 : entity_map = new entity_map_t (EXPERIMENT (1, 400));
22096 : 4044 : vec_safe_reserve (entity_ary, EXPERIMENT (1, 400));
22097 : 4044 : imported_temploid_friends
22098 : 4044 : = decl_tree_cache_map::create_ggc (EXPERIMENT (1, 400));
22099 : : }
22100 : :
22101 : : #if CHECKING_P
22102 : 4155 : note_defs = note_defs_table_t::create_ggc (1000);
22103 : : #endif
22104 : :
22105 : 4155 : if (flag_header_unit && cpp_get_options (reader)->preprocessed)
22106 : 9 : begin_header_unit (reader);
22107 : :
22108 : : /* Collect here to make sure things are tagged correctly (when
22109 : : aggressively GC'd). */
22110 : 4155 : ggc_collect ();
22111 : 4155 : }
22112 : :
22113 : : /* If NODE is a deferred macro, load it. */
22114 : :
22115 : : static int
22116 : 79705 : load_macros (cpp_reader *reader, cpp_hashnode *node, void *)
22117 : : {
22118 : 79705 : location_t main_loc
22119 : 79705 : = MAP_START_LOCATION (LINEMAPS_ORDINARY_MAP_AT (line_table, 0));
22120 : :
22121 : 79705 : if (cpp_user_macro_p (node)
22122 : 79705 : && !node->value.macro)
22123 : : {
22124 : 72 : cpp_macro *macro = cpp_get_deferred_macro (reader, node, main_loc);
22125 : 72 : dump () && dump ("Loaded macro #%s %I",
22126 : : macro ? "define" : "undef", identifier (node));
22127 : : }
22128 : :
22129 : 79705 : return 1;
22130 : : }
22131 : :
22132 : : /* At the end of tokenizing, we no longer need the macro tables of
22133 : : imports. But the user might have requested some checking. */
22134 : :
22135 : : void
22136 : 92836 : maybe_check_all_macros (cpp_reader *reader)
22137 : : {
22138 : 92836 : if (!warn_imported_macros)
22139 : : return;
22140 : :
22141 : : /* Force loading of any remaining deferred macros. This will
22142 : : produce diagnostics if they are ill-formed. */
22143 : 21 : unsigned n = dump.push (NULL);
22144 : 21 : cpp_forall_identifiers (reader, load_macros, NULL);
22145 : 21 : dump.pop (n);
22146 : : }
22147 : :
22148 : : // State propagated from finish_module_processing to fini_modules
22149 : :
22150 : : struct module_processing_cookie
22151 : : {
22152 : : elf_out out;
22153 : : module_state_config config;
22154 : : char *cmi_name;
22155 : : char *tmp_name;
22156 : : unsigned crc;
22157 : : bool began;
22158 : :
22159 : 2436 : module_processing_cookie (char *cmi, char *tmp, int fd, int e)
22160 : 2436 : : out (fd, e), cmi_name (cmi), tmp_name (tmp), crc (0), began (false)
22161 : : {
22162 : : }
22163 : 2436 : ~module_processing_cookie ()
22164 : : {
22165 : 2436 : XDELETEVEC (tmp_name);
22166 : 2436 : XDELETEVEC (cmi_name);
22167 : 2436 : }
22168 : : };
22169 : :
22170 : : /* Write the CMI, if we're a module interface. */
22171 : :
22172 : : void *
22173 : 92636 : finish_module_processing (cpp_reader *reader)
22174 : : {
22175 : 92636 : module_processing_cookie *cookie = nullptr;
22176 : :
22177 : 92636 : if (header_module_p ())
22178 : 850 : module_kind &= ~MK_EXPORTING;
22179 : :
22180 : 92636 : if (!modules || !(*modules)[0]->name)
22181 : : {
22182 : 90197 : if (flag_module_only)
22183 : 6 : warning (0, "%<-fmodule-only%> used for non-interface");
22184 : : }
22185 : 2439 : else if (!flag_syntax_only)
22186 : : {
22187 : 2436 : int fd = -1;
22188 : 2436 : int e = -1;
22189 : :
22190 : 2436 : timevar_start (TV_MODULE_EXPORT);
22191 : :
22192 : : /* Force a valid but empty line map at the end. This simplifies
22193 : : the line table preparation and writing logic. */
22194 : 2436 : linemap_add (line_table, LC_ENTER, false, "", 0);
22195 : :
22196 : : /* We write to a tmpname, and then atomically rename. */
22197 : 2436 : char *cmi_name = NULL;
22198 : 2436 : char *tmp_name = NULL;
22199 : 2436 : module_state *state = (*modules)[0];
22200 : :
22201 : 2436 : unsigned n = dump.push (state);
22202 : 2436 : state->announce ("creating");
22203 : 2436 : if (state->filename)
22204 : : {
22205 : 2436 : size_t len = 0;
22206 : 2436 : cmi_name = xstrdup (maybe_add_cmi_prefix (state->filename, &len));
22207 : 2436 : tmp_name = XNEWVEC (char, len + 3);
22208 : 2436 : memcpy (tmp_name, cmi_name, len);
22209 : 2436 : strcpy (&tmp_name[len], "~");
22210 : :
22211 : 2436 : if (!errorcount)
22212 : 41 : for (unsigned again = 2; ; again--)
22213 : : {
22214 : 2374 : fd = open (tmp_name,
22215 : : O_RDWR | O_CREAT | O_TRUNC | O_CLOEXEC | O_BINARY,
22216 : : S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH);
22217 : 2374 : e = errno;
22218 : 2374 : if (fd >= 0 || !again || e != ENOENT)
22219 : : break;
22220 : 41 : create_dirs (tmp_name);
22221 : : }
22222 : 2436 : if (note_module_cmi_yes || state->inform_cmi_p)
22223 : 3 : inform (state->loc, "writing CMI %qs", cmi_name);
22224 : 2684 : dump () && dump ("CMI is %s", cmi_name);
22225 : : }
22226 : :
22227 : 2436 : cookie = new module_processing_cookie (cmi_name, tmp_name, fd, e);
22228 : :
22229 : 2436 : if (errorcount)
22230 : : /* Don't write the module if we have reported errors. */;
22231 : 2333 : else if (erroneous_templates
22232 : 2333 : && !erroneous_templates->is_empty ())
22233 : : {
22234 : : /* Don't write the module if it contains an erroneous template.
22235 : : Also emit notes about where errors occurred in case
22236 : : -Wno-template-body was passed. */
22237 : 6 : auto_diagnostic_group d;
22238 : 6 : error_at (state->loc, "not writing module %qs due to errors "
22239 : : "in template bodies", state->get_flatname ());
22240 : 6 : if (!warn_template_body)
22241 : 3 : inform (state->loc, "enable %<-Wtemplate-body%> for more details");
22242 : 12 : for (auto e : *erroneous_templates)
22243 : 6 : inform (e.second, "first error in %qD appeared here", e.first);
22244 : 6 : }
22245 : 2327 : else if (cookie->out.begin ())
22246 : : {
22247 : : /* So crashes finger-point the module decl. */
22248 : 2327 : iloc_sentinel ils = state->loc;
22249 : 2327 : if (state->write_begin (&cookie->out, reader, cookie->config,
22250 : 2327 : cookie->crc))
22251 : 2302 : cookie->began = true;
22252 : 2327 : }
22253 : :
22254 : 2436 : dump.pop (n);
22255 : 2436 : timevar_stop (TV_MODULE_EXPORT);
22256 : :
22257 : 2436 : ggc_collect ();
22258 : : }
22259 : :
22260 : 92636 : if (modules)
22261 : : {
22262 : 4007 : unsigned n = dump.push (NULL);
22263 : 4515 : dump () && dump ("Imported %u modules", modules->length () - 1);
22264 : 4515 : dump () && dump ("Containing %u clusters", available_clusters);
22265 : 4007 : dump () && dump ("Loaded %u clusters (%u%%)", loaded_clusters,
22266 : 508 : (loaded_clusters * 100 + available_clusters / 2) /
22267 : 508 : (available_clusters + !available_clusters));
22268 : 4007 : dump.pop (n);
22269 : : }
22270 : :
22271 : 92636 : return cookie;
22272 : : }
22273 : :
22274 : : // Do the final emission of a module. At this point we know whether
22275 : : // the module static initializer is a NOP or not.
22276 : :
22277 : : static void
22278 : 2436 : late_finish_module (cpp_reader *reader, module_processing_cookie *cookie,
22279 : : bool init_fn_non_empty)
22280 : : {
22281 : 2436 : timevar_start (TV_MODULE_EXPORT);
22282 : :
22283 : 2436 : module_state *state = (*modules)[0];
22284 : 2436 : unsigned n = dump.push (state);
22285 : 2436 : state->announce ("finishing");
22286 : :
22287 : 2436 : cookie->config.active_init = init_fn_non_empty;
22288 : 2436 : if (cookie->began)
22289 : 2302 : state->write_end (&cookie->out, reader, cookie->config, cookie->crc);
22290 : :
22291 : 2436 : if (cookie->out.end () && cookie->cmi_name)
22292 : : {
22293 : : /* Some OS's do not replace NEWNAME if it already exists.
22294 : : This'll have a race condition in erroneous concurrent
22295 : : builds. */
22296 : 2333 : unlink (cookie->cmi_name);
22297 : 2333 : if (rename (cookie->tmp_name, cookie->cmi_name))
22298 : : {
22299 : 0 : dump () && dump ("Rename ('%s','%s') errno=%u",
22300 : 0 : cookie->tmp_name, cookie->cmi_name, errno);
22301 : 0 : cookie->out.set_error (errno);
22302 : : }
22303 : : }
22304 : :
22305 : 2436 : if (cookie->out.get_error () && cookie->began)
22306 : : {
22307 : 0 : error_at (state->loc, "failed to write compiled module: %s",
22308 : 0 : cookie->out.get_error (state->filename));
22309 : 0 : state->note_cmi_name ();
22310 : : }
22311 : :
22312 : 2436 : if (!errorcount)
22313 : : {
22314 : 2299 : auto *mapper = get_mapper (cpp_main_loc (reader), cpp_get_deps (reader));
22315 : 2299 : mapper->ModuleCompiled (state->get_flatname ());
22316 : : }
22317 : 137 : else if (cookie->cmi_name)
22318 : : {
22319 : : /* We failed, attempt to erase all evidence we even tried. */
22320 : 137 : unlink (cookie->tmp_name);
22321 : 137 : unlink (cookie->cmi_name);
22322 : : }
22323 : :
22324 : 2436 : delete cookie;
22325 : 2436 : dump.pop (n);
22326 : 2436 : timevar_stop (TV_MODULE_EXPORT);
22327 : 2436 : }
22328 : :
22329 : : void
22330 : 92636 : fini_modules (cpp_reader *reader, void *cookie, bool has_inits)
22331 : : {
22332 : 92636 : if (cookie)
22333 : 2436 : late_finish_module (reader,
22334 : : static_cast<module_processing_cookie *> (cookie),
22335 : : has_inits);
22336 : :
22337 : : /* We're done with the macro tables now. */
22338 : 92636 : vec_free (macro_exports);
22339 : 92636 : vec_free (macro_imports);
22340 : 92636 : headers = NULL;
22341 : :
22342 : : /* We're now done with everything but the module names. */
22343 : 92636 : set_cmi_repo (NULL);
22344 : 92636 : if (mapper)
22345 : : {
22346 : 4007 : timevar_start (TV_MODULE_MAPPER);
22347 : 4007 : module_client::close_module_client (0, mapper);
22348 : 4007 : mapper = nullptr;
22349 : 4007 : timevar_stop (TV_MODULE_MAPPER);
22350 : : }
22351 : 92636 : module_state_config::release ();
22352 : :
22353 : : #if CHECKING_P
22354 : 92636 : note_defs = NULL;
22355 : : #endif
22356 : :
22357 : 92636 : if (modules)
22358 : 6471 : for (unsigned ix = modules->length (); --ix;)
22359 : 2464 : if (module_state *state = (*modules)[ix])
22360 : 2464 : state->release ();
22361 : :
22362 : : /* No need to lookup modules anymore. */
22363 : 92636 : modules_hash = NULL;
22364 : :
22365 : : /* Or entity array. We still need the entity map to find import numbers. */
22366 : 92636 : vec_free (entity_ary);
22367 : 92636 : entity_ary = NULL;
22368 : :
22369 : : /* Or remember any pending entities. */
22370 : 96643 : delete pending_table;
22371 : 92636 : pending_table = NULL;
22372 : :
22373 : : /* Or any keys -- Let it go! */
22374 : 94646 : delete keyed_table;
22375 : 92636 : keyed_table = NULL;
22376 : :
22377 : : /* Allow a GC, we've possibly made much data unreachable. */
22378 : 92636 : ggc_collect ();
22379 : 92636 : }
22380 : :
22381 : : /* If CODE is a module option, handle it & return true. Otherwise
22382 : : return false. For unknown reasons I cannot get the option
22383 : : generation machinery to set fmodule-mapper or -fmodule-header to
22384 : : make a string type option variable. */
22385 : :
22386 : : bool
22387 : 1826940 : handle_module_option (unsigned code, const char *str, int)
22388 : : {
22389 : 1826940 : auto hdr = CMS_header;
22390 : :
22391 : 1826940 : switch (opt_code (code))
22392 : : {
22393 : 42 : case OPT_fmodule_mapper_:
22394 : 42 : module_mapper_name = str;
22395 : 42 : return true;
22396 : :
22397 : 11 : case OPT_fmodule_header_:
22398 : 11 : {
22399 : 11 : if (!strcmp (str, "user"))
22400 : : hdr = CMS_user;
22401 : 11 : else if (!strcmp (str, "system"))
22402 : : hdr = CMS_system;
22403 : : else
22404 : 0 : error ("unknown header kind %qs", str);
22405 : : }
22406 : : /* Fallthrough. */
22407 : :
22408 : 865 : case OPT_fmodule_header:
22409 : 865 : flag_header_unit = hdr;
22410 : 865 : flag_modules = 1;
22411 : 865 : return true;
22412 : :
22413 : 1 : case OPT_flang_info_include_translate_:
22414 : 1 : vec_safe_push (note_includes, str);
22415 : 1 : return true;
22416 : :
22417 : 6 : case OPT_flang_info_module_cmi_:
22418 : 6 : vec_safe_push (note_cmis, str);
22419 : 6 : return true;
22420 : :
22421 : : default:
22422 : : return false;
22423 : : }
22424 : : }
22425 : :
22426 : : /* Set preprocessor callbacks and options for modules. */
22427 : :
22428 : : void
22429 : 94248 : module_preprocess_options (cpp_reader *reader)
22430 : : {
22431 : 94248 : gcc_checking_assert (!lang_hooks.preprocess_undef);
22432 : 94248 : if (modules_p ())
22433 : : {
22434 : 4155 : auto *cb = cpp_get_callbacks (reader);
22435 : :
22436 : 4155 : cb->translate_include = maybe_translate_include;
22437 : 4155 : cb->user_deferred_macro = module_state::deferred_macro;
22438 : 4155 : if (flag_header_unit)
22439 : : {
22440 : : /* If the preprocessor hook is already in use, that
22441 : : implementation will call the undef langhook. */
22442 : 862 : if (cb->undef)
22443 : 0 : lang_hooks.preprocess_undef = module_state::undef_macro;
22444 : : else
22445 : 862 : cb->undef = module_state::undef_macro;
22446 : : }
22447 : 4155 : auto *opt = cpp_get_options (reader);
22448 : 4155 : opt->module_directives = true;
22449 : 4155 : if (flag_no_output)
22450 : 12 : opt->directives_only = true;
22451 : 4155 : if (opt->main_search == CMS_none)
22452 : 4155 : opt->main_search = cpp_main_search (flag_header_unit);
22453 : : }
22454 : 94248 : }
22455 : :
22456 : : #include "gt-cp-module.h"
|