Branch data Line data Source code
1 : : /* C++ modules. Experimental!
2 : : Copyright (C) 2017-2024 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_MEMORY
212 : : #define INCLUDE_STRING
213 : : #define INCLUDE_VECTOR
214 : : #include "system.h"
215 : : #include "coretypes.h"
216 : : #include "cp-tree.h"
217 : : #include "timevar.h"
218 : : #include "stringpool.h"
219 : : #include "dumpfile.h"
220 : : #include "bitmap.h"
221 : : #include "cgraph.h"
222 : : #include "varasm.h"
223 : : #include "tree-iterator.h"
224 : : #include "cpplib.h"
225 : : #include "mkdeps.h"
226 : : #include "incpath.h"
227 : : #include "libiberty.h"
228 : : #include "stor-layout.h"
229 : : #include "version.h"
230 : : #include "tree-diagnostic.h"
231 : : #include "toplev.h"
232 : : #include "opts.h"
233 : : #include "attribs.h"
234 : : #include "intl.h"
235 : : #include "langhooks.h"
236 : : /* This TU doesn't need or want to see the networking. */
237 : : #define CODY_NETWORKING 0
238 : : #include "mapper-client.h"
239 : : #include <zlib.h> // for crc32, crc32_combine
240 : :
241 : : #if 0 // 1 for testing no mmap
242 : : #define MAPPED_READING 0
243 : : #define MAPPED_WRITING 0
244 : : #else
245 : : #if HAVE_MMAP_FILE && _POSIX_MAPPED_FILES > 0
246 : : /* mmap, munmap. */
247 : : #define MAPPED_READING 1
248 : : #if HAVE_SYSCONF && defined (_SC_PAGE_SIZE)
249 : : /* msync, sysconf (_SC_PAGE_SIZE), ftruncate */
250 : : /* posix_fallocate used if available. */
251 : : #define MAPPED_WRITING 1
252 : : #else
253 : : #define MAPPED_WRITING 0
254 : : #endif
255 : : #else
256 : : #define MAPPED_READING 0
257 : : #define MAPPED_WRITING 0
258 : : #endif
259 : : #endif
260 : :
261 : : /* Some open(2) flag differences, what a colourful world it is! */
262 : : #if defined (O_CLOEXEC)
263 : : // OK
264 : : #elif defined (_O_NOINHERIT)
265 : : /* Windows' _O_NOINHERIT matches O_CLOEXEC flag */
266 : : #define O_CLOEXEC _O_NOINHERIT
267 : : #else
268 : : #define O_CLOEXEC 0
269 : : #endif
270 : : #if defined (O_BINARY)
271 : : // Ok?
272 : : #elif defined (_O_BINARY)
273 : : /* Windows' open(2) call defaults to text! */
274 : : #define O_BINARY _O_BINARY
275 : : #else
276 : : #define O_BINARY 0
277 : : #endif
278 : :
279 : 323431 : static inline cpp_hashnode *cpp_node (tree id)
280 : : {
281 : 323431 : return CPP_HASHNODE (GCC_IDENT_TO_HT_IDENT (id));
282 : : }
283 : :
284 : 230880 : static inline tree identifier (const cpp_hashnode *node)
285 : : {
286 : : /* HT_NODE() expands to node->ident that HT_IDENT_TO_GCC_IDENT()
287 : : then subtracts a nonzero constant, deriving a pointer to
288 : : a different member than ident. That's strictly undefined
289 : : and detected by -Warray-bounds. Suppress it. See PR 101372. */
290 : 230880 : #pragma GCC diagnostic push
291 : 230880 : #pragma GCC diagnostic ignored "-Warray-bounds"
292 : 230880 : return HT_IDENT_TO_GCC_IDENT (HT_NODE (const_cast<cpp_hashnode *> (node)));
293 : 230880 : #pragma GCC diagnostic pop
294 : : }
295 : :
296 : : /* Id for dumping module information. */
297 : : int module_dump_id;
298 : :
299 : : /* We have a special module owner. */
300 : : #define MODULE_UNKNOWN (~0U) /* Not yet known. */
301 : :
302 : : /* Prefix for section names. */
303 : : #define MOD_SNAME_PFX ".gnu.c++"
304 : :
305 : : /* Format a version for user consumption. */
306 : :
307 : : typedef char verstr_t[32];
308 : : static void
309 : 2980 : version2string (unsigned version, verstr_t &out)
310 : : {
311 : 2980 : unsigned major = MODULE_MAJOR (version);
312 : 2980 : unsigned minor = MODULE_MINOR (version);
313 : :
314 : 2980 : if (IS_EXPERIMENTAL (version))
315 : 2980 : sprintf (out, "%04u/%02u/%02u-%02u:%02u%s",
316 : 2980 : 2000 + major / 10000, (major / 100) % 100, (major % 100),
317 : : minor / 100, minor % 100,
318 : : EXPERIMENT ("", " (experimental)"));
319 : : else
320 : 0 : sprintf (out, "%u.%u", major, minor);
321 : 2980 : }
322 : :
323 : : /* Include files to note translation for. */
324 : : static vec<const char *, va_heap, vl_embed> *note_includes;
325 : :
326 : : /* Modules to note CMI pathames. */
327 : : static vec<const char *, va_heap, vl_embed> *note_cmis;
328 : :
329 : : /* Traits to hash an arbitrary pointer. Entries are not deletable,
330 : : and removal is a noop (removal needed upon destruction). */
331 : : template <typename T>
332 : : struct nodel_ptr_hash : pointer_hash<T>, typed_noop_remove <T *> {
333 : : /* Nothing is deletable. Everything is insertable. */
334 : : static bool is_deleted (T *) { return false; }
335 : : static void mark_deleted (T *) { gcc_unreachable (); }
336 : : };
337 : :
338 : : /* Map from pointer to signed integer. */
339 : : typedef simple_hashmap_traits<nodel_ptr_hash<void>, int> ptr_int_traits;
340 : : typedef hash_map<void *,signed,ptr_int_traits> ptr_int_hash_map;
341 : :
342 : : /********************************************************************/
343 : : /* Basic streaming & ELF. Serialization is usually via mmap. For
344 : : writing we slide a buffer over the output file, syncing it
345 : : approproiately. For reading we simply map the whole file (as a
346 : : file-backed read-only map -- it's just address space, leaving the
347 : : OS pager to deal with getting the data to us). Some buffers need
348 : : to be more conventional malloc'd contents. */
349 : :
350 : : /* Variable length buffer. */
351 : :
352 : : namespace {
353 : : class data {
354 : : public:
355 : 1894 : class allocator {
356 : : public:
357 : : /* Tools tend to moan if the dtor's not virtual. */
358 : 102610 : virtual ~allocator () {}
359 : :
360 : : public:
361 : : void grow (data &obj, unsigned needed, bool exact);
362 : : void shrink (data &obj);
363 : :
364 : : public:
365 : : virtual char *grow (char *ptr, unsigned needed);
366 : : virtual void shrink (char *ptr);
367 : : };
368 : :
369 : : public:
370 : : char *buffer; /* Buffer being transferred. */
371 : : /* Although size_t would be the usual size, we know we never get
372 : : more than 4GB of buffer -- because that's the limit of the
373 : : encapsulation format. And if you need bigger imports, you're
374 : : doing it wrong. */
375 : : unsigned size; /* Allocated size of buffer. */
376 : : unsigned pos; /* Position in buffer. */
377 : :
378 : : public:
379 : 1111796 : data ()
380 : 1111796 : :buffer (NULL), size (0), pos (0)
381 : : {
382 : : }
383 : 1121420 : ~data ()
384 : : {
385 : : /* Make sure the derived and/or using class know what they're
386 : : doing. */
387 : 1121420 : gcc_checking_assert (!buffer);
388 : 1121420 : }
389 : :
390 : : protected:
391 : 768966304 : char *use (unsigned count)
392 : : {
393 : 768966304 : if (size < pos + count)
394 : : return NULL;
395 : 768966304 : char *res = &buffer[pos];
396 : 768966304 : pos += count;
397 : 373887135 : return res;
398 : : }
399 : :
400 : : unsigned calc_crc (unsigned) const;
401 : :
402 : : public:
403 : 56721344 : void unuse (unsigned count)
404 : : {
405 : 56721344 : pos -= count;
406 : 18667 : }
407 : :
408 : : public:
409 : : static allocator simple_memory;
410 : : };
411 : : } // anon namespace
412 : :
413 : : /* The simple data allocator. */
414 : : data::allocator data::simple_memory;
415 : :
416 : : /* Grow buffer to at least size NEEDED. */
417 : :
418 : : void
419 : 894616 : data::allocator::grow (data &obj, unsigned needed, bool exact)
420 : : {
421 : 894616 : gcc_checking_assert (needed ? needed > obj.size : !obj.size);
422 : 894616 : if (!needed)
423 : : /* Pick a default size. */
424 : 376135 : needed = EXPERIMENT (100, 1000);
425 : :
426 : 894616 : if (!exact)
427 : 889144 : needed *= 2;
428 : 894616 : obj.buffer = grow (obj.buffer, needed);
429 : 894616 : if (obj.buffer)
430 : 894616 : obj.size = needed;
431 : : else
432 : 0 : obj.pos = obj.size = 0;
433 : 894616 : }
434 : :
435 : : /* Free a buffer. */
436 : :
437 : : void
438 : 385605 : data::allocator::shrink (data &obj)
439 : : {
440 : 0 : shrink (obj.buffer);
441 : 385605 : obj.buffer = NULL;
442 : 385605 : obj.size = 0;
443 : 0 : }
444 : :
445 : : char *
446 : 7647 : data::allocator::grow (char *ptr, unsigned needed)
447 : : {
448 : 7647 : return XRESIZEVAR (char, ptr, needed);
449 : : }
450 : :
451 : : void
452 : 9470 : data::allocator::shrink (char *ptr)
453 : : {
454 : 9470 : XDELETEVEC (ptr);
455 : 9470 : }
456 : :
457 : : /* Calculate the crc32 of the buffer. Note the CRC is stored in the
458 : : first 4 bytes, so don't include them. */
459 : :
460 : : unsigned
461 : 741118 : data::calc_crc (unsigned l) const
462 : : {
463 : 741118 : return crc32 (0, (unsigned char *)buffer + 4, l - 4);
464 : : }
465 : :
466 : : class elf_in;
467 : :
468 : : /* Byte stream reader. */
469 : :
470 : : namespace {
471 : : class bytes_in : public data {
472 : : typedef data parent;
473 : :
474 : : protected:
475 : : bool overrun; /* Sticky read-too-much flag. */
476 : :
477 : : public:
478 : 371595 : bytes_in ()
479 : 371595 : : parent (), overrun (false)
480 : : {
481 : : }
482 : 373520 : ~bytes_in ()
483 : : {
484 : 8678 : }
485 : :
486 : : public:
487 : : /* Begin reading a named section. */
488 : : bool begin (location_t loc, elf_in *src, const char *name);
489 : : /* Begin reading a numbered section with optional name. */
490 : : bool begin (location_t loc, elf_in *src, unsigned, const char * = NULL);
491 : : /* Complete reading a buffer. Propagate errors and return true on
492 : : success. */
493 : : bool end (elf_in *src);
494 : : /* Return true if there is unread data. */
495 : 2751791 : bool more_p () const
496 : : {
497 : 2751791 : return pos != size;
498 : : }
499 : :
500 : : public:
501 : : /* Start reading at OFFSET. */
502 : 572 : void random_access (unsigned offset)
503 : : {
504 : 572 : if (offset > size)
505 : 0 : set_overrun ();
506 : 572 : pos = offset;
507 : : }
508 : :
509 : : public:
510 : 2322612 : void align (unsigned boundary)
511 : : {
512 : 2322612 : if (unsigned pad = pos & (boundary - 1))
513 : 4496862 : read (boundary - pad);
514 : : }
515 : :
516 : : public:
517 : 395079169 : const char *read (unsigned count)
518 : : {
519 : 2174250 : char *ptr = use (count);
520 : 395079169 : if (!ptr)
521 : 0 : set_overrun ();
522 : 323673954 : return ptr;
523 : : }
524 : :
525 : : public:
526 : : bool check_crc () const;
527 : : /* We store the CRC in the first 4 bytes, using host endianness. */
528 : 372484 : unsigned get_crc () const
529 : : {
530 : 372484 : return *(const unsigned *)&buffer[0];
531 : : }
532 : :
533 : : public:
534 : : /* Manipulate the overrun flag. */
535 : 227085257 : bool get_overrun () const
536 : : {
537 : 227085257 : return overrun;
538 : : }
539 : 14 : void set_overrun ()
540 : : {
541 : 14 : overrun = true;
542 : 0 : }
543 : :
544 : : public:
545 : : unsigned u32 (); /* Read uncompressed integer. */
546 : :
547 : : public:
548 : : int c () ATTRIBUTE_UNUSED; /* Read a char. */
549 : : int i (); /* Read a signed int. */
550 : : unsigned u (); /* Read an unsigned int. */
551 : : size_t z (); /* Read a size_t. */
552 : : HOST_WIDE_INT wi (); /* Read a HOST_WIDE_INT. */
553 : : unsigned HOST_WIDE_INT wu (); /* Read an unsigned HOST_WIDE_INT. */
554 : : const char *str (size_t * = NULL); /* Read a string. */
555 : : const void *buf (size_t); /* Read a fixed-length buffer. */
556 : : cpp_hashnode *cpp_node (); /* Read a cpp node. */
557 : :
558 : : struct bits_in;
559 : : bits_in stream_bits ();
560 : : };
561 : : } // anon namespace
562 : :
563 : : /* Verify the buffer's CRC is correct. */
564 : :
565 : : bool
566 : 370455 : bytes_in::check_crc () const
567 : : {
568 : 370455 : if (size < 4)
569 : : return false;
570 : :
571 : 370455 : unsigned c_crc = calc_crc (size);
572 : 370455 : if (c_crc != get_crc ())
573 : : return false;
574 : :
575 : : return true;
576 : : }
577 : :
578 : : class elf_out;
579 : :
580 : : /* Byte stream writer. */
581 : :
582 : : namespace {
583 : : class bytes_out : public data {
584 : : typedef data parent;
585 : :
586 : : public:
587 : : allocator *memory; /* Obtainer of memory. */
588 : :
589 : : public:
590 : 736260 : bytes_out (allocator *memory)
591 : 736260 : : parent (), memory (memory)
592 : : {
593 : : }
594 : 736260 : ~bytes_out ()
595 : : {
596 : 752250 : }
597 : :
598 : : public:
599 : 780169304 : bool streaming_p () const
600 : : {
601 : 780169304 : return memory != NULL;
602 : : }
603 : :
604 : : public:
605 : : void set_crc (unsigned *crc_ptr);
606 : :
607 : : public:
608 : : /* Begin writing, maybe reserve space for CRC. */
609 : : void begin (bool need_crc = true);
610 : : /* Finish writing. Spill to section by number. */
611 : : unsigned end (elf_out *, unsigned, unsigned *crc_ptr = NULL);
612 : :
613 : : public:
614 : 2429735 : void align (unsigned boundary)
615 : : {
616 : 2429735 : if (unsigned pad = pos & (boundary - 1))
617 : 2279166 : write (boundary - pad);
618 : 2429735 : }
619 : :
620 : : public:
621 : 373887135 : char *write (unsigned count, bool exact = false)
622 : : {
623 : 373887135 : if (size < pos + count)
624 : 509010 : memory->grow (*this, pos + count, exact);
625 : 373887135 : return use (count);
626 : : }
627 : :
628 : : public:
629 : : void u32 (unsigned); /* Write uncompressed integer. */
630 : :
631 : : public:
632 : : void c (unsigned char) ATTRIBUTE_UNUSED; /* Write unsigned char. */
633 : : void i (int); /* Write signed int. */
634 : : void u (unsigned); /* Write unsigned int. */
635 : : void z (size_t s); /* Write size_t. */
636 : : void wi (HOST_WIDE_INT); /* Write HOST_WIDE_INT. */
637 : : void wu (unsigned HOST_WIDE_INT); /* Write unsigned HOST_WIDE_INT. */
638 : 18293 : void str (const char *ptr)
639 : : {
640 : 18293 : str (ptr, strlen (ptr));
641 : 18293 : }
642 : 427943 : void cpp_node (const cpp_hashnode *node)
643 : : {
644 : 427943 : str ((const char *)NODE_NAME (node), NODE_LEN (node));
645 : 17970 : }
646 : : void str (const char *, size_t); /* Write string of known length. */
647 : : void buf (const void *, size_t); /* Write fixed length buffer. */
648 : : void *buf (size_t); /* Create a writable buffer */
649 : :
650 : : struct bits_out;
651 : : bits_out stream_bits ();
652 : :
653 : : public:
654 : : /* Format a NUL-terminated raw string. */
655 : : void printf (const char *, ...) ATTRIBUTE_PRINTF_2;
656 : : void print_time (const char *, const tm *, const char *);
657 : :
658 : : public:
659 : : /* Dump instrumentation. */
660 : : static void instrument ();
661 : :
662 : : protected:
663 : : /* Instrumentation. */
664 : : static unsigned spans[4];
665 : : static unsigned lengths[4];
666 : : };
667 : : } // anon namespace
668 : :
669 : : /* Finish bit packet. Rewind the bytes not used. */
670 : :
671 : : static unsigned
672 : 56684070 : bit_flush (data& bits, uint32_t& bit_val, unsigned& bit_pos)
673 : : {
674 : 56684070 : gcc_assert (bit_pos);
675 : 56684070 : unsigned bytes = (bit_pos + 7) / 8;
676 : 56684070 : bits.unuse (4 - bytes);
677 : 56684070 : bit_pos = 0;
678 : 56684070 : bit_val = 0;
679 : 56684070 : return bytes;
680 : : }
681 : :
682 : : /* Bit stream reader (RAII-enabled). Bools are packed into bytes. You
683 : : cannot mix bools and non-bools. Use bflush to flush the current stream
684 : : of bools on demand. Upon destruction bflush is called.
685 : :
686 : : When reading, we don't know how many bools we'll read in. So read
687 : : 4 bytes-worth, and then rewind when flushing if we didn't need them
688 : : all. You can't have a block of bools closer than 4 bytes to the
689 : : end of the buffer.
690 : :
691 : : Both bits_in and bits_out maintain the necessary state for bit packing,
692 : : and since these objects are locally constructed the compiler can more
693 : : easily track their state across consecutive reads/writes and optimize
694 : : away redundant buffering checks. */
695 : :
696 : : struct bytes_in::bits_in {
697 : : bytes_in& in;
698 : : uint32_t bit_val = 0;
699 : : unsigned bit_pos = 0;
700 : :
701 : 22076640 : bits_in (bytes_in& in)
702 : 22076640 : : in (in)
703 : : { }
704 : :
705 : 22076640 : ~bits_in ()
706 : : {
707 : 20571937 : bflush ();
708 : 22076640 : }
709 : :
710 : : bits_in(bits_in&&) = default;
711 : : bits_in(const bits_in&) = delete;
712 : : bits_in& operator=(const bits_in&) = delete;
713 : :
714 : : /* Completed a block of bools. */
715 : 45987023 : void bflush ()
716 : : {
717 : 45987023 : if (bit_pos)
718 : 25415086 : bit_flush (in, bit_val, bit_pos);
719 : 45987023 : }
720 : :
721 : : /* Read one bit. */
722 : 767004695 : bool b ()
723 : : {
724 : 767004695 : if (!bit_pos)
725 : 33482258 : bit_val = in.u32 ();
726 : 767004695 : bool x = (bit_val >> bit_pos) & 1;
727 : 767004695 : bit_pos = (bit_pos + 1) % 32;
728 : 767004695 : return x;
729 : : }
730 : : };
731 : :
732 : : /* Factory function for bits_in. */
733 : :
734 : : bytes_in::bits_in
735 : 22076640 : bytes_in::stream_bits ()
736 : : {
737 : 22076640 : return bits_in (*this);
738 : : }
739 : :
740 : : /* Bit stream writer (RAII-enabled), counterpart to bits_in. */
741 : :
742 : : struct bytes_out::bits_out {
743 : : bytes_out& out;
744 : : uint32_t bit_val = 0;
745 : : unsigned bit_pos = 0;
746 : : char is_set = -1;
747 : :
748 : 20577001 : bits_out (bytes_out& out)
749 : 20577001 : : out (out)
750 : : { }
751 : :
752 : 20577001 : ~bits_out ()
753 : : {
754 : 115397 : bflush ();
755 : : }
756 : :
757 : : bits_out(bits_out&&) = default;
758 : : bits_out(const bits_out&) = delete;
759 : : bits_out& operator=(const bits_out&) = delete;
760 : :
761 : : /* Completed a block of bools. */
762 : 42836416 : void bflush ()
763 : : {
764 : 42836416 : if (bit_pos)
765 : : {
766 : 23684795 : out.u32 (bit_val);
767 : 23684795 : out.lengths[2] += bit_flush (out, bit_val, bit_pos);
768 : : }
769 : 42836416 : out.spans[2]++;
770 : 42836416 : is_set = -1;
771 : 42836416 : }
772 : :
773 : : /* Write one bit.
774 : :
775 : : It may be worth optimizing for most bools being zero. Some kind of
776 : : run-length encoding? */
777 : 711348773 : void b (bool x)
778 : : {
779 : 711348773 : if (is_set != x)
780 : : {
781 : 75058143 : is_set = x;
782 : 75058143 : out.spans[x]++;
783 : : }
784 : 711348773 : out.lengths[x]++;
785 : 711348773 : bit_val |= unsigned (x) << bit_pos++;
786 : 711348773 : if (bit_pos == 32)
787 : : {
788 : 7584189 : out.u32 (bit_val);
789 : 7584189 : out.lengths[2] += bit_flush (out, bit_val, bit_pos);
790 : : }
791 : 711348773 : }
792 : : };
793 : :
794 : : /* Factory function for bits_out. */
795 : :
796 : : bytes_out::bits_out
797 : 20577001 : bytes_out::stream_bits ()
798 : : {
799 : 20577001 : return bits_out (*this);
800 : : }
801 : :
802 : : /* Instrumentation. */
803 : : unsigned bytes_out::spans[4];
804 : : unsigned bytes_out::lengths[4];
805 : :
806 : : /* If CRC_PTR non-null, set the CRC of the buffer. Mix the CRC into
807 : : that pointed to by CRC_PTR. */
808 : :
809 : : void
810 : 372487 : bytes_out::set_crc (unsigned *crc_ptr)
811 : : {
812 : 372487 : if (crc_ptr)
813 : : {
814 : 370663 : gcc_checking_assert (pos >= 4);
815 : :
816 : 370663 : unsigned crc = calc_crc (pos);
817 : 370663 : unsigned accum = *crc_ptr;
818 : : /* Only mix the existing *CRC_PTR if it is non-zero. */
819 : 370663 : accum = accum ? crc32_combine (accum, crc, pos - 4) : crc;
820 : 370663 : *crc_ptr = accum;
821 : :
822 : : /* Buffer will be sufficiently aligned. */
823 : 370663 : *(unsigned *)buffer = crc;
824 : : }
825 : 372487 : }
826 : :
827 : : /* Exactly 4 bytes. Used internally for bool packing and a few other
828 : : places. We can't simply use uint32_t because (a) alignment and
829 : : (b) we need little-endian for the bool streaming rewinding to make
830 : : sense. */
831 : :
832 : : void
833 : 31274887 : bytes_out::u32 (unsigned val)
834 : : {
835 : 31274887 : if (char *ptr = write (4))
836 : : {
837 : 31274887 : ptr[0] = val;
838 : 31274887 : ptr[1] = val >> 8;
839 : 31274887 : ptr[2] = val >> 16;
840 : 31274887 : ptr[3] = val >> 24;
841 : : }
842 : 31274887 : }
843 : :
844 : : unsigned
845 : 33488625 : bytes_in::u32 ()
846 : : {
847 : 33488625 : unsigned val = 0;
848 : 33488625 : if (const char *ptr = read (4))
849 : : {
850 : 33488625 : val |= (unsigned char)ptr[0];
851 : 33488625 : val |= (unsigned char)ptr[1] << 8;
852 : 33488625 : val |= (unsigned char)ptr[2] << 16;
853 : 33488625 : val |= (unsigned char)ptr[3] << 24;
854 : : }
855 : :
856 : 33488625 : return val;
857 : : }
858 : :
859 : : /* Chars are unsigned and written as single bytes. */
860 : :
861 : : void
862 : 0 : bytes_out::c (unsigned char v)
863 : : {
864 : 0 : if (char *ptr = write (1))
865 : 0 : *ptr = v;
866 : 0 : }
867 : :
868 : : int
869 : 0 : bytes_in::c ()
870 : : {
871 : 0 : int v = 0;
872 : 0 : if (const char *ptr = read (1))
873 : 0 : v = (unsigned char)ptr[0];
874 : 0 : return v;
875 : : }
876 : :
877 : : /* Ints 7-bit as a byte. Otherwise a 3bit count of following bytes in
878 : : big-endian form. 4 bits are in the first byte. */
879 : :
880 : : void
881 : 128042601 : bytes_out::i (int v)
882 : : {
883 : 128042601 : if (char *ptr = write (1))
884 : : {
885 : 128042601 : if (v <= 0x3f && v >= -0x40)
886 : 100837603 : *ptr = v & 0x7f;
887 : : else
888 : : {
889 : 27204998 : unsigned bytes = 0;
890 : 27204998 : int probe;
891 : 27204998 : if (v >= 0)
892 : 0 : for (probe = v >> 8; probe > 0x7; probe >>= 8)
893 : 0 : bytes++;
894 : : else
895 : 42723516 : for (probe = v >> 8; probe < -0x8; probe >>= 8)
896 : 15518518 : bytes++;
897 : 27204998 : *ptr = 0x80 | bytes << 4 | (probe & 0xf);
898 : 27204998 : if ((ptr = write (++bytes)))
899 : 69928514 : for (; bytes--; v >>= 8)
900 : 42723516 : ptr[bytes] = v & 0xff;
901 : : }
902 : : }
903 : 128042601 : }
904 : :
905 : : int
906 : 140231946 : bytes_in::i ()
907 : : {
908 : 140231946 : int v = 0;
909 : 140231946 : if (const char *ptr = read (1))
910 : : {
911 : 140231946 : v = *ptr & 0xff;
912 : 140231946 : if (v & 0x80)
913 : : {
914 : 30642346 : unsigned bytes = (v >> 4) & 0x7;
915 : 30642346 : v &= 0xf;
916 : 30642346 : if (v & 0x8)
917 : 30642346 : v |= -1 ^ 0x7;
918 : : /* unsigned necessary due to left shifts of -ve values. */
919 : 30642346 : unsigned uv = unsigned (v);
920 : 30642346 : if ((ptr = read (++bytes)))
921 : 79960427 : while (bytes--)
922 : 49318081 : uv = (uv << 8) | (*ptr++ & 0xff);
923 : 30642346 : v = int (uv);
924 : : }
925 : 109589600 : else if (v & 0x40)
926 : 15440116 : v |= -1 ^ 0x3f;
927 : : }
928 : :
929 : 140231946 : return v;
930 : : }
931 : :
932 : : void
933 : 142405253 : bytes_out::u (unsigned v)
934 : : {
935 : 142405253 : if (char *ptr = write (1))
936 : : {
937 : 142405253 : if (v <= 0x7f)
938 : 105600028 : *ptr = v;
939 : : else
940 : : {
941 : 36805225 : unsigned bytes = 0;
942 : 36805225 : unsigned probe;
943 : 87982749 : for (probe = v >> 8; probe > 0xf; probe >>= 8)
944 : 51177524 : bytes++;
945 : 36805225 : *ptr = 0x80 | bytes << 4 | probe;
946 : 36805225 : if ((ptr = write (++bytes)))
947 : 124787974 : for (; bytes--; v >>= 8)
948 : 87982749 : ptr[bytes] = v & 0xff;
949 : : }
950 : : }
951 : 142405253 : }
952 : :
953 : : unsigned
954 : 144574321 : bytes_in::u ()
955 : : {
956 : 144574321 : unsigned v = 0;
957 : :
958 : 144574321 : if (const char *ptr = read (1))
959 : : {
960 : 144574321 : v = *ptr & 0xff;
961 : 144574321 : if (v & 0x80)
962 : : {
963 : 38311525 : unsigned bytes = (v >> 4) & 0x7;
964 : 38311525 : v &= 0xf;
965 : 38311525 : if ((ptr = read (++bytes)))
966 : 128307865 : while (bytes--)
967 : 89996340 : v = (v << 8) | (*ptr++ & 0xff);
968 : : }
969 : : }
970 : :
971 : 144574321 : return v;
972 : : }
973 : :
974 : : void
975 : 3134451 : bytes_out::wi (HOST_WIDE_INT v)
976 : : {
977 : 3134451 : if (char *ptr = write (1))
978 : : {
979 : 3134451 : if (v <= 0x3f && v >= -0x40)
980 : 2860906 : *ptr = v & 0x7f;
981 : : else
982 : : {
983 : 273545 : unsigned bytes = 0;
984 : 273545 : HOST_WIDE_INT probe;
985 : 273545 : if (v >= 0)
986 : 362554 : for (probe = v >> 8; probe > 0x7; probe >>= 8)
987 : 92040 : bytes++;
988 : : else
989 : 9858 : for (probe = v >> 8; probe < -0x8; probe >>= 8)
990 : 6827 : bytes++;
991 : 273545 : *ptr = 0x80 | bytes << 4 | (probe & 0xf);
992 : 273545 : if ((ptr = write (++bytes)))
993 : 645957 : for (; bytes--; v >>= 8)
994 : 372412 : ptr[bytes] = v & 0xff;
995 : : }
996 : : }
997 : 3134451 : }
998 : :
999 : : HOST_WIDE_INT
1000 : 3056450 : bytes_in::wi ()
1001 : : {
1002 : 3056450 : HOST_WIDE_INT v = 0;
1003 : 3056450 : if (const char *ptr = read (1))
1004 : : {
1005 : 3056450 : v = *ptr & 0xff;
1006 : 3056450 : if (v & 0x80)
1007 : : {
1008 : 277094 : unsigned bytes = (v >> 4) & 0x7;
1009 : 277094 : v &= 0xf;
1010 : 277094 : if (v & 0x8)
1011 : 3313 : v |= -1 ^ 0x7;
1012 : : /* unsigned necessary due to left shifts of -ve values. */
1013 : 277094 : unsigned HOST_WIDE_INT uv = (unsigned HOST_WIDE_INT) v;
1014 : 277094 : if ((ptr = read (++bytes)))
1015 : 649929 : while (bytes--)
1016 : 372835 : uv = (uv << 8) | (*ptr++ & 0xff);
1017 : 277094 : v = (HOST_WIDE_INT) uv;
1018 : : }
1019 : 2779356 : else if (v & 0x40)
1020 : 14625 : v |= -1 ^ 0x3f;
1021 : : }
1022 : :
1023 : 3056450 : return v;
1024 : : }
1025 : :
1026 : : /* unsigned wide ints are just written as signed wide ints. */
1027 : :
1028 : : inline void
1029 : 3133343 : bytes_out::wu (unsigned HOST_WIDE_INT v)
1030 : : {
1031 : 3133343 : wi ((HOST_WIDE_INT) v);
1032 : : }
1033 : :
1034 : : inline unsigned HOST_WIDE_INT
1035 : 3055254 : bytes_in::wu ()
1036 : : {
1037 : 5342484 : return (unsigned HOST_WIDE_INT) wi ();
1038 : : }
1039 : :
1040 : : /* size_t written as unsigned or unsigned wide int. */
1041 : :
1042 : : inline void
1043 : 2386598 : bytes_out::z (size_t s)
1044 : : {
1045 : 2386598 : if (sizeof (s) == sizeof (unsigned))
1046 : : u (s);
1047 : : else
1048 : 4718051 : wu (s);
1049 : : }
1050 : :
1051 : : inline size_t
1052 : 2287200 : bytes_in::z ()
1053 : : {
1054 : 2287200 : if (sizeof (size_t) == sizeof (unsigned))
1055 : : return u ();
1056 : : else
1057 : 4574400 : return wu ();
1058 : : }
1059 : :
1060 : : /* Buffer simply memcpied. */
1061 : : void *
1062 : 2429735 : bytes_out::buf (size_t len)
1063 : : {
1064 : 2429735 : align (sizeof (void *) * 2);
1065 : 2429735 : return write (len);
1066 : : }
1067 : :
1068 : : void
1069 : 2357792 : bytes_out::buf (const void *src, size_t len)
1070 : : {
1071 : 2357792 : if (void *ptr = buf (len))
1072 : 2357792 : memcpy (ptr, src, len);
1073 : 2357792 : }
1074 : :
1075 : : const void *
1076 : 2322612 : bytes_in::buf (size_t len)
1077 : : {
1078 : 2322612 : align (sizeof (void *) * 2);
1079 : 2322612 : const char *ptr = read (len);
1080 : :
1081 : 2322612 : return ptr;
1082 : : }
1083 : :
1084 : : /* strings as an size_t length, followed by the buffer. Make sure
1085 : : there's a NUL terminator on read. */
1086 : :
1087 : : void
1088 : 2386598 : bytes_out::str (const char *string, size_t len)
1089 : : {
1090 : 2331453 : z (len);
1091 : 2331453 : if (len)
1092 : : {
1093 : 2331453 : gcc_checking_assert (!string[len]);
1094 : 2331453 : buf (string, len + 1);
1095 : : }
1096 : 55145 : }
1097 : :
1098 : : const char *
1099 : 2287200 : bytes_in::str (size_t *len_p)
1100 : : {
1101 : 2287200 : size_t len = z ();
1102 : :
1103 : : /* We're about to trust some user data. */
1104 : 2287200 : if (overrun)
1105 : 0 : len = 0;
1106 : 2287200 : if (len_p)
1107 : 2284600 : *len_p = len;
1108 : 2287200 : const char *str = NULL;
1109 : 2287200 : if (len)
1110 : : {
1111 : 2286872 : str = reinterpret_cast<const char *> (buf (len + 1));
1112 : 2286872 : if (!str || str[len])
1113 : : {
1114 : 0 : set_overrun ();
1115 : 0 : str = NULL;
1116 : : }
1117 : : }
1118 : 0 : return str ? str : "";
1119 : : }
1120 : :
1121 : : cpp_hashnode *
1122 : 323759 : bytes_in::cpp_node ()
1123 : : {
1124 : 323759 : size_t len;
1125 : 323759 : const char *s = str (&len);
1126 : 323759 : if (!len)
1127 : : return NULL;
1128 : 323431 : return ::cpp_node (get_identifier_with_length (s, len));
1129 : : }
1130 : :
1131 : : /* Format a string directly to the buffer, including a terminating
1132 : : NUL. Intended for human consumption. */
1133 : :
1134 : : void
1135 : 18667 : bytes_out::printf (const char *format, ...)
1136 : : {
1137 : 18667 : va_list args;
1138 : : /* Exercise buffer expansion. */
1139 : 18667 : size_t len = EXPERIMENT (10, 500);
1140 : :
1141 : 37274 : while (char *ptr = write (len))
1142 : : {
1143 : 37274 : va_start (args, format);
1144 : 37274 : size_t actual = vsnprintf (ptr, len, format, args) + 1;
1145 : 37274 : va_end (args);
1146 : 37274 : if (actual <= len)
1147 : : {
1148 : 18667 : unuse (len - actual);
1149 : 18667 : break;
1150 : : }
1151 : 18607 : unuse (len);
1152 : 18607 : len = actual;
1153 : 18607 : }
1154 : 18667 : }
1155 : :
1156 : : void
1157 : 3648 : bytes_out::print_time (const char *kind, const tm *time, const char *tz)
1158 : : {
1159 : 3648 : printf ("%stime: %4u/%02u/%02u %02u:%02u:%02u %s",
1160 : 3648 : kind, time->tm_year + 1900, time->tm_mon + 1, time->tm_mday,
1161 : 3648 : time->tm_hour, time->tm_min, time->tm_sec, tz);
1162 : 3648 : }
1163 : :
1164 : : /* Encapsulated Lazy Records Of Named Declarations.
1165 : : Header: Stunningly Elf32_Ehdr-like
1166 : : Sections: Sectional data
1167 : : [1-N) : User data sections
1168 : : N .strtab : strings, stunningly ELF STRTAB-like
1169 : : Index: Section table, stunningly ELF32_Shdr-like. */
1170 : :
1171 : : class elf {
1172 : : protected:
1173 : : /* Constants used within the format. */
1174 : : enum private_constants {
1175 : : /* File kind. */
1176 : : ET_NONE = 0,
1177 : : EM_NONE = 0,
1178 : : OSABI_NONE = 0,
1179 : :
1180 : : /* File format. */
1181 : : EV_CURRENT = 1,
1182 : : CLASS32 = 1,
1183 : : DATA2LSB = 1,
1184 : : DATA2MSB = 2,
1185 : :
1186 : : /* Section numbering. */
1187 : : SHN_UNDEF = 0,
1188 : : SHN_LORESERVE = 0xff00,
1189 : : SHN_XINDEX = 0xffff,
1190 : :
1191 : : /* Section types. */
1192 : : SHT_NONE = 0, /* No contents. */
1193 : : SHT_PROGBITS = 1, /* Random bytes. */
1194 : : SHT_STRTAB = 3, /* A string table. */
1195 : :
1196 : : /* Section flags. */
1197 : : SHF_NONE = 0x00, /* Nothing. */
1198 : : SHF_STRINGS = 0x20, /* NUL-Terminated strings. */
1199 : :
1200 : : /* I really hope we do not get CMI files larger than 4GB. */
1201 : : MY_CLASS = CLASS32,
1202 : : /* It is host endianness that is relevant. */
1203 : : MY_ENDIAN = DATA2LSB
1204 : : #ifdef WORDS_BIGENDIAN
1205 : : ^ DATA2LSB ^ DATA2MSB
1206 : : #endif
1207 : : };
1208 : :
1209 : : public:
1210 : : /* Constants visible to users. */
1211 : : enum public_constants {
1212 : : /* Special error codes. Breaking layering a bit. */
1213 : : E_BAD_DATA = -1, /* Random unexpected data errors. */
1214 : : E_BAD_LAZY = -2, /* Badly ordered laziness. */
1215 : : E_BAD_IMPORT = -3 /* A nested import failed. */
1216 : : };
1217 : :
1218 : : protected:
1219 : : /* File identification. On-disk representation. */
1220 : : struct ident {
1221 : : uint8_t magic[4]; /* 0x7f, 'E', 'L', 'F' */
1222 : : uint8_t klass; /* 4:CLASS32 */
1223 : : uint8_t data; /* 5:DATA2[LM]SB */
1224 : : uint8_t version; /* 6:EV_CURRENT */
1225 : : uint8_t osabi; /* 7:OSABI_NONE */
1226 : : uint8_t abiver; /* 8: 0 */
1227 : : uint8_t pad[7]; /* 9-15 */
1228 : : };
1229 : : /* File header. On-disk representation. */
1230 : : struct header {
1231 : : struct ident ident;
1232 : : uint16_t type; /* ET_NONE */
1233 : : uint16_t machine; /* EM_NONE */
1234 : : uint32_t version; /* EV_CURRENT */
1235 : : uint32_t entry; /* 0 */
1236 : : uint32_t phoff; /* 0 */
1237 : : uint32_t shoff; /* Section Header Offset in file */
1238 : : uint32_t flags;
1239 : : uint16_t ehsize; /* ELROND Header SIZE -- sizeof (header) */
1240 : : uint16_t phentsize; /* 0 */
1241 : : uint16_t phnum; /* 0 */
1242 : : uint16_t shentsize; /* Section Header SIZE -- sizeof (section) */
1243 : : uint16_t shnum; /* Section Header NUM */
1244 : : uint16_t shstrndx; /* Section Header STRing iNDeX */
1245 : : };
1246 : : /* File section. On-disk representation. */
1247 : : struct section {
1248 : : uint32_t name; /* String table offset. */
1249 : : uint32_t type; /* SHT_* */
1250 : : uint32_t flags; /* SHF_* */
1251 : : uint32_t addr; /* 0 */
1252 : : uint32_t offset; /* OFFSET in file */
1253 : : uint32_t size; /* SIZE of section */
1254 : : uint32_t link; /* 0 */
1255 : : uint32_t info; /* 0 */
1256 : : uint32_t addralign; /* 0 */
1257 : : uint32_t entsize; /* ENTry SIZE, usually 0 */
1258 : : };
1259 : :
1260 : : protected:
1261 : : data hdr; /* The header. */
1262 : : data sectab; /* The section table. */
1263 : : data strtab; /* String table. */
1264 : : int fd; /* File descriptor we're reading or writing. */
1265 : : int err; /* Sticky error code. */
1266 : :
1267 : : public:
1268 : : /* Construct from STREAM. E is errno if STREAM NULL. */
1269 : 3941 : elf (int fd, int e)
1270 : 7882 : :hdr (), sectab (), strtab (), fd (fd), err (fd >= 0 ? 0 : e)
1271 : : {}
1272 : 3880 : ~elf ()
1273 : : {
1274 : 3880 : gcc_checking_assert (fd < 0 && !hdr.buffer
1275 : : && !sectab.buffer && !strtab.buffer);
1276 : 3880 : }
1277 : :
1278 : : public:
1279 : : /* Return the error, if we have an error. */
1280 : 738862 : int get_error () const
1281 : : {
1282 : 738862 : return err;
1283 : : }
1284 : : /* Set the error, unless it's already been set. */
1285 : 26 : void set_error (int e = E_BAD_DATA)
1286 : : {
1287 : 26 : if (!err)
1288 : 19 : err = e;
1289 : 0 : }
1290 : : /* Get an error string. */
1291 : : const char *get_error (const char *) const;
1292 : :
1293 : : public:
1294 : : /* Begin reading/writing file. Return false on error. */
1295 : 3853 : bool begin () const
1296 : : {
1297 : 3853 : return !get_error ();
1298 : : }
1299 : : /* Finish reading/writing file. Return false on error. */
1300 : : bool end ();
1301 : : };
1302 : :
1303 : : /* Return error string. */
1304 : :
1305 : : const char *
1306 : 37 : elf::get_error (const char *name) const
1307 : : {
1308 : 37 : if (!name)
1309 : : return "Unknown CMI mapping";
1310 : :
1311 : 37 : switch (err)
1312 : : {
1313 : 0 : case 0:
1314 : 0 : gcc_unreachable ();
1315 : : case E_BAD_DATA:
1316 : : return "Bad file data";
1317 : 6 : case E_BAD_IMPORT:
1318 : 6 : return "Bad import dependency";
1319 : 0 : case E_BAD_LAZY:
1320 : 0 : return "Bad lazy ordering";
1321 : 18 : default:
1322 : 18 : return xstrerror (err);
1323 : : }
1324 : : }
1325 : :
1326 : : /* Finish file, return true if there's an error. */
1327 : :
1328 : : bool
1329 : 5539 : elf::end ()
1330 : : {
1331 : : /* Close the stream and free the section table. */
1332 : 5539 : if (fd >= 0 && close (fd))
1333 : 0 : set_error (errno);
1334 : 5539 : fd = -1;
1335 : :
1336 : 5539 : return !get_error ();
1337 : : }
1338 : :
1339 : : /* ELROND reader. */
1340 : :
1341 : : class elf_in : public elf {
1342 : : typedef elf parent;
1343 : :
1344 : : private:
1345 : : /* For freezing & defrosting. */
1346 : : #if !defined (HOST_LACKS_INODE_NUMBERS)
1347 : : dev_t device;
1348 : : ino_t inode;
1349 : : #endif
1350 : :
1351 : : public:
1352 : 2047 : elf_in (int fd, int e)
1353 : 4094 : :parent (fd, e)
1354 : : {
1355 : : }
1356 : 1986 : ~elf_in ()
1357 : : {
1358 : 1986 : }
1359 : :
1360 : : public:
1361 : 360586 : bool is_frozen () const
1362 : : {
1363 : 18 : return fd < 0 && hdr.pos;
1364 : : }
1365 : 18 : bool is_freezable () const
1366 : : {
1367 : 9 : return fd >= 0 && hdr.pos;
1368 : : }
1369 : : void freeze ();
1370 : : bool defrost (const char *);
1371 : :
1372 : : /* If BYTES is in the mmapped area, allocate a new buffer for it. */
1373 : 0 : void preserve (bytes_in &bytes ATTRIBUTE_UNUSED)
1374 : : {
1375 : : #if MAPPED_READING
1376 : 0 : if (hdr.buffer && bytes.buffer >= hdr.buffer
1377 : 0 : && bytes.buffer < hdr.buffer + hdr.pos)
1378 : : {
1379 : 0 : char *buf = bytes.buffer;
1380 : 0 : bytes.buffer = data::simple_memory.grow (NULL, bytes.size);
1381 : 0 : memcpy (bytes.buffer, buf, bytes.size);
1382 : : }
1383 : : #endif
1384 : 0 : }
1385 : : /* If BYTES is not in SELF's mmapped area, free it. SELF might be
1386 : : NULL. */
1387 : 877 : static void release (elf_in *self ATTRIBUTE_UNUSED, bytes_in &bytes)
1388 : : {
1389 : : #if MAPPED_READING
1390 : 877 : if (!(self && self->hdr.buffer && bytes.buffer >= self->hdr.buffer
1391 : 877 : && bytes.buffer < self->hdr.buffer + self->hdr.pos))
1392 : : #endif
1393 : 0 : data::simple_memory.shrink (bytes.buffer);
1394 : 877 : bytes.buffer = NULL;
1395 : 877 : bytes.size = 0;
1396 : 877 : }
1397 : :
1398 : : public:
1399 : 374513 : static void grow (data &data, unsigned needed)
1400 : : {
1401 : 374513 : gcc_checking_assert (!data.buffer);
1402 : : #if !MAPPED_READING
1403 : : data.buffer = XNEWVEC (char, needed);
1404 : : #endif
1405 : 374513 : data.size = needed;
1406 : 374513 : }
1407 : 378848 : static void shrink (data &data)
1408 : : {
1409 : : #if !MAPPED_READING
1410 : : XDELETEVEC (data.buffer);
1411 : : #endif
1412 : 378848 : data.buffer = NULL;
1413 : 378848 : data.size = 0;
1414 : 0 : }
1415 : :
1416 : : public:
1417 : 372484 : const section *get_section (unsigned s) const
1418 : : {
1419 : 372484 : if (s * sizeof (section) < sectab.size)
1420 : 372484 : return reinterpret_cast<const section *>
1421 : 372484 : (§ab.buffer[s * sizeof (section)]);
1422 : : else
1423 : : return NULL;
1424 : : }
1425 : : unsigned get_section_limit () const
1426 : : {
1427 : : return sectab.size / sizeof (section);
1428 : : }
1429 : :
1430 : : protected:
1431 : : const char *read (data *, unsigned, unsigned);
1432 : :
1433 : : public:
1434 : : /* Read section by number. */
1435 : 372484 : bool read (data *d, const section *s)
1436 : : {
1437 : 372484 : return s && read (d, s->offset, s->size);
1438 : : }
1439 : :
1440 : : /* Find section by name. */
1441 : : unsigned find (const char *name);
1442 : : /* Find section by index. */
1443 : : const section *find (unsigned snum, unsigned type = SHT_PROGBITS);
1444 : :
1445 : : public:
1446 : : /* Release the string table, when we're done with it. */
1447 : 5655 : void release ()
1448 : : {
1449 : 5655 : shrink (strtab);
1450 : 30 : }
1451 : :
1452 : : public:
1453 : : bool begin (location_t);
1454 : 3645 : bool end ()
1455 : : {
1456 : 3645 : release ();
1457 : : #if MAPPED_READING
1458 : 3645 : if (hdr.buffer)
1459 : 1986 : munmap (hdr.buffer, hdr.pos);
1460 : 3645 : hdr.buffer = NULL;
1461 : : #endif
1462 : 3645 : shrink (sectab);
1463 : :
1464 : 3645 : return parent::end ();
1465 : : }
1466 : :
1467 : : public:
1468 : : /* Return string name at OFFSET. Checks OFFSET range. Always
1469 : : returns non-NULL. We know offset 0 is an empty string. */
1470 : 344568 : const char *name (unsigned offset)
1471 : : {
1472 : 689136 : return &strtab.buffer[offset < strtab.size ? offset : 0];
1473 : : }
1474 : : };
1475 : :
1476 : : /* ELROND writer. */
1477 : :
1478 : : class elf_out : public elf, public data::allocator {
1479 : : typedef elf parent;
1480 : : /* Desired section alignment on disk. */
1481 : : static const int SECTION_ALIGN = 16;
1482 : :
1483 : : private:
1484 : : ptr_int_hash_map identtab; /* Map of IDENTIFIERS to strtab offsets. */
1485 : : unsigned pos; /* Write position in file. */
1486 : : #if MAPPED_WRITING
1487 : : unsigned offset; /* Offset of the mapping. */
1488 : : unsigned extent; /* Length of mapping. */
1489 : : unsigned page_size; /* System page size. */
1490 : : #endif
1491 : :
1492 : : public:
1493 : 1894 : elf_out (int fd, int e)
1494 : 3718 : :parent (fd, e), identtab (500), pos (0)
1495 : : {
1496 : : #if MAPPED_WRITING
1497 : 1894 : offset = extent = 0;
1498 : 1894 : page_size = sysconf (_SC_PAGE_SIZE);
1499 : 1894 : if (page_size < SECTION_ALIGN)
1500 : : /* Something really strange. */
1501 : 0 : set_error (EINVAL);
1502 : : #endif
1503 : 1894 : }
1504 : 1894 : ~elf_out ()
1505 : 1894 : {
1506 : 1894 : data::simple_memory.shrink (hdr);
1507 : 1894 : data::simple_memory.shrink (sectab);
1508 : 1894 : data::simple_memory.shrink (strtab);
1509 : 1894 : }
1510 : :
1511 : : #if MAPPED_WRITING
1512 : : private:
1513 : : void create_mapping (unsigned ext, bool extending = true);
1514 : : void remove_mapping ();
1515 : : #endif
1516 : :
1517 : : protected:
1518 : : using allocator::grow;
1519 : : char *grow (char *, unsigned needed) final override;
1520 : : #if MAPPED_WRITING
1521 : : using allocator::shrink;
1522 : : void shrink (char *) final override;
1523 : : #endif
1524 : :
1525 : : public:
1526 : 3864 : unsigned get_section_limit () const
1527 : : {
1528 : 3864 : return sectab.pos / sizeof (section);
1529 : : }
1530 : :
1531 : : protected:
1532 : : unsigned add (unsigned type, unsigned name = 0,
1533 : : unsigned off = 0, unsigned size = 0, unsigned flags = SHF_NONE);
1534 : : unsigned write (const data &);
1535 : : #if MAPPED_WRITING
1536 : : unsigned write (const bytes_out &);
1537 : : #endif
1538 : :
1539 : : public:
1540 : : /* IDENTIFIER to strtab offset. */
1541 : : unsigned name (tree ident);
1542 : : /* String literal to strtab offset. */
1543 : : unsigned name (const char *n);
1544 : : /* Qualified name of DECL to strtab offset. */
1545 : : unsigned qualified_name (tree decl, bool is_defn);
1546 : :
1547 : : private:
1548 : : unsigned strtab_write (const char *s, unsigned l);
1549 : : void strtab_write (tree decl, int);
1550 : :
1551 : : public:
1552 : : /* Add a section with contents or strings. */
1553 : : unsigned add (const bytes_out &, bool string_p, unsigned name);
1554 : :
1555 : : public:
1556 : : /* Begin and end writing. */
1557 : : bool begin ();
1558 : : bool end ();
1559 : : };
1560 : :
1561 : : /* Begin reading section NAME (of type PROGBITS) from SOURCE.
1562 : : Data always checked for CRC. */
1563 : :
1564 : : bool
1565 : 11890 : bytes_in::begin (location_t loc, elf_in *source, const char *name)
1566 : : {
1567 : 11890 : unsigned snum = source->find (name);
1568 : :
1569 : 11890 : return begin (loc, source, snum, name);
1570 : : }
1571 : :
1572 : : /* Begin reading section numbered SNUM with NAME (may be NULL). */
1573 : :
1574 : : bool
1575 : 370455 : bytes_in::begin (location_t loc, elf_in *source, unsigned snum, const char *name)
1576 : : {
1577 : 370455 : if (!source->read (this, source->find (snum))
1578 : 370455 : || !size || !check_crc ())
1579 : : {
1580 : 0 : source->set_error (elf::E_BAD_DATA);
1581 : 0 : source->shrink (*this);
1582 : 0 : if (name)
1583 : 0 : error_at (loc, "section %qs is missing or corrupted", name);
1584 : : else
1585 : 0 : error_at (loc, "section #%u is missing or corrupted", snum);
1586 : 0 : return false;
1587 : : }
1588 : 370455 : pos = 4;
1589 : 370455 : return true;
1590 : : }
1591 : :
1592 : : /* Finish reading a section. */
1593 : :
1594 : : bool
1595 : 369548 : bytes_in::end (elf_in *src)
1596 : : {
1597 : 369548 : if (more_p ())
1598 : 7 : set_overrun ();
1599 : 369548 : if (overrun)
1600 : 7 : src->set_error ();
1601 : :
1602 : 369548 : src->shrink (*this);
1603 : :
1604 : 369548 : return !overrun;
1605 : : }
1606 : :
1607 : : /* Begin writing buffer. */
1608 : :
1609 : : void
1610 : 372487 : bytes_out::begin (bool need_crc)
1611 : : {
1612 : 0 : if (need_crc)
1613 : 0 : pos = 4;
1614 : 0 : memory->grow (*this, 0, false);
1615 : 360145 : }
1616 : :
1617 : : /* Finish writing buffer. Stream out to SINK as named section NAME.
1618 : : Return section number or 0 on failure. If CRC_PTR is true, crc
1619 : : the data. Otherwise it is a string section. */
1620 : :
1621 : : unsigned
1622 : 372487 : bytes_out::end (elf_out *sink, unsigned name, unsigned *crc_ptr)
1623 : : {
1624 : 372487 : lengths[3] += pos;
1625 : 372487 : spans[3]++;
1626 : :
1627 : 372487 : set_crc (crc_ptr);
1628 : 372487 : unsigned sec_num = sink->add (*this, !crc_ptr, name);
1629 : 372487 : memory->shrink (*this);
1630 : :
1631 : 372487 : return sec_num;
1632 : : }
1633 : :
1634 : : /* Close and open the file, without destroying it. */
1635 : :
1636 : : void
1637 : 9 : elf_in::freeze ()
1638 : : {
1639 : 9 : gcc_checking_assert (!is_frozen ());
1640 : : #if MAPPED_READING
1641 : 9 : if (munmap (hdr.buffer, hdr.pos) < 0)
1642 : 0 : set_error (errno);
1643 : : #endif
1644 : 9 : if (close (fd) < 0)
1645 : 0 : set_error (errno);
1646 : 9 : fd = -1;
1647 : 9 : }
1648 : :
1649 : : bool
1650 : 9 : elf_in::defrost (const char *name)
1651 : : {
1652 : 9 : gcc_checking_assert (is_frozen ());
1653 : 9 : struct stat stat;
1654 : :
1655 : 9 : fd = open (name, O_RDONLY | O_CLOEXEC | O_BINARY);
1656 : 9 : if (fd < 0 || fstat (fd, &stat) < 0)
1657 : 0 : set_error (errno);
1658 : : else
1659 : : {
1660 : 9 : bool ok = hdr.pos == unsigned (stat.st_size);
1661 : : #ifndef HOST_LACKS_INODE_NUMBERS
1662 : 9 : if (device != stat.st_dev
1663 : 9 : || inode != stat.st_ino)
1664 : : ok = false;
1665 : : #endif
1666 : 9 : if (!ok)
1667 : 0 : set_error (EMFILE);
1668 : : #if MAPPED_READING
1669 : 0 : if (ok)
1670 : : {
1671 : 9 : char *mapping = reinterpret_cast<char *>
1672 : 9 : (mmap (NULL, hdr.pos, PROT_READ, MAP_SHARED, fd, 0));
1673 : 9 : if (mapping == MAP_FAILED)
1674 : 0 : fail:
1675 : 0 : set_error (errno);
1676 : : else
1677 : : {
1678 : 9 : if (madvise (mapping, hdr.pos, MADV_RANDOM))
1679 : 0 : goto fail;
1680 : :
1681 : : /* These buffers are never NULL in this case. */
1682 : 9 : strtab.buffer = mapping + strtab.pos;
1683 : 9 : sectab.buffer = mapping + sectab.pos;
1684 : 9 : hdr.buffer = mapping;
1685 : : }
1686 : : }
1687 : : #endif
1688 : : }
1689 : :
1690 : 9 : return !get_error ();
1691 : : }
1692 : :
1693 : : /* Read at current position into BUFFER. Return true on success. */
1694 : :
1695 : : const char *
1696 : 374513 : elf_in::read (data *data, unsigned pos, unsigned length)
1697 : : {
1698 : : #if MAPPED_READING
1699 : 374513 : if (pos + length > hdr.pos)
1700 : : {
1701 : 0 : set_error (EINVAL);
1702 : 0 : return NULL;
1703 : : }
1704 : : #else
1705 : : if (pos != ~0u && lseek (fd, pos, SEEK_SET) < 0)
1706 : : {
1707 : : set_error (errno);
1708 : : return NULL;
1709 : : }
1710 : : #endif
1711 : 374513 : grow (*data, length);
1712 : : #if MAPPED_READING
1713 : 374513 : data->buffer = hdr.buffer + pos;
1714 : : #else
1715 : : if (::read (fd, data->buffer, data->size) != ssize_t (length))
1716 : : {
1717 : : set_error (errno);
1718 : : shrink (*data);
1719 : : return NULL;
1720 : : }
1721 : : #endif
1722 : :
1723 : 374513 : return data->buffer;
1724 : : }
1725 : :
1726 : : /* Read section SNUM of TYPE. Return section pointer or NULL on error. */
1727 : :
1728 : : const elf::section *
1729 : 372484 : elf_in::find (unsigned snum, unsigned type)
1730 : : {
1731 : 372484 : const section *sec = get_section (snum);
1732 : 372484 : if (!snum || !sec || sec->type != type)
1733 : 0 : return NULL;
1734 : : return sec;
1735 : : }
1736 : :
1737 : : /* Find a section NAME and TYPE. Return section number, or zero on
1738 : : failure. */
1739 : :
1740 : : unsigned
1741 : 11916 : elf_in::find (const char *sname)
1742 : : {
1743 : 69875 : for (unsigned pos = sectab.size; pos -= sizeof (section); )
1744 : : {
1745 : 69875 : const section *sec
1746 : 69875 : = reinterpret_cast<const section *> (§ab.buffer[pos]);
1747 : :
1748 : 139750 : if (0 == strcmp (sname, name (sec->name)))
1749 : 11916 : return pos / sizeof (section);
1750 : : }
1751 : :
1752 : : return 0;
1753 : : }
1754 : :
1755 : : /* Begin reading file. Verify header. Pull in section and string
1756 : : tables. Return true on success. */
1757 : :
1758 : : bool
1759 : 2029 : elf_in::begin (location_t loc)
1760 : : {
1761 : 2029 : if (!parent::begin ())
1762 : : return false;
1763 : :
1764 : 2029 : struct stat stat;
1765 : 2029 : unsigned size = 0;
1766 : 2029 : if (!fstat (fd, &stat))
1767 : : {
1768 : : #if !defined (HOST_LACKS_INODE_NUMBERS)
1769 : 2029 : device = stat.st_dev;
1770 : 2029 : inode = stat.st_ino;
1771 : : #endif
1772 : : /* Never generate files > 4GB, check we've not been given one. */
1773 : 2029 : if (stat.st_size == unsigned (stat.st_size))
1774 : 2029 : size = unsigned (stat.st_size);
1775 : : }
1776 : :
1777 : : #if MAPPED_READING
1778 : : /* MAP_SHARED so that the file is backing store. If someone else
1779 : : concurrently writes it, they're wrong. */
1780 : 2029 : void *mapping = mmap (NULL, size, PROT_READ, MAP_SHARED, fd, 0);
1781 : 2029 : if (mapping == MAP_FAILED)
1782 : : {
1783 : 0 : fail:
1784 : 0 : set_error (errno);
1785 : 0 : return false;
1786 : : }
1787 : : /* We'll be hopping over this randomly. Some systems declare the
1788 : : first parm as char *, and other declare it as void *. */
1789 : 2029 : if (madvise (reinterpret_cast <char *> (mapping), size, MADV_RANDOM))
1790 : 0 : goto fail;
1791 : :
1792 : 2029 : hdr.buffer = (char *)mapping;
1793 : : #else
1794 : : read (&hdr, 0, sizeof (header));
1795 : : #endif
1796 : 2029 : hdr.pos = size; /* Record size of the file. */
1797 : :
1798 : 2029 : const header *h = reinterpret_cast<const header *> (hdr.buffer);
1799 : 2029 : if (!h)
1800 : : return false;
1801 : :
1802 : 2029 : if (h->ident.magic[0] != 0x7f
1803 : : || h->ident.magic[1] != 'E'
1804 : : || h->ident.magic[2] != 'L'
1805 : 2029 : || h->ident.magic[3] != 'F')
1806 : : {
1807 : 0 : error_at (loc, "not Encapsulated Lazy Records of Named Declarations");
1808 : 0 : failed:
1809 : 0 : shrink (hdr);
1810 : 0 : return false;
1811 : : }
1812 : :
1813 : : /* We expect a particular format -- the ELF is not intended to be
1814 : : distributable. */
1815 : 2029 : if (h->ident.klass != MY_CLASS
1816 : : || h->ident.data != MY_ENDIAN
1817 : 2029 : || h->ident.version != EV_CURRENT
1818 : : || h->type != ET_NONE
1819 : 2029 : || h->machine != EM_NONE
1820 : 2029 : || h->ident.osabi != OSABI_NONE)
1821 : : {
1822 : 0 : error_at (loc, "unexpected encapsulation format or type");
1823 : 0 : goto failed;
1824 : : }
1825 : :
1826 : 2029 : int e = -1;
1827 : 2029 : if (!h->shoff || h->shentsize != sizeof (section))
1828 : : {
1829 : 0 : malformed:
1830 : 0 : set_error (e);
1831 : 0 : error_at (loc, "encapsulation is malformed");
1832 : 0 : goto failed;
1833 : : }
1834 : :
1835 : 2029 : unsigned strndx = h->shstrndx;
1836 : 2029 : unsigned shnum = h->shnum;
1837 : 2029 : if (shnum == SHN_XINDEX)
1838 : : {
1839 : 0 : if (!read (§ab, h->shoff, sizeof (section)))
1840 : : {
1841 : 0 : section_table_fail:
1842 : 0 : e = errno;
1843 : 0 : goto malformed;
1844 : : }
1845 : 0 : shnum = get_section (0)->size;
1846 : : /* Freeing does mean we'll re-read it in the case we're not
1847 : : mapping, but this is going to be rare. */
1848 : 0 : shrink (sectab);
1849 : : }
1850 : :
1851 : 2029 : if (!shnum)
1852 : 0 : goto malformed;
1853 : :
1854 : 2029 : if (!read (§ab, h->shoff, shnum * sizeof (section)))
1855 : 0 : goto section_table_fail;
1856 : :
1857 : 2029 : if (strndx == SHN_XINDEX)
1858 : 0 : strndx = get_section (0)->link;
1859 : :
1860 : 2029 : if (!read (&strtab, find (strndx, SHT_STRTAB)))
1861 : 0 : goto malformed;
1862 : :
1863 : : /* The string table should be at least one byte, with NUL chars
1864 : : at either end. */
1865 : 2029 : if (!(strtab.size && !strtab.buffer[0]
1866 : 2029 : && !strtab.buffer[strtab.size - 1]))
1867 : 0 : goto malformed;
1868 : :
1869 : : #if MAPPED_READING
1870 : : /* Record the offsets of the section and string tables. */
1871 : 2029 : sectab.pos = h->shoff;
1872 : 2029 : strtab.pos = shnum * sizeof (section);
1873 : : #else
1874 : : shrink (hdr);
1875 : : #endif
1876 : :
1877 : 2029 : return true;
1878 : : }
1879 : :
1880 : : /* Create a new mapping. */
1881 : :
1882 : : #if MAPPED_WRITING
1883 : : void
1884 : 2714 : elf_out::create_mapping (unsigned ext, bool extending)
1885 : : {
1886 : : #ifndef HAVE_POSIX_FALLOCATE
1887 : : #define posix_fallocate(fd,off,len) ftruncate (fd, off + len)
1888 : : #endif
1889 : 2714 : void *mapping = MAP_FAILED;
1890 : 2714 : if (extending && ext < 1024 * 1024)
1891 : : {
1892 : 2397 : if (!posix_fallocate (fd, offset, ext * 2))
1893 : 2397 : mapping = mmap (NULL, ext * 2, PROT_READ | PROT_WRITE,
1894 : 2397 : MAP_SHARED, fd, offset);
1895 : 2397 : if (mapping != MAP_FAILED)
1896 : : ext *= 2;
1897 : : }
1898 : 2397 : if (mapping == MAP_FAILED)
1899 : : {
1900 : 317 : if (!extending || !posix_fallocate (fd, offset, ext))
1901 : 317 : mapping = mmap (NULL, ext, PROT_READ | PROT_WRITE,
1902 : 317 : MAP_SHARED, fd, offset);
1903 : 317 : if (mapping == MAP_FAILED)
1904 : : {
1905 : 0 : set_error (errno);
1906 : : mapping = NULL;
1907 : : ext = 0;
1908 : : }
1909 : : }
1910 : : #undef posix_fallocate
1911 : 2714 : hdr.buffer = (char *)mapping;
1912 : 2714 : extent = ext;
1913 : 2714 : }
1914 : : #endif
1915 : :
1916 : : /* Flush out the current mapping. */
1917 : :
1918 : : #if MAPPED_WRITING
1919 : : void
1920 : 2714 : elf_out::remove_mapping ()
1921 : : {
1922 : 2714 : if (hdr.buffer)
1923 : : {
1924 : : /* MS_ASYNC dtrt with the removed mapping, including a
1925 : : subsequent overlapping remap. */
1926 : 2714 : if (msync (hdr.buffer, extent, MS_ASYNC)
1927 : 2714 : || munmap (hdr.buffer, extent))
1928 : : /* We're somewhat screwed at this point. */
1929 : 0 : set_error (errno);
1930 : : }
1931 : :
1932 : 2714 : hdr.buffer = NULL;
1933 : 2714 : }
1934 : : #endif
1935 : :
1936 : : /* Grow a mapping of PTR to be NEEDED bytes long. This gets
1937 : : interesting if the new size grows the EXTENT. */
1938 : :
1939 : : char *
1940 : 886969 : elf_out::grow (char *data, unsigned needed)
1941 : : {
1942 : 886969 : if (!data)
1943 : : {
1944 : : /* First allocation, check we're aligned. */
1945 : 376135 : gcc_checking_assert (!(pos & (SECTION_ALIGN - 1)));
1946 : : #if MAPPED_WRITING
1947 : 376135 : data = hdr.buffer + (pos - offset);
1948 : : #endif
1949 : : }
1950 : :
1951 : : #if MAPPED_WRITING
1952 : 886969 : unsigned off = data - hdr.buffer;
1953 : 886969 : if (off + needed > extent)
1954 : : {
1955 : : /* We need to grow the mapping. */
1956 : 769 : unsigned lwm = off & ~(page_size - 1);
1957 : 769 : unsigned hwm = (off + needed + page_size - 1) & ~(page_size - 1);
1958 : :
1959 : 769 : gcc_checking_assert (hwm > extent);
1960 : :
1961 : 769 : remove_mapping ();
1962 : :
1963 : 769 : offset += lwm;
1964 : 769 : create_mapping (extent < hwm - lwm ? hwm - lwm : extent);
1965 : :
1966 : 769 : data = hdr.buffer + (off - lwm);
1967 : : }
1968 : : #else
1969 : : data = allocator::grow (data, needed);
1970 : : #endif
1971 : :
1972 : 886969 : return data;
1973 : : }
1974 : :
1975 : : #if MAPPED_WRITING
1976 : : /* Shrinking is a NOP. */
1977 : : void
1978 : 376135 : elf_out::shrink (char *)
1979 : : {
1980 : 376135 : }
1981 : : #endif
1982 : :
1983 : : /* Write S of length L to the strtab buffer. L must include the ending
1984 : : NUL, if that's what you want. */
1985 : :
1986 : : unsigned
1987 : 1682582 : elf_out::strtab_write (const char *s, unsigned l)
1988 : : {
1989 : 1682582 : if (strtab.pos + l > strtab.size)
1990 : 1039 : data::simple_memory.grow (strtab, strtab.pos + l, false);
1991 : 1682582 : memcpy (strtab.buffer + strtab.pos, s, l);
1992 : 1682582 : unsigned res = strtab.pos;
1993 : 1682582 : strtab.pos += l;
1994 : 1682582 : return res;
1995 : : }
1996 : :
1997 : : /* Write qualified name of decl. INNER >0 if this is a definition, <0
1998 : : if this is a qualifier of an outer name. */
1999 : :
2000 : : void
2001 : 672699 : elf_out::strtab_write (tree decl, int inner)
2002 : : {
2003 : 672699 : tree ctx = CP_DECL_CONTEXT (decl);
2004 : 672699 : if (TYPE_P (ctx))
2005 : 6399 : ctx = TYPE_NAME (ctx);
2006 : 672699 : if (ctx != global_namespace)
2007 : 327845 : strtab_write (ctx, -1);
2008 : :
2009 : 672699 : tree name = DECL_NAME (decl);
2010 : 672699 : if (!name)
2011 : 0 : name = DECL_ASSEMBLER_NAME_RAW (decl);
2012 : 672699 : strtab_write (IDENTIFIER_POINTER (name), IDENTIFIER_LENGTH (name));
2013 : :
2014 : 672699 : if (inner)
2015 : 467322 : strtab_write (&"::{}"[inner+1], 2);
2016 : 672699 : }
2017 : :
2018 : : /* Map IDENTIFIER IDENT to strtab offset. Inserts into strtab if not
2019 : : already there. */
2020 : :
2021 : : unsigned
2022 : 196766 : elf_out::name (tree ident)
2023 : : {
2024 : 196766 : unsigned res = 0;
2025 : 196766 : if (ident)
2026 : : {
2027 : 196766 : bool existed;
2028 : 196766 : int *slot = &identtab.get_or_insert (ident, &existed);
2029 : 196766 : if (!existed)
2030 : 352450 : *slot = strtab_write (IDENTIFIER_POINTER (ident),
2031 : 176225 : IDENTIFIER_LENGTH (ident) + 1);
2032 : 196766 : res = *slot;
2033 : : }
2034 : 196766 : return res;
2035 : : }
2036 : :
2037 : : /* Map LITERAL to strtab offset. Does not detect duplicates and
2038 : : expects LITERAL to remain live until strtab is written out. */
2039 : :
2040 : : unsigned
2041 : 21482 : elf_out::name (const char *literal)
2042 : : {
2043 : 21482 : return strtab_write (literal, strlen (literal) + 1);
2044 : : }
2045 : :
2046 : : /* Map a DECL's qualified name to strtab offset. Does not detect
2047 : : duplicates. */
2048 : :
2049 : : unsigned
2050 : 344854 : elf_out::qualified_name (tree decl, bool is_defn)
2051 : : {
2052 : 344854 : gcc_checking_assert (DECL_P (decl) && decl != global_namespace);
2053 : 344854 : unsigned result = strtab.pos;
2054 : :
2055 : 344854 : strtab_write (decl, is_defn);
2056 : 344854 : strtab_write ("", 1);
2057 : :
2058 : 344854 : return result;
2059 : : }
2060 : :
2061 : : /* Add section to file. Return section number. TYPE & NAME identify
2062 : : the section. OFF and SIZE identify the file location of its
2063 : : data. FLAGS contains additional info. */
2064 : :
2065 : : unsigned
2066 : 376135 : elf_out::add (unsigned type, unsigned name, unsigned off, unsigned size,
2067 : : unsigned flags)
2068 : : {
2069 : 376135 : gcc_checking_assert (!(off & (SECTION_ALIGN - 1)));
2070 : 376135 : if (sectab.pos + sizeof (section) > sectab.size)
2071 : 2960 : data::simple_memory.grow (sectab, sectab.pos + sizeof (section), false);
2072 : 376135 : section *sec = reinterpret_cast<section *> (sectab.buffer + sectab.pos);
2073 : 376135 : memset (sec, 0, sizeof (section));
2074 : 376135 : sec->type = type;
2075 : 376135 : sec->flags = flags;
2076 : 376135 : sec->name = name;
2077 : 376135 : sec->offset = off;
2078 : 376135 : sec->size = size;
2079 : 376135 : if (flags & SHF_STRINGS)
2080 : 3648 : sec->entsize = 1;
2081 : :
2082 : 376135 : unsigned res = sectab.pos;
2083 : 376135 : sectab.pos += sizeof (section);
2084 : 376135 : return res / sizeof (section);
2085 : : }
2086 : :
2087 : : /* Pad to the next alignment boundary, then write BUFFER to disk.
2088 : : Return the position of the start of the write, or zero on failure. */
2089 : :
2090 : : unsigned
2091 : 7290 : elf_out::write (const data &buffer)
2092 : : {
2093 : : #if MAPPED_WRITING
2094 : : /* HDR is always mapped. */
2095 : 7290 : if (&buffer != &hdr)
2096 : : {
2097 : 3648 : bytes_out out (this);
2098 : 3648 : grow (out, buffer.pos, true);
2099 : 3648 : if (out.buffer)
2100 : 3648 : memcpy (out.buffer, buffer.buffer, buffer.pos);
2101 : 3648 : shrink (out);
2102 : 3648 : }
2103 : : else
2104 : : /* We should have been aligned during the first allocation. */
2105 : 3642 : gcc_checking_assert (!(pos & (SECTION_ALIGN - 1)));
2106 : : #else
2107 : : if (::write (fd, buffer.buffer, buffer.pos) != ssize_t (buffer.pos))
2108 : : {
2109 : : set_error (errno);
2110 : : return 0;
2111 : : }
2112 : : #endif
2113 : 7290 : unsigned res = pos;
2114 : 7290 : pos += buffer.pos;
2115 : :
2116 : 7290 : if (unsigned padding = -pos & (SECTION_ALIGN - 1))
2117 : : {
2118 : : #if !MAPPED_WRITING
2119 : : /* Align the section on disk, should help the necessary copies.
2120 : : fseeking to extend is non-portable. */
2121 : : static char zero[SECTION_ALIGN];
2122 : : if (::write (fd, &zero, padding) != ssize_t (padding))
2123 : : set_error (errno);
2124 : : #endif
2125 : 6319 : pos += padding;
2126 : : }
2127 : 7290 : return res;
2128 : : }
2129 : :
2130 : : /* Write a streaming buffer. It must be using us as an allocator. */
2131 : :
2132 : : #if MAPPED_WRITING
2133 : : unsigned
2134 : 372487 : elf_out::write (const bytes_out &buf)
2135 : : {
2136 : 372487 : gcc_checking_assert (buf.memory == this);
2137 : : /* A directly mapped buffer. */
2138 : 372487 : gcc_checking_assert (buf.buffer - hdr.buffer >= 0
2139 : : && buf.buffer - hdr.buffer + buf.size <= extent);
2140 : 372487 : unsigned res = pos;
2141 : 372487 : pos += buf.pos;
2142 : :
2143 : : /* Align up. We're not going to advance into the next page. */
2144 : 372487 : pos += -pos & (SECTION_ALIGN - 1);
2145 : :
2146 : 372487 : return res;
2147 : : }
2148 : : #endif
2149 : :
2150 : : /* Write data and add section. STRING_P is true for a string
2151 : : section, false for PROGBITS. NAME identifies the section (0 is the
2152 : : empty name). DATA is the contents. Return section number or 0 on
2153 : : failure (0 is the undef section). */
2154 : :
2155 : : unsigned
2156 : 372487 : elf_out::add (const bytes_out &data, bool string_p, unsigned name)
2157 : : {
2158 : 372487 : unsigned off = write (data);
2159 : :
2160 : 744974 : return add (string_p ? SHT_STRTAB : SHT_PROGBITS, name,
2161 : 372487 : off, data.pos, string_p ? SHF_STRINGS : SHF_NONE);
2162 : : }
2163 : :
2164 : : /* Begin writing the file. Initialize the section table and write an
2165 : : empty header. Return false on failure. */
2166 : :
2167 : : bool
2168 : 1824 : elf_out::begin ()
2169 : : {
2170 : 1824 : if (!parent::begin ())
2171 : : return false;
2172 : :
2173 : : /* Let the allocators pick a default. */
2174 : 1824 : data::simple_memory.grow (strtab, 0, false);
2175 : 1824 : data::simple_memory.grow (sectab, 0, false);
2176 : :
2177 : : /* The string table starts with an empty string. */
2178 : 1824 : name ("");
2179 : :
2180 : : /* Create the UNDEF section. */
2181 : 1824 : add (SHT_NONE);
2182 : :
2183 : : #if MAPPED_WRITING
2184 : : /* Start a mapping. */
2185 : 1824 : create_mapping (EXPERIMENT (page_size,
2186 : : (32767 + page_size) & ~(page_size - 1)));
2187 : 1824 : if (!hdr.buffer)
2188 : : return false;
2189 : : #endif
2190 : :
2191 : : /* Write an empty header. */
2192 : 1824 : grow (hdr, sizeof (header), true);
2193 : 1824 : header *h = reinterpret_cast<header *> (hdr.buffer);
2194 : 1824 : memset (h, 0, sizeof (header));
2195 : 1824 : hdr.pos = hdr.size;
2196 : 1824 : write (hdr);
2197 : 1824 : return !get_error ();
2198 : : }
2199 : :
2200 : : /* Finish writing the file. Write out the string & section tables.
2201 : : Fill in the header. Return true on error. */
2202 : :
2203 : : bool
2204 : 1894 : elf_out::end ()
2205 : : {
2206 : 1894 : if (fd >= 0)
2207 : : {
2208 : : /* Write the string table. */
2209 : 1824 : unsigned strnam = name (".strtab");
2210 : 1824 : unsigned stroff = write (strtab);
2211 : 1824 : unsigned strndx = add (SHT_STRTAB, strnam, stroff, strtab.pos,
2212 : : SHF_STRINGS);
2213 : :
2214 : : /* Store escape values in section[0]. */
2215 : 1824 : if (strndx >= SHN_LORESERVE)
2216 : : {
2217 : 0 : reinterpret_cast<section *> (sectab.buffer)->link = strndx;
2218 : 0 : strndx = SHN_XINDEX;
2219 : : }
2220 : 1824 : unsigned shnum = sectab.pos / sizeof (section);
2221 : 1824 : if (shnum >= SHN_LORESERVE)
2222 : : {
2223 : 0 : reinterpret_cast<section *> (sectab.buffer)->size = shnum;
2224 : 0 : shnum = SHN_XINDEX;
2225 : : }
2226 : :
2227 : 1824 : unsigned shoff = write (sectab);
2228 : :
2229 : : #if MAPPED_WRITING
2230 : 1824 : if (offset)
2231 : : {
2232 : 121 : remove_mapping ();
2233 : 121 : offset = 0;
2234 : 121 : create_mapping ((sizeof (header) + page_size - 1) & ~(page_size - 1),
2235 : : false);
2236 : : }
2237 : 1824 : unsigned length = pos;
2238 : : #else
2239 : : if (lseek (fd, 0, SEEK_SET) < 0)
2240 : : set_error (errno);
2241 : : #endif
2242 : : /* Write header. */
2243 : 1824 : if (!get_error ())
2244 : : {
2245 : : /* Write the correct header now. */
2246 : 1818 : header *h = reinterpret_cast<header *> (hdr.buffer);
2247 : 1818 : h->ident.magic[0] = 0x7f;
2248 : 1818 : h->ident.magic[1] = 'E'; /* Elrond */
2249 : 1818 : h->ident.magic[2] = 'L'; /* is an */
2250 : 1818 : h->ident.magic[3] = 'F'; /* elf. */
2251 : 1818 : h->ident.klass = MY_CLASS;
2252 : 1818 : h->ident.data = MY_ENDIAN;
2253 : 1818 : h->ident.version = EV_CURRENT;
2254 : 1818 : h->ident.osabi = OSABI_NONE;
2255 : 1818 : h->type = ET_NONE;
2256 : 1818 : h->machine = EM_NONE;
2257 : 1818 : h->version = EV_CURRENT;
2258 : 1818 : h->shoff = shoff;
2259 : 1818 : h->ehsize = sizeof (header);
2260 : 1818 : h->shentsize = sizeof (section);
2261 : 1818 : h->shnum = shnum;
2262 : 1818 : h->shstrndx = strndx;
2263 : :
2264 : 1818 : pos = 0;
2265 : 1818 : write (hdr);
2266 : : }
2267 : :
2268 : : #if MAPPED_WRITING
2269 : 1824 : remove_mapping ();
2270 : 1824 : if (ftruncate (fd, length))
2271 : 0 : set_error (errno);
2272 : : #endif
2273 : : }
2274 : :
2275 : 1894 : data::simple_memory.shrink (sectab);
2276 : 1894 : data::simple_memory.shrink (strtab);
2277 : :
2278 : 1894 : return parent::end ();
2279 : : }
2280 : :
2281 : : /********************************************************************/
2282 : :
2283 : : /* A dependency set. This is used during stream out to determine the
2284 : : connectivity of the graph. Every namespace-scope declaration that
2285 : : needs writing has a depset. The depset is filled with the (depsets
2286 : : of) declarations within this module that it references. For a
2287 : : declaration that'll generally be named types. For definitions
2288 : : it'll also be declarations in the body.
2289 : :
2290 : : From that we can convert the graph to a DAG, via determining the
2291 : : Strongly Connected Clusters. Each cluster is streamed
2292 : : independently, and thus we achieve lazy loading.
2293 : :
2294 : : Other decls that get a depset are namespaces themselves and
2295 : : unnameable declarations. */
2296 : :
2297 : : class depset {
2298 : : private:
2299 : : tree entity; /* Entity, or containing namespace. */
2300 : : uintptr_t discriminator; /* Flags or identifier. */
2301 : :
2302 : : public:
2303 : : /* The kinds of entity the depset could describe. The ordering is
2304 : : significant, see entity_kind_name. */
2305 : : enum entity_kind
2306 : : {
2307 : : EK_DECL, /* A decl. */
2308 : : EK_SPECIALIZATION, /* A specialization. */
2309 : : EK_PARTIAL, /* A partial specialization. */
2310 : : EK_USING, /* A using declaration (at namespace scope). */
2311 : : EK_NAMESPACE, /* A namespace. */
2312 : : EK_REDIRECT, /* Redirect to a template_decl. */
2313 : : EK_EXPLICIT_HWM,
2314 : : EK_BINDING = EK_EXPLICIT_HWM, /* Implicitly encoded. */
2315 : : EK_FOR_BINDING, /* A decl being inserted for a binding. */
2316 : : EK_INNER_DECL, /* A decl defined outside of its imported
2317 : : context. */
2318 : : EK_DIRECT_HWM = EK_PARTIAL + 1,
2319 : :
2320 : : EK_BITS = 3 /* Only need to encode below EK_EXPLICIT_HWM. */
2321 : : };
2322 : :
2323 : : private:
2324 : : /* Placement of bit fields in discriminator. */
2325 : : enum disc_bits
2326 : : {
2327 : : DB_ZERO_BIT, /* Set to disambiguate identifier from flags */
2328 : : DB_SPECIAL_BIT, /* First dep slot is special. */
2329 : : DB_KIND_BIT, /* Kind of the entity. */
2330 : : DB_KIND_BITS = EK_BITS,
2331 : : DB_DEFN_BIT = DB_KIND_BIT + DB_KIND_BITS,
2332 : : DB_IS_MEMBER_BIT, /* Is an out-of-class member. */
2333 : : DB_IS_INTERNAL_BIT, /* It is an (erroneous)
2334 : : internal-linkage entity. */
2335 : : DB_REFS_INTERNAL_BIT, /* Refers to an internal-linkage
2336 : : entity. */
2337 : : DB_IMPORTED_BIT, /* An imported entity. */
2338 : : DB_UNREACHED_BIT, /* A yet-to-be reached entity. */
2339 : : DB_HIDDEN_BIT, /* A hidden binding. */
2340 : : /* The following bits are not independent, but enumerating them is
2341 : : awkward. */
2342 : : DB_TYPE_SPEC_BIT, /* Specialization in the type table. */
2343 : : DB_FRIEND_SPEC_BIT, /* An instantiated template friend. */
2344 : : };
2345 : :
2346 : : public:
2347 : : /* The first slot is special for EK_SPECIALIZATIONS it is a
2348 : : spec_entry pointer. It is not relevant for the SCC
2349 : : determination. */
2350 : : vec<depset *> deps; /* Depsets we reference. */
2351 : :
2352 : : public:
2353 : : unsigned cluster; /* Strongly connected cluster, later entity number */
2354 : : unsigned section; /* Section written to. */
2355 : : /* During SCC construction, section is lowlink, until the depset is
2356 : : removed from the stack. See Tarjan algorithm for details. */
2357 : :
2358 : : private:
2359 : : /* Construction via factories. Destruction via hash traits. */
2360 : : depset (tree entity);
2361 : : ~depset ();
2362 : :
2363 : : public:
2364 : : static depset *make_binding (tree, tree);
2365 : : static depset *make_entity (tree, entity_kind, bool = false);
2366 : : /* Late setting a binding name -- /then/ insert into hash! */
2367 : : inline void set_binding_name (tree name)
2368 : : {
2369 : : gcc_checking_assert (!get_name ());
2370 : : discriminator = reinterpret_cast<uintptr_t> (name);
2371 : : }
2372 : :
2373 : : private:
2374 : 2953528 : template<unsigned I> void set_flag_bit ()
2375 : : {
2376 : 0 : gcc_checking_assert (I < 2 || !is_binding ());
2377 : 2953528 : discriminator |= 1u << I;
2378 : 953979 : }
2379 : 470297 : template<unsigned I> void clear_flag_bit ()
2380 : : {
2381 : 0 : gcc_checking_assert (I < 2 || !is_binding ());
2382 : 470297 : discriminator &= ~(1u << I);
2383 : 470297 : }
2384 : 533392958 : template<unsigned I> bool get_flag_bit () const
2385 : : {
2386 : 0 : gcc_checking_assert (I < 2 || !is_binding ());
2387 : 605458241 : return bool ((discriminator >> I) & 1);
2388 : : }
2389 : :
2390 : : public:
2391 : 521500010 : bool is_binding () const
2392 : : {
2393 : 73489559 : return !get_flag_bit<DB_ZERO_BIT> ();
2394 : : }
2395 : 316231593 : entity_kind get_entity_kind () const
2396 : : {
2397 : 26887 : if (is_binding ())
2398 : : return EK_BINDING;
2399 : 238866608 : return entity_kind ((discriminator >> DB_KIND_BIT) & ((1u << EK_BITS) - 1));
2400 : : }
2401 : : const char *entity_kind_name () const;
2402 : :
2403 : : public:
2404 : 9610226 : bool has_defn () const
2405 : : {
2406 : 774 : return get_flag_bit<DB_DEFN_BIT> ();
2407 : : }
2408 : :
2409 : : public:
2410 : : /* This class-member is defined here, but the class was imported. */
2411 : 1422495 : bool is_member () const
2412 : : {
2413 : 1422495 : gcc_checking_assert (get_entity_kind () == EK_DECL);
2414 : 1422495 : return get_flag_bit<DB_IS_MEMBER_BIT> ();
2415 : : }
2416 : : public:
2417 : 6022126 : bool is_internal () const
2418 : : {
2419 : 6022126 : return get_flag_bit<DB_IS_INTERNAL_BIT> ();
2420 : : }
2421 : 1540846 : bool refs_internal () const
2422 : : {
2423 : 1540846 : return get_flag_bit<DB_REFS_INTERNAL_BIT> ();
2424 : : }
2425 : 30475041 : bool is_import () const
2426 : : {
2427 : 9722723 : return get_flag_bit<DB_IMPORTED_BIT> ();
2428 : : }
2429 : 18302128 : bool is_unreached () const
2430 : : {
2431 : 1285063 : return get_flag_bit<DB_UNREACHED_BIT> ();
2432 : : }
2433 : 1564165 : bool is_hidden () const
2434 : : {
2435 : 1564165 : return get_flag_bit<DB_HIDDEN_BIT> ();
2436 : : }
2437 : 1564128 : bool is_type_spec () const
2438 : : {
2439 : 1564128 : return get_flag_bit<DB_TYPE_SPEC_BIT> ();
2440 : : }
2441 : 1564128 : bool is_friend_spec () const
2442 : : {
2443 : 1564128 : return get_flag_bit<DB_FRIEND_SPEC_BIT> ();
2444 : : }
2445 : :
2446 : : public:
2447 : : /* We set these bit outside of depset. */
2448 : 5403 : void set_hidden_binding ()
2449 : : {
2450 : 5403 : set_flag_bit<DB_HIDDEN_BIT> ();
2451 : 5403 : }
2452 : 9 : void clear_hidden_binding ()
2453 : : {
2454 : 9 : clear_flag_bit<DB_HIDDEN_BIT> ();
2455 : 9 : }
2456 : :
2457 : : public:
2458 : 11892948 : bool is_special () const
2459 : : {
2460 : 11892948 : return get_flag_bit<DB_SPECIAL_BIT> ();
2461 : : }
2462 : 1999549 : void set_special ()
2463 : : {
2464 : 1999549 : set_flag_bit<DB_SPECIAL_BIT> ();
2465 : 0 : }
2466 : :
2467 : : public:
2468 : 143887722 : tree get_entity () const
2469 : : {
2470 : 143887722 : return entity;
2471 : : }
2472 : 25877206 : tree get_name () const
2473 : : {
2474 : 25877206 : gcc_checking_assert (is_binding ());
2475 : 25877206 : return reinterpret_cast <tree> (discriminator);
2476 : : }
2477 : :
2478 : : public:
2479 : : /* Traits for a hash table of pointers to bindings. */
2480 : : struct traits {
2481 : : /* Each entry is a pointer to a depset. */
2482 : : typedef depset *value_type;
2483 : : /* We lookup by container:maybe-identifier pair. */
2484 : : typedef std::pair<tree,tree> compare_type;
2485 : :
2486 : : static const bool empty_zero_p = true;
2487 : :
2488 : : /* hash and equality for compare_type. */
2489 : 18026716 : inline static hashval_t hash (const compare_type &p)
2490 : : {
2491 : 18026716 : hashval_t h = pointer_hash<tree_node>::hash (p.first);
2492 : 18026716 : if (p.second)
2493 : : {
2494 : 194401 : hashval_t nh = IDENTIFIER_HASH_VALUE (p.second);
2495 : 194401 : h = iterative_hash_hashval_t (h, nh);
2496 : : }
2497 : 18026716 : return h;
2498 : : }
2499 : 89288617 : inline static bool equal (const value_type b, const compare_type &p)
2500 : : {
2501 : 89288617 : if (b->entity != p.first)
2502 : : return false;
2503 : :
2504 : 12793599 : if (p.second)
2505 : 19713 : return b->discriminator == reinterpret_cast<uintptr_t> (p.second);
2506 : : else
2507 : 12773886 : return !b->is_binding ();
2508 : : }
2509 : :
2510 : : /* (re)hasher for a binding itself. */
2511 : 69507657 : inline static hashval_t hash (const value_type b)
2512 : : {
2513 : 69507657 : hashval_t h = pointer_hash<tree_node>::hash (b->entity);
2514 : 69507657 : if (b->is_binding ())
2515 : : {
2516 : 5569622 : hashval_t nh = IDENTIFIER_HASH_VALUE (b->get_name ());
2517 : 5569622 : h = iterative_hash_hashval_t (h, nh);
2518 : : }
2519 : 69507657 : return h;
2520 : : }
2521 : :
2522 : : /* Empty via NULL. */
2523 : 0 : static inline void mark_empty (value_type &p) {p = NULL;}
2524 : : static inline bool is_empty (value_type p) {return !p;}
2525 : :
2526 : : /* Nothing is deletable. Everything is insertable. */
2527 : : static bool is_deleted (value_type) { return false; }
2528 : : static void mark_deleted (value_type) { gcc_unreachable (); }
2529 : :
2530 : : /* We own the entities in the hash table. */
2531 : 2993423 : static void remove (value_type p)
2532 : : {
2533 : 2993423 : delete (p);
2534 : 2993423 : }
2535 : : };
2536 : :
2537 : : public:
2538 : : class hash : public hash_table<traits> {
2539 : : typedef traits::compare_type key_t;
2540 : : typedef hash_table<traits> parent;
2541 : :
2542 : : public:
2543 : : vec<depset *> worklist; /* Worklist of decls to walk. */
2544 : : hash *chain; /* Original table. */
2545 : : depset *current; /* Current depset being depended. */
2546 : : unsigned section; /* When writing out, the section. */
2547 : : bool reached_unreached; /* We reached an unreached entity. */
2548 : :
2549 : : public:
2550 : 360125 : hash (size_t size, hash *c = NULL)
2551 : 720250 : : parent (size), chain (c), current (NULL), section (0),
2552 : 360125 : reached_unreached (false)
2553 : : {
2554 : 360125 : worklist.create (size);
2555 : 360125 : }
2556 : 360125 : ~hash ()
2557 : : {
2558 : 360125 : worklist.release ();
2559 : 360125 : }
2560 : :
2561 : : public:
2562 : 50775677 : bool is_key_order () const
2563 : : {
2564 : 50775677 : return chain != NULL;
2565 : : }
2566 : :
2567 : : private:
2568 : : depset **entity_slot (tree entity, bool = true);
2569 : : depset **binding_slot (tree ctx, tree name, bool = true);
2570 : : depset *maybe_add_declaration (tree decl);
2571 : :
2572 : : public:
2573 : : depset *find_dependency (tree entity);
2574 : : depset *find_binding (tree ctx, tree name);
2575 : : depset *make_dependency (tree decl, entity_kind);
2576 : : void add_dependency (depset *);
2577 : :
2578 : : public:
2579 : : void add_mergeable (depset *);
2580 : : depset *add_dependency (tree decl, entity_kind);
2581 : : void add_namespace_context (depset *, tree ns);
2582 : :
2583 : : private:
2584 : : static bool add_binding_entity (tree, WMB_Flags, void *);
2585 : :
2586 : : public:
2587 : : bool add_namespace_entities (tree ns, bitmap partitions);
2588 : : void add_specializations (bool decl_p);
2589 : : void add_partial_entities (vec<tree, va_gc> *);
2590 : : void add_class_entities (vec<tree, va_gc> *);
2591 : :
2592 : : public:
2593 : : void find_dependencies (module_state *);
2594 : : bool finalize_dependencies ();
2595 : : vec<depset *> connect ();
2596 : : };
2597 : :
2598 : : public:
2599 : : struct tarjan {
2600 : : vec<depset *> result;
2601 : : vec<depset *> stack;
2602 : : unsigned index;
2603 : :
2604 : 360119 : tarjan (unsigned size)
2605 : 360119 : : index (0)
2606 : : {
2607 : 360119 : result.create (size);
2608 : 360119 : stack.create (50);
2609 : 360119 : }
2610 : 360119 : ~tarjan ()
2611 : : {
2612 : 360119 : gcc_assert (!stack.length ());
2613 : 360119 : stack.release ();
2614 : 360119 : }
2615 : :
2616 : : public:
2617 : : void connect (depset *);
2618 : : };
2619 : : };
2620 : :
2621 : : inline
2622 : 2993423 : depset::depset (tree entity)
2623 : 2993423 : :entity (entity), discriminator (0), cluster (0), section (0)
2624 : : {
2625 : 2993423 : deps.create (0);
2626 : : }
2627 : :
2628 : : inline
2629 : 2993423 : depset::~depset ()
2630 : : {
2631 : 2993423 : deps.release ();
2632 : 2993423 : }
2633 : :
2634 : : const char *
2635 : 244754 : depset::entity_kind_name () const
2636 : : {
2637 : : /* Same order as entity_kind. */
2638 : 244754 : static const char *const names[] =
2639 : : {"decl", "specialization", "partial", "using",
2640 : : "namespace", "redirect", "binding"};
2641 : 244754 : entity_kind kind = get_entity_kind ();
2642 : 244559 : gcc_checking_assert (kind < ARRAY_SIZE (names));
2643 : 244754 : return names[kind];
2644 : : }
2645 : :
2646 : : /* Create a depset for a namespace binding NS::NAME. */
2647 : :
2648 : 194401 : depset *depset::make_binding (tree ns, tree name)
2649 : : {
2650 : 194401 : depset *binding = new depset (ns);
2651 : :
2652 : 194401 : binding->discriminator = reinterpret_cast <uintptr_t> (name);
2653 : :
2654 : 194401 : return binding;
2655 : : }
2656 : :
2657 : 2799022 : depset *depset::make_entity (tree entity, entity_kind ek, bool is_defn)
2658 : : {
2659 : 2799022 : depset *r = new depset (entity);
2660 : :
2661 : 2799022 : r->discriminator = ((1 << DB_ZERO_BIT)
2662 : 2799022 : | (ek << DB_KIND_BIT)
2663 : 2799022 : | is_defn << DB_DEFN_BIT);
2664 : :
2665 : 2799022 : return r;
2666 : : }
2667 : :
2668 : : class pending_key
2669 : : {
2670 : : public:
2671 : : tree ns;
2672 : : tree id;
2673 : : };
2674 : :
2675 : : template<>
2676 : : struct default_hash_traits<pending_key>
2677 : : {
2678 : : using value_type = pending_key;
2679 : :
2680 : : static const bool empty_zero_p = false;
2681 : 46970731 : static hashval_t hash (const value_type &k)
2682 : : {
2683 : 46970731 : hashval_t h = IDENTIFIER_HASH_VALUE (k.id);
2684 : 46970731 : h = iterative_hash_hashval_t (DECL_UID (k.ns), h);
2685 : :
2686 : 46970731 : return h;
2687 : : }
2688 : 12135884 : static bool equal (const value_type &k, const value_type &l)
2689 : : {
2690 : 12135884 : return k.ns == l.ns && k.id == l.id;
2691 : : }
2692 : 203979 : static void mark_empty (value_type &k)
2693 : : {
2694 : 203979 : k.ns = k.id = NULL_TREE;
2695 : : }
2696 : 4887 : static void mark_deleted (value_type &k)
2697 : : {
2698 : 4887 : k.ns = NULL_TREE;
2699 : 4887 : gcc_checking_assert (k.id);
2700 : 4887 : }
2701 : 359408479 : static bool is_empty (const value_type &k)
2702 : : {
2703 : 359358070 : return k.ns == NULL_TREE && k.id == NULL_TREE;
2704 : : }
2705 : 16663946 : static bool is_deleted (const value_type &k)
2706 : : {
2707 : 16663946 : return k.ns == NULL_TREE && k.id != NULL_TREE;
2708 : : }
2709 : : static void remove (value_type &)
2710 : : {
2711 : : }
2712 : : };
2713 : :
2714 : : typedef hash_map<pending_key, auto_vec<unsigned>> pending_map_t;
2715 : :
2716 : : /* Not-loaded entities that are keyed to a namespace-scope
2717 : : identifier. See module_state::write_pendings for details. */
2718 : : pending_map_t *pending_table;
2719 : :
2720 : : /* Decls that need some post processing once a batch of lazy loads has
2721 : : completed. */
2722 : : vec<tree, va_heap, vl_embed> *post_load_decls;
2723 : :
2724 : : /* Some entities are keyed to another entitity for ODR purposes.
2725 : : For example, at namespace scope, 'inline auto var = []{};', that
2726 : : lambda is keyed to 'var', and follows its ODRness. */
2727 : : typedef hash_map<tree, auto_vec<tree>> keyed_map_t;
2728 : : static keyed_map_t *keyed_table;
2729 : :
2730 : : /********************************************************************/
2731 : : /* Tree streaming. The tree streaming is very specific to the tree
2732 : : structures themselves. A tag indicates the kind of tree being
2733 : : streamed. -ve tags indicate backreferences to already-streamed
2734 : : trees. Backreferences are auto-numbered. */
2735 : :
2736 : : /* Tree tags. */
2737 : : enum tree_tag {
2738 : : tt_null, /* NULL_TREE. */
2739 : : tt_fixed, /* Fixed vector index. */
2740 : :
2741 : : tt_node, /* By-value node. */
2742 : : tt_decl, /* By-value mergeable decl. */
2743 : : tt_tpl_parm, /* Template parm. */
2744 : :
2745 : : /* The ordering of the following 4 is relied upon in
2746 : : trees_out::tree_node. */
2747 : : tt_id, /* Identifier node. */
2748 : : tt_conv_id, /* Conversion operator name. */
2749 : : tt_anon_id, /* Anonymous name. */
2750 : : tt_lambda_id, /* Lambda name. */
2751 : :
2752 : : tt_typedef_type, /* A (possibly implicit) typedefed type. */
2753 : : tt_derived_type, /* A type derived from another type. */
2754 : : tt_variant_type, /* A variant of another type. */
2755 : :
2756 : : tt_tinfo_var, /* Typeinfo object. */
2757 : : tt_tinfo_typedef, /* Typeinfo typedef. */
2758 : : tt_ptrmem_type, /* Pointer to member type. */
2759 : : tt_nttp_var, /* NTTP_OBJECT VAR_DECL. */
2760 : :
2761 : : tt_parm, /* Function parameter or result. */
2762 : : tt_enum_value, /* An enum value. */
2763 : : tt_enum_decl, /* An enum decl. */
2764 : : tt_data_member, /* Data member/using-decl. */
2765 : :
2766 : : tt_binfo, /* A BINFO. */
2767 : : tt_vtable, /* A vtable. */
2768 : : tt_thunk, /* A thunk. */
2769 : : tt_clone_ref,
2770 : :
2771 : : tt_entity, /* A extra-cluster entity. */
2772 : :
2773 : : tt_template, /* The TEMPLATE_RESULT of a template. */
2774 : : };
2775 : :
2776 : : enum walk_kind {
2777 : : WK_none, /* No walk to do (a back- or fixed-ref happened). */
2778 : : WK_normal, /* Normal walk (by-name if possible). */
2779 : :
2780 : : WK_value, /* By-value walk. */
2781 : : };
2782 : :
2783 : : enum merge_kind
2784 : : {
2785 : : MK_unique, /* Known unique. */
2786 : : MK_named, /* Found by CTX, NAME + maybe_arg types etc. */
2787 : : MK_field, /* Found by CTX and index on TYPE_FIELDS */
2788 : : MK_vtable, /* Found by CTX and index on TYPE_VTABLES */
2789 : : MK_as_base, /* Found by CTX. */
2790 : :
2791 : : MK_partial,
2792 : :
2793 : : MK_enum, /* Found by CTX, & 1stMemberNAME. */
2794 : : MK_keyed, /* Found by key & index. */
2795 : : MK_local_type, /* Found by CTX, index. */
2796 : :
2797 : : MK_friend_spec, /* Like named, but has a tmpl & args too. */
2798 : : MK_local_friend, /* Found by CTX, index. */
2799 : :
2800 : : MK_indirect_lwm = MK_enum,
2801 : :
2802 : : /* Template specialization kinds below. These are all found via
2803 : : primary template and specialization args. */
2804 : : MK_template_mask = 0x10, /* A template specialization. */
2805 : :
2806 : : MK_tmpl_decl_mask = 0x4, /* In decl table. */
2807 : :
2808 : : MK_tmpl_tmpl_mask = 0x1, /* We want TEMPLATE_DECL. */
2809 : :
2810 : : MK_type_spec = MK_template_mask,
2811 : : MK_decl_spec = MK_template_mask | MK_tmpl_decl_mask,
2812 : :
2813 : : MK_hwm = 0x20
2814 : : };
2815 : : /* This is more than a debugging array. NULLs are used to determine
2816 : : an invalid merge_kind number. */
2817 : : static char const *const merge_kind_name[MK_hwm] =
2818 : : {
2819 : : "unique", "named", "field", "vtable", /* 0...3 */
2820 : : "asbase", "partial", "enum", "attached", /* 4...7 */
2821 : :
2822 : : "local type", "friend spec", "local friend", NULL, /* 8...11 */
2823 : : NULL, NULL, NULL, NULL,
2824 : :
2825 : : "type spec", "type tmpl spec", /* 16,17 type (template). */
2826 : : NULL, NULL,
2827 : :
2828 : : "decl spec", "decl tmpl spec", /* 20,21 decl (template). */
2829 : : NULL, NULL,
2830 : : NULL, NULL, NULL, NULL,
2831 : : NULL, NULL, NULL, NULL,
2832 : : };
2833 : :
2834 : : /* Mergeable entity location data. */
2835 : : struct merge_key {
2836 : : cp_ref_qualifier ref_q : 2;
2837 : : unsigned index;
2838 : :
2839 : : tree ret; /* Return type, if appropriate. */
2840 : : tree args; /* Arg types, if appropriate. */
2841 : :
2842 : : tree constraints; /* Constraints. */
2843 : :
2844 : 3484266 : merge_key ()
2845 : 3484266 : :ref_q (REF_QUAL_NONE), index (0),
2846 : 3484266 : ret (NULL_TREE), args (NULL_TREE),
2847 : 3484266 : constraints (NULL_TREE)
2848 : : {
2849 : : }
2850 : : };
2851 : :
2852 : : /* Hashmap of merged duplicates. Usually decls, but can contain
2853 : : BINFOs. */
2854 : : typedef hash_map<tree,uintptr_t,
2855 : : simple_hashmap_traits<nodel_ptr_hash<tree_node>,uintptr_t> >
2856 : : duplicate_hash_map;
2857 : :
2858 : : /* Data needed for post-processing. */
2859 : : struct post_process_data {
2860 : : tree decl;
2861 : : location_t start_locus;
2862 : : location_t end_locus;
2863 : : };
2864 : :
2865 : : /* Tree stream reader. Note that reading a stream doesn't mark the
2866 : : read trees with TREE_VISITED. Thus it's quite safe to have
2867 : : multiple concurrent readers. Which is good, because lazy
2868 : : loading.
2869 : :
2870 : : It's important that trees_in/out have internal linkage so that the
2871 : : compiler knows core_bools, lang_type_bools and lang_decl_bools have
2872 : : only a single caller (tree_node_bools) and inlines them appropriately. */
2873 : : namespace {
2874 : : class trees_in : public bytes_in {
2875 : : typedef bytes_in parent;
2876 : :
2877 : : private:
2878 : : module_state *state; /* Module being imported. */
2879 : : vec<tree> back_refs; /* Back references. */
2880 : : duplicate_hash_map *duplicates; /* Map from existings to duplicate. */
2881 : : vec<post_process_data> post_decls; /* Decls to post process. */
2882 : : unsigned unused; /* Inhibit any interior TREE_USED
2883 : : marking. */
2884 : :
2885 : : public:
2886 : : trees_in (module_state *);
2887 : : ~trees_in ();
2888 : :
2889 : : public:
2890 : : int insert (tree);
2891 : : tree back_ref (int);
2892 : :
2893 : : private:
2894 : : tree start (unsigned = 0);
2895 : :
2896 : : public:
2897 : : /* Needed for binfo writing */
2898 : : bool core_bools (tree, bits_in&);
2899 : :
2900 : : private:
2901 : : /* Stream tree_core, lang_decl_specific and lang_type_specific
2902 : : bits. */
2903 : : bool core_vals (tree);
2904 : : bool lang_type_bools (tree, bits_in&);
2905 : : bool lang_type_vals (tree);
2906 : : bool lang_decl_bools (tree, bits_in&);
2907 : : bool lang_decl_vals (tree);
2908 : : bool lang_vals (tree);
2909 : : bool tree_node_bools (tree);
2910 : : bool tree_node_vals (tree);
2911 : : tree tree_value ();
2912 : : tree decl_value ();
2913 : : tree tpl_parm_value ();
2914 : :
2915 : : private:
2916 : : tree chained_decls (); /* Follow DECL_CHAIN. */
2917 : : vec<tree, va_heap> *vec_chained_decls ();
2918 : : vec<tree, va_gc> *tree_vec (); /* vec of tree. */
2919 : : vec<tree_pair_s, va_gc> *tree_pair_vec (); /* vec of tree_pair. */
2920 : : tree tree_list (bool has_purpose);
2921 : :
2922 : : public:
2923 : : /* Read a tree node. */
2924 : : tree tree_node (bool is_use = false);
2925 : :
2926 : : private:
2927 : : bool install_entity (tree decl);
2928 : : tree tpl_parms (unsigned &tpl_levels);
2929 : : bool tpl_parms_fini (tree decl, unsigned tpl_levels);
2930 : : bool tpl_header (tree decl, unsigned *tpl_levels);
2931 : : int fn_parms_init (tree);
2932 : : void fn_parms_fini (int tag, tree fn, tree existing, bool has_defn);
2933 : : unsigned add_indirect_tpl_parms (tree);
2934 : : public:
2935 : : bool add_indirects (tree);
2936 : :
2937 : : public:
2938 : : /* Serialize various definitions. */
2939 : : bool read_definition (tree decl);
2940 : :
2941 : : private:
2942 : : bool is_matching_decl (tree existing, tree decl, bool is_typedef);
2943 : : static bool install_implicit_member (tree decl);
2944 : : bool read_function_def (tree decl, tree maybe_template);
2945 : : bool read_var_def (tree decl, tree maybe_template);
2946 : : bool read_class_def (tree decl, tree maybe_template);
2947 : : bool read_enum_def (tree decl, tree maybe_template);
2948 : :
2949 : : public:
2950 : : tree decl_container ();
2951 : : tree key_mergeable (int tag, merge_kind, tree decl, tree inner, tree type,
2952 : : tree container, bool is_attached);
2953 : : unsigned binfo_mergeable (tree *);
2954 : :
2955 : : private:
2956 : : tree key_local_type (const merge_key&, tree, tree);
2957 : : uintptr_t *find_duplicate (tree existing);
2958 : : void register_duplicate (tree decl, tree existing);
2959 : : /* Mark as an already diagnosed bad duplicate. */
2960 : 21 : void unmatched_duplicate (tree existing)
2961 : : {
2962 : 42 : *find_duplicate (existing) |= 1;
2963 : 21 : }
2964 : :
2965 : : public:
2966 : 274253 : bool is_duplicate (tree decl)
2967 : : {
2968 : 548506 : return find_duplicate (decl) != NULL;
2969 : : }
2970 : 229206 : tree maybe_duplicate (tree decl)
2971 : : {
2972 : 229206 : if (uintptr_t *dup = find_duplicate (decl))
2973 : 17135 : return reinterpret_cast<tree> (*dup & ~uintptr_t (1));
2974 : : return decl;
2975 : : }
2976 : : tree odr_duplicate (tree decl, bool has_defn);
2977 : :
2978 : : public:
2979 : : /* Return the decls to postprocess. */
2980 : : const vec<post_process_data>& post_process ()
2981 : : {
2982 : : return post_decls;
2983 : : }
2984 : : private:
2985 : : /* Register DATA for postprocessing. */
2986 : 135521 : void post_process (post_process_data data)
2987 : : {
2988 : 135521 : post_decls.safe_push (data);
2989 : : }
2990 : :
2991 : : private:
2992 : : void assert_definition (tree, bool installing);
2993 : : };
2994 : : } // anon namespace
2995 : :
2996 : 360870 : trees_in::trees_in (module_state *state)
2997 : 360870 : :parent (), state (state), unused (0)
2998 : : {
2999 : 360870 : duplicates = NULL;
3000 : 360870 : back_refs.create (500);
3001 : 360870 : post_decls.create (0);
3002 : 360870 : }
3003 : :
3004 : 360870 : trees_in::~trees_in ()
3005 : : {
3006 : 573309 : delete (duplicates);
3007 : 360870 : back_refs.release ();
3008 : 360870 : post_decls.release ();
3009 : 360870 : }
3010 : :
3011 : : /* Tree stream writer. */
3012 : : namespace {
3013 : : class trees_out : public bytes_out {
3014 : : typedef bytes_out parent;
3015 : :
3016 : : private:
3017 : : module_state *state; /* The module we are writing. */
3018 : : ptr_int_hash_map tree_map; /* Trees to references */
3019 : : depset::hash *dep_hash; /* Dependency table. */
3020 : : int ref_num; /* Back reference number. */
3021 : : unsigned section;
3022 : : #if CHECKING_P
3023 : : int importedness; /* Checker that imports not occurring
3024 : : inappropriately. +ve imports ok,
3025 : : -ve imports not ok. */
3026 : : #endif
3027 : :
3028 : : public:
3029 : : trees_out (allocator *, module_state *, depset::hash &deps, unsigned sec = 0);
3030 : : ~trees_out ();
3031 : :
3032 : : private:
3033 : : void mark_trees ();
3034 : : void unmark_trees ();
3035 : :
3036 : : public:
3037 : : /* Hey, let's ignore the well known STL iterator idiom. */
3038 : : void begin ();
3039 : : unsigned end (elf_out *sink, unsigned name, unsigned *crc_ptr);
3040 : : void end ();
3041 : :
3042 : : public:
3043 : : enum tags
3044 : : {
3045 : : tag_backref = -1, /* Upper bound on the backrefs. */
3046 : : tag_value = 0, /* Write by value. */
3047 : : tag_fixed /* Lower bound on the fixed trees. */
3048 : : };
3049 : :
3050 : : public:
3051 : 23958266 : bool is_key_order () const
3052 : : {
3053 : 23958266 : return dep_hash->is_key_order ();
3054 : : }
3055 : :
3056 : : public:
3057 : : int insert (tree, walk_kind = WK_normal);
3058 : :
3059 : : private:
3060 : : void start (tree, bool = false);
3061 : :
3062 : : private:
3063 : : walk_kind ref_node (tree);
3064 : : public:
3065 : : int get_tag (tree);
3066 : 716602 : void set_importing (int i ATTRIBUTE_UNUSED)
3067 : : {
3068 : : #if CHECKING_P
3069 : 716602 : importedness = i;
3070 : : #endif
3071 : : }
3072 : :
3073 : : private:
3074 : : void core_bools (tree, bits_out&);
3075 : : void core_vals (tree);
3076 : : void lang_type_bools (tree, bits_out&);
3077 : : void lang_type_vals (tree);
3078 : : void lang_decl_bools (tree, bits_out&);
3079 : : void lang_decl_vals (tree);
3080 : : void lang_vals (tree);
3081 : : void tree_node_bools (tree);
3082 : : void tree_node_vals (tree);
3083 : :
3084 : : private:
3085 : : void chained_decls (tree);
3086 : : void vec_chained_decls (tree);
3087 : : void tree_vec (vec<tree, va_gc> *);
3088 : : void tree_pair_vec (vec<tree_pair_s, va_gc> *);
3089 : : void tree_list (tree, bool has_purpose);
3090 : :
3091 : : public:
3092 : : /* Mark a node for by-value walking. */
3093 : : void mark_by_value (tree);
3094 : :
3095 : : public:
3096 : : void tree_node (tree);
3097 : :
3098 : : private:
3099 : : void install_entity (tree decl, depset *);
3100 : : void tpl_parms (tree parms, unsigned &tpl_levels);
3101 : : void tpl_parms_fini (tree decl, unsigned tpl_levels);
3102 : : void fn_parms_fini (tree) {}
3103 : : unsigned add_indirect_tpl_parms (tree);
3104 : : public:
3105 : : void add_indirects (tree);
3106 : : void fn_parms_init (tree);
3107 : : void tpl_header (tree decl, unsigned *tpl_levels);
3108 : :
3109 : : public:
3110 : : merge_kind get_merge_kind (tree decl, depset *maybe_dep);
3111 : : tree decl_container (tree decl);
3112 : : void key_mergeable (int tag, merge_kind, tree decl, tree inner,
3113 : : tree container, depset *maybe_dep);
3114 : : void binfo_mergeable (tree binfo);
3115 : :
3116 : : private:
3117 : : void key_local_type (merge_key&, tree, tree);
3118 : : bool decl_node (tree, walk_kind ref);
3119 : : void type_node (tree);
3120 : : void tree_value (tree);
3121 : : void tpl_parm_value (tree);
3122 : :
3123 : : public:
3124 : : void decl_value (tree, depset *);
3125 : :
3126 : : public:
3127 : : /* Serialize various definitions. */
3128 : : void write_definition (tree decl);
3129 : : void mark_declaration (tree decl, bool do_defn);
3130 : :
3131 : : private:
3132 : : void mark_function_def (tree decl);
3133 : : void mark_var_def (tree decl);
3134 : : void mark_class_def (tree decl);
3135 : : void mark_enum_def (tree decl);
3136 : : void mark_class_member (tree decl, bool do_defn = true);
3137 : : void mark_binfos (tree type);
3138 : :
3139 : : private:
3140 : : void write_var_def (tree decl);
3141 : : void write_function_def (tree decl);
3142 : : void write_class_def (tree decl);
3143 : : void write_enum_def (tree decl);
3144 : :
3145 : : private:
3146 : : static void assert_definition (tree);
3147 : :
3148 : : public:
3149 : : static void instrument ();
3150 : :
3151 : : private:
3152 : : /* Tree instrumentation. */
3153 : : static unsigned tree_val_count;
3154 : : static unsigned decl_val_count;
3155 : : static unsigned back_ref_count;
3156 : : static unsigned null_count;
3157 : : };
3158 : : } // anon namespace
3159 : :
3160 : : /* Instrumentation counters. */
3161 : : unsigned trees_out::tree_val_count;
3162 : : unsigned trees_out::decl_val_count;
3163 : : unsigned trees_out::back_ref_count;
3164 : : unsigned trees_out::null_count;
3165 : :
3166 : 720270 : trees_out::trees_out (allocator *mem, module_state *state, depset::hash &deps,
3167 : 720270 : unsigned section)
3168 : 1440540 : :parent (mem), state (state), tree_map (500),
3169 : 720270 : dep_hash (&deps), ref_num (0), section (section)
3170 : : {
3171 : : #if CHECKING_P
3172 : 720270 : importedness = 0;
3173 : : #endif
3174 : 720270 : }
3175 : :
3176 : 720270 : trees_out::~trees_out ()
3177 : : {
3178 : 720270 : }
3179 : :
3180 : : /********************************************************************/
3181 : : /* Location. We're aware of the line-map concept and reproduce it
3182 : : here. Each imported module allocates a contiguous span of ordinary
3183 : : maps, and of macro maps. adhoc maps are serialized by contents,
3184 : : not pre-allocated. The scattered linemaps of a module are
3185 : : coalesced when writing. */
3186 : :
3187 : :
3188 : : /* I use half-open [first,second) ranges. */
3189 : : typedef std::pair<unsigned,unsigned> range_t;
3190 : :
3191 : : /* A range of locations. */
3192 : : typedef std::pair<location_t,location_t> loc_range_t;
3193 : :
3194 : : /* Spans of the line maps that are occupied by this TU. I.e. not
3195 : : within imports. Only extended when in an interface unit.
3196 : : Interval zero corresponds to the forced header linemap(s). This
3197 : : is a singleton object. */
3198 : :
3199 : : class loc_spans {
3200 : : public:
3201 : : /* An interval of line maps. The line maps here represent a contiguous
3202 : : non-imported range. */
3203 : 4034 : struct span {
3204 : : loc_range_t ordinary; /* Ordinary map location range. */
3205 : : loc_range_t macro; /* Macro map location range. */
3206 : : int ordinary_delta; /* Add to ordinary loc to get serialized loc. */
3207 : : int macro_delta; /* Likewise for macro loc. */
3208 : : };
3209 : :
3210 : : private:
3211 : : vec<span> *spans;
3212 : :
3213 : : public:
3214 : : loc_spans ()
3215 : : /* Do not preallocate spans, as that causes
3216 : : --enable-detailed-mem-stats problems. */
3217 : : : spans (nullptr)
3218 : : {
3219 : : }
3220 : 100716 : ~loc_spans ()
3221 : : {
3222 : 100716 : delete spans;
3223 : 100716 : }
3224 : :
3225 : : public:
3226 : 228 : span &operator[] (unsigned ix)
3227 : : {
3228 : 456 : return (*spans)[ix];
3229 : : }
3230 : : unsigned length () const
3231 : : {
3232 : : return spans->length ();
3233 : : }
3234 : :
3235 : : public:
3236 : 39929 : bool init_p () const
3237 : : {
3238 : 39929 : return spans != nullptr;
3239 : : }
3240 : : /* Initializer. */
3241 : : void init (const line_maps *lmaps, const line_map_ordinary *map);
3242 : :
3243 : : /* Slightly skewed preprocessed files can cause us to miss an
3244 : : initialization in some places. Fallback initializer. */
3245 : 3947 : void maybe_init ()
3246 : : {
3247 : 3947 : if (!init_p ())
3248 : 3 : init (line_table, nullptr);
3249 : 3947 : }
3250 : :
3251 : : public:
3252 : : enum {
3253 : : SPAN_RESERVED = 0, /* Reserved (fixed) locations. */
3254 : : SPAN_FIRST = 1, /* LWM of locations to stream */
3255 : : SPAN_MAIN = 2 /* Main file and onwards. */
3256 : : };
3257 : :
3258 : : public:
3259 : 399552 : location_t main_start () const
3260 : : {
3261 : 399552 : return (*spans)[SPAN_MAIN].ordinary.first;
3262 : : }
3263 : :
3264 : : public:
3265 : : void open (location_t);
3266 : : void close ();
3267 : :
3268 : : public:
3269 : : /* Propagate imported linemaps to us, if needed. */
3270 : : bool maybe_propagate (module_state *import, location_t loc);
3271 : :
3272 : : public:
3273 : : const span *ordinary (location_t);
3274 : : const span *macro (location_t);
3275 : : };
3276 : :
3277 : : static loc_spans spans;
3278 : :
3279 : : /* Information about ordinary locations we stream out. */
3280 : : struct ord_loc_info
3281 : : {
3282 : : const line_map_ordinary *src; // line map we're based on
3283 : : unsigned offset; // offset to this line
3284 : : unsigned span; // number of locs we span
3285 : : unsigned remap; // serialization
3286 : :
3287 : 313744137 : static int compare (const void *a_, const void *b_)
3288 : : {
3289 : 313744137 : auto *a = static_cast<const ord_loc_info *> (a_);
3290 : 313744137 : auto *b = static_cast<const ord_loc_info *> (b_);
3291 : :
3292 : 313744137 : if (a->src != b->src)
3293 : 72170488 : return a->src < b->src ? -1 : +1;
3294 : :
3295 : : // Ensure no overlap
3296 : 264976857 : gcc_checking_assert (a->offset + a->span <= b->offset
3297 : : || b->offset + b->span <= a->offset);
3298 : :
3299 : 264976857 : gcc_checking_assert (a->offset != b->offset);
3300 : 264976857 : return a->offset < b->offset ? -1 : +1;
3301 : : }
3302 : : };
3303 : : struct ord_loc_traits
3304 : : {
3305 : : typedef ord_loc_info value_type;
3306 : : typedef value_type compare_type;
3307 : :
3308 : : static const bool empty_zero_p = false;
3309 : :
3310 : 134260757 : static hashval_t hash (const value_type &v)
3311 : : {
3312 : 113399946 : auto h = pointer_hash<const line_map_ordinary>::hash (v.src);
3313 : 134260757 : return iterative_hash_hashval_t (v.offset, h);
3314 : : }
3315 : 148599371 : static bool equal (const value_type &v, const compare_type p)
3316 : : {
3317 : 148599371 : return v.src == p.src && v.offset == p.offset;
3318 : : }
3319 : :
3320 : 8969810 : static void mark_empty (value_type &v)
3321 : : {
3322 : 8969810 : v.src = nullptr;
3323 : : }
3324 : 436313930 : static bool is_empty (value_type &v)
3325 : : {
3326 : 436313930 : return !v.src;
3327 : : }
3328 : :
3329 : : static bool is_deleted (value_type &) { return false; }
3330 : : static void mark_deleted (value_type &) { gcc_unreachable (); }
3331 : :
3332 : : static void remove (value_type &) {}
3333 : : };
3334 : : /* Table keyed by ord_loc_info, used for noting. */
3335 : : static hash_table<ord_loc_traits> *ord_loc_table;
3336 : : /* Sorted vector, used for writing. */
3337 : : static vec<ord_loc_info> *ord_loc_remap;
3338 : :
3339 : : /* Information about macro locations we stream out. */
3340 : : struct macro_loc_info
3341 : : {
3342 : : const line_map_macro *src; // original expansion
3343 : : unsigned remap; // serialization
3344 : :
3345 : 12877829 : static int compare (const void *a_, const void *b_)
3346 : : {
3347 : 12877829 : auto *a = static_cast<const macro_loc_info *> (a_);
3348 : 12877829 : auto *b = static_cast<const macro_loc_info *> (b_);
3349 : :
3350 : 12877829 : gcc_checking_assert (MAP_START_LOCATION (a->src)
3351 : : != MAP_START_LOCATION (b->src));
3352 : 12877829 : if (MAP_START_LOCATION (a->src) < MAP_START_LOCATION (b->src))
3353 : : return -1;
3354 : : else
3355 : 6222131 : return +1;
3356 : : }
3357 : : };
3358 : : struct macro_loc_traits
3359 : : {
3360 : : typedef macro_loc_info value_type;
3361 : : typedef const line_map_macro *compare_type;
3362 : :
3363 : : static const bool empty_zero_p = false;
3364 : :
3365 : 12935108 : static hashval_t hash (compare_type p)
3366 : : {
3367 : 12935108 : return pointer_hash<const line_map_macro>::hash (p);
3368 : : }
3369 : 10935413 : static hashval_t hash (const value_type &v)
3370 : : {
3371 : 10935413 : return hash (v.src);
3372 : : }
3373 : : static bool equal (const value_type &v, const compare_type p)
3374 : : {
3375 : : return v.src == p;
3376 : : }
3377 : :
3378 : 700613 : static void mark_empty (value_type &v)
3379 : : {
3380 : 700613 : v.src = nullptr;
3381 : : }
3382 : 40621930 : static bool is_empty (value_type &v)
3383 : : {
3384 : 40621930 : return !v.src;
3385 : : }
3386 : :
3387 : : static bool is_deleted (value_type &) { return false; }
3388 : : static void mark_deleted (value_type &) { gcc_unreachable (); }
3389 : :
3390 : : static void remove (value_type &) {}
3391 : : };
3392 : : /* Table keyed by line_map_macro, used for noting. */
3393 : : static hash_table<macro_loc_traits> *macro_loc_table;
3394 : : /* Sorted vector, used for writing. */
3395 : : static vec<macro_loc_info> *macro_loc_remap;
3396 : :
3397 : : /* Indirection to allow bsearching imports by ordinary location. */
3398 : : static vec<module_state *> *ool;
3399 : :
3400 : : /********************************************************************/
3401 : : /* Data needed by a module during the process of loading. */
3402 : : struct GTY(()) slurping {
3403 : :
3404 : : /* Remap import's module numbering to our numbering. Values are
3405 : : shifted by 1. Bit0 encodes if the import is direct. */
3406 : : vec<unsigned, va_heap, vl_embed> *
3407 : : GTY((skip)) remap; /* Module owner remapping. */
3408 : :
3409 : : elf_in *GTY((skip)) from; /* The elf loader. */
3410 : :
3411 : : /* This map is only for header imports themselves -- the global
3412 : : headers bitmap hold it for the current TU. */
3413 : : bitmap headers; /* Transitive set of direct imports, including
3414 : : self. Used for macro visibility and
3415 : : priority. */
3416 : :
3417 : : /* These objects point into the mmapped area, unless we're not doing
3418 : : that, or we got frozen or closed. In those cases they point to
3419 : : buffers we own. */
3420 : : bytes_in macro_defs; /* Macro definitions. */
3421 : : bytes_in macro_tbl; /* Macro table. */
3422 : :
3423 : : /* Location remapping. first->ordinary, second->macro. */
3424 : : range_t GTY((skip)) loc_deltas;
3425 : :
3426 : : unsigned current; /* Section currently being loaded. */
3427 : : unsigned remaining; /* Number of lazy sections yet to read. */
3428 : : unsigned lru; /* An LRU counter. */
3429 : :
3430 : : public:
3431 : : slurping (elf_in *);
3432 : : ~slurping ();
3433 : :
3434 : : public:
3435 : : /* Close the ELF file, if it's open. */
3436 : 3645 : void close ()
3437 : : {
3438 : 3645 : if (from)
3439 : : {
3440 : 1986 : from->end ();
3441 : 3972 : delete from;
3442 : 1986 : from = NULL;
3443 : : }
3444 : 3645 : }
3445 : :
3446 : : public:
3447 : : void release_macros ();
3448 : :
3449 : : public:
3450 : 2029 : void alloc_remap (unsigned size)
3451 : : {
3452 : 2029 : gcc_assert (!remap);
3453 : 2029 : vec_safe_reserve (remap, size);
3454 : 4320 : for (unsigned ix = size; ix--;)
3455 : 2291 : remap->quick_push (0);
3456 : 2029 : }
3457 : 1783305 : unsigned remap_module (unsigned owner)
3458 : : {
3459 : 1783305 : if (owner < remap->length ())
3460 : 1783305 : return (*remap)[owner] >> 1;
3461 : : return 0;
3462 : : }
3463 : :
3464 : : public:
3465 : : /* GC allocation. But we must explicitly delete it. */
3466 : 2047 : static void *operator new (size_t x)
3467 : : {
3468 : 4094 : return ggc_alloc_atomic (x);
3469 : : }
3470 : 1986 : static void operator delete (void *p)
3471 : : {
3472 : 1986 : ggc_free (p);
3473 : 1986 : }
3474 : : };
3475 : :
3476 : 2047 : slurping::slurping (elf_in *from)
3477 : 2047 : : remap (NULL), from (from),
3478 : 2047 : headers (BITMAP_GGC_ALLOC ()), macro_defs (), macro_tbl (),
3479 : 2047 : loc_deltas (0, 0),
3480 : 2047 : current (~0u), remaining (0), lru (0)
3481 : : {
3482 : 2047 : }
3483 : :
3484 : 1986 : slurping::~slurping ()
3485 : : {
3486 : 1986 : vec_free (remap);
3487 : 1986 : remap = NULL;
3488 : 1986 : release_macros ();
3489 : 1986 : close ();
3490 : 1986 : }
3491 : :
3492 : 3645 : void slurping::release_macros ()
3493 : : {
3494 : 3645 : if (macro_defs.size)
3495 : 699 : elf_in::release (from, macro_defs);
3496 : 3645 : if (macro_tbl.size)
3497 : 0 : elf_in::release (from, macro_tbl);
3498 : 3645 : }
3499 : :
3500 : : /* Flags for extensions that end up being streamed. */
3501 : :
3502 : : enum streamed_extensions {
3503 : : SE_OPENMP = 1 << 0,
3504 : : SE_BITS = 1
3505 : : };
3506 : :
3507 : : /* Counter indices. */
3508 : : enum module_state_counts
3509 : : {
3510 : : MSC_sec_lwm,
3511 : : MSC_sec_hwm,
3512 : : MSC_pendings,
3513 : : MSC_entities,
3514 : : MSC_namespaces,
3515 : : MSC_bindings,
3516 : : MSC_macros,
3517 : : MSC_inits,
3518 : : MSC_HWM
3519 : : };
3520 : :
3521 : : /********************************************************************/
3522 : : struct module_state_config;
3523 : :
3524 : : /* Increasing levels of loadedness. */
3525 : : enum module_loadedness {
3526 : : ML_NONE, /* Not loaded. */
3527 : : ML_CONFIG, /* Config loaed. */
3528 : : ML_PREPROCESSOR, /* Preprocessor loaded. */
3529 : : ML_LANGUAGE, /* Language loaded. */
3530 : : };
3531 : :
3532 : : /* Increasing levels of directness (toplevel) of import. */
3533 : : enum module_directness {
3534 : : MD_NONE, /* Not direct. */
3535 : : MD_PARTITION_DIRECT, /* Direct import of a partition. */
3536 : : MD_DIRECT, /* Direct import. */
3537 : : MD_PURVIEW_DIRECT, /* direct import in purview. */
3538 : : };
3539 : :
3540 : : /* State of a particular module. */
3541 : :
3542 : : class GTY((chain_next ("%h.parent"), for_user)) module_state {
3543 : : public:
3544 : : /* We always import & export ourselves. */
3545 : : bitmap imports; /* Transitive modules we're importing. */
3546 : : bitmap exports; /* Subset of that, that we're exporting. */
3547 : :
3548 : : module_state *parent;
3549 : : tree name; /* Name of the module. */
3550 : :
3551 : : slurping *slurp; /* Data for loading. */
3552 : :
3553 : : const char *flatname; /* Flatname of module. */
3554 : : char *filename; /* CMI Filename */
3555 : :
3556 : : /* Indices into the entity_ary. */
3557 : : unsigned entity_lwm;
3558 : : unsigned entity_num;
3559 : :
3560 : : /* Location ranges for this module. adhoc-locs are decomposed, so
3561 : : don't have a range. */
3562 : : loc_range_t GTY((skip)) ordinary_locs;
3563 : : loc_range_t GTY((skip)) macro_locs; // [lwm,num)
3564 : :
3565 : : /* LOC is first set too the importing location. When initially
3566 : : loaded it refers to a module loc whose parent is the importing
3567 : : location. */
3568 : : location_t loc; /* Location referring to module itself. */
3569 : : unsigned crc; /* CRC we saw reading it in. */
3570 : :
3571 : : unsigned mod; /* Module owner number. */
3572 : : unsigned remap; /* Remapping during writing. */
3573 : :
3574 : : unsigned short subst; /* Mangle subst if !0. */
3575 : :
3576 : : /* How loaded this module is. */
3577 : : enum module_loadedness loadedness : 2;
3578 : :
3579 : : bool module_p : 1; /* /The/ module of this TU. */
3580 : : bool header_p : 1; /* Is a header unit. */
3581 : : bool interface_p : 1; /* An interface. */
3582 : : bool partition_p : 1; /* A partition. */
3583 : :
3584 : : /* How directly this module is imported. */
3585 : : enum module_directness directness : 2;
3586 : :
3587 : : bool exported_p : 1; /* directness != MD_NONE && exported. */
3588 : : bool cmi_noted_p : 1; /* We've told the user about the CMI, don't
3589 : : do it again */
3590 : : bool active_init_p : 1; /* This module's global initializer needs
3591 : : calling. */
3592 : : bool inform_cmi_p : 1; /* Inform of a read/write. */
3593 : : bool visited_p : 1; /* A walk-once flag. */
3594 : : /* Record extensions emitted or permitted. */
3595 : : unsigned extensions : SE_BITS;
3596 : : /* 14 bits used, 2 bits remain */
3597 : :
3598 : : public:
3599 : : module_state (tree name, module_state *, bool);
3600 : : ~module_state ();
3601 : :
3602 : : public:
3603 : 2052 : void release ()
3604 : : {
3605 : 2052 : imports = exports = NULL;
3606 : 2052 : slurped ();
3607 : 1986 : }
3608 : 3711 : void slurped ()
3609 : : {
3610 : 3711 : delete slurp;
3611 : 3711 : slurp = NULL;
3612 : 3711 : }
3613 : 2102930 : elf_in *from () const
3614 : : {
3615 : 2102930 : return slurp->from;
3616 : : }
3617 : :
3618 : : public:
3619 : : /* Kind of this module. */
3620 : 10035 : bool is_module () const
3621 : : {
3622 : 10035 : return module_p;
3623 : : }
3624 : 2932579 : bool is_header () const
3625 : : {
3626 : 2932579 : return header_p;
3627 : : }
3628 : 318 : bool is_interface () const
3629 : : {
3630 : 318 : return interface_p;
3631 : : }
3632 : 47116 : bool is_partition () const
3633 : : {
3634 : 47116 : return partition_p;
3635 : : }
3636 : :
3637 : : /* How this module is used in the current TU. */
3638 : 30 : bool is_exported () const
3639 : : {
3640 : 30 : return exported_p;
3641 : : }
3642 : 14012 : bool is_direct () const
3643 : : {
3644 : 14012 : return directness >= MD_DIRECT;
3645 : : }
3646 : 210 : bool is_purview_direct () const
3647 : : {
3648 : 210 : return directness == MD_PURVIEW_DIRECT;
3649 : : }
3650 : 299 : bool is_partition_direct () const
3651 : : {
3652 : 299 : return directness == MD_PARTITION_DIRECT;
3653 : : }
3654 : :
3655 : : public:
3656 : : /* Is this a real module? */
3657 : 11891 : bool has_location () const
3658 : : {
3659 : 11891 : return loc != UNKNOWN_LOCATION;
3660 : : }
3661 : :
3662 : : public:
3663 : : bool check_not_purview (location_t loc);
3664 : :
3665 : : public:
3666 : : void mangle (bool include_partition);
3667 : :
3668 : : public:
3669 : : void set_import (module_state const *, bool is_export);
3670 : : void announce (const char *) const;
3671 : :
3672 : : public:
3673 : : /* Read and write module. */
3674 : : void write_begin (elf_out *to, cpp_reader *,
3675 : : module_state_config &, unsigned &crc);
3676 : : void write_end (elf_out *to, cpp_reader *,
3677 : : module_state_config &, unsigned &crc);
3678 : : bool read_initial (cpp_reader *);
3679 : : bool read_preprocessor (bool);
3680 : : bool read_language (bool);
3681 : :
3682 : : public:
3683 : : /* Read a section. */
3684 : : bool load_section (unsigned snum, binding_slot *mslot);
3685 : : /* Lazily read a section. */
3686 : : bool lazy_load (unsigned index, binding_slot *mslot);
3687 : :
3688 : : public:
3689 : : /* Juggle a limited number of file numbers. */
3690 : : static void freeze_an_elf ();
3691 : : bool maybe_defrost ();
3692 : :
3693 : : public:
3694 : : void maybe_completed_reading ();
3695 : : bool check_read (bool outermost, bool ok);
3696 : :
3697 : : private:
3698 : : /* The README, for human consumption. */
3699 : : void write_readme (elf_out *to, cpp_reader *, const char *dialect);
3700 : : void write_env (elf_out *to);
3701 : :
3702 : : private:
3703 : : /* Import tables. */
3704 : : void write_imports (bytes_out &cfg, bool direct);
3705 : : unsigned read_imports (bytes_in &cfg, cpp_reader *, line_maps *maps);
3706 : :
3707 : : private:
3708 : : void write_imports (elf_out *to, unsigned *crc_ptr);
3709 : : bool read_imports (cpp_reader *, line_maps *);
3710 : :
3711 : : private:
3712 : : void write_partitions (elf_out *to, unsigned, unsigned *crc_ptr);
3713 : : bool read_partitions (unsigned);
3714 : :
3715 : : private:
3716 : : void write_config (elf_out *to, struct module_state_config &, unsigned crc);
3717 : : bool read_config (struct module_state_config &);
3718 : : static void write_counts (elf_out *to, unsigned [MSC_HWM], unsigned *crc_ptr);
3719 : : bool read_counts (unsigned *);
3720 : :
3721 : : public:
3722 : : void note_cmi_name ();
3723 : :
3724 : : private:
3725 : : static unsigned write_bindings (elf_out *to, vec<depset *> depsets,
3726 : : unsigned *crc_ptr);
3727 : : bool read_bindings (unsigned count, unsigned lwm, unsigned hwm);
3728 : :
3729 : : static void write_namespace (bytes_out &sec, depset *ns_dep);
3730 : : tree read_namespace (bytes_in &sec);
3731 : :
3732 : : void write_namespaces (elf_out *to, vec<depset *> spaces,
3733 : : unsigned, unsigned *crc_ptr);
3734 : : bool read_namespaces (unsigned);
3735 : :
3736 : : void intercluster_seed (trees_out &sec, unsigned index, depset *dep);
3737 : : unsigned write_cluster (elf_out *to, depset *depsets[], unsigned size,
3738 : : depset::hash &, unsigned *counts, unsigned *crc_ptr);
3739 : : bool read_cluster (unsigned snum);
3740 : :
3741 : : private:
3742 : : unsigned write_inits (elf_out *to, depset::hash &, unsigned *crc_ptr);
3743 : : bool read_inits (unsigned count);
3744 : :
3745 : : private:
3746 : : unsigned write_pendings (elf_out *to, vec<depset *> depsets,
3747 : : depset::hash &, unsigned *crc_ptr);
3748 : : bool read_pendings (unsigned count);
3749 : :
3750 : : private:
3751 : : void write_entities (elf_out *to, vec<depset *> depsets,
3752 : : unsigned count, unsigned *crc_ptr);
3753 : : bool read_entities (unsigned count, unsigned lwm, unsigned hwm);
3754 : :
3755 : : private:
3756 : : void write_init_maps ();
3757 : : range_t write_prepare_maps (module_state_config *, bool);
3758 : : bool read_prepare_maps (const module_state_config *);
3759 : :
3760 : : void write_ordinary_maps (elf_out *to, range_t &,
3761 : : bool, unsigned *crc_ptr);
3762 : : bool read_ordinary_maps (unsigned, unsigned);
3763 : : void write_macro_maps (elf_out *to, range_t &, unsigned *crc_ptr);
3764 : : bool read_macro_maps (unsigned);
3765 : :
3766 : : private:
3767 : : void write_define (bytes_out &, const cpp_macro *);
3768 : : cpp_macro *read_define (bytes_in &, cpp_reader *) const;
3769 : : vec<cpp_hashnode *> *prepare_macros (cpp_reader *);
3770 : : unsigned write_macros (elf_out *to, vec<cpp_hashnode *> *, unsigned *crc_ptr);
3771 : : bool read_macros ();
3772 : : void install_macros ();
3773 : :
3774 : : public:
3775 : : void import_macros ();
3776 : :
3777 : : public:
3778 : : static void undef_macro (cpp_reader *, location_t, cpp_hashnode *);
3779 : : static cpp_macro *deferred_macro (cpp_reader *, location_t, cpp_hashnode *);
3780 : :
3781 : : public:
3782 : : static bool note_location (location_t);
3783 : : static void write_location (bytes_out &, location_t);
3784 : : location_t read_location (bytes_in &) const;
3785 : :
3786 : : public:
3787 : : void set_flatname ();
3788 : 36065 : const char *get_flatname () const
3789 : : {
3790 : 36065 : return flatname;
3791 : : }
3792 : : location_t imported_from () const;
3793 : :
3794 : : public:
3795 : : void set_filename (const Cody::Packet &);
3796 : : bool do_import (cpp_reader *, bool outermost);
3797 : : };
3798 : :
3799 : : /* Hash module state by name. This cannot be a member of
3800 : : module_state, because of GTY restrictions. We never delete from
3801 : : the hash table, but ggc_ptr_hash doesn't support that
3802 : : simplification. */
3803 : :
3804 : : struct module_state_hash : ggc_ptr_hash<module_state> {
3805 : : typedef std::pair<tree,uintptr_t> compare_type; /* {name,parent} */
3806 : :
3807 : : static inline hashval_t hash (const value_type m);
3808 : : static inline hashval_t hash (const compare_type &n);
3809 : : static inline bool equal (const value_type existing,
3810 : : const compare_type &candidate);
3811 : : };
3812 : :
3813 : 7597 : module_state::module_state (tree name, module_state *parent, bool partition)
3814 : 7597 : : imports (BITMAP_GGC_ALLOC ()), exports (BITMAP_GGC_ALLOC ()),
3815 : 7597 : parent (parent), name (name), slurp (NULL),
3816 : 7597 : flatname (NULL), filename (NULL),
3817 : 7597 : entity_lwm (~0u >> 1), entity_num (0),
3818 : 7597 : ordinary_locs (0, 0), macro_locs (0, 0),
3819 : 7597 : loc (UNKNOWN_LOCATION),
3820 : 7597 : crc (0), mod (MODULE_UNKNOWN), remap (0), subst (0)
3821 : : {
3822 : 7597 : loadedness = ML_NONE;
3823 : :
3824 : 7597 : module_p = header_p = interface_p = partition_p = false;
3825 : :
3826 : 7597 : directness = MD_NONE;
3827 : 7597 : exported_p = false;
3828 : :
3829 : 7597 : cmi_noted_p = false;
3830 : 7597 : active_init_p = false;
3831 : :
3832 : 7597 : partition_p = partition;
3833 : :
3834 : 7597 : inform_cmi_p = false;
3835 : 7597 : visited_p = false;
3836 : :
3837 : 7597 : extensions = 0;
3838 : 7597 : if (name && TREE_CODE (name) == STRING_CST)
3839 : : {
3840 : 1490 : header_p = true;
3841 : :
3842 : 1490 : const char *string = TREE_STRING_POINTER (name);
3843 : 1490 : gcc_checking_assert (string[0] == '.'
3844 : : ? IS_DIR_SEPARATOR (string[1])
3845 : : : IS_ABSOLUTE_PATH (string));
3846 : : }
3847 : :
3848 : 7597 : gcc_checking_assert (!(parent && header_p));
3849 : 7597 : }
3850 : :
3851 : 66 : module_state::~module_state ()
3852 : : {
3853 : 66 : release ();
3854 : 66 : }
3855 : :
3856 : : /* Hash module state. */
3857 : : static hashval_t
3858 : 10454 : module_name_hash (const_tree name)
3859 : : {
3860 : 10454 : if (TREE_CODE (name) == STRING_CST)
3861 : 2718 : return htab_hash_string (TREE_STRING_POINTER (name));
3862 : : else
3863 : 7736 : return IDENTIFIER_HASH_VALUE (name);
3864 : : }
3865 : :
3866 : : hashval_t
3867 : 2448 : module_state_hash::hash (const value_type m)
3868 : : {
3869 : 2448 : hashval_t ph = pointer_hash<void>::hash
3870 : 2448 : (reinterpret_cast<void *> (reinterpret_cast<uintptr_t> (m->parent)
3871 : 2448 : | m->is_partition ()));
3872 : 2448 : hashval_t nh = module_name_hash (m->name);
3873 : 2448 : return iterative_hash_hashval_t (ph, nh);
3874 : : }
3875 : :
3876 : : /* Hash a name. */
3877 : : hashval_t
3878 : 8006 : module_state_hash::hash (const compare_type &c)
3879 : : {
3880 : 8006 : hashval_t ph = pointer_hash<void>::hash (reinterpret_cast<void *> (c.second));
3881 : 8006 : hashval_t nh = module_name_hash (c.first);
3882 : :
3883 : 8006 : return iterative_hash_hashval_t (ph, nh);
3884 : : }
3885 : :
3886 : : bool
3887 : 5248 : module_state_hash::equal (const value_type existing,
3888 : : const compare_type &candidate)
3889 : : {
3890 : 5248 : uintptr_t ep = (reinterpret_cast<uintptr_t> (existing->parent)
3891 : 5248 : | existing->is_partition ());
3892 : 5248 : if (ep != candidate.second)
3893 : : return false;
3894 : :
3895 : : /* Identifier comparison is by pointer. If the string_csts happen
3896 : : to be the same object, then they're equal too. */
3897 : 4400 : if (existing->name == candidate.first)
3898 : : return true;
3899 : :
3900 : : /* If neither are string csts, they can't be equal. */
3901 : 846 : if (TREE_CODE (candidate.first) != STRING_CST
3902 : 350 : || TREE_CODE (existing->name) != STRING_CST)
3903 : : return false;
3904 : :
3905 : : /* String equality. */
3906 : 295 : if (TREE_STRING_LENGTH (existing->name)
3907 : 295 : == TREE_STRING_LENGTH (candidate.first)
3908 : 295 : && !memcmp (TREE_STRING_POINTER (existing->name),
3909 : 292 : TREE_STRING_POINTER (candidate.first),
3910 : 292 : TREE_STRING_LENGTH (existing->name)))
3911 : 93 : return true;
3912 : :
3913 : : return false;
3914 : : }
3915 : :
3916 : : /********************************************************************/
3917 : : /* Global state */
3918 : :
3919 : : /* Mapper name. */
3920 : : static const char *module_mapper_name;
3921 : :
3922 : : /* Deferred import queue (FIFO). */
3923 : : static vec<module_state *, va_heap, vl_embed> *pending_imports;
3924 : :
3925 : : /* CMI repository path and workspace. */
3926 : : static char *cmi_repo;
3927 : : static size_t cmi_repo_length;
3928 : : static char *cmi_path;
3929 : : static size_t cmi_path_alloc;
3930 : :
3931 : : /* Count of available and loaded clusters. */
3932 : : static unsigned available_clusters;
3933 : : static unsigned loaded_clusters;
3934 : :
3935 : : /* What the current TU is. */
3936 : : unsigned module_kind;
3937 : :
3938 : : /* Global trees. */
3939 : : static const std::pair<tree *, unsigned> global_tree_arys[] =
3940 : : {
3941 : : std::pair<tree *, unsigned> (sizetype_tab, stk_type_kind_last),
3942 : : std::pair<tree *, unsigned> (integer_types, itk_none),
3943 : : std::pair<tree *, unsigned> (global_trees, TI_MODULE_HWM),
3944 : : std::pair<tree *, unsigned> (c_global_trees, CTI_MODULE_HWM),
3945 : : std::pair<tree *, unsigned> (cp_global_trees, CPTI_MODULE_HWM),
3946 : : std::pair<tree *, unsigned> (NULL, 0)
3947 : : };
3948 : : static GTY(()) vec<tree, va_gc> *fixed_trees;
3949 : : static unsigned global_crc;
3950 : :
3951 : : /* Lazy loading can open many files concurrently, there are
3952 : : per-process limits on that. We pay attention to the process limit,
3953 : : and attempt to increase it when we run out. Otherwise we use an
3954 : : LRU scheme to figure out who to flush. Note that if the import
3955 : : graph /depth/ exceeds lazy_limit, we'll exceed the limit. */
3956 : : static unsigned lazy_lru; /* LRU counter. */
3957 : : static unsigned lazy_open; /* Number of open modules */
3958 : : static unsigned lazy_limit; /* Current limit of open modules. */
3959 : : static unsigned lazy_hard_limit; /* Hard limit on open modules. */
3960 : : /* Account for source, assembler and dump files & directory searches.
3961 : : We don't keep the source file's open, so we don't have to account
3962 : : for #include depth. I think dump files are opened and closed per
3963 : : pass, but ICBW. */
3964 : : #define LAZY_HEADROOM 15 /* File descriptor headroom. */
3965 : :
3966 : : /* Vector of module state. Indexed by OWNER. Has at least 2 slots. */
3967 : : static GTY(()) vec<module_state *, va_gc> *modules;
3968 : :
3969 : : /* Hash of module state, findable by {name, parent}. */
3970 : : static GTY(()) hash_table<module_state_hash> *modules_hash;
3971 : :
3972 : : /* Map of imported entities. We map DECL_UID to index of entity
3973 : : vector. */
3974 : : typedef hash_map<unsigned/*UID*/, unsigned/*index*/,
3975 : : simple_hashmap_traits<int_hash<unsigned,0>, unsigned>
3976 : : > entity_map_t;
3977 : : static entity_map_t *entity_map;
3978 : : /* Doesn't need GTYing, because any tree referenced here is also
3979 : : findable by, symbol table, specialization table, return type of
3980 : : reachable function. */
3981 : : static vec<binding_slot, va_heap, vl_embed> *entity_ary;
3982 : :
3983 : : /* Members entities of imported classes that are defined in this TU.
3984 : : These are where the entity's context is not from the current TU.
3985 : : We need to emit the definition (but not the enclosing class).
3986 : :
3987 : : We could find these by walking ALL the imported classes that we
3988 : : could provide a member definition. But that's expensive,
3989 : : especially when you consider lazy implicit member declarations,
3990 : : which could be ANY imported class. */
3991 : : static GTY(()) vec<tree, va_gc> *class_members;
3992 : :
3993 : : /* The same problem exists for class template partial
3994 : : specializations. Now that we have constraints, the invariant of
3995 : : expecting them in the instantiation table no longer holds. One of
3996 : : the constrained partial specializations will be there, but the
3997 : : others not so much. It's not even an unconstrained partial
3998 : : spacialization in the table :( so any partial template declaration
3999 : : is added to this list too. */
4000 : : static GTY(()) vec<tree, va_gc> *partial_specializations;
4001 : :
4002 : : /********************************************************************/
4003 : :
4004 : : /* Our module mapper (created lazily). */
4005 : : module_client *mapper;
4006 : :
4007 : : static module_client *make_mapper (location_t loc, class mkdeps *deps);
4008 : 32293 : inline module_client *get_mapper (location_t loc, class mkdeps *deps)
4009 : : {
4010 : 32293 : auto *res = mapper;
4011 : 254 : if (!res)
4012 : 3238 : res = make_mapper (loc, deps);
4013 : 32293 : return res;
4014 : : }
4015 : :
4016 : : /********************************************************************/
4017 : : static tree
4018 : 407685 : get_clone_target (tree decl)
4019 : : {
4020 : 407685 : tree target;
4021 : :
4022 : 407685 : if (TREE_CODE (decl) == TEMPLATE_DECL)
4023 : : {
4024 : 44508 : tree res_orig = DECL_CLONED_FUNCTION (DECL_TEMPLATE_RESULT (decl));
4025 : :
4026 : 44508 : target = DECL_TI_TEMPLATE (res_orig);
4027 : : }
4028 : : else
4029 : 363177 : target = DECL_CLONED_FUNCTION (decl);
4030 : :
4031 : 407685 : gcc_checking_assert (DECL_MAYBE_IN_CHARGE_CDTOR_P (target));
4032 : :
4033 : 407685 : return target;
4034 : : }
4035 : :
4036 : : /* Like FOR_EACH_CLONE, but will walk cloned templates. */
4037 : : #define FOR_EVERY_CLONE(CLONE, FN) \
4038 : : if (!DECL_MAYBE_IN_CHARGE_CDTOR_P (FN)); \
4039 : : else \
4040 : : for (CLONE = DECL_CHAIN (FN); \
4041 : : CLONE && DECL_CLONED_FUNCTION_P (CLONE); \
4042 : : CLONE = DECL_CHAIN (CLONE))
4043 : :
4044 : : /* It'd be nice if USE_TEMPLATE was a field of template_info
4045 : : (a) it'd solve the enum case dealt with below,
4046 : : (b) both class templates and decl templates would store this in the
4047 : : same place
4048 : : (c) this function wouldn't need the by-ref arg, which is annoying. */
4049 : :
4050 : : static tree
4051 : 175787139 : node_template_info (tree decl, int &use)
4052 : : {
4053 : 175787139 : tree ti = NULL_TREE;
4054 : 175787139 : int use_tpl = -1;
4055 : 175787139 : if (DECL_IMPLICIT_TYPEDEF_P (decl))
4056 : : {
4057 : 20322426 : tree type = TREE_TYPE (decl);
4058 : :
4059 : 20322426 : ti = TYPE_TEMPLATE_INFO (type);
4060 : 20322426 : if (ti)
4061 : : {
4062 : 5057968 : if (TYPE_LANG_SPECIFIC (type))
4063 : 5050602 : use_tpl = CLASSTYPE_USE_TEMPLATE (type);
4064 : : else
4065 : : {
4066 : : /* An enum, where we don't explicitly encode use_tpl.
4067 : : If the containing context (a type or a function), is
4068 : : an ({im,ex}plicit) instantiation, then this is too.
4069 : : If it's a partial or explicit specialization, then
4070 : : this is not!. */
4071 : 7366 : tree ctx = CP_DECL_CONTEXT (decl);
4072 : 7366 : if (TYPE_P (ctx))
4073 : 7231 : ctx = TYPE_NAME (ctx);
4074 : 7366 : node_template_info (ctx, use);
4075 : 7366 : use_tpl = use != 2 ? use : 0;
4076 : : }
4077 : : }
4078 : : }
4079 : 155464713 : else if (DECL_LANG_SPECIFIC (decl)
4080 : 155464713 : && (VAR_P (decl)
4081 : : || TREE_CODE (decl) == TYPE_DECL
4082 : : || TREE_CODE (decl) == FUNCTION_DECL
4083 : : || TREE_CODE (decl) == FIELD_DECL
4084 : : || TREE_CODE (decl) == CONCEPT_DECL
4085 : : || TREE_CODE (decl) == TEMPLATE_DECL))
4086 : : {
4087 : 84465868 : use_tpl = DECL_USE_TEMPLATE (decl);
4088 : 84465868 : ti = DECL_TEMPLATE_INFO (decl);
4089 : : }
4090 : :
4091 : 175787139 : use = use_tpl;
4092 : 175787139 : return ti;
4093 : : }
4094 : :
4095 : : /* Find the index in entity_ary for an imported DECL. It should
4096 : : always be there, but bugs can cause it to be missing, and that can
4097 : : crash the crash reporting -- let's not do that! When streaming
4098 : : out we place entities from this module there too -- with negated
4099 : : indices. */
4100 : :
4101 : : static unsigned
4102 : 1618464 : import_entity_index (tree decl, bool null_ok = false)
4103 : : {
4104 : 1618464 : if (unsigned *slot = entity_map->get (DECL_UID (decl)))
4105 : 1618449 : return *slot;
4106 : :
4107 : 15 : gcc_checking_assert (null_ok);
4108 : : return ~(~0u >> 1);
4109 : : }
4110 : :
4111 : : /* Find the module for an imported entity at INDEX in the entity ary.
4112 : : There must be one. */
4113 : :
4114 : : static module_state *
4115 : 34964 : import_entity_module (unsigned index)
4116 : : {
4117 : 34964 : if (index > ~(~0u >> 1))
4118 : : /* This is an index for an exported entity. */
4119 : 18 : return (*modules)[0];
4120 : :
4121 : : /* Do not include the current TU (not an off-by-one error). */
4122 : 34946 : unsigned pos = 1;
4123 : 34946 : unsigned len = modules->length () - pos;
4124 : 76353 : while (len)
4125 : : {
4126 : 41407 : unsigned half = len / 2;
4127 : 41407 : module_state *probe = (*modules)[pos + half];
4128 : 41407 : if (index < probe->entity_lwm)
4129 : : len = half;
4130 : 35043 : else if (index < probe->entity_lwm + probe->entity_num)
4131 : 34946 : return probe;
4132 : : else
4133 : : {
4134 : 97 : pos += half + 1;
4135 : 97 : len = len - (half + 1);
4136 : : }
4137 : : }
4138 : 0 : gcc_unreachable ();
4139 : : }
4140 : :
4141 : :
4142 : : /********************************************************************/
4143 : : /* A dumping machinery. */
4144 : :
4145 : : class dumper {
4146 : : public:
4147 : : enum {
4148 : : LOCATION = TDF_LINENO, /* -lineno:Source location streaming. */
4149 : : DEPEND = TDF_GRAPH, /* -graph:Dependency graph construction. */
4150 : : CLUSTER = TDF_BLOCKS, /* -blocks:Clusters. */
4151 : : TREE = TDF_UID, /* -uid:Tree streaming. */
4152 : : MERGE = TDF_ALIAS, /* -alias:Mergeable Entities. */
4153 : : ELF = TDF_ASMNAME, /* -asmname:Elf data. */
4154 : : MACRO = TDF_VOPS /* -vops:Macros. */
4155 : : };
4156 : :
4157 : : private:
4158 : : struct impl {
4159 : : typedef vec<module_state *, va_heap, vl_embed> stack_t;
4160 : :
4161 : : FILE *stream; /* Dump stream. */
4162 : : unsigned indent; /* Local indentation. */
4163 : : bool bol; /* Beginning of line. */
4164 : : stack_t stack; /* Trailing array of module_state. */
4165 : :
4166 : : bool nested_name (tree); /* Dump a name following DECL_CONTEXT. */
4167 : : };
4168 : :
4169 : : public:
4170 : : /* The dumper. */
4171 : : impl *dumps;
4172 : : dump_flags_t flags;
4173 : :
4174 : : public:
4175 : : /* Push/pop module state dumping. */
4176 : : unsigned push (module_state *);
4177 : : void pop (unsigned);
4178 : :
4179 : : public:
4180 : : /* Change local indentation. */
4181 : 461588079 : void indent ()
4182 : : {
4183 : 282 : if (dumps)
4184 : 7615372 : dumps->indent++;
4185 : : }
4186 : 461588079 : void outdent ()
4187 : : {
4188 : 461588079 : if (dumps)
4189 : : {
4190 : 7615372 : gcc_checking_assert (dumps->indent);
4191 : 7615372 : dumps->indent--;
4192 : : }
4193 : 461588079 : }
4194 : :
4195 : : public:
4196 : : /* Is dump enabled?. */
4197 : 303651394 : bool operator () (int mask = 0)
4198 : : {
4199 : 8453917 : if (!dumps || !dumps->stream)
4200 : : return false;
4201 : 3506364 : if (mask && !(mask & flags))
4202 : 30700 : return false;
4203 : : return true;
4204 : : }
4205 : : /* Dump some information. */
4206 : : bool operator () (const char *, ...);
4207 : : };
4208 : :
4209 : : /* The dumper. */
4210 : : static dumper dump = {0, dump_flags_t (0)};
4211 : :
4212 : : /* Push to dumping M. Return previous indentation level. */
4213 : :
4214 : : unsigned
4215 : 91716 : dumper::push (module_state *m)
4216 : : {
4217 : 91716 : FILE *stream = NULL;
4218 : 91716 : if (!dumps || !dumps->stack.length ())
4219 : : {
4220 : 90671 : stream = dump_begin (module_dump_id, &flags);
4221 : 90671 : if (!stream)
4222 : : return 0;
4223 : : }
4224 : :
4225 : 5259 : if (!dumps || !dumps->stack.space (1))
4226 : : {
4227 : : /* Create or extend the dump implementor. */
4228 : 1015 : unsigned current = dumps ? dumps->stack.length () : 0;
4229 : 533 : unsigned count = current ? current * 2 : EXPERIMENT (1, 20);
4230 : 1015 : size_t alloc = (offsetof (impl, stack)
4231 : 1015 : + impl::stack_t::embedded_size (count));
4232 : 1015 : dumps = XRESIZEVAR (impl, dumps, alloc);
4233 : 1015 : dumps->stack.embedded_init (count, current);
4234 : : }
4235 : 5259 : if (stream)
4236 : 4214 : dumps->stream = stream;
4237 : :
4238 : 5259 : unsigned n = dumps->indent;
4239 : 5259 : dumps->indent = 0;
4240 : 5259 : dumps->bol = true;
4241 : 5259 : dumps->stack.quick_push (m);
4242 : 5259 : if (m)
4243 : : {
4244 : 1660 : module_state *from = NULL;
4245 : :
4246 : 1660 : if (dumps->stack.length () > 1)
4247 : 527 : from = dumps->stack[dumps->stack.length () - 2];
4248 : : else
4249 : 1133 : dump ("");
4250 : 3020 : dump (from ? "Starting module %M (from %M)"
4251 : : : "Starting module %M", m, from);
4252 : : }
4253 : :
4254 : : return n;
4255 : : }
4256 : :
4257 : : /* Pop from dumping. Restore indentation to N. */
4258 : :
4259 : 91688 : void dumper::pop (unsigned n)
4260 : : {
4261 : 91688 : if (!dumps)
4262 : : return;
4263 : :
4264 : 10518 : gcc_checking_assert (dump () && !dumps->indent);
4265 : 5259 : if (module_state *m = dumps->stack[dumps->stack.length () - 1])
4266 : : {
4267 : 1660 : module_state *from = (dumps->stack.length () > 1
4268 : 1660 : ? dumps->stack[dumps->stack.length () - 2] : NULL);
4269 : 1887 : dump (from ? "Finishing module %M (returning to %M)"
4270 : : : "Finishing module %M", m, from);
4271 : : }
4272 : 5259 : dumps->stack.pop ();
4273 : 5259 : dumps->indent = n;
4274 : 5259 : if (!dumps->stack.length ())
4275 : : {
4276 : 4214 : dump_end (module_dump_id, dumps->stream);
4277 : 4214 : dumps->stream = NULL;
4278 : : }
4279 : : }
4280 : :
4281 : : /* Dump a nested name for arbitrary tree T. Sometimes it won't have a
4282 : : name. */
4283 : :
4284 : : bool
4285 : 1992714 : dumper::impl::nested_name (tree t)
4286 : : {
4287 : 1992714 : tree ti = NULL_TREE;
4288 : 1992714 : int origin = -1;
4289 : 1992714 : tree name = NULL_TREE;
4290 : :
4291 : 1992714 : if (t && TREE_CODE (t) == TREE_BINFO)
4292 : 378 : t = BINFO_TYPE (t);
4293 : :
4294 : 1992693 : if (t && TYPE_P (t))
4295 : 942750 : t = TYPE_NAME (t);
4296 : :
4297 : 1992702 : if (t && DECL_P (t))
4298 : : {
4299 : 1830472 : if (t == global_namespace || DECL_TEMPLATE_PARM_P (t))
4300 : : ;
4301 : 1662741 : else if (tree ctx = DECL_CONTEXT (t))
4302 : 1325232 : if (TREE_CODE (ctx) == TRANSLATION_UNIT_DECL
4303 : 1325232 : || nested_name (ctx))
4304 : 1325232 : fputs ("::", stream);
4305 : :
4306 : 1830472 : int use_tpl;
4307 : 1830472 : ti = node_template_info (t, use_tpl);
4308 : 497009 : if (ti && TREE_CODE (TI_TEMPLATE (ti)) == TEMPLATE_DECL
4309 : 2327427 : && (DECL_TEMPLATE_RESULT (TI_TEMPLATE (ti)) == t))
4310 : : t = TI_TEMPLATE (ti);
4311 : 1830472 : tree not_tmpl = t;
4312 : 1830472 : if (TREE_CODE (t) == TEMPLATE_DECL)
4313 : : {
4314 : 184642 : fputs ("template ", stream);
4315 : 184642 : not_tmpl = DECL_TEMPLATE_RESULT (t);
4316 : : }
4317 : :
4318 : 184642 : if (not_tmpl
4319 : 1830432 : && DECL_P (not_tmpl)
4320 : 1830432 : && DECL_LANG_SPECIFIC (not_tmpl)
4321 : 1087581 : && DECL_MODULE_IMPORT_P (not_tmpl))
4322 : : {
4323 : : /* We need to be careful here, so as to not explode on
4324 : : inconsistent data -- we're probably debugging, because
4325 : : Something Is Wrong. */
4326 : 16266 : unsigned index = import_entity_index (t, true);
4327 : 16266 : if (!(index & ~(~0u >> 1)))
4328 : 15753 : origin = import_entity_module (index)->mod;
4329 : 513 : else if (index > ~(~0u >> 1))
4330 : : /* An imported partition member that we're emitting. */
4331 : : origin = 0;
4332 : : else
4333 : 15 : origin = -2;
4334 : : }
4335 : :
4336 : 1830472 : name = DECL_NAME (t) ? DECL_NAME (t)
4337 : 7698 : : HAS_DECL_ASSEMBLER_NAME_P (t) ? DECL_ASSEMBLER_NAME_RAW (t)
4338 : : : NULL_TREE;
4339 : 1830472 : }
4340 : : else
4341 : : name = t;
4342 : :
4343 : 1992714 : if (name)
4344 : 1917862 : switch (TREE_CODE (name))
4345 : : {
4346 : 29948 : default:
4347 : 29948 : fputs ("#unnamed#", stream);
4348 : 29948 : break;
4349 : :
4350 : 1847326 : case IDENTIFIER_NODE:
4351 : 1847326 : fwrite (IDENTIFIER_POINTER (name), 1, IDENTIFIER_LENGTH (name), stream);
4352 : 1847326 : break;
4353 : :
4354 : 40528 : case INTEGER_CST:
4355 : 40528 : print_hex (wi::to_wide (name), stream);
4356 : 40528 : break;
4357 : :
4358 : 60 : case STRING_CST:
4359 : : /* If TREE_TYPE is NULL, this is a raw string. */
4360 : 120 : fwrite (TREE_STRING_POINTER (name), 1,
4361 : 60 : TREE_STRING_LENGTH (name) - (TREE_TYPE (name) != NULL_TREE),
4362 : : stream);
4363 : 60 : break;
4364 : : }
4365 : : else
4366 : 74852 : fputs ("#null#", stream);
4367 : :
4368 : 1992714 : if (origin >= 0)
4369 : : {
4370 : 16251 : const module_state *module = (*modules)[origin];
4371 : 32502 : fprintf (stream, "@%s:%d", !module ? "" : !module->name ? "(unnamed)"
4372 : 16251 : : module->get_flatname (), origin);
4373 : : }
4374 : 1976463 : else if (origin == -2)
4375 : 15 : fprintf (stream, "@???");
4376 : :
4377 : 1992714 : if (ti)
4378 : : {
4379 : 497009 : tree args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (ti));
4380 : 497009 : fputs ("<", stream);
4381 : 497009 : if (args)
4382 : 1290562 : for (int ix = 0; ix != TREE_VEC_LENGTH (args); ix++)
4383 : : {
4384 : 793553 : if (ix)
4385 : 296556 : fputs (",", stream);
4386 : 793553 : nested_name (TREE_VEC_ELT (args, ix));
4387 : : }
4388 : 497009 : fputs (">", stream);
4389 : : }
4390 : :
4391 : 1992714 : return true;
4392 : : }
4393 : :
4394 : : /* Formatted dumping. FORMAT begins with '+' do not emit a trailing
4395 : : new line. (Normally it is appended.)
4396 : : Escapes:
4397 : : %C - tree_code
4398 : : %I - identifier
4399 : : %M - module_state
4400 : : %N - name -- DECL_NAME
4401 : : %P - context:name pair
4402 : : %R - unsigned:unsigned ratio
4403 : : %S - symbol -- DECL_ASSEMBLER_NAME
4404 : : %U - long unsigned
4405 : : %V - version
4406 : : --- the following are printf-like, but without its flexibility
4407 : : %d - decimal int
4408 : : %p - pointer
4409 : : %s - string
4410 : : %u - unsigned int
4411 : : %x - hex int
4412 : :
4413 : : We do not implement the printf modifiers. */
4414 : :
4415 : : bool
4416 : 648866 : dumper::operator () (const char *format, ...)
4417 : : {
4418 : 648866 : if (!(*this) ())
4419 : : return false;
4420 : :
4421 : 608651 : bool no_nl = format[0] == '+';
4422 : 608651 : format += no_nl;
4423 : :
4424 : 608651 : if (dumps->bol)
4425 : : {
4426 : : /* Module import indent. */
4427 : 456339 : if (unsigned depth = dumps->stack.length () - 1)
4428 : : {
4429 : 18722 : const char *prefix = ">>>>";
4430 : 37444 : fprintf (dumps->stream, (depth <= strlen (prefix)
4431 : 18722 : ? &prefix[strlen (prefix) - depth]
4432 : : : ">.%d.>"), depth);
4433 : : }
4434 : :
4435 : : /* Local indent. */
4436 : 456339 : if (unsigned indent = dumps->indent)
4437 : : {
4438 : 341862 : const char *prefix = " ";
4439 : 643175 : fprintf (dumps->stream, (indent <= strlen (prefix)
4440 : 301313 : ? &prefix[strlen (prefix) - indent]
4441 : : : " .%d. "), indent);
4442 : : }
4443 : 456339 : dumps->bol = false;
4444 : : }
4445 : :
4446 : 608651 : va_list args;
4447 : 608651 : va_start (args, format);
4448 : 2283508 : while (const char *esc = strchr (format, '%'))
4449 : : {
4450 : 1674857 : fwrite (format, 1, (size_t)(esc - format), dumps->stream);
4451 : 1674857 : format = ++esc;
4452 : 1674857 : switch (*format++)
4453 : : {
4454 : 0 : default:
4455 : 0 : gcc_unreachable ();
4456 : :
4457 : 482 : case '%':
4458 : 482 : fputc ('%', dumps->stream);
4459 : 482 : break;
4460 : :
4461 : 355670 : case 'C': /* Code */
4462 : 355670 : {
4463 : 355670 : tree_code code = (tree_code)va_arg (args, unsigned);
4464 : 355670 : fputs (get_tree_code_name (code), dumps->stream);
4465 : : }
4466 : 355670 : break;
4467 : :
4468 : 81 : case 'I': /* Identifier. */
4469 : 81 : {
4470 : 81 : tree t = va_arg (args, tree);
4471 : 81 : dumps->nested_name (t);
4472 : : }
4473 : 81 : break;
4474 : :
4475 : 6243 : case 'M': /* Module. */
4476 : 6243 : {
4477 : 6243 : const char *str = "(none)";
4478 : 6243 : if (module_state *m = va_arg (args, module_state *))
4479 : : {
4480 : 6243 : if (!m->has_location ())
4481 : : str = "(detached)";
4482 : : else
4483 : 6243 : str = m->get_flatname ();
4484 : : }
4485 : 6243 : fputs (str, dumps->stream);
4486 : : }
4487 : 6243 : break;
4488 : :
4489 : 396145 : case 'N': /* Name. */
4490 : 396145 : {
4491 : 396145 : tree t = va_arg (args, tree);
4492 : 792743 : while (t && TREE_CODE (t) == OVERLOAD)
4493 : 453 : t = OVL_FUNCTION (t);
4494 : 396145 : fputc ('\'', dumps->stream);
4495 : 396145 : dumps->nested_name (t);
4496 : 396145 : fputc ('\'', dumps->stream);
4497 : : }
4498 : 396145 : break;
4499 : :
4500 : 17709 : case 'P': /* Pair. */
4501 : 17709 : {
4502 : 17709 : tree ctx = va_arg (args, tree);
4503 : 17709 : tree name = va_arg (args, tree);
4504 : 17709 : fputc ('\'', dumps->stream);
4505 : 17709 : dumps->nested_name (ctx);
4506 : 17709 : if (ctx && ctx != global_namespace)
4507 : 12690 : fputs ("::", dumps->stream);
4508 : 17709 : dumps->nested_name (name);
4509 : 17709 : fputc ('\'', dumps->stream);
4510 : : }
4511 : 17709 : break;
4512 : :
4513 : 684 : case 'R': /* Ratio */
4514 : 684 : {
4515 : 684 : unsigned a = va_arg (args, unsigned);
4516 : 684 : unsigned b = va_arg (args, unsigned);
4517 : 684 : fprintf (dumps->stream, "%.1f", (float) a / (b + !b));
4518 : : }
4519 : 684 : break;
4520 : :
4521 : 33087 : case 'S': /* Symbol name */
4522 : 33087 : {
4523 : 33087 : tree t = va_arg (args, tree);
4524 : 33087 : if (t && TYPE_P (t))
4525 : 11970 : t = TYPE_NAME (t);
4526 : 31683 : if (t && HAS_DECL_ASSEMBLER_NAME_P (t)
4527 : 30333 : && DECL_ASSEMBLER_NAME_SET_P (t))
4528 : : {
4529 : 171 : fputc ('(', dumps->stream);
4530 : 171 : fputs (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (t)),
4531 : 171 : dumps->stream);
4532 : 171 : fputc (')', dumps->stream);
4533 : : }
4534 : : }
4535 : : break;
4536 : :
4537 : 0 : case 'U': /* long unsigned. */
4538 : 0 : {
4539 : 0 : unsigned long u = va_arg (args, unsigned long);
4540 : 0 : fprintf (dumps->stream, "%lu", u);
4541 : : }
4542 : 0 : break;
4543 : :
4544 : 674 : case 'V': /* Verson. */
4545 : 674 : {
4546 : 674 : unsigned v = va_arg (args, unsigned);
4547 : 674 : verstr_t string;
4548 : :
4549 : 674 : version2string (v, string);
4550 : 674 : fputs (string, dumps->stream);
4551 : : }
4552 : 674 : break;
4553 : :
4554 : 0 : case 'c': /* Character. */
4555 : 0 : {
4556 : 0 : int c = va_arg (args, int);
4557 : 0 : fputc (c, dumps->stream);
4558 : : }
4559 : 0 : break;
4560 : :
4561 : 56998 : case 'd': /* Decimal Int. */
4562 : 56998 : {
4563 : 56998 : int d = va_arg (args, int);
4564 : 56998 : fprintf (dumps->stream, "%d", d);
4565 : : }
4566 : 56998 : break;
4567 : :
4568 : 0 : case 'p': /* Pointer. */
4569 : 0 : {
4570 : 0 : void *p = va_arg (args, void *);
4571 : 0 : fprintf (dumps->stream, "%p", p);
4572 : : }
4573 : 0 : break;
4574 : :
4575 : 477503 : case 's': /* String. */
4576 : 477503 : {
4577 : 477503 : const char *s = va_arg (args, char *);
4578 : 477503 : gcc_checking_assert (s);
4579 : 477503 : fputs (s, dumps->stream);
4580 : : }
4581 : 477503 : break;
4582 : :
4583 : 327427 : case 'u': /* Unsigned. */
4584 : 327427 : {
4585 : 327427 : unsigned u = va_arg (args, unsigned);
4586 : 327427 : fprintf (dumps->stream, "%u", u);
4587 : : }
4588 : 327427 : break;
4589 : :
4590 : 2154 : case 'x': /* Hex. */
4591 : 2154 : {
4592 : 2154 : unsigned x = va_arg (args, unsigned);
4593 : 2154 : fprintf (dumps->stream, "%x", x);
4594 : : }
4595 : 2154 : break;
4596 : : }
4597 : : }
4598 : 608651 : fputs (format, dumps->stream);
4599 : 608651 : va_end (args);
4600 : 608651 : if (!no_nl)
4601 : : {
4602 : 456339 : dumps->bol = true;
4603 : 456339 : fputc ('\n', dumps->stream);
4604 : : }
4605 : : return true;
4606 : : }
4607 : :
4608 : : struct note_def_cache_hasher : ggc_cache_ptr_hash<tree_node>
4609 : : {
4610 : 286426 : static int keep_cache_entry (tree t)
4611 : : {
4612 : 286426 : if (!CHECKING_P)
4613 : : /* GTY is unfortunately not clever enough to conditionalize
4614 : : this. */
4615 : : gcc_unreachable ();
4616 : :
4617 : 286426 : if (ggc_marked_p (t))
4618 : : return -1;
4619 : :
4620 : 0 : unsigned n = dump.push (NULL);
4621 : : /* This might or might not be an error. We should note its
4622 : : dropping whichever. */
4623 : 0 : dump () && dump ("Dropping %N from note_defs table", t);
4624 : 0 : dump.pop (n);
4625 : :
4626 : 0 : return 0;
4627 : : }
4628 : : };
4629 : :
4630 : : /* We should stream each definition at most once.
4631 : : This needs to be a cache because there are cases where a definition
4632 : : ends up being not retained, and we need to drop those so we don't
4633 : : get confused if memory is reallocated. */
4634 : : typedef hash_table<note_def_cache_hasher> note_defs_table_t;
4635 : : static GTY((cache)) note_defs_table_t *note_defs;
4636 : :
4637 : : void
4638 : 490509 : trees_in::assert_definition (tree decl ATTRIBUTE_UNUSED,
4639 : : bool installing ATTRIBUTE_UNUSED)
4640 : : {
4641 : : #if CHECKING_P
4642 : 490509 : tree *slot = note_defs->find_slot (decl, installing ? INSERT : NO_INSERT);
4643 : 490509 : tree not_tmpl = STRIP_TEMPLATE (decl);
4644 : 490509 : if (installing)
4645 : : {
4646 : : /* We must be inserting for the first time. */
4647 : 216256 : gcc_assert (!*slot);
4648 : 216256 : *slot = decl;
4649 : : }
4650 : : else
4651 : : /* If this is not the mergeable entity, it should not be in the
4652 : : table. If it is a non-global-module mergeable entity, it
4653 : : should be in the table. Global module entities could have been
4654 : : defined textually in the current TU and so might or might not
4655 : : be present. */
4656 : 274253 : gcc_assert (!is_duplicate (decl)
4657 : : ? !slot
4658 : : : (slot
4659 : : || !DECL_LANG_SPECIFIC (not_tmpl)
4660 : : || !DECL_MODULE_PURVIEW_P (not_tmpl)
4661 : : || (!DECL_MODULE_IMPORT_P (not_tmpl)
4662 : : && header_module_p ())));
4663 : :
4664 : 490509 : if (not_tmpl != decl)
4665 : 291562 : gcc_assert (!note_defs->find_slot (not_tmpl, NO_INSERT));
4666 : : #endif
4667 : 490509 : }
4668 : :
4669 : : void
4670 : 461701 : trees_out::assert_definition (tree decl ATTRIBUTE_UNUSED)
4671 : : {
4672 : : #if CHECKING_P
4673 : 461701 : tree *slot = note_defs->find_slot (decl, INSERT);
4674 : 461701 : gcc_assert (!*slot);
4675 : 461701 : *slot = decl;
4676 : 461701 : if (TREE_CODE (decl) == TEMPLATE_DECL)
4677 : 273345 : gcc_assert (!note_defs->find_slot (DECL_TEMPLATE_RESULT (decl), NO_INSERT));
4678 : : #endif
4679 : 461701 : }
4680 : :
4681 : : /********************************************************************/
4682 : : static bool
4683 : 8526 : noisy_p ()
4684 : : {
4685 : 0 : if (quiet_flag)
4686 : : return false;
4687 : :
4688 : 0 : pp_needs_newline (global_dc->printer) = true;
4689 : 0 : diagnostic_set_last_function (global_dc, (diagnostic_info *) NULL);
4690 : :
4691 : 0 : return true;
4692 : : }
4693 : :
4694 : : /* Set the cmi repo. Strip trailing '/', '.' becomes NULL. */
4695 : :
4696 : : static void
4697 : 102384 : set_cmi_repo (const char *r)
4698 : : {
4699 : 102384 : XDELETEVEC (cmi_repo);
4700 : 102384 : XDELETEVEC (cmi_path);
4701 : 102384 : cmi_path_alloc = 0;
4702 : :
4703 : 102384 : cmi_repo = NULL;
4704 : 102384 : cmi_repo_length = 0;
4705 : :
4706 : 102384 : if (!r || !r[0])
4707 : : return;
4708 : :
4709 : 3235 : size_t len = strlen (r);
4710 : 3235 : cmi_repo = XNEWVEC (char, len + 1);
4711 : 3235 : memcpy (cmi_repo, r, len + 1);
4712 : :
4713 : 3235 : if (len > 1 && IS_DIR_SEPARATOR (cmi_repo[len-1]))
4714 : 3235 : len--;
4715 : 3235 : if (len == 1 && cmi_repo[0] == '.')
4716 : 21 : len--;
4717 : 3235 : cmi_repo[len] = 0;
4718 : 3235 : cmi_repo_length = len;
4719 : : }
4720 : :
4721 : : /* TO is a repo-relative name. Provide one that we may use from where
4722 : : we are. */
4723 : :
4724 : : static const char *
4725 : 4014 : maybe_add_cmi_prefix (const char *to, size_t *len_p = NULL)
4726 : : {
4727 : 4014 : size_t len = len_p || cmi_repo_length ? strlen (to) : 0;
4728 : :
4729 : 4014 : if (cmi_repo_length && !IS_ABSOLUTE_PATH (to))
4730 : : {
4731 : 3993 : if (cmi_path_alloc < cmi_repo_length + len + 2)
4732 : : {
4733 : 3211 : XDELETEVEC (cmi_path);
4734 : 3211 : cmi_path_alloc = cmi_repo_length + len * 2 + 2;
4735 : 3211 : cmi_path = XNEWVEC (char, cmi_path_alloc);
4736 : :
4737 : 3211 : memcpy (cmi_path, cmi_repo, cmi_repo_length);
4738 : 3211 : cmi_path[cmi_repo_length] = DIR_SEPARATOR;
4739 : : }
4740 : :
4741 : 3993 : memcpy (&cmi_path[cmi_repo_length + 1], to, len + 1);
4742 : 3993 : len += cmi_repo_length + 1;
4743 : 3993 : to = cmi_path;
4744 : : }
4745 : :
4746 : 4014 : if (len_p)
4747 : 1891 : *len_p = len;
4748 : :
4749 : 4014 : return to;
4750 : : }
4751 : :
4752 : : /* Try and create the directories of PATH. */
4753 : :
4754 : : static void
4755 : 55 : create_dirs (char *path)
4756 : : {
4757 : : /* Try and create the missing directories. */
4758 : 3622 : for (char *base = path; *base; base++)
4759 : 3567 : if (IS_DIR_SEPARATOR (*base))
4760 : : {
4761 : 363 : char sep = *base;
4762 : 363 : *base = 0;
4763 : 363 : int failed = mkdir (path, S_IRWXU | S_IRWXG | S_IRWXO);
4764 : 377 : dump () && dump ("Mkdir ('%s') errno:=%u", path, failed ? errno : 0);
4765 : 363 : *base = sep;
4766 : 363 : if (failed
4767 : : /* Maybe racing with another creator (of a *different*
4768 : : module). */
4769 : 61 : && errno != EEXIST)
4770 : : break;
4771 : : }
4772 : 55 : }
4773 : :
4774 : : /* Given a CLASSTYPE_DECL_LIST VALUE get the template friend decl,
4775 : : if that's what this is. */
4776 : :
4777 : : static tree
4778 : 69011 : friend_from_decl_list (tree frnd)
4779 : : {
4780 : 69011 : tree res = frnd;
4781 : :
4782 : 69011 : if (TREE_CODE (frnd) != TEMPLATE_DECL)
4783 : : {
4784 : 33169 : tree tmpl = NULL_TREE;
4785 : 33169 : if (TYPE_P (frnd))
4786 : : {
4787 : 6204 : res = TYPE_NAME (frnd);
4788 : 6168 : if (CLASS_TYPE_P (frnd)
4789 : 12372 : && CLASSTYPE_TEMPLATE_INFO (frnd))
4790 : 6168 : tmpl = CLASSTYPE_TI_TEMPLATE (frnd);
4791 : : }
4792 : 26965 : else if (DECL_TEMPLATE_INFO (frnd))
4793 : : {
4794 : 26965 : tmpl = DECL_TI_TEMPLATE (frnd);
4795 : 26965 : if (TREE_CODE (tmpl) != TEMPLATE_DECL)
4796 : : tmpl = NULL_TREE;
4797 : : }
4798 : :
4799 : 38563 : if (tmpl && DECL_TEMPLATE_RESULT (tmpl) == res)
4800 : : res = tmpl;
4801 : : }
4802 : :
4803 : 69011 : return res;
4804 : : }
4805 : :
4806 : : static tree
4807 : 33023 : find_enum_member (tree ctx, tree name)
4808 : : {
4809 : 33023 : for (tree values = TYPE_VALUES (ctx);
4810 : 486044 : values; values = TREE_CHAIN (values))
4811 : 477815 : if (DECL_NAME (TREE_VALUE (values)) == name)
4812 : : return TREE_VALUE (values);
4813 : :
4814 : : return NULL_TREE;
4815 : : }
4816 : :
4817 : : /********************************************************************/
4818 : : /* Instrumentation gathered writing bytes. */
4819 : :
4820 : : void
4821 : 228 : bytes_out::instrument ()
4822 : : {
4823 : 228 : dump ("Wrote %u bytes in %u blocks", lengths[3], spans[3]);
4824 : 228 : dump ("Wrote %u bits in %u bytes", lengths[0] + lengths[1], lengths[2]);
4825 : 684 : for (unsigned ix = 0; ix < 2; ix++)
4826 : 684 : dump (" %u %s spans of %R bits", spans[ix],
4827 : : ix ? "one" : "zero", lengths[ix], spans[ix]);
4828 : 228 : dump (" %u blocks with %R bits padding", spans[2],
4829 : 228 : lengths[2] * 8 - (lengths[0] + lengths[1]), spans[2]);
4830 : 228 : }
4831 : :
4832 : : /* Instrumentation gathered writing trees. */
4833 : : void
4834 : 1818 : trees_out::instrument ()
4835 : : {
4836 : 1818 : if (dump (""))
4837 : : {
4838 : 228 : bytes_out::instrument ();
4839 : 228 : dump ("Wrote:");
4840 : 228 : dump (" %u decl trees", decl_val_count);
4841 : 228 : dump (" %u other trees", tree_val_count);
4842 : 228 : dump (" %u back references", back_ref_count);
4843 : 228 : dump (" %u null trees", null_count);
4844 : : }
4845 : 1818 : }
4846 : :
4847 : : /* Setup and teardown for a tree walk. */
4848 : :
4849 : : void
4850 : 2907440 : trees_out::begin ()
4851 : : {
4852 : 2907440 : gcc_assert (!streaming_p () || !tree_map.elements ());
4853 : :
4854 : 2907440 : mark_trees ();
4855 : 2907440 : if (streaming_p ())
4856 : 360145 : parent::begin ();
4857 : 2907440 : }
4858 : :
4859 : : unsigned
4860 : 360145 : trees_out::end (elf_out *sink, unsigned name, unsigned *crc_ptr)
4861 : : {
4862 : 360145 : gcc_checking_assert (streaming_p ());
4863 : :
4864 : 360145 : unmark_trees ();
4865 : 360145 : return parent::end (sink, name, crc_ptr);
4866 : : }
4867 : :
4868 : : void
4869 : 2547295 : trees_out::end ()
4870 : : {
4871 : 2547295 : gcc_assert (!streaming_p ());
4872 : :
4873 : 2547295 : unmark_trees ();
4874 : : /* Do not parent::end -- we weren't streaming. */
4875 : 2547295 : }
4876 : :
4877 : : void
4878 : 2907440 : trees_out::mark_trees ()
4879 : : {
4880 : 2907440 : if (size_t size = tree_map.elements ())
4881 : : {
4882 : : /* This isn't our first rodeo, destroy and recreate the
4883 : : tree_map. I'm a bad bad man. Use the previous size as a
4884 : : guess for the next one (so not all bad). */
4885 : 2200804 : tree_map.~ptr_int_hash_map ();
4886 : 2200804 : new (&tree_map) ptr_int_hash_map (size);
4887 : : }
4888 : :
4889 : : /* Install the fixed trees, with +ve references. */
4890 : 2907440 : unsigned limit = fixed_trees->length ();
4891 : 560387243 : for (unsigned ix = 0; ix != limit; ix++)
4892 : : {
4893 : 557479803 : tree val = (*fixed_trees)[ix];
4894 : 557479803 : bool existed = tree_map.put (val, ix + tag_fixed);
4895 : 557479803 : gcc_checking_assert (!TREE_VISITED (val) && !existed);
4896 : 557479803 : TREE_VISITED (val) = true;
4897 : : }
4898 : :
4899 : 2907440 : ref_num = 0;
4900 : 2907440 : }
4901 : :
4902 : : /* Unmark the trees we encountered */
4903 : :
4904 : : void
4905 : 2907440 : trees_out::unmark_trees ()
4906 : : {
4907 : 2907440 : ptr_int_hash_map::iterator end (tree_map.end ());
4908 : 657195330 : for (ptr_int_hash_map::iterator iter (tree_map.begin ()); iter != end; ++iter)
4909 : : {
4910 : 654287890 : tree node = reinterpret_cast<tree> ((*iter).first);
4911 : 654287890 : int ref = (*iter).second;
4912 : : /* We should have visited the node, and converted its mergeable
4913 : : reference to a regular reference. */
4914 : 654287890 : gcc_checking_assert (TREE_VISITED (node)
4915 : : && (ref <= tag_backref || ref >= tag_fixed));
4916 : 654287890 : TREE_VISITED (node) = false;
4917 : : }
4918 : 2907440 : }
4919 : :
4920 : : /* Mark DECL for by-value walking. We do this by inserting it into
4921 : : the tree map with a reference of zero. May be called multiple
4922 : : times on the same node. */
4923 : :
4924 : : void
4925 : 4477237 : trees_out::mark_by_value (tree decl)
4926 : : {
4927 : 4477237 : gcc_checking_assert (DECL_P (decl)
4928 : : /* Enum consts are INTEGER_CSTS. */
4929 : : || TREE_CODE (decl) == INTEGER_CST
4930 : : || TREE_CODE (decl) == TREE_BINFO);
4931 : :
4932 : 4477237 : if (TREE_VISITED (decl))
4933 : : /* Must already be forced or fixed. */
4934 : 3788 : gcc_checking_assert (*tree_map.get (decl) >= tag_value);
4935 : : else
4936 : : {
4937 : 4473449 : bool existed = tree_map.put (decl, tag_value);
4938 : 4473449 : gcc_checking_assert (!existed);
4939 : 4473449 : TREE_VISITED (decl) = true;
4940 : : }
4941 : 4477237 : }
4942 : :
4943 : : int
4944 : 117215004 : trees_out::get_tag (tree t)
4945 : : {
4946 : 117215004 : gcc_checking_assert (TREE_VISITED (t));
4947 : 117215004 : return *tree_map.get (t);
4948 : : }
4949 : :
4950 : : /* Insert T into the map, return its tag number. */
4951 : :
4952 : : int
4953 : 96808087 : trees_out::insert (tree t, walk_kind walk)
4954 : : {
4955 : 96808087 : gcc_checking_assert (walk != WK_normal || !TREE_VISITED (t));
4956 : 96808087 : int tag = --ref_num;
4957 : 96808087 : bool existed;
4958 : 96808087 : int &slot = tree_map.get_or_insert (t, &existed);
4959 : 96808087 : gcc_checking_assert (TREE_VISITED (t) == existed
4960 : : && (!existed
4961 : : || (walk == WK_value && slot == tag_value)));
4962 : 96808087 : TREE_VISITED (t) = true;
4963 : 96808087 : slot = tag;
4964 : :
4965 : 96808087 : return tag;
4966 : : }
4967 : :
4968 : : /* Insert T into the backreference array. Return its back reference
4969 : : number. */
4970 : :
4971 : : int
4972 : 29415529 : trees_in::insert (tree t)
4973 : : {
4974 : 29415529 : gcc_checking_assert (t || get_overrun ());
4975 : 29415529 : back_refs.safe_push (t);
4976 : 29415529 : return -(int)back_refs.length ();
4977 : : }
4978 : :
4979 : : /* A chained set of decls. */
4980 : :
4981 : : void
4982 : 107372 : trees_out::chained_decls (tree decls)
4983 : : {
4984 : 266604 : for (; decls; decls = DECL_CHAIN (decls))
4985 : 159232 : tree_node (decls);
4986 : 107372 : tree_node (NULL_TREE);
4987 : 107372 : }
4988 : :
4989 : : tree
4990 : 57467 : trees_in::chained_decls ()
4991 : : {
4992 : 57467 : tree decls = NULL_TREE;
4993 : 57467 : for (tree *chain = &decls;;)
4994 : 140470 : if (tree decl = tree_node ())
4995 : : {
4996 : 83003 : if (!DECL_P (decl) || DECL_CHAIN (decl))
4997 : : {
4998 : 0 : set_overrun ();
4999 : 0 : break;
5000 : : }
5001 : 83003 : *chain = decl;
5002 : 83003 : chain = &DECL_CHAIN (decl);
5003 : : }
5004 : : else
5005 : 83003 : break;
5006 : :
5007 : 57467 : return decls;
5008 : : }
5009 : :
5010 : : /* A vector of decls following DECL_CHAIN. */
5011 : :
5012 : : void
5013 : 449900 : trees_out::vec_chained_decls (tree decls)
5014 : : {
5015 : 449900 : if (streaming_p ())
5016 : : {
5017 : : unsigned len = 0;
5018 : :
5019 : 1288433 : for (tree decl = decls; decl; decl = DECL_CHAIN (decl))
5020 : 1063489 : len++;
5021 : 224944 : u (len);
5022 : : }
5023 : :
5024 : 2576896 : for (tree decl = decls; decl; decl = DECL_CHAIN (decl))
5025 : : {
5026 : 488844 : if (DECL_IMPLICIT_TYPEDEF_P (decl)
5027 : 2143146 : && TYPE_NAME (TREE_TYPE (decl)) != decl)
5028 : : /* An anonynmous struct with a typedef name. An odd thing to
5029 : : write. */
5030 : 12 : tree_node (NULL_TREE);
5031 : : else
5032 : 2126984 : tree_node (decl);
5033 : : }
5034 : 449900 : }
5035 : :
5036 : : vec<tree, va_heap> *
5037 : 218347 : trees_in::vec_chained_decls ()
5038 : : {
5039 : 218347 : vec<tree, va_heap> *v = NULL;
5040 : :
5041 : 218347 : if (unsigned len = u ())
5042 : : {
5043 : 114706 : vec_alloc (v, len);
5044 : :
5045 : 1254909 : for (unsigned ix = 0; ix < len; ix++)
5046 : : {
5047 : 1140203 : tree decl = tree_node ();
5048 : 1140203 : if (decl && !DECL_P (decl))
5049 : : {
5050 : 0 : set_overrun ();
5051 : 0 : break;
5052 : : }
5053 : 1140203 : v->quick_push (decl);
5054 : : }
5055 : :
5056 : 114706 : if (get_overrun ())
5057 : : {
5058 : 0 : vec_free (v);
5059 : 0 : v = NULL;
5060 : : }
5061 : : }
5062 : :
5063 : 218347 : return v;
5064 : : }
5065 : :
5066 : : /* A vector of trees. */
5067 : :
5068 : : void
5069 : 330160 : trees_out::tree_vec (vec<tree, va_gc> *v)
5070 : : {
5071 : 330160 : unsigned len = vec_safe_length (v);
5072 : 330160 : if (streaming_p ())
5073 : 165077 : u (len);
5074 : 432870 : for (unsigned ix = 0; ix != len; ix++)
5075 : 102710 : tree_node ((*v)[ix]);
5076 : 330160 : }
5077 : :
5078 : : vec<tree, va_gc> *
5079 : 161208 : trees_in::tree_vec ()
5080 : : {
5081 : 161208 : vec<tree, va_gc> *v = NULL;
5082 : 161208 : if (unsigned len = u ())
5083 : : {
5084 : 46134 : vec_alloc (v, len);
5085 : 96948 : for (unsigned ix = 0; ix != len; ix++)
5086 : 50814 : v->quick_push (tree_node ());
5087 : : }
5088 : 161208 : return v;
5089 : : }
5090 : :
5091 : : /* A vector of tree pairs. */
5092 : :
5093 : : void
5094 : 8476 : trees_out::tree_pair_vec (vec<tree_pair_s, va_gc> *v)
5095 : : {
5096 : 8476 : unsigned len = vec_safe_length (v);
5097 : 8476 : if (streaming_p ())
5098 : 4238 : u (len);
5099 : 8476 : if (len)
5100 : 46772 : for (unsigned ix = 0; ix != len; ix++)
5101 : : {
5102 : 38454 : tree_pair_s const &s = (*v)[ix];
5103 : 38454 : tree_node (s.purpose);
5104 : 38454 : tree_node (s.value);
5105 : : }
5106 : 8476 : }
5107 : :
5108 : : vec<tree_pair_s, va_gc> *
5109 : 4551 : trees_in::tree_pair_vec ()
5110 : : {
5111 : 4551 : vec<tree_pair_s, va_gc> *v = NULL;
5112 : 4551 : if (unsigned len = u ())
5113 : : {
5114 : 4474 : vec_alloc (v, len);
5115 : 25339 : for (unsigned ix = 0; ix != len; ix++)
5116 : : {
5117 : 20865 : tree_pair_s s;
5118 : 20865 : s.purpose = tree_node ();
5119 : 20865 : s.value = tree_node ();
5120 : 20865 : v->quick_push (s);
5121 : : }
5122 : : }
5123 : 4551 : return v;
5124 : : }
5125 : :
5126 : : void
5127 : 468912 : trees_out::tree_list (tree list, bool has_purpose)
5128 : : {
5129 : 1959192 : for (; list; list = TREE_CHAIN (list))
5130 : : {
5131 : 1490280 : gcc_checking_assert (TREE_VALUE (list));
5132 : 1490280 : tree_node (TREE_VALUE (list));
5133 : 1490280 : if (has_purpose)
5134 : 1451276 : tree_node (TREE_PURPOSE (list));
5135 : : }
5136 : 468912 : tree_node (NULL_TREE);
5137 : 468912 : }
5138 : :
5139 : : tree
5140 : 228710 : trees_in::tree_list (bool has_purpose)
5141 : : {
5142 : 228710 : tree res = NULL_TREE;
5143 : :
5144 : 1031810 : for (tree *chain = &res; tree value = tree_node ();
5145 : 1606200 : chain = &TREE_CHAIN (*chain))
5146 : : {
5147 : 803100 : tree purpose = has_purpose ? tree_node () : NULL_TREE;
5148 : 803100 : *chain = build_tree_list (purpose, value);
5149 : 803100 : }
5150 : :
5151 : 228710 : return res;
5152 : : }
5153 : : /* Start tree write. Write information to allocate the receiving
5154 : : node. */
5155 : :
5156 : : void
5157 : 19008978 : trees_out::start (tree t, bool code_streamed)
5158 : : {
5159 : 19008978 : if (TYPE_P (t))
5160 : : {
5161 : 799524 : enum tree_code code = TREE_CODE (t);
5162 : 799524 : gcc_checking_assert (TYPE_MAIN_VARIANT (t) == t);
5163 : : /* All these types are TYPE_NON_COMMON. */
5164 : 799524 : gcc_checking_assert (code == RECORD_TYPE
5165 : : || code == UNION_TYPE
5166 : : || code == ENUMERAL_TYPE
5167 : : || code == TEMPLATE_TYPE_PARM
5168 : : || code == TEMPLATE_TEMPLATE_PARM
5169 : : || code == BOUND_TEMPLATE_TEMPLATE_PARM);
5170 : : }
5171 : :
5172 : 19008978 : if (!code_streamed)
5173 : 18282818 : u (TREE_CODE (t));
5174 : :
5175 : 19008978 : switch (TREE_CODE (t))
5176 : : {
5177 : 17005083 : default:
5178 : 17005083 : if (VL_EXP_CLASS_P (t))
5179 : 702920 : u (VL_EXP_OPERAND_LENGTH (t));
5180 : : break;
5181 : :
5182 : 744335 : case INTEGER_CST:
5183 : 744335 : u (TREE_INT_CST_NUNITS (t));
5184 : 744335 : u (TREE_INT_CST_EXT_NUNITS (t));
5185 : 744335 : break;
5186 : :
5187 : 3 : case OMP_CLAUSE:
5188 : 3 : state->extensions |= SE_OPENMP;
5189 : 3 : u (OMP_CLAUSE_CODE (t));
5190 : 3 : break;
5191 : :
5192 : 28406 : case STRING_CST:
5193 : 28406 : str (TREE_STRING_POINTER (t), TREE_STRING_LENGTH (t));
5194 : 28406 : break;
5195 : :
5196 : 18 : case VECTOR_CST:
5197 : 18 : u (VECTOR_CST_LOG2_NPATTERNS (t));
5198 : 18 : u (VECTOR_CST_NELTS_PER_PATTERN (t));
5199 : 18 : break;
5200 : :
5201 : 160839 : case TREE_BINFO:
5202 : 160839 : u (BINFO_N_BASE_BINFOS (t));
5203 : 160839 : break;
5204 : :
5205 : 1070294 : case TREE_VEC:
5206 : 1070294 : u (TREE_VEC_LENGTH (t));
5207 : 1070294 : break;
5208 : :
5209 : 0 : case FIXED_CST:
5210 : 0 : gcc_unreachable (); /* Not supported in C++. */
5211 : 0 : break;
5212 : :
5213 : 0 : case IDENTIFIER_NODE:
5214 : 0 : case SSA_NAME:
5215 : 0 : case TARGET_MEM_REF:
5216 : 0 : case TRANSLATION_UNIT_DECL:
5217 : : /* We shouldn't meet these. */
5218 : 0 : gcc_unreachable ();
5219 : 19008978 : break;
5220 : : }
5221 : 19008978 : }
5222 : :
5223 : : /* Start tree read. Allocate the receiving node. */
5224 : :
5225 : : tree
5226 : 20543587 : trees_in::start (unsigned code)
5227 : : {
5228 : 20543587 : tree t = NULL_TREE;
5229 : :
5230 : 20543587 : if (!code)
5231 : 18365068 : code = u ();
5232 : :
5233 : 20543587 : switch (code)
5234 : : {
5235 : 18474886 : default:
5236 : 18474886 : if (code >= MAX_TREE_CODES)
5237 : : {
5238 : 0 : fail:
5239 : 0 : set_overrun ();
5240 : 0 : return NULL_TREE;
5241 : : }
5242 : 18474886 : else if (TREE_CODE_CLASS (code) == tcc_vl_exp)
5243 : : {
5244 : 773988 : unsigned ops = u ();
5245 : 773988 : t = build_vl_exp (tree_code (code), ops);
5246 : : }
5247 : : else
5248 : 17700898 : t = make_node (tree_code (code));
5249 : : break;
5250 : :
5251 : 765504 : case INTEGER_CST:
5252 : 765504 : {
5253 : 765504 : unsigned n = u ();
5254 : 765504 : unsigned e = u ();
5255 : 765504 : t = make_int_cst (n, e);
5256 : : }
5257 : 765504 : break;
5258 : :
5259 : 3 : case OMP_CLAUSE:
5260 : 3 : {
5261 : 3 : if (!(state->extensions & SE_OPENMP))
5262 : 0 : goto fail;
5263 : :
5264 : 3 : unsigned omp_code = u ();
5265 : 3 : t = build_omp_clause (UNKNOWN_LOCATION, omp_clause_code (omp_code));
5266 : : }
5267 : 3 : break;
5268 : :
5269 : 31564 : case STRING_CST:
5270 : 31564 : {
5271 : 31564 : size_t l;
5272 : 31564 : const char *chars = str (&l);
5273 : 31564 : t = build_string (l, chars);
5274 : : }
5275 : 31564 : break;
5276 : :
5277 : 24 : case VECTOR_CST:
5278 : 24 : {
5279 : 24 : unsigned log2_npats = u ();
5280 : 24 : unsigned elts_per = u ();
5281 : 24 : t = make_vector (log2_npats, elts_per);
5282 : : }
5283 : 24 : break;
5284 : :
5285 : 156657 : case TREE_BINFO:
5286 : 156657 : t = make_tree_binfo (u ());
5287 : 156657 : break;
5288 : :
5289 : 1114949 : case TREE_VEC:
5290 : 1114949 : t = make_tree_vec (u ());
5291 : 1114949 : break;
5292 : :
5293 : 0 : case FIXED_CST:
5294 : 0 : case IDENTIFIER_NODE:
5295 : 0 : case SSA_NAME:
5296 : 0 : case TARGET_MEM_REF:
5297 : 0 : case TRANSLATION_UNIT_DECL:
5298 : 0 : goto fail;
5299 : : }
5300 : :
5301 : : return t;
5302 : : }
5303 : :
5304 : : /* The structure streamers access the raw fields, because the
5305 : : alternative, of using the accessor macros can require using
5306 : : different accessors for the same underlying field, depending on the
5307 : : tree code. That's both confusing and annoying. */
5308 : :
5309 : : /* Read & write the core boolean flags. */
5310 : :
5311 : : void
5312 : 19036224 : trees_out::core_bools (tree t, bits_out& bits)
5313 : : {
5314 : : #define WB(X) (bits.b (X))
5315 : : /* Stream X if COND holds, and if !COND stream a dummy value so that the
5316 : : overall number of bits streamed is independent of the runtime value
5317 : : of COND, which allows the compiler to better optimize this function. */
5318 : : #define WB_IF(COND, X) WB ((COND) ? (X) : false)
5319 : 19036224 : tree_code code = TREE_CODE (t);
5320 : :
5321 : 19036224 : WB (t->base.side_effects_flag);
5322 : 19036224 : WB (t->base.constant_flag);
5323 : 19036224 : WB (t->base.addressable_flag);
5324 : 19036224 : WB (t->base.volatile_flag);
5325 : 19036224 : WB (t->base.readonly_flag);
5326 : : /* base.asm_written_flag is a property of the current TU's use of
5327 : : this decl. */
5328 : 19036224 : WB (t->base.nowarning_flag);
5329 : : /* base.visited read as zero (it's set for writer, because that's
5330 : : how we mark nodes). */
5331 : : /* base.used_flag is not streamed. Readers may set TREE_USED of
5332 : : decls they use. */
5333 : 19036224 : WB (t->base.nothrow_flag);
5334 : 19036224 : WB (t->base.static_flag);
5335 : : /* This is TYPE_CACHED_VALUES_P for types. */
5336 : 19036224 : WB_IF (TREE_CODE_CLASS (code) != tcc_type, t->base.public_flag);
5337 : 19036224 : WB (t->base.private_flag);
5338 : 19036224 : WB (t->base.protected_flag);
5339 : 19036224 : WB (t->base.deprecated_flag);
5340 : 19036224 : WB (t->base.default_def_flag);
5341 : :
5342 : 19036224 : switch (code)
5343 : : {
5344 : : case CALL_EXPR:
5345 : : case INTEGER_CST:
5346 : : case SSA_NAME:
5347 : : case TARGET_MEM_REF:
5348 : : case TREE_VEC:
5349 : : /* These use different base.u fields. */
5350 : : return;
5351 : :
5352 : 16533229 : default:
5353 : 16533229 : WB (t->base.u.bits.lang_flag_0);
5354 : 16533229 : bool flag_1 = t->base.u.bits.lang_flag_1;
5355 : 16533229 : if (!flag_1)
5356 : : ;
5357 : 506642 : else if (code == TEMPLATE_INFO)
5358 : : /* This is TI_PENDING_TEMPLATE_FLAG, not relevant to reader. */
5359 : : flag_1 = false;
5360 : 502324 : else if (code == VAR_DECL)
5361 : : {
5362 : : /* This is DECL_INITIALIZED_P. */
5363 : 83340 : if (TREE_CODE (DECL_CONTEXT (t)) != FUNCTION_DECL)
5364 : : /* We'll set this when reading the definition. */
5365 : 16533229 : flag_1 = false;
5366 : : }
5367 : 16533229 : WB (flag_1);
5368 : 16533229 : WB (t->base.u.bits.lang_flag_2);
5369 : 16533229 : WB (t->base.u.bits.lang_flag_3);
5370 : 16533229 : WB (t->base.u.bits.lang_flag_4);
5371 : 16533229 : WB (t->base.u.bits.lang_flag_5);
5372 : 16533229 : WB (t->base.u.bits.lang_flag_6);
5373 : 16533229 : WB (t->base.u.bits.saturating_flag);
5374 : 16533229 : WB (t->base.u.bits.unsigned_flag);
5375 : 16533229 : WB (t->base.u.bits.packed_flag);
5376 : 16533229 : WB (t->base.u.bits.user_align);
5377 : 16533229 : WB (t->base.u.bits.nameless_flag);
5378 : 16533229 : WB (t->base.u.bits.atomic_flag);
5379 : 16533229 : WB (t->base.u.bits.unavailable_flag);
5380 : 16533229 : break;
5381 : : }
5382 : :
5383 : 16533229 : if (TREE_CODE_CLASS (code) == tcc_type)
5384 : : {
5385 : 826770 : WB (t->type_common.no_force_blk_flag);
5386 : 826770 : WB (t->type_common.needs_constructing_flag);
5387 : 826770 : WB (t->type_common.transparent_aggr_flag);
5388 : 826770 : WB (t->type_common.restrict_flag);
5389 : 826770 : WB (t->type_common.string_flag);
5390 : 826770 : WB (t->type_common.lang_flag_0);
5391 : 826770 : WB (t->type_common.lang_flag_1);
5392 : 826770 : WB (t->type_common.lang_flag_2);
5393 : 826770 : WB (t->type_common.lang_flag_3);
5394 : 826770 : WB (t->type_common.lang_flag_4);
5395 : 826770 : WB (t->type_common.lang_flag_5);
5396 : 826770 : WB (t->type_common.lang_flag_6);
5397 : 826770 : WB (t->type_common.typeless_storage);
5398 : : }
5399 : :
5400 : 16533229 : if (TREE_CODE_CLASS (code) != tcc_declaration)
5401 : : return;
5402 : :
5403 : 4446371 : if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
5404 : : {
5405 : 4446371 : WB (t->decl_common.nonlocal_flag);
5406 : 4446371 : WB (t->decl_common.virtual_flag);
5407 : 4446371 : WB (t->decl_common.ignored_flag);
5408 : 4446371 : WB (t->decl_common.abstract_flag);
5409 : 4446371 : WB (t->decl_common.artificial_flag);
5410 : 4446371 : WB (t->decl_common.preserve_flag);
5411 : 4446371 : WB (t->decl_common.debug_expr_is_from);
5412 : 4446371 : WB (t->decl_common.lang_flag_0);
5413 : 4446371 : WB (t->decl_common.lang_flag_1);
5414 : 4446371 : WB (t->decl_common.lang_flag_2);
5415 : 4446371 : WB (t->decl_common.lang_flag_3);
5416 : 4446371 : WB (t->decl_common.lang_flag_4);
5417 : :
5418 : 4446371 : {
5419 : : /* This is DECL_INTERFACE_KNOWN: We should redetermine whether
5420 : : we need to import or export any vtables or typeinfo objects
5421 : : on stream-in. */
5422 : 4446371 : bool interface_known = t->decl_common.lang_flag_5;
5423 : 4446371 : if (VAR_P (t) && (DECL_VTABLE_OR_VTT_P (t) || DECL_TINFO_P (t)))
5424 : : interface_known = false;
5425 : 4446371 : WB (interface_known);
5426 : : }
5427 : :
5428 : 4446371 : WB (t->decl_common.lang_flag_6);
5429 : 4446371 : WB (t->decl_common.lang_flag_7);
5430 : 4446371 : WB (t->decl_common.lang_flag_8);
5431 : 4446371 : WB (t->decl_common.decl_flag_0);
5432 : :
5433 : 4446371 : {
5434 : : /* DECL_EXTERNAL -> decl_flag_1
5435 : : == it is defined elsewhere
5436 : : DECL_NOT_REALLY_EXTERN -> base.not_really_extern
5437 : : == that was a lie, it is here */
5438 : :
5439 : 4446371 : bool is_external = t->decl_common.decl_flag_1;
5440 : 4446371 : if (!is_external)
5441 : : /* decl_flag_1 is DECL_EXTERNAL. Things we emit here, might
5442 : : well be external from the POV of an importer. */
5443 : : // FIXME: Do we need to know if this is a TEMPLATE_RESULT --
5444 : : // a flag from the caller?
5445 : 3708217 : switch (code)
5446 : : {
5447 : : default:
5448 : : break;
5449 : :
5450 : 247369 : case VAR_DECL:
5451 : 247369 : if (TREE_PUBLIC (t)
5452 : 43532 : && !(TREE_STATIC (t)
5453 : 43532 : && DECL_FUNCTION_SCOPE_P (t)
5454 : 135 : && DECL_DECLARED_INLINE_P (DECL_CONTEXT (t)))
5455 : 290856 : && !DECL_VAR_DECLARED_INLINE_P (t))
5456 : : is_external = true;
5457 : : break;
5458 : :
5459 : 38745 : case FUNCTION_DECL:
5460 : 38745 : if (TREE_PUBLIC (t)
5461 : 75962 : && !DECL_DECLARED_INLINE_P (t))
5462 : : is_external = true;
5463 : : break;
5464 : : }
5465 : 4446371 : WB (is_external);
5466 : : }
5467 : :
5468 : 4446371 : WB (t->decl_common.decl_flag_2);
5469 : 4446371 : WB (t->decl_common.decl_flag_3);
5470 : 4446371 : WB (t->decl_common.not_gimple_reg_flag);
5471 : 4446371 : WB (t->decl_common.decl_by_reference_flag);
5472 : 4446371 : WB (t->decl_common.decl_read_flag);
5473 : 4446371 : WB (t->decl_common.decl_nonshareable_flag);
5474 : 4446371 : WB (t->decl_common.decl_not_flexarray);
5475 : : }
5476 : : else
5477 : : return;
5478 : :
5479 : 4446371 : if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
5480 : : {
5481 : 2077855 : WB (t->decl_with_vis.defer_output);
5482 : 2077855 : WB (t->decl_with_vis.hard_register);
5483 : 2077855 : WB (t->decl_with_vis.common_flag);
5484 : 2077855 : WB (t->decl_with_vis.in_text_section);
5485 : 2077855 : WB (t->decl_with_vis.in_constant_pool);
5486 : 2077855 : WB (t->decl_with_vis.dllimport_flag);
5487 : 2077855 : WB (t->decl_with_vis.weak_flag);
5488 : 2077855 : WB (t->decl_with_vis.seen_in_bind_expr);
5489 : 2077855 : WB (t->decl_with_vis.comdat_flag);
5490 : 2077855 : WB (t->decl_with_vis.visibility_specified);
5491 : 2077855 : WB (t->decl_with_vis.init_priority_p);
5492 : 2077855 : WB (t->decl_with_vis.shadowed_for_var_p);
5493 : 2077855 : WB (t->decl_with_vis.cxx_constructor);
5494 : 2077855 : WB (t->decl_with_vis.cxx_destructor);
5495 : 2077855 : WB (t->decl_with_vis.final);
5496 : 2077855 : WB (t->decl_with_vis.regdecl_flag);
5497 : : }
5498 : : else
5499 : : return;
5500 : :
5501 : 2077855 : if (CODE_CONTAINS_STRUCT (code, TS_FUNCTION_DECL))
5502 : : {
5503 : 621510 : WB (t->function_decl.static_ctor_flag);
5504 : 621510 : WB (t->function_decl.static_dtor_flag);
5505 : 621510 : WB (t->function_decl.uninlinable);
5506 : 621510 : WB (t->function_decl.possibly_inlined);
5507 : 621510 : WB (t->function_decl.novops_flag);
5508 : 621510 : WB (t->function_decl.returns_twice_flag);
5509 : 621510 : WB (t->function_decl.malloc_flag);
5510 : 621510 : WB (t->function_decl.declared_inline_flag);
5511 : 621510 : WB (t->function_decl.no_inline_warning_flag);
5512 : 621510 : WB (t->function_decl.no_instrument_function_entry_exit);
5513 : 621510 : WB (t->function_decl.no_limit_stack);
5514 : 621510 : WB (t->function_decl.disregard_inline_limits);
5515 : 621510 : WB (t->function_decl.pure_flag);
5516 : 621510 : WB (t->function_decl.looping_const_or_pure_flag);
5517 : :
5518 : 621510 : WB (t->function_decl.has_debug_args_flag);
5519 : 621510 : WB (t->function_decl.versioned_function);
5520 : :
5521 : : /* decl_type is a (misnamed) 2 bit discriminator. */
5522 : 621510 : unsigned kind = t->function_decl.decl_type;
5523 : 621510 : WB ((kind >> 0) & 1);
5524 : 621510 : WB ((kind >> 1) & 1);
5525 : : }
5526 : : #undef WB_IF
5527 : : #undef WB
5528 : : }
5529 : :
5530 : : bool
5531 : 20571371 : trees_in::core_bools (tree t, bits_in& bits)
5532 : : {
5533 : : #define RB(X) ((X) = bits.b ())
5534 : : /* See the comment for WB_IF in trees_out::core_bools. */
5535 : : #define RB_IF(COND, X) ((COND) ? RB (X) : bits.b ())
5536 : :
5537 : 20571371 : tree_code code = TREE_CODE (t);
5538 : :
5539 : 20571371 : RB (t->base.side_effects_flag);
5540 : 20571371 : RB (t->base.constant_flag);
5541 : 20571371 : RB (t->base.addressable_flag);
5542 : 20571371 : RB (t->base.volatile_flag);
5543 : 20571371 : RB (t->base.readonly_flag);
5544 : : /* base.asm_written_flag is not streamed. */
5545 : 20571371 : RB (t->base.nowarning_flag);
5546 : : /* base.visited is not streamed. */
5547 : : /* base.used_flag is not streamed. */
5548 : 20571371 : RB (t->base.nothrow_flag);
5549 : 20571371 : RB (t->base.static_flag);
5550 : 20571371 : RB_IF (TREE_CODE_CLASS (code) != tcc_type, t->base.public_flag);
5551 : 20571371 : RB (t->base.private_flag);
5552 : 20571371 : RB (t->base.protected_flag);
5553 : 20571371 : RB (t->base.deprecated_flag);
5554 : 20571371 : RB (t->base.default_def_flag);
5555 : :
5556 : 20571371 : switch (code)
5557 : : {
5558 : 2638950 : case CALL_EXPR:
5559 : 2638950 : case INTEGER_CST:
5560 : 2638950 : case SSA_NAME:
5561 : 2638950 : case TARGET_MEM_REF:
5562 : 2638950 : case TREE_VEC:
5563 : : /* These use different base.u fields. */
5564 : 2638950 : goto done;
5565 : :
5566 : 17932421 : default:
5567 : 17932421 : RB (t->base.u.bits.lang_flag_0);
5568 : 17932421 : RB (t->base.u.bits.lang_flag_1);
5569 : 17932421 : RB (t->base.u.bits.lang_flag_2);
5570 : 17932421 : RB (t->base.u.bits.lang_flag_3);
5571 : 17932421 : RB (t->base.u.bits.lang_flag_4);
5572 : 17932421 : RB (t->base.u.bits.lang_flag_5);
5573 : 17932421 : RB (t->base.u.bits.lang_flag_6);
5574 : 17932421 : RB (t->base.u.bits.saturating_flag);
5575 : 17932421 : RB (t->base.u.bits.unsigned_flag);
5576 : 17932421 : RB (t->base.u.bits.packed_flag);
5577 : 17932421 : RB (t->base.u.bits.user_align);
5578 : 17932421 : RB (t->base.u.bits.nameless_flag);
5579 : 17932421 : RB (t->base.u.bits.atomic_flag);
5580 : 17932421 : RB (t->base.u.bits.unavailable_flag);
5581 : 17932421 : break;
5582 : : }
5583 : :
5584 : 17932421 : if (TREE_CODE_CLASS (code) == tcc_type)
5585 : : {
5586 : 849602 : RB (t->type_common.no_force_blk_flag);
5587 : 849602 : RB (t->type_common.needs_constructing_flag);
5588 : 849602 : RB (t->type_common.transparent_aggr_flag);
5589 : 849602 : RB (t->type_common.restrict_flag);
5590 : 849602 : RB (t->type_common.string_flag);
5591 : 849602 : RB (t->type_common.lang_flag_0);
5592 : 849602 : RB (t->type_common.lang_flag_1);
5593 : 849602 : RB (t->type_common.lang_flag_2);
5594 : 849602 : RB (t->type_common.lang_flag_3);
5595 : 849602 : RB (t->type_common.lang_flag_4);
5596 : 849602 : RB (t->type_common.lang_flag_5);
5597 : 849602 : RB (t->type_common.lang_flag_6);
5598 : 849602 : RB (t->type_common.typeless_storage);
5599 : : }
5600 : :
5601 : 17932421 : if (TREE_CODE_CLASS (code) != tcc_declaration)
5602 : 13157288 : goto done;
5603 : :
5604 : 4775133 : if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
5605 : : {
5606 : 4775133 : RB (t->decl_common.nonlocal_flag);
5607 : 4775133 : RB (t->decl_common.virtual_flag);
5608 : 4775133 : RB (t->decl_common.ignored_flag);
5609 : 4775133 : RB (t->decl_common.abstract_flag);
5610 : 4775133 : RB (t->decl_common.artificial_flag);
5611 : 4775133 : RB (t->decl_common.preserve_flag);
5612 : 4775133 : RB (t->decl_common.debug_expr_is_from);
5613 : 4775133 : RB (t->decl_common.lang_flag_0);
5614 : 4775133 : RB (t->decl_common.lang_flag_1);
5615 : 4775133 : RB (t->decl_common.lang_flag_2);
5616 : 4775133 : RB (t->decl_common.lang_flag_3);
5617 : 4775133 : RB (t->decl_common.lang_flag_4);
5618 : 4775133 : RB (t->decl_common.lang_flag_5);
5619 : 4775133 : RB (t->decl_common.lang_flag_6);
5620 : 4775133 : RB (t->decl_common.lang_flag_7);
5621 : 4775133 : RB (t->decl_common.lang_flag_8);
5622 : 4775133 : RB (t->decl_common.decl_flag_0);
5623 : 4775133 : RB (t->decl_common.decl_flag_1);
5624 : 4775133 : RB (t->decl_common.decl_flag_2);
5625 : 4775133 : RB (t->decl_common.decl_flag_3);
5626 : 4775133 : RB (t->decl_common.not_gimple_reg_flag);
5627 : 4775133 : RB (t->decl_common.decl_by_reference_flag);
5628 : 4775133 : RB (t->decl_common.decl_read_flag);
5629 : 4775133 : RB (t->decl_common.decl_nonshareable_flag);
5630 : 4775133 : RB (t->decl_common.decl_not_flexarray);
5631 : : }
5632 : : else
5633 : 0 : goto done;
5634 : :
5635 : 4775133 : if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
5636 : : {
5637 : 2203968 : RB (t->decl_with_vis.defer_output);
5638 : 2203968 : RB (t->decl_with_vis.hard_register);
5639 : 2203968 : RB (t->decl_with_vis.common_flag);
5640 : 2203968 : RB (t->decl_with_vis.in_text_section);
5641 : 2203968 : RB (t->decl_with_vis.in_constant_pool);
5642 : 2203968 : RB (t->decl_with_vis.dllimport_flag);
5643 : 2203968 : RB (t->decl_with_vis.weak_flag);
5644 : 2203968 : RB (t->decl_with_vis.seen_in_bind_expr);
5645 : 2203968 : RB (t->decl_with_vis.comdat_flag);
5646 : 2203968 : RB (t->decl_with_vis.visibility_specified);
5647 : 2203968 : RB (t->decl_with_vis.init_priority_p);
5648 : 2203968 : RB (t->decl_with_vis.shadowed_for_var_p);
5649 : 2203968 : RB (t->decl_with_vis.cxx_constructor);
5650 : 2203968 : RB (t->decl_with_vis.cxx_destructor);
5651 : 2203968 : RB (t->decl_with_vis.final);
5652 : 2203968 : RB (t->decl_with_vis.regdecl_flag);
5653 : : }
5654 : : else
5655 : 2571165 : goto done;
5656 : :
5657 : 2203968 : if (CODE_CONTAINS_STRUCT (code, TS_FUNCTION_DECL))
5658 : : {
5659 : 689451 : RB (t->function_decl.static_ctor_flag);
5660 : 689451 : RB (t->function_decl.static_dtor_flag);
5661 : 689451 : RB (t->function_decl.uninlinable);
5662 : 689451 : RB (t->function_decl.possibly_inlined);
5663 : 689451 : RB (t->function_decl.novops_flag);
5664 : 689451 : RB (t->function_decl.returns_twice_flag);
5665 : 689451 : RB (t->function_decl.malloc_flag);
5666 : 689451 : RB (t->function_decl.declared_inline_flag);
5667 : 689451 : RB (t->function_decl.no_inline_warning_flag);
5668 : 689451 : RB (t->function_decl.no_instrument_function_entry_exit);
5669 : 689451 : RB (t->function_decl.no_limit_stack);
5670 : 689451 : RB (t->function_decl.disregard_inline_limits);
5671 : 689451 : RB (t->function_decl.pure_flag);
5672 : 689451 : RB (t->function_decl.looping_const_or_pure_flag);
5673 : :
5674 : 689451 : RB (t->function_decl.has_debug_args_flag);
5675 : 689451 : RB (t->function_decl.versioned_function);
5676 : :
5677 : : /* decl_type is a (misnamed) 2 bit discriminator. */
5678 : 689451 : unsigned kind = 0;
5679 : 689451 : kind |= unsigned (bits.b ()) << 0;
5680 : 689451 : kind |= unsigned (bits.b ()) << 1;
5681 : 689451 : t->function_decl.decl_type = function_decl_type (kind);
5682 : : }
5683 : : #undef RB_IF
5684 : : #undef RB
5685 : 1514517 : done:
5686 : 20571371 : return !get_overrun ();
5687 : : }
5688 : :
5689 : : void
5690 : 2874601 : trees_out::lang_decl_bools (tree t, bits_out& bits)
5691 : : {
5692 : : #define WB(X) (bits.b (X))
5693 : 2874601 : const struct lang_decl *lang = DECL_LANG_SPECIFIC (t);
5694 : :
5695 : 2874601 : bits.bflush ();
5696 : 2874601 : WB (lang->u.base.language == lang_cplusplus);
5697 : 2874601 : WB ((lang->u.base.use_template >> 0) & 1);
5698 : 2874601 : WB ((lang->u.base.use_template >> 1) & 1);
5699 : : /* Do not write lang->u.base.not_really_extern, importer will set
5700 : : when reading the definition (if any). */
5701 : 2874601 : WB (lang->u.base.initialized_in_class);
5702 : 2874601 : WB (lang->u.base.threadprivate_or_deleted_p);
5703 : : /* Do not write lang->u.base.anticipated_p, it is a property of the
5704 : : current TU. */
5705 : 2874601 : WB (lang->u.base.friend_or_tls);
5706 : 2874601 : WB (lang->u.base.unknown_bound_p);
5707 : : /* Do not write lang->u.base.odr_used, importer will recalculate if
5708 : : they do ODR use this decl. */
5709 : 2874601 : WB (lang->u.base.concept_p);
5710 : 2874601 : WB (lang->u.base.var_declared_inline_p);
5711 : 2874601 : WB (lang->u.base.dependent_init_p);
5712 : : /* When building a header unit, everthing is marked as purview, (so
5713 : : we know which decls to write). But when we import them we do not
5714 : : want to mark them as in module purview. */
5715 : 5738495 : WB (lang->u.base.module_purview_p && !header_module_p ());
5716 : 2874601 : WB (lang->u.base.module_attach_p);
5717 : 2874601 : WB (lang->u.base.module_keyed_decls_p);
5718 : 2874601 : switch (lang->u.base.selector)
5719 : : {
5720 : 0 : default:
5721 : 0 : gcc_unreachable ();
5722 : :
5723 : 621510 : case lds_fn: /* lang_decl_fn. */
5724 : 621510 : WB (lang->u.fn.global_ctor_p);
5725 : 621510 : WB (lang->u.fn.global_dtor_p);
5726 : 621510 : WB (lang->u.fn.static_function);
5727 : 621510 : WB (lang->u.fn.pure_virtual);
5728 : 621510 : WB (lang->u.fn.defaulted_p);
5729 : 621510 : WB (lang->u.fn.has_in_charge_parm_p);
5730 : 621510 : WB (lang->u.fn.has_vtt_parm_p);
5731 : : /* There shouldn't be a pending inline at this point. */
5732 : 621510 : gcc_assert (!lang->u.fn.pending_inline_p);
5733 : 621510 : WB (lang->u.fn.nonconverting);
5734 : 621510 : WB (lang->u.fn.thunk_p);
5735 : 621510 : WB (lang->u.fn.this_thunk_p);
5736 : : /* Do not stream lang->u.hidden_friend_p, it is a property of
5737 : : the TU. */
5738 : 621510 : WB (lang->u.fn.omp_declare_reduction_p);
5739 : 621510 : WB (lang->u.fn.has_dependent_explicit_spec_p);
5740 : 621510 : WB (lang->u.fn.immediate_fn_p);
5741 : 621510 : WB (lang->u.fn.maybe_deleted);
5742 : : /* We do not stream lang->u.fn.implicit_constexpr. */
5743 : 621510 : WB (lang->u.fn.escalated_p);
5744 : 621510 : WB (lang->u.fn.xobj_func);
5745 : 621510 : goto lds_min;
5746 : :
5747 : 1366 : case lds_decomp: /* lang_decl_decomp. */
5748 : : /* No bools. */
5749 : 1366 : goto lds_min;
5750 : :
5751 : : case lds_min: /* lang_decl_min. */
5752 : 2874601 : lds_min:
5753 : : /* No bools. */
5754 : : break;
5755 : :
5756 : : case lds_ns: /* lang_decl_ns. */
5757 : : /* No bools. */
5758 : : break;
5759 : :
5760 : : case lds_parm: /* lang_decl_parm. */
5761 : : /* No bools. */
5762 : : break;
5763 : : }
5764 : : #undef WB
5765 : 2874601 : }
5766 : :
5767 : : bool
5768 : 3099977 : trees_in::lang_decl_bools (tree t, bits_in& bits)
5769 : : {
5770 : : #define RB(X) ((X) = bits.b ())
5771 : 3099977 : struct lang_decl *lang = DECL_LANG_SPECIFIC (t);
5772 : :
5773 : 3099977 : bits.bflush ();
5774 : 3099977 : lang->u.base.language = bits.b () ? lang_cplusplus : lang_c;
5775 : 3099977 : unsigned v;
5776 : 3099977 : v = bits.b () << 0;
5777 : 3099977 : v |= bits.b () << 1;
5778 : 3099977 : lang->u.base.use_template = v;
5779 : : /* lang->u.base.not_really_extern is not streamed. */
5780 : 3099977 : RB (lang->u.base.initialized_in_class);
5781 : 3099977 : RB (lang->u.base.threadprivate_or_deleted_p);
5782 : : /* lang->u.base.anticipated_p is not streamed. */
5783 : 3099977 : RB (lang->u.base.friend_or_tls);
5784 : 3099977 : RB (lang->u.base.unknown_bound_p);
5785 : : /* lang->u.base.odr_used is not streamed. */
5786 : 3099977 : RB (lang->u.base.concept_p);
5787 : 3099977 : RB (lang->u.base.var_declared_inline_p);
5788 : 3099977 : RB (lang->u.base.dependent_init_p);
5789 : 3099977 : RB (lang->u.base.module_purview_p);
5790 : 3099977 : RB (lang->u.base.module_attach_p);
5791 : 3099977 : RB (lang->u.base.module_keyed_decls_p);
5792 : 3099977 : switch (lang->u.base.selector)
5793 : : {
5794 : 0 : default:
5795 : 0 : gcc_unreachable ();
5796 : :
5797 : 689451 : case lds_fn: /* lang_decl_fn. */
5798 : 689451 : RB (lang->u.fn.global_ctor_p);
5799 : 689451 : RB (lang->u.fn.global_dtor_p);
5800 : 689451 : RB (lang->u.fn.static_function);
5801 : 689451 : RB (lang->u.fn.pure_virtual);
5802 : 689451 : RB (lang->u.fn.defaulted_p);
5803 : 689451 : RB (lang->u.fn.has_in_charge_parm_p);
5804 : 689451 : RB (lang->u.fn.has_vtt_parm_p);
5805 : 689451 : RB (lang->u.fn.nonconverting);
5806 : 689451 : RB (lang->u.fn.thunk_p);
5807 : 689451 : RB (lang->u.fn.this_thunk_p);
5808 : : /* lang->u.fn.hidden_friend_p is not streamed. */
5809 : 689451 : RB (lang->u.fn.omp_declare_reduction_p);
5810 : 689451 : RB (lang->u.fn.has_dependent_explicit_spec_p);
5811 : 689451 : RB (lang->u.fn.immediate_fn_p);
5812 : 689451 : RB (lang->u.fn.maybe_deleted);
5813 : : /* We do not stream lang->u.fn.implicit_constexpr. */
5814 : 689451 : RB (lang->u.fn.escalated_p);
5815 : 689451 : RB (lang->u.fn.xobj_func);
5816 : 689451 : goto lds_min;
5817 : :
5818 : 1411 : case lds_decomp: /* lang_decl_decomp. */
5819 : : /* No bools. */
5820 : 1411 : goto lds_min;
5821 : :
5822 : : case lds_min: /* lang_decl_min. */
5823 : 3099977 : lds_min:
5824 : : /* No bools. */
5825 : : break;
5826 : :
5827 : : case lds_ns: /* lang_decl_ns. */
5828 : : /* No bools. */
5829 : : break;
5830 : :
5831 : : case lds_parm: /* lang_decl_parm. */
5832 : : /* No bools. */
5833 : : break;
5834 : : }
5835 : : #undef RB
5836 : 3099977 : return !get_overrun ();
5837 : : }
5838 : :
5839 : : void
5840 : 233193 : trees_out::lang_type_bools (tree t, bits_out& bits)
5841 : : {
5842 : : #define WB(X) (bits.b (X))
5843 : 233193 : const struct lang_type *lang = TYPE_LANG_SPECIFIC (t);
5844 : :
5845 : 233193 : bits.bflush ();
5846 : 233193 : WB (lang->has_type_conversion);
5847 : 233193 : WB (lang->has_copy_ctor);
5848 : 233193 : WB (lang->has_default_ctor);
5849 : 233193 : WB (lang->const_needs_init);
5850 : 233193 : WB (lang->ref_needs_init);
5851 : 233193 : WB (lang->has_const_copy_assign);
5852 : 233193 : WB ((lang->use_template >> 0) & 1);
5853 : 233193 : WB ((lang->use_template >> 1) & 1);
5854 : :
5855 : 233193 : WB (lang->has_mutable);
5856 : 233193 : WB (lang->com_interface);
5857 : 233193 : WB (lang->non_pod_class);
5858 : 233193 : WB (lang->nearly_empty_p);
5859 : 233193 : WB (lang->user_align);
5860 : 233193 : WB (lang->has_copy_assign);
5861 : 233193 : WB (lang->has_new);
5862 : 233193 : WB (lang->has_array_new);
5863 : :
5864 : 233193 : WB ((lang->gets_delete >> 0) & 1);
5865 : 233193 : WB ((lang->gets_delete >> 1) & 1);
5866 : 233193 : WB (lang->interface_only);
5867 : 233193 : WB (lang->interface_unknown);
5868 : 233193 : WB (lang->contains_empty_class_p);
5869 : 233193 : WB (lang->anon_aggr);
5870 : 233193 : WB (lang->non_zero_init);
5871 : 233193 : WB (lang->empty_p);
5872 : :
5873 : 233193 : WB (lang->vec_new_uses_cookie);
5874 : 233193 : WB (lang->declared_class);
5875 : 233193 : WB (lang->diamond_shaped);
5876 : 233193 : WB (lang->repeated_base);
5877 : 233193 : gcc_assert (!lang->being_defined);
5878 : : // lang->debug_requested
5879 : 233193 : WB (lang->fields_readonly);
5880 : 233193 : WB (lang->ptrmemfunc_flag);
5881 : :
5882 : 233193 : WB (lang->lazy_default_ctor);
5883 : 233193 : WB (lang->lazy_copy_ctor);
5884 : 233193 : WB (lang->lazy_copy_assign);
5885 : 233193 : WB (lang->lazy_destructor);
5886 : 233193 : WB (lang->has_const_copy_ctor);
5887 : 233193 : WB (lang->has_complex_copy_ctor);
5888 : 233193 : WB (lang->has_complex_copy_assign);
5889 : 233193 : WB (lang->non_aggregate);
5890 : :
5891 : 233193 : WB (lang->has_complex_dflt);
5892 : 233193 : WB (lang->has_list_ctor);
5893 : 233193 : WB (lang->non_std_layout);
5894 : 233193 : WB (lang->is_literal);
5895 : 233193 : WB (lang->lazy_move_ctor);
5896 : 233193 : WB (lang->lazy_move_assign);
5897 : 233193 : WB (lang->has_complex_move_ctor);
5898 : 233193 : WB (lang->has_complex_move_assign);
5899 : :
5900 : 233193 : WB (lang->has_constexpr_ctor);
5901 : 233193 : WB (lang->unique_obj_representations);
5902 : 233193 : WB (lang->unique_obj_representations_set);
5903 : : #undef WB
5904 : 233193 : }
5905 : :
5906 : : bool
5907 : 238469 : trees_in::lang_type_bools (tree t, bits_in& bits)
5908 : : {
5909 : : #define RB(X) ((X) = bits.b ())
5910 : 238469 : struct lang_type *lang = TYPE_LANG_SPECIFIC (t);
5911 : :
5912 : 238469 : bits.bflush ();
5913 : 238469 : RB (lang->has_type_conversion);
5914 : 238469 : RB (lang->has_copy_ctor);
5915 : 238469 : RB (lang->has_default_ctor);
5916 : 238469 : RB (lang->const_needs_init);
5917 : 238469 : RB (lang->ref_needs_init);
5918 : 238469 : RB (lang->has_const_copy_assign);
5919 : 238469 : unsigned v;
5920 : 238469 : v = bits.b () << 0;
5921 : 238469 : v |= bits.b () << 1;
5922 : 238469 : lang->use_template = v;
5923 : :
5924 : 238469 : RB (lang->has_mutable);
5925 : 238469 : RB (lang->com_interface);
5926 : 238469 : RB (lang->non_pod_class);
5927 : 238469 : RB (lang->nearly_empty_p);
5928 : 238469 : RB (lang->user_align);
5929 : 238469 : RB (lang->has_copy_assign);
5930 : 238469 : RB (lang->has_new);
5931 : 238469 : RB (lang->has_array_new);
5932 : :
5933 : 238469 : v = bits.b () << 0;
5934 : 238469 : v |= bits.b () << 1;
5935 : 238469 : lang->gets_delete = v;
5936 : 238469 : RB (lang->interface_only);
5937 : 238469 : RB (lang->interface_unknown);
5938 : 238469 : RB (lang->contains_empty_class_p);
5939 : 238469 : RB (lang->anon_aggr);
5940 : 238469 : RB (lang->non_zero_init);
5941 : 238469 : RB (lang->empty_p);
5942 : :
5943 : 238469 : RB (lang->vec_new_uses_cookie);
5944 : 238469 : RB (lang->declared_class);
5945 : 238469 : RB (lang->diamond_shaped);
5946 : 238469 : RB (lang->repeated_base);
5947 : 238469 : gcc_assert (!lang->being_defined);
5948 : 238469 : gcc_assert (!lang->debug_requested);
5949 : 238469 : RB (lang->fields_readonly);
5950 : 238469 : RB (lang->ptrmemfunc_flag);
5951 : :
5952 : 238469 : RB (lang->lazy_default_ctor);
5953 : 238469 : RB (lang->lazy_copy_ctor);
5954 : 238469 : RB (lang->lazy_copy_assign);
5955 : 238469 : RB (lang->lazy_destructor);
5956 : 238469 : RB (lang->has_const_copy_ctor);
5957 : 238469 : RB (lang->has_complex_copy_ctor);
5958 : 238469 : RB (lang->has_complex_copy_assign);
5959 : 238469 : RB (lang->non_aggregate);
5960 : :
5961 : 238469 : RB (lang->has_complex_dflt);
5962 : 238469 : RB (lang->has_list_ctor);
5963 : 238469 : RB (lang->non_std_layout);
5964 : 238469 : RB (lang->is_literal);
5965 : 238469 : RB (lang->lazy_move_ctor);
5966 : 238469 : RB (lang->lazy_move_assign);
5967 : 238469 : RB (lang->has_complex_move_ctor);
5968 : 238469 : RB (lang->has_complex_move_assign);
5969 : :
5970 : 238469 : RB (lang->has_constexpr_ctor);
5971 : 238469 : RB (lang->unique_obj_representations);
5972 : 238469 : RB (lang->unique_obj_representations_set);
5973 : : #undef RB
5974 : 238469 : return !get_overrun ();
5975 : : }
5976 : :
5977 : : /* Read & write the core values and pointers. */
5978 : :
5979 : : void
5980 : 49658684 : trees_out::core_vals (tree t)
5981 : : {
5982 : : #define WU(X) (u (X))
5983 : : #define WT(X) (tree_node (X))
5984 : 49658684 : tree_code code = TREE_CODE (t);
5985 : :
5986 : : /* First by shape of the tree. */
5987 : :
5988 : 49658684 : if (CODE_CONTAINS_STRUCT (code, TS_DECL_MINIMAL))
5989 : : {
5990 : : /* Write this early, for better log information. */
5991 : 11583705 : WT (t->decl_minimal.name);
5992 : 11583705 : if (!DECL_TEMPLATE_PARM_P (t))
5993 : 8935811 : WT (t->decl_minimal.context);
5994 : :
5995 : 11583705 : if (state)
5996 : 9475470 : state->write_location (*this, t->decl_minimal.locus);
5997 : : }
5998 : :
5999 : 49658684 : if (CODE_CONTAINS_STRUCT (code, TS_TYPE_COMMON))
6000 : : {
6001 : : /* The only types we write also have TYPE_NON_COMMON. */
6002 : 2910818 : gcc_checking_assert (CODE_CONTAINS_STRUCT (code, TS_TYPE_NON_COMMON));
6003 : :
6004 : : /* We only stream the main variant. */
6005 : 2910818 : gcc_checking_assert (TYPE_MAIN_VARIANT (t) == t);
6006 : :
6007 : : /* Stream the name & context first, for better log information */
6008 : 2910818 : WT (t->type_common.name);
6009 : 2910818 : WT (t->type_common.context);
6010 : :
6011 : : /* By construction we want to make sure we have the canonical
6012 : : and main variants already in the type table, so emit them
6013 : : now. */
6014 : 2910818 : WT (t->type_common.main_variant);
6015 : :
6016 : 2910818 : tree canonical = t->type_common.canonical;
6017 : 2910818 : if (canonical && DECL_TEMPLATE_PARM_P (TYPE_NAME (t)))
6018 : : /* We do not want to wander into different templates.
6019 : : Reconstructed on stream in. */
6020 : : canonical = t;
6021 : 2910818 : WT (canonical);
6022 : :
6023 : : /* type_common.next_variant is internally manipulated. */
6024 : : /* type_common.pointer_to, type_common.reference_to. */
6025 : :
6026 : 2910818 : if (streaming_p ())
6027 : : {
6028 : 799524 : WU (t->type_common.precision);
6029 : 799524 : WU (t->type_common.contains_placeholder_bits);
6030 : 799524 : WU (t->type_common.mode);
6031 : 799524 : WU (t->type_common.align);
6032 : : }
6033 : :
6034 : 2910818 : if (!RECORD_OR_UNION_CODE_P (code))
6035 : : {
6036 : 2381724 : WT (t->type_common.size);
6037 : 2381724 : WT (t->type_common.size_unit);
6038 : : }
6039 : 2910818 : WT (t->type_common.attributes);
6040 : :
6041 : 2910818 : WT (t->type_common.common.chain); /* TYPE_STUB_DECL. */
6042 : : }
6043 : :
6044 : 49658684 : if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
6045 : : {
6046 : 11583705 : if (streaming_p ())
6047 : : {
6048 : 4446371 : WU (t->decl_common.mode);
6049 : 4446371 : WU (t->decl_common.off_align);
6050 : 4446371 : WU (t->decl_common.align);
6051 : : }
6052 : :
6053 : : /* For templates these hold instantiation (partial and/or
6054 : : specialization) information. */
6055 : 11583705 : if (code != TEMPLATE_DECL)
6056 : : {
6057 : 10667627 : WT (t->decl_common.size);
6058 : 10667627 : WT (t->decl_common.size_unit);
6059 : : }
6060 : :
6061 : 11583705 : WT (t->decl_common.attributes);
6062 : : // FIXME: Does this introduce cross-decl links? For instance
6063 : : // from instantiation to the template. If so, we'll need more
6064 : : // deduplication logic. I think we'll need to walk the blocks
6065 : : // of the owning function_decl's abstract origin in tandem, to
6066 : : // generate the locating data needed?
6067 : 11583705 : WT (t->decl_common.abstract_origin);
6068 : : }
6069 : :
6070 : 49658684 : if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
6071 : : {
6072 : 5480905 : WT (t->decl_with_vis.assembler_name);
6073 : 5480905 : if (streaming_p ())
6074 : 2077855 : WU (t->decl_with_vis.visibility);
6075 : : }
6076 : :
6077 : 49658684 : if (CODE_CONTAINS_STRUCT (code, TS_TYPE_NON_COMMON))
6078 : : {
6079 : 2910818 : if (code == ENUMERAL_TYPE)
6080 : : {
6081 : : /* These fields get set even for opaque enums that lack a
6082 : : definition, so we stream them directly for each ENUMERAL_TYPE.
6083 : : We stream TYPE_VALUES as part of the definition. */
6084 : 11432 : WT (t->type_non_common.maxval);
6085 : 11432 : WT (t->type_non_common.minval);
6086 : : }
6087 : : /* Records and unions hold FIELDS, VFIELD & BINFO on these
6088 : : things. */
6089 : 2899386 : else if (!RECORD_OR_UNION_CODE_P (code))
6090 : : {
6091 : : // FIXME: These are from tpl_parm_value's 'type' writing.
6092 : : // Perhaps it should just be doing them directly?
6093 : 2370292 : gcc_checking_assert (code == TEMPLATE_TYPE_PARM
6094 : : || code == TEMPLATE_TEMPLATE_PARM
6095 : : || code == BOUND_TEMPLATE_TEMPLATE_PARM);
6096 : 2370292 : gcc_checking_assert (!TYPE_CACHED_VALUES_P (t));
6097 : 2370292 : WT (t->type_non_common.values);
6098 : 2370292 : WT (t->type_non_common.maxval);
6099 : 2370292 : WT (t->type_non_common.minval);
6100 : : }
6101 : :
6102 : 2910818 : WT (t->type_non_common.lang_1);
6103 : : }
6104 : :
6105 : 49658684 : if (CODE_CONTAINS_STRUCT (code, TS_EXP))
6106 : : {
6107 : 13988088 : if (state)
6108 : 13732652 : state->write_location (*this, t->exp.locus);
6109 : :
6110 : : /* Walk in forward order, as (for instance) REQUIRES_EXPR has a
6111 : : bunch of unscoped parms on its first operand. It's safer to
6112 : : create those in order. */
6113 : 13988088 : bool vl = TREE_CODE_CLASS (code) == tcc_vl_exp;
6114 : 51246387 : for (unsigned limit = (vl ? VL_EXP_OPERAND_LENGTH (t)
6115 : 12529121 : : TREE_OPERAND_LENGTH (t)),
6116 : 38717266 : ix = unsigned (vl); ix != limit; ix++)
6117 : 24729178 : WT (TREE_OPERAND (t, ix));
6118 : : }
6119 : : else
6120 : : /* The CODE_CONTAINS tables were inaccurate when I started. */
6121 : 35670596 : gcc_checking_assert (TREE_CODE_CLASS (code) != tcc_expression
6122 : : && TREE_CODE_CLASS (code) != tcc_binary
6123 : : && TREE_CODE_CLASS (code) != tcc_unary
6124 : : && TREE_CODE_CLASS (code) != tcc_reference
6125 : : && TREE_CODE_CLASS (code) != tcc_comparison
6126 : : && TREE_CODE_CLASS (code) != tcc_statement
6127 : : && TREE_CODE_CLASS (code) != tcc_vl_exp);
6128 : :
6129 : : /* Then by CODE. Special cases and/or 1:1 tree shape
6130 : : correspondance. */
6131 : 49658684 : switch (code)
6132 : : {
6133 : : default:
6134 : : break;
6135 : :
6136 : 0 : case ARGUMENT_PACK_SELECT: /* Transient during instantiation. */
6137 : 0 : case DEFERRED_PARSE: /* Expanded upon completion of
6138 : : outermost class. */
6139 : 0 : case IDENTIFIER_NODE: /* Streamed specially. */
6140 : 0 : case BINDING_VECTOR: /* Only in namespace-scope symbol
6141 : : table. */
6142 : 0 : case SSA_NAME:
6143 : 0 : case TRANSLATION_UNIT_DECL: /* There is only one, it is a
6144 : : global_tree. */
6145 : 0 : case USERDEF_LITERAL: /* Expanded during parsing. */
6146 : 0 : gcc_unreachable (); /* Should never meet. */
6147 : :
6148 : : /* Constants. */
6149 : 18 : case COMPLEX_CST:
6150 : 18 : WT (TREE_REALPART (t));
6151 : 18 : WT (TREE_IMAGPART (t));
6152 : 18 : break;
6153 : :
6154 : 0 : case FIXED_CST:
6155 : 0 : gcc_unreachable (); /* Not supported in C++. */
6156 : :
6157 : 4632521 : case INTEGER_CST:
6158 : 4632521 : if (streaming_p ())
6159 : : {
6160 : 744335 : unsigned num = TREE_INT_CST_EXT_NUNITS (t);
6161 : 1491059 : for (unsigned ix = 0; ix != num; ix++)
6162 : 746724 : wu (TREE_INT_CST_ELT (t, ix));
6163 : : }
6164 : : break;
6165 : :
6166 : 0 : case POLY_INT_CST:
6167 : 0 : if (streaming_p ())
6168 : 0 : for (unsigned ix = 0; ix != NUM_POLY_INT_COEFFS; ix++)
6169 : 0 : WT (POLY_INT_CST_COEFF (t, ix));
6170 : : break;
6171 : :
6172 : 52795 : case REAL_CST:
6173 : 52795 : if (streaming_p ())
6174 : 26339 : buf (TREE_REAL_CST_PTR (t), sizeof (real_value));
6175 : : break;
6176 : :
6177 : : case STRING_CST:
6178 : : /* Streamed during start. */
6179 : : break;
6180 : :
6181 : 36 : case VECTOR_CST:
6182 : 102 : for (unsigned ix = vector_cst_encoded_nelts (t); ix--;)
6183 : 66 : WT (VECTOR_CST_ENCODED_ELT (t, ix));
6184 : : break;
6185 : :
6186 : : /* Decls. */
6187 : 532339 : case VAR_DECL:
6188 : 532339 : if (DECL_CONTEXT (t)
6189 : 532339 : && TREE_CODE (DECL_CONTEXT (t)) != FUNCTION_DECL)
6190 : : break;
6191 : : /* FALLTHROUGH */
6192 : :
6193 : 5165787 : case RESULT_DECL:
6194 : 5165787 : case PARM_DECL:
6195 : 5165787 : if (DECL_HAS_VALUE_EXPR_P (t))
6196 : 24749 : WT (DECL_VALUE_EXPR (t));
6197 : : /* FALLTHROUGH */
6198 : :
6199 : 5387245 : case CONST_DECL:
6200 : 5387245 : case IMPORTED_DECL:
6201 : 5387245 : WT (t->decl_common.initial);
6202 : 5387245 : break;
6203 : :
6204 : 160128 : case FIELD_DECL:
6205 : 160128 : WT (t->field_decl.offset);
6206 : 160128 : WT (t->field_decl.bit_field_type);
6207 : 160128 : WT (t->field_decl.qualifier); /* bitfield unit. */
6208 : 160128 : WT (t->field_decl.bit_offset);
6209 : 160128 : WT (t->field_decl.fcontext);
6210 : 160128 : WT (t->decl_common.initial);
6211 : 160128 : break;
6212 : :
6213 : 42268 : case LABEL_DECL:
6214 : 42268 : if (streaming_p ())
6215 : : {
6216 : 21134 : WU (t->label_decl.label_decl_uid);
6217 : 21134 : WU (t->label_decl.eh_landing_pad_nr);
6218 : : }
6219 : : break;
6220 : :
6221 : 1243041 : case FUNCTION_DECL:
6222 : 1243041 : if (streaming_p ())
6223 : : {
6224 : : /* Builtins can be streamed by value when a header declares
6225 : : them. */
6226 : 621510 : WU (DECL_BUILT_IN_CLASS (t));
6227 : 621510 : if (DECL_BUILT_IN_CLASS (t) != NOT_BUILT_IN)
6228 : 13661 : WU (DECL_UNCHECKED_FUNCTION_CODE (t));
6229 : : }
6230 : :
6231 : 1243041 : WT (t->function_decl.personality);
6232 : 1243041 : WT (t->function_decl.function_specific_target);
6233 : 1243041 : WT (t->function_decl.function_specific_optimization);
6234 : 1243041 : WT (t->function_decl.vindex);
6235 : :
6236 : 1243041 : if (DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (t))
6237 : 5212 : WT (lookup_explicit_specifier (t));
6238 : : break;
6239 : :
6240 : 127088 : case USING_DECL:
6241 : : /* USING_DECL_DECLS */
6242 : 127088 : WT (t->decl_common.initial);
6243 : : /* FALLTHROUGH */
6244 : :
6245 : 3705181 : case TYPE_DECL:
6246 : : /* USING_DECL: USING_DECL_SCOPE */
6247 : : /* TYPE_DECL: DECL_ORIGINAL_TYPE */
6248 : 3705181 : WT (t->decl_non_common.result);
6249 : 3705181 : break;
6250 : :
6251 : : /* Miscellaneous common nodes. */
6252 : 748923 : case BLOCK:
6253 : 748923 : if (state)
6254 : : {
6255 : 748923 : state->write_location (*this, t->block.locus);
6256 : 748923 : state->write_location (*this, t->block.end_locus);
6257 : : }
6258 : :
6259 : : /* DECL_LOCAL_DECL_P decls are first encountered here and
6260 : : streamed by value. */
6261 : 1119539 : for (tree decls = t->block.vars; decls; decls = DECL_CHAIN (decls))
6262 : : {
6263 : 370616 : if (VAR_OR_FUNCTION_DECL_P (decls)
6264 : 370616 : && DECL_LOCAL_DECL_P (decls))
6265 : : {
6266 : : /* Make sure this is the first encounter, and mark for
6267 : : walk-by-value. */
6268 : 272 : gcc_checking_assert (!TREE_VISITED (decls)
6269 : : && !DECL_TEMPLATE_INFO (decls));
6270 : 272 : mark_by_value (decls);
6271 : : }
6272 : 370616 : tree_node (decls);
6273 : : }
6274 : 748923 : tree_node (NULL_TREE);
6275 : :
6276 : : /* nonlocalized_vars is a middle-end thing. */
6277 : 748923 : WT (t->block.subblocks);
6278 : 748923 : WT (t->block.supercontext);
6279 : : // FIXME: As for decl's abstract_origin, does this introduce crosslinks?
6280 : 748923 : WT (t->block.abstract_origin);
6281 : : /* fragment_origin, fragment_chain are middle-end things. */
6282 : 748923 : WT (t->block.chain);
6283 : : /* nonlocalized_vars, block_num & die are middle endy/debug
6284 : : things. */
6285 : 748923 : break;
6286 : :
6287 : 1429859 : case CALL_EXPR:
6288 : 1429859 : if (streaming_p ())
6289 : 688366 : WU (t->base.u.ifn);
6290 : : break;
6291 : :
6292 : : case CONSTRUCTOR:
6293 : : // This must be streamed /after/ we've streamed the type,
6294 : : // because it can directly refer to elements of the type. Eg,
6295 : : // FIELD_DECLs of a RECORD_TYPE.
6296 : : break;
6297 : :
6298 : 6 : case OMP_CLAUSE:
6299 : 6 : {
6300 : : /* The ompcode is serialized in start. */
6301 : 6 : if (streaming_p ())
6302 : 3 : WU (t->omp_clause.subcode.map_kind);
6303 : 6 : if (state)
6304 : 6 : state->write_location (*this, t->omp_clause.locus);
6305 : :
6306 : 6 : unsigned len = omp_clause_num_ops[OMP_CLAUSE_CODE (t)];
6307 : 18 : for (unsigned ix = 0; ix != len; ix++)
6308 : 12 : WT (t->omp_clause.ops[ix]);
6309 : : }
6310 : : break;
6311 : :
6312 : 509410 : case STATEMENT_LIST:
6313 : 2020777 : for (tree stmt : tsi_range (t))
6314 : 1511367 : if (stmt)
6315 : 1511367 : WT (stmt);
6316 : 509410 : WT (NULL_TREE);
6317 : 509410 : break;
6318 : :
6319 : 0 : case OPTIMIZATION_NODE:
6320 : 0 : case TARGET_OPTION_NODE:
6321 : : // FIXME: Our representation for these two nodes is a cache of
6322 : : // the resulting set of options. Not a record of the options
6323 : : // that got changed by a particular attribute or pragma. Should
6324 : : // we record that, or should we record the diff from the command
6325 : : // line options? The latter seems the right behaviour, but is
6326 : : // (a) harder, and I guess could introduce strangeness if the
6327 : : // importer has set some incompatible set of optimization flags?
6328 : 0 : gcc_unreachable ();
6329 : 321684 : break;
6330 : :
6331 : 321684 : case TREE_BINFO:
6332 : 321684 : {
6333 : 321684 : WT (t->binfo.common.chain);
6334 : 321684 : WT (t->binfo.offset);
6335 : 321684 : WT (t->binfo.inheritance);
6336 : 321684 : WT (t->binfo.vptr_field);
6337 : :
6338 : 321684 : WT (t->binfo.vtable);
6339 : 321684 : WT (t->binfo.virtuals);
6340 : 321684 : WT (t->binfo.vtt_subvtt);
6341 : 321684 : WT (t->binfo.vtt_vptr);
6342 : :
6343 : 321684 : tree_vec (BINFO_BASE_ACCESSES (t));
6344 : 321684 : unsigned num = vec_safe_length (BINFO_BASE_ACCESSES (t));
6345 : 420554 : for (unsigned ix = 0; ix != num; ix++)
6346 : 98870 : WT (BINFO_BASE_BINFO (t, ix));
6347 : : }
6348 : : break;
6349 : :
6350 : 3951446 : case TREE_LIST:
6351 : 3951446 : WT (t->list.purpose);
6352 : 3951446 : WT (t->list.value);
6353 : 3951446 : WT (t->list.common.chain);
6354 : 3951446 : break;
6355 : :
6356 : 3270787 : case TREE_VEC:
6357 : 9252021 : for (unsigned ix = TREE_VEC_LENGTH (t); ix--;)
6358 : 5981234 : WT (TREE_VEC_ELT (t, ix));
6359 : : /* We stash NON_DEFAULT_TEMPLATE_ARGS_COUNT on TREE_CHAIN! */
6360 : 3270787 : gcc_checking_assert (!t->type_common.common.chain
6361 : : || (TREE_CODE (t->type_common.common.chain)
6362 : : == INTEGER_CST));
6363 : 3270787 : WT (t->type_common.common.chain);
6364 : 3270787 : break;
6365 : :
6366 : : /* C++-specific nodes ... */
6367 : 254712 : case BASELINK:
6368 : 254712 : WT (((lang_tree_node *)t)->baselink.binfo);
6369 : 254712 : WT (((lang_tree_node *)t)->baselink.functions);
6370 : 254712 : WT (((lang_tree_node *)t)->baselink.access_binfo);
6371 : 254712 : break;
6372 : :
6373 : 66548 : case CONSTRAINT_INFO:
6374 : 66548 : WT (((lang_tree_node *)t)->constraint_info.template_reqs);
6375 : 66548 : WT (((lang_tree_node *)t)->constraint_info.declarator_reqs);
6376 : 66548 : WT (((lang_tree_node *)t)->constraint_info.associated_constr);
6377 : 66548 : break;
6378 : :
6379 : 15473 : case DEFERRED_NOEXCEPT:
6380 : 15473 : WT (((lang_tree_node *)t)->deferred_noexcept.pattern);
6381 : 15473 : WT (((lang_tree_node *)t)->deferred_noexcept.args);
6382 : 15473 : break;
6383 : :
6384 : 12831 : case LAMBDA_EXPR:
6385 : 12831 : WT (((lang_tree_node *)t)->lambda_expression.capture_list);
6386 : 12831 : WT (((lang_tree_node *)t)->lambda_expression.this_capture);
6387 : 12831 : WT (((lang_tree_node *)t)->lambda_expression.extra_scope);
6388 : 12831 : WT (((lang_tree_node *)t)->lambda_expression.regen_info);
6389 : 12831 : WT (((lang_tree_node *)t)->lambda_expression.extra_args);
6390 : : /* pending_proxies is a parse-time thing. */
6391 : 12831 : gcc_assert (!((lang_tree_node *)t)->lambda_expression.pending_proxies);
6392 : 12831 : if (state)
6393 : 12831 : state->write_location
6394 : 12831 : (*this, ((lang_tree_node *)t)->lambda_expression.locus);
6395 : 12831 : if (streaming_p ())
6396 : : {
6397 : 4350 : WU (((lang_tree_node *)t)->lambda_expression.default_capture_mode);
6398 : 4350 : WU (((lang_tree_node *)t)->lambda_expression.discriminator_scope);
6399 : 4350 : WU (((lang_tree_node *)t)->lambda_expression.discriminator_sig);
6400 : : }
6401 : : break;
6402 : :
6403 : 2502354 : case OVERLOAD:
6404 : 2502354 : WT (((lang_tree_node *)t)->overload.function);
6405 : 2502354 : WT (t->common.chain);
6406 : 2502354 : break;
6407 : :
6408 : 0 : case PTRMEM_CST:
6409 : 0 : WT (((lang_tree_node *)t)->ptrmem.member);
6410 : 0 : break;
6411 : :
6412 : 19847 : case STATIC_ASSERT:
6413 : 19847 : WT (((lang_tree_node *)t)->static_assertion.condition);
6414 : 19847 : WT (((lang_tree_node *)t)->static_assertion.message);
6415 : 19847 : if (state)
6416 : 19847 : state->write_location
6417 : 19847 : (*this, ((lang_tree_node *)t)->static_assertion.location);
6418 : : break;
6419 : :
6420 : 916078 : case TEMPLATE_DECL:
6421 : : /* Streamed with the template_decl node itself. */
6422 : 916078 : gcc_checking_assert
6423 : : (TREE_VISITED (((lang_tree_node *)t)->template_decl.arguments));
6424 : 916078 : gcc_checking_assert
6425 : : (TREE_VISITED (((lang_tree_node *)t)->template_decl.result));
6426 : 916078 : if (DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (t))
6427 : 11976 : WT (DECL_CHAIN (t));
6428 : : break;
6429 : :
6430 : 2080814 : case TEMPLATE_INFO:
6431 : 2080814 : {
6432 : 2080814 : WT (((lang_tree_node *)t)->template_info.tmpl);
6433 : 2080814 : WT (((lang_tree_node *)t)->template_info.args);
6434 : 2080814 : WT (((lang_tree_node *)t)->template_info.partial);
6435 : :
6436 : 2080814 : const auto *ac = (((lang_tree_node *)t)
6437 : : ->template_info.deferred_access_checks);
6438 : 2080814 : unsigned len = vec_safe_length (ac);
6439 : 2080814 : if (streaming_p ())
6440 : 1038529 : u (len);
6441 : 2080814 : if (len)
6442 : : {
6443 : 0 : for (unsigned ix = 0; ix != len; ix++)
6444 : : {
6445 : 0 : const auto &m = (*ac)[ix];
6446 : 0 : WT (m.binfo);
6447 : 0 : WT (m.decl);
6448 : 0 : WT (m.diag_decl);
6449 : 0 : if (state)
6450 : 0 : state->write_location (*this, m.loc);
6451 : : }
6452 : : }
6453 : : }
6454 : : break;
6455 : :
6456 : 2549156 : case TEMPLATE_PARM_INDEX:
6457 : 2549156 : if (streaming_p ())
6458 : : {
6459 : 566793 : WU (((lang_tree_node *)t)->tpi.index);
6460 : 566793 : WU (((lang_tree_node *)t)->tpi.level);
6461 : 566793 : WU (((lang_tree_node *)t)->tpi.orig_level);
6462 : : }
6463 : 2549156 : WT (((lang_tree_node *)t)->tpi.decl);
6464 : : /* TEMPLATE_PARM_DESCENDANTS (AKA TREE_CHAIN) is an internal
6465 : : cache, do not stream. */
6466 : 2549156 : break;
6467 : :
6468 : 31487 : case TRAIT_EXPR:
6469 : 31487 : WT (((lang_tree_node *)t)->trait_expression.type1);
6470 : 31487 : WT (((lang_tree_node *)t)->trait_expression.type2);
6471 : 31487 : if (streaming_p ())
6472 : 12108 : WU (((lang_tree_node *)t)->trait_expression.kind);
6473 : : break;
6474 : : }
6475 : :
6476 : 49658684 : if (CODE_CONTAINS_STRUCT (code, TS_TYPED))
6477 : : {
6478 : : /* We want to stream the type of a expression-like nodes /after/
6479 : : we've streamed the operands. The type often contains (bits
6480 : : of the) types of the operands, and with things like decltype
6481 : : and noexcept in play, we really want to stream the decls
6482 : : defining the type before we try and stream the type on its
6483 : : own. Otherwise we can find ourselves trying to read in a
6484 : : decl, when we're already partially reading in a component of
6485 : : its type. And that's bad. */
6486 : 46727079 : tree type = t->typed.type;
6487 : 46727079 : unsigned prec = 0;
6488 : :
6489 : 46727079 : switch (code)
6490 : : {
6491 : : default:
6492 : : break;
6493 : :
6494 : : case TEMPLATE_DECL:
6495 : : /* We fill in the template's type separately. */
6496 : 46727079 : type = NULL_TREE;
6497 : : break;
6498 : :
6499 : 3578093 : case TYPE_DECL:
6500 : 3578093 : if (DECL_ORIGINAL_TYPE (t) && t == TYPE_NAME (type))
6501 : : /* This is a typedef. We set its type separately. */
6502 : : type = NULL_TREE;
6503 : : break;
6504 : :
6505 : 11432 : case ENUMERAL_TYPE:
6506 : 11432 : if (type && !ENUM_FIXED_UNDERLYING_TYPE_P (t))
6507 : : {
6508 : : /* Type is a restricted range integer type derived from the
6509 : : integer_types. Find the right one. */
6510 : 7536 : prec = TYPE_PRECISION (type);
6511 : 7536 : tree name = DECL_NAME (TYPE_NAME (type));
6512 : :
6513 : 98406 : for (unsigned itk = itk_none; itk--;)
6514 : 98406 : if (integer_types[itk]
6515 : 98406 : && DECL_NAME (TYPE_NAME (integer_types[itk])) == name)
6516 : : {
6517 : : type = integer_types[itk];
6518 : : break;
6519 : : }
6520 : 7536 : gcc_assert (type != t->typed.type);
6521 : : }
6522 : : break;
6523 : : }
6524 : :
6525 : 46727079 : WT (type);
6526 : 46727079 : if (prec && streaming_p ())
6527 : 3768 : WU (prec);
6528 : : }
6529 : :
6530 : 49658684 : if (TREE_CODE (t) == CONSTRUCTOR)
6531 : : {
6532 : 113760 : unsigned len = vec_safe_length (t->constructor.elts);
6533 : 113760 : if (streaming_p ())
6534 : 55947 : WU (len);
6535 : 113760 : if (len)
6536 : 259966 : for (unsigned ix = 0; ix != len; ix++)
6537 : : {
6538 : 223934 : const constructor_elt &elt = (*t->constructor.elts)[ix];
6539 : :
6540 : 223934 : WT (elt.index);
6541 : 223934 : WT (elt.value);
6542 : : }
6543 : : }
6544 : :
6545 : : #undef WT
6546 : : #undef WU
6547 : 49658684 : }
6548 : :
6549 : : // Streaming in a reference to a decl can cause that decl to be
6550 : : // TREE_USED, which is the mark_used behaviour we need most of the
6551 : : // time. The trees_in::unused can be incremented to inhibit this,
6552 : : // which is at least needed for vtables.
6553 : :
6554 : : bool
6555 : 20543587 : trees_in::core_vals (tree t)
6556 : : {
6557 : : #define RU(X) ((X) = u ())
6558 : : #define RUC(T,X) ((X) = T (u ()))
6559 : : #define RT(X) ((X) = tree_node ())
6560 : : #define RTU(X) ((X) = tree_node (true))
6561 : 20543587 : tree_code code = TREE_CODE (t);
6562 : :
6563 : : /* First by tree shape. */
6564 : 20543587 : if (CODE_CONTAINS_STRUCT (code, TS_DECL_MINIMAL))
6565 : : {
6566 : 4775133 : RT (t->decl_minimal.name);
6567 : 4775133 : if (!DECL_TEMPLATE_PARM_P (t))
6568 : 4157431 : RT (t->decl_minimal.context);
6569 : :
6570 : : /* Don't zap the locus just yet, we don't record it correctly
6571 : : and thus lose all location information. */
6572 : 4775133 : t->decl_minimal.locus = state->read_location (*this);
6573 : : }
6574 : :
6575 : 20543587 : if (CODE_CONTAINS_STRUCT (code, TS_TYPE_COMMON))
6576 : : {
6577 : 821818 : RT (t->type_common.name);
6578 : 821818 : RT (t->type_common.context);
6579 : :
6580 : 821818 : RT (t->type_common.main_variant);
6581 : 821818 : RT (t->type_common.canonical);
6582 : :
6583 : : /* type_common.next_variant is internally manipulated. */
6584 : : /* type_common.pointer_to, type_common.reference_to. */
6585 : :
6586 : 821818 : RU (t->type_common.precision);
6587 : 821818 : RU (t->type_common.contains_placeholder_bits);
6588 : 821818 : RUC (machine_mode, t->type_common.mode);
6589 : 821818 : RU (t->type_common.align);
6590 : :
6591 : 821818 : if (!RECORD_OR_UNION_CODE_P (code))
6592 : : {
6593 : 553318 : RT (t->type_common.size);
6594 : 553318 : RT (t->type_common.size_unit);
6595 : : }
6596 : 821818 : RT (t->type_common.attributes);
6597 : :
6598 : 821818 : RT (t->type_common.common.chain); /* TYPE_STUB_DECL. */
6599 : : }
6600 : :
6601 : 20543587 : if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
6602 : : {
6603 : 4775133 : RUC (machine_mode, t->decl_common.mode);
6604 : 4775133 : RU (t->decl_common.off_align);
6605 : 4775133 : RU (t->decl_common.align);
6606 : :
6607 : 4775133 : if (code != TEMPLATE_DECL)
6608 : : {
6609 : 4295709 : RT (t->decl_common.size);
6610 : 4295709 : RT (t->decl_common.size_unit);
6611 : : }
6612 : :
6613 : 4775133 : RT (t->decl_common.attributes);
6614 : 4775133 : RT (t->decl_common.abstract_origin);
6615 : : }
6616 : :
6617 : 20543587 : if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
6618 : : {
6619 : 2203968 : RT (t->decl_with_vis.assembler_name);
6620 : 2203968 : RUC (symbol_visibility, t->decl_with_vis.visibility);
6621 : : }
6622 : :
6623 : 20543587 : if (CODE_CONTAINS_STRUCT (code, TS_TYPE_NON_COMMON))
6624 : : {
6625 : 821818 : if (code == ENUMERAL_TYPE)
6626 : : {
6627 : : /* These fields get set even for opaque enums that lack a
6628 : : definition, so we stream them directly for each ENUMERAL_TYPE.
6629 : : We stream TYPE_VALUES as part of the definition. */
6630 : 5154 : RT (t->type_non_common.maxval);
6631 : 5154 : RT (t->type_non_common.minval);
6632 : : }
6633 : : /* Records and unions hold FIELDS, VFIELD & BINFO on these
6634 : : things. */
6635 : 816664 : else if (!RECORD_OR_UNION_CODE_P (code))
6636 : : {
6637 : : /* This is not clobbering TYPE_CACHED_VALUES, because this
6638 : : is a type that doesn't have any. */
6639 : 548164 : gcc_checking_assert (!TYPE_CACHED_VALUES_P (t));
6640 : 548164 : RT (t->type_non_common.values);
6641 : 548164 : RT (t->type_non_common.maxval);
6642 : 548164 : RT (t->type_non_common.minval);
6643 : : }
6644 : :
6645 : 821818 : RT (t->type_non_common.lang_1);
6646 : : }
6647 : :
6648 : 20543587 : if (CODE_CONTAINS_STRUCT (code, TS_EXP))
6649 : : {
6650 : 7539197 : t->exp.locus = state->read_location (*this);
6651 : :
6652 : 7539197 : bool vl = TREE_CODE_CLASS (code) == tcc_vl_exp;
6653 : 14304406 : for (unsigned limit = (vl ? VL_EXP_OPERAND_LENGTH (t)
6654 : 6765209 : : TREE_OPERAND_LENGTH (t)),
6655 : 20857597 : ix = unsigned (vl); ix != limit; ix++)
6656 : 13318400 : RTU (TREE_OPERAND (t, ix));
6657 : : }
6658 : :
6659 : : /* Then by CODE. Special cases and/or 1:1 tree shape
6660 : : correspondance. */
6661 : 20543587 : switch (code)
6662 : : {
6663 : : default:
6664 : : break;
6665 : :
6666 : : case ARGUMENT_PACK_SELECT:
6667 : : case DEFERRED_PARSE:
6668 : : case IDENTIFIER_NODE:
6669 : : case BINDING_VECTOR:
6670 : : case SSA_NAME:
6671 : : case TRANSLATION_UNIT_DECL:
6672 : : case USERDEF_LITERAL:
6673 : : return false; /* Should never meet. */
6674 : :
6675 : : /* Constants. */
6676 : 9 : case COMPLEX_CST:
6677 : 9 : RT (TREE_REALPART (t));
6678 : 9 : RT (TREE_IMAGPART (t));
6679 : 9 : break;
6680 : :
6681 : : case FIXED_CST:
6682 : : /* Not suported in C++. */
6683 : : return false;
6684 : :
6685 : 765504 : case INTEGER_CST:
6686 : 765504 : {
6687 : 765504 : unsigned num = TREE_INT_CST_EXT_NUNITS (t);
6688 : 1533528 : for (unsigned ix = 0; ix != num; ix++)
6689 : 768024 : TREE_INT_CST_ELT (t, ix) = wu ();
6690 : : }
6691 : : break;
6692 : :
6693 : : case POLY_INT_CST:
6694 : 0 : for (unsigned ix = 0; ix != NUM_POLY_INT_COEFFS; ix++)
6695 : 0 : RT (POLY_INT_CST_COEFF (t, ix));
6696 : : break;
6697 : :
6698 : 35439 : case REAL_CST:
6699 : 35439 : if (const void *bytes = buf (sizeof (real_value)))
6700 : 35439 : memcpy (TREE_REAL_CST_PTR (t), bytes, sizeof (real_value));
6701 : : break;
6702 : :
6703 : : case STRING_CST:
6704 : : /* Streamed during start. */
6705 : : break;
6706 : :
6707 : 24 : case VECTOR_CST:
6708 : 63 : for (unsigned ix = vector_cst_encoded_nelts (t); ix--;)
6709 : 39 : RT (VECTOR_CST_ENCODED_ELT (t, ix));
6710 : : break;
6711 : :
6712 : : /* Decls. */
6713 : 293120 : case VAR_DECL:
6714 : 293120 : if (DECL_CONTEXT (t)
6715 : 293120 : && TREE_CODE (DECL_CONTEXT (t)) != FUNCTION_DECL)
6716 : : break;
6717 : : /* FALLTHROUGH */
6718 : :
6719 : 2147718 : case RESULT_DECL:
6720 : 2147718 : case PARM_DECL:
6721 : 2147718 : if (DECL_HAS_VALUE_EXPR_P (t))
6722 : : {
6723 : : /* The DECL_VALUE hash table is a cache, thus if we're
6724 : : reading a duplicate (which we end up discarding), the
6725 : : value expr will also be cleaned up at the next gc. */
6726 : 11575 : tree val = tree_node ();
6727 : 11575 : SET_DECL_VALUE_EXPR (t, val);
6728 : : }
6729 : : /* FALLTHROUGH */
6730 : :
6731 : 2209702 : case CONST_DECL:
6732 : 2209702 : case IMPORTED_DECL:
6733 : 2209702 : RT (t->decl_common.initial);
6734 : 2209702 : break;
6735 : :
6736 : 85386 : case FIELD_DECL:
6737 : 85386 : RT (t->field_decl.offset);
6738 : 85386 : RT (t->field_decl.bit_field_type);
6739 : 85386 : RT (t->field_decl.qualifier);
6740 : 85386 : RT (t->field_decl.bit_offset);
6741 : 85386 : RT (t->field_decl.fcontext);
6742 : 85386 : RT (t->decl_common.initial);
6743 : 85386 : break;
6744 : :
6745 : 22839 : case LABEL_DECL:
6746 : 22839 : RU (t->label_decl.label_decl_uid);
6747 : 22839 : RU (t->label_decl.eh_landing_pad_nr);
6748 : 22839 : break;
6749 : :
6750 : 689451 : case FUNCTION_DECL:
6751 : 689451 : {
6752 : 689451 : unsigned bltin = u ();
6753 : 689451 : t->function_decl.built_in_class = built_in_class (bltin);
6754 : 689451 : if (bltin != NOT_BUILT_IN)
6755 : : {
6756 : 16139 : bltin = u ();
6757 : 16139 : DECL_UNCHECKED_FUNCTION_CODE (t) = built_in_function (bltin);
6758 : : }
6759 : :
6760 : 689451 : RT (t->function_decl.personality);
6761 : 689451 : RT (t->function_decl.function_specific_target);
6762 : 689451 : RT (t->function_decl.function_specific_optimization);
6763 : 689451 : RT (t->function_decl.vindex);
6764 : :
6765 : 689451 : if (DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (t))
6766 : : {
6767 : 2776 : tree spec;
6768 : 2776 : RT (spec);
6769 : 2776 : store_explicit_specifier (t, spec);
6770 : : }
6771 : : }
6772 : : break;
6773 : :
6774 : 61515 : case USING_DECL:
6775 : : /* USING_DECL_DECLS */
6776 : 61515 : RT (t->decl_common.initial);
6777 : : /* FALLTHROUGH */
6778 : :
6779 : 1221199 : case TYPE_DECL:
6780 : : /* USING_DECL: USING_DECL_SCOPE */
6781 : : /* TYPE_DECL: DECL_ORIGINAL_TYPE */
6782 : 1221199 : RT (t->decl_non_common.result);
6783 : 1221199 : break;
6784 : :
6785 : : /* Miscellaneous common nodes. */
6786 : 416671 : case BLOCK:
6787 : 416671 : t->block.locus = state->read_location (*this);
6788 : 416671 : t->block.end_locus = state->read_location (*this);
6789 : :
6790 : 416671 : for (tree *chain = &t->block.vars;;)
6791 : 632266 : if (tree decl = tree_node ())
6792 : : {
6793 : : /* For a deduplicated local type or enumerator, chain the
6794 : : duplicate decl instead of the canonical in-TU decl. Seeing
6795 : : a duplicate here means the containing function whose body
6796 : : we're streaming in is a duplicate too, so we'll end up
6797 : : discarding this BLOCK (and the rest of the duplicate function
6798 : : body) anyway. */
6799 : 215595 : decl = maybe_duplicate (decl);
6800 : :
6801 : 215595 : if (!DECL_P (decl) || DECL_CHAIN (decl))
6802 : : {
6803 : 0 : set_overrun ();
6804 : 0 : break;
6805 : : }
6806 : 215595 : *chain = decl;
6807 : 215595 : chain = &DECL_CHAIN (decl);
6808 : : }
6809 : : else
6810 : 215595 : break;
6811 : :
6812 : : /* nonlocalized_vars is middle-end. */
6813 : 416671 : RT (t->block.subblocks);
6814 : 416671 : RT (t->block.supercontext);
6815 : 416671 : RT (t->block.abstract_origin);
6816 : : /* fragment_origin, fragment_chain are middle-end. */
6817 : 416671 : RT (t->block.chain);
6818 : : /* nonlocalized_vars, block_num, die are middle endy/debug
6819 : : things. */
6820 : 416671 : break;
6821 : :
6822 : 758497 : case CALL_EXPR:
6823 : 758497 : RUC (internal_fn, t->base.u.ifn);
6824 : 758497 : break;
6825 : :
6826 : : case CONSTRUCTOR:
6827 : : // Streamed after the node's type.
6828 : : break;
6829 : :
6830 : 3 : case OMP_CLAUSE:
6831 : 3 : {
6832 : 3 : RU (t->omp_clause.subcode.map_kind);
6833 : 3 : t->omp_clause.locus = state->read_location (*this);
6834 : :
6835 : 3 : unsigned len = omp_clause_num_ops[OMP_CLAUSE_CODE (t)];
6836 : 9 : for (unsigned ix = 0; ix != len; ix++)
6837 : 6 : RT (t->omp_clause.ops[ix]);
6838 : : }
6839 : : break;
6840 : :
6841 : 280681 : case STATEMENT_LIST:
6842 : 280681 : {
6843 : 280681 : tree_stmt_iterator iter = tsi_start (t);
6844 : 1126146 : for (tree stmt; RT (stmt);)
6845 : 845465 : tsi_link_after (&iter, stmt, TSI_CONTINUE_LINKING);
6846 : : }
6847 : 280681 : break;
6848 : :
6849 : 0 : case OPTIMIZATION_NODE:
6850 : 0 : case TARGET_OPTION_NODE:
6851 : : /* Not yet implemented, see trees_out::core_vals. */
6852 : 0 : gcc_unreachable ();
6853 : 156657 : break;
6854 : :
6855 : 156657 : case TREE_BINFO:
6856 : 156657 : RT (t->binfo.common.chain);
6857 : 156657 : RT (t->binfo.offset);
6858 : 156657 : RT (t->binfo.inheritance);
6859 : 156657 : RT (t->binfo.vptr_field);
6860 : :
6861 : : /* Do not mark the vtables as USED in the address expressions
6862 : : here. */
6863 : 156657 : unused++;
6864 : 156657 : RT (t->binfo.vtable);
6865 : 156657 : RT (t->binfo.virtuals);
6866 : 156657 : RT (t->binfo.vtt_subvtt);
6867 : 156657 : RT (t->binfo.vtt_vptr);
6868 : 156657 : unused--;
6869 : :
6870 : 156657 : BINFO_BASE_ACCESSES (t) = tree_vec ();
6871 : 156657 : if (!get_overrun ())
6872 : : {
6873 : 156657 : unsigned num = vec_safe_length (BINFO_BASE_ACCESSES (t));
6874 : 205341 : for (unsigned ix = 0; ix != num; ix++)
6875 : 48684 : BINFO_BASE_APPEND (t, tree_node ());
6876 : : }
6877 : : break;
6878 : :
6879 : 1877875 : case TREE_LIST:
6880 : 1877875 : RT (t->list.purpose);
6881 : 1877875 : RT (t->list.value);
6882 : 1877875 : RT (t->list.common.chain);
6883 : 1877875 : break;
6884 : :
6885 : 1114949 : case TREE_VEC:
6886 : 3091181 : for (unsigned ix = TREE_VEC_LENGTH (t); ix--;)
6887 : 1976232 : RT (TREE_VEC_ELT (t, ix));
6888 : 1114949 : RT (t->type_common.common.chain);
6889 : 1114949 : break;
6890 : :
6891 : : /* C++-specific nodes ... */
6892 : 131224 : case BASELINK:
6893 : 131224 : RT (((lang_tree_node *)t)->baselink.binfo);
6894 : 131224 : RTU (((lang_tree_node *)t)->baselink.functions);
6895 : 131224 : RT (((lang_tree_node *)t)->baselink.access_binfo);
6896 : 131224 : break;
6897 : :
6898 : 27934 : case CONSTRAINT_INFO:
6899 : 27934 : RT (((lang_tree_node *)t)->constraint_info.template_reqs);
6900 : 27934 : RT (((lang_tree_node *)t)->constraint_info.declarator_reqs);
6901 : 27934 : RT (((lang_tree_node *)t)->constraint_info.associated_constr);
6902 : 27934 : break;
6903 : :
6904 : 8377 : case DEFERRED_NOEXCEPT:
6905 : 8377 : RT (((lang_tree_node *)t)->deferred_noexcept.pattern);
6906 : 8377 : RT (((lang_tree_node *)t)->deferred_noexcept.args);
6907 : 8377 : break;
6908 : :
6909 : 4763 : case LAMBDA_EXPR:
6910 : 4763 : RT (((lang_tree_node *)t)->lambda_expression.capture_list);
6911 : 4763 : RT (((lang_tree_node *)t)->lambda_expression.this_capture);
6912 : 4763 : RT (((lang_tree_node *)t)->lambda_expression.extra_scope);
6913 : 4763 : RT (((lang_tree_node *)t)->lambda_expression.regen_info);
6914 : 4763 : RT (((lang_tree_node *)t)->lambda_expression.extra_args);
6915 : : /* lambda_expression.pending_proxies is NULL */
6916 : 4763 : ((lang_tree_node *)t)->lambda_expression.locus
6917 : 4763 : = state->read_location (*this);
6918 : 4763 : RUC (cp_lambda_default_capture_mode_type,
6919 : : ((lang_tree_node *)t)->lambda_expression.default_capture_mode);
6920 : 4763 : RU (((lang_tree_node *)t)->lambda_expression.discriminator_scope);
6921 : 4763 : RU (((lang_tree_node *)t)->lambda_expression.discriminator_sig);
6922 : 4763 : break;
6923 : :
6924 : 798889 : case OVERLOAD:
6925 : 798889 : RT (((lang_tree_node *)t)->overload.function);
6926 : 798889 : RT (t->common.chain);
6927 : 798889 : break;
6928 : :
6929 : 0 : case PTRMEM_CST:
6930 : 0 : RT (((lang_tree_node *)t)->ptrmem.member);
6931 : 0 : break;
6932 : :
6933 : 10645 : case STATIC_ASSERT:
6934 : 10645 : RT (((lang_tree_node *)t)->static_assertion.condition);
6935 : 10645 : RT (((lang_tree_node *)t)->static_assertion.message);
6936 : 10645 : ((lang_tree_node *)t)->static_assertion.location
6937 : 10645 : = state->read_location (*this);
6938 : 10645 : break;
6939 : :
6940 : 479424 : case TEMPLATE_DECL:
6941 : : /* Streamed when reading the raw template decl itself. */
6942 : 479424 : gcc_assert (((lang_tree_node *)t)->template_decl.arguments);
6943 : 479424 : gcc_assert (((lang_tree_node *)t)->template_decl.result);
6944 : 479424 : if (DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (t))
6945 : 6701 : RT (DECL_CHAIN (t));
6946 : : break;
6947 : :
6948 : 1098246 : case TEMPLATE_INFO:
6949 : 1098246 : RT (((lang_tree_node *)t)->template_info.tmpl);
6950 : 1098246 : RT (((lang_tree_node *)t)->template_info.args);
6951 : 1098246 : RT (((lang_tree_node *)t)->template_info.partial);
6952 : 1098246 : if (unsigned len = u ())
6953 : : {
6954 : 0 : auto &ac = (((lang_tree_node *)t)
6955 : : ->template_info.deferred_access_checks);
6956 : 0 : vec_alloc (ac, len);
6957 : 0 : for (unsigned ix = 0; ix != len; ix++)
6958 : : {
6959 : 0 : deferred_access_check m;
6960 : :
6961 : 0 : RT (m.binfo);
6962 : 0 : RT (m.decl);
6963 : 0 : RT (m.diag_decl);
6964 : 0 : m.loc = state->read_location (*this);
6965 : 0 : ac->quick_push (m);
6966 : : }
6967 : : }
6968 : : break;
6969 : :
6970 : 588166 : case TEMPLATE_PARM_INDEX:
6971 : 588166 : RU (((lang_tree_node *)t)->tpi.index);
6972 : 588166 : RU (((lang_tree_node *)t)->tpi.level);
6973 : 588166 : RU (((lang_tree_node *)t)->tpi.orig_level);
6974 : 588166 : RT (((lang_tree_node *)t)->tpi.decl);
6975 : 588166 : break;
6976 : :
6977 : 11613 : case TRAIT_EXPR:
6978 : 11613 : RT (((lang_tree_node *)t)->trait_expression.type1);
6979 : 11613 : RT (((lang_tree_node *)t)->trait_expression.type2);
6980 : 11613 : RUC (cp_trait_kind, ((lang_tree_node *)t)->trait_expression.kind);
6981 : 11613 : break;
6982 : : }
6983 : :
6984 : 20543587 : if (CODE_CONTAINS_STRUCT (code, TS_TYPED))
6985 : : {
6986 : 18981714 : tree type = tree_node ();
6987 : :
6988 : 18981714 : if (type && code == ENUMERAL_TYPE && !ENUM_FIXED_UNDERLYING_TYPE_P (t))
6989 : : {
6990 : 3248 : unsigned precision = u ();
6991 : :
6992 : 3248 : type = build_distinct_type_copy (type);
6993 : 3248 : TYPE_PRECISION (type) = precision;
6994 : 6496 : set_min_and_max_values_for_integral_type (type, precision,
6995 : 3248 : TYPE_SIGN (type));
6996 : : }
6997 : :
6998 : 18981714 : if (code != TEMPLATE_DECL)
6999 : 18502290 : t->typed.type = type;
7000 : : }
7001 : :
7002 : 20543587 : if (TREE_CODE (t) == CONSTRUCTOR)
7003 : 59819 : if (unsigned len = u ())
7004 : : {
7005 : 19753 : vec_alloc (t->constructor.elts, len);
7006 : 136485 : for (unsigned ix = 0; ix != len; ix++)
7007 : : {
7008 : 116732 : constructor_elt elt;
7009 : :
7010 : 116732 : RT (elt.index);
7011 : 116732 : RTU (elt.value);
7012 : 116732 : t->constructor.elts->quick_push (elt);
7013 : : }
7014 : : }
7015 : :
7016 : : #undef RT
7017 : : #undef RM
7018 : : #undef RU
7019 : 20543587 : return !get_overrun ();
7020 : : }
7021 : :
7022 : : void
7023 : 6582251 : trees_out::lang_decl_vals (tree t)
7024 : : {
7025 : 6582251 : const struct lang_decl *lang = DECL_LANG_SPECIFIC (t);
7026 : : #define WU(X) (u (X))
7027 : : #define WT(X) (tree_node (X))
7028 : : /* Module index already written. */
7029 : 6582251 : switch (lang->u.base.selector)
7030 : : {
7031 : 0 : default:
7032 : 0 : gcc_unreachable ();
7033 : :
7034 : 1243041 : case lds_fn: /* lang_decl_fn. */
7035 : 1243041 : if (streaming_p ())
7036 : : {
7037 : 1134771 : if (DECL_NAME (t) && IDENTIFIER_OVL_OP_P (DECL_NAME (t)))
7038 : 104531 : WU (lang->u.fn.ovl_op_code);
7039 : : }
7040 : :
7041 : 1243041 : if (DECL_CLASS_SCOPE_P (t))
7042 : 899292 : WT (lang->u.fn.context);
7043 : :
7044 : 1243041 : if (lang->u.fn.thunk_p)
7045 : : {
7046 : : /* The thunked-to function. */
7047 : 2216 : WT (lang->u.fn.befriending_classes);
7048 : 2216 : if (streaming_p ())
7049 : 1108 : wi (lang->u.fn.u5.fixed_offset);
7050 : : }
7051 : 1240825 : else if (decl_tls_wrapper_p (t))
7052 : : /* The wrapped variable. */
7053 : 6 : WT (lang->u.fn.befriending_classes);
7054 : : else
7055 : 1240819 : WT (lang->u.fn.u5.cloned_function);
7056 : :
7057 : 1243041 : if (FNDECL_USED_AUTO (t))
7058 : 2245 : WT (lang->u.fn.u.saved_auto_return_type);
7059 : :
7060 : 1243041 : goto lds_min;
7061 : :
7062 : 2756 : case lds_decomp: /* lang_decl_decomp. */
7063 : 2756 : WT (lang->u.decomp.base);
7064 : 2756 : goto lds_min;
7065 : :
7066 : 3886747 : case lds_min: /* lang_decl_min. */
7067 : 3886747 : lds_min:
7068 : 3886747 : WT (lang->u.min.template_info);
7069 : 3886747 : {
7070 : 3886747 : tree access = lang->u.min.access;
7071 : :
7072 : : /* DECL_ACCESS needs to be maintained by the definition of the
7073 : : (derived) class that changes the access. The other users
7074 : : of DECL_ACCESS need to write it here. */
7075 : 1243041 : if (!DECL_THUNK_P (t)
7076 : 5127572 : && (DECL_CONTEXT (t) && TYPE_P (DECL_CONTEXT (t))))
7077 : : access = NULL_TREE;
7078 : :
7079 : 3886747 : WT (access);
7080 : : }
7081 : 3886747 : break;
7082 : :
7083 : : case lds_ns: /* lang_decl_ns. */
7084 : : break;
7085 : :
7086 : 2695254 : case lds_parm: /* lang_decl_parm. */
7087 : 2695254 : if (streaming_p ())
7088 : : {
7089 : 940746 : WU (lang->u.parm.level);
7090 : 940746 : WU (lang->u.parm.index);
7091 : : }
7092 : : break;
7093 : : }
7094 : : #undef WU
7095 : : #undef WT
7096 : 6582251 : }
7097 : :
7098 : : bool
7099 : 3099977 : trees_in::lang_decl_vals (tree t)
7100 : : {
7101 : 3099977 : struct lang_decl *lang = DECL_LANG_SPECIFIC (t);
7102 : : #define RU(X) ((X) = u ())
7103 : : #define RT(X) ((X) = tree_node ())
7104 : :
7105 : : /* Module index already read. */
7106 : 3099977 : switch (lang->u.base.selector)
7107 : : {
7108 : 0 : default:
7109 : 0 : gcc_unreachable ();
7110 : :
7111 : 689451 : case lds_fn: /* lang_decl_fn. */
7112 : 1257465 : if (DECL_NAME (t) && IDENTIFIER_OVL_OP_P (DECL_NAME (t)))
7113 : : {
7114 : 117097 : unsigned code = u ();
7115 : :
7116 : : /* Check consistency. */
7117 : 117097 : if (code >= OVL_OP_MAX
7118 : 117097 : || (ovl_op_info[IDENTIFIER_ASSIGN_OP_P (DECL_NAME (t))][code]
7119 : 117097 : .ovl_op_code) == OVL_OP_ERROR_MARK)
7120 : 0 : set_overrun ();
7121 : : else
7122 : 117097 : lang->u.fn.ovl_op_code = code;
7123 : : }
7124 : :
7125 : 689451 : if (DECL_CLASS_SCOPE_P (t))
7126 : 499728 : RT (lang->u.fn.context);
7127 : :
7128 : 689451 : if (lang->u.fn.thunk_p)
7129 : : {
7130 : 1196 : RT (lang->u.fn.befriending_classes);
7131 : 1196 : lang->u.fn.u5.fixed_offset = wi ();
7132 : : }
7133 : 688255 : else if (decl_tls_wrapper_p (t))
7134 : 6 : RT (lang->u.fn.befriending_classes);
7135 : : else
7136 : 688249 : RT (lang->u.fn.u5.cloned_function);
7137 : :
7138 : 689451 : if (FNDECL_USED_AUTO (t))
7139 : 1223 : RT (lang->u.fn.u.saved_auto_return_type);
7140 : 689451 : goto lds_min;
7141 : :
7142 : 1411 : case lds_decomp: /* lang_decl_decomp. */
7143 : 1411 : RT (lang->u.decomp.base);
7144 : 1411 : goto lds_min;
7145 : :
7146 : 2077078 : case lds_min: /* lang_decl_min. */
7147 : 2077078 : lds_min:
7148 : 2077078 : RT (lang->u.min.template_info);
7149 : 2077078 : RT (lang->u.min.access);
7150 : 2077078 : break;
7151 : :
7152 : : case lds_ns: /* lang_decl_ns. */
7153 : : break;
7154 : :
7155 : 1022752 : case lds_parm: /* lang_decl_parm. */
7156 : 1022752 : RU (lang->u.parm.level);
7157 : 1022752 : RU (lang->u.parm.index);
7158 : 1022752 : break;
7159 : : }
7160 : : #undef RU
7161 : : #undef RT
7162 : 3099977 : return !get_overrun ();
7163 : : }
7164 : :
7165 : : /* Most of the value contents of lang_type is streamed in
7166 : : define_class. */
7167 : :
7168 : : void
7169 : 466392 : trees_out::lang_type_vals (tree t)
7170 : : {
7171 : 466392 : const struct lang_type *lang = TYPE_LANG_SPECIFIC (t);
7172 : : #define WU(X) (u (X))
7173 : : #define WT(X) (tree_node (X))
7174 : 466392 : if (streaming_p ())
7175 : 233193 : WU (lang->align);
7176 : : #undef WU
7177 : : #undef WT
7178 : 466392 : }
7179 : :
7180 : : bool
7181 : 238469 : trees_in::lang_type_vals (tree t)
7182 : : {
7183 : 238469 : struct lang_type *lang = TYPE_LANG_SPECIFIC (t);
7184 : : #define RU(X) ((X) = u ())
7185 : : #define RT(X) ((X) = tree_node ())
7186 : 238469 : RU (lang->align);
7187 : : #undef RU
7188 : : #undef RT
7189 : 238469 : return !get_overrun ();
7190 : : }
7191 : :
7192 : : /* Write out the bools of T, including information about any
7193 : : LANG_SPECIFIC information. Including allocation of any lang
7194 : : specific object. */
7195 : :
7196 : : void
7197 : 19036224 : trees_out::tree_node_bools (tree t)
7198 : : {
7199 : 19036224 : gcc_checking_assert (streaming_p ());
7200 : :
7201 : : /* We should never stream a namespace. */
7202 : 19036224 : gcc_checking_assert (TREE_CODE (t) != NAMESPACE_DECL
7203 : : || DECL_NAMESPACE_ALIAS (t));
7204 : :
7205 : 19036224 : bits_out bits = stream_bits ();
7206 : 19036224 : core_bools (t, bits);
7207 : :
7208 : 19036224 : switch (TREE_CODE_CLASS (TREE_CODE (t)))
7209 : : {
7210 : 4446371 : case tcc_declaration:
7211 : 4446371 : {
7212 : 4446371 : bool specific = DECL_LANG_SPECIFIC (t) != NULL;
7213 : 4446371 : bits.b (specific);
7214 : 4446371 : if (specific && VAR_P (t))
7215 : 410044 : bits.b (DECL_DECOMPOSITION_P (t));
7216 : 2874601 : if (specific)
7217 : 2874601 : lang_decl_bools (t, bits);
7218 : : }
7219 : : break;
7220 : :
7221 : 826770 : case tcc_type:
7222 : 826770 : {
7223 : 826770 : bool specific = (TYPE_MAIN_VARIANT (t) == t
7224 : 826770 : && TYPE_LANG_SPECIFIC (t) != NULL);
7225 : 826770 : gcc_assert (TYPE_LANG_SPECIFIC (t)
7226 : : == TYPE_LANG_SPECIFIC (TYPE_MAIN_VARIANT (t)));
7227 : :
7228 : 826770 : bits.b (specific);
7229 : 826770 : if (specific)
7230 : 233193 : lang_type_bools (t, bits);
7231 : : }
7232 : : break;
7233 : :
7234 : : default:
7235 : : break;
7236 : : }
7237 : :
7238 : 19036224 : bits.bflush ();
7239 : 19036224 : }
7240 : :
7241 : : bool
7242 : 20571371 : trees_in::tree_node_bools (tree t)
7243 : : {
7244 : 20571371 : bits_in bits = stream_bits ();
7245 : 20571371 : bool ok = core_bools (t, bits);
7246 : :
7247 : 20571371 : if (ok)
7248 : 20571371 : switch (TREE_CODE_CLASS (TREE_CODE (t)))
7249 : : {
7250 : 4775133 : case tcc_declaration:
7251 : 4775133 : if (bits.b ())
7252 : : {
7253 : 3099977 : bool decomp = VAR_P (t) && bits.b ();
7254 : :
7255 : 3099977 : ok = maybe_add_lang_decl_raw (t, decomp);
7256 : 3099977 : if (ok)
7257 : 3099977 : ok = lang_decl_bools (t, bits);
7258 : : }
7259 : : break;
7260 : :
7261 : 849602 : case tcc_type:
7262 : 849602 : if (bits.b ())
7263 : : {
7264 : 238469 : ok = maybe_add_lang_type_raw (t);
7265 : 238469 : if (ok)
7266 : 238469 : ok = lang_type_bools (t, bits);
7267 : : }
7268 : : break;
7269 : :
7270 : : default:
7271 : : break;
7272 : : }
7273 : :
7274 : 20571371 : bits.bflush ();
7275 : 20571371 : if (!ok || get_overrun ())
7276 : 0 : return false;
7277 : :
7278 : : return true;
7279 : 20571371 : }
7280 : :
7281 : :
7282 : : /* Write out the lang-specifc vals of node T. */
7283 : :
7284 : : void
7285 : 49658684 : trees_out::lang_vals (tree t)
7286 : : {
7287 : 49658684 : switch (TREE_CODE_CLASS (TREE_CODE (t)))
7288 : : {
7289 : 11583705 : case tcc_declaration:
7290 : 11583705 : if (DECL_LANG_SPECIFIC (t))
7291 : 6582251 : lang_decl_vals (t);
7292 : : break;
7293 : :
7294 : 2910818 : case tcc_type:
7295 : 2910818 : if (TYPE_MAIN_VARIANT (t) == t && TYPE_LANG_SPECIFIC (t))
7296 : 466392 : lang_type_vals (t);
7297 : : break;
7298 : :
7299 : : default:
7300 : : break;
7301 : : }
7302 : 49658684 : }
7303 : :
7304 : : bool
7305 : 20543587 : trees_in::lang_vals (tree t)
7306 : : {
7307 : 20543587 : bool ok = true;
7308 : :
7309 : 20543587 : switch (TREE_CODE_CLASS (TREE_CODE (t)))
7310 : : {
7311 : 4775133 : case tcc_declaration:
7312 : 4775133 : if (DECL_LANG_SPECIFIC (t))
7313 : 3099977 : ok = lang_decl_vals (t);
7314 : : break;
7315 : :
7316 : 821818 : case tcc_type:
7317 : 821818 : if (TYPE_LANG_SPECIFIC (t))
7318 : 238469 : ok = lang_type_vals (t);
7319 : : else
7320 : 583349 : TYPE_LANG_SPECIFIC (t) = TYPE_LANG_SPECIFIC (TYPE_MAIN_VARIANT (t));
7321 : : break;
7322 : :
7323 : : default:
7324 : : break;
7325 : : }
7326 : :
7327 : 20543587 : return ok;
7328 : : }
7329 : :
7330 : : /* Write out the value fields of node T. */
7331 : :
7332 : : void
7333 : 49658684 : trees_out::tree_node_vals (tree t)
7334 : : {
7335 : 49658684 : core_vals (t);
7336 : 49658684 : lang_vals (t);
7337 : 49658684 : }
7338 : :
7339 : : bool
7340 : 20543587 : trees_in::tree_node_vals (tree t)
7341 : : {
7342 : 20543587 : bool ok = core_vals (t);
7343 : 20543587 : if (ok)
7344 : 20543587 : ok = lang_vals (t);
7345 : :
7346 : 20543587 : return ok;
7347 : : }
7348 : :
7349 : :
7350 : : /* If T is a back reference, fixed reference or NULL, write out its
7351 : : code and return WK_none. Otherwise return WK_value if we must write
7352 : : by value, or WK_normal otherwise. */
7353 : :
7354 : : walk_kind
7355 : 333027023 : trees_out::ref_node (tree t)
7356 : : {
7357 : 333027023 : if (!t)
7358 : : {
7359 : 133807522 : if (streaming_p ())
7360 : : {
7361 : : /* NULL_TREE -> tt_null. */
7362 : 51298046 : null_count++;
7363 : 51298046 : i (tt_null);
7364 : : }
7365 : 133807522 : return WK_none;
7366 : : }
7367 : :
7368 : 199219501 : if (!TREE_VISITED (t))
7369 : : return WK_normal;
7370 : :
7371 : : /* An already-visited tree. It must be in the map. */
7372 : 117215004 : int val = get_tag (t);
7373 : :
7374 : 117215004 : if (val == tag_value)
7375 : : /* An entry we should walk into. */
7376 : : return WK_value;
7377 : :
7378 : 115257946 : const char *kind;
7379 : :
7380 : 115257946 : if (val <= tag_backref)
7381 : : {
7382 : : /* Back reference -> -ve number */
7383 : 88064514 : if (streaming_p ())
7384 : 41858979 : i (val);
7385 : : kind = "backref";
7386 : : }
7387 : 27193432 : else if (val >= tag_fixed)
7388 : : {
7389 : : /* Fixed reference -> tt_fixed */
7390 : 27193432 : val -= tag_fixed;
7391 : 27193432 : if (streaming_p ())
7392 : 9547737 : i (tt_fixed), u (val);
7393 : : kind = "fixed";
7394 : : }
7395 : :
7396 : 115257946 : if (streaming_p ())
7397 : : {
7398 : 51406716 : back_ref_count++;
7399 : 51406716 : dump (dumper::TREE)
7400 : 13293 : && dump ("Wrote %s:%d %C:%N%S", kind, val, TREE_CODE (t), t, t);
7401 : : }
7402 : : return WK_none;
7403 : : }
7404 : :
7405 : : tree
7406 : 45237674 : trees_in::back_ref (int tag)
7407 : : {
7408 : 45237674 : tree res = NULL_TREE;
7409 : :
7410 : 45237674 : if (tag < 0 && unsigned (~tag) < back_refs.length ())
7411 : 45237674 : res = back_refs[~tag];
7412 : :
7413 : 45237674 : if (!res
7414 : : /* Checking TREE_CODE is a dereference, so we know this is not a
7415 : : wild pointer. Checking the code provides evidence we've not
7416 : : corrupted something. */
7417 : 45237674 : || TREE_CODE (res) >= MAX_TREE_CODES)
7418 : 0 : set_overrun ();
7419 : : else
7420 : 45251627 : dump (dumper::TREE) && dump ("Read backref:%d found %C:%N%S", tag,
7421 : : TREE_CODE (res), res, res);
7422 : 45237674 : return res;
7423 : : }
7424 : :
7425 : : unsigned
7426 : 4512881 : trees_out::add_indirect_tpl_parms (tree parms)
7427 : : {
7428 : 4512881 : unsigned len = 0;
7429 : 8409622 : for (; parms; parms = TREE_CHAIN (parms), len++)
7430 : : {
7431 : 4739907 : if (TREE_VISITED (parms))
7432 : : break;
7433 : :
7434 : 3896741 : int tag = insert (parms);
7435 : 3896741 : if (streaming_p ())
7436 : 3905927 : dump (dumper::TREE)
7437 : 153 : && dump ("Indirect:%d template's parameter %u %C:%N",
7438 : 153 : tag, len, TREE_CODE (parms), parms);
7439 : : }
7440 : :
7441 : 4512881 : if (streaming_p ())
7442 : 740921 : u (len);
7443 : :
7444 : 4512881 : return len;
7445 : : }
7446 : :
7447 : : unsigned
7448 : 767147 : trees_in::add_indirect_tpl_parms (tree parms)
7449 : : {
7450 : 767147 : unsigned len = u ();
7451 : 1369999 : for (unsigned ix = 0; ix != len; parms = TREE_CHAIN (parms), ix++)
7452 : : {
7453 : 602852 : int tag = insert (parms);
7454 : 603264 : dump (dumper::TREE)
7455 : 159 : && dump ("Indirect:%d template's parameter %u %C:%N",
7456 : 159 : tag, ix, TREE_CODE (parms), parms);
7457 : : }
7458 : :
7459 : 767147 : return len;
7460 : : }
7461 : :
7462 : : /* We've just found DECL by name. Insert nodes that come with it, but
7463 : : cannot be found by name, so we'll not accidentally walk into them. */
7464 : :
7465 : : void
7466 : 10512992 : trees_out::add_indirects (tree decl)
7467 : : {
7468 : 10512992 : unsigned count = 0;
7469 : :
7470 : : // FIXME:OPTIMIZATION We'll eventually want default fn parms of
7471 : : // templates and perhaps default template parms too. The former can
7472 : : // be referenced from instantiations (as they are lazily
7473 : : // instantiated). Also (deferred?) exception specifications of
7474 : : // templates. See the note about PARM_DECLs in trees_out::decl_node.
7475 : 10512992 : tree inner = decl;
7476 : 10512992 : if (TREE_CODE (decl) == TEMPLATE_DECL)
7477 : : {
7478 : 4512881 : count += add_indirect_tpl_parms (DECL_TEMPLATE_PARMS (decl));
7479 : :
7480 : 4512881 : inner = DECL_TEMPLATE_RESULT (decl);
7481 : 4512881 : int tag = insert (inner);
7482 : 4512881 : if (streaming_p ())
7483 : 740921 : dump (dumper::TREE)
7484 : 219 : && dump ("Indirect:%d template's result %C:%N",
7485 : 219 : tag, TREE_CODE (inner), inner);
7486 : 4512881 : count++;
7487 : : }
7488 : :
7489 : 10512992 : if (TREE_CODE (inner) == TYPE_DECL)
7490 : : {
7491 : : /* Make sure the type is in the map too. Otherwise we get
7492 : : different RECORD_TYPEs for the same type, and things go
7493 : : south. */
7494 : 5900612 : tree type = TREE_TYPE (inner);
7495 : 5900612 : gcc_checking_assert (DECL_ORIGINAL_TYPE (inner)
7496 : : || TYPE_NAME (type) == inner);
7497 : 5900612 : int tag = insert (type);
7498 : 5900612 : if (streaming_p ())
7499 : 662018 : dump (dumper::TREE) && dump ("Indirect:%d decl's type %C:%N", tag,
7500 : 360 : TREE_CODE (type), type);
7501 : 5900612 : count++;
7502 : : }
7503 : :
7504 : 10512992 : if (streaming_p ())
7505 : : {
7506 : 1587352 : u (count);
7507 : 1587862 : dump (dumper::TREE) && dump ("Inserted %u indirects", count);
7508 : : }
7509 : 10512992 : }
7510 : :
7511 : : bool
7512 : 1623961 : trees_in::add_indirects (tree decl)
7513 : : {
7514 : 1623961 : unsigned count = 0;
7515 : :
7516 : 1623961 : tree inner = decl;
7517 : 1623961 : if (TREE_CODE (inner) == TEMPLATE_DECL)
7518 : : {
7519 : 767147 : count += add_indirect_tpl_parms (DECL_TEMPLATE_PARMS (decl));
7520 : :
7521 : 767147 : inner = DECL_TEMPLATE_RESULT (decl);
7522 : 767147 : int tag = insert (inner);
7523 : 767147 : dump (dumper::TREE)
7524 : 228 : && dump ("Indirect:%d templates's result %C:%N", tag,
7525 : 228 : TREE_CODE (inner), inner);
7526 : 767147 : count++;
7527 : : }
7528 : :
7529 : 1623961 : if (TREE_CODE (inner) == TYPE_DECL)
7530 : : {
7531 : 655911 : tree type = TREE_TYPE (inner);
7532 : 655911 : gcc_checking_assert (DECL_ORIGINAL_TYPE (inner)
7533 : : || TYPE_NAME (type) == inner);
7534 : 655911 : int tag = insert (type);
7535 : 655911 : dump (dumper::TREE)
7536 : 360 : && dump ("Indirect:%d decl's type %C:%N", tag, TREE_CODE (type), type);
7537 : 655911 : count++;
7538 : : }
7539 : :
7540 : 1624534 : dump (dumper::TREE) && dump ("Inserted %u indirects", count);
7541 : 1623961 : return count == u ();
7542 : : }
7543 : :
7544 : : /* Stream a template parameter. There are 4.5 kinds of parameter:
7545 : : a) Template - TEMPLATE_DECL->TYPE_DECL->TEMPLATE_TEMPLATE_PARM
7546 : : TEMPLATE_TYPE_PARM_INDEX TPI
7547 : : b) Type - TYPE_DECL->TEMPLATE_TYPE_PARM TEMPLATE_TYPE_PARM_INDEX TPI
7548 : : c.1) NonTYPE - PARM_DECL DECL_INITIAL TPI We meet this first
7549 : : c.2) NonTYPE - CONST_DECL DECL_INITIAL Same TPI
7550 : : d) BoundTemplate - TYPE_DECL->BOUND_TEMPLATE_TEMPLATE_PARM
7551 : : TEMPLATE_TYPE_PARM_INDEX->TPI
7552 : : TEMPLATE_TEMPLATE_PARM_INFO->TEMPLATE_INFO
7553 : :
7554 : : All of these point to a TEMPLATE_PARM_INDEX, and #B also has a TEMPLATE_INFO
7555 : : */
7556 : :
7557 : : void
7558 : 2647894 : trees_out::tpl_parm_value (tree parm)
7559 : : {
7560 : 2647894 : gcc_checking_assert (DECL_P (parm) && DECL_TEMPLATE_PARM_P (parm));
7561 : :
7562 : 2647894 : int parm_tag = insert (parm);
7563 : 2647894 : if (streaming_p ())
7564 : : {
7565 : 594617 : i (tt_tpl_parm);
7566 : 594617 : dump (dumper::TREE) && dump ("Writing template parm:%d %C:%N",
7567 : 114 : parm_tag, TREE_CODE (parm), parm);
7568 : 594617 : start (parm);
7569 : 594617 : tree_node_bools (parm);
7570 : : }
7571 : :
7572 : 2647894 : tree inner = parm;
7573 : 2647894 : if (TREE_CODE (inner) == TEMPLATE_DECL)
7574 : : {
7575 : 6352 : inner = DECL_TEMPLATE_RESULT (inner);
7576 : 6352 : int inner_tag = insert (inner);
7577 : 6352 : if (streaming_p ())
7578 : : {
7579 : 1654 : dump (dumper::TREE) && dump ("Writing inner template parm:%d %C:%N",
7580 : 0 : inner_tag, TREE_CODE (inner), inner);
7581 : 1654 : start (inner);
7582 : 1654 : tree_node_bools (inner);
7583 : : }
7584 : : }
7585 : :
7586 : 2647894 : tree type = NULL_TREE;
7587 : 2647894 : if (TREE_CODE (inner) == TYPE_DECL)
7588 : : {
7589 : 2370292 : type = TREE_TYPE (inner);
7590 : 2370292 : int type_tag = insert (type);
7591 : 2370292 : if (streaming_p ())
7592 : : {
7593 : 529267 : dump (dumper::TREE) && dump ("Writing template parm type:%d %C:%N",
7594 : 108 : type_tag, TREE_CODE (type), type);
7595 : 529267 : start (type);
7596 : 529267 : tree_node_bools (type);
7597 : : }
7598 : : }
7599 : :
7600 : 2647894 : if (inner != parm)
7601 : : {
7602 : : /* This is a template-template parameter. */
7603 : 6352 : unsigned tpl_levels = 0;
7604 : 6352 : tpl_header (parm, &tpl_levels);
7605 : 6352 : tpl_parms_fini (parm, tpl_levels);
7606 : : }
7607 : :
7608 : 2647894 : tree_node_vals (parm);
7609 : 2647894 : if (inner != parm)
7610 : 6352 : tree_node_vals (inner);
7611 : 2647894 : if (type)
7612 : : {
7613 : 2370292 : tree_node_vals (type);
7614 : 2370292 : if (DECL_NAME (inner) == auto_identifier
7615 : 2370292 : || DECL_NAME (inner) == decltype_auto_identifier)
7616 : : {
7617 : : /* Placeholder auto. */
7618 : 70441 : tree_node (DECL_INITIAL (inner));
7619 : 70441 : tree_node (DECL_SIZE_UNIT (inner));
7620 : : }
7621 : : }
7622 : :
7623 : 2647894 : if (streaming_p ())
7624 : 594617 : dump (dumper::TREE) && dump ("Wrote template parm:%d %C:%N",
7625 : 114 : parm_tag, TREE_CODE (parm), parm);
7626 : 2647894 : }
7627 : :
7628 : : tree
7629 : 617702 : trees_in::tpl_parm_value ()
7630 : : {
7631 : 617702 : tree parm = start ();
7632 : 617702 : if (!parm || !tree_node_bools (parm))
7633 : 0 : return NULL_TREE;
7634 : :
7635 : 617702 : int parm_tag = insert (parm);
7636 : 617702 : dump (dumper::TREE) && dump ("Reading template parm:%d %C:%N",
7637 : 198 : parm_tag, TREE_CODE (parm), parm);
7638 : :
7639 : 617702 : tree inner = parm;
7640 : 617702 : if (TREE_CODE (inner) == TEMPLATE_DECL)
7641 : : {
7642 : 1338 : inner = start ();
7643 : 1338 : if (!inner || !tree_node_bools (inner))
7644 : 0 : return NULL_TREE;
7645 : 1338 : int inner_tag = insert (inner);
7646 : 1338 : dump (dumper::TREE) && dump ("Reading inner template parm:%d %C:%N",
7647 : 0 : inner_tag, TREE_CODE (inner), inner);
7648 : 1338 : DECL_TEMPLATE_RESULT (parm) = inner;
7649 : : }
7650 : :
7651 : 617702 : tree type = NULL_TREE;
7652 : 617702 : if (TREE_CODE (inner) == TYPE_DECL)
7653 : : {
7654 : 548164 : type = start ();
7655 : 548164 : if (!type || !tree_node_bools (type))
7656 : 0 : return NULL_TREE;
7657 : 548164 : int type_tag = insert (type);
7658 : 548164 : dump (dumper::TREE) && dump ("Reading template parm type:%d %C:%N",
7659 : 120 : type_tag, TREE_CODE (type), type);
7660 : :
7661 : 548164 : TREE_TYPE (inner) = TREE_TYPE (parm) = type;
7662 : 548164 : TYPE_NAME (type) = parm;
7663 : : }
7664 : :
7665 : 617702 : if (inner != parm)
7666 : : {
7667 : : /* A template template parameter. */
7668 : 1338 : unsigned tpl_levels = 0;
7669 : 1338 : tpl_header (parm, &tpl_levels);
7670 : 1338 : tpl_parms_fini (parm, tpl_levels);
7671 : : }
7672 : :
7673 : 617702 : tree_node_vals (parm);
7674 : 617702 : if (inner != parm)
7675 : 1338 : tree_node_vals (inner);
7676 : 617702 : if (type)
7677 : : {
7678 : 548164 : tree_node_vals (type);
7679 : 548164 : if (DECL_NAME (inner) == auto_identifier
7680 : 548164 : || DECL_NAME (inner) == decltype_auto_identifier)
7681 : : {
7682 : : /* Placeholder auto. */
7683 : 30665 : DECL_INITIAL (inner) = tree_node ();
7684 : 30665 : DECL_SIZE_UNIT (inner) = tree_node ();
7685 : : }
7686 : 548164 : if (TYPE_CANONICAL (type))
7687 : : {
7688 : 548164 : gcc_checking_assert (TYPE_CANONICAL (type) == type);
7689 : 548164 : TYPE_CANONICAL (type) = canonical_type_parameter (type);
7690 : : }
7691 : : }
7692 : :
7693 : 617702 : dump (dumper::TREE) && dump ("Read template parm:%d %C:%N",
7694 : 198 : parm_tag, TREE_CODE (parm), parm);
7695 : :
7696 : : return parm;
7697 : : }
7698 : :
7699 : : void
7700 : 1655687 : trees_out::install_entity (tree decl, depset *dep)
7701 : : {
7702 : 1655687 : gcc_checking_assert (streaming_p ());
7703 : :
7704 : : /* Write the entity index, so we can insert it as soon as we
7705 : : know this is new. */
7706 : 1655687 : u (dep ? dep->cluster + 1 : 0);
7707 : 1655687 : if (CHECKING_P && dep)
7708 : : {
7709 : : /* Add it to the entity map, such that we can tell it is
7710 : : part of us. */
7711 : 1258176 : bool existed;
7712 : 1258176 : unsigned *slot = &entity_map->get_or_insert
7713 : 1258176 : (DECL_UID (decl), &existed);
7714 : 1258176 : if (existed)
7715 : : /* If it existed, it should match. */
7716 : 395 : gcc_checking_assert (decl == (*entity_ary)[*slot]);
7717 : 1258176 : *slot = ~dep->cluster;
7718 : : }
7719 : 1655687 : }
7720 : :
7721 : : bool
7722 : 1768403 : trees_in::install_entity (tree decl)
7723 : : {
7724 : 1768403 : unsigned entity_index = u ();
7725 : 1768403 : if (!entity_index)
7726 : : return false;
7727 : :
7728 : 1333093 : if (entity_index > state->entity_num)
7729 : : {
7730 : 0 : set_overrun ();
7731 : 0 : return false;
7732 : : }
7733 : :
7734 : : /* Insert the real decl into the entity ary. */
7735 : 1333093 : unsigned ident = state->entity_lwm + entity_index - 1;
7736 : 1333093 : (*entity_ary)[ident] = decl;
7737 : :
7738 : : /* And into the entity map, if it's not already there. */
7739 : 1333093 : tree not_tmpl = STRIP_TEMPLATE (decl);
7740 : 1333093 : if (!DECL_LANG_SPECIFIC (not_tmpl)
7741 : 2486290 : || !DECL_MODULE_ENTITY_P (not_tmpl))
7742 : : {
7743 : 1332722 : retrofit_lang_decl (not_tmpl);
7744 : 1332722 : DECL_MODULE_ENTITY_P (not_tmpl) = true;
7745 : :
7746 : : /* Insert into the entity hash (it cannot already be there). */
7747 : 1332722 : bool existed;
7748 : 1332722 : unsigned &slot = entity_map->get_or_insert (DECL_UID (decl), &existed);
7749 : 1332722 : gcc_checking_assert (!existed);
7750 : 1332722 : slot = ident;
7751 : : }
7752 : 371 : else if (state->is_partition ())
7753 : : {
7754 : : /* The decl is already in the entity map, but we see it again now from a
7755 : : partition: we want to overwrite if the original decl wasn't also from
7756 : : a (possibly different) partition. Otherwise, for things like template
7757 : : instantiations, make_dependency might not realise that this is also
7758 : : provided from a partition and should be considered part of this module
7759 : : (and thus always emitted into the primary interface's CMI). */
7760 : 42 : unsigned *slot = entity_map->get (DECL_UID (decl));
7761 : 42 : module_state *imp = import_entity_module (*slot);
7762 : 42 : if (!imp->is_partition ())
7763 : 24 : *slot = ident;
7764 : : }
7765 : :
7766 : : return true;
7767 : : }
7768 : :
7769 : : static bool has_definition (tree decl);
7770 : :
7771 : : /* DECL is a decl node that must be written by value. DEP is the
7772 : : decl's depset. */
7773 : :
7774 : : void
7775 : 4584980 : trees_out::decl_value (tree decl, depset *dep)
7776 : : {
7777 : : /* We should not be writing clones or template parms. */
7778 : 4584980 : gcc_checking_assert (DECL_P (decl)
7779 : : && !DECL_CLONED_FUNCTION_P (decl)
7780 : : && !DECL_TEMPLATE_PARM_P (decl));
7781 : :
7782 : : /* We should never be writing non-typedef ptrmemfuncs by value. */
7783 : 4584980 : gcc_checking_assert (TREE_CODE (decl) != TYPE_DECL
7784 : : || DECL_ORIGINAL_TYPE (decl)
7785 : : || !TYPE_PTRMEMFUNC_P (TREE_TYPE (decl)));
7786 : :
7787 : 4584980 : merge_kind mk = get_merge_kind (decl, dep);
7788 : :
7789 : 4584980 : if (CHECKING_P)
7790 : : {
7791 : : /* Never start in the middle of a template. */
7792 : 4584980 : int use_tpl = -1;
7793 : 4584980 : if (tree ti = node_template_info (decl, use_tpl))
7794 : 1647638 : gcc_checking_assert (TREE_CODE (TI_TEMPLATE (ti)) == OVERLOAD
7795 : : || TREE_CODE (TI_TEMPLATE (ti)) == FIELD_DECL
7796 : : || (DECL_TEMPLATE_RESULT (TI_TEMPLATE (ti))
7797 : : != decl));
7798 : : }
7799 : :
7800 : 4584980 : if (streaming_p ())
7801 : : {
7802 : : /* A new node -> tt_decl. */
7803 : 1655687 : decl_val_count++;
7804 : 1655687 : i (tt_decl);
7805 : 1655687 : u (mk);
7806 : 1655687 : start (decl);
7807 : :
7808 : 1655687 : if (mk != MK_unique)
7809 : : {
7810 : 1425380 : bits_out bits = stream_bits ();
7811 : 1425380 : if (!(mk & MK_template_mask) && !state->is_header ())
7812 : : {
7813 : : /* Tell the importer whether this is a global module entity,
7814 : : or a module entity. */
7815 : 81663 : tree o = get_originating_module_decl (decl);
7816 : 81663 : bool is_attached = false;
7817 : :
7818 : 81663 : tree not_tmpl = STRIP_TEMPLATE (o);
7819 : 81663 : if (DECL_LANG_SPECIFIC (not_tmpl)
7820 : 96470 : && DECL_MODULE_ATTACH_P (not_tmpl))
7821 : : is_attached = true;
7822 : :
7823 : 81663 : bits.b (is_attached);
7824 : : }
7825 : 1425380 : bits.b (dep && dep->has_defn ());
7826 : 1425380 : }
7827 : 1655687 : tree_node_bools (decl);
7828 : : }
7829 : :
7830 : 4584980 : int tag = insert (decl, WK_value);
7831 : 4584980 : if (streaming_p ())
7832 : 1655687 : dump (dumper::TREE)
7833 : 639 : && dump ("Writing %s:%d %C:%N%S", merge_kind_name[mk], tag,
7834 : 639 : TREE_CODE (decl), decl, decl);
7835 : :
7836 : 4584980 : tree inner = decl;
7837 : 4584980 : int inner_tag = 0;
7838 : 4584980 : if (TREE_CODE (decl) == TEMPLATE_DECL)
7839 : : {
7840 : 1364589 : inner = DECL_TEMPLATE_RESULT (decl);
7841 : 1364589 : inner_tag = insert (inner, WK_value);
7842 : :
7843 : 1364589 : if (streaming_p ())
7844 : : {
7845 : 454863 : int code = TREE_CODE (inner);
7846 : 454863 : u (code);
7847 : 454863 : start (inner, true);
7848 : 454863 : tree_node_bools (inner);
7849 : 454863 : dump (dumper::TREE)
7850 : 132 : && dump ("Writing %s:%d %C:%N%S", merge_kind_name[mk], inner_tag,
7851 : 132 : TREE_CODE (inner), inner, inner);
7852 : : }
7853 : : }
7854 : :
7855 : 4584980 : tree type = NULL_TREE;
7856 : 4584980 : int type_tag = 0;
7857 : 4584980 : tree stub_decl = NULL_TREE;
7858 : 4584980 : int stub_tag = 0;
7859 : 4584980 : if (TREE_CODE (inner) == TYPE_DECL)
7860 : : {
7861 : 1784858 : type = TREE_TYPE (inner);
7862 : 1784858 : bool has_type = (type == TYPE_MAIN_VARIANT (type)
7863 : 1784858 : && TYPE_NAME (type) == inner);
7864 : :
7865 : 1784858 : if (streaming_p ())
7866 : 601505 : u (has_type ? TREE_CODE (type) : 0);
7867 : :
7868 : 1784858 : if (has_type)
7869 : : {
7870 : 810783 : type_tag = insert (type, WK_value);
7871 : 810783 : if (streaming_p ())
7872 : : {
7873 : 270257 : start (type, true);
7874 : 270257 : tree_node_bools (type);
7875 : 270257 : dump (dumper::TREE)
7876 : 153 : && dump ("Writing type:%d %C:%N", type_tag,
7877 : 153 : TREE_CODE (type), type);
7878 : : }
7879 : :
7880 : 810783 : stub_decl = TYPE_STUB_DECL (type);
7881 : 810783 : bool has_stub = inner != stub_decl;
7882 : 810783 : if (streaming_p ())
7883 : 270257 : u (has_stub ? TREE_CODE (stub_decl) : 0);
7884 : 810783 : if (has_stub)
7885 : : {
7886 : 3120 : stub_tag = insert (stub_decl);
7887 : 3120 : if (streaming_p ())
7888 : : {
7889 : 1040 : start (stub_decl, true);
7890 : 1040 : tree_node_bools (stub_decl);
7891 : 1040 : dump (dumper::TREE)
7892 : 0 : && dump ("Writing stub_decl:%d %C:%N", stub_tag,
7893 : 0 : TREE_CODE (stub_decl), stub_decl);
7894 : : }
7895 : : }
7896 : : else
7897 : : stub_decl = NULL_TREE;
7898 : : }
7899 : : else
7900 : : /* Regular typedef. */
7901 : : type = NULL_TREE;
7902 : : }
7903 : :
7904 : : /* Stream the container, we want it correctly canonicalized before
7905 : : we start emitting keys for this decl. */
7906 : 4584980 : tree container = decl_container (decl);
7907 : :
7908 : 4584980 : unsigned tpl_levels = 0;
7909 : 4584980 : if (decl != inner)
7910 : 1364589 : tpl_header (decl, &tpl_levels);
7911 : 4584980 : if (TREE_CODE (inner) == FUNCTION_DECL)
7912 : 1863316 : fn_parms_init (inner);
7913 : :
7914 : : /* Now write out the merging information, and then really
7915 : : install the tag values. */
7916 : 4584980 : key_mergeable (tag, mk, decl, inner, container, dep);
7917 : :
7918 : 4584980 : if (streaming_p ())
7919 : 1655687 : dump (dumper::MERGE)
7920 : 723 : && dump ("Wrote:%d's %s merge key %C:%N", tag,
7921 : 723 : merge_kind_name[mk], TREE_CODE (decl), decl);
7922 : :
7923 : 4584980 : if (TREE_CODE (inner) == FUNCTION_DECL)
7924 : 4584980 : fn_parms_fini (inner);
7925 : :
7926 : 4584980 : if (!is_key_order ())
7927 : 3324811 : tree_node_vals (decl);
7928 : :
7929 : 4584980 : if (inner_tag)
7930 : : {
7931 : 1364589 : if (!is_key_order ())
7932 : 909726 : tree_node_vals (inner);
7933 : 1364589 : tpl_parms_fini (decl, tpl_levels);
7934 : : }
7935 : :
7936 : 4584980 : if (type && !is_key_order ())
7937 : : {
7938 : 540526 : tree_node_vals (type);
7939 : 540526 : if (stub_decl)
7940 : 2080 : tree_node_vals (stub_decl);
7941 : : }
7942 : :
7943 : 4584980 : if (!is_key_order ())
7944 : : {
7945 : 3324811 : if (mk & MK_template_mask
7946 : 2282059 : || mk == MK_partial
7947 : 2282059 : || mk == MK_friend_spec)
7948 : : {
7949 : 51144 : if (mk != MK_partial)
7950 : : {
7951 : : // FIXME: We should make use of the merge-key by
7952 : : // exposing it outside of key_mergeable. But this gets
7953 : : // the job done.
7954 : 1042752 : auto *entry = reinterpret_cast <spec_entry *> (dep->deps[0]);
7955 : :
7956 : 1042752 : if (streaming_p ())
7957 : 521376 : u (get_mergeable_specialization_flags (entry->tmpl, decl));
7958 : 1042752 : tree_node (entry->tmpl);
7959 : 1042752 : tree_node (entry->args);
7960 : : }
7961 : : else
7962 : : {
7963 : 51144 : tree ti = get_template_info (inner);
7964 : 51144 : tree_node (TI_TEMPLATE (ti));
7965 : 51144 : tree_node (TI_ARGS (ti));
7966 : : }
7967 : : }
7968 : 3324811 : tree_node (get_constraints (decl));
7969 : : }
7970 : :
7971 : 4584980 : if (streaming_p ())
7972 : : {
7973 : : /* Do not stray outside this section. */
7974 : 1655687 : gcc_checking_assert (!dep || dep->section == dep_hash->section);
7975 : :
7976 : : /* Write the entity index, so we can insert it as soon as we
7977 : : know this is new. */
7978 : 1655687 : install_entity (decl, dep);
7979 : : }
7980 : :
7981 : 4584980 : if (DECL_LANG_SPECIFIC (inner)
7982 : 4197274 : && DECL_MODULE_KEYED_DECLS_P (inner)
7983 : 4585094 : && !is_key_order ())
7984 : : {
7985 : : /* Stream the keyed entities. */
7986 : 76 : auto *attach_vec = keyed_table->get (inner);
7987 : 76 : unsigned num = attach_vec->length ();
7988 : 76 : if (streaming_p ())
7989 : 38 : u (num);
7990 : 152 : for (unsigned ix = 0; ix != num; ix++)
7991 : : {
7992 : 76 : tree attached = (*attach_vec)[ix];
7993 : 76 : tree_node (attached);
7994 : 76 : if (streaming_p ())
7995 : 91 : dump (dumper::MERGE)
7996 : 15 : && dump ("Written %d[%u] attached decl %N", tag, ix, attached);
7997 : : }
7998 : : }
7999 : :
8000 : 4584980 : bool is_typedef = false;
8001 : 4584980 : if (!type && TREE_CODE (inner) == TYPE_DECL)
8002 : : {
8003 : 974075 : tree t = TREE_TYPE (inner);
8004 : 974075 : unsigned tdef_flags = 0;
8005 : 974075 : if (DECL_ORIGINAL_TYPE (inner)
8006 : 974075 : && TYPE_NAME (TREE_TYPE (inner)) == inner)
8007 : : {
8008 : 974075 : tdef_flags |= 1;
8009 : 974075 : if (TYPE_STRUCTURAL_EQUALITY_P (t)
8010 : 200407 : && TYPE_DEPENDENT_P_VALID (t)
8011 : 1111386 : && TYPE_DEPENDENT_P (t))
8012 : : tdef_flags |= 2;
8013 : : }
8014 : 974075 : if (streaming_p ())
8015 : 331248 : u (tdef_flags);
8016 : :
8017 : 974075 : if (tdef_flags & 1)
8018 : : {
8019 : : /* A typedef type. */
8020 : 974075 : int type_tag = insert (t);
8021 : 974075 : if (streaming_p ())
8022 : 331248 : dump (dumper::TREE)
8023 : 198 : && dump ("Cloned:%d %s %C:%N", type_tag,
8024 : 198 : tdef_flags & 2 ? "depalias" : "typedef",
8025 : 198 : TREE_CODE (t), t);
8026 : :
8027 : : is_typedef = true;
8028 : : }
8029 : : }
8030 : :
8031 : 4584980 : if (streaming_p () && DECL_MAYBE_IN_CHARGE_CDTOR_P (decl))
8032 : : {
8033 : 128045 : bool cloned_p
8034 : 128045 : = (DECL_CHAIN (decl) && DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl)));
8035 : 88354 : bool needs_vtt_parm_p
8036 : 88354 : = (cloned_p && CLASSTYPE_VBASECLASSES (DECL_CONTEXT (decl)));
8037 : 88354 : bool omit_inherited_parms_p
8038 : 88354 : = (cloned_p && DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl)
8039 : 70473 : && base_ctor_omit_inherited_parms (decl));
8040 : 128045 : unsigned flags = (int (cloned_p) << 0
8041 : 128045 : | int (needs_vtt_parm_p) << 1
8042 : 128045 : | int (omit_inherited_parms_p) << 2);
8043 : 128045 : u (flags);
8044 : 128120 : dump (dumper::TREE) && dump ("CDTOR %N is %scloned",
8045 : : decl, cloned_p ? "" : "not ");
8046 : : }
8047 : :
8048 : 4584980 : if (streaming_p () && VAR_P (decl) && CP_DECL_THREAD_LOCAL_P (decl))
8049 : 39 : u (decl_tls_model (decl));
8050 : :
8051 : 4584980 : if (streaming_p ())
8052 : 1655687 : dump (dumper::TREE) && dump ("Written decl:%d %C:%N", tag,
8053 : 639 : TREE_CODE (decl), decl);
8054 : :
8055 : 4584980 : if (NAMESPACE_SCOPE_P (inner))
8056 : 2817488 : gcc_checking_assert (!dep == (VAR_OR_FUNCTION_DECL_P (inner)
8057 : : && DECL_LOCAL_DECL_P (inner)));
8058 : 3176100 : else if ((TREE_CODE (inner) == TYPE_DECL
8059 : 962525 : && !is_typedef
8060 : 166143 : && TYPE_NAME (TREE_TYPE (inner)) == inner)
8061 : 3972482 : || TREE_CODE (inner) == FUNCTION_DECL)
8062 : : {
8063 : 1513970 : bool write_defn = !dep && has_definition (decl);
8064 : 1513970 : if (streaming_p ())
8065 : 505021 : u (write_defn);
8066 : 1513970 : if (write_defn)
8067 : 0 : write_definition (decl);
8068 : : }
8069 : 4584980 : }
8070 : :
8071 : : tree
8072 : 1768403 : trees_in::decl_value ()
8073 : : {
8074 : 1768403 : int tag = 0;
8075 : 1768403 : bool is_attached = false;
8076 : 1768403 : bool has_defn = false;
8077 : 1768403 : unsigned mk_u = u ();
8078 : 1768403 : if (mk_u >= MK_hwm || !merge_kind_name[mk_u])
8079 : : {
8080 : 0 : set_overrun ();
8081 : 0 : return NULL_TREE;
8082 : : }
8083 : :
8084 : 1768403 : unsigned saved_unused = unused;
8085 : 1768403 : unused = 0;
8086 : :
8087 : 1768403 : merge_kind mk = merge_kind (mk_u);
8088 : :
8089 : 1768403 : tree decl = start ();
8090 : 1768403 : if (decl)
8091 : : {
8092 : 1768403 : if (mk != MK_unique)
8093 : : {
8094 : 1504703 : bits_in bits = stream_bits ();
8095 : 1504703 : if (!(mk & MK_template_mask) && !state->is_header ())
8096 : 41661 : is_attached = bits.b ();
8097 : :
8098 : 1504703 : has_defn = bits.b ();
8099 : 1504703 : }
8100 : :
8101 : 1768403 : if (!tree_node_bools (decl))
8102 : 0 : decl = NULL_TREE;
8103 : : }
8104 : :
8105 : : /* Insert into map. */
8106 : 1768403 : tag = insert (decl);
8107 : 1768403 : if (decl)
8108 : 1768403 : dump (dumper::TREE)
8109 : 936 : && dump ("Reading:%d %C", tag, TREE_CODE (decl));
8110 : :
8111 : 1768403 : tree inner = decl;
8112 : 1768403 : int inner_tag = 0;
8113 : 1768403 : if (decl && TREE_CODE (decl) == TEMPLATE_DECL)
8114 : : {
8115 : 478086 : int code = u ();
8116 : 478086 : inner = start (code);
8117 : 478086 : if (inner && tree_node_bools (inner))
8118 : 478086 : DECL_TEMPLATE_RESULT (decl) = inner;
8119 : : else
8120 : : decl = NULL_TREE;
8121 : :
8122 : 478086 : inner_tag = insert (inner);
8123 : 478086 : if (decl)
8124 : 478086 : dump (dumper::TREE)
8125 : 204 : && dump ("Reading:%d %C", inner_tag, TREE_CODE (inner));
8126 : : }
8127 : :
8128 : 1768403 : tree type = NULL_TREE;
8129 : 1768403 : int type_tag = 0;
8130 : 1768403 : tree stub_decl = NULL_TREE;
8131 : 1768403 : int stub_tag = 0;
8132 : 1768403 : if (decl && TREE_CODE (inner) == TYPE_DECL)
8133 : : {
8134 : 610586 : if (unsigned type_code = u ())
8135 : : {
8136 : 273654 : type = start (type_code);
8137 : 273654 : if (type && tree_node_bools (type))
8138 : : {
8139 : 273654 : TREE_TYPE (inner) = type;
8140 : 273654 : TYPE_NAME (type) = inner;
8141 : : }
8142 : : else
8143 : : decl = NULL_TREE;
8144 : :
8145 : 273654 : type_tag = insert (type);
8146 : 273654 : if (decl)
8147 : 273654 : dump (dumper::TREE)
8148 : 210 : && dump ("Reading type:%d %C", type_tag, TREE_CODE (type));
8149 : :
8150 : 273654 : if (unsigned stub_code = u ())
8151 : : {
8152 : 934 : stub_decl = start (stub_code);
8153 : 934 : if (stub_decl && tree_node_bools (stub_decl))
8154 : : {
8155 : 934 : TREE_TYPE (stub_decl) = type;
8156 : 934 : TYPE_STUB_DECL (type) = stub_decl;
8157 : : }
8158 : : else
8159 : : decl = NULL_TREE;
8160 : :
8161 : 934 : stub_tag = insert (stub_decl);
8162 : 934 : if (decl)
8163 : 934 : dump (dumper::TREE)
8164 : 0 : && dump ("Reading stub_decl:%d %C", stub_tag,
8165 : 0 : TREE_CODE (stub_decl));
8166 : : }
8167 : : }
8168 : : }
8169 : :
8170 : 1768403 : if (!decl)
8171 : : {
8172 : 0 : bail:
8173 : 0 : if (inner_tag != 0)
8174 : 0 : back_refs[~inner_tag] = NULL_TREE;
8175 : 0 : if (type_tag != 0)
8176 : 0 : back_refs[~type_tag] = NULL_TREE;
8177 : 0 : if (stub_tag != 0)
8178 : 0 : back_refs[~stub_tag] = NULL_TREE;
8179 : 0 : if (tag != 0)
8180 : 0 : back_refs[~tag] = NULL_TREE;
8181 : 0 : set_overrun ();
8182 : : /* Bail. */
8183 : 0 : unused = saved_unused;
8184 : 0 : return NULL_TREE;
8185 : : }
8186 : :
8187 : : /* Read the container, to ensure it's already been streamed in. */
8188 : 1768403 : tree container = decl_container ();
8189 : 1768403 : unsigned tpl_levels = 0;
8190 : :
8191 : : /* Figure out if this decl is already known about. */
8192 : 1768403 : int parm_tag = 0;
8193 : :
8194 : 1768403 : if (decl != inner)
8195 : 478086 : if (!tpl_header (decl, &tpl_levels))
8196 : 0 : goto bail;
8197 : 1768403 : if (TREE_CODE (inner) == FUNCTION_DECL)
8198 : 689451 : parm_tag = fn_parms_init (inner);
8199 : :
8200 : 1768403 : tree existing = key_mergeable (tag, mk, decl, inner, type, container,
8201 : 1768403 : is_attached);
8202 : 1768403 : tree existing_inner = existing;
8203 : 1768403 : if (existing)
8204 : : {
8205 : 917065 : if (existing == error_mark_node)
8206 : 0 : goto bail;
8207 : :
8208 : 917065 : if (TREE_CODE (STRIP_TEMPLATE (existing)) == TYPE_DECL)
8209 : : {
8210 : 348985 : tree etype = TREE_TYPE (existing);
8211 : 348985 : if (TYPE_LANG_SPECIFIC (etype)
8212 : 254445 : && COMPLETE_TYPE_P (etype)
8213 : 498157 : && !CLASSTYPE_MEMBER_VEC (etype))
8214 : : /* Give it a member vec, we're likely gonna be looking
8215 : : inside it. */
8216 : 31282 : set_class_bindings (etype, -1);
8217 : : }
8218 : :
8219 : : /* Install the existing decl into the back ref array. */
8220 : 917065 : register_duplicate (decl, existing);
8221 : 917065 : back_refs[~tag] = existing;
8222 : 917065 : if (inner_tag != 0)
8223 : : {
8224 : 305536 : existing_inner = DECL_TEMPLATE_RESULT (existing);
8225 : 305536 : back_refs[~inner_tag] = existing_inner;
8226 : : }
8227 : :
8228 : 917065 : if (type_tag != 0)
8229 : : {
8230 : 165699 : tree existing_type = TREE_TYPE (existing);
8231 : 165699 : back_refs[~type_tag] = existing_type;
8232 : 165699 : if (stub_tag != 0)
8233 : 564 : back_refs[~stub_tag] = TYPE_STUB_DECL (existing_type);
8234 : : }
8235 : : }
8236 : :
8237 : 1768403 : if (parm_tag)
8238 : 689451 : fn_parms_fini (parm_tag, inner, existing_inner, has_defn);
8239 : :
8240 : 1768403 : if (!tree_node_vals (decl))
8241 : 0 : goto bail;
8242 : :
8243 : 1768403 : if (inner_tag)
8244 : : {
8245 : 478086 : gcc_checking_assert (DECL_TEMPLATE_RESULT (decl) == inner);
8246 : :
8247 : 478086 : if (!tree_node_vals (inner))
8248 : 0 : goto bail;
8249 : :
8250 : 478086 : if (!tpl_parms_fini (decl, tpl_levels))
8251 : 0 : goto bail;
8252 : : }
8253 : :
8254 : 1768403 : if (type && (!tree_node_vals (type)
8255 : 273654 : || (stub_decl && !tree_node_vals (stub_decl))))
8256 : 0 : goto bail;
8257 : :
8258 : 1768403 : spec_entry spec;
8259 : 1768403 : unsigned spec_flags = 0;
8260 : 1768403 : if (mk & MK_template_mask
8261 : 1203129 : || mk == MK_partial
8262 : 1203129 : || mk == MK_friend_spec)
8263 : : {
8264 : 22008 : if (mk == MK_partial)
8265 : : spec_flags = 2;
8266 : : else
8267 : 565274 : spec_flags = u ();
8268 : :
8269 : 587282 : spec.tmpl = tree_node ();
8270 : 587282 : spec.args = tree_node ();
8271 : : }
8272 : : /* Hold constraints on the spec field, for a short while. */
8273 : 1768403 : spec.spec = tree_node ();
8274 : :
8275 : 1769339 : dump (dumper::TREE) && dump ("Read:%d %C:%N", tag, TREE_CODE (decl), decl);
8276 : :
8277 : 1768403 : existing = back_refs[~tag];
8278 : 1768403 : bool installed = install_entity (existing);
8279 : 1768403 : bool is_new = existing == decl;
8280 : :
8281 : 1768403 : if (DECL_LANG_SPECIFIC (inner)
8282 : 3370173 : && DECL_MODULE_KEYED_DECLS_P (inner))
8283 : : {
8284 : : /* Read and maybe install the attached entities. */
8285 : 56 : bool existed;
8286 : 56 : auto &set = keyed_table->get_or_insert (STRIP_TEMPLATE (existing),
8287 : : &existed);
8288 : 56 : unsigned num = u ();
8289 : 56 : if (is_new == existed)
8290 : 0 : set_overrun ();
8291 : 56 : if (is_new)
8292 : 24 : set.reserve (num);
8293 : 112 : for (unsigned ix = 0; !get_overrun () && ix != num; ix++)
8294 : : {
8295 : 56 : tree attached = tree_node ();
8296 : 56 : dump (dumper::MERGE)
8297 : 58 : && dump ("Read %d[%u] %s attached decl %N", tag, ix,
8298 : : is_new ? "new" : "matched", attached);
8299 : 56 : if (is_new)
8300 : 24 : set.quick_push (attached);
8301 : 32 : else if (set[ix] != attached)
8302 : 0 : set_overrun ();
8303 : : }
8304 : : }
8305 : :
8306 : : /* Regular typedefs will have a NULL TREE_TYPE at this point. */
8307 : 1768403 : unsigned tdef_flags = 0;
8308 : 1768403 : bool is_typedef = false;
8309 : 1768403 : if (!type && TREE_CODE (inner) == TYPE_DECL)
8310 : : {
8311 : 336932 : tdef_flags = u ();
8312 : 336932 : if (tdef_flags & 1)
8313 : 336932 : is_typedef = true;
8314 : : }
8315 : :
8316 : 1768403 : if (is_new)
8317 : : {
8318 : : /* A newly discovered node. */
8319 : 851338 : if (TREE_CODE (decl) == FUNCTION_DECL && DECL_VIRTUAL_P (decl))
8320 : : /* Mark this identifier as naming a virtual function --
8321 : : lookup_overrides relies on this optimization. */
8322 : 7874 : IDENTIFIER_VIRTUAL_P (DECL_NAME (decl)) = true;
8323 : :
8324 : 851338 : if (installed)
8325 : : {
8326 : : /* Mark the entity as imported. */
8327 : 521888 : retrofit_lang_decl (inner);
8328 : 521888 : DECL_MODULE_IMPORT_P (inner) = true;
8329 : : }
8330 : :
8331 : 851338 : if (spec.spec)
8332 : 15108 : set_constraints (decl, spec.spec);
8333 : :
8334 : 851338 : if (TREE_CODE (decl) == INTEGER_CST && !TREE_OVERFLOW (decl))
8335 : : {
8336 : 0 : decl = cache_integer_cst (decl, true);
8337 : 0 : back_refs[~tag] = decl;
8338 : : }
8339 : :
8340 : 851338 : if (is_typedef)
8341 : : {
8342 : : /* Frob it to be ready for cloning. */
8343 : 153646 : TREE_TYPE (inner) = DECL_ORIGINAL_TYPE (inner);
8344 : 153646 : DECL_ORIGINAL_TYPE (inner) = NULL_TREE;
8345 : 153646 : set_underlying_type (inner);
8346 : 153646 : if (tdef_flags & 2)
8347 : : {
8348 : : /* Match instantiate_alias_template's handling. */
8349 : 28432 : tree type = TREE_TYPE (inner);
8350 : 28432 : TYPE_DEPENDENT_P (type) = true;
8351 : 28432 : TYPE_DEPENDENT_P_VALID (type) = true;
8352 : 28432 : SET_TYPE_STRUCTURAL_EQUALITY (type);
8353 : : }
8354 : : }
8355 : :
8356 : 851338 : if (inner_tag)
8357 : : /* Set the TEMPLATE_DECL's type. */
8358 : 172550 : TREE_TYPE (decl) = TREE_TYPE (inner);
8359 : :
8360 : : /* Redetermine whether we need to import or export this declaration
8361 : : for this TU. But for extern templates we know we must import:
8362 : : they'll be defined in a different TU.
8363 : : FIXME: How do dllexport and dllimport interact across a module?
8364 : : See also https://github.com/itanium-cxx-abi/cxx-abi/issues/170.
8365 : : May have to revisit? */
8366 : 851338 : if (type
8367 : 107955 : && CLASS_TYPE_P (type)
8368 : 92037 : && TYPE_LANG_SPECIFIC (type)
8369 : 943375 : && !(CLASSTYPE_EXPLICIT_INSTANTIATION (type)
8370 : 966 : && CLASSTYPE_INTERFACE_KNOWN (type)
8371 : 966 : && CLASSTYPE_INTERFACE_ONLY (type)))
8372 : : {
8373 : 91095 : CLASSTYPE_INTERFACE_ONLY (type) = false;
8374 : 91095 : CLASSTYPE_INTERFACE_UNKNOWN (type) = true;
8375 : : }
8376 : :
8377 : : /* Add to specialization tables now that constraints etc are
8378 : : added. */
8379 : 851338 : if (mk == MK_partial)
8380 : : {
8381 : 8103 : bool is_type = TREE_CODE (inner) == TYPE_DECL;
8382 : 8103 : spec.spec = is_type ? type : inner;
8383 : 8103 : add_mergeable_specialization (!is_type, &spec, decl, spec_flags);
8384 : : }
8385 : 843235 : else if (mk & MK_template_mask)
8386 : : {
8387 : 236393 : bool is_type = !(mk & MK_tmpl_decl_mask);
8388 : 236393 : spec.spec = is_type ? type : mk & MK_tmpl_tmpl_mask ? inner : decl;
8389 : 236393 : add_mergeable_specialization (!is_type, &spec, decl, spec_flags);
8390 : : }
8391 : :
8392 : 851338 : if (NAMESPACE_SCOPE_P (decl)
8393 : 185759 : && (mk == MK_named || mk == MK_unique
8394 : 185759 : || mk == MK_enum || mk == MK_friend_spec)
8395 : 940895 : && !(VAR_OR_FUNCTION_DECL_P (decl) && DECL_LOCAL_DECL_P (decl)))
8396 : 89444 : add_module_namespace_decl (CP_DECL_CONTEXT (decl), decl);
8397 : :
8398 : 851338 : if (DECL_ARTIFICIAL (decl)
8399 : 223285 : && TREE_CODE (decl) == FUNCTION_DECL
8400 : 20605 : && !DECL_TEMPLATE_INFO (decl)
8401 : 20335 : && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
8402 : 20202 : && TYPE_SIZE (DECL_CONTEXT (decl))
8403 : 854007 : && !DECL_THUNK_P (decl))
8404 : : /* A new implicit member function, when the class is
8405 : : complete. This means the importee declared it, and
8406 : : we must now add it to the class. Note that implicit
8407 : : member fns of template instantiations do not themselves
8408 : : look like templates. */
8409 : 1473 : if (!install_implicit_member (inner))
8410 : 0 : set_overrun ();
8411 : :
8412 : : /* When importing a TLS wrapper from a header unit, we haven't
8413 : : actually emitted its definition yet. Remember it so we can
8414 : : do this later. */
8415 : 851338 : if (state->is_header ()
8416 : 851338 : && decl_tls_wrapper_p (decl))
8417 : 6 : note_vague_linkage_fn (decl);
8418 : :
8419 : : /* Setup aliases for the declaration. */
8420 : 851338 : if (tree alias = lookup_attribute ("alias", DECL_ATTRIBUTES (decl)))
8421 : : {
8422 : 3 : alias = TREE_VALUE (TREE_VALUE (alias));
8423 : 3 : alias = get_identifier (TREE_STRING_POINTER (alias));
8424 : 3 : assemble_alias (decl, alias);
8425 : : }
8426 : : }
8427 : : else
8428 : : {
8429 : : /* DECL is the to-be-discarded decl. Its internal pointers will
8430 : : be to the EXISTING's structure. Frob it to point to its
8431 : : own other structures, so loading its definition will alter
8432 : : it, and not the existing decl. */
8433 : 918437 : dump (dumper::MERGE) && dump ("Deduping %N", existing);
8434 : :
8435 : 917065 : if (inner_tag)
8436 : 305536 : DECL_TEMPLATE_RESULT (decl) = inner;
8437 : :
8438 : 917065 : if (type)
8439 : : {
8440 : : /* Point at the to-be-discarded type & decl. */
8441 : 165699 : TYPE_NAME (type) = inner;
8442 : 165699 : TREE_TYPE (inner) = type;
8443 : :
8444 : 330834 : TYPE_STUB_DECL (type) = stub_decl ? stub_decl : inner;
8445 : 165699 : if (stub_decl)
8446 : 564 : TREE_TYPE (stub_decl) = type;
8447 : : }
8448 : :
8449 : 917065 : if (inner_tag)
8450 : : /* Set the TEMPLATE_DECL's type. */
8451 : 305536 : TREE_TYPE (decl) = TREE_TYPE (inner);
8452 : :
8453 : 917065 : if (!is_matching_decl (existing, decl, is_typedef))
8454 : 9 : unmatched_duplicate (existing);
8455 : :
8456 : 917065 : if (TREE_CODE (inner) == FUNCTION_DECL)
8457 : : {
8458 : 425594 : tree e_inner = STRIP_TEMPLATE (existing);
8459 : 425594 : for (auto parm = DECL_ARGUMENTS (inner);
8460 : 1304542 : parm; parm = DECL_CHAIN (parm))
8461 : 878948 : DECL_CONTEXT (parm) = e_inner;
8462 : : }
8463 : :
8464 : : /* And our result is the existing node. */
8465 : : decl = existing;
8466 : : }
8467 : :
8468 : 1768403 : if (mk == MK_friend_spec)
8469 : : {
8470 : 0 : tree e = match_mergeable_specialization (true, &spec);
8471 : 0 : if (!e)
8472 : : {
8473 : 0 : spec.spec = inner;
8474 : 0 : add_mergeable_specialization (true, &spec, decl, spec_flags);
8475 : : }
8476 : 0 : else if (e != existing)
8477 : 0 : set_overrun ();
8478 : : }
8479 : :
8480 : 1768403 : if (is_typedef)
8481 : : {
8482 : : /* Insert the type into the array now. */
8483 : 336932 : tag = insert (TREE_TYPE (decl));
8484 : 336932 : dump (dumper::TREE)
8485 : 243 : && dump ("Cloned:%d typedef %C:%N",
8486 : 243 : tag, TREE_CODE (TREE_TYPE (decl)), TREE_TYPE (decl));
8487 : : }
8488 : :
8489 : 1768403 : unused = saved_unused;
8490 : :
8491 : 1768403 : if (DECL_MAYBE_IN_CHARGE_CDTOR_P (decl))
8492 : : {
8493 : 142500 : unsigned flags = u ();
8494 : :
8495 : 142500 : if (is_new)
8496 : : {
8497 : 57650 : bool cloned_p = flags & 1;
8498 : 57746 : dump (dumper::TREE) && dump ("CDTOR %N is %scloned",
8499 : : decl, cloned_p ? "" : "not ");
8500 : 57650 : if (cloned_p)
8501 : 84420 : build_cdtor_clones (decl, flags & 2, flags & 4,
8502 : : /* Update the member vec, if there is
8503 : : one (we're in a different cluster
8504 : : to the class defn). */
8505 : 42210 : CLASSTYPE_MEMBER_VEC (DECL_CONTEXT (decl)));
8506 : : }
8507 : : }
8508 : :
8509 : 1768403 : if (VAR_P (decl) && CP_DECL_THREAD_LOCAL_P (decl))
8510 : : {
8511 : 42 : enum tls_model model = tls_model (u ());
8512 : 42 : if (is_new)
8513 : 18 : set_decl_tls_model (decl, model);
8514 : : }
8515 : :
8516 : 1768403 : if (!NAMESPACE_SCOPE_P (inner)
8517 : 1277853 : && ((TREE_CODE (inner) == TYPE_DECL
8518 : 331437 : && !is_typedef
8519 : 53965 : && TYPE_NAME (TREE_TYPE (inner)) == inner)
8520 : 1223888 : || TREE_CODE (inner) == FUNCTION_DECL)
8521 : 2322096 : && u ())
8522 : 0 : read_definition (decl);
8523 : :
8524 : : return decl;
8525 : : }
8526 : :
8527 : : /* DECL is an unnameable member of CTX. Return a suitable identifying
8528 : : index. */
8529 : :
8530 : : static unsigned
8531 : 1372 : get_field_ident (tree ctx, tree decl)
8532 : : {
8533 : 1372 : gcc_checking_assert (TREE_CODE (decl) == USING_DECL
8534 : : || !DECL_NAME (decl)
8535 : : || IDENTIFIER_ANON_P (DECL_NAME (decl)));
8536 : :
8537 : 1372 : unsigned ix = 0;
8538 : 1372 : for (tree fields = TYPE_FIELDS (ctx);
8539 : 11000 : fields; fields = DECL_CHAIN (fields))
8540 : : {
8541 : 11000 : if (fields == decl)
8542 : 1372 : return ix;
8543 : :
8544 : 9628 : if (DECL_CONTEXT (fields) == ctx
8545 : 9628 : && (TREE_CODE (fields) == USING_DECL
8546 : 9622 : || (TREE_CODE (fields) == FIELD_DECL
8547 : 30 : && (!DECL_NAME (fields)
8548 : 3 : || IDENTIFIER_ANON_P (DECL_NAME (fields))))))
8549 : : /* Count this field. */
8550 : 27 : ix++;
8551 : : }
8552 : 0 : gcc_unreachable ();
8553 : : }
8554 : :
8555 : : static tree
8556 : 1482 : lookup_field_ident (tree ctx, unsigned ix)
8557 : : {
8558 : 1482 : for (tree fields = TYPE_FIELDS (ctx);
8559 : 11337 : fields; fields = DECL_CHAIN (fields))
8560 : 11337 : if (DECL_CONTEXT (fields) == ctx
8561 : 11337 : && (TREE_CODE (fields) == USING_DECL
8562 : 11331 : || (TREE_CODE (fields) == FIELD_DECL
8563 : 1521 : && (!DECL_NAME (fields)
8564 : 3 : || IDENTIFIER_ANON_P (DECL_NAME (fields))))))
8565 : 1518 : if (!ix--)
8566 : 1482 : return fields;
8567 : :
8568 : : return NULL_TREE;
8569 : : }
8570 : :
8571 : : /* Reference DECL. REF indicates the walk kind we are performing.
8572 : : Return true if we should write this decl by value. */
8573 : :
8574 : : bool
8575 : 15470296 : trees_out::decl_node (tree decl, walk_kind ref)
8576 : : {
8577 : 15470296 : gcc_checking_assert (DECL_P (decl) && !DECL_TEMPLATE_PARM_P (decl)
8578 : : && DECL_CONTEXT (decl));
8579 : :
8580 : 15470296 : if (ref == WK_value)
8581 : : {
8582 : 1595494 : depset *dep = dep_hash->find_dependency (decl);
8583 : 1595494 : decl_value (decl, dep);
8584 : 1595494 : return false;
8585 : : }
8586 : :
8587 : 13874802 : switch (TREE_CODE (decl))
8588 : : {
8589 : : default:
8590 : : break;
8591 : :
8592 : 1415195 : case FUNCTION_DECL:
8593 : 1415195 : gcc_checking_assert (!DECL_LOCAL_DECL_P (decl));
8594 : : break;
8595 : :
8596 : : case RESULT_DECL:
8597 : : /* Unlike PARM_DECLs, RESULT_DECLs are only generated and
8598 : : referenced when we're inside the function itself. */
8599 : : return true;
8600 : :
8601 : 334214 : case PARM_DECL:
8602 : 334214 : {
8603 : 334214 : if (streaming_p ())
8604 : 80475 : i (tt_parm);
8605 : 334214 : tree_node (DECL_CONTEXT (decl));
8606 : 334214 : if (streaming_p ())
8607 : : {
8608 : : /* That must have put this in the map. */
8609 : 80475 : walk_kind ref = ref_node (decl);
8610 : 80475 : if (ref != WK_none)
8611 : : // FIXME:OPTIMIZATION We can wander into bits of the
8612 : : // template this was instantiated from. For instance
8613 : : // deferred noexcept and default parms. Currently we'll
8614 : : // end up cloning those bits of tree. It would be nice
8615 : : // to reference those specific nodes. I think putting
8616 : : // those things in the map when we reference their
8617 : : // template by name. See the note in add_indirects.
8618 : : return true;
8619 : :
8620 : 0 : dump (dumper::TREE)
8621 : 0 : && dump ("Wrote %s reference %N",
8622 : 0 : TREE_CODE (decl) == PARM_DECL ? "parameter" : "result",
8623 : : decl);
8624 : : }
8625 : : }
8626 : : return false;
8627 : :
8628 : : case IMPORTED_DECL:
8629 : : /* This describes a USING_DECL to the ME's debug machinery. It
8630 : : originates from the fortran FE, and has nothing to do with
8631 : : C++ modules. */
8632 : : return true;
8633 : :
8634 : : case LABEL_DECL:
8635 : : return true;
8636 : :
8637 : 58634 : case CONST_DECL:
8638 : 58634 : {
8639 : : /* If I end up cloning enum decls, implementing C++20 using
8640 : : E::v, this will need tweaking. */
8641 : 58634 : if (streaming_p ())
8642 : 10206 : i (tt_enum_decl);
8643 : 58634 : tree ctx = DECL_CONTEXT (decl);
8644 : 58634 : gcc_checking_assert (TREE_CODE (ctx) == ENUMERAL_TYPE);
8645 : 58634 : tree_node (ctx);
8646 : 58634 : tree_node (DECL_NAME (decl));
8647 : :
8648 : 58634 : int tag = insert (decl);
8649 : 58634 : if (streaming_p ())
8650 : 10206 : dump (dumper::TREE)
8651 : 21 : && dump ("Wrote enum decl:%d %C:%N", tag, TREE_CODE (decl), decl);
8652 : : return false;
8653 : : }
8654 : 6331 : break;
8655 : :
8656 : 6331 : case USING_DECL:
8657 : 6331 : if (TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
8658 : : break;
8659 : : /* FALLTHROUGH */
8660 : :
8661 : 175831 : case FIELD_DECL:
8662 : 175831 : {
8663 : 175831 : if (streaming_p ())
8664 : 9188 : i (tt_data_member);
8665 : :
8666 : 175831 : tree ctx = DECL_CONTEXT (decl);
8667 : 175831 : tree_node (ctx);
8668 : :
8669 : 175831 : tree name = NULL_TREE;
8670 : :
8671 : 175831 : if (TREE_CODE (decl) == USING_DECL)
8672 : : ;
8673 : : else
8674 : : {
8675 : 174470 : name = DECL_NAME (decl);
8676 : 339252 : if (name && IDENTIFIER_ANON_P (name))
8677 : : name = NULL_TREE;
8678 : : }
8679 : :
8680 : 175831 : tree_node (name);
8681 : 175831 : if (!name && streaming_p ())
8682 : : {
8683 : 1372 : unsigned ix = get_field_ident (ctx, decl);
8684 : 1372 : u (ix);
8685 : : }
8686 : :
8687 : 175831 : int tag = insert (decl);
8688 : 175831 : if (streaming_p ())
8689 : 9188 : dump (dumper::TREE)
8690 : 21 : && dump ("Wrote member:%d %C:%N", tag, TREE_CODE (decl), decl);
8691 : : return false;
8692 : : }
8693 : 539607 : break;
8694 : :
8695 : 539607 : case VAR_DECL:
8696 : 539607 : gcc_checking_assert (!DECL_LOCAL_DECL_P (decl));
8697 : 539607 : if (DECL_VTABLE_OR_VTT_P (decl))
8698 : : {
8699 : : /* VTT or VTABLE, they are all on the vtables list. */
8700 : 2120 : tree ctx = CP_DECL_CONTEXT (decl);
8701 : 2120 : tree vtable = CLASSTYPE_VTABLES (ctx);
8702 : 2126 : for (unsigned ix = 0; ; vtable = DECL_CHAIN (vtable), ix++)
8703 : 2126 : if (vtable == decl)
8704 : : {
8705 : 2120 : gcc_checking_assert (DECL_VIRTUAL_P (decl));
8706 : 2120 : if (streaming_p ())
8707 : : {
8708 : 15 : u (tt_vtable);
8709 : 15 : u (ix);
8710 : 15 : dump (dumper::TREE)
8711 : 0 : && dump ("Writing vtable %N[%u]", ctx, ix);
8712 : : }
8713 : 2120 : tree_node (ctx);
8714 : 2120 : return false;
8715 : : }
8716 : : gcc_unreachable ();
8717 : : }
8718 : :
8719 : 537487 : if (DECL_TINFO_P (decl))
8720 : : {
8721 : 9514 : tinfo:
8722 : : /* A typeinfo, tt_tinfo_typedef or tt_tinfo_var. */
8723 : 17371 : bool is_var = VAR_P (decl);
8724 : 17371 : tree type = TREE_TYPE (decl);
8725 : 17371 : unsigned ix = get_pseudo_tinfo_index (type);
8726 : 17371 : if (streaming_p ())
8727 : : {
8728 : 10895 : i (is_var ? tt_tinfo_var : tt_tinfo_typedef);
8729 : 7770 : u (ix);
8730 : : }
8731 : :
8732 : 17371 : if (is_var)
8733 : : {
8734 : : /* We also need the type it is for and mangled name, so
8735 : : the reader doesn't need to complete the type (which
8736 : : would break section ordering). The type it is for is
8737 : : stashed on the name's TREE_TYPE. */
8738 : 9514 : tree name = DECL_NAME (decl);
8739 : 9514 : tree_node (name);
8740 : 9514 : type = TREE_TYPE (name);
8741 : 9514 : tree_node (type);
8742 : : }
8743 : :
8744 : 17371 : int tag = insert (decl);
8745 : 17371 : if (streaming_p ())
8746 : 7770 : dump (dumper::TREE)
8747 : 27 : && dump ("Wrote tinfo_%s:%d %u %N", is_var ? "var" : "type",
8748 : : tag, ix, type);
8749 : :
8750 : 17371 : if (!is_var)
8751 : : {
8752 : 7857 : tag = insert (type);
8753 : 7857 : if (streaming_p ())
8754 : 3125 : dump (dumper::TREE)
8755 : 9 : && dump ("Wrote tinfo_type:%d %u %N", tag, ix, type);
8756 : : }
8757 : 17371 : return false;
8758 : : }
8759 : :
8760 : 527973 : if (DECL_NTTP_OBJECT_P (decl))
8761 : : {
8762 : : /* A NTTP parm object. */
8763 : 10 : if (streaming_p ())
8764 : 2 : i (tt_nttp_var);
8765 : 10 : tree_node (tparm_object_argument (decl));
8766 : 10 : tree_node (DECL_NAME (decl));
8767 : 10 : int tag = insert (decl);
8768 : 10 : if (streaming_p ())
8769 : 2 : dump (dumper::TREE)
8770 : 0 : && dump ("Wrote nttp object:%d %N", tag, DECL_NAME (decl));
8771 : 10 : return false;
8772 : : }
8773 : :
8774 : : break;
8775 : :
8776 : 5040475 : case TYPE_DECL:
8777 : 5040475 : if (DECL_TINFO_P (decl))
8778 : 7857 : goto tinfo;
8779 : : break;
8780 : : }
8781 : :
8782 : 12581899 : if (DECL_THUNK_P (decl))
8783 : : {
8784 : : /* Thunks are similar to binfos -- write the thunked-to decl and
8785 : : then thunk-specific key info. */
8786 : 0 : if (streaming_p ())
8787 : : {
8788 : 0 : i (tt_thunk);
8789 : 0 : i (THUNK_FIXED_OFFSET (decl));
8790 : : }
8791 : :
8792 : : tree target = decl;
8793 : 0 : while (DECL_THUNK_P (target))
8794 : 0 : target = THUNK_TARGET (target);
8795 : 0 : tree_node (target);
8796 : 0 : tree_node (THUNK_VIRTUAL_OFFSET (decl));
8797 : 0 : int tag = insert (decl);
8798 : 0 : if (streaming_p ())
8799 : 0 : dump (dumper::TREE)
8800 : 0 : && dump ("Wrote:%d thunk %N to %N", tag, DECL_NAME (decl), target);
8801 : 0 : return false;
8802 : : }
8803 : :
8804 : 12581899 : if (DECL_CLONED_FUNCTION_P (decl))
8805 : : {
8806 : 407685 : tree target = get_clone_target (decl);
8807 : 407685 : if (streaming_p ())
8808 : 192948 : i (tt_clone_ref);
8809 : :
8810 : 407685 : tree_node (target);
8811 : 407685 : tree_node (DECL_NAME (decl));
8812 : 407685 : if (DECL_VIRTUAL_P (decl))
8813 : 35928 : tree_node (DECL_VINDEX (decl));
8814 : 407685 : int tag = insert (decl);
8815 : 407685 : if (streaming_p ())
8816 : 192948 : dump (dumper::TREE)
8817 : 156 : && dump ("Wrote:%d clone %N of %N", tag, DECL_NAME (decl), target);
8818 : 407685 : return false;
8819 : : }
8820 : :
8821 : : /* Everything left should be a thing that is in the entity table.
8822 : : Mostly things that can be defined outside of their (original
8823 : : declaration) context. */
8824 : 12174214 : gcc_checking_assert (TREE_CODE (decl) == TEMPLATE_DECL
8825 : : || VAR_P (decl)
8826 : : || TREE_CODE (decl) == FUNCTION_DECL
8827 : : || TREE_CODE (decl) == TYPE_DECL
8828 : : || TREE_CODE (decl) == USING_DECL
8829 : : || TREE_CODE (decl) == CONCEPT_DECL
8830 : : || TREE_CODE (decl) == NAMESPACE_DECL);
8831 : :
8832 : 12174214 : int use_tpl = -1;
8833 : 12174214 : tree ti = node_template_info (decl, use_tpl);
8834 : 12174214 : tree tpl = NULL_TREE;
8835 : :
8836 : : /* If this is the TEMPLATE_DECL_RESULT of a TEMPLATE_DECL, get the
8837 : : TEMPLATE_DECL. Note TI_TEMPLATE is not a TEMPLATE_DECL for
8838 : : (some) friends, so we need to check that. */
8839 : : // FIXME: Should local friend template specializations be by value?
8840 : : // They don't get idents so we'll never know they're imported, but I
8841 : : // think we can only reach them from the TU that defines the
8842 : : // befriending class?
8843 : 4685871 : if (ti && TREE_CODE (TI_TEMPLATE (ti)) == TEMPLATE_DECL
8844 : 16860041 : && DECL_TEMPLATE_RESULT (TI_TEMPLATE (ti)) == decl)
8845 : : {
8846 : : tpl = TI_TEMPLATE (ti);
8847 : 1188127 : partial_template:
8848 : 1188127 : if (streaming_p ())
8849 : : {
8850 : 3321 : i (tt_template);
8851 : 3321 : dump (dumper::TREE)
8852 : 9 : && dump ("Writing implicit template %C:%N%S",
8853 : 9 : TREE_CODE (tpl), tpl, tpl);
8854 : : }
8855 : 1188127 : tree_node (tpl);
8856 : :
8857 : : /* Streaming TPL caused us to visit DECL and maybe its type. */
8858 : 1188127 : gcc_checking_assert (TREE_VISITED (decl));
8859 : 1188127 : if (DECL_IMPLICIT_TYPEDEF_P (decl))
8860 : 609896 : gcc_checking_assert (TREE_VISITED (TREE_TYPE (decl)));
8861 : : return false;
8862 : : }
8863 : :
8864 : 11126626 : tree ctx = CP_DECL_CONTEXT (decl);
8865 : 11126626 : depset *dep = NULL;
8866 : 11126626 : if (streaming_p ())
8867 : 1816204 : dep = dep_hash->find_dependency (decl);
8868 : 9310422 : else if (TREE_CODE (ctx) != FUNCTION_DECL
8869 : 302981 : || TREE_CODE (decl) == TEMPLATE_DECL
8870 : 273917 : || DECL_IMPLICIT_TYPEDEF_P (decl)
8871 : 9554665 : || (DECL_LANG_SPECIFIC (decl)
8872 : 188340 : && DECL_MODULE_IMPORT_P (decl)))
8873 : : {
8874 : 9066179 : auto kind = (TREE_CODE (decl) == NAMESPACE_DECL
8875 : 764520 : && !DECL_NAMESPACE_ALIAS (decl)
8876 : 9066179 : ? depset::EK_NAMESPACE : depset::EK_DECL);
8877 : 9066179 : dep = dep_hash->add_dependency (decl, kind);
8878 : : }
8879 : :
8880 : 10882383 : if (!dep)
8881 : : {
8882 : : /* Some internal entity of context. Do by value. */
8883 : 473095 : decl_value (decl, NULL);
8884 : 473095 : return false;
8885 : : }
8886 : :
8887 : 10653531 : if (dep->get_entity_kind () == depset::EK_REDIRECT)
8888 : : {
8889 : : /* The DECL_TEMPLATE_RESULT of a partial specialization.
8890 : : Write the partial specialization's template. */
8891 : 140539 : depset *redirect = dep->deps[0];
8892 : 140539 : gcc_checking_assert (redirect->get_entity_kind () == depset::EK_PARTIAL);
8893 : 140539 : tpl = redirect->get_entity ();
8894 : 140539 : goto partial_template;
8895 : : }
8896 : :
8897 : 10512992 : if (streaming_p ())
8898 : : {
8899 : : /* Locate the entity. */
8900 : 1587352 : unsigned index = dep->cluster;
8901 : 1587352 : unsigned import = 0;
8902 : :
8903 : 1587352 : if (dep->is_import ())
8904 : 626 : import = dep->section;
8905 : 1586726 : else if (CHECKING_P)
8906 : : /* It should be what we put there. */
8907 : 1586726 : gcc_checking_assert (index == ~import_entity_index (decl));
8908 : :
8909 : : #if CHECKING_P
8910 : 626 : gcc_assert (!import || importedness >= 0);
8911 : : #endif
8912 : 1587352 : i (tt_entity);
8913 : 1587352 : u (import);
8914 : 1587352 : u (index);
8915 : : }
8916 : :
8917 : 10512992 : int tag = insert (decl);
8918 : 10512992 : if (streaming_p () && dump (dumper::TREE))
8919 : : {
8920 : 510 : char const *kind = "import";
8921 : 510 : module_state *from = (*modules)[0];
8922 : 510 : if (dep->is_import ())
8923 : : /* Rediscover the unremapped index. */
8924 : 78 : from = import_entity_module (import_entity_index (decl));
8925 : : else
8926 : : {
8927 : 432 : tree o = get_originating_module_decl (decl);
8928 : 432 : o = STRIP_TEMPLATE (o);
8929 : 864 : kind = (DECL_LANG_SPECIFIC (o) && DECL_MODULE_PURVIEW_P (o)
8930 : 432 : ? "purview" : "GMF");
8931 : : }
8932 : 510 : dump ("Wrote %s:%d %C:%N@%M", kind,
8933 : 510 : tag, TREE_CODE (decl), decl, from);
8934 : : }
8935 : :
8936 : 10512992 : add_indirects (decl);
8937 : :
8938 : 10512992 : return false;
8939 : : }
8940 : :
8941 : : void
8942 : 12705353 : trees_out::type_node (tree type)
8943 : : {
8944 : 12705353 : gcc_assert (TYPE_P (type));
8945 : :
8946 : 12705353 : tree root = (TYPE_NAME (type)
8947 : 12705353 : ? TREE_TYPE (TYPE_NAME (type)) : TYPE_MAIN_VARIANT (type));
8948 : :
8949 : 12705353 : if (type != root)
8950 : : {
8951 : 2824792 : if (streaming_p ())
8952 : 597712 : i (tt_variant_type);
8953 : 2824792 : tree_node (root);
8954 : :
8955 : 2824792 : int flags = -1;
8956 : :
8957 : 2824792 : if (TREE_CODE (type) == FUNCTION_TYPE
8958 : 2824792 : || TREE_CODE (type) == METHOD_TYPE)
8959 : : {
8960 : 696228 : int quals = type_memfn_quals (type);
8961 : 696228 : int rquals = type_memfn_rqual (type);
8962 : 696228 : tree raises = TYPE_RAISES_EXCEPTIONS (type);
8963 : 696228 : bool late = TYPE_HAS_LATE_RETURN_TYPE (type);
8964 : :
8965 : 696228 : if (raises != TYPE_RAISES_EXCEPTIONS (root)
8966 : 19583 : || rquals != type_memfn_rqual (root)
8967 : 14347 : || quals != type_memfn_quals (root)
8968 : 710269 : || late != TYPE_HAS_LATE_RETURN_TYPE (root))
8969 : 696228 : flags = rquals | (int (late) << 2) | (quals << 3);
8970 : : }
8971 : : else
8972 : : {
8973 : 2128564 : if (TYPE_USER_ALIGN (type))
8974 : 22867 : flags = TYPE_ALIGN_RAW (type);
8975 : : }
8976 : :
8977 : 2824792 : if (streaming_p ())
8978 : 597712 : i (flags);
8979 : :
8980 : 2824792 : if (flags < 0)
8981 : : ;
8982 : 719095 : else if (TREE_CODE (type) == FUNCTION_TYPE
8983 : 719095 : || TREE_CODE (type) == METHOD_TYPE)
8984 : : {
8985 : 696228 : tree raises = TYPE_RAISES_EXCEPTIONS (type);
8986 : 696228 : if (raises == TYPE_RAISES_EXCEPTIONS (root))
8987 : 19583 : raises = error_mark_node;
8988 : 696228 : tree_node (raises);
8989 : : }
8990 : :
8991 : 2824792 : tree_node (TYPE_ATTRIBUTES (type));
8992 : :
8993 : 2824792 : if (streaming_p ())
8994 : : {
8995 : : /* Qualifiers. */
8996 : 597712 : int rquals = cp_type_quals (root);
8997 : 597712 : int quals = cp_type_quals (type);
8998 : 597712 : if (quals == rquals)
8999 : 294834 : quals = -1;
9000 : 597712 : i (quals);
9001 : : }
9002 : :
9003 : 2824792 : if (ref_node (type) != WK_none)
9004 : : {
9005 : 2824792 : int tag = insert (type);
9006 : 2824792 : if (streaming_p ())
9007 : : {
9008 : 597712 : i (0);
9009 : 597712 : dump (dumper::TREE)
9010 : 198 : && dump ("Wrote:%d variant type %C", tag, TREE_CODE (type));
9011 : : }
9012 : : }
9013 : 2824792 : return;
9014 : : }
9015 : :
9016 : 9880561 : if (tree name = TYPE_NAME (type))
9017 : 4005170 : if ((TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name))
9018 : 3129008 : || DECL_TEMPLATE_PARM_P (name)
9019 : 2166939 : || TREE_CODE (type) == RECORD_TYPE
9020 : 369398 : || TREE_CODE (type) == UNION_TYPE
9021 : 4364285 : || TREE_CODE (type) == ENUMERAL_TYPE)
9022 : : {
9023 : : /* We can meet template parms that we didn't meet in the
9024 : : tpl_parms walk, because we're referring to a derived type
9025 : : that was previously constructed from equivalent template
9026 : : parms. */
9027 : 3743862 : if (streaming_p ())
9028 : : {
9029 : 241385 : i (tt_typedef_type);
9030 : 241385 : dump (dumper::TREE)
9031 : 57 : && dump ("Writing %stypedef %C:%N",
9032 : 57 : DECL_IMPLICIT_TYPEDEF_P (name) ? "implicit " : "",
9033 : 57 : TREE_CODE (name), name);
9034 : : }
9035 : 3743862 : tree_node (name);
9036 : 3743862 : if (streaming_p ())
9037 : 241385 : dump (dumper::TREE) && dump ("Wrote typedef %C:%N%S",
9038 : 57 : TREE_CODE (name), name, name);
9039 : 3743862 : gcc_checking_assert (TREE_VISITED (type));
9040 : : return;
9041 : : }
9042 : :
9043 : 6136699 : if (TYPE_PTRMEMFUNC_P (type))
9044 : : {
9045 : : /* This is a distinct type node, masquerading as a structure. */
9046 : 10329 : tree fn_type = TYPE_PTRMEMFUNC_FN_TYPE (type);
9047 : 10329 : if (streaming_p ())
9048 : 3121 : i (tt_ptrmem_type);
9049 : 10329 : tree_node (fn_type);
9050 : 10329 : int tag = insert (type);
9051 : 10329 : if (streaming_p ())
9052 : 3124 : dump (dumper::TREE) && dump ("Written:%d ptrmem type", tag);
9053 : 10329 : return;
9054 : : }
9055 : :
9056 : 6126370 : if (streaming_p ())
9057 : : {
9058 : 1970849 : u (tt_derived_type);
9059 : 1970849 : u (TREE_CODE (type));
9060 : : }
9061 : :
9062 : 6126370 : tree_node (TREE_TYPE (type));
9063 : 6126370 : switch (TREE_CODE (type))
9064 : : {
9065 : 0 : default:
9066 : : /* We should never meet a type here that is indescribable in
9067 : : terms of other types. */
9068 : 0 : gcc_unreachable ();
9069 : :
9070 : 81656 : case ARRAY_TYPE:
9071 : 81656 : tree_node (TYPE_DOMAIN (type));
9072 : 81656 : if (streaming_p ())
9073 : : /* Dependent arrays are constructed with TYPE_DEPENENT_P
9074 : : already set. */
9075 : 26098 : u (TYPE_DEPENDENT_P (type));
9076 : : break;
9077 : :
9078 : : case COMPLEX_TYPE:
9079 : : /* No additional data. */
9080 : : break;
9081 : :
9082 : 12 : case BOOLEAN_TYPE:
9083 : : /* A non-standard boolean type. */
9084 : 12 : if (streaming_p ())
9085 : 6 : u (TYPE_PRECISION (type));
9086 : : break;
9087 : :
9088 : 78129 : case INTEGER_TYPE:
9089 : 78129 : if (TREE_TYPE (type))
9090 : : {
9091 : : /* A range type (representing an array domain). */
9092 : 74994 : tree_node (TYPE_MIN_VALUE (type));
9093 : 74994 : tree_node (TYPE_MAX_VALUE (type));
9094 : : }
9095 : : else
9096 : : {
9097 : : /* A new integral type (representing a bitfield). */
9098 : 3135 : if (streaming_p ())
9099 : : {
9100 : 841 : unsigned prec = TYPE_PRECISION (type);
9101 : 841 : bool unsigned_p = TYPE_UNSIGNED (type);
9102 : :
9103 : 841 : u ((prec << 1) | unsigned_p);
9104 : : }
9105 : : }
9106 : : break;
9107 : :
9108 : 1387730 : case METHOD_TYPE:
9109 : 1387730 : case FUNCTION_TYPE:
9110 : 1387730 : {
9111 : 1387730 : gcc_checking_assert (type_memfn_rqual (type) == REF_QUAL_NONE);
9112 : :
9113 : 1387730 : tree arg_types = TYPE_ARG_TYPES (type);
9114 : 1387730 : if (TREE_CODE (type) == METHOD_TYPE)
9115 : : {
9116 : 857003 : tree_node (TREE_TYPE (TREE_VALUE (arg_types)));
9117 : 857003 : arg_types = TREE_CHAIN (arg_types);
9118 : : }
9119 : 1387730 : tree_node (arg_types);
9120 : : }
9121 : 1387730 : break;
9122 : :
9123 : 2083 : case OFFSET_TYPE:
9124 : 2083 : tree_node (TYPE_OFFSET_BASETYPE (type));
9125 : 2083 : break;
9126 : :
9127 : : case POINTER_TYPE:
9128 : : /* No additional data. */
9129 : : break;
9130 : :
9131 : 1029173 : case REFERENCE_TYPE:
9132 : 1029173 : if (streaming_p ())
9133 : 224037 : u (TYPE_REF_IS_RVALUE (type));
9134 : : break;
9135 : :
9136 : 1218014 : case DECLTYPE_TYPE:
9137 : 1218014 : case TYPEOF_TYPE:
9138 : 1218014 : case DEPENDENT_OPERATOR_TYPE:
9139 : 1218014 : tree_node (TYPE_VALUES_RAW (type));
9140 : 1218014 : if (TREE_CODE (type) == DECLTYPE_TYPE)
9141 : : /* We stash a whole bunch of things into decltype's
9142 : : flags. */
9143 : 79958 : if (streaming_p ())
9144 : 27246 : tree_node_bools (type);
9145 : : break;
9146 : :
9147 : 730 : case TRAIT_TYPE:
9148 : 730 : tree_node (TRAIT_TYPE_KIND_RAW (type));
9149 : 730 : tree_node (TRAIT_TYPE_TYPE1 (type));
9150 : 730 : tree_node (TRAIT_TYPE_TYPE2 (type));
9151 : 730 : break;
9152 : :
9153 : : case TYPE_ARGUMENT_PACK:
9154 : : /* No additional data. */
9155 : : break;
9156 : :
9157 : 205967 : case TYPE_PACK_EXPANSION:
9158 : 205967 : if (streaming_p ())
9159 : 84020 : u (PACK_EXPANSION_LOCAL_P (type));
9160 : 411934 : tree_node (PACK_EXPANSION_PARAMETER_PACKS (type));
9161 : 205967 : tree_node (PACK_EXPANSION_EXTRA_ARGS (type));
9162 : 205967 : break;
9163 : :
9164 : 263967 : case TYPENAME_TYPE:
9165 : 263967 : {
9166 : 263967 : tree_node (TYPE_CONTEXT (type));
9167 : 263967 : tree_node (DECL_NAME (TYPE_NAME (type)));
9168 : 263967 : tree_node (TYPENAME_TYPE_FULLNAME (type));
9169 : 263967 : if (streaming_p ())
9170 : : {
9171 : 94834 : enum tag_types tag_type = none_type;
9172 : 94834 : if (TYPENAME_IS_ENUM_P (type))
9173 : : tag_type = enum_type;
9174 : 94834 : else if (TYPENAME_IS_CLASS_P (type))
9175 : 0 : tag_type = class_type;
9176 : 94834 : u (int (tag_type));
9177 : : }
9178 : : }
9179 : : break;
9180 : :
9181 : 309 : case UNBOUND_CLASS_TEMPLATE:
9182 : 309 : {
9183 : 309 : tree decl = TYPE_NAME (type);
9184 : 309 : tree_node (DECL_CONTEXT (decl));
9185 : 309 : tree_node (DECL_NAME (decl));
9186 : 309 : tree_node (DECL_TEMPLATE_PARMS (decl));
9187 : : }
9188 : 309 : break;
9189 : :
9190 : 42 : case VECTOR_TYPE:
9191 : 42 : if (streaming_p ())
9192 : : {
9193 : 21 : poly_uint64 nunits = TYPE_VECTOR_SUBPARTS (type);
9194 : 42 : for (unsigned ix = 0; ix != NUM_POLY_INT_COEFFS; ix++)
9195 : 21 : wu (nunits.coeffs[ix]);
9196 : : }
9197 : : break;
9198 : : }
9199 : :
9200 : : /* We may have met the type during emitting the above. */
9201 : 6126370 : if (ref_node (type) != WK_none)
9202 : : {
9203 : 5557342 : int tag = insert (type);
9204 : 5557342 : if (streaming_p ())
9205 : : {
9206 : 1700552 : i (0);
9207 : 1700552 : dump (dumper::TREE)
9208 : 543 : && dump ("Wrote:%d derived type %C", tag, TREE_CODE (type));
9209 : : }
9210 : : }
9211 : :
9212 : : return;
9213 : : }
9214 : :
9215 : : /* T is (mostly*) a non-mergeable node that must be written by value.
9216 : : The mergeable case is a BINFO, which are as-if DECLSs. */
9217 : :
9218 : : void
9219 : 35973215 : trees_out::tree_value (tree t)
9220 : : {
9221 : : /* We should never be writing a type by value. tree_type should
9222 : : have streamed it, or we're going via its TYPE_DECL. */
9223 : 35973215 : gcc_checking_assert (!TYPE_P (t));
9224 : :
9225 : 35973215 : if (DECL_P (t))
9226 : : /* No template, type, var or function, except anonymous
9227 : : non-context vars. */
9228 : 809054 : gcc_checking_assert ((TREE_CODE (t) != TEMPLATE_DECL
9229 : : && TREE_CODE (t) != TYPE_DECL
9230 : : && (TREE_CODE (t) != VAR_DECL
9231 : : || (!DECL_NAME (t) && !DECL_CONTEXT (t)))
9232 : : && TREE_CODE (t) != FUNCTION_DECL));
9233 : :
9234 : 35973215 : if (streaming_p ())
9235 : : {
9236 : : /* A new node -> tt_node. */
9237 : 14206991 : tree_val_count++;
9238 : 14206991 : i (tt_node);
9239 : 14206991 : start (t);
9240 : 14206991 : tree_node_bools (t);
9241 : : }
9242 : :
9243 : 35973215 : if (TREE_CODE (t) == TREE_BINFO)
9244 : : /* Binfos are decl-like and need merging information. */
9245 : 321684 : binfo_mergeable (t);
9246 : :
9247 : 35973215 : int tag = insert (t, WK_value);
9248 : 35973215 : if (streaming_p ())
9249 : 14206991 : dump (dumper::TREE)
9250 : 2484 : && dump ("Writing tree:%d %C:%N", tag, TREE_CODE (t), t);
9251 : :
9252 : 35973215 : tree_node_vals (t);
9253 : :
9254 : 35973215 : if (streaming_p ())
9255 : 14209475 : dump (dumper::TREE) && dump ("Written tree:%d %C:%N", tag, TREE_CODE (t), t);
9256 : 35973215 : }
9257 : :
9258 : : tree
9259 : 15429461 : trees_in::tree_value ()
9260 : : {
9261 : 15429461 : tree t = start ();
9262 : 15429461 : if (!t || !tree_node_bools (t))
9263 : 0 : return NULL_TREE;
9264 : :
9265 : 15429461 : tree existing = t;
9266 : 15429461 : if (TREE_CODE (t) == TREE_BINFO)
9267 : : {
9268 : 156657 : tree type;
9269 : 156657 : unsigned ix = binfo_mergeable (&type);
9270 : 156657 : if (TYPE_BINFO (type))
9271 : : {
9272 : : /* We already have a definition, this must be a duplicate. */
9273 : 93027 : dump (dumper::MERGE)
9274 : 220 : && dump ("Deduping binfo %N[%u]", type, ix);
9275 : 93027 : existing = TYPE_BINFO (type);
9276 : 136582 : while (existing && ix--)
9277 : 43555 : existing = TREE_CHAIN (existing);
9278 : 93027 : if (existing)
9279 : 93027 : register_duplicate (t, existing);
9280 : : else
9281 : : /* Error, mismatch -- diagnose in read_class_def's
9282 : : checking. */
9283 : : existing = t;
9284 : : }
9285 : : }
9286 : :
9287 : : /* Insert into map. */
9288 : 15429461 : int tag = insert (existing);
9289 : 15429461 : dump (dumper::TREE)
9290 : 3372 : && dump ("Reading tree:%d %C", tag, TREE_CODE (t));
9291 : :
9292 : 15429461 : if (!tree_node_vals (t))
9293 : : {
9294 : 0 : back_refs[~tag] = NULL_TREE;
9295 : 0 : set_overrun ();
9296 : : /* Bail. */
9297 : 0 : return NULL_TREE;
9298 : : }
9299 : :
9300 : 15429461 : if (TREE_CODE (t) == LAMBDA_EXPR
9301 : 15429461 : && CLASSTYPE_LAMBDA_EXPR (TREE_TYPE (t)))
9302 : : {
9303 : 3147 : existing = CLASSTYPE_LAMBDA_EXPR (TREE_TYPE (t));
9304 : 3147 : back_refs[~tag] = existing;
9305 : : }
9306 : :
9307 : 15432833 : dump (dumper::TREE) && dump ("Read tree:%d %C:%N", tag, TREE_CODE (t), t);
9308 : :
9309 : 15429461 : if (TREE_CODE (existing) == INTEGER_CST && !TREE_OVERFLOW (existing))
9310 : : {
9311 : 765504 : existing = cache_integer_cst (t, true);
9312 : 765504 : back_refs[~tag] = existing;
9313 : : }
9314 : :
9315 : : return existing;
9316 : : }
9317 : :
9318 : : /* Stream out tree node T. We automatically create local back
9319 : : references, which is essentially a single pass lisp
9320 : : self-referential structure pretty-printer. */
9321 : :
9322 : : void
9323 : 323342264 : trees_out::tree_node (tree t)
9324 : : {
9325 : 323342264 : dump.indent ();
9326 : 323342264 : walk_kind ref = ref_node (t);
9327 : 323342264 : if (ref == WK_none)
9328 : 247843318 : goto done;
9329 : :
9330 : 75498946 : if (ref != WK_normal)
9331 : 1957058 : goto skip_normal;
9332 : :
9333 : 73541888 : if (TREE_CODE (t) == IDENTIFIER_NODE)
9334 : : {
9335 : : /* An identifier node -> tt_id, tt_conv_id, tt_anon_id, tt_lambda_id. */
9336 : 9386427 : int code = tt_id;
9337 : 9386427 : if (IDENTIFIER_ANON_P (t))
9338 : 30937 : code = IDENTIFIER_LAMBDA_P (t) ? tt_lambda_id : tt_anon_id;
9339 : 18266455 : else if (IDENTIFIER_CONV_OP_P (t))
9340 : 11631 : code = tt_conv_id;
9341 : :
9342 : 9386427 : if (streaming_p ())
9343 : 1870425 : i (code);
9344 : :
9345 : 9386427 : if (code == tt_conv_id)
9346 : : {
9347 : 11631 : tree type = TREE_TYPE (t);
9348 : 11631 : gcc_checking_assert (type || t == conv_op_identifier);
9349 : 11631 : tree_node (type);
9350 : : }
9351 : 9374796 : else if (code == tt_id && streaming_p ())
9352 : 1856811 : str (IDENTIFIER_POINTER (t), IDENTIFIER_LENGTH (t));
9353 : :
9354 : 9386427 : int tag = insert (t);
9355 : 9386427 : if (streaming_p ())
9356 : : {
9357 : : /* We know the ordering of the 4 id tags. */
9358 : 1870425 : static const char *const kinds[] =
9359 : : {"", "conv_op ", "anon ", "lambda "};
9360 : 1870425 : dump (dumper::TREE)
9361 : 1005 : && dump ("Written:%d %sidentifier:%N", tag,
9362 : 1002 : kinds[code - tt_id],
9363 : 3 : code == tt_conv_id ? TREE_TYPE (t) : t);
9364 : : }
9365 : 9386427 : goto done;
9366 : : }
9367 : :
9368 : 64155461 : if (TREE_CODE (t) == TREE_BINFO)
9369 : : {
9370 : : /* A BINFO -> tt_binfo.
9371 : : We must do this by reference. We stream the binfo tree
9372 : : itself when streaming its owning RECORD_TYPE. That we got
9373 : : here means the dominating type is not in this SCC. */
9374 : 71294 : if (streaming_p ())
9375 : 1981 : i (tt_binfo);
9376 : 71294 : binfo_mergeable (t);
9377 : 71294 : gcc_checking_assert (!TREE_VISITED (t));
9378 : 71294 : int tag = insert (t);
9379 : 71294 : if (streaming_p ())
9380 : 1981 : dump (dumper::TREE) && dump ("Inserting binfo:%d %N", tag, t);
9381 : 71294 : goto done;
9382 : : }
9383 : :
9384 : 64084167 : if (TREE_CODE (t) == INTEGER_CST
9385 : 4622306 : && !TREE_OVERFLOW (t)
9386 : 68706473 : && TREE_CODE (TREE_TYPE (t)) == ENUMERAL_TYPE)
9387 : : {
9388 : : /* An integral constant of enumeral type. See if it matches one
9389 : : of the enumeration values. */
9390 : 31524 : for (tree values = TYPE_VALUES (TREE_TYPE (t));
9391 : 292661 : values; values = TREE_CHAIN (values))
9392 : : {
9393 : 290802 : tree decl = TREE_VALUE (values);
9394 : 290802 : if (tree_int_cst_equal (DECL_INITIAL (decl), t))
9395 : : {
9396 : 29665 : if (streaming_p ())
9397 : 8273 : u (tt_enum_value);
9398 : 29665 : tree_node (decl);
9399 : 29707 : dump (dumper::TREE) && dump ("Written enum value %N", decl);
9400 : 29665 : goto done;
9401 : : }
9402 : : }
9403 : : /* It didn't match. We'll write it a an explicit INTEGER_CST
9404 : : node. */
9405 : : }
9406 : :
9407 : 64054502 : if (TYPE_P (t))
9408 : : {
9409 : 12705353 : type_node (t);
9410 : 12705353 : goto done;
9411 : : }
9412 : :
9413 : 51349149 : if (DECL_P (t))
9414 : : {
9415 : 16546552 : if (DECL_TEMPLATE_PARM_P (t))
9416 : : {
9417 : 2647894 : tpl_parm_value (t);
9418 : 2647894 : goto done;
9419 : : }
9420 : :
9421 : 13898658 : if (!DECL_CONTEXT (t))
9422 : : {
9423 : : /* There are a few cases of decls with no context. We'll write
9424 : : these by value, but first assert they are cases we expect. */
9425 : 23856 : gcc_checking_assert (ref == WK_normal);
9426 : 23856 : switch (TREE_CODE (t))
9427 : : {
9428 : 0 : default: gcc_unreachable ();
9429 : :
9430 : 9476 : case LABEL_DECL:
9431 : : /* CASE_LABEL_EXPRs contain uncontexted LABEL_DECLs. */
9432 : 9476 : gcc_checking_assert (!DECL_NAME (t));
9433 : : break;
9434 : :
9435 : 412 : case VAR_DECL:
9436 : : /* AGGR_INIT_EXPRs cons up anonymous uncontexted VAR_DECLs. */
9437 : 412 : gcc_checking_assert (!DECL_NAME (t)
9438 : : && DECL_ARTIFICIAL (t));
9439 : : break;
9440 : :
9441 : : case PARM_DECL:
9442 : : /* REQUIRES_EXPRs have a tree list of uncontexted
9443 : : PARM_DECLS. It'd be nice if they had a
9444 : : distinguishing flag to double check. */
9445 : : break;
9446 : : }
9447 : 23856 : goto by_value;
9448 : : }
9449 : : }
9450 : :
9451 : 34802597 : skip_normal:
9452 : 50634457 : if (DECL_P (t) && !decl_node (t, ref))
9453 : 14685098 : goto done;
9454 : :
9455 : : /* Otherwise by value */
9456 : 35973215 : by_value:
9457 : 35973215 : tree_value (t);
9458 : :
9459 : 323342264 : done:
9460 : : /* And, breath out. */
9461 : 323342264 : dump.outdent ();
9462 : 323342264 : }
9463 : :
9464 : : /* Stream in a tree node. */
9465 : :
9466 : : tree
9467 : 134597612 : trees_in::tree_node (bool is_use)
9468 : : {
9469 : 134597612 : if (get_overrun ())
9470 : : return NULL_TREE;
9471 : :
9472 : 134597612 : dump.indent ();
9473 : 134597612 : int tag = i ();
9474 : 134597612 : tree res = NULL_TREE;
9475 : 134597612 : switch (tag)
9476 : : {
9477 : 44645872 : default:
9478 : : /* backref, pull it out of the map. */
9479 : 44645872 : res = back_ref (tag);
9480 : 44645872 : break;
9481 : :
9482 : : case tt_null:
9483 : : /* NULL_TREE. */
9484 : : break;
9485 : :
9486 : 10346335 : case tt_fixed:
9487 : : /* A fixed ref, find it in the fixed_ref array. */
9488 : 10346335 : {
9489 : 10346335 : unsigned fix = u ();
9490 : 10346335 : if (fix < (*fixed_trees).length ())
9491 : : {
9492 : 10346335 : res = (*fixed_trees)[fix];
9493 : 10346335 : dump (dumper::TREE) && dump ("Read fixed:%u %C:%N%S", fix,
9494 : 4953 : TREE_CODE (res), res, res);
9495 : : }
9496 : :
9497 : 10341382 : if (!res)
9498 : 0 : set_overrun ();
9499 : : }
9500 : : break;
9501 : :
9502 : 83820 : case tt_parm:
9503 : 83820 : {
9504 : 83820 : tree fn = tree_node ();
9505 : 83820 : if (fn && TREE_CODE (fn) == FUNCTION_DECL)
9506 : 83820 : res = tree_node ();
9507 : 83820 : if (res)
9508 : 83820 : dump (dumper::TREE)
9509 : 15 : && dump ("Read %s reference %N",
9510 : 15 : TREE_CODE (res) == PARM_DECL ? "parameter" : "result",
9511 : : res);
9512 : : }
9513 : : break;
9514 : :
9515 : 15429461 : case tt_node:
9516 : : /* A new node. Stream it in. */
9517 : 15429461 : res = tree_value ();
9518 : 15429461 : break;
9519 : :
9520 : 1768403 : case tt_decl:
9521 : : /* A new decl. Stream it in. */
9522 : 1768403 : res = decl_value ();
9523 : 1768403 : break;
9524 : :
9525 : 617702 : case tt_tpl_parm:
9526 : : /* A template parameter. Stream it in. */
9527 : 617702 : res = tpl_parm_value ();
9528 : 617702 : break;
9529 : :
9530 : 1913915 : case tt_id:
9531 : : /* An identifier node. */
9532 : 1913915 : {
9533 : 1913915 : size_t l;
9534 : 1913915 : const char *chars = str (&l);
9535 : 1913915 : res = get_identifier_with_length (chars, l);
9536 : 1913915 : int tag = insert (res);
9537 : 1913915 : dump (dumper::TREE)
9538 : 1392 : && dump ("Read identifier:%d %N", tag, res);
9539 : : }
9540 : 1913915 : break;
9541 : :
9542 : 3877 : case tt_conv_id:
9543 : : /* A conversion operator. Get the type and recreate the
9544 : : identifier. */
9545 : 3877 : {
9546 : 3877 : tree type = tree_node ();
9547 : 3877 : if (!get_overrun ())
9548 : : {
9549 : 3877 : res = type ? make_conv_op_name (type) : conv_op_identifier;
9550 : 3877 : int tag = insert (res);
9551 : 3877 : dump (dumper::TREE)
9552 : 27 : && dump ("Created conv_op:%d %S for %N", tag, res, type);
9553 : : }
9554 : : }
9555 : : break;
9556 : :
9557 : 9735 : case tt_anon_id:
9558 : 9735 : case tt_lambda_id:
9559 : : /* An anonymous or lambda id. */
9560 : 9735 : {
9561 : 9735 : res = make_anon_name ();
9562 : 9735 : if (tag == tt_lambda_id)
9563 : 4638 : IDENTIFIER_LAMBDA_P (res) = true;
9564 : 9735 : int tag = insert (res);
9565 : 9738 : dump (dumper::TREE)
9566 : 3 : && dump ("Read %s identifier:%d %N",
9567 : 3 : IDENTIFIER_LAMBDA_P (res) ? "lambda" : "anon", tag, res);
9568 : : }
9569 : : break;
9570 : :
9571 : 260207 : case tt_typedef_type:
9572 : 260207 : res = tree_node ();
9573 : 260207 : if (res)
9574 : : {
9575 : 260207 : dump (dumper::TREE)
9576 : 72 : && dump ("Read %stypedef %C:%N",
9577 : 72 : DECL_IMPLICIT_TYPEDEF_P (res) ? "implicit " : "",
9578 : 72 : TREE_CODE (res), res);
9579 : 260207 : res = TREE_TYPE (res);
9580 : : }
9581 : : break;
9582 : :
9583 : 2131493 : case tt_derived_type:
9584 : : /* A type derived from some other type. */
9585 : 2131493 : {
9586 : 2131493 : enum tree_code code = tree_code (u ());
9587 : 2131493 : res = tree_node ();
9588 : :
9589 : 2131493 : switch (code)
9590 : : {
9591 : 0 : default:
9592 : 0 : set_overrun ();
9593 : 0 : break;
9594 : :
9595 : 27121 : case ARRAY_TYPE:
9596 : 27121 : {
9597 : 27121 : tree domain = tree_node ();
9598 : 27121 : int dep = u ();
9599 : 27121 : if (!get_overrun ())
9600 : 27121 : res = build_cplus_array_type (res, domain, dep);
9601 : : }
9602 : : break;
9603 : :
9604 : 102 : case COMPLEX_TYPE:
9605 : 102 : if (!get_overrun ())
9606 : 102 : res = build_complex_type (res);
9607 : : break;
9608 : :
9609 : 9 : case BOOLEAN_TYPE:
9610 : 9 : {
9611 : 9 : unsigned precision = u ();
9612 : 9 : if (!get_overrun ())
9613 : 9 : res = build_nonstandard_boolean_type (precision);
9614 : : }
9615 : : break;
9616 : :
9617 : 24819 : case INTEGER_TYPE:
9618 : 24819 : if (res)
9619 : : {
9620 : : /* A range type (representing an array domain). */
9621 : 23999 : tree min = tree_node ();
9622 : 23999 : tree max = tree_node ();
9623 : :
9624 : 23999 : if (!get_overrun ())
9625 : 23999 : res = build_range_type (res, min, max);
9626 : : }
9627 : : else
9628 : : {
9629 : : /* A new integral type (representing a bitfield). */
9630 : 820 : unsigned enc = u ();
9631 : 820 : if (!get_overrun ())
9632 : 820 : res = build_nonstandard_integer_type (enc >> 1, enc & 1);
9633 : : }
9634 : : break;
9635 : :
9636 : 642160 : case FUNCTION_TYPE:
9637 : 642160 : case METHOD_TYPE:
9638 : 642160 : {
9639 : 642160 : tree klass = code == METHOD_TYPE ? tree_node () : NULL_TREE;
9640 : 642160 : tree args = tree_node ();
9641 : 642160 : if (!get_overrun ())
9642 : : {
9643 : 642160 : if (klass)
9644 : 390060 : res = build_method_type_directly (klass, res, args);
9645 : : else
9646 : 252100 : res = build_function_type (res, args);
9647 : : }
9648 : : }
9649 : : break;
9650 : :
9651 : 560 : case OFFSET_TYPE:
9652 : 560 : {
9653 : 560 : tree base = tree_node ();
9654 : 560 : if (!get_overrun ())
9655 : 560 : res = build_offset_type (base, res);
9656 : : }
9657 : : break;
9658 : :
9659 : 295593 : case POINTER_TYPE:
9660 : 295593 : if (!get_overrun ())
9661 : 295593 : res = build_pointer_type (res);
9662 : : break;
9663 : :
9664 : 235289 : case REFERENCE_TYPE:
9665 : 235289 : {
9666 : 235289 : bool rval = bool (u ());
9667 : 235289 : if (!get_overrun ())
9668 : 235289 : res = cp_build_reference_type (res, rval);
9669 : : }
9670 : : break;
9671 : :
9672 : 640335 : case DECLTYPE_TYPE:
9673 : 640335 : case TYPEOF_TYPE:
9674 : 640335 : case DEPENDENT_OPERATOR_TYPE:
9675 : 640335 : {
9676 : 640335 : tree expr = tree_node ();
9677 : 640335 : if (!get_overrun ())
9678 : : {
9679 : 640335 : res = cxx_make_type (code);
9680 : 640335 : TYPE_VALUES_RAW (res) = expr;
9681 : 640335 : if (code == DECLTYPE_TYPE)
9682 : 27784 : tree_node_bools (res);
9683 : 640335 : SET_TYPE_STRUCTURAL_EQUALITY (res);
9684 : : }
9685 : : }
9686 : : break;
9687 : :
9688 : 346 : case TRAIT_TYPE:
9689 : 346 : {
9690 : 346 : tree kind = tree_node ();
9691 : 346 : tree type1 = tree_node ();
9692 : 346 : tree type2 = tree_node ();
9693 : 346 : if (!get_overrun ())
9694 : : {
9695 : 346 : res = cxx_make_type (TRAIT_TYPE);
9696 : 346 : TRAIT_TYPE_KIND_RAW (res) = kind;
9697 : 346 : TRAIT_TYPE_TYPE1 (res) = type1;
9698 : 346 : TRAIT_TYPE_TYPE2 (res) = type2;
9699 : 346 : SET_TYPE_STRUCTURAL_EQUALITY (res);
9700 : : }
9701 : : }
9702 : : break;
9703 : :
9704 : 84119 : case TYPE_ARGUMENT_PACK:
9705 : 84119 : if (!get_overrun ())
9706 : : {
9707 : 84119 : tree pack = cxx_make_type (TYPE_ARGUMENT_PACK);
9708 : 84119 : ARGUMENT_PACK_ARGS (pack) = res;
9709 : : res = pack;
9710 : : }
9711 : : break;
9712 : :
9713 : 82717 : case TYPE_PACK_EXPANSION:
9714 : 82717 : {
9715 : 82717 : bool local = u ();
9716 : 82717 : tree param_packs = tree_node ();
9717 : 82717 : tree extra_args = tree_node ();
9718 : 82717 : if (!get_overrun ())
9719 : : {
9720 : 82717 : tree expn = cxx_make_type (TYPE_PACK_EXPANSION);
9721 : 82717 : SET_TYPE_STRUCTURAL_EQUALITY (expn);
9722 : 82717 : PACK_EXPANSION_PATTERN (expn) = res;
9723 : 165434 : PACK_EXPANSION_PARAMETER_PACKS (expn) = param_packs;
9724 : 82717 : PACK_EXPANSION_EXTRA_ARGS (expn) = extra_args;
9725 : 82717 : PACK_EXPANSION_LOCAL_P (expn) = local;
9726 : 82717 : res = expn;
9727 : : }
9728 : : }
9729 : : break;
9730 : :
9731 : 98224 : case TYPENAME_TYPE:
9732 : 98224 : {
9733 : 98224 : tree ctx = tree_node ();
9734 : 98224 : tree name = tree_node ();
9735 : 98224 : tree fullname = tree_node ();
9736 : 98224 : enum tag_types tag_type = tag_types (u ());
9737 : :
9738 : 98224 : if (!get_overrun ())
9739 : 98224 : res = build_typename_type (ctx, name, fullname, tag_type);
9740 : : }
9741 : : break;
9742 : :
9743 : 69 : case UNBOUND_CLASS_TEMPLATE:
9744 : 69 : {
9745 : 69 : tree ctx = tree_node ();
9746 : 69 : tree name = tree_node ();
9747 : 69 : tree parms = tree_node ();
9748 : :
9749 : 69 : if (!get_overrun ())
9750 : 69 : res = make_unbound_class_template_raw (ctx, name, parms);
9751 : : }
9752 : : break;
9753 : :
9754 : : case VECTOR_TYPE:
9755 : : {
9756 : : poly_uint64 nunits;
9757 : 60 : for (unsigned ix = 0; ix != NUM_POLY_INT_COEFFS; ix++)
9758 : 30 : nunits.coeffs[ix] = wu ();
9759 : 30 : if (!get_overrun ())
9760 : 30 : res = build_vector_type (res, nunits);
9761 : : }
9762 : : break;
9763 : : }
9764 : :
9765 : 2131493 : int tag = i ();
9766 : 2131493 : if (!tag)
9767 : : {
9768 : 1825219 : tag = insert (res);
9769 : 1825219 : if (res)
9770 : 1825219 : dump (dumper::TREE)
9771 : 663 : && dump ("Created:%d derived type %C", tag, code);
9772 : : }
9773 : : else
9774 : 306274 : res = back_ref (tag);
9775 : : }
9776 : : break;
9777 : :
9778 : 642076 : case tt_variant_type:
9779 : : /* Variant of some type. */
9780 : 642076 : {
9781 : 642076 : res = tree_node ();
9782 : 642076 : int flags = i ();
9783 : 642076 : if (get_overrun ())
9784 : : ;
9785 : 642076 : else if (flags < 0)
9786 : : /* No change. */;
9787 : 327879 : else if (TREE_CODE (res) == FUNCTION_TYPE
9788 : 327879 : || TREE_CODE (res) == METHOD_TYPE)
9789 : : {
9790 : 325095 : cp_ref_qualifier rqual = cp_ref_qualifier (flags & 3);
9791 : 325095 : bool late = (flags >> 2) & 1;
9792 : 325095 : cp_cv_quals quals = cp_cv_quals (flags >> 3);
9793 : :
9794 : 325095 : tree raises = tree_node ();
9795 : 325095 : if (raises == error_mark_node)
9796 : 10216 : raises = TYPE_RAISES_EXCEPTIONS (res);
9797 : :
9798 : 325095 : res = build_cp_fntype_variant (res, rqual, raises, late);
9799 : 325095 : if (TREE_CODE (res) == FUNCTION_TYPE)
9800 : 126344 : res = apply_memfn_quals (res, quals, rqual);
9801 : : }
9802 : : else
9803 : : {
9804 : 2784 : res = build_aligned_type (res, (1u << flags) >> 1);
9805 : 2784 : TYPE_USER_ALIGN (res) = true;
9806 : : }
9807 : :
9808 : 642076 : if (tree attribs = tree_node ())
9809 : 20907 : res = cp_build_type_attribute_variant (res, attribs);
9810 : :
9811 : 642076 : int quals = i ();
9812 : 642076 : if (quals >= 0 && !get_overrun ())
9813 : 316336 : res = cp_build_qualified_type (res, quals);
9814 : :
9815 : 642076 : int tag = i ();
9816 : 642076 : if (!tag)
9817 : : {
9818 : 642076 : tag = insert (res);
9819 : 642076 : if (res)
9820 : 642076 : dump (dumper::TREE)
9821 : 288 : && dump ("Created:%d variant type %C", tag, TREE_CODE (res));
9822 : : }
9823 : : else
9824 : 0 : res = back_ref (tag);
9825 : : }
9826 : : break;
9827 : :
9828 : 8448 : case tt_tinfo_var:
9829 : 8448 : case tt_tinfo_typedef:
9830 : : /* A tinfo var or typedef. */
9831 : 8448 : {
9832 : 8448 : bool is_var = tag == tt_tinfo_var;
9833 : 8448 : unsigned ix = u ();
9834 : 8448 : tree type = NULL_TREE;
9835 : :
9836 : 8448 : if (is_var)
9837 : : {
9838 : 5076 : tree name = tree_node ();
9839 : 5076 : type = tree_node ();
9840 : :
9841 : 5076 : if (!get_overrun ())
9842 : 5076 : res = get_tinfo_decl_direct (type, name, int (ix));
9843 : : }
9844 : : else
9845 : : {
9846 : 3372 : if (!get_overrun ())
9847 : : {
9848 : 3372 : type = get_pseudo_tinfo_type (ix);
9849 : 3372 : res = TYPE_NAME (type);
9850 : : }
9851 : : }
9852 : 8448 : if (res)
9853 : : {
9854 : 8448 : int tag = insert (res);
9855 : 8448 : dump (dumper::TREE)
9856 : 36 : && dump ("Created tinfo_%s:%d %S:%u for %N",
9857 : : is_var ? "var" : "decl", tag, res, ix, type);
9858 : 8448 : if (!is_var)
9859 : : {
9860 : 3372 : tag = insert (type);
9861 : 3372 : dump (dumper::TREE)
9862 : 12 : && dump ("Created tinfo_type:%d %u %N", tag, ix, type);
9863 : : }
9864 : : }
9865 : : }
9866 : : break;
9867 : :
9868 : 2726 : case tt_ptrmem_type:
9869 : : /* A pointer to member function. */
9870 : 2726 : {
9871 : 2726 : tree type = tree_node ();
9872 : 2726 : if (type && TREE_CODE (type) == POINTER_TYPE
9873 : 5452 : && TREE_CODE (TREE_TYPE (type)) == METHOD_TYPE)
9874 : : {
9875 : 2726 : res = build_ptrmemfunc_type (type);
9876 : 2726 : int tag = insert (res);
9877 : 2729 : dump (dumper::TREE) && dump ("Created:%d ptrmem type", tag);
9878 : : }
9879 : : else
9880 : 0 : set_overrun ();
9881 : : }
9882 : : break;
9883 : :
9884 : 2 : case tt_nttp_var:
9885 : : /* An NTTP object. */
9886 : 2 : {
9887 : 2 : tree init = tree_node ();
9888 : 2 : tree name = tree_node ();
9889 : 2 : if (!get_overrun ())
9890 : : {
9891 : 2 : res = get_template_parm_object (init, name);
9892 : 2 : int tag = insert (res);
9893 : 2 : dump (dumper::TREE)
9894 : 0 : && dump ("Created nttp object:%d %N", tag, name);
9895 : : }
9896 : : }
9897 : : break;
9898 : :
9899 : 9552 : case tt_enum_value:
9900 : : /* An enum const value. */
9901 : 9552 : {
9902 : 9552 : if (tree decl = tree_node ())
9903 : : {
9904 : 9570 : dump (dumper::TREE) && dump ("Read enum value %N", decl);
9905 : 9552 : res = DECL_INITIAL (decl);
9906 : : }
9907 : :
9908 : 9552 : if (!res)
9909 : 0 : set_overrun ();
9910 : : }
9911 : : break;
9912 : :
9913 : 11195 : case tt_enum_decl:
9914 : : /* An enum decl. */
9915 : 11195 : {
9916 : 11195 : tree ctx = tree_node ();
9917 : 11195 : tree name = tree_node ();
9918 : :
9919 : 11195 : if (!get_overrun ()
9920 : 11195 : && TREE_CODE (ctx) == ENUMERAL_TYPE)
9921 : 11195 : res = find_enum_member (ctx, name);
9922 : :
9923 : 11195 : if (!res)
9924 : 0 : set_overrun ();
9925 : : else
9926 : : {
9927 : 11195 : int tag = insert (res);
9928 : 11195 : dump (dumper::TREE)
9929 : 18 : && dump ("Read enum decl:%d %C:%N", tag, TREE_CODE (res), res);
9930 : : }
9931 : : }
9932 : : break;
9933 : :
9934 : 10269 : case tt_data_member:
9935 : : /* A data member. */
9936 : 10269 : {
9937 : 10269 : tree ctx = tree_node ();
9938 : 10269 : tree name = tree_node ();
9939 : :
9940 : 10269 : if (!get_overrun ()
9941 : 10269 : && RECORD_OR_UNION_TYPE_P (ctx))
9942 : : {
9943 : 10269 : if (name)
9944 : 8787 : res = lookup_class_binding (ctx, name);
9945 : : else
9946 : 1482 : res = lookup_field_ident (ctx, u ());
9947 : :
9948 : 10269 : if (!res
9949 : 10269 : || TREE_CODE (res) != FIELD_DECL
9950 : 20538 : || DECL_CONTEXT (res) != ctx)
9951 : : res = NULL_TREE;
9952 : : }
9953 : :
9954 : 10269 : if (!res)
9955 : 0 : set_overrun ();
9956 : : else
9957 : : {
9958 : 10269 : int tag = insert (res);
9959 : 10269 : dump (dumper::TREE)
9960 : 21 : && dump ("Read member:%d %C:%N", tag, TREE_CODE (res), res);
9961 : : }
9962 : : }
9963 : : break;
9964 : :
9965 : 2211 : case tt_binfo:
9966 : : /* A BINFO. Walk the tree of the dominating type. */
9967 : 2211 : {
9968 : 2211 : tree type;
9969 : 2211 : unsigned ix = binfo_mergeable (&type);
9970 : 2211 : if (type)
9971 : : {
9972 : 2211 : res = TYPE_BINFO (type);
9973 : 2308 : for (; ix && res; res = TREE_CHAIN (res))
9974 : 97 : ix--;
9975 : 2211 : if (!res)
9976 : 0 : set_overrun ();
9977 : : }
9978 : :
9979 : 2211 : if (get_overrun ())
9980 : : break;
9981 : :
9982 : : /* Insert binfo into backreferences. */
9983 : 2211 : tag = insert (res);
9984 : 2211 : dump (dumper::TREE) && dump ("Read binfo:%d %N", tag, res);
9985 : : }
9986 : 2211 : break;
9987 : :
9988 : 21 : case tt_vtable:
9989 : 21 : {
9990 : 21 : unsigned ix = u ();
9991 : 21 : tree ctx = tree_node ();
9992 : 21 : dump (dumper::TREE) && dump ("Reading vtable %N[%u]", ctx, ix);
9993 : 21 : if (TREE_CODE (ctx) == RECORD_TYPE && TYPE_LANG_SPECIFIC (ctx))
9994 : 21 : for (res = CLASSTYPE_VTABLES (ctx); res; res = DECL_CHAIN (res))
9995 : 21 : if (!ix--)
9996 : : break;
9997 : 21 : if (!res)
9998 : 0 : set_overrun ();
9999 : : }
10000 : : break;
10001 : :
10002 : 0 : case tt_thunk:
10003 : 0 : {
10004 : 0 : int fixed = i ();
10005 : 0 : tree target = tree_node ();
10006 : 0 : tree virt = tree_node ();
10007 : :
10008 : 0 : for (tree thunk = DECL_THUNKS (target);
10009 : 0 : thunk; thunk = DECL_CHAIN (thunk))
10010 : 0 : if (THUNK_FIXED_OFFSET (thunk) == fixed
10011 : 0 : && !THUNK_VIRTUAL_OFFSET (thunk) == !virt
10012 : 0 : && (!virt
10013 : 0 : || tree_int_cst_equal (virt, THUNK_VIRTUAL_OFFSET (thunk))))
10014 : : {
10015 : : res = thunk;
10016 : : break;
10017 : : }
10018 : :
10019 : 0 : int tag = insert (res);
10020 : 0 : if (res)
10021 : 0 : dump (dumper::TREE)
10022 : 0 : && dump ("Read:%d thunk %N to %N", tag, DECL_NAME (res), target);
10023 : : else
10024 : 0 : set_overrun ();
10025 : : }
10026 : : break;
10027 : :
10028 : 213498 : case tt_clone_ref:
10029 : 213498 : {
10030 : 213498 : tree target = tree_node ();
10031 : 213498 : tree name = tree_node ();
10032 : :
10033 : 213498 : if (DECL_P (target) && DECL_MAYBE_IN_CHARGE_CDTOR_P (target))
10034 : : {
10035 : 213498 : tree clone;
10036 : 335744 : FOR_EVERY_CLONE (clone, target)
10037 : 335744 : if (DECL_NAME (clone) == name)
10038 : : {
10039 : : res = clone;
10040 : : break;
10041 : : }
10042 : : }
10043 : :
10044 : : /* A clone might have a different vtable entry. */
10045 : 213498 : if (res && DECL_VIRTUAL_P (res))
10046 : 16291 : DECL_VINDEX (res) = tree_node ();
10047 : :
10048 : 213498 : if (!res)
10049 : 0 : set_overrun ();
10050 : 213498 : int tag = insert (res);
10051 : 213498 : if (res)
10052 : 213498 : dump (dumper::TREE)
10053 : 222 : && dump ("Read:%d clone %N of %N", tag, DECL_NAME (res), target);
10054 : : else
10055 : 0 : set_overrun ();
10056 : : }
10057 : : break;
10058 : :
10059 : 1623961 : case tt_entity:
10060 : : /* Index into the entity table. Perhaps not loaded yet! */
10061 : 1623961 : {
10062 : 1623961 : unsigned origin = state->slurp->remap_module (u ());
10063 : 1623961 : unsigned ident = u ();
10064 : 1623961 : module_state *from = (*modules)[origin];
10065 : :
10066 : 1623961 : if (!origin || ident >= from->entity_num)
10067 : 0 : set_overrun ();
10068 : 1623961 : if (!get_overrun ())
10069 : : {
10070 : 1623961 : binding_slot *slot = &(*entity_ary)[from->entity_lwm + ident];
10071 : 1623961 : if (slot->is_lazy ())
10072 : 31134 : if (!from->lazy_load (ident, slot))
10073 : 0 : set_overrun ();
10074 : 1623961 : res = *slot;
10075 : : }
10076 : :
10077 : 1623961 : if (res)
10078 : : {
10079 : 1623961 : const char *kind = (origin != state->mod ? "Imported" : "Named");
10080 : 1623961 : int tag = insert (res);
10081 : 1623961 : dump (dumper::TREE)
10082 : 573 : && dump ("%s:%d %C:%N@%M", kind, tag, TREE_CODE (res),
10083 : 573 : res, (*modules)[origin]);
10084 : :
10085 : 1623961 : if (!add_indirects (res))
10086 : : {
10087 : 0 : set_overrun ();
10088 : 0 : res = NULL_TREE;
10089 : : }
10090 : : }
10091 : : }
10092 : : break;
10093 : :
10094 : 3736 : case tt_template:
10095 : : /* A template. */
10096 : 3736 : if (tree tpl = tree_node ())
10097 : : {
10098 : 3736 : res = DECL_TEMPLATE_RESULT (tpl);
10099 : 3736 : dump (dumper::TREE)
10100 : 9 : && dump ("Read template %C:%N", TREE_CODE (res), res);
10101 : : }
10102 : : break;
10103 : : }
10104 : :
10105 : 134597612 : if (is_use && !unused && res && DECL_P (res) && !TREE_USED (res))
10106 : : {
10107 : : /* Mark decl used as mark_used does -- we cannot call
10108 : : mark_used in the middle of streaming, we only need a subset
10109 : : of its functionality. */
10110 : 858915 : TREE_USED (res) = true;
10111 : :
10112 : : /* And for structured bindings also the underlying decl. */
10113 : 858915 : if (DECL_DECOMPOSITION_P (res) && DECL_DECOMP_BASE (res))
10114 : 908 : TREE_USED (DECL_DECOMP_BASE (res)) = true;
10115 : :
10116 : 858915 : if (DECL_CLONED_FUNCTION_P (res))
10117 : 8355 : TREE_USED (DECL_CLONED_FUNCTION (res)) = true;
10118 : : }
10119 : :
10120 : 134597612 : dump.outdent ();
10121 : 134597612 : return res;
10122 : : }
10123 : :
10124 : : void
10125 : 2219142 : trees_out::tpl_parms (tree parms, unsigned &tpl_levels)
10126 : : {
10127 : 2219142 : if (!parms)
10128 : : return;
10129 : :
10130 : 1501323 : if (TREE_VISITED (parms))
10131 : : {
10132 : 653122 : ref_node (parms);
10133 : 653122 : return;
10134 : : }
10135 : :
10136 : 848201 : tpl_parms (TREE_CHAIN (parms), tpl_levels);
10137 : :
10138 : 848201 : tree vec = TREE_VALUE (parms);
10139 : 848201 : unsigned len = TREE_VEC_LENGTH (vec);
10140 : : /* Depth. */
10141 : 848201 : int tag = insert (parms);
10142 : 848201 : if (streaming_p ())
10143 : : {
10144 : 231405 : i (len + 1);
10145 : 231471 : dump (dumper::TREE)
10146 : 66 : && dump ("Writing template parms:%d level:%N length:%d",
10147 : 66 : tag, TREE_PURPOSE (parms), len);
10148 : : }
10149 : 848201 : tree_node (TREE_PURPOSE (parms));
10150 : :
10151 : 2392281 : for (unsigned ix = 0; ix != len; ix++)
10152 : : {
10153 : 1544080 : tree parm = TREE_VEC_ELT (vec, ix);
10154 : 1544080 : tree decl = TREE_VALUE (parm);
10155 : :
10156 : 1544080 : gcc_checking_assert (DECL_TEMPLATE_PARM_P (decl));
10157 : 1544080 : if (CHECKING_P)
10158 : 1544080 : switch (TREE_CODE (decl))
10159 : : {
10160 : 0 : default: gcc_unreachable ();
10161 : :
10162 : 3684 : case TEMPLATE_DECL:
10163 : 3684 : gcc_assert ((TREE_CODE (TREE_TYPE (decl)) == TEMPLATE_TEMPLATE_PARM)
10164 : : && (TREE_CODE (DECL_TEMPLATE_RESULT (decl)) == TYPE_DECL)
10165 : : && (TYPE_NAME (TREE_TYPE (decl)) == decl));
10166 : : break;
10167 : :
10168 : 1441634 : case TYPE_DECL:
10169 : 1441634 : gcc_assert ((TREE_CODE (TREE_TYPE (decl)) == TEMPLATE_TYPE_PARM)
10170 : : && (TYPE_NAME (TREE_TYPE (decl)) == decl));
10171 : : break;
10172 : :
10173 : 98762 : case PARM_DECL:
10174 : 98762 : gcc_assert ((TREE_CODE (DECL_INITIAL (decl)) == TEMPLATE_PARM_INDEX)
10175 : : && (TREE_CODE (TEMPLATE_PARM_DECL (DECL_INITIAL (decl)))
10176 : : == CONST_DECL)
10177 : : && (DECL_TEMPLATE_PARM_P
10178 : : (TEMPLATE_PARM_DECL (DECL_INITIAL (decl)))));
10179 : : break;
10180 : : }
10181 : :
10182 : 1544080 : tree_node (decl);
10183 : 1544080 : tree_node (TEMPLATE_PARM_CONSTRAINTS (parm));
10184 : : }
10185 : :
10186 : 848201 : tpl_levels++;
10187 : : }
10188 : :
10189 : : tree
10190 : 479424 : trees_in::tpl_parms (unsigned &tpl_levels)
10191 : : {
10192 : 479424 : tree parms = NULL_TREE;
10193 : :
10194 : 1003548 : while (int len = i ())
10195 : : {
10196 : 524124 : if (len < 0)
10197 : : {
10198 : 285528 : parms = back_ref (len);
10199 : 285528 : continue;
10200 : : }
10201 : :
10202 : 238596 : len -= 1;
10203 : 238596 : parms = tree_cons (NULL_TREE, NULL_TREE, parms);
10204 : 238596 : int tag = insert (parms);
10205 : 238596 : TREE_PURPOSE (parms) = tree_node ();
10206 : :
10207 : 238596 : dump (dumper::TREE)
10208 : 105 : && dump ("Reading template parms:%d level:%N length:%d",
10209 : 105 : tag, TREE_PURPOSE (parms), len);
10210 : :
10211 : 238596 : tree vec = make_tree_vec (len);
10212 : 652410 : for (int ix = 0; ix != len; ix++)
10213 : : {
10214 : 413814 : tree decl = tree_node ();
10215 : 413814 : if (!decl)
10216 : : return NULL_TREE;
10217 : :
10218 : 413814 : tree parm = build_tree_list (NULL, decl);
10219 : 413814 : TEMPLATE_PARM_CONSTRAINTS (parm) = tree_node ();
10220 : :
10221 : 413814 : TREE_VEC_ELT (vec, ix) = parm;
10222 : : }
10223 : :
10224 : 238596 : TREE_VALUE (parms) = vec;
10225 : 238596 : tpl_levels++;
10226 : : }
10227 : :
10228 : : return parms;
10229 : : }
10230 : :
10231 : : void
10232 : 1370941 : trees_out::tpl_parms_fini (tree tmpl, unsigned tpl_levels)
10233 : : {
10234 : 1370941 : for (tree parms = DECL_TEMPLATE_PARMS (tmpl);
10235 : 2219142 : tpl_levels--; parms = TREE_CHAIN (parms))
10236 : : {
10237 : 848201 : tree vec = TREE_VALUE (parms);
10238 : :
10239 : 848201 : tree_node (TREE_TYPE (vec));
10240 : 2392281 : for (unsigned ix = TREE_VEC_LENGTH (vec); ix--;)
10241 : : {
10242 : 1544080 : tree parm = TREE_VEC_ELT (vec, ix);
10243 : 1544080 : tree dflt = TREE_PURPOSE (parm);
10244 : 1544080 : tree_node (dflt);
10245 : :
10246 : : /* Template template parameters need a context of their owning
10247 : : template. This is quite tricky to infer correctly on stream-in
10248 : : (see PR c++/98881) so we'll just provide it directly. */
10249 : 1544080 : tree decl = TREE_VALUE (parm);
10250 : 1544080 : if (TREE_CODE (decl) == TEMPLATE_DECL)
10251 : 3684 : tree_node (DECL_CONTEXT (decl));
10252 : : }
10253 : : }
10254 : 1370941 : }
10255 : :
10256 : : bool
10257 : 479424 : trees_in::tpl_parms_fini (tree tmpl, unsigned tpl_levels)
10258 : : {
10259 : 479424 : for (tree parms = DECL_TEMPLATE_PARMS (tmpl);
10260 : 718020 : tpl_levels--; parms = TREE_CHAIN (parms))
10261 : : {
10262 : 238596 : tree vec = TREE_VALUE (parms);
10263 : :
10264 : 238596 : TREE_TYPE (vec) = tree_node ();
10265 : 652410 : for (unsigned ix = TREE_VEC_LENGTH (vec); ix--;)
10266 : : {
10267 : 413814 : tree parm = TREE_VEC_ELT (vec, ix);
10268 : 413814 : tree dflt = tree_node ();
10269 : 413814 : TREE_PURPOSE (parm) = dflt;
10270 : :
10271 : 413814 : tree decl = TREE_VALUE (parm);
10272 : 413814 : if (TREE_CODE (decl) == TEMPLATE_DECL)
10273 : 994 : DECL_CONTEXT (decl) = tree_node ();
10274 : :
10275 : 413814 : if (get_overrun ())
10276 : : return false;
10277 : : }
10278 : : }
10279 : : return true;
10280 : : }
10281 : :
10282 : : /* PARMS is a LIST, one node per level.
10283 : : TREE_VALUE is a TREE_VEC of parm info for that level.
10284 : : each ELT is a TREE_LIST
10285 : : TREE_VALUE is PARM_DECL, TYPE_DECL or TEMPLATE_DECL
10286 : : TREE_PURPOSE is the default value. */
10287 : :
10288 : : void
10289 : 1370941 : trees_out::tpl_header (tree tpl, unsigned *tpl_levels)
10290 : : {
10291 : 1370941 : tree parms = DECL_TEMPLATE_PARMS (tpl);
10292 : 1370941 : tpl_parms (parms, *tpl_levels);
10293 : :
10294 : : /* Mark end. */
10295 : 1370941 : if (streaming_p ())
10296 : 456517 : u (0);
10297 : :
10298 : 1370941 : if (*tpl_levels)
10299 : 807016 : tree_node (TEMPLATE_PARMS_CONSTRAINTS (parms));
10300 : 1370941 : }
10301 : :
10302 : : bool
10303 : 479424 : trees_in::tpl_header (tree tpl, unsigned *tpl_levels)
10304 : : {
10305 : 479424 : tree parms = tpl_parms (*tpl_levels);
10306 : 479424 : if (!parms)
10307 : : return false;
10308 : :
10309 : 479424 : DECL_TEMPLATE_PARMS (tpl) = parms;
10310 : :
10311 : 479424 : if (*tpl_levels)
10312 : 236281 : TEMPLATE_PARMS_CONSTRAINTS (parms) = tree_node ();
10313 : :
10314 : : return true;
10315 : : }
10316 : :
10317 : : /* Stream skeleton parm nodes, with their flags, type & parm indices.
10318 : : All the parms will have consecutive tags. */
10319 : :
10320 : : void
10321 : 1863316 : trees_out::fn_parms_init (tree fn)
10322 : : {
10323 : : /* First init them. */
10324 : 1863316 : int base_tag = ref_num - 1;
10325 : 1863316 : int ix = 0;
10326 : 1863316 : for (tree parm = DECL_ARGUMENTS (fn);
10327 : 5747104 : parm; parm = DECL_CHAIN (parm), ix++)
10328 : : {
10329 : 3883788 : if (streaming_p ())
10330 : : {
10331 : 1294602 : start (parm);
10332 : 1294602 : tree_node_bools (parm);
10333 : : }
10334 : 3883788 : int tag = insert (parm);
10335 : 3883788 : gcc_checking_assert (base_tag - ix == tag);
10336 : : }
10337 : : /* Mark the end. */
10338 : 1863316 : if (streaming_p ())
10339 : 621510 : u (0);
10340 : :
10341 : : /* Now stream their contents. */
10342 : 1863316 : ix = 0;
10343 : 1863316 : for (tree parm = DECL_ARGUMENTS (fn);
10344 : 5747104 : parm; parm = DECL_CHAIN (parm), ix++)
10345 : : {
10346 : 3883788 : if (streaming_p ())
10347 : 1294602 : dump (dumper::TREE)
10348 : 225 : && dump ("Writing parm:%d %u (%N) of %N",
10349 : : base_tag - ix, ix, parm, fn);
10350 : 3883788 : tree_node_vals (parm);
10351 : : }
10352 : :
10353 : 1863316 : if (!streaming_p ())
10354 : : {
10355 : : /* We must walk contract attrs so the dependency graph is complete. */
10356 : 1241806 : for (tree contract = DECL_CONTRACTS (fn);
10357 : 1241920 : contract;
10358 : 114 : contract = CONTRACT_CHAIN (contract))
10359 : 114 : tree_node (contract);
10360 : : }
10361 : :
10362 : : /* Write a reference to contracts pre/post functions, if any, to avoid
10363 : : regenerating them in importers. */
10364 : 1863316 : tree_node (DECL_PRE_FN (fn));
10365 : 1863316 : tree_node (DECL_POST_FN (fn));
10366 : 1863316 : }
10367 : :
10368 : : /* Build skeleton parm nodes, read their flags, type & parm indices. */
10369 : :
10370 : : int
10371 : 689451 : trees_in::fn_parms_init (tree fn)
10372 : : {
10373 : 689451 : int base_tag = ~(int)back_refs.length ();
10374 : :
10375 : 689451 : tree *parm_ptr = &DECL_ARGUMENTS (fn);
10376 : 689451 : int ix = 0;
10377 : 2115296 : for (; int code = u (); ix++)
10378 : : {
10379 : 1425845 : tree parm = start (code);
10380 : 1425845 : if (!tree_node_bools (parm))
10381 : : return 0;
10382 : :
10383 : 1425845 : int tag = insert (parm);
10384 : 1425845 : gcc_checking_assert (base_tag - ix == tag);
10385 : 1425845 : *parm_ptr = parm;
10386 : 1425845 : parm_ptr = &DECL_CHAIN (parm);
10387 : 1425845 : }
10388 : :
10389 : 689451 : ix = 0;
10390 : 689451 : for (tree parm = DECL_ARGUMENTS (fn);
10391 : 2115296 : parm; parm = DECL_CHAIN (parm), ix++)
10392 : : {
10393 : 1425845 : dump (dumper::TREE)
10394 : 375 : && dump ("Reading parm:%d %u (%N) of %N",
10395 : : base_tag - ix, ix, parm, fn);
10396 : 1425845 : if (!tree_node_vals (parm))
10397 : : return 0;
10398 : : }
10399 : :
10400 : : /* Reload references to contract functions, if any. */
10401 : 689451 : tree pre_fn = tree_node ();
10402 : 689451 : tree post_fn = tree_node ();
10403 : 689451 : set_contract_functions (fn, pre_fn, post_fn);
10404 : :
10405 : 689451 : return base_tag;
10406 : : }
10407 : :
10408 : : /* Read the remaining parm node data. Replace with existing (if
10409 : : non-null) in the map. */
10410 : :
10411 : : void
10412 : 689451 : trees_in::fn_parms_fini (int tag, tree fn, tree existing, bool is_defn)
10413 : : {
10414 : 1115045 : tree existing_parm = existing ? DECL_ARGUMENTS (existing) : NULL_TREE;
10415 : 689451 : tree parms = DECL_ARGUMENTS (fn);
10416 : 689451 : unsigned ix = 0;
10417 : 2115296 : for (tree parm = parms; parm; parm = DECL_CHAIN (parm), ix++)
10418 : : {
10419 : 1425845 : if (existing_parm)
10420 : : {
10421 : 1274514 : if (is_defn && !DECL_SAVED_TREE (existing))
10422 : : {
10423 : : /* If we're about to become the definition, set the
10424 : : names of the parms from us. */
10425 : 28989 : DECL_NAME (existing_parm) = DECL_NAME (parm);
10426 : 28989 : DECL_SOURCE_LOCATION (existing_parm) = DECL_SOURCE_LOCATION (parm);
10427 : : }
10428 : :
10429 : 875330 : back_refs[~tag] = existing_parm;
10430 : 875330 : existing_parm = DECL_CHAIN (existing_parm);
10431 : : }
10432 : 1425845 : tag--;
10433 : : }
10434 : 689451 : }
10435 : :
10436 : : /* Encode into KEY the position of the local type (class or enum)
10437 : : declaration DECL within FN. The position is encoded as the
10438 : : index of the innermost BLOCK (numbered in BFS order) along with
10439 : : the index within its BLOCK_VARS list. */
10440 : :
10441 : : void
10442 : 14862 : trees_out::key_local_type (merge_key& key, tree decl, tree fn)
10443 : : {
10444 : 14862 : auto_vec<tree, 4> blocks;
10445 : 14862 : blocks.quick_push (DECL_INITIAL (fn));
10446 : 14862 : unsigned block_ix = 0;
10447 : 72018 : while (block_ix != blocks.length ())
10448 : : {
10449 : 28578 : tree block = blocks[block_ix];
10450 : 28578 : unsigned decl_ix = 0;
10451 : 83877 : for (tree var = BLOCK_VARS (block); var; var = DECL_CHAIN (var))
10452 : : {
10453 : 70161 : if (TREE_CODE (var) != TYPE_DECL)
10454 : 42072 : continue;
10455 : 28089 : if (var == decl)
10456 : : {
10457 : 14862 : key.index = (block_ix << 10) | decl_ix;
10458 : 14862 : return;
10459 : : }
10460 : 13227 : ++decl_ix;
10461 : : }
10462 : 28545 : for (tree sub = BLOCK_SUBBLOCKS (block); sub; sub = BLOCK_CHAIN (sub))
10463 : 14829 : blocks.safe_push (sub);
10464 : 13716 : ++block_ix;
10465 : : }
10466 : :
10467 : : /* Not-found value. */
10468 : 0 : key.index = 1023;
10469 : 14862 : }
10470 : :
10471 : : /* Look up the local type corresponding at the position encoded by
10472 : : KEY within FN and named NAME. */
10473 : :
10474 : : tree
10475 : 5376 : trees_in::key_local_type (const merge_key& key, tree fn, tree name)
10476 : : {
10477 : 5376 : if (!DECL_INITIAL (fn))
10478 : : return NULL_TREE;
10479 : :
10480 : 3455 : const unsigned block_pos = key.index >> 10;
10481 : 3455 : const unsigned decl_pos = key.index & 1023;
10482 : :
10483 : 3455 : if (decl_pos == 1023)
10484 : : return NULL_TREE;
10485 : :
10486 : 3455 : auto_vec<tree, 4> blocks;
10487 : 3455 : blocks.quick_push (DECL_INITIAL (fn));
10488 : 3455 : unsigned block_ix = 0;
10489 : 16841 : while (block_ix != blocks.length ())
10490 : : {
10491 : 6693 : tree block = blocks[block_ix];
10492 : 6693 : if (block_ix == block_pos)
10493 : : {
10494 : 3455 : unsigned decl_ix = 0;
10495 : 9522 : for (tree var = BLOCK_VARS (block); var; var = DECL_CHAIN (var))
10496 : : {
10497 : 9522 : if (TREE_CODE (var) != TYPE_DECL)
10498 : 3791 : continue;
10499 : : /* Prefer using the identifier as the key for more robustness
10500 : : to ODR violations, except for anonymous types since their
10501 : : compiler-generated identifiers aren't stable. */
10502 : 11462 : if (IDENTIFIER_ANON_P (name)
10503 : 5731 : ? decl_ix == decl_pos
10504 : 593 : : DECL_NAME (var) == name)
10505 : 3455 : return var;
10506 : 2276 : ++decl_ix;
10507 : : }
10508 : : return NULL_TREE;
10509 : : }
10510 : 6716 : for (tree sub = BLOCK_SUBBLOCKS (block); sub; sub = BLOCK_CHAIN (sub))
10511 : 3478 : blocks.safe_push (sub);
10512 : 3238 : ++block_ix;
10513 : : }
10514 : :
10515 : : return NULL_TREE;
10516 : 3455 : }
10517 : :
10518 : : /* DEP is the depset of some decl we're streaming by value. Determine
10519 : : the merging behaviour. */
10520 : :
10521 : : merge_kind
10522 : 4584980 : trees_out::get_merge_kind (tree decl, depset *dep)
10523 : : {
10524 : 4584980 : if (!dep)
10525 : : {
10526 : 810413 : if (VAR_OR_FUNCTION_DECL_P (decl))
10527 : : {
10528 : : /* Any var or function with template info should have DEP. */
10529 : 430171 : gcc_checking_assert (!DECL_LANG_SPECIFIC (decl)
10530 : : || !DECL_TEMPLATE_INFO (decl));
10531 : 430171 : if (DECL_LOCAL_DECL_P (decl))
10532 : : return MK_unique;
10533 : : }
10534 : :
10535 : : /* Either unique, or some member of a class that cannot have an
10536 : : out-of-class definition. For instance a FIELD_DECL. */
10537 : 810141 : tree ctx = CP_DECL_CONTEXT (decl);
10538 : 810141 : if (TREE_CODE (ctx) == FUNCTION_DECL)
10539 : : {
10540 : : /* USING_DECLs and NAMESPACE_DECLs cannot have DECL_TEMPLATE_INFO --
10541 : : this isn't permitting them to have one. */
10542 : 473095 : gcc_checking_assert (TREE_CODE (decl) == USING_DECL
10543 : : || TREE_CODE (decl) == NAMESPACE_DECL
10544 : : || !DECL_LANG_SPECIFIC (decl)
10545 : : || !DECL_TEMPLATE_INFO (decl));
10546 : :
10547 : : return MK_unique;
10548 : : }
10549 : :
10550 : 337046 : if (TREE_CODE (decl) == TEMPLATE_DECL
10551 : 337046 : && DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (decl))
10552 : : return MK_local_friend;
10553 : :
10554 : 337046 : gcc_checking_assert (TYPE_P (ctx));
10555 : 337046 : if (TREE_CODE (decl) == USING_DECL)
10556 : : return MK_field;
10557 : :
10558 : 214928 : if (TREE_CODE (decl) == FIELD_DECL)
10559 : : {
10560 : 160128 : if (DECL_NAME (decl))
10561 : : {
10562 : : /* Anonymous FIELD_DECLs have a NULL name. */
10563 : 123120 : gcc_checking_assert (!IDENTIFIER_ANON_P (DECL_NAME (decl)));
10564 : : return MK_named;
10565 : : }
10566 : :
10567 : 37008 : if (!DECL_NAME (decl)
10568 : 37008 : && !RECORD_OR_UNION_TYPE_P (TREE_TYPE (decl))
10569 : 38360 : && !DECL_BIT_FIELD_REPRESENTATIVE (decl))
10570 : : {
10571 : : /* The underlying storage unit for a bitfield. We do not
10572 : : need to dedup it, because it's only reachable through
10573 : : the bitfields it represents. And those are deduped. */
10574 : : // FIXME: Is that assertion correct -- do we ever fish it
10575 : : // out and put it in an expr?
10576 : 402 : gcc_checking_assert ((TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
10577 : : ? TREE_CODE (TREE_TYPE (TREE_TYPE (decl)))
10578 : : : TREE_CODE (TREE_TYPE (decl)))
10579 : : == INTEGER_TYPE);
10580 : : return MK_unique;
10581 : : }
10582 : :
10583 : : return MK_field;
10584 : : }
10585 : :
10586 : 54800 : if (TREE_CODE (decl) == CONST_DECL)
10587 : : return MK_named;
10588 : :
10589 : 12300 : if (TREE_CODE (decl) == VAR_DECL
10590 : 12300 : && DECL_VTABLE_OR_VTT_P (decl))
10591 : : return MK_vtable;
10592 : :
10593 : 2216 : if (DECL_THUNK_P (decl))
10594 : : /* Thunks are unique-enough, because they're only referenced
10595 : : from the vtable. And that's either new (so we want the
10596 : : thunks), or it's a duplicate (so it will be dropped). */
10597 : : return MK_unique;
10598 : :
10599 : : /* There should be no other cases. */
10600 : 0 : gcc_unreachable ();
10601 : : }
10602 : :
10603 : 3774567 : gcc_checking_assert (TREE_CODE (decl) != FIELD_DECL
10604 : : && TREE_CODE (decl) != USING_DECL
10605 : : && TREE_CODE (decl) != CONST_DECL);
10606 : :
10607 : 3774567 : if (is_key_order ())
10608 : : {
10609 : : /* When doing the mergeablilty graph, there's an indirection to
10610 : : the actual depset. */
10611 : 1258176 : gcc_assert (dep->is_special ());
10612 : 1258176 : dep = dep->deps[0];
10613 : : }
10614 : :
10615 : 3774567 : gcc_checking_assert (decl == dep->get_entity ());
10616 : :
10617 : 3774567 : merge_kind mk = MK_named;
10618 : 3774567 : switch (dep->get_entity_kind ())
10619 : : {
10620 : 0 : default:
10621 : 0 : gcc_unreachable ();
10622 : :
10623 : : case depset::EK_PARTIAL:
10624 : : mk = MK_partial;
10625 : : break;
10626 : :
10627 : 2133723 : case depset::EK_DECL:
10628 : 2133723 : {
10629 : 2133723 : tree ctx = CP_DECL_CONTEXT (decl);
10630 : :
10631 : 2133723 : switch (TREE_CODE (ctx))
10632 : : {
10633 : 0 : default:
10634 : 0 : gcc_unreachable ();
10635 : :
10636 : 14862 : case FUNCTION_DECL:
10637 : 14862 : gcc_checking_assert
10638 : : (DECL_IMPLICIT_TYPEDEF_P (STRIP_TEMPLATE (decl)));
10639 : :
10640 : : mk = MK_local_type;
10641 : : break;
10642 : :
10643 : 2118861 : case RECORD_TYPE:
10644 : 2118861 : case UNION_TYPE:
10645 : 2118861 : case NAMESPACE_DECL:
10646 : 2118861 : if (DECL_NAME (decl) == as_base_identifier)
10647 : : {
10648 : : mk = MK_as_base;
10649 : : break;
10650 : : }
10651 : :
10652 : : /* A lambda may have a class as its context, even though it
10653 : : isn't a member in the traditional sense; see the test
10654 : : g++.dg/modules/lambda-6_a.C. */
10655 : 2699334 : if (DECL_IMPLICIT_TYPEDEF_P (STRIP_TEMPLATE (decl))
10656 : 2322867 : && LAMBDA_TYPE_P (TREE_TYPE (decl)))
10657 : 342 : if (tree scope
10658 : 171 : = LAMBDA_EXPR_EXTRA_SCOPE (CLASSTYPE_LAMBDA_EXPR
10659 : : (TREE_TYPE (decl))))
10660 : : {
10661 : : /* Lambdas attached to fields are keyed to its class. */
10662 : 132 : if (TREE_CODE (scope) == FIELD_DECL)
10663 : 9 : scope = TYPE_NAME (DECL_CONTEXT (scope));
10664 : 132 : if (DECL_LANG_SPECIFIC (scope)
10665 : 264 : && DECL_MODULE_KEYED_DECLS_P (scope))
10666 : : {
10667 : : mk = MK_keyed;
10668 : : break;
10669 : : }
10670 : : }
10671 : :
10672 : 2024679 : if (TREE_CODE (decl) == TEMPLATE_DECL
10673 : 2024679 : && DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (decl))
10674 : : {
10675 : : mk = MK_local_friend;
10676 : : break;
10677 : : }
10678 : :
10679 : 2006715 : if (IDENTIFIER_ANON_P (DECL_NAME (decl)))
10680 : : {
10681 : 31044 : if (RECORD_OR_UNION_TYPE_P (ctx))
10682 : : mk = MK_field;
10683 : 1875 : else if (DECL_IMPLICIT_TYPEDEF_P (decl)
10684 : 1875 : && UNSCOPED_ENUM_P (TREE_TYPE (decl))
10685 : 3744 : && TYPE_VALUES (TREE_TYPE (decl)))
10686 : : /* Keyed by first enum value, and underlying type. */
10687 : : mk = MK_enum;
10688 : : else
10689 : : /* No way to merge it, it is an ODR land-mine. */
10690 : : mk = MK_unique;
10691 : : }
10692 : : }
10693 : : }
10694 : : break;
10695 : :
10696 : 1564128 : case depset::EK_SPECIALIZATION:
10697 : 1564128 : {
10698 : 1564128 : gcc_checking_assert (dep->is_special ());
10699 : :
10700 : 1564128 : if (TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
10701 : : /* An block-scope classes of templates are themselves
10702 : : templates. */
10703 : 6024 : gcc_checking_assert (DECL_IMPLICIT_TYPEDEF_P (decl));
10704 : :
10705 : 1564128 : if (dep->is_friend_spec ())
10706 : : mk = MK_friend_spec;
10707 : 1564128 : else if (dep->is_type_spec ())
10708 : : mk = MK_type_spec;
10709 : : else
10710 : 1092096 : mk = MK_decl_spec;
10711 : :
10712 : 1564128 : if (TREE_CODE (decl) == TEMPLATE_DECL)
10713 : : {
10714 : 103824 : spec_entry *entry = reinterpret_cast <spec_entry *> (dep->deps[0]);
10715 : 103824 : if (TREE_CODE (entry->spec) != TEMPLATE_DECL)
10716 : 13749 : mk = merge_kind (mk | MK_tmpl_tmpl_mask);
10717 : : }
10718 : : }
10719 : : break;
10720 : : }
10721 : :
10722 : : return mk;
10723 : : }
10724 : :
10725 : :
10726 : : /* The container of DECL -- not necessarily its context! */
10727 : :
10728 : : tree
10729 : 4584980 : trees_out::decl_container (tree decl)
10730 : : {
10731 : 4584980 : int use_tpl;
10732 : 4584980 : tree tpl = NULL_TREE;
10733 : 4584980 : if (tree template_info = node_template_info (decl, use_tpl))
10734 : 1647638 : tpl = TI_TEMPLATE (template_info);
10735 : 4584980 : if (tpl == decl)
10736 : 0 : tpl = nullptr;
10737 : :
10738 : : /* Stream the template we're instantiated from. */
10739 : 4584980 : tree_node (tpl);
10740 : :
10741 : 4584980 : tree container = NULL_TREE;
10742 : 4584980 : if (TREE_CODE (decl) == TEMPLATE_DECL
10743 : 4584980 : && DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (decl))
10744 : 17964 : container = DECL_CHAIN (decl);
10745 : : else
10746 : 4567016 : container = CP_DECL_CONTEXT (decl);
10747 : :
10748 : 4584980 : if (TYPE_P (container))
10749 : 2700020 : container = TYPE_NAME (container);
10750 : :
10751 : 4584980 : tree_node (container);
10752 : :
10753 : 4584980 : return container;
10754 : : }
10755 : :
10756 : : tree
10757 : 1768403 : trees_in::decl_container ()
10758 : : {
10759 : : /* The maybe-template. */
10760 : 1768403 : (void)tree_node ();
10761 : :
10762 : 1768403 : tree container = tree_node ();
10763 : :
10764 : 1768403 : return container;
10765 : : }
10766 : :
10767 : : /* Write out key information about a mergeable DEP. Does not write
10768 : : the contents of DEP itself. The context has already been
10769 : : written. The container has already been streamed. */
10770 : :
10771 : : void
10772 : 4584980 : trees_out::key_mergeable (int tag, merge_kind mk, tree decl, tree inner,
10773 : : tree container, depset *dep)
10774 : : {
10775 : 4584980 : if (dep && is_key_order ())
10776 : : {
10777 : 1258176 : gcc_checking_assert (dep->is_special ());
10778 : 1258176 : dep = dep->deps[0];
10779 : : }
10780 : :
10781 : 4584980 : if (streaming_p ())
10782 : 1655687 : dump (dumper::MERGE)
10783 : 723 : && dump ("Writing:%d's %s merge key (%s) %C:%N", tag, merge_kind_name[mk],
10784 : 672 : dep ? dep->entity_kind_name () : "contained",
10785 : 723 : TREE_CODE (decl), decl);
10786 : :
10787 : : /* Now write the locating information. */
10788 : 4584980 : if (mk & MK_template_mask)
10789 : : {
10790 : : /* Specializations are located via their originating template,
10791 : : and the set of template args they specialize. */
10792 : 1564128 : gcc_checking_assert (dep && dep->is_special ());
10793 : 1564128 : spec_entry *entry = reinterpret_cast <spec_entry *> (dep->deps[0]);
10794 : :
10795 : 1564128 : tree_node (entry->tmpl);
10796 : 1564128 : tree_node (entry->args);
10797 : 1564128 : if (mk & MK_tmpl_decl_mask)
10798 : 1092096 : if (flag_concepts && TREE_CODE (inner) == VAR_DECL)
10799 : : {
10800 : : /* Variable template partial specializations might need
10801 : : constraints (see spec_hasher::equal). It's simpler to
10802 : : write NULL when we don't need them. */
10803 : 16896 : tree constraints = NULL_TREE;
10804 : :
10805 : 16896 : if (uses_template_parms (entry->args))
10806 : 198 : constraints = get_constraints (inner);
10807 : 16896 : tree_node (constraints);
10808 : : }
10809 : :
10810 : 1564128 : if (CHECKING_P)
10811 : : {
10812 : : /* Make sure we can locate the decl. */
10813 : 1564128 : tree existing = match_mergeable_specialization
10814 : 1564128 : (bool (mk & MK_tmpl_decl_mask), entry);
10815 : :
10816 : 1564128 : gcc_assert (existing);
10817 : 1564128 : if (mk & MK_tmpl_decl_mask)
10818 : : {
10819 : 1092096 : if (mk & MK_tmpl_tmpl_mask)
10820 : 11211 : existing = DECL_TI_TEMPLATE (existing);
10821 : : }
10822 : : else
10823 : : {
10824 : 472032 : if (mk & MK_tmpl_tmpl_mask)
10825 : 2538 : existing = CLASSTYPE_TI_TEMPLATE (existing);
10826 : : else
10827 : 469494 : existing = TYPE_NAME (existing);
10828 : : }
10829 : :
10830 : : /* The walkabout should have found ourselves. */
10831 : 1564128 : gcc_checking_assert (TREE_CODE (decl) == TYPE_DECL
10832 : : ? same_type_p (TREE_TYPE (decl),
10833 : : TREE_TYPE (existing))
10834 : : : existing == decl);
10835 : : }
10836 : : }
10837 : 3020852 : else if (mk != MK_unique)
10838 : : {
10839 : 2544837 : merge_key key;
10840 : 2544837 : tree name = DECL_NAME (decl);
10841 : :
10842 : 2544837 : switch (mk)
10843 : : {
10844 : 0 : default:
10845 : 0 : gcc_unreachable ();
10846 : :
10847 : 2141291 : case MK_named:
10848 : 2141291 : case MK_friend_spec:
10849 : 4071196 : if (IDENTIFIER_CONV_OP_P (name))
10850 : 6099 : name = conv_op_identifier;
10851 : :
10852 : 2141291 : if (TREE_CODE (inner) == FUNCTION_DECL)
10853 : : {
10854 : : /* Functions are distinguished by parameter types. */
10855 : 1185240 : tree fn_type = TREE_TYPE (inner);
10856 : :
10857 : 1185240 : key.ref_q = type_memfn_rqual (fn_type);
10858 : 1185240 : key.args = TYPE_ARG_TYPES (fn_type);
10859 : :
10860 : 1185240 : if (tree reqs = get_constraints (inner))
10861 : : {
10862 : 48879 : if (cxx_dialect < cxx20)
10863 : 42 : reqs = CI_ASSOCIATED_CONSTRAINTS (reqs);
10864 : : else
10865 : 97716 : reqs = CI_DECLARATOR_REQS (reqs);
10866 : 48879 : key.constraints = reqs;
10867 : : }
10868 : :
10869 : 1185240 : if (IDENTIFIER_CONV_OP_P (name)
10870 : 1185240 : || (decl != inner
10871 : 636180 : && !(name == fun_identifier
10872 : : /* In case the user names something _FUN */
10873 : 48 : && LAMBDA_TYPE_P (DECL_CONTEXT (inner)))))
10874 : : /* And a function template, or conversion operator needs
10875 : : the return type. Except for the _FUN thunk of a
10876 : : generic lambda, which has a recursive decl_type'd
10877 : : return type. */
10878 : : // FIXME: What if the return type is a voldemort?
10879 : 642255 : key.ret = fndecl_declared_return_type (inner);
10880 : : }
10881 : : break;
10882 : :
10883 : 187881 : case MK_field:
10884 : 187881 : {
10885 : 187881 : unsigned ix = 0;
10886 : 187881 : if (TREE_CODE (inner) != FIELD_DECL)
10887 : : name = NULL_TREE;
10888 : : else
10889 : 36606 : gcc_checking_assert (!name || !IDENTIFIER_ANON_P (name));
10890 : :
10891 : 187881 : for (tree field = TYPE_FIELDS (TREE_TYPE (container));
10892 : 391804 : ; field = DECL_CHAIN (field))
10893 : : {
10894 : 579685 : tree finner = STRIP_TEMPLATE (field);
10895 : 579685 : if (TREE_CODE (finner) == TREE_CODE (inner))
10896 : : {
10897 : 255006 : if (finner == inner)
10898 : : break;
10899 : 67125 : ix++;
10900 : : }
10901 : 391804 : }
10902 : 187881 : key.index = ix;
10903 : : }
10904 : 187881 : break;
10905 : :
10906 : 10084 : case MK_vtable:
10907 : 10084 : {
10908 : 10084 : tree vtable = CLASSTYPE_VTABLES (TREE_TYPE (container));
10909 : 13246 : for (unsigned ix = 0; ; vtable = DECL_CHAIN (vtable), ix++)
10910 : 13246 : if (vtable == decl)
10911 : : {
10912 : 10084 : key.index = ix;
10913 : 10084 : break;
10914 : : }
10915 : 10084 : name = NULL_TREE;
10916 : : }
10917 : 10084 : break;
10918 : :
10919 : 94050 : case MK_as_base:
10920 : 94050 : gcc_checking_assert
10921 : : (decl == TYPE_NAME (CLASSTYPE_AS_BASE (TREE_TYPE (container))));
10922 : : break;
10923 : :
10924 : 17964 : case MK_local_friend:
10925 : 17964 : {
10926 : : /* Find by index on the class's DECL_LIST */
10927 : 17964 : unsigned ix = 0;
10928 : 17964 : for (tree decls = CLASSTYPE_DECL_LIST (TREE_CHAIN (decl));
10929 : 505191 : decls; decls = TREE_CHAIN (decls))
10930 : 505191 : if (!TREE_PURPOSE (decls))
10931 : : {
10932 : 64497 : tree frnd = friend_from_decl_list (TREE_VALUE (decls));
10933 : 64497 : if (frnd == decl)
10934 : : break;
10935 : 46533 : ix++;
10936 : : }
10937 : 17964 : key.index = ix;
10938 : 17964 : name = NULL_TREE;
10939 : : }
10940 : 17964 : break;
10941 : :
10942 : 14862 : case MK_local_type:
10943 : 14862 : key_local_type (key, STRIP_TEMPLATE (decl), container);
10944 : 14862 : break;
10945 : :
10946 : 1857 : case MK_enum:
10947 : 1857 : {
10948 : : /* Anonymous enums are located by their first identifier,
10949 : : and underlying type. */
10950 : 1857 : tree type = TREE_TYPE (decl);
10951 : :
10952 : 1857 : gcc_checking_assert (UNSCOPED_ENUM_P (type));
10953 : : /* Using the type name drops the bit precision we might
10954 : : have been using on the enum. */
10955 : 1857 : key.ret = TYPE_NAME (ENUM_UNDERLYING_TYPE (type));
10956 : 1857 : if (tree values = TYPE_VALUES (type))
10957 : 1857 : name = DECL_NAME (TREE_VALUE (values));
10958 : : }
10959 : : break;
10960 : :
10961 : 132 : case MK_keyed:
10962 : 132 : {
10963 : 264 : gcc_checking_assert (LAMBDA_TYPE_P (TREE_TYPE (inner)));
10964 : 132 : tree scope = LAMBDA_EXPR_EXTRA_SCOPE (CLASSTYPE_LAMBDA_EXPR
10965 : : (TREE_TYPE (inner)));
10966 : 132 : gcc_checking_assert (TREE_CODE (scope) == VAR_DECL
10967 : : || TREE_CODE (scope) == FIELD_DECL
10968 : : || TREE_CODE (scope) == PARM_DECL
10969 : : || TREE_CODE (scope) == TYPE_DECL);
10970 : : /* Lambdas attached to fields are keyed to the class. */
10971 : 132 : if (TREE_CODE (scope) == FIELD_DECL)
10972 : 9 : scope = TYPE_NAME (DECL_CONTEXT (scope));
10973 : 132 : auto *root = keyed_table->get (scope);
10974 : 132 : unsigned ix = root->length ();
10975 : : /* If we don't find it, we'll write a really big number
10976 : : that the reader will ignore. */
10977 : 132 : while (ix--)
10978 : 132 : if ((*root)[ix] == inner)
10979 : : break;
10980 : :
10981 : : /* Use the keyed-to decl as the 'name'. */
10982 : 132 : name = scope;
10983 : 132 : key.index = ix;
10984 : : }
10985 : 132 : break;
10986 : :
10987 : 76716 : case MK_partial:
10988 : 76716 : {
10989 : 76716 : tree ti = get_template_info (inner);
10990 : 76716 : key.constraints = get_constraints (inner);
10991 : 76716 : key.ret = TI_TEMPLATE (ti);
10992 : 76716 : key.args = TI_ARGS (ti);
10993 : : }
10994 : 76716 : break;
10995 : : }
10996 : :
10997 : 2544837 : tree_node (name);
10998 : 2544837 : if (streaming_p ())
10999 : : {
11000 : 904004 : unsigned code = (key.ref_q << 0) | (key.index << 2);
11001 : 904004 : u (code);
11002 : : }
11003 : :
11004 : 2544837 : if (mk == MK_enum)
11005 : 1857 : tree_node (key.ret);
11006 : 2542980 : else if (mk == MK_partial
11007 : 2466264 : || (mk == MK_named && inner
11008 : 2141291 : && TREE_CODE (inner) == FUNCTION_DECL))
11009 : : {
11010 : 1261956 : tree_node (key.ret);
11011 : 1261956 : tree arg = key.args;
11012 : 1261956 : if (mk == MK_named)
11013 : 3532572 : while (arg && arg != void_list_node)
11014 : : {
11015 : 2347332 : tree_node (TREE_VALUE (arg));
11016 : 2347332 : arg = TREE_CHAIN (arg);
11017 : : }
11018 : 1261956 : tree_node (arg);
11019 : 1261956 : tree_node (key.constraints);
11020 : : }
11021 : : }
11022 : 4584980 : }
11023 : :
11024 : : /* DECL is a new declaration that may be duplicated in OVL. Use RET &
11025 : : ARGS to find its clone, or NULL. If DECL's DECL_NAME is NULL, this
11026 : : has been found by a proxy. It will be an enum type located by its
11027 : : first member.
11028 : :
11029 : : We're conservative with matches, so ambiguous decls will be
11030 : : registered as different, then lead to a lookup error if the two
11031 : : modules are both visible. Perhaps we want to do something similar
11032 : : to duplicate decls to get ODR errors on loading? We already have
11033 : : some special casing for namespaces. */
11034 : :
11035 : : static tree
11036 : 568224 : check_mergeable_decl (merge_kind mk, tree decl, tree ovl, merge_key const &key)
11037 : : {
11038 : 568224 : tree found = NULL_TREE;
11039 : 2344130 : for (ovl_iterator iter (ovl); !found && iter; ++iter)
11040 : : {
11041 : 1138309 : tree match = *iter;
11042 : :
11043 : 1138309 : tree d_inner = decl;
11044 : 1138309 : tree m_inner = match;
11045 : :
11046 : 1336156 : again:
11047 : 1336156 : if (TREE_CODE (d_inner) != TREE_CODE (m_inner))
11048 : : {
11049 : 173028 : if (TREE_CODE (match) == NAMESPACE_DECL
11050 : 173028 : && !DECL_NAMESPACE_ALIAS (match))
11051 : : /* Namespaces are never overloaded. */
11052 : : found = match;
11053 : :
11054 : 173028 : continue;
11055 : : }
11056 : :
11057 : 1163128 : switch (TREE_CODE (d_inner))
11058 : : {
11059 : 407373 : case TEMPLATE_DECL:
11060 : 407373 : if (template_heads_equivalent_p (d_inner, m_inner))
11061 : : {
11062 : 197847 : d_inner = DECL_TEMPLATE_RESULT (d_inner);
11063 : 197847 : m_inner = DECL_TEMPLATE_RESULT (m_inner);
11064 : 197847 : if (d_inner == error_mark_node
11065 : 197847 : && TYPE_DECL_ALIAS_P (m_inner))
11066 : : {
11067 : : found = match;
11068 : : break;
11069 : : }
11070 : 197847 : goto again;
11071 : : }
11072 : : break;
11073 : :
11074 : 554762 : case FUNCTION_DECL:
11075 : 554762 : if (tree m_type = TREE_TYPE (m_inner))
11076 : 554762 : if ((!key.ret
11077 : 315002 : || same_type_p (key.ret, fndecl_declared_return_type (m_inner)))
11078 : 509654 : && type_memfn_rqual (m_type) == key.ref_q
11079 : 509304 : && compparms (key.args, TYPE_ARG_TYPES (m_type))
11080 : : /* Reject if old is a "C" builtin and new is not "C".
11081 : : Matches decls_match behaviour. */
11082 : 278521 : && (!DECL_IS_UNDECLARED_BUILTIN (m_inner)
11083 : 12054 : || !DECL_EXTERN_C_P (m_inner)
11084 : 11814 : || DECL_EXTERN_C_P (d_inner))
11085 : : /* Reject if one is a different member of a
11086 : : guarded/pre/post fn set. */
11087 : 278506 : && (!flag_contracts
11088 : 340 : || (DECL_IS_PRE_FN_P (d_inner)
11089 : 170 : == DECL_IS_PRE_FN_P (m_inner)))
11090 : 833268 : && (!flag_contracts
11091 : 340 : || (DECL_IS_POST_FN_P (d_inner)
11092 : 170 : == DECL_IS_POST_FN_P (m_inner))))
11093 : : {
11094 : 278506 : tree m_reqs = get_constraints (m_inner);
11095 : 278506 : if (m_reqs)
11096 : : {
11097 : 10005 : if (cxx_dialect < cxx20)
11098 : 10 : m_reqs = CI_ASSOCIATED_CONSTRAINTS (m_reqs);
11099 : : else
11100 : 20000 : m_reqs = CI_DECLARATOR_REQS (m_reqs);
11101 : : }
11102 : :
11103 : 278506 : if (cp_tree_equal (key.constraints, m_reqs))
11104 : 350565 : found = match;
11105 : : }
11106 : : break;
11107 : :
11108 : 129035 : case TYPE_DECL:
11109 : 258070 : if (DECL_IMPLICIT_TYPEDEF_P (d_inner)
11110 : 129035 : == DECL_IMPLICIT_TYPEDEF_P (m_inner))
11111 : : {
11112 : 128976 : if (!IDENTIFIER_ANON_P (DECL_NAME (m_inner)))
11113 : 128611 : return match;
11114 : 365 : else if (mk == MK_enum
11115 : 365 : && (TYPE_NAME (ENUM_UNDERLYING_TYPE (TREE_TYPE (m_inner)))
11116 : 365 : == key.ret))
11117 : : found = match;
11118 : : }
11119 : : break;
11120 : :
11121 : : default:
11122 : : found = match;
11123 : : break;
11124 : : }
11125 : : }
11126 : :
11127 : 439613 : return found;
11128 : : }
11129 : :
11130 : : /* DECL, INNER & TYPE are a skeleton set of nodes for a decl. Only
11131 : : the bools have been filled in. Read its merging key and merge it.
11132 : : Returns the existing decl if there is one. */
11133 : :
11134 : : tree
11135 : 1768403 : trees_in::key_mergeable (int tag, merge_kind mk, tree decl, tree inner,
11136 : : tree type, tree container, bool is_attached)
11137 : : {
11138 : 1768403 : const char *kind = "new";
11139 : 1768403 : tree existing = NULL_TREE;
11140 : :
11141 : 1768403 : if (mk & MK_template_mask)
11142 : : {
11143 : : // FIXME: We could stream the specialization hash?
11144 : 565274 : spec_entry spec;
11145 : 565274 : spec.tmpl = tree_node ();
11146 : 565274 : spec.args = tree_node ();
11147 : :
11148 : 565274 : if (get_overrun ())
11149 : 0 : return error_mark_node;
11150 : :
11151 : 565274 : DECL_NAME (decl) = DECL_NAME (spec.tmpl);
11152 : 565274 : DECL_CONTEXT (decl) = DECL_CONTEXT (spec.tmpl);
11153 : 565274 : DECL_NAME (inner) = DECL_NAME (decl);
11154 : 565274 : DECL_CONTEXT (inner) = DECL_CONTEXT (decl);
11155 : :
11156 : 565274 : tree constr = NULL_TREE;
11157 : 565274 : bool is_decl = mk & MK_tmpl_decl_mask;
11158 : 565274 : if (is_decl)
11159 : : {
11160 : 399755 : if (flag_concepts && TREE_CODE (inner) == VAR_DECL)
11161 : : {
11162 : 5973 : constr = tree_node ();
11163 : 5973 : if (constr)
11164 : 0 : set_constraints (inner, constr);
11165 : : }
11166 : 795037 : spec.spec = (mk & MK_tmpl_tmpl_mask) ? inner : decl;
11167 : : }
11168 : : else
11169 : 165519 : spec.spec = type;
11170 : 565274 : existing = match_mergeable_specialization (is_decl, &spec);
11171 : 565274 : if (constr)
11172 : : /* We'll add these back later, if this is the new decl. */
11173 : 0 : remove_constraints (inner);
11174 : :
11175 : 565274 : if (!existing)
11176 : : ; /* We'll add to the table once read. */
11177 : 328881 : else if (mk & MK_tmpl_decl_mask)
11178 : : {
11179 : : /* A declaration specialization. */
11180 : 227965 : if (mk & MK_tmpl_tmpl_mask)
11181 : 2658 : existing = DECL_TI_TEMPLATE (existing);
11182 : : }
11183 : : else
11184 : : {
11185 : : /* A type specialization. */
11186 : 100916 : if (mk & MK_tmpl_tmpl_mask)
11187 : 638 : existing = CLASSTYPE_TI_TEMPLATE (existing);
11188 : : else
11189 : 100278 : existing = TYPE_NAME (existing);
11190 : : }
11191 : : }
11192 : 1203129 : else if (mk == MK_unique)
11193 : : kind = "unique";
11194 : : else
11195 : : {
11196 : 939429 : tree name = tree_node ();
11197 : :
11198 : 939429 : merge_key key;
11199 : 939429 : unsigned code = u ();
11200 : 939429 : key.ref_q = cp_ref_qualifier ((code >> 0) & 3);
11201 : 939429 : key.index = code >> 2;
11202 : :
11203 : 939429 : if (mk == MK_enum)
11204 : 607 : key.ret = tree_node ();
11205 : 938822 : else if (mk == MK_partial
11206 : 916814 : || ((mk == MK_named || mk == MK_friend_spec)
11207 : 782022 : && TREE_CODE (inner) == FUNCTION_DECL))
11208 : : {
11209 : 457448 : key.ret = tree_node ();
11210 : 457448 : tree arg, *arg_ptr = &key.args;
11211 : 457448 : while ((arg = tree_node ())
11212 : 1311536 : && arg != void_list_node
11213 : 2194112 : && mk != MK_partial)
11214 : : {
11215 : 857328 : *arg_ptr = tree_cons (NULL_TREE, arg, NULL_TREE);
11216 : 857328 : arg_ptr = &TREE_CHAIN (*arg_ptr);
11217 : : }
11218 : 457448 : *arg_ptr = arg;
11219 : 457448 : key.constraints = tree_node ();
11220 : : }
11221 : :
11222 : 939429 : if (get_overrun ())
11223 : 0 : return error_mark_node;
11224 : :
11225 : 939429 : if (mk < MK_indirect_lwm)
11226 : : {
11227 : 926677 : DECL_NAME (decl) = name;
11228 : 926677 : DECL_CONTEXT (decl) = FROB_CONTEXT (container);
11229 : : }
11230 : 939429 : DECL_NAME (inner) = DECL_NAME (decl);
11231 : 939429 : DECL_CONTEXT (inner) = DECL_CONTEXT (decl);
11232 : :
11233 : 939429 : if (mk == MK_partial)
11234 : : {
11235 : 22008 : for (tree spec = DECL_TEMPLATE_SPECIALIZATIONS (key.ret);
11236 : 104945 : spec; spec = TREE_CHAIN (spec))
11237 : : {
11238 : 96842 : tree tmpl = TREE_VALUE (spec);
11239 : 96842 : tree ti = get_template_info (tmpl);
11240 : 96842 : if (template_args_equal (key.args, TI_ARGS (ti))
11241 : 111711 : && cp_tree_equal (key.constraints,
11242 : : get_constraints
11243 : 14869 : (DECL_TEMPLATE_RESULT (tmpl))))
11244 : : {
11245 : : existing = tmpl;
11246 : : break;
11247 : : }
11248 : : }
11249 : : }
11250 : 917421 : else if (mk == MK_keyed
11251 : 68 : && DECL_LANG_SPECIFIC (name)
11252 : 917489 : && DECL_MODULE_KEYED_DECLS_P (name))
11253 : : {
11254 : 68 : gcc_checking_assert (TREE_CODE (container) == NAMESPACE_DECL
11255 : : || TREE_CODE (container) == TYPE_DECL);
11256 : 68 : if (auto *set = keyed_table->get (name))
11257 : 100 : if (key.index < set->length ())
11258 : : {
11259 : 38 : existing = (*set)[key.index];
11260 : 38 : if (existing)
11261 : : {
11262 : 38 : gcc_checking_assert
11263 : : (DECL_IMPLICIT_TYPEDEF_P (existing));
11264 : 38 : if (inner != decl)
11265 : 8 : existing
11266 : 8 : = CLASSTYPE_TI_TEMPLATE (TREE_TYPE (existing));
11267 : : }
11268 : : }
11269 : : }
11270 : : else
11271 : 917353 : switch (TREE_CODE (container))
11272 : : {
11273 : 0 : default:
11274 : 0 : gcc_unreachable ();
11275 : :
11276 : 251146 : case NAMESPACE_DECL:
11277 : 251146 : if (is_attached
11278 : 251146 : && !(state->is_module () || state->is_partition ()))
11279 : : kind = "unique";
11280 : : else
11281 : : {
11282 : 249870 : gcc_checking_assert (mk == MK_named || mk == MK_enum);
11283 : 249870 : tree mvec;
11284 : 249870 : tree *vslot = mergeable_namespace_slots (container, name,
11285 : : is_attached, &mvec);
11286 : 249870 : existing = check_mergeable_decl (mk, decl, *vslot, key);
11287 : 249870 : if (!existing)
11288 : 88148 : add_mergeable_namespace_entity (vslot, decl);
11289 : : else
11290 : : {
11291 : : /* Note that we now have duplicates to deal with in
11292 : : name lookup. */
11293 : 161722 : if (is_attached)
11294 : 15 : BINDING_VECTOR_PARTITION_DUPS_P (mvec) = true;
11295 : : else
11296 : 161707 : BINDING_VECTOR_GLOBAL_DUPS_P (mvec) = true;
11297 : : }
11298 : : }
11299 : : break;
11300 : :
11301 : 5376 : case FUNCTION_DECL:
11302 : 5376 : gcc_checking_assert (mk == MK_local_type);
11303 : 5376 : existing = key_local_type (key, container, name);
11304 : 5376 : if (existing && inner != decl)
11305 : 6392 : existing = TYPE_TI_TEMPLATE (TREE_TYPE (existing));
11306 : : break;
11307 : :
11308 : 660831 : case TYPE_DECL:
11309 : 3134 : if (is_attached && !(state->is_module () || state->is_partition ())
11310 : : /* Implicit member functions can come from
11311 : : anywhere. */
11312 : 663230 : && !(DECL_ARTIFICIAL (decl)
11313 : 1417 : && TREE_CODE (decl) == FUNCTION_DECL
11314 : 463 : && !DECL_THUNK_P (decl)))
11315 : : kind = "unique";
11316 : : else
11317 : : {
11318 : 658895 : tree ctx = TREE_TYPE (container);
11319 : :
11320 : : /* For some reason templated enumeral types are not marked
11321 : : as COMPLETE_TYPE_P, even though they have members.
11322 : : This may well be a bug elsewhere. */
11323 : 658895 : if (TREE_CODE (ctx) == ENUMERAL_TYPE)
11324 : 21828 : existing = find_enum_member (ctx, name);
11325 : 637067 : else if (COMPLETE_TYPE_P (ctx))
11326 : : {
11327 : 397317 : switch (mk)
11328 : : {
11329 : 0 : default:
11330 : 0 : gcc_unreachable ();
11331 : :
11332 : 318927 : case MK_named:
11333 : 318927 : existing = lookup_class_binding (ctx, name);
11334 : 318927 : if (existing)
11335 : : {
11336 : 318354 : tree inner = decl;
11337 : 318354 : if (TREE_CODE (inner) == TEMPLATE_DECL
11338 : 318354 : && !DECL_MEMBER_TEMPLATE_P (inner))
11339 : 150510 : inner = DECL_TEMPLATE_RESULT (inner);
11340 : :
11341 : 318354 : existing = check_mergeable_decl
11342 : 318354 : (mk, inner, existing, key);
11343 : :
11344 : 318354 : if (!existing && DECL_ALIAS_TEMPLATE_P (decl))
11345 : : {} // FIXME: Insert into specialization
11346 : : // tables, we'll need the arguments for that!
11347 : : }
11348 : : break;
11349 : :
11350 : 54235 : case MK_field:
11351 : 54235 : {
11352 : 54235 : unsigned ix = key.index;
11353 : 54235 : for (tree field = TYPE_FIELDS (ctx);
11354 : 156265 : field; field = DECL_CHAIN (field))
11355 : : {
11356 : 156265 : tree finner = STRIP_TEMPLATE (field);
11357 : 156265 : if (TREE_CODE (finner) == TREE_CODE (inner))
11358 : 72724 : if (!ix--)
11359 : : {
11360 : : existing = field;
11361 : : break;
11362 : : }
11363 : : }
11364 : : }
11365 : : break;
11366 : :
11367 : 3545 : case MK_vtable:
11368 : 3545 : {
11369 : 3545 : unsigned ix = key.index;
11370 : 3545 : for (tree vtable = CLASSTYPE_VTABLES (ctx);
11371 : 4613 : vtable; vtable = DECL_CHAIN (vtable))
11372 : 4234 : if (!ix--)
11373 : : {
11374 : : existing = vtable;
11375 : : break;
11376 : : }
11377 : : }
11378 : : break;
11379 : :
11380 : 16096 : case MK_as_base:
11381 : 16096 : {
11382 : 16096 : tree as_base = CLASSTYPE_AS_BASE (ctx);
11383 : 16096 : if (as_base && as_base != ctx)
11384 : 16096 : existing = TYPE_NAME (as_base);
11385 : : }
11386 : : break;
11387 : :
11388 : 4514 : case MK_local_friend:
11389 : 4514 : {
11390 : 4514 : unsigned ix = key.index;
11391 : 4514 : for (tree decls = CLASSTYPE_DECL_LIST (ctx);
11392 : 127863 : decls; decls = TREE_CHAIN (decls))
11393 : 127863 : if (!TREE_PURPOSE (decls) && !ix--)
11394 : : {
11395 : 4514 : existing
11396 : 4514 : = friend_from_decl_list (TREE_VALUE (decls));
11397 : 4514 : break;
11398 : : }
11399 : : }
11400 : : break;
11401 : : }
11402 : :
11403 : 397317 : if (existing && mk < MK_indirect_lwm && mk != MK_partial
11404 : 390951 : && TREE_CODE (decl) == TEMPLATE_DECL
11405 : 585009 : && !DECL_MEMBER_TEMPLATE_P (decl))
11406 : : {
11407 : 154209 : tree ti;
11408 : 154209 : if (DECL_IMPLICIT_TYPEDEF_P (existing))
11409 : 1874 : ti = TYPE_TEMPLATE_INFO (TREE_TYPE (existing));
11410 : : else
11411 : 152335 : ti = DECL_TEMPLATE_INFO (existing);
11412 : 154209 : existing = TI_TEMPLATE (ti);
11413 : : }
11414 : : }
11415 : : }
11416 : : }
11417 : : }
11418 : :
11419 : 1768403 : dump (dumper::MERGE)
11420 : 2284 : && dump ("Read:%d's %s merge key (%s) %C:%N", tag, merge_kind_name[mk],
11421 : 2284 : existing ? "matched" : kind, TREE_CODE (decl), decl);
11422 : :
11423 : : return existing;
11424 : : }
11425 : :
11426 : : void
11427 : 392978 : trees_out::binfo_mergeable (tree binfo)
11428 : : {
11429 : 392978 : tree dom = binfo;
11430 : 512451 : while (tree parent = BINFO_INHERITANCE_CHAIN (dom))
11431 : : dom = parent;
11432 : 392978 : tree type = BINFO_TYPE (dom);
11433 : 392978 : gcc_checking_assert (TYPE_BINFO (type) == dom);
11434 : 392978 : tree_node (type);
11435 : 392978 : if (streaming_p ())
11436 : : {
11437 : : unsigned ix = 0;
11438 : 232018 : for (; dom != binfo; dom = TREE_CHAIN (dom))
11439 : 69198 : ix++;
11440 : 162820 : u (ix);
11441 : : }
11442 : 392978 : }
11443 : :
11444 : : unsigned
11445 : 158868 : trees_in::binfo_mergeable (tree *type)
11446 : : {
11447 : 158868 : *type = tree_node ();
11448 : 158868 : return u ();
11449 : : }
11450 : :
11451 : : /* DECL is a just streamed mergeable decl that should match EXISTING. Check
11452 : : it does and issue an appropriate diagnostic if not. Merge any
11453 : : bits from DECL to EXISTING. This is stricter matching than
11454 : : decls_match, because we can rely on ODR-sameness, and we cannot use
11455 : : decls_match because it can cause instantiations of constraints. */
11456 : :
11457 : : bool
11458 : 917065 : trees_in::is_matching_decl (tree existing, tree decl, bool is_typedef)
11459 : : {
11460 : : // FIXME: We should probably do some duplicate decl-like stuff here
11461 : : // (beware, default parms should be the same?) Can we just call
11462 : : // duplicate_decls and teach it how to handle the module-specific
11463 : : // permitted/required duplications?
11464 : :
11465 : : // We know at this point that the decls have matched by key, so we
11466 : : // can elide some of the checking
11467 : 917065 : gcc_checking_assert (TREE_CODE (existing) == TREE_CODE (decl));
11468 : :
11469 : 917065 : tree d_inner = decl;
11470 : 917065 : tree e_inner = existing;
11471 : 917065 : if (TREE_CODE (decl) == TEMPLATE_DECL)
11472 : : {
11473 : 305536 : d_inner = DECL_TEMPLATE_RESULT (d_inner);
11474 : 305536 : e_inner = DECL_TEMPLATE_RESULT (e_inner);
11475 : 305536 : gcc_checking_assert (TREE_CODE (e_inner) == TREE_CODE (d_inner));
11476 : : }
11477 : :
11478 : 917065 : if (TREE_CODE (d_inner) == FUNCTION_DECL)
11479 : : {
11480 : 425594 : tree e_ret = fndecl_declared_return_type (existing);
11481 : 425594 : tree d_ret = fndecl_declared_return_type (decl);
11482 : :
11483 : 179759 : if (decl != d_inner && DECL_NAME (d_inner) == fun_identifier
11484 : 425610 : && LAMBDA_TYPE_P (DECL_CONTEXT (d_inner)))
11485 : : /* This has a recursive type that will compare different. */;
11486 : 425586 : else if (!same_type_p (d_ret, e_ret))
11487 : 6 : goto mismatch;
11488 : :
11489 : 425588 : tree e_type = TREE_TYPE (e_inner);
11490 : 425588 : tree d_type = TREE_TYPE (d_inner);
11491 : :
11492 : 425588 : if (DECL_EXTERN_C_P (d_inner) != DECL_EXTERN_C_P (e_inner))
11493 : 0 : goto mismatch;
11494 : :
11495 : 425588 : for (tree e_args = TYPE_ARG_TYPES (e_type),
11496 : 425588 : d_args = TYPE_ARG_TYPES (d_type);
11497 : 726244 : e_args != d_args && (e_args || d_args);
11498 : 300656 : e_args = TREE_CHAIN (e_args), d_args = TREE_CHAIN (d_args))
11499 : : {
11500 : 300656 : if (!(e_args && d_args))
11501 : 0 : goto mismatch;
11502 : :
11503 : 300656 : if (!same_type_p (TREE_VALUE (d_args), TREE_VALUE (e_args)))
11504 : 0 : goto mismatch;
11505 : :
11506 : : // FIXME: Check default values
11507 : : }
11508 : :
11509 : : /* If EXISTING has an undeduced or uninstantiated exception
11510 : : specification, but DECL does not, propagate the exception
11511 : : specification. Otherwise we end up asserting or trying to
11512 : : instantiate it in the middle of loading. */
11513 : 425588 : tree e_spec = TYPE_RAISES_EXCEPTIONS (e_type);
11514 : 425588 : tree d_spec = TYPE_RAISES_EXCEPTIONS (d_type);
11515 : 623387 : if (DEFERRED_NOEXCEPT_SPEC_P (e_spec))
11516 : : {
11517 : 31078 : if (!DEFERRED_NOEXCEPT_SPEC_P (d_spec)
11518 : 61843 : || (UNEVALUATED_NOEXCEPT_SPEC_P (e_spec)
11519 : 25230 : && !UNEVALUATED_NOEXCEPT_SPEC_P (d_spec)))
11520 : : {
11521 : 313 : dump (dumper::MERGE)
11522 : 3 : && dump ("Propagating instantiated noexcept to %N", existing);
11523 : 313 : TREE_TYPE (existing) = d_type;
11524 : :
11525 : : /* Propagate to existing clones. */
11526 : 313 : tree clone;
11527 : 903 : FOR_EACH_CLONE (clone, existing)
11528 : : {
11529 : 590 : if (TREE_TYPE (clone) == e_type)
11530 : 384 : TREE_TYPE (clone) = d_type;
11531 : : else
11532 : 206 : TREE_TYPE (clone)
11533 : 412 : = build_exception_variant (TREE_TYPE (clone), d_spec);
11534 : : }
11535 : : }
11536 : : }
11537 : 168878 : else if (!DEFERRED_NOEXCEPT_SPEC_P (d_spec)
11538 : 563388 : && !comp_except_specs (d_spec, e_spec, ce_type))
11539 : 2235 : goto mismatch;
11540 : : }
11541 : 491471 : else if (is_typedef)
11542 : : {
11543 : 183286 : if (!DECL_ORIGINAL_TYPE (e_inner)
11544 : 183286 : || !same_type_p (DECL_ORIGINAL_TYPE (d_inner),
11545 : : DECL_ORIGINAL_TYPE (e_inner)))
11546 : 0 : goto mismatch;
11547 : : }
11548 : : /* Using cp_tree_equal because we can meet TYPE_ARGUMENT_PACKs
11549 : : here. I suspect the entities that directly do that are things
11550 : : that shouldn't go to duplicate_decls (FIELD_DECLs etc). */
11551 : 308185 : else if (!cp_tree_equal (TREE_TYPE (decl), TREE_TYPE (existing)))
11552 : : {
11553 : 1582 : mismatch:
11554 : 3823 : if (DECL_IS_UNDECLARED_BUILTIN (existing))
11555 : : /* Just like duplicate_decls, presum the user knows what
11556 : : they're doing in overriding a builtin. */
11557 : 2238 : TREE_TYPE (existing) = TREE_TYPE (decl);
11558 : 1585 : else if (decl_function_context (decl))
11559 : : /* The type of a mergeable local entity (such as a function scope
11560 : : capturing lambda's closure type fields) can depend on an
11561 : : unmergeable local entity (such as a local variable), so type
11562 : : equality isn't feasible in general for local entities. */;
11563 : : else
11564 : : {
11565 : : // FIXME:QOI Might be template specialization from a module,
11566 : : // not necessarily global module
11567 : 9 : error_at (DECL_SOURCE_LOCATION (decl),
11568 : : "conflicting global module declaration %#qD", decl);
11569 : 9 : inform (DECL_SOURCE_LOCATION (existing),
11570 : : "existing declaration %#qD", existing);
11571 : 9 : return false;
11572 : : }
11573 : : }
11574 : :
11575 : 917056 : if (DECL_IS_UNDECLARED_BUILTIN (existing)
11576 : 917056 : && !DECL_IS_UNDECLARED_BUILTIN (decl))
11577 : : {
11578 : : /* We're matching a builtin that the user has yet to declare.
11579 : : We are the one! This is very much duplicate-decl
11580 : : shenanigans. */
11581 : 2609 : DECL_SOURCE_LOCATION (existing) = DECL_SOURCE_LOCATION (decl);
11582 : 2609 : if (TREE_CODE (decl) != TYPE_DECL)
11583 : : {
11584 : : /* Propagate exceptions etc. */
11585 : 2589 : TREE_TYPE (existing) = TREE_TYPE (decl);
11586 : 2589 : TREE_NOTHROW (existing) = TREE_NOTHROW (decl);
11587 : : }
11588 : : /* This is actually an import! */
11589 : 2609 : DECL_MODULE_IMPORT_P (existing) = true;
11590 : :
11591 : : /* Yay, sliced! */
11592 : 2609 : existing->base = decl->base;
11593 : :
11594 : 2609 : if (TREE_CODE (decl) == FUNCTION_DECL)
11595 : : {
11596 : : /* Ew :( */
11597 : 2589 : memcpy (&existing->decl_common.size,
11598 : : &decl->decl_common.size,
11599 : : (offsetof (tree_decl_common, pt_uid)
11600 : : - offsetof (tree_decl_common, size)));
11601 : 2589 : auto bltin_class = DECL_BUILT_IN_CLASS (decl);
11602 : 2589 : existing->function_decl.built_in_class = bltin_class;
11603 : 2589 : auto fncode = DECL_UNCHECKED_FUNCTION_CODE (decl);
11604 : 2589 : DECL_UNCHECKED_FUNCTION_CODE (existing) = fncode;
11605 : 2589 : if (existing->function_decl.built_in_class == BUILT_IN_NORMAL)
11606 : : {
11607 : 2355 : if (builtin_decl_explicit_p (built_in_function (fncode)))
11608 : 2355 : switch (fncode)
11609 : : {
11610 : 0 : case BUILT_IN_STPCPY:
11611 : 0 : set_builtin_decl_implicit_p
11612 : 0 : (built_in_function (fncode), true);
11613 : 0 : break;
11614 : 2355 : default:
11615 : 2355 : set_builtin_decl_declared_p
11616 : 2355 : (built_in_function (fncode), true);
11617 : 2355 : break;
11618 : : }
11619 : 2355 : copy_attributes_to_builtin (decl);
11620 : : }
11621 : : }
11622 : : }
11623 : :
11624 : 917056 : if (VAR_OR_FUNCTION_DECL_P (decl)
11625 : 917056 : && DECL_TEMPLATE_INSTANTIATED (decl))
11626 : : /* Don't instantiate again! */
11627 : 16972 : DECL_TEMPLATE_INSTANTIATED (existing) = true;
11628 : :
11629 : 917056 : if (TREE_CODE (d_inner) == FUNCTION_DECL
11630 : 917056 : && DECL_DECLARED_INLINE_P (d_inner))
11631 : 329595 : DECL_DECLARED_INLINE_P (e_inner) = true;
11632 : 917056 : if (!DECL_EXTERNAL (d_inner))
11633 : 396538 : DECL_EXTERNAL (e_inner) = false;
11634 : :
11635 : : // FIXME: Check default tmpl and fn parms here
11636 : :
11637 : : return true;
11638 : : }
11639 : :
11640 : : /* FN is an implicit member function that we've discovered is new to
11641 : : the class. Add it to the TYPE_FIELDS chain and the method vector.
11642 : : Reset the appropriate classtype lazy flag. */
11643 : :
11644 : : bool
11645 : 1473 : trees_in::install_implicit_member (tree fn)
11646 : : {
11647 : 1473 : tree ctx = DECL_CONTEXT (fn);
11648 : 1473 : tree name = DECL_NAME (fn);
11649 : : /* We know these are synthesized, so the set of expected prototypes
11650 : : is quite restricted. We're not validating correctness, just
11651 : : distinguishing beteeen the small set of possibilities. */
11652 : 1473 : tree parm_type = TREE_VALUE (FUNCTION_FIRST_USER_PARMTYPE (fn));
11653 : 1473 : if (IDENTIFIER_CTOR_P (name))
11654 : : {
11655 : 1068 : if (CLASSTYPE_LAZY_DEFAULT_CTOR (ctx)
11656 : 1068 : && VOID_TYPE_P (parm_type))
11657 : 210 : CLASSTYPE_LAZY_DEFAULT_CTOR (ctx) = false;
11658 : 858 : else if (!TYPE_REF_P (parm_type))
11659 : : return false;
11660 : 858 : else if (CLASSTYPE_LAZY_COPY_CTOR (ctx)
11661 : 858 : && !TYPE_REF_IS_RVALUE (parm_type))
11662 : 450 : CLASSTYPE_LAZY_COPY_CTOR (ctx) = false;
11663 : 408 : else if (CLASSTYPE_LAZY_MOVE_CTOR (ctx))
11664 : 408 : CLASSTYPE_LAZY_MOVE_CTOR (ctx) = false;
11665 : : else
11666 : : return false;
11667 : : }
11668 : 405 : else if (IDENTIFIER_DTOR_P (name))
11669 : : {
11670 : 321 : if (CLASSTYPE_LAZY_DESTRUCTOR (ctx))
11671 : 321 : CLASSTYPE_LAZY_DESTRUCTOR (ctx) = false;
11672 : : else
11673 : : return false;
11674 : 321 : if (DECL_VIRTUAL_P (fn))
11675 : : /* A virtual dtor should have been created when the class
11676 : : became complete. */
11677 : : return false;
11678 : : }
11679 : 84 : else if (name == assign_op_identifier)
11680 : : {
11681 : 84 : if (!TYPE_REF_P (parm_type))
11682 : : return false;
11683 : 84 : else if (CLASSTYPE_LAZY_COPY_ASSIGN (ctx)
11684 : 84 : && !TYPE_REF_IS_RVALUE (parm_type))
11685 : 42 : CLASSTYPE_LAZY_COPY_ASSIGN (ctx) = false;
11686 : 42 : else if (CLASSTYPE_LAZY_MOVE_ASSIGN (ctx))
11687 : 42 : CLASSTYPE_LAZY_MOVE_ASSIGN (ctx) = false;
11688 : : else
11689 : : return false;
11690 : : }
11691 : : else
11692 : : return false;
11693 : :
11694 : 1503 : dump (dumper::MERGE) && dump ("Adding implicit member %N", fn);
11695 : :
11696 : 1473 : DECL_CHAIN (fn) = TYPE_FIELDS (ctx);
11697 : 1473 : TYPE_FIELDS (ctx) = fn;
11698 : :
11699 : 1473 : add_method (ctx, fn, false);
11700 : :
11701 : : /* Propagate TYPE_FIELDS. */
11702 : 1473 : fixup_type_variants (ctx);
11703 : :
11704 : 1473 : return true;
11705 : : }
11706 : :
11707 : : /* Return non-zero if DECL has a definition that would be interesting to
11708 : : write out. */
11709 : :
11710 : : static bool
11711 : 1769551 : has_definition (tree decl)
11712 : : {
11713 : 1769551 : bool is_tmpl = TREE_CODE (decl) == TEMPLATE_DECL;
11714 : 1769551 : if (is_tmpl)
11715 : 460476 : decl = DECL_TEMPLATE_RESULT (decl);
11716 : :
11717 : 1769551 : switch (TREE_CODE (decl))
11718 : : {
11719 : : default:
11720 : : break;
11721 : :
11722 : 666433 : case FUNCTION_DECL:
11723 : 666433 : if (!DECL_SAVED_TREE (decl))
11724 : : /* Not defined. */
11725 : : break;
11726 : :
11727 : 291211 : if (DECL_DECLARED_INLINE_P (decl))
11728 : : return true;
11729 : :
11730 : 21653 : if (DECL_THIS_STATIC (decl)
11731 : 21653 : && (header_module_p ()
11732 : 39 : || (!DECL_LANG_SPECIFIC (decl) || !DECL_MODULE_PURVIEW_P (decl))))
11733 : : /* GM static function. */
11734 : : return true;
11735 : :
11736 : 21456 : if (DECL_TEMPLATE_INFO (decl))
11737 : : {
11738 : 21032 : int use_tpl = DECL_USE_TEMPLATE (decl);
11739 : :
11740 : : // FIXME: Partial specializations have definitions too.
11741 : 21032 : if (use_tpl < 2)
11742 : 21008 : return true;
11743 : : }
11744 : : break;
11745 : :
11746 : 740672 : case TYPE_DECL:
11747 : 740672 : {
11748 : 740672 : tree type = TREE_TYPE (decl);
11749 : 740672 : if (type == TYPE_MAIN_VARIANT (type)
11750 : 338737 : && decl == TYPE_NAME (type)
11751 : 1079409 : && (TREE_CODE (type) == ENUMERAL_TYPE
11752 : 338737 : ? TYPE_VALUES (type) : TYPE_FIELDS (type)))
11753 : 167103 : return true;
11754 : : }
11755 : : break;
11756 : :
11757 : 69476 : case VAR_DECL:
11758 : : /* DECL_INITIALIZED_P might not be set on a dependent VAR_DECL. */
11759 : 69476 : if (DECL_LANG_SPECIFIC (decl)
11760 : 69449 : && DECL_TEMPLATE_INFO (decl)
11761 : 108617 : && DECL_INITIAL (decl))
11762 : : return true;
11763 : : else
11764 : : {
11765 : 34907 : if (!DECL_INITIALIZED_P (decl))
11766 : : return false;
11767 : :
11768 : 28395 : if (header_module_p ()
11769 : 28395 : || (!DECL_LANG_SPECIFIC (decl) || !DECL_MODULE_PURVIEW_P (decl)))
11770 : : /* GM static variable. */
11771 : : return true;
11772 : :
11773 : 145 : if (!TREE_CONSTANT (decl))
11774 : : return false;
11775 : :
11776 : 41 : return true;
11777 : : }
11778 : 7078 : break;
11779 : :
11780 : 7078 : case CONCEPT_DECL:
11781 : 7078 : if (DECL_INITIAL (decl))
11782 : 7078 : return true;
11783 : :
11784 : : break;
11785 : : }
11786 : :
11787 : : return false;
11788 : : }
11789 : :
11790 : : uintptr_t *
11791 : 993989 : trees_in::find_duplicate (tree existing)
11792 : : {
11793 : 503480 : if (!duplicates)
11794 : : return NULL;
11795 : :
11796 : 722981 : return duplicates->get (existing);
11797 : : }
11798 : :
11799 : : /* We're starting to read a duplicate DECL. EXISTING is the already
11800 : : known node. */
11801 : :
11802 : : void
11803 : 1315628 : trees_in::register_duplicate (tree decl, tree existing)
11804 : : {
11805 : 1315628 : if (!duplicates)
11806 : 212439 : duplicates = new duplicate_hash_map (40);
11807 : :
11808 : 1315628 : bool existed;
11809 : 1315628 : uintptr_t &slot = duplicates->get_or_insert (existing, &existed);
11810 : 1315628 : gcc_checking_assert (!existed);
11811 : 1315628 : slot = reinterpret_cast<uintptr_t> (decl);
11812 : :
11813 : 1315628 : if (TREE_CODE (decl) == TEMPLATE_DECL)
11814 : : /* Also register the DECL_TEMPLATE_RESULT as a duplicate so
11815 : : that passing decl's _RESULT to maybe_duplicate naturally
11816 : : gives us existing's _RESULT back. */
11817 : 611072 : register_duplicate (DECL_TEMPLATE_RESULT (decl),
11818 : 305536 : DECL_TEMPLATE_RESULT (existing));
11819 : 1315628 : }
11820 : :
11821 : : /* We've read a definition of MAYBE_EXISTING. If not a duplicate,
11822 : : return MAYBE_EXISTING (into which the definition should be
11823 : : installed). Otherwise return NULL if already known bad, or the
11824 : : duplicate we read (for ODR checking, or extracting additional merge
11825 : : information). */
11826 : :
11827 : : tree
11828 : 490509 : trees_in::odr_duplicate (tree maybe_existing, bool has_defn)
11829 : : {
11830 : 490509 : tree res = NULL_TREE;
11831 : :
11832 : 799033 : if (uintptr_t *dup = find_duplicate (maybe_existing))
11833 : : {
11834 : 297456 : if (!(*dup & 1))
11835 : 297456 : res = reinterpret_cast<tree> (*dup);
11836 : : }
11837 : : else
11838 : : res = maybe_existing;
11839 : :
11840 : 490509 : assert_definition (maybe_existing, res && !has_defn);
11841 : :
11842 : : // FIXME: We probably need to return the template, so that the
11843 : : // template header can be checked?
11844 : 490509 : return res ? STRIP_TEMPLATE (res) : NULL_TREE;
11845 : : }
11846 : :
11847 : : /* The following writer functions rely on the current behaviour of
11848 : : depset::hash::add_dependency making the decl and defn depset nodes
11849 : : depend on eachother. That way we don't have to worry about seeding
11850 : : the tree map with named decls that cannot be looked up by name (I.e
11851 : : template and function parms). We know the decl and definition will
11852 : : be in the same cluster, which is what we want. */
11853 : :
11854 : : void
11855 : 567513 : trees_out::write_function_def (tree decl)
11856 : : {
11857 : 567513 : tree_node (DECL_RESULT (decl));
11858 : 567513 : tree_node (DECL_INITIAL (decl));
11859 : 567513 : tree_node (DECL_SAVED_TREE (decl));
11860 : 1211556 : tree_node (DECL_FRIEND_CONTEXT (decl));
11861 : :
11862 : 567513 : constexpr_fundef *cexpr = retrieve_constexpr_fundef (decl);
11863 : :
11864 : 567513 : if (streaming_p ())
11865 : 283755 : u (cexpr != nullptr);
11866 : 567513 : if (cexpr)
11867 : : {
11868 : 105704 : chained_decls (cexpr->parms);
11869 : 105704 : tree_node (cexpr->result);
11870 : 105704 : tree_node (cexpr->body);
11871 : : }
11872 : :
11873 : 567513 : function* f = DECL_STRUCT_FUNCTION (decl);
11874 : :
11875 : 567513 : if (streaming_p ())
11876 : : {
11877 : 283755 : unsigned flags = 0;
11878 : :
11879 : 283755 : if (f)
11880 : 283125 : flags |= 2;
11881 : 283755 : if (DECL_NOT_REALLY_EXTERN (decl))
11882 : 89605 : flags |= 1;
11883 : :
11884 : 283755 : u (flags);
11885 : : }
11886 : :
11887 : 567513 : if (state && f)
11888 : : {
11889 : 566253 : state->write_location (*this, f->function_start_locus);
11890 : 566253 : state->write_location (*this, f->function_end_locus);
11891 : : }
11892 : 567513 : }
11893 : :
11894 : : void
11895 : 0 : trees_out::mark_function_def (tree)
11896 : : {
11897 : 0 : }
11898 : :
11899 : : bool
11900 : 314228 : trees_in::read_function_def (tree decl, tree maybe_template)
11901 : : {
11902 : 314616 : dump () && dump ("Reading function definition %N", decl);
11903 : 314228 : tree result = tree_node ();
11904 : 314228 : tree initial = tree_node ();
11905 : 314228 : tree saved = tree_node ();
11906 : 314228 : tree context = tree_node ();
11907 : 314228 : constexpr_fundef cexpr;
11908 : 314228 : post_process_data pdata {};
11909 : 314228 : pdata.decl = maybe_template;
11910 : :
11911 : 314228 : tree maybe_dup = odr_duplicate (maybe_template, DECL_SAVED_TREE (decl));
11912 : 628456 : bool installing = maybe_dup && !DECL_SAVED_TREE (decl);
11913 : :
11914 : 314228 : if (u ())
11915 : : {
11916 : 56561 : cexpr.parms = chained_decls ();
11917 : 56561 : cexpr.result = tree_node ();
11918 : 56561 : cexpr.body = tree_node ();
11919 : 56561 : cexpr.decl = decl;
11920 : : }
11921 : : else
11922 : 257667 : cexpr.decl = NULL_TREE;
11923 : :
11924 : 314228 : unsigned flags = u ();
11925 : :
11926 : 314228 : if (flags & 2)
11927 : : {
11928 : 313495 : pdata.start_locus = state->read_location (*this);
11929 : 313495 : pdata.end_locus = state->read_location (*this);
11930 : : }
11931 : :
11932 : 314228 : if (get_overrun ())
11933 : : return NULL_TREE;
11934 : :
11935 : 314228 : if (installing)
11936 : : {
11937 : 135521 : DECL_NOT_REALLY_EXTERN (decl) = flags & 1;
11938 : 135521 : DECL_RESULT (decl) = result;
11939 : 135521 : DECL_INITIAL (decl) = initial;
11940 : 135521 : DECL_SAVED_TREE (decl) = saved;
11941 : :
11942 : 135521 : if (context)
11943 : 3668 : SET_DECL_FRIEND_CONTEXT (decl, context);
11944 : 135521 : if (cexpr.decl)
11945 : 34655 : register_constexpr_fundef (cexpr);
11946 : 135521 : post_process (pdata);
11947 : : }
11948 : : else if (maybe_dup)
11949 : : {
11950 : : // FIXME:QOI Check matching defn
11951 : : }
11952 : :
11953 : : return true;
11954 : : }
11955 : :
11956 : : /* Also for CONCEPT_DECLs. */
11957 : :
11958 : : void
11959 : 117898 : trees_out::write_var_def (tree decl)
11960 : : {
11961 : 117898 : tree init = DECL_INITIAL (decl);
11962 : 117898 : tree_node (init);
11963 : 117898 : if (!init)
11964 : : {
11965 : 1118 : tree dyn_init = NULL_TREE;
11966 : :
11967 : : /* We only need to write initializers in header modules. */
11968 : 2172 : if (header_module_p () && DECL_NONTRIVIALLY_INITIALIZED_P (decl))
11969 : : {
11970 : 52 : dyn_init = value_member (decl,
11971 : 52 : CP_DECL_THREAD_LOCAL_P (decl)
11972 : : ? tls_aggregates : static_aggregates);
11973 : 52 : gcc_checking_assert (dyn_init);
11974 : : /* Mark it so write_inits knows this is needed. */
11975 : 52 : TREE_LANG_FLAG_0 (dyn_init) = true;
11976 : 52 : dyn_init = TREE_PURPOSE (dyn_init);
11977 : : }
11978 : 1118 : tree_node (dyn_init);
11979 : : }
11980 : 117898 : }
11981 : :
11982 : : void
11983 : 0 : trees_out::mark_var_def (tree)
11984 : : {
11985 : 0 : }
11986 : :
11987 : : bool
11988 : 61017 : trees_in::read_var_def (tree decl, tree maybe_template)
11989 : : {
11990 : : /* Do not mark the virtual table entries as used. */
11991 : 61017 : bool vtable = VAR_P (decl) && DECL_VTABLE_OR_VTT_P (decl);
11992 : 61017 : unused += vtable;
11993 : 61017 : tree init = tree_node ();
11994 : 61017 : tree dyn_init = init ? NULL_TREE : tree_node ();
11995 : 61017 : unused -= vtable;
11996 : :
11997 : 61017 : if (get_overrun ())
11998 : : return false;
11999 : :
12000 : 61017 : bool initialized = (VAR_P (decl) ? bool (DECL_INITIALIZED_P (decl))
12001 : 6505 : : bool (DECL_INITIAL (decl)));
12002 : 61017 : tree maybe_dup = odr_duplicate (maybe_template, initialized);
12003 : 61017 : bool installing = maybe_dup && !initialized;
12004 : 61017 : if (installing)
12005 : : {
12006 : 33164 : if (DECL_EXTERNAL (decl))
12007 : 16642 : DECL_NOT_REALLY_EXTERN (decl) = true;
12008 : 33164 : if (VAR_P (decl))
12009 : : {
12010 : 30361 : DECL_INITIALIZED_P (decl) = true;
12011 : 30361 : if (maybe_dup && DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (maybe_dup))
12012 : 30194 : DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
12013 : 30361 : if (DECL_IMPLICIT_INSTANTIATION (decl)
12014 : 30361 : || (DECL_CLASS_SCOPE_P (decl)
12015 : 18384 : && !DECL_VTABLE_OR_VTT_P (decl)
12016 : 16084 : && !DECL_TEMPLATE_INFO (decl)))
12017 : 11896 : note_vague_linkage_variable (decl);
12018 : : }
12019 : 33164 : DECL_INITIAL (decl) = init;
12020 : 33164 : if (!dyn_init)
12021 : : ;
12022 : 18 : else if (CP_DECL_THREAD_LOCAL_P (decl))
12023 : 0 : tls_aggregates = tree_cons (dyn_init, decl, tls_aggregates);
12024 : : else
12025 : 18 : static_aggregates = tree_cons (dyn_init, decl, static_aggregates);
12026 : : }
12027 : : else if (maybe_dup)
12028 : : {
12029 : : // FIXME:QOI Check matching defn
12030 : : }
12031 : :
12032 : : return true;
12033 : : }
12034 : :
12035 : : /* If MEMBER doesn't have an independent life outside the class,
12036 : : return it (or its TEMPLATE_DECL). Otherwise NULL. */
12037 : :
12038 : : static tree
12039 : 281854 : member_owned_by_class (tree member)
12040 : : {
12041 : 281854 : gcc_assert (DECL_P (member));
12042 : :
12043 : : /* Clones are owned by their origin. */
12044 : 281854 : if (DECL_CLONED_FUNCTION_P (member))
12045 : : return NULL;
12046 : :
12047 : 281854 : if (TREE_CODE (member) == FIELD_DECL)
12048 : : /* FIELD_DECLS can have template info in some cases. We always
12049 : : want the FIELD_DECL though, as there's never a TEMPLATE_DECL
12050 : : wrapping them. */
12051 : : return member;
12052 : :
12053 : 122128 : int use_tpl = -1;
12054 : 122128 : if (tree ti = node_template_info (member, use_tpl))
12055 : : {
12056 : : // FIXME: Don't bail on things that CANNOT have their own
12057 : : // template header. No, make sure they're in the same cluster.
12058 : 0 : if (use_tpl > 0)
12059 : : return NULL_TREE;
12060 : :
12061 : 0 : if (DECL_TEMPLATE_RESULT (TI_TEMPLATE (ti)) == member)
12062 : 281854 : member = TI_TEMPLATE (ti);
12063 : : }
12064 : : return member;
12065 : : }
12066 : :
12067 : : void
12068 : 226818 : trees_out::write_class_def (tree defn)
12069 : : {
12070 : 226818 : gcc_assert (DECL_P (defn));
12071 : 226818 : if (streaming_p ())
12072 : 116198 : dump () && dump ("Writing class definition %N", defn);
12073 : :
12074 : 226818 : tree type = TREE_TYPE (defn);
12075 : 226818 : tree_node (TYPE_SIZE (type));
12076 : 226818 : tree_node (TYPE_SIZE_UNIT (type));
12077 : 226818 : tree_node (TYPE_VFIELD (type));
12078 : 226818 : tree_node (TYPE_BINFO (type));
12079 : :
12080 : 226818 : vec_chained_decls (TYPE_FIELDS (type));
12081 : :
12082 : : /* Every class but __as_base has a type-specific. */
12083 : 449900 : gcc_checking_assert (!TYPE_LANG_SPECIFIC (type) == IS_FAKE_BASE_TYPE (type));
12084 : :
12085 : 226818 : if (TYPE_LANG_SPECIFIC (type))
12086 : : {
12087 : 223082 : {
12088 : 223082 : vec<tree, va_gc> *v = CLASSTYPE_MEMBER_VEC (type);
12089 : 223082 : if (!v)
12090 : : {
12091 : 60527 : gcc_checking_assert (!streaming_p ());
12092 : : /* Force a class vector. */
12093 : 60527 : v = set_class_bindings (type, -1);
12094 : 60527 : gcc_checking_assert (v);
12095 : : }
12096 : :
12097 : 223082 : unsigned len = v->length ();
12098 : 223082 : if (streaming_p ())
12099 : 111538 : u (len);
12100 : 1726000 : for (unsigned ix = 0; ix != len; ix++)
12101 : : {
12102 : 1502918 : tree m = (*v)[ix];
12103 : 1502918 : if (TREE_CODE (m) == TYPE_DECL
12104 : 488926 : && DECL_ARTIFICIAL (m)
12105 : 1742232 : && TYPE_STUB_DECL (TREE_TYPE (m)) == m)
12106 : : /* This is a using-decl for a type, or an anonymous
12107 : : struct (maybe with a typedef name). Write the type. */
12108 : 15112 : m = TREE_TYPE (m);
12109 : 1502918 : tree_node (m);
12110 : : }
12111 : : }
12112 : 223082 : tree_node (CLASSTYPE_LAMBDA_EXPR (type));
12113 : :
12114 : : /* TYPE_CONTAINS_VPTR_P looks at the vbase vector, which the
12115 : : reader won't know at this point. */
12116 : 223082 : int has_vptr = TYPE_CONTAINS_VPTR_P (type);
12117 : :
12118 : 223082 : if (streaming_p ())
12119 : : {
12120 : 111538 : unsigned nvbases = vec_safe_length (CLASSTYPE_VBASECLASSES (type));
12121 : 111538 : u (nvbases);
12122 : 111538 : i (has_vptr);
12123 : : }
12124 : :
12125 : 223082 : if (has_vptr)
12126 : : {
12127 : 8476 : tree_vec (CLASSTYPE_PURE_VIRTUALS (type));
12128 : 8476 : tree_pair_vec (CLASSTYPE_VCALL_INDICES (type));
12129 : 8476 : tree_node (CLASSTYPE_KEY_METHOD (type));
12130 : : }
12131 : : }
12132 : :
12133 : 226818 : if (TYPE_LANG_SPECIFIC (type))
12134 : : {
12135 : 223082 : tree_node (CLASSTYPE_PRIMARY_BINFO (type));
12136 : :
12137 : 223082 : tree as_base = CLASSTYPE_AS_BASE (type);
12138 : 223082 : if (as_base)
12139 : 108892 : as_base = TYPE_NAME (as_base);
12140 : 223082 : tree_node (as_base);
12141 : :
12142 : : /* Write the vtables. */
12143 : 223082 : tree vtables = CLASSTYPE_VTABLES (type);
12144 : 223082 : vec_chained_decls (vtables);
12145 : 456248 : for (; vtables; vtables = TREE_CHAIN (vtables))
12146 : 10084 : write_definition (vtables);
12147 : :
12148 : : /* Write the friend classes. */
12149 : 223082 : tree_list (CLASSTYPE_FRIEND_CLASSES (type), false);
12150 : :
12151 : : /* Write the friend functions. */
12152 : 223082 : for (tree friends = DECL_FRIENDLIST (defn);
12153 : 245830 : friends; friends = TREE_CHAIN (friends))
12154 : : {
12155 : : /* Name of these friends. */
12156 : 22748 : tree_node (TREE_PURPOSE (friends));
12157 : 22748 : tree_list (TREE_VALUE (friends), false);
12158 : : }
12159 : : /* End of friend fns. */
12160 : 223082 : tree_node (NULL_TREE);
12161 : :
12162 : : /* Write the decl list. */
12163 : 223082 : tree_list (CLASSTYPE_DECL_LIST (type), true);
12164 : :
12165 : 223082 : if (TYPE_CONTAINS_VPTR_P (type))
12166 : : {
12167 : : /* Write the thunks. */
12168 : 8476 : for (tree decls = TYPE_FIELDS (type);
12169 : 247676 : decls; decls = DECL_CHAIN (decls))
12170 : 239200 : if (TREE_CODE (decls) == FUNCTION_DECL
12171 : 174312 : && DECL_VIRTUAL_P (decls)
12172 : 284646 : && DECL_THUNKS (decls))
12173 : : {
12174 : 1668 : tree_node (decls);
12175 : : /* Thunks are always unique, so chaining is ok. */
12176 : 1668 : chained_decls (DECL_THUNKS (decls));
12177 : : }
12178 : 8476 : tree_node (NULL_TREE);
12179 : : }
12180 : : }
12181 : 226818 : }
12182 : :
12183 : : void
12184 : 281854 : trees_out::mark_class_member (tree member, bool do_defn)
12185 : : {
12186 : 281854 : gcc_assert (DECL_P (member));
12187 : :
12188 : 281854 : member = member_owned_by_class (member);
12189 : 281854 : if (member)
12190 : 281854 : mark_declaration (member, do_defn && has_definition (member));
12191 : 281854 : }
12192 : :
12193 : : void
12194 : 226818 : trees_out::mark_class_def (tree defn)
12195 : : {
12196 : 226818 : gcc_assert (DECL_P (defn));
12197 : 226818 : tree type = TREE_TYPE (defn);
12198 : : /* Mark the class members that are not type-decls and cannot have
12199 : : independent definitions. */
12200 : 2343730 : for (tree member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
12201 : 2116912 : if (TREE_CODE (member) == FIELD_DECL
12202 : 2116912 : || TREE_CODE (member) == USING_DECL
12203 : : /* A cloned enum-decl from 'using enum unrelated;' */
12204 : 2116912 : || (TREE_CODE (member) == CONST_DECL
12205 : 10814 : && DECL_CONTEXT (member) == type))
12206 : : {
12207 : 281854 : mark_class_member (member);
12208 : 281854 : if (TREE_CODE (member) == FIELD_DECL)
12209 : 159726 : if (tree repr = DECL_BIT_FIELD_REPRESENTATIVE (member))
12210 : : /* If we're marking a class template definition, then
12211 : : this'll contain the width (as set by grokbitfield)
12212 : : instead of a decl. */
12213 : 2970 : if (DECL_P (repr))
12214 : 2550 : mark_declaration (repr, false);
12215 : : }
12216 : :
12217 : : /* Mark the binfo hierarchy. */
12218 : 548502 : for (tree child = TYPE_BINFO (type); child; child = TREE_CHAIN (child))
12219 : 321684 : mark_by_value (child);
12220 : :
12221 : 226818 : if (TYPE_LANG_SPECIFIC (type))
12222 : : {
12223 : 223082 : for (tree vtable = CLASSTYPE_VTABLES (type);
12224 : 233166 : vtable; vtable = TREE_CHAIN (vtable))
12225 : 10084 : mark_declaration (vtable, true);
12226 : :
12227 : 223082 : if (TYPE_CONTAINS_VPTR_P (type))
12228 : : /* Mark the thunks, they belong to the class definition,
12229 : : /not/ the thunked-to function. */
12230 : 8476 : for (tree decls = TYPE_FIELDS (type);
12231 : 247676 : decls; decls = DECL_CHAIN (decls))
12232 : 239200 : if (TREE_CODE (decls) == FUNCTION_DECL)
12233 : 174312 : for (tree thunks = DECL_THUNKS (decls);
12234 : 176528 : thunks; thunks = DECL_CHAIN (thunks))
12235 : 2216 : mark_declaration (thunks, false);
12236 : : }
12237 : 226818 : }
12238 : :
12239 : : /* Nop sorting, needed for resorting the member vec. */
12240 : :
12241 : : static void
12242 : 10795264 : nop (void *, void *, void *)
12243 : : {
12244 : 10795264 : }
12245 : :
12246 : : bool
12247 : 110229 : trees_in::read_class_def (tree defn, tree maybe_template)
12248 : : {
12249 : 110229 : gcc_assert (DECL_P (defn));
12250 : 110701 : dump () && dump ("Reading class definition %N", defn);
12251 : 110229 : tree type = TREE_TYPE (defn);
12252 : 110229 : tree size = tree_node ();
12253 : 110229 : tree size_unit = tree_node ();
12254 : 110229 : tree vfield = tree_node ();
12255 : 110229 : tree binfo = tree_node ();
12256 : 110229 : vec<tree, va_gc> *vbase_vec = NULL;
12257 : 110229 : vec<tree, va_gc> *member_vec = NULL;
12258 : 110229 : vec<tree, va_gc> *pure_virts = NULL;
12259 : 110229 : vec<tree_pair_s, va_gc> *vcall_indices = NULL;
12260 : 110229 : tree key_method = NULL_TREE;
12261 : 110229 : tree lambda = NULL_TREE;
12262 : :
12263 : : /* Read the fields. */
12264 : 110229 : vec<tree, va_heap> *fields = vec_chained_decls ();
12265 : :
12266 : 110229 : if (TYPE_LANG_SPECIFIC (type))
12267 : : {
12268 : 108118 : if (unsigned len = u ())
12269 : : {
12270 : 108118 : vec_alloc (member_vec, len);
12271 : 900896 : for (unsigned ix = 0; ix != len; ix++)
12272 : : {
12273 : 792778 : tree m = tree_node ();
12274 : 792778 : if (get_overrun ())
12275 : : break;
12276 : 792778 : if (TYPE_P (m))
12277 : 7382 : m = TYPE_STUB_DECL (m);
12278 : 792778 : member_vec->quick_push (m);
12279 : : }
12280 : : }
12281 : 108118 : lambda = tree_node ();
12282 : :
12283 : 108118 : if (!get_overrun ())
12284 : : {
12285 : 108118 : unsigned nvbases = u ();
12286 : 108118 : if (nvbases)
12287 : : {
12288 : 542 : vec_alloc (vbase_vec, nvbases);
12289 : 2588 : for (tree child = binfo; child; child = TREE_CHAIN (child))
12290 : 2046 : if (BINFO_VIRTUAL_P (child))
12291 : 542 : vbase_vec->quick_push (child);
12292 : : }
12293 : : }
12294 : :
12295 : 108118 : if (!get_overrun ())
12296 : : {
12297 : 108118 : int has_vptr = i ();
12298 : 108118 : if (has_vptr)
12299 : : {
12300 : 4551 : pure_virts = tree_vec ();
12301 : 4551 : vcall_indices = tree_pair_vec ();
12302 : 4551 : key_method = tree_node ();
12303 : : }
12304 : : }
12305 : : }
12306 : :
12307 : 110229 : tree maybe_dup = odr_duplicate (maybe_template, TYPE_SIZE (type));
12308 : 110229 : bool installing = maybe_dup && !TYPE_SIZE (type);
12309 : 45613 : if (installing)
12310 : : {
12311 : 45613 : if (maybe_dup != defn)
12312 : : {
12313 : : // FIXME: This is needed on other defns too, almost
12314 : : // duplicate-decl like? See is_matching_decl too.
12315 : : /* Copy flags from the duplicate. */
12316 : 371 : tree type_dup = TREE_TYPE (maybe_dup);
12317 : :
12318 : : /* Core pieces. */
12319 : 371 : TYPE_MODE_RAW (type) = TYPE_MODE_RAW (type_dup);
12320 : 371 : SET_DECL_MODE (defn, DECL_MODE (maybe_dup));
12321 : 371 : TREE_ADDRESSABLE (type) = TREE_ADDRESSABLE (type_dup);
12322 : 371 : DECL_SIZE (defn) = DECL_SIZE (maybe_dup);
12323 : 371 : DECL_SIZE_UNIT (defn) = DECL_SIZE_UNIT (maybe_dup);
12324 : 371 : DECL_ALIGN_RAW (defn) = DECL_ALIGN_RAW (maybe_dup);
12325 : 742 : DECL_WARN_IF_NOT_ALIGN_RAW (defn)
12326 : 371 : = DECL_WARN_IF_NOT_ALIGN_RAW (maybe_dup);
12327 : 371 : DECL_USER_ALIGN (defn) = DECL_USER_ALIGN (maybe_dup);
12328 : :
12329 : : /* C++ pieces. */
12330 : 371 : TYPE_POLYMORPHIC_P (type) = TYPE_POLYMORPHIC_P (type_dup);
12331 : 742 : TYPE_HAS_USER_CONSTRUCTOR (type)
12332 : 371 : = TYPE_HAS_USER_CONSTRUCTOR (type_dup);
12333 : 742 : TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
12334 : 371 : = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type_dup);
12335 : :
12336 : 371 : if (auto ls = TYPE_LANG_SPECIFIC (type_dup))
12337 : : {
12338 : 371 : if (TYPE_LANG_SPECIFIC (type))
12339 : : {
12340 : 1113 : CLASSTYPE_BEFRIENDING_CLASSES (type_dup)
12341 : 371 : = CLASSTYPE_BEFRIENDING_CLASSES (type);
12342 : 371 : if (!ANON_AGGR_TYPE_P (type))
12343 : 1113 : CLASSTYPE_TYPEINFO_VAR (type_dup)
12344 : 371 : = CLASSTYPE_TYPEINFO_VAR (type);
12345 : : }
12346 : 1946 : for (tree v = type; v; v = TYPE_NEXT_VARIANT (v))
12347 : 1575 : TYPE_LANG_SPECIFIC (v) = ls;
12348 : : }
12349 : : }
12350 : :
12351 : 45613 : TYPE_SIZE (type) = size;
12352 : 45613 : TYPE_SIZE_UNIT (type) = size_unit;
12353 : :
12354 : 45613 : if (fields)
12355 : : {
12356 : 45613 : tree *chain = &TYPE_FIELDS (type);
12357 : 45613 : unsigned len = fields->length ();
12358 : 493461 : for (unsigned ix = 0; ix != len; ix++)
12359 : : {
12360 : 447848 : tree decl = (*fields)[ix];
12361 : :
12362 : 447848 : if (!decl)
12363 : : {
12364 : : /* An anonymous struct with typedef name. */
12365 : 3 : tree tdef = (*fields)[ix+1];
12366 : 3 : decl = TYPE_STUB_DECL (TREE_TYPE (tdef));
12367 : 3 : gcc_checking_assert (IDENTIFIER_ANON_P (DECL_NAME (decl))
12368 : : && decl != tdef);
12369 : : }
12370 : :
12371 : 814475 : gcc_checking_assert (!*chain == !DECL_CLONED_FUNCTION_P (decl));
12372 : 447848 : *chain = decl;
12373 : 447848 : chain = &DECL_CHAIN (decl);
12374 : :
12375 : 447848 : if (TREE_CODE (decl) == FIELD_DECL
12376 : 447848 : && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
12377 : : {
12378 : 224 : tree anon_type = TYPE_MAIN_VARIANT (TREE_TYPE (decl));
12379 : 224 : if (DECL_NAME (defn) == as_base_identifier)
12380 : : /* ANON_AGGR_TYPE_FIELD should already point to the
12381 : : original FIELD_DECL; don't overwrite it to point
12382 : : to the as-base FIELD_DECL copy. */
12383 : 8 : gcc_checking_assert (ANON_AGGR_TYPE_FIELD (anon_type));
12384 : : else
12385 : 216 : ANON_AGGR_TYPE_FIELD (anon_type) = decl;
12386 : : }
12387 : :
12388 : 447848 : if (TREE_CODE (decl) == USING_DECL
12389 : 447848 : && TREE_CODE (USING_DECL_SCOPE (decl)) == RECORD_TYPE)
12390 : : {
12391 : : /* Reconstruct DECL_ACCESS. */
12392 : 21632 : tree decls = USING_DECL_DECLS (decl);
12393 : 21632 : tree access = declared_access (decl);
12394 : :
12395 : 24660 : for (ovl_iterator iter (decls); iter; ++iter)
12396 : : {
12397 : 1935 : tree d = *iter;
12398 : :
12399 : 1935 : retrofit_lang_decl (d);
12400 : 1935 : tree list = DECL_ACCESS (d);
12401 : :
12402 : 1935 : if (!purpose_member (type, list))
12403 : 2260 : DECL_ACCESS (d) = tree_cons (type, access, list);
12404 : : }
12405 : : }
12406 : : }
12407 : : }
12408 : :
12409 : 45613 : TYPE_VFIELD (type) = vfield;
12410 : 45613 : TYPE_BINFO (type) = binfo;
12411 : :
12412 : 45613 : if (TYPE_LANG_SPECIFIC (type))
12413 : : {
12414 : 44740 : CLASSTYPE_LAMBDA_EXPR (type) = lambda;
12415 : :
12416 : 44740 : CLASSTYPE_MEMBER_VEC (type) = member_vec;
12417 : 44740 : CLASSTYPE_PURE_VIRTUALS (type) = pure_virts;
12418 : 44740 : CLASSTYPE_VCALL_INDICES (type) = vcall_indices;
12419 : :
12420 : 44740 : CLASSTYPE_KEY_METHOD (type) = key_method;
12421 : :
12422 : 44740 : CLASSTYPE_VBASECLASSES (type) = vbase_vec;
12423 : :
12424 : : /* Resort the member vector. */
12425 : 44740 : resort_type_member_vec (member_vec, NULL, nop, NULL);
12426 : : }
12427 : : }
12428 : : else if (maybe_dup)
12429 : : {
12430 : : // FIXME:QOI Check matching defn
12431 : : }
12432 : :
12433 : 110229 : if (TYPE_LANG_SPECIFIC (type))
12434 : : {
12435 : 108118 : tree primary = tree_node ();
12436 : 108118 : tree as_base = tree_node ();
12437 : :
12438 : 108118 : if (as_base)
12439 : 53685 : as_base = TREE_TYPE (as_base);
12440 : :
12441 : : /* Read the vtables. */
12442 : 108118 : vec<tree, va_heap> *vtables = vec_chained_decls ();
12443 : 108118 : if (vtables)
12444 : : {
12445 : 4477 : unsigned len = vtables->length ();
12446 : 9943 : for (unsigned ix = 0; ix != len; ix++)
12447 : : {
12448 : 5466 : tree vtable = (*vtables)[ix];
12449 : 5466 : read_var_def (vtable, vtable);
12450 : : }
12451 : : }
12452 : :
12453 : 108118 : tree friend_classes = tree_list (false);
12454 : 108118 : tree friend_functions = NULL_TREE;
12455 : 108118 : for (tree *chain = &friend_functions;
12456 : 120592 : tree name = tree_node (); chain = &TREE_CHAIN (*chain))
12457 : : {
12458 : 12474 : tree val = tree_list (false);
12459 : 12474 : *chain = build_tree_list (name, val);
12460 : 12474 : }
12461 : 108118 : tree decl_list = tree_list (true);
12462 : :
12463 : 108118 : if (installing)
12464 : : {
12465 : 44740 : CLASSTYPE_PRIMARY_BINFO (type) = primary;
12466 : 44740 : CLASSTYPE_AS_BASE (type) = as_base;
12467 : :
12468 : 44740 : if (vtables)
12469 : : {
12470 : 1888 : if (!CLASSTYPE_KEY_METHOD (type)
12471 : : /* Sneaky user may have defined it inline
12472 : : out-of-class. */
12473 : 1888 : || DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (type)))
12474 : 1247 : vec_safe_push (keyed_classes, type);
12475 : 1888 : unsigned len = vtables->length ();
12476 : 1888 : tree *chain = &CLASSTYPE_VTABLES (type);
12477 : 4188 : for (unsigned ix = 0; ix != len; ix++)
12478 : : {
12479 : 2300 : tree vtable = (*vtables)[ix];
12480 : 2300 : gcc_checking_assert (!*chain);
12481 : 2300 : *chain = vtable;
12482 : 2300 : chain = &DECL_CHAIN (vtable);
12483 : : }
12484 : : }
12485 : 44740 : CLASSTYPE_FRIEND_CLASSES (type) = friend_classes;
12486 : 44740 : DECL_FRIENDLIST (defn) = friend_functions;
12487 : 44740 : CLASSTYPE_DECL_LIST (type) = decl_list;
12488 : :
12489 : 47021 : for (; friend_classes; friend_classes = TREE_CHAIN (friend_classes))
12490 : : {
12491 : 2281 : tree f = TREE_VALUE (friend_classes);
12492 : :
12493 : 2281 : if (CLASS_TYPE_P (f))
12494 : : {
12495 : 1403 : CLASSTYPE_BEFRIENDING_CLASSES (f)
12496 : 2806 : = tree_cons (NULL_TREE, type,
12497 : 1403 : CLASSTYPE_BEFRIENDING_CLASSES (f));
12498 : 2287 : dump () && dump ("Class %N befriending %C:%N",
12499 : 6 : type, TREE_CODE (f), f);
12500 : : }
12501 : : }
12502 : :
12503 : 49482 : for (; friend_functions;
12504 : 4742 : friend_functions = TREE_CHAIN (friend_functions))
12505 : 4742 : for (tree friend_decls = TREE_VALUE (friend_functions);
12506 : 10544 : friend_decls; friend_decls = TREE_CHAIN (friend_decls))
12507 : : {
12508 : 5802 : tree f = TREE_VALUE (friend_decls);
12509 : :
12510 : 5802 : DECL_BEFRIENDING_CLASSES (f)
12511 : 5802 : = tree_cons (NULL_TREE, type, DECL_BEFRIENDING_CLASSES (f));
12512 : 5829 : dump () && dump ("Class %N befriending %C:%N",
12513 : 27 : type, TREE_CODE (f), f);
12514 : : }
12515 : : }
12516 : :
12517 : 108118 : if (TYPE_CONTAINS_VPTR_P (type))
12518 : : /* Read and install the thunks. */
12519 : 5457 : while (tree vfunc = tree_node ())
12520 : : {
12521 : 906 : tree thunks = chained_decls ();
12522 : 906 : if (installing)
12523 : 378 : SET_DECL_THUNKS (vfunc, thunks);
12524 : : }
12525 : :
12526 : 108118 : vec_free (vtables);
12527 : : }
12528 : :
12529 : : /* Propagate to all variants. */
12530 : 110229 : if (installing)
12531 : 45613 : fixup_type_variants (type);
12532 : :
12533 : : /* IS_FAKE_BASE_TYPE is inaccurate at this point, because if this is
12534 : : the fake base, we've not hooked it into the containing class's
12535 : : data structure yet. Fortunately it has a unique name. */
12536 : 45613 : if (installing
12537 : 45613 : && DECL_NAME (defn) != as_base_identifier
12538 : 44740 : && (!CLASSTYPE_TEMPLATE_INFO (type)
12539 : 39234 : || !uses_template_parms (TI_ARGS (CLASSTYPE_TEMPLATE_INFO (type)))))
12540 : : /* Emit debug info. It'd be nice to know if the interface TU
12541 : : already emitted this. */
12542 : 24564 : rest_of_type_compilation (type, !LOCAL_CLASS_P (type));
12543 : :
12544 : 110229 : vec_free (fields);
12545 : :
12546 : 110229 : return !get_overrun ();
12547 : : }
12548 : :
12549 : : void
12550 : 11182 : trees_out::write_enum_def (tree decl)
12551 : : {
12552 : 11182 : tree type = TREE_TYPE (decl);
12553 : :
12554 : 11182 : tree_node (TYPE_VALUES (type));
12555 : : /* Note that we stream TYPE_MIN/MAX_VALUE directly as part of the
12556 : : ENUMERAL_TYPE. */
12557 : 11182 : }
12558 : :
12559 : : void
12560 : 11182 : trees_out::mark_enum_def (tree decl)
12561 : : {
12562 : 11182 : tree type = TREE_TYPE (decl);
12563 : :
12564 : 53672 : for (tree values = TYPE_VALUES (type); values; values = TREE_CHAIN (values))
12565 : : {
12566 : 42490 : tree cst = TREE_VALUE (values);
12567 : 42490 : mark_by_value (cst);
12568 : : /* We must mark the init to avoid circularity in tt_enum_int. */
12569 : 42490 : if (tree init = DECL_INITIAL (cst))
12570 : 42118 : if (TREE_CODE (init) == INTEGER_CST)
12571 : 41520 : mark_by_value (init);
12572 : : }
12573 : 11182 : }
12574 : :
12575 : : bool
12576 : 5035 : trees_in::read_enum_def (tree defn, tree maybe_template)
12577 : : {
12578 : 5035 : tree type = TREE_TYPE (defn);
12579 : 5035 : tree values = tree_node ();
12580 : :
12581 : 5035 : if (get_overrun ())
12582 : : return false;
12583 : :
12584 : 5035 : tree maybe_dup = odr_duplicate (maybe_template, TYPE_VALUES (type));
12585 : 10070 : bool installing = maybe_dup && !TYPE_VALUES (type);
12586 : :
12587 : 5035 : if (installing)
12588 : : {
12589 : 1958 : TYPE_VALUES (type) = values;
12590 : : /* Note that we stream TYPE_MIN/MAX_VALUE directly as part of the
12591 : : ENUMERAL_TYPE. */
12592 : :
12593 : 3269 : rest_of_type_compilation (type, DECL_NAMESPACE_SCOPE_P (defn));
12594 : : }
12595 : 3077 : else if (maybe_dup)
12596 : : {
12597 : 3077 : tree known = TYPE_VALUES (type);
12598 : 16673 : for (; known && values;
12599 : 13596 : known = TREE_CHAIN (known), values = TREE_CHAIN (values))
12600 : : {
12601 : 13605 : tree known_decl = TREE_VALUE (known);
12602 : 13605 : tree new_decl = TREE_VALUE (values);
12603 : :
12604 : 13605 : if (DECL_NAME (known_decl) != DECL_NAME (new_decl))
12605 : : break;
12606 : :
12607 : 13599 : new_decl = maybe_duplicate (new_decl);
12608 : :
12609 : 13599 : if (!cp_tree_equal (DECL_INITIAL (known_decl),
12610 : 13599 : DECL_INITIAL (new_decl)))
12611 : : break;
12612 : : }
12613 : :
12614 : 3077 : if (known || values)
12615 : : {
12616 : 12 : error_at (DECL_SOURCE_LOCATION (maybe_dup),
12617 : : "definition of %qD does not match", maybe_dup);
12618 : 12 : inform (DECL_SOURCE_LOCATION (defn),
12619 : : "existing definition %qD", defn);
12620 : :
12621 : 12 : tree known_decl = NULL_TREE, new_decl = NULL_TREE;
12622 : :
12623 : 12 : if (known)
12624 : 9 : known_decl = TREE_VALUE (known);
12625 : 12 : if (values)
12626 : 12 : new_decl = maybe_duplicate (TREE_VALUE (values));
12627 : :
12628 : 12 : if (known_decl && new_decl)
12629 : : {
12630 : 9 : inform (DECL_SOURCE_LOCATION (new_decl),
12631 : : "... this enumerator %qD", new_decl);
12632 : 9 : inform (DECL_SOURCE_LOCATION (known_decl),
12633 : : "enumerator %qD does not match ...", known_decl);
12634 : : }
12635 : 3 : else if (known_decl || new_decl)
12636 : : {
12637 : 3 : tree extra = known_decl ? known_decl : new_decl;
12638 : 3 : inform (DECL_SOURCE_LOCATION (extra),
12639 : : "additional enumerators beginning with %qD", extra);
12640 : : }
12641 : : else
12642 : 0 : inform (DECL_SOURCE_LOCATION (maybe_dup),
12643 : : "enumeration range differs");
12644 : :
12645 : : /* Mark it bad. */
12646 : 12 : unmatched_duplicate (maybe_template);
12647 : : }
12648 : : }
12649 : :
12650 : : return true;
12651 : : }
12652 : :
12653 : : /* Write out the body of DECL. See above circularity note. */
12654 : :
12655 : : void
12656 : 923411 : trees_out::write_definition (tree decl)
12657 : : {
12658 : 923411 : if (streaming_p ())
12659 : : {
12660 : 461701 : assert_definition (decl);
12661 : 461701 : dump ()
12662 : 11273 : && dump ("Writing definition %C:%N", TREE_CODE (decl), decl);
12663 : : }
12664 : : else
12665 : 461710 : dump (dumper::DEPEND)
12666 : 10814 : && dump ("Depending definition %C:%N", TREE_CODE (decl), decl);
12667 : :
12668 : 1470101 : again:
12669 : 1470101 : switch (TREE_CODE (decl))
12670 : : {
12671 : 0 : default:
12672 : 0 : gcc_unreachable ();
12673 : :
12674 : 546690 : case TEMPLATE_DECL:
12675 : 546690 : decl = DECL_TEMPLATE_RESULT (decl);
12676 : 546690 : goto again;
12677 : :
12678 : 567513 : case FUNCTION_DECL:
12679 : 567513 : write_function_def (decl);
12680 : 567513 : break;
12681 : :
12682 : 238000 : case TYPE_DECL:
12683 : 238000 : {
12684 : 238000 : tree type = TREE_TYPE (decl);
12685 : 238000 : gcc_assert (TYPE_MAIN_VARIANT (type) == type
12686 : : && TYPE_NAME (type) == decl);
12687 : 238000 : if (TREE_CODE (type) == ENUMERAL_TYPE)
12688 : 11182 : write_enum_def (decl);
12689 : : else
12690 : 226818 : write_class_def (decl);
12691 : : }
12692 : : break;
12693 : :
12694 : 117898 : case VAR_DECL:
12695 : 117898 : case CONCEPT_DECL:
12696 : 117898 : write_var_def (decl);
12697 : 117898 : break;
12698 : : }
12699 : 923411 : }
12700 : :
12701 : : /* Mark a declaration for by-value walking. If DO_DEFN is true, mark
12702 : : its body too. */
12703 : :
12704 : : void
12705 : 4071271 : trees_out::mark_declaration (tree decl, bool do_defn)
12706 : : {
12707 : 4071271 : mark_by_value (decl);
12708 : :
12709 : 4071271 : if (TREE_CODE (decl) == TEMPLATE_DECL)
12710 : 1364589 : decl = DECL_TEMPLATE_RESULT (decl);
12711 : :
12712 : 4071271 : if (!do_defn)
12713 : : return;
12714 : :
12715 : 923411 : switch (TREE_CODE (decl))
12716 : : {
12717 : 0 : default:
12718 : 0 : gcc_unreachable ();
12719 : :
12720 : : case FUNCTION_DECL:
12721 : : mark_function_def (decl);
12722 : : break;
12723 : :
12724 : 238000 : case TYPE_DECL:
12725 : 238000 : {
12726 : 238000 : tree type = TREE_TYPE (decl);
12727 : 238000 : gcc_assert (TYPE_MAIN_VARIANT (type) == type
12728 : : && TYPE_NAME (type) == decl);
12729 : 238000 : if (TREE_CODE (type) == ENUMERAL_TYPE)
12730 : 11182 : mark_enum_def (decl);
12731 : : else
12732 : 226818 : mark_class_def (decl);
12733 : : }
12734 : : break;
12735 : :
12736 : : case VAR_DECL:
12737 : : case CONCEPT_DECL:
12738 : : mark_var_def (decl);
12739 : : break;
12740 : : }
12741 : : }
12742 : :
12743 : : /* Read in the body of DECL. See above circularity note. */
12744 : :
12745 : : bool
12746 : 485043 : trees_in::read_definition (tree decl)
12747 : : {
12748 : 486019 : dump () && dump ("Reading definition %C %N", TREE_CODE (decl), decl);
12749 : :
12750 : : tree maybe_template = decl;
12751 : :
12752 : 485043 : again:
12753 : 776605 : switch (TREE_CODE (decl))
12754 : : {
12755 : : default:
12756 : : break;
12757 : :
12758 : 291562 : case TEMPLATE_DECL:
12759 : 291562 : decl = DECL_TEMPLATE_RESULT (decl);
12760 : 291562 : goto again;
12761 : :
12762 : 314228 : case FUNCTION_DECL:
12763 : 314228 : return read_function_def (decl, maybe_template);
12764 : :
12765 : 115264 : case TYPE_DECL:
12766 : 115264 : {
12767 : 115264 : tree type = TREE_TYPE (decl);
12768 : 115264 : gcc_assert (TYPE_MAIN_VARIANT (type) == type
12769 : : && TYPE_NAME (type) == decl);
12770 : 115264 : if (TREE_CODE (type) == ENUMERAL_TYPE)
12771 : 5035 : return read_enum_def (decl, maybe_template);
12772 : : else
12773 : 110229 : return read_class_def (decl, maybe_template);
12774 : : }
12775 : 55551 : break;
12776 : :
12777 : 55551 : case VAR_DECL:
12778 : 55551 : case CONCEPT_DECL:
12779 : 55551 : return read_var_def (decl, maybe_template);
12780 : : }
12781 : :
12782 : : return false;
12783 : : }
12784 : :
12785 : : /* Lookup an maybe insert a slot for depset for KEY. */
12786 : :
12787 : : depset **
12788 : 17832315 : depset::hash::entity_slot (tree entity, bool insert)
12789 : : {
12790 : 17832315 : traits::compare_type key (entity, NULL);
12791 : 26484193 : depset **slot = find_slot_with_hash (key, traits::hash (key),
12792 : : insert ? INSERT : NO_INSERT);
12793 : :
12794 : 17832315 : return slot;
12795 : : }
12796 : :
12797 : : depset **
12798 : 194401 : depset::hash::binding_slot (tree ctx, tree name, bool insert)
12799 : : {
12800 : 194401 : traits::compare_type key (ctx, name);
12801 : 194401 : depset **slot = find_slot_with_hash (key, traits::hash (key),
12802 : : insert ? INSERT : NO_INSERT);
12803 : :
12804 : 194401 : return slot;
12805 : : }
12806 : :
12807 : : depset *
12808 : 8191402 : depset::hash::find_dependency (tree decl)
12809 : : {
12810 : 8191402 : depset **slot = entity_slot (decl, false);
12811 : :
12812 : 8191402 : return slot ? *slot : NULL;
12813 : : }
12814 : :
12815 : : depset *
12816 : 0 : depset::hash::find_binding (tree ctx, tree name)
12817 : : {
12818 : 0 : depset **slot = binding_slot (ctx, name, false);
12819 : :
12820 : 0 : return slot ? *slot : NULL;
12821 : : }
12822 : :
12823 : : /* DECL is a newly discovered dependency. Create the depset, if it
12824 : : doesn't already exist. Add it to the worklist if so.
12825 : :
12826 : : DECL will be an OVL_USING_P OVERLOAD, if it's from a binding that's
12827 : : a using decl.
12828 : :
12829 : : We do not have to worry about adding the same dependency more than
12830 : : once. First it's harmless, but secondly the TREE_VISITED marking
12831 : : prevents us wanting to do it anyway. */
12832 : :
12833 : : depset *
12834 : 7922261 : depset::hash::make_dependency (tree decl, entity_kind ek)
12835 : : {
12836 : : /* Make sure we're being told consistent information. */
12837 : 14712712 : gcc_checking_assert ((ek == EK_NAMESPACE)
12838 : : == (TREE_CODE (decl) == NAMESPACE_DECL
12839 : : && !DECL_NAMESPACE_ALIAS (decl)));
12840 : 7922261 : gcc_checking_assert (ek != EK_BINDING && ek != EK_REDIRECT);
12841 : 7922261 : gcc_checking_assert (TREE_CODE (decl) != FIELD_DECL
12842 : : && (TREE_CODE (decl) != USING_DECL
12843 : : || TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL));
12844 : 7922261 : gcc_checking_assert (!is_key_order ());
12845 : 7922261 : if (ek == EK_USING)
12846 : 26887 : gcc_checking_assert (TREE_CODE (decl) == OVERLOAD);
12847 : :
12848 : 7922261 : if (TREE_CODE (decl) == TEMPLATE_DECL)
12849 : : /* The template should have copied these from its result decl. */
12850 : 2909660 : gcc_checking_assert (DECL_MODULE_EXPORT_P (decl)
12851 : : == DECL_MODULE_EXPORT_P (DECL_TEMPLATE_RESULT (decl)));
12852 : :
12853 : 7922261 : depset **slot = entity_slot (decl, true);
12854 : 7922261 : depset *dep = *slot;
12855 : 7922261 : bool for_binding = ek == EK_FOR_BINDING;
12856 : :
12857 : 7922261 : if (!dep)
12858 : : {
12859 : 578546 : if ((DECL_IMPLICIT_TYPEDEF_P (decl)
12860 : : /* ... not an enum, for instance. */
12861 : 296766 : && RECORD_OR_UNION_TYPE_P (TREE_TYPE (decl))
12862 : 291105 : && TYPE_LANG_SPECIFIC (TREE_TYPE (decl))
12863 : 259736 : && CLASSTYPE_USE_TEMPLATE (TREE_TYPE (decl)) == 2)
12864 : 2076439 : || (VAR_P (decl)
12865 : 56874 : && DECL_LANG_SPECIFIC (decl)
12866 : 56847 : && DECL_USE_TEMPLATE (decl) == 2))
12867 : : {
12868 : : /* A partial or explicit specialization. Partial
12869 : : specializations might not be in the hash table, because
12870 : : there can be multiple differently-constrained variants.
12871 : :
12872 : : template<typename T> class silly;
12873 : : template<typename T> requires true class silly {};
12874 : :
12875 : : We need to find them, insert their TEMPLATE_DECL in the
12876 : : dep_hash, and then convert the dep we just found into a
12877 : : redirect. */
12878 : :
12879 : 47716 : tree ti = get_template_info (decl);
12880 : 47716 : tree tmpl = TI_TEMPLATE (ti);
12881 : 47716 : tree partial = NULL_TREE;
12882 : 47716 : for (tree spec = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
12883 : 174528 : spec; spec = TREE_CHAIN (spec))
12884 : 155290 : if (DECL_TEMPLATE_RESULT (TREE_VALUE (spec)) == decl)
12885 : : {
12886 : : partial = TREE_VALUE (spec);
12887 : : break;
12888 : : }
12889 : :
12890 : 47716 : if (partial)
12891 : : {
12892 : : /* Eagerly create an empty redirect. The following
12893 : : make_dependency call could cause hash reallocation,
12894 : : and invalidate slot's value. */
12895 : 28478 : depset *redirect = make_entity (decl, EK_REDIRECT);
12896 : :
12897 : : /* Redirects are never reached -- always snap to their target. */
12898 : 28478 : redirect->set_flag_bit<DB_UNREACHED_BIT> ();
12899 : :
12900 : 28478 : *slot = redirect;
12901 : :
12902 : 28478 : depset *tmpl_dep = make_dependency (partial, EK_PARTIAL);
12903 : 28478 : gcc_checking_assert (tmpl_dep->get_entity_kind () == EK_PARTIAL);
12904 : :
12905 : 28478 : redirect->deps.safe_push (tmpl_dep);
12906 : :
12907 : 28478 : return redirect;
12908 : : }
12909 : : }
12910 : :
12911 : 1512368 : bool has_def = ek != EK_USING && has_definition (decl);
12912 : 1485481 : if (ek > EK_BINDING)
12913 : 216430 : ek = EK_DECL;
12914 : :
12915 : : /* The only OVERLOADS we should see are USING decls from
12916 : : bindings. */
12917 : 1512368 : *slot = dep = make_entity (decl, ek, has_def);
12918 : :
12919 : 1512368 : if (CHECKING_P && TREE_CODE (decl) == TEMPLATE_DECL)
12920 : : /* The template_result should otherwise not be in the
12921 : : table, or be an empty redirect (created above). */
12922 : 460476 : if (auto *eslot = entity_slot (DECL_TEMPLATE_RESULT (decl), false))
12923 : 28478 : gcc_checking_assert ((*eslot)->get_entity_kind () == EK_REDIRECT
12924 : : && !(*eslot)->deps.length ());
12925 : :
12926 : 1512368 : if (ek != EK_USING)
12927 : : {
12928 : 1485481 : tree not_tmpl = STRIP_TEMPLATE (decl);
12929 : :
12930 : 1485481 : if (DECL_LANG_SPECIFIC (not_tmpl)
12931 : 2924470 : && DECL_MODULE_IMPORT_P (not_tmpl))
12932 : : {
12933 : : /* Store the module number and index in cluster/section,
12934 : : so we don't have to look them up again. */
12935 : 3924 : unsigned index = import_entity_index (decl);
12936 : 3924 : module_state *from = import_entity_module (index);
12937 : : /* Remap will be zero for imports from partitions, which
12938 : : we want to treat as-if declared in this TU. */
12939 : 3924 : if (from->remap)
12940 : : {
12941 : 3777 : dep->cluster = index - from->entity_lwm;
12942 : 3777 : dep->section = from->remap;
12943 : 3777 : dep->set_flag_bit<DB_IMPORTED_BIT> ();
12944 : : }
12945 : : }
12946 : :
12947 : 1485481 : if (ek == EK_DECL
12948 : 711613 : && !dep->is_import ()
12949 : 711267 : && TREE_CODE (CP_DECL_CONTEXT (decl)) == NAMESPACE_DECL
12950 : 1730346 : && !(TREE_CODE (decl) == TEMPLATE_DECL
12951 : 115357 : && DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (decl)))
12952 : : {
12953 : 238898 : tree ctx = CP_DECL_CONTEXT (decl);
12954 : :
12955 : 238898 : if (!TREE_PUBLIC (ctx))
12956 : : /* Member of internal namespace. */
12957 : 6 : dep->set_flag_bit<DB_IS_INTERNAL_BIT> ();
12958 : 238892 : else if (VAR_OR_FUNCTION_DECL_P (not_tmpl)
12959 : 238892 : && DECL_THIS_STATIC (not_tmpl))
12960 : : {
12961 : : /* An internal decl. This is ok in a GM entity. */
12962 : 1693 : if (!(header_module_p ()
12963 : 5 : || !DECL_LANG_SPECIFIC (not_tmpl)
12964 : 5 : || !DECL_MODULE_PURVIEW_P (not_tmpl)))
12965 : 3 : dep->set_flag_bit<DB_IS_INTERNAL_BIT> ();
12966 : : }
12967 : : }
12968 : : }
12969 : :
12970 : 1512368 : if (!dep->is_import ())
12971 : 1508591 : worklist.safe_push (dep);
12972 : : }
12973 : :
12974 : 7893783 : dump (dumper::DEPEND)
12975 : 173525 : && dump ("%s on %s %C:%N found",
12976 : : ek == EK_REDIRECT ? "Redirect"
12977 : 173525 : : for_binding ? "Binding" : "Dependency",
12978 : 173525 : dep->entity_kind_name (), TREE_CODE (decl), decl);
12979 : :
12980 : 7893783 : return dep;
12981 : : }
12982 : :
12983 : : /* DEP is a newly discovered dependency. Append it to current's
12984 : : depset. */
12985 : :
12986 : : void
12987 : 6022096 : depset::hash::add_dependency (depset *dep)
12988 : : {
12989 : 6022096 : gcc_checking_assert (current && !is_key_order ());
12990 : 6022096 : current->deps.safe_push (dep);
12991 : :
12992 : 6022096 : if (dep->is_internal () && !current->is_internal ())
12993 : 15 : current->set_flag_bit<DB_REFS_INTERNAL_BIT> ();
12994 : :
12995 : 6022096 : if (current->get_entity_kind () == EK_USING
12996 : 26887 : && DECL_IMPLICIT_TYPEDEF_P (dep->get_entity ())
12997 : 6032296 : && TREE_CODE (TREE_TYPE (dep->get_entity ())) == ENUMERAL_TYPE)
12998 : : {
12999 : : /* CURRENT is an unwrapped using-decl and DECL is an enum's
13000 : : implicit typedef. Is CURRENT a member of the enum? */
13001 : 10013 : tree c_decl = OVL_FUNCTION (current->get_entity ());
13002 : :
13003 : 10013 : if (TREE_CODE (c_decl) == CONST_DECL
13004 : 20005 : && (current->deps[0]->get_entity ()
13005 : 9992 : == CP_DECL_CONTEXT (dep->get_entity ())))
13006 : : /* Make DECL depend on CURRENT. */
13007 : 9926 : dep->deps.safe_push (current);
13008 : : }
13009 : :
13010 : 6022096 : if (dep->is_unreached ())
13011 : : {
13012 : : /* The dependency is reachable now. */
13013 : 465530 : reached_unreached = true;
13014 : 465530 : dep->clear_flag_bit<DB_UNREACHED_BIT> ();
13015 : 465530 : dump (dumper::DEPEND)
13016 : 10948 : && dump ("Reaching unreached %s %C:%N", dep->entity_kind_name (),
13017 : 10948 : TREE_CODE (dep->get_entity ()), dep->get_entity ());
13018 : : }
13019 : 6022096 : }
13020 : :
13021 : : depset *
13022 : 9066179 : depset::hash::add_dependency (tree decl, entity_kind ek)
13023 : : {
13024 : 9066179 : depset *dep;
13025 : :
13026 : 9066179 : if (is_key_order ())
13027 : : {
13028 : 2972276 : dep = find_dependency (decl);
13029 : 2972276 : if (dep)
13030 : : {
13031 : 1445552 : current->deps.safe_push (dep);
13032 : 1445552 : dump (dumper::MERGE)
13033 : 456 : && dump ("Key dependency on %s %C:%N found",
13034 : 456 : dep->entity_kind_name (), TREE_CODE (decl), decl);
13035 : : }
13036 : : else
13037 : : {
13038 : : /* It's not a mergeable decl, look for it in the original
13039 : : table. */
13040 : 1526724 : dep = chain->find_dependency (decl);
13041 : 1526724 : gcc_checking_assert (dep);
13042 : : }
13043 : : }
13044 : : else
13045 : : {
13046 : 6093903 : dep = make_dependency (decl, ek);
13047 : 6093903 : if (dep->get_entity_kind () != EK_REDIRECT)
13048 : 6022096 : add_dependency (dep);
13049 : : }
13050 : :
13051 : 9066179 : return dep;
13052 : : }
13053 : :
13054 : : void
13055 : 743741 : depset::hash::add_namespace_context (depset *dep, tree ns)
13056 : : {
13057 : 743741 : depset *ns_dep = make_dependency (ns, depset::EK_NAMESPACE);
13058 : 743741 : dep->deps.safe_push (ns_dep);
13059 : :
13060 : : /* Mark it as special if imported so we don't walk connect when
13061 : : SCCing. */
13062 : 743741 : if (!dep->is_binding () && ns_dep->is_import ())
13063 : 0 : dep->set_special ();
13064 : 743741 : }
13065 : :
13066 : : struct add_binding_data
13067 : : {
13068 : : tree ns;
13069 : : bitmap partitions;
13070 : : depset *binding;
13071 : : depset::hash *hash;
13072 : : bool met_namespace;
13073 : : };
13074 : :
13075 : : /* Return true if we are, or contain something that is exported. */
13076 : :
13077 : : bool
13078 : 3681775 : depset::hash::add_binding_entity (tree decl, WMB_Flags flags, void *data_)
13079 : : {
13080 : 3681775 : auto data = static_cast <add_binding_data *> (data_);
13081 : :
13082 : 3681775 : if (!(TREE_CODE (decl) == NAMESPACE_DECL && !DECL_NAMESPACE_ALIAS (decl)))
13083 : : {
13084 : 3675663 : tree inner = decl;
13085 : :
13086 : 3675663 : if (TREE_CODE (inner) == CONST_DECL
13087 : 3675663 : && TREE_CODE (DECL_CONTEXT (inner)) == ENUMERAL_TYPE)
13088 : 10814 : inner = TYPE_NAME (DECL_CONTEXT (inner));
13089 : 3664849 : else if (TREE_CODE (inner) == TEMPLATE_DECL)
13090 : 126872 : inner = DECL_TEMPLATE_RESULT (inner);
13091 : :
13092 : 7233122 : if ((!DECL_LANG_SPECIFIC (inner) || !DECL_MODULE_PURVIEW_P (inner))
13093 : 6989785 : && !((flags & WMB_Using) && (flags & WMB_Export)))
13094 : : /* Ignore global module fragment entities unless explicitly
13095 : : exported with a using declaration. */
13096 : : return false;
13097 : :
13098 : 243352 : if (VAR_OR_FUNCTION_DECL_P (inner)
13099 : 243352 : && DECL_THIS_STATIC (inner))
13100 : : {
13101 : 1718 : if (!header_module_p ())
13102 : : /* Ignore internal-linkage entitites. */
13103 : : return false;
13104 : : }
13105 : :
13106 : 243322 : if ((TREE_CODE (decl) == VAR_DECL
13107 : 243322 : || TREE_CODE (decl) == TYPE_DECL)
13108 : 243322 : && DECL_TINFO_P (decl))
13109 : : /* Ignore TINFO things. */
13110 : : return false;
13111 : :
13112 : 243322 : if (TREE_CODE (decl) == VAR_DECL && DECL_NTTP_OBJECT_P (decl))
13113 : : /* Ignore NTTP objects. */
13114 : : return false;
13115 : :
13116 : 243322 : if (!(flags & WMB_Using) && CP_DECL_CONTEXT (decl) != data->ns)
13117 : : {
13118 : : /* A using that lost its wrapper or an unscoped enum
13119 : : constant. */
13120 : 11971 : flags = WMB_Flags (flags | WMB_Using);
13121 : 11971 : if (DECL_MODULE_EXPORT_P (TREE_CODE (decl) == CONST_DECL
13122 : : ? TYPE_NAME (TREE_TYPE (decl))
13123 : : : STRIP_TEMPLATE (decl)))
13124 : 11968 : flags = WMB_Flags (flags | WMB_Export);
13125 : : }
13126 : :
13127 : 243322 : if (!data->binding)
13128 : : /* No binding to check. */;
13129 : 48921 : else if (flags & WMB_Using)
13130 : : {
13131 : : /* Look in the binding to see if we already have this
13132 : : using. */
13133 : 25616 : for (unsigned ix = data->binding->deps.length (); --ix;)
13134 : : {
13135 : 18700 : depset *d = data->binding->deps[ix];
13136 : 37400 : if (d->get_entity_kind () == EK_USING
13137 : 18700 : && OVL_FUNCTION (d->get_entity ()) == decl)
13138 : : {
13139 : 0 : if (!(flags & WMB_Hidden))
13140 : 0 : d->clear_hidden_binding ();
13141 : 0 : if (flags & WMB_Export)
13142 : 0 : OVL_EXPORT_P (d->get_entity ()) = true;
13143 : 0 : return bool (flags & WMB_Export);
13144 : : }
13145 : : }
13146 : : }
13147 : 45463 : else if (flags & WMB_Dups)
13148 : : {
13149 : : /* Look in the binding to see if we already have this decl. */
13150 : 18 : for (unsigned ix = data->binding->deps.length (); --ix;)
13151 : : {
13152 : 9 : depset *d = data->binding->deps[ix];
13153 : 9 : if (d->get_entity () == decl)
13154 : : {
13155 : 9 : if (!(flags & WMB_Hidden))
13156 : 9 : d->clear_hidden_binding ();
13157 : 9 : return false;
13158 : : }
13159 : : }
13160 : : }
13161 : :
13162 : : /* We're adding something. */
13163 : 243313 : if (!data->binding)
13164 : : {
13165 : 194401 : data->binding = make_binding (data->ns, DECL_NAME (decl));
13166 : 194401 : data->hash->add_namespace_context (data->binding, data->ns);
13167 : :
13168 : 194401 : depset **slot = data->hash->binding_slot (data->ns,
13169 : 194401 : DECL_NAME (decl), true);
13170 : 194401 : gcc_checking_assert (!*slot);
13171 : 194401 : *slot = data->binding;
13172 : : }
13173 : :
13174 : : /* Make sure nobody left a tree visited lying about. */
13175 : 243313 : gcc_checking_assert (!TREE_VISITED (decl));
13176 : :
13177 : 243313 : if (flags & WMB_Using)
13178 : : {
13179 : 26887 : decl = ovl_make (decl, NULL_TREE);
13180 : 26887 : if (flags & WMB_Export)
13181 : 26878 : OVL_EXPORT_P (decl) = true;
13182 : : }
13183 : :
13184 : 243313 : depset *dep = data->hash->make_dependency
13185 : 459739 : (decl, flags & WMB_Using ? EK_USING : EK_FOR_BINDING);
13186 : 243313 : if (flags & WMB_Hidden)
13187 : 5403 : dep->set_hidden_binding ();
13188 : 243313 : data->binding->deps.safe_push (dep);
13189 : : /* Binding and contents are mutually dependent. */
13190 : 243313 : dep->deps.safe_push (data->binding);
13191 : :
13192 : 243313 : return (flags & WMB_Using
13193 : 243313 : ? flags & WMB_Export : DECL_MODULE_EXPORT_P (decl));
13194 : : }
13195 : 6112 : else if (DECL_NAME (decl) && !data->met_namespace)
13196 : : {
13197 : : /* Namespace, walk exactly once. */
13198 : 6085 : gcc_checking_assert (TREE_PUBLIC (decl));
13199 : 6085 : data->met_namespace = true;
13200 : 6085 : if (data->hash->add_namespace_entities (decl, data->partitions))
13201 : : {
13202 : : /* It contains an exported thing, so it is exported. */
13203 : 1808 : gcc_checking_assert (DECL_MODULE_PURVIEW_P (decl));
13204 : 1808 : DECL_MODULE_EXPORT_P (decl) = true;
13205 : : }
13206 : :
13207 : 6085 : if (DECL_MODULE_PURVIEW_P (decl))
13208 : : {
13209 : 2014 : data->hash->make_dependency (decl, depset::EK_NAMESPACE);
13210 : :
13211 : 2014 : return DECL_MODULE_EXPORT_P (decl);
13212 : : }
13213 : : }
13214 : :
13215 : : return false;
13216 : : }
13217 : :
13218 : : /* Recursively find all the namespace bindings of NS. Add a depset
13219 : : for every binding that contains an export or module-linkage entity.
13220 : : Add a defining depset for every such decl that we need to write a
13221 : : definition. Such defining depsets depend on the binding depset.
13222 : : Returns true if we contain something exported. */
13223 : :
13224 : : bool
13225 : 7909 : depset::hash::add_namespace_entities (tree ns, bitmap partitions)
13226 : : {
13227 : 8724 : dump () && dump ("Looking for writables in %N", ns);
13228 : 7909 : dump.indent ();
13229 : :
13230 : 7909 : unsigned count = 0;
13231 : 7909 : add_binding_data data;
13232 : 7909 : data.ns = ns;
13233 : 7909 : data.partitions = partitions;
13234 : 7909 : data.hash = this;
13235 : :
13236 : 7909 : hash_table<named_decl_hash>::iterator end
13237 : 7909 : (DECL_NAMESPACE_BINDINGS (ns)->end ());
13238 : 4818073 : for (hash_table<named_decl_hash>::iterator iter
13239 : 9636146 : (DECL_NAMESPACE_BINDINGS (ns)->begin ()); iter != end; ++iter)
13240 : : {
13241 : 4810164 : data.binding = nullptr;
13242 : 4810164 : data.met_namespace = false;
13243 : 4810164 : if (walk_module_binding (*iter, partitions, add_binding_entity, &data))
13244 : 195742 : count++;
13245 : : }
13246 : :
13247 : 7909 : if (count)
13248 : 3405 : dump () && dump ("Found %u entries", count);
13249 : 7909 : dump.outdent ();
13250 : :
13251 : 7909 : return count != 0;
13252 : : }
13253 : :
13254 : : void
13255 : 147 : depset::hash::add_partial_entities (vec<tree, va_gc> *partial_classes)
13256 : : {
13257 : 41874 : for (unsigned ix = 0; ix != partial_classes->length (); ix++)
13258 : : {
13259 : 41727 : tree inner = (*partial_classes)[ix];
13260 : :
13261 : 41727 : depset *dep = make_dependency (inner, depset::EK_DECL);
13262 : :
13263 : 41727 : if (dep->get_entity_kind () == depset::EK_REDIRECT)
13264 : : /* We should have recorded the template as a partial
13265 : : specialization. */
13266 : 25330 : gcc_checking_assert (dep->deps[0]->get_entity_kind ()
13267 : : == depset::EK_PARTIAL);
13268 : : else
13269 : : /* It was an explicit specialization, not a partial one. */
13270 : 16397 : gcc_checking_assert (dep->get_entity_kind ()
13271 : : == depset::EK_SPECIALIZATION);
13272 : : }
13273 : 147 : }
13274 : :
13275 : : /* Add the members of imported classes that we defined in this TU.
13276 : : This will also include lazily created implicit member function
13277 : : declarations. (All others will be definitions.) */
13278 : :
13279 : : void
13280 : 12 : depset::hash::add_class_entities (vec<tree, va_gc> *class_members)
13281 : : {
13282 : 24 : for (unsigned ix = 0; ix != class_members->length (); ix++)
13283 : : {
13284 : 12 : tree defn = (*class_members)[ix];
13285 : 12 : depset *dep = make_dependency (defn, EK_INNER_DECL);
13286 : :
13287 : 12 : if (dep->get_entity_kind () == EK_REDIRECT)
13288 : 0 : dep = dep->deps[0];
13289 : :
13290 : : /* Only non-instantiations need marking as members. */
13291 : 24 : if (dep->get_entity_kind () == EK_DECL)
13292 : 12 : dep->set_flag_bit <DB_IS_MEMBER_BIT> ();
13293 : : }
13294 : 12 : }
13295 : :
13296 : : /* We add the partial & explicit specializations, and the explicit
13297 : : instantiations. */
13298 : :
13299 : : static void
13300 : 771806 : specialization_add (bool decl_p, spec_entry *entry, void *data_)
13301 : : {
13302 : 771806 : vec<spec_entry *> *data = reinterpret_cast <vec<spec_entry *> *> (data_);
13303 : :
13304 : 771806 : if (!decl_p)
13305 : : {
13306 : : /* We exclusively use decls to locate things. Make sure there's
13307 : : no mismatch between the two specialization tables we keep.
13308 : : pt.cc optimizes instantiation lookup using a complicated
13309 : : heuristic. We don't attempt to replicate that algorithm, but
13310 : : observe its behaviour and reproduce it upon read back. */
13311 : :
13312 : 247799 : gcc_checking_assert (TREE_CODE (entry->spec) == ENUMERAL_TYPE
13313 : : || DECL_CLASS_TEMPLATE_P (entry->tmpl));
13314 : :
13315 : 247799 : gcc_checking_assert (!match_mergeable_specialization (true, entry));
13316 : : }
13317 : 524007 : else if (VAR_OR_FUNCTION_DECL_P (entry->spec))
13318 : 259878 : gcc_checking_assert (!DECL_LOCAL_DECL_P (entry->spec));
13319 : :
13320 : 771806 : data->safe_push (entry);
13321 : 771806 : }
13322 : :
13323 : : /* Arbitrary stable comparison. */
13324 : :
13325 : : static int
13326 : 45415590 : specialization_cmp (const void *a_, const void *b_)
13327 : : {
13328 : 45415590 : const spec_entry *ea = *reinterpret_cast<const spec_entry *const *> (a_);
13329 : 45415590 : const spec_entry *eb = *reinterpret_cast<const spec_entry *const *> (b_);
13330 : :
13331 : 45415590 : if (ea == eb)
13332 : : return 0;
13333 : :
13334 : 45415590 : tree a = ea->spec;
13335 : 45415590 : tree b = eb->spec;
13336 : 45415590 : if (TYPE_P (a))
13337 : : {
13338 : 13912658 : a = TYPE_NAME (a);
13339 : 13912658 : b = TYPE_NAME (b);
13340 : : }
13341 : :
13342 : 45415590 : if (a == b)
13343 : : /* This can happen with friend specializations. Just order by
13344 : : entry address. See note in depset_cmp. */
13345 : 229 : return ea < eb ? -1 : +1;
13346 : :
13347 : 45415426 : return DECL_UID (a) < DECL_UID (b) ? -1 : +1;
13348 : : }
13349 : :
13350 : : /* We add all kinds of specialializations. Implicit specializations
13351 : : should only streamed and walked if they are reachable from
13352 : : elsewhere. Hence the UNREACHED flag. This is making the
13353 : : assumption that it is cheaper to reinstantiate them on demand
13354 : : elsewhere, rather than stream them in when we instantiate their
13355 : : general template. Also, if we do stream them, we can only do that
13356 : : if they are not internal (which they can become if they themselves
13357 : : touch an internal entity?). */
13358 : :
13359 : : void
13360 : 3648 : depset::hash::add_specializations (bool decl_p)
13361 : : {
13362 : 3648 : vec<spec_entry *> data;
13363 : 3648 : data.create (100);
13364 : 3648 : walk_specializations (decl_p, specialization_add, &data);
13365 : 3648 : data.qsort (specialization_cmp);
13366 : 775454 : while (data.length ())
13367 : : {
13368 : 771806 : spec_entry *entry = data.pop ();
13369 : 771806 : tree spec = entry->spec;
13370 : 771806 : int use_tpl = 0;
13371 : 771806 : bool is_friend = false;
13372 : :
13373 : 771806 : if (decl_p && DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (entry->tmpl))
13374 : : /* A friend of a template. This is keyed to the
13375 : : instantiation. */
13376 : : is_friend = true;
13377 : :
13378 : 771806 : if (decl_p)
13379 : : {
13380 : 524007 : if (tree ti = DECL_TEMPLATE_INFO (spec))
13381 : : {
13382 : 524007 : tree tmpl = TI_TEMPLATE (ti);
13383 : :
13384 : 524007 : use_tpl = DECL_USE_TEMPLATE (spec);
13385 : 524007 : if (spec == DECL_TEMPLATE_RESULT (tmpl))
13386 : : {
13387 : 4559 : spec = tmpl;
13388 : 4559 : gcc_checking_assert (DECL_USE_TEMPLATE (spec) == use_tpl);
13389 : : }
13390 : 519448 : else if (is_friend)
13391 : : {
13392 : 2733 : if (TI_TEMPLATE (ti) != entry->tmpl
13393 : 2733 : || !template_args_equal (TI_ARGS (ti), entry->tmpl))
13394 : 2733 : goto template_friend;
13395 : : }
13396 : : }
13397 : : else
13398 : : {
13399 : 0 : template_friend:;
13400 : 2733 : gcc_checking_assert (is_friend);
13401 : : /* This is a friend of a template class, but not the one
13402 : : that generated entry->spec itself (i.e. it's an
13403 : : equivalent clone). We do not need to record
13404 : : this. */
13405 : 2733 : continue;
13406 : : }
13407 : : }
13408 : : else
13409 : : {
13410 : 247799 : if (TREE_CODE (spec) == ENUMERAL_TYPE)
13411 : : {
13412 : 1451 : tree ctx = DECL_CONTEXT (TYPE_NAME (spec));
13413 : :
13414 : 1451 : if (TYPE_P (ctx))
13415 : 1445 : use_tpl = CLASSTYPE_USE_TEMPLATE (ctx);
13416 : : else
13417 : 6 : use_tpl = DECL_USE_TEMPLATE (ctx);
13418 : : }
13419 : : else
13420 : 246348 : use_tpl = CLASSTYPE_USE_TEMPLATE (spec);
13421 : :
13422 : 247799 : tree ti = TYPE_TEMPLATE_INFO (spec);
13423 : 247799 : tree tmpl = TI_TEMPLATE (ti);
13424 : :
13425 : 247799 : spec = TYPE_NAME (spec);
13426 : 247799 : if (spec == DECL_TEMPLATE_RESULT (tmpl))
13427 : : {
13428 : 1000 : spec = tmpl;
13429 : 1000 : use_tpl = DECL_USE_TEMPLATE (spec);
13430 : : }
13431 : : }
13432 : :
13433 : 769073 : bool needs_reaching = false;
13434 : 769073 : if (use_tpl == 1)
13435 : : /* Implicit instantiations only walked if we reach them. */
13436 : : needs_reaching = true;
13437 : 92259 : else if (!DECL_LANG_SPECIFIC (STRIP_TEMPLATE (spec))
13438 : 174686 : || !DECL_MODULE_PURVIEW_P (STRIP_TEMPLATE (spec)))
13439 : : /* Likewise, GMF explicit or partial specializations. */
13440 : : needs_reaching = true;
13441 : :
13442 : : #if false && CHECKING_P
13443 : : /* The instantiation isn't always on
13444 : : DECL_TEMPLATE_INSTANTIATIONS, */
13445 : : // FIXME: we probably need to remember this information?
13446 : : /* Verify the specialization is on the
13447 : : DECL_TEMPLATE_INSTANTIATIONS of the template. */
13448 : : for (tree cons = DECL_TEMPLATE_INSTANTIATIONS (entry->tmpl);
13449 : : cons; cons = TREE_CHAIN (cons))
13450 : : if (TREE_VALUE (cons) == entry->spec)
13451 : : {
13452 : : gcc_assert (entry->args == TREE_PURPOSE (cons));
13453 : : goto have_spec;
13454 : : }
13455 : : gcc_unreachable ();
13456 : : have_spec:;
13457 : : #endif
13458 : :
13459 : : /* Make sure nobody left a tree visited lying about. */
13460 : 769073 : gcc_checking_assert (!TREE_VISITED (spec));
13461 : 769073 : depset *dep = make_dependency (spec, depset::EK_SPECIALIZATION);
13462 : 769073 : if (dep->is_special ())
13463 : 0 : gcc_unreachable ();
13464 : : else
13465 : : {
13466 : 769073 : if (dep->get_entity_kind () == depset::EK_REDIRECT)
13467 : 27700 : dep = dep->deps[0];
13468 : 741373 : else if (dep->get_entity_kind () == depset::EK_SPECIALIZATION)
13469 : : {
13470 : 741373 : dep->set_special ();
13471 : 741373 : dep->deps.safe_push (reinterpret_cast<depset *> (entry));
13472 : 741373 : if (!decl_p)
13473 : 223128 : dep->set_flag_bit<DB_TYPE_SPEC_BIT> ();
13474 : : }
13475 : :
13476 : 769073 : if (needs_reaching)
13477 : 693157 : dep->set_flag_bit<DB_UNREACHED_BIT> ();
13478 : 769073 : if (is_friend)
13479 : 0 : dep->set_flag_bit<DB_FRIEND_SPEC_BIT> ();
13480 : : }
13481 : : }
13482 : 3648 : data.release ();
13483 : 3648 : }
13484 : :
13485 : : /* Add a depset into the mergeable hash. */
13486 : :
13487 : : void
13488 : 1258176 : depset::hash::add_mergeable (depset *mergeable)
13489 : : {
13490 : 1258176 : gcc_checking_assert (is_key_order ());
13491 : 1258176 : entity_kind ek = mergeable->get_entity_kind ();
13492 : 1258176 : tree decl = mergeable->get_entity ();
13493 : 1258176 : gcc_checking_assert (ek < EK_DIRECT_HWM);
13494 : :
13495 : 1258176 : depset **slot = entity_slot (decl, true);
13496 : 1258176 : gcc_checking_assert (!*slot);
13497 : 1258176 : depset *dep = make_entity (decl, ek);
13498 : 1258176 : *slot = dep;
13499 : :
13500 : 1258176 : worklist.safe_push (dep);
13501 : :
13502 : : /* So we can locate the mergeable depset this depset refers to,
13503 : : mark the first dep. */
13504 : 1258176 : dep->set_special ();
13505 : 1258176 : dep->deps.safe_push (mergeable);
13506 : 1258176 : }
13507 : :
13508 : : /* Find the innermost-namespace scope of DECL, and that
13509 : : namespace-scope decl. */
13510 : :
13511 : : tree
13512 : 37774993 : find_pending_key (tree decl, tree *decl_p = nullptr)
13513 : : {
13514 : 37774993 : tree ns = decl;
13515 : 45335213 : do
13516 : : {
13517 : 45335213 : decl = ns;
13518 : 45335213 : ns = CP_DECL_CONTEXT (ns);
13519 : 45335213 : if (TYPE_P (ns))
13520 : 4445119 : ns = TYPE_NAME (ns);
13521 : : }
13522 : 45335213 : while (TREE_CODE (ns) != NAMESPACE_DECL);
13523 : :
13524 : 37774993 : if (decl_p)
13525 : 37228033 : *decl_p = decl;
13526 : :
13527 : 37774993 : return ns;
13528 : : }
13529 : :
13530 : : /* Iteratively find dependencies. During the walk we may find more
13531 : : entries on the same binding that need walking. */
13532 : :
13533 : : void
13534 : 360125 : depset::hash::find_dependencies (module_state *module)
13535 : : {
13536 : 360125 : trees_out walker (NULL, module, *this);
13537 : 360125 : vec<depset *> unreached;
13538 : 720250 : unreached.create (worklist.length ());
13539 : :
13540 : 842 : for (;;)
13541 : : {
13542 : 360967 : reached_unreached = false;
13543 : 4736397 : while (worklist.length ())
13544 : : {
13545 : 4375430 : depset *item = worklist.pop ();
13546 : :
13547 : 4375430 : gcc_checking_assert (!item->is_binding ());
13548 : 4375430 : if (item->is_unreached ())
13549 : 1828135 : unreached.quick_push (item);
13550 : : else
13551 : : {
13552 : 2547295 : current = item;
13553 : 2547295 : tree decl = current->get_entity ();
13554 : 2547295 : dump (is_key_order () ? dumper::MERGE : dumper::DEPEND)
13555 : 2575547 : && dump ("Dependencies of %s %C:%N",
13556 : 28252 : is_key_order () ? "key-order"
13557 : 28252 : : current->entity_kind_name (), TREE_CODE (decl), decl);
13558 : 2547295 : dump.indent ();
13559 : 2547295 : walker.begin ();
13560 : 2547295 : if (current->get_entity_kind () == EK_USING)
13561 : 26887 : walker.tree_node (OVL_FUNCTION (decl));
13562 : 2520408 : else if (TREE_VISITED (decl))
13563 : : /* A global tree. */;
13564 : 2518771 : else if (item->get_entity_kind () == EK_NAMESPACE)
13565 : : {
13566 : 2380 : module->note_location (DECL_SOURCE_LOCATION (decl));
13567 : 2380 : add_namespace_context (current, CP_DECL_CONTEXT (decl));
13568 : : }
13569 : : else
13570 : : {
13571 : 2516391 : walker.mark_declaration (decl, current->has_defn ());
13572 : :
13573 : 2516391 : if (!walker.is_key_order ()
13574 : 2516391 : && (item->get_entity_kind () == EK_SPECIALIZATION
13575 : 736839 : || item->get_entity_kind () == EK_PARTIAL
13576 : 711267 : || (item->get_entity_kind () == EK_DECL
13577 : 711267 : && item->is_member ())))
13578 : : {
13579 : 546960 : tree ns = find_pending_key (decl, nullptr);
13580 : 546960 : add_namespace_context (item, ns);
13581 : : }
13582 : :
13583 : 2516391 : walker.decl_value (decl, current);
13584 : 2516391 : if (current->has_defn ())
13585 : 456668 : walker.write_definition (decl);
13586 : : }
13587 : 2547295 : walker.end ();
13588 : :
13589 : 2547295 : if (!walker.is_key_order ()
13590 : 1289119 : && TREE_CODE (decl) == TEMPLATE_DECL
13591 : 3002158 : && !DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (decl))
13592 : : /* Mark all the explicit & partial specializations as
13593 : : reachable. */
13594 : 448875 : for (tree cons = DECL_TEMPLATE_INSTANTIATIONS (decl);
13595 : 713587 : cons; cons = TREE_CHAIN (cons))
13596 : : {
13597 : 264712 : tree spec = TREE_VALUE (cons);
13598 : 264712 : if (TYPE_P (spec))
13599 : 233917 : spec = TYPE_NAME (spec);
13600 : 264712 : int use_tpl;
13601 : 264712 : node_template_info (spec, use_tpl);
13602 : 264712 : if (use_tpl & 2)
13603 : : {
13604 : 43626 : depset *spec_dep = find_dependency (spec);
13605 : 43626 : if (spec_dep->get_entity_kind () == EK_REDIRECT)
13606 : 24791 : spec_dep = spec_dep->deps[0];
13607 : 43626 : if (spec_dep->is_unreached ())
13608 : : {
13609 : 4758 : reached_unreached = true;
13610 : 4758 : spec_dep->clear_flag_bit<DB_UNREACHED_BIT> ();
13611 : 265741 : dump (dumper::DEPEND)
13612 : 1029 : && dump ("Reaching unreached specialization"
13613 : 1029 : " %C:%N", TREE_CODE (spec), spec);
13614 : : }
13615 : : }
13616 : : }
13617 : :
13618 : 2547295 : dump.outdent ();
13619 : 2547295 : current = NULL;
13620 : : }
13621 : : }
13622 : :
13623 : 360967 : if (!reached_unreached)
13624 : : break;
13625 : :
13626 : : /* It's possible the we reached the unreached before we
13627 : : processed it in the above loop, so we'll be doing this an
13628 : : extra time. However, to avoid that we have to do some
13629 : : bit shuffling that also involves a scan of the list.
13630 : : Swings & roundabouts I guess. */
13631 : 842 : std::swap (worklist, unreached);
13632 : : }
13633 : :
13634 : 360125 : unreached.release ();
13635 : 360125 : }
13636 : :
13637 : : /* Compare two entries of a single binding. TYPE_DECL before
13638 : : non-exported before exported. */
13639 : :
13640 : : static int
13641 : 674182 : binding_cmp (const void *a_, const void *b_)
13642 : : {
13643 : 674182 : depset *a = *(depset *const *)a_;
13644 : 674182 : depset *b = *(depset *const *)b_;
13645 : :
13646 : 674182 : tree a_ent = a->get_entity ();
13647 : 674182 : tree b_ent = b->get_entity ();
13648 : 674182 : gcc_checking_assert (a_ent != b_ent
13649 : : && !a->is_binding ()
13650 : : && !b->is_binding ());
13651 : :
13652 : : /* Implicit typedefs come first. */
13653 : 674182 : bool a_implicit = DECL_IMPLICIT_TYPEDEF_P (a_ent);
13654 : 674182 : bool b_implicit = DECL_IMPLICIT_TYPEDEF_P (b_ent);
13655 : 674182 : if (a_implicit || b_implicit)
13656 : : {
13657 : : /* A binding with two implicit type decls? That's unpossible! */
13658 : 308 : gcc_checking_assert (!(a_implicit && b_implicit));
13659 : 462 : return a_implicit ? -1 : +1; /* Implicit first. */
13660 : : }
13661 : :
13662 : : /* Hidden before non-hidden. */
13663 : 673874 : bool a_hidden = a->is_hidden ();
13664 : 673874 : bool b_hidden = b->is_hidden ();
13665 : 673874 : if (a_hidden != b_hidden)
13666 : 70273 : return a_hidden ? -1 : +1;
13667 : :
13668 : 627921 : bool a_using = a->get_entity_kind () == depset::EK_USING;
13669 : 627921 : bool a_export;
13670 : 627921 : if (a_using)
13671 : : {
13672 : 37080 : a_export = OVL_EXPORT_P (a_ent);
13673 : 37080 : a_ent = OVL_FUNCTION (a_ent);
13674 : : }
13675 : : else
13676 : 590841 : a_export = DECL_MODULE_EXPORT_P (TREE_CODE (a_ent) == CONST_DECL
13677 : : ? TYPE_NAME (TREE_TYPE (a_ent))
13678 : : : STRIP_TEMPLATE (a_ent));
13679 : :
13680 : 627921 : bool b_using = b->get_entity_kind () == depset::EK_USING;
13681 : 627921 : bool b_export;
13682 : 627921 : if (b_using)
13683 : : {
13684 : 30864 : b_export = OVL_EXPORT_P (b_ent);
13685 : 30864 : b_ent = OVL_FUNCTION (b_ent);
13686 : : }
13687 : : else
13688 : 597057 : b_export = DECL_MODULE_EXPORT_P (TREE_CODE (b_ent) == CONST_DECL
13689 : : ? TYPE_NAME (TREE_TYPE (b_ent))
13690 : : : STRIP_TEMPLATE (b_ent));
13691 : :
13692 : : /* Non-exports before exports. */
13693 : 627921 : if (a_export != b_export)
13694 : 72 : return a_export ? +1 : -1;
13695 : :
13696 : : /* At this point we don't care, but want a stable sort. */
13697 : :
13698 : 627876 : if (a_using != b_using)
13699 : : /* using first. */
13700 : 31578 : return a_using? -1 : +1;
13701 : :
13702 : 604752 : return DECL_UID (a_ent) < DECL_UID (b_ent) ? -1 : +1;
13703 : : }
13704 : :
13705 : : /* Sort the bindings, issue errors about bad internal refs. */
13706 : :
13707 : : bool
13708 : 1824 : depset::hash::finalize_dependencies ()
13709 : : {
13710 : 1824 : bool ok = true;
13711 : 1824 : depset::hash::iterator end (this->end ());
13712 : 3472318 : for (depset::hash::iterator iter (begin ()); iter != end; ++iter)
13713 : : {
13714 : 1735247 : depset *dep = *iter;
13715 : 1735247 : if (dep->is_binding ())
13716 : : {
13717 : : /* Keep the containing namespace dep first. */
13718 : 194401 : gcc_checking_assert (dep->deps.length () > 1
13719 : : && (dep->deps[0]->get_entity_kind ()
13720 : : == EK_NAMESPACE)
13721 : : && (dep->deps[0]->get_entity ()
13722 : : == dep->get_entity ()));
13723 : 194401 : if (dep->deps.length () > 2)
13724 : 15102 : gcc_qsort (&dep->deps[1], dep->deps.length () - 1,
13725 : : sizeof (dep->deps[1]), binding_cmp);
13726 : : }
13727 : 1540846 : else if (dep->refs_internal ())
13728 : : {
13729 : 30 : for (unsigned ix = dep->deps.length (); ix--;)
13730 : : {
13731 : 15 : depset *rdep = dep->deps[ix];
13732 : 15 : if (rdep->is_internal ())
13733 : : {
13734 : : // FIXME:QOI Better location information? We're
13735 : : // losing, so it doesn't matter about efficiency
13736 : 15 : tree decl = dep->get_entity ();
13737 : 15 : error_at (DECL_SOURCE_LOCATION (decl),
13738 : : "%q#D references internal linkage entity %q#D",
13739 : : decl, rdep->get_entity ());
13740 : 15 : break;
13741 : : }
13742 : : }
13743 : : ok = false;
13744 : : }
13745 : : }
13746 : :
13747 : 1824 : return ok;
13748 : : }
13749 : :
13750 : : /* Core of TARJAN's algorithm to find Strongly Connected Components
13751 : : within a graph. See https://en.wikipedia.org/wiki/
13752 : : Tarjan%27s_strongly_connected_components_algorithm for details.
13753 : :
13754 : : We use depset::section as lowlink. Completed nodes have
13755 : : depset::cluster containing the cluster number, with the top
13756 : : bit set.
13757 : :
13758 : : A useful property is that the output vector is a reverse
13759 : : topological sort of the resulting DAG. In our case that means
13760 : : dependent SCCs are found before their dependers. We make use of
13761 : : that property. */
13762 : :
13763 : : void
13764 : 2741636 : depset::tarjan::connect (depset *v)
13765 : : {
13766 : 2741636 : gcc_checking_assert (v->is_binding ()
13767 : : || !(v->is_unreached () || v->is_import ()));
13768 : :
13769 : 2741636 : v->cluster = v->section = ++index;
13770 : 2741636 : stack.safe_push (v);
13771 : :
13772 : : /* Walk all our dependencies, ignore a first marked slot */
13773 : 22889106 : for (unsigned ix = v->is_special (); ix != v->deps.length (); ix++)
13774 : : {
13775 : 8707848 : depset *dep = v->deps[ix];
13776 : :
13777 : 17172395 : if (dep->is_binding () || !dep->is_import ())
13778 : : {
13779 : 8707170 : unsigned lwm = dep->cluster;
13780 : :
13781 : 8707170 : if (!dep->cluster)
13782 : : {
13783 : : /* A new node. Connect it. */
13784 : 1519243 : connect (dep);
13785 : 1519243 : lwm = dep->section;
13786 : : }
13787 : :
13788 : 8707170 : if (dep->section && v->section > lwm)
13789 : 1388693 : v->section = lwm;
13790 : : }
13791 : : }
13792 : :
13793 : 2741636 : if (v->section == v->cluster)
13794 : : {
13795 : : /* Root of a new SCC. Push all the members onto the result list. */
13796 : : unsigned num = v->cluster;
13797 : 2741636 : depset *p;
13798 : 2741636 : do
13799 : : {
13800 : 2741636 : p = stack.pop ();
13801 : 2741636 : p->cluster = num;
13802 : 2741636 : p->section = 0;
13803 : 2741636 : result.quick_push (p);
13804 : : }
13805 : 2741636 : while (p != v);
13806 : : }
13807 : 2741636 : }
13808 : :
13809 : : /* Compare two depsets. The specific ordering is unimportant, we're
13810 : : just trying to get consistency. */
13811 : :
13812 : : static int
13813 : 129947515 : depset_cmp (const void *a_, const void *b_)
13814 : : {
13815 : 129947515 : depset *a = *(depset *const *)a_;
13816 : 129947515 : depset *b = *(depset *const *)b_;
13817 : :
13818 : 129947515 : depset::entity_kind a_kind = a->get_entity_kind ();
13819 : 129947515 : depset::entity_kind b_kind = b->get_entity_kind ();
13820 : :
13821 : 129947515 : if (a_kind != b_kind)
13822 : : /* Different entity kinds, order by that. */
13823 : 6314692 : return a_kind < b_kind ? -1 : +1;
13824 : :
13825 : 125435560 : tree a_decl = a->get_entity ();
13826 : 125435560 : tree b_decl = b->get_entity ();
13827 : 125435560 : if (a_kind == depset::EK_USING)
13828 : : {
13829 : : /* If one is a using, the other must be too. */
13830 : 1525652 : a_decl = OVL_FUNCTION (a_decl);
13831 : 1525652 : b_decl = OVL_FUNCTION (b_decl);
13832 : : }
13833 : :
13834 : 125435560 : if (a_decl != b_decl)
13835 : : /* Different entities, order by their UID. */
13836 : 115471959 : return DECL_UID (a_decl) < DECL_UID (b_decl) ? -1 : +1;
13837 : :
13838 : 9963601 : if (a_kind == depset::EK_BINDING)
13839 : : {
13840 : : /* Both are bindings. Order by identifier hash. */
13841 : 9958216 : gcc_checking_assert (a->get_name () != b->get_name ());
13842 : 9958216 : hashval_t ah = IDENTIFIER_HASH_VALUE (a->get_name ());
13843 : 9958216 : hashval_t bh = IDENTIFIER_HASH_VALUE (b->get_name ());
13844 : 14837738 : return (ah == bh ? 0 : ah < bh ? -1 : +1);
13845 : : }
13846 : :
13847 : : /* They are the same decl. This can happen with two using decls
13848 : : pointing to the same target. The best we can aim for is
13849 : : consistently telling qsort how to order them. Hopefully we'll
13850 : : never have to debug a case that depends on this. Oh, who am I
13851 : : kidding? Good luck. */
13852 : 5385 : gcc_checking_assert (a_kind == depset::EK_USING);
13853 : :
13854 : : /* Order by depset address. Not the best, but it is something. */
13855 : 5385 : return a < b ? -1 : +1;
13856 : : }
13857 : :
13858 : : /* Sort the clusters in SCC such that those that depend on one another
13859 : : are placed later. */
13860 : :
13861 : : // FIXME: I am not convinced this is needed and, if needed,
13862 : : // sufficient. We emit the decls in this order but that emission
13863 : : // could walk into later decls (from the body of the decl, or default
13864 : : // arg-like things). Why doesn't that walk do the right thing? And
13865 : : // if it DTRT why do we need to sort here -- won't things naturally
13866 : : // work? I think part of the issue is that when we're going to refer
13867 : : // to an entity by name, and that entity is in the same cluster as us,
13868 : : // we need to actually walk that entity, if we've not already walked
13869 : : // it.
13870 : : static void
13871 : 358301 : sort_cluster (depset::hash *original, depset *scc[], unsigned size)
13872 : : {
13873 : 358301 : depset::hash table (size, original);
13874 : :
13875 : 358301 : dump.indent ();
13876 : :
13877 : : /* Place bindings last, usings before that. It's not strictly
13878 : : necessary, but it does make things neater. Says Mr OCD. */
13879 : : unsigned bind_lwm = size;
13880 : : unsigned use_lwm = size;
13881 : 1837756 : for (unsigned ix = 0; ix != use_lwm;)
13882 : : {
13883 : 1479455 : depset *dep = scc[ix];
13884 : 1479455 : switch (dep->get_entity_kind ())
13885 : : {
13886 : 194392 : case depset::EK_BINDING:
13887 : : /* Move to end. No increment. Notice this could be moving
13888 : : a using decl, which we'll then move again. */
13889 : 194392 : if (--bind_lwm != ix)
13890 : : {
13891 : 102662 : scc[ix] = scc[bind_lwm];
13892 : 102662 : scc[bind_lwm] = dep;
13893 : : }
13894 : 194392 : if (use_lwm > bind_lwm)
13895 : : {
13896 : 159098 : use_lwm--;
13897 : 159098 : break;
13898 : : }
13899 : : /* We must have copied a using, so move it too. */
13900 : 35294 : dep = scc[ix];
13901 : 35294 : gcc_checking_assert (dep->get_entity_kind () == depset::EK_USING);
13902 : : /* FALLTHROUGH */
13903 : :
13904 : 62181 : case depset::EK_USING:
13905 : 62181 : if (--use_lwm != ix)
13906 : : {
13907 : 46087 : scc[ix] = scc[use_lwm];
13908 : 46087 : scc[use_lwm] = dep;
13909 : : }
13910 : : break;
13911 : :
13912 : 1258176 : case depset::EK_DECL:
13913 : 1258176 : case depset::EK_SPECIALIZATION:
13914 : 1258176 : case depset::EK_PARTIAL:
13915 : 1258176 : table.add_mergeable (dep);
13916 : 1258176 : ix++;
13917 : 1258176 : break;
13918 : :
13919 : 0 : default:
13920 : 0 : gcc_unreachable ();
13921 : : }
13922 : : }
13923 : :
13924 : 358301 : gcc_checking_assert (use_lwm <= bind_lwm);
13925 : 358529 : dump (dumper::MERGE) && dump ("Ordering %u/%u depsets", use_lwm, size);
13926 : :
13927 : 358301 : table.find_dependencies (nullptr);
13928 : :
13929 : 358301 : vec<depset *> order = table.connect ();
13930 : 716602 : gcc_checking_assert (order.length () == use_lwm);
13931 : :
13932 : : /* Now rewrite entries [0,lwm), in the dependency order we
13933 : : discovered. Usually each entity is in its own cluster. Rarely,
13934 : : we can get multi-entity clusters, in which case all but one must
13935 : : only be reached from within the cluster. This happens for
13936 : : something like:
13937 : :
13938 : : template<typename T>
13939 : : auto Foo (const T &arg) -> TPL<decltype (arg)>;
13940 : :
13941 : : The instantiation of TPL will be in the specialization table, and
13942 : : refer to Foo via arg. But we can only get to that specialization
13943 : : from Foo's declaration, so we only need to treat Foo as mergable
13944 : : (We'll do structural comparison of TPL<decltype (arg)>).
13945 : :
13946 : : Finding the single cluster entry dep is very tricky and
13947 : : expensive. Let's just not do that. It's harmless in this case
13948 : : anyway. */
13949 : : unsigned pos = 0;
13950 : : unsigned cluster = ~0u;
13951 : 3232954 : for (unsigned ix = 0; ix != order.length (); ix++)
13952 : : {
13953 : 1258176 : gcc_checking_assert (order[ix]->is_special ());
13954 : 1258176 : depset *dep = order[ix]->deps[0];
13955 : 1258176 : scc[pos++] = dep;
13956 : 1258848 : dump (dumper::MERGE)
13957 : 672 : && dump ("Mergeable %u is %N%s", ix, dep->get_entity (),
13958 : 672 : order[ix]->cluster == cluster ? " (tight)" : "");
13959 : 1258176 : cluster = order[ix]->cluster;
13960 : : }
13961 : :
13962 : 358301 : gcc_checking_assert (pos == use_lwm);
13963 : :
13964 : 358301 : order.release ();
13965 : 358529 : dump (dumper::MERGE) && dump ("Ordered %u keys", pos);
13966 : 358301 : dump.outdent ();
13967 : 358301 : }
13968 : :
13969 : : /* Reduce graph to SCCS clusters. SCCS will be populated with the
13970 : : depsets in dependency order. Each depset's CLUSTER field contains
13971 : : its cluster number. Each SCC has a unique cluster number, and are
13972 : : contiguous in SCCS. Cluster numbers are otherwise arbitrary. */
13973 : :
13974 : : vec<depset *>
13975 : 360119 : depset::hash::connect ()
13976 : : {
13977 : 360119 : tarjan connector (size ());
13978 : 360119 : vec<depset *> deps;
13979 : 360119 : deps.create (size ());
13980 : 360119 : iterator end (this->end ());
13981 : 6346845 : for (iterator iter (begin ()); iter != end; ++iter)
13982 : : {
13983 : 2993363 : depset *item = *iter;
13984 : :
13985 : 2993363 : entity_kind kind = item->get_entity_kind ();
13986 : 2798971 : if (kind == EK_BINDING
13987 : 2798971 : || !(kind == EK_REDIRECT
13988 : 2770493 : || item->is_unreached ()
13989 : 2547624 : || item->is_import ()))
13990 : 2741636 : deps.quick_push (item);
13991 : : }
13992 : :
13993 : : /* Iteration over the hash table is an unspecified ordering. While
13994 : : that has advantages, it causes 2 problems. Firstly repeatable
13995 : : builds are tricky. Secondly creating testcases that check
13996 : : dependencies are correct by making sure a bad ordering would
13997 : : happen if that was wrong. */
13998 : 1942631 : deps.qsort (depset_cmp);
13999 : :
14000 : 3101755 : while (deps.length ())
14001 : : {
14002 : 2741636 : depset *v = deps.pop ();
14003 : 2741636 : dump (dumper::CLUSTER) &&
14004 : 1599 : (v->is_binding ()
14005 : 195 : ? dump ("Connecting binding %P", v->get_entity (), v->get_name ())
14006 : 1404 : : dump ("Connecting %s %s %C:%N",
14007 : 1404 : is_key_order () ? "key-order"
14008 : 774 : : !v->has_defn () ? "declaration" : "definition",
14009 : 1404 : v->entity_kind_name (), TREE_CODE (v->get_entity ()),
14010 : : v->get_entity ()));
14011 : 2741636 : if (!v->cluster)
14012 : 1222393 : connector.connect (v);
14013 : : }
14014 : :
14015 : 360119 : deps.release ();
14016 : 720238 : return connector.result;
14017 : 360119 : }
14018 : :
14019 : : /* Initialize location spans. */
14020 : :
14021 : : void
14022 : 3238 : loc_spans::init (const line_maps *lmaps, const line_map_ordinary *map)
14023 : : {
14024 : 3238 : gcc_checking_assert (!init_p ());
14025 : 3238 : spans = new vec<span> ();
14026 : 3238 : spans->reserve (20);
14027 : :
14028 : 3238 : span interval;
14029 : 3238 : interval.ordinary.first = 0;
14030 : 3238 : interval.macro.second = MAX_LOCATION_T + 1;
14031 : 3238 : interval.ordinary_delta = interval.macro_delta = 0;
14032 : :
14033 : : /* A span for reserved fixed locs. */
14034 : 3238 : interval.ordinary.second
14035 : 3238 : = MAP_START_LOCATION (LINEMAPS_ORDINARY_MAP_AT (line_table, 0));
14036 : 3238 : interval.macro.first = interval.macro.second;
14037 : 3238 : dump (dumper::LOCATION)
14038 : 42 : && dump ("Fixed span %u ordinary:[%u,%u) macro:[%u,%u)", spans->length (),
14039 : : interval.ordinary.first, interval.ordinary.second,
14040 : : interval.macro.first, interval.macro.second);
14041 : 3238 : spans->quick_push (interval);
14042 : :
14043 : : /* A span for command line & forced headers. */
14044 : 3238 : interval.ordinary.first = interval.ordinary.second;
14045 : 3238 : interval.macro.second = interval.macro.first;
14046 : 3238 : if (map)
14047 : : {
14048 : 3235 : interval.ordinary.second = map->start_location;
14049 : 6440 : interval.macro.first = LINEMAPS_MACRO_LOWEST_LOCATION (lmaps);
14050 : : }
14051 : 3238 : dump (dumper::LOCATION)
14052 : 21 : && dump ("Pre span %u ordinary:[%u,%u) macro:[%u,%u)", spans->length (),
14053 : : interval.ordinary.first, interval.ordinary.second,
14054 : : interval.macro.first, interval.macro.second);
14055 : 3238 : spans->quick_push (interval);
14056 : :
14057 : : /* Start an interval for the main file. */
14058 : 3238 : interval.ordinary.first = interval.ordinary.second;
14059 : 3238 : interval.macro.second = interval.macro.first;
14060 : 3238 : dump (dumper::LOCATION)
14061 : 21 : && dump ("Main span %u ordinary:[%u,*) macro:[*,%u)", spans->length (),
14062 : : interval.ordinary.first, interval.macro.second);
14063 : 3238 : spans->quick_push (interval);
14064 : 3238 : }
14065 : :
14066 : : /* Reopen the span, if we want the about-to-be-inserted set of maps to
14067 : : be propagated in our own location table. I.e. we are the primary
14068 : : interface and we're importing a partition. */
14069 : :
14070 : : bool
14071 : 2075 : loc_spans::maybe_propagate (module_state *import, location_t hwm)
14072 : : {
14073 : 2075 : bool opened = (module_interface_p () && !module_partition_p ()
14074 : 2344 : && import->is_partition ());
14075 : 87 : if (opened)
14076 : 87 : open (hwm);
14077 : 2075 : return opened;
14078 : : }
14079 : :
14080 : : /* Open a new linemap interval. The just-created ordinary map is the
14081 : : first map of the interval. */
14082 : :
14083 : : void
14084 : 796 : loc_spans::open (location_t hwm)
14085 : : {
14086 : 796 : span interval;
14087 : 796 : interval.ordinary.first = interval.ordinary.second = hwm;
14088 : 1592 : interval.macro.first = interval.macro.second
14089 : 796 : = LINEMAPS_MACRO_LOWEST_LOCATION (line_table);
14090 : 796 : interval.ordinary_delta = interval.macro_delta = 0;
14091 : 796 : dump (dumper::LOCATION)
14092 : 0 : && dump ("Opening span %u ordinary:[%u,... macro:...,%u)",
14093 : 0 : spans->length (), interval.ordinary.first,
14094 : : interval.macro.second);
14095 : 796 : if (spans->length ())
14096 : : {
14097 : : /* No overlapping! */
14098 : 796 : auto &last = spans->last ();
14099 : 796 : gcc_checking_assert (interval.ordinary.first >= last.ordinary.second);
14100 : 796 : gcc_checking_assert (interval.macro.second <= last.macro.first);
14101 : : }
14102 : 796 : spans->safe_push (interval);
14103 : 796 : }
14104 : :
14105 : : /* Close out the current linemap interval. The last maps are within
14106 : : the interval. */
14107 : :
14108 : : void
14109 : 4034 : loc_spans::close ()
14110 : : {
14111 : 4034 : span &interval = spans->last ();
14112 : :
14113 : 4034 : interval.ordinary.second
14114 : 4034 : = ((line_table->highest_location + (1 << line_table->default_range_bits))
14115 : 4034 : & ~((1u << line_table->default_range_bits) - 1));
14116 : 4034 : interval.macro.first = LINEMAPS_MACRO_LOWEST_LOCATION (line_table);
14117 : 4034 : dump (dumper::LOCATION)
14118 : 21 : && dump ("Closing span %u ordinary:[%u,%u) macro:[%u,%u)",
14119 : 21 : spans->length () - 1,
14120 : : interval.ordinary.first,interval.ordinary.second,
14121 : : interval.macro.first, interval.macro.second);
14122 : 4034 : }
14123 : :
14124 : : /* Given an ordinary location LOC, return the lmap_interval it resides
14125 : : in. NULL if it is not in an interval. */
14126 : :
14127 : : const loc_spans::span *
14128 : 40988876 : loc_spans::ordinary (location_t loc)
14129 : : {
14130 : 40988876 : unsigned len = spans->length ();
14131 : 81583017 : unsigned pos = 0;
14132 : 81595017 : while (len)
14133 : : {
14134 : 81582281 : unsigned half = len / 2;
14135 : 81582281 : const span &probe = (*spans)[pos + half];
14136 : 81582281 : if (loc < probe.ordinary.first)
14137 : : len = half;
14138 : 81570281 : else if (loc < probe.ordinary.second)
14139 : 40976140 : return &probe;
14140 : : else
14141 : : {
14142 : 40594141 : pos += half + 1;
14143 : 40594141 : len = len - (half + 1);
14144 : : }
14145 : : }
14146 : : return NULL;
14147 : : }
14148 : :
14149 : : /* Likewise, given a macro location LOC, return the lmap interval it
14150 : : resides in. */
14151 : :
14152 : : const loc_spans::span *
14153 : 3983999 : loc_spans::macro (location_t loc)
14154 : : {
14155 : 3983999 : unsigned len = spans->length ();
14156 : 7953648 : unsigned pos = 0;
14157 : 7953648 : while (len)
14158 : : {
14159 : 7953636 : unsigned half = len / 2;
14160 : 7953636 : const span &probe = (*spans)[pos + half];
14161 : 7953636 : if (loc >= probe.macro.second)
14162 : : len = half;
14163 : 7953636 : else if (loc >= probe.macro.first)
14164 : 3983987 : return &probe;
14165 : : else
14166 : : {
14167 : 3969649 : pos += half + 1;
14168 : 3969649 : len = len - (half + 1);
14169 : : }
14170 : : }
14171 : : return NULL;
14172 : : }
14173 : :
14174 : : /* Return the ordinary location closest to FROM. */
14175 : :
14176 : : static location_t
14177 : 4673 : ordinary_loc_of (line_maps *lmaps, location_t from)
14178 : : {
14179 : 9373 : while (!IS_ORDINARY_LOC (from))
14180 : : {
14181 : 27 : if (IS_ADHOC_LOC (from))
14182 : 27 : from = get_location_from_adhoc_loc (lmaps, from);
14183 : 54 : if (from >= LINEMAPS_MACRO_LOWEST_LOCATION (lmaps))
14184 : : {
14185 : : /* Find the ordinary location nearest FROM. */
14186 : 0 : const line_map *map = linemap_lookup (lmaps, from);
14187 : 0 : const line_map_macro *mac_map = linemap_check_macro (map);
14188 : 0 : from = mac_map->get_expansion_point_location ();
14189 : : }
14190 : : }
14191 : 4673 : return from;
14192 : : }
14193 : :
14194 : : static module_state **
14195 : 8006 : get_module_slot (tree name, module_state *parent, bool partition, bool insert)
14196 : : {
14197 : 8006 : module_state_hash::compare_type ct (name, uintptr_t (parent) | partition);
14198 : 8006 : hashval_t hv = module_state_hash::hash (ct);
14199 : :
14200 : 8006 : return modules_hash->find_slot_with_hash (ct, hv, insert ? INSERT : NO_INSERT);
14201 : : }
14202 : :
14203 : : static module_state *
14204 : 312 : get_primary (module_state *parent)
14205 : : {
14206 : 2794 : while (parent->is_partition ())
14207 : 195 : parent = parent->parent;
14208 : :
14209 : 2599 : if (!parent->name)
14210 : : // Implementation unit has null name
14211 : 120 : parent = parent->parent;
14212 : :
14213 : 324 : return parent;
14214 : : }
14215 : :
14216 : : /* Find or create module NAME & PARENT in the hash table. */
14217 : :
14218 : : module_state *
14219 : 8006 : get_module (tree name, module_state *parent, bool partition)
14220 : : {
14221 : : /* We might be given an empty NAME if preprocessing fails to handle
14222 : : a header-name token. */
14223 : 8006 : if (name && TREE_CODE (name) == STRING_CST
14224 : 10246 : && TREE_STRING_LENGTH (name) == 0)
14225 : : return nullptr;
14226 : :
14227 : 8006 : if (partition)
14228 : : {
14229 : 519 : if (!parent)
14230 : 123 : parent = get_primary ((*modules)[0]);
14231 : :
14232 : 519 : if (!parent->is_partition () && !parent->flatname)
14233 : 108 : parent->set_flatname ();
14234 : : }
14235 : :
14236 : 8006 : module_state **slot = get_module_slot (name, parent, partition, true);
14237 : 8006 : module_state *state = *slot;
14238 : 8006 : if (!state)
14239 : : {
14240 : 4359 : state = (new (ggc_alloc<module_state> ())
14241 : 4359 : module_state (name, parent, partition));
14242 : 4359 : *slot = state;
14243 : : }
14244 : : return state;
14245 : : }
14246 : :
14247 : : /* Process string name PTR into a module_state. */
14248 : :
14249 : : static module_state *
14250 : 293 : get_module (const char *ptr)
14251 : : {
14252 : : /* On DOS based file systems, there is an ambiguity with A:B which can be
14253 : : interpreted as a module Module:Partition or Drive:PATH. Interpret strings
14254 : : which clearly starts as pathnames as header-names and everything else is
14255 : : treated as a (possibly malformed) named moduled. */
14256 : 293 : if (IS_DIR_SEPARATOR (ptr[ptr[0] == '.']) // ./FOO or /FOO
14257 : : #if HAVE_DOS_BASED_FILE_SYSTEM
14258 : : || (HAS_DRIVE_SPEC (ptr) && IS_DIR_SEPARATOR (ptr[2])) // A:/FOO
14259 : : #endif
14260 : : || false)
14261 : : /* A header name. */
14262 : 94 : return get_module (build_string (strlen (ptr), ptr));
14263 : :
14264 : : bool partition = false;
14265 : : module_state *mod = NULL;
14266 : :
14267 : 918 : for (const char *probe = ptr;; probe++)
14268 : 1117 : if (!*probe || *probe == '.' || *probe == ':')
14269 : : {
14270 : 253 : if (probe == ptr)
14271 : : return NULL;
14272 : :
14273 : 253 : mod = get_module (get_identifier_with_length (ptr, probe - ptr),
14274 : : mod, partition);
14275 : 253 : ptr = probe;
14276 : 253 : if (*ptr == ':')
14277 : : {
14278 : 54 : if (partition)
14279 : : return NULL;
14280 : : partition = true;
14281 : : }
14282 : :
14283 : 253 : if (!*ptr++)
14284 : : break;
14285 : : }
14286 : 864 : else if (!(ISALPHA (*probe) || *probe == '_'
14287 : 12 : || (probe != ptr && ISDIGIT (*probe))))
14288 : : return NULL;
14289 : :
14290 : : return mod;
14291 : : }
14292 : :
14293 : : /* Create a new mapper connecting to OPTION. */
14294 : :
14295 : : module_client *
14296 : 3238 : make_mapper (location_t loc, class mkdeps *deps)
14297 : : {
14298 : 3238 : timevar_start (TV_MODULE_MAPPER);
14299 : 3238 : const char *option = module_mapper_name;
14300 : 3238 : if (!option)
14301 : 3196 : option = getenv ("CXX_MODULE_MAPPER");
14302 : :
14303 : 6476 : mapper = module_client::open_module_client
14304 : 3238 : (loc, option, deps, &set_cmi_repo,
14305 : 3238 : (save_decoded_options[0].opt_index == OPT_SPECIAL_program_name)
14306 : 3238 : && save_decoded_options[0].arg != progname
14307 : : ? save_decoded_options[0].arg : nullptr);
14308 : :
14309 : 3238 : timevar_stop (TV_MODULE_MAPPER);
14310 : :
14311 : 3238 : return mapper;
14312 : : }
14313 : :
14314 : : static unsigned lazy_snum;
14315 : :
14316 : : static bool
14317 : 6674 : recursive_lazy (unsigned snum = ~0u)
14318 : : {
14319 : 6674 : if (lazy_snum)
14320 : : {
14321 : 0 : error_at (input_location, "recursive lazy load");
14322 : 0 : return true;
14323 : : }
14324 : :
14325 : 6674 : lazy_snum = snum;
14326 : 6674 : return false;
14327 : : }
14328 : :
14329 : : /* If THIS is the current purview, issue an import error and return false. */
14330 : :
14331 : : bool
14332 : 1954 : module_state::check_not_purview (location_t from)
14333 : : {
14334 : 1954 : module_state *imp = (*modules)[0];
14335 : 1954 : if (imp && !imp->name)
14336 : 1476 : imp = imp->parent;
14337 : 1954 : if (imp == this)
14338 : : {
14339 : : /* Cannot import the current module. */
14340 : 12 : error_at (from, "cannot import module in its own purview");
14341 : 12 : inform (loc, "module %qs declared here", get_flatname ());
14342 : 12 : return false;
14343 : : }
14344 : : return true;
14345 : : }
14346 : :
14347 : : /* Module name substitutions. */
14348 : : static vec<module_state *,va_heap> substs;
14349 : :
14350 : : void
14351 : 5327 : module_state::mangle (bool include_partition)
14352 : : {
14353 : 5327 : if (subst)
14354 : 113 : mangle_module_substitution (subst);
14355 : : else
14356 : : {
14357 : 5214 : if (parent)
14358 : 543 : parent->mangle (include_partition);
14359 : 5214 : if (include_partition || !is_partition ())
14360 : : {
14361 : : // Partitions are significant for global initializer
14362 : : // functions
14363 : 5154 : bool partition = is_partition () && !parent->is_partition ();
14364 : 5154 : subst = mangle_module_component (name, partition);
14365 : 5154 : substs.safe_push (this);
14366 : : }
14367 : : }
14368 : 5327 : }
14369 : :
14370 : : void
14371 : 4784 : mangle_module (int mod, bool include_partition)
14372 : : {
14373 : 4784 : module_state *imp = (*modules)[mod];
14374 : :
14375 : 4784 : gcc_checking_assert (!imp->is_header ());
14376 : :
14377 : 4784 : if (!imp->name)
14378 : : /* Set when importing the primary module interface. */
14379 : 198 : imp = imp->parent;
14380 : :
14381 : 4784 : imp->mangle (include_partition);
14382 : 4784 : }
14383 : :
14384 : : /* Clean up substitutions. */
14385 : : void
14386 : 4641 : mangle_module_fini ()
14387 : : {
14388 : 9795 : while (substs.length ())
14389 : 5154 : substs.pop ()->subst = 0;
14390 : 4641 : }
14391 : :
14392 : : /* Announce WHAT about the module. */
14393 : :
14394 : : void
14395 : 8526 : module_state::announce (const char *what) const
14396 : : {
14397 : 8526 : if (noisy_p ())
14398 : : {
14399 : 0 : fprintf (stderr, " %s:%s", what, get_flatname ());
14400 : 0 : fflush (stderr);
14401 : : }
14402 : 8526 : }
14403 : :
14404 : : /* A human-readable README section. The contents of this section to
14405 : : not contribute to the CRC, so the contents can change per
14406 : : compilation. That allows us to embed CWD, hostname, build time and
14407 : : what not. It is a STRTAB that may be extracted with:
14408 : : readelf -pgnu.c++.README $(module).gcm */
14409 : :
14410 : : void
14411 : 1824 : module_state::write_readme (elf_out *to, cpp_reader *reader, const char *dialect)
14412 : : {
14413 : 1824 : bytes_out readme (to);
14414 : :
14415 : 1824 : readme.begin (false);
14416 : :
14417 : 1824 : readme.printf ("GNU C++ %s",
14418 : 1824 : is_header () ? "header unit"
14419 : 1135 : : !is_partition () ? "primary interface"
14420 : 93 : : is_interface () ? "interface partition"
14421 : : : "internal partition");
14422 : :
14423 : : /* Compiler's version. */
14424 : 1824 : readme.printf ("compiler: %s", version_string);
14425 : :
14426 : : /* Module format version. */
14427 : 1824 : verstr_t string;
14428 : 1824 : version2string (MODULE_VERSION, string);
14429 : 1824 : readme.printf ("version: %s", string);
14430 : :
14431 : : /* Module information. */
14432 : 1824 : readme.printf ("module: %s", get_flatname ());
14433 : 1824 : readme.printf ("source: %s", main_input_filename);
14434 : 1824 : readme.printf ("dialect: %s", dialect);
14435 : 1824 : if (extensions)
14436 : 3 : readme.printf ("extensions: %s",
14437 : 3 : extensions & SE_OPENMP ? "-fopenmp" : "");
14438 : :
14439 : : /* The following fields could be expected to change between
14440 : : otherwise identical compilations. Consider a distributed build
14441 : : system. We should have a way of overriding that. */
14442 : 1824 : if (char *cwd = getcwd (NULL, 0))
14443 : : {
14444 : 1824 : readme.printf ("cwd: %s", cwd);
14445 : 1824 : free (cwd);
14446 : : }
14447 : 3648 : readme.printf ("repository: %s", cmi_repo ? cmi_repo : ".");
14448 : : #if NETWORKING
14449 : : {
14450 : : char hostname[64];
14451 : : if (!gethostname (hostname, sizeof (hostname)))
14452 : : readme.printf ("host: %s", hostname);
14453 : : }
14454 : : #endif
14455 : 1824 : {
14456 : : /* This of course will change! */
14457 : 1824 : time_t stampy;
14458 : 1824 : auto kind = cpp_get_date (reader, &stampy);
14459 : 1824 : if (kind != CPP_time_kind::UNKNOWN)
14460 : : {
14461 : 1824 : struct tm *time;
14462 : :
14463 : 1824 : time = gmtime (&stampy);
14464 : 1824 : readme.print_time ("build", time, "UTC");
14465 : :
14466 : 1824 : if (kind == CPP_time_kind::DYNAMIC)
14467 : : {
14468 : 1824 : time = localtime (&stampy);
14469 : 1824 : readme.print_time ("local", time,
14470 : : #if defined (__USE_MISC) || defined (__USE_BSD) /* Is there a better way? */
14471 : : time->tm_zone
14472 : : #else
14473 : : ""
14474 : : #endif
14475 : : );
14476 : : }
14477 : : }
14478 : : }
14479 : :
14480 : : /* Its direct imports. */
14481 : 2255 : for (unsigned ix = 1; ix < modules->length (); ix++)
14482 : : {
14483 : 431 : module_state *state = (*modules)[ix];
14484 : :
14485 : 431 : if (state->is_direct ())
14486 : 649 : readme.printf ("%s: %s %s", state->exported_p ? "export" : "import",
14487 : : state->get_flatname (), state->filename);
14488 : : }
14489 : :
14490 : 1824 : readme.end (to, to->name (MOD_SNAME_PFX ".README"), NULL);
14491 : 1824 : }
14492 : :
14493 : : /* Sort environment var names in reverse order. */
14494 : :
14495 : : static int
14496 : 0 : env_var_cmp (const void *a_, const void *b_)
14497 : : {
14498 : 0 : const unsigned char *a = *(const unsigned char *const *)a_;
14499 : 0 : const unsigned char *b = *(const unsigned char *const *)b_;
14500 : :
14501 : 0 : for (unsigned ix = 0; ; ix++)
14502 : : {
14503 : 0 : bool a_end = !a[ix] || a[ix] == '=';
14504 : 0 : if (a[ix] == b[ix])
14505 : : {
14506 : 0 : if (a_end)
14507 : : break;
14508 : : }
14509 : : else
14510 : : {
14511 : 0 : bool b_end = !b[ix] || b[ix] == '=';
14512 : :
14513 : 0 : if (!a_end && !b_end)
14514 : 0 : return a[ix] < b[ix] ? +1 : -1;
14515 : 0 : if (a_end && b_end)
14516 : : break;
14517 : 0 : return a_end ? +1 : -1;
14518 : : }
14519 : 0 : }
14520 : :
14521 : : return 0;
14522 : : }
14523 : :
14524 : : /* Write the environment. It is a STRTAB that may be extracted with:
14525 : : readelf -pgnu.c++.ENV $(module).gcm */
14526 : :
14527 : : void
14528 : 0 : module_state::write_env (elf_out *to)
14529 : : {
14530 : 0 : vec<const char *> vars;
14531 : 0 : vars.create (20);
14532 : :
14533 : 0 : extern char **environ;
14534 : 0 : while (const char *var = environ[vars.length ()])
14535 : 0 : vars.safe_push (var);
14536 : 0 : vars.qsort (env_var_cmp);
14537 : :
14538 : 0 : bytes_out env (to);
14539 : 0 : env.begin (false);
14540 : 0 : while (vars.length ())
14541 : 0 : env.printf ("%s", vars.pop ());
14542 : 0 : env.end (to, to->name (MOD_SNAME_PFX ".ENV"), NULL);
14543 : :
14544 : 0 : vars.release ();
14545 : 0 : }
14546 : :
14547 : : /* Write the direct or indirect imports.
14548 : : u:N
14549 : : {
14550 : : u:index
14551 : : s:name
14552 : : u32:crc
14553 : : s:filename (direct)
14554 : : u:exported (direct)
14555 : : } imports[N]
14556 : : */
14557 : :
14558 : : void
14559 : 624 : module_state::write_imports (bytes_out &sec, bool direct)
14560 : : {
14561 : 624 : unsigned count = 0;
14562 : :
14563 : 1312 : for (unsigned ix = 1; ix < modules->length (); ix++)
14564 : : {
14565 : 688 : module_state *imp = (*modules)[ix];
14566 : :
14567 : 688 : if (imp->remap && imp->is_direct () == direct)
14568 : 332 : count++;
14569 : : }
14570 : :
14571 : 624 : gcc_assert (!direct || count);
14572 : :
14573 : 624 : sec.u (count);
14574 : 1312 : for (unsigned ix = 1; ix < modules->length (); ix++)
14575 : : {
14576 : 688 : module_state *imp = (*modules)[ix];
14577 : :
14578 : 688 : if (imp->remap && imp->is_direct () == direct)
14579 : : {
14580 : 452 : dump () && dump ("Writing %simport:%u->%u %M (crc=%x)",
14581 : : !direct ? "indirect "
14582 : 60 : : imp->exported_p ? "exported " : "",
14583 : : ix, imp->remap, imp, imp->crc);
14584 : 332 : sec.u (imp->remap);
14585 : 332 : sec.str (imp->get_flatname ());
14586 : 332 : sec.u32 (imp->crc);
14587 : 332 : if (direct)
14588 : : {
14589 : 331 : write_location (sec, imp->imported_from ());
14590 : 331 : sec.str (imp->filename);
14591 : 331 : int exportedness = 0;
14592 : 331 : if (imp->exported_p)
14593 : : exportedness = +1;
14594 : 210 : else if (!imp->is_purview_direct ())
14595 : 12 : exportedness = -1;
14596 : 331 : sec.i (exportedness);
14597 : : }
14598 : : }
14599 : : }
14600 : 624 : }
14601 : :
14602 : : /* READER, LMAPS != NULL == direct imports,
14603 : : == NUL == indirect imports. */
14604 : :
14605 : : unsigned
14606 : 508 : module_state::read_imports (bytes_in &sec, cpp_reader *reader, line_maps *lmaps)
14607 : : {
14608 : 508 : unsigned count = sec.u ();
14609 : 508 : unsigned loaded = 0;
14610 : :
14611 : 1285 : while (count--)
14612 : : {
14613 : 269 : unsigned ix = sec.u ();
14614 : 269 : if (ix >= slurp->remap->length () || !ix || (*slurp->remap)[ix])
14615 : : {
14616 : 0 : sec.set_overrun ();
14617 : 0 : break;
14618 : : }
14619 : :
14620 : 269 : const char *name = sec.str (NULL);
14621 : 269 : module_state *imp = get_module (name);
14622 : 269 : unsigned crc = sec.u32 ();
14623 : 269 : int exportedness = 0;
14624 : :
14625 : : /* If the import is a partition, it must be the same primary
14626 : : module as this TU. */
14627 : 269 : if (imp && imp->is_partition () &&
14628 : : (!named_module_p ()
14629 : 108 : || (get_primary ((*modules)[0]) != get_primary (imp))))
14630 : : imp = NULL;
14631 : :
14632 : 269 : if (!imp)
14633 : 0 : sec.set_overrun ();
14634 : 269 : if (sec.get_overrun ())
14635 : : break;
14636 : :
14637 : 269 : if (lmaps)
14638 : : {
14639 : : /* A direct import, maybe load it. */
14640 : 269 : location_t floc = read_location (sec);
14641 : 269 : const char *fname = sec.str (NULL);
14642 : 269 : exportedness = sec.i ();
14643 : :
14644 : 269 : if (sec.get_overrun ())
14645 : : break;
14646 : :
14647 : 269 : if (!imp->check_not_purview (loc))
14648 : 3 : continue;
14649 : :
14650 : 266 : if (imp->loadedness == ML_NONE)
14651 : : {
14652 : 210 : imp->loc = floc;
14653 : 210 : imp->crc = crc;
14654 : 210 : if (!imp->get_flatname ())
14655 : 180 : imp->set_flatname ();
14656 : :
14657 : 210 : unsigned n = dump.push (imp);
14658 : :
14659 : 210 : if (!imp->filename && fname)
14660 : 177 : imp->filename = xstrdup (fname);
14661 : :
14662 : 210 : if (imp->is_partition ())
14663 : 27 : dump () && dump ("Importing elided partition %M", imp);
14664 : :
14665 : 210 : if (!imp->do_import (reader, false))
14666 : 3 : imp = NULL;
14667 : 210 : dump.pop (n);
14668 : 210 : if (!imp)
14669 : 3 : continue;
14670 : : }
14671 : :
14672 : 263 : if (is_partition ())
14673 : : {
14674 : 45 : if (!imp->is_direct ())
14675 : 21 : imp->directness = MD_PARTITION_DIRECT;
14676 : 45 : if (exportedness > 0)
14677 : 3 : imp->exported_p = true;
14678 : : }
14679 : : }
14680 : : else
14681 : : {
14682 : : /* An indirect import, find it, it should already be here. */
14683 : 0 : if (imp->loadedness == ML_NONE)
14684 : : {
14685 : 0 : error_at (loc, "indirect import %qs is not already loaded", name);
14686 : 0 : continue;
14687 : : }
14688 : : }
14689 : :
14690 : 263 : if (imp->crc != crc)
14691 : 0 : error_at (loc, "import %qs has CRC mismatch", imp->get_flatname ());
14692 : :
14693 : 263 : (*slurp->remap)[ix] = (imp->mod << 1) | (lmaps != NULL);
14694 : :
14695 : 263 : if (lmaps && exportedness >= 0)
14696 : 251 : set_import (imp, bool (exportedness));
14697 : 401 : dump () && dump ("Found %simport:%u %M->%u", !lmaps ? "indirect "
14698 : 69 : : exportedness > 0 ? "exported "
14699 : 36 : : exportedness < 0 ? "gmf" : "", ix, imp,
14700 : : imp->mod);
14701 : 263 : loaded++;
14702 : : }
14703 : :
14704 : 508 : return loaded;
14705 : : }
14706 : :
14707 : : /* Write the import table to MOD_SNAME_PFX.imp. */
14708 : :
14709 : : void
14710 : 312 : module_state::write_imports (elf_out *to, unsigned *crc_ptr)
14711 : : {
14712 : 369 : dump () && dump ("Writing imports");
14713 : 312 : dump.indent ();
14714 : :
14715 : 312 : bytes_out sec (to);
14716 : 312 : sec.begin ();
14717 : :
14718 : 312 : write_imports (sec, true);
14719 : 312 : write_imports (sec, false);
14720 : :
14721 : 312 : sec.end (to, to->name (MOD_SNAME_PFX ".imp"), crc_ptr);
14722 : 312 : dump.outdent ();
14723 : 312 : }
14724 : :
14725 : : bool
14726 : 254 : module_state::read_imports (cpp_reader *reader, line_maps *lmaps)
14727 : : {
14728 : 254 : bytes_in sec;
14729 : :
14730 : 254 : if (!sec.begin (loc, from (), MOD_SNAME_PFX ".imp"))
14731 : : return false;
14732 : :
14733 : 320 : dump () && dump ("Reading %u imports", slurp->remap->length () - 1);
14734 : 254 : dump.indent ();
14735 : :
14736 : : /* Read the imports. */
14737 : 254 : unsigned direct = read_imports (sec, reader, lmaps);
14738 : 254 : unsigned indirect = read_imports (sec, NULL, NULL);
14739 : 254 : if (direct + indirect + 1 != slurp->remap->length ())
14740 : 6 : from ()->set_error (elf::E_BAD_IMPORT);
14741 : :
14742 : 254 : dump.outdent ();
14743 : 254 : if (!sec.end (from ()))
14744 : : return false;
14745 : : return true;
14746 : 254 : }
14747 : :
14748 : : /* We're the primary module interface, but have partitions. Document
14749 : : them so that non-partition module implementation units know which
14750 : : have already been loaded. */
14751 : :
14752 : : void
14753 : 69 : module_state::write_partitions (elf_out *to, unsigned count, unsigned *crc_ptr)
14754 : : {
14755 : 87 : dump () && dump ("Writing %u elided partitions", count);
14756 : 69 : dump.indent ();
14757 : :
14758 : 69 : bytes_out sec (to);
14759 : 69 : sec.begin ();
14760 : :
14761 : 180 : for (unsigned ix = 1; ix != modules->length (); ix++)
14762 : : {
14763 : 111 : module_state *imp = (*modules)[ix];
14764 : 111 : if (imp->is_partition ())
14765 : : {
14766 : 129 : dump () && dump ("Writing elided partition %M (crc=%x)",
14767 : : imp, imp->crc);
14768 : 99 : sec.str (imp->get_flatname ());
14769 : 99 : sec.u32 (imp->crc);
14770 : 192 : write_location (sec, imp->is_direct ()
14771 : 93 : ? imp->imported_from () : UNKNOWN_LOCATION);
14772 : 99 : sec.str (imp->filename);
14773 : : }
14774 : : }
14775 : :
14776 : 69 : sec.end (to, to->name (MOD_SNAME_PFX ".prt"), crc_ptr);
14777 : 69 : dump.outdent ();
14778 : 69 : }
14779 : :
14780 : : bool
14781 : 12 : module_state::read_partitions (unsigned count)
14782 : : {
14783 : 12 : bytes_in sec;
14784 : 12 : if (!sec.begin (loc, from (), MOD_SNAME_PFX ".prt"))
14785 : : return false;
14786 : :
14787 : 15 : dump () && dump ("Reading %u elided partitions", count);
14788 : 12 : dump.indent ();
14789 : :
14790 : 30 : while (count--)
14791 : : {
14792 : 18 : const char *name = sec.str (NULL);
14793 : 18 : unsigned crc = sec.u32 ();
14794 : 18 : location_t floc = read_location (sec);
14795 : 18 : const char *fname = sec.str (NULL);
14796 : :
14797 : 18 : if (sec.get_overrun ())
14798 : : break;
14799 : :
14800 : 24 : dump () && dump ("Reading elided partition %s (crc=%x)", name, crc);
14801 : :
14802 : 18 : module_state *imp = get_module (name);
14803 : 18 : if (!imp /* Partition should be ... */
14804 : 18 : || !imp->is_partition () /* a partition ... */
14805 : 18 : || imp->loadedness != ML_NONE /* that is not yet loaded ... */
14806 : 36 : || get_primary (imp) != this) /* whose primary is this. */
14807 : : {
14808 : 0 : sec.set_overrun ();
14809 : 0 : break;
14810 : : }
14811 : :
14812 : 18 : if (!imp->has_location ())
14813 : 15 : imp->loc = floc;
14814 : 18 : imp->crc = crc;
14815 : 18 : if (!imp->filename && fname[0])
14816 : 15 : imp->filename = xstrdup (fname);
14817 : : }
14818 : :
14819 : 12 : dump.outdent ();
14820 : 12 : if (!sec.end (from ()))
14821 : : return false;
14822 : : return true;
14823 : 12 : }
14824 : :
14825 : : /* Data for config reading and writing. */
14826 : : struct module_state_config {
14827 : : const char *dialect_str;
14828 : : unsigned num_imports;
14829 : : unsigned num_partitions;
14830 : : unsigned num_entities;
14831 : : unsigned ordinary_locs;
14832 : : unsigned macro_locs;
14833 : : unsigned loc_range_bits;
14834 : : unsigned active_init;
14835 : :
14836 : : public:
14837 : 3923 : module_state_config ()
14838 : 3923 : :dialect_str (get_dialect ()),
14839 : 3923 : num_imports (0), num_partitions (0), num_entities (0),
14840 : 3923 : ordinary_locs (0), macro_locs (0), loc_range_bits (0),
14841 : 3923 : active_init (0)
14842 : : {
14843 : 3923 : }
14844 : :
14845 : 99146 : static void release ()
14846 : : {
14847 : 99146 : XDELETEVEC (dialect);
14848 : 99146 : dialect = NULL;
14849 : : }
14850 : :
14851 : : private:
14852 : : static const char *get_dialect ();
14853 : : static char *dialect;
14854 : : };
14855 : :
14856 : : char *module_state_config::dialect;
14857 : :
14858 : : /* Generate a string of the significant compilation options.
14859 : : Generally assume the user knows what they're doing, in the same way
14860 : : that object files can be mixed. */
14861 : :
14862 : : const char *
14863 : 3923 : module_state_config::get_dialect ()
14864 : : {
14865 : 3923 : if (!dialect)
14866 : 3136 : dialect = concat (get_cxx_dialect_name (cxx_dialect),
14867 : : /* C++ implies these, only show if disabled. */
14868 : 3136 : flag_exceptions ? "" : "/no-exceptions",
14869 : 3136 : flag_rtti ? "" : "/no-rtti",
14870 : 3136 : flag_new_inheriting_ctors ? "" : "/old-inheriting-ctors",
14871 : : /* C++ 20 implies concepts. */
14872 : 3136 : cxx_dialect < cxx20 && flag_concepts ? "/concepts" : "",
14873 : 3136 : flag_coroutines ? "/coroutines" : "",
14874 : 3136 : flag_module_implicit_inline ? "/implicit-inline" : "",
14875 : 3136 : flag_contracts ? "/contracts" : "",
14876 : : NULL);
14877 : :
14878 : 3923 : return dialect;
14879 : : }
14880 : :
14881 : : /* Contents of a cluster. */
14882 : : enum cluster_tag {
14883 : : ct_decl, /* A decl. */
14884 : : ct_defn, /* A definition. */
14885 : : ct_bind, /* A binding. */
14886 : : ct_hwm
14887 : : };
14888 : :
14889 : : /* Binding modifiers. */
14890 : : enum ct_bind_flags
14891 : : {
14892 : : cbf_export = 0x1, /* An exported decl. */
14893 : : cbf_hidden = 0x2, /* A hidden (friend) decl. */
14894 : : cbf_using = 0x4, /* A using decl. */
14895 : : cbf_wrapped = 0x8, /* ... that is wrapped. */
14896 : : };
14897 : :
14898 : : /* DEP belongs to a different cluster, seed it to prevent
14899 : : unfortunately timed duplicate import. */
14900 : : // FIXME: QOI For inter-cluster references we could just only pick
14901 : : // one entity from an earlier cluster. Even better track
14902 : : // dependencies between earlier clusters
14903 : :
14904 : : void
14905 : 8275297 : module_state::intercluster_seed (trees_out &sec, unsigned index_hwm, depset *dep)
14906 : : {
14907 : 8275297 : if (dep->is_import ()
14908 : 8275297 : || dep->cluster < index_hwm)
14909 : : {
14910 : 3587206 : tree ent = dep->get_entity ();
14911 : 3587206 : if (!TREE_VISITED (ent))
14912 : : {
14913 : 1586263 : sec.tree_node (ent);
14914 : 1586653 : dump (dumper::CLUSTER)
14915 : 390 : && dump ("Seeded %s %N",
14916 : 390 : dep->is_import () ? "import" : "intercluster", ent);
14917 : : }
14918 : : }
14919 : 8275297 : }
14920 : :
14921 : : /* Write the cluster of depsets in SCC[0-SIZE).
14922 : : dep->section -> section number
14923 : : dep->cluster -> entity number
14924 : : */
14925 : :
14926 : : unsigned
14927 : 358301 : module_state::write_cluster (elf_out *to, depset *scc[], unsigned size,
14928 : : depset::hash &table, unsigned *counts,
14929 : : unsigned *crc_ptr)
14930 : : {
14931 : 365599 : dump () && dump ("Writing section:%u %u depsets", table.section, size);
14932 : 358301 : dump.indent ();
14933 : :
14934 : 358301 : trees_out sec (to, this, table, table.section);
14935 : 358301 : sec.begin ();
14936 : 358301 : unsigned index_lwm = counts[MSC_entities];
14937 : :
14938 : : /* Determine entity numbers, mark for writing. */
14939 : 358583 : dump (dumper::CLUSTER) && dump ("Cluster members:") && (dump.indent (), true);
14940 : 1837756 : for (unsigned ix = 0; ix != size; ix++)
14941 : : {
14942 : 1479455 : depset *b = scc[ix];
14943 : :
14944 : 1479455 : switch (b->get_entity_kind ())
14945 : : {
14946 : 0 : default:
14947 : 0 : gcc_unreachable ();
14948 : :
14949 : 194392 : case depset::EK_BINDING:
14950 : 194392 : {
14951 : 194392 : dump (dumper::CLUSTER)
14952 : 195 : && dump ("[%u]=%s %P", ix, b->entity_kind_name (),
14953 : : b->get_entity (), b->get_name ());
14954 : 194392 : depset *ns_dep = b->deps[0];
14955 : 194392 : gcc_checking_assert (ns_dep->get_entity_kind ()
14956 : : == depset::EK_NAMESPACE
14957 : : && ns_dep->get_entity () == b->get_entity ());
14958 : 437696 : for (unsigned jx = b->deps.length (); --jx;)
14959 : : {
14960 : 243304 : depset *dep = b->deps[jx];
14961 : : // We could be declaring something that is also a
14962 : : // (merged) import
14963 : 270191 : gcc_checking_assert (dep->is_import ()
14964 : : || TREE_VISITED (dep->get_entity ())
14965 : : || (dep->get_entity_kind ()
14966 : : == depset::EK_USING));
14967 : : }
14968 : : }
14969 : : break;
14970 : :
14971 : 1258176 : case depset::EK_DECL:
14972 : 1258176 : case depset::EK_SPECIALIZATION:
14973 : 1258176 : case depset::EK_PARTIAL:
14974 : 1258176 : b->cluster = counts[MSC_entities]++;
14975 : 1258176 : sec.mark_declaration (b->get_entity (), b->has_defn ());
14976 : : /* FALLTHROUGH */
14977 : :
14978 : 1285063 : case depset::EK_USING:
14979 : 2570126 : gcc_checking_assert (!b->is_import ()
14980 : : && !b->is_unreached ());
14981 : 1508790 : dump (dumper::CLUSTER)
14982 : 1278 : && dump ("[%u]=%s %s %N", ix, b->entity_kind_name (),
14983 : 639 : b->has_defn () ? "definition" : "declaration",
14984 : : b->get_entity ());
14985 : : break;
14986 : : }
14987 : : }
14988 : 358583 : dump (dumper::CLUSTER) && (dump.outdent (), true);
14989 : :
14990 : : /* Ensure every out-of-cluster decl is referenced before we start
14991 : : streaming. We must do both imports *and* earlier clusters,
14992 : : because the latter could reach into the former and cause a
14993 : : duplicate loop. */
14994 : 358301 : sec.set_importing (+1);
14995 : 1837756 : for (unsigned ix = 0; ix != size; ix++)
14996 : : {
14997 : 1479455 : depset *b = scc[ix];
14998 : 17470523 : for (unsigned jx = b->is_special (); jx != b->deps.length (); jx++)
14999 : : {
15000 : 7259922 : depset *dep = b->deps[jx];
15001 : :
15002 : 7259922 : if (dep->is_binding ())
15003 : : {
15004 : 1501977 : for (unsigned ix = dep->deps.length (); --ix;)
15005 : : {
15006 : 1015375 : depset *bind = dep->deps[ix];
15007 : 1015375 : if (bind->get_entity_kind () == depset::EK_USING)
15008 : 58287 : bind = bind->deps[1];
15009 : :
15010 : 1015375 : intercluster_seed (sec, index_lwm, bind);
15011 : : }
15012 : : /* Also check the namespace itself. */
15013 : 243301 : dep = dep->deps[0];
15014 : : }
15015 : :
15016 : 7259922 : intercluster_seed (sec, index_lwm, dep);
15017 : : }
15018 : : }
15019 : 358301 : sec.tree_node (NULL_TREE);
15020 : : /* We're done importing now. */
15021 : 358301 : sec.set_importing (-1);
15022 : :
15023 : : /* Write non-definitions. */
15024 : 1837756 : for (unsigned ix = 0; ix != size; ix++)
15025 : : {
15026 : 1479455 : depset *b = scc[ix];
15027 : 1479455 : tree decl = b->get_entity ();
15028 : 1479455 : switch (b->get_entity_kind ())
15029 : : {
15030 : 0 : default:
15031 : 0 : gcc_unreachable ();
15032 : 194392 : break;
15033 : :
15034 : 194392 : case depset::EK_BINDING:
15035 : 194392 : {
15036 : 194392 : gcc_assert (TREE_CODE (decl) == NAMESPACE_DECL);
15037 : 195381 : dump () && dump ("Depset:%u binding %C:%P", ix, TREE_CODE (decl),
15038 : : decl, b->get_name ());
15039 : 194392 : sec.u (ct_bind);
15040 : 194392 : sec.tree_node (decl);
15041 : 194392 : sec.tree_node (b->get_name ());
15042 : :
15043 : : /* Write in reverse order, so reading will see the exports
15044 : : first, thus building the overload chain will be
15045 : : optimized. */
15046 : 632088 : for (unsigned jx = b->deps.length (); --jx;)
15047 : : {
15048 : 243304 : depset *dep = b->deps[jx];
15049 : 243304 : tree bound = dep->get_entity ();
15050 : 243304 : unsigned flags = 0;
15051 : 243304 : if (dep->get_entity_kind () == depset::EK_USING)
15052 : : {
15053 : 26887 : tree ovl = bound;
15054 : 26887 : bound = OVL_FUNCTION (bound);
15055 : 26887 : if (!(TREE_CODE (bound) == CONST_DECL
15056 : 9992 : && UNSCOPED_ENUM_P (TREE_TYPE (bound))
15057 : 9992 : && decl == TYPE_NAME (TREE_TYPE (bound))))
15058 : : {
15059 : : /* An unscope enumerator in its enumeration's
15060 : : scope is not a using. */
15061 : 26887 : flags |= cbf_using;
15062 : 26887 : if (OVL_USING_P (ovl))
15063 : 0 : flags |= cbf_wrapped;
15064 : : }
15065 : 26887 : if (OVL_EXPORT_P (ovl))
15066 : 26878 : flags |= cbf_export;
15067 : : }
15068 : : else
15069 : : {
15070 : : /* An implicit typedef must be at one. */
15071 : 216417 : gcc_assert (!DECL_IMPLICIT_TYPEDEF_P (bound) || jx == 1);
15072 : 216417 : if (dep->is_hidden ())
15073 : : flags |= cbf_hidden;
15074 : 211014 : else if (DECL_MODULE_EXPORT_P (STRIP_TEMPLATE (bound)))
15075 : 210400 : flags |= cbf_export;
15076 : : }
15077 : :
15078 : 243304 : gcc_checking_assert (DECL_P (bound));
15079 : :
15080 : 243304 : sec.i (flags);
15081 : 243304 : sec.tree_node (bound);
15082 : : }
15083 : :
15084 : : /* Terminate the list. */
15085 : 194392 : sec.i (-1);
15086 : : }
15087 : 194392 : break;
15088 : :
15089 : 26887 : case depset::EK_USING:
15090 : 26908 : dump () && dump ("Depset:%u %s %C:%N", ix, b->entity_kind_name (),
15091 : 21 : TREE_CODE (decl), decl);
15092 : : break;
15093 : :
15094 : 1258176 : case depset::EK_SPECIALIZATION:
15095 : 1258176 : case depset::EK_PARTIAL:
15096 : 1258176 : case depset::EK_DECL:
15097 : 1287490 : dump () && dump ("Depset:%u %s entity:%u %C:%N", ix,
15098 : : b->entity_kind_name (), b->cluster,
15099 : 29314 : TREE_CODE (decl), decl);
15100 : :
15101 : 1258176 : sec.u (ct_decl);
15102 : 1258176 : sec.tree_node (decl);
15103 : :
15104 : 1508769 : dump () && dump ("Wrote declaration entity:%u %C:%N",
15105 : 29314 : b->cluster, TREE_CODE (decl), decl);
15106 : : break;
15107 : : }
15108 : : }
15109 : :
15110 : : depset *namer = NULL;
15111 : :
15112 : : /* Write out definitions */
15113 : 1837756 : for (unsigned ix = 0; ix != size; ix++)
15114 : : {
15115 : 1479455 : depset *b = scc[ix];
15116 : 1479455 : tree decl = b->get_entity ();
15117 : 1479455 : switch (b->get_entity_kind ())
15118 : : {
15119 : : default:
15120 : : break;
15121 : :
15122 : 1258176 : case depset::EK_SPECIALIZATION:
15123 : 1258176 : case depset::EK_PARTIAL:
15124 : 1258176 : case depset::EK_DECL:
15125 : 1258176 : if (!namer)
15126 : 344854 : namer = b;
15127 : :
15128 : 1258176 : if (b->has_defn ())
15129 : : {
15130 : 456659 : sec.u (ct_defn);
15131 : 456659 : sec.tree_node (decl);
15132 : 467765 : dump () && dump ("Writing definition %N", decl);
15133 : 456659 : sec.write_definition (decl);
15134 : :
15135 : 456659 : if (!namer->has_defn ())
15136 : 1479455 : namer = b;
15137 : : }
15138 : : break;
15139 : : }
15140 : : }
15141 : :
15142 : : /* We don't find the section by name. Use depset's decl's name for
15143 : : human friendliness. */
15144 : 358301 : unsigned name = 0;
15145 : 358301 : tree naming_decl = NULL_TREE;
15146 : 358301 : if (namer)
15147 : : {
15148 : 344854 : naming_decl = namer->get_entity ();
15149 : 344854 : if (namer->get_entity_kind () == depset::EK_USING)
15150 : : /* This unfortunately names the section from the target of the
15151 : : using decl. But the name is only a guide, so Do Not Care. */
15152 : 0 : naming_decl = OVL_FUNCTION (naming_decl);
15153 : 344854 : if (DECL_IMPLICIT_TYPEDEF_P (naming_decl))
15154 : : /* Lose any anonymousness. */
15155 : 116768 : naming_decl = TYPE_NAME (TREE_TYPE (naming_decl));
15156 : 344854 : name = to->qualified_name (naming_decl, namer->has_defn ());
15157 : : }
15158 : :
15159 : 358301 : unsigned bytes = sec.pos;
15160 : 358301 : unsigned snum = sec.end (to, name, crc_ptr);
15161 : :
15162 : 1837756 : for (unsigned ix = size; ix--;)
15163 : 1479455 : gcc_checking_assert (scc[ix]->section == snum);
15164 : :
15165 : 358301 : dump.outdent ();
15166 : 365599 : dump () && dump ("Wrote section:%u named-by:%N", table.section, naming_decl);
15167 : :
15168 : 358301 : return bytes;
15169 : 358301 : }
15170 : :
15171 : : /* Read a cluster from section SNUM. */
15172 : :
15173 : : bool
15174 : 358539 : module_state::read_cluster (unsigned snum)
15175 : : {
15176 : 358539 : trees_in sec (this);
15177 : :
15178 : 358539 : if (!sec.begin (loc, from (), snum))
15179 : : return false;
15180 : :
15181 : 359585 : dump () && dump ("Reading section:%u", snum);
15182 : 358539 : dump.indent ();
15183 : :
15184 : : /* We care about structural equality. */
15185 : 358539 : comparing_dependent_aliases++;
15186 : :
15187 : : /* First seed the imports. */
15188 : 1981102 : while (tree import = sec.tree_node ())
15189 : 3603830 : dump (dumper::CLUSTER) && dump ("Seeded import %N", import);
15190 : :
15191 : 2381514 : while (!sec.get_overrun () && sec.more_p ())
15192 : : {
15193 : 2022975 : unsigned ct = sec.u ();
15194 : 2022975 : switch (ct)
15195 : : {
15196 : 0 : default:
15197 : 0 : sec.set_overrun ();
15198 : 0 : break;
15199 : :
15200 : 204839 : case ct_bind:
15201 : : /* A set of namespace bindings. */
15202 : 204839 : {
15203 : 204839 : tree ns = sec.tree_node ();
15204 : 204839 : tree name = sec.tree_node ();
15205 : 204839 : tree decls = NULL_TREE;
15206 : 204839 : tree visible = NULL_TREE;
15207 : 204839 : tree type = NULL_TREE;
15208 : 204839 : bool dedup = false;
15209 : :
15210 : : /* We rely on the bindings being in the reverse order of
15211 : : the resulting overload set. */
15212 : 464678 : for (;;)
15213 : : {
15214 : 464678 : int flags = sec.i ();
15215 : 464678 : if (flags < 0)
15216 : : break;
15217 : :
15218 : 259839 : if ((flags & cbf_hidden)
15219 : 6049 : && (flags & (cbf_using | cbf_export)))
15220 : 0 : sec.set_overrun ();
15221 : :
15222 : 259839 : tree decl = sec.tree_node ();
15223 : 259839 : if (sec.get_overrun ())
15224 : : break;
15225 : :
15226 : 259839 : if (decls && TREE_CODE (decl) == TYPE_DECL)
15227 : : {
15228 : : /* Stat hack. */
15229 : 82 : if (type || !DECL_IMPLICIT_TYPEDEF_P (decl))
15230 : 0 : sec.set_overrun ();
15231 : : type = decl;
15232 : : }
15233 : : else
15234 : : {
15235 : : if (decls
15236 : 204839 : || (flags & (cbf_hidden | cbf_wrapped))
15237 : 276537 : || DECL_FUNCTION_TEMPLATE_P (decl))
15238 : : {
15239 : 82068 : decls = ovl_make (decl, decls);
15240 : 82068 : if (flags & cbf_using)
15241 : : {
15242 : 4060 : dedup = true;
15243 : 4060 : OVL_USING_P (decls) = true;
15244 : 4060 : if (flags & cbf_export)
15245 : 4060 : OVL_EXPORT_P (decls) = true;
15246 : : }
15247 : :
15248 : 82068 : if (flags & cbf_hidden)
15249 : 6049 : OVL_HIDDEN_P (decls) = true;
15250 : 76019 : else if (dedup)
15251 : 4060 : OVL_DEDUP_P (decls) = true;
15252 : : }
15253 : : else
15254 : : decls = decl;
15255 : :
15256 : 259757 : if (flags & cbf_export
15257 : 259757 : || (!(flags & cbf_hidden)
15258 : 451 : && (is_module () || is_partition ())))
15259 : : visible = decls;
15260 : : }
15261 : : }
15262 : :
15263 : 204839 : if (!decls)
15264 : 0 : sec.set_overrun ();
15265 : :
15266 : 204839 : if (sec.get_overrun ())
15267 : : break; /* Bail. */
15268 : :
15269 : 205614 : dump () && dump ("Binding of %P", ns, name);
15270 : 204839 : if (!set_module_binding (ns, name, mod,
15271 : 204839 : is_header () ? -1
15272 : 1751 : : is_module () || is_partition () ? 1
15273 : : : 0,
15274 : : decls, type, visible))
15275 : 0 : sec.set_overrun ();
15276 : : }
15277 : : break;
15278 : :
15279 : 1333093 : case ct_decl:
15280 : : /* A decl. */
15281 : 1333093 : {
15282 : 1333093 : tree decl = sec.tree_node ();
15283 : 3717301 : dump () && dump ("Read declaration of %N", decl);
15284 : : }
15285 : : break;
15286 : :
15287 : 485043 : case ct_defn:
15288 : 485043 : {
15289 : 485043 : tree decl = sec.tree_node ();
15290 : 486019 : dump () && dump ("Reading definition of %N", decl);
15291 : 485043 : sec.read_definition (decl);
15292 : : }
15293 : 485043 : break;
15294 : : }
15295 : : }
15296 : :
15297 : : /* When lazy loading is in effect, we can be in the middle of
15298 : : parsing or instantiating a function. Save it away.
15299 : : push_function_context does too much work. */
15300 : 358539 : tree old_cfd = current_function_decl;
15301 : 358539 : struct function *old_cfun = cfun;
15302 : 551478 : for (const post_process_data& pdata : sec.post_process ())
15303 : : {
15304 : 135521 : tree decl = pdata.decl;
15305 : :
15306 : 135521 : bool abstract = false;
15307 : 135521 : if (TREE_CODE (decl) == TEMPLATE_DECL)
15308 : : {
15309 : 75345 : abstract = true;
15310 : 75345 : decl = DECL_TEMPLATE_RESULT (decl);
15311 : : }
15312 : :
15313 : 135521 : current_function_decl = decl;
15314 : 135521 : allocate_struct_function (decl, abstract);
15315 : 135521 : cfun->language = ggc_cleared_alloc<language_function> ();
15316 : 135521 : cfun->language->base.x_stmt_tree.stmts_are_full_exprs_p = 1;
15317 : 135521 : cfun->function_start_locus = pdata.start_locus;
15318 : 135521 : cfun->function_end_locus = pdata.end_locus;
15319 : :
15320 : 135521 : if (abstract)
15321 : : ;
15322 : 60176 : else if (DECL_ABSTRACT_P (decl))
15323 : 10654 : vec_safe_push (post_load_decls, decl);
15324 : : else
15325 : : {
15326 : 49522 : bool aggr = aggregate_value_p (DECL_RESULT (decl), decl);
15327 : : #ifdef PCC_STATIC_STRUCT_RETURN
15328 : : cfun->returns_pcc_struct = aggr;
15329 : : #endif
15330 : 49522 : cfun->returns_struct = aggr;
15331 : :
15332 : 49522 : if (DECL_COMDAT (decl))
15333 : : // FIXME: Comdat grouping?
15334 : 48924 : comdat_linkage (decl);
15335 : 49522 : note_vague_linkage_fn (decl);
15336 : 49522 : cgraph_node::finalize_function (decl, true);
15337 : : }
15338 : :
15339 : : }
15340 : : /* Look, function.cc's interface to cfun does too much for us, we
15341 : : just need to restore the old value. I do not want to go
15342 : : redesigning that API right now. */
15343 : : #undef cfun
15344 : 358539 : cfun = old_cfun;
15345 : 358539 : current_function_decl = old_cfd;
15346 : 358539 : comparing_dependent_aliases--;
15347 : :
15348 : 358539 : dump.outdent ();
15349 : 359585 : dump () && dump ("Read section:%u", snum);
15350 : :
15351 : 358539 : loaded_clusters++;
15352 : :
15353 : 358539 : if (!sec.end (from ()))
15354 : : return false;
15355 : :
15356 : : return true;
15357 : 358539 : }
15358 : :
15359 : : void
15360 : 196766 : module_state::write_namespace (bytes_out &sec, depset *dep)
15361 : : {
15362 : 196766 : unsigned ns_num = dep->cluster;
15363 : 196766 : unsigned ns_import = 0;
15364 : :
15365 : 196766 : if (dep->is_import ())
15366 : 0 : ns_import = dep->section;
15367 : 196766 : else if (dep->get_entity () != global_namespace)
15368 : 116416 : ns_num++;
15369 : :
15370 : 196766 : sec.u (ns_import);
15371 : 196766 : sec.u (ns_num);
15372 : 196766 : }
15373 : :
15374 : : tree
15375 : 268612 : module_state::read_namespace (bytes_in &sec)
15376 : : {
15377 : 268612 : unsigned ns_import = sec.u ();
15378 : 268612 : unsigned ns_num = sec.u ();
15379 : 268612 : tree ns = NULL_TREE;
15380 : :
15381 : 268612 : if (ns_import || ns_num)
15382 : : {
15383 : 155980 : if (!ns_import)
15384 : 155980 : ns_num--;
15385 : :
15386 : 155980 : if (unsigned origin = slurp->remap_module (ns_import))
15387 : : {
15388 : 155980 : module_state *from = (*modules)[origin];
15389 : 155980 : if (ns_num < from->entity_num)
15390 : : {
15391 : 155980 : binding_slot &slot = (*entity_ary)[from->entity_lwm + ns_num];
15392 : :
15393 : 155980 : if (!slot.is_lazy ())
15394 : 155980 : ns = slot;
15395 : : }
15396 : : }
15397 : : else
15398 : 0 : sec.set_overrun ();
15399 : : }
15400 : : else
15401 : 112632 : ns = global_namespace;
15402 : :
15403 : 268612 : return ns;
15404 : : }
15405 : :
15406 : : /* SPACES is a sorted vector of namespaces. Write out the namespaces
15407 : : to MOD_SNAME_PFX.nms section. */
15408 : :
15409 : : void
15410 : 342 : module_state::write_namespaces (elf_out *to, vec<depset *> spaces,
15411 : : unsigned num, unsigned *crc_p)
15412 : : {
15413 : 375 : dump () && dump ("Writing namespaces");
15414 : 342 : dump.indent ();
15415 : :
15416 : 342 : bytes_out sec (to);
15417 : 342 : sec.begin ();
15418 : :
15419 : 2716 : for (unsigned ix = 0; ix != num; ix++)
15420 : : {
15421 : 2374 : depset *b = spaces[ix];
15422 : 2374 : tree ns = b->get_entity ();
15423 : :
15424 : 2374 : gcc_checking_assert (TREE_CODE (ns) == NAMESPACE_DECL);
15425 : : /* P1815 may have something to say about this. */
15426 : 2374 : gcc_checking_assert (TREE_PUBLIC (ns));
15427 : :
15428 : 2374 : unsigned flags = 0;
15429 : 2374 : if (TREE_PUBLIC (ns))
15430 : 2374 : flags |= 1;
15431 : 2374 : if (DECL_NAMESPACE_INLINE_P (ns))
15432 : 500 : flags |= 2;
15433 : 2374 : if (DECL_MODULE_PURVIEW_P (ns))
15434 : 2014 : flags |= 4;
15435 : 2374 : if (DECL_MODULE_EXPORT_P (ns))
15436 : 1953 : flags |= 8;
15437 : :
15438 : 2488 : dump () && dump ("Writing namespace:%u %N%s%s%s%s",
15439 : : b->cluster, ns,
15440 : : flags & 1 ? ", public" : "",
15441 : 114 : flags & 2 ? ", inline" : "",
15442 : 114 : flags & 4 ? ", purview" : "",
15443 : 114 : flags & 8 ? ", export" : "");
15444 : 2374 : sec.u (b->cluster);
15445 : 2374 : sec.u (to->name (DECL_NAME (ns)));
15446 : 2374 : write_namespace (sec, b->deps[0]);
15447 : :
15448 : 2374 : sec.u (flags);
15449 : 2374 : write_location (sec, DECL_SOURCE_LOCATION (ns));
15450 : :
15451 : 2374 : if (DECL_NAMESPACE_INLINE_P (ns))
15452 : : {
15453 : 500 : if (tree attr = lookup_attribute ("abi_tag", DECL_ATTRIBUTES (ns)))
15454 : : {
15455 : 139 : tree tags = TREE_VALUE (attr);
15456 : 139 : sec.u (list_length (tags));
15457 : 281 : for (tree tag = tags; tag; tag = TREE_CHAIN (tag))
15458 : 142 : sec.str (TREE_STRING_POINTER (TREE_VALUE (tag)));
15459 : : }
15460 : : else
15461 : 361 : sec.u (0);
15462 : : }
15463 : : }
15464 : :
15465 : 342 : sec.end (to, to->name (MOD_SNAME_PFX ".nms"), crc_p);
15466 : 342 : dump.outdent ();
15467 : 342 : }
15468 : :
15469 : : /* Read the namespace hierarchy from MOD_SNAME_PFX.namespace. Fill in
15470 : : SPACES from that data. */
15471 : :
15472 : : bool
15473 : 348 : module_state::read_namespaces (unsigned num)
15474 : : {
15475 : 348 : bytes_in sec;
15476 : :
15477 : 348 : if (!sec.begin (loc, from (), MOD_SNAME_PFX ".nms"))
15478 : : return false;
15479 : :
15480 : 393 : dump () && dump ("Reading namespaces");
15481 : 348 : dump.indent ();
15482 : :
15483 : 3328 : for (unsigned ix = 0; ix != num; ix++)
15484 : : {
15485 : 2980 : unsigned entity_index = sec.u ();
15486 : 2980 : unsigned name = sec.u ();
15487 : :
15488 : 2980 : tree parent = read_namespace (sec);
15489 : :
15490 : : /* See comment in write_namespace about why not bits. */
15491 : 2980 : unsigned flags = sec.u ();
15492 : 2980 : location_t src_loc = read_location (sec);
15493 : 2980 : unsigned tags_count = (flags & 2) ? sec.u () : 0;
15494 : :
15495 : 2980 : if (entity_index >= entity_num
15496 : 2980 : || !parent
15497 : 2980 : || (flags & 0xc) == 0x8)
15498 : 0 : sec.set_overrun ();
15499 : :
15500 : : tree tags = NULL_TREE;
15501 : 3153 : while (tags_count--)
15502 : : {
15503 : 173 : size_t len;
15504 : 173 : const char *str = sec.str (&len);
15505 : 173 : tags = tree_cons (NULL_TREE, build_string (len + 1, str), tags);
15506 : 173 : tags = nreverse (tags);
15507 : : }
15508 : :
15509 : 2980 : if (sec.get_overrun ())
15510 : : break;
15511 : :
15512 : 5960 : tree id = name ? get_identifier (from ()->name (name)) : NULL_TREE;
15513 : :
15514 : 3094 : dump () && dump ("Read namespace:%u %P%s%s%s%s",
15515 : : entity_index, parent, id,
15516 : 57 : flags & 1 ? ", public" : "",
15517 : : flags & 2 ? ", inline" : "",
15518 : 57 : flags & 4 ? ", purview" : "",
15519 : 57 : flags & 8 ? ", export" : "");
15520 : 2980 : bool visible_p = ((flags & 8)
15521 : 2980 : || ((flags & 1)
15522 : 467 : && (flags & 4)
15523 : 43 : && (is_partition () || is_module ())));
15524 : 2980 : tree inner = add_imported_namespace (parent, id, src_loc, mod,
15525 : : bool (flags & 2), visible_p);
15526 : 2980 : if (!inner)
15527 : : {
15528 : 0 : sec.set_overrun ();
15529 : 0 : break;
15530 : : }
15531 : :
15532 : 2980 : if (is_partition ())
15533 : : {
15534 : 15 : if (flags & 4)
15535 : 15 : DECL_MODULE_PURVIEW_P (inner) = true;
15536 : 15 : if (flags & 8)
15537 : 15 : DECL_MODULE_EXPORT_P (inner) = true;
15538 : : }
15539 : :
15540 : 2980 : if (tags)
15541 : 170 : DECL_ATTRIBUTES (inner)
15542 : 340 : = tree_cons (get_identifier ("abi_tag"), tags, DECL_ATTRIBUTES (inner));
15543 : :
15544 : : /* Install the namespace. */
15545 : 2980 : (*entity_ary)[entity_lwm + entity_index] = inner;
15546 : 2980 : if (DECL_MODULE_IMPORT_P (inner))
15547 : : {
15548 : 0 : bool existed;
15549 : 0 : unsigned *slot = &entity_map->get_or_insert
15550 : 0 : (DECL_UID (inner), &existed);
15551 : 0 : if (existed)
15552 : : /* If it existed, it should match. */
15553 : 0 : gcc_checking_assert (inner == (*entity_ary)[*slot]);
15554 : : else
15555 : 0 : *slot = entity_lwm + entity_index;
15556 : : }
15557 : : }
15558 : 348 : dump.outdent ();
15559 : 348 : if (!sec.end (from ()))
15560 : : return false;
15561 : : return true;
15562 : 348 : }
15563 : :
15564 : : /* Write the binding TABLE to MOD_SNAME_PFX.bnd */
15565 : :
15566 : : unsigned
15567 : 1818 : module_state::write_bindings (elf_out *to, vec<depset *> sccs, unsigned *crc_p)
15568 : : {
15569 : 2046 : dump () && dump ("Writing binding table");
15570 : 1818 : dump.indent ();
15571 : :
15572 : 1818 : unsigned num = 0;
15573 : 1818 : bytes_out sec (to);
15574 : 1818 : sec.begin ();
15575 : :
15576 : 2970556 : for (unsigned ix = 0; ix != sccs.length (); ix++)
15577 : : {
15578 : 1483460 : depset *b = sccs[ix];
15579 : 1483460 : if (b->is_binding ())
15580 : : {
15581 : 194392 : tree ns = b->get_entity ();
15582 : 195381 : dump () && dump ("Bindings %P section:%u", ns, b->get_name (),
15583 : : b->section);
15584 : 194392 : sec.u (to->name (b->get_name ()));
15585 : 194392 : write_namespace (sec, b->deps[0]);
15586 : 194392 : sec.u (b->section);
15587 : 194392 : num++;
15588 : : }
15589 : : }
15590 : :
15591 : 1818 : sec.end (to, to->name (MOD_SNAME_PFX ".bnd"), crc_p);
15592 : 1818 : dump.outdent ();
15593 : :
15594 : 1818 : return num;
15595 : 1818 : }
15596 : :
15597 : : /* Read the binding table from MOD_SNAME_PFX.bind. */
15598 : :
15599 : : bool
15600 : 1980 : module_state::read_bindings (unsigned num, unsigned lwm, unsigned hwm)
15601 : : {
15602 : 1980 : bytes_in sec;
15603 : :
15604 : 1980 : if (!sec.begin (loc, from (), MOD_SNAME_PFX ".bnd"))
15605 : : return false;
15606 : :
15607 : 2426 : dump () && dump ("Reading binding table");
15608 : 1980 : dump.indent ();
15609 : 267612 : for (; !sec.get_overrun () && num--;)
15610 : : {
15611 : 265632 : const char *name = from ()->name (sec.u ());
15612 : 265632 : tree ns = read_namespace (sec);
15613 : 265632 : unsigned snum = sec.u ();
15614 : :
15615 : 265632 : if (!ns || !name || (snum - lwm) >= (hwm - lwm))
15616 : 0 : sec.set_overrun ();
15617 : 265632 : if (!sec.get_overrun ())
15618 : : {
15619 : 265632 : tree id = get_identifier (name);
15620 : 267010 : dump () && dump ("Bindings %P section:%u", ns, id, snum);
15621 : 265632 : if (mod && !import_module_binding (ns, id, mod, snum))
15622 : : break;
15623 : : }
15624 : : }
15625 : :
15626 : 1980 : dump.outdent ();
15627 : 1980 : if (!sec.end (from ()))
15628 : : return false;
15629 : : return true;
15630 : 1980 : }
15631 : :
15632 : : /* Write the entity table to MOD_SNAME_PFX.ent
15633 : :
15634 : : Each entry is a section number. */
15635 : :
15636 : : void
15637 : 1631 : module_state::write_entities (elf_out *to, vec<depset *> depsets,
15638 : : unsigned count, unsigned *crc_p)
15639 : : {
15640 : 1820 : dump () && dump ("Writing entities");
15641 : 1631 : dump.indent ();
15642 : :
15643 : 1631 : bytes_out sec (to);
15644 : 1631 : sec.begin ();
15645 : :
15646 : 1631 : unsigned current = 0;
15647 : 2970182 : for (unsigned ix = 0; ix < depsets.length (); ix++)
15648 : : {
15649 : 1483460 : depset *d = depsets[ix];
15650 : :
15651 : 2745641 : switch (d->get_entity_kind ())
15652 : : {
15653 : : default:
15654 : : break;
15655 : :
15656 : 4005 : case depset::EK_NAMESPACE:
15657 : 4005 : if (!d->is_import () && d->get_entity () != global_namespace)
15658 : : {
15659 : 2374 : gcc_checking_assert (d->cluster == current);
15660 : 2374 : current++;
15661 : 2374 : sec.u (0);
15662 : : }
15663 : : break;
15664 : :
15665 : 1258176 : case depset::EK_DECL:
15666 : 1258176 : case depset::EK_SPECIALIZATION:
15667 : 1258176 : case depset::EK_PARTIAL:
15668 : 2516352 : gcc_checking_assert (!d->is_unreached ()
15669 : : && !d->is_import ()
15670 : : && d->cluster == current
15671 : : && d->section);
15672 : 1258176 : current++;
15673 : 1258176 : sec.u (d->section);
15674 : 1258176 : break;
15675 : : }
15676 : : }
15677 : 1631 : gcc_assert (count == current);
15678 : 1631 : sec.end (to, to->name (MOD_SNAME_PFX ".ent"), crc_p);
15679 : 1631 : dump.outdent ();
15680 : 1631 : }
15681 : :
15682 : : bool
15683 : 1788 : module_state::read_entities (unsigned count, unsigned lwm, unsigned hwm)
15684 : : {
15685 : 1788 : trees_in sec (this);
15686 : :
15687 : 1788 : if (!sec.begin (loc, from (), MOD_SNAME_PFX ".ent"))
15688 : : return false;
15689 : :
15690 : 2186 : dump () && dump ("Reading entities");
15691 : 1788 : dump.indent ();
15692 : :
15693 : 1695594 : for (binding_slot *slot = entity_ary->begin () + entity_lwm; count--; slot++)
15694 : : {
15695 : 1693806 : unsigned snum = sec.u ();
15696 : 1693806 : if (snum && (snum - lwm) >= (hwm - lwm))
15697 : 0 : sec.set_overrun ();
15698 : 1693806 : if (sec.get_overrun ())
15699 : : break;
15700 : :
15701 : 1693806 : if (snum)
15702 : 1690826 : slot->set_lazy (snum << 2);
15703 : : }
15704 : :
15705 : 1788 : dump.outdent ();
15706 : 1788 : if (!sec.end (from ()))
15707 : : return false;
15708 : : return true;
15709 : 1788 : }
15710 : :
15711 : : /* Write the pending table to MOD_SNAME_PFX.pnd
15712 : :
15713 : : The pending table holds information about clusters that need to be
15714 : : loaded because they contain information about something that is not
15715 : : found by namespace-scope lookup.
15716 : :
15717 : : The three cases are:
15718 : :
15719 : : (a) Template (maybe-partial) specializations that we have
15720 : : instantiated or defined. When an importer needs to instantiate
15721 : : that template, they /must have/ the partial, explicit & extern
15722 : : specializations available. If they have the other specializations
15723 : : available, they'll have less work to do. Thus, when we're about to
15724 : : instantiate FOO, we have to be able to ask 'are there any
15725 : : specialization of FOO in our imports?'.
15726 : :
15727 : : (b) (Maybe-implicit) member functions definitions. A class could
15728 : : be defined in one header, and an inline member defined in a
15729 : : different header (this occurs in the STL). Similarly, like the
15730 : : specialization case, an implicit member function could have been
15731 : : 'instantiated' in one module, and it'd be nice to not have to
15732 : : reinstantiate it in another.
15733 : :
15734 : : (c) A member classes completed elsewhere. A member class could be
15735 : : declared in one header and defined in another. We need to know to
15736 : : load the class definition before looking in it. This turns out to
15737 : : be a specific case of #b, so we can treat these the same. But it
15738 : : does highlight an issue -- there could be an intermediate import
15739 : : between the outermost containing namespace-scope class and the
15740 : : innermost being-defined member class. This is actually possible
15741 : : with all of these cases, so be aware -- we're not just talking of
15742 : : one level of import to get to the innermost namespace.
15743 : :
15744 : : This gets complicated fast, it took me multiple attempts to even
15745 : : get something remotely working. Partially because I focussed on
15746 : : optimizing what I think turns out to be a smaller problem, given
15747 : : the known need to do the more general case *anyway*. I document
15748 : : the smaller problem, because it does appear to be the natural way
15749 : : to do it. It's trap!
15750 : :
15751 : : **** THE TRAP
15752 : :
15753 : : Let's refer to the primary template or the containing class as the
15754 : : KEY. And the specialization or member as the PENDING-ENTITY. (To
15755 : : avoid having to say those mouthfuls all the time.)
15756 : :
15757 : : In either case, we have an entity and we need some way of mapping
15758 : : that to a set of entities that need to be loaded before we can
15759 : : proceed with whatever processing of the entity we were going to do.
15760 : :
15761 : : We need to link the key to the pending-entity in some way. Given a
15762 : : key, tell me the pending-entities I need to have loaded. However
15763 : : we tie the key to the pending-entity must not rely on the key being
15764 : : loaded -- that'd defeat the lazy loading scheme.
15765 : :
15766 : : As the key will be an import in we know its entity number (either
15767 : : because we imported it, or we're writing it out too). Thus we can
15768 : : generate a map of key-indices to pending-entities. The
15769 : : pending-entity indices will be into our span of the entity table,
15770 : : and thus allow them to be lazily loaded. The key index will be
15771 : : into another slot of the entity table. Notice that this checking
15772 : : could be expensive, we don't want to iterate over a bunch of
15773 : : pending-entity indices (across multiple imports), every time we're
15774 : : about do to the thing with the key. We need to quickly determine
15775 : : 'definitely nothing needed'.
15776 : :
15777 : : That's almost good enough, except that key indices are not unique
15778 : : in a couple of cases :( Specifically the Global Module or a module
15779 : : partition can result in multiple modules assigning an entity index
15780 : : for the key. The decl-merging on loading will detect that so we
15781 : : only have one Key loaded, and in the entity hash it'll indicate the
15782 : : entity index of first load. Which might be different to how we
15783 : : know it. Notice this is restricted to GM entities or this-module
15784 : : entities. Foreign imports cannot have this.
15785 : :
15786 : : We can simply resolve this in the direction of how this module
15787 : : referred to the key to how the importer knows it. Look in the
15788 : : entity table slot that we nominate, maybe lazy load it, and then
15789 : : lookup the resultant entity in the entity hash to learn how the
15790 : : importer knows it.
15791 : :
15792 : : But we need to go in the other direction :( Given the key, find all
15793 : : the index-aliases of that key. We can partially solve that by
15794 : : adding an alias hash table. Whenever we load a merged decl, add or
15795 : : augment a mapping from the entity (or its entity-index) to the
15796 : : newly-discovered index. Then when we look for pending entities of
15797 : : a key, we also iterate over this aliases this mapping provides.
15798 : :
15799 : : But that requires the alias to be loaded. And that's not
15800 : : necessarily true.
15801 : :
15802 : : *** THE SIMPLER WAY
15803 : :
15804 : : The remaining fixed thing we have is the innermost namespace
15805 : : containing the ultimate namespace-scope container of the key and
15806 : : the name of that container (which might be the key itself). I.e. a
15807 : : namespace-decl/identifier/module tuple. Let's call this the
15808 : : top-key. We'll discover that the module is not important here,
15809 : : because of cross-module possibilities mentioned in case #c above.
15810 : : We can't markup namespace-binding slots. The best we can do is
15811 : : mark the binding vector with 'there's something here', and have
15812 : : another map from namespace/identifier pairs to a vector of pending
15813 : : entity indices.
15814 : :
15815 : : Maintain a pending-entity map. This is keyed by top-key, and
15816 : : maps to a vector of pending-entity indices. On the binding vector
15817 : : have flags saying whether the pending-name-entity map has contents.
15818 : : (We might want to further extend the key to be GM-vs-Partition and
15819 : : specialization-vs-member, but let's not get ahead of ourselves.)
15820 : :
15821 : : For every key-like entity, find the outermost namespace-scope
15822 : : name. Use that to lookup in the pending-entity map and then make
15823 : : sure the specified entities are loaded.
15824 : :
15825 : : An optimization might be to have a flag in each key-entity saying
15826 : : that its top key might be in the entity table. It's not clear to
15827 : : me how to set that flag cheaply -- cheaper than just looking.
15828 : :
15829 : : FIXME: It'd be nice to have a bit in decls to tell us whether to
15830 : : even try this. We can have a 'already done' flag, that we set when
15831 : : we've done KLASS's lazy pendings. When we import a module that
15832 : : registers pendings on the same top-key as KLASS we need to clear
15833 : : the flag. A recursive walk of the top-key clearing the bit will
15834 : : suffice. Plus we only need to recurse on classes that have the bit
15835 : : set. (That means we need to set the bit on parents of KLASS here,
15836 : : don't forget.) However, first: correctness, second: efficiency. */
15837 : :
15838 : : unsigned
15839 : 1818 : module_state::write_pendings (elf_out *to, vec<depset *> depsets,
15840 : : depset::hash &table, unsigned *crc_p)
15841 : : {
15842 : 2046 : dump () && dump ("Writing pending-entities");
15843 : 1818 : dump.indent ();
15844 : :
15845 : 1818 : trees_out sec (to, this, table);
15846 : 1818 : sec.begin ();
15847 : :
15848 : 1818 : unsigned count = 0;
15849 : 1818 : tree cache_ns = NULL_TREE;
15850 : 1818 : tree cache_id = NULL_TREE;
15851 : 1818 : unsigned cache_section = ~0;
15852 : 2970556 : for (unsigned ix = 0; ix < depsets.length (); ix++)
15853 : : {
15854 : 1483460 : depset *d = depsets[ix];
15855 : :
15856 : 1483460 : if (d->is_binding ())
15857 : 936500 : continue;
15858 : :
15859 : 1289068 : if (d->is_import ())
15860 : 0 : continue;
15861 : :
15862 : 1289068 : if (!(d->get_entity_kind () == depset::EK_SPECIALIZATION
15863 : 767692 : || d->get_entity_kind () == depset::EK_PARTIAL
15864 : 742120 : || (d->get_entity_kind () == depset::EK_DECL && d->is_member ())))
15865 : 742108 : continue;
15866 : :
15867 : 546960 : tree key_decl = nullptr;
15868 : 546960 : tree key_ns = find_pending_key (d->get_entity (), &key_decl);
15869 : 546960 : tree key_name = DECL_NAME (key_decl);
15870 : :
15871 : 546960 : if (IDENTIFIER_ANON_P (key_name))
15872 : : {
15873 : 6 : gcc_checking_assert (IDENTIFIER_LAMBDA_P (key_name));
15874 : 6 : if (tree attached = LAMBDA_TYPE_EXTRA_SCOPE (TREE_TYPE (key_decl)))
15875 : 6 : key_name = DECL_NAME (attached);
15876 : : else
15877 : : {
15878 : : /* There's nothing to attach it to. Must
15879 : : always reinstantiate. */
15880 : 0 : dump ()
15881 : 0 : && dump ("Unattached lambda %N[%u] section:%u",
15882 : 0 : d->get_entity_kind () == depset::EK_DECL
15883 : : ? "Member" : "Specialization", d->get_entity (),
15884 : : d->cluster, d->section);
15885 : 0 : continue;
15886 : : }
15887 : : }
15888 : :
15889 : 546960 : char const *also = "";
15890 : 546960 : if (d->section == cache_section
15891 : 372104 : && key_ns == cache_ns
15892 : 372104 : && key_name == cache_id)
15893 : : /* Same section & key as previous, no need to repeat ourselves. */
15894 : : also = "also ";
15895 : : else
15896 : : {
15897 : 237078 : cache_ns = key_ns;
15898 : 237078 : cache_id = key_name;
15899 : 237078 : cache_section = d->section;
15900 : 237078 : gcc_checking_assert (table.find_dependency (cache_ns));
15901 : 237078 : sec.tree_node (cache_ns);
15902 : 237078 : sec.tree_node (cache_id);
15903 : 237078 : sec.u (d->cluster);
15904 : 237078 : count++;
15905 : : }
15906 : 571810 : dump () && dump ("Pending %s %N entity:%u section:%u %skeyed to %P",
15907 : 12425 : d->get_entity_kind () == depset::EK_DECL
15908 : : ? "member" : "specialization", d->get_entity (),
15909 : : d->cluster, cache_section, also, cache_ns, cache_id);
15910 : : }
15911 : 1818 : sec.end (to, to->name (MOD_SNAME_PFX ".pnd"), crc_p);
15912 : 1818 : dump.outdent ();
15913 : :
15914 : 1818 : return count;
15915 : 1818 : }
15916 : :
15917 : : bool
15918 : 517 : module_state::read_pendings (unsigned count)
15919 : : {
15920 : 517 : trees_in sec (this);
15921 : :
15922 : 517 : if (!sec.begin (loc, from (), MOD_SNAME_PFX ".pnd"))
15923 : : return false;
15924 : :
15925 : 660 : dump () && dump ("Reading %u pendings", count);
15926 : 517 : dump.indent ();
15927 : :
15928 : 316001 : for (unsigned ix = 0; ix != count; ix++)
15929 : : {
15930 : 315484 : pending_key key;
15931 : 315484 : unsigned index;
15932 : :
15933 : 315484 : key.ns = sec.tree_node ();
15934 : 315484 : key.id = sec.tree_node ();
15935 : 315484 : index = sec.u ();
15936 : :
15937 : 315484 : if (!key.ns || !key.id
15938 : 315484 : || !(TREE_CODE (key.ns) == NAMESPACE_DECL
15939 : 315484 : && !DECL_NAMESPACE_ALIAS (key.ns))
15940 : 315484 : || !identifier_p (key.id)
15941 : 630968 : || index >= entity_num)
15942 : 0 : sec.set_overrun ();
15943 : :
15944 : 315484 : if (sec.get_overrun ())
15945 : : break;
15946 : :
15947 : 315823 : dump () && dump ("Pending:%u keyed to %P", index, key.ns, key.id);
15948 : :
15949 : 315484 : index += entity_lwm;
15950 : 315484 : auto &vec = pending_table->get_or_insert (key);
15951 : 315484 : vec.safe_push (index);
15952 : : }
15953 : :
15954 : 517 : dump.outdent ();
15955 : 517 : if (!sec.end (from ()))
15956 : : return false;
15957 : : return true;
15958 : 517 : }
15959 : :
15960 : : /* Read & write locations. */
15961 : : enum loc_kind {
15962 : : LK_ORDINARY,
15963 : : LK_MACRO,
15964 : : LK_IMPORT_ORDINARY,
15965 : : LK_IMPORT_MACRO,
15966 : : LK_ADHOC,
15967 : : LK_RESERVED,
15968 : : };
15969 : :
15970 : : static const module_state *
15971 : 6369 : module_for_ordinary_loc (location_t loc)
15972 : : {
15973 : 6369 : unsigned pos = 0;
15974 : 12738 : unsigned len = ool->length () - pos;
15975 : :
15976 : 6370 : while (len)
15977 : : {
15978 : 6370 : unsigned half = len / 2;
15979 : 6370 : module_state *probe = (*ool)[pos + half];
15980 : 6370 : if (loc < probe->ordinary_locs.first)
15981 : : len = half;
15982 : 6369 : else if (loc < probe->ordinary_locs.first + probe->ordinary_locs.second)
15983 : 6369 : return probe;
15984 : : else
15985 : : {
15986 : 0 : pos += half + 1;
15987 : 0 : len = len - (half + 1);
15988 : : }
15989 : : }
15990 : :
15991 : : return nullptr;
15992 : : }
15993 : :
15994 : : static const module_state *
15995 : 6 : module_for_macro_loc (location_t loc)
15996 : : {
15997 : 6 : unsigned pos = 1;
15998 : 6 : unsigned len = modules->length () - pos;
15999 : :
16000 : 6 : while (len)
16001 : : {
16002 : 6 : unsigned half = len / 2;
16003 : 6 : module_state *probe = (*modules)[pos + half];
16004 : 6 : if (loc < probe->macro_locs.first)
16005 : : {
16006 : 0 : pos += half + 1;
16007 : 0 : len = len - (half + 1);
16008 : : }
16009 : 6 : else if (loc >= probe->macro_locs.first + probe->macro_locs.second)
16010 : : len = half;
16011 : : else
16012 : 6 : return probe;
16013 : : }
16014 : :
16015 : : return NULL;
16016 : : }
16017 : :
16018 : : location_t
16019 : 848 : module_state::imported_from () const
16020 : : {
16021 : 848 : location_t from = loc;
16022 : 848 : line_map_ordinary const *fmap
16023 : 848 : = linemap_check_ordinary (linemap_lookup (line_table, from));
16024 : :
16025 : 848 : if (MAP_MODULE_P (fmap))
16026 : 848 : from = linemap_included_from (fmap);
16027 : :
16028 : 848 : return from;
16029 : : }
16030 : :
16031 : : /* Note that LOC will need writing. This allows us to prune locations
16032 : : that are not needed. */
16033 : :
16034 : : bool
16035 : 29258457 : module_state::note_location (location_t loc)
16036 : : {
16037 : 29258457 : bool added = false;
16038 : 29258457 : if (!macro_loc_table && !ord_loc_table)
16039 : : ;
16040 : 29258457 : else if (loc < RESERVED_LOCATION_COUNT)
16041 : : ;
16042 : 27006556 : else if (IS_ADHOC_LOC (loc))
16043 : : {
16044 : 4139677 : location_t locus = get_location_from_adhoc_loc (line_table, loc);
16045 : 4139677 : note_location (locus);
16046 : 4139677 : source_range range = get_range_from_loc (line_table, loc);
16047 : 4139677 : if (range.m_start != locus)
16048 : 2703188 : note_location (range.m_start);
16049 : 4139677 : note_location (range.m_finish);
16050 : : }
16051 : 45733671 : else if (loc >= LINEMAPS_MACRO_LOWEST_LOCATION (line_table))
16052 : : {
16053 : 1999701 : if (spans.macro (loc))
16054 : : {
16055 : 1999695 : const line_map *map = linemap_lookup (line_table, loc);
16056 : 1999695 : const line_map_macro *mac_map = linemap_check_macro (map);
16057 : 1999695 : hashval_t hv = macro_loc_traits::hash (mac_map);
16058 : 1999695 : macro_loc_info *slot
16059 : 1999695 : = macro_loc_table->find_slot_with_hash (mac_map, hv, INSERT);
16060 : 1999695 : if (!slot->src)
16061 : : {
16062 : 219220 : slot->src = mac_map;
16063 : 219220 : slot->remap = 0;
16064 : : // Expansion locations could themselves be from a
16065 : : // macro, we need to note them all.
16066 : 219220 : note_location (mac_map->m_expansion);
16067 : 219220 : gcc_checking_assert (mac_map->n_tokens);
16068 : 219220 : location_t tloc = UNKNOWN_LOCATION;
16069 : 8459558 : for (unsigned ix = mac_map->n_tokens * 2; ix--;)
16070 : 8240338 : if (mac_map->macro_locations[ix] != tloc)
16071 : : {
16072 : 4370433 : tloc = mac_map->macro_locations[ix];
16073 : 4370433 : note_location (tloc);
16074 : : }
16075 : : added = true;
16076 : : }
16077 : : }
16078 : : }
16079 : 20867178 : else if (IS_ORDINARY_LOC (loc))
16080 : : {
16081 : 20867178 : if (spans.ordinary (loc))
16082 : : {
16083 : 20860811 : const line_map *map = linemap_lookup (line_table, loc);
16084 : 20860811 : const line_map_ordinary *ord_map = linemap_check_ordinary (map);
16085 : 20860811 : ord_loc_info lkup;
16086 : 20860811 : lkup.src = ord_map;
16087 : 20860811 : lkup.span = 1 << ord_map->m_column_and_range_bits;
16088 : 20860811 : lkup.offset = (loc - MAP_START_LOCATION (ord_map)) & ~(lkup.span - 1);
16089 : 20860811 : lkup.remap = 0;
16090 : 20860811 : ord_loc_info *slot = (ord_loc_table->find_slot_with_hash
16091 : 20860811 : (lkup, ord_loc_traits::hash (lkup), INSERT));
16092 : 20860811 : if (!slot->src)
16093 : : {
16094 : 2363271 : *slot = lkup;
16095 : 2363271 : added = true;
16096 : : }
16097 : : }
16098 : : }
16099 : : else
16100 : 0 : gcc_unreachable ();
16101 : 29258457 : return added;
16102 : : }
16103 : :
16104 : : /* If we're not streaming, record that we need location LOC.
16105 : : Otherwise stream it. */
16106 : :
16107 : : void
16108 : 42941035 : module_state::write_location (bytes_out &sec, location_t loc)
16109 : : {
16110 : 42941035 : if (!sec.streaming_p ())
16111 : : {
16112 : 13279530 : note_location (loc);
16113 : 13279530 : return;
16114 : : }
16115 : :
16116 : 29661505 : if (loc < RESERVED_LOCATION_COUNT)
16117 : : {
16118 : 3530396 : dump (dumper::LOCATION) && dump ("Reserved location %u", unsigned (loc));
16119 : 3530378 : sec.u (LK_RESERVED + loc);
16120 : : }
16121 : 26131127 : else if (IS_ADHOC_LOC (loc))
16122 : : {
16123 : 4025134 : dump (dumper::LOCATION) && dump ("Adhoc location");
16124 : 4025131 : sec.u (LK_ADHOC);
16125 : 4025131 : location_t locus = get_location_from_adhoc_loc (line_table, loc);
16126 : 4025131 : write_location (sec, locus);
16127 : 4025131 : source_range range = get_range_from_loc (line_table, loc);
16128 : 4025131 : if (range.m_start == locus)
16129 : : /* Compress. */
16130 : 1296261 : range.m_start = UNKNOWN_LOCATION;
16131 : 4025131 : write_location (sec, range.m_start);
16132 : 4025131 : write_location (sec, range.m_finish);
16133 : 4025131 : unsigned discriminator = get_discriminator_from_adhoc_loc (line_table, loc);
16134 : 4025131 : sec.u (discriminator);
16135 : : }
16136 : 44211905 : else if (loc >= LINEMAPS_MACRO_LOWEST_LOCATION (line_table))
16137 : : {
16138 : 1984298 : const macro_loc_info *info = nullptr;
16139 : 1984298 : unsigned offset = 0;
16140 : 1984298 : if (unsigned hwm = macro_loc_remap->length ())
16141 : : {
16142 : 1984292 : info = macro_loc_remap->begin ();
16143 : 29516122 : while (hwm != 1)
16144 : : {
16145 : 25547538 : unsigned mid = hwm / 2;
16146 : 25547538 : if (MAP_START_LOCATION (info[mid].src) <= loc)
16147 : : {
16148 : 12906707 : info += mid;
16149 : 12906707 : hwm -= mid;
16150 : : }
16151 : : else
16152 : : hwm = mid;
16153 : : }
16154 : 1984292 : offset = loc - MAP_START_LOCATION (info->src);
16155 : 1984292 : if (offset > info->src->n_tokens)
16156 : 0 : info = nullptr;
16157 : : }
16158 : :
16159 : 1984298 : gcc_checking_assert (bool (info) == bool (spans.macro (loc)));
16160 : :
16161 : 1984298 : if (info)
16162 : : {
16163 : 1984292 : offset += info->remap;
16164 : 1984292 : sec.u (LK_MACRO);
16165 : 1984292 : sec.u (offset);
16166 : 1984292 : dump (dumper::LOCATION)
16167 : 9 : && dump ("Macro location %u output %u", loc, offset);
16168 : : }
16169 : 6 : else if (const module_state *import = module_for_macro_loc (loc))
16170 : : {
16171 : 6 : unsigned off = loc - import->macro_locs.first;
16172 : 6 : sec.u (LK_IMPORT_MACRO);
16173 : 6 : sec.u (import->remap);
16174 : 6 : sec.u (off);
16175 : 6 : dump (dumper::LOCATION)
16176 : 0 : && dump ("Imported macro location %u output %u:%u",
16177 : 0 : loc, import->remap, off);
16178 : : }
16179 : : else
16180 : 0 : gcc_unreachable ();
16181 : : }
16182 : 20121698 : else if (IS_ORDINARY_LOC (loc))
16183 : : {
16184 : 20121698 : const ord_loc_info *info = nullptr;
16185 : 20121698 : unsigned offset = 0;
16186 : 20121698 : if (unsigned hwm = ord_loc_remap->length ())
16187 : : {
16188 : 20121698 : info = ord_loc_remap->begin ();
16189 : 294327772 : while (hwm != 1)
16190 : : {
16191 : 254084376 : unsigned mid = hwm / 2;
16192 : 254084376 : if (MAP_START_LOCATION (info[mid].src) + info[mid].offset <= loc)
16193 : : {
16194 : 137121769 : info += mid;
16195 : 137121769 : hwm -= mid;
16196 : : }
16197 : : else
16198 : : hwm = mid;
16199 : : }
16200 : 20121698 : offset = loc - MAP_START_LOCATION (info->src) - info->offset;
16201 : 20121698 : if (offset > info->span)
16202 : 6369 : info = nullptr;
16203 : : }
16204 : :
16205 : 20121698 : gcc_checking_assert (bool (info) == bool (spans.ordinary (loc)));
16206 : :
16207 : 20121698 : if (info)
16208 : : {
16209 : 20115329 : offset += info->remap;
16210 : 20115329 : sec.u (LK_ORDINARY);
16211 : 20115329 : sec.u (offset);
16212 : :
16213 : 20115329 : dump (dumper::LOCATION)
16214 : 78 : && dump ("Ordinary location %u output %u", loc, offset);
16215 : : }
16216 : 6369 : else if (const module_state *import = module_for_ordinary_loc (loc))
16217 : : {
16218 : 6369 : unsigned off = loc - import->ordinary_locs.first;
16219 : 6369 : sec.u (LK_IMPORT_ORDINARY);
16220 : 6369 : sec.u (import->remap);
16221 : 6369 : sec.u (off);
16222 : 6369 : dump (dumper::LOCATION)
16223 : 0 : && dump ("Imported ordinary location %u output %u:%u",
16224 : 0 : import->remap, import->remap, off);
16225 : : }
16226 : : else
16227 : 0 : gcc_unreachable ();
16228 : : }
16229 : : else
16230 : 0 : gcc_unreachable ();
16231 : : }
16232 : :
16233 : : location_t
16234 : 30016318 : module_state::read_location (bytes_in &sec) const
16235 : : {
16236 : 30016318 : location_t locus = UNKNOWN_LOCATION;
16237 : 30016318 : unsigned kind = sec.u ();
16238 : 30016318 : switch (kind)
16239 : : {
16240 : 3644710 : default:
16241 : 3644710 : {
16242 : 3644710 : if (kind < LK_RESERVED + RESERVED_LOCATION_COUNT)
16243 : 3644710 : locus = location_t (kind - LK_RESERVED);
16244 : : else
16245 : 0 : sec.set_overrun ();
16246 : 3644710 : dump (dumper::LOCATION)
16247 : 0 : && dump ("Reserved location %u", unsigned (locus));
16248 : : }
16249 : : break;
16250 : :
16251 : 4215035 : case LK_ADHOC:
16252 : 4215035 : {
16253 : 4215035 : dump (dumper::LOCATION) && dump ("Adhoc location");
16254 : 4215035 : locus = read_location (sec);
16255 : 4215035 : source_range range;
16256 : 4215035 : range.m_start = read_location (sec);
16257 : 4215035 : if (range.m_start == UNKNOWN_LOCATION)
16258 : 1203628 : range.m_start = locus;
16259 : 4215035 : range.m_finish = read_location (sec);
16260 : 4215035 : unsigned discriminator = sec.u ();
16261 : 4215035 : if (locus != loc && range.m_start != loc && range.m_finish != loc)
16262 : 2238833 : locus = line_table->get_or_create_combined_loc (locus, range,
16263 : : nullptr, discriminator);
16264 : : }
16265 : : break;
16266 : :
16267 : 1888724 : case LK_MACRO:
16268 : 1888724 : {
16269 : 1888724 : unsigned off = sec.u ();
16270 : :
16271 : 1888724 : if (macro_locs.second)
16272 : : {
16273 : 1394574 : if (off < macro_locs.second)
16274 : 1394574 : locus = off + macro_locs.first;
16275 : : else
16276 : 0 : sec.set_overrun ();
16277 : : }
16278 : : else
16279 : 494150 : locus = loc;
16280 : 1888724 : dump (dumper::LOCATION)
16281 : 0 : && dump ("Macro %u becoming %u", off, locus);
16282 : : }
16283 : : break;
16284 : :
16285 : 20267200 : case LK_ORDINARY:
16286 : 20267200 : {
16287 : 20267200 : unsigned off = sec.u ();
16288 : 20267200 : if (ordinary_locs.second)
16289 : : {
16290 : 12526719 : if (off < ordinary_locs.second)
16291 : 12526719 : locus = off + ordinary_locs.first;
16292 : : else
16293 : 0 : sec.set_overrun ();
16294 : : }
16295 : : else
16296 : 7740481 : locus = loc;
16297 : :
16298 : 20267200 : dump (dumper::LOCATION)
16299 : 0 : && dump ("Ordinary location %u becoming %u", off, locus);
16300 : : }
16301 : : break;
16302 : :
16303 : 649 : case LK_IMPORT_MACRO:
16304 : 649 : case LK_IMPORT_ORDINARY:
16305 : 649 : {
16306 : 649 : unsigned mod = sec.u ();
16307 : 649 : unsigned off = sec.u ();
16308 : 649 : const module_state *import = NULL;
16309 : :
16310 : 649 : if (!mod && !slurp->remap)
16311 : : /* This is an early read of a partition location during the
16312 : : read of our ordinary location map. */
16313 : : import = this;
16314 : : else
16315 : : {
16316 : 649 : mod = slurp->remap_module (mod);
16317 : 649 : if (!mod)
16318 : 0 : sec.set_overrun ();
16319 : : else
16320 : 649 : import = (*modules)[mod];
16321 : : }
16322 : :
16323 : 649 : if (import)
16324 : : {
16325 : 649 : if (kind == LK_IMPORT_MACRO)
16326 : : {
16327 : 6 : if (!import->macro_locs.second)
16328 : 0 : locus = import->loc;
16329 : 6 : else if (off < import->macro_locs.second)
16330 : 6 : locus = off + import->macro_locs.first;
16331 : : else
16332 : 0 : sec.set_overrun ();
16333 : : }
16334 : : else
16335 : : {
16336 : 643 : if (!import->ordinary_locs.second)
16337 : 0 : locus = import->loc;
16338 : 643 : else if (off < import->ordinary_locs.second)
16339 : 643 : locus = import->ordinary_locs.first + off;
16340 : : else
16341 : 0 : sec.set_overrun ();
16342 : : }
16343 : : }
16344 : : }
16345 : : break;
16346 : : }
16347 : :
16348 : 30016318 : return locus;
16349 : : }
16350 : :
16351 : : /* Allocate hash tables to record needed locations. */
16352 : :
16353 : : void
16354 : 1824 : module_state::write_init_maps ()
16355 : : {
16356 : 1824 : macro_loc_table = new hash_table<macro_loc_traits> (EXPERIMENT (1, 400));
16357 : 1824 : ord_loc_table = new hash_table<ord_loc_traits> (EXPERIMENT (1, 400));
16358 : 1824 : }
16359 : :
16360 : : /* Prepare the span adjustments. We prune unneeded locations -- at
16361 : : this point every needed location must have been seen by
16362 : : note_location. */
16363 : :
16364 : : range_t
16365 : 1818 : module_state::write_prepare_maps (module_state_config *cfg, bool has_partitions)
16366 : : {
16367 : 2046 : dump () && dump ("Preparing locations");
16368 : 1818 : dump.indent ();
16369 : :
16370 : 2046 : dump () && dump ("Reserved locations [%u,%u) macro [%u,%u)",
16371 : 228 : spans[loc_spans::SPAN_RESERVED].ordinary.first,
16372 : 228 : spans[loc_spans::SPAN_RESERVED].ordinary.second,
16373 : 228 : spans[loc_spans::SPAN_RESERVED].macro.first,
16374 : 228 : spans[loc_spans::SPAN_RESERVED].macro.second);
16375 : :
16376 : 1818 : range_t info {0, 0};
16377 : :
16378 : : // Sort the noted lines.
16379 : 1818 : vec_alloc (ord_loc_remap, ord_loc_table->size ());
16380 : 1818 : for (auto iter = ord_loc_table->begin (), end = ord_loc_table->end ();
16381 : 4698188 : iter != end; ++iter)
16382 : 2348185 : ord_loc_remap->quick_push (*iter);
16383 : 1818 : ord_loc_remap->qsort (&ord_loc_info::compare);
16384 : :
16385 : : // Note included-from maps.
16386 : 1818 : bool added = false;
16387 : 1818 : const line_map_ordinary *current = nullptr;
16388 : 2353639 : for (auto iter = ord_loc_remap->begin (), end = ord_loc_remap->end ();
16389 : 2350003 : iter != end; ++iter)
16390 : 2348185 : if (iter->src != current)
16391 : : {
16392 : 39223 : current = iter->src;
16393 : 30088 : for (auto probe = current;
16394 : 39223 : auto from = linemap_included_from (probe);
16395 : 15044 : probe = linemap_check_ordinary (linemap_lookup (line_table, from)))
16396 : : {
16397 : 37149 : if (has_partitions)
16398 : : {
16399 : : // Partition locations need to elide their module map
16400 : : // entry.
16401 : 90 : probe
16402 : 90 : = linemap_check_ordinary (linemap_lookup (line_table, from));
16403 : 90 : if (MAP_MODULE_P (probe))
16404 : 78 : from = linemap_included_from (probe);
16405 : : }
16406 : :
16407 : 37149 : if (!note_location (from))
16408 : : break;
16409 : 15044 : added = true;
16410 : 15044 : }
16411 : : }
16412 : 1818 : if (added)
16413 : : {
16414 : : // Reconstruct the line array as we added items to the hash table.
16415 : 386 : vec_free (ord_loc_remap);
16416 : 386 : vec_alloc (ord_loc_remap, ord_loc_table->size ());
16417 : 386 : for (auto iter = ord_loc_table->begin (), end = ord_loc_table->end ();
16418 : 4714180 : iter != end; ++iter)
16419 : 2356897 : ord_loc_remap->quick_push (*iter);
16420 : 386 : ord_loc_remap->qsort (&ord_loc_info::compare);
16421 : : }
16422 : 1818 : delete ord_loc_table;
16423 : 1818 : ord_loc_table = nullptr;
16424 : :
16425 : : // Merge (sufficiently) adjacent spans, and calculate remapping.
16426 : 1818 : constexpr unsigned adjacency = 2; // Allow 2 missing lines.
16427 : 3636 : auto begin = ord_loc_remap->begin (), end = ord_loc_remap->end ();
16428 : 1818 : auto dst = begin;
16429 : 1818 : unsigned offset = 0, range_bits = 0;
16430 : 1818 : ord_loc_info *base = nullptr;
16431 : 2365047 : for (auto iter = begin; iter != end; ++iter)
16432 : : {
16433 : 2363229 : if (base && iter->src == base->src)
16434 : : {
16435 : 4399483 : if (base->offset + base->span +
16436 : 2327981 : ((adjacency << base->src->m_column_and_range_bits)
16437 : : // If there are few c&r bits, allow further separation.
16438 : 2327981 : | (adjacency << 4))
16439 : 2327981 : >= iter->offset)
16440 : : {
16441 : : // Merge.
16442 : 2071502 : offset -= base->span;
16443 : 2071502 : base->span = iter->offset + iter->span - base->offset;
16444 : 2071502 : offset += base->span;
16445 : 2071502 : continue;
16446 : : }
16447 : : }
16448 : 35248 : else if (range_bits < iter->src->m_range_bits)
16449 : 1754 : range_bits = iter->src->m_range_bits;
16450 : :
16451 : 291727 : offset += ((1u << iter->src->m_range_bits) - 1);
16452 : 291727 : offset &= ~((1u << iter->src->m_range_bits) - 1);
16453 : 291727 : iter->remap = offset;
16454 : 291727 : offset += iter->span;
16455 : 291727 : base = dst;
16456 : 291727 : *dst++ = *iter;
16457 : : }
16458 : 1818 : ord_loc_remap->truncate (dst - begin);
16459 : :
16460 : 1818 : info.first = ord_loc_remap->length ();
16461 : 1818 : cfg->ordinary_locs = offset;
16462 : 1818 : cfg->loc_range_bits = range_bits;
16463 : 2046 : dump () && dump ("Ordinary maps:%u locs:%u range_bits:%u",
16464 : : info.first, cfg->ordinary_locs,
16465 : : cfg->loc_range_bits);
16466 : :
16467 : : // Remap the macro locations.
16468 : 1818 : vec_alloc (macro_loc_remap, macro_loc_table->size ());
16469 : 1818 : for (auto iter = macro_loc_table->begin (), end = macro_loc_table->end ();
16470 : 440258 : iter != end; ++iter)
16471 : 219220 : macro_loc_remap->quick_push (*iter);
16472 : 1818 : delete macro_loc_table;
16473 : 1818 : macro_loc_table = nullptr;
16474 : :
16475 : 1818 : macro_loc_remap->qsort (¯o_loc_info::compare);
16476 : 1818 : offset = 0;
16477 : 5454 : for (auto iter = macro_loc_remap->begin (), end = macro_loc_remap->end ();
16478 : 221038 : iter != end; ++iter)
16479 : : {
16480 : 219220 : auto mac = iter->src;
16481 : 219220 : iter->remap = offset;
16482 : 219220 : offset += mac->n_tokens;
16483 : : }
16484 : 1818 : info.second = macro_loc_remap->length ();
16485 : 1818 : cfg->macro_locs = offset;
16486 : :
16487 : 2046 : dump () && dump ("Macro maps:%u locs:%u", info.second, cfg->macro_locs);
16488 : :
16489 : 1818 : dump.outdent ();
16490 : :
16491 : : // If we have no ordinary locs, we must also have no macro locs.
16492 : 1818 : gcc_checking_assert (cfg->ordinary_locs || !cfg->macro_locs);
16493 : :
16494 : 1818 : return info;
16495 : : }
16496 : :
16497 : : bool
16498 : 2022 : module_state::read_prepare_maps (const module_state_config *cfg)
16499 : : {
16500 : 2022 : location_t ordinary = line_table->highest_location + 1;
16501 : 2022 : ordinary += cfg->ordinary_locs;
16502 : :
16503 : 2022 : location_t macro = LINEMAPS_MACRO_LOWEST_LOCATION (line_table);
16504 : 2022 : macro -= cfg->macro_locs;
16505 : :
16506 : 2022 : if (ordinary < LINE_MAP_MAX_LOCATION_WITH_COLS
16507 : 2022 : && macro >= LINE_MAP_MAX_LOCATION)
16508 : : /* OK, we have enough locations. */
16509 : : return true;
16510 : :
16511 : 13 : ordinary_locs.first = ordinary_locs.second = 0;
16512 : 13 : macro_locs.first = macro_locs.second = 0;
16513 : :
16514 : 13 : static bool informed = false;
16515 : 13 : if (!informed)
16516 : : {
16517 : : /* Just give the notice once. */
16518 : 13 : informed = true;
16519 : 13 : inform (loc, "unable to represent further imported source locations");
16520 : : }
16521 : :
16522 : : return false;
16523 : : }
16524 : :
16525 : : /* Write & read the location maps. Not called if there are no
16526 : : locations. */
16527 : :
16528 : : void
16529 : 1754 : module_state::write_ordinary_maps (elf_out *to, range_t &info,
16530 : : bool has_partitions, unsigned *crc_p)
16531 : : {
16532 : 1967 : dump () && dump ("Writing ordinary location maps");
16533 : 1754 : dump.indent ();
16534 : :
16535 : 1754 : vec<const char *> filenames;
16536 : 1754 : filenames.create (20);
16537 : :
16538 : : /* Determine the unique filenames. */
16539 : 1754 : const line_map_ordinary *current = nullptr;
16540 : 296989 : for (auto iter = ord_loc_remap->begin (), end = ord_loc_remap->end ();
16541 : 293481 : iter != end; ++iter)
16542 : 291727 : if (iter->src != current)
16543 : : {
16544 : 35248 : current = iter->src;
16545 : 35248 : const char *fname = ORDINARY_MAP_FILE_NAME (iter->src);
16546 : :
16547 : : /* We should never find a module linemap in an interval. */
16548 : 35248 : gcc_checking_assert (!MAP_MODULE_P (iter->src));
16549 : :
16550 : : /* We expect very few filenames, so just an array.
16551 : : (Not true when headers are still in play :() */
16552 : 2199716 : for (unsigned jx = filenames.length (); jx--;)
16553 : : {
16554 : 2149002 : const char *name = filenames[jx];
16555 : 2149002 : if (0 == strcmp (name, fname))
16556 : : {
16557 : : /* Reset the linemap's name, because for things like
16558 : : preprocessed input we could have multiple instances
16559 : : of the same name, and we'd rather not percolate
16560 : : that. */
16561 : 19782 : const_cast<line_map_ordinary *> (iter->src)->to_file = name;
16562 : 19782 : fname = NULL;
16563 : 19782 : break;
16564 : : }
16565 : : }
16566 : 35248 : if (fname)
16567 : 15466 : filenames.safe_push (fname);
16568 : : }
16569 : :
16570 : 1754 : bytes_out sec (to);
16571 : 1754 : sec.begin ();
16572 : :
16573 : : /* Write the filenames. */
16574 : 1754 : unsigned len = filenames.length ();
16575 : 1754 : sec.u (len);
16576 : 1967 : dump () && dump ("%u source file names", len);
16577 : 17220 : for (unsigned ix = 0; ix != len; ix++)
16578 : : {
16579 : 15466 : const char *fname = filenames[ix];
16580 : 15481 : dump (dumper::LOCATION) && dump ("Source file[%u]=%s", ix, fname);
16581 : 15466 : sec.str (fname);
16582 : : }
16583 : :
16584 : 1754 : sec.u (info.first); /* Num maps. */
16585 : 1754 : const ord_loc_info *base = nullptr;
16586 : 296989 : for (auto iter = ord_loc_remap->begin (), end = ord_loc_remap->end ();
16587 : 293481 : iter != end; ++iter)
16588 : : {
16589 : 291727 : dump (dumper::LOCATION)
16590 : 36 : && dump ("Span:%u ordinary [%u+%u,+%u)->[%u,+%u)",
16591 : 36 : iter - ord_loc_remap->begin (),
16592 : 18 : MAP_START_LOCATION (iter->src), iter->offset, iter->span,
16593 : : iter->remap, iter->span);
16594 : :
16595 : 291727 : if (!base || iter->src != base->src)
16596 : 35248 : base = iter;
16597 : 291727 : sec.u (iter->offset - base->offset);
16598 : 291727 : if (base == iter)
16599 : : {
16600 : 35248 : sec.u (iter->src->sysp);
16601 : 35248 : sec.u (iter->src->m_range_bits);
16602 : 35248 : sec.u (iter->src->m_column_and_range_bits - iter->src->m_range_bits);
16603 : :
16604 : 35248 : const char *fname = ORDINARY_MAP_FILE_NAME (iter->src);
16605 : 8160700 : for (unsigned ix = 0; ix != filenames.length (); ix++)
16606 : 4080350 : if (filenames[ix] == fname)
16607 : : {
16608 : 35248 : sec.u (ix);
16609 : 35248 : break;
16610 : : }
16611 : 35248 : unsigned line = ORDINARY_MAP_STARTING_LINE_NUMBER (iter->src);
16612 : 35248 : line += iter->offset >> iter->src->m_column_and_range_bits;
16613 : 35248 : sec.u (line);
16614 : : }
16615 : 291727 : sec.u (iter->remap);
16616 : 291727 : if (base == iter)
16617 : : {
16618 : : /* Write the included from location, which means reading it
16619 : : while reading in the ordinary maps. So we'd better not
16620 : : be getting ahead of ourselves. */
16621 : 35248 : location_t from = linemap_included_from (iter->src);
16622 : 35248 : gcc_checking_assert (from < MAP_START_LOCATION (iter->src));
16623 : 35248 : if (from != UNKNOWN_LOCATION && has_partitions)
16624 : : {
16625 : : /* A partition's span will have a from pointing at a
16626 : : MODULE_INC. Find that map's from. */
16627 : 87 : line_map_ordinary const *fmap
16628 : 87 : = linemap_check_ordinary (linemap_lookup (line_table, from));
16629 : 87 : if (MAP_MODULE_P (fmap))
16630 : 75 : from = linemap_included_from (fmap);
16631 : : }
16632 : 35248 : write_location (sec, from);
16633 : : }
16634 : : }
16635 : :
16636 : 1754 : filenames.release ();
16637 : :
16638 : 1754 : sec.end (to, to->name (MOD_SNAME_PFX ".olm"), crc_p);
16639 : 1754 : dump.outdent ();
16640 : 1754 : }
16641 : :
16642 : : void
16643 : 121 : module_state::write_macro_maps (elf_out *to, range_t &info, unsigned *crc_p)
16644 : : {
16645 : 136 : dump () && dump ("Writing macro location maps");
16646 : 121 : dump.indent ();
16647 : :
16648 : 121 : bytes_out sec (to);
16649 : 121 : sec.begin ();
16650 : :
16651 : 136 : dump () && dump ("Macro maps:%u", info.second);
16652 : 121 : sec.u (info.second);
16653 : :
16654 : 121 : unsigned macro_num = 0;
16655 : 363 : for (auto iter = macro_loc_remap->end (), begin = macro_loc_remap->begin ();
16656 : 219341 : iter-- != begin;)
16657 : : {
16658 : 219220 : auto mac = iter->src;
16659 : 219220 : sec.u (iter->remap);
16660 : 219220 : sec.u (mac->n_tokens);
16661 : 219220 : sec.cpp_node (mac->macro);
16662 : 219220 : write_location (sec, mac->m_expansion);
16663 : 219220 : const location_t *locs = mac->macro_locations;
16664 : : /* There are lots of identical runs. */
16665 : 219220 : location_t prev = UNKNOWN_LOCATION;
16666 : 219220 : unsigned count = 0;
16667 : 219220 : unsigned runs = 0;
16668 : 8459558 : for (unsigned jx = mac->n_tokens * 2; jx--;)
16669 : : {
16670 : 8240338 : location_t tok_loc = locs[jx];
16671 : 8240338 : if (tok_loc == prev)
16672 : : {
16673 : 3869905 : count++;
16674 : 3869905 : continue;
16675 : : }
16676 : 4370433 : runs++;
16677 : 4370433 : sec.u (count);
16678 : 4370433 : count = 1;
16679 : 4370433 : prev = tok_loc;
16680 : 4370433 : write_location (sec, tok_loc);
16681 : : }
16682 : 219220 : sec.u (count);
16683 : 219220 : dump (dumper::LOCATION)
16684 : 9 : && dump ("Macro:%u %I %u/%u*2 locations [%u,%u)->%u",
16685 : 9 : macro_num, identifier (mac->macro),
16686 : : runs, mac->n_tokens,
16687 : : MAP_START_LOCATION (mac),
16688 : 9 : MAP_START_LOCATION (mac) + mac->n_tokens,
16689 : : iter->remap);
16690 : 219220 : macro_num++;
16691 : : }
16692 : 121 : gcc_assert (macro_num == info.second);
16693 : :
16694 : 121 : sec.end (to, to->name (MOD_SNAME_PFX ".mlm"), crc_p);
16695 : 121 : dump.outdent ();
16696 : 121 : }
16697 : :
16698 : : bool
16699 : 1949 : module_state::read_ordinary_maps (unsigned num_ord_locs, unsigned range_bits)
16700 : : {
16701 : 1949 : bytes_in sec;
16702 : :
16703 : 1949 : if (!sec.begin (loc, from (), MOD_SNAME_PFX ".olm"))
16704 : : return false;
16705 : 2383 : dump () && dump ("Reading ordinary location maps");
16706 : 1949 : dump.indent ();
16707 : :
16708 : : /* Read the filename table. */
16709 : 1949 : unsigned len = sec.u ();
16710 : 2383 : dump () && dump ("%u source file names", len);
16711 : 1949 : vec<const char *> filenames;
16712 : 1949 : filenames.create (len);
16713 : 17138 : for (unsigned ix = 0; ix != len; ix++)
16714 : : {
16715 : 15189 : size_t l;
16716 : 15189 : const char *buf = sec.str (&l);
16717 : 15189 : char *fname = XNEWVEC (char, l + 1);
16718 : 15189 : memcpy (fname, buf, l + 1);
16719 : 15189 : dump (dumper::LOCATION) && dump ("Source file[%u]=%s", ix, fname);
16720 : : /* We leak these names into the line-map table. But it
16721 : : doesn't own them. */
16722 : 15189 : filenames.quick_push (fname);
16723 : : }
16724 : :
16725 : 1949 : unsigned num_ordinary = sec.u ();
16726 : 2383 : dump () && dump ("Ordinary maps:%u, range_bits:%u", num_ordinary, range_bits);
16727 : :
16728 : 1949 : location_t offset = line_table->highest_location + 1;
16729 : 1949 : offset += ((1u << range_bits) - 1);
16730 : 1949 : offset &= ~((1u << range_bits) - 1);
16731 : 1949 : ordinary_locs.first = offset;
16732 : :
16733 : 1949 : bool propagated = spans.maybe_propagate (this, offset);
16734 : 1949 : line_map_ordinary *maps = static_cast<line_map_ordinary *>
16735 : 1949 : (line_map_new_raw (line_table, false, num_ordinary));
16736 : :
16737 : 1949 : const line_map_ordinary *base = nullptr;
16738 : 271177 : for (unsigned ix = 0; ix != num_ordinary && !sec.get_overrun (); ix++)
16739 : : {
16740 : 269228 : line_map_ordinary *map = &maps[ix];
16741 : :
16742 : 269228 : unsigned offset = sec.u ();
16743 : 269228 : if (!offset)
16744 : : {
16745 : 33612 : map->reason = LC_RENAME;
16746 : 33612 : map->sysp = sec.u ();
16747 : 33612 : map->m_range_bits = sec.u ();
16748 : 33612 : map->m_column_and_range_bits = sec.u () + map->m_range_bits;
16749 : 33612 : unsigned fnum = sec.u ();
16750 : 67224 : map->to_file = (fnum < filenames.length () ? filenames[fnum] : "");
16751 : 33612 : map->to_line = sec.u ();
16752 : 33612 : base = map;
16753 : : }
16754 : : else
16755 : : {
16756 : 235616 : *map = *base;
16757 : 235616 : map->to_line += offset >> map->m_column_and_range_bits;
16758 : : }
16759 : 269228 : unsigned remap = sec.u ();
16760 : 269228 : map->start_location = remap + ordinary_locs.first;
16761 : 269228 : if (base == map)
16762 : : {
16763 : : /* Root the outermost map at our location. */
16764 : 33612 : ordinary_locs.second = remap;
16765 : 33612 : location_t from = read_location (sec);
16766 : 33612 : map->included_from = from != UNKNOWN_LOCATION ? from : loc;
16767 : : }
16768 : : }
16769 : :
16770 : 1949 : ordinary_locs.second = num_ord_locs;
16771 : : /* highest_location is the one handed out, not the next one to
16772 : : hand out. */
16773 : 1949 : line_table->highest_location = ordinary_locs.first + ordinary_locs.second - 1;
16774 : :
16775 : 1949 : if (line_table->highest_location >= LINE_MAP_MAX_LOCATION_WITH_COLS)
16776 : : /* We shouldn't run out of locations, as we checked before
16777 : : starting. */
16778 : 0 : sec.set_overrun ();
16779 : 2383 : dump () && dump ("Ordinary location [%u,+%u)",
16780 : : ordinary_locs.first, ordinary_locs.second);
16781 : :
16782 : 1949 : if (propagated)
16783 : 84 : spans.close ();
16784 : :
16785 : 1949 : filenames.release ();
16786 : :
16787 : 1949 : dump.outdent ();
16788 : 1949 : if (!sec.end (from ()))
16789 : : return false;
16790 : :
16791 : : return true;
16792 : 1949 : }
16793 : :
16794 : : bool
16795 : 126 : module_state::read_macro_maps (unsigned num_macro_locs)
16796 : : {
16797 : 126 : bytes_in sec;
16798 : :
16799 : 126 : if (!sec.begin (loc, from (), MOD_SNAME_PFX ".mlm"))
16800 : : return false;
16801 : 132 : dump () && dump ("Reading macro location maps");
16802 : 126 : dump.indent ();
16803 : :
16804 : 126 : unsigned num_macros = sec.u ();
16805 : 132 : dump () && dump ("Macro maps:%u locs:%u", num_macros, num_macro_locs);
16806 : :
16807 : 252 : bool propagated = spans.maybe_propagate (this,
16808 : 126 : line_table->highest_location + 1);
16809 : :
16810 : 126 : location_t offset = LINEMAPS_MACRO_LOWEST_LOCATION (line_table);
16811 : 126 : macro_locs.second = num_macro_locs;
16812 : 126 : macro_locs.first = offset - num_macro_locs;
16813 : :
16814 : 132 : dump () && dump ("Macro loc delta %d", offset);
16815 : 132 : dump () && dump ("Macro locations [%u,%u)",
16816 : : macro_locs.first, macro_locs.second);
16817 : :
16818 : 177954 : for (unsigned ix = 0; ix != num_macros && !sec.get_overrun (); ix++)
16819 : : {
16820 : 177828 : unsigned offset = sec.u ();
16821 : 177828 : unsigned n_tokens = sec.u ();
16822 : 177828 : cpp_hashnode *node = sec.cpp_node ();
16823 : 177828 : location_t exp_loc = read_location (sec);
16824 : :
16825 : 177828 : const line_map_macro *macro
16826 : 177828 : = linemap_enter_macro (line_table, node, exp_loc, n_tokens);
16827 : 177828 : if (!macro)
16828 : : /* We shouldn't run out of locations, as we checked that we
16829 : : had enough before starting. */
16830 : : break;
16831 : 177828 : gcc_checking_assert (MAP_START_LOCATION (macro)
16832 : : == offset + macro_locs.first);
16833 : :
16834 : 177828 : location_t *locs = macro->macro_locations;
16835 : 177828 : location_t tok_loc = UNKNOWN_LOCATION;
16836 : 177828 : unsigned count = sec.u ();
16837 : 177828 : unsigned runs = 0;
16838 : 6510436 : for (unsigned jx = macro->n_tokens * 2; jx-- && !sec.get_overrun ();)
16839 : : {
16840 : 9697203 : while (!count-- && !sec.get_overrun ())
16841 : : {
16842 : 3364595 : runs++;
16843 : 3364595 : tok_loc = read_location (sec);
16844 : 3364595 : count = sec.u ();
16845 : : }
16846 : 6332608 : locs[jx] = tok_loc;
16847 : : }
16848 : 177828 : if (count)
16849 : 0 : sec.set_overrun ();
16850 : 177855 : dump (dumper::LOCATION)
16851 : 0 : && dump ("Macro:%u %I %u/%u*2 locations [%u,%u)",
16852 : : ix, identifier (node), runs, n_tokens,
16853 : : MAP_START_LOCATION (macro),
16854 : 0 : MAP_START_LOCATION (macro) + n_tokens);
16855 : : }
16856 : :
16857 : 132 : dump () && dump ("Macro location lwm:%u", macro_locs.first);
16858 : 126 : if (propagated)
16859 : 3 : spans.close ();
16860 : :
16861 : 126 : dump.outdent ();
16862 : 126 : if (!sec.end (from ()))
16863 : : return false;
16864 : :
16865 : : return true;
16866 : 126 : }
16867 : :
16868 : : /* Serialize the definition of MACRO. */
16869 : :
16870 : : void
16871 : 115397 : module_state::write_define (bytes_out &sec, const cpp_macro *macro)
16872 : : {
16873 : 115397 : sec.u (macro->count);
16874 : :
16875 : 115397 : bytes_out::bits_out bits = sec.stream_bits ();
16876 : 115397 : bits.b (macro->fun_like);
16877 : 115397 : bits.b (macro->variadic);
16878 : 115397 : bits.b (macro->syshdr);
16879 : 115397 : bits.bflush ();
16880 : :
16881 : 115397 : write_location (sec, macro->line);
16882 : 115397 : if (macro->fun_like)
16883 : : {
16884 : 12976 : sec.u (macro->paramc);
16885 : 12976 : const cpp_hashnode *const *parms = macro->parm.params;
16886 : 33181 : for (unsigned ix = 0; ix != macro->paramc; ix++)
16887 : 20205 : sec.cpp_node (parms[ix]);
16888 : : }
16889 : :
16890 : : unsigned len = 0;
16891 : 366767 : for (unsigned ix = 0; ix != macro->count; ix++)
16892 : : {
16893 : 251370 : const cpp_token *token = ¯o->exp.tokens[ix];
16894 : 251370 : write_location (sec, token->src_loc);
16895 : 251370 : sec.u (token->type);
16896 : 251370 : sec.u (token->flags);
16897 : 251370 : switch (cpp_token_val_index (token))
16898 : : {
16899 : 0 : default:
16900 : 0 : gcc_unreachable ();
16901 : :
16902 : 17970 : case CPP_TOKEN_FLD_ARG_NO:
16903 : : /* An argument reference. */
16904 : 17970 : sec.u (token->val.macro_arg.arg_no);
16905 : 17970 : sec.cpp_node (token->val.macro_arg.spelling);
16906 : 17970 : break;
16907 : :
16908 : 55145 : case CPP_TOKEN_FLD_NODE:
16909 : : /* An identifier. */
16910 : 55145 : sec.cpp_node (token->val.node.node);
16911 : 55145 : if (token->val.node.spelling == token->val.node.node)
16912 : : /* The spelling will usually be the same. so optimize
16913 : : that. */
16914 : 55145 : sec.str (NULL, 0);
16915 : : else
16916 : 0 : sec.cpp_node (token->val.node.spelling);
16917 : : break;
16918 : :
16919 : : case CPP_TOKEN_FLD_NONE:
16920 : : break;
16921 : :
16922 : 77783 : case CPP_TOKEN_FLD_STR:
16923 : : /* A string, number or comment. Not always NUL terminated,
16924 : : we stream out in a single contatenation with embedded
16925 : : NULs as that's a safe default. */
16926 : 77783 : len += token->val.str.len + 1;
16927 : 77783 : sec.u (token->val.str.len);
16928 : 77783 : break;
16929 : :
16930 : 0 : case CPP_TOKEN_FLD_SOURCE:
16931 : 0 : case CPP_TOKEN_FLD_TOKEN_NO:
16932 : 0 : case CPP_TOKEN_FLD_PRAGMA:
16933 : : /* These do not occur inside a macro itself. */
16934 : 0 : gcc_unreachable ();
16935 : : }
16936 : : }
16937 : :
16938 : 115397 : if (len)
16939 : : {
16940 : 71943 : char *ptr = reinterpret_cast<char *> (sec.buf (len));
16941 : 71943 : len = 0;
16942 : 223929 : for (unsigned ix = 0; ix != macro->count; ix++)
16943 : : {
16944 : 151986 : const cpp_token *token = ¯o->exp.tokens[ix];
16945 : 151986 : if (cpp_token_val_index (token) == CPP_TOKEN_FLD_STR)
16946 : : {
16947 : 77783 : memcpy (ptr + len, token->val.str.text,
16948 : 77783 : token->val.str.len);
16949 : 77783 : len += token->val.str.len;
16950 : 77783 : ptr[len++] = 0;
16951 : : }
16952 : : }
16953 : : }
16954 : 115397 : }
16955 : :
16956 : : /* Read a macro definition. */
16957 : :
16958 : : cpp_macro *
16959 : 566 : module_state::read_define (bytes_in &sec, cpp_reader *reader) const
16960 : : {
16961 : 566 : unsigned count = sec.u ();
16962 : : /* We rely on knowing cpp_reader's hash table is ident_hash, and
16963 : : its subobject allocator is stringpool_ggc_alloc and that is just
16964 : : a wrapper for ggc_alloc_atomic. */
16965 : 566 : cpp_macro *macro
16966 : 1132 : = (cpp_macro *)ggc_alloc_atomic (sizeof (cpp_macro)
16967 : 566 : + sizeof (cpp_token) * (count - !!count));
16968 : 566 : memset (macro, 0, sizeof (cpp_macro) + sizeof (cpp_token) * (count - !!count));
16969 : :
16970 : 566 : macro->count = count;
16971 : 566 : macro->kind = cmk_macro;
16972 : 566 : macro->imported_p = true;
16973 : :
16974 : 566 : bytes_in::bits_in bits = sec.stream_bits ();
16975 : 566 : macro->fun_like = bits.b ();
16976 : 566 : macro->variadic = bits.b ();
16977 : 566 : macro->syshdr = bits.b ();
16978 : 566 : bits.bflush ();
16979 : :
16980 : 566 : macro->line = read_location (sec);
16981 : :
16982 : 566 : if (macro->fun_like)
16983 : : {
16984 : 104 : unsigned paramc = sec.u ();
16985 : 104 : cpp_hashnode **params
16986 : 104 : = (cpp_hashnode **)ggc_alloc_atomic (sizeof (cpp_hashnode *) * paramc);
16987 : 104 : macro->paramc = paramc;
16988 : 104 : macro->parm.params = params;
16989 : 235 : for (unsigned ix = 0; ix != paramc; ix++)
16990 : 131 : params[ix] = sec.cpp_node ();
16991 : : }
16992 : :
16993 : : unsigned len = 0;
16994 : 1826 : for (unsigned ix = 0; ix != count && !sec.get_overrun (); ix++)
16995 : : {
16996 : 1260 : cpp_token *token = ¯o->exp.tokens[ix];
16997 : 1260 : token->src_loc = read_location (sec);
16998 : 1260 : token->type = cpp_ttype (sec.u ());
16999 : 1260 : token->flags = sec.u ();
17000 : 1260 : switch (cpp_token_val_index (token))
17001 : : {
17002 : 0 : default:
17003 : 0 : sec.set_overrun ();
17004 : 0 : break;
17005 : :
17006 : 109 : case CPP_TOKEN_FLD_ARG_NO:
17007 : : /* An argument reference. */
17008 : 109 : {
17009 : 109 : unsigned arg_no = sec.u ();
17010 : 109 : if (arg_no - 1 >= macro->paramc)
17011 : 0 : sec.set_overrun ();
17012 : 109 : token->val.macro_arg.arg_no = arg_no;
17013 : 109 : token->val.macro_arg.spelling = sec.cpp_node ();
17014 : : }
17015 : 109 : break;
17016 : :
17017 : 328 : case CPP_TOKEN_FLD_NODE:
17018 : : /* An identifier. */
17019 : 328 : token->val.node.node = sec.cpp_node ();
17020 : 328 : token->val.node.spelling = sec.cpp_node ();
17021 : 328 : if (!token->val.node.spelling)
17022 : 328 : token->val.node.spelling = token->val.node.node;
17023 : : break;
17024 : :
17025 : : case CPP_TOKEN_FLD_NONE:
17026 : : break;
17027 : :
17028 : 306 : case CPP_TOKEN_FLD_STR:
17029 : : /* A string, number or comment. */
17030 : 306 : token->val.str.len = sec.u ();
17031 : 306 : len += token->val.str.len + 1;
17032 : 306 : break;
17033 : : }
17034 : : }
17035 : :
17036 : 566 : if (len)
17037 : 301 : if (const char *ptr = reinterpret_cast<const char *> (sec.buf (len)))
17038 : : {
17039 : : /* There should be a final NUL. */
17040 : 301 : if (ptr[len-1])
17041 : 0 : sec.set_overrun ();
17042 : : /* cpp_alloc_token_string will add a final NUL. */
17043 : 301 : const unsigned char *buf
17044 : 301 : = cpp_alloc_token_string (reader, (const unsigned char *)ptr, len - 1);
17045 : 301 : len = 0;
17046 : 790 : for (unsigned ix = 0; ix != count && !sec.get_overrun (); ix++)
17047 : : {
17048 : 489 : cpp_token *token = ¯o->exp.tokens[ix];
17049 : 489 : if (cpp_token_val_index (token) == CPP_TOKEN_FLD_STR)
17050 : : {
17051 : 306 : token->val.str.text = buf + len;
17052 : 306 : len += token->val.str.len;
17053 : 306 : if (buf[len++])
17054 : 0 : sec.set_overrun ();
17055 : : }
17056 : : }
17057 : : }
17058 : :
17059 : 566 : if (sec.get_overrun ())
17060 : 0 : return NULL;
17061 : : return macro;
17062 : 566 : }
17063 : :
17064 : : /* Exported macro data. */
17065 : : struct GTY(()) macro_export {
17066 : : cpp_macro *def;
17067 : : location_t undef_loc;
17068 : :
17069 : 193574 : macro_export ()
17070 : 193574 : :def (NULL), undef_loc (UNKNOWN_LOCATION)
17071 : : {
17072 : : }
17073 : : };
17074 : :
17075 : : /* Imported macro data. */
17076 : : class macro_import {
17077 : : public:
17078 : : struct slot {
17079 : : #if defined (WORDS_BIGENDIAN) && SIZEOF_VOID_P == 8
17080 : : int offset;
17081 : : #endif
17082 : : /* We need to ensure we don't use the LSB for representation, as
17083 : : that's the union discriminator below. */
17084 : : unsigned bits;
17085 : :
17086 : : #if !(defined (WORDS_BIGENDIAN) && SIZEOF_VOID_P == 8)
17087 : : int offset;
17088 : : #endif
17089 : :
17090 : : public:
17091 : : enum Layout {
17092 : : L_DEF = 1,
17093 : : L_UNDEF = 2,
17094 : : L_BOTH = 3,
17095 : : L_MODULE_SHIFT = 2
17096 : : };
17097 : :
17098 : : public:
17099 : : /* Not a regular ctor, because we put it in a union, and that's
17100 : : not allowed in C++ 98. */
17101 : 338037 : static slot ctor (unsigned module, unsigned defness)
17102 : : {
17103 : 338037 : gcc_checking_assert (defness);
17104 : 338037 : slot s;
17105 : 338037 : s.bits = defness | (module << L_MODULE_SHIFT);
17106 : 338037 : s.offset = -1;
17107 : 338037 : return s;
17108 : : }
17109 : :
17110 : : public:
17111 : 234105 : unsigned get_defness () const
17112 : : {
17113 : 234105 : return bits & L_BOTH;
17114 : : }
17115 : 198205 : unsigned get_module () const
17116 : : {
17117 : 198205 : return bits >> L_MODULE_SHIFT;
17118 : : }
17119 : 12 : void become_undef ()
17120 : : {
17121 : 12 : bits &= ~unsigned (L_DEF);
17122 : 12 : bits |= unsigned (L_UNDEF);
17123 : : }
17124 : : };
17125 : :
17126 : : private:
17127 : : typedef vec<slot, va_heap, vl_embed> ary_t;
17128 : : union either {
17129 : : /* Discriminated by bits 0|1 != 0. The expected case is that
17130 : : there will be exactly one slot per macro, hence the effort of
17131 : : packing that. */
17132 : : ary_t *ary;
17133 : : slot single;
17134 : : } u;
17135 : :
17136 : : public:
17137 : 254743 : macro_import ()
17138 : 254743 : {
17139 : 254743 : u.ary = NULL;
17140 : : }
17141 : :
17142 : : private:
17143 : 13103329 : bool single_p () const
17144 : : {
17145 : 13103329 : return u.single.bits & slot::L_BOTH;
17146 : : }
17147 : 13358081 : bool occupied_p () const
17148 : : {
17149 : 13358081 : return u.ary != NULL;
17150 : : }
17151 : :
17152 : : public:
17153 : 1656 : unsigned length () const
17154 : : {
17155 : 1656 : gcc_checking_assert (occupied_p ());
17156 : 1656 : return single_p () ? 1 : u.ary->length ();
17157 : : }
17158 : 12825377 : slot &operator[] (unsigned ix)
17159 : : {
17160 : 12825377 : gcc_checking_assert (occupied_p ());
17161 : 12825377 : if (single_p ())
17162 : : {
17163 : 12580811 : gcc_checking_assert (!ix);
17164 : 12580811 : return u.single;
17165 : : }
17166 : : else
17167 : 244566 : return (*u.ary)[ix];
17168 : : }
17169 : :
17170 : : public:
17171 : : slot &exported ();
17172 : : slot &append (unsigned module, unsigned defness);
17173 : : };
17174 : :
17175 : : /* O is a new import to append to the list for. If we're an empty
17176 : : set, initialize us. */
17177 : :
17178 : : macro_import::slot &
17179 : 338037 : macro_import::append (unsigned module, unsigned defness)
17180 : : {
17181 : 338037 : if (!occupied_p ())
17182 : : {
17183 : 254743 : u.single = slot::ctor (module, defness);
17184 : 254743 : return u.single;
17185 : : }
17186 : : else
17187 : : {
17188 : 83294 : bool single = single_p ();
17189 : 83294 : ary_t *m = single ? NULL : u.ary;
17190 : 83294 : vec_safe_reserve (m, 1 + single);
17191 : 83294 : if (single)
17192 : 83291 : m->quick_push (u.single);
17193 : 83294 : u.ary = m;
17194 : 83294 : return *u.ary->quick_push (slot::ctor (module, defness));
17195 : : }
17196 : : }
17197 : :
17198 : : /* We're going to export something. Make sure the first import slot
17199 : : is us. */
17200 : :
17201 : : macro_import::slot &
17202 : 193011 : macro_import::exported ()
17203 : : {
17204 : 193011 : if (occupied_p () && !(*this)[0].get_module ())
17205 : : {
17206 : 9 : slot &res = (*this)[0];
17207 : 9 : res.bits |= slot::L_DEF;
17208 : 9 : return res;
17209 : : }
17210 : :
17211 : 193002 : slot *a = &append (0, slot::L_DEF);
17212 : 193002 : if (!single_p ())
17213 : : {
17214 : 79207 : slot &f = (*this)[0];
17215 : 79207 : std::swap (f, *a);
17216 : 79207 : a = &f;
17217 : : }
17218 : : return *a;
17219 : : }
17220 : :
17221 : : /* The import (&exported) macros. cpp_hasnode's deferred field
17222 : : indexes this array (offset by 1, so zero means 'not present'. */
17223 : :
17224 : : static vec<macro_import, va_heap, vl_embed> *macro_imports;
17225 : :
17226 : : /* The exported macros. A macro_import slot's zeroth element's offset
17227 : : indexes this array. If the zeroth slot is not for module zero,
17228 : : there is no export. */
17229 : :
17230 : : static GTY(()) vec<macro_export, va_gc> *macro_exports;
17231 : :
17232 : : /* The reachable set of header imports from this TU. */
17233 : :
17234 : : static GTY(()) bitmap headers;
17235 : :
17236 : : /* Get the (possibly empty) macro imports for NODE. */
17237 : :
17238 : : static macro_import &
17239 : 258839 : get_macro_imports (cpp_hashnode *node)
17240 : : {
17241 : 258839 : if (node->deferred)
17242 : 4096 : return (*macro_imports)[node->deferred - 1];
17243 : :
17244 : 254743 : vec_safe_reserve (macro_imports, 1);
17245 : 254743 : node->deferred = macro_imports->length () + 1;
17246 : 254743 : return *vec_safe_push (macro_imports, macro_import ());
17247 : : }
17248 : :
17249 : : /* Get the macro export for export EXP of NODE. */
17250 : :
17251 : : static macro_export &
17252 : 193011 : get_macro_export (macro_import::slot &slot)
17253 : : {
17254 : 193011 : if (slot.offset >= 0)
17255 : 9 : return (*macro_exports)[slot.offset];
17256 : :
17257 : 193002 : vec_safe_reserve (macro_exports, 1);
17258 : 193002 : slot.offset = macro_exports->length ();
17259 : 193002 : return *macro_exports->quick_push (macro_export ());
17260 : : }
17261 : :
17262 : : /* If NODE is an exportable macro, add it to the export set. */
17263 : :
17264 : : static int
17265 : 3098135 : maybe_add_macro (cpp_reader *, cpp_hashnode *node, void *data_)
17266 : : {
17267 : 3098135 : bool exporting = false;
17268 : :
17269 : 3098135 : if (cpp_user_macro_p (node))
17270 : 434355 : if (cpp_macro *macro = node->value.macro)
17271 : : /* Ignore imported, builtins, command line and forced header macros. */
17272 : 433129 : if (!macro->imported_p
17273 : 433129 : && !macro->lazy && macro->line >= spans.main_start ())
17274 : : {
17275 : 113804 : gcc_checking_assert (macro->kind == cmk_macro);
17276 : : /* I don't want to deal with this corner case, that I suspect is
17277 : : a devil's advocate reading of the standard. */
17278 : 113804 : gcc_checking_assert (!macro->extra_tokens);
17279 : :
17280 : 113804 : macro_import::slot &slot = get_macro_imports (node).exported ();
17281 : 113804 : macro_export &exp = get_macro_export (slot);
17282 : 113804 : exp.def = macro;
17283 : 113804 : exporting = true;
17284 : : }
17285 : :
17286 : 2984331 : if (!exporting && node->deferred)
17287 : : {
17288 : 1633 : macro_import &imports = (*macro_imports)[node->deferred - 1];
17289 : 1633 : macro_import::slot &slot = imports[0];
17290 : 1633 : if (!slot.get_module ())
17291 : : {
17292 : 1600 : gcc_checking_assert (slot.get_defness ());
17293 : : exporting = true;
17294 : : }
17295 : : }
17296 : :
17297 : 113804 : if (exporting)
17298 : 115404 : static_cast<vec<cpp_hashnode *> *> (data_)->safe_push (node);
17299 : :
17300 : 3098135 : return 1; /* Don't stop. */
17301 : : }
17302 : :
17303 : : /* Order cpp_hashnodes A_ and B_ by their exported macro locations. */
17304 : :
17305 : : static int
17306 : 6158308 : macro_loc_cmp (const void *a_, const void *b_)
17307 : : {
17308 : 6158308 : const cpp_hashnode *node_a = *(const cpp_hashnode *const *)a_;
17309 : 6158308 : macro_import &import_a = (*macro_imports)[node_a->deferred - 1];
17310 : 6158308 : const macro_export &export_a = (*macro_exports)[import_a[0].offset];
17311 : 6158308 : location_t loc_a = export_a.def ? export_a.def->line : export_a.undef_loc;
17312 : :
17313 : 6158308 : const cpp_hashnode *node_b = *(const cpp_hashnode *const *)b_;
17314 : 6158308 : macro_import &import_b = (*macro_imports)[node_b->deferred - 1];
17315 : 6158308 : const macro_export &export_b = (*macro_exports)[import_b[0].offset];
17316 : 6158308 : location_t loc_b = export_b.def ? export_b.def->line : export_b.undef_loc;
17317 : :
17318 : 6158308 : if (loc_a < loc_b)
17319 : : return +1;
17320 : 3162316 : else if (loc_a > loc_b)
17321 : : return -1;
17322 : : else
17323 : 0 : return 0;
17324 : : }
17325 : :
17326 : : /* Gather the macro definitions and undefinitions that we will need to
17327 : : write out. */
17328 : :
17329 : : vec<cpp_hashnode *> *
17330 : 689 : module_state::prepare_macros (cpp_reader *reader)
17331 : : {
17332 : 689 : vec<cpp_hashnode *> *macros;
17333 : 689 : vec_alloc (macros, 100);
17334 : :
17335 : 689 : cpp_forall_identifiers (reader, maybe_add_macro, macros);
17336 : :
17337 : 713 : dump (dumper::MACRO) && dump ("No more than %u macros", macros->length ());
17338 : :
17339 : 689 : macros->qsort (macro_loc_cmp);
17340 : :
17341 : : // Note the locations.
17342 : 116782 : for (unsigned ix = macros->length (); ix--;)
17343 : : {
17344 : 115404 : cpp_hashnode *node = (*macros)[ix];
17345 : 115404 : macro_import::slot &slot = (*macro_imports)[node->deferred - 1][0];
17346 : 115404 : macro_export &mac = (*macro_exports)[slot.offset];
17347 : :
17348 : 115404 : if (IDENTIFIER_KEYWORD_P (identifier (node)))
17349 : 1 : continue;
17350 : :
17351 : 115403 : if (mac.undef_loc != UNKNOWN_LOCATION)
17352 : 12 : note_location (mac.undef_loc);
17353 : 115403 : if (mac.def)
17354 : : {
17355 : 115397 : note_location (mac.def->line);
17356 : 366767 : for (unsigned ix = 0; ix != mac.def->count; ix++)
17357 : 251370 : note_location (mac.def->exp.tokens[ix].src_loc);
17358 : : }
17359 : : }
17360 : :
17361 : 689 : return macros;
17362 : : }
17363 : :
17364 : : /* Write out the exported defines. This is two sections, one
17365 : : containing the definitions, the other a table of node names. */
17366 : :
17367 : : unsigned
17368 : 689 : module_state::write_macros (elf_out *to, vec<cpp_hashnode *> *macros,
17369 : : unsigned *crc_p)
17370 : : {
17371 : 746 : dump () && dump ("Writing macros");
17372 : 689 : dump.indent ();
17373 : :
17374 : : /* Write the defs */
17375 : 689 : bytes_out sec (to);
17376 : 689 : sec.begin ();
17377 : :
17378 : 689 : unsigned count = 0;
17379 : 116782 : for (unsigned ix = macros->length (); ix--;)
17380 : : {
17381 : 115404 : cpp_hashnode *node = (*macros)[ix];
17382 : 115404 : macro_import::slot &slot = (*macro_imports)[node->deferred - 1][0];
17383 : 115404 : gcc_assert (!slot.get_module () && slot.get_defness ());
17384 : :
17385 : 115404 : macro_export &mac = (*macro_exports)[slot.offset];
17386 : 115404 : gcc_assert (!!(slot.get_defness () & macro_import::slot::L_UNDEF)
17387 : : == (mac.undef_loc != UNKNOWN_LOCATION)
17388 : : && !!(slot.get_defness () & macro_import::slot::L_DEF)
17389 : : == (mac.def != NULL));
17390 : :
17391 : 115404 : if (IDENTIFIER_KEYWORD_P (identifier (node)))
17392 : : {
17393 : 1 : warning_at (mac.def->line, 0,
17394 : : "not exporting %<#define %E%> as it is a keyword",
17395 : : identifier (node));
17396 : 1 : slot.offset = 0;
17397 : 1 : continue;
17398 : : }
17399 : :
17400 : 115403 : count++;
17401 : 115403 : slot.offset = sec.pos;
17402 : 115403 : dump (dumper::MACRO)
17403 : 24 : && dump ("Writing macro %s%s%s %I at %u",
17404 : 24 : slot.get_defness () & macro_import::slot::L_UNDEF
17405 : : ? "#undef" : "",
17406 : 24 : slot.get_defness () == macro_import::slot::L_BOTH
17407 : : ? " & " : "",
17408 : 24 : slot.get_defness () & macro_import::slot::L_DEF
17409 : : ? "#define" : "",
17410 : : identifier (node), slot.offset);
17411 : 115403 : if (mac.undef_loc != UNKNOWN_LOCATION)
17412 : 12 : write_location (sec, mac.undef_loc);
17413 : 115403 : if (mac.def)
17414 : 115397 : write_define (sec, mac.def);
17415 : : }
17416 : 689 : if (count)
17417 : : // We may have ended on a tokenless macro with a very short
17418 : : // location, that will cause problems reading its bit flags.
17419 : 140 : sec.u (0);
17420 : 689 : sec.end (to, to->name (MOD_SNAME_PFX ".def"), crc_p);
17421 : :
17422 : 689 : if (count)
17423 : : {
17424 : : /* Write the table. */
17425 : 140 : bytes_out sec (to);
17426 : 140 : sec.begin ();
17427 : 140 : sec.u (count);
17428 : :
17429 : 115683 : for (unsigned ix = macros->length (); ix--;)
17430 : : {
17431 : 115403 : const cpp_hashnode *node = (*macros)[ix];
17432 : 115403 : macro_import::slot &slot = (*macro_imports)[node->deferred - 1][0];
17433 : :
17434 : 115403 : if (slot.offset)
17435 : : {
17436 : 115403 : sec.cpp_node (node);
17437 : 115403 : sec.u (slot.get_defness ());
17438 : 115403 : sec.u (slot.offset);
17439 : : }
17440 : : }
17441 : 140 : sec.end (to, to->name (MOD_SNAME_PFX ".mac"), crc_p);
17442 : 140 : }
17443 : :
17444 : 689 : dump.outdent ();
17445 : 689 : return count;
17446 : 689 : }
17447 : :
17448 : : bool
17449 : 729 : module_state::read_macros ()
17450 : : {
17451 : : /* Get the def section. */
17452 : 729 : if (!slurp->macro_defs.begin (loc, from (), MOD_SNAME_PFX ".def"))
17453 : : return false;
17454 : :
17455 : : /* Get the tbl section, if there are defs. */
17456 : 729 : if (slurp->macro_defs.more_p ()
17457 : 729 : && !slurp->macro_tbl.begin (loc, from (), MOD_SNAME_PFX ".mac"))
17458 : : return false;
17459 : :
17460 : : return true;
17461 : : }
17462 : :
17463 : : /* Install the macro name table. */
17464 : :
17465 : : void
17466 : 735 : module_state::install_macros ()
17467 : : {
17468 : 735 : bytes_in &sec = slurp->macro_tbl;
17469 : 735 : if (!sec.size)
17470 : : return;
17471 : :
17472 : 199 : dump () && dump ("Reading macro table %M", this);
17473 : 178 : dump.indent ();
17474 : :
17475 : 178 : unsigned count = sec.u ();
17476 : 199 : dump () && dump ("%u macros", count);
17477 : 145213 : while (count--)
17478 : : {
17479 : 145035 : cpp_hashnode *node = sec.cpp_node ();
17480 : 145035 : macro_import &imp = get_macro_imports (node);
17481 : 145035 : unsigned flags = sec.u () & macro_import::slot::L_BOTH;
17482 : 145035 : if (!flags)
17483 : 0 : sec.set_overrun ();
17484 : :
17485 : 145035 : if (sec.get_overrun ())
17486 : : break;
17487 : :
17488 : 145035 : macro_import::slot &slot = imp.append (mod, flags);
17489 : 145035 : slot.offset = sec.u ();
17490 : :
17491 : 145035 : dump (dumper::MACRO)
17492 : 84 : && dump ("Read %s macro %s%s%s %I at %u",
17493 : 30 : imp.length () > 1 ? "add" : "new",
17494 : 30 : flags & macro_import::slot::L_UNDEF ? "#undef" : "",
17495 : : flags == macro_import::slot::L_BOTH ? " & " : "",
17496 : 30 : flags & macro_import::slot::L_DEF ? "#define" : "",
17497 : : identifier (node), slot.offset);
17498 : :
17499 : : /* We'll leak an imported definition's TOKEN_FLD_STR's data
17500 : : here. But that only happens when we've had to resolve the
17501 : : deferred macro before this import -- why are you doing
17502 : : that? */
17503 : 145035 : if (cpp_macro *cur = cpp_set_deferred_macro (node))
17504 : 79195 : if (!cur->imported_p)
17505 : : {
17506 : 79195 : macro_import::slot &slot = imp.exported ();
17507 : 79195 : macro_export &exp = get_macro_export (slot);
17508 : 79195 : exp.def = cur;
17509 : 224408 : dump (dumper::MACRO)
17510 : 0 : && dump ("Saving current #define %I", identifier (node));
17511 : : }
17512 : : }
17513 : :
17514 : : /* We're now done with the table. */
17515 : 178 : elf_in::release (slurp->from, sec);
17516 : :
17517 : 178 : dump.outdent ();
17518 : : }
17519 : :
17520 : : /* Import the transitive macros. */
17521 : :
17522 : : void
17523 : 706 : module_state::import_macros ()
17524 : : {
17525 : 706 : bitmap_ior_into (headers, slurp->headers);
17526 : :
17527 : 706 : bitmap_iterator bititer;
17528 : 706 : unsigned bitnum;
17529 : 1441 : EXECUTE_IF_SET_IN_BITMAP (slurp->headers, 0, bitnum, bititer)
17530 : 735 : (*modules)[bitnum]->install_macros ();
17531 : 706 : }
17532 : :
17533 : : /* NODE is being undefined at LOC. Record it in the export table, if
17534 : : necessary. */
17535 : :
17536 : : void
17537 : 286131 : module_state::undef_macro (cpp_reader *, location_t loc, cpp_hashnode *node)
17538 : : {
17539 : 286131 : if (!node->deferred)
17540 : : /* The macro is not imported, so our undef is irrelevant. */
17541 : : return;
17542 : :
17543 : 12 : unsigned n = dump.push (NULL);
17544 : :
17545 : 12 : macro_import::slot &slot = (*macro_imports)[node->deferred - 1].exported ();
17546 : 12 : macro_export &exp = get_macro_export (slot);
17547 : :
17548 : 12 : exp.undef_loc = loc;
17549 : 12 : slot.become_undef ();
17550 : 12 : exp.def = NULL;
17551 : :
17552 : 18 : dump (dumper::MACRO) && dump ("Recording macro #undef %I", identifier (node));
17553 : :
17554 : 12 : dump.pop (n);
17555 : : }
17556 : :
17557 : : /* NODE is a deferred macro node. Determine the definition and return
17558 : : it, with NULL if undefined. May issue diagnostics.
17559 : :
17560 : : This can leak memory, when merging declarations -- the string
17561 : : contents (TOKEN_FLD_STR) of each definition are allocated in
17562 : : unreclaimable cpp objstack. Only one will win. However, I do not
17563 : : expect this to be common -- mostly macros have a single point of
17564 : : definition. Perhaps we could restore the objstack to its position
17565 : : after the first imported definition (if that wins)? The macros
17566 : : themselves are GC'd. */
17567 : :
17568 : : cpp_macro *
17569 : 542 : module_state::deferred_macro (cpp_reader *reader, location_t loc,
17570 : : cpp_hashnode *node)
17571 : : {
17572 : 542 : macro_import &imports = (*macro_imports)[node->deferred - 1];
17573 : :
17574 : 542 : unsigned n = dump.push (NULL);
17575 : 548 : dump (dumper::MACRO) && dump ("Deferred macro %I", identifier (node));
17576 : :
17577 : 542 : bitmap visible (BITMAP_GGC_ALLOC ());
17578 : :
17579 : 542 : if (!((imports[0].get_defness () & macro_import::slot::L_UNDEF)
17580 : 0 : && !imports[0].get_module ()))
17581 : : {
17582 : : /* Calculate the set of visible header imports. */
17583 : 542 : bitmap_copy (visible, headers);
17584 : 1518 : for (unsigned ix = imports.length (); ix--;)
17585 : : {
17586 : 976 : const macro_import::slot &slot = imports[ix];
17587 : 976 : unsigned mod = slot.get_module ();
17588 : 976 : if ((slot.get_defness () & macro_import::slot::L_UNDEF)
17589 : 976 : && bitmap_bit_p (visible, mod))
17590 : : {
17591 : 12 : bitmap arg = mod ? (*modules)[mod]->slurp->headers : headers;
17592 : 12 : bitmap_and_compl_into (visible, arg);
17593 : 12 : bitmap_set_bit (visible, mod);
17594 : : }
17595 : : }
17596 : : }
17597 : 542 : bitmap_set_bit (visible, 0);
17598 : :
17599 : : /* Now find the macros that are still visible. */
17600 : 542 : bool failed = false;
17601 : 542 : cpp_macro *def = NULL;
17602 : 542 : vec<macro_export> defs;
17603 : 542 : defs.create (imports.length ());
17604 : 1518 : for (unsigned ix = imports.length (); ix--;)
17605 : : {
17606 : 976 : const macro_import::slot &slot = imports[ix];
17607 : 976 : unsigned mod = slot.get_module ();
17608 : 976 : if (bitmap_bit_p (visible, mod))
17609 : : {
17610 : 964 : macro_export *pushed = NULL;
17611 : 964 : if (mod)
17612 : : {
17613 : 572 : const module_state *imp = (*modules)[mod];
17614 : 572 : bytes_in &sec = imp->slurp->macro_defs;
17615 : 572 : if (!sec.get_overrun ())
17616 : : {
17617 : 572 : dump (dumper::MACRO)
17618 : 6 : && dump ("Reading macro %s%s%s %I module %M at %u",
17619 : 6 : slot.get_defness () & macro_import::slot::L_UNDEF
17620 : : ? "#undef" : "",
17621 : 6 : slot.get_defness () == macro_import::slot::L_BOTH
17622 : : ? " & " : "",
17623 : 6 : slot.get_defness () & macro_import::slot::L_DEF
17624 : : ? "#define" : "",
17625 : 6 : identifier (node), imp, slot.offset);
17626 : 572 : sec.random_access (slot.offset);
17627 : :
17628 : 572 : macro_export exp;
17629 : 572 : if (slot.get_defness () & macro_import::slot::L_UNDEF)
17630 : 12 : exp.undef_loc = imp->read_location (sec);
17631 : 572 : if (slot.get_defness () & macro_import::slot::L_DEF)
17632 : 566 : exp.def = imp->read_define (sec, reader);
17633 : 572 : if (sec.get_overrun ())
17634 : 0 : error_at (loc, "macro definitions of %qE corrupted",
17635 : 0 : imp->name);
17636 : : else
17637 : 572 : pushed = defs.quick_push (exp);
17638 : : }
17639 : : }
17640 : : else
17641 : 392 : pushed = defs.quick_push ((*macro_exports)[slot.offset]);
17642 : 964 : if (pushed && pushed->def)
17643 : : {
17644 : 958 : if (!def)
17645 : : def = pushed->def;
17646 : 419 : else if (cpp_compare_macros (def, pushed->def))
17647 : 976 : failed = true;
17648 : : }
17649 : : }
17650 : : }
17651 : :
17652 : 542 : if (failed)
17653 : : {
17654 : : /* If LOC is the first loc, this is the end of file check, which
17655 : : is a warning. */
17656 : 15 : if (loc == MAP_START_LOCATION (LINEMAPS_ORDINARY_MAP_AT (line_table, 0)))
17657 : 9 : warning_at (loc, OPT_Winvalid_imported_macros,
17658 : : "inconsistent imported macro definition %qE",
17659 : : identifier (node));
17660 : : else
17661 : 6 : error_at (loc, "inconsistent imported macro definition %qE",
17662 : : identifier (node));
17663 : 60 : for (unsigned ix = defs.length (); ix--;)
17664 : : {
17665 : 30 : macro_export &exp = defs[ix];
17666 : 30 : if (exp.undef_loc)
17667 : 0 : inform (exp.undef_loc, "%<#undef %E%>", identifier (node));
17668 : 30 : if (exp.def)
17669 : 30 : inform (exp.def->line, "%<#define %s%>",
17670 : : cpp_macro_definition (reader, node, exp.def));
17671 : : }
17672 : : def = NULL;
17673 : : }
17674 : :
17675 : 542 : defs.release ();
17676 : :
17677 : 542 : dump.pop (n);
17678 : :
17679 : 542 : return def;
17680 : : }
17681 : :
17682 : : /* Stream the static aggregates. Sadly some headers (ahem:
17683 : : iostream) contain static vars, and rely on them to run global
17684 : : ctors. */
17685 : : unsigned
17686 : 689 : module_state::write_inits (elf_out *to, depset::hash &table, unsigned *crc_ptr)
17687 : : {
17688 : 689 : if (!static_aggregates && !tls_aggregates)
17689 : : return 0;
17690 : :
17691 : 26 : dump () && dump ("Writing initializers");
17692 : 26 : dump.indent ();
17693 : :
17694 : 26 : static_aggregates = nreverse (static_aggregates);
17695 : 26 : tls_aggregates = nreverse (tls_aggregates);
17696 : :
17697 : 26 : unsigned count = 0;
17698 : 26 : trees_out sec (to, this, table, ~0u);
17699 : 26 : sec.begin ();
17700 : :
17701 : 26 : tree list = static_aggregates;
17702 : 78 : for (int passes = 0; passes != 2; passes++)
17703 : : {
17704 : 78 : for (tree init = list; init; init = TREE_CHAIN (init))
17705 : 26 : if (TREE_LANG_FLAG_0 (init))
17706 : : {
17707 : 26 : tree decl = TREE_VALUE (init);
17708 : :
17709 : 26 : dump ("Initializer:%u for %N", count, decl);
17710 : 26 : sec.tree_node (decl);
17711 : 26 : ++count;
17712 : : }
17713 : :
17714 : 52 : list = tls_aggregates;
17715 : : }
17716 : :
17717 : 26 : sec.end (to, to->name (MOD_SNAME_PFX ".ini"), crc_ptr);
17718 : 26 : dump.outdent ();
17719 : :
17720 : 26 : return count;
17721 : 26 : }
17722 : :
17723 : : /* We have to defer some post-load processing until we've completed
17724 : : reading, because they can cause more reading. */
17725 : :
17726 : : static void
17727 : 7086 : post_load_processing ()
17728 : : {
17729 : : /* We mustn't cause a GC, our caller should have arranged for that
17730 : : not to happen. */
17731 : 7086 : gcc_checking_assert (function_depth);
17732 : :
17733 : 7086 : if (!post_load_decls)
17734 : : return;
17735 : :
17736 : 4757 : tree old_cfd = current_function_decl;
17737 : 4757 : struct function *old_cfun = cfun;
17738 : 15411 : while (post_load_decls->length ())
17739 : : {
17740 : 10654 : tree decl = post_load_decls->pop ();
17741 : :
17742 : 10698 : dump () && dump ("Post-load processing of %N", decl);
17743 : :
17744 : 10654 : gcc_checking_assert (DECL_ABSTRACT_P (decl));
17745 : : /* Cloning can cause loading -- specifically operator delete for
17746 : : the deleting dtor. */
17747 : 10654 : maybe_clone_body (decl);
17748 : : }
17749 : :
17750 : 4757 : cfun = old_cfun;
17751 : 4757 : current_function_decl = old_cfd;
17752 : : }
17753 : :
17754 : : bool
17755 : 26 : module_state::read_inits (unsigned count)
17756 : : {
17757 : 26 : trees_in sec (this);
17758 : 26 : if (!sec.begin (loc, from (), from ()->find (MOD_SNAME_PFX ".ini")))
17759 : : return false;
17760 : 37 : dump () && dump ("Reading %u initializers", count);
17761 : 26 : dump.indent ();
17762 : :
17763 : 26 : lazy_snum = ~0u;
17764 : 52 : for (unsigned ix = 0; ix != count; ix++)
17765 : : {
17766 : : /* Merely referencing the decl causes its initializer to be read
17767 : : and added to the correct list. */
17768 : 26 : tree decl = sec.tree_node ();
17769 : :
17770 : 26 : if (sec.get_overrun ())
17771 : : break;
17772 : 26 : if (decl)
17773 : 26 : dump ("Initializer:%u for %N", count, decl);
17774 : : }
17775 : 26 : lazy_snum = 0;
17776 : 26 : post_load_processing ();
17777 : 26 : dump.outdent ();
17778 : 26 : if (!sec.end (from ()))
17779 : : return false;
17780 : : return true;
17781 : 26 : }
17782 : :
17783 : : void
17784 : 1818 : module_state::write_counts (elf_out *to, unsigned counts[MSC_HWM],
17785 : : unsigned *crc_ptr)
17786 : : {
17787 : 1818 : bytes_out cfg (to);
17788 : :
17789 : 1818 : cfg.begin ();
17790 : :
17791 : 16362 : for (unsigned ix = MSC_HWM; ix--;)
17792 : 14544 : cfg.u (counts[ix]);
17793 : :
17794 : 1818 : if (dump ())
17795 : : {
17796 : 228 : dump ("Cluster sections are [%u,%u)",
17797 : : counts[MSC_sec_lwm], counts[MSC_sec_hwm]);
17798 : 228 : dump ("Bindings %u", counts[MSC_bindings]);
17799 : 228 : dump ("Pendings %u", counts[MSC_pendings]);
17800 : 228 : dump ("Entities %u", counts[MSC_entities]);
17801 : 228 : dump ("Namespaces %u", counts[MSC_namespaces]);
17802 : 228 : dump ("Macros %u", counts[MSC_macros]);
17803 : 228 : dump ("Initializers %u", counts[MSC_inits]);
17804 : : }
17805 : :
17806 : 1818 : cfg.end (to, to->name (MOD_SNAME_PFX ".cnt"), crc_ptr);
17807 : 1818 : }
17808 : :
17809 : : bool
17810 : 1980 : module_state::read_counts (unsigned counts[MSC_HWM])
17811 : : {
17812 : 1980 : bytes_in cfg;
17813 : :
17814 : 1980 : if (!cfg.begin (loc, from (), MOD_SNAME_PFX ".cnt"))
17815 : : return false;
17816 : :
17817 : 17820 : for (unsigned ix = MSC_HWM; ix--;)
17818 : 15840 : counts[ix] = cfg.u ();
17819 : :
17820 : 1980 : if (dump ())
17821 : : {
17822 : 446 : dump ("Declaration sections are [%u,%u)",
17823 : : counts[MSC_sec_lwm], counts[MSC_sec_hwm]);
17824 : 446 : dump ("Bindings %u", counts[MSC_bindings]);
17825 : 446 : dump ("Pendings %u", counts[MSC_pendings]);
17826 : 446 : dump ("Entities %u", counts[MSC_entities]);
17827 : 446 : dump ("Namespaces %u", counts[MSC_namespaces]);
17828 : 446 : dump ("Macros %u", counts[MSC_macros]);
17829 : 446 : dump ("Initializers %u", counts[MSC_inits]);
17830 : : }
17831 : :
17832 : 1980 : return cfg.end (from ());
17833 : 1980 : }
17834 : :
17835 : : /* Tool configuration: MOD_SNAME_PFX .config
17836 : :
17837 : : This is data that confirms current state (or fails). */
17838 : :
17839 : : void
17840 : 1824 : module_state::write_config (elf_out *to, module_state_config &config,
17841 : : unsigned inner_crc)
17842 : : {
17843 : 1824 : bytes_out cfg (to);
17844 : :
17845 : 1824 : cfg.begin ();
17846 : :
17847 : : /* Write version and inner crc as u32 values, for easier
17848 : : debug inspection. */
17849 : 2052 : dump () && dump ("Writing version=%V, inner_crc=%x",
17850 : : MODULE_VERSION, inner_crc);
17851 : 1824 : cfg.u32 (unsigned (MODULE_VERSION));
17852 : 1824 : cfg.u32 (inner_crc);
17853 : :
17854 : 1824 : cfg.u (to->name (is_header () ? "" : get_flatname ()));
17855 : :
17856 : : /* Configuration. */
17857 : 2052 : dump () && dump ("Writing target='%s', host='%s'",
17858 : : TARGET_MACHINE, HOST_MACHINE);
17859 : 1824 : unsigned target = to->name (TARGET_MACHINE);
17860 : 1824 : unsigned host = (!strcmp (TARGET_MACHINE, HOST_MACHINE)
17861 : : ? target : to->name (HOST_MACHINE));
17862 : 1824 : cfg.u (target);
17863 : 1824 : cfg.u (host);
17864 : :
17865 : 1824 : cfg.str (config.dialect_str);
17866 : 1824 : cfg.u (extensions);
17867 : :
17868 : : /* Global tree information. We write the globals crc separately,
17869 : : rather than mix it directly into the overall crc, as it is used
17870 : : to ensure data match between instances of the compiler, not
17871 : : integrity of the file. */
17872 : 2052 : dump () && dump ("Writing globals=%u, crc=%x",
17873 : : fixed_trees->length (), global_crc);
17874 : 1824 : cfg.u (fixed_trees->length ());
17875 : 1824 : cfg.u32 (global_crc);
17876 : :
17877 : 1824 : if (is_partition ())
17878 : 93 : cfg.u (is_interface ());
17879 : :
17880 : 1824 : cfg.u (config.num_imports);
17881 : 1824 : cfg.u (config.num_partitions);
17882 : 1824 : cfg.u (config.num_entities);
17883 : :
17884 : 1824 : cfg.u (config.ordinary_locs);
17885 : 1824 : cfg.u (config.macro_locs);
17886 : 1824 : cfg.u (config.loc_range_bits);
17887 : :
17888 : 1824 : cfg.u (config.active_init);
17889 : :
17890 : : /* Now generate CRC, we'll have incorporated the inner CRC because
17891 : : of its serialization above. */
17892 : 1824 : cfg.end (to, to->name (MOD_SNAME_PFX ".cfg"), &crc);
17893 : 2052 : dump () && dump ("Writing CRC=%x", crc);
17894 : 1824 : }
17895 : :
17896 : : void
17897 : 37 : module_state::note_cmi_name ()
17898 : : {
17899 : 37 : if (!cmi_noted_p && filename)
17900 : : {
17901 : 37 : cmi_noted_p = true;
17902 : 37 : inform (loc, "compiled module file is %qs",
17903 : : maybe_add_cmi_prefix (filename));
17904 : : }
17905 : 37 : }
17906 : :
17907 : : bool
17908 : 2029 : module_state::read_config (module_state_config &config)
17909 : : {
17910 : 2029 : bytes_in cfg;
17911 : :
17912 : 2029 : if (!cfg.begin (loc, from (), MOD_SNAME_PFX ".cfg"))
17913 : : return false;
17914 : :
17915 : : /* Check version. */
17916 : 2029 : unsigned my_ver = MODULE_VERSION;
17917 : 2029 : unsigned their_ver = cfg.u32 ();
17918 : 2475 : dump () && dump (my_ver == their_ver ? "Version %V"
17919 : : : "Expecting %V found %V", my_ver, their_ver);
17920 : 2029 : if (their_ver != my_ver)
17921 : : {
17922 : : /* The compiler versions differ. Close enough? */
17923 : 0 : verstr_t my_string, their_string;
17924 : :
17925 : 0 : version2string (my_ver, my_string);
17926 : 0 : version2string (their_ver, their_string);
17927 : :
17928 : : /* Reject when either is non-experimental or when experimental
17929 : : major versions differ. */
17930 : 0 : bool reject_p = ((!IS_EXPERIMENTAL (my_ver)
17931 : : || !IS_EXPERIMENTAL (their_ver)
17932 : 0 : || MODULE_MAJOR (my_ver) != MODULE_MAJOR (their_ver))
17933 : : /* The 'I know what I'm doing' switch. */
17934 : 0 : && !flag_module_version_ignore);
17935 : 0 : bool inform_p = true;
17936 : 0 : if (reject_p)
17937 : : {
17938 : 0 : cfg.set_overrun ();
17939 : 0 : error_at (loc, "compiled module is %sversion %s",
17940 : : IS_EXPERIMENTAL (their_ver) ? "experimental " : "",
17941 : : their_string);
17942 : : }
17943 : : else
17944 : 0 : inform_p = warning_at (loc, 0, "compiled module is %sversion %s",
17945 : : IS_EXPERIMENTAL (their_ver) ? "experimental " : "",
17946 : : their_string);
17947 : :
17948 : 0 : if (inform_p)
17949 : : {
17950 : 0 : inform (loc, "compiler is %sversion %s%s%s",
17951 : : IS_EXPERIMENTAL (my_ver) ? "experimental " : "",
17952 : : my_string,
17953 : 0 : reject_p ? "" : flag_module_version_ignore
17954 : 0 : ? ", be it on your own head!" : ", close enough?",
17955 : : reject_p ? "" : " \xc2\xaf\\_(\xe3\x83\x84)_/\xc2\xaf");
17956 : 0 : note_cmi_name ();
17957 : : }
17958 : :
17959 : 0 : if (reject_p)
17960 : 0 : goto done;
17961 : : }
17962 : :
17963 : : /* We wrote the inner crc merely to merge it, so simply read it
17964 : : back and forget it. */
17965 : 2029 : cfg.u32 ();
17966 : :
17967 : : /* Check module name. */
17968 : 2029 : {
17969 : 2029 : const char *their_name = from ()->name (cfg.u ());
17970 : 2029 : const char *our_name = "";
17971 : :
17972 : 2029 : if (!is_header ())
17973 : 1246 : our_name = get_flatname ();
17974 : :
17975 : : /* Header units can be aliased, so name checking is
17976 : : inappropriate. */
17977 : 2029 : if (0 != strcmp (their_name, our_name))
17978 : : {
17979 : 0 : error_at (loc,
17980 : 0 : their_name[0] && our_name[0] ? G_("module %qs found")
17981 : : : their_name[0]
17982 : : ? G_("header module expected, module %qs found")
17983 : : : G_("module %qs expected, header module found"),
17984 : 0 : their_name[0] ? their_name : our_name);
17985 : 0 : cfg.set_overrun ();
17986 : 0 : goto done;
17987 : : }
17988 : : }
17989 : :
17990 : : /* Check the CRC after the above sanity checks, so that the user is
17991 : : clued in. */
17992 : 2029 : {
17993 : 2029 : unsigned e_crc = crc;
17994 : 2029 : crc = cfg.get_crc ();
17995 : 2475 : dump () && dump ("Reading CRC=%x", crc);
17996 : 2029 : if (!is_direct () && crc != e_crc)
17997 : : {
17998 : 3 : error_at (loc, "module %qs CRC mismatch", get_flatname ());
17999 : 3 : cfg.set_overrun ();
18000 : 3 : goto done;
18001 : : }
18002 : : }
18003 : :
18004 : : /* Check target & host. */
18005 : 2026 : {
18006 : 2026 : const char *their_target = from ()->name (cfg.u ());
18007 : 2026 : const char *their_host = from ()->name (cfg.u ());
18008 : 2472 : dump () && dump ("Read target='%s', host='%s'", their_target, their_host);
18009 : 2026 : if (strcmp (their_target, TARGET_MACHINE)
18010 : 2026 : || strcmp (their_host, HOST_MACHINE))
18011 : : {
18012 : 0 : error_at (loc, "target & host is %qs:%qs, expected %qs:%qs",
18013 : : their_target, TARGET_MACHINE, their_host, HOST_MACHINE);
18014 : 0 : cfg.set_overrun ();
18015 : 0 : goto done;
18016 : : }
18017 : : }
18018 : :
18019 : : /* Check compilation dialect. This must match. */
18020 : 2026 : {
18021 : 2026 : const char *their_dialect = cfg.str ();
18022 : 2026 : if (strcmp (their_dialect, config.dialect_str))
18023 : : {
18024 : 1 : error_at (loc, "language dialect differs %qs, expected %qs",
18025 : : their_dialect, config.dialect_str);
18026 : 1 : cfg.set_overrun ();
18027 : 1 : goto done;
18028 : : }
18029 : : }
18030 : :
18031 : : /* Check for extensions. If they set any, we must have them set
18032 : : too. */
18033 : 2025 : {
18034 : 2025 : unsigned ext = cfg.u ();
18035 : 2025 : unsigned allowed = (flag_openmp ? SE_OPENMP : 0);
18036 : :
18037 : 2025 : if (unsigned bad = ext & ~allowed)
18038 : : {
18039 : 3 : if (bad & SE_OPENMP)
18040 : 3 : error_at (loc, "module contains OpenMP, use %<-fopenmp%> to enable");
18041 : 3 : cfg.set_overrun ();
18042 : 3 : goto done;
18043 : : }
18044 : 2022 : extensions = ext;
18045 : : }
18046 : :
18047 : : /* Check global trees. */
18048 : 2022 : {
18049 : 2022 : unsigned their_fixed_length = cfg.u ();
18050 : 2022 : unsigned their_fixed_crc = cfg.u32 ();
18051 : 2468 : dump () && dump ("Read globals=%u, crc=%x",
18052 : : their_fixed_length, their_fixed_crc);
18053 : 2022 : if (!flag_preprocess_only
18054 : 2022 : && (their_fixed_length != fixed_trees->length ()
18055 : 1992 : || their_fixed_crc != global_crc))
18056 : : {
18057 : 0 : error_at (loc, "fixed tree mismatch");
18058 : 0 : cfg.set_overrun ();
18059 : 0 : goto done;
18060 : : }
18061 : : }
18062 : :
18063 : : /* All non-partitions are interfaces. */
18064 : 2022 : interface_p = !is_partition () || cfg.u ();
18065 : :
18066 : 2022 : config.num_imports = cfg.u ();
18067 : 2022 : config.num_partitions = cfg.u ();
18068 : 2022 : config.num_entities = cfg.u ();
18069 : :
18070 : 2022 : config.ordinary_locs = cfg.u ();
18071 : 2022 : config.macro_locs = cfg.u ();
18072 : 2022 : config.loc_range_bits = cfg.u ();
18073 : :
18074 : 2022 : config.active_init = cfg.u ();
18075 : :
18076 : 2029 : done:
18077 : 2029 : return cfg.end (from ());
18078 : 2029 : }
18079 : :
18080 : : /* Comparator for ordering the Ordered Ordinary Location array. */
18081 : :
18082 : : static int
18083 : 80 : ool_cmp (const void *a_, const void *b_)
18084 : : {
18085 : 80 : auto *a = *static_cast<const module_state *const *> (a_);
18086 : 80 : auto *b = *static_cast<const module_state *const *> (b_);
18087 : 80 : if (a == b)
18088 : : return 0;
18089 : 80 : else if (a->ordinary_locs.first < b->ordinary_locs.first)
18090 : : return -1;
18091 : : else
18092 : 27 : return +1;
18093 : : }
18094 : :
18095 : : /* Use ELROND format to record the following sections:
18096 : : qualified-names : binding value(s)
18097 : : MOD_SNAME_PFX.README : human readable, strings
18098 : : MOD_SNAME_PFX.ENV : environment strings, strings
18099 : : MOD_SNAME_PFX.nms : namespace hierarchy
18100 : : MOD_SNAME_PFX.bnd : binding table
18101 : : MOD_SNAME_PFX.spc : specialization table
18102 : : MOD_SNAME_PFX.imp : import table
18103 : : MOD_SNAME_PFX.ent : entity table
18104 : : MOD_SNAME_PFX.prt : partitions table
18105 : : MOD_SNAME_PFX.olm : ordinary line maps
18106 : : MOD_SNAME_PFX.mlm : macro line maps
18107 : : MOD_SNAME_PFX.def : macro definitions
18108 : : MOD_SNAME_PFX.mac : macro index
18109 : : MOD_SNAME_PFX.ini : inits
18110 : : MOD_SNAME_PFX.cnt : counts
18111 : : MOD_SNAME_PFX.cfg : config data
18112 : : */
18113 : :
18114 : : void
18115 : 1824 : module_state::write_begin (elf_out *to, cpp_reader *reader,
18116 : : module_state_config &config, unsigned &crc)
18117 : : {
18118 : : /* Figure out remapped module numbers, which might elide
18119 : : partitions. */
18120 : 1824 : bitmap partitions = NULL;
18121 : 1824 : if (!is_header () && !is_partition ())
18122 : 1042 : partitions = BITMAP_GGC_ALLOC ();
18123 : 1824 : write_init_maps ();
18124 : :
18125 : 1824 : unsigned mod_hwm = 1;
18126 : 2255 : for (unsigned ix = 1; ix != modules->length (); ix++)
18127 : : {
18128 : 431 : module_state *imp = (*modules)[ix];
18129 : :
18130 : : /* Promote any non-partition direct import from a partition, unless
18131 : : we're a partition. */
18132 : 398 : if (!is_partition () && !imp->is_partition ()
18133 : 730 : && imp->is_partition_direct ())
18134 : 9 : imp->directness = MD_PURVIEW_DIRECT;
18135 : :
18136 : : /* Write any import that is not a partition, unless we're a
18137 : : partition. */
18138 : 431 : if (!partitions || !imp->is_partition ())
18139 : 332 : imp->remap = mod_hwm++;
18140 : : else
18141 : : {
18142 : 129 : dump () && dump ("Partition %M %u", imp, ix);
18143 : 99 : bitmap_set_bit (partitions, ix);
18144 : 99 : imp->remap = 0;
18145 : : /* All interface partitions must be exported. */
18146 : 99 : if (imp->is_interface () && !bitmap_bit_p (exports, imp->mod))
18147 : : {
18148 : 3 : error_at (imp->loc, "interface partition is not exported");
18149 : 3 : bitmap_set_bit (exports, imp->mod);
18150 : : }
18151 : :
18152 : : /* All the partition entities should have been loaded when
18153 : : loading the partition. */
18154 : : if (CHECKING_P)
18155 : 366 : for (unsigned jx = 0; jx != imp->entity_num; jx++)
18156 : : {
18157 : 267 : binding_slot *slot = &(*entity_ary)[imp->entity_lwm + jx];
18158 : 267 : gcc_checking_assert (!slot->is_lazy ());
18159 : : }
18160 : : }
18161 : :
18162 : 431 : if (imp->is_direct () && (imp->remap || imp->is_partition ()))
18163 : 424 : note_location (imp->imported_from ());
18164 : : }
18165 : :
18166 : 1824 : if (partitions && bitmap_empty_p (partitions))
18167 : : /* No partitions present. */
18168 : : partitions = nullptr;
18169 : :
18170 : : /* Find the set of decls we must write out. */
18171 : 1824 : depset::hash table (DECL_NAMESPACE_BINDINGS (global_namespace)->size () * 8);
18172 : : /* Add the specializations before the writables, so that we can
18173 : : detect injected friend specializations. */
18174 : 1824 : table.add_specializations (true);
18175 : 1824 : table.add_specializations (false);
18176 : 1824 : if (partial_specializations)
18177 : : {
18178 : 147 : table.add_partial_entities (partial_specializations);
18179 : 147 : partial_specializations = NULL;
18180 : : }
18181 : 1824 : table.add_namespace_entities (global_namespace, partitions);
18182 : 1824 : if (class_members)
18183 : : {
18184 : 12 : table.add_class_entities (class_members);
18185 : 12 : class_members = NULL;
18186 : : }
18187 : :
18188 : : /* Now join everything up. */
18189 : 1824 : table.find_dependencies (this);
18190 : :
18191 : 1824 : if (!table.finalize_dependencies ())
18192 : : {
18193 : 6 : to->set_error ();
18194 : 6 : return;
18195 : : }
18196 : :
18197 : : #if CHECKING_P
18198 : : /* We're done verifying at-most once reading, reset to verify
18199 : : at-most once writing. */
18200 : 1818 : note_defs = note_defs_table_t::create_ggc (1000);
18201 : : #endif
18202 : :
18203 : : /* Determine Strongy Connected Components. */
18204 : 1818 : vec<depset *> sccs = table.connect ();
18205 : :
18206 : 1818 : vec_alloc (ool, modules->length ());
18207 : 2249 : for (unsigned ix = modules->length (); --ix;)
18208 : : {
18209 : 431 : auto *import = (*modules)[ix];
18210 : 431 : if (import->loadedness > ML_NONE
18211 : 431 : && !(partitions && bitmap_bit_p (partitions, import->mod)))
18212 : 332 : ool->quick_push (import);
18213 : : }
18214 : 1818 : ool->qsort (ool_cmp);
18215 : :
18216 : 1818 : vec<cpp_hashnode *> *macros = nullptr;
18217 : 1818 : if (is_header ())
18218 : 689 : macros = prepare_macros (reader);
18219 : :
18220 : 1818 : config.num_imports = mod_hwm;
18221 : 1818 : config.num_partitions = modules->length () - mod_hwm;
18222 : 1818 : auto map_info = write_prepare_maps (&config, bool (config.num_partitions));
18223 : 1818 : unsigned counts[MSC_HWM];
18224 : 1818 : memset (counts, 0, sizeof (counts));
18225 : :
18226 : : /* depset::cluster is the cluster number,
18227 : : depset::section is unspecified scratch value.
18228 : :
18229 : : The following loops make use of the tarjan property that
18230 : : dependencies will be earlier in the SCCS array. */
18231 : :
18232 : : /* This first loop determines the number of depsets in each SCC, and
18233 : : also the number of namespaces we're dealing with. During the
18234 : : loop, the meaning of a couple of depset fields now change:
18235 : :
18236 : : depset::cluster -> size_of cluster, if first of cluster & !namespace
18237 : : depset::section -> section number of cluster (if !namespace). */
18238 : :
18239 : 1818 : unsigned n_spaces = 0;
18240 : 1818 : counts[MSC_sec_lwm] = counts[MSC_sec_hwm] = to->get_section_limit ();
18241 : 728248 : for (unsigned size, ix = 0; ix < sccs.length (); ix += size)
18242 : : {
18243 : 362306 : depset **base = &sccs[ix];
18244 : :
18245 : 684977 : if (base[0]->get_entity_kind () == depset::EK_NAMESPACE)
18246 : : {
18247 : 4005 : n_spaces++;
18248 : 4005 : size = 1;
18249 : : }
18250 : : else
18251 : : {
18252 : : /* Count the members in this cluster. */
18253 : 1479455 : for (size = 1; ix + size < sccs.length (); size++)
18254 : 1477863 : if (base[size]->cluster != base[0]->cluster)
18255 : : break;
18256 : :
18257 : 1837756 : for (unsigned jx = 0; jx != size; jx++)
18258 : : {
18259 : : /* Set the section number. */
18260 : 1479455 : base[jx]->cluster = ~(~0u >> 1); /* A bad value. */
18261 : 1479455 : base[jx]->section = counts[MSC_sec_hwm];
18262 : : }
18263 : :
18264 : : /* Save the size in the first member's cluster slot. */
18265 : 358301 : base[0]->cluster = size;
18266 : :
18267 : 358301 : counts[MSC_sec_hwm]++;
18268 : : }
18269 : : }
18270 : :
18271 : : /* Write the clusters. Namespace decls are put in the spaces array.
18272 : : The meaning of depset::cluster changes to provide the
18273 : : unnamed-decl count of the depset's decl (and remains zero for
18274 : : non-decls and non-unnamed). */
18275 : 1818 : unsigned bytes = 0;
18276 : 1818 : vec<depset *> spaces;
18277 : 1818 : spaces.create (n_spaces);
18278 : :
18279 : 728248 : for (unsigned size, ix = 0; ix < sccs.length (); ix += size)
18280 : : {
18281 : 362306 : depset **base = &sccs[ix];
18282 : :
18283 : 362306 : if (base[0]->get_entity_kind () == depset::EK_NAMESPACE)
18284 : : {
18285 : 4005 : tree decl = base[0]->get_entity ();
18286 : 4005 : if (decl == global_namespace)
18287 : 1631 : base[0]->cluster = 0;
18288 : 2374 : else if (!base[0]->is_import ())
18289 : : {
18290 : 2374 : base[0]->cluster = counts[MSC_entities]++;
18291 : 2374 : spaces.quick_push (base[0]);
18292 : 2374 : counts[MSC_namespaces]++;
18293 : 2374 : if (CHECKING_P)
18294 : : {
18295 : : /* Add it to the entity map, such that we can tell it is
18296 : : part of us. */
18297 : 2374 : bool existed;
18298 : 2374 : unsigned *slot = &entity_map->get_or_insert
18299 : 2374 : (DECL_UID (decl), &existed);
18300 : 2374 : if (existed)
18301 : : /* It must have come from a partition. */
18302 : 0 : gcc_checking_assert
18303 : : (import_entity_module (*slot)->is_partition ());
18304 : 2374 : *slot = ~base[0]->cluster;
18305 : : }
18306 : 364710 : dump (dumper::CLUSTER) && dump ("Cluster namespace %N", decl);
18307 : : }
18308 : : size = 1;
18309 : : }
18310 : : else
18311 : : {
18312 : 358301 : size = base[0]->cluster;
18313 : :
18314 : : /* Cluster is now used to number entities. */
18315 : 358301 : base[0]->cluster = ~(~0u >> 1); /* A bad value. */
18316 : :
18317 : 358301 : sort_cluster (&table, base, size);
18318 : :
18319 : : /* Record the section for consistency checking during stream
18320 : : out -- we don't want to start writing decls in different
18321 : : sections. */
18322 : 358301 : table.section = base[0]->section;
18323 : 358301 : bytes += write_cluster (to, base, size, table, counts, &crc);
18324 : 358301 : table.section = 0;
18325 : : }
18326 : : }
18327 : :
18328 : : /* depset::cluster - entity number (on entities)
18329 : : depset::section - cluster number */
18330 : : /* We'd better have written as many sections and found as many
18331 : : namespaces as we predicted. */
18332 : 3636 : gcc_assert (counts[MSC_sec_hwm] == to->get_section_limit ()
18333 : : && spaces.length () == counts[MSC_namespaces]);
18334 : :
18335 : : /* Write the entitites. None happens if we contain namespaces or
18336 : : nothing. */
18337 : 1818 : config.num_entities = counts[MSC_entities];
18338 : 1818 : if (counts[MSC_entities])
18339 : 1631 : write_entities (to, sccs, counts[MSC_entities], &crc);
18340 : :
18341 : : /* Write the namespaces. */
18342 : 1818 : if (counts[MSC_namespaces])
18343 : 342 : write_namespaces (to, spaces, counts[MSC_namespaces], &crc);
18344 : :
18345 : : /* Write the bindings themselves. */
18346 : 1818 : counts[MSC_bindings] = write_bindings (to, sccs, &crc);
18347 : :
18348 : : /* Write the unnamed. */
18349 : 1818 : counts[MSC_pendings] = write_pendings (to, sccs, table, &crc);
18350 : :
18351 : : /* Write the import table. */
18352 : 1818 : if (config.num_imports > 1)
18353 : 312 : write_imports (to, &crc);
18354 : :
18355 : : /* Write elided partition table. */
18356 : 1818 : if (config.num_partitions)
18357 : 69 : write_partitions (to, config.num_partitions, &crc);
18358 : :
18359 : : /* Write the line maps. */
18360 : 1818 : if (config.ordinary_locs)
18361 : 1754 : write_ordinary_maps (to, map_info, bool (config.num_partitions), &crc);
18362 : 1818 : if (config.macro_locs)
18363 : 121 : write_macro_maps (to, map_info, &crc);
18364 : :
18365 : 1818 : if (is_header ())
18366 : : {
18367 : 689 : counts[MSC_macros] = write_macros (to, macros, &crc);
18368 : 689 : counts[MSC_inits] = write_inits (to, table, &crc);
18369 : 689 : vec_free (macros);
18370 : : }
18371 : :
18372 : 1818 : unsigned clusters = counts[MSC_sec_hwm] - counts[MSC_sec_lwm];
18373 : 1818 : dump () && dump ("Wrote %u clusters, average %u bytes/cluster",
18374 : 228 : clusters, (bytes + clusters / 2) / (clusters + !clusters));
18375 : 1818 : trees_out::instrument ();
18376 : :
18377 : 1818 : write_counts (to, counts, &crc);
18378 : :
18379 : 1818 : spaces.release ();
18380 : 1818 : sccs.release ();
18381 : :
18382 : 1818 : vec_free (macro_loc_remap);
18383 : 1818 : vec_free (ord_loc_remap);
18384 : 1818 : vec_free (ool);
18385 : :
18386 : : // FIXME:QOI: Have a command line switch to control more detailed
18387 : : // information (which might leak data you do not want to leak).
18388 : : // Perhaps (some of) the write_readme contents should also be
18389 : : // so-controlled.
18390 : 1818 : if (false)
18391 : : write_env (to);
18392 : 1824 : }
18393 : :
18394 : : // Finish module writing after we've emitted all dynamic initializers.
18395 : :
18396 : : void
18397 : 1824 : module_state::write_end (elf_out *to, cpp_reader *reader,
18398 : : module_state_config &config, unsigned &crc)
18399 : : {
18400 : : /* And finish up. */
18401 : 1824 : write_config (to, config, crc);
18402 : :
18403 : : /* Human-readable info. */
18404 : 1824 : write_readme (to, reader, config.dialect_str);
18405 : :
18406 : 2052 : dump () && dump ("Wrote %u sections", to->get_section_limit ());
18407 : 1824 : }
18408 : :
18409 : : /* Initial read of a CMI. Checks config, loads up imports and line
18410 : : maps. */
18411 : :
18412 : : bool
18413 : 2029 : module_state::read_initial (cpp_reader *reader)
18414 : : {
18415 : 2029 : module_state_config config;
18416 : 2029 : bool ok = true;
18417 : :
18418 : 2029 : if (ok && !from ()->begin (loc))
18419 : : ok = false;
18420 : :
18421 : 2029 : if (ok && !read_config (config))
18422 : : ok = false;
18423 : :
18424 : 2022 : bool have_locs = ok && read_prepare_maps (&config);
18425 : :
18426 : : /* Ordinary maps before the imports. */
18427 : 2009 : if (!(have_locs && config.ordinary_locs))
18428 : 80 : ordinary_locs.first = line_table->highest_location + 1;
18429 : 1949 : else if (!read_ordinary_maps (config.ordinary_locs, config.loc_range_bits))
18430 : : ok = false;
18431 : :
18432 : : /* Allocate the REMAP vector. */
18433 : 2029 : slurp->alloc_remap (config.num_imports);
18434 : :
18435 : 2029 : if (ok)
18436 : : {
18437 : : /* Read the import table. Decrement current to stop this CMI
18438 : : from being evicted during the import. */
18439 : 2022 : slurp->current--;
18440 : 2022 : if (config.num_imports > 1 && !read_imports (reader, line_table))
18441 : : ok = false;
18442 : 2022 : slurp->current++;
18443 : : }
18444 : :
18445 : : /* Read the elided partition table, if we're the primary partition. */
18446 : 2022 : if (ok && config.num_partitions && is_module ()
18447 : 2034 : && !read_partitions (config.num_partitions))
18448 : : ok = false;
18449 : :
18450 : : /* Determine the module's number. */
18451 : 2029 : gcc_checking_assert (mod == MODULE_UNKNOWN);
18452 : 2029 : gcc_checking_assert (this != (*modules)[0]);
18453 : :
18454 : 2029 : {
18455 : : /* Allocate space in the entities array now -- that array must be
18456 : : monotonically in step with the modules array. */
18457 : 2029 : entity_lwm = vec_safe_length (entity_ary);
18458 : 2029 : entity_num = config.num_entities;
18459 : 2029 : gcc_checking_assert (modules->length () == 1
18460 : : || modules->last ()->entity_lwm <= entity_lwm);
18461 : 2029 : vec_safe_reserve (entity_ary, config.num_entities);
18462 : :
18463 : 2029 : binding_slot slot;
18464 : 2029 : slot.u.binding = NULL_TREE;
18465 : 1695856 : for (unsigned count = config.num_entities; count--;)
18466 : 1693827 : entity_ary->quick_push (slot);
18467 : : }
18468 : :
18469 : : /* We'll run out of other resources before we run out of module
18470 : : indices. */
18471 : 2029 : mod = modules->length ();
18472 : 2029 : vec_safe_push (modules, this);
18473 : :
18474 : : /* We always import and export ourselves. */
18475 : 2029 : bitmap_set_bit (imports, mod);
18476 : 2029 : bitmap_set_bit (exports, mod);
18477 : :
18478 : 2029 : if (ok)
18479 : 2022 : (*slurp->remap)[0] = mod << 1;
18480 : 2475 : dump () && dump ("Assigning %M module number %u", this, mod);
18481 : :
18482 : : /* We should not have been frozen during the importing done by
18483 : : read_config. */
18484 : 2029 : gcc_assert (!from ()->is_frozen ());
18485 : :
18486 : : /* Macro maps after the imports. */
18487 : 2029 : if (!(ok && have_locs && config.macro_locs))
18488 : 3803 : macro_locs.first = LINEMAPS_MACRO_LOWEST_LOCATION (line_table);
18489 : 126 : else if (!read_macro_maps (config.macro_locs))
18490 : : ok = false;
18491 : :
18492 : : /* Note whether there's an active initializer. */
18493 : 2029 : active_init_p = !is_header () && bool (config.active_init);
18494 : :
18495 : 2029 : gcc_assert (slurp->current == ~0u);
18496 : 2029 : return ok;
18497 : : }
18498 : :
18499 : : /* Read a preprocessor state. */
18500 : :
18501 : : bool
18502 : 735 : module_state::read_preprocessor (bool outermost)
18503 : : {
18504 : 735 : gcc_checking_assert (is_header () && slurp
18505 : : && slurp->remap_module (0) == mod);
18506 : :
18507 : 735 : if (loadedness == ML_PREPROCESSOR)
18508 : 6 : return !(from () && from ()->get_error ());
18509 : :
18510 : 729 : bool ok = true;
18511 : :
18512 : : /* Read direct header imports. */
18513 : 729 : unsigned len = slurp->remap->length ();
18514 : 758 : for (unsigned ix = 1; ok && ix != len; ix++)
18515 : : {
18516 : 29 : unsigned map = (*slurp->remap)[ix];
18517 : 29 : if (map & 1)
18518 : : {
18519 : 29 : module_state *import = (*modules)[map >> 1];
18520 : 29 : if (import->is_header ())
18521 : : {
18522 : 29 : ok = import->read_preprocessor (false);
18523 : 29 : bitmap_ior_into (slurp->headers, import->slurp->headers);
18524 : : }
18525 : : }
18526 : : }
18527 : :
18528 : : /* Record as a direct header. */
18529 : 729 : if (ok)
18530 : 729 : bitmap_set_bit (slurp->headers, mod);
18531 : :
18532 : 729 : if (ok && !read_macros ())
18533 : : ok = false;
18534 : :
18535 : 729 : loadedness = ML_PREPROCESSOR;
18536 : 729 : announce ("macros");
18537 : :
18538 : 729 : if (flag_preprocess_only)
18539 : : /* We're done with the string table. */
18540 : 30 : from ()->release ();
18541 : :
18542 : 729 : return check_read (outermost, ok);
18543 : : }
18544 : :
18545 : : /* Read language state. */
18546 : :
18547 : : bool
18548 : 2036 : module_state::read_language (bool outermost)
18549 : : {
18550 : 2036 : gcc_checking_assert (!lazy_snum);
18551 : :
18552 : 2036 : if (loadedness == ML_LANGUAGE)
18553 : 56 : return !(slurp && from () && from ()->get_error ());
18554 : :
18555 : 1980 : gcc_checking_assert (slurp && slurp->current == ~0u
18556 : : && slurp->remap_module (0) == mod);
18557 : :
18558 : 1980 : bool ok = true;
18559 : :
18560 : : /* Read direct imports. */
18561 : 1980 : unsigned len = slurp->remap->length ();
18562 : 2243 : for (unsigned ix = 1; ok && ix != len; ix++)
18563 : : {
18564 : 263 : unsigned map = (*slurp->remap)[ix];
18565 : 263 : if (map & 1)
18566 : : {
18567 : 263 : module_state *import = (*modules)[map >> 1];
18568 : 263 : if (!import->read_language (false))
18569 : 263 : ok = false;
18570 : : }
18571 : : }
18572 : :
18573 : 1980 : unsigned counts[MSC_HWM];
18574 : :
18575 : 1980 : if (ok && !read_counts (counts))
18576 : : ok = false;
18577 : :
18578 : 1980 : function_depth++; /* Prevent unexpected GCs. */
18579 : :
18580 : 1980 : if (ok && counts[MSC_entities] != entity_num)
18581 : : ok = false;
18582 : 1980 : if (ok && counts[MSC_entities]
18583 : 1788 : && !read_entities (counts[MSC_entities],
18584 : : counts[MSC_sec_lwm], counts[MSC_sec_hwm]))
18585 : : ok = false;
18586 : :
18587 : : /* Read the namespace hierarchy. */
18588 : 1980 : if (ok && counts[MSC_namespaces]
18589 : 2328 : && !read_namespaces (counts[MSC_namespaces]))
18590 : : ok = false;
18591 : :
18592 : 1980 : if (ok && !read_bindings (counts[MSC_bindings],
18593 : : counts[MSC_sec_lwm], counts[MSC_sec_hwm]))
18594 : : ok = false;
18595 : :
18596 : : /* And unnamed. */
18597 : 1980 : if (ok && counts[MSC_pendings] && !read_pendings (counts[MSC_pendings]))
18598 : : ok = false;
18599 : :
18600 : 1980 : if (ok)
18601 : : {
18602 : 1980 : slurp->remaining = counts[MSC_sec_hwm] - counts[MSC_sec_lwm];
18603 : 1980 : available_clusters += counts[MSC_sec_hwm] - counts[MSC_sec_lwm];
18604 : : }
18605 : :
18606 : 1980 : if (!flag_module_lazy
18607 : 1980 : || (is_partition ()
18608 : 132 : && module_interface_p ()
18609 : 117 : && !module_partition_p ()))
18610 : : {
18611 : : /* Read the sections in forward order, so that dependencies are read
18612 : : first. See note about tarjan_connect. */
18613 : 386 : ggc_collect ();
18614 : :
18615 : 386 : lazy_snum = ~0u;
18616 : :
18617 : 386 : unsigned hwm = counts[MSC_sec_hwm];
18618 : 322412 : for (unsigned ix = counts[MSC_sec_lwm]; ok && ix != hwm; ix++)
18619 : 322026 : if (!load_section (ix, NULL))
18620 : : {
18621 : : ok = false;
18622 : : break;
18623 : : }
18624 : 386 : lazy_snum = 0;
18625 : 386 : post_load_processing ();
18626 : :
18627 : 386 : ggc_collect ();
18628 : :
18629 : 386 : if (ok && CHECKING_P)
18630 : 1139145 : for (unsigned ix = 0; ix != entity_num; ix++)
18631 : 1138759 : gcc_assert (!(*entity_ary)[ix + entity_lwm].is_lazy ());
18632 : : }
18633 : :
18634 : : // If the import is a header-unit, we need to register initializers
18635 : : // of any static objects it contains (looking at you _Ioinit).
18636 : : // Notice, the ordering of these initializers will be that of a
18637 : : // dynamic initializer at this point in the current TU. (Other
18638 : : // instances of these objects in other TUs will be initialized as
18639 : : // part of that TU's global initializers.)
18640 : 1980 : if (ok && counts[MSC_inits] && !read_inits (counts[MSC_inits]))
18641 : : ok = false;
18642 : :
18643 : 1980 : function_depth--;
18644 : :
18645 : 2267 : announce (flag_module_lazy ? "lazy" : "imported");
18646 : 1980 : loadedness = ML_LANGUAGE;
18647 : :
18648 : 1980 : gcc_assert (slurp->current == ~0u);
18649 : :
18650 : : /* We're done with the string table. */
18651 : 1980 : from ()->release ();
18652 : :
18653 : 1980 : return check_read (outermost, ok);
18654 : : }
18655 : :
18656 : : bool
18657 : 358539 : module_state::maybe_defrost ()
18658 : : {
18659 : 358539 : bool ok = true;
18660 : 358539 : if (from ()->is_frozen ())
18661 : : {
18662 : 9 : if (lazy_open >= lazy_limit)
18663 : 3 : freeze_an_elf ();
18664 : 18 : dump () && dump ("Defrosting '%s'", filename);
18665 : 9 : ok = from ()->defrost (maybe_add_cmi_prefix (filename));
18666 : 9 : lazy_open++;
18667 : : }
18668 : :
18669 : 358539 : return ok;
18670 : : }
18671 : :
18672 : : /* Load section SNUM, dealing with laziness. It doesn't matter if we
18673 : : have multiple concurrent loads, because we do not use TREE_VISITED
18674 : : when reading back in. */
18675 : :
18676 : : bool
18677 : 358539 : module_state::load_section (unsigned snum, binding_slot *mslot)
18678 : : {
18679 : 358539 : if (from ()->get_error ())
18680 : : return false;
18681 : :
18682 : 358539 : if (snum >= slurp->current)
18683 : 0 : from ()->set_error (elf::E_BAD_LAZY);
18684 : 358539 : else if (maybe_defrost ())
18685 : : {
18686 : 358539 : unsigned old_current = slurp->current;
18687 : 358539 : slurp->current = snum;
18688 : 358539 : slurp->lru = 0; /* Do not swap out. */
18689 : 358539 : slurp->remaining--;
18690 : 358539 : read_cluster (snum);
18691 : 358539 : slurp->lru = ++lazy_lru;
18692 : 358539 : slurp->current = old_current;
18693 : : }
18694 : :
18695 : 358539 : if (mslot && mslot->is_lazy ())
18696 : : {
18697 : : /* Oops, the section didn't set this slot. */
18698 : 0 : from ()->set_error (elf::E_BAD_DATA);
18699 : 0 : *mslot = NULL_TREE;
18700 : : }
18701 : :
18702 : 358539 : bool ok = !from ()->get_error ();
18703 : 358539 : if (!ok)
18704 : : {
18705 : 0 : error_at (loc, "failed to read compiled module cluster %u: %s",
18706 : 0 : snum, from ()->get_error (filename));
18707 : 0 : note_cmi_name ();
18708 : : }
18709 : :
18710 : 358539 : maybe_completed_reading ();
18711 : :
18712 : 358539 : return ok;
18713 : : }
18714 : :
18715 : : void
18716 : 363267 : module_state::maybe_completed_reading ()
18717 : : {
18718 : 363267 : if (loadedness == ML_LANGUAGE && slurp->current == ~0u && !slurp->remaining)
18719 : : {
18720 : 1659 : lazy_open--;
18721 : : /* We no longer need the macros, all tokenizing has been done. */
18722 : 1659 : slurp->release_macros ();
18723 : :
18724 : 1659 : from ()->end ();
18725 : 1659 : slurp->close ();
18726 : 1659 : slurped ();
18727 : : }
18728 : 363267 : }
18729 : :
18730 : : /* After a reading operation, make sure things are still ok. If not,
18731 : : emit an error and clean up. */
18732 : :
18733 : : bool
18734 : 4756 : module_state::check_read (bool outermost, bool ok)
18735 : : {
18736 : 4756 : gcc_checking_assert (!outermost || slurp->current == ~0u);
18737 : :
18738 : 4756 : if (!ok)
18739 : 7 : from ()->set_error ();
18740 : :
18741 : 4756 : if (int e = from ()->get_error ())
18742 : : {
18743 : 31 : error_at (loc, "failed to read compiled module: %s",
18744 : 31 : from ()->get_error (filename));
18745 : 31 : note_cmi_name ();
18746 : :
18747 : 31 : if (e == EMFILE
18748 : 31 : || e == ENFILE
18749 : : #if MAPPED_READING
18750 : 31 : || e == ENOMEM
18751 : : #endif
18752 : : || false)
18753 : 0 : inform (loc, "consider using %<-fno-module-lazy%>,"
18754 : : " increasing %<-param-lazy-modules=%u%> value,"
18755 : : " or increasing the per-process file descriptor limit",
18756 : : param_lazy_modules);
18757 : 31 : else if (e == ENOENT)
18758 : 18 : inform (loc, "imports must be built before being imported");
18759 : :
18760 : 31 : if (outermost)
18761 : 28 : fatal_error (loc, "returning to the gate for a mechanical issue");
18762 : :
18763 : : ok = false;
18764 : : }
18765 : :
18766 : 4728 : maybe_completed_reading ();
18767 : :
18768 : 4728 : return ok;
18769 : : }
18770 : :
18771 : : /* Return the IDENTIFIER_NODE naming module IX. This is the name
18772 : : including dots. */
18773 : :
18774 : : char const *
18775 : 222 : module_name (unsigned ix, bool header_ok)
18776 : : {
18777 : 222 : if (modules)
18778 : : {
18779 : 222 : module_state *imp = (*modules)[ix];
18780 : :
18781 : 222 : if (ix && !imp->name)
18782 : 0 : imp = imp->parent;
18783 : :
18784 : 222 : if (header_ok || !imp->is_header ())
18785 : 162 : return imp->get_flatname ();
18786 : : }
18787 : :
18788 : : return NULL;
18789 : : }
18790 : :
18791 : : /* Return the bitmap describing what modules are imported. Remember,
18792 : : we always import ourselves. */
18793 : :
18794 : : bitmap
18795 : 96165 : get_import_bitmap ()
18796 : : {
18797 : 96165 : return (*modules)[0]->imports;
18798 : : }
18799 : :
18800 : : /* Return the visible imports and path of instantiation for an
18801 : : instantiation at TINST. If TINST is nullptr, we're not in an
18802 : : instantiation, and thus will return the visible imports of the
18803 : : current TU (and NULL *PATH_MAP_P). We cache the information on
18804 : : the tinst level itself. */
18805 : :
18806 : : static bitmap
18807 : 111628 : path_of_instantiation (tinst_level *tinst, bitmap *path_map_p)
18808 : : {
18809 : 111628 : gcc_checking_assert (modules_p ());
18810 : :
18811 : 111628 : if (!tinst)
18812 : : {
18813 : : /* Not inside an instantiation, just the regular case. */
18814 : 65301 : *path_map_p = nullptr;
18815 : 65301 : return get_import_bitmap ();
18816 : : }
18817 : :
18818 : 46327 : if (!tinst->path)
18819 : : {
18820 : : /* Calculate. */
18821 : 13827 : bitmap visible = path_of_instantiation (tinst->next, path_map_p);
18822 : 13827 : bitmap path_map = *path_map_p;
18823 : :
18824 : 13827 : if (!path_map)
18825 : : {
18826 : 2964 : path_map = BITMAP_GGC_ALLOC ();
18827 : 2964 : bitmap_set_bit (path_map, 0);
18828 : : }
18829 : :
18830 : 13827 : tree decl = tinst->tldcl;
18831 : 13827 : if (TREE_CODE (decl) == TREE_LIST)
18832 : 0 : decl = TREE_PURPOSE (decl);
18833 : 13827 : if (TYPE_P (decl))
18834 : 1353 : decl = TYPE_NAME (decl);
18835 : :
18836 : 13827 : if (unsigned mod = get_originating_module (decl))
18837 : 1409 : if (!bitmap_bit_p (path_map, mod))
18838 : : {
18839 : : /* This is brand new information! */
18840 : 83 : bitmap new_path = BITMAP_GGC_ALLOC ();
18841 : 83 : bitmap_copy (new_path, path_map);
18842 : 83 : bitmap_set_bit (new_path, mod);
18843 : 83 : path_map = new_path;
18844 : :
18845 : 83 : bitmap imports = (*modules)[mod]->imports;
18846 : 83 : if (bitmap_intersect_compl_p (imports, visible))
18847 : : {
18848 : : /* IMPORTS contains additional modules to VISIBLE. */
18849 : 6 : bitmap new_visible = BITMAP_GGC_ALLOC ();
18850 : :
18851 : 6 : bitmap_ior (new_visible, visible, imports);
18852 : 6 : visible = new_visible;
18853 : : }
18854 : : }
18855 : :
18856 : 13827 : tinst->path = path_map;
18857 : 13827 : tinst->visible = visible;
18858 : : }
18859 : :
18860 : 46327 : *path_map_p = tinst->path;
18861 : 46327 : return tinst->visible;
18862 : : }
18863 : :
18864 : : /* Return the bitmap describing what modules are visible along the
18865 : : path of instantiation. If we're not an instantiation, this will be
18866 : : the visible imports of the TU. *PATH_MAP_P is filled in with the
18867 : : modules owning the instantiation path -- we see the module-linkage
18868 : : entities of those modules. */
18869 : :
18870 : : bitmap
18871 : 17457774 : visible_instantiation_path (bitmap *path_map_p)
18872 : : {
18873 : 17457774 : if (!modules_p ())
18874 : : return NULL;
18875 : :
18876 : 97801 : return path_of_instantiation (current_instantiation (), path_map_p);
18877 : : }
18878 : :
18879 : : /* We've just directly imported IMPORT. Update our import/export
18880 : : bitmaps. IS_EXPORT is true if we're reexporting the OTHER. */
18881 : :
18882 : : void
18883 : 2088 : module_state::set_import (module_state const *import, bool is_export)
18884 : : {
18885 : 2088 : gcc_checking_assert (this != import);
18886 : :
18887 : : /* We see IMPORT's exports (which includes IMPORT). If IMPORT is
18888 : : the primary interface or a partition we'll see its imports. */
18889 : 2088 : bitmap_ior_into (imports, import->is_module () || import->is_partition ()
18890 : : ? import->imports : import->exports);
18891 : :
18892 : 2088 : if (is_export)
18893 : : /* We'll export OTHER's exports. */
18894 : 297 : bitmap_ior_into (exports, import->exports);
18895 : 2088 : }
18896 : :
18897 : : /* Return the declaring entity of DECL. That is the decl determining
18898 : : how to decorate DECL with module information. Returns NULL_TREE if
18899 : : it's the global module. */
18900 : :
18901 : : tree
18902 : 151591402 : get_originating_module_decl (tree decl)
18903 : : {
18904 : : /* An enumeration constant. */
18905 : 151591402 : if (TREE_CODE (decl) == CONST_DECL
18906 : 10077 : && DECL_CONTEXT (decl)
18907 : 151601479 : && (TREE_CODE (DECL_CONTEXT (decl)) == ENUMERAL_TYPE))
18908 : 10075 : decl = TYPE_NAME (DECL_CONTEXT (decl));
18909 : 151581327 : else if (TREE_CODE (decl) == FIELD_DECL
18910 : 151575844 : || TREE_CODE (decl) == USING_DECL
18911 : 303150707 : || CONST_DECL_USING_P (decl))
18912 : : {
18913 : 11949 : decl = DECL_CONTEXT (decl);
18914 : 11949 : if (TREE_CODE (decl) != FUNCTION_DECL)
18915 : 11949 : decl = TYPE_NAME (decl);
18916 : : }
18917 : :
18918 : 151591402 : gcc_checking_assert (TREE_CODE (decl) == TEMPLATE_DECL
18919 : : || TREE_CODE (decl) == FUNCTION_DECL
18920 : : || TREE_CODE (decl) == TYPE_DECL
18921 : : || TREE_CODE (decl) == VAR_DECL
18922 : : || TREE_CODE (decl) == CONCEPT_DECL
18923 : : || TREE_CODE (decl) == NAMESPACE_DECL);
18924 : :
18925 : 152450000 : for (;;)
18926 : : {
18927 : : /* Uninstantiated template friends are owned by the befriending
18928 : : class -- not their context. */
18929 : 152020701 : if (TREE_CODE (decl) == TEMPLATE_DECL
18930 : 152020701 : && DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (decl))
18931 : 2306 : decl = TYPE_NAME (DECL_CHAIN (decl));
18932 : :
18933 : 152020701 : int use;
18934 : 152020701 : if (tree ti = node_template_info (decl, use))
18935 : : {
18936 : 363334 : decl = TI_TEMPLATE (ti);
18937 : 363334 : if (TREE_CODE (decl) != TEMPLATE_DECL)
18938 : : {
18939 : : /* A friend template specialization. */
18940 : 9 : gcc_checking_assert (OVL_P (decl));
18941 : 9 : return global_namespace;
18942 : : }
18943 : : }
18944 : : else
18945 : : {
18946 : 151657367 : tree ctx = CP_DECL_CONTEXT (decl);
18947 : 151657367 : if (TREE_CODE (ctx) == NAMESPACE_DECL)
18948 : : break;
18949 : :
18950 : 65974 : if (TYPE_P (ctx))
18951 : : {
18952 : 63864 : ctx = TYPE_NAME (ctx);
18953 : 63864 : if (!ctx)
18954 : : {
18955 : : /* Some kind of internal type. */
18956 : 0 : gcc_checking_assert (DECL_ARTIFICIAL (decl));
18957 : 0 : return global_namespace;
18958 : : }
18959 : : }
18960 : : decl = ctx;
18961 : : }
18962 : 429299 : }
18963 : :
18964 : 151591393 : return decl;
18965 : : }
18966 : :
18967 : : int
18968 : 909546 : get_originating_module (tree decl, bool for_mangle)
18969 : : {
18970 : 909546 : tree owner = get_originating_module_decl (decl);
18971 : 909546 : tree not_tmpl = STRIP_TEMPLATE (owner);
18972 : :
18973 : 909546 : if (!DECL_LANG_SPECIFIC (not_tmpl))
18974 : 191345 : return for_mangle ? -1 : 0;
18975 : :
18976 : 1367335 : if (for_mangle && !DECL_MODULE_ATTACH_P (not_tmpl))
18977 : : return -1;
18978 : :
18979 : 173781 : int mod = !DECL_MODULE_IMPORT_P (not_tmpl) ? 0 : get_importing_module (owner);
18980 : 173781 : gcc_checking_assert (!for_mangle || !(*modules)[mod]->is_header ());
18981 : : return mod;
18982 : : }
18983 : :
18984 : : unsigned
18985 : 11344 : get_importing_module (tree decl, bool flexible)
18986 : : {
18987 : 11344 : unsigned index = import_entity_index (decl, flexible);
18988 : 11344 : if (index == ~(~0u >> 1))
18989 : : return -1;
18990 : 11344 : module_state *module = import_entity_module (index);
18991 : :
18992 : 11344 : return module->mod;
18993 : : }
18994 : :
18995 : : /* Is it permissible to redeclare DECL. */
18996 : :
18997 : : bool
18998 : 207573 : module_may_redeclare (tree decl)
18999 : : {
19000 : 230939 : for (;;)
19001 : : {
19002 : 219256 : tree ctx = CP_DECL_CONTEXT (decl);
19003 : 219256 : if (TREE_CODE (ctx) == NAMESPACE_DECL)
19004 : : // Found the namespace-scope decl.
19005 : : break;
19006 : 21808 : if (!CLASS_TYPE_P (ctx))
19007 : : // We've met a non-class scope. Such a thing is not
19008 : : // reopenable, so we must be ok.
19009 : : return true;
19010 : 11683 : decl = TYPE_NAME (ctx);
19011 : 11683 : }
19012 : :
19013 : 197448 : tree not_tmpl = STRIP_TEMPLATE (decl);
19014 : :
19015 : 197448 : int use_tpl = 0;
19016 : 197448 : if (node_template_info (not_tmpl, use_tpl) && use_tpl)
19017 : : // Specializations of any kind can be redeclared anywhere.
19018 : : // FIXME: Should we be checking this in more places on the scope chain?
19019 : : return true;
19020 : :
19021 : 210675 : if (!DECL_LANG_SPECIFIC (not_tmpl) || !DECL_MODULE_ATTACH_P (not_tmpl))
19022 : : // Decl is attached to global module. Current scope needs to be too.
19023 : 128830 : return !module_attach_p ();
19024 : :
19025 : 736 : module_state *me = (*modules)[0];
19026 : 736 : module_state *them = me;
19027 : :
19028 : 736 : if (DECL_LANG_SPECIFIC (not_tmpl) && DECL_MODULE_IMPORT_P (not_tmpl))
19029 : : {
19030 : : /* We can be given the TEMPLATE_RESULT. We want the
19031 : : TEMPLATE_DECL. */
19032 : 126 : int use_tpl = -1;
19033 : 126 : if (tree ti = node_template_info (decl, use_tpl))
19034 : : {
19035 : 9 : tree tmpl = TI_TEMPLATE (ti);
19036 : 9 : if (use_tpl == 2)
19037 : : {
19038 : : /* A partial specialization. Find that specialization's
19039 : : template_decl. */
19040 : 0 : for (tree list = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
19041 : 0 : list; list = TREE_CHAIN (list))
19042 : 0 : if (DECL_TEMPLATE_RESULT (TREE_VALUE (list)) == decl)
19043 : : {
19044 : : decl = TREE_VALUE (list);
19045 : : break;
19046 : : }
19047 : : }
19048 : 9 : else if (DECL_TEMPLATE_RESULT (tmpl) == decl)
19049 : 126 : decl = tmpl;
19050 : : }
19051 : 126 : unsigned index = import_entity_index (decl);
19052 : 126 : them = import_entity_module (index);
19053 : : }
19054 : :
19055 : : // Decl is attached to named module. Current scope needs to be
19056 : : // attaching to the same module.
19057 : 736 : if (!module_attach_p ())
19058 : : return false;
19059 : :
19060 : : // Both attached to named module.
19061 : 727 : if (me == them)
19062 : : return true;
19063 : :
19064 : 351 : return me && get_primary (them) == get_primary (me);
19065 : : }
19066 : :
19067 : : /* DECL is being created by this TU. Record it came from here. We
19068 : : record module purview, so we can see if partial or explicit
19069 : : specialization needs to be written out, even though its purviewness
19070 : : comes from the most general template. */
19071 : :
19072 : : void
19073 : 716394976 : set_instantiating_module (tree decl)
19074 : : {
19075 : 716394976 : gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
19076 : : || VAR_P (decl)
19077 : : || TREE_CODE (decl) == TYPE_DECL
19078 : : || TREE_CODE (decl) == CONCEPT_DECL
19079 : : || TREE_CODE (decl) == TEMPLATE_DECL
19080 : : || (TREE_CODE (decl) == NAMESPACE_DECL
19081 : : && DECL_NAMESPACE_ALIAS (decl)));
19082 : :
19083 : 716394976 : if (!modules_p ())
19084 : : return;
19085 : :
19086 : 3553937 : decl = STRIP_TEMPLATE (decl);
19087 : :
19088 : 3553937 : if (!DECL_LANG_SPECIFIC (decl) && module_purview_p ())
19089 : 496710 : retrofit_lang_decl (decl);
19090 : :
19091 : 3553937 : if (DECL_LANG_SPECIFIC (decl))
19092 : : {
19093 : 2866959 : DECL_MODULE_PURVIEW_P (decl) = module_purview_p ();
19094 : : /* If this was imported, we'll still be in the entity_hash. */
19095 : 2866959 : DECL_MODULE_IMPORT_P (decl) = false;
19096 : : }
19097 : : }
19098 : :
19099 : : /* If DECL is a class member, whose class is not defined in this TU
19100 : : (it was imported), remember this decl. */
19101 : :
19102 : : void
19103 : 183941 : set_defining_module (tree decl)
19104 : : {
19105 : 183941 : gcc_checking_assert (!DECL_LANG_SPECIFIC (decl)
19106 : : || !DECL_MODULE_IMPORT_P (decl));
19107 : :
19108 : 183941 : if (module_p ())
19109 : : {
19110 : : /* We need to track all declarations within a module, not just those
19111 : : in the module purview, because we don't necessarily know yet if
19112 : : this module will require a CMI while in the global fragment. */
19113 : 110215 : tree ctx = DECL_CONTEXT (decl);
19114 : 110215 : if (ctx
19115 : 110215 : && (TREE_CODE (ctx) == RECORD_TYPE || TREE_CODE (ctx) == UNION_TYPE)
19116 : 20564 : && DECL_LANG_SPECIFIC (TYPE_NAME (ctx))
19117 : 128543 : && DECL_MODULE_IMPORT_P (TYPE_NAME (ctx)))
19118 : : {
19119 : : /* This entity's context is from an import. We may need to
19120 : : record this entity to make sure we emit it in the CMI.
19121 : : Template specializations are in the template hash tables,
19122 : : so we don't need to record them here as well. */
19123 : 12 : int use_tpl = -1;
19124 : 12 : tree ti = node_template_info (decl, use_tpl);
19125 : 12 : if (use_tpl <= 0)
19126 : : {
19127 : 12 : if (ti)
19128 : : {
19129 : 3 : gcc_checking_assert (!use_tpl);
19130 : : /* Get to the TEMPLATE_DECL. */
19131 : 3 : decl = TI_TEMPLATE (ti);
19132 : : }
19133 : :
19134 : : /* Record it on the class_members list. */
19135 : 12 : vec_safe_push (class_members, decl);
19136 : : }
19137 : : }
19138 : 96083 : else if (DECL_IMPLICIT_TYPEDEF_P (decl)
19139 : 206286 : && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
19140 : : /* This is a partial or explicit specialization. */
19141 : 41727 : vec_safe_push (partial_specializations, decl);
19142 : : }
19143 : 183941 : }
19144 : :
19145 : : void
19146 : 251757612 : set_originating_module (tree decl, bool friend_p ATTRIBUTE_UNUSED)
19147 : : {
19148 : 251757612 : set_instantiating_module (decl);
19149 : :
19150 : 251757612 : if (!DECL_NAMESPACE_SCOPE_P (decl))
19151 : : return;
19152 : :
19153 : 153961950 : gcc_checking_assert (friend_p || decl == get_originating_module_decl (decl));
19154 : :
19155 : 153961950 : if (module_attach_p ())
19156 : : {
19157 : 2478 : retrofit_lang_decl (decl);
19158 : 2478 : DECL_MODULE_ATTACH_P (decl) = true;
19159 : : }
19160 : :
19161 : 153961950 : if (!module_exporting_p ())
19162 : : return;
19163 : :
19164 : : // FIXME: Check ill-formed linkage
19165 : 359431 : DECL_MODULE_EXPORT_P (decl) = true;
19166 : : }
19167 : :
19168 : : /* DECL is keyed to CTX for odr purposes. */
19169 : :
19170 : : void
19171 : 676236 : maybe_key_decl (tree ctx, tree decl)
19172 : : {
19173 : 676236 : if (!modules_p ())
19174 : : return;
19175 : :
19176 : : /* We only need to deal with lambdas attached to var, field,
19177 : : parm, or type decls. */
19178 : 8865 : if (TREE_CODE (ctx) != VAR_DECL
19179 : 8865 : && TREE_CODE (ctx) != FIELD_DECL
19180 : 8804 : && TREE_CODE (ctx) != PARM_DECL
19181 : 8792 : && TREE_CODE (ctx) != TYPE_DECL)
19182 : : return;
19183 : :
19184 : : /* For fields, key it to the containing type to handle deduplication
19185 : : correctly. */
19186 : 85 : if (TREE_CODE (ctx) == FIELD_DECL)
19187 : 6 : ctx = TYPE_NAME (DECL_CONTEXT (ctx));
19188 : :
19189 : 85 : if (!keyed_table)
19190 : 46 : keyed_table = new keyed_map_t (EXPERIMENT (1, 400));
19191 : :
19192 : 85 : auto &vec = keyed_table->get_or_insert (ctx);
19193 : 85 : if (!vec.length ())
19194 : : {
19195 : 85 : retrofit_lang_decl (ctx);
19196 : 85 : DECL_MODULE_KEYED_DECLS_P (ctx) = true;
19197 : : }
19198 : 85 : vec.safe_push (decl);
19199 : : }
19200 : :
19201 : : /* Create the flat name string. It is simplest to have it handy. */
19202 : :
19203 : : void
19204 : 4172 : module_state::set_flatname ()
19205 : : {
19206 : 4172 : gcc_checking_assert (!flatname);
19207 : 4172 : if (parent)
19208 : : {
19209 : 353 : auto_vec<tree,5> ids;
19210 : 353 : size_t len = 0;
19211 : 353 : char const *primary = NULL;
19212 : 353 : size_t pfx_len = 0;
19213 : :
19214 : 353 : for (module_state *probe = this;
19215 : 891 : probe;
19216 : 538 : probe = probe->parent)
19217 : 796 : if (is_partition () && !probe->is_partition ())
19218 : : {
19219 : 258 : primary = probe->get_flatname ();
19220 : 258 : pfx_len = strlen (primary);
19221 : 258 : break;
19222 : : }
19223 : : else
19224 : : {
19225 : 538 : ids.safe_push (probe->name);
19226 : 538 : len += IDENTIFIER_LENGTH (probe->name) + 1;
19227 : : }
19228 : :
19229 : 353 : char *flat = XNEWVEC (char, pfx_len + len + is_partition ());
19230 : 353 : flatname = flat;
19231 : :
19232 : 353 : if (primary)
19233 : : {
19234 : 258 : memcpy (flat, primary, pfx_len);
19235 : 258 : flat += pfx_len;
19236 : 258 : *flat++ = ':';
19237 : : }
19238 : :
19239 : 891 : for (unsigned len = 0; ids.length ();)
19240 : : {
19241 : 538 : if (len)
19242 : 185 : flat[len++] = '.';
19243 : 538 : tree elt = ids.pop ();
19244 : 538 : unsigned l = IDENTIFIER_LENGTH (elt);
19245 : 538 : memcpy (flat + len, IDENTIFIER_POINTER (elt), l + 1);
19246 : 538 : len += l;
19247 : : }
19248 : 353 : }
19249 : 3819 : else if (is_header ())
19250 : 1490 : flatname = TREE_STRING_POINTER (name);
19251 : : else
19252 : 2329 : flatname = IDENTIFIER_POINTER (name);
19253 : 4172 : }
19254 : :
19255 : : /* Read the CMI file for a module. */
19256 : :
19257 : : bool
19258 : 2047 : module_state::do_import (cpp_reader *reader, bool outermost)
19259 : : {
19260 : 2047 : gcc_assert (global_namespace == current_scope () && loadedness == ML_NONE);
19261 : :
19262 : 2047 : loc = linemap_module_loc (line_table, loc, get_flatname ());
19263 : :
19264 : 2047 : if (lazy_open >= lazy_limit)
19265 : 9 : freeze_an_elf ();
19266 : :
19267 : 2047 : int fd = -1;
19268 : 2047 : int e = ENOENT;
19269 : 2047 : if (filename)
19270 : : {
19271 : 2047 : const char *file = maybe_add_cmi_prefix (filename);
19272 : 2493 : dump () && dump ("CMI is %s", file);
19273 : 2047 : if (note_module_cmi_yes || inform_cmi_p)
19274 : 12 : inform (loc, "reading CMI %qs", file);
19275 : : /* Add the CMI file to the dependency tracking. */
19276 : 2047 : if (cpp_get_deps (reader))
19277 : 6 : deps_add_dep (cpp_get_deps (reader), file);
19278 : 2047 : fd = open (file, O_RDONLY | O_CLOEXEC | O_BINARY);
19279 : 2047 : e = errno;
19280 : : }
19281 : :
19282 : 2047 : gcc_checking_assert (!slurp);
19283 : 4076 : slurp = new slurping (new elf_in (fd, e));
19284 : :
19285 : 2047 : bool ok = true;
19286 : 2047 : if (!from ()->get_error ())
19287 : : {
19288 : 2029 : announce ("importing");
19289 : 2029 : loadedness = ML_CONFIG;
19290 : 2029 : lazy_open++;
19291 : 2029 : ok = read_initial (reader);
19292 : 2029 : slurp->lru = ++lazy_lru;
19293 : : }
19294 : :
19295 : 2047 : gcc_assert (slurp->current == ~0u);
19296 : :
19297 : 2047 : return check_read (outermost, ok);
19298 : : }
19299 : :
19300 : : /* Attempt to increase the file descriptor limit. */
19301 : :
19302 : : static bool
19303 : 3244 : try_increase_lazy (unsigned want)
19304 : : {
19305 : 3244 : gcc_checking_assert (lazy_open >= lazy_limit);
19306 : :
19307 : : /* If we're increasing, saturate at hard limit. */
19308 : 3244 : if (want > lazy_hard_limit && lazy_limit < lazy_hard_limit)
19309 : 3244 : want = lazy_hard_limit;
19310 : :
19311 : : #if HAVE_SETRLIMIT
19312 : 3244 : if ((!lazy_limit || !param_lazy_modules)
19313 : 3232 : && lazy_hard_limit
19314 : 3232 : && want <= lazy_hard_limit)
19315 : : {
19316 : 3232 : struct rlimit rlimit;
19317 : 3232 : rlimit.rlim_cur = want + LAZY_HEADROOM;
19318 : 3232 : rlimit.rlim_max = lazy_hard_limit + LAZY_HEADROOM;
19319 : 3232 : if (!setrlimit (RLIMIT_NOFILE, &rlimit))
19320 : 3232 : lazy_limit = want;
19321 : : }
19322 : : #endif
19323 : :
19324 : 3244 : return lazy_open < lazy_limit;
19325 : : }
19326 : :
19327 : : /* Pick a victim module to freeze its reader. */
19328 : :
19329 : : void
19330 : 12 : module_state::freeze_an_elf ()
19331 : : {
19332 : 12 : if (try_increase_lazy (lazy_open * 2))
19333 : : return;
19334 : :
19335 : 12 : module_state *victim = NULL;
19336 : 12 : for (unsigned ix = modules->length (); ix--;)
19337 : : {
19338 : 30 : module_state *candidate = (*modules)[ix];
19339 : 30 : if (candidate && candidate->slurp && candidate->slurp->lru
19340 : 60 : && candidate->from ()->is_freezable ()
19341 : 39 : && (!victim || victim->slurp->lru > candidate->slurp->lru))
19342 : : victim = candidate;
19343 : : }
19344 : :
19345 : 12 : if (victim)
19346 : : {
19347 : 18 : dump () && dump ("Freezing '%s'", victim->filename);
19348 : 9 : if (victim->slurp->macro_defs.size)
19349 : : /* Save the macro definitions to a buffer. */
19350 : 0 : victim->from ()->preserve (victim->slurp->macro_defs);
19351 : 9 : if (victim->slurp->macro_tbl.size)
19352 : : /* Save the macro definitions to a buffer. */
19353 : 0 : victim->from ()->preserve (victim->slurp->macro_tbl);
19354 : 9 : victim->from ()->freeze ();
19355 : 9 : lazy_open--;
19356 : : }
19357 : : else
19358 : 3 : dump () && dump ("No module available for freezing");
19359 : : }
19360 : :
19361 : : /* Load the lazy slot *MSLOT, INDEX'th slot of the module. */
19362 : :
19363 : : bool
19364 : 34726 : module_state::lazy_load (unsigned index, binding_slot *mslot)
19365 : : {
19366 : 34726 : unsigned n = dump.push (this);
19367 : :
19368 : 34726 : gcc_checking_assert (function_depth);
19369 : :
19370 : 34726 : unsigned cookie = mslot->get_lazy ();
19371 : 34726 : unsigned snum = cookie >> 2;
19372 : 35005 : dump () && dump ("Loading entity %M[%u] section:%u", this, index, snum);
19373 : :
19374 : 34726 : bool ok = load_section (snum, mslot);
19375 : :
19376 : 34726 : dump.pop (n);
19377 : :
19378 : 34726 : return ok;
19379 : : }
19380 : :
19381 : : /* Load MOD's binding for NS::ID into *MSLOT. *MSLOT contains the
19382 : : lazy cookie. OUTER is true if this is the outermost lazy, (used
19383 : : for diagnostics). */
19384 : :
19385 : : void
19386 : 1787 : lazy_load_binding (unsigned mod, tree ns, tree id, binding_slot *mslot)
19387 : : {
19388 : 1787 : int count = errorcount + warningcount;
19389 : :
19390 : 1787 : timevar_start (TV_MODULE_IMPORT);
19391 : :
19392 : : /* Make sure lazy loading from a template context behaves as if
19393 : : from a non-template context. */
19394 : 1787 : processing_template_decl_sentinel ptds;
19395 : :
19396 : : /* Stop GC happening, even in outermost loads (because our caller
19397 : : could well be building up a lookup set). */
19398 : 1787 : function_depth++;
19399 : :
19400 : 1787 : gcc_checking_assert (mod);
19401 : 1787 : module_state *module = (*modules)[mod];
19402 : 1787 : unsigned n = dump.push (module);
19403 : :
19404 : 1787 : unsigned snum = mslot->get_lazy ();
19405 : 2066 : dump () && dump ("Lazily binding %P@%N section:%u", ns, id,
19406 : : module->name, snum);
19407 : :
19408 : 1787 : bool ok = !recursive_lazy (snum);
19409 : 1787 : if (ok)
19410 : : {
19411 : 1787 : ok = module->load_section (snum, mslot);
19412 : 1787 : lazy_snum = 0;
19413 : 1787 : post_load_processing ();
19414 : : }
19415 : :
19416 : 1787 : dump.pop (n);
19417 : :
19418 : 1787 : function_depth--;
19419 : :
19420 : 1787 : timevar_stop (TV_MODULE_IMPORT);
19421 : :
19422 : 1787 : if (!ok)
19423 : 0 : fatal_error (input_location,
19424 : 0 : module->is_header ()
19425 : : ? G_("failed to load binding %<%E%s%E%>")
19426 : : : G_("failed to load binding %<%E%s%E@%s%>"),
19427 : 0 : ns, &"::"[ns == global_namespace ? 2 : 0], id,
19428 : : module->get_flatname ());
19429 : :
19430 : 1787 : if (count != errorcount + warningcount)
19431 : 21 : inform (input_location,
19432 : 21 : module->is_header ()
19433 : : ? G_("during load of binding %<%E%s%E%>")
19434 : : : G_("during load of binding %<%E%s%E@%s%>"),
19435 : 21 : ns, &"::"[ns == global_namespace ? 2 : 0], id,
19436 : : module->get_flatname ());
19437 : 1787 : }
19438 : :
19439 : : /* Load any pending entities keyed to the top-key of DECL. */
19440 : :
19441 : : void
19442 : 36681073 : lazy_load_pendings (tree decl)
19443 : : {
19444 : : /* Make sure lazy loading from a template context behaves as if
19445 : : from a non-template context. */
19446 : 36681073 : processing_template_decl_sentinel ptds;
19447 : :
19448 : 36681073 : tree key_decl;
19449 : 36681073 : pending_key key;
19450 : 36681073 : key.ns = find_pending_key (decl, &key_decl);
19451 : 36681073 : key.id = DECL_NAME (key_decl);
19452 : :
19453 : 36681073 : auto *pending_vec = pending_table ? pending_table->get (key) : nullptr;
19454 : 36681073 : if (!pending_vec)
19455 : 36676186 : return;
19456 : :
19457 : 4887 : int count = errorcount + warningcount;
19458 : :
19459 : 4887 : timevar_start (TV_MODULE_IMPORT);
19460 : 4887 : bool ok = !recursive_lazy ();
19461 : 4887 : if (ok)
19462 : : {
19463 : 4887 : function_depth++; /* Prevent GC */
19464 : 4887 : unsigned n = dump.push (NULL);
19465 : 5063 : dump () && dump ("Reading %u pending entities keyed to %P",
19466 : : pending_vec->length (), key.ns, key.id);
19467 : 4887 : for (unsigned ix = pending_vec->length (); ix--;)
19468 : : {
19469 : 72143 : unsigned index = (*pending_vec)[ix];
19470 : 72143 : binding_slot *slot = &(*entity_ary)[index];
19471 : :
19472 : 72143 : if (slot->is_lazy ())
19473 : : {
19474 : 3592 : module_state *import = import_entity_module (index);
19475 : 3592 : if (!import->lazy_load (index - import->entity_lwm, slot))
19476 : 72143 : ok = false;
19477 : : }
19478 : 68551 : else if (dump ())
19479 : : {
19480 : 105 : module_state *import = import_entity_module (index);
19481 : 77135 : dump () && dump ("Entity %M[%u] already loaded",
19482 : 105 : import, index - import->entity_lwm);
19483 : : }
19484 : : }
19485 : :
19486 : 4887 : pending_table->remove (key);
19487 : 4887 : dump.pop (n);
19488 : 4887 : lazy_snum = 0;
19489 : 4887 : post_load_processing ();
19490 : 4887 : function_depth--;
19491 : : }
19492 : :
19493 : 4887 : timevar_stop (TV_MODULE_IMPORT);
19494 : :
19495 : 4887 : if (!ok)
19496 : 0 : fatal_error (input_location, "failed to load pendings for %<%E%s%E%>",
19497 : 0 : key.ns, &"::"[key.ns == global_namespace ? 2 : 0], key.id);
19498 : :
19499 : 4887 : if (count != errorcount + warningcount)
19500 : 0 : inform (input_location, "during load of pendings for %<%E%s%E%>",
19501 : 0 : key.ns, &"::"[key.ns == global_namespace ? 2 : 0], key.id);
19502 : 36681073 : }
19503 : :
19504 : : static void
19505 : 1865 : direct_import (module_state *import, cpp_reader *reader)
19506 : : {
19507 : 1865 : timevar_start (TV_MODULE_IMPORT);
19508 : 1865 : unsigned n = dump.push (import);
19509 : :
19510 : 1865 : gcc_checking_assert (import->is_direct () && import->has_location ());
19511 : 1865 : if (import->loadedness == ML_NONE)
19512 : 1122 : if (!import->do_import (reader, true))
19513 : 0 : gcc_unreachable ();
19514 : :
19515 : 1837 : if (import->loadedness < ML_LANGUAGE)
19516 : : {
19517 : 1773 : if (!keyed_table)
19518 : 1563 : keyed_table = new keyed_map_t (EXPERIMENT (1, 400));
19519 : 1773 : import->read_language (true);
19520 : : }
19521 : :
19522 : 1837 : (*modules)[0]->set_import (import, import->exported_p);
19523 : :
19524 : 1837 : dump.pop (n);
19525 : 1837 : timevar_stop (TV_MODULE_IMPORT);
19526 : 1837 : }
19527 : :
19528 : : /* Import module IMPORT. */
19529 : :
19530 : : void
19531 : 1685 : import_module (module_state *import, location_t from_loc, bool exporting_p,
19532 : : tree, cpp_reader *reader)
19533 : : {
19534 : 1685 : if (!import->check_not_purview (from_loc))
19535 : : return;
19536 : :
19537 : 1676 : if (!import->is_header () && current_lang_depth ())
19538 : : /* Only header units should appear inside language
19539 : : specifications. The std doesn't specify this, but I think
19540 : : that's an error in resolving US 033, because language linkage
19541 : : is also our escape clause to getting things into the global
19542 : : module, so we don't want to confuse things by having to think
19543 : : about whether 'extern "C++" { import foo; }' puts foo's
19544 : : contents into the global module all of a sudden. */
19545 : 6 : warning (0, "import of named module %qs inside language-linkage block",
19546 : : import->get_flatname ());
19547 : :
19548 : 1676 : if (exporting_p || module_exporting_p ())
19549 : 208 : import->exported_p = true;
19550 : :
19551 : 1676 : if (import->loadedness != ML_NONE)
19552 : : {
19553 : 740 : from_loc = ordinary_loc_of (line_table, from_loc);
19554 : 740 : linemap_module_reparent (line_table, import->loc, from_loc);
19555 : : }
19556 : 1676 : gcc_checking_assert (!import->module_p);
19557 : 1676 : gcc_checking_assert (import->is_direct () && import->has_location ());
19558 : :
19559 : 1676 : direct_import (import, reader);
19560 : : }
19561 : :
19562 : : /* Declare the name of the current module to be NAME. EXPORTING_p is
19563 : : true if this TU is the exporting module unit. */
19564 : :
19565 : : void
19566 : 2095 : declare_module (module_state *module, location_t from_loc, bool exporting_p,
19567 : : tree, cpp_reader *reader)
19568 : : {
19569 : 2095 : gcc_assert (global_namespace == current_scope ());
19570 : :
19571 : 2095 : module_state *current = (*modules)[0];
19572 : 2095 : if (module_purview_p () || module->loadedness > ML_CONFIG)
19573 : : {
19574 : 12 : error_at (from_loc, module_purview_p ()
19575 : : ? G_("module already declared")
19576 : : : G_("module already imported"));
19577 : 6 : if (module_purview_p ())
19578 : 0 : module = current;
19579 : 6 : inform (module->loc, module_purview_p ()
19580 : : ? G_("module %qs declared here")
19581 : : : G_("module %qs imported here"),
19582 : : module->get_flatname ());
19583 : 6 : return;
19584 : : }
19585 : :
19586 : 2089 : gcc_checking_assert (module->module_p);
19587 : 2089 : gcc_checking_assert (module->is_direct () && module->has_location ());
19588 : :
19589 : : /* Yer a module, 'arry. */
19590 : 2089 : module_kind = module->is_header () ? MK_HEADER : MK_NAMED | MK_ATTACH;
19591 : :
19592 : : // Even in header units, we consider the decls to be purview
19593 : 2089 : module_kind |= MK_PURVIEW;
19594 : :
19595 : 2089 : if (module->is_partition ())
19596 : 93 : module_kind |= MK_PARTITION;
19597 : 2089 : if (exporting_p)
19598 : : {
19599 : 1882 : module->interface_p = true;
19600 : 1882 : module_kind |= MK_INTERFACE;
19601 : : }
19602 : :
19603 : 2089 : if (module_has_cmi_p ())
19604 : : {
19605 : : /* Copy the importing information we may have already done. We
19606 : : do not need to separate out the imports that only happen in
19607 : : the GMF, inspite of what the literal wording of the std
19608 : : might imply. See p2191, the core list had a discussion
19609 : : where the module implementors agreed that the GMF of a named
19610 : : module is invisible to importers. */
19611 : 1900 : module->imports = current->imports;
19612 : :
19613 : 1900 : module->mod = 0;
19614 : 1900 : (*modules)[0] = module;
19615 : : }
19616 : : else
19617 : : {
19618 : 189 : module->interface_p = true;
19619 : 189 : current->parent = module; /* So mangler knows module identity. */
19620 : 189 : direct_import (module, reader);
19621 : : }
19622 : : }
19623 : :
19624 : : /* Return true IFF we must emit a module global initializer function
19625 : : (which will be called by importers' init code). */
19626 : :
19627 : : bool
19628 : 105570 : module_global_init_needed ()
19629 : : {
19630 : 105570 : return module_has_cmi_p () && !header_module_p ();
19631 : : }
19632 : :
19633 : : /* Calculate which, if any, import initializers need calling. */
19634 : :
19635 : : bool
19636 : 99146 : module_determine_import_inits ()
19637 : : {
19638 : 99146 : if (!modules || header_module_p ())
19639 : : return false;
19640 : :
19641 : : /* Prune active_init_p. We need the same bitmap allocation
19642 : : scheme as for the imports member. */
19643 : 2455 : function_depth++; /* Disable GC. */
19644 : 2455 : bitmap covered_imports (BITMAP_GGC_ALLOC ());
19645 : :
19646 : 2455 : bool any = false;
19647 : :
19648 : : /* Because indirect imports are before their direct import, and
19649 : : we're scanning the array backwards, we only need one pass! */
19650 : 4364 : for (unsigned ix = modules->length (); --ix;)
19651 : : {
19652 : 1909 : module_state *import = (*modules)[ix];
19653 : :
19654 : 1909 : if (!import->active_init_p)
19655 : : ;
19656 : 34 : else if (bitmap_bit_p (covered_imports, ix))
19657 : 6 : import->active_init_p = false;
19658 : : else
19659 : : {
19660 : : /* Everything this imports is therefore handled by its
19661 : : initializer, so doesn't need initializing by us. */
19662 : 28 : bitmap_ior_into (covered_imports, import->imports);
19663 : 28 : any = true;
19664 : : }
19665 : : }
19666 : 2455 : function_depth--;
19667 : :
19668 : 2455 : return any;
19669 : : }
19670 : :
19671 : : /* Emit calls to each direct import's global initializer. Including
19672 : : direct imports of directly imported header units. The initializers
19673 : : of (static) entities in header units will be called by their
19674 : : importing modules (for the instance contained within that), or by
19675 : : the current TU (for the instances we've brought in). Of course
19676 : : such header unit behaviour is evil, but iostream went through that
19677 : : door some time ago. */
19678 : :
19679 : : void
19680 : 28 : module_add_import_initializers ()
19681 : : {
19682 : 28 : if (!modules || header_module_p ())
19683 : 0 : return;
19684 : :
19685 : 28 : tree fntype = build_function_type (void_type_node, void_list_node);
19686 : 28 : releasing_vec args; // There are no args
19687 : :
19688 : 65 : for (unsigned ix = modules->length (); --ix;)
19689 : : {
19690 : 37 : module_state *import = (*modules)[ix];
19691 : 37 : if (import->active_init_p)
19692 : : {
19693 : 28 : tree name = mangle_module_global_init (ix);
19694 : 28 : tree fndecl = build_lang_decl (FUNCTION_DECL, name, fntype);
19695 : :
19696 : 28 : DECL_CONTEXT (fndecl) = FROB_CONTEXT (global_namespace);
19697 : 28 : SET_DECL_ASSEMBLER_NAME (fndecl, name);
19698 : 28 : TREE_PUBLIC (fndecl) = true;
19699 : 28 : determine_visibility (fndecl);
19700 : :
19701 : 28 : tree call = cp_build_function_call_vec (fndecl, &args,
19702 : : tf_warning_or_error);
19703 : 28 : finish_expr_stmt (call);
19704 : : }
19705 : : }
19706 : 28 : }
19707 : :
19708 : : /* NAME & LEN are a preprocessed header name, possibly including the
19709 : : surrounding "" or <> characters. Return the raw string name of the
19710 : : module to which it refers. This will be an absolute path, or begin
19711 : : with ./, so it is immediately distinguishable from a (non-header
19712 : : unit) module name. If READER is non-null, ask the preprocessor to
19713 : : locate the header to which it refers using the appropriate include
19714 : : path. Note that we do never do \ processing of the string, as that
19715 : : matches the preprocessor's behaviour. */
19716 : :
19717 : : static const char *
19718 : 26993 : canonicalize_header_name (cpp_reader *reader, location_t loc, bool unquoted,
19719 : : const char *str, size_t &len_r)
19720 : : {
19721 : 26993 : size_t len = len_r;
19722 : 26993 : static char *buf = 0;
19723 : 26993 : static size_t alloc = 0;
19724 : :
19725 : 26993 : if (!unquoted)
19726 : : {
19727 : 6 : gcc_checking_assert (len >= 2
19728 : : && ((reader && str[0] == '<' && str[len-1] == '>')
19729 : : || (str[0] == '"' && str[len-1] == '"')));
19730 : 6 : str += 1;
19731 : 6 : len -= 2;
19732 : : }
19733 : :
19734 : 26993 : if (reader)
19735 : : {
19736 : 6 : gcc_assert (!unquoted);
19737 : :
19738 : 6 : if (len >= alloc)
19739 : : {
19740 : 6 : alloc = len + 1;
19741 : 6 : buf = XRESIZEVEC (char, buf, alloc);
19742 : : }
19743 : 6 : memcpy (buf, str, len);
19744 : 6 : buf[len] = 0;
19745 : :
19746 : 12 : if (const char *hdr
19747 : 6 : = cpp_probe_header_unit (reader, buf, str[-1] == '<', loc))
19748 : : {
19749 : 6 : len = strlen (hdr);
19750 : 6 : str = hdr;
19751 : : }
19752 : : else
19753 : 0 : str = buf;
19754 : : }
19755 : :
19756 : 26993 : if (!(str[0] == '.' ? IS_DIR_SEPARATOR (str[1]) : IS_ABSOLUTE_PATH (str)))
19757 : : {
19758 : : /* Prepend './' */
19759 : 9 : if (len + 3 > alloc)
19760 : : {
19761 : 9 : alloc = len + 3;
19762 : 9 : buf = XRESIZEVEC (char, buf, alloc);
19763 : : }
19764 : :
19765 : 9 : buf[0] = '.';
19766 : 9 : buf[1] = DIR_SEPARATOR;
19767 : 9 : memmove (buf + 2, str, len);
19768 : 9 : len += 2;
19769 : 9 : buf[len] = 0;
19770 : 9 : str = buf;
19771 : : }
19772 : :
19773 : 26993 : len_r = len;
19774 : 26993 : return str;
19775 : : }
19776 : :
19777 : : /* Set the CMI name from a cody packet. Issue an error if
19778 : : ill-formed. */
19779 : :
19780 : 3860 : void module_state::set_filename (const Cody::Packet &packet)
19781 : : {
19782 : 3860 : gcc_checking_assert (!filename);
19783 : 3860 : if (packet.GetCode () == Cody::Client::PC_PATHNAME)
19784 : 3857 : filename = xstrdup (packet.GetString ().c_str ());
19785 : : else
19786 : : {
19787 : 3 : gcc_checking_assert (packet.GetCode () == Cody::Client::PC_ERROR);
19788 : 3 : error_at (loc, "unknown Compiled Module Interface: %s",
19789 : 3 : packet.GetString ().c_str ());
19790 : : }
19791 : 3860 : }
19792 : :
19793 : : /* Figure out whether to treat HEADER as an include or an import. */
19794 : :
19795 : : static char *
19796 : 29485 : maybe_translate_include (cpp_reader *reader, line_maps *lmaps, location_t loc,
19797 : : const char *path)
19798 : : {
19799 : 29485 : if (!modules_p ())
19800 : : {
19801 : : /* Turn off. */
19802 : 0 : cpp_get_callbacks (reader)->translate_include = NULL;
19803 : 0 : return nullptr;
19804 : : }
19805 : :
19806 : 29485 : if (!spans.init_p ())
19807 : : /* Before the main file, don't divert. */
19808 : : return nullptr;
19809 : :
19810 : 26280 : dump.push (NULL);
19811 : :
19812 : 27142 : dump () && dump ("Checking include translation '%s'", path);
19813 : 26280 : auto *mapper = get_mapper (cpp_main_loc (reader), cpp_get_deps (reader));
19814 : :
19815 : 26280 : size_t len = strlen (path);
19816 : 26280 : path = canonicalize_header_name (NULL, loc, true, path, len);
19817 : 26280 : auto packet = mapper->IncludeTranslate (path, Cody::Flags::None, len);
19818 : 26280 : int xlate = false;
19819 : 26280 : if (packet.GetCode () == Cody::Client::PC_BOOL)
19820 : 26252 : xlate = -int (packet.GetInteger ());
19821 : 28 : else if (packet.GetCode () == Cody::Client::PC_PATHNAME)
19822 : : {
19823 : : /* Record the CMI name for when we do the import. */
19824 : 28 : module_state *import = get_module (build_string (len, path));
19825 : 28 : import->set_filename (packet);
19826 : 28 : xlate = +1;
19827 : : }
19828 : : else
19829 : : {
19830 : 0 : gcc_checking_assert (packet.GetCode () == Cody::Client::PC_ERROR);
19831 : 0 : error_at (loc, "cannot determine %<#include%> translation of %s: %s",
19832 : 0 : path, packet.GetString ().c_str ());
19833 : : }
19834 : :
19835 : 26280 : bool note = false;
19836 : 26280 : if (note_include_translate_yes && xlate > 1)
19837 : : note = true;
19838 : 26280 : else if (note_include_translate_no && xlate == 0)
19839 : : note = true;
19840 : 26280 : else if (note_includes)
19841 : : /* We do not expect the note_includes vector to be large, so O(N)
19842 : : iteration. */
19843 : 1192 : for (unsigned ix = note_includes->length (); !note && ix--;)
19844 : 596 : if (!strcmp ((*note_includes)[ix], path))
19845 : 3 : note = true;
19846 : :
19847 : 596 : if (note)
19848 : 3 : inform (loc, xlate
19849 : : ? G_("include %qs translated to import")
19850 : : : G_("include %qs processed textually") , path);
19851 : :
19852 : 28004 : dump () && dump (xlate ? "Translating include to import"
19853 : : : "Keeping include as include");
19854 : 26280 : dump.pop (0);
19855 : :
19856 : 26280 : if (!(xlate > 0))
19857 : : return nullptr;
19858 : :
19859 : : /* Create the translation text. */
19860 : 28 : loc = ordinary_loc_of (lmaps, loc);
19861 : 28 : const line_map_ordinary *map
19862 : 28 : = linemap_check_ordinary (linemap_lookup (lmaps, loc));
19863 : 28 : unsigned col = SOURCE_COLUMN (map, loc);
19864 : 28 : col -= (col != 0); /* Columns are 1-based. */
19865 : :
19866 : 28 : unsigned alloc = len + col + 60;
19867 : 28 : char *res = XNEWVEC (char, alloc);
19868 : :
19869 : 28 : strcpy (res, "__import");
19870 : 28 : unsigned actual = 8;
19871 : 28 : if (col > actual)
19872 : : {
19873 : : /* Pad out so the filename appears at the same position. */
19874 : 28 : memset (res + actual, ' ', col - actual);
19875 : 28 : actual = col;
19876 : : }
19877 : : /* No need to encode characters, that's not how header names are
19878 : : handled. */
19879 : 28 : actual += snprintf (res + actual, alloc - actual,
19880 : : "\"%s\" [[__translated]];\n", path);
19881 : 28 : gcc_checking_assert (actual < alloc);
19882 : :
19883 : : /* cpplib will delete the buffer. */
19884 : : return res;
19885 : 26280 : }
19886 : :
19887 : : static void
19888 : 707 : begin_header_unit (cpp_reader *reader)
19889 : : {
19890 : : /* Set the module header name from the main_input_filename. */
19891 : 707 : const char *main = main_input_filename;
19892 : 707 : size_t len = strlen (main);
19893 : 707 : main = canonicalize_header_name (NULL, 0, true, main, len);
19894 : 707 : module_state *module = get_module (build_string (len, main));
19895 : :
19896 : 707 : preprocess_module (module, cpp_main_loc (reader), false, false, true, reader);
19897 : 707 : }
19898 : :
19899 : : /* We've just properly entered the main source file. I.e. after the
19900 : : command line, builtins and forced headers. Record the line map and
19901 : : location of this map. Note we may be called more than once. The
19902 : : first call sticks. */
19903 : :
19904 : : void
19905 : 100974 : module_begin_main_file (cpp_reader *reader, line_maps *lmaps,
19906 : : const line_map_ordinary *map)
19907 : : {
19908 : 100974 : gcc_checking_assert (lmaps == line_table);
19909 : 100974 : if (modules_p () && !spans.init_p ())
19910 : : {
19911 : 3235 : unsigned n = dump.push (NULL);
19912 : 3235 : spans.init (lmaps, map);
19913 : 3235 : dump.pop (n);
19914 : 3235 : if (flag_header_unit && !cpp_get_options (reader)->preprocessed)
19915 : : {
19916 : : /* Tell the preprocessor this is an include file. */
19917 : 698 : cpp_retrofit_as_include (reader);
19918 : 698 : begin_header_unit (reader);
19919 : : }
19920 : : }
19921 : 100974 : }
19922 : :
19923 : : /* Process the pending_import queue, making sure we know the
19924 : : filenames. */
19925 : :
19926 : : static void
19927 : 3947 : name_pending_imports (cpp_reader *reader)
19928 : : {
19929 : 3947 : auto *mapper = get_mapper (cpp_main_loc (reader), cpp_get_deps (reader));
19930 : :
19931 : 3947 : if (!vec_safe_length (pending_imports))
19932 : : /* Not doing anything. */
19933 : : return;
19934 : :
19935 : 3310 : timevar_start (TV_MODULE_MAPPER);
19936 : :
19937 : 3310 : auto n = dump.push (NULL);
19938 : 3828 : dump () && dump ("Resolving direct import names");
19939 : 3310 : bool want_deps = (bool (mapper->get_flags () & Cody::Flags::NameOnly)
19940 : 3310 : || cpp_get_deps (reader));
19941 : 3310 : bool any = false;
19942 : :
19943 : 7242 : for (unsigned ix = 0; ix != pending_imports->length (); ix++)
19944 : : {
19945 : 3932 : module_state *module = (*pending_imports)[ix];
19946 : 3932 : gcc_checking_assert (module->is_direct ());
19947 : 3932 : if (!module->filename && !module->visited_p)
19948 : : {
19949 : 3856 : bool export_p = (module->module_p
19950 : 3856 : && (module->is_partition () || module->exported_p));
19951 : :
19952 : 3856 : Cody::Flags flags = Cody::Flags::None;
19953 : 3856 : if (flag_preprocess_only
19954 : 3856 : && !(module->is_header () && !export_p))
19955 : : {
19956 : 84 : if (!want_deps)
19957 : 24 : continue;
19958 : : flags = Cody::Flags::NameOnly;
19959 : : }
19960 : :
19961 : 3832 : if (!any)
19962 : : {
19963 : 3291 : any = true;
19964 : 3291 : mapper->Cork ();
19965 : : }
19966 : 3832 : if (export_p)
19967 : 1927 : mapper->ModuleExport (module->get_flatname (), flags);
19968 : : else
19969 : 1905 : mapper->ModuleImport (module->get_flatname (), flags);
19970 : 3832 : module->visited_p = true;
19971 : : }
19972 : : }
19973 : :
19974 : 3310 : if (any)
19975 : : {
19976 : 3291 : auto response = mapper->Uncork ();
19977 : 3291 : auto r_iter = response.begin ();
19978 : 7192 : for (unsigned ix = 0; ix != pending_imports->length (); ix++)
19979 : : {
19980 : 3901 : module_state *module = (*pending_imports)[ix];
19981 : 3901 : if (module->visited_p)
19982 : : {
19983 : 3832 : module->visited_p = false;
19984 : 3832 : gcc_checking_assert (!module->filename);
19985 : :
19986 : 3832 : module->set_filename (*r_iter);
19987 : 3832 : ++r_iter;
19988 : : }
19989 : : }
19990 : 3291 : }
19991 : :
19992 : 3310 : dump.pop (n);
19993 : :
19994 : 3310 : timevar_stop (TV_MODULE_MAPPER);
19995 : : }
19996 : :
19997 : : /* We've just lexed a module-specific control line for MODULE. Mark
19998 : : the module as a direct import, and possibly load up its macro
19999 : : state. Returns the primary module, if this is a module
20000 : : declaration. */
20001 : : /* Perhaps we should offer a preprocessing mode where we read the
20002 : : directives from the header unit, rather than require the header's
20003 : : CMI. */
20004 : :
20005 : : module_state *
20006 : 3948 : preprocess_module (module_state *module, location_t from_loc,
20007 : : bool in_purview, bool is_import, bool is_export,
20008 : : cpp_reader *reader)
20009 : : {
20010 : 3948 : if (!is_import)
20011 : : {
20012 : 2164 : if (module->loc)
20013 : : /* It's already been mentioned, so ignore its module-ness. */
20014 : : is_import = true;
20015 : : else
20016 : : {
20017 : : /* Record it is the module. */
20018 : 2152 : module->module_p = true;
20019 : 2152 : if (is_export)
20020 : : {
20021 : 1912 : module->exported_p = true;
20022 : 1912 : module->interface_p = true;
20023 : : }
20024 : : }
20025 : : }
20026 : :
20027 : 3948 : if (module->directness < MD_DIRECT + in_purview)
20028 : : {
20029 : : /* Mark as a direct import. */
20030 : 3905 : module->directness = module_directness (MD_DIRECT + in_purview);
20031 : :
20032 : : /* Set the location to be most informative for users. */
20033 : 3905 : from_loc = ordinary_loc_of (line_table, from_loc);
20034 : 3905 : if (module->loadedness != ML_NONE)
20035 : 3 : linemap_module_reparent (line_table, module->loc, from_loc);
20036 : : else
20037 : : {
20038 : 3902 : module->loc = from_loc;
20039 : 3902 : if (!module->flatname)
20040 : 3884 : module->set_flatname ();
20041 : : }
20042 : : }
20043 : :
20044 : 3948 : auto desired = ML_CONFIG;
20045 : 3948 : if (is_import
20046 : 1796 : && module->is_header ()
20047 : 4673 : && (!cpp_get_options (reader)->preprocessed
20048 : 3 : || cpp_get_options (reader)->directives_only))
20049 : : /* We need preprocessor state now. */
20050 : : desired = ML_PREPROCESSOR;
20051 : :
20052 : 3948 : if (!is_import || module->loadedness < desired)
20053 : : {
20054 : 3932 : vec_safe_push (pending_imports, module);
20055 : :
20056 : 3932 : if (desired == ML_PREPROCESSOR)
20057 : : {
20058 : 709 : unsigned n = dump.push (NULL);
20059 : :
20060 : 897 : dump () && dump ("Reading %M preprocessor state", module);
20061 : 709 : name_pending_imports (reader);
20062 : :
20063 : : /* Preserve the state of the line-map. */
20064 : 709 : unsigned pre_hwm = LINEMAPS_ORDINARY_USED (line_table);
20065 : :
20066 : : /* We only need to close the span, if we're going to emit a
20067 : : CMI. But that's a little tricky -- our token scanner
20068 : : needs to be smarter -- and this isn't much state.
20069 : : Remember, we've not parsed anything at this point, so
20070 : : our module state flags are inadequate. */
20071 : 709 : spans.maybe_init ();
20072 : 709 : spans.close ();
20073 : :
20074 : 709 : timevar_start (TV_MODULE_IMPORT);
20075 : :
20076 : : /* Load the config of each pending import -- we must assign
20077 : : module numbers monotonically. */
20078 : 1578 : for (unsigned ix = 0; ix != pending_imports->length (); ix++)
20079 : : {
20080 : 869 : auto *import = (*pending_imports)[ix];
20081 : 1020 : if (!(import->module_p
20082 : 151 : && (import->is_partition () || import->exported_p))
20083 : 724 : && import->loadedness == ML_NONE
20084 : 1593 : && (import->is_header () || !flag_preprocess_only))
20085 : : {
20086 : 715 : unsigned n = dump.push (import);
20087 : 715 : import->do_import (reader, true);
20088 : 715 : dump.pop (n);
20089 : : }
20090 : : }
20091 : 709 : vec_free (pending_imports);
20092 : :
20093 : : /* Restore the line-map state. */
20094 : 709 : spans.open (linemap_module_restore (line_table, pre_hwm));
20095 : :
20096 : : /* Now read the preprocessor state of this particular
20097 : : import. */
20098 : 709 : if (module->loadedness == ML_CONFIG
20099 : 709 : && module->read_preprocessor (true))
20100 : 706 : module->import_macros ();
20101 : :
20102 : 709 : timevar_stop (TV_MODULE_IMPORT);
20103 : :
20104 : 709 : dump.pop (n);
20105 : : }
20106 : : }
20107 : :
20108 : 3948 : return is_import ? NULL : get_primary (module);
20109 : : }
20110 : :
20111 : : /* We've completed phase-4 translation. Emit any dependency
20112 : : information for the not-yet-loaded direct imports, and fill in
20113 : : their file names. We'll have already loaded up the direct header
20114 : : unit wavefront. */
20115 : :
20116 : : void
20117 : 3238 : preprocessed_module (cpp_reader *reader)
20118 : : {
20119 : 3238 : unsigned n = dump.push (NULL);
20120 : :
20121 : 3720 : dump () && dump ("Completed phase-4 (tokenization) processing");
20122 : :
20123 : 3238 : name_pending_imports (reader);
20124 : 3238 : vec_free (pending_imports);
20125 : :
20126 : 3238 : spans.maybe_init ();
20127 : 3238 : spans.close ();
20128 : :
20129 : 3238 : using iterator = hash_table<module_state_hash>::iterator;
20130 : 3238 : if (mkdeps *deps = cpp_get_deps (reader))
20131 : : {
20132 : : /* Walk the module hash, informing the dependency machinery. */
20133 : 42 : iterator end = modules_hash->end ();
20134 : 258 : for (iterator iter = modules_hash->begin (); iter != end; ++iter)
20135 : : {
20136 : 87 : module_state *module = *iter;
20137 : :
20138 : 87 : if (module->is_direct ())
20139 : : {
20140 : 72 : if (module->is_module ()
20141 : 72 : && (module->is_interface () || module->is_partition ()))
20142 : 30 : deps_add_module_target (deps, module->get_flatname (),
20143 : 30 : maybe_add_cmi_prefix (module->filename),
20144 : 30 : module->is_header (),
20145 : 30 : module->is_exported ());
20146 : : else
20147 : 42 : deps_add_module_dep (deps, module->get_flatname ());
20148 : : }
20149 : : }
20150 : : }
20151 : :
20152 : 3238 : if (flag_header_unit && !flag_preprocess_only)
20153 : : {
20154 : : /* Find the main module -- remember, it's not yet in the module
20155 : : array. */
20156 : 698 : iterator end = modules_hash->end ();
20157 : 1484 : for (iterator iter = modules_hash->begin (); iter != end; ++iter)
20158 : : {
20159 : 742 : module_state *module = *iter;
20160 : 742 : if (module->is_module ())
20161 : : {
20162 : 698 : declare_module (module, cpp_main_loc (reader), true, NULL, reader);
20163 : 698 : module_kind |= MK_EXPORTING;
20164 : 698 : break;
20165 : : }
20166 : : }
20167 : : }
20168 : :
20169 : 3238 : dump.pop (n);
20170 : 3238 : }
20171 : :
20172 : : /* VAL is a global tree, add it to the global vec if it is
20173 : : interesting. Add some of its targets, if they too are
20174 : : interesting. We do not add identifiers, as they can be re-found
20175 : : via the identifier hash table. There is a cost to the number of
20176 : : global trees. */
20177 : :
20178 : : static int
20179 : 1996785 : maybe_add_global (tree val, unsigned &crc)
20180 : : {
20181 : 1996785 : int v = 0;
20182 : :
20183 : 1996785 : if (val && !(identifier_p (val) || TREE_VISITED (val)))
20184 : : {
20185 : 617397 : TREE_VISITED (val) = true;
20186 : 617397 : crc = crc32_unsigned (crc, fixed_trees->length ());
20187 : 617397 : vec_safe_push (fixed_trees, val);
20188 : 617397 : v++;
20189 : :
20190 : 617397 : if (CODE_CONTAINS_STRUCT (TREE_CODE (val), TS_TYPED))
20191 : 617397 : v += maybe_add_global (TREE_TYPE (val), crc);
20192 : 617397 : if (CODE_CONTAINS_STRUCT (TREE_CODE (val), TS_TYPE_COMMON))
20193 : 401512 : v += maybe_add_global (TYPE_NAME (val), crc);
20194 : : }
20195 : :
20196 : 1996785 : return v;
20197 : : }
20198 : :
20199 : : /* Initialize module state. Create the hash table, determine the
20200 : : global trees. Create the module for current TU. */
20201 : :
20202 : : void
20203 : 3238 : init_modules (cpp_reader *reader)
20204 : : {
20205 : : /* PCH should not be reachable because of lang-specs, but the
20206 : : user could have overriden that. */
20207 : 3238 : if (pch_file)
20208 : 0 : fatal_error (input_location,
20209 : : "C++ modules are incompatible with precompiled headers");
20210 : :
20211 : 3238 : if (cpp_get_options (reader)->traditional)
20212 : 0 : fatal_error (input_location,
20213 : : "C++ modules are incompatible with traditional preprocessing");
20214 : :
20215 : 3238 : if (flag_preprocess_only)
20216 : : {
20217 : 57 : cpp_options *cpp_opts = cpp_get_options (reader);
20218 : 57 : if (flag_no_output
20219 : 57 : || (cpp_opts->deps.style != DEPS_NONE
20220 : 33 : && !cpp_opts->deps.need_preprocessor_output))
20221 : : {
20222 : 0 : warning (0, flag_dump_macros == 'M'
20223 : : ? G_("macro debug output may be incomplete with modules")
20224 : : : G_("module dependencies require preprocessing"));
20225 : 0 : if (cpp_opts->deps.style != DEPS_NONE)
20226 : 0 : inform (input_location, "you should use the %<-%s%> option",
20227 : : cpp_opts->deps.style == DEPS_SYSTEM ? "MD" : "MMD");
20228 : : }
20229 : : }
20230 : :
20231 : : /* :: is always exported. */
20232 : 3238 : DECL_MODULE_EXPORT_P (global_namespace) = true;
20233 : :
20234 : 3238 : modules_hash = hash_table<module_state_hash>::create_ggc (31);
20235 : 3238 : vec_safe_reserve (modules, 20);
20236 : :
20237 : : /* Create module for current TU. */
20238 : 3238 : module_state *current
20239 : 3238 : = new (ggc_alloc<module_state> ()) module_state (NULL_TREE, NULL, false);
20240 : 3238 : current->mod = 0;
20241 : 3238 : bitmap_set_bit (current->imports, 0);
20242 : 3238 : modules->quick_push (current);
20243 : :
20244 : 3238 : gcc_checking_assert (!fixed_trees);
20245 : :
20246 : 3238 : headers = BITMAP_GGC_ALLOC ();
20247 : :
20248 : 3238 : if (note_includes)
20249 : : /* Canonicalize header names. */
20250 : 6 : for (unsigned ix = 0; ix != note_includes->length (); ix++)
20251 : : {
20252 : 3 : const char *hdr = (*note_includes)[ix];
20253 : 3 : size_t len = strlen (hdr);
20254 : :
20255 : 3 : bool system = hdr[0] == '<';
20256 : 3 : bool user = hdr[0] == '"';
20257 : 3 : bool delimed = system || user;
20258 : :
20259 : 3 : if (len <= (delimed ? 2 : 0)
20260 : 3 : || (delimed && hdr[len-1] != (system ? '>' : '"')))
20261 : 0 : error ("invalid header name %qs", hdr);
20262 : :
20263 : 3 : hdr = canonicalize_header_name (delimed ? reader : NULL,
20264 : : 0, !delimed, hdr, len);
20265 : 3 : char *path = XNEWVEC (char, len + 1);
20266 : 3 : memcpy (path, hdr, len);
20267 : 3 : path[len] = 0;
20268 : :
20269 : 3 : (*note_includes)[ix] = path;
20270 : : }
20271 : :
20272 : 3238 : if (note_cmis)
20273 : : /* Canonicalize & mark module names. */
20274 : 12 : for (unsigned ix = 0; ix != note_cmis->length (); ix++)
20275 : : {
20276 : 6 : const char *name = (*note_cmis)[ix];
20277 : 6 : size_t len = strlen (name);
20278 : :
20279 : 6 : bool is_system = name[0] == '<';
20280 : 6 : bool is_user = name[0] == '"';
20281 : 6 : bool is_pathname = false;
20282 : 6 : if (!(is_system || is_user))
20283 : 12 : for (unsigned ix = len; !is_pathname && ix--;)
20284 : 9 : is_pathname = IS_DIR_SEPARATOR (name[ix]);
20285 : 6 : if (is_system || is_user || is_pathname)
20286 : : {
20287 : 3 : if (len <= (is_pathname ? 0 : 2)
20288 : 3 : || (!is_pathname && name[len-1] != (is_system ? '>' : '"')))
20289 : : {
20290 : 0 : error ("invalid header name %qs", name);
20291 : 0 : continue;
20292 : : }
20293 : : else
20294 : 3 : name = canonicalize_header_name (is_pathname ? nullptr : reader,
20295 : : 0, is_pathname, name, len);
20296 : : }
20297 : 6 : if (auto module = get_module (name))
20298 : 6 : module->inform_cmi_p = 1;
20299 : : else
20300 : 0 : error ("invalid module name %qs", name);
20301 : : }
20302 : :
20303 : 3238 : dump.push (NULL);
20304 : :
20305 : : /* Determine lazy handle bound. */
20306 : 3238 : {
20307 : 3238 : unsigned limit = 1000;
20308 : : #if HAVE_GETRLIMIT
20309 : 3238 : struct rlimit rlimit;
20310 : 3238 : if (!getrlimit (RLIMIT_NOFILE, &rlimit))
20311 : : {
20312 : 3238 : lazy_hard_limit = (rlimit.rlim_max < 1000000
20313 : 3238 : ? unsigned (rlimit.rlim_max) : 1000000);
20314 : 3238 : lazy_hard_limit = (lazy_hard_limit > LAZY_HEADROOM
20315 : 3238 : ? lazy_hard_limit - LAZY_HEADROOM : 0);
20316 : 3238 : if (rlimit.rlim_cur < limit)
20317 : 0 : limit = unsigned (rlimit.rlim_cur);
20318 : : }
20319 : : #endif
20320 : 3238 : limit = limit > LAZY_HEADROOM ? limit - LAZY_HEADROOM : 1;
20321 : :
20322 : 3238 : if (unsigned parm = param_lazy_modules)
20323 : : {
20324 : 3238 : if (parm <= limit || !lazy_hard_limit || !try_increase_lazy (parm))
20325 : 6 : lazy_limit = parm;
20326 : : }
20327 : : else
20328 : 0 : lazy_limit = limit;
20329 : : }
20330 : :
20331 : 3238 : if (dump ())
20332 : : {
20333 : 482 : verstr_t ver;
20334 : 482 : version2string (MODULE_VERSION, ver);
20335 : 482 : dump ("Source: %s", main_input_filename);
20336 : 482 : dump ("Compiler: %s", version_string);
20337 : 482 : dump ("Modules: %s", ver);
20338 : 482 : dump ("Checking: %s",
20339 : : #if CHECKING_P
20340 : : "checking"
20341 : : #elif ENABLE_ASSERT_CHECKING
20342 : : "asserting"
20343 : : #else
20344 : : "release"
20345 : : #endif
20346 : : );
20347 : 482 : dump ("Compiled by: "
20348 : : #ifdef __GNUC__
20349 : : "GCC %d.%d, %s", __GNUC__, __GNUC_MINOR__,
20350 : : #ifdef __OPTIMIZE__
20351 : : "optimizing"
20352 : : #else
20353 : : "not optimizing"
20354 : : #endif
20355 : : #else
20356 : : "not GCC"
20357 : : #endif
20358 : : );
20359 : 482 : dump ("Reading: %s", MAPPED_READING ? "mmap" : "fileio");
20360 : 482 : dump ("Writing: %s", MAPPED_WRITING ? "mmap" : "fileio");
20361 : 482 : dump ("Lazy limit: %u", lazy_limit);
20362 : 482 : dump ("Lazy hard limit: %u", lazy_hard_limit);
20363 : 482 : dump ("");
20364 : : }
20365 : :
20366 : : /* Construct the global tree array. This is an array of unique
20367 : : global trees (& types). Do this now, rather than lazily, as
20368 : : some global trees are lazily created and we don't want that to
20369 : : mess with our syndrome of fixed trees. */
20370 : 3238 : unsigned crc = 0;
20371 : 3238 : vec_alloc (fixed_trees, 250);
20372 : :
20373 : 3720 : dump () && dump ("+Creating globals");
20374 : : /* Insert the TRANSLATION_UNIT_DECL. */
20375 : 3238 : TREE_VISITED (DECL_CONTEXT (global_namespace)) = true;
20376 : 3238 : fixed_trees->quick_push (DECL_CONTEXT (global_namespace));
20377 : 19428 : for (unsigned jx = 0; global_tree_arys[jx].first; jx++)
20378 : : {
20379 : 16190 : const tree *ptr = global_tree_arys[jx].first;
20380 : 16190 : unsigned limit = global_tree_arys[jx].second;
20381 : :
20382 : 984352 : for (unsigned ix = 0; ix != limit; ix++, ptr++)
20383 : : {
20384 : 968162 : !(ix & 31) && dump ("") && dump ("+\t%u:%u:", jx, ix);
20385 : 968162 : unsigned v = maybe_add_global (*ptr, crc);
20386 : 1112280 : dump () && dump ("+%u", v);
20387 : : }
20388 : : }
20389 : : /* OS- and machine-specific types are dynamically registered at
20390 : : runtime, so cannot be part of global_tree_arys. */
20391 : 3238 : registered_builtin_types && dump ("") && dump ("+\tB:");
20392 : 12952 : for (tree t = registered_builtin_types; t; t = TREE_CHAIN (t))
20393 : : {
20394 : 9714 : unsigned v = maybe_add_global (TREE_VALUE (t), crc);
20395 : 11160 : dump () && dump ("+%u", v);
20396 : : }
20397 : 3238 : global_crc = crc32_unsigned (crc, fixed_trees->length ());
20398 : 3238 : dump ("") && dump ("Created %u unique globals, crc=%x",
20399 : : fixed_trees->length (), global_crc);
20400 : 623873 : for (unsigned ix = fixed_trees->length (); ix--;)
20401 : 620635 : TREE_VISITED ((*fixed_trees)[ix]) = false;
20402 : :
20403 : 3238 : dump.pop (0);
20404 : :
20405 : 3238 : if (!flag_module_lazy)
20406 : : /* Get the mapper now, if we're not being lazy. */
20407 : 254 : get_mapper (cpp_main_loc (reader), cpp_get_deps (reader));
20408 : :
20409 : 3238 : if (!flag_preprocess_only)
20410 : : {
20411 : 3181 : pending_table = new pending_map_t (EXPERIMENT (1, 400));
20412 : 3181 : entity_map = new entity_map_t (EXPERIMENT (1, 400));
20413 : 3181 : vec_safe_reserve (entity_ary, EXPERIMENT (1, 400));
20414 : : }
20415 : :
20416 : : #if CHECKING_P
20417 : 3238 : note_defs = note_defs_table_t::create_ggc (1000);
20418 : : #endif
20419 : :
20420 : 3238 : if (flag_header_unit && cpp_get_options (reader)->preprocessed)
20421 : 9 : begin_header_unit (reader);
20422 : :
20423 : : /* Collect here to make sure things are tagged correctly (when
20424 : : aggressively GC'd). */
20425 : 3238 : ggc_collect ();
20426 : 3238 : }
20427 : :
20428 : : /* If NODE is a deferred macro, load it. */
20429 : :
20430 : : static int
20431 : 77731 : load_macros (cpp_reader *reader, cpp_hashnode *node, void *)
20432 : : {
20433 : 77731 : location_t main_loc
20434 : 77731 : = MAP_START_LOCATION (LINEMAPS_ORDINARY_MAP_AT (line_table, 0));
20435 : :
20436 : 77731 : if (cpp_user_macro_p (node)
20437 : 77731 : && !node->value.macro)
20438 : : {
20439 : 72 : cpp_macro *macro = cpp_get_deferred_macro (reader, node, main_loc);
20440 : 72 : dump () && dump ("Loaded macro #%s %I",
20441 : : macro ? "define" : "undef", identifier (node));
20442 : : }
20443 : :
20444 : 77731 : return 1;
20445 : : }
20446 : :
20447 : : /* At the end of tokenizing, we no longer need the macro tables of
20448 : : imports. But the user might have requested some checking. */
20449 : :
20450 : : void
20451 : 99341 : maybe_check_all_macros (cpp_reader *reader)
20452 : : {
20453 : 99341 : if (!warn_imported_macros)
20454 : : return;
20455 : :
20456 : : /* Force loading of any remaining deferred macros. This will
20457 : : produce diagnostics if they are ill-formed. */
20458 : 21 : unsigned n = dump.push (NULL);
20459 : 21 : cpp_forall_identifiers (reader, load_macros, NULL);
20460 : 21 : dump.pop (n);
20461 : : }
20462 : :
20463 : : // State propagated from finish_module_processing to fini_modules
20464 : :
20465 : : struct module_processing_cookie
20466 : : {
20467 : : elf_out out;
20468 : : module_state_config config;
20469 : : char *cmi_name;
20470 : : char *tmp_name;
20471 : : unsigned crc;
20472 : : bool began;
20473 : :
20474 : 1894 : module_processing_cookie (char *cmi, char *tmp, int fd, int e)
20475 : 1894 : : out (fd, e), cmi_name (cmi), tmp_name (tmp), crc (0), began (false)
20476 : : {
20477 : : }
20478 : 1894 : ~module_processing_cookie ()
20479 : : {
20480 : 1894 : XDELETEVEC (tmp_name);
20481 : 1894 : XDELETEVEC (cmi_name);
20482 : 1894 : }
20483 : : };
20484 : :
20485 : : /* Write the CMI, if we're a module interface. */
20486 : :
20487 : : void *
20488 : 99146 : finish_module_processing (cpp_reader *reader)
20489 : : {
20490 : 99146 : module_processing_cookie *cookie = nullptr;
20491 : :
20492 : 99146 : if (header_module_p ())
20493 : 698 : module_kind &= ~MK_EXPORTING;
20494 : :
20495 : 99146 : if (!modules || !(*modules)[0]->name)
20496 : : {
20497 : 97249 : if (flag_module_only)
20498 : 6 : warning (0, "%<-fmodule-only%> used for non-interface");
20499 : : }
20500 : 1897 : else if (!flag_syntax_only)
20501 : : {
20502 : 1894 : int fd = -1;
20503 : 1894 : int e = -1;
20504 : :
20505 : 1894 : timevar_start (TV_MODULE_EXPORT);
20506 : :
20507 : : /* Force a valid but empty line map at the end. This simplifies
20508 : : the line table preparation and writing logic. */
20509 : 1894 : linemap_add (line_table, LC_ENTER, false, "", 0);
20510 : :
20511 : : /* We write to a tmpname, and then atomically rename. */
20512 : 1894 : char *cmi_name = NULL;
20513 : 1894 : char *tmp_name = NULL;
20514 : 1894 : module_state *state = (*modules)[0];
20515 : :
20516 : 1894 : unsigned n = dump.push (state);
20517 : 1894 : state->announce ("creating");
20518 : 1894 : if (state->filename)
20519 : : {
20520 : 1891 : size_t len = 0;
20521 : 1891 : cmi_name = xstrdup (maybe_add_cmi_prefix (state->filename, &len));
20522 : 1891 : tmp_name = XNEWVEC (char, len + 3);
20523 : 1891 : memcpy (tmp_name, cmi_name, len);
20524 : 1891 : strcpy (&tmp_name[len], "~");
20525 : :
20526 : 1891 : if (!errorcount)
20527 : 55 : for (unsigned again = 2; ; again--)
20528 : : {
20529 : 1879 : fd = open (tmp_name,
20530 : : O_RDWR | O_CREAT | O_TRUNC | O_CLOEXEC | O_BINARY,
20531 : : S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH);
20532 : 1879 : e = errno;
20533 : 1879 : if (fd >= 0 || !again || e != ENOENT)
20534 : : break;
20535 : 55 : create_dirs (tmp_name);
20536 : : }
20537 : 1891 : if (note_module_cmi_yes || state->inform_cmi_p)
20538 : 3 : inform (state->loc, "writing CMI %qs", cmi_name);
20539 : 2119 : dump () && dump ("CMI is %s", cmi_name);
20540 : : }
20541 : :
20542 : 1894 : cookie = new module_processing_cookie (cmi_name, tmp_name, fd, e);
20543 : :
20544 : 1894 : if (errorcount)
20545 : 70 : warning_at (state->loc, 0, "not writing module %qs due to errors",
20546 : : state->get_flatname ());
20547 : 1824 : else if (cookie->out.begin ())
20548 : : {
20549 : 1824 : cookie->began = true;
20550 : 1824 : auto loc = input_location;
20551 : : /* So crashes finger-point the module decl. */
20552 : 1824 : input_location = state->loc;
20553 : 1824 : state->write_begin (&cookie->out, reader, cookie->config, cookie->crc);
20554 : 1824 : input_location = loc;
20555 : : }
20556 : :
20557 : 1894 : dump.pop (n);
20558 : 1894 : timevar_stop (TV_MODULE_EXPORT);
20559 : :
20560 : 1894 : ggc_collect ();
20561 : : }
20562 : :
20563 : 99146 : if (modules)
20564 : : {
20565 : 3153 : unsigned n = dump.push (NULL);
20566 : 3635 : dump () && dump ("Imported %u modules", modules->length () - 1);
20567 : 3635 : dump () && dump ("Containing %u clusters", available_clusters);
20568 : 3153 : dump () && dump ("Loaded %u clusters (%u%%)", loaded_clusters,
20569 : 482 : (loaded_clusters * 100 + available_clusters / 2) /
20570 : 482 : (available_clusters + !available_clusters));
20571 : 3153 : dump.pop (n);
20572 : : }
20573 : :
20574 : 99146 : return cookie;
20575 : : }
20576 : :
20577 : : // Do the final emission of a module. At this point we know whether
20578 : : // the module static initializer is a NOP or not.
20579 : :
20580 : : static void
20581 : 1894 : late_finish_module (cpp_reader *reader, module_processing_cookie *cookie,
20582 : : bool init_fn_non_empty)
20583 : : {
20584 : 1894 : timevar_start (TV_MODULE_EXPORT);
20585 : :
20586 : 1894 : module_state *state = (*modules)[0];
20587 : 1894 : unsigned n = dump.push (state);
20588 : 1894 : state->announce ("finishing");
20589 : :
20590 : 1894 : cookie->config.active_init = init_fn_non_empty;
20591 : 1894 : if (cookie->began)
20592 : 1824 : state->write_end (&cookie->out, reader, cookie->config, cookie->crc);
20593 : :
20594 : 1894 : if (cookie->out.end () && cookie->cmi_name)
20595 : : {
20596 : : /* Some OS's do not replace NEWNAME if it already exists.
20597 : : This'll have a race condition in erroneous concurrent
20598 : : builds. */
20599 : 1818 : unlink (cookie->cmi_name);
20600 : 1818 : if (rename (cookie->tmp_name, cookie->cmi_name))
20601 : : {
20602 : 0 : dump () && dump ("Rename ('%s','%s') errno=%u",
20603 : 0 : cookie->tmp_name, cookie->cmi_name, errno);
20604 : 0 : cookie->out.set_error (errno);
20605 : : }
20606 : : }
20607 : :
20608 : 1894 : if (cookie->out.get_error () && cookie->began)
20609 : : {
20610 : 6 : error_at (state->loc, "failed to write compiled module: %s",
20611 : 6 : cookie->out.get_error (state->filename));
20612 : 6 : state->note_cmi_name ();
20613 : : }
20614 : :
20615 : 1894 : if (!errorcount)
20616 : : {
20617 : 1812 : auto *mapper = get_mapper (cpp_main_loc (reader), cpp_get_deps (reader));
20618 : 1812 : mapper->ModuleCompiled (state->get_flatname ());
20619 : : }
20620 : 82 : else if (cookie->cmi_name)
20621 : : {
20622 : : /* We failed, attempt to erase all evidence we even tried. */
20623 : 79 : unlink (cookie->tmp_name);
20624 : 79 : unlink (cookie->cmi_name);
20625 : : }
20626 : :
20627 : 1894 : delete cookie;
20628 : 1894 : dump.pop (n);
20629 : 1894 : timevar_stop (TV_MODULE_EXPORT);
20630 : 1894 : }
20631 : :
20632 : : void
20633 : 99146 : fini_modules (cpp_reader *reader, void *cookie, bool has_inits)
20634 : : {
20635 : 99146 : if (cookie)
20636 : 1894 : late_finish_module (reader,
20637 : : static_cast<module_processing_cookie *> (cookie),
20638 : : has_inits);
20639 : :
20640 : : /* We're done with the macro tables now. */
20641 : 99146 : vec_free (macro_exports);
20642 : 99146 : vec_free (macro_imports);
20643 : 99146 : headers = NULL;
20644 : :
20645 : : /* We're now done with everything but the module names. */
20646 : 99146 : set_cmi_repo (NULL);
20647 : 99146 : if (mapper)
20648 : : {
20649 : 3153 : timevar_start (TV_MODULE_MAPPER);
20650 : 3153 : module_client::close_module_client (0, mapper);
20651 : 3153 : mapper = nullptr;
20652 : 3153 : timevar_stop (TV_MODULE_MAPPER);
20653 : : }
20654 : 99146 : module_state_config::release ();
20655 : :
20656 : : #if CHECKING_P
20657 : 99146 : note_defs = NULL;
20658 : : #endif
20659 : :
20660 : 99146 : if (modules)
20661 : 5139 : for (unsigned ix = modules->length (); --ix;)
20662 : 1986 : if (module_state *state = (*modules)[ix])
20663 : 1986 : state->release ();
20664 : :
20665 : : /* No need to lookup modules anymore. */
20666 : 99146 : modules_hash = NULL;
20667 : :
20668 : : /* Or entity array. We still need the entity map to find import numbers. */
20669 : 99146 : vec_free (entity_ary);
20670 : 99146 : entity_ary = NULL;
20671 : :
20672 : : /* Or remember any pending entities. */
20673 : 102299 : delete pending_table;
20674 : 99146 : pending_table = NULL;
20675 : :
20676 : : /* Or any keys -- Let it go! */
20677 : 100755 : delete keyed_table;
20678 : 99146 : keyed_table = NULL;
20679 : :
20680 : : /* Allow a GC, we've possibly made much data unreachable. */
20681 : 99146 : ggc_collect ();
20682 : 99146 : }
20683 : :
20684 : : /* If CODE is a module option, handle it & return true. Otherwise
20685 : : return false. For unknown reasons I cannot get the option
20686 : : generation machinery to set fmodule-mapper or -fmodule-header to
20687 : : make a string type option variable. */
20688 : :
20689 : : bool
20690 : 1944615 : handle_module_option (unsigned code, const char *str, int)
20691 : : {
20692 : 1944615 : auto hdr = CMS_header;
20693 : :
20694 : 1944615 : switch (opt_code (code))
20695 : : {
20696 : 42 : case OPT_fmodule_mapper_:
20697 : 42 : module_mapper_name = str;
20698 : 42 : return true;
20699 : :
20700 : 15 : case OPT_fmodule_header_:
20701 : 15 : {
20702 : 15 : if (!strcmp (str, "user"))
20703 : : hdr = CMS_user;
20704 : 15 : else if (!strcmp (str, "system"))
20705 : : hdr = CMS_system;
20706 : : else
20707 : 0 : error ("unknown header kind %qs", str);
20708 : : }
20709 : : /* Fallthrough. */
20710 : :
20711 : 710 : case OPT_fmodule_header:
20712 : 710 : flag_header_unit = hdr;
20713 : 710 : flag_modules = 1;
20714 : 710 : return true;
20715 : :
20716 : 3 : case OPT_flang_info_include_translate_:
20717 : 3 : vec_safe_push (note_includes, str);
20718 : 3 : return true;
20719 : :
20720 : 6 : case OPT_flang_info_module_cmi_:
20721 : 6 : vec_safe_push (note_cmis, str);
20722 : 6 : return true;
20723 : :
20724 : : default:
20725 : : return false;
20726 : : }
20727 : : }
20728 : :
20729 : : /* Set preprocessor callbacks and options for modules. */
20730 : :
20731 : : void
20732 : 100715 : module_preprocess_options (cpp_reader *reader)
20733 : : {
20734 : 100715 : gcc_checking_assert (!lang_hooks.preprocess_undef);
20735 : 100715 : if (modules_p ())
20736 : : {
20737 : 3238 : auto *cb = cpp_get_callbacks (reader);
20738 : :
20739 : 3238 : cb->translate_include = maybe_translate_include;
20740 : 3238 : cb->user_deferred_macro = module_state::deferred_macro;
20741 : 3238 : if (flag_header_unit)
20742 : : {
20743 : : /* If the preprocessor hook is already in use, that
20744 : : implementation will call the undef langhook. */
20745 : 707 : if (cb->undef)
20746 : 0 : lang_hooks.preprocess_undef = module_state::undef_macro;
20747 : : else
20748 : 707 : cb->undef = module_state::undef_macro;
20749 : : }
20750 : 3238 : auto *opt = cpp_get_options (reader);
20751 : 3238 : opt->module_directives = true;
20752 : 3238 : opt->main_search = cpp_main_search (flag_header_unit);
20753 : : }
20754 : 100715 : }
20755 : :
20756 : : #include "gt-cp-module.h"
|