Branch data Line data Source code
1 : : /* C++ modules. Experimental!
2 : : Copyright (C) 2017-2025 Free Software Foundation, Inc.
3 : : Written by Nathan Sidwell <nathan@acm.org> while at FaceBook
4 : :
5 : : This file is part of GCC.
6 : :
7 : : GCC is free software; you can redistribute it and/or modify it
8 : : under the terms of the GNU General Public License as published by
9 : : the Free Software Foundation; either version 3, or (at your option)
10 : : any later version.
11 : :
12 : : GCC is distributed in the hope that it will be useful, but
13 : : WITHOUT ANY WARRANTY; without even the implied warranty of
14 : : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 : : General Public License for more details.
16 : :
17 : : You should have received a copy of the GNU General Public License
18 : : along with GCC; see the file COPYING3. If not see
19 : : <http://www.gnu.org/licenses/>. */
20 : :
21 : : /* Comments in this file have a non-negligible chance of being wrong
22 : : or at least inaccurate. Due to (a) my misunderstanding, (b)
23 : : ambiguities that I have interpretted differently to original intent
24 : : (c) changes in the specification, (d) my poor wording, (e) source
25 : : changes. */
26 : :
27 : : /* (Incomplete) Design Notes
28 : :
29 : : A hash table contains all module names. Imported modules are
30 : : present in a modules array, which by construction places an
31 : : import's dependencies before the import itself. The single
32 : : exception is the current TU, which always occupies slot zero (even
33 : : when it is not a module).
34 : :
35 : : Imported decls occupy an entity_ary, an array of binding_slots, indexed
36 : : by importing module and index within that module. A flat index is
37 : : used, as each module reserves a contiguous range of indices.
38 : : Initially each slot indicates the CMI section containing the
39 : : streamed decl. When the decl is imported it will point to the decl
40 : : itself.
41 : :
42 : : Additionally each imported decl is mapped in the entity_map via its
43 : : DECL_UID to the flat index in the entity_ary. Thus we can locate
44 : : the index for any imported decl by using this map and then
45 : : de-flattening the index via a binary seach of the module vector.
46 : : Cross-module references are by (remapped) module number and
47 : : module-local index.
48 : :
49 : : Each importable DECL contains several flags. The simple set are
50 : : DECL_MODULE_EXPORT_P, DECL_MODULE_PURVIEW_P, DECL_MODULE_ATTACH_P
51 : : and DECL_MODULE_IMPORT_P. The first indicates whether it is
52 : : exported, the second whether it is in module or header-unit
53 : : purview. The third indicates it is attached to the named module in
54 : : whose purview it resides and the fourth indicates whether it was an
55 : : import into this TU or not. DECL_MODULE_ATTACH_P will be false for
56 : : all decls in a header-unit, and for those in a named module inside
57 : : a linkage declaration.
58 : :
59 : : The more detailed flags are DECL_MODULE_PARTITION_P,
60 : : DECL_MODULE_ENTITY_P. The first is set in a primary interface unit
61 : : on decls that were read from module partitions (these will have
62 : : DECL_MODULE_IMPORT_P set too). Such decls will be streamed out to
63 : : the primary's CMI. DECL_MODULE_ENTITY_P is set when an entity is
64 : : imported, even if it matched a non-imported entity. Such a decl
65 : : will not have DECL_MODULE_IMPORT_P set, even though it has an entry
66 : : in the entity map and array.
67 : :
68 : : Header units are module-like.
69 : :
70 : : For namespace-scope lookup, the decls for a particular module are
71 : : held located in a sparse array hanging off the binding of the name.
72 : : This is partitioned into two: a few fixed slots at the start
73 : : followed by the sparse slots afterwards. By construction we only
74 : : need to append new slots to the end -- there is never a need to
75 : : insert in the middle. The fixed slots are MODULE_SLOT_CURRENT for
76 : : the current TU (regardless of whether it is a module or not),
77 : : MODULE_SLOT_GLOBAL and MODULE_SLOT_PARTITION. These latter two
78 : : slots are used for merging entities across the global module and
79 : : module partitions respectively. MODULE_SLOT_PARTITION is only
80 : : present in a module. Neither of those two slots is searched during
81 : : name lookup -- they are internal use only. This vector is created
82 : : lazily once we require it, if there is only a declaration from the
83 : : current TU, a regular binding is present. It is converted on
84 : : demand.
85 : :
86 : : OPTIMIZATION: Outside of the current TU, we only need ADL to work.
87 : : We could optimize regular lookup for the current TU by glomming all
88 : : the visible decls on its slot. Perhaps wait until design is a
89 : : little more settled though.
90 : :
91 : : There is only one instance of each extern-linkage namespace. It
92 : : appears in every module slot that makes it visible. It also
93 : : appears in MODULE_SLOT_GLOBAL. (It is an ODR violation if they
94 : : collide with some other global module entity.) We also have an
95 : : optimization that shares the slot for adjacent modules that declare
96 : : the same such namespace.
97 : :
98 : : A module interface compilation produces a Compiled Module Interface
99 : : (CMI). The format used is Encapsulated Lazy Records Of Numbered
100 : : Declarations, which is essentially ELF's section encapsulation. (As
101 : : all good nerds are aware, Elrond is half Elf.) Some sections are
102 : : named, and contain information about the module as a whole (indices
103 : : etc), and other sections are referenced by number. Although I
104 : : don't defend against actively hostile CMIs, there is some
105 : : checksumming involved to verify data integrity. When dumping out
106 : : an interface, we generate a graph of all the
107 : : independently-redeclarable DECLS that are needed, and the decls
108 : : they reference. From that we determine the strongly connected
109 : : components (SCC) within this TU. Each SCC is dumped to a separate
110 : : numbered section of the CMI. We generate a binding table section,
111 : : mapping each namespace&name to a defining section. This allows
112 : : lazy loading.
113 : :
114 : : Lazy loading employs mmap to map a read-only image of the CMI.
115 : : It thus only occupies address space and is paged in on demand,
116 : : backed by the CMI file itself. If mmap is unavailable, regular
117 : : FILEIO is used. Also, there's a bespoke ELF reader/writer here,
118 : : which implements just the section table and sections (including
119 : : string sections) of a 32-bit ELF in host byte-order. You can of
120 : : course inspect it with readelf. I figured 32-bit is sufficient,
121 : : for a single module. I detect running out of section numbers, but
122 : : do not implement the ELF overflow mechanism. At least you'll get
123 : : an error if that happens.
124 : :
125 : : We do not separate declarations and definitions. My guess is that
126 : : if you refer to the declaration, you'll also need the definition
127 : : (template body, inline function, class definition etc). But this
128 : : does mean we can get larger SCCs than if we separated them. It is
129 : : unclear whether this is a win or not.
130 : :
131 : : Notice that we embed section indices into the contents of other
132 : : sections. Thus random manipulation of the CMI file by ELF tools
133 : : may well break it. The kosher way would probably be to introduce
134 : : indirection via section symbols, but that would require defining a
135 : : relocation type.
136 : :
137 : : Notice that lazy loading of one module's decls can cause lazy
138 : : loading of other decls in the same or another module. Clearly we
139 : : want to avoid loops. In a correct program there can be no loops in
140 : : the module dependency graph, and the above-mentioned SCC algorithm
141 : : places all intra-module circular dependencies in the same SCC. It
142 : : also orders the SCCs wrt each other, so dependent SCCs come first.
143 : : As we load dependent modules first, we know there can be no
144 : : reference to a higher-numbered module, and because we write out
145 : : dependent SCCs first, likewise for SCCs within the module. This
146 : : allows us to immediately detect broken references. When loading,
147 : : we must ensure the rest of the compiler doesn't cause some
148 : : unconnected load to occur (for instance, instantiate a template).
149 : :
150 : : Classes used:
151 : :
152 : : dumper - logger
153 : :
154 : : data - buffer
155 : :
156 : : bytes_in : data - scalar reader
157 : : bytes_out : data - scalar writer
158 : :
159 : : bytes_in::bits_in - bit stream reader
160 : : bytes_out::bits_out - bit stream writer
161 : :
162 : : elf - ELROND format
163 : : elf_in : elf - ELROND reader
164 : : elf_out : elf - ELROND writer
165 : :
166 : : trees_in : bytes_in - tree reader
167 : : trees_out : bytes_out - tree writer
168 : :
169 : : depset - dependency set
170 : : depset::hash - hash table of depsets
171 : : depset::tarjan - SCC determinator
172 : :
173 : : uidset<T> - set T's related to a UID
174 : : uidset<T>::hash hash table of uidset<T>
175 : :
176 : : loc_spans - location map data
177 : :
178 : : module_state - module object
179 : :
180 : : slurping - data needed during loading
181 : :
182 : : macro_import - imported macro data
183 : : macro_export - exported macro data
184 : :
185 : : The ELROND objects use mmap, for both reading and writing. If mmap
186 : : is unavailable, fileno IO is used to read and write blocks of data.
187 : :
188 : : The mapper object uses fileno IO to communicate with the server or
189 : : program. */
190 : :
191 : : /* In expermental (trunk) sources, MODULE_VERSION is a #define passed
192 : : in from the Makefile. It records the modification date of the
193 : : source directory -- that's the only way to stay sane. In release
194 : : sources, we (plan to) use the compiler's major.minor versioning.
195 : : While the format might not change between at minor versions, it
196 : : seems simplest to tie the two together. There's no concept of
197 : : inter-version compatibility. */
198 : : #define IS_EXPERIMENTAL(V) ((V) >= (1U << 20))
199 : : #define MODULE_MAJOR(V) ((V) / 10000)
200 : : #define MODULE_MINOR(V) ((V) % 10000)
201 : : #define EXPERIMENT(A,B) (IS_EXPERIMENTAL (MODULE_VERSION) ? (A) : (B))
202 : : #ifndef MODULE_VERSION
203 : : #include "bversion.h"
204 : : #define MODULE_VERSION (BUILDING_GCC_MAJOR * 10000U + BUILDING_GCC_MINOR)
205 : : #elif !IS_EXPERIMENTAL (MODULE_VERSION)
206 : : #error "This is not the version I was looking for."
207 : : #endif
208 : :
209 : : #define _DEFAULT_SOURCE 1 /* To get TZ field of struct tm, if available. */
210 : : #include "config.h"
211 : : #define INCLUDE_STRING
212 : : #define INCLUDE_VECTOR
213 : : #include "system.h"
214 : : #include "coretypes.h"
215 : : #include "cp-tree.h"
216 : : #include "timevar.h"
217 : : #include "stringpool.h"
218 : : #include "dumpfile.h"
219 : : #include "bitmap.h"
220 : : #include "cgraph.h"
221 : : #include "varasm.h"
222 : : #include "tree-iterator.h"
223 : : #include "cpplib.h"
224 : : #include "mkdeps.h"
225 : : #include "incpath.h"
226 : : #include "libiberty.h"
227 : : #include "stor-layout.h"
228 : : #include "version.h"
229 : : #include "tree-diagnostic.h"
230 : : #include "toplev.h"
231 : : #include "opts.h"
232 : : #include "attribs.h"
233 : : #include "intl.h"
234 : : #include "langhooks.h"
235 : : #include "contracts.h"
236 : : /* This TU doesn't need or want to see the networking. */
237 : : #define CODY_NETWORKING 0
238 : : #include "mapper-client.h"
239 : : #include <zlib.h> // for crc32, crc32_combine
240 : :
241 : : #if 0 // 1 for testing no mmap
242 : : #define MAPPED_READING 0
243 : : #define MAPPED_WRITING 0
244 : : #else
245 : : #if HAVE_MMAP_FILE && HAVE_MUNMAP && HAVE_MSYNC
246 : : /* mmap, munmap, msync. */
247 : : #define MAPPED_READING 1
248 : : #if HAVE_SYSCONF && defined (_SC_PAGE_SIZE)
249 : : /* sysconf (_SC_PAGE_SIZE), ftruncate */
250 : : /* posix_fallocate used if available. */
251 : : #define MAPPED_WRITING 1
252 : : #else
253 : : #define MAPPED_WRITING 0
254 : : #endif
255 : : #else
256 : : #define MAPPED_READING 0
257 : : #define MAPPED_WRITING 0
258 : : #endif
259 : : #endif
260 : :
261 : : /* Some open(2) flag differences, what a colourful world it is! */
262 : : #if defined (O_CLOEXEC)
263 : : // OK
264 : : #elif defined (_O_NOINHERIT)
265 : : /* Windows' _O_NOINHERIT matches O_CLOEXEC flag */
266 : : #define O_CLOEXEC _O_NOINHERIT
267 : : #else
268 : : #define O_CLOEXEC 0
269 : : #endif
270 : : #if defined (O_BINARY)
271 : : // Ok?
272 : : #elif defined (_O_BINARY)
273 : : /* Windows' open(2) call defaults to text! */
274 : : #define O_BINARY _O_BINARY
275 : : #else
276 : : #define O_BINARY 0
277 : : #endif
278 : :
279 : 225720 : static inline cpp_hashnode *cpp_node (tree id)
280 : : {
281 : 225720 : return CPP_HASHNODE (GCC_IDENT_TO_HT_IDENT (id));
282 : : }
283 : :
284 : 135172 : 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 : 135172 : #pragma GCC diagnostic push
291 : 135172 : #pragma GCC diagnostic ignored "-Warray-bounds"
292 : 135172 : return HT_IDENT_TO_GCC_IDENT (HT_NODE (const_cast<cpp_hashnode *> (node)));
293 : 135172 : #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 : 3854 : version2string (unsigned version, verstr_t &out)
310 : : {
311 : 3854 : unsigned major = MODULE_MAJOR (version);
312 : 3854 : unsigned minor = MODULE_MINOR (version);
313 : :
314 : 3854 : if (IS_EXPERIMENTAL (version))
315 : 3854 : sprintf (out, "%04u/%02u/%02u-%02u:%02u%s",
316 : 3854 : 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 : 3854 : }
322 : :
323 : : /* Include files to note translation for. */
324 : : static vec<const char *, va_heap, vl_embed> *note_includes;
325 : :
326 : : /* Modules to note CMI pathames. */
327 : : static vec<const char *, va_heap, vl_embed> *note_cmis;
328 : :
329 : : /* Traits to hash an arbitrary pointer. Entries are not deletable,
330 : : and removal is a noop (removal needed upon destruction). */
331 : : template <typename T>
332 : : struct nodel_ptr_hash : pointer_hash<T>, typed_noop_remove <T *> {
333 : : /* Nothing is deletable. Everything is insertable. */
334 : : static bool is_deleted (T *) { return false; }
335 : : static void mark_deleted (T *) { gcc_unreachable (); }
336 : : };
337 : :
338 : : /* Map from pointer to signed integer. */
339 : : typedef simple_hashmap_traits<nodel_ptr_hash<void>, int> ptr_int_traits;
340 : : typedef hash_map<void *,signed,ptr_int_traits> ptr_int_hash_map;
341 : :
342 : : /********************************************************************/
343 : : /* Basic streaming & ELF. Serialization is usually via mmap. For
344 : : writing we slide a buffer over the output file, syncing it
345 : : approproiately. For reading we simply map the whole file (as a
346 : : file-backed read-only map -- it's just address space, leaving the
347 : : OS pager to deal with getting the data to us). Some buffers need
348 : : to be more conventional malloc'd contents. */
349 : :
350 : : /* Variable length buffer. */
351 : :
352 : : namespace {
353 : :
354 : : constexpr line_map_uint_t loc_one = 1;
355 : :
356 : : class data {
357 : : public:
358 : 2682 : class allocator {
359 : : public:
360 : : /* Tools tend to moan if the dtor's not virtual. */
361 : 99401 : virtual ~allocator () {}
362 : :
363 : : public:
364 : : void grow (data &obj, unsigned needed, bool exact);
365 : : void shrink (data &obj);
366 : :
367 : : public:
368 : : virtual char *grow (char *ptr, unsigned needed);
369 : : virtual void shrink (char *ptr);
370 : : };
371 : :
372 : : public:
373 : : char *buffer; /* Buffer being transferred. */
374 : : /* Although size_t would be the usual size, we know we never get
375 : : more than 4GB of buffer -- because that's the limit of the
376 : : encapsulation format. And if you need bigger imports, you're
377 : : doing it wrong. */
378 : : unsigned size; /* Allocated size of buffer. */
379 : : unsigned pos; /* Position in buffer. */
380 : :
381 : : public:
382 : 671639 : data ()
383 : 671639 : :buffer (NULL), size (0), pos (0)
384 : : {
385 : : }
386 : 685029 : ~data ()
387 : : {
388 : : /* Make sure the derived and/or using class know what they're
389 : : doing. */
390 : 685029 : gcc_checking_assert (!buffer);
391 : 685029 : }
392 : :
393 : : protected:
394 : 433741858 : char *use (unsigned count)
395 : : {
396 : 433741858 : if (size < pos + count)
397 : : return NULL;
398 : 433741858 : char *res = &buffer[pos];
399 : 433741858 : pos += count;
400 : 232866192 : return res;
401 : : }
402 : :
403 : : unsigned calc_crc (unsigned) const;
404 : :
405 : : public:
406 : 32885778 : void unuse (unsigned count)
407 : : {
408 : 32885778 : pos -= count;
409 : 26027 : }
410 : :
411 : : public:
412 : : static allocator simple_memory;
413 : : };
414 : : } // anon namespace
415 : :
416 : : /* The simple data allocator. */
417 : : data::allocator data::simple_memory;
418 : :
419 : : /* Grow buffer to at least size NEEDED. */
420 : :
421 : : void
422 : 585670 : data::allocator::grow (data &obj, unsigned needed, bool exact)
423 : : {
424 : 585670 : gcc_checking_assert (needed ? needed > obj.size : !obj.size);
425 : 585670 : if (!needed)
426 : : /* Pick a default size. */
427 : 250820 : needed = EXPERIMENT (100, 1000);
428 : :
429 : 585670 : if (!exact)
430 : 577939 : needed *= 2;
431 : 585670 : obj.buffer = grow (obj.buffer, needed);
432 : 585670 : if (obj.buffer)
433 : 585670 : obj.size = needed;
434 : : else
435 : 0 : obj.pos = obj.size = 0;
436 : 585670 : }
437 : :
438 : : /* Free a buffer. */
439 : :
440 : : void
441 : 264242 : data::allocator::shrink (data &obj)
442 : : {
443 : 0 : shrink (obj.buffer);
444 : 264242 : obj.buffer = NULL;
445 : 264242 : obj.size = 0;
446 : 0 : }
447 : :
448 : : char *
449 : 10794 : data::allocator::grow (char *ptr, unsigned needed)
450 : : {
451 : 10794 : return XRESIZEVAR (char, ptr, needed);
452 : : }
453 : :
454 : : void
455 : 13410 : data::allocator::shrink (char *ptr)
456 : : {
457 : 13410 : XDELETEVEC (ptr);
458 : 13410 : }
459 : :
460 : : /* Calculate the crc32 of the buffer. Note the CRC is stored in the
461 : : first 4 bytes, so don't include them. */
462 : :
463 : : unsigned
464 : 428604 : data::calc_crc (unsigned l) const
465 : : {
466 : 428604 : return crc32 (0, (unsigned char *)buffer + 4, l - 4);
467 : : }
468 : :
469 : : class elf_in;
470 : :
471 : : /* Byte stream reader. */
472 : :
473 : : namespace {
474 : : class bytes_in : public data {
475 : : typedef data parent;
476 : :
477 : : protected:
478 : : bool overrun; /* Sticky read-too-much flag. */
479 : :
480 : : public:
481 : 187196 : bytes_in ()
482 : 187196 : : parent (), overrun (false)
483 : : {
484 : : }
485 : 189845 : ~bytes_in ()
486 : : {
487 : 14750 : }
488 : :
489 : : public:
490 : : /* Begin reading a named section. */
491 : : bool begin (location_t loc, elf_in *src, const char *name);
492 : : /* Begin reading a numbered section with optional name. */
493 : : bool begin (location_t loc, elf_in *src, unsigned, const char * = NULL);
494 : : /* Complete reading a buffer. Propagate errors and return true on
495 : : success. */
496 : : bool end (elf_in *src);
497 : : /* Return true if there is unread data. */
498 : 1316889 : bool more_p () const
499 : : {
500 : 1316889 : return pos != size;
501 : : }
502 : :
503 : : public:
504 : : /* Start reading at OFFSET. */
505 : 327 : void random_access (unsigned offset)
506 : : {
507 : 327 : if (offset > size)
508 : 0 : set_overrun ();
509 : 327 : pos = offset;
510 : : }
511 : :
512 : : public:
513 : 1218666 : void align (unsigned boundary)
514 : : {
515 : 1218666 : if (unsigned pad = pos & (boundary - 1))
516 : 2360780 : read (boundary - pad);
517 : : }
518 : :
519 : : public:
520 : 200875666 : const char *read (unsigned count)
521 : : {
522 : 1142114 : char *ptr = use (count);
523 : 200875666 : if (!ptr)
524 : 0 : set_overrun ();
525 : 163640482 : return ptr;
526 : : }
527 : :
528 : : public:
529 : : bool check_crc () const;
530 : : /* We store the CRC in the first 4 bytes, using host endianness. */
531 : 188304 : unsigned get_crc () const
532 : : {
533 : 188304 : return *(const unsigned *)&buffer[0];
534 : : }
535 : :
536 : : public:
537 : : /* Manipulate the overrun flag. */
538 : 117412820 : bool get_overrun () const
539 : : {
540 : 117412820 : return overrun;
541 : : }
542 : 26 : void set_overrun ()
543 : : {
544 : 26 : overrun = true;
545 : 0 : }
546 : :
547 : : public:
548 : : unsigned u32 (); /* Read uncompressed integer. */
549 : :
550 : : public:
551 : : int c () ATTRIBUTE_UNUSED; /* Read a char. */
552 : : int i (); /* Read a signed int. */
553 : : unsigned u (); /* Read an unsigned int. */
554 : : size_t z (); /* Read a size_t. */
555 : : location_t loc (); /* Read a location_t. */
556 : : HOST_WIDE_INT wi (); /* Read a HOST_WIDE_INT. */
557 : : unsigned HOST_WIDE_INT wu (); /* Read an unsigned HOST_WIDE_INT. */
558 : : const char *str (size_t * = NULL); /* Read a string. */
559 : : const void *buf (size_t); /* Read a fixed-length buffer. */
560 : : cpp_hashnode *cpp_node (); /* Read a cpp node. */
561 : :
562 : : struct bits_in;
563 : : bits_in stream_bits ();
564 : : };
565 : : } // anon namespace
566 : :
567 : : /* Verify the buffer's CRC is correct. */
568 : :
569 : : bool
570 : 185474 : bytes_in::check_crc () const
571 : : {
572 : 185474 : if (size < 4)
573 : : return false;
574 : :
575 : 185474 : unsigned c_crc = calc_crc (size);
576 : 185474 : if (c_crc != get_crc ())
577 : : return false;
578 : :
579 : : return true;
580 : : }
581 : :
582 : : class elf_out;
583 : :
584 : : /* Byte stream writer. */
585 : :
586 : : namespace {
587 : : class bytes_out : public data {
588 : : typedef data parent;
589 : :
590 : : public:
591 : : allocator *memory; /* Obtainer of memory. */
592 : :
593 : : public:
594 : 478954 : bytes_out (allocator *memory)
595 : 478954 : : parent (), memory (memory)
596 : : {
597 : : }
598 : 478954 : ~bytes_out ()
599 : : {
600 : 506736 : }
601 : :
602 : : public:
603 : 559408827 : bool streaming_p () const
604 : : {
605 : 559408827 : return memory != NULL;
606 : : }
607 : :
608 : : public:
609 : : void set_crc (unsigned *crc_ptr);
610 : :
611 : : public:
612 : : /* Begin writing, maybe reserve space for CRC. */
613 : : void begin (bool need_crc = true);
614 : : /* Finish writing. Spill to section by number. */
615 : : unsigned end (elf_out *, unsigned, unsigned *crc_ptr = NULL);
616 : :
617 : : public:
618 : 1540773 : void align (unsigned boundary)
619 : : {
620 : 1540773 : if (unsigned pad = pos & (boundary - 1))
621 : 1443891 : write (boundary - pad);
622 : 1540773 : }
623 : :
624 : : public:
625 : 232866192 : char *write (unsigned count, bool exact = false)
626 : : {
627 : 232866192 : if (size < pos + count)
628 : 321471 : memory->grow (*this, pos + count, exact);
629 : 232866192 : return use (count);
630 : : }
631 : :
632 : : public:
633 : : void u32 (unsigned); /* Write uncompressed integer. */
634 : :
635 : : public:
636 : : void c (unsigned char) ATTRIBUTE_UNUSED; /* Write unsigned char. */
637 : : void i (int); /* Write signed int. */
638 : : void u (unsigned); /* Write unsigned int. */
639 : : void z (size_t s); /* Write size_t. */
640 : : void loc (location_t); /* Write location_t. */
641 : : void wi (HOST_WIDE_INT); /* Write HOST_WIDE_INT. */
642 : : void wu (unsigned HOST_WIDE_INT); /* Write unsigned HOST_WIDE_INT. */
643 : 15831 : void str (const char *ptr)
644 : : {
645 : 15831 : str (ptr, strlen (ptr));
646 : 15831 : }
647 : 244971 : void cpp_node (const cpp_hashnode *node)
648 : : {
649 : 244971 : str ((const char *)NODE_NAME (node), NODE_LEN (node));
650 : 11999 : }
651 : : void str (const char *, size_t); /* Write string of known length. */
652 : : void buf (const void *, size_t); /* Write fixed length buffer. */
653 : : void *buf (size_t); /* Create a writable buffer */
654 : :
655 : : struct bits_out;
656 : : bits_out stream_bits ();
657 : :
658 : : public:
659 : : /* Format a NUL-terminated raw string. */
660 : : void printf (const char *, ...) ATTRIBUTE_PRINTF_2;
661 : : void print_time (const char *, const tm *, const char *);
662 : :
663 : : public:
664 : : /* Dump instrumentation. */
665 : : static void instrument ();
666 : :
667 : : protected:
668 : : /* Instrumentation. */
669 : : static unsigned spans[4];
670 : : static unsigned lengths[4];
671 : : };
672 : : } // anon namespace
673 : :
674 : : /* Finish bit packet. Rewind the bytes not used. */
675 : :
676 : : static unsigned
677 : 32834142 : bit_flush (data& bits, uint32_t& bit_val, unsigned& bit_pos)
678 : : {
679 : 32834142 : gcc_assert (bit_pos);
680 : 32834142 : unsigned bytes = (bit_pos + 7) / 8;
681 : 32834142 : bits.unuse (4 - bytes);
682 : 32834142 : bit_pos = 0;
683 : 32834142 : bit_val = 0;
684 : 32834142 : return bytes;
685 : : }
686 : :
687 : : /* Bit stream reader (RAII-enabled). Bools are packed into bytes. You
688 : : cannot mix bools and non-bools. Use bflush to flush the current stream
689 : : of bools on demand. Upon destruction bflush is called.
690 : :
691 : : When reading, we don't know how many bools we'll read in. So read
692 : : 4 bytes-worth, and then rewind when flushing if we didn't need them
693 : : all. You can't have a block of bools closer than 4 bytes to the
694 : : end of the buffer.
695 : :
696 : : Both bits_in and bits_out maintain the necessary state for bit packing,
697 : : and since these objects are locally constructed the compiler can more
698 : : easily track their state across consecutive reads/writes and optimize
699 : : away redundant buffering checks. */
700 : :
701 : : struct bytes_in::bits_in {
702 : : bytes_in& in;
703 : : uint32_t bit_val = 0;
704 : : unsigned bit_pos = 0;
705 : :
706 : 11243504 : bits_in (bytes_in& in)
707 : 11243504 : : in (in)
708 : : { }
709 : :
710 : 11243504 : ~bits_in ()
711 : : {
712 : 10519387 : bflush ();
713 : 11243504 : }
714 : :
715 : : bits_in(bits_in&&) = default;
716 : : bits_in(const bits_in&) = delete;
717 : : bits_in& operator=(const bits_in&) = delete;
718 : :
719 : : /* Completed a block of bools. */
720 : 23348541 : void bflush ()
721 : : {
722 : 23348541 : if (bit_pos)
723 : 12829154 : bit_flush (in, bit_val, bit_pos);
724 : 23348541 : }
725 : :
726 : : /* Read one bit. */
727 : 384351471 : bool b ()
728 : : {
729 : 384351471 : if (!bit_pos)
730 : 16741386 : bit_val = in.u32 ();
731 : 384351471 : bool x = (bit_val >> bit_pos) & 1;
732 : 384351471 : bit_pos = (bit_pos + 1) % 32;
733 : 384351471 : return x;
734 : : }
735 : : };
736 : :
737 : : /* Factory function for bits_in. */
738 : :
739 : : bytes_in::bits_in
740 : 11243504 : bytes_in::stream_bits ()
741 : : {
742 : 11243504 : return bits_in (*this);
743 : : }
744 : :
745 : : /* Bit stream writer (RAII-enabled), counterpart to bits_in. */
746 : :
747 : : struct bytes_out::bits_out {
748 : : bytes_out& out;
749 : : uint32_t bit_val = 0;
750 : : unsigned bit_pos = 0;
751 : : char is_set = -1;
752 : :
753 : 13329535 : bits_out (bytes_out& out)
754 : 13329535 : : out (out)
755 : : { }
756 : :
757 : 13329535 : ~bits_out ()
758 : : {
759 : 67543 : bflush ();
760 : : }
761 : :
762 : : bits_out(bits_out&&) = default;
763 : : bits_out(const bits_out&) = delete;
764 : : bits_out& operator=(const bits_out&) = delete;
765 : :
766 : : /* Completed a block of bools. */
767 : 27678249 : void bflush ()
768 : : {
769 : 27678249 : if (bit_pos)
770 : : {
771 : 15249520 : out.u32 (bit_val);
772 : 15249520 : out.lengths[2] += bit_flush (out, bit_val, bit_pos);
773 : : }
774 : 27678249 : out.spans[2]++;
775 : 27678249 : is_set = -1;
776 : 27678249 : }
777 : :
778 : : /* Write one bit.
779 : :
780 : : It may be worth optimizing for most bools being zero. Some kind of
781 : : run-length encoding? */
782 : 455639713 : void b (bool x)
783 : : {
784 : 455639713 : if (is_set != x)
785 : : {
786 : 48984656 : is_set = x;
787 : 48984656 : out.spans[x]++;
788 : : }
789 : 455639713 : out.lengths[x]++;
790 : 455639713 : bit_val |= unsigned (x) << bit_pos++;
791 : 455639713 : if (bit_pos == 32)
792 : : {
793 : 4755468 : out.u32 (bit_val);
794 : 4755468 : out.lengths[2] += bit_flush (out, bit_val, bit_pos);
795 : : }
796 : 455639713 : }
797 : : };
798 : :
799 : : /* Factory function for bits_out. */
800 : :
801 : : bytes_out::bits_out
802 : 13329535 : bytes_out::stream_bits ()
803 : : {
804 : 13329535 : return bits_out (*this);
805 : : }
806 : :
807 : : /* Instrumentation. */
808 : : unsigned bytes_out::spans[4];
809 : : unsigned bytes_out::lengths[4];
810 : :
811 : : /* If CRC_PTR non-null, set the CRC of the buffer. Mix the CRC into
812 : : that pointed to by CRC_PTR. */
813 : :
814 : : void
815 : 245674 : bytes_out::set_crc (unsigned *crc_ptr)
816 : : {
817 : 245674 : if (crc_ptr)
818 : : {
819 : 243130 : gcc_checking_assert (pos >= 4);
820 : :
821 : 243130 : unsigned crc = calc_crc (pos);
822 : 243130 : unsigned accum = *crc_ptr;
823 : : /* Only mix the existing *CRC_PTR if it is non-zero. */
824 : 243130 : accum = accum ? crc32_combine (accum, crc, pos - 4) : crc;
825 : 243130 : *crc_ptr = accum;
826 : :
827 : : /* Buffer will be sufficiently aligned. */
828 : 243130 : *(unsigned *)buffer = crc;
829 : : }
830 : 245674 : }
831 : :
832 : : /* Exactly 4 bytes. Used internally for bool packing and a few other
833 : : places. We can't simply use uint32_t because (a) alignment and
834 : : (b) we need little-endian for the bool streaming rewinding to make
835 : : sense. */
836 : :
837 : : void
838 : 20013215 : bytes_out::u32 (unsigned val)
839 : : {
840 : 20013215 : if (char *ptr = write (4))
841 : : {
842 : 20013215 : ptr[0] = val;
843 : 20013215 : ptr[1] = val >> 8;
844 : 20013215 : ptr[2] = val >> 16;
845 : 20013215 : ptr[3] = val >> 24;
846 : : }
847 : 20013215 : }
848 : :
849 : : unsigned
850 : 16750257 : bytes_in::u32 ()
851 : : {
852 : 16750257 : unsigned val = 0;
853 : 16750257 : if (const char *ptr = read (4))
854 : : {
855 : 16750257 : val |= (unsigned char)ptr[0];
856 : 16750257 : val |= (unsigned char)ptr[1] << 8;
857 : 16750257 : val |= (unsigned char)ptr[2] << 16;
858 : 16750257 : val |= (unsigned char)ptr[3] << 24;
859 : : }
860 : :
861 : 16750257 : return val;
862 : : }
863 : :
864 : : /* Chars are unsigned and written as single bytes. */
865 : :
866 : : void
867 : 0 : bytes_out::c (unsigned char v)
868 : : {
869 : 0 : if (char *ptr = write (1))
870 : 0 : *ptr = v;
871 : 0 : }
872 : :
873 : : int
874 : 0 : bytes_in::c ()
875 : : {
876 : 0 : int v = 0;
877 : 0 : if (const char *ptr = read (1))
878 : 0 : v = (unsigned char)ptr[0];
879 : 0 : return v;
880 : : }
881 : :
882 : : /* Ints 7-bit as a byte. Otherwise a 3bit count of following bytes in
883 : : big-endian form. 4 bits are in the first byte. */
884 : :
885 : : void
886 : 82222780 : bytes_out::i (int v)
887 : : {
888 : 82222780 : if (char *ptr = write (1))
889 : : {
890 : 82222780 : if (v <= 0x3f && v >= -0x40)
891 : 65018221 : *ptr = v & 0x7f;
892 : : else
893 : : {
894 : 17204559 : unsigned bytes = 0;
895 : 17204559 : int probe;
896 : 17204559 : if (v >= 0)
897 : 0 : for (probe = v >> 8; probe > 0x7; probe >>= 8)
898 : 0 : bytes++;
899 : : else
900 : 26698744 : for (probe = v >> 8; probe < -0x8; probe >>= 8)
901 : 9494185 : bytes++;
902 : 17204559 : *ptr = 0x80 | bytes << 4 | (probe & 0xf);
903 : 17204559 : if ((ptr = write (++bytes)))
904 : 43903303 : for (; bytes--; v >>= 8)
905 : 26698744 : ptr[bytes] = v & 0xff;
906 : : }
907 : : }
908 : 82222780 : }
909 : :
910 : : int
911 : 70111606 : bytes_in::i ()
912 : : {
913 : 70111606 : int v = 0;
914 : 70111606 : if (const char *ptr = read (1))
915 : : {
916 : 70111606 : v = *ptr & 0xff;
917 : 70111606 : if (v & 0x80)
918 : : {
919 : 15397439 : unsigned bytes = (v >> 4) & 0x7;
920 : 15397439 : v &= 0xf;
921 : 15397439 : if (v & 0x8)
922 : 15397439 : v |= -1 ^ 0x7;
923 : : /* unsigned necessary due to left shifts of -ve values. */
924 : 15397439 : unsigned uv = unsigned (v);
925 : 15397439 : if ((ptr = read (++bytes)))
926 : 39999750 : while (bytes--)
927 : 24602311 : uv = (uv << 8) | (*ptr++ & 0xff);
928 : 15397439 : v = int (uv);
929 : : }
930 : 54714167 : else if (v & 0x40)
931 : 7403419 : v |= -1 ^ 0x3f;
932 : : }
933 : :
934 : 70111606 : return v;
935 : : }
936 : :
937 : : void
938 : 70792328 : bytes_out::u (unsigned v)
939 : : {
940 : 70792328 : if (char *ptr = write (1))
941 : : {
942 : 70792328 : if (v <= 0x7f)
943 : 61720578 : *ptr = v;
944 : : else
945 : : {
946 : 9071750 : unsigned bytes = 0;
947 : 9071750 : unsigned probe;
948 : 10645208 : for (probe = v >> 8; probe > 0xf; probe >>= 8)
949 : 1573458 : bytes++;
950 : 9071750 : *ptr = 0x80 | bytes << 4 | probe;
951 : 9071750 : if ((ptr = write (++bytes)))
952 : 19716958 : for (; bytes--; v >>= 8)
953 : 10645208 : ptr[bytes] = v & 0xff;
954 : : }
955 : : }
956 : 70792328 : }
957 : :
958 : : unsigned
959 : 61488200 : bytes_in::u ()
960 : : {
961 : 61488200 : unsigned v = 0;
962 : :
963 : 61488200 : if (const char *ptr = read (1))
964 : : {
965 : 61488200 : v = *ptr & 0xff;
966 : 61488200 : if (v & 0x80)
967 : : {
968 : 8094790 : unsigned bytes = (v >> 4) & 0x7;
969 : 8094790 : v &= 0xf;
970 : 8094790 : if ((ptr = read (++bytes)))
971 : 17698082 : while (bytes--)
972 : 9603292 : v = (v << 8) | (*ptr++ & 0xff);
973 : : }
974 : : }
975 : :
976 : 61488200 : return v;
977 : : }
978 : :
979 : : void
980 : 16956872 : bytes_out::wi (HOST_WIDE_INT v)
981 : : {
982 : 16956872 : if (char *ptr = write (1))
983 : : {
984 : 16956872 : if (v <= 0x3f && v >= -0x40)
985 : 3388484 : *ptr = v & 0x7f;
986 : : else
987 : : {
988 : 13568388 : unsigned bytes = 0;
989 : 13568388 : HOST_WIDE_INT probe;
990 : 13568388 : if (v >= 0)
991 : 48619542 : for (probe = v >> 8; probe > 0x7; probe >>= 8)
992 : 35053572 : bytes++;
993 : : else
994 : 7931 : for (probe = v >> 8; probe < -0x8; probe >>= 8)
995 : 5513 : bytes++;
996 : 13568388 : *ptr = 0x80 | bytes << 4 | (probe & 0xf);
997 : 13568388 : if ((ptr = write (++bytes)))
998 : 62195861 : for (; bytes--; v >>= 8)
999 : 48627473 : ptr[bytes] = v & 0xff;
1000 : : }
1001 : : }
1002 : 16956872 : }
1003 : :
1004 : : HOST_WIDE_INT
1005 : 14071753 : bytes_in::wi ()
1006 : : {
1007 : 14071753 : HOST_WIDE_INT v = 0;
1008 : 14071753 : if (const char *ptr = read (1))
1009 : : {
1010 : 14071753 : v = *ptr & 0xff;
1011 : 14071753 : if (v & 0x80)
1012 : : {
1013 : 12600841 : unsigned bytes = (v >> 4) & 0x7;
1014 : 12600841 : v &= 0xf;
1015 : 12600841 : if (v & 0x8)
1016 : 1634 : v |= -1 ^ 0x7;
1017 : : /* unsigned necessary due to left shifts of -ve values. */
1018 : 12600841 : unsigned HOST_WIDE_INT uv = (unsigned HOST_WIDE_INT) v;
1019 : 12600841 : if ((ptr = read (++bytes)))
1020 : 57556763 : while (bytes--)
1021 : 44955922 : uv = (uv << 8) | (*ptr++ & 0xff);
1022 : 12600841 : v = (HOST_WIDE_INT) uv;
1023 : : }
1024 : 1470912 : else if (v & 0x40)
1025 : 6801 : v |= -1 ^ 0x3f;
1026 : : }
1027 : :
1028 : 14071753 : return v;
1029 : : }
1030 : :
1031 : : /* unsigned wide ints are just written as signed wide ints. */
1032 : :
1033 : : inline void
1034 : 16956340 : bytes_out::wu (unsigned HOST_WIDE_INT v)
1035 : : {
1036 : 16956340 : wi ((HOST_WIDE_INT) v);
1037 : : }
1038 : :
1039 : : inline unsigned HOST_WIDE_INT
1040 : 14071327 : bytes_in::wu ()
1041 : : {
1042 : 27668172 : return (unsigned HOST_WIDE_INT) wi ();
1043 : : }
1044 : :
1045 : : /* size_t written as unsigned or unsigned wide int. */
1046 : :
1047 : : inline void
1048 : 1512029 : bytes_out::z (size_t s)
1049 : : {
1050 : 1512029 : if (sizeof (s) == sizeof (unsigned))
1051 : : u (s);
1052 : : else
1053 : 2993871 : wu (s);
1054 : 12 : }
1055 : :
1056 : : inline size_t
1057 : 1204764 : bytes_in::z ()
1058 : : {
1059 : 1204764 : if (sizeof (size_t) == sizeof (unsigned))
1060 : : return u ();
1061 : : else
1062 : 2409528 : return wu ();
1063 : : }
1064 : :
1065 : : /* location_t written as 32- or 64-bit as needed. */
1066 : :
1067 : 14879733 : inline void bytes_out::loc (location_t l)
1068 : : {
1069 : 14879733 : if (sizeof (location_t) > sizeof (unsigned))
1070 : 28215397 : wu (l);
1071 : : else
1072 : : u (l);
1073 : 1541525 : }
1074 : :
1075 : 12394868 : inline location_t bytes_in::loc ()
1076 : : {
1077 : 12394868 : if (sizeof (location_t) > sizeof (unsigned))
1078 : 24786919 : return wu ();
1079 : : else
1080 : : return u ();
1081 : : }
1082 : :
1083 : : /* Buffer simply memcpied. */
1084 : : void *
1085 : 1540773 : bytes_out::buf (size_t len)
1086 : : {
1087 : 1540773 : align (sizeof (void *) * 2);
1088 : 1540773 : return write (len);
1089 : : }
1090 : :
1091 : : void
1092 : 1496252 : bytes_out::buf (const void *src, size_t len)
1093 : : {
1094 : 1496252 : if (void *ptr = buf (len))
1095 : 1496252 : memcpy (ptr, src, len);
1096 : 1496252 : }
1097 : :
1098 : : const void *
1099 : 1218666 : bytes_in::buf (size_t len)
1100 : : {
1101 : 1218666 : align (sizeof (void *) * 2);
1102 : 1218666 : const char *ptr = read (len);
1103 : :
1104 : 1218666 : return ptr;
1105 : : }
1106 : :
1107 : : /* strings as an size_t length, followed by the buffer. Make sure
1108 : : there's a NUL terminator on read. */
1109 : :
1110 : : void
1111 : 1512011 : bytes_out::str (const char *string, size_t len)
1112 : : {
1113 : 1481836 : z (len);
1114 : 1481836 : if (len)
1115 : : {
1116 : 1481836 : gcc_checking_assert (!string[len]);
1117 : 1481836 : buf (string, len + 1);
1118 : : }
1119 : 30175 : }
1120 : :
1121 : : const char *
1122 : 1204758 : bytes_in::str (size_t *len_p)
1123 : : {
1124 : 1204758 : size_t len = z ();
1125 : :
1126 : : /* We're about to trust some user data. */
1127 : 1204758 : if (overrun)
1128 : 0 : len = 0;
1129 : 1204758 : if (len_p)
1130 : 1201138 : *len_p = len;
1131 : 1204758 : const char *str = NULL;
1132 : 1204758 : if (len)
1133 : : {
1134 : 1204549 : str = reinterpret_cast<const char *> (buf (len + 1));
1135 : 1204549 : if (!str || str[len])
1136 : : {
1137 : 0 : set_overrun ();
1138 : 0 : str = NULL;
1139 : : }
1140 : : }
1141 : 0 : return str ? str : "";
1142 : : }
1143 : :
1144 : : cpp_hashnode *
1145 : 225929 : bytes_in::cpp_node ()
1146 : : {
1147 : 225929 : size_t len;
1148 : 225929 : const char *s = str (&len);
1149 : 225929 : if (!len)
1150 : : return NULL;
1151 : 225720 : return ::cpp_node (get_identifier_with_length (s, len));
1152 : : }
1153 : :
1154 : : /* Format a string directly to the buffer, including a terminating
1155 : : NUL. Intended for human consumption. */
1156 : :
1157 : : void
1158 : 26027 : bytes_out::printf (const char *format, ...)
1159 : : {
1160 : 26027 : va_list args;
1161 : : /* Exercise buffer expansion. */
1162 : 26027 : size_t len = EXPERIMENT (10, 500);
1163 : :
1164 : 51636 : while (char *ptr = write (len))
1165 : : {
1166 : 51636 : va_start (args, format);
1167 : 51636 : size_t actual = vsnprintf (ptr, len, format, args) + 1;
1168 : 51636 : va_end (args);
1169 : 51636 : if (actual <= len)
1170 : : {
1171 : 26027 : unuse (len - actual);
1172 : 26027 : break;
1173 : : }
1174 : 25609 : unuse (len);
1175 : 25609 : len = actual;
1176 : 25609 : }
1177 : 26027 : }
1178 : :
1179 : : void
1180 : 5088 : bytes_out::print_time (const char *kind, const tm *time, const char *tz)
1181 : : {
1182 : 5088 : printf ("%stime: %4u/%02u/%02u %02u:%02u:%02u %s",
1183 : 5088 : kind, time->tm_year + 1900, time->tm_mon + 1, time->tm_mday,
1184 : 5088 : time->tm_hour, time->tm_min, time->tm_sec, tz);
1185 : 5088 : }
1186 : :
1187 : : /* Encapsulated Lazy Records Of Named Declarations.
1188 : : Header: Stunningly Elf32_Ehdr-like
1189 : : Sections: Sectional data
1190 : : [1-N) : User data sections
1191 : : N .strtab : strings, stunningly ELF STRTAB-like
1192 : : Index: Section table, stunningly ELF32_Shdr-like. */
1193 : :
1194 : : class elf {
1195 : : protected:
1196 : : /* Constants used within the format. */
1197 : : enum private_constants {
1198 : : /* File kind. */
1199 : : ET_NONE = 0,
1200 : : EM_NONE = 0,
1201 : : OSABI_NONE = 0,
1202 : :
1203 : : /* File format. */
1204 : : EV_CURRENT = 1,
1205 : : CLASS32 = 1,
1206 : : DATA2LSB = 1,
1207 : : DATA2MSB = 2,
1208 : :
1209 : : /* Section numbering. */
1210 : : SHN_UNDEF = 0,
1211 : : SHN_LORESERVE = 0xff00,
1212 : : SHN_XINDEX = 0xffff,
1213 : :
1214 : : /* Section types. */
1215 : : SHT_NONE = 0, /* No contents. */
1216 : : SHT_PROGBITS = 1, /* Random bytes. */
1217 : : SHT_STRTAB = 3, /* A string table. */
1218 : :
1219 : : /* Section flags. */
1220 : : SHF_NONE = 0x00, /* Nothing. */
1221 : : SHF_STRINGS = 0x20, /* NUL-Terminated strings. */
1222 : :
1223 : : /* I really hope we do not get CMI files larger than 4GB. */
1224 : : MY_CLASS = CLASS32,
1225 : : /* It is host endianness that is relevant. */
1226 : : MY_ENDIAN = DATA2LSB
1227 : : #ifdef WORDS_BIGENDIAN
1228 : : ^ DATA2LSB ^ DATA2MSB
1229 : : #endif
1230 : : };
1231 : :
1232 : : public:
1233 : : /* Constants visible to users. */
1234 : : enum public_constants {
1235 : : /* Special error codes. Breaking layering a bit. */
1236 : : E_BAD_DATA = -1, /* Random unexpected data errors. */
1237 : : E_BAD_LAZY = -2, /* Badly ordered laziness. */
1238 : : E_BAD_IMPORT = -3 /* A nested import failed. */
1239 : : };
1240 : :
1241 : : protected:
1242 : : /* File identification. On-disk representation. */
1243 : : struct ident {
1244 : : uint8_t magic[4]; /* 0x7f, 'E', 'L', 'F' */
1245 : : uint8_t klass; /* 4:CLASS32 */
1246 : : uint8_t data; /* 5:DATA2[LM]SB */
1247 : : uint8_t version; /* 6:EV_CURRENT */
1248 : : uint8_t osabi; /* 7:OSABI_NONE */
1249 : : uint8_t abiver; /* 8: 0 */
1250 : : uint8_t pad[7]; /* 9-15 */
1251 : : };
1252 : : /* File header. On-disk representation. */
1253 : : struct header {
1254 : : struct ident ident;
1255 : : uint16_t type; /* ET_NONE */
1256 : : uint16_t machine; /* EM_NONE */
1257 : : uint32_t version; /* EV_CURRENT */
1258 : : uint32_t entry; /* 0 */
1259 : : uint32_t phoff; /* 0 */
1260 : : uint32_t shoff; /* Section Header Offset in file */
1261 : : uint32_t flags;
1262 : : uint16_t ehsize; /* ELROND Header SIZE -- sizeof (header) */
1263 : : uint16_t phentsize; /* 0 */
1264 : : uint16_t phnum; /* 0 */
1265 : : uint16_t shentsize; /* Section Header SIZE -- sizeof (section) */
1266 : : uint16_t shnum; /* Section Header NUM */
1267 : : uint16_t shstrndx; /* Section Header STRing iNDeX */
1268 : : };
1269 : : /* File section. On-disk representation. */
1270 : : struct section {
1271 : : uint32_t name; /* String table offset. */
1272 : : uint32_t type; /* SHT_* */
1273 : : uint32_t flags; /* SHF_* */
1274 : : uint32_t addr; /* 0 */
1275 : : uint32_t offset; /* OFFSET in file */
1276 : : uint32_t size; /* SIZE of section */
1277 : : uint32_t link; /* 0 */
1278 : : uint32_t info; /* 0 */
1279 : : uint32_t addralign; /* 0 */
1280 : : uint32_t entsize; /* ENTry SIZE, usually 0 */
1281 : : };
1282 : :
1283 : : protected:
1284 : : data hdr; /* The header. */
1285 : : data sectab; /* The section table. */
1286 : : data strtab; /* String table. */
1287 : : int fd; /* File descriptor we're reading or writing. */
1288 : : int err; /* Sticky error code. */
1289 : :
1290 : : public:
1291 : : /* Construct from STREAM. E is errno if STREAM NULL. */
1292 : 5489 : elf (int fd, int e)
1293 : 10978 : :hdr (), sectab (), strtab (), fd (fd), err (fd >= 0 ? 0 : e)
1294 : : {}
1295 : 5410 : ~elf ()
1296 : : {
1297 : 5410 : gcc_checking_assert (fd < 0 && !hdr.buffer
1298 : : && !sectab.buffer && !strtab.buffer);
1299 : 5410 : }
1300 : :
1301 : : public:
1302 : : /* Return the error, if we have an error. */
1303 : 361186 : int get_error () const
1304 : : {
1305 : 361186 : return err;
1306 : : }
1307 : : /* Set the error, unless it's already been set. */
1308 : 53 : void set_error (int e = E_BAD_DATA)
1309 : : {
1310 : 53 : if (!err)
1311 : 19 : err = e;
1312 : 0 : }
1313 : : /* Get an error string. */
1314 : : const char *get_error (const char *) const;
1315 : :
1316 : : public:
1317 : : /* Begin reading/writing file. Return false on error. */
1318 : 5380 : bool begin () const
1319 : : {
1320 : 5380 : return !get_error ();
1321 : : }
1322 : : /* Finish reading/writing file. Return false on error. */
1323 : : bool end ();
1324 : : };
1325 : :
1326 : : /* Return error string. */
1327 : :
1328 : : const char *
1329 : 40 : elf::get_error (const char *name) const
1330 : : {
1331 : 40 : if (!name)
1332 : : return "Unknown CMI mapping";
1333 : :
1334 : 40 : switch (err)
1335 : : {
1336 : 0 : case 0:
1337 : 0 : gcc_unreachable ();
1338 : : case E_BAD_DATA:
1339 : : return "Bad file data";
1340 : 6 : case E_BAD_IMPORT:
1341 : 6 : return "Bad import dependency";
1342 : 0 : case E_BAD_LAZY:
1343 : 0 : return "Bad lazy ordering";
1344 : 21 : default:
1345 : 21 : return xstrerror (err);
1346 : : }
1347 : : }
1348 : :
1349 : : /* Finish file, return true if there's an error. */
1350 : :
1351 : : bool
1352 : 7657 : elf::end ()
1353 : : {
1354 : : /* Close the stream and free the section table. */
1355 : 7657 : if (fd >= 0 && close (fd))
1356 : 0 : set_error (errno);
1357 : 7657 : fd = -1;
1358 : :
1359 : 7657 : return !get_error ();
1360 : : }
1361 : :
1362 : : /* ELROND reader. */
1363 : :
1364 : : class elf_in : public elf {
1365 : : typedef elf parent;
1366 : :
1367 : : private:
1368 : : /* For freezing & defrosting. */
1369 : : #if !defined (HOST_LACKS_INODE_NUMBERS)
1370 : : dev_t device;
1371 : : ino_t inode;
1372 : : #endif
1373 : :
1374 : : public:
1375 : 2807 : elf_in (int fd, int e)
1376 : 5614 : :parent (fd, e)
1377 : : {
1378 : : }
1379 : 2728 : ~elf_in ()
1380 : : {
1381 : 2728 : }
1382 : :
1383 : : public:
1384 : 168309 : bool is_frozen () const
1385 : : {
1386 : 18 : return fd < 0 && hdr.pos;
1387 : : }
1388 : 18 : bool is_freezable () const
1389 : : {
1390 : 9 : return fd >= 0 && hdr.pos;
1391 : : }
1392 : : void freeze ();
1393 : : bool defrost (const char *);
1394 : :
1395 : : /* If BYTES is in the mmapped area, allocate a new buffer for it. */
1396 : 0 : void preserve (bytes_in &bytes ATTRIBUTE_UNUSED)
1397 : : {
1398 : : #if MAPPED_READING
1399 : 0 : if (hdr.buffer && bytes.buffer >= hdr.buffer
1400 : 0 : && bytes.buffer < hdr.buffer + hdr.pos)
1401 : : {
1402 : 0 : char *buf = bytes.buffer;
1403 : 0 : bytes.buffer = data::simple_memory.grow (NULL, bytes.size);
1404 : 0 : memcpy (bytes.buffer, buf, bytes.size);
1405 : : }
1406 : : #endif
1407 : 0 : }
1408 : : /* If BYTES is not in SELF's mmapped area, free it. SELF might be
1409 : : NULL. */
1410 : 1046 : static void release (elf_in *self ATTRIBUTE_UNUSED, bytes_in &bytes)
1411 : : {
1412 : : #if MAPPED_READING
1413 : 1046 : if (!(self && self->hdr.buffer && bytes.buffer >= self->hdr.buffer
1414 : 1046 : && bytes.buffer < self->hdr.buffer + self->hdr.pos))
1415 : : #endif
1416 : 0 : data::simple_memory.shrink (bytes.buffer);
1417 : 1046 : bytes.buffer = NULL;
1418 : 1046 : bytes.size = 0;
1419 : 1046 : }
1420 : :
1421 : : public:
1422 : 191046 : static void grow (data &data, unsigned needed)
1423 : : {
1424 : 191046 : gcc_checking_assert (!data.buffer);
1425 : : #if !MAPPED_READING
1426 : : data.buffer = XNEWVEC (char, needed);
1427 : : #endif
1428 : 191046 : data.size = needed;
1429 : 191046 : }
1430 : 197100 : static void shrink (data &data)
1431 : : {
1432 : : #if !MAPPED_READING
1433 : : XDELETEVEC (data.buffer);
1434 : : #endif
1435 : 197100 : data.buffer = NULL;
1436 : 197100 : data.size = 0;
1437 : 0 : }
1438 : :
1439 : : public:
1440 : 188260 : const section *get_section (unsigned s) const
1441 : : {
1442 : 188260 : if (s * sizeof (section) < sectab.size)
1443 : 188260 : return reinterpret_cast<const section *>
1444 : 188260 : (§ab.buffer[s * sizeof (section)]);
1445 : : else
1446 : : return NULL;
1447 : : }
1448 : : unsigned get_section_limit () const
1449 : : {
1450 : : return sectab.size / sizeof (section);
1451 : : }
1452 : :
1453 : : protected:
1454 : : const char *read (data *, unsigned, unsigned);
1455 : :
1456 : : public:
1457 : : /* Read section by number. */
1458 : 188260 : bool read (data *d, const section *s)
1459 : : {
1460 : 188260 : return s && read (d, s->offset, s->size);
1461 : : }
1462 : :
1463 : : /* Find section by name. */
1464 : : unsigned find (const char *name);
1465 : : /* Find section by index. */
1466 : : const section *find (unsigned snum, unsigned type = SHT_PROGBITS);
1467 : :
1468 : : public:
1469 : : /* Release the string table, when we're done with it. */
1470 : 7736 : void release ()
1471 : : {
1472 : 7736 : shrink (strtab);
1473 : 39 : }
1474 : :
1475 : : public:
1476 : : bool begin (location_t);
1477 : 4975 : bool end ()
1478 : : {
1479 : 4975 : release ();
1480 : : #if MAPPED_READING
1481 : 4975 : if (hdr.buffer)
1482 : 2728 : munmap (hdr.buffer, hdr.pos);
1483 : 4975 : hdr.buffer = NULL;
1484 : : #endif
1485 : 4975 : shrink (sectab);
1486 : :
1487 : 4975 : return parent::end ();
1488 : : }
1489 : :
1490 : : public:
1491 : : /* Return string name at OFFSET. Checks OFFSET range. Always
1492 : : returns non-NULL. We know offset 0 is an empty string. */
1493 : 281982 : const char *name (unsigned offset)
1494 : : {
1495 : 563964 : return &strtab.buffer[offset < strtab.size ? offset : 0];
1496 : : }
1497 : : };
1498 : :
1499 : : /* ELROND writer. */
1500 : :
1501 : : class elf_out : public elf, public data::allocator {
1502 : : typedef elf parent;
1503 : : /* Desired section alignment on disk. */
1504 : : static const int SECTION_ALIGN = 16;
1505 : :
1506 : : private:
1507 : : ptr_int_hash_map identtab; /* Map of IDENTIFIERS to strtab offsets. */
1508 : : unsigned pos; /* Write position in file. */
1509 : : #if MAPPED_WRITING
1510 : : unsigned offset; /* Offset of the mapping. */
1511 : : unsigned extent; /* Length of mapping. */
1512 : : unsigned page_size; /* System page size. */
1513 : : #endif
1514 : :
1515 : : public:
1516 : 2682 : elf_out (int fd, int e)
1517 : 5261 : :parent (fd, e), identtab (500), pos (0)
1518 : : {
1519 : : #if MAPPED_WRITING
1520 : 2682 : offset = extent = 0;
1521 : 2682 : page_size = sysconf (_SC_PAGE_SIZE);
1522 : 2682 : if (page_size < SECTION_ALIGN)
1523 : : /* Something really strange. */
1524 : 0 : set_error (EINVAL);
1525 : : #endif
1526 : 2682 : }
1527 : 2682 : ~elf_out ()
1528 : 2682 : {
1529 : 2682 : data::simple_memory.shrink (hdr);
1530 : 2682 : data::simple_memory.shrink (sectab);
1531 : 2682 : data::simple_memory.shrink (strtab);
1532 : 2682 : }
1533 : :
1534 : : #if MAPPED_WRITING
1535 : : private:
1536 : : void create_mapping (unsigned ext, bool extending = true);
1537 : : void remove_mapping ();
1538 : : #endif
1539 : :
1540 : : protected:
1541 : : using allocator::grow;
1542 : : char *grow (char *, unsigned needed) final override;
1543 : : #if MAPPED_WRITING
1544 : : using allocator::shrink;
1545 : : void shrink (char *) final override;
1546 : : #endif
1547 : :
1548 : : public:
1549 : 5361 : unsigned get_section_limit () const
1550 : : {
1551 : 5361 : return sectab.pos / sizeof (section);
1552 : : }
1553 : :
1554 : : protected:
1555 : : unsigned add (unsigned type, unsigned name = 0,
1556 : : unsigned off = 0, unsigned size = 0, unsigned flags = SHF_NONE);
1557 : : unsigned write (const data &);
1558 : : #if MAPPED_WRITING
1559 : : unsigned write (const bytes_out &);
1560 : : #endif
1561 : :
1562 : : public:
1563 : : /* IDENTIFIER to strtab offset. */
1564 : : unsigned name (tree ident);
1565 : : /* String literal to strtab offset. */
1566 : : unsigned name (const char *n);
1567 : : /* Qualified name of DECL to strtab offset. */
1568 : : unsigned qualified_name (tree decl, bool is_defn);
1569 : :
1570 : : private:
1571 : : unsigned strtab_write (const char *s, unsigned l);
1572 : : void strtab_write (tree decl, int);
1573 : :
1574 : : public:
1575 : : /* Add a section with contents or strings. */
1576 : : unsigned add (const bytes_out &, bool string_p, unsigned name);
1577 : :
1578 : : public:
1579 : : /* Begin and end writing. */
1580 : : bool begin ();
1581 : : bool end ();
1582 : : };
1583 : :
1584 : : /* Begin reading section NAME (of type PROGBITS) from SOURCE.
1585 : : Data always checked for CRC. */
1586 : :
1587 : : bool
1588 : 19924 : bytes_in::begin (location_t loc, elf_in *source, const char *name)
1589 : : {
1590 : 19924 : unsigned snum = source->find (name);
1591 : :
1592 : 19924 : return begin (loc, source, snum, name);
1593 : : }
1594 : :
1595 : : /* Begin reading section numbered SNUM with NAME (may be NULL). */
1596 : :
1597 : : bool
1598 : 185474 : bytes_in::begin (location_t loc, elf_in *source, unsigned snum, const char *name)
1599 : : {
1600 : 185474 : if (!source->read (this, source->find (snum))
1601 : 185474 : || !size || !check_crc ())
1602 : : {
1603 : 0 : source->set_error (elf::E_BAD_DATA);
1604 : 0 : source->shrink (*this);
1605 : 0 : if (name)
1606 : 0 : error_at (loc, "section %qs is missing or corrupted", name);
1607 : : else
1608 : 0 : error_at (loc, "section #%u is missing or corrupted", snum);
1609 : 0 : return false;
1610 : : }
1611 : 185474 : pos = 4;
1612 : 185474 : return true;
1613 : : }
1614 : :
1615 : : /* Finish reading a section. */
1616 : :
1617 : : bool
1618 : 184389 : bytes_in::end (elf_in *src)
1619 : : {
1620 : 184389 : if (more_p ())
1621 : 13 : set_overrun ();
1622 : 184389 : if (overrun)
1623 : 13 : src->set_error ();
1624 : :
1625 : 184389 : src->shrink (*this);
1626 : :
1627 : 184389 : return !overrun;
1628 : : }
1629 : :
1630 : : /* Begin writing buffer. */
1631 : :
1632 : : void
1633 : 245674 : bytes_out::begin (bool need_crc)
1634 : : {
1635 : 0 : if (need_crc)
1636 : 0 : pos = 4;
1637 : 0 : memory->grow (*this, 0, false);
1638 : 225594 : }
1639 : :
1640 : : /* Finish writing buffer. Stream out to SINK as named section NAME.
1641 : : Return section number or 0 on failure. If CRC_PTR is true, crc
1642 : : the data. Otherwise it is a string section. */
1643 : :
1644 : : unsigned
1645 : 245674 : bytes_out::end (elf_out *sink, unsigned name, unsigned *crc_ptr)
1646 : : {
1647 : 245674 : lengths[3] += pos;
1648 : 245674 : spans[3]++;
1649 : :
1650 : 245674 : set_crc (crc_ptr);
1651 : 245674 : unsigned sec_num = sink->add (*this, !crc_ptr, name);
1652 : 245674 : memory->shrink (*this);
1653 : :
1654 : 245674 : return sec_num;
1655 : : }
1656 : :
1657 : : /* Close and open the file, without destroying it. */
1658 : :
1659 : : void
1660 : 9 : elf_in::freeze ()
1661 : : {
1662 : 9 : gcc_checking_assert (!is_frozen ());
1663 : : #if MAPPED_READING
1664 : 9 : if (munmap (hdr.buffer, hdr.pos) < 0)
1665 : 0 : set_error (errno);
1666 : : #endif
1667 : 9 : if (close (fd) < 0)
1668 : 0 : set_error (errno);
1669 : 9 : fd = -1;
1670 : 9 : }
1671 : :
1672 : : bool
1673 : 9 : elf_in::defrost (const char *name)
1674 : : {
1675 : 9 : gcc_checking_assert (is_frozen ());
1676 : 9 : struct stat stat;
1677 : :
1678 : 9 : fd = open (name, O_RDONLY | O_CLOEXEC | O_BINARY);
1679 : 9 : if (fd < 0 || fstat (fd, &stat) < 0)
1680 : 0 : set_error (errno);
1681 : : else
1682 : : {
1683 : 9 : bool ok = hdr.pos == unsigned (stat.st_size);
1684 : : #ifndef HOST_LACKS_INODE_NUMBERS
1685 : 9 : if (device != stat.st_dev
1686 : 9 : || inode != stat.st_ino)
1687 : : ok = false;
1688 : : #endif
1689 : 9 : if (!ok)
1690 : 0 : set_error (EMFILE);
1691 : : #if MAPPED_READING
1692 : 0 : if (ok)
1693 : : {
1694 : 9 : char *mapping = reinterpret_cast<char *>
1695 : 9 : (mmap (NULL, hdr.pos, PROT_READ, MAP_SHARED, fd, 0));
1696 : 9 : if (mapping == MAP_FAILED)
1697 : 0 : fail:
1698 : 0 : set_error (errno);
1699 : : else
1700 : : {
1701 : 9 : if (madvise (mapping, hdr.pos, MADV_RANDOM))
1702 : 0 : goto fail;
1703 : :
1704 : : /* These buffers are never NULL in this case. */
1705 : 9 : strtab.buffer = mapping + strtab.pos;
1706 : 9 : sectab.buffer = mapping + sectab.pos;
1707 : 9 : hdr.buffer = mapping;
1708 : : }
1709 : : }
1710 : : #endif
1711 : : }
1712 : :
1713 : 9 : return !get_error ();
1714 : : }
1715 : :
1716 : : /* Read at current position into BUFFER. Return true on success. */
1717 : :
1718 : : const char *
1719 : 191046 : elf_in::read (data *data, unsigned pos, unsigned length)
1720 : : {
1721 : : #if MAPPED_READING
1722 : 191046 : if (pos + length > hdr.pos)
1723 : : {
1724 : 0 : set_error (EINVAL);
1725 : 0 : return NULL;
1726 : : }
1727 : : #else
1728 : : if (pos != ~0u && lseek (fd, pos, SEEK_SET) < 0)
1729 : : {
1730 : : set_error (errno);
1731 : : return NULL;
1732 : : }
1733 : : #endif
1734 : 191046 : grow (*data, length);
1735 : : #if MAPPED_READING
1736 : 191046 : data->buffer = hdr.buffer + pos;
1737 : : #else
1738 : : if (::read (fd, data->buffer, data->size) != ssize_t (length))
1739 : : {
1740 : : set_error (errno);
1741 : : shrink (*data);
1742 : : return NULL;
1743 : : }
1744 : : #endif
1745 : :
1746 : 191046 : return data->buffer;
1747 : : }
1748 : :
1749 : : /* Read section SNUM of TYPE. Return section pointer or NULL on error. */
1750 : :
1751 : : const elf::section *
1752 : 188260 : elf_in::find (unsigned snum, unsigned type)
1753 : : {
1754 : 188260 : const section *sec = get_section (snum);
1755 : 188260 : if (!snum || !sec || sec->type != type)
1756 : 0 : return NULL;
1757 : : return sec;
1758 : : }
1759 : :
1760 : : /* Find a section NAME and TYPE. Return section number, or zero on
1761 : : failure. */
1762 : :
1763 : : unsigned
1764 : 19969 : elf_in::find (const char *sname)
1765 : : {
1766 : 127928 : for (unsigned pos = sectab.size; pos -= sizeof (section); )
1767 : : {
1768 : 127928 : const section *sec
1769 : 127928 : = reinterpret_cast<const section *> (§ab.buffer[pos]);
1770 : :
1771 : 255856 : if (0 == strcmp (sname, name (sec->name)))
1772 : 19969 : return pos / sizeof (section);
1773 : : }
1774 : :
1775 : : return 0;
1776 : : }
1777 : :
1778 : : /* Begin reading file. Verify header. Pull in section and string
1779 : : tables. Return true on success. */
1780 : :
1781 : : bool
1782 : 2807 : elf_in::begin (location_t loc)
1783 : : {
1784 : 2807 : if (!parent::begin ())
1785 : : return false;
1786 : :
1787 : 2786 : struct stat stat;
1788 : 2786 : unsigned size = 0;
1789 : 2786 : if (!fstat (fd, &stat))
1790 : : {
1791 : : #if !defined (HOST_LACKS_INODE_NUMBERS)
1792 : 2786 : device = stat.st_dev;
1793 : 2786 : inode = stat.st_ino;
1794 : : #endif
1795 : : /* Never generate files > 4GB, check we've not been given one. */
1796 : 2786 : if (stat.st_size == unsigned (stat.st_size))
1797 : 2786 : size = unsigned (stat.st_size);
1798 : : }
1799 : :
1800 : : #if MAPPED_READING
1801 : : /* MAP_SHARED so that the file is backing store. If someone else
1802 : : concurrently writes it, they're wrong. */
1803 : 2786 : void *mapping = mmap (NULL, size, PROT_READ, MAP_SHARED, fd, 0);
1804 : 2786 : if (mapping == MAP_FAILED)
1805 : : {
1806 : 0 : fail:
1807 : 0 : set_error (errno);
1808 : 0 : return false;
1809 : : }
1810 : : /* We'll be hopping over this randomly. Some systems declare the
1811 : : first parm as char *, and other declare it as void *. */
1812 : 2786 : if (madvise (reinterpret_cast <char *> (mapping), size, MADV_RANDOM))
1813 : 0 : goto fail;
1814 : :
1815 : 2786 : hdr.buffer = (char *)mapping;
1816 : : #else
1817 : : read (&hdr, 0, sizeof (header));
1818 : : #endif
1819 : 2786 : hdr.pos = size; /* Record size of the file. */
1820 : :
1821 : 2786 : const header *h = reinterpret_cast<const header *> (hdr.buffer);
1822 : 2786 : if (!h)
1823 : : return false;
1824 : :
1825 : 2786 : if (h->ident.magic[0] != 0x7f
1826 : 2786 : || h->ident.magic[1] != 'E'
1827 : 2786 : || h->ident.magic[2] != 'L'
1828 : 2786 : || h->ident.magic[3] != 'F')
1829 : : {
1830 : 0 : error_at (loc, "not Encapsulated Lazy Records of Named Declarations");
1831 : 0 : failed:
1832 : 0 : shrink (hdr);
1833 : 0 : return false;
1834 : : }
1835 : :
1836 : : /* We expect a particular format -- the ELF is not intended to be
1837 : : distributable. */
1838 : 2786 : if (h->ident.klass != MY_CLASS
1839 : 2786 : || h->ident.data != MY_ENDIAN
1840 : 2786 : || h->ident.version != EV_CURRENT
1841 : 2786 : || h->type != ET_NONE
1842 : 2786 : || h->machine != EM_NONE
1843 : 2786 : || h->ident.osabi != OSABI_NONE)
1844 : : {
1845 : 0 : error_at (loc, "unexpected encapsulation format or type");
1846 : 0 : goto failed;
1847 : : }
1848 : :
1849 : 2786 : int e = -1;
1850 : 2786 : if (!h->shoff || h->shentsize != sizeof (section))
1851 : : {
1852 : 0 : malformed:
1853 : 0 : set_error (e);
1854 : 0 : error_at (loc, "encapsulation is malformed");
1855 : 0 : goto failed;
1856 : : }
1857 : :
1858 : 2786 : unsigned strndx = h->shstrndx;
1859 : 2786 : unsigned shnum = h->shnum;
1860 : 2786 : if (shnum == SHN_XINDEX)
1861 : : {
1862 : 0 : if (!read (§ab, h->shoff, sizeof (section)))
1863 : : {
1864 : 0 : section_table_fail:
1865 : 0 : e = errno;
1866 : 0 : goto malformed;
1867 : : }
1868 : 0 : shnum = get_section (0)->size;
1869 : : /* Freeing does mean we'll re-read it in the case we're not
1870 : : mapping, but this is going to be rare. */
1871 : 0 : shrink (sectab);
1872 : : }
1873 : :
1874 : 2786 : if (!shnum)
1875 : 0 : goto malformed;
1876 : :
1877 : 2786 : if (!read (§ab, h->shoff, shnum * sizeof (section)))
1878 : 0 : goto section_table_fail;
1879 : :
1880 : 2786 : if (strndx == SHN_XINDEX)
1881 : 0 : strndx = get_section (0)->link;
1882 : :
1883 : 2786 : if (!read (&strtab, find (strndx, SHT_STRTAB)))
1884 : 0 : goto malformed;
1885 : :
1886 : : /* The string table should be at least one byte, with NUL chars
1887 : : at either end. */
1888 : 2786 : if (!(strtab.size && !strtab.buffer[0]
1889 : 2786 : && !strtab.buffer[strtab.size - 1]))
1890 : 0 : goto malformed;
1891 : :
1892 : : #if MAPPED_READING
1893 : : /* Record the offsets of the section and string tables. */
1894 : 2786 : sectab.pos = h->shoff;
1895 : 2786 : strtab.pos = shnum * sizeof (section);
1896 : : #else
1897 : : shrink (hdr);
1898 : : #endif
1899 : :
1900 : 2786 : return true;
1901 : : }
1902 : :
1903 : : /* Create a new mapping. */
1904 : :
1905 : : #if MAPPED_WRITING
1906 : : void
1907 : 3314 : elf_out::create_mapping (unsigned ext, bool extending)
1908 : : {
1909 : : /* A wrapper around posix_fallocate, falling back to ftruncate
1910 : : if the underlying filesystem does not support the operation. */
1911 : 6495 : auto allocate = [](int fd, off_t offset, off_t length)
1912 : : {
1913 : : #ifdef HAVE_POSIX_FALLOCATE
1914 : 3181 : int result = posix_fallocate (fd, offset, length);
1915 : 3181 : if (result != EINVAL)
1916 : 3181 : return result == 0;
1917 : : /* Not supported by the underlying filesystem, fallback to ftruncate. */
1918 : : #endif
1919 : 0 : return ftruncate (fd, offset + length) == 0;
1920 : : };
1921 : :
1922 : 3314 : void *mapping = MAP_FAILED;
1923 : 3314 : if (extending && ext < 1024 * 1024)
1924 : : {
1925 : 3069 : if (allocate (fd, offset, ext * 2))
1926 : 3069 : mapping = mmap (NULL, ext * 2, PROT_READ | PROT_WRITE,
1927 : 3069 : MAP_SHARED, fd, offset);
1928 : 3069 : if (mapping != MAP_FAILED)
1929 : : ext *= 2;
1930 : : }
1931 : : if (mapping == MAP_FAILED)
1932 : : {
1933 : 245 : if (!extending || allocate (fd, offset, ext))
1934 : 245 : mapping = mmap (NULL, ext, PROT_READ | PROT_WRITE,
1935 : 245 : MAP_SHARED, fd, offset);
1936 : 245 : if (mapping == MAP_FAILED)
1937 : : {
1938 : 0 : set_error (errno);
1939 : : mapping = NULL;
1940 : : ext = 0;
1941 : : }
1942 : : }
1943 : 3314 : hdr.buffer = (char *)mapping;
1944 : 3314 : extent = ext;
1945 : 3314 : }
1946 : : #endif
1947 : :
1948 : : /* Flush out the current mapping. */
1949 : :
1950 : : #if MAPPED_WRITING
1951 : : void
1952 : 3320 : elf_out::remove_mapping ()
1953 : : {
1954 : 3320 : if (hdr.buffer)
1955 : : {
1956 : : /* MS_ASYNC dtrt with the removed mapping, including a
1957 : : subsequent overlapping remap. */
1958 : 3314 : if (msync (hdr.buffer, extent, MS_ASYNC)
1959 : 3314 : || munmap (hdr.buffer, extent))
1960 : : /* We're somewhat screwed at this point. */
1961 : 0 : set_error (errno);
1962 : : }
1963 : :
1964 : 3320 : hdr.buffer = NULL;
1965 : 3320 : }
1966 : : #endif
1967 : :
1968 : : /* Grow a mapping of PTR to be NEEDED bytes long. This gets
1969 : : interesting if the new size grows the EXTENT. */
1970 : :
1971 : : char *
1972 : 574876 : elf_out::grow (char *data, unsigned needed)
1973 : : {
1974 : 574876 : if (!data)
1975 : : {
1976 : : /* First allocation, check we're aligned. */
1977 : 250832 : gcc_checking_assert (!(pos & (SECTION_ALIGN - 1)));
1978 : : #if MAPPED_WRITING
1979 : 250832 : data = hdr.buffer + (pos - offset);
1980 : : #endif
1981 : : }
1982 : :
1983 : : #if MAPPED_WRITING
1984 : 574876 : unsigned off = data - hdr.buffer;
1985 : 574876 : if (off + needed > extent)
1986 : : {
1987 : : /* We need to grow the mapping. */
1988 : 608 : unsigned lwm = off & ~(page_size - 1);
1989 : 608 : unsigned hwm = (off + needed + page_size - 1) & ~(page_size - 1);
1990 : :
1991 : 608 : gcc_checking_assert (hwm > extent);
1992 : :
1993 : 608 : remove_mapping ();
1994 : :
1995 : 608 : offset += lwm;
1996 : 608 : create_mapping (extent < hwm - lwm ? hwm - lwm : extent);
1997 : :
1998 : 608 : data = hdr.buffer + (off - lwm);
1999 : : }
2000 : : #else
2001 : : data = allocator::grow (data, needed);
2002 : : #endif
2003 : :
2004 : 574876 : return data;
2005 : : }
2006 : :
2007 : : #if MAPPED_WRITING
2008 : : /* Shrinking is a NOP. */
2009 : : void
2010 : 250832 : elf_out::shrink (char *)
2011 : : {
2012 : 250832 : }
2013 : : #endif
2014 : :
2015 : : /* Write S of length L to the strtab buffer. L must include the ending
2016 : : NUL, if that's what you want. */
2017 : :
2018 : : unsigned
2019 : 1076294 : elf_out::strtab_write (const char *s, unsigned l)
2020 : : {
2021 : 1076294 : if (strtab.pos + l > strtab.size)
2022 : 1179 : data::simple_memory.grow (strtab, strtab.pos + l, false);
2023 : 1076294 : memcpy (strtab.buffer + strtab.pos, s, l);
2024 : 1076294 : unsigned res = strtab.pos;
2025 : 1076294 : strtab.pos += l;
2026 : 1076294 : return res;
2027 : : }
2028 : :
2029 : : /* Write qualified name of decl. INNER >0 if this is a definition, <0
2030 : : if this is a qualifier of an outer name. */
2031 : :
2032 : : void
2033 : 422728 : elf_out::strtab_write (tree decl, int inner)
2034 : : {
2035 : 422728 : tree ctx = CP_DECL_CONTEXT (decl);
2036 : 422728 : if (TYPE_P (ctx))
2037 : 4735 : ctx = TYPE_NAME (ctx);
2038 : 422728 : if (ctx != global_namespace)
2039 : 207517 : strtab_write (ctx, -1);
2040 : :
2041 : 422728 : tree name = DECL_NAME (decl);
2042 : 422728 : if (!name)
2043 : 372 : name = DECL_ASSEMBLER_NAME_RAW (decl);
2044 : 422728 : strtab_write (IDENTIFIER_POINTER (name), IDENTIFIER_LENGTH (name));
2045 : :
2046 : 422728 : if (inner)
2047 : 297102 : strtab_write (&"::{}"[inner+1], 2);
2048 : 422728 : }
2049 : :
2050 : : /* Map IDENTIFIER IDENT to strtab offset. Inserts into strtab if not
2051 : : already there. */
2052 : :
2053 : : unsigned
2054 : 120786 : elf_out::name (tree ident)
2055 : : {
2056 : 120786 : unsigned res = 0;
2057 : 120786 : if (ident)
2058 : : {
2059 : 120740 : bool existed;
2060 : 120740 : int *slot = &identtab.get_or_insert (ident, &existed);
2061 : 120740 : if (!existed)
2062 : 216688 : *slot = strtab_write (IDENTIFIER_POINTER (ident),
2063 : 108344 : IDENTIFIER_LENGTH (ident) + 1);
2064 : 120740 : res = *slot;
2065 : : }
2066 : 120786 : return res;
2067 : : }
2068 : :
2069 : : /* Map LITERAL to strtab offset. Does not detect duplicates and
2070 : : expects LITERAL to remain live until strtab is written out. */
2071 : :
2072 : : unsigned
2073 : 32909 : elf_out::name (const char *literal)
2074 : : {
2075 : 32909 : return strtab_write (literal, strlen (literal) + 1);
2076 : : }
2077 : :
2078 : : /* Map a DECL's qualified name to strtab offset. Does not detect
2079 : : duplicates. */
2080 : :
2081 : : unsigned
2082 : 215211 : elf_out::qualified_name (tree decl, bool is_defn)
2083 : : {
2084 : 215211 : gcc_checking_assert (DECL_P (decl) && decl != global_namespace);
2085 : 215211 : unsigned result = strtab.pos;
2086 : :
2087 : 215211 : strtab_write (decl, is_defn);
2088 : 215211 : strtab_write ("", 1);
2089 : :
2090 : 215211 : return result;
2091 : : }
2092 : :
2093 : : /* Add section to file. Return section number. TYPE & NAME identify
2094 : : the section. OFF and SIZE identify the file location of its
2095 : : data. FLAGS contains additional info. */
2096 : :
2097 : : unsigned
2098 : 250826 : elf_out::add (unsigned type, unsigned name, unsigned off, unsigned size,
2099 : : unsigned flags)
2100 : : {
2101 : 250826 : gcc_checking_assert (!(off & (SECTION_ALIGN - 1)));
2102 : 250826 : if (sectab.pos + sizeof (section) > sectab.size)
2103 : 4469 : data::simple_memory.grow (sectab, sectab.pos + sizeof (section), false);
2104 : 250826 : section *sec = reinterpret_cast<section *> (sectab.buffer + sectab.pos);
2105 : 250826 : memset (sec, 0, sizeof (section));
2106 : 250826 : sec->type = type;
2107 : 250826 : sec->flags = flags;
2108 : 250826 : sec->name = name;
2109 : 250826 : sec->offset = off;
2110 : 250826 : sec->size = size;
2111 : 250826 : if (flags & SHF_STRINGS)
2112 : 5123 : sec->entsize = 1;
2113 : :
2114 : 250826 : unsigned res = sectab.pos;
2115 : 250826 : sectab.pos += sizeof (section);
2116 : 250826 : return res / sizeof (section);
2117 : : }
2118 : :
2119 : : /* Pad to the next alignment boundary, then write BUFFER to disk.
2120 : : Return the position of the start of the write, or zero on failure. */
2121 : :
2122 : : unsigned
2123 : 10310 : elf_out::write (const data &buffer)
2124 : : {
2125 : : #if MAPPED_WRITING
2126 : : /* HDR is always mapped. */
2127 : 10310 : if (&buffer != &hdr)
2128 : : {
2129 : 5158 : bytes_out out (this);
2130 : 5158 : grow (out, buffer.pos, true);
2131 : 5158 : if (out.buffer)
2132 : 5158 : memcpy (out.buffer, buffer.buffer, buffer.pos);
2133 : 5158 : shrink (out);
2134 : 5158 : }
2135 : : else
2136 : : /* We should have been aligned during the first allocation. */
2137 : 5152 : gcc_checking_assert (!(pos & (SECTION_ALIGN - 1)));
2138 : : #else
2139 : : if (::write (fd, buffer.buffer, buffer.pos) != ssize_t (buffer.pos))
2140 : : {
2141 : : set_error (errno);
2142 : : return 0;
2143 : : }
2144 : : #endif
2145 : 10310 : unsigned res = pos;
2146 : 10310 : pos += buffer.pos;
2147 : :
2148 : 10310 : if (unsigned padding = -pos & (SECTION_ALIGN - 1))
2149 : : {
2150 : : #if !MAPPED_WRITING
2151 : : /* Align the section on disk, should help the necessary copies.
2152 : : fseeking to extend is non-portable. */
2153 : : static char zero[SECTION_ALIGN];
2154 : : if (::write (fd, &zero, padding) != ssize_t (padding))
2155 : : set_error (errno);
2156 : : #endif
2157 : 8795 : pos += padding;
2158 : : }
2159 : 10310 : return res;
2160 : : }
2161 : :
2162 : : /* Write a streaming buffer. It must be using us as an allocator. */
2163 : :
2164 : : #if MAPPED_WRITING
2165 : : unsigned
2166 : 245674 : elf_out::write (const bytes_out &buf)
2167 : : {
2168 : 245674 : gcc_checking_assert (buf.memory == this);
2169 : : /* A directly mapped buffer. */
2170 : 245674 : gcc_checking_assert (buf.buffer - hdr.buffer >= 0
2171 : : && buf.buffer - hdr.buffer + buf.size <= extent);
2172 : 245674 : unsigned res = pos;
2173 : 245674 : pos += buf.pos;
2174 : :
2175 : : /* Align up. We're not going to advance into the next page. */
2176 : 245674 : pos += -pos & (SECTION_ALIGN - 1);
2177 : :
2178 : 245674 : return res;
2179 : : }
2180 : : #endif
2181 : :
2182 : : /* Write data and add section. STRING_P is true for a string
2183 : : section, false for PROGBITS. NAME identifies the section (0 is the
2184 : : empty name). DATA is the contents. Return section number or 0 on
2185 : : failure (0 is the undef section). */
2186 : :
2187 : : unsigned
2188 : 245674 : elf_out::add (const bytes_out &data, bool string_p, unsigned name)
2189 : : {
2190 : 245674 : unsigned off = write (data);
2191 : :
2192 : 491348 : return add (string_p ? SHT_STRTAB : SHT_PROGBITS, name,
2193 : 245674 : off, data.pos, string_p ? SHF_STRINGS : SHF_NONE);
2194 : : }
2195 : :
2196 : : /* Begin writing the file. Initialize the section table and write an
2197 : : empty header. Return false on failure. */
2198 : :
2199 : : bool
2200 : 2573 : elf_out::begin ()
2201 : : {
2202 : 2573 : if (!parent::begin ())
2203 : : return false;
2204 : :
2205 : : /* Let the allocators pick a default. */
2206 : 2573 : data::simple_memory.grow (strtab, 0, false);
2207 : 2573 : data::simple_memory.grow (sectab, 0, false);
2208 : :
2209 : : /* The string table starts with an empty string. */
2210 : 2573 : name ("");
2211 : :
2212 : : /* Create the UNDEF section. */
2213 : 2573 : add (SHT_NONE);
2214 : :
2215 : : #if MAPPED_WRITING
2216 : : /* Start a mapping. */
2217 : 2573 : create_mapping (EXPERIMENT (page_size,
2218 : : (32767 + page_size) & ~(page_size - 1)));
2219 : 2573 : if (!hdr.buffer)
2220 : : return false;
2221 : : #endif
2222 : :
2223 : : /* Write an empty header. */
2224 : 2573 : grow (hdr, sizeof (header), true);
2225 : 2573 : header *h = reinterpret_cast<header *> (hdr.buffer);
2226 : 2573 : memset (h, 0, sizeof (header));
2227 : 2573 : hdr.pos = hdr.size;
2228 : 2573 : write (hdr);
2229 : 2573 : return !get_error ();
2230 : : }
2231 : :
2232 : : /* Finish writing the file. Write out the string & section tables.
2233 : : Fill in the header. Return true on error. */
2234 : :
2235 : : bool
2236 : 2682 : elf_out::end ()
2237 : : {
2238 : 2682 : if (fd >= 0)
2239 : : {
2240 : : /* Write the string table. */
2241 : 2579 : unsigned strnam = name (".strtab");
2242 : 2579 : unsigned stroff = write (strtab);
2243 : 2579 : unsigned strndx = add (SHT_STRTAB, strnam, stroff, strtab.pos,
2244 : : SHF_STRINGS);
2245 : :
2246 : : /* Store escape values in section[0]. */
2247 : 2579 : if (strndx >= SHN_LORESERVE)
2248 : : {
2249 : 0 : reinterpret_cast<section *> (sectab.buffer)->link = strndx;
2250 : 0 : strndx = SHN_XINDEX;
2251 : : }
2252 : 2579 : unsigned shnum = sectab.pos / sizeof (section);
2253 : 2579 : if (shnum >= SHN_LORESERVE)
2254 : : {
2255 : 0 : reinterpret_cast<section *> (sectab.buffer)->size = shnum;
2256 : 0 : shnum = SHN_XINDEX;
2257 : : }
2258 : :
2259 : 2579 : unsigned shoff = write (sectab);
2260 : :
2261 : : #if MAPPED_WRITING
2262 : 2579 : if (offset)
2263 : : {
2264 : 133 : remove_mapping ();
2265 : 133 : offset = 0;
2266 : 133 : create_mapping ((sizeof (header) + page_size - 1) & ~(page_size - 1),
2267 : : false);
2268 : : }
2269 : 2579 : unsigned length = pos;
2270 : : #else
2271 : : if (lseek (fd, 0, SEEK_SET) < 0)
2272 : : set_error (errno);
2273 : : #endif
2274 : : /* Write header. */
2275 : 2579 : if (!get_error ())
2276 : : {
2277 : : /* Write the correct header now. */
2278 : 2579 : header *h = reinterpret_cast<header *> (hdr.buffer);
2279 : 2579 : h->ident.magic[0] = 0x7f;
2280 : 2579 : h->ident.magic[1] = 'E'; /* Elrond */
2281 : 2579 : h->ident.magic[2] = 'L'; /* is an */
2282 : 2579 : h->ident.magic[3] = 'F'; /* elf. */
2283 : 2579 : h->ident.klass = MY_CLASS;
2284 : 2579 : h->ident.data = MY_ENDIAN;
2285 : 2579 : h->ident.version = EV_CURRENT;
2286 : 2579 : h->ident.osabi = OSABI_NONE;
2287 : 2579 : h->type = ET_NONE;
2288 : 2579 : h->machine = EM_NONE;
2289 : 2579 : h->version = EV_CURRENT;
2290 : 2579 : h->shoff = shoff;
2291 : 2579 : h->ehsize = sizeof (header);
2292 : 2579 : h->shentsize = sizeof (section);
2293 : 2579 : h->shnum = shnum;
2294 : 2579 : h->shstrndx = strndx;
2295 : :
2296 : 2579 : pos = 0;
2297 : 2579 : write (hdr);
2298 : : }
2299 : :
2300 : : #if MAPPED_WRITING
2301 : 2579 : remove_mapping ();
2302 : 2579 : if (ftruncate (fd, length))
2303 : 0 : set_error (errno);
2304 : : #endif
2305 : : }
2306 : :
2307 : 2682 : data::simple_memory.shrink (sectab);
2308 : 2682 : data::simple_memory.shrink (strtab);
2309 : :
2310 : 2682 : return parent::end ();
2311 : : }
2312 : :
2313 : : /********************************************************************/
2314 : :
2315 : : /* A dependency set. This is used during stream out to determine the
2316 : : connectivity of the graph. Every namespace-scope declaration that
2317 : : needs writing has a depset. The depset is filled with the (depsets
2318 : : of) declarations within this module that it references. For a
2319 : : declaration that'll generally be named types. For definitions
2320 : : it'll also be declarations in the body.
2321 : :
2322 : : From that we can convert the graph to a DAG, via determining the
2323 : : Strongly Connected Clusters. Each cluster is streamed
2324 : : independently, and thus we achieve lazy loading.
2325 : :
2326 : : Other decls that get a depset are namespaces themselves and
2327 : : unnameable declarations. */
2328 : :
2329 : : class depset {
2330 : : private:
2331 : : tree entity; /* Entity, or containing namespace. */
2332 : : uintptr_t discriminator; /* Flags or identifier. */
2333 : :
2334 : : public:
2335 : : /* The kinds of entity the depset could describe. The ordering is
2336 : : significant, see entity_kind_name. */
2337 : : enum entity_kind
2338 : : {
2339 : : EK_DECL, /* A decl. */
2340 : : EK_SPECIALIZATION, /* A specialization. */
2341 : : EK_PARTIAL, /* A partial specialization. */
2342 : : EK_USING, /* A using declaration (at namespace scope). */
2343 : : EK_NAMESPACE, /* A namespace. */
2344 : : EK_TU_LOCAL, /* A TU-local decl for ADL. */
2345 : : EK_REDIRECT, /* Redirect to a template_decl. */
2346 : : EK_EXPLICIT_HWM,
2347 : : EK_BINDING = EK_EXPLICIT_HWM, /* Implicitly encoded. */
2348 : : EK_FOR_BINDING, /* A decl being inserted for a binding. */
2349 : : EK_INNER_DECL, /* A decl defined outside of its imported
2350 : : context. */
2351 : : EK_DIRECT_HWM = EK_PARTIAL + 1,
2352 : :
2353 : : EK_BITS = 3 /* Only need to encode below EK_EXPLICIT_HWM. */
2354 : : };
2355 : : static_assert (EK_EXPLICIT_HWM < (1u << EK_BITS),
2356 : : "not enough bits reserved for entity_kind");
2357 : :
2358 : : private:
2359 : : /* Placement of bit fields in discriminator. */
2360 : : enum disc_bits
2361 : : {
2362 : : DB_ZERO_BIT, /* Set to disambiguate identifier from flags */
2363 : : DB_SPECIAL_BIT, /* First dep slot is special. */
2364 : : DB_KIND_BIT, /* Kind of the entity. */
2365 : : DB_KIND_BITS = EK_BITS,
2366 : : DB_DEFN_BIT = DB_KIND_BIT + DB_KIND_BITS,
2367 : : DB_IS_PENDING_BIT, /* Is a maybe-pending entity. */
2368 : : DB_TU_LOCAL_BIT, /* Is a TU-local entity. */
2369 : : DB_REF_GLOBAL_BIT, /* Refers to a GMF TU-local entity. */
2370 : : DB_REF_PURVIEW_BIT, /* Refers to a purview TU-local entity. */
2371 : : DB_EXPOSE_GLOBAL_BIT, /* Exposes a GMF TU-local entity. */
2372 : : DB_EXPOSE_PURVIEW_BIT, /* Exposes a purview TU-local entity. */
2373 : : DB_IMPORTED_BIT, /* An imported entity. */
2374 : : DB_UNREACHED_BIT, /* A yet-to-be reached entity. */
2375 : : DB_MAYBE_RECURSIVE_BIT, /* An entity maybe in a recursive cluster. */
2376 : : DB_ENTRY_BIT, /* The first reached recursive dep. */
2377 : : DB_HIDDEN_BIT, /* A hidden binding. */
2378 : : /* The following bits are not independent, but enumerating them is
2379 : : awkward. */
2380 : : DB_TYPE_SPEC_BIT, /* Specialization in the type table. */
2381 : : DB_FRIEND_SPEC_BIT, /* An instantiated template friend. */
2382 : : DB_HWM,
2383 : : };
2384 : : static_assert (DB_HWM <= sizeof(discriminator) * CHAR_BIT,
2385 : : "not enough bits in discriminator");
2386 : :
2387 : : public:
2388 : : /* The first slot is special for EK_SPECIALIZATIONS it is a
2389 : : spec_entry pointer. It is not relevant for the SCC
2390 : : determination. */
2391 : : vec<depset *> deps; /* Depsets we reference. */
2392 : :
2393 : : public:
2394 : : unsigned cluster; /* Strongly connected cluster, later entity number */
2395 : : unsigned section; /* Section written to. */
2396 : : /* During SCC construction, section is lowlink, until the depset is
2397 : : removed from the stack. See Tarjan algorithm for details. */
2398 : :
2399 : : private:
2400 : : /* Construction via factories. Destruction via hash traits. */
2401 : : depset (tree entity);
2402 : : ~depset ();
2403 : :
2404 : : public:
2405 : : static depset *make_binding (tree, tree);
2406 : : static depset *make_entity (tree, entity_kind, bool = false);
2407 : : /* Late setting a binding name -- /then/ insert into hash! */
2408 : : inline void set_binding_name (tree name)
2409 : : {
2410 : : gcc_checking_assert (!get_name ());
2411 : : discriminator = reinterpret_cast<uintptr_t> (name);
2412 : : }
2413 : :
2414 : : private:
2415 : 3449018 : template<unsigned I> void set_flag_bit ()
2416 : : {
2417 : 0 : gcc_checking_assert (I < 2 || !is_binding ());
2418 : 3449018 : discriminator |= 1u << I;
2419 : 2064570 : }
2420 : 287740 : template<unsigned I> void clear_flag_bit ()
2421 : : {
2422 : 0 : gcc_checking_assert (I < 2 || !is_binding ());
2423 : 287740 : discriminator &= ~(1u << I);
2424 : 287740 : }
2425 : 360175729 : template<unsigned I> bool get_flag_bit () const
2426 : : {
2427 : 0 : gcc_checking_assert (I < 2 || !is_binding ());
2428 : 438900067 : return bool ((discriminator >> I) & 1);
2429 : : }
2430 : :
2431 : : public:
2432 : 352662650 : bool is_binding () const
2433 : : {
2434 : 81076648 : return !get_flag_bit<DB_ZERO_BIT> ();
2435 : : }
2436 : 195842519 : entity_kind get_entity_kind () const
2437 : : {
2438 : 13608 : if (is_binding ())
2439 : : return EK_BINDING;
2440 : 147702907 : return entity_kind ((discriminator >> DB_KIND_BIT) & ((1u << EK_BITS) - 1));
2441 : : }
2442 : : const char *entity_kind_name () const;
2443 : :
2444 : : public:
2445 : 6531001 : bool has_defn () const
2446 : : {
2447 : : /* Never consider TU-local entities as having definitions, since
2448 : : we will never be accessing them from importers anyway. */
2449 : 6531001 : return get_flag_bit<DB_DEFN_BIT> () && !is_tu_local ();
2450 : : }
2451 : :
2452 : : public:
2453 : : /* This entity might be found other than by namespace-scope lookup;
2454 : : see module_state::write_pendings for more details. */
2455 : 1601986 : bool is_pending_entity () const
2456 : : {
2457 : 2576102 : return (get_entity_kind () == EK_SPECIALIZATION
2458 : 974116 : || get_entity_kind () == EK_PARTIAL
2459 : 2547400 : || (get_entity_kind () == EK_DECL
2460 : 927476 : && get_flag_bit<DB_IS_PENDING_BIT> ()));
2461 : : }
2462 : :
2463 : : public:
2464 : : /* Only consider global module entities as being TU-local
2465 : : when STRICT is set; otherwise, as an extension we support
2466 : : emitting declarations referencing TU-local GMF entities
2467 : : (and only check purview entities), to assist in migration. */
2468 : 31846023 : bool is_tu_local (bool strict = false) const
2469 : : {
2470 : : /* Non-strict is only intended for migration purposes, so
2471 : : for simplicity's sake we only care about whether this is
2472 : : a non-purview variable or function at namespace scope;
2473 : : these are the most common cases (coming from C), and
2474 : : that way we don't have to care about diagnostics for
2475 : : nested types and so forth. */
2476 : 13248491 : tree inner = STRIP_TEMPLATE (get_entity ());
2477 : 31846023 : return (get_flag_bit<DB_TU_LOCAL_BIT> ()
2478 : 31846023 : && (strict
2479 : 3209 : || !VAR_OR_FUNCTION_DECL_P (inner)
2480 : 2052 : || !NAMESPACE_SCOPE_P (inner)
2481 : 2025 : || (DECL_LANG_SPECIFIC (inner)
2482 : 2025 : && DECL_MODULE_PURVIEW_P (inner))));
2483 : : }
2484 : 783100 : bool refs_tu_local (bool strict = false) const
2485 : : {
2486 : 783100 : return (get_flag_bit<DB_REF_PURVIEW_BIT> ()
2487 : 783100 : || (strict && get_flag_bit <DB_REF_GLOBAL_BIT> ()));
2488 : : }
2489 : 2222982 : bool is_exposure (bool strict = false) const
2490 : : {
2491 : 2222982 : return (get_flag_bit<DB_EXPOSE_PURVIEW_BIT> ()
2492 : 2222982 : || (strict && get_flag_bit <DB_EXPOSE_GLOBAL_BIT> ()));
2493 : : }
2494 : :
2495 : : public:
2496 : 19661024 : bool is_import () const
2497 : : {
2498 : 5988106 : return get_flag_bit<DB_IMPORTED_BIT> ();
2499 : : }
2500 : 12012562 : bool is_unreached () const
2501 : : {
2502 : 805301 : return get_flag_bit<DB_UNREACHED_BIT> ();
2503 : : }
2504 : 1011924 : bool is_hidden () const
2505 : : {
2506 : 1011924 : return get_flag_bit<DB_HIDDEN_BIT> ();
2507 : : }
2508 : 648228 : bool is_maybe_recursive () const
2509 : : {
2510 : 648228 : return get_flag_bit<DB_MAYBE_RECURSIVE_BIT> ();
2511 : : }
2512 : 822 : bool is_entry () const
2513 : : {
2514 : 822 : return get_flag_bit<DB_ENTRY_BIT> ();
2515 : : }
2516 : 941727 : bool is_type_spec () const
2517 : : {
2518 : 941727 : return get_flag_bit<DB_TYPE_SPEC_BIT> ();
2519 : : }
2520 : 941727 : bool is_friend_spec () const
2521 : : {
2522 : 941727 : return get_flag_bit<DB_FRIEND_SPEC_BIT> ();
2523 : : }
2524 : :
2525 : : public:
2526 : : /* We set these bit outside of depset. */
2527 : 4749 : void set_hidden_binding ()
2528 : : {
2529 : 4749 : set_flag_bit<DB_HIDDEN_BIT> ();
2530 : 4749 : }
2531 : 30 : void clear_hidden_binding ()
2532 : : {
2533 : 30 : clear_flag_bit<DB_HIDDEN_BIT> ();
2534 : 30 : }
2535 : :
2536 : : public:
2537 : 7513079 : bool is_special () const
2538 : : {
2539 : 7513079 : return get_flag_bit<DB_SPECIAL_BIT> ();
2540 : : }
2541 : 1384448 : void set_special ()
2542 : : {
2543 : 1384448 : set_flag_bit<DB_SPECIAL_BIT> ();
2544 : 0 : }
2545 : :
2546 : : public:
2547 : 120500302 : tree get_entity () const
2548 : : {
2549 : 31846023 : return entity;
2550 : : }
2551 : 16123693 : tree get_name () const
2552 : : {
2553 : 16123693 : gcc_checking_assert (is_binding ());
2554 : 16123693 : return reinterpret_cast <tree> (discriminator);
2555 : : }
2556 : :
2557 : : public:
2558 : : /* Traits for a hash table of pointers to bindings. */
2559 : : struct traits {
2560 : : /* Each entry is a pointer to a depset. */
2561 : : typedef depset *value_type;
2562 : : /* We lookup by container:maybe-identifier pair. */
2563 : : typedef std::pair<tree,tree> compare_type;
2564 : :
2565 : : static const bool empty_zero_p = true;
2566 : :
2567 : : /* hash and equality for compare_type. */
2568 : 12053338 : inline static hashval_t hash (const compare_type &p)
2569 : : {
2570 : 12053338 : hashval_t h = pointer_hash<tree_node>::hash (p.first);
2571 : 12053338 : if (p.second)
2572 : : {
2573 : 153576 : hashval_t nh = IDENTIFIER_HASH_VALUE (p.second);
2574 : 153576 : h = iterative_hash_hashval_t (h, nh);
2575 : : }
2576 : 12053338 : return h;
2577 : : }
2578 : 51299620 : inline static bool equal (const value_type b, const compare_type &p)
2579 : : {
2580 : 51299620 : if (b->entity != p.first)
2581 : : return false;
2582 : :
2583 : 8574905 : if (p.second)
2584 : 17536 : return b->discriminator == reinterpret_cast<uintptr_t> (p.second);
2585 : : else
2586 : 8557369 : return !b->is_binding ();
2587 : : }
2588 : :
2589 : : /* (re)hasher for a binding itself. */
2590 : 38187522 : inline static hashval_t hash (const value_type b)
2591 : : {
2592 : 38187522 : hashval_t h = pointer_hash<tree_node>::hash (b->entity);
2593 : 38187522 : if (b->is_binding ())
2594 : : {
2595 : 4296431 : hashval_t nh = IDENTIFIER_HASH_VALUE (b->get_name ());
2596 : 4296431 : h = iterative_hash_hashval_t (h, nh);
2597 : : }
2598 : 38187522 : return h;
2599 : : }
2600 : :
2601 : : /* Empty via NULL. */
2602 : 0 : static inline void mark_empty (value_type &p) {p = NULL;}
2603 : : static inline bool is_empty (value_type p) {return !p;}
2604 : :
2605 : : /* Nothing is deletable. Everything is insertable. */
2606 : : static bool is_deleted (value_type) { return false; }
2607 : : static void mark_deleted (value_type) { gcc_unreachable (); }
2608 : :
2609 : : /* We own the entities in the hash table. */
2610 : 2022519 : static void remove (value_type p)
2611 : : {
2612 : 2022519 : delete (p);
2613 : 2022519 : }
2614 : : };
2615 : :
2616 : : public:
2617 : : class hash : public hash_table<traits> {
2618 : : typedef traits::compare_type key_t;
2619 : : typedef hash_table<traits> parent;
2620 : :
2621 : : public:
2622 : : vec<depset *> worklist; /* Worklist of decls to walk. */
2623 : : hash *chain; /* Original table. */
2624 : : depset *current; /* Current depset being depended. */
2625 : : unsigned section; /* When writing out, the section. */
2626 : : bool reached_unreached; /* We reached an unreached entity. */
2627 : : bool writing_merge_key; /* We're writing merge key information. */
2628 : : bool ignore_tu_local; /* In a context where referencing a TU-local
2629 : : entity is not an exposure. */
2630 : :
2631 : : public:
2632 : 225578 : hash (size_t size, hash *c = NULL)
2633 : 451156 : : parent (size), chain (c), current (NULL), section (0),
2634 : 225578 : reached_unreached (false), writing_merge_key (false),
2635 : 225578 : ignore_tu_local (false)
2636 : : {
2637 : 225578 : worklist.create (size);
2638 : 225578 : }
2639 : 225578 : ~hash ()
2640 : : {
2641 : 2573 : worklist.release ();
2642 : 225578 : }
2643 : :
2644 : : public:
2645 : 67804900 : bool is_key_order () const
2646 : : {
2647 : 67804900 : return chain != NULL;
2648 : : }
2649 : :
2650 : : private:
2651 : : depset **entity_slot (tree entity, bool = true);
2652 : : depset **binding_slot (tree ctx, tree name, bool = true);
2653 : : depset *maybe_add_declaration (tree decl);
2654 : :
2655 : : public:
2656 : : depset *find_dependency (tree entity);
2657 : : depset *find_binding (tree ctx, tree name);
2658 : : depset *make_dependency (tree decl, entity_kind);
2659 : : void add_dependency (depset *);
2660 : :
2661 : : public:
2662 : : void add_mergeable (depset *);
2663 : : depset *add_dependency (tree decl, entity_kind);
2664 : : void add_namespace_context (depset *, tree ns);
2665 : :
2666 : : private:
2667 : : static bool add_binding_entity (tree, WMB_Flags, void *);
2668 : :
2669 : : public:
2670 : : bool add_namespace_entities (tree ns, bitmap partitions);
2671 : : void add_specializations (bool decl_p);
2672 : : void add_partial_entities (vec<tree, va_gc> *);
2673 : : void add_class_entities (vec<tree, va_gc> *);
2674 : :
2675 : : private:
2676 : : void add_deduction_guides (tree decl);
2677 : :
2678 : : public:
2679 : : void find_dependencies (module_state *);
2680 : : bool finalize_dependencies ();
2681 : : vec<depset *> connect ();
2682 : :
2683 : : private:
2684 : : bool diagnose_bad_internal_ref (depset *dep, bool strict = false);
2685 : : bool diagnose_template_names_tu_local (depset *dep, bool strict = false);
2686 : : };
2687 : :
2688 : : public:
2689 : : struct tarjan {
2690 : : vec<depset *> result;
2691 : : vec<depset *> stack;
2692 : : unsigned index;
2693 : :
2694 : 225549 : tarjan (unsigned size)
2695 : 225549 : : index (0)
2696 : : {
2697 : 225549 : result.create (size);
2698 : 225549 : stack.create (50);
2699 : 225549 : }
2700 : 225549 : ~tarjan ()
2701 : : {
2702 : 225549 : gcc_assert (!stack.length ());
2703 : 225549 : stack.release ();
2704 : 225549 : }
2705 : :
2706 : : public:
2707 : : void connect (depset *);
2708 : : };
2709 : : };
2710 : :
2711 : : inline
2712 : 2022519 : depset::depset (tree entity)
2713 : 2022519 : :entity (entity), discriminator (0), cluster (0), section (0)
2714 : : {
2715 : 2022519 : deps.create (0);
2716 : : }
2717 : :
2718 : : inline
2719 : 2022519 : depset::~depset ()
2720 : : {
2721 : 2022519 : deps.release ();
2722 : 2022519 : }
2723 : :
2724 : : const char *
2725 : 43245 : depset::entity_kind_name () const
2726 : : {
2727 : : /* Same order as entity_kind. */
2728 : 43245 : static const char *const names[] =
2729 : : {"decl", "specialization", "partial", "using",
2730 : : "namespace", "tu-local", "redirect", "binding"};
2731 : 43245 : static_assert (ARRAY_SIZE (names) == EK_EXPLICIT_HWM + 1,
2732 : : "names must have an entry for every explicit entity_kind");
2733 : 43245 : entity_kind kind = get_entity_kind ();
2734 : 43245 : gcc_checking_assert (kind < ARRAY_SIZE (names));
2735 : 43245 : return names[kind];
2736 : : }
2737 : :
2738 : : /* Create a depset for a namespace binding NS::NAME. */
2739 : :
2740 : 118970 : depset *depset::make_binding (tree ns, tree name)
2741 : : {
2742 : 118970 : depset *binding = new depset (ns);
2743 : :
2744 : 118970 : binding->discriminator = reinterpret_cast <uintptr_t> (name);
2745 : :
2746 : 118970 : return binding;
2747 : : }
2748 : :
2749 : 1903549 : depset *depset::make_entity (tree entity, entity_kind ek, bool is_defn)
2750 : : {
2751 : 1903549 : depset *r = new depset (entity);
2752 : :
2753 : 1903549 : r->discriminator = ((1 << DB_ZERO_BIT)
2754 : 1903549 : | (ek << DB_KIND_BIT)
2755 : 1903549 : | is_defn << DB_DEFN_BIT);
2756 : :
2757 : 1903549 : return r;
2758 : : }
2759 : :
2760 : : class pending_key
2761 : : {
2762 : : public:
2763 : : tree ns;
2764 : : tree id;
2765 : : };
2766 : :
2767 : : template<>
2768 : : struct default_hash_traits<pending_key>
2769 : : {
2770 : : using value_type = pending_key;
2771 : :
2772 : : static const bool empty_zero_p = false;
2773 : 33078214 : static hashval_t hash (const value_type &k)
2774 : : {
2775 : 33078214 : hashval_t h = IDENTIFIER_HASH_VALUE (k.id);
2776 : 33078214 : h = iterative_hash_hashval_t (DECL_UID (k.ns), h);
2777 : :
2778 : 33078214 : return h;
2779 : : }
2780 : 9869582 : static bool equal (const value_type &k, const value_type &l)
2781 : : {
2782 : 9869582 : return k.ns == l.ns && k.id == l.id;
2783 : : }
2784 : 176327 : static void mark_empty (value_type &k)
2785 : : {
2786 : 176327 : k.ns = k.id = NULL_TREE;
2787 : : }
2788 : 4797 : static void mark_deleted (value_type &k)
2789 : : {
2790 : 4797 : k.ns = NULL_TREE;
2791 : 4797 : gcc_checking_assert (k.id);
2792 : 4797 : }
2793 : 253623991 : static bool is_empty (const value_type &k)
2794 : : {
2795 : 253583979 : return k.ns == NULL_TREE && k.id == NULL_TREE;
2796 : : }
2797 : 14872718 : static bool is_deleted (const value_type &k)
2798 : : {
2799 : 14872718 : return k.ns == NULL_TREE && k.id != NULL_TREE;
2800 : : }
2801 : : static void remove (value_type &)
2802 : : {
2803 : : }
2804 : : };
2805 : :
2806 : : typedef hash_map<pending_key, auto_vec<unsigned>> pending_map_t;
2807 : :
2808 : : /* Not-loaded entities that are keyed to a namespace-scope
2809 : : identifier. See module_state::write_pendings for details. */
2810 : : pending_map_t *pending_table;
2811 : :
2812 : : /* Decls that need some post processing once a batch of lazy loads has
2813 : : completed. */
2814 : : vec<tree, va_heap, vl_embed> *post_load_decls;
2815 : :
2816 : : /* Some entities are keyed to another entitity for ODR purposes.
2817 : : For example, at namespace scope, 'inline auto var = []{};', that
2818 : : lambda is keyed to 'var', and follows its ODRness. */
2819 : : typedef hash_map<tree, auto_vec<tree>> keyed_map_t;
2820 : : static keyed_map_t *keyed_table;
2821 : :
2822 : : static tree get_keyed_decl_scope (tree);
2823 : :
2824 : : /* Instantiations of temploid friends imported from another module
2825 : : need to be attached to the same module as the temploid. This maps
2826 : : these decls to the temploid they are instantiated from, as there is
2827 : : no other easy way to get this information. */
2828 : : static GTY((cache)) decl_tree_cache_map *imported_temploid_friends;
2829 : :
2830 : : /********************************************************************/
2831 : : /* Tree streaming. The tree streaming is very specific to the tree
2832 : : structures themselves. A tag indicates the kind of tree being
2833 : : streamed. -ve tags indicate backreferences to already-streamed
2834 : : trees. Backreferences are auto-numbered. */
2835 : :
2836 : : /* Tree tags. */
2837 : : enum tree_tag {
2838 : : tt_null, /* NULL_TREE. */
2839 : : tt_tu_local, /* A TU-local entity. */
2840 : : tt_fixed, /* Fixed vector index. */
2841 : :
2842 : : tt_node, /* By-value node. */
2843 : : tt_decl, /* By-value mergeable decl. */
2844 : : tt_tpl_parm, /* Template parm. */
2845 : :
2846 : : /* The ordering of the following 5 is relied upon in
2847 : : trees_out::tree_node. */
2848 : : tt_id, /* Identifier node. */
2849 : : tt_conv_id, /* Conversion operator name. */
2850 : : tt_anon_id, /* Anonymous name. */
2851 : : tt_lambda_id, /* Lambda name. */
2852 : : tt_internal_id, /* Internal name. */
2853 : :
2854 : : tt_typedef_type, /* A (possibly implicit) typedefed type. */
2855 : : tt_derived_type, /* A type derived from another type. */
2856 : : tt_variant_type, /* A variant of another type. */
2857 : :
2858 : : tt_tinfo_var, /* Typeinfo object. */
2859 : : tt_tinfo_typedef, /* Typeinfo typedef. */
2860 : : tt_ptrmem_type, /* Pointer to member type. */
2861 : : tt_nttp_var, /* NTTP_OBJECT VAR_DECL. */
2862 : :
2863 : : tt_parm, /* Function parameter or result. */
2864 : : tt_enum_value, /* An enum value. */
2865 : : tt_enum_decl, /* An enum decl. */
2866 : : tt_data_member, /* Data member/using-decl. */
2867 : :
2868 : : tt_binfo, /* A BINFO. */
2869 : : tt_vtable, /* A vtable. */
2870 : : tt_thunk, /* A thunk. */
2871 : : tt_clone_ref,
2872 : :
2873 : : tt_entity, /* A extra-cluster entity. */
2874 : :
2875 : : tt_template, /* The TEMPLATE_RESULT of a template. */
2876 : : };
2877 : :
2878 : : enum walk_kind {
2879 : : WK_none, /* No walk to do (a back- or fixed-ref happened). */
2880 : : WK_normal, /* Normal walk (by-name if possible). */
2881 : :
2882 : : WK_value, /* By-value walk. */
2883 : : };
2884 : :
2885 : : enum merge_kind
2886 : : {
2887 : : MK_unique, /* Known unique. */
2888 : : MK_named, /* Found by CTX, NAME + maybe_arg types etc. */
2889 : : MK_field, /* Found by CTX and index on TYPE_FIELDS */
2890 : : MK_vtable, /* Found by CTX and index on TYPE_VTABLES */
2891 : : MK_as_base, /* Found by CTX. */
2892 : :
2893 : : MK_partial,
2894 : :
2895 : : MK_enum, /* Found by CTX, & 1stMemberNAME. */
2896 : : MK_keyed, /* Found by key & index. */
2897 : : MK_local_type, /* Found by CTX, index. */
2898 : :
2899 : : MK_friend_spec, /* Like named, but has a tmpl & args too. */
2900 : : MK_local_friend, /* Found by CTX, index. */
2901 : :
2902 : : MK_indirect_lwm = MK_enum,
2903 : :
2904 : : /* Template specialization kinds below. These are all found via
2905 : : primary template and specialization args. */
2906 : : MK_template_mask = 0x10, /* A template specialization. */
2907 : :
2908 : : MK_tmpl_decl_mask = 0x4, /* In decl table. */
2909 : :
2910 : : MK_tmpl_tmpl_mask = 0x1, /* We want TEMPLATE_DECL. */
2911 : :
2912 : : MK_type_spec = MK_template_mask,
2913 : : MK_decl_spec = MK_template_mask | MK_tmpl_decl_mask,
2914 : :
2915 : : MK_hwm = 0x20
2916 : : };
2917 : : /* This is more than a debugging array. NULLs are used to determine
2918 : : an invalid merge_kind number. */
2919 : : static char const *const merge_kind_name[MK_hwm] =
2920 : : {
2921 : : "unique", "named", "field", "vtable", /* 0...3 */
2922 : : "asbase", "partial", "enum", "attached", /* 4...7 */
2923 : :
2924 : : "local type", "friend spec", "local friend", NULL, /* 8...11 */
2925 : : NULL, NULL, NULL, NULL,
2926 : :
2927 : : "type spec", "type tmpl spec", /* 16,17 type (template). */
2928 : : NULL, NULL,
2929 : :
2930 : : "decl spec", "decl tmpl spec", /* 20,21 decl (template). */
2931 : : NULL, NULL,
2932 : : NULL, NULL, NULL, NULL,
2933 : : NULL, NULL, NULL, NULL,
2934 : : };
2935 : :
2936 : : /* Mergeable entity location data. */
2937 : : struct merge_key {
2938 : : cp_ref_qualifier ref_q : 2;
2939 : : unsigned index;
2940 : :
2941 : : tree ret; /* Return type, if appropriate. */
2942 : : tree args; /* Arg types, if appropriate. */
2943 : :
2944 : : tree constraints; /* Constraints. */
2945 : :
2946 : 2115286 : merge_key ()
2947 : 2115286 : :ref_q (REF_QUAL_NONE), index (0),
2948 : 2115286 : ret (NULL_TREE), args (NULL_TREE),
2949 : 2115286 : constraints (NULL_TREE)
2950 : : {
2951 : : }
2952 : : };
2953 : :
2954 : : /* Hashmap of merged duplicates. Usually decls, but can contain
2955 : : BINFOs. */
2956 : : typedef hash_map<tree,uintptr_t,
2957 : : simple_hashmap_traits<nodel_ptr_hash<tree_node>,uintptr_t> >
2958 : : duplicate_hash_map;
2959 : :
2960 : : /* Data needed for post-processing. */
2961 : : struct post_process_data {
2962 : : tree decl;
2963 : : location_t start_locus;
2964 : : location_t end_locus;
2965 : : bool returns_value;
2966 : : bool returns_null;
2967 : : bool returns_abnormally;
2968 : : bool infinite_loop;
2969 : : };
2970 : :
2971 : : /* Tree stream reader. Note that reading a stream doesn't mark the
2972 : : read trees with TREE_VISITED. Thus it's quite safe to have
2973 : : multiple concurrent readers. Which is good, because lazy
2974 : : loading.
2975 : :
2976 : : It's important that trees_in/out have internal linkage so that the
2977 : : compiler knows core_bools, lang_type_bools and lang_decl_bools have
2978 : : only a single caller (tree_node_bools) and inlines them appropriately. */
2979 : : namespace {
2980 : : class trees_in : public bytes_in {
2981 : : typedef bytes_in parent;
2982 : :
2983 : : private:
2984 : : module_state *state; /* Module being imported. */
2985 : : vec<tree> back_refs; /* Back references. */
2986 : : duplicate_hash_map *duplicates; /* Map from existings to duplicate. */
2987 : : vec<post_process_data> post_decls; /* Decls to post process. */
2988 : : unsigned unused; /* Inhibit any interior TREE_USED
2989 : : marking. */
2990 : :
2991 : : public:
2992 : : trees_in (module_state *);
2993 : : ~trees_in ();
2994 : :
2995 : : public:
2996 : : int insert (tree);
2997 : : tree back_ref (int);
2998 : :
2999 : : private:
3000 : : tree start (unsigned = 0);
3001 : :
3002 : : public:
3003 : : /* Needed for binfo writing */
3004 : : bool core_bools (tree, bits_in&);
3005 : :
3006 : : private:
3007 : : /* Stream tree_core, lang_decl_specific and lang_type_specific
3008 : : bits. */
3009 : : bool core_vals (tree);
3010 : : bool lang_type_bools (tree, bits_in&);
3011 : : bool lang_type_vals (tree);
3012 : : bool lang_decl_bools (tree, bits_in&);
3013 : : bool lang_decl_vals (tree);
3014 : : bool lang_vals (tree);
3015 : : bool tree_node_bools (tree);
3016 : : bool tree_node_vals (tree);
3017 : : tree tree_value ();
3018 : : tree decl_value ();
3019 : : tree tpl_parm_value ();
3020 : :
3021 : : private:
3022 : : tree chained_decls (); /* Follow DECL_CHAIN. */
3023 : : vec<tree, va_heap> *vec_chained_decls ();
3024 : : vec<tree, va_gc> *tree_vec (); /* vec of tree. */
3025 : : vec<tree_pair_s, va_gc> *tree_pair_vec (); /* vec of tree_pair. */
3026 : : tree tree_list (bool has_purpose);
3027 : :
3028 : : public:
3029 : : /* Read a tree node. */
3030 : : tree tree_node (bool is_use = false);
3031 : :
3032 : : private:
3033 : : bool install_entity (tree decl);
3034 : : tree tpl_parms (unsigned &tpl_levels);
3035 : : bool tpl_parms_fini (tree decl, unsigned tpl_levels);
3036 : : bool tpl_header (tree decl, unsigned *tpl_levels);
3037 : : int fn_parms_init (tree);
3038 : : void fn_parms_fini (int tag, tree fn, tree existing, bool has_defn);
3039 : : unsigned add_indirect_tpl_parms (tree);
3040 : : public:
3041 : : bool add_indirects (tree);
3042 : :
3043 : : public:
3044 : : /* Serialize various definitions. */
3045 : : bool read_definition (tree decl);
3046 : :
3047 : : private:
3048 : : void check_abi_tags (tree existing, tree decl, tree &eattr, tree &dattr);
3049 : : bool is_matching_decl (tree existing, tree decl, bool is_typedef);
3050 : : static bool install_implicit_member (tree decl);
3051 : : bool read_function_def (tree decl, tree maybe_template);
3052 : : bool read_var_def (tree decl, tree maybe_template);
3053 : : bool read_class_def (tree decl, tree maybe_template);
3054 : : bool read_enum_def (tree decl, tree maybe_template);
3055 : :
3056 : : public:
3057 : : tree decl_container ();
3058 : : tree key_mergeable (int tag, merge_kind, tree decl, tree inner, tree type,
3059 : : tree container, bool is_attached,
3060 : : bool is_imported_temploid_friend);
3061 : : unsigned binfo_mergeable (tree *);
3062 : :
3063 : : private:
3064 : : tree key_local_type (const merge_key&, tree, tree);
3065 : : uintptr_t *find_duplicate (tree existing);
3066 : : void register_duplicate (tree decl, tree existing);
3067 : : /* Mark as an already diagnosed bad duplicate. */
3068 : 54 : void unmatched_duplicate (tree existing)
3069 : : {
3070 : 108 : *find_duplicate (existing) |= 1;
3071 : 54 : }
3072 : :
3073 : : public:
3074 : 129320 : bool is_duplicate (tree decl)
3075 : : {
3076 : 258640 : return find_duplicate (decl) != NULL;
3077 : : }
3078 : 114793 : tree maybe_duplicate (tree decl)
3079 : : {
3080 : 114793 : if (uintptr_t *dup = find_duplicate (decl))
3081 : 8680 : return reinterpret_cast<tree> (*dup & ~uintptr_t (1));
3082 : : return decl;
3083 : : }
3084 : : tree odr_duplicate (tree decl, bool has_defn);
3085 : :
3086 : : public:
3087 : : /* Return the decls to postprocess. */
3088 : : const vec<post_process_data>& post_process ()
3089 : : {
3090 : : return post_decls;
3091 : : }
3092 : : private:
3093 : : /* Register DATA for postprocessing. */
3094 : 71993 : void post_process (post_process_data data)
3095 : : {
3096 : 71993 : post_decls.safe_push (data);
3097 : : }
3098 : :
3099 : : private:
3100 : : void assert_definition (tree, bool installing);
3101 : : };
3102 : : } // anon namespace
3103 : :
3104 : 169515 : trees_in::trees_in (module_state *state)
3105 : 169515 : :parent (), state (state), unused (0)
3106 : : {
3107 : 169515 : duplicates = NULL;
3108 : 169515 : back_refs.create (500);
3109 : 169515 : post_decls.create (0);
3110 : 169515 : }
3111 : :
3112 : 169515 : trees_in::~trees_in ()
3113 : : {
3114 : 264576 : delete (duplicates);
3115 : 169515 : back_refs.release ();
3116 : 169515 : post_decls.release ();
3117 : 169515 : }
3118 : :
3119 : : /* Tree stream writer. */
3120 : : namespace {
3121 : : class trees_out : public bytes_out {
3122 : : typedef bytes_out parent;
3123 : :
3124 : : private:
3125 : : module_state *state; /* The module we are writing. */
3126 : : ptr_int_hash_map tree_map; /* Trees to references */
3127 : : depset::hash *dep_hash; /* Dependency table. */
3128 : : int ref_num; /* Back reference number. */
3129 : : unsigned section;
3130 : : bool writing_local_entities; /* Whether we might walk into a TU-local
3131 : : entity we need to emit placeholders for. */
3132 : : bool walking_bit_field_unit; /* Whether we're walking the underlying
3133 : : storage for a bit field. There's no other
3134 : : great way to detect this. */
3135 : : #if CHECKING_P
3136 : : int importedness; /* Checker that imports not occurring
3137 : : inappropriately. +ve imports ok,
3138 : : -ve imports not ok. */
3139 : : #endif
3140 : :
3141 : : public:
3142 : : trees_out (allocator *, module_state *, depset::hash &deps, unsigned sec = 0);
3143 : : ~trees_out ();
3144 : :
3145 : : private:
3146 : : void mark_trees ();
3147 : : void unmark_trees ();
3148 : :
3149 : : public:
3150 : : /* Hey, let's ignore the well known STL iterator idiom. */
3151 : : void begin ();
3152 : : unsigned end (elf_out *sink, unsigned name, unsigned *crc_ptr);
3153 : : void end ();
3154 : :
3155 : : public:
3156 : : enum tags
3157 : : {
3158 : : tag_backref = -1, /* Upper bound on the backrefs. */
3159 : : tag_value = 0, /* Write by value. */
3160 : : tag_fixed /* Lower bound on the fixed trees. */
3161 : : };
3162 : :
3163 : : public:
3164 : : /* The walk is used for three similar purposes:
3165 : :
3166 : : 1. The initial scan for dependencies.
3167 : : 2. Once dependencies have been found, ordering them.
3168 : : 3. Writing dependencies to file (streaming_p).
3169 : :
3170 : : For cases where it matters, these accessers can be used to determine
3171 : : which state we're in. */
3172 : 47772030 : bool is_initial_scan () const
3173 : : {
3174 : 80663511 : return !streaming_p () && !is_key_order ();
3175 : : }
3176 : 44828364 : bool is_key_order () const
3177 : : {
3178 : 32891481 : return dep_hash->is_key_order ();
3179 : : }
3180 : :
3181 : : public:
3182 : : int insert (tree, walk_kind = WK_normal);
3183 : :
3184 : : private:
3185 : : void start (tree, bool = false);
3186 : :
3187 : : private:
3188 : : walk_kind ref_node (tree);
3189 : : public:
3190 : : int get_tag (tree);
3191 : 446010 : void set_importing (int i ATTRIBUTE_UNUSED)
3192 : : {
3193 : : #if CHECKING_P
3194 : 446010 : importedness = i;
3195 : : #endif
3196 : : }
3197 : :
3198 : : private:
3199 : : void core_bools (tree, bits_out&);
3200 : : void core_vals (tree);
3201 : : void lang_type_bools (tree, bits_out&);
3202 : : void lang_type_vals (tree);
3203 : : void lang_decl_bools (tree, bits_out&);
3204 : : void lang_decl_vals (tree);
3205 : : void lang_vals (tree);
3206 : : void tree_node_bools (tree);
3207 : : void tree_node_vals (tree);
3208 : :
3209 : : private:
3210 : : void chained_decls (tree);
3211 : : void vec_chained_decls (tree);
3212 : : void tree_vec (vec<tree, va_gc> *);
3213 : : void tree_pair_vec (vec<tree_pair_s, va_gc> *);
3214 : : void tree_list (tree, bool has_purpose);
3215 : :
3216 : : private:
3217 : : bool has_tu_local_dep (tree) const;
3218 : : tree find_tu_local_decl (tree);
3219 : :
3220 : : public:
3221 : : /* Mark a node for by-value walking. */
3222 : : void mark_by_value (tree);
3223 : :
3224 : : public:
3225 : : void tree_node (tree);
3226 : :
3227 : : private:
3228 : : void install_entity (tree decl, depset *);
3229 : : void tpl_parms (tree parms, unsigned &tpl_levels);
3230 : : void tpl_parms_fini (tree decl, unsigned tpl_levels);
3231 : : void fn_parms_fini (tree) {}
3232 : : unsigned add_indirect_tpl_parms (tree);
3233 : : public:
3234 : : void add_indirects (tree);
3235 : : void fn_parms_init (tree);
3236 : : void tpl_header (tree decl, unsigned *tpl_levels);
3237 : :
3238 : : public:
3239 : : merge_kind get_merge_kind (tree decl, depset *maybe_dep);
3240 : : tree decl_container (tree decl);
3241 : : void key_mergeable (int tag, merge_kind, tree decl, tree inner,
3242 : : tree container, depset *maybe_dep);
3243 : : void binfo_mergeable (tree binfo);
3244 : :
3245 : : private:
3246 : : void key_local_type (merge_key&, tree, tree);
3247 : : bool decl_node (tree, walk_kind ref);
3248 : : void type_node (tree);
3249 : : void tree_value (tree);
3250 : : void tpl_parm_value (tree);
3251 : :
3252 : : public:
3253 : : void decl_value (tree, depset *);
3254 : :
3255 : : public:
3256 : : /* Serialize various definitions. */
3257 : : void write_definition (tree decl, bool refs_tu_local = false);
3258 : : void mark_declaration (tree decl, bool do_defn);
3259 : :
3260 : : private:
3261 : : void mark_function_def (tree decl);
3262 : : void mark_var_def (tree decl);
3263 : : void mark_class_def (tree decl);
3264 : : void mark_enum_def (tree decl);
3265 : : void mark_class_member (tree decl, bool do_defn = true);
3266 : : void mark_binfos (tree type);
3267 : :
3268 : : private:
3269 : : void write_var_def (tree decl);
3270 : : void write_function_def (tree decl);
3271 : : void write_class_def (tree decl);
3272 : : void write_enum_def (tree decl);
3273 : :
3274 : : private:
3275 : : static void assert_definition (tree);
3276 : :
3277 : : public:
3278 : : static void instrument ();
3279 : :
3280 : : private:
3281 : : /* Tree instrumentation. */
3282 : : static unsigned tree_val_count;
3283 : : static unsigned decl_val_count;
3284 : : static unsigned back_ref_count;
3285 : : static unsigned tu_local_count;
3286 : : static unsigned null_count;
3287 : : };
3288 : : } // anon namespace
3289 : :
3290 : : /* Instrumentation counters. */
3291 : : unsigned trees_out::tree_val_count;
3292 : : unsigned trees_out::decl_val_count;
3293 : : unsigned trees_out::back_ref_count;
3294 : : unsigned trees_out::tu_local_count;
3295 : : unsigned trees_out::null_count;
3296 : :
3297 : 451172 : trees_out::trees_out (allocator *mem, module_state *state, depset::hash &deps,
3298 : 451172 : unsigned section)
3299 : 902344 : :parent (mem), state (state), tree_map (500),
3300 : 451172 : dep_hash (&deps), ref_num (0), section (section),
3301 : 451172 : writing_local_entities (false), walking_bit_field_unit (false)
3302 : : {
3303 : : #if CHECKING_P
3304 : 451172 : importedness = 0;
3305 : : #endif
3306 : 451172 : }
3307 : :
3308 : 451172 : trees_out::~trees_out ()
3309 : : {
3310 : 451172 : }
3311 : :
3312 : : /********************************************************************/
3313 : : /* Location. We're aware of the line-map concept and reproduce it
3314 : : here. Each imported module allocates a contiguous span of ordinary
3315 : : maps, and of macro maps. adhoc maps are serialized by contents,
3316 : : not pre-allocated. The scattered linemaps of a module are
3317 : : coalesced when writing. */
3318 : :
3319 : :
3320 : : /* I use half-open [first,second) ranges. */
3321 : : typedef std::pair<line_map_uint_t,line_map_uint_t> range_t;
3322 : :
3323 : : /* A range of locations. */
3324 : : typedef std::pair<location_t,location_t> loc_range_t;
3325 : :
3326 : : /* Spans of the line maps that are occupied by this TU. I.e. not
3327 : : within imports. Only extended when in an interface unit.
3328 : : Interval zero corresponds to the forced header linemap(s). This
3329 : : is a singleton object. */
3330 : :
3331 : : class loc_spans {
3332 : : public:
3333 : : /* An interval of line maps. The line maps here represent a contiguous
3334 : : non-imported range. */
3335 : 5619 : struct span {
3336 : : loc_range_t ordinary; /* Ordinary map location range. */
3337 : : loc_range_t macro; /* Macro map location range. */
3338 : : /* Add to locs to get serialized loc. */
3339 : : location_diff_t ordinary_delta;
3340 : : location_diff_t macro_delta;
3341 : : };
3342 : :
3343 : : private:
3344 : : vec<span> *spans;
3345 : : bool locs_exhausted_p;
3346 : :
3347 : : public:
3348 : : loc_spans ()
3349 : : /* Do not preallocate spans, as that causes
3350 : : --enable-detailed-mem-stats problems. */
3351 : : : spans (nullptr), locs_exhausted_p (false)
3352 : : {
3353 : : }
3354 : 96719 : ~loc_spans ()
3355 : : {
3356 : 96719 : delete spans;
3357 : 96719 : }
3358 : :
3359 : : public:
3360 : 273 : span &operator[] (unsigned ix)
3361 : : {
3362 : 546 : return (*spans)[ix];
3363 : : }
3364 : : unsigned length () const
3365 : : {
3366 : : return spans->length ();
3367 : : }
3368 : :
3369 : : public:
3370 : 14674 : bool init_p () const
3371 : : {
3372 : 14674 : return spans != nullptr;
3373 : : }
3374 : : /* Initializer. */
3375 : : void init (const line_maps *lmaps, const line_map_ordinary *map);
3376 : :
3377 : : /* Slightly skewed preprocessed files can cause us to miss an
3378 : : initialization in some places. Fallback initializer. */
3379 : 5465 : void maybe_init ()
3380 : : {
3381 : 5465 : if (!init_p ())
3382 : 6 : init (line_table, nullptr);
3383 : 5465 : }
3384 : :
3385 : : public:
3386 : : enum {
3387 : : SPAN_RESERVED = 0, /* Reserved (fixed) locations. */
3388 : : SPAN_FIRST = 1, /* LWM of locations to stream */
3389 : : SPAN_MAIN = 2 /* Main file and onwards. */
3390 : : };
3391 : :
3392 : : public:
3393 : 443115 : location_t main_start () const
3394 : : {
3395 : 443115 : return (*spans)[SPAN_MAIN].ordinary.first;
3396 : : }
3397 : :
3398 : : public:
3399 : : void open (location_t);
3400 : : void close ();
3401 : :
3402 : : public:
3403 : : /* Propagate imported linemaps to us, if needed. */
3404 : : bool maybe_propagate (module_state *import, location_t loc);
3405 : :
3406 : : public:
3407 : : /* Whether we can no longer represent new imported locations. */
3408 : 0 : bool locations_exhausted_p () const
3409 : : {
3410 : 0 : return locs_exhausted_p;
3411 : : }
3412 : 0 : void report_location_exhaustion (location_t loc)
3413 : : {
3414 : 0 : if (!locs_exhausted_p)
3415 : : {
3416 : : /* Just give the notice once. */
3417 : 0 : locs_exhausted_p = true;
3418 : 0 : inform (loc, "unable to represent further imported source locations");
3419 : : }
3420 : : }
3421 : :
3422 : : public:
3423 : : const span *ordinary (location_t);
3424 : : const span *macro (location_t);
3425 : : };
3426 : :
3427 : : static loc_spans spans;
3428 : :
3429 : : /* Information about ordinary locations we stream out. */
3430 : : struct ord_loc_info
3431 : : {
3432 : : const line_map_ordinary *src; // line map we're based on
3433 : : line_map_uint_t offset; // offset to this line
3434 : : line_map_uint_t span; // number of locs we span
3435 : : line_map_uint_t remap; // serialization
3436 : :
3437 : 196975826 : static int compare (const void *a_, const void *b_)
3438 : : {
3439 : 196975826 : auto *a = static_cast<const ord_loc_info *> (a_);
3440 : 196975826 : auto *b = static_cast<const ord_loc_info *> (b_);
3441 : :
3442 : 196975826 : if (a->src != b->src)
3443 : 43997673 : return a->src < b->src ? -1 : +1;
3444 : :
3445 : : // Ensure no overlap
3446 : 167229386 : gcc_checking_assert (a->offset + a->span <= b->offset
3447 : : || b->offset + b->span <= a->offset);
3448 : :
3449 : 167229386 : gcc_checking_assert (a->offset != b->offset);
3450 : 167229386 : return a->offset < b->offset ? -1 : +1;
3451 : : }
3452 : : };
3453 : : struct ord_loc_traits
3454 : : {
3455 : : typedef ord_loc_info value_type;
3456 : : typedef value_type compare_type;
3457 : :
3458 : : static const bool empty_zero_p = false;
3459 : :
3460 : 85061981 : static hashval_t hash (const value_type &v)
3461 : : {
3462 : 72709213 : auto h = pointer_hash<const line_map_ordinary>::hash (v.src);
3463 : 85061981 : return iterative_hash_hashval_t (v.offset, h);
3464 : : }
3465 : 93482228 : static bool equal (const value_type &v, const compare_type p)
3466 : : {
3467 : 93482228 : return v.src == p.src && v.offset == p.offset;
3468 : : }
3469 : :
3470 : 5900187 : static void mark_empty (value_type &v)
3471 : : {
3472 : 5900187 : v.src = nullptr;
3473 : : }
3474 : 266285009 : static bool is_empty (value_type &v)
3475 : : {
3476 : 266285009 : return !v.src;
3477 : : }
3478 : :
3479 : : static bool is_deleted (value_type &) { return false; }
3480 : : static void mark_deleted (value_type &) { gcc_unreachable (); }
3481 : :
3482 : 1498111 : static void remove (value_type &) {}
3483 : : };
3484 : : /* Table keyed by ord_loc_info, used for noting. */
3485 : : static hash_table<ord_loc_traits> *ord_loc_table;
3486 : : /* Sorted vector, used for writing. */
3487 : : static vec<ord_loc_info> *ord_loc_remap;
3488 : :
3489 : : /* Information about macro locations we stream out. */
3490 : : struct macro_loc_info
3491 : : {
3492 : : const line_map_macro *src; // original expansion
3493 : : line_map_uint_t remap; // serialization
3494 : :
3495 : 7052734 : static int compare (const void *a_, const void *b_)
3496 : : {
3497 : 7052734 : auto *a = static_cast<const macro_loc_info *> (a_);
3498 : 7052734 : auto *b = static_cast<const macro_loc_info *> (b_);
3499 : :
3500 : 7052734 : gcc_checking_assert (MAP_START_LOCATION (a->src)
3501 : : != MAP_START_LOCATION (b->src));
3502 : 7052734 : if (MAP_START_LOCATION (a->src) < MAP_START_LOCATION (b->src))
3503 : : return -1;
3504 : : else
3505 : 3390746 : return +1;
3506 : : }
3507 : : };
3508 : : struct macro_loc_traits
3509 : : {
3510 : : typedef macro_loc_info value_type;
3511 : : typedef const line_map_macro *compare_type;
3512 : :
3513 : : static const bool empty_zero_p = false;
3514 : :
3515 : 6621350 : static hashval_t hash (compare_type p)
3516 : : {
3517 : 6621350 : return pointer_hash<const line_map_macro>::hash (p);
3518 : : }
3519 : 5571040 : static hashval_t hash (const value_type &v)
3520 : : {
3521 : 5571040 : return hash (v.src);
3522 : : }
3523 : : static bool equal (const value_type &v, const compare_type p)
3524 : : {
3525 : : return v.src == p;
3526 : : }
3527 : :
3528 : 547488 : static void mark_empty (value_type &v)
3529 : : {
3530 : 547488 : v.src = nullptr;
3531 : : }
3532 : 21574910 : static bool is_empty (value_type &v)
3533 : : {
3534 : 21574910 : return !v.src;
3535 : : }
3536 : :
3537 : : static bool is_deleted (value_type &) { return false; }
3538 : : static void mark_deleted (value_type &) { gcc_unreachable (); }
3539 : :
3540 : 121548 : static void remove (value_type &) {}
3541 : : };
3542 : : /* Table keyed by line_map_macro, used for noting. */
3543 : : static hash_table<macro_loc_traits> *macro_loc_table;
3544 : : /* Sorted vector, used for writing. */
3545 : : static vec<macro_loc_info> *macro_loc_remap;
3546 : :
3547 : : /* Indirection to allow bsearching imports by ordinary location. */
3548 : : static vec<module_state *> *ool;
3549 : :
3550 : : /********************************************************************/
3551 : : /* Data needed by a module during the process of loading. */
3552 : : struct GTY(()) slurping {
3553 : :
3554 : : /* Remap import's module numbering to our numbering. Values are
3555 : : shifted by 1. Bit0 encodes if the import is direct. */
3556 : : vec<unsigned, va_heap, vl_embed> *
3557 : : GTY((skip)) remap; /* Module owner remapping. */
3558 : :
3559 : : elf_in *GTY((skip)) from; /* The elf loader. */
3560 : :
3561 : : /* This map is only for header imports themselves -- the global
3562 : : headers bitmap hold it for the current TU. */
3563 : : bitmap headers; /* Transitive set of direct imports, including
3564 : : self. Used for macro visibility and
3565 : : priority. */
3566 : :
3567 : : /* These objects point into the mmapped area, unless we're not doing
3568 : : that, or we got frozen or closed. In those cases they point to
3569 : : buffers we own. */
3570 : : bytes_in macro_defs; /* Macro definitions. */
3571 : : bytes_in macro_tbl; /* Macro table. */
3572 : :
3573 : : /* Location remapping. first->ordinary, second->macro. */
3574 : : range_t GTY((skip)) loc_deltas;
3575 : :
3576 : : unsigned current; /* Section currently being loaded. */
3577 : : unsigned remaining; /* Number of lazy sections yet to read. */
3578 : : unsigned lru; /* An LRU counter. */
3579 : :
3580 : : public:
3581 : : slurping (elf_in *);
3582 : : ~slurping ();
3583 : :
3584 : : public:
3585 : : /* Close the ELF file, if it's open. */
3586 : 4975 : void close ()
3587 : : {
3588 : 4975 : if (from)
3589 : : {
3590 : 2728 : from->end ();
3591 : 5456 : delete from;
3592 : 2728 : from = NULL;
3593 : : }
3594 : 4975 : }
3595 : :
3596 : : public:
3597 : : void release_macros ();
3598 : :
3599 : : public:
3600 : 2786 : void alloc_remap (unsigned size)
3601 : : {
3602 : 2786 : gcc_assert (!remap);
3603 : 2786 : vec_safe_reserve (remap, size);
3604 : 5923 : for (unsigned ix = size; ix--;)
3605 : 3137 : remap->quick_push (0);
3606 : 2786 : }
3607 : 872877 : unsigned remap_module (unsigned owner)
3608 : : {
3609 : 872877 : if (owner < remap->length ())
3610 : 872877 : return (*remap)[owner] >> 1;
3611 : : return 0;
3612 : : }
3613 : :
3614 : : public:
3615 : : /* GC allocation. But we must explicitly delete it. */
3616 : 2807 : static void *operator new (size_t x)
3617 : : {
3618 : 5614 : return ggc_alloc_atomic (x);
3619 : : }
3620 : 2728 : static void operator delete (void *p)
3621 : : {
3622 : 2728 : ggc_free (p);
3623 : 2728 : }
3624 : : };
3625 : :
3626 : 2807 : slurping::slurping (elf_in *from)
3627 : 2807 : : remap (NULL), from (from),
3628 : 2807 : headers (BITMAP_GGC_ALLOC ()), macro_defs (), macro_tbl (),
3629 : 2807 : loc_deltas (0, 0),
3630 : 2807 : current (~0u), remaining (0), lru (0)
3631 : : {
3632 : 2807 : }
3633 : :
3634 : 2728 : slurping::~slurping ()
3635 : : {
3636 : 2728 : vec_free (remap);
3637 : 2728 : remap = NULL;
3638 : 2728 : release_macros ();
3639 : 2728 : close ();
3640 : 2728 : }
3641 : :
3642 : 4975 : void slurping::release_macros ()
3643 : : {
3644 : 4975 : if (macro_defs.size)
3645 : 868 : elf_in::release (from, macro_defs);
3646 : 4975 : if (macro_tbl.size)
3647 : 0 : elf_in::release (from, macro_tbl);
3648 : 4975 : }
3649 : :
3650 : : /* Flags for extensions that end up being streamed. */
3651 : :
3652 : : enum streamed_extensions {
3653 : : SE_OPENMP_SIMD = 1 << 0,
3654 : : SE_OPENMP = 1 << 1,
3655 : : SE_OPENACC = 1 << 2,
3656 : : SE_BITS = 3
3657 : : };
3658 : :
3659 : : /* Counter indices. */
3660 : : enum module_state_counts
3661 : : {
3662 : : MSC_sec_lwm,
3663 : : MSC_sec_hwm,
3664 : : MSC_pendings,
3665 : : MSC_entities,
3666 : : MSC_namespaces,
3667 : : MSC_using_directives,
3668 : : MSC_bindings,
3669 : : MSC_macros,
3670 : : MSC_inits,
3671 : : MSC_HWM
3672 : : };
3673 : :
3674 : : /********************************************************************/
3675 : : struct module_state_config;
3676 : :
3677 : : /* Increasing levels of loadedness. */
3678 : : enum module_loadedness {
3679 : : ML_NONE, /* Not loaded. */
3680 : : ML_CONFIG, /* Config loaed. */
3681 : : ML_PREPROCESSOR, /* Preprocessor loaded. */
3682 : : ML_LANGUAGE, /* Language loaded. */
3683 : : };
3684 : :
3685 : : /* Increasing levels of directness (toplevel) of import. */
3686 : : enum module_directness {
3687 : : MD_NONE, /* Not direct. */
3688 : : MD_PARTITION_DIRECT, /* Direct import of a partition. */
3689 : : MD_DIRECT, /* Direct import. */
3690 : : MD_PURVIEW_DIRECT, /* direct import in purview. */
3691 : : };
3692 : :
3693 : : /* State of a particular module. */
3694 : :
3695 : : class GTY((chain_next ("%h.parent"), for_user)) module_state {
3696 : : public:
3697 : : /* We always import & export ourselves. */
3698 : : bitmap imports; /* Transitive modules we're importing. */
3699 : : bitmap exports; /* Subset of that, that we're exporting. */
3700 : :
3701 : : /* For a named module interface A.B, parent is A and name is B.
3702 : : For a partition M:P, parent is M and name is P.
3703 : : For an implementation unit I, parent is I's interface and name is NULL.
3704 : : Otherwise parent is NULL and name will be the flatname. */
3705 : : module_state *parent;
3706 : : tree name;
3707 : :
3708 : : slurping *slurp; /* Data for loading. */
3709 : :
3710 : : const char *flatname; /* Flatname of module. */
3711 : : char *filename; /* CMI Filename */
3712 : :
3713 : : /* Indices into the entity_ary. */
3714 : : unsigned entity_lwm;
3715 : : unsigned entity_num;
3716 : :
3717 : : /* Location ranges for this module. adhoc-locs are decomposed, so
3718 : : don't have a range. */
3719 : : loc_range_t GTY((skip)) ordinary_locs;
3720 : : loc_range_t GTY((skip)) macro_locs; // [lwm,num)
3721 : :
3722 : : /* LOC is first set too the importing location. When initially
3723 : : loaded it refers to a module loc whose parent is the importing
3724 : : location. */
3725 : : location_t loc; /* Location referring to module itself. */
3726 : : unsigned crc; /* CRC we saw reading it in. */
3727 : :
3728 : : unsigned mod; /* Module owner number. */
3729 : : unsigned remap; /* Remapping during writing. */
3730 : :
3731 : : unsigned short subst; /* Mangle subst if !0. */
3732 : :
3733 : : /* How loaded this module is. */
3734 : : enum module_loadedness loadedness : 2;
3735 : :
3736 : : bool module_p : 1; /* /The/ module of this TU. */
3737 : : bool header_p : 1; /* Is a header unit. */
3738 : : bool interface_p : 1; /* An interface. */
3739 : : bool partition_p : 1; /* A partition. */
3740 : :
3741 : : /* How directly this module is imported. */
3742 : : enum module_directness directness : 2;
3743 : :
3744 : : bool exported_p : 1; /* directness != MD_NONE && exported. */
3745 : : bool cmi_noted_p : 1; /* We've told the user about the CMI, don't
3746 : : do it again */
3747 : : bool active_init_p : 1; /* This module's global initializer needs
3748 : : calling. */
3749 : : bool inform_cmi_p : 1; /* Inform of a read/write. */
3750 : : bool visited_p : 1; /* A walk-once flag. */
3751 : : /* Record extensions emitted or permitted. */
3752 : : unsigned extensions : SE_BITS;
3753 : : /* 14 bits used, 2 bits remain */
3754 : :
3755 : : public:
3756 : : module_state (tree name, module_state *, bool);
3757 : : ~module_state ();
3758 : :
3759 : : public:
3760 : 2779 : void release ()
3761 : : {
3762 : 2779 : imports = exports = NULL;
3763 : 2779 : slurped ();
3764 : 2728 : }
3765 : 5026 : void slurped ()
3766 : : {
3767 : 5026 : delete slurp;
3768 : 5026 : slurp = NULL;
3769 : 5026 : }
3770 : 1037663 : elf_in *from () const
3771 : : {
3772 : 1037663 : return slurp->from;
3773 : : }
3774 : :
3775 : : public:
3776 : : /* Kind of this module. */
3777 : 115320 : bool is_module () const
3778 : : {
3779 : 115320 : return module_p;
3780 : : }
3781 : 1639019 : bool is_header () const
3782 : : {
3783 : 1639019 : return header_p;
3784 : : }
3785 : 543 : bool is_interface () const
3786 : : {
3787 : 543 : return interface_p;
3788 : : }
3789 : 248105 : bool is_partition () const
3790 : : {
3791 : 248105 : return partition_p;
3792 : : }
3793 : :
3794 : : /* How this module is used in the current TU. */
3795 : 3037 : bool is_exported () const
3796 : : {
3797 : 3037 : return exported_p;
3798 : : }
3799 : 19331 : bool is_direct () const
3800 : : {
3801 : 19331 : return directness >= MD_DIRECT;
3802 : : }
3803 : 257 : bool is_purview_direct () const
3804 : : {
3805 : 257 : return directness == MD_PURVIEW_DIRECT;
3806 : : }
3807 : 385 : bool is_partition_direct () const
3808 : : {
3809 : 385 : return directness == MD_PARTITION_DIRECT;
3810 : : }
3811 : :
3812 : : public:
3813 : : /* Is this a real module? */
3814 : 15080 : bool has_location () const
3815 : : {
3816 : 15080 : return loc != UNKNOWN_LOCATION;
3817 : : }
3818 : :
3819 : : public:
3820 : : bool check_circular_import (location_t loc);
3821 : :
3822 : : public:
3823 : : void mangle (bool include_partition);
3824 : :
3825 : : public:
3826 : : void set_import (module_state const *, bool is_export);
3827 : : void announce (const char *) const;
3828 : :
3829 : : public:
3830 : : /* Read and write module. */
3831 : : bool write_begin (elf_out *to, cpp_reader *,
3832 : : module_state_config &, unsigned &crc);
3833 : : void write_end (elf_out *to, cpp_reader *,
3834 : : module_state_config &, unsigned &crc);
3835 : : bool read_initial (cpp_reader *);
3836 : : bool read_preprocessor (bool);
3837 : : bool read_language (bool);
3838 : :
3839 : : public:
3840 : : /* Read a section. */
3841 : : bool load_section (unsigned snum, binding_slot *mslot);
3842 : : /* Lazily read a section. */
3843 : : bool lazy_load (unsigned index, binding_slot *mslot);
3844 : :
3845 : : public:
3846 : : /* Juggle a limited number of file numbers. */
3847 : : static void freeze_an_elf ();
3848 : : bool maybe_defrost ();
3849 : :
3850 : : public:
3851 : : void maybe_completed_reading ();
3852 : : bool check_read (bool outermost, bool ok);
3853 : :
3854 : : private:
3855 : : /* The README, for human consumption. */
3856 : : void write_readme (elf_out *to, cpp_reader *, const char *dialect);
3857 : : void write_env (elf_out *to);
3858 : :
3859 : : private:
3860 : : /* Import tables. */
3861 : : void write_imports (bytes_out &cfg, bool direct);
3862 : : unsigned read_imports (bytes_in &cfg, cpp_reader *, line_maps *maps);
3863 : :
3864 : : private:
3865 : : void write_imports (elf_out *to, unsigned *crc_ptr);
3866 : : bool read_imports (cpp_reader *, line_maps *);
3867 : :
3868 : : private:
3869 : : void write_partitions (elf_out *to, unsigned, unsigned *crc_ptr);
3870 : : bool read_partitions (unsigned);
3871 : :
3872 : : private:
3873 : : void write_config (elf_out *to, struct module_state_config &, unsigned crc);
3874 : : bool read_config (struct module_state_config &, bool = true);
3875 : : static void write_counts (elf_out *to, unsigned [MSC_HWM], unsigned *crc_ptr);
3876 : : bool read_counts (unsigned *);
3877 : :
3878 : : public:
3879 : : void note_cmi_name ();
3880 : :
3881 : : private:
3882 : : static unsigned write_bindings (elf_out *to, vec<depset *> depsets,
3883 : : unsigned *crc_ptr);
3884 : : bool read_bindings (unsigned count, unsigned lwm, unsigned hwm);
3885 : :
3886 : : static void write_namespace (bytes_out &sec, depset *ns_dep);
3887 : : tree read_namespace (bytes_in &sec);
3888 : :
3889 : : void write_namespaces (elf_out *to, vec<depset *> spaces,
3890 : : unsigned, unsigned *crc_ptr);
3891 : : bool read_namespaces (unsigned);
3892 : :
3893 : : unsigned write_using_directives (elf_out *to, depset::hash &,
3894 : : vec<depset *> spaces, unsigned *crc_ptr);
3895 : : bool read_using_directives (unsigned);
3896 : :
3897 : : void intercluster_seed (trees_out &sec, unsigned index, depset *dep);
3898 : : unsigned write_cluster (elf_out *to, depset *depsets[], unsigned size,
3899 : : depset::hash &, unsigned *counts, unsigned *crc_ptr);
3900 : : bool read_cluster (unsigned snum);
3901 : : bool open_slurp (cpp_reader *);
3902 : :
3903 : : private:
3904 : : unsigned write_inits (elf_out *to, depset::hash &, unsigned *crc_ptr);
3905 : : bool read_inits (unsigned count);
3906 : :
3907 : : private:
3908 : : unsigned write_pendings (elf_out *to, vec<depset *> depsets,
3909 : : depset::hash &, unsigned *crc_ptr);
3910 : : bool read_pendings (unsigned count);
3911 : :
3912 : : private:
3913 : : void write_entities (elf_out *to, vec<depset *> depsets,
3914 : : unsigned count, unsigned *crc_ptr);
3915 : : bool read_entities (unsigned count, unsigned lwm, unsigned hwm);
3916 : :
3917 : : private:
3918 : : void write_init_maps ();
3919 : : range_t write_prepare_maps (module_state_config *, bool);
3920 : : bool read_prepare_maps (const module_state_config *);
3921 : :
3922 : : void write_ordinary_maps (elf_out *to, range_t &,
3923 : : bool, unsigned *crc_ptr);
3924 : : bool read_ordinary_maps (line_map_uint_t, unsigned);
3925 : : void write_macro_maps (elf_out *to, range_t &, unsigned *crc_ptr);
3926 : : bool read_macro_maps (line_map_uint_t);
3927 : :
3928 : : void write_diagnostic_classification (elf_out *, diagnostics::context *,
3929 : : unsigned *);
3930 : : bool read_diagnostic_classification (diagnostics::context *);
3931 : :
3932 : : private:
3933 : : void write_define (bytes_out &, const cpp_macro *);
3934 : : cpp_macro *read_define (bytes_in &, cpp_reader *) const;
3935 : : vec<cpp_hashnode *> *prepare_macros (cpp_reader *);
3936 : : unsigned write_macros (elf_out *to, vec<cpp_hashnode *> *, unsigned *crc_ptr);
3937 : : bool read_macros ();
3938 : : void install_macros ();
3939 : :
3940 : : public:
3941 : : void import_macros ();
3942 : :
3943 : : public:
3944 : : static void undef_macro (cpp_reader *, location_t, cpp_hashnode *);
3945 : : static cpp_macro *deferred_macro (cpp_reader *, location_t, cpp_hashnode *);
3946 : :
3947 : : public:
3948 : : static bool note_location (location_t);
3949 : : static void write_location (bytes_out &, location_t);
3950 : : location_t read_location (bytes_in &) const;
3951 : :
3952 : : public:
3953 : : void set_flatname ();
3954 : 44591 : const char *get_flatname () const
3955 : : {
3956 : 44591 : return flatname;
3957 : : }
3958 : : location_t imported_from () const;
3959 : :
3960 : : public:
3961 : : void set_filename (const Cody::Packet &);
3962 : : bool do_import (cpp_reader *, bool outermost);
3963 : : bool check_importable (cpp_reader *);
3964 : : };
3965 : :
3966 : : /* Hash module state by name. This cannot be a member of
3967 : : module_state, because of GTY restrictions. We never delete from
3968 : : the hash table, but ggc_ptr_hash doesn't support that
3969 : : simplification. */
3970 : :
3971 : : struct module_state_hash : ggc_ptr_hash<module_state> {
3972 : : typedef std::pair<tree,uintptr_t> compare_type; /* {name,parent} */
3973 : :
3974 : : static inline hashval_t hash (const value_type m);
3975 : : static inline hashval_t hash (const compare_type &n);
3976 : : static inline bool equal (const value_type existing,
3977 : : const compare_type &candidate);
3978 : : };
3979 : :
3980 : 10787 : module_state::module_state (tree name, module_state *parent, bool partition)
3981 : 10787 : : imports (BITMAP_GGC_ALLOC ()), exports (BITMAP_GGC_ALLOC ()),
3982 : 10787 : parent (parent), name (name), slurp (NULL),
3983 : 10787 : flatname (NULL), filename (NULL),
3984 : 10787 : entity_lwm (~0u >> 1), entity_num (0),
3985 : 10787 : ordinary_locs (0, 0), macro_locs (0, 0),
3986 : 10787 : loc (UNKNOWN_LOCATION),
3987 : 10787 : crc (0), mod (MODULE_UNKNOWN), remap (0), subst (0)
3988 : : {
3989 : 10787 : loadedness = ML_NONE;
3990 : :
3991 : 10787 : module_p = header_p = interface_p = partition_p = false;
3992 : :
3993 : 10787 : directness = MD_NONE;
3994 : 10787 : exported_p = false;
3995 : :
3996 : 10787 : cmi_noted_p = false;
3997 : 10787 : active_init_p = false;
3998 : :
3999 : 10787 : partition_p = partition;
4000 : :
4001 : 10787 : inform_cmi_p = false;
4002 : 10787 : visited_p = false;
4003 : :
4004 : 10787 : extensions = 0;
4005 : 10787 : if (name && TREE_CODE (name) == STRING_CST)
4006 : : {
4007 : 1861 : header_p = true;
4008 : :
4009 : 1861 : const char *string = TREE_STRING_POINTER (name);
4010 : 1861 : gcc_checking_assert (string[0] == '.'
4011 : : ? IS_DIR_SEPARATOR (string[1])
4012 : : : IS_ABSOLUTE_PATH (string));
4013 : : }
4014 : :
4015 : 10787 : gcc_checking_assert (!(parent && header_p));
4016 : 10787 : }
4017 : :
4018 : 51 : module_state::~module_state ()
4019 : : {
4020 : 51 : release ();
4021 : 51 : }
4022 : :
4023 : : /* Hash module state. */
4024 : : static hashval_t
4025 : 15638 : module_name_hash (const_tree name)
4026 : : {
4027 : 15638 : if (TREE_CODE (name) == STRING_CST)
4028 : 3431 : return htab_hash_string (TREE_STRING_POINTER (name));
4029 : : else
4030 : 12207 : return IDENTIFIER_HASH_VALUE (name);
4031 : : }
4032 : :
4033 : : hashval_t
4034 : 4254 : module_state_hash::hash (const value_type m)
4035 : : {
4036 : 4254 : hashval_t ph = pointer_hash<void>::hash
4037 : 4254 : (reinterpret_cast<void *> (reinterpret_cast<uintptr_t> (m->parent)
4038 : 4254 : | m->is_partition ()));
4039 : 4254 : hashval_t nh = module_name_hash (m->name);
4040 : 4254 : return iterative_hash_hashval_t (ph, nh);
4041 : : }
4042 : :
4043 : : /* Hash a name. */
4044 : : hashval_t
4045 : 11384 : module_state_hash::hash (const compare_type &c)
4046 : : {
4047 : 11384 : hashval_t ph = pointer_hash<void>::hash (reinterpret_cast<void *> (c.second));
4048 : 11384 : hashval_t nh = module_name_hash (c.first);
4049 : :
4050 : 11384 : return iterative_hash_hashval_t (ph, nh);
4051 : : }
4052 : :
4053 : : bool
4054 : 7686 : module_state_hash::equal (const value_type existing,
4055 : : const compare_type &candidate)
4056 : : {
4057 : 7686 : uintptr_t ep = (reinterpret_cast<uintptr_t> (existing->parent)
4058 : 7686 : | existing->is_partition ());
4059 : 7686 : if (ep != candidate.second)
4060 : : return false;
4061 : :
4062 : : /* Identifier comparison is by pointer. If the string_csts happen
4063 : : to be the same object, then they're equal too. */
4064 : 6389 : if (existing->name == candidate.first)
4065 : : return true;
4066 : :
4067 : : /* If neither are string csts, they can't be equal. */
4068 : 1329 : if (TREE_CODE (candidate.first) != STRING_CST
4069 : 483 : || TREE_CODE (existing->name) != STRING_CST)
4070 : : return false;
4071 : :
4072 : : /* String equality. */
4073 : 417 : if (TREE_STRING_LENGTH (existing->name)
4074 : 417 : == TREE_STRING_LENGTH (candidate.first)
4075 : 417 : && !memcmp (TREE_STRING_POINTER (existing->name),
4076 : 414 : TREE_STRING_POINTER (candidate.first),
4077 : 414 : TREE_STRING_LENGTH (existing->name)))
4078 : 131 : return true;
4079 : :
4080 : : return false;
4081 : : }
4082 : :
4083 : : /********************************************************************/
4084 : : /* Global state */
4085 : :
4086 : : /* Mapper name. */
4087 : : static const char *module_mapper_name;
4088 : :
4089 : : /* Deferred import queue (FIFO). */
4090 : : static vec<module_state *, va_heap, vl_embed> *pending_imports;
4091 : :
4092 : : /* CMI repository path and workspace. */
4093 : : static char *cmi_repo;
4094 : : static size_t cmi_repo_length;
4095 : : static char *cmi_path;
4096 : : static size_t cmi_path_alloc;
4097 : :
4098 : : /* Count of available and loaded clusters. */
4099 : : static unsigned available_clusters;
4100 : : static unsigned loaded_clusters;
4101 : :
4102 : : /* What the current TU is. */
4103 : : unsigned module_kind;
4104 : :
4105 : : /* Global trees. */
4106 : : static const std::pair<tree *, unsigned> global_tree_arys[] =
4107 : : {
4108 : : std::pair<tree *, unsigned> (sizetype_tab, stk_type_kind_last),
4109 : : std::pair<tree *, unsigned> (integer_types, itk_none),
4110 : : std::pair<tree *, unsigned> (global_trees, TI_MODULE_HWM),
4111 : : std::pair<tree *, unsigned> (c_global_trees, CTI_MODULE_HWM),
4112 : : std::pair<tree *, unsigned> (cp_global_trees, CPTI_MODULE_HWM),
4113 : : std::pair<tree *, unsigned> (NULL, 0)
4114 : : };
4115 : : static GTY(()) vec<tree, va_gc> *fixed_trees;
4116 : : static unsigned global_crc;
4117 : :
4118 : : /* Lazy loading can open many files concurrently, there are
4119 : : per-process limits on that. We pay attention to the process limit,
4120 : : and attempt to increase it when we run out. Otherwise we use an
4121 : : LRU scheme to figure out who to flush. Note that if the import
4122 : : graph /depth/ exceeds lazy_limit, we'll exceed the limit. */
4123 : : static unsigned lazy_lru; /* LRU counter. */
4124 : : static unsigned lazy_open; /* Number of open modules */
4125 : : static unsigned lazy_limit; /* Current limit of open modules. */
4126 : : static unsigned lazy_hard_limit; /* Hard limit on open modules. */
4127 : : /* Account for source, assembler and dump files & directory searches.
4128 : : We don't keep the source file's open, so we don't have to account
4129 : : for #include depth. I think dump files are opened and closed per
4130 : : pass, but ICBW. */
4131 : : #define LAZY_HEADROOM 15 /* File descriptor headroom. */
4132 : :
4133 : : /* Vector of module state. Indexed by OWNER. Index 0 is reserved for the
4134 : : current TU; imports start at 1. */
4135 : : static GTY(()) vec<module_state *, va_gc> *modules;
4136 : :
4137 : : /* Get the module state for the current TU's module. */
4138 : :
4139 : : static module_state *
4140 : 189333 : this_module() {
4141 : 189333 : return (*modules)[0];
4142 : : }
4143 : :
4144 : : /* Hash of module state, findable by {name, parent}. */
4145 : : static GTY(()) hash_table<module_state_hash> *modules_hash;
4146 : :
4147 : : /* Map of imported entities. We map DECL_UID to index of entity
4148 : : vector. */
4149 : : typedef hash_map<unsigned/*UID*/, unsigned/*index*/,
4150 : : simple_hashmap_traits<int_hash<unsigned,0>, unsigned>
4151 : : > entity_map_t;
4152 : : static entity_map_t *entity_map;
4153 : : /* Doesn't need GTYing, because any tree referenced here is also
4154 : : findable by, symbol table, specialization table, return type of
4155 : : reachable function. */
4156 : : static vec<binding_slot, va_heap, vl_embed> *entity_ary;
4157 : :
4158 : : /* Members entities of imported classes that are defined in this TU.
4159 : : These are where the entity's context is not from the current TU.
4160 : : We need to emit the definition (but not the enclosing class).
4161 : :
4162 : : We could find these by walking ALL the imported classes that we
4163 : : could provide a member definition. But that's expensive,
4164 : : especially when you consider lazy implicit member declarations,
4165 : : which could be ANY imported class. */
4166 : : static GTY(()) vec<tree, va_gc> *class_members;
4167 : :
4168 : : /* The same problem exists for class template partial
4169 : : specializations. Now that we have constraints, the invariant of
4170 : : expecting them in the instantiation table no longer holds. One of
4171 : : the constrained partial specializations will be there, but the
4172 : : others not so much. It's not even an unconstrained partial
4173 : : spacialization in the table :( so any partial template declaration
4174 : : is added to this list too. */
4175 : : static GTY(()) vec<tree, va_gc> *partial_specializations;
4176 : :
4177 : : /********************************************************************/
4178 : :
4179 : : /* Our module mapper (created lazily). */
4180 : : module_client *mapper;
4181 : :
4182 : : static module_client *make_mapper (location_t loc, class mkdeps *deps);
4183 : 30880 : inline module_client *get_mapper (location_t loc, class mkdeps *deps)
4184 : : {
4185 : 30880 : auto *res = mapper;
4186 : 290 : if (!res)
4187 : 4594 : res = make_mapper (loc, deps);
4188 : 30880 : return res;
4189 : : }
4190 : :
4191 : : /********************************************************************/
4192 : : static tree
4193 : 254861 : get_clone_target (tree decl)
4194 : : {
4195 : 254861 : tree target;
4196 : :
4197 : 254861 : if (TREE_CODE (decl) == TEMPLATE_DECL)
4198 : : {
4199 : 28260 : tree res_orig = DECL_CLONED_FUNCTION (DECL_TEMPLATE_RESULT (decl));
4200 : :
4201 : 28260 : target = DECL_TI_TEMPLATE (res_orig);
4202 : : }
4203 : : else
4204 : 226601 : target = DECL_CLONED_FUNCTION (decl);
4205 : :
4206 : 254861 : gcc_checking_assert (DECL_MAYBE_IN_CHARGE_CDTOR_P (target));
4207 : :
4208 : 254861 : return target;
4209 : : }
4210 : :
4211 : : /* Like FOR_EACH_CLONE, but will walk cloned templates. */
4212 : : #define FOR_EVERY_CLONE(CLONE, FN) \
4213 : : if (!DECL_MAYBE_IN_CHARGE_CDTOR_P (FN)); \
4214 : : else \
4215 : : for (CLONE = DECL_CHAIN (FN); \
4216 : : CLONE && DECL_CLONED_FUNCTION_P (CLONE); \
4217 : : CLONE = DECL_CHAIN (CLONE))
4218 : :
4219 : : /* It'd be nice if USE_TEMPLATE was a field of template_info
4220 : : (a) it'd solve the enum case dealt with below,
4221 : : (b) both class templates and decl templates would store this in the
4222 : : same place
4223 : : (c) this function wouldn't need the by-ref arg, which is annoying. */
4224 : :
4225 : : static tree
4226 : 180885150 : node_template_info (tree decl, int &use)
4227 : : {
4228 : 180885150 : tree ti = NULL_TREE;
4229 : 180885150 : int use_tpl = -1;
4230 : 180885150 : if (DECL_IMPLICIT_TYPEDEF_P (decl))
4231 : : {
4232 : 19049150 : tree type = TREE_TYPE (decl);
4233 : :
4234 : 19049150 : ti = TYPE_TEMPLATE_INFO (type);
4235 : 19049150 : if (ti)
4236 : : {
4237 : 3423131 : if (TYPE_LANG_SPECIFIC (type))
4238 : 3415803 : use_tpl = CLASSTYPE_USE_TEMPLATE (type);
4239 : : else
4240 : : {
4241 : : /* An enum, where we don't explicitly encode use_tpl.
4242 : : If the containing context (a type or a function), is
4243 : : an ({im,ex}plicit) instantiation, then this is too.
4244 : : If it's a partial or explicit specialization, then
4245 : : this is not!. */
4246 : 7328 : tree ctx = CP_DECL_CONTEXT (decl);
4247 : 7328 : if (TYPE_P (ctx))
4248 : 7215 : ctx = TYPE_NAME (ctx);
4249 : 7328 : node_template_info (ctx, use);
4250 : 7328 : use_tpl = use != 2 ? use : 0;
4251 : : }
4252 : : }
4253 : : }
4254 : 161836000 : else if (DECL_LANG_SPECIFIC (decl)
4255 : 161836000 : && (VAR_P (decl)
4256 : : || TREE_CODE (decl) == TYPE_DECL
4257 : : || TREE_CODE (decl) == FUNCTION_DECL
4258 : : || TREE_CODE (decl) == FIELD_DECL
4259 : : || TREE_CODE (decl) == CONCEPT_DECL
4260 : : || TREE_CODE (decl) == TEMPLATE_DECL))
4261 : : {
4262 : 85981428 : use_tpl = DECL_USE_TEMPLATE (decl);
4263 : 85981428 : ti = DECL_TEMPLATE_INFO (decl);
4264 : : }
4265 : :
4266 : 180885150 : use = use_tpl;
4267 : 180885150 : return ti;
4268 : : }
4269 : :
4270 : : /* Find the index in entity_ary for an imported DECL. It should
4271 : : always be there, but bugs can cause it to be missing, and that can
4272 : : crash the crash reporting -- let's not do that! When streaming
4273 : : out we place entities from this module there too -- with negated
4274 : : indices. */
4275 : :
4276 : : static unsigned
4277 : 1014203 : import_entity_index (tree decl, bool null_ok = false)
4278 : : {
4279 : 1014203 : if (unsigned *slot = entity_map->get (DECL_UID (decl)))
4280 : 1014158 : return *slot;
4281 : :
4282 : 45 : gcc_checking_assert (null_ok);
4283 : : return ~(~0u >> 1);
4284 : : }
4285 : :
4286 : : /* Find the module for an imported entity at INDEX in the entity ary.
4287 : : There must be one. */
4288 : :
4289 : : static module_state *
4290 : 29082 : import_entity_module (unsigned index)
4291 : : {
4292 : 29082 : if (index > ~(~0u >> 1))
4293 : : /* This is an index for an exported entity. */
4294 : 21 : return this_module ();
4295 : :
4296 : : /* Do not include the current TU (not an off-by-one error). */
4297 : 29061 : unsigned pos = 1;
4298 : 29061 : unsigned len = modules->length () - pos;
4299 : 66102 : while (len)
4300 : : {
4301 : 37041 : unsigned half = len / 2;
4302 : 37041 : module_state *probe = (*modules)[pos + half];
4303 : 37041 : if (index < probe->entity_lwm)
4304 : : len = half;
4305 : 29179 : else if (index < probe->entity_lwm + probe->entity_num)
4306 : : return probe;
4307 : : else
4308 : : {
4309 : 118 : pos += half + 1;
4310 : 118 : len = len - (half + 1);
4311 : : }
4312 : : }
4313 : 0 : gcc_unreachable ();
4314 : : }
4315 : :
4316 : :
4317 : : /********************************************************************/
4318 : : /* A dumping machinery. */
4319 : :
4320 : : class dumper {
4321 : : public:
4322 : : enum {
4323 : : LOCATION = TDF_LINENO, /* -lineno:Source location streaming. */
4324 : : DEPEND = TDF_GRAPH, /* -graph:Dependency graph construction. */
4325 : : CLUSTER = TDF_BLOCKS, /* -blocks:Clusters. */
4326 : : TREE = TDF_UID, /* -uid:Tree streaming. */
4327 : : MERGE = TDF_ALIAS, /* -alias:Mergeable Entities. */
4328 : : ELF = TDF_ASMNAME, /* -asmname:Elf data. */
4329 : : MACRO = TDF_VOPS /* -vops:Macros. */
4330 : : };
4331 : :
4332 : : private:
4333 : : struct impl {
4334 : : typedef vec<module_state *, va_heap, vl_embed> stack_t;
4335 : :
4336 : : FILE *stream; /* Dump stream. */
4337 : : unsigned indent; /* Local indentation. */
4338 : : bool bol; /* Beginning of line. */
4339 : : stack_t stack; /* Trailing array of module_state. */
4340 : :
4341 : : bool nested_name (tree); /* Dump a name following DECL_CONTEXT. */
4342 : : };
4343 : :
4344 : : public:
4345 : : /* The dumper. */
4346 : : impl *dumps;
4347 : : dump_flags_t flags;
4348 : :
4349 : : public:
4350 : : /* Push/pop module state dumping. */
4351 : : unsigned push (module_state *);
4352 : : void pop (unsigned);
4353 : :
4354 : : public:
4355 : : /* Change local indentation. */
4356 : 276723455 : void indent ()
4357 : : {
4358 : 309 : if (dumps)
4359 : 534353 : dumps->indent++;
4360 : : }
4361 : 276723455 : void outdent ()
4362 : : {
4363 : 276723455 : if (dumps)
4364 : : {
4365 : 534353 : gcc_checking_assert (dumps->indent);
4366 : 534353 : dumps->indent--;
4367 : : }
4368 : 276723455 : }
4369 : :
4370 : : public:
4371 : : /* Is dump enabled?. */
4372 : 171810674 : bool operator () (int mask = 0)
4373 : : {
4374 : 3569109 : if (!dumps || !dumps->stream)
4375 : : return false;
4376 : 405377 : if (mask && !(mask & flags))
4377 : 3932 : return false;
4378 : : return true;
4379 : : }
4380 : : /* Dump some information. */
4381 : : bool operator () (const char *, ...);
4382 : : };
4383 : :
4384 : : /* The dumper. */
4385 : : static dumper dump = {0, dump_flags_t (0)};
4386 : :
4387 : : /* Push to dumping M. Return previous indentation level. */
4388 : :
4389 : : unsigned
4390 : 89200 : dumper::push (module_state *m)
4391 : : {
4392 : 89200 : FILE *stream = NULL;
4393 : 89200 : if (!dumps || !dumps->stack.length ())
4394 : : {
4395 : 88014 : stream = dump_begin (module_dump_id, &flags);
4396 : 88014 : if (!stream)
4397 : : return 0;
4398 : : }
4399 : :
4400 : 6992 : if (!dumps || !dumps->stack.space (1))
4401 : : {
4402 : : /* Create or extend the dump implementor. */
4403 : 1155 : unsigned current = dumps ? dumps->stack.length () : 0;
4404 : 611 : unsigned count = current ? current * 2 : EXPERIMENT (1, 20);
4405 : 1155 : size_t alloc = (offsetof (impl, stack)
4406 : 1155 : + impl::stack_t::embedded_size (count));
4407 : 1155 : dumps = XRESIZEVAR (impl, dumps, alloc);
4408 : 1155 : dumps->stack.embedded_init (count, current);
4409 : : }
4410 : 6992 : if (stream)
4411 : 5806 : dumps->stream = stream;
4412 : :
4413 : 6992 : unsigned n = dumps->indent;
4414 : 6992 : dumps->indent = 0;
4415 : 6992 : dumps->bol = true;
4416 : 6992 : dumps->stack.quick_push (m);
4417 : 6992 : if (m)
4418 : : {
4419 : 1892 : module_state *from = NULL;
4420 : :
4421 : 1892 : if (dumps->stack.length () > 1)
4422 : 606 : from = dumps->stack[dumps->stack.length () - 2];
4423 : : else
4424 : 1286 : dump ("");
4425 : 3419 : dump (from ? "Starting module %M (from %M)"
4426 : : : "Starting module %M", m, from);
4427 : : }
4428 : :
4429 : : return n;
4430 : : }
4431 : :
4432 : : /* Pop from dumping. Restore indentation to N. */
4433 : :
4434 : 89157 : void dumper::pop (unsigned n)
4435 : : {
4436 : 89157 : if (!dumps)
4437 : : return;
4438 : :
4439 : 13984 : gcc_checking_assert (dump () && !dumps->indent);
4440 : 6992 : if (module_state *m = dumps->stack[dumps->stack.length () - 1])
4441 : : {
4442 : 1892 : module_state *from = (dumps->stack.length () > 1
4443 : 1892 : ? dumps->stack[dumps->stack.length () - 2] : NULL);
4444 : 2133 : dump (from ? "Finishing module %M (returning to %M)"
4445 : : : "Finishing module %M", m, from);
4446 : : }
4447 : 6992 : dumps->stack.pop ();
4448 : 6992 : dumps->indent = n;
4449 : 6992 : if (!dumps->stack.length ())
4450 : : {
4451 : 5806 : dump_end (module_dump_id, dumps->stream);
4452 : 5806 : dumps->stream = NULL;
4453 : : }
4454 : : }
4455 : :
4456 : : /* Dump a nested name for arbitrary tree T. Sometimes it won't have a
4457 : : name. */
4458 : :
4459 : : bool
4460 : 503909 : dumper::impl::nested_name (tree t)
4461 : : {
4462 : 503909 : tree ti = NULL_TREE;
4463 : 503909 : int origin = -1;
4464 : 503909 : tree name = NULL_TREE;
4465 : :
4466 : 503909 : if (t && TREE_CODE (t) == TU_LOCAL_ENTITY)
4467 : 0 : t = TU_LOCAL_ENTITY_NAME (t);
4468 : :
4469 : 503879 : if (t && TREE_CODE (t) == TREE_BINFO)
4470 : 384 : t = BINFO_TYPE (t);
4471 : :
4472 : 503909 : if (t && TYPE_P (t))
4473 : 250683 : t = TYPE_NAME (t);
4474 : :
4475 : 503867 : if (t && DECL_P (t))
4476 : : {
4477 : 421449 : if (t == global_namespace || DECL_TEMPLATE_PARM_P (t))
4478 : : ;
4479 : 390203 : else if (tree ctx = DECL_CONTEXT (t))
4480 : 299261 : if (TREE_CODE (ctx) == TRANSLATION_UNIT_DECL
4481 : 299261 : || nested_name (ctx))
4482 : 299261 : fputs ("::", stream);
4483 : :
4484 : 421449 : int use_tpl;
4485 : 421449 : ti = node_template_info (t, use_tpl);
4486 : 137187 : if (ti && TREE_CODE (TI_TEMPLATE (ti)) == TEMPLATE_DECL
4487 : 558594 : && (DECL_TEMPLATE_RESULT (TI_TEMPLATE (ti)) == t))
4488 : : t = TI_TEMPLATE (ti);
4489 : 421449 : tree not_tmpl = t;
4490 : 421449 : if (TREE_CODE (t) == TEMPLATE_DECL)
4491 : : {
4492 : 22563 : fputs ("template ", stream);
4493 : 22563 : not_tmpl = DECL_TEMPLATE_RESULT (t);
4494 : : }
4495 : :
4496 : 22563 : if (not_tmpl
4497 : 421445 : && DECL_P (not_tmpl)
4498 : 421445 : && DECL_LANG_SPECIFIC (not_tmpl)
4499 : 244908 : && DECL_MODULE_IMPORT_P (not_tmpl))
4500 : : {
4501 : : /* We need to be careful here, so as to not explode on
4502 : : inconsistent data -- we're probably debugging, because
4503 : : Something Is Wrong. */
4504 : 18248 : unsigned index = import_entity_index (t, true);
4505 : 18248 : if (!(index & ~(~0u >> 1)))
4506 : 17690 : origin = import_entity_module (index)->mod;
4507 : 558 : else if (index > ~(~0u >> 1))
4508 : : /* An imported partition member that we're emitting. */
4509 : : origin = 0;
4510 : : else
4511 : 45 : origin = -2;
4512 : : }
4513 : :
4514 : 424523 : name = DECL_NAME (t) ? DECL_NAME (t)
4515 : 4147 : : HAS_DECL_ASSEMBLER_NAME_P (t) ? DECL_ASSEMBLER_NAME_RAW (t)
4516 : : : NULL_TREE;
4517 : : }
4518 : : else
4519 : : name = t;
4520 : :
4521 : 421449 : if (name)
4522 : 469127 : switch (TREE_CODE (name))
4523 : : {
4524 : 13485 : default:
4525 : 13485 : fputs ("#unnamed#", stream);
4526 : 13485 : break;
4527 : :
4528 : 431337 : case IDENTIFIER_NODE:
4529 : 431337 : fwrite (IDENTIFIER_POINTER (name), 1, IDENTIFIER_LENGTH (name), stream);
4530 : 431337 : break;
4531 : :
4532 : 24213 : case INTEGER_CST:
4533 : 24213 : print_hex (wi::to_wide (name), stream);
4534 : 24213 : break;
4535 : :
4536 : 92 : case STRING_CST:
4537 : : /* If TREE_TYPE is NULL, this is a raw string. */
4538 : 184 : fwrite (TREE_STRING_POINTER (name), 1,
4539 : 92 : TREE_STRING_LENGTH (name) - (TREE_TYPE (name) != NULL_TREE),
4540 : : stream);
4541 : 92 : break;
4542 : : }
4543 : : else
4544 : 34782 : fputs ("#null#", stream);
4545 : :
4546 : 503909 : if (origin >= 0)
4547 : : {
4548 : 18203 : const module_state *module = (*modules)[origin];
4549 : 36406 : fprintf (stream, "@%s:%d", !module ? "" : !module->name ? "(unnamed)"
4550 : 18203 : : module->get_flatname (), origin);
4551 : : }
4552 : 485706 : else if (origin == -2)
4553 : 45 : fprintf (stream, "@???");
4554 : :
4555 : 503909 : if (ti)
4556 : : {
4557 : 137187 : tree args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (ti));
4558 : 137187 : fputs ("<", stream);
4559 : 137187 : if (args)
4560 : 347598 : for (int ix = 0; ix != TREE_VEC_LENGTH (args); ix++)
4561 : : {
4562 : 210411 : if (ix)
4563 : 73224 : fputs (",", stream);
4564 : 210411 : nested_name (TREE_VEC_ELT (args, ix));
4565 : : }
4566 : 137187 : fputs (">", stream);
4567 : : }
4568 : :
4569 : 503909 : return true;
4570 : : }
4571 : :
4572 : : /* Formatted dumping. FORMAT begins with '+' do not emit a trailing
4573 : : new line. (Normally it is appended.)
4574 : : Escapes:
4575 : : %C - tree_code
4576 : : %I - identifier
4577 : : %K - location_t or line_map_uint_t
4578 : : %M - module_state
4579 : : %N - name -- DECL_NAME
4580 : : %P - context:name pair
4581 : : %R - unsigned:unsigned ratio
4582 : : %S - symbol -- DECL_ASSEMBLER_NAME
4583 : : %U - long unsigned
4584 : : %V - version
4585 : : --- the following are printf-like, but without its flexibility
4586 : : %d - decimal int
4587 : : %p - pointer
4588 : : %s - string
4589 : : %u - unsigned int
4590 : : %x - hex int
4591 : :
4592 : : We do not implement the printf modifiers. */
4593 : :
4594 : : bool
4595 : 418441 : dumper::operator () (const char *format, ...)
4596 : : {
4597 : 418441 : if (!(*this) ())
4598 : : return false;
4599 : :
4600 : 359032 : bool no_nl = format[0] == '+';
4601 : 359032 : format += no_nl;
4602 : :
4603 : 359032 : if (dumps->bol)
4604 : : {
4605 : : /* Module import indent. */
4606 : 186584 : if (unsigned depth = dumps->stack.length () - 1)
4607 : : {
4608 : 21722 : const char *prefix = ">>>>";
4609 : 43426 : fprintf (dumps->stream, (depth <= strlen (prefix)
4610 : 21704 : ? &prefix[strlen (prefix) - depth]
4611 : : : ">.%d.>"), depth);
4612 : : }
4613 : :
4614 : : /* Local indent. */
4615 : 186584 : if (unsigned indent = dumps->indent)
4616 : : {
4617 : 98495 : const char *prefix = " ";
4618 : 192040 : fprintf (dumps->stream, (indent <= strlen (prefix)
4619 : 93545 : ? &prefix[strlen (prefix) - indent]
4620 : : : " .%d. "), indent);
4621 : : }
4622 : 186584 : dumps->bol = false;
4623 : : }
4624 : :
4625 : 359032 : va_list args;
4626 : 359032 : va_start (args, format);
4627 : 1056845 : while (const char *esc = strchr (format, '%'))
4628 : : {
4629 : 697813 : fwrite (format, 1, (size_t)(esc - format), dumps->stream);
4630 : 697813 : format = ++esc;
4631 : 697813 : switch (*format++)
4632 : : {
4633 : 0 : default:
4634 : 0 : gcc_unreachable ();
4635 : :
4636 : 544 : case '%':
4637 : 544 : fputc ('%', dumps->stream);
4638 : 544 : break;
4639 : :
4640 : 108299 : case 'C': /* Code */
4641 : 108299 : {
4642 : 108299 : tree_code code = (tree_code)va_arg (args, unsigned);
4643 : 108299 : fputs (get_tree_code_name (code), dumps->stream);
4644 : : }
4645 : 108299 : break;
4646 : :
4647 : 81 : case 'I': /* Identifier. */
4648 : 81 : {
4649 : 81 : tree t = va_arg (args, tree);
4650 : 81 : dumps->nested_name (t);
4651 : : }
4652 : 81 : break;
4653 : :
4654 : 4296 : case 'K': /* location_t, either 32- or 64-bit. */
4655 : 4296 : {
4656 : 4296 : unsigned long long u = va_arg (args, location_t);
4657 : 4296 : fprintf (dumps->stream, "%llu", u);
4658 : : }
4659 : 4296 : break;
4660 : :
4661 : 7248 : case 'M': /* Module. */
4662 : 7248 : {
4663 : 7248 : const char *str = "(none)";
4664 : 7248 : if (module_state *m = va_arg (args, module_state *))
4665 : : {
4666 : 7248 : if (!m->has_location ())
4667 : : str = "(detached)";
4668 : : else
4669 : 7248 : str = m->get_flatname ();
4670 : : }
4671 : 7248 : fputs (str, dumps->stream);
4672 : : }
4673 : 7248 : break;
4674 : :
4675 : 118912 : case 'N': /* Name. */
4676 : 118912 : {
4677 : 118912 : tree t = va_arg (args, tree);
4678 : 238313 : while (t && TREE_CODE (t) == OVERLOAD)
4679 : 489 : t = OVL_FUNCTION (t);
4680 : 118912 : fputc ('\'', dumps->stream);
4681 : 118912 : dumps->nested_name (t);
4682 : 118912 : fputc ('\'', dumps->stream);
4683 : : }
4684 : 118912 : break;
4685 : :
4686 : 6829 : case 'P': /* Pair. */
4687 : 6829 : {
4688 : 6829 : tree ctx = va_arg (args, tree);
4689 : 6829 : tree name = va_arg (args, tree);
4690 : 6829 : fputc ('\'', dumps->stream);
4691 : 6829 : dumps->nested_name (ctx);
4692 : 6829 : if (ctx && ctx != global_namespace)
4693 : 855 : fputs ("::", dumps->stream);
4694 : 6829 : dumps->nested_name (name);
4695 : 6829 : fputc ('\'', dumps->stream);
4696 : : }
4697 : 6829 : break;
4698 : :
4699 : 819 : case 'R': /* Ratio */
4700 : 819 : {
4701 : 819 : unsigned a = va_arg (args, unsigned);
4702 : 819 : unsigned b = va_arg (args, unsigned);
4703 : 819 : fprintf (dumps->stream, "%.1f", (float) a / (b + !b));
4704 : : }
4705 : 819 : break;
4706 : :
4707 : 34701 : case 'S': /* Symbol name */
4708 : 34701 : {
4709 : 34701 : tree t = va_arg (args, tree);
4710 : 34701 : if (t && TYPE_P (t))
4711 : 12631 : t = TYPE_NAME (t);
4712 : 32921 : if (t && HAS_DECL_ASSEMBLER_NAME_P (t)
4713 : 31744 : && DECL_ASSEMBLER_NAME_SET_P (t))
4714 : : {
4715 : 169 : fputc ('(', dumps->stream);
4716 : 169 : fputs (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (t)),
4717 : 169 : dumps->stream);
4718 : 169 : fputc (')', dumps->stream);
4719 : : }
4720 : : }
4721 : : break;
4722 : :
4723 : 0 : case 'U': /* long unsigned. */
4724 : 0 : {
4725 : 0 : unsigned long u = va_arg (args, unsigned long);
4726 : 0 : fprintf (dumps->stream, "%lu", u);
4727 : : }
4728 : 0 : break;
4729 : :
4730 : 766 : case 'V': /* Version. */
4731 : 766 : {
4732 : 766 : unsigned v = va_arg (args, unsigned);
4733 : 766 : verstr_t string;
4734 : :
4735 : 766 : version2string (v, string);
4736 : 766 : fputs (string, dumps->stream);
4737 : : }
4738 : 766 : break;
4739 : :
4740 : 0 : case 'c': /* Character. */
4741 : 0 : {
4742 : 0 : int c = va_arg (args, int);
4743 : 0 : fputc (c, dumps->stream);
4744 : : }
4745 : 0 : break;
4746 : :
4747 : 61469 : case 'd': /* Decimal Int. */
4748 : 61469 : {
4749 : 61469 : int d = va_arg (args, int);
4750 : 61469 : fprintf (dumps->stream, "%d", d);
4751 : : }
4752 : 61469 : break;
4753 : :
4754 : 0 : case 'p': /* Pointer. */
4755 : 0 : {
4756 : 0 : void *p = va_arg (args, void *);
4757 : 0 : fprintf (dumps->stream, "%p", p);
4758 : : }
4759 : 0 : break;
4760 : :
4761 : 121928 : case 's': /* String. */
4762 : 121928 : {
4763 : 121928 : const char *s = va_arg (args, char *);
4764 : 121928 : gcc_checking_assert (s);
4765 : 121928 : fputs (s, dumps->stream);
4766 : : }
4767 : 121928 : break;
4768 : :
4769 : 229461 : case 'u': /* Unsigned. */
4770 : 229461 : {
4771 : 229461 : unsigned u = va_arg (args, unsigned);
4772 : 229461 : fprintf (dumps->stream, "%u", u);
4773 : : }
4774 : 229461 : break;
4775 : :
4776 : 2460 : case 'x': /* Hex. */
4777 : 2460 : {
4778 : 2460 : unsigned x = va_arg (args, unsigned);
4779 : 2460 : fprintf (dumps->stream, "%x", x);
4780 : : }
4781 : 2460 : break;
4782 : : }
4783 : : }
4784 : 359032 : fputs (format, dumps->stream);
4785 : 359032 : va_end (args);
4786 : 359032 : if (!no_nl)
4787 : : {
4788 : 186584 : dumps->bol = true;
4789 : 186584 : fputc ('\n', dumps->stream);
4790 : : }
4791 : : return true;
4792 : : }
4793 : :
4794 : : struct note_def_cache_hasher : ggc_cache_ptr_hash<tree_node>
4795 : : {
4796 : 156287 : static int keep_cache_entry (tree t)
4797 : : {
4798 : 156287 : if (!CHECKING_P)
4799 : : /* GTY is unfortunately not clever enough to conditionalize
4800 : : this. */
4801 : : gcc_unreachable ();
4802 : :
4803 : 156287 : if (ggc_marked_p (t))
4804 : : return -1;
4805 : :
4806 : 0 : unsigned n = dump.push (NULL);
4807 : : /* This might or might not be an error. We should note its
4808 : : dropping whichever. */
4809 : 0 : dump () && dump ("Dropping %N from note_defs table", t);
4810 : 0 : dump.pop (n);
4811 : :
4812 : 0 : return 0;
4813 : : }
4814 : : };
4815 : :
4816 : : /* We should stream each definition at most once.
4817 : : This needs to be a cache because there are cases where a definition
4818 : : ends up being not retained, and we need to drop those so we don't
4819 : : get confused if memory is reallocated. */
4820 : : typedef hash_table<note_def_cache_hasher> note_defs_table_t;
4821 : : static GTY((cache)) note_defs_table_t *note_defs;
4822 : :
4823 : : void
4824 : 245120 : trees_in::assert_definition (tree decl ATTRIBUTE_UNUSED,
4825 : : bool installing ATTRIBUTE_UNUSED)
4826 : : {
4827 : : #if CHECKING_P
4828 : 245120 : tree *slot = note_defs->find_slot (decl, installing ? INSERT : NO_INSERT);
4829 : 245120 : tree not_tmpl = STRIP_TEMPLATE (decl);
4830 : 245120 : if (installing)
4831 : : {
4832 : : /* We must be inserting for the first time. */
4833 : 115800 : gcc_assert (!*slot);
4834 : 115800 : *slot = decl;
4835 : : }
4836 : : else
4837 : : /* If this is not the mergeable entity, it should not be in the
4838 : : table. If it is a non-global-module mergeable entity, it
4839 : : should be in the table. Global module entities could have been
4840 : : defined textually in the current TU and so might or might not
4841 : : be present. */
4842 : 129320 : gcc_assert (!is_duplicate (decl)
4843 : : ? !slot
4844 : : : (slot
4845 : : || !DECL_LANG_SPECIFIC (not_tmpl)
4846 : : || !DECL_MODULE_PURVIEW_P (not_tmpl)
4847 : : || (!DECL_MODULE_IMPORT_P (not_tmpl)
4848 : : && header_module_p ())));
4849 : :
4850 : 245120 : if (not_tmpl != decl)
4851 : 142117 : gcc_assert (!note_defs->find_slot (not_tmpl, NO_INSERT));
4852 : : #endif
4853 : 245120 : }
4854 : :
4855 : : void
4856 : 309826 : trees_out::assert_definition (tree decl ATTRIBUTE_UNUSED)
4857 : : {
4858 : : #if CHECKING_P
4859 : 309826 : tree *slot = note_defs->find_slot (decl, INSERT);
4860 : 309826 : gcc_assert (!*slot);
4861 : 309826 : *slot = decl;
4862 : 309826 : if (TREE_CODE (decl) == TEMPLATE_DECL)
4863 : 167874 : gcc_assert (!note_defs->find_slot (DECL_TEMPLATE_RESULT (decl), NO_INSERT));
4864 : : #endif
4865 : 309826 : }
4866 : :
4867 : : /********************************************************************/
4868 : : static bool
4869 : 11779 : noisy_p ()
4870 : : {
4871 : 0 : if (quiet_flag)
4872 : : return false;
4873 : :
4874 : 0 : pp_needs_newline (global_dc->get_reference_printer ()) = true;
4875 : 0 : diagnostic_set_last_function (global_dc,
4876 : : (diagnostics::diagnostic_info *) nullptr);
4877 : :
4878 : 0 : return true;
4879 : : }
4880 : :
4881 : : /* Set the cmi repo. Strip trailing '/', '.' becomes NULL. */
4882 : :
4883 : : static void
4884 : 99573 : set_cmi_repo (const char *r)
4885 : : {
4886 : 99573 : XDELETEVEC (cmi_repo);
4887 : 99573 : XDELETEVEC (cmi_path);
4888 : 99573 : cmi_path_alloc = 0;
4889 : :
4890 : 99573 : cmi_repo = NULL;
4891 : 99573 : cmi_repo_length = 0;
4892 : :
4893 : 99573 : if (!r || !r[0])
4894 : : return;
4895 : :
4896 : 4591 : size_t len = strlen (r);
4897 : 4591 : cmi_repo = XNEWVEC (char, len + 1);
4898 : 4591 : memcpy (cmi_repo, r, len + 1);
4899 : :
4900 : 4591 : if (len > 1 && IS_DIR_SEPARATOR (cmi_repo[len-1]))
4901 : 4591 : len--;
4902 : 4591 : if (len == 1 && cmi_repo[0] == '.')
4903 : 21 : len--;
4904 : 4591 : cmi_repo[len] = 0;
4905 : 4591 : cmi_repo_length = len;
4906 : : }
4907 : :
4908 : : /* TO is a repo-relative name. Provide one that we may use from where
4909 : : we are. */
4910 : :
4911 : : static const char *
4912 : 5571 : maybe_add_cmi_prefix (const char *to, size_t *len_p = NULL)
4913 : : {
4914 : 5571 : size_t len = len_p || cmi_repo_length ? strlen (to) : 0;
4915 : :
4916 : 5571 : if (cmi_repo_length && !IS_ABSOLUTE_PATH (to))
4917 : : {
4918 : 5550 : if (cmi_path_alloc < cmi_repo_length + len + 2)
4919 : : {
4920 : 4479 : XDELETEVEC (cmi_path);
4921 : 4479 : cmi_path_alloc = cmi_repo_length + len * 2 + 2;
4922 : 4479 : cmi_path = XNEWVEC (char, cmi_path_alloc);
4923 : :
4924 : 4479 : memcpy (cmi_path, cmi_repo, cmi_repo_length);
4925 : 4479 : cmi_path[cmi_repo_length] = DIR_SEPARATOR;
4926 : : }
4927 : :
4928 : 5550 : memcpy (&cmi_path[cmi_repo_length + 1], to, len + 1);
4929 : 5550 : len += cmi_repo_length + 1;
4930 : 5550 : to = cmi_path;
4931 : : }
4932 : :
4933 : 5571 : if (len_p)
4934 : 2682 : *len_p = len;
4935 : :
4936 : 5571 : return to;
4937 : : }
4938 : :
4939 : : /* Try and create the directories of PATH. */
4940 : :
4941 : : static void
4942 : 40 : create_dirs (char *path)
4943 : : {
4944 : 40 : char *base = path;
4945 : : /* Skip past initial slashes of absolute path. */
4946 : 40 : while (IS_DIR_SEPARATOR (*base))
4947 : 0 : base++;
4948 : :
4949 : : /* Try and create the missing directories. */
4950 : 2488 : for (; *base; base++)
4951 : 2448 : if (IS_DIR_SEPARATOR (*base))
4952 : : {
4953 : 248 : char sep = *base;
4954 : 248 : *base = 0;
4955 : 248 : int failed = mkdir (path, S_IRWXU | S_IRWXG | S_IRWXO);
4956 : 253 : dump () && dump ("Mkdir ('%s') errno:=%u", path, failed ? errno : 0);
4957 : 248 : *base = sep;
4958 : 248 : if (failed
4959 : : /* Maybe racing with another creator (of a *different*
4960 : : module). */
4961 : 47 : && errno != EEXIST)
4962 : : break;
4963 : : }
4964 : 40 : }
4965 : :
4966 : : /* Given a CLASSTYPE_DECL_LIST VALUE get the template friend decl,
4967 : : if that's what this is. */
4968 : :
4969 : : static tree
4970 : 62498 : friend_from_decl_list (tree frnd)
4971 : : {
4972 : 62498 : tree res = frnd;
4973 : :
4974 : 62498 : if (TREE_CODE (frnd) != TEMPLATE_DECL)
4975 : : {
4976 : 35800 : tree tmpl = NULL_TREE;
4977 : 35800 : if (TYPE_P (frnd))
4978 : : {
4979 : 5745 : res = TYPE_NAME (frnd);
4980 : 5661 : if (CLASS_TYPE_P (frnd)
4981 : 11406 : && CLASSTYPE_TEMPLATE_INFO (frnd))
4982 : 5652 : tmpl = CLASSTYPE_TI_TEMPLATE (frnd);
4983 : : }
4984 : 30055 : else if (DECL_TEMPLATE_INFO (frnd))
4985 : : {
4986 : 30055 : tmpl = DECL_TI_TEMPLATE (frnd);
4987 : 30055 : if (TREE_CODE (tmpl) != TEMPLATE_DECL)
4988 : : tmpl = NULL_TREE;
4989 : : }
4990 : :
4991 : 40853 : if (tmpl && DECL_TEMPLATE_RESULT (tmpl) == res)
4992 : : res = tmpl;
4993 : : }
4994 : :
4995 : 62498 : return res;
4996 : : }
4997 : :
4998 : : static tree
4999 : 20714 : find_enum_member (tree ctx, tree name)
5000 : : {
5001 : 20714 : for (tree values = TYPE_VALUES (ctx);
5002 : 424658 : values; values = TREE_CHAIN (values))
5003 : 419587 : if (DECL_NAME (TREE_VALUE (values)) == name)
5004 : : return TREE_VALUE (values);
5005 : :
5006 : : return NULL_TREE;
5007 : : }
5008 : :
5009 : : /********************************************************************/
5010 : : /* Instrumentation gathered writing bytes. */
5011 : :
5012 : : void
5013 : 273 : bytes_out::instrument ()
5014 : : {
5015 : 273 : dump ("Wrote %u bytes in %u blocks", lengths[3], spans[3]);
5016 : 273 : dump ("Wrote %u bits in %u bytes", lengths[0] + lengths[1], lengths[2]);
5017 : 819 : for (unsigned ix = 0; ix < 2; ix++)
5018 : 819 : dump (" %u %s spans of %R bits", spans[ix],
5019 : : ix ? "one" : "zero", lengths[ix], spans[ix]);
5020 : 273 : dump (" %u blocks with %R bits padding", spans[2],
5021 : 273 : lengths[2] * 8 - (lengths[0] + lengths[1]), spans[2]);
5022 : 273 : }
5023 : :
5024 : : /* Instrumentation gathered writing trees. */
5025 : : void
5026 : 2544 : trees_out::instrument ()
5027 : : {
5028 : 2544 : if (dump (""))
5029 : : {
5030 : 273 : bytes_out::instrument ();
5031 : 273 : dump ("Wrote:");
5032 : 273 : dump (" %u decl trees", decl_val_count);
5033 : 273 : dump (" %u other trees", tree_val_count);
5034 : 273 : dump (" %u back references", back_ref_count);
5035 : 273 : dump (" %u TU-local entities", tu_local_count);
5036 : 273 : dump (" %u null trees", null_count);
5037 : : }
5038 : 2544 : }
5039 : :
5040 : : /* Setup and teardown for a tree walk. */
5041 : :
5042 : : void
5043 : 1827723 : trees_out::begin ()
5044 : : {
5045 : 1827723 : gcc_assert (!streaming_p () || !tree_map.elements ());
5046 : :
5047 : 1827723 : mark_trees ();
5048 : 1827723 : if (streaming_p ())
5049 : 225594 : parent::begin ();
5050 : 1827723 : }
5051 : :
5052 : : unsigned
5053 : 225594 : trees_out::end (elf_out *sink, unsigned name, unsigned *crc_ptr)
5054 : : {
5055 : 225594 : gcc_checking_assert (streaming_p ());
5056 : :
5057 : 225594 : unmark_trees ();
5058 : 225594 : return parent::end (sink, name, crc_ptr);
5059 : : }
5060 : :
5061 : : void
5062 : 1602129 : trees_out::end ()
5063 : : {
5064 : 1602129 : gcc_assert (!streaming_p ());
5065 : :
5066 : 1602129 : unmark_trees ();
5067 : : /* Do not parent::end -- we weren't streaming. */
5068 : 1602129 : }
5069 : :
5070 : : void
5071 : 1827723 : trees_out::mark_trees ()
5072 : : {
5073 : 1827723 : if (size_t size = tree_map.elements ())
5074 : : {
5075 : : /* This isn't our first rodeo, destroy and recreate the
5076 : : tree_map. I'm a bad bad man. Use the previous size as a
5077 : : guess for the next one (so not all bad). */
5078 : 1384584 : tree_map.~ptr_int_hash_map ();
5079 : 1384584 : new (&tree_map) ptr_int_hash_map (size);
5080 : : }
5081 : :
5082 : : /* Install the fixed trees, with +ve references. */
5083 : 1827723 : unsigned limit = fixed_trees->length ();
5084 : 352546832 : for (unsigned ix = 0; ix != limit; ix++)
5085 : : {
5086 : 350719109 : tree val = (*fixed_trees)[ix];
5087 : 350719109 : bool existed = tree_map.put (val, ix + tag_fixed);
5088 : 350719109 : gcc_checking_assert (!TREE_VISITED (val) && !existed);
5089 : 350719109 : TREE_VISITED (val) = true;
5090 : : }
5091 : :
5092 : 1827723 : ref_num = 0;
5093 : 1827723 : }
5094 : :
5095 : : /* Unmark the trees we encountered */
5096 : :
5097 : : void
5098 : 1827723 : trees_out::unmark_trees ()
5099 : : {
5100 : 1827723 : ptr_int_hash_map::iterator end (tree_map.end ());
5101 : 413536528 : for (ptr_int_hash_map::iterator iter (tree_map.begin ()); iter != end; ++iter)
5102 : : {
5103 : 411708805 : tree node = reinterpret_cast<tree> ((*iter).first);
5104 : 411708805 : int ref = (*iter).second;
5105 : : /* We should have visited the node, and converted its mergeable
5106 : : reference to a regular reference. */
5107 : 411708805 : gcc_checking_assert (TREE_VISITED (node)
5108 : : && (ref <= tag_backref || ref >= tag_fixed));
5109 : 411708805 : TREE_VISITED (node) = false;
5110 : : }
5111 : 1827723 : }
5112 : :
5113 : : /* Mark DECL for by-value walking. We do this by inserting it into
5114 : : the tree map with a reference of zero. May be called multiple
5115 : : times on the same node. */
5116 : :
5117 : : void
5118 : 2840550 : trees_out::mark_by_value (tree decl)
5119 : : {
5120 : 2840550 : gcc_checking_assert (DECL_P (decl)
5121 : : /* Enum consts are INTEGER_CSTS. */
5122 : : || TREE_CODE (decl) == INTEGER_CST
5123 : : || TREE_CODE (decl) == TREE_BINFO);
5124 : :
5125 : 2840550 : if (TREE_VISITED (decl))
5126 : : /* Must already be forced or fixed. */
5127 : 3084 : gcc_checking_assert (*tree_map.get (decl) >= tag_value);
5128 : : else
5129 : : {
5130 : 2837466 : bool existed = tree_map.put (decl, tag_value);
5131 : 2837466 : gcc_checking_assert (!existed);
5132 : 2837466 : TREE_VISITED (decl) = true;
5133 : : }
5134 : 2840550 : }
5135 : :
5136 : : int
5137 : 74433156 : trees_out::get_tag (tree t)
5138 : : {
5139 : 74433156 : gcc_checking_assert (TREE_VISITED (t));
5140 : 74433156 : return *tree_map.get (t);
5141 : : }
5142 : :
5143 : : /* Insert T into the map, return its tag number. */
5144 : :
5145 : : int
5146 : 60989696 : trees_out::insert (tree t, walk_kind walk)
5147 : : {
5148 : 60989696 : gcc_checking_assert (walk != WK_normal || !TREE_VISITED (t));
5149 : 60989696 : int tag = --ref_num;
5150 : 60989696 : bool existed;
5151 : 60989696 : int &slot = tree_map.get_or_insert (t, &existed);
5152 : 60989696 : gcc_checking_assert (TREE_VISITED (t) == existed
5153 : : && (!existed
5154 : : || (walk == WK_value && slot == tag_value)));
5155 : 60989696 : TREE_VISITED (t) = true;
5156 : 60989696 : slot = tag;
5157 : :
5158 : 60989696 : return tag;
5159 : : }
5160 : :
5161 : : /* Insert T into the backreference array. Return its back reference
5162 : : number. */
5163 : :
5164 : : int
5165 : 14776687 : trees_in::insert (tree t)
5166 : : {
5167 : 14776687 : gcc_checking_assert (t || get_overrun ());
5168 : 14776687 : back_refs.safe_push (t);
5169 : 14776687 : return -(int)back_refs.length ();
5170 : : }
5171 : :
5172 : : /* A chained set of decls. */
5173 : :
5174 : : void
5175 : 88944 : trees_out::chained_decls (tree decls)
5176 : : {
5177 : 208396 : for (; decls; decls = DECL_CHAIN (decls))
5178 : 119452 : tree_node (decls);
5179 : 88944 : tree_node (NULL_TREE);
5180 : 88944 : }
5181 : :
5182 : : tree
5183 : 35266 : trees_in::chained_decls ()
5184 : : {
5185 : 35266 : tree decls = NULL_TREE;
5186 : 35266 : for (tree *chain = &decls;;)
5187 : 87211 : if (tree decl = tree_node ())
5188 : : {
5189 : 51945 : if (!DECL_P (decl) || DECL_CHAIN (decl))
5190 : : {
5191 : 0 : set_overrun ();
5192 : 0 : break;
5193 : : }
5194 : 51945 : *chain = decl;
5195 : 51945 : chain = &DECL_CHAIN (decl);
5196 : : }
5197 : : else
5198 : 51945 : break;
5199 : :
5200 : 35266 : return decls;
5201 : : }
5202 : :
5203 : : /* A vector of decls following DECL_CHAIN. */
5204 : :
5205 : : void
5206 : 282010 : trees_out::vec_chained_decls (tree decls)
5207 : : {
5208 : 282010 : if (streaming_p ())
5209 : : {
5210 : : unsigned len = 0;
5211 : :
5212 : 811676 : for (tree decl = decls; decl; decl = DECL_CHAIN (decl))
5213 : 670711 : len++;
5214 : 140965 : u (len);
5215 : : }
5216 : :
5217 : 1623758 : for (tree decl = decls; decl; decl = DECL_CHAIN (decl))
5218 : : {
5219 : 292367 : if (DECL_IMPLICIT_TYPEDEF_P (decl)
5220 : 1352505 : && TYPE_NAME (TREE_TYPE (decl)) != decl)
5221 : : /* An anonynmous struct with a typedef name. An odd thing to
5222 : : write. */
5223 : 8 : tree_node (NULL_TREE);
5224 : : else
5225 : 1341740 : tree_node (decl);
5226 : : }
5227 : 282010 : }
5228 : :
5229 : : vec<tree, va_heap> *
5230 : 103590 : trees_in::vec_chained_decls ()
5231 : : {
5232 : 103590 : vec<tree, va_heap> *v = NULL;
5233 : :
5234 : 103590 : if (unsigned len = u ())
5235 : : {
5236 : 54318 : vec_alloc (v, len);
5237 : :
5238 : 604551 : for (unsigned ix = 0; ix < len; ix++)
5239 : : {
5240 : 550233 : tree decl = tree_node ();
5241 : 550233 : if (decl && !DECL_P (decl))
5242 : : {
5243 : 0 : set_overrun ();
5244 : 0 : break;
5245 : : }
5246 : 550233 : v->quick_push (decl);
5247 : : }
5248 : :
5249 : 54318 : if (get_overrun ())
5250 : : {
5251 : 0 : vec_free (v);
5252 : 0 : v = NULL;
5253 : : }
5254 : : }
5255 : :
5256 : 103590 : return v;
5257 : : }
5258 : :
5259 : : /* A vector of trees. */
5260 : :
5261 : : void
5262 : 199614 : trees_out::tree_vec (vec<tree, va_gc> *v)
5263 : : {
5264 : 199614 : unsigned len = vec_safe_length (v);
5265 : 199614 : if (streaming_p ())
5266 : 99787 : u (len);
5267 : 256804 : for (unsigned ix = 0; ix != len; ix++)
5268 : 57190 : tree_node ((*v)[ix]);
5269 : 199614 : }
5270 : :
5271 : : vec<tree, va_gc> *
5272 : 73417 : trees_in::tree_vec ()
5273 : : {
5274 : 73417 : vec<tree, va_gc> *v = NULL;
5275 : 73417 : if (unsigned len = u ())
5276 : : {
5277 : 19271 : vec_alloc (v, len);
5278 : 40416 : for (unsigned ix = 0; ix != len; ix++)
5279 : 21145 : v->quick_push (tree_node ());
5280 : : }
5281 : 73417 : return v;
5282 : : }
5283 : :
5284 : : /* A vector of tree pairs. */
5285 : :
5286 : : void
5287 : 4790 : trees_out::tree_pair_vec (vec<tree_pair_s, va_gc> *v)
5288 : : {
5289 : 4790 : unsigned len = vec_safe_length (v);
5290 : 4790 : if (streaming_p ())
5291 : 2395 : u (len);
5292 : 4790 : if (len)
5293 : 25630 : for (unsigned ix = 0; ix != len; ix++)
5294 : : {
5295 : 20946 : tree_pair_s const &s = (*v)[ix];
5296 : 20946 : tree_node (s.purpose);
5297 : 20946 : tree_node (s.value);
5298 : : }
5299 : 4790 : }
5300 : :
5301 : : vec<tree_pair_s, va_gc> *
5302 : 2048 : trees_in::tree_pair_vec ()
5303 : : {
5304 : 2048 : vec<tree_pair_s, va_gc> *v = NULL;
5305 : 2048 : if (unsigned len = u ())
5306 : : {
5307 : 2004 : vec_alloc (v, len);
5308 : 11189 : for (unsigned ix = 0; ix != len; ix++)
5309 : : {
5310 : 9185 : tree_pair_s s;
5311 : 9185 : s.purpose = tree_node ();
5312 : 9185 : s.value = tree_node ();
5313 : 9185 : v->quick_push (s);
5314 : : }
5315 : : }
5316 : 2048 : return v;
5317 : : }
5318 : :
5319 : : void
5320 : 298440 : trees_out::tree_list (tree list, bool has_purpose)
5321 : : {
5322 : 1236537 : for (; list; list = TREE_CHAIN (list))
5323 : : {
5324 : 938097 : gcc_checking_assert (TREE_VALUE (list));
5325 : 938097 : tree_node (TREE_VALUE (list));
5326 : 938097 : if (has_purpose)
5327 : 907329 : tree_node (TREE_PURPOSE (list));
5328 : : }
5329 : 298440 : tree_node (NULL_TREE);
5330 : 298440 : }
5331 : :
5332 : : tree
5333 : 111188 : trees_in::tree_list (bool has_purpose)
5334 : : {
5335 : 111188 : tree res = NULL_TREE;
5336 : :
5337 : 494511 : for (tree *chain = &res; tree value = tree_node ();
5338 : 766646 : chain = &TREE_CHAIN (*chain))
5339 : : {
5340 : 383323 : tree purpose = has_purpose ? tree_node () : NULL_TREE;
5341 : 383323 : *chain = build_tree_list (purpose, value);
5342 : 383323 : }
5343 : :
5344 : 111188 : return res;
5345 : : }
5346 : :
5347 : : #define CASE_OMP_SIMD_CODE \
5348 : : case OMP_SIMD: \
5349 : : case OMP_STRUCTURED_BLOCK: \
5350 : : case OMP_LOOP: \
5351 : : case OMP_ORDERED: \
5352 : : case OMP_TILE: \
5353 : : case OMP_UNROLL
5354 : : #define CASE_OMP_CODE \
5355 : : case OMP_PARALLEL: \
5356 : : case OMP_TASK: \
5357 : : case OMP_FOR: \
5358 : : case OMP_DISTRIBUTE: \
5359 : : case OMP_TASKLOOP: \
5360 : : case OMP_TEAMS: \
5361 : : case OMP_TARGET_DATA: \
5362 : : case OMP_TARGET: \
5363 : : case OMP_SECTIONS: \
5364 : : case OMP_CRITICAL: \
5365 : : case OMP_SINGLE: \
5366 : : case OMP_SCOPE: \
5367 : : case OMP_TASKGROUP: \
5368 : : case OMP_MASKED: \
5369 : : case OMP_DISPATCH: \
5370 : : case OMP_INTEROP: \
5371 : : case OMP_MASTER: \
5372 : : case OMP_TARGET_UPDATE: \
5373 : : case OMP_TARGET_ENTER_DATA: \
5374 : : case OMP_TARGET_EXIT_DATA: \
5375 : : case OMP_METADIRECTIVE: \
5376 : : case OMP_ATOMIC: \
5377 : : case OMP_ATOMIC_READ: \
5378 : : case OMP_ATOMIC_CAPTURE_OLD: \
5379 : : case OMP_ATOMIC_CAPTURE_NEW
5380 : : #define CASE_OACC_CODE \
5381 : : case OACC_PARALLEL: \
5382 : : case OACC_KERNELS: \
5383 : : case OACC_SERIAL: \
5384 : : case OACC_DATA: \
5385 : : case OACC_HOST_DATA: \
5386 : : case OACC_LOOP: \
5387 : : case OACC_CACHE: \
5388 : : case OACC_DECLARE: \
5389 : : case OACC_ENTER_DATA: \
5390 : : case OACC_EXIT_DATA: \
5391 : : case OACC_UPDATE
5392 : :
5393 : : /* Start tree write. Write information to allocate the receiving
5394 : : node. */
5395 : :
5396 : : void
5397 : 12341762 : trees_out::start (tree t, bool code_streamed)
5398 : : {
5399 : 12341762 : if (TYPE_P (t))
5400 : : {
5401 : 501265 : enum tree_code code = TREE_CODE (t);
5402 : 501265 : gcc_checking_assert (TYPE_MAIN_VARIANT (t) == t);
5403 : : /* All these types are TYPE_NON_COMMON. */
5404 : 501265 : gcc_checking_assert (code == RECORD_TYPE
5405 : : || code == UNION_TYPE
5406 : : || code == ENUMERAL_TYPE
5407 : : || code == TEMPLATE_TYPE_PARM
5408 : : || code == TEMPLATE_TEMPLATE_PARM
5409 : : || code == BOUND_TEMPLATE_TEMPLATE_PARM);
5410 : : }
5411 : :
5412 : 12341762 : if (!code_streamed)
5413 : 11892842 : u (TREE_CODE (t));
5414 : :
5415 : 12341762 : switch (TREE_CODE (t))
5416 : : {
5417 : 10936389 : default:
5418 : 10936389 : if (VL_EXP_CLASS_P (t))
5419 : 460198 : u (VL_EXP_OPERAND_LENGTH (t));
5420 : : break;
5421 : :
5422 : 562901 : case INTEGER_CST:
5423 : 562901 : u (TREE_INT_CST_NUNITS (t));
5424 : 562901 : u (TREE_INT_CST_EXT_NUNITS (t));
5425 : 562901 : break;
5426 : :
5427 : 12 : case OMP_CLAUSE:
5428 : 12 : u (OMP_CLAUSE_CODE (t));
5429 : 12 : break;
5430 : :
5431 : 6 : CASE_OMP_SIMD_CODE:
5432 : 6 : state->extensions |= SE_OPENMP_SIMD;
5433 : 6 : break;
5434 : :
5435 : 3 : CASE_OMP_CODE:
5436 : 3 : state->extensions |= SE_OPENMP;
5437 : 3 : break;
5438 : :
5439 : 6 : CASE_OACC_CODE:
5440 : 6 : state->extensions |= SE_OPENACC;
5441 : 6 : break;
5442 : :
5443 : 41604 : case STRING_CST:
5444 : 41604 : str (TREE_STRING_POINTER (t), TREE_STRING_LENGTH (t));
5445 : 41604 : break;
5446 : :
5447 : 18 : case RAW_DATA_CST:
5448 : 18 : if (RAW_DATA_OWNER (t) == NULL_TREE)
5449 : : {
5450 : : /* Stream RAW_DATA_CST with no owner (i.e. data pointing
5451 : : into libcpp buffers) as something we can stream in as
5452 : : STRING_CST which owns the data. */
5453 : 6 : u (0);
5454 : : /* Can't use str (RAW_DATA_POINTER (t), RAW_DATA_LENGTH (t));
5455 : : here as there isn't a null termination after it. */
5456 : 6 : z (RAW_DATA_LENGTH (t));
5457 : 6 : if (RAW_DATA_LENGTH (t))
5458 : 6 : if (void *ptr = buf (RAW_DATA_LENGTH (t) + 1))
5459 : : {
5460 : 6 : memcpy (ptr, RAW_DATA_POINTER (t), RAW_DATA_LENGTH (t));
5461 : 6 : ((char *) ptr)[RAW_DATA_LENGTH (t)] = '\0';
5462 : : }
5463 : : }
5464 : : else
5465 : : {
5466 : 12 : gcc_assert (RAW_DATA_LENGTH (t));
5467 : 12 : u (RAW_DATA_LENGTH (t));
5468 : : }
5469 : : break;
5470 : :
5471 : 18 : case VECTOR_CST:
5472 : 18 : u (VECTOR_CST_LOG2_NPATTERNS (t));
5473 : 18 : u (VECTOR_CST_NELTS_PER_PATTERN (t));
5474 : 18 : break;
5475 : :
5476 : 97392 : case TREE_BINFO:
5477 : 97392 : u (BINFO_N_BASE_BINFOS (t));
5478 : 97392 : break;
5479 : :
5480 : 703413 : case TREE_VEC:
5481 : 703413 : u (TREE_VEC_LENGTH (t));
5482 : 703413 : break;
5483 : :
5484 : 0 : case FIXED_CST:
5485 : 0 : gcc_unreachable (); /* Not supported in C++. */
5486 : 0 : break;
5487 : :
5488 : 0 : case IDENTIFIER_NODE:
5489 : 0 : case SSA_NAME:
5490 : 0 : case TARGET_MEM_REF:
5491 : 0 : case TRANSLATION_UNIT_DECL:
5492 : : /* We shouldn't meet these. */
5493 : 0 : gcc_unreachable ();
5494 : 12341762 : break;
5495 : : }
5496 : 12341762 : }
5497 : :
5498 : : /* Start tree read. Allocate the receiving node. */
5499 : :
5500 : : tree
5501 : 10503437 : trees_in::start (unsigned code)
5502 : : {
5503 : 10503437 : tree t = NULL_TREE;
5504 : :
5505 : 10503437 : if (!code)
5506 : 9490425 : code = u ();
5507 : :
5508 : 10503437 : switch (code)
5509 : : {
5510 : 9338596 : default:
5511 : 9338596 : if (code >= MAX_TREE_CODES)
5512 : : {
5513 : 0 : fail:
5514 : 0 : set_overrun ();
5515 : 0 : return NULL_TREE;
5516 : : }
5517 : 9338596 : else if (TREE_CODE_CLASS (code) == tcc_vl_exp)
5518 : : {
5519 : 397222 : unsigned ops = u ();
5520 : 397222 : t = build_vl_exp (tree_code (code), ops);
5521 : : }
5522 : : else
5523 : 8941374 : t = make_node (tree_code (code));
5524 : : break;
5525 : :
5526 : 470374 : case INTEGER_CST:
5527 : 470374 : {
5528 : 470374 : unsigned n = u ();
5529 : 470374 : unsigned e = u ();
5530 : 470374 : t = make_int_cst (n, e);
5531 : : }
5532 : 470374 : break;
5533 : :
5534 : 12 : case OMP_CLAUSE:
5535 : 12 : t = build_omp_clause (UNKNOWN_LOCATION, omp_clause_code (u ()));
5536 : 12 : break;
5537 : :
5538 : 9 : CASE_OMP_SIMD_CODE:
5539 : 9 : if (!(state->extensions & SE_OPENMP_SIMD))
5540 : 0 : goto fail;
5541 : 9 : t = make_node (tree_code (code));
5542 : 9 : break;
5543 : :
5544 : 3 : CASE_OMP_CODE:
5545 : 3 : if (!(state->extensions & SE_OPENMP))
5546 : 0 : goto fail;
5547 : 3 : t = make_node (tree_code (code));
5548 : 3 : break;
5549 : :
5550 : 6 : CASE_OACC_CODE:
5551 : 6 : if (!(state->extensions & SE_OPENACC))
5552 : 0 : goto fail;
5553 : 6 : t = make_node (tree_code (code));
5554 : 6 : break;
5555 : :
5556 : 39563 : case STRING_CST:
5557 : 39563 : {
5558 : 39563 : size_t l;
5559 : 39563 : const char *chars = str (&l);
5560 : 39563 : t = build_string (l, chars);
5561 : : }
5562 : 39563 : break;
5563 : :
5564 : 9 : case RAW_DATA_CST:
5565 : 9 : {
5566 : 9 : size_t l = u ();
5567 : 9 : if (l == 0)
5568 : : {
5569 : : /* Stream in RAW_DATA_CST with no owner as STRING_CST
5570 : : which owns the data. */
5571 : 3 : const char *chars = str (&l);
5572 : 3 : t = build_string (l, chars);
5573 : : }
5574 : : else
5575 : : {
5576 : 6 : t = make_node (RAW_DATA_CST);
5577 : 6 : RAW_DATA_LENGTH (t) = l;
5578 : : }
5579 : : }
5580 : 9 : break;
5581 : :
5582 : 24 : case VECTOR_CST:
5583 : 24 : {
5584 : 24 : unsigned log2_npats = u ();
5585 : 24 : unsigned elts_per = u ();
5586 : 24 : t = make_vector (log2_npats, elts_per);
5587 : : }
5588 : 24 : break;
5589 : :
5590 : 71369 : case TREE_BINFO:
5591 : 71369 : t = make_tree_binfo (u ());
5592 : 71369 : break;
5593 : :
5594 : 583472 : case TREE_VEC:
5595 : 583472 : t = make_tree_vec (u ());
5596 : 583472 : break;
5597 : :
5598 : 0 : case FIXED_CST:
5599 : 0 : case IDENTIFIER_NODE:
5600 : 0 : case SSA_NAME:
5601 : 0 : case TARGET_MEM_REF:
5602 : 0 : case TRANSLATION_UNIT_DECL:
5603 : 0 : goto fail;
5604 : : }
5605 : :
5606 : : return t;
5607 : : }
5608 : :
5609 : : /* The kinds of interface an importer could have for a decl. */
5610 : :
5611 : : enum class importer_interface {
5612 : : unknown, /* The definition may or may not need to be emitted. */
5613 : : external, /* The definition can always be found in another TU. */
5614 : : internal, /* The definition should be emitted in the importer's TU. */
5615 : : always_emit, /* The definition must be emitted in the importer's TU,
5616 : : regardless of if it's used or not. */
5617 : : };
5618 : :
5619 : : /* Returns what kind of interface an importer will have of DECL. */
5620 : :
5621 : : static importer_interface
5622 : 473667 : get_importer_interface (tree decl)
5623 : : {
5624 : : /* Internal linkage entities must be emitted in each importer if
5625 : : there is a definition available. */
5626 : 473667 : if (!TREE_PUBLIC (decl))
5627 : : return importer_interface::internal;
5628 : :
5629 : : /* Other entities that aren't vague linkage are either not definitions
5630 : : or will be publicly emitted in this TU, so importers can just refer
5631 : : to an external definition. */
5632 : 247567 : if (!vague_linkage_p (decl))
5633 : : return importer_interface::external;
5634 : :
5635 : : /* For explicit instantiations, importers can always rely on there
5636 : : being a definition in another TU, unless this is a definition
5637 : : in a header module: in which case the importer will always need
5638 : : to emit it. */
5639 : 242497 : if (DECL_LANG_SPECIFIC (decl)
5640 : 242497 : && DECL_EXPLICIT_INSTANTIATION (decl))
5641 : 18583 : return (header_module_p () && !DECL_EXTERNAL (decl)
5642 : 18583 : ? importer_interface::always_emit
5643 : : : importer_interface::external);
5644 : :
5645 : : /* A gnu_inline function is never emitted in any TU. */
5646 : 223914 : if (TREE_CODE (decl) == FUNCTION_DECL
5647 : 153203 : && DECL_DECLARED_INLINE_P (decl)
5648 : 372170 : && lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl)))
5649 : : return importer_interface::external;
5650 : :
5651 : : /* Everything else has vague linkage. */
5652 : : return importer_interface::unknown;
5653 : : }
5654 : :
5655 : : /* The structure streamers access the raw fields, because the
5656 : : alternative, of using the accessor macros can require using
5657 : : different accessors for the same underlying field, depending on the
5658 : : tree code. That's both confusing and annoying. */
5659 : :
5660 : : /* Read & write the core boolean flags. */
5661 : :
5662 : : void
5663 : 12361186 : trees_out::core_bools (tree t, bits_out& bits)
5664 : : {
5665 : : #define WB(X) (bits.b (X))
5666 : : /* Stream X if COND holds, and if !COND stream a dummy value so that the
5667 : : overall number of bits streamed is independent of the runtime value
5668 : : of COND, which allows the compiler to better optimize this function. */
5669 : : #define WB_IF(COND, X) WB ((COND) ? (X) : false)
5670 : 12361186 : tree_code code = TREE_CODE (t);
5671 : :
5672 : 12361186 : WB (t->base.side_effects_flag);
5673 : 12361186 : WB (t->base.constant_flag);
5674 : 12361186 : WB (t->base.addressable_flag);
5675 : 12361186 : WB (t->base.volatile_flag);
5676 : 12361186 : WB (t->base.readonly_flag);
5677 : : /* base.asm_written_flag is a property of the current TU's use of
5678 : : this decl. */
5679 : 12361186 : WB (t->base.nowarning_flag);
5680 : : /* base.visited read as zero (it's set for writer, because that's
5681 : : how we mark nodes). */
5682 : : /* base.used_flag is not streamed. Readers may set TREE_USED of
5683 : : decls they use. */
5684 : 12361186 : WB (t->base.nothrow_flag);
5685 : 12361186 : WB (t->base.static_flag);
5686 : : /* This is TYPE_CACHED_VALUES_P for types. */
5687 : 12361186 : WB_IF (TREE_CODE_CLASS (code) != tcc_type, t->base.public_flag);
5688 : 12361186 : WB (t->base.private_flag);
5689 : 12361186 : WB (t->base.protected_flag);
5690 : 12361186 : WB (t->base.deprecated_flag);
5691 : 12361186 : WB (t->base.default_def_flag);
5692 : :
5693 : 12361186 : switch (code)
5694 : : {
5695 : : case CALL_EXPR:
5696 : : case INTEGER_CST:
5697 : : case SSA_NAME:
5698 : : case TARGET_MEM_REF:
5699 : : case TREE_VEC:
5700 : : /* These use different base.u fields. */
5701 : : return;
5702 : :
5703 : 10645917 : default:
5704 : 10645917 : WB (t->base.u.bits.lang_flag_0);
5705 : 10645917 : bool flag_1 = t->base.u.bits.lang_flag_1;
5706 : 10645917 : if (!flag_1)
5707 : : ;
5708 : 339129 : else if (code == TEMPLATE_INFO)
5709 : : /* This is TI_PENDING_TEMPLATE_FLAG, not relevant to reader. */
5710 : : flag_1 = false;
5711 : 335475 : else if (code == VAR_DECL)
5712 : : {
5713 : : /* This is DECL_INITIALIZED_P. */
5714 : 69268 : if (TREE_CODE (DECL_CONTEXT (t)) != FUNCTION_DECL)
5715 : : /* We'll set this when reading the definition. */
5716 : 10645917 : flag_1 = false;
5717 : : }
5718 : 10645917 : WB (flag_1);
5719 : 10645917 : WB (t->base.u.bits.lang_flag_2);
5720 : 10645917 : WB (t->base.u.bits.lang_flag_3);
5721 : 10645917 : WB (t->base.u.bits.lang_flag_4);
5722 : 10645917 : WB (t->base.u.bits.lang_flag_5);
5723 : 10645917 : WB (t->base.u.bits.lang_flag_6);
5724 : 10645917 : WB (t->base.u.bits.saturating_flag);
5725 : 10645917 : WB (t->base.u.bits.unsigned_flag);
5726 : 10645917 : WB (t->base.u.bits.packed_flag);
5727 : 10645917 : WB (t->base.u.bits.user_align);
5728 : 10645917 : WB (t->base.u.bits.nameless_flag);
5729 : 10645917 : WB (t->base.u.bits.atomic_flag);
5730 : 10645917 : WB (t->base.u.bits.unavailable_flag);
5731 : 10645917 : break;
5732 : : }
5733 : :
5734 : 10645917 : if (TREE_CODE_CLASS (code) == tcc_type)
5735 : : {
5736 : 520689 : WB (t->type_common.no_force_blk_flag);
5737 : 520689 : WB (t->type_common.needs_constructing_flag);
5738 : 520689 : WB (t->type_common.transparent_aggr_flag);
5739 : 520689 : WB (t->type_common.restrict_flag);
5740 : 520689 : WB (t->type_common.string_flag);
5741 : 520689 : WB (t->type_common.lang_flag_0);
5742 : 520689 : WB (t->type_common.lang_flag_1);
5743 : 520689 : WB (t->type_common.lang_flag_2);
5744 : 520689 : WB (t->type_common.lang_flag_3);
5745 : 520689 : WB (t->type_common.lang_flag_4);
5746 : 520689 : WB (t->type_common.lang_flag_5);
5747 : 520689 : WB (t->type_common.lang_flag_6);
5748 : 520689 : WB (t->type_common.typeless_storage);
5749 : : }
5750 : :
5751 : 10645917 : if (TREE_CODE_CLASS (code) != tcc_declaration)
5752 : : return;
5753 : :
5754 : 2771577 : if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
5755 : : {
5756 : 2771577 : WB (t->decl_common.nonlocal_flag);
5757 : 2771577 : WB (t->decl_common.virtual_flag);
5758 : 2771577 : WB (t->decl_common.ignored_flag);
5759 : 2771577 : WB (t->decl_common.abstract_flag);
5760 : 2771577 : WB (t->decl_common.artificial_flag);
5761 : 2771577 : WB (t->decl_common.preserve_flag);
5762 : 2771577 : WB (t->decl_common.debug_expr_is_from);
5763 : 2771577 : WB (t->decl_common.lang_flag_0);
5764 : 2771577 : WB (t->decl_common.lang_flag_1);
5765 : 2771577 : WB (t->decl_common.lang_flag_2);
5766 : 2771577 : WB (t->decl_common.lang_flag_3);
5767 : 2771577 : WB (t->decl_common.lang_flag_4);
5768 : :
5769 : 2771577 : {
5770 : : /* This is DECL_INTERFACE_KNOWN: We should redetermine whether
5771 : : we need to import or export any vague-linkage entities on
5772 : : stream-in. */
5773 : 2771577 : bool interface_known = t->decl_common.lang_flag_5;
5774 : 2771577 : if (interface_known
5775 : 2771577 : && get_importer_interface (t) == importer_interface::unknown)
5776 : : interface_known = false;
5777 : 2771577 : WB (interface_known);
5778 : : }
5779 : :
5780 : 2771577 : WB (t->decl_common.lang_flag_6);
5781 : 2771577 : WB (t->decl_common.lang_flag_7);
5782 : 2771577 : WB (t->decl_common.lang_flag_8);
5783 : 2771577 : WB (t->decl_common.decl_flag_0);
5784 : :
5785 : 2771577 : {
5786 : : /* DECL_EXTERNAL -> decl_flag_1
5787 : : == it is defined elsewhere
5788 : : DECL_NOT_REALLY_EXTERN -> base.not_really_extern
5789 : : == that was a lie, it is here */
5790 : :
5791 : 2771577 : bool is_external = t->decl_common.decl_flag_1;
5792 : : /* maybe_emit_vtables relies on vtables being marked as
5793 : : DECL_EXTERNAL and DECL_NOT_REALLY_EXTERN before processing. */
5794 : 2771577 : if (!is_external && VAR_P (t) && DECL_VTABLE_OR_VTT_P (t))
5795 : : is_external = true;
5796 : : /* Things we emit here might well be external from the POV of an
5797 : : importer. */
5798 : 2771370 : if (!is_external
5799 : 2328171 : && VAR_OR_FUNCTION_DECL_P (t)
5800 : 2979509 : && get_importer_interface (t) == importer_interface::external)
5801 : : is_external = true;
5802 : 2771577 : WB (is_external);
5803 : : }
5804 : :
5805 : 2771577 : WB (t->decl_common.decl_flag_2);
5806 : 2771577 : WB (t->decl_common.decl_flag_3);
5807 : 2771577 : WB (t->decl_common.not_gimple_reg_flag);
5808 : 2771577 : WB (t->decl_common.decl_by_reference_flag);
5809 : 2771577 : WB (t->decl_common.decl_read_flag);
5810 : 2771577 : WB (t->decl_common.decl_nonshareable_flag);
5811 : 2771577 : WB (t->decl_common.decl_not_flexarray);
5812 : : }
5813 : : else
5814 : : return;
5815 : :
5816 : 2771577 : if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
5817 : : {
5818 : 1319062 : WB (t->decl_with_vis.defer_output);
5819 : 1319062 : WB (t->decl_with_vis.hard_register);
5820 : 1319062 : WB (t->decl_with_vis.common_flag);
5821 : 1319062 : WB (t->decl_with_vis.in_text_section);
5822 : 1319062 : WB (t->decl_with_vis.in_constant_pool);
5823 : 1319062 : WB (t->decl_with_vis.dllimport_flag);
5824 : 1319062 : WB (t->decl_with_vis.weak_flag);
5825 : 1319062 : WB (t->decl_with_vis.seen_in_bind_expr);
5826 : 1319062 : WB (t->decl_with_vis.comdat_flag);
5827 : 1319062 : WB (t->decl_with_vis.visibility_specified);
5828 : 1319062 : WB (t->decl_with_vis.init_priority_p);
5829 : 1319062 : WB (t->decl_with_vis.shadowed_for_var_p);
5830 : 1319062 : WB (t->decl_with_vis.cxx_constructor);
5831 : 1319062 : WB (t->decl_with_vis.cxx_destructor);
5832 : 1319062 : WB (t->decl_with_vis.final);
5833 : 1319062 : WB (t->decl_with_vis.regdecl_flag);
5834 : : }
5835 : : else
5836 : : return;
5837 : :
5838 : 1319062 : if (CODE_CONTAINS_STRUCT (code, TS_FUNCTION_DECL))
5839 : : {
5840 : 379099 : WB (t->function_decl.static_ctor_flag);
5841 : 379099 : WB (t->function_decl.static_dtor_flag);
5842 : 379099 : WB (t->function_decl.uninlinable);
5843 : 379099 : WB (t->function_decl.possibly_inlined);
5844 : 379099 : WB (t->function_decl.novops_flag);
5845 : 379099 : WB (t->function_decl.returns_twice_flag);
5846 : 379099 : WB (t->function_decl.malloc_flag);
5847 : 379099 : WB (t->function_decl.declared_inline_flag);
5848 : 379099 : WB (t->function_decl.no_inline_warning_flag);
5849 : 379099 : WB (t->function_decl.no_instrument_function_entry_exit);
5850 : 379099 : WB (t->function_decl.no_limit_stack);
5851 : 379099 : WB (t->function_decl.disregard_inline_limits);
5852 : 379099 : WB (t->function_decl.pure_flag);
5853 : 379099 : WB (t->function_decl.looping_const_or_pure_flag);
5854 : :
5855 : 379099 : WB (t->function_decl.has_debug_args_flag);
5856 : 379099 : WB (t->function_decl.versioned_function);
5857 : 379099 : WB (t->function_decl.replaceable_operator);
5858 : :
5859 : : /* decl_type is a (misnamed) 2 bit discriminator. */
5860 : 379099 : unsigned kind = (unsigned)t->function_decl.decl_type;
5861 : 379099 : WB ((kind >> 0) & 1);
5862 : 379099 : WB ((kind >> 1) & 1);
5863 : : }
5864 : : #undef WB_IF
5865 : : #undef WB
5866 : : }
5867 : :
5868 : : bool
5869 : 10519066 : trees_in::core_bools (tree t, bits_in& bits)
5870 : : {
5871 : : #define RB(X) ((X) = bits.b ())
5872 : : /* See the comment for WB_IF in trees_out::core_bools. */
5873 : : #define RB_IF(COND, X) ((COND) ? RB (X) : bits.b ())
5874 : :
5875 : 10519066 : tree_code code = TREE_CODE (t);
5876 : :
5877 : 10519066 : RB (t->base.side_effects_flag);
5878 : 10519066 : RB (t->base.constant_flag);
5879 : 10519066 : RB (t->base.addressable_flag);
5880 : 10519066 : RB (t->base.volatile_flag);
5881 : 10519066 : RB (t->base.readonly_flag);
5882 : : /* base.asm_written_flag is not streamed. */
5883 : 10519066 : RB (t->base.nowarning_flag);
5884 : : /* base.visited is not streamed. */
5885 : : /* base.used_flag is not streamed. */
5886 : 10519066 : RB (t->base.nothrow_flag);
5887 : 10519066 : RB (t->base.static_flag);
5888 : 10519066 : RB_IF (TREE_CODE_CLASS (code) != tcc_type, t->base.public_flag);
5889 : 10519066 : RB (t->base.private_flag);
5890 : 10519066 : RB (t->base.protected_flag);
5891 : 10519066 : RB (t->base.deprecated_flag);
5892 : 10519066 : RB (t->base.default_def_flag);
5893 : :
5894 : 10519066 : switch (code)
5895 : : {
5896 : 1441461 : case CALL_EXPR:
5897 : 1441461 : case INTEGER_CST:
5898 : 1441461 : case SSA_NAME:
5899 : 1441461 : case TARGET_MEM_REF:
5900 : 1441461 : case TREE_VEC:
5901 : : /* These use different base.u fields. */
5902 : 1441461 : goto done;
5903 : :
5904 : 9077605 : default:
5905 : 9077605 : RB (t->base.u.bits.lang_flag_0);
5906 : 9077605 : RB (t->base.u.bits.lang_flag_1);
5907 : 9077605 : RB (t->base.u.bits.lang_flag_2);
5908 : 9077605 : RB (t->base.u.bits.lang_flag_3);
5909 : 9077605 : RB (t->base.u.bits.lang_flag_4);
5910 : 9077605 : RB (t->base.u.bits.lang_flag_5);
5911 : 9077605 : RB (t->base.u.bits.lang_flag_6);
5912 : 9077605 : RB (t->base.u.bits.saturating_flag);
5913 : 9077605 : RB (t->base.u.bits.unsigned_flag);
5914 : 9077605 : RB (t->base.u.bits.packed_flag);
5915 : 9077605 : RB (t->base.u.bits.user_align);
5916 : 9077605 : RB (t->base.u.bits.nameless_flag);
5917 : 9077605 : RB (t->base.u.bits.atomic_flag);
5918 : 9077605 : RB (t->base.u.bits.unavailable_flag);
5919 : 9077605 : break;
5920 : : }
5921 : :
5922 : 9077605 : if (TREE_CODE_CLASS (code) == tcc_type)
5923 : : {
5924 : 414420 : RB (t->type_common.no_force_blk_flag);
5925 : 414420 : RB (t->type_common.needs_constructing_flag);
5926 : 414420 : RB (t->type_common.transparent_aggr_flag);
5927 : 414420 : RB (t->type_common.restrict_flag);
5928 : 414420 : RB (t->type_common.string_flag);
5929 : 414420 : RB (t->type_common.lang_flag_0);
5930 : 414420 : RB (t->type_common.lang_flag_1);
5931 : 414420 : RB (t->type_common.lang_flag_2);
5932 : 414420 : RB (t->type_common.lang_flag_3);
5933 : 414420 : RB (t->type_common.lang_flag_4);
5934 : 414420 : RB (t->type_common.lang_flag_5);
5935 : 414420 : RB (t->type_common.lang_flag_6);
5936 : 414420 : RB (t->type_common.typeless_storage);
5937 : : }
5938 : :
5939 : 9077605 : if (TREE_CODE_CLASS (code) != tcc_declaration)
5940 : 6767941 : goto done;
5941 : :
5942 : 2309664 : if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
5943 : : {
5944 : 2309664 : RB (t->decl_common.nonlocal_flag);
5945 : 2309664 : RB (t->decl_common.virtual_flag);
5946 : 2309664 : RB (t->decl_common.ignored_flag);
5947 : 2309664 : RB (t->decl_common.abstract_flag);
5948 : 2309664 : RB (t->decl_common.artificial_flag);
5949 : 2309664 : RB (t->decl_common.preserve_flag);
5950 : 2309664 : RB (t->decl_common.debug_expr_is_from);
5951 : 2309664 : RB (t->decl_common.lang_flag_0);
5952 : 2309664 : RB (t->decl_common.lang_flag_1);
5953 : 2309664 : RB (t->decl_common.lang_flag_2);
5954 : 2309664 : RB (t->decl_common.lang_flag_3);
5955 : 2309664 : RB (t->decl_common.lang_flag_4);
5956 : 2309664 : RB (t->decl_common.lang_flag_5);
5957 : 2309664 : RB (t->decl_common.lang_flag_6);
5958 : 2309664 : RB (t->decl_common.lang_flag_7);
5959 : 2309664 : RB (t->decl_common.lang_flag_8);
5960 : 2309664 : RB (t->decl_common.decl_flag_0);
5961 : 2309664 : RB (t->decl_common.decl_flag_1);
5962 : 2309664 : RB (t->decl_common.decl_flag_2);
5963 : 2309664 : RB (t->decl_common.decl_flag_3);
5964 : 2309664 : RB (t->decl_common.not_gimple_reg_flag);
5965 : 2309664 : RB (t->decl_common.decl_by_reference_flag);
5966 : 2309664 : RB (t->decl_common.decl_read_flag);
5967 : 2309664 : RB (t->decl_common.decl_nonshareable_flag);
5968 : 2309664 : RB (t->decl_common.decl_not_flexarray);
5969 : : }
5970 : : else
5971 : 0 : goto done;
5972 : :
5973 : 2309664 : if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
5974 : : {
5975 : 1075773 : RB (t->decl_with_vis.defer_output);
5976 : 1075773 : RB (t->decl_with_vis.hard_register);
5977 : 1075773 : RB (t->decl_with_vis.common_flag);
5978 : 1075773 : RB (t->decl_with_vis.in_text_section);
5979 : 1075773 : RB (t->decl_with_vis.in_constant_pool);
5980 : 1075773 : RB (t->decl_with_vis.dllimport_flag);
5981 : 1075773 : RB (t->decl_with_vis.weak_flag);
5982 : 1075773 : RB (t->decl_with_vis.seen_in_bind_expr);
5983 : 1075773 : RB (t->decl_with_vis.comdat_flag);
5984 : 1075773 : RB (t->decl_with_vis.visibility_specified);
5985 : 1075773 : RB (t->decl_with_vis.init_priority_p);
5986 : 1075773 : RB (t->decl_with_vis.shadowed_for_var_p);
5987 : 1075773 : RB (t->decl_with_vis.cxx_constructor);
5988 : 1075773 : RB (t->decl_with_vis.cxx_destructor);
5989 : 1075773 : RB (t->decl_with_vis.final);
5990 : 1075773 : RB (t->decl_with_vis.regdecl_flag);
5991 : : }
5992 : : else
5993 : 1233891 : goto done;
5994 : :
5995 : 1075773 : if (CODE_CONTAINS_STRUCT (code, TS_FUNCTION_DECL))
5996 : : {
5997 : 324408 : RB (t->function_decl.static_ctor_flag);
5998 : 324408 : RB (t->function_decl.static_dtor_flag);
5999 : 324408 : RB (t->function_decl.uninlinable);
6000 : 324408 : RB (t->function_decl.possibly_inlined);
6001 : 324408 : RB (t->function_decl.novops_flag);
6002 : 324408 : RB (t->function_decl.returns_twice_flag);
6003 : 324408 : RB (t->function_decl.malloc_flag);
6004 : 324408 : RB (t->function_decl.declared_inline_flag);
6005 : 324408 : RB (t->function_decl.no_inline_warning_flag);
6006 : 324408 : RB (t->function_decl.no_instrument_function_entry_exit);
6007 : 324408 : RB (t->function_decl.no_limit_stack);
6008 : 324408 : RB (t->function_decl.disregard_inline_limits);
6009 : 324408 : RB (t->function_decl.pure_flag);
6010 : 324408 : RB (t->function_decl.looping_const_or_pure_flag);
6011 : :
6012 : 324408 : RB (t->function_decl.has_debug_args_flag);
6013 : 324408 : RB (t->function_decl.versioned_function);
6014 : 324408 : RB (t->function_decl.replaceable_operator);
6015 : :
6016 : : /* decl_type is a (misnamed) 2 bit discriminator. */
6017 : 324408 : unsigned kind = 0;
6018 : 324408 : kind |= unsigned (bits.b ()) << 0;
6019 : 324408 : kind |= unsigned (bits.b ()) << 1;
6020 : 324408 : t->function_decl.decl_type = function_decl_type (kind);
6021 : : }
6022 : : #undef RB_IF
6023 : : #undef RB
6024 : 751365 : done:
6025 : 10519066 : return !get_overrun ();
6026 : : }
6027 : :
6028 : : void
6029 : 1775845 : trees_out::lang_decl_bools (tree t, bits_out& bits)
6030 : : {
6031 : : #define WB(X) (bits.b (X))
6032 : 1775845 : const struct lang_decl *lang = DECL_LANG_SPECIFIC (t);
6033 : :
6034 : 1775845 : bits.bflush ();
6035 : 1775845 : WB (lang->u.base.language == lang_cplusplus);
6036 : 1775845 : WB ((lang->u.base.use_template >> 0) & 1);
6037 : 1775845 : WB ((lang->u.base.use_template >> 1) & 1);
6038 : : /* Do not write lang->u.base.not_really_extern, importer will set
6039 : : when reading the definition (if any). */
6040 : 1775845 : WB (lang->u.base.initialized_in_class);
6041 : 1775845 : WB (lang->u.base.threadprivate_or_deleted_p);
6042 : : /* Do not write lang->u.base.anticipated_p, it is a property of the
6043 : : current TU. */
6044 : 1775845 : WB (lang->u.base.friend_or_tls);
6045 : 1775845 : WB (lang->u.base.unknown_bound_p);
6046 : : /* Do not write lang->u.base.odr_used, importer will recalculate if
6047 : : they do ODR use this decl. */
6048 : 1775845 : WB (lang->u.base.concept_p);
6049 : 1775845 : WB (lang->u.base.var_declared_inline_p);
6050 : 1775845 : WB (lang->u.base.dependent_init_p);
6051 : : /* When building a header unit, everthing is marked as purview, (so
6052 : : we know which decls to write). But when we import them we do not
6053 : : want to mark them as in module purview. */
6054 : 3474135 : WB (lang->u.base.module_purview_p && !header_module_p ());
6055 : 1775845 : WB (lang->u.base.module_attach_p);
6056 : 1775845 : WB (lang->u.base.module_keyed_decls_p);
6057 : 1775845 : switch (lang->u.base.selector)
6058 : : {
6059 : 0 : default:
6060 : 0 : gcc_unreachable ();
6061 : :
6062 : 379099 : case lds_fn: /* lang_decl_fn. */
6063 : 379099 : WB (lang->u.fn.global_ctor_p);
6064 : 379099 : WB (lang->u.fn.global_dtor_p);
6065 : 379099 : WB (lang->u.fn.static_function);
6066 : 379099 : WB (lang->u.fn.pure_virtual);
6067 : 379099 : WB (lang->u.fn.defaulted_p);
6068 : 379099 : WB (lang->u.fn.has_in_charge_parm_p);
6069 : 379099 : WB (lang->u.fn.has_vtt_parm_p);
6070 : : /* There shouldn't be a pending inline at this point. */
6071 : 379099 : gcc_assert (!lang->u.fn.pending_inline_p);
6072 : 379099 : WB (lang->u.fn.nonconverting);
6073 : 379099 : WB (lang->u.fn.thunk_p);
6074 : 379099 : WB (lang->u.fn.this_thunk_p);
6075 : : /* Do not stream lang->u.hidden_friend_p, it is a property of
6076 : : the TU. */
6077 : 379099 : WB (lang->u.fn.omp_declare_reduction_p);
6078 : 379099 : WB (lang->u.fn.has_dependent_explicit_spec_p);
6079 : 379099 : WB (lang->u.fn.immediate_fn_p);
6080 : 379099 : WB (lang->u.fn.maybe_deleted);
6081 : 379099 : WB (lang->u.fn.implicit_constexpr);
6082 : 379099 : WB (lang->u.fn.escalated_p);
6083 : 379099 : WB (lang->u.fn.xobj_func);
6084 : 379099 : goto lds_min;
6085 : :
6086 : 1378 : case lds_decomp: /* lang_decl_decomp. */
6087 : : /* No bools. */
6088 : 1378 : goto lds_min;
6089 : :
6090 : : case lds_min: /* lang_decl_min. */
6091 : 1775845 : lds_min:
6092 : : /* No bools. */
6093 : : break;
6094 : :
6095 : : case lds_ns: /* lang_decl_ns. */
6096 : : /* No bools. */
6097 : : break;
6098 : :
6099 : : case lds_parm: /* lang_decl_parm. */
6100 : : /* No bools. */
6101 : : break;
6102 : : }
6103 : : #undef WB
6104 : 1775845 : }
6105 : :
6106 : : bool
6107 : 1473275 : trees_in::lang_decl_bools (tree t, bits_in& bits)
6108 : : {
6109 : : #define RB(X) ((X) = bits.b ())
6110 : 1473275 : struct lang_decl *lang = DECL_LANG_SPECIFIC (t);
6111 : :
6112 : 1473275 : bits.bflush ();
6113 : 1473275 : lang->u.base.language = bits.b () ? lang_cplusplus : lang_c;
6114 : 1473275 : unsigned v;
6115 : 1473275 : v = bits.b () << 0;
6116 : 1473275 : v |= bits.b () << 1;
6117 : 1473275 : lang->u.base.use_template = v;
6118 : : /* lang->u.base.not_really_extern is not streamed. */
6119 : 1473275 : RB (lang->u.base.initialized_in_class);
6120 : 1473275 : RB (lang->u.base.threadprivate_or_deleted_p);
6121 : : /* lang->u.base.anticipated_p is not streamed. */
6122 : 1473275 : RB (lang->u.base.friend_or_tls);
6123 : 1473275 : RB (lang->u.base.unknown_bound_p);
6124 : : /* lang->u.base.odr_used is not streamed. */
6125 : 1473275 : RB (lang->u.base.concept_p);
6126 : 1473275 : RB (lang->u.base.var_declared_inline_p);
6127 : 1473275 : RB (lang->u.base.dependent_init_p);
6128 : 1473275 : RB (lang->u.base.module_purview_p);
6129 : 1473275 : RB (lang->u.base.module_attach_p);
6130 : 1473275 : RB (lang->u.base.module_keyed_decls_p);
6131 : 1473275 : switch (lang->u.base.selector)
6132 : : {
6133 : 0 : default:
6134 : 0 : gcc_unreachable ();
6135 : :
6136 : 324408 : case lds_fn: /* lang_decl_fn. */
6137 : 324408 : RB (lang->u.fn.global_ctor_p);
6138 : 324408 : RB (lang->u.fn.global_dtor_p);
6139 : 324408 : RB (lang->u.fn.static_function);
6140 : 324408 : RB (lang->u.fn.pure_virtual);
6141 : 324408 : RB (lang->u.fn.defaulted_p);
6142 : 324408 : RB (lang->u.fn.has_in_charge_parm_p);
6143 : 324408 : RB (lang->u.fn.has_vtt_parm_p);
6144 : 324408 : RB (lang->u.fn.nonconverting);
6145 : 324408 : RB (lang->u.fn.thunk_p);
6146 : 324408 : RB (lang->u.fn.this_thunk_p);
6147 : : /* lang->u.fn.hidden_friend_p is not streamed. */
6148 : 324408 : RB (lang->u.fn.omp_declare_reduction_p);
6149 : 324408 : RB (lang->u.fn.has_dependent_explicit_spec_p);
6150 : 324408 : RB (lang->u.fn.immediate_fn_p);
6151 : 324408 : RB (lang->u.fn.maybe_deleted);
6152 : 324408 : RB (lang->u.fn.implicit_constexpr);
6153 : 324408 : RB (lang->u.fn.escalated_p);
6154 : 324408 : RB (lang->u.fn.xobj_func);
6155 : 324408 : goto lds_min;
6156 : :
6157 : 1303 : case lds_decomp: /* lang_decl_decomp. */
6158 : : /* No bools. */
6159 : 1303 : goto lds_min;
6160 : :
6161 : : case lds_min: /* lang_decl_min. */
6162 : 1473275 : lds_min:
6163 : : /* No bools. */
6164 : : break;
6165 : :
6166 : : case lds_ns: /* lang_decl_ns. */
6167 : : /* No bools. */
6168 : : break;
6169 : :
6170 : : case lds_parm: /* lang_decl_parm. */
6171 : : /* No bools. */
6172 : : break;
6173 : : }
6174 : : #undef RB
6175 : 1473275 : return !get_overrun ();
6176 : : }
6177 : :
6178 : : void
6179 : 144140 : trees_out::lang_type_bools (tree t, bits_out& bits)
6180 : : {
6181 : : #define WB(X) (bits.b (X))
6182 : 144140 : const struct lang_type *lang = TYPE_LANG_SPECIFIC (t);
6183 : :
6184 : 144140 : bits.bflush ();
6185 : 144140 : WB (lang->has_type_conversion);
6186 : 144140 : WB (lang->has_copy_ctor);
6187 : 144140 : WB (lang->has_default_ctor);
6188 : 144140 : WB (lang->const_needs_init);
6189 : 144140 : WB (lang->ref_needs_init);
6190 : 144140 : WB (lang->has_const_copy_assign);
6191 : 144140 : WB ((lang->use_template >> 0) & 1);
6192 : 144140 : WB ((lang->use_template >> 1) & 1);
6193 : :
6194 : 144140 : WB (lang->has_mutable);
6195 : 144140 : WB (lang->com_interface);
6196 : 144140 : WB (lang->non_pod_class);
6197 : 144140 : WB (lang->nearly_empty_p);
6198 : 144140 : WB (lang->user_align);
6199 : 144140 : WB (lang->has_copy_assign);
6200 : 144140 : WB (lang->has_new);
6201 : 144140 : WB (lang->has_array_new);
6202 : :
6203 : 144140 : WB ((lang->gets_delete >> 0) & 1);
6204 : 144140 : WB ((lang->gets_delete >> 1) & 1);
6205 : 144140 : WB (lang->interface_only);
6206 : 144140 : WB (lang->interface_unknown);
6207 : 144140 : WB (lang->contains_empty_class_p);
6208 : 144140 : WB (lang->anon_aggr);
6209 : 144140 : WB (lang->non_zero_init);
6210 : 144140 : WB (lang->empty_p);
6211 : :
6212 : 144140 : WB (lang->vec_new_uses_cookie);
6213 : 144140 : WB (lang->declared_class);
6214 : 144140 : WB (lang->diamond_shaped);
6215 : 144140 : WB (lang->repeated_base);
6216 : 144140 : gcc_checking_assert (!lang->being_defined);
6217 : : // lang->debug_requested
6218 : 144140 : WB (lang->fields_readonly);
6219 : 144140 : WB (lang->ptrmemfunc_flag);
6220 : :
6221 : 144140 : WB (lang->lazy_default_ctor);
6222 : 144140 : WB (lang->lazy_copy_ctor);
6223 : 144140 : WB (lang->lazy_copy_assign);
6224 : 144140 : WB (lang->lazy_destructor);
6225 : 144140 : WB (lang->has_const_copy_ctor);
6226 : 144140 : WB (lang->has_complex_copy_ctor);
6227 : 144140 : WB (lang->has_complex_copy_assign);
6228 : 144140 : WB (lang->non_aggregate);
6229 : :
6230 : 144140 : WB (lang->has_complex_dflt);
6231 : 144140 : WB (lang->has_list_ctor);
6232 : 144140 : WB (lang->non_std_layout);
6233 : 144140 : WB (lang->is_literal);
6234 : 144140 : WB (lang->lazy_move_ctor);
6235 : 144140 : WB (lang->lazy_move_assign);
6236 : 144140 : WB (lang->has_complex_move_ctor);
6237 : 144140 : WB (lang->has_complex_move_assign);
6238 : :
6239 : 144140 : WB (lang->has_constexpr_ctor);
6240 : 144140 : WB (lang->unique_obj_representations);
6241 : 144140 : WB (lang->unique_obj_representations_set);
6242 : 144140 : gcc_checking_assert (!lang->erroneous);
6243 : 144140 : WB (lang->non_pod_aggregate);
6244 : 144140 : WB (lang->non_aggregate_pod);
6245 : : #undef WB
6246 : 144140 : }
6247 : :
6248 : : bool
6249 : 112375 : trees_in::lang_type_bools (tree t, bits_in& bits)
6250 : : {
6251 : : #define RB(X) ((X) = bits.b ())
6252 : 112375 : struct lang_type *lang = TYPE_LANG_SPECIFIC (t);
6253 : :
6254 : 112375 : bits.bflush ();
6255 : 112375 : RB (lang->has_type_conversion);
6256 : 112375 : RB (lang->has_copy_ctor);
6257 : 112375 : RB (lang->has_default_ctor);
6258 : 112375 : RB (lang->const_needs_init);
6259 : 112375 : RB (lang->ref_needs_init);
6260 : 112375 : RB (lang->has_const_copy_assign);
6261 : 112375 : unsigned v;
6262 : 112375 : v = bits.b () << 0;
6263 : 112375 : v |= bits.b () << 1;
6264 : 112375 : lang->use_template = v;
6265 : :
6266 : 112375 : RB (lang->has_mutable);
6267 : 112375 : RB (lang->com_interface);
6268 : 112375 : RB (lang->non_pod_class);
6269 : 112375 : RB (lang->nearly_empty_p);
6270 : 112375 : RB (lang->user_align);
6271 : 112375 : RB (lang->has_copy_assign);
6272 : 112375 : RB (lang->has_new);
6273 : 112375 : RB (lang->has_array_new);
6274 : :
6275 : 112375 : v = bits.b () << 0;
6276 : 112375 : v |= bits.b () << 1;
6277 : 112375 : lang->gets_delete = v;
6278 : 112375 : RB (lang->interface_only);
6279 : 112375 : RB (lang->interface_unknown);
6280 : 112375 : RB (lang->contains_empty_class_p);
6281 : 112375 : RB (lang->anon_aggr);
6282 : 112375 : RB (lang->non_zero_init);
6283 : 112375 : RB (lang->empty_p);
6284 : :
6285 : 112375 : RB (lang->vec_new_uses_cookie);
6286 : 112375 : RB (lang->declared_class);
6287 : 112375 : RB (lang->diamond_shaped);
6288 : 112375 : RB (lang->repeated_base);
6289 : 112375 : gcc_checking_assert (!lang->being_defined);
6290 : 112375 : gcc_checking_assert (!lang->debug_requested);
6291 : 112375 : RB (lang->fields_readonly);
6292 : 112375 : RB (lang->ptrmemfunc_flag);
6293 : :
6294 : 112375 : RB (lang->lazy_default_ctor);
6295 : 112375 : RB (lang->lazy_copy_ctor);
6296 : 112375 : RB (lang->lazy_copy_assign);
6297 : 112375 : RB (lang->lazy_destructor);
6298 : 112375 : RB (lang->has_const_copy_ctor);
6299 : 112375 : RB (lang->has_complex_copy_ctor);
6300 : 112375 : RB (lang->has_complex_copy_assign);
6301 : 112375 : RB (lang->non_aggregate);
6302 : :
6303 : 112375 : RB (lang->has_complex_dflt);
6304 : 112375 : RB (lang->has_list_ctor);
6305 : 112375 : RB (lang->non_std_layout);
6306 : 112375 : RB (lang->is_literal);
6307 : 112375 : RB (lang->lazy_move_ctor);
6308 : 112375 : RB (lang->lazy_move_assign);
6309 : 112375 : RB (lang->has_complex_move_ctor);
6310 : 112375 : RB (lang->has_complex_move_assign);
6311 : :
6312 : 112375 : RB (lang->has_constexpr_ctor);
6313 : 112375 : RB (lang->unique_obj_representations);
6314 : 112375 : RB (lang->unique_obj_representations_set);
6315 : 112375 : gcc_checking_assert (!lang->erroneous);
6316 : 112375 : RB (lang->non_pod_aggregate);
6317 : 112375 : RB (lang->non_aggregate_pod);
6318 : : #undef RB
6319 : 112375 : return !get_overrun ();
6320 : : }
6321 : :
6322 : : /* Read & write the core values and pointers. */
6323 : :
6324 : : void
6325 : 31915337 : trees_out::core_vals (tree t)
6326 : : {
6327 : : #define WU(X) (u (X))
6328 : : #define WT(X) (tree_node (X))
6329 : 31915337 : tree_code code = TREE_CODE (t);
6330 : :
6331 : : /* First by shape of the tree. */
6332 : :
6333 : 31915337 : if (CODE_CONTAINS_STRUCT (code, TS_DECL_MINIMAL))
6334 : : {
6335 : : /* Write this early, for better log information. */
6336 : 7218334 : WT (t->decl_minimal.name);
6337 : 7218334 : if (!DECL_TEMPLATE_PARM_P (t))
6338 : 5593869 : WT (t->decl_minimal.context);
6339 : :
6340 : 7218334 : if (state)
6341 : 5953761 : state->write_location (*this, t->decl_minimal.locus);
6342 : :
6343 : 7218334 : if (streaming_p ())
6344 : 2771577 : if (has_warning_spec (t))
6345 : 500 : u (get_warning_spec (t));
6346 : : }
6347 : :
6348 : 31915337 : if (CODE_CONTAINS_STRUCT (code, TS_TYPE_COMMON))
6349 : : {
6350 : : /* The only types we write also have TYPE_NON_COMMON. */
6351 : 1802138 : gcc_checking_assert (CODE_CONTAINS_STRUCT (code, TS_TYPE_NON_COMMON));
6352 : :
6353 : : /* We only stream the main variant. */
6354 : 1802138 : gcc_checking_assert (TYPE_MAIN_VARIANT (t) == t);
6355 : :
6356 : : /* Stream the name & context first, for better log information */
6357 : 1802138 : WT (t->type_common.name);
6358 : 1802138 : WT (t->type_common.context);
6359 : :
6360 : : /* By construction we want to make sure we have the canonical
6361 : : and main variants already in the type table, so emit them
6362 : : now. */
6363 : 1802138 : WT (t->type_common.main_variant);
6364 : :
6365 : 1802138 : tree canonical = t->type_common.canonical;
6366 : 1802138 : if (canonical && DECL_TEMPLATE_PARM_P (TYPE_NAME (t)))
6367 : : /* We do not want to wander into different templates.
6368 : : Reconstructed on stream in. */
6369 : : canonical = t;
6370 : 1802138 : WT (canonical);
6371 : :
6372 : : /* type_common.next_variant is internally manipulated. */
6373 : : /* type_common.pointer_to, type_common.reference_to. */
6374 : :
6375 : 1802138 : if (streaming_p ())
6376 : : {
6377 : 501265 : WU (t->type_common.precision);
6378 : 501265 : WU (t->type_common.contains_placeholder_bits);
6379 : 501265 : WU (t->type_common.mode);
6380 : 501265 : WU (t->type_common.align);
6381 : : }
6382 : :
6383 : 1802138 : if (!RECORD_OR_UNION_CODE_P (code))
6384 : : {
6385 : 1470696 : WT (t->type_common.size);
6386 : 1470696 : WT (t->type_common.size_unit);
6387 : : }
6388 : 1802138 : WT (t->type_common.attributes);
6389 : :
6390 : 1802138 : WT (t->type_common.common.chain); /* TYPE_STUB_DECL. */
6391 : : }
6392 : :
6393 : 31915337 : if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
6394 : : {
6395 : 7218334 : if (streaming_p ())
6396 : : {
6397 : 2771577 : WU (t->decl_common.mode);
6398 : 2771577 : WU (t->decl_common.off_align);
6399 : 2771577 : WU (t->decl_common.align);
6400 : : }
6401 : :
6402 : : /* For templates these hold instantiation (partial and/or
6403 : : specialization) information. */
6404 : 7218334 : if (code != TEMPLATE_DECL)
6405 : : {
6406 : 6655991 : WT (t->decl_common.size);
6407 : 6655991 : WT (t->decl_common.size_unit);
6408 : : }
6409 : :
6410 : 7218334 : WT (t->decl_common.attributes);
6411 : : // FIXME: Does this introduce cross-decl links? For instance
6412 : : // from instantiation to the template. If so, we'll need more
6413 : : // deduplication logic. I think we'll need to walk the blocks
6414 : : // of the owning function_decl's abstract origin in tandem, to
6415 : : // generate the locating data needed?
6416 : 7218334 : WT (t->decl_common.abstract_origin);
6417 : : }
6418 : :
6419 : 31915337 : if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
6420 : : {
6421 : 3447211 : WT (t->decl_with_vis.assembler_name);
6422 : 3447211 : if (streaming_p ())
6423 : 1319062 : WU (t->decl_with_vis.visibility);
6424 : : }
6425 : :
6426 : 31915337 : if (CODE_CONTAINS_STRUCT (code, TS_TYPE_NON_COMMON))
6427 : : {
6428 : 1802138 : if (code == ENUMERAL_TYPE)
6429 : : {
6430 : : /* These fields get set even for opaque enums that lack a
6431 : : definition, so we stream them directly for each ENUMERAL_TYPE.
6432 : : We stream TYPE_VALUES as part of the definition. */
6433 : 7536 : WT (t->type_non_common.maxval);
6434 : 7536 : WT (t->type_non_common.minval);
6435 : : }
6436 : : /* Records and unions hold FIELDS, VFIELD & BINFO on these
6437 : : things. */
6438 : 1794602 : else if (!RECORD_OR_UNION_CODE_P (code))
6439 : : {
6440 : : // FIXME: These are from tpl_parm_value's 'type' writing.
6441 : : // Perhaps it should just be doing them directly?
6442 : 1463160 : gcc_checking_assert (code == TEMPLATE_TYPE_PARM
6443 : : || code == TEMPLATE_TEMPLATE_PARM
6444 : : || code == BOUND_TEMPLATE_TEMPLATE_PARM);
6445 : 1463160 : gcc_checking_assert (!TYPE_CACHED_VALUES_P (t));
6446 : 1463160 : WT (t->type_non_common.values);
6447 : 1463160 : WT (t->type_non_common.maxval);
6448 : 1463160 : WT (t->type_non_common.minval);
6449 : : }
6450 : :
6451 : 1802138 : WT (t->type_non_common.lang_1);
6452 : : }
6453 : :
6454 : 31915337 : if (CODE_CONTAINS_STRUCT (code, TS_EXP))
6455 : : {
6456 : 9394622 : if (state)
6457 : 9196890 : state->write_location (*this, t->exp.locus);
6458 : :
6459 : 9394622 : if (streaming_p ())
6460 : 4553159 : if (has_warning_spec (t))
6461 : 363185 : u (get_warning_spec (t));
6462 : :
6463 : : /* Walk in forward order, as (for instance) REQUIRES_EXPR has a
6464 : : bunch of unscoped parms on its first operand. It's safer to
6465 : : create those in order. */
6466 : 9394622 : bool vl = TREE_CODE_CLASS (code) == tcc_vl_exp;
6467 : 26096239 : for (unsigned limit = (vl ? VL_EXP_OPERAND_LENGTH (t)
6468 : 9394622 : : TREE_OPERAND_LENGTH (t)),
6469 : 26096239 : ix = unsigned (vl); ix != limit; ix++)
6470 : 16701617 : WT (TREE_OPERAND (t, ix));
6471 : : }
6472 : : else
6473 : : /* The CODE_CONTAINS tables were inaccurate when I started. */
6474 : 22520715 : gcc_checking_assert (TREE_CODE_CLASS (code) != tcc_expression
6475 : : && TREE_CODE_CLASS (code) != tcc_binary
6476 : : && TREE_CODE_CLASS (code) != tcc_unary
6477 : : && TREE_CODE_CLASS (code) != tcc_reference
6478 : : && TREE_CODE_CLASS (code) != tcc_comparison
6479 : : && TREE_CODE_CLASS (code) != tcc_statement
6480 : : && TREE_CODE_CLASS (code) != tcc_vl_exp);
6481 : :
6482 : : /* Then by CODE. Special cases and/or 1:1 tree shape
6483 : : correspondance. */
6484 : 31915337 : switch (code)
6485 : : {
6486 : : default:
6487 : : break;
6488 : :
6489 : 0 : case ARGUMENT_PACK_SELECT: /* Transient during instantiation. */
6490 : 0 : case DEFERRED_PARSE: /* Expanded upon completion of
6491 : : outermost class. */
6492 : 0 : case IDENTIFIER_NODE: /* Streamed specially. */
6493 : 0 : case BINDING_VECTOR: /* Only in namespace-scope symbol
6494 : : table. */
6495 : 0 : case SSA_NAME:
6496 : 0 : case TRANSLATION_UNIT_DECL: /* There is only one, it is a
6497 : : global_tree. */
6498 : 0 : case USERDEF_LITERAL: /* Expanded during parsing. */
6499 : 0 : gcc_unreachable (); /* Should never meet. */
6500 : :
6501 : : /* Constants. */
6502 : 18 : case COMPLEX_CST:
6503 : 18 : WT (TREE_REALPART (t));
6504 : 18 : WT (TREE_IMAGPART (t));
6505 : 18 : break;
6506 : :
6507 : 0 : case FIXED_CST:
6508 : 0 : gcc_unreachable (); /* Not supported in C++. */
6509 : :
6510 : 3049203 : case INTEGER_CST:
6511 : 3049203 : if (streaming_p ())
6512 : : {
6513 : 562901 : unsigned num = TREE_INT_CST_EXT_NUNITS (t);
6514 : 1127458 : for (unsigned ix = 0; ix != num; ix++)
6515 : 564557 : wu (TREE_INT_CST_ELT (t, ix));
6516 : : }
6517 : : break;
6518 : :
6519 : 0 : case POLY_INT_CST:
6520 : 0 : if (streaming_p ())
6521 : 0 : for (unsigned ix = 0; ix != NUM_POLY_INT_COEFFS; ix++)
6522 : 0 : WT (POLY_INT_CST_COEFF (t, ix));
6523 : : break;
6524 : :
6525 : 28880 : case REAL_CST:
6526 : 28880 : if (streaming_p ())
6527 : 14416 : buf (TREE_REAL_CST_PTR (t), sizeof (real_value));
6528 : : break;
6529 : :
6530 : : case STRING_CST:
6531 : : /* Streamed during start. */
6532 : : break;
6533 : :
6534 : 36 : case RAW_DATA_CST:
6535 : 36 : if (RAW_DATA_OWNER (t) == NULL_TREE)
6536 : : break; /* Streamed as STRING_CST during start. */
6537 : 24 : WT (RAW_DATA_OWNER (t));
6538 : 24 : if (streaming_p ())
6539 : : {
6540 : 12 : if (TREE_CODE (RAW_DATA_OWNER (t)) == RAW_DATA_CST)
6541 : 6 : z (RAW_DATA_POINTER (t) - RAW_DATA_POINTER (RAW_DATA_OWNER (t)));
6542 : 6 : else if (TREE_CODE (RAW_DATA_OWNER (t)) == STRING_CST)
6543 : 6 : z (RAW_DATA_POINTER (t)
6544 : 6 : - TREE_STRING_POINTER (RAW_DATA_OWNER (t)));
6545 : : else
6546 : 0 : gcc_unreachable ();
6547 : : }
6548 : : break;
6549 : :
6550 : 36 : case VECTOR_CST:
6551 : 102 : for (unsigned ix = vector_cst_encoded_nelts (t); ix--;)
6552 : 66 : WT (VECTOR_CST_ENCODED_ELT (t, ix));
6553 : : break;
6554 : :
6555 : : /* Decls. */
6556 : 384700 : case VAR_DECL:
6557 : 384700 : if (DECL_CONTEXT (t)
6558 : 384700 : && TREE_CODE (DECL_CONTEXT (t)) != FUNCTION_DECL)
6559 : : {
6560 : 103892 : if (DECL_HAS_VALUE_EXPR_P (t))
6561 : 18 : WT (DECL_VALUE_EXPR (t));
6562 : : break;
6563 : : }
6564 : : /* FALLTHROUGH */
6565 : :
6566 : 3210959 : case RESULT_DECL:
6567 : 3210959 : case PARM_DECL:
6568 : 3210959 : if (DECL_HAS_VALUE_EXPR_P (t))
6569 : 25824 : WT (DECL_VALUE_EXPR (t));
6570 : : /* FALLTHROUGH */
6571 : :
6572 : 3350141 : case CONST_DECL:
6573 : 3350141 : case IMPORTED_DECL:
6574 : 3350141 : WT (t->decl_common.initial);
6575 : 3350141 : break;
6576 : :
6577 : 98149 : case FIELD_DECL:
6578 : 98149 : WT (t->field_decl.offset);
6579 : 98149 : WT (t->field_decl.bit_field_type);
6580 : 98149 : {
6581 : 98149 : auto ovr = make_temp_override (walking_bit_field_unit, true);
6582 : 98149 : WT (t->field_decl.qualifier); /* bitfield unit. */
6583 : 98149 : }
6584 : 98149 : WT (t->field_decl.bit_offset);
6585 : 98149 : WT (t->field_decl.fcontext);
6586 : 98149 : WT (t->decl_common.initial);
6587 : 98149 : break;
6588 : :
6589 : 29680 : case LABEL_DECL:
6590 : 29680 : if (streaming_p ())
6591 : : {
6592 : 14840 : WU (t->label_decl.label_decl_uid);
6593 : 14840 : WU (t->label_decl.eh_landing_pad_nr);
6594 : : }
6595 : : break;
6596 : :
6597 : 758379 : case FUNCTION_DECL:
6598 : 758379 : if (streaming_p ())
6599 : : {
6600 : : /* Builtins can be streamed by value when a header declares
6601 : : them. */
6602 : 379099 : WU (DECL_BUILT_IN_CLASS (t));
6603 : 379099 : if (DECL_BUILT_IN_CLASS (t) != NOT_BUILT_IN)
6604 : 7651 : WU (DECL_UNCHECKED_FUNCTION_CODE (t));
6605 : : }
6606 : :
6607 : 758379 : WT (t->function_decl.personality);
6608 : : /* Rather than streaming target/optimize nodes, we should reconstruct
6609 : : them on stream-in from any attributes applied to the function. */
6610 : 758379 : if (streaming_p () && t->function_decl.function_specific_target)
6611 : 0 : warning_at (DECL_SOURCE_LOCATION (t), 0,
6612 : : "%<target%> attribute currently unsupported in modules");
6613 : 758379 : if (streaming_p () && t->function_decl.function_specific_optimization)
6614 : 3 : warning_at (DECL_SOURCE_LOCATION (t), 0,
6615 : : "%<optimize%> attribute currently unsupported in modules");
6616 : 758379 : WT (t->function_decl.vindex);
6617 : :
6618 : 758379 : if (DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (t))
6619 : 4724 : WT (lookup_explicit_specifier (t));
6620 : : break;
6621 : :
6622 : 93363 : case USING_DECL:
6623 : : /* USING_DECL_DECLS */
6624 : 93363 : WT (t->decl_common.initial);
6625 : : /* FALLTHROUGH */
6626 : :
6627 : 2303899 : case TYPE_DECL:
6628 : : /* USING_DECL: USING_DECL_SCOPE */
6629 : : /* TYPE_DECL: DECL_ORIGINAL_TYPE */
6630 : 2303899 : WT (t->decl_non_common.result);
6631 : 2303899 : break;
6632 : :
6633 : : /* Miscellaneous common nodes. */
6634 : 485340 : case BLOCK:
6635 : 485340 : if (state)
6636 : : {
6637 : 485340 : state->write_location (*this, t->block.locus);
6638 : 485340 : state->write_location (*this, t->block.end_locus);
6639 : : }
6640 : :
6641 : : /* DECL_LOCAL_DECL_P decls are first encountered here and
6642 : : streamed by value. */
6643 : 721798 : for (tree decls = t->block.vars; decls; decls = DECL_CHAIN (decls))
6644 : : {
6645 : 236458 : if (VAR_OR_FUNCTION_DECL_P (decls)
6646 : 236458 : && DECL_LOCAL_DECL_P (decls))
6647 : : {
6648 : : /* Make sure this is the first encounter, and mark for
6649 : : walk-by-value. */
6650 : 236 : gcc_checking_assert (!TREE_VISITED (decls)
6651 : : && !DECL_TEMPLATE_INFO (decls));
6652 : 236 : mark_by_value (decls);
6653 : : }
6654 : 236458 : tree_node (decls);
6655 : : }
6656 : 485340 : tree_node (NULL_TREE);
6657 : :
6658 : : /* nonlocalized_vars is a middle-end thing. */
6659 : 485340 : WT (t->block.subblocks);
6660 : 485340 : WT (t->block.supercontext);
6661 : : // FIXME: As for decl's abstract_origin, does this introduce crosslinks?
6662 : 485340 : WT (t->block.abstract_origin);
6663 : : /* fragment_origin, fragment_chain are middle-end things. */
6664 : 485340 : WT (t->block.chain);
6665 : : /* nonlocalized_vars, block_num & die are middle endy/debug
6666 : : things. */
6667 : 485340 : break;
6668 : :
6669 : 942109 : case CALL_EXPR:
6670 : 942109 : if (streaming_p ())
6671 : 448955 : WU (t->base.u.ifn);
6672 : : break;
6673 : :
6674 : : case CONSTRUCTOR:
6675 : : // This must be streamed /after/ we've streamed the type,
6676 : : // because it can directly refer to elements of the type. Eg,
6677 : : // FIELD_DECLs of a RECORD_TYPE.
6678 : : break;
6679 : :
6680 : 24 : case OMP_CLAUSE:
6681 : 24 : {
6682 : : /* The ompcode is serialized in start. */
6683 : 24 : if (streaming_p ())
6684 : 12 : WU (t->omp_clause.subcode.map_kind);
6685 : 24 : if (state)
6686 : 24 : state->write_location (*this, t->omp_clause.locus);
6687 : :
6688 : 24 : unsigned len = omp_clause_num_ops[OMP_CLAUSE_CODE (t)];
6689 : 78 : for (unsigned ix = 0; ix != len; ix++)
6690 : 54 : WT (t->omp_clause.ops[ix]);
6691 : : }
6692 : : break;
6693 : :
6694 : 338516 : case STATEMENT_LIST:
6695 : 1343495 : for (tree stmt : tsi_range (t))
6696 : 1004979 : if (stmt)
6697 : 1004979 : WT (stmt);
6698 : 338516 : WT (NULL_TREE);
6699 : 338516 : break;
6700 : :
6701 : 0 : case OPTIMIZATION_NODE:
6702 : 0 : case TARGET_OPTION_NODE:
6703 : : // FIXME: Our representation for these two nodes is a cache of
6704 : : // the resulting set of options. Not a record of the options
6705 : : // that got changed by a particular attribute or pragma. Instead
6706 : : // of recording that, we probably should just rebuild the options
6707 : : // on stream-in from the function attributes. This could introduce
6708 : : // strangeness if the importer has some incompatible set of flags
6709 : : // but we currently assume users "know what they're doing" in such
6710 : : // a case anyway.
6711 : 0 : gcc_unreachable ();
6712 : 194824 : break;
6713 : :
6714 : 194824 : case TREE_BINFO:
6715 : 194824 : {
6716 : 194824 : WT (t->binfo.common.chain);
6717 : 194824 : WT (t->binfo.offset);
6718 : 194824 : WT (t->binfo.inheritance);
6719 : 194824 : WT (t->binfo.vptr_field);
6720 : :
6721 : 194824 : WT (t->binfo.vtable);
6722 : 194824 : WT (t->binfo.virtuals);
6723 : 194824 : WT (t->binfo.vtt_subvtt);
6724 : 194824 : WT (t->binfo.vtt_vptr);
6725 : :
6726 : 194824 : tree_vec (BINFO_BASE_ACCESSES (t));
6727 : 194824 : unsigned num = vec_safe_length (BINFO_BASE_ACCESSES (t));
6728 : 249874 : for (unsigned ix = 0; ix != num; ix++)
6729 : 55050 : WT (BINFO_BASE_BINFO (t, ix));
6730 : : }
6731 : : break;
6732 : :
6733 : 2493196 : case TREE_LIST:
6734 : 2493196 : WT (t->list.purpose);
6735 : 2493196 : WT (t->list.value);
6736 : 2493196 : WT (t->list.common.chain);
6737 : 2493196 : break;
6738 : :
6739 : 2124754 : case TREE_VEC:
6740 : 5816669 : for (unsigned ix = TREE_VEC_LENGTH (t); ix--;)
6741 : 3691915 : WT (TREE_VEC_ELT (t, ix));
6742 : : /* We stash NON_DEFAULT_TEMPLATE_ARGS_COUNT on TREE_CHAIN! */
6743 : 2124754 : gcc_checking_assert (!t->type_common.common.chain
6744 : : || (TREE_CODE (t->type_common.common.chain)
6745 : : == INTEGER_CST));
6746 : 2124754 : WT (t->type_common.common.chain);
6747 : 2124754 : break;
6748 : :
6749 : : /* C++-specific nodes ... */
6750 : 166994 : case BASELINK:
6751 : 166994 : WT (((lang_tree_node *)t)->baselink.binfo);
6752 : 166994 : WT (((lang_tree_node *)t)->baselink.functions);
6753 : 166994 : WT (((lang_tree_node *)t)->baselink.access_binfo);
6754 : 166994 : WT (((lang_tree_node *)t)->baselink.common.chain);
6755 : 166994 : break;
6756 : :
6757 : 72432 : case CONSTRAINT_INFO:
6758 : 72432 : WT (((lang_tree_node *)t)->constraint_info.template_reqs);
6759 : 72432 : WT (((lang_tree_node *)t)->constraint_info.declarator_reqs);
6760 : 72432 : WT (((lang_tree_node *)t)->constraint_info.associated_constr);
6761 : 72432 : break;
6762 : :
6763 : 11474 : case DEFERRED_NOEXCEPT:
6764 : 11474 : WT (((lang_tree_node *)t)->deferred_noexcept.pattern);
6765 : 11474 : WT (((lang_tree_node *)t)->deferred_noexcept.args);
6766 : 11474 : break;
6767 : :
6768 : 9936 : case LAMBDA_EXPR:
6769 : 9936 : WT (((lang_tree_node *)t)->lambda_expression.capture_list);
6770 : 9936 : WT (((lang_tree_node *)t)->lambda_expression.this_capture);
6771 : 9936 : WT (((lang_tree_node *)t)->lambda_expression.extra_scope);
6772 : 9936 : WT (((lang_tree_node *)t)->lambda_expression.regen_info);
6773 : 9936 : WT (((lang_tree_node *)t)->lambda_expression.extra_args);
6774 : : /* pending_proxies is a parse-time thing. */
6775 : 9936 : gcc_assert (!((lang_tree_node *)t)->lambda_expression.pending_proxies);
6776 : 9936 : if (state)
6777 : 9681 : state->write_location
6778 : 9681 : (*this, ((lang_tree_node *)t)->lambda_expression.locus);
6779 : 9936 : if (streaming_p ())
6780 : : {
6781 : 3309 : WU (((lang_tree_node *)t)->lambda_expression.default_capture_mode);
6782 : 3309 : WU (((lang_tree_node *)t)->lambda_expression.discriminator_scope);
6783 : 3309 : WU (((lang_tree_node *)t)->lambda_expression.discriminator_sig);
6784 : : }
6785 : : break;
6786 : :
6787 : 1483407 : case OVERLOAD:
6788 : 1483407 : WT (((lang_tree_node *)t)->overload.function);
6789 : 1483407 : WT (t->common.chain);
6790 : 1483407 : break;
6791 : :
6792 : 0 : case PTRMEM_CST:
6793 : 0 : WT (((lang_tree_node *)t)->ptrmem.member);
6794 : 0 : break;
6795 : :
6796 : 13398 : case STATIC_ASSERT:
6797 : 13398 : WT (((lang_tree_node *)t)->static_assertion.condition);
6798 : 13398 : WT (((lang_tree_node *)t)->static_assertion.message);
6799 : 13398 : if (state)
6800 : 13398 : state->write_location
6801 : 13398 : (*this, ((lang_tree_node *)t)->static_assertion.location);
6802 : : break;
6803 : :
6804 : 562343 : case TEMPLATE_DECL:
6805 : : /* Streamed with the template_decl node itself. */
6806 : 562343 : gcc_checking_assert
6807 : : (TREE_VISITED (((lang_tree_node *)t)->template_decl.arguments));
6808 : 562343 : gcc_checking_assert
6809 : : (TREE_VISITED (((lang_tree_node *)t)->template_decl.result));
6810 : 562343 : if (DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (t))
6811 : 9976 : WT (DECL_CHAIN (t));
6812 : : break;
6813 : :
6814 : 1261605 : case TEMPLATE_INFO:
6815 : 1261605 : {
6816 : 1261605 : WT (((lang_tree_node *)t)->template_info.tmpl);
6817 : 1261605 : WT (((lang_tree_node *)t)->template_info.args);
6818 : 1261605 : WT (((lang_tree_node *)t)->template_info.partial);
6819 : :
6820 : 1261605 : const auto *ac = (((lang_tree_node *)t)
6821 : : ->template_info.deferred_access_checks);
6822 : 1261605 : unsigned len = vec_safe_length (ac);
6823 : 1261605 : if (streaming_p ())
6824 : 629336 : u (len);
6825 : 1261605 : if (len)
6826 : : {
6827 : 0 : for (unsigned ix = 0; ix != len; ix++)
6828 : : {
6829 : 0 : const auto &m = (*ac)[ix];
6830 : 0 : WT (m.binfo);
6831 : 0 : WT (m.decl);
6832 : 0 : WT (m.diag_decl);
6833 : 0 : if (state)
6834 : 0 : state->write_location (*this, m.loc);
6835 : : }
6836 : : }
6837 : : }
6838 : : break;
6839 : :
6840 : 1566684 : case TEMPLATE_PARM_INDEX:
6841 : 1566684 : if (streaming_p ())
6842 : : {
6843 : 352375 : WU (((lang_tree_node *)t)->tpi.index);
6844 : 352375 : WU (((lang_tree_node *)t)->tpi.level);
6845 : 352375 : WU (((lang_tree_node *)t)->tpi.orig_level);
6846 : : }
6847 : 1566684 : WT (((lang_tree_node *)t)->tpi.decl);
6848 : : /* TEMPLATE_PARM_DESCENDANTS (AKA TREE_CHAIN) is an internal
6849 : : cache, do not stream. */
6850 : 1566684 : break;
6851 : :
6852 : 24721 : case TRAIT_EXPR:
6853 : 24721 : WT (((lang_tree_node *)t)->trait_expression.type1);
6854 : 24721 : WT (((lang_tree_node *)t)->trait_expression.type2);
6855 : 24721 : if (streaming_p ())
6856 : 9511 : WU (((lang_tree_node *)t)->trait_expression.kind);
6857 : : break;
6858 : :
6859 : 4 : case TU_LOCAL_ENTITY:
6860 : 4 : WT (((lang_tree_node *)t)->tu_local_entity.name);
6861 : 4 : if (state)
6862 : 4 : state->write_location
6863 : 4 : (*this, ((lang_tree_node *)t)->tu_local_entity.loc);
6864 : : break;
6865 : : }
6866 : :
6867 : 31915337 : if (CODE_CONTAINS_STRUCT (code, TS_TYPED))
6868 : : {
6869 : : /* We want to stream the type of a expression-like nodes /after/
6870 : : we've streamed the operands. The type often contains (bits
6871 : : of the) types of the operands, and with things like decltype
6872 : : and noexcept in play, we really want to stream the decls
6873 : : defining the type before we try and stream the type on its
6874 : : own. Otherwise we can find ourselves trying to read in a
6875 : : decl, when we're already partially reading in a component of
6876 : : its type. And that's bad. */
6877 : 30071084 : tree type = t->typed.type;
6878 : 30071084 : unsigned prec = 0;
6879 : :
6880 : 30071084 : switch (code)
6881 : : {
6882 : : default:
6883 : : break;
6884 : :
6885 : : case TEMPLATE_DECL:
6886 : : /* We fill in the template's type separately. */
6887 : 30071084 : type = NULL_TREE;
6888 : : break;
6889 : :
6890 : 2210536 : case TYPE_DECL:
6891 : 2210536 : if (DECL_ORIGINAL_TYPE (t) && t == TYPE_NAME (type))
6892 : : /* This is a typedef. We set its type separately. */
6893 : : type = NULL_TREE;
6894 : : break;
6895 : :
6896 : 7536 : case ENUMERAL_TYPE:
6897 : 7536 : if (type && !ENUM_FIXED_UNDERLYING_TYPE_P (t))
6898 : : {
6899 : : /* Type is a restricted range integer type derived from the
6900 : : integer_types. Find the right one. */
6901 : 4808 : prec = TYPE_PRECISION (type);
6902 : 4808 : tree name = DECL_NAME (TYPE_NAME (type));
6903 : :
6904 : 62828 : for (unsigned itk = itk_none; itk--;)
6905 : 62828 : if (integer_types[itk]
6906 : 62828 : && DECL_NAME (TYPE_NAME (integer_types[itk])) == name)
6907 : : {
6908 : : type = integer_types[itk];
6909 : : break;
6910 : : }
6911 : 4808 : gcc_assert (type != t->typed.type);
6912 : : }
6913 : : break;
6914 : : }
6915 : :
6916 : 30071084 : WT (type);
6917 : 30071084 : if (prec && streaming_p ())
6918 : 2402 : WU (prec);
6919 : : }
6920 : :
6921 : 31915337 : if (TREE_CODE (t) == CONSTRUCTOR)
6922 : : {
6923 : 85943 : unsigned len = vec_safe_length (t->constructor.elts);
6924 : 85943 : if (streaming_p ())
6925 : 42307 : WU (len);
6926 : 85943 : if (len)
6927 : 326862 : for (unsigned ix = 0; ix != len; ix++)
6928 : : {
6929 : 279720 : const constructor_elt &elt = (*t->constructor.elts)[ix];
6930 : :
6931 : 279720 : WT (elt.index);
6932 : 279720 : WT (elt.value);
6933 : : }
6934 : : }
6935 : :
6936 : : #undef WT
6937 : : #undef WU
6938 : 31915337 : }
6939 : :
6940 : : // Streaming in a reference to a decl can cause that decl to be
6941 : : // TREE_USED, which is the mark_used behaviour we need most of the
6942 : : // time. The trees_in::unused can be incremented to inhibit this,
6943 : : // which is at least needed for vtables.
6944 : :
6945 : : bool
6946 : 10503437 : trees_in::core_vals (tree t)
6947 : : {
6948 : : #define RU(X) ((X) = u ())
6949 : : #define RUC(T,X) ((X) = T (u ()))
6950 : : #define RT(X) ((X) = tree_node ())
6951 : : #define RTU(X) ((X) = tree_node (true))
6952 : 10503437 : tree_code code = TREE_CODE (t);
6953 : :
6954 : : /* First by tree shape. */
6955 : 10503437 : if (CODE_CONTAINS_STRUCT (code, TS_DECL_MINIMAL))
6956 : : {
6957 : 2309664 : RT (t->decl_minimal.name);
6958 : 2309664 : if (!DECL_TEMPLATE_PARM_P (t))
6959 : 2010710 : RT (t->decl_minimal.context);
6960 : :
6961 : : /* Don't zap the locus just yet, we don't record it correctly
6962 : : and thus lose all location information. */
6963 : 2309664 : t->decl_minimal.locus = state->read_location (*this);
6964 : 2309664 : if (has_warning_spec (t))
6965 : 381 : put_warning_spec (t, u ());
6966 : : }
6967 : :
6968 : 10503437 : if (CODE_CONTAINS_STRUCT (code, TS_TYPE_COMMON))
6969 : : {
6970 : 398791 : RT (t->type_common.name);
6971 : 398791 : RT (t->type_common.context);
6972 : :
6973 : 398791 : RT (t->type_common.main_variant);
6974 : 398791 : RT (t->type_common.canonical);
6975 : :
6976 : : /* type_common.next_variant is internally manipulated. */
6977 : : /* type_common.pointer_to, type_common.reference_to. */
6978 : :
6979 : 398791 : RU (t->type_common.precision);
6980 : 398791 : RU (t->type_common.contains_placeholder_bits);
6981 : 398791 : RUC (machine_mode, t->type_common.mode);
6982 : 398791 : RU (t->type_common.align);
6983 : :
6984 : 398791 : if (!RECORD_OR_UNION_CODE_P (code))
6985 : : {
6986 : 271258 : RT (t->type_common.size);
6987 : 271258 : RT (t->type_common.size_unit);
6988 : : }
6989 : 398791 : RT (t->type_common.attributes);
6990 : :
6991 : 398791 : RT (t->type_common.common.chain); /* TYPE_STUB_DECL. */
6992 : : }
6993 : :
6994 : 10503437 : if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
6995 : : {
6996 : 2309664 : RUC (machine_mode, t->decl_common.mode);
6997 : 2309664 : RU (t->decl_common.off_align);
6998 : 2309664 : RU (t->decl_common.align);
6999 : :
7000 : 2309664 : if (code != TEMPLATE_DECL)
7001 : : {
7002 : 2076833 : RT (t->decl_common.size);
7003 : 2076833 : RT (t->decl_common.size_unit);
7004 : : }
7005 : :
7006 : 2309664 : RT (t->decl_common.attributes);
7007 : 2309664 : RT (t->decl_common.abstract_origin);
7008 : : }
7009 : :
7010 : 10503437 : if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
7011 : : {
7012 : 1075773 : RT (t->decl_with_vis.assembler_name);
7013 : 1075773 : RUC (symbol_visibility, t->decl_with_vis.visibility);
7014 : : }
7015 : :
7016 : 10503437 : if (CODE_CONTAINS_STRUCT (code, TS_TYPE_NON_COMMON))
7017 : : {
7018 : 398791 : if (code == ENUMERAL_TYPE)
7019 : : {
7020 : : /* These fields get set even for opaque enums that lack a
7021 : : definition, so we stream them directly for each ENUMERAL_TYPE.
7022 : : We stream TYPE_VALUES as part of the definition. */
7023 : 2475 : RT (t->type_non_common.maxval);
7024 : 2475 : RT (t->type_non_common.minval);
7025 : : }
7026 : : /* Records and unions hold FIELDS, VFIELD & BINFO on these
7027 : : things. */
7028 : 396316 : else if (!RECORD_OR_UNION_CODE_P (code))
7029 : : {
7030 : : /* This is not clobbering TYPE_CACHED_VALUES, because this
7031 : : is a type that doesn't have any. */
7032 : 268783 : gcc_checking_assert (!TYPE_CACHED_VALUES_P (t));
7033 : 268783 : RT (t->type_non_common.values);
7034 : 268783 : RT (t->type_non_common.maxval);
7035 : 268783 : RT (t->type_non_common.minval);
7036 : : }
7037 : :
7038 : 398791 : RT (t->type_non_common.lang_1);
7039 : : }
7040 : :
7041 : 10503437 : if (CODE_CONTAINS_STRUCT (code, TS_EXP))
7042 : : {
7043 : 3994568 : t->exp.locus = state->read_location (*this);
7044 : 3994568 : if (has_warning_spec (t))
7045 : 318831 : put_warning_spec (t, u ());
7046 : :
7047 : 3994568 : bool vl = TREE_CODE_CLASS (code) == tcc_vl_exp;
7048 : 3994568 : for (unsigned limit = (vl ? VL_EXP_OPERAND_LENGTH (t)
7049 : 3994568 : : TREE_OPERAND_LENGTH (t)),
7050 : 11085170 : ix = unsigned (vl); ix != limit; ix++)
7051 : 7090602 : RTU (TREE_OPERAND (t, ix));
7052 : : }
7053 : :
7054 : : /* Then by CODE. Special cases and/or 1:1 tree shape
7055 : : correspondance. */
7056 : 10503437 : switch (code)
7057 : : {
7058 : : default:
7059 : : break;
7060 : :
7061 : : case ARGUMENT_PACK_SELECT:
7062 : : case DEFERRED_PARSE:
7063 : : case IDENTIFIER_NODE:
7064 : : case BINDING_VECTOR:
7065 : : case SSA_NAME:
7066 : : case TRANSLATION_UNIT_DECL:
7067 : : case USERDEF_LITERAL:
7068 : : return false; /* Should never meet. */
7069 : :
7070 : : /* Constants. */
7071 : 9 : case COMPLEX_CST:
7072 : 9 : RT (TREE_REALPART (t));
7073 : 9 : RT (TREE_IMAGPART (t));
7074 : 9 : break;
7075 : :
7076 : : case FIXED_CST:
7077 : : /* Not suported in C++. */
7078 : : return false;
7079 : :
7080 : 470374 : case INTEGER_CST:
7081 : 470374 : {
7082 : 470374 : unsigned num = TREE_INT_CST_EXT_NUNITS (t);
7083 : 942039 : for (unsigned ix = 0; ix != num; ix++)
7084 : 471665 : TREE_INT_CST_ELT (t, ix) = wu ();
7085 : : }
7086 : : break;
7087 : :
7088 : : case POLY_INT_CST:
7089 : 0 : for (unsigned ix = 0; ix != NUM_POLY_INT_COEFFS; ix++)
7090 : 0 : RT (POLY_INT_CST_COEFF (t, ix));
7091 : : break;
7092 : :
7093 : 13938 : case REAL_CST:
7094 : 13938 : if (const void *bytes = buf (sizeof (real_value)))
7095 : 13938 : memcpy (TREE_REAL_CST_PTR (t), bytes, sizeof (real_value));
7096 : : break;
7097 : :
7098 : : case STRING_CST:
7099 : : /* Streamed during start. */
7100 : : break;
7101 : :
7102 : 6 : case RAW_DATA_CST:
7103 : 6 : RT (RAW_DATA_OWNER (t));
7104 : 6 : gcc_assert (TREE_CODE (RAW_DATA_OWNER (t)) == STRING_CST
7105 : : && TREE_STRING_LENGTH (RAW_DATA_OWNER (t)));
7106 : 6 : RAW_DATA_POINTER (t) = TREE_STRING_POINTER (RAW_DATA_OWNER (t)) + z ();
7107 : 6 : break;
7108 : :
7109 : 24 : case VECTOR_CST:
7110 : 63 : for (unsigned ix = vector_cst_encoded_nelts (t); ix--;)
7111 : 39 : RT (VECTOR_CST_ENCODED_ELT (t, ix));
7112 : : break;
7113 : :
7114 : : /* Decls. */
7115 : 157084 : case VAR_DECL:
7116 : 157084 : if (DECL_CONTEXT (t)
7117 : 157084 : && TREE_CODE (DECL_CONTEXT (t)) != FUNCTION_DECL)
7118 : : {
7119 : 33743 : if (DECL_HAS_VALUE_EXPR_P (t))
7120 : : {
7121 : 9 : tree val = tree_node ();
7122 : 9 : SET_DECL_VALUE_EXPR (t, val);
7123 : : }
7124 : : break;
7125 : : }
7126 : : /* FALLTHROUGH */
7127 : :
7128 : 1033713 : case RESULT_DECL:
7129 : 1033713 : case PARM_DECL:
7130 : 1033713 : if (DECL_HAS_VALUE_EXPR_P (t))
7131 : : {
7132 : : /* The DECL_VALUE hash table is a cache, thus if we're
7133 : : reading a duplicate (which we end up discarding), the
7134 : : value expr will also be cleaned up at the next gc. */
7135 : 10174 : tree val = tree_node ();
7136 : 10174 : SET_DECL_VALUE_EXPR (t, val);
7137 : : }
7138 : : /* FALLTHROUGH */
7139 : :
7140 : 1066837 : case CONST_DECL:
7141 : 1066837 : case IMPORTED_DECL:
7142 : 1066837 : RT (t->decl_common.initial);
7143 : 1066837 : break;
7144 : :
7145 : 40312 : case FIELD_DECL:
7146 : 40312 : RT (t->field_decl.offset);
7147 : 40312 : RT (t->field_decl.bit_field_type);
7148 : 40312 : RT (t->field_decl.qualifier);
7149 : 40312 : RT (t->field_decl.bit_offset);
7150 : 40312 : RT (t->field_decl.fcontext);
7151 : 40312 : RT (t->decl_common.initial);
7152 : 40312 : break;
7153 : :
7154 : 12595 : case LABEL_DECL:
7155 : 12595 : RU (t->label_decl.label_decl_uid);
7156 : 12595 : RU (t->label_decl.eh_landing_pad_nr);
7157 : 12595 : break;
7158 : :
7159 : 324408 : case FUNCTION_DECL:
7160 : 324408 : {
7161 : 324408 : unsigned bltin = u ();
7162 : 324408 : t->function_decl.built_in_class = built_in_class (bltin);
7163 : 324408 : if (bltin != NOT_BUILT_IN)
7164 : : {
7165 : 6648 : bltin = u ();
7166 : 6648 : DECL_UNCHECKED_FUNCTION_CODE (t) = built_in_function (bltin);
7167 : : }
7168 : :
7169 : 324408 : RT (t->function_decl.personality);
7170 : : /* These properties are not streamed, and should be reconstructed
7171 : : from any function attributes. */
7172 : : // t->function_decl.function_specific_target);
7173 : : // t->function_decl.function_specific_optimization);
7174 : 324408 : RT (t->function_decl.vindex);
7175 : :
7176 : 324408 : if (DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (t))
7177 : : {
7178 : 2244 : tree spec;
7179 : 2244 : RT (spec);
7180 : 2244 : store_explicit_specifier (t, spec);
7181 : : }
7182 : : }
7183 : : break;
7184 : :
7185 : 38206 : case USING_DECL:
7186 : : /* USING_DECL_DECLS */
7187 : 38206 : RT (t->decl_common.initial);
7188 : : /* FALLTHROUGH */
7189 : :
7190 : 594156 : case TYPE_DECL:
7191 : : /* USING_DECL: USING_DECL_SCOPE */
7192 : : /* TYPE_DECL: DECL_ORIGINAL_TYPE */
7193 : 594156 : RT (t->decl_non_common.result);
7194 : 594156 : break;
7195 : :
7196 : : /* Miscellaneous common nodes. */
7197 : 210206 : case BLOCK:
7198 : 210206 : t->block.locus = state->read_location (*this);
7199 : 210206 : t->block.end_locus = state->read_location (*this);
7200 : :
7201 : 210206 : for (tree *chain = &t->block.vars;;)
7202 : 318301 : if (tree decl = tree_node ())
7203 : : {
7204 : : /* For a deduplicated local type or enumerator, chain the
7205 : : duplicate decl instead of the canonical in-TU decl. Seeing
7206 : : a duplicate here means the containing function whose body
7207 : : we're streaming in is a duplicate too, so we'll end up
7208 : : discarding this BLOCK (and the rest of the duplicate function
7209 : : body) anyway. */
7210 : 108095 : decl = maybe_duplicate (decl);
7211 : :
7212 : 108095 : if (!DECL_P (decl))
7213 : : {
7214 : 0 : set_overrun ();
7215 : 0 : break;
7216 : : }
7217 : :
7218 : : /* If DECL_CHAIN is already set then this was a backreference to a
7219 : : local type or enumerator from a previous read (PR c++/114630).
7220 : : Let's copy the node so we can keep building the chain for ODR
7221 : : checking later. */
7222 : 108095 : if (DECL_CHAIN (decl))
7223 : : {
7224 : 12 : gcc_checking_assert (TREE_CODE (decl) == TYPE_DECL
7225 : : && find_duplicate (DECL_CONTEXT (decl)));
7226 : 6 : decl = copy_decl (decl);
7227 : : }
7228 : :
7229 : 108095 : *chain = decl;
7230 : 108095 : chain = &DECL_CHAIN (decl);
7231 : : }
7232 : : else
7233 : 108095 : break;
7234 : :
7235 : : /* nonlocalized_vars is middle-end. */
7236 : 210206 : RT (t->block.subblocks);
7237 : 210206 : RT (t->block.supercontext);
7238 : 210206 : RT (t->block.abstract_origin);
7239 : : /* fragment_origin, fragment_chain are middle-end. */
7240 : 210206 : RT (t->block.chain);
7241 : : /* nonlocalized_vars, block_num, die are middle endy/debug
7242 : : things. */
7243 : 210206 : break;
7244 : :
7245 : 387615 : case CALL_EXPR:
7246 : 387615 : RUC (internal_fn, t->base.u.ifn);
7247 : 387615 : break;
7248 : :
7249 : : case CONSTRUCTOR:
7250 : : // Streamed after the node's type.
7251 : : break;
7252 : :
7253 : 12 : case OMP_CLAUSE:
7254 : 12 : {
7255 : 12 : RU (t->omp_clause.subcode.map_kind);
7256 : 12 : t->omp_clause.locus = state->read_location (*this);
7257 : :
7258 : 12 : unsigned len = omp_clause_num_ops[OMP_CLAUSE_CODE (t)];
7259 : 45 : for (unsigned ix = 0; ix != len; ix++)
7260 : 33 : RT (t->omp_clause.ops[ix]);
7261 : : }
7262 : : break;
7263 : :
7264 : 144845 : case STATEMENT_LIST:
7265 : 144845 : {
7266 : 144845 : tree_stmt_iterator iter = tsi_start (t);
7267 : 585661 : for (tree stmt; RT (stmt);)
7268 : : {
7269 : 440816 : if (TREE_CODE (stmt) == DEBUG_BEGIN_STMT
7270 : 0 : && !MAY_HAVE_DEBUG_MARKER_STMTS)
7271 : 0 : continue;
7272 : 440816 : tsi_link_after (&iter, stmt, TSI_CONTINUE_LINKING);
7273 : : }
7274 : : }
7275 : 144845 : break;
7276 : :
7277 : 0 : case OPTIMIZATION_NODE:
7278 : 0 : case TARGET_OPTION_NODE:
7279 : : /* Not implemented, see trees_out::core_vals. */
7280 : 0 : gcc_unreachable ();
7281 : 71369 : break;
7282 : :
7283 : 71369 : case TREE_BINFO:
7284 : 71369 : RT (t->binfo.common.chain);
7285 : 71369 : RT (t->binfo.offset);
7286 : 71369 : RT (t->binfo.inheritance);
7287 : 71369 : RT (t->binfo.vptr_field);
7288 : :
7289 : : /* Do not mark the vtables as USED in the address expressions
7290 : : here. */
7291 : 71369 : unused++;
7292 : 71369 : RT (t->binfo.vtable);
7293 : 71369 : RT (t->binfo.virtuals);
7294 : 71369 : RT (t->binfo.vtt_subvtt);
7295 : 71369 : RT (t->binfo.vtt_vptr);
7296 : 71369 : unused--;
7297 : :
7298 : 71369 : BINFO_BASE_ACCESSES (t) = tree_vec ();
7299 : 71369 : if (!get_overrun ())
7300 : : {
7301 : 71369 : unsigned num = vec_safe_length (BINFO_BASE_ACCESSES (t));
7302 : 91499 : for (unsigned ix = 0; ix != num; ix++)
7303 : 20130 : BINFO_BASE_APPEND (t, tree_node ());
7304 : : }
7305 : : break;
7306 : :
7307 : 918626 : case TREE_LIST:
7308 : 918626 : RT (t->list.purpose);
7309 : 918626 : RT (t->list.value);
7310 : 918626 : RT (t->list.common.chain);
7311 : 918626 : break;
7312 : :
7313 : 583472 : case TREE_VEC:
7314 : 1562521 : for (unsigned ix = TREE_VEC_LENGTH (t); ix--;)
7315 : 979049 : RT (TREE_VEC_ELT (t, ix));
7316 : 583472 : RT (t->type_common.common.chain);
7317 : 583472 : break;
7318 : :
7319 : : /* C++-specific nodes ... */
7320 : 67888 : case BASELINK:
7321 : 67888 : RT (((lang_tree_node *)t)->baselink.binfo);
7322 : 67888 : RTU (((lang_tree_node *)t)->baselink.functions);
7323 : 67888 : RT (((lang_tree_node *)t)->baselink.access_binfo);
7324 : 67888 : RT (((lang_tree_node *)t)->baselink.common.chain);
7325 : 67888 : break;
7326 : :
7327 : 28334 : case CONSTRAINT_INFO:
7328 : 28334 : RT (((lang_tree_node *)t)->constraint_info.template_reqs);
7329 : 28334 : RT (((lang_tree_node *)t)->constraint_info.declarator_reqs);
7330 : 28334 : RT (((lang_tree_node *)t)->constraint_info.associated_constr);
7331 : 28334 : break;
7332 : :
7333 : 4988 : case DEFERRED_NOEXCEPT:
7334 : 4988 : RT (((lang_tree_node *)t)->deferred_noexcept.pattern);
7335 : 4988 : RT (((lang_tree_node *)t)->deferred_noexcept.args);
7336 : 4988 : break;
7337 : :
7338 : 3006 : case LAMBDA_EXPR:
7339 : 3006 : RT (((lang_tree_node *)t)->lambda_expression.capture_list);
7340 : 3006 : RT (((lang_tree_node *)t)->lambda_expression.this_capture);
7341 : 3006 : RT (((lang_tree_node *)t)->lambda_expression.extra_scope);
7342 : 3006 : RT (((lang_tree_node *)t)->lambda_expression.regen_info);
7343 : 3006 : RT (((lang_tree_node *)t)->lambda_expression.extra_args);
7344 : : /* lambda_expression.pending_proxies is NULL */
7345 : 3006 : ((lang_tree_node *)t)->lambda_expression.locus
7346 : 3006 : = state->read_location (*this);
7347 : 3006 : RUC (cp_lambda_default_capture_mode_type,
7348 : : ((lang_tree_node *)t)->lambda_expression.default_capture_mode);
7349 : 3006 : RU (((lang_tree_node *)t)->lambda_expression.discriminator_scope);
7350 : 3006 : RU (((lang_tree_node *)t)->lambda_expression.discriminator_sig);
7351 : 3006 : break;
7352 : :
7353 : 388750 : case OVERLOAD:
7354 : 388750 : RT (((lang_tree_node *)t)->overload.function);
7355 : 388750 : RT (t->common.chain);
7356 : 388750 : break;
7357 : :
7358 : 0 : case PTRMEM_CST:
7359 : 0 : RT (((lang_tree_node *)t)->ptrmem.member);
7360 : 0 : break;
7361 : :
7362 : 5567 : case STATIC_ASSERT:
7363 : 5567 : RT (((lang_tree_node *)t)->static_assertion.condition);
7364 : 5567 : RT (((lang_tree_node *)t)->static_assertion.message);
7365 : 5567 : ((lang_tree_node *)t)->static_assertion.location
7366 : 5567 : = state->read_location (*this);
7367 : 5567 : break;
7368 : :
7369 : 232831 : case TEMPLATE_DECL:
7370 : : /* Streamed when reading the raw template decl itself. */
7371 : 232831 : gcc_assert (((lang_tree_node *)t)->template_decl.arguments);
7372 : 232831 : gcc_assert (((lang_tree_node *)t)->template_decl.result);
7373 : 232831 : if (DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (t))
7374 : 4717 : RT (DECL_CHAIN (t));
7375 : : break;
7376 : :
7377 : 517789 : case TEMPLATE_INFO:
7378 : 517789 : RT (((lang_tree_node *)t)->template_info.tmpl);
7379 : 517789 : RT (((lang_tree_node *)t)->template_info.args);
7380 : 517789 : RT (((lang_tree_node *)t)->template_info.partial);
7381 : 517789 : if (unsigned len = u ())
7382 : : {
7383 : 0 : auto &ac = (((lang_tree_node *)t)
7384 : : ->template_info.deferred_access_checks);
7385 : 0 : vec_alloc (ac, len);
7386 : 0 : for (unsigned ix = 0; ix != len; ix++)
7387 : : {
7388 : 0 : deferred_access_check m;
7389 : :
7390 : 0 : RT (m.binfo);
7391 : 0 : RT (m.decl);
7392 : 0 : RT (m.diag_decl);
7393 : 0 : m.loc = state->read_location (*this);
7394 : 0 : ac->quick_push (m);
7395 : : }
7396 : : }
7397 : : break;
7398 : :
7399 : 286148 : case TEMPLATE_PARM_INDEX:
7400 : 286148 : RU (((lang_tree_node *)t)->tpi.index);
7401 : 286148 : RU (((lang_tree_node *)t)->tpi.level);
7402 : 286148 : RU (((lang_tree_node *)t)->tpi.orig_level);
7403 : 286148 : RT (((lang_tree_node *)t)->tpi.decl);
7404 : 286148 : break;
7405 : :
7406 : 6748 : case TRAIT_EXPR:
7407 : 6748 : RT (((lang_tree_node *)t)->trait_expression.type1);
7408 : 6748 : RT (((lang_tree_node *)t)->trait_expression.type2);
7409 : 6748 : RUC (cp_trait_kind, ((lang_tree_node *)t)->trait_expression.kind);
7410 : 6748 : break;
7411 : :
7412 : 2 : case TU_LOCAL_ENTITY:
7413 : 2 : RT (((lang_tree_node *)t)->tu_local_entity.name);
7414 : 2 : ((lang_tree_node *)t)->tu_local_entity.loc
7415 : 2 : = state->read_location (*this);
7416 : : }
7417 : :
7418 : 10503437 : if (CODE_CONTAINS_STRUCT (code, TS_TYPED))
7419 : : {
7420 : 9736551 : tree type = tree_node ();
7421 : :
7422 : 9736551 : if (type && code == ENUMERAL_TYPE && !ENUM_FIXED_UNDERLYING_TYPE_P (t))
7423 : : {
7424 : 1450 : unsigned precision = u ();
7425 : :
7426 : 1450 : type = build_distinct_type_copy (type);
7427 : 1450 : TYPE_PRECISION (type) = precision;
7428 : 2900 : set_min_and_max_values_for_integral_type (type, precision,
7429 : 1450 : TYPE_SIGN (type));
7430 : : }
7431 : :
7432 : 9736551 : if (code != TEMPLATE_DECL)
7433 : 9503720 : t->typed.type = type;
7434 : : }
7435 : :
7436 : 10503437 : if (TREE_CODE (t) == CONSTRUCTOR)
7437 : 38747 : if (unsigned len = u ())
7438 : : {
7439 : 23735 : vec_alloc (t->constructor.elts, len);
7440 : 164778 : for (unsigned ix = 0; ix != len; ix++)
7441 : : {
7442 : 141043 : constructor_elt elt;
7443 : :
7444 : 141043 : RT (elt.index);
7445 : 141043 : RTU (elt.value);
7446 : 141043 : t->constructor.elts->quick_push (elt);
7447 : : }
7448 : : }
7449 : :
7450 : : #undef RT
7451 : : #undef RM
7452 : : #undef RU
7453 : 10503437 : return !get_overrun ();
7454 : : }
7455 : :
7456 : : void
7457 : 4093737 : trees_out::lang_decl_vals (tree t)
7458 : : {
7459 : 4093737 : const struct lang_decl *lang = DECL_LANG_SPECIFIC (t);
7460 : : #define WU(X) (u (X))
7461 : : #define WT(X) (tree_node (X))
7462 : : /* Module index already written. */
7463 : 4093737 : switch (lang->u.base.selector)
7464 : : {
7465 : 0 : default:
7466 : 0 : gcc_unreachable ();
7467 : :
7468 : 758379 : case lds_fn: /* lang_decl_fn. */
7469 : 758379 : if (streaming_p ())
7470 : : {
7471 : 379099 : if (DECL_NAME (t) && IDENTIFIER_OVL_OP_P (DECL_NAME (t)))
7472 : 62918 : WU (lang->u.fn.ovl_op_code);
7473 : : }
7474 : :
7475 : 961348 : if (DECL_CLASS_SCOPE_P (t) || DECL_UNIQUE_FRIEND_P (t))
7476 : 575604 : WT (lang->u.fn.context);
7477 : :
7478 : 758379 : if (lang->u.fn.thunk_p)
7479 : : {
7480 : : /* The thunked-to function. */
7481 : 1064 : WT (lang->u.fn.befriending_classes);
7482 : 1064 : if (streaming_p ())
7483 : 532 : wi (lang->u.fn.u5.fixed_offset);
7484 : : }
7485 : 757315 : else if (decl_tls_wrapper_p (t))
7486 : : /* The wrapped variable. */
7487 : 18 : WT (lang->u.fn.befriending_classes);
7488 : : else
7489 : 757297 : WT (lang->u.fn.u5.cloned_function);
7490 : :
7491 : 758379 : if (FNDECL_USED_AUTO (t))
7492 : 2396 : WT (lang->u.fn.u.saved_auto_return_type);
7493 : :
7494 : 758379 : goto lds_min;
7495 : :
7496 : 2766 : case lds_decomp: /* lang_decl_decomp. */
7497 : 2766 : WT (lang->u.decomp.base);
7498 : 2766 : goto lds_min;
7499 : :
7500 : 2433677 : case lds_min: /* lang_decl_min. */
7501 : 2433677 : lds_min:
7502 : 2433677 : WT (lang->u.min.template_info);
7503 : 2433677 : {
7504 : 2433677 : tree access = lang->u.min.access;
7505 : :
7506 : : /* DECL_ACCESS needs to be maintained by the definition of the
7507 : : (derived) class that changes the access. The other users
7508 : : of DECL_ACCESS need to write it here. */
7509 : 758379 : if (!DECL_THUNK_P (t)
7510 : 3190992 : && (DECL_CONTEXT (t) && TYPE_P (DECL_CONTEXT (t))))
7511 : : access = NULL_TREE;
7512 : :
7513 : 2433677 : WT (access);
7514 : : }
7515 : 2433677 : break;
7516 : :
7517 : : case lds_ns: /* lang_decl_ns. */
7518 : : break;
7519 : :
7520 : 1659895 : case lds_parm: /* lang_decl_parm. */
7521 : 1659895 : if (streaming_p ())
7522 : : {
7523 : 566013 : WU (lang->u.parm.level);
7524 : 566013 : WU (lang->u.parm.index);
7525 : : }
7526 : : break;
7527 : : }
7528 : : #undef WU
7529 : : #undef WT
7530 : 4093737 : }
7531 : :
7532 : : bool
7533 : 1473275 : trees_in::lang_decl_vals (tree t)
7534 : : {
7535 : 1473275 : struct lang_decl *lang = DECL_LANG_SPECIFIC (t);
7536 : : #define RU(X) ((X) = u ())
7537 : : #define RT(X) ((X) = tree_node ())
7538 : :
7539 : : /* Module index already read. */
7540 : 1473275 : switch (lang->u.base.selector)
7541 : : {
7542 : 0 : default:
7543 : 0 : gcc_unreachable ();
7544 : :
7545 : 324408 : case lds_fn: /* lang_decl_fn. */
7546 : 324408 : if (DECL_NAME (t) && IDENTIFIER_OVL_OP_P (DECL_NAME (t)))
7547 : : {
7548 : 56000 : unsigned code = u ();
7549 : :
7550 : : /* Check consistency. */
7551 : 56000 : if (code >= OVL_OP_MAX
7552 : 56000 : || (ovl_op_info[IDENTIFIER_ASSIGN_OP_P (DECL_NAME (t))][code]
7553 : 56000 : .ovl_op_code) == OVL_OP_ERROR_MARK)
7554 : 0 : set_overrun ();
7555 : : else
7556 : 56000 : lang->u.fn.ovl_op_code = code;
7557 : : }
7558 : :
7559 : 409565 : if (DECL_CLASS_SCOPE_P (t) || DECL_UNIQUE_FRIEND_P (t))
7560 : 248660 : RT (lang->u.fn.context);
7561 : :
7562 : 324408 : if (lang->u.fn.thunk_p)
7563 : : {
7564 : 426 : RT (lang->u.fn.befriending_classes);
7565 : 426 : lang->u.fn.u5.fixed_offset = wi ();
7566 : : }
7567 : 323982 : else if (decl_tls_wrapper_p (t))
7568 : 15 : RT (lang->u.fn.befriending_classes);
7569 : : else
7570 : 323967 : RT (lang->u.fn.u5.cloned_function);
7571 : :
7572 : 324408 : if (FNDECL_USED_AUTO (t))
7573 : 1020 : RT (lang->u.fn.u.saved_auto_return_type);
7574 : 324408 : goto lds_min;
7575 : :
7576 : 1303 : case lds_decomp: /* lang_decl_decomp. */
7577 : 1303 : RT (lang->u.decomp.base);
7578 : 1303 : goto lds_min;
7579 : :
7580 : 994016 : case lds_min: /* lang_decl_min. */
7581 : 994016 : lds_min:
7582 : 994016 : RT (lang->u.min.template_info);
7583 : 994016 : RT (lang->u.min.access);
7584 : 994016 : break;
7585 : :
7586 : : case lds_ns: /* lang_decl_ns. */
7587 : : break;
7588 : :
7589 : 479161 : case lds_parm: /* lang_decl_parm. */
7590 : 479161 : RU (lang->u.parm.level);
7591 : 479161 : RU (lang->u.parm.index);
7592 : 479161 : break;
7593 : : }
7594 : : #undef RU
7595 : : #undef RT
7596 : 1473275 : return !get_overrun ();
7597 : : }
7598 : :
7599 : : /* Most of the value contents of lang_type is streamed in
7600 : : define_class. */
7601 : :
7602 : : void
7603 : 288320 : trees_out::lang_type_vals (tree t)
7604 : : {
7605 : 288320 : const struct lang_type *lang = TYPE_LANG_SPECIFIC (t);
7606 : : #define WU(X) (u (X))
7607 : : #define WT(X) (tree_node (X))
7608 : 288320 : if (streaming_p ())
7609 : 144140 : WU (lang->align);
7610 : : #undef WU
7611 : : #undef WT
7612 : 288320 : }
7613 : :
7614 : : bool
7615 : 112375 : trees_in::lang_type_vals (tree t)
7616 : : {
7617 : 112375 : struct lang_type *lang = TYPE_LANG_SPECIFIC (t);
7618 : : #define RU(X) ((X) = u ())
7619 : : #define RT(X) ((X) = tree_node ())
7620 : 112375 : RU (lang->align);
7621 : : #undef RU
7622 : : #undef RT
7623 : 112375 : return !get_overrun ();
7624 : : }
7625 : :
7626 : : /* Write out the bools of T, including information about any
7627 : : LANG_SPECIFIC information. Including allocation of any lang
7628 : : specific object. */
7629 : :
7630 : : void
7631 : 12361186 : trees_out::tree_node_bools (tree t)
7632 : : {
7633 : 12361186 : gcc_checking_assert (streaming_p ());
7634 : :
7635 : : /* We should never stream a namespace. */
7636 : 12361186 : gcc_checking_assert (TREE_CODE (t) != NAMESPACE_DECL
7637 : : || DECL_NAMESPACE_ALIAS (t));
7638 : :
7639 : 12361186 : bits_out bits = stream_bits ();
7640 : 12361186 : core_bools (t, bits);
7641 : :
7642 : 12361186 : switch (TREE_CODE_CLASS (TREE_CODE (t)))
7643 : : {
7644 : 2771577 : case tcc_declaration:
7645 : 2771577 : {
7646 : 2771577 : bool specific = DECL_LANG_SPECIFIC (t) != NULL;
7647 : 2771577 : bits.b (specific);
7648 : 2771577 : if (specific && VAR_P (t))
7649 : 281518 : bits.b (DECL_DECOMPOSITION_P (t));
7650 : 1775845 : if (specific)
7651 : 1775845 : lang_decl_bools (t, bits);
7652 : : }
7653 : : break;
7654 : :
7655 : 520689 : case tcc_type:
7656 : 520689 : {
7657 : 520689 : bool specific = (TYPE_MAIN_VARIANT (t) == t
7658 : 520689 : && TYPE_LANG_SPECIFIC (t) != NULL);
7659 : 520689 : gcc_assert (TYPE_LANG_SPECIFIC (t)
7660 : : == TYPE_LANG_SPECIFIC (TYPE_MAIN_VARIANT (t)));
7661 : :
7662 : 520689 : bits.b (specific);
7663 : 520689 : if (specific)
7664 : 144140 : lang_type_bools (t, bits);
7665 : : }
7666 : : break;
7667 : :
7668 : : default:
7669 : : break;
7670 : : }
7671 : :
7672 : 12361186 : bits.bflush ();
7673 : 12361186 : }
7674 : :
7675 : : bool
7676 : 10519066 : trees_in::tree_node_bools (tree t)
7677 : : {
7678 : 10519066 : bits_in bits = stream_bits ();
7679 : 10519066 : bool ok = core_bools (t, bits);
7680 : :
7681 : 10519066 : if (ok)
7682 : 10519066 : switch (TREE_CODE_CLASS (TREE_CODE (t)))
7683 : : {
7684 : 2309664 : case tcc_declaration:
7685 : 2309664 : if (bits.b ())
7686 : : {
7687 : 1473275 : bool decomp = VAR_P (t) && bits.b ();
7688 : :
7689 : 1473275 : ok = maybe_add_lang_decl_raw (t, decomp);
7690 : 1473275 : if (ok)
7691 : 1473275 : ok = lang_decl_bools (t, bits);
7692 : : }
7693 : : break;
7694 : :
7695 : 414420 : case tcc_type:
7696 : 414420 : if (bits.b ())
7697 : : {
7698 : 112375 : ok = maybe_add_lang_type_raw (t);
7699 : 112375 : if (ok)
7700 : 112375 : ok = lang_type_bools (t, bits);
7701 : : }
7702 : : break;
7703 : :
7704 : : default:
7705 : : break;
7706 : : }
7707 : :
7708 : 10519066 : bits.bflush ();
7709 : 10519066 : if (!ok || get_overrun ())
7710 : 0 : return false;
7711 : :
7712 : : return true;
7713 : 10519066 : }
7714 : :
7715 : :
7716 : : /* Write out the lang-specific vals of node T. */
7717 : :
7718 : : void
7719 : 31915337 : trees_out::lang_vals (tree t)
7720 : : {
7721 : 31915337 : switch (TREE_CODE_CLASS (TREE_CODE (t)))
7722 : : {
7723 : 7218334 : case tcc_declaration:
7724 : 7218334 : if (DECL_LANG_SPECIFIC (t))
7725 : 4093737 : lang_decl_vals (t);
7726 : : break;
7727 : :
7728 : 1802138 : case tcc_type:
7729 : 1802138 : if (TYPE_MAIN_VARIANT (t) == t && TYPE_LANG_SPECIFIC (t))
7730 : 288320 : lang_type_vals (t);
7731 : : break;
7732 : :
7733 : : default:
7734 : : break;
7735 : : }
7736 : 31915337 : }
7737 : :
7738 : : bool
7739 : 10503437 : trees_in::lang_vals (tree t)
7740 : : {
7741 : 10503437 : bool ok = true;
7742 : :
7743 : 10503437 : switch (TREE_CODE_CLASS (TREE_CODE (t)))
7744 : : {
7745 : 2309664 : case tcc_declaration:
7746 : 2309664 : if (DECL_LANG_SPECIFIC (t))
7747 : 1473275 : ok = lang_decl_vals (t);
7748 : : break;
7749 : :
7750 : 398791 : case tcc_type:
7751 : 398791 : if (TYPE_LANG_SPECIFIC (t))
7752 : 112375 : ok = lang_type_vals (t);
7753 : : else
7754 : 286416 : TYPE_LANG_SPECIFIC (t) = TYPE_LANG_SPECIFIC (TYPE_MAIN_VARIANT (t));
7755 : : break;
7756 : :
7757 : : default:
7758 : : break;
7759 : : }
7760 : :
7761 : 10503437 : return ok;
7762 : : }
7763 : :
7764 : : /* Write out the value fields of node T. */
7765 : :
7766 : : void
7767 : 31915337 : trees_out::tree_node_vals (tree t)
7768 : : {
7769 : 31915337 : core_vals (t);
7770 : 31915337 : lang_vals (t);
7771 : 31915337 : }
7772 : :
7773 : : bool
7774 : 10503437 : trees_in::tree_node_vals (tree t)
7775 : : {
7776 : 10503437 : bool ok = core_vals (t);
7777 : 10503437 : if (ok)
7778 : 10503437 : ok = lang_vals (t);
7779 : :
7780 : 10503437 : return ok;
7781 : : }
7782 : :
7783 : :
7784 : : /* If T is a back reference, fixed reference or NULL, write out its
7785 : : code and return WK_none. Otherwise return WK_value if we must write
7786 : : by value, or WK_normal otherwise. */
7787 : :
7788 : : walk_kind
7789 : 213171755 : trees_out::ref_node (tree t)
7790 : : {
7791 : 213171755 : if (!t)
7792 : : {
7793 : 86812181 : if (streaming_p ())
7794 : : {
7795 : : /* NULL_TREE -> tt_null. */
7796 : 33199211 : null_count++;
7797 : 33199211 : i (tt_null);
7798 : : }
7799 : 86812181 : return WK_none;
7800 : : }
7801 : :
7802 : 126359574 : if (!TREE_VISITED (t))
7803 : : return WK_normal;
7804 : :
7805 : : /* An already-visited tree. It must be in the map. */
7806 : 74433156 : int val = get_tag (t);
7807 : :
7808 : 74433156 : if (val == tag_value)
7809 : : /* An entry we should walk into. */
7810 : : return WK_value;
7811 : :
7812 : 73200076 : const char *kind;
7813 : :
7814 : 73200076 : if (val <= tag_backref)
7815 : : {
7816 : : /* Back reference -> -ve number */
7817 : 55915654 : if (streaming_p ())
7818 : 26525607 : i (val);
7819 : : kind = "backref";
7820 : : }
7821 : 17284422 : else if (val >= tag_fixed)
7822 : : {
7823 : : /* Fixed reference -> tt_fixed */
7824 : 17284422 : val -= tag_fixed;
7825 : 17284422 : if (streaming_p ())
7826 : 6130091 : i (tt_fixed), u (val);
7827 : : kind = "fixed";
7828 : : }
7829 : :
7830 : 73200076 : if (streaming_p ())
7831 : : {
7832 : 32655698 : back_ref_count++;
7833 : 32655698 : dump (dumper::TREE)
7834 : 14184 : && dump ("Wrote %s:%d %C:%N%S", kind, val, TREE_CODE (t), t, t);
7835 : : }
7836 : : return WK_none;
7837 : : }
7838 : :
7839 : : tree
7840 : 22408210 : trees_in::back_ref (int tag)
7841 : : {
7842 : 22408210 : tree res = NULL_TREE;
7843 : :
7844 : 22408210 : if (tag < 0 && unsigned (~tag) < back_refs.length ())
7845 : 22408210 : res = back_refs[~tag];
7846 : :
7847 : 22408210 : if (!res
7848 : : /* Checking TREE_CODE is a dereference, so we know this is not a
7849 : : wild pointer. Checking the code provides evidence we've not
7850 : : corrupted something. */
7851 : 22408210 : || TREE_CODE (res) >= MAX_TREE_CODES)
7852 : 0 : set_overrun ();
7853 : : else
7854 : 22422747 : dump (dumper::TREE) && dump ("Read backref:%d found %C:%N%S", tag,
7855 : : TREE_CODE (res), res, res);
7856 : 22408210 : return res;
7857 : : }
7858 : :
7859 : : unsigned
7860 : 2764280 : trees_out::add_indirect_tpl_parms (tree parms)
7861 : : {
7862 : 2764280 : unsigned len = 0;
7863 : 5170310 : for (; parms; parms = TREE_CHAIN (parms), len++)
7864 : : {
7865 : 2919174 : if (TREE_VISITED (parms))
7866 : : break;
7867 : :
7868 : 2406030 : int tag = insert (parms);
7869 : 2406030 : if (streaming_p ())
7870 : 2406321 : dump (dumper::TREE)
7871 : 153 : && dump ("Indirect:%d template's parameter %u %C:%N",
7872 : 153 : tag, len, TREE_CODE (parms), parms);
7873 : : }
7874 : :
7875 : 2764280 : if (streaming_p ())
7876 : 459872 : u (len);
7877 : :
7878 : 2764280 : return len;
7879 : : }
7880 : :
7881 : : unsigned
7882 : 375001 : trees_in::add_indirect_tpl_parms (tree parms)
7883 : : {
7884 : 375001 : unsigned len = u ();
7885 : 675781 : for (unsigned ix = 0; ix != len; parms = TREE_CHAIN (parms), ix++)
7886 : : {
7887 : 300780 : int tag = insert (parms);
7888 : 301224 : dump (dumper::TREE)
7889 : 159 : && dump ("Indirect:%d template's parameter %u %C:%N",
7890 : 159 : tag, ix, TREE_CODE (parms), parms);
7891 : : }
7892 : :
7893 : 375001 : return len;
7894 : : }
7895 : :
7896 : : /* We've just found DECL by name. Insert nodes that come with it, but
7897 : : cannot be found by name, so we'll not accidentally walk into them. */
7898 : :
7899 : : void
7900 : 6432481 : trees_out::add_indirects (tree decl)
7901 : : {
7902 : 6432481 : unsigned count = 0;
7903 : :
7904 : : // FIXME:OPTIMIZATION We'll eventually want default fn parms of
7905 : : // templates and perhaps default template parms too. The former can
7906 : : // be referenced from instantiations (as they are lazily
7907 : : // instantiated). Also (deferred?) exception specifications of
7908 : : // templates. See the note about PARM_DECLs in trees_out::decl_node.
7909 : 6432481 : tree inner = decl;
7910 : 6432481 : if (TREE_CODE (decl) == TEMPLATE_DECL)
7911 : : {
7912 : 2764280 : count += add_indirect_tpl_parms (DECL_TEMPLATE_PARMS (decl));
7913 : :
7914 : 2764280 : inner = DECL_TEMPLATE_RESULT (decl);
7915 : 2764280 : int tag = insert (inner);
7916 : 2764280 : if (streaming_p ())
7917 : 459872 : dump (dumper::TREE)
7918 : 219 : && dump ("Indirect:%d template's result %C:%N",
7919 : 219 : tag, TREE_CODE (inner), inner);
7920 : 2764280 : count++;
7921 : : }
7922 : :
7923 : 6432481 : if (TREE_CODE (inner) == TYPE_DECL)
7924 : : {
7925 : : /* Make sure the type is in the map too. Otherwise we get
7926 : : different RECORD_TYPEs for the same type, and things go
7927 : : south. */
7928 : 3546182 : tree type = TREE_TYPE (inner);
7929 : 3546182 : gcc_checking_assert (DECL_ORIGINAL_TYPE (inner)
7930 : : || TYPE_NAME (type) == inner);
7931 : 3546182 : int tag = insert (type);
7932 : 3546182 : if (streaming_p ())
7933 : 408656 : dump (dumper::TREE) && dump ("Indirect:%d decl's type %C:%N", tag,
7934 : 362 : TREE_CODE (type), type);
7935 : 3546182 : count++;
7936 : : }
7937 : :
7938 : 6432481 : if (streaming_p ())
7939 : : {
7940 : 988409 : u (count);
7941 : 988938 : dump (dumper::TREE) && dump ("Inserted %u indirects", count);
7942 : : }
7943 : 6432481 : }
7944 : :
7945 : : bool
7946 : 778294 : trees_in::add_indirects (tree decl)
7947 : : {
7948 : 778294 : unsigned count = 0;
7949 : :
7950 : 778294 : tree inner = decl;
7951 : 778294 : if (TREE_CODE (inner) == TEMPLATE_DECL)
7952 : : {
7953 : 375001 : count += add_indirect_tpl_parms (DECL_TEMPLATE_PARMS (decl));
7954 : :
7955 : 375001 : inner = DECL_TEMPLATE_RESULT (decl);
7956 : 375001 : int tag = insert (inner);
7957 : 375001 : dump (dumper::TREE)
7958 : 228 : && dump ("Indirect:%d templates's result %C:%N", tag,
7959 : 228 : TREE_CODE (inner), inner);
7960 : 375001 : count++;
7961 : : }
7962 : :
7963 : 778294 : if (TREE_CODE (inner) == TYPE_DECL)
7964 : : {
7965 : 301782 : tree type = TREE_TYPE (inner);
7966 : 301782 : gcc_checking_assert (DECL_ORIGINAL_TYPE (inner)
7967 : : || TYPE_NAME (type) == inner);
7968 : 301782 : int tag = insert (type);
7969 : 301782 : dump (dumper::TREE)
7970 : 362 : && dump ("Indirect:%d decl's type %C:%N", tag, TREE_CODE (type), type);
7971 : 301782 : count++;
7972 : : }
7973 : :
7974 : 778899 : dump (dumper::TREE) && dump ("Inserted %u indirects", count);
7975 : 778294 : return count == u ();
7976 : : }
7977 : :
7978 : : /* Stream a template parameter. There are 4.5 kinds of parameter:
7979 : : a) Template - TEMPLATE_DECL->TYPE_DECL->TEMPLATE_TEMPLATE_PARM
7980 : : TEMPLATE_TYPE_PARM_INDEX TPI
7981 : : b) Type - TYPE_DECL->TEMPLATE_TYPE_PARM TEMPLATE_TYPE_PARM_INDEX TPI
7982 : : c.1) NonTYPE - PARM_DECL DECL_INITIAL TPI We meet this first
7983 : : c.2) NonTYPE - CONST_DECL DECL_INITIAL Same TPI
7984 : : d) BoundTemplate - TYPE_DECL->BOUND_TEMPLATE_TEMPLATE_PARM
7985 : : TEMPLATE_TYPE_PARM_INDEX->TPI
7986 : : TEMPLATE_TEMPLATE_PARM_INFO->TEMPLATE_INFO
7987 : :
7988 : : All of these point to a TEMPLATE_PARM_INDEX, and #B also has a TEMPLATE_INFO
7989 : : */
7990 : :
7991 : : void
7992 : 1624465 : trees_out::tpl_parm_value (tree parm)
7993 : : {
7994 : 1624465 : gcc_checking_assert (DECL_P (parm) && DECL_TEMPLATE_PARM_P (parm));
7995 : :
7996 : 1624465 : int parm_tag = insert (parm);
7997 : 1624465 : if (streaming_p ())
7998 : : {
7999 : 367773 : i (tt_tpl_parm);
8000 : 367773 : dump (dumper::TREE) && dump ("Writing template parm:%d %C:%N",
8001 : 114 : parm_tag, TREE_CODE (parm), parm);
8002 : 367773 : start (parm);
8003 : 367773 : tree_node_bools (parm);
8004 : : }
8005 : :
8006 : 1624465 : tree inner = parm;
8007 : 1624465 : if (TREE_CODE (inner) == TEMPLATE_DECL)
8008 : : {
8009 : 4786 : inner = DECL_TEMPLATE_RESULT (inner);
8010 : 4786 : int inner_tag = insert (inner);
8011 : 4786 : if (streaming_p ())
8012 : : {
8013 : 1234 : dump (dumper::TREE) && dump ("Writing inner template parm:%d %C:%N",
8014 : 0 : inner_tag, TREE_CODE (inner), inner);
8015 : 1234 : start (inner);
8016 : 1234 : tree_node_bools (inner);
8017 : : }
8018 : : }
8019 : :
8020 : 1624465 : tree type = NULL_TREE;
8021 : 1624465 : if (TREE_CODE (inner) == TYPE_DECL)
8022 : : {
8023 : 1463160 : type = TREE_TYPE (inner);
8024 : 1463160 : int type_tag = insert (type);
8025 : 1463160 : if (streaming_p ())
8026 : : {
8027 : 331807 : dump (dumper::TREE) && dump ("Writing template parm type:%d %C:%N",
8028 : 108 : type_tag, TREE_CODE (type), type);
8029 : 331807 : start (type);
8030 : 331807 : tree_node_bools (type);
8031 : : }
8032 : : }
8033 : :
8034 : 1624465 : if (inner != parm)
8035 : : {
8036 : : /* This is a template-template parameter. */
8037 : 4786 : unsigned tpl_levels = 0;
8038 : 4786 : tpl_header (parm, &tpl_levels);
8039 : 4786 : tpl_parms_fini (parm, tpl_levels);
8040 : : }
8041 : :
8042 : 1624465 : tree_node_vals (parm);
8043 : 1624465 : if (inner != parm)
8044 : 4786 : tree_node_vals (inner);
8045 : 1624465 : if (type)
8046 : : {
8047 : 1463160 : tree_node_vals (type);
8048 : 1463160 : if (DECL_NAME (inner) == auto_identifier
8049 : 1463160 : || DECL_NAME (inner) == decltype_auto_identifier)
8050 : : {
8051 : : /* Placeholder auto. */
8052 : 63632 : tree_node (DECL_INITIAL (inner));
8053 : 63632 : tree_node (DECL_SIZE_UNIT (inner));
8054 : : }
8055 : : }
8056 : :
8057 : 1624465 : if (streaming_p ())
8058 : 367773 : dump (dumper::TREE) && dump ("Wrote template parm:%d %C:%N",
8059 : 114 : parm_tag, TREE_CODE (parm), parm);
8060 : 1624465 : }
8061 : :
8062 : : tree
8063 : 298954 : trees_in::tpl_parm_value ()
8064 : : {
8065 : 298954 : tree parm = start ();
8066 : 298954 : if (!parm || !tree_node_bools (parm))
8067 : 0 : return NULL_TREE;
8068 : :
8069 : 298954 : int parm_tag = insert (parm);
8070 : 298954 : dump (dumper::TREE) && dump ("Reading template parm:%d %C:%N",
8071 : 198 : parm_tag, TREE_CODE (parm), parm);
8072 : :
8073 : 298954 : tree inner = parm;
8074 : 298954 : if (TREE_CODE (inner) == TEMPLATE_DECL)
8075 : : {
8076 : 824 : inner = start ();
8077 : 824 : if (!inner || !tree_node_bools (inner))
8078 : 0 : return NULL_TREE;
8079 : 824 : int inner_tag = insert (inner);
8080 : 824 : dump (dumper::TREE) && dump ("Reading inner template parm:%d %C:%N",
8081 : 0 : inner_tag, TREE_CODE (inner), inner);
8082 : 824 : DECL_TEMPLATE_RESULT (parm) = inner;
8083 : : }
8084 : :
8085 : 298954 : tree type = NULL_TREE;
8086 : 298954 : if (TREE_CODE (inner) == TYPE_DECL)
8087 : : {
8088 : 268783 : type = start ();
8089 : 268783 : if (!type || !tree_node_bools (type))
8090 : 0 : return NULL_TREE;
8091 : 268783 : int type_tag = insert (type);
8092 : 268783 : dump (dumper::TREE) && dump ("Reading template parm type:%d %C:%N",
8093 : 120 : type_tag, TREE_CODE (type), type);
8094 : :
8095 : 268783 : TREE_TYPE (inner) = TREE_TYPE (parm) = type;
8096 : 268783 : TYPE_NAME (type) = parm;
8097 : : }
8098 : :
8099 : 298954 : if (inner != parm)
8100 : : {
8101 : : /* A template template parameter. */
8102 : 824 : unsigned tpl_levels = 0;
8103 : 824 : tpl_header (parm, &tpl_levels);
8104 : 824 : tpl_parms_fini (parm, tpl_levels);
8105 : : }
8106 : :
8107 : 298954 : tree_node_vals (parm);
8108 : 298954 : if (inner != parm)
8109 : 824 : tree_node_vals (inner);
8110 : 298954 : if (type)
8111 : : {
8112 : 268783 : tree_node_vals (type);
8113 : 268783 : if (DECL_NAME (inner) == auto_identifier
8114 : 268783 : || DECL_NAME (inner) == decltype_auto_identifier)
8115 : : {
8116 : : /* Placeholder auto. */
8117 : 22884 : DECL_INITIAL (inner) = tree_node ();
8118 : 22884 : DECL_SIZE_UNIT (inner) = tree_node ();
8119 : : }
8120 : 268783 : if (TYPE_CANONICAL (type))
8121 : : {
8122 : 268783 : gcc_checking_assert (TYPE_CANONICAL (type) == type);
8123 : 268783 : TYPE_CANONICAL (type) = canonical_type_parameter (type);
8124 : : }
8125 : : }
8126 : :
8127 : 298954 : dump (dumper::TREE) && dump ("Read template parm:%d %C:%N",
8128 : 198 : parm_tag, TREE_CODE (parm), parm);
8129 : :
8130 : : return parm;
8131 : : }
8132 : :
8133 : : void
8134 : 1058893 : trees_out::install_entity (tree decl, depset *dep)
8135 : : {
8136 : 1058893 : gcc_checking_assert (streaming_p ());
8137 : :
8138 : : /* Write the entity index, so we can insert it as soon as we
8139 : : know this is new. */
8140 : 1058893 : u (dep ? dep->cluster + 1 : 0);
8141 : 1058893 : if (CHECKING_P && dep)
8142 : : {
8143 : : /* Add it to the entity map, such that we can tell it is
8144 : : part of us. */
8145 : 791738 : bool existed;
8146 : 791738 : unsigned *slot = &entity_map->get_or_insert
8147 : 791738 : (DECL_UID (decl), &existed);
8148 : 791738 : if (existed)
8149 : : /* If it existed, it should match. */
8150 : 833 : gcc_checking_assert (decl == (*entity_ary)[*slot]);
8151 : 791738 : *slot = ~dep->cluster;
8152 : : }
8153 : 1058893 : }
8154 : :
8155 : : bool
8156 : 866974 : trees_in::install_entity (tree decl)
8157 : : {
8158 : 866974 : unsigned entity_index = u ();
8159 : 866974 : if (!entity_index)
8160 : : return false;
8161 : :
8162 : 634359 : if (entity_index > state->entity_num)
8163 : : {
8164 : 0 : set_overrun ();
8165 : 0 : return false;
8166 : : }
8167 : :
8168 : : /* Insert the real decl into the entity ary. */
8169 : 634359 : unsigned ident = state->entity_lwm + entity_index - 1;
8170 : 634359 : (*entity_ary)[ident] = decl;
8171 : :
8172 : : /* And into the entity map, if it's not already there. */
8173 : 634359 : tree not_tmpl = STRIP_TEMPLATE (decl);
8174 : 634359 : if (!DECL_LANG_SPECIFIC (not_tmpl)
8175 : 1183878 : || !DECL_MODULE_ENTITY_P (not_tmpl))
8176 : : {
8177 : : /* We don't want to use retrofit_lang_decl directly so that we aren't
8178 : : affected by the language state when we load in. */
8179 : 633736 : if (!DECL_LANG_SPECIFIC (not_tmpl))
8180 : : {
8181 : 84840 : maybe_add_lang_decl_raw (not_tmpl, false);
8182 : 84840 : SET_DECL_LANGUAGE (not_tmpl, lang_cplusplus);
8183 : : }
8184 : 633736 : DECL_MODULE_ENTITY_P (not_tmpl) = true;
8185 : :
8186 : : /* Insert into the entity hash (it cannot already be there). */
8187 : 633736 : bool existed;
8188 : 633736 : unsigned &slot = entity_map->get_or_insert (DECL_UID (decl), &existed);
8189 : 633736 : gcc_checking_assert (!existed);
8190 : 633736 : slot = ident;
8191 : : }
8192 : : else
8193 : : {
8194 : 623 : unsigned *slot = entity_map->get (DECL_UID (decl));
8195 : :
8196 : : /* The entity must be in the entity map already. However, DECL may
8197 : : be the DECL_TEMPLATE_RESULT of an existing partial specialisation
8198 : : if we matched it while streaming another instantiation; in this
8199 : : case we already registered that TEMPLATE_DECL. */
8200 : 623 : if (!slot)
8201 : : {
8202 : 9 : tree type = TREE_TYPE (decl);
8203 : 9 : gcc_checking_assert (TREE_CODE (decl) == TYPE_DECL
8204 : : && CLASS_TYPE_P (type)
8205 : : && CLASSTYPE_TEMPLATE_SPECIALIZATION (type));
8206 : 9 : slot = entity_map->get (DECL_UID (CLASSTYPE_TI_TEMPLATE (type)));
8207 : : }
8208 : 9 : gcc_checking_assert (slot);
8209 : :
8210 : 623 : if (state->is_partition ())
8211 : : {
8212 : : /* The decl is already in the entity map, but we see it again now
8213 : : from a partition: we want to overwrite if the original decl
8214 : : wasn't also from a (possibly different) partition. Otherwise,
8215 : : for things like template instantiations, make_dependency might
8216 : : not realise that this is also provided from a partition and
8217 : : should be considered part of this module (and thus always
8218 : : emitted into the primary interface's CMI). */
8219 : 150 : module_state *imp = import_entity_module (*slot);
8220 : 150 : if (!imp->is_partition ())
8221 : 24 : *slot = ident;
8222 : : }
8223 : : }
8224 : :
8225 : : return true;
8226 : : }
8227 : :
8228 : : static bool has_definition (tree decl);
8229 : :
8230 : : /* DECL is a decl node that must be written by value. DEP is the
8231 : : decl's depset. */
8232 : :
8233 : : void
8234 : 2920683 : trees_out::decl_value (tree decl, depset *dep)
8235 : : {
8236 : : /* We should not be writing clones or template parms. */
8237 : 2920683 : gcc_checking_assert (DECL_P (decl)
8238 : : && !DECL_CLONED_FUNCTION_P (decl)
8239 : : && !DECL_TEMPLATE_PARM_P (decl));
8240 : :
8241 : : /* We should never be writing non-typedef ptrmemfuncs by value. */
8242 : 2920683 : gcc_checking_assert (TREE_CODE (decl) != TYPE_DECL
8243 : : || DECL_ORIGINAL_TYPE (decl)
8244 : : || !TYPE_PTRMEMFUNC_P (TREE_TYPE (decl)));
8245 : :
8246 : : /* There's no need to walk any of the contents of a known TU-local entity,
8247 : : since importers should never see any of it regardless. But make sure we
8248 : : at least note its location so importers can use it for diagnostics. */
8249 : 2920683 : if (dep && dep->is_tu_local ())
8250 : : {
8251 : 434 : gcc_checking_assert (is_initial_scan ());
8252 : 434 : insert (decl, WK_value);
8253 : 434 : state->note_location (DECL_SOURCE_LOCATION (decl));
8254 : 434 : return;
8255 : : }
8256 : :
8257 : 2920249 : merge_kind mk = get_merge_kind (decl, dep);
8258 : :
8259 : 2920249 : if (CHECKING_P)
8260 : : {
8261 : : /* Never start in the middle of a template. */
8262 : 2920249 : int use_tpl = -1;
8263 : 2920249 : if (tree ti = node_template_info (decl, use_tpl))
8264 : 990776 : gcc_checking_assert (TREE_CODE (TI_TEMPLATE (ti)) == OVERLOAD
8265 : : || TREE_CODE (TI_TEMPLATE (ti)) == FIELD_DECL
8266 : : || (DECL_TEMPLATE_RESULT (TI_TEMPLATE (ti))
8267 : : != decl));
8268 : : }
8269 : :
8270 : 2920249 : if (streaming_p ())
8271 : : {
8272 : : /* A new node -> tt_decl. */
8273 : 1058893 : decl_val_count++;
8274 : 1058893 : i (tt_decl);
8275 : 1058893 : u (mk);
8276 : 1058893 : start (decl);
8277 : :
8278 : 1058893 : if (mk != MK_unique)
8279 : : {
8280 : 900806 : bits_out bits = stream_bits ();
8281 : 900806 : if (!(mk & MK_template_mask) && !state->is_header ())
8282 : : {
8283 : : /* Tell the importer whether this is a global module entity,
8284 : : or a module entity. */
8285 : 116245 : tree o = get_originating_module_decl (decl);
8286 : 116245 : bool is_attached = false;
8287 : :
8288 : 116245 : tree not_tmpl = STRIP_TEMPLATE (o);
8289 : 116245 : if (DECL_LANG_SPECIFIC (not_tmpl)
8290 : 177133 : && DECL_MODULE_ATTACH_P (not_tmpl))
8291 : : is_attached = true;
8292 : :
8293 : 116245 : bits.b (is_attached);
8294 : : }
8295 : 900806 : bits.b (dep && dep->has_defn ());
8296 : 900806 : }
8297 : 1058893 : tree_node_bools (decl);
8298 : : }
8299 : :
8300 : 2920249 : int tag = insert (decl, WK_value);
8301 : 2920249 : if (streaming_p ())
8302 : 1058893 : dump (dumper::TREE)
8303 : 683 : && dump ("Writing %s:%d %C:%N%S", merge_kind_name[mk], tag,
8304 : 683 : TREE_CODE (decl), decl, decl);
8305 : :
8306 : 2920249 : tree inner = decl;
8307 : 2920249 : int inner_tag = 0;
8308 : 2920249 : if (TREE_CODE (decl) == TEMPLATE_DECL)
8309 : : {
8310 : 836311 : inner = DECL_TEMPLATE_RESULT (decl);
8311 : 836311 : inner_tag = insert (inner, WK_value);
8312 : :
8313 : : /* On stream-in we assume that a template and its result will
8314 : : have the same type. */
8315 : 836311 : gcc_checking_assert (TREE_TYPE (decl) == TREE_TYPE (inner));
8316 : :
8317 : 836311 : if (streaming_p ())
8318 : : {
8319 : 278754 : int code = TREE_CODE (inner);
8320 : 278754 : u (code);
8321 : 278754 : start (inner, true);
8322 : 278754 : tree_node_bools (inner);
8323 : 278754 : dump (dumper::TREE)
8324 : 132 : && dump ("Writing %s:%d %C:%N%S", merge_kind_name[mk], inner_tag,
8325 : 132 : TREE_CODE (inner), inner, inner);
8326 : : }
8327 : : }
8328 : :
8329 : 2920249 : tree type = NULL_TREE;
8330 : 2920249 : int type_tag = 0;
8331 : 2920249 : tree stub_decl = NULL_TREE;
8332 : 2920249 : int stub_tag = 0;
8333 : 2920249 : if (TREE_CODE (inner) == TYPE_DECL)
8334 : : {
8335 : 1104649 : type = TREE_TYPE (inner);
8336 : 1104649 : bool has_type = (type == TYPE_MAIN_VARIANT (type)
8337 : 1104649 : && TYPE_NAME (type) == inner);
8338 : :
8339 : 1104649 : if (streaming_p ())
8340 : 372238 : u (has_type ? TREE_CODE (type) : 0);
8341 : :
8342 : 1104649 : if (has_type)
8343 : : {
8344 : 508394 : type_tag = insert (type, WK_value);
8345 : 508394 : if (streaming_p ())
8346 : : {
8347 : 169444 : start (type, true);
8348 : 169444 : tree_node_bools (type);
8349 : 169444 : dump (dumper::TREE)
8350 : 155 : && dump ("Writing type:%d %C:%N", type_tag,
8351 : 155 : TREE_CODE (type), type);
8352 : : }
8353 : :
8354 : 508394 : stub_decl = TYPE_STUB_DECL (type);
8355 : 508394 : bool has_stub = inner != stub_decl;
8356 : 508394 : if (streaming_p ())
8357 : 169444 : u (has_stub ? TREE_CODE (stub_decl) : 0);
8358 : 508394 : if (has_stub)
8359 : : {
8360 : 2169 : stub_tag = insert (stub_decl);
8361 : 2169 : if (streaming_p ())
8362 : : {
8363 : 722 : start (stub_decl, true);
8364 : 722 : tree_node_bools (stub_decl);
8365 : 722 : dump (dumper::TREE)
8366 : 0 : && dump ("Writing stub_decl:%d %C:%N", stub_tag,
8367 : 0 : TREE_CODE (stub_decl), stub_decl);
8368 : : }
8369 : : }
8370 : : else
8371 : : stub_decl = NULL_TREE;
8372 : : }
8373 : : else
8374 : : /* Regular typedef. */
8375 : : type = NULL_TREE;
8376 : : }
8377 : :
8378 : : /* Stream the container, we want it correctly canonicalized before
8379 : : we start emitting keys for this decl. */
8380 : 2920249 : tree container = decl_container (decl);
8381 : 2920249 : unsigned tpl_levels = 0;
8382 : :
8383 : : /* Also tell the importer whether this is a temploid friend attached
8384 : : to a different module (which has implications for merging), so that
8385 : : importers can reconstruct this information on stream-in. */
8386 : 2920249 : if (TREE_CODE (inner) == FUNCTION_DECL || TREE_CODE (inner) == TYPE_DECL)
8387 : : {
8388 : 2241486 : tree* temploid_friend_slot = imported_temploid_friends->get (decl);
8389 : 2241486 : gcc_checking_assert (!temploid_friend_slot || *temploid_friend_slot);
8390 : 2241486 : tree_node (temploid_friend_slot ? *temploid_friend_slot : NULL_TREE);
8391 : : }
8392 : :
8393 : 2920249 : {
8394 : 2920249 : auto wmk = make_temp_override (dep_hash->writing_merge_key, true);
8395 : 2920249 : if (decl != inner)
8396 : 836311 : tpl_header (decl, &tpl_levels);
8397 : 2920249 : if (TREE_CODE (inner) == FUNCTION_DECL)
8398 : 1136837 : fn_parms_init (inner);
8399 : :
8400 : : /* Now write out the merging information, and then really
8401 : : install the tag values. */
8402 : 2920249 : key_mergeable (tag, mk, decl, inner, container, dep);
8403 : :
8404 : 2920249 : if (streaming_p ())
8405 : 2923011 : dump (dumper::MERGE)
8406 : 798 : && dump ("Wrote:%d's %s merge key %C:%N", tag,
8407 : 798 : merge_kind_name[mk], TREE_CODE (decl), decl);
8408 : 2920249 : }
8409 : :
8410 : 2920249 : if (TREE_CODE (inner) == FUNCTION_DECL)
8411 : 2920249 : fn_parms_fini (inner);
8412 : :
8413 : 2920249 : if (!is_key_order ())
8414 : 2127345 : tree_node_vals (decl);
8415 : :
8416 : 2920249 : if (inner_tag)
8417 : : {
8418 : 836311 : if (!is_key_order ())
8419 : 557557 : tree_node_vals (inner);
8420 : 836311 : tpl_parms_fini (decl, tpl_levels);
8421 : : }
8422 : :
8423 : 2920249 : if (type && !is_key_order ())
8424 : : {
8425 : 338950 : tree_node_vals (type);
8426 : 338950 : if (stub_decl)
8427 : 1447 : tree_node_vals (stub_decl);
8428 : : }
8429 : :
8430 : 2920249 : if (!is_key_order ())
8431 : : {
8432 : 2127345 : if (mk & MK_template_mask
8433 : 1499527 : || mk == MK_partial
8434 : 1499527 : || mk == MK_friend_spec)
8435 : : {
8436 : 28702 : if (mk != MK_partial)
8437 : : {
8438 : : // FIXME: We should make use of the merge-key by
8439 : : // exposing it outside of key_mergeable. But this gets
8440 : : // the job done.
8441 : 627818 : auto *entry = reinterpret_cast <spec_entry *> (dep->deps[0]);
8442 : :
8443 : 627818 : if (streaming_p ())
8444 : 313909 : u (get_mergeable_specialization_flags (mk & MK_tmpl_decl_mask,
8445 : : entry->tmpl, decl));
8446 : 627818 : tree_node (entry->tmpl);
8447 : 627818 : tree_node (entry->args);
8448 : : }
8449 : : else
8450 : : {
8451 : 28702 : tree ti = get_template_info (inner);
8452 : 28702 : tree_node (TI_TEMPLATE (ti));
8453 : 28702 : tree_node (TI_ARGS (ti));
8454 : : }
8455 : : }
8456 : 2127345 : tree_node (get_constraints (decl));
8457 : : }
8458 : :
8459 : 2920249 : if (streaming_p ())
8460 : : {
8461 : : /* Do not stray outside this section. */
8462 : 1058893 : gcc_checking_assert (!dep || dep->section == dep_hash->section);
8463 : :
8464 : : /* Write the entity index, so we can insert it as soon as we
8465 : : know this is new. */
8466 : 1058893 : install_entity (decl, dep);
8467 : : }
8468 : :
8469 : 2920249 : if (DECL_LANG_SPECIFIC (inner)
8470 : 2639959 : && DECL_MODULE_KEYED_DECLS_P (inner)
8471 : 2920687 : && !is_key_order ())
8472 : : {
8473 : : /* Stream the keyed entities. */
8474 : 296 : auto *attach_vec = keyed_table->get (inner);
8475 : 296 : unsigned num = attach_vec->length ();
8476 : 296 : if (streaming_p ())
8477 : 142 : u (num);
8478 : 613 : for (unsigned ix = 0; ix != num; ix++)
8479 : : {
8480 : 317 : tree attached = (*attach_vec)[ix];
8481 : 317 : tree_node (attached);
8482 : 317 : if (streaming_p ())
8483 : 332 : dump (dumper::MERGE)
8484 : 15 : && dump ("Written %d[%u] attached decl %N", tag, ix, attached);
8485 : : }
8486 : : }
8487 : :
8488 : 2920249 : bool is_typedef = false;
8489 : 2920249 : if (!type && TREE_CODE (inner) == TYPE_DECL)
8490 : : {
8491 : 596255 : tree t = TREE_TYPE (inner);
8492 : 596255 : unsigned tdef_flags = 0;
8493 : 596255 : if (DECL_ORIGINAL_TYPE (inner)
8494 : 596255 : && TYPE_NAME (TREE_TYPE (inner)) == inner)
8495 : : {
8496 : 596255 : tdef_flags |= 1;
8497 : 596255 : if (TYPE_STRUCTURAL_EQUALITY_P (t)
8498 : 129322 : && TYPE_DEPENDENT_P_VALID (t)
8499 : 719082 : && TYPE_DEPENDENT_P (t))
8500 : : tdef_flags |= 2;
8501 : : }
8502 : 596255 : if (streaming_p ())
8503 : 202794 : u (tdef_flags);
8504 : :
8505 : 596255 : if (tdef_flags & 1)
8506 : : {
8507 : : /* A typedef type. */
8508 : 596255 : int type_tag = insert (t);
8509 : 596255 : if (streaming_p ())
8510 : 202794 : dump (dumper::TREE)
8511 : 206 : && dump ("Cloned:%d %s %C:%N", type_tag,
8512 : : tdef_flags & 2 ? "depalias" : "typedef",
8513 : 206 : TREE_CODE (t), t);
8514 : :
8515 : : is_typedef = true;
8516 : : }
8517 : : }
8518 : :
8519 : 2920249 : if (streaming_p () && DECL_MAYBE_IN_CHARGE_CDTOR_P (decl))
8520 : : {
8521 : 79372 : bool cloned_p
8522 : 79372 : = (DECL_CHAIN (decl) && DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl)));
8523 : 55546 : bool needs_vtt_parm_p
8524 : 55546 : = (cloned_p && CLASSTYPE_VBASECLASSES (DECL_CONTEXT (decl)));
8525 : 55546 : bool omit_inherited_parms_p
8526 : 55546 : = (cloned_p && DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl)
8527 : 43867 : && base_ctor_omit_inherited_parms (decl));
8528 : 79372 : unsigned flags = (int (cloned_p) << 0
8529 : 79372 : | int (needs_vtt_parm_p) << 1
8530 : 79372 : | int (omit_inherited_parms_p) << 2);
8531 : 79372 : u (flags);
8532 : 79451 : dump (dumper::TREE) && dump ("CDTOR %N is %scloned",
8533 : : decl, cloned_p ? "" : "not ");
8534 : : }
8535 : :
8536 : 2920249 : if (streaming_p () && VAR_P (decl) && CP_DECL_THREAD_LOCAL_P (decl))
8537 : 172 : u (decl_tls_model (decl));
8538 : :
8539 : 2920249 : if (streaming_p ())
8540 : 1058893 : dump (dumper::TREE) && dump ("Written decl:%d %C:%N", tag,
8541 : 683 : TREE_CODE (decl), decl);
8542 : :
8543 : 2920249 : if (NAMESPACE_SCOPE_P (inner))
8544 : 1756538 : gcc_checking_assert (!dep == (VAR_OR_FUNCTION_DECL_P (inner)
8545 : : && DECL_LOCAL_DECL_P (inner)));
8546 : 2041862 : else if ((TREE_CODE (inner) == TYPE_DECL
8547 : 587299 : && !is_typedef
8548 : 114527 : && TYPE_NAME (TREE_TYPE (inner)) == inner)
8549 : 2514634 : || TREE_CODE (inner) == FUNCTION_DECL)
8550 : : {
8551 : 947057 : bool write_defn = !dep && has_definition (decl);
8552 : 947057 : if (streaming_p ())
8553 : 315818 : u (write_defn);
8554 : 947057 : if (write_defn)
8555 : 0 : write_definition (decl);
8556 : : }
8557 : : }
8558 : :
8559 : : tree
8560 : 866974 : trees_in::decl_value ()
8561 : : {
8562 : 866974 : int tag = 0;
8563 : 866974 : bool is_attached = false;
8564 : 866974 : bool has_defn = false;
8565 : 866974 : unsigned mk_u = u ();
8566 : 866974 : if (mk_u >= MK_hwm || !merge_kind_name[mk_u])
8567 : : {
8568 : 0 : set_overrun ();
8569 : 0 : return NULL_TREE;
8570 : : }
8571 : :
8572 : 866974 : unsigned saved_unused = unused;
8573 : 866974 : unused = 0;
8574 : :
8575 : 866974 : merge_kind mk = merge_kind (mk_u);
8576 : :
8577 : 866974 : tree decl = start ();
8578 : 866974 : if (decl)
8579 : : {
8580 : 866974 : if (mk != MK_unique)
8581 : : {
8582 : 724117 : bits_in bits = stream_bits ();
8583 : 724117 : if (!(mk & MK_template_mask) && !state->is_header ())
8584 : 47493 : is_attached = bits.b ();
8585 : :
8586 : 724117 : has_defn = bits.b ();
8587 : 724117 : }
8588 : :
8589 : 866974 : if (!tree_node_bools (decl))
8590 : 0 : decl = NULL_TREE;
8591 : : }
8592 : :
8593 : : /* Insert into map. */
8594 : 866974 : tag = insert (decl);
8595 : 866974 : if (decl)
8596 : 866974 : dump (dumper::TREE)
8597 : 964 : && dump ("Reading:%d %C", tag, TREE_CODE (decl));
8598 : :
8599 : 866974 : tree inner = decl;
8600 : 866974 : int inner_tag = 0;
8601 : 866974 : if (decl && TREE_CODE (decl) == TEMPLATE_DECL)
8602 : : {
8603 : 232007 : int code = u ();
8604 : 232007 : inner = start (code);
8605 : 232007 : if (inner && tree_node_bools (inner))
8606 : 232007 : DECL_TEMPLATE_RESULT (decl) = inner;
8607 : : else
8608 : 0 : decl = NULL_TREE;
8609 : :
8610 : 232007 : inner_tag = insert (inner);
8611 : 232007 : if (decl)
8612 : 232007 : dump (dumper::TREE)
8613 : 204 : && dump ("Reading:%d %C", inner_tag, TREE_CODE (inner));
8614 : : }
8615 : :
8616 : 866974 : tree type = NULL_TREE;
8617 : 866974 : int type_tag = 0;
8618 : 866974 : tree stub_decl = NULL_TREE;
8619 : 866974 : int stub_tag = 0;
8620 : 866974 : if (decl && TREE_CODE (inner) == TYPE_DECL)
8621 : : {
8622 : 286740 : if (unsigned type_code = u ())
8623 : : {
8624 : 129994 : type = start (type_code);
8625 : 129994 : if (type && tree_node_bools (type))
8626 : : {
8627 : 129994 : TREE_TYPE (inner) = type;
8628 : 129994 : TYPE_NAME (type) = inner;
8629 : : }
8630 : : else
8631 : 0 : decl = NULL_TREE;
8632 : :
8633 : 129994 : type_tag = insert (type);
8634 : 129994 : if (decl)
8635 : 129994 : dump (dumper::TREE)
8636 : 212 : && dump ("Reading type:%d %C", type_tag, TREE_CODE (type));
8637 : :
8638 : 129994 : if (unsigned stub_code = u ())
8639 : : {
8640 : 413 : stub_decl = start (stub_code);
8641 : 413 : if (stub_decl && tree_node_bools (stub_decl))
8642 : : {
8643 : 413 : TREE_TYPE (stub_decl) = type;
8644 : 413 : TYPE_STUB_DECL (type) = stub_decl;
8645 : : }
8646 : : else
8647 : 0 : decl = NULL_TREE;
8648 : :
8649 : 413 : stub_tag = insert (stub_decl);
8650 : 413 : if (decl)
8651 : 413 : dump (dumper::TREE)
8652 : 0 : && dump ("Reading stub_decl:%d %C", stub_tag,
8653 : 0 : TREE_CODE (stub_decl));
8654 : : }
8655 : : }
8656 : : }
8657 : :
8658 : 866974 : if (!decl)
8659 : : {
8660 : 0 : bail:
8661 : 0 : if (inner_tag != 0)
8662 : 0 : back_refs[~inner_tag] = NULL_TREE;
8663 : 0 : if (type_tag != 0)
8664 : 0 : back_refs[~type_tag] = NULL_TREE;
8665 : 0 : if (stub_tag != 0)
8666 : 0 : back_refs[~stub_tag] = NULL_TREE;
8667 : 0 : if (tag != 0)
8668 : 0 : back_refs[~tag] = NULL_TREE;
8669 : 0 : set_overrun ();
8670 : : /* Bail. */
8671 : 0 : unused = saved_unused;
8672 : 0 : return NULL_TREE;
8673 : : }
8674 : :
8675 : : /* Read the container, to ensure it's already been streamed in. */
8676 : 866974 : tree container = decl_container ();
8677 : 866974 : unsigned tpl_levels = 0;
8678 : :
8679 : : /* If this is an imported temploid friend, get the owning decl its
8680 : : attachment is determined by (or NULL_TREE otherwise). */
8681 : 866974 : tree temploid_friend = NULL_TREE;
8682 : 866974 : if (TREE_CODE (inner) == FUNCTION_DECL || TREE_CODE (inner) == TYPE_DECL)
8683 : 611148 : temploid_friend = tree_node ();
8684 : :
8685 : : /* Figure out if this decl is already known about. */
8686 : 866974 : int parm_tag = 0;
8687 : :
8688 : 866974 : if (decl != inner)
8689 : 232007 : if (!tpl_header (decl, &tpl_levels))
8690 : 0 : goto bail;
8691 : 866974 : if (TREE_CODE (inner) == FUNCTION_DECL)
8692 : 324408 : parm_tag = fn_parms_init (inner);
8693 : :
8694 : 866974 : tree existing = key_mergeable (tag, mk, decl, inner, type, container,
8695 : : is_attached, temploid_friend);
8696 : 866974 : tree existing_inner = existing;
8697 : 866974 : if (existing)
8698 : : {
8699 : 417605 : if (existing == error_mark_node)
8700 : 0 : goto bail;
8701 : :
8702 : 417605 : if (TREE_CODE (STRIP_TEMPLATE (existing)) == TYPE_DECL)
8703 : : {
8704 : 156936 : tree etype = TREE_TYPE (existing);
8705 : 156936 : if (TYPE_LANG_SPECIFIC (etype)
8706 : 113355 : && COMPLETE_TYPE_P (etype)
8707 : 222862 : && !CLASSTYPE_MEMBER_VEC (etype))
8708 : : /* Give it a member vec, we're likely gonna be looking
8709 : : inside it. */
8710 : 13702 : set_class_bindings (etype, -1);
8711 : : }
8712 : :
8713 : : /* Install the existing decl into the back ref array. */
8714 : 417605 : register_duplicate (decl, existing);
8715 : 417605 : back_refs[~tag] = existing;
8716 : 417605 : if (inner_tag != 0)
8717 : : {
8718 : 141133 : existing_inner = DECL_TEMPLATE_RESULT (existing);
8719 : 141133 : back_refs[~inner_tag] = existing_inner;
8720 : : }
8721 : :
8722 : 417605 : if (type_tag != 0)
8723 : : {
8724 : 75030 : tree existing_type = TREE_TYPE (existing);
8725 : 75030 : back_refs[~type_tag] = existing_type;
8726 : 75030 : if (stub_tag != 0)
8727 : 254 : back_refs[~stub_tag] = TYPE_STUB_DECL (existing_type);
8728 : : }
8729 : : }
8730 : :
8731 : 866974 : if (parm_tag)
8732 : 324408 : fn_parms_fini (parm_tag, inner, existing_inner, has_defn);
8733 : :
8734 : 866974 : if (!tree_node_vals (decl))
8735 : 0 : goto bail;
8736 : :
8737 : 866974 : if (inner_tag)
8738 : : {
8739 : 232007 : gcc_checking_assert (DECL_TEMPLATE_RESULT (decl) == inner);
8740 : :
8741 : 232007 : if (!tree_node_vals (inner))
8742 : 0 : goto bail;
8743 : :
8744 : 232007 : if (!tpl_parms_fini (decl, tpl_levels))
8745 : 0 : goto bail;
8746 : : }
8747 : :
8748 : 866974 : if (type && (!tree_node_vals (type)
8749 : 129994 : || (stub_decl && !tree_node_vals (stub_decl))))
8750 : 0 : goto bail;
8751 : :
8752 : 866974 : spec_entry spec;
8753 : 866974 : unsigned spec_flags = 0;
8754 : 866974 : if (mk & MK_template_mask
8755 : 606252 : || mk == MK_partial
8756 : 606252 : || mk == MK_friend_spec)
8757 : : {
8758 : 9534 : if (mk == MK_partial)
8759 : : spec_flags = 2;
8760 : : else
8761 : 260722 : spec_flags = u ();
8762 : :
8763 : 270256 : spec.tmpl = tree_node ();
8764 : 270256 : spec.args = tree_node ();
8765 : : }
8766 : : /* Hold constraints on the spec field, for a short while. */
8767 : 866974 : spec.spec = tree_node ();
8768 : :
8769 : 867938 : dump (dumper::TREE) && dump ("Read:%d %C:%N", tag, TREE_CODE (decl), decl);
8770 : :
8771 : 866974 : existing = back_refs[~tag];
8772 : 866974 : bool installed = install_entity (existing);
8773 : 866974 : bool is_new = existing == decl;
8774 : :
8775 : 866974 : if (DECL_LANG_SPECIFIC (inner)
8776 : 1633314 : && DECL_MODULE_KEYED_DECLS_P (inner))
8777 : : {
8778 : : /* Read and maybe install the attached entities. */
8779 : 167 : bool existed;
8780 : 167 : auto &set = keyed_table->get_or_insert (STRIP_TEMPLATE (existing),
8781 : : &existed);
8782 : 167 : unsigned num = u ();
8783 : 167 : if (is_new == existed)
8784 : 0 : set_overrun ();
8785 : 167 : if (is_new)
8786 : 56 : set.reserve (num);
8787 : 350 : for (unsigned ix = 0; !get_overrun () && ix != num; ix++)
8788 : : {
8789 : 183 : tree attached = tree_node ();
8790 : 183 : dump (dumper::MERGE)
8791 : 60 : && dump ("Read %d[%u] %s attached decl %N", tag, ix,
8792 : : is_new ? "new" : "matched", attached);
8793 : 183 : if (is_new)
8794 : 62 : set.quick_push (attached);
8795 : 121 : else if (set[ix] != attached)
8796 : 0 : set_overrun ();
8797 : : }
8798 : : }
8799 : :
8800 : : /* Regular typedefs will have a NULL TREE_TYPE at this point. */
8801 : 866974 : unsigned tdef_flags = 0;
8802 : 866974 : bool is_typedef = false;
8803 : 866974 : if (!type && TREE_CODE (inner) == TYPE_DECL)
8804 : : {
8805 : 156746 : tdef_flags = u ();
8806 : 156746 : if (tdef_flags & 1)
8807 : 156746 : is_typedef = true;
8808 : : }
8809 : :
8810 : 866974 : if (is_new)
8811 : : {
8812 : : /* A newly discovered node. */
8813 : 449369 : if (TREE_CODE (decl) == FUNCTION_DECL && DECL_VIRTUAL_P (decl))
8814 : : /* Mark this identifier as naming a virtual function --
8815 : : lookup_overrides relies on this optimization. */
8816 : 3992 : IDENTIFIER_VIRTUAL_P (DECL_NAME (decl)) = true;
8817 : :
8818 : 449369 : if (installed)
8819 : : {
8820 : : /* Mark the entity as imported. */
8821 : 268187 : retrofit_lang_decl (inner);
8822 : 268187 : DECL_MODULE_IMPORT_P (inner) = true;
8823 : : }
8824 : :
8825 : 449369 : if (temploid_friend)
8826 : 34 : imported_temploid_friends->put (decl, temploid_friend);
8827 : :
8828 : 449369 : if (spec.spec)
8829 : 16866 : set_constraints (decl, spec.spec);
8830 : :
8831 : 449369 : if (TREE_CODE (decl) == INTEGER_CST && !TREE_OVERFLOW (decl))
8832 : : {
8833 : 0 : decl = cache_integer_cst (decl, true);
8834 : 0 : back_refs[~tag] = decl;
8835 : : }
8836 : :
8837 : 449369 : if (is_typedef)
8838 : : {
8839 : : /* Frob it to be ready for cloning. */
8840 : 74840 : TREE_TYPE (inner) = DECL_ORIGINAL_TYPE (inner);
8841 : 74840 : DECL_ORIGINAL_TYPE (inner) = NULL_TREE;
8842 : 74840 : if (TREE_CODE (TREE_TYPE (inner)) != TU_LOCAL_ENTITY)
8843 : : {
8844 : 74837 : set_underlying_type (inner);
8845 : 74837 : if (tdef_flags & 2)
8846 : : {
8847 : : /* Match instantiate_alias_template's handling. */
8848 : 18099 : tree type = TREE_TYPE (inner);
8849 : 18099 : TYPE_DEPENDENT_P (type) = true;
8850 : 18099 : TYPE_DEPENDENT_P_VALID (type) = true;
8851 : 18099 : SET_TYPE_STRUCTURAL_EQUALITY (type);
8852 : : }
8853 : : }
8854 : : }
8855 : :
8856 : 449369 : if (inner_tag)
8857 : : /* Set the TEMPLATE_DECL's type. */
8858 : 90874 : TREE_TYPE (decl) = TREE_TYPE (inner);
8859 : :
8860 : : /* Redetermine whether we need to import or export this declaration
8861 : : for this TU. But for extern templates we know we must import:
8862 : : they'll be defined in a different TU.
8863 : : FIXME: How do dllexport and dllimport interact across a module?
8864 : : See also https://github.com/itanium-cxx-abi/cxx-abi/issues/170.
8865 : : May have to revisit? */
8866 : 449369 : if (type
8867 : 54964 : && CLASS_TYPE_P (type)
8868 : 46372 : && TYPE_LANG_SPECIFIC (type)
8869 : 495741 : && !(CLASSTYPE_EXPLICIT_INSTANTIATION (type)
8870 : 501 : && CLASSTYPE_INTERFACE_KNOWN (type)
8871 : 501 : && CLASSTYPE_INTERFACE_ONLY (type)))
8872 : : {
8873 : 45920 : CLASSTYPE_INTERFACE_ONLY (type) = false;
8874 : 45920 : CLASSTYPE_INTERFACE_UNKNOWN (type) = true;
8875 : : }
8876 : :
8877 : : /* Add to specialization tables now that constraints etc are
8878 : : added. */
8879 : 449369 : if (mk == MK_partial)
8880 : : {
8881 : 3484 : bool is_type = TREE_CODE (inner) == TYPE_DECL;
8882 : 3484 : spec.spec = is_type ? type : inner;
8883 : 3484 : add_mergeable_specialization (!is_type, &spec, decl, spec_flags);
8884 : : }
8885 : 445885 : else if (mk & MK_template_mask)
8886 : : {
8887 : 117372 : bool is_type = !(mk & MK_tmpl_decl_mask);
8888 : 117372 : spec.spec = is_type ? type : mk & MK_tmpl_tmpl_mask ? inner : decl;
8889 : 117372 : add_mergeable_specialization (!is_type, &spec, decl, spec_flags);
8890 : : }
8891 : :
8892 : 449369 : if (NAMESPACE_SCOPE_P (decl)
8893 : 94246 : && (mk == MK_named || mk == MK_unique
8894 : 94246 : || mk == MK_enum || mk == MK_friend_spec)
8895 : 495011 : && !(VAR_OR_FUNCTION_DECL_P (decl) && DECL_LOCAL_DECL_P (decl)))
8896 : 45567 : add_module_namespace_decl (CP_DECL_CONTEXT (decl), decl);
8897 : :
8898 : 449369 : if (DECL_ARTIFICIAL (decl)
8899 : 127034 : && TREE_CODE (decl) == FUNCTION_DECL
8900 : 12585 : && !DECL_TEMPLATE_INFO (decl)
8901 : 12341 : && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
8902 : 12205 : && TYPE_SIZE (DECL_CONTEXT (decl))
8903 : 450659 : && !DECL_THUNK_P (decl))
8904 : : /* A new implicit member function, when the class is
8905 : : complete. This means the importee declared it, and
8906 : : we must now add it to the class. Note that implicit
8907 : : member fns of template instantiations do not themselves
8908 : : look like templates. */
8909 : 864 : if (!install_implicit_member (inner))
8910 : 0 : set_overrun ();
8911 : :
8912 : : /* When importing a TLS wrapper from a header unit, we haven't
8913 : : actually emitted its definition yet. Remember it so we can
8914 : : do this later. */
8915 : 449369 : if (state->is_header ()
8916 : 449369 : && decl_tls_wrapper_p (decl))
8917 : 6 : note_vague_linkage_fn (decl);
8918 : :
8919 : : /* Setup aliases for the declaration. */
8920 : 449369 : if (tree alias = lookup_attribute ("alias", DECL_ATTRIBUTES (decl)))
8921 : : {
8922 : 3 : alias = TREE_VALUE (TREE_VALUE (alias));
8923 : 3 : alias = get_identifier (TREE_STRING_POINTER (alias));
8924 : 3 : assemble_alias (decl, alias);
8925 : : }
8926 : : }
8927 : : else
8928 : : {
8929 : : /* DECL is the to-be-discarded decl. Its internal pointers will
8930 : : be to the EXISTING's structure. Frob it to point to its
8931 : : own other structures, so loading its definition will alter
8932 : : it, and not the existing decl. */
8933 : 419040 : dump (dumper::MERGE) && dump ("Deduping %N", existing);
8934 : :
8935 : 417605 : if (inner_tag)
8936 : 141133 : DECL_TEMPLATE_RESULT (decl) = inner;
8937 : :
8938 : 417605 : if (type)
8939 : : {
8940 : : /* Point at the to-be-discarded type & decl. */
8941 : 75030 : TYPE_NAME (type) = inner;
8942 : 75030 : TREE_TYPE (inner) = type;
8943 : :
8944 : 149806 : TYPE_STUB_DECL (type) = stub_decl ? stub_decl : inner;
8945 : 75030 : if (stub_decl)
8946 : 254 : TREE_TYPE (stub_decl) = type;
8947 : :
8948 : 75030 : tree etype = TREE_TYPE (existing);
8949 : :
8950 : : /* Handle separate declarations with different attributes. */
8951 : 75030 : tree &dattr = TYPE_ATTRIBUTES (type);
8952 : 75030 : tree &eattr = TYPE_ATTRIBUTES (etype);
8953 : 75030 : check_abi_tags (existing, decl, eattr, dattr);
8954 : : // TODO: handle other conflicting type attributes
8955 : 75030 : eattr = merge_attributes (eattr, dattr);
8956 : :
8957 : : /* When merging a partial specialisation, the existing decl may have
8958 : : had its TYPE_CANONICAL adjusted. If so we should use structural
8959 : : equality to ensure is_matching_decl doesn't get confused. */
8960 : 75030 : if ((spec_flags & 2)
8961 : 75030 : && TYPE_CANONICAL (type) != TYPE_CANONICAL (etype))
8962 : 3 : SET_TYPE_STRUCTURAL_EQUALITY (type);
8963 : : }
8964 : :
8965 : 417605 : if (inner_tag)
8966 : : /* Set the TEMPLATE_DECL's type. */
8967 : 141133 : TREE_TYPE (decl) = TREE_TYPE (inner);
8968 : :
8969 : 417605 : if (!is_matching_decl (existing, decl, is_typedef))
8970 : 42 : unmatched_duplicate (existing);
8971 : :
8972 : 417605 : if (TREE_CODE (inner) == FUNCTION_DECL)
8973 : : {
8974 : 188461 : tree e_inner = STRIP_TEMPLATE (existing);
8975 : 188461 : for (auto parm = DECL_ARGUMENTS (inner);
8976 : 565241 : parm; parm = DECL_CHAIN (parm))
8977 : 376780 : DECL_CONTEXT (parm) = e_inner;
8978 : : }
8979 : :
8980 : : /* And our result is the existing node. */
8981 : 417605 : decl = existing;
8982 : : }
8983 : :
8984 : 866974 : if (mk == MK_friend_spec)
8985 : : {
8986 : 0 : tree e = match_mergeable_specialization (true, &spec);
8987 : 0 : if (!e)
8988 : : {
8989 : 0 : spec.spec = inner;
8990 : 0 : add_mergeable_specialization (true, &spec, decl, spec_flags);
8991 : : }
8992 : 0 : else if (e != existing)
8993 : 0 : set_overrun ();
8994 : : }
8995 : :
8996 : 866974 : if (is_typedef)
8997 : : {
8998 : : /* Insert the type into the array now. */
8999 : 156746 : tag = insert (TREE_TYPE (decl));
9000 : 156746 : dump (dumper::TREE)
9001 : 247 : && dump ("Cloned:%d typedef %C:%N",
9002 : 247 : tag, TREE_CODE (TREE_TYPE (decl)), TREE_TYPE (decl));
9003 : : }
9004 : :
9005 : 866974 : unused = saved_unused;
9006 : :
9007 : 866974 : if (DECL_MAYBE_IN_CHARGE_CDTOR_P (decl))
9008 : : {
9009 : 69582 : unsigned flags = u ();
9010 : :
9011 : 69582 : if (is_new)
9012 : : {
9013 : 30850 : bool cloned_p = flags & 1;
9014 : 30950 : dump (dumper::TREE) && dump ("CDTOR %N is %scloned",
9015 : : decl, cloned_p ? "" : "not ");
9016 : 30850 : if (cloned_p)
9017 : : {
9018 : : /* Update the member vec, if there is one (we're in a different
9019 : : cluster to the class defn) and this isn't a primary template
9020 : : specialization (as in tsubst_function_decl). */
9021 : 22808 : bool up = (CLASSTYPE_MEMBER_VEC (DECL_CONTEXT (decl))
9022 : 22808 : && !primary_template_specialization_p (decl));
9023 : 22808 : build_cdtor_clones (decl, flags & 2, flags & 4, up);
9024 : : }
9025 : : }
9026 : : }
9027 : :
9028 : 866974 : if (VAR_P (decl) && CP_DECL_THREAD_LOCAL_P (decl))
9029 : : {
9030 : 162 : enum tls_model model = tls_model (u ());
9031 : 162 : if (is_new)
9032 : 142 : set_decl_tls_model (decl, model);
9033 : : }
9034 : :
9035 : 866974 : if (!NAMESPACE_SCOPE_P (inner)
9036 : 634242 : && ((TREE_CODE (inner) == TYPE_DECL
9037 : 153305 : && !is_typedef
9038 : 27898 : && TYPE_NAME (TREE_TYPE (inner)) == inner)
9039 : 606344 : || TREE_CODE (inner) == FUNCTION_DECL)
9040 : 1134123 : && u ())
9041 : 0 : read_definition (decl);
9042 : :
9043 : : return decl;
9044 : : }
9045 : :
9046 : : /* DECL is an unnameable member of CTX. Return a suitable identifying
9047 : : index. */
9048 : :
9049 : : static unsigned
9050 : 798 : get_field_ident (tree ctx, tree decl)
9051 : : {
9052 : 798 : gcc_checking_assert (TREE_CODE (decl) == USING_DECL
9053 : : || !DECL_NAME (decl)
9054 : : || IDENTIFIER_ANON_P (DECL_NAME (decl)));
9055 : :
9056 : 798 : unsigned ix = 0;
9057 : 798 : for (tree fields = TYPE_FIELDS (ctx);
9058 : 6641 : fields; fields = DECL_CHAIN (fields))
9059 : : {
9060 : 6641 : if (fields == decl)
9061 : 798 : return ix;
9062 : :
9063 : 5843 : if (DECL_CONTEXT (fields) == ctx
9064 : 5843 : && (TREE_CODE (fields) == USING_DECL
9065 : 5832 : || (TREE_CODE (fields) == FIELD_DECL
9066 : 19 : && (!DECL_NAME (fields)
9067 : 7 : || IDENTIFIER_ANON_P (DECL_NAME (fields))))))
9068 : : /* Count this field. */
9069 : 15 : ix++;
9070 : : }
9071 : 0 : gcc_unreachable ();
9072 : : }
9073 : :
9074 : : static tree
9075 : 680 : lookup_field_ident (tree ctx, unsigned ix)
9076 : : {
9077 : 680 : for (tree fields = TYPE_FIELDS (ctx);
9078 : 5452 : fields; fields = DECL_CHAIN (fields))
9079 : 5452 : if (DECL_CONTEXT (fields) == ctx
9080 : 5452 : && (TREE_CODE (fields) == USING_DECL
9081 : 5440 : || (TREE_CODE (fields) == FIELD_DECL
9082 : 692 : && (!DECL_NAME (fields)
9083 : 3 : || IDENTIFIER_ANON_P (DECL_NAME (fields))))))
9084 : 695 : if (!ix--)
9085 : : return fields;
9086 : :
9087 : : return NULL_TREE;
9088 : : }
9089 : :
9090 : : /* Reference DECL. REF indicates the walk kind we are performing.
9091 : : Return true if we should write this decl by value. */
9092 : :
9093 : : bool
9094 : 9525051 : trees_out::decl_node (tree decl, walk_kind ref)
9095 : : {
9096 : 9525051 : gcc_checking_assert (DECL_P (decl) && !DECL_TEMPLATE_PARM_P (decl)
9097 : : && DECL_CONTEXT (decl));
9098 : :
9099 : 9525051 : if (ref == WK_value)
9100 : : {
9101 : 1011940 : depset *dep = dep_hash->find_dependency (decl);
9102 : 1011940 : decl_value (decl, dep);
9103 : 1011940 : return false;
9104 : : }
9105 : :
9106 : 8513111 : switch (TREE_CODE (decl))
9107 : : {
9108 : : default:
9109 : : break;
9110 : :
9111 : 885406 : case FUNCTION_DECL:
9112 : 885406 : gcc_checking_assert (!DECL_LOCAL_DECL_P (decl));
9113 : : break;
9114 : :
9115 : : case RESULT_DECL:
9116 : : /* Unlike PARM_DECLs, RESULT_DECLs are only generated and
9117 : : referenced when we're inside the function itself. */
9118 : : return true;
9119 : :
9120 : 137080 : case PARM_DECL:
9121 : 137080 : {
9122 : 137080 : if (streaming_p ())
9123 : 61199 : i (tt_parm);
9124 : 137080 : tree_node (DECL_CONTEXT (decl));
9125 : :
9126 : : /* That must have put this in the map. */
9127 : 137080 : walk_kind ref = ref_node (decl);
9128 : 137080 : if (ref != WK_none)
9129 : : // FIXME:OPTIMIZATION We can wander into bits of the
9130 : : // template this was instantiated from, for instance
9131 : : // deferred noexcept and default parms, or references
9132 : : // to parms from earlier forward-decls (PR c++/119608).
9133 : : //
9134 : : // Currently we'll end up cloning those bits of tree.
9135 : : // It would be nice to reference those specific nodes.
9136 : : // I think putting those things in the map when we
9137 : : // reference their template by name.
9138 : : //
9139 : : // See the note in add_indirects.
9140 : : return true;
9141 : :
9142 : 0 : if (streaming_p ())
9143 : 0 : dump (dumper::TREE)
9144 : 0 : && dump ("Wrote %s reference %N",
9145 : 0 : TREE_CODE (decl) == PARM_DECL ? "parameter" : "result",
9146 : : decl);
9147 : : }
9148 : : return false;
9149 : :
9150 : : case IMPORTED_DECL:
9151 : : /* This describes a USING_DECL to the ME's debug machinery. It
9152 : : originates from the fortran FE, and has nothing to do with
9153 : : C++ modules. */
9154 : : return true;
9155 : :
9156 : : case LABEL_DECL:
9157 : : return true;
9158 : :
9159 : 46854 : case CONST_DECL:
9160 : 46854 : {
9161 : : /* If I end up cloning enum decls, implementing C++20 using
9162 : : E::v, this will need tweaking. */
9163 : 46854 : if (streaming_p ())
9164 : 10805 : i (tt_enum_decl);
9165 : 46854 : tree ctx = DECL_CONTEXT (decl);
9166 : 46854 : gcc_checking_assert (TREE_CODE (ctx) == ENUMERAL_TYPE);
9167 : 46854 : tree_node (ctx);
9168 : 46854 : tree_node (DECL_NAME (decl));
9169 : :
9170 : 46854 : int tag = insert (decl);
9171 : 46854 : if (streaming_p ())
9172 : 10805 : dump (dumper::TREE)
9173 : 21 : && dump ("Wrote enum decl:%d %C:%N", tag, TREE_CODE (decl), decl);
9174 : : return false;
9175 : : }
9176 : 14542 : break;
9177 : :
9178 : 14542 : case USING_DECL:
9179 : 14542 : if (TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
9180 : : break;
9181 : : /* FALLTHROUGH */
9182 : :
9183 : 112065 : case FIELD_DECL:
9184 : 112065 : {
9185 : 112065 : if (streaming_p ())
9186 : 6897 : i (tt_data_member);
9187 : :
9188 : 112065 : tree ctx = DECL_CONTEXT (decl);
9189 : 112065 : tree_node (ctx);
9190 : :
9191 : 112065 : tree name = NULL_TREE;
9192 : :
9193 : 112065 : if (TREE_CODE (decl) == USING_DECL)
9194 : : ;
9195 : : else
9196 : : {
9197 : 111279 : name = DECL_NAME (decl);
9198 : 216607 : if (name && IDENTIFIER_ANON_P (name))
9199 : : name = NULL_TREE;
9200 : : }
9201 : :
9202 : 112065 : tree_node (name);
9203 : 112065 : if (!name && streaming_p ())
9204 : : {
9205 : 798 : unsigned ix = get_field_ident (ctx, decl);
9206 : 798 : u (ix);
9207 : : }
9208 : :
9209 : 112065 : int tag = insert (decl);
9210 : 112065 : if (streaming_p ())
9211 : 6897 : dump (dumper::TREE)
9212 : 26 : && dump ("Wrote member:%d %C:%N", tag, TREE_CODE (decl), decl);
9213 : : return false;
9214 : : }
9215 : 374526 : break;
9216 : :
9217 : 374526 : case VAR_DECL:
9218 : 374526 : gcc_checking_assert (!DECL_LOCAL_DECL_P (decl));
9219 : 374526 : if (DECL_VTABLE_OR_VTT_P (decl))
9220 : : {
9221 : : /* VTT or VTABLE, they are all on the vtables list. */
9222 : 1792 : tree ctx = CP_DECL_CONTEXT (decl);
9223 : 1792 : tree vtable = CLASSTYPE_VTABLES (ctx);
9224 : 1804 : for (unsigned ix = 0; ; vtable = DECL_CHAIN (vtable), ix++)
9225 : 1804 : if (vtable == decl)
9226 : : {
9227 : 1792 : gcc_checking_assert (DECL_VIRTUAL_P (decl));
9228 : 1792 : if (streaming_p ())
9229 : : {
9230 : 8 : u (tt_vtable);
9231 : 8 : u (ix);
9232 : 8 : dump (dumper::TREE)
9233 : 0 : && dump ("Writing vtable %N[%u]", ctx, ix);
9234 : : }
9235 : 1792 : tree_node (ctx);
9236 : 1792 : return false;
9237 : : }
9238 : : gcc_unreachable ();
9239 : : }
9240 : :
9241 : 372734 : if (DECL_TINFO_P (decl))
9242 : : {
9243 : 5260 : tinfo:
9244 : : /* A typeinfo, tt_tinfo_typedef or tt_tinfo_var. */
9245 : 9669 : bool is_var = VAR_P (decl);
9246 : 9669 : tree type = TREE_TYPE (decl);
9247 : 9669 : unsigned ix = get_pseudo_tinfo_index (type);
9248 : 9669 : if (streaming_p ())
9249 : : {
9250 : 6153 : i (is_var ? tt_tinfo_var : tt_tinfo_typedef);
9251 : 4364 : u (ix);
9252 : : }
9253 : :
9254 : 9669 : if (is_var)
9255 : : {
9256 : : /* We also need the type it is for and mangled name, so
9257 : : the reader doesn't need to complete the type (which
9258 : : would break section ordering). The type it is for is
9259 : : stashed on the name's TREE_TYPE. */
9260 : 5260 : tree name = DECL_NAME (decl);
9261 : 5260 : tree_node (name);
9262 : 5260 : type = TREE_TYPE (name);
9263 : 5260 : tree_node (type);
9264 : : }
9265 : :
9266 : 9669 : int tag = insert (decl);
9267 : 9669 : if (streaming_p ())
9268 : 4364 : dump (dumper::TREE)
9269 : 27 : && dump ("Wrote tinfo_%s:%d %u %N", is_var ? "var" : "type",
9270 : : tag, ix, type);
9271 : :
9272 : 9669 : if (!is_var)
9273 : : {
9274 : 4409 : tag = insert (type);
9275 : 4409 : if (streaming_p ())
9276 : 1789 : dump (dumper::TREE)
9277 : 9 : && dump ("Wrote tinfo_type:%d %u %N", tag, ix, type);
9278 : : }
9279 : 9669 : return false;
9280 : : }
9281 : :
9282 : 367474 : if (DECL_NTTP_OBJECT_P (decl))
9283 : : {
9284 : : /* A NTTP parm object. */
9285 : 33 : if (streaming_p ())
9286 : 7 : i (tt_nttp_var);
9287 : 33 : tree_node (tparm_object_argument (decl));
9288 : 33 : tree_node (DECL_NAME (decl));
9289 : 33 : int tag = insert (decl);
9290 : 33 : if (streaming_p ())
9291 : 7 : dump (dumper::TREE)
9292 : 0 : && dump ("Wrote nttp object:%d %N", tag, DECL_NAME (decl));
9293 : 33 : return false;
9294 : : }
9295 : :
9296 : : break;
9297 : :
9298 : 3023567 : case TYPE_DECL:
9299 : 3023567 : if (DECL_TINFO_P (decl))
9300 : 4409 : goto tinfo;
9301 : : break;
9302 : : }
9303 : :
9304 : 7733081 : if (DECL_THUNK_P (decl))
9305 : : {
9306 : : /* Thunks are similar to binfos -- write the thunked-to decl and
9307 : : then thunk-specific key info. */
9308 : 0 : if (streaming_p ())
9309 : : {
9310 : 0 : i (tt_thunk);
9311 : 0 : i (THUNK_FIXED_OFFSET (decl));
9312 : : }
9313 : :
9314 : : tree target = decl;
9315 : 0 : while (DECL_THUNK_P (target))
9316 : 0 : target = THUNK_TARGET (target);
9317 : 0 : tree_node (target);
9318 : 0 : tree_node (THUNK_VIRTUAL_OFFSET (decl));
9319 : 0 : int tag = insert (decl);
9320 : 0 : if (streaming_p ())
9321 : 0 : dump (dumper::TREE)
9322 : 0 : && dump ("Wrote:%d thunk %N to %N", tag, DECL_NAME (decl), target);
9323 : 0 : return false;
9324 : : }
9325 : :
9326 : 7733081 : if (DECL_CLONED_FUNCTION_P (decl))
9327 : : {
9328 : 254861 : tree target = get_clone_target (decl);
9329 : 254861 : if (streaming_p ())
9330 : 120585 : i (tt_clone_ref);
9331 : :
9332 : 254861 : tree_node (target);
9333 : 254861 : tree_node (DECL_NAME (decl));
9334 : 254861 : if (DECL_VIRTUAL_P (decl))
9335 : 18993 : tree_node (DECL_VINDEX (decl));
9336 : 254861 : int tag = insert (decl);
9337 : 254861 : if (streaming_p ())
9338 : 120585 : dump (dumper::TREE)
9339 : 164 : && dump ("Wrote:%d clone %N of %N", tag, DECL_NAME (decl), target);
9340 : 254861 : return false;
9341 : : }
9342 : :
9343 : : /* Everything left should be a thing that is in the entity table.
9344 : : Mostly things that can be defined outside of their (original
9345 : : declaration) context. */
9346 : 7478220 : gcc_checking_assert (TREE_CODE (decl) == TEMPLATE_DECL
9347 : : || VAR_P (decl)
9348 : : || TREE_CODE (decl) == FUNCTION_DECL
9349 : : || TREE_CODE (decl) == TYPE_DECL
9350 : : || TREE_CODE (decl) == USING_DECL
9351 : : || TREE_CODE (decl) == CONCEPT_DECL
9352 : : || TREE_CODE (decl) == NAMESPACE_DECL);
9353 : :
9354 : 7478220 : int use_tpl = -1;
9355 : 7478220 : tree ti = node_template_info (decl, use_tpl);
9356 : 7478220 : tree tpl = NULL_TREE;
9357 : :
9358 : : /* If this is the TEMPLATE_DECL_RESULT of a TEMPLATE_DECL, get the
9359 : : TEMPLATE_DECL. Note TI_TEMPLATE is not a TEMPLATE_DECL for
9360 : : (some) friends, so we need to check that. */
9361 : : // FIXME: Should local friend template specializations be by value?
9362 : : // They don't get idents so we'll never know they're imported, but I
9363 : : // think we can only reach them from the TU that defines the
9364 : : // befriending class?
9365 : 2823514 : if (ti && TREE_CODE (TI_TEMPLATE (ti)) == TEMPLATE_DECL
9366 : 10301694 : && DECL_TEMPLATE_RESULT (TI_TEMPLATE (ti)) == decl)
9367 : : {
9368 : : tpl = TI_TEMPLATE (ti);
9369 : 721044 : partial_template:
9370 : 721044 : if (streaming_p ())
9371 : : {
9372 : 2255 : i (tt_template);
9373 : 2255 : dump (dumper::TREE)
9374 : 9 : && dump ("Writing implicit template %C:%N%S",
9375 : 9 : TREE_CODE (tpl), tpl, tpl);
9376 : : }
9377 : 721044 : tree_node (tpl);
9378 : :
9379 : : /* Streaming TPL caused us to visit DECL and maybe its type,
9380 : : if it wasn't TU-local. */
9381 : 721044 : if (CHECKING_P && !has_tu_local_dep (tpl))
9382 : : {
9383 : 721017 : gcc_checking_assert (TREE_VISITED (decl));
9384 : 721017 : if (DECL_IMPLICIT_TYPEDEF_P (decl))
9385 : 375478 : gcc_checking_assert (TREE_VISITED (TREE_TYPE (decl)));
9386 : : }
9387 : : return false;
9388 : : }
9389 : :
9390 : 6838535 : tree ctx = CP_DECL_CONTEXT (decl);
9391 : 6838535 : depset *dep = NULL;
9392 : 6838535 : if (streaming_p ())
9393 : 1145476 : dep = dep_hash->find_dependency (decl);
9394 : 5693059 : else if (TREE_CODE (ctx) != FUNCTION_DECL
9395 : 210466 : || TREE_CODE (decl) == TEMPLATE_DECL
9396 : 190302 : || DECL_IMPLICIT_TYPEDEF_P (decl)
9397 : 5860418 : || (DECL_LANG_SPECIFIC (decl)
9398 : 120294 : && DECL_MODULE_IMPORT_P (decl)))
9399 : : {
9400 : 5525700 : auto kind = (TREE_CODE (decl) == NAMESPACE_DECL
9401 : 478348 : && !DECL_NAMESPACE_ALIAS (decl)
9402 : 5525700 : ? depset::EK_NAMESPACE : depset::EK_DECL);
9403 : 5525700 : dep = dep_hash->add_dependency (decl, kind);
9404 : : }
9405 : :
9406 : 6671176 : if (!dep || dep->is_tu_local ())
9407 : : {
9408 : : /* Some internal entity of context. Do by value. */
9409 : 324695 : decl_value (decl, dep);
9410 : 324695 : return false;
9411 : : }
9412 : :
9413 : 6513840 : if (dep->get_entity_kind () == depset::EK_REDIRECT)
9414 : : {
9415 : : /* The DECL_TEMPLATE_RESULT of a partial specialization.
9416 : : Write the partial specialization's template. */
9417 : 81359 : depset *redirect = dep->deps[0];
9418 : 81359 : gcc_checking_assert (redirect->get_entity_kind () == depset::EK_PARTIAL);
9419 : 81359 : tpl = redirect->get_entity ();
9420 : 81359 : goto partial_template;
9421 : : }
9422 : :
9423 : 6432481 : if (streaming_p ())
9424 : : {
9425 : : /* Locate the entity. */
9426 : 988409 : unsigned index = dep->cluster;
9427 : 988409 : unsigned import = 0;
9428 : :
9429 : 988409 : if (dep->is_import ())
9430 : 759 : import = dep->section;
9431 : 987650 : else if (CHECKING_P)
9432 : : /* It should be what we put there. */
9433 : 987650 : gcc_checking_assert (index == ~import_entity_index (decl));
9434 : :
9435 : : #if CHECKING_P
9436 : 759 : gcc_assert (!import || importedness >= 0);
9437 : : #endif
9438 : 988409 : i (tt_entity);
9439 : 988409 : u (import);
9440 : 988409 : u (index);
9441 : : }
9442 : :
9443 : 6432481 : int tag = insert (decl);
9444 : 6432481 : if (streaming_p () && dump (dumper::TREE))
9445 : : {
9446 : 529 : char const *kind = "import";
9447 : 529 : module_state *from = this_module ();
9448 : 529 : if (dep->is_import ())
9449 : : /* Rediscover the unremapped index. */
9450 : 78 : from = import_entity_module (import_entity_index (decl));
9451 : : else
9452 : : {
9453 : 451 : tree o = get_originating_module_decl (decl);
9454 : 451 : o = STRIP_TEMPLATE (o);
9455 : 902 : kind = (DECL_LANG_SPECIFIC (o) && DECL_MODULE_PURVIEW_P (o)
9456 : 451 : ? "purview" : "GMF");
9457 : : }
9458 : 529 : dump ("Wrote %s:%d %C:%N@%M", kind,
9459 : 529 : tag, TREE_CODE (decl), decl, from);
9460 : : }
9461 : :
9462 : 6432481 : add_indirects (decl);
9463 : :
9464 : 6432481 : return false;
9465 : : }
9466 : :
9467 : : void
9468 : 7819061 : trees_out::type_node (tree type)
9469 : : {
9470 : 7819061 : gcc_assert (TYPE_P (type));
9471 : :
9472 : 7819061 : tree root = (TYPE_NAME (type)
9473 : 7819061 : ? TREE_TYPE (TYPE_NAME (type)) : TYPE_MAIN_VARIANT (type));
9474 : 7819061 : gcc_checking_assert (root);
9475 : :
9476 : 7819061 : if (type != root)
9477 : : {
9478 : 1772511 : if (streaming_p ())
9479 : 370586 : i (tt_variant_type);
9480 : 1772511 : tree_node (root);
9481 : :
9482 : 1772511 : int flags = -1;
9483 : :
9484 : 1772511 : if (TREE_CODE (type) == FUNCTION_TYPE
9485 : 1772511 : || TREE_CODE (type) == METHOD_TYPE)
9486 : : {
9487 : 423430 : int quals = type_memfn_quals (type);
9488 : 423430 : int rquals = type_memfn_rqual (type);
9489 : 423430 : tree raises = TYPE_RAISES_EXCEPTIONS (type);
9490 : 423430 : bool late = TYPE_HAS_LATE_RETURN_TYPE (type);
9491 : :
9492 : 423430 : if (raises != TYPE_RAISES_EXCEPTIONS (root)
9493 : 11033 : || rquals != type_memfn_rqual (root)
9494 : 7995 : || quals != type_memfn_quals (root)
9495 : 431407 : || late != TYPE_HAS_LATE_RETURN_TYPE (root))
9496 : 423430 : flags = rquals | (int (late) << 2) | (quals << 3);
9497 : : }
9498 : : else
9499 : : {
9500 : 1349081 : if (TYPE_USER_ALIGN (type))
9501 : 11798 : flags = TYPE_ALIGN_RAW (type);
9502 : : }
9503 : :
9504 : 1772511 : if (streaming_p ())
9505 : 370586 : i (flags);
9506 : :
9507 : 1772511 : if (flags < 0)
9508 : : ;
9509 : 435228 : else if (TREE_CODE (type) == FUNCTION_TYPE
9510 : 435228 : || TREE_CODE (type) == METHOD_TYPE)
9511 : : {
9512 : 423430 : tree raises = TYPE_RAISES_EXCEPTIONS (type);
9513 : 423430 : if (raises == TYPE_RAISES_EXCEPTIONS (root))
9514 : 11033 : raises = error_mark_node;
9515 : 423430 : tree_node (raises);
9516 : : }
9517 : :
9518 : : /* build_type_attribute_variant creates a new TYPE_MAIN_VARIANT, so
9519 : : variants should all have the same set of attributes. */
9520 : 1772511 : gcc_checking_assert (TYPE_ATTRIBUTES (type)
9521 : : == TYPE_ATTRIBUTES (TYPE_MAIN_VARIANT (type)));
9522 : :
9523 : 1772511 : if (streaming_p ())
9524 : : {
9525 : : /* Qualifiers. */
9526 : 370586 : int rquals = cp_type_quals (root);
9527 : 370586 : int quals = cp_type_quals (type);
9528 : 370586 : if (quals == rquals)
9529 : 171790 : quals = -1;
9530 : 370586 : i (quals);
9531 : : }
9532 : :
9533 : 1772511 : if (ref_node (type) != WK_none)
9534 : : {
9535 : 1772511 : int tag = insert (type);
9536 : 1772511 : if (streaming_p ())
9537 : : {
9538 : 370586 : i (0);
9539 : 370586 : dump (dumper::TREE)
9540 : 203 : && dump ("Wrote:%d variant type %C", tag, TREE_CODE (type));
9541 : : }
9542 : : }
9543 : 1772511 : return;
9544 : : }
9545 : :
9546 : 6046550 : if (tree name = TYPE_NAME (type))
9547 : 2377702 : if ((TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name))
9548 : 1857960 : || DECL_TEMPLATE_PARM_P (name)
9549 : 1248291 : || TREE_CODE (type) == RECORD_TYPE
9550 : 226092 : || TREE_CODE (type) == UNION_TYPE
9551 : 2598392 : || TREE_CODE (type) == ENUMERAL_TYPE)
9552 : : {
9553 : 2214659 : gcc_checking_assert (DECL_P (name));
9554 : :
9555 : : /* We can meet template parms that we didn't meet in the
9556 : : tpl_parms walk, because we're referring to a derived type
9557 : : that was previously constructed from equivalent template
9558 : : parms. */
9559 : 2214659 : if (streaming_p ())
9560 : : {
9561 : 155566 : i (tt_typedef_type);
9562 : 155566 : dump (dumper::TREE)
9563 : 59 : && dump ("Writing %stypedef %C:%N",
9564 : 59 : DECL_IMPLICIT_TYPEDEF_P (name) ? "implicit " : "",
9565 : 59 : TREE_CODE (name), name);
9566 : : }
9567 : 2214659 : tree_node (name);
9568 : 2214659 : if (streaming_p ())
9569 : 155566 : dump (dumper::TREE) && dump ("Wrote typedef %C:%N%S",
9570 : 59 : TREE_CODE (name), name, name);
9571 : :
9572 : : /* We'll have either visited this type or have newly discovered
9573 : : that it's TU-local; either way we won't need to visit it again. */
9574 : 2214659 : gcc_checking_assert (TREE_VISITED (type) || has_tu_local_dep (name));
9575 : 2214659 : return;
9576 : : }
9577 : :
9578 : 3831891 : if (TYPE_PTRMEMFUNC_P (type))
9579 : : {
9580 : : /* This is a distinct type node, masquerading as a structure. */
9581 : 3823 : tree fn_type = TYPE_PTRMEMFUNC_FN_TYPE (type);
9582 : 3823 : if (streaming_p ())
9583 : 1073 : i (tt_ptrmem_type);
9584 : 3823 : tree_node (fn_type);
9585 : 3823 : int tag = insert (type);
9586 : 3823 : if (streaming_p ())
9587 : 1076 : dump (dumper::TREE) && dump ("Written:%d ptrmem type", tag);
9588 : 3823 : return;
9589 : : }
9590 : :
9591 : 3828068 : if (streaming_p ())
9592 : : {
9593 : 1220244 : u (tt_derived_type);
9594 : 1220244 : u (TREE_CODE (type));
9595 : : }
9596 : :
9597 : 3828068 : tree_node (TREE_TYPE (type));
9598 : 3828068 : switch (TREE_CODE (type))
9599 : : {
9600 : 0 : default:
9601 : : /* We should never meet a type here that is indescribable in
9602 : : terms of other types. */
9603 : 0 : gcc_unreachable ();
9604 : :
9605 : 63781 : case ARRAY_TYPE:
9606 : 63781 : tree_node (TYPE_DOMAIN (type));
9607 : 63781 : if (streaming_p ())
9608 : : /* Dependent arrays are constructed with TYPE_DEPENENT_P
9609 : : already set. */
9610 : 20454 : u (TYPE_DEPENDENT_P (type));
9611 : : break;
9612 : :
9613 : : case COMPLEX_TYPE:
9614 : : /* No additional data. */
9615 : : break;
9616 : :
9617 : 12 : case BOOLEAN_TYPE:
9618 : : /* A non-standard boolean type. */
9619 : 12 : if (streaming_p ())
9620 : 6 : u (TYPE_PRECISION (type));
9621 : : break;
9622 : :
9623 : 58569 : case INTEGER_TYPE:
9624 : 58569 : if (TREE_TYPE (type))
9625 : : {
9626 : : /* A range type (representing an array domain). */
9627 : 55720 : tree_node (TYPE_MIN_VALUE (type));
9628 : 55720 : tree_node (TYPE_MAX_VALUE (type));
9629 : : }
9630 : : else
9631 : : {
9632 : : /* A new integral type (representing a bitfield). */
9633 : 2849 : if (streaming_p ())
9634 : : {
9635 : 815 : unsigned prec = TYPE_PRECISION (type);
9636 : 815 : bool unsigned_p = TYPE_UNSIGNED (type);
9637 : :
9638 : 815 : u ((prec << 1) | unsigned_p);
9639 : : }
9640 : : }
9641 : : break;
9642 : :
9643 : 840475 : case METHOD_TYPE:
9644 : 840475 : case FUNCTION_TYPE:
9645 : 840475 : {
9646 : 840475 : gcc_checking_assert (type_memfn_rqual (type) == REF_QUAL_NONE);
9647 : :
9648 : 840475 : tree arg_types = TYPE_ARG_TYPES (type);
9649 : 840475 : if (TREE_CODE (type) == METHOD_TYPE)
9650 : : {
9651 : 513663 : tree_node (TREE_TYPE (TREE_VALUE (arg_types)));
9652 : 513663 : arg_types = TREE_CHAIN (arg_types);
9653 : : }
9654 : 840475 : tree_node (arg_types);
9655 : : }
9656 : 840475 : break;
9657 : :
9658 : 1222 : case OFFSET_TYPE:
9659 : 1222 : tree_node (TYPE_OFFSET_BASETYPE (type));
9660 : 1222 : break;
9661 : :
9662 : : case POINTER_TYPE:
9663 : : /* No additional data. */
9664 : : break;
9665 : :
9666 : 645849 : case REFERENCE_TYPE:
9667 : 645849 : if (streaming_p ())
9668 : 142275 : u (TYPE_REF_IS_RVALUE (type));
9669 : : break;
9670 : :
9671 : 755800 : case DECLTYPE_TYPE:
9672 : 755800 : case TYPEOF_TYPE:
9673 : 755800 : case DEPENDENT_OPERATOR_TYPE:
9674 : 755800 : tree_node (TYPE_VALUES_RAW (type));
9675 : 755800 : if (TREE_CODE (type) == DECLTYPE_TYPE)
9676 : : /* We stash a whole bunch of things into decltype's
9677 : : flags. */
9678 : 57028 : if (streaming_p ())
9679 : 19424 : tree_node_bools (type);
9680 : : break;
9681 : :
9682 : 7803 : case TRAIT_TYPE:
9683 : 7803 : tree_node (TRAIT_TYPE_KIND_RAW (type));
9684 : 7803 : tree_node (TRAIT_TYPE_TYPE1 (type));
9685 : 7803 : tree_node (TRAIT_TYPE_TYPE2 (type));
9686 : 7803 : break;
9687 : :
9688 : : case TYPE_ARGUMENT_PACK:
9689 : : /* No additional data. */
9690 : : break;
9691 : :
9692 : 130341 : case TYPE_PACK_EXPANSION:
9693 : 130341 : if (streaming_p ())
9694 : 52778 : u (PACK_EXPANSION_LOCAL_P (type));
9695 : 260682 : tree_node (PACK_EXPANSION_PARAMETER_PACKS (type));
9696 : 130341 : tree_node (PACK_EXPANSION_EXTRA_ARGS (type));
9697 : 130341 : break;
9698 : :
9699 : 22 : case PACK_INDEX_TYPE:
9700 : 22 : tree_node (PACK_INDEX_PACK (type));
9701 : 22 : tree_node (PACK_INDEX_INDEX (type));
9702 : 22 : break;
9703 : :
9704 : 165057 : case TYPENAME_TYPE:
9705 : 165057 : {
9706 : 165057 : tree_node (TYPE_CONTEXT (type));
9707 : 165057 : tree_node (DECL_NAME (TYPE_NAME (type)));
9708 : 165057 : tree_node (TYPENAME_TYPE_FULLNAME (type));
9709 : 165057 : if (streaming_p ())
9710 : : {
9711 : 58306 : enum tag_types tag_type = none_type;
9712 : 58306 : if (TYPENAME_IS_ENUM_P (type))
9713 : : tag_type = enum_type;
9714 : 58306 : else if (TYPENAME_IS_CLASS_P (type))
9715 : : tag_type = class_type;
9716 : 56532 : else if (TYPENAME_IS_UNION_P (type))
9717 : 0 : tag_type = union_type;
9718 : 58306 : u (int (tag_type));
9719 : : }
9720 : : }
9721 : : break;
9722 : :
9723 : 246 : case UNBOUND_CLASS_TEMPLATE:
9724 : 246 : {
9725 : 246 : tree decl = TYPE_NAME (type);
9726 : 246 : tree_node (DECL_CONTEXT (decl));
9727 : 246 : tree_node (DECL_NAME (decl));
9728 : 246 : tree_node (DECL_TEMPLATE_PARMS (decl));
9729 : : }
9730 : 246 : break;
9731 : :
9732 : 42 : case VECTOR_TYPE:
9733 : 42 : if (streaming_p ())
9734 : : {
9735 : 21 : poly_uint64 nunits = TYPE_VECTOR_SUBPARTS (type);
9736 : 42 : for (unsigned ix = 0; ix != NUM_POLY_INT_COEFFS; ix++)
9737 : 21 : wu (nunits.coeffs[ix]);
9738 : : }
9739 : : break;
9740 : : }
9741 : :
9742 : 3828068 : tree_node (TYPE_ATTRIBUTES (type));
9743 : :
9744 : : /* We may have met the type during emitting the above. */
9745 : 3828068 : if (ref_node (type) != WK_none)
9746 : : {
9747 : 3478682 : int tag = insert (type);
9748 : 3478682 : if (streaming_p ())
9749 : : {
9750 : 1056912 : i (0);
9751 : 1056912 : dump (dumper::TREE)
9752 : 558 : && dump ("Wrote:%d derived type %C", tag, TREE_CODE (type));
9753 : : }
9754 : : }
9755 : :
9756 : : return;
9757 : : }
9758 : :
9759 : : /* T is (mostly*) a non-mergeable node that must be written by value.
9760 : : The mergeable case is a BINFO, which are as-if DECLSs. */
9761 : :
9762 : : void
9763 : 23524820 : trees_out::tree_value (tree t)
9764 : : {
9765 : : /* We should never be writing a type by value. tree_type should
9766 : : have streamed it, or we're going via its TYPE_DECL. */
9767 : 23524820 : gcc_checking_assert (!TYPE_P (t));
9768 : :
9769 : 23524820 : if (DECL_P (t))
9770 : : /* No template, type, var or function, except anonymous
9771 : : non-context vars and types. */
9772 : 629955 : gcc_checking_assert ((TREE_CODE (t) != TEMPLATE_DECL
9773 : : && (TREE_CODE (t) != TYPE_DECL
9774 : : || (DECL_ARTIFICIAL (t) && !DECL_CONTEXT (t)))
9775 : : && (TREE_CODE (t) != VAR_DECL
9776 : : || ((!DECL_NAME (t)
9777 : : || IDENTIFIER_INTERNAL_P (DECL_NAME (t)))
9778 : : && !DECL_CONTEXT (t)))
9779 : : && TREE_CODE (t) != FUNCTION_DECL));
9780 : :
9781 : 23524820 : if (streaming_p ())
9782 : : {
9783 : : /* A new node -> tt_node. */
9784 : 9375561 : tree_val_count++;
9785 : 9375561 : i (tt_node);
9786 : 9375561 : start (t);
9787 : 9375561 : tree_node_bools (t);
9788 : : }
9789 : :
9790 : 23524820 : if (TREE_CODE (t) == TREE_BINFO)
9791 : : /* Binfos are decl-like and need merging information. */
9792 : 194824 : binfo_mergeable (t);
9793 : :
9794 : 23524820 : int tag = insert (t, WK_value);
9795 : 23524820 : if (streaming_p ())
9796 : 9375561 : dump (dumper::TREE)
9797 : 2823 : && dump ("Writing tree:%d %C:%N", tag, TREE_CODE (t), t);
9798 : :
9799 : 23524820 : int type_tag = 0;
9800 : 23524820 : tree type = NULL_TREE;
9801 : 23524820 : if (TREE_CODE (t) == TYPE_DECL)
9802 : : {
9803 : 28 : type = TREE_TYPE (t);
9804 : :
9805 : : /* We only support a limited set of features for uncontexted types;
9806 : : these are typically types created in the language-independent
9807 : : parts of the frontend (such as ubsan). */
9808 : 28 : gcc_checking_assert (RECORD_OR_UNION_TYPE_P (type)
9809 : : && TYPE_MAIN_VARIANT (type) == type
9810 : : && TYPE_NAME (type) == t
9811 : : && TYPE_STUB_DECL (type) == t
9812 : : && !TYPE_VFIELD (type)
9813 : : && !TYPE_BINFO (type)
9814 : : && !CLASS_TYPE_P (type));
9815 : :
9816 : 28 : if (streaming_p ())
9817 : : {
9818 : 14 : start (type);
9819 : 14 : tree_node_bools (type);
9820 : : }
9821 : :
9822 : 28 : type_tag = insert (type, WK_value);
9823 : 28 : if (streaming_p ())
9824 : 14 : dump (dumper::TREE)
9825 : 0 : && dump ("Writing type: %d %C:%N", type_tag,
9826 : 0 : TREE_CODE (type), type);
9827 : : }
9828 : :
9829 : 23524820 : tree_node_vals (t);
9830 : :
9831 : 23524820 : if (type)
9832 : : {
9833 : 28 : tree_node_vals (type);
9834 : 28 : tree_node (TYPE_SIZE (type));
9835 : 28 : tree_node (TYPE_SIZE_UNIT (type));
9836 : 28 : chained_decls (TYPE_FIELDS (type));
9837 : 28 : if (streaming_p ())
9838 : 14 : dump (dumper::TREE)
9839 : 0 : && dump ("Written type:%d %C:%N", type_tag, TREE_CODE (type), type);
9840 : : }
9841 : :
9842 : : /* For uncontexted VAR_DECLs we need to stream the definition so that
9843 : : importers can recreate their value. */
9844 : 23524820 : if (TREE_CODE (t) == VAR_DECL)
9845 : : {
9846 : 588 : gcc_checking_assert (!DECL_NONTRIVIALLY_INITIALIZED_P (t));
9847 : 588 : tree_node (DECL_INITIAL (t));
9848 : : }
9849 : :
9850 : 23524820 : if (streaming_p ())
9851 : 9378384 : dump (dumper::TREE) && dump ("Written tree:%d %C:%N", tag, TREE_CODE (t), t);
9852 : 23524820 : }
9853 : :
9854 : : tree
9855 : 8054876 : trees_in::tree_value ()
9856 : : {
9857 : 8054876 : tree t = start ();
9858 : 8054876 : if (!t || !tree_node_bools (t))
9859 : 0 : return NULL_TREE;
9860 : :
9861 : 8054876 : tree existing = t;
9862 : 8054876 : if (TREE_CODE (t) == TREE_BINFO)
9863 : : {
9864 : 71369 : tree type;
9865 : 71369 : unsigned ix = binfo_mergeable (&type);
9866 : 71369 : if (TYPE_BINFO (type))
9867 : : {
9868 : : /* We already have a definition, this must be a duplicate. */
9869 : 40263 : dump (dumper::MERGE)
9870 : 232 : && dump ("Deduping binfo %N[%u]", type, ix);
9871 : 40263 : existing = TYPE_BINFO (type);
9872 : 55200 : while (existing && ix--)
9873 : 14937 : existing = TREE_CHAIN (existing);
9874 : 40263 : if (existing)
9875 : 40263 : register_duplicate (t, existing);
9876 : : else
9877 : : /* Error, mismatch -- diagnose in read_class_def's
9878 : : checking. */
9879 : : existing = t;
9880 : : }
9881 : : }
9882 : :
9883 : : /* Insert into map. */
9884 : 8054876 : int tag = insert (existing);
9885 : 8054876 : dump (dumper::TREE)
9886 : 3677 : && dump ("Reading tree:%d %C", tag, TREE_CODE (t));
9887 : :
9888 : 8054876 : int type_tag = 0;
9889 : 8054876 : tree type = NULL_TREE;
9890 : 8054876 : if (TREE_CODE (t) == TYPE_DECL)
9891 : : {
9892 : 14 : type = start ();
9893 : 14 : if (!type || !tree_node_bools (type))
9894 : : t = NULL_TREE;
9895 : :
9896 : 14 : type_tag = insert (type);
9897 : 14 : if (t)
9898 : 14 : dump (dumper::TREE)
9899 : 0 : && dump ("Reading type:%d %C", type_tag, TREE_CODE (type));
9900 : : }
9901 : :
9902 : : if (!t)
9903 : : {
9904 : 0 : bail:
9905 : 0 : back_refs[~tag] = NULL_TREE;
9906 : 0 : if (type_tag)
9907 : 0 : back_refs[~type_tag] = NULL_TREE;
9908 : 0 : set_overrun ();
9909 : 0 : return NULL_TREE;
9910 : : }
9911 : :
9912 : 8054876 : if (!tree_node_vals (t))
9913 : 0 : goto bail;
9914 : :
9915 : 8054876 : if (type)
9916 : : {
9917 : 14 : if (!tree_node_vals (type))
9918 : 0 : goto bail;
9919 : :
9920 : 14 : TYPE_SIZE (type) = tree_node ();
9921 : 14 : TYPE_SIZE_UNIT (type) = tree_node ();
9922 : 14 : TYPE_FIELDS (type) = chained_decls ();
9923 : 14 : if (get_overrun ())
9924 : 0 : goto bail;
9925 : :
9926 : 14 : dump (dumper::TREE)
9927 : 0 : && dump ("Read type:%d %C:%N", type_tag, TREE_CODE (type), type);
9928 : : }
9929 : :
9930 : 8054876 : if (TREE_CODE (t) == VAR_DECL)
9931 : : {
9932 : 290 : DECL_INITIAL (t) = tree_node ();
9933 : 290 : if (TREE_STATIC (t))
9934 : 8 : varpool_node::finalize_decl (t);
9935 : : }
9936 : :
9937 : 8054876 : if (TREE_CODE (t) == LAMBDA_EXPR
9938 : 8054876 : && CLASSTYPE_LAMBDA_EXPR (TREE_TYPE (t)))
9939 : : {
9940 : 1917 : existing = CLASSTYPE_LAMBDA_EXPR (TREE_TYPE (t));
9941 : 1917 : back_refs[~tag] = existing;
9942 : : }
9943 : :
9944 : 8058553 : dump (dumper::TREE) && dump ("Read tree:%d %C:%N", tag, TREE_CODE (t), t);
9945 : :
9946 : 8054876 : if (TREE_CODE (existing) == INTEGER_CST && !TREE_OVERFLOW (existing))
9947 : : {
9948 : 470374 : existing = cache_integer_cst (t, true);
9949 : 470374 : back_refs[~tag] = existing;
9950 : : }
9951 : :
9952 : : return existing;
9953 : : }
9954 : :
9955 : : /* Whether DECL has a TU-local dependency in the hash. */
9956 : :
9957 : : bool
9958 : 721393 : trees_out::has_tu_local_dep (tree decl) const
9959 : : {
9960 : : /* Only the contexts of fields or enums remember that they're
9961 : : TU-local. */
9962 : 721393 : if (DECL_CONTEXT (decl)
9963 : 721393 : && (TREE_CODE (decl) == FIELD_DECL
9964 : 721390 : || TREE_CODE (decl) == CONST_DECL))
9965 : 3 : decl = TYPE_NAME (DECL_CONTEXT (decl));
9966 : :
9967 : 721393 : depset *dep = dep_hash->find_dependency (decl);
9968 : 721393 : if (!dep)
9969 : : {
9970 : : /* This might be the DECL_TEMPLATE_RESULT of a TEMPLATE_DECL
9971 : : which we found was TU-local and gave up early. */
9972 : 8625 : int use_tpl = -1;
9973 : 8625 : if (tree ti = node_template_info (decl, use_tpl))
9974 : 1771 : dep = dep_hash->find_dependency (TI_TEMPLATE (ti));
9975 : : }
9976 : :
9977 : 721393 : return dep && dep->is_tu_local ();
9978 : : }
9979 : :
9980 : : /* If T depends on a TU-local entity, return that decl. */
9981 : :
9982 : : tree
9983 : 371 : trees_out::find_tu_local_decl (tree t)
9984 : : {
9985 : : /* We need to have walked all deps first before we can check. */
9986 : 371 : gcc_checking_assert (!is_initial_scan ());
9987 : :
9988 : 903 : auto walker = [](tree *tp, int *walk_subtrees, void *data) -> tree
9989 : : {
9990 : 532 : auto self = (trees_out *)data;
9991 : :
9992 : 532 : tree decl = NULL_TREE;
9993 : 532 : if (TYPE_P (*tp))
9994 : : {
9995 : : /* A PMF type is a record type, which we otherwise wouldn't walk;
9996 : : return whether the function type is TU-local. */
9997 : 358 : if (TYPE_PTRMEMFUNC_P (*tp))
9998 : : {
9999 : 3 : *walk_subtrees = 0;
10000 : 3 : return self->find_tu_local_decl (TYPE_PTRMEMFUNC_FN_TYPE (*tp));
10001 : : }
10002 : : else
10003 : 355 : decl = TYPE_MAIN_DECL (*tp);
10004 : : }
10005 : 174 : else if (DECL_P (*tp))
10006 : : decl = *tp;
10007 : :
10008 : 361 : if (decl)
10009 : : {
10010 : : /* We found a DECL, this will tell us whether we're TU-local. */
10011 : 59 : *walk_subtrees = 0;
10012 : 59 : return self->has_tu_local_dep (decl) ? decl : NULL_TREE;
10013 : : }
10014 : : return NULL_TREE;
10015 : : };
10016 : :
10017 : : /* We need to walk without duplicates so that we step into the pointed-to
10018 : : types of array types. */
10019 : 371 : return cp_walk_tree_without_duplicates (&t, walker, this);
10020 : : }
10021 : :
10022 : : /* Get the name for TU-local decl T to be used in diagnostics. */
10023 : :
10024 : : static tree
10025 : 197 : name_for_tu_local_decl (tree t)
10026 : : {
10027 : 197 : int flags = (TFF_SCOPE | TFF_DECL_SPECIFIERS);
10028 : 197 : const char *str = decl_as_string (t, flags);
10029 : 197 : return get_identifier (str);
10030 : : }
10031 : :
10032 : : /* Stream out tree node T. We automatically create local back
10033 : : references, which is essentially a single pass lisp
10034 : : self-referential structure pretty-printer. */
10035 : :
10036 : : void
10037 : 207047161 : trees_out::tree_node (tree t)
10038 : : {
10039 : 207047161 : dump.indent ();
10040 : 207047161 : walk_kind ref = ref_node (t);
10041 : 207047161 : if (ref == WK_none)
10042 : 159275936 : goto done;
10043 : :
10044 : : /* Find TU-local entities and intercept streaming to instead write a
10045 : : placeholder value; this way we don't need to emit such decls.
10046 : : We only need to do this when writing a definition of an entity
10047 : : that we know names a TU-local entity. */
10048 : 56019718 : if (!is_initial_scan () && writing_local_entities)
10049 : : {
10050 : 904 : tree local_decl = NULL_TREE;
10051 : 904 : if (DECL_P (t) && has_tu_local_dep (t))
10052 : : local_decl = t;
10053 : : /* Consider a type to be TU-local if it refers to any TU-local decl,
10054 : : no matter how deep.
10055 : :
10056 : : This worsens diagnostics slightly, as we often no longer point
10057 : : directly to the at-fault entity when instantiating. However, this
10058 : : reduces the module size slightly and means that much less of pt.cc
10059 : : needs to know about us. */
10060 : 806 : else if (TYPE_P (t))
10061 : 142 : local_decl = find_tu_local_decl (t);
10062 : 664 : else if (EXPR_P (t))
10063 : 226 : local_decl = find_tu_local_decl (TREE_TYPE (t));
10064 : :
10065 : 466 : if (local_decl)
10066 : : {
10067 : 152 : int tag = insert (t, WK_value);
10068 : 152 : if (streaming_p ())
10069 : : {
10070 : 152 : tu_local_count++;
10071 : 152 : i (tt_tu_local);
10072 : 152 : dump (dumper::TREE)
10073 : 0 : && dump ("Writing TU-local entity:%d %C:%N",
10074 : 0 : tag, TREE_CODE (t), t);
10075 : : }
10076 : 152 : tree_node (name_for_tu_local_decl (local_decl));
10077 : 152 : if (state)
10078 : 152 : state->write_location (*this, DECL_SOURCE_LOCATION (local_decl));
10079 : 152 : goto done;
10080 : : }
10081 : : }
10082 : :
10083 : 47771073 : if (ref != WK_normal)
10084 : 1233080 : goto skip_normal;
10085 : :
10086 : 46537993 : if (TREE_CODE (t) == IDENTIFIER_NODE)
10087 : : {
10088 : : /* An identifier node -> tt_id, tt_conv_id, tt_anon_id, tt_lambda_id,
10089 : : tt_internal_id. */
10090 : 5816492 : int code = tt_id;
10091 : 5816492 : if (IDENTIFIER_ANON_P (t))
10092 : 21760 : code = IDENTIFIER_LAMBDA_P (t) ? tt_lambda_id : tt_anon_id;
10093 : 5794732 : else if (IDENTIFIER_INTERNAL_P (t))
10094 : : code = tt_internal_id;
10095 : 5794718 : else if (IDENTIFIER_CONV_OP_P (t))
10096 : 8394 : code = tt_conv_id;
10097 : :
10098 : 5816492 : if (streaming_p ())
10099 : 1188951 : i (code);
10100 : :
10101 : 5816492 : if (code == tt_conv_id)
10102 : : {
10103 : 8394 : tree type = TREE_TYPE (t);
10104 : 8394 : gcc_checking_assert (type || t == conv_op_identifier);
10105 : 8394 : tree_node (type);
10106 : : }
10107 : 5808098 : else if (code == tt_id && streaming_p ())
10108 : 1179430 : str (IDENTIFIER_POINTER (t), IDENTIFIER_LENGTH (t));
10109 : 4628668 : else if (code == tt_internal_id && streaming_p ())
10110 : 7 : str (prefix_for_internal_label (t));
10111 : :
10112 : 5816492 : int tag = insert (t);
10113 : 5816492 : if (streaming_p ())
10114 : : {
10115 : : /* We know the ordering of the 5 id tags. */
10116 : 1188951 : static const char *const kinds[] =
10117 : : {"", "conv_op ", "anon ", "lambda ", "internal "};
10118 : 1188951 : dump (dumper::TREE)
10119 : 1074 : && dump ("Written:%d %sidentifier:%N", tag,
10120 : 1071 : kinds[code - tt_id],
10121 : 3 : code == tt_conv_id ? TREE_TYPE (t) : t);
10122 : : }
10123 : 5816492 : goto done;
10124 : : }
10125 : :
10126 : 40721501 : if (TREE_CODE (t) == TREE_BINFO)
10127 : : {
10128 : : /* A BINFO -> tt_binfo.
10129 : : We must do this by reference. We stream the binfo tree
10130 : : itself when streaming its owning RECORD_TYPE. That we got
10131 : : here means the dominating type is not in this SCC. */
10132 : 46892 : if (streaming_p ())
10133 : 1191 : i (tt_binfo);
10134 : 46892 : binfo_mergeable (t);
10135 : 46892 : gcc_checking_assert (!TREE_VISITED (t));
10136 : 46892 : int tag = insert (t);
10137 : 46892 : if (streaming_p ())
10138 : 1191 : dump (dumper::TREE) && dump ("Inserting binfo:%d %N", tag, t);
10139 : 46892 : goto done;
10140 : : }
10141 : :
10142 : 40674609 : if (TREE_CODE (t) == INTEGER_CST
10143 : 3046796 : && !TREE_OVERFLOW (t)
10144 : 43721405 : && TREE_CODE (TREE_TYPE (t)) == ENUMERAL_TYPE)
10145 : : {
10146 : : /* An integral constant of enumeral type. See if it matches one
10147 : : of the enumeration values. */
10148 : 25703 : for (tree values = TYPE_VALUES (TREE_TYPE (t));
10149 : 698967 : values; values = TREE_CHAIN (values))
10150 : : {
10151 : 697173 : tree decl = TREE_VALUE (values);
10152 : 697173 : if (tree_int_cst_equal (DECL_INITIAL (decl), t))
10153 : : {
10154 : 23909 : if (streaming_p ())
10155 : 6858 : u (tt_enum_value);
10156 : 23909 : tree_node (decl);
10157 : 23951 : dump (dumper::TREE) && dump ("Written enum value %N", decl);
10158 : 23909 : goto done;
10159 : : }
10160 : : }
10161 : : /* It didn't match. We'll write it a an explicit INTEGER_CST
10162 : : node. */
10163 : : }
10164 : :
10165 : 40650700 : if (TYPE_P (t))
10166 : : {
10167 : 7819061 : type_node (t);
10168 : 7819061 : goto done;
10169 : : }
10170 : :
10171 : 32831639 : if (DECL_P (t))
10172 : : {
10173 : 10157914 : if (DECL_TEMPLATE_PARM_P (t))
10174 : : {
10175 : 1624465 : tpl_parm_value (t);
10176 : 1624465 : goto done;
10177 : : }
10178 : :
10179 : 8533449 : if (!DECL_CONTEXT (t))
10180 : : {
10181 : : /* There are a few cases of decls with no context. We'll write
10182 : : these by value, but first assert they are cases we expect. */
10183 : 20338 : gcc_checking_assert (ref == WK_normal);
10184 : 20338 : switch (TREE_CODE (t))
10185 : : {
10186 : 0 : default: gcc_unreachable ();
10187 : :
10188 : 6498 : case LABEL_DECL:
10189 : : /* CASE_LABEL_EXPRs contain uncontexted LABEL_DECLs. */
10190 : 6498 : gcc_checking_assert (!DECL_NAME (t));
10191 : : break;
10192 : :
10193 : 588 : case VAR_DECL:
10194 : : /* AGGR_INIT_EXPRs cons up anonymous uncontexted VAR_DECLs,
10195 : : and internal vars are created by sanitizers and
10196 : : __builtin_source_location. */
10197 : 588 : gcc_checking_assert ((!DECL_NAME (t)
10198 : : || IDENTIFIER_INTERNAL_P (DECL_NAME (t)))
10199 : : && DECL_ARTIFICIAL (t));
10200 : : break;
10201 : :
10202 : : case PARM_DECL:
10203 : : /* REQUIRES_EXPRs have a tree list of uncontexted
10204 : : PARM_DECLS. It'd be nice if they had a
10205 : : distinguishing flag to double check. */
10206 : : break;
10207 : :
10208 : 28 : case TYPE_DECL:
10209 : : /* Some parts of the compiler need internal struct types;
10210 : : these types may not have an appropriate context to use.
10211 : : Walk the whole type (including its definition) by value. */
10212 : 28 : gcc_checking_assert (DECL_ARTIFICIAL (t)
10213 : : && TYPE_ARTIFICIAL (TREE_TYPE (t))
10214 : : && RECORD_OR_UNION_TYPE_P (TREE_TYPE (t))
10215 : : && !CLASS_TYPE_P (TREE_TYPE (t)));
10216 : : break;
10217 : : }
10218 : 20338 : mark_declaration (t, has_definition (t));
10219 : 20338 : goto by_value;
10220 : : }
10221 : : }
10222 : :
10223 : 22673725 : skip_normal:
10224 : 32419916 : if (DECL_P (t) && !decl_node (t, ref))
10225 : 8915434 : goto done;
10226 : :
10227 : : /* Otherwise by value */
10228 : 23524820 : by_value:
10229 : 23524820 : tree_value (t);
10230 : :
10231 : 207047161 : done:
10232 : : /* And, breath out. */
10233 : 207047161 : dump.outdent ();
10234 : 207047161 : }
10235 : :
10236 : : /* Stream in a tree node. */
10237 : :
10238 : : tree
10239 : 67421391 : trees_in::tree_node (bool is_use)
10240 : : {
10241 : 67421391 : if (get_overrun ())
10242 : : return NULL_TREE;
10243 : :
10244 : 67421391 : dump.indent ();
10245 : 67421391 : int tag = i ();
10246 : 67421391 : tree res = NULL_TREE;
10247 : 67421391 : switch (tag)
10248 : : {
10249 : 22122071 : default:
10250 : : /* backref, pull it out of the map. */
10251 : 22122071 : res = back_ref (tag);
10252 : 22122071 : break;
10253 : :
10254 : : case tt_null:
10255 : : /* NULL_TREE. */
10256 : : break;
10257 : :
10258 : 152 : case tt_tu_local:
10259 : 152 : {
10260 : : /* A translation-unit-local entity. */
10261 : 152 : res = make_node (TU_LOCAL_ENTITY);
10262 : 152 : int tag = insert (res);
10263 : :
10264 : 152 : TU_LOCAL_ENTITY_NAME (res) = tree_node ();
10265 : 152 : TU_LOCAL_ENTITY_LOCATION (res) = state->read_location (*this);
10266 : 152 : dump (dumper::TREE) && dump ("Read TU-local entity:%d %N", tag, res);
10267 : : }
10268 : : break;
10269 : :
10270 : 5165283 : case tt_fixed:
10271 : : /* A fixed ref, find it in the fixed_ref array. */
10272 : 5165283 : {
10273 : 5165283 : unsigned fix = u ();
10274 : 5165283 : if (fix < (*fixed_trees).length ())
10275 : : {
10276 : 5165283 : res = (*fixed_trees)[fix];
10277 : 5165283 : dump (dumper::TREE) && dump ("Read fixed:%u %C:%N%S", fix,
10278 : 5046 : TREE_CODE (res), res, res);
10279 : : }
10280 : :
10281 : 5160237 : if (!res)
10282 : 0 : set_overrun ();
10283 : : }
10284 : : break;
10285 : :
10286 : 53101 : case tt_parm:
10287 : 53101 : {
10288 : 53101 : tree fn = tree_node ();
10289 : 53101 : if (fn && TREE_CODE (fn) == FUNCTION_DECL)
10290 : 53101 : res = tree_node ();
10291 : 53101 : if (res)
10292 : 53101 : dump (dumper::TREE)
10293 : 21 : && dump ("Read %s reference %N",
10294 : 21 : TREE_CODE (res) == PARM_DECL ? "parameter" : "result",
10295 : : res);
10296 : : }
10297 : : break;
10298 : :
10299 : 8054876 : case tt_node:
10300 : : /* A new node. Stream it in. */
10301 : 8054876 : res = tree_value ();
10302 : 8054876 : break;
10303 : :
10304 : 866974 : case tt_decl:
10305 : : /* A new decl. Stream it in. */
10306 : 866974 : res = decl_value ();
10307 : 866974 : break;
10308 : :
10309 : 298954 : case tt_tpl_parm:
10310 : : /* A template parameter. Stream it in. */
10311 : 298954 : res = tpl_parm_value ();
10312 : 298954 : break;
10313 : :
10314 : 921539 : case tt_id:
10315 : : /* An identifier node. */
10316 : 921539 : {
10317 : 921539 : size_t l;
10318 : 921539 : const char *chars = str (&l);
10319 : 921539 : res = get_identifier_with_length (chars, l);
10320 : 921539 : int tag = insert (res);
10321 : 921539 : dump (dumper::TREE)
10322 : 1488 : && dump ("Read identifier:%d %N", tag, res);
10323 : : }
10324 : 921539 : break;
10325 : :
10326 : 2304 : case tt_conv_id:
10327 : : /* A conversion operator. Get the type and recreate the
10328 : : identifier. */
10329 : 2304 : {
10330 : 2304 : tree type = tree_node ();
10331 : 2304 : if (!get_overrun ())
10332 : : {
10333 : 2304 : res = type ? make_conv_op_name (type) : conv_op_identifier;
10334 : 2304 : int tag = insert (res);
10335 : 2304 : dump (dumper::TREE)
10336 : 27 : && dump ("Created conv_op:%d %S for %N", tag, res, type);
10337 : : }
10338 : : }
10339 : : break;
10340 : :
10341 : 5195 : case tt_anon_id:
10342 : 5195 : case tt_lambda_id:
10343 : : /* An anonymous or lambda id. */
10344 : 5195 : {
10345 : 5195 : res = make_anon_name ();
10346 : 5195 : if (tag == tt_lambda_id)
10347 : 2897 : IDENTIFIER_LAMBDA_P (res) = true;
10348 : 5195 : int tag = insert (res);
10349 : 5198 : dump (dumper::TREE)
10350 : 3 : && dump ("Read %s identifier:%d %N",
10351 : 3 : IDENTIFIER_LAMBDA_P (res) ? "lambda" : "anon", tag, res);
10352 : : }
10353 : : break;
10354 : :
10355 : 8 : case tt_internal_id:
10356 : : /* An internal label. */
10357 : 8 : {
10358 : 8 : const char *prefix = str ();
10359 : 8 : res = generate_internal_label (prefix);
10360 : 8 : int tag = insert (res);
10361 : 8 : dump (dumper::TREE)
10362 : 1 : && dump ("Read internal identifier:%d %N", tag, res);
10363 : : }
10364 : : break;
10365 : :
10366 : 132005 : case tt_typedef_type:
10367 : 132005 : res = tree_node ();
10368 : 132005 : if (res)
10369 : : {
10370 : 132005 : dump (dumper::TREE)
10371 : 74 : && dump ("Read %stypedef %C:%N",
10372 : 74 : DECL_IMPLICIT_TYPEDEF_P (res) ? "implicit " : "",
10373 : 74 : TREE_CODE (res), res);
10374 : 132005 : if (TREE_CODE (res) != TU_LOCAL_ENTITY)
10375 : 132004 : res = TREE_TYPE (res);
10376 : : }
10377 : : break;
10378 : :
10379 : 1031210 : case tt_derived_type:
10380 : : /* A type derived from some other type. */
10381 : 1031210 : {
10382 : 1031210 : enum tree_code code = tree_code (u ());
10383 : 1031210 : res = tree_node ();
10384 : :
10385 : 1031210 : switch (code)
10386 : : {
10387 : 0 : default:
10388 : 0 : set_overrun ();
10389 : 0 : break;
10390 : :
10391 : 16588 : case ARRAY_TYPE:
10392 : 16588 : {
10393 : 16588 : tree domain = tree_node ();
10394 : 16588 : int dep = u ();
10395 : 16588 : if (!get_overrun ())
10396 : 16588 : res = build_cplus_array_type (res, domain, dep);
10397 : : }
10398 : : break;
10399 : :
10400 : 87 : case COMPLEX_TYPE:
10401 : 87 : if (!get_overrun ())
10402 : 87 : res = build_complex_type (res);
10403 : : break;
10404 : :
10405 : 9 : case BOOLEAN_TYPE:
10406 : 9 : {
10407 : 9 : unsigned precision = u ();
10408 : 9 : if (!get_overrun ())
10409 : 9 : res = build_nonstandard_boolean_type (precision);
10410 : : }
10411 : : break;
10412 : :
10413 : 15158 : case INTEGER_TYPE:
10414 : 15158 : if (res)
10415 : : {
10416 : : /* A range type (representing an array domain). */
10417 : 14525 : tree min = tree_node ();
10418 : 14525 : tree max = tree_node ();
10419 : :
10420 : 14525 : if (!get_overrun ())
10421 : 14525 : res = build_range_type (res, min, max);
10422 : : }
10423 : : else
10424 : : {
10425 : : /* A new integral type (representing a bitfield). */
10426 : 633 : unsigned enc = u ();
10427 : 633 : if (!get_overrun ())
10428 : 633 : res = build_nonstandard_integer_type (enc >> 1, enc & 1);
10429 : : }
10430 : : break;
10431 : :
10432 : 292654 : case FUNCTION_TYPE:
10433 : 292654 : case METHOD_TYPE:
10434 : 292654 : {
10435 : 292654 : tree klass = code == METHOD_TYPE ? tree_node () : NULL_TREE;
10436 : 292654 : tree args = tree_node ();
10437 : 292654 : if (!get_overrun ())
10438 : : {
10439 : 292654 : if (klass)
10440 : 178706 : res = build_method_type_directly (klass, res, args);
10441 : : else
10442 : 113948 : res = cp_build_function_type (res, args);
10443 : : }
10444 : : }
10445 : : break;
10446 : :
10447 : 228 : case OFFSET_TYPE:
10448 : 228 : {
10449 : 228 : tree base = tree_node ();
10450 : 228 : if (!get_overrun ())
10451 : 228 : res = build_offset_type (base, res);
10452 : : }
10453 : : break;
10454 : :
10455 : 142300 : case POINTER_TYPE:
10456 : 142300 : if (!get_overrun ())
10457 : 142300 : res = build_pointer_type (res);
10458 : : break;
10459 : :
10460 : 116821 : case REFERENCE_TYPE:
10461 : 116821 : {
10462 : 116821 : bool rval = bool (u ());
10463 : 116821 : if (!get_overrun ())
10464 : 116821 : res = cp_build_reference_type (res, rval);
10465 : : }
10466 : : break;
10467 : :
10468 : 312510 : case DECLTYPE_TYPE:
10469 : 312510 : case TYPEOF_TYPE:
10470 : 312510 : case DEPENDENT_OPERATOR_TYPE:
10471 : 312510 : {
10472 : 312510 : tree expr = tree_node ();
10473 : 312510 : if (!get_overrun ())
10474 : : {
10475 : 312510 : res = cxx_make_type (code);
10476 : 312510 : TYPE_VALUES_RAW (res) = expr;
10477 : 312510 : if (code == DECLTYPE_TYPE)
10478 : 15629 : tree_node_bools (res);
10479 : 312510 : SET_TYPE_STRUCTURAL_EQUALITY (res);
10480 : : }
10481 : : }
10482 : : break;
10483 : :
10484 : 2040 : case TRAIT_TYPE:
10485 : 2040 : {
10486 : 2040 : tree kind = tree_node ();
10487 : 2040 : tree type1 = tree_node ();
10488 : 2040 : tree type2 = tree_node ();
10489 : 2040 : if (!get_overrun ())
10490 : : {
10491 : 2040 : res = cxx_make_type (TRAIT_TYPE);
10492 : 2040 : TRAIT_TYPE_KIND_RAW (res) = kind;
10493 : 2040 : TRAIT_TYPE_TYPE1 (res) = type1;
10494 : 2040 : TRAIT_TYPE_TYPE2 (res) = type2;
10495 : 2040 : SET_TYPE_STRUCTURAL_EQUALITY (res);
10496 : : }
10497 : : }
10498 : : break;
10499 : :
10500 : 42837 : case TYPE_ARGUMENT_PACK:
10501 : 42837 : if (!get_overrun ())
10502 : : {
10503 : 42837 : tree pack = cxx_make_type (TYPE_ARGUMENT_PACK);
10504 : 42837 : ARGUMENT_PACK_ARGS (pack) = res;
10505 : : res = pack;
10506 : : }
10507 : : break;
10508 : :
10509 : 42464 : case TYPE_PACK_EXPANSION:
10510 : 42464 : {
10511 : 42464 : bool local = u ();
10512 : 42464 : tree param_packs = tree_node ();
10513 : 42464 : tree extra_args = tree_node ();
10514 : 42464 : if (!get_overrun ())
10515 : : {
10516 : 42464 : tree expn = cxx_make_type (TYPE_PACK_EXPANSION);
10517 : 42464 : SET_TYPE_STRUCTURAL_EQUALITY (expn);
10518 : 42464 : PACK_EXPANSION_PATTERN (expn) = res;
10519 : 84928 : PACK_EXPANSION_PARAMETER_PACKS (expn) = param_packs;
10520 : 42464 : PACK_EXPANSION_EXTRA_ARGS (expn) = extra_args;
10521 : 42464 : PACK_EXPANSION_LOCAL_P (expn) = local;
10522 : 42464 : res = expn;
10523 : : }
10524 : : }
10525 : : break;
10526 : :
10527 : 13 : case PACK_INDEX_TYPE:
10528 : 13 : {
10529 : 13 : tree pack = tree_node ();
10530 : 13 : tree index = tree_node ();
10531 : 13 : if (!get_overrun ())
10532 : 13 : res = make_pack_index (pack, index);
10533 : : }
10534 : : break;
10535 : :
10536 : 47427 : case TYPENAME_TYPE:
10537 : 47427 : {
10538 : 47427 : tree ctx = tree_node ();
10539 : 47427 : tree name = tree_node ();
10540 : 47427 : tree fullname = tree_node ();
10541 : 47427 : enum tag_types tag_type = tag_types (u ());
10542 : :
10543 : 47427 : if (!get_overrun ())
10544 : 47427 : res = build_typename_type (ctx, name, fullname, tag_type);
10545 : : }
10546 : : break;
10547 : :
10548 : 44 : case UNBOUND_CLASS_TEMPLATE:
10549 : 44 : {
10550 : 44 : tree ctx = tree_node ();
10551 : 44 : tree name = tree_node ();
10552 : 44 : tree parms = tree_node ();
10553 : :
10554 : 44 : if (!get_overrun ())
10555 : 44 : res = make_unbound_class_template_raw (ctx, name, parms);
10556 : : }
10557 : : break;
10558 : :
10559 : : case VECTOR_TYPE:
10560 : : {
10561 : : poly_uint64 nunits;
10562 : 60 : for (unsigned ix = 0; ix != NUM_POLY_INT_COEFFS; ix++)
10563 : 30 : nunits.coeffs[ix] = wu ();
10564 : 30 : if (!get_overrun ())
10565 : 30 : res = build_vector_type (res, nunits);
10566 : : }
10567 : : break;
10568 : : }
10569 : :
10570 : : /* In the exporting TU, a derived type with attributes was built by
10571 : : build_type_attribute_variant as a distinct copy, with itself as
10572 : : TYPE_MAIN_VARIANT. We repeat that on import to get the version
10573 : : without attributes as TYPE_CANONICAL. */
10574 : 1031210 : if (tree attribs = tree_node ())
10575 : 13199 : res = cp_build_type_attribute_variant (res, attribs);
10576 : :
10577 : 1031210 : int tag = i ();
10578 : 1031210 : if (!tag)
10579 : : {
10580 : 882771 : tag = insert (res);
10581 : 882771 : if (res)
10582 : 882771 : dump (dumper::TREE)
10583 : 678 : && dump ("Created:%d derived type %C", tag, code);
10584 : : }
10585 : : else
10586 : 148439 : res = back_ref (tag);
10587 : : }
10588 : : break;
10589 : :
10590 : 304622 : case tt_variant_type:
10591 : : /* Variant of some type. */
10592 : 304622 : {
10593 : 304622 : res = tree_node ();
10594 : 304622 : int flags = i ();
10595 : 304622 : if (get_overrun ())
10596 : : ;
10597 : 304622 : else if (flags < 0)
10598 : : /* No change. */;
10599 : 147636 : else if (TREE_CODE (res) == FUNCTION_TYPE
10600 : 147636 : || TREE_CODE (res) == METHOD_TYPE)
10601 : : {
10602 : 146346 : cp_ref_qualifier rqual = cp_ref_qualifier (flags & 3);
10603 : 146346 : bool late = (flags >> 2) & 1;
10604 : 146346 : cp_cv_quals quals = cp_cv_quals (flags >> 3);
10605 : :
10606 : 146346 : tree raises = tree_node ();
10607 : 146346 : if (raises == error_mark_node)
10608 : 4969 : raises = TYPE_RAISES_EXCEPTIONS (res);
10609 : :
10610 : 146346 : res = build_cp_fntype_variant (res, rqual, raises, late);
10611 : 146346 : if (TREE_CODE (res) == FUNCTION_TYPE)
10612 : 56067 : res = apply_memfn_quals (res, quals, rqual);
10613 : : }
10614 : : else
10615 : : {
10616 : 1290 : res = build_aligned_type (res, (1u << flags) >> 1);
10617 : 1290 : TYPE_USER_ALIGN (res) = true;
10618 : : }
10619 : :
10620 : 304622 : int quals = i ();
10621 : 304622 : if (quals >= 0 && !get_overrun ())
10622 : 157960 : res = cp_build_qualified_type (res, quals);
10623 : :
10624 : 304622 : int tag = i ();
10625 : 304622 : if (!tag)
10626 : : {
10627 : 304622 : tag = insert (res);
10628 : 304622 : if (res)
10629 : 304622 : dump (dumper::TREE)
10630 : 292 : && dump ("Created:%d variant type %C", tag, TREE_CODE (res));
10631 : : }
10632 : : else
10633 : 0 : res = back_ref (tag);
10634 : : }
10635 : : break;
10636 : :
10637 : 3734 : case tt_tinfo_var:
10638 : 3734 : case tt_tinfo_typedef:
10639 : : /* A tinfo var or typedef. */
10640 : 3734 : {
10641 : 3734 : bool is_var = tag == tt_tinfo_var;
10642 : 3734 : unsigned ix = u ();
10643 : 3734 : tree type = NULL_TREE;
10644 : :
10645 : 3734 : if (is_var)
10646 : : {
10647 : 2230 : tree name = tree_node ();
10648 : 2230 : type = tree_node ();
10649 : :
10650 : 2230 : if (!get_overrun ())
10651 : 2230 : res = get_tinfo_decl_direct (type, name, int (ix));
10652 : : }
10653 : : else
10654 : : {
10655 : 1504 : if (!get_overrun ())
10656 : : {
10657 : 1504 : type = get_pseudo_tinfo_type (ix);
10658 : 1504 : res = TYPE_NAME (type);
10659 : : }
10660 : : }
10661 : 3734 : if (res)
10662 : : {
10663 : 3734 : int tag = insert (res);
10664 : 3734 : dump (dumper::TREE)
10665 : 36 : && dump ("Created tinfo_%s:%d %S:%u for %N",
10666 : : is_var ? "var" : "decl", tag, res, ix, type);
10667 : 3734 : if (!is_var)
10668 : : {
10669 : 1504 : tag = insert (type);
10670 : 1504 : dump (dumper::TREE)
10671 : 12 : && dump ("Created tinfo_type:%d %u %N", tag, ix, type);
10672 : : }
10673 : : }
10674 : : }
10675 : : break;
10676 : :
10677 : 1001 : case tt_ptrmem_type:
10678 : : /* A pointer to member function. */
10679 : 1001 : {
10680 : 1001 : tree type = tree_node ();
10681 : 1001 : if (type && TREE_CODE (type) == POINTER_TYPE
10682 : 2002 : && TREE_CODE (TREE_TYPE (type)) == METHOD_TYPE)
10683 : : {
10684 : 1001 : res = build_ptrmemfunc_type (type);
10685 : 1001 : int tag = insert (res);
10686 : 1004 : dump (dumper::TREE) && dump ("Created:%d ptrmem type", tag);
10687 : : }
10688 : : else
10689 : 0 : set_overrun ();
10690 : : }
10691 : : break;
10692 : :
10693 : 6 : case tt_nttp_var:
10694 : : /* An NTTP object. */
10695 : 6 : {
10696 : 6 : tree init = tree_node ();
10697 : 6 : tree name = tree_node ();
10698 : 6 : if (!get_overrun ())
10699 : : {
10700 : : /* We don't want to check the initializer as that may require
10701 : : name lookup, which could recursively start lazy loading.
10702 : : Instead we know that INIT is already valid so we can just
10703 : : apply that directly. */
10704 : 6 : res = get_template_parm_object (init, name, /*check_init=*/false);
10705 : 6 : int tag = insert (res);
10706 : 6 : dump (dumper::TREE)
10707 : 0 : && dump ("Created nttp object:%d %N", tag, name);
10708 : : }
10709 : : }
10710 : : break;
10711 : :
10712 : 5177 : case tt_enum_value:
10713 : : /* An enum const value. */
10714 : 5177 : {
10715 : 5177 : if (tree decl = tree_node ())
10716 : : {
10717 : 5195 : dump (dumper::TREE) && dump ("Read enum value %N", decl);
10718 : 5177 : res = DECL_INITIAL (decl);
10719 : : }
10720 : :
10721 : 5177 : if (!res)
10722 : 0 : set_overrun ();
10723 : : }
10724 : : break;
10725 : :
10726 : 8957 : case tt_enum_decl:
10727 : : /* An enum decl. */
10728 : 8957 : {
10729 : 8957 : tree ctx = tree_node ();
10730 : 8957 : tree name = tree_node ();
10731 : :
10732 : 8957 : if (!get_overrun ()
10733 : 8957 : && TREE_CODE (ctx) == ENUMERAL_TYPE)
10734 : 8957 : res = find_enum_member (ctx, name);
10735 : :
10736 : 8957 : if (!res)
10737 : 0 : set_overrun ();
10738 : : else
10739 : : {
10740 : 8957 : int tag = insert (res);
10741 : 8957 : dump (dumper::TREE)
10742 : 18 : && dump ("Read enum decl:%d %C:%N", tag, TREE_CODE (res), res);
10743 : : }
10744 : : }
10745 : : break;
10746 : :
10747 : 5708 : case tt_data_member:
10748 : : /* A data member. */
10749 : 5708 : {
10750 : 5708 : tree ctx = tree_node ();
10751 : 5708 : tree name = tree_node ();
10752 : :
10753 : 5708 : if (!get_overrun ()
10754 : 5708 : && RECORD_OR_UNION_TYPE_P (ctx))
10755 : : {
10756 : 5708 : if (name)
10757 : 5028 : res = lookup_class_binding (ctx, name);
10758 : : else
10759 : 680 : res = lookup_field_ident (ctx, u ());
10760 : :
10761 : 5708 : if (!res
10762 : 5708 : || (TREE_CODE (res) != FIELD_DECL
10763 : 5708 : && TREE_CODE (res) != USING_DECL)
10764 : 11416 : || DECL_CONTEXT (res) != ctx)
10765 : : res = NULL_TREE;
10766 : : }
10767 : :
10768 : 5708 : if (!res)
10769 : 0 : set_overrun ();
10770 : : else
10771 : : {
10772 : 5708 : int tag = insert (res);
10773 : 5708 : dump (dumper::TREE)
10774 : 26 : && dump ("Read member:%d %C:%N", tag, TREE_CODE (res), res);
10775 : : }
10776 : : }
10777 : : break;
10778 : :
10779 : 1058 : case tt_binfo:
10780 : : /* A BINFO. Walk the tree of the dominating type. */
10781 : 1058 : {
10782 : 1058 : tree type;
10783 : 1058 : unsigned ix = binfo_mergeable (&type);
10784 : 1058 : if (type)
10785 : : {
10786 : 1058 : res = TYPE_BINFO (type);
10787 : 1113 : for (; ix && res; res = TREE_CHAIN (res))
10788 : 55 : ix--;
10789 : 1058 : if (!res)
10790 : 0 : set_overrun ();
10791 : : }
10792 : :
10793 : 1058 : if (get_overrun ())
10794 : : break;
10795 : :
10796 : : /* Insert binfo into backreferences. */
10797 : 1058 : tag = insert (res);
10798 : 1058 : dump (dumper::TREE) && dump ("Read binfo:%d %N", tag, res);
10799 : : }
10800 : 1058 : break;
10801 : :
10802 : 9 : case tt_vtable:
10803 : 9 : {
10804 : 9 : unsigned ix = u ();
10805 : 9 : tree ctx = tree_node ();
10806 : 9 : dump (dumper::TREE) && dump ("Reading vtable %N[%u]", ctx, ix);
10807 : 9 : if (TREE_CODE (ctx) == RECORD_TYPE && TYPE_LANG_SPECIFIC (ctx))
10808 : 9 : for (res = CLASSTYPE_VTABLES (ctx); res; res = DECL_CHAIN (res))
10809 : 9 : if (!ix--)
10810 : : break;
10811 : 9 : if (!res)
10812 : 0 : set_overrun ();
10813 : : }
10814 : : break;
10815 : :
10816 : 0 : case tt_thunk:
10817 : 0 : {
10818 : 0 : int fixed = i ();
10819 : 0 : tree target = tree_node ();
10820 : 0 : tree virt = tree_node ();
10821 : :
10822 : 0 : for (tree thunk = DECL_THUNKS (target);
10823 : 0 : thunk; thunk = DECL_CHAIN (thunk))
10824 : 0 : if (THUNK_FIXED_OFFSET (thunk) == fixed
10825 : 0 : && !THUNK_VIRTUAL_OFFSET (thunk) == !virt
10826 : 0 : && (!virt
10827 : 0 : || tree_int_cst_equal (virt, THUNK_VIRTUAL_OFFSET (thunk))))
10828 : : {
10829 : : res = thunk;
10830 : : break;
10831 : : }
10832 : :
10833 : 0 : int tag = insert (res);
10834 : 0 : if (res)
10835 : 0 : dump (dumper::TREE)
10836 : 0 : && dump ("Read:%d thunk %N to %N", tag, DECL_NAME (res), target);
10837 : : else
10838 : 0 : set_overrun ();
10839 : : }
10840 : : break;
10841 : :
10842 : 103840 : case tt_clone_ref:
10843 : 103840 : {
10844 : 103840 : tree target = tree_node ();
10845 : 103840 : tree name = tree_node ();
10846 : :
10847 : 103840 : if (DECL_P (target) && DECL_MAYBE_IN_CHARGE_CDTOR_P (target))
10848 : : {
10849 : 103840 : tree clone;
10850 : 162207 : FOR_EVERY_CLONE (clone, target)
10851 : 162207 : if (DECL_NAME (clone) == name)
10852 : : {
10853 : : res = clone;
10854 : : break;
10855 : : }
10856 : : }
10857 : :
10858 : : /* A clone might have a different vtable entry. */
10859 : 103840 : if (res && DECL_VIRTUAL_P (res))
10860 : 6568 : DECL_VINDEX (res) = tree_node ();
10861 : :
10862 : 103840 : if (!res)
10863 : 0 : set_overrun ();
10864 : 103840 : int tag = insert (res);
10865 : 103840 : if (res)
10866 : 103840 : dump (dumper::TREE)
10867 : 230 : && dump ("Read:%d clone %N of %N", tag, DECL_NAME (res), target);
10868 : : else
10869 : 0 : set_overrun ();
10870 : : }
10871 : : break;
10872 : :
10873 : 778294 : case tt_entity:
10874 : : /* Index into the entity table. Perhaps not loaded yet! */
10875 : 778294 : {
10876 : 778294 : unsigned origin = state->slurp->remap_module (u ());
10877 : 778294 : unsigned ident = u ();
10878 : 778294 : module_state *from = (*modules)[origin];
10879 : :
10880 : 778294 : if (!origin || ident >= from->entity_num)
10881 : 0 : set_overrun ();
10882 : 778294 : if (!get_overrun ())
10883 : : {
10884 : 778294 : binding_slot *slot = &(*entity_ary)[from->entity_lwm + ident];
10885 : 778294 : if (slot->is_lazy ())
10886 : 23226 : if (!from->lazy_load (ident, slot))
10887 : 0 : set_overrun ();
10888 : 778294 : res = *slot;
10889 : : }
10890 : :
10891 : 778294 : if (res)
10892 : : {
10893 : 778294 : const char *kind = (origin != state->mod ? "Imported" : "Named");
10894 : 778294 : int tag = insert (res);
10895 : 778294 : dump (dumper::TREE)
10896 : 605 : && dump ("%s:%d %C:%N@%M", kind, tag, TREE_CODE (res),
10897 : 605 : res, (*modules)[origin]);
10898 : :
10899 : 778294 : if (!add_indirects (res))
10900 : : {
10901 : 0 : set_overrun ();
10902 : 0 : res = NULL_TREE;
10903 : : }
10904 : : }
10905 : : }
10906 : : break;
10907 : :
10908 : 2054 : case tt_template:
10909 : : /* A template. */
10910 : 2054 : if (tree tpl = tree_node ())
10911 : : {
10912 : 2054 : res = (TREE_CODE (tpl) == TU_LOCAL_ENTITY ?
10913 : 2054 : tpl : DECL_TEMPLATE_RESULT (tpl));
10914 : 2054 : dump (dumper::TREE)
10915 : 9 : && dump ("Read template %C:%N", TREE_CODE (res), res);
10916 : : }
10917 : : break;
10918 : : }
10919 : :
10920 : 67421391 : if (is_use && !unused && res && DECL_P (res) && !TREE_USED (res))
10921 : : {
10922 : : /* Mark decl used as mark_used does -- we cannot call
10923 : : mark_used in the middle of streaming, we only need a subset
10924 : : of its functionality. */
10925 : 467766 : TREE_USED (res) = true;
10926 : :
10927 : : /* And for structured bindings also the underlying decl. */
10928 : 467766 : if (DECL_DECOMPOSITION_P (res) && !DECL_DECOMP_IS_BASE (res))
10929 : 860 : TREE_USED (DECL_DECOMP_BASE (res)) = true;
10930 : :
10931 : 467766 : if (DECL_CLONED_FUNCTION_P (res))
10932 : 4198 : TREE_USED (DECL_CLONED_FUNCTION (res)) = true;
10933 : : }
10934 : :
10935 : 67421391 : dump.outdent ();
10936 : 67421391 : return res;
10937 : : }
10938 : :
10939 : : void
10940 : 1381527 : trees_out::tpl_parms (tree parms, unsigned &tpl_levels)
10941 : : {
10942 : 1381527 : if (!parms)
10943 : : return;
10944 : :
10945 : 927365 : if (TREE_VISITED (parms))
10946 : : {
10947 : 386935 : ref_node (parms);
10948 : 386935 : return;
10949 : : }
10950 : :
10951 : 540430 : tpl_parms (TREE_CHAIN (parms), tpl_levels);
10952 : :
10953 : 540430 : tree vec = TREE_VALUE (parms);
10954 : 540430 : unsigned len = TREE_VEC_LENGTH (vec);
10955 : : /* Depth. */
10956 : 540430 : int tag = insert (parms);
10957 : 540430 : if (streaming_p ())
10958 : : {
10959 : 146295 : i (len + 1);
10960 : 146361 : dump (dumper::TREE)
10961 : 66 : && dump ("Writing template parms:%d level:%N length:%d",
10962 : 66 : tag, TREE_PURPOSE (parms), len);
10963 : : }
10964 : 540430 : tree_node (TREE_PURPOSE (parms));
10965 : :
10966 : 1475233 : for (unsigned ix = 0; ix != len; ix++)
10967 : : {
10968 : 934803 : tree parm = TREE_VEC_ELT (vec, ix);
10969 : 934803 : tree decl = TREE_VALUE (parm);
10970 : :
10971 : 934803 : gcc_checking_assert (DECL_TEMPLATE_PARM_P (decl));
10972 : 934803 : if (CHECKING_P)
10973 : 934803 : switch (TREE_CODE (decl))
10974 : : {
10975 : 0 : default: gcc_unreachable ();
10976 : :
10977 : 2757 : case TEMPLATE_DECL:
10978 : 2757 : gcc_assert ((TREE_CODE (TREE_TYPE (decl)) == TEMPLATE_TEMPLATE_PARM)
10979 : : && (TREE_CODE (DECL_TEMPLATE_RESULT (decl)) == TYPE_DECL)
10980 : : && (TYPE_NAME (TREE_TYPE (decl)) == decl));
10981 : : break;
10982 : :
10983 : 874220 : case TYPE_DECL:
10984 : 874220 : gcc_assert ((TREE_CODE (TREE_TYPE (decl)) == TEMPLATE_TYPE_PARM)
10985 : : && (TYPE_NAME (TREE_TYPE (decl)) == decl));
10986 : : break;
10987 : :
10988 : 57826 : case PARM_DECL:
10989 : 57826 : gcc_assert ((TREE_CODE (DECL_INITIAL (decl)) == TEMPLATE_PARM_INDEX)
10990 : : && (TREE_CODE (TEMPLATE_PARM_DECL (DECL_INITIAL (decl)))
10991 : : == CONST_DECL)
10992 : : && (DECL_TEMPLATE_PARM_P
10993 : : (TEMPLATE_PARM_DECL (DECL_INITIAL (decl)))));
10994 : : break;
10995 : : }
10996 : :
10997 : 934803 : tree_node (decl);
10998 : 934803 : tree_node (TEMPLATE_PARM_CONSTRAINTS (parm));
10999 : : }
11000 : :
11001 : 540430 : tpl_levels++;
11002 : : }
11003 : :
11004 : : tree
11005 : 232831 : trees_in::tpl_parms (unsigned &tpl_levels)
11006 : : {
11007 : 232831 : tree parms = NULL_TREE;
11008 : :
11009 : 488779 : while (int len = i ())
11010 : : {
11011 : 255948 : if (len < 0)
11012 : : {
11013 : 137700 : parms = back_ref (len);
11014 : 137700 : continue;
11015 : : }
11016 : :
11017 : 118248 : len -= 1;
11018 : 118248 : parms = tree_cons (NULL_TREE, NULL_TREE, parms);
11019 : 118248 : int tag = insert (parms);
11020 : 118248 : TREE_PURPOSE (parms) = tree_node ();
11021 : :
11022 : 118248 : dump (dumper::TREE)
11023 : 105 : && dump ("Reading template parms:%d level:%N length:%d",
11024 : 105 : tag, TREE_PURPOSE (parms), len);
11025 : :
11026 : 118248 : tree vec = make_tree_vec (len);
11027 : 313237 : for (int ix = 0; ix != len; ix++)
11028 : : {
11029 : 194989 : tree decl = tree_node ();
11030 : 194989 : if (!decl)
11031 : : return NULL_TREE;
11032 : :
11033 : 194989 : tree parm = build_tree_list (NULL, decl);
11034 : 194989 : TEMPLATE_PARM_CONSTRAINTS (parm) = tree_node ();
11035 : :
11036 : 194989 : TREE_VEC_ELT (vec, ix) = parm;
11037 : : }
11038 : :
11039 : 118248 : TREE_VALUE (parms) = vec;
11040 : 118248 : tpl_levels++;
11041 : : }
11042 : :
11043 : : return parms;
11044 : : }
11045 : :
11046 : : void
11047 : 841097 : trees_out::tpl_parms_fini (tree tmpl, unsigned tpl_levels)
11048 : : {
11049 : 841097 : for (tree parms = DECL_TEMPLATE_PARMS (tmpl);
11050 : 1381527 : tpl_levels--; parms = TREE_CHAIN (parms))
11051 : : {
11052 : 540430 : tree vec = TREE_VALUE (parms);
11053 : :
11054 : 540430 : tree_node (TREE_TYPE (vec));
11055 : 1475233 : for (unsigned ix = TREE_VEC_LENGTH (vec); ix--;)
11056 : : {
11057 : 934803 : tree parm = TREE_VEC_ELT (vec, ix);
11058 : 934803 : tree dflt = TREE_PURPOSE (parm);
11059 : 934803 : tree_node (dflt);
11060 : :
11061 : : /* Template template parameters need a context of their owning
11062 : : template. This is quite tricky to infer correctly on stream-in
11063 : : (see PR c++/98881) so we'll just provide it directly. */
11064 : 934803 : tree decl = TREE_VALUE (parm);
11065 : 934803 : if (TREE_CODE (decl) == TEMPLATE_DECL)
11066 : 2757 : tree_node (DECL_CONTEXT (decl));
11067 : : }
11068 : : }
11069 : 841097 : }
11070 : :
11071 : : bool
11072 : 232831 : trees_in::tpl_parms_fini (tree tmpl, unsigned tpl_levels)
11073 : : {
11074 : 232831 : for (tree parms = DECL_TEMPLATE_PARMS (tmpl);
11075 : 351079 : tpl_levels--; parms = TREE_CHAIN (parms))
11076 : : {
11077 : 118248 : tree vec = TREE_VALUE (parms);
11078 : :
11079 : 118248 : TREE_TYPE (vec) = tree_node ();
11080 : 313237 : for (unsigned ix = TREE_VEC_LENGTH (vec); ix--;)
11081 : : {
11082 : 194989 : tree parm = TREE_VEC_ELT (vec, ix);
11083 : 194989 : tree dflt = tree_node ();
11084 : 194989 : TREE_PURPOSE (parm) = dflt;
11085 : :
11086 : 194989 : tree decl = TREE_VALUE (parm);
11087 : 194989 : if (TREE_CODE (decl) == TEMPLATE_DECL)
11088 : 612 : DECL_CONTEXT (decl) = tree_node ();
11089 : :
11090 : 194989 : if (get_overrun ())
11091 : : return false;
11092 : : }
11093 : : }
11094 : : return true;
11095 : : }
11096 : :
11097 : : /* PARMS is a LIST, one node per level.
11098 : : TREE_VALUE is a TREE_VEC of parm info for that level.
11099 : : each ELT is a TREE_LIST
11100 : : TREE_VALUE is PARM_DECL, TYPE_DECL or TEMPLATE_DECL
11101 : : TREE_PURPOSE is the default value. */
11102 : :
11103 : : void
11104 : 841097 : trees_out::tpl_header (tree tpl, unsigned *tpl_levels)
11105 : : {
11106 : 841097 : tree parms = DECL_TEMPLATE_PARMS (tpl);
11107 : 841097 : tpl_parms (parms, *tpl_levels);
11108 : :
11109 : : /* Mark end. */
11110 : 841097 : if (streaming_p ())
11111 : 279988 : u (0);
11112 : :
11113 : 841097 : if (*tpl_levels)
11114 : 509617 : tree_node (TEMPLATE_PARMS_CONSTRAINTS (parms));
11115 : 841097 : }
11116 : :
11117 : : bool
11118 : 232831 : trees_in::tpl_header (tree tpl, unsigned *tpl_levels)
11119 : : {
11120 : 232831 : tree parms = tpl_parms (*tpl_levels);
11121 : 232831 : if (!parms)
11122 : : return false;
11123 : :
11124 : 232831 : DECL_TEMPLATE_PARMS (tpl) = parms;
11125 : :
11126 : 232831 : if (*tpl_levels)
11127 : 116809 : TEMPLATE_PARMS_CONSTRAINTS (parms) = tree_node ();
11128 : :
11129 : : return true;
11130 : : }
11131 : :
11132 : : /* Stream skeleton parm nodes, with their flags, type & parm indices.
11133 : : All the parms will have consecutive tags. */
11134 : :
11135 : : void
11136 : 1136837 : trees_out::fn_parms_init (tree fn)
11137 : : {
11138 : : /* First init them. */
11139 : 1136837 : int base_tag = ref_num - 1;
11140 : 1136837 : int ix = 0;
11141 : 1136837 : for (tree parm = DECL_ARGUMENTS (fn);
11142 : 3409616 : parm; parm = DECL_CHAIN (parm), ix++)
11143 : : {
11144 : 2272779 : if (streaming_p ())
11145 : : {
11146 : 757560 : start (parm);
11147 : 757560 : tree_node_bools (parm);
11148 : : }
11149 : 2272779 : int tag = insert (parm);
11150 : 2272779 : gcc_checking_assert (base_tag - ix == tag);
11151 : : }
11152 : : /* Mark the end. */
11153 : 1136837 : if (streaming_p ())
11154 : 379099 : u (0);
11155 : :
11156 : : /* Now stream their contents. */
11157 : 1136837 : ix = 0;
11158 : 1136837 : for (tree parm = DECL_ARGUMENTS (fn);
11159 : 3409616 : parm; parm = DECL_CHAIN (parm), ix++)
11160 : : {
11161 : 2272779 : if (streaming_p ())
11162 : 757560 : dump (dumper::TREE)
11163 : 222 : && dump ("Writing parm:%d %u (%N) of %N",
11164 : : base_tag - ix, ix, parm, fn);
11165 : 2272779 : tree_node_vals (parm);
11166 : : }
11167 : :
11168 : 1136837 : if (!streaming_p ())
11169 : : {
11170 : : /* We must walk contract attrs so the dependency graph is complete. */
11171 : 757738 : for (tree contract = DECL_CONTRACTS (fn);
11172 : 757846 : contract;
11173 : 108 : contract = CONTRACT_CHAIN (contract))
11174 : 108 : tree_node (contract);
11175 : : }
11176 : :
11177 : : /* Write a reference to contracts pre/post functions, if any, to avoid
11178 : : regenerating them in importers. */
11179 : 1136837 : tree_node (DECL_PRE_FN (fn));
11180 : 1136837 : tree_node (DECL_POST_FN (fn));
11181 : 1136837 : }
11182 : :
11183 : : /* Build skeleton parm nodes, read their flags, type & parm indices. */
11184 : :
11185 : : int
11186 : 324408 : trees_in::fn_parms_init (tree fn)
11187 : : {
11188 : 324408 : int base_tag = ~(int)back_refs.length ();
11189 : :
11190 : 324408 : tree *parm_ptr = &DECL_ARGUMENTS (fn);
11191 : 324408 : int ix = 0;
11192 : 975006 : for (; int code = u (); ix++)
11193 : : {
11194 : 650598 : tree parm = start (code);
11195 : 650598 : if (!tree_node_bools (parm))
11196 : : return 0;
11197 : :
11198 : 650598 : int tag = insert (parm);
11199 : 650598 : gcc_checking_assert (base_tag - ix == tag);
11200 : 650598 : *parm_ptr = parm;
11201 : 650598 : parm_ptr = &DECL_CHAIN (parm);
11202 : 650598 : }
11203 : :
11204 : 324408 : ix = 0;
11205 : 324408 : for (tree parm = DECL_ARGUMENTS (fn);
11206 : 975006 : parm; parm = DECL_CHAIN (parm), ix++)
11207 : : {
11208 : 650598 : dump (dumper::TREE)
11209 : 362 : && dump ("Reading parm:%d %u (%N) of %N",
11210 : : base_tag - ix, ix, parm, fn);
11211 : 650598 : if (!tree_node_vals (parm))
11212 : : return 0;
11213 : : }
11214 : :
11215 : : /* Reload references to contract functions, if any. */
11216 : 324408 : tree pre_fn = tree_node ();
11217 : 324408 : tree post_fn = tree_node ();
11218 : 324408 : set_contract_functions (fn, pre_fn, post_fn);
11219 : :
11220 : 324408 : return base_tag;
11221 : : }
11222 : :
11223 : : /* Read the remaining parm node data. Replace with existing (if
11224 : : non-null) in the map. */
11225 : :
11226 : : void
11227 : 324408 : trees_in::fn_parms_fini (int tag, tree fn, tree existing, bool is_defn)
11228 : : {
11229 : 512869 : tree existing_parm = existing ? DECL_ARGUMENTS (existing) : NULL_TREE;
11230 : 324408 : tree parms = DECL_ARGUMENTS (fn);
11231 : 975006 : for (tree parm = parms; parm; parm = DECL_CHAIN (parm))
11232 : : {
11233 : 650598 : if (existing_parm)
11234 : : {
11235 : 554435 : if (is_defn && !DECL_SAVED_TREE (existing))
11236 : : {
11237 : : /* If we're about to become the definition, set the
11238 : : names of the parms from us. */
11239 : 13699 : DECL_NAME (existing_parm) = DECL_NAME (parm);
11240 : 13699 : DECL_SOURCE_LOCATION (existing_parm) = DECL_SOURCE_LOCATION (parm);
11241 : :
11242 : : /* And some other flags important for codegen are only set
11243 : : by the definition. */
11244 : 13699 : TREE_ADDRESSABLE (existing_parm) = TREE_ADDRESSABLE (parm);
11245 : 13699 : DECL_BY_REFERENCE (existing_parm) = DECL_BY_REFERENCE (parm);
11246 : 13699 : DECL_NONLOCAL (existing_parm) = DECL_NONLOCAL (parm);
11247 : 13699 : DECL_ARG_TYPE (existing_parm) = DECL_ARG_TYPE (parm);
11248 : :
11249 : : /* Invisiref parms had their types adjusted by cp_genericize. */
11250 : 13699 : if (DECL_BY_REFERENCE (parm))
11251 : : {
11252 : 6 : TREE_TYPE (existing_parm) = TREE_TYPE (parm);
11253 : 6 : relayout_decl (existing_parm);
11254 : : }
11255 : : }
11256 : :
11257 : 375460 : back_refs[~tag] = existing_parm;
11258 : 375460 : existing_parm = DECL_CHAIN (existing_parm);
11259 : : }
11260 : 650598 : tag--;
11261 : : }
11262 : 324408 : }
11263 : :
11264 : : /* Encode into KEY the position of the local type (class or enum)
11265 : : declaration DECL within FN. The position is encoded as the
11266 : : index of the innermost BLOCK (numbered in BFS order) along with
11267 : : the index within its BLOCK_VARS list. */
11268 : :
11269 : : void
11270 : 10707 : trees_out::key_local_type (merge_key& key, tree decl, tree fn)
11271 : : {
11272 : 10707 : auto_vec<tree, 4> blocks;
11273 : 10707 : blocks.quick_push (DECL_INITIAL (fn));
11274 : 10707 : unsigned block_ix = 0;
11275 : 47901 : while (block_ix != blocks.length ())
11276 : : {
11277 : 18597 : tree block = blocks[block_ix];
11278 : 18597 : unsigned decl_ix = 0;
11279 : 52608 : for (tree var = BLOCK_VARS (block); var; var = DECL_CHAIN (var))
11280 : : {
11281 : 44718 : if (TREE_CODE (var) != TYPE_DECL)
11282 : 26742 : continue;
11283 : 17976 : if (var == decl)
11284 : : {
11285 : 10707 : key.index = (block_ix << 10) | decl_ix;
11286 : 10707 : return;
11287 : : }
11288 : 7269 : ++decl_ix;
11289 : : }
11290 : 16470 : for (tree sub = BLOCK_SUBBLOCKS (block); sub; sub = BLOCK_CHAIN (sub))
11291 : 8580 : blocks.safe_push (sub);
11292 : 7890 : ++block_ix;
11293 : : }
11294 : :
11295 : : /* Not-found value. */
11296 : 0 : key.index = 1023;
11297 : 10707 : }
11298 : :
11299 : : /* Look up the local type corresponding at the position encoded by
11300 : : KEY within FN and named NAME. */
11301 : :
11302 : : tree
11303 : 3194 : trees_in::key_local_type (const merge_key& key, tree fn, tree name)
11304 : : {
11305 : 3194 : if (!DECL_INITIAL (fn))
11306 : : return NULL_TREE;
11307 : :
11308 : 1945 : const unsigned block_pos = key.index >> 10;
11309 : 1945 : const unsigned decl_pos = key.index & 1023;
11310 : :
11311 : 1945 : if (decl_pos == 1023)
11312 : : return NULL_TREE;
11313 : :
11314 : 1945 : auto_vec<tree, 4> blocks;
11315 : 1945 : blocks.quick_push (DECL_INITIAL (fn));
11316 : 1945 : unsigned block_ix = 0;
11317 : 8783 : while (block_ix != blocks.length ())
11318 : : {
11319 : 3419 : tree block = blocks[block_ix];
11320 : 3419 : if (block_ix == block_pos)
11321 : : {
11322 : 1945 : unsigned decl_ix = 0;
11323 : 5047 : for (tree var = BLOCK_VARS (block); var; var = DECL_CHAIN (var))
11324 : : {
11325 : 5047 : if (TREE_CODE (var) != TYPE_DECL)
11326 : 2173 : continue;
11327 : : /* Prefer using the identifier as the key for more robustness
11328 : : to ODR violations, except for anonymous types since their
11329 : : compiler-generated identifiers aren't stable. */
11330 : 5748 : if (IDENTIFIER_ANON_P (name)
11331 : 2874 : ? decl_ix == decl_pos
11332 : 302 : : DECL_NAME (var) == name)
11333 : : return var;
11334 : 929 : ++decl_ix;
11335 : : }
11336 : : return NULL_TREE;
11337 : : }
11338 : 3057 : for (tree sub = BLOCK_SUBBLOCKS (block); sub; sub = BLOCK_CHAIN (sub))
11339 : 1583 : blocks.safe_push (sub);
11340 : 1474 : ++block_ix;
11341 : : }
11342 : :
11343 : : return NULL_TREE;
11344 : 1945 : }
11345 : :
11346 : : /* DEP is the depset of some decl we're streaming by value. Determine
11347 : : the merging behaviour. */
11348 : :
11349 : : merge_kind
11350 : 2920249 : trees_out::get_merge_kind (tree decl, depset *dep)
11351 : : {
11352 : 2920249 : if (!dep)
11353 : : {
11354 : 544628 : if (VAR_OR_FUNCTION_DECL_P (decl))
11355 : : {
11356 : : /* Any var or function with template info should have DEP. */
11357 : 287810 : gcc_checking_assert (!DECL_LANG_SPECIFIC (decl)
11358 : : || !DECL_TEMPLATE_INFO (decl));
11359 : 287810 : if (DECL_LOCAL_DECL_P (decl))
11360 : : return MK_unique;
11361 : : }
11362 : :
11363 : : /* Either unique, or some member of a class that cannot have an
11364 : : out-of-class definition. For instance a FIELD_DECL. */
11365 : 544392 : tree ctx = CP_DECL_CONTEXT (decl);
11366 : 544392 : if (TREE_CODE (ctx) == FUNCTION_DECL)
11367 : : {
11368 : : /* USING_DECLs and NAMESPACE_DECLs cannot have DECL_TEMPLATE_INFO --
11369 : : this isn't permitting them to have one. */
11370 : 324426 : gcc_checking_assert (TREE_CODE (decl) == USING_DECL
11371 : : || TREE_CODE (decl) == NAMESPACE_DECL
11372 : : || !DECL_LANG_SPECIFIC (decl)
11373 : : || !DECL_TEMPLATE_INFO (decl));
11374 : :
11375 : : return MK_unique;
11376 : : }
11377 : :
11378 : 219966 : if (TREE_CODE (decl) == TEMPLATE_DECL
11379 : 219966 : && DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (decl))
11380 : : return MK_local_friend;
11381 : :
11382 : 219966 : gcc_checking_assert (TYPE_P (ctx));
11383 : :
11384 : : /* Internal-only types will not need to dedup their members. */
11385 : 219966 : if (!DECL_CONTEXT (TYPE_NAME (ctx)))
11386 : : return MK_unique;
11387 : :
11388 : 219910 : if (TREE_CODE (decl) == USING_DECL)
11389 : : return MK_field;
11390 : :
11391 : 140303 : if (TREE_CODE (decl) == FIELD_DECL)
11392 : : {
11393 : 98093 : if (DECL_NAME (decl))
11394 : : {
11395 : : /* Anonymous FIELD_DECLs have a NULL name. */
11396 : 76780 : gcc_checking_assert (!IDENTIFIER_ANON_P (DECL_NAME (decl)));
11397 : : return MK_named;
11398 : : }
11399 : :
11400 : 21313 : if (walking_bit_field_unit)
11401 : : {
11402 : : /* The underlying storage unit for a bitfield. We do not
11403 : : need to dedup it, because it's only reachable through
11404 : : the bitfields it represents. And those are deduped. */
11405 : : // FIXME: Is that assertion correct -- do we ever fish it
11406 : : // out and put it in an expr?
11407 : 354 : gcc_checking_assert (!DECL_NAME (decl)
11408 : : && !RECORD_OR_UNION_TYPE_P (TREE_TYPE (decl))
11409 : : && !DECL_BIT_FIELD_REPRESENTATIVE (decl));
11410 : 354 : gcc_checking_assert ((TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
11411 : : ? TREE_CODE (TREE_TYPE (TREE_TYPE (decl)))
11412 : : : TREE_CODE (TREE_TYPE (decl)))
11413 : : == INTEGER_TYPE);
11414 : : return MK_unique;
11415 : : }
11416 : :
11417 : : return MK_field;
11418 : : }
11419 : :
11420 : 42210 : if (TREE_CODE (decl) == CONST_DECL)
11421 : : return MK_named;
11422 : :
11423 : 6620 : if (TREE_CODE (decl) == VAR_DECL
11424 : 6620 : && DECL_VTABLE_OR_VTT_P (decl))
11425 : : return MK_vtable;
11426 : :
11427 : 1064 : if (DECL_THUNK_P (decl))
11428 : : /* Thunks are unique-enough, because they're only referenced
11429 : : from the vtable. And that's either new (so we want the
11430 : : thunks), or it's a duplicate (so it will be dropped). */
11431 : : return MK_unique;
11432 : :
11433 : : /* There should be no other cases. */
11434 : 0 : gcc_unreachable ();
11435 : : }
11436 : :
11437 : 2375621 : gcc_checking_assert (TREE_CODE (decl) != FIELD_DECL
11438 : : && TREE_CODE (decl) != USING_DECL
11439 : : && TREE_CODE (decl) != CONST_DECL);
11440 : :
11441 : 2375621 : if (is_key_order ())
11442 : : {
11443 : : /* When doing the mergeablilty graph, there's an indirection to
11444 : : the actual depset. */
11445 : 791738 : gcc_assert (dep->is_special ());
11446 : 791738 : dep = dep->deps[0];
11447 : : }
11448 : :
11449 : 2375621 : gcc_checking_assert (decl == dep->get_entity ());
11450 : :
11451 : 2375621 : merge_kind mk = MK_named;
11452 : 2375621 : switch (dep->get_entity_kind ())
11453 : : {
11454 : 0 : default:
11455 : 0 : gcc_unreachable ();
11456 : :
11457 : : case depset::EK_PARTIAL:
11458 : : mk = MK_partial;
11459 : : break;
11460 : :
11461 : 1390841 : case depset::EK_DECL:
11462 : 1390841 : {
11463 : 1390841 : tree ctx = CP_DECL_CONTEXT (decl);
11464 : :
11465 : 1390841 : switch (TREE_CODE (ctx))
11466 : : {
11467 : 0 : default:
11468 : 0 : gcc_unreachable ();
11469 : :
11470 : 10707 : case FUNCTION_DECL:
11471 : 10707 : gcc_checking_assert
11472 : : (DECL_IMPLICIT_TYPEDEF_P (STRIP_TEMPLATE (decl)));
11473 : :
11474 : : mk = MK_local_type;
11475 : : break;
11476 : :
11477 : 1380134 : case RECORD_TYPE:
11478 : 1380134 : case UNION_TYPE:
11479 : 1380134 : case NAMESPACE_DECL:
11480 : 1380134 : if (DECL_NAME (decl) == as_base_identifier)
11481 : : {
11482 : : mk = MK_as_base;
11483 : : break;
11484 : : }
11485 : :
11486 : : /* A lambda may have a class as its context, even though it
11487 : : isn't a member in the traditional sense; see the test
11488 : : g++.dg/modules/lambda-6_a.C. */
11489 : 1745102 : if (DECL_IMPLICIT_TYPEDEF_P (STRIP_TEMPLATE (decl))
11490 : 1510757 : && LAMBDA_TYPE_P (TREE_TYPE (decl)))
11491 : : {
11492 : 502 : if (get_keyed_decl_scope (decl))
11493 : : mk = MK_keyed;
11494 : : else
11495 : : /* Lambdas not attached to any mangling scope are TU-local
11496 : : and so cannot be deduplicated. */
11497 : 326631 : mk = MK_unique;
11498 : : break;
11499 : : }
11500 : :
11501 : 1314994 : if (TREE_CODE (decl) == TEMPLATE_DECL
11502 : 1314994 : ? DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (decl)
11503 : 596370 : : decl_specialization_friend_p (decl))
11504 : : {
11505 : : mk = MK_local_friend;
11506 : : break;
11507 : : }
11508 : :
11509 : 1299928 : if (DECL_DECOMPOSITION_P (decl))
11510 : : {
11511 : : mk = MK_unique;
11512 : : break;
11513 : : }
11514 : :
11515 : 1299451 : if (IDENTIFIER_ANON_P (DECL_NAME (decl)))
11516 : : {
11517 : 21329 : if (RECORD_OR_UNION_TYPE_P (ctx))
11518 : : mk = MK_field;
11519 : 1015 : else if (DECL_IMPLICIT_TYPEDEF_P (decl)
11520 : 1015 : && UNSCOPED_ENUM_P (TREE_TYPE (decl))
11521 : 2030 : && TYPE_VALUES (TREE_TYPE (decl)))
11522 : : /* Keyed by first enum value, and underlying type. */
11523 : : mk = MK_enum;
11524 : : else
11525 : : /* No way to merge it, it is an ODR land-mine. */
11526 : : mk = MK_unique;
11527 : : }
11528 : : }
11529 : : }
11530 : : break;
11531 : :
11532 : 941727 : case depset::EK_SPECIALIZATION:
11533 : 941727 : {
11534 : 941727 : gcc_checking_assert (dep->is_special ());
11535 : :
11536 : 941727 : if (TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
11537 : : /* An block-scope classes of templates are themselves
11538 : : templates. */
11539 : 4140 : gcc_checking_assert (DECL_IMPLICIT_TYPEDEF_P (decl));
11540 : :
11541 : 941727 : if (dep->is_friend_spec ())
11542 : : mk = MK_friend_spec;
11543 : 941727 : else if (dep->is_type_spec ())
11544 : : mk = MK_type_spec;
11545 : : else
11546 : 651072 : mk = MK_decl_spec;
11547 : :
11548 : 941727 : if (TREE_CODE (decl) == TEMPLATE_DECL)
11549 : : {
11550 : 65301 : spec_entry *entry = reinterpret_cast <spec_entry *> (dep->deps[0]);
11551 : 65301 : if (TREE_CODE (entry->spec) != TEMPLATE_DECL)
11552 : 7041 : mk = merge_kind (mk | MK_tmpl_tmpl_mask);
11553 : : }
11554 : : }
11555 : : break;
11556 : : }
11557 : :
11558 : : return mk;
11559 : : }
11560 : :
11561 : :
11562 : : /* The container of DECL -- not necessarily its context! */
11563 : :
11564 : : tree
11565 : 2920249 : trees_out::decl_container (tree decl)
11566 : : {
11567 : 2920249 : int use_tpl;
11568 : 2920249 : tree tpl = NULL_TREE;
11569 : 2920249 : if (tree template_info = node_template_info (decl, use_tpl))
11570 : 990776 : tpl = TI_TEMPLATE (template_info);
11571 : 2920249 : if (tpl == decl)
11572 : 0 : tpl = nullptr;
11573 : :
11574 : : /* Stream the template we're instantiated from. */
11575 : 2920249 : tree_node (tpl);
11576 : :
11577 : 2920249 : tree container = NULL_TREE;
11578 : 2920249 : if (TREE_CODE (decl) == TEMPLATE_DECL
11579 : 2920249 : ? DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (decl)
11580 : 2083938 : : decl_specialization_friend_p (decl))
11581 : 15066 : container = DECL_CHAIN (decl);
11582 : : else
11583 : 2905183 : container = CP_DECL_CONTEXT (decl);
11584 : :
11585 : 2920249 : if (TYPE_P (container))
11586 : 1717601 : container = TYPE_NAME (container);
11587 : :
11588 : 2920249 : tree_node (container);
11589 : :
11590 : 2920249 : return container;
11591 : : }
11592 : :
11593 : : tree
11594 : 866974 : trees_in::decl_container ()
11595 : : {
11596 : : /* The maybe-template. */
11597 : 866974 : (void)tree_node ();
11598 : :
11599 : 866974 : tree container = tree_node ();
11600 : :
11601 : 866974 : return container;
11602 : : }
11603 : :
11604 : : /* Write out key information about a mergeable DEP. Does not write
11605 : : the contents of DEP itself. The context has already been
11606 : : written. The container has already been streamed. */
11607 : :
11608 : : void
11609 : 2920249 : trees_out::key_mergeable (int tag, merge_kind mk, tree decl, tree inner,
11610 : : tree container, depset *dep)
11611 : : {
11612 : 2920249 : if (dep && is_key_order ())
11613 : : {
11614 : 791738 : gcc_checking_assert (dep->is_special ());
11615 : 791738 : dep = dep->deps[0];
11616 : : }
11617 : :
11618 : 2920249 : if (streaming_p ())
11619 : 1058893 : dump (dumper::MERGE)
11620 : 798 : && dump ("Writing:%d's %s merge key (%s) %C:%N", tag, merge_kind_name[mk],
11621 : 723 : dep ? dep->entity_kind_name () : "contained",
11622 : 798 : TREE_CODE (decl), decl);
11623 : :
11624 : : /* Now write the locating information. */
11625 : 2920249 : if (mk & MK_template_mask)
11626 : : {
11627 : : /* Specializations are located via their originating template,
11628 : : and the set of template args they specialize. */
11629 : 941727 : gcc_checking_assert (dep && dep->is_special ());
11630 : 941727 : spec_entry *entry = reinterpret_cast <spec_entry *> (dep->deps[0]);
11631 : :
11632 : 941727 : tree_node (entry->tmpl);
11633 : 941727 : tree_node (entry->args);
11634 : 941727 : if (mk & MK_tmpl_decl_mask)
11635 : 651072 : if (flag_concepts && TREE_CODE (inner) == VAR_DECL)
11636 : : {
11637 : : /* Variable template partial specializations might need
11638 : : constraints (see spec_hasher::equal). It's simpler to
11639 : : write NULL when we don't need them. */
11640 : 13620 : tree constraints = NULL_TREE;
11641 : :
11642 : 13620 : if (uses_template_parms (entry->args))
11643 : 414 : constraints = get_constraints (inner);
11644 : 13620 : tree_node (constraints);
11645 : : }
11646 : :
11647 : 941727 : if (CHECKING_P)
11648 : : {
11649 : : /* Make sure we can locate the decl. */
11650 : 941727 : tree existing = match_mergeable_specialization
11651 : 941727 : (bool (mk & MK_tmpl_decl_mask), entry);
11652 : :
11653 : 941727 : gcc_assert (existing);
11654 : 941727 : if (mk & MK_tmpl_decl_mask)
11655 : : {
11656 : 651072 : if (mk & MK_tmpl_tmpl_mask)
11657 : 5586 : existing = DECL_TI_TEMPLATE (existing);
11658 : : }
11659 : : else
11660 : : {
11661 : 290655 : if (mk & MK_tmpl_tmpl_mask)
11662 : 1455 : existing = CLASSTYPE_TI_TEMPLATE (existing);
11663 : : else
11664 : 289200 : existing = TYPE_NAME (existing);
11665 : : }
11666 : :
11667 : : /* The walkabout should have found ourselves. */
11668 : 941727 : gcc_checking_assert (TREE_CODE (decl) == TYPE_DECL
11669 : : ? same_type_p (TREE_TYPE (decl),
11670 : : TREE_TYPE (existing))
11671 : : : existing == decl);
11672 : : }
11673 : : }
11674 : 1978522 : else if (mk != MK_unique)
11675 : : {
11676 : 1651891 : merge_key key;
11677 : 1651891 : tree name = DECL_NAME (decl);
11678 : :
11679 : 1651891 : switch (mk)
11680 : : {
11681 : 0 : default:
11682 : 0 : gcc_unreachable ();
11683 : :
11684 : 1390492 : case MK_named:
11685 : 1390492 : case MK_friend_spec:
11686 : 1390492 : if (IDENTIFIER_CONV_OP_P (name))
11687 : 4525 : name = conv_op_identifier;
11688 : :
11689 : 1390492 : if (TREE_CODE (inner) == FUNCTION_DECL)
11690 : : {
11691 : : /* Functions are distinguished by parameter types. */
11692 : 729343 : tree fn_type = TREE_TYPE (inner);
11693 : :
11694 : 729343 : key.ref_q = type_memfn_rqual (fn_type);
11695 : 729343 : key.args = TYPE_ARG_TYPES (fn_type);
11696 : :
11697 : 729343 : if (tree reqs = get_constraints (inner))
11698 : : {
11699 : 50514 : if (cxx_dialect < cxx20)
11700 : 48 : reqs = CI_ASSOCIATED_CONSTRAINTS (reqs);
11701 : : else
11702 : 100980 : reqs = CI_DECLARATOR_REQS (reqs);
11703 : 50514 : key.constraints = reqs;
11704 : : }
11705 : :
11706 : 729343 : if (IDENTIFIER_CONV_OP_P (name)
11707 : 729343 : || (decl != inner
11708 : 380277 : && !(name == fun_identifier
11709 : : /* In case the user names something _FUN */
11710 : 84 : && LAMBDA_TYPE_P (DECL_CONTEXT (inner)))))
11711 : : /* And a function template, or conversion operator needs
11712 : : the return type. Except for the _FUN thunk of a
11713 : : generic lambda, which has a recursive decl_type'd
11714 : : return type. */
11715 : : // FIXME: What if the return type is a voldemort?
11716 : 384760 : key.ret = fndecl_declared_return_type (inner);
11717 : : }
11718 : : break;
11719 : :
11720 : 120880 : case MK_field:
11721 : 120880 : {
11722 : 120880 : unsigned ix = 0;
11723 : 120880 : if (TREE_CODE (inner) != FIELD_DECL)
11724 : : name = NULL_TREE;
11725 : : else
11726 : 20959 : gcc_checking_assert (!name || !IDENTIFIER_ANON_P (name));
11727 : :
11728 : 120880 : for (tree field = TYPE_FIELDS (TREE_TYPE (container));
11729 : 2901660 : ; field = DECL_CHAIN (field))
11730 : : {
11731 : 3022540 : tree finner = STRIP_TEMPLATE (field);
11732 : 3022540 : if (TREE_CODE (finner) == TREE_CODE (inner))
11733 : : {
11734 : 1021288 : if (finner == inner)
11735 : : break;
11736 : 900408 : ix++;
11737 : : }
11738 : 2901660 : }
11739 : 120880 : key.index = ix;
11740 : : }
11741 : 120880 : break;
11742 : :
11743 : 5556 : case MK_vtable:
11744 : 5556 : {
11745 : 5556 : tree vtable = CLASSTYPE_VTABLES (TREE_TYPE (container));
11746 : 7020 : for (unsigned ix = 0; ; vtable = DECL_CHAIN (vtable), ix++)
11747 : 7020 : if (vtable == decl)
11748 : : {
11749 : 5556 : key.index = ix;
11750 : 5556 : break;
11751 : : }
11752 : 5556 : name = NULL_TREE;
11753 : : }
11754 : 5556 : break;
11755 : :
11756 : 64638 : case MK_as_base:
11757 : 64638 : gcc_checking_assert
11758 : : (decl == TYPE_NAME (CLASSTYPE_AS_BASE (TREE_TYPE (container))));
11759 : : break;
11760 : :
11761 : 15066 : case MK_local_friend:
11762 : 15066 : {
11763 : : /* Find by index on the class's DECL_LIST. We set TREE_CHAIN to
11764 : : point to the class in push_template_decl or grokfndecl. */
11765 : 15066 : unsigned ix = 0;
11766 : 15066 : for (tree decls = CLASSTYPE_DECL_LIST (TREE_CHAIN (decl));
11767 : 412953 : decls; decls = TREE_CHAIN (decls))
11768 : 412953 : if (!TREE_PURPOSE (decls))
11769 : : {
11770 : 59541 : tree frnd = friend_from_decl_list (TREE_VALUE (decls));
11771 : 59541 : if (frnd == decl)
11772 : : break;
11773 : 44475 : ix++;
11774 : : }
11775 : 15066 : key.index = ix;
11776 : 15066 : name = NULL_TREE;
11777 : : }
11778 : 15066 : break;
11779 : :
11780 : 10707 : case MK_local_type:
11781 : 10707 : key_local_type (key, STRIP_TEMPLATE (decl), container);
11782 : 10707 : break;
11783 : :
11784 : 1015 : case MK_enum:
11785 : 1015 : {
11786 : : /* Anonymous enums are located by their first identifier,
11787 : : and underlying type. */
11788 : 1015 : tree type = TREE_TYPE (decl);
11789 : :
11790 : 1015 : gcc_checking_assert (UNSCOPED_ENUM_P (type));
11791 : : /* Using the type name drops the bit precision we might
11792 : : have been using on the enum. */
11793 : 1015 : key.ret = TYPE_NAME (ENUM_UNDERLYING_TYPE (type));
11794 : 1015 : if (tree values = TYPE_VALUES (type))
11795 : 1015 : name = DECL_NAME (TREE_VALUE (values));
11796 : : }
11797 : : break;
11798 : :
11799 : 484 : case MK_keyed:
11800 : 484 : {
11801 : 484 : tree scope = get_keyed_decl_scope (inner);
11802 : 484 : gcc_checking_assert (scope);
11803 : :
11804 : 484 : auto *root = keyed_table->get (scope);
11805 : 484 : unsigned ix = root->length ();
11806 : : /* If we don't find it, we'll write a really big number
11807 : : that the reader will ignore. */
11808 : 515 : while (ix--)
11809 : 515 : if ((*root)[ix] == inner)
11810 : : break;
11811 : :
11812 : : /* Use the keyed-to decl as the 'name'. */
11813 : 484 : name = scope;
11814 : 484 : key.index = ix;
11815 : : }
11816 : 484 : break;
11817 : :
11818 : 43053 : case MK_partial:
11819 : 43053 : {
11820 : 43053 : tree ti = get_template_info (inner);
11821 : 43053 : key.constraints = get_constraints (inner);
11822 : 43053 : key.ret = TI_TEMPLATE (ti);
11823 : 43053 : key.args = TI_ARGS (ti);
11824 : : }
11825 : 43053 : break;
11826 : : }
11827 : :
11828 : 1651891 : tree_node (name);
11829 : 1651891 : if (streaming_p ())
11830 : : {
11831 : 586897 : unsigned code = (key.ref_q << 0) | (key.index << 2);
11832 : 586897 : u (code);
11833 : : }
11834 : :
11835 : 1651891 : if (mk == MK_enum)
11836 : 1015 : tree_node (key.ret);
11837 : 1650876 : else if (mk == MK_partial
11838 : 1607823 : || (mk == MK_named && inner
11839 : 1390492 : && TREE_CODE (inner) == FUNCTION_DECL))
11840 : : {
11841 : 772396 : tree_node (key.ret);
11842 : 772396 : tree arg = key.args;
11843 : 772396 : if (mk == MK_named)
11844 : 2090748 : while (arg && arg != void_list_node)
11845 : : {
11846 : 1361405 : tree_node (TREE_VALUE (arg));
11847 : 1361405 : arg = TREE_CHAIN (arg);
11848 : : }
11849 : 772396 : tree_node (arg);
11850 : 772396 : tree_node (key.constraints);
11851 : : }
11852 : : }
11853 : 2920249 : }
11854 : :
11855 : : /* DECL is a new declaration that may be duplicated in OVL. Use RET &
11856 : : ARGS to find its clone, or NULL. If DECL's DECL_NAME is NULL, this
11857 : : has been found by a proxy. It will be an enum type located by its
11858 : : first member.
11859 : :
11860 : : We're conservative with matches, so ambiguous decls will be
11861 : : registered as different, then lead to a lookup error if the two
11862 : : modules are both visible. Perhaps we want to do something similar
11863 : : to duplicate decls to get ODR errors on loading? We already have
11864 : : some special casing for namespaces. */
11865 : :
11866 : : static tree
11867 : 265361 : check_mergeable_decl (merge_kind mk, tree decl, tree ovl, merge_key const &key)
11868 : : {
11869 : 265361 : tree found = NULL_TREE;
11870 : 1095080 : for (ovl_iterator iter (ovl); !found && iter; ++iter)
11871 : : {
11872 : 530608 : tree match = *iter;
11873 : :
11874 : 530608 : tree d_inner = decl;
11875 : 530608 : tree m_inner = match;
11876 : :
11877 : 614703 : again:
11878 : 614703 : if (TREE_CODE (d_inner) != TREE_CODE (m_inner))
11879 : : {
11880 : 81961 : if (TREE_CODE (match) == NAMESPACE_DECL
11881 : 81961 : && !DECL_NAMESPACE_ALIAS (match))
11882 : : /* Namespaces are never overloaded. */
11883 : : found = match;
11884 : :
11885 : 81961 : continue;
11886 : : }
11887 : :
11888 : 532742 : switch (TREE_CODE (d_inner))
11889 : : {
11890 : 176839 : case TEMPLATE_DECL:
11891 : 176839 : if (template_heads_equivalent_p (d_inner, m_inner))
11892 : : {
11893 : 84095 : d_inner = DECL_TEMPLATE_RESULT (d_inner);
11894 : 84095 : m_inner = DECL_TEMPLATE_RESULT (m_inner);
11895 : 84095 : if (d_inner == error_mark_node
11896 : 84095 : && TYPE_DECL_ALIAS_P (m_inner))
11897 : : {
11898 : : found = match;
11899 : : break;
11900 : : }
11901 : 84095 : goto again;
11902 : : }
11903 : : break;
11904 : :
11905 : 258661 : case FUNCTION_DECL:
11906 : 258661 : if (tree m_type = TREE_TYPE (m_inner))
11907 : 258661 : if ((!key.ret
11908 : 133100 : || same_type_p (key.ret, fndecl_declared_return_type (m_inner)))
11909 : 241438 : && type_memfn_rqual (m_type) == key.ref_q
11910 : 241182 : && compparms (key.args, TYPE_ARG_TYPES (m_type))
11911 : : /* Reject if old is a "C" builtin and new is not "C".
11912 : : Matches decls_match behaviour. */
11913 : 124387 : && (!DECL_IS_UNDECLARED_BUILTIN (m_inner)
11914 : 5225 : || !DECL_EXTERN_C_P (m_inner)
11915 : 5097 : || DECL_EXTERN_C_P (d_inner))
11916 : : /* Reject if one is a different member of a
11917 : : guarded/pre/post fn set. */
11918 : 124382 : && (!flag_contracts
11919 : 352 : || (DECL_IS_PRE_FN_P (d_inner)
11920 : 176 : == DECL_IS_PRE_FN_P (m_inner)))
11921 : 383043 : && (!flag_contracts
11922 : 352 : || (DECL_IS_POST_FN_P (d_inner)
11923 : 176 : == DECL_IS_POST_FN_P (m_inner))))
11924 : : {
11925 : 124382 : tree m_reqs = get_constraints (m_inner);
11926 : 124382 : if (m_reqs)
11927 : : {
11928 : 9074 : if (cxx_dialect < cxx20)
11929 : 8 : m_reqs = CI_ASSOCIATED_CONSTRAINTS (m_reqs);
11930 : : else
11931 : 18140 : m_reqs = CI_DECLARATOR_REQS (m_reqs);
11932 : : }
11933 : :
11934 : 124382 : if (cp_tree_equal (key.constraints, m_reqs))
11935 : 162418 : found = match;
11936 : : }
11937 : : break;
11938 : :
11939 : 59149 : case TYPE_DECL:
11940 : 118298 : if (DECL_IMPLICIT_TYPEDEF_P (d_inner)
11941 : 59149 : == DECL_IMPLICIT_TYPEDEF_P (m_inner))
11942 : : {
11943 : 59123 : if (!IDENTIFIER_ANON_P (DECL_NAME (m_inner)))
11944 : 58995 : return match;
11945 : 128 : else if (mk == MK_enum
11946 : 128 : && (TYPE_NAME (ENUM_UNDERLYING_TYPE (TREE_TYPE (m_inner)))
11947 : 128 : == key.ret))
11948 : : found = match;
11949 : : }
11950 : : break;
11951 : :
11952 : : default:
11953 : : found = match;
11954 : : break;
11955 : : }
11956 : : }
11957 : :
11958 : 206366 : return found;
11959 : : }
11960 : :
11961 : : /* DECL, INNER & TYPE are a skeleton set of nodes for a decl. Only
11962 : : the bools have been filled in. Read its merging key and merge it.
11963 : : Returns the existing decl if there is one. */
11964 : :
11965 : : tree
11966 : 866974 : trees_in::key_mergeable (int tag, merge_kind mk, tree decl, tree inner,
11967 : : tree type, tree container, bool is_attached,
11968 : : bool is_imported_temploid_friend)
11969 : : {
11970 : 866974 : const char *kind = "new";
11971 : 866974 : tree existing = NULL_TREE;
11972 : :
11973 : 866974 : if (mk & MK_template_mask)
11974 : : {
11975 : : // FIXME: We could stream the specialization hash?
11976 : 260722 : spec_entry spec;
11977 : 260722 : spec.tmpl = tree_node ();
11978 : 260722 : spec.args = tree_node ();
11979 : :
11980 : 260722 : if (get_overrun ())
11981 : 0 : return error_mark_node;
11982 : :
11983 : 260722 : DECL_NAME (decl) = DECL_NAME (spec.tmpl);
11984 : 260722 : DECL_CONTEXT (decl) = DECL_CONTEXT (spec.tmpl);
11985 : 260722 : DECL_NAME (inner) = DECL_NAME (decl);
11986 : 260722 : DECL_CONTEXT (inner) = DECL_CONTEXT (decl);
11987 : :
11988 : 260722 : tree constr = NULL_TREE;
11989 : 260722 : bool is_decl = mk & MK_tmpl_decl_mask;
11990 : 260722 : if (is_decl)
11991 : : {
11992 : 184037 : if (flag_concepts && TREE_CODE (inner) == VAR_DECL)
11993 : : {
11994 : 3968 : constr = tree_node ();
11995 : 3968 : if (constr)
11996 : 0 : set_constraints (inner, constr);
11997 : : }
11998 : 366366 : spec.spec = (mk & MK_tmpl_tmpl_mask) ? inner : decl;
11999 : : }
12000 : : else
12001 : 76685 : spec.spec = type;
12002 : 260722 : existing = match_mergeable_specialization (is_decl, &spec);
12003 : 260722 : if (constr)
12004 : : /* We'll add these back later, if this is the new decl. */
12005 : 0 : remove_constraints (inner);
12006 : :
12007 : 260722 : if (!existing)
12008 : : ; /* We'll add to the table once read. */
12009 : 143350 : else if (mk & MK_tmpl_decl_mask)
12010 : : {
12011 : : /* A declaration specialization. */
12012 : 98511 : if (mk & MK_tmpl_tmpl_mask)
12013 : 1025 : existing = DECL_TI_TEMPLATE (existing);
12014 : : }
12015 : : else
12016 : : {
12017 : : /* A type specialization. */
12018 : 44839 : if (mk & MK_tmpl_tmpl_mask)
12019 : 234 : existing = CLASSTYPE_TI_TEMPLATE (existing);
12020 : : else
12021 : 44605 : existing = TYPE_NAME (existing);
12022 : : }
12023 : : }
12024 : 606252 : else if (mk == MK_unique)
12025 : : kind = "unique";
12026 : : else
12027 : : {
12028 : 463395 : tree name = tree_node ();
12029 : :
12030 : 463395 : merge_key key;
12031 : 463395 : unsigned code = u ();
12032 : 463395 : key.ref_q = cp_ref_qualifier ((code >> 0) & 3);
12033 : 463395 : key.index = code >> 2;
12034 : :
12035 : 463395 : if (mk == MK_enum)
12036 : 221 : key.ret = tree_node ();
12037 : 463174 : else if (mk == MK_partial
12038 : 453640 : || ((mk == MK_named || mk == MK_friend_spec)
12039 : 383486 : && TREE_CODE (inner) == FUNCTION_DECL))
12040 : : {
12041 : 215612 : key.ret = tree_node ();
12042 : 215612 : tree arg, *arg_ptr = &key.args;
12043 : 215612 : while ((arg = tree_node ())
12044 : 603041 : && arg != void_list_node
12045 : 1002554 : && mk != MK_partial)
12046 : : {
12047 : 388704 : *arg_ptr = tree_cons (NULL_TREE, arg, NULL_TREE);
12048 : 388704 : arg_ptr = &TREE_CHAIN (*arg_ptr);
12049 : : }
12050 : 215612 : *arg_ptr = arg;
12051 : 215612 : key.constraints = tree_node ();
12052 : : }
12053 : :
12054 : 463395 : if (get_overrun ())
12055 : 0 : return error_mark_node;
12056 : :
12057 : 463395 : if (mk < MK_indirect_lwm)
12058 : : {
12059 : 455037 : DECL_NAME (decl) = name;
12060 : 455037 : DECL_CONTEXT (decl) = FROB_CONTEXT (container);
12061 : : }
12062 : 463395 : DECL_NAME (inner) = DECL_NAME (decl);
12063 : 463395 : DECL_CONTEXT (inner) = DECL_CONTEXT (decl);
12064 : :
12065 : 463395 : if (mk == MK_partial)
12066 : : {
12067 : 9534 : for (tree spec = DECL_TEMPLATE_SPECIALIZATIONS (key.ret);
12068 : 47272 : spec; spec = TREE_CHAIN (spec))
12069 : : {
12070 : 43788 : tree tmpl = TREE_VALUE (spec);
12071 : 43788 : tree ti = get_template_info (tmpl);
12072 : 43788 : if (template_args_equal (key.args, TI_ARGS (ti))
12073 : 50489 : && cp_tree_equal (key.constraints,
12074 : : get_constraints
12075 : 6701 : (DECL_TEMPLATE_RESULT (tmpl))))
12076 : : {
12077 : : existing = tmpl;
12078 : : break;
12079 : : }
12080 : : }
12081 : : }
12082 : 453861 : else if (mk == MK_keyed
12083 : 195 : && DECL_LANG_SPECIFIC (name)
12084 : 454056 : && DECL_MODULE_KEYED_DECLS_P (name))
12085 : : {
12086 : 195 : gcc_checking_assert (TREE_CODE (container) == NAMESPACE_DECL
12087 : : || TREE_CODE (container) == TYPE_DECL);
12088 : 195 : if (auto *set = keyed_table->get (name))
12089 : 463522 : if (key.index < set->length ())
12090 : : {
12091 : 127 : existing = (*set)[key.index];
12092 : 127 : if (existing)
12093 : : {
12094 : 127 : gcc_checking_assert
12095 : : (DECL_IMPLICIT_TYPEDEF_P (existing));
12096 : 127 : if (inner != decl)
12097 : 84 : existing
12098 : 84 : = CLASSTYPE_TI_TEMPLATE (TREE_TYPE (existing));
12099 : : }
12100 : : }
12101 : : }
12102 : : else
12103 : 453666 : switch (TREE_CODE (container))
12104 : : {
12105 : 0 : default:
12106 : 0 : gcc_unreachable ();
12107 : :
12108 : 117589 : case NAMESPACE_DECL:
12109 : 117589 : if (is_attached
12110 : 117589 : && !is_imported_temploid_friend
12111 : 117589 : && !(state->is_module () || state->is_partition ()))
12112 : : kind = "unique";
12113 : : else
12114 : : {
12115 : 115668 : gcc_checking_assert (mk == MK_named || mk == MK_enum);
12116 : 115668 : tree mvec;
12117 : 115668 : tree *vslot = mergeable_namespace_slots (container, name,
12118 : : is_attached, &mvec);
12119 : 115668 : existing = check_mergeable_decl (mk, decl, *vslot, key);
12120 : 115668 : if (!existing)
12121 : 43481 : add_mergeable_namespace_entity (vslot, decl);
12122 : : else
12123 : : {
12124 : : /* Note that we now have duplicates to deal with in
12125 : : name lookup. */
12126 : 72187 : if (is_attached)
12127 : 36 : BINDING_VECTOR_PARTITION_DUPS_P (mvec) = true;
12128 : : else
12129 : 72151 : BINDING_VECTOR_GLOBAL_DUPS_P (mvec) = true;
12130 : : }
12131 : : }
12132 : : break;
12133 : :
12134 : 3194 : case FUNCTION_DECL:
12135 : 3194 : gcc_checking_assert (mk == MK_local_type);
12136 : 3194 : existing = key_local_type (key, container, name);
12137 : 3194 : if (existing && inner != decl)
12138 : 3572 : existing = TYPE_TI_TEMPLATE (TREE_TYPE (existing));
12139 : : break;
12140 : :
12141 : 332883 : case TYPE_DECL:
12142 : 332883 : gcc_checking_assert (!is_imported_temploid_friend);
12143 : 4467 : if (is_attached && !(state->is_module () || state->is_partition ())
12144 : : /* Implicit member functions can come from
12145 : : anywhere. */
12146 : 336421 : && !(DECL_ARTIFICIAL (decl)
12147 : 2083 : && TREE_CODE (decl) == FUNCTION_DECL
12148 : 664 : && !DECL_THUNK_P (decl)))
12149 : : kind = "unique";
12150 : : else
12151 : : {
12152 : 330009 : tree ctx = TREE_TYPE (container);
12153 : :
12154 : : /* For some reason templated enumeral types are not marked
12155 : : as COMPLETE_TYPE_P, even though they have members.
12156 : : This may well be a bug elsewhere. */
12157 : 330009 : if (TREE_CODE (ctx) == ENUMERAL_TYPE)
12158 : 11757 : existing = find_enum_member (ctx, name);
12159 : 318252 : else if (COMPLETE_TYPE_P (ctx))
12160 : : {
12161 : 188263 : switch (mk)
12162 : : {
12163 : 0 : default:
12164 : 0 : gcc_unreachable ();
12165 : :
12166 : 150090 : case MK_named:
12167 : 150090 : existing = lookup_class_binding (ctx, name);
12168 : 150090 : if (existing)
12169 : : {
12170 : 149693 : tree inner = decl;
12171 : 149693 : if (TREE_CODE (inner) == TEMPLATE_DECL
12172 : 149693 : && !DECL_MEMBER_TEMPLATE_P (inner))
12173 : 67154 : inner = DECL_TEMPLATE_RESULT (inner);
12174 : :
12175 : 149693 : existing = check_mergeable_decl
12176 : 149693 : (mk, inner, existing, key);
12177 : :
12178 : 149693 : if (!existing && DECL_ALIAS_TEMPLATE_P (decl))
12179 : : {} // FIXME: Insert into specialization
12180 : : // tables, we'll need the arguments for that!
12181 : : }
12182 : : break;
12183 : :
12184 : 26327 : case MK_field:
12185 : 26327 : {
12186 : 26327 : unsigned ix = key.index;
12187 : 26327 : for (tree field = TYPE_FIELDS (ctx);
12188 : 889740 : field; field = DECL_CHAIN (field))
12189 : : {
12190 : 889740 : tree finner = STRIP_TEMPLATE (field);
12191 : 889740 : if (TREE_CODE (finner) == TREE_CODE (inner))
12192 : 298701 : if (!ix--)
12193 : : {
12194 : : existing = field;
12195 : : break;
12196 : : }
12197 : : }
12198 : : }
12199 : : break;
12200 : :
12201 : 1318 : case MK_vtable:
12202 : 1318 : {
12203 : 1318 : unsigned ix = key.index;
12204 : 1318 : for (tree vtable = CLASSTYPE_VTABLES (ctx);
12205 : 1647 : vtable; vtable = DECL_CHAIN (vtable))
12206 : 1508 : if (!ix--)
12207 : : {
12208 : : existing = vtable;
12209 : : break;
12210 : : }
12211 : : }
12212 : : break;
12213 : :
12214 : 7571 : case MK_as_base:
12215 : 7571 : {
12216 : 7571 : tree as_base = CLASSTYPE_AS_BASE (ctx);
12217 : 7571 : if (as_base && as_base != ctx)
12218 : 7571 : existing = TYPE_NAME (as_base);
12219 : : }
12220 : : break;
12221 : :
12222 : 2957 : case MK_local_friend:
12223 : 2957 : {
12224 : 2957 : unsigned ix = key.index;
12225 : 2957 : for (tree decls = CLASSTYPE_DECL_LIST (ctx);
12226 : 82420 : decls; decls = TREE_CHAIN (decls))
12227 : 82420 : if (!TREE_PURPOSE (decls) && !ix--)
12228 : : {
12229 : 2957 : existing
12230 : 2957 : = friend_from_decl_list (TREE_VALUE (decls));
12231 : 2957 : break;
12232 : : }
12233 : : }
12234 : : break;
12235 : : }
12236 : :
12237 : 188263 : if (existing && mk < MK_indirect_lwm && mk != MK_partial
12238 : 184303 : && TREE_CODE (decl) == TEMPLATE_DECL
12239 : 273746 : && !DECL_MEMBER_TEMPLATE_P (decl))
12240 : : {
12241 : 69190 : tree ti;
12242 : 69190 : if (DECL_IMPLICIT_TYPEDEF_P (existing))
12243 : 875 : ti = TYPE_TEMPLATE_INFO (TREE_TYPE (existing));
12244 : : else
12245 : 68315 : ti = DECL_TEMPLATE_INFO (existing);
12246 : 69190 : existing = TI_TEMPLATE (ti);
12247 : : }
12248 : : }
12249 : : }
12250 : : }
12251 : : }
12252 : :
12253 : 866974 : dump (dumper::MERGE)
12254 : 2401 : && dump ("Read:%d's %s merge key (%s) %C:%N", tag, merge_kind_name[mk],
12255 : 2401 : existing ? "matched" : kind, TREE_CODE (decl), decl);
12256 : :
12257 : : return existing;
12258 : : }
12259 : :
12260 : : void
12261 : 241716 : trees_out::binfo_mergeable (tree binfo)
12262 : : {
12263 : 241716 : tree dom = binfo;
12264 : 308368 : while (tree parent = BINFO_INHERITANCE_CHAIN (dom))
12265 : : dom = parent;
12266 : 241716 : tree type = BINFO_TYPE (dom);
12267 : 241716 : gcc_checking_assert (TYPE_BINFO (type) == dom);
12268 : 241716 : tree_node (type);
12269 : 241716 : if (streaming_p ())
12270 : : {
12271 : : unsigned ix = 0;
12272 : 133081 : for (; dom != binfo; dom = TREE_CHAIN (dom))
12273 : 34498 : ix++;
12274 : 98583 : u (ix);
12275 : : }
12276 : 241716 : }
12277 : :
12278 : : unsigned
12279 : 72427 : trees_in::binfo_mergeable (tree *type)
12280 : : {
12281 : 72427 : *type = tree_node ();
12282 : 72427 : return u ();
12283 : : }
12284 : :
12285 : : /* DECL is a just streamed declaration with attributes DATTR that should
12286 : : have matching ABI tags as EXISTING's attributes EATTR. Check that the
12287 : : ABI tags match, and report an error if not. */
12288 : :
12289 : : void
12290 : 282868 : trees_in::check_abi_tags (tree existing, tree decl, tree &eattr, tree &dattr)
12291 : : {
12292 : 282868 : tree etags = lookup_attribute ("abi_tag", eattr);
12293 : 282868 : tree dtags = lookup_attribute ("abi_tag", dattr);
12294 : 282868 : if ((etags == nullptr) != (dtags == nullptr)
12295 : 282868 : || (etags && !attribute_value_equal (etags, dtags)))
12296 : : {
12297 : 30 : if (etags)
12298 : 21 : etags = TREE_VALUE (etags);
12299 : 30 : if (dtags)
12300 : 24 : dtags = TREE_VALUE (dtags);
12301 : :
12302 : : /* We only error if mangling wouldn't consider the tags equivalent. */
12303 : 30 : if (!equal_abi_tags (etags, dtags))
12304 : : {
12305 : 21 : auto_diagnostic_group d;
12306 : 21 : if (dtags)
12307 : 15 : error_at (DECL_SOURCE_LOCATION (decl),
12308 : : "mismatching abi tags for %qD with tags %qE",
12309 : : decl, dtags);
12310 : : else
12311 : 6 : error_at (DECL_SOURCE_LOCATION (decl),
12312 : : "mismatching abi tags for %qD with no tags", decl);
12313 : 21 : if (etags)
12314 : 15 : inform (DECL_SOURCE_LOCATION (existing),
12315 : : "existing declaration here with tags %qE", etags);
12316 : : else
12317 : 6 : inform (DECL_SOURCE_LOCATION (existing),
12318 : : "existing declaration here with no tags");
12319 : 21 : }
12320 : :
12321 : : /* Always use the existing abi_tags as the canonical set so that
12322 : : later processing doesn't get confused. */
12323 : 30 : if (dtags)
12324 : 24 : dattr = remove_attribute ("abi_tag", dattr);
12325 : 30 : if (etags)
12326 : 21 : duplicate_one_attribute (&dattr, eattr, "abi_tag");
12327 : : }
12328 : 282868 : }
12329 : :
12330 : : /* DECL is a just streamed mergeable decl that should match EXISTING. Check
12331 : : it does and issue an appropriate diagnostic if not. Merge any
12332 : : bits from DECL to EXISTING. This is stricter matching than
12333 : : decls_match, because we can rely on ODR-sameness, and we cannot use
12334 : : decls_match because it can cause instantiations of constraints. */
12335 : :
12336 : : bool
12337 : 417605 : trees_in::is_matching_decl (tree existing, tree decl, bool is_typedef)
12338 : : {
12339 : : // FIXME: We should probably do some duplicate decl-like stuff here
12340 : : // (beware, default parms should be the same?) Can we just call
12341 : : // duplicate_decls and teach it how to handle the module-specific
12342 : : // permitted/required duplications?
12343 : :
12344 : : // We know at this point that the decls have matched by key, so we
12345 : : // can elide some of the checking
12346 : 417605 : gcc_checking_assert (TREE_CODE (existing) == TREE_CODE (decl));
12347 : :
12348 : 417605 : tree d_inner = decl;
12349 : 417605 : tree e_inner = existing;
12350 : 417605 : if (TREE_CODE (decl) == TEMPLATE_DECL)
12351 : : {
12352 : 141133 : d_inner = DECL_TEMPLATE_RESULT (d_inner);
12353 : 141133 : e_inner = DECL_TEMPLATE_RESULT (e_inner);
12354 : 141133 : gcc_checking_assert (TREE_CODE (e_inner) == TREE_CODE (d_inner));
12355 : : }
12356 : :
12357 : : // FIXME: do more precise errors at point of mismatch
12358 : 417605 : const char *mismatch_msg = nullptr;
12359 : :
12360 : 417605 : if (VAR_OR_FUNCTION_DECL_P (d_inner)
12361 : 417605 : && DECL_EXTERN_C_P (d_inner) != DECL_EXTERN_C_P (e_inner))
12362 : : {
12363 : 6 : mismatch_msg = G_("conflicting language linkage for imported "
12364 : : "declaration %#qD");
12365 : 6 : goto mismatch;
12366 : : }
12367 : 417599 : else if (TREE_CODE (d_inner) == FUNCTION_DECL)
12368 : : {
12369 : 188461 : tree e_ret = fndecl_declared_return_type (existing);
12370 : 188461 : tree d_ret = fndecl_declared_return_type (decl);
12371 : :
12372 : 82491 : if (decl != d_inner && DECL_NAME (d_inner) == fun_identifier
12373 : 188485 : && LAMBDA_TYPE_P (DECL_CONTEXT (d_inner)))
12374 : : /* This has a recursive type that will compare different. */;
12375 : 188449 : else if (!same_type_p (d_ret, e_ret))
12376 : : {
12377 : 6 : mismatch_msg = G_("conflicting type for imported declaration %#qD");
12378 : 6 : goto mismatch;
12379 : : }
12380 : :
12381 : 188455 : tree e_type = TREE_TYPE (e_inner);
12382 : 188455 : tree d_type = TREE_TYPE (d_inner);
12383 : :
12384 : 188455 : for (tree e_args = TYPE_ARG_TYPES (e_type),
12385 : 188455 : d_args = TYPE_ARG_TYPES (d_type);
12386 : 322492 : e_args != d_args && (e_args || d_args);
12387 : 134037 : e_args = TREE_CHAIN (e_args), d_args = TREE_CHAIN (d_args))
12388 : : {
12389 : 134037 : if (!(e_args && d_args))
12390 : : {
12391 : 0 : mismatch_msg = G_("conflicting argument list for imported "
12392 : : "declaration %#qD");
12393 : 0 : goto mismatch;
12394 : : }
12395 : :
12396 : 134037 : if (!same_type_p (TREE_VALUE (d_args), TREE_VALUE (e_args)))
12397 : : {
12398 : 0 : mismatch_msg = G_("conflicting argument types for imported "
12399 : : "declaration %#qD");
12400 : 0 : goto mismatch;
12401 : : }
12402 : : }
12403 : :
12404 : : /* If EXISTING has an undeduced or uninstantiated exception
12405 : : specification, but DECL does not, propagate the exception
12406 : : specification. Otherwise we end up asserting or trying to
12407 : : instantiate it in the middle of loading. */
12408 : 188455 : tree e_spec = TYPE_RAISES_EXCEPTIONS (e_type);
12409 : 188455 : tree d_spec = TYPE_RAISES_EXCEPTIONS (d_type);
12410 : 277350 : if (DECL_MAYBE_DELETED (e_inner) || DEFERRED_NOEXCEPT_SPEC_P (e_spec))
12411 : : {
12412 : 15205 : if (!DEFERRED_NOEXCEPT_SPEC_P (d_spec)
12413 : 45247 : || (UNEVALUATED_NOEXCEPT_SPEC_P (e_spec)
12414 : 12251 : && !UNEVALUATED_NOEXCEPT_SPEC_P (d_spec)))
12415 : : {
12416 : 259 : dump (dumper::MERGE)
12417 : 3 : && dump ("Propagating instantiated noexcept to %N", existing);
12418 : 259 : TREE_TYPE (existing) = d_type;
12419 : :
12420 : : /* Propagate to existing clones. */
12421 : 259 : tree clone;
12422 : 533 : FOR_EACH_CLONE (clone, existing)
12423 : : {
12424 : 274 : if (TREE_TYPE (clone) == e_type)
12425 : 274 : TREE_TYPE (clone) = d_type;
12426 : : else
12427 : 0 : TREE_TYPE (clone)
12428 : 0 : = build_exception_variant (TREE_TYPE (clone), d_spec);
12429 : : }
12430 : : }
12431 : : }
12432 : 173200 : else if (!DECL_MAYBE_DELETED (d_inner)
12433 : 247672 : && !DEFERRED_NOEXCEPT_SPEC_P (d_spec)
12434 : 346399 : && !comp_except_specs (d_spec, e_spec, ce_type))
12435 : : {
12436 : 764 : mismatch_msg = G_("conflicting %<noexcept%> specifier for "
12437 : : "imported declaration %#qD");
12438 : 764 : goto mismatch;
12439 : : }
12440 : :
12441 : : /* Similarly if EXISTING has an undeduced return type, but DECL's
12442 : : is already deduced. */
12443 : 187691 : if (undeduced_auto_decl (existing) && !undeduced_auto_decl (decl))
12444 : : {
12445 : 13 : dump (dumper::MERGE)
12446 : 0 : && dump ("Propagating deduced return type to %N", existing);
12447 : 13 : gcc_checking_assert (existing == e_inner);
12448 : 13 : FNDECL_USED_AUTO (existing) = true;
12449 : 13 : DECL_SAVED_AUTO_RETURN_TYPE (existing) = TREE_TYPE (e_type);
12450 : 13 : TREE_TYPE (existing) = change_return_type (TREE_TYPE (d_type), e_type);
12451 : : }
12452 : 187678 : else if (type_uses_auto (d_ret)
12453 : 187678 : && !same_type_p (TREE_TYPE (d_type), TREE_TYPE (e_type)))
12454 : : {
12455 : 3 : mismatch_msg = G_("conflicting deduced return type for "
12456 : : "imported declaration %#qD");
12457 : 3 : goto mismatch;
12458 : : }
12459 : :
12460 : : /* Similarly if EXISTING has undeduced constexpr, but DECL's
12461 : : is already deduced. */
12462 : 187688 : if (DECL_DECLARED_CONSTEXPR_P (e_inner)
12463 : 187688 : == DECL_DECLARED_CONSTEXPR_P (d_inner))
12464 : : /* Already matches. */;
12465 : 2 : else if (DECL_DECLARED_CONSTEXPR_P (d_inner)
12466 : 2 : && (DECL_MAYBE_DELETED (e_inner)
12467 : 0 : || decl_implicit_constexpr_p (d_inner)))
12468 : : /* DECL was deduced, copy to EXISTING. */
12469 : : {
12470 : 1 : DECL_DECLARED_CONSTEXPR_P (e_inner) = true;
12471 : 1 : if (decl_implicit_constexpr_p (d_inner))
12472 : 0 : DECL_LANG_SPECIFIC (e_inner)->u.fn.implicit_constexpr = true;
12473 : : }
12474 : 1 : else if (DECL_DECLARED_CONSTEXPR_P (e_inner)
12475 : 1 : && (DECL_MAYBE_DELETED (d_inner)
12476 : 0 : || decl_implicit_constexpr_p (e_inner)))
12477 : : /* EXISTING was deduced, leave it alone. */;
12478 : : else
12479 : : {
12480 : 0 : mismatch_msg = G_("conflicting %<constexpr%> for imported "
12481 : : "declaration %#qD");
12482 : 0 : goto mismatch;
12483 : : }
12484 : :
12485 : : /* Don't synthesize a defaulted function if we're importing one
12486 : : we've already determined. */
12487 : 187688 : if (!DECL_MAYBE_DELETED (d_inner))
12488 : 187578 : DECL_MAYBE_DELETED (e_inner) = false;
12489 : : }
12490 : 229138 : else if (is_typedef)
12491 : : {
12492 : 81906 : if (!DECL_ORIGINAL_TYPE (e_inner)
12493 : 81906 : || !same_type_p (DECL_ORIGINAL_TYPE (d_inner),
12494 : : DECL_ORIGINAL_TYPE (e_inner)))
12495 : : {
12496 : 3 : mismatch_msg = G_("conflicting imported declaration %q#D");
12497 : 3 : goto mismatch;
12498 : : }
12499 : : }
12500 : : /* Using cp_tree_equal because we can meet TYPE_ARGUMENT_PACKs
12501 : : here. I suspect the entities that directly do that are things
12502 : : that shouldn't go to duplicate_decls (FIELD_DECLs etc). */
12503 : 147232 : else if (!cp_tree_equal (TREE_TYPE (decl), TREE_TYPE (existing)))
12504 : : {
12505 : : mismatch_msg = G_("conflicting type for imported declaration %#qD");
12506 : 1457 : mismatch:
12507 : 1457 : if (DECL_IS_UNDECLARED_BUILTIN (existing))
12508 : : /* Just like duplicate_decls, presum the user knows what
12509 : : they're doing in overriding a builtin. */
12510 : 767 : TREE_TYPE (existing) = TREE_TYPE (decl);
12511 : 690 : else if (decl_function_context (decl))
12512 : : /* The type of a mergeable local entity (such as a function scope
12513 : : capturing lambda's closure type fields) can depend on an
12514 : : unmergeable local entity (such as a local variable), so type
12515 : : equality isn't feasible in general for local entities. */;
12516 : : else
12517 : : {
12518 : 21 : gcc_checking_assert (mismatch_msg);
12519 : 21 : auto_diagnostic_group d;
12520 : 21 : error_at (DECL_SOURCE_LOCATION (decl), mismatch_msg, decl);
12521 : 21 : inform (DECL_SOURCE_LOCATION (existing),
12522 : : "existing declaration %#qD", existing);
12523 : 21 : return false;
12524 : 21 : }
12525 : : }
12526 : :
12527 : 417584 : if (DECL_IS_UNDECLARED_BUILTIN (existing)
12528 : 417584 : && !DECL_IS_UNDECLARED_BUILTIN (decl))
12529 : : {
12530 : : /* We're matching a builtin that the user has yet to declare.
12531 : : We are the one! This is very much duplicate-decl
12532 : : shenanigans. */
12533 : 949 : DECL_SOURCE_LOCATION (existing) = DECL_SOURCE_LOCATION (decl);
12534 : 949 : if (TREE_CODE (decl) != TYPE_DECL)
12535 : : {
12536 : : /* Propagate exceptions etc. */
12537 : 938 : TREE_TYPE (existing) = TREE_TYPE (decl);
12538 : 938 : TREE_NOTHROW (existing) = TREE_NOTHROW (decl);
12539 : : }
12540 : : /* This is actually an import! */
12541 : 949 : DECL_MODULE_IMPORT_P (existing) = true;
12542 : :
12543 : : /* Yay, sliced! */
12544 : 949 : existing->base = decl->base;
12545 : :
12546 : 949 : if (TREE_CODE (decl) == FUNCTION_DECL)
12547 : : {
12548 : : /* Ew :( */
12549 : 938 : memcpy (&existing->decl_common.size,
12550 : : &decl->decl_common.size,
12551 : : (offsetof (tree_decl_common, pt_uid)
12552 : : - offsetof (tree_decl_common, size)));
12553 : 938 : auto bltin_class = DECL_BUILT_IN_CLASS (decl);
12554 : 938 : existing->function_decl.built_in_class = bltin_class;
12555 : 938 : auto fncode = DECL_UNCHECKED_FUNCTION_CODE (decl);
12556 : 938 : DECL_UNCHECKED_FUNCTION_CODE (existing) = fncode;
12557 : 938 : if (existing->function_decl.built_in_class == BUILT_IN_NORMAL)
12558 : : {
12559 : 818 : if (builtin_decl_explicit_p (built_in_function (fncode)))
12560 : 818 : switch (fncode)
12561 : : {
12562 : 0 : case BUILT_IN_STPCPY:
12563 : 0 : set_builtin_decl_implicit_p
12564 : 0 : (built_in_function (fncode), true);
12565 : 0 : break;
12566 : 818 : default:
12567 : 818 : set_builtin_decl_declared_p
12568 : 818 : (built_in_function (fncode), true);
12569 : 818 : break;
12570 : : }
12571 : 818 : copy_attributes_to_builtin (decl);
12572 : : }
12573 : : }
12574 : : }
12575 : :
12576 : 417584 : if (VAR_OR_FUNCTION_DECL_P (decl)
12577 : 417584 : && DECL_TEMPLATE_INSTANTIATED (decl))
12578 : : /* Don't instantiate again! */
12579 : 8209 : DECL_TEMPLATE_INSTANTIATED (existing) = true;
12580 : :
12581 : 417584 : if (TREE_CODE (d_inner) == FUNCTION_DECL
12582 : 417584 : && DECL_DECLARED_INLINE_P (d_inner))
12583 : : {
12584 : 153275 : DECL_DECLARED_INLINE_P (e_inner) = true;
12585 : 153275 : if (!DECL_SAVED_TREE (e_inner)
12586 : 74983 : && lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (d_inner))
12587 : 153281 : && !lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (e_inner)))
12588 : : {
12589 : 18 : DECL_INTERFACE_KNOWN (e_inner)
12590 : 6 : |= DECL_INTERFACE_KNOWN (d_inner);
12591 : 6 : DECL_DISREGARD_INLINE_LIMITS (e_inner)
12592 : 6 : |= DECL_DISREGARD_INLINE_LIMITS (d_inner);
12593 : : // TODO: we will eventually want to merge all decl attributes
12594 : 6 : duplicate_one_attribute (&DECL_ATTRIBUTES (e_inner),
12595 : 6 : DECL_ATTRIBUTES (d_inner), "gnu_inline");
12596 : : }
12597 : : }
12598 : 417584 : if (!DECL_EXTERNAL (d_inner))
12599 : 197897 : DECL_EXTERNAL (e_inner) = false;
12600 : :
12601 : 417584 : if (VAR_OR_FUNCTION_DECL_P (d_inner))
12602 : 415676 : check_abi_tags (existing, decl,
12603 : 207838 : DECL_ATTRIBUTES (e_inner), DECL_ATTRIBUTES (d_inner));
12604 : :
12605 : 417584 : if (TREE_CODE (decl) == TEMPLATE_DECL)
12606 : : {
12607 : : /* Merge default template arguments. */
12608 : 141131 : tree d_parms = DECL_INNERMOST_TEMPLATE_PARMS (decl);
12609 : 141131 : tree e_parms = DECL_INNERMOST_TEMPLATE_PARMS (existing);
12610 : 141131 : gcc_checking_assert (TREE_VEC_LENGTH (d_parms)
12611 : : == TREE_VEC_LENGTH (e_parms));
12612 : 405555 : for (int i = 0; i < TREE_VEC_LENGTH (d_parms); ++i)
12613 : : {
12614 : 264433 : tree d_default = TREE_PURPOSE (TREE_VEC_ELT (d_parms, i));
12615 : 264433 : tree& e_default = TREE_PURPOSE (TREE_VEC_ELT (e_parms, i));
12616 : 264433 : if (e_default == NULL_TREE)
12617 : 224318 : e_default = d_default;
12618 : 40115 : else if (d_default != NULL_TREE
12619 : 40115 : && !cp_tree_equal (d_default, e_default))
12620 : : {
12621 : 9 : auto_diagnostic_group d;
12622 : 9 : tree d_parm = TREE_VALUE (TREE_VEC_ELT (d_parms, i));
12623 : 9 : tree e_parm = TREE_VALUE (TREE_VEC_ELT (e_parms, i));
12624 : 9 : error_at (DECL_SOURCE_LOCATION (d_parm),
12625 : : "conflicting default argument for %#qD", d_parm);
12626 : 9 : inform (DECL_SOURCE_LOCATION (e_parm),
12627 : : "existing default declared here");
12628 : 9 : return false;
12629 : 9 : }
12630 : : }
12631 : : }
12632 : :
12633 : 417575 : if (TREE_CODE (d_inner) == FUNCTION_DECL)
12634 : : {
12635 : : /* Merge default function arguments. */
12636 : 188452 : tree d_parm = FUNCTION_FIRST_USER_PARMTYPE (d_inner);
12637 : 188452 : tree e_parm = FUNCTION_FIRST_USER_PARMTYPE (e_inner);
12638 : 188452 : int i = 0;
12639 : 447876 : for (; d_parm && d_parm != void_list_node;
12640 : 259424 : d_parm = TREE_CHAIN (d_parm), e_parm = TREE_CHAIN (e_parm), ++i)
12641 : : {
12642 : 259436 : tree d_default = TREE_PURPOSE (d_parm);
12643 : 259436 : tree& e_default = TREE_PURPOSE (e_parm);
12644 : 259436 : if (e_default == NULL_TREE)
12645 : 244712 : e_default = d_default;
12646 : 14724 : else if (d_default != NULL_TREE
12647 : 14724 : && !cp_tree_equal (d_default, e_default))
12648 : : {
12649 : 12 : auto_diagnostic_group d;
12650 : 12 : error_at (get_fndecl_argument_location (d_inner, i),
12651 : : "conflicting default argument for parameter %P of %#qD",
12652 : : i, decl);
12653 : 12 : inform (get_fndecl_argument_location (e_inner, i),
12654 : : "existing default declared here");
12655 : 12 : return false;
12656 : 12 : }
12657 : : }
12658 : : }
12659 : :
12660 : : return true;
12661 : : }
12662 : :
12663 : : /* FN is an implicit member function that we've discovered is new to
12664 : : the class. Add it to the TYPE_FIELDS chain and the method vector.
12665 : : Reset the appropriate classtype lazy flag. */
12666 : :
12667 : : bool
12668 : 864 : trees_in::install_implicit_member (tree fn)
12669 : : {
12670 : 864 : tree ctx = DECL_CONTEXT (fn);
12671 : 864 : tree name = DECL_NAME (fn);
12672 : : /* We know these are synthesized, so the set of expected prototypes
12673 : : is quite restricted. We're not validating correctness, just
12674 : : distinguishing beteeen the small set of possibilities. */
12675 : 864 : tree parm_type = TREE_VALUE (FUNCTION_FIRST_USER_PARMTYPE (fn));
12676 : 864 : if (IDENTIFIER_CTOR_P (name))
12677 : : {
12678 : 584 : if (CLASSTYPE_LAZY_DEFAULT_CTOR (ctx)
12679 : 584 : && VOID_TYPE_P (parm_type))
12680 : 148 : CLASSTYPE_LAZY_DEFAULT_CTOR (ctx) = false;
12681 : 436 : else if (!TYPE_REF_P (parm_type))
12682 : : return false;
12683 : 436 : else if (CLASSTYPE_LAZY_COPY_CTOR (ctx)
12684 : 436 : && !TYPE_REF_IS_RVALUE (parm_type))
12685 : 221 : CLASSTYPE_LAZY_COPY_CTOR (ctx) = false;
12686 : 215 : else if (CLASSTYPE_LAZY_MOVE_CTOR (ctx))
12687 : 215 : CLASSTYPE_LAZY_MOVE_CTOR (ctx) = false;
12688 : : else
12689 : : return false;
12690 : : }
12691 : 280 : else if (IDENTIFIER_DTOR_P (name))
12692 : : {
12693 : 218 : if (CLASSTYPE_LAZY_DESTRUCTOR (ctx))
12694 : 218 : CLASSTYPE_LAZY_DESTRUCTOR (ctx) = false;
12695 : : else
12696 : : return false;
12697 : 218 : if (DECL_VIRTUAL_P (fn))
12698 : : /* A virtual dtor should have been created when the class
12699 : : became complete. */
12700 : : return false;
12701 : : }
12702 : 62 : else if (name == assign_op_identifier)
12703 : : {
12704 : 62 : if (!TYPE_REF_P (parm_type))
12705 : : return false;
12706 : 62 : else if (CLASSTYPE_LAZY_COPY_ASSIGN (ctx)
12707 : 62 : && !TYPE_REF_IS_RVALUE (parm_type))
12708 : 31 : CLASSTYPE_LAZY_COPY_ASSIGN (ctx) = false;
12709 : 31 : else if (CLASSTYPE_LAZY_MOVE_ASSIGN (ctx))
12710 : 31 : CLASSTYPE_LAZY_MOVE_ASSIGN (ctx) = false;
12711 : : else
12712 : : return false;
12713 : : }
12714 : : else
12715 : : return false;
12716 : :
12717 : 894 : dump (dumper::MERGE) && dump ("Adding implicit member %N", fn);
12718 : :
12719 : 864 : DECL_CHAIN (fn) = TYPE_FIELDS (ctx);
12720 : 864 : TYPE_FIELDS (ctx) = fn;
12721 : :
12722 : 864 : add_method (ctx, fn, false);
12723 : :
12724 : : /* Propagate TYPE_FIELDS. */
12725 : 864 : fixup_type_variants (ctx);
12726 : :
12727 : 864 : return true;
12728 : : }
12729 : :
12730 : : /* Return non-zero if DECL has a definition that would be interesting to
12731 : : write out. */
12732 : :
12733 : : static bool
12734 : 1285526 : has_definition (tree decl)
12735 : : {
12736 : 1285526 : bool is_tmpl = TREE_CODE (decl) == TEMPLATE_DECL;
12737 : 1285526 : if (is_tmpl)
12738 : 289404 : decl = DECL_TEMPLATE_RESULT (decl);
12739 : :
12740 : 1285526 : switch (TREE_CODE (decl))
12741 : : {
12742 : : default:
12743 : : break;
12744 : :
12745 : 445225 : case FUNCTION_DECL:
12746 : 445225 : if (!DECL_SAVED_TREE (decl))
12747 : : /* Not defined. */
12748 : : break;
12749 : :
12750 : 193572 : if (DECL_DECLARED_INLINE_P (decl))
12751 : : return true;
12752 : :
12753 : 11337 : if (header_module_p ())
12754 : : /* We always need to write definitions in header modules,
12755 : : since there's no TU to emit them in otherwise. */
12756 : : return true;
12757 : :
12758 : 3445 : if (DECL_TEMPLATE_INFO (decl))
12759 : : {
12760 : 2754 : int use_tpl = DECL_USE_TEMPLATE (decl);
12761 : :
12762 : : // FIXME: Partial specializations have definitions too.
12763 : 2754 : if (use_tpl < 2)
12764 : : return true;
12765 : : }
12766 : : break;
12767 : :
12768 : 550794 : case TYPE_DECL:
12769 : 550794 : {
12770 : 550794 : tree type = TREE_TYPE (decl);
12771 : 550794 : if (type == TYPE_MAIN_VARIANT (type)
12772 : 255491 : && decl == TYPE_NAME (type)
12773 : 806285 : && (TREE_CODE (type) == ENUMERAL_TYPE
12774 : 255491 : ? TYPE_VALUES (type) : TYPE_FIELDS (type)))
12775 : : return true;
12776 : : }
12777 : : break;
12778 : :
12779 : 75367 : case VAR_DECL:
12780 : : /* DECL_INITIALIZED_P might not be set on a dependent VAR_DECL. */
12781 : 75367 : if (DECL_LANG_SPECIFIC (decl)
12782 : 74698 : && DECL_TEMPLATE_INFO (decl)
12783 : 116890 : && DECL_INITIAL (decl))
12784 : : return true;
12785 : : else
12786 : : {
12787 : 37248 : if (!DECL_INITIALIZED_P (decl))
12788 : : /* Not defined. */
12789 : : return false;
12790 : :
12791 : 32056 : if (header_module_p ())
12792 : : /* We always need to write definitions in header modules,
12793 : : since there's no TU to emit them in otherwise. */
12794 : : return true;
12795 : :
12796 : 10010 : if (decl_maybe_constant_var_p (decl))
12797 : : /* We might need its constant value. */
12798 : : return true;
12799 : :
12800 : 360 : if (vague_linkage_p (decl))
12801 : : /* These are emitted as needed. */
12802 : : return true;
12803 : :
12804 : : return false;
12805 : : }
12806 : 5844 : break;
12807 : :
12808 : 5844 : case CONCEPT_DECL:
12809 : 5844 : if (DECL_INITIAL (decl))
12810 : : return true;
12811 : :
12812 : : break;
12813 : : }
12814 : :
12815 : : return false;
12816 : : }
12817 : :
12818 : : uintptr_t *
12819 : 489293 : trees_in::find_duplicate (tree existing)
12820 : : {
12821 : 244173 : if (!duplicates)
12822 : : return NULL;
12823 : :
12824 : 347036 : return duplicates->get (existing);
12825 : : }
12826 : :
12827 : : /* We're starting to read a duplicate DECL. EXISTING is the already
12828 : : known node. */
12829 : :
12830 : : void
12831 : 599001 : trees_in::register_duplicate (tree decl, tree existing)
12832 : : {
12833 : 599001 : if (!duplicates)
12834 : 95061 : duplicates = new duplicate_hash_map (40);
12835 : :
12836 : 599001 : bool existed;
12837 : 599001 : uintptr_t &slot = duplicates->get_or_insert (existing, &existed);
12838 : 599001 : gcc_checking_assert (!existed);
12839 : 599001 : slot = reinterpret_cast<uintptr_t> (decl);
12840 : :
12841 : 599001 : if (TREE_CODE (decl) == TEMPLATE_DECL)
12842 : : /* Also register the DECL_TEMPLATE_RESULT as a duplicate so
12843 : : that passing decl's _RESULT to maybe_duplicate naturally
12844 : : gives us existing's _RESULT back. */
12845 : 282266 : register_duplicate (DECL_TEMPLATE_RESULT (decl),
12846 : 141133 : DECL_TEMPLATE_RESULT (existing));
12847 : 599001 : }
12848 : :
12849 : : /* We've read a definition of MAYBE_EXISTING. If not a duplicate,
12850 : : return MAYBE_EXISTING (into which the definition should be
12851 : : installed). Otherwise return NULL if already known bad, or the
12852 : : duplicate we read (for ODR checking, or extracting additional merge
12853 : : information). */
12854 : :
12855 : : tree
12856 : 245120 : trees_in::odr_duplicate (tree maybe_existing, bool has_defn)
12857 : : {
12858 : 245120 : tree res = NULL_TREE;
12859 : :
12860 : 393419 : if (uintptr_t *dup = find_duplicate (maybe_existing))
12861 : : {
12862 : 140988 : if (!(*dup & 1))
12863 : 140985 : res = reinterpret_cast<tree> (*dup);
12864 : : }
12865 : : else
12866 : : res = maybe_existing;
12867 : :
12868 : 245120 : assert_definition (maybe_existing, res && !has_defn);
12869 : :
12870 : : // FIXME: We probably need to return the template, so that the
12871 : : // template header can be checked?
12872 : 245120 : return res ? STRIP_TEMPLATE (res) : NULL_TREE;
12873 : : }
12874 : :
12875 : : /* The following writer functions rely on the current behaviour of
12876 : : depset::hash::add_dependency making the decl and defn depset nodes
12877 : : depend on eachother. That way we don't have to worry about seeding
12878 : : the tree map with named decls that cannot be looked up by name (I.e
12879 : : template and function parms). We know the decl and definition will
12880 : : be in the same cluster, which is what we want. */
12881 : :
12882 : : void
12883 : 362445 : trees_out::write_function_def (tree decl)
12884 : : {
12885 : 362445 : tree_node (DECL_RESULT (decl));
12886 : :
12887 : 362445 : {
12888 : : /* The function body for a non-inline function or function template
12889 : : is ignored for determining exposures. This should only matter
12890 : : for templates (we don't emit the bodies of non-inline functions
12891 : : to begin with). */
12892 : 362445 : auto ovr = make_temp_override (dep_hash->ignore_tu_local,
12893 : 362445 : !DECL_DECLARED_INLINE_P (decl));
12894 : 362445 : tree_node (DECL_INITIAL (decl));
12895 : 362445 : tree_node (DECL_SAVED_TREE (decl));
12896 : 362445 : }
12897 : :
12898 : 771250 : tree_node (DECL_FRIEND_CONTEXT (decl));
12899 : :
12900 : 362445 : constexpr_fundef *cexpr = retrieve_constexpr_fundef (decl);
12901 : :
12902 : 362445 : if (streaming_p ())
12903 : 181184 : u (cexpr != nullptr);
12904 : 362445 : if (cexpr)
12905 : : {
12906 : 88112 : chained_decls (cexpr->parms);
12907 : 88112 : tree_node (cexpr->result);
12908 : 88112 : tree_node (cexpr->body);
12909 : : }
12910 : :
12911 : 362445 : function* f = DECL_STRUCT_FUNCTION (decl);
12912 : :
12913 : 362445 : if (streaming_p ())
12914 : : {
12915 : 181184 : unsigned flags = 0;
12916 : :
12917 : : /* Whether the importer should emit this definition, if used. */
12918 : 181184 : flags |= 1 * (DECL_NOT_REALLY_EXTERN (decl)
12919 : 181184 : && (get_importer_interface (decl)
12920 : : != importer_interface::external));
12921 : :
12922 : : /* Make sure DECL_REALLY_EXTERN and DECL_INTERFACE_KNOWN are consistent
12923 : : on non-templates or we'll crash later in import_export_decl. */
12924 : 117399 : gcc_checking_assert (flags || DECL_INTERFACE_KNOWN (decl)
12925 : : || (DECL_LANG_SPECIFIC (decl)
12926 : : && DECL_TEMPLATE_INFO (decl)
12927 : : && uses_template_parms (DECL_TI_ARGS (decl))));
12928 : :
12929 : 181184 : if (f)
12930 : : {
12931 : 180560 : flags |= 2;
12932 : : /* These flags are needed in tsubst_lambda_expr. */
12933 : 180560 : flags |= 4 * f->language->returns_value;
12934 : 180560 : flags |= 8 * f->language->returns_null;
12935 : 180560 : flags |= 16 * f->language->returns_abnormally;
12936 : 180560 : flags |= 32 * f->language->infinite_loop;
12937 : : }
12938 : :
12939 : 181184 : u (flags);
12940 : : }
12941 : :
12942 : 362445 : if (state && f)
12943 : : {
12944 : 361197 : state->write_location (*this, f->function_start_locus);
12945 : 361197 : state->write_location (*this, f->function_end_locus);
12946 : : }
12947 : 362445 : }
12948 : :
12949 : : void
12950 : 0 : trees_out::mark_function_def (tree)
12951 : : {
12952 : 0 : }
12953 : :
12954 : : bool
12955 : 154946 : trees_in::read_function_def (tree decl, tree maybe_template)
12956 : : {
12957 : 155391 : dump () && dump ("Reading function definition %N", decl);
12958 : 154946 : tree result = tree_node ();
12959 : 154946 : tree initial = tree_node ();
12960 : 154946 : tree saved = tree_node ();
12961 : 154946 : tree context = tree_node ();
12962 : 154946 : constexpr_fundef cexpr;
12963 : 154946 : post_process_data pdata {};
12964 : 154946 : pdata.decl = maybe_template;
12965 : :
12966 : 154946 : tree maybe_dup = odr_duplicate (maybe_template, DECL_SAVED_TREE (decl));
12967 : 309889 : bool installing = maybe_dup && !DECL_SAVED_TREE (decl);
12968 : :
12969 : 154946 : if (u ())
12970 : : {
12971 : 34924 : cexpr.parms = chained_decls ();
12972 : 34924 : cexpr.result = tree_node ();
12973 : 34924 : cexpr.body = tree_node ();
12974 : 34924 : cexpr.decl = decl;
12975 : : }
12976 : : else
12977 : 120022 : cexpr.decl = NULL_TREE;
12978 : :
12979 : 154946 : unsigned flags = u ();
12980 : :
12981 : 154946 : if (flags & 2)
12982 : : {
12983 : 154390 : pdata.start_locus = state->read_location (*this);
12984 : 154390 : pdata.end_locus = state->read_location (*this);
12985 : 154390 : pdata.returns_value = flags & 4;
12986 : 154390 : pdata.returns_null = flags & 8;
12987 : 154390 : pdata.returns_abnormally = flags & 16;
12988 : 154390 : pdata.infinite_loop = flags & 32;
12989 : : }
12990 : :
12991 : 154946 : if (get_overrun ())
12992 : : return NULL_TREE;
12993 : :
12994 : 154946 : if (installing)
12995 : : {
12996 : 71993 : DECL_NOT_REALLY_EXTERN (decl) = flags & 1;
12997 : 71993 : DECL_RESULT (decl) = result;
12998 : 71993 : DECL_INITIAL (decl) = initial;
12999 : 71993 : DECL_SAVED_TREE (decl) = saved;
13000 : :
13001 : 71993 : if (context)
13002 : 3211 : SET_DECL_FRIEND_CONTEXT (decl, context);
13003 : 71993 : if (cexpr.decl)
13004 : 21813 : register_constexpr_fundef (cexpr);
13005 : 71993 : post_process (pdata);
13006 : : }
13007 : : else if (maybe_dup)
13008 : : {
13009 : : // FIXME:QOI Check matching defn
13010 : : }
13011 : :
13012 : : return true;
13013 : : }
13014 : :
13015 : : /* Also for CONCEPT_DECLs. */
13016 : :
13017 : : void
13018 : 107896 : trees_out::write_var_def (tree decl)
13019 : : {
13020 : : /* The initializer of a non-inline variable or variable template is
13021 : : ignored for determining exposures. */
13022 : 107896 : auto ovr = make_temp_override (dep_hash->ignore_tu_local,
13023 : 107896 : VAR_P (decl) && !DECL_INLINE_VAR_P (decl));
13024 : :
13025 : 107896 : tree init = DECL_INITIAL (decl);
13026 : 107896 : tree_node (init);
13027 : 107896 : if (!init)
13028 : : {
13029 : 1222 : tree dyn_init = NULL_TREE;
13030 : :
13031 : : /* We only need to write initializers in header modules. */
13032 : 2266 : if (header_module_p () && DECL_NONTRIVIALLY_INITIALIZED_P (decl))
13033 : : {
13034 : 450 : dyn_init = value_member (decl,
13035 : 450 : CP_DECL_THREAD_LOCAL_P (decl)
13036 : : ? tls_aggregates : static_aggregates);
13037 : 450 : gcc_checking_assert (dyn_init);
13038 : : /* Mark it so write_inits knows this is needed. */
13039 : 450 : TREE_LANG_FLAG_0 (dyn_init) = true;
13040 : 450 : dyn_init = TREE_PURPOSE (dyn_init);
13041 : : }
13042 : 1222 : tree_node (dyn_init);
13043 : : }
13044 : 107896 : }
13045 : :
13046 : : void
13047 : 0 : trees_out::mark_var_def (tree)
13048 : : {
13049 : 0 : }
13050 : :
13051 : : bool
13052 : 35459 : trees_in::read_var_def (tree decl, tree maybe_template)
13053 : : {
13054 : : /* Do not mark the virtual table entries as used. */
13055 : 35459 : bool vtable = VAR_P (decl) && DECL_VTABLE_OR_VTT_P (decl);
13056 : 35459 : unused += vtable;
13057 : 35459 : tree init = tree_node ();
13058 : 35459 : tree dyn_init = init ? NULL_TREE : tree_node ();
13059 : 35459 : unused -= vtable;
13060 : :
13061 : 35459 : if (get_overrun ())
13062 : : return false;
13063 : :
13064 : 35459 : bool initialized = (VAR_P (decl) ? bool (DECL_INITIALIZED_P (decl))
13065 : 35459 : : bool (DECL_INITIAL (decl)));
13066 : 35459 : tree maybe_dup = odr_duplicate (maybe_template, initialized);
13067 : 35459 : bool installing = maybe_dup && !initialized;
13068 : 35459 : if (installing)
13069 : : {
13070 : 19523 : DECL_INITIAL (decl) = init;
13071 : 19523 : if (DECL_EXTERNAL (decl))
13072 : 2412 : DECL_NOT_REALLY_EXTERN (decl) = true;
13073 : 19523 : if (VAR_P (decl))
13074 : : {
13075 : 17396 : DECL_INITIALIZED_P (decl) = true;
13076 : 17396 : if (maybe_dup && DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (maybe_dup))
13077 : 17051 : DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
13078 : 17396 : tentative_decl_linkage (decl);
13079 : 17396 : if (DECL_EXPLICIT_INSTANTIATION (decl)
13080 : 17396 : && !DECL_EXTERNAL (decl))
13081 : 9 : setup_explicit_instantiation_definition_linkage (decl);
13082 : : /* Class non-template static members are handled in read_class_def.
13083 : : But still handle specialisations of member templates. */
13084 : 34792 : if ((!DECL_CLASS_SCOPE_P (decl)
13085 : 10840 : || primary_template_specialization_p (decl))
13086 : 24065 : && (DECL_IMPLICIT_INSTANTIATION (decl)
13087 : 6610 : || (DECL_EXPLICIT_INSTANTIATION (decl)
13088 : 18 : && !DECL_EXTERNAL (decl))))
13089 : 68 : note_vague_linkage_variable (decl);
13090 : : }
13091 : 19523 : if (!dyn_init)
13092 : : ;
13093 : 216 : else if (CP_DECL_THREAD_LOCAL_P (decl))
13094 : 96 : tls_aggregates = tree_cons (dyn_init, decl, tls_aggregates);
13095 : : else
13096 : 120 : static_aggregates = tree_cons (dyn_init, decl, static_aggregates);
13097 : : }
13098 : : else if (maybe_dup)
13099 : : {
13100 : : // FIXME:QOI Check matching defn
13101 : : }
13102 : :
13103 : : return true;
13104 : : }
13105 : :
13106 : : /* If MEMBER doesn't have an independent life outside the class,
13107 : : return it (or its TEMPLATE_DECL). Otherwise NULL. */
13108 : :
13109 : : static tree
13110 : 184122 : member_owned_by_class (tree member)
13111 : : {
13112 : 184122 : gcc_assert (DECL_P (member));
13113 : :
13114 : : /* Clones are owned by their origin. */
13115 : 184122 : if (DECL_CLONED_FUNCTION_P (member))
13116 : : return NULL;
13117 : :
13118 : 184122 : if (TREE_CODE (member) == FIELD_DECL)
13119 : : /* FIELD_DECLS can have template info in some cases. We always
13120 : : want the FIELD_DECL though, as there's never a TEMPLATE_DECL
13121 : : wrapping them. */
13122 : : return member;
13123 : :
13124 : 86327 : int use_tpl = -1;
13125 : 86327 : if (tree ti = node_template_info (member, use_tpl))
13126 : : {
13127 : : // FIXME: Don't bail on things that CANNOT have their own
13128 : : // template header. No, make sure they're in the same cluster.
13129 : 0 : if (use_tpl > 0)
13130 : : return NULL_TREE;
13131 : :
13132 : 0 : if (DECL_TEMPLATE_RESULT (TI_TEMPLATE (ti)) == member)
13133 : 184122 : member = TI_TEMPLATE (ti);
13134 : : }
13135 : : return member;
13136 : : }
13137 : :
13138 : : void
13139 : 142112 : trees_out::write_class_def (tree defn)
13140 : : {
13141 : 142112 : gcc_assert (DECL_P (defn));
13142 : 142112 : if (streaming_p ())
13143 : 71341 : dump () && dump ("Writing class definition %N", defn);
13144 : :
13145 : 142112 : tree type = TREE_TYPE (defn);
13146 : 142112 : tree_node (TYPE_SIZE (type));
13147 : 142112 : tree_node (TYPE_SIZE_UNIT (type));
13148 : 142112 : tree_node (TYPE_VFIELD (type));
13149 : 142112 : tree_node (TYPE_BINFO (type));
13150 : :
13151 : 142112 : vec_chained_decls (TYPE_FIELDS (type));
13152 : :
13153 : : /* Every class but __as_base has a type-specific. */
13154 : 282010 : gcc_checking_assert (!TYPE_LANG_SPECIFIC (type) == IS_FAKE_BASE_TYPE (type));
13155 : :
13156 : 142112 : if (TYPE_LANG_SPECIFIC (type))
13157 : : {
13158 : 139898 : {
13159 : 139898 : vec<tree, va_gc> *v = CLASSTYPE_MEMBER_VEC (type);
13160 : 139898 : if (!v)
13161 : : {
13162 : 37578 : gcc_checking_assert (!streaming_p ());
13163 : : /* Force a class vector. */
13164 : 37578 : v = set_class_bindings (type, -1);
13165 : 37578 : gcc_checking_assert (v);
13166 : : }
13167 : :
13168 : 139898 : unsigned len = v->length ();
13169 : 139898 : if (streaming_p ())
13170 : 69929 : u (len);
13171 : 1098946 : for (unsigned ix = 0; ix != len; ix++)
13172 : : {
13173 : 959048 : tree m = (*v)[ix];
13174 : 959048 : if (TREE_CODE (m) == TYPE_DECL
13175 : 292403 : && DECL_ARTIFICIAL (m)
13176 : 1109733 : && TYPE_STUB_DECL (TREE_TYPE (m)) == m)
13177 : : /* This is a using-decl for a type, or an anonymous
13178 : : struct (maybe with a typedef name). Write the type. */
13179 : 10020 : m = TREE_TYPE (m);
13180 : 959048 : tree_node (m);
13181 : : }
13182 : : }
13183 : 139898 : tree_node (CLASSTYPE_LAMBDA_EXPR (type));
13184 : :
13185 : : /* TYPE_CONTAINS_VPTR_P looks at the vbase vector, which the
13186 : : reader won't know at this point. */
13187 : 139898 : int has_vptr = TYPE_CONTAINS_VPTR_P (type);
13188 : :
13189 : 139898 : if (streaming_p ())
13190 : : {
13191 : 69929 : unsigned nvbases = vec_safe_length (CLASSTYPE_VBASECLASSES (type));
13192 : 69929 : u (nvbases);
13193 : 69929 : i (has_vptr);
13194 : : }
13195 : :
13196 : 139898 : if (has_vptr)
13197 : : {
13198 : 4790 : tree_vec (CLASSTYPE_PURE_VIRTUALS (type));
13199 : 4790 : tree_pair_vec (CLASSTYPE_VCALL_INDICES (type));
13200 : 4790 : tree_node (CLASSTYPE_KEY_METHOD (type));
13201 : : }
13202 : : }
13203 : :
13204 : 142112 : if (TYPE_LANG_SPECIFIC (type))
13205 : : {
13206 : 139898 : tree_node (CLASSTYPE_PRIMARY_BINFO (type));
13207 : :
13208 : 139898 : tree as_base = CLASSTYPE_AS_BASE (type);
13209 : 139898 : if (as_base)
13210 : 71755 : as_base = TYPE_NAME (as_base);
13211 : 139898 : tree_node (as_base);
13212 : :
13213 : : /* Write the vtables. */
13214 : 139898 : tree vtables = CLASSTYPE_VTABLES (type);
13215 : 139898 : vec_chained_decls (vtables);
13216 : 285352 : for (; vtables; vtables = TREE_CHAIN (vtables))
13217 : 5556 : write_definition (vtables);
13218 : :
13219 : 139898 : {
13220 : : /* Friend declarations in class definitions are ignored when
13221 : : determining exposures. */
13222 : 139898 : auto ovr = make_temp_override (dep_hash->ignore_tu_local, true);
13223 : :
13224 : : /* Write the friend classes. */
13225 : 139898 : tree_list (CLASSTYPE_FRIEND_CLASSES (type), false);
13226 : :
13227 : : /* Write the friend functions. */
13228 : 139898 : for (tree friends = DECL_FRIENDLIST (defn);
13229 : 158542 : friends; friends = TREE_CHAIN (friends))
13230 : : {
13231 : 18644 : tree_node (FRIEND_NAME (friends));
13232 : 18644 : tree_list (FRIEND_DECLS (friends), false);
13233 : : }
13234 : : /* End of friend fns. */
13235 : 139898 : tree_node (NULL_TREE);
13236 : 139898 : }
13237 : :
13238 : : /* Write the decl list. We don't need to ignore exposures of friend
13239 : : decls here as any such decls should already have been added and
13240 : : ignored above. */
13241 : 139898 : tree_list (CLASSTYPE_DECL_LIST (type), true);
13242 : :
13243 : 139898 : if (TYPE_CONTAINS_VPTR_P (type))
13244 : : {
13245 : : /* Write the thunks. */
13246 : 4790 : for (tree decls = TYPE_FIELDS (type);
13247 : 134764 : decls; decls = DECL_CHAIN (decls))
13248 : 129974 : if (TREE_CODE (decls) == FUNCTION_DECL
13249 : 94320 : && DECL_VIRTUAL_P (decls)
13250 : 154614 : && DECL_THUNKS (decls))
13251 : : {
13252 : 804 : tree_node (decls);
13253 : : /* Thunks are always unique, so chaining is ok. */
13254 : 804 : chained_decls (DECL_THUNKS (decls));
13255 : : }
13256 : 4790 : tree_node (NULL_TREE);
13257 : : }
13258 : : }
13259 : 142112 : }
13260 : :
13261 : : void
13262 : 184122 : trees_out::mark_class_member (tree member, bool do_defn)
13263 : : {
13264 : 184122 : gcc_assert (DECL_P (member));
13265 : :
13266 : 184122 : member = member_owned_by_class (member);
13267 : 184122 : if (member)
13268 : 368244 : mark_declaration (member, do_defn && has_definition (member));
13269 : 184122 : }
13270 : :
13271 : : void
13272 : 142140 : trees_out::mark_class_def (tree defn)
13273 : : {
13274 : 142140 : gcc_assert (DECL_P (defn));
13275 : 142140 : tree type = TREE_TYPE (defn);
13276 : : /* Mark the class members that are not type-decls and cannot have
13277 : : independent definitions. */
13278 : 1478388 : for (tree member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
13279 : 1336248 : if (TREE_CODE (member) == FIELD_DECL
13280 : 1336248 : || TREE_CODE (member) == USING_DECL
13281 : : /* A cloned enum-decl from 'using enum unrelated;' */
13282 : 1336248 : || (TREE_CODE (member) == CONST_DECL
13283 : 13504 : && DECL_CONTEXT (member) == type))
13284 : : {
13285 : 184122 : mark_class_member (member);
13286 : 184122 : if (TREE_CODE (member) == FIELD_DECL)
13287 : 97795 : if (tree repr = DECL_BIT_FIELD_REPRESENTATIVE (member))
13288 : : /* If we're marking a class template definition, then
13289 : : this'll contain the width (as set by grokbitfield)
13290 : : instead of a decl. */
13291 : 2050 : if (DECL_P (repr))
13292 : 1652 : mark_declaration (repr, false);
13293 : : }
13294 : :
13295 : : /* Mark the binfo hierarchy. */
13296 : 336964 : for (tree child = TYPE_BINFO (type); child; child = TREE_CHAIN (child))
13297 : 194824 : mark_by_value (child);
13298 : :
13299 : 142140 : if (TYPE_LANG_SPECIFIC (type))
13300 : : {
13301 : 139902 : for (tree vtable = CLASSTYPE_VTABLES (type);
13302 : 145458 : vtable; vtable = TREE_CHAIN (vtable))
13303 : 5556 : mark_declaration (vtable, true);
13304 : :
13305 : 139902 : if (TYPE_CONTAINS_VPTR_P (type))
13306 : : /* Mark the thunks, they belong to the class definition,
13307 : : /not/ the thunked-to function. */
13308 : 4790 : for (tree decls = TYPE_FIELDS (type);
13309 : 134764 : decls; decls = DECL_CHAIN (decls))
13310 : 129974 : if (TREE_CODE (decls) == FUNCTION_DECL)
13311 : 94320 : for (tree thunks = DECL_THUNKS (decls);
13312 : 95384 : thunks; thunks = DECL_CHAIN (thunks))
13313 : 1064 : mark_declaration (thunks, false);
13314 : : }
13315 : 142140 : }
13316 : :
13317 : : /* Nop sorting, needed for resorting the member vec. */
13318 : :
13319 : : static void
13320 : 5758078 : nop (void *, void *, void *)
13321 : : {
13322 : 5758078 : }
13323 : :
13324 : : bool
13325 : 52302 : trees_in::read_class_def (tree defn, tree maybe_template)
13326 : : {
13327 : 52302 : gcc_assert (DECL_P (defn));
13328 : 52839 : dump () && dump ("Reading class definition %N", defn);
13329 : 52302 : tree type = TREE_TYPE (defn);
13330 : 52302 : tree size = tree_node ();
13331 : 52302 : tree size_unit = tree_node ();
13332 : 52302 : tree vfield = tree_node ();
13333 : 52302 : tree binfo = tree_node ();
13334 : 52302 : vec<tree, va_gc> *vbase_vec = NULL;
13335 : 52302 : vec<tree, va_gc> *member_vec = NULL;
13336 : 52302 : vec<tree, va_gc> *pure_virts = NULL;
13337 : 52302 : vec<tree_pair_s, va_gc> *vcall_indices = NULL;
13338 : 52302 : tree key_method = NULL_TREE;
13339 : 52302 : tree lambda = NULL_TREE;
13340 : :
13341 : : /* Read the fields. */
13342 : 52302 : vec<tree, va_heap> *fields = vec_chained_decls ();
13343 : :
13344 : 52302 : if (TYPE_LANG_SPECIFIC (type))
13345 : : {
13346 : 51288 : if (unsigned len = u ())
13347 : : {
13348 : 51288 : vec_alloc (member_vec, len);
13349 : 434025 : for (unsigned ix = 0; ix != len; ix++)
13350 : : {
13351 : 382737 : tree m = tree_node ();
13352 : 382737 : if (get_overrun ())
13353 : : break;
13354 : 382737 : if (TYPE_P (m))
13355 : 3683 : m = TYPE_STUB_DECL (m);
13356 : 382737 : member_vec->quick_push (m);
13357 : : }
13358 : : }
13359 : 51288 : lambda = tree_node ();
13360 : :
13361 : 51288 : if (!get_overrun ())
13362 : : {
13363 : 51288 : unsigned nvbases = u ();
13364 : 51288 : if (nvbases)
13365 : : {
13366 : 220 : vec_alloc (vbase_vec, nvbases);
13367 : 987 : for (tree child = binfo; child; child = TREE_CHAIN (child))
13368 : 767 : if (BINFO_VIRTUAL_P (child))
13369 : 220 : vbase_vec->quick_push (child);
13370 : : }
13371 : : }
13372 : :
13373 : 51288 : if (!get_overrun ())
13374 : : {
13375 : 51288 : int has_vptr = i ();
13376 : 51288 : if (has_vptr)
13377 : : {
13378 : 2048 : pure_virts = tree_vec ();
13379 : 2048 : vcall_indices = tree_pair_vec ();
13380 : 2048 : key_method = tree_node ();
13381 : : }
13382 : : }
13383 : : }
13384 : :
13385 : 52302 : tree maybe_dup = odr_duplicate (maybe_template, TYPE_SIZE (type));
13386 : 52302 : bool installing = maybe_dup && !TYPE_SIZE (type);
13387 : 23280 : if (installing)
13388 : : {
13389 : 23280 : if (maybe_dup != defn)
13390 : : {
13391 : : // FIXME: This is needed on other defns too, almost
13392 : : // duplicate-decl like? See is_matching_decl too.
13393 : : /* Copy flags from the duplicate. */
13394 : 234 : tree type_dup = TREE_TYPE (maybe_dup);
13395 : :
13396 : : /* Core pieces. */
13397 : 234 : TYPE_MODE_RAW (type) = TYPE_MODE_RAW (type_dup);
13398 : 234 : TYPE_ALIGN_RAW (type) = TYPE_ALIGN_RAW (type_dup);
13399 : 468 : TYPE_WARN_IF_NOT_ALIGN_RAW (type)
13400 : 234 : = TYPE_WARN_IF_NOT_ALIGN_RAW (type_dup);
13401 : 234 : TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (type_dup);
13402 : :
13403 : 234 : SET_DECL_MODE (defn, DECL_MODE (maybe_dup));
13404 : 234 : DECL_SIZE (defn) = DECL_SIZE (maybe_dup);
13405 : 234 : DECL_SIZE_UNIT (defn) = DECL_SIZE_UNIT (maybe_dup);
13406 : 234 : DECL_ALIGN_RAW (defn) = DECL_ALIGN_RAW (maybe_dup);
13407 : 468 : DECL_WARN_IF_NOT_ALIGN_RAW (defn)
13408 : 234 : = DECL_WARN_IF_NOT_ALIGN_RAW (maybe_dup);
13409 : 234 : DECL_USER_ALIGN (defn) = DECL_USER_ALIGN (maybe_dup);
13410 : :
13411 : 234 : TYPE_TYPELESS_STORAGE (type) = TYPE_TYPELESS_STORAGE (type_dup);
13412 : 234 : TYPE_CXX_ODR_P (type) = TYPE_CXX_ODR_P (type_dup);
13413 : 234 : TYPE_NO_FORCE_BLK (type) = TYPE_NO_FORCE_BLK (type_dup);
13414 : 234 : TYPE_TRANSPARENT_AGGR (type) = TYPE_TRANSPARENT_AGGR (type_dup);
13415 : 468 : TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type)
13416 : 234 : = TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type_dup);
13417 : :
13418 : 234 : TYPE_EMPTY_P (type) = TYPE_EMPTY_P (type_dup);
13419 : 234 : TREE_ADDRESSABLE (type) = TREE_ADDRESSABLE (type_dup);
13420 : :
13421 : : /* C++ pieces. */
13422 : 234 : TYPE_POLYMORPHIC_P (type) = TYPE_POLYMORPHIC_P (type_dup);
13423 : 234 : CLASSTYPE_FINAL (type) = CLASSTYPE_FINAL (type_dup);
13424 : :
13425 : 468 : TYPE_HAS_USER_CONSTRUCTOR (type)
13426 : 234 : = TYPE_HAS_USER_CONSTRUCTOR (type_dup);
13427 : 468 : TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
13428 : 234 : = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type_dup);
13429 : 468 : TYPE_NEEDS_CONSTRUCTING (type)
13430 : 234 : = TYPE_NEEDS_CONSTRUCTING (type_dup);
13431 : :
13432 : 234 : if (auto ls = TYPE_LANG_SPECIFIC (type_dup))
13433 : : {
13434 : 234 : if (TYPE_LANG_SPECIFIC (type))
13435 : : {
13436 : 702 : CLASSTYPE_BEFRIENDING_CLASSES (type_dup)
13437 : 234 : = CLASSTYPE_BEFRIENDING_CLASSES (type);
13438 : 234 : if (!ANON_AGGR_TYPE_P (type))
13439 : 702 : CLASSTYPE_TYPEINFO_VAR (type_dup)
13440 : 234 : = CLASSTYPE_TYPEINFO_VAR (type);
13441 : : }
13442 : 1087 : for (tree v = type; v; v = TYPE_NEXT_VARIANT (v))
13443 : 853 : TYPE_LANG_SPECIFIC (v) = ls;
13444 : : }
13445 : : }
13446 : :
13447 : 23280 : TYPE_SIZE (type) = size;
13448 : 23280 : TYPE_SIZE_UNIT (type) = size_unit;
13449 : :
13450 : 23280 : if (fields)
13451 : : {
13452 : 23280 : tree *chain = &TYPE_FIELDS (type);
13453 : 23280 : unsigned len = fields->length ();
13454 : 259077 : for (unsigned ix = 0; ix != len; ix++)
13455 : : {
13456 : 235797 : tree decl = (*fields)[ix];
13457 : :
13458 : 235797 : if (!decl)
13459 : : {
13460 : : /* An anonymous struct with typedef name. */
13461 : 3 : tree tdef = (*fields)[ix+1];
13462 : 3 : decl = TYPE_STUB_DECL (TREE_TYPE (tdef));
13463 : 3 : gcc_checking_assert (IDENTIFIER_ANON_P (DECL_NAME (decl))
13464 : : && decl != tdef);
13465 : : }
13466 : :
13467 : 427893 : gcc_checking_assert (!*chain == !DECL_CLONED_FUNCTION_P (decl));
13468 : 235797 : *chain = decl;
13469 : 235797 : chain = &DECL_CHAIN (decl);
13470 : :
13471 : 235797 : if (TREE_CODE (decl) == FIELD_DECL
13472 : 235797 : && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
13473 : : {
13474 : 159 : tree anon_type = TYPE_MAIN_VARIANT (TREE_TYPE (decl));
13475 : 159 : if (DECL_NAME (defn) == as_base_identifier)
13476 : : /* ANON_AGGR_TYPE_FIELD should already point to the
13477 : : original FIELD_DECL; don't overwrite it to point
13478 : : to the as-base FIELD_DECL copy. */
13479 : 10 : gcc_checking_assert (ANON_AGGR_TYPE_FIELD (anon_type));
13480 : : else
13481 : 149 : ANON_AGGR_TYPE_FIELD (anon_type) = decl;
13482 : : }
13483 : :
13484 : 235797 : if (TREE_CODE (decl) == USING_DECL
13485 : 235797 : && TREE_CODE (USING_DECL_SCOPE (decl)) == RECORD_TYPE)
13486 : : {
13487 : : /* Reconstruct DECL_ACCESS. */
13488 : 10833 : tree decls = USING_DECL_DECLS (decl);
13489 : 10833 : tree access = declared_access (decl);
13490 : :
13491 : 12378 : for (ovl_iterator iter (decls); iter; ++iter)
13492 : : {
13493 : 985 : tree d = *iter;
13494 : :
13495 : 985 : retrofit_lang_decl (d);
13496 : 985 : tree list = DECL_ACCESS (d);
13497 : :
13498 : 985 : if (!purpose_member (type, list))
13499 : 1159 : DECL_ACCESS (d) = tree_cons (type, access, list);
13500 : : }
13501 : : }
13502 : :
13503 : 235797 : if (TREE_CODE (decl) == VAR_DECL
13504 : 8686 : && TREE_CODE (maybe_template) != TEMPLATE_DECL)
13505 : 7661 : note_vague_linkage_variable (decl);
13506 : : }
13507 : : }
13508 : :
13509 : 23280 : TYPE_VFIELD (type) = vfield;
13510 : 23280 : TYPE_BINFO (type) = binfo;
13511 : :
13512 : 23280 : if (TYPE_LANG_SPECIFIC (type))
13513 : : {
13514 : 22762 : if (!TYPE_POLYMORPHIC_P (type))
13515 : 21758 : SET_CLASSTYPE_LAMBDA_EXPR (type, lambda);
13516 : : else
13517 : 1004 : gcc_checking_assert (lambda == NULL_TREE);
13518 : :
13519 : 22762 : CLASSTYPE_MEMBER_VEC (type) = member_vec;
13520 : 22762 : CLASSTYPE_PURE_VIRTUALS (type) = pure_virts;
13521 : 22762 : CLASSTYPE_VCALL_INDICES (type) = vcall_indices;
13522 : :
13523 : 22762 : if (TYPE_POLYMORPHIC_P (type))
13524 : 1004 : SET_CLASSTYPE_KEY_METHOD (type, key_method);
13525 : : else
13526 : 21758 : gcc_checking_assert (key_method == NULL_TREE);
13527 : :
13528 : 22762 : CLASSTYPE_VBASECLASSES (type) = vbase_vec;
13529 : :
13530 : : /* Resort the member vector. */
13531 : 22762 : resort_type_member_vec (member_vec, NULL, nop, NULL);
13532 : : }
13533 : : }
13534 : : else if (maybe_dup)
13535 : : {
13536 : : // FIXME:QOI Check matching defn
13537 : : }
13538 : :
13539 : 52302 : if (TYPE_LANG_SPECIFIC (type))
13540 : : {
13541 : 51288 : tree primary = tree_node ();
13542 : 51288 : tree as_base = tree_node ();
13543 : :
13544 : 51288 : if (as_base)
13545 : 25701 : as_base = TREE_TYPE (as_base);
13546 : :
13547 : : /* Read the vtables. */
13548 : 51288 : vec<tree, va_heap> *vtables = vec_chained_decls ();
13549 : 51288 : if (vtables)
13550 : : {
13551 : 2016 : unsigned len = vtables->length ();
13552 : 4395 : for (unsigned ix = 0; ix != len; ix++)
13553 : : {
13554 : 2379 : tree vtable = (*vtables)[ix];
13555 : 2379 : read_var_def (vtable, vtable);
13556 : : }
13557 : : }
13558 : :
13559 : 51288 : tree friend_classes = tree_list (false);
13560 : 51288 : tree friend_functions = NULL_TREE;
13561 : 51288 : for (tree *chain = &friend_functions;
13562 : 59900 : tree name = tree_node (); chain = &TREE_CHAIN (*chain))
13563 : : {
13564 : 8612 : tree val = tree_list (false);
13565 : 8612 : *chain = build_tree_list (name, val);
13566 : 8612 : }
13567 : 51288 : tree decl_list = tree_list (true);
13568 : :
13569 : 51288 : if (installing)
13570 : : {
13571 : 22762 : CLASSTYPE_PRIMARY_BINFO (type) = primary;
13572 : 22762 : CLASSTYPE_AS_BASE (type) = as_base;
13573 : :
13574 : 22762 : if (vtables)
13575 : : {
13576 : 1016 : if ((!CLASSTYPE_KEY_METHOD (type)
13577 : : /* Sneaky user may have defined it inline
13578 : : out-of-class. */
13579 : 598 : || DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (type)))
13580 : : /* An imported non-template class attached to a module
13581 : : doesn't need to have its vtables emitted here. */
13582 : 1022 : && (CLASSTYPE_USE_TEMPLATE (type)
13583 : 138 : || !DECL_MODULE_ATTACH_P (defn)))
13584 : 675 : vec_safe_push (keyed_classes, type);
13585 : 1016 : unsigned len = vtables->length ();
13586 : 1016 : tree *chain = &CLASSTYPE_VTABLES (type);
13587 : 2216 : for (unsigned ix = 0; ix != len; ix++)
13588 : : {
13589 : 1200 : tree vtable = (*vtables)[ix];
13590 : 1200 : gcc_checking_assert (!*chain);
13591 : 1200 : *chain = vtable;
13592 : 1200 : chain = &DECL_CHAIN (vtable);
13593 : : }
13594 : : }
13595 : 22762 : CLASSTYPE_FRIEND_CLASSES (type) = friend_classes;
13596 : 22762 : DECL_FRIENDLIST (defn) = friend_functions;
13597 : 22762 : CLASSTYPE_DECL_LIST (type) = decl_list;
13598 : :
13599 : 24243 : for (; friend_classes; friend_classes = TREE_CHAIN (friend_classes))
13600 : : {
13601 : 1481 : tree f = TREE_VALUE (friend_classes);
13602 : 1481 : if (TREE_CODE (f) == TEMPLATE_DECL)
13603 : 600 : f = TREE_TYPE (f);
13604 : :
13605 : 1481 : if (CLASS_TYPE_P (f))
13606 : : {
13607 : 1449 : CLASSTYPE_BEFRIENDING_CLASSES (f)
13608 : 2898 : = tree_cons (NULL_TREE, type,
13609 : 1449 : CLASSTYPE_BEFRIENDING_CLASSES (f));
13610 : 1487 : dump () && dump ("Class %N befriending %C:%N",
13611 : 6 : type, TREE_CODE (f), f);
13612 : : }
13613 : : }
13614 : :
13615 : 26633 : for (; friend_functions;
13616 : 3871 : friend_functions = TREE_CHAIN (friend_functions))
13617 : 3871 : for (tree friend_decls = TREE_VALUE (friend_functions);
13618 : 8700 : friend_decls; friend_decls = TREE_CHAIN (friend_decls))
13619 : : {
13620 : 4829 : tree f = TREE_VALUE (friend_decls);
13621 : 4829 : if (TREE_CODE (f) == TU_LOCAL_ENTITY)
13622 : 36 : continue;
13623 : :
13624 : 4793 : DECL_BEFRIENDING_CLASSES (f)
13625 : 4793 : = tree_cons (NULL_TREE, type, DECL_BEFRIENDING_CLASSES (f));
13626 : 4856 : dump () && dump ("Class %N befriending %C:%N",
13627 : 27 : type, TREE_CODE (f), f);
13628 : : }
13629 : : }
13630 : :
13631 : 51288 : if (TYPE_CONTAINS_VPTR_P (type))
13632 : : /* Read and install the thunks. */
13633 : 2376 : while (tree vfunc = tree_node ())
13634 : : {
13635 : 328 : tree thunks = chained_decls ();
13636 : 328 : if (installing)
13637 : 162 : SET_DECL_THUNKS (vfunc, thunks);
13638 : : }
13639 : :
13640 : 51288 : vec_free (vtables);
13641 : : }
13642 : :
13643 : : /* Propagate to all variants. */
13644 : 52302 : if (installing)
13645 : 23280 : fixup_type_variants (type);
13646 : :
13647 : : /* IS_FAKE_BASE_TYPE is inaccurate at this point, because if this is
13648 : : the fake base, we've not hooked it into the containing class's
13649 : : data structure yet. Fortunately it has a unique name. */
13650 : 23280 : if (installing
13651 : 23280 : && DECL_NAME (defn) != as_base_identifier
13652 : 22762 : && (!CLASSTYPE_TEMPLATE_INFO (type)
13653 : 19328 : || !uses_template_parms (TI_ARGS (CLASSTYPE_TEMPLATE_INFO (type)))))
13654 : : /* Emit debug info. It'd be nice to know if the interface TU
13655 : : already emitted this. */
13656 : 12726 : rest_of_type_compilation (type, !LOCAL_CLASS_P (type));
13657 : :
13658 : 52302 : vec_free (fields);
13659 : :
13660 : 52302 : return !get_overrun ();
13661 : : }
13662 : :
13663 : : void
13664 : 7368 : trees_out::write_enum_def (tree decl)
13665 : : {
13666 : 7368 : tree type = TREE_TYPE (decl);
13667 : :
13668 : 7368 : tree_node (TYPE_VALUES (type));
13669 : : /* Note that we stream TYPE_MIN/MAX_VALUE directly as part of the
13670 : : ENUMERAL_TYPE. */
13671 : 7368 : }
13672 : :
13673 : : void
13674 : 7368 : trees_out::mark_enum_def (tree decl)
13675 : : {
13676 : 7368 : tree type = TREE_TYPE (decl);
13677 : :
13678 : 36238 : for (tree values = TYPE_VALUES (type); values; values = TREE_CHAIN (values))
13679 : : {
13680 : 28870 : tree cst = TREE_VALUE (values);
13681 : 28870 : mark_by_value (cst);
13682 : : /* We must mark the init to avoid circularity in tt_enum_int. */
13683 : 28870 : if (tree init = DECL_INITIAL (cst))
13684 : 28662 : if (TREE_CODE (init) == INTEGER_CST)
13685 : 28102 : mark_by_value (init);
13686 : : }
13687 : 7368 : }
13688 : :
13689 : : bool
13690 : 2413 : trees_in::read_enum_def (tree defn, tree maybe_template)
13691 : : {
13692 : 2413 : tree type = TREE_TYPE (defn);
13693 : 2413 : tree values = tree_node ();
13694 : :
13695 : 2413 : if (get_overrun ())
13696 : : return false;
13697 : :
13698 : 2413 : tree maybe_dup = odr_duplicate (maybe_template, TYPE_VALUES (type));
13699 : 4826 : bool installing = maybe_dup && !TYPE_VALUES (type);
13700 : :
13701 : 2413 : if (installing)
13702 : : {
13703 : 1004 : TYPE_VALUES (type) = values;
13704 : : /* Note that we stream TYPE_MIN/MAX_VALUE directly as part of the
13705 : : ENUMERAL_TYPE. */
13706 : :
13707 : 1637 : rest_of_type_compilation (type, DECL_NAMESPACE_SCOPE_P (defn));
13708 : : }
13709 : 1409 : else if (maybe_dup)
13710 : : {
13711 : 1409 : tree known = TYPE_VALUES (type);
13712 : 8092 : for (; known && values;
13713 : 6683 : known = TREE_CHAIN (known), values = TREE_CHAIN (values))
13714 : : {
13715 : 6692 : tree known_decl = TREE_VALUE (known);
13716 : 6692 : tree new_decl = TREE_VALUE (values);
13717 : :
13718 : 6692 : if (DECL_NAME (known_decl) != DECL_NAME (new_decl))
13719 : : break;
13720 : :
13721 : 6686 : new_decl = maybe_duplicate (new_decl);
13722 : :
13723 : 6686 : if (!cp_tree_equal (DECL_INITIAL (known_decl),
13724 : 6686 : DECL_INITIAL (new_decl)))
13725 : : break;
13726 : : }
13727 : :
13728 : 1409 : if (known || values)
13729 : : {
13730 : 12 : auto_diagnostic_group d;
13731 : 12 : error_at (DECL_SOURCE_LOCATION (maybe_dup),
13732 : : "definition of %qD does not match", maybe_dup);
13733 : 12 : inform (DECL_SOURCE_LOCATION (defn),
13734 : : "existing definition %qD", defn);
13735 : :
13736 : 12 : tree known_decl = NULL_TREE, new_decl = NULL_TREE;
13737 : :
13738 : 12 : if (known)
13739 : 9 : known_decl = TREE_VALUE (known);
13740 : 12 : if (values)
13741 : 12 : new_decl = maybe_duplicate (TREE_VALUE (values));
13742 : :
13743 : 12 : if (known_decl && new_decl)
13744 : : {
13745 : 9 : inform (DECL_SOURCE_LOCATION (new_decl),
13746 : : "enumerator %qD does not match ...", new_decl);
13747 : 9 : inform (DECL_SOURCE_LOCATION (known_decl),
13748 : : "... this enumerator %qD", known_decl);
13749 : : }
13750 : 3 : else if (known_decl || new_decl)
13751 : : {
13752 : 3 : tree extra = known_decl ? known_decl : new_decl;
13753 : 3 : inform (DECL_SOURCE_LOCATION (extra),
13754 : : "additional enumerators beginning with %qD", extra);
13755 : : }
13756 : : else
13757 : 0 : inform (DECL_SOURCE_LOCATION (maybe_dup),
13758 : : "enumeration range differs");
13759 : :
13760 : : /* Mark it bad. */
13761 : 12 : unmatched_duplicate (maybe_template);
13762 : 12 : }
13763 : : }
13764 : :
13765 : : return true;
13766 : : }
13767 : :
13768 : : /* Write out the body of DECL. See above circularity note. */
13769 : :
13770 : : void
13771 : 619821 : trees_out::write_definition (tree decl, bool refs_tu_local)
13772 : : {
13773 : 619821 : auto ovr = make_temp_override (writing_local_entities,
13774 : 619821 : writing_local_entities || refs_tu_local);
13775 : :
13776 : 619821 : if (streaming_p ())
13777 : : {
13778 : 309826 : assert_definition (decl);
13779 : 309826 : dump ()
13780 : 641 : && dump ("Writing definition %C:%N", TREE_CODE (decl), decl);
13781 : : }
13782 : : else
13783 : 309995 : dump (dumper::DEPEND)
13784 : 96 : && dump ("Depending definition %C:%N", TREE_CODE (decl), decl);
13785 : :
13786 : 955611 : again:
13787 : 955611 : switch (TREE_CODE (decl))
13788 : : {
13789 : 0 : default:
13790 : 0 : gcc_unreachable ();
13791 : :
13792 : 335790 : case TEMPLATE_DECL:
13793 : 335790 : decl = DECL_TEMPLATE_RESULT (decl);
13794 : 335790 : goto again;
13795 : :
13796 : 362445 : case FUNCTION_DECL:
13797 : 362445 : write_function_def (decl);
13798 : 362445 : break;
13799 : :
13800 : 149480 : case TYPE_DECL:
13801 : 149480 : {
13802 : 149480 : tree type = TREE_TYPE (decl);
13803 : 149480 : gcc_assert (TYPE_MAIN_VARIANT (type) == type
13804 : : && TYPE_NAME (type) == decl);
13805 : 149480 : if (TREE_CODE (type) == ENUMERAL_TYPE)
13806 : 7368 : write_enum_def (decl);
13807 : : else
13808 : 142112 : write_class_def (decl);
13809 : : }
13810 : : break;
13811 : :
13812 : 107896 : case VAR_DECL:
13813 : 107896 : case CONCEPT_DECL:
13814 : 107896 : write_var_def (decl);
13815 : 107896 : break;
13816 : : }
13817 : 619821 : }
13818 : :
13819 : : /* Mark a declaration for by-value walking. If DO_DEFN is true, mark
13820 : : its body too. */
13821 : :
13822 : : void
13823 : 2588518 : trees_out::mark_declaration (tree decl, bool do_defn)
13824 : : {
13825 : 2588518 : mark_by_value (decl);
13826 : :
13827 : 2588518 : if (TREE_CODE (decl) == TEMPLATE_DECL)
13828 : 836334 : decl = DECL_TEMPLATE_RESULT (decl);
13829 : :
13830 : 2588518 : if (!do_defn)
13831 : : return;
13832 : :
13833 : 619849 : switch (TREE_CODE (decl))
13834 : : {
13835 : 0 : default:
13836 : 0 : gcc_unreachable ();
13837 : :
13838 : : case FUNCTION_DECL:
13839 : : mark_function_def (decl);
13840 : : break;
13841 : :
13842 : 149508 : case TYPE_DECL:
13843 : 149508 : {
13844 : 149508 : tree type = TREE_TYPE (decl);
13845 : 149508 : gcc_assert (TYPE_MAIN_VARIANT (type) == type
13846 : : && TYPE_NAME (type) == decl);
13847 : 149508 : if (TREE_CODE (type) == ENUMERAL_TYPE)
13848 : 7368 : mark_enum_def (decl);
13849 : : else
13850 : 142140 : mark_class_def (decl);
13851 : : }
13852 : : break;
13853 : :
13854 : : case VAR_DECL:
13855 : : case CONCEPT_DECL:
13856 : : mark_var_def (decl);
13857 : : break;
13858 : : }
13859 : : }
13860 : :
13861 : : /* Read in the body of DECL. See above circularity note. */
13862 : :
13863 : : bool
13864 : 242741 : trees_in::read_definition (tree decl)
13865 : : {
13866 : 243840 : dump () && dump ("Reading definition %C %N", TREE_CODE (decl), decl);
13867 : :
13868 : : tree maybe_template = decl;
13869 : :
13870 : 242741 : again:
13871 : 384858 : switch (TREE_CODE (decl))
13872 : : {
13873 : : default:
13874 : : break;
13875 : :
13876 : 142117 : case TEMPLATE_DECL:
13877 : 142117 : decl = DECL_TEMPLATE_RESULT (decl);
13878 : 142117 : goto again;
13879 : :
13880 : 154946 : case FUNCTION_DECL:
13881 : 154946 : return read_function_def (decl, maybe_template);
13882 : :
13883 : 54715 : case TYPE_DECL:
13884 : 54715 : {
13885 : 54715 : tree type = TREE_TYPE (decl);
13886 : 54715 : gcc_assert (TYPE_MAIN_VARIANT (type) == type
13887 : : && TYPE_NAME (type) == decl);
13888 : 54715 : if (TREE_CODE (type) == ENUMERAL_TYPE)
13889 : 2413 : return read_enum_def (decl, maybe_template);
13890 : : else
13891 : 52302 : return read_class_def (decl, maybe_template);
13892 : : }
13893 : 33080 : break;
13894 : :
13895 : 33080 : case VAR_DECL:
13896 : 33080 : case CONCEPT_DECL:
13897 : 33080 : return read_var_def (decl, maybe_template);
13898 : : }
13899 : :
13900 : : return false;
13901 : : }
13902 : :
13903 : : /* Lookup an maybe insert a slot for depset for KEY. */
13904 : :
13905 : : depset **
13906 : 11899762 : depset::hash::entity_slot (tree entity, bool insert)
13907 : : {
13908 : 11899762 : traits::compare_type key (entity, NULL);
13909 : 18020119 : depset **slot = find_slot_with_hash (key, traits::hash (key),
13910 : : insert ? INSERT : NO_INSERT);
13911 : :
13912 : 11899762 : return slot;
13913 : : }
13914 : :
13915 : : depset **
13916 : 153576 : depset::hash::binding_slot (tree ctx, tree name, bool insert)
13917 : : {
13918 : 153576 : traits::compare_type key (ctx, name);
13919 : 188182 : depset **slot = find_slot_with_hash (key, traits::hash (key),
13920 : : insert ? INSERT : NO_INSERT);
13921 : :
13922 : 153576 : return slot;
13923 : : }
13924 : :
13925 : : depset *
13926 : 5830953 : depset::hash::find_dependency (tree decl)
13927 : : {
13928 : 5830953 : depset **slot = entity_slot (decl, false);
13929 : :
13930 : 5830953 : return slot ? *slot : NULL;
13931 : : }
13932 : :
13933 : : depset *
13934 : 34606 : depset::hash::find_binding (tree ctx, tree name)
13935 : : {
13936 : 34606 : depset **slot = binding_slot (ctx, name, false);
13937 : :
13938 : 34606 : return slot ? *slot : NULL;
13939 : : }
13940 : :
13941 : : static bool is_tu_local_entity (tree decl, bool explain = false);
13942 : : static bool is_tu_local_value (tree decl, tree expr, bool explain = false);
13943 : : static bool has_tu_local_tmpl_arg (tree decl, tree args, bool explain);
13944 : :
13945 : : /* Returns true if DECL is a TU-local entity, as defined by [basic.link].
13946 : : If EXPLAIN is true, emit an informative note about why DECL is TU-local. */
13947 : :
13948 : : static bool
13949 : 1890631 : is_tu_local_entity (tree decl, bool explain/*=false*/)
13950 : : {
13951 : 1890631 : gcc_checking_assert (DECL_P (decl));
13952 : 1890631 : location_t loc = DECL_SOURCE_LOCATION (decl);
13953 : 1890631 : tree type = TREE_TYPE (decl);
13954 : :
13955 : : /* Only types, functions, variables, and template (specialisations)
13956 : : can be TU-local. */
13957 : 1890631 : if (TREE_CODE (decl) != TYPE_DECL
13958 : : && TREE_CODE (decl) != FUNCTION_DECL
13959 : : && TREE_CODE (decl) != VAR_DECL
13960 : : && TREE_CODE (decl) != TEMPLATE_DECL)
13961 : : return false;
13962 : :
13963 : : /* An explicit type alias is not an entity; we don't want to stream
13964 : : such aliases if they refer to TU-local entities, so propagate this
13965 : : from the original type. The built-in declarations of 'int' and such
13966 : : are never TU-local. */
13967 : 1887812 : if (TREE_CODE (decl) == TYPE_DECL
13968 : 640757 : && !DECL_SELF_REFERENCE_P (decl)
13969 : 2501854 : && !DECL_IMPLICIT_TYPEDEF_P (decl))
13970 : : {
13971 : 339449 : tree orig = DECL_ORIGINAL_TYPE (decl);
13972 : 339449 : if (orig && TYPE_NAME (orig))
13973 : : {
13974 : 68485 : if (explain)
13975 : 11 : inform (loc, "%qD is an alias of TU-local type %qT", decl, orig);
13976 : 68485 : return is_tu_local_entity (TYPE_NAME (orig), explain);
13977 : : }
13978 : : else
13979 : : return false;
13980 : : }
13981 : :
13982 : : /* Check specializations first for slightly better explanations. */
13983 : 1548363 : int use_tpl = -1;
13984 : 1548363 : tree ti = node_template_info (decl, use_tpl);
13985 : 1893887 : if (use_tpl > 0 && TREE_CODE (TI_TEMPLATE (ti)) == TEMPLATE_DECL)
13986 : : {
13987 : : /* A specialization of a TU-local template. */
13988 : 345410 : tree tmpl = TI_TEMPLATE (ti);
13989 : 345410 : if (is_tu_local_entity (tmpl))
13990 : : {
13991 : 66 : if (explain)
13992 : : {
13993 : 18 : inform (loc, "%qD is a specialization of TU-local template %qD",
13994 : : decl, tmpl);
13995 : 18 : is_tu_local_entity (tmpl, /*explain=*/true);
13996 : : }
13997 : 66 : return true;
13998 : : }
13999 : :
14000 : : /* A specialization of a template with any TU-local template argument. */
14001 : 345344 : if (has_tu_local_tmpl_arg (decl, TI_ARGS (ti), explain))
14002 : : return true;
14003 : :
14004 : : /* FIXME A specialization of a template whose (possibly instantiated)
14005 : : declaration is an exposure. This should always be covered by the
14006 : : above cases?? */
14007 : : }
14008 : :
14009 : : /* A type, function, variable, or template with internal linkage. */
14010 : 1548268 : linkage_kind kind = decl_linkage (decl);
14011 : 1548268 : if (kind == lk_internal
14012 : : /* But although weakrefs are marked static, don't consider them
14013 : : to be TU-local. */
14014 : 1548268 : && !lookup_attribute ("weakref", DECL_ATTRIBUTES (decl)))
14015 : : {
14016 : 775 : if (explain)
14017 : 165 : inform (loc, "%qD declared with internal linkage", decl);
14018 : 775 : return true;
14019 : : }
14020 : :
14021 : : /* Does not have a name with linkage and is declared, or introduced by a
14022 : : lambda-expression, within the definition of a TU-local entity. */
14023 : 1547493 : if (kind == lk_none)
14024 : : {
14025 : 221238 : tree ctx = CP_DECL_CONTEXT (decl);
14026 : 275676 : if (LAMBDA_TYPE_P (type))
14027 : 34560 : if (tree extra = LAMBDA_TYPE_EXTRA_SCOPE (type))
14028 : 221238 : ctx = extra;
14029 : :
14030 : 221238 : if (TREE_CODE (ctx) == NAMESPACE_DECL)
14031 : : {
14032 : 31 : if (!TREE_PUBLIC (ctx))
14033 : : {
14034 : 0 : if (explain)
14035 : 0 : inform (loc, "%qD has no linkage and is declared in an "
14036 : : "anonymous namespace", decl);
14037 : 0 : return true;
14038 : : }
14039 : : }
14040 : 221207 : else if (TYPE_P (ctx))
14041 : : {
14042 : 23257 : tree ctx_decl = TYPE_MAIN_DECL (ctx);
14043 : 23257 : if (is_tu_local_entity (ctx_decl))
14044 : : {
14045 : 0 : if (explain)
14046 : : {
14047 : 0 : inform (loc, "%qD has no linkage and is declared within "
14048 : : "TU-local entity %qT", decl, ctx);
14049 : 0 : is_tu_local_entity (ctx_decl, /*explain=*/true);
14050 : : }
14051 : 0 : return true;
14052 : : }
14053 : : }
14054 : 197950 : else if (is_tu_local_entity (ctx))
14055 : : {
14056 : 21 : if (explain)
14057 : : {
14058 : 6 : inform (loc, "%qD has no linkage and is declared within "
14059 : : "TU-local entity %qD", decl, ctx);
14060 : 6 : is_tu_local_entity (ctx, /*explain=*/true);
14061 : : }
14062 : 21 : return true;
14063 : : }
14064 : : }
14065 : :
14066 : : /* A type with no name that is defined outside a class-specifier, function
14067 : : body, or initializer; or is introduced by a defining-type-specifier that
14068 : : is used to declare only TU-local entities.
14069 : :
14070 : : We consider types with names for linkage purposes as having names, since
14071 : : these aren't really TU-local. */
14072 : 1547472 : tree inner = STRIP_TEMPLATE (decl);
14073 : 416909 : if (inner
14074 : 1547472 : && TREE_CODE (inner) == TYPE_DECL
14075 : 1154880 : && TYPE_ANON_P (type)
14076 : 19127 : && !DECL_SELF_REFERENCE_P (inner)
14077 : : /* An enum with an enumerator name for linkage. */
14078 : 435213 : && !(UNSCOPED_ENUM_P (type) && TYPE_VALUES (type)))
14079 : : {
14080 : 16980 : tree main_decl = TYPE_MAIN_DECL (type);
14081 : 33743 : if (LAMBDA_TYPE_P (type))
14082 : : {
14083 : : /* A lambda expression is, in practice, TU-local iff it has no
14084 : : mangling scope. This currently doesn't line up exactly with
14085 : : the standard's definition due to some ABI issues, but it's
14086 : : pretty close, and avoids other issues down the line. */
14087 : 33464 : if (!LAMBDA_TYPE_EXTRA_SCOPE (type))
14088 : : {
14089 : 4 : if (explain)
14090 : 2 : inform (loc, "%qT has no name and cannot be differentiated "
14091 : : "from similar lambdas in other TUs", type);
14092 : 4 : return true;
14093 : : }
14094 : : }
14095 : 496 : else if (!DECL_CLASS_SCOPE_P (main_decl)
14096 : 278 : && !decl_function_context (main_decl))
14097 : : {
14098 : 30 : if (explain)
14099 : 12 : inform (loc, "%qT has no name and is not defined within a class, "
14100 : : "function, or initializer", type);
14101 : 30 : return true;
14102 : : }
14103 : :
14104 : : // FIXME introduced by a defining-type-specifier only declaring TU-local
14105 : : // entities; does this refer to e.g. 'static struct {} a;"? I can't
14106 : : // think of any cases where this isn't covered by earlier cases. */
14107 : : }
14108 : :
14109 : : return false;
14110 : : }
14111 : :
14112 : : /* Helper for is_tu_local_entity. Returns true if one of the ARGS of
14113 : : DECL is TU-local. Emits an explanation if EXPLAIN is true. */
14114 : :
14115 : : static bool
14116 : 384016 : has_tu_local_tmpl_arg (tree decl, tree args, bool explain)
14117 : : {
14118 : 384016 : if (!args || TREE_CODE (args) != TREE_VEC)
14119 : : return false;
14120 : :
14121 : 1028378 : for (tree a : tree_vec_range (args))
14122 : : {
14123 : 644391 : if (TREE_CODE (a) == TREE_VEC)
14124 : : {
14125 : 38672 : if (has_tu_local_tmpl_arg (decl, a, explain))
14126 : 29 : return true;
14127 : : }
14128 : : else if (!WILDCARD_TYPE_P (a))
14129 : : {
14130 : 551030 : if (DECL_P (a) && is_tu_local_entity (a))
14131 : : {
14132 : 0 : if (explain)
14133 : : {
14134 : 0 : inform (DECL_SOURCE_LOCATION (decl),
14135 : : "%qD has TU-local template argument %qD",
14136 : : decl, a);
14137 : 0 : is_tu_local_entity (a, /*explain=*/true);
14138 : : }
14139 : 0 : return true;
14140 : : }
14141 : :
14142 : 551030 : if (TYPE_P (a) && TYPE_NAME (a) && is_tu_local_entity (TYPE_NAME (a)))
14143 : : {
14144 : 14 : if (explain)
14145 : : {
14146 : 1 : inform (DECL_SOURCE_LOCATION (decl),
14147 : : "%qD has TU-local template argument %qT",
14148 : : decl, a);
14149 : 1 : is_tu_local_entity (TYPE_NAME (a), /*explain=*/true);
14150 : : }
14151 : 14 : return true;
14152 : : }
14153 : :
14154 : 551016 : if (EXPR_P (a) && is_tu_local_value (decl, a, explain))
14155 : : return true;
14156 : : }
14157 : : }
14158 : :
14159 : 383987 : return false;
14160 : : }
14161 : :
14162 : : /* Returns true if EXPR (part of the initializer for DECL) is a TU-local value
14163 : : or object. Emits an explanation if EXPLAIN is true. */
14164 : :
14165 : : static bool
14166 : 43694 : is_tu_local_value (tree decl, tree expr, bool explain/*=false*/)
14167 : : {
14168 : 43694 : if (!expr)
14169 : : return false;
14170 : :
14171 : 42667 : tree e = expr;
14172 : 42667 : STRIP_ANY_LOCATION_WRAPPER (e);
14173 : 42667 : STRIP_NOPS (e);
14174 : 42667 : if (TREE_CODE (e) == TARGET_EXPR)
14175 : 0 : e = TARGET_EXPR_INITIAL (e);
14176 : 0 : if (!e)
14177 : : return false;
14178 : :
14179 : : /* It is, or is a pointer to, a TU-local function or the object associated
14180 : : with a TU-local variable. */
14181 : 42667 : tree object = NULL_TREE;
14182 : 42667 : if (TREE_CODE (e) == ADDR_EXPR)
14183 : 1892 : object = TREE_OPERAND (e, 0);
14184 : 40775 : else if (TREE_CODE (e) == PTRMEM_CST)
14185 : 0 : object = PTRMEM_CST_MEMBER (e);
14186 : 40775 : else if (VAR_OR_FUNCTION_DECL_P (e))
14187 : : object = e;
14188 : :
14189 : 1892 : if (object
14190 : 2168 : && VAR_OR_FUNCTION_DECL_P (object)
14191 : 2258 : && is_tu_local_entity (object))
14192 : : {
14193 : 54 : if (explain)
14194 : : {
14195 : : /* We've lost a lot of location information by the time we get here,
14196 : : so let's just do our best effort. */
14197 : 18 : auto loc = cp_expr_loc_or_loc (expr, DECL_SOURCE_LOCATION (decl));
14198 : 18 : if (VAR_P (object))
14199 : 9 : inform (loc, "%qD refers to TU-local object %qD", decl, object);
14200 : : else
14201 : 9 : inform (loc, "%qD refers to TU-local function %qD", decl, object);
14202 : 18 : is_tu_local_entity (object, true);
14203 : : }
14204 : 54 : return true;
14205 : : }
14206 : :
14207 : : /* It is an object of class or array type and any of its subobjects or
14208 : : any of the objects or functions to which its non-static data members
14209 : : of reference type refer is TU-local and is usable in constant
14210 : : expressions. */
14211 : 42613 : if (TREE_CODE (e) == CONSTRUCTOR && AGGREGATE_TYPE_P (TREE_TYPE (e)))
14212 : 20800 : for (auto &f : CONSTRUCTOR_ELTS (e))
14213 : 14434 : if (is_tu_local_value (decl, f.value, explain))
14214 : : return true;
14215 : :
14216 : : return false;
14217 : : }
14218 : :
14219 : : /* Complains if DECL is a TU-local entity imported from a named module.
14220 : : Returns TRUE if instantiation should fail. */
14221 : :
14222 : : bool
14223 : 7366527508 : instantiating_tu_local_entity (tree decl)
14224 : : {
14225 : 7366527508 : if (!modules_p ())
14226 : : return false;
14227 : :
14228 : 21540755 : if (TREE_CODE (decl) == TU_LOCAL_ENTITY)
14229 : : {
14230 : 86 : auto_diagnostic_group d;
14231 : 86 : error ("instantiation exposes TU-local entity %qD",
14232 : 86 : TU_LOCAL_ENTITY_NAME (decl));
14233 : 86 : inform (TU_LOCAL_ENTITY_LOCATION (decl), "declared here");
14234 : 86 : return true;
14235 : 86 : }
14236 : :
14237 : : /* Currently, only TU-local variables and functions will be emitted
14238 : : from named modules. */
14239 : 21540669 : if (!VAR_OR_FUNCTION_DECL_P (decl))
14240 : : return false;
14241 : :
14242 : : /* From this point we will only be emitting warnings; if we're not
14243 : : warning about this case then there's no need to check further. */
14244 : 526601 : if (!warn_expose_global_module_tu_local
14245 : 1053202 : || !warning_enabled_at (DECL_SOURCE_LOCATION (decl),
14246 : 526601 : OPT_Wexpose_global_module_tu_local))
14247 : 6680 : return false;
14248 : :
14249 : 519921 : if (!is_tu_local_entity (decl))
14250 : : return false;
14251 : :
14252 : 47 : tree origin = get_originating_module_decl (decl);
14253 : 47 : if (!DECL_LANG_SPECIFIC (STRIP_TEMPLATE (origin))
14254 : 84 : || !DECL_MODULE_IMPORT_P (STRIP_TEMPLATE (origin)))
14255 : : return false;
14256 : :
14257 : : /* Referencing TU-local entities from a header is generally OK.
14258 : : We don't have an easy way to detect if this declaration came
14259 : : from a header via a separate named module, but we can just
14260 : : ignore that case for warning purposes. */
14261 : 9 : unsigned index = import_entity_index (origin);
14262 : 9 : module_state *mod = import_entity_module (index);
14263 : 9 : if (mod->is_header ())
14264 : : return false;
14265 : :
14266 : 9 : auto_diagnostic_group d;
14267 : 9 : warning (OPT_Wexpose_global_module_tu_local,
14268 : : "instantiation exposes TU-local entity %qD", decl);
14269 : 9 : inform (DECL_SOURCE_LOCATION (decl), "declared here");
14270 : :
14271 : : /* We treat TU-local entities from the GMF as not actually being
14272 : : TU-local as an extension, so allow instantation to proceed. */
14273 : 9 : return false;
14274 : 9 : }
14275 : :
14276 : : /* DECL is a newly discovered dependency. Create the depset, if it
14277 : : doesn't already exist. Add it to the worklist if so.
14278 : :
14279 : : DECL will be an OVL_USING_P OVERLOAD, if it's from a binding that's
14280 : : a using decl.
14281 : :
14282 : : We do not have to worry about adding the same dependency more than
14283 : : once. First it's harmless, but secondly the TREE_VISITED marking
14284 : : prevents us wanting to do it anyway. */
14285 : :
14286 : : depset *
14287 : 4987667 : depset::hash::make_dependency (tree decl, entity_kind ek)
14288 : : {
14289 : : /* Make sure we're being told consistent information. */
14290 : 9257129 : gcc_checking_assert ((ek == EK_NAMESPACE)
14291 : : == (TREE_CODE (decl) == NAMESPACE_DECL
14292 : : && !DECL_NAMESPACE_ALIAS (decl)));
14293 : 4987667 : gcc_checking_assert (ek != EK_BINDING && ek != EK_REDIRECT);
14294 : 4987667 : gcc_checking_assert (TREE_CODE (decl) != FIELD_DECL
14295 : : && (TREE_CODE (decl) != USING_DECL
14296 : : || TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL));
14297 : 4987667 : gcc_checking_assert (!is_key_order ());
14298 : 4987667 : if (ek == EK_USING)
14299 : 13566 : gcc_checking_assert (TREE_CODE (decl) == OVERLOAD);
14300 : 4987667 : if (ek == EK_TU_LOCAL)
14301 : 87 : gcc_checking_assert (DECL_DECLARES_FUNCTION_P (decl));
14302 : :
14303 : 4987667 : if (TREE_CODE (decl) == TEMPLATE_DECL)
14304 : : /* The template should have copied these from its result decl. */
14305 : 1773599 : gcc_checking_assert (DECL_MODULE_EXPORT_P (decl)
14306 : : == DECL_MODULE_EXPORT_P (DECL_TEMPLATE_RESULT (decl)));
14307 : :
14308 : 4987667 : depset **slot = entity_slot (decl, true);
14309 : 4987667 : depset *dep = *slot;
14310 : 4987667 : bool for_binding = ek == EK_FOR_BINDING;
14311 : :
14312 : 4987667 : if (!dep)
14313 : : {
14314 : 451248 : if ((DECL_IMPLICIT_TYPEDEF_P (decl)
14315 : : /* ... not an enum, for instance. */
14316 : 227983 : && RECORD_OR_UNION_TYPE_P (TREE_TYPE (decl))
14317 : 224154 : && TYPE_LANG_SPECIFIC (TREE_TYPE (decl))
14318 : 202578 : && CLASSTYPE_USE_TEMPLATE (TREE_TYPE (decl)) == 2)
14319 : 1533099 : || (VAR_P (decl)
14320 : 65758 : && DECL_LANG_SPECIFIC (decl)
14321 : 65677 : && DECL_USE_TEMPLATE (decl) == 2))
14322 : : {
14323 : : /* A partial or explicit specialization. Partial
14324 : : specializations might not be in the hash table, because
14325 : : there can be multiple differently-constrained variants.
14326 : :
14327 : : template<typename T> class silly;
14328 : : template<typename T> requires true class silly {};
14329 : :
14330 : : We need to find them, insert their TEMPLATE_DECL in the
14331 : : dep_hash, and then convert the dep we just found into a
14332 : : redirect. */
14333 : :
14334 : 34552 : tree ti = get_template_info (decl);
14335 : 34552 : tree tmpl = TI_TEMPLATE (ti);
14336 : 34552 : tree partial = NULL_TREE;
14337 : 34552 : for (tree spec = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
14338 : 106330 : spec; spec = TREE_CHAIN (spec))
14339 : 90021 : if (DECL_TEMPLATE_RESULT (TREE_VALUE (spec)) == decl)
14340 : : {
14341 : : partial = TREE_VALUE (spec);
14342 : : break;
14343 : : }
14344 : :
14345 : 34552 : if (partial)
14346 : : {
14347 : : /* Eagerly create an empty redirect. The following
14348 : : make_dependency call could cause hash reallocation,
14349 : : and invalidate slot's value. */
14350 : 18243 : depset *redirect = make_entity (decl, EK_REDIRECT);
14351 : :
14352 : : /* Redirects are never reached -- always snap to their target. */
14353 : 18243 : redirect->set_flag_bit<DB_UNREACHED_BIT> ();
14354 : :
14355 : 18243 : *slot = redirect;
14356 : :
14357 : 18243 : depset *tmpl_dep = make_dependency (partial, EK_PARTIAL);
14358 : 18243 : gcc_checking_assert (tmpl_dep->get_entity_kind () == EK_PARTIAL);
14359 : :
14360 : 18243 : redirect->deps.safe_push (tmpl_dep);
14361 : :
14362 : 18243 : return redirect;
14363 : : }
14364 : : }
14365 : :
14366 : 1093568 : bool has_def = ek != EK_USING && has_definition (decl);
14367 : 1080002 : if (ek > EK_BINDING)
14368 : 133603 : ek = EK_DECL;
14369 : :
14370 : : /* The only OVERLOADS we should see are USING decls from
14371 : : bindings. */
14372 : 1093568 : *slot = dep = make_entity (decl, ek, has_def);
14373 : :
14374 : 1093568 : if (CHECKING_P && TREE_CODE (decl) == TEMPLATE_DECL)
14375 : : /* The template_result should otherwise not be in the
14376 : : table, or be an empty redirect (created above). */
14377 : 289404 : if (auto *eslot = entity_slot (DECL_TEMPLATE_RESULT (decl), false))
14378 : 18243 : gcc_checking_assert ((*eslot)->get_entity_kind () == EK_REDIRECT
14379 : : && !(*eslot)->deps.length ());
14380 : :
14381 : 1093568 : if (ek != EK_USING)
14382 : : {
14383 : 1080002 : tree not_tmpl = STRIP_TEMPLATE (decl);
14384 : 1080002 : bool imported_from_module_p = false;
14385 : :
14386 : 1080002 : if (DECL_LANG_SPECIFIC (not_tmpl)
14387 : 2094956 : && DECL_MODULE_IMPORT_P (not_tmpl))
14388 : : {
14389 : : /* Store the module number and index in cluster/section,
14390 : : so we don't have to look them up again. */
14391 : 2300 : unsigned index = import_entity_index (decl);
14392 : 2300 : module_state *from = import_entity_module (index);
14393 : : /* Remap will be zero for imports from partitions, which
14394 : : we want to treat as-if declared in this TU. */
14395 : 2300 : if (from->remap)
14396 : : {
14397 : 1846 : dep->cluster = index - from->entity_lwm;
14398 : 1846 : dep->section = from->remap;
14399 : 1846 : dep->set_flag_bit<DB_IMPORTED_BIT> ();
14400 : :
14401 : 1846 : if (!from->is_header ())
14402 : 1080002 : imported_from_module_p = true;
14403 : : }
14404 : : }
14405 : :
14406 : : /* Check for TU-local entities. This is unnecessary in header
14407 : : units because we can export internal-linkage decls, and
14408 : : no declarations are exposures. Similarly, if the decl was
14409 : : imported from a non-header module we know it cannot have
14410 : : been TU-local. */
14411 : 1080002 : if (!header_module_p () && !imported_from_module_p)
14412 : : {
14413 : 318759 : if (is_tu_local_entity (decl))
14414 : 300 : dep->set_flag_bit<DB_TU_LOCAL_BIT> ();
14415 : :
14416 : 318759 : if (VAR_P (decl)
14417 : 21871 : && decl_maybe_constant_var_p (decl)
14418 : 339933 : && is_tu_local_value (decl, DECL_INITIAL (decl)))
14419 : : {
14420 : : /* A potentially-constant variable initialized to a TU-local
14421 : : value is not usable in constant expressions within other
14422 : : translation units. We can achieve this by simply not
14423 : : streaming the definition in such cases. */
14424 : 24 : dep->clear_flag_bit<DB_DEFN_BIT> ();
14425 : :
14426 : 24 : if (DECL_DECLARED_CONSTEXPR_P (decl)
14427 : 39 : || DECL_INLINE_VAR_P (decl))
14428 : : /* A constexpr variable initialized to a TU-local value,
14429 : : or an inline value (PR c++/119996), is an exposure.
14430 : :
14431 : : For simplicity, we don't support "non-strict" TU-local
14432 : : values: even if the TU-local entity we refer to in the
14433 : : initialiser is in the GMF, we still won't consider this
14434 : : valid in constant expressions in other TUs, and so
14435 : : complain accordingly. */
14436 : 15 : dep->set_flag_bit<DB_EXPOSE_PURVIEW_BIT> ();
14437 : : }
14438 : : }
14439 : :
14440 : : /* A namespace-scope type may be declared in one module unit
14441 : : and defined in another; make sure that we're found when
14442 : : completing the class. */
14443 : 1080002 : if (ek == EK_DECL
14444 : 464534 : && !dep->is_import ()
14445 : 463998 : && dep->has_defn ()
14446 : 238846 : && DECL_NAMESPACE_SCOPE_P (not_tmpl)
14447 : 87770 : && DECL_IMPLICIT_TYPEDEF_P (not_tmpl)
14448 : : /* Anonymous types can't be forward-declared. */
14449 : 1105198 : && !IDENTIFIER_ANON_P (DECL_NAME (not_tmpl)))
14450 : 24787 : dep->set_flag_bit<DB_IS_PENDING_BIT> ();
14451 : :
14452 : : /* Namespace-scope functions can be found by ADL by template
14453 : : instantiations in this module. We need to create bindings
14454 : : for them so that name lookup recognises they exist, if they
14455 : : won't be discarded. add_binding_entity is too early to do
14456 : : this for GM functions, because if nobody ends up using them
14457 : : we'll have leftover bindings laying around, and it's tricky
14458 : : to delete them and any namespaces they've implicitly created
14459 : : deps on. The downside is this means we don't pick up on
14460 : : using-decls, but by [module.global.frag] p3.6 we don't have
14461 : : to. */
14462 : 1080002 : if (ek == EK_DECL
14463 : 1080002 : && !for_binding
14464 : 330943 : && !dep->is_import ()
14465 : 330413 : && !dep->is_tu_local ()
14466 : 330300 : && DECL_NAMESPACE_SCOPE_P (decl)
14467 : 20286 : && DECL_DECLARES_FUNCTION_P (decl)
14468 : : /* Compiler-generated functions won't participate in ADL. */
14469 : 12899 : && !DECL_ARTIFICIAL (decl)
14470 : : /* A hidden friend doesn't need a binding. */
14471 : 1087971 : && !(DECL_LANG_SPECIFIC (not_tmpl)
14472 : 7969 : && DECL_UNIQUE_FRIEND_P (not_tmpl)))
14473 : : {
14474 : : /* This will only affect GM functions. */
14475 : 5172 : gcc_checking_assert (!DECL_LANG_SPECIFIC (not_tmpl)
14476 : : || !DECL_MODULE_PURVIEW_P (not_tmpl));
14477 : : /* We shouldn't see any instantiations or specialisations. */
14478 : 2586 : gcc_checking_assert (!DECL_LANG_SPECIFIC (decl)
14479 : : || !DECL_USE_TEMPLATE (decl));
14480 : :
14481 : 2586 : tree ns = CP_DECL_CONTEXT (decl);
14482 : 2586 : tree name = DECL_NAME (decl);
14483 : 2586 : depset *binding = find_binding (ns, name);
14484 : 2586 : if (!binding)
14485 : : {
14486 : 1243 : binding = make_binding (ns, name);
14487 : 1243 : add_namespace_context (binding, ns);
14488 : :
14489 : 1243 : depset **slot = binding_slot (ns, name, /*insert=*/true);
14490 : 1243 : *slot = binding;
14491 : : }
14492 : :
14493 : 2586 : binding->deps.safe_push (dep);
14494 : 2586 : dep->deps.safe_push (binding);
14495 : 2595 : dump (dumper::DEPEND)
14496 : 9 : && dump ("Built ADL binding for %C:%N",
14497 : 9 : TREE_CODE (decl), decl);
14498 : : }
14499 : : }
14500 : :
14501 : 1093568 : if (!dep->is_import ())
14502 : 1091722 : worklist.safe_push (dep);
14503 : : }
14504 : :
14505 : 4969424 : dump (dumper::DEPEND)
14506 : 36825 : && dump ("%s on %s %C:%N found",
14507 : : ek == EK_REDIRECT ? "Redirect"
14508 : 36825 : : (for_binding || ek == EK_TU_LOCAL) ? "Binding"
14509 : : : "Dependency",
14510 : 36825 : dep->entity_kind_name (), TREE_CODE (decl), decl);
14511 : :
14512 : 4969424 : return dep;
14513 : : }
14514 : :
14515 : : /* Whether REF is an exposure of a member type of SOURCE.
14516 : :
14517 : : This comes up with exposures of class-scope lambdas, that we currently
14518 : : treat as TU-local due to ABI reasons. In such a case the type of the
14519 : : lambda will be exposed in two places, first by the class type it is in
14520 : : the TYPE_FIELDS list of, and second by the actual member declaring that
14521 : : lambda. We only want the second case to warn. */
14522 : :
14523 : : static bool
14524 : 250 : is_exposure_of_member_type (depset *source, depset *ref)
14525 : : {
14526 : 250 : gcc_checking_assert (source->refs_tu_local (/*strict=*/true)
14527 : : && ref->is_tu_local (/*strict=*/true));
14528 : 250 : tree source_entity = STRIP_TEMPLATE (source->get_entity ());
14529 : 250 : tree ref_entity = STRIP_TEMPLATE (ref->get_entity ());
14530 : :
14531 : 250 : if (!source->is_tu_local (/*strict=*/true)
14532 : 238 : && source_entity
14533 : 238 : && ref_entity
14534 : 238 : && DECL_IMPLICIT_TYPEDEF_P (source_entity)
14535 : 2 : && DECL_IMPLICIT_TYPEDEF_P (ref_entity)
14536 : 2 : && DECL_CLASS_SCOPE_P (ref_entity)
14537 : 252 : && DECL_CONTEXT (ref_entity) == TREE_TYPE (source_entity))
14538 : : {
14539 : 4 : gcc_checking_assert (LAMBDA_TYPE_P (TREE_TYPE (ref_entity)));
14540 : : return true;
14541 : : }
14542 : : else
14543 : : return false;
14544 : : }
14545 : :
14546 : : /* DEP is a newly discovered dependency. Append it to current's
14547 : : depset. */
14548 : :
14549 : : void
14550 : 3677277 : depset::hash::add_dependency (depset *dep)
14551 : : {
14552 : 3677277 : gcc_checking_assert (current && !is_key_order ());
14553 : 3677277 : current->deps.safe_push (dep);
14554 : :
14555 : 3677277 : if (dep->is_tu_local (/*strict=*/true))
14556 : : {
14557 : 326 : if (dep->is_tu_local ())
14558 : 269 : current->set_flag_bit<DB_REF_PURVIEW_BIT> ();
14559 : : else
14560 : 57 : current->set_flag_bit<DB_REF_GLOBAL_BIT> ();
14561 : :
14562 : 326 : if (!ignore_tu_local && !is_exposure_of_member_type (current, dep))
14563 : : {
14564 : 130 : if (dep->is_tu_local ())
14565 : 94 : current->set_flag_bit<DB_EXPOSE_PURVIEW_BIT> ();
14566 : : else
14567 : 36 : current->set_flag_bit<DB_EXPOSE_GLOBAL_BIT> ();
14568 : : }
14569 : : }
14570 : :
14571 : 3677277 : if (current->get_entity_kind () == EK_USING
14572 : 13566 : && DECL_IMPLICIT_TYPEDEF_P (dep->get_entity ())
14573 : 3681172 : && TREE_CODE (TREE_TYPE (dep->get_entity ())) == ENUMERAL_TYPE)
14574 : : {
14575 : : /* CURRENT is an unwrapped using-decl and DECL is an enum's
14576 : : implicit typedef. Is CURRENT a member of the enum? */
14577 : 3766 : tree c_decl = OVL_FUNCTION (current->get_entity ());
14578 : :
14579 : 3766 : if (TREE_CODE (c_decl) == CONST_DECL
14580 : 7521 : && (current->deps[0]->get_entity ()
14581 : 3755 : == CP_DECL_CONTEXT (dep->get_entity ())))
14582 : : /* Make DECL depend on CURRENT. */
14583 : 3704 : dep->deps.safe_push (current);
14584 : : }
14585 : :
14586 : : /* If two dependencies recursively depend on each other existing within
14587 : : their own merge keys, we must ensure that the first dep we saw while
14588 : : walking is written first in this cluster. See sort_cluster for more
14589 : : details. */
14590 : 3677277 : if (writing_merge_key)
14591 : : {
14592 : 648129 : dep->set_flag_bit<DB_MAYBE_RECURSIVE_BIT> ();
14593 : 648129 : if (!current->is_maybe_recursive ())
14594 : 611797 : current->set_flag_bit<DB_ENTRY_BIT> ();
14595 : : }
14596 : :
14597 : 3677277 : if (dep->is_unreached ())
14598 : : {
14599 : : /* The dependency is reachable now. */
14600 : 282318 : reached_unreached = true;
14601 : 282318 : dep->clear_flag_bit<DB_UNREACHED_BIT> ();
14602 : 282318 : dump (dumper::DEPEND)
14603 : 30 : && dump ("Reaching unreached %s %C:%N", dep->entity_kind_name (),
14604 : 30 : TREE_CODE (dep->get_entity ()), dep->get_entity ());
14605 : : }
14606 : 3677277 : }
14607 : :
14608 : : depset *
14609 : 5525700 : depset::hash::add_dependency (tree decl, entity_kind ek)
14610 : : {
14611 : 5525700 : depset *dep;
14612 : :
14613 : 5525700 : if (is_key_order ())
14614 : : {
14615 : 1806746 : dep = find_dependency (decl);
14616 : 1806746 : if (dep)
14617 : : {
14618 : 892571 : current->deps.safe_push (dep);
14619 : 892571 : dump (dumper::MERGE)
14620 : 471 : && dump ("Key dependency on %s %C:%N found",
14621 : 471 : dep->entity_kind_name (), TREE_CODE (decl), decl);
14622 : : }
14623 : : else
14624 : : {
14625 : : /* It's not a mergeable decl, look for it in the original
14626 : : table. */
14627 : 914175 : dep = chain->find_dependency (decl);
14628 : 914175 : gcc_checking_assert (dep);
14629 : : }
14630 : : }
14631 : : else
14632 : : {
14633 : 3718954 : dep = make_dependency (decl, ek);
14634 : 3718954 : if (dep->get_entity_kind () != EK_REDIRECT)
14635 : 3677277 : add_dependency (dep);
14636 : : }
14637 : :
14638 : 5525700 : return dep;
14639 : : }
14640 : :
14641 : : void
14642 : 474175 : depset::hash::add_namespace_context (depset *dep, tree ns)
14643 : : {
14644 : 474175 : depset *ns_dep = make_dependency (ns, depset::EK_NAMESPACE);
14645 : 474175 : dep->deps.safe_push (ns_dep);
14646 : :
14647 : : /* Mark it as special if imported so we don't walk connect when
14648 : : SCCing. */
14649 : 474175 : if (!dep->is_binding () && ns_dep->is_import ())
14650 : 0 : dep->set_special ();
14651 : 474175 : }
14652 : :
14653 : : struct add_binding_data
14654 : : {
14655 : : tree ns;
14656 : : bitmap partitions;
14657 : : depset *binding;
14658 : : depset::hash *hash;
14659 : : bool met_namespace;
14660 : : };
14661 : :
14662 : : /* Return true if we are, or contain something that is exported. */
14663 : :
14664 : : bool
14665 : 5293980 : depset::hash::add_binding_entity (tree decl, WMB_Flags flags, void *data_)
14666 : : {
14667 : 5293980 : auto data = static_cast <add_binding_data *> (data_);
14668 : 5293980 : decl = strip_using_decl (decl);
14669 : :
14670 : 5293980 : if (!(TREE_CODE (decl) == NAMESPACE_DECL && !DECL_NAMESPACE_ALIAS (decl)))
14671 : : {
14672 : 5286543 : tree inner = decl;
14673 : :
14674 : 5286543 : if (TREE_CODE (inner) == CONST_DECL
14675 : 4981 : && TREE_CODE (DECL_CONTEXT (inner)) == ENUMERAL_TYPE
14676 : : /* A using-decl could make a CONST_DECL purview for a non-purview
14677 : : enumeration. */
14678 : 5291524 : && (!DECL_LANG_SPECIFIC (inner) || !DECL_MODULE_PURVIEW_P (inner)))
14679 : 4943 : inner = TYPE_NAME (DECL_CONTEXT (inner));
14680 : 5281600 : else if (TREE_CODE (inner) == TEMPLATE_DECL)
14681 : 94977 : inner = DECL_TEMPLATE_RESULT (inner);
14682 : :
14683 : 10411097 : if ((!DECL_LANG_SPECIFIC (inner) || !DECL_MODULE_PURVIEW_P (inner))
14684 : 10263929 : && !((flags & WMB_Using) && (flags & WMB_Purview)))
14685 : : /* Ignore entities not within the module purview. We'll need to
14686 : : create bindings for any non-discarded function calls for ADL,
14687 : : but it's simpler to handle that at the point of use rather
14688 : : than trying to clear out bindings after the fact. */
14689 : : return false;
14690 : :
14691 : 147282 : bool internal_decl = false;
14692 : 147282 : if (!header_module_p () && is_tu_local_entity (decl))
14693 : : {
14694 : : /* A TU-local entity. For ADL we still need to create bindings
14695 : : for internal-linkage functions attached to a named module. */
14696 : 120 : if (DECL_DECLARES_FUNCTION_P (inner)
14697 : 96 : && DECL_LANG_SPECIFIC (inner)
14698 : 312 : && DECL_MODULE_ATTACH_P (inner))
14699 : : {
14700 : 87 : gcc_checking_assert (!DECL_MODULE_EXPORT_P (inner));
14701 : : internal_decl = true;
14702 : : }
14703 : : else
14704 : : return false;
14705 : : }
14706 : :
14707 : 147153 : if ((TREE_CODE (decl) == VAR_DECL
14708 : 147153 : || TREE_CODE (decl) == TYPE_DECL)
14709 : 147153 : && DECL_TINFO_P (decl))
14710 : : /* Ignore TINFO things. */
14711 : : return false;
14712 : :
14713 : 147153 : if (TREE_CODE (decl) == VAR_DECL && DECL_NTTP_OBJECT_P (decl))
14714 : : /* Ignore NTTP objects. */
14715 : : return false;
14716 : :
14717 : 147153 : if (deduction_guide_p (decl))
14718 : : {
14719 : : /* Ignore deduction guides, bindings for them will be created within
14720 : : find_dependencies for their class template. But still build a dep
14721 : : for them so that we don't discard them. */
14722 : 1333 : data->hash->make_dependency (decl, EK_FOR_BINDING);
14723 : 1333 : return false;
14724 : : }
14725 : :
14726 : 145820 : if (!(flags & WMB_Using) && CP_DECL_CONTEXT (decl) != data->ns)
14727 : : {
14728 : : /* An unscoped enum constant implicitly brought into the containing
14729 : : namespace. We treat this like a using-decl. */
14730 : 3117 : gcc_checking_assert (TREE_CODE (decl) == CONST_DECL);
14731 : :
14732 : 3117 : flags = WMB_Flags (flags | WMB_Using);
14733 : 3117 : if (DECL_MODULE_EXPORT_P (TYPE_NAME (TREE_TYPE (decl)))
14734 : : /* A using-decl can make an enum constant exported for a
14735 : : non-exported enumeration. */
14736 : 3117 : || (DECL_LANG_SPECIFIC (decl) && DECL_MODULE_EXPORT_P (decl)))
14737 : 2895 : flags = WMB_Flags (flags | WMB_Export);
14738 : : }
14739 : :
14740 : 145820 : if (!data->binding)
14741 : : /* No binding to check. */;
14742 : 28684 : else if (flags & WMB_Using)
14743 : : {
14744 : : /* Look in the binding to see if we already have this
14745 : : using. */
14746 : 19136 : for (unsigned ix = data->binding->deps.length (); --ix;)
14747 : : {
14748 : 15076 : depset *d = data->binding->deps[ix];
14749 : 30152 : if (d->get_entity_kind () == EK_USING
14750 : 15076 : && OVL_FUNCTION (d->get_entity ()) == decl)
14751 : : {
14752 : 0 : if (!(flags & WMB_Hidden))
14753 : 0 : d->clear_hidden_binding ();
14754 : 0 : OVL_PURVIEW_P (d->get_entity ()) = true;
14755 : 0 : if (flags & WMB_Export)
14756 : 0 : OVL_EXPORT_P (d->get_entity ()) = true;
14757 : 0 : return bool (flags & WMB_Export);
14758 : : }
14759 : : }
14760 : : }
14761 : 26654 : else if (flags & WMB_Dups)
14762 : : {
14763 : : /* Look in the binding to see if we already have this decl. */
14764 : 78 : for (unsigned ix = data->binding->deps.length (); --ix;)
14765 : : {
14766 : 39 : depset *d = data->binding->deps[ix];
14767 : 39 : if (d->get_entity () == decl)
14768 : : {
14769 : 33 : if (!(flags & WMB_Hidden))
14770 : 30 : d->clear_hidden_binding ();
14771 : 33 : return false;
14772 : : }
14773 : : }
14774 : : }
14775 : :
14776 : : /* We're adding something. */
14777 : 145787 : if (!data->binding)
14778 : : {
14779 : 117136 : data->binding = make_binding (data->ns, DECL_NAME (decl));
14780 : 117136 : data->hash->add_namespace_context (data->binding, data->ns);
14781 : :
14782 : 117136 : depset **slot = data->hash->binding_slot (data->ns,
14783 : 117136 : DECL_NAME (decl), true);
14784 : 117136 : gcc_checking_assert (!*slot);
14785 : 117136 : *slot = data->binding;
14786 : : }
14787 : :
14788 : : /* Make sure nobody left a tree visited lying about. */
14789 : 145787 : gcc_checking_assert (!TREE_VISITED (decl));
14790 : :
14791 : 145787 : if (flags & WMB_Using)
14792 : : {
14793 : 13566 : decl = ovl_make (decl, NULL_TREE);
14794 : 13566 : OVL_USING_P (decl) = true;
14795 : 13566 : OVL_PURVIEW_P (decl) = true;
14796 : 13566 : if (flags & WMB_Export)
14797 : 12706 : OVL_EXPORT_P (decl) = true;
14798 : : }
14799 : :
14800 : 145787 : entity_kind ek = EK_FOR_BINDING;
14801 : 145787 : if (internal_decl)
14802 : : ek = EK_TU_LOCAL;
14803 : 145700 : else if (flags & WMB_Using)
14804 : 13566 : ek = EK_USING;
14805 : :
14806 : 145787 : depset *dep = data->hash->make_dependency (decl, ek);
14807 : 145787 : if (flags & WMB_Hidden)
14808 : 4749 : dep->set_hidden_binding ();
14809 : 145787 : data->binding->deps.safe_push (dep);
14810 : : /* Binding and contents are mutually dependent. */
14811 : 145787 : dep->deps.safe_push (data->binding);
14812 : :
14813 : 145787 : return (flags & WMB_Using
14814 : 145787 : ? flags & WMB_Export : DECL_MODULE_EXPORT_P (decl));
14815 : : }
14816 : 7437 : else if (!data->met_namespace)
14817 : : {
14818 : : /* Namespace, walk exactly once. */
14819 : 7428 : data->met_namespace = true;
14820 : 7428 : if (data->hash->add_namespace_entities (decl, data->partitions))
14821 : : {
14822 : : /* It contains an exported thing, so it is exported. */
14823 : 1415 : gcc_checking_assert (DECL_MODULE_PURVIEW_P (decl));
14824 : 1415 : gcc_checking_assert (TREE_PUBLIC (decl) || header_module_p ());
14825 : 1415 : DECL_MODULE_EXPORT_P (decl) = true;
14826 : : }
14827 : :
14828 : 7428 : if (DECL_MODULE_PURVIEW_P (decl))
14829 : : {
14830 : 1737 : data->hash->make_dependency (decl, depset::EK_NAMESPACE);
14831 : :
14832 : 1737 : return DECL_MODULE_EXPORT_P (decl);
14833 : : }
14834 : : }
14835 : :
14836 : : return false;
14837 : : }
14838 : :
14839 : : /* Recursively find all the namespace bindings of NS. Add a depset
14840 : : for every binding that contains an export or module-linkage entity.
14841 : : Add a defining depset for every such decl that we need to write a
14842 : : definition. Such defining depsets depend on the binding depset.
14843 : : Returns true if we contain something exported. */
14844 : :
14845 : : bool
14846 : 10001 : depset::hash::add_namespace_entities (tree ns, bitmap partitions)
14847 : : {
14848 : 11069 : dump () && dump ("Looking for writables in %N", ns);
14849 : 10001 : dump.indent ();
14850 : :
14851 : 10001 : unsigned count = 0;
14852 : 10001 : add_binding_data data;
14853 : 10001 : data.ns = ns;
14854 : 10001 : data.partitions = partitions;
14855 : 10001 : data.hash = this;
14856 : :
14857 : 13873507 : for (tree binding : *DECL_NAMESPACE_BINDINGS (ns))
14858 : : {
14859 : 6931753 : data.binding = nullptr;
14860 : 6931753 : data.met_namespace = false;
14861 : 6931753 : if (walk_module_binding (binding, partitions, add_binding_entity, &data))
14862 : 109526 : count++;
14863 : : }
14864 : :
14865 : : /* Seed any using-directives so that we emit the relevant namespaces. */
14866 : 10532 : for (tree udir : NAMESPACE_LEVEL (ns)->using_directives)
14867 : 183 : if (TREE_CODE (udir) == USING_DECL && DECL_MODULE_PURVIEW_P (udir))
14868 : : {
14869 : 147 : make_dependency (USING_DECL_DECLS (udir), depset::EK_NAMESPACE);
14870 : 147 : if (DECL_MODULE_EXPORT_P (udir))
14871 : 85 : count++;
14872 : : }
14873 : :
14874 : 10001 : if (count)
14875 : 3575 : dump () && dump ("Found %u entries", count);
14876 : 10001 : dump.outdent ();
14877 : :
14878 : 10001 : return count != 0;
14879 : : }
14880 : :
14881 : : void
14882 : 200 : depset::hash::add_partial_entities (vec<tree, va_gc> *partial_classes)
14883 : : {
14884 : 15758 : for (unsigned ix = 0; ix != partial_classes->length (); ix++)
14885 : : {
14886 : 15558 : tree inner = (*partial_classes)[ix];
14887 : :
14888 : 15558 : depset *dep = make_dependency (inner, depset::EK_DECL);
14889 : :
14890 : 15558 : if (dep->get_entity_kind () == depset::EK_REDIRECT)
14891 : : {
14892 : 15558 : dep = dep->deps[0];
14893 : : /* We should have recorded the template as a partial
14894 : : specialization. */
14895 : 15558 : gcc_checking_assert (dep->get_entity_kind ()
14896 : : == depset::EK_PARTIAL);
14897 : :
14898 : : /* Only emit GM entities if reached. */
14899 : 15558 : if (!DECL_LANG_SPECIFIC (inner)
14900 : 26186 : || !DECL_MODULE_PURVIEW_P (inner))
14901 : 4988 : dep->set_flag_bit<DB_UNREACHED_BIT> ();
14902 : : }
14903 : : else
14904 : : {
14905 : : /* It was an explicit specialization, not a partial one.
14906 : : We should have already added this. */
14907 : 0 : gcc_checking_assert (dep->get_entity_kind ()
14908 : : == depset::EK_SPECIALIZATION);
14909 : 0 : gcc_checking_assert (dep->is_special ());
14910 : : }
14911 : : }
14912 : 200 : }
14913 : :
14914 : : /* Add the members of imported classes that we defined in this TU.
14915 : : This will also include lazily created implicit member function
14916 : : declarations. (All others will be definitions.) */
14917 : :
14918 : : void
14919 : 12 : depset::hash::add_class_entities (vec<tree, va_gc> *class_members)
14920 : : {
14921 : 24 : for (unsigned ix = 0; ix != class_members->length (); ix++)
14922 : : {
14923 : 12 : tree defn = (*class_members)[ix];
14924 : 12 : depset *dep = make_dependency (defn, EK_INNER_DECL);
14925 : :
14926 : 12 : if (dep->get_entity_kind () == EK_REDIRECT)
14927 : 0 : dep = dep->deps[0];
14928 : :
14929 : : /* Only non-instantiations need marking as pendings. */
14930 : 24 : if (dep->get_entity_kind () == EK_DECL)
14931 : 12 : dep->set_flag_bit <DB_IS_PENDING_BIT> ();
14932 : : }
14933 : 12 : }
14934 : :
14935 : : /* We add the partial & explicit specializations, and the explicit
14936 : : instantiations. */
14937 : :
14938 : : static void
14939 : 613230 : specialization_add (bool decl_p, spec_entry *entry, void *data_)
14940 : : {
14941 : 613230 : vec<spec_entry *> *data = reinterpret_cast <vec<spec_entry *> *> (data_);
14942 : :
14943 : 613230 : if (!decl_p)
14944 : : {
14945 : : /* We exclusively use decls to locate things. Make sure there's
14946 : : no mismatch between the two specialization tables we keep.
14947 : : pt.cc optimizes instantiation lookup using a complicated
14948 : : heuristic. We don't attempt to replicate that algorithm, but
14949 : : observe its behaviour and reproduce it upon read back. */
14950 : :
14951 : 194121 : gcc_checking_assert (TREE_CODE (entry->spec) == ENUMERAL_TYPE
14952 : : || DECL_CLASS_TEMPLATE_P (entry->tmpl));
14953 : :
14954 : 194121 : gcc_checking_assert (!match_mergeable_specialization (true, entry));
14955 : : }
14956 : 419109 : else if (VAR_OR_FUNCTION_DECL_P (entry->spec))
14957 : 205575 : gcc_checking_assert (!DECL_LOCAL_DECL_P (entry->spec));
14958 : :
14959 : 613230 : data->safe_push (entry);
14960 : 613230 : }
14961 : :
14962 : : /* Arbitrary stable comparison. */
14963 : :
14964 : : static int
14965 : 36299980 : specialization_cmp (const void *a_, const void *b_)
14966 : : {
14967 : 36299980 : const spec_entry *ea = *reinterpret_cast<const spec_entry *const *> (a_);
14968 : 36299980 : const spec_entry *eb = *reinterpret_cast<const spec_entry *const *> (b_);
14969 : :
14970 : 36299980 : if (ea == eb)
14971 : : return 0;
14972 : :
14973 : 36299980 : tree a = ea->spec;
14974 : 36299980 : tree b = eb->spec;
14975 : 36299980 : if (TYPE_P (a))
14976 : : {
14977 : 10912882 : a = TYPE_NAME (a);
14978 : 10912882 : b = TYPE_NAME (b);
14979 : : }
14980 : :
14981 : 36299980 : if (a == b)
14982 : : /* This can happen with friend specializations. Just order by
14983 : : entry address. See note in depset_cmp. */
14984 : 157 : return ea < eb ? -1 : +1;
14985 : :
14986 : 36299863 : return DECL_UID (a) < DECL_UID (b) ? -1 : +1;
14987 : : }
14988 : :
14989 : : /* We add all kinds of specialializations. Implicit specializations
14990 : : should only streamed and walked if they are reachable from
14991 : : elsewhere. Hence the UNREACHED flag. This is making the
14992 : : assumption that it is cheaper to reinstantiate them on demand
14993 : : elsewhere, rather than stream them in when we instantiate their
14994 : : general template. Also, if we do stream them, we can only do that
14995 : : if they are not internal (which they can become if they themselves
14996 : : touch an internal entity?). */
14997 : :
14998 : : void
14999 : 5146 : depset::hash::add_specializations (bool decl_p)
15000 : : {
15001 : 5146 : vec<spec_entry *> data;
15002 : 5146 : data.create (100);
15003 : 5146 : walk_specializations (decl_p, specialization_add, &data);
15004 : 5146 : data.qsort (specialization_cmp);
15005 : 618376 : while (data.length ())
15006 : : {
15007 : 613230 : spec_entry *entry = data.pop ();
15008 : 613230 : tree spec = entry->spec;
15009 : 613230 : int use_tpl = 0;
15010 : 613230 : bool is_friend = false;
15011 : :
15012 : 613230 : if (decl_p && DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (entry->tmpl))
15013 : : /* A friend of a template. This is keyed to the
15014 : : instantiation. */
15015 : : is_friend = true;
15016 : :
15017 : 613230 : if (decl_p)
15018 : : {
15019 : 419109 : if (tree ti = DECL_TEMPLATE_INFO (spec))
15020 : : {
15021 : 419109 : tree tmpl = TI_TEMPLATE (ti);
15022 : :
15023 : 419109 : use_tpl = DECL_USE_TEMPLATE (spec);
15024 : 419109 : if (spec == DECL_TEMPLATE_RESULT (tmpl))
15025 : : {
15026 : 2806 : spec = tmpl;
15027 : 2806 : gcc_checking_assert (DECL_USE_TEMPLATE (spec) == use_tpl);
15028 : : }
15029 : 416303 : else if (is_friend)
15030 : : {
15031 : 2966 : if (TI_TEMPLATE (ti) != entry->tmpl
15032 : 2966 : || !template_args_equal (TI_ARGS (ti), entry->tmpl))
15033 : 2966 : goto template_friend;
15034 : : }
15035 : : }
15036 : : else
15037 : : {
15038 : 0 : template_friend:;
15039 : 2966 : gcc_checking_assert (is_friend);
15040 : : /* This is a friend of a template class, but not the one
15041 : : that generated entry->spec itself (i.e. it's an
15042 : : equivalent clone). We do not need to record
15043 : : this. */
15044 : 2966 : continue;
15045 : : }
15046 : : }
15047 : : else
15048 : : {
15049 : 194121 : if (TREE_CODE (spec) == ENUMERAL_TYPE)
15050 : : {
15051 : 1050 : tree ctx = DECL_CONTEXT (TYPE_NAME (spec));
15052 : :
15053 : 1050 : if (TYPE_P (ctx))
15054 : 1044 : use_tpl = CLASSTYPE_USE_TEMPLATE (ctx);
15055 : : else
15056 : 6 : use_tpl = DECL_USE_TEMPLATE (ctx);
15057 : : }
15058 : : else
15059 : 193071 : use_tpl = CLASSTYPE_USE_TEMPLATE (spec);
15060 : :
15061 : 194121 : tree ti = TYPE_TEMPLATE_INFO (spec);
15062 : 194121 : tree tmpl = TI_TEMPLATE (ti);
15063 : :
15064 : 194121 : spec = TYPE_NAME (spec);
15065 : 194121 : if (spec == DECL_TEMPLATE_RESULT (tmpl))
15066 : : {
15067 : 782 : spec = tmpl;
15068 : 782 : use_tpl = DECL_USE_TEMPLATE (spec);
15069 : : }
15070 : : }
15071 : :
15072 : 610264 : bool needs_reaching = false;
15073 : 610264 : if (use_tpl == 1)
15074 : : /* Implicit instantiations only walked if we reach them. */
15075 : : needs_reaching = true;
15076 : 58385 : else if (!DECL_LANG_SPECIFIC (STRIP_TEMPLATE (spec))
15077 : 106087 : || !DECL_MODULE_PURVIEW_P (STRIP_TEMPLATE (spec)))
15078 : : /* Likewise, GMF explicit or partial specializations. */
15079 : : needs_reaching = true;
15080 : :
15081 : : #if false && CHECKING_P
15082 : : /* The instantiation isn't always on
15083 : : DECL_TEMPLATE_INSTANTIATIONS, */
15084 : : // FIXME: we probably need to remember this information?
15085 : : /* Verify the specialization is on the
15086 : : DECL_TEMPLATE_INSTANTIATIONS of the template. */
15087 : : for (tree cons = DECL_TEMPLATE_INSTANTIATIONS (entry->tmpl);
15088 : : cons; cons = TREE_CHAIN (cons))
15089 : : if (TREE_VALUE (cons) == entry->spec)
15090 : : {
15091 : : gcc_assert (entry->args == TREE_PURPOSE (cons));
15092 : : goto have_spec;
15093 : : }
15094 : : gcc_unreachable ();
15095 : : have_spec:;
15096 : : #endif
15097 : :
15098 : : /* Make sure nobody left a tree visited lying about. */
15099 : 610264 : gcc_checking_assert (!TREE_VISITED (spec));
15100 : 610264 : depset *dep = make_dependency (spec, depset::EK_SPECIALIZATION);
15101 : 610264 : if (dep->is_special ())
15102 : 0 : gcc_unreachable ();
15103 : : else
15104 : : {
15105 : 610264 : if (dep->get_entity_kind () == depset::EK_REDIRECT)
15106 : 17554 : dep = dep->deps[0];
15107 : 592710 : else if (dep->get_entity_kind () == depset::EK_SPECIALIZATION)
15108 : : {
15109 : 592710 : dep->set_special ();
15110 : 592710 : dep->deps.safe_push (reinterpret_cast<depset *> (entry));
15111 : 592710 : if (!decl_p)
15112 : 179252 : dep->set_flag_bit<DB_TYPE_SPEC_BIT> ();
15113 : : }
15114 : :
15115 : 610264 : if (needs_reaching)
15116 : 569996 : dep->set_flag_bit<DB_UNREACHED_BIT> ();
15117 : 610264 : if (is_friend)
15118 : 0 : dep->set_flag_bit<DB_FRIEND_SPEC_BIT> ();
15119 : : }
15120 : : }
15121 : 5146 : data.release ();
15122 : 5146 : }
15123 : :
15124 : : /* Add a depset into the mergeable hash. */
15125 : :
15126 : : void
15127 : 791738 : depset::hash::add_mergeable (depset *mergeable)
15128 : : {
15129 : 791738 : gcc_checking_assert (is_key_order ());
15130 : 791738 : entity_kind ek = mergeable->get_entity_kind ();
15131 : 791738 : tree decl = mergeable->get_entity ();
15132 : 791738 : gcc_checking_assert (ek < EK_DIRECT_HWM);
15133 : :
15134 : 791738 : depset **slot = entity_slot (decl, true);
15135 : 791738 : gcc_checking_assert (!*slot);
15136 : 791738 : depset *dep = make_entity (decl, ek);
15137 : 791738 : *slot = dep;
15138 : :
15139 : 791738 : worklist.safe_push (dep);
15140 : :
15141 : : /* So we can locate the mergeable depset this depset refers to,
15142 : : mark the first dep. */
15143 : 791738 : dep->set_special ();
15144 : 791738 : dep->deps.safe_push (mergeable);
15145 : 791738 : }
15146 : :
15147 : : /* Find the innermost-namespace scope of DECL, and that
15148 : : namespace-scope decl. */
15149 : :
15150 : : tree
15151 : 25908550 : find_pending_key (tree decl, tree *decl_p = nullptr)
15152 : : {
15153 : 25908550 : tree ns = decl;
15154 : 30968511 : do
15155 : : {
15156 : 30968511 : decl = ns;
15157 : 30968511 : ns = CP_DECL_CONTEXT (ns);
15158 : 30968511 : if (TYPE_P (ns))
15159 : 3201185 : ns = TYPE_NAME (ns);
15160 : : }
15161 : 30968511 : while (TREE_CODE (ns) != NAMESPACE_DECL);
15162 : :
15163 : 25908550 : if (decl_p)
15164 : 25555439 : *decl_p = decl;
15165 : :
15166 : 25908550 : return ns;
15167 : : }
15168 : :
15169 : : /* Creates bindings and dependencies for all deduction guides of
15170 : : the given class template DECL as needed. */
15171 : :
15172 : : void
15173 : 40043 : depset::hash::add_deduction_guides (tree decl)
15174 : : {
15175 : : /* Alias templates never have deduction guides. */
15176 : 40043 : if (DECL_ALIAS_TEMPLATE_P (decl))
15177 : 39446 : return;
15178 : :
15179 : : /* We don't need to do anything for class-scope deduction guides,
15180 : : as they will be added as members anyway. */
15181 : 40043 : if (!DECL_NAMESPACE_SCOPE_P (decl))
15182 : : return;
15183 : :
15184 : 32020 : tree ns = CP_DECL_CONTEXT (decl);
15185 : 32020 : tree name = dguide_name (decl);
15186 : :
15187 : : /* We always add all deduction guides with a given name at once,
15188 : : so if there's already a binding there's nothing to do. */
15189 : 32020 : if (find_binding (ns, name))
15190 : : return;
15191 : :
15192 : 30409 : tree guides = lookup_qualified_name (ns, name, LOOK_want::NORMAL,
15193 : : /*complain=*/false);
15194 : 30409 : if (guides == error_mark_node)
15195 : : return;
15196 : :
15197 : 597 : depset *binding = nullptr;
15198 : 2651 : for (tree t : lkp_range (guides))
15199 : : {
15200 : 1457 : gcc_checking_assert (!TREE_VISITED (t));
15201 : 1457 : depset *dep = make_dependency (t, EK_FOR_BINDING);
15202 : :
15203 : : /* We don't want to create bindings for imported deduction guides, as
15204 : : this would potentially cause name lookup to return duplicates. */
15205 : 1457 : if (dep->is_import ())
15206 : 6 : continue;
15207 : :
15208 : 1451 : if (!binding)
15209 : : {
15210 : : /* We have bindings to add. */
15211 : 591 : binding = make_binding (ns, name);
15212 : 591 : add_namespace_context (binding, ns);
15213 : :
15214 : 591 : depset **slot = binding_slot (ns, name, /*insert=*/true);
15215 : 591 : *slot = binding;
15216 : : }
15217 : :
15218 : 1451 : binding->deps.safe_push (dep);
15219 : 1451 : dep->deps.safe_push (binding);
15220 : 1451 : dump (dumper::DEPEND)
15221 : 0 : && dump ("Built binding for deduction guide %C:%N",
15222 : 0 : TREE_CODE (decl), decl);
15223 : : }
15224 : : }
15225 : :
15226 : : /* Iteratively find dependencies. During the walk we may find more
15227 : : entries on the same binding that need walking. */
15228 : :
15229 : : void
15230 : 225578 : depset::hash::find_dependencies (module_state *module)
15231 : : {
15232 : 225578 : trees_out walker (NULL, module, *this);
15233 : 225578 : vec<depset *> unreached;
15234 : 451156 : unreached.create (worklist.length ());
15235 : :
15236 : 1060 : for (;;)
15237 : : {
15238 : 226638 : reached_unreached = false;
15239 : 3418448 : while (worklist.length ())
15240 : : {
15241 : 3191810 : depset *item = worklist.pop ();
15242 : :
15243 : 3191810 : gcc_checking_assert (!item->is_binding ());
15244 : 3191810 : if (item->is_unreached ())
15245 : 1589681 : unreached.quick_push (item);
15246 : : else
15247 : : {
15248 : 1602129 : current = item;
15249 : 1602129 : tree decl = current->get_entity ();
15250 : 1602129 : dump (is_key_order () ? dumper::MERGE : dumper::DEPEND)
15251 : 1603182 : && dump ("Dependencies of %s %C:%N",
15252 : 1053 : is_key_order () ? "key-order"
15253 : 1053 : : current->entity_kind_name (), TREE_CODE (decl), decl);
15254 : 1602129 : dump.indent ();
15255 : 1602129 : walker.begin ();
15256 : 1602129 : if (current->get_entity_kind () == EK_USING)
15257 : 13566 : walker.tree_node (OVL_FUNCTION (decl));
15258 : 1588563 : else if (current->get_entity_kind () == EK_TU_LOCAL)
15259 : : /* We only stream its name and location. */
15260 : 87 : module->note_location (DECL_SOURCE_LOCATION (decl));
15261 : 1588476 : else if (TREE_VISITED (decl))
15262 : : /* A global tree. */;
15263 : 1586142 : else if (current->get_entity_kind () == EK_NAMESPACE)
15264 : : {
15265 : 2094 : module->note_location (DECL_SOURCE_LOCATION (decl));
15266 : 2094 : add_namespace_context (current, CP_DECL_CONTEXT (decl));
15267 : : }
15268 : : else
15269 : : {
15270 : 1584048 : walker.mark_declaration (decl, current->has_defn ());
15271 : :
15272 : 1584048 : if (!is_key_order ()
15273 : 1584048 : && item->is_pending_entity ())
15274 : : {
15275 : 353111 : tree ns = find_pending_key (decl, nullptr);
15276 : 353111 : add_namespace_context (item, ns);
15277 : : }
15278 : :
15279 : 1584048 : walker.decl_value (decl, current);
15280 : 1584048 : if (current->has_defn ())
15281 : 307217 : walker.write_definition (decl, current->refs_tu_local ());
15282 : : }
15283 : 1602129 : walker.end ();
15284 : :
15285 : : /* If we see either a class template or a deduction guide, make
15286 : : sure to add all visible deduction guides. We need to check
15287 : : both in case they have been added in separate modules, or
15288 : : one is in the GMF and would have otherwise been discarded. */
15289 : 1602129 : if (!is_key_order ()
15290 : 1602129 : && DECL_CLASS_TEMPLATE_P (decl))
15291 : 38586 : add_deduction_guides (decl);
15292 : 1602129 : if (!is_key_order ()
15293 : 1602129 : && deduction_guide_p (decl))
15294 : 1457 : add_deduction_guides (TYPE_NAME (TREE_TYPE (TREE_TYPE (decl))));
15295 : :
15296 : 1602129 : if (!is_key_order ()
15297 : 810391 : && TREE_CODE (decl) == TEMPLATE_DECL
15298 : 1880967 : && !DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (decl))
15299 : : {
15300 : : /* Mark all the explicit & partial specializations as
15301 : : reachable. We search both specialization lists as some
15302 : : constrained partial specializations for class types are
15303 : : only found in DECL_TEMPLATE_SPECIALIZATIONS. */
15304 : 790798 : auto mark_reached = [this](tree spec)
15305 : : {
15306 : 516966 : if (TYPE_P (spec))
15307 : 156705 : spec = TYPE_NAME (spec);
15308 : 516966 : int use_tpl;
15309 : 516966 : node_template_info (spec, use_tpl);
15310 : 516966 : if (use_tpl & 2)
15311 : : {
15312 : 60468 : depset *spec_dep = find_dependency (spec);
15313 : 60468 : if (spec_dep->get_entity_kind () == EK_REDIRECT)
15314 : 13737 : spec_dep = spec_dep->deps[0];
15315 : 60468 : if (spec_dep->is_unreached ())
15316 : : {
15317 : 5368 : reached_unreached = true;
15318 : 5368 : spec_dep->clear_flag_bit<DB_UNREACHED_BIT> ();
15319 : 5368 : dump (dumper::DEPEND)
15320 : 0 : && dump ("Reaching unreached specialization"
15321 : 0 : " %C:%N", TREE_CODE (spec), spec);
15322 : : }
15323 : : }
15324 : 790798 : };
15325 : :
15326 : 273832 : for (tree cons = DECL_TEMPLATE_INSTANTIATIONS (decl);
15327 : 776442 : cons; cons = TREE_CHAIN (cons))
15328 : 502610 : mark_reached (TREE_VALUE (cons));
15329 : 273832 : for (tree cons = DECL_TEMPLATE_SPECIALIZATIONS (decl);
15330 : 288188 : cons; cons = TREE_CHAIN (cons))
15331 : 14356 : mark_reached (TREE_VALUE (cons));
15332 : : }
15333 : :
15334 : 1602129 : dump.outdent ();
15335 : 1602129 : current = NULL;
15336 : : }
15337 : : }
15338 : :
15339 : 226638 : if (!reached_unreached)
15340 : : break;
15341 : :
15342 : : /* It's possible the we reached the unreached before we
15343 : : processed it in the above loop, so we'll be doing this an
15344 : : extra time. However, to avoid that we have to do some
15345 : : bit shuffling that also involves a scan of the list.
15346 : : Swings & roundabouts I guess. */
15347 : 1060 : std::swap (worklist, unreached);
15348 : : }
15349 : :
15350 : 225578 : unreached.release ();
15351 : 225578 : }
15352 : :
15353 : : /* Compare two entries of a single binding. TYPE_DECL before
15354 : : non-exported before exported. */
15355 : :
15356 : : static int
15357 : 438197 : binding_cmp (const void *a_, const void *b_)
15358 : : {
15359 : 438197 : depset *a = *(depset *const *)a_;
15360 : 438197 : depset *b = *(depset *const *)b_;
15361 : :
15362 : 438197 : tree a_ent = a->get_entity ();
15363 : 438197 : tree b_ent = b->get_entity ();
15364 : 438197 : gcc_checking_assert (a_ent != b_ent
15365 : : && !a->is_binding ()
15366 : : && !b->is_binding ());
15367 : :
15368 : : /* Implicit typedefs come first. */
15369 : 438197 : bool a_implicit = DECL_IMPLICIT_TYPEDEF_P (a_ent);
15370 : 438197 : bool b_implicit = DECL_IMPLICIT_TYPEDEF_P (b_ent);
15371 : 438089 : if (a_implicit || b_implicit)
15372 : : {
15373 : : /* A binding with two implicit type decls? That's unpossible! */
15374 : 216 : gcc_checking_assert (!(a_implicit && b_implicit));
15375 : 324 : return a_implicit ? -1 : +1; /* Implicit first. */
15376 : : }
15377 : :
15378 : : /* TU-local before non-TU-local. */
15379 : 437981 : bool a_internal = a->get_entity_kind () == depset::EK_TU_LOCAL;
15380 : 437981 : bool b_internal = b->get_entity_kind () == depset::EK_TU_LOCAL;
15381 : 437981 : if (a_internal != b_internal)
15382 : 0 : return a_internal ? -1 : +1; /* Internal first. */
15383 : :
15384 : : /* Hidden before non-hidden. */
15385 : 437981 : bool a_hidden = a->is_hidden ();
15386 : 437981 : bool b_hidden = b->is_hidden ();
15387 : 437981 : if (a_hidden != b_hidden)
15388 : 49621 : return a_hidden ? -1 : +1;
15389 : :
15390 : 405446 : bool a_using = a->get_entity_kind () == depset::EK_USING;
15391 : 405446 : bool a_export;
15392 : 405446 : if (a_using)
15393 : : {
15394 : 27717 : a_export = OVL_EXPORT_P (a_ent);
15395 : 27717 : a_ent = OVL_FUNCTION (a_ent);
15396 : : }
15397 : 377729 : else if (TREE_CODE (a_ent) == CONST_DECL
15398 : 0 : && DECL_LANG_SPECIFIC (a_ent)
15399 : 377729 : && DECL_MODULE_EXPORT_P (a_ent))
15400 : : a_export = true;
15401 : : else
15402 : 377729 : a_export = DECL_MODULE_EXPORT_P (TREE_CODE (a_ent) == CONST_DECL
15403 : : ? TYPE_NAME (TREE_TYPE (a_ent))
15404 : : : STRIP_TEMPLATE (a_ent));
15405 : :
15406 : 405446 : bool b_using = b->get_entity_kind () == depset::EK_USING;
15407 : 405446 : bool b_export;
15408 : 405446 : if (b_using)
15409 : : {
15410 : 24248 : b_export = OVL_EXPORT_P (b_ent);
15411 : 24248 : b_ent = OVL_FUNCTION (b_ent);
15412 : : }
15413 : 381198 : else if (TREE_CODE (b_ent) == CONST_DECL
15414 : 0 : && DECL_LANG_SPECIFIC (b_ent)
15415 : 381198 : && DECL_MODULE_EXPORT_P (b_ent))
15416 : : b_export = true;
15417 : : else
15418 : 381198 : b_export = DECL_MODULE_EXPORT_P (TREE_CODE (b_ent) == CONST_DECL
15419 : : ? TYPE_NAME (TREE_TYPE (b_ent))
15420 : : : STRIP_TEMPLATE (b_ent));
15421 : :
15422 : : /* Non-exports before exports. */
15423 : 405446 : if (a_export != b_export)
15424 : 360 : return a_export ? +1 : -1;
15425 : :
15426 : : /* At this point we don't care, but want a stable sort. */
15427 : :
15428 : 405236 : if (a_using != b_using)
15429 : : /* using first. */
15430 : 20026 : return a_using? -1 : +1;
15431 : :
15432 : 390702 : return DECL_UID (a_ent) < DECL_UID (b_ent) ? -1 : +1;
15433 : : }
15434 : :
15435 : : /* True iff TMPL has an explicit instantiation definition.
15436 : :
15437 : : This is local to module.cc because register_specialization skips adding most
15438 : : instantiations unless module_maybe_has_cmi_p. */
15439 : :
15440 : : static bool
15441 : 73 : template_has_explicit_inst (tree tmpl)
15442 : : {
15443 : 85 : for (tree t = DECL_TEMPLATE_INSTANTIATIONS (tmpl); t; t = TREE_CHAIN (t))
15444 : : {
15445 : 24 : tree spec = TREE_VALUE (t);
15446 : 24 : if (DECL_EXPLICIT_INSTANTIATION (spec)
15447 : 24 : && !DECL_REALLY_EXTERN (spec))
15448 : : return true;
15449 : : }
15450 : : return false;
15451 : : }
15452 : :
15453 : : /* Complain about DEP that exposes a TU-local entity.
15454 : :
15455 : : If STRICT, DEP only referenced entities from the GMF. Returns TRUE
15456 : : if we explained anything. */
15457 : :
15458 : : bool
15459 : 127 : depset::hash::diagnose_bad_internal_ref (depset *dep, bool strict)
15460 : : {
15461 : 127 : tree decl = dep->get_entity ();
15462 : :
15463 : : /* Don't need to walk if we're not going to be emitting
15464 : : any diagnostics anyway. */
15465 : 148 : if (strict && !warning_enabled_at (DECL_SOURCE_LOCATION (decl),
15466 : 21 : OPT_Wexpose_global_module_tu_local))
15467 : : return false;
15468 : :
15469 : 520 : for (depset *rdep : dep->deps)
15470 : 132 : if (!rdep->is_binding () && rdep->is_tu_local (strict)
15471 : 366 : && !is_exposure_of_member_type (dep, rdep))
15472 : : {
15473 : : // FIXME:QOI Better location information? We're
15474 : : // losing, so it doesn't matter about efficiency.
15475 : 118 : tree exposed = rdep->get_entity ();
15476 : 118 : auto_diagnostic_group d;
15477 : 118 : if (strict)
15478 : : {
15479 : : /* Allow suppressing the warning from the point of declaration
15480 : : of the otherwise-exposed decl, for cases we know that
15481 : : exposures will never be 'bad'. */
15482 : 27 : if (warning_enabled_at (DECL_SOURCE_LOCATION (exposed),
15483 : 27 : OPT_Wexpose_global_module_tu_local)
15484 : 45 : && pedwarn (DECL_SOURCE_LOCATION (decl),
15485 : 18 : OPT_Wexpose_global_module_tu_local,
15486 : : "%qD exposes TU-local entity %qD", decl, exposed))
15487 : : {
15488 : 18 : bool informed = is_tu_local_entity (exposed, /*explain=*/true);
15489 : 18 : gcc_checking_assert (informed);
15490 : : return true;
15491 : : }
15492 : : }
15493 : : else
15494 : : {
15495 : 91 : error_at (DECL_SOURCE_LOCATION (decl),
15496 : : "%qD exposes TU-local entity %qD", decl, exposed);
15497 : 91 : bool informed = is_tu_local_entity (exposed, /*explain=*/true);
15498 : 91 : gcc_checking_assert (informed);
15499 : 91 : if (dep->is_tu_local (/*strict=*/true))
15500 : 3 : inform (DECL_SOURCE_LOCATION (decl),
15501 : : "%qD is also TU-local but has been exposed elsewhere",
15502 : : decl);
15503 : 91 : return true;
15504 : : }
15505 : 118 : }
15506 : :
15507 : : return false;
15508 : : }
15509 : :
15510 : : /* Warn about a template DEP that references a TU-local entity.
15511 : :
15512 : : If STRICT, DEP only referenced entities from the GMF. Returns TRUE
15513 : : if we explained anything. */
15514 : :
15515 : : bool
15516 : 91 : depset::hash::diagnose_template_names_tu_local (depset *dep, bool strict)
15517 : : {
15518 : 91 : tree decl = dep->get_entity ();
15519 : :
15520 : : /* Don't bother walking if we know we won't be emitting anything. */
15521 : 91 : if (!warning_enabled_at (DECL_SOURCE_LOCATION (decl),
15522 : 91 : OPT_Wtemplate_names_tu_local)
15523 : : /* Only warn strictly if users haven't silenced this warning here. */
15524 : 118 : || (strict && !warning_enabled_at (DECL_SOURCE_LOCATION (decl),
15525 : 27 : OPT_Wexpose_global_module_tu_local)))
15526 : 0 : return false;
15527 : :
15528 : : /* Friend decls in a class body are ignored, but this is harmless:
15529 : : it should not impact any consumers. */
15530 : 91 : if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (decl)))
15531 : : return false;
15532 : :
15533 : : /* We should now only be warning about templates. */
15534 : 73 : gcc_checking_assert
15535 : : (TREE_CODE (decl) == TEMPLATE_DECL
15536 : : && VAR_OR_FUNCTION_DECL_P (DECL_TEMPLATE_RESULT (decl)));
15537 : :
15538 : : /* Don't warn if we've seen any explicit instantiation definitions,
15539 : : the intent might be for importers to only use those. */
15540 : 73 : if (template_has_explicit_inst (decl))
15541 : : return false;
15542 : :
15543 : 256 : for (depset *rdep : dep->deps)
15544 : 128 : if (!rdep->is_binding () && rdep->is_tu_local (strict))
15545 : : {
15546 : 64 : tree ref = rdep->get_entity ();
15547 : 64 : auto_diagnostic_group d;
15548 : 64 : if (strict)
15549 : : {
15550 : 15 : if (warning_enabled_at (DECL_SOURCE_LOCATION (ref),
15551 : 15 : OPT_Wexpose_global_module_tu_local)
15552 : 21 : && warning_at (DECL_SOURCE_LOCATION (decl),
15553 : 6 : OPT_Wtemplate_names_tu_local,
15554 : : "%qD refers to TU-local entity %qD, which may "
15555 : : "cause issues when instantiating in other TUs",
15556 : : decl, ref))
15557 : : {
15558 : 6 : is_tu_local_entity (ref, /*explain=*/true);
15559 : 6 : return true;
15560 : : }
15561 : : }
15562 : 49 : else if (warning_at (DECL_SOURCE_LOCATION (decl),
15563 : 49 : OPT_Wtemplate_names_tu_local,
15564 : : "%qD refers to TU-local entity %qD and cannot "
15565 : : "be instantiated in other TUs", decl, ref))
15566 : : {
15567 : 49 : is_tu_local_entity (ref, /*explain=*/true);
15568 : 49 : return true;
15569 : : }
15570 : 64 : }
15571 : :
15572 : : return false;
15573 : : }
15574 : :
15575 : : /* Sort the bindings, issue errors about bad internal refs. */
15576 : :
15577 : : bool
15578 : 2573 : depset::hash::finalize_dependencies ()
15579 : : {
15580 : 2573 : bool ok = true;
15581 : 2464135 : for (depset *dep : *this)
15582 : : {
15583 : 1230781 : if (dep->is_binding ())
15584 : : {
15585 : : /* Keep the containing namespace dep first. */
15586 : 118970 : gcc_checking_assert (dep->deps.length () > 1
15587 : : && (dep->deps[0]->get_entity_kind ()
15588 : : == EK_NAMESPACE)
15589 : : && (dep->deps[0]->get_entity ()
15590 : : == dep->get_entity ()));
15591 : 118970 : if (dep->deps.length () > 2)
15592 : 8839 : gcc_qsort (&dep->deps[1], dep->deps.length () - 1,
15593 : : sizeof (dep->deps[1]), binding_cmp);
15594 : :
15595 : : /* Bindings shouldn't refer to imported entities. */
15596 : 118970 : if (CHECKING_P)
15597 : 625704 : for (depset *entity : dep->deps)
15598 : 268794 : gcc_checking_assert (!entity->is_import ());
15599 : 118970 : continue;
15600 : 118970 : }
15601 : :
15602 : : /* Otherwise, we'll check for bad internal refs.
15603 : : Don't complain about any references from TU-local entities. */
15604 : 1111811 : if (dep->is_tu_local ())
15605 : 267 : continue;
15606 : :
15607 : 1111544 : if (dep->is_exposure ())
15608 : : {
15609 : 106 : bool explained = diagnose_bad_internal_ref (dep);
15610 : :
15611 : : /* A TU-local variable will always be considered an exposure,
15612 : : so we don't have to worry about strict-only handling. */
15613 : 106 : tree decl = dep->get_entity ();
15614 : 106 : if (!explained
15615 : 15 : && VAR_P (decl)
15616 : 121 : && (DECL_DECLARED_CONSTEXPR_P (decl)
15617 : 6 : || DECL_INLINE_VAR_P (decl)))
15618 : : {
15619 : 15 : auto_diagnostic_group d;
15620 : 15 : if (DECL_DECLARED_CONSTEXPR_P (decl))
15621 : 9 : error_at (DECL_SOURCE_LOCATION (decl),
15622 : : "%qD is declared %<constexpr%> and is initialized to "
15623 : : "a TU-local value", decl);
15624 : : else
15625 : : {
15626 : : /* This can only occur with references. */
15627 : 6 : gcc_checking_assert (TYPE_REF_P (TREE_TYPE (decl)));
15628 : 6 : error_at (DECL_SOURCE_LOCATION (decl),
15629 : : "%qD is a reference declared %<inline%> and is "
15630 : : "constant-initialized to a TU-local value", decl);
15631 : : }
15632 : 15 : bool informed = is_tu_local_value (decl, DECL_INITIAL (decl),
15633 : : /*explain=*/true);
15634 : 15 : gcc_checking_assert (informed);
15635 : 15 : explained = true;
15636 : 15 : }
15637 : :
15638 : : /* We should have emitted an error above, unless the warning was
15639 : : silenced. */
15640 : 106 : gcc_checking_assert (explained);
15641 : 106 : ok = false;
15642 : 106 : continue;
15643 : 106 : }
15644 : :
15645 : : /* In all other cases, we're just warning (rather than erroring).
15646 : : We don't want to do too much warning, so let's just bail after
15647 : : the first warning we successfully emit. */
15648 : 1111456 : if (warn_expose_global_module_tu_local
15649 : 1111438 : && !dep->is_tu_local (/*strict=*/true)
15650 : 1111408 : && dep->is_exposure (/*strict=*/true)
15651 : 1111459 : && diagnose_bad_internal_ref (dep, /*strict=*/true))
15652 : 18 : continue;
15653 : :
15654 : 1111469 : if (warn_template_names_tu_local
15655 : 84329 : && dep->refs_tu_local ()
15656 : 1111484 : && diagnose_template_names_tu_local (dep))
15657 : 49 : continue;
15658 : :
15659 : 1111371 : if (warn_template_names_tu_local
15660 : 84280 : && warn_expose_global_module_tu_local
15661 : 84280 : && !dep->is_tu_local (/*strict=*/true)
15662 : 84256 : && dep->refs_tu_local (/*strict=*/true)
15663 : 30 : && !dep->is_exposure (/*strict=*/true)
15664 : 1111398 : && diagnose_template_names_tu_local (dep, /*strict=*/true))
15665 : : continue;
15666 : : }
15667 : :
15668 : 2573 : return ok;
15669 : : }
15670 : :
15671 : : /* Core of TARJAN's algorithm to find Strongly Connected Components
15672 : : within a graph. See https://en.wikipedia.org/wiki/
15673 : : Tarjan%27s_strongly_connected_components_algorithm for details.
15674 : :
15675 : : We use depset::section as lowlink. Completed nodes have
15676 : : depset::cluster containing the cluster number, with the top
15677 : : bit set.
15678 : :
15679 : : A useful property is that the output vector is a reverse
15680 : : topological sort of the resulting DAG. In our case that means
15681 : : dependent SCCs are found before their dependers. We make use of
15682 : : that property. */
15683 : :
15684 : : void
15685 : 1720130 : depset::tarjan::connect (depset *v)
15686 : : {
15687 : 1720130 : gcc_checking_assert (v->is_binding ()
15688 : : || !(v->is_tu_local ()
15689 : : || v->is_unreached ()
15690 : : || v->is_import ()));
15691 : :
15692 : 1720130 : v->cluster = v->section = ++index;
15693 : 1720130 : stack.safe_push (v);
15694 : :
15695 : : /* Walk all our dependencies, ignore a first marked slot */
15696 : 14124644 : for (unsigned ix = v->is_special (); ix != v->deps.length (); ix++)
15697 : : {
15698 : 5345893 : depset *dep = v->deps[ix];
15699 : :
15700 : 5345893 : if (dep->is_binding ()
15701 : 10542261 : || !(dep->is_import () || dep->is_tu_local ()))
15702 : : {
15703 : 5344890 : unsigned lwm = dep->cluster;
15704 : :
15705 : 5344890 : if (!dep->cluster)
15706 : : {
15707 : : /* A new node. Connect it. */
15708 : 948721 : connect (dep);
15709 : 948721 : lwm = dep->section;
15710 : : }
15711 : :
15712 : 5344890 : if (dep->section && v->section > lwm)
15713 : 858898 : v->section = lwm;
15714 : : }
15715 : : }
15716 : :
15717 : 1720130 : if (v->section == v->cluster)
15718 : : {
15719 : : /* Root of a new SCC. Push all the members onto the result list. */
15720 : : unsigned num = v->cluster;
15721 : 1720130 : depset *p;
15722 : 1720130 : do
15723 : : {
15724 : 1720130 : p = stack.pop ();
15725 : 1720130 : p->cluster = num;
15726 : 1720130 : p->section = 0;
15727 : 1720130 : result.quick_push (p);
15728 : : }
15729 : 1720130 : while (p != v);
15730 : : }
15731 : 1720130 : }
15732 : :
15733 : : /* Compare two depsets. The specific ordering is unimportant, we're
15734 : : just trying to get consistency. */
15735 : :
15736 : : static int
15737 : 80024932 : depset_cmp (const void *a_, const void *b_)
15738 : : {
15739 : 80024932 : depset *a = *(depset *const *)a_;
15740 : 80024932 : depset *b = *(depset *const *)b_;
15741 : :
15742 : 80024932 : depset::entity_kind a_kind = a->get_entity_kind ();
15743 : 80024932 : depset::entity_kind b_kind = b->get_entity_kind ();
15744 : :
15745 : 80024932 : if (a_kind != b_kind)
15746 : : /* Different entity kinds, order by that. */
15747 : 4131919 : return a_kind < b_kind ? -1 : +1;
15748 : :
15749 : 77088666 : tree a_decl = a->get_entity ();
15750 : 77088666 : tree b_decl = b->get_entity ();
15751 : 77088666 : if (a_kind == depset::EK_USING)
15752 : : {
15753 : : /* If one is a using, the other must be too. */
15754 : 742697 : a_decl = OVL_FUNCTION (a_decl);
15755 : 742697 : b_decl = OVL_FUNCTION (b_decl);
15756 : : }
15757 : :
15758 : 77088666 : if (a_decl != b_decl)
15759 : : /* Different entities, order by their UID. */
15760 : 71292128 : return DECL_UID (a_decl) < DECL_UID (b_decl) ? -1 : +1;
15761 : :
15762 : 5796538 : if (a_kind == depset::EK_BINDING)
15763 : : {
15764 : : /* Both are bindings. Order by identifier hash. */
15765 : 5793615 : gcc_checking_assert (a->get_name () != b->get_name ());
15766 : 5793615 : hashval_t ah = IDENTIFIER_HASH_VALUE (a->get_name ());
15767 : 5793615 : hashval_t bh = IDENTIFIER_HASH_VALUE (b->get_name ());
15768 : 8629563 : return (ah == bh ? 0 : ah < bh ? -1 : +1);
15769 : : }
15770 : :
15771 : : /* They are the same decl. This can happen with two using decls
15772 : : pointing to the same target. The best we can aim for is
15773 : : consistently telling qsort how to order them. Hopefully we'll
15774 : : never have to debug a case that depends on this. Oh, who am I
15775 : : kidding? Good luck. */
15776 : 2923 : gcc_checking_assert (a_kind == depset::EK_USING);
15777 : :
15778 : : /* Order by depset address. Not the best, but it is something. */
15779 : 2923 : return a < b ? -1 : +1;
15780 : : }
15781 : :
15782 : : /* Sort the clusters in SCC such that those that depend on one another
15783 : : are placed later. */
15784 : :
15785 : : // FIXME: I am not convinced this is needed and, if needed,
15786 : : // sufficient. We emit the decls in this order but that emission
15787 : : // could walk into later decls (from the body of the decl, or default
15788 : : // arg-like things). Why doesn't that walk do the right thing? And
15789 : : // if it DTRT why do we need to sort here -- won't things naturally
15790 : : // work? I think part of the issue is that when we're going to refer
15791 : : // to an entity by name, and that entity is in the same cluster as us,
15792 : : // we need to actually walk that entity, if we've not already walked
15793 : : // it.
15794 : : static void
15795 : 223005 : sort_cluster (depset::hash *original, depset *scc[], unsigned size)
15796 : : {
15797 : 223005 : depset::hash table (size, original);
15798 : :
15799 : 223005 : dump.indent ();
15800 : :
15801 : : /* Place bindings last, usings before that. It's not strictly
15802 : : necessary, but it does make things neater. Says Mr OCD. */
15803 : : unsigned bind_lwm = size;
15804 : : unsigned use_lwm = size;
15805 : 1147022 : for (unsigned ix = 0; ix != use_lwm;)
15806 : : {
15807 : 924017 : depset *dep = scc[ix];
15808 : 924017 : switch (dep->get_entity_kind ())
15809 : : {
15810 : 118716 : case depset::EK_BINDING:
15811 : : /* Move to end. No increment. Notice this could be moving
15812 : : a using decl, which we'll then move again. */
15813 : 118716 : if (--bind_lwm != ix)
15814 : : {
15815 : 62256 : scc[ix] = scc[bind_lwm];
15816 : 62256 : scc[bind_lwm] = dep;
15817 : : }
15818 : 118716 : if (use_lwm > bind_lwm)
15819 : : {
15820 : 100151 : use_lwm--;
15821 : 100151 : break;
15822 : : }
15823 : : /* We must have copied a using or TU-local, so move it too. */
15824 : 18565 : dep = scc[ix];
15825 : 18565 : gcc_checking_assert
15826 : : (dep->get_entity_kind () == depset::EK_USING
15827 : : || dep->get_entity_kind () == depset::EK_TU_LOCAL);
15828 : : /* FALLTHROUGH */
15829 : :
15830 : 32128 : case depset::EK_USING:
15831 : 32128 : case depset::EK_TU_LOCAL:
15832 : 32128 : if (--use_lwm != ix)
15833 : : {
15834 : 22956 : scc[ix] = scc[use_lwm];
15835 : 22956 : scc[use_lwm] = dep;
15836 : : }
15837 : : break;
15838 : :
15839 : 791738 : case depset::EK_DECL:
15840 : 791738 : case depset::EK_SPECIALIZATION:
15841 : 791738 : case depset::EK_PARTIAL:
15842 : 791738 : table.add_mergeable (dep);
15843 : 791738 : ix++;
15844 : 791738 : break;
15845 : :
15846 : 0 : default:
15847 : 0 : gcc_unreachable ();
15848 : : }
15849 : : }
15850 : :
15851 : 223005 : gcc_checking_assert (use_lwm <= bind_lwm);
15852 : 223269 : dump (dumper::MERGE) && dump ("Ordering %u/%u depsets", use_lwm, size);
15853 : :
15854 : 223005 : table.find_dependencies (nullptr);
15855 : :
15856 : 446010 : auto_vec<depset *> order = table.connect ();
15857 : 446010 : gcc_checking_assert (order.length () == use_lwm);
15858 : :
15859 : : /* Now rewrite entries [0,lwm), in the dependency order we
15860 : : discovered. Usually each entity is in its own cluster. Rarely,
15861 : : we can get multi-entity clusters, in which case all but one must
15862 : : only be reached from within the cluster. This happens for
15863 : : something like:
15864 : :
15865 : : template<typename T>
15866 : : auto Foo (const T &arg) -> TPL<decltype (arg)>;
15867 : :
15868 : : The instantiation of TPL will be in the specialization table, and
15869 : : refer to Foo via arg. But we can only get to that specialization
15870 : : from Foo's declaration, so we only need to treat Foo as mergable
15871 : : (We'll do structural comparison of TPL<decltype (arg)>).
15872 : :
15873 : : We approximate finding the single cluster entry dep by checking for
15874 : : entities recursively depending on a dep first seen when streaming
15875 : : its own merge key; the first dep we see in such a cluster should be
15876 : : the first one streamed. */
15877 : : unsigned entry_pos = ~0u;
15878 : : unsigned cluster = ~0u;
15879 : 2029486 : for (unsigned ix = 0; ix != order.length (); ix++)
15880 : : {
15881 : 791738 : gcc_checking_assert (order[ix]->is_special ());
15882 : 791738 : bool tight = order[ix]->cluster == cluster;
15883 : 791738 : depset *dep = order[ix]->deps[0];
15884 : 792461 : dump (dumper::MERGE)
15885 : 1443 : && dump ("Mergeable %u is %N%s%s", ix, dep->get_entity (),
15886 : 723 : tight ? " (tight)" : "", dep->is_entry () ? " (entry)" : "");
15887 : 791738 : scc[ix] = dep;
15888 : 791738 : if (tight)
15889 : : {
15890 : 93 : gcc_checking_assert (dep->is_maybe_recursive ());
15891 : 93 : if (dep->is_entry ())
15892 : : {
15893 : : /* There should only be one entry dep in a cluster. */
15894 : 6 : gcc_checking_assert (!scc[entry_pos]->is_entry ());
15895 : 6 : gcc_checking_assert (scc[entry_pos]->is_maybe_recursive ());
15896 : 6 : scc[ix] = scc[entry_pos];
15897 : 6 : scc[entry_pos] = dep;
15898 : : }
15899 : : }
15900 : : else
15901 : : entry_pos = ix;
15902 : 791738 : cluster = order[ix]->cluster;
15903 : : }
15904 : :
15905 : 223269 : dump (dumper::MERGE) && dump ("Ordered %u keys", order.length ());
15906 : 223005 : dump.outdent ();
15907 : 223005 : }
15908 : :
15909 : : /* Reduce graph to SCCS clusters. SCCS will be populated with the
15910 : : depsets in dependency order. Each depset's CLUSTER field contains
15911 : : its cluster number. Each SCC has a unique cluster number, and are
15912 : : contiguous in SCCS. Cluster numbers are otherwise arbitrary. */
15913 : :
15914 : : vec<depset *>
15915 : 225549 : depset::hash::connect ()
15916 : : {
15917 : 225549 : tarjan connector (size ());
15918 : 225549 : vec<depset *> deps;
15919 : 225549 : deps.create (size ());
15920 : 4268825 : for (depset *item : *this)
15921 : : {
15922 : 2021638 : entity_kind kind = item->get_entity_kind ();
15923 : 1902922 : if (kind == EK_BINDING
15924 : 1902922 : || !(kind == EK_REDIRECT
15925 : 1884679 : || item->is_tu_local ()
15926 : 1884554 : || item->is_unreached ()
15927 : 1602014 : || item->is_import ()))
15928 : 1720130 : deps.quick_push (item);
15929 : : }
15930 : :
15931 : : /* Iteration over the hash table is an unspecified ordering. While
15932 : : that has advantages, it causes 2 problems. Firstly repeatable
15933 : : builds are tricky. Secondly creating testcases that check
15934 : : dependencies are correct by making sure a bad ordering would
15935 : : happen if that was wrong. */
15936 : 1222507 : deps.qsort (depset_cmp);
15937 : :
15938 : 1945679 : while (deps.length ())
15939 : : {
15940 : 1720130 : depset *v = deps.pop ();
15941 : 1720130 : dump (dumper::CLUSTER) &&
15942 : 1800 : (v->is_binding ()
15943 : 210 : ? dump ("Connecting binding %P", v->get_entity (), v->get_name ())
15944 : 1590 : : dump ("Connecting %s %s %C:%N",
15945 : 1590 : is_key_order () ? "key-order"
15946 : 870 : : !v->has_defn () ? "declaration" : "definition",
15947 : 1590 : v->entity_kind_name (), TREE_CODE (v->get_entity ()),
15948 : : v->get_entity ()));
15949 : 1720130 : if (!v->cluster)
15950 : 771409 : connector.connect (v);
15951 : : }
15952 : :
15953 : 225549 : deps.release ();
15954 : 451098 : return connector.result;
15955 : 225549 : }
15956 : :
15957 : : /* Initialize location spans. */
15958 : :
15959 : : void
15960 : 4594 : loc_spans::init (const line_maps *lmaps, const line_map_ordinary *map)
15961 : : {
15962 : 4594 : gcc_checking_assert (!init_p ());
15963 : 4594 : spans = new vec<span> ();
15964 : 4594 : spans->reserve (20);
15965 : :
15966 : 4594 : span interval;
15967 : 4594 : interval.ordinary.first = 0;
15968 : 4594 : interval.macro.second = MAX_LOCATION_T + 1;
15969 : 4594 : interval.ordinary_delta = interval.macro_delta = 0;
15970 : :
15971 : : /* A span for reserved fixed locs. */
15972 : 4594 : interval.ordinary.second
15973 : 4594 : = MAP_START_LOCATION (LINEMAPS_ORDINARY_MAP_AT (line_table, 0));
15974 : 4594 : interval.macro.first = interval.macro.second;
15975 : 4594 : dump (dumper::LOCATION)
15976 : 42 : && dump ("Fixed span %u ordinary:[%K,%K) macro:[%K,%K)", spans->length (),
15977 : : interval.ordinary.first, interval.ordinary.second,
15978 : : interval.macro.first, interval.macro.second);
15979 : 4594 : spans->quick_push (interval);
15980 : :
15981 : : /* A span for command line & forced headers. */
15982 : 4594 : interval.ordinary.first = interval.ordinary.second;
15983 : 4594 : interval.macro.second = interval.macro.first;
15984 : 4594 : if (map)
15985 : : {
15986 : 4588 : interval.ordinary.second = map->start_location;
15987 : 4588 : interval.macro.first = LINEMAPS_MACRO_LOWEST_LOCATION (lmaps);
15988 : : }
15989 : 4594 : dump (dumper::LOCATION)
15990 : 21 : && dump ("Pre span %u ordinary:[%K,%K) macro:[%K,%K)", spans->length (),
15991 : : interval.ordinary.first, interval.ordinary.second,
15992 : : interval.macro.first, interval.macro.second);
15993 : 4594 : spans->quick_push (interval);
15994 : :
15995 : : /* Start an interval for the main file. */
15996 : 4594 : interval.ordinary.first = interval.ordinary.second;
15997 : 4594 : interval.macro.second = interval.macro.first;
15998 : 4594 : dump (dumper::LOCATION)
15999 : 21 : && dump ("Main span %u ordinary:[%K,*) macro:[*,%K)", spans->length (),
16000 : : interval.ordinary.first, interval.macro.second);
16001 : 4594 : spans->quick_push (interval);
16002 : 4594 : }
16003 : :
16004 : : /* Reopen the span, if we want the about-to-be-inserted set of maps to
16005 : : be propagated in our own location table. I.e. we are the primary
16006 : : interface and we're importing a partition. */
16007 : :
16008 : : bool
16009 : 2842 : loc_spans::maybe_propagate (module_state *import, location_t hwm)
16010 : : {
16011 : 2842 : bool opened = (module_interface_p () && !module_partition_p ()
16012 : 3223 : && import->is_partition ());
16013 : 151 : if (opened)
16014 : 151 : open (hwm);
16015 : 2842 : return opened;
16016 : : }
16017 : :
16018 : : /* Open a new linemap interval. The just-created ordinary map is the
16019 : : first map of the interval. */
16020 : :
16021 : : void
16022 : 1025 : loc_spans::open (location_t hwm)
16023 : : {
16024 : 1025 : span interval;
16025 : 1025 : interval.ordinary.first = interval.ordinary.second = hwm;
16026 : 2050 : interval.macro.first = interval.macro.second
16027 : 1025 : = LINEMAPS_MACRO_LOWEST_LOCATION (line_table);
16028 : 1025 : interval.ordinary_delta = interval.macro_delta = 0;
16029 : 1025 : dump (dumper::LOCATION)
16030 : 0 : && dump ("Opening span %u ordinary:[%K,... macro:...,%K)",
16031 : 0 : spans->length (), interval.ordinary.first,
16032 : : interval.macro.second);
16033 : 1025 : if (spans->length ())
16034 : : {
16035 : : /* No overlapping! */
16036 : 1025 : auto &last = spans->last ();
16037 : 1025 : gcc_checking_assert (interval.ordinary.first >= last.ordinary.second);
16038 : 1025 : gcc_checking_assert (interval.macro.second <= last.macro.first);
16039 : : }
16040 : 1025 : spans->safe_push (interval);
16041 : 1025 : }
16042 : :
16043 : : /* Close out the current linemap interval. The last maps are within
16044 : : the interval. */
16045 : :
16046 : : void
16047 : 5616 : loc_spans::close ()
16048 : : {
16049 : 5616 : span &interval = spans->last ();
16050 : :
16051 : 5616 : interval.ordinary.second
16052 : 5616 : = ((line_table->highest_location
16053 : 5616 : + (loc_one << line_table->default_range_bits))
16054 : 5616 : & ~((loc_one << line_table->default_range_bits) - 1));
16055 : 5616 : interval.macro.first = LINEMAPS_MACRO_LOWEST_LOCATION (line_table);
16056 : 5616 : dump (dumper::LOCATION)
16057 : 21 : && dump ("Closing span %u ordinary:[%K,%K) macro:[%K,%K)",
16058 : 21 : spans->length () - 1,
16059 : : interval.ordinary.first,interval.ordinary.second,
16060 : : interval.macro.first, interval.macro.second);
16061 : 5616 : }
16062 : :
16063 : : /* Given an ordinary location LOC, return the lmap_interval it resides
16064 : : in. NULL if it is not in an interval. */
16065 : :
16066 : : const loc_spans::span *
16067 : 24144592 : loc_spans::ordinary (location_t loc)
16068 : : {
16069 : 24144592 : unsigned len = spans->length ();
16070 : 48158795 : unsigned pos = 0;
16071 : 48165906 : while (len)
16072 : : {
16073 : 48158132 : unsigned half = len / 2;
16074 : 48158132 : const span &probe = (*spans)[pos + half];
16075 : 48158132 : if (loc < probe.ordinary.first)
16076 : : len = half;
16077 : 48151021 : else if (loc < probe.ordinary.second)
16078 : : return &probe;
16079 : : else
16080 : : {
16081 : 24014203 : pos += half + 1;
16082 : 24014203 : len = len - (half + 1);
16083 : : }
16084 : : }
16085 : : return NULL;
16086 : : }
16087 : :
16088 : : /* Likewise, given a macro location LOC, return the lmap interval it
16089 : : resides in. */
16090 : :
16091 : : const loc_spans::span *
16092 : 2094531 : loc_spans::macro (location_t loc)
16093 : : {
16094 : 2094531 : unsigned len = spans->length ();
16095 : 4186004 : unsigned pos = 0;
16096 : 4186004 : while (len)
16097 : : {
16098 : 4185992 : unsigned half = len / 2;
16099 : 4185992 : const span &probe = (*spans)[pos + half];
16100 : 4185992 : if (loc >= probe.macro.second)
16101 : : len = half;
16102 : 4185992 : else if (loc >= probe.macro.first)
16103 : : return &probe;
16104 : : else
16105 : : {
16106 : 2091473 : pos += half + 1;
16107 : 2091473 : len = len - (half + 1);
16108 : : }
16109 : : }
16110 : : return NULL;
16111 : : }
16112 : :
16113 : : /* Return the ordinary location closest to FROM. */
16114 : :
16115 : : static location_t
16116 : 6427 : ordinary_loc_of (line_maps *lmaps, location_t from)
16117 : : {
16118 : 12857 : while (!IS_ORDINARY_LOC (from))
16119 : : {
16120 : 3 : if (IS_ADHOC_LOC (from))
16121 : 3 : from = get_location_from_adhoc_loc (lmaps, from);
16122 : 3 : if (from >= LINEMAPS_MACRO_LOWEST_LOCATION (lmaps))
16123 : : {
16124 : : /* Find the ordinary location nearest FROM. */
16125 : 0 : const line_map *map = linemap_lookup (lmaps, from);
16126 : 0 : const line_map_macro *mac_map = linemap_check_macro (map);
16127 : 0 : from = mac_map->get_expansion_point_location ();
16128 : : }
16129 : : }
16130 : 6427 : return from;
16131 : : }
16132 : :
16133 : : static module_state **
16134 : 11384 : get_module_slot (tree name, module_state *parent, bool partition, bool insert)
16135 : : {
16136 : 11384 : module_state_hash::compare_type ct (name, uintptr_t (parent) | partition);
16137 : 11384 : hashval_t hv = module_state_hash::hash (ct);
16138 : :
16139 : 11384 : return modules_hash->find_slot_with_hash (ct, hv, insert ? INSERT : NO_INSERT);
16140 : : }
16141 : :
16142 : : static module_state *
16143 : 89508 : get_primary (module_state *parent)
16144 : : {
16145 : 93110 : while (parent->is_partition ())
16146 : 479 : parent = parent->parent;
16147 : :
16148 : 92631 : if (!parent->name)
16149 : : // Implementation unit has null name
16150 : 48071 : parent = parent->parent;
16151 : :
16152 : 88814 : return parent;
16153 : : }
16154 : :
16155 : : /* Find or create module NAME & PARENT in the hash table. */
16156 : :
16157 : : module_state *
16158 : 11384 : get_module (tree name, module_state *parent, bool partition)
16159 : : {
16160 : : /* We might be given an empty NAME if preprocessing fails to handle
16161 : : a header-name token. */
16162 : 11384 : if (name && TREE_CODE (name) == STRING_CST
16163 : 14176 : && TREE_STRING_LENGTH (name) == 0)
16164 : : return nullptr;
16165 : :
16166 : 11384 : if (partition)
16167 : : {
16168 : 937 : if (!parent)
16169 : 199 : parent = get_primary (this_module ());
16170 : :
16171 : 937 : if (!parent->is_partition () && !parent->flatname)
16172 : 220 : parent->set_flatname ();
16173 : : }
16174 : :
16175 : 11384 : module_state **slot = get_module_slot (name, parent, partition, true);
16176 : 11384 : module_state *state = *slot;
16177 : 11384 : if (!state)
16178 : : {
16179 : 6193 : state = (new (ggc_alloc<module_state> ())
16180 : 6193 : module_state (name, parent, partition));
16181 : 6193 : *slot = state;
16182 : : }
16183 : : return state;
16184 : : }
16185 : :
16186 : : /* Process string name PTR into a module_state. */
16187 : :
16188 : : static module_state *
16189 : 400 : get_module (const char *ptr)
16190 : : {
16191 : : /* On DOS based file systems, there is an ambiguity with A:B which can be
16192 : : interpreted as a module Module:Partition or Drive:PATH. Interpret strings
16193 : : which clearly starts as pathnames as header-names and everything else is
16194 : : treated as a (possibly malformed) named moduled. */
16195 : 400 : if (IS_DIR_SEPARATOR (ptr[ptr[0] == '.']) // ./FOO or /FOO
16196 : : #if HAVE_DOS_BASED_FILE_SYSTEM
16197 : : || (HAS_DRIVE_SPEC (ptr) && IS_DIR_SEPARATOR (ptr[2])) // A:/FOO
16198 : : #endif
16199 : : || false)
16200 : : /* A header name. */
16201 : 107 : return get_module (build_string (strlen (ptr), ptr));
16202 : :
16203 : : bool partition = false;
16204 : : module_state *mod = NULL;
16205 : :
16206 : 1156 : for (const char *probe = ptr;; probe++)
16207 : 1449 : if (!*probe || *probe == '.' || *probe == ':')
16208 : : {
16209 : 371 : if (probe == ptr)
16210 : : return NULL;
16211 : :
16212 : 371 : mod = get_module (get_identifier_with_length (ptr, probe - ptr),
16213 : : mod, partition);
16214 : 371 : ptr = probe;
16215 : 371 : if (*ptr == ':')
16216 : : {
16217 : 75 : if (partition)
16218 : : return NULL;
16219 : : partition = true;
16220 : : }
16221 : :
16222 : 371 : if (!*ptr++)
16223 : : break;
16224 : : }
16225 : 1078 : else if (!(ISALPHA (*probe) || *probe == '_'
16226 : 18 : || (probe != ptr && ISDIGIT (*probe))))
16227 : : return NULL;
16228 : :
16229 : : return mod;
16230 : : }
16231 : :
16232 : : /* Create a new mapper connecting to OPTION. */
16233 : :
16234 : : module_client *
16235 : 4594 : make_mapper (location_t loc, class mkdeps *deps)
16236 : : {
16237 : 4594 : timevar_start (TV_MODULE_MAPPER);
16238 : 4594 : const char *option = module_mapper_name;
16239 : 4594 : if (!option)
16240 : 4552 : option = getenv ("CXX_MODULE_MAPPER");
16241 : :
16242 : 9188 : mapper = module_client::open_module_client
16243 : 4594 : (loc, option, deps, &set_cmi_repo,
16244 : 4594 : (save_decoded_options[0].opt_index == OPT_SPECIAL_program_name)
16245 : 4594 : && save_decoded_options[0].arg != progname
16246 : : ? save_decoded_options[0].arg : nullptr);
16247 : :
16248 : 4594 : timevar_stop (TV_MODULE_MAPPER);
16249 : :
16250 : 4594 : return mapper;
16251 : : }
16252 : :
16253 : : static unsigned lazy_snum;
16254 : :
16255 : : static bool
16256 : 7634 : recursive_lazy (unsigned snum = ~0u)
16257 : : {
16258 : 7634 : if (lazy_snum)
16259 : : {
16260 : 0 : error_at (input_location, "recursive lazy load");
16261 : 0 : return true;
16262 : : }
16263 : :
16264 : 7634 : lazy_snum = snum;
16265 : 7634 : return false;
16266 : : }
16267 : :
16268 : : /* If THIS has an interface dependency on itself, report an error and
16269 : : return false. */
16270 : :
16271 : : bool
16272 : 2693 : module_state::check_circular_import (location_t from)
16273 : : {
16274 : 2693 : if (this == this_module ())
16275 : : {
16276 : : /* Cannot import the current module. */
16277 : 9 : auto_diagnostic_group d;
16278 : 9 : error_at (from, "module %qs depends on itself", get_flatname ());
16279 : 9 : if (!header_module_p ())
16280 : 6 : inform (loc, "module %qs declared here", get_flatname ());
16281 : 9 : return false;
16282 : 9 : }
16283 : : return true;
16284 : : }
16285 : :
16286 : : /* Module name substitutions. */
16287 : : static vec<module_state *,va_heap> substs;
16288 : :
16289 : : void
16290 : 7837 : module_state::mangle (bool include_partition)
16291 : : {
16292 : 7837 : if (subst)
16293 : 242 : mangle_module_substitution (subst);
16294 : : else
16295 : : {
16296 : 7595 : if (parent)
16297 : 726 : parent->mangle (include_partition);
16298 : 7595 : if (include_partition || !is_partition ())
16299 : : {
16300 : : // Partitions are significant for global initializer
16301 : : // functions
16302 : 7500 : bool partition = is_partition () && !parent->is_partition ();
16303 : 7500 : subst = mangle_module_component (name, partition);
16304 : 7500 : substs.safe_push (this);
16305 : : }
16306 : : }
16307 : 7837 : }
16308 : :
16309 : : void
16310 : 7111 : mangle_module (int mod, bool include_partition)
16311 : : {
16312 : 7111 : module_state *imp = (*modules)[mod];
16313 : :
16314 : 7111 : gcc_checking_assert (!imp->is_header ());
16315 : :
16316 : 7111 : if (!imp->name)
16317 : : /* Set when importing the primary module interface. */
16318 : 231 : imp = imp->parent;
16319 : :
16320 : : /* Ensure this is actually a module unit. */
16321 : 231 : gcc_checking_assert (imp);
16322 : :
16323 : 7111 : imp->mangle (include_partition);
16324 : 7111 : }
16325 : :
16326 : : /* Clean up substitutions. */
16327 : : void
16328 : 6826 : mangle_module_fini ()
16329 : : {
16330 : 14326 : while (substs.length ())
16331 : 7500 : substs.pop ()->subst = 0;
16332 : 6826 : }
16333 : :
16334 : : /* Announce WHAT about the module. */
16335 : :
16336 : : void
16337 : 11779 : module_state::announce (const char *what) const
16338 : : {
16339 : 11779 : if (noisy_p ())
16340 : : {
16341 : 0 : fprintf (stderr, " %s:%s", what, get_flatname ());
16342 : 0 : fflush (stderr);
16343 : : }
16344 : 11779 : }
16345 : :
16346 : : /* A human-readable README section. The contents of this section to
16347 : : not contribute to the CRC, so the contents can change per
16348 : : compilation. That allows us to embed CWD, hostname, build time and
16349 : : what not. It is a STRTAB that may be extracted with:
16350 : : readelf -pgnu.c++.README $(module).gcm */
16351 : :
16352 : : void
16353 : 2544 : module_state::write_readme (elf_out *to, cpp_reader *reader, const char *dialect)
16354 : : {
16355 : 2544 : bytes_out readme (to);
16356 : :
16357 : 2544 : readme.begin (false);
16358 : :
16359 : 2544 : readme.printf ("GNU C++ %s",
16360 : 2544 : is_header () ? "header unit"
16361 : 1668 : : !is_partition () ? "primary interface"
16362 : 172 : : is_interface () ? "interface partition"
16363 : : : "internal partition");
16364 : :
16365 : : /* Compiler's version. */
16366 : 2544 : readme.printf ("compiler: %s", version_string);
16367 : :
16368 : : /* Module format version. */
16369 : 2544 : verstr_t string;
16370 : 2544 : version2string (MODULE_VERSION, string);
16371 : 2544 : readme.printf ("version: %s", string);
16372 : :
16373 : : /* Module information. */
16374 : 2544 : readme.printf ("module: %s", get_flatname ());
16375 : 2544 : readme.printf ("source: %s", main_input_filename);
16376 : 2544 : readme.printf ("dialect: %s", dialect);
16377 : 2544 : if (extensions)
16378 : 24 : readme.printf ("extensions: %s%s%s",
16379 : : extensions & SE_OPENMP ? "-fopenmp"
16380 : 6 : : extensions & SE_OPENMP_SIMD ? "-fopenmp-simd" : "",
16381 : : (extensions & SE_OPENACC)
16382 : 3 : && (extensions & (SE_OPENMP | SE_OPENMP_SIMD))
16383 : : ? " " : "",
16384 : 9 : extensions & SE_OPENACC ? "-fopenacc" : "");
16385 : :
16386 : : /* The following fields could be expected to change between
16387 : : otherwise identical compilations. Consider a distributed build
16388 : : system. We should have a way of overriding that. */
16389 : 2544 : if (char *cwd = getcwd (NULL, 0))
16390 : : {
16391 : 2544 : readme.printf ("cwd: %s", cwd);
16392 : 2544 : free (cwd);
16393 : : }
16394 : 5088 : readme.printf ("repository: %s", cmi_repo ? cmi_repo : ".");
16395 : : #if NETWORKING
16396 : : {
16397 : : char hostname[64];
16398 : : if (!gethostname (hostname, sizeof (hostname)))
16399 : : readme.printf ("host: %s", hostname);
16400 : : }
16401 : : #endif
16402 : 2544 : {
16403 : : /* This of course will change! */
16404 : 2544 : time_t stampy;
16405 : 2544 : auto kind = cpp_get_date (reader, &stampy);
16406 : 2544 : if (kind != CPP_time_kind::UNKNOWN)
16407 : : {
16408 : 2544 : struct tm *time;
16409 : :
16410 : 2544 : time = gmtime (&stampy);
16411 : 2544 : readme.print_time ("build", time, "UTC");
16412 : :
16413 : 2544 : if (kind == CPP_time_kind::DYNAMIC)
16414 : : {
16415 : 2544 : time = localtime (&stampy);
16416 : 2544 : readme.print_time ("local", time,
16417 : : #if defined (__USE_MISC) || defined (__USE_BSD) /* Is there a better way? */
16418 : : time->tm_zone
16419 : : #else
16420 : : ""
16421 : : #endif
16422 : : );
16423 : : }
16424 : : }
16425 : : }
16426 : :
16427 : : /* Its direct imports. */
16428 : 3139 : for (unsigned ix = 1; ix < modules->length (); ix++)
16429 : : {
16430 : 595 : module_state *state = (*modules)[ix];
16431 : :
16432 : 595 : if (state->is_direct ())
16433 : 885 : readme.printf ("%s: %s %s", state->exported_p ? "export" : "import",
16434 : : state->get_flatname (), state->filename);
16435 : : }
16436 : :
16437 : 2544 : readme.end (to, to->name (MOD_SNAME_PFX ".README"), NULL);
16438 : 2544 : }
16439 : :
16440 : : /* Sort environment var names in reverse order. */
16441 : :
16442 : : static int
16443 : 0 : env_var_cmp (const void *a_, const void *b_)
16444 : : {
16445 : 0 : const unsigned char *a = *(const unsigned char *const *)a_;
16446 : 0 : const unsigned char *b = *(const unsigned char *const *)b_;
16447 : :
16448 : 0 : for (unsigned ix = 0; ; ix++)
16449 : : {
16450 : 0 : bool a_end = !a[ix] || a[ix] == '=';
16451 : 0 : if (a[ix] == b[ix])
16452 : : {
16453 : 0 : if (a_end)
16454 : : break;
16455 : : }
16456 : : else
16457 : : {
16458 : 0 : bool b_end = !b[ix] || b[ix] == '=';
16459 : :
16460 : 0 : if (!a_end && !b_end)
16461 : 0 : return a[ix] < b[ix] ? +1 : -1;
16462 : 0 : if (a_end && b_end)
16463 : : break;
16464 : 0 : return a_end ? +1 : -1;
16465 : : }
16466 : 0 : }
16467 : :
16468 : : return 0;
16469 : : }
16470 : :
16471 : : /* Write the environment. It is a STRTAB that may be extracted with:
16472 : : readelf -pgnu.c++.ENV $(module).gcm */
16473 : :
16474 : : void
16475 : 0 : module_state::write_env (elf_out *to)
16476 : : {
16477 : 0 : vec<const char *> vars;
16478 : 0 : vars.create (20);
16479 : :
16480 : 0 : extern char **environ;
16481 : 0 : while (const char *var = environ[vars.length ()])
16482 : 0 : vars.safe_push (var);
16483 : 0 : vars.qsort (env_var_cmp);
16484 : :
16485 : 0 : bytes_out env (to);
16486 : 0 : env.begin (false);
16487 : 0 : while (vars.length ())
16488 : 0 : env.printf ("%s", vars.pop ());
16489 : 0 : env.end (to, to->name (MOD_SNAME_PFX ".ENV"), NULL);
16490 : :
16491 : 0 : vars.release ();
16492 : 0 : }
16493 : :
16494 : : /* Write the direct or indirect imports.
16495 : : u:N
16496 : : {
16497 : : u:index
16498 : : s:name
16499 : : u32:crc
16500 : : s:filename (direct)
16501 : : u:exported (direct)
16502 : : } imports[N]
16503 : : */
16504 : :
16505 : : void
16506 : 810 : module_state::write_imports (bytes_out &sec, bool direct)
16507 : : {
16508 : 810 : unsigned count = 0;
16509 : :
16510 : 1704 : for (unsigned ix = 1; ix < modules->length (); ix++)
16511 : : {
16512 : 894 : module_state *imp = (*modules)[ix];
16513 : :
16514 : 894 : if (imp->remap && imp->is_direct () == direct)
16515 : 432 : count++;
16516 : : }
16517 : :
16518 : 810 : gcc_assert (!direct || count);
16519 : :
16520 : 810 : sec.u (count);
16521 : 1704 : for (unsigned ix = 1; ix < modules->length (); ix++)
16522 : : {
16523 : 894 : module_state *imp = (*modules)[ix];
16524 : :
16525 : 894 : if (imp->remap && imp->is_direct () == direct)
16526 : : {
16527 : 582 : dump () && dump ("Writing %simport:%u->%u %M (crc=%x)",
16528 : : !direct ? "indirect "
16529 : 75 : : imp->exported_p ? "exported " : "",
16530 : : ix, imp->remap, imp, imp->crc);
16531 : 432 : sec.u (imp->remap);
16532 : 432 : sec.str (imp->get_flatname ());
16533 : 432 : sec.u32 (imp->crc);
16534 : 432 : if (direct)
16535 : : {
16536 : 424 : write_location (sec, imp->imported_from ());
16537 : 424 : sec.str (imp->filename);
16538 : 424 : int exportedness = 0;
16539 : 424 : if (imp->exported_p)
16540 : : exportedness = +1;
16541 : 257 : else if (!imp->is_purview_direct ())
16542 : 12 : exportedness = -1;
16543 : 424 : sec.i (exportedness);
16544 : : }
16545 : : }
16546 : : }
16547 : 810 : }
16548 : :
16549 : : /* READER, LMAPS != NULL == direct imports,
16550 : : == NUL == indirect imports. */
16551 : :
16552 : : unsigned
16553 : 692 : module_state::read_imports (bytes_in &sec, cpp_reader *reader, line_maps *lmaps)
16554 : : {
16555 : 692 : unsigned count = sec.u ();
16556 : 692 : unsigned loaded = 0;
16557 : :
16558 : 1748 : while (count--)
16559 : : {
16560 : 364 : unsigned ix = sec.u ();
16561 : 364 : if (ix >= slurp->remap->length () || !ix || (*slurp->remap)[ix])
16562 : : {
16563 : 0 : sec.set_overrun ();
16564 : 0 : break;
16565 : : }
16566 : :
16567 : 364 : const char *name = sec.str (NULL);
16568 : 364 : module_state *imp = get_module (name);
16569 : 364 : unsigned crc = sec.u32 ();
16570 : 364 : int exportedness = 0;
16571 : :
16572 : : /* If the import is a partition, it must be the same primary
16573 : : module as this TU. */
16574 : 364 : if (imp && imp->is_partition () &&
16575 : : (!named_module_p ()
16576 : 135 : || (get_primary (this_module ()) != get_primary (imp))))
16577 : : imp = NULL;
16578 : :
16579 : 364 : if (!imp)
16580 : 0 : sec.set_overrun ();
16581 : 364 : if (sec.get_overrun ())
16582 : : break;
16583 : :
16584 : 364 : if (lmaps)
16585 : : {
16586 : : /* A direct import, maybe load it. */
16587 : 361 : location_t floc = read_location (sec);
16588 : 361 : const char *fname = sec.str (NULL);
16589 : 361 : exportedness = sec.i ();
16590 : :
16591 : 361 : if (sec.get_overrun ())
16592 : : break;
16593 : :
16594 : 361 : if (!imp->check_circular_import (floc))
16595 : 3 : continue;
16596 : :
16597 : 358 : if (imp->loadedness == ML_NONE)
16598 : : {
16599 : 284 : imp->loc = floc;
16600 : 284 : imp->crc = crc;
16601 : 284 : if (!imp->get_flatname ())
16602 : 242 : imp->set_flatname ();
16603 : :
16604 : 284 : unsigned n = dump.push (imp);
16605 : :
16606 : 284 : if (!imp->filename && fname)
16607 : 242 : imp->filename = xstrdup (fname);
16608 : :
16609 : 284 : if (imp->is_partition ())
16610 : 33 : dump () && dump ("Importing elided partition %M", imp);
16611 : :
16612 : 284 : if (!imp->do_import (reader, false))
16613 : 3 : imp = NULL;
16614 : 284 : dump.pop (n);
16615 : 284 : if (!imp)
16616 : 3 : continue;
16617 : : }
16618 : :
16619 : 355 : if (is_partition ())
16620 : : {
16621 : 63 : if (!imp->is_direct ())
16622 : 33 : imp->directness = MD_PARTITION_DIRECT;
16623 : 63 : if (exportedness > 0)
16624 : 3 : imp->exported_p = true;
16625 : : }
16626 : : }
16627 : : else
16628 : : {
16629 : : /* An indirect import, find it, it should already be here. */
16630 : 3 : if (imp->loadedness == ML_NONE)
16631 : : {
16632 : 0 : error_at (loc, "indirect import %qs is not already loaded", name);
16633 : 0 : continue;
16634 : : }
16635 : : }
16636 : :
16637 : 358 : if (imp->crc != crc)
16638 : 0 : error_at (loc, "import %qs has CRC mismatch", imp->get_flatname ());
16639 : :
16640 : 358 : (*slurp->remap)[ix] = (imp->mod << 1) | (lmaps != NULL);
16641 : :
16642 : 358 : if (lmaps && exportedness >= 0)
16643 : 343 : set_import (imp, bool (exportedness));
16644 : 520 : dump () && dump ("Found %simport:%u %M->%u", !lmaps ? "indirect "
16645 : 81 : : exportedness > 0 ? "exported "
16646 : 48 : : exportedness < 0 ? "gmf" : "", ix, imp,
16647 : : imp->mod);
16648 : 358 : loaded++;
16649 : : }
16650 : :
16651 : 692 : return loaded;
16652 : : }
16653 : :
16654 : : /* Write the import table to MOD_SNAME_PFX.imp. */
16655 : :
16656 : : void
16657 : 405 : module_state::write_imports (elf_out *to, unsigned *crc_ptr)
16658 : : {
16659 : 477 : dump () && dump ("Writing imports");
16660 : 405 : dump.indent ();
16661 : :
16662 : 405 : bytes_out sec (to);
16663 : 405 : sec.begin ();
16664 : :
16665 : 405 : write_imports (sec, true);
16666 : 405 : write_imports (sec, false);
16667 : :
16668 : 405 : sec.end (to, to->name (MOD_SNAME_PFX ".imp"), crc_ptr);
16669 : 405 : dump.outdent ();
16670 : 405 : }
16671 : :
16672 : : bool
16673 : 346 : module_state::read_imports (cpp_reader *reader, line_maps *lmaps)
16674 : : {
16675 : 346 : bytes_in sec;
16676 : :
16677 : 346 : if (!sec.begin (loc, from (), MOD_SNAME_PFX ".imp"))
16678 : : return false;
16679 : :
16680 : 424 : dump () && dump ("Reading %u imports", slurp->remap->length () - 1);
16681 : 346 : dump.indent ();
16682 : :
16683 : : /* Read the imports. */
16684 : 346 : unsigned direct = read_imports (sec, reader, lmaps);
16685 : 346 : unsigned indirect = read_imports (sec, NULL, NULL);
16686 : 346 : if (direct + indirect + 1 != slurp->remap->length ())
16687 : 6 : from ()->set_error (elf::E_BAD_IMPORT);
16688 : :
16689 : 346 : dump.outdent ();
16690 : 346 : if (!sec.end (from ()))
16691 : : return false;
16692 : : return true;
16693 : 346 : }
16694 : :
16695 : : /* We're the primary module interface, but have partitions. Document
16696 : : them so that non-partition module implementation units know which
16697 : : have already been loaded. */
16698 : :
16699 : : void
16700 : 115 : module_state::write_partitions (elf_out *to, unsigned count, unsigned *crc_ptr)
16701 : : {
16702 : 133 : dump () && dump ("Writing %u elided partitions", count);
16703 : 115 : dump.indent ();
16704 : :
16705 : 115 : bytes_out sec (to);
16706 : 115 : sec.begin ();
16707 : :
16708 : 293 : for (unsigned ix = 1; ix != modules->length (); ix++)
16709 : : {
16710 : 178 : module_state *imp = (*modules)[ix];
16711 : 178 : if (imp->is_partition ())
16712 : : {
16713 : 193 : dump () && dump ("Writing elided partition %M (crc=%x)",
16714 : : imp, imp->crc);
16715 : 163 : sec.str (imp->get_flatname ());
16716 : 163 : sec.u32 (imp->crc);
16717 : 317 : write_location (sec, imp->is_direct ()
16718 : 154 : ? imp->imported_from () : UNKNOWN_LOCATION);
16719 : 163 : sec.str (imp->filename);
16720 : : }
16721 : : }
16722 : :
16723 : 115 : sec.end (to, to->name (MOD_SNAME_PFX ".prt"), crc_ptr);
16724 : 115 : dump.outdent ();
16725 : 115 : }
16726 : :
16727 : : bool
16728 : 18 : module_state::read_partitions (unsigned count)
16729 : : {
16730 : 18 : bytes_in sec;
16731 : 18 : if (!sec.begin (loc, from (), MOD_SNAME_PFX ".prt"))
16732 : : return false;
16733 : :
16734 : 21 : dump () && dump ("Reading %u elided partitions", count);
16735 : 18 : dump.indent ();
16736 : :
16737 : 48 : while (count--)
16738 : : {
16739 : 30 : const char *name = sec.str (NULL);
16740 : 30 : unsigned crc = sec.u32 ();
16741 : 30 : location_t floc = read_location (sec);
16742 : 30 : const char *fname = sec.str (NULL);
16743 : :
16744 : 30 : if (sec.get_overrun ())
16745 : : break;
16746 : :
16747 : 36 : dump () && dump ("Reading elided partition %s (crc=%x)", name, crc);
16748 : :
16749 : 30 : module_state *imp = get_module (name);
16750 : 30 : if (!imp /* Partition should be ... */
16751 : 30 : || !imp->is_partition () /* a partition ... */
16752 : 30 : || imp->loadedness != ML_NONE /* that is not yet loaded ... */
16753 : 60 : || get_primary (imp) != this) /* whose primary is this. */
16754 : : {
16755 : 0 : sec.set_overrun ();
16756 : 0 : break;
16757 : : }
16758 : :
16759 : 30 : if (!imp->has_location ())
16760 : 27 : imp->loc = floc;
16761 : 30 : imp->crc = crc;
16762 : 30 : if (!imp->filename && fname[0])
16763 : 27 : imp->filename = xstrdup (fname);
16764 : : }
16765 : :
16766 : 18 : dump.outdent ();
16767 : 18 : if (!sec.end (from ()))
16768 : : return false;
16769 : : return true;
16770 : 18 : }
16771 : :
16772 : : /* Data for config reading and writing. */
16773 : : struct module_state_config {
16774 : : const char *dialect_str = get_dialect ();
16775 : : line_map_uint_t ordinary_locs = 0;
16776 : : line_map_uint_t macro_locs = 0;
16777 : : unsigned num_imports = 0;
16778 : : unsigned num_partitions = 0;
16779 : : unsigned num_entities = 0;
16780 : : unsigned loc_range_bits = 0;
16781 : : unsigned active_init = 0;
16782 : :
16783 : 94979 : static void release ()
16784 : : {
16785 : 94979 : XDELETEVEC (dialect);
16786 : 94979 : dialect = NULL;
16787 : : }
16788 : :
16789 : : private:
16790 : : static const char *get_dialect ();
16791 : : static char *dialect;
16792 : : };
16793 : :
16794 : : char *module_state_config::dialect;
16795 : :
16796 : : /* Generate a string of the significant compilation options.
16797 : : Generally assume the user knows what they're doing, in the same way
16798 : : that object files can be mixed. */
16799 : :
16800 : : const char *
16801 : 5512 : module_state_config::get_dialect ()
16802 : : {
16803 : 5512 : if (!dialect)
16804 : 8790 : dialect = concat (get_cxx_dialect_name (cxx_dialect),
16805 : : /* C++ implies these, only show if disabled. */
16806 : 4395 : flag_exceptions ? "" : "/no-exceptions",
16807 : 4395 : flag_rtti ? "" : "/no-rtti",
16808 : 4395 : flag_new_inheriting_ctors ? "" : "/old-inheriting-ctors",
16809 : : /* C++ 20 implies concepts and coroutines. */
16810 : 1405 : cxx_dialect < cxx20 && flag_concepts ? "/concepts" : "",
16811 : 4395 : (cxx_dialect < cxx20 && flag_coroutines
16812 : : ? "/coroutines" : ""),
16813 : 4395 : flag_module_implicit_inline ? "/implicit-inline" : "",
16814 : 4395 : flag_contracts ? "/contracts" : "",
16815 : : NULL);
16816 : :
16817 : 5512 : return dialect;
16818 : : }
16819 : :
16820 : : /* Contents of a cluster. */
16821 : : enum cluster_tag {
16822 : : ct_decl, /* A decl. */
16823 : : ct_defn, /* A definition. */
16824 : : ct_bind, /* A binding. */
16825 : : ct_hwm
16826 : : };
16827 : :
16828 : : /* Binding modifiers. */
16829 : : enum ct_bind_flags
16830 : : {
16831 : : cbf_export = 0x1, /* An exported decl. */
16832 : : cbf_hidden = 0x2, /* A hidden (friend) decl. */
16833 : : cbf_using = 0x4, /* A using decl. */
16834 : : cbf_internal = 0x8, /* A TU-local decl. */
16835 : : };
16836 : :
16837 : : /* DEP belongs to a different cluster, seed it to prevent
16838 : : unfortunately timed duplicate import. */
16839 : : // FIXME: QOI For inter-cluster references we could just only pick
16840 : : // one entity from an earlier cluster. Even better track
16841 : : // dependencies between earlier clusters
16842 : :
16843 : : void
16844 : 5073771 : module_state::intercluster_seed (trees_out &sec, unsigned index_hwm, depset *dep)
16845 : : {
16846 : 5073771 : if (dep->is_tu_local ())
16847 : : /* We only stream placeholders for TU-local entities anyway. */;
16848 : 5073571 : else if (dep->is_import () || dep->cluster < index_hwm)
16849 : : {
16850 : 2242176 : tree ent = dep->get_entity ();
16851 : 2242176 : if (!TREE_VISITED (ent))
16852 : : {
16853 : 986960 : sec.tree_node (ent);
16854 : 987410 : dump (dumper::CLUSTER)
16855 : 450 : && dump ("Seeded %s %N",
16856 : 450 : dep->is_import () ? "import" : "intercluster", ent);
16857 : : }
16858 : : }
16859 : 5073771 : }
16860 : :
16861 : : /* Write the cluster of depsets in SCC[0-SIZE).
16862 : : dep->section -> section number
16863 : : dep->cluster -> entity number
16864 : : */
16865 : :
16866 : : unsigned
16867 : 223005 : module_state::write_cluster (elf_out *to, depset *scc[], unsigned size,
16868 : : depset::hash &table, unsigned *counts,
16869 : : unsigned *crc_ptr)
16870 : : {
16871 : 224340 : dump () && dump ("Writing section:%u %u depsets", table.section, size);
16872 : 223005 : dump.indent ();
16873 : :
16874 : 223005 : trees_out sec (to, this, table, table.section);
16875 : 223005 : sec.begin ();
16876 : 223005 : unsigned index_lwm = counts[MSC_entities];
16877 : :
16878 : : /* Determine entity numbers, mark for writing. */
16879 : 223314 : dump (dumper::CLUSTER) && dump ("Cluster members:") && (dump.indent (), true);
16880 : 1147022 : for (unsigned ix = 0; ix != size; ix++)
16881 : : {
16882 : 924017 : depset *b = scc[ix];
16883 : :
16884 : 924017 : switch (b->get_entity_kind ())
16885 : : {
16886 : 0 : default:
16887 : 0 : gcc_unreachable ();
16888 : :
16889 : 118716 : case depset::EK_BINDING:
16890 : 118716 : {
16891 : 118716 : dump (dumper::CLUSTER)
16892 : 210 : && dump ("[%u]=%s %P", ix, b->entity_kind_name (),
16893 : : b->get_entity (), b->get_name ());
16894 : 118716 : depset *ns_dep = b->deps[0];
16895 : 118716 : gcc_checking_assert (ns_dep->get_entity_kind ()
16896 : : == depset::EK_NAMESPACE
16897 : : && ns_dep->get_entity () == b->get_entity ());
16898 : 268286 : for (unsigned jx = b->deps.length (); --jx;)
16899 : : {
16900 : 149570 : depset *dep = b->deps[jx];
16901 : : // We could be declaring something that is also a
16902 : : // (merged) import
16903 : 163178 : gcc_checking_assert (dep->is_import ()
16904 : : || TREE_VISITED (dep->get_entity ())
16905 : : || (dep->get_entity_kind ()
16906 : : == depset::EK_USING)
16907 : : || (dep->get_entity_kind ()
16908 : : == depset::EK_TU_LOCAL));
16909 : : }
16910 : : }
16911 : : break;
16912 : :
16913 : 791738 : case depset::EK_DECL:
16914 : 791738 : case depset::EK_SPECIALIZATION:
16915 : 791738 : case depset::EK_PARTIAL:
16916 : 791738 : b->cluster = counts[MSC_entities]++;
16917 : 791738 : sec.mark_declaration (b->get_entity (), b->has_defn ());
16918 : : /* FALLTHROUGH */
16919 : :
16920 : 805301 : case depset::EK_USING:
16921 : 805301 : case depset::EK_TU_LOCAL:
16922 : 1610602 : gcc_checking_assert (!b->is_import ()
16923 : : && !b->is_unreached ());
16924 : 926354 : dump (dumper::CLUSTER)
16925 : 1161 : && dump ("[%u]=%s %s %N", ix, b->entity_kind_name (),
16926 : 729 : b->has_defn () ? "definition" : "declaration",
16927 : : b->get_entity ());
16928 : : break;
16929 : : }
16930 : : }
16931 : 223314 : dump (dumper::CLUSTER) && (dump.outdent (), true);
16932 : :
16933 : : /* Ensure every out-of-cluster decl is referenced before we start
16934 : : streaming. We must do both imports *and* earlier clusters,
16935 : : because the latter could reach into the former and cause a
16936 : : duplicate loop. */
16937 : 223005 : sec.set_importing (+1);
16938 : 1147022 : for (unsigned ix = 0; ix != size; ix++)
16939 : : {
16940 : 924017 : depset *b = scc[ix];
16941 : 10745441 : for (unsigned jx = b->is_special (); jx != b->deps.length (); jx++)
16942 : : {
16943 : 4451252 : depset *dep = b->deps[jx];
16944 : :
16945 : 4451252 : if (dep->is_binding ())
16946 : : {
16947 : 921569 : for (unsigned ix = dep->deps.length (); --ix;)
16948 : : {
16949 : 622519 : depset *bind = dep->deps[ix];
16950 : 622519 : if (bind->get_entity_kind () == depset::EK_USING)
16951 : 40147 : bind = bind->deps[1];
16952 : :
16953 : 622519 : intercluster_seed (sec, index_lwm, bind);
16954 : : }
16955 : : /* Also check the namespace itself. */
16956 : 149525 : dep = dep->deps[0];
16957 : : }
16958 : :
16959 : 4451252 : intercluster_seed (sec, index_lwm, dep);
16960 : : }
16961 : : }
16962 : 223005 : sec.tree_node (NULL_TREE);
16963 : : /* We're done importing now. */
16964 : 223005 : sec.set_importing (-1);
16965 : :
16966 : : /* Write non-definitions. */
16967 : 1147022 : for (unsigned ix = 0; ix != size; ix++)
16968 : : {
16969 : 924017 : depset *b = scc[ix];
16970 : 924017 : tree decl = b->get_entity ();
16971 : 924017 : switch (b->get_entity_kind ())
16972 : : {
16973 : 0 : default:
16974 : 0 : gcc_unreachable ();
16975 : 118716 : break;
16976 : :
16977 : 118716 : case depset::EK_BINDING:
16978 : 118716 : {
16979 : 118716 : gcc_assert (TREE_CODE (decl) == NAMESPACE_DECL);
16980 : 119806 : dump () && dump ("Depset:%u binding %C:%P", ix, TREE_CODE (decl),
16981 : : decl, b->get_name ());
16982 : 118716 : sec.u (ct_bind);
16983 : 118716 : sec.tree_node (decl);
16984 : 118716 : sec.tree_node (b->get_name ());
16985 : :
16986 : : /* Write in reverse order, so reading will see the exports
16987 : : first, thus building the overload chain will be
16988 : : optimized. */
16989 : 387002 : for (unsigned jx = b->deps.length (); --jx;)
16990 : : {
16991 : 149570 : depset *dep = b->deps[jx];
16992 : 149570 : tree bound = dep->get_entity ();
16993 : 149570 : unsigned flags = 0;
16994 : 149570 : if (dep->get_entity_kind () == depset::EK_TU_LOCAL)
16995 : : flags |= cbf_internal;
16996 : 149525 : else if (dep->get_entity_kind () == depset::EK_USING)
16997 : : {
16998 : 13563 : tree ovl = bound;
16999 : 13563 : bound = OVL_FUNCTION (bound);
17000 : 13563 : if (!(TREE_CODE (bound) == CONST_DECL
17001 : 3752 : && UNSCOPED_ENUM_P (TREE_TYPE (bound))
17002 : 3147 : && decl == TYPE_NAME (TREE_TYPE (bound))))
17003 : : /* An unscoped enumerator in its enumeration's
17004 : : scope is not a using. */
17005 : : flags |= cbf_using;
17006 : 13563 : if (OVL_EXPORT_P (ovl))
17007 : 12706 : flags |= cbf_export;
17008 : : }
17009 : : else
17010 : : {
17011 : : /* An implicit typedef must be at one. */
17012 : 135962 : gcc_assert (!DECL_IMPLICIT_TYPEDEF_P (bound) || jx == 1);
17013 : 135962 : if (dep->is_hidden ())
17014 : : flags |= cbf_hidden;
17015 : 131213 : else if (DECL_MODULE_EXPORT_P (STRIP_TEMPLATE (bound)))
17016 : 117776 : flags |= cbf_export;
17017 : : }
17018 : :
17019 : 149570 : gcc_checking_assert (DECL_P (bound));
17020 : :
17021 : 149570 : sec.i (flags);
17022 : 149570 : if (flags & cbf_internal)
17023 : : {
17024 : 45 : sec.tree_node (name_for_tu_local_decl (bound));
17025 : 45 : write_location (sec, DECL_SOURCE_LOCATION (bound));
17026 : : }
17027 : : else
17028 : 149525 : sec.tree_node (bound);
17029 : : }
17030 : :
17031 : : /* Terminate the list. */
17032 : 118716 : sec.i (-1);
17033 : : }
17034 : 118716 : break;
17035 : :
17036 : 13563 : case depset::EK_USING:
17037 : 13563 : case depset::EK_TU_LOCAL:
17038 : 13590 : dump () && dump ("Depset:%u %s %C:%N", ix, b->entity_kind_name (),
17039 : 27 : TREE_CODE (decl), decl);
17040 : : break;
17041 : :
17042 : 791738 : case depset::EK_SPECIALIZATION:
17043 : 791738 : case depset::EK_PARTIAL:
17044 : 791738 : case depset::EK_DECL:
17045 : 794048 : dump () && dump ("Depset:%u %s entity:%u %C:%N", ix,
17046 : : b->entity_kind_name (), b->cluster,
17047 : 2310 : TREE_CODE (decl), decl);
17048 : :
17049 : 791738 : sec.u (ct_decl);
17050 : 791738 : sec.tree_node (decl);
17051 : :
17052 : 926327 : dump () && dump ("Wrote declaration entity:%u %C:%N",
17053 : 2310 : b->cluster, TREE_CODE (decl), decl);
17054 : : break;
17055 : : }
17056 : : }
17057 : :
17058 : : depset *namer = NULL;
17059 : :
17060 : : /* Write out definitions */
17061 : 1147022 : for (unsigned ix = 0; ix != size; ix++)
17062 : : {
17063 : 924017 : depset *b = scc[ix];
17064 : 924017 : tree decl = b->get_entity ();
17065 : 924017 : switch (b->get_entity_kind ())
17066 : : {
17067 : : default:
17068 : : break;
17069 : :
17070 : 791738 : case depset::EK_SPECIALIZATION:
17071 : 791738 : case depset::EK_PARTIAL:
17072 : 791738 : case depset::EK_DECL:
17073 : 791738 : if (!namer)
17074 : 215211 : namer = b;
17075 : :
17076 : 791738 : if (b->has_defn ())
17077 : : {
17078 : 307048 : sec.u (ct_defn);
17079 : 307048 : sec.tree_node (decl);
17080 : 307680 : dump () && dump ("Writing definition %N", decl);
17081 : 307048 : sec.write_definition (decl, b->refs_tu_local ());
17082 : :
17083 : 307048 : if (!namer->has_defn ())
17084 : 924017 : namer = b;
17085 : : }
17086 : : break;
17087 : : }
17088 : : }
17089 : :
17090 : : /* We don't find the section by name. Use depset's decl's name for
17091 : : human friendliness. */
17092 : 223005 : unsigned name = 0;
17093 : 223005 : tree naming_decl = NULL_TREE;
17094 : 223005 : if (namer)
17095 : : {
17096 : 215211 : naming_decl = namer->get_entity ();
17097 : 215211 : if (namer->get_entity_kind () == depset::EK_USING)
17098 : : /* This unfortunately names the section from the target of the
17099 : : using decl. But the name is only a guide, so Do Not Care. */
17100 : 0 : naming_decl = OVL_FUNCTION (naming_decl);
17101 : 215211 : if (DECL_IMPLICIT_TYPEDEF_P (naming_decl))
17102 : : /* Lose any anonymousness. */
17103 : 73949 : naming_decl = TYPE_NAME (TREE_TYPE (naming_decl));
17104 : 215211 : name = to->qualified_name (naming_decl, namer->has_defn ());
17105 : : }
17106 : :
17107 : 223005 : unsigned bytes = sec.pos;
17108 : 223005 : unsigned snum = sec.end (to, name, crc_ptr);
17109 : :
17110 : 1147022 : for (unsigned ix = size; ix--;)
17111 : 924017 : gcc_checking_assert (scc[ix]->section == snum);
17112 : :
17113 : 223005 : dump.outdent ();
17114 : 224340 : dump () && dump ("Wrote section:%u named-by:%N", table.section, naming_decl);
17115 : :
17116 : 223005 : return bytes;
17117 : 223005 : }
17118 : :
17119 : : /* Read a cluster from section SNUM. */
17120 : :
17121 : : bool
17122 : 165505 : module_state::read_cluster (unsigned snum)
17123 : : {
17124 : 165505 : trees_in sec (this);
17125 : :
17126 : 165505 : if (!sec.begin (loc, from (), snum))
17127 : : return false;
17128 : :
17129 : 166654 : dump () && dump ("Reading section:%u", snum);
17130 : 165505 : dump.indent ();
17131 : :
17132 : : /* We care about structural equality. */
17133 : 165505 : comparing_dependent_aliases++;
17134 : :
17135 : : /* First seed the imports. */
17136 : 942134 : while (tree import = sec.tree_node ())
17137 : 1718949 : dump (dumper::CLUSTER) && dump ("Seeded import %N", import);
17138 : :
17139 : 1131593 : while (!sec.get_overrun () && sec.more_p ())
17140 : : {
17141 : 966088 : unsigned ct = sec.u ();
17142 : 966088 : switch (ct)
17143 : : {
17144 : 0 : default:
17145 : 0 : sec.set_overrun ();
17146 : 0 : break;
17147 : :
17148 : 88988 : case ct_bind:
17149 : : /* A set of namespace bindings. */
17150 : 88988 : {
17151 : 88988 : tree ns = sec.tree_node ();
17152 : 88988 : tree name = sec.tree_node ();
17153 : 88988 : tree decls = NULL_TREE;
17154 : 88988 : tree visible = NULL_TREE;
17155 : 88988 : tree internal = NULL_TREE;
17156 : 88988 : tree type = NULL_TREE;
17157 : 88988 : bool dedup = false;
17158 : 88988 : bool global_p = is_header ();
17159 : :
17160 : : /* We rely on the bindings being in the reverse order of
17161 : : the resulting overload set. */
17162 : 204711 : for (;;)
17163 : : {
17164 : 204711 : int flags = sec.i ();
17165 : 204711 : if (flags < 0)
17166 : : break;
17167 : :
17168 : 115723 : if ((flags & cbf_hidden)
17169 : 4107 : && (flags & (cbf_using | cbf_export)))
17170 : 0 : sec.set_overrun ();
17171 : 115723 : if ((flags & cbf_internal)
17172 : 15 : && flags != cbf_internal)
17173 : 0 : sec.set_overrun ();
17174 : :
17175 : 0 : if (flags & cbf_internal)
17176 : : {
17177 : 15 : tree name = sec.tree_node ();
17178 : 15 : location_t loc = read_location (sec);
17179 : 15 : if (sec.get_overrun ())
17180 : : break;
17181 : :
17182 : 15 : tree decl = make_node (TU_LOCAL_ENTITY);
17183 : 15 : TU_LOCAL_ENTITY_NAME (decl) = name;
17184 : 15 : TU_LOCAL_ENTITY_LOCATION (decl) = loc;
17185 : 15 : internal = tree_cons (NULL_TREE, decl, internal);
17186 : 15 : continue;
17187 : 15 : }
17188 : :
17189 : 115708 : tree decl = sec.tree_node ();
17190 : 115708 : if (sec.get_overrun ())
17191 : : break;
17192 : :
17193 : 115708 : if (!global_p)
17194 : : {
17195 : : /* Check if the decl could require GM merging. */
17196 : 4202 : tree orig = get_originating_module_decl (decl);
17197 : 4202 : tree inner = STRIP_TEMPLATE (orig);
17198 : 4202 : if (!DECL_LANG_SPECIFIC (inner)
17199 : 8404 : || !DECL_MODULE_ATTACH_P (inner))
17200 : : global_p = true;
17201 : : }
17202 : :
17203 : 115708 : if (decls && TREE_CODE (decl) == TYPE_DECL)
17204 : : {
17205 : : /* Stat hack. */
17206 : 48 : if (type || !DECL_IMPLICIT_TYPEDEF_P (decl))
17207 : 0 : sec.set_overrun ();
17208 : :
17209 : 48 : if (flags & cbf_using)
17210 : : {
17211 : 3 : type = build_lang_decl_loc (UNKNOWN_LOCATION,
17212 : : USING_DECL,
17213 : 3 : DECL_NAME (decl),
17214 : : NULL_TREE);
17215 : 3 : USING_DECL_DECLS (type) = decl;
17216 : 3 : USING_DECL_SCOPE (type) = CP_DECL_CONTEXT (decl);
17217 : 3 : DECL_CONTEXT (type) = ns;
17218 : :
17219 : 3 : DECL_MODULE_PURVIEW_P (type) = true;
17220 : 3 : if (flags & cbf_export)
17221 : 3 : DECL_MODULE_EXPORT_P (type) = true;
17222 : : }
17223 : : else
17224 : : type = decl;
17225 : : }
17226 : : else
17227 : : {
17228 : 115660 : if ((flags & cbf_using) &&
17229 : 9807 : !DECL_DECLARES_FUNCTION_P (decl))
17230 : : {
17231 : : /* We should only see a single non-function using-decl
17232 : : for a binding; more than that would clash. */
17233 : 3802 : if (decls)
17234 : 0 : sec.set_overrun ();
17235 : :
17236 : : /* FIXME: Propagate the location of the using-decl
17237 : : for use in diagnostics. */
17238 : 3802 : decls = build_lang_decl_loc (UNKNOWN_LOCATION,
17239 : : USING_DECL,
17240 : 3802 : DECL_NAME (decl),
17241 : : NULL_TREE);
17242 : 3802 : USING_DECL_DECLS (decls) = decl;
17243 : : /* We don't currently record the actual scope of the
17244 : : using-declaration, but this approximation should
17245 : : generally be good enough. */
17246 : 3802 : USING_DECL_SCOPE (decls) = CP_DECL_CONTEXT (decl);
17247 : 3802 : DECL_CONTEXT (decls) = ns;
17248 : :
17249 : 3802 : DECL_MODULE_PURVIEW_P (decls) = true;
17250 : 3802 : if (flags & cbf_export)
17251 : 3790 : DECL_MODULE_EXPORT_P (decls) = true;
17252 : : }
17253 : 111858 : else if (decls
17254 : 85171 : || (flags & (cbf_hidden | cbf_using))
17255 : 191994 : || DECL_FUNCTION_TEMPLATE_P (decl))
17256 : : {
17257 : 44225 : decls = ovl_make (decl, decls);
17258 : 44225 : if (flags & cbf_using)
17259 : : {
17260 : 6005 : dedup = true;
17261 : 6005 : OVL_USING_P (decls) = true;
17262 : 6005 : OVL_PURVIEW_P (decls) = true;
17263 : 6005 : if (flags & cbf_export)
17264 : 5990 : OVL_EXPORT_P (decls) = true;
17265 : : }
17266 : :
17267 : 44225 : if (flags & cbf_hidden)
17268 : 4107 : OVL_HIDDEN_P (decls) = true;
17269 : 40118 : else if (dedup)
17270 : 6041 : OVL_DEDUP_P (decls) = true;
17271 : : }
17272 : : else
17273 : : decls = decl;
17274 : :
17275 : 115648 : if (flags & cbf_export
17276 : 115660 : || (!(flags & cbf_hidden)
17277 : 3440 : && (is_module () || is_partition ())))
17278 : : visible = decls;
17279 : : }
17280 : : }
17281 : :
17282 : 88988 : if (!decls && !internal)
17283 : 0 : sec.set_overrun ();
17284 : :
17285 : 88988 : if (sec.get_overrun ())
17286 : : break; /* Bail. */
17287 : :
17288 : 89822 : dump () && dump ("Binding of %P", ns, name);
17289 : 88988 : if (!set_module_binding (ns, name, mod, global_p,
17290 : 88988 : is_module () || is_partition (),
17291 : : decls, type, visible, internal))
17292 : 0 : sec.set_overrun ();
17293 : : }
17294 : : break;
17295 : :
17296 : 634359 : case ct_decl:
17297 : : /* A decl. */
17298 : 634359 : {
17299 : 634359 : tree decl = sec.tree_node ();
17300 : 1768966 : dump () && dump ("Read declaration of %N", decl);
17301 : : }
17302 : : break;
17303 : :
17304 : 242741 : case ct_defn:
17305 : 242741 : {
17306 : 242741 : tree decl = sec.tree_node ();
17307 : 243840 : dump () && dump ("Reading definition of %N", decl);
17308 : 242741 : sec.read_definition (decl);
17309 : : }
17310 : 242741 : break;
17311 : : }
17312 : : }
17313 : :
17314 : : /* When lazy loading is in effect, we can be in the middle of
17315 : : parsing or instantiating a function. Save it away.
17316 : : push_function_context does too much work. */
17317 : 165505 : tree old_cfd = current_function_decl;
17318 : 165505 : struct function *old_cfun = cfun;
17319 : 266106 : for (const post_process_data& pdata : sec.post_process ())
17320 : : {
17321 : 71993 : tree decl = pdata.decl;
17322 : :
17323 : 71993 : bool abstract = false;
17324 : 71993 : if (TREE_CODE (decl) == TEMPLATE_DECL)
17325 : : {
17326 : 39834 : abstract = true;
17327 : 39834 : decl = DECL_TEMPLATE_RESULT (decl);
17328 : : }
17329 : :
17330 : 71993 : current_function_decl = decl;
17331 : 71993 : allocate_struct_function (decl, abstract);
17332 : 71993 : cfun->language = ggc_cleared_alloc<language_function> ();
17333 : 71993 : cfun->language->base.x_stmt_tree.stmts_are_full_exprs_p = 1;
17334 : 71993 : cfun->function_start_locus = pdata.start_locus;
17335 : 71993 : cfun->function_end_locus = pdata.end_locus;
17336 : 71993 : cfun->language->returns_value = pdata.returns_value;
17337 : 71993 : cfun->language->returns_null = pdata.returns_null;
17338 : 71993 : cfun->language->returns_abnormally = pdata.returns_abnormally;
17339 : 71993 : cfun->language->infinite_loop = pdata.infinite_loop;
17340 : :
17341 : : /* Make sure we emit explicit instantiations.
17342 : : FIXME do we want to do this in expand_or_defer_fn instead? */
17343 : 71993 : if (DECL_EXPLICIT_INSTANTIATION (decl)
17344 : 71993 : && !DECL_EXTERNAL (decl))
17345 : 24 : setup_explicit_instantiation_definition_linkage (decl);
17346 : :
17347 : 71993 : if (abstract)
17348 : : ;
17349 : 32159 : else if (DECL_MAYBE_IN_CHARGE_CDTOR_P (decl))
17350 : 5390 : vec_safe_push (post_load_decls, decl);
17351 : : else
17352 : : {
17353 : 26769 : bool aggr = aggregate_value_p (DECL_RESULT (decl), decl);
17354 : : #ifdef PCC_STATIC_STRUCT_RETURN
17355 : : cfun->returns_pcc_struct = aggr;
17356 : : #endif
17357 : 26769 : cfun->returns_struct = aggr;
17358 : 26769 : expand_or_defer_fn (decl);
17359 : :
17360 : : /* If we first see this function after at_eof, it doesn't get
17361 : : note_vague_linkage_fn from tentative_decl_linkage, so the loop in
17362 : : c_parse_final_cleanups won't consider it. But with DECL_COMDAT we
17363 : : can just clear DECL_EXTERNAL and let cgraph decide.
17364 : : FIXME handle this outside module.cc after GCC 15. */
17365 : 2912 : if (at_eof && DECL_COMDAT (decl) && DECL_EXTERNAL (decl)
17366 : 27570 : && DECL_NOT_REALLY_EXTERN (decl))
17367 : 768 : DECL_EXTERNAL (decl) = false;
17368 : : }
17369 : :
17370 : : }
17371 : 165505 : set_cfun (old_cfun);
17372 : 165505 : current_function_decl = old_cfd;
17373 : 165505 : comparing_dependent_aliases--;
17374 : :
17375 : 165505 : dump.outdent ();
17376 : 166654 : dump () && dump ("Read section:%u", snum);
17377 : :
17378 : 165505 : loaded_clusters++;
17379 : :
17380 : 165505 : if (!sec.end (from ()))
17381 : : return false;
17382 : :
17383 : : return true;
17384 : 165505 : }
17385 : :
17386 : : void
17387 : 121044 : module_state::write_namespace (bytes_out &sec, depset *dep)
17388 : : {
17389 : 121044 : unsigned ns_num = dep->cluster;
17390 : 121044 : unsigned ns_import = 0;
17391 : :
17392 : 121044 : if (dep->is_import ())
17393 : 0 : ns_import = dep->section;
17394 : 121044 : else if (dep->get_entity () != global_namespace)
17395 : 75335 : ns_num++;
17396 : :
17397 : 121044 : sec.u (ns_import);
17398 : 121044 : sec.u (ns_num);
17399 : 121044 : }
17400 : :
17401 : : tree
17402 : 145918 : module_state::read_namespace (bytes_in &sec)
17403 : : {
17404 : 145918 : unsigned ns_import = sec.u ();
17405 : 145918 : unsigned ns_num = sec.u ();
17406 : 145918 : tree ns = NULL_TREE;
17407 : :
17408 : 145918 : if (ns_import || ns_num)
17409 : : {
17410 : 89781 : if (!ns_import)
17411 : 89781 : ns_num--;
17412 : :
17413 : 89781 : if (unsigned origin = slurp->remap_module (ns_import))
17414 : : {
17415 : 89781 : module_state *from = (*modules)[origin];
17416 : 89781 : if (ns_num < from->entity_num)
17417 : : {
17418 : 89781 : binding_slot &slot = (*entity_ary)[from->entity_lwm + ns_num];
17419 : :
17420 : 89781 : if (!slot.is_lazy ())
17421 : 89781 : ns = slot;
17422 : : }
17423 : : }
17424 : : else
17425 : 0 : sec.set_overrun ();
17426 : : }
17427 : : else
17428 : 56137 : ns = global_namespace;
17429 : :
17430 : 145918 : return ns;
17431 : : }
17432 : :
17433 : : /* SPACES is a sorted vector of namespaces. Write out the namespaces
17434 : : to MOD_SNAME_PFX.nms section. */
17435 : :
17436 : : void
17437 : 527 : module_state::write_namespaces (elf_out *to, vec<depset *> spaces,
17438 : : unsigned num, unsigned *crc_p)
17439 : : {
17440 : 576 : dump () && dump ("Writing namespaces");
17441 : 527 : dump.indent ();
17442 : :
17443 : 527 : bytes_out sec (to);
17444 : 527 : sec.begin ();
17445 : :
17446 : 2597 : for (unsigned ix = 0; ix != num; ix++)
17447 : : {
17448 : 2070 : depset *b = spaces[ix];
17449 : 2070 : tree ns = b->get_entity ();
17450 : :
17451 : : /* This could be an anonymous namespace even for a named module,
17452 : : since we can still emit no-linkage decls. */
17453 : 2070 : gcc_checking_assert (TREE_CODE (ns) == NAMESPACE_DECL);
17454 : :
17455 : 2070 : unsigned flags = 0;
17456 : 2070 : if (TREE_PUBLIC (ns))
17457 : 2018 : flags |= 1;
17458 : 2070 : if (DECL_NAMESPACE_INLINE_P (ns))
17459 : 378 : flags |= 2;
17460 : 2070 : if (DECL_MODULE_PURVIEW_P (ns))
17461 : 1719 : flags |= 4;
17462 : 2070 : if (DECL_MODULE_EXPORT_P (ns))
17463 : 1538 : flags |= 8;
17464 : 2070 : if (TREE_DEPRECATED (ns))
17465 : 11 : flags |= 16;
17466 : :
17467 : 2182 : dump () && dump ("Writing namespace:%u %N%s%s%s%s",
17468 : : b->cluster, ns,
17469 : 112 : flags & 1 ? ", public" : "",
17470 : 112 : flags & 2 ? ", inline" : "",
17471 : 112 : flags & 4 ? ", purview" : "",
17472 : 112 : flags & 8 ? ", export" : "",
17473 : 112 : flags & 16 ? ", deprecated" : "");
17474 : 2070 : sec.u (b->cluster);
17475 : 2070 : sec.u (to->name (DECL_NAME (ns)));
17476 : 2070 : write_namespace (sec, b->deps[0]);
17477 : :
17478 : 2070 : sec.u (flags);
17479 : 2070 : write_location (sec, DECL_SOURCE_LOCATION (ns));
17480 : :
17481 : 2070 : if (DECL_NAMESPACE_INLINE_P (ns))
17482 : : {
17483 : 378 : if (tree attr = lookup_attribute ("abi_tag", DECL_ATTRIBUTES (ns)))
17484 : : {
17485 : 110 : tree tags = TREE_VALUE (attr);
17486 : 110 : sec.u (list_length (tags));
17487 : 223 : for (tree tag = tags; tag; tag = TREE_CHAIN (tag))
17488 : 113 : sec.str (TREE_STRING_POINTER (TREE_VALUE (tag)));
17489 : : }
17490 : : else
17491 : 268 : sec.u (0);
17492 : : }
17493 : : }
17494 : :
17495 : 527 : sec.end (to, to->name (MOD_SNAME_PFX ".nms"), crc_p);
17496 : 527 : dump.outdent ();
17497 : 527 : }
17498 : :
17499 : : /* Read the namespace hierarchy from MOD_SNAME_PFX.namespace. Fill in
17500 : : SPACES from that data. */
17501 : :
17502 : : bool
17503 : 555 : module_state::read_namespaces (unsigned num)
17504 : : {
17505 : 555 : bytes_in sec;
17506 : :
17507 : 555 : if (!sec.begin (loc, from (), MOD_SNAME_PFX ".nms"))
17508 : : return false;
17509 : :
17510 : 619 : dump () && dump ("Reading namespaces");
17511 : 555 : dump.indent ();
17512 : :
17513 : 2932 : for (unsigned ix = 0; ix != num; ix++)
17514 : : {
17515 : 2377 : unsigned entity_index = sec.u ();
17516 : 2377 : unsigned name = sec.u ();
17517 : :
17518 : 2377 : tree parent = read_namespace (sec);
17519 : :
17520 : : /* See comment in write_namespace about why not bits. */
17521 : 2377 : unsigned flags = sec.u ();
17522 : 2377 : location_t src_loc = read_location (sec);
17523 : 2377 : unsigned tags_count = (flags & 2) ? sec.u () : 0;
17524 : :
17525 : 2377 : if (entity_index >= entity_num
17526 : 2377 : || !parent
17527 : 2377 : || (flags & 0xc) == 0x8)
17528 : 0 : sec.set_overrun ();
17529 : :
17530 : : tree tags = NULL_TREE;
17531 : 2500 : while (tags_count--)
17532 : : {
17533 : 123 : size_t len;
17534 : 123 : const char *str = sec.str (&len);
17535 : 123 : tags = tree_cons (NULL_TREE, build_string (len + 1, str), tags);
17536 : 123 : tags = nreverse (tags);
17537 : : }
17538 : :
17539 : 2377 : if (sec.get_overrun ())
17540 : : break;
17541 : :
17542 : 4702 : tree id = name ? get_identifier (from ()->name (name)) : NULL_TREE;
17543 : :
17544 : 2547 : dump () && dump ("Read namespace:%u %P%s%s%s%s",
17545 : : entity_index, parent, id,
17546 : 85 : flags & 1 ? ", public" : "",
17547 : : flags & 2 ? ", inline" : "",
17548 : 85 : flags & 4 ? ", purview" : "",
17549 : 85 : flags & 8 ? ", export" : "",
17550 : 85 : flags & 16 ? ", deprecated" : "");
17551 : 2377 : bool visible_p = ((flags & 8)
17552 : 2377 : || ((flags & 1)
17553 : 515 : && (flags & 4)
17554 : 83 : && (is_partition () || is_module ())));
17555 : 2377 : tree inner = add_imported_namespace (parent, id, src_loc, mod,
17556 : : bool (flags & 2), visible_p);
17557 : 2377 : if (!inner)
17558 : : {
17559 : 0 : sec.set_overrun ();
17560 : 0 : break;
17561 : : }
17562 : :
17563 : 2377 : if (is_partition ())
17564 : : {
17565 : 42 : if (flags & 4)
17566 : 36 : DECL_MODULE_PURVIEW_P (inner) = true;
17567 : 42 : if (flags & 8)
17568 : 24 : DECL_MODULE_EXPORT_P (inner) = true;
17569 : : }
17570 : :
17571 : 2377 : if (flags & 16)
17572 : 14 : TREE_DEPRECATED (inner) = true;
17573 : :
17574 : 2377 : if (tags)
17575 : 120 : DECL_ATTRIBUTES (inner)
17576 : 240 : = tree_cons (get_identifier ("abi_tag"), tags, DECL_ATTRIBUTES (inner));
17577 : :
17578 : : /* Install the namespace. */
17579 : 2377 : (*entity_ary)[entity_lwm + entity_index] = inner;
17580 : 2377 : if (DECL_MODULE_IMPORT_P (inner))
17581 : : {
17582 : 0 : bool existed;
17583 : 0 : unsigned *slot = &entity_map->get_or_insert
17584 : 0 : (DECL_UID (inner), &existed);
17585 : 0 : if (existed)
17586 : : /* If it existed, it should match. */
17587 : 0 : gcc_checking_assert (inner == (*entity_ary)[*slot]);
17588 : : else
17589 : 0 : *slot = entity_lwm + entity_index;
17590 : : }
17591 : : }
17592 : :
17593 : 555 : dump.outdent ();
17594 : 555 : if (!sec.end (from ()))
17595 : : return false;
17596 : : return true;
17597 : 555 : }
17598 : :
17599 : : unsigned
17600 : 527 : module_state::write_using_directives (elf_out *to, depset::hash &table,
17601 : : vec<depset *> spaces, unsigned *crc_p)
17602 : : {
17603 : 576 : dump () && dump ("Writing using-directives");
17604 : 527 : dump.indent ();
17605 : :
17606 : 527 : bytes_out sec (to);
17607 : 527 : sec.begin ();
17608 : :
17609 : 527 : unsigned num = 0;
17610 : 3124 : auto emit_one_ns = [&](depset *parent_dep)
17611 : : {
17612 : 2597 : tree parent = parent_dep->get_entity ();
17613 : 3032 : for (auto udir : NAMESPACE_LEVEL (parent)->using_directives)
17614 : : {
17615 : 149 : if (TREE_CODE (udir) != USING_DECL || !DECL_MODULE_PURVIEW_P (udir))
17616 : 20 : continue;
17617 : 129 : bool exported = DECL_MODULE_EXPORT_P (udir);
17618 : 129 : tree target = USING_DECL_DECLS (udir);
17619 : 129 : depset *target_dep = table.find_dependency (target);
17620 : 129 : gcc_checking_assert (target_dep);
17621 : :
17622 : 147 : dump () && dump ("Writing using-directive in %N for %N",
17623 : : parent, target);
17624 : 129 : sec.u (exported);
17625 : 129 : write_namespace (sec, parent_dep);
17626 : 129 : write_namespace (sec, target_dep);
17627 : 129 : ++num;
17628 : : }
17629 : 3124 : };
17630 : :
17631 : 527 : emit_one_ns (table.find_dependency (global_namespace));
17632 : 3651 : for (depset *parent_dep : spaces)
17633 : 2070 : emit_one_ns (parent_dep);
17634 : :
17635 : 527 : sec.end (to, to->name (MOD_SNAME_PFX ".udi"), crc_p);
17636 : 527 : dump.outdent ();
17637 : :
17638 : 527 : return num;
17639 : 527 : }
17640 : :
17641 : : bool
17642 : 130 : module_state::read_using_directives (unsigned num)
17643 : : {
17644 : 130 : if (!bitmap_bit_p (this_module ()->imports, mod))
17645 : : {
17646 : 9 : dump () && dump ("Ignoring using-directives because module %M "
17647 : : "is not visible in this TU", this);
17648 : 6 : return true;
17649 : : }
17650 : :
17651 : 124 : bytes_in sec;
17652 : :
17653 : 124 : if (!sec.begin (loc, from (), MOD_SNAME_PFX ".udi"))
17654 : : return false;
17655 : :
17656 : 130 : dump () && dump ("Reading using-directives");
17657 : 124 : dump.indent ();
17658 : :
17659 : 272 : for (unsigned ix = 0; ix != num; ++ix)
17660 : : {
17661 : 148 : bool exported = sec.u ();
17662 : 148 : tree parent = read_namespace (sec);
17663 : 148 : tree target = read_namespace (sec);
17664 : 148 : if (sec.get_overrun ())
17665 : : break;
17666 : :
17667 : 154 : dump () && dump ("Read using-directive in %N for %N", parent, target);
17668 : 148 : if (exported || is_module () || is_partition ())
17669 : 124 : add_using_namespace (parent, target);
17670 : : }
17671 : :
17672 : 124 : dump.outdent ();
17673 : 124 : if (!sec.end (from ()))
17674 : : return false;
17675 : : return true;
17676 : 124 : }
17677 : :
17678 : : /* Write the binding TABLE to MOD_SNAME_PFX.bnd */
17679 : :
17680 : : unsigned
17681 : 2544 : module_state::write_bindings (elf_out *to, vec<depset *> sccs, unsigned *crc_p)
17682 : : {
17683 : 2817 : dump () && dump ("Writing binding table");
17684 : 2544 : dump.indent ();
17685 : :
17686 : 2544 : unsigned num = 0;
17687 : 2544 : bytes_out sec (to);
17688 : 2544 : sec.begin ();
17689 : :
17690 : 1861872 : for (unsigned ix = 0; ix != sccs.length (); ix++)
17691 : : {
17692 : 928392 : depset *b = sccs[ix];
17693 : 928392 : if (b->is_binding ())
17694 : : {
17695 : 118716 : tree ns = b->get_entity ();
17696 : 119806 : dump () && dump ("Bindings %P section:%u", ns, b->get_name (),
17697 : : b->section);
17698 : 118716 : sec.u (to->name (b->get_name ()));
17699 : 118716 : write_namespace (sec, b->deps[0]);
17700 : 118716 : sec.u (b->section);
17701 : 118716 : num++;
17702 : : }
17703 : : }
17704 : :
17705 : 2544 : sec.end (to, to->name (MOD_SNAME_PFX ".bnd"), crc_p);
17706 : 2544 : dump.outdent ();
17707 : :
17708 : 2544 : return num;
17709 : 2544 : }
17710 : :
17711 : : /* Read the binding table from MOD_SNAME_PFX.bind. */
17712 : :
17713 : : bool
17714 : 2722 : module_state::read_bindings (unsigned num, unsigned lwm, unsigned hwm)
17715 : : {
17716 : 2722 : bytes_in sec;
17717 : :
17718 : 2722 : if (!sec.begin (loc, from (), MOD_SNAME_PFX ".bnd"))
17719 : : return false;
17720 : :
17721 : 3212 : dump () && dump ("Reading binding table");
17722 : 2722 : dump.indent ();
17723 : 145967 : for (; !sec.get_overrun () && num--;)
17724 : : {
17725 : 143245 : const char *name = from ()->name (sec.u ());
17726 : 143245 : tree ns = read_namespace (sec);
17727 : 143245 : unsigned snum = sec.u ();
17728 : :
17729 : 143245 : if (!ns || !name || (snum - lwm) >= (hwm - lwm))
17730 : 0 : sec.set_overrun ();
17731 : 143245 : if (!sec.get_overrun ())
17732 : : {
17733 : 143245 : tree id = get_identifier (name);
17734 : 144708 : dump () && dump ("Bindings %P section:%u", ns, id, snum);
17735 : 143245 : if (mod && !import_module_binding (ns, id, mod, snum))
17736 : : break;
17737 : : }
17738 : : }
17739 : :
17740 : 2722 : dump.outdent ();
17741 : 2722 : if (!sec.end (from ()))
17742 : : return false;
17743 : : return true;
17744 : 2722 : }
17745 : :
17746 : : /* Write the entity table to MOD_SNAME_PFX.ent
17747 : :
17748 : : Each entry is a section number. */
17749 : :
17750 : : void
17751 : 2299 : module_state::write_entities (elf_out *to, vec<depset *> depsets,
17752 : : unsigned count, unsigned *crc_p)
17753 : : {
17754 : 2522 : dump () && dump ("Writing entities");
17755 : 2299 : dump.indent ();
17756 : :
17757 : 2299 : bytes_out sec (to);
17758 : 2299 : sec.begin ();
17759 : :
17760 : 2299 : unsigned current = 0;
17761 : 930670 : for (unsigned ix = 0; ix < depsets.length (); ix++)
17762 : : {
17763 : 928371 : depset *d = depsets[ix];
17764 : :
17765 : 1724478 : switch (d->get_entity_kind ())
17766 : : {
17767 : : default:
17768 : : break;
17769 : :
17770 : 4369 : case depset::EK_NAMESPACE:
17771 : 4369 : if (!d->is_import () && d->get_entity () != global_namespace)
17772 : : {
17773 : 2070 : gcc_checking_assert (d->cluster == current);
17774 : 2070 : current++;
17775 : 2070 : sec.u (0);
17776 : : }
17777 : : break;
17778 : :
17779 : 791738 : case depset::EK_DECL:
17780 : 791738 : case depset::EK_SPECIALIZATION:
17781 : 791738 : case depset::EK_PARTIAL:
17782 : 1583476 : gcc_checking_assert (!d->is_unreached ()
17783 : : && !d->is_import ()
17784 : : && d->cluster == current
17785 : : && d->section);
17786 : 791738 : current++;
17787 : 791738 : sec.u (d->section);
17788 : 791738 : break;
17789 : : }
17790 : : }
17791 : 2299 : gcc_assert (count == current);
17792 : 2299 : sec.end (to, to->name (MOD_SNAME_PFX ".ent"), crc_p);
17793 : 2299 : dump.outdent ();
17794 : 2299 : }
17795 : :
17796 : : bool
17797 : 2517 : module_state::read_entities (unsigned count, unsigned lwm, unsigned hwm)
17798 : : {
17799 : 2517 : trees_in sec (this);
17800 : :
17801 : 2517 : if (!sec.begin (loc, from (), MOD_SNAME_PFX ".ent"))
17802 : : return false;
17803 : :
17804 : 2956 : dump () && dump ("Reading entities");
17805 : 2517 : dump.indent ();
17806 : :
17807 : 959276 : for (binding_slot *slot = entity_ary->begin () + entity_lwm; count--; slot++)
17808 : : {
17809 : 956759 : unsigned snum = sec.u ();
17810 : 956759 : if (snum && (snum - lwm) >= (hwm - lwm))
17811 : 0 : sec.set_overrun ();
17812 : 956759 : if (sec.get_overrun ())
17813 : : break;
17814 : :
17815 : 956759 : if (snum)
17816 : 954382 : slot->set_lazy (snum << 2);
17817 : : }
17818 : :
17819 : 2517 : dump.outdent ();
17820 : 2517 : if (!sec.end (from ()))
17821 : : return false;
17822 : : return true;
17823 : 2517 : }
17824 : :
17825 : : /* Write the pending table to MOD_SNAME_PFX.pnd
17826 : :
17827 : : The pending table holds information about clusters that need to be
17828 : : loaded because they contain information about something that is not
17829 : : found by namespace-scope lookup.
17830 : :
17831 : : The three cases are:
17832 : :
17833 : : (a) Template (maybe-partial) specializations that we have
17834 : : instantiated or defined. When an importer needs to instantiate
17835 : : that template, they /must have/ the partial, explicit & extern
17836 : : specializations available. If they have the other specializations
17837 : : available, they'll have less work to do. Thus, when we're about to
17838 : : instantiate FOO, we have to be able to ask 'are there any
17839 : : specialization of FOO in our imports?'.
17840 : :
17841 : : (b) (Maybe-implicit) member functions definitions. A class could
17842 : : be defined in one header, and an inline member defined in a
17843 : : different header (this occurs in the STL). Similarly, like the
17844 : : specialization case, an implicit member function could have been
17845 : : 'instantiated' in one module, and it'd be nice to not have to
17846 : : reinstantiate it in another.
17847 : :
17848 : : (c) Classes completed elsewhere. A class could be declared in one
17849 : : header and defined in another. We need to know to load the class
17850 : : definition before looking in it. It does highlight an issue --
17851 : : there could be an intermediate import between the outermost containing
17852 : : namespace-scope class and the innermost being-defined class. This is
17853 : : actually possible with all of these cases, so be aware -- we're not
17854 : : just talking of one level of import to get to the innermost namespace.
17855 : :
17856 : : This gets complicated fast, it took me multiple attempts to even
17857 : : get something remotely working. Partially because I focussed on
17858 : : optimizing what I think turns out to be a smaller problem, given
17859 : : the known need to do the more general case *anyway*. I document
17860 : : the smaller problem, because it does appear to be the natural way
17861 : : to do it. It's trap!
17862 : :
17863 : : **** THE TRAP
17864 : :
17865 : : Let's refer to the primary template or the containing class as the
17866 : : KEY. And the specialization or member as the PENDING-ENTITY. (To
17867 : : avoid having to say those mouthfuls all the time.)
17868 : :
17869 : : In either case, we have an entity and we need some way of mapping
17870 : : that to a set of entities that need to be loaded before we can
17871 : : proceed with whatever processing of the entity we were going to do.
17872 : :
17873 : : We need to link the key to the pending-entity in some way. Given a
17874 : : key, tell me the pending-entities I need to have loaded. However
17875 : : we tie the key to the pending-entity must not rely on the key being
17876 : : loaded -- that'd defeat the lazy loading scheme.
17877 : :
17878 : : As the key will be an import in we know its entity number (either
17879 : : because we imported it, or we're writing it out too). Thus we can
17880 : : generate a map of key-indices to pending-entities. The
17881 : : pending-entity indices will be into our span of the entity table,
17882 : : and thus allow them to be lazily loaded. The key index will be
17883 : : into another slot of the entity table. Notice that this checking
17884 : : could be expensive, we don't want to iterate over a bunch of
17885 : : pending-entity indices (across multiple imports), every time we're
17886 : : about do to the thing with the key. We need to quickly determine
17887 : : 'definitely nothing needed'.
17888 : :
17889 : : That's almost good enough, except that key indices are not unique
17890 : : in a couple of cases :( Specifically the Global Module or a module
17891 : : partition can result in multiple modules assigning an entity index
17892 : : for the key. The decl-merging on loading will detect that so we
17893 : : only have one Key loaded, and in the entity hash it'll indicate the
17894 : : entity index of first load. Which might be different to how we
17895 : : know it. Notice this is restricted to GM entities or this-module
17896 : : entities. Foreign imports cannot have this.
17897 : :
17898 : : We can simply resolve this in the direction of how this module
17899 : : referred to the key to how the importer knows it. Look in the
17900 : : entity table slot that we nominate, maybe lazy load it, and then
17901 : : lookup the resultant entity in the entity hash to learn how the
17902 : : importer knows it.
17903 : :
17904 : : But we need to go in the other direction :( Given the key, find all
17905 : : the index-aliases of that key. We can partially solve that by
17906 : : adding an alias hash table. Whenever we load a merged decl, add or
17907 : : augment a mapping from the entity (or its entity-index) to the
17908 : : newly-discovered index. Then when we look for pending entities of
17909 : : a key, we also iterate over this aliases this mapping provides.
17910 : :
17911 : : But that requires the alias to be loaded. And that's not
17912 : : necessarily true.
17913 : :
17914 : : *** THE SIMPLER WAY
17915 : :
17916 : : The remaining fixed thing we have is the innermost namespace
17917 : : containing the ultimate namespace-scope container of the key and
17918 : : the name of that container (which might be the key itself). I.e. a
17919 : : namespace-decl/identifier/module tuple. Let's call this the
17920 : : top-key. We'll discover that the module is not important here,
17921 : : because of cross-module possibilities mentioned in case #c above.
17922 : : We can't markup namespace-binding slots. The best we can do is
17923 : : mark the binding vector with 'there's something here', and have
17924 : : another map from namespace/identifier pairs to a vector of pending
17925 : : entity indices.
17926 : :
17927 : : Maintain a pending-entity map. This is keyed by top-key, and
17928 : : maps to a vector of pending-entity indices. On the binding vector
17929 : : have flags saying whether the pending-name-entity map has contents.
17930 : : (We might want to further extend the key to be GM-vs-Partition and
17931 : : specialization-vs-member, but let's not get ahead of ourselves.)
17932 : :
17933 : : For every key-like entity, find the outermost namespace-scope
17934 : : name. Use that to lookup in the pending-entity map and then make
17935 : : sure the specified entities are loaded.
17936 : :
17937 : : An optimization might be to have a flag in each key-entity saying
17938 : : that its top key might be in the entity table. It's not clear to
17939 : : me how to set that flag cheaply -- cheaper than just looking.
17940 : :
17941 : : FIXME: It'd be nice to have a bit in decls to tell us whether to
17942 : : even try this. We can have a 'already done' flag, that we set when
17943 : : we've done KLASS's lazy pendings. When we import a module that
17944 : : registers pendings on the same top-key as KLASS we need to clear
17945 : : the flag. A recursive walk of the top-key clearing the bit will
17946 : : suffice. Plus we only need to recurse on classes that have the bit
17947 : : set. (That means we need to set the bit on parents of KLASS here,
17948 : : don't forget.) However, first: correctness, second: efficiency. */
17949 : :
17950 : : unsigned
17951 : 2544 : module_state::write_pendings (elf_out *to, vec<depset *> depsets,
17952 : : depset::hash &table, unsigned *crc_p)
17953 : : {
17954 : 2817 : dump () && dump ("Writing pending-entities");
17955 : 2544 : dump.indent ();
17956 : :
17957 : 2544 : trees_out sec (to, this, table);
17958 : 2544 : sec.begin ();
17959 : :
17960 : 2544 : unsigned count = 0;
17961 : 2544 : tree cache_ns = NULL_TREE;
17962 : 2544 : tree cache_id = NULL_TREE;
17963 : 2544 : unsigned cache_section = ~0;
17964 : 930936 : for (unsigned ix = 0; ix < depsets.length (); ix++)
17965 : : {
17966 : 928392 : depset *d = depsets[ix];
17967 : :
17968 : 928392 : if (d->is_binding ())
17969 : 575351 : continue;
17970 : :
17971 : 809676 : if (d->is_import ())
17972 : 0 : continue;
17973 : :
17974 : 809676 : if (!d->is_pending_entity ())
17975 : 456635 : continue;
17976 : :
17977 : 353041 : tree key_decl = nullptr;
17978 : 353041 : tree key_ns = find_pending_key (d->get_entity (), &key_decl);
17979 : 353041 : tree key_name = DECL_NAME (key_decl);
17980 : :
17981 : 353041 : if (IDENTIFIER_ANON_P (key_name))
17982 : : {
17983 : 6 : gcc_checking_assert (IDENTIFIER_LAMBDA_P (key_name));
17984 : 12 : if (tree attached = LAMBDA_TYPE_EXTRA_SCOPE (TREE_TYPE (key_decl)))
17985 : 6 : key_name = DECL_NAME (attached);
17986 : : else
17987 : : {
17988 : : /* There's nothing to attach it to. Must
17989 : : always reinstantiate. */
17990 : 0 : dump ()
17991 : 0 : && dump ("Unattached lambda %N[%u] section:%u",
17992 : 0 : d->get_entity_kind () == depset::EK_DECL
17993 : : ? "Member" : "Specialization", d->get_entity (),
17994 : : d->cluster, d->section);
17995 : 0 : continue;
17996 : : }
17997 : : }
17998 : :
17999 : 353041 : char const *also = "";
18000 : 353041 : if (d->section == cache_section
18001 : 223100 : && key_ns == cache_ns
18002 : 223100 : && key_name == cache_id)
18003 : : /* Same section & key as previous, no need to repeat ourselves. */
18004 : : also = "also ";
18005 : : else
18006 : : {
18007 : 168328 : cache_ns = key_ns;
18008 : 168328 : cache_id = key_name;
18009 : 168328 : cache_section = d->section;
18010 : 168328 : gcc_checking_assert (table.find_dependency (cache_ns));
18011 : 168328 : sec.tree_node (cache_ns);
18012 : 168328 : sec.tree_node (cache_id);
18013 : 168328 : sec.u (d->cluster);
18014 : 168328 : count++;
18015 : : }
18016 : 354201 : dump () && dump ("Pending %s %N entity:%u section:%u %skeyed to %P",
18017 : 580 : d->get_entity_kind () == depset::EK_DECL
18018 : : ? "member" : "specialization", d->get_entity (),
18019 : : d->cluster, cache_section, also, cache_ns, cache_id);
18020 : : }
18021 : 2544 : sec.end (to, to->name (MOD_SNAME_PFX ".pnd"), crc_p);
18022 : 2544 : dump.outdent ();
18023 : :
18024 : 2544 : return count;
18025 : 2544 : }
18026 : :
18027 : : bool
18028 : 1448 : module_state::read_pendings (unsigned count)
18029 : : {
18030 : 1448 : trees_in sec (this);
18031 : :
18032 : 1448 : if (!sec.begin (loc, from (), MOD_SNAME_PFX ".pnd"))
18033 : : return false;
18034 : :
18035 : 1746 : dump () && dump ("Reading %u pendings", count);
18036 : 1448 : dump.indent ();
18037 : :
18038 : 201784 : for (unsigned ix = 0; ix != count; ix++)
18039 : : {
18040 : 200336 : pending_key key;
18041 : 200336 : unsigned index;
18042 : :
18043 : 200336 : key.ns = sec.tree_node ();
18044 : 200336 : key.id = sec.tree_node ();
18045 : 200336 : index = sec.u ();
18046 : :
18047 : 200336 : if (!key.ns || !key.id
18048 : 200336 : || !(TREE_CODE (key.ns) == NAMESPACE_DECL
18049 : 200336 : && !DECL_NAMESPACE_ALIAS (key.ns))
18050 : 200336 : || !identifier_p (key.id)
18051 : 400672 : || index >= entity_num)
18052 : 0 : sec.set_overrun ();
18053 : :
18054 : 200336 : if (sec.get_overrun ())
18055 : : break;
18056 : :
18057 : 201075 : dump () && dump ("Pending:%u keyed to %P", index, key.ns, key.id);
18058 : :
18059 : 200336 : index += entity_lwm;
18060 : 200336 : auto &vec = pending_table->get_or_insert (key);
18061 : 200336 : vec.safe_push (index);
18062 : : }
18063 : :
18064 : 1448 : dump.outdent ();
18065 : 1448 : if (!sec.end (from ()))
18066 : : return false;
18067 : : return true;
18068 : 1448 : }
18069 : :
18070 : : /* Read & write locations. */
18071 : : enum loc_kind {
18072 : : LK_ORDINARY,
18073 : : LK_MACRO,
18074 : : LK_IMPORT_ORDINARY,
18075 : : LK_IMPORT_MACRO,
18076 : : LK_ADHOC,
18077 : : LK_RESERVED,
18078 : : };
18079 : :
18080 : : static const module_state *
18081 : 3884 : module_for_ordinary_loc (location_t loc)
18082 : : {
18083 : 3884 : unsigned pos = 0;
18084 : 7768 : unsigned len = ool->length () - pos;
18085 : :
18086 : 3887 : while (len)
18087 : : {
18088 : 3887 : unsigned half = len / 2;
18089 : 3887 : module_state *probe = (*ool)[pos + half];
18090 : 3887 : if (loc < probe->ordinary_locs.first)
18091 : : len = half;
18092 : 3884 : else if (loc < probe->ordinary_locs.first + probe->ordinary_locs.second)
18093 : : return probe;
18094 : : else
18095 : : {
18096 : 0 : pos += half + 1;
18097 : 0 : len = len - (half + 1);
18098 : : }
18099 : : }
18100 : :
18101 : : return nullptr;
18102 : : }
18103 : :
18104 : : static const module_state *
18105 : 6 : module_for_macro_loc (location_t loc)
18106 : : {
18107 : 6 : unsigned pos = 1;
18108 : 6 : unsigned len = modules->length () - pos;
18109 : :
18110 : 6 : while (len)
18111 : : {
18112 : 6 : unsigned half = len / 2;
18113 : 6 : module_state *probe = (*modules)[pos + half];
18114 : 6 : if (loc < probe->macro_locs.first)
18115 : : {
18116 : 0 : pos += half + 1;
18117 : 0 : len = len - (half + 1);
18118 : : }
18119 : 6 : else if (loc >= probe->macro_locs.first + probe->macro_locs.second)
18120 : : len = half;
18121 : : else
18122 : : return probe;
18123 : : }
18124 : :
18125 : : return NULL;
18126 : : }
18127 : :
18128 : : location_t
18129 : 1163 : module_state::imported_from () const
18130 : : {
18131 : 1163 : location_t from = loc;
18132 : 1163 : line_map_ordinary const *fmap
18133 : 1163 : = linemap_check_ordinary (linemap_lookup (line_table, from));
18134 : :
18135 : 1163 : if (MAP_MODULE_P (fmap))
18136 : 1163 : from = linemap_included_from (fmap);
18137 : :
18138 : 1163 : return from;
18139 : : }
18140 : :
18141 : : /* Note that LOC will need writing. This allows us to prune locations
18142 : : that are not needed. */
18143 : :
18144 : : bool
18145 : 16754797 : module_state::note_location (location_t loc)
18146 : : {
18147 : 16754797 : bool added = false;
18148 : 16754797 : if (!macro_loc_table && !ord_loc_table)
18149 : : ;
18150 : 16754797 : else if (loc < RESERVED_LOCATION_COUNT)
18151 : : ;
18152 : 15249718 : else if (IS_ADHOC_LOC (loc))
18153 : : {
18154 : 1842744 : location_t locus = get_location_from_adhoc_loc (line_table, loc);
18155 : 1842744 : note_location (locus);
18156 : 1842744 : source_range range = get_range_from_loc (line_table, loc);
18157 : 1842744 : if (range.m_start != locus)
18158 : 1783515 : note_location (range.m_start);
18159 : 1842744 : note_location (range.m_finish);
18160 : : }
18161 : 13406974 : else if (loc >= LINEMAPS_MACRO_LOWEST_LOCATION (line_table))
18162 : : {
18163 : 1050316 : if (spans.macro (loc))
18164 : : {
18165 : 1050310 : const line_map *map = linemap_lookup (line_table, loc);
18166 : 1050310 : const line_map_macro *mac_map = linemap_check_macro (map);
18167 : 1050310 : hashval_t hv = macro_loc_traits::hash (mac_map);
18168 : 1050310 : macro_loc_info *slot
18169 : 1050310 : = macro_loc_table->find_slot_with_hash (mac_map, hv, INSERT);
18170 : 1050310 : if (!slot->src)
18171 : : {
18172 : 121548 : slot->src = mac_map;
18173 : 121548 : slot->remap = 0;
18174 : : // Expansion locations could themselves be from a
18175 : : // macro, we need to note them all.
18176 : 121548 : note_location (mac_map->m_expansion);
18177 : 121548 : gcc_checking_assert (mac_map->n_tokens);
18178 : 121548 : location_t tloc = UNKNOWN_LOCATION;
18179 : 4300106 : for (unsigned ix = mac_map->n_tokens * 2; ix--;)
18180 : 4178558 : if (mac_map->macro_locations[ix] != tloc)
18181 : : {
18182 : 2213415 : tloc = mac_map->macro_locations[ix];
18183 : 2213415 : note_location (tloc);
18184 : : }
18185 : : added = true;
18186 : : }
18187 : : }
18188 : : }
18189 : 12356658 : else if (IS_ORDINARY_LOC (loc))
18190 : : {
18191 : 12356658 : if (spans.ordinary (loc))
18192 : : {
18193 : 12352768 : const line_map *map = linemap_lookup (line_table, loc);
18194 : 12352768 : const line_map_ordinary *ord_map = linemap_check_ordinary (map);
18195 : 12352768 : ord_loc_info lkup;
18196 : 12352768 : lkup.src = ord_map;
18197 : 12352768 : lkup.span = loc_one << ord_map->m_column_and_range_bits;
18198 : 12352768 : lkup.offset = (loc - MAP_START_LOCATION (ord_map)) & ~(lkup.span - 1);
18199 : 12352768 : lkup.remap = 0;
18200 : 12352768 : ord_loc_info *slot = (ord_loc_table->find_slot_with_hash
18201 : 12352768 : (lkup, ord_loc_traits::hash (lkup), INSERT));
18202 : 12352768 : if (!slot->src)
18203 : : {
18204 : 1498623 : *slot = lkup;
18205 : 1498623 : added = true;
18206 : : }
18207 : : }
18208 : : }
18209 : : else
18210 : 0 : gcc_unreachable ();
18211 : 16754797 : return added;
18212 : : }
18213 : :
18214 : : /* If we're not streaming, record that we need location LOC.
18215 : : Otherwise stream it. */
18216 : :
18217 : : void
18218 : 24873351 : module_state::write_location (bytes_out &sec, location_t loc)
18219 : : {
18220 : 24873351 : if (!sec.streaming_p ())
18221 : : {
18222 : 8702643 : note_location (loc);
18223 : 8702643 : return;
18224 : : }
18225 : :
18226 : 16170708 : if (loc < RESERVED_LOCATION_COUNT)
18227 : : {
18228 : 1541543 : dump (dumper::LOCATION) && dump ("Reserved location %K", loc);
18229 : 1541525 : sec.loc (LK_RESERVED + loc);
18230 : : }
18231 : 14629183 : else if (IS_ADHOC_LOC (loc))
18232 : : {
18233 : 1797037 : dump (dumper::LOCATION) && dump ("Adhoc location");
18234 : 1797034 : sec.u (LK_ADHOC);
18235 : 1797034 : location_t locus = get_location_from_adhoc_loc (line_table, loc);
18236 : 1797034 : write_location (sec, locus);
18237 : 1797034 : source_range range = get_range_from_loc (line_table, loc);
18238 : 1797034 : if (range.m_start == locus)
18239 : : /* Compress. */
18240 : 55620 : range.m_start = UNKNOWN_LOCATION;
18241 : 1797034 : write_location (sec, range.m_start);
18242 : 1797034 : write_location (sec, range.m_finish);
18243 : 1797034 : unsigned discriminator = get_discriminator_from_adhoc_loc (line_table, loc);
18244 : 1797034 : sec.u (discriminator);
18245 : : }
18246 : 12832149 : else if (loc >= LINEMAPS_MACRO_LOWEST_LOCATION (line_table))
18247 : : {
18248 : 1044215 : const macro_loc_info *info = nullptr;
18249 : 1044215 : line_map_uint_t offset = 0;
18250 : 1044215 : if (unsigned hwm = macro_loc_remap->length ())
18251 : : {
18252 : 1044209 : info = macro_loc_remap->begin ();
18253 : 15363024 : while (hwm != 1)
18254 : : {
18255 : 13274606 : unsigned mid = hwm / 2;
18256 : 13274606 : if (MAP_START_LOCATION (info[mid].src) <= loc)
18257 : : {
18258 : 6707392 : info += mid;
18259 : 6707392 : hwm -= mid;
18260 : : }
18261 : : else
18262 : : hwm = mid;
18263 : : }
18264 : 1044209 : offset = loc - MAP_START_LOCATION (info->src);
18265 : 1044209 : if (offset > info->src->n_tokens)
18266 : 0 : info = nullptr;
18267 : : }
18268 : :
18269 : 1044215 : gcc_checking_assert (bool (info) == bool (spans.macro (loc)));
18270 : :
18271 : 1044215 : if (info)
18272 : : {
18273 : 1044209 : offset += info->remap;
18274 : 1044209 : sec.u (LK_MACRO);
18275 : 1044209 : sec.loc (offset);
18276 : 1044209 : dump (dumper::LOCATION)
18277 : 9 : && dump ("Macro location %K output %K", loc, offset);
18278 : : }
18279 : 6 : else if (const module_state *import = module_for_macro_loc (loc))
18280 : : {
18281 : 6 : auto off = loc - import->macro_locs.first;
18282 : 6 : sec.u (LK_IMPORT_MACRO);
18283 : 6 : sec.u (import->remap);
18284 : 6 : sec.loc (off);
18285 : 6 : dump (dumper::LOCATION)
18286 : 0 : && dump ("Imported macro location %K output %u:%K",
18287 : 0 : loc, import->remap, off);
18288 : : }
18289 : : else
18290 : 0 : gcc_unreachable ();
18291 : : }
18292 : 11787934 : else if (IS_ORDINARY_LOC (loc))
18293 : : {
18294 : : /* If we ran out of locations for imported decls, this location could
18295 : : be a module unit's location. In that case, remap the location
18296 : : to be where we imported the module from. */
18297 : 11787934 : if (spans.locations_exhausted_p () || CHECKING_P)
18298 : : {
18299 : 11787934 : const line_map_ordinary *map
18300 : 11787934 : = linemap_check_ordinary (linemap_lookup (line_table, loc));
18301 : 11787934 : if (MAP_MODULE_P (map) && loc == MAP_START_LOCATION (map))
18302 : : {
18303 : 0 : gcc_checking_assert (spans.locations_exhausted_p ());
18304 : 0 : write_location (sec, linemap_included_from (map));
18305 : 0 : return;
18306 : : }
18307 : : }
18308 : :
18309 : 11787934 : const ord_loc_info *info = nullptr;
18310 : 11787934 : line_map_uint_t offset = 0;
18311 : 11787934 : if (line_map_uint_t hwm = ord_loc_remap->length ())
18312 : : {
18313 : 11787934 : info = ord_loc_remap->begin ();
18314 : 170678702 : while (hwm != 1)
18315 : : {
18316 : 147102834 : auto mid = hwm / 2;
18317 : 147102834 : if (MAP_START_LOCATION (info[mid].src) + info[mid].offset <= loc)
18318 : : {
18319 : 77143319 : info += mid;
18320 : 77143319 : hwm -= mid;
18321 : : }
18322 : : else
18323 : : hwm = mid;
18324 : : }
18325 : 11787934 : offset = loc - MAP_START_LOCATION (info->src) - info->offset;
18326 : 11787934 : if (offset > info->span)
18327 : 3884 : info = nullptr;
18328 : : }
18329 : :
18330 : 11787934 : gcc_checking_assert (bool (info) == bool (spans.ordinary (loc)));
18331 : :
18332 : 11787934 : if (info)
18333 : : {
18334 : 11784050 : offset += info->remap;
18335 : 11784050 : sec.u (LK_ORDINARY);
18336 : 11784050 : sec.loc (offset);
18337 : :
18338 : 11784050 : dump (dumper::LOCATION)
18339 : 78 : && dump ("Ordinary location %K output %K", loc, offset);
18340 : : }
18341 : 3884 : else if (const module_state *import = module_for_ordinary_loc (loc))
18342 : : {
18343 : 3884 : auto off = loc - import->ordinary_locs.first;
18344 : 3884 : sec.u (LK_IMPORT_ORDINARY);
18345 : 3884 : sec.u (import->remap);
18346 : 3884 : sec.loc (off);
18347 : 3884 : dump (dumper::LOCATION)
18348 : 0 : && dump ("Imported ordinary location %K output %u:%K",
18349 : 0 : loc, import->remap, off);
18350 : : }
18351 : : else
18352 : 0 : gcc_unreachable ();
18353 : : }
18354 : : else
18355 : 0 : gcc_unreachable ();
18356 : : }
18357 : :
18358 : : location_t
18359 : 14692217 : module_state::read_location (bytes_in &sec) const
18360 : : {
18361 : 14692217 : location_t locus = UNKNOWN_LOCATION;
18362 : 14692217 : unsigned kind = sec.u ();
18363 : 14692217 : switch (kind)
18364 : : {
18365 : 1356319 : default:
18366 : 1356319 : {
18367 : 1356319 : if (kind < LK_RESERVED + RESERVED_LOCATION_COUNT)
18368 : 1356319 : locus = location_t (kind - LK_RESERVED);
18369 : : else
18370 : 0 : sec.set_overrun ();
18371 : 1356319 : dump (dumper::LOCATION)
18372 : 0 : && dump ("Reserved location %K", locus);
18373 : : }
18374 : : break;
18375 : :
18376 : 1562413 : case LK_ADHOC:
18377 : 1562413 : {
18378 : 1562413 : dump (dumper::LOCATION) && dump ("Adhoc location");
18379 : 1562413 : locus = read_location (sec);
18380 : 1562413 : source_range range;
18381 : 1562413 : range.m_start = read_location (sec);
18382 : 1562413 : if (range.m_start == UNKNOWN_LOCATION)
18383 : 45915 : range.m_start = locus;
18384 : 1562413 : range.m_finish = read_location (sec);
18385 : 1562413 : unsigned discriminator = sec.u ();
18386 : 1562413 : if (locus != loc && range.m_start != loc && range.m_finish != loc)
18387 : 1562413 : locus = line_table->get_or_create_combined_loc (locus, range,
18388 : : nullptr, discriminator);
18389 : : }
18390 : : break;
18391 : :
18392 : 1122968 : case LK_MACRO:
18393 : 1122968 : {
18394 : 1122968 : auto off = sec.loc ();
18395 : :
18396 : 1122968 : if (macro_locs.second)
18397 : : {
18398 : 1122968 : if (off < macro_locs.second)
18399 : 1122968 : locus = off + macro_locs.first;
18400 : : else
18401 : 0 : sec.set_overrun ();
18402 : : }
18403 : : else
18404 : 0 : locus = loc;
18405 : 1122968 : dump (dumper::LOCATION)
18406 : 0 : && dump ("Macro %K becoming %K", off, locus);
18407 : : }
18408 : : break;
18409 : :
18410 : 10649350 : case LK_ORDINARY:
18411 : 10649350 : {
18412 : 10649350 : auto off = sec.loc ();
18413 : 10649350 : if (ordinary_locs.second)
18414 : : {
18415 : 10649350 : if (off < ordinary_locs.second)
18416 : 10649350 : locus = off + ordinary_locs.first;
18417 : : else
18418 : 0 : sec.set_overrun ();
18419 : : }
18420 : : else
18421 : 0 : locus = loc;
18422 : :
18423 : 10649350 : dump (dumper::LOCATION)
18424 : 0 : && dump ("Ordinary location %K becoming %K", off, locus);
18425 : : }
18426 : : break;
18427 : :
18428 : 1167 : case LK_IMPORT_MACRO:
18429 : 1167 : case LK_IMPORT_ORDINARY:
18430 : 1167 : {
18431 : 1167 : unsigned mod = sec.u ();
18432 : 1167 : location_t off = sec.loc ();
18433 : 1167 : const module_state *import = NULL;
18434 : :
18435 : 1167 : if (!mod && !slurp->remap)
18436 : : /* This is an early read of a partition location during the
18437 : : read of our ordinary location map. */
18438 : : import = this;
18439 : : else
18440 : : {
18441 : 1167 : mod = slurp->remap_module (mod);
18442 : 1167 : if (!mod)
18443 : 0 : sec.set_overrun ();
18444 : : else
18445 : 1167 : import = (*modules)[mod];
18446 : : }
18447 : :
18448 : 1167 : if (import)
18449 : : {
18450 : 1167 : if (kind == LK_IMPORT_MACRO)
18451 : : {
18452 : 6 : if (!import->macro_locs.second)
18453 : 0 : locus = import->loc;
18454 : 6 : else if (off < import->macro_locs.second)
18455 : 6 : locus = off + import->macro_locs.first;
18456 : : else
18457 : 0 : sec.set_overrun ();
18458 : : }
18459 : : else
18460 : : {
18461 : 1161 : if (!import->ordinary_locs.second)
18462 : 0 : locus = import->loc;
18463 : 1161 : else if (off < import->ordinary_locs.second)
18464 : 1161 : locus = import->ordinary_locs.first + off;
18465 : : else
18466 : 0 : sec.set_overrun ();
18467 : : }
18468 : : }
18469 : : }
18470 : : break;
18471 : : }
18472 : :
18473 : 14692217 : return locus;
18474 : : }
18475 : :
18476 : : /* Allocate hash tables to record needed locations. */
18477 : :
18478 : : void
18479 : 2573 : module_state::write_init_maps ()
18480 : : {
18481 : 2573 : macro_loc_table = new hash_table<macro_loc_traits> (EXPERIMENT (1, 400));
18482 : 2573 : ord_loc_table = new hash_table<ord_loc_traits> (EXPERIMENT (1, 400));
18483 : 2573 : }
18484 : :
18485 : : /* Prepare the span adjustments. We prune unneeded locations -- at
18486 : : this point every needed location must have been seen by
18487 : : note_location. */
18488 : :
18489 : : range_t
18490 : 2544 : module_state::write_prepare_maps (module_state_config *cfg, bool has_partitions)
18491 : : {
18492 : 2817 : dump () && dump ("Preparing locations");
18493 : 2544 : dump.indent ();
18494 : :
18495 : 2817 : dump () && dump ("Reserved locations [%K,%K) macro [%K,%K)",
18496 : 273 : spans[loc_spans::SPAN_RESERVED].ordinary.first,
18497 : 273 : spans[loc_spans::SPAN_RESERVED].ordinary.second,
18498 : 273 : spans[loc_spans::SPAN_RESERVED].macro.first,
18499 : 273 : spans[loc_spans::SPAN_RESERVED].macro.second);
18500 : :
18501 : 2544 : range_t info {0, 0};
18502 : :
18503 : : // Sort the noted lines.
18504 : 2544 : vec_alloc (ord_loc_remap, ord_loc_table->size ());
18505 : 2544 : for (auto iter = ord_loc_table->begin (), end = ord_loc_table->end ();
18506 : 2978324 : iter != end; ++iter)
18507 : 1487890 : ord_loc_remap->quick_push (*iter);
18508 : 2544 : ord_loc_remap->qsort (&ord_loc_info::compare);
18509 : :
18510 : : // Note included-from maps.
18511 : 2544 : bool added = false;
18512 : 2544 : const line_map_ordinary *current = nullptr;
18513 : 1495522 : for (auto iter = ord_loc_remap->begin (), end = ord_loc_remap->end ();
18514 : 1490434 : iter != end; ++iter)
18515 : 1487890 : if (iter->src != current)
18516 : : {
18517 : 26379 : current = iter->src;
18518 : 10221 : for (auto probe = current;
18519 : 26379 : auto from = linemap_included_from (probe);
18520 : 10221 : probe = linemap_check_ordinary (linemap_lookup (line_table, from)))
18521 : : {
18522 : 23566 : if (has_partitions)
18523 : : {
18524 : : // Partition locations need to elide their module map
18525 : : // entry.
18526 : 184 : probe
18527 : 184 : = linemap_check_ordinary (linemap_lookup (line_table, from));
18528 : 184 : if (MAP_MODULE_P (probe))
18529 : 154 : from = linemap_included_from (probe);
18530 : : }
18531 : :
18532 : 23566 : if (!note_location (from))
18533 : : break;
18534 : 10221 : added = true;
18535 : 10221 : }
18536 : : }
18537 : 2544 : if (added)
18538 : : {
18539 : : // Reconstruct the line array as we added items to the hash table.
18540 : 466 : vec_free (ord_loc_remap);
18541 : 466 : vec_alloc (ord_loc_remap, ord_loc_table->size ());
18542 : 466 : for (auto iter = ord_loc_table->begin (), end = ord_loc_table->end ();
18543 : 2975450 : iter != end; ++iter)
18544 : 1487492 : ord_loc_remap->quick_push (*iter);
18545 : 466 : ord_loc_remap->qsort (&ord_loc_info::compare);
18546 : : }
18547 : 2544 : delete ord_loc_table;
18548 : 2544 : ord_loc_table = nullptr;
18549 : :
18550 : : // Merge (sufficiently) adjacent spans, and calculate remapping.
18551 : 2544 : constexpr line_map_uint_t adjacency = 2; // Allow 2 missing lines.
18552 : 5088 : auto begin = ord_loc_remap->begin (), end = ord_loc_remap->end ();
18553 : 2544 : auto dst = begin;
18554 : 2544 : line_map_uint_t offset = 0;
18555 : 2544 : unsigned range_bits = 0;
18556 : 2544 : ord_loc_info *base = nullptr;
18557 : 1500655 : for (auto iter = begin; iter != end; ++iter)
18558 : : {
18559 : 1498111 : if (base && iter->src == base->src)
18560 : : {
18561 : 2785481 : if (base->offset + base->span +
18562 : 1475799 : ((adjacency << base->src->m_column_and_range_bits)
18563 : : // If there are few c&r bits, allow further separation.
18564 : 1475799 : | (adjacency << 4))
18565 : 1475799 : >= iter->offset)
18566 : : {
18567 : : // Merge.
18568 : 1309682 : offset -= base->span;
18569 : 1309682 : base->span = iter->offset + iter->span - base->offset;
18570 : 1309682 : offset += base->span;
18571 : 1309682 : continue;
18572 : : }
18573 : : }
18574 : 22312 : else if (range_bits < iter->src->m_range_bits)
18575 : 2448 : range_bits = iter->src->m_range_bits;
18576 : :
18577 : 188429 : offset += ((loc_one << iter->src->m_range_bits) - 1);
18578 : 188429 : offset &= ~((loc_one << iter->src->m_range_bits) - 1);
18579 : 188429 : iter->remap = offset;
18580 : 188429 : offset += iter->span;
18581 : 188429 : base = dst;
18582 : 188429 : *dst++ = *iter;
18583 : : }
18584 : 2544 : ord_loc_remap->truncate (dst - begin);
18585 : :
18586 : 2544 : info.first = ord_loc_remap->length ();
18587 : 2544 : cfg->ordinary_locs = offset;
18588 : 2544 : cfg->loc_range_bits = range_bits;
18589 : 2817 : dump () && dump ("Ordinary maps:%K locs:%K range_bits:%u",
18590 : : info.first,
18591 : : cfg->ordinary_locs,
18592 : : cfg->loc_range_bits);
18593 : :
18594 : : // Remap the macro locations.
18595 : 2544 : vec_alloc (macro_loc_remap, macro_loc_table->size ());
18596 : 2544 : for (auto iter = macro_loc_table->begin (), end = macro_loc_table->end ();
18597 : 245640 : iter != end; ++iter)
18598 : 121548 : macro_loc_remap->quick_push (*iter);
18599 : 2544 : delete macro_loc_table;
18600 : 2544 : macro_loc_table = nullptr;
18601 : :
18602 : 2544 : macro_loc_remap->qsort (¯o_loc_info::compare);
18603 : 2544 : offset = 0;
18604 : 7632 : for (auto iter = macro_loc_remap->begin (), end = macro_loc_remap->end ();
18605 : 124092 : iter != end; ++iter)
18606 : : {
18607 : 121548 : auto mac = iter->src;
18608 : 121548 : iter->remap = offset;
18609 : 121548 : offset += mac->n_tokens;
18610 : : }
18611 : 2544 : info.second = macro_loc_remap->length ();
18612 : 2544 : cfg->macro_locs = offset;
18613 : :
18614 : 2817 : dump () && dump ("Macro maps:%K locs:%K", info.second, cfg->macro_locs);
18615 : :
18616 : 2544 : dump.outdent ();
18617 : :
18618 : : // If we have no ordinary locs, we must also have no macro locs.
18619 : 2544 : gcc_checking_assert (cfg->ordinary_locs || !cfg->macro_locs);
18620 : :
18621 : 2544 : return info;
18622 : : }
18623 : :
18624 : : bool
18625 : 2773 : module_state::read_prepare_maps (const module_state_config *cfg)
18626 : : {
18627 : 2773 : location_t ordinary = line_table->highest_location + 1;
18628 : 2773 : ordinary += cfg->ordinary_locs;
18629 : :
18630 : 2773 : location_t macro = LINEMAPS_MACRO_LOWEST_LOCATION (line_table);
18631 : 2773 : macro -= cfg->macro_locs;
18632 : :
18633 : 2773 : if (ordinary < LINE_MAP_MAX_LOCATION_WITH_COLS
18634 : 2773 : && macro >= LINE_MAP_MAX_LOCATION)
18635 : : /* OK, we have enough locations. */
18636 : : return true;
18637 : :
18638 : 0 : ordinary_locs.first = ordinary_locs.second = 0;
18639 : 0 : macro_locs.first = macro_locs.second = 0;
18640 : :
18641 : 0 : spans.report_location_exhaustion (loc);
18642 : :
18643 : : return false;
18644 : : }
18645 : :
18646 : : /* Write & read the location maps. Not called if there are no
18647 : : locations. */
18648 : :
18649 : : void
18650 : 2448 : module_state::write_ordinary_maps (elf_out *to, range_t &info,
18651 : : bool has_partitions, unsigned *crc_p)
18652 : : {
18653 : 2699 : dump () && dump ("Writing ordinary location maps");
18654 : 2448 : dump.indent ();
18655 : :
18656 : 2448 : vec<const char *> filenames;
18657 : 2448 : filenames.create (20);
18658 : :
18659 : : /* Determine the unique filenames. */
18660 : 2448 : const line_map_ordinary *current = nullptr;
18661 : 195773 : for (auto iter = ord_loc_remap->begin (), end = ord_loc_remap->end ();
18662 : 190877 : iter != end; ++iter)
18663 : 188429 : if (iter->src != current)
18664 : : {
18665 : 22312 : current = iter->src;
18666 : 22312 : const char *fname = ORDINARY_MAP_FILE_NAME (iter->src);
18667 : :
18668 : : /* We should never find a module linemap in an interval. */
18669 : 22312 : gcc_checking_assert (!MAP_MODULE_P (iter->src));
18670 : :
18671 : : /* We expect very few filenames, so just an array.
18672 : : (Not true when headers are still in play :() */
18673 : 1304742 : for (unsigned jx = filenames.length (); jx--;)
18674 : : {
18675 : 1270445 : const char *name = filenames[jx];
18676 : 1270445 : if (0 == strcmp (name, fname))
18677 : : {
18678 : : /* Reset the linemap's name, because for things like
18679 : : preprocessed input we could have multiple instances
18680 : : of the same name, and we'd rather not percolate
18681 : : that. */
18682 : 10327 : const_cast<line_map_ordinary *> (iter->src)->to_file = name;
18683 : 10327 : fname = NULL;
18684 : 10327 : break;
18685 : : }
18686 : : }
18687 : 22312 : if (fname)
18688 : 11985 : filenames.safe_push (fname);
18689 : : }
18690 : :
18691 : 2448 : bytes_out sec (to);
18692 : 2448 : sec.begin ();
18693 : :
18694 : : /* Write the filenames. */
18695 : 2448 : unsigned len = filenames.length ();
18696 : 2448 : sec.u (len);
18697 : 2699 : dump () && dump ("%u source file names", len);
18698 : 14433 : for (unsigned ix = 0; ix != len; ix++)
18699 : : {
18700 : 11985 : const char *fname = filenames[ix];
18701 : 12000 : dump (dumper::LOCATION) && dump ("Source file[%u]=%s", ix, fname);
18702 : 11985 : sec.str (fname);
18703 : : }
18704 : :
18705 : 2448 : sec.loc (info.first); /* Num maps. */
18706 : 2448 : const ord_loc_info *base = nullptr;
18707 : 195773 : for (auto iter = ord_loc_remap->begin (), end = ord_loc_remap->end ();
18708 : 190877 : iter != end; ++iter)
18709 : : {
18710 : 188429 : dump (dumper::LOCATION)
18711 : 36 : && dump ("Span:%K ordinary [%K+%K,+%K)->[%K,+%K)",
18712 : 36 : (location_t) (iter - ord_loc_remap->begin ()),
18713 : 18 : MAP_START_LOCATION (iter->src),
18714 : : iter->offset, iter->span, iter->remap,
18715 : : iter->span);
18716 : :
18717 : 188429 : if (!base || iter->src != base->src)
18718 : 22312 : base = iter;
18719 : 188429 : sec.loc (iter->offset - base->offset);
18720 : 188429 : if (base == iter)
18721 : : {
18722 : 22312 : sec.u (iter->src->sysp);
18723 : 22312 : sec.u (iter->src->m_range_bits);
18724 : 22312 : sec.u (iter->src->m_column_and_range_bits - iter->src->m_range_bits);
18725 : :
18726 : 22312 : const char *fname = ORDINARY_MAP_FILE_NAME (iter->src);
18727 : 4067264 : for (unsigned ix = 0; ix != filenames.length (); ix++)
18728 : 2033632 : if (filenames[ix] == fname)
18729 : : {
18730 : 22312 : sec.u (ix);
18731 : 22312 : break;
18732 : : }
18733 : 22312 : unsigned line = ORDINARY_MAP_STARTING_LINE_NUMBER (iter->src);
18734 : 22312 : line += iter->offset >> iter->src->m_column_and_range_bits;
18735 : 22312 : sec.u (line);
18736 : : }
18737 : 188429 : sec.loc (iter->remap);
18738 : 188429 : if (base == iter)
18739 : : {
18740 : : /* Write the included from location, which means reading it
18741 : : while reading in the ordinary maps. So we'd better not
18742 : : be getting ahead of ourselves. */
18743 : 22312 : location_t from = linemap_included_from (iter->src);
18744 : 22312 : gcc_checking_assert (from < MAP_START_LOCATION (iter->src));
18745 : 22312 : if (from != UNKNOWN_LOCATION && has_partitions)
18746 : : {
18747 : : /* A partition's span will have a from pointing at a
18748 : : MODULE_INC. Find that map's from. */
18749 : 178 : line_map_ordinary const *fmap
18750 : 178 : = linemap_check_ordinary (linemap_lookup (line_table, from));
18751 : 178 : if (MAP_MODULE_P (fmap))
18752 : 148 : from = linemap_included_from (fmap);
18753 : : }
18754 : 22312 : write_location (sec, from);
18755 : : }
18756 : : }
18757 : :
18758 : 2448 : filenames.release ();
18759 : :
18760 : 2448 : sec.end (to, to->name (MOD_SNAME_PFX ".olm"), crc_p);
18761 : 2448 : dump.outdent ();
18762 : 2448 : }
18763 : :
18764 : : /* Return the prefix to use for dumping a #pragma diagnostic change to DK. */
18765 : :
18766 : : static const char *
18767 : 852 : dk_string (enum diagnostics::kind dk)
18768 : : {
18769 : 852 : gcc_assert (dk > diagnostics::kind::unspecified
18770 : : && dk < diagnostics::kind::last_diagnostic_kind);
18771 : 852 : if (dk == diagnostics::kind::ignored)
18772 : : /* diagnostics/kinds.def has an empty string for ignored. */
18773 : : return "ignored: ";
18774 : : else
18775 : 0 : return diagnostics::get_text_for_kind (dk);
18776 : : }
18777 : :
18778 : : /* Dump one #pragma GCC diagnostic entry. */
18779 : :
18780 : : static bool
18781 : 1738 : dump_dc_change (unsigned index, unsigned opt, enum diagnostics::kind dk)
18782 : : {
18783 : 1738 : if (dk == diagnostics::kind::pop)
18784 : 886 : return dump (" Index %u: pop from %d", index, opt);
18785 : : else
18786 : 852 : return dump (" Index %u: %s%s", index, dk_string (dk),
18787 : 1704 : cl_options[opt].opt_text);
18788 : : }
18789 : :
18790 : : /* Write out any #pragma GCC diagnostic info to the .dgc section. */
18791 : :
18792 : : void
18793 : 4992 : module_state::write_diagnostic_classification (elf_out *to,
18794 : : diagnostics::context *dc,
18795 : : unsigned *crc_p)
18796 : : {
18797 : 4992 : auto &changes = dc->get_classification_history ();
18798 : :
18799 : 4992 : bytes_out sec (to);
18800 : 4992 : if (sec.streaming_p ())
18801 : : {
18802 : 2448 : sec.begin ();
18803 : 2699 : dump () && dump ("Writing diagnostic change locations");
18804 : 2448 : dump.indent ();
18805 : : }
18806 : :
18807 : 4992 : unsigned len = changes.length ();
18808 : :
18809 : : /* We don't want to write out any entries that came from one of our imports.
18810 : : But then we need to adjust the total, and change diagnostics::kind::pop
18811 : : targets to match the index in our actual output. So remember how many
18812 : : lines we had skipped at each step, where -1 means this line itself
18813 : : is skipped. */
18814 : 4992 : int skips = 0;
18815 : 4992 : auto_vec<int> skips_at (len);
18816 : 4992 : skips_at.safe_grow (len);
18817 : :
18818 : 45204 : for (unsigned i = 0; i < len; ++i)
18819 : : {
18820 : 40212 : const auto &c = changes[i];
18821 : 40212 : skips_at[i] = skips;
18822 : 40212 : if (linemap_location_from_module_p (line_table, c.location))
18823 : : {
18824 : 6346 : ++skips;
18825 : 6346 : skips_at[i] = -1;
18826 : 6346 : continue;
18827 : : }
18828 : : }
18829 : :
18830 : 4992 : if (sec.streaming_p ())
18831 : : {
18832 : 2448 : sec.u (len - skips);
18833 : 2699 : dump () && dump ("Diagnostic changes: %u", len - skips);
18834 : : }
18835 : :
18836 : 45204 : for (unsigned i = 0; i < len; ++i)
18837 : : {
18838 : 40212 : if (skips_at[i] == -1)
18839 : 6346 : continue;
18840 : :
18841 : 33866 : const auto &c = changes[i];
18842 : 33866 : write_location (sec, c.location);
18843 : 33866 : if (sec.streaming_p ())
18844 : : {
18845 : 16933 : unsigned opt = c.option;
18846 : 16933 : if (c.kind == diagnostics::kind::pop)
18847 : 8643 : opt -= skips_at[opt];
18848 : 16933 : sec.u (opt);
18849 : 16933 : sec.u (static_cast<unsigned> (c.kind));
18850 : 41895 : dump () && dump_dc_change (i - skips_at[i], opt, c.kind);
18851 : : }
18852 : : }
18853 : :
18854 : 4992 : if (sec.streaming_p ())
18855 : : {
18856 : 2448 : sec.end (to, to->name (MOD_SNAME_PFX ".dgc"), crc_p);
18857 : 2448 : dump.outdent ();
18858 : : }
18859 : 4992 : }
18860 : :
18861 : : /* Read any #pragma GCC diagnostic info from the .dgc section. */
18862 : :
18863 : : bool
18864 : 2715 : module_state::read_diagnostic_classification (diagnostics::context *dc)
18865 : : {
18866 : 2715 : bytes_in sec;
18867 : :
18868 : 2715 : if (!sec.begin (loc, from (), MOD_SNAME_PFX ".dgc"))
18869 : : return false;
18870 : :
18871 : 3193 : dump () && dump ("Reading diagnostic change locations");
18872 : 2715 : dump.indent ();
18873 : :
18874 : 2715 : unsigned len = sec.u ();
18875 : 3193 : dump () && dump ("Diagnostic changes: %u", len);
18876 : :
18877 : 2715 : auto &changes = dc->get_classification_history ();
18878 : 2715 : int offset = changes.length ();
18879 : 2715 : changes.reserve (len + 1);
18880 : 22599 : for (unsigned i = 0; i < len; ++i)
18881 : : {
18882 : 19884 : location_t loc = read_location (sec);
18883 : 19884 : int opt = sec.u ();
18884 : 19884 : enum diagnostics::kind kind = (enum diagnostics::kind) sec.u ();
18885 : 19884 : if (kind == diagnostics::kind::pop)
18886 : : /* For a pop, opt is the 'changes' index to return to. */
18887 : 10156 : opt += offset;
18888 : 19884 : changes.quick_push ({ loc, opt, kind });
18889 : 19939 : dump () && dump_dc_change (changes.length () - 1, opt, kind);
18890 : : }
18891 : :
18892 : : /* Did the import pop all its diagnostic changes? */
18893 : 2715 : bool last_was_reset = (len == 0);
18894 : 2715 : if (len)
18895 : 214 : for (int i = changes.length () - 1; ; --i)
18896 : : {
18897 : 8909 : gcc_checking_assert (i >= offset);
18898 : :
18899 : 8909 : const auto &c = changes[i];
18900 : 8909 : if (c.kind != diagnostics::kind::pop)
18901 : : break;
18902 : 8900 : else if (c.option == offset)
18903 : : {
18904 : : last_was_reset = true;
18905 : : break;
18906 : : }
18907 : : else
18908 : : /* As in update_effective_level_from_pragmas, the loop will decrement
18909 : : i so we actually jump to c.option - 1. */
18910 : 8802 : i = c.option;
18911 : 8802 : }
18912 : 2715 : if (!last_was_reset)
18913 : : {
18914 : : /* It didn't, so add a pop at its last location to avoid affecting later
18915 : : imports. */
18916 : 9 : location_t last_loc = ordinary_locs.first + ordinary_locs.second - 1;
18917 : 9 : changes.quick_push ({ last_loc, offset, diagnostics::kind::pop });
18918 : 15 : dump () && dump (" Adding final pop from index %d", offset);
18919 : : }
18920 : :
18921 : 2715 : dump.outdent ();
18922 : 2715 : if (!sec.end (from ()))
18923 : : return false;
18924 : :
18925 : : return true;
18926 : 2715 : }
18927 : :
18928 : : void
18929 : 117 : module_state::write_macro_maps (elf_out *to, range_t &info, unsigned *crc_p)
18930 : : {
18931 : 129 : dump () && dump ("Writing macro location maps");
18932 : 117 : dump.indent ();
18933 : :
18934 : 117 : bytes_out sec (to);
18935 : 117 : sec.begin ();
18936 : :
18937 : 129 : dump () && dump ("Macro maps:%K", info.second);
18938 : 117 : sec.loc (info.second);
18939 : :
18940 : 117 : line_map_uint_t macro_num = 0;
18941 : 234 : for (auto iter = macro_loc_remap->end (), begin = macro_loc_remap->begin ();
18942 : 121665 : iter-- != begin;)
18943 : : {
18944 : 121548 : auto mac = iter->src;
18945 : 121548 : sec.loc (iter->remap);
18946 : 121548 : sec.u (mac->n_tokens);
18947 : 121548 : sec.cpp_node (mac->macro);
18948 : 121548 : write_location (sec, mac->m_expansion);
18949 : 121548 : const location_t *locs = mac->macro_locations;
18950 : : /* There are lots of identical runs. */
18951 : 121548 : location_t prev = UNKNOWN_LOCATION;
18952 : 121548 : unsigned count = 0;
18953 : 121548 : unsigned runs = 0;
18954 : 4300106 : for (unsigned jx = mac->n_tokens * 2; jx--;)
18955 : : {
18956 : 4178558 : location_t tok_loc = locs[jx];
18957 : 4178558 : if (tok_loc == prev)
18958 : : {
18959 : 1965143 : count++;
18960 : 1965143 : continue;
18961 : : }
18962 : 2213415 : runs++;
18963 : 2213415 : sec.u (count);
18964 : 2213415 : count = 1;
18965 : 2213415 : prev = tok_loc;
18966 : 2213415 : write_location (sec, tok_loc);
18967 : : }
18968 : 121548 : sec.u (count);
18969 : 121548 : dump (dumper::LOCATION)
18970 : 9 : && dump ("Macro:%K %I %u/%u*2 locations [%K,%K)->%K",
18971 : 9 : macro_num, identifier (mac->macro),
18972 : : runs, mac->n_tokens,
18973 : : MAP_START_LOCATION (mac),
18974 : 9 : MAP_START_LOCATION (mac) + mac->n_tokens,
18975 : : iter->remap);
18976 : 121548 : macro_num++;
18977 : : }
18978 : 117 : gcc_assert (macro_num == info.second);
18979 : :
18980 : 117 : sec.end (to, to->name (MOD_SNAME_PFX ".mlm"), crc_p);
18981 : 117 : dump.outdent ();
18982 : 117 : }
18983 : :
18984 : : bool
18985 : 2715 : module_state::read_ordinary_maps (line_map_uint_t num_ord_locs,
18986 : : unsigned range_bits)
18987 : : {
18988 : 2715 : bytes_in sec;
18989 : :
18990 : 2715 : if (!sec.begin (loc, from (), MOD_SNAME_PFX ".olm"))
18991 : : return false;
18992 : 3193 : dump () && dump ("Reading ordinary location maps");
18993 : 2715 : dump.indent ();
18994 : :
18995 : : /* Read the filename table. */
18996 : 2715 : unsigned len = sec.u ();
18997 : 3193 : dump () && dump ("%u source file names", len);
18998 : 2715 : vec<const char *> filenames;
18999 : 2715 : filenames.create (len);
19000 : 16696 : for (unsigned ix = 0; ix != len; ix++)
19001 : : {
19002 : 13981 : size_t l;
19003 : 13981 : const char *buf = sec.str (&l);
19004 : 13981 : char *fname = XNEWVEC (char, l + 1);
19005 : 13981 : memcpy (fname, buf, l + 1);
19006 : 13981 : dump (dumper::LOCATION) && dump ("Source file[%u]=%s", ix, fname);
19007 : : /* We leak these names into the line-map table. But it
19008 : : doesn't own them. */
19009 : 13981 : filenames.quick_push (fname);
19010 : : }
19011 : :
19012 : 2715 : line_map_uint_t num_ordinary = sec.loc ();
19013 : 3193 : dump () && dump ("Ordinary maps:%K, range_bits:%u",
19014 : : num_ordinary, range_bits);
19015 : :
19016 : 2715 : location_t offset = line_table->highest_location + 1;
19017 : 2715 : offset += ((loc_one << range_bits) - 1);
19018 : 2715 : offset &= ~((loc_one << range_bits) - 1);
19019 : 2715 : ordinary_locs.first = offset;
19020 : :
19021 : 2715 : bool propagated = spans.maybe_propagate (this, offset);
19022 : 2715 : line_map_ordinary *maps = static_cast<line_map_ordinary *>
19023 : 2715 : (line_map_new_raw (line_table, false, num_ordinary));
19024 : :
19025 : 2715 : const line_map_ordinary *base = nullptr;
19026 : 232535 : for (line_map_uint_t ix = 0; ix != num_ordinary && !sec.get_overrun (); ix++)
19027 : : {
19028 : 229820 : line_map_ordinary *map = &maps[ix];
19029 : :
19030 : 229820 : location_t offset = sec.loc ();
19031 : 229820 : if (!offset)
19032 : : {
19033 : 26395 : map->reason = LC_RENAME;
19034 : 26395 : map->sysp = sec.u ();
19035 : 26395 : map->m_range_bits = sec.u ();
19036 : 26395 : map->m_column_and_range_bits = sec.u () + map->m_range_bits;
19037 : 26395 : unsigned fnum = sec.u ();
19038 : 52790 : map->to_file = (fnum < filenames.length () ? filenames[fnum] : "");
19039 : 26395 : map->to_line = sec.u ();
19040 : 26395 : base = map;
19041 : : }
19042 : : else
19043 : : {
19044 : 203425 : *map = *base;
19045 : 203425 : map->to_line += offset >> map->m_column_and_range_bits;
19046 : : }
19047 : 229820 : location_t remap = sec.loc ();
19048 : 229820 : map->start_location = remap + ordinary_locs.first;
19049 : 229820 : if (base == map)
19050 : : {
19051 : : /* Root the outermost map at our location. */
19052 : 26395 : ordinary_locs.second = remap;
19053 : 26395 : location_t from = read_location (sec);
19054 : 26395 : map->included_from = from != UNKNOWN_LOCATION ? from : loc;
19055 : : }
19056 : : }
19057 : :
19058 : 2715 : ordinary_locs.second = num_ord_locs;
19059 : : /* highest_location is the one handed out, not the next one to
19060 : : hand out. */
19061 : 2715 : line_table->highest_location = ordinary_locs.first + ordinary_locs.second - 1;
19062 : :
19063 : 2715 : if (line_table->highest_location >= LINE_MAP_MAX_LOCATION_WITH_COLS)
19064 : : /* We shouldn't run out of locations, as we checked before
19065 : : starting. */
19066 : 0 : sec.set_overrun ();
19067 : 3193 : dump () && dump ("Ordinary location [%K,+%K)",
19068 : : ordinary_locs.first, ordinary_locs.second);
19069 : :
19070 : 2715 : if (propagated)
19071 : 148 : spans.close ();
19072 : :
19073 : 2715 : filenames.release ();
19074 : :
19075 : 2715 : dump.outdent ();
19076 : 2715 : if (!sec.end (from ()))
19077 : : return false;
19078 : :
19079 : : return true;
19080 : 2715 : }
19081 : :
19082 : : bool
19083 : 127 : module_state::read_macro_maps (line_map_uint_t num_macro_locs)
19084 : : {
19085 : 127 : bytes_in sec;
19086 : :
19087 : 127 : if (!sec.begin (loc, from (), MOD_SNAME_PFX ".mlm"))
19088 : : return false;
19089 : 133 : dump () && dump ("Reading macro location maps");
19090 : 127 : dump.indent ();
19091 : :
19092 : 127 : line_map_uint_t num_macros = sec.loc ();
19093 : 133 : dump () && dump ("Macro maps:%K locs:%K",
19094 : : num_macros, num_macro_locs);
19095 : :
19096 : 254 : bool propagated = spans.maybe_propagate (this,
19097 : 127 : line_table->highest_location + 1);
19098 : :
19099 : 127 : location_t offset = LINEMAPS_MACRO_LOWEST_LOCATION (line_table);
19100 : 127 : macro_locs.second = num_macro_locs;
19101 : 127 : macro_locs.first = offset - num_macro_locs;
19102 : :
19103 : 133 : dump () && dump ("Macro loc delta %K", offset);
19104 : 133 : dump () && dump ("Macro locations [%K,%K)",
19105 : : macro_locs.first, macro_locs.second);
19106 : :
19107 : 153394 : for (line_map_uint_t ix = 0; ix != num_macros && !sec.get_overrun (); ix++)
19108 : : {
19109 : 153267 : location_t offset = sec.loc ();
19110 : 153267 : unsigned n_tokens = sec.u ();
19111 : 153267 : cpp_hashnode *node = sec.cpp_node ();
19112 : 153267 : location_t exp_loc = read_location (sec);
19113 : :
19114 : 153267 : const line_map_macro *macro
19115 : 153267 : = linemap_enter_macro (line_table, node, exp_loc, n_tokens);
19116 : 153267 : if (!macro)
19117 : : /* We shouldn't run out of locations, as we checked that we
19118 : : had enough before starting. */
19119 : : break;
19120 : 153267 : gcc_checking_assert (MAP_START_LOCATION (macro)
19121 : : == offset + macro_locs.first);
19122 : :
19123 : 153267 : location_t *locs = macro->macro_locations;
19124 : 153267 : location_t tok_loc = UNKNOWN_LOCATION;
19125 : 153267 : unsigned count = sec.u ();
19126 : 153267 : unsigned runs = 0;
19127 : 5359401 : for (unsigned jx = macro->n_tokens * 2; jx-- && !sec.get_overrun ();)
19128 : : {
19129 : 7965484 : while (!count-- && !sec.get_overrun ())
19130 : : {
19131 : 2759350 : runs++;
19132 : 2759350 : tok_loc = read_location (sec);
19133 : 2759350 : count = sec.u ();
19134 : : }
19135 : 5206134 : locs[jx] = tok_loc;
19136 : : }
19137 : 153267 : if (count)
19138 : 0 : sec.set_overrun ();
19139 : 153294 : dump (dumper::LOCATION)
19140 : 0 : && dump ("Macro:%K %I %u/%u*2 locations [%K,%K)",
19141 : : ix, identifier (node), runs, n_tokens,
19142 : : MAP_START_LOCATION (macro),
19143 : 0 : MAP_START_LOCATION (macro) + n_tokens);
19144 : : }
19145 : :
19146 : 133 : dump () && dump ("Macro location lwm:%K", macro_locs.first);
19147 : 127 : if (propagated)
19148 : 3 : spans.close ();
19149 : :
19150 : 127 : dump.outdent ();
19151 : 127 : if (!sec.end (from ()))
19152 : : return false;
19153 : :
19154 : : return true;
19155 : 127 : }
19156 : :
19157 : : /* Serialize the definition of MACRO. */
19158 : :
19159 : : void
19160 : 67543 : module_state::write_define (bytes_out &sec, const cpp_macro *macro)
19161 : : {
19162 : 67543 : sec.u (macro->count);
19163 : :
19164 : 67543 : bytes_out::bits_out bits = sec.stream_bits ();
19165 : 67543 : bits.b (macro->fun_like);
19166 : 67543 : bits.b (macro->variadic);
19167 : 67543 : bits.b (macro->syshdr);
19168 : 67543 : bits.bflush ();
19169 : :
19170 : 67543 : write_location (sec, macro->line);
19171 : 67543 : if (macro->fun_like)
19172 : : {
19173 : 9021 : sec.u (macro->paramc);
19174 : 9021 : const cpp_hashnode *const *parms = macro->parm.params;
19175 : 22721 : for (unsigned ix = 0; ix != macro->paramc; ix++)
19176 : 13700 : sec.cpp_node (parms[ix]);
19177 : : }
19178 : :
19179 : : unsigned len = 0;
19180 : 221410 : for (unsigned ix = 0; ix != macro->count; ix++)
19181 : : {
19182 : 153867 : const cpp_token *token = ¯o->exp.tokens[ix];
19183 : 153867 : write_location (sec, token->src_loc);
19184 : 153867 : sec.u (token->type);
19185 : 153867 : sec.u (token->flags);
19186 : 153867 : switch (cpp_token_val_index (token))
19187 : : {
19188 : 0 : default:
19189 : 0 : gcc_unreachable ();
19190 : :
19191 : 11999 : case CPP_TOKEN_FLD_ARG_NO:
19192 : : /* An argument reference. */
19193 : 11999 : sec.u (token->val.macro_arg.arg_no);
19194 : 11999 : sec.cpp_node (token->val.macro_arg.spelling);
19195 : 11999 : break;
19196 : :
19197 : 30175 : case CPP_TOKEN_FLD_NODE:
19198 : : /* An identifier. */
19199 : 30175 : sec.cpp_node (token->val.node.node);
19200 : 30175 : if (token->val.node.spelling == token->val.node.node)
19201 : : /* The spelling will usually be the same. so optimize
19202 : : that. */
19203 : 30175 : sec.str (NULL, 0);
19204 : : else
19205 : 0 : sec.cpp_node (token->val.node.spelling);
19206 : : break;
19207 : :
19208 : : case CPP_TOKEN_FLD_NONE:
19209 : : break;
19210 : :
19211 : 48027 : case CPP_TOKEN_FLD_STR:
19212 : : /* A string, number or comment. Not always NUL terminated,
19213 : : we stream out in a single contatenation with embedded
19214 : : NULs as that's a safe default. */
19215 : 48027 : len += token->val.str.len + 1;
19216 : 48027 : sec.u (token->val.str.len);
19217 : 48027 : break;
19218 : :
19219 : 0 : case CPP_TOKEN_FLD_SOURCE:
19220 : 0 : case CPP_TOKEN_FLD_TOKEN_NO:
19221 : 0 : case CPP_TOKEN_FLD_PRAGMA:
19222 : : /* These do not occur inside a macro itself. */
19223 : 0 : gcc_unreachable ();
19224 : : }
19225 : : }
19226 : :
19227 : 67543 : if (len)
19228 : : {
19229 : 44515 : char *ptr = reinterpret_cast<char *> (sec.buf (len));
19230 : 44515 : len = 0;
19231 : 135936 : for (unsigned ix = 0; ix != macro->count; ix++)
19232 : : {
19233 : 91421 : const cpp_token *token = ¯o->exp.tokens[ix];
19234 : 91421 : if (cpp_token_val_index (token) == CPP_TOKEN_FLD_STR)
19235 : : {
19236 : 48027 : memcpy (ptr + len, token->val.str.text,
19237 : 48027 : token->val.str.len);
19238 : 48027 : len += token->val.str.len;
19239 : 48027 : ptr[len++] = 0;
19240 : : }
19241 : : }
19242 : : }
19243 : 67543 : }
19244 : :
19245 : : /* Read a macro definition. */
19246 : :
19247 : : cpp_macro *
19248 : 321 : module_state::read_define (bytes_in &sec, cpp_reader *reader) const
19249 : : {
19250 : 321 : unsigned count = sec.u ();
19251 : : /* We rely on knowing cpp_reader's hash table is ident_hash, and
19252 : : its subobject allocator is stringpool_ggc_alloc and that is just
19253 : : a wrapper for ggc_alloc_atomic. */
19254 : 321 : cpp_macro *macro
19255 : 642 : = (cpp_macro *)ggc_alloc_atomic (sizeof (cpp_macro)
19256 : 321 : + sizeof (cpp_token) * (count - !!count));
19257 : 321 : memset (macro, 0, sizeof (cpp_macro) + sizeof (cpp_token) * (count - !!count));
19258 : :
19259 : 321 : macro->count = count;
19260 : 321 : macro->kind = cmk_macro;
19261 : 321 : macro->imported_p = true;
19262 : :
19263 : 321 : bytes_in::bits_in bits = sec.stream_bits ();
19264 : 321 : macro->fun_like = bits.b ();
19265 : 321 : macro->variadic = bits.b ();
19266 : 321 : macro->syshdr = bits.b ();
19267 : 321 : bits.bflush ();
19268 : :
19269 : 321 : macro->line = read_location (sec);
19270 : :
19271 : 321 : if (macro->fun_like)
19272 : : {
19273 : 66 : unsigned paramc = sec.u ();
19274 : 66 : cpp_hashnode **params
19275 : 66 : = (cpp_hashnode **)ggc_alloc_atomic (sizeof (cpp_hashnode *) * paramc);
19276 : 66 : macro->paramc = paramc;
19277 : 66 : macro->parm.params = params;
19278 : 141 : for (unsigned ix = 0; ix != paramc; ix++)
19279 : 75 : params[ix] = sec.cpp_node ();
19280 : : }
19281 : :
19282 : : unsigned len = 0;
19283 : 1124 : for (unsigned ix = 0; ix != count && !sec.get_overrun (); ix++)
19284 : : {
19285 : 803 : cpp_token *token = ¯o->exp.tokens[ix];
19286 : 803 : token->src_loc = read_location (sec);
19287 : 803 : token->type = cpp_ttype (sec.u ());
19288 : 803 : token->flags = sec.u ();
19289 : 803 : switch (cpp_token_val_index (token))
19290 : : {
19291 : 0 : default:
19292 : 0 : sec.set_overrun ();
19293 : 0 : break;
19294 : :
19295 : 62 : case CPP_TOKEN_FLD_ARG_NO:
19296 : : /* An argument reference. */
19297 : 62 : {
19298 : 62 : unsigned arg_no = sec.u ();
19299 : 62 : if (arg_no - 1 >= macro->paramc)
19300 : 0 : sec.set_overrun ();
19301 : 62 : token->val.macro_arg.arg_no = arg_no;
19302 : 62 : token->val.macro_arg.spelling = sec.cpp_node ();
19303 : : }
19304 : 62 : break;
19305 : :
19306 : 209 : case CPP_TOKEN_FLD_NODE:
19307 : : /* An identifier. */
19308 : 209 : token->val.node.node = sec.cpp_node ();
19309 : 209 : token->val.node.spelling = sec.cpp_node ();
19310 : 209 : if (!token->val.node.spelling)
19311 : 209 : token->val.node.spelling = token->val.node.node;
19312 : : break;
19313 : :
19314 : : case CPP_TOKEN_FLD_NONE:
19315 : : break;
19316 : :
19317 : 180 : case CPP_TOKEN_FLD_STR:
19318 : : /* A string, number or comment. */
19319 : 180 : token->val.str.len = sec.u ();
19320 : 180 : len += token->val.str.len + 1;
19321 : 180 : break;
19322 : : }
19323 : : }
19324 : :
19325 : 321 : if (len)
19326 : 179 : if (const char *ptr = reinterpret_cast<const char *> (sec.buf (len)))
19327 : : {
19328 : : /* There should be a final NUL. */
19329 : 179 : if (ptr[len-1])
19330 : 0 : sec.set_overrun ();
19331 : : /* cpp_alloc_token_string will add a final NUL. */
19332 : 179 : const unsigned char *buf
19333 : 179 : = cpp_alloc_token_string (reader, (const unsigned char *)ptr, len - 1);
19334 : 179 : len = 0;
19335 : 661 : for (unsigned ix = 0; ix != count && !sec.get_overrun (); ix++)
19336 : : {
19337 : 482 : cpp_token *token = ¯o->exp.tokens[ix];
19338 : 482 : if (cpp_token_val_index (token) == CPP_TOKEN_FLD_STR)
19339 : : {
19340 : 180 : token->val.str.text = buf + len;
19341 : 180 : len += token->val.str.len;
19342 : 180 : if (buf[len++])
19343 : 0 : sec.set_overrun ();
19344 : : }
19345 : : }
19346 : : }
19347 : :
19348 : 321 : if (sec.get_overrun ())
19349 : 0 : return NULL;
19350 : : return macro;
19351 : 321 : }
19352 : :
19353 : : /* Exported macro data. */
19354 : : struct GTY(()) macro_export {
19355 : : cpp_macro *def;
19356 : : location_t undef_loc;
19357 : :
19358 : 97106 : macro_export ()
19359 : 97106 : :def (NULL), undef_loc (UNKNOWN_LOCATION)
19360 : : {
19361 : : }
19362 : : };
19363 : :
19364 : : /* Imported macro data. */
19365 : : class macro_import {
19366 : : public:
19367 : : struct slot {
19368 : : #if defined (WORDS_BIGENDIAN) && SIZEOF_VOID_P == 8
19369 : : int offset;
19370 : : #endif
19371 : : /* We need to ensure we don't use the LSB for representation, as
19372 : : that's the union discriminator below. */
19373 : : unsigned bits;
19374 : :
19375 : : #if !(defined (WORDS_BIGENDIAN) && SIZEOF_VOID_P == 8)
19376 : : int offset;
19377 : : #endif
19378 : :
19379 : : public:
19380 : : enum Layout {
19381 : : L_DEF = 1,
19382 : : L_UNDEF = 2,
19383 : : L_BOTH = 3,
19384 : : L_MODULE_SHIFT = 2
19385 : : };
19386 : :
19387 : : public:
19388 : : /* Not a regular ctor, because we put it in a union, and that's
19389 : : not allowed in C++ 98. */
19390 : 168886 : static slot ctor (unsigned module, unsigned defness)
19391 : : {
19392 : 168886 : gcc_checking_assert (defness);
19393 : 168886 : slot s;
19394 : 168886 : s.bits = defness | (module << L_MODULE_SHIFT);
19395 : 168886 : s.offset = -1;
19396 : 168886 : return s;
19397 : : }
19398 : :
19399 : : public:
19400 : 136607 : unsigned get_defness () const
19401 : : {
19402 : 136607 : return bits & L_BOTH;
19403 : : }
19404 : 98848 : unsigned get_module () const
19405 : : {
19406 : 98848 : return bits >> L_MODULE_SHIFT;
19407 : : }
19408 : 12 : void become_undef ()
19409 : : {
19410 : 12 : bits &= ~unsigned (L_DEF);
19411 : 12 : bits |= unsigned (L_UNDEF);
19412 : : }
19413 : : };
19414 : :
19415 : : private:
19416 : : typedef vec<slot, va_heap, vl_embed> ary_t;
19417 : : union either {
19418 : : /* Discriminated by bits 0|1 != 0. The expected case is that
19419 : : there will be exactly one slot per macro, hence the effort of
19420 : : packing that. */
19421 : : ary_t *ary;
19422 : : slot single;
19423 : : } u;
19424 : :
19425 : : public:
19426 : 134895 : macro_import ()
19427 : 134895 : {
19428 : 134895 : u.ary = NULL;
19429 : : }
19430 : :
19431 : : private:
19432 : 7411922 : bool single_p () const
19433 : : {
19434 : 7411922 : return u.single.bits & slot::L_BOTH;
19435 : : }
19436 : 7546826 : bool occupied_p () const
19437 : : {
19438 : 7546826 : return u.ary != NULL;
19439 : : }
19440 : :
19441 : : public:
19442 : 921 : unsigned length () const
19443 : : {
19444 : 921 : gcc_checking_assert (occupied_p ());
19445 : 921 : return single_p () ? 1 : u.ary->length ();
19446 : : }
19447 : 7280231 : slot &operator[] (unsigned ix)
19448 : : {
19449 : 7280231 : gcc_checking_assert (occupied_p ());
19450 : 7280231 : if (single_p ())
19451 : : {
19452 : 7194162 : gcc_checking_assert (!ix);
19453 : 7194162 : return u.single;
19454 : : }
19455 : : else
19456 : 86069 : return (*u.ary)[ix];
19457 : : }
19458 : :
19459 : : public:
19460 : : slot &exported ();
19461 : : slot &append (unsigned module, unsigned defness);
19462 : : };
19463 : :
19464 : : /* O is a new import to append to the list for. If we're an empty
19465 : : set, initialize us. */
19466 : :
19467 : : macro_import::slot &
19468 : 168886 : macro_import::append (unsigned module, unsigned defness)
19469 : : {
19470 : 168886 : if (!occupied_p ())
19471 : : {
19472 : 134895 : u.single = slot::ctor (module, defness);
19473 : 134895 : return u.single;
19474 : : }
19475 : : else
19476 : : {
19477 : 33991 : bool single = single_p ();
19478 : 33991 : ary_t *m = single ? NULL : u.ary;
19479 : 33991 : vec_safe_reserve (m, 1 + single);
19480 : 33991 : if (single)
19481 : 33988 : m->quick_push (u.single);
19482 : 33991 : u.ary = m;
19483 : 33991 : return *u.ary->quick_push (slot::ctor (module, defness));
19484 : : }
19485 : : }
19486 : :
19487 : : /* We're going to export something. Make sure the first import slot
19488 : : is us. */
19489 : :
19490 : : macro_import::slot &
19491 : 96788 : macro_import::exported ()
19492 : : {
19493 : 96788 : if (occupied_p () && !(*this)[0].get_module ())
19494 : : {
19495 : 9 : slot &res = (*this)[0];
19496 : 9 : res.bits |= slot::L_DEF;
19497 : 9 : return res;
19498 : : }
19499 : :
19500 : 96779 : slot *a = &append (0, slot::L_DEF);
19501 : 96779 : if (!single_p ())
19502 : : {
19503 : 29783 : slot &f = (*this)[0];
19504 : 29783 : std::swap (f, *a);
19505 : 29783 : a = &f;
19506 : : }
19507 : : return *a;
19508 : : }
19509 : :
19510 : : /* The import (&exported) macros. cpp_hasnode's deferred field
19511 : : indexes this array (offset by 1, so zero means 'not present'. */
19512 : :
19513 : : static vec<macro_import, va_heap, vl_embed> *macro_imports;
19514 : :
19515 : : /* The exported macros. A macro_import slot's zeroth element's offset
19516 : : indexes this array. If the zeroth slot is not for module zero,
19517 : : there is no export. */
19518 : :
19519 : : static GTY(()) vec<macro_export, va_gc> *macro_exports;
19520 : :
19521 : : /* The reachable set of header imports from this TU. */
19522 : :
19523 : : static GTY(()) bitmap headers;
19524 : :
19525 : : /* Get the (possibly empty) macro imports for NODE. */
19526 : :
19527 : : static macro_import &
19528 : 139112 : get_macro_imports (cpp_hashnode *node)
19529 : : {
19530 : 139112 : if (node->deferred)
19531 : 4217 : return (*macro_imports)[node->deferred - 1];
19532 : :
19533 : 134895 : vec_safe_reserve (macro_imports, 1);
19534 : 134895 : node->deferred = macro_imports->length () + 1;
19535 : 134895 : return *vec_safe_push (macro_imports, macro_import ());
19536 : : }
19537 : :
19538 : : /* Get the macro export for export EXP of NODE. */
19539 : :
19540 : : static macro_export &
19541 : 96788 : get_macro_export (macro_import::slot &slot)
19542 : : {
19543 : 96788 : if (slot.offset >= 0)
19544 : 9 : return (*macro_exports)[slot.offset];
19545 : :
19546 : 96779 : vec_safe_reserve (macro_exports, 1);
19547 : 96779 : slot.offset = macro_exports->length ();
19548 : 96779 : return *macro_exports->quick_push (macro_export ());
19549 : : }
19550 : :
19551 : : /* If NODE is an exportable macro, add it to the export set. */
19552 : :
19553 : : static int
19554 : 3788597 : maybe_add_macro (cpp_reader *, cpp_hashnode *node, void *data_)
19555 : : {
19556 : 3788597 : bool exporting = false;
19557 : :
19558 : 3788597 : if (cpp_user_macro_p (node))
19559 : 486283 : if (cpp_macro *macro = node->value.macro)
19560 : : /* Ignore imported, builtins, command line and forced header macros. */
19561 : 485848 : if (!macro->imported_p
19562 : 485848 : && !macro->lazy && macro->line >= spans.main_start ())
19563 : : {
19564 : 67005 : gcc_checking_assert (macro->kind == cmk_macro);
19565 : : /* I don't want to deal with this corner case, that I suspect is
19566 : : a devil's advocate reading of the standard. */
19567 : 67005 : gcc_checking_assert (!macro->extra_tokens);
19568 : :
19569 : 67005 : macro_import::slot &slot = get_macro_imports (node).exported ();
19570 : 67005 : macro_export &exp = get_macro_export (slot);
19571 : 67005 : exp.def = macro;
19572 : 67005 : exporting = true;
19573 : : }
19574 : :
19575 : 3721592 : if (!exporting && node->deferred)
19576 : : {
19577 : 576 : macro_import &imports = (*macro_imports)[node->deferred - 1];
19578 : 576 : macro_import::slot &slot = imports[0];
19579 : 576 : if (!slot.get_module ())
19580 : : {
19581 : 545 : gcc_checking_assert (slot.get_defness ());
19582 : : exporting = true;
19583 : : }
19584 : : }
19585 : :
19586 : 67005 : if (exporting)
19587 : 67550 : static_cast<vec<cpp_hashnode *> *> (data_)->safe_push (node);
19588 : :
19589 : 3788597 : return 1; /* Don't stop. */
19590 : : }
19591 : :
19592 : : /* Order cpp_hashnodes A_ and B_ by their exported macro locations. */
19593 : :
19594 : : static int
19595 : 3508102 : macro_loc_cmp (const void *a_, const void *b_)
19596 : : {
19597 : 3508102 : const cpp_hashnode *node_a = *(const cpp_hashnode *const *)a_;
19598 : 3508102 : macro_import &import_a = (*macro_imports)[node_a->deferred - 1];
19599 : 3508102 : const macro_export &export_a = (*macro_exports)[import_a[0].offset];
19600 : 3508102 : location_t loc_a = export_a.def ? export_a.def->line : export_a.undef_loc;
19601 : :
19602 : 3508102 : const cpp_hashnode *node_b = *(const cpp_hashnode *const *)b_;
19603 : 3508102 : macro_import &import_b = (*macro_imports)[node_b->deferred - 1];
19604 : 3508102 : const macro_export &export_b = (*macro_exports)[import_b[0].offset];
19605 : 3508102 : location_t loc_b = export_b.def ? export_b.def->line : export_b.undef_loc;
19606 : :
19607 : 3508102 : if (loc_a < loc_b)
19608 : : return +1;
19609 : 1802918 : else if (loc_a > loc_b)
19610 : : return -1;
19611 : : else
19612 : 0 : return 0;
19613 : : }
19614 : :
19615 : : /* Gather the macro definitions and undefinitions that we will need to
19616 : : write out. */
19617 : :
19618 : : vec<cpp_hashnode *> *
19619 : 876 : module_state::prepare_macros (cpp_reader *reader)
19620 : : {
19621 : 876 : vec<cpp_hashnode *> *macros;
19622 : 876 : vec_alloc (macros, 100);
19623 : :
19624 : 876 : cpp_forall_identifiers (reader, maybe_add_macro, macros);
19625 : :
19626 : 900 : dump (dumper::MACRO) && dump ("No more than %u macros", macros->length ());
19627 : :
19628 : 876 : macros->qsort (macro_loc_cmp);
19629 : :
19630 : : // Note the locations.
19631 : 69302 : for (unsigned ix = macros->length (); ix--;)
19632 : : {
19633 : 67550 : cpp_hashnode *node = (*macros)[ix];
19634 : 67550 : macro_import::slot &slot = (*macro_imports)[node->deferred - 1][0];
19635 : 67550 : macro_export &mac = (*macro_exports)[slot.offset];
19636 : :
19637 : 67550 : if (IDENTIFIER_KEYWORD_P (identifier (node)))
19638 : 1 : continue;
19639 : :
19640 : 67549 : if (mac.undef_loc != UNKNOWN_LOCATION)
19641 : 12 : note_location (mac.undef_loc);
19642 : 67549 : if (mac.def)
19643 : : {
19644 : 67543 : note_location (mac.def->line);
19645 : 221410 : for (unsigned ix = 0; ix != mac.def->count; ix++)
19646 : 153867 : note_location (mac.def->exp.tokens[ix].src_loc);
19647 : : }
19648 : : }
19649 : :
19650 : 876 : return macros;
19651 : : }
19652 : :
19653 : : /* Write out the exported defines. This is two sections, one
19654 : : containing the definitions, the other a table of node names. */
19655 : :
19656 : : unsigned
19657 : 876 : module_state::write_macros (elf_out *to, vec<cpp_hashnode *> *macros,
19658 : : unsigned *crc_p)
19659 : : {
19660 : 943 : dump () && dump ("Writing macros");
19661 : 876 : dump.indent ();
19662 : :
19663 : : /* Write the defs */
19664 : 876 : bytes_out sec (to);
19665 : 876 : sec.begin ();
19666 : :
19667 : 876 : unsigned count = 0;
19668 : 69302 : for (unsigned ix = macros->length (); ix--;)
19669 : : {
19670 : 67550 : cpp_hashnode *node = (*macros)[ix];
19671 : 67550 : macro_import::slot &slot = (*macro_imports)[node->deferred - 1][0];
19672 : 67550 : gcc_assert (!slot.get_module () && slot.get_defness ());
19673 : :
19674 : 67550 : macro_export &mac = (*macro_exports)[slot.offset];
19675 : 67550 : gcc_assert (!!(slot.get_defness () & macro_import::slot::L_UNDEF)
19676 : : == (mac.undef_loc != UNKNOWN_LOCATION)
19677 : : && !!(slot.get_defness () & macro_import::slot::L_DEF)
19678 : : == (mac.def != NULL));
19679 : :
19680 : 67550 : if (IDENTIFIER_KEYWORD_P (identifier (node)))
19681 : : {
19682 : 1 : warning_at (mac.def->line, 0,
19683 : : "not exporting %<#define %E%> as it is a keyword",
19684 : : identifier (node));
19685 : 1 : slot.offset = 0;
19686 : 1 : continue;
19687 : : }
19688 : :
19689 : 67549 : count++;
19690 : 67549 : slot.offset = sec.pos;
19691 : 67549 : dump (dumper::MACRO)
19692 : 24 : && dump ("Writing macro %s%s%s %I at %u",
19693 : 24 : slot.get_defness () & macro_import::slot::L_UNDEF
19694 : : ? "#undef" : "",
19695 : 24 : slot.get_defness () == macro_import::slot::L_BOTH
19696 : : ? " & " : "",
19697 : 24 : slot.get_defness () & macro_import::slot::L_DEF
19698 : : ? "#define" : "",
19699 : : identifier (node), slot.offset);
19700 : 67549 : if (mac.undef_loc != UNKNOWN_LOCATION)
19701 : 12 : write_location (sec, mac.undef_loc);
19702 : 67549 : if (mac.def)
19703 : 67543 : write_define (sec, mac.def);
19704 : : }
19705 : 876 : if (count)
19706 : : // We may have ended on a tokenless macro with a very short
19707 : : // location, that will cause problems reading its bit flags.
19708 : 142 : sec.u (0);
19709 : 876 : sec.end (to, to->name (MOD_SNAME_PFX ".def"), crc_p);
19710 : :
19711 : 876 : if (count)
19712 : : {
19713 : : /* Write the table. */
19714 : 142 : bytes_out sec (to);
19715 : 142 : sec.begin ();
19716 : 142 : sec.u (count);
19717 : :
19718 : 67833 : for (unsigned ix = macros->length (); ix--;)
19719 : : {
19720 : 67549 : const cpp_hashnode *node = (*macros)[ix];
19721 : 67549 : macro_import::slot &slot = (*macro_imports)[node->deferred - 1][0];
19722 : :
19723 : 67549 : if (slot.offset)
19724 : : {
19725 : 67549 : sec.cpp_node (node);
19726 : 67549 : sec.u (slot.get_defness ());
19727 : 67549 : sec.u (slot.offset);
19728 : : }
19729 : : }
19730 : 142 : sec.end (to, to->name (MOD_SNAME_PFX ".mac"), crc_p);
19731 : 142 : }
19732 : :
19733 : 876 : dump.outdent ();
19734 : 876 : return count;
19735 : 876 : }
19736 : :
19737 : : bool
19738 : 907 : module_state::read_macros ()
19739 : : {
19740 : : /* Get the def section. */
19741 : 907 : if (!slurp->macro_defs.begin (loc, from (), MOD_SNAME_PFX ".def"))
19742 : : return false;
19743 : :
19744 : : /* Get the tbl section, if there are defs. */
19745 : 907 : if (slurp->macro_defs.more_p ()
19746 : 907 : && !slurp->macro_tbl.begin (loc, from (), MOD_SNAME_PFX ".mac"))
19747 : : return false;
19748 : :
19749 : : return true;
19750 : : }
19751 : :
19752 : : /* Install the macro name table. */
19753 : :
19754 : : void
19755 : 913 : module_state::install_macros ()
19756 : : {
19757 : 913 : bytes_in &sec = slurp->macro_tbl;
19758 : 913 : if (!sec.size)
19759 : : return;
19760 : :
19761 : 200 : dump () && dump ("Reading macro table %M", this);
19762 : 178 : dump.indent ();
19763 : :
19764 : 178 : unsigned count = sec.u ();
19765 : 200 : dump () && dump ("%u macros", count);
19766 : 72285 : while (count--)
19767 : : {
19768 : 72107 : cpp_hashnode *node = sec.cpp_node ();
19769 : 72107 : macro_import &imp = get_macro_imports (node);
19770 : 72107 : unsigned flags = sec.u () & macro_import::slot::L_BOTH;
19771 : 72107 : if (!flags)
19772 : 0 : sec.set_overrun ();
19773 : :
19774 : 72107 : if (sec.get_overrun ())
19775 : : break;
19776 : :
19777 : 72107 : macro_import::slot &slot = imp.append (mod, flags);
19778 : 72107 : slot.offset = sec.u ();
19779 : :
19780 : 72107 : dump (dumper::MACRO)
19781 : 84 : && dump ("Read %s macro %s%s%s %I at %u",
19782 : 30 : imp.length () > 1 ? "add" : "new",
19783 : 27 : flags & macro_import::slot::L_UNDEF ? "#undef" : "",
19784 : : flags == macro_import::slot::L_BOTH ? " & " : "",
19785 : 30 : flags & macro_import::slot::L_DEF ? "#define" : "",
19786 : : identifier (node), slot.offset);
19787 : :
19788 : : /* We'll leak an imported definition's TOKEN_FLD_STR's data
19789 : : here. But that only happens when we've had to resolve the
19790 : : deferred macro before this import -- why are you doing
19791 : : that? */
19792 : 72107 : if (cpp_macro *cur = cpp_set_deferred_macro (node))
19793 : 29771 : if (!cur->imported_p)
19794 : : {
19795 : 29771 : macro_import::slot &slot = imp.exported ();
19796 : 29771 : macro_export &exp = get_macro_export (slot);
19797 : 29771 : exp.def = cur;
19798 : 102056 : dump (dumper::MACRO)
19799 : 0 : && dump ("Saving current #define %I", identifier (node));
19800 : : }
19801 : : }
19802 : :
19803 : : /* We're now done with the table. */
19804 : 178 : elf_in::release (slurp->from, sec);
19805 : :
19806 : 178 : dump.outdent ();
19807 : : }
19808 : :
19809 : : /* Import the transitive macros. */
19810 : :
19811 : : void
19812 : 871 : module_state::import_macros ()
19813 : : {
19814 : 871 : bitmap_ior_into (headers, slurp->headers);
19815 : :
19816 : 871 : bitmap_iterator bititer;
19817 : 871 : unsigned bitnum;
19818 : 1784 : EXECUTE_IF_SET_IN_BITMAP (slurp->headers, 0, bitnum, bititer)
19819 : 913 : (*modules)[bitnum]->install_macros ();
19820 : 871 : }
19821 : :
19822 : : /* NODE is being undefined at LOC. Record it in the export table, if
19823 : : necessary. */
19824 : :
19825 : : void
19826 : 203793 : module_state::undef_macro (cpp_reader *, location_t loc, cpp_hashnode *node)
19827 : : {
19828 : 203793 : if (!node->deferred)
19829 : : /* The macro is not imported, so our undef is irrelevant. */
19830 : : return;
19831 : :
19832 : 12 : unsigned n = dump.push (NULL);
19833 : :
19834 : 12 : macro_import::slot &slot = (*macro_imports)[node->deferred - 1].exported ();
19835 : 12 : macro_export &exp = get_macro_export (slot);
19836 : :
19837 : 12 : exp.undef_loc = loc;
19838 : 12 : slot.become_undef ();
19839 : 12 : exp.def = NULL;
19840 : :
19841 : 18 : dump (dumper::MACRO) && dump ("Recording macro #undef %I", identifier (node));
19842 : :
19843 : 12 : dump.pop (n);
19844 : : }
19845 : :
19846 : : /* NODE is a deferred macro node. Determine the definition and return
19847 : : it, with NULL if undefined. May issue diagnostics.
19848 : :
19849 : : This can leak memory, when merging declarations -- the string
19850 : : contents (TOKEN_FLD_STR) of each definition are allocated in
19851 : : unreclaimable cpp objstack. Only one will win. However, I do not
19852 : : expect this to be common -- mostly macros have a single point of
19853 : : definition. Perhaps we could restore the objstack to its position
19854 : : after the first imported definition (if that wins)? The macros
19855 : : themselves are GC'd. */
19856 : :
19857 : : cpp_macro *
19858 : 297 : module_state::deferred_macro (cpp_reader *reader, location_t loc,
19859 : : cpp_hashnode *node)
19860 : : {
19861 : 297 : macro_import &imports = (*macro_imports)[node->deferred - 1];
19862 : :
19863 : 297 : unsigned n = dump.push (NULL);
19864 : 303 : dump (dumper::MACRO) && dump ("Deferred macro %I", identifier (node));
19865 : :
19866 : 297 : bitmap visible (BITMAP_GGC_ALLOC ());
19867 : :
19868 : 297 : if (!((imports[0].get_defness () & macro_import::slot::L_UNDEF)
19869 : 0 : && !imports[0].get_module ()))
19870 : : {
19871 : : /* Calculate the set of visible header imports. */
19872 : 297 : bitmap_copy (visible, headers);
19873 : 762 : for (unsigned ix = imports.length (); ix--;)
19874 : : {
19875 : 465 : const macro_import::slot &slot = imports[ix];
19876 : 465 : unsigned mod = slot.get_module ();
19877 : 465 : if ((slot.get_defness () & macro_import::slot::L_UNDEF)
19878 : 465 : && bitmap_bit_p (visible, mod))
19879 : : {
19880 : 12 : bitmap arg = mod ? (*modules)[mod]->slurp->headers : headers;
19881 : 12 : bitmap_and_compl_into (visible, arg);
19882 : 12 : bitmap_set_bit (visible, mod);
19883 : : }
19884 : : }
19885 : : }
19886 : 297 : bitmap_set_bit (visible, 0);
19887 : :
19888 : : /* Now find the macros that are still visible. */
19889 : 297 : bool failed = false;
19890 : 297 : cpp_macro *def = NULL;
19891 : 297 : vec<macro_export> defs;
19892 : 297 : defs.create (imports.length ());
19893 : 762 : for (unsigned ix = imports.length (); ix--;)
19894 : : {
19895 : 465 : const macro_import::slot &slot = imports[ix];
19896 : 465 : unsigned mod = slot.get_module ();
19897 : 465 : if (bitmap_bit_p (visible, mod))
19898 : : {
19899 : 453 : macro_export *pushed = NULL;
19900 : 453 : if (mod)
19901 : : {
19902 : 327 : const module_state *imp = (*modules)[mod];
19903 : 327 : bytes_in &sec = imp->slurp->macro_defs;
19904 : 327 : if (!sec.get_overrun ())
19905 : : {
19906 : 327 : dump (dumper::MACRO)
19907 : 6 : && dump ("Reading macro %s%s%s %I module %M at %u",
19908 : 6 : slot.get_defness () & macro_import::slot::L_UNDEF
19909 : : ? "#undef" : "",
19910 : 6 : slot.get_defness () == macro_import::slot::L_BOTH
19911 : : ? " & " : "",
19912 : 6 : slot.get_defness () & macro_import::slot::L_DEF
19913 : : ? "#define" : "",
19914 : 6 : identifier (node), imp, slot.offset);
19915 : 327 : sec.random_access (slot.offset);
19916 : :
19917 : 327 : macro_export exp;
19918 : 327 : if (slot.get_defness () & macro_import::slot::L_UNDEF)
19919 : 12 : exp.undef_loc = imp->read_location (sec);
19920 : 327 : if (slot.get_defness () & macro_import::slot::L_DEF)
19921 : 321 : exp.def = imp->read_define (sec, reader);
19922 : 327 : if (sec.get_overrun ())
19923 : 0 : error_at (loc, "macro definitions of %qE corrupted",
19924 : 0 : imp->name);
19925 : : else
19926 : 327 : pushed = defs.quick_push (exp);
19927 : : }
19928 : : }
19929 : : else
19930 : 126 : pushed = defs.quick_push ((*macro_exports)[slot.offset]);
19931 : 453 : if (pushed && pushed->def)
19932 : : {
19933 : 447 : if (!def)
19934 : : def = pushed->def;
19935 : 153 : else if (cpp_compare_macros (def, pushed->def))
19936 : 465 : failed = true;
19937 : : }
19938 : : }
19939 : : }
19940 : :
19941 : 297 : if (failed)
19942 : : {
19943 : : /* If LOC is the first loc, this is the end of file check, which
19944 : : is a warning. */
19945 : 15 : auto_diagnostic_group d;
19946 : 15 : if (loc == MAP_START_LOCATION (LINEMAPS_ORDINARY_MAP_AT (line_table, 0)))
19947 : 9 : warning_at (loc, OPT_Winvalid_imported_macros,
19948 : : "inconsistent imported macro definition %qE",
19949 : : identifier (node));
19950 : : else
19951 : 6 : error_at (loc, "inconsistent imported macro definition %qE",
19952 : : identifier (node));
19953 : 60 : for (unsigned ix = defs.length (); ix--;)
19954 : : {
19955 : 30 : macro_export &exp = defs[ix];
19956 : 30 : if (exp.undef_loc)
19957 : 0 : inform (exp.undef_loc, "%<#undef %E%>", identifier (node));
19958 : 30 : if (exp.def)
19959 : 30 : inform (exp.def->line, "%<#define %s%>",
19960 : : cpp_macro_definition (reader, node, exp.def));
19961 : : }
19962 : 15 : def = NULL;
19963 : 15 : }
19964 : :
19965 : 297 : defs.release ();
19966 : :
19967 : 297 : dump.pop (n);
19968 : :
19969 : 297 : return def;
19970 : : }
19971 : :
19972 : : /* Stream the static aggregates. Sadly some headers (ahem:
19973 : : iostream) contain static vars, and rely on them to run global
19974 : : ctors. */
19975 : : unsigned
19976 : 876 : module_state::write_inits (elf_out *to, depset::hash &table, unsigned *crc_ptr)
19977 : : {
19978 : 876 : if (!static_aggregates && !tls_aggregates)
19979 : : return 0;
19980 : :
19981 : 45 : dump () && dump ("Writing initializers");
19982 : 45 : dump.indent ();
19983 : :
19984 : 45 : static_aggregates = nreverse (static_aggregates);
19985 : 45 : tls_aggregates = nreverse (tls_aggregates);
19986 : :
19987 : 45 : unsigned count = 0;
19988 : 45 : trees_out sec (to, this, table, ~0u);
19989 : 45 : sec.begin ();
19990 : :
19991 : 45 : tree list = static_aggregates;
19992 : 135 : for (int passes = 0; passes != 2; passes++)
19993 : : {
19994 : 258 : for (tree init = list; init; init = TREE_CHAIN (init))
19995 : 168 : if (TREE_LANG_FLAG_0 (init))
19996 : : {
19997 : 144 : if (STATIC_INIT_DECOMP_BASE_P (init))
19998 : : {
19999 : : /* Ensure that in the returned result chain if the
20000 : : STATIC_INIT_DECOMP_*BASE_P flags are set, there is
20001 : : always one or more STATIC_INIT_DECOMP_BASE_P TREE_LIST
20002 : : followed by one or more STATIC_INIT_DECOMP_NONBASE_P. */
20003 : 21 : int phase = 0;
20004 : 21 : tree last = NULL_TREE;
20005 : 21 : for (tree init2 = TREE_CHAIN (init);
20006 : 126 : init2; init2 = TREE_CHAIN (init2))
20007 : : {
20008 : 147 : if (phase == 0 && STATIC_INIT_DECOMP_BASE_P (init2))
20009 : : ;
20010 : 126 : else if (phase == 0
20011 : 147 : && STATIC_INIT_DECOMP_NONBASE_P (init2))
20012 : : {
20013 : 120 : phase = TREE_LANG_FLAG_0 (init2) ? 2 : 1;
20014 : : last = init2;
20015 : : }
20016 : 105 : else if (IN_RANGE (phase, 1, 2)
20017 : 210 : && STATIC_INIT_DECOMP_NONBASE_P (init2))
20018 : : {
20019 : 84 : if (TREE_LANG_FLAG_0 (init2))
20020 : 81 : phase = 2;
20021 : : last = init2;
20022 : : }
20023 : : else
20024 : : break;
20025 : : }
20026 : 21 : if (phase == 2)
20027 : : {
20028 : : /* In that case, add markers about it so that the
20029 : : STATIC_INIT_DECOMP_BASE_P and
20030 : : STATIC_INIT_DECOMP_NONBASE_P flags can be restored. */
20031 : 21 : sec.tree_node (build_int_cst (integer_type_node,
20032 : 21 : 2 * passes + 1));
20033 : 21 : phase = 1;
20034 : 147 : for (tree init2 = init; init2 != TREE_CHAIN (last);
20035 : 126 : init2 = TREE_CHAIN (init2))
20036 : 126 : if (TREE_LANG_FLAG_0 (init2))
20037 : : {
20038 : 102 : tree decl = TREE_VALUE (init2);
20039 : 102 : if (phase == 1
20040 : 102 : && STATIC_INIT_DECOMP_NONBASE_P (init2))
20041 : : {
20042 : 21 : sec.tree_node (build_int_cst (integer_type_node,
20043 : 21 : 2 * passes + 2));
20044 : 21 : phase = 2;
20045 : : }
20046 : 102 : dump ("Initializer:%u for %N", count, decl);
20047 : 102 : sec.tree_node (decl);
20048 : 102 : ++count;
20049 : : }
20050 : 21 : sec.tree_node (integer_zero_node);
20051 : 21 : init = last;
20052 : 21 : continue;
20053 : 21 : }
20054 : : }
20055 : :
20056 : 123 : tree decl = TREE_VALUE (init);
20057 : :
20058 : 123 : dump ("Initializer:%u for %N", count, decl);
20059 : 123 : sec.tree_node (decl);
20060 : 123 : ++count;
20061 : : }
20062 : :
20063 : 90 : list = tls_aggregates;
20064 : : }
20065 : :
20066 : 45 : sec.end (to, to->name (MOD_SNAME_PFX ".ini"), crc_ptr);
20067 : 45 : dump.outdent ();
20068 : :
20069 : 45 : return count;
20070 : 45 : }
20071 : :
20072 : : /* We have to defer some post-load processing until we've completed
20073 : : reading, because they can cause more reading. */
20074 : :
20075 : : static void
20076 : 8174 : post_load_processing ()
20077 : : {
20078 : : /* We mustn't cause a GC, our caller should have arranged for that
20079 : : not to happen. */
20080 : 8174 : gcc_checking_assert (function_depth);
20081 : :
20082 : 8174 : if (!post_load_decls)
20083 : : return;
20084 : :
20085 : 3879 : tree old_cfd = current_function_decl;
20086 : 3879 : struct function *old_cfun = cfun;
20087 : 9269 : while (post_load_decls->length ())
20088 : : {
20089 : 5390 : tree decl = post_load_decls->pop ();
20090 : :
20091 : 5445 : dump () && dump ("Post-load processing of %N", decl);
20092 : :
20093 : 5390 : gcc_checking_assert (DECL_MAYBE_IN_CHARGE_CDTOR_P (decl));
20094 : 5390 : expand_or_defer_fn (decl);
20095 : : /* As in module_state::read_cluster. */
20096 : 511 : if (at_eof && DECL_COMDAT (decl) && DECL_EXTERNAL (decl)
20097 : 5420 : && DECL_NOT_REALLY_EXTERN (decl))
20098 : 8 : DECL_EXTERNAL (decl) = false;
20099 : : }
20100 : :
20101 : 3879 : set_cfun (old_cfun);
20102 : 3879 : current_function_decl = old_cfd;
20103 : : }
20104 : :
20105 : : bool
20106 : 45 : module_state::read_inits (unsigned count)
20107 : : {
20108 : 45 : trees_in sec (this);
20109 : 45 : if (!sec.begin (loc, from (), from ()->find (MOD_SNAME_PFX ".ini")))
20110 : : return false;
20111 : 57 : dump () && dump ("Reading %u initializers", count);
20112 : 45 : dump.indent ();
20113 : :
20114 : 45 : lazy_snum = ~0u;
20115 : 45 : int decomp_phase = 0;
20116 : 45 : tree *aggrp = NULL;
20117 : 270 : for (unsigned ix = 0; ix != count; ix++)
20118 : : {
20119 : 225 : tree last = NULL_TREE;
20120 : 225 : if (decomp_phase)
20121 : 102 : last = *aggrp;
20122 : : /* Merely referencing the decl causes its initializer to be read
20123 : : and added to the correct list. */
20124 : 225 : tree decl = sec.tree_node ();
20125 : : /* module_state::write_inits can add special INTEGER_CST markers in
20126 : : between the decls. 1 means STATIC_INIT_DECOMP_BASE_P entries
20127 : : follow in static_aggregates, 2 means STATIC_INIT_DECOMP_NONBASE_P
20128 : : entries follow in static_aggregates, 3 means
20129 : : STATIC_INIT_DECOMP_BASE_P entries follow in tls_aggregates,
20130 : : 4 means STATIC_INIT_DECOMP_NONBASE_P follow in tls_aggregates,
20131 : : 0 means end of STATIC_INIT_DECOMP_{,NON}BASE_P sequence. */
20132 : 225 : if (tree_fits_shwi_p (decl))
20133 : : {
20134 : 63 : if (sec.get_overrun ())
20135 : : break;
20136 : 63 : decomp_phase = tree_to_shwi (decl);
20137 : 63 : if (decomp_phase)
20138 : : {
20139 : 42 : aggrp = decomp_phase > 2 ? &tls_aggregates : &static_aggregates;
20140 : : last = *aggrp;
20141 : : }
20142 : 63 : decl = sec.tree_node ();
20143 : : }
20144 : :
20145 : 225 : if (sec.get_overrun ())
20146 : : break;
20147 : 225 : if (decl)
20148 : 225 : dump ("Initializer:%u for %N", ix, decl);
20149 : 225 : if (decomp_phase)
20150 : : {
20151 : 102 : tree init = *aggrp;
20152 : 102 : gcc_assert (TREE_VALUE (init) == decl && TREE_CHAIN (init) == last);
20153 : 102 : if ((decomp_phase & 1) != 0)
20154 : 21 : STATIC_INIT_DECOMP_BASE_P (init) = 1;
20155 : : else
20156 : 81 : STATIC_INIT_DECOMP_NONBASE_P (init) = 1;
20157 : : }
20158 : : }
20159 : 45 : if (decomp_phase && !sec.get_overrun ())
20160 : : {
20161 : 0 : tree decl = sec.tree_node ();
20162 : 0 : gcc_assert (integer_zerop (decl));
20163 : : }
20164 : 45 : lazy_snum = 0;
20165 : 45 : post_load_processing ();
20166 : 45 : dump.outdent ();
20167 : 45 : if (!sec.end (from ()))
20168 : : return false;
20169 : : return true;
20170 : 45 : }
20171 : :
20172 : : void
20173 : 2544 : module_state::write_counts (elf_out *to, unsigned counts[MSC_HWM],
20174 : : unsigned *crc_ptr)
20175 : : {
20176 : 2544 : bytes_out cfg (to);
20177 : :
20178 : 2544 : cfg.begin ();
20179 : :
20180 : 25440 : for (unsigned ix = MSC_HWM; ix--;)
20181 : 22896 : cfg.u (counts[ix]);
20182 : :
20183 : 2544 : if (dump ())
20184 : : {
20185 : 273 : dump ("Cluster sections are [%u,%u)",
20186 : : counts[MSC_sec_lwm], counts[MSC_sec_hwm]);
20187 : 273 : dump ("Bindings %u", counts[MSC_bindings]);
20188 : 273 : dump ("Pendings %u", counts[MSC_pendings]);
20189 : 273 : dump ("Entities %u", counts[MSC_entities]);
20190 : 273 : dump ("Namespaces %u", counts[MSC_namespaces]);
20191 : 273 : dump ("Using-directives %u", counts[MSC_using_directives]);
20192 : 273 : dump ("Macros %u", counts[MSC_macros]);
20193 : 273 : dump ("Initializers %u", counts[MSC_inits]);
20194 : : }
20195 : :
20196 : 2544 : cfg.end (to, to->name (MOD_SNAME_PFX ".cnt"), crc_ptr);
20197 : 2544 : }
20198 : :
20199 : : bool
20200 : 2722 : module_state::read_counts (unsigned counts[MSC_HWM])
20201 : : {
20202 : 2722 : bytes_in cfg;
20203 : :
20204 : 2722 : if (!cfg.begin (loc, from (), MOD_SNAME_PFX ".cnt"))
20205 : : return false;
20206 : :
20207 : 27220 : for (unsigned ix = MSC_HWM; ix--;)
20208 : 24498 : counts[ix] = cfg.u ();
20209 : :
20210 : 2722 : if (dump ())
20211 : : {
20212 : 490 : dump ("Declaration sections are [%u,%u)",
20213 : : counts[MSC_sec_lwm], counts[MSC_sec_hwm]);
20214 : 490 : dump ("Bindings %u", counts[MSC_bindings]);
20215 : 490 : dump ("Pendings %u", counts[MSC_pendings]);
20216 : 490 : dump ("Entities %u", counts[MSC_entities]);
20217 : 490 : dump ("Namespaces %u", counts[MSC_namespaces]);
20218 : 490 : dump ("Using-directives %u", counts[MSC_using_directives]);
20219 : 490 : dump ("Macros %u", counts[MSC_macros]);
20220 : 490 : dump ("Initializers %u", counts[MSC_inits]);
20221 : : }
20222 : :
20223 : 2722 : return cfg.end (from ());
20224 : 2722 : }
20225 : :
20226 : : /* Tool configuration: MOD_SNAME_PFX .config
20227 : :
20228 : : This is data that confirms current state (or fails). */
20229 : :
20230 : : void
20231 : 2544 : module_state::write_config (elf_out *to, module_state_config &config,
20232 : : unsigned inner_crc)
20233 : : {
20234 : 2544 : bytes_out cfg (to);
20235 : :
20236 : 2544 : cfg.begin ();
20237 : :
20238 : : /* Write version and inner crc as u32 values, for easier
20239 : : debug inspection. */
20240 : 2817 : dump () && dump ("Writing version=%V, inner_crc=%x",
20241 : : MODULE_VERSION, inner_crc);
20242 : 2544 : cfg.u32 (unsigned (MODULE_VERSION));
20243 : 2544 : cfg.u32 (inner_crc);
20244 : :
20245 : 2544 : cfg.u (to->name (is_header () ? "" : get_flatname ()));
20246 : :
20247 : : /* Configuration. */
20248 : 2817 : dump () && dump ("Writing target='%s', host='%s'",
20249 : : TARGET_MACHINE, HOST_MACHINE);
20250 : 2544 : unsigned target = to->name (TARGET_MACHINE);
20251 : 2544 : unsigned host = (!strcmp (TARGET_MACHINE, HOST_MACHINE)
20252 : : ? target : to->name (HOST_MACHINE));
20253 : 2544 : cfg.u (target);
20254 : 2544 : cfg.u (host);
20255 : :
20256 : 2544 : cfg.str (config.dialect_str);
20257 : 2544 : cfg.u (extensions);
20258 : :
20259 : : /* Global tree information. We write the globals crc separately,
20260 : : rather than mix it directly into the overall crc, as it is used
20261 : : to ensure data match between instances of the compiler, not
20262 : : integrity of the file. */
20263 : 2817 : dump () && dump ("Writing globals=%u, crc=%x",
20264 : : fixed_trees->length (), global_crc);
20265 : 2544 : cfg.u (fixed_trees->length ());
20266 : 2544 : cfg.u32 (global_crc);
20267 : :
20268 : 2544 : if (is_partition ())
20269 : 172 : cfg.u (is_interface ());
20270 : :
20271 : 2544 : cfg.u (config.num_imports);
20272 : 2544 : cfg.u (config.num_partitions);
20273 : 2544 : cfg.u (config.num_entities);
20274 : :
20275 : 2544 : cfg.loc (config.ordinary_locs);
20276 : 2544 : cfg.loc (config.macro_locs);
20277 : 2544 : cfg.u (config.loc_range_bits);
20278 : :
20279 : 2544 : cfg.u (config.active_init);
20280 : :
20281 : : /* Now generate CRC, we'll have incorporated the inner CRC because
20282 : : of its serialization above. */
20283 : 2544 : cfg.end (to, to->name (MOD_SNAME_PFX ".cfg"), &crc);
20284 : 2817 : dump () && dump ("Writing CRC=%x", crc);
20285 : 2544 : }
20286 : :
20287 : : void
20288 : 40 : module_state::note_cmi_name ()
20289 : : {
20290 : 40 : if (!cmi_noted_p && filename)
20291 : : {
20292 : 40 : cmi_noted_p = true;
20293 : 40 : inform (loc, "compiled module file is %qs",
20294 : : maybe_add_cmi_prefix (filename));
20295 : : }
20296 : 40 : }
20297 : :
20298 : : bool
20299 : 2830 : module_state::read_config (module_state_config &config, bool complain)
20300 : : {
20301 : 2830 : bytes_in cfg;
20302 : :
20303 : 2830 : if (!cfg.begin (loc, from (), MOD_SNAME_PFX ".cfg"))
20304 : : return false;
20305 : :
20306 : : /* Check version. */
20307 : 2830 : unsigned my_ver = MODULE_VERSION;
20308 : 2830 : unsigned their_ver = cfg.u32 ();
20309 : 3323 : dump () && dump (my_ver == their_ver ? "Version %V"
20310 : : : "Expecting %V found %V", my_ver, their_ver);
20311 : 2830 : if (their_ver != my_ver)
20312 : : {
20313 : : /* The compiler versions differ. Close enough? */
20314 : 0 : verstr_t my_string, their_string;
20315 : :
20316 : 0 : version2string (my_ver, my_string);
20317 : 0 : version2string (their_ver, their_string);
20318 : :
20319 : : /* Reject when either is non-experimental or when experimental
20320 : : major versions differ. */
20321 : 0 : auto_diagnostic_group d;
20322 : 0 : bool reject_p = ((!IS_EXPERIMENTAL (my_ver)
20323 : : || !IS_EXPERIMENTAL (their_ver)
20324 : 0 : || MODULE_MAJOR (my_ver) != MODULE_MAJOR (their_ver))
20325 : : /* The 'I know what I'm doing' switch. */
20326 : 0 : && !flag_module_version_ignore);
20327 : 0 : bool inform_p = true;
20328 : 0 : if (!complain)
20329 : : inform_p = false;
20330 : 0 : else if (reject_p)
20331 : : {
20332 : 0 : cfg.set_overrun ();
20333 : 0 : error_at (loc, "compiled module is %sversion %s",
20334 : : IS_EXPERIMENTAL (their_ver) ? "experimental " : "",
20335 : : their_string);
20336 : : }
20337 : : else
20338 : 0 : inform_p = warning_at (loc, 0, "compiled module is %sversion %s",
20339 : : IS_EXPERIMENTAL (their_ver) ? "experimental " : "",
20340 : : their_string);
20341 : :
20342 : 0 : if (inform_p)
20343 : : {
20344 : 0 : inform (loc, "compiler is %sversion %s%s%s",
20345 : : IS_EXPERIMENTAL (my_ver) ? "experimental " : "",
20346 : : my_string,
20347 : 0 : reject_p ? "" : flag_module_version_ignore
20348 : 0 : ? ", be it on your own head!" : ", close enough?",
20349 : : reject_p ? "" : " \xc2\xaf\\_(\xe3\x83\x84)_/\xc2\xaf");
20350 : 0 : note_cmi_name ();
20351 : : }
20352 : :
20353 : 0 : if (reject_p)
20354 : 0 : goto done;
20355 : 0 : }
20356 : :
20357 : : /* We wrote the inner crc merely to merge it, so simply read it
20358 : : back and forget it. */
20359 : 2830 : cfg.u32 ();
20360 : :
20361 : : /* Check module name. */
20362 : 2830 : {
20363 : 2830 : const char *their_name = from ()->name (cfg.u ());
20364 : 2830 : const char *our_name = "";
20365 : :
20366 : 2830 : if (!is_header ())
20367 : 1825 : our_name = get_flatname ();
20368 : :
20369 : : /* Header units can be aliased, so name checking is
20370 : : inappropriate. */
20371 : 2830 : if (0 != strcmp (their_name, our_name))
20372 : : {
20373 : 0 : error_at (loc,
20374 : 0 : their_name[0] && our_name[0] ? G_("module %qs found")
20375 : : : their_name[0]
20376 : : ? G_("header module expected, module %qs found")
20377 : : : G_("module %qs expected, header module found"),
20378 : 0 : their_name[0] ? their_name : our_name);
20379 : 0 : cfg.set_overrun ();
20380 : 0 : goto done;
20381 : : }
20382 : : }
20383 : :
20384 : : /* Check the CRC after the above sanity checks, so that the user is
20385 : : clued in. */
20386 : 2830 : {
20387 : 2830 : unsigned e_crc = crc;
20388 : 2830 : crc = cfg.get_crc ();
20389 : 3323 : dump () && dump ("Reading CRC=%x", crc);
20390 : : /* When not complaining we haven't set directness yet, so ignore the
20391 : : mismatch. */
20392 : 2830 : if (complain && !is_direct () && crc != e_crc)
20393 : : {
20394 : 3 : error_at (loc, "module %qs CRC mismatch", get_flatname ());
20395 : 3 : cfg.set_overrun ();
20396 : 3 : goto done;
20397 : : }
20398 : : }
20399 : :
20400 : : /* Check target & host. */
20401 : 2827 : {
20402 : 2827 : const char *their_target = from ()->name (cfg.u ());
20403 : 2827 : const char *their_host = from ()->name (cfg.u ());
20404 : 3320 : dump () && dump ("Read target='%s', host='%s'", their_target, their_host);
20405 : 2827 : if (strcmp (their_target, TARGET_MACHINE)
20406 : 2827 : || strcmp (their_host, HOST_MACHINE))
20407 : : {
20408 : 0 : error_at (loc, "target & host is %qs:%qs, expected %qs:%qs",
20409 : : their_target, TARGET_MACHINE, their_host, HOST_MACHINE);
20410 : 0 : cfg.set_overrun ();
20411 : 0 : goto done;
20412 : : }
20413 : : }
20414 : :
20415 : : /* Check compilation dialect. This must match. */
20416 : 2827 : {
20417 : 2827 : const char *their_dialect = cfg.str ();
20418 : 2827 : if (strcmp (their_dialect, config.dialect_str))
20419 : : {
20420 : 1 : if (complain)
20421 : 1 : error_at (loc, "language dialect differs %qs, expected %qs",
20422 : : their_dialect, config.dialect_str);
20423 : 1 : cfg.set_overrun ();
20424 : 1 : goto done;
20425 : : }
20426 : : }
20427 : :
20428 : : /* Check for extensions. If they set any, we must have them set
20429 : : too. */
20430 : 2826 : {
20431 : 2826 : unsigned ext = cfg.u ();
20432 : 2826 : unsigned allowed = (flag_openmp ? SE_OPENMP | SE_OPENMP_SIMD : 0);
20433 : 2826 : if (flag_openmp_simd)
20434 : 3 : allowed |= SE_OPENMP_SIMD;
20435 : 2826 : if (flag_openacc)
20436 : 3 : allowed |= SE_OPENACC;
20437 : :
20438 : 2826 : if (unsigned bad = ext & ~allowed)
20439 : : {
20440 : 9 : if (bad & SE_OPENMP)
20441 : 3 : error_at (loc, "module contains OpenMP, use %<-fopenmp%> to enable");
20442 : 6 : else if (bad & SE_OPENMP_SIMD)
20443 : 3 : error_at (loc, "module contains OpenMP, use %<-fopenmp%> or "
20444 : : "%<-fopenmp-simd%> to enable");
20445 : 9 : if (bad & SE_OPENACC)
20446 : 3 : error_at (loc, "module contains OpenACC, use %<-fopenacc%> to "
20447 : : "enable");
20448 : 9 : cfg.set_overrun ();
20449 : 9 : goto done;
20450 : : }
20451 : 2817 : extensions = ext;
20452 : : }
20453 : :
20454 : : /* Check global trees. */
20455 : 2817 : {
20456 : 2817 : unsigned their_fixed_length = cfg.u ();
20457 : 2817 : unsigned their_fixed_crc = cfg.u32 ();
20458 : 3310 : dump () && dump ("Read globals=%u, crc=%x",
20459 : : their_fixed_length, their_fixed_crc);
20460 : 2817 : if (!flag_preprocess_only
20461 : 2817 : && (their_fixed_length != fixed_trees->length ()
20462 : 2763 : || their_fixed_crc != global_crc))
20463 : : {
20464 : 0 : error_at (loc, "fixed tree mismatch");
20465 : 0 : cfg.set_overrun ();
20466 : 0 : goto done;
20467 : : }
20468 : : }
20469 : :
20470 : : /* All non-partitions are interfaces. */
20471 : 2817 : interface_p = !is_partition () || cfg.u ();
20472 : :
20473 : 2817 : config.num_imports = cfg.u ();
20474 : 2817 : config.num_partitions = cfg.u ();
20475 : 2817 : config.num_entities = cfg.u ();
20476 : :
20477 : 2817 : config.ordinary_locs = cfg.loc ();
20478 : 2817 : config.macro_locs = cfg.loc ();
20479 : 2817 : config.loc_range_bits = cfg.u ();
20480 : :
20481 : 2817 : config.active_init = cfg.u ();
20482 : :
20483 : 2830 : done:
20484 : 2830 : return cfg.end (from ());
20485 : 2830 : }
20486 : :
20487 : : /* Comparator for ordering the Ordered Ordinary Location array. */
20488 : :
20489 : : static int
20490 : 108 : ool_cmp (const void *a_, const void *b_)
20491 : : {
20492 : 108 : auto *a = *static_cast<const module_state *const *> (a_);
20493 : 108 : auto *b = *static_cast<const module_state *const *> (b_);
20494 : 108 : if (a == b)
20495 : : return 0;
20496 : 108 : else if (a->ordinary_locs.first < b->ordinary_locs.first)
20497 : : return -1;
20498 : : else
20499 : 44 : return +1;
20500 : : }
20501 : :
20502 : : /* Use ELROND format to record the following sections:
20503 : : qualified-names : binding value(s)
20504 : : MOD_SNAME_PFX.README : human readable, strings
20505 : : MOD_SNAME_PFX.ENV : environment strings, strings
20506 : : MOD_SNAME_PFX.nms : namespace hierarchy
20507 : : MOD_SNAME_PFX.udi : namespace using-directives
20508 : : MOD_SNAME_PFX.bnd : binding table
20509 : : MOD_SNAME_PFX.spc : specialization table
20510 : : MOD_SNAME_PFX.imp : import table
20511 : : MOD_SNAME_PFX.ent : entity table
20512 : : MOD_SNAME_PFX.prt : partitions table
20513 : : MOD_SNAME_PFX.olm : ordinary line maps
20514 : : MOD_SNAME_PFX.mlm : macro line maps
20515 : : MOD_SNAME_PFX.def : macro definitions
20516 : : MOD_SNAME_PFX.mac : macro index
20517 : : MOD_SNAME_PFX.ini : inits
20518 : : MOD_SNAME_PFX.cnt : counts
20519 : : MOD_SNAME_PFX.cfg : config data
20520 : : */
20521 : :
20522 : : bool
20523 : 2573 : module_state::write_begin (elf_out *to, cpp_reader *reader,
20524 : : module_state_config &config, unsigned &crc)
20525 : : {
20526 : : /* Figure out remapped module numbers, which might elide
20527 : : partitions. */
20528 : 2573 : bitmap partitions = NULL;
20529 : 2573 : if (!is_header () && !is_partition ())
20530 : 1525 : partitions = BITMAP_GGC_ALLOC ();
20531 : 2573 : write_init_maps ();
20532 : :
20533 : 2573 : unsigned mod_hwm = 1;
20534 : 3175 : for (unsigned ix = 1; ix != modules->length (); ix++)
20535 : : {
20536 : 602 : module_state *imp = (*modules)[ix];
20537 : :
20538 : : /* Promote any non-partition direct import from a partition, unless
20539 : : we're a partition. */
20540 : 548 : if (!is_partition () && !imp->is_partition ()
20541 : 987 : && imp->is_partition_direct ())
20542 : 9 : imp->directness = MD_PURVIEW_DIRECT;
20543 : :
20544 : : /* Write any import that is not a partition, unless we're a
20545 : : partition. */
20546 : 602 : if (!partitions || !imp->is_partition ())
20547 : 439 : imp->remap = mod_hwm++;
20548 : : else
20549 : : {
20550 : 193 : dump () && dump ("Partition %M %u", imp, ix);
20551 : 163 : bitmap_set_bit (partitions, ix);
20552 : 163 : imp->remap = 0;
20553 : : /* All interface partitions must be exported. */
20554 : 163 : if (imp->is_interface () && !bitmap_bit_p (exports, imp->mod))
20555 : : {
20556 : 3 : error_at (imp->loc, "interface partition is not exported");
20557 : 3 : bitmap_set_bit (exports, imp->mod);
20558 : : }
20559 : :
20560 : : /* All the partition entities should have been loaded when
20561 : : loading the partition. */
20562 : : if (CHECKING_P)
20563 : 894 : for (unsigned jx = 0; jx != imp->entity_num; jx++)
20564 : : {
20565 : 731 : binding_slot *slot = &(*entity_ary)[imp->entity_lwm + jx];
20566 : 731 : gcc_checking_assert (!slot->is_lazy ());
20567 : : }
20568 : : }
20569 : :
20570 : 602 : if (imp->is_direct () && (imp->remap || imp->is_partition ()))
20571 : 585 : note_location (imp->imported_from ());
20572 : : }
20573 : :
20574 : 2573 : if (partitions && bitmap_empty_p (partitions))
20575 : : /* No partitions present. */
20576 : : partitions = nullptr;
20577 : :
20578 : : /* Find the set of decls we must write out. */
20579 : 2573 : depset::hash table (DECL_NAMESPACE_BINDINGS (global_namespace)->size () * 8);
20580 : : /* Add the specializations before the writables, so that we can
20581 : : detect injected friend specializations. */
20582 : 2573 : table.add_specializations (true);
20583 : 2573 : table.add_specializations (false);
20584 : 2573 : if (partial_specializations)
20585 : : {
20586 : 200 : table.add_partial_entities (partial_specializations);
20587 : 200 : partial_specializations = NULL;
20588 : : }
20589 : 2573 : table.add_namespace_entities (global_namespace, partitions);
20590 : 2573 : if (class_members)
20591 : : {
20592 : 12 : table.add_class_entities (class_members);
20593 : 12 : class_members = NULL;
20594 : : }
20595 : :
20596 : : /* Now join everything up. */
20597 : 2573 : table.find_dependencies (this);
20598 : :
20599 : 2573 : if (!table.finalize_dependencies ())
20600 : : return false;
20601 : :
20602 : : #if CHECKING_P
20603 : : /* We're done verifying at-most once reading, reset to verify
20604 : : at-most once writing. */
20605 : 2544 : note_defs = note_defs_table_t::create_ggc (1000);
20606 : : #endif
20607 : :
20608 : : /* Determine Strongly Connected Components. This will also strip any
20609 : : unnecessary dependencies on imported or TU-local entities. */
20610 : 2544 : vec<depset *> sccs = table.connect ();
20611 : :
20612 : 2544 : vec_alloc (ool, modules->length ());
20613 : 3139 : for (unsigned ix = modules->length (); --ix;)
20614 : : {
20615 : 595 : auto *import = (*modules)[ix];
20616 : 595 : if (import->loadedness > ML_NONE
20617 : 595 : && !(partitions && bitmap_bit_p (partitions, import->mod)))
20618 : 432 : ool->quick_push (import);
20619 : : }
20620 : 2544 : ool->qsort (ool_cmp);
20621 : :
20622 : 2544 : write_diagnostic_classification (nullptr, global_dc, nullptr);
20623 : :
20624 : 2544 : vec<cpp_hashnode *> *macros = nullptr;
20625 : 2544 : if (is_header ())
20626 : 876 : macros = prepare_macros (reader);
20627 : :
20628 : 2544 : config.num_imports = mod_hwm;
20629 : 2544 : config.num_partitions = modules->length () - mod_hwm;
20630 : 2544 : auto map_info = write_prepare_maps (&config, bool (config.num_partitions));
20631 : 2544 : unsigned counts[MSC_HWM];
20632 : 2544 : memset (counts, 0, sizeof (counts));
20633 : :
20634 : : /* depset::cluster is the cluster number,
20635 : : depset::section is unspecified scratch value.
20636 : :
20637 : : The following loops make use of the tarjan property that
20638 : : dependencies will be earlier in the SCCS array. */
20639 : :
20640 : : /* This first loop determines the number of depsets in each SCC, and
20641 : : also the number of namespaces we're dealing with. During the
20642 : : loop, the meaning of a couple of depset fields now change:
20643 : :
20644 : : depset::cluster -> size_of cluster, if first of cluster & !namespace
20645 : : depset::section -> section number of cluster (if !namespace). */
20646 : :
20647 : 2544 : unsigned n_spaces = 0;
20648 : 2544 : counts[MSC_sec_lwm] = counts[MSC_sec_hwm] = to->get_section_limit ();
20649 : 229924 : for (unsigned size, ix = 0; ix < sccs.length (); ix += size)
20650 : : {
20651 : 227380 : depset **base = &sccs[ix];
20652 : :
20653 : 429189 : if (base[0]->get_entity_kind () == depset::EK_NAMESPACE)
20654 : : {
20655 : 4375 : n_spaces++;
20656 : 4375 : size = 1;
20657 : : }
20658 : : else
20659 : : {
20660 : : /* Count the members in this cluster. */
20661 : 924017 : for (size = 1; ix + size < sccs.length (); size++)
20662 : 921774 : if (base[size]->cluster != base[0]->cluster)
20663 : : break;
20664 : :
20665 : 1147022 : for (unsigned jx = 0; jx != size; jx++)
20666 : : {
20667 : : /* Set the section number. */
20668 : 924017 : base[jx]->cluster = ~(~0u >> 1); /* A bad value. */
20669 : 924017 : base[jx]->section = counts[MSC_sec_hwm];
20670 : : }
20671 : :
20672 : : /* Save the size in the first member's cluster slot. */
20673 : 223005 : base[0]->cluster = size;
20674 : :
20675 : 223005 : counts[MSC_sec_hwm]++;
20676 : : }
20677 : : }
20678 : :
20679 : : /* Write the clusters. Namespace decls are put in the spaces array.
20680 : : The meaning of depset::cluster changes to provide the
20681 : : unnamed-decl count of the depset's decl (and remains zero for
20682 : : non-decls and non-unnamed). */
20683 : 2544 : unsigned bytes = 0;
20684 : 2544 : vec<depset *> spaces;
20685 : 2544 : spaces.create (n_spaces);
20686 : :
20687 : 229924 : for (unsigned size, ix = 0; ix < sccs.length (); ix += size)
20688 : : {
20689 : 227380 : depset **base = &sccs[ix];
20690 : :
20691 : 227380 : if (base[0]->get_entity_kind () == depset::EK_NAMESPACE)
20692 : : {
20693 : 4375 : tree decl = base[0]->get_entity ();
20694 : 4375 : if (decl == global_namespace)
20695 : 2305 : base[0]->cluster = 0;
20696 : 2070 : else if (!base[0]->is_import ())
20697 : : {
20698 : 2070 : base[0]->cluster = counts[MSC_entities]++;
20699 : 2070 : spaces.quick_push (base[0]);
20700 : 2070 : counts[MSC_namespaces]++;
20701 : 2070 : if (CHECKING_P)
20702 : : {
20703 : : /* Add it to the entity map, such that we can tell it is
20704 : : part of us. */
20705 : 2070 : bool existed;
20706 : 2070 : unsigned *slot = &entity_map->get_or_insert
20707 : 2070 : (DECL_UID (decl), &existed);
20708 : 2070 : if (existed)
20709 : : /* It must have come from a partition. */
20710 : 0 : gcc_checking_assert
20711 : : (import_entity_module (*slot)->is_partition ());
20712 : 2070 : *slot = ~base[0]->cluster;
20713 : : }
20714 : 229480 : dump (dumper::CLUSTER) && dump ("Cluster namespace %N", decl);
20715 : : }
20716 : : size = 1;
20717 : : }
20718 : : else
20719 : : {
20720 : 223005 : size = base[0]->cluster;
20721 : :
20722 : : /* Cluster is now used to number entities. */
20723 : 223005 : base[0]->cluster = ~(~0u >> 1); /* A bad value. */
20724 : :
20725 : 223005 : sort_cluster (&table, base, size);
20726 : :
20727 : : /* Record the section for consistency checking during stream
20728 : : out -- we don't want to start writing decls in different
20729 : : sections. */
20730 : 223005 : table.section = base[0]->section;
20731 : 223005 : bytes += write_cluster (to, base, size, table, counts, &crc);
20732 : 223005 : table.section = 0;
20733 : : }
20734 : : }
20735 : :
20736 : : /* depset::cluster - entity number (on entities)
20737 : : depset::section - cluster number */
20738 : : /* We'd better have written as many sections and found as many
20739 : : namespaces as we predicted. */
20740 : 5088 : gcc_assert (counts[MSC_sec_hwm] == to->get_section_limit ()
20741 : : && spaces.length () == counts[MSC_namespaces]);
20742 : :
20743 : : /* Write the entitites. None happens if we contain namespaces or
20744 : : nothing. */
20745 : 2544 : config.num_entities = counts[MSC_entities];
20746 : 2544 : if (counts[MSC_entities])
20747 : 2299 : write_entities (to, sccs, counts[MSC_entities], &crc);
20748 : :
20749 : : /* Write the namespaces. */
20750 : 2544 : if (counts[MSC_namespaces])
20751 : 527 : write_namespaces (to, spaces, counts[MSC_namespaces], &crc);
20752 : :
20753 : : /* Write any using-directives. */
20754 : 2544 : if (counts[MSC_namespaces])
20755 : 527 : counts[MSC_using_directives]
20756 : 527 : = write_using_directives (to, table, spaces, &crc);
20757 : :
20758 : : /* Write the bindings themselves. */
20759 : 2544 : counts[MSC_bindings] = write_bindings (to, sccs, &crc);
20760 : :
20761 : : /* Write the unnamed. */
20762 : 2544 : counts[MSC_pendings] = write_pendings (to, sccs, table, &crc);
20763 : :
20764 : : /* Write the import table. */
20765 : 2544 : if (config.num_imports > 1)
20766 : 405 : write_imports (to, &crc);
20767 : :
20768 : : /* Write elided partition table. */
20769 : 2544 : if (config.num_partitions)
20770 : 115 : write_partitions (to, config.num_partitions, &crc);
20771 : :
20772 : : /* Write the line maps. */
20773 : 2544 : if (config.ordinary_locs)
20774 : : {
20775 : 2448 : write_ordinary_maps (to, map_info, bool (config.num_partitions), &crc);
20776 : 2448 : write_diagnostic_classification (to, global_dc, &crc);
20777 : : }
20778 : 2544 : if (config.macro_locs)
20779 : 117 : write_macro_maps (to, map_info, &crc);
20780 : :
20781 : 2544 : if (is_header ())
20782 : : {
20783 : 876 : counts[MSC_macros] = write_macros (to, macros, &crc);
20784 : 876 : counts[MSC_inits] = write_inits (to, table, &crc);
20785 : 876 : vec_free (macros);
20786 : : }
20787 : :
20788 : 2544 : unsigned clusters = counts[MSC_sec_hwm] - counts[MSC_sec_lwm];
20789 : 2544 : dump () && dump ("Wrote %u clusters, average %u bytes/cluster",
20790 : 273 : clusters, (bytes + clusters / 2) / (clusters + !clusters));
20791 : 2544 : trees_out::instrument ();
20792 : :
20793 : 2544 : write_counts (to, counts, &crc);
20794 : :
20795 : 2544 : spaces.release ();
20796 : 2544 : sccs.release ();
20797 : :
20798 : 2544 : vec_free (macro_loc_remap);
20799 : 2544 : vec_free (ord_loc_remap);
20800 : 2544 : vec_free (ool);
20801 : :
20802 : : // FIXME:QOI: Have a command line switch to control more detailed
20803 : : // information (which might leak data you do not want to leak).
20804 : : // Perhaps (some of) the write_readme contents should also be
20805 : : // so-controlled.
20806 : 2544 : if (false)
20807 : : write_env (to);
20808 : :
20809 : 2544 : return true;
20810 : 2573 : }
20811 : :
20812 : : // Finish module writing after we've emitted all dynamic initializers.
20813 : :
20814 : : void
20815 : 2544 : module_state::write_end (elf_out *to, cpp_reader *reader,
20816 : : module_state_config &config, unsigned &crc)
20817 : : {
20818 : : /* And finish up. */
20819 : 2544 : write_config (to, config, crc);
20820 : :
20821 : : /* Human-readable info. */
20822 : 2544 : write_readme (to, reader, config.dialect_str);
20823 : :
20824 : 2817 : dump () && dump ("Wrote %u sections", to->get_section_limit ());
20825 : 2544 : }
20826 : :
20827 : : /* Initial read of a CMI. Checks config, loads up imports and line
20828 : : maps. */
20829 : :
20830 : : bool
20831 : 2786 : module_state::read_initial (cpp_reader *reader)
20832 : : {
20833 : 2786 : module_state_config config;
20834 : 2786 : bool ok = true;
20835 : :
20836 : 2786 : if (ok && !read_config (config))
20837 : : ok = false;
20838 : :
20839 : 2773 : bool have_locs = ok && read_prepare_maps (&config);
20840 : :
20841 : : /* Ordinary maps before the imports. */
20842 : 2773 : if (!(have_locs && config.ordinary_locs))
20843 : 71 : ordinary_locs.first = line_table->highest_location + 1;
20844 : 2715 : else if (!read_ordinary_maps (config.ordinary_locs, config.loc_range_bits))
20845 : : ok = false;
20846 : :
20847 : 2773 : if (ok && have_locs && config.ordinary_locs
20848 : 5501 : && !read_diagnostic_classification (global_dc))
20849 : : ok = false;
20850 : :
20851 : : /* Allocate the REMAP vector. */
20852 : 2786 : slurp->alloc_remap (config.num_imports);
20853 : :
20854 : 2786 : if (ok)
20855 : : {
20856 : : /* Read the import table. Decrement current to stop this CMI
20857 : : from being evicted during the import. */
20858 : 2773 : slurp->current--;
20859 : 2773 : if (config.num_imports > 1 && !read_imports (reader, line_table))
20860 : : ok = false;
20861 : 2773 : slurp->current++;
20862 : : }
20863 : :
20864 : : /* Read the elided partition table, if we're the primary partition. */
20865 : 2773 : if (ok && config.num_partitions && is_module ()
20866 : 2791 : && !read_partitions (config.num_partitions))
20867 : : ok = false;
20868 : :
20869 : : /* Determine the module's number. */
20870 : 2786 : gcc_checking_assert (mod == MODULE_UNKNOWN);
20871 : 2786 : gcc_checking_assert (this != this_module ());
20872 : :
20873 : 2786 : {
20874 : : /* Allocate space in the entities array now -- that array must be
20875 : : monotonically in step with the modules array. */
20876 : 2786 : entity_lwm = vec_safe_length (entity_ary);
20877 : 2786 : entity_num = config.num_entities;
20878 : 2786 : gcc_checking_assert (modules->length () == 1
20879 : : || modules->last ()->entity_lwm <= entity_lwm);
20880 : 2786 : vec_safe_reserve (entity_ary, config.num_entities);
20881 : :
20882 : 2786 : binding_slot slot;
20883 : 2786 : slot.u.binding = NULL_TREE;
20884 : 959566 : for (unsigned count = config.num_entities; count--;)
20885 : 956780 : entity_ary->quick_push (slot);
20886 : : }
20887 : :
20888 : : /* We'll run out of other resources before we run out of module
20889 : : indices. */
20890 : 2786 : mod = modules->length ();
20891 : 2786 : vec_safe_push (modules, this);
20892 : :
20893 : : /* We always import and export ourselves. */
20894 : 2786 : bitmap_set_bit (imports, mod);
20895 : 2786 : bitmap_set_bit (exports, mod);
20896 : :
20897 : 2786 : if (ok)
20898 : 2773 : (*slurp->remap)[0] = mod << 1;
20899 : 3276 : dump () && dump ("Assigning %M module number %u", this, mod);
20900 : :
20901 : : /* We should not have been frozen during the importing done by
20902 : : read_config. */
20903 : 2786 : gcc_assert (!from ()->is_frozen ());
20904 : :
20905 : : /* Macro maps after the imports. */
20906 : 2786 : if (!(ok && have_locs && config.macro_locs))
20907 : 2659 : macro_locs.first = LINEMAPS_MACRO_LOWEST_LOCATION (line_table);
20908 : 127 : else if (!read_macro_maps (config.macro_locs))
20909 : : ok = false;
20910 : :
20911 : : /* Note whether there's an active initializer. */
20912 : 2786 : active_init_p = !is_header () && bool (config.active_init);
20913 : :
20914 : 2786 : gcc_assert (slurp->current == ~0u);
20915 : 2786 : return ok;
20916 : : }
20917 : :
20918 : : /* Read a preprocessor state. */
20919 : :
20920 : : bool
20921 : 913 : module_state::read_preprocessor (bool outermost)
20922 : : {
20923 : 913 : gcc_checking_assert (is_header () && slurp
20924 : : && slurp->remap_module (0) == mod);
20925 : :
20926 : 913 : if (loadedness == ML_PREPROCESSOR)
20927 : 6 : return !(from () && from ()->get_error ());
20928 : :
20929 : 907 : bool ok = true;
20930 : :
20931 : : /* Read direct header imports. */
20932 : 907 : unsigned len = slurp->remap->length ();
20933 : 949 : for (unsigned ix = 1; ok && ix != len; ix++)
20934 : : {
20935 : 42 : unsigned map = (*slurp->remap)[ix];
20936 : 42 : if (map & 1)
20937 : : {
20938 : 42 : module_state *import = (*modules)[map >> 1];
20939 : 42 : if (import->is_header ())
20940 : : {
20941 : 42 : ok = import->read_preprocessor (false);
20942 : 42 : bitmap_ior_into (slurp->headers, import->slurp->headers);
20943 : : }
20944 : : }
20945 : : }
20946 : :
20947 : : /* Record as a direct header. */
20948 : 907 : if (ok)
20949 : 907 : bitmap_set_bit (slurp->headers, mod);
20950 : :
20951 : 907 : if (ok && !read_macros ())
20952 : : ok = false;
20953 : :
20954 : 907 : loadedness = ML_PREPROCESSOR;
20955 : 907 : announce ("macros");
20956 : :
20957 : 907 : if (flag_preprocess_only)
20958 : : /* We're done with the string table. */
20959 : 39 : from ()->release ();
20960 : :
20961 : 907 : return check_read (outermost, ok);
20962 : : }
20963 : :
20964 : : /* Read language state. */
20965 : :
20966 : : bool
20967 : 2796 : module_state::read_language (bool outermost)
20968 : : {
20969 : 2796 : gcc_checking_assert (!lazy_snum);
20970 : :
20971 : 2796 : if (loadedness == ML_LANGUAGE)
20972 : 74 : return !(slurp && from () && from ()->get_error ());
20973 : :
20974 : 2722 : gcc_checking_assert (slurp && slurp->current == ~0u
20975 : : && slurp->remap_module (0) == mod);
20976 : :
20977 : 2722 : bool ok = true;
20978 : :
20979 : : /* Read direct imports. */
20980 : 2722 : unsigned len = slurp->remap->length ();
20981 : 3080 : for (unsigned ix = 1; ok && ix != len; ix++)
20982 : : {
20983 : 358 : unsigned map = (*slurp->remap)[ix];
20984 : 358 : if (map & 1)
20985 : : {
20986 : 355 : module_state *import = (*modules)[map >> 1];
20987 : 355 : if (!import->read_language (false))
20988 : 358 : ok = false;
20989 : : }
20990 : : }
20991 : :
20992 : 2722 : unsigned counts[MSC_HWM];
20993 : :
20994 : 2722 : if (ok && !read_counts (counts))
20995 : : ok = false;
20996 : :
20997 : 2722 : function_depth++; /* Prevent unexpected GCs. */
20998 : :
20999 : 2722 : if (ok && counts[MSC_entities] != entity_num)
21000 : : ok = false;
21001 : 2722 : if (ok && counts[MSC_entities]
21002 : 2517 : && !read_entities (counts[MSC_entities],
21003 : : counts[MSC_sec_lwm], counts[MSC_sec_hwm]))
21004 : : ok = false;
21005 : :
21006 : : /* Read the namespace hierarchy. */
21007 : 2722 : if (ok && counts[MSC_namespaces]
21008 : 3277 : && !read_namespaces (counts[MSC_namespaces]))
21009 : : ok = false;
21010 : :
21011 : : /* Read any using-directives. */
21012 : 2722 : if (ok && counts[MSC_using_directives]
21013 : 2852 : && !read_using_directives (counts[MSC_using_directives]))
21014 : : ok = false;
21015 : :
21016 : 2722 : if (ok && !read_bindings (counts[MSC_bindings],
21017 : : counts[MSC_sec_lwm], counts[MSC_sec_hwm]))
21018 : : ok = false;
21019 : :
21020 : : /* And unnamed. */
21021 : 2722 : if (ok && counts[MSC_pendings] && !read_pendings (counts[MSC_pendings]))
21022 : : ok = false;
21023 : :
21024 : 2722 : if (ok)
21025 : : {
21026 : 2722 : slurp->remaining = counts[MSC_sec_hwm] - counts[MSC_sec_lwm];
21027 : 2722 : available_clusters += counts[MSC_sec_hwm] - counts[MSC_sec_lwm];
21028 : : }
21029 : :
21030 : 2722 : if (!flag_module_lazy
21031 : 2722 : || (is_partition ()
21032 : 214 : && module_interface_p ()
21033 : 187 : && !module_partition_p ()))
21034 : : {
21035 : : /* Read the sections in forward order, so that dependencies are read
21036 : : first. See note about tarjan_connect. */
21037 : 495 : ggc_collect ();
21038 : :
21039 : 495 : lazy_snum = ~0u;
21040 : :
21041 : 495 : unsigned hwm = counts[MSC_sec_hwm];
21042 : 137323 : for (unsigned ix = counts[MSC_sec_lwm]; ok && ix != hwm; ix++)
21043 : 136828 : if (!load_section (ix, NULL))
21044 : : {
21045 : : ok = false;
21046 : : break;
21047 : : }
21048 : 495 : lazy_snum = 0;
21049 : 495 : post_load_processing ();
21050 : :
21051 : 495 : ggc_collect ();
21052 : :
21053 : 495 : if (ok && CHECKING_P)
21054 : 496674 : for (unsigned ix = 0; ix != entity_num; ix++)
21055 : 496179 : gcc_assert (!(*entity_ary)[ix + entity_lwm].is_lazy ());
21056 : : }
21057 : :
21058 : : // If the import is a header-unit, we need to register initializers
21059 : : // of any static objects it contains (looking at you _Ioinit).
21060 : : // Notice, the ordering of these initializers will be that of a
21061 : : // dynamic initializer at this point in the current TU. (Other
21062 : : // instances of these objects in other TUs will be initialized as
21063 : : // part of that TU's global initializers.)
21064 : 2722 : if (ok && counts[MSC_inits] && !read_inits (counts[MSC_inits]))
21065 : : ok = false;
21066 : :
21067 : 2722 : function_depth--;
21068 : :
21069 : 3054 : announce (flag_module_lazy ? "lazy" : "imported");
21070 : 2722 : loadedness = ML_LANGUAGE;
21071 : :
21072 : 2722 : gcc_assert (slurp->current == ~0u);
21073 : :
21074 : : /* We're done with the string table. */
21075 : 2722 : from ()->release ();
21076 : :
21077 : 2722 : return check_read (outermost, ok);
21078 : : }
21079 : :
21080 : : bool
21081 : 165505 : module_state::maybe_defrost ()
21082 : : {
21083 : 165505 : bool ok = true;
21084 : 165505 : if (from ()->is_frozen ())
21085 : : {
21086 : 9 : if (lazy_open >= lazy_limit)
21087 : 3 : freeze_an_elf ();
21088 : 18 : dump () && dump ("Defrosting '%s'", filename);
21089 : 9 : ok = from ()->defrost (maybe_add_cmi_prefix (filename));
21090 : 9 : lazy_open++;
21091 : : }
21092 : :
21093 : 165505 : return ok;
21094 : : }
21095 : :
21096 : : /* Load section SNUM, dealing with laziness. It doesn't matter if we
21097 : : have multiple concurrent loads, because we do not use TREE_VISITED
21098 : : when reading back in. */
21099 : :
21100 : : bool
21101 : 165505 : module_state::load_section (unsigned snum, binding_slot *mslot)
21102 : : {
21103 : 165505 : if (from ()->get_error ())
21104 : : return false;
21105 : :
21106 : 165505 : if (snum >= slurp->current)
21107 : 0 : from ()->set_error (elf::E_BAD_LAZY);
21108 : 165505 : else if (maybe_defrost ())
21109 : : {
21110 : 165505 : unsigned old_current = slurp->current;
21111 : 165505 : slurp->current = snum;
21112 : 165505 : slurp->lru = 0; /* Do not swap out. */
21113 : 165505 : slurp->remaining--;
21114 : 165505 : read_cluster (snum);
21115 : 165505 : slurp->lru = ++lazy_lru;
21116 : 165505 : slurp->current = old_current;
21117 : : }
21118 : :
21119 : 165505 : if (mslot && mslot->is_lazy ())
21120 : : {
21121 : : /* Oops, the section didn't set this slot. */
21122 : 0 : from ()->set_error (elf::E_BAD_DATA);
21123 : 0 : *mslot = NULL_TREE;
21124 : : }
21125 : :
21126 : 165505 : bool ok = !from ()->get_error ();
21127 : 165505 : if (!ok)
21128 : : {
21129 : 0 : error_at (loc, "failed to read compiled module cluster %u: %s",
21130 : 0 : snum, from ()->get_error (filename));
21131 : 0 : note_cmi_name ();
21132 : : }
21133 : :
21134 : 165505 : maybe_completed_reading ();
21135 : :
21136 : 165505 : return ok;
21137 : : }
21138 : :
21139 : : void
21140 : 171904 : module_state::maybe_completed_reading ()
21141 : : {
21142 : 171904 : if (loadedness == ML_LANGUAGE && slurp->current == ~0u && !slurp->remaining)
21143 : : {
21144 : 2247 : lazy_open--;
21145 : : /* We no longer need the macros, all tokenizing has been done. */
21146 : 2247 : slurp->release_macros ();
21147 : :
21148 : 2247 : from ()->end ();
21149 : 2247 : slurp->close ();
21150 : 2247 : slurped ();
21151 : : }
21152 : 171904 : }
21153 : :
21154 : : /* After a reading operation, make sure things are still ok. If not,
21155 : : emit an error and clean up. */
21156 : :
21157 : : bool
21158 : 6436 : module_state::check_read (bool outermost, bool ok)
21159 : : {
21160 : 6436 : gcc_checking_assert (!outermost || slurp->current == ~0u);
21161 : :
21162 : 6436 : if (!ok)
21163 : 34 : from ()->set_error ();
21164 : :
21165 : 6436 : if (int e = from ()->get_error ())
21166 : : {
21167 : 40 : auto_diagnostic_group d;
21168 : 40 : error_at (loc, "failed to read compiled module: %s",
21169 : 40 : from ()->get_error (filename));
21170 : 40 : note_cmi_name ();
21171 : :
21172 : 40 : if (e == EMFILE
21173 : 40 : || e == ENFILE
21174 : : #if MAPPED_READING
21175 : 40 : || e == ENOMEM
21176 : : #endif
21177 : : || false)
21178 : 0 : inform (loc, "consider using %<-fno-module-lazy%>,"
21179 : : " increasing %<-param-lazy-modules=%u%> value,"
21180 : : " or increasing the per-process file descriptor limit",
21181 : : param_lazy_modules);
21182 : 40 : else if (e == ENOENT)
21183 : 21 : inform (loc, "imports must be built before being imported");
21184 : :
21185 : 40 : if (outermost)
21186 : 37 : fatal_error (loc, "returning to the gate for a mechanical issue");
21187 : :
21188 : 3 : ok = false;
21189 : 3 : }
21190 : :
21191 : 6399 : maybe_completed_reading ();
21192 : :
21193 : 6399 : return ok;
21194 : : }
21195 : :
21196 : : /* Return the IDENTIFIER_NODE naming module IX. This is the name
21197 : : including dots. */
21198 : :
21199 : : char const *
21200 : 331 : module_name (unsigned ix, bool header_ok)
21201 : : {
21202 : 331 : if (modules)
21203 : : {
21204 : 331 : module_state *imp = (*modules)[ix];
21205 : :
21206 : 331 : if (ix && !imp->name)
21207 : 0 : imp = imp->parent;
21208 : :
21209 : 331 : if (header_ok || !imp->is_header ())
21210 : 331 : return imp->get_flatname ();
21211 : : }
21212 : :
21213 : : return NULL;
21214 : : }
21215 : :
21216 : : /* Return the bitmap describing what modules are imported. Remember,
21217 : : we always import ourselves. */
21218 : :
21219 : : bitmap
21220 : 73909 : get_import_bitmap ()
21221 : : {
21222 : 73909 : return this_module ()->imports;
21223 : : }
21224 : :
21225 : : /* Return the visible imports and path of instantiation for an
21226 : : instantiation at TINST. If TINST is nullptr, we're not in an
21227 : : instantiation, and thus will return the visible imports of the
21228 : : current TU (and NULL *PATH_MAP_P). We cache the information on
21229 : : the tinst level itself. */
21230 : :
21231 : : static bitmap
21232 : 96942 : path_of_instantiation (tinst_level *tinst, bitmap *path_map_p)
21233 : : {
21234 : 96942 : gcc_checking_assert (modules_p ());
21235 : :
21236 : 96942 : if (!tinst || TREE_CODE (tinst->tldcl) == TEMPLATE_FOR_STMT)
21237 : : {
21238 : 1 : gcc_assert (!tinst || !tinst->next);
21239 : : /* Not inside an instantiation, just the regular case. */
21240 : 43513 : *path_map_p = nullptr;
21241 : 43513 : return get_import_bitmap ();
21242 : : }
21243 : :
21244 : 53429 : if (!tinst->path)
21245 : : {
21246 : : /* Calculate. */
21247 : 15648 : bitmap visible = path_of_instantiation (tinst->next, path_map_p);
21248 : 15648 : bitmap path_map = *path_map_p;
21249 : :
21250 : 15648 : if (!path_map)
21251 : : {
21252 : 2036 : path_map = BITMAP_GGC_ALLOC ();
21253 : 2036 : bitmap_set_bit (path_map, 0);
21254 : : }
21255 : :
21256 : 15648 : tree decl = tinst->tldcl;
21257 : 15648 : if (TREE_CODE (decl) == TREE_LIST)
21258 : 0 : decl = TREE_PURPOSE (decl);
21259 : 15648 : if (TYPE_P (decl))
21260 : 1789 : decl = TYPE_NAME (decl);
21261 : :
21262 : 15648 : if (unsigned mod = get_originating_module (decl))
21263 : 2090 : if (!bitmap_bit_p (path_map, mod))
21264 : : {
21265 : : /* This is brand new information! */
21266 : 109 : bitmap new_path = BITMAP_GGC_ALLOC ();
21267 : 109 : bitmap_copy (new_path, path_map);
21268 : 109 : bitmap_set_bit (new_path, mod);
21269 : 109 : path_map = new_path;
21270 : :
21271 : 109 : bitmap imports = (*modules)[mod]->imports;
21272 : 109 : if (bitmap_intersect_compl_p (imports, visible))
21273 : : {
21274 : : /* IMPORTS contains additional modules to VISIBLE. */
21275 : 12 : bitmap new_visible = BITMAP_GGC_ALLOC ();
21276 : :
21277 : 12 : bitmap_ior (new_visible, visible, imports);
21278 : 12 : visible = new_visible;
21279 : : }
21280 : : }
21281 : :
21282 : 15648 : tinst->path = path_map;
21283 : 15648 : tinst->visible = visible;
21284 : : }
21285 : :
21286 : 53429 : *path_map_p = tinst->path;
21287 : 53429 : return tinst->visible;
21288 : : }
21289 : :
21290 : : /* Return the bitmap describing what modules are visible along the
21291 : : path of instantiation. If we're not an instantiation, this will be
21292 : : the visible imports of the TU. *PATH_MAP_P is filled in with the
21293 : : modules owning the instantiation path -- we see the module-linkage
21294 : : entities of those modules. */
21295 : :
21296 : : bitmap
21297 : 21638648 : visible_instantiation_path (bitmap *path_map_p)
21298 : : {
21299 : 21638648 : if (!modules_p ())
21300 : : return NULL;
21301 : :
21302 : 81294 : return path_of_instantiation (current_instantiation (), path_map_p);
21303 : : }
21304 : :
21305 : : /* We've just directly imported IMPORT. Update our import/export
21306 : : bitmaps. IS_EXPORT is true if we're reexporting the OTHER. */
21307 : :
21308 : : void
21309 : 2863 : module_state::set_import (module_state const *import, bool is_export)
21310 : : {
21311 : 2863 : gcc_checking_assert (this != import);
21312 : :
21313 : : /* We see IMPORT's exports (which includes IMPORT). If IMPORT is
21314 : : the primary interface or a partition we'll see its imports. */
21315 : 2863 : bitmap_ior_into (imports, import->is_module () || import->is_partition ()
21316 : : ? import->imports : import->exports);
21317 : :
21318 : 2863 : if (is_export)
21319 : : /* We'll export OTHER's exports. */
21320 : 407 : bitmap_ior_into (exports, import->exports);
21321 : 2863 : }
21322 : :
21323 : : /* Return the declaring entity of DECL. That is the decl determining
21324 : : how to decorate DECL with module information. Returns NULL_TREE if
21325 : : it's the global module. */
21326 : :
21327 : : tree
21328 : 164250611 : get_originating_module_decl (tree decl)
21329 : : {
21330 : : /* An enumeration constant. */
21331 : 164250611 : if (TREE_CODE (decl) == CONST_DECL
21332 : 4860 : && DECL_CONTEXT (decl)
21333 : 164255471 : && (TREE_CODE (DECL_CONTEXT (decl)) == ENUMERAL_TYPE))
21334 : 4341 : decl = TYPE_NAME (DECL_CONTEXT (decl));
21335 : 164246270 : else if (TREE_CODE (decl) == FIELD_DECL
21336 : 164237817 : || TREE_CODE (decl) == USING_DECL
21337 : 328476543 : || CONST_DECL_USING_P (decl))
21338 : : {
21339 : 16516 : decl = DECL_CONTEXT (decl);
21340 : 16516 : if (TREE_CODE (decl) != FUNCTION_DECL)
21341 : 16516 : decl = TYPE_NAME (decl);
21342 : : }
21343 : :
21344 : 164250611 : gcc_checking_assert (TREE_CODE (decl) == TEMPLATE_DECL
21345 : : || TREE_CODE (decl) == FUNCTION_DECL
21346 : : || TREE_CODE (decl) == TYPE_DECL
21347 : : || TREE_CODE (decl) == VAR_DECL
21348 : : || TREE_CODE (decl) == CONCEPT_DECL
21349 : : || TREE_CODE (decl) == NAMESPACE_DECL);
21350 : :
21351 : 165439463 : for (;;)
21352 : : {
21353 : : /* Uninstantiated template friends are owned by the befriending
21354 : : class -- not their context. */
21355 : 164845037 : if (TREE_CODE (decl) == TEMPLATE_DECL
21356 : 164845037 : && DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (decl))
21357 : 6147 : decl = TYPE_NAME (DECL_CHAIN (decl));
21358 : :
21359 : : /* An imported temploid friend is attached to the same module the
21360 : : befriending class was. */
21361 : 164845037 : if (imported_temploid_friends)
21362 : 2137954 : if (tree *slot = imported_temploid_friends->get (decl))
21363 : 607 : decl = *slot;
21364 : :
21365 : 164845037 : int use;
21366 : 164845037 : if (tree ti = node_template_info (decl, use))
21367 : : {
21368 : 480789 : decl = TI_TEMPLATE (ti);
21369 : 480789 : if (TREE_CODE (decl) != TEMPLATE_DECL)
21370 : : {
21371 : : /* A friend template specialization. */
21372 : 22 : gcc_checking_assert (OVL_P (decl));
21373 : 22 : return global_namespace;
21374 : : }
21375 : : }
21376 : : else
21377 : : {
21378 : 164364248 : tree ctx = CP_DECL_CONTEXT (decl);
21379 : 164364248 : if (TREE_CODE (ctx) == NAMESPACE_DECL)
21380 : : break;
21381 : :
21382 : 113659 : if (TYPE_P (ctx))
21383 : : {
21384 : 107415 : ctx = TYPE_NAME (ctx);
21385 : 107415 : if (!ctx)
21386 : : {
21387 : : /* Some kind of internal type. */
21388 : 0 : gcc_checking_assert (DECL_ARTIFICIAL (decl));
21389 : 0 : return global_namespace;
21390 : : }
21391 : : }
21392 : 113659 : decl = ctx;
21393 : : }
21394 : 594426 : }
21395 : :
21396 : 164250589 : return decl;
21397 : : }
21398 : :
21399 : : /* If DECL is imported, return which module imported it, or 0 for the current
21400 : : module. Except that if GLOBAL_M1, return -1 for decls attached to the
21401 : : global module. */
21402 : :
21403 : : int
21404 : 853316 : get_originating_module (tree decl, bool global_m1)
21405 : : {
21406 : 853316 : tree owner = get_originating_module_decl (decl);
21407 : 853316 : tree not_tmpl = STRIP_TEMPLATE (owner);
21408 : :
21409 : 853316 : if (!DECL_LANG_SPECIFIC (not_tmpl))
21410 : 226629 : return global_m1 ? -1 : 0;
21411 : :
21412 : 1219595 : if (global_m1 && !DECL_MODULE_ATTACH_P (not_tmpl))
21413 : : return -1;
21414 : :
21415 : 89239 : int mod = !DECL_MODULE_IMPORT_P (not_tmpl) ? 0 : get_importing_module (owner);
21416 : 89239 : gcc_checking_assert (!global_m1 || !(*modules)[mod]->is_header ());
21417 : : return mod;
21418 : : }
21419 : :
21420 : : /* DECL is imported, return which module imported it.
21421 : : If FLEXIBLE, return -1 if not found, otherwise checking ICE. */
21422 : :
21423 : : unsigned
21424 : 5348 : get_importing_module (tree decl, bool flexible)
21425 : : {
21426 : 5348 : unsigned index = import_entity_index (decl, flexible);
21427 : 5348 : if (index == ~(~0u >> 1))
21428 : : return -1;
21429 : 5348 : module_state *module = import_entity_module (index);
21430 : :
21431 : 5348 : return module->mod;
21432 : : }
21433 : :
21434 : : /* Is it permissible to redeclare OLDDECL with NEWDECL.
21435 : :
21436 : : If NEWDECL is NULL, assumes that OLDDECL will be redeclared using
21437 : : the current scope's module and attachment. */
21438 : :
21439 : : bool
21440 : 139921 : module_may_redeclare (tree olddecl, tree newdecl)
21441 : : {
21442 : 139921 : tree decl = olddecl;
21443 : 156565 : for (;;)
21444 : : {
21445 : 148243 : tree ctx = CP_DECL_CONTEXT (decl);
21446 : 148243 : if (TREE_CODE (ctx) == NAMESPACE_DECL)
21447 : : // Found the namespace-scope decl.
21448 : : break;
21449 : 15918 : if (!CLASS_TYPE_P (ctx))
21450 : : // We've met a non-class scope. Such a thing is not
21451 : : // reopenable, so we must be ok.
21452 : : return true;
21453 : 8322 : decl = TYPE_NAME (ctx);
21454 : 8322 : }
21455 : :
21456 : 132325 : int use_tpl = 0;
21457 : 132325 : if (node_template_info (STRIP_TEMPLATE (decl), use_tpl) && use_tpl)
21458 : : // Specializations of any kind can be redeclared anywhere.
21459 : : // FIXME: Should we be checking this in more places on the scope chain?
21460 : : return true;
21461 : :
21462 : 88649 : module_state *old_mod = get_primary (this_module ());
21463 : 88649 : module_state *new_mod = old_mod;
21464 : :
21465 : 88649 : tree old_origin = get_originating_module_decl (decl);
21466 : 88649 : tree old_inner = STRIP_TEMPLATE (old_origin);
21467 : 88649 : bool olddecl_attached_p = (DECL_LANG_SPECIFIC (old_inner)
21468 : 141850 : && DECL_MODULE_ATTACH_P (old_inner));
21469 : 141850 : if (DECL_LANG_SPECIFIC (old_inner) && DECL_MODULE_IMPORT_P (old_inner))
21470 : : {
21471 : 434 : unsigned index = import_entity_index (old_origin);
21472 : 434 : old_mod = get_primary (import_entity_module (index));
21473 : : }
21474 : :
21475 : 88649 : bool newdecl_attached_p = module_attach_p ();
21476 : 88649 : if (newdecl)
21477 : : {
21478 : 24928 : tree new_origin = get_originating_module_decl (newdecl);
21479 : 24928 : tree new_inner = STRIP_TEMPLATE (new_origin);
21480 : 24928 : newdecl_attached_p = (DECL_LANG_SPECIFIC (new_inner)
21481 : 48643 : && DECL_MODULE_ATTACH_P (new_inner));
21482 : 48643 : if (DECL_LANG_SPECIFIC (new_inner) && DECL_MODULE_IMPORT_P (new_inner))
21483 : : {
21484 : 136 : unsigned index = import_entity_index (new_origin);
21485 : 136 : new_mod = get_primary (import_entity_module (index));
21486 : : }
21487 : : }
21488 : :
21489 : : /* Module attachment needs to match. */
21490 : 88649 : if (olddecl_attached_p == newdecl_attached_p)
21491 : : {
21492 : 88607 : if (!olddecl_attached_p)
21493 : : /* Both are GM entities, OK. */
21494 : : return true;
21495 : :
21496 : 1218 : if (new_mod == old_mod)
21497 : : /* Both attached to same named module, OK. */
21498 : : return true;
21499 : : }
21500 : :
21501 : : /* Attached to different modules, error. */
21502 : 45 : decl = newdecl ? newdecl : olddecl;
21503 : 45 : location_t loc = newdecl ? DECL_SOURCE_LOCATION (newdecl) : input_location;
21504 : 45 : if (DECL_IS_UNDECLARED_BUILTIN (olddecl))
21505 : : {
21506 : 3 : if (newdecl_attached_p)
21507 : 3 : error_at (loc, "declaring %qD in module %qs conflicts with builtin "
21508 : : "in global module", decl, new_mod->get_flatname ());
21509 : : else
21510 : 0 : error_at (loc, "declaration %qD conflicts with builtin", decl);
21511 : : }
21512 : 81 : else if (DECL_LANG_SPECIFIC (old_inner) && DECL_MODULE_IMPORT_P (old_inner))
21513 : : {
21514 : 27 : auto_diagnostic_group d;
21515 : 27 : if (newdecl_attached_p)
21516 : 3 : error_at (loc, "redeclaring %qD in module %qs conflicts with import",
21517 : : decl, new_mod->get_flatname ());
21518 : : else
21519 : 24 : error_at (loc, "redeclaring %qD in global module conflicts with import",
21520 : : decl);
21521 : :
21522 : 27 : if (olddecl_attached_p)
21523 : 27 : inform (DECL_SOURCE_LOCATION (olddecl),
21524 : : "import declared attached to module %qs",
21525 : : old_mod->get_flatname ());
21526 : : else
21527 : 0 : inform (DECL_SOURCE_LOCATION (olddecl),
21528 : : "import declared in global module");
21529 : 27 : }
21530 : : else
21531 : : {
21532 : 15 : auto_diagnostic_group d;
21533 : 15 : if (newdecl_attached_p)
21534 : 15 : error_at (loc, "conflicting declaration of %qD in module %qs",
21535 : : decl, new_mod->get_flatname ());
21536 : : else
21537 : 0 : error_at (loc, "conflicting declaration of %qD in global module",
21538 : : decl);
21539 : :
21540 : 15 : if (olddecl_attached_p)
21541 : 0 : inform (DECL_SOURCE_LOCATION (olddecl),
21542 : : "previously declared in module %qs",
21543 : : old_mod->get_flatname ());
21544 : : else
21545 : 15 : inform (DECL_SOURCE_LOCATION (olddecl),
21546 : : "previously declared in global module");
21547 : 15 : }
21548 : : return false;
21549 : : }
21550 : :
21551 : : /* DECL is being created by this TU. Record it came from here. We
21552 : : record module purview, so we can see if partial or explicit
21553 : : specialization needs to be written out, even though its purviewness
21554 : : comes from the most general template. */
21555 : :
21556 : : void
21557 : 791662327 : set_instantiating_module (tree decl)
21558 : : {
21559 : 791662327 : gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
21560 : : || VAR_P (decl)
21561 : : || TREE_CODE (decl) == TYPE_DECL
21562 : : || TREE_CODE (decl) == CONCEPT_DECL
21563 : : || TREE_CODE (decl) == TEMPLATE_DECL
21564 : : || TREE_CODE (decl) == CONST_DECL
21565 : : || (TREE_CODE (decl) == NAMESPACE_DECL
21566 : : && DECL_NAMESPACE_ALIAS (decl)));
21567 : :
21568 : 791662327 : if (!modules_p ())
21569 : : return;
21570 : :
21571 : 2539485 : decl = STRIP_TEMPLATE (decl);
21572 : :
21573 : 2539485 : if (!DECL_LANG_SPECIFIC (decl) && module_purview_p ())
21574 : 319642 : retrofit_lang_decl (decl);
21575 : :
21576 : 2539485 : if (DECL_LANG_SPECIFIC (decl))
21577 : : {
21578 : 2015774 : DECL_MODULE_PURVIEW_P (decl) = module_purview_p ();
21579 : : /* If this was imported, we'll still be in the entity_hash. */
21580 : 2015774 : DECL_MODULE_IMPORT_P (decl) = false;
21581 : : }
21582 : : }
21583 : :
21584 : : /* If DECL is a class member, whose class is not defined in this TU
21585 : : (it was imported), remember this decl. */
21586 : :
21587 : : void
21588 : 118517 : set_defining_module (tree decl)
21589 : : {
21590 : 118517 : gcc_checking_assert (!DECL_LANG_SPECIFIC (decl)
21591 : : || !DECL_MODULE_IMPORT_P (decl));
21592 : :
21593 : 118517 : if (module_maybe_has_cmi_p ())
21594 : : {
21595 : : /* We need to track all declarations within a module, not just those
21596 : : in the module purview, because we don't necessarily know yet if
21597 : : this module will require a CMI while in the global fragment. */
21598 : 79464 : tree ctx = DECL_CONTEXT (decl);
21599 : 79464 : if (ctx
21600 : 79464 : && (TREE_CODE (ctx) == RECORD_TYPE || TREE_CODE (ctx) == UNION_TYPE)
21601 : 13821 : && DECL_LANG_SPECIFIC (TYPE_NAME (ctx))
21602 : 89630 : && DECL_MODULE_IMPORT_P (TYPE_NAME (ctx)))
21603 : : {
21604 : : /* This entity's context is from an import. We may need to
21605 : : record this entity to make sure we emit it in the CMI.
21606 : : Template specializations are in the template hash tables,
21607 : : so we don't need to record them here as well. */
21608 : 12 : int use_tpl = -1;
21609 : 12 : tree ti = node_template_info (decl, use_tpl);
21610 : 12 : if (use_tpl <= 0)
21611 : : {
21612 : 12 : if (ti)
21613 : : {
21614 : 3 : gcc_checking_assert (!use_tpl);
21615 : : /* Get to the TEMPLATE_DECL. */
21616 : 3 : decl = TI_TEMPLATE (ti);
21617 : : }
21618 : :
21619 : : /* Record it on the class_members list. */
21620 : 12 : vec_safe_push (class_members, decl);
21621 : : }
21622 : : }
21623 : : }
21624 : 118517 : }
21625 : :
21626 : : /* Also remember DECL if it's a newly declared class template partial
21627 : : specialization, because these are not necessarily added to the
21628 : : instantiation tables. */
21629 : :
21630 : : void
21631 : 7163872 : set_defining_module_for_partial_spec (tree decl)
21632 : : {
21633 : 7163872 : if (module_maybe_has_cmi_p ()
21634 : 18270 : && DECL_IMPLICIT_TYPEDEF_P (decl)
21635 : 7179457 : && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
21636 : 15585 : vec_safe_push (partial_specializations, decl);
21637 : 7163872 : }
21638 : :
21639 : : void
21640 : 273809510 : set_originating_module (tree decl, bool friend_p ATTRIBUTE_UNUSED)
21641 : : {
21642 : 273809510 : set_instantiating_module (decl);
21643 : :
21644 : 273809510 : if (!DECL_NAMESPACE_SCOPE_P (decl))
21645 : : return;
21646 : :
21647 : 167494149 : gcc_checking_assert (friend_p || decl == get_originating_module_decl (decl));
21648 : :
21649 : 167494149 : if (module_attach_p ())
21650 : : {
21651 : 4210 : retrofit_lang_decl (decl);
21652 : 4210 : DECL_MODULE_ATTACH_P (decl) = true;
21653 : : }
21654 : :
21655 : : /* It is ill-formed to export a declaration with internal linkage. However,
21656 : : at the point this function is called we don't yet always know whether this
21657 : : declaration has internal linkage; instead we defer this check for callers
21658 : : to do once visibility has been determined. */
21659 : 167494149 : if (module_exporting_p ())
21660 : 197857 : DECL_MODULE_EXPORT_P (decl) = true;
21661 : : }
21662 : :
21663 : : /* Checks whether DECL within a module unit has valid linkage for its kind.
21664 : : Must be called after visibility for DECL has been finalised. */
21665 : :
21666 : : void
21667 : 337081128 : check_module_decl_linkage (tree decl)
21668 : : {
21669 : 337081128 : if (!module_has_cmi_p ())
21670 : : return;
21671 : :
21672 : : /* A header unit shall not contain a definition of a non-inline function
21673 : : or variable (not template) whose name has external linkage. */
21674 : 457241 : if (header_module_p ()
21675 : 408594 : && !processing_template_decl
21676 : 136184 : && ((TREE_CODE (decl) == FUNCTION_DECL
21677 : 79901 : && !DECL_DECLARED_INLINE_P (decl))
21678 : 104574 : || (TREE_CODE (decl) == VAR_DECL
21679 : 31519 : && !DECL_INLINE_VAR_P (decl)))
21680 : 39771 : && decl_defined_p (decl)
21681 : 3802 : && !(DECL_LANG_SPECIFIC (decl)
21682 : 3802 : && DECL_TEMPLATE_INSTANTIATION (decl))
21683 : 461043 : && decl_linkage (decl) == lk_external)
21684 : 60 : error_at (DECL_SOURCE_LOCATION (decl),
21685 : : "external linkage definition of %qD in header module must "
21686 : : "be declared %<inline%>", decl);
21687 : :
21688 : : /* An internal-linkage declaration cannot be generally be exported.
21689 : : But it's OK to export any declaration from a header unit, including
21690 : : internal linkage declarations. */
21691 : 457241 : if (!header_module_p () && DECL_MODULE_EXPORT_P (decl))
21692 : : {
21693 : : /* Let's additionally treat any exported declaration within an
21694 : : internal namespace as exporting a declaration with internal
21695 : : linkage, as this would also implicitly export the internal
21696 : : linkage namespace. */
21697 : 2006 : if (decl_internal_context_p (decl))
21698 : : {
21699 : 50 : error_at (DECL_SOURCE_LOCATION (decl),
21700 : : "exporting declaration %qD declared in unnamed namespace",
21701 : : decl);
21702 : 50 : DECL_MODULE_EXPORT_P (decl) = false;
21703 : : }
21704 : 1956 : else if (decl_linkage (decl) == lk_internal)
21705 : : {
21706 : 17 : error_at (DECL_SOURCE_LOCATION (decl),
21707 : : "exporting declaration %qD with internal linkage", decl);
21708 : 17 : DECL_MODULE_EXPORT_P (decl) = false;
21709 : : }
21710 : : }
21711 : : }
21712 : :
21713 : : /* DECL is keyed to CTX for odr purposes. */
21714 : :
21715 : : void
21716 : 978654 : maybe_key_decl (tree ctx, tree decl)
21717 : : {
21718 : 978654 : if (!modules_p ())
21719 : : return;
21720 : :
21721 : : /* We only need to deal with lambdas attached to var, field,
21722 : : parm, type, or concept decls. */
21723 : 6946 : if (TREE_CODE (ctx) != VAR_DECL
21724 : 6946 : && TREE_CODE (ctx) != FIELD_DECL
21725 : : && TREE_CODE (ctx) != PARM_DECL
21726 : : && TREE_CODE (ctx) != TYPE_DECL
21727 : : && TREE_CODE (ctx) != CONCEPT_DECL)
21728 : : return;
21729 : :
21730 : : /* For members, key it to the containing type to handle deduplication
21731 : : correctly. For fields, this is necessary as FIELD_DECLs have no
21732 : : dep and so would only be streamed after the lambda type, defeating
21733 : : our ability to merge them.
21734 : :
21735 : : Other class-scope key decls might depend on the type of the lambda
21736 : : but be within the same cluster; we need to ensure that we never
21737 : : first see the key decl while streaming the lambda type as merging
21738 : : would then fail when comparing the partially-streamed lambda type
21739 : : of the key decl with the existing (PR c++/122310).
21740 : :
21741 : : Perhaps sort_cluster can be adjusted to handle this better, but
21742 : : this is a simple workaround (and might down on the number of
21743 : : entries in keyed_table as a bonus). */
21744 : 536 : while (DECL_CLASS_SCOPE_P (ctx))
21745 : 200 : ctx = TYPE_NAME (DECL_CONTEXT (ctx));
21746 : :
21747 : 336 : if (!keyed_table)
21748 : 85 : keyed_table = new keyed_map_t (EXPERIMENT (1, 400));
21749 : :
21750 : 336 : auto &vec = keyed_table->get_or_insert (ctx);
21751 : 336 : if (!vec.length ())
21752 : : {
21753 : 313 : retrofit_lang_decl (ctx);
21754 : 313 : DECL_MODULE_KEYED_DECLS_P (ctx) = true;
21755 : : }
21756 : 336 : vec.safe_push (decl);
21757 : : }
21758 : :
21759 : : /* Find the scope that the lambda DECL is keyed to, if any. */
21760 : :
21761 : : static tree
21762 : 986 : get_keyed_decl_scope (tree decl)
21763 : : {
21764 : 1972 : gcc_checking_assert (LAMBDA_TYPE_P (TREE_TYPE (decl)));
21765 : 986 : tree scope = LAMBDA_TYPE_EXTRA_SCOPE (TREE_TYPE (decl));
21766 : 986 : if (!scope)
21767 : : return NULL_TREE;
21768 : :
21769 : 968 : gcc_checking_assert (TREE_CODE (scope) == VAR_DECL
21770 : : || TREE_CODE (scope) == FIELD_DECL
21771 : : || TREE_CODE (scope) == PARM_DECL
21772 : : || TREE_CODE (scope) == TYPE_DECL
21773 : : || TREE_CODE (scope) == CONCEPT_DECL);
21774 : :
21775 : 1566 : while (DECL_CLASS_SCOPE_P (scope))
21776 : 598 : scope = TYPE_NAME (DECL_CONTEXT (scope));
21777 : :
21778 : 1936 : gcc_checking_assert (DECL_LANG_SPECIFIC (scope)
21779 : : && DECL_MODULE_KEYED_DECLS_P (scope));
21780 : : return scope;
21781 : : }
21782 : :
21783 : : /* DECL is an instantiated friend that should be attached to the same
21784 : : module that ORIG is. */
21785 : :
21786 : : void
21787 : 2799599 : propagate_defining_module (tree decl, tree orig)
21788 : : {
21789 : 2799599 : if (!modules_p ())
21790 : : return;
21791 : :
21792 : 4824 : tree not_tmpl = STRIP_TEMPLATE (orig);
21793 : 9627 : if (DECL_LANG_SPECIFIC (not_tmpl) && DECL_MODULE_ATTACH_P (not_tmpl))
21794 : : {
21795 : 117 : tree inner = STRIP_TEMPLATE (decl);
21796 : 117 : retrofit_lang_decl (inner);
21797 : 117 : DECL_MODULE_ATTACH_P (inner) = true;
21798 : : }
21799 : :
21800 : 9627 : if (DECL_LANG_SPECIFIC (not_tmpl) && DECL_MODULE_IMPORT_P (not_tmpl))
21801 : : {
21802 : 267 : bool exists = imported_temploid_friends->put (decl, orig);
21803 : :
21804 : : /* We should only be called if lookup for an existing decl
21805 : : failed, in which case there shouldn't already be an entry
21806 : : in the map. */
21807 : 267 : gcc_assert (!exists);
21808 : : }
21809 : : }
21810 : :
21811 : : /* NEWDECL matched with OLDDECL, transfer defining module information
21812 : : onto OLDDECL. We've already validated attachment matches. */
21813 : :
21814 : : void
21815 : 19107829 : transfer_defining_module (tree olddecl, tree newdecl)
21816 : : {
21817 : 19107829 : if (!modules_p ())
21818 : : return;
21819 : :
21820 : 41392 : tree old_inner = STRIP_TEMPLATE (olddecl);
21821 : 41392 : tree new_inner = STRIP_TEMPLATE (newdecl);
21822 : :
21823 : 41392 : if (DECL_LANG_SPECIFIC (new_inner))
21824 : : {
21825 : 41304 : gcc_checking_assert (DECL_LANG_SPECIFIC (old_inner));
21826 : 41304 : if (DECL_MODULE_PURVIEW_P (new_inner))
21827 : 19767 : DECL_MODULE_PURVIEW_P (old_inner) = true;
21828 : 41304 : if (!DECL_MODULE_IMPORT_P (new_inner))
21829 : 41304 : DECL_MODULE_IMPORT_P (old_inner) = false;
21830 : : }
21831 : :
21832 : 41392 : if (tree *p = imported_temploid_friends->get (newdecl))
21833 : : {
21834 : 58 : tree orig = *p;
21835 : 58 : tree &slot = imported_temploid_friends->get_or_insert (olddecl);
21836 : 58 : if (!slot)
21837 : 41 : slot = orig;
21838 : 17 : else if (slot != orig)
21839 : : /* This can happen when multiple classes declare the same
21840 : : friend function (e.g. g++.dg/modules/tpl-friend-4);
21841 : : make sure we at least attach to the same module. */
21842 : 3 : gcc_checking_assert (get_originating_module (slot)
21843 : : == get_originating_module (orig));
21844 : : }
21845 : : }
21846 : :
21847 : : /* DECL is being freed, clear data we don't need anymore. */
21848 : :
21849 : : void
21850 : 29812 : remove_defining_module (tree decl)
21851 : : {
21852 : 29812 : if (!modules_p ())
21853 : : return;
21854 : :
21855 : 29812 : if (imported_temploid_friends)
21856 : 29812 : imported_temploid_friends->remove (decl);
21857 : : }
21858 : :
21859 : : /* Create the flat name string. It is simplest to have it handy. */
21860 : :
21861 : : void
21862 : 5892 : module_state::set_flatname ()
21863 : : {
21864 : 5892 : gcc_checking_assert (!flatname);
21865 : 5892 : if (parent)
21866 : : {
21867 : 613 : auto_vec<tree,5> ids;
21868 : 613 : size_t len = 0;
21869 : 613 : char const *primary = NULL;
21870 : 613 : size_t pfx_len = 0;
21871 : :
21872 : 613 : for (module_state *probe = this;
21873 : 1519 : probe;
21874 : 906 : probe = probe->parent)
21875 : 1358 : if (is_partition () && !probe->is_partition ())
21876 : : {
21877 : 452 : primary = probe->get_flatname ();
21878 : 452 : pfx_len = strlen (primary);
21879 : 452 : break;
21880 : : }
21881 : : else
21882 : : {
21883 : 906 : ids.safe_push (probe->name);
21884 : 906 : len += IDENTIFIER_LENGTH (probe->name) + 1;
21885 : : }
21886 : :
21887 : 613 : char *flat = XNEWVEC (char, pfx_len + len + is_partition ());
21888 : 613 : flatname = flat;
21889 : :
21890 : 613 : if (primary)
21891 : : {
21892 : 452 : memcpy (flat, primary, pfx_len);
21893 : 452 : flat += pfx_len;
21894 : 452 : *flat++ = ':';
21895 : : }
21896 : :
21897 : 1519 : for (unsigned len = 0; ids.length ();)
21898 : : {
21899 : 906 : if (len)
21900 : 293 : flat[len++] = '.';
21901 : 906 : tree elt = ids.pop ();
21902 : 906 : unsigned l = IDENTIFIER_LENGTH (elt);
21903 : 906 : memcpy (flat + len, IDENTIFIER_POINTER (elt), l + 1);
21904 : 906 : len += l;
21905 : : }
21906 : 613 : }
21907 : 5279 : else if (is_header ())
21908 : 1861 : flatname = TREE_STRING_POINTER (name);
21909 : : else
21910 : 3418 : flatname = IDENTIFIER_POINTER (name);
21911 : 5892 : }
21912 : :
21913 : : /* Open the GCM file and prepare to read. Return whether that was
21914 : : successful. */
21915 : :
21916 : : bool
21917 : 2851 : module_state::open_slurp (cpp_reader *reader)
21918 : : {
21919 : 2851 : if (slurp)
21920 : : return true;
21921 : :
21922 : 2807 : if (lazy_open >= lazy_limit)
21923 : 9 : freeze_an_elf ();
21924 : :
21925 : 2807 : int fd = -1;
21926 : 2807 : int e = ENOENT;
21927 : 2807 : if (filename)
21928 : : {
21929 : 2807 : const char *file = maybe_add_cmi_prefix (filename);
21930 : 3297 : dump () && dump ("CMI is %s", file);
21931 : 2807 : if (note_module_cmi_yes || inform_cmi_p)
21932 : 12 : inform (loc, "reading CMI %qs", file);
21933 : : /* Add the CMI file to the dependency tracking. */
21934 : 2807 : if (cpp_get_deps (reader))
21935 : 12 : deps_add_dep (cpp_get_deps (reader), file);
21936 : 2807 : fd = open (file, O_RDONLY | O_CLOEXEC | O_BINARY);
21937 : 2807 : e = errno;
21938 : : }
21939 : :
21940 : 2807 : gcc_checking_assert (!slurp);
21941 : 5593 : slurp = new slurping (new elf_in (fd, e));
21942 : :
21943 : 2807 : bool ok = from ()->begin (loc);
21944 : 2807 : if (ok)
21945 : : {
21946 : 2786 : lazy_open++;
21947 : 2786 : slurp->lru = ++lazy_lru;
21948 : : }
21949 : : return ok;
21950 : : }
21951 : :
21952 : : /* Return whether importing this GCM would work without an error in
21953 : : read_config. */
21954 : :
21955 : : bool
21956 : 47 : module_state::check_importable (cpp_reader *reader)
21957 : : {
21958 : 47 : if (loadedness > ML_CONFIG)
21959 : : return true;
21960 : 44 : if (!open_slurp (reader))
21961 : : return false;
21962 : 44 : module_state_config config;
21963 : 44 : return read_config (config, /*complain*/false);
21964 : : }
21965 : :
21966 : : /* Read the CMI file for a module. */
21967 : :
21968 : : bool
21969 : 2807 : module_state::do_import (cpp_reader *reader, bool outermost)
21970 : : {
21971 : 2807 : gcc_assert (global_namespace == current_scope () && loadedness == ML_NONE);
21972 : :
21973 : : /* If this TU is a partition of the module we're importing,
21974 : : that module is the primary module interface. */
21975 : 2807 : if (this_module ()->is_partition ()
21976 : 2852 : && this == get_primary (this_module ()))
21977 : 6 : module_p = true;
21978 : :
21979 : 2807 : loc = linemap_module_loc (line_table, loc, get_flatname ());
21980 : :
21981 : 2807 : bool ok = open_slurp (reader);
21982 : 2807 : if (!from ()->get_error ())
21983 : : {
21984 : 2786 : announce ("importing");
21985 : 2786 : loadedness = ML_CONFIG;
21986 : 2786 : ok = read_initial (reader);
21987 : : }
21988 : :
21989 : 2807 : gcc_assert (slurp->current == ~0u);
21990 : :
21991 : 2807 : return check_read (outermost, ok);
21992 : : }
21993 : :
21994 : : /* Attempt to increase the file descriptor limit. */
21995 : :
21996 : : static bool
21997 : 4600 : try_increase_lazy (unsigned want)
21998 : : {
21999 : 4600 : gcc_checking_assert (lazy_open >= lazy_limit);
22000 : :
22001 : : /* If we're increasing, saturate at hard limit. */
22002 : 4600 : if (want > lazy_hard_limit && lazy_limit < lazy_hard_limit)
22003 : 4600 : want = lazy_hard_limit;
22004 : :
22005 : : #if HAVE_SETRLIMIT
22006 : 4600 : if ((!lazy_limit || !param_lazy_modules)
22007 : 4588 : && lazy_hard_limit
22008 : 4588 : && want <= lazy_hard_limit)
22009 : : {
22010 : 4588 : struct rlimit rlimit;
22011 : 4588 : rlimit.rlim_cur = want + LAZY_HEADROOM;
22012 : 4588 : rlimit.rlim_max = lazy_hard_limit + LAZY_HEADROOM;
22013 : 4588 : if (!setrlimit (RLIMIT_NOFILE, &rlimit))
22014 : 4588 : lazy_limit = want;
22015 : : }
22016 : : #endif
22017 : :
22018 : 4600 : return lazy_open < lazy_limit;
22019 : : }
22020 : :
22021 : : /* Pick a victim module to freeze its reader. */
22022 : :
22023 : : void
22024 : 12 : module_state::freeze_an_elf ()
22025 : : {
22026 : 12 : if (try_increase_lazy (lazy_open * 2))
22027 : : return;
22028 : :
22029 : 12 : module_state *victim = NULL;
22030 : 12 : for (unsigned ix = modules->length (); ix--;)
22031 : : {
22032 : 30 : module_state *candidate = (*modules)[ix];
22033 : 30 : if (candidate && candidate->slurp && candidate->slurp->lru
22034 : 60 : && candidate->from ()->is_freezable ()
22035 : 39 : && (!victim || victim->slurp->lru > candidate->slurp->lru))
22036 : : victim = candidate;
22037 : : }
22038 : :
22039 : 12 : if (victim)
22040 : : {
22041 : 18 : dump () && dump ("Freezing '%s'", victim->filename);
22042 : 9 : if (victim->slurp->macro_defs.size)
22043 : : /* Save the macro definitions to a buffer. */
22044 : 0 : victim->from ()->preserve (victim->slurp->macro_defs);
22045 : 9 : if (victim->slurp->macro_tbl.size)
22046 : : /* Save the macro definitions to a buffer. */
22047 : 0 : victim->from ()->preserve (victim->slurp->macro_tbl);
22048 : 9 : victim->from ()->freeze ();
22049 : 9 : lazy_open--;
22050 : : }
22051 : : else
22052 : 3 : dump () && dump ("No module available for freezing");
22053 : : }
22054 : :
22055 : : /* Load the lazy slot *MSLOT, INDEX'th slot of the module. */
22056 : :
22057 : : bool
22058 : 25840 : module_state::lazy_load (unsigned index, binding_slot *mslot)
22059 : : {
22060 : 25840 : unsigned n = dump.push (this);
22061 : :
22062 : 25840 : gcc_checking_assert (function_depth);
22063 : :
22064 : 25840 : unsigned cookie = mslot->get_lazy ();
22065 : 25840 : unsigned snum = cookie >> 2;
22066 : 26175 : dump () && dump ("Loading entity %M[%u] section:%u", this, index, snum);
22067 : :
22068 : 25840 : bool ok = load_section (snum, mslot);
22069 : :
22070 : 25840 : dump.pop (n);
22071 : :
22072 : 25840 : return ok;
22073 : : }
22074 : :
22075 : : /* Load MOD's binding for NS::ID into *MSLOT. *MSLOT contains the
22076 : : lazy cookie. OUTER is true if this is the outermost lazy, (used
22077 : : for diagnostics). */
22078 : :
22079 : : void
22080 : 2837 : lazy_load_binding (unsigned mod, tree ns, tree id, binding_slot *mslot)
22081 : : {
22082 : 2837 : int count = errorcount + warningcount;
22083 : :
22084 : 2837 : bool timer_running = timevar_cond_start (TV_MODULE_IMPORT);
22085 : :
22086 : : /* Make sure lazy loading from a template context behaves as if
22087 : : from a non-template context. */
22088 : 2837 : processing_template_decl_sentinel ptds;
22089 : :
22090 : : /* Stop GC happening, even in outermost loads (because our caller
22091 : : could well be building up a lookup set). */
22092 : 2837 : function_depth++;
22093 : :
22094 : 2837 : gcc_checking_assert (mod);
22095 : 2837 : module_state *module = (*modules)[mod];
22096 : 2837 : unsigned n = dump.push (module);
22097 : :
22098 : 2837 : unsigned snum = mslot->get_lazy ();
22099 : 3144 : dump () && dump ("Lazily binding %P@%N section:%u", ns, id,
22100 : : module->name, snum);
22101 : :
22102 : 2837 : bool ok = !recursive_lazy (snum);
22103 : 2837 : if (ok)
22104 : : {
22105 : 2837 : ok = module->load_section (snum, mslot);
22106 : 2837 : lazy_snum = 0;
22107 : 2837 : post_load_processing ();
22108 : : }
22109 : :
22110 : 2837 : dump.pop (n);
22111 : :
22112 : 2837 : function_depth--;
22113 : :
22114 : 2837 : timevar_cond_stop (TV_MODULE_IMPORT, timer_running);
22115 : :
22116 : 2837 : if (!ok)
22117 : 0 : fatal_error (input_location,
22118 : 0 : module->is_header ()
22119 : : ? G_("failed to load binding %<%E%s%E%>")
22120 : : : G_("failed to load binding %<%E%s%E@%s%>"),
22121 : 0 : ns, &"::"[ns == global_namespace ? 2 : 0], id,
22122 : : module->get_flatname ());
22123 : :
22124 : 2837 : if (count != errorcount + warningcount)
22125 : 27 : inform (input_location,
22126 : 27 : module->is_header ()
22127 : : ? G_("during load of binding %<%E%s%E%>")
22128 : : : G_("during load of binding %<%E%s%E@%s%>"),
22129 : 27 : ns, &"::"[ns == global_namespace ? 2 : 0], id,
22130 : : module->get_flatname ());
22131 : 2837 : }
22132 : :
22133 : : /* Load any pending entities keyed to the top-key of DECL. */
22134 : :
22135 : : void
22136 : 25202398 : lazy_load_pendings (tree decl)
22137 : : {
22138 : : /* Make sure lazy loading from a template context behaves as if
22139 : : from a non-template context. */
22140 : 25202398 : processing_template_decl_sentinel ptds;
22141 : :
22142 : 25202398 : tree key_decl;
22143 : 25202398 : pending_key key;
22144 : 25202398 : key.ns = find_pending_key (decl, &key_decl);
22145 : 25202398 : key.id = DECL_NAME (key_decl);
22146 : :
22147 : 25202398 : auto *pending_vec = pending_table ? pending_table->get (key) : nullptr;
22148 : 25197806 : if (!pending_vec)
22149 : 25197601 : return;
22150 : :
22151 : 4797 : int count = errorcount + warningcount;
22152 : :
22153 : 4797 : bool timer_running = timevar_cond_start (TV_MODULE_IMPORT);
22154 : 4797 : bool ok = !recursive_lazy ();
22155 : 4797 : if (ok)
22156 : : {
22157 : 4797 : function_depth++; /* Prevent GC */
22158 : 4797 : unsigned n = dump.push (NULL);
22159 : 5239 : dump () && dump ("Reading %u pending entities keyed to %P",
22160 : : pending_vec->length (), key.ns, key.id);
22161 : 4797 : for (unsigned ix = pending_vec->length (); ix--;)
22162 : : {
22163 : 50223 : unsigned index = (*pending_vec)[ix];
22164 : 50223 : binding_slot *slot = &(*entity_ary)[index];
22165 : :
22166 : 50223 : if (slot->is_lazy ())
22167 : : {
22168 : 2614 : module_state *import = import_entity_module (index);
22169 : 2614 : if (!import->lazy_load (index - import->entity_lwm, slot))
22170 : 50223 : ok = false;
22171 : : }
22172 : 102629 : else if (dump ())
22173 : : {
22174 : 323 : module_state *import = import_entity_module (index);
22175 : 323 : dump () && dump ("Entity %M[%u] already loaded",
22176 : 323 : import, index - import->entity_lwm);
22177 : : }
22178 : : }
22179 : :
22180 : 4797 : pending_table->remove (key);
22181 : 4797 : dump.pop (n);
22182 : 4797 : lazy_snum = 0;
22183 : 4797 : post_load_processing ();
22184 : 4797 : function_depth--;
22185 : : }
22186 : :
22187 : 4797 : timevar_cond_stop (TV_MODULE_IMPORT, timer_running);
22188 : :
22189 : 4797 : if (!ok)
22190 : 0 : fatal_error (input_location, "failed to load pendings for %<%E%s%E%>",
22191 : 0 : key.ns, &"::"[key.ns == global_namespace ? 2 : 0], key.id);
22192 : :
22193 : 4797 : if (count != errorcount + warningcount)
22194 : 0 : inform (input_location, "during load of pendings for %<%E%s%E%>",
22195 : 0 : key.ns, &"::"[key.ns == global_namespace ? 2 : 0], key.id);
22196 : 25202398 : }
22197 : :
22198 : : static void
22199 : 2557 : direct_import (module_state *import, cpp_reader *reader)
22200 : : {
22201 : 2557 : timevar_start (TV_MODULE_IMPORT);
22202 : 2557 : unsigned n = dump.push (import);
22203 : :
22204 : 2557 : gcc_checking_assert (import->is_direct () && import->has_location ());
22205 : 2557 : if (import->loadedness == ML_NONE)
22206 : 1643 : if (!import->do_import (reader, true))
22207 : 0 : gcc_unreachable ();
22208 : :
22209 : 2520 : this_module ()->set_import (import, import->exported_p);
22210 : :
22211 : 2520 : if (import->loadedness < ML_LANGUAGE)
22212 : : {
22213 : 2441 : if (!keyed_table)
22214 : 2147 : keyed_table = new keyed_map_t (EXPERIMENT (1, 400));
22215 : 2441 : import->read_language (true);
22216 : : }
22217 : :
22218 : 2520 : dump.pop (n);
22219 : 2520 : timevar_stop (TV_MODULE_IMPORT);
22220 : 2520 : }
22221 : :
22222 : : /* Import module IMPORT. */
22223 : :
22224 : : void
22225 : 2335 : import_module (module_state *import, location_t from_loc, bool exporting_p,
22226 : : tree, cpp_reader *reader)
22227 : : {
22228 : : /* A non-partition implementation unit has no name. */
22229 : 2335 : if (!this_module ()->name && this_module ()->parent == import)
22230 : : {
22231 : 3 : auto_diagnostic_group d;
22232 : 3 : error_at (from_loc, "import of %qs within its own implementation unit",
22233 : : import->get_flatname());
22234 : 3 : inform (import->loc, "module declared here");
22235 : 3 : return;
22236 : 3 : }
22237 : :
22238 : 2332 : if (!import->check_circular_import (from_loc))
22239 : : return;
22240 : :
22241 : 2326 : if (!import->is_header () && current_lang_depth ())
22242 : : /* Only header units should appear inside language
22243 : : specifications. The std doesn't specify this, but I think
22244 : : that's an error in resolving US 033, because language linkage
22245 : : is also our escape clause to getting things into the global
22246 : : module, so we don't want to confuse things by having to think
22247 : : about whether 'extern "C++" { import foo; }' puts foo's
22248 : : contents into the global module all of a sudden. */
22249 : 6 : warning (0, "import of named module %qs inside language-linkage block",
22250 : : import->get_flatname ());
22251 : :
22252 : 2326 : if (exporting_p || module_exporting_p ())
22253 : 280 : import->exported_p = true;
22254 : :
22255 : 2326 : if (import->loadedness != ML_NONE)
22256 : : {
22257 : 911 : from_loc = ordinary_loc_of (line_table, from_loc);
22258 : 911 : linemap_module_reparent (line_table, import->loc, from_loc);
22259 : : }
22260 : :
22261 : 2326 : gcc_checking_assert (import->is_direct () && import->has_location ());
22262 : :
22263 : 2326 : direct_import (import, reader);
22264 : : }
22265 : :
22266 : : /* Declare the name of the current module to be NAME. EXPORTING_p is
22267 : : true if this TU is the exporting module unit. */
22268 : :
22269 : : void
22270 : 2925 : declare_module (module_state *module, location_t from_loc, bool exporting_p,
22271 : : tree, cpp_reader *reader)
22272 : : {
22273 : 2925 : gcc_assert (global_namespace == current_scope ());
22274 : :
22275 : 2925 : module_state *current = this_module ();
22276 : 2925 : if (module_purview_p () || module->loadedness > ML_CONFIG)
22277 : : {
22278 : 6 : auto_diagnostic_group d;
22279 : 12 : error_at (from_loc, module_purview_p ()
22280 : : ? G_("module already declared")
22281 : : : G_("module already imported"));
22282 : 6 : if (module_purview_p ())
22283 : 0 : module = current;
22284 : 12 : inform (module->loc, module_purview_p ()
22285 : : ? G_("module %qs declared here")
22286 : : : G_("module %qs imported here"),
22287 : : module->get_flatname ());
22288 : 6 : return;
22289 : 6 : }
22290 : :
22291 : 2919 : gcc_checking_assert (module->is_module ());
22292 : 2919 : gcc_checking_assert (module->is_direct () && module->has_location ());
22293 : :
22294 : : /* Yer a module, 'arry. */
22295 : 2919 : module_kind = module->is_header () ? MK_HEADER : MK_NAMED | MK_ATTACH;
22296 : :
22297 : : // Even in header units, we consider the decls to be purview
22298 : 2919 : module_kind |= MK_PURVIEW;
22299 : :
22300 : 2919 : if (module->is_partition ())
22301 : 178 : module_kind |= MK_PARTITION;
22302 : 2919 : if (exporting_p)
22303 : : {
22304 : 2629 : module->interface_p = true;
22305 : 2629 : module_kind |= MK_INTERFACE;
22306 : : }
22307 : :
22308 : 2919 : if (module_has_cmi_p ())
22309 : : {
22310 : : /* Copy the importing information we may have already done. We
22311 : : do not need to separate out the imports that only happen in
22312 : : the GMF, inspite of what the literal wording of the std
22313 : : might imply. See p2191, the core list had a discussion
22314 : : where the module implementors agreed that the GMF of a named
22315 : : module is invisible to importers. */
22316 : 2688 : module->imports = current->imports;
22317 : :
22318 : 2688 : module->mod = 0;
22319 : 2688 : (*modules)[0] = module;
22320 : : }
22321 : : else
22322 : : {
22323 : 231 : module->interface_p = true;
22324 : 231 : current->parent = module; /* So mangler knows module identity. */
22325 : 231 : direct_import (module, reader);
22326 : : }
22327 : : }
22328 : :
22329 : : /* Return true IFF we must emit a module global initializer function
22330 : : (which will be called by importers' init code). */
22331 : :
22332 : : bool
22333 : 102026 : module_global_init_needed ()
22334 : : {
22335 : 102026 : return module_has_cmi_p () && !header_module_p ();
22336 : : }
22337 : :
22338 : : /* Calculate which, if any, import initializers need calling. */
22339 : :
22340 : : bool
22341 : 94979 : module_determine_import_inits ()
22342 : : {
22343 : 94979 : if (!modules || header_module_p ())
22344 : : return false;
22345 : :
22346 : : /* Prune active_init_p. We need the same bitmap allocation
22347 : : scheme as for the imports member. */
22348 : 3533 : function_depth++; /* Disable GC. */
22349 : 3533 : bitmap covered_imports (BITMAP_GGC_ALLOC ());
22350 : :
22351 : 3533 : bool any = false;
22352 : :
22353 : : /* Because indirect imports are before their direct import, and
22354 : : we're scanning the array backwards, we only need one pass! */
22355 : 6156 : for (unsigned ix = modules->length (); --ix;)
22356 : : {
22357 : 2623 : module_state *import = (*modules)[ix];
22358 : :
22359 : 2623 : if (!import->active_init_p)
22360 : : ;
22361 : 43 : else if (bitmap_bit_p (covered_imports, ix))
22362 : 6 : import->active_init_p = false;
22363 : : else
22364 : : {
22365 : : /* Everything this imports is therefore handled by its
22366 : : initializer, so doesn't need initializing by us. */
22367 : 37 : bitmap_ior_into (covered_imports, import->imports);
22368 : 37 : any = true;
22369 : : }
22370 : : }
22371 : 3533 : function_depth--;
22372 : :
22373 : 3533 : return any;
22374 : : }
22375 : :
22376 : : /* Emit calls to each direct import's global initializer. Including
22377 : : direct imports of directly imported header units. The initializers
22378 : : of (static) entities in header units will be called by their
22379 : : importing modules (for the instance contained within that), or by
22380 : : the current TU (for the instances we've brought in). Of course
22381 : : such header unit behaviour is evil, but iostream went through that
22382 : : door some time ago. */
22383 : :
22384 : : void
22385 : 37 : module_add_import_initializers ()
22386 : : {
22387 : 37 : if (!modules || header_module_p ())
22388 : 0 : return;
22389 : :
22390 : 37 : tree fntype = build_function_type (void_type_node, void_list_node);
22391 : 37 : releasing_vec args; // There are no args
22392 : :
22393 : 83 : for (unsigned ix = modules->length (); --ix;)
22394 : : {
22395 : 46 : module_state *import = (*modules)[ix];
22396 : 46 : if (import->active_init_p)
22397 : : {
22398 : 37 : tree name = mangle_module_global_init (ix);
22399 : 37 : tree fndecl = build_lang_decl (FUNCTION_DECL, name, fntype);
22400 : :
22401 : 37 : DECL_CONTEXT (fndecl) = FROB_CONTEXT (global_namespace);
22402 : 37 : SET_DECL_ASSEMBLER_NAME (fndecl, name);
22403 : 37 : TREE_PUBLIC (fndecl) = true;
22404 : 37 : determine_visibility (fndecl);
22405 : :
22406 : 37 : tree call = cp_build_function_call_vec (fndecl, &args,
22407 : : tf_warning_or_error);
22408 : 37 : finish_expr_stmt (call);
22409 : : }
22410 : : }
22411 : 37 : }
22412 : :
22413 : : /* NAME & LEN are a preprocessed header name, possibly including the
22414 : : surrounding "" or <> characters. Return the raw string name of the
22415 : : module to which it refers. This will be an absolute path, or begin
22416 : : with ./, so it is immediately distinguishable from a (non-header
22417 : : unit) module name. If READER is non-null, ask the preprocessor to
22418 : : locate the header to which it refers using the appropriate include
22419 : : path. Note that we do never do \ processing of the string, as that
22420 : : matches the preprocessor's behaviour. */
22421 : :
22422 : : static const char *
22423 : 23488 : canonicalize_header_name (cpp_reader *reader, location_t loc, bool unquoted,
22424 : : const char *str, size_t &len_r)
22425 : : {
22426 : 23488 : size_t len = len_r;
22427 : 23488 : static char *buf = 0;
22428 : 23488 : static size_t alloc = 0;
22429 : :
22430 : 23488 : if (!unquoted)
22431 : : {
22432 : 4 : gcc_checking_assert (len >= 2
22433 : : && ((reader && str[0] == '<' && str[len-1] == '>')
22434 : : || (str[0] == '"' && str[len-1] == '"')));
22435 : 4 : str += 1;
22436 : 4 : len -= 2;
22437 : : }
22438 : :
22439 : 23488 : if (reader)
22440 : : {
22441 : 4 : gcc_assert (!unquoted);
22442 : :
22443 : 4 : if (len >= alloc)
22444 : : {
22445 : 4 : alloc = len + 1;
22446 : 4 : buf = XRESIZEVEC (char, buf, alloc);
22447 : : }
22448 : 4 : memcpy (buf, str, len);
22449 : 4 : buf[len] = 0;
22450 : :
22451 : 8 : if (const char *hdr
22452 : 4 : = cpp_probe_header_unit (reader, buf, str[-1] == '<', loc))
22453 : : {
22454 : 4 : len = strlen (hdr);
22455 : 4 : str = hdr;
22456 : : }
22457 : : else
22458 : 0 : str = buf;
22459 : : }
22460 : :
22461 : 23488 : if (!(str[0] == '.' ? IS_DIR_SEPARATOR (str[1]) : IS_ABSOLUTE_PATH (str)))
22462 : : {
22463 : : /* Prepend './' */
22464 : 9 : if (len + 3 > alloc)
22465 : : {
22466 : 9 : alloc = len + 3;
22467 : 9 : buf = XRESIZEVEC (char, buf, alloc);
22468 : : }
22469 : :
22470 : 9 : buf[0] = '.';
22471 : 9 : buf[1] = DIR_SEPARATOR;
22472 : 9 : memmove (buf + 2, str, len);
22473 : 9 : len += 2;
22474 : 9 : buf[len] = 0;
22475 : 9 : str = buf;
22476 : : }
22477 : :
22478 : 23488 : len_r = len;
22479 : 23488 : return str;
22480 : : }
22481 : :
22482 : : /* Set the CMI name from a cody packet. Issue an error if
22483 : : ill-formed. */
22484 : :
22485 : 5370 : void module_state::set_filename (const Cody::Packet &packet)
22486 : : {
22487 : 5370 : if (packet.GetCode () == Cody::Client::PC_PATHNAME)
22488 : : {
22489 : : /* If we've seen this import before we better have the same CMI. */
22490 : 5367 : const std::string &path = packet.GetString ();
22491 : 5367 : if (!filename)
22492 : 5364 : filename = xstrdup (packet.GetString ().c_str ());
22493 : 3 : else if (filename != path)
22494 : 0 : error_at (loc, "mismatching compiled module interface: "
22495 : : "had %qs, got %qs", filename, path.c_str ());
22496 : : }
22497 : : else
22498 : : {
22499 : 3 : gcc_checking_assert (packet.GetCode () == Cody::Client::PC_ERROR);
22500 : 3 : fatal_error (loc, "unknown compiled module interface: %s",
22501 : 3 : packet.GetString ().c_str ());
22502 : : }
22503 : 5367 : }
22504 : :
22505 : : /* Figure out whether to treat HEADER as an include or an import. */
22506 : :
22507 : : static char *
22508 : 22584 : maybe_translate_include (cpp_reader *reader, line_maps *lmaps, location_t loc,
22509 : : const char *path)
22510 : : {
22511 : 22584 : if (!modules_p ())
22512 : : {
22513 : : /* Turn off. */
22514 : 0 : cpp_get_callbacks (reader)->translate_include = NULL;
22515 : 0 : return nullptr;
22516 : : }
22517 : :
22518 : 22584 : dump.push (NULL);
22519 : :
22520 : 24496 : dump () && dump ("Checking include translation '%s'", path);
22521 : 22584 : auto *mapper = get_mapper (cpp_main_loc (reader), cpp_get_deps (reader));
22522 : :
22523 : 22584 : size_t len = strlen (path);
22524 : 22584 : path = canonicalize_header_name (NULL, loc, true, path, len);
22525 : 22584 : auto packet = mapper->IncludeTranslate (path, Cody::Flags::None, len);
22526 : :
22527 : 22584 : enum class xlate_kind {
22528 : : unknown, text, import, invalid
22529 : 22584 : } translate = xlate_kind::unknown;
22530 : :
22531 : 22584 : if (packet.GetCode () == Cody::Client::PC_BOOL)
22532 : 22537 : translate = packet.GetInteger () ? xlate_kind::text : xlate_kind::unknown;
22533 : 47 : else if (packet.GetCode () == Cody::Client::PC_PATHNAME)
22534 : : {
22535 : : /* Record the CMI name for when we do the import.
22536 : : We may already know about this import, but libcpp doesn't yet. */
22537 : 47 : module_state *import = get_module (build_string (len, path));
22538 : 47 : import->set_filename (packet);
22539 : 47 : if (import->check_importable (reader))
22540 : : translate = xlate_kind::import;
22541 : : else
22542 : 0 : translate = xlate_kind::invalid;
22543 : : }
22544 : : else
22545 : : {
22546 : 0 : gcc_checking_assert (packet.GetCode () == Cody::Client::PC_ERROR);
22547 : 0 : error_at (loc, "cannot determine %<#include%> translation of %s: %s",
22548 : 0 : path, packet.GetString ().c_str ());
22549 : : }
22550 : :
22551 : 22584 : bool note = (translate == xlate_kind::invalid);
22552 : 22584 : if (note_include_translate_yes && translate == xlate_kind::import)
22553 : : note = true;
22554 : 22581 : else if (note_include_translate_no && translate == xlate_kind::unknown)
22555 : : note = true;
22556 : 22575 : else if (note_includes)
22557 : : /* We do not expect the note_includes vector to be large, so O(N)
22558 : : iteration. */
22559 : 410 : for (unsigned ix = note_includes->length (); !note && ix--;)
22560 : 205 : if (!strcmp ((*note_includes)[ix], path))
22561 : 1 : note = true;
22562 : :
22563 : 22575 : if (note)
22564 : 16 : inform (loc, translate == xlate_kind::import
22565 : : ? G_("include %qs translated to import")
22566 : : : translate == xlate_kind::invalid
22567 : 6 : ? G_("import of %qs failed, falling back to include")
22568 : : : G_("include %qs processed textually"), path);
22569 : :
22570 : 26405 : dump () && dump (translate == xlate_kind::import
22571 : : ? "Translating include to import"
22572 : : : "Keeping include as include");
22573 : 22584 : dump.pop (0);
22574 : :
22575 : 22584 : if (translate != xlate_kind::import)
22576 : : return nullptr;
22577 : :
22578 : : /* Create the translation text. */
22579 : 47 : loc = ordinary_loc_of (lmaps, loc);
22580 : 47 : const line_map_ordinary *map
22581 : 47 : = linemap_check_ordinary (linemap_lookup (lmaps, loc));
22582 : 47 : unsigned col = SOURCE_COLUMN (map, loc);
22583 : 47 : col -= (col != 0); /* Columns are 1-based. */
22584 : :
22585 : 47 : unsigned alloc = len + col + 60;
22586 : 47 : char *res = XNEWVEC (char, alloc);
22587 : :
22588 : 47 : strcpy (res, "__import");
22589 : 47 : unsigned actual = 8;
22590 : 47 : if (col > actual)
22591 : : {
22592 : : /* Pad out so the filename appears at the same position. */
22593 : 44 : memset (res + actual, ' ', col - actual);
22594 : 44 : actual = col;
22595 : : }
22596 : : /* No need to encode characters, that's not how header names are
22597 : : handled. */
22598 : 47 : actual += snprintf (res + actual, alloc - actual,
22599 : : "\"%s\" [[__translated]];\n", path);
22600 : 47 : gcc_checking_assert (actual < alloc);
22601 : :
22602 : : /* cpplib will delete the buffer. */
22603 : : return res;
22604 : 22584 : }
22605 : :
22606 : : static void
22607 : 900 : begin_header_unit (cpp_reader *reader)
22608 : : {
22609 : : /* Set the module header name from the main_input_filename. */
22610 : 900 : const char *main = main_input_filename;
22611 : 900 : size_t len = strlen (main);
22612 : 900 : main = canonicalize_header_name (NULL, 0, true, main, len);
22613 : 900 : module_state *module = get_module (build_string (len, main));
22614 : :
22615 : 900 : preprocess_module (module, cpp_main_loc (reader), false, false, true, reader);
22616 : 900 : }
22617 : :
22618 : : /* We've just properly entered the main source file. I.e. after the
22619 : : command line, builtins and forced headers. Record the line map and
22620 : : location of this map. Note we may be called more than once. The
22621 : : first call sticks. */
22622 : :
22623 : : void
22624 : 96910 : module_begin_main_file (cpp_reader *reader, line_maps *lmaps,
22625 : : const line_map_ordinary *map)
22626 : : {
22627 : 96910 : gcc_checking_assert (lmaps == line_table);
22628 : 96910 : if (modules_p () && !spans.init_p ())
22629 : : {
22630 : 4588 : unsigned n = dump.push (NULL);
22631 : 4588 : spans.init (lmaps, map);
22632 : 4588 : dump.pop (n);
22633 : 4588 : if (flag_header_unit && !cpp_get_options (reader)->preprocessed)
22634 : : {
22635 : : /* Tell the preprocessor this is an include file. */
22636 : 891 : cpp_retrofit_as_include (reader);
22637 : 891 : begin_header_unit (reader);
22638 : : }
22639 : : }
22640 : 96910 : }
22641 : :
22642 : : /* Process the pending_import queue, making sure we know the
22643 : : filenames. */
22644 : :
22645 : : static void
22646 : 5468 : name_pending_imports (cpp_reader *reader)
22647 : : {
22648 : 5468 : auto *mapper = get_mapper (cpp_main_loc (reader), cpp_get_deps (reader));
22649 : :
22650 : 5468 : if (!vec_safe_length (pending_imports))
22651 : : /* Not doing anything. */
22652 : : return;
22653 : :
22654 : 4656 : timevar_start (TV_MODULE_MAPPER);
22655 : :
22656 : 4656 : auto n = dump.push (NULL);
22657 : 5236 : dump () && dump ("Resolving direct import names");
22658 : 4656 : bool want_deps = (bool (mapper->get_flags () & Cody::Flags::NameOnly)
22659 : 4656 : || cpp_get_deps (reader));
22660 : 4656 : bool any = false;
22661 : :
22662 : 10152 : for (unsigned ix = 0; ix != pending_imports->length (); ix++)
22663 : : {
22664 : 5496 : module_state *module = (*pending_imports)[ix];
22665 : 5496 : gcc_checking_assert (module->is_direct ());
22666 : 5496 : if (!module->filename && !module->visited_p)
22667 : : {
22668 : 5389 : bool export_p = (module->is_module ()
22669 : 5389 : && (module->is_partition ()
22670 : 2831 : || module->is_exported ()));
22671 : :
22672 : 5389 : Cody::Flags flags = Cody::Flags::None;
22673 : 5389 : if (flag_preprocess_only
22674 : 5389 : && !(module->is_header () && !export_p))
22675 : : {
22676 : 129 : if (!want_deps)
22677 : 66 : continue;
22678 : : flags = Cody::Flags::NameOnly;
22679 : : }
22680 : :
22681 : 5323 : if (!any)
22682 : : {
22683 : 4586 : any = true;
22684 : 4586 : mapper->Cork ();
22685 : : }
22686 : 5323 : if (export_p)
22687 : 2739 : mapper->ModuleExport (module->get_flatname (), flags);
22688 : : else
22689 : 2584 : mapper->ModuleImport (module->get_flatname (), flags);
22690 : 5323 : module->visited_p = true;
22691 : : }
22692 : : }
22693 : :
22694 : 4656 : if (any)
22695 : : {
22696 : 4586 : auto response = mapper->Uncork ();
22697 : 4586 : auto r_iter = response.begin ();
22698 : 9997 : for (unsigned ix = 0; ix != pending_imports->length (); ix++)
22699 : : {
22700 : 5414 : module_state *module = (*pending_imports)[ix];
22701 : 5414 : if (module->visited_p)
22702 : : {
22703 : 5323 : module->visited_p = false;
22704 : 5323 : gcc_checking_assert (!module->filename);
22705 : :
22706 : 5323 : module->set_filename (*r_iter);
22707 : 5320 : ++r_iter;
22708 : : }
22709 : : }
22710 : 4583 : }
22711 : :
22712 : 4653 : dump.pop (n);
22713 : :
22714 : 4653 : timevar_stop (TV_MODULE_MAPPER);
22715 : : }
22716 : :
22717 : : /* We've just lexed a module-specific control line for MODULE. Mark
22718 : : the module as a direct import, and possibly load up its macro
22719 : : state. Returns the primary module, if this is a module
22720 : : declaration. */
22721 : : /* Perhaps we should offer a preprocessing mode where we read the
22722 : : directives from the header unit, rather than require the header's
22723 : : CMI. */
22724 : :
22725 : : module_state *
22726 : 5515 : preprocess_module (module_state *module, location_t from_loc,
22727 : : bool in_purview, bool is_import, bool is_export,
22728 : : cpp_reader *reader)
22729 : : {
22730 : 5515 : if (!is_import)
22731 : : {
22732 : 3069 : if (module->loc)
22733 : : /* It's already been mentioned, so ignore its module-ness. */
22734 : : is_import = true;
22735 : : else
22736 : : {
22737 : : /* Record it is the module. */
22738 : 3048 : module->module_p = true;
22739 : 3048 : if (is_export)
22740 : : {
22741 : 2725 : module->exported_p = true;
22742 : 2725 : module->interface_p = true;
22743 : : }
22744 : : }
22745 : : }
22746 : :
22747 : 5515 : if (module->directness < MD_DIRECT + in_purview)
22748 : : {
22749 : : /* Mark as a direct import. */
22750 : 5469 : module->directness = module_directness (MD_DIRECT + in_purview);
22751 : :
22752 : : /* Set the location to be most informative for users. */
22753 : 5469 : from_loc = ordinary_loc_of (line_table, from_loc);
22754 : 5469 : if (module->loadedness != ML_NONE)
22755 : 6 : linemap_module_reparent (line_table, module->loc, from_loc);
22756 : : else
22757 : : {
22758 : : /* Don't overwrite the location if we're importing ourselves
22759 : : after already having seen a module-declaration. */
22760 : 5463 : if (!(is_import && module->is_module ()))
22761 : 5436 : module->loc = from_loc;
22762 : 5463 : if (!module->flatname)
22763 : 5430 : module->set_flatname ();
22764 : : }
22765 : : }
22766 : :
22767 : 5515 : auto desired = ML_CONFIG;
22768 : 5515 : if (is_import
22769 : 2467 : && module->is_header ()
22770 : 6408 : && (!cpp_get_options (reader)->preprocessed
22771 : 3 : || cpp_get_options (reader)->directives_only))
22772 : : /* We need preprocessor state now. */
22773 : : desired = ML_PREPROCESSOR;
22774 : :
22775 : 5515 : if (!is_import || module->loadedness < desired)
22776 : : {
22777 : 5496 : vec_safe_push (pending_imports, module);
22778 : :
22779 : 5496 : if (desired == ML_PREPROCESSOR)
22780 : : {
22781 : 874 : unsigned n = dump.push (NULL);
22782 : :
22783 : 1073 : dump () && dump ("Reading %M preprocessor state", module);
22784 : 874 : name_pending_imports (reader);
22785 : :
22786 : : /* Preserve the state of the line-map. */
22787 : 874 : auto pre_hwm = LINEMAPS_ORDINARY_USED (line_table);
22788 : :
22789 : : /* We only need to close the span, if we're going to emit a
22790 : : CMI. But that's a little tricky -- our token scanner
22791 : : needs to be smarter -- and this isn't much state.
22792 : : Remember, we've not parsed anything at this point, so
22793 : : our module state flags are inadequate. */
22794 : 874 : spans.maybe_init ();
22795 : 874 : spans.close ();
22796 : :
22797 : 874 : timevar_start (TV_MODULE_IMPORT);
22798 : :
22799 : : /* Load the config of each pending import -- we must assign
22800 : : module numbers monotonically. */
22801 : 1933 : for (unsigned ix = 0; ix != pending_imports->length (); ix++)
22802 : : {
22803 : 1059 : auto *import = (*pending_imports)[ix];
22804 : 1235 : if (!(import->is_module ()
22805 : 176 : && (import->is_partition () || import->is_exported ()))
22806 : 889 : && import->loadedness == ML_NONE
22807 : 1948 : && (import->is_header () || !flag_preprocess_only))
22808 : : {
22809 : 880 : unsigned n = dump.push (import);
22810 : 880 : import->do_import (reader, true);
22811 : 880 : dump.pop (n);
22812 : : }
22813 : : }
22814 : 874 : vec_free (pending_imports);
22815 : :
22816 : : /* Restore the line-map state. */
22817 : 874 : spans.open (linemap_module_restore (line_table, pre_hwm));
22818 : :
22819 : : /* Now read the preprocessor state of this particular
22820 : : import. */
22821 : 874 : if (module->loadedness == ML_CONFIG
22822 : 874 : && module->read_preprocessor (true))
22823 : 871 : module->import_macros ();
22824 : :
22825 : 874 : timevar_stop (TV_MODULE_IMPORT);
22826 : :
22827 : 874 : dump.pop (n);
22828 : : }
22829 : : }
22830 : :
22831 : 5515 : return is_import ? NULL : get_primary (module);
22832 : : }
22833 : :
22834 : : /* We've completed phase-4 translation. Emit any dependency
22835 : : information for the not-yet-loaded direct imports, and fill in
22836 : : their file names. We'll have already loaded up the direct header
22837 : : unit wavefront. */
22838 : :
22839 : : void
22840 : 4594 : preprocessed_module (cpp_reader *reader)
22841 : : {
22842 : 4594 : unsigned n = dump.push (NULL);
22843 : :
22844 : 5138 : dump () && dump ("Completed phase-4 (tokenization) processing");
22845 : :
22846 : 4594 : name_pending_imports (reader);
22847 : 4591 : vec_free (pending_imports);
22848 : :
22849 : 4591 : spans.maybe_init ();
22850 : 4591 : spans.close ();
22851 : :
22852 : 4591 : using iterator = hash_table<module_state_hash>::iterator;
22853 : 4591 : if (mkdeps *deps = cpp_get_deps (reader))
22854 : : {
22855 : : /* Walk the module hash, informing the dependency machinery. */
22856 : 51 : iterator end = modules_hash->end ();
22857 : 294 : for (iterator iter = modules_hash->begin (); iter != end; ++iter)
22858 : : {
22859 : 96 : module_state *module = *iter;
22860 : :
22861 : 96 : if (module->is_direct ())
22862 : : {
22863 : 81 : if (module->is_module ()
22864 : 81 : && (module->is_interface () || module->is_partition ()))
22865 : 33 : deps_add_module_target (deps, module->get_flatname (),
22866 : 33 : maybe_add_cmi_prefix (module->filename),
22867 : 33 : module->is_header (),
22868 : 33 : module->is_exported ());
22869 : : else
22870 : 48 : deps_add_module_dep (deps, module->get_flatname ());
22871 : : }
22872 : : }
22873 : : }
22874 : :
22875 : 4591 : if (flag_header_unit && !flag_preprocess_only)
22876 : : {
22877 : : /* Find the main module -- remember, it's not yet in the module
22878 : : array. */
22879 : 888 : iterator end = modules_hash->end ();
22880 : 1884 : for (iterator iter = modules_hash->begin (); iter != end; ++iter)
22881 : : {
22882 : 942 : module_state *module = *iter;
22883 : 942 : if (module->is_module ())
22884 : : {
22885 : 888 : declare_module (module, cpp_main_loc (reader), true, NULL, reader);
22886 : 888 : module_kind |= MK_EXPORTING;
22887 : 888 : break;
22888 : : }
22889 : : }
22890 : : }
22891 : :
22892 : 4591 : dump.pop (n);
22893 : 4591 : }
22894 : :
22895 : : /* VAL is a global tree, add it to the global vec if it is
22896 : : interesting. Add some of its targets, if they too are
22897 : : interesting. We do not add identifiers, as they can be re-found
22898 : : via the identifier hash table. There is a cost to the number of
22899 : : global trees. */
22900 : :
22901 : : static int
22902 : 2842222 : maybe_add_global (tree val, unsigned &crc)
22903 : : {
22904 : 2842222 : int v = 0;
22905 : :
22906 : 2842222 : if (val && !(identifier_p (val) || TREE_VISITED (val)))
22907 : : {
22908 : 875990 : TREE_VISITED (val) = true;
22909 : 875990 : crc = crc32_unsigned (crc, fixed_trees->length ());
22910 : 875990 : vec_safe_push (fixed_trees, val);
22911 : 875990 : v++;
22912 : :
22913 : 875990 : if (CODE_CONTAINS_STRUCT (TREE_CODE (val), TS_TYPED))
22914 : 875990 : v += maybe_add_global (TREE_TYPE (val), crc);
22915 : 875990 : if (CODE_CONTAINS_STRUCT (TREE_CODE (val), TS_TYPE_COMMON))
22916 : 574250 : v += maybe_add_global (TYPE_NAME (val), crc);
22917 : : }
22918 : :
22919 : 2842222 : return v;
22920 : : }
22921 : :
22922 : : /* Initialize module state. Create the hash table, determine the
22923 : : global trees. Create the module for current TU. */
22924 : :
22925 : : void
22926 : 4594 : init_modules (cpp_reader *reader)
22927 : : {
22928 : : /* PCH should not be reachable because of lang-specs, but the
22929 : : user could have overriden that. */
22930 : 4594 : if (pch_file)
22931 : 0 : fatal_error (input_location,
22932 : : "C++ modules are incompatible with precompiled headers");
22933 : :
22934 : 4594 : if (cpp_get_options (reader)->traditional)
22935 : 0 : fatal_error (input_location,
22936 : : "C++ modules are incompatible with traditional preprocessing");
22937 : :
22938 : : /* :: is always exported. */
22939 : 4594 : DECL_MODULE_EXPORT_P (global_namespace) = true;
22940 : :
22941 : 4594 : modules_hash = hash_table<module_state_hash>::create_ggc (31);
22942 : 4594 : vec_safe_reserve (modules, 20);
22943 : :
22944 : : /* Create module for current TU. */
22945 : 4594 : module_state *current
22946 : 4594 : = new (ggc_alloc<module_state> ()) module_state (NULL_TREE, NULL, false);
22947 : 4594 : current->mod = 0;
22948 : 4594 : bitmap_set_bit (current->imports, 0);
22949 : 4594 : modules->quick_push (current);
22950 : :
22951 : 4594 : gcc_checking_assert (!fixed_trees);
22952 : :
22953 : 4594 : headers = BITMAP_GGC_ALLOC ();
22954 : :
22955 : 4594 : if (note_includes)
22956 : : /* Canonicalize header names. */
22957 : 2 : for (unsigned ix = 0; ix != note_includes->length (); ix++)
22958 : : {
22959 : 1 : const char *hdr = (*note_includes)[ix];
22960 : 1 : size_t len = strlen (hdr);
22961 : :
22962 : 1 : bool system = hdr[0] == '<';
22963 : 1 : bool user = hdr[0] == '"';
22964 : 1 : bool delimed = system || user;
22965 : :
22966 : 1 : if (len <= (delimed ? 2 : 0)
22967 : 1 : || (delimed && hdr[len-1] != (system ? '>' : '"')))
22968 : 0 : error ("invalid header name %qs", hdr);
22969 : :
22970 : 1 : hdr = canonicalize_header_name (delimed ? reader : NULL,
22971 : : 0, !delimed, hdr, len);
22972 : 1 : char *path = XNEWVEC (char, len + 1);
22973 : 1 : memcpy (path, hdr, len);
22974 : 1 : path[len] = 0;
22975 : :
22976 : 1 : (*note_includes)[ix] = path;
22977 : : }
22978 : :
22979 : 4594 : if (note_cmis)
22980 : : /* Canonicalize & mark module names. */
22981 : 12 : for (unsigned ix = 0; ix != note_cmis->length (); ix++)
22982 : : {
22983 : 6 : const char *name = (*note_cmis)[ix];
22984 : 6 : size_t len = strlen (name);
22985 : :
22986 : 6 : bool is_system = name[0] == '<';
22987 : 6 : bool is_user = name[0] == '"';
22988 : 6 : bool is_pathname = false;
22989 : 6 : if (!(is_system || is_user))
22990 : 12 : for (unsigned ix = len; !is_pathname && ix--;)
22991 : 9 : is_pathname = IS_DIR_SEPARATOR (name[ix]);
22992 : 6 : if (is_system || is_user || is_pathname)
22993 : : {
22994 : 3 : if (len <= (is_pathname ? 0 : 2)
22995 : 3 : || (!is_pathname && name[len-1] != (is_system ? '>' : '"')))
22996 : : {
22997 : 0 : error ("invalid header name %qs", name);
22998 : 0 : continue;
22999 : : }
23000 : : else
23001 : 3 : name = canonicalize_header_name (is_pathname ? nullptr : reader,
23002 : : 0, is_pathname, name, len);
23003 : : }
23004 : 6 : if (auto module = get_module (name))
23005 : 6 : module->inform_cmi_p = 1;
23006 : : else
23007 : 0 : error ("invalid module name %qs", name);
23008 : : }
23009 : :
23010 : 4594 : dump.push (NULL);
23011 : :
23012 : : /* Determine lazy handle bound. */
23013 : 4594 : {
23014 : 4594 : unsigned limit = 1000;
23015 : : #if HAVE_GETRLIMIT
23016 : 4594 : struct rlimit rlimit;
23017 : 4594 : if (!getrlimit (RLIMIT_NOFILE, &rlimit))
23018 : : {
23019 : 4594 : lazy_hard_limit = (rlimit.rlim_max < 1000000
23020 : 4594 : ? unsigned (rlimit.rlim_max) : 1000000);
23021 : 4594 : lazy_hard_limit = (lazy_hard_limit > LAZY_HEADROOM
23022 : 4594 : ? lazy_hard_limit - LAZY_HEADROOM : 0);
23023 : 4594 : if (rlimit.rlim_cur < limit)
23024 : 0 : limit = unsigned (rlimit.rlim_cur);
23025 : : }
23026 : : #endif
23027 : 4594 : limit = limit > LAZY_HEADROOM ? limit - LAZY_HEADROOM : 1;
23028 : :
23029 : 4594 : if (unsigned parm = param_lazy_modules)
23030 : : {
23031 : 4594 : if (parm <= limit || !lazy_hard_limit || !try_increase_lazy (parm))
23032 : 6 : lazy_limit = parm;
23033 : : }
23034 : : else
23035 : 0 : lazy_limit = limit;
23036 : : }
23037 : :
23038 : 4594 : if (dump ())
23039 : : {
23040 : 544 : verstr_t ver;
23041 : 544 : version2string (MODULE_VERSION, ver);
23042 : 544 : dump ("Source: %s", main_input_filename);
23043 : 544 : dump ("Compiler: %s", version_string);
23044 : 544 : dump ("Modules: %s", ver);
23045 : 544 : dump ("Checking: %s",
23046 : : #if CHECKING_P
23047 : : "checking"
23048 : : #elif ENABLE_ASSERT_CHECKING
23049 : : "asserting"
23050 : : #else
23051 : : "release"
23052 : : #endif
23053 : : );
23054 : 544 : dump ("Compiled by: "
23055 : : #ifdef __GNUC__
23056 : : "GCC %d.%d, %s", __GNUC__, __GNUC_MINOR__,
23057 : : #ifdef __OPTIMIZE__
23058 : : "optimizing"
23059 : : #else
23060 : : "not optimizing"
23061 : : #endif
23062 : : #else
23063 : : "not GCC"
23064 : : #endif
23065 : : );
23066 : 544 : dump ("Reading: %s", MAPPED_READING ? "mmap" : "fileio");
23067 : 544 : dump ("Writing: %s", MAPPED_WRITING ? "mmap" : "fileio");
23068 : 544 : dump ("Lazy limit: %u", lazy_limit);
23069 : 544 : dump ("Lazy hard limit: %u", lazy_hard_limit);
23070 : 544 : dump ("");
23071 : : }
23072 : :
23073 : : /* Construct the global tree array. This is an array of unique
23074 : : global trees (& types). Do this now, rather than lazily, as
23075 : : some global trees are lazily created and we don't want that to
23076 : : mess with our syndrome of fixed trees. */
23077 : 4594 : unsigned crc = 0;
23078 : 4594 : vec_alloc (fixed_trees, 250);
23079 : :
23080 : 5138 : dump () && dump ("+Creating globals");
23081 : : /* Insert the TRANSLATION_UNIT_DECL. */
23082 : 4594 : TREE_VISITED (DECL_CONTEXT (global_namespace)) = true;
23083 : 4594 : fixed_trees->quick_push (DECL_CONTEXT (global_namespace));
23084 : 27564 : for (unsigned jx = 0; global_tree_arys[jx].first; jx++)
23085 : : {
23086 : 22970 : const tree *ptr = global_tree_arys[jx].first;
23087 : 22970 : unsigned limit = global_tree_arys[jx].second;
23088 : :
23089 : 1401170 : for (unsigned ix = 0; ix != limit; ix++, ptr++)
23090 : : {
23091 : 1378200 : !(ix & 31) && dump ("") && dump ("+\t%u:%u:", jx, ix);
23092 : 1378200 : unsigned v = maybe_add_global (*ptr, crc);
23093 : 1541400 : dump () && dump ("+%u", v);
23094 : : }
23095 : : }
23096 : : /* OS- and machine-specific types are dynamically registered at
23097 : : runtime, so cannot be part of global_tree_arys. */
23098 : 4594 : registered_builtin_types && dump ("") && dump ("+\tB:");
23099 : 18376 : for (tree t = registered_builtin_types; t; t = TREE_CHAIN (t))
23100 : : {
23101 : 13782 : unsigned v = maybe_add_global (TREE_VALUE (t), crc);
23102 : 15414 : dump () && dump ("+%u", v);
23103 : : }
23104 : 4594 : global_crc = crc32_unsigned (crc, fixed_trees->length ());
23105 : 4594 : dump ("") && dump ("Created %u unique globals, crc=%x",
23106 : : fixed_trees->length (), global_crc);
23107 : 885178 : for (unsigned ix = fixed_trees->length (); ix--;)
23108 : 880584 : TREE_VISITED ((*fixed_trees)[ix]) = false;
23109 : :
23110 : 4594 : dump.pop (0);
23111 : :
23112 : 4594 : if (!flag_module_lazy)
23113 : : /* Get the mapper now, if we're not being lazy. */
23114 : 290 : get_mapper (cpp_main_loc (reader), cpp_get_deps (reader));
23115 : :
23116 : 4594 : if (!flag_preprocess_only)
23117 : : {
23118 : 4461 : pending_table = new pending_map_t (EXPERIMENT (1, 400));
23119 : 4461 : entity_map = new entity_map_t (EXPERIMENT (1, 400));
23120 : 4461 : vec_safe_reserve (entity_ary, EXPERIMENT (1, 400));
23121 : 4461 : imported_temploid_friends
23122 : 4461 : = decl_tree_cache_map::create_ggc (EXPERIMENT (1, 400));
23123 : : }
23124 : :
23125 : : #if CHECKING_P
23126 : 4594 : note_defs = note_defs_table_t::create_ggc (1000);
23127 : : #endif
23128 : :
23129 : 4594 : if (flag_header_unit && cpp_get_options (reader)->preprocessed)
23130 : 9 : begin_header_unit (reader);
23131 : :
23132 : : /* Collect here to make sure things are tagged correctly (when
23133 : : aggressively GC'd). */
23134 : 4594 : ggc_collect ();
23135 : 4594 : }
23136 : :
23137 : : /* If NODE is a deferred macro, load it. */
23138 : :
23139 : : static int
23140 : 82736 : load_macros (cpp_reader *reader, cpp_hashnode *node, void *)
23141 : : {
23142 : 82736 : location_t main_loc
23143 : 82736 : = MAP_START_LOCATION (LINEMAPS_ORDINARY_MAP_AT (line_table, 0));
23144 : :
23145 : 82736 : if (cpp_user_macro_p (node)
23146 : 82736 : && !node->value.macro)
23147 : : {
23148 : 72 : cpp_macro *macro = cpp_get_deferred_macro (reader, node, main_loc);
23149 : 72 : dump () && dump ("Loaded macro #%s %I",
23150 : : macro ? "define" : "undef", identifier (node));
23151 : : }
23152 : :
23153 : 82736 : return 1;
23154 : : }
23155 : :
23156 : : /* At the end of tokenizing, we no longer need the macro tables of
23157 : : imports. But the user might have requested some checking. */
23158 : :
23159 : : void
23160 : 95186 : maybe_check_all_macros (cpp_reader *reader)
23161 : : {
23162 : 95186 : if (!warn_imported_macros)
23163 : : return;
23164 : :
23165 : : /* Force loading of any remaining deferred macros. This will
23166 : : produce diagnostics if they are ill-formed. */
23167 : 21 : unsigned n = dump.push (NULL);
23168 : 21 : cpp_forall_identifiers (reader, load_macros, NULL);
23169 : 21 : dump.pop (n);
23170 : : }
23171 : :
23172 : : // State propagated from finish_module_processing to fini_modules
23173 : :
23174 : : struct module_processing_cookie
23175 : : {
23176 : : elf_out out;
23177 : : module_state_config config;
23178 : : char *cmi_name;
23179 : : char *tmp_name;
23180 : : unsigned crc;
23181 : : bool began;
23182 : :
23183 : 2682 : module_processing_cookie (char *cmi, char *tmp, int fd, int e)
23184 : 2682 : : out (fd, e), cmi_name (cmi), tmp_name (tmp), crc (0), began (false)
23185 : : {
23186 : : }
23187 : 2682 : ~module_processing_cookie ()
23188 : : {
23189 : 2682 : XDELETEVEC (tmp_name);
23190 : 2682 : XDELETEVEC (cmi_name);
23191 : 2682 : }
23192 : : };
23193 : :
23194 : : /* Write the CMI, if we're a module interface. */
23195 : :
23196 : : void *
23197 : 94979 : finish_module_processing (cpp_reader *reader)
23198 : : {
23199 : 94979 : module_processing_cookie *cookie = nullptr;
23200 : :
23201 : 94979 : if (header_module_p ())
23202 : 888 : module_kind &= ~MK_EXPORTING;
23203 : :
23204 : 94979 : if (!modules || !this_module ()->name)
23205 : : {
23206 : 92294 : if (flag_module_only)
23207 : 6 : warning (0, "%<-fmodule-only%> used for non-interface");
23208 : : }
23209 : 2685 : else if (!flag_syntax_only)
23210 : : {
23211 : 2682 : int fd = -1;
23212 : 2682 : int e = -1;
23213 : :
23214 : 2682 : timevar_start (TV_MODULE_EXPORT);
23215 : :
23216 : : /* Force a valid but empty line map at the end. This simplifies
23217 : : the line table preparation and writing logic. */
23218 : 2682 : linemap_add (line_table, LC_ENTER, false, "", 0);
23219 : :
23220 : : /* We write to a tmpname, and then atomically rename. */
23221 : 2682 : char *cmi_name = NULL;
23222 : 2682 : char *tmp_name = NULL;
23223 : 2682 : module_state *state = this_module ();
23224 : :
23225 : 2682 : unsigned n = dump.push (state);
23226 : 2682 : state->announce ("creating");
23227 : 2682 : if (state->filename)
23228 : : {
23229 : 2682 : size_t len = 0;
23230 : 2682 : cmi_name = xstrdup (maybe_add_cmi_prefix (state->filename, &len));
23231 : 2682 : tmp_name = XNEWVEC (char, len + 3);
23232 : 2682 : memcpy (tmp_name, cmi_name, len);
23233 : 2682 : strcpy (&tmp_name[len], "~");
23234 : :
23235 : 2682 : if (!errorcount)
23236 : 40 : for (unsigned again = 2; ; again--)
23237 : : {
23238 : 2619 : fd = open (tmp_name,
23239 : : O_RDWR | O_CREAT | O_TRUNC | O_CLOEXEC | O_BINARY,
23240 : : S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH);
23241 : 2619 : e = errno;
23242 : 2619 : if (fd >= 0 || !again || e != ENOENT)
23243 : : break;
23244 : 40 : create_dirs (tmp_name);
23245 : : }
23246 : 2682 : if (note_module_cmi_yes || state->inform_cmi_p)
23247 : 3 : inform (state->loc, "writing CMI %qs", cmi_name);
23248 : 2955 : dump () && dump ("CMI is %s", cmi_name);
23249 : : }
23250 : :
23251 : 2682 : cookie = new module_processing_cookie (cmi_name, tmp_name, fd, e);
23252 : :
23253 : 2682 : if (errorcount)
23254 : : /* Don't write the module if we have reported errors. */;
23255 : 2579 : else if (erroneous_templates
23256 : 2579 : && !erroneous_templates->is_empty ())
23257 : : {
23258 : : /* Don't write the module if it contains an erroneous template.
23259 : : Also emit notes about where errors occurred in case
23260 : : -Wno-template-body was passed. */
23261 : 6 : auto_diagnostic_group d;
23262 : 6 : error_at (state->loc, "not writing module %qs due to errors "
23263 : : "in template bodies", state->get_flatname ());
23264 : 6 : if (!warn_template_body)
23265 : 3 : inform (state->loc, "enable %<-Wtemplate-body%> for more details");
23266 : 12 : for (auto e : *erroneous_templates)
23267 : 6 : inform (e.second, "first error in %qD appeared here", e.first);
23268 : 6 : }
23269 : 2573 : else if (cookie->out.begin ())
23270 : : {
23271 : : /* So crashes finger-point the module decl. */
23272 : 2573 : iloc_sentinel ils = state->loc;
23273 : 2573 : if (state->write_begin (&cookie->out, reader, cookie->config,
23274 : 2573 : cookie->crc))
23275 : 2544 : cookie->began = true;
23276 : 2573 : }
23277 : :
23278 : 2682 : dump.pop (n);
23279 : 2682 : timevar_stop (TV_MODULE_EXPORT);
23280 : :
23281 : 2682 : ggc_collect ();
23282 : : }
23283 : :
23284 : 94979 : if (modules)
23285 : : {
23286 : 4421 : unsigned n = dump.push (NULL);
23287 : 4965 : dump () && dump ("Imported %u modules", modules->length () - 1);
23288 : 4965 : dump () && dump ("Containing %u clusters", available_clusters);
23289 : 4421 : dump () && dump ("Loaded %u clusters (%u%%)", loaded_clusters,
23290 : 544 : (loaded_clusters * 100 + available_clusters / 2) /
23291 : 544 : (available_clusters + !available_clusters));
23292 : 4421 : dump.pop (n);
23293 : : }
23294 : :
23295 : 94979 : return cookie;
23296 : : }
23297 : :
23298 : : // Do the final emission of a module. At this point we know whether
23299 : : // the module static initializer is a NOP or not.
23300 : :
23301 : : static void
23302 : 2682 : late_finish_module (cpp_reader *reader, module_processing_cookie *cookie,
23303 : : bool init_fn_non_empty)
23304 : : {
23305 : 2682 : timevar_start (TV_MODULE_EXPORT);
23306 : :
23307 : 2682 : module_state *state = this_module ();
23308 : 2682 : unsigned n = dump.push (state);
23309 : 2682 : state->announce ("finishing");
23310 : :
23311 : 2682 : cookie->config.active_init = init_fn_non_empty;
23312 : 2682 : if (cookie->began)
23313 : 2544 : state->write_end (&cookie->out, reader, cookie->config, cookie->crc);
23314 : :
23315 : 2682 : if (cookie->out.end () && cookie->cmi_name)
23316 : : {
23317 : : /* Some OS's do not replace NEWNAME if it already exists.
23318 : : This'll have a race condition in erroneous concurrent
23319 : : builds. */
23320 : 2579 : unlink (cookie->cmi_name);
23321 : 2579 : if (rename (cookie->tmp_name, cookie->cmi_name))
23322 : : {
23323 : 0 : dump () && dump ("Rename ('%s','%s') errno=%u",
23324 : 0 : cookie->tmp_name, cookie->cmi_name, errno);
23325 : 0 : cookie->out.set_error (errno);
23326 : : }
23327 : : }
23328 : :
23329 : 2682 : if (cookie->out.get_error () && cookie->began)
23330 : : {
23331 : 0 : error_at (state->loc, "failed to write compiled module: %s",
23332 : 0 : cookie->out.get_error (state->filename));
23333 : 0 : state->note_cmi_name ();
23334 : : }
23335 : :
23336 : 2682 : if (!errorcount)
23337 : : {
23338 : 2538 : auto *mapper = get_mapper (cpp_main_loc (reader), cpp_get_deps (reader));
23339 : 2538 : mapper->ModuleCompiled (state->get_flatname ());
23340 : : }
23341 : 144 : else if (cookie->cmi_name)
23342 : : {
23343 : : /* We failed, attempt to erase all evidence we even tried. */
23344 : 144 : unlink (cookie->tmp_name);
23345 : 144 : unlink (cookie->cmi_name);
23346 : : }
23347 : :
23348 : 2682 : delete cookie;
23349 : 2682 : dump.pop (n);
23350 : 2682 : timevar_stop (TV_MODULE_EXPORT);
23351 : 2682 : }
23352 : :
23353 : : void
23354 : 94979 : fini_modules (cpp_reader *reader, void *cookie, bool has_inits)
23355 : : {
23356 : 94979 : if (cookie)
23357 : 2682 : late_finish_module (reader,
23358 : : static_cast<module_processing_cookie *> (cookie),
23359 : : has_inits);
23360 : :
23361 : : /* We're done with the macro tables now. */
23362 : 94979 : vec_free (macro_exports);
23363 : 94979 : vec_free (macro_imports);
23364 : 94979 : headers = NULL;
23365 : :
23366 : : /* We're now done with everything but the module names. */
23367 : 94979 : set_cmi_repo (NULL);
23368 : 94979 : if (mapper)
23369 : : {
23370 : 4421 : timevar_start (TV_MODULE_MAPPER);
23371 : 4421 : module_client::close_module_client (0, mapper);
23372 : 4421 : mapper = nullptr;
23373 : 4421 : timevar_stop (TV_MODULE_MAPPER);
23374 : : }
23375 : 94979 : module_state_config::release ();
23376 : :
23377 : : #if CHECKING_P
23378 : 94979 : note_defs = NULL;
23379 : : #endif
23380 : :
23381 : 94979 : if (modules)
23382 : 7149 : for (unsigned ix = modules->length (); --ix;)
23383 : 2728 : if (module_state *state = (*modules)[ix])
23384 : 2728 : state->release ();
23385 : :
23386 : : /* No need to lookup modules anymore. */
23387 : 94979 : modules_hash = NULL;
23388 : :
23389 : : /* Or entity array. We still need the entity map to find import numbers. */
23390 : 94979 : vec_free (entity_ary);
23391 : 94979 : entity_ary = NULL;
23392 : :
23393 : : /* Or remember any pending entities. */
23394 : 99400 : delete pending_table;
23395 : 94979 : pending_table = NULL;
23396 : :
23397 : : /* Or any keys -- Let it go! */
23398 : 97211 : delete keyed_table;
23399 : 94979 : keyed_table = NULL;
23400 : :
23401 : : /* Allow a GC, we've possibly made much data unreachable. */
23402 : 94979 : ggc_collect ();
23403 : 94979 : }
23404 : :
23405 : : /* If CODE is a module option, handle it & return true. Otherwise
23406 : : return false. For unknown reasons I cannot get the option
23407 : : generation machinery to set fmodule-mapper or -fmodule-header to
23408 : : make a string type option variable. */
23409 : :
23410 : : bool
23411 : 1886692 : handle_module_option (unsigned code, const char *str, int)
23412 : : {
23413 : 1886692 : auto hdr = CMS_header;
23414 : :
23415 : 1886692 : switch (opt_code (code))
23416 : : {
23417 : 42 : case OPT_fmodule_mapper_:
23418 : 42 : module_mapper_name = str;
23419 : 42 : return true;
23420 : :
23421 : 11 : case OPT_fmodule_header_:
23422 : 11 : {
23423 : 11 : if (!strcmp (str, "user"))
23424 : : hdr = CMS_user;
23425 : 11 : else if (!strcmp (str, "system"))
23426 : : hdr = CMS_system;
23427 : : else
23428 : 0 : error ("unknown header kind %qs", str);
23429 : : }
23430 : : /* Fallthrough. */
23431 : :
23432 : 903 : case OPT_fmodule_header:
23433 : 903 : flag_header_unit = hdr;
23434 : 903 : flag_modules = 1;
23435 : 903 : return true;
23436 : :
23437 : 1 : case OPT_flang_info_include_translate_:
23438 : 1 : vec_safe_push (note_includes, str);
23439 : 1 : return true;
23440 : :
23441 : 6 : case OPT_flang_info_module_cmi_:
23442 : 6 : vec_safe_push (note_cmis, str);
23443 : 6 : return true;
23444 : :
23445 : : default:
23446 : : return false;
23447 : : }
23448 : : }
23449 : :
23450 : : /* Set preprocessor callbacks and options for modules. */
23451 : :
23452 : : void
23453 : 96718 : module_preprocess_options (cpp_reader *reader)
23454 : : {
23455 : 96718 : gcc_checking_assert (!lang_hooks.preprocess_undef);
23456 : 96718 : if (modules_p ())
23457 : : {
23458 : 4594 : auto *cb = cpp_get_callbacks (reader);
23459 : :
23460 : 4594 : cb->translate_include = maybe_translate_include;
23461 : 4594 : cb->user_deferred_macro = module_state::deferred_macro;
23462 : 4594 : if (flag_header_unit)
23463 : : {
23464 : : /* If the preprocessor hook is already in use, that
23465 : : implementation will call the undef langhook. */
23466 : 900 : if (cb->undef)
23467 : 0 : lang_hooks.preprocess_undef = module_state::undef_macro;
23468 : : else
23469 : 900 : cb->undef = module_state::undef_macro;
23470 : : }
23471 : 4594 : auto *opt = cpp_get_options (reader);
23472 : 4594 : opt->module_directives = true;
23473 : 4594 : if (flag_no_output)
23474 : 12 : opt->directives_only = true;
23475 : 4594 : if (opt->main_search == CMS_none)
23476 : 4594 : opt->main_search = cpp_main_search (flag_header_unit);
23477 : : }
23478 : 96718 : }
23479 : :
23480 : : #include "gt-cp-module.h"
|