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 : 225764 : static inline cpp_hashnode *cpp_node (tree id)
280 : : {
281 : 225764 : return CPP_HASHNODE (GCC_IDENT_TO_HT_IDENT (id));
282 : : }
283 : :
284 : 135034 : 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 : 135034 : #pragma GCC diagnostic push
291 : 135034 : #pragma GCC diagnostic ignored "-Warray-bounds"
292 : 135034 : return HT_IDENT_TO_GCC_IDENT (HT_NODE (const_cast<cpp_hashnode *> (node)));
293 : 135034 : #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 : 3791 : version2string (unsigned version, verstr_t &out)
310 : : {
311 : 3791 : unsigned major = MODULE_MAJOR (version);
312 : 3791 : unsigned minor = MODULE_MINOR (version);
313 : :
314 : 3791 : if (IS_EXPERIMENTAL (version))
315 : 3791 : sprintf (out, "%04u/%02u/%02u-%02u:%02u%s",
316 : 3791 : 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 : 3791 : }
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 : 2622 : class allocator {
359 : : public:
360 : : /* Tools tend to moan if the dtor's not virtual. */
361 : 99178 : 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 : 669158 : data ()
383 : 669158 : :buffer (NULL), size (0), pos (0)
384 : : {
385 : : }
386 : 682257 : ~data ()
387 : : {
388 : : /* Make sure the derived and/or using class know what they're
389 : : doing. */
390 : 682257 : gcc_checking_assert (!buffer);
391 : 682257 : }
392 : :
393 : : protected:
394 : 433431465 : char *use (unsigned count)
395 : : {
396 : 433431465 : if (size < pos + count)
397 : : return NULL;
398 : 433431465 : char *res = &buffer[pos];
399 : 433431465 : pos += count;
400 : 232521304 : return res;
401 : : }
402 : :
403 : : unsigned calc_crc (unsigned) const;
404 : :
405 : : public:
406 : 32864940 : void unuse (unsigned count)
407 : : {
408 : 32864940 : pos -= count;
409 : 25418 : }
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 : 583818 : data::allocator::grow (data &obj, unsigned needed, bool exact)
423 : : {
424 : 583818 : gcc_checking_assert (needed ? needed > obj.size : !obj.size);
425 : 583818 : if (!needed)
426 : : /* Pick a default size. */
427 : 249778 : needed = EXPERIMENT (100, 1000);
428 : :
429 : 583818 : if (!exact)
430 : 576267 : needed *= 2;
431 : 583818 : obj.buffer = grow (obj.buffer, needed);
432 : 583818 : if (obj.buffer)
433 : 583818 : obj.size = needed;
434 : : else
435 : 0 : obj.pos = obj.size = 0;
436 : 583818 : }
437 : :
438 : : /* Free a buffer. */
439 : :
440 : : void
441 : 262900 : data::allocator::shrink (data &obj)
442 : : {
443 : 0 : shrink (obj.buffer);
444 : 262900 : obj.buffer = NULL;
445 : 262900 : obj.size = 0;
446 : 0 : }
447 : :
448 : : char *
449 : 10539 : data::allocator::grow (char *ptr, unsigned needed)
450 : : {
451 : 10539 : return XRESIZEVAR (char, ptr, needed);
452 : : }
453 : :
454 : : void
455 : 13110 : data::allocator::shrink (char *ptr)
456 : : {
457 : 13110 : XDELETEVEC (ptr);
458 : 13110 : }
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 : 426976 : data::calc_crc (unsigned l) const
465 : : {
466 : 426976 : 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 : 186379 : bytes_in ()
482 : 186379 : : parent (), overrun (false)
483 : : {
484 : : }
485 : 188971 : ~bytes_in ()
486 : : {
487 : 14391 : }
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 : 1315359 : bool more_p () const
499 : : {
500 : 1315359 : 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 : 1218415 : void align (unsigned boundary)
514 : : {
515 : 1218415 : if (unsigned pad = pos & (boundary - 1))
516 : 2360216 : read (boundary - pad);
517 : : }
518 : :
519 : : public:
520 : 200910161 : const char *read (unsigned count)
521 : : {
522 : 1141801 : char *ptr = use (count);
523 : 200910161 : if (!ptr)
524 : 0 : set_overrun ();
525 : 163634228 : 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 : 187437 : unsigned get_crc () const
532 : : {
533 : 187437 : return *(const unsigned *)&buffer[0];
534 : : }
535 : :
536 : : public:
537 : : /* Manipulate the overrun flag. */
538 : 117381729 : bool get_overrun () const
539 : : {
540 : 117381729 : 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 : 184708 : bytes_in::check_crc () const
571 : : {
572 : 184708 : if (size < 4)
573 : : return false;
574 : :
575 : 184708 : unsigned c_crc = calc_crc (size);
576 : 184708 : 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 : 477407 : bytes_out (allocator *memory)
595 : 477407 : : parent (), memory (memory)
596 : : {
597 : : }
598 : 477407 : ~bytes_out ()
599 : : {
600 : 504532 : }
601 : :
602 : : public:
603 : 558516946 : bool streaming_p () const
604 : : {
605 : 558516946 : 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 : 1539317 : void align (unsigned boundary)
619 : : {
620 : 1539317 : if (unsigned pad = pos & (boundary - 1))
621 : 1442584 : write (boundary - pad);
622 : 1539317 : }
623 : :
624 : : public:
625 : 232521304 : char *write (unsigned count, bool exact = false)
626 : : {
627 : 232521304 : if (size < pos + count)
628 : 320976 : memory->grow (*this, pos + count, exact);
629 : 232521304 : 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 : 15682 : void str (const char *ptr)
644 : : {
645 : 15682 : str (ptr, strlen (ptr));
646 : 15682 : }
647 : 244945 : void cpp_node (const cpp_hashnode *node)
648 : : {
649 : 244945 : 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 : 32814477 : bit_flush (data& bits, uint32_t& bit_val, unsigned& bit_pos)
678 : : {
679 : 32814477 : gcc_assert (bit_pos);
680 : 32814477 : unsigned bytes = (bit_pos + 7) / 8;
681 : 32814477 : bits.unuse (4 - bytes);
682 : 32814477 : bit_pos = 0;
683 : 32814477 : bit_val = 0;
684 : 32814477 : 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 : 11248695 : bits_in (bytes_in& in)
707 : 11248695 : : in (in)
708 : : { }
709 : :
710 : 11248695 : ~bits_in ()
711 : : {
712 : 10524226 : bflush ();
713 : 11248695 : }
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 : 23359207 : void bflush ()
721 : : {
722 : 23359207 : if (bit_pos)
723 : 12834981 : bit_flush (in, bit_val, bit_pos);
724 : 23359207 : }
725 : :
726 : : /* Read one bit. */
727 : 384984223 : bool b ()
728 : : {
729 : 384984223 : if (!bit_pos)
730 : 16748094 : bit_val = in.u32 ();
731 : 384984223 : bool x = (bit_val >> bit_pos) & 1;
732 : 384984223 : bit_pos = (bit_pos + 1) % 32;
733 : 384984223 : return x;
734 : : }
735 : : };
736 : :
737 : : /* Factory function for bits_in. */
738 : :
739 : : bytes_in::bits_in
740 : 11248695 : bytes_in::stream_bits ()
741 : : {
742 : 11248695 : 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 : 13313506 : bits_out (bytes_out& out)
754 : 13313506 : : out (out)
755 : : { }
756 : :
757 : 13313506 : ~bits_out ()
758 : : {
759 : 67474 : 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 : 27644860 : void bflush ()
768 : : {
769 : 27644860 : if (bit_pos)
770 : : {
771 : 15230950 : out.u32 (bit_val);
772 : 15230950 : out.lengths[2] += bit_flush (out, bit_val, bit_pos);
773 : : }
774 : 27644860 : out.spans[2]++;
775 : 27644860 : is_set = -1;
776 : 27644860 : }
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 : 455645668 : void b (bool x)
783 : : {
784 : 455645668 : if (is_set != x)
785 : : {
786 : 48926691 : is_set = x;
787 : 48926691 : out.spans[x]++;
788 : : }
789 : 455645668 : out.lengths[x]++;
790 : 455645668 : bit_val |= unsigned (x) << bit_pos++;
791 : 455645668 : if (bit_pos == 32)
792 : : {
793 : 4748546 : out.u32 (bit_val);
794 : 4748546 : out.lengths[2] += bit_flush (out, bit_val, bit_pos);
795 : : }
796 : 455645668 : }
797 : : };
798 : :
799 : : /* Factory function for bits_out. */
800 : :
801 : : bytes_out::bits_out
802 : 13313506 : bytes_out::stream_bits ()
803 : : {
804 : 13313506 : 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 : 244752 : bytes_out::set_crc (unsigned *crc_ptr)
816 : : {
817 : 244752 : if (crc_ptr)
818 : : {
819 : 242268 : gcc_checking_assert (pos >= 4);
820 : :
821 : 242268 : unsigned crc = calc_crc (pos);
822 : 242268 : unsigned accum = *crc_ptr;
823 : : /* Only mix the existing *CRC_PTR if it is non-zero. */
824 : 242268 : accum = accum ? crc32_combine (accum, crc, pos - 4) : crc;
825 : 242268 : *crc_ptr = accum;
826 : :
827 : : /* Buffer will be sufficiently aligned. */
828 : 242268 : *(unsigned *)buffer = crc;
829 : : }
830 : 244752 : }
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 : 19987534 : bytes_out::u32 (unsigned val)
839 : : {
840 : 19987534 : if (char *ptr = write (4))
841 : : {
842 : 19987534 : ptr[0] = val;
843 : 19987534 : ptr[1] = val >> 8;
844 : 19987534 : ptr[2] = val >> 16;
845 : 19987534 : ptr[3] = val >> 24;
846 : : }
847 : 19987534 : }
848 : :
849 : : unsigned
850 : 16756656 : bytes_in::u32 ()
851 : : {
852 : 16756656 : unsigned val = 0;
853 : 16756656 : if (const char *ptr = read (4))
854 : : {
855 : 16756656 : val |= (unsigned char)ptr[0];
856 : 16756656 : val |= (unsigned char)ptr[1] << 8;
857 : 16756656 : val |= (unsigned char)ptr[2] << 16;
858 : 16756656 : val |= (unsigned char)ptr[3] << 24;
859 : : }
860 : :
861 : 16756656 : 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 : 82033424 : bytes_out::i (int v)
887 : : {
888 : 82033424 : if (char *ptr = write (1))
889 : : {
890 : 82033424 : if (v <= 0x3f && v >= -0x40)
891 : 64842933 : *ptr = v & 0x7f;
892 : : else
893 : : {
894 : 17190491 : unsigned bytes = 0;
895 : 17190491 : int probe;
896 : 17190491 : if (v >= 0)
897 : 0 : for (probe = v >> 8; probe > 0x7; probe >>= 8)
898 : 0 : bytes++;
899 : : else
900 : 26663904 : for (probe = v >> 8; probe < -0x8; probe >>= 8)
901 : 9473413 : bytes++;
902 : 17190491 : *ptr = 0x80 | bytes << 4 | (probe & 0xf);
903 : 17190491 : if ((ptr = write (++bytes)))
904 : 43854395 : for (; bytes--; v >>= 8)
905 : 26663904 : ptr[bytes] = v & 0xff;
906 : : }
907 : : }
908 : 82033424 : }
909 : :
910 : : int
911 : 70066838 : bytes_in::i ()
912 : : {
913 : 70066838 : int v = 0;
914 : 70066838 : if (const char *ptr = read (1))
915 : : {
916 : 70066838 : v = *ptr & 0xff;
917 : 70066838 : if (v & 0x80)
918 : : {
919 : 15427873 : unsigned bytes = (v >> 4) & 0x7;
920 : 15427873 : v &= 0xf;
921 : 15427873 : if (v & 0x8)
922 : 15427873 : v |= -1 ^ 0x7;
923 : : /* unsigned necessary due to left shifts of -ve values. */
924 : 15427873 : unsigned uv = unsigned (v);
925 : 15427873 : if ((ptr = read (++bytes)))
926 : 40067509 : while (bytes--)
927 : 24639636 : uv = (uv << 8) | (*ptr++ & 0xff);
928 : 15427873 : v = int (uv);
929 : : }
930 : 54638965 : else if (v & 0x40)
931 : 7393850 : v |= -1 ^ 0x3f;
932 : : }
933 : :
934 : 70066838 : return v;
935 : : }
936 : :
937 : : void
938 : 70711507 : bytes_out::u (unsigned v)
939 : : {
940 : 70711507 : if (char *ptr = write (1))
941 : : {
942 : 70711507 : if (v <= 0x7f)
943 : 61651161 : *ptr = v;
944 : : else
945 : : {
946 : 9060346 : unsigned bytes = 0;
947 : 9060346 : unsigned probe;
948 : 10633033 : for (probe = v >> 8; probe > 0xf; probe >>= 8)
949 : 1572687 : bytes++;
950 : 9060346 : *ptr = 0x80 | bytes << 4 | probe;
951 : 9060346 : if ((ptr = write (++bytes)))
952 : 19693379 : for (; bytes--; v >>= 8)
953 : 10633033 : ptr[bytes] = v & 0xff;
954 : : }
955 : : }
956 : 70711507 : }
957 : :
958 : : unsigned
959 : 61510653 : bytes_in::u ()
960 : : {
961 : 61510653 : unsigned v = 0;
962 : :
963 : 61510653 : if (const char *ptr = read (1))
964 : : {
965 : 61510653 : v = *ptr & 0xff;
966 : 61510653 : if (v & 0x80)
967 : : {
968 : 8095419 : unsigned bytes = (v >> 4) & 0x7;
969 : 8095419 : v &= 0xf;
970 : 8095419 : if ((ptr = read (++bytes)))
971 : 17701089 : while (bytes--)
972 : 9605670 : v = (v << 8) | (*ptr++ & 0xff);
973 : : }
974 : : }
975 : :
976 : 61510653 : return v;
977 : : }
978 : :
979 : : void
980 : 16945765 : bytes_out::wi (HOST_WIDE_INT v)
981 : : {
982 : 16945765 : if (char *ptr = write (1))
983 : : {
984 : 16945765 : if (v <= 0x3f && v >= -0x40)
985 : 3385892 : *ptr = v & 0x7f;
986 : : else
987 : : {
988 : 13559873 : unsigned bytes = 0;
989 : 13559873 : HOST_WIDE_INT probe;
990 : 13559873 : if (v >= 0)
991 : 48591975 : for (probe = v >> 8; probe > 0x7; probe >>= 8)
992 : 35034538 : bytes++;
993 : : else
994 : 7957 : for (probe = v >> 8; probe < -0x8; probe >>= 8)
995 : 5521 : bytes++;
996 : 13559873 : *ptr = 0x80 | bytes << 4 | (probe & 0xf);
997 : 13559873 : if ((ptr = write (++bytes)))
998 : 62159805 : for (; bytes--; v >>= 8)
999 : 48599932 : ptr[bytes] = v & 0xff;
1000 : : }
1001 : : }
1002 : 16945765 : }
1003 : :
1004 : : HOST_WIDE_INT
1005 : 14081666 : bytes_in::wi ()
1006 : : {
1007 : 14081666 : HOST_WIDE_INT v = 0;
1008 : 14081666 : if (const char *ptr = read (1))
1009 : : {
1010 : 14081666 : v = *ptr & 0xff;
1011 : 14081666 : if (v & 0x80)
1012 : : {
1013 : 12610840 : unsigned bytes = (v >> 4) & 0x7;
1014 : 12610840 : v &= 0xf;
1015 : 12610840 : if (v & 0x8)
1016 : 1670 : v |= -1 ^ 0x7;
1017 : : /* unsigned necessary due to left shifts of -ve values. */
1018 : 12610840 : unsigned HOST_WIDE_INT uv = (unsigned HOST_WIDE_INT) v;
1019 : 12610840 : if ((ptr = read (++bytes)))
1020 : 57610438 : while (bytes--)
1021 : 44999598 : uv = (uv << 8) | (*ptr++ & 0xff);
1022 : 12610840 : v = (HOST_WIDE_INT) uv;
1023 : : }
1024 : 1470826 : else if (v & 0x40)
1025 : 6911 : v |= -1 ^ 0x3f;
1026 : : }
1027 : :
1028 : 14081666 : return v;
1029 : : }
1030 : :
1031 : : /* unsigned wide ints are just written as signed wide ints. */
1032 : :
1033 : : inline void
1034 : 16945201 : bytes_out::wu (unsigned HOST_WIDE_INT v)
1035 : : {
1036 : 16945201 : wi ((HOST_WIDE_INT) v);
1037 : : }
1038 : :
1039 : : inline unsigned HOST_WIDE_INT
1040 : 14081176 : bytes_in::wu ()
1041 : : {
1042 : 27687103 : return (unsigned HOST_WIDE_INT) wi ();
1043 : : }
1044 : :
1045 : : /* size_t written as unsigned or unsigned wide int. */
1046 : :
1047 : : inline void
1048 : 1510646 : bytes_out::z (size_t s)
1049 : : {
1050 : 1510646 : if (sizeof (s) == sizeof (unsigned))
1051 : : u (s);
1052 : : else
1053 : 2991099 : wu (s);
1054 : 12 : }
1055 : :
1056 : : inline size_t
1057 : 1204497 : bytes_in::z ()
1058 : : {
1059 : 1204497 : if (sizeof (size_t) == sizeof (unsigned))
1060 : : return u ();
1061 : : else
1062 : 2408994 : return wu ();
1063 : : }
1064 : :
1065 : : /* location_t written as 32- or 64-bit as needed. */
1066 : :
1067 : 14869569 : inline void bytes_out::loc (location_t l)
1068 : : {
1069 : 14869569 : if (sizeof (location_t) > sizeof (unsigned))
1070 : 28196291 : wu (l);
1071 : : else
1072 : : u (l);
1073 : 1540363 : }
1074 : :
1075 : 12404116 : inline location_t bytes_in::loc ()
1076 : : {
1077 : 12404116 : if (sizeof (location_t) > sizeof (unsigned))
1078 : 24805516 : return wu ();
1079 : : else
1080 : : return u ();
1081 : : }
1082 : :
1083 : : /* Buffer simply memcpied. */
1084 : : void *
1085 : 1539317 : bytes_out::buf (size_t len)
1086 : : {
1087 : 1539317 : align (sizeof (void *) * 2);
1088 : 1539317 : return write (len);
1089 : : }
1090 : :
1091 : : void
1092 : 1494871 : bytes_out::buf (const void *src, size_t len)
1093 : : {
1094 : 1494871 : if (void *ptr = buf (len))
1095 : 1494871 : memcpy (ptr, src, len);
1096 : 1494871 : }
1097 : :
1098 : : const void *
1099 : 1218415 : bytes_in::buf (size_t len)
1100 : : {
1101 : 1218415 : align (sizeof (void *) * 2);
1102 : 1218415 : const char *ptr = read (len);
1103 : :
1104 : 1218415 : 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 : 1510628 : bytes_out::str (const char *string, size_t len)
1112 : : {
1113 : 1480447 : z (len);
1114 : 1480447 : if (len)
1115 : : {
1116 : 1480447 : gcc_checking_assert (!string[len]);
1117 : 1480447 : buf (string, len + 1);
1118 : : }
1119 : 30181 : }
1120 : :
1121 : : const char *
1122 : 1204491 : bytes_in::str (size_t *len_p)
1123 : : {
1124 : 1204491 : size_t len = z ();
1125 : :
1126 : : /* We're about to trust some user data. */
1127 : 1204491 : if (overrun)
1128 : 0 : len = 0;
1129 : 1204491 : if (len_p)
1130 : 1200984 : *len_p = len;
1131 : 1204491 : const char *str = NULL;
1132 : 1204491 : if (len)
1133 : : {
1134 : 1204282 : str = reinterpret_cast<const char *> (buf (len + 1));
1135 : 1204282 : 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 : 225973 : bytes_in::cpp_node ()
1146 : : {
1147 : 225973 : size_t len;
1148 : 225973 : const char *s = str (&len);
1149 : 225973 : if (!len)
1150 : : return NULL;
1151 : 225764 : 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 : 25418 : bytes_out::printf (const char *format, ...)
1159 : : {
1160 : 25418 : va_list args;
1161 : : /* Exercise buffer expansion. */
1162 : 25418 : size_t len = EXPERIMENT (10, 500);
1163 : :
1164 : 50463 : while (char *ptr = write (len))
1165 : : {
1166 : 50463 : va_start (args, format);
1167 : 50463 : size_t actual = vsnprintf (ptr, len, format, args) + 1;
1168 : 50463 : va_end (args);
1169 : 50463 : if (actual <= len)
1170 : : {
1171 : 25418 : unuse (len - actual);
1172 : 25418 : break;
1173 : : }
1174 : 25045 : unuse (len);
1175 : 25045 : len = actual;
1176 : 25045 : }
1177 : 25418 : }
1178 : :
1179 : : void
1180 : 4968 : bytes_out::print_time (const char *kind, const tm *time, const char *tz)
1181 : : {
1182 : 4968 : printf ("%stime: %4u/%02u/%02u %02u:%02u:%02u %s",
1183 : 4968 : kind, time->tm_year + 1900, time->tm_mon + 1, time->tm_mday,
1184 : 4968 : time->tm_hour, time->tm_min, time->tm_sec, tz);
1185 : 4968 : }
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 : 5372 : elf (int fd, int e)
1293 : 10744 : :hdr (), sectab (), strtab (), fd (fd), err (fd >= 0 ? 0 : e)
1294 : : {}
1295 : 5293 : ~elf ()
1296 : : {
1297 : 5293 : gcc_checking_assert (fd < 0 && !hdr.buffer
1298 : : && !sectab.buffer && !strtab.buffer);
1299 : 5293 : }
1300 : :
1301 : : public:
1302 : : /* Return the error, if we have an error. */
1303 : 360003 : int get_error () const
1304 : : {
1305 : 360003 : return err;
1306 : : }
1307 : : /* Set the error, unless it's already been set. */
1308 : 32 : void set_error (int e = E_BAD_DATA)
1309 : : {
1310 : 32 : 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 : 5242 : bool begin () const
1319 : : {
1320 : 5242 : 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 : 7492 : elf::end ()
1353 : : {
1354 : : /* Close the stream and free the section table. */
1355 : 7492 : if (fd >= 0 && close (fd))
1356 : 0 : set_error (errno);
1357 : 7492 : fd = -1;
1358 : :
1359 : 7492 : 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 : 2750 : elf_in (int fd, int e)
1376 : 5500 : :parent (fd, e)
1377 : : {
1378 : : }
1379 : 2671 : ~elf_in ()
1380 : : {
1381 : 2671 : }
1382 : :
1383 : : public:
1384 : 167992 : 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 : 1040 : static void release (elf_in *self ATTRIBUTE_UNUSED, bytes_in &bytes)
1411 : : {
1412 : : #if MAPPED_READING
1413 : 1040 : if (!(self && self->hdr.buffer && bytes.buffer >= self->hdr.buffer
1414 : 1040 : && bytes.buffer < self->hdr.buffer + self->hdr.pos))
1415 : : #endif
1416 : 0 : data::simple_memory.shrink (bytes.buffer);
1417 : 1040 : bytes.buffer = NULL;
1418 : 1040 : bytes.size = 0;
1419 : 1040 : }
1420 : :
1421 : : public:
1422 : 190166 : static void grow (data &data, unsigned needed)
1423 : : {
1424 : 190166 : gcc_checking_assert (!data.buffer);
1425 : : #if !MAPPED_READING
1426 : : data.buffer = XNEWVEC (char, needed);
1427 : : #endif
1428 : 190166 : data.size = needed;
1429 : 190166 : }
1430 : 196073 : static void shrink (data &data)
1431 : : {
1432 : : #if !MAPPED_READING
1433 : : XDELETEVEC (data.buffer);
1434 : : #endif
1435 : 196073 : data.buffer = NULL;
1436 : 196073 : data.size = 0;
1437 : 0 : }
1438 : :
1439 : : public:
1440 : 187437 : const section *get_section (unsigned s) const
1441 : : {
1442 : 187437 : if (s * sizeof (section) < sectab.size)
1443 : 187437 : return reinterpret_cast<const section *>
1444 : 187437 : (§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 : 187437 : bool read (data *d, const section *s)
1459 : : {
1460 : 187437 : 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 : 7574 : void release ()
1471 : : {
1472 : 7574 : shrink (strtab);
1473 : 39 : }
1474 : :
1475 : : public:
1476 : : bool begin (location_t);
1477 : 4870 : bool end ()
1478 : : {
1479 : 4870 : release ();
1480 : : #if MAPPED_READING
1481 : 4870 : if (hdr.buffer)
1482 : 2671 : munmap (hdr.buffer, hdr.pos);
1483 : 4870 : hdr.buffer = NULL;
1484 : : #endif
1485 : 4870 : shrink (sectab);
1486 : :
1487 : 4870 : 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 : 278125 : const char *name (unsigned offset)
1494 : : {
1495 : 556250 : 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 : 2622 : elf_out (int fd, int e)
1517 : 5141 : :parent (fd, e), identtab (500), pos (0)
1518 : : {
1519 : : #if MAPPED_WRITING
1520 : 2622 : offset = extent = 0;
1521 : 2622 : page_size = sysconf (_SC_PAGE_SIZE);
1522 : 2622 : if (page_size < SECTION_ALIGN)
1523 : : /* Something really strange. */
1524 : 0 : set_error (EINVAL);
1525 : : #endif
1526 : 2622 : }
1527 : 2622 : ~elf_out ()
1528 : 2622 : {
1529 : 2622 : data::simple_memory.shrink (hdr);
1530 : 2622 : data::simple_memory.shrink (sectab);
1531 : 2622 : data::simple_memory.shrink (strtab);
1532 : 2622 : }
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 : 5241 : unsigned get_section_limit () const
1550 : : {
1551 : 5241 : 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 : 19418 : bytes_in::begin (location_t loc, elf_in *source, const char *name)
1589 : : {
1590 : 19418 : unsigned snum = source->find (name);
1591 : :
1592 : 19418 : return begin (loc, source, snum, name);
1593 : : }
1594 : :
1595 : : /* Begin reading section numbered SNUM with NAME (may be NULL). */
1596 : :
1597 : : bool
1598 : 184708 : bytes_in::begin (location_t loc, elf_in *source, unsigned snum, const char *name)
1599 : : {
1600 : 184708 : if (!source->read (this, source->find (snum))
1601 : 184708 : || !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 : 184708 : pos = 4;
1612 : 184708 : return true;
1613 : : }
1614 : :
1615 : : /* Finish reading a section. */
1616 : :
1617 : : bool
1618 : 183629 : bytes_in::end (elf_in *src)
1619 : : {
1620 : 183629 : if (more_p ())
1621 : 13 : set_overrun ();
1622 : 183629 : if (overrun)
1623 : 13 : src->set_error ();
1624 : :
1625 : 183629 : src->shrink (*this);
1626 : :
1627 : 183629 : return !overrun;
1628 : : }
1629 : :
1630 : : /* Begin writing buffer. */
1631 : :
1632 : : void
1633 : 244752 : bytes_out::begin (bool need_crc)
1634 : : {
1635 : 0 : if (need_crc)
1636 : 0 : pos = 4;
1637 : 0 : memory->grow (*this, 0, false);
1638 : 225149 : }
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 : 244752 : bytes_out::end (elf_out *sink, unsigned name, unsigned *crc_ptr)
1646 : : {
1647 : 244752 : lengths[3] += pos;
1648 : 244752 : spans[3]++;
1649 : :
1650 : 244752 : set_crc (crc_ptr);
1651 : 244752 : unsigned sec_num = sink->add (*this, !crc_ptr, name);
1652 : 244752 : memory->shrink (*this);
1653 : :
1654 : 244752 : 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 : 190166 : elf_in::read (data *data, unsigned pos, unsigned length)
1720 : : {
1721 : : #if MAPPED_READING
1722 : 190166 : 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 : 190166 : grow (*data, length);
1735 : : #if MAPPED_READING
1736 : 190166 : 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 : 190166 : return data->buffer;
1747 : : }
1748 : :
1749 : : /* Read section SNUM of TYPE. Return section pointer or NULL on error. */
1750 : :
1751 : : const elf::section *
1752 : 187437 : elf_in::find (unsigned snum, unsigned type)
1753 : : {
1754 : 187437 : const section *sec = get_section (snum);
1755 : 187437 : 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 : 19463 : elf_in::find (const char *sname)
1765 : : {
1766 : 124709 : for (unsigned pos = sectab.size; pos -= sizeof (section); )
1767 : : {
1768 : 124709 : const section *sec
1769 : 124709 : = reinterpret_cast<const section *> (§ab.buffer[pos]);
1770 : :
1771 : 249418 : if (0 == strcmp (sname, name (sec->name)))
1772 : 19463 : 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 : 2729 : elf_in::begin (location_t loc)
1783 : : {
1784 : 2729 : if (!parent::begin ())
1785 : : return false;
1786 : :
1787 : 2729 : struct stat stat;
1788 : 2729 : unsigned size = 0;
1789 : 2729 : if (!fstat (fd, &stat))
1790 : : {
1791 : : #if !defined (HOST_LACKS_INODE_NUMBERS)
1792 : 2729 : device = stat.st_dev;
1793 : 2729 : inode = stat.st_ino;
1794 : : #endif
1795 : : /* Never generate files > 4GB, check we've not been given one. */
1796 : 2729 : if (stat.st_size == unsigned (stat.st_size))
1797 : 2729 : 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 : 2729 : void *mapping = mmap (NULL, size, PROT_READ, MAP_SHARED, fd, 0);
1804 : 2729 : 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 : 2729 : if (madvise (reinterpret_cast <char *> (mapping), size, MADV_RANDOM))
1813 : 0 : goto fail;
1814 : :
1815 : 2729 : hdr.buffer = (char *)mapping;
1816 : : #else
1817 : : read (&hdr, 0, sizeof (header));
1818 : : #endif
1819 : 2729 : hdr.pos = size; /* Record size of the file. */
1820 : :
1821 : 2729 : const header *h = reinterpret_cast<const header *> (hdr.buffer);
1822 : 2729 : if (!h)
1823 : : return false;
1824 : :
1825 : 2729 : if (h->ident.magic[0] != 0x7f
1826 : 2729 : || h->ident.magic[1] != 'E'
1827 : 2729 : || h->ident.magic[2] != 'L'
1828 : 2729 : || 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 : 2729 : if (h->ident.klass != MY_CLASS
1839 : 2729 : || h->ident.data != MY_ENDIAN
1840 : 2729 : || h->ident.version != EV_CURRENT
1841 : 2729 : || h->type != ET_NONE
1842 : 2729 : || h->machine != EM_NONE
1843 : 2729 : || h->ident.osabi != OSABI_NONE)
1844 : : {
1845 : 0 : error_at (loc, "unexpected encapsulation format or type");
1846 : 0 : goto failed;
1847 : : }
1848 : :
1849 : 2729 : int e = -1;
1850 : 2729 : 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 : 2729 : unsigned strndx = h->shstrndx;
1859 : 2729 : unsigned shnum = h->shnum;
1860 : 2729 : 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 : 2729 : if (!shnum)
1875 : 0 : goto malformed;
1876 : :
1877 : 2729 : if (!read (§ab, h->shoff, shnum * sizeof (section)))
1878 : 0 : goto section_table_fail;
1879 : :
1880 : 2729 : if (strndx == SHN_XINDEX)
1881 : 0 : strndx = get_section (0)->link;
1882 : :
1883 : 2729 : 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 : 2729 : if (!(strtab.size && !strtab.buffer[0]
1889 : 2729 : && !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 : 2729 : sectab.pos = h->shoff;
1895 : 2729 : strtab.pos = shnum * sizeof (section);
1896 : : #else
1897 : : shrink (hdr);
1898 : : #endif
1899 : :
1900 : 2729 : return true;
1901 : : }
1902 : :
1903 : : /* Create a new mapping. */
1904 : :
1905 : : #if MAPPED_WRITING
1906 : : void
1907 : 3239 : 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 : 6352 : auto allocate = [](int fd, off_t offset, off_t length)
1912 : : {
1913 : : #ifdef HAVE_POSIX_FALLOCATE
1914 : 3113 : int result = posix_fallocate (fd, offset, length);
1915 : 3113 : if (result != EINVAL)
1916 : 3113 : 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 : 3239 : void *mapping = MAP_FAILED;
1923 : 3239 : if (extending && ext < 1024 * 1024)
1924 : : {
1925 : 3000 : if (allocate (fd, offset, ext * 2))
1926 : 3000 : mapping = mmap (NULL, ext * 2, PROT_READ | PROT_WRITE,
1927 : 3000 : MAP_SHARED, fd, offset);
1928 : 3000 : if (mapping != MAP_FAILED)
1929 : : ext *= 2;
1930 : : }
1931 : : if (mapping == MAP_FAILED)
1932 : : {
1933 : 239 : if (!extending || allocate (fd, offset, ext))
1934 : 239 : mapping = mmap (NULL, ext, PROT_READ | PROT_WRITE,
1935 : 239 : MAP_SHARED, fd, offset);
1936 : 239 : if (mapping == MAP_FAILED)
1937 : : {
1938 : 0 : set_error (errno);
1939 : : mapping = NULL;
1940 : : ext = 0;
1941 : : }
1942 : : }
1943 : 3239 : hdr.buffer = (char *)mapping;
1944 : 3239 : extent = ext;
1945 : 3239 : }
1946 : : #endif
1947 : :
1948 : : /* Flush out the current mapping. */
1949 : :
1950 : : #if MAPPED_WRITING
1951 : : void
1952 : 3245 : elf_out::remove_mapping ()
1953 : : {
1954 : 3245 : if (hdr.buffer)
1955 : : {
1956 : : /* MS_ASYNC dtrt with the removed mapping, including a
1957 : : subsequent overlapping remap. */
1958 : 3239 : if (msync (hdr.buffer, extent, MS_ASYNC)
1959 : 3239 : || munmap (hdr.buffer, extent))
1960 : : /* We're somewhat screwed at this point. */
1961 : 0 : set_error (errno);
1962 : : }
1963 : :
1964 : 3245 : hdr.buffer = NULL;
1965 : 3245 : }
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 : 573279 : elf_out::grow (char *data, unsigned needed)
1973 : : {
1974 : 573279 : if (!data)
1975 : : {
1976 : : /* First allocation, check we're aligned. */
1977 : 249790 : gcc_checking_assert (!(pos & (SECTION_ALIGN - 1)));
1978 : : #if MAPPED_WRITING
1979 : 249790 : data = hdr.buffer + (pos - offset);
1980 : : #endif
1981 : : }
1982 : :
1983 : : #if MAPPED_WRITING
1984 : 573279 : unsigned off = data - hdr.buffer;
1985 : 573279 : if (off + needed > extent)
1986 : : {
1987 : : /* We need to grow the mapping. */
1988 : 600 : unsigned lwm = off & ~(page_size - 1);
1989 : 600 : unsigned hwm = (off + needed + page_size - 1) & ~(page_size - 1);
1990 : :
1991 : 600 : gcc_checking_assert (hwm > extent);
1992 : :
1993 : 600 : remove_mapping ();
1994 : :
1995 : 600 : offset += lwm;
1996 : 600 : create_mapping (extent < hwm - lwm ? hwm - lwm : extent);
1997 : :
1998 : 600 : data = hdr.buffer + (off - lwm);
1999 : : }
2000 : : #else
2001 : : data = allocator::grow (data, needed);
2002 : : #endif
2003 : :
2004 : 573279 : return data;
2005 : : }
2006 : :
2007 : : #if MAPPED_WRITING
2008 : : /* Shrinking is a NOP. */
2009 : : void
2010 : 249790 : elf_out::shrink (char *)
2011 : : {
2012 : 249790 : }
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 : 1073840 : elf_out::strtab_write (const char *s, unsigned l)
2020 : : {
2021 : 1073840 : if (strtab.pos + l > strtab.size)
2022 : 1158 : data::simple_memory.grow (strtab, strtab.pos + l, false);
2023 : 1073840 : memcpy (strtab.buffer + strtab.pos, s, l);
2024 : 1073840 : unsigned res = strtab.pos;
2025 : 1073840 : strtab.pos += l;
2026 : 1073840 : 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 : 422189 : elf_out::strtab_write (tree decl, int inner)
2034 : : {
2035 : 422189 : tree ctx = CP_DECL_CONTEXT (decl);
2036 : 422189 : if (TYPE_P (ctx))
2037 : 4751 : ctx = TYPE_NAME (ctx);
2038 : 422189 : if (ctx != global_namespace)
2039 : 207357 : strtab_write (ctx, -1);
2040 : :
2041 : 422189 : tree name = DECL_NAME (decl);
2042 : 422189 : if (!name)
2043 : 348 : name = DECL_ASSEMBLER_NAME_RAW (decl);
2044 : 422189 : strtab_write (IDENTIFIER_POINTER (name), IDENTIFIER_LENGTH (name));
2045 : :
2046 : 422189 : if (inner)
2047 : 296665 : strtab_write (&"::{}"[inner+1], 2);
2048 : 422189 : }
2049 : :
2050 : : /* Map IDENTIFIER IDENT to strtab offset. Inserts into strtab if not
2051 : : already there. */
2052 : :
2053 : : unsigned
2054 : 120455 : elf_out::name (tree ident)
2055 : : {
2056 : 120455 : unsigned res = 0;
2057 : 120455 : if (ident)
2058 : : {
2059 : 120412 : bool existed;
2060 : 120412 : int *slot = &identtab.get_or_insert (ident, &existed);
2061 : 120412 : if (!existed)
2062 : 216044 : *slot = strtab_write (IDENTIFIER_POINTER (ident),
2063 : 108022 : IDENTIFIER_LENGTH (ident) + 1);
2064 : 120412 : res = *slot;
2065 : : }
2066 : 120455 : 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 : 32132 : elf_out::name (const char *literal)
2074 : : {
2075 : 32132 : 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 : 214832 : elf_out::qualified_name (tree decl, bool is_defn)
2083 : : {
2084 : 214832 : gcc_checking_assert (DECL_P (decl) && decl != global_namespace);
2085 : 214832 : unsigned result = strtab.pos;
2086 : :
2087 : 214832 : strtab_write (decl, is_defn);
2088 : 214832 : strtab_write ("", 1);
2089 : :
2090 : 214832 : 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 : 249784 : elf_out::add (unsigned type, unsigned name, unsigned off, unsigned size,
2099 : : unsigned flags)
2100 : : {
2101 : 249784 : gcc_checking_assert (!(off & (SECTION_ALIGN - 1)));
2102 : 249784 : if (sectab.pos + sizeof (section) > sectab.size)
2103 : 4355 : data::simple_memory.grow (sectab, sectab.pos + sizeof (section), false);
2104 : 249784 : section *sec = reinterpret_cast<section *> (sectab.buffer + sectab.pos);
2105 : 249784 : memset (sec, 0, sizeof (section));
2106 : 249784 : sec->type = type;
2107 : 249784 : sec->flags = flags;
2108 : 249784 : sec->name = name;
2109 : 249784 : sec->offset = off;
2110 : 249784 : sec->size = size;
2111 : 249784 : if (flags & SHF_STRINGS)
2112 : 5003 : sec->entsize = 1;
2113 : :
2114 : 249784 : unsigned res = sectab.pos;
2115 : 249784 : sectab.pos += sizeof (section);
2116 : 249784 : 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 : 10070 : elf_out::write (const data &buffer)
2124 : : {
2125 : : #if MAPPED_WRITING
2126 : : /* HDR is always mapped. */
2127 : 10070 : if (&buffer != &hdr)
2128 : : {
2129 : 5038 : bytes_out out (this);
2130 : 5038 : grow (out, buffer.pos, true);
2131 : 5038 : if (out.buffer)
2132 : 5038 : memcpy (out.buffer, buffer.buffer, buffer.pos);
2133 : 5038 : shrink (out);
2134 : 5038 : }
2135 : : else
2136 : : /* We should have been aligned during the first allocation. */
2137 : 5032 : 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 : 10070 : unsigned res = pos;
2146 : 10070 : pos += buffer.pos;
2147 : :
2148 : 10070 : 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 : 8591 : pos += padding;
2158 : : }
2159 : 10070 : 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 : 244752 : elf_out::write (const bytes_out &buf)
2167 : : {
2168 : 244752 : gcc_checking_assert (buf.memory == this);
2169 : : /* A directly mapped buffer. */
2170 : 244752 : gcc_checking_assert (buf.buffer - hdr.buffer >= 0
2171 : : && buf.buffer - hdr.buffer + buf.size <= extent);
2172 : 244752 : unsigned res = pos;
2173 : 244752 : pos += buf.pos;
2174 : :
2175 : : /* Align up. We're not going to advance into the next page. */
2176 : 244752 : pos += -pos & (SECTION_ALIGN - 1);
2177 : :
2178 : 244752 : 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 : 244752 : elf_out::add (const bytes_out &data, bool string_p, unsigned name)
2189 : : {
2190 : 244752 : unsigned off = write (data);
2191 : :
2192 : 489504 : return add (string_p ? SHT_STRTAB : SHT_PROGBITS, name,
2193 : 244752 : 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 : 2513 : elf_out::begin ()
2201 : : {
2202 : 2513 : if (!parent::begin ())
2203 : : return false;
2204 : :
2205 : : /* Let the allocators pick a default. */
2206 : 2513 : data::simple_memory.grow (strtab, 0, false);
2207 : 2513 : data::simple_memory.grow (sectab, 0, false);
2208 : :
2209 : : /* The string table starts with an empty string. */
2210 : 2513 : name ("");
2211 : :
2212 : : /* Create the UNDEF section. */
2213 : 2513 : add (SHT_NONE);
2214 : :
2215 : : #if MAPPED_WRITING
2216 : : /* Start a mapping. */
2217 : 2513 : create_mapping (EXPERIMENT (page_size,
2218 : : (32767 + page_size) & ~(page_size - 1)));
2219 : 2513 : if (!hdr.buffer)
2220 : : return false;
2221 : : #endif
2222 : :
2223 : : /* Write an empty header. */
2224 : 2513 : grow (hdr, sizeof (header), true);
2225 : 2513 : header *h = reinterpret_cast<header *> (hdr.buffer);
2226 : 2513 : memset (h, 0, sizeof (header));
2227 : 2513 : hdr.pos = hdr.size;
2228 : 2513 : write (hdr);
2229 : 2513 : 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 : 2622 : elf_out::end ()
2237 : : {
2238 : 2622 : if (fd >= 0)
2239 : : {
2240 : : /* Write the string table. */
2241 : 2519 : unsigned strnam = name (".strtab");
2242 : 2519 : unsigned stroff = write (strtab);
2243 : 2519 : unsigned strndx = add (SHT_STRTAB, strnam, stroff, strtab.pos,
2244 : : SHF_STRINGS);
2245 : :
2246 : : /* Store escape values in section[0]. */
2247 : 2519 : if (strndx >= SHN_LORESERVE)
2248 : : {
2249 : 0 : reinterpret_cast<section *> (sectab.buffer)->link = strndx;
2250 : 0 : strndx = SHN_XINDEX;
2251 : : }
2252 : 2519 : unsigned shnum = sectab.pos / sizeof (section);
2253 : 2519 : if (shnum >= SHN_LORESERVE)
2254 : : {
2255 : 0 : reinterpret_cast<section *> (sectab.buffer)->size = shnum;
2256 : 0 : shnum = SHN_XINDEX;
2257 : : }
2258 : :
2259 : 2519 : unsigned shoff = write (sectab);
2260 : :
2261 : : #if MAPPED_WRITING
2262 : 2519 : if (offset)
2263 : : {
2264 : 126 : remove_mapping ();
2265 : 126 : offset = 0;
2266 : 126 : create_mapping ((sizeof (header) + page_size - 1) & ~(page_size - 1),
2267 : : false);
2268 : : }
2269 : 2519 : unsigned length = pos;
2270 : : #else
2271 : : if (lseek (fd, 0, SEEK_SET) < 0)
2272 : : set_error (errno);
2273 : : #endif
2274 : : /* Write header. */
2275 : 2519 : if (!get_error ())
2276 : : {
2277 : : /* Write the correct header now. */
2278 : 2519 : header *h = reinterpret_cast<header *> (hdr.buffer);
2279 : 2519 : h->ident.magic[0] = 0x7f;
2280 : 2519 : h->ident.magic[1] = 'E'; /* Elrond */
2281 : 2519 : h->ident.magic[2] = 'L'; /* is an */
2282 : 2519 : h->ident.magic[3] = 'F'; /* elf. */
2283 : 2519 : h->ident.klass = MY_CLASS;
2284 : 2519 : h->ident.data = MY_ENDIAN;
2285 : 2519 : h->ident.version = EV_CURRENT;
2286 : 2519 : h->ident.osabi = OSABI_NONE;
2287 : 2519 : h->type = ET_NONE;
2288 : 2519 : h->machine = EM_NONE;
2289 : 2519 : h->version = EV_CURRENT;
2290 : 2519 : h->shoff = shoff;
2291 : 2519 : h->ehsize = sizeof (header);
2292 : 2519 : h->shentsize = sizeof (section);
2293 : 2519 : h->shnum = shnum;
2294 : 2519 : h->shstrndx = strndx;
2295 : :
2296 : 2519 : pos = 0;
2297 : 2519 : write (hdr);
2298 : : }
2299 : :
2300 : : #if MAPPED_WRITING
2301 : 2519 : remove_mapping ();
2302 : 2519 : if (ftruncate (fd, length))
2303 : 0 : set_error (errno);
2304 : : #endif
2305 : : }
2306 : :
2307 : 2622 : data::simple_memory.shrink (sectab);
2308 : 2622 : data::simple_memory.shrink (strtab);
2309 : :
2310 : 2622 : 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, /* It is a TU-local entity. */
2369 : : DB_REFS_TU_LOCAL_BIT, /* Refers to a TU-local entity (but is not
2370 : : necessarily an exposure.) */
2371 : : DB_EXPOSURE_BIT, /* Exposes a TU-local entity. */
2372 : : DB_IMPORTED_BIT, /* An imported entity. */
2373 : : DB_UNREACHED_BIT, /* A yet-to-be reached entity. */
2374 : : DB_MAYBE_RECURSIVE_BIT, /* An entity maybe in a recursive cluster. */
2375 : : DB_ENTRY_BIT, /* The first reached recursive dep. */
2376 : : DB_HIDDEN_BIT, /* A hidden binding. */
2377 : : /* The following bits are not independent, but enumerating them is
2378 : : awkward. */
2379 : : DB_TYPE_SPEC_BIT, /* Specialization in the type table. */
2380 : : DB_FRIEND_SPEC_BIT, /* An instantiated template friend. */
2381 : : DB_HWM,
2382 : : };
2383 : : static_assert (DB_HWM <= sizeof(discriminator) * CHAR_BIT,
2384 : : "not enough bits in discriminator");
2385 : :
2386 : : public:
2387 : : /* The first slot is special for EK_SPECIALIZATIONS it is a
2388 : : spec_entry pointer. It is not relevant for the SCC
2389 : : determination. */
2390 : : vec<depset *> deps; /* Depsets we reference. */
2391 : :
2392 : : public:
2393 : : unsigned cluster; /* Strongly connected cluster, later entity number */
2394 : : unsigned section; /* Section written to. */
2395 : : /* During SCC construction, section is lowlink, until the depset is
2396 : : removed from the stack. See Tarjan algorithm for details. */
2397 : :
2398 : : private:
2399 : : /* Construction via factories. Destruction via hash traits. */
2400 : : depset (tree entity);
2401 : : ~depset ();
2402 : :
2403 : : public:
2404 : : static depset *make_binding (tree, tree);
2405 : : static depset *make_entity (tree, entity_kind, bool = false);
2406 : : /* Late setting a binding name -- /then/ insert into hash! */
2407 : : inline void set_binding_name (tree name)
2408 : : {
2409 : : gcc_checking_assert (!get_name ());
2410 : : discriminator = reinterpret_cast<uintptr_t> (name);
2411 : : }
2412 : :
2413 : : private:
2414 : 3436678 : template<unsigned I> void set_flag_bit ()
2415 : : {
2416 : 0 : gcc_checking_assert (I < 2 || !is_binding ());
2417 : 3436678 : discriminator |= 1u << I;
2418 : 2056781 : }
2419 : 287488 : template<unsigned I> void clear_flag_bit ()
2420 : : {
2421 : 0 : gcc_checking_assert (I < 2 || !is_binding ());
2422 : 287488 : discriminator &= ~(1u << I);
2423 : 287488 : }
2424 : 359075002 : template<unsigned I> bool get_flag_bit () const
2425 : : {
2426 : 0 : gcc_checking_assert (I < 2 || !is_binding ());
2427 : 432965752 : return bool ((discriminator >> I) & 1);
2428 : : }
2429 : :
2430 : : public:
2431 : 351572296 : bool is_binding () const
2432 : : {
2433 : 76235019 : return !get_flag_bit<DB_ZERO_BIT> ();
2434 : : }
2435 : 195581613 : entity_kind get_entity_kind () const
2436 : : {
2437 : 13602 : if (is_binding ())
2438 : : return EK_BINDING;
2439 : 147529269 : return entity_kind ((discriminator >> DB_KIND_BIT) & ((1u << EK_BITS) - 1));
2440 : : }
2441 : : const char *entity_kind_name () const;
2442 : :
2443 : : public:
2444 : 6519912 : bool has_defn () const
2445 : : {
2446 : : /* Never consider TU-local entities as having definitions, since
2447 : : we will never be accessing them from importers anyway. */
2448 : 8685536 : return get_flag_bit<DB_DEFN_BIT> () && !is_tu_local ();
2449 : : }
2450 : :
2451 : : public:
2452 : : /* This entity might be found other than by namespace-scope lookup;
2453 : : see module_state::write_pendings for more details. */
2454 : 1599400 : bool is_pending_entity () const
2455 : : {
2456 : 2570586 : return (get_entity_kind () == EK_SPECIALIZATION
2457 : 971186 : || get_entity_kind () == EK_PARTIAL
2458 : 2542030 : || (get_entity_kind () == EK_DECL
2459 : 924809 : && get_flag_bit<DB_IS_PENDING_BIT> ()));
2460 : : }
2461 : : public:
2462 : 29493378 : bool is_tu_local () const
2463 : : {
2464 : 16947455 : return get_flag_bit<DB_TU_LOCAL_BIT> ();
2465 : : }
2466 : 696109 : bool refs_tu_local () const
2467 : : {
2468 : 83217 : return get_flag_bit<DB_REFS_TU_LOCAL_BIT> ();
2469 : : }
2470 : 1106816 : bool is_exposure () const
2471 : : {
2472 : 1106816 : return get_flag_bit<DB_EXPOSURE_BIT> ();
2473 : : }
2474 : 19628793 : bool is_import () const
2475 : : {
2476 : 5981839 : return get_flag_bit<DB_IMPORTED_BIT> ();
2477 : : }
2478 : 11980255 : bool is_unreached () const
2479 : : {
2480 : 804074 : return get_flag_bit<DB_UNREACHED_BIT> ();
2481 : : }
2482 : 1008505 : bool is_hidden () const
2483 : : {
2484 : 1008505 : return get_flag_bit<DB_HIDDEN_BIT> ();
2485 : : }
2486 : 646847 : bool is_maybe_recursive () const
2487 : : {
2488 : 646847 : return get_flag_bit<DB_MAYBE_RECURSIVE_BIT> ();
2489 : : }
2490 : 822 : bool is_entry () const
2491 : : {
2492 : 822 : return get_flag_bit<DB_ENTRY_BIT> ();
2493 : : }
2494 : 942252 : bool is_type_spec () const
2495 : : {
2496 : 942252 : return get_flag_bit<DB_TYPE_SPEC_BIT> ();
2497 : : }
2498 : 942252 : bool is_friend_spec () const
2499 : : {
2500 : 942252 : return get_flag_bit<DB_FRIEND_SPEC_BIT> ();
2501 : : }
2502 : :
2503 : : public:
2504 : : /* We set these bit outside of depset. */
2505 : 4690 : void set_hidden_binding ()
2506 : : {
2507 : 4690 : set_flag_bit<DB_HIDDEN_BIT> ();
2508 : 4690 : }
2509 : 30 : void clear_hidden_binding ()
2510 : : {
2511 : 30 : clear_flag_bit<DB_HIDDEN_BIT> ();
2512 : 30 : }
2513 : :
2514 : : public:
2515 : 7502706 : bool is_special () const
2516 : : {
2517 : 7502706 : return get_flag_bit<DB_SPECIAL_BIT> ();
2518 : : }
2519 : 1379897 : void set_special ()
2520 : : {
2521 : 1379897 : set_flag_bit<DB_SPECIAL_BIT> ();
2522 : 0 : }
2523 : :
2524 : : public:
2525 : 88549270 : tree get_entity () const
2526 : : {
2527 : 88549270 : return entity;
2528 : : }
2529 : 14780187 : tree get_name () const
2530 : : {
2531 : 14780187 : gcc_checking_assert (is_binding ());
2532 : 14780187 : return reinterpret_cast <tree> (discriminator);
2533 : : }
2534 : :
2535 : : public:
2536 : : /* Traits for a hash table of pointers to bindings. */
2537 : : struct traits {
2538 : : /* Each entry is a pointer to a depset. */
2539 : : typedef depset *value_type;
2540 : : /* We lookup by container:maybe-identifier pair. */
2541 : : typedef std::pair<tree,tree> compare_type;
2542 : :
2543 : : static const bool empty_zero_p = true;
2544 : :
2545 : : /* hash and equality for compare_type. */
2546 : 12035187 : inline static hashval_t hash (const compare_type &p)
2547 : : {
2548 : 12035187 : hashval_t h = pointer_hash<tree_node>::hash (p.first);
2549 : 12035187 : if (p.second)
2550 : : {
2551 : 153145 : hashval_t nh = IDENTIFIER_HASH_VALUE (p.second);
2552 : 153145 : h = iterative_hash_hashval_t (h, nh);
2553 : : }
2554 : 12035187 : return h;
2555 : : }
2556 : 56859873 : inline static bool equal (const value_type b, const compare_type &p)
2557 : : {
2558 : 56859873 : if (b->entity != p.first)
2559 : : return false;
2560 : :
2561 : 8530506 : if (p.second)
2562 : 11156 : return b->discriminator == reinterpret_cast<uintptr_t> (p.second);
2563 : : else
2564 : 8519350 : return !b->is_binding ();
2565 : : }
2566 : :
2567 : : /* (re)hasher for a binding itself. */
2568 : 43607808 : inline static hashval_t hash (const value_type b)
2569 : : {
2570 : 43607808 : hashval_t h = pointer_hash<tree_node>::hash (b->entity);
2571 : 43607808 : if (b->is_binding ())
2572 : : {
2573 : 2966025 : hashval_t nh = IDENTIFIER_HASH_VALUE (b->get_name ());
2574 : 2966025 : h = iterative_hash_hashval_t (h, nh);
2575 : : }
2576 : 43607808 : return h;
2577 : : }
2578 : :
2579 : : /* Empty via NULL. */
2580 : 0 : static inline void mark_empty (value_type &p) {p = NULL;}
2581 : : static inline bool is_empty (value_type p) {return !p;}
2582 : :
2583 : : /* Nothing is deletable. Everything is insertable. */
2584 : : static bool is_deleted (value_type) { return false; }
2585 : : static void mark_deleted (value_type) { gcc_unreachable (); }
2586 : :
2587 : : /* We own the entities in the hash table. */
2588 : 2016011 : static void remove (value_type p)
2589 : : {
2590 : 2016011 : delete (p);
2591 : 2016011 : }
2592 : : };
2593 : :
2594 : : public:
2595 : : class hash : public hash_table<traits> {
2596 : : typedef traits::compare_type key_t;
2597 : : typedef hash_table<traits> parent;
2598 : :
2599 : : public:
2600 : : vec<depset *> worklist; /* Worklist of decls to walk. */
2601 : : hash *chain; /* Original table. */
2602 : : depset *current; /* Current depset being depended. */
2603 : : unsigned section; /* When writing out, the section. */
2604 : : bool reached_unreached; /* We reached an unreached entity. */
2605 : : bool writing_merge_key; /* We're writing merge key information. */
2606 : : bool ignore_tu_local; /* In a context where referencing a TU-local
2607 : : entity is not an exposure. */
2608 : :
2609 : : public:
2610 : 225133 : hash (size_t size, hash *c = NULL)
2611 : 450266 : : parent (size), chain (c), current (NULL), section (0),
2612 : 225133 : reached_unreached (false), writing_merge_key (false),
2613 : 225133 : ignore_tu_local (false)
2614 : : {
2615 : 225133 : worklist.create (size);
2616 : 225133 : }
2617 : 225133 : ~hash ()
2618 : : {
2619 : 2513 : worklist.release ();
2620 : 225133 : }
2621 : :
2622 : : public:
2623 : 67713816 : bool is_key_order () const
2624 : : {
2625 : 67713816 : return chain != NULL;
2626 : : }
2627 : :
2628 : : private:
2629 : : depset **entity_slot (tree entity, bool = true);
2630 : : depset **binding_slot (tree ctx, tree name, bool = true);
2631 : : depset *maybe_add_declaration (tree decl);
2632 : :
2633 : : public:
2634 : : depset *find_dependency (tree entity);
2635 : : depset *find_binding (tree ctx, tree name);
2636 : : depset *make_dependency (tree decl, entity_kind);
2637 : : void add_dependency (depset *);
2638 : :
2639 : : public:
2640 : : void add_mergeable (depset *);
2641 : : depset *add_dependency (tree decl, entity_kind);
2642 : : void add_namespace_context (depset *, tree ns);
2643 : :
2644 : : private:
2645 : : bool has_tu_local_tmpl_arg (tree decl, tree args, bool explain);
2646 : : bool is_tu_local_entity (tree decl, bool explain = false);
2647 : : bool is_tu_local_value (tree decl, tree expr, bool explain = false);
2648 : :
2649 : : private:
2650 : : static bool add_binding_entity (tree, WMB_Flags, void *);
2651 : :
2652 : : public:
2653 : : bool add_namespace_entities (tree ns, bitmap partitions);
2654 : : void add_specializations (bool decl_p);
2655 : : void add_partial_entities (vec<tree, va_gc> *);
2656 : : void add_class_entities (vec<tree, va_gc> *);
2657 : :
2658 : : private:
2659 : : void add_deduction_guides (tree decl);
2660 : :
2661 : : public:
2662 : : void find_dependencies (module_state *);
2663 : : bool finalize_dependencies ();
2664 : : vec<depset *> connect ();
2665 : : };
2666 : :
2667 : : public:
2668 : : struct tarjan {
2669 : : vec<depset *> result;
2670 : : vec<depset *> stack;
2671 : : unsigned index;
2672 : :
2673 : 225104 : tarjan (unsigned size)
2674 : 225104 : : index (0)
2675 : : {
2676 : 225104 : result.create (size);
2677 : 225104 : stack.create (50);
2678 : 225104 : }
2679 : 225104 : ~tarjan ()
2680 : : {
2681 : 225104 : gcc_assert (!stack.length ());
2682 : 225104 : stack.release ();
2683 : 225104 : }
2684 : :
2685 : : public:
2686 : : void connect (depset *);
2687 : : };
2688 : : };
2689 : :
2690 : : inline
2691 : 2016011 : depset::depset (tree entity)
2692 : 2016011 : :entity (entity), discriminator (0), cluster (0), section (0)
2693 : : {
2694 : 2016011 : deps.create (0);
2695 : : }
2696 : :
2697 : : inline
2698 : 2016011 : depset::~depset ()
2699 : : {
2700 : 2016011 : deps.release ();
2701 : 2016011 : }
2702 : :
2703 : : const char *
2704 : 42396 : depset::entity_kind_name () const
2705 : : {
2706 : : /* Same order as entity_kind. */
2707 : 42396 : static const char *const names[] =
2708 : : {"decl", "specialization", "partial", "using",
2709 : : "namespace", "tu-local", "redirect", "binding"};
2710 : 42396 : static_assert (ARRAY_SIZE (names) == EK_EXPLICIT_HWM + 1,
2711 : : "names must have an entry for every explicit entity_kind");
2712 : 42396 : entity_kind kind = get_entity_kind ();
2713 : 42396 : gcc_checking_assert (kind < ARRAY_SIZE (names));
2714 : 42396 : return names[kind];
2715 : : }
2716 : :
2717 : : /* Create a depset for a namespace binding NS::NAME. */
2718 : :
2719 : 118678 : depset *depset::make_binding (tree ns, tree name)
2720 : : {
2721 : 118678 : depset *binding = new depset (ns);
2722 : :
2723 : 118678 : binding->discriminator = reinterpret_cast <uintptr_t> (name);
2724 : :
2725 : 118678 : return binding;
2726 : : }
2727 : :
2728 : 1897333 : depset *depset::make_entity (tree entity, entity_kind ek, bool is_defn)
2729 : : {
2730 : 1897333 : depset *r = new depset (entity);
2731 : :
2732 : 1897333 : r->discriminator = ((1 << DB_ZERO_BIT)
2733 : 1897333 : | (ek << DB_KIND_BIT)
2734 : 1897333 : | is_defn << DB_DEFN_BIT);
2735 : :
2736 : 1897333 : return r;
2737 : : }
2738 : :
2739 : : class pending_key
2740 : : {
2741 : : public:
2742 : : tree ns;
2743 : : tree id;
2744 : : };
2745 : :
2746 : : template<>
2747 : : struct default_hash_traits<pending_key>
2748 : : {
2749 : : using value_type = pending_key;
2750 : :
2751 : : static const bool empty_zero_p = false;
2752 : 34011858 : static hashval_t hash (const value_type &k)
2753 : : {
2754 : 34011858 : hashval_t h = IDENTIFIER_HASH_VALUE (k.id);
2755 : 34011858 : h = iterative_hash_hashval_t (DECL_UID (k.ns), h);
2756 : :
2757 : 34011858 : return h;
2758 : : }
2759 : 10888156 : static bool equal (const value_type &k, const value_type &l)
2760 : : {
2761 : 10888156 : return k.ns == l.ns && k.id == l.id;
2762 : : }
2763 : 175631 : static void mark_empty (value_type &k)
2764 : : {
2765 : 175631 : k.ns = k.id = NULL_TREE;
2766 : : }
2767 : 4847 : static void mark_deleted (value_type &k)
2768 : : {
2769 : 4847 : k.ns = NULL_TREE;
2770 : 4847 : gcc_checking_assert (k.id);
2771 : 4847 : }
2772 : 252313231 : static bool is_empty (const value_type &k)
2773 : : {
2774 : 252273320 : return k.ns == NULL_TREE && k.id == NULL_TREE;
2775 : : }
2776 : 15300699 : static bool is_deleted (const value_type &k)
2777 : : {
2778 : 15300699 : return k.ns == NULL_TREE && k.id != NULL_TREE;
2779 : : }
2780 : : static void remove (value_type &)
2781 : : {
2782 : : }
2783 : : };
2784 : :
2785 : : typedef hash_map<pending_key, auto_vec<unsigned>> pending_map_t;
2786 : :
2787 : : /* Not-loaded entities that are keyed to a namespace-scope
2788 : : identifier. See module_state::write_pendings for details. */
2789 : : pending_map_t *pending_table;
2790 : :
2791 : : /* Decls that need some post processing once a batch of lazy loads has
2792 : : completed. */
2793 : : vec<tree, va_heap, vl_embed> *post_load_decls;
2794 : :
2795 : : /* Some entities are keyed to another entitity for ODR purposes.
2796 : : For example, at namespace scope, 'inline auto var = []{};', that
2797 : : lambda is keyed to 'var', and follows its ODRness. */
2798 : : typedef hash_map<tree, auto_vec<tree>> keyed_map_t;
2799 : : static keyed_map_t *keyed_table;
2800 : :
2801 : : /* Instantiations of temploid friends imported from another module
2802 : : need to be attached to the same module as the temploid. This maps
2803 : : these decls to the temploid they are instantiated from, as there is
2804 : : no other easy way to get this information. */
2805 : : static GTY((cache)) decl_tree_cache_map *imported_temploid_friends;
2806 : :
2807 : : /********************************************************************/
2808 : : /* Tree streaming. The tree streaming is very specific to the tree
2809 : : structures themselves. A tag indicates the kind of tree being
2810 : : streamed. -ve tags indicate backreferences to already-streamed
2811 : : trees. Backreferences are auto-numbered. */
2812 : :
2813 : : /* Tree tags. */
2814 : : enum tree_tag {
2815 : : tt_null, /* NULL_TREE. */
2816 : : tt_tu_local, /* A TU-local entity. */
2817 : : tt_fixed, /* Fixed vector index. */
2818 : :
2819 : : tt_node, /* By-value node. */
2820 : : tt_decl, /* By-value mergeable decl. */
2821 : : tt_tpl_parm, /* Template parm. */
2822 : :
2823 : : /* The ordering of the following 5 is relied upon in
2824 : : trees_out::tree_node. */
2825 : : tt_id, /* Identifier node. */
2826 : : tt_conv_id, /* Conversion operator name. */
2827 : : tt_anon_id, /* Anonymous name. */
2828 : : tt_lambda_id, /* Lambda name. */
2829 : : tt_internal_id, /* Internal name. */
2830 : :
2831 : : tt_typedef_type, /* A (possibly implicit) typedefed type. */
2832 : : tt_derived_type, /* A type derived from another type. */
2833 : : tt_variant_type, /* A variant of another type. */
2834 : :
2835 : : tt_tinfo_var, /* Typeinfo object. */
2836 : : tt_tinfo_typedef, /* Typeinfo typedef. */
2837 : : tt_ptrmem_type, /* Pointer to member type. */
2838 : : tt_nttp_var, /* NTTP_OBJECT VAR_DECL. */
2839 : :
2840 : : tt_parm, /* Function parameter or result. */
2841 : : tt_enum_value, /* An enum value. */
2842 : : tt_enum_decl, /* An enum decl. */
2843 : : tt_data_member, /* Data member/using-decl. */
2844 : :
2845 : : tt_binfo, /* A BINFO. */
2846 : : tt_vtable, /* A vtable. */
2847 : : tt_thunk, /* A thunk. */
2848 : : tt_clone_ref,
2849 : :
2850 : : tt_entity, /* A extra-cluster entity. */
2851 : :
2852 : : tt_template, /* The TEMPLATE_RESULT of a template. */
2853 : : };
2854 : :
2855 : : enum walk_kind {
2856 : : WK_none, /* No walk to do (a back- or fixed-ref happened). */
2857 : : WK_normal, /* Normal walk (by-name if possible). */
2858 : :
2859 : : WK_value, /* By-value walk. */
2860 : : };
2861 : :
2862 : : enum merge_kind
2863 : : {
2864 : : MK_unique, /* Known unique. */
2865 : : MK_named, /* Found by CTX, NAME + maybe_arg types etc. */
2866 : : MK_field, /* Found by CTX and index on TYPE_FIELDS */
2867 : : MK_vtable, /* Found by CTX and index on TYPE_VTABLES */
2868 : : MK_as_base, /* Found by CTX. */
2869 : :
2870 : : MK_partial,
2871 : :
2872 : : MK_enum, /* Found by CTX, & 1stMemberNAME. */
2873 : : MK_keyed, /* Found by key & index. */
2874 : : MK_local_type, /* Found by CTX, index. */
2875 : :
2876 : : MK_friend_spec, /* Like named, but has a tmpl & args too. */
2877 : : MK_local_friend, /* Found by CTX, index. */
2878 : :
2879 : : MK_indirect_lwm = MK_enum,
2880 : :
2881 : : /* Template specialization kinds below. These are all found via
2882 : : primary template and specialization args. */
2883 : : MK_template_mask = 0x10, /* A template specialization. */
2884 : :
2885 : : MK_tmpl_decl_mask = 0x4, /* In decl table. */
2886 : :
2887 : : MK_tmpl_tmpl_mask = 0x1, /* We want TEMPLATE_DECL. */
2888 : :
2889 : : MK_type_spec = MK_template_mask,
2890 : : MK_decl_spec = MK_template_mask | MK_tmpl_decl_mask,
2891 : :
2892 : : MK_hwm = 0x20
2893 : : };
2894 : : /* This is more than a debugging array. NULLs are used to determine
2895 : : an invalid merge_kind number. */
2896 : : static char const *const merge_kind_name[MK_hwm] =
2897 : : {
2898 : : "unique", "named", "field", "vtable", /* 0...3 */
2899 : : "asbase", "partial", "enum", "attached", /* 4...7 */
2900 : :
2901 : : "local type", "friend spec", "local friend", NULL, /* 8...11 */
2902 : : NULL, NULL, NULL, NULL,
2903 : :
2904 : : "type spec", "type tmpl spec", /* 16,17 type (template). */
2905 : : NULL, NULL,
2906 : :
2907 : : "decl spec", "decl tmpl spec", /* 20,21 decl (template). */
2908 : : NULL, NULL,
2909 : : NULL, NULL, NULL, NULL,
2910 : : NULL, NULL, NULL, NULL,
2911 : : };
2912 : :
2913 : : /* Mergeable entity location data. */
2914 : : struct merge_key {
2915 : : cp_ref_qualifier ref_q : 2;
2916 : : unsigned index;
2917 : :
2918 : : tree ret; /* Return type, if appropriate. */
2919 : : tree args; /* Arg types, if appropriate. */
2920 : :
2921 : : tree constraints; /* Constraints. */
2922 : :
2923 : 2110330 : merge_key ()
2924 : 2110330 : :ref_q (REF_QUAL_NONE), index (0),
2925 : 2110330 : ret (NULL_TREE), args (NULL_TREE),
2926 : 2110330 : constraints (NULL_TREE)
2927 : : {
2928 : : }
2929 : : };
2930 : :
2931 : : /* Hashmap of merged duplicates. Usually decls, but can contain
2932 : : BINFOs. */
2933 : : typedef hash_map<tree,uintptr_t,
2934 : : simple_hashmap_traits<nodel_ptr_hash<tree_node>,uintptr_t> >
2935 : : duplicate_hash_map;
2936 : :
2937 : : /* Data needed for post-processing. */
2938 : : struct post_process_data {
2939 : : tree decl;
2940 : : location_t start_locus;
2941 : : location_t end_locus;
2942 : : bool returns_value;
2943 : : bool returns_null;
2944 : : bool returns_abnormally;
2945 : : bool infinite_loop;
2946 : : };
2947 : :
2948 : : /* Tree stream reader. Note that reading a stream doesn't mark the
2949 : : read trees with TREE_VISITED. Thus it's quite safe to have
2950 : : multiple concurrent readers. Which is good, because lazy
2951 : : loading.
2952 : :
2953 : : It's important that trees_in/out have internal linkage so that the
2954 : : compiler knows core_bools, lang_type_bools and lang_decl_bools have
2955 : : only a single caller (tree_node_bools) and inlines them appropriately. */
2956 : : namespace {
2957 : : class trees_in : public bytes_in {
2958 : : typedef bytes_in parent;
2959 : :
2960 : : private:
2961 : : module_state *state; /* Module being imported. */
2962 : : vec<tree> back_refs; /* Back references. */
2963 : : duplicate_hash_map *duplicates; /* Map from existings to duplicate. */
2964 : : vec<post_process_data> post_decls; /* Decls to post process. */
2965 : : unsigned unused; /* Inhibit any interior TREE_USED
2966 : : marking. */
2967 : :
2968 : : public:
2969 : : trees_in (module_state *);
2970 : : ~trees_in ();
2971 : :
2972 : : public:
2973 : : int insert (tree);
2974 : : tree back_ref (int);
2975 : :
2976 : : private:
2977 : : tree start (unsigned = 0);
2978 : :
2979 : : public:
2980 : : /* Needed for binfo writing */
2981 : : bool core_bools (tree, bits_in&);
2982 : :
2983 : : private:
2984 : : /* Stream tree_core, lang_decl_specific and lang_type_specific
2985 : : bits. */
2986 : : bool core_vals (tree);
2987 : : bool lang_type_bools (tree, bits_in&);
2988 : : bool lang_type_vals (tree);
2989 : : bool lang_decl_bools (tree, bits_in&);
2990 : : bool lang_decl_vals (tree);
2991 : : bool lang_vals (tree);
2992 : : bool tree_node_bools (tree);
2993 : : bool tree_node_vals (tree);
2994 : : tree tree_value ();
2995 : : tree decl_value ();
2996 : : tree tpl_parm_value ();
2997 : :
2998 : : private:
2999 : : tree chained_decls (); /* Follow DECL_CHAIN. */
3000 : : vec<tree, va_heap> *vec_chained_decls ();
3001 : : vec<tree, va_gc> *tree_vec (); /* vec of tree. */
3002 : : vec<tree_pair_s, va_gc> *tree_pair_vec (); /* vec of tree_pair. */
3003 : : tree tree_list (bool has_purpose);
3004 : :
3005 : : public:
3006 : : /* Read a tree node. */
3007 : : tree tree_node (bool is_use = false);
3008 : :
3009 : : private:
3010 : : bool install_entity (tree decl);
3011 : : tree tpl_parms (unsigned &tpl_levels);
3012 : : bool tpl_parms_fini (tree decl, unsigned tpl_levels);
3013 : : bool tpl_header (tree decl, unsigned *tpl_levels);
3014 : : int fn_parms_init (tree);
3015 : : void fn_parms_fini (int tag, tree fn, tree existing, bool has_defn);
3016 : : unsigned add_indirect_tpl_parms (tree);
3017 : : public:
3018 : : bool add_indirects (tree);
3019 : :
3020 : : public:
3021 : : /* Serialize various definitions. */
3022 : : bool read_definition (tree decl);
3023 : :
3024 : : private:
3025 : : void check_abi_tags (tree existing, tree decl, tree &eattr, tree &dattr);
3026 : : bool is_matching_decl (tree existing, tree decl, bool is_typedef);
3027 : : static bool install_implicit_member (tree decl);
3028 : : bool read_function_def (tree decl, tree maybe_template);
3029 : : bool read_var_def (tree decl, tree maybe_template);
3030 : : bool read_class_def (tree decl, tree maybe_template);
3031 : : bool read_enum_def (tree decl, tree maybe_template);
3032 : :
3033 : : public:
3034 : : tree decl_container ();
3035 : : tree key_mergeable (int tag, merge_kind, tree decl, tree inner, tree type,
3036 : : tree container, bool is_attached,
3037 : : bool is_imported_temploid_friend);
3038 : : unsigned binfo_mergeable (tree *);
3039 : :
3040 : : private:
3041 : : tree key_local_type (const merge_key&, tree, tree);
3042 : : uintptr_t *find_duplicate (tree existing);
3043 : : void register_duplicate (tree decl, tree existing);
3044 : : /* Mark as an already diagnosed bad duplicate. */
3045 : 54 : void unmatched_duplicate (tree existing)
3046 : : {
3047 : 108 : *find_duplicate (existing) |= 1;
3048 : 54 : }
3049 : :
3050 : : public:
3051 : 129182 : bool is_duplicate (tree decl)
3052 : : {
3053 : 258364 : return find_duplicate (decl) != NULL;
3054 : : }
3055 : 115164 : tree maybe_duplicate (tree decl)
3056 : : {
3057 : 115164 : if (uintptr_t *dup = find_duplicate (decl))
3058 : 8688 : return reinterpret_cast<tree> (*dup & ~uintptr_t (1));
3059 : : return decl;
3060 : : }
3061 : : tree odr_duplicate (tree decl, bool has_defn);
3062 : :
3063 : : public:
3064 : : /* Return the decls to postprocess. */
3065 : : const vec<post_process_data>& post_process ()
3066 : : {
3067 : : return post_decls;
3068 : : }
3069 : : private:
3070 : : /* Register DATA for postprocessing. */
3071 : 71909 : void post_process (post_process_data data)
3072 : : {
3073 : 71909 : post_decls.safe_push (data);
3074 : : }
3075 : :
3076 : : private:
3077 : : void assert_definition (tree, bool installing);
3078 : : };
3079 : : } // anon namespace
3080 : :
3081 : 169159 : trees_in::trees_in (module_state *state)
3082 : 169159 : :parent (), state (state), unused (0)
3083 : : {
3084 : 169159 : duplicates = NULL;
3085 : 169159 : back_refs.create (500);
3086 : 169159 : post_decls.create (0);
3087 : 169159 : }
3088 : :
3089 : 169159 : trees_in::~trees_in ()
3090 : : {
3091 : 264142 : delete (duplicates);
3092 : 169159 : back_refs.release ();
3093 : 169159 : post_decls.release ();
3094 : 169159 : }
3095 : :
3096 : : /* Tree stream writer. */
3097 : : namespace {
3098 : : class trees_out : public bytes_out {
3099 : : typedef bytes_out parent;
3100 : :
3101 : : private:
3102 : : module_state *state; /* The module we are writing. */
3103 : : ptr_int_hash_map tree_map; /* Trees to references */
3104 : : depset::hash *dep_hash; /* Dependency table. */
3105 : : int ref_num; /* Back reference number. */
3106 : : unsigned section;
3107 : : bool writing_local_entities; /* Whether we might walk into a TU-local
3108 : : entity we need to emit placeholders for. */
3109 : : bool walking_bit_field_unit; /* Whether we're walking the underlying
3110 : : storage for a bit field. There's no other
3111 : : great way to detect this. */
3112 : : #if CHECKING_P
3113 : : int importedness; /* Checker that imports not occurring
3114 : : inappropriately. +ve imports ok,
3115 : : -ve imports not ok. */
3116 : : #endif
3117 : :
3118 : : public:
3119 : : trees_out (allocator *, module_state *, depset::hash &deps, unsigned sec = 0);
3120 : : ~trees_out ();
3121 : :
3122 : : private:
3123 : : void mark_trees ();
3124 : : void unmark_trees ();
3125 : :
3126 : : public:
3127 : : /* Hey, let's ignore the well known STL iterator idiom. */
3128 : : void begin ();
3129 : : unsigned end (elf_out *sink, unsigned name, unsigned *crc_ptr);
3130 : : void end ();
3131 : :
3132 : : public:
3133 : : enum tags
3134 : : {
3135 : : tag_backref = -1, /* Upper bound on the backrefs. */
3136 : : tag_value = 0, /* Write by value. */
3137 : : tag_fixed /* Lower bound on the fixed trees. */
3138 : : };
3139 : :
3140 : : public:
3141 : : /* The walk is used for three similar purposes:
3142 : :
3143 : : 1. The initial scan for dependencies.
3144 : : 2. Once dependencies have been found, ordering them.
3145 : : 3. Writing dependencies to file (streaming_p).
3146 : :
3147 : : For cases where it matters, these accessers can be used to determine
3148 : : which state we're in. */
3149 : 47714124 : bool is_initial_scan () const
3150 : : {
3151 : 80564769 : return !streaming_p () && !is_key_order ();
3152 : : }
3153 : 44769442 : bool is_key_order () const
3154 : : {
3155 : 32850645 : return dep_hash->is_key_order ();
3156 : : }
3157 : :
3158 : : public:
3159 : : int insert (tree, walk_kind = WK_normal);
3160 : :
3161 : : private:
3162 : : void start (tree, bool = false);
3163 : :
3164 : : private:
3165 : : walk_kind ref_node (tree);
3166 : : public:
3167 : : int get_tag (tree);
3168 : 445240 : void set_importing (int i ATTRIBUTE_UNUSED)
3169 : : {
3170 : : #if CHECKING_P
3171 : 445240 : importedness = i;
3172 : : #endif
3173 : : }
3174 : :
3175 : : private:
3176 : : void core_bools (tree, bits_out&);
3177 : : void core_vals (tree);
3178 : : void lang_type_bools (tree, bits_out&);
3179 : : void lang_type_vals (tree);
3180 : : void lang_decl_bools (tree, bits_out&);
3181 : : void lang_decl_vals (tree);
3182 : : void lang_vals (tree);
3183 : : void tree_node_bools (tree);
3184 : : void tree_node_vals (tree);
3185 : :
3186 : : private:
3187 : : void chained_decls (tree);
3188 : : void vec_chained_decls (tree);
3189 : : void tree_vec (vec<tree, va_gc> *);
3190 : : void tree_pair_vec (vec<tree_pair_s, va_gc> *);
3191 : : void tree_list (tree, bool has_purpose);
3192 : :
3193 : : private:
3194 : : bool has_tu_local_dep (tree) const;
3195 : : tree find_tu_local_decl (tree);
3196 : :
3197 : : public:
3198 : : /* Mark a node for by-value walking. */
3199 : : void mark_by_value (tree);
3200 : :
3201 : : public:
3202 : : void tree_node (tree);
3203 : :
3204 : : private:
3205 : : void install_entity (tree decl, depset *);
3206 : : void tpl_parms (tree parms, unsigned &tpl_levels);
3207 : : void tpl_parms_fini (tree decl, unsigned tpl_levels);
3208 : : void fn_parms_fini (tree) {}
3209 : : unsigned add_indirect_tpl_parms (tree);
3210 : : public:
3211 : : void add_indirects (tree);
3212 : : void fn_parms_init (tree);
3213 : : void tpl_header (tree decl, unsigned *tpl_levels);
3214 : :
3215 : : public:
3216 : : merge_kind get_merge_kind (tree decl, depset *maybe_dep);
3217 : : tree decl_container (tree decl);
3218 : : void key_mergeable (int tag, merge_kind, tree decl, tree inner,
3219 : : tree container, depset *maybe_dep);
3220 : : void binfo_mergeable (tree binfo);
3221 : :
3222 : : private:
3223 : : void key_local_type (merge_key&, tree, tree);
3224 : : bool decl_node (tree, walk_kind ref);
3225 : : void type_node (tree);
3226 : : void tree_value (tree);
3227 : : void tpl_parm_value (tree);
3228 : :
3229 : : public:
3230 : : void decl_value (tree, depset *);
3231 : :
3232 : : public:
3233 : : /* Serialize various definitions. */
3234 : : void write_definition (tree decl, bool refs_tu_local = false);
3235 : : void mark_declaration (tree decl, bool do_defn);
3236 : :
3237 : : private:
3238 : : void mark_function_def (tree decl);
3239 : : void mark_var_def (tree decl);
3240 : : void mark_class_def (tree decl);
3241 : : void mark_enum_def (tree decl);
3242 : : void mark_class_member (tree decl, bool do_defn = true);
3243 : : void mark_binfos (tree type);
3244 : :
3245 : : private:
3246 : : void write_var_def (tree decl);
3247 : : void write_function_def (tree decl);
3248 : : void write_class_def (tree decl);
3249 : : void write_enum_def (tree decl);
3250 : :
3251 : : private:
3252 : : static void assert_definition (tree);
3253 : :
3254 : : public:
3255 : : static void instrument ();
3256 : :
3257 : : private:
3258 : : /* Tree instrumentation. */
3259 : : static unsigned tree_val_count;
3260 : : static unsigned decl_val_count;
3261 : : static unsigned back_ref_count;
3262 : : static unsigned tu_local_count;
3263 : : static unsigned null_count;
3264 : : };
3265 : : } // anon namespace
3266 : :
3267 : : /* Instrumentation counters. */
3268 : : unsigned trees_out::tree_val_count;
3269 : : unsigned trees_out::decl_val_count;
3270 : : unsigned trees_out::back_ref_count;
3271 : : unsigned trees_out::tu_local_count;
3272 : : unsigned trees_out::null_count;
3273 : :
3274 : 450282 : trees_out::trees_out (allocator *mem, module_state *state, depset::hash &deps,
3275 : 450282 : unsigned section)
3276 : 900564 : :parent (mem), state (state), tree_map (500),
3277 : 450282 : dep_hash (&deps), ref_num (0), section (section),
3278 : 450282 : writing_local_entities (false), walking_bit_field_unit (false)
3279 : : {
3280 : : #if CHECKING_P
3281 : 450282 : importedness = 0;
3282 : : #endif
3283 : 450282 : }
3284 : :
3285 : 450282 : trees_out::~trees_out ()
3286 : : {
3287 : 450282 : }
3288 : :
3289 : : /********************************************************************/
3290 : : /* Location. We're aware of the line-map concept and reproduce it
3291 : : here. Each imported module allocates a contiguous span of ordinary
3292 : : maps, and of macro maps. adhoc maps are serialized by contents,
3293 : : not pre-allocated. The scattered linemaps of a module are
3294 : : coalesced when writing. */
3295 : :
3296 : :
3297 : : /* I use half-open [first,second) ranges. */
3298 : : typedef std::pair<line_map_uint_t,line_map_uint_t> range_t;
3299 : :
3300 : : /* A range of locations. */
3301 : : typedef std::pair<location_t,location_t> loc_range_t;
3302 : :
3303 : : /* Spans of the line maps that are occupied by this TU. I.e. not
3304 : : within imports. Only extended when in an interface unit.
3305 : : Interval zero corresponds to the forced header linemap(s). This
3306 : : is a singleton object. */
3307 : :
3308 : : class loc_spans {
3309 : : public:
3310 : : /* An interval of line maps. The line maps here represent a contiguous
3311 : : non-imported range. */
3312 : 5502 : struct span {
3313 : : loc_range_t ordinary; /* Ordinary map location range. */
3314 : : loc_range_t macro; /* Macro map location range. */
3315 : : /* Add to locs to get serialized loc. */
3316 : : location_diff_t ordinary_delta;
3317 : : location_diff_t macro_delta;
3318 : : };
3319 : :
3320 : : private:
3321 : : vec<span> *spans;
3322 : : bool locs_exhausted_p;
3323 : :
3324 : : public:
3325 : : loc_spans ()
3326 : : /* Do not preallocate spans, as that causes
3327 : : --enable-detailed-mem-stats problems. */
3328 : : : spans (nullptr), locs_exhausted_p (false)
3329 : : {
3330 : : }
3331 : 96556 : ~loc_spans ()
3332 : : {
3333 : 96556 : delete spans;
3334 : 96556 : }
3335 : :
3336 : : public:
3337 : 273 : span &operator[] (unsigned ix)
3338 : : {
3339 : 546 : return (*spans)[ix];
3340 : : }
3341 : : unsigned length () const
3342 : : {
3343 : : return spans->length ();
3344 : : }
3345 : :
3346 : : public:
3347 : 14350 : bool init_p () const
3348 : : {
3349 : 14350 : return spans != nullptr;
3350 : : }
3351 : : /* Initializer. */
3352 : : void init (const line_maps *lmaps, const line_map_ordinary *map);
3353 : :
3354 : : /* Slightly skewed preprocessed files can cause us to miss an
3355 : : initialization in some places. Fallback initializer. */
3356 : 5351 : void maybe_init ()
3357 : : {
3358 : 5351 : if (!init_p ())
3359 : 6 : init (line_table, nullptr);
3360 : 5351 : }
3361 : :
3362 : : public:
3363 : : enum {
3364 : : SPAN_RESERVED = 0, /* Reserved (fixed) locations. */
3365 : : SPAN_FIRST = 1, /* LWM of locations to stream */
3366 : : SPAN_MAIN = 2 /* Main file and onwards. */
3367 : : };
3368 : :
3369 : : public:
3370 : 439422 : location_t main_start () const
3371 : : {
3372 : 439422 : return (*spans)[SPAN_MAIN].ordinary.first;
3373 : : }
3374 : :
3375 : : public:
3376 : : void open (location_t);
3377 : : void close ();
3378 : :
3379 : : public:
3380 : : /* Propagate imported linemaps to us, if needed. */
3381 : : bool maybe_propagate (module_state *import, location_t loc);
3382 : :
3383 : : public:
3384 : : /* Whether we can no longer represent new imported locations. */
3385 : 0 : bool locations_exhausted_p () const
3386 : : {
3387 : 0 : return locs_exhausted_p;
3388 : : }
3389 : 0 : void report_location_exhaustion (location_t loc)
3390 : : {
3391 : 0 : if (!locs_exhausted_p)
3392 : : {
3393 : : /* Just give the notice once. */
3394 : 0 : locs_exhausted_p = true;
3395 : 0 : inform (loc, "unable to represent further imported source locations");
3396 : : }
3397 : : }
3398 : :
3399 : : public:
3400 : : const span *ordinary (location_t);
3401 : : const span *macro (location_t);
3402 : : };
3403 : :
3404 : : static loc_spans spans;
3405 : :
3406 : : /* Information about ordinary locations we stream out. */
3407 : : struct ord_loc_info
3408 : : {
3409 : : const line_map_ordinary *src; // line map we're based on
3410 : : line_map_uint_t offset; // offset to this line
3411 : : line_map_uint_t span; // number of locs we span
3412 : : line_map_uint_t remap; // serialization
3413 : :
3414 : 196652225 : static int compare (const void *a_, const void *b_)
3415 : : {
3416 : 196652225 : auto *a = static_cast<const ord_loc_info *> (a_);
3417 : 196652225 : auto *b = static_cast<const ord_loc_info *> (b_);
3418 : :
3419 : 196652225 : if (a->src != b->src)
3420 : 43991004 : return a->src < b->src ? -1 : +1;
3421 : :
3422 : : // Ensure no overlap
3423 : 166909701 : gcc_checking_assert (a->offset + a->span <= b->offset
3424 : : || b->offset + b->span <= a->offset);
3425 : :
3426 : 166909701 : gcc_checking_assert (a->offset != b->offset);
3427 : 166909701 : return a->offset < b->offset ? -1 : +1;
3428 : : }
3429 : : };
3430 : : struct ord_loc_traits
3431 : : {
3432 : : typedef ord_loc_info value_type;
3433 : : typedef value_type compare_type;
3434 : :
3435 : : static const bool empty_zero_p = false;
3436 : :
3437 : 80648966 : static hashval_t hash (const value_type &v)
3438 : : {
3439 : 68304972 : auto h = pointer_hash<const line_map_ordinary>::hash (v.src);
3440 : 80648966 : return iterative_hash_hashval_t (v.offset, h);
3441 : : }
3442 : 88946024 : static bool equal (const value_type &v, const compare_type p)
3443 : : {
3444 : 88946024 : return v.src == p.src && v.offset == p.offset;
3445 : : }
3446 : :
3447 : 5898135 : static void mark_empty (value_type &v)
3448 : : {
3449 : 5898135 : v.src = nullptr;
3450 : : }
3451 : 261513789 : static bool is_empty (value_type &v)
3452 : : {
3453 : 261513789 : return !v.src;
3454 : : }
3455 : :
3456 : : static bool is_deleted (value_type &) { return false; }
3457 : : static void mark_deleted (value_type &) { gcc_unreachable (); }
3458 : :
3459 : 1495284 : static void remove (value_type &) {}
3460 : : };
3461 : : /* Table keyed by ord_loc_info, used for noting. */
3462 : : static hash_table<ord_loc_traits> *ord_loc_table;
3463 : : /* Sorted vector, used for writing. */
3464 : : static vec<ord_loc_info> *ord_loc_remap;
3465 : :
3466 : : /* Information about macro locations we stream out. */
3467 : : struct macro_loc_info
3468 : : {
3469 : : const line_map_macro *src; // original expansion
3470 : : line_map_uint_t remap; // serialization
3471 : :
3472 : 7055803 : static int compare (const void *a_, const void *b_)
3473 : : {
3474 : 7055803 : auto *a = static_cast<const macro_loc_info *> (a_);
3475 : 7055803 : auto *b = static_cast<const macro_loc_info *> (b_);
3476 : :
3477 : 7055803 : gcc_checking_assert (MAP_START_LOCATION (a->src)
3478 : : != MAP_START_LOCATION (b->src));
3479 : 7055803 : if (MAP_START_LOCATION (a->src) < MAP_START_LOCATION (b->src))
3480 : : return -1;
3481 : : else
3482 : 3392419 : return +1;
3483 : : }
3484 : : };
3485 : : struct macro_loc_traits
3486 : : {
3487 : : typedef macro_loc_info value_type;
3488 : : typedef const line_map_macro *compare_type;
3489 : :
3490 : : static const bool empty_zero_p = false;
3491 : :
3492 : 6503643 : static hashval_t hash (compare_type p)
3493 : : {
3494 : 6503643 : return pointer_hash<const line_map_macro>::hash (p);
3495 : : }
3496 : 5453751 : static hashval_t hash (const value_type &v)
3497 : : {
3498 : 5453751 : return hash (v.src);
3499 : : }
3500 : : static bool equal (const value_type &v, const compare_type p)
3501 : : {
3502 : : return v.src == p;
3503 : : }
3504 : :
3505 : 547068 : static void mark_empty (value_type &v)
3506 : : {
3507 : 547068 : v.src = nullptr;
3508 : : }
3509 : 21471817 : static bool is_empty (value_type &v)
3510 : : {
3511 : 21471817 : return !v.src;
3512 : : }
3513 : :
3514 : : static bool is_deleted (value_type &) { return false; }
3515 : : static void mark_deleted (value_type &) { gcc_unreachable (); }
3516 : :
3517 : 121585 : static void remove (value_type &) {}
3518 : : };
3519 : : /* Table keyed by line_map_macro, used for noting. */
3520 : : static hash_table<macro_loc_traits> *macro_loc_table;
3521 : : /* Sorted vector, used for writing. */
3522 : : static vec<macro_loc_info> *macro_loc_remap;
3523 : :
3524 : : /* Indirection to allow bsearching imports by ordinary location. */
3525 : : static vec<module_state *> *ool;
3526 : :
3527 : : /********************************************************************/
3528 : : /* Data needed by a module during the process of loading. */
3529 : : struct GTY(()) slurping {
3530 : :
3531 : : /* Remap import's module numbering to our numbering. Values are
3532 : : shifted by 1. Bit0 encodes if the import is direct. */
3533 : : vec<unsigned, va_heap, vl_embed> *
3534 : : GTY((skip)) remap; /* Module owner remapping. */
3535 : :
3536 : : elf_in *GTY((skip)) from; /* The elf loader. */
3537 : :
3538 : : /* This map is only for header imports themselves -- the global
3539 : : headers bitmap hold it for the current TU. */
3540 : : bitmap headers; /* Transitive set of direct imports, including
3541 : : self. Used for macro visibility and
3542 : : priority. */
3543 : :
3544 : : /* These objects point into the mmapped area, unless we're not doing
3545 : : that, or we got frozen or closed. In those cases they point to
3546 : : buffers we own. */
3547 : : bytes_in macro_defs; /* Macro definitions. */
3548 : : bytes_in macro_tbl; /* Macro table. */
3549 : :
3550 : : /* Location remapping. first->ordinary, second->macro. */
3551 : : range_t GTY((skip)) loc_deltas;
3552 : :
3553 : : unsigned current; /* Section currently being loaded. */
3554 : : unsigned remaining; /* Number of lazy sections yet to read. */
3555 : : unsigned lru; /* An LRU counter. */
3556 : :
3557 : : public:
3558 : : slurping (elf_in *);
3559 : : ~slurping ();
3560 : :
3561 : : public:
3562 : : /* Close the ELF file, if it's open. */
3563 : 4870 : void close ()
3564 : : {
3565 : 4870 : if (from)
3566 : : {
3567 : 2671 : from->end ();
3568 : 5342 : delete from;
3569 : 2671 : from = NULL;
3570 : : }
3571 : 4870 : }
3572 : :
3573 : : public:
3574 : : void release_macros ();
3575 : :
3576 : : public:
3577 : 2729 : void alloc_remap (unsigned size)
3578 : : {
3579 : 2729 : gcc_assert (!remap);
3580 : 2729 : vec_safe_reserve (remap, size);
3581 : 5806 : for (unsigned ix = size; ix--;)
3582 : 3077 : remap->quick_push (0);
3583 : 2729 : }
3584 : 872551 : unsigned remap_module (unsigned owner)
3585 : : {
3586 : 872551 : if (owner < remap->length ())
3587 : 872551 : return (*remap)[owner] >> 1;
3588 : : return 0;
3589 : : }
3590 : :
3591 : : public:
3592 : : /* GC allocation. But we must explicitly delete it. */
3593 : 2750 : static void *operator new (size_t x)
3594 : : {
3595 : 5500 : return ggc_alloc_atomic (x);
3596 : : }
3597 : 2671 : static void operator delete (void *p)
3598 : : {
3599 : 2671 : ggc_free (p);
3600 : 2671 : }
3601 : : };
3602 : :
3603 : 2750 : slurping::slurping (elf_in *from)
3604 : 2750 : : remap (NULL), from (from),
3605 : 2750 : headers (BITMAP_GGC_ALLOC ()), macro_defs (), macro_tbl (),
3606 : 2750 : loc_deltas (0, 0),
3607 : 2750 : current (~0u), remaining (0), lru (0)
3608 : : {
3609 : 2750 : }
3610 : :
3611 : 2671 : slurping::~slurping ()
3612 : : {
3613 : 2671 : vec_free (remap);
3614 : 2671 : remap = NULL;
3615 : 2671 : release_macros ();
3616 : 2671 : close ();
3617 : 2671 : }
3618 : :
3619 : 4870 : void slurping::release_macros ()
3620 : : {
3621 : 4870 : if (macro_defs.size)
3622 : 859 : elf_in::release (from, macro_defs);
3623 : 4870 : if (macro_tbl.size)
3624 : 0 : elf_in::release (from, macro_tbl);
3625 : 4870 : }
3626 : :
3627 : : /* Flags for extensions that end up being streamed. */
3628 : :
3629 : : enum streamed_extensions {
3630 : : SE_OPENMP_SIMD = 1 << 0,
3631 : : SE_OPENMP = 1 << 1,
3632 : : SE_OPENACC = 1 << 2,
3633 : : SE_BITS = 3
3634 : : };
3635 : :
3636 : : /* Counter indices. */
3637 : : enum module_state_counts
3638 : : {
3639 : : MSC_sec_lwm,
3640 : : MSC_sec_hwm,
3641 : : MSC_pendings,
3642 : : MSC_entities,
3643 : : MSC_namespaces,
3644 : : MSC_using_directives,
3645 : : MSC_bindings,
3646 : : MSC_macros,
3647 : : MSC_inits,
3648 : : MSC_HWM
3649 : : };
3650 : :
3651 : : /********************************************************************/
3652 : : struct module_state_config;
3653 : :
3654 : : /* Increasing levels of loadedness. */
3655 : : enum module_loadedness {
3656 : : ML_NONE, /* Not loaded. */
3657 : : ML_CONFIG, /* Config loaed. */
3658 : : ML_PREPROCESSOR, /* Preprocessor loaded. */
3659 : : ML_LANGUAGE, /* Language loaded. */
3660 : : };
3661 : :
3662 : : /* Increasing levels of directness (toplevel) of import. */
3663 : : enum module_directness {
3664 : : MD_NONE, /* Not direct. */
3665 : : MD_PARTITION_DIRECT, /* Direct import of a partition. */
3666 : : MD_DIRECT, /* Direct import. */
3667 : : MD_PURVIEW_DIRECT, /* direct import in purview. */
3668 : : };
3669 : :
3670 : : /* State of a particular module. */
3671 : :
3672 : : class GTY((chain_next ("%h.parent"), for_user)) module_state {
3673 : : public:
3674 : : /* We always import & export ourselves. */
3675 : : bitmap imports; /* Transitive modules we're importing. */
3676 : : bitmap exports; /* Subset of that, that we're exporting. */
3677 : :
3678 : : /* For a named module interface A.B, parent is A and name is B.
3679 : : For a partition M:P, parent is M and name is P.
3680 : : For an implementation unit I, parent is I's interface and name is NULL.
3681 : : Otherwise parent is NULL and name will be the flatname. */
3682 : : module_state *parent;
3683 : : tree name;
3684 : :
3685 : : slurping *slurp; /* Data for loading. */
3686 : :
3687 : : const char *flatname; /* Flatname of module. */
3688 : : char *filename; /* CMI Filename */
3689 : :
3690 : : /* Indices into the entity_ary. */
3691 : : unsigned entity_lwm;
3692 : : unsigned entity_num;
3693 : :
3694 : : /* Location ranges for this module. adhoc-locs are decomposed, so
3695 : : don't have a range. */
3696 : : loc_range_t GTY((skip)) ordinary_locs;
3697 : : loc_range_t GTY((skip)) macro_locs; // [lwm,num)
3698 : :
3699 : : /* LOC is first set too the importing location. When initially
3700 : : loaded it refers to a module loc whose parent is the importing
3701 : : location. */
3702 : : location_t loc; /* Location referring to module itself. */
3703 : : unsigned crc; /* CRC we saw reading it in. */
3704 : :
3705 : : unsigned mod; /* Module owner number. */
3706 : : unsigned remap; /* Remapping during writing. */
3707 : :
3708 : : unsigned short subst; /* Mangle subst if !0. */
3709 : :
3710 : : /* How loaded this module is. */
3711 : : enum module_loadedness loadedness : 2;
3712 : :
3713 : : bool module_p : 1; /* /The/ module of this TU. */
3714 : : bool header_p : 1; /* Is a header unit. */
3715 : : bool interface_p : 1; /* An interface. */
3716 : : bool partition_p : 1; /* A partition. */
3717 : :
3718 : : /* How directly this module is imported. */
3719 : : enum module_directness directness : 2;
3720 : :
3721 : : bool exported_p : 1; /* directness != MD_NONE && exported. */
3722 : : bool cmi_noted_p : 1; /* We've told the user about the CMI, don't
3723 : : do it again */
3724 : : bool active_init_p : 1; /* This module's global initializer needs
3725 : : calling. */
3726 : : bool inform_cmi_p : 1; /* Inform of a read/write. */
3727 : : bool visited_p : 1; /* A walk-once flag. */
3728 : : /* Record extensions emitted or permitted. */
3729 : : unsigned extensions : SE_BITS;
3730 : : /* 14 bits used, 2 bits remain */
3731 : :
3732 : : public:
3733 : : module_state (tree name, module_state *, bool);
3734 : : ~module_state ();
3735 : :
3736 : : public:
3737 : 2722 : void release ()
3738 : : {
3739 : 2722 : imports = exports = NULL;
3740 : 2722 : slurped ();
3741 : 2671 : }
3742 : 4921 : void slurped ()
3743 : : {
3744 : 4921 : delete slurp;
3745 : 4921 : slurp = NULL;
3746 : 4921 : }
3747 : 1034379 : elf_in *from () const
3748 : : {
3749 : 1034379 : return slurp->from;
3750 : : }
3751 : :
3752 : : public:
3753 : : /* Kind of this module. */
3754 : 114325 : bool is_module () const
3755 : : {
3756 : 114325 : return module_p;
3757 : : }
3758 : 1635749 : bool is_header () const
3759 : : {
3760 : 1635749 : return header_p;
3761 : : }
3762 : 528 : bool is_interface () const
3763 : : {
3764 : 528 : return interface_p;
3765 : : }
3766 : 245514 : bool is_partition () const
3767 : : {
3768 : 245514 : return partition_p;
3769 : : }
3770 : :
3771 : : /* How this module is used in the current TU. */
3772 : 2980 : bool is_exported () const
3773 : : {
3774 : 2980 : return exported_p;
3775 : : }
3776 : 18944 : bool is_direct () const
3777 : : {
3778 : 18944 : return directness >= MD_DIRECT;
3779 : : }
3780 : 251 : bool is_purview_direct () const
3781 : : {
3782 : 251 : return directness == MD_PURVIEW_DIRECT;
3783 : : }
3784 : 379 : bool is_partition_direct () const
3785 : : {
3786 : 379 : return directness == MD_PARTITION_DIRECT;
3787 : : }
3788 : :
3789 : : public:
3790 : : /* Is this a real module? */
3791 : 14909 : bool has_location () const
3792 : : {
3793 : 14909 : return loc != UNKNOWN_LOCATION;
3794 : : }
3795 : :
3796 : : public:
3797 : : bool check_circular_import (location_t loc);
3798 : :
3799 : : public:
3800 : : void mangle (bool include_partition);
3801 : :
3802 : : public:
3803 : : void set_import (module_state const *, bool is_export);
3804 : : void announce (const char *) const;
3805 : :
3806 : : public:
3807 : : /* Read and write module. */
3808 : : bool write_begin (elf_out *to, cpp_reader *,
3809 : : module_state_config &, unsigned &crc);
3810 : : void write_end (elf_out *to, cpp_reader *,
3811 : : module_state_config &, unsigned &crc);
3812 : : bool read_initial (cpp_reader *);
3813 : : bool read_preprocessor (bool);
3814 : : bool read_language (bool);
3815 : :
3816 : : public:
3817 : : /* Read a section. */
3818 : : bool load_section (unsigned snum, binding_slot *mslot);
3819 : : /* Lazily read a section. */
3820 : : bool lazy_load (unsigned index, binding_slot *mslot);
3821 : :
3822 : : public:
3823 : : /* Juggle a limited number of file numbers. */
3824 : : static void freeze_an_elf ();
3825 : : bool maybe_defrost ();
3826 : :
3827 : : public:
3828 : : void maybe_completed_reading ();
3829 : : bool check_read (bool outermost, bool ok);
3830 : :
3831 : : private:
3832 : : /* The README, for human consumption. */
3833 : : void write_readme (elf_out *to, cpp_reader *, const char *dialect);
3834 : : void write_env (elf_out *to);
3835 : :
3836 : : private:
3837 : : /* Import tables. */
3838 : : void write_imports (bytes_out &cfg, bool direct);
3839 : : unsigned read_imports (bytes_in &cfg, cpp_reader *, line_maps *maps);
3840 : :
3841 : : private:
3842 : : void write_imports (elf_out *to, unsigned *crc_ptr);
3843 : : bool read_imports (cpp_reader *, line_maps *);
3844 : :
3845 : : private:
3846 : : void write_partitions (elf_out *to, unsigned, unsigned *crc_ptr);
3847 : : bool read_partitions (unsigned);
3848 : :
3849 : : private:
3850 : : void write_config (elf_out *to, struct module_state_config &, unsigned crc);
3851 : : bool read_config (struct module_state_config &);
3852 : : static void write_counts (elf_out *to, unsigned [MSC_HWM], unsigned *crc_ptr);
3853 : : bool read_counts (unsigned *);
3854 : :
3855 : : public:
3856 : : void note_cmi_name ();
3857 : :
3858 : : private:
3859 : : static unsigned write_bindings (elf_out *to, vec<depset *> depsets,
3860 : : unsigned *crc_ptr);
3861 : : bool read_bindings (unsigned count, unsigned lwm, unsigned hwm);
3862 : :
3863 : : static void write_namespace (bytes_out &sec, depset *ns_dep);
3864 : : tree read_namespace (bytes_in &sec);
3865 : :
3866 : : void write_namespaces (elf_out *to, vec<depset *> spaces,
3867 : : unsigned, unsigned *crc_ptr);
3868 : : bool read_namespaces (unsigned);
3869 : :
3870 : : unsigned write_using_directives (elf_out *to, depset::hash &,
3871 : : vec<depset *> spaces, unsigned *crc_ptr);
3872 : : bool read_using_directives (unsigned);
3873 : :
3874 : : void intercluster_seed (trees_out &sec, unsigned index, depset *dep);
3875 : : unsigned write_cluster (elf_out *to, depset *depsets[], unsigned size,
3876 : : depset::hash &, unsigned *counts, unsigned *crc_ptr);
3877 : : bool read_cluster (unsigned snum);
3878 : :
3879 : : private:
3880 : : unsigned write_inits (elf_out *to, depset::hash &, unsigned *crc_ptr);
3881 : : bool read_inits (unsigned count);
3882 : :
3883 : : private:
3884 : : unsigned write_pendings (elf_out *to, vec<depset *> depsets,
3885 : : depset::hash &, unsigned *crc_ptr);
3886 : : bool read_pendings (unsigned count);
3887 : :
3888 : : private:
3889 : : void write_entities (elf_out *to, vec<depset *> depsets,
3890 : : unsigned count, unsigned *crc_ptr);
3891 : : bool read_entities (unsigned count, unsigned lwm, unsigned hwm);
3892 : :
3893 : : private:
3894 : : void write_init_maps ();
3895 : : range_t write_prepare_maps (module_state_config *, bool);
3896 : : bool read_prepare_maps (const module_state_config *);
3897 : :
3898 : : void write_ordinary_maps (elf_out *to, range_t &,
3899 : : bool, unsigned *crc_ptr);
3900 : : bool read_ordinary_maps (line_map_uint_t, unsigned);
3901 : : void write_macro_maps (elf_out *to, range_t &, unsigned *crc_ptr);
3902 : : bool read_macro_maps (line_map_uint_t);
3903 : :
3904 : : void write_diagnostic_classification (elf_out *, diagnostics::context *,
3905 : : unsigned *);
3906 : : bool read_diagnostic_classification (diagnostics::context *);
3907 : :
3908 : : private:
3909 : : void write_define (bytes_out &, const cpp_macro *);
3910 : : cpp_macro *read_define (bytes_in &, cpp_reader *) const;
3911 : : vec<cpp_hashnode *> *prepare_macros (cpp_reader *);
3912 : : unsigned write_macros (elf_out *to, vec<cpp_hashnode *> *, unsigned *crc_ptr);
3913 : : bool read_macros ();
3914 : : void install_macros ();
3915 : :
3916 : : public:
3917 : : void import_macros ();
3918 : :
3919 : : public:
3920 : : static void undef_macro (cpp_reader *, location_t, cpp_hashnode *);
3921 : : static cpp_macro *deferred_macro (cpp_reader *, location_t, cpp_hashnode *);
3922 : :
3923 : : public:
3924 : : static bool note_location (location_t);
3925 : : static void write_location (bytes_out &, location_t);
3926 : : location_t read_location (bytes_in &) const;
3927 : :
3928 : : public:
3929 : : void set_flatname ();
3930 : 44162 : const char *get_flatname () const
3931 : : {
3932 : 44162 : return flatname;
3933 : : }
3934 : : location_t imported_from () const;
3935 : :
3936 : : public:
3937 : : void set_filename (const Cody::Packet &);
3938 : : bool do_import (cpp_reader *, bool outermost);
3939 : : };
3940 : :
3941 : : /* Hash module state by name. This cannot be a member of
3942 : : module_state, because of GTY restrictions. We never delete from
3943 : : the hash table, but ggc_ptr_hash doesn't support that
3944 : : simplification. */
3945 : :
3946 : : struct module_state_hash : ggc_ptr_hash<module_state> {
3947 : : typedef std::pair<tree,uintptr_t> compare_type; /* {name,parent} */
3948 : :
3949 : : static inline hashval_t hash (const value_type m);
3950 : : static inline hashval_t hash (const compare_type &n);
3951 : : static inline bool equal (const value_type existing,
3952 : : const compare_type &candidate);
3953 : : };
3954 : :
3955 : 10556 : module_state::module_state (tree name, module_state *parent, bool partition)
3956 : 10556 : : imports (BITMAP_GGC_ALLOC ()), exports (BITMAP_GGC_ALLOC ()),
3957 : 10556 : parent (parent), name (name), slurp (NULL),
3958 : 10556 : flatname (NULL), filename (NULL),
3959 : 10556 : entity_lwm (~0u >> 1), entity_num (0),
3960 : 10556 : ordinary_locs (0, 0), macro_locs (0, 0),
3961 : 10556 : loc (UNKNOWN_LOCATION),
3962 : 10556 : crc (0), mod (MODULE_UNKNOWN), remap (0), subst (0)
3963 : : {
3964 : 10556 : loadedness = ML_NONE;
3965 : :
3966 : 10556 : module_p = header_p = interface_p = partition_p = false;
3967 : :
3968 : 10556 : directness = MD_NONE;
3969 : 10556 : exported_p = false;
3970 : :
3971 : 10556 : cmi_noted_p = false;
3972 : 10556 : active_init_p = false;
3973 : :
3974 : 10556 : partition_p = partition;
3975 : :
3976 : 10556 : inform_cmi_p = false;
3977 : 10556 : visited_p = false;
3978 : :
3979 : 10556 : extensions = 0;
3980 : 10556 : if (name && TREE_CODE (name) == STRING_CST)
3981 : : {
3982 : 1843 : header_p = true;
3983 : :
3984 : 1843 : const char *string = TREE_STRING_POINTER (name);
3985 : 1843 : gcc_checking_assert (string[0] == '.'
3986 : : ? IS_DIR_SEPARATOR (string[1])
3987 : : : IS_ABSOLUTE_PATH (string));
3988 : : }
3989 : :
3990 : 10556 : gcc_checking_assert (!(parent && header_p));
3991 : 10556 : }
3992 : :
3993 : 51 : module_state::~module_state ()
3994 : : {
3995 : 51 : release ();
3996 : 51 : }
3997 : :
3998 : : /* Hash module state. */
3999 : : static hashval_t
4000 : 15276 : module_name_hash (const_tree name)
4001 : : {
4002 : 15276 : if (TREE_CODE (name) == STRING_CST)
4003 : 3398 : return htab_hash_string (TREE_STRING_POINTER (name));
4004 : : else
4005 : 11878 : return IDENTIFIER_HASH_VALUE (name);
4006 : : }
4007 : :
4008 : : hashval_t
4009 : 4132 : module_state_hash::hash (const value_type m)
4010 : : {
4011 : 4132 : hashval_t ph = pointer_hash<void>::hash
4012 : 4132 : (reinterpret_cast<void *> (reinterpret_cast<uintptr_t> (m->parent)
4013 : 4132 : | m->is_partition ()));
4014 : 4132 : hashval_t nh = module_name_hash (m->name);
4015 : 4132 : return iterative_hash_hashval_t (ph, nh);
4016 : : }
4017 : :
4018 : : /* Hash a name. */
4019 : : hashval_t
4020 : 11144 : module_state_hash::hash (const compare_type &c)
4021 : : {
4022 : 11144 : hashval_t ph = pointer_hash<void>::hash (reinterpret_cast<void *> (c.second));
4023 : 11144 : hashval_t nh = module_name_hash (c.first);
4024 : :
4025 : 11144 : return iterative_hash_hashval_t (ph, nh);
4026 : : }
4027 : :
4028 : : bool
4029 : 7539 : module_state_hash::equal (const value_type existing,
4030 : : const compare_type &candidate)
4031 : : {
4032 : 7539 : uintptr_t ep = (reinterpret_cast<uintptr_t> (existing->parent)
4033 : 7539 : | existing->is_partition ());
4034 : 7539 : if (ep != candidate.second)
4035 : : return false;
4036 : :
4037 : : /* Identifier comparison is by pointer. If the string_csts happen
4038 : : to be the same object, then they're equal too. */
4039 : 6230 : if (existing->name == candidate.first)
4040 : : return true;
4041 : :
4042 : : /* If neither are string csts, they can't be equal. */
4043 : 1284 : if (TREE_CODE (candidate.first) != STRING_CST
4044 : 483 : || TREE_CODE (existing->name) != STRING_CST)
4045 : : return false;
4046 : :
4047 : : /* String equality. */
4048 : 417 : if (TREE_STRING_LENGTH (existing->name)
4049 : 417 : == TREE_STRING_LENGTH (candidate.first)
4050 : 417 : && !memcmp (TREE_STRING_POINTER (existing->name),
4051 : 414 : TREE_STRING_POINTER (candidate.first),
4052 : 414 : TREE_STRING_LENGTH (existing->name)))
4053 : 131 : return true;
4054 : :
4055 : : return false;
4056 : : }
4057 : :
4058 : : /********************************************************************/
4059 : : /* Global state */
4060 : :
4061 : : /* Mapper name. */
4062 : : static const char *module_mapper_name;
4063 : :
4064 : : /* Deferred import queue (FIFO). */
4065 : : static vec<module_state *, va_heap, vl_embed> *pending_imports;
4066 : :
4067 : : /* CMI repository path and workspace. */
4068 : : static char *cmi_repo;
4069 : : static size_t cmi_repo_length;
4070 : : static char *cmi_path;
4071 : : static size_t cmi_path_alloc;
4072 : :
4073 : : /* Count of available and loaded clusters. */
4074 : : static unsigned available_clusters;
4075 : : static unsigned loaded_clusters;
4076 : :
4077 : : /* What the current TU is. */
4078 : : unsigned module_kind;
4079 : :
4080 : : /* Global trees. */
4081 : : static const std::pair<tree *, unsigned> global_tree_arys[] =
4082 : : {
4083 : : std::pair<tree *, unsigned> (sizetype_tab, stk_type_kind_last),
4084 : : std::pair<tree *, unsigned> (integer_types, itk_none),
4085 : : std::pair<tree *, unsigned> (global_trees, TI_MODULE_HWM),
4086 : : std::pair<tree *, unsigned> (c_global_trees, CTI_MODULE_HWM),
4087 : : std::pair<tree *, unsigned> (cp_global_trees, CPTI_MODULE_HWM),
4088 : : std::pair<tree *, unsigned> (NULL, 0)
4089 : : };
4090 : : static GTY(()) vec<tree, va_gc> *fixed_trees;
4091 : : static unsigned global_crc;
4092 : :
4093 : : /* Lazy loading can open many files concurrently, there are
4094 : : per-process limits on that. We pay attention to the process limit,
4095 : : and attempt to increase it when we run out. Otherwise we use an
4096 : : LRU scheme to figure out who to flush. Note that if the import
4097 : : graph /depth/ exceeds lazy_limit, we'll exceed the limit. */
4098 : : static unsigned lazy_lru; /* LRU counter. */
4099 : : static unsigned lazy_open; /* Number of open modules */
4100 : : static unsigned lazy_limit; /* Current limit of open modules. */
4101 : : static unsigned lazy_hard_limit; /* Hard limit on open modules. */
4102 : : /* Account for source, assembler and dump files & directory searches.
4103 : : We don't keep the source file's open, so we don't have to account
4104 : : for #include depth. I think dump files are opened and closed per
4105 : : pass, but ICBW. */
4106 : : #define LAZY_HEADROOM 15 /* File descriptor headroom. */
4107 : :
4108 : : /* Vector of module state. Indexed by OWNER. Index 0 is reserved for the
4109 : : current TU; imports start at 1. */
4110 : : static GTY(()) vec<module_state *, va_gc> *modules;
4111 : :
4112 : : /* Get the module state for the current TU's module. */
4113 : :
4114 : : static module_state *
4115 : 187222 : this_module() {
4116 : 187222 : return (*modules)[0];
4117 : : }
4118 : :
4119 : : /* Hash of module state, findable by {name, parent}. */
4120 : : static GTY(()) hash_table<module_state_hash> *modules_hash;
4121 : :
4122 : : /* Map of imported entities. We map DECL_UID to index of entity
4123 : : vector. */
4124 : : typedef hash_map<unsigned/*UID*/, unsigned/*index*/,
4125 : : simple_hashmap_traits<int_hash<unsigned,0>, unsigned>
4126 : : > entity_map_t;
4127 : : static entity_map_t *entity_map;
4128 : : /* Doesn't need GTYing, because any tree referenced here is also
4129 : : findable by, symbol table, specialization table, return type of
4130 : : reachable function. */
4131 : : static vec<binding_slot, va_heap, vl_embed> *entity_ary;
4132 : :
4133 : : /* Members entities of imported classes that are defined in this TU.
4134 : : These are where the entity's context is not from the current TU.
4135 : : We need to emit the definition (but not the enclosing class).
4136 : :
4137 : : We could find these by walking ALL the imported classes that we
4138 : : could provide a member definition. But that's expensive,
4139 : : especially when you consider lazy implicit member declarations,
4140 : : which could be ANY imported class. */
4141 : : static GTY(()) vec<tree, va_gc> *class_members;
4142 : :
4143 : : /* The same problem exists for class template partial
4144 : : specializations. Now that we have constraints, the invariant of
4145 : : expecting them in the instantiation table no longer holds. One of
4146 : : the constrained partial specializations will be there, but the
4147 : : others not so much. It's not even an unconstrained partial
4148 : : spacialization in the table :( so any partial template declaration
4149 : : is added to this list too. */
4150 : : static GTY(()) vec<tree, va_gc> *partial_specializations;
4151 : :
4152 : : /********************************************************************/
4153 : :
4154 : : /* Our module mapper (created lazily). */
4155 : : module_client *mapper;
4156 : :
4157 : : static module_client *make_mapper (location_t loc, class mkdeps *deps);
4158 : 30556 : inline module_client *get_mapper (location_t loc, class mkdeps *deps)
4159 : : {
4160 : 30556 : auto *res = mapper;
4161 : 284 : if (!res)
4162 : 4489 : res = make_mapper (loc, deps);
4163 : 30556 : return res;
4164 : : }
4165 : :
4166 : : /********************************************************************/
4167 : : static tree
4168 : 254730 : get_clone_target (tree decl)
4169 : : {
4170 : 254730 : tree target;
4171 : :
4172 : 254730 : if (TREE_CODE (decl) == TEMPLATE_DECL)
4173 : : {
4174 : 28256 : tree res_orig = DECL_CLONED_FUNCTION (DECL_TEMPLATE_RESULT (decl));
4175 : :
4176 : 28256 : target = DECL_TI_TEMPLATE (res_orig);
4177 : : }
4178 : : else
4179 : 226474 : target = DECL_CLONED_FUNCTION (decl);
4180 : :
4181 : 254730 : gcc_checking_assert (DECL_MAYBE_IN_CHARGE_CDTOR_P (target));
4182 : :
4183 : 254730 : return target;
4184 : : }
4185 : :
4186 : : /* Like FOR_EACH_CLONE, but will walk cloned templates. */
4187 : : #define FOR_EVERY_CLONE(CLONE, FN) \
4188 : : if (!DECL_MAYBE_IN_CHARGE_CDTOR_P (FN)); \
4189 : : else \
4190 : : for (CLONE = DECL_CHAIN (FN); \
4191 : : CLONE && DECL_CLONED_FUNCTION_P (CLONE); \
4192 : : CLONE = DECL_CHAIN (CLONE))
4193 : :
4194 : : /* It'd be nice if USE_TEMPLATE was a field of template_info
4195 : : (a) it'd solve the enum case dealt with below,
4196 : : (b) both class templates and decl templates would store this in the
4197 : : same place
4198 : : (c) this function wouldn't need the by-ref arg, which is annoying. */
4199 : :
4200 : : static tree
4201 : 178671683 : node_template_info (tree decl, int &use)
4202 : : {
4203 : 178671683 : tree ti = NULL_TREE;
4204 : 178671683 : int use_tpl = -1;
4205 : 178671683 : if (DECL_IMPLICIT_TYPEDEF_P (decl))
4206 : : {
4207 : 18867443 : tree type = TREE_TYPE (decl);
4208 : :
4209 : 18867443 : ti = TYPE_TEMPLATE_INFO (type);
4210 : 18867443 : if (ti)
4211 : : {
4212 : 3367428 : if (TYPE_LANG_SPECIFIC (type))
4213 : 3360097 : use_tpl = CLASSTYPE_USE_TEMPLATE (type);
4214 : : else
4215 : : {
4216 : : /* An enum, where we don't explicitly encode use_tpl.
4217 : : If the containing context (a type or a function), is
4218 : : an ({im,ex}plicit) instantiation, then this is too.
4219 : : If it's a partial or explicit specialization, then
4220 : : this is not!. */
4221 : 7331 : tree ctx = CP_DECL_CONTEXT (decl);
4222 : 7331 : if (TYPE_P (ctx))
4223 : 7218 : ctx = TYPE_NAME (ctx);
4224 : 7331 : node_template_info (ctx, use);
4225 : 7331 : use_tpl = use != 2 ? use : 0;
4226 : : }
4227 : : }
4228 : : }
4229 : 159804240 : else if (DECL_LANG_SPECIFIC (decl)
4230 : 159804240 : && (VAR_P (decl)
4231 : : || TREE_CODE (decl) == TYPE_DECL
4232 : : || TREE_CODE (decl) == FUNCTION_DECL
4233 : : || TREE_CODE (decl) == FIELD_DECL
4234 : : || TREE_CODE (decl) == CONCEPT_DECL
4235 : : || TREE_CODE (decl) == TEMPLATE_DECL))
4236 : : {
4237 : 84641934 : use_tpl = DECL_USE_TEMPLATE (decl);
4238 : 84641934 : ti = DECL_TEMPLATE_INFO (decl);
4239 : : }
4240 : :
4241 : 178671683 : use = use_tpl;
4242 : 178671683 : return ti;
4243 : : }
4244 : :
4245 : : /* Find the index in entity_ary for an imported DECL. It should
4246 : : always be there, but bugs can cause it to be missing, and that can
4247 : : crash the crash reporting -- let's not do that! When streaming
4248 : : out we place entities from this module there too -- with negated
4249 : : indices. */
4250 : :
4251 : : static unsigned
4252 : 1012897 : import_entity_index (tree decl, bool null_ok = false)
4253 : : {
4254 : 1012897 : if (unsigned *slot = entity_map->get (DECL_UID (decl)))
4255 : 1012852 : return *slot;
4256 : :
4257 : 45 : gcc_checking_assert (null_ok);
4258 : : return ~(~0u >> 1);
4259 : : }
4260 : :
4261 : : /* Find the module for an imported entity at INDEX in the entity ary.
4262 : : There must be one. */
4263 : :
4264 : : static module_state *
4265 : 28499 : import_entity_module (unsigned index)
4266 : : {
4267 : 28499 : if (index > ~(~0u >> 1))
4268 : : /* This is an index for an exported entity. */
4269 : 18 : return this_module ();
4270 : :
4271 : : /* Do not include the current TU (not an off-by-one error). */
4272 : 28481 : unsigned pos = 1;
4273 : 28481 : unsigned len = modules->length () - pos;
4274 : 64912 : while (len)
4275 : : {
4276 : 36431 : unsigned half = len / 2;
4277 : 36431 : module_state *probe = (*modules)[pos + half];
4278 : 36431 : if (index < probe->entity_lwm)
4279 : : len = half;
4280 : 28599 : else if (index < probe->entity_lwm + probe->entity_num)
4281 : : return probe;
4282 : : else
4283 : : {
4284 : 118 : pos += half + 1;
4285 : 118 : len = len - (half + 1);
4286 : : }
4287 : : }
4288 : 0 : gcc_unreachable ();
4289 : : }
4290 : :
4291 : :
4292 : : /********************************************************************/
4293 : : /* A dumping machinery. */
4294 : :
4295 : : class dumper {
4296 : : public:
4297 : : enum {
4298 : : LOCATION = TDF_LINENO, /* -lineno:Source location streaming. */
4299 : : DEPEND = TDF_GRAPH, /* -graph:Dependency graph construction. */
4300 : : CLUSTER = TDF_BLOCKS, /* -blocks:Clusters. */
4301 : : TREE = TDF_UID, /* -uid:Tree streaming. */
4302 : : MERGE = TDF_ALIAS, /* -alias:Mergeable Entities. */
4303 : : ELF = TDF_ASMNAME, /* -asmname:Elf data. */
4304 : : MACRO = TDF_VOPS /* -vops:Macros. */
4305 : : };
4306 : :
4307 : : private:
4308 : : struct impl {
4309 : : typedef vec<module_state *, va_heap, vl_embed> stack_t;
4310 : :
4311 : : FILE *stream; /* Dump stream. */
4312 : : unsigned indent; /* Local indentation. */
4313 : : bool bol; /* Beginning of line. */
4314 : : stack_t stack; /* Trailing array of module_state. */
4315 : :
4316 : : bool nested_name (tree); /* Dump a name following DECL_CONTEXT. */
4317 : : };
4318 : :
4319 : : public:
4320 : : /* The dumper. */
4321 : : impl *dumps;
4322 : : dump_flags_t flags;
4323 : :
4324 : : public:
4325 : : /* Push/pop module state dumping. */
4326 : : unsigned push (module_state *);
4327 : : void pop (unsigned);
4328 : :
4329 : : public:
4330 : : /* Change local indentation. */
4331 : 276220485 : void indent ()
4332 : : {
4333 : 309 : if (dumps)
4334 : 534332 : dumps->indent++;
4335 : : }
4336 : 276220485 : void outdent ()
4337 : : {
4338 : 276220485 : if (dumps)
4339 : : {
4340 : 534332 : gcc_checking_assert (dumps->indent);
4341 : 534332 : dumps->indent--;
4342 : : }
4343 : 276220485 : }
4344 : :
4345 : : public:
4346 : : /* Is dump enabled?. */
4347 : 171702073 : bool operator () (int mask = 0)
4348 : : {
4349 : 3563096 : if (!dumps || !dumps->stream)
4350 : : return false;
4351 : 403625 : if (mask && !(mask & flags))
4352 : 3932 : return false;
4353 : : return true;
4354 : : }
4355 : : /* Dump some information. */
4356 : : bool operator () (const char *, ...);
4357 : : };
4358 : :
4359 : : /* The dumper. */
4360 : : static dumper dump = {0, dump_flags_t (0)};
4361 : :
4362 : : /* Push to dumping M. Return previous indentation level. */
4363 : :
4364 : : unsigned
4365 : 88191 : dumper::push (module_state *m)
4366 : : {
4367 : 88191 : FILE *stream = NULL;
4368 : 88191 : if (!dumps || !dumps->stack.length ())
4369 : : {
4370 : 87005 : stream = dump_begin (module_dump_id, &flags);
4371 : 87005 : if (!stream)
4372 : : return 0;
4373 : : }
4374 : :
4375 : 6992 : if (!dumps || !dumps->stack.space (1))
4376 : : {
4377 : : /* Create or extend the dump implementor. */
4378 : 1155 : unsigned current = dumps ? dumps->stack.length () : 0;
4379 : 611 : unsigned count = current ? current * 2 : EXPERIMENT (1, 20);
4380 : 1155 : size_t alloc = (offsetof (impl, stack)
4381 : 1155 : + impl::stack_t::embedded_size (count));
4382 : 1155 : dumps = XRESIZEVAR (impl, dumps, alloc);
4383 : 1155 : dumps->stack.embedded_init (count, current);
4384 : : }
4385 : 6992 : if (stream)
4386 : 5806 : dumps->stream = stream;
4387 : :
4388 : 6992 : unsigned n = dumps->indent;
4389 : 6992 : dumps->indent = 0;
4390 : 6992 : dumps->bol = true;
4391 : 6992 : dumps->stack.quick_push (m);
4392 : 6992 : if (m)
4393 : : {
4394 : 1892 : module_state *from = NULL;
4395 : :
4396 : 1892 : if (dumps->stack.length () > 1)
4397 : 606 : from = dumps->stack[dumps->stack.length () - 2];
4398 : : else
4399 : 1286 : dump ("");
4400 : 3419 : dump (from ? "Starting module %M (from %M)"
4401 : : : "Starting module %M", m, from);
4402 : : }
4403 : :
4404 : : return n;
4405 : : }
4406 : :
4407 : : /* Pop from dumping. Restore indentation to N. */
4408 : :
4409 : 88148 : void dumper::pop (unsigned n)
4410 : : {
4411 : 88148 : if (!dumps)
4412 : : return;
4413 : :
4414 : 13984 : gcc_checking_assert (dump () && !dumps->indent);
4415 : 6992 : if (module_state *m = dumps->stack[dumps->stack.length () - 1])
4416 : : {
4417 : 1892 : module_state *from = (dumps->stack.length () > 1
4418 : 1892 : ? dumps->stack[dumps->stack.length () - 2] : NULL);
4419 : 2133 : dump (from ? "Finishing module %M (returning to %M)"
4420 : : : "Finishing module %M", m, from);
4421 : : }
4422 : 6992 : dumps->stack.pop ();
4423 : 6992 : dumps->indent = n;
4424 : 6992 : if (!dumps->stack.length ())
4425 : : {
4426 : 5806 : dump_end (module_dump_id, dumps->stream);
4427 : 5806 : dumps->stream = NULL;
4428 : : }
4429 : : }
4430 : :
4431 : : /* Dump a nested name for arbitrary tree T. Sometimes it won't have a
4432 : : name. */
4433 : :
4434 : : bool
4435 : 482563 : dumper::impl::nested_name (tree t)
4436 : : {
4437 : 482563 : tree ti = NULL_TREE;
4438 : 482563 : int origin = -1;
4439 : 482563 : tree name = NULL_TREE;
4440 : :
4441 : 482563 : if (t && TREE_CODE (t) == TU_LOCAL_ENTITY)
4442 : 0 : t = TU_LOCAL_ENTITY_NAME (t);
4443 : :
4444 : 482533 : if (t && TREE_CODE (t) == TREE_BINFO)
4445 : 384 : t = BINFO_TYPE (t);
4446 : :
4447 : 482563 : if (t && TYPE_P (t))
4448 : 239451 : t = TYPE_NAME (t);
4449 : :
4450 : 482521 : if (t && DECL_P (t))
4451 : : {
4452 : 405213 : if (t == global_namespace || DECL_TEMPLATE_PARM_P (t))
4453 : : ;
4454 : 373988 : else if (tree ctx = DECL_CONTEXT (t))
4455 : 286972 : if (TREE_CODE (ctx) == TRANSLATION_UNIT_DECL
4456 : 286972 : || nested_name (ctx))
4457 : 286972 : fputs ("::", stream);
4458 : :
4459 : 405213 : int use_tpl;
4460 : 405213 : ti = node_template_info (t, use_tpl);
4461 : 130423 : if (ti && TREE_CODE (TI_TEMPLATE (ti)) == TEMPLATE_DECL
4462 : 535594 : && (DECL_TEMPLATE_RESULT (TI_TEMPLATE (ti)) == t))
4463 : : t = TI_TEMPLATE (ti);
4464 : 405213 : tree not_tmpl = t;
4465 : 405213 : if (TREE_CODE (t) == TEMPLATE_DECL)
4466 : : {
4467 : 22536 : fputs ("template ", stream);
4468 : 22536 : not_tmpl = DECL_TEMPLATE_RESULT (t);
4469 : : }
4470 : :
4471 : 22536 : if (not_tmpl
4472 : 405209 : && DECL_P (not_tmpl)
4473 : 405209 : && DECL_LANG_SPECIFIC (not_tmpl)
4474 : 238831 : && DECL_MODULE_IMPORT_P (not_tmpl))
4475 : : {
4476 : : /* We need to be careful here, so as to not explode on
4477 : : inconsistent data -- we're probably debugging, because
4478 : : Something Is Wrong. */
4479 : 18248 : unsigned index = import_entity_index (t, true);
4480 : 18248 : if (!(index & ~(~0u >> 1)))
4481 : 17690 : origin = import_entity_module (index)->mod;
4482 : 558 : else if (index > ~(~0u >> 1))
4483 : : /* An imported partition member that we're emitting. */
4484 : : origin = 0;
4485 : : else
4486 : 45 : origin = -2;
4487 : : }
4488 : :
4489 : 408281 : name = DECL_NAME (t) ? DECL_NAME (t)
4490 : 4141 : : HAS_DECL_ASSEMBLER_NAME_P (t) ? DECL_ASSEMBLER_NAME_RAW (t)
4491 : : : NULL_TREE;
4492 : : }
4493 : : else
4494 : : name = t;
4495 : :
4496 : 405213 : if (name)
4497 : 449011 : switch (TREE_CODE (name))
4498 : : {
4499 : 13467 : default:
4500 : 13467 : fputs ("#unnamed#", stream);
4501 : 13467 : break;
4502 : :
4503 : 415101 : case IDENTIFIER_NODE:
4504 : 415101 : fwrite (IDENTIFIER_POINTER (name), 1, IDENTIFIER_LENGTH (name), stream);
4505 : 415101 : break;
4506 : :
4507 : 20351 : case INTEGER_CST:
4508 : 20351 : print_hex (wi::to_wide (name), stream);
4509 : 20351 : break;
4510 : :
4511 : 92 : case STRING_CST:
4512 : : /* If TREE_TYPE is NULL, this is a raw string. */
4513 : 184 : fwrite (TREE_STRING_POINTER (name), 1,
4514 : 92 : TREE_STRING_LENGTH (name) - (TREE_TYPE (name) != NULL_TREE),
4515 : : stream);
4516 : 92 : break;
4517 : : }
4518 : : else
4519 : 33552 : fputs ("#null#", stream);
4520 : :
4521 : 482563 : if (origin >= 0)
4522 : : {
4523 : 18203 : const module_state *module = (*modules)[origin];
4524 : 36406 : fprintf (stream, "@%s:%d", !module ? "" : !module->name ? "(unnamed)"
4525 : 18203 : : module->get_flatname (), origin);
4526 : : }
4527 : 464360 : else if (origin == -2)
4528 : 45 : fprintf (stream, "@???");
4529 : :
4530 : 482563 : if (ti)
4531 : : {
4532 : 130423 : tree args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (ti));
4533 : 130423 : fputs ("<", stream);
4534 : 130423 : if (args)
4535 : 327742 : for (int ix = 0; ix != TREE_VEC_LENGTH (args); ix++)
4536 : : {
4537 : 197319 : if (ix)
4538 : 66896 : fputs (",", stream);
4539 : 197319 : nested_name (TREE_VEC_ELT (args, ix));
4540 : : }
4541 : 130423 : fputs (">", stream);
4542 : : }
4543 : :
4544 : 482563 : return true;
4545 : : }
4546 : :
4547 : : /* Formatted dumping. FORMAT begins with '+' do not emit a trailing
4548 : : new line. (Normally it is appended.)
4549 : : Escapes:
4550 : : %C - tree_code
4551 : : %I - identifier
4552 : : %K - location_t or line_map_uint_t
4553 : : %M - module_state
4554 : : %N - name -- DECL_NAME
4555 : : %P - context:name pair
4556 : : %R - unsigned:unsigned ratio
4557 : : %S - symbol -- DECL_ASSEMBLER_NAME
4558 : : %U - long unsigned
4559 : : %V - version
4560 : : --- the following are printf-like, but without its flexibility
4561 : : %d - decimal int
4562 : : %p - pointer
4563 : : %s - string
4564 : : %u - unsigned int
4565 : : %x - hex int
4566 : :
4567 : : We do not implement the printf modifiers. */
4568 : :
4569 : : bool
4570 : 416032 : dumper::operator () (const char *format, ...)
4571 : : {
4572 : 416032 : if (!(*this) ())
4573 : : return false;
4574 : :
4575 : 358153 : bool no_nl = format[0] == '+';
4576 : 358153 : format += no_nl;
4577 : :
4578 : 358153 : if (dumps->bol)
4579 : : {
4580 : : /* Module import indent. */
4581 : 185705 : if (unsigned depth = dumps->stack.length () - 1)
4582 : : {
4583 : 21725 : const char *prefix = ">>>>";
4584 : 43432 : fprintf (dumps->stream, (depth <= strlen (prefix)
4585 : 21707 : ? &prefix[strlen (prefix) - depth]
4586 : : : ">.%d.>"), depth);
4587 : : }
4588 : :
4589 : : /* Local indent. */
4590 : 185705 : if (unsigned indent = dumps->indent)
4591 : : {
4592 : 98479 : const char *prefix = " ";
4593 : 192008 : fprintf (dumps->stream, (indent <= strlen (prefix)
4594 : 93529 : ? &prefix[strlen (prefix) - indent]
4595 : : : " .%d. "), indent);
4596 : : }
4597 : 185705 : dumps->bol = false;
4598 : : }
4599 : :
4600 : 358153 : va_list args;
4601 : 358153 : va_start (args, format);
4602 : 1052520 : while (const char *esc = strchr (format, '%'))
4603 : : {
4604 : 694367 : fwrite (format, 1, (size_t)(esc - format), dumps->stream);
4605 : 694367 : format = ++esc;
4606 : 694367 : switch (*format++)
4607 : : {
4608 : 0 : default:
4609 : 0 : gcc_unreachable ();
4610 : :
4611 : 544 : case '%':
4612 : 544 : fputc ('%', dumps->stream);
4613 : 544 : break;
4614 : :
4615 : 107450 : case 'C': /* Code */
4616 : 107450 : {
4617 : 107450 : tree_code code = (tree_code)va_arg (args, unsigned);
4618 : 107450 : fputs (get_tree_code_name (code), dumps->stream);
4619 : : }
4620 : 107450 : break;
4621 : :
4622 : 81 : case 'I': /* Identifier. */
4623 : 81 : {
4624 : 81 : tree t = va_arg (args, tree);
4625 : 81 : dumps->nested_name (t);
4626 : : }
4627 : 81 : break;
4628 : :
4629 : 4296 : case 'K': /* location_t, either 32- or 64-bit. */
4630 : 4296 : {
4631 : 4296 : unsigned long long u = va_arg (args, location_t);
4632 : 4296 : fprintf (dumps->stream, "%llu", u);
4633 : : }
4634 : 4296 : break;
4635 : :
4636 : 7248 : case 'M': /* Module. */
4637 : 7248 : {
4638 : 7248 : const char *str = "(none)";
4639 : 7248 : if (module_state *m = va_arg (args, module_state *))
4640 : : {
4641 : 7248 : if (!m->has_location ())
4642 : : str = "(detached)";
4643 : : else
4644 : 7248 : str = m->get_flatname ();
4645 : : }
4646 : 7248 : fputs (str, dumps->stream);
4647 : : }
4648 : 7248 : break;
4649 : :
4650 : 118045 : case 'N': /* Name. */
4651 : 118045 : {
4652 : 118045 : tree t = va_arg (args, tree);
4653 : 236579 : while (t && TREE_CODE (t) == OVERLOAD)
4654 : 489 : t = OVL_FUNCTION (t);
4655 : 118045 : fputc ('\'', dumps->stream);
4656 : 118045 : dumps->nested_name (t);
4657 : 118045 : fputc ('\'', dumps->stream);
4658 : : }
4659 : 118045 : break;
4660 : :
4661 : 6829 : case 'P': /* Pair. */
4662 : 6829 : {
4663 : 6829 : tree ctx = va_arg (args, tree);
4664 : 6829 : tree name = va_arg (args, tree);
4665 : 6829 : fputc ('\'', dumps->stream);
4666 : 6829 : dumps->nested_name (ctx);
4667 : 6829 : if (ctx && ctx != global_namespace)
4668 : 855 : fputs ("::", dumps->stream);
4669 : 6829 : dumps->nested_name (name);
4670 : 6829 : fputc ('\'', dumps->stream);
4671 : : }
4672 : 6829 : break;
4673 : :
4674 : 819 : case 'R': /* Ratio */
4675 : 819 : {
4676 : 819 : unsigned a = va_arg (args, unsigned);
4677 : 819 : unsigned b = va_arg (args, unsigned);
4678 : 819 : fprintf (dumps->stream, "%.1f", (float) a / (b + !b));
4679 : : }
4680 : 819 : break;
4681 : :
4682 : 34701 : case 'S': /* Symbol name */
4683 : 34701 : {
4684 : 34701 : tree t = va_arg (args, tree);
4685 : 34701 : if (t && TYPE_P (t))
4686 : 12631 : t = TYPE_NAME (t);
4687 : 32921 : if (t && HAS_DECL_ASSEMBLER_NAME_P (t)
4688 : 31744 : && DECL_ASSEMBLER_NAME_SET_P (t))
4689 : : {
4690 : 169 : fputc ('(', dumps->stream);
4691 : 169 : fputs (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (t)),
4692 : 169 : dumps->stream);
4693 : 169 : fputc (')', dumps->stream);
4694 : : }
4695 : : }
4696 : : break;
4697 : :
4698 : 0 : case 'U': /* long unsigned. */
4699 : 0 : {
4700 : 0 : unsigned long u = va_arg (args, unsigned long);
4701 : 0 : fprintf (dumps->stream, "%lu", u);
4702 : : }
4703 : 0 : break;
4704 : :
4705 : 763 : case 'V': /* Version. */
4706 : 763 : {
4707 : 763 : unsigned v = va_arg (args, unsigned);
4708 : 763 : verstr_t string;
4709 : :
4710 : 763 : version2string (v, string);
4711 : 763 : fputs (string, dumps->stream);
4712 : : }
4713 : 763 : break;
4714 : :
4715 : 0 : case 'c': /* Character. */
4716 : 0 : {
4717 : 0 : int c = va_arg (args, int);
4718 : 0 : fputc (c, dumps->stream);
4719 : : }
4720 : 0 : break;
4721 : :
4722 : 61467 : case 'd': /* Decimal Int. */
4723 : 61467 : {
4724 : 61467 : int d = va_arg (args, int);
4725 : 61467 : fprintf (dumps->stream, "%d", d);
4726 : : }
4727 : 61467 : break;
4728 : :
4729 : 0 : case 'p': /* Pointer. */
4730 : 0 : {
4731 : 0 : void *p = va_arg (args, void *);
4732 : 0 : fprintf (dumps->stream, "%p", p);
4733 : : }
4734 : 0 : break;
4735 : :
4736 : 120218 : case 's': /* String. */
4737 : 120218 : {
4738 : 120218 : const char *s = va_arg (args, char *);
4739 : 120218 : gcc_checking_assert (s);
4740 : 120218 : fputs (s, dumps->stream);
4741 : : }
4742 : 120218 : break;
4743 : :
4744 : 229452 : case 'u': /* Unsigned. */
4745 : 229452 : {
4746 : 229452 : unsigned u = va_arg (args, unsigned);
4747 : 229452 : fprintf (dumps->stream, "%u", u);
4748 : : }
4749 : 229452 : break;
4750 : :
4751 : 2454 : case 'x': /* Hex. */
4752 : 2454 : {
4753 : 2454 : unsigned x = va_arg (args, unsigned);
4754 : 2454 : fprintf (dumps->stream, "%x", x);
4755 : : }
4756 : 2454 : break;
4757 : : }
4758 : : }
4759 : 358153 : fputs (format, dumps->stream);
4760 : 358153 : va_end (args);
4761 : 358153 : if (!no_nl)
4762 : : {
4763 : 185705 : dumps->bol = true;
4764 : 185705 : fputc ('\n', dumps->stream);
4765 : : }
4766 : : return true;
4767 : : }
4768 : :
4769 : : struct note_def_cache_hasher : ggc_cache_ptr_hash<tree_node>
4770 : : {
4771 : 156121 : static int keep_cache_entry (tree t)
4772 : : {
4773 : 156121 : if (!CHECKING_P)
4774 : : /* GTY is unfortunately not clever enough to conditionalize
4775 : : this. */
4776 : : gcc_unreachable ();
4777 : :
4778 : 156121 : if (ggc_marked_p (t))
4779 : : return -1;
4780 : :
4781 : 0 : unsigned n = dump.push (NULL);
4782 : : /* This might or might not be an error. We should note its
4783 : : dropping whichever. */
4784 : 0 : dump () && dump ("Dropping %N from note_defs table", t);
4785 : 0 : dump.pop (n);
4786 : :
4787 : 0 : return 0;
4788 : : }
4789 : : };
4790 : :
4791 : : /* We should stream each definition at most once.
4792 : : This needs to be a cache because there are cases where a definition
4793 : : ends up being not retained, and we need to drop those so we don't
4794 : : get confused if memory is reallocated. */
4795 : : typedef hash_table<note_def_cache_hasher> note_defs_table_t;
4796 : : static GTY((cache)) note_defs_table_t *note_defs;
4797 : :
4798 : : void
4799 : 244772 : trees_in::assert_definition (tree decl ATTRIBUTE_UNUSED,
4800 : : bool installing ATTRIBUTE_UNUSED)
4801 : : {
4802 : : #if CHECKING_P
4803 : 244772 : tree *slot = note_defs->find_slot (decl, installing ? INSERT : NO_INSERT);
4804 : 244772 : tree not_tmpl = STRIP_TEMPLATE (decl);
4805 : 244772 : if (installing)
4806 : : {
4807 : : /* We must be inserting for the first time. */
4808 : 115590 : gcc_assert (!*slot);
4809 : 115590 : *slot = decl;
4810 : : }
4811 : : else
4812 : : /* If this is not the mergeable entity, it should not be in the
4813 : : table. If it is a non-global-module mergeable entity, it
4814 : : should be in the table. Global module entities could have been
4815 : : defined textually in the current TU and so might or might not
4816 : : be present. */
4817 : 129182 : gcc_assert (!is_duplicate (decl)
4818 : : ? !slot
4819 : : : (slot
4820 : : || !DECL_LANG_SPECIFIC (not_tmpl)
4821 : : || !DECL_MODULE_PURVIEW_P (not_tmpl)
4822 : : || (!DECL_MODULE_IMPORT_P (not_tmpl)
4823 : : && header_module_p ())));
4824 : :
4825 : 244772 : if (not_tmpl != decl)
4826 : 141694 : gcc_assert (!note_defs->find_slot (not_tmpl, NO_INSERT));
4827 : : #endif
4828 : 244772 : }
4829 : :
4830 : : void
4831 : 309101 : trees_out::assert_definition (tree decl ATTRIBUTE_UNUSED)
4832 : : {
4833 : : #if CHECKING_P
4834 : 309101 : tree *slot = note_defs->find_slot (decl, INSERT);
4835 : 309101 : gcc_assert (!*slot);
4836 : 309101 : *slot = decl;
4837 : 309101 : if (TREE_CODE (decl) == TEMPLATE_DECL)
4838 : 167241 : gcc_assert (!note_defs->find_slot (DECL_TEMPLATE_RESULT (decl), NO_INSERT));
4839 : : #endif
4840 : 309101 : }
4841 : :
4842 : : /********************************************************************/
4843 : : static bool
4844 : 11536 : noisy_p ()
4845 : : {
4846 : 0 : if (quiet_flag)
4847 : : return false;
4848 : :
4849 : 0 : pp_needs_newline (global_dc->get_reference_printer ()) = true;
4850 : 0 : diagnostic_set_last_function (global_dc,
4851 : : (diagnostics::diagnostic_info *) nullptr);
4852 : :
4853 : 0 : return true;
4854 : : }
4855 : :
4856 : : /* Set the cmi repo. Strip trailing '/', '.' becomes NULL. */
4857 : :
4858 : : static void
4859 : 99324 : set_cmi_repo (const char *r)
4860 : : {
4861 : 99324 : XDELETEVEC (cmi_repo);
4862 : 99324 : XDELETEVEC (cmi_path);
4863 : 99324 : cmi_path_alloc = 0;
4864 : :
4865 : 99324 : cmi_repo = NULL;
4866 : 99324 : cmi_repo_length = 0;
4867 : :
4868 : 99324 : if (!r || !r[0])
4869 : : return;
4870 : :
4871 : 4486 : size_t len = strlen (r);
4872 : 4486 : cmi_repo = XNEWVEC (char, len + 1);
4873 : 4486 : memcpy (cmi_repo, r, len + 1);
4874 : :
4875 : 4486 : if (len > 1 && IS_DIR_SEPARATOR (cmi_repo[len-1]))
4876 : 4486 : len--;
4877 : 4486 : if (len == 1 && cmi_repo[0] == '.')
4878 : 21 : len--;
4879 : 4486 : cmi_repo[len] = 0;
4880 : 4486 : cmi_repo_length = len;
4881 : : }
4882 : :
4883 : : /* TO is a repo-relative name. Provide one that we may use from where
4884 : : we are. */
4885 : :
4886 : : static const char *
4887 : 5454 : maybe_add_cmi_prefix (const char *to, size_t *len_p = NULL)
4888 : : {
4889 : 5454 : size_t len = len_p || cmi_repo_length ? strlen (to) : 0;
4890 : :
4891 : 5454 : if (cmi_repo_length && !IS_ABSOLUTE_PATH (to))
4892 : : {
4893 : 5433 : if (cmi_path_alloc < cmi_repo_length + len + 2)
4894 : : {
4895 : 4377 : XDELETEVEC (cmi_path);
4896 : 4377 : cmi_path_alloc = cmi_repo_length + len * 2 + 2;
4897 : 4377 : cmi_path = XNEWVEC (char, cmi_path_alloc);
4898 : :
4899 : 4377 : memcpy (cmi_path, cmi_repo, cmi_repo_length);
4900 : 4377 : cmi_path[cmi_repo_length] = DIR_SEPARATOR;
4901 : : }
4902 : :
4903 : 5433 : memcpy (&cmi_path[cmi_repo_length + 1], to, len + 1);
4904 : 5433 : len += cmi_repo_length + 1;
4905 : 5433 : to = cmi_path;
4906 : : }
4907 : :
4908 : 5454 : if (len_p)
4909 : 2622 : *len_p = len;
4910 : :
4911 : 5454 : return to;
4912 : : }
4913 : :
4914 : : /* Try and create the directories of PATH. */
4915 : :
4916 : : static void
4917 : 33 : create_dirs (char *path)
4918 : : {
4919 : : /* Try and create the missing directories. */
4920 : 2077 : for (char *base = path; *base; base++)
4921 : 2044 : if (IS_DIR_SEPARATOR (*base))
4922 : : {
4923 : 202 : char sep = *base;
4924 : 202 : *base = 0;
4925 : 202 : int failed = mkdir (path, S_IRWXU | S_IRWXG | S_IRWXO);
4926 : 205 : dump () && dump ("Mkdir ('%s') errno:=%u", path, failed ? errno : 0);
4927 : 202 : *base = sep;
4928 : 202 : if (failed
4929 : : /* Maybe racing with another creator (of a *different*
4930 : : module). */
4931 : 40 : && errno != EEXIST)
4932 : : break;
4933 : : }
4934 : 33 : }
4935 : :
4936 : : /* Given a CLASSTYPE_DECL_LIST VALUE get the template friend decl,
4937 : : if that's what this is. */
4938 : :
4939 : : static tree
4940 : 61807 : friend_from_decl_list (tree frnd)
4941 : : {
4942 : 61807 : tree res = frnd;
4943 : :
4944 : 61807 : if (TREE_CODE (frnd) != TEMPLATE_DECL)
4945 : : {
4946 : 35315 : tree tmpl = NULL_TREE;
4947 : 35315 : if (TYPE_P (frnd))
4948 : : {
4949 : 5415 : res = TYPE_NAME (frnd);
4950 : 5331 : if (CLASS_TYPE_P (frnd)
4951 : 10746 : && CLASSTYPE_TEMPLATE_INFO (frnd))
4952 : 5322 : tmpl = CLASSTYPE_TI_TEMPLATE (frnd);
4953 : : }
4954 : 29900 : else if (DECL_TEMPLATE_INFO (frnd))
4955 : : {
4956 : 29900 : tmpl = DECL_TI_TEMPLATE (frnd);
4957 : 29900 : if (TREE_CODE (tmpl) != TEMPLATE_DECL)
4958 : : tmpl = NULL_TREE;
4959 : : }
4960 : :
4961 : 40148 : if (tmpl && DECL_TEMPLATE_RESULT (tmpl) == res)
4962 : : res = tmpl;
4963 : : }
4964 : :
4965 : 61807 : return res;
4966 : : }
4967 : :
4968 : : static tree
4969 : 20706 : find_enum_member (tree ctx, tree name)
4970 : : {
4971 : 20706 : for (tree values = TYPE_VALUES (ctx);
4972 : 424531 : values; values = TREE_CHAIN (values))
4973 : 419460 : if (DECL_NAME (TREE_VALUE (values)) == name)
4974 : : return TREE_VALUE (values);
4975 : :
4976 : : return NULL_TREE;
4977 : : }
4978 : :
4979 : : /********************************************************************/
4980 : : /* Instrumentation gathered writing bytes. */
4981 : :
4982 : : void
4983 : 273 : bytes_out::instrument ()
4984 : : {
4985 : 273 : dump ("Wrote %u bytes in %u blocks", lengths[3], spans[3]);
4986 : 273 : dump ("Wrote %u bits in %u bytes", lengths[0] + lengths[1], lengths[2]);
4987 : 819 : for (unsigned ix = 0; ix < 2; ix++)
4988 : 819 : dump (" %u %s spans of %R bits", spans[ix],
4989 : : ix ? "one" : "zero", lengths[ix], spans[ix]);
4990 : 273 : dump (" %u blocks with %R bits padding", spans[2],
4991 : 273 : lengths[2] * 8 - (lengths[0] + lengths[1]), spans[2]);
4992 : 273 : }
4993 : :
4994 : : /* Instrumentation gathered writing trees. */
4995 : : void
4996 : 2484 : trees_out::instrument ()
4997 : : {
4998 : 2484 : if (dump (""))
4999 : : {
5000 : 273 : bytes_out::instrument ();
5001 : 273 : dump ("Wrote:");
5002 : 273 : dump (" %u decl trees", decl_val_count);
5003 : 273 : dump (" %u other trees", tree_val_count);
5004 : 273 : dump (" %u back references", back_ref_count);
5005 : 273 : dump (" %u TU-local entities", tu_local_count);
5006 : 273 : dump (" %u null trees", null_count);
5007 : : }
5008 : 2484 : }
5009 : :
5010 : : /* Setup and teardown for a tree walk. */
5011 : :
5012 : : void
5013 : 1824686 : trees_out::begin ()
5014 : : {
5015 : 1824686 : gcc_assert (!streaming_p () || !tree_map.elements ());
5016 : :
5017 : 1824686 : mark_trees ();
5018 : 1824686 : if (streaming_p ())
5019 : 225149 : parent::begin ();
5020 : 1824686 : }
5021 : :
5022 : : unsigned
5023 : 225149 : trees_out::end (elf_out *sink, unsigned name, unsigned *crc_ptr)
5024 : : {
5025 : 225149 : gcc_checking_assert (streaming_p ());
5026 : :
5027 : 225149 : unmark_trees ();
5028 : 225149 : return parent::end (sink, name, crc_ptr);
5029 : : }
5030 : :
5031 : : void
5032 : 1599537 : trees_out::end ()
5033 : : {
5034 : 1599537 : gcc_assert (!streaming_p ());
5035 : :
5036 : 1599537 : unmark_trees ();
5037 : : /* Do not parent::end -- we weren't streaming. */
5038 : 1599537 : }
5039 : :
5040 : : void
5041 : 1824686 : trees_out::mark_trees ()
5042 : : {
5043 : 1824686 : if (size_t size = tree_map.elements ())
5044 : : {
5045 : : /* This isn't our first rodeo, destroy and recreate the
5046 : : tree_map. I'm a bad bad man. Use the previous size as a
5047 : : guess for the next one (so not all bad). */
5048 : 1382431 : tree_map.~ptr_int_hash_map ();
5049 : 1382431 : new (&tree_map) ptr_int_hash_map (size);
5050 : : }
5051 : :
5052 : : /* Install the fixed trees, with +ve references. */
5053 : 1824686 : unsigned limit = fixed_trees->length ();
5054 : 351961300 : for (unsigned ix = 0; ix != limit; ix++)
5055 : : {
5056 : 350136614 : tree val = (*fixed_trees)[ix];
5057 : 350136614 : bool existed = tree_map.put (val, ix + tag_fixed);
5058 : 350136614 : gcc_checking_assert (!TREE_VISITED (val) && !existed);
5059 : 350136614 : TREE_VISITED (val) = true;
5060 : : }
5061 : :
5062 : 1824686 : ref_num = 0;
5063 : 1824686 : }
5064 : :
5065 : : /* Unmark the trees we encountered */
5066 : :
5067 : : void
5068 : 1824686 : trees_out::unmark_trees ()
5069 : : {
5070 : 1824686 : ptr_int_hash_map::iterator end (tree_map.end ());
5071 : 412867569 : for (ptr_int_hash_map::iterator iter (tree_map.begin ()); iter != end; ++iter)
5072 : : {
5073 : 411042883 : tree node = reinterpret_cast<tree> ((*iter).first);
5074 : 411042883 : int ref = (*iter).second;
5075 : : /* We should have visited the node, and converted its mergeable
5076 : : reference to a regular reference. */
5077 : 411042883 : gcc_checking_assert (TREE_VISITED (node)
5078 : : && (ref <= tag_backref || ref >= tag_fixed));
5079 : 411042883 : TREE_VISITED (node) = false;
5080 : : }
5081 : 1824686 : }
5082 : :
5083 : : /* Mark DECL for by-value walking. We do this by inserting it into
5084 : : the tree map with a reference of zero. May be called multiple
5085 : : times on the same node. */
5086 : :
5087 : : void
5088 : 2836534 : trees_out::mark_by_value (tree decl)
5089 : : {
5090 : 2836534 : gcc_checking_assert (DECL_P (decl)
5091 : : /* Enum consts are INTEGER_CSTS. */
5092 : : || TREE_CODE (decl) == INTEGER_CST
5093 : : || TREE_CODE (decl) == TREE_BINFO);
5094 : :
5095 : 2836534 : if (TREE_VISITED (decl))
5096 : : /* Must already be forced or fixed. */
5097 : 2264 : gcc_checking_assert (*tree_map.get (decl) >= tag_value);
5098 : : else
5099 : : {
5100 : 2834270 : bool existed = tree_map.put (decl, tag_value);
5101 : 2834270 : gcc_checking_assert (!existed);
5102 : 2834270 : TREE_VISITED (decl) = true;
5103 : : }
5104 : 2836534 : }
5105 : :
5106 : : int
5107 : 74339963 : trees_out::get_tag (tree t)
5108 : : {
5109 : 74339963 : gcc_checking_assert (TREE_VISITED (t));
5110 : 74339963 : return *tree_map.get (t);
5111 : : }
5112 : :
5113 : : /* Insert T into the map, return its tag number. */
5114 : :
5115 : : int
5116 : 60906269 : trees_out::insert (tree t, walk_kind walk)
5117 : : {
5118 : 60906269 : gcc_checking_assert (walk != WK_normal || !TREE_VISITED (t));
5119 : 60906269 : int tag = --ref_num;
5120 : 60906269 : bool existed;
5121 : 60906269 : int &slot = tree_map.get_or_insert (t, &existed);
5122 : 60906269 : gcc_checking_assert (TREE_VISITED (t) == existed
5123 : : && (!existed
5124 : : || (walk == WK_value && slot == tag_value)));
5125 : 60906269 : TREE_VISITED (t) = true;
5126 : 60906269 : slot = tag;
5127 : :
5128 : 60906269 : return tag;
5129 : : }
5130 : :
5131 : : /* Insert T into the backreference array. Return its back reference
5132 : : number. */
5133 : :
5134 : : int
5135 : 14781783 : trees_in::insert (tree t)
5136 : : {
5137 : 14781783 : gcc_checking_assert (t || get_overrun ());
5138 : 14781783 : back_refs.safe_push (t);
5139 : 14781783 : return -(int)back_refs.length ();
5140 : : }
5141 : :
5142 : : /* A chained set of decls. */
5143 : :
5144 : : void
5145 : 88852 : trees_out::chained_decls (tree decls)
5146 : : {
5147 : 208152 : for (; decls; decls = DECL_CHAIN (decls))
5148 : 119300 : tree_node (decls);
5149 : 88852 : tree_node (NULL_TREE);
5150 : 88852 : }
5151 : :
5152 : : tree
5153 : 35269 : trees_in::chained_decls ()
5154 : : {
5155 : 35269 : tree decls = NULL_TREE;
5156 : 35269 : for (tree *chain = &decls;;)
5157 : 87204 : if (tree decl = tree_node ())
5158 : : {
5159 : 51935 : if (!DECL_P (decl) || DECL_CHAIN (decl))
5160 : : {
5161 : 0 : set_overrun ();
5162 : 0 : break;
5163 : : }
5164 : 51935 : *chain = decl;
5165 : 51935 : chain = &DECL_CHAIN (decl);
5166 : : }
5167 : : else
5168 : 51935 : break;
5169 : :
5170 : 35269 : return decls;
5171 : : }
5172 : :
5173 : : /* A vector of decls following DECL_CHAIN. */
5174 : :
5175 : : void
5176 : 281374 : trees_out::vec_chained_decls (tree decls)
5177 : : {
5178 : 281374 : if (streaming_p ())
5179 : : {
5180 : : unsigned len = 0;
5181 : :
5182 : 810723 : for (tree decl = decls; decl; decl = DECL_CHAIN (decl))
5183 : 670076 : len++;
5184 : 140647 : u (len);
5185 : : }
5186 : :
5187 : 1621852 : for (tree decl = decls; decl; decl = DECL_CHAIN (decl))
5188 : : {
5189 : 292227 : if (DECL_IMPLICIT_TYPEDEF_P (decl)
5190 : 1351195 : && TYPE_NAME (TREE_TYPE (decl)) != decl)
5191 : : /* An anonynmous struct with a typedef name. An odd thing to
5192 : : write. */
5193 : 8 : tree_node (NULL_TREE);
5194 : : else
5195 : 1340470 : tree_node (decl);
5196 : : }
5197 : 281374 : }
5198 : :
5199 : : vec<tree, va_heap> *
5200 : 103418 : trees_in::vec_chained_decls ()
5201 : : {
5202 : 103418 : vec<tree, va_heap> *v = NULL;
5203 : :
5204 : 103418 : if (unsigned len = u ())
5205 : : {
5206 : 54274 : vec_alloc (v, len);
5207 : :
5208 : 605731 : for (unsigned ix = 0; ix < len; ix++)
5209 : : {
5210 : 551457 : tree decl = tree_node ();
5211 : 551457 : if (decl && !DECL_P (decl))
5212 : : {
5213 : 0 : set_overrun ();
5214 : 0 : break;
5215 : : }
5216 : 551457 : v->quick_push (decl);
5217 : : }
5218 : :
5219 : 54274 : if (get_overrun ())
5220 : : {
5221 : 0 : vec_free (v);
5222 : 0 : v = NULL;
5223 : : }
5224 : : }
5225 : :
5226 : 103418 : return v;
5227 : : }
5228 : :
5229 : : /* A vector of trees. */
5230 : :
5231 : : void
5232 : 199344 : trees_out::tree_vec (vec<tree, va_gc> *v)
5233 : : {
5234 : 199344 : unsigned len = vec_safe_length (v);
5235 : 199344 : if (streaming_p ())
5236 : 99652 : u (len);
5237 : 256574 : for (unsigned ix = 0; ix != len; ix++)
5238 : 57230 : tree_node ((*v)[ix]);
5239 : 199344 : }
5240 : :
5241 : : vec<tree, va_gc> *
5242 : 73414 : trees_in::tree_vec ()
5243 : : {
5244 : 73414 : vec<tree, va_gc> *v = NULL;
5245 : 73414 : if (unsigned len = u ())
5246 : : {
5247 : 19336 : vec_alloc (v, len);
5248 : 40556 : for (unsigned ix = 0; ix != len; ix++)
5249 : 21220 : v->quick_push (tree_node ());
5250 : : }
5251 : 73414 : return v;
5252 : : }
5253 : :
5254 : : /* A vector of tree pairs. */
5255 : :
5256 : : void
5257 : 4820 : trees_out::tree_pair_vec (vec<tree_pair_s, va_gc> *v)
5258 : : {
5259 : 4820 : unsigned len = vec_safe_length (v);
5260 : 4820 : if (streaming_p ())
5261 : 2410 : u (len);
5262 : 4820 : if (len)
5263 : 25734 : for (unsigned ix = 0; ix != len; ix++)
5264 : : {
5265 : 21022 : tree_pair_s const &s = (*v)[ix];
5266 : 21022 : tree_node (s.purpose);
5267 : 21022 : tree_node (s.value);
5268 : : }
5269 : 4820 : }
5270 : :
5271 : : vec<tree_pair_s, va_gc> *
5272 : 2078 : trees_in::tree_pair_vec ()
5273 : : {
5274 : 2078 : vec<tree_pair_s, va_gc> *v = NULL;
5275 : 2078 : if (unsigned len = u ())
5276 : : {
5277 : 2032 : vec_alloc (v, len);
5278 : 11293 : for (unsigned ix = 0; ix != len; ix++)
5279 : : {
5280 : 9261 : tree_pair_s s;
5281 : 9261 : s.purpose = tree_node ();
5282 : 9261 : s.value = tree_node ();
5283 : 9261 : v->quick_push (s);
5284 : : }
5285 : : }
5286 : 2078 : return v;
5287 : : }
5288 : :
5289 : : void
5290 : 297528 : trees_out::tree_list (tree list, bool has_purpose)
5291 : : {
5292 : 1234301 : for (; list; list = TREE_CHAIN (list))
5293 : : {
5294 : 936773 : gcc_checking_assert (TREE_VALUE (list));
5295 : 936773 : tree_node (TREE_VALUE (list));
5296 : 936773 : if (has_purpose)
5297 : 906285 : tree_node (TREE_PURPOSE (list));
5298 : : }
5299 : 297528 : tree_node (NULL_TREE);
5300 : 297528 : }
5301 : :
5302 : : tree
5303 : 110871 : trees_in::tree_list (bool has_purpose)
5304 : : {
5305 : 110871 : tree res = NULL_TREE;
5306 : :
5307 : 494744 : for (tree *chain = &res; tree value = tree_node ();
5308 : 767746 : chain = &TREE_CHAIN (*chain))
5309 : : {
5310 : 383873 : tree purpose = has_purpose ? tree_node () : NULL_TREE;
5311 : 383873 : *chain = build_tree_list (purpose, value);
5312 : 383873 : }
5313 : :
5314 : 110871 : return res;
5315 : : }
5316 : :
5317 : : #define CASE_OMP_SIMD_CODE \
5318 : : case OMP_SIMD: \
5319 : : case OMP_STRUCTURED_BLOCK: \
5320 : : case OMP_LOOP: \
5321 : : case OMP_ORDERED: \
5322 : : case OMP_TILE: \
5323 : : case OMP_UNROLL
5324 : : #define CASE_OMP_CODE \
5325 : : case OMP_PARALLEL: \
5326 : : case OMP_TASK: \
5327 : : case OMP_FOR: \
5328 : : case OMP_DISTRIBUTE: \
5329 : : case OMP_TASKLOOP: \
5330 : : case OMP_TEAMS: \
5331 : : case OMP_TARGET_DATA: \
5332 : : case OMP_TARGET: \
5333 : : case OMP_SECTIONS: \
5334 : : case OMP_CRITICAL: \
5335 : : case OMP_SINGLE: \
5336 : : case OMP_SCOPE: \
5337 : : case OMP_TASKGROUP: \
5338 : : case OMP_MASKED: \
5339 : : case OMP_DISPATCH: \
5340 : : case OMP_INTEROP: \
5341 : : case OMP_MASTER: \
5342 : : case OMP_TARGET_UPDATE: \
5343 : : case OMP_TARGET_ENTER_DATA: \
5344 : : case OMP_TARGET_EXIT_DATA: \
5345 : : case OMP_METADIRECTIVE: \
5346 : : case OMP_ATOMIC: \
5347 : : case OMP_ATOMIC_READ: \
5348 : : case OMP_ATOMIC_CAPTURE_OLD: \
5349 : : case OMP_ATOMIC_CAPTURE_NEW
5350 : : #define CASE_OACC_CODE \
5351 : : case OACC_PARALLEL: \
5352 : : case OACC_KERNELS: \
5353 : : case OACC_SERIAL: \
5354 : : case OACC_DATA: \
5355 : : case OACC_HOST_DATA: \
5356 : : case OACC_LOOP: \
5357 : : case OACC_CACHE: \
5358 : : case OACC_DECLARE: \
5359 : : case OACC_ENTER_DATA: \
5360 : : case OACC_EXIT_DATA: \
5361 : : case OACC_UPDATE
5362 : :
5363 : : /* Start tree write. Write information to allocate the receiving
5364 : : node. */
5365 : :
5366 : : void
5367 : 12327066 : trees_out::start (tree t, bool code_streamed)
5368 : : {
5369 : 12327066 : if (TYPE_P (t))
5370 : : {
5371 : 500347 : enum tree_code code = TREE_CODE (t);
5372 : 500347 : gcc_checking_assert (TYPE_MAIN_VARIANT (t) == t);
5373 : : /* All these types are TYPE_NON_COMMON. */
5374 : 500347 : gcc_checking_assert (code == RECORD_TYPE
5375 : : || code == UNION_TYPE
5376 : : || code == ENUMERAL_TYPE
5377 : : || code == TEMPLATE_TYPE_PARM
5378 : : || code == TEMPLATE_TEMPLATE_PARM
5379 : : || code == BOUND_TEMPLATE_TEMPLATE_PARM);
5380 : : }
5381 : :
5382 : 12327066 : if (!code_streamed)
5383 : 11879293 : u (TREE_CODE (t));
5384 : :
5385 : 12327066 : switch (TREE_CODE (t))
5386 : : {
5387 : 10924684 : default:
5388 : 10924684 : if (VL_EXP_CLASS_P (t))
5389 : 460237 : u (VL_EXP_OPERAND_LENGTH (t));
5390 : : break;
5391 : :
5392 : 563291 : case INTEGER_CST:
5393 : 563291 : u (TREE_INT_CST_NUNITS (t));
5394 : 563291 : u (TREE_INT_CST_EXT_NUNITS (t));
5395 : 563291 : break;
5396 : :
5397 : 12 : case OMP_CLAUSE:
5398 : 12 : u (OMP_CLAUSE_CODE (t));
5399 : 12 : break;
5400 : :
5401 : 6 : CASE_OMP_SIMD_CODE:
5402 : 6 : state->extensions |= SE_OPENMP_SIMD;
5403 : 6 : break;
5404 : :
5405 : 3 : CASE_OMP_CODE:
5406 : 3 : state->extensions |= SE_OPENMP;
5407 : 3 : break;
5408 : :
5409 : 6 : CASE_OACC_CODE:
5410 : 6 : state->extensions |= SE_OPENACC;
5411 : 6 : break;
5412 : :
5413 : 41449 : case STRING_CST:
5414 : 41449 : str (TREE_STRING_POINTER (t), TREE_STRING_LENGTH (t));
5415 : 41449 : break;
5416 : :
5417 : 18 : case RAW_DATA_CST:
5418 : 18 : if (RAW_DATA_OWNER (t) == NULL_TREE)
5419 : : {
5420 : : /* Stream RAW_DATA_CST with no owner (i.e. data pointing
5421 : : into libcpp buffers) as something we can stream in as
5422 : : STRING_CST which owns the data. */
5423 : 6 : u (0);
5424 : : /* Can't use str (RAW_DATA_POINTER (t), RAW_DATA_LENGTH (t));
5425 : : here as there isn't a null termination after it. */
5426 : 6 : z (RAW_DATA_LENGTH (t));
5427 : 6 : if (RAW_DATA_LENGTH (t))
5428 : 6 : if (void *ptr = buf (RAW_DATA_LENGTH (t) + 1))
5429 : : {
5430 : 6 : memcpy (ptr, RAW_DATA_POINTER (t), RAW_DATA_LENGTH (t));
5431 : 6 : ((char *) ptr)[RAW_DATA_LENGTH (t)] = '\0';
5432 : : }
5433 : : }
5434 : : else
5435 : : {
5436 : 12 : gcc_assert (RAW_DATA_LENGTH (t));
5437 : 12 : u (RAW_DATA_LENGTH (t));
5438 : : }
5439 : : break;
5440 : :
5441 : 18 : case VECTOR_CST:
5442 : 18 : u (VECTOR_CST_LOG2_NPATTERNS (t));
5443 : 18 : u (VECTOR_CST_NELTS_PER_PATTERN (t));
5444 : 18 : break;
5445 : :
5446 : 97242 : case TREE_BINFO:
5447 : 97242 : u (BINFO_N_BASE_BINFOS (t));
5448 : 97242 : break;
5449 : :
5450 : 700337 : case TREE_VEC:
5451 : 700337 : u (TREE_VEC_LENGTH (t));
5452 : 700337 : break;
5453 : :
5454 : 0 : case FIXED_CST:
5455 : 0 : gcc_unreachable (); /* Not supported in C++. */
5456 : 0 : break;
5457 : :
5458 : 0 : case IDENTIFIER_NODE:
5459 : 0 : case SSA_NAME:
5460 : 0 : case TARGET_MEM_REF:
5461 : 0 : case TRANSLATION_UNIT_DECL:
5462 : : /* We shouldn't meet these. */
5463 : 0 : gcc_unreachable ();
5464 : 12327066 : break;
5465 : : }
5466 : 12327066 : }
5467 : :
5468 : : /* Start tree read. Allocate the receiving node. */
5469 : :
5470 : : tree
5471 : 10508310 : trees_in::start (unsigned code)
5472 : : {
5473 : 10508310 : tree t = NULL_TREE;
5474 : :
5475 : 10508310 : if (!code)
5476 : 9494448 : code = u ();
5477 : :
5478 : 10508310 : switch (code)
5479 : : {
5480 : 9345121 : default:
5481 : 9345121 : if (code >= MAX_TREE_CODES)
5482 : : {
5483 : 0 : fail:
5484 : 0 : set_overrun ();
5485 : 0 : return NULL_TREE;
5486 : : }
5487 : 9345121 : else if (TREE_CODE_CLASS (code) == tcc_vl_exp)
5488 : : {
5489 : 398131 : unsigned ops = u ();
5490 : 398131 : t = build_vl_exp (tree_code (code), ops);
5491 : : }
5492 : : else
5493 : 8946990 : t = make_node (tree_code (code));
5494 : : break;
5495 : :
5496 : 471206 : case INTEGER_CST:
5497 : 471206 : {
5498 : 471206 : unsigned n = u ();
5499 : 471206 : unsigned e = u ();
5500 : 471206 : t = make_int_cst (n, e);
5501 : : }
5502 : 471206 : break;
5503 : :
5504 : 12 : case OMP_CLAUSE:
5505 : 12 : t = build_omp_clause (UNKNOWN_LOCATION, omp_clause_code (u ()));
5506 : 12 : break;
5507 : :
5508 : 9 : CASE_OMP_SIMD_CODE:
5509 : 9 : if (!(state->extensions & SE_OPENMP_SIMD))
5510 : 0 : goto fail;
5511 : 9 : t = make_node (tree_code (code));
5512 : 9 : break;
5513 : :
5514 : 3 : CASE_OMP_CODE:
5515 : 3 : if (!(state->extensions & SE_OPENMP))
5516 : 0 : goto fail;
5517 : 3 : t = make_node (tree_code (code));
5518 : 3 : break;
5519 : :
5520 : 6 : CASE_OACC_CODE:
5521 : 6 : if (!(state->extensions & SE_OPENACC))
5522 : 0 : goto fail;
5523 : 6 : t = make_node (tree_code (code));
5524 : 6 : break;
5525 : :
5526 : 39408 : case STRING_CST:
5527 : 39408 : {
5528 : 39408 : size_t l;
5529 : 39408 : const char *chars = str (&l);
5530 : 39408 : t = build_string (l, chars);
5531 : : }
5532 : 39408 : break;
5533 : :
5534 : 9 : case RAW_DATA_CST:
5535 : 9 : {
5536 : 9 : size_t l = u ();
5537 : 9 : if (l == 0)
5538 : : {
5539 : : /* Stream in RAW_DATA_CST with no owner as STRING_CST
5540 : : which owns the data. */
5541 : 3 : const char *chars = str (&l);
5542 : 3 : t = build_string (l, chars);
5543 : : }
5544 : : else
5545 : : {
5546 : 6 : t = make_node (RAW_DATA_CST);
5547 : 6 : RAW_DATA_LENGTH (t) = l;
5548 : : }
5549 : : }
5550 : 9 : break;
5551 : :
5552 : 24 : case VECTOR_CST:
5553 : 24 : {
5554 : 24 : unsigned log2_npats = u ();
5555 : 24 : unsigned elts_per = u ();
5556 : 24 : t = make_vector (log2_npats, elts_per);
5557 : : }
5558 : 24 : break;
5559 : :
5560 : 71336 : case TREE_BINFO:
5561 : 71336 : t = make_tree_binfo (u ());
5562 : 71336 : break;
5563 : :
5564 : 581176 : case TREE_VEC:
5565 : 581176 : t = make_tree_vec (u ());
5566 : 581176 : break;
5567 : :
5568 : 0 : case FIXED_CST:
5569 : 0 : case IDENTIFIER_NODE:
5570 : 0 : case SSA_NAME:
5571 : 0 : case TARGET_MEM_REF:
5572 : 0 : case TRANSLATION_UNIT_DECL:
5573 : 0 : goto fail;
5574 : : }
5575 : :
5576 : : return t;
5577 : : }
5578 : :
5579 : : /* The kinds of interface an importer could have for a decl. */
5580 : :
5581 : : enum class importer_interface {
5582 : : unknown, /* The definition may or may not need to be emitted. */
5583 : : external, /* The definition can always be found in another TU. */
5584 : : internal, /* The definition should be emitted in the importer's TU. */
5585 : : always_emit, /* The definition must be emitted in the importer's TU,
5586 : : regardless of if it's used or not. */
5587 : : };
5588 : :
5589 : : /* Returns what kind of interface an importer will have of DECL. */
5590 : :
5591 : : static importer_interface
5592 : 473871 : get_importer_interface (tree decl)
5593 : : {
5594 : : /* Internal linkage entities must be emitted in each importer if
5595 : : there is a definition available. */
5596 : 473871 : if (!TREE_PUBLIC (decl))
5597 : : return importer_interface::internal;
5598 : :
5599 : : /* Other entities that aren't vague linkage are either not definitions
5600 : : or will be publicly emitted in this TU, so importers can just refer
5601 : : to an external definition. */
5602 : 247604 : if (!vague_linkage_p (decl))
5603 : : return importer_interface::external;
5604 : :
5605 : : /* For explicit instantiations, importers can always rely on there
5606 : : being a definition in another TU, unless this is a definition
5607 : : in a header module: in which case the importer will always need
5608 : : to emit it. */
5609 : 242610 : if (DECL_LANG_SPECIFIC (decl)
5610 : 242610 : && DECL_EXPLICIT_INSTANTIATION (decl))
5611 : 18965 : return (header_module_p () && !DECL_EXTERNAL (decl)
5612 : 18965 : ? importer_interface::always_emit
5613 : : : importer_interface::external);
5614 : :
5615 : : /* A gnu_inline function is never emitted in any TU. */
5616 : 223645 : if (TREE_CODE (decl) == FUNCTION_DECL
5617 : 153081 : && DECL_DECLARED_INLINE_P (decl)
5618 : 371756 : && lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl)))
5619 : : return importer_interface::external;
5620 : :
5621 : : /* Everything else has vague linkage. */
5622 : : return importer_interface::unknown;
5623 : : }
5624 : :
5625 : : /* The structure streamers access the raw fields, because the
5626 : : alternative, of using the accessor macros can require using
5627 : : different accessors for the same underlying field, depending on the
5628 : : tree code. That's both confusing and annoying. */
5629 : :
5630 : : /* Read & write the core boolean flags. */
5631 : :
5632 : : void
5633 : 12346436 : trees_out::core_bools (tree t, bits_out& bits)
5634 : : {
5635 : : #define WB(X) (bits.b (X))
5636 : : /* Stream X if COND holds, and if !COND stream a dummy value so that the
5637 : : overall number of bits streamed is independent of the runtime value
5638 : : of COND, which allows the compiler to better optimize this function. */
5639 : : #define WB_IF(COND, X) WB ((COND) ? (X) : false)
5640 : 12346436 : tree_code code = TREE_CODE (t);
5641 : :
5642 : 12346436 : WB (t->base.side_effects_flag);
5643 : 12346436 : WB (t->base.constant_flag);
5644 : 12346436 : WB (t->base.addressable_flag);
5645 : 12346436 : WB (t->base.volatile_flag);
5646 : 12346436 : WB (t->base.readonly_flag);
5647 : : /* base.asm_written_flag is a property of the current TU's use of
5648 : : this decl. */
5649 : 12346436 : WB (t->base.nowarning_flag);
5650 : : /* base.visited read as zero (it's set for writer, because that's
5651 : : how we mark nodes). */
5652 : : /* base.used_flag is not streamed. Readers may set TREE_USED of
5653 : : decls they use. */
5654 : 12346436 : WB (t->base.nothrow_flag);
5655 : 12346436 : WB (t->base.static_flag);
5656 : : /* This is TYPE_CACHED_VALUES_P for types. */
5657 : 12346436 : WB_IF (TREE_CODE_CLASS (code) != tcc_type, t->base.public_flag);
5658 : 12346436 : WB (t->base.private_flag);
5659 : 12346436 : WB (t->base.protected_flag);
5660 : 12346436 : WB (t->base.deprecated_flag);
5661 : 12346436 : WB (t->base.default_def_flag);
5662 : :
5663 : 12346436 : switch (code)
5664 : : {
5665 : : case CALL_EXPR:
5666 : : case INTEGER_CST:
5667 : : case SSA_NAME:
5668 : : case TARGET_MEM_REF:
5669 : : case TREE_VEC:
5670 : : /* These use different base.u fields. */
5671 : : return;
5672 : :
5673 : 10633805 : default:
5674 : 10633805 : WB (t->base.u.bits.lang_flag_0);
5675 : 10633805 : bool flag_1 = t->base.u.bits.lang_flag_1;
5676 : 10633805 : if (!flag_1)
5677 : : ;
5678 : 339145 : else if (code == TEMPLATE_INFO)
5679 : : /* This is TI_PENDING_TEMPLATE_FLAG, not relevant to reader. */
5680 : : flag_1 = false;
5681 : 335520 : else if (code == VAR_DECL)
5682 : : {
5683 : : /* This is DECL_INITIALIZED_P. */
5684 : 69245 : if (TREE_CODE (DECL_CONTEXT (t)) != FUNCTION_DECL)
5685 : : /* We'll set this when reading the definition. */
5686 : 10633805 : flag_1 = false;
5687 : : }
5688 : 10633805 : WB (flag_1);
5689 : 10633805 : WB (t->base.u.bits.lang_flag_2);
5690 : 10633805 : WB (t->base.u.bits.lang_flag_3);
5691 : 10633805 : WB (t->base.u.bits.lang_flag_4);
5692 : 10633805 : WB (t->base.u.bits.lang_flag_5);
5693 : 10633805 : WB (t->base.u.bits.lang_flag_6);
5694 : 10633805 : WB (t->base.u.bits.saturating_flag);
5695 : 10633805 : WB (t->base.u.bits.unsigned_flag);
5696 : 10633805 : WB (t->base.u.bits.packed_flag);
5697 : 10633805 : WB (t->base.u.bits.user_align);
5698 : 10633805 : WB (t->base.u.bits.nameless_flag);
5699 : 10633805 : WB (t->base.u.bits.atomic_flag);
5700 : 10633805 : WB (t->base.u.bits.unavailable_flag);
5701 : 10633805 : break;
5702 : : }
5703 : :
5704 : 10633805 : if (TREE_CODE_CLASS (code) == tcc_type)
5705 : : {
5706 : 519717 : WB (t->type_common.no_force_blk_flag);
5707 : 519717 : WB (t->type_common.needs_constructing_flag);
5708 : 519717 : WB (t->type_common.transparent_aggr_flag);
5709 : 519717 : WB (t->type_common.restrict_flag);
5710 : 519717 : WB (t->type_common.string_flag);
5711 : 519717 : WB (t->type_common.lang_flag_0);
5712 : 519717 : WB (t->type_common.lang_flag_1);
5713 : 519717 : WB (t->type_common.lang_flag_2);
5714 : 519717 : WB (t->type_common.lang_flag_3);
5715 : 519717 : WB (t->type_common.lang_flag_4);
5716 : 519717 : WB (t->type_common.lang_flag_5);
5717 : 519717 : WB (t->type_common.lang_flag_6);
5718 : 519717 : WB (t->type_common.typeless_storage);
5719 : : }
5720 : :
5721 : 10633805 : if (TREE_CODE_CLASS (code) != tcc_declaration)
5722 : : return;
5723 : :
5724 : 2767729 : if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
5725 : : {
5726 : 2767729 : WB (t->decl_common.nonlocal_flag);
5727 : 2767729 : WB (t->decl_common.virtual_flag);
5728 : 2767729 : WB (t->decl_common.ignored_flag);
5729 : 2767729 : WB (t->decl_common.abstract_flag);
5730 : 2767729 : WB (t->decl_common.artificial_flag);
5731 : 2767729 : WB (t->decl_common.preserve_flag);
5732 : 2767729 : WB (t->decl_common.debug_expr_is_from);
5733 : 2767729 : WB (t->decl_common.lang_flag_0);
5734 : 2767729 : WB (t->decl_common.lang_flag_1);
5735 : 2767729 : WB (t->decl_common.lang_flag_2);
5736 : 2767729 : WB (t->decl_common.lang_flag_3);
5737 : 2767729 : WB (t->decl_common.lang_flag_4);
5738 : :
5739 : 2767729 : {
5740 : : /* This is DECL_INTERFACE_KNOWN: We should redetermine whether
5741 : : we need to import or export any vague-linkage entities on
5742 : : stream-in. */
5743 : 2767729 : bool interface_known = t->decl_common.lang_flag_5;
5744 : 2767729 : if (interface_known
5745 : 2767729 : && get_importer_interface (t) == importer_interface::unknown)
5746 : : interface_known = false;
5747 : 2767729 : WB (interface_known);
5748 : : }
5749 : :
5750 : 2767729 : WB (t->decl_common.lang_flag_6);
5751 : 2767729 : WB (t->decl_common.lang_flag_7);
5752 : 2767729 : WB (t->decl_common.lang_flag_8);
5753 : 2767729 : WB (t->decl_common.decl_flag_0);
5754 : :
5755 : 2767729 : {
5756 : : /* DECL_EXTERNAL -> decl_flag_1
5757 : : == it is defined elsewhere
5758 : : DECL_NOT_REALLY_EXTERN -> base.not_really_extern
5759 : : == that was a lie, it is here */
5760 : :
5761 : 2767729 : bool is_external = t->decl_common.decl_flag_1;
5762 : : /* maybe_emit_vtables relies on vtables being marked as
5763 : : DECL_EXTERNAL and DECL_NOT_REALLY_EXTERN before processing. */
5764 : 2767729 : if (!is_external && VAR_P (t) && DECL_VTABLE_OR_VTT_P (t))
5765 : : is_external = true;
5766 : : /* Things we emit here might well be external from the POV of an
5767 : : importer. */
5768 : 2767522 : if (!is_external
5769 : 2324813 : && VAR_OR_FUNCTION_DECL_P (t)
5770 : 2975488 : && get_importer_interface (t) == importer_interface::external)
5771 : : is_external = true;
5772 : 2767729 : WB (is_external);
5773 : : }
5774 : :
5775 : 2767729 : WB (t->decl_common.decl_flag_2);
5776 : 2767729 : WB (t->decl_common.decl_flag_3);
5777 : 2767729 : WB (t->decl_common.not_gimple_reg_flag);
5778 : 2767729 : WB (t->decl_common.decl_by_reference_flag);
5779 : 2767729 : WB (t->decl_common.decl_read_flag);
5780 : 2767729 : WB (t->decl_common.decl_nonshareable_flag);
5781 : 2767729 : WB (t->decl_common.decl_not_flexarray);
5782 : : }
5783 : : else
5784 : : return;
5785 : :
5786 : 2767729 : if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
5787 : : {
5788 : 1317220 : WB (t->decl_with_vis.defer_output);
5789 : 1317220 : WB (t->decl_with_vis.hard_register);
5790 : 1317220 : WB (t->decl_with_vis.common_flag);
5791 : 1317220 : WB (t->decl_with_vis.in_text_section);
5792 : 1317220 : WB (t->decl_with_vis.in_constant_pool);
5793 : 1317220 : WB (t->decl_with_vis.dllimport_flag);
5794 : 1317220 : WB (t->decl_with_vis.weak_flag);
5795 : 1317220 : WB (t->decl_with_vis.seen_in_bind_expr);
5796 : 1317220 : WB (t->decl_with_vis.comdat_flag);
5797 : 1317220 : WB (t->decl_with_vis.visibility_specified);
5798 : 1317220 : WB (t->decl_with_vis.init_priority_p);
5799 : 1317220 : WB (t->decl_with_vis.shadowed_for_var_p);
5800 : 1317220 : WB (t->decl_with_vis.cxx_constructor);
5801 : 1317220 : WB (t->decl_with_vis.cxx_destructor);
5802 : 1317220 : WB (t->decl_with_vis.final);
5803 : 1317220 : WB (t->decl_with_vis.regdecl_flag);
5804 : : }
5805 : : else
5806 : : return;
5807 : :
5808 : 1317220 : if (CODE_CONTAINS_STRUCT (code, TS_FUNCTION_DECL))
5809 : : {
5810 : 378541 : WB (t->function_decl.static_ctor_flag);
5811 : 378541 : WB (t->function_decl.static_dtor_flag);
5812 : 378541 : WB (t->function_decl.uninlinable);
5813 : 378541 : WB (t->function_decl.possibly_inlined);
5814 : 378541 : WB (t->function_decl.novops_flag);
5815 : 378541 : WB (t->function_decl.returns_twice_flag);
5816 : 378541 : WB (t->function_decl.malloc_flag);
5817 : 378541 : WB (t->function_decl.declared_inline_flag);
5818 : 378541 : WB (t->function_decl.no_inline_warning_flag);
5819 : 378541 : WB (t->function_decl.no_instrument_function_entry_exit);
5820 : 378541 : WB (t->function_decl.no_limit_stack);
5821 : 378541 : WB (t->function_decl.disregard_inline_limits);
5822 : 378541 : WB (t->function_decl.pure_flag);
5823 : 378541 : WB (t->function_decl.looping_const_or_pure_flag);
5824 : :
5825 : 378541 : WB (t->function_decl.has_debug_args_flag);
5826 : 378541 : WB (t->function_decl.versioned_function);
5827 : 378541 : WB (t->function_decl.replaceable_operator);
5828 : :
5829 : : /* decl_type is a (misnamed) 2 bit discriminator. */
5830 : 378541 : unsigned kind = (unsigned)t->function_decl.decl_type;
5831 : 378541 : WB ((kind >> 0) & 1);
5832 : 378541 : WB ((kind >> 1) & 1);
5833 : : }
5834 : : #undef WB_IF
5835 : : #undef WB
5836 : : }
5837 : :
5838 : : bool
5839 : 10523905 : trees_in::core_bools (tree t, bits_in& bits)
5840 : : {
5841 : : #define RB(X) ((X) = bits.b ())
5842 : : /* See the comment for WB_IF in trees_out::core_bools. */
5843 : : #define RB_IF(COND, X) ((COND) ? RB (X) : bits.b ())
5844 : :
5845 : 10523905 : tree_code code = TREE_CODE (t);
5846 : :
5847 : 10523905 : RB (t->base.side_effects_flag);
5848 : 10523905 : RB (t->base.constant_flag);
5849 : 10523905 : RB (t->base.addressable_flag);
5850 : 10523905 : RB (t->base.volatile_flag);
5851 : 10523905 : RB (t->base.readonly_flag);
5852 : : /* base.asm_written_flag is not streamed. */
5853 : 10523905 : RB (t->base.nowarning_flag);
5854 : : /* base.visited is not streamed. */
5855 : : /* base.used_flag is not streamed. */
5856 : 10523905 : RB (t->base.nothrow_flag);
5857 : 10523905 : RB (t->base.static_flag);
5858 : 10523905 : RB_IF (TREE_CODE_CLASS (code) != tcc_type, t->base.public_flag);
5859 : 10523905 : RB (t->base.private_flag);
5860 : 10523905 : RB (t->base.protected_flag);
5861 : 10523905 : RB (t->base.deprecated_flag);
5862 : 10523905 : RB (t->base.default_def_flag);
5863 : :
5864 : 10523905 : switch (code)
5865 : : {
5866 : 1440900 : case CALL_EXPR:
5867 : 1440900 : case INTEGER_CST:
5868 : 1440900 : case SSA_NAME:
5869 : 1440900 : case TARGET_MEM_REF:
5870 : 1440900 : case TREE_VEC:
5871 : : /* These use different base.u fields. */
5872 : 1440900 : goto done;
5873 : :
5874 : 9083005 : default:
5875 : 9083005 : RB (t->base.u.bits.lang_flag_0);
5876 : 9083005 : RB (t->base.u.bits.lang_flag_1);
5877 : 9083005 : RB (t->base.u.bits.lang_flag_2);
5878 : 9083005 : RB (t->base.u.bits.lang_flag_3);
5879 : 9083005 : RB (t->base.u.bits.lang_flag_4);
5880 : 9083005 : RB (t->base.u.bits.lang_flag_5);
5881 : 9083005 : RB (t->base.u.bits.lang_flag_6);
5882 : 9083005 : RB (t->base.u.bits.saturating_flag);
5883 : 9083005 : RB (t->base.u.bits.unsigned_flag);
5884 : 9083005 : RB (t->base.u.bits.packed_flag);
5885 : 9083005 : RB (t->base.u.bits.user_align);
5886 : 9083005 : RB (t->base.u.bits.nameless_flag);
5887 : 9083005 : RB (t->base.u.bits.atomic_flag);
5888 : 9083005 : RB (t->base.u.bits.unavailable_flag);
5889 : 9083005 : break;
5890 : : }
5891 : :
5892 : 9083005 : if (TREE_CODE_CLASS (code) == tcc_type)
5893 : : {
5894 : 413941 : RB (t->type_common.no_force_blk_flag);
5895 : 413941 : RB (t->type_common.needs_constructing_flag);
5896 : 413941 : RB (t->type_common.transparent_aggr_flag);
5897 : 413941 : RB (t->type_common.restrict_flag);
5898 : 413941 : RB (t->type_common.string_flag);
5899 : 413941 : RB (t->type_common.lang_flag_0);
5900 : 413941 : RB (t->type_common.lang_flag_1);
5901 : 413941 : RB (t->type_common.lang_flag_2);
5902 : 413941 : RB (t->type_common.lang_flag_3);
5903 : 413941 : RB (t->type_common.lang_flag_4);
5904 : 413941 : RB (t->type_common.lang_flag_5);
5905 : 413941 : RB (t->type_common.lang_flag_6);
5906 : 413941 : RB (t->type_common.typeless_storage);
5907 : : }
5908 : :
5909 : 9083005 : if (TREE_CODE_CLASS (code) != tcc_declaration)
5910 : 6772176 : goto done;
5911 : :
5912 : 2310829 : if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
5913 : : {
5914 : 2310829 : RB (t->decl_common.nonlocal_flag);
5915 : 2310829 : RB (t->decl_common.virtual_flag);
5916 : 2310829 : RB (t->decl_common.ignored_flag);
5917 : 2310829 : RB (t->decl_common.abstract_flag);
5918 : 2310829 : RB (t->decl_common.artificial_flag);
5919 : 2310829 : RB (t->decl_common.preserve_flag);
5920 : 2310829 : RB (t->decl_common.debug_expr_is_from);
5921 : 2310829 : RB (t->decl_common.lang_flag_0);
5922 : 2310829 : RB (t->decl_common.lang_flag_1);
5923 : 2310829 : RB (t->decl_common.lang_flag_2);
5924 : 2310829 : RB (t->decl_common.lang_flag_3);
5925 : 2310829 : RB (t->decl_common.lang_flag_4);
5926 : 2310829 : RB (t->decl_common.lang_flag_5);
5927 : 2310829 : RB (t->decl_common.lang_flag_6);
5928 : 2310829 : RB (t->decl_common.lang_flag_7);
5929 : 2310829 : RB (t->decl_common.lang_flag_8);
5930 : 2310829 : RB (t->decl_common.decl_flag_0);
5931 : 2310829 : RB (t->decl_common.decl_flag_1);
5932 : 2310829 : RB (t->decl_common.decl_flag_2);
5933 : 2310829 : RB (t->decl_common.decl_flag_3);
5934 : 2310829 : RB (t->decl_common.not_gimple_reg_flag);
5935 : 2310829 : RB (t->decl_common.decl_by_reference_flag);
5936 : 2310829 : RB (t->decl_common.decl_read_flag);
5937 : 2310829 : RB (t->decl_common.decl_nonshareable_flag);
5938 : 2310829 : RB (t->decl_common.decl_not_flexarray);
5939 : : }
5940 : : else
5941 : 0 : goto done;
5942 : :
5943 : 2310829 : if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
5944 : : {
5945 : 1076142 : RB (t->decl_with_vis.defer_output);
5946 : 1076142 : RB (t->decl_with_vis.hard_register);
5947 : 1076142 : RB (t->decl_with_vis.common_flag);
5948 : 1076142 : RB (t->decl_with_vis.in_text_section);
5949 : 1076142 : RB (t->decl_with_vis.in_constant_pool);
5950 : 1076142 : RB (t->decl_with_vis.dllimport_flag);
5951 : 1076142 : RB (t->decl_with_vis.weak_flag);
5952 : 1076142 : RB (t->decl_with_vis.seen_in_bind_expr);
5953 : 1076142 : RB (t->decl_with_vis.comdat_flag);
5954 : 1076142 : RB (t->decl_with_vis.visibility_specified);
5955 : 1076142 : RB (t->decl_with_vis.init_priority_p);
5956 : 1076142 : RB (t->decl_with_vis.shadowed_for_var_p);
5957 : 1076142 : RB (t->decl_with_vis.cxx_constructor);
5958 : 1076142 : RB (t->decl_with_vis.cxx_destructor);
5959 : 1076142 : RB (t->decl_with_vis.final);
5960 : 1076142 : RB (t->decl_with_vis.regdecl_flag);
5961 : : }
5962 : : else
5963 : 1234687 : goto done;
5964 : :
5965 : 1076142 : if (CODE_CONTAINS_STRUCT (code, TS_FUNCTION_DECL))
5966 : : {
5967 : 324797 : RB (t->function_decl.static_ctor_flag);
5968 : 324797 : RB (t->function_decl.static_dtor_flag);
5969 : 324797 : RB (t->function_decl.uninlinable);
5970 : 324797 : RB (t->function_decl.possibly_inlined);
5971 : 324797 : RB (t->function_decl.novops_flag);
5972 : 324797 : RB (t->function_decl.returns_twice_flag);
5973 : 324797 : RB (t->function_decl.malloc_flag);
5974 : 324797 : RB (t->function_decl.declared_inline_flag);
5975 : 324797 : RB (t->function_decl.no_inline_warning_flag);
5976 : 324797 : RB (t->function_decl.no_instrument_function_entry_exit);
5977 : 324797 : RB (t->function_decl.no_limit_stack);
5978 : 324797 : RB (t->function_decl.disregard_inline_limits);
5979 : 324797 : RB (t->function_decl.pure_flag);
5980 : 324797 : RB (t->function_decl.looping_const_or_pure_flag);
5981 : :
5982 : 324797 : RB (t->function_decl.has_debug_args_flag);
5983 : 324797 : RB (t->function_decl.versioned_function);
5984 : 324797 : RB (t->function_decl.replaceable_operator);
5985 : :
5986 : : /* decl_type is a (misnamed) 2 bit discriminator. */
5987 : 324797 : unsigned kind = 0;
5988 : 324797 : kind |= unsigned (bits.b ()) << 0;
5989 : 324797 : kind |= unsigned (bits.b ()) << 1;
5990 : 324797 : t->function_decl.decl_type = function_decl_type (kind);
5991 : : }
5992 : : #undef RB_IF
5993 : : #undef RB
5994 : 751345 : done:
5995 : 10523905 : return !get_overrun ();
5996 : : }
5997 : :
5998 : : void
5999 : 1773564 : trees_out::lang_decl_bools (tree t, bits_out& bits)
6000 : : {
6001 : : #define WB(X) (bits.b (X))
6002 : 1773564 : const struct lang_decl *lang = DECL_LANG_SPECIFIC (t);
6003 : :
6004 : 1773564 : bits.bflush ();
6005 : 1773564 : WB (lang->u.base.language == lang_cplusplus);
6006 : 1773564 : WB ((lang->u.base.use_template >> 0) & 1);
6007 : 1773564 : WB ((lang->u.base.use_template >> 1) & 1);
6008 : : /* Do not write lang->u.base.not_really_extern, importer will set
6009 : : when reading the definition (if any). */
6010 : 1773564 : WB (lang->u.base.initialized_in_class);
6011 : 1773564 : WB (lang->u.base.threadprivate_or_deleted_p);
6012 : : /* Do not write lang->u.base.anticipated_p, it is a property of the
6013 : : current TU. */
6014 : 1773564 : WB (lang->u.base.friend_or_tls);
6015 : 1773564 : WB (lang->u.base.unknown_bound_p);
6016 : : /* Do not write lang->u.base.odr_used, importer will recalculate if
6017 : : they do ODR use this decl. */
6018 : 1773564 : WB (lang->u.base.concept_p);
6019 : 1773564 : WB (lang->u.base.var_declared_inline_p);
6020 : 1773564 : WB (lang->u.base.dependent_init_p);
6021 : : /* When building a header unit, everthing is marked as purview, (so
6022 : : we know which decls to write). But when we import them we do not
6023 : : want to mark them as in module purview. */
6024 : 3470375 : WB (lang->u.base.module_purview_p && !header_module_p ());
6025 : 1773564 : WB (lang->u.base.module_attach_p);
6026 : 1773564 : WB (lang->u.base.module_keyed_decls_p);
6027 : 1773564 : switch (lang->u.base.selector)
6028 : : {
6029 : 0 : default:
6030 : 0 : gcc_unreachable ();
6031 : :
6032 : 378541 : case lds_fn: /* lang_decl_fn. */
6033 : 378541 : WB (lang->u.fn.global_ctor_p);
6034 : 378541 : WB (lang->u.fn.global_dtor_p);
6035 : 378541 : WB (lang->u.fn.static_function);
6036 : 378541 : WB (lang->u.fn.pure_virtual);
6037 : 378541 : WB (lang->u.fn.defaulted_p);
6038 : 378541 : WB (lang->u.fn.has_in_charge_parm_p);
6039 : 378541 : WB (lang->u.fn.has_vtt_parm_p);
6040 : : /* There shouldn't be a pending inline at this point. */
6041 : 378541 : gcc_assert (!lang->u.fn.pending_inline_p);
6042 : 378541 : WB (lang->u.fn.nonconverting);
6043 : 378541 : WB (lang->u.fn.thunk_p);
6044 : 378541 : WB (lang->u.fn.this_thunk_p);
6045 : : /* Do not stream lang->u.hidden_friend_p, it is a property of
6046 : : the TU. */
6047 : 378541 : WB (lang->u.fn.omp_declare_reduction_p);
6048 : 378541 : WB (lang->u.fn.has_dependent_explicit_spec_p);
6049 : 378541 : WB (lang->u.fn.immediate_fn_p);
6050 : 378541 : WB (lang->u.fn.maybe_deleted);
6051 : 378541 : WB (lang->u.fn.implicit_constexpr);
6052 : 378541 : WB (lang->u.fn.escalated_p);
6053 : 378541 : WB (lang->u.fn.xobj_func);
6054 : 378541 : goto lds_min;
6055 : :
6056 : 1378 : case lds_decomp: /* lang_decl_decomp. */
6057 : : /* No bools. */
6058 : 1378 : goto lds_min;
6059 : :
6060 : : case lds_min: /* lang_decl_min. */
6061 : 1773564 : lds_min:
6062 : : /* No bools. */
6063 : : break;
6064 : :
6065 : : case lds_ns: /* lang_decl_ns. */
6066 : : /* No bools. */
6067 : : break;
6068 : :
6069 : : case lds_parm: /* lang_decl_parm. */
6070 : : /* No bools. */
6071 : : break;
6072 : : }
6073 : : #undef WB
6074 : 1773564 : }
6075 : :
6076 : : bool
6077 : 1474085 : trees_in::lang_decl_bools (tree t, bits_in& bits)
6078 : : {
6079 : : #define RB(X) ((X) = bits.b ())
6080 : 1474085 : struct lang_decl *lang = DECL_LANG_SPECIFIC (t);
6081 : :
6082 : 1474085 : bits.bflush ();
6083 : 1474085 : lang->u.base.language = bits.b () ? lang_cplusplus : lang_c;
6084 : 1474085 : unsigned v;
6085 : 1474085 : v = bits.b () << 0;
6086 : 1474085 : v |= bits.b () << 1;
6087 : 1474085 : lang->u.base.use_template = v;
6088 : : /* lang->u.base.not_really_extern is not streamed. */
6089 : 1474085 : RB (lang->u.base.initialized_in_class);
6090 : 1474085 : RB (lang->u.base.threadprivate_or_deleted_p);
6091 : : /* lang->u.base.anticipated_p is not streamed. */
6092 : 1474085 : RB (lang->u.base.friend_or_tls);
6093 : 1474085 : RB (lang->u.base.unknown_bound_p);
6094 : : /* lang->u.base.odr_used is not streamed. */
6095 : 1474085 : RB (lang->u.base.concept_p);
6096 : 1474085 : RB (lang->u.base.var_declared_inline_p);
6097 : 1474085 : RB (lang->u.base.dependent_init_p);
6098 : 1474085 : RB (lang->u.base.module_purview_p);
6099 : 1474085 : RB (lang->u.base.module_attach_p);
6100 : 1474085 : RB (lang->u.base.module_keyed_decls_p);
6101 : 1474085 : switch (lang->u.base.selector)
6102 : : {
6103 : 0 : default:
6104 : 0 : gcc_unreachable ();
6105 : :
6106 : 324797 : case lds_fn: /* lang_decl_fn. */
6107 : 324797 : RB (lang->u.fn.global_ctor_p);
6108 : 324797 : RB (lang->u.fn.global_dtor_p);
6109 : 324797 : RB (lang->u.fn.static_function);
6110 : 324797 : RB (lang->u.fn.pure_virtual);
6111 : 324797 : RB (lang->u.fn.defaulted_p);
6112 : 324797 : RB (lang->u.fn.has_in_charge_parm_p);
6113 : 324797 : RB (lang->u.fn.has_vtt_parm_p);
6114 : 324797 : RB (lang->u.fn.nonconverting);
6115 : 324797 : RB (lang->u.fn.thunk_p);
6116 : 324797 : RB (lang->u.fn.this_thunk_p);
6117 : : /* lang->u.fn.hidden_friend_p is not streamed. */
6118 : 324797 : RB (lang->u.fn.omp_declare_reduction_p);
6119 : 324797 : RB (lang->u.fn.has_dependent_explicit_spec_p);
6120 : 324797 : RB (lang->u.fn.immediate_fn_p);
6121 : 324797 : RB (lang->u.fn.maybe_deleted);
6122 : 324797 : RB (lang->u.fn.implicit_constexpr);
6123 : 324797 : RB (lang->u.fn.escalated_p);
6124 : 324797 : RB (lang->u.fn.xobj_func);
6125 : 324797 : goto lds_min;
6126 : :
6127 : 1303 : case lds_decomp: /* lang_decl_decomp. */
6128 : : /* No bools. */
6129 : 1303 : goto lds_min;
6130 : :
6131 : : case lds_min: /* lang_decl_min. */
6132 : 1474085 : lds_min:
6133 : : /* No bools. */
6134 : : break;
6135 : :
6136 : : case lds_ns: /* lang_decl_ns. */
6137 : : /* No bools. */
6138 : : break;
6139 : :
6140 : : case lds_parm: /* lang_decl_parm. */
6141 : : /* No bools. */
6142 : : break;
6143 : : }
6144 : : #undef RB
6145 : 1474085 : return !get_overrun ();
6146 : : }
6147 : :
6148 : : void
6149 : 143880 : trees_out::lang_type_bools (tree t, bits_out& bits)
6150 : : {
6151 : : #define WB(X) (bits.b (X))
6152 : 143880 : const struct lang_type *lang = TYPE_LANG_SPECIFIC (t);
6153 : :
6154 : 143880 : bits.bflush ();
6155 : 143880 : WB (lang->has_type_conversion);
6156 : 143880 : WB (lang->has_copy_ctor);
6157 : 143880 : WB (lang->has_default_ctor);
6158 : 143880 : WB (lang->const_needs_init);
6159 : 143880 : WB (lang->ref_needs_init);
6160 : 143880 : WB (lang->has_const_copy_assign);
6161 : 143880 : WB ((lang->use_template >> 0) & 1);
6162 : 143880 : WB ((lang->use_template >> 1) & 1);
6163 : :
6164 : 143880 : WB (lang->has_mutable);
6165 : 143880 : WB (lang->com_interface);
6166 : 143880 : WB (lang->non_pod_class);
6167 : 143880 : WB (lang->nearly_empty_p);
6168 : 143880 : WB (lang->user_align);
6169 : 143880 : WB (lang->has_copy_assign);
6170 : 143880 : WB (lang->has_new);
6171 : 143880 : WB (lang->has_array_new);
6172 : :
6173 : 143880 : WB ((lang->gets_delete >> 0) & 1);
6174 : 143880 : WB ((lang->gets_delete >> 1) & 1);
6175 : 143880 : WB (lang->interface_only);
6176 : 143880 : WB (lang->interface_unknown);
6177 : 143880 : WB (lang->contains_empty_class_p);
6178 : 143880 : WB (lang->anon_aggr);
6179 : 143880 : WB (lang->non_zero_init);
6180 : 143880 : WB (lang->empty_p);
6181 : :
6182 : 143880 : WB (lang->vec_new_uses_cookie);
6183 : 143880 : WB (lang->declared_class);
6184 : 143880 : WB (lang->diamond_shaped);
6185 : 143880 : WB (lang->repeated_base);
6186 : 143880 : gcc_checking_assert (!lang->being_defined);
6187 : : // lang->debug_requested
6188 : 143880 : WB (lang->fields_readonly);
6189 : 143880 : WB (lang->ptrmemfunc_flag);
6190 : :
6191 : 143880 : WB (lang->lazy_default_ctor);
6192 : 143880 : WB (lang->lazy_copy_ctor);
6193 : 143880 : WB (lang->lazy_copy_assign);
6194 : 143880 : WB (lang->lazy_destructor);
6195 : 143880 : WB (lang->has_const_copy_ctor);
6196 : 143880 : WB (lang->has_complex_copy_ctor);
6197 : 143880 : WB (lang->has_complex_copy_assign);
6198 : 143880 : WB (lang->non_aggregate);
6199 : :
6200 : 143880 : WB (lang->has_complex_dflt);
6201 : 143880 : WB (lang->has_list_ctor);
6202 : 143880 : WB (lang->non_std_layout);
6203 : 143880 : WB (lang->is_literal);
6204 : 143880 : WB (lang->lazy_move_ctor);
6205 : 143880 : WB (lang->lazy_move_assign);
6206 : 143880 : WB (lang->has_complex_move_ctor);
6207 : 143880 : WB (lang->has_complex_move_assign);
6208 : :
6209 : 143880 : WB (lang->has_constexpr_ctor);
6210 : 143880 : WB (lang->unique_obj_representations);
6211 : 143880 : WB (lang->unique_obj_representations_set);
6212 : 143880 : gcc_checking_assert (!lang->erroneous);
6213 : 143880 : WB (lang->non_pod_aggregate);
6214 : 143880 : WB (lang->non_aggregate_pod);
6215 : 143880 : WB (lang->trivially_relocatable);
6216 : 143880 : WB (lang->trivially_relocatable_computed);
6217 : :
6218 : 143880 : WB (lang->replaceable);
6219 : 143880 : WB (lang->replaceable_computed);
6220 : : #undef WB
6221 : 143880 : }
6222 : :
6223 : : bool
6224 : 112201 : trees_in::lang_type_bools (tree t, bits_in& bits)
6225 : : {
6226 : : #define RB(X) ((X) = bits.b ())
6227 : 112201 : struct lang_type *lang = TYPE_LANG_SPECIFIC (t);
6228 : :
6229 : 112201 : bits.bflush ();
6230 : 112201 : RB (lang->has_type_conversion);
6231 : 112201 : RB (lang->has_copy_ctor);
6232 : 112201 : RB (lang->has_default_ctor);
6233 : 112201 : RB (lang->const_needs_init);
6234 : 112201 : RB (lang->ref_needs_init);
6235 : 112201 : RB (lang->has_const_copy_assign);
6236 : 112201 : unsigned v;
6237 : 112201 : v = bits.b () << 0;
6238 : 112201 : v |= bits.b () << 1;
6239 : 112201 : lang->use_template = v;
6240 : :
6241 : 112201 : RB (lang->has_mutable);
6242 : 112201 : RB (lang->com_interface);
6243 : 112201 : RB (lang->non_pod_class);
6244 : 112201 : RB (lang->nearly_empty_p);
6245 : 112201 : RB (lang->user_align);
6246 : 112201 : RB (lang->has_copy_assign);
6247 : 112201 : RB (lang->has_new);
6248 : 112201 : RB (lang->has_array_new);
6249 : :
6250 : 112201 : v = bits.b () << 0;
6251 : 112201 : v |= bits.b () << 1;
6252 : 112201 : lang->gets_delete = v;
6253 : 112201 : RB (lang->interface_only);
6254 : 112201 : RB (lang->interface_unknown);
6255 : 112201 : RB (lang->contains_empty_class_p);
6256 : 112201 : RB (lang->anon_aggr);
6257 : 112201 : RB (lang->non_zero_init);
6258 : 112201 : RB (lang->empty_p);
6259 : :
6260 : 112201 : RB (lang->vec_new_uses_cookie);
6261 : 112201 : RB (lang->declared_class);
6262 : 112201 : RB (lang->diamond_shaped);
6263 : 112201 : RB (lang->repeated_base);
6264 : 112201 : gcc_checking_assert (!lang->being_defined);
6265 : 112201 : gcc_checking_assert (!lang->debug_requested);
6266 : 112201 : RB (lang->fields_readonly);
6267 : 112201 : RB (lang->ptrmemfunc_flag);
6268 : :
6269 : 112201 : RB (lang->lazy_default_ctor);
6270 : 112201 : RB (lang->lazy_copy_ctor);
6271 : 112201 : RB (lang->lazy_copy_assign);
6272 : 112201 : RB (lang->lazy_destructor);
6273 : 112201 : RB (lang->has_const_copy_ctor);
6274 : 112201 : RB (lang->has_complex_copy_ctor);
6275 : 112201 : RB (lang->has_complex_copy_assign);
6276 : 112201 : RB (lang->non_aggregate);
6277 : :
6278 : 112201 : RB (lang->has_complex_dflt);
6279 : 112201 : RB (lang->has_list_ctor);
6280 : 112201 : RB (lang->non_std_layout);
6281 : 112201 : RB (lang->is_literal);
6282 : 112201 : RB (lang->lazy_move_ctor);
6283 : 112201 : RB (lang->lazy_move_assign);
6284 : 112201 : RB (lang->has_complex_move_ctor);
6285 : 112201 : RB (lang->has_complex_move_assign);
6286 : :
6287 : 112201 : RB (lang->has_constexpr_ctor);
6288 : 112201 : RB (lang->unique_obj_representations);
6289 : 112201 : RB (lang->unique_obj_representations_set);
6290 : 112201 : gcc_checking_assert (!lang->erroneous);
6291 : 112201 : RB (lang->non_pod_aggregate);
6292 : 112201 : RB (lang->non_aggregate_pod);
6293 : 112201 : RB (lang->trivially_relocatable);
6294 : 112201 : RB (lang->trivially_relocatable_computed);
6295 : :
6296 : 112201 : RB (lang->replaceable);
6297 : 112201 : RB (lang->replaceable_computed);
6298 : : #undef RB
6299 : 112201 : return !get_overrun ();
6300 : : }
6301 : :
6302 : : /* Read & write the core values and pointers. */
6303 : :
6304 : : void
6305 : 31873542 : trees_out::core_vals (tree t)
6306 : : {
6307 : : #define WU(X) (u (X))
6308 : : #define WT(X) (tree_node (X))
6309 : 31873542 : tree_code code = TREE_CODE (t);
6310 : :
6311 : : /* First by shape of the tree. */
6312 : :
6313 : 31873542 : if (CODE_CONTAINS_STRUCT (code, TS_DECL_MINIMAL))
6314 : : {
6315 : : /* Write this early, for better log information. */
6316 : 7209143 : WT (t->decl_minimal.name);
6317 : 7209143 : if (!DECL_TEMPLATE_PARM_P (t))
6318 : 5586963 : WT (t->decl_minimal.context);
6319 : :
6320 : 7209143 : if (state)
6321 : 5945829 : state->write_location (*this, t->decl_minimal.locus);
6322 : :
6323 : 7209143 : if (streaming_p ())
6324 : 2767729 : if (has_warning_spec (t))
6325 : 500 : u (get_warning_spec (t));
6326 : : }
6327 : :
6328 : 31873542 : if (CODE_CONTAINS_STRUCT (code, TS_TYPE_COMMON))
6329 : : {
6330 : : /* The only types we write also have TYPE_NON_COMMON. */
6331 : 1799509 : gcc_checking_assert (CODE_CONTAINS_STRUCT (code, TS_TYPE_NON_COMMON));
6332 : :
6333 : : /* We only stream the main variant. */
6334 : 1799509 : gcc_checking_assert (TYPE_MAIN_VARIANT (t) == t);
6335 : :
6336 : : /* Stream the name & context first, for better log information */
6337 : 1799509 : WT (t->type_common.name);
6338 : 1799509 : WT (t->type_common.context);
6339 : :
6340 : : /* By construction we want to make sure we have the canonical
6341 : : and main variants already in the type table, so emit them
6342 : : now. */
6343 : 1799509 : WT (t->type_common.main_variant);
6344 : :
6345 : 1799509 : tree canonical = t->type_common.canonical;
6346 : 1799509 : if (canonical && DECL_TEMPLATE_PARM_P (TYPE_NAME (t)))
6347 : : /* We do not want to wander into different templates.
6348 : : Reconstructed on stream in. */
6349 : : canonical = t;
6350 : 1799509 : WT (canonical);
6351 : :
6352 : : /* type_common.next_variant is internally manipulated. */
6353 : : /* type_common.pointer_to, type_common.reference_to. */
6354 : :
6355 : 1799509 : if (streaming_p ())
6356 : : {
6357 : 500347 : WU (t->type_common.precision);
6358 : 500347 : WU (t->type_common.contains_placeholder_bits);
6359 : 500347 : WU (t->type_common.mode);
6360 : 500347 : WU (t->type_common.align);
6361 : : }
6362 : :
6363 : 1799509 : if (!RECORD_OR_UNION_CODE_P (code))
6364 : : {
6365 : 1468709 : WT (t->type_common.size);
6366 : 1468709 : WT (t->type_common.size_unit);
6367 : : }
6368 : 1799509 : WT (t->type_common.attributes);
6369 : :
6370 : 1799509 : WT (t->type_common.common.chain); /* TYPE_STUB_DECL. */
6371 : : }
6372 : :
6373 : 31873542 : if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
6374 : : {
6375 : 7209143 : if (streaming_p ())
6376 : : {
6377 : 2767729 : WU (t->decl_common.mode);
6378 : 2767729 : WU (t->decl_common.off_align);
6379 : 2767729 : WU (t->decl_common.align);
6380 : : }
6381 : :
6382 : : /* For templates these hold instantiation (partial and/or
6383 : : specialization) information. */
6384 : 7209143 : if (code != TEMPLATE_DECL)
6385 : : {
6386 : 6648452 : WT (t->decl_common.size);
6387 : 6648452 : WT (t->decl_common.size_unit);
6388 : : }
6389 : :
6390 : 7209143 : WT (t->decl_common.attributes);
6391 : : // FIXME: Does this introduce cross-decl links? For instance
6392 : : // from instantiation to the template. If so, we'll need more
6393 : : // deduplication logic. I think we'll need to walk the blocks
6394 : : // of the owning function_decl's abstract origin in tandem, to
6395 : : // generate the locating data needed?
6396 : 7209143 : WT (t->decl_common.abstract_origin);
6397 : : }
6398 : :
6399 : 31873542 : if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
6400 : : {
6401 : 3442681 : WT (t->decl_with_vis.assembler_name);
6402 : 3442681 : if (streaming_p ())
6403 : 1317220 : WU (t->decl_with_vis.visibility);
6404 : : }
6405 : :
6406 : 31873542 : if (CODE_CONTAINS_STRUCT (code, TS_TYPE_NON_COMMON))
6407 : : {
6408 : 1799509 : if (code == ENUMERAL_TYPE)
6409 : : {
6410 : : /* These fields get set even for opaque enums that lack a
6411 : : definition, so we stream them directly for each ENUMERAL_TYPE.
6412 : : We stream TYPE_VALUES as part of the definition. */
6413 : 7536 : WT (t->type_non_common.maxval);
6414 : 7536 : WT (t->type_non_common.minval);
6415 : : }
6416 : : /* Records and unions hold FIELDS, VFIELD & BINFO on these
6417 : : things. */
6418 : 1791973 : else if (!RECORD_OR_UNION_CODE_P (code))
6419 : : {
6420 : : // FIXME: These are from tpl_parm_value's 'type' writing.
6421 : : // Perhaps it should just be doing them directly?
6422 : 1461173 : gcc_checking_assert (code == TEMPLATE_TYPE_PARM
6423 : : || code == TEMPLATE_TEMPLATE_PARM
6424 : : || code == BOUND_TEMPLATE_TEMPLATE_PARM);
6425 : 1461173 : gcc_checking_assert (!TYPE_CACHED_VALUES_P (t));
6426 : 1461173 : WT (t->type_non_common.values);
6427 : 1461173 : WT (t->type_non_common.maxval);
6428 : 1461173 : WT (t->type_non_common.minval);
6429 : : }
6430 : :
6431 : 1799509 : WT (t->type_non_common.lang_1);
6432 : : }
6433 : :
6434 : 31873542 : if (CODE_CONTAINS_STRUCT (code, TS_EXP))
6435 : : {
6436 : 9386828 : if (state)
6437 : 9191710 : state->write_location (*this, t->exp.locus);
6438 : :
6439 : 9386828 : if (streaming_p ())
6440 : 4550677 : if (has_warning_spec (t))
6441 : 362667 : u (get_warning_spec (t));
6442 : :
6443 : : /* Walk in forward order, as (for instance) REQUIRES_EXPR has a
6444 : : bunch of unscoped parms on its first operand. It's safer to
6445 : : create those in order. */
6446 : 9386828 : bool vl = TREE_CODE_CLASS (code) == tcc_vl_exp;
6447 : 26073446 : for (unsigned limit = (vl ? VL_EXP_OPERAND_LENGTH (t)
6448 : 9386828 : : TREE_OPERAND_LENGTH (t)),
6449 : 26073446 : ix = unsigned (vl); ix != limit; ix++)
6450 : 16686618 : WT (TREE_OPERAND (t, ix));
6451 : : }
6452 : : else
6453 : : /* The CODE_CONTAINS tables were inaccurate when I started. */
6454 : 22486714 : gcc_checking_assert (TREE_CODE_CLASS (code) != tcc_expression
6455 : : && TREE_CODE_CLASS (code) != tcc_binary
6456 : : && TREE_CODE_CLASS (code) != tcc_unary
6457 : : && TREE_CODE_CLASS (code) != tcc_reference
6458 : : && TREE_CODE_CLASS (code) != tcc_comparison
6459 : : && TREE_CODE_CLASS (code) != tcc_statement
6460 : : && TREE_CODE_CLASS (code) != tcc_vl_exp);
6461 : :
6462 : : /* Then by CODE. Special cases and/or 1:1 tree shape
6463 : : correspondance. */
6464 : 31873542 : switch (code)
6465 : : {
6466 : : default:
6467 : : break;
6468 : :
6469 : 0 : case ARGUMENT_PACK_SELECT: /* Transient during instantiation. */
6470 : 0 : case DEFERRED_PARSE: /* Expanded upon completion of
6471 : : outermost class. */
6472 : 0 : case IDENTIFIER_NODE: /* Streamed specially. */
6473 : 0 : case BINDING_VECTOR: /* Only in namespace-scope symbol
6474 : : table. */
6475 : 0 : case SSA_NAME:
6476 : 0 : case TRANSLATION_UNIT_DECL: /* There is only one, it is a
6477 : : global_tree. */
6478 : 0 : case USERDEF_LITERAL: /* Expanded during parsing. */
6479 : 0 : gcc_unreachable (); /* Should never meet. */
6480 : :
6481 : : /* Constants. */
6482 : 18 : case COMPLEX_CST:
6483 : 18 : WT (TREE_REALPART (t));
6484 : 18 : WT (TREE_IMAGPART (t));
6485 : 18 : break;
6486 : :
6487 : 0 : case FIXED_CST:
6488 : 0 : gcc_unreachable (); /* Not supported in C++. */
6489 : :
6490 : 3047767 : case INTEGER_CST:
6491 : 3047767 : if (streaming_p ())
6492 : : {
6493 : 563291 : unsigned num = TREE_INT_CST_EXT_NUNITS (t);
6494 : 1128256 : for (unsigned ix = 0; ix != num; ix++)
6495 : 564965 : wu (TREE_INT_CST_ELT (t, ix));
6496 : : }
6497 : : break;
6498 : :
6499 : 0 : case POLY_INT_CST:
6500 : 0 : if (streaming_p ())
6501 : 0 : for (unsigned ix = 0; ix != NUM_POLY_INT_COEFFS; ix++)
6502 : 0 : WT (POLY_INT_CST_COEFF (t, ix));
6503 : : break;
6504 : :
6505 : 28896 : case REAL_CST:
6506 : 28896 : if (streaming_p ())
6507 : 14424 : buf (TREE_REAL_CST_PTR (t), sizeof (real_value));
6508 : : break;
6509 : :
6510 : : case STRING_CST:
6511 : : /* Streamed during start. */
6512 : : break;
6513 : :
6514 : 36 : case RAW_DATA_CST:
6515 : 36 : if (RAW_DATA_OWNER (t) == NULL_TREE)
6516 : : break; /* Streamed as STRING_CST during start. */
6517 : 24 : WT (RAW_DATA_OWNER (t));
6518 : 24 : if (streaming_p ())
6519 : : {
6520 : 12 : if (TREE_CODE (RAW_DATA_OWNER (t)) == RAW_DATA_CST)
6521 : 6 : z (RAW_DATA_POINTER (t) - RAW_DATA_POINTER (RAW_DATA_OWNER (t)));
6522 : 6 : else if (TREE_CODE (RAW_DATA_OWNER (t)) == STRING_CST)
6523 : 6 : z (RAW_DATA_POINTER (t)
6524 : 6 : - TREE_STRING_POINTER (RAW_DATA_OWNER (t)));
6525 : : else
6526 : 0 : gcc_unreachable ();
6527 : : }
6528 : : break;
6529 : :
6530 : 36 : case VECTOR_CST:
6531 : 102 : for (unsigned ix = vector_cst_encoded_nelts (t); ix--;)
6532 : 66 : WT (VECTOR_CST_ENCODED_ELT (t, ix));
6533 : : break;
6534 : :
6535 : : /* Decls. */
6536 : 384469 : case VAR_DECL:
6537 : 384469 : if (DECL_CONTEXT (t)
6538 : 384469 : && TREE_CODE (DECL_CONTEXT (t)) != FUNCTION_DECL)
6539 : : {
6540 : 103585 : if (DECL_HAS_VALUE_EXPR_P (t))
6541 : 18 : WT (DECL_VALUE_EXPR (t));
6542 : : break;
6543 : : }
6544 : : /* FALLTHROUGH */
6545 : :
6546 : 3208114 : case RESULT_DECL:
6547 : 3208114 : case PARM_DECL:
6548 : 3208114 : if (DECL_HAS_VALUE_EXPR_P (t))
6549 : 25759 : WT (DECL_VALUE_EXPR (t));
6550 : : /* FALLTHROUGH */
6551 : :
6552 : 3347160 : case CONST_DECL:
6553 : 3347160 : case IMPORTED_DECL:
6554 : 3347160 : WT (t->decl_common.initial);
6555 : 3347160 : break;
6556 : :
6557 : 98301 : case FIELD_DECL:
6558 : 98301 : WT (t->field_decl.offset);
6559 : 98301 : WT (t->field_decl.bit_field_type);
6560 : 98301 : {
6561 : 98301 : auto ovr = make_temp_override (walking_bit_field_unit, true);
6562 : 98301 : WT (t->field_decl.qualifier); /* bitfield unit. */
6563 : 98301 : }
6564 : 98301 : WT (t->field_decl.bit_offset);
6565 : 98301 : WT (t->field_decl.fcontext);
6566 : 98301 : WT (t->decl_common.initial);
6567 : 98301 : break;
6568 : :
6569 : 29576 : case LABEL_DECL:
6570 : 29576 : if (streaming_p ())
6571 : : {
6572 : 14788 : WU (t->label_decl.label_decl_uid);
6573 : 14788 : WU (t->label_decl.eh_landing_pad_nr);
6574 : : }
6575 : : break;
6576 : :
6577 : 757251 : case FUNCTION_DECL:
6578 : 757251 : if (streaming_p ())
6579 : : {
6580 : : /* Builtins can be streamed by value when a header declares
6581 : : them. */
6582 : 378541 : WU (DECL_BUILT_IN_CLASS (t));
6583 : 378541 : if (DECL_BUILT_IN_CLASS (t) != NOT_BUILT_IN)
6584 : 7637 : WU (DECL_UNCHECKED_FUNCTION_CODE (t));
6585 : : }
6586 : :
6587 : 757251 : WT (t->function_decl.personality);
6588 : : /* Rather than streaming target/optimize nodes, we should reconstruct
6589 : : them on stream-in from any attributes applied to the function. */
6590 : 757251 : if (streaming_p () && t->function_decl.function_specific_target)
6591 : 0 : warning_at (DECL_SOURCE_LOCATION (t), 0,
6592 : : "%<target%> attribute currently unsupported in modules");
6593 : 757251 : if (streaming_p () && t->function_decl.function_specific_optimization)
6594 : 3 : warning_at (DECL_SOURCE_LOCATION (t), 0,
6595 : : "%<optimize%> attribute currently unsupported in modules");
6596 : 757251 : WT (t->function_decl.vindex);
6597 : :
6598 : 757251 : if (DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (t))
6599 : 4462 : WT (lookup_explicit_specifier (t));
6600 : : break;
6601 : :
6602 : 93153 : case USING_DECL:
6603 : : /* USING_DECL_DECLS */
6604 : 93153 : WT (t->decl_common.initial);
6605 : : /* FALLTHROUGH */
6606 : :
6607 : 2300728 : case TYPE_DECL:
6608 : : /* USING_DECL: USING_DECL_SCOPE */
6609 : : /* TYPE_DECL: DECL_ORIGINAL_TYPE */
6610 : 2300728 : WT (t->decl_non_common.result);
6611 : 2300728 : break;
6612 : :
6613 : : /* Miscellaneous common nodes. */
6614 : 484622 : case BLOCK:
6615 : 484622 : if (state)
6616 : : {
6617 : 484622 : state->write_location (*this, t->block.locus);
6618 : 484622 : state->write_location (*this, t->block.end_locus);
6619 : : }
6620 : :
6621 : : /* DECL_LOCAL_DECL_P decls are first encountered here and
6622 : : streamed by value. */
6623 : 721259 : for (tree decls = t->block.vars; decls; decls = DECL_CHAIN (decls))
6624 : : {
6625 : 236637 : if (VAR_OR_FUNCTION_DECL_P (decls)
6626 : 236637 : && DECL_LOCAL_DECL_P (decls))
6627 : : {
6628 : : /* Make sure this is the first encounter, and mark for
6629 : : walk-by-value. */
6630 : 236 : gcc_checking_assert (!TREE_VISITED (decls)
6631 : : && !DECL_TEMPLATE_INFO (decls));
6632 : 236 : mark_by_value (decls);
6633 : : }
6634 : 236637 : tree_node (decls);
6635 : : }
6636 : 484622 : tree_node (NULL_TREE);
6637 : :
6638 : : /* nonlocalized_vars is a middle-end thing. */
6639 : 484622 : WT (t->block.subblocks);
6640 : 484622 : WT (t->block.supercontext);
6641 : : // FIXME: As for decl's abstract_origin, does this introduce crosslinks?
6642 : 484622 : WT (t->block.abstract_origin);
6643 : : /* fragment_origin, fragment_chain are middle-end things. */
6644 : 484622 : WT (t->block.chain);
6645 : : /* nonlocalized_vars, block_num & die are middle endy/debug
6646 : : things. */
6647 : 484622 : break;
6648 : :
6649 : 942175 : case CALL_EXPR:
6650 : 942175 : if (streaming_p ())
6651 : 449003 : WU (t->base.u.ifn);
6652 : : break;
6653 : :
6654 : : case CONSTRUCTOR:
6655 : : // This must be streamed /after/ we've streamed the type,
6656 : : // because it can directly refer to elements of the type. Eg,
6657 : : // FIELD_DECLs of a RECORD_TYPE.
6658 : : break;
6659 : :
6660 : 24 : case OMP_CLAUSE:
6661 : 24 : {
6662 : : /* The ompcode is serialized in start. */
6663 : 24 : if (streaming_p ())
6664 : 12 : WU (t->omp_clause.subcode.map_kind);
6665 : 24 : if (state)
6666 : 24 : state->write_location (*this, t->omp_clause.locus);
6667 : :
6668 : 24 : unsigned len = omp_clause_num_ops[OMP_CLAUSE_CODE (t)];
6669 : 78 : for (unsigned ix = 0; ix != len; ix++)
6670 : 54 : WT (t->omp_clause.ops[ix]);
6671 : : }
6672 : : break;
6673 : :
6674 : 338293 : case STATEMENT_LIST:
6675 : 1343089 : for (tree stmt : tsi_range (t))
6676 : 1004796 : if (stmt)
6677 : 1004796 : WT (stmt);
6678 : 338293 : WT (NULL_TREE);
6679 : 338293 : break;
6680 : :
6681 : 0 : case OPTIMIZATION_NODE:
6682 : 0 : case TARGET_OPTION_NODE:
6683 : : // FIXME: Our representation for these two nodes is a cache of
6684 : : // the resulting set of options. Not a record of the options
6685 : : // that got changed by a particular attribute or pragma. Instead
6686 : : // of recording that, we probably should just rebuild the options
6687 : : // on stream-in from the function attributes. This could introduce
6688 : : // strangeness if the importer has some incompatible set of flags
6689 : : // but we currently assume users "know what they're doing" in such
6690 : : // a case anyway.
6691 : 0 : gcc_unreachable ();
6692 : 194524 : break;
6693 : :
6694 : 194524 : case TREE_BINFO:
6695 : 194524 : {
6696 : 194524 : WT (t->binfo.common.chain);
6697 : 194524 : WT (t->binfo.offset);
6698 : 194524 : WT (t->binfo.inheritance);
6699 : 194524 : WT (t->binfo.vptr_field);
6700 : :
6701 : 194524 : WT (t->binfo.vtable);
6702 : 194524 : WT (t->binfo.virtuals);
6703 : 194524 : WT (t->binfo.vtt_subvtt);
6704 : 194524 : WT (t->binfo.vtt_vptr);
6705 : :
6706 : 194524 : tree_vec (BINFO_BASE_ACCESSES (t));
6707 : 194524 : unsigned num = vec_safe_length (BINFO_BASE_ACCESSES (t));
6708 : 249614 : for (unsigned ix = 0; ix != num; ix++)
6709 : 55090 : WT (BINFO_BASE_BINFO (t, ix));
6710 : : }
6711 : : break;
6712 : :
6713 : 2491111 : case TREE_LIST:
6714 : 2491111 : WT (t->list.purpose);
6715 : 2491111 : WT (t->list.value);
6716 : 2491111 : WT (t->list.common.chain);
6717 : 2491111 : break;
6718 : :
6719 : 2117829 : case TREE_VEC:
6720 : 5802296 : for (unsigned ix = TREE_VEC_LENGTH (t); ix--;)
6721 : 3684467 : WT (TREE_VEC_ELT (t, ix));
6722 : : /* We stash NON_DEFAULT_TEMPLATE_ARGS_COUNT on TREE_CHAIN! */
6723 : 2117829 : gcc_checking_assert (!t->type_common.common.chain
6724 : : || (TREE_CODE (t->type_common.common.chain)
6725 : : == INTEGER_CST));
6726 : 2117829 : WT (t->type_common.common.chain);
6727 : 2117829 : break;
6728 : :
6729 : : /* C++-specific nodes ... */
6730 : 166736 : case BASELINK:
6731 : 166736 : WT (((lang_tree_node *)t)->baselink.binfo);
6732 : 166736 : WT (((lang_tree_node *)t)->baselink.functions);
6733 : 166736 : WT (((lang_tree_node *)t)->baselink.access_binfo);
6734 : 166736 : break;
6735 : :
6736 : 71784 : case CONSTRAINT_INFO:
6737 : 71784 : WT (((lang_tree_node *)t)->constraint_info.template_reqs);
6738 : 71784 : WT (((lang_tree_node *)t)->constraint_info.declarator_reqs);
6739 : 71784 : WT (((lang_tree_node *)t)->constraint_info.associated_constr);
6740 : 71784 : break;
6741 : :
6742 : 11456 : case DEFERRED_NOEXCEPT:
6743 : 11456 : WT (((lang_tree_node *)t)->deferred_noexcept.pattern);
6744 : 11456 : WT (((lang_tree_node *)t)->deferred_noexcept.args);
6745 : 11456 : break;
6746 : :
6747 : 9907 : case LAMBDA_EXPR:
6748 : 9907 : WT (((lang_tree_node *)t)->lambda_expression.capture_list);
6749 : 9907 : WT (((lang_tree_node *)t)->lambda_expression.this_capture);
6750 : 9907 : WT (((lang_tree_node *)t)->lambda_expression.extra_scope);
6751 : 9907 : WT (((lang_tree_node *)t)->lambda_expression.regen_info);
6752 : 9907 : WT (((lang_tree_node *)t)->lambda_expression.extra_args);
6753 : : /* pending_proxies is a parse-time thing. */
6754 : 9907 : gcc_assert (!((lang_tree_node *)t)->lambda_expression.pending_proxies);
6755 : 9907 : if (state)
6756 : 9652 : state->write_location
6757 : 9652 : (*this, ((lang_tree_node *)t)->lambda_expression.locus);
6758 : 9907 : if (streaming_p ())
6759 : : {
6760 : 3295 : WU (((lang_tree_node *)t)->lambda_expression.default_capture_mode);
6761 : 3295 : WU (((lang_tree_node *)t)->lambda_expression.discriminator_scope);
6762 : 3295 : WU (((lang_tree_node *)t)->lambda_expression.discriminator_sig);
6763 : : }
6764 : : break;
6765 : :
6766 : 1478469 : case OVERLOAD:
6767 : 1478469 : WT (((lang_tree_node *)t)->overload.function);
6768 : 1478469 : WT (t->common.chain);
6769 : 1478469 : break;
6770 : :
6771 : 0 : case PTRMEM_CST:
6772 : 0 : WT (((lang_tree_node *)t)->ptrmem.member);
6773 : 0 : break;
6774 : :
6775 : 13204 : case STATIC_ASSERT:
6776 : 13204 : WT (((lang_tree_node *)t)->static_assertion.condition);
6777 : 13204 : WT (((lang_tree_node *)t)->static_assertion.message);
6778 : 13204 : if (state)
6779 : 13204 : state->write_location
6780 : 13204 : (*this, ((lang_tree_node *)t)->static_assertion.location);
6781 : : break;
6782 : :
6783 : 560691 : case TEMPLATE_DECL:
6784 : : /* Streamed with the template_decl node itself. */
6785 : 560691 : gcc_checking_assert
6786 : : (TREE_VISITED (((lang_tree_node *)t)->template_decl.arguments));
6787 : 560691 : gcc_checking_assert
6788 : : (TREE_VISITED (((lang_tree_node *)t)->template_decl.result));
6789 : 560691 : if (DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (t))
6790 : 9882 : WT (DECL_CHAIN (t));
6791 : : break;
6792 : :
6793 : 1259921 : case TEMPLATE_INFO:
6794 : 1259921 : {
6795 : 1259921 : WT (((lang_tree_node *)t)->template_info.tmpl);
6796 : 1259921 : WT (((lang_tree_node *)t)->template_info.args);
6797 : 1259921 : WT (((lang_tree_node *)t)->template_info.partial);
6798 : :
6799 : 1259921 : const auto *ac = (((lang_tree_node *)t)
6800 : : ->template_info.deferred_access_checks);
6801 : 1259921 : unsigned len = vec_safe_length (ac);
6802 : 1259921 : if (streaming_p ())
6803 : 628497 : u (len);
6804 : 1259921 : if (len)
6805 : : {
6806 : 0 : for (unsigned ix = 0; ix != len; ix++)
6807 : : {
6808 : 0 : const auto &m = (*ac)[ix];
6809 : 0 : WT (m.binfo);
6810 : 0 : WT (m.decl);
6811 : 0 : WT (m.diag_decl);
6812 : 0 : if (state)
6813 : 0 : state->write_location (*this, m.loc);
6814 : : }
6815 : : }
6816 : : }
6817 : : break;
6818 : :
6819 : 1564561 : case TEMPLATE_PARM_INDEX:
6820 : 1564561 : if (streaming_p ())
6821 : : {
6822 : 351734 : WU (((lang_tree_node *)t)->tpi.index);
6823 : 351734 : WU (((lang_tree_node *)t)->tpi.level);
6824 : 351734 : WU (((lang_tree_node *)t)->tpi.orig_level);
6825 : : }
6826 : 1564561 : WT (((lang_tree_node *)t)->tpi.decl);
6827 : : /* TEMPLATE_PARM_DESCENDANTS (AKA TREE_CHAIN) is an internal
6828 : : cache, do not stream. */
6829 : 1564561 : break;
6830 : :
6831 : 24499 : case TRAIT_EXPR:
6832 : 24499 : WT (((lang_tree_node *)t)->trait_expression.type1);
6833 : 24499 : WT (((lang_tree_node *)t)->trait_expression.type2);
6834 : 24499 : if (streaming_p ())
6835 : 9430 : WU (((lang_tree_node *)t)->trait_expression.kind);
6836 : : break;
6837 : :
6838 : 4 : case TU_LOCAL_ENTITY:
6839 : 4 : WT (((lang_tree_node *)t)->tu_local_entity.name);
6840 : 4 : if (state)
6841 : 4 : state->write_location
6842 : 4 : (*this, ((lang_tree_node *)t)->tu_local_entity.loc);
6843 : : break;
6844 : : }
6845 : :
6846 : 31873542 : if (CODE_CONTAINS_STRUCT (code, TS_TYPED))
6847 : : {
6848 : : /* We want to stream the type of a expression-like nodes /after/
6849 : : we've streamed the operands. The type often contains (bits
6850 : : of the) types of the operands, and with things like decltype
6851 : : and noexcept in play, we really want to stream the decls
6852 : : defining the type before we try and stream the type on its
6853 : : own. Otherwise we can find ourselves trying to read in a
6854 : : decl, when we're already partially reading in a component of
6855 : : its type. And that's bad. */
6856 : 30032551 : tree type = t->typed.type;
6857 : 30032551 : unsigned prec = 0;
6858 : :
6859 : 30032551 : switch (code)
6860 : : {
6861 : : default:
6862 : : break;
6863 : :
6864 : : case TEMPLATE_DECL:
6865 : : /* We fill in the template's type separately. */
6866 : 30032551 : type = NULL_TREE;
6867 : : break;
6868 : :
6869 : 2207575 : case TYPE_DECL:
6870 : 2207575 : if (DECL_ORIGINAL_TYPE (t) && t == TYPE_NAME (type))
6871 : : /* This is a typedef. We set its type separately. */
6872 : : type = NULL_TREE;
6873 : : break;
6874 : :
6875 : 7536 : case ENUMERAL_TYPE:
6876 : 7536 : if (type && !ENUM_FIXED_UNDERLYING_TYPE_P (t))
6877 : : {
6878 : : /* Type is a restricted range integer type derived from the
6879 : : integer_types. Find the right one. */
6880 : 4808 : prec = TYPE_PRECISION (type);
6881 : 4808 : tree name = DECL_NAME (TYPE_NAME (type));
6882 : :
6883 : 62828 : for (unsigned itk = itk_none; itk--;)
6884 : 62828 : if (integer_types[itk]
6885 : 62828 : && DECL_NAME (TYPE_NAME (integer_types[itk])) == name)
6886 : : {
6887 : : type = integer_types[itk];
6888 : : break;
6889 : : }
6890 : 4808 : gcc_assert (type != t->typed.type);
6891 : : }
6892 : : break;
6893 : : }
6894 : :
6895 : 30032551 : WT (type);
6896 : 30032551 : if (prec && streaming_p ())
6897 : 2402 : WU (prec);
6898 : : }
6899 : :
6900 : 31873542 : if (TREE_CODE (t) == CONSTRUCTOR)
6901 : : {
6902 : 85861 : unsigned len = vec_safe_length (t->constructor.elts);
6903 : 85861 : if (streaming_p ())
6904 : 42266 : WU (len);
6905 : 85861 : if (len)
6906 : 327806 : for (unsigned ix = 0; ix != len; ix++)
6907 : : {
6908 : 280566 : const constructor_elt &elt = (*t->constructor.elts)[ix];
6909 : :
6910 : 280566 : WT (elt.index);
6911 : 280566 : WT (elt.value);
6912 : : }
6913 : : }
6914 : :
6915 : : #undef WT
6916 : : #undef WU
6917 : 31873542 : }
6918 : :
6919 : : // Streaming in a reference to a decl can cause that decl to be
6920 : : // TREE_USED, which is the mark_used behaviour we need most of the
6921 : : // time. The trees_in::unused can be incremented to inhibit this,
6922 : : // which is at least needed for vtables.
6923 : :
6924 : : bool
6925 : 10508310 : trees_in::core_vals (tree t)
6926 : : {
6927 : : #define RU(X) ((X) = u ())
6928 : : #define RUC(T,X) ((X) = T (u ()))
6929 : : #define RT(X) ((X) = tree_node ())
6930 : : #define RTU(X) ((X) = tree_node (true))
6931 : 10508310 : tree_code code = TREE_CODE (t);
6932 : :
6933 : : /* First by tree shape. */
6934 : 10508310 : if (CODE_CONTAINS_STRUCT (code, TS_DECL_MINIMAL))
6935 : : {
6936 : 2310829 : RT (t->decl_minimal.name);
6937 : 2310829 : if (!DECL_TEMPLATE_PARM_P (t))
6938 : 2012176 : RT (t->decl_minimal.context);
6939 : :
6940 : : /* Don't zap the locus just yet, we don't record it correctly
6941 : : and thus lose all location information. */
6942 : 2310829 : t->decl_minimal.locus = state->read_location (*this);
6943 : 2310829 : if (has_warning_spec (t))
6944 : 381 : put_warning_spec (t, u ());
6945 : : }
6946 : :
6947 : 10508310 : if (CODE_CONTAINS_STRUCT (code, TS_TYPE_COMMON))
6948 : : {
6949 : 398346 : RT (t->type_common.name);
6950 : 398346 : RT (t->type_common.context);
6951 : :
6952 : 398346 : RT (t->type_common.main_variant);
6953 : 398346 : RT (t->type_common.canonical);
6954 : :
6955 : : /* type_common.next_variant is internally manipulated. */
6956 : : /* type_common.pointer_to, type_common.reference_to. */
6957 : :
6958 : 398346 : RU (t->type_common.precision);
6959 : 398346 : RU (t->type_common.contains_placeholder_bits);
6960 : 398346 : RUC (machine_mode, t->type_common.mode);
6961 : 398346 : RU (t->type_common.align);
6962 : :
6963 : 398346 : if (!RECORD_OR_UNION_CODE_P (code))
6964 : : {
6965 : 271010 : RT (t->type_common.size);
6966 : 271010 : RT (t->type_common.size_unit);
6967 : : }
6968 : 398346 : RT (t->type_common.attributes);
6969 : :
6970 : 398346 : RT (t->type_common.common.chain); /* TYPE_STUB_DECL. */
6971 : : }
6972 : :
6973 : 10508310 : if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
6974 : : {
6975 : 2310829 : RUC (machine_mode, t->decl_common.mode);
6976 : 2310829 : RU (t->decl_common.off_align);
6977 : 2310829 : RU (t->decl_common.align);
6978 : :
6979 : 2310829 : if (code != TEMPLATE_DECL)
6980 : : {
6981 : 2078469 : RT (t->decl_common.size);
6982 : 2078469 : RT (t->decl_common.size_unit);
6983 : : }
6984 : :
6985 : 2310829 : RT (t->decl_common.attributes);
6986 : 2310829 : RT (t->decl_common.abstract_origin);
6987 : : }
6988 : :
6989 : 10508310 : if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
6990 : : {
6991 : 1076142 : RT (t->decl_with_vis.assembler_name);
6992 : 1076142 : RUC (symbol_visibility, t->decl_with_vis.visibility);
6993 : : }
6994 : :
6995 : 10508310 : if (CODE_CONTAINS_STRUCT (code, TS_TYPE_NON_COMMON))
6996 : : {
6997 : 398346 : if (code == ENUMERAL_TYPE)
6998 : : {
6999 : : /* These fields get set even for opaque enums that lack a
7000 : : definition, so we stream them directly for each ENUMERAL_TYPE.
7001 : : We stream TYPE_VALUES as part of the definition. */
7002 : 2475 : RT (t->type_non_common.maxval);
7003 : 2475 : RT (t->type_non_common.minval);
7004 : : }
7005 : : /* Records and unions hold FIELDS, VFIELD & BINFO on these
7006 : : things. */
7007 : 395871 : else if (!RECORD_OR_UNION_CODE_P (code))
7008 : : {
7009 : : /* This is not clobbering TYPE_CACHED_VALUES, because this
7010 : : is a type that doesn't have any. */
7011 : 268535 : gcc_checking_assert (!TYPE_CACHED_VALUES_P (t));
7012 : 268535 : RT (t->type_non_common.values);
7013 : 268535 : RT (t->type_non_common.maxval);
7014 : 268535 : RT (t->type_non_common.minval);
7015 : : }
7016 : :
7017 : 398346 : RT (t->type_non_common.lang_1);
7018 : : }
7019 : :
7020 : 10508310 : if (CODE_CONTAINS_STRUCT (code, TS_EXP))
7021 : : {
7022 : 4000102 : t->exp.locus = state->read_location (*this);
7023 : 4000102 : if (has_warning_spec (t))
7024 : 319033 : put_warning_spec (t, u ());
7025 : :
7026 : 4000102 : bool vl = TREE_CODE_CLASS (code) == tcc_vl_exp;
7027 : 4000102 : for (unsigned limit = (vl ? VL_EXP_OPERAND_LENGTH (t)
7028 : 4000102 : : TREE_OPERAND_LENGTH (t)),
7029 : 11100176 : ix = unsigned (vl); ix != limit; ix++)
7030 : 7100074 : RTU (TREE_OPERAND (t, ix));
7031 : : }
7032 : :
7033 : : /* Then by CODE. Special cases and/or 1:1 tree shape
7034 : : correspondance. */
7035 : 10508310 : switch (code)
7036 : : {
7037 : : default:
7038 : : break;
7039 : :
7040 : : case ARGUMENT_PACK_SELECT:
7041 : : case DEFERRED_PARSE:
7042 : : case IDENTIFIER_NODE:
7043 : : case BINDING_VECTOR:
7044 : : case SSA_NAME:
7045 : : case TRANSLATION_UNIT_DECL:
7046 : : case USERDEF_LITERAL:
7047 : : return false; /* Should never meet. */
7048 : :
7049 : : /* Constants. */
7050 : 9 : case COMPLEX_CST:
7051 : 9 : RT (TREE_REALPART (t));
7052 : 9 : RT (TREE_IMAGPART (t));
7053 : 9 : break;
7054 : :
7055 : : case FIXED_CST:
7056 : : /* Not suported in C++. */
7057 : : return false;
7058 : :
7059 : 471206 : case INTEGER_CST:
7060 : 471206 : {
7061 : 471206 : unsigned num = TREE_INT_CST_EXT_NUNITS (t);
7062 : 943739 : for (unsigned ix = 0; ix != num; ix++)
7063 : 472533 : TREE_INT_CST_ELT (t, ix) = wu ();
7064 : : }
7065 : : break;
7066 : :
7067 : : case POLY_INT_CST:
7068 : 0 : for (unsigned ix = 0; ix != NUM_POLY_INT_COEFFS; ix++)
7069 : 0 : RT (POLY_INT_CST_COEFF (t, ix));
7070 : : break;
7071 : :
7072 : 13954 : case REAL_CST:
7073 : 13954 : if (const void *bytes = buf (sizeof (real_value)))
7074 : 13954 : memcpy (TREE_REAL_CST_PTR (t), bytes, sizeof (real_value));
7075 : : break;
7076 : :
7077 : : case STRING_CST:
7078 : : /* Streamed during start. */
7079 : : break;
7080 : :
7081 : 6 : case RAW_DATA_CST:
7082 : 6 : RT (RAW_DATA_OWNER (t));
7083 : 6 : gcc_assert (TREE_CODE (RAW_DATA_OWNER (t)) == STRING_CST
7084 : : && TREE_STRING_LENGTH (RAW_DATA_OWNER (t)));
7085 : 6 : RAW_DATA_POINTER (t) = TREE_STRING_POINTER (RAW_DATA_OWNER (t)) + z ();
7086 : 6 : break;
7087 : :
7088 : 24 : case VECTOR_CST:
7089 : 63 : for (unsigned ix = vector_cst_encoded_nelts (t); ix--;)
7090 : 39 : RT (VECTOR_CST_ENCODED_ELT (t, ix));
7091 : : break;
7092 : :
7093 : : /* Decls. */
7094 : 157427 : case VAR_DECL:
7095 : 157427 : if (DECL_CONTEXT (t)
7096 : 157427 : && TREE_CODE (DECL_CONTEXT (t)) != FUNCTION_DECL)
7097 : : {
7098 : 33655 : if (DECL_HAS_VALUE_EXPR_P (t))
7099 : : {
7100 : 9 : tree val = tree_node ();
7101 : 9 : SET_DECL_VALUE_EXPR (t, val);
7102 : : }
7103 : : break;
7104 : : }
7105 : : /* FALLTHROUGH */
7106 : :
7107 : 1035302 : case RESULT_DECL:
7108 : 1035302 : case PARM_DECL:
7109 : 1035302 : if (DECL_HAS_VALUE_EXPR_P (t))
7110 : : {
7111 : : /* The DECL_VALUE hash table is a cache, thus if we're
7112 : : reading a duplicate (which we end up discarding), the
7113 : : value expr will also be cleaned up at the next gc. */
7114 : 10154 : tree val = tree_node ();
7115 : 10154 : SET_DECL_VALUE_EXPR (t, val);
7116 : : }
7117 : : /* FALLTHROUGH */
7118 : :
7119 : 1068399 : case CONST_DECL:
7120 : 1068399 : case IMPORTED_DECL:
7121 : 1068399 : RT (t->decl_common.initial);
7122 : 1068399 : break;
7123 : :
7124 : 40500 : case FIELD_DECL:
7125 : 40500 : RT (t->field_decl.offset);
7126 : 40500 : RT (t->field_decl.bit_field_type);
7127 : 40500 : RT (t->field_decl.qualifier);
7128 : 40500 : RT (t->field_decl.bit_offset);
7129 : 40500 : RT (t->field_decl.fcontext);
7130 : 40500 : RT (t->decl_common.initial);
7131 : 40500 : break;
7132 : :
7133 : 12543 : case LABEL_DECL:
7134 : 12543 : RU (t->label_decl.label_decl_uid);
7135 : 12543 : RU (t->label_decl.eh_landing_pad_nr);
7136 : 12543 : break;
7137 : :
7138 : 324797 : case FUNCTION_DECL:
7139 : 324797 : {
7140 : 324797 : unsigned bltin = u ();
7141 : 324797 : t->function_decl.built_in_class = built_in_class (bltin);
7142 : 324797 : if (bltin != NOT_BUILT_IN)
7143 : : {
7144 : 6632 : bltin = u ();
7145 : 6632 : DECL_UNCHECKED_FUNCTION_CODE (t) = built_in_function (bltin);
7146 : : }
7147 : :
7148 : 324797 : RT (t->function_decl.personality);
7149 : : /* These properties are not streamed, and should be reconstructed
7150 : : from any function attributes. */
7151 : : // t->function_decl.function_specific_target);
7152 : : // t->function_decl.function_specific_optimization);
7153 : 324797 : RT (t->function_decl.vindex);
7154 : :
7155 : 324797 : if (DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (t))
7156 : : {
7157 : 2104 : tree spec;
7158 : 2104 : RT (spec);
7159 : 2104 : store_explicit_specifier (t, spec);
7160 : : }
7161 : : }
7162 : : break;
7163 : :
7164 : 38115 : case USING_DECL:
7165 : : /* USING_DECL_DECLS */
7166 : 38115 : RT (t->decl_common.initial);
7167 : : /* FALLTHROUGH */
7168 : :
7169 : 593793 : case TYPE_DECL:
7170 : : /* USING_DECL: USING_DECL_SCOPE */
7171 : : /* TYPE_DECL: DECL_ORIGINAL_TYPE */
7172 : 593793 : RT (t->decl_non_common.result);
7173 : 593793 : break;
7174 : :
7175 : : /* Miscellaneous common nodes. */
7176 : 210256 : case BLOCK:
7177 : 210256 : t->block.locus = state->read_location (*this);
7178 : 210256 : t->block.end_locus = state->read_location (*this);
7179 : :
7180 : 210256 : for (tree *chain = &t->block.vars;;)
7181 : 318722 : if (tree decl = tree_node ())
7182 : : {
7183 : : /* For a deduplicated local type or enumerator, chain the
7184 : : duplicate decl instead of the canonical in-TU decl. Seeing
7185 : : a duplicate here means the containing function whose body
7186 : : we're streaming in is a duplicate too, so we'll end up
7187 : : discarding this BLOCK (and the rest of the duplicate function
7188 : : body) anyway. */
7189 : 108466 : decl = maybe_duplicate (decl);
7190 : :
7191 : 108466 : if (!DECL_P (decl))
7192 : : {
7193 : 0 : set_overrun ();
7194 : 0 : break;
7195 : : }
7196 : :
7197 : : /* If DECL_CHAIN is already set then this was a backreference to a
7198 : : local type or enumerator from a previous read (PR c++/114630).
7199 : : Let's copy the node so we can keep building the chain for ODR
7200 : : checking later. */
7201 : 108466 : if (DECL_CHAIN (decl))
7202 : : {
7203 : 12 : gcc_checking_assert (TREE_CODE (decl) == TYPE_DECL
7204 : : && find_duplicate (DECL_CONTEXT (decl)));
7205 : 6 : decl = copy_decl (decl);
7206 : : }
7207 : :
7208 : 108466 : *chain = decl;
7209 : 108466 : chain = &DECL_CHAIN (decl);
7210 : : }
7211 : : else
7212 : 108466 : break;
7213 : :
7214 : : /* nonlocalized_vars is middle-end. */
7215 : 210256 : RT (t->block.subblocks);
7216 : 210256 : RT (t->block.supercontext);
7217 : 210256 : RT (t->block.abstract_origin);
7218 : : /* fragment_origin, fragment_chain are middle-end. */
7219 : 210256 : RT (t->block.chain);
7220 : : /* nonlocalized_vars, block_num, die are middle endy/debug
7221 : : things. */
7222 : 210256 : break;
7223 : :
7224 : 388518 : case CALL_EXPR:
7225 : 388518 : RUC (internal_fn, t->base.u.ifn);
7226 : 388518 : break;
7227 : :
7228 : : case CONSTRUCTOR:
7229 : : // Streamed after the node's type.
7230 : : break;
7231 : :
7232 : 12 : case OMP_CLAUSE:
7233 : 12 : {
7234 : 12 : RU (t->omp_clause.subcode.map_kind);
7235 : 12 : t->omp_clause.locus = state->read_location (*this);
7236 : :
7237 : 12 : unsigned len = omp_clause_num_ops[OMP_CLAUSE_CODE (t)];
7238 : 45 : for (unsigned ix = 0; ix != len; ix++)
7239 : 33 : RT (t->omp_clause.ops[ix]);
7240 : : }
7241 : : break;
7242 : :
7243 : 145059 : case STATEMENT_LIST:
7244 : 145059 : {
7245 : 145059 : tree_stmt_iterator iter = tsi_start (t);
7246 : 586732 : for (tree stmt; RT (stmt);)
7247 : : {
7248 : 441673 : if (TREE_CODE (stmt) == DEBUG_BEGIN_STMT
7249 : 0 : && !MAY_HAVE_DEBUG_MARKER_STMTS)
7250 : 0 : continue;
7251 : 441673 : tsi_link_after (&iter, stmt, TSI_CONTINUE_LINKING);
7252 : : }
7253 : : }
7254 : 145059 : break;
7255 : :
7256 : 0 : case OPTIMIZATION_NODE:
7257 : 0 : case TARGET_OPTION_NODE:
7258 : : /* Not implemented, see trees_out::core_vals. */
7259 : 0 : gcc_unreachable ();
7260 : 71336 : break;
7261 : :
7262 : 71336 : case TREE_BINFO:
7263 : 71336 : RT (t->binfo.common.chain);
7264 : 71336 : RT (t->binfo.offset);
7265 : 71336 : RT (t->binfo.inheritance);
7266 : 71336 : RT (t->binfo.vptr_field);
7267 : :
7268 : : /* Do not mark the vtables as USED in the address expressions
7269 : : here. */
7270 : 71336 : unused++;
7271 : 71336 : RT (t->binfo.vtable);
7272 : 71336 : RT (t->binfo.virtuals);
7273 : 71336 : RT (t->binfo.vtt_subvtt);
7274 : 71336 : RT (t->binfo.vtt_vptr);
7275 : 71336 : unused--;
7276 : :
7277 : 71336 : BINFO_BASE_ACCESSES (t) = tree_vec ();
7278 : 71336 : if (!get_overrun ())
7279 : : {
7280 : 71336 : unsigned num = vec_safe_length (BINFO_BASE_ACCESSES (t));
7281 : 91541 : for (unsigned ix = 0; ix != num; ix++)
7282 : 20205 : BINFO_BASE_APPEND (t, tree_node ());
7283 : : }
7284 : : break;
7285 : :
7286 : 919626 : case TREE_LIST:
7287 : 919626 : RT (t->list.purpose);
7288 : 919626 : RT (t->list.value);
7289 : 919626 : RT (t->list.common.chain);
7290 : 919626 : break;
7291 : :
7292 : 581176 : case TREE_VEC:
7293 : 1557911 : for (unsigned ix = TREE_VEC_LENGTH (t); ix--;)
7294 : 976735 : RT (TREE_VEC_ELT (t, ix));
7295 : 581176 : RT (t->type_common.common.chain);
7296 : 581176 : break;
7297 : :
7298 : : /* C++-specific nodes ... */
7299 : 67814 : case BASELINK:
7300 : 67814 : RT (((lang_tree_node *)t)->baselink.binfo);
7301 : 67814 : RTU (((lang_tree_node *)t)->baselink.functions);
7302 : 67814 : RT (((lang_tree_node *)t)->baselink.access_binfo);
7303 : 67814 : break;
7304 : :
7305 : 28105 : case CONSTRAINT_INFO:
7306 : 28105 : RT (((lang_tree_node *)t)->constraint_info.template_reqs);
7307 : 28105 : RT (((lang_tree_node *)t)->constraint_info.declarator_reqs);
7308 : 28105 : RT (((lang_tree_node *)t)->constraint_info.associated_constr);
7309 : 28105 : break;
7310 : :
7311 : 4990 : case DEFERRED_NOEXCEPT:
7312 : 4990 : RT (((lang_tree_node *)t)->deferred_noexcept.pattern);
7313 : 4990 : RT (((lang_tree_node *)t)->deferred_noexcept.args);
7314 : 4990 : break;
7315 : :
7316 : 2996 : case LAMBDA_EXPR:
7317 : 2996 : RT (((lang_tree_node *)t)->lambda_expression.capture_list);
7318 : 2996 : RT (((lang_tree_node *)t)->lambda_expression.this_capture);
7319 : 2996 : RT (((lang_tree_node *)t)->lambda_expression.extra_scope);
7320 : 2996 : RT (((lang_tree_node *)t)->lambda_expression.regen_info);
7321 : 2996 : RT (((lang_tree_node *)t)->lambda_expression.extra_args);
7322 : : /* lambda_expression.pending_proxies is NULL */
7323 : 2996 : ((lang_tree_node *)t)->lambda_expression.locus
7324 : 2996 : = state->read_location (*this);
7325 : 2996 : RUC (cp_lambda_default_capture_mode_type,
7326 : : ((lang_tree_node *)t)->lambda_expression.default_capture_mode);
7327 : 2996 : RU (((lang_tree_node *)t)->lambda_expression.discriminator_scope);
7328 : 2996 : RU (((lang_tree_node *)t)->lambda_expression.discriminator_sig);
7329 : 2996 : break;
7330 : :
7331 : 387952 : case OVERLOAD:
7332 : 387952 : RT (((lang_tree_node *)t)->overload.function);
7333 : 387952 : RT (t->common.chain);
7334 : 387952 : break;
7335 : :
7336 : 0 : case PTRMEM_CST:
7337 : 0 : RT (((lang_tree_node *)t)->ptrmem.member);
7338 : 0 : break;
7339 : :
7340 : 5475 : case STATIC_ASSERT:
7341 : 5475 : RT (((lang_tree_node *)t)->static_assertion.condition);
7342 : 5475 : RT (((lang_tree_node *)t)->static_assertion.message);
7343 : 5475 : ((lang_tree_node *)t)->static_assertion.location
7344 : 5475 : = state->read_location (*this);
7345 : 5475 : break;
7346 : :
7347 : 232360 : case TEMPLATE_DECL:
7348 : : /* Streamed when reading the raw template decl itself. */
7349 : 232360 : gcc_assert (((lang_tree_node *)t)->template_decl.arguments);
7350 : 232360 : gcc_assert (((lang_tree_node *)t)->template_decl.result);
7351 : 232360 : if (DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (t))
7352 : 4657 : RT (DECL_CHAIN (t));
7353 : : break;
7354 : :
7355 : 518289 : case TEMPLATE_INFO:
7356 : 518289 : RT (((lang_tree_node *)t)->template_info.tmpl);
7357 : 518289 : RT (((lang_tree_node *)t)->template_info.args);
7358 : 518289 : RT (((lang_tree_node *)t)->template_info.partial);
7359 : 518289 : if (unsigned len = u ())
7360 : : {
7361 : 0 : auto &ac = (((lang_tree_node *)t)
7362 : : ->template_info.deferred_access_checks);
7363 : 0 : vec_alloc (ac, len);
7364 : 0 : for (unsigned ix = 0; ix != len; ix++)
7365 : : {
7366 : 0 : deferred_access_check m;
7367 : :
7368 : 0 : RT (m.binfo);
7369 : 0 : RT (m.decl);
7370 : 0 : RT (m.diag_decl);
7371 : 0 : m.loc = state->read_location (*this);
7372 : 0 : ac->quick_push (m);
7373 : : }
7374 : : }
7375 : : break;
7376 : :
7377 : 285873 : case TEMPLATE_PARM_INDEX:
7378 : 285873 : RU (((lang_tree_node *)t)->tpi.index);
7379 : 285873 : RU (((lang_tree_node *)t)->tpi.level);
7380 : 285873 : RU (((lang_tree_node *)t)->tpi.orig_level);
7381 : 285873 : RT (((lang_tree_node *)t)->tpi.decl);
7382 : 285873 : break;
7383 : :
7384 : 6670 : case TRAIT_EXPR:
7385 : 6670 : RT (((lang_tree_node *)t)->trait_expression.type1);
7386 : 6670 : RT (((lang_tree_node *)t)->trait_expression.type2);
7387 : 6670 : RUC (cp_trait_kind, ((lang_tree_node *)t)->trait_expression.kind);
7388 : 6670 : break;
7389 : :
7390 : 2 : case TU_LOCAL_ENTITY:
7391 : 2 : RT (((lang_tree_node *)t)->tu_local_entity.name);
7392 : 2 : ((lang_tree_node *)t)->tu_local_entity.loc
7393 : 2 : = state->read_location (*this);
7394 : : }
7395 : :
7396 : 10508310 : if (CODE_CONTAINS_STRUCT (code, TS_TYPED))
7397 : : {
7398 : 9741193 : tree type = tree_node ();
7399 : :
7400 : 9741193 : if (type && code == ENUMERAL_TYPE && !ENUM_FIXED_UNDERLYING_TYPE_P (t))
7401 : : {
7402 : 1450 : unsigned precision = u ();
7403 : :
7404 : 1450 : type = build_distinct_type_copy (type);
7405 : 1450 : TYPE_PRECISION (type) = precision;
7406 : 2900 : set_min_and_max_values_for_integral_type (type, precision,
7407 : 1450 : TYPE_SIGN (type));
7408 : : }
7409 : :
7410 : 9741193 : if (code != TEMPLATE_DECL)
7411 : 9508833 : t->typed.type = type;
7412 : : }
7413 : :
7414 : 10508310 : if (TREE_CODE (t) == CONSTRUCTOR)
7415 : 38792 : if (unsigned len = u ())
7416 : : {
7417 : 23834 : vec_alloc (t->constructor.elts, len);
7418 : 165700 : for (unsigned ix = 0; ix != len; ix++)
7419 : : {
7420 : 141866 : constructor_elt elt;
7421 : :
7422 : 141866 : RT (elt.index);
7423 : 141866 : RTU (elt.value);
7424 : 141866 : t->constructor.elts->quick_push (elt);
7425 : : }
7426 : : }
7427 : :
7428 : : #undef RT
7429 : : #undef RM
7430 : : #undef RU
7431 : 10508310 : return !get_overrun ();
7432 : : }
7433 : :
7434 : : void
7435 : 4088835 : trees_out::lang_decl_vals (tree t)
7436 : : {
7437 : 4088835 : const struct lang_decl *lang = DECL_LANG_SPECIFIC (t);
7438 : : #define WU(X) (u (X))
7439 : : #define WT(X) (tree_node (X))
7440 : : /* Module index already written. */
7441 : 4088835 : switch (lang->u.base.selector)
7442 : : {
7443 : 0 : default:
7444 : 0 : gcc_unreachable ();
7445 : :
7446 : 757251 : case lds_fn: /* lang_decl_fn. */
7447 : 757251 : if (streaming_p ())
7448 : : {
7449 : 378541 : if (DECL_NAME (t) && IDENTIFIER_OVL_OP_P (DECL_NAME (t)))
7450 : 62762 : WU (lang->u.fn.ovl_op_code);
7451 : : }
7452 : :
7453 : 959820 : if (DECL_CLASS_SCOPE_P (t) || DECL_UNIQUE_FRIEND_P (t))
7454 : 574688 : WT (lang->u.fn.context);
7455 : :
7456 : 757251 : if (lang->u.fn.thunk_p)
7457 : : {
7458 : : /* The thunked-to function. */
7459 : 1128 : WT (lang->u.fn.befriending_classes);
7460 : 1128 : if (streaming_p ())
7461 : 564 : wi (lang->u.fn.u5.fixed_offset);
7462 : : }
7463 : 756123 : else if (decl_tls_wrapper_p (t))
7464 : : /* The wrapped variable. */
7465 : 18 : WT (lang->u.fn.befriending_classes);
7466 : : else
7467 : 756105 : WT (lang->u.fn.u5.cloned_function);
7468 : :
7469 : 757251 : if (FNDECL_USED_AUTO (t))
7470 : 2390 : WT (lang->u.fn.u.saved_auto_return_type);
7471 : :
7472 : 757251 : goto lds_min;
7473 : :
7474 : 2766 : case lds_decomp: /* lang_decl_decomp. */
7475 : 2766 : WT (lang->u.decomp.base);
7476 : 2766 : goto lds_min;
7477 : :
7478 : 2429712 : case lds_min: /* lang_decl_min. */
7479 : 2429712 : lds_min:
7480 : 2429712 : WT (lang->u.min.template_info);
7481 : 2429712 : {
7482 : 2429712 : tree access = lang->u.min.access;
7483 : :
7484 : : /* DECL_ACCESS needs to be maintained by the definition of the
7485 : : (derived) class that changes the access. The other users
7486 : : of DECL_ACCESS need to write it here. */
7487 : 757251 : if (!DECL_THUNK_P (t)
7488 : 3185835 : && (DECL_CONTEXT (t) && TYPE_P (DECL_CONTEXT (t))))
7489 : : access = NULL_TREE;
7490 : :
7491 : 2429712 : WT (access);
7492 : : }
7493 : 2429712 : break;
7494 : :
7495 : : case lds_ns: /* lang_decl_ns. */
7496 : : break;
7497 : :
7498 : 1658958 : case lds_parm: /* lang_decl_parm. */
7499 : 1658958 : if (streaming_p ())
7500 : : {
7501 : 565688 : WU (lang->u.parm.level);
7502 : 565688 : WU (lang->u.parm.index);
7503 : : }
7504 : : break;
7505 : : }
7506 : : #undef WU
7507 : : #undef WT
7508 : 4088835 : }
7509 : :
7510 : : bool
7511 : 1474085 : trees_in::lang_decl_vals (tree t)
7512 : : {
7513 : 1474085 : struct lang_decl *lang = DECL_LANG_SPECIFIC (t);
7514 : : #define RU(X) ((X) = u ())
7515 : : #define RT(X) ((X) = tree_node ())
7516 : :
7517 : : /* Module index already read. */
7518 : 1474085 : switch (lang->u.base.selector)
7519 : : {
7520 : 0 : default:
7521 : 0 : gcc_unreachable ();
7522 : :
7523 : 324797 : case lds_fn: /* lang_decl_fn. */
7524 : 324797 : if (DECL_NAME (t) && IDENTIFIER_OVL_OP_P (DECL_NAME (t)))
7525 : : {
7526 : 56099 : unsigned code = u ();
7527 : :
7528 : : /* Check consistency. */
7529 : 56099 : if (code >= OVL_OP_MAX
7530 : 56099 : || (ovl_op_info[IDENTIFIER_ASSIGN_OP_P (DECL_NAME (t))][code]
7531 : 56099 : .ovl_op_code) == OVL_OP_ERROR_MARK)
7532 : 0 : set_overrun ();
7533 : : else
7534 : 56099 : lang->u.fn.ovl_op_code = code;
7535 : : }
7536 : :
7537 : 409822 : if (DECL_CLASS_SCOPE_P (t) || DECL_UNIQUE_FRIEND_P (t))
7538 : 249089 : RT (lang->u.fn.context);
7539 : :
7540 : 324797 : if (lang->u.fn.thunk_p)
7541 : : {
7542 : 490 : RT (lang->u.fn.befriending_classes);
7543 : 490 : lang->u.fn.u5.fixed_offset = wi ();
7544 : : }
7545 : 324307 : else if (decl_tls_wrapper_p (t))
7546 : 15 : RT (lang->u.fn.befriending_classes);
7547 : : else
7548 : 324292 : RT (lang->u.fn.u5.cloned_function);
7549 : :
7550 : 324797 : if (FNDECL_USED_AUTO (t))
7551 : 1019 : RT (lang->u.fn.u.saved_auto_return_type);
7552 : 324797 : goto lds_min;
7553 : :
7554 : 1303 : case lds_decomp: /* lang_decl_decomp. */
7555 : 1303 : RT (lang->u.decomp.base);
7556 : 1303 : goto lds_min;
7557 : :
7558 : 994159 : case lds_min: /* lang_decl_min. */
7559 : 994159 : lds_min:
7560 : 994159 : RT (lang->u.min.template_info);
7561 : 994159 : RT (lang->u.min.access);
7562 : 994159 : break;
7563 : :
7564 : : case lds_ns: /* lang_decl_ns. */
7565 : : break;
7566 : :
7567 : 479828 : case lds_parm: /* lang_decl_parm. */
7568 : 479828 : RU (lang->u.parm.level);
7569 : 479828 : RU (lang->u.parm.index);
7570 : 479828 : break;
7571 : : }
7572 : : #undef RU
7573 : : #undef RT
7574 : 1474085 : return !get_overrun ();
7575 : : }
7576 : :
7577 : : /* Most of the value contents of lang_type is streamed in
7578 : : define_class. */
7579 : :
7580 : : void
7581 : 287800 : trees_out::lang_type_vals (tree t)
7582 : : {
7583 : 287800 : const struct lang_type *lang = TYPE_LANG_SPECIFIC (t);
7584 : : #define WU(X) (u (X))
7585 : : #define WT(X) (tree_node (X))
7586 : 287800 : if (streaming_p ())
7587 : 143880 : WU (lang->align);
7588 : : #undef WU
7589 : : #undef WT
7590 : 287800 : }
7591 : :
7592 : : bool
7593 : 112201 : trees_in::lang_type_vals (tree t)
7594 : : {
7595 : 112201 : struct lang_type *lang = TYPE_LANG_SPECIFIC (t);
7596 : : #define RU(X) ((X) = u ())
7597 : : #define RT(X) ((X) = tree_node ())
7598 : 112201 : RU (lang->align);
7599 : : #undef RU
7600 : : #undef RT
7601 : 112201 : return !get_overrun ();
7602 : : }
7603 : :
7604 : : /* Write out the bools of T, including information about any
7605 : : LANG_SPECIFIC information. Including allocation of any lang
7606 : : specific object. */
7607 : :
7608 : : void
7609 : 12346436 : trees_out::tree_node_bools (tree t)
7610 : : {
7611 : 12346436 : gcc_checking_assert (streaming_p ());
7612 : :
7613 : : /* We should never stream a namespace. */
7614 : 12346436 : gcc_checking_assert (TREE_CODE (t) != NAMESPACE_DECL
7615 : : || DECL_NAMESPACE_ALIAS (t));
7616 : :
7617 : 12346436 : bits_out bits = stream_bits ();
7618 : 12346436 : core_bools (t, bits);
7619 : :
7620 : 12346436 : switch (TREE_CODE_CLASS (TREE_CODE (t)))
7621 : : {
7622 : 2767729 : case tcc_declaration:
7623 : 2767729 : {
7624 : 2767729 : bool specific = DECL_LANG_SPECIFIC (t) != NULL;
7625 : 2767729 : bits.b (specific);
7626 : 2767729 : if (specific && VAR_P (t))
7627 : 281522 : bits.b (DECL_DECOMPOSITION_P (t));
7628 : 1773564 : if (specific)
7629 : 1773564 : lang_decl_bools (t, bits);
7630 : : }
7631 : : break;
7632 : :
7633 : 519717 : case tcc_type:
7634 : 519717 : {
7635 : 519717 : bool specific = (TYPE_MAIN_VARIANT (t) == t
7636 : 519717 : && TYPE_LANG_SPECIFIC (t) != NULL);
7637 : 519717 : gcc_assert (TYPE_LANG_SPECIFIC (t)
7638 : : == TYPE_LANG_SPECIFIC (TYPE_MAIN_VARIANT (t)));
7639 : :
7640 : 519717 : bits.b (specific);
7641 : 519717 : if (specific)
7642 : 143880 : lang_type_bools (t, bits);
7643 : : }
7644 : : break;
7645 : :
7646 : : default:
7647 : : break;
7648 : : }
7649 : :
7650 : 12346436 : bits.bflush ();
7651 : 12346436 : }
7652 : :
7653 : : bool
7654 : 10523905 : trees_in::tree_node_bools (tree t)
7655 : : {
7656 : 10523905 : bits_in bits = stream_bits ();
7657 : 10523905 : bool ok = core_bools (t, bits);
7658 : :
7659 : 10523905 : if (ok)
7660 : 10523905 : switch (TREE_CODE_CLASS (TREE_CODE (t)))
7661 : : {
7662 : 2310829 : case tcc_declaration:
7663 : 2310829 : if (bits.b ())
7664 : : {
7665 : 1474085 : bool decomp = VAR_P (t) && bits.b ();
7666 : :
7667 : 1474085 : ok = maybe_add_lang_decl_raw (t, decomp);
7668 : 1474085 : if (ok)
7669 : 1474085 : ok = lang_decl_bools (t, bits);
7670 : : }
7671 : : break;
7672 : :
7673 : 413941 : case tcc_type:
7674 : 413941 : if (bits.b ())
7675 : : {
7676 : 112201 : ok = maybe_add_lang_type_raw (t);
7677 : 112201 : if (ok)
7678 : 112201 : ok = lang_type_bools (t, bits);
7679 : : }
7680 : : break;
7681 : :
7682 : : default:
7683 : : break;
7684 : : }
7685 : :
7686 : 10523905 : bits.bflush ();
7687 : 10523905 : if (!ok || get_overrun ())
7688 : 0 : return false;
7689 : :
7690 : : return true;
7691 : 10523905 : }
7692 : :
7693 : :
7694 : : /* Write out the lang-specific vals of node T. */
7695 : :
7696 : : void
7697 : 31873542 : trees_out::lang_vals (tree t)
7698 : : {
7699 : 31873542 : switch (TREE_CODE_CLASS (TREE_CODE (t)))
7700 : : {
7701 : 7209143 : case tcc_declaration:
7702 : 7209143 : if (DECL_LANG_SPECIFIC (t))
7703 : 4088835 : lang_decl_vals (t);
7704 : : break;
7705 : :
7706 : 1799509 : case tcc_type:
7707 : 1799509 : if (TYPE_MAIN_VARIANT (t) == t && TYPE_LANG_SPECIFIC (t))
7708 : 287800 : lang_type_vals (t);
7709 : : break;
7710 : :
7711 : : default:
7712 : : break;
7713 : : }
7714 : 31873542 : }
7715 : :
7716 : : bool
7717 : 10508310 : trees_in::lang_vals (tree t)
7718 : : {
7719 : 10508310 : bool ok = true;
7720 : :
7721 : 10508310 : switch (TREE_CODE_CLASS (TREE_CODE (t)))
7722 : : {
7723 : 2310829 : case tcc_declaration:
7724 : 2310829 : if (DECL_LANG_SPECIFIC (t))
7725 : 1474085 : ok = lang_decl_vals (t);
7726 : : break;
7727 : :
7728 : 398346 : case tcc_type:
7729 : 398346 : if (TYPE_LANG_SPECIFIC (t))
7730 : 112201 : ok = lang_type_vals (t);
7731 : : else
7732 : 286145 : TYPE_LANG_SPECIFIC (t) = TYPE_LANG_SPECIFIC (TYPE_MAIN_VARIANT (t));
7733 : : break;
7734 : :
7735 : : default:
7736 : : break;
7737 : : }
7738 : :
7739 : 10508310 : return ok;
7740 : : }
7741 : :
7742 : : /* Write out the value fields of node T. */
7743 : :
7744 : : void
7745 : 31873542 : trees_out::tree_node_vals (tree t)
7746 : : {
7747 : 31873542 : core_vals (t);
7748 : 31873542 : lang_vals (t);
7749 : 31873542 : }
7750 : :
7751 : : bool
7752 : 10508310 : trees_in::tree_node_vals (tree t)
7753 : : {
7754 : 10508310 : bool ok = core_vals (t);
7755 : 10508310 : if (ok)
7756 : 10508310 : ok = lang_vals (t);
7757 : :
7758 : 10508310 : return ok;
7759 : : }
7760 : :
7761 : :
7762 : : /* If T is a back reference, fixed reference or NULL, write out its
7763 : : code and return WK_none. Otherwise return WK_value if we must write
7764 : : by value, or WK_normal otherwise. */
7765 : :
7766 : : walk_kind
7767 : 212703448 : trees_out::ref_node (tree t)
7768 : : {
7769 : 212703448 : if (!t)
7770 : : {
7771 : 86504117 : if (streaming_p ())
7772 : : {
7773 : : /* NULL_TREE -> tt_null. */
7774 : 33069612 : null_count++;
7775 : 33069612 : i (tt_null);
7776 : : }
7777 : 86504117 : return WK_none;
7778 : : }
7779 : :
7780 : 126199331 : if (!TREE_VISITED (t))
7781 : : return WK_normal;
7782 : :
7783 : : /* An already-visited tree. It must be in the map. */
7784 : 74339963 : int val = get_tag (t);
7785 : :
7786 : 74339963 : if (val == tag_value)
7787 : : /* An entry we should walk into. */
7788 : : return WK_value;
7789 : :
7790 : 73107498 : const char *kind;
7791 : :
7792 : 73107498 : if (val <= tag_backref)
7793 : : {
7794 : : /* Back reference -> -ve number */
7795 : 55848643 : if (streaming_p ())
7796 : 26495365 : i (val);
7797 : : kind = "backref";
7798 : : }
7799 : 17258855 : else if (val >= tag_fixed)
7800 : : {
7801 : : /* Fixed reference -> tt_fixed */
7802 : 17258855 : val -= tag_fixed;
7803 : 17258855 : if (streaming_p ())
7804 : 6120949 : i (tt_fixed), u (val);
7805 : : kind = "fixed";
7806 : : }
7807 : :
7808 : 73107498 : if (streaming_p ())
7809 : : {
7810 : 32616314 : back_ref_count++;
7811 : 32616314 : dump (dumper::TREE)
7812 : 14184 : && dump ("Wrote %s:%d %C:%N%S", kind, val, TREE_CODE (t), t, t);
7813 : : }
7814 : : return WK_none;
7815 : : }
7816 : :
7817 : : tree
7818 : 22429484 : trees_in::back_ref (int tag)
7819 : : {
7820 : 22429484 : tree res = NULL_TREE;
7821 : :
7822 : 22429484 : if (tag < 0 && unsigned (~tag) < back_refs.length ())
7823 : 22429484 : res = back_refs[~tag];
7824 : :
7825 : 22429484 : if (!res
7826 : : /* Checking TREE_CODE is a dereference, so we know this is not a
7827 : : wild pointer. Checking the code provides evidence we've not
7828 : : corrupted something. */
7829 : 22429484 : || TREE_CODE (res) >= MAX_TREE_CODES)
7830 : 0 : set_overrun ();
7831 : : else
7832 : 22444021 : dump (dumper::TREE) && dump ("Read backref:%d found %C:%N%S", tag,
7833 : : TREE_CODE (res), res, res);
7834 : 22429484 : return res;
7835 : : }
7836 : :
7837 : : unsigned
7838 : 2756803 : trees_out::add_indirect_tpl_parms (tree parms)
7839 : : {
7840 : 2756803 : unsigned len = 0;
7841 : 5155181 : for (; parms; parms = TREE_CHAIN (parms), len++)
7842 : : {
7843 : 2911490 : if (TREE_VISITED (parms))
7844 : : break;
7845 : :
7846 : 2398378 : int tag = insert (parms);
7847 : 2398378 : if (streaming_p ())
7848 : 2398669 : dump (dumper::TREE)
7849 : 153 : && dump ("Indirect:%d template's parameter %u %C:%N",
7850 : 153 : tag, len, TREE_CODE (parms), parms);
7851 : : }
7852 : :
7853 : 2756803 : if (streaming_p ())
7854 : 459304 : u (len);
7855 : :
7856 : 2756803 : return len;
7857 : : }
7858 : :
7859 : : unsigned
7860 : 374656 : trees_in::add_indirect_tpl_parms (tree parms)
7861 : : {
7862 : 374656 : unsigned len = u ();
7863 : 674622 : for (unsigned ix = 0; ix != len; parms = TREE_CHAIN (parms), ix++)
7864 : : {
7865 : 299966 : int tag = insert (parms);
7866 : 300410 : dump (dumper::TREE)
7867 : 159 : && dump ("Indirect:%d template's parameter %u %C:%N",
7868 : 159 : tag, ix, TREE_CODE (parms), parms);
7869 : : }
7870 : :
7871 : 374656 : return len;
7872 : : }
7873 : :
7874 : : /* We've just found DECL by name. Insert nodes that come with it, but
7875 : : cannot be found by name, so we'll not accidentally walk into them. */
7876 : :
7877 : : void
7878 : 6426443 : trees_out::add_indirects (tree decl)
7879 : : {
7880 : 6426443 : unsigned count = 0;
7881 : :
7882 : : // FIXME:OPTIMIZATION We'll eventually want default fn parms of
7883 : : // templates and perhaps default template parms too. The former can
7884 : : // be referenced from instantiations (as they are lazily
7885 : : // instantiated). Also (deferred?) exception specifications of
7886 : : // templates. See the note about PARM_DECLs in trees_out::decl_node.
7887 : 6426443 : tree inner = decl;
7888 : 6426443 : if (TREE_CODE (decl) == TEMPLATE_DECL)
7889 : : {
7890 : 2756803 : count += add_indirect_tpl_parms (DECL_TEMPLATE_PARMS (decl));
7891 : :
7892 : 2756803 : inner = DECL_TEMPLATE_RESULT (decl);
7893 : 2756803 : int tag = insert (inner);
7894 : 2756803 : if (streaming_p ())
7895 : 459304 : dump (dumper::TREE)
7896 : 219 : && dump ("Indirect:%d template's result %C:%N",
7897 : 219 : tag, TREE_CODE (inner), inner);
7898 : 2756803 : count++;
7899 : : }
7900 : :
7901 : 6426443 : if (TREE_CODE (inner) == TYPE_DECL)
7902 : : {
7903 : : /* Make sure the type is in the map too. Otherwise we get
7904 : : different RECORD_TYPEs for the same type, and things go
7905 : : south. */
7906 : 3546640 : tree type = TREE_TYPE (inner);
7907 : 3546640 : gcc_checking_assert (DECL_ORIGINAL_TYPE (inner)
7908 : : || TYPE_NAME (type) == inner);
7909 : 3546640 : int tag = insert (type);
7910 : 3546640 : if (streaming_p ())
7911 : 408539 : dump (dumper::TREE) && dump ("Indirect:%d decl's type %C:%N", tag,
7912 : 362 : TREE_CODE (type), type);
7913 : 3546640 : count++;
7914 : : }
7915 : :
7916 : 6426443 : if (streaming_p ())
7917 : : {
7918 : 987649 : u (count);
7919 : 988178 : dump (dumper::TREE) && dump ("Inserted %u indirects", count);
7920 : : }
7921 : 6426443 : }
7922 : :
7923 : : bool
7924 : 778343 : trees_in::add_indirects (tree decl)
7925 : : {
7926 : 778343 : unsigned count = 0;
7927 : :
7928 : 778343 : tree inner = decl;
7929 : 778343 : if (TREE_CODE (inner) == TEMPLATE_DECL)
7930 : : {
7931 : 374656 : count += add_indirect_tpl_parms (DECL_TEMPLATE_PARMS (decl));
7932 : :
7933 : 374656 : inner = DECL_TEMPLATE_RESULT (decl);
7934 : 374656 : int tag = insert (inner);
7935 : 374656 : dump (dumper::TREE)
7936 : 228 : && dump ("Indirect:%d templates's result %C:%N", tag,
7937 : 228 : TREE_CODE (inner), inner);
7938 : 374656 : count++;
7939 : : }
7940 : :
7941 : 778343 : if (TREE_CODE (inner) == TYPE_DECL)
7942 : : {
7943 : 302234 : tree type = TREE_TYPE (inner);
7944 : 302234 : gcc_checking_assert (DECL_ORIGINAL_TYPE (inner)
7945 : : || TYPE_NAME (type) == inner);
7946 : 302234 : int tag = insert (type);
7947 : 302234 : dump (dumper::TREE)
7948 : 362 : && dump ("Indirect:%d decl's type %C:%N", tag, TREE_CODE (type), type);
7949 : 302234 : count++;
7950 : : }
7951 : :
7952 : 778948 : dump (dumper::TREE) && dump ("Inserted %u indirects", count);
7953 : 778343 : return count == u ();
7954 : : }
7955 : :
7956 : : /* Stream a template parameter. There are 4.5 kinds of parameter:
7957 : : a) Template - TEMPLATE_DECL->TYPE_DECL->TEMPLATE_TEMPLATE_PARM
7958 : : TEMPLATE_TYPE_PARM_INDEX TPI
7959 : : b) Type - TYPE_DECL->TEMPLATE_TYPE_PARM TEMPLATE_TYPE_PARM_INDEX TPI
7960 : : c.1) NonTYPE - PARM_DECL DECL_INITIAL TPI We meet this first
7961 : : c.2) NonTYPE - CONST_DECL DECL_INITIAL Same TPI
7962 : : d) BoundTemplate - TYPE_DECL->BOUND_TEMPLATE_TEMPLATE_PARM
7963 : : TEMPLATE_TYPE_PARM_INDEX->TPI
7964 : : TEMPLATE_TEMPLATE_PARM_INFO->TEMPLATE_INFO
7965 : :
7966 : : All of these point to a TEMPLATE_PARM_INDEX, and #B also has a TEMPLATE_INFO
7967 : : */
7968 : :
7969 : : void
7970 : 1622180 : trees_out::tpl_parm_value (tree parm)
7971 : : {
7972 : 1622180 : gcc_checking_assert (DECL_P (parm) && DECL_TEMPLATE_PARM_P (parm));
7973 : :
7974 : 1622180 : int parm_tag = insert (parm);
7975 : 1622180 : if (streaming_p ())
7976 : : {
7977 : 367078 : i (tt_tpl_parm);
7978 : 367078 : dump (dumper::TREE) && dump ("Writing template parm:%d %C:%N",
7979 : 114 : parm_tag, TREE_CODE (parm), parm);
7980 : 367078 : start (parm);
7981 : 367078 : tree_node_bools (parm);
7982 : : }
7983 : :
7984 : 1622180 : tree inner = parm;
7985 : 1622180 : if (TREE_CODE (inner) == TEMPLATE_DECL)
7986 : : {
7987 : 4786 : inner = DECL_TEMPLATE_RESULT (inner);
7988 : 4786 : int inner_tag = insert (inner);
7989 : 4786 : if (streaming_p ())
7990 : : {
7991 : 1234 : dump (dumper::TREE) && dump ("Writing inner template parm:%d %C:%N",
7992 : 0 : inner_tag, TREE_CODE (inner), inner);
7993 : 1234 : start (inner);
7994 : 1234 : tree_node_bools (inner);
7995 : : }
7996 : : }
7997 : :
7998 : 1622180 : tree type = NULL_TREE;
7999 : 1622180 : if (TREE_CODE (inner) == TYPE_DECL)
8000 : : {
8001 : 1461173 : type = TREE_TYPE (inner);
8002 : 1461173 : int type_tag = insert (type);
8003 : 1461173 : if (streaming_p ())
8004 : : {
8005 : 331210 : dump (dumper::TREE) && dump ("Writing template parm type:%d %C:%N",
8006 : 108 : type_tag, TREE_CODE (type), type);
8007 : 331210 : start (type);
8008 : 331210 : tree_node_bools (type);
8009 : : }
8010 : : }
8011 : :
8012 : 1622180 : if (inner != parm)
8013 : : {
8014 : : /* This is a template-template parameter. */
8015 : 4786 : unsigned tpl_levels = 0;
8016 : 4786 : tpl_header (parm, &tpl_levels);
8017 : 4786 : tpl_parms_fini (parm, tpl_levels);
8018 : : }
8019 : :
8020 : 1622180 : tree_node_vals (parm);
8021 : 1622180 : if (inner != parm)
8022 : 4786 : tree_node_vals (inner);
8023 : 1622180 : if (type)
8024 : : {
8025 : 1461173 : tree_node_vals (type);
8026 : 1461173 : if (DECL_NAME (inner) == auto_identifier
8027 : 1461173 : || DECL_NAME (inner) == decltype_auto_identifier)
8028 : : {
8029 : : /* Placeholder auto. */
8030 : 63505 : tree_node (DECL_INITIAL (inner));
8031 : 63505 : tree_node (DECL_SIZE_UNIT (inner));
8032 : : }
8033 : : }
8034 : :
8035 : 1622180 : if (streaming_p ())
8036 : 367078 : dump (dumper::TREE) && dump ("Wrote template parm:%d %C:%N",
8037 : 114 : parm_tag, TREE_CODE (parm), parm);
8038 : 1622180 : }
8039 : :
8040 : : tree
8041 : 298653 : trees_in::tpl_parm_value ()
8042 : : {
8043 : 298653 : tree parm = start ();
8044 : 298653 : if (!parm || !tree_node_bools (parm))
8045 : 0 : return NULL_TREE;
8046 : :
8047 : 298653 : int parm_tag = insert (parm);
8048 : 298653 : dump (dumper::TREE) && dump ("Reading template parm:%d %C:%N",
8049 : 198 : parm_tag, TREE_CODE (parm), parm);
8050 : :
8051 : 298653 : tree inner = parm;
8052 : 298653 : if (TREE_CODE (inner) == TEMPLATE_DECL)
8053 : : {
8054 : 824 : inner = start ();
8055 : 824 : if (!inner || !tree_node_bools (inner))
8056 : 0 : return NULL_TREE;
8057 : 824 : int inner_tag = insert (inner);
8058 : 824 : dump (dumper::TREE) && dump ("Reading inner template parm:%d %C:%N",
8059 : 0 : inner_tag, TREE_CODE (inner), inner);
8060 : 824 : DECL_TEMPLATE_RESULT (parm) = inner;
8061 : : }
8062 : :
8063 : 298653 : tree type = NULL_TREE;
8064 : 298653 : if (TREE_CODE (inner) == TYPE_DECL)
8065 : : {
8066 : 268535 : type = start ();
8067 : 268535 : if (!type || !tree_node_bools (type))
8068 : 0 : return NULL_TREE;
8069 : 268535 : int type_tag = insert (type);
8070 : 268535 : dump (dumper::TREE) && dump ("Reading template parm type:%d %C:%N",
8071 : 120 : type_tag, TREE_CODE (type), type);
8072 : :
8073 : 268535 : TREE_TYPE (inner) = TREE_TYPE (parm) = type;
8074 : 268535 : TYPE_NAME (type) = parm;
8075 : : }
8076 : :
8077 : 298653 : if (inner != parm)
8078 : : {
8079 : : /* A template template parameter. */
8080 : 824 : unsigned tpl_levels = 0;
8081 : 824 : tpl_header (parm, &tpl_levels);
8082 : 824 : tpl_parms_fini (parm, tpl_levels);
8083 : : }
8084 : :
8085 : 298653 : tree_node_vals (parm);
8086 : 298653 : if (inner != parm)
8087 : 824 : tree_node_vals (inner);
8088 : 298653 : if (type)
8089 : : {
8090 : 268535 : tree_node_vals (type);
8091 : 268535 : if (DECL_NAME (inner) == auto_identifier
8092 : 268535 : || DECL_NAME (inner) == decltype_auto_identifier)
8093 : : {
8094 : : /* Placeholder auto. */
8095 : 22851 : DECL_INITIAL (inner) = tree_node ();
8096 : 22851 : DECL_SIZE_UNIT (inner) = tree_node ();
8097 : : }
8098 : 268535 : if (TYPE_CANONICAL (type))
8099 : : {
8100 : 268535 : gcc_checking_assert (TYPE_CANONICAL (type) == type);
8101 : 268535 : TYPE_CANONICAL (type) = canonical_type_parameter (type);
8102 : : }
8103 : : }
8104 : :
8105 : 298653 : dump (dumper::TREE) && dump ("Read template parm:%d %C:%N",
8106 : 198 : parm_tag, TREE_CODE (parm), parm);
8107 : :
8108 : : return parm;
8109 : : }
8110 : :
8111 : : void
8112 : 1057724 : trees_out::install_entity (tree decl, depset *dep)
8113 : : {
8114 : 1057724 : gcc_checking_assert (streaming_p ());
8115 : :
8116 : : /* Write the entity index, so we can insert it as soon as we
8117 : : know this is new. */
8118 : 1057724 : u (dep ? dep->cluster + 1 : 0);
8119 : 1057724 : if (CHECKING_P && dep)
8120 : : {
8121 : : /* Add it to the entity map, such that we can tell it is
8122 : : part of us. */
8123 : 790517 : bool existed;
8124 : 790517 : unsigned *slot = &entity_map->get_or_insert
8125 : 790517 : (DECL_UID (decl), &existed);
8126 : 790517 : if (existed)
8127 : : /* If it existed, it should match. */
8128 : 818 : gcc_checking_assert (decl == (*entity_ary)[*slot]);
8129 : 790517 : *slot = ~dep->cluster;
8130 : : }
8131 : 1057724 : }
8132 : :
8133 : : bool
8134 : 867779 : trees_in::install_entity (tree decl)
8135 : : {
8136 : 867779 : unsigned entity_index = u ();
8137 : 867779 : if (!entity_index)
8138 : : return false;
8139 : :
8140 : 634534 : if (entity_index > state->entity_num)
8141 : : {
8142 : 0 : set_overrun ();
8143 : 0 : return false;
8144 : : }
8145 : :
8146 : : /* Insert the real decl into the entity ary. */
8147 : 634534 : unsigned ident = state->entity_lwm + entity_index - 1;
8148 : 634534 : (*entity_ary)[ident] = decl;
8149 : :
8150 : : /* And into the entity map, if it's not already there. */
8151 : 634534 : tree not_tmpl = STRIP_TEMPLATE (decl);
8152 : 634534 : if (!DECL_LANG_SPECIFIC (not_tmpl)
8153 : 1184279 : || !DECL_MODULE_ENTITY_P (not_tmpl))
8154 : : {
8155 : : /* We don't want to use retrofit_lang_decl directly so that we aren't
8156 : : affected by the language state when we load in. */
8157 : 633914 : if (!DECL_LANG_SPECIFIC (not_tmpl))
8158 : : {
8159 : 84789 : maybe_add_lang_decl_raw (not_tmpl, false);
8160 : 84789 : SET_DECL_LANGUAGE (not_tmpl, lang_cplusplus);
8161 : : }
8162 : 633914 : DECL_MODULE_ENTITY_P (not_tmpl) = true;
8163 : :
8164 : : /* Insert into the entity hash (it cannot already be there). */
8165 : 633914 : bool existed;
8166 : 633914 : unsigned &slot = entity_map->get_or_insert (DECL_UID (decl), &existed);
8167 : 633914 : gcc_checking_assert (!existed);
8168 : 633914 : slot = ident;
8169 : : }
8170 : : else
8171 : : {
8172 : 620 : unsigned *slot = entity_map->get (DECL_UID (decl));
8173 : :
8174 : : /* The entity must be in the entity map already. However, DECL may
8175 : : be the DECL_TEMPLATE_RESULT of an existing partial specialisation
8176 : : if we matched it while streaming another instantiation; in this
8177 : : case we already registered that TEMPLATE_DECL. */
8178 : 620 : if (!slot)
8179 : : {
8180 : 9 : tree type = TREE_TYPE (decl);
8181 : 9 : gcc_checking_assert (TREE_CODE (decl) == TYPE_DECL
8182 : : && CLASS_TYPE_P (type)
8183 : : && CLASSTYPE_TEMPLATE_SPECIALIZATION (type));
8184 : 9 : slot = entity_map->get (DECL_UID (CLASSTYPE_TI_TEMPLATE (type)));
8185 : : }
8186 : 9 : gcc_checking_assert (slot);
8187 : :
8188 : 620 : if (state->is_partition ())
8189 : : {
8190 : : /* The decl is already in the entity map, but we see it again now
8191 : : from a partition: we want to overwrite if the original decl
8192 : : wasn't also from a (possibly different) partition. Otherwise,
8193 : : for things like template instantiations, make_dependency might
8194 : : not realise that this is also provided from a partition and
8195 : : should be considered part of this module (and thus always
8196 : : emitted into the primary interface's CMI). */
8197 : 150 : module_state *imp = import_entity_module (*slot);
8198 : 150 : if (!imp->is_partition ())
8199 : 24 : *slot = ident;
8200 : : }
8201 : : }
8202 : :
8203 : : return true;
8204 : : }
8205 : :
8206 : : static bool has_definition (tree decl);
8207 : :
8208 : : /* DECL is a decl node that must be written by value. DEP is the
8209 : : decl's depset. */
8210 : :
8211 : : void
8212 : 2917032 : trees_out::decl_value (tree decl, depset *dep)
8213 : : {
8214 : : /* We should not be writing clones or template parms. */
8215 : 2917032 : gcc_checking_assert (DECL_P (decl)
8216 : : && !DECL_CLONED_FUNCTION_P (decl)
8217 : : && !DECL_TEMPLATE_PARM_P (decl));
8218 : :
8219 : : /* We should never be writing non-typedef ptrmemfuncs by value. */
8220 : 2917032 : gcc_checking_assert (TREE_CODE (decl) != TYPE_DECL
8221 : : || DECL_ORIGINAL_TYPE (decl)
8222 : : || !TYPE_PTRMEMFUNC_P (TREE_TYPE (decl)));
8223 : :
8224 : : /* There's no need to walk any of the contents of a known TU-local entity,
8225 : : since importers should never see any of it regardless. But make sure we
8226 : : at least note its location so importers can use it for diagnostics. */
8227 : 5289388 : if (dep && dep->is_tu_local ())
8228 : : {
8229 : 413 : gcc_checking_assert (is_initial_scan ());
8230 : 413 : insert (decl, WK_value);
8231 : 413 : state->note_location (DECL_SOURCE_LOCATION (decl));
8232 : 413 : return;
8233 : : }
8234 : :
8235 : 2916619 : merge_kind mk = get_merge_kind (decl, dep);
8236 : :
8237 : 2916619 : if (CHECKING_P)
8238 : : {
8239 : : /* Never start in the middle of a template. */
8240 : 2916619 : int use_tpl = -1;
8241 : 2916619 : if (tree ti = node_template_info (decl, use_tpl))
8242 : 990995 : gcc_checking_assert (TREE_CODE (TI_TEMPLATE (ti)) == OVERLOAD
8243 : : || TREE_CODE (TI_TEMPLATE (ti)) == FIELD_DECL
8244 : : || (DECL_TEMPLATE_RESULT (TI_TEMPLATE (ti))
8245 : : != decl));
8246 : : }
8247 : :
8248 : 2916619 : if (streaming_p ())
8249 : : {
8250 : : /* A new node -> tt_decl. */
8251 : 1057724 : decl_val_count++;
8252 : 1057724 : i (tt_decl);
8253 : 1057724 : u (mk);
8254 : 1057724 : start (decl);
8255 : :
8256 : 1057724 : if (mk != MK_unique)
8257 : : {
8258 : 899596 : bits_out bits = stream_bits ();
8259 : 899596 : if (!(mk & MK_template_mask) && !state->is_header ())
8260 : : {
8261 : : /* Tell the importer whether this is a global module entity,
8262 : : or a module entity. */
8263 : 115542 : tree o = get_originating_module_decl (decl);
8264 : 115542 : bool is_attached = false;
8265 : :
8266 : 115542 : tree not_tmpl = STRIP_TEMPLATE (o);
8267 : 115542 : if (DECL_LANG_SPECIFIC (not_tmpl)
8268 : 175813 : && DECL_MODULE_ATTACH_P (not_tmpl))
8269 : : is_attached = true;
8270 : :
8271 : 115542 : bits.b (is_attached);
8272 : : }
8273 : 899596 : bits.b (dep && dep->has_defn ());
8274 : 899596 : }
8275 : 1057724 : tree_node_bools (decl);
8276 : : }
8277 : :
8278 : 2916619 : int tag = insert (decl, WK_value);
8279 : 2916619 : if (streaming_p ())
8280 : 1057724 : dump (dumper::TREE)
8281 : 683 : && dump ("Writing %s:%d %C:%N%S", merge_kind_name[mk], tag,
8282 : 683 : TREE_CODE (decl), decl, decl);
8283 : :
8284 : 2916619 : tree inner = decl;
8285 : 2916619 : int inner_tag = 0;
8286 : 2916619 : if (TREE_CODE (decl) == TEMPLATE_DECL)
8287 : : {
8288 : 833833 : inner = DECL_TEMPLATE_RESULT (decl);
8289 : 833833 : inner_tag = insert (inner, WK_value);
8290 : :
8291 : : /* On stream-in we assume that a template and its result will
8292 : : have the same type. */
8293 : 833833 : gcc_checking_assert (TREE_TYPE (decl) == TREE_TYPE (inner));
8294 : :
8295 : 833833 : if (streaming_p ())
8296 : : {
8297 : 277928 : int code = TREE_CODE (inner);
8298 : 277928 : u (code);
8299 : 277928 : start (inner, true);
8300 : 277928 : tree_node_bools (inner);
8301 : 277928 : dump (dumper::TREE)
8302 : 132 : && dump ("Writing %s:%d %C:%N%S", merge_kind_name[mk], inner_tag,
8303 : 132 : TREE_CODE (inner), inner, inner);
8304 : : }
8305 : : }
8306 : :
8307 : 2916619 : tree type = NULL_TREE;
8308 : 2916619 : int type_tag = 0;
8309 : 2916619 : tree stub_decl = NULL_TREE;
8310 : 2916619 : int stub_tag = 0;
8311 : 2916619 : if (TREE_CODE (inner) == TYPE_DECL)
8312 : : {
8313 : 1103236 : type = TREE_TYPE (inner);
8314 : 1103236 : bool has_type = (type == TYPE_MAIN_VARIANT (type)
8315 : 1103236 : && TYPE_NAME (type) == inner);
8316 : :
8317 : 1103236 : if (streaming_p ())
8318 : 371751 : u (has_type ? TREE_CODE (type) : 0);
8319 : :
8320 : 1103236 : if (has_type)
8321 : : {
8322 : 507431 : type_tag = insert (type, WK_value);
8323 : 507431 : if (streaming_p ())
8324 : : {
8325 : 169123 : start (type, true);
8326 : 169123 : tree_node_bools (type);
8327 : 169123 : dump (dumper::TREE)
8328 : 155 : && dump ("Writing type:%d %C:%N", type_tag,
8329 : 155 : TREE_CODE (type), type);
8330 : : }
8331 : :
8332 : 507431 : stub_decl = TYPE_STUB_DECL (type);
8333 : 507431 : bool has_stub = inner != stub_decl;
8334 : 507431 : if (streaming_p ())
8335 : 169123 : u (has_stub ? TREE_CODE (stub_decl) : 0);
8336 : 507431 : if (has_stub)
8337 : : {
8338 : 2169 : stub_tag = insert (stub_decl);
8339 : 2169 : if (streaming_p ())
8340 : : {
8341 : 722 : start (stub_decl, true);
8342 : 722 : tree_node_bools (stub_decl);
8343 : 722 : dump (dumper::TREE)
8344 : 0 : && dump ("Writing stub_decl:%d %C:%N", stub_tag,
8345 : 0 : TREE_CODE (stub_decl), stub_decl);
8346 : : }
8347 : : }
8348 : : else
8349 : : stub_decl = NULL_TREE;
8350 : : }
8351 : : else
8352 : : /* Regular typedef. */
8353 : : type = NULL_TREE;
8354 : : }
8355 : :
8356 : : /* Stream the container, we want it correctly canonicalized before
8357 : : we start emitting keys for this decl. */
8358 : 2916619 : tree container = decl_container (decl);
8359 : 2916619 : unsigned tpl_levels = 0;
8360 : :
8361 : : /* Also tell the importer whether this is a temploid friend attached
8362 : : to a different module (which has implications for merging), so that
8363 : : importers can reconstruct this information on stream-in. */
8364 : 2916619 : if (TREE_CODE (inner) == FUNCTION_DECL || TREE_CODE (inner) == TYPE_DECL)
8365 : : {
8366 : 2238355 : tree* temploid_friend_slot = imported_temploid_friends->get (decl);
8367 : 2238355 : gcc_checking_assert (!temploid_friend_slot || *temploid_friend_slot);
8368 : 2238355 : tree_node (temploid_friend_slot ? *temploid_friend_slot : NULL_TREE);
8369 : : }
8370 : :
8371 : 2916619 : {
8372 : 2916619 : auto wmk = make_temp_override (dep_hash->writing_merge_key, true);
8373 : 2916619 : if (decl != inner)
8374 : 833833 : tpl_header (decl, &tpl_levels);
8375 : 2916619 : if (TREE_CODE (inner) == FUNCTION_DECL)
8376 : 1135119 : fn_parms_init (inner);
8377 : :
8378 : : /* Now write out the merging information, and then really
8379 : : install the tag values. */
8380 : 2916619 : key_mergeable (tag, mk, decl, inner, container, dep);
8381 : :
8382 : 2916619 : if (streaming_p ())
8383 : 2919381 : dump (dumper::MERGE)
8384 : 798 : && dump ("Wrote:%d's %s merge key %C:%N", tag,
8385 : 798 : merge_kind_name[mk], TREE_CODE (decl), decl);
8386 : 2916619 : }
8387 : :
8388 : 2916619 : if (TREE_CODE (inner) == FUNCTION_DECL)
8389 : 2916619 : fn_parms_fini (inner);
8390 : :
8391 : 2916619 : if (!is_key_order ())
8392 : 2124954 : tree_node_vals (decl);
8393 : :
8394 : 2916619 : if (inner_tag)
8395 : : {
8396 : 833833 : if (!is_key_order ())
8397 : 555905 : tree_node_vals (inner);
8398 : 833833 : tpl_parms_fini (decl, tpl_levels);
8399 : : }
8400 : :
8401 : 2916619 : if (type && !is_key_order ())
8402 : : {
8403 : 338308 : tree_node_vals (type);
8404 : 338308 : if (stub_decl)
8405 : 1447 : tree_node_vals (stub_decl);
8406 : : }
8407 : :
8408 : 2916619 : if (!is_key_order ())
8409 : : {
8410 : 2124954 : if (mk & MK_template_mask
8411 : 1496786 : || mk == MK_partial
8412 : 1496786 : || mk == MK_friend_spec)
8413 : : {
8414 : 28556 : if (mk != MK_partial)
8415 : : {
8416 : : // FIXME: We should make use of the merge-key by
8417 : : // exposing it outside of key_mergeable. But this gets
8418 : : // the job done.
8419 : 628168 : auto *entry = reinterpret_cast <spec_entry *> (dep->deps[0]);
8420 : :
8421 : 628168 : if (streaming_p ())
8422 : 314084 : u (get_mergeable_specialization_flags (mk & MK_tmpl_decl_mask,
8423 : : entry->tmpl, decl));
8424 : 628168 : tree_node (entry->tmpl);
8425 : 628168 : tree_node (entry->args);
8426 : : }
8427 : : else
8428 : : {
8429 : 28556 : tree ti = get_template_info (inner);
8430 : 28556 : tree_node (TI_TEMPLATE (ti));
8431 : 28556 : tree_node (TI_ARGS (ti));
8432 : : }
8433 : : }
8434 : 2124954 : tree_node (get_constraints (decl));
8435 : : }
8436 : :
8437 : 2916619 : if (streaming_p ())
8438 : : {
8439 : : /* Do not stray outside this section. */
8440 : 1057724 : gcc_checking_assert (!dep || dep->section == dep_hash->section);
8441 : :
8442 : : /* Write the entity index, so we can insert it as soon as we
8443 : : know this is new. */
8444 : 1057724 : install_entity (decl, dep);
8445 : : }
8446 : :
8447 : 2916619 : if (DECL_LANG_SPECIFIC (inner)
8448 : 2636419 : && DECL_MODULE_KEYED_DECLS_P (inner)
8449 : 2917028 : && !is_key_order ())
8450 : : {
8451 : : /* Stream the keyed entities. */
8452 : 276 : auto *attach_vec = keyed_table->get (inner);
8453 : 276 : unsigned num = attach_vec->length ();
8454 : 276 : if (streaming_p ())
8455 : 133 : u (num);
8456 : 564 : for (unsigned ix = 0; ix != num; ix++)
8457 : : {
8458 : 288 : tree attached = (*attach_vec)[ix];
8459 : 288 : tree_node (attached);
8460 : 288 : if (streaming_p ())
8461 : 303 : dump (dumper::MERGE)
8462 : 15 : && dump ("Written %d[%u] attached decl %N", tag, ix, attached);
8463 : : }
8464 : : }
8465 : :
8466 : 2916619 : bool is_typedef = false;
8467 : 2916619 : if (!type && TREE_CODE (inner) == TYPE_DECL)
8468 : : {
8469 : 595805 : tree t = TREE_TYPE (inner);
8470 : 595805 : unsigned tdef_flags = 0;
8471 : 595805 : if (DECL_ORIGINAL_TYPE (inner)
8472 : 595805 : && TYPE_NAME (TREE_TYPE (inner)) == inner)
8473 : : {
8474 : 595805 : tdef_flags |= 1;
8475 : 595805 : if (TYPE_STRUCTURAL_EQUALITY_P (t)
8476 : 129231 : && TYPE_DEPENDENT_P_VALID (t)
8477 : 718508 : && TYPE_DEPENDENT_P (t))
8478 : : tdef_flags |= 2;
8479 : : }
8480 : 595805 : if (streaming_p ())
8481 : 202628 : u (tdef_flags);
8482 : :
8483 : 595805 : if (tdef_flags & 1)
8484 : : {
8485 : : /* A typedef type. */
8486 : 595805 : int type_tag = insert (t);
8487 : 595805 : if (streaming_p ())
8488 : 202628 : dump (dumper::TREE)
8489 : 206 : && dump ("Cloned:%d %s %C:%N", type_tag,
8490 : : tdef_flags & 2 ? "depalias" : "typedef",
8491 : 206 : TREE_CODE (t), t);
8492 : :
8493 : : is_typedef = true;
8494 : : }
8495 : : }
8496 : :
8497 : 2916619 : if (streaming_p () && DECL_MAYBE_IN_CHARGE_CDTOR_P (decl))
8498 : : {
8499 : 79172 : bool cloned_p
8500 : 79172 : = (DECL_CHAIN (decl) && DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl)));
8501 : 55484 : bool needs_vtt_parm_p
8502 : 55484 : = (cloned_p && CLASSTYPE_VBASECLASSES (DECL_CONTEXT (decl)));
8503 : 55484 : bool omit_inherited_parms_p
8504 : 55484 : = (cloned_p && DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl)
8505 : 43830 : && base_ctor_omit_inherited_parms (decl));
8506 : 79172 : unsigned flags = (int (cloned_p) << 0
8507 : 79172 : | int (needs_vtt_parm_p) << 1
8508 : 79172 : | int (omit_inherited_parms_p) << 2);
8509 : 79172 : u (flags);
8510 : 79251 : dump (dumper::TREE) && dump ("CDTOR %N is %scloned",
8511 : : decl, cloned_p ? "" : "not ");
8512 : : }
8513 : :
8514 : 2916619 : if (streaming_p () && VAR_P (decl) && CP_DECL_THREAD_LOCAL_P (decl))
8515 : 172 : u (decl_tls_model (decl));
8516 : :
8517 : 2916619 : if (streaming_p ())
8518 : 1057724 : dump (dumper::TREE) && dump ("Written decl:%d %C:%N", tag,
8519 : 683 : TREE_CODE (decl), decl);
8520 : :
8521 : 2916619 : if (NAMESPACE_SCOPE_P (inner))
8522 : 1752794 : gcc_checking_assert (!dep == (VAR_OR_FUNCTION_DECL_P (inner)
8523 : : && DECL_LOCAL_DECL_P (inner)));
8524 : 2040104 : else if ((TREE_CODE (inner) == TYPE_DECL
8525 : 586804 : && !is_typedef
8526 : 114293 : && TYPE_NAME (TREE_TYPE (inner)) == inner)
8527 : 2512615 : || TREE_CODE (inner) == FUNCTION_DECL)
8528 : : {
8529 : 945699 : bool write_defn = !dep && has_definition (decl);
8530 : 945699 : if (streaming_p ())
8531 : 315376 : u (write_defn);
8532 : 945699 : if (write_defn)
8533 : 0 : write_definition (decl);
8534 : : }
8535 : : }
8536 : :
8537 : : tree
8538 : 867779 : trees_in::decl_value ()
8539 : : {
8540 : 867779 : int tag = 0;
8541 : 867779 : bool is_attached = false;
8542 : 867779 : bool has_defn = false;
8543 : 867779 : unsigned mk_u = u ();
8544 : 867779 : if (mk_u >= MK_hwm || !merge_kind_name[mk_u])
8545 : : {
8546 : 0 : set_overrun ();
8547 : 0 : return NULL_TREE;
8548 : : }
8549 : :
8550 : 867779 : unsigned saved_unused = unused;
8551 : 867779 : unused = 0;
8552 : :
8553 : 867779 : merge_kind mk = merge_kind (mk_u);
8554 : :
8555 : 867779 : tree decl = start ();
8556 : 867779 : if (decl)
8557 : : {
8558 : 867779 : if (mk != MK_unique)
8559 : : {
8560 : 724469 : bits_in bits = stream_bits ();
8561 : 724469 : if (!(mk & MK_template_mask) && !state->is_header ())
8562 : 47101 : is_attached = bits.b ();
8563 : :
8564 : 724469 : has_defn = bits.b ();
8565 : 724469 : }
8566 : :
8567 : 867779 : if (!tree_node_bools (decl))
8568 : 0 : decl = NULL_TREE;
8569 : : }
8570 : :
8571 : : /* Insert into map. */
8572 : 867779 : tag = insert (decl);
8573 : 867779 : if (decl)
8574 : 867779 : dump (dumper::TREE)
8575 : 964 : && dump ("Reading:%d %C", tag, TREE_CODE (decl));
8576 : :
8577 : 867779 : tree inner = decl;
8578 : 867779 : int inner_tag = 0;
8579 : 867779 : if (decl && TREE_CODE (decl) == TEMPLATE_DECL)
8580 : : {
8581 : 231536 : int code = u ();
8582 : 231536 : inner = start (code);
8583 : 231536 : if (inner && tree_node_bools (inner))
8584 : 231536 : DECL_TEMPLATE_RESULT (decl) = inner;
8585 : : else
8586 : 0 : decl = NULL_TREE;
8587 : :
8588 : 231536 : inner_tag = insert (inner);
8589 : 231536 : if (decl)
8590 : 231536 : dump (dumper::TREE)
8591 : 204 : && dump ("Reading:%d %C", inner_tag, TREE_CODE (inner));
8592 : : }
8593 : :
8594 : 867779 : tree type = NULL_TREE;
8595 : 867779 : int type_tag = 0;
8596 : 867779 : tree stub_decl = NULL_TREE;
8597 : 867779 : int stub_tag = 0;
8598 : 867779 : if (decl && TREE_CODE (inner) == TYPE_DECL)
8599 : : {
8600 : 286716 : if (unsigned type_code = u ())
8601 : : {
8602 : 129797 : type = start (type_code);
8603 : 129797 : if (type && tree_node_bools (type))
8604 : : {
8605 : 129797 : TREE_TYPE (inner) = type;
8606 : 129797 : TYPE_NAME (type) = inner;
8607 : : }
8608 : : else
8609 : 0 : decl = NULL_TREE;
8610 : :
8611 : 129797 : type_tag = insert (type);
8612 : 129797 : if (decl)
8613 : 129797 : dump (dumper::TREE)
8614 : 212 : && dump ("Reading type:%d %C", type_tag, TREE_CODE (type));
8615 : :
8616 : 129797 : if (unsigned stub_code = u ())
8617 : : {
8618 : 413 : stub_decl = start (stub_code);
8619 : 413 : if (stub_decl && tree_node_bools (stub_decl))
8620 : : {
8621 : 413 : TREE_TYPE (stub_decl) = type;
8622 : 413 : TYPE_STUB_DECL (type) = stub_decl;
8623 : : }
8624 : : else
8625 : 0 : decl = NULL_TREE;
8626 : :
8627 : 413 : stub_tag = insert (stub_decl);
8628 : 413 : if (decl)
8629 : 413 : dump (dumper::TREE)
8630 : 0 : && dump ("Reading stub_decl:%d %C", stub_tag,
8631 : 0 : TREE_CODE (stub_decl));
8632 : : }
8633 : : }
8634 : : }
8635 : :
8636 : 867779 : if (!decl)
8637 : : {
8638 : 0 : bail:
8639 : 0 : if (inner_tag != 0)
8640 : 0 : back_refs[~inner_tag] = NULL_TREE;
8641 : 0 : if (type_tag != 0)
8642 : 0 : back_refs[~type_tag] = NULL_TREE;
8643 : 0 : if (stub_tag != 0)
8644 : 0 : back_refs[~stub_tag] = NULL_TREE;
8645 : 0 : if (tag != 0)
8646 : 0 : back_refs[~tag] = NULL_TREE;
8647 : 0 : set_overrun ();
8648 : : /* Bail. */
8649 : 0 : unused = saved_unused;
8650 : 0 : return NULL_TREE;
8651 : : }
8652 : :
8653 : : /* Read the container, to ensure it's already been streamed in. */
8654 : 867779 : tree container = decl_container ();
8655 : 867779 : unsigned tpl_levels = 0;
8656 : :
8657 : : /* If this is an imported temploid friend, get the owning decl its
8658 : : attachment is determined by (or NULL_TREE otherwise). */
8659 : 867779 : tree temploid_friend = NULL_TREE;
8660 : 867779 : if (TREE_CODE (inner) == FUNCTION_DECL || TREE_CODE (inner) == TYPE_DECL)
8661 : 611513 : temploid_friend = tree_node ();
8662 : :
8663 : : /* Figure out if this decl is already known about. */
8664 : 867779 : int parm_tag = 0;
8665 : :
8666 : 867779 : if (decl != inner)
8667 : 231536 : if (!tpl_header (decl, &tpl_levels))
8668 : 0 : goto bail;
8669 : 867779 : if (TREE_CODE (inner) == FUNCTION_DECL)
8670 : 324797 : parm_tag = fn_parms_init (inner);
8671 : :
8672 : 867779 : tree existing = key_mergeable (tag, mk, decl, inner, type, container,
8673 : : is_attached, temploid_friend);
8674 : 867779 : tree existing_inner = existing;
8675 : 867779 : if (existing)
8676 : : {
8677 : 417795 : if (existing == error_mark_node)
8678 : 0 : goto bail;
8679 : :
8680 : 417795 : if (TREE_CODE (STRIP_TEMPLATE (existing)) == TYPE_DECL)
8681 : : {
8682 : 156926 : tree etype = TREE_TYPE (existing);
8683 : 156926 : if (TYPE_LANG_SPECIFIC (etype)
8684 : 113287 : && COMPLETE_TYPE_P (etype)
8685 : 222849 : && !CLASSTYPE_MEMBER_VEC (etype))
8686 : : /* Give it a member vec, we're likely gonna be looking
8687 : : inside it. */
8688 : 13647 : set_class_bindings (etype, -1);
8689 : : }
8690 : :
8691 : : /* Install the existing decl into the back ref array. */
8692 : 417795 : register_duplicate (decl, existing);
8693 : 417795 : back_refs[~tag] = existing;
8694 : 417795 : if (inner_tag != 0)
8695 : : {
8696 : 140894 : existing_inner = DECL_TEMPLATE_RESULT (existing);
8697 : 140894 : back_refs[~inner_tag] = existing_inner;
8698 : : }
8699 : :
8700 : 417795 : if (type_tag != 0)
8701 : : {
8702 : 74922 : tree existing_type = TREE_TYPE (existing);
8703 : 74922 : back_refs[~type_tag] = existing_type;
8704 : 74922 : if (stub_tag != 0)
8705 : 254 : back_refs[~stub_tag] = TYPE_STUB_DECL (existing_type);
8706 : : }
8707 : : }
8708 : :
8709 : 867779 : if (parm_tag)
8710 : 324797 : fn_parms_fini (parm_tag, inner, existing_inner, has_defn);
8711 : :
8712 : 867779 : if (!tree_node_vals (decl))
8713 : 0 : goto bail;
8714 : :
8715 : 867779 : if (inner_tag)
8716 : : {
8717 : 231536 : gcc_checking_assert (DECL_TEMPLATE_RESULT (decl) == inner);
8718 : :
8719 : 231536 : if (!tree_node_vals (inner))
8720 : 0 : goto bail;
8721 : :
8722 : 231536 : if (!tpl_parms_fini (decl, tpl_levels))
8723 : 0 : goto bail;
8724 : : }
8725 : :
8726 : 867779 : if (type && (!tree_node_vals (type)
8727 : 129797 : || (stub_decl && !tree_node_vals (stub_decl))))
8728 : 0 : goto bail;
8729 : :
8730 : 867779 : spec_entry spec;
8731 : 867779 : unsigned spec_flags = 0;
8732 : 867779 : if (mk & MK_template_mask
8733 : 605930 : || mk == MK_partial
8734 : 605930 : || mk == MK_friend_spec)
8735 : : {
8736 : 9469 : if (mk == MK_partial)
8737 : : spec_flags = 2;
8738 : : else
8739 : 261849 : spec_flags = u ();
8740 : :
8741 : 271318 : spec.tmpl = tree_node ();
8742 : 271318 : spec.args = tree_node ();
8743 : : }
8744 : : /* Hold constraints on the spec field, for a short while. */
8745 : 867779 : spec.spec = tree_node ();
8746 : :
8747 : 868743 : dump (dumper::TREE) && dump ("Read:%d %C:%N", tag, TREE_CODE (decl), decl);
8748 : :
8749 : 867779 : existing = back_refs[~tag];
8750 : 867779 : bool installed = install_entity (existing);
8751 : 867779 : bool is_new = existing == decl;
8752 : :
8753 : 867779 : if (DECL_LANG_SPECIFIC (inner)
8754 : 1634733 : && DECL_MODULE_KEYED_DECLS_P (inner))
8755 : : {
8756 : : /* Read and maybe install the attached entities. */
8757 : 158 : bool existed;
8758 : 158 : auto &set = keyed_table->get_or_insert (STRIP_TEMPLATE (existing),
8759 : : &existed);
8760 : 158 : unsigned num = u ();
8761 : 158 : if (is_new == existed)
8762 : 0 : set_overrun ();
8763 : 158 : if (is_new)
8764 : 56 : set.reserve (num);
8765 : 328 : for (unsigned ix = 0; !get_overrun () && ix != num; ix++)
8766 : : {
8767 : 170 : tree attached = tree_node ();
8768 : 170 : dump (dumper::MERGE)
8769 : 60 : && dump ("Read %d[%u] %s attached decl %N", tag, ix,
8770 : : is_new ? "new" : "matched", attached);
8771 : 170 : if (is_new)
8772 : 62 : set.quick_push (attached);
8773 : 108 : else if (set[ix] != attached)
8774 : 0 : set_overrun ();
8775 : : }
8776 : : }
8777 : :
8778 : : /* Regular typedefs will have a NULL TREE_TYPE at this point. */
8779 : 867779 : unsigned tdef_flags = 0;
8780 : 867779 : bool is_typedef = false;
8781 : 867779 : if (!type && TREE_CODE (inner) == TYPE_DECL)
8782 : : {
8783 : 156919 : tdef_flags = u ();
8784 : 156919 : if (tdef_flags & 1)
8785 : 156919 : is_typedef = true;
8786 : : }
8787 : :
8788 : 867779 : if (is_new)
8789 : : {
8790 : : /* A newly discovered node. */
8791 : 449984 : if (TREE_CODE (decl) == FUNCTION_DECL && DECL_VIRTUAL_P (decl))
8792 : : /* Mark this identifier as naming a virtual function --
8793 : : lookup_overrides relies on this optimization. */
8794 : 4084 : IDENTIFIER_VIRTUAL_P (DECL_NAME (decl)) = true;
8795 : :
8796 : 449984 : if (installed)
8797 : : {
8798 : : /* Mark the entity as imported. */
8799 : 268265 : retrofit_lang_decl (inner);
8800 : 268265 : DECL_MODULE_IMPORT_P (inner) = true;
8801 : : }
8802 : :
8803 : 449984 : if (temploid_friend)
8804 : 22 : imported_temploid_friends->put (decl, temploid_friend);
8805 : :
8806 : 449984 : if (spec.spec)
8807 : 16776 : set_constraints (decl, spec.spec);
8808 : :
8809 : 449984 : if (TREE_CODE (decl) == INTEGER_CST && !TREE_OVERFLOW (decl))
8810 : : {
8811 : 0 : decl = cache_integer_cst (decl, true);
8812 : 0 : back_refs[~tag] = decl;
8813 : : }
8814 : :
8815 : 449984 : if (is_typedef)
8816 : : {
8817 : : /* Frob it to be ready for cloning. */
8818 : 74915 : TREE_TYPE (inner) = DECL_ORIGINAL_TYPE (inner);
8819 : 74915 : DECL_ORIGINAL_TYPE (inner) = NULL_TREE;
8820 : 74915 : if (TREE_CODE (TREE_TYPE (inner)) != TU_LOCAL_ENTITY)
8821 : : {
8822 : 74912 : set_underlying_type (inner);
8823 : 74912 : if (tdef_flags & 2)
8824 : : {
8825 : : /* Match instantiate_alias_template's handling. */
8826 : 18040 : tree type = TREE_TYPE (inner);
8827 : 18040 : TYPE_DEPENDENT_P (type) = true;
8828 : 18040 : TYPE_DEPENDENT_P_VALID (type) = true;
8829 : 18040 : SET_TYPE_STRUCTURAL_EQUALITY (type);
8830 : : }
8831 : : }
8832 : : }
8833 : :
8834 : 449984 : if (inner_tag)
8835 : : /* Set the TEMPLATE_DECL's type. */
8836 : 90642 : TREE_TYPE (decl) = TREE_TYPE (inner);
8837 : :
8838 : : /* Redetermine whether we need to import or export this declaration
8839 : : for this TU. But for extern templates we know we must import:
8840 : : they'll be defined in a different TU.
8841 : : FIXME: How do dllexport and dllimport interact across a module?
8842 : : See also https://github.com/itanium-cxx-abi/cxx-abi/issues/170.
8843 : : May have to revisit? */
8844 : 449984 : if (type
8845 : 54875 : && CLASS_TYPE_P (type)
8846 : 46295 : && TYPE_LANG_SPECIFIC (type)
8847 : 496279 : && !(CLASSTYPE_EXPLICIT_INSTANTIATION (type)
8848 : 523 : && CLASSTYPE_INTERFACE_KNOWN (type)
8849 : 523 : && CLASSTYPE_INTERFACE_ONLY (type)))
8850 : : {
8851 : 45821 : CLASSTYPE_INTERFACE_ONLY (type) = false;
8852 : 45821 : CLASSTYPE_INTERFACE_UNKNOWN (type) = true;
8853 : : }
8854 : :
8855 : : /* Add to specialization tables now that constraints etc are
8856 : : added. */
8857 : 449984 : if (mk == MK_partial)
8858 : : {
8859 : 3448 : bool is_type = TREE_CODE (inner) == TYPE_DECL;
8860 : 3448 : spec.spec = is_type ? type : inner;
8861 : 3448 : add_mergeable_specialization (!is_type, &spec, decl, spec_flags);
8862 : : }
8863 : 446536 : else if (mk & MK_template_mask)
8864 : : {
8865 : 117945 : bool is_type = !(mk & MK_tmpl_decl_mask);
8866 : 117945 : spec.spec = is_type ? type : mk & MK_tmpl_tmpl_mask ? inner : decl;
8867 : 117945 : add_mergeable_specialization (!is_type, &spec, decl, spec_flags);
8868 : : }
8869 : :
8870 : 449984 : if (NAMESPACE_SCOPE_P (decl)
8871 : 93975 : && (mk == MK_named || mk == MK_unique
8872 : 93975 : || mk == MK_enum || mk == MK_friend_spec)
8873 : 495465 : && !(VAR_OR_FUNCTION_DECL_P (decl) && DECL_LOCAL_DECL_P (decl)))
8874 : 45406 : add_module_namespace_decl (CP_DECL_CONTEXT (decl), decl);
8875 : :
8876 : 449984 : if (DECL_ARTIFICIAL (decl)
8877 : 127019 : && TREE_CODE (decl) == FUNCTION_DECL
8878 : 12558 : && !DECL_TEMPLATE_INFO (decl)
8879 : 12314 : && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
8880 : 12177 : && TYPE_SIZE (DECL_CONTEXT (decl))
8881 : 451343 : && !DECL_THUNK_P (decl))
8882 : : /* A new implicit member function, when the class is
8883 : : complete. This means the importee declared it, and
8884 : : we must now add it to the class. Note that implicit
8885 : : member fns of template instantiations do not themselves
8886 : : look like templates. */
8887 : 869 : if (!install_implicit_member (inner))
8888 : 0 : set_overrun ();
8889 : :
8890 : : /* When importing a TLS wrapper from a header unit, we haven't
8891 : : actually emitted its definition yet. Remember it so we can
8892 : : do this later. */
8893 : 449984 : if (state->is_header ()
8894 : 449984 : && decl_tls_wrapper_p (decl))
8895 : 6 : note_vague_linkage_fn (decl);
8896 : :
8897 : : /* Setup aliases for the declaration. */
8898 : 449984 : if (tree alias = lookup_attribute ("alias", DECL_ATTRIBUTES (decl)))
8899 : : {
8900 : 3 : alias = TREE_VALUE (TREE_VALUE (alias));
8901 : 3 : alias = get_identifier (TREE_STRING_POINTER (alias));
8902 : 3 : assemble_alias (decl, alias);
8903 : : }
8904 : : }
8905 : : else
8906 : : {
8907 : : /* DECL is the to-be-discarded decl. Its internal pointers will
8908 : : be to the EXISTING's structure. Frob it to point to its
8909 : : own other structures, so loading its definition will alter
8910 : : it, and not the existing decl. */
8911 : 419230 : dump (dumper::MERGE) && dump ("Deduping %N", existing);
8912 : :
8913 : 417795 : if (inner_tag)
8914 : 140894 : DECL_TEMPLATE_RESULT (decl) = inner;
8915 : :
8916 : 417795 : if (type)
8917 : : {
8918 : : /* Point at the to-be-discarded type & decl. */
8919 : 74922 : TYPE_NAME (type) = inner;
8920 : 74922 : TREE_TYPE (inner) = type;
8921 : :
8922 : 149590 : TYPE_STUB_DECL (type) = stub_decl ? stub_decl : inner;
8923 : 74922 : if (stub_decl)
8924 : 254 : TREE_TYPE (stub_decl) = type;
8925 : :
8926 : 74922 : tree etype = TREE_TYPE (existing);
8927 : :
8928 : : /* Handle separate declarations with different attributes. */
8929 : 74922 : tree &dattr = TYPE_ATTRIBUTES (type);
8930 : 74922 : tree &eattr = TYPE_ATTRIBUTES (etype);
8931 : 74922 : check_abi_tags (existing, decl, eattr, dattr);
8932 : : // TODO: handle other conflicting type attributes
8933 : 74922 : eattr = merge_attributes (eattr, dattr);
8934 : :
8935 : : /* When merging a partial specialisation, the existing decl may have
8936 : : had its TYPE_CANONICAL adjusted. If so we should use structural
8937 : : equality to ensure is_matching_decl doesn't get confused. */
8938 : 74922 : if ((spec_flags & 2)
8939 : 74922 : && TYPE_CANONICAL (type) != TYPE_CANONICAL (etype))
8940 : 3 : SET_TYPE_STRUCTURAL_EQUALITY (type);
8941 : : }
8942 : :
8943 : 417795 : if (inner_tag)
8944 : : /* Set the TEMPLATE_DECL's type. */
8945 : 140894 : TREE_TYPE (decl) = TREE_TYPE (inner);
8946 : :
8947 : 417795 : if (!is_matching_decl (existing, decl, is_typedef))
8948 : 42 : unmatched_duplicate (existing);
8949 : :
8950 : 417795 : if (TREE_CODE (inner) == FUNCTION_DECL)
8951 : : {
8952 : 188636 : tree e_inner = STRIP_TEMPLATE (existing);
8953 : 188636 : for (auto parm = DECL_ARGUMENTS (inner);
8954 : 566122 : parm; parm = DECL_CHAIN (parm))
8955 : 377486 : DECL_CONTEXT (parm) = e_inner;
8956 : : }
8957 : :
8958 : : /* And our result is the existing node. */
8959 : 417795 : decl = existing;
8960 : : }
8961 : :
8962 : 867779 : if (mk == MK_friend_spec)
8963 : : {
8964 : 0 : tree e = match_mergeable_specialization (true, &spec);
8965 : 0 : if (!e)
8966 : : {
8967 : 0 : spec.spec = inner;
8968 : 0 : add_mergeable_specialization (true, &spec, decl, spec_flags);
8969 : : }
8970 : 0 : else if (e != existing)
8971 : 0 : set_overrun ();
8972 : : }
8973 : :
8974 : 867779 : if (is_typedef)
8975 : : {
8976 : : /* Insert the type into the array now. */
8977 : 156919 : tag = insert (TREE_TYPE (decl));
8978 : 156919 : dump (dumper::TREE)
8979 : 247 : && dump ("Cloned:%d typedef %C:%N",
8980 : 247 : tag, TREE_CODE (TREE_TYPE (decl)), TREE_TYPE (decl));
8981 : : }
8982 : :
8983 : 867779 : unused = saved_unused;
8984 : :
8985 : 867779 : if (DECL_MAYBE_IN_CHARGE_CDTOR_P (decl))
8986 : : {
8987 : 69713 : unsigned flags = u ();
8988 : :
8989 : 69713 : if (is_new)
8990 : : {
8991 : 30942 : bool cloned_p = flags & 1;
8992 : 31042 : dump (dumper::TREE) && dump ("CDTOR %N is %scloned",
8993 : : decl, cloned_p ? "" : "not ");
8994 : 30942 : if (cloned_p)
8995 : 45852 : build_cdtor_clones (decl, flags & 2, flags & 4,
8996 : : /* Update the member vec, if there is
8997 : : one (we're in a different cluster
8998 : : to the class defn). */
8999 : 22926 : CLASSTYPE_MEMBER_VEC (DECL_CONTEXT (decl)));
9000 : : }
9001 : : }
9002 : :
9003 : 867779 : if (VAR_P (decl) && CP_DECL_THREAD_LOCAL_P (decl))
9004 : : {
9005 : 162 : enum tls_model model = tls_model (u ());
9006 : 162 : if (is_new)
9007 : 142 : set_decl_tls_model (decl, model);
9008 : : }
9009 : :
9010 : 867779 : if (!NAMESPACE_SCOPE_P (inner)
9011 : 635502 : && ((TREE_CODE (inner) == TYPE_DECL
9012 : 153501 : && !is_typedef
9013 : 27878 : && TYPE_NAME (TREE_TYPE (inner)) == inner)
9014 : 607624 : || TREE_CODE (inner) == FUNCTION_DECL)
9015 : 1135429 : && u ())
9016 : 0 : read_definition (decl);
9017 : :
9018 : : return decl;
9019 : : }
9020 : :
9021 : : /* DECL is an unnameable member of CTX. Return a suitable identifying
9022 : : index. */
9023 : :
9024 : : static unsigned
9025 : 800 : get_field_ident (tree ctx, tree decl)
9026 : : {
9027 : 800 : gcc_checking_assert (TREE_CODE (decl) == USING_DECL
9028 : : || !DECL_NAME (decl)
9029 : : || IDENTIFIER_ANON_P (DECL_NAME (decl)));
9030 : :
9031 : 800 : unsigned ix = 0;
9032 : 800 : for (tree fields = TYPE_FIELDS (ctx);
9033 : 6661 : fields; fields = DECL_CHAIN (fields))
9034 : : {
9035 : 6661 : if (fields == decl)
9036 : 800 : return ix;
9037 : :
9038 : 5861 : if (DECL_CONTEXT (fields) == ctx
9039 : 5861 : && (TREE_CODE (fields) == USING_DECL
9040 : 5850 : || (TREE_CODE (fields) == FIELD_DECL
9041 : 19 : && (!DECL_NAME (fields)
9042 : 7 : || IDENTIFIER_ANON_P (DECL_NAME (fields))))))
9043 : : /* Count this field. */
9044 : 15 : ix++;
9045 : : }
9046 : 0 : gcc_unreachable ();
9047 : : }
9048 : :
9049 : : static tree
9050 : 684 : lookup_field_ident (tree ctx, unsigned ix)
9051 : : {
9052 : 684 : for (tree fields = TYPE_FIELDS (ctx);
9053 : 5492 : fields; fields = DECL_CHAIN (fields))
9054 : 5492 : if (DECL_CONTEXT (fields) == ctx
9055 : 5492 : && (TREE_CODE (fields) == USING_DECL
9056 : 5480 : || (TREE_CODE (fields) == FIELD_DECL
9057 : 696 : && (!DECL_NAME (fields)
9058 : 3 : || IDENTIFIER_ANON_P (DECL_NAME (fields))))))
9059 : 699 : if (!ix--)
9060 : : return fields;
9061 : :
9062 : : return NULL_TREE;
9063 : : }
9064 : :
9065 : : /* Reference DECL. REF indicates the walk kind we are performing.
9066 : : Return true if we should write this decl by value. */
9067 : :
9068 : : bool
9069 : 9514956 : trees_out::decl_node (tree decl, walk_kind ref)
9070 : : {
9071 : 9514956 : gcc_checking_assert (DECL_P (decl) && !DECL_TEMPLATE_PARM_P (decl)
9072 : : && DECL_CONTEXT (decl));
9073 : :
9074 : 9514956 : if (ref == WK_value)
9075 : : {
9076 : 1010805 : depset *dep = dep_hash->find_dependency (decl);
9077 : 1010805 : decl_value (decl, dep);
9078 : 1010805 : return false;
9079 : : }
9080 : :
9081 : 8504151 : switch (TREE_CODE (decl))
9082 : : {
9083 : : default:
9084 : : break;
9085 : :
9086 : 885115 : case FUNCTION_DECL:
9087 : 885115 : gcc_checking_assert (!DECL_LOCAL_DECL_P (decl));
9088 : : break;
9089 : :
9090 : : case RESULT_DECL:
9091 : : /* Unlike PARM_DECLs, RESULT_DECLs are only generated and
9092 : : referenced when we're inside the function itself. */
9093 : : return true;
9094 : :
9095 : 136795 : case PARM_DECL:
9096 : 136795 : {
9097 : 136795 : if (streaming_p ())
9098 : 61089 : i (tt_parm);
9099 : 136795 : tree_node (DECL_CONTEXT (decl));
9100 : :
9101 : : /* That must have put this in the map. */
9102 : 136795 : walk_kind ref = ref_node (decl);
9103 : 136795 : if (ref != WK_none)
9104 : : // FIXME:OPTIMIZATION We can wander into bits of the
9105 : : // template this was instantiated from, for instance
9106 : : // deferred noexcept and default parms, or references
9107 : : // to parms from earlier forward-decls (PR c++/119608).
9108 : : //
9109 : : // Currently we'll end up cloning those bits of tree.
9110 : : // It would be nice to reference those specific nodes.
9111 : : // I think putting those things in the map when we
9112 : : // reference their template by name.
9113 : : //
9114 : : // See the note in add_indirects.
9115 : : return true;
9116 : :
9117 : 0 : if (streaming_p ())
9118 : 0 : dump (dumper::TREE)
9119 : 0 : && dump ("Wrote %s reference %N",
9120 : 0 : TREE_CODE (decl) == PARM_DECL ? "parameter" : "result",
9121 : : decl);
9122 : : }
9123 : : return false;
9124 : :
9125 : : case IMPORTED_DECL:
9126 : : /* This describes a USING_DECL to the ME's debug machinery. It
9127 : : originates from the fortran FE, and has nothing to do with
9128 : : C++ modules. */
9129 : : return true;
9130 : :
9131 : : case LABEL_DECL:
9132 : : return true;
9133 : :
9134 : 46847 : case CONST_DECL:
9135 : 46847 : {
9136 : : /* If I end up cloning enum decls, implementing C++20 using
9137 : : E::v, this will need tweaking. */
9138 : 46847 : if (streaming_p ())
9139 : 10791 : i (tt_enum_decl);
9140 : 46847 : tree ctx = DECL_CONTEXT (decl);
9141 : 46847 : gcc_checking_assert (TREE_CODE (ctx) == ENUMERAL_TYPE);
9142 : 46847 : tree_node (ctx);
9143 : 46847 : tree_node (DECL_NAME (decl));
9144 : :
9145 : 46847 : int tag = insert (decl);
9146 : 46847 : if (streaming_p ())
9147 : 10791 : dump (dumper::TREE)
9148 : 21 : && dump ("Wrote enum decl:%d %C:%N", tag, TREE_CODE (decl), decl);
9149 : : return false;
9150 : : }
9151 : 14490 : break;
9152 : :
9153 : 14490 : case USING_DECL:
9154 : 14490 : if (TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
9155 : : break;
9156 : : /* FALLTHROUGH */
9157 : :
9158 : 111960 : case FIELD_DECL:
9159 : 111960 : {
9160 : 111960 : if (streaming_p ())
9161 : 6908 : i (tt_data_member);
9162 : :
9163 : 111960 : tree ctx = DECL_CONTEXT (decl);
9164 : 111960 : tree_node (ctx);
9165 : :
9166 : 111960 : tree name = NULL_TREE;
9167 : :
9168 : 111960 : if (TREE_CODE (decl) == USING_DECL)
9169 : : ;
9170 : : else
9171 : : {
9172 : 111226 : name = DECL_NAME (decl);
9173 : 216492 : if (name && IDENTIFIER_ANON_P (name))
9174 : : name = NULL_TREE;
9175 : : }
9176 : :
9177 : 111960 : tree_node (name);
9178 : 111960 : if (!name && streaming_p ())
9179 : : {
9180 : 800 : unsigned ix = get_field_ident (ctx, decl);
9181 : 800 : u (ix);
9182 : : }
9183 : :
9184 : 111960 : int tag = insert (decl);
9185 : 111960 : if (streaming_p ())
9186 : 6908 : dump (dumper::TREE)
9187 : 26 : && dump ("Wrote member:%d %C:%N", tag, TREE_CODE (decl), decl);
9188 : : return false;
9189 : : }
9190 : 374789 : break;
9191 : :
9192 : 374789 : case VAR_DECL:
9193 : 374789 : gcc_checking_assert (!DECL_LOCAL_DECL_P (decl));
9194 : 374789 : if (DECL_VTABLE_OR_VTT_P (decl))
9195 : : {
9196 : : /* VTT or VTABLE, they are all on the vtables list. */
9197 : 1792 : tree ctx = CP_DECL_CONTEXT (decl);
9198 : 1792 : tree vtable = CLASSTYPE_VTABLES (ctx);
9199 : 1804 : for (unsigned ix = 0; ; vtable = DECL_CHAIN (vtable), ix++)
9200 : 1804 : if (vtable == decl)
9201 : : {
9202 : 1792 : gcc_checking_assert (DECL_VIRTUAL_P (decl));
9203 : 1792 : if (streaming_p ())
9204 : : {
9205 : 8 : u (tt_vtable);
9206 : 8 : u (ix);
9207 : 8 : dump (dumper::TREE)
9208 : 0 : && dump ("Writing vtable %N[%u]", ctx, ix);
9209 : : }
9210 : 1792 : tree_node (ctx);
9211 : 1792 : return false;
9212 : : }
9213 : : gcc_unreachable ();
9214 : : }
9215 : :
9216 : 372997 : if (DECL_TINFO_P (decl))
9217 : : {
9218 : 5312 : tinfo:
9219 : : /* A typeinfo, tt_tinfo_typedef or tt_tinfo_var. */
9220 : 9762 : bool is_var = VAR_P (decl);
9221 : 9762 : tree type = TREE_TYPE (decl);
9222 : 9762 : unsigned ix = get_pseudo_tinfo_index (type);
9223 : 9762 : if (streaming_p ())
9224 : : {
9225 : 6211 : i (is_var ? tt_tinfo_var : tt_tinfo_typedef);
9226 : 4405 : u (ix);
9227 : : }
9228 : :
9229 : 9762 : if (is_var)
9230 : : {
9231 : : /* We also need the type it is for and mangled name, so
9232 : : the reader doesn't need to complete the type (which
9233 : : would break section ordering). The type it is for is
9234 : : stashed on the name's TREE_TYPE. */
9235 : 5312 : tree name = DECL_NAME (decl);
9236 : 5312 : tree_node (name);
9237 : 5312 : type = TREE_TYPE (name);
9238 : 5312 : tree_node (type);
9239 : : }
9240 : :
9241 : 9762 : int tag = insert (decl);
9242 : 9762 : if (streaming_p ())
9243 : 4405 : dump (dumper::TREE)
9244 : 27 : && dump ("Wrote tinfo_%s:%d %u %N", is_var ? "var" : "type",
9245 : : tag, ix, type);
9246 : :
9247 : 9762 : if (!is_var)
9248 : : {
9249 : 4450 : tag = insert (type);
9250 : 4450 : if (streaming_p ())
9251 : 1806 : dump (dumper::TREE)
9252 : 9 : && dump ("Wrote tinfo_type:%d %u %N", tag, ix, type);
9253 : : }
9254 : 9762 : return false;
9255 : : }
9256 : :
9257 : 367685 : if (DECL_NTTP_OBJECT_P (decl))
9258 : : {
9259 : : /* A NTTP parm object. */
9260 : 33 : if (streaming_p ())
9261 : 7 : i (tt_nttp_var);
9262 : 33 : tree_node (tparm_object_argument (decl));
9263 : 33 : tree_node (DECL_NAME (decl));
9264 : 33 : int tag = insert (decl);
9265 : 33 : if (streaming_p ())
9266 : 7 : dump (dumper::TREE)
9267 : 0 : && dump ("Wrote nttp object:%d %N", tag, DECL_NAME (decl));
9268 : 33 : return false;
9269 : : }
9270 : :
9271 : : break;
9272 : :
9273 : 3024442 : case TYPE_DECL:
9274 : 3024442 : if (DECL_TINFO_P (decl))
9275 : 4450 : goto tinfo;
9276 : : break;
9277 : : }
9278 : :
9279 : 7725487 : if (DECL_THUNK_P (decl))
9280 : : {
9281 : : /* Thunks are similar to binfos -- write the thunked-to decl and
9282 : : then thunk-specific key info. */
9283 : 0 : if (streaming_p ())
9284 : : {
9285 : 0 : i (tt_thunk);
9286 : 0 : i (THUNK_FIXED_OFFSET (decl));
9287 : : }
9288 : :
9289 : : tree target = decl;
9290 : 0 : while (DECL_THUNK_P (target))
9291 : 0 : target = THUNK_TARGET (target);
9292 : 0 : tree_node (target);
9293 : 0 : tree_node (THUNK_VIRTUAL_OFFSET (decl));
9294 : 0 : int tag = insert (decl);
9295 : 0 : if (streaming_p ())
9296 : 0 : dump (dumper::TREE)
9297 : 0 : && dump ("Wrote:%d thunk %N to %N", tag, DECL_NAME (decl), target);
9298 : 0 : return false;
9299 : : }
9300 : :
9301 : 7725487 : if (DECL_CLONED_FUNCTION_P (decl))
9302 : : {
9303 : 254730 : tree target = get_clone_target (decl);
9304 : 254730 : if (streaming_p ())
9305 : 120514 : i (tt_clone_ref);
9306 : :
9307 : 254730 : tree_node (target);
9308 : 254730 : tree_node (DECL_NAME (decl));
9309 : 254730 : if (DECL_VIRTUAL_P (decl))
9310 : 19127 : tree_node (DECL_VINDEX (decl));
9311 : 254730 : int tag = insert (decl);
9312 : 254730 : if (streaming_p ())
9313 : 120514 : dump (dumper::TREE)
9314 : 164 : && dump ("Wrote:%d clone %N of %N", tag, DECL_NAME (decl), target);
9315 : 254730 : return false;
9316 : : }
9317 : :
9318 : : /* Everything left should be a thing that is in the entity table.
9319 : : Mostly things that can be defined outside of their (original
9320 : : declaration) context. */
9321 : 7470757 : gcc_checking_assert (TREE_CODE (decl) == TEMPLATE_DECL
9322 : : || VAR_P (decl)
9323 : : || TREE_CODE (decl) == FUNCTION_DECL
9324 : : || TREE_CODE (decl) == TYPE_DECL
9325 : : || TREE_CODE (decl) == USING_DECL
9326 : : || TREE_CODE (decl) == CONCEPT_DECL
9327 : : || TREE_CODE (decl) == NAMESPACE_DECL);
9328 : :
9329 : 7470757 : int use_tpl = -1;
9330 : 7470757 : tree ti = node_template_info (decl, use_tpl);
9331 : 7470757 : tree tpl = NULL_TREE;
9332 : :
9333 : : /* If this is the TEMPLATE_DECL_RESULT of a TEMPLATE_DECL, get the
9334 : : TEMPLATE_DECL. Note TI_TEMPLATE is not a TEMPLATE_DECL for
9335 : : (some) friends, so we need to check that. */
9336 : : // FIXME: Should local friend template specializations be by value?
9337 : : // They don't get idents so we'll never know they're imported, but I
9338 : : // think we can only reach them from the TU that defines the
9339 : : // befriending class?
9340 : 2823505 : if (ti && TREE_CODE (TI_TEMPLATE (ti)) == TEMPLATE_DECL
9341 : 10294228 : && DECL_TEMPLATE_RESULT (TI_TEMPLATE (ti)) == decl)
9342 : : {
9343 : : tpl = TI_TEMPLATE (ti);
9344 : 719666 : partial_template:
9345 : 719666 : if (streaming_p ())
9346 : : {
9347 : 2240 : i (tt_template);
9348 : 2240 : dump (dumper::TREE)
9349 : 9 : && dump ("Writing implicit template %C:%N%S",
9350 : 9 : TREE_CODE (tpl), tpl, tpl);
9351 : : }
9352 : 719666 : tree_node (tpl);
9353 : :
9354 : : /* Streaming TPL caused us to visit DECL and maybe its type,
9355 : : if it wasn't TU-local. */
9356 : 719666 : if (CHECKING_P && !has_tu_local_dep (tpl))
9357 : : {
9358 : 719639 : gcc_checking_assert (TREE_VISITED (decl));
9359 : 719639 : if (DECL_IMPLICIT_TYPEDEF_P (decl))
9360 : 374072 : gcc_checking_assert (TREE_VISITED (TREE_TYPE (decl)));
9361 : : }
9362 : : return false;
9363 : : }
9364 : :
9365 : 6831271 : tree ctx = CP_DECL_CONTEXT (decl);
9366 : 6831271 : depset *dep = NULL;
9367 : 6831271 : if (streaming_p ())
9368 : 1144725 : dep = dep_hash->find_dependency (decl);
9369 : 5686546 : else if (TREE_CODE (ctx) != FUNCTION_DECL
9370 : 210441 : || TREE_CODE (decl) == TEMPLATE_DECL
9371 : 190255 : || DECL_IMPLICIT_TYPEDEF_P (decl)
9372 : 5853858 : || (DECL_LANG_SPECIFIC (decl)
9373 : 120349 : && DECL_MODULE_IMPORT_P (decl)))
9374 : : {
9375 : 5519234 : auto kind = (TREE_CODE (decl) == NAMESPACE_DECL
9376 : 477660 : && !DECL_NAMESPACE_ALIAS (decl)
9377 : 5519234 : ? depset::EK_NAMESPACE : depset::EK_DECL);
9378 : 5519234 : dep = dep_hash->add_dependency (decl, kind);
9379 : : }
9380 : :
9381 : 13170842 : if (!dep || dep->is_tu_local ())
9382 : : {
9383 : : /* Some internal entity of context. Do by value. */
9384 : 324648 : decl_value (decl, dep);
9385 : 324648 : return false;
9386 : : }
9387 : :
9388 : 6506623 : if (dep->get_entity_kind () == depset::EK_REDIRECT)
9389 : : {
9390 : : /* The DECL_TEMPLATE_RESULT of a partial specialization.
9391 : : Write the partial specialization's template. */
9392 : 80180 : depset *redirect = dep->deps[0];
9393 : 80180 : gcc_checking_assert (redirect->get_entity_kind () == depset::EK_PARTIAL);
9394 : 80180 : tpl = redirect->get_entity ();
9395 : 80180 : goto partial_template;
9396 : : }
9397 : :
9398 : 6426443 : if (streaming_p ())
9399 : : {
9400 : : /* Locate the entity. */
9401 : 987649 : unsigned index = dep->cluster;
9402 : 987649 : unsigned import = 0;
9403 : :
9404 : 987649 : if (dep->is_import ())
9405 : 729 : import = dep->section;
9406 : 986920 : else if (CHECKING_P)
9407 : : /* It should be what we put there. */
9408 : 986920 : gcc_checking_assert (index == ~import_entity_index (decl));
9409 : :
9410 : : #if CHECKING_P
9411 : 729 : gcc_assert (!import || importedness >= 0);
9412 : : #endif
9413 : 987649 : i (tt_entity);
9414 : 987649 : u (import);
9415 : 987649 : u (index);
9416 : : }
9417 : :
9418 : 6426443 : int tag = insert (decl);
9419 : 6426443 : if (streaming_p () && dump (dumper::TREE))
9420 : : {
9421 : 529 : char const *kind = "import";
9422 : 529 : module_state *from = this_module ();
9423 : 529 : if (dep->is_import ())
9424 : : /* Rediscover the unremapped index. */
9425 : 78 : from = import_entity_module (import_entity_index (decl));
9426 : : else
9427 : : {
9428 : 451 : tree o = get_originating_module_decl (decl);
9429 : 451 : o = STRIP_TEMPLATE (o);
9430 : 902 : kind = (DECL_LANG_SPECIFIC (o) && DECL_MODULE_PURVIEW_P (o)
9431 : 451 : ? "purview" : "GMF");
9432 : : }
9433 : 529 : dump ("Wrote %s:%d %C:%N@%M", kind,
9434 : 529 : tag, TREE_CODE (decl), decl, from);
9435 : : }
9436 : :
9437 : 6426443 : add_indirects (decl);
9438 : :
9439 : 6426443 : return false;
9440 : : }
9441 : :
9442 : : void
9443 : 7809647 : trees_out::type_node (tree type)
9444 : : {
9445 : 7809647 : gcc_assert (TYPE_P (type));
9446 : :
9447 : 7809647 : tree root = (TYPE_NAME (type)
9448 : 7809647 : ? TREE_TYPE (TYPE_NAME (type)) : TYPE_MAIN_VARIANT (type));
9449 : 7809647 : gcc_checking_assert (root);
9450 : :
9451 : 7809647 : if (type != root)
9452 : : {
9453 : 1768887 : if (streaming_p ())
9454 : 369772 : i (tt_variant_type);
9455 : 1768887 : tree_node (root);
9456 : :
9457 : 1768887 : int flags = -1;
9458 : :
9459 : 1768887 : if (TREE_CODE (type) == FUNCTION_TYPE
9460 : 1768887 : || TREE_CODE (type) == METHOD_TYPE)
9461 : : {
9462 : 422039 : int quals = type_memfn_quals (type);
9463 : 422039 : int rquals = type_memfn_rqual (type);
9464 : 422039 : tree raises = TYPE_RAISES_EXCEPTIONS (type);
9465 : 422039 : bool late = TYPE_HAS_LATE_RETURN_TYPE (type);
9466 : :
9467 : 422039 : if (raises != TYPE_RAISES_EXCEPTIONS (root)
9468 : 11081 : || rquals != type_memfn_rqual (root)
9469 : 7995 : || quals != type_memfn_quals (root)
9470 : 430016 : || late != TYPE_HAS_LATE_RETURN_TYPE (root))
9471 : 422039 : flags = rquals | (int (late) << 2) | (quals << 3);
9472 : : }
9473 : : else
9474 : : {
9475 : 1346848 : if (TYPE_USER_ALIGN (type))
9476 : 11798 : flags = TYPE_ALIGN_RAW (type);
9477 : : }
9478 : :
9479 : 1768887 : if (streaming_p ())
9480 : 369772 : i (flags);
9481 : :
9482 : 1768887 : if (flags < 0)
9483 : : ;
9484 : 433837 : else if (TREE_CODE (type) == FUNCTION_TYPE
9485 : 433837 : || TREE_CODE (type) == METHOD_TYPE)
9486 : : {
9487 : 422039 : tree raises = TYPE_RAISES_EXCEPTIONS (type);
9488 : 422039 : if (raises == TYPE_RAISES_EXCEPTIONS (root))
9489 : 11081 : raises = error_mark_node;
9490 : 422039 : tree_node (raises);
9491 : : }
9492 : :
9493 : : /* build_type_attribute_variant creates a new TYPE_MAIN_VARIANT, so
9494 : : variants should all have the same set of attributes. */
9495 : 1768887 : gcc_checking_assert (TYPE_ATTRIBUTES (type)
9496 : : == TYPE_ATTRIBUTES (TYPE_MAIN_VARIANT (type)));
9497 : :
9498 : 1768887 : if (streaming_p ())
9499 : : {
9500 : : /* Qualifiers. */
9501 : 369772 : int rquals = cp_type_quals (root);
9502 : 369772 : int quals = cp_type_quals (type);
9503 : 369772 : if (quals == rquals)
9504 : 171216 : quals = -1;
9505 : 369772 : i (quals);
9506 : : }
9507 : :
9508 : 1768887 : if (ref_node (type) != WK_none)
9509 : : {
9510 : 1768887 : int tag = insert (type);
9511 : 1768887 : if (streaming_p ())
9512 : : {
9513 : 369772 : i (0);
9514 : 369772 : dump (dumper::TREE)
9515 : 203 : && dump ("Wrote:%d variant type %C", tag, TREE_CODE (type));
9516 : : }
9517 : : }
9518 : 1768887 : return;
9519 : : }
9520 : :
9521 : 6040760 : if (tree name = TYPE_NAME (type))
9522 : 2379367 : if ((TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name))
9523 : 1858818 : || DECL_TEMPLATE_PARM_P (name)
9524 : 1249532 : || TREE_CODE (type) == RECORD_TYPE
9525 : 225760 : || TREE_CODE (type) == UNION_TYPE
9526 : 2599725 : || TREE_CODE (type) == ENUMERAL_TYPE)
9527 : : {
9528 : 2216746 : gcc_checking_assert (DECL_P (name));
9529 : :
9530 : : /* We can meet template parms that we didn't meet in the
9531 : : tpl_parms walk, because we're referring to a derived type
9532 : : that was previously constructed from equivalent template
9533 : : parms. */
9534 : 2216746 : if (streaming_p ())
9535 : : {
9536 : 155553 : i (tt_typedef_type);
9537 : 155553 : dump (dumper::TREE)
9538 : 59 : && dump ("Writing %stypedef %C:%N",
9539 : 59 : DECL_IMPLICIT_TYPEDEF_P (name) ? "implicit " : "",
9540 : 59 : TREE_CODE (name), name);
9541 : : }
9542 : 2216746 : tree_node (name);
9543 : 2216746 : if (streaming_p ())
9544 : 155553 : dump (dumper::TREE) && dump ("Wrote typedef %C:%N%S",
9545 : 59 : TREE_CODE (name), name, name);
9546 : :
9547 : : /* We'll have either visited this type or have newly discovered
9548 : : that it's TU-local; either way we won't need to visit it again. */
9549 : 2216746 : gcc_checking_assert (TREE_VISITED (type) || has_tu_local_dep (name));
9550 : 2216746 : return;
9551 : : }
9552 : :
9553 : 3824014 : if (TYPE_PTRMEMFUNC_P (type))
9554 : : {
9555 : : /* This is a distinct type node, masquerading as a structure. */
9556 : 3823 : tree fn_type = TYPE_PTRMEMFUNC_FN_TYPE (type);
9557 : 3823 : if (streaming_p ())
9558 : 1073 : i (tt_ptrmem_type);
9559 : 3823 : tree_node (fn_type);
9560 : 3823 : int tag = insert (type);
9561 : 3823 : if (streaming_p ())
9562 : 1076 : dump (dumper::TREE) && dump ("Written:%d ptrmem type", tag);
9563 : 3823 : return;
9564 : : }
9565 : :
9566 : 3820191 : if (streaming_p ())
9567 : : {
9568 : 1218344 : u (tt_derived_type);
9569 : 1218344 : u (TREE_CODE (type));
9570 : : }
9571 : :
9572 : 3820191 : tree_node (TREE_TYPE (type));
9573 : 3820191 : switch (TREE_CODE (type))
9574 : : {
9575 : 0 : default:
9576 : : /* We should never meet a type here that is indescribable in
9577 : : terms of other types. */
9578 : 0 : gcc_unreachable ();
9579 : :
9580 : 63628 : case ARRAY_TYPE:
9581 : 63628 : tree_node (TYPE_DOMAIN (type));
9582 : 63628 : if (streaming_p ())
9583 : : /* Dependent arrays are constructed with TYPE_DEPENENT_P
9584 : : already set. */
9585 : 20407 : u (TYPE_DEPENDENT_P (type));
9586 : : break;
9587 : :
9588 : : case COMPLEX_TYPE:
9589 : : /* No additional data. */
9590 : : break;
9591 : :
9592 : 12 : case BOOLEAN_TYPE:
9593 : : /* A non-standard boolean type. */
9594 : 12 : if (streaming_p ())
9595 : 6 : u (TYPE_PRECISION (type));
9596 : : break;
9597 : :
9598 : 58493 : case INTEGER_TYPE:
9599 : 58493 : if (TREE_TYPE (type))
9600 : : {
9601 : : /* A range type (representing an array domain). */
9602 : 55644 : tree_node (TYPE_MIN_VALUE (type));
9603 : 55644 : tree_node (TYPE_MAX_VALUE (type));
9604 : : }
9605 : : else
9606 : : {
9607 : : /* A new integral type (representing a bitfield). */
9608 : 2849 : if (streaming_p ())
9609 : : {
9610 : 815 : unsigned prec = TYPE_PRECISION (type);
9611 : 815 : bool unsigned_p = TYPE_UNSIGNED (type);
9612 : :
9613 : 815 : u ((prec << 1) | unsigned_p);
9614 : : }
9615 : : }
9616 : : break;
9617 : :
9618 : 839769 : case METHOD_TYPE:
9619 : 839769 : case FUNCTION_TYPE:
9620 : 839769 : {
9621 : 839769 : gcc_checking_assert (type_memfn_rqual (type) == REF_QUAL_NONE);
9622 : :
9623 : 839769 : tree arg_types = TYPE_ARG_TYPES (type);
9624 : 839769 : if (TREE_CODE (type) == METHOD_TYPE)
9625 : : {
9626 : 513095 : tree_node (TREE_TYPE (TREE_VALUE (arg_types)));
9627 : 513095 : arg_types = TREE_CHAIN (arg_types);
9628 : : }
9629 : 839769 : tree_node (arg_types);
9630 : : }
9631 : 839769 : break;
9632 : :
9633 : 1222 : case OFFSET_TYPE:
9634 : 1222 : tree_node (TYPE_OFFSET_BASETYPE (type));
9635 : 1222 : break;
9636 : :
9637 : : case POINTER_TYPE:
9638 : : /* No additional data. */
9639 : : break;
9640 : :
9641 : 644744 : case REFERENCE_TYPE:
9642 : 644744 : if (streaming_p ())
9643 : 142206 : u (TYPE_REF_IS_RVALUE (type));
9644 : : break;
9645 : :
9646 : 751620 : case DECLTYPE_TYPE:
9647 : 751620 : case TYPEOF_TYPE:
9648 : 751620 : case DEPENDENT_OPERATOR_TYPE:
9649 : 751620 : tree_node (TYPE_VALUES_RAW (type));
9650 : 751620 : if (TREE_CODE (type) == DECLTYPE_TYPE)
9651 : : /* We stash a whole bunch of things into decltype's
9652 : : flags. */
9653 : 56860 : if (streaming_p ())
9654 : 19370 : tree_node_bools (type);
9655 : : break;
9656 : :
9657 : 7803 : case TRAIT_TYPE:
9658 : 7803 : tree_node (TRAIT_TYPE_KIND_RAW (type));
9659 : 7803 : tree_node (TRAIT_TYPE_TYPE1 (type));
9660 : 7803 : tree_node (TRAIT_TYPE_TYPE2 (type));
9661 : 7803 : break;
9662 : :
9663 : : case TYPE_ARGUMENT_PACK:
9664 : : /* No additional data. */
9665 : : break;
9666 : :
9667 : 130498 : case TYPE_PACK_EXPANSION:
9668 : 130498 : if (streaming_p ())
9669 : 52855 : u (PACK_EXPANSION_LOCAL_P (type));
9670 : 260996 : tree_node (PACK_EXPANSION_PARAMETER_PACKS (type));
9671 : 130498 : tree_node (PACK_EXPANSION_EXTRA_ARGS (type));
9672 : 130498 : break;
9673 : :
9674 : 22 : case PACK_INDEX_TYPE:
9675 : 22 : tree_node (PACK_INDEX_PACK (type));
9676 : 22 : tree_node (PACK_INDEX_INDEX (type));
9677 : 22 : break;
9678 : :
9679 : 164635 : case TYPENAME_TYPE:
9680 : 164635 : {
9681 : 164635 : tree_node (TYPE_CONTEXT (type));
9682 : 164635 : tree_node (DECL_NAME (TYPE_NAME (type)));
9683 : 164635 : tree_node (TYPENAME_TYPE_FULLNAME (type));
9684 : 164635 : if (streaming_p ())
9685 : : {
9686 : 58307 : enum tag_types tag_type = none_type;
9687 : 58307 : if (TYPENAME_IS_ENUM_P (type))
9688 : : tag_type = enum_type;
9689 : 58307 : else if (TYPENAME_IS_CLASS_P (type))
9690 : : tag_type = class_type;
9691 : 56533 : else if (TYPENAME_IS_UNION_P (type))
9692 : 0 : tag_type = union_type;
9693 : 58307 : u (int (tag_type));
9694 : : }
9695 : : }
9696 : : break;
9697 : :
9698 : 246 : case UNBOUND_CLASS_TEMPLATE:
9699 : 246 : {
9700 : 246 : tree decl = TYPE_NAME (type);
9701 : 246 : tree_node (DECL_CONTEXT (decl));
9702 : 246 : tree_node (DECL_NAME (decl));
9703 : 246 : tree_node (DECL_TEMPLATE_PARMS (decl));
9704 : : }
9705 : 246 : break;
9706 : :
9707 : 42 : case VECTOR_TYPE:
9708 : 42 : if (streaming_p ())
9709 : : {
9710 : 21 : poly_uint64 nunits = TYPE_VECTOR_SUBPARTS (type);
9711 : 42 : for (unsigned ix = 0; ix != NUM_POLY_INT_COEFFS; ix++)
9712 : 21 : wu (nunits.coeffs[ix]);
9713 : : }
9714 : : break;
9715 : : }
9716 : :
9717 : 3820191 : tree_node (TYPE_ATTRIBUTES (type));
9718 : :
9719 : : /* We may have met the type during emitting the above. */
9720 : 3820191 : if (ref_node (type) != WK_none)
9721 : : {
9722 : 3472811 : int tag = insert (type);
9723 : 3472811 : if (streaming_p ())
9724 : : {
9725 : 1055691 : i (0);
9726 : 1055691 : dump (dumper::TREE)
9727 : 558 : && dump ("Wrote:%d derived type %C", tag, TREE_CODE (type));
9728 : : }
9729 : : }
9730 : :
9731 : : return;
9732 : : }
9733 : :
9734 : : /* T is (mostly*) a non-mergeable node that must be written by value.
9735 : : The mergeable case is a BINFO, which are as-if DECLSs. */
9736 : :
9737 : : void
9738 : 23493386 : trees_out::tree_value (tree t)
9739 : : {
9740 : : /* We should never be writing a type by value. tree_type should
9741 : : have streamed it, or we're going via its TYPE_DECL. */
9742 : 23493386 : gcc_checking_assert (!TYPE_P (t));
9743 : :
9744 : 23493386 : if (DECL_P (t))
9745 : : /* No template, type, var or function, except anonymous
9746 : : non-context vars and types. */
9747 : 628496 : gcc_checking_assert ((TREE_CODE (t) != TEMPLATE_DECL
9748 : : && (TREE_CODE (t) != TYPE_DECL
9749 : : || (DECL_ARTIFICIAL (t) && !DECL_CONTEXT (t)))
9750 : : && (TREE_CODE (t) != VAR_DECL
9751 : : || ((!DECL_NAME (t)
9752 : : || IDENTIFIER_INTERNAL_P (DECL_NAME (t)))
9753 : : && !DECL_CONTEXT (t)))
9754 : : && TREE_CODE (t) != FUNCTION_DECL));
9755 : :
9756 : 23493386 : if (streaming_p ())
9757 : : {
9758 : : /* A new node -> tt_node. */
9759 : 9364941 : tree_val_count++;
9760 : 9364941 : i (tt_node);
9761 : 9364941 : start (t);
9762 : 9364941 : tree_node_bools (t);
9763 : : }
9764 : :
9765 : 23493386 : if (TREE_CODE (t) == TREE_BINFO)
9766 : : /* Binfos are decl-like and need merging information. */
9767 : 194524 : binfo_mergeable (t);
9768 : :
9769 : 23493386 : int tag = insert (t, WK_value);
9770 : 23493386 : if (streaming_p ())
9771 : 9364941 : dump (dumper::TREE)
9772 : 2823 : && dump ("Writing tree:%d %C:%N", tag, TREE_CODE (t), t);
9773 : :
9774 : 23493386 : int type_tag = 0;
9775 : 23493386 : tree type = NULL_TREE;
9776 : 23493386 : if (TREE_CODE (t) == TYPE_DECL)
9777 : : {
9778 : 28 : type = TREE_TYPE (t);
9779 : :
9780 : : /* We only support a limited set of features for uncontexted types;
9781 : : these are typically types created in the language-independent
9782 : : parts of the frontend (such as ubsan). */
9783 : 28 : gcc_checking_assert (RECORD_OR_UNION_TYPE_P (type)
9784 : : && TYPE_MAIN_VARIANT (type) == type
9785 : : && TYPE_NAME (type) == t
9786 : : && TYPE_STUB_DECL (type) == t
9787 : : && !TYPE_VFIELD (type)
9788 : : && !TYPE_BINFO (type)
9789 : : && !CLASS_TYPE_P (type));
9790 : :
9791 : 28 : if (streaming_p ())
9792 : : {
9793 : 14 : start (type);
9794 : 14 : tree_node_bools (type);
9795 : : }
9796 : :
9797 : 28 : type_tag = insert (type, WK_value);
9798 : 28 : if (streaming_p ())
9799 : 14 : dump (dumper::TREE)
9800 : 0 : && dump ("Writing type: %d %C:%N", type_tag,
9801 : 0 : TREE_CODE (type), type);
9802 : : }
9803 : :
9804 : 23493386 : tree_node_vals (t);
9805 : :
9806 : 23493386 : if (type)
9807 : : {
9808 : 28 : tree_node_vals (type);
9809 : 28 : tree_node (TYPE_SIZE (type));
9810 : 28 : tree_node (TYPE_SIZE_UNIT (type));
9811 : 28 : chained_decls (TYPE_FIELDS (type));
9812 : 28 : if (streaming_p ())
9813 : 14 : dump (dumper::TREE)
9814 : 0 : && dump ("Written type:%d %C:%N", type_tag, TREE_CODE (type), type);
9815 : : }
9816 : :
9817 : : /* For uncontexted VAR_DECLs we need to stream the definition so that
9818 : : importers can recreate their value. */
9819 : 23493386 : if (TREE_CODE (t) == VAR_DECL)
9820 : : {
9821 : 588 : gcc_checking_assert (!DECL_NONTRIVIALLY_INITIALIZED_P (t));
9822 : 588 : tree_node (DECL_INITIAL (t));
9823 : : }
9824 : :
9825 : 23493386 : if (streaming_p ())
9826 : 9367764 : dump (dumper::TREE) && dump ("Written tree:%d %C:%N", tag, TREE_CODE (t), t);
9827 : 23493386 : }
9828 : :
9829 : : tree
9830 : 8058643 : trees_in::tree_value ()
9831 : : {
9832 : 8058643 : tree t = start ();
9833 : 8058643 : if (!t || !tree_node_bools (t))
9834 : 0 : return NULL_TREE;
9835 : :
9836 : 8058643 : tree existing = t;
9837 : 8058643 : if (TREE_CODE (t) == TREE_BINFO)
9838 : : {
9839 : 71336 : tree type;
9840 : 71336 : unsigned ix = binfo_mergeable (&type);
9841 : 71336 : if (TYPE_BINFO (type))
9842 : : {
9843 : : /* We already have a definition, this must be a duplicate. */
9844 : 40248 : dump (dumper::MERGE)
9845 : 232 : && dump ("Deduping binfo %N[%u]", type, ix);
9846 : 40248 : existing = TYPE_BINFO (type);
9847 : 55265 : while (existing && ix--)
9848 : 15017 : existing = TREE_CHAIN (existing);
9849 : 40248 : if (existing)
9850 : 40248 : register_duplicate (t, existing);
9851 : : else
9852 : : /* Error, mismatch -- diagnose in read_class_def's
9853 : : checking. */
9854 : : existing = t;
9855 : : }
9856 : : }
9857 : :
9858 : : /* Insert into map. */
9859 : 8058643 : int tag = insert (existing);
9860 : 8058643 : dump (dumper::TREE)
9861 : 3677 : && dump ("Reading tree:%d %C", tag, TREE_CODE (t));
9862 : :
9863 : 8058643 : int type_tag = 0;
9864 : 8058643 : tree type = NULL_TREE;
9865 : 8058643 : if (TREE_CODE (t) == TYPE_DECL)
9866 : : {
9867 : 14 : type = start ();
9868 : 14 : if (!type || !tree_node_bools (type))
9869 : : t = NULL_TREE;
9870 : :
9871 : 14 : type_tag = insert (type);
9872 : 14 : if (t)
9873 : 14 : dump (dumper::TREE)
9874 : 0 : && dump ("Reading type:%d %C", type_tag, TREE_CODE (type));
9875 : : }
9876 : :
9877 : : if (!t)
9878 : : {
9879 : 0 : bail:
9880 : 0 : back_refs[~tag] = NULL_TREE;
9881 : 0 : if (type_tag)
9882 : 0 : back_refs[~type_tag] = NULL_TREE;
9883 : 0 : set_overrun ();
9884 : 0 : return NULL_TREE;
9885 : : }
9886 : :
9887 : 8058643 : if (!tree_node_vals (t))
9888 : 0 : goto bail;
9889 : :
9890 : 8058643 : if (type)
9891 : : {
9892 : 14 : if (!tree_node_vals (type))
9893 : 0 : goto bail;
9894 : :
9895 : 14 : TYPE_SIZE (type) = tree_node ();
9896 : 14 : TYPE_SIZE_UNIT (type) = tree_node ();
9897 : 14 : TYPE_FIELDS (type) = chained_decls ();
9898 : 14 : if (get_overrun ())
9899 : 0 : goto bail;
9900 : :
9901 : 14 : dump (dumper::TREE)
9902 : 0 : && dump ("Read type:%d %C:%N", type_tag, TREE_CODE (type), type);
9903 : : }
9904 : :
9905 : 8058643 : if (TREE_CODE (t) == VAR_DECL)
9906 : : {
9907 : 290 : DECL_INITIAL (t) = tree_node ();
9908 : 290 : if (TREE_STATIC (t))
9909 : 8 : varpool_node::finalize_decl (t);
9910 : : }
9911 : :
9912 : 8058643 : if (TREE_CODE (t) == LAMBDA_EXPR
9913 : 8058643 : && CLASSTYPE_LAMBDA_EXPR (TREE_TYPE (t)))
9914 : : {
9915 : 1905 : existing = CLASSTYPE_LAMBDA_EXPR (TREE_TYPE (t));
9916 : 1905 : back_refs[~tag] = existing;
9917 : : }
9918 : :
9919 : 8062320 : dump (dumper::TREE) && dump ("Read tree:%d %C:%N", tag, TREE_CODE (t), t);
9920 : :
9921 : 8058643 : if (TREE_CODE (existing) == INTEGER_CST && !TREE_OVERFLOW (existing))
9922 : : {
9923 : 471206 : existing = cache_integer_cst (t, true);
9924 : 471206 : back_refs[~tag] = existing;
9925 : : }
9926 : :
9927 : : return existing;
9928 : : }
9929 : :
9930 : : /* Whether DECL has a TU-local dependency in the hash. */
9931 : :
9932 : : bool
9933 : 720009 : trees_out::has_tu_local_dep (tree decl) const
9934 : : {
9935 : : /* Only the contexts of fields or enums remember that they're
9936 : : TU-local. */
9937 : 720009 : if (DECL_CONTEXT (decl)
9938 : 720009 : && (TREE_CODE (decl) == FIELD_DECL
9939 : 720006 : || TREE_CODE (decl) == CONST_DECL))
9940 : 3 : decl = TYPE_NAME (DECL_CONTEXT (decl));
9941 : :
9942 : 720009 : depset *dep = dep_hash->find_dependency (decl);
9943 : 720009 : if (!dep)
9944 : : {
9945 : : /* This might be the DECL_TEMPLATE_RESULT of a TEMPLATE_DECL
9946 : : which we found was TU-local and gave up early. */
9947 : 8465 : int use_tpl = -1;
9948 : 8465 : if (tree ti = node_template_info (decl, use_tpl))
9949 : 1743 : dep = dep_hash->find_dependency (TI_TEMPLATE (ti));
9950 : : }
9951 : :
9952 : 720024 : return dep && dep->is_tu_local ();
9953 : : }
9954 : :
9955 : : /* If T depends on a TU-local entity, return that decl. */
9956 : :
9957 : : tree
9958 : 371 : trees_out::find_tu_local_decl (tree t)
9959 : : {
9960 : : /* We need to have walked all deps first before we can check. */
9961 : 371 : gcc_checking_assert (!is_initial_scan ());
9962 : :
9963 : 903 : auto walker = [](tree *tp, int *walk_subtrees, void *data) -> tree
9964 : : {
9965 : 532 : auto self = (trees_out *)data;
9966 : :
9967 : 532 : tree decl = NULL_TREE;
9968 : 532 : if (TYPE_P (*tp))
9969 : : {
9970 : : /* A PMF type is a record type, which we otherwise wouldn't walk;
9971 : : return whether the function type is TU-local. */
9972 : 358 : if (TYPE_PTRMEMFUNC_P (*tp))
9973 : : {
9974 : 3 : *walk_subtrees = 0;
9975 : 3 : return self->find_tu_local_decl (TYPE_PTRMEMFUNC_FN_TYPE (*tp));
9976 : : }
9977 : : else
9978 : 355 : decl = TYPE_MAIN_DECL (*tp);
9979 : : }
9980 : 174 : else if (DECL_P (*tp))
9981 : : decl = *tp;
9982 : :
9983 : 361 : if (decl)
9984 : : {
9985 : : /* We found a DECL, this will tell us whether we're TU-local. */
9986 : 59 : *walk_subtrees = 0;
9987 : 59 : return self->has_tu_local_dep (decl) ? decl : NULL_TREE;
9988 : : }
9989 : : return NULL_TREE;
9990 : : };
9991 : :
9992 : : /* We need to walk without duplicates so that we step into the pointed-to
9993 : : types of array types. */
9994 : 371 : return cp_walk_tree_without_duplicates (&t, walker, this);
9995 : : }
9996 : :
9997 : : /* Get the name for TU-local decl T to be used in diagnostics. */
9998 : :
9999 : : static tree
10000 : 197 : name_for_tu_local_decl (tree t)
10001 : : {
10002 : 197 : int flags = (TFF_SCOPE | TFF_DECL_SPECIFIERS);
10003 : 197 : const char *str = decl_as_string (t, flags);
10004 : 197 : return get_identifier (str);
10005 : : }
10006 : :
10007 : : /* Stream out tree node T. We automatically create local back
10008 : : references, which is essentially a single pass lisp
10009 : : self-referential structure pretty-printer. */
10010 : :
10011 : : void
10012 : 206591451 : trees_out::tree_node (tree t)
10013 : : {
10014 : 206591451 : dump.indent ();
10015 : 206591451 : walk_kind ref = ref_node (t);
10016 : 206591451 : if (ref == WK_none)
10017 : 158878111 : goto done;
10018 : :
10019 : : /* Find TU-local entities and intercept streaming to instead write a
10020 : : placeholder value; this way we don't need to emit such decls.
10021 : : We only need to do this when writing a definition of an entity
10022 : : that we know names a TU-local entity. */
10023 : 55946243 : if (!is_initial_scan () && writing_local_entities)
10024 : : {
10025 : 904 : tree local_decl = NULL_TREE;
10026 : 904 : if (DECL_P (t) && has_tu_local_dep (t))
10027 : : local_decl = t;
10028 : : /* Consider a type to be TU-local if it refers to any TU-local decl,
10029 : : no matter how deep.
10030 : :
10031 : : This worsens diagnostics slightly, as we often no longer point
10032 : : directly to the at-fault entity when instantiating. However, this
10033 : : reduces the module size slightly and means that much less of pt.cc
10034 : : needs to know about us. */
10035 : 806 : else if (TYPE_P (t))
10036 : 142 : local_decl = find_tu_local_decl (t);
10037 : 664 : else if (EXPR_P (t))
10038 : 226 : local_decl = find_tu_local_decl (TREE_TYPE (t));
10039 : :
10040 : 466 : if (local_decl)
10041 : : {
10042 : 152 : int tag = insert (t, WK_value);
10043 : 152 : if (streaming_p ())
10044 : : {
10045 : 152 : tu_local_count++;
10046 : 152 : i (tt_tu_local);
10047 : 152 : dump (dumper::TREE)
10048 : 0 : && dump ("Writing TU-local entity:%d %C:%N",
10049 : 0 : tag, TREE_CODE (t), t);
10050 : : }
10051 : 152 : tree_node (name_for_tu_local_decl (local_decl));
10052 : 152 : if (state)
10053 : 152 : state->write_location (*this, DECL_SOURCE_LOCATION (local_decl));
10054 : 152 : goto done;
10055 : : }
10056 : : }
10057 : :
10058 : 47713188 : if (ref != WK_normal)
10059 : 1232465 : goto skip_normal;
10060 : :
10061 : 46480723 : if (TREE_CODE (t) == IDENTIFIER_NODE)
10062 : : {
10063 : : /* An identifier node -> tt_id, tt_conv_id, tt_anon_id, tt_lambda_id,
10064 : : tt_internal_id. */
10065 : 5810550 : int code = tt_id;
10066 : 5810550 : if (IDENTIFIER_ANON_P (t))
10067 : 21737 : code = IDENTIFIER_LAMBDA_P (t) ? tt_lambda_id : tt_anon_id;
10068 : 5788813 : else if (IDENTIFIER_INTERNAL_P (t))
10069 : : code = tt_internal_id;
10070 : 5788799 : else if (IDENTIFIER_CONV_OP_P (t))
10071 : 8270 : code = tt_conv_id;
10072 : :
10073 : 5810550 : if (streaming_p ())
10074 : 1187846 : i (code);
10075 : :
10076 : 5810550 : if (code == tt_conv_id)
10077 : : {
10078 : 8270 : tree type = TREE_TYPE (t);
10079 : 8270 : gcc_checking_assert (type || t == conv_op_identifier);
10080 : 8270 : tree_node (type);
10081 : : }
10082 : 5802280 : else if (code == tt_id && streaming_p ())
10083 : 1178371 : str (IDENTIFIER_POINTER (t), IDENTIFIER_LENGTH (t));
10084 : 4623909 : else if (code == tt_internal_id && streaming_p ())
10085 : 7 : str (prefix_for_internal_label (t));
10086 : :
10087 : 5810550 : int tag = insert (t);
10088 : 5810550 : if (streaming_p ())
10089 : : {
10090 : : /* We know the ordering of the 5 id tags. */
10091 : 1187846 : static const char *const kinds[] =
10092 : : {"", "conv_op ", "anon ", "lambda ", "internal "};
10093 : 1187846 : dump (dumper::TREE)
10094 : 1074 : && dump ("Written:%d %sidentifier:%N", tag,
10095 : 1071 : kinds[code - tt_id],
10096 : 3 : code == tt_conv_id ? TREE_TYPE (t) : t);
10097 : : }
10098 : 5810550 : goto done;
10099 : : }
10100 : :
10101 : 40670173 : if (TREE_CODE (t) == TREE_BINFO)
10102 : : {
10103 : : /* A BINFO -> tt_binfo.
10104 : : We must do this by reference. We stream the binfo tree
10105 : : itself when streaming its owning RECORD_TYPE. That we got
10106 : : here means the dominating type is not in this SCC. */
10107 : 46768 : if (streaming_p ())
10108 : 1192 : i (tt_binfo);
10109 : 46768 : binfo_mergeable (t);
10110 : 46768 : gcc_checking_assert (!TREE_VISITED (t));
10111 : 46768 : int tag = insert (t);
10112 : 46768 : if (streaming_p ())
10113 : 1192 : dump (dumper::TREE) && dump ("Inserting binfo:%d %N", tag, t);
10114 : 46768 : goto done;
10115 : : }
10116 : :
10117 : 40623405 : if (TREE_CODE (t) == INTEGER_CST
10118 : 3044602 : && !TREE_OVERFLOW (t)
10119 : 43668007 : && TREE_CODE (TREE_TYPE (t)) == ENUMERAL_TYPE)
10120 : : {
10121 : : /* An integral constant of enumeral type. See if it matches one
10122 : : of the enumeration values. */
10123 : 25763 : for (tree values = TYPE_VALUES (TREE_TYPE (t));
10124 : 699171 : values; values = TREE_CHAIN (values))
10125 : : {
10126 : 697379 : tree decl = TREE_VALUE (values);
10127 : 697379 : if (tree_int_cst_equal (DECL_INITIAL (decl), t))
10128 : : {
10129 : 23971 : if (streaming_p ())
10130 : 6911 : u (tt_enum_value);
10131 : 23971 : tree_node (decl);
10132 : 24013 : dump (dumper::TREE) && dump ("Written enum value %N", decl);
10133 : 23971 : goto done;
10134 : : }
10135 : : }
10136 : : /* It didn't match. We'll write it a an explicit INTEGER_CST
10137 : : node. */
10138 : : }
10139 : :
10140 : 40599434 : if (TYPE_P (t))
10141 : : {
10142 : 7809647 : type_node (t);
10143 : 7809647 : goto done;
10144 : : }
10145 : :
10146 : 32789787 : if (DECL_P (t))
10147 : : {
10148 : 10146557 : if (DECL_TEMPLATE_PARM_P (t))
10149 : : {
10150 : 1622180 : tpl_parm_value (t);
10151 : 1622180 : goto done;
10152 : : }
10153 : :
10154 : 8524377 : if (!DECL_CONTEXT (t))
10155 : : {
10156 : : /* There are a few cases of decls with no context. We'll write
10157 : : these by value, but first assert they are cases we expect. */
10158 : 20226 : gcc_checking_assert (ref == WK_normal);
10159 : 20226 : switch (TREE_CODE (t))
10160 : : {
10161 : 0 : default: gcc_unreachable ();
10162 : :
10163 : 6394 : case LABEL_DECL:
10164 : : /* CASE_LABEL_EXPRs contain uncontexted LABEL_DECLs. */
10165 : 6394 : gcc_checking_assert (!DECL_NAME (t));
10166 : : break;
10167 : :
10168 : 588 : case VAR_DECL:
10169 : : /* AGGR_INIT_EXPRs cons up anonymous uncontexted VAR_DECLs,
10170 : : and internal vars are created by sanitizers and
10171 : : __builtin_source_location. */
10172 : 588 : gcc_checking_assert ((!DECL_NAME (t)
10173 : : || IDENTIFIER_INTERNAL_P (DECL_NAME (t)))
10174 : : && DECL_ARTIFICIAL (t));
10175 : : break;
10176 : :
10177 : : case PARM_DECL:
10178 : : /* REQUIRES_EXPRs have a tree list of uncontexted
10179 : : PARM_DECLS. It'd be nice if they had a
10180 : : distinguishing flag to double check. */
10181 : : break;
10182 : :
10183 : 28 : case TYPE_DECL:
10184 : : /* Some parts of the compiler need internal struct types;
10185 : : these types may not have an appropriate context to use.
10186 : : Walk the whole type (including its definition) by value. */
10187 : 28 : gcc_checking_assert (DECL_ARTIFICIAL (t)
10188 : : && TYPE_ARTIFICIAL (TREE_TYPE (t))
10189 : : && RECORD_OR_UNION_TYPE_P (TREE_TYPE (t))
10190 : : && !CLASS_TYPE_P (TREE_TYPE (t)));
10191 : : break;
10192 : : }
10193 : 20226 : mark_declaration (t, has_definition (t));
10194 : 20226 : goto by_value;
10195 : : }
10196 : : }
10197 : :
10198 : 22643230 : skip_normal:
10199 : 32379846 : if (DECL_P (t) && !decl_node (t, ref))
10200 : 8906686 : goto done;
10201 : :
10202 : : /* Otherwise by value */
10203 : 23493386 : by_value:
10204 : 23493386 : tree_value (t);
10205 : :
10206 : 206591451 : done:
10207 : : /* And, breath out. */
10208 : 206591451 : dump.outdent ();
10209 : 206591451 : }
10210 : :
10211 : : /* Stream in a tree node. */
10212 : :
10213 : : tree
10214 : 67378743 : trees_in::tree_node (bool is_use)
10215 : : {
10216 : 67378743 : if (get_overrun ())
10217 : : return NULL_TREE;
10218 : :
10219 : 67378743 : dump.indent ();
10220 : 67378743 : int tag = i ();
10221 : 67378743 : tree res = NULL_TREE;
10222 : 67378743 : switch (tag)
10223 : : {
10224 : 22144114 : default:
10225 : : /* backref, pull it out of the map. */
10226 : 22144114 : res = back_ref (tag);
10227 : 22144114 : break;
10228 : :
10229 : : case tt_null:
10230 : : /* NULL_TREE. */
10231 : : break;
10232 : :
10233 : 152 : case tt_tu_local:
10234 : 152 : {
10235 : : /* A translation-unit-local entity. */
10236 : 152 : res = make_node (TU_LOCAL_ENTITY);
10237 : 152 : int tag = insert (res);
10238 : :
10239 : 152 : TU_LOCAL_ENTITY_NAME (res) = tree_node ();
10240 : 152 : TU_LOCAL_ENTITY_LOCATION (res) = state->read_location (*this);
10241 : 152 : dump (dumper::TREE) && dump ("Read TU-local entity:%d %N", tag, res);
10242 : : }
10243 : : break;
10244 : :
10245 : 5164466 : case tt_fixed:
10246 : : /* A fixed ref, find it in the fixed_ref array. */
10247 : 5164466 : {
10248 : 5164466 : unsigned fix = u ();
10249 : 5164466 : if (fix < (*fixed_trees).length ())
10250 : : {
10251 : 5164466 : res = (*fixed_trees)[fix];
10252 : 5164466 : dump (dumper::TREE) && dump ("Read fixed:%u %C:%N%S", fix,
10253 : 5046 : TREE_CODE (res), res, res);
10254 : : }
10255 : :
10256 : 5159420 : if (!res)
10257 : 0 : set_overrun ();
10258 : : }
10259 : : break;
10260 : :
10261 : 53027 : case tt_parm:
10262 : 53027 : {
10263 : 53027 : tree fn = tree_node ();
10264 : 53027 : if (fn && TREE_CODE (fn) == FUNCTION_DECL)
10265 : 53027 : res = tree_node ();
10266 : 53027 : if (res)
10267 : 53027 : dump (dumper::TREE)
10268 : 21 : && dump ("Read %s reference %N",
10269 : 21 : TREE_CODE (res) == PARM_DECL ? "parameter" : "result",
10270 : : res);
10271 : : }
10272 : : break;
10273 : :
10274 : 8058643 : case tt_node:
10275 : : /* A new node. Stream it in. */
10276 : 8058643 : res = tree_value ();
10277 : 8058643 : break;
10278 : :
10279 : 867779 : case tt_decl:
10280 : : /* A new decl. Stream it in. */
10281 : 867779 : res = decl_value ();
10282 : 867779 : break;
10283 : :
10284 : 298653 : case tt_tpl_parm:
10285 : : /* A template parameter. Stream it in. */
10286 : 298653 : res = tpl_parm_value ();
10287 : 298653 : break;
10288 : :
10289 : 921556 : case tt_id:
10290 : : /* An identifier node. */
10291 : 921556 : {
10292 : 921556 : size_t l;
10293 : 921556 : const char *chars = str (&l);
10294 : 921556 : res = get_identifier_with_length (chars, l);
10295 : 921556 : int tag = insert (res);
10296 : 921556 : dump (dumper::TREE)
10297 : 1488 : && dump ("Read identifier:%d %N", tag, res);
10298 : : }
10299 : 921556 : break;
10300 : :
10301 : 2269 : case tt_conv_id:
10302 : : /* A conversion operator. Get the type and recreate the
10303 : : identifier. */
10304 : 2269 : {
10305 : 2269 : tree type = tree_node ();
10306 : 2269 : if (!get_overrun ())
10307 : : {
10308 : 2269 : res = type ? make_conv_op_name (type) : conv_op_identifier;
10309 : 2269 : int tag = insert (res);
10310 : 2269 : dump (dumper::TREE)
10311 : 27 : && dump ("Created conv_op:%d %S for %N", tag, res, type);
10312 : : }
10313 : : }
10314 : : break;
10315 : :
10316 : 5190 : case tt_anon_id:
10317 : 5190 : case tt_lambda_id:
10318 : : /* An anonymous or lambda id. */
10319 : 5190 : {
10320 : 5190 : res = make_anon_name ();
10321 : 5190 : if (tag == tt_lambda_id)
10322 : 2892 : IDENTIFIER_LAMBDA_P (res) = true;
10323 : 5190 : int tag = insert (res);
10324 : 5193 : dump (dumper::TREE)
10325 : 3 : && dump ("Read %s identifier:%d %N",
10326 : 3 : IDENTIFIER_LAMBDA_P (res) ? "lambda" : "anon", tag, res);
10327 : : }
10328 : : break;
10329 : :
10330 : 8 : case tt_internal_id:
10331 : : /* An internal label. */
10332 : 8 : {
10333 : 8 : const char *prefix = str ();
10334 : 8 : res = generate_internal_label (prefix);
10335 : 8 : int tag = insert (res);
10336 : 8 : dump (dumper::TREE)
10337 : 1 : && dump ("Read internal identifier:%d %N", tag, res);
10338 : : }
10339 : : break;
10340 : :
10341 : 132287 : case tt_typedef_type:
10342 : 132287 : res = tree_node ();
10343 : 132287 : if (res)
10344 : : {
10345 : 132287 : dump (dumper::TREE)
10346 : 74 : && dump ("Read %stypedef %C:%N",
10347 : 74 : DECL_IMPLICIT_TYPEDEF_P (res) ? "implicit " : "",
10348 : 74 : TREE_CODE (res), res);
10349 : 132287 : if (TREE_CODE (res) != TU_LOCAL_ENTITY)
10350 : 132286 : res = TREE_TYPE (res);
10351 : : }
10352 : : break;
10353 : :
10354 : 1031374 : case tt_derived_type:
10355 : : /* A type derived from some other type. */
10356 : 1031374 : {
10357 : 1031374 : enum tree_code code = tree_code (u ());
10358 : 1031374 : res = tree_node ();
10359 : :
10360 : 1031374 : switch (code)
10361 : : {
10362 : 0 : default:
10363 : 0 : set_overrun ();
10364 : 0 : break;
10365 : :
10366 : 16582 : case ARRAY_TYPE:
10367 : 16582 : {
10368 : 16582 : tree domain = tree_node ();
10369 : 16582 : int dep = u ();
10370 : 16582 : if (!get_overrun ())
10371 : 16582 : res = build_cplus_array_type (res, domain, dep);
10372 : : }
10373 : : break;
10374 : :
10375 : 87 : case COMPLEX_TYPE:
10376 : 87 : if (!get_overrun ())
10377 : 87 : res = build_complex_type (res);
10378 : : break;
10379 : :
10380 : 9 : case BOOLEAN_TYPE:
10381 : 9 : {
10382 : 9 : unsigned precision = u ();
10383 : 9 : if (!get_overrun ())
10384 : 9 : res = build_nonstandard_boolean_type (precision);
10385 : : }
10386 : : break;
10387 : :
10388 : 15153 : case INTEGER_TYPE:
10389 : 15153 : if (res)
10390 : : {
10391 : : /* A range type (representing an array domain). */
10392 : 14520 : tree min = tree_node ();
10393 : 14520 : tree max = tree_node ();
10394 : :
10395 : 14520 : if (!get_overrun ())
10396 : 14520 : res = build_range_type (res, min, max);
10397 : : }
10398 : : else
10399 : : {
10400 : : /* A new integral type (representing a bitfield). */
10401 : 633 : unsigned enc = u ();
10402 : 633 : if (!get_overrun ())
10403 : 633 : res = build_nonstandard_integer_type (enc >> 1, enc & 1);
10404 : : }
10405 : : break;
10406 : :
10407 : 293332 : case FUNCTION_TYPE:
10408 : 293332 : case METHOD_TYPE:
10409 : 293332 : {
10410 : 293332 : tree klass = code == METHOD_TYPE ? tree_node () : NULL_TREE;
10411 : 293332 : tree args = tree_node ();
10412 : 293332 : if (!get_overrun ())
10413 : : {
10414 : 293332 : if (klass)
10415 : 179388 : res = build_method_type_directly (klass, res, args);
10416 : : else
10417 : 113944 : res = cp_build_function_type (res, args);
10418 : : }
10419 : : }
10420 : : break;
10421 : :
10422 : 228 : case OFFSET_TYPE:
10423 : 228 : {
10424 : 228 : tree base = tree_node ();
10425 : 228 : if (!get_overrun ())
10426 : 228 : res = build_offset_type (base, res);
10427 : : }
10428 : : break;
10429 : :
10430 : 142641 : case POINTER_TYPE:
10431 : 142641 : if (!get_overrun ())
10432 : 142641 : res = build_pointer_type (res);
10433 : : break;
10434 : :
10435 : 117157 : case REFERENCE_TYPE:
10436 : 117157 : {
10437 : 117157 : bool rval = bool (u ());
10438 : 117157 : if (!get_overrun ())
10439 : 117157 : res = cp_build_reference_type (res, rval);
10440 : : }
10441 : : break;
10442 : :
10443 : 311454 : case DECLTYPE_TYPE:
10444 : 311454 : case TYPEOF_TYPE:
10445 : 311454 : case DEPENDENT_OPERATOR_TYPE:
10446 : 311454 : {
10447 : 311454 : tree expr = tree_node ();
10448 : 311454 : if (!get_overrun ())
10449 : : {
10450 : 311454 : res = cxx_make_type (code);
10451 : 311454 : TYPE_VALUES_RAW (res) = expr;
10452 : 311454 : if (code == DECLTYPE_TYPE)
10453 : 15595 : tree_node_bools (res);
10454 : 311454 : SET_TYPE_STRUCTURAL_EQUALITY (res);
10455 : : }
10456 : : }
10457 : : break;
10458 : :
10459 : 2040 : case TRAIT_TYPE:
10460 : 2040 : {
10461 : 2040 : tree kind = tree_node ();
10462 : 2040 : tree type1 = tree_node ();
10463 : 2040 : tree type2 = tree_node ();
10464 : 2040 : if (!get_overrun ())
10465 : : {
10466 : 2040 : res = cxx_make_type (TRAIT_TYPE);
10467 : 2040 : TRAIT_TYPE_KIND_RAW (res) = kind;
10468 : 2040 : TRAIT_TYPE_TYPE1 (res) = type1;
10469 : 2040 : TRAIT_TYPE_TYPE2 (res) = type2;
10470 : 2040 : SET_TYPE_STRUCTURAL_EQUALITY (res);
10471 : : }
10472 : : }
10473 : : break;
10474 : :
10475 : 42604 : case TYPE_ARGUMENT_PACK:
10476 : 42604 : if (!get_overrun ())
10477 : : {
10478 : 42604 : tree pack = cxx_make_type (TYPE_ARGUMENT_PACK);
10479 : 42604 : ARGUMENT_PACK_ARGS (pack) = res;
10480 : : res = pack;
10481 : : }
10482 : : break;
10483 : :
10484 : 42528 : case TYPE_PACK_EXPANSION:
10485 : 42528 : {
10486 : 42528 : bool local = u ();
10487 : 42528 : tree param_packs = tree_node ();
10488 : 42528 : tree extra_args = tree_node ();
10489 : 42528 : if (!get_overrun ())
10490 : : {
10491 : 42528 : tree expn = cxx_make_type (TYPE_PACK_EXPANSION);
10492 : 42528 : SET_TYPE_STRUCTURAL_EQUALITY (expn);
10493 : 42528 : PACK_EXPANSION_PATTERN (expn) = res;
10494 : 85056 : PACK_EXPANSION_PARAMETER_PACKS (expn) = param_packs;
10495 : 42528 : PACK_EXPANSION_EXTRA_ARGS (expn) = extra_args;
10496 : 42528 : PACK_EXPANSION_LOCAL_P (expn) = local;
10497 : 42528 : res = expn;
10498 : : }
10499 : : }
10500 : : break;
10501 : :
10502 : 13 : case PACK_INDEX_TYPE:
10503 : 13 : {
10504 : 13 : tree pack = tree_node ();
10505 : 13 : tree index = tree_node ();
10506 : 13 : if (!get_overrun ())
10507 : 13 : res = make_pack_index (pack, index);
10508 : : }
10509 : : break;
10510 : :
10511 : 47472 : case TYPENAME_TYPE:
10512 : 47472 : {
10513 : 47472 : tree ctx = tree_node ();
10514 : 47472 : tree name = tree_node ();
10515 : 47472 : tree fullname = tree_node ();
10516 : 47472 : enum tag_types tag_type = tag_types (u ());
10517 : :
10518 : 47472 : if (!get_overrun ())
10519 : 47472 : res = build_typename_type (ctx, name, fullname, tag_type);
10520 : : }
10521 : : break;
10522 : :
10523 : 44 : case UNBOUND_CLASS_TEMPLATE:
10524 : 44 : {
10525 : 44 : tree ctx = tree_node ();
10526 : 44 : tree name = tree_node ();
10527 : 44 : tree parms = tree_node ();
10528 : :
10529 : 44 : if (!get_overrun ())
10530 : 44 : res = make_unbound_class_template_raw (ctx, name, parms);
10531 : : }
10532 : : break;
10533 : :
10534 : : case VECTOR_TYPE:
10535 : : {
10536 : : poly_uint64 nunits;
10537 : 60 : for (unsigned ix = 0; ix != NUM_POLY_INT_COEFFS; ix++)
10538 : 30 : nunits.coeffs[ix] = wu ();
10539 : 30 : if (!get_overrun ())
10540 : 30 : res = build_vector_type (res, nunits);
10541 : : }
10542 : : break;
10543 : : }
10544 : :
10545 : : /* In the exporting TU, a derived type with attributes was built by
10546 : : build_type_attribute_variant as a distinct copy, with itself as
10547 : : TYPE_MAIN_VARIANT. We repeat that on import to get the version
10548 : : without attributes as TYPE_CANONICAL. */
10549 : 1031374 : if (tree attribs = tree_node ())
10550 : 13204 : res = cp_build_type_attribute_variant (res, attribs);
10551 : :
10552 : 1031374 : int tag = i ();
10553 : 1031374 : if (!tag)
10554 : : {
10555 : 883446 : tag = insert (res);
10556 : 883446 : if (res)
10557 : 883446 : dump (dumper::TREE)
10558 : 678 : && dump ("Created:%d derived type %C", tag, code);
10559 : : }
10560 : : else
10561 : 147928 : res = back_ref (tag);
10562 : : }
10563 : : break;
10564 : :
10565 : 304455 : case tt_variant_type:
10566 : : /* Variant of some type. */
10567 : 304455 : {
10568 : 304455 : res = tree_node ();
10569 : 304455 : int flags = i ();
10570 : 304455 : if (get_overrun ())
10571 : : ;
10572 : 304455 : else if (flags < 0)
10573 : : /* No change. */;
10574 : 147302 : else if (TREE_CODE (res) == FUNCTION_TYPE
10575 : 147302 : || TREE_CODE (res) == METHOD_TYPE)
10576 : : {
10577 : 146012 : cp_ref_qualifier rqual = cp_ref_qualifier (flags & 3);
10578 : 146012 : bool late = (flags >> 2) & 1;
10579 : 146012 : cp_cv_quals quals = cp_cv_quals (flags >> 3);
10580 : :
10581 : 146012 : tree raises = tree_node ();
10582 : 146012 : if (raises == error_mark_node)
10583 : 5017 : raises = TYPE_RAISES_EXCEPTIONS (res);
10584 : :
10585 : 146012 : res = build_cp_fntype_variant (res, rqual, raises, late);
10586 : 146012 : if (TREE_CODE (res) == FUNCTION_TYPE)
10587 : 56026 : res = apply_memfn_quals (res, quals, rqual);
10588 : : }
10589 : : else
10590 : : {
10591 : 1290 : res = build_aligned_type (res, (1u << flags) >> 1);
10592 : 1290 : TYPE_USER_ALIGN (res) = true;
10593 : : }
10594 : :
10595 : 304455 : int quals = i ();
10596 : 304455 : if (quals >= 0 && !get_overrun ())
10597 : 158121 : res = cp_build_qualified_type (res, quals);
10598 : :
10599 : 304455 : int tag = i ();
10600 : 304455 : if (!tag)
10601 : : {
10602 : 304455 : tag = insert (res);
10603 : 304455 : if (res)
10604 : 304455 : dump (dumper::TREE)
10605 : 292 : && dump ("Created:%d variant type %C", tag, TREE_CODE (res));
10606 : : }
10607 : : else
10608 : 0 : res = back_ref (tag);
10609 : : }
10610 : : break;
10611 : :
10612 : 3816 : case tt_tinfo_var:
10613 : 3816 : case tt_tinfo_typedef:
10614 : : /* A tinfo var or typedef. */
10615 : 3816 : {
10616 : 3816 : bool is_var = tag == tt_tinfo_var;
10617 : 3816 : unsigned ix = u ();
10618 : 3816 : tree type = NULL_TREE;
10619 : :
10620 : 3816 : if (is_var)
10621 : : {
10622 : 2278 : tree name = tree_node ();
10623 : 2278 : type = tree_node ();
10624 : :
10625 : 2278 : if (!get_overrun ())
10626 : 2278 : res = get_tinfo_decl_direct (type, name, int (ix));
10627 : : }
10628 : : else
10629 : : {
10630 : 1538 : if (!get_overrun ())
10631 : : {
10632 : 1538 : type = get_pseudo_tinfo_type (ix);
10633 : 1538 : res = TYPE_NAME (type);
10634 : : }
10635 : : }
10636 : 3816 : if (res)
10637 : : {
10638 : 3816 : int tag = insert (res);
10639 : 3816 : dump (dumper::TREE)
10640 : 36 : && dump ("Created tinfo_%s:%d %S:%u for %N",
10641 : : is_var ? "var" : "decl", tag, res, ix, type);
10642 : 3816 : if (!is_var)
10643 : : {
10644 : 1538 : tag = insert (type);
10645 : 1538 : dump (dumper::TREE)
10646 : 12 : && dump ("Created tinfo_type:%d %u %N", tag, ix, type);
10647 : : }
10648 : : }
10649 : : }
10650 : : break;
10651 : :
10652 : 1001 : case tt_ptrmem_type:
10653 : : /* A pointer to member function. */
10654 : 1001 : {
10655 : 1001 : tree type = tree_node ();
10656 : 1001 : if (type && TREE_CODE (type) == POINTER_TYPE
10657 : 2002 : && TREE_CODE (TREE_TYPE (type)) == METHOD_TYPE)
10658 : : {
10659 : 1001 : res = build_ptrmemfunc_type (type);
10660 : 1001 : int tag = insert (res);
10661 : 1004 : dump (dumper::TREE) && dump ("Created:%d ptrmem type", tag);
10662 : : }
10663 : : else
10664 : 0 : set_overrun ();
10665 : : }
10666 : : break;
10667 : :
10668 : 6 : case tt_nttp_var:
10669 : : /* An NTTP object. */
10670 : 6 : {
10671 : 6 : tree init = tree_node ();
10672 : 6 : tree name = tree_node ();
10673 : 6 : if (!get_overrun ())
10674 : : {
10675 : : /* We don't want to check the initializer as that may require
10676 : : name lookup, which could recursively start lazy loading.
10677 : : Instead we know that INIT is already valid so we can just
10678 : : apply that directly. */
10679 : 6 : res = get_template_parm_object (init, name, /*check_init=*/false);
10680 : 6 : int tag = insert (res);
10681 : 6 : dump (dumper::TREE)
10682 : 0 : && dump ("Created nttp object:%d %N", tag, name);
10683 : : }
10684 : : }
10685 : : break;
10686 : :
10687 : 5243 : case tt_enum_value:
10688 : : /* An enum const value. */
10689 : 5243 : {
10690 : 5243 : if (tree decl = tree_node ())
10691 : : {
10692 : 5261 : dump (dumper::TREE) && dump ("Read enum value %N", decl);
10693 : 5243 : res = DECL_INITIAL (decl);
10694 : : }
10695 : :
10696 : 5243 : if (!res)
10697 : 0 : set_overrun ();
10698 : : }
10699 : : break;
10700 : :
10701 : 8949 : case tt_enum_decl:
10702 : : /* An enum decl. */
10703 : 8949 : {
10704 : 8949 : tree ctx = tree_node ();
10705 : 8949 : tree name = tree_node ();
10706 : :
10707 : 8949 : if (!get_overrun ()
10708 : 8949 : && TREE_CODE (ctx) == ENUMERAL_TYPE)
10709 : 8949 : res = find_enum_member (ctx, name);
10710 : :
10711 : 8949 : if (!res)
10712 : 0 : set_overrun ();
10713 : : else
10714 : : {
10715 : 8949 : int tag = insert (res);
10716 : 8949 : dump (dumper::TREE)
10717 : 18 : && dump ("Read enum decl:%d %C:%N", tag, TREE_CODE (res), res);
10718 : : }
10719 : : }
10720 : : break;
10721 : :
10722 : 5734 : case tt_data_member:
10723 : : /* A data member. */
10724 : 5734 : {
10725 : 5734 : tree ctx = tree_node ();
10726 : 5734 : tree name = tree_node ();
10727 : :
10728 : 5734 : if (!get_overrun ()
10729 : 5734 : && RECORD_OR_UNION_TYPE_P (ctx))
10730 : : {
10731 : 5734 : if (name)
10732 : 5050 : res = lookup_class_binding (ctx, name);
10733 : : else
10734 : 684 : res = lookup_field_ident (ctx, u ());
10735 : :
10736 : 5734 : if (!res
10737 : 5734 : || (TREE_CODE (res) != FIELD_DECL
10738 : 5734 : && TREE_CODE (res) != USING_DECL)
10739 : 11468 : || DECL_CONTEXT (res) != ctx)
10740 : : res = NULL_TREE;
10741 : : }
10742 : :
10743 : 5734 : if (!res)
10744 : 0 : set_overrun ();
10745 : : else
10746 : : {
10747 : 5734 : int tag = insert (res);
10748 : 5734 : dump (dumper::TREE)
10749 : 26 : && dump ("Read member:%d %C:%N", tag, TREE_CODE (res), res);
10750 : : }
10751 : : }
10752 : : break;
10753 : :
10754 : 1060 : case tt_binfo:
10755 : : /* A BINFO. Walk the tree of the dominating type. */
10756 : 1060 : {
10757 : 1060 : tree type;
10758 : 1060 : unsigned ix = binfo_mergeable (&type);
10759 : 1060 : if (type)
10760 : : {
10761 : 1060 : res = TYPE_BINFO (type);
10762 : 1115 : for (; ix && res; res = TREE_CHAIN (res))
10763 : 55 : ix--;
10764 : 1060 : if (!res)
10765 : 0 : set_overrun ();
10766 : : }
10767 : :
10768 : 1060 : if (get_overrun ())
10769 : : break;
10770 : :
10771 : : /* Insert binfo into backreferences. */
10772 : 1060 : tag = insert (res);
10773 : 1060 : dump (dumper::TREE) && dump ("Read binfo:%d %N", tag, res);
10774 : : }
10775 : 1060 : break;
10776 : :
10777 : 9 : case tt_vtable:
10778 : 9 : {
10779 : 9 : unsigned ix = u ();
10780 : 9 : tree ctx = tree_node ();
10781 : 9 : dump (dumper::TREE) && dump ("Reading vtable %N[%u]", ctx, ix);
10782 : 9 : if (TREE_CODE (ctx) == RECORD_TYPE && TYPE_LANG_SPECIFIC (ctx))
10783 : 9 : for (res = CLASSTYPE_VTABLES (ctx); res; res = DECL_CHAIN (res))
10784 : 9 : if (!ix--)
10785 : : break;
10786 : 9 : if (!res)
10787 : 0 : set_overrun ();
10788 : : }
10789 : : break;
10790 : :
10791 : 0 : case tt_thunk:
10792 : 0 : {
10793 : 0 : int fixed = i ();
10794 : 0 : tree target = tree_node ();
10795 : 0 : tree virt = tree_node ();
10796 : :
10797 : 0 : for (tree thunk = DECL_THUNKS (target);
10798 : 0 : thunk; thunk = DECL_CHAIN (thunk))
10799 : 0 : if (THUNK_FIXED_OFFSET (thunk) == fixed
10800 : 0 : && !THUNK_VIRTUAL_OFFSET (thunk) == !virt
10801 : 0 : && (!virt
10802 : 0 : || tree_int_cst_equal (virt, THUNK_VIRTUAL_OFFSET (thunk))))
10803 : : {
10804 : : res = thunk;
10805 : : break;
10806 : : }
10807 : :
10808 : 0 : int tag = insert (res);
10809 : 0 : if (res)
10810 : 0 : dump (dumper::TREE)
10811 : 0 : && dump ("Read:%d thunk %N to %N", tag, DECL_NAME (res), target);
10812 : : else
10813 : 0 : set_overrun ();
10814 : : }
10815 : : break;
10816 : :
10817 : 104364 : case tt_clone_ref:
10818 : 104364 : {
10819 : 104364 : tree target = tree_node ();
10820 : 104364 : tree name = tree_node ();
10821 : :
10822 : 104364 : if (DECL_P (target) && DECL_MAYBE_IN_CHARGE_CDTOR_P (target))
10823 : : {
10824 : 104364 : tree clone;
10825 : 163103 : FOR_EVERY_CLONE (clone, target)
10826 : 163103 : if (DECL_NAME (clone) == name)
10827 : : {
10828 : : res = clone;
10829 : : break;
10830 : : }
10831 : : }
10832 : :
10833 : : /* A clone might have a different vtable entry. */
10834 : 104364 : if (res && DECL_VIRTUAL_P (res))
10835 : 6684 : DECL_VINDEX (res) = tree_node ();
10836 : :
10837 : 104364 : if (!res)
10838 : 0 : set_overrun ();
10839 : 104364 : int tag = insert (res);
10840 : 104364 : if (res)
10841 : 104364 : dump (dumper::TREE)
10842 : 230 : && dump ("Read:%d clone %N of %N", tag, DECL_NAME (res), target);
10843 : : else
10844 : 0 : set_overrun ();
10845 : : }
10846 : : break;
10847 : :
10848 : 778343 : case tt_entity:
10849 : : /* Index into the entity table. Perhaps not loaded yet! */
10850 : 778343 : {
10851 : 778343 : unsigned origin = state->slurp->remap_module (u ());
10852 : 778343 : unsigned ident = u ();
10853 : 778343 : module_state *from = (*modules)[origin];
10854 : :
10855 : 778343 : if (!origin || ident >= from->entity_num)
10856 : 0 : set_overrun ();
10857 : 778343 : if (!get_overrun ())
10858 : : {
10859 : 778343 : binding_slot *slot = &(*entity_ary)[from->entity_lwm + ident];
10860 : 778343 : if (slot->is_lazy ())
10861 : 23128 : if (!from->lazy_load (ident, slot))
10862 : 0 : set_overrun ();
10863 : 778343 : res = *slot;
10864 : : }
10865 : :
10866 : 778343 : if (res)
10867 : : {
10868 : 778343 : const char *kind = (origin != state->mod ? "Imported" : "Named");
10869 : 778343 : int tag = insert (res);
10870 : 778343 : dump (dumper::TREE)
10871 : 605 : && dump ("%s:%d %C:%N@%M", kind, tag, TREE_CODE (res),
10872 : 605 : res, (*modules)[origin]);
10873 : :
10874 : 778343 : if (!add_indirects (res))
10875 : : {
10876 : 0 : set_overrun ();
10877 : 0 : res = NULL_TREE;
10878 : : }
10879 : : }
10880 : : }
10881 : : break;
10882 : :
10883 : 2045 : case tt_template:
10884 : : /* A template. */
10885 : 2045 : if (tree tpl = tree_node ())
10886 : : {
10887 : 2045 : res = (TREE_CODE (tpl) == TU_LOCAL_ENTITY ?
10888 : 2045 : tpl : DECL_TEMPLATE_RESULT (tpl));
10889 : 2045 : dump (dumper::TREE)
10890 : 9 : && dump ("Read template %C:%N", TREE_CODE (res), res);
10891 : : }
10892 : : break;
10893 : : }
10894 : :
10895 : 67378743 : if (is_use && !unused && res && DECL_P (res) && !TREE_USED (res))
10896 : : {
10897 : : /* Mark decl used as mark_used does -- we cannot call
10898 : : mark_used in the middle of streaming, we only need a subset
10899 : : of its functionality. */
10900 : 468230 : TREE_USED (res) = true;
10901 : :
10902 : : /* And for structured bindings also the underlying decl. */
10903 : 468230 : if (DECL_DECOMPOSITION_P (res) && !DECL_DECOMP_IS_BASE (res))
10904 : 860 : TREE_USED (DECL_DECOMP_BASE (res)) = true;
10905 : :
10906 : 468230 : if (DECL_CLONED_FUNCTION_P (res))
10907 : 4204 : TREE_USED (DECL_CLONED_FUNCTION (res)) = true;
10908 : : }
10909 : :
10910 : 67378743 : dump.outdent ();
10911 : 67378743 : return res;
10912 : : }
10913 : :
10914 : : void
10915 : 1376653 : trees_out::tpl_parms (tree parms, unsigned &tpl_levels)
10916 : : {
10917 : 1376653 : if (!parms)
10918 : : return;
10919 : :
10920 : 924158 : if (TREE_VISITED (parms))
10921 : : {
10922 : 386124 : ref_node (parms);
10923 : 386124 : return;
10924 : : }
10925 : :
10926 : 538034 : tpl_parms (TREE_CHAIN (parms), tpl_levels);
10927 : :
10928 : 538034 : tree vec = TREE_VALUE (parms);
10929 : 538034 : unsigned len = TREE_VEC_LENGTH (vec);
10930 : : /* Depth. */
10931 : 538034 : int tag = insert (parms);
10932 : 538034 : if (streaming_p ())
10933 : : {
10934 : 145689 : i (len + 1);
10935 : 145755 : dump (dumper::TREE)
10936 : 66 : && dump ("Writing template parms:%d level:%N length:%d",
10937 : 66 : tag, TREE_PURPOSE (parms), len);
10938 : : }
10939 : 538034 : tree_node (TREE_PURPOSE (parms));
10940 : :
10941 : 1471045 : for (unsigned ix = 0; ix != len; ix++)
10942 : : {
10943 : 933011 : tree parm = TREE_VEC_ELT (vec, ix);
10944 : 933011 : tree decl = TREE_VALUE (parm);
10945 : :
10946 : 933011 : gcc_checking_assert (DECL_TEMPLATE_PARM_P (decl));
10947 : 933011 : if (CHECKING_P)
10948 : 933011 : switch (TREE_CODE (decl))
10949 : : {
10950 : 0 : default: gcc_unreachable ();
10951 : :
10952 : 2757 : case TEMPLATE_DECL:
10953 : 2757 : gcc_assert ((TREE_CODE (TREE_TYPE (decl)) == TEMPLATE_TEMPLATE_PARM)
10954 : : && (TREE_CODE (DECL_TEMPLATE_RESULT (decl)) == TYPE_DECL)
10955 : : && (TYPE_NAME (TREE_TYPE (decl)) == decl));
10956 : : break;
10957 : :
10958 : 872590 : case TYPE_DECL:
10959 : 872590 : gcc_assert ((TREE_CODE (TREE_TYPE (decl)) == TEMPLATE_TYPE_PARM)
10960 : : && (TYPE_NAME (TREE_TYPE (decl)) == decl));
10961 : : break;
10962 : :
10963 : 57664 : case PARM_DECL:
10964 : 57664 : gcc_assert ((TREE_CODE (DECL_INITIAL (decl)) == TEMPLATE_PARM_INDEX)
10965 : : && (TREE_CODE (TEMPLATE_PARM_DECL (DECL_INITIAL (decl)))
10966 : : == CONST_DECL)
10967 : : && (DECL_TEMPLATE_PARM_P
10968 : : (TEMPLATE_PARM_DECL (DECL_INITIAL (decl)))));
10969 : : break;
10970 : : }
10971 : :
10972 : 933011 : tree_node (decl);
10973 : 933011 : tree_node (TEMPLATE_PARM_CONSTRAINTS (parm));
10974 : : }
10975 : :
10976 : 538034 : tpl_levels++;
10977 : : }
10978 : :
10979 : : tree
10980 : 232360 : trees_in::tpl_parms (unsigned &tpl_levels)
10981 : : {
10982 : 232360 : tree parms = NULL_TREE;
10983 : :
10984 : 487613 : while (int len = i ())
10985 : : {
10986 : 255253 : if (len < 0)
10987 : : {
10988 : 137442 : parms = back_ref (len);
10989 : 137442 : continue;
10990 : : }
10991 : :
10992 : 117811 : len -= 1;
10993 : 117811 : parms = tree_cons (NULL_TREE, NULL_TREE, parms);
10994 : 117811 : int tag = insert (parms);
10995 : 117811 : TREE_PURPOSE (parms) = tree_node ();
10996 : :
10997 : 117811 : dump (dumper::TREE)
10998 : 105 : && dump ("Reading template parms:%d level:%N length:%d",
10999 : 105 : tag, TREE_PURPOSE (parms), len);
11000 : :
11001 : 117811 : tree vec = make_tree_vec (len);
11002 : 312357 : for (int ix = 0; ix != len; ix++)
11003 : : {
11004 : 194546 : tree decl = tree_node ();
11005 : 194546 : if (!decl)
11006 : : return NULL_TREE;
11007 : :
11008 : 194546 : tree parm = build_tree_list (NULL, decl);
11009 : 194546 : TEMPLATE_PARM_CONSTRAINTS (parm) = tree_node ();
11010 : :
11011 : 194546 : TREE_VEC_ELT (vec, ix) = parm;
11012 : : }
11013 : :
11014 : 117811 : TREE_VALUE (parms) = vec;
11015 : 117811 : tpl_levels++;
11016 : : }
11017 : :
11018 : : return parms;
11019 : : }
11020 : :
11021 : : void
11022 : 838619 : trees_out::tpl_parms_fini (tree tmpl, unsigned tpl_levels)
11023 : : {
11024 : 838619 : for (tree parms = DECL_TEMPLATE_PARMS (tmpl);
11025 : 1376653 : tpl_levels--; parms = TREE_CHAIN (parms))
11026 : : {
11027 : 538034 : tree vec = TREE_VALUE (parms);
11028 : :
11029 : 538034 : tree_node (TREE_TYPE (vec));
11030 : 1471045 : for (unsigned ix = TREE_VEC_LENGTH (vec); ix--;)
11031 : : {
11032 : 933011 : tree parm = TREE_VEC_ELT (vec, ix);
11033 : 933011 : tree dflt = TREE_PURPOSE (parm);
11034 : 933011 : tree_node (dflt);
11035 : :
11036 : : /* Template template parameters need a context of their owning
11037 : : template. This is quite tricky to infer correctly on stream-in
11038 : : (see PR c++/98881) so we'll just provide it directly. */
11039 : 933011 : tree decl = TREE_VALUE (parm);
11040 : 933011 : if (TREE_CODE (decl) == TEMPLATE_DECL)
11041 : 2757 : tree_node (DECL_CONTEXT (decl));
11042 : : }
11043 : : }
11044 : 838619 : }
11045 : :
11046 : : bool
11047 : 232360 : trees_in::tpl_parms_fini (tree tmpl, unsigned tpl_levels)
11048 : : {
11049 : 232360 : for (tree parms = DECL_TEMPLATE_PARMS (tmpl);
11050 : 350171 : tpl_levels--; parms = TREE_CHAIN (parms))
11051 : : {
11052 : 117811 : tree vec = TREE_VALUE (parms);
11053 : :
11054 : 117811 : TREE_TYPE (vec) = tree_node ();
11055 : 312357 : for (unsigned ix = TREE_VEC_LENGTH (vec); ix--;)
11056 : : {
11057 : 194546 : tree parm = TREE_VEC_ELT (vec, ix);
11058 : 194546 : tree dflt = tree_node ();
11059 : 194546 : TREE_PURPOSE (parm) = dflt;
11060 : :
11061 : 194546 : tree decl = TREE_VALUE (parm);
11062 : 194546 : if (TREE_CODE (decl) == TEMPLATE_DECL)
11063 : 612 : DECL_CONTEXT (decl) = tree_node ();
11064 : :
11065 : 194546 : if (get_overrun ())
11066 : : return false;
11067 : : }
11068 : : }
11069 : : return true;
11070 : : }
11071 : :
11072 : : /* PARMS is a LIST, one node per level.
11073 : : TREE_VALUE is a TREE_VEC of parm info for that level.
11074 : : each ELT is a TREE_LIST
11075 : : TREE_VALUE is PARM_DECL, TYPE_DECL or TEMPLATE_DECL
11076 : : TREE_PURPOSE is the default value. */
11077 : :
11078 : : void
11079 : 838619 : trees_out::tpl_header (tree tpl, unsigned *tpl_levels)
11080 : : {
11081 : 838619 : tree parms = DECL_TEMPLATE_PARMS (tpl);
11082 : 838619 : tpl_parms (parms, *tpl_levels);
11083 : :
11084 : : /* Mark end. */
11085 : 838619 : if (streaming_p ())
11086 : 279162 : u (0);
11087 : :
11088 : 838619 : if (*tpl_levels)
11089 : 507661 : tree_node (TEMPLATE_PARMS_CONSTRAINTS (parms));
11090 : 838619 : }
11091 : :
11092 : : bool
11093 : 232360 : trees_in::tpl_header (tree tpl, unsigned *tpl_levels)
11094 : : {
11095 : 232360 : tree parms = tpl_parms (*tpl_levels);
11096 : 232360 : if (!parms)
11097 : : return false;
11098 : :
11099 : 232360 : DECL_TEMPLATE_PARMS (tpl) = parms;
11100 : :
11101 : 232360 : if (*tpl_levels)
11102 : 116385 : TEMPLATE_PARMS_CONSTRAINTS (parms) = tree_node ();
11103 : :
11104 : : return true;
11105 : : }
11106 : :
11107 : : /* Stream skeleton parm nodes, with their flags, type & parm indices.
11108 : : All the parms will have consecutive tags. */
11109 : :
11110 : : void
11111 : 1135119 : trees_out::fn_parms_init (tree fn)
11112 : : {
11113 : : /* First init them. */
11114 : 1135119 : int base_tag = ref_num - 1;
11115 : 1135119 : int ix = 0;
11116 : 1135119 : for (tree parm = DECL_ARGUMENTS (fn);
11117 : 3406494 : parm; parm = DECL_CHAIN (parm), ix++)
11118 : : {
11119 : 2271375 : if (streaming_p ())
11120 : : {
11121 : 757092 : start (parm);
11122 : 757092 : tree_node_bools (parm);
11123 : : }
11124 : 2271375 : int tag = insert (parm);
11125 : 2271375 : gcc_checking_assert (base_tag - ix == tag);
11126 : : }
11127 : : /* Mark the end. */
11128 : 1135119 : if (streaming_p ())
11129 : 378541 : u (0);
11130 : :
11131 : : /* Now stream their contents. */
11132 : 1135119 : ix = 0;
11133 : 1135119 : for (tree parm = DECL_ARGUMENTS (fn);
11134 : 3406494 : parm; parm = DECL_CHAIN (parm), ix++)
11135 : : {
11136 : 2271375 : if (streaming_p ())
11137 : 757092 : dump (dumper::TREE)
11138 : 222 : && dump ("Writing parm:%d %u (%N) of %N",
11139 : : base_tag - ix, ix, parm, fn);
11140 : 2271375 : tree_node_vals (parm);
11141 : : }
11142 : :
11143 : 1135119 : if (!streaming_p ())
11144 : : {
11145 : : /* We must walk contract attrs so the dependency graph is complete. */
11146 : 756578 : for (tree contract = DECL_CONTRACTS (fn);
11147 : 756686 : contract;
11148 : 108 : contract = CONTRACT_CHAIN (contract))
11149 : 108 : tree_node (contract);
11150 : : }
11151 : :
11152 : : /* Write a reference to contracts pre/post functions, if any, to avoid
11153 : : regenerating them in importers. */
11154 : 1135119 : tree_node (DECL_PRE_FN (fn));
11155 : 1135119 : tree_node (DECL_POST_FN (fn));
11156 : 1135119 : }
11157 : :
11158 : : /* Build skeleton parm nodes, read their flags, type & parm indices. */
11159 : :
11160 : : int
11161 : 324797 : trees_in::fn_parms_init (tree fn)
11162 : : {
11163 : 324797 : int base_tag = ~(int)back_refs.length ();
11164 : :
11165 : 324797 : tree *parm_ptr = &DECL_ARGUMENTS (fn);
11166 : 324797 : int ix = 0;
11167 : 976913 : for (; int code = u (); ix++)
11168 : : {
11169 : 652116 : tree parm = start (code);
11170 : 652116 : if (!tree_node_bools (parm))
11171 : : return 0;
11172 : :
11173 : 652116 : int tag = insert (parm);
11174 : 652116 : gcc_checking_assert (base_tag - ix == tag);
11175 : 652116 : *parm_ptr = parm;
11176 : 652116 : parm_ptr = &DECL_CHAIN (parm);
11177 : 652116 : }
11178 : :
11179 : 324797 : ix = 0;
11180 : 324797 : for (tree parm = DECL_ARGUMENTS (fn);
11181 : 976913 : parm; parm = DECL_CHAIN (parm), ix++)
11182 : : {
11183 : 652116 : dump (dumper::TREE)
11184 : 362 : && dump ("Reading parm:%d %u (%N) of %N",
11185 : : base_tag - ix, ix, parm, fn);
11186 : 652116 : if (!tree_node_vals (parm))
11187 : : return 0;
11188 : : }
11189 : :
11190 : : /* Reload references to contract functions, if any. */
11191 : 324797 : tree pre_fn = tree_node ();
11192 : 324797 : tree post_fn = tree_node ();
11193 : 324797 : set_contract_functions (fn, pre_fn, post_fn);
11194 : :
11195 : 324797 : return base_tag;
11196 : : }
11197 : :
11198 : : /* Read the remaining parm node data. Replace with existing (if
11199 : : non-null) in the map. */
11200 : :
11201 : : void
11202 : 324797 : trees_in::fn_parms_fini (int tag, tree fn, tree existing, bool is_defn)
11203 : : {
11204 : 513433 : tree existing_parm = existing ? DECL_ARGUMENTS (existing) : NULL_TREE;
11205 : 324797 : tree parms = DECL_ARGUMENTS (fn);
11206 : 976913 : for (tree parm = parms; parm; parm = DECL_CHAIN (parm))
11207 : : {
11208 : 652116 : if (existing_parm)
11209 : : {
11210 : 555018 : if (is_defn && !DECL_SAVED_TREE (existing))
11211 : : {
11212 : : /* If we're about to become the definition, set the
11213 : : names of the parms from us. */
11214 : 13713 : DECL_NAME (existing_parm) = DECL_NAME (parm);
11215 : 13713 : DECL_SOURCE_LOCATION (existing_parm) = DECL_SOURCE_LOCATION (parm);
11216 : :
11217 : : /* And some other flags important for codegen are only set
11218 : : by the definition. */
11219 : 13713 : TREE_ADDRESSABLE (existing_parm) = TREE_ADDRESSABLE (parm);
11220 : 13713 : DECL_BY_REFERENCE (existing_parm) = DECL_BY_REFERENCE (parm);
11221 : 13713 : DECL_NONLOCAL (existing_parm) = DECL_NONLOCAL (parm);
11222 : 13713 : DECL_ARG_TYPE (existing_parm) = DECL_ARG_TYPE (parm);
11223 : :
11224 : : /* Invisiref parms had their types adjusted by cp_genericize. */
11225 : 13713 : if (DECL_BY_REFERENCE (parm))
11226 : : {
11227 : 6 : TREE_TYPE (existing_parm) = TREE_TYPE (parm);
11228 : 6 : relayout_decl (existing_parm);
11229 : : }
11230 : : }
11231 : :
11232 : 376175 : back_refs[~tag] = existing_parm;
11233 : 376175 : existing_parm = DECL_CHAIN (existing_parm);
11234 : : }
11235 : 652116 : tag--;
11236 : : }
11237 : 324797 : }
11238 : :
11239 : : /* Encode into KEY the position of the local type (class or enum)
11240 : : declaration DECL within FN. The position is encoded as the
11241 : : index of the innermost BLOCK (numbered in BFS order) along with
11242 : : the index within its BLOCK_VARS list. */
11243 : :
11244 : : void
11245 : 10725 : trees_out::key_local_type (merge_key& key, tree decl, tree fn)
11246 : : {
11247 : 10725 : auto_vec<tree, 4> blocks;
11248 : 10725 : blocks.quick_push (DECL_INITIAL (fn));
11249 : 10725 : unsigned block_ix = 0;
11250 : 48075 : while (block_ix != blocks.length ())
11251 : : {
11252 : 18675 : tree block = blocks[block_ix];
11253 : 18675 : unsigned decl_ix = 0;
11254 : 52710 : for (tree var = BLOCK_VARS (block); var; var = DECL_CHAIN (var))
11255 : : {
11256 : 44760 : if (TREE_CODE (var) != TYPE_DECL)
11257 : 26736 : continue;
11258 : 18024 : if (var == decl)
11259 : : {
11260 : 10725 : key.index = (block_ix << 10) | decl_ix;
11261 : 10725 : return;
11262 : : }
11263 : 7299 : ++decl_ix;
11264 : : }
11265 : 16593 : for (tree sub = BLOCK_SUBBLOCKS (block); sub; sub = BLOCK_CHAIN (sub))
11266 : 8643 : blocks.safe_push (sub);
11267 : 7950 : ++block_ix;
11268 : : }
11269 : :
11270 : : /* Not-found value. */
11271 : 0 : key.index = 1023;
11272 : 10725 : }
11273 : :
11274 : : /* Look up the local type corresponding at the position encoded by
11275 : : KEY within FN and named NAME. */
11276 : :
11277 : : tree
11278 : 3206 : trees_in::key_local_type (const merge_key& key, tree fn, tree name)
11279 : : {
11280 : 3206 : if (!DECL_INITIAL (fn))
11281 : : return NULL_TREE;
11282 : :
11283 : 1953 : const unsigned block_pos = key.index >> 10;
11284 : 1953 : const unsigned decl_pos = key.index & 1023;
11285 : :
11286 : 1953 : if (decl_pos == 1023)
11287 : : return NULL_TREE;
11288 : :
11289 : 1953 : auto_vec<tree, 4> blocks;
11290 : 1953 : blocks.quick_push (DECL_INITIAL (fn));
11291 : 1953 : unsigned block_ix = 0;
11292 : 8837 : while (block_ix != blocks.length ())
11293 : : {
11294 : 3442 : tree block = blocks[block_ix];
11295 : 3442 : if (block_ix == block_pos)
11296 : : {
11297 : 1953 : unsigned decl_ix = 0;
11298 : 5055 : for (tree var = BLOCK_VARS (block); var; var = DECL_CHAIN (var))
11299 : : {
11300 : 5055 : if (TREE_CODE (var) != TYPE_DECL)
11301 : 2173 : continue;
11302 : : /* Prefer using the identifier as the key for more robustness
11303 : : to ODR violations, except for anonymous types since their
11304 : : compiler-generated identifiers aren't stable. */
11305 : 5764 : if (IDENTIFIER_ANON_P (name)
11306 : 2882 : ? decl_ix == decl_pos
11307 : 304 : : DECL_NAME (var) == name)
11308 : : return var;
11309 : 929 : ++decl_ix;
11310 : : }
11311 : : return NULL_TREE;
11312 : : }
11313 : 3088 : for (tree sub = BLOCK_SUBBLOCKS (block); sub; sub = BLOCK_CHAIN (sub))
11314 : 1599 : blocks.safe_push (sub);
11315 : 1489 : ++block_ix;
11316 : : }
11317 : :
11318 : : return NULL_TREE;
11319 : 1953 : }
11320 : :
11321 : : /* DEP is the depset of some decl we're streaming by value. Determine
11322 : : the merging behaviour. */
11323 : :
11324 : : merge_kind
11325 : 2916619 : trees_out::get_merge_kind (tree decl, depset *dep)
11326 : : {
11327 : 2916619 : if (!dep)
11328 : : {
11329 : 544676 : if (VAR_OR_FUNCTION_DECL_P (decl))
11330 : : {
11331 : : /* Any var or function with template info should have DEP. */
11332 : 288012 : gcc_checking_assert (!DECL_LANG_SPECIFIC (decl)
11333 : : || !DECL_TEMPLATE_INFO (decl));
11334 : 288012 : if (DECL_LOCAL_DECL_P (decl))
11335 : : return MK_unique;
11336 : : }
11337 : :
11338 : : /* Either unique, or some member of a class that cannot have an
11339 : : out-of-class definition. For instance a FIELD_DECL. */
11340 : 544440 : tree ctx = CP_DECL_CONTEXT (decl);
11341 : 544440 : if (TREE_CODE (ctx) == FUNCTION_DECL)
11342 : : {
11343 : : /* USING_DECLs and NAMESPACE_DECLs cannot have DECL_TEMPLATE_INFO --
11344 : : this isn't permitting them to have one. */
11345 : 324388 : gcc_checking_assert (TREE_CODE (decl) == USING_DECL
11346 : : || TREE_CODE (decl) == NAMESPACE_DECL
11347 : : || !DECL_LANG_SPECIFIC (decl)
11348 : : || !DECL_TEMPLATE_INFO (decl));
11349 : :
11350 : : return MK_unique;
11351 : : }
11352 : :
11353 : 220052 : if (TREE_CODE (decl) == TEMPLATE_DECL
11354 : 220052 : && DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (decl))
11355 : : return MK_local_friend;
11356 : :
11357 : 220052 : gcc_checking_assert (TYPE_P (ctx));
11358 : :
11359 : : /* Internal-only types will not need to dedup their members. */
11360 : 220052 : if (!DECL_CONTEXT (TYPE_NAME (ctx)))
11361 : : return MK_unique;
11362 : :
11363 : 219996 : if (TREE_CODE (decl) == USING_DECL)
11364 : : return MK_field;
11365 : :
11366 : 140599 : if (TREE_CODE (decl) == FIELD_DECL)
11367 : : {
11368 : 98245 : if (DECL_NAME (decl))
11369 : : {
11370 : : /* Anonymous FIELD_DECLs have a NULL name. */
11371 : 76866 : gcc_checking_assert (!IDENTIFIER_ANON_P (DECL_NAME (decl)));
11372 : : return MK_named;
11373 : : }
11374 : :
11375 : 21379 : if (walking_bit_field_unit)
11376 : : {
11377 : : /* The underlying storage unit for a bitfield. We do not
11378 : : need to dedup it, because it's only reachable through
11379 : : the bitfields it represents. And those are deduped. */
11380 : : // FIXME: Is that assertion correct -- do we ever fish it
11381 : : // out and put it in an expr?
11382 : 354 : gcc_checking_assert (!DECL_NAME (decl)
11383 : : && !RECORD_OR_UNION_TYPE_P (TREE_TYPE (decl))
11384 : : && !DECL_BIT_FIELD_REPRESENTATIVE (decl));
11385 : 354 : gcc_checking_assert ((TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
11386 : : ? TREE_CODE (TREE_TYPE (TREE_TYPE (decl)))
11387 : : : TREE_CODE (TREE_TYPE (decl)))
11388 : : == INTEGER_TYPE);
11389 : : return MK_unique;
11390 : : }
11391 : :
11392 : : return MK_field;
11393 : : }
11394 : :
11395 : 42354 : if (TREE_CODE (decl) == CONST_DECL)
11396 : : return MK_named;
11397 : :
11398 : 6764 : if (TREE_CODE (decl) == VAR_DECL
11399 : 6764 : && DECL_VTABLE_OR_VTT_P (decl))
11400 : : return MK_vtable;
11401 : :
11402 : 1128 : if (DECL_THUNK_P (decl))
11403 : : /* Thunks are unique-enough, because they're only referenced
11404 : : from the vtable. And that's either new (so we want the
11405 : : thunks), or it's a duplicate (so it will be dropped). */
11406 : : return MK_unique;
11407 : :
11408 : : /* There should be no other cases. */
11409 : 0 : gcc_unreachable ();
11410 : : }
11411 : :
11412 : 2371943 : gcc_checking_assert (TREE_CODE (decl) != FIELD_DECL
11413 : : && TREE_CODE (decl) != USING_DECL
11414 : : && TREE_CODE (decl) != CONST_DECL);
11415 : :
11416 : 2371943 : if (is_key_order ())
11417 : : {
11418 : : /* When doing the mergeablilty graph, there's an indirection to
11419 : : the actual depset. */
11420 : 790517 : gcc_assert (dep->is_special ());
11421 : 790517 : dep = dep->deps[0];
11422 : : }
11423 : :
11424 : 2371943 : gcc_checking_assert (decl == dep->get_entity ());
11425 : :
11426 : 2371943 : merge_kind mk = MK_named;
11427 : 2371943 : switch (dep->get_entity_kind ())
11428 : : {
11429 : 0 : default:
11430 : 0 : gcc_unreachable ();
11431 : :
11432 : : case depset::EK_PARTIAL:
11433 : : mk = MK_partial;
11434 : : break;
11435 : :
11436 : 1386857 : case depset::EK_DECL:
11437 : 1386857 : {
11438 : 1386857 : tree ctx = CP_DECL_CONTEXT (decl);
11439 : :
11440 : 1386857 : switch (TREE_CODE (ctx))
11441 : : {
11442 : 0 : default:
11443 : 0 : gcc_unreachable ();
11444 : :
11445 : 10725 : case FUNCTION_DECL:
11446 : 10725 : gcc_checking_assert
11447 : : (DECL_IMPLICIT_TYPEDEF_P (STRIP_TEMPLATE (decl)));
11448 : :
11449 : : mk = MK_local_type;
11450 : : break;
11451 : :
11452 : 1376132 : case RECORD_TYPE:
11453 : 1376132 : case UNION_TYPE:
11454 : 1376132 : case NAMESPACE_DECL:
11455 : 1376132 : if (DECL_NAME (decl) == as_base_identifier)
11456 : : {
11457 : : mk = MK_as_base;
11458 : : break;
11459 : : }
11460 : :
11461 : : /* A lambda may have a class as its context, even though it
11462 : : isn't a member in the traditional sense; see the test
11463 : : g++.dg/modules/lambda-6_a.C. */
11464 : 1740686 : if (DECL_IMPLICIT_TYPEDEF_P (STRIP_TEMPLATE (decl))
11465 : 1506269 : && LAMBDA_TYPE_P (TREE_TYPE (decl)))
11466 : : {
11467 : 926 : if (tree scope = LAMBDA_TYPE_EXTRA_SCOPE (TREE_TYPE (decl)))
11468 : : {
11469 : : /* Lambdas attached to fields are keyed to its class. */
11470 : 445 : if (TREE_CODE (scope) == FIELD_DECL)
11471 : 9 : scope = TYPE_NAME (DECL_CONTEXT (scope));
11472 : 445 : if (DECL_LANG_SPECIFIC (scope)
11473 : 890 : && DECL_MODULE_KEYED_DECLS_P (scope))
11474 : : {
11475 : : mk = MK_keyed;
11476 : : break;
11477 : : }
11478 : : }
11479 : : /* Lambdas not attached to any mangling scope are TU-local. */
11480 : : mk = MK_unique;
11481 : : break;
11482 : : }
11483 : :
11484 : 1311214 : if (TREE_CODE (decl) == TEMPLATE_DECL
11485 : 1311214 : && DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (decl))
11486 : : {
11487 : : mk = MK_local_friend;
11488 : : break;
11489 : : }
11490 : :
11491 : 1296391 : if (DECL_DECOMPOSITION_P (decl))
11492 : : {
11493 : : mk = MK_unique;
11494 : : break;
11495 : : }
11496 : :
11497 : 1295914 : if (IDENTIFIER_ANON_P (DECL_NAME (decl)))
11498 : : {
11499 : 21302 : if (RECORD_OR_UNION_TYPE_P (ctx))
11500 : : mk = MK_field;
11501 : 1015 : else if (DECL_IMPLICIT_TYPEDEF_P (decl)
11502 : 1015 : && UNSCOPED_ENUM_P (TREE_TYPE (decl))
11503 : 2030 : && TYPE_VALUES (TREE_TYPE (decl)))
11504 : : /* Keyed by first enum value, and underlying type. */
11505 : : mk = MK_enum;
11506 : : else
11507 : : /* No way to merge it, it is an ODR land-mine. */
11508 : : mk = MK_unique;
11509 : : }
11510 : : }
11511 : : }
11512 : : break;
11513 : :
11514 : 942252 : case depset::EK_SPECIALIZATION:
11515 : 942252 : {
11516 : 942252 : gcc_checking_assert (dep->is_special ());
11517 : :
11518 : 942252 : if (TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
11519 : : /* An block-scope classes of templates are themselves
11520 : : templates. */
11521 : 4140 : gcc_checking_assert (DECL_IMPLICIT_TYPEDEF_P (decl));
11522 : :
11523 : 942252 : if (dep->is_friend_spec ())
11524 : : mk = MK_friend_spec;
11525 : 942252 : else if (dep->is_type_spec ())
11526 : : mk = MK_type_spec;
11527 : : else
11528 : 651930 : mk = MK_decl_spec;
11529 : :
11530 : 942252 : if (TREE_CODE (decl) == TEMPLATE_DECL)
11531 : : {
11532 : 65394 : spec_entry *entry = reinterpret_cast <spec_entry *> (dep->deps[0]);
11533 : 65394 : if (TREE_CODE (entry->spec) != TEMPLATE_DECL)
11534 : 7008 : mk = merge_kind (mk | MK_tmpl_tmpl_mask);
11535 : : }
11536 : : }
11537 : : break;
11538 : : }
11539 : :
11540 : : return mk;
11541 : : }
11542 : :
11543 : :
11544 : : /* The container of DECL -- not necessarily its context! */
11545 : :
11546 : : tree
11547 : 2916619 : trees_out::decl_container (tree decl)
11548 : : {
11549 : 2916619 : int use_tpl;
11550 : 2916619 : tree tpl = NULL_TREE;
11551 : 2916619 : if (tree template_info = node_template_info (decl, use_tpl))
11552 : 990995 : tpl = TI_TEMPLATE (template_info);
11553 : 2916619 : if (tpl == decl)
11554 : 0 : tpl = nullptr;
11555 : :
11556 : : /* Stream the template we're instantiated from. */
11557 : 2916619 : tree_node (tpl);
11558 : :
11559 : 2916619 : tree container = NULL_TREE;
11560 : 2916619 : if (TREE_CODE (decl) == TEMPLATE_DECL
11561 : 2916619 : && DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (decl))
11562 : 14823 : container = DECL_CHAIN (decl);
11563 : : else
11564 : 2901796 : container = CP_DECL_CONTEXT (decl);
11565 : :
11566 : 2916619 : if (TYPE_P (container))
11567 : 1715620 : container = TYPE_NAME (container);
11568 : :
11569 : 2916619 : tree_node (container);
11570 : :
11571 : 2916619 : return container;
11572 : : }
11573 : :
11574 : : tree
11575 : 867779 : trees_in::decl_container ()
11576 : : {
11577 : : /* The maybe-template. */
11578 : 867779 : (void)tree_node ();
11579 : :
11580 : 867779 : tree container = tree_node ();
11581 : :
11582 : 867779 : return container;
11583 : : }
11584 : :
11585 : : /* Write out key information about a mergeable DEP. Does not write
11586 : : the contents of DEP itself. The context has already been
11587 : : written. The container has already been streamed. */
11588 : :
11589 : : void
11590 : 2916619 : trees_out::key_mergeable (int tag, merge_kind mk, tree decl, tree inner,
11591 : : tree container, depset *dep)
11592 : : {
11593 : 2916619 : if (dep && is_key_order ())
11594 : : {
11595 : 790517 : gcc_checking_assert (dep->is_special ());
11596 : 790517 : dep = dep->deps[0];
11597 : : }
11598 : :
11599 : 2916619 : if (streaming_p ())
11600 : 1057724 : dump (dumper::MERGE)
11601 : 798 : && dump ("Writing:%d's %s merge key (%s) %C:%N", tag, merge_kind_name[mk],
11602 : 723 : dep ? dep->entity_kind_name () : "contained",
11603 : 798 : TREE_CODE (decl), decl);
11604 : :
11605 : : /* Now write the locating information. */
11606 : 2916619 : if (mk & MK_template_mask)
11607 : : {
11608 : : /* Specializations are located via their originating template,
11609 : : and the set of template args they specialize. */
11610 : 942252 : gcc_checking_assert (dep && dep->is_special ());
11611 : 942252 : spec_entry *entry = reinterpret_cast <spec_entry *> (dep->deps[0]);
11612 : :
11613 : 942252 : tree_node (entry->tmpl);
11614 : 942252 : tree_node (entry->args);
11615 : 942252 : if (mk & MK_tmpl_decl_mask)
11616 : 651930 : if (flag_concepts && TREE_CODE (inner) == VAR_DECL)
11617 : : {
11618 : : /* Variable template partial specializations might need
11619 : : constraints (see spec_hasher::equal). It's simpler to
11620 : : write NULL when we don't need them. */
11621 : 13551 : tree constraints = NULL_TREE;
11622 : :
11623 : 13551 : if (uses_template_parms (entry->args))
11624 : 396 : constraints = get_constraints (inner);
11625 : 13551 : tree_node (constraints);
11626 : : }
11627 : :
11628 : 942252 : if (CHECKING_P)
11629 : : {
11630 : : /* Make sure we can locate the decl. */
11631 : 942252 : tree existing = match_mergeable_specialization
11632 : 942252 : (bool (mk & MK_tmpl_decl_mask), entry);
11633 : :
11634 : 942252 : gcc_assert (existing);
11635 : 942252 : if (mk & MK_tmpl_decl_mask)
11636 : : {
11637 : 651930 : if (mk & MK_tmpl_tmpl_mask)
11638 : 5556 : existing = DECL_TI_TEMPLATE (existing);
11639 : : }
11640 : : else
11641 : : {
11642 : 290322 : if (mk & MK_tmpl_tmpl_mask)
11643 : 1452 : existing = CLASSTYPE_TI_TEMPLATE (existing);
11644 : : else
11645 : 288870 : existing = TYPE_NAME (existing);
11646 : : }
11647 : :
11648 : : /* The walkabout should have found ourselves. */
11649 : 942252 : gcc_checking_assert (TREE_CODE (decl) == TYPE_DECL
11650 : : ? same_type_p (TREE_TYPE (decl),
11651 : : TREE_TYPE (existing))
11652 : : : existing == decl);
11653 : : }
11654 : : }
11655 : 1974367 : else if (mk != MK_unique)
11656 : : {
11657 : 1647710 : merge_key key;
11658 : 1647710 : tree name = DECL_NAME (decl);
11659 : :
11660 : 1647710 : switch (mk)
11661 : : {
11662 : 0 : default:
11663 : 0 : gcc_unreachable ();
11664 : :
11665 : 1387068 : case MK_named:
11666 : 1387068 : case MK_friend_spec:
11667 : 1387068 : if (IDENTIFIER_CONV_OP_P (name))
11668 : 4438 : name = conv_op_identifier;
11669 : :
11670 : 1387068 : if (TREE_CODE (inner) == FUNCTION_DECL)
11671 : : {
11672 : : /* Functions are distinguished by parameter types. */
11673 : 726709 : tree fn_type = TREE_TYPE (inner);
11674 : :
11675 : 726709 : key.ref_q = type_memfn_rqual (fn_type);
11676 : 726709 : key.args = TYPE_ARG_TYPES (fn_type);
11677 : :
11678 : 726709 : if (tree reqs = get_constraints (inner))
11679 : : {
11680 : 49536 : if (cxx_dialect < cxx20)
11681 : 42 : reqs = CI_ASSOCIATED_CONSTRAINTS (reqs);
11682 : : else
11683 : 99030 : reqs = CI_DECLARATOR_REQS (reqs);
11684 : 49536 : key.constraints = reqs;
11685 : : }
11686 : :
11687 : 726709 : if (IDENTIFIER_CONV_OP_P (name)
11688 : 726709 : || (decl != inner
11689 : 378780 : && !(name == fun_identifier
11690 : : /* In case the user names something _FUN */
11691 : 66 : && LAMBDA_TYPE_P (DECL_CONTEXT (inner)))))
11692 : : /* And a function template, or conversion operator needs
11693 : : the return type. Except for the _FUN thunk of a
11694 : : generic lambda, which has a recursive decl_type'd
11695 : : return type. */
11696 : : // FIXME: What if the return type is a voldemort?
11697 : 383185 : key.ret = fndecl_declared_return_type (inner);
11698 : : }
11699 : : break;
11700 : :
11701 : 120709 : case MK_field:
11702 : 120709 : {
11703 : 120709 : unsigned ix = 0;
11704 : 120709 : if (TREE_CODE (inner) != FIELD_DECL)
11705 : : name = NULL_TREE;
11706 : : else
11707 : 21025 : gcc_checking_assert (!name || !IDENTIFIER_ANON_P (name));
11708 : :
11709 : 120709 : for (tree field = TYPE_FIELDS (TREE_TYPE (container));
11710 : 2902797 : ; field = DECL_CHAIN (field))
11711 : : {
11712 : 3023506 : tree finner = STRIP_TEMPLATE (field);
11713 : 3023506 : if (TREE_CODE (finner) == TREE_CODE (inner))
11714 : : {
11715 : 1021169 : if (finner == inner)
11716 : : break;
11717 : 900460 : ix++;
11718 : : }
11719 : 2902797 : }
11720 : 120709 : key.index = ix;
11721 : : }
11722 : 120709 : break;
11723 : :
11724 : 5636 : case MK_vtable:
11725 : 5636 : {
11726 : 5636 : tree vtable = CLASSTYPE_VTABLES (TREE_TYPE (container));
11727 : 7196 : for (unsigned ix = 0; ; vtable = DECL_CHAIN (vtable), ix++)
11728 : 7196 : if (vtable == decl)
11729 : : {
11730 : 5636 : key.index = ix;
11731 : 5636 : break;
11732 : : }
11733 : 5636 : name = NULL_TREE;
11734 : : }
11735 : 5636 : break;
11736 : :
11737 : 64455 : case MK_as_base:
11738 : 64455 : gcc_checking_assert
11739 : : (decl == TYPE_NAME (CLASSTYPE_AS_BASE (TREE_TYPE (container))));
11740 : : break;
11741 : :
11742 : 14823 : case MK_local_friend:
11743 : 14823 : {
11744 : : /* Find by index on the class's DECL_LIST */
11745 : 14823 : unsigned ix = 0;
11746 : 14823 : for (tree decls = CLASSTYPE_DECL_LIST (TREE_CHAIN (decl));
11747 : 410013 : decls; decls = TREE_CHAIN (decls))
11748 : 410013 : if (!TREE_PURPOSE (decls))
11749 : : {
11750 : 58878 : tree frnd = friend_from_decl_list (TREE_VALUE (decls));
11751 : 58878 : if (frnd == decl)
11752 : : break;
11753 : 44055 : ix++;
11754 : : }
11755 : 14823 : key.index = ix;
11756 : 14823 : name = NULL_TREE;
11757 : : }
11758 : 14823 : break;
11759 : :
11760 : 10725 : case MK_local_type:
11761 : 10725 : key_local_type (key, STRIP_TEMPLATE (decl), container);
11762 : 10725 : break;
11763 : :
11764 : 1015 : case MK_enum:
11765 : 1015 : {
11766 : : /* Anonymous enums are located by their first identifier,
11767 : : and underlying type. */
11768 : 1015 : tree type = TREE_TYPE (decl);
11769 : :
11770 : 1015 : gcc_checking_assert (UNSCOPED_ENUM_P (type));
11771 : : /* Using the type name drops the bit precision we might
11772 : : have been using on the enum. */
11773 : 1015 : key.ret = TYPE_NAME (ENUM_UNDERLYING_TYPE (type));
11774 : 1015 : if (tree values = TYPE_VALUES (type))
11775 : 1015 : name = DECL_NAME (TREE_VALUE (values));
11776 : : }
11777 : : break;
11778 : :
11779 : 445 : case MK_keyed:
11780 : 445 : {
11781 : 890 : gcc_checking_assert (LAMBDA_TYPE_P (TREE_TYPE (inner)));
11782 : 445 : tree scope = LAMBDA_TYPE_EXTRA_SCOPE (TREE_TYPE (inner));
11783 : 445 : gcc_checking_assert (TREE_CODE (scope) == VAR_DECL
11784 : : || TREE_CODE (scope) == FIELD_DECL
11785 : : || TREE_CODE (scope) == PARM_DECL
11786 : : || TREE_CODE (scope) == TYPE_DECL
11787 : : || TREE_CODE (scope) == CONCEPT_DECL);
11788 : : /* Lambdas attached to fields are keyed to the class. */
11789 : 445 : if (TREE_CODE (scope) == FIELD_DECL)
11790 : 9 : scope = TYPE_NAME (DECL_CONTEXT (scope));
11791 : 445 : auto *root = keyed_table->get (scope);
11792 : 445 : unsigned ix = root->length ();
11793 : : /* If we don't find it, we'll write a really big number
11794 : : that the reader will ignore. */
11795 : 463 : while (ix--)
11796 : 463 : if ((*root)[ix] == inner)
11797 : : break;
11798 : :
11799 : : /* Use the keyed-to decl as the 'name'. */
11800 : 445 : name = scope;
11801 : 445 : key.index = ix;
11802 : : }
11803 : 445 : break;
11804 : :
11805 : 42834 : case MK_partial:
11806 : 42834 : {
11807 : 42834 : tree ti = get_template_info (inner);
11808 : 42834 : key.constraints = get_constraints (inner);
11809 : 42834 : key.ret = TI_TEMPLATE (ti);
11810 : 42834 : key.args = TI_ARGS (ti);
11811 : : }
11812 : 42834 : break;
11813 : : }
11814 : :
11815 : 1647710 : tree_node (name);
11816 : 1647710 : if (streaming_p ())
11817 : : {
11818 : 585512 : unsigned code = (key.ref_q << 0) | (key.index << 2);
11819 : 585512 : u (code);
11820 : : }
11821 : :
11822 : 1647710 : if (mk == MK_enum)
11823 : 1015 : tree_node (key.ret);
11824 : 1646695 : else if (mk == MK_partial
11825 : 1603861 : || (mk == MK_named && inner
11826 : 1387068 : && TREE_CODE (inner) == FUNCTION_DECL))
11827 : : {
11828 : 769543 : tree_node (key.ret);
11829 : 769543 : tree arg = key.args;
11830 : 769543 : if (mk == MK_named)
11831 : 2084343 : while (arg && arg != void_list_node)
11832 : : {
11833 : 1357634 : tree_node (TREE_VALUE (arg));
11834 : 1357634 : arg = TREE_CHAIN (arg);
11835 : : }
11836 : 769543 : tree_node (arg);
11837 : 769543 : tree_node (key.constraints);
11838 : : }
11839 : : }
11840 : 2916619 : }
11841 : :
11842 : : /* DECL is a new declaration that may be duplicated in OVL. Use RET &
11843 : : ARGS to find its clone, or NULL. If DECL's DECL_NAME is NULL, this
11844 : : has been found by a proxy. It will be an enum type located by its
11845 : : first member.
11846 : :
11847 : : We're conservative with matches, so ambiguous decls will be
11848 : : registered as different, then lead to a lookup error if the two
11849 : : modules are both visible. Perhaps we want to do something similar
11850 : : to duplicate decls to get ODR errors on loading? We already have
11851 : : some special casing for namespaces. */
11852 : :
11853 : : static tree
11854 : 264990 : check_mergeable_decl (merge_kind mk, tree decl, tree ovl, merge_key const &key)
11855 : : {
11856 : 264990 : tree found = NULL_TREE;
11857 : 1089529 : for (ovl_iterator iter (ovl); !found && iter; ++iter)
11858 : : {
11859 : 527933 : tree match = *iter;
11860 : :
11861 : 527933 : tree d_inner = decl;
11862 : 527933 : tree m_inner = match;
11863 : :
11864 : 611822 : again:
11865 : 611822 : if (TREE_CODE (d_inner) != TREE_CODE (m_inner))
11866 : : {
11867 : 81101 : if (TREE_CODE (match) == NAMESPACE_DECL
11868 : 81101 : && !DECL_NAMESPACE_ALIAS (match))
11869 : : /* Namespaces are never overloaded. */
11870 : : found = match;
11871 : :
11872 : 81101 : continue;
11873 : : }
11874 : :
11875 : 530721 : switch (TREE_CODE (d_inner))
11876 : : {
11877 : 174843 : case TEMPLATE_DECL:
11878 : 174843 : if (template_heads_equivalent_p (d_inner, m_inner))
11879 : : {
11880 : 83889 : d_inner = DECL_TEMPLATE_RESULT (d_inner);
11881 : 83889 : m_inner = DECL_TEMPLATE_RESULT (m_inner);
11882 : 83889 : if (d_inner == error_mark_node
11883 : 83889 : && TYPE_DECL_ALIAS_P (m_inner))
11884 : : {
11885 : : found = match;
11886 : : break;
11887 : : }
11888 : 83889 : goto again;
11889 : : }
11890 : : break;
11891 : :
11892 : 258673 : case FUNCTION_DECL:
11893 : 258673 : if (tree m_type = TREE_TYPE (m_inner))
11894 : 258673 : if ((!key.ret
11895 : 133222 : || same_type_p (key.ret, fndecl_declared_return_type (m_inner)))
11896 : 241491 : && type_memfn_rqual (m_type) == key.ref_q
11897 : 241231 : && compparms (key.args, TYPE_ARG_TYPES (m_type))
11898 : : /* Reject if old is a "C" builtin and new is not "C".
11899 : : Matches decls_match behaviour. */
11900 : 124106 : && (!DECL_IS_UNDECLARED_BUILTIN (m_inner)
11901 : 5209 : || !DECL_EXTERN_C_P (m_inner)
11902 : 5081 : || DECL_EXTERN_C_P (d_inner))
11903 : : /* Reject if one is a different member of a
11904 : : guarded/pre/post fn set. */
11905 : 124101 : && (!flag_contracts
11906 : 352 : || (DECL_IS_PRE_FN_P (d_inner)
11907 : 176 : == DECL_IS_PRE_FN_P (m_inner)))
11908 : 382774 : && (!flag_contracts
11909 : 352 : || (DECL_IS_POST_FN_P (d_inner)
11910 : 176 : == DECL_IS_POST_FN_P (m_inner))))
11911 : : {
11912 : 124101 : tree m_reqs = get_constraints (m_inner);
11913 : 124101 : if (m_reqs)
11914 : : {
11915 : 8897 : if (cxx_dialect < cxx20)
11916 : 8 : m_reqs = CI_ASSOCIATED_CONSTRAINTS (m_reqs);
11917 : : else
11918 : 17786 : m_reqs = CI_DECLARATOR_REQS (m_reqs);
11919 : : }
11920 : :
11921 : 124101 : if (cp_tree_equal (key.constraints, m_reqs))
11922 : 162138 : found = match;
11923 : : }
11924 : : break;
11925 : :
11926 : 59111 : case TYPE_DECL:
11927 : 118222 : if (DECL_IMPLICIT_TYPEDEF_P (d_inner)
11928 : 59111 : == DECL_IMPLICIT_TYPEDEF_P (m_inner))
11929 : : {
11930 : 59085 : if (!IDENTIFIER_ANON_P (DECL_NAME (m_inner)))
11931 : 58957 : return match;
11932 : 128 : else if (mk == MK_enum
11933 : 128 : && (TYPE_NAME (ENUM_UNDERLYING_TYPE (TREE_TYPE (m_inner)))
11934 : 128 : == key.ret))
11935 : : found = match;
11936 : : }
11937 : : break;
11938 : :
11939 : : default:
11940 : : found = match;
11941 : : break;
11942 : : }
11943 : : }
11944 : :
11945 : 206033 : return found;
11946 : : }
11947 : :
11948 : : /* DECL, INNER & TYPE are a skeleton set of nodes for a decl. Only
11949 : : the bools have been filled in. Read its merging key and merge it.
11950 : : Returns the existing decl if there is one. */
11951 : :
11952 : : tree
11953 : 867779 : trees_in::key_mergeable (int tag, merge_kind mk, tree decl, tree inner,
11954 : : tree type, tree container, bool is_attached,
11955 : : bool is_imported_temploid_friend)
11956 : : {
11957 : 867779 : const char *kind = "new";
11958 : 867779 : tree existing = NULL_TREE;
11959 : :
11960 : 867779 : if (mk & MK_template_mask)
11961 : : {
11962 : : // FIXME: We could stream the specialization hash?
11963 : 261849 : spec_entry spec;
11964 : 261849 : spec.tmpl = tree_node ();
11965 : 261849 : spec.args = tree_node ();
11966 : :
11967 : 261849 : if (get_overrun ())
11968 : 0 : return error_mark_node;
11969 : :
11970 : 261849 : DECL_NAME (decl) = DECL_NAME (spec.tmpl);
11971 : 261849 : DECL_CONTEXT (decl) = DECL_CONTEXT (spec.tmpl);
11972 : 261849 : DECL_NAME (inner) = DECL_NAME (decl);
11973 : 261849 : DECL_CONTEXT (inner) = DECL_CONTEXT (decl);
11974 : :
11975 : 261849 : tree constr = NULL_TREE;
11976 : 261849 : bool is_decl = mk & MK_tmpl_decl_mask;
11977 : 261849 : if (is_decl)
11978 : : {
11979 : 185199 : if (flag_concepts && TREE_CODE (inner) == VAR_DECL)
11980 : : {
11981 : 3952 : constr = tree_node ();
11982 : 3952 : if (constr)
11983 : 0 : set_constraints (inner, constr);
11984 : : }
11985 : 368696 : spec.spec = (mk & MK_tmpl_tmpl_mask) ? inner : decl;
11986 : : }
11987 : : else
11988 : 76650 : spec.spec = type;
11989 : 261849 : existing = match_mergeable_specialization (is_decl, &spec);
11990 : 261849 : if (constr)
11991 : : /* We'll add these back later, if this is the new decl. */
11992 : 0 : remove_constraints (inner);
11993 : :
11994 : 261849 : if (!existing)
11995 : : ; /* We'll add to the table once read. */
11996 : 143904 : else if (mk & MK_tmpl_decl_mask)
11997 : : {
11998 : : /* A declaration specialization. */
11999 : 99090 : if (mk & MK_tmpl_tmpl_mask)
12000 : 1025 : existing = DECL_TI_TEMPLATE (existing);
12001 : : }
12002 : : else
12003 : : {
12004 : : /* A type specialization. */
12005 : 44814 : if (mk & MK_tmpl_tmpl_mask)
12006 : 234 : existing = CLASSTYPE_TI_TEMPLATE (existing);
12007 : : else
12008 : 44580 : existing = TYPE_NAME (existing);
12009 : : }
12010 : : }
12011 : 605930 : else if (mk == MK_unique)
12012 : : kind = "unique";
12013 : : else
12014 : : {
12015 : 462620 : tree name = tree_node ();
12016 : :
12017 : 462620 : merge_key key;
12018 : 462620 : unsigned code = u ();
12019 : 462620 : key.ref_q = cp_ref_qualifier ((code >> 0) & 3);
12020 : 462620 : key.index = code >> 2;
12021 : :
12022 : 462620 : if (mk == MK_enum)
12023 : 221 : key.ret = tree_node ();
12024 : 462399 : else if (mk == MK_partial
12025 : 452930 : || ((mk == MK_named || mk == MK_friend_spec)
12026 : 382837 : && TREE_CODE (inner) == FUNCTION_DECL))
12027 : : {
12028 : 214989 : key.ret = tree_node ();
12029 : 214989 : tree arg, *arg_ptr = &key.args;
12030 : 214989 : while ((arg = tree_node ())
12031 : 601768 : && arg != void_list_node
12032 : 1000566 : && mk != MK_partial)
12033 : : {
12034 : 388054 : *arg_ptr = tree_cons (NULL_TREE, arg, NULL_TREE);
12035 : 388054 : arg_ptr = &TREE_CHAIN (*arg_ptr);
12036 : : }
12037 : 214989 : *arg_ptr = arg;
12038 : 214989 : key.constraints = tree_node ();
12039 : : }
12040 : :
12041 : 462620 : if (get_overrun ())
12042 : 0 : return error_mark_node;
12043 : :
12044 : 462620 : if (mk < MK_indirect_lwm)
12045 : : {
12046 : 454354 : DECL_NAME (decl) = name;
12047 : 454354 : DECL_CONTEXT (decl) = FROB_CONTEXT (container);
12048 : : }
12049 : 462620 : DECL_NAME (inner) = DECL_NAME (decl);
12050 : 462620 : DECL_CONTEXT (inner) = DECL_CONTEXT (decl);
12051 : :
12052 : 462620 : if (mk == MK_partial)
12053 : : {
12054 : 9469 : for (tree spec = DECL_TEMPLATE_SPECIALIZATIONS (key.ret);
12055 : 47057 : spec; spec = TREE_CHAIN (spec))
12056 : : {
12057 : 43609 : tree tmpl = TREE_VALUE (spec);
12058 : 43609 : tree ti = get_template_info (tmpl);
12059 : 43609 : if (template_args_equal (key.args, TI_ARGS (ti))
12060 : 50281 : && cp_tree_equal (key.constraints,
12061 : : get_constraints
12062 : 6672 : (DECL_TEMPLATE_RESULT (tmpl))))
12063 : : {
12064 : : existing = tmpl;
12065 : : break;
12066 : : }
12067 : : }
12068 : : }
12069 : 453151 : else if (mk == MK_keyed
12070 : 182 : && DECL_LANG_SPECIFIC (name)
12071 : 453333 : && DECL_MODULE_KEYED_DECLS_P (name))
12072 : : {
12073 : 182 : gcc_checking_assert (TREE_CODE (container) == NAMESPACE_DECL
12074 : : || TREE_CODE (container) == TYPE_DECL);
12075 : 182 : if (auto *set = keyed_table->get (name))
12076 : 462734 : if (key.index < set->length ())
12077 : : {
12078 : 114 : existing = (*set)[key.index];
12079 : 114 : if (existing)
12080 : : {
12081 : 114 : gcc_checking_assert
12082 : : (DECL_IMPLICIT_TYPEDEF_P (existing));
12083 : 114 : if (inner != decl)
12084 : 79 : existing
12085 : 79 : = CLASSTYPE_TI_TEMPLATE (TREE_TYPE (existing));
12086 : : }
12087 : : }
12088 : : }
12089 : : else
12090 : 452969 : switch (TREE_CODE (container))
12091 : : {
12092 : 0 : default:
12093 : 0 : gcc_unreachable ();
12094 : :
12095 : 117324 : case NAMESPACE_DECL:
12096 : 117324 : if (is_attached
12097 : 117324 : && !is_imported_temploid_friend
12098 : 117324 : && !(state->is_module () || state->is_partition ()))
12099 : : kind = "unique";
12100 : : else
12101 : : {
12102 : 115508 : gcc_checking_assert (mk == MK_named || mk == MK_enum);
12103 : 115508 : tree mvec;
12104 : 115508 : tree *vslot = mergeable_namespace_slots (container, name,
12105 : : is_attached, &mvec);
12106 : 115508 : existing = check_mergeable_decl (mk, decl, *vslot, key);
12107 : 115508 : if (!existing)
12108 : 43425 : add_mergeable_namespace_entity (vslot, decl);
12109 : : else
12110 : : {
12111 : : /* Note that we now have duplicates to deal with in
12112 : : name lookup. */
12113 : 72083 : if (is_attached)
12114 : 33 : BINDING_VECTOR_PARTITION_DUPS_P (mvec) = true;
12115 : : else
12116 : 72050 : BINDING_VECTOR_GLOBAL_DUPS_P (mvec) = true;
12117 : : }
12118 : : }
12119 : : break;
12120 : :
12121 : 3206 : case FUNCTION_DECL:
12122 : 3206 : gcc_checking_assert (mk == MK_local_type);
12123 : 3206 : existing = key_local_type (key, container, name);
12124 : 3206 : if (existing && inner != decl)
12125 : 3588 : existing = TYPE_TI_TEMPLATE (TREE_TYPE (existing));
12126 : : break;
12127 : :
12128 : 332439 : case TYPE_DECL:
12129 : 332439 : gcc_checking_assert (!is_imported_temploid_friend);
12130 : 4263 : if (is_attached && !(state->is_module () || state->is_partition ())
12131 : : /* Implicit member functions can come from
12132 : : anywhere. */
12133 : 335773 : && !(DECL_ARTIFICIAL (decl)
12134 : 1975 : && TREE_CODE (decl) == FUNCTION_DECL
12135 : 628 : && !DECL_THUNK_P (decl)))
12136 : : kind = "unique";
12137 : : else
12138 : : {
12139 : 329733 : tree ctx = TREE_TYPE (container);
12140 : :
12141 : : /* For some reason templated enumeral types are not marked
12142 : : as COMPLETE_TYPE_P, even though they have members.
12143 : : This may well be a bug elsewhere. */
12144 : 329733 : if (TREE_CODE (ctx) == ENUMERAL_TYPE)
12145 : 11757 : existing = find_enum_member (ctx, name);
12146 : 317976 : else if (COMPLETE_TYPE_P (ctx))
12147 : : {
12148 : 188068 : switch (mk)
12149 : : {
12150 : 0 : default:
12151 : 0 : gcc_unreachable ();
12152 : :
12153 : 149881 : case MK_named:
12154 : 149881 : existing = lookup_class_binding (ctx, name);
12155 : 149881 : if (existing)
12156 : : {
12157 : 149482 : tree inner = decl;
12158 : 149482 : if (TREE_CODE (inner) == TEMPLATE_DECL
12159 : 149482 : && !DECL_MEMBER_TEMPLATE_P (inner))
12160 : 67128 : inner = DECL_TEMPLATE_RESULT (inner);
12161 : :
12162 : 149482 : existing = check_mergeable_decl
12163 : 149482 : (mk, inner, existing, key);
12164 : :
12165 : 149482 : if (!existing && DECL_ALIAS_TEMPLATE_P (decl))
12166 : : {} // FIXME: Insert into specialization
12167 : : // tables, we'll need the arguments for that!
12168 : : }
12169 : : break;
12170 : :
12171 : 26314 : case MK_field:
12172 : 26314 : {
12173 : 26314 : unsigned ix = key.index;
12174 : 26314 : for (tree field = TYPE_FIELDS (ctx);
12175 : 890389 : field; field = DECL_CHAIN (field))
12176 : : {
12177 : 890389 : tree finner = STRIP_TEMPLATE (field);
12178 : 890389 : if (TREE_CODE (finner) == TREE_CODE (inner))
12179 : 298714 : if (!ix--)
12180 : : {
12181 : : existing = field;
12182 : : break;
12183 : : }
12184 : : }
12185 : : }
12186 : : break;
12187 : :
12188 : 1384 : case MK_vtable:
12189 : 1384 : {
12190 : 1384 : unsigned ix = key.index;
12191 : 1384 : for (tree vtable = CLASSTYPE_VTABLES (ctx);
12192 : 1761 : vtable; vtable = DECL_CHAIN (vtable))
12193 : 1596 : if (!ix--)
12194 : : {
12195 : : existing = vtable;
12196 : : break;
12197 : : }
12198 : : }
12199 : : break;
12200 : :
12201 : 7560 : case MK_as_base:
12202 : 7560 : {
12203 : 7560 : tree as_base = CLASSTYPE_AS_BASE (ctx);
12204 : 7560 : if (as_base && as_base != ctx)
12205 : 7560 : existing = TYPE_NAME (as_base);
12206 : : }
12207 : : break;
12208 : :
12209 : 2929 : case MK_local_friend:
12210 : 2929 : {
12211 : 2929 : unsigned ix = key.index;
12212 : 2929 : for (tree decls = CLASSTYPE_DECL_LIST (ctx);
12213 : 81938 : decls; decls = TREE_CHAIN (decls))
12214 : 81938 : if (!TREE_PURPOSE (decls) && !ix--)
12215 : : {
12216 : 2929 : existing
12217 : 2929 : = friend_from_decl_list (TREE_VALUE (decls));
12218 : 2929 : break;
12219 : : }
12220 : : }
12221 : : break;
12222 : : }
12223 : :
12224 : 188068 : if (existing && mk < MK_indirect_lwm && mk != MK_partial
12225 : 184105 : && TREE_CODE (decl) == TEMPLATE_DECL
12226 : 273363 : && !DECL_MEMBER_TEMPLATE_P (decl))
12227 : : {
12228 : 69165 : tree ti;
12229 : 69165 : if (DECL_IMPLICIT_TYPEDEF_P (existing))
12230 : 871 : ti = TYPE_TEMPLATE_INFO (TREE_TYPE (existing));
12231 : : else
12232 : 68294 : ti = DECL_TEMPLATE_INFO (existing);
12233 : 69165 : existing = TI_TEMPLATE (ti);
12234 : : }
12235 : : }
12236 : : }
12237 : : }
12238 : : }
12239 : :
12240 : 867779 : dump (dumper::MERGE)
12241 : 2401 : && dump ("Read:%d's %s merge key (%s) %C:%N", tag, merge_kind_name[mk],
12242 : 2401 : existing ? "matched" : kind, TREE_CODE (decl), decl);
12243 : :
12244 : : return existing;
12245 : : }
12246 : :
12247 : : void
12248 : 241292 : trees_out::binfo_mergeable (tree binfo)
12249 : : {
12250 : 241292 : tree dom = binfo;
12251 : 308008 : while (tree parent = BINFO_INHERITANCE_CHAIN (dom))
12252 : : dom = parent;
12253 : 241292 : tree type = BINFO_TYPE (dom);
12254 : 241292 : gcc_checking_assert (TYPE_BINFO (type) == dom);
12255 : 241292 : tree_node (type);
12256 : 241292 : if (streaming_p ())
12257 : : {
12258 : : unsigned ix = 0;
12259 : 132997 : for (; dom != binfo; dom = TREE_CHAIN (dom))
12260 : 34563 : ix++;
12261 : 98434 : u (ix);
12262 : : }
12263 : 241292 : }
12264 : :
12265 : : unsigned
12266 : 72396 : trees_in::binfo_mergeable (tree *type)
12267 : : {
12268 : 72396 : *type = tree_node ();
12269 : 72396 : return u ();
12270 : : }
12271 : :
12272 : : /* DECL is a just streamed declaration with attributes DATTR that should
12273 : : have matching ABI tags as EXISTING's attributes EATTR. Check that the
12274 : : ABI tags match, and report an error if not. */
12275 : :
12276 : : void
12277 : 282907 : trees_in::check_abi_tags (tree existing, tree decl, tree &eattr, tree &dattr)
12278 : : {
12279 : 282907 : tree etags = lookup_attribute ("abi_tag", eattr);
12280 : 282907 : tree dtags = lookup_attribute ("abi_tag", dattr);
12281 : 282907 : if ((etags == nullptr) != (dtags == nullptr)
12282 : 282907 : || (etags && !attribute_value_equal (etags, dtags)))
12283 : : {
12284 : 30 : if (etags)
12285 : 21 : etags = TREE_VALUE (etags);
12286 : 30 : if (dtags)
12287 : 24 : dtags = TREE_VALUE (dtags);
12288 : :
12289 : : /* We only error if mangling wouldn't consider the tags equivalent. */
12290 : 30 : if (!equal_abi_tags (etags, dtags))
12291 : : {
12292 : 21 : auto_diagnostic_group d;
12293 : 21 : if (dtags)
12294 : 15 : error_at (DECL_SOURCE_LOCATION (decl),
12295 : : "mismatching abi tags for %qD with tags %qE",
12296 : : decl, dtags);
12297 : : else
12298 : 6 : error_at (DECL_SOURCE_LOCATION (decl),
12299 : : "mismatching abi tags for %qD with no tags", decl);
12300 : 21 : if (etags)
12301 : 15 : inform (DECL_SOURCE_LOCATION (existing),
12302 : : "existing declaration here with tags %qE", etags);
12303 : : else
12304 : 6 : inform (DECL_SOURCE_LOCATION (existing),
12305 : : "existing declaration here with no tags");
12306 : 21 : }
12307 : :
12308 : : /* Always use the existing abi_tags as the canonical set so that
12309 : : later processing doesn't get confused. */
12310 : 30 : if (dtags)
12311 : 24 : dattr = remove_attribute ("abi_tag", dattr);
12312 : 30 : if (etags)
12313 : 21 : duplicate_one_attribute (&dattr, eattr, "abi_tag");
12314 : : }
12315 : 282907 : }
12316 : :
12317 : : /* DECL is a just streamed mergeable decl that should match EXISTING. Check
12318 : : it does and issue an appropriate diagnostic if not. Merge any
12319 : : bits from DECL to EXISTING. This is stricter matching than
12320 : : decls_match, because we can rely on ODR-sameness, and we cannot use
12321 : : decls_match because it can cause instantiations of constraints. */
12322 : :
12323 : : bool
12324 : 417795 : trees_in::is_matching_decl (tree existing, tree decl, bool is_typedef)
12325 : : {
12326 : : // FIXME: We should probably do some duplicate decl-like stuff here
12327 : : // (beware, default parms should be the same?) Can we just call
12328 : : // duplicate_decls and teach it how to handle the module-specific
12329 : : // permitted/required duplications?
12330 : :
12331 : : // We know at this point that the decls have matched by key, so we
12332 : : // can elide some of the checking
12333 : 417795 : gcc_checking_assert (TREE_CODE (existing) == TREE_CODE (decl));
12334 : :
12335 : 417795 : tree d_inner = decl;
12336 : 417795 : tree e_inner = existing;
12337 : 417795 : if (TREE_CODE (decl) == TEMPLATE_DECL)
12338 : : {
12339 : 140894 : d_inner = DECL_TEMPLATE_RESULT (d_inner);
12340 : 140894 : e_inner = DECL_TEMPLATE_RESULT (e_inner);
12341 : 140894 : gcc_checking_assert (TREE_CODE (e_inner) == TREE_CODE (d_inner));
12342 : : }
12343 : :
12344 : : // FIXME: do more precise errors at point of mismatch
12345 : 417795 : const char *mismatch_msg = nullptr;
12346 : :
12347 : 417795 : if (VAR_OR_FUNCTION_DECL_P (d_inner)
12348 : 417795 : && DECL_EXTERN_C_P (d_inner) != DECL_EXTERN_C_P (e_inner))
12349 : : {
12350 : 6 : mismatch_msg = G_("conflicting language linkage for imported "
12351 : : "declaration %#qD");
12352 : 6 : goto mismatch;
12353 : : }
12354 : 417789 : else if (TREE_CODE (d_inner) == FUNCTION_DECL)
12355 : : {
12356 : 188636 : tree e_ret = fndecl_declared_return_type (existing);
12357 : 188636 : tree d_ret = fndecl_declared_return_type (decl);
12358 : :
12359 : 82349 : if (decl != d_inner && DECL_NAME (d_inner) == fun_identifier
12360 : 188654 : && LAMBDA_TYPE_P (DECL_CONTEXT (d_inner)))
12361 : : /* This has a recursive type that will compare different. */;
12362 : 188627 : else if (!same_type_p (d_ret, e_ret))
12363 : : {
12364 : 6 : mismatch_msg = G_("conflicting type for imported declaration %#qD");
12365 : 6 : goto mismatch;
12366 : : }
12367 : :
12368 : 188630 : tree e_type = TREE_TYPE (e_inner);
12369 : 188630 : tree d_type = TREE_TYPE (d_inner);
12370 : :
12371 : 188630 : for (tree e_args = TYPE_ARG_TYPES (e_type),
12372 : 188630 : d_args = TYPE_ARG_TYPES (d_type);
12373 : 322553 : e_args != d_args && (e_args || d_args);
12374 : 133923 : e_args = TREE_CHAIN (e_args), d_args = TREE_CHAIN (d_args))
12375 : : {
12376 : 133923 : if (!(e_args && d_args))
12377 : : {
12378 : 0 : mismatch_msg = G_("conflicting argument list for imported "
12379 : : "declaration %#qD");
12380 : 0 : goto mismatch;
12381 : : }
12382 : :
12383 : 133923 : if (!same_type_p (TREE_VALUE (d_args), TREE_VALUE (e_args)))
12384 : : {
12385 : 0 : mismatch_msg = G_("conflicting argument types for imported "
12386 : : "declaration %#qD");
12387 : 0 : goto mismatch;
12388 : : }
12389 : : }
12390 : :
12391 : : /* If EXISTING has an undeduced or uninstantiated exception
12392 : : specification, but DECL does not, propagate the exception
12393 : : specification. Otherwise we end up asserting or trying to
12394 : : instantiate it in the middle of loading. */
12395 : 188630 : tree e_spec = TYPE_RAISES_EXCEPTIONS (e_type);
12396 : 188630 : tree d_spec = TYPE_RAISES_EXCEPTIONS (d_type);
12397 : 277222 : if (DECL_MAYBE_DELETED (e_inner) || DEFERRED_NOEXCEPT_SPEC_P (e_spec))
12398 : : {
12399 : 15151 : if (!DEFERRED_NOEXCEPT_SPEC_P (d_spec)
12400 : 45081 : || (UNEVALUATED_NOEXCEPT_SPEC_P (e_spec)
12401 : 12194 : && !UNEVALUATED_NOEXCEPT_SPEC_P (d_spec)))
12402 : : {
12403 : 261 : dump (dumper::MERGE)
12404 : 3 : && dump ("Propagating instantiated noexcept to %N", existing);
12405 : 261 : TREE_TYPE (existing) = d_type;
12406 : :
12407 : : /* Propagate to existing clones. */
12408 : 261 : tree clone;
12409 : 537 : FOR_EACH_CLONE (clone, existing)
12410 : : {
12411 : 276 : if (TREE_TYPE (clone) == e_type)
12412 : 276 : TREE_TYPE (clone) = d_type;
12413 : : else
12414 : 0 : TREE_TYPE (clone)
12415 : 0 : = build_exception_variant (TREE_TYPE (clone), d_spec);
12416 : : }
12417 : : }
12418 : : }
12419 : 173429 : else if (!DECL_MAYBE_DELETED (d_inner)
12420 : 247649 : && !DEFERRED_NOEXCEPT_SPEC_P (d_spec)
12421 : 346857 : && !comp_except_specs (d_spec, e_spec, ce_type))
12422 : : {
12423 : 761 : mismatch_msg = G_("conflicting %<noexcept%> specifier for "
12424 : : "imported declaration %#qD");
12425 : 761 : goto mismatch;
12426 : : }
12427 : :
12428 : : /* Similarly if EXISTING has an undeduced return type, but DECL's
12429 : : is already deduced. */
12430 : 187869 : if (undeduced_auto_decl (existing) && !undeduced_auto_decl (decl))
12431 : : {
12432 : 13 : dump (dumper::MERGE)
12433 : 0 : && dump ("Propagating deduced return type to %N", existing);
12434 : 13 : gcc_checking_assert (existing == e_inner);
12435 : 13 : FNDECL_USED_AUTO (existing) = true;
12436 : 13 : DECL_SAVED_AUTO_RETURN_TYPE (existing) = TREE_TYPE (e_type);
12437 : 13 : TREE_TYPE (existing) = change_return_type (TREE_TYPE (d_type), e_type);
12438 : : }
12439 : 187856 : else if (type_uses_auto (d_ret)
12440 : 187856 : && !same_type_p (TREE_TYPE (d_type), TREE_TYPE (e_type)))
12441 : : {
12442 : 3 : mismatch_msg = G_("conflicting deduced return type for "
12443 : : "imported declaration %#qD");
12444 : 3 : goto mismatch;
12445 : : }
12446 : :
12447 : : /* Similarly if EXISTING has undeduced constexpr, but DECL's
12448 : : is already deduced. */
12449 : 187866 : if (DECL_DECLARED_CONSTEXPR_P (e_inner)
12450 : 187866 : == DECL_DECLARED_CONSTEXPR_P (d_inner))
12451 : : /* Already matches. */;
12452 : 2 : else if (DECL_DECLARED_CONSTEXPR_P (d_inner)
12453 : 2 : && (DECL_MAYBE_DELETED (e_inner)
12454 : 0 : || decl_implicit_constexpr_p (d_inner)))
12455 : : /* DECL was deduced, copy to EXISTING. */
12456 : : {
12457 : 1 : DECL_DECLARED_CONSTEXPR_P (e_inner) = true;
12458 : 1 : if (decl_implicit_constexpr_p (d_inner))
12459 : 0 : DECL_LANG_SPECIFIC (e_inner)->u.fn.implicit_constexpr = true;
12460 : : }
12461 : 1 : else if (DECL_DECLARED_CONSTEXPR_P (e_inner)
12462 : 1 : && (DECL_MAYBE_DELETED (d_inner)
12463 : 0 : || decl_implicit_constexpr_p (e_inner)))
12464 : : /* EXISTING was deduced, leave it alone. */;
12465 : : else
12466 : : {
12467 : 0 : mismatch_msg = G_("conflicting %<constexpr%> for imported "
12468 : : "declaration %#qD");
12469 : 0 : goto mismatch;
12470 : : }
12471 : :
12472 : : /* Don't synthesize a defaulted function if we're importing one
12473 : : we've already determined. */
12474 : 187866 : if (!DECL_MAYBE_DELETED (d_inner))
12475 : 187756 : DECL_MAYBE_DELETED (e_inner) = false;
12476 : : }
12477 : 229153 : else if (is_typedef)
12478 : : {
12479 : 82004 : if (!DECL_ORIGINAL_TYPE (e_inner)
12480 : 82004 : || !same_type_p (DECL_ORIGINAL_TYPE (d_inner),
12481 : : DECL_ORIGINAL_TYPE (e_inner)))
12482 : : {
12483 : 3 : mismatch_msg = G_("conflicting imported declaration %q#D");
12484 : 3 : goto mismatch;
12485 : : }
12486 : : }
12487 : : /* Using cp_tree_equal because we can meet TYPE_ARGUMENT_PACKs
12488 : : here. I suspect the entities that directly do that are things
12489 : : that shouldn't go to duplicate_decls (FIELD_DECLs etc). */
12490 : 147149 : else if (!cp_tree_equal (TREE_TYPE (decl), TREE_TYPE (existing)))
12491 : : {
12492 : : mismatch_msg = G_("conflicting type for imported declaration %#qD");
12493 : 1454 : mismatch:
12494 : 1454 : if (DECL_IS_UNDECLARED_BUILTIN (existing))
12495 : : /* Just like duplicate_decls, presum the user knows what
12496 : : they're doing in overriding a builtin. */
12497 : 764 : TREE_TYPE (existing) = TREE_TYPE (decl);
12498 : 690 : else if (decl_function_context (decl))
12499 : : /* The type of a mergeable local entity (such as a function scope
12500 : : capturing lambda's closure type fields) can depend on an
12501 : : unmergeable local entity (such as a local variable), so type
12502 : : equality isn't feasible in general for local entities. */;
12503 : : else
12504 : : {
12505 : 21 : gcc_checking_assert (mismatch_msg);
12506 : 21 : auto_diagnostic_group d;
12507 : 21 : error_at (DECL_SOURCE_LOCATION (decl), mismatch_msg, decl);
12508 : 21 : inform (DECL_SOURCE_LOCATION (existing),
12509 : : "existing declaration %#qD", existing);
12510 : 21 : return false;
12511 : 21 : }
12512 : : }
12513 : :
12514 : 417774 : if (DECL_IS_UNDECLARED_BUILTIN (existing)
12515 : 417774 : && !DECL_IS_UNDECLARED_BUILTIN (decl))
12516 : : {
12517 : : /* We're matching a builtin that the user has yet to declare.
12518 : : We are the one! This is very much duplicate-decl
12519 : : shenanigans. */
12520 : 946 : DECL_SOURCE_LOCATION (existing) = DECL_SOURCE_LOCATION (decl);
12521 : 946 : if (TREE_CODE (decl) != TYPE_DECL)
12522 : : {
12523 : : /* Propagate exceptions etc. */
12524 : 935 : TREE_TYPE (existing) = TREE_TYPE (decl);
12525 : 935 : TREE_NOTHROW (existing) = TREE_NOTHROW (decl);
12526 : : }
12527 : : /* This is actually an import! */
12528 : 946 : DECL_MODULE_IMPORT_P (existing) = true;
12529 : :
12530 : : /* Yay, sliced! */
12531 : 946 : existing->base = decl->base;
12532 : :
12533 : 946 : if (TREE_CODE (decl) == FUNCTION_DECL)
12534 : : {
12535 : : /* Ew :( */
12536 : 935 : memcpy (&existing->decl_common.size,
12537 : : &decl->decl_common.size,
12538 : : (offsetof (tree_decl_common, pt_uid)
12539 : : - offsetof (tree_decl_common, size)));
12540 : 935 : auto bltin_class = DECL_BUILT_IN_CLASS (decl);
12541 : 935 : existing->function_decl.built_in_class = bltin_class;
12542 : 935 : auto fncode = DECL_UNCHECKED_FUNCTION_CODE (decl);
12543 : 935 : DECL_UNCHECKED_FUNCTION_CODE (existing) = fncode;
12544 : 935 : if (existing->function_decl.built_in_class == BUILT_IN_NORMAL)
12545 : : {
12546 : 815 : if (builtin_decl_explicit_p (built_in_function (fncode)))
12547 : 815 : switch (fncode)
12548 : : {
12549 : 0 : case BUILT_IN_STPCPY:
12550 : 0 : set_builtin_decl_implicit_p
12551 : 0 : (built_in_function (fncode), true);
12552 : 0 : break;
12553 : 815 : default:
12554 : 815 : set_builtin_decl_declared_p
12555 : 815 : (built_in_function (fncode), true);
12556 : 815 : break;
12557 : : }
12558 : 815 : copy_attributes_to_builtin (decl);
12559 : : }
12560 : : }
12561 : : }
12562 : :
12563 : 417774 : if (VAR_OR_FUNCTION_DECL_P (decl)
12564 : 417774 : && DECL_TEMPLATE_INSTANTIATED (decl))
12565 : : /* Don't instantiate again! */
12566 : 8226 : DECL_TEMPLATE_INSTANTIATED (existing) = true;
12567 : :
12568 : 417774 : if (TREE_CODE (d_inner) == FUNCTION_DECL
12569 : 417774 : && DECL_DECLARED_INLINE_P (d_inner))
12570 : : {
12571 : 153344 : DECL_DECLARED_INLINE_P (e_inner) = true;
12572 : 153344 : if (!DECL_SAVED_TREE (e_inner)
12573 : 75230 : && lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (d_inner))
12574 : 153350 : && !lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (e_inner)))
12575 : : {
12576 : 18 : DECL_INTERFACE_KNOWN (e_inner)
12577 : 6 : |= DECL_INTERFACE_KNOWN (d_inner);
12578 : 6 : DECL_DISREGARD_INLINE_LIMITS (e_inner)
12579 : 6 : |= DECL_DISREGARD_INLINE_LIMITS (d_inner);
12580 : : // TODO: we will eventually want to merge all decl attributes
12581 : 6 : duplicate_one_attribute (&DECL_ATTRIBUTES (e_inner),
12582 : 6 : DECL_ATTRIBUTES (d_inner), "gnu_inline");
12583 : : }
12584 : : }
12585 : 417774 : if (!DECL_EXTERNAL (d_inner))
12586 : 197761 : DECL_EXTERNAL (e_inner) = false;
12587 : :
12588 : 417774 : if (VAR_OR_FUNCTION_DECL_P (d_inner))
12589 : 415970 : check_abi_tags (existing, decl,
12590 : 207985 : DECL_ATTRIBUTES (e_inner), DECL_ATTRIBUTES (d_inner));
12591 : :
12592 : 417774 : if (TREE_CODE (decl) == TEMPLATE_DECL)
12593 : : {
12594 : : /* Merge default template arguments. */
12595 : 140892 : tree d_parms = DECL_INNERMOST_TEMPLATE_PARMS (decl);
12596 : 140892 : tree e_parms = DECL_INNERMOST_TEMPLATE_PARMS (existing);
12597 : 140892 : gcc_checking_assert (TREE_VEC_LENGTH (d_parms)
12598 : : == TREE_VEC_LENGTH (e_parms));
12599 : 405429 : for (int i = 0; i < TREE_VEC_LENGTH (d_parms); ++i)
12600 : : {
12601 : 264546 : tree d_default = TREE_PURPOSE (TREE_VEC_ELT (d_parms, i));
12602 : 264546 : tree& e_default = TREE_PURPOSE (TREE_VEC_ELT (e_parms, i));
12603 : 264546 : if (e_default == NULL_TREE)
12604 : 224135 : e_default = d_default;
12605 : 40411 : else if (d_default != NULL_TREE
12606 : 40411 : && !cp_tree_equal (d_default, e_default))
12607 : : {
12608 : 9 : auto_diagnostic_group d;
12609 : 9 : tree d_parm = TREE_VALUE (TREE_VEC_ELT (d_parms, i));
12610 : 9 : tree e_parm = TREE_VALUE (TREE_VEC_ELT (e_parms, i));
12611 : 9 : error_at (DECL_SOURCE_LOCATION (d_parm),
12612 : : "conflicting default argument for %#qD", d_parm);
12613 : 9 : inform (DECL_SOURCE_LOCATION (e_parm),
12614 : : "existing default declared here");
12615 : 9 : return false;
12616 : 9 : }
12617 : : }
12618 : : }
12619 : :
12620 : 417765 : if (TREE_CODE (d_inner) == FUNCTION_DECL)
12621 : : {
12622 : : /* Merge default function arguments. */
12623 : 188627 : tree d_parm = FUNCTION_FIRST_USER_PARMTYPE (d_inner);
12624 : 188627 : tree e_parm = FUNCTION_FIRST_USER_PARMTYPE (e_inner);
12625 : 188627 : int i = 0;
12626 : 448400 : for (; d_parm && d_parm != void_list_node;
12627 : 259773 : d_parm = TREE_CHAIN (d_parm), e_parm = TREE_CHAIN (e_parm), ++i)
12628 : : {
12629 : 259785 : tree d_default = TREE_PURPOSE (d_parm);
12630 : 259785 : tree& e_default = TREE_PURPOSE (e_parm);
12631 : 259785 : if (e_default == NULL_TREE)
12632 : 245000 : e_default = d_default;
12633 : 14785 : else if (d_default != NULL_TREE
12634 : 14785 : && !cp_tree_equal (d_default, e_default))
12635 : : {
12636 : 12 : auto_diagnostic_group d;
12637 : 12 : error_at (get_fndecl_argument_location (d_inner, i),
12638 : : "conflicting default argument for parameter %P of %#qD",
12639 : : i, decl);
12640 : 12 : inform (get_fndecl_argument_location (e_inner, i),
12641 : : "existing default declared here");
12642 : 12 : return false;
12643 : 12 : }
12644 : : }
12645 : : }
12646 : :
12647 : : return true;
12648 : : }
12649 : :
12650 : : /* FN is an implicit member function that we've discovered is new to
12651 : : the class. Add it to the TYPE_FIELDS chain and the method vector.
12652 : : Reset the appropriate classtype lazy flag. */
12653 : :
12654 : : bool
12655 : 869 : trees_in::install_implicit_member (tree fn)
12656 : : {
12657 : 869 : tree ctx = DECL_CONTEXT (fn);
12658 : 869 : tree name = DECL_NAME (fn);
12659 : : /* We know these are synthesized, so the set of expected prototypes
12660 : : is quite restricted. We're not validating correctness, just
12661 : : distinguishing beteeen the small set of possibilities. */
12662 : 869 : tree parm_type = TREE_VALUE (FUNCTION_FIRST_USER_PARMTYPE (fn));
12663 : 869 : if (IDENTIFIER_CTOR_P (name))
12664 : : {
12665 : 586 : if (CLASSTYPE_LAZY_DEFAULT_CTOR (ctx)
12666 : 586 : && VOID_TYPE_P (parm_type))
12667 : 148 : CLASSTYPE_LAZY_DEFAULT_CTOR (ctx) = false;
12668 : 438 : else if (!TYPE_REF_P (parm_type))
12669 : : return false;
12670 : 438 : else if (CLASSTYPE_LAZY_COPY_CTOR (ctx)
12671 : 438 : && !TYPE_REF_IS_RVALUE (parm_type))
12672 : 222 : CLASSTYPE_LAZY_COPY_CTOR (ctx) = false;
12673 : 216 : else if (CLASSTYPE_LAZY_MOVE_CTOR (ctx))
12674 : 216 : CLASSTYPE_LAZY_MOVE_CTOR (ctx) = false;
12675 : : else
12676 : : return false;
12677 : : }
12678 : 283 : else if (IDENTIFIER_DTOR_P (name))
12679 : : {
12680 : 219 : if (CLASSTYPE_LAZY_DESTRUCTOR (ctx))
12681 : 219 : CLASSTYPE_LAZY_DESTRUCTOR (ctx) = false;
12682 : : else
12683 : : return false;
12684 : 219 : if (DECL_VIRTUAL_P (fn))
12685 : : /* A virtual dtor should have been created when the class
12686 : : became complete. */
12687 : : return false;
12688 : : }
12689 : 64 : else if (name == assign_op_identifier)
12690 : : {
12691 : 64 : if (!TYPE_REF_P (parm_type))
12692 : : return false;
12693 : 64 : else if (CLASSTYPE_LAZY_COPY_ASSIGN (ctx)
12694 : 64 : && !TYPE_REF_IS_RVALUE (parm_type))
12695 : 32 : CLASSTYPE_LAZY_COPY_ASSIGN (ctx) = false;
12696 : 32 : else if (CLASSTYPE_LAZY_MOVE_ASSIGN (ctx))
12697 : 32 : CLASSTYPE_LAZY_MOVE_ASSIGN (ctx) = false;
12698 : : else
12699 : : return false;
12700 : : }
12701 : : else
12702 : : return false;
12703 : :
12704 : 899 : dump (dumper::MERGE) && dump ("Adding implicit member %N", fn);
12705 : :
12706 : 869 : DECL_CHAIN (fn) = TYPE_FIELDS (ctx);
12707 : 869 : TYPE_FIELDS (ctx) = fn;
12708 : :
12709 : 869 : add_method (ctx, fn, false);
12710 : :
12711 : : /* Propagate TYPE_FIELDS. */
12712 : 869 : fixup_type_variants (ctx);
12713 : :
12714 : 869 : return true;
12715 : : }
12716 : :
12717 : : /* Return non-zero if DECL has a definition that would be interesting to
12718 : : write out. */
12719 : :
12720 : : static bool
12721 : 1280515 : has_definition (tree decl)
12722 : : {
12723 : 1280515 : bool is_tmpl = TREE_CODE (decl) == TEMPLATE_DECL;
12724 : 1280515 : if (is_tmpl)
12725 : 288464 : decl = DECL_TEMPLATE_RESULT (decl);
12726 : :
12727 : 1280515 : switch (TREE_CODE (decl))
12728 : : {
12729 : : default:
12730 : : break;
12731 : :
12732 : 443876 : case FUNCTION_DECL:
12733 : 443876 : if (!DECL_SAVED_TREE (decl))
12734 : : /* Not defined. */
12735 : : break;
12736 : :
12737 : 192947 : if (DECL_DECLARED_INLINE_P (decl))
12738 : : return true;
12739 : :
12740 : 11274 : if (header_module_p ())
12741 : : /* We always need to write definitions in header modules,
12742 : : since there's no TU to emit them in otherwise. */
12743 : : return true;
12744 : :
12745 : 3337 : if (DECL_TEMPLATE_INFO (decl))
12746 : : {
12747 : 2670 : int use_tpl = DECL_USE_TEMPLATE (decl);
12748 : :
12749 : : // FIXME: Partial specializations have definitions too.
12750 : 2670 : if (use_tpl < 2)
12751 : : return true;
12752 : : }
12753 : : break;
12754 : :
12755 : 548093 : case TYPE_DECL:
12756 : 548093 : {
12757 : 548093 : tree type = TREE_TYPE (decl);
12758 : 548093 : if (type == TYPE_MAIN_VARIANT (type)
12759 : 254414 : && decl == TYPE_NAME (type)
12760 : 802507 : && (TREE_CODE (type) == ENUMERAL_TYPE
12761 : 254414 : ? TYPE_VALUES (type) : TYPE_FIELDS (type)))
12762 : : return true;
12763 : : }
12764 : : break;
12765 : :
12766 : 74693 : case VAR_DECL:
12767 : : /* DECL_INITIALIZED_P might not be set on a dependent VAR_DECL. */
12768 : 74693 : if (DECL_LANG_SPECIFIC (decl)
12769 : 74024 : && DECL_TEMPLATE_INFO (decl)
12770 : 115582 : && DECL_INITIAL (decl))
12771 : : return true;
12772 : : else
12773 : : {
12774 : 37173 : if (!DECL_INITIALIZED_P (decl))
12775 : : /* Not defined. */
12776 : : return false;
12777 : :
12778 : 32019 : if (header_module_p ())
12779 : : /* We always need to write definitions in header modules,
12780 : : since there's no TU to emit them in otherwise. */
12781 : : return true;
12782 : :
12783 : 9988 : if (decl_maybe_constant_var_p (decl))
12784 : : /* We might need its constant value. */
12785 : : return true;
12786 : :
12787 : 339 : if (vague_linkage_p (decl))
12788 : : /* These are emitted as needed. */
12789 : : return true;
12790 : :
12791 : : return false;
12792 : : }
12793 : 5844 : break;
12794 : :
12795 : 5844 : case CONCEPT_DECL:
12796 : 5844 : if (DECL_INITIAL (decl))
12797 : : return true;
12798 : :
12799 : : break;
12800 : : }
12801 : :
12802 : : return false;
12803 : : }
12804 : :
12805 : : uintptr_t *
12806 : 489178 : trees_in::find_duplicate (tree existing)
12807 : : {
12808 : 244406 : if (!duplicates)
12809 : : return NULL;
12810 : :
12811 : 346892 : return duplicates->get (existing);
12812 : : }
12813 : :
12814 : : /* We're starting to read a duplicate DECL. EXISTING is the already
12815 : : known node. */
12816 : :
12817 : : void
12818 : 598937 : trees_in::register_duplicate (tree decl, tree existing)
12819 : : {
12820 : 598937 : if (!duplicates)
12821 : 94983 : duplicates = new duplicate_hash_map (40);
12822 : :
12823 : 598937 : bool existed;
12824 : 598937 : uintptr_t &slot = duplicates->get_or_insert (existing, &existed);
12825 : 598937 : gcc_checking_assert (!existed);
12826 : 598937 : slot = reinterpret_cast<uintptr_t> (decl);
12827 : :
12828 : 598937 : if (TREE_CODE (decl) == TEMPLATE_DECL)
12829 : : /* Also register the DECL_TEMPLATE_RESULT as a duplicate so
12830 : : that passing decl's _RESULT to maybe_duplicate naturally
12831 : : gives us existing's _RESULT back. */
12832 : 281788 : register_duplicate (DECL_TEMPLATE_RESULT (decl),
12833 : 140894 : DECL_TEMPLATE_RESULT (existing));
12834 : 598937 : }
12835 : :
12836 : : /* We've read a definition of MAYBE_EXISTING. If not a duplicate,
12837 : : return MAYBE_EXISTING (into which the definition should be
12838 : : installed). Otherwise return NULL if already known bad, or the
12839 : : duplicate we read (for ODR checking, or extracting additional merge
12840 : : information). */
12841 : :
12842 : : tree
12843 : 244772 : trees_in::odr_duplicate (tree maybe_existing, bool has_defn)
12844 : : {
12845 : 244772 : tree res = NULL_TREE;
12846 : :
12847 : 392883 : if (uintptr_t *dup = find_duplicate (maybe_existing))
12848 : : {
12849 : 140814 : if (!(*dup & 1))
12850 : 140811 : res = reinterpret_cast<tree> (*dup);
12851 : : }
12852 : : else
12853 : : res = maybe_existing;
12854 : :
12855 : 244772 : assert_definition (maybe_existing, res && !has_defn);
12856 : :
12857 : : // FIXME: We probably need to return the template, so that the
12858 : : // template header can be checked?
12859 : 244772 : return res ? STRIP_TEMPLATE (res) : NULL_TREE;
12860 : : }
12861 : :
12862 : : /* The following writer functions rely on the current behaviour of
12863 : : depset::hash::add_dependency making the decl and defn depset nodes
12864 : : depend on eachother. That way we don't have to worry about seeding
12865 : : the tree map with named decls that cannot be looked up by name (I.e
12866 : : template and function parms). We know the decl and definition will
12867 : : be in the same cluster, which is what we want. */
12868 : :
12869 : : void
12870 : 361509 : trees_out::write_function_def (tree decl)
12871 : : {
12872 : 361509 : tree_node (DECL_RESULT (decl));
12873 : :
12874 : 361509 : {
12875 : : /* The function body for a non-inline function or function template
12876 : : is ignored for determining exposures. This should only matter
12877 : : for templates (we don't emit the bodies of non-inline functions
12878 : : to begin with). */
12879 : 361509 : auto ovr = make_temp_override (dep_hash->ignore_tu_local,
12880 : 361509 : !DECL_DECLARED_INLINE_P (decl));
12881 : 361509 : tree_node (DECL_INITIAL (decl));
12882 : 361509 : tree_node (DECL_SAVED_TREE (decl));
12883 : 361509 : }
12884 : :
12885 : 769206 : tree_node (DECL_FRIEND_CONTEXT (decl));
12886 : :
12887 : 361509 : constexpr_fundef *cexpr = retrieve_constexpr_fundef (decl);
12888 : :
12889 : 361509 : if (streaming_p ())
12890 : 180722 : u (cexpr != nullptr);
12891 : 361509 : if (cexpr)
12892 : : {
12893 : 87972 : chained_decls (cexpr->parms);
12894 : 87972 : tree_node (cexpr->result);
12895 : 87972 : tree_node (cexpr->body);
12896 : : }
12897 : :
12898 : 361509 : function* f = DECL_STRUCT_FUNCTION (decl);
12899 : :
12900 : 361509 : if (streaming_p ())
12901 : : {
12902 : 180722 : unsigned flags = 0;
12903 : :
12904 : : /* Whether the importer should emit this definition, if used. */
12905 : 180722 : flags |= 1 * (DECL_NOT_REALLY_EXTERN (decl)
12906 : 180722 : && (get_importer_interface (decl)
12907 : : != importer_interface::external));
12908 : :
12909 : 180722 : if (f)
12910 : : {
12911 : 180105 : flags |= 2;
12912 : : /* These flags are needed in tsubst_lambda_expr. */
12913 : 180105 : flags |= 4 * f->language->returns_value;
12914 : 180105 : flags |= 8 * f->language->returns_null;
12915 : 180105 : flags |= 16 * f->language->returns_abnormally;
12916 : 180105 : flags |= 32 * f->language->infinite_loop;
12917 : : }
12918 : :
12919 : 180722 : u (flags);
12920 : : }
12921 : :
12922 : 361509 : if (state && f)
12923 : : {
12924 : 360275 : state->write_location (*this, f->function_start_locus);
12925 : 360275 : state->write_location (*this, f->function_end_locus);
12926 : : }
12927 : 361509 : }
12928 : :
12929 : : void
12930 : 0 : trees_out::mark_function_def (tree)
12931 : : {
12932 : 0 : }
12933 : :
12934 : : bool
12935 : 154728 : trees_in::read_function_def (tree decl, tree maybe_template)
12936 : : {
12937 : 155173 : dump () && dump ("Reading function definition %N", decl);
12938 : 154728 : tree result = tree_node ();
12939 : 154728 : tree initial = tree_node ();
12940 : 154728 : tree saved = tree_node ();
12941 : 154728 : tree context = tree_node ();
12942 : 154728 : constexpr_fundef cexpr;
12943 : 154728 : post_process_data pdata {};
12944 : 154728 : pdata.decl = maybe_template;
12945 : :
12946 : 154728 : tree maybe_dup = odr_duplicate (maybe_template, DECL_SAVED_TREE (decl));
12947 : 309453 : bool installing = maybe_dup && !DECL_SAVED_TREE (decl);
12948 : :
12949 : 154728 : if (u ())
12950 : : {
12951 : 34879 : cexpr.parms = chained_decls ();
12952 : 34879 : cexpr.result = tree_node ();
12953 : 34879 : cexpr.body = tree_node ();
12954 : 34879 : cexpr.decl = decl;
12955 : : }
12956 : : else
12957 : 119849 : cexpr.decl = NULL_TREE;
12958 : :
12959 : 154728 : unsigned flags = u ();
12960 : :
12961 : 154728 : if (flags & 2)
12962 : : {
12963 : 154175 : pdata.start_locus = state->read_location (*this);
12964 : 154175 : pdata.end_locus = state->read_location (*this);
12965 : 154175 : pdata.returns_value = flags & 4;
12966 : 154175 : pdata.returns_null = flags & 8;
12967 : 154175 : pdata.returns_abnormally = flags & 16;
12968 : 154175 : pdata.infinite_loop = flags & 32;
12969 : : }
12970 : :
12971 : 154728 : if (get_overrun ())
12972 : : return NULL_TREE;
12973 : :
12974 : 154728 : if (installing)
12975 : : {
12976 : 71909 : DECL_NOT_REALLY_EXTERN (decl) = flags & 1;
12977 : 71909 : DECL_RESULT (decl) = result;
12978 : 71909 : DECL_INITIAL (decl) = initial;
12979 : 71909 : DECL_SAVED_TREE (decl) = saved;
12980 : :
12981 : 71909 : if (context)
12982 : 3178 : SET_DECL_FRIEND_CONTEXT (decl, context);
12983 : 71909 : if (cexpr.decl)
12984 : 21805 : register_constexpr_fundef (cexpr);
12985 : 71909 : post_process (pdata);
12986 : : }
12987 : : else if (maybe_dup)
12988 : : {
12989 : : // FIXME:QOI Check matching defn
12990 : : }
12991 : :
12992 : : return true;
12993 : : }
12994 : :
12995 : : /* Also for CONCEPT_DECLs. */
12996 : :
12997 : : void
12998 : 107674 : trees_out::write_var_def (tree decl)
12999 : : {
13000 : : /* The initializer of a non-inline variable or variable template is
13001 : : ignored for determining exposures. */
13002 : 107674 : auto ovr = make_temp_override (dep_hash->ignore_tu_local,
13003 : 107674 : VAR_P (decl) && !DECL_INLINE_VAR_P (decl));
13004 : :
13005 : 107674 : tree init = DECL_INITIAL (decl);
13006 : 107674 : tree_node (init);
13007 : 107674 : if (!init)
13008 : : {
13009 : 1210 : tree dyn_init = NULL_TREE;
13010 : :
13011 : : /* We only need to write initializers in header modules. */
13012 : 2248 : if (header_module_p () && DECL_NONTRIVIALLY_INITIALIZED_P (decl))
13013 : : {
13014 : 450 : dyn_init = value_member (decl,
13015 : 450 : CP_DECL_THREAD_LOCAL_P (decl)
13016 : : ? tls_aggregates : static_aggregates);
13017 : 450 : gcc_checking_assert (dyn_init);
13018 : : /* Mark it so write_inits knows this is needed. */
13019 : 450 : TREE_LANG_FLAG_0 (dyn_init) = true;
13020 : 450 : dyn_init = TREE_PURPOSE (dyn_init);
13021 : : }
13022 : 1210 : tree_node (dyn_init);
13023 : : }
13024 : 107674 : }
13025 : :
13026 : : void
13027 : 0 : trees_out::mark_var_def (tree)
13028 : : {
13029 : 0 : }
13030 : :
13031 : : bool
13032 : 35401 : trees_in::read_var_def (tree decl, tree maybe_template)
13033 : : {
13034 : : /* Do not mark the virtual table entries as used. */
13035 : 35401 : bool vtable = VAR_P (decl) && DECL_VTABLE_OR_VTT_P (decl);
13036 : 35401 : unused += vtable;
13037 : 35401 : tree init = tree_node ();
13038 : 35401 : tree dyn_init = init ? NULL_TREE : tree_node ();
13039 : 35401 : unused -= vtable;
13040 : :
13041 : 35401 : if (get_overrun ())
13042 : : return false;
13043 : :
13044 : 35401 : bool initialized = (VAR_P (decl) ? bool (DECL_INITIALIZED_P (decl))
13045 : 35401 : : bool (DECL_INITIAL (decl)));
13046 : 35401 : tree maybe_dup = odr_duplicate (maybe_template, initialized);
13047 : 35401 : bool installing = maybe_dup && !initialized;
13048 : 35401 : if (installing)
13049 : : {
13050 : 19437 : DECL_INITIAL (decl) = init;
13051 : 19437 : if (DECL_EXTERNAL (decl))
13052 : 2439 : DECL_NOT_REALLY_EXTERN (decl) = true;
13053 : 19437 : if (VAR_P (decl))
13054 : : {
13055 : 17310 : DECL_INITIALIZED_P (decl) = true;
13056 : 17310 : if (maybe_dup && DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (maybe_dup))
13057 : 16968 : DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
13058 : 17310 : tentative_decl_linkage (decl);
13059 : 17310 : if (DECL_EXPLICIT_INSTANTIATION (decl)
13060 : 17310 : && !DECL_EXTERNAL (decl))
13061 : 9 : setup_explicit_instantiation_definition_linkage (decl);
13062 : 17310 : if (DECL_IMPLICIT_INSTANTIATION (decl)
13063 : 16524 : || (DECL_EXPLICIT_INSTANTIATION (decl)
13064 : 18 : && !DECL_EXTERNAL (decl))
13065 : 33825 : || (DECL_CLASS_SCOPE_P (decl)
13066 : 10070 : && !DECL_VTABLE_OR_VTT_P (decl)
13067 : 8830 : && !DECL_TEMPLATE_INFO (decl)))
13068 : 6777 : note_vague_linkage_variable (decl);
13069 : : }
13070 : 19437 : if (!dyn_init)
13071 : : ;
13072 : 216 : else if (CP_DECL_THREAD_LOCAL_P (decl))
13073 : 96 : tls_aggregates = tree_cons (dyn_init, decl, tls_aggregates);
13074 : : else
13075 : 120 : static_aggregates = tree_cons (dyn_init, decl, static_aggregates);
13076 : : }
13077 : : else if (maybe_dup)
13078 : : {
13079 : : // FIXME:QOI Check matching defn
13080 : : }
13081 : :
13082 : : return true;
13083 : : }
13084 : :
13085 : : /* If MEMBER doesn't have an independent life outside the class,
13086 : : return it (or its TEMPLATE_DECL). Otherwise NULL. */
13087 : :
13088 : : static tree
13089 : 184064 : member_owned_by_class (tree member)
13090 : : {
13091 : 184064 : gcc_assert (DECL_P (member));
13092 : :
13093 : : /* Clones are owned by their origin. */
13094 : 184064 : if (DECL_CLONED_FUNCTION_P (member))
13095 : : return NULL;
13096 : :
13097 : 184064 : if (TREE_CODE (member) == FIELD_DECL)
13098 : : /* FIELD_DECLS can have template info in some cases. We always
13099 : : want the FIELD_DECL though, as there's never a TEMPLATE_DECL
13100 : : wrapping them. */
13101 : : return member;
13102 : :
13103 : 86117 : int use_tpl = -1;
13104 : 86117 : if (tree ti = node_template_info (member, use_tpl))
13105 : : {
13106 : : // FIXME: Don't bail on things that CANNOT have their own
13107 : : // template header. No, make sure they're in the same cluster.
13108 : 0 : if (use_tpl > 0)
13109 : : return NULL_TREE;
13110 : :
13111 : 0 : if (DECL_TEMPLATE_RESULT (TI_TEMPLATE (ti)) == member)
13112 : 184064 : member = TI_TEMPLATE (ti);
13113 : : }
13114 : : return member;
13115 : : }
13116 : :
13117 : : void
13118 : 141808 : trees_out::write_class_def (tree defn)
13119 : : {
13120 : 141808 : gcc_assert (DECL_P (defn));
13121 : 141808 : if (streaming_p ())
13122 : 71189 : dump () && dump ("Writing class definition %N", defn);
13123 : :
13124 : 141808 : tree type = TREE_TYPE (defn);
13125 : 141808 : tree_node (TYPE_SIZE (type));
13126 : 141808 : tree_node (TYPE_SIZE_UNIT (type));
13127 : 141808 : tree_node (TYPE_VFIELD (type));
13128 : 141808 : tree_node (TYPE_BINFO (type));
13129 : :
13130 : 141808 : vec_chained_decls (TYPE_FIELDS (type));
13131 : :
13132 : : /* Every class but __as_base has a type-specific. */
13133 : 281374 : gcc_checking_assert (!TYPE_LANG_SPECIFIC (type) == IS_FAKE_BASE_TYPE (type));
13134 : :
13135 : 141808 : if (TYPE_LANG_SPECIFIC (type))
13136 : : {
13137 : 139566 : {
13138 : 139566 : vec<tree, va_gc> *v = CLASSTYPE_MEMBER_VEC (type);
13139 : 139566 : if (!v)
13140 : : {
13141 : 37456 : gcc_checking_assert (!streaming_p ());
13142 : : /* Force a class vector. */
13143 : 37456 : v = set_class_bindings (type, -1);
13144 : 37456 : gcc_checking_assert (v);
13145 : : }
13146 : :
13147 : 139566 : unsigned len = v->length ();
13148 : 139566 : if (streaming_p ())
13149 : 69763 : u (len);
13150 : 1097476 : for (unsigned ix = 0; ix != len; ix++)
13151 : : {
13152 : 957910 : tree m = (*v)[ix];
13153 : 957910 : if (TREE_CODE (m) == TYPE_DECL
13154 : 292263 : && DECL_ARTIFICIAL (m)
13155 : 1108223 : && TYPE_STUB_DECL (TREE_TYPE (m)) == m)
13156 : : /* This is a using-decl for a type, or an anonymous
13157 : : struct (maybe with a typedef name). Write the type. */
13158 : 9980 : m = TREE_TYPE (m);
13159 : 957910 : tree_node (m);
13160 : : }
13161 : : }
13162 : 139566 : tree_node (CLASSTYPE_LAMBDA_EXPR (type));
13163 : :
13164 : : /* TYPE_CONTAINS_VPTR_P looks at the vbase vector, which the
13165 : : reader won't know at this point. */
13166 : 139566 : int has_vptr = TYPE_CONTAINS_VPTR_P (type);
13167 : :
13168 : 139566 : if (streaming_p ())
13169 : : {
13170 : 69763 : unsigned nvbases = vec_safe_length (CLASSTYPE_VBASECLASSES (type));
13171 : 69763 : u (nvbases);
13172 : 69763 : i (has_vptr);
13173 : : }
13174 : :
13175 : 139566 : if (has_vptr)
13176 : : {
13177 : 4820 : tree_vec (CLASSTYPE_PURE_VIRTUALS (type));
13178 : 4820 : tree_pair_vec (CLASSTYPE_VCALL_INDICES (type));
13179 : 4820 : tree_node (CLASSTYPE_KEY_METHOD (type));
13180 : : }
13181 : : }
13182 : :
13183 : 141808 : if (TYPE_LANG_SPECIFIC (type))
13184 : : {
13185 : 139566 : tree_node (CLASSTYPE_PRIMARY_BINFO (type));
13186 : :
13187 : 139566 : tree as_base = CLASSTYPE_AS_BASE (type);
13188 : 139566 : if (as_base)
13189 : 71633 : as_base = TYPE_NAME (as_base);
13190 : 139566 : tree_node (as_base);
13191 : :
13192 : : /* Write the vtables. */
13193 : 139566 : tree vtables = CLASSTYPE_VTABLES (type);
13194 : 139566 : vec_chained_decls (vtables);
13195 : 284768 : for (; vtables; vtables = TREE_CHAIN (vtables))
13196 : 5636 : write_definition (vtables);
13197 : :
13198 : 139566 : {
13199 : : /* Friend declarations in class definitions are ignored when
13200 : : determining exposures. */
13201 : 139566 : auto ovr = make_temp_override (dep_hash->ignore_tu_local, true);
13202 : :
13203 : : /* Write the friend classes. */
13204 : 139566 : tree_list (CLASSTYPE_FRIEND_CLASSES (type), false);
13205 : :
13206 : : /* Write the friend functions. */
13207 : 139566 : for (tree friends = DECL_FRIENDLIST (defn);
13208 : 157962 : friends; friends = TREE_CHAIN (friends))
13209 : : {
13210 : 18396 : tree_node (FRIEND_NAME (friends));
13211 : 18396 : tree_list (FRIEND_DECLS (friends), false);
13212 : : }
13213 : : /* End of friend fns. */
13214 : 139566 : tree_node (NULL_TREE);
13215 : 139566 : }
13216 : :
13217 : : /* Write the decl list. We don't need to ignore exposures of friend
13218 : : decls here as any such decls should already have been added and
13219 : : ignored above. */
13220 : 139566 : tree_list (CLASSTYPE_DECL_LIST (type), true);
13221 : :
13222 : 139566 : if (TYPE_CONTAINS_VPTR_P (type))
13223 : : {
13224 : : /* Write the thunks. */
13225 : 4820 : for (tree decls = TYPE_FIELDS (type);
13226 : 136758 : decls; decls = DECL_CHAIN (decls))
13227 : 131938 : if (TREE_CODE (decls) == FUNCTION_DECL
13228 : 95522 : && DECL_VIRTUAL_P (decls)
13229 : 156692 : && DECL_THUNKS (decls))
13230 : : {
13231 : 852 : tree_node (decls);
13232 : : /* Thunks are always unique, so chaining is ok. */
13233 : 852 : chained_decls (DECL_THUNKS (decls));
13234 : : }
13235 : 4820 : tree_node (NULL_TREE);
13236 : : }
13237 : : }
13238 : 141808 : }
13239 : :
13240 : : void
13241 : 184064 : trees_out::mark_class_member (tree member, bool do_defn)
13242 : : {
13243 : 184064 : gcc_assert (DECL_P (member));
13244 : :
13245 : 184064 : member = member_owned_by_class (member);
13246 : 184064 : if (member)
13247 : 368128 : mark_declaration (member, do_defn && has_definition (member));
13248 : 184064 : }
13249 : :
13250 : : void
13251 : 141836 : trees_out::mark_class_def (tree defn)
13252 : : {
13253 : 141836 : gcc_assert (DECL_P (defn));
13254 : 141836 : tree type = TREE_TYPE (defn);
13255 : : /* Mark the class members that are not type-decls and cannot have
13256 : : independent definitions. */
13257 : 1476734 : for (tree member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
13258 : 1334898 : if (TREE_CODE (member) == FIELD_DECL
13259 : 1334898 : || TREE_CODE (member) == USING_DECL
13260 : : /* A cloned enum-decl from 'using enum unrelated;' */
13261 : 1334898 : || (TREE_CODE (member) == CONST_DECL
13262 : 13504 : && DECL_CONTEXT (member) == type))
13263 : : {
13264 : 184064 : mark_class_member (member);
13265 : 184064 : if (TREE_CODE (member) == FIELD_DECL)
13266 : 97947 : if (tree repr = DECL_BIT_FIELD_REPRESENTATIVE (member))
13267 : : /* If we're marking a class template definition, then
13268 : : this'll contain the width (as set by grokbitfield)
13269 : : instead of a decl. */
13270 : 2050 : if (DECL_P (repr))
13271 : 1652 : mark_declaration (repr, false);
13272 : : }
13273 : :
13274 : : /* Mark the binfo hierarchy. */
13275 : 336360 : for (tree child = TYPE_BINFO (type); child; child = TREE_CHAIN (child))
13276 : 194524 : mark_by_value (child);
13277 : :
13278 : 141836 : if (TYPE_LANG_SPECIFIC (type))
13279 : : {
13280 : 139570 : for (tree vtable = CLASSTYPE_VTABLES (type);
13281 : 145206 : vtable; vtable = TREE_CHAIN (vtable))
13282 : 5636 : mark_declaration (vtable, true);
13283 : :
13284 : 139570 : if (TYPE_CONTAINS_VPTR_P (type))
13285 : : /* Mark the thunks, they belong to the class definition,
13286 : : /not/ the thunked-to function. */
13287 : 4820 : for (tree decls = TYPE_FIELDS (type);
13288 : 136758 : decls; decls = DECL_CHAIN (decls))
13289 : 131938 : if (TREE_CODE (decls) == FUNCTION_DECL)
13290 : 95522 : for (tree thunks = DECL_THUNKS (decls);
13291 : 96650 : thunks; thunks = DECL_CHAIN (thunks))
13292 : 1128 : mark_declaration (thunks, false);
13293 : : }
13294 : 141836 : }
13295 : :
13296 : : /* Nop sorting, needed for resorting the member vec. */
13297 : :
13298 : : static void
13299 : 5779018 : nop (void *, void *, void *)
13300 : : {
13301 : 5779018 : }
13302 : :
13303 : : bool
13304 : 52230 : trees_in::read_class_def (tree defn, tree maybe_template)
13305 : : {
13306 : 52230 : gcc_assert (DECL_P (defn));
13307 : 52767 : dump () && dump ("Reading class definition %N", defn);
13308 : 52230 : tree type = TREE_TYPE (defn);
13309 : 52230 : tree size = tree_node ();
13310 : 52230 : tree size_unit = tree_node ();
13311 : 52230 : tree vfield = tree_node ();
13312 : 52230 : tree binfo = tree_node ();
13313 : 52230 : vec<tree, va_gc> *vbase_vec = NULL;
13314 : 52230 : vec<tree, va_gc> *member_vec = NULL;
13315 : 52230 : vec<tree, va_gc> *pure_virts = NULL;
13316 : 52230 : vec<tree_pair_s, va_gc> *vcall_indices = NULL;
13317 : 52230 : tree key_method = NULL_TREE;
13318 : 52230 : tree lambda = NULL_TREE;
13319 : :
13320 : : /* Read the fields. */
13321 : 52230 : vec<tree, va_heap> *fields = vec_chained_decls ();
13322 : :
13323 : 52230 : if (TYPE_LANG_SPECIFIC (type))
13324 : : {
13325 : 51188 : if (unsigned len = u ())
13326 : : {
13327 : 51188 : vec_alloc (member_vec, len);
13328 : 434253 : for (unsigned ix = 0; ix != len; ix++)
13329 : : {
13330 : 383065 : tree m = tree_node ();
13331 : 383065 : if (get_overrun ())
13332 : : break;
13333 : 383065 : if (TYPE_P (m))
13334 : 3674 : m = TYPE_STUB_DECL (m);
13335 : 383065 : member_vec->quick_push (m);
13336 : : }
13337 : : }
13338 : 51188 : lambda = tree_node ();
13339 : :
13340 : 51188 : if (!get_overrun ())
13341 : : {
13342 : 51188 : unsigned nvbases = u ();
13343 : 51188 : if (nvbases)
13344 : : {
13345 : 246 : vec_alloc (vbase_vec, nvbases);
13346 : 1117 : for (tree child = binfo; child; child = TREE_CHAIN (child))
13347 : 871 : if (BINFO_VIRTUAL_P (child))
13348 : 246 : vbase_vec->quick_push (child);
13349 : : }
13350 : : }
13351 : :
13352 : 51188 : if (!get_overrun ())
13353 : : {
13354 : 51188 : int has_vptr = i ();
13355 : 51188 : if (has_vptr)
13356 : : {
13357 : 2078 : pure_virts = tree_vec ();
13358 : 2078 : vcall_indices = tree_pair_vec ();
13359 : 2078 : key_method = tree_node ();
13360 : : }
13361 : : }
13362 : : }
13363 : :
13364 : 52230 : tree maybe_dup = odr_duplicate (maybe_template, TYPE_SIZE (type));
13365 : 52230 : bool installing = maybe_dup && !TYPE_SIZE (type);
13366 : 23240 : if (installing)
13367 : : {
13368 : 23240 : if (maybe_dup != defn)
13369 : : {
13370 : : // FIXME: This is needed on other defns too, almost
13371 : : // duplicate-decl like? See is_matching_decl too.
13372 : : /* Copy flags from the duplicate. */
13373 : 234 : tree type_dup = TREE_TYPE (maybe_dup);
13374 : :
13375 : : /* Core pieces. */
13376 : 234 : TYPE_MODE_RAW (type) = TYPE_MODE_RAW (type_dup);
13377 : 234 : TYPE_ALIGN_RAW (type) = TYPE_ALIGN_RAW (type_dup);
13378 : 468 : TYPE_WARN_IF_NOT_ALIGN_RAW (type)
13379 : 234 : = TYPE_WARN_IF_NOT_ALIGN_RAW (type_dup);
13380 : 234 : TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (type_dup);
13381 : :
13382 : 234 : SET_DECL_MODE (defn, DECL_MODE (maybe_dup));
13383 : 234 : DECL_SIZE (defn) = DECL_SIZE (maybe_dup);
13384 : 234 : DECL_SIZE_UNIT (defn) = DECL_SIZE_UNIT (maybe_dup);
13385 : 234 : DECL_ALIGN_RAW (defn) = DECL_ALIGN_RAW (maybe_dup);
13386 : 468 : DECL_WARN_IF_NOT_ALIGN_RAW (defn)
13387 : 234 : = DECL_WARN_IF_NOT_ALIGN_RAW (maybe_dup);
13388 : 234 : DECL_USER_ALIGN (defn) = DECL_USER_ALIGN (maybe_dup);
13389 : :
13390 : 234 : TYPE_TYPELESS_STORAGE (type) = TYPE_TYPELESS_STORAGE (type_dup);
13391 : 234 : TYPE_CXX_ODR_P (type) = TYPE_CXX_ODR_P (type_dup);
13392 : 234 : TYPE_NO_FORCE_BLK (type) = TYPE_NO_FORCE_BLK (type_dup);
13393 : 234 : TYPE_TRANSPARENT_AGGR (type) = TYPE_TRANSPARENT_AGGR (type_dup);
13394 : 468 : TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type)
13395 : 234 : = TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type_dup);
13396 : :
13397 : 234 : TYPE_EMPTY_P (type) = TYPE_EMPTY_P (type_dup);
13398 : 234 : TREE_ADDRESSABLE (type) = TREE_ADDRESSABLE (type_dup);
13399 : :
13400 : : /* C++ pieces. */
13401 : 234 : TYPE_POLYMORPHIC_P (type) = TYPE_POLYMORPHIC_P (type_dup);
13402 : 234 : CLASSTYPE_FINAL (type) = CLASSTYPE_FINAL (type_dup);
13403 : :
13404 : 468 : TYPE_HAS_USER_CONSTRUCTOR (type)
13405 : 234 : = TYPE_HAS_USER_CONSTRUCTOR (type_dup);
13406 : 468 : TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
13407 : 234 : = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type_dup);
13408 : 468 : TYPE_NEEDS_CONSTRUCTING (type)
13409 : 234 : = TYPE_NEEDS_CONSTRUCTING (type_dup);
13410 : :
13411 : 234 : if (auto ls = TYPE_LANG_SPECIFIC (type_dup))
13412 : : {
13413 : 234 : if (TYPE_LANG_SPECIFIC (type))
13414 : : {
13415 : 702 : CLASSTYPE_BEFRIENDING_CLASSES (type_dup)
13416 : 234 : = CLASSTYPE_BEFRIENDING_CLASSES (type);
13417 : 234 : if (!ANON_AGGR_TYPE_P (type))
13418 : 702 : CLASSTYPE_TYPEINFO_VAR (type_dup)
13419 : 234 : = CLASSTYPE_TYPEINFO_VAR (type);
13420 : : }
13421 : 1087 : for (tree v = type; v; v = TYPE_NEXT_VARIANT (v))
13422 : 853 : TYPE_LANG_SPECIFIC (v) = ls;
13423 : : }
13424 : : }
13425 : :
13426 : 23240 : TYPE_SIZE (type) = size;
13427 : 23240 : TYPE_SIZE_UNIT (type) = size_unit;
13428 : :
13429 : 23240 : if (fields)
13430 : : {
13431 : 23240 : tree *chain = &TYPE_FIELDS (type);
13432 : 23240 : unsigned len = fields->length ();
13433 : 259659 : for (unsigned ix = 0; ix != len; ix++)
13434 : : {
13435 : 236419 : tree decl = (*fields)[ix];
13436 : :
13437 : 236419 : if (!decl)
13438 : : {
13439 : : /* An anonymous struct with typedef name. */
13440 : 3 : tree tdef = (*fields)[ix+1];
13441 : 3 : decl = TYPE_STUB_DECL (TREE_TYPE (tdef));
13442 : 3 : gcc_checking_assert (IDENTIFIER_ANON_P (DECL_NAME (decl))
13443 : : && decl != tdef);
13444 : : }
13445 : :
13446 : 428891 : gcc_checking_assert (!*chain == !DECL_CLONED_FUNCTION_P (decl));
13447 : 236419 : *chain = decl;
13448 : 236419 : chain = &DECL_CHAIN (decl);
13449 : :
13450 : 236419 : if (TREE_CODE (decl) == FIELD_DECL
13451 : 236419 : && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
13452 : : {
13453 : 159 : tree anon_type = TYPE_MAIN_VARIANT (TREE_TYPE (decl));
13454 : 159 : if (DECL_NAME (defn) == as_base_identifier)
13455 : : /* ANON_AGGR_TYPE_FIELD should already point to the
13456 : : original FIELD_DECL; don't overwrite it to point
13457 : : to the as-base FIELD_DECL copy. */
13458 : 10 : gcc_checking_assert (ANON_AGGR_TYPE_FIELD (anon_type));
13459 : : else
13460 : 149 : ANON_AGGR_TYPE_FIELD (anon_type) = decl;
13461 : : }
13462 : :
13463 : 236419 : if (TREE_CODE (decl) == USING_DECL
13464 : 236419 : && TREE_CODE (USING_DECL_SCOPE (decl)) == RECORD_TYPE)
13465 : : {
13466 : : /* Reconstruct DECL_ACCESS. */
13467 : 10783 : tree decls = USING_DECL_DECLS (decl);
13468 : 10783 : tree access = declared_access (decl);
13469 : :
13470 : 12328 : for (ovl_iterator iter (decls); iter; ++iter)
13471 : : {
13472 : 985 : tree d = *iter;
13473 : :
13474 : 985 : retrofit_lang_decl (d);
13475 : 985 : tree list = DECL_ACCESS (d);
13476 : :
13477 : 985 : if (!purpose_member (type, list))
13478 : 1159 : DECL_ACCESS (d) = tree_cons (type, access, list);
13479 : : }
13480 : : }
13481 : : }
13482 : : }
13483 : :
13484 : 23240 : TYPE_VFIELD (type) = vfield;
13485 : 23240 : TYPE_BINFO (type) = binfo;
13486 : :
13487 : 23240 : if (TYPE_LANG_SPECIFIC (type))
13488 : : {
13489 : 22708 : if (!TYPE_POLYMORPHIC_P (type))
13490 : 21690 : SET_CLASSTYPE_LAMBDA_EXPR (type, lambda);
13491 : : else
13492 : 1018 : gcc_checking_assert (lambda == NULL_TREE);
13493 : :
13494 : 22708 : CLASSTYPE_MEMBER_VEC (type) = member_vec;
13495 : 22708 : CLASSTYPE_PURE_VIRTUALS (type) = pure_virts;
13496 : 22708 : CLASSTYPE_VCALL_INDICES (type) = vcall_indices;
13497 : :
13498 : 22708 : if (TYPE_POLYMORPHIC_P (type))
13499 : 1018 : SET_CLASSTYPE_KEY_METHOD (type, key_method);
13500 : : else
13501 : 21690 : gcc_checking_assert (key_method == NULL_TREE);
13502 : :
13503 : 22708 : CLASSTYPE_VBASECLASSES (type) = vbase_vec;
13504 : :
13505 : : /* Resort the member vector. */
13506 : 22708 : resort_type_member_vec (member_vec, NULL, nop, NULL);
13507 : : }
13508 : : }
13509 : : else if (maybe_dup)
13510 : : {
13511 : : // FIXME:QOI Check matching defn
13512 : : }
13513 : :
13514 : 52230 : if (TYPE_LANG_SPECIFIC (type))
13515 : : {
13516 : 51188 : tree primary = tree_node ();
13517 : 51188 : tree as_base = tree_node ();
13518 : :
13519 : 51188 : if (as_base)
13520 : 25692 : as_base = TREE_TYPE (as_base);
13521 : :
13522 : : /* Read the vtables. */
13523 : 51188 : vec<tree, va_heap> *vtables = vec_chained_decls ();
13524 : 51188 : if (vtables)
13525 : : {
13526 : 2044 : unsigned len = vtables->length ();
13527 : 4503 : for (unsigned ix = 0; ix != len; ix++)
13528 : : {
13529 : 2459 : tree vtable = (*vtables)[ix];
13530 : 2459 : read_var_def (vtable, vtable);
13531 : : }
13532 : : }
13533 : :
13534 : 51188 : tree friend_classes = tree_list (false);
13535 : 51188 : tree friend_functions = NULL_TREE;
13536 : 51188 : for (tree *chain = &friend_functions;
13537 : 59683 : tree name = tree_node (); chain = &TREE_CHAIN (*chain))
13538 : : {
13539 : 8495 : tree val = tree_list (false);
13540 : 8495 : *chain = build_tree_list (name, val);
13541 : 8495 : }
13542 : 51188 : tree decl_list = tree_list (true);
13543 : :
13544 : 51188 : if (installing)
13545 : : {
13546 : 22708 : CLASSTYPE_PRIMARY_BINFO (type) = primary;
13547 : 22708 : CLASSTYPE_AS_BASE (type) = as_base;
13548 : :
13549 : 22708 : if (vtables)
13550 : : {
13551 : 1030 : if ((!CLASSTYPE_KEY_METHOD (type)
13552 : : /* Sneaky user may have defined it inline
13553 : : out-of-class. */
13554 : 598 : || DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (type)))
13555 : : /* An imported non-template class attached to a module
13556 : : doesn't need to have its vtables emitted here. */
13557 : 1036 : && (CLASSTYPE_USE_TEMPLATE (type)
13558 : 138 : || !DECL_MODULE_ATTACH_P (defn)))
13559 : 689 : vec_safe_push (keyed_classes, type);
13560 : 1030 : unsigned len = vtables->length ();
13561 : 1030 : tree *chain = &CLASSTYPE_VTABLES (type);
13562 : 2270 : for (unsigned ix = 0; ix != len; ix++)
13563 : : {
13564 : 1240 : tree vtable = (*vtables)[ix];
13565 : 1240 : gcc_checking_assert (!*chain);
13566 : 1240 : *chain = vtable;
13567 : 1240 : chain = &DECL_CHAIN (vtable);
13568 : : }
13569 : : }
13570 : 22708 : CLASSTYPE_FRIEND_CLASSES (type) = friend_classes;
13571 : 22708 : DECL_FRIENDLIST (defn) = friend_functions;
13572 : 22708 : CLASSTYPE_DECL_LIST (type) = decl_list;
13573 : :
13574 : 24188 : for (; friend_classes; friend_classes = TREE_CHAIN (friend_classes))
13575 : : {
13576 : 1480 : tree f = TREE_VALUE (friend_classes);
13577 : 1480 : if (TREE_CODE (f) == TEMPLATE_DECL)
13578 : 594 : f = TREE_TYPE (f);
13579 : :
13580 : 1480 : if (CLASS_TYPE_P (f))
13581 : : {
13582 : 1448 : CLASSTYPE_BEFRIENDING_CLASSES (f)
13583 : 2896 : = tree_cons (NULL_TREE, type,
13584 : 1448 : CLASSTYPE_BEFRIENDING_CLASSES (f));
13585 : 1486 : dump () && dump ("Class %N befriending %C:%N",
13586 : 6 : type, TREE_CODE (f), f);
13587 : : }
13588 : : }
13589 : :
13590 : 26499 : for (; friend_functions;
13591 : 3791 : friend_functions = TREE_CHAIN (friend_functions))
13592 : 3791 : for (tree friend_decls = TREE_VALUE (friend_functions);
13593 : 8539 : friend_decls; friend_decls = TREE_CHAIN (friend_decls))
13594 : : {
13595 : 4748 : tree f = TREE_VALUE (friend_decls);
13596 : 4748 : if (TREE_CODE (f) == TU_LOCAL_ENTITY)
13597 : 36 : continue;
13598 : :
13599 : 4712 : DECL_BEFRIENDING_CLASSES (f)
13600 : 4712 : = tree_cons (NULL_TREE, type, DECL_BEFRIENDING_CLASSES (f));
13601 : 4775 : dump () && dump ("Class %N befriending %C:%N",
13602 : 27 : type, TREE_CODE (f), f);
13603 : : }
13604 : : }
13605 : :
13606 : 51188 : if (TYPE_CONTAINS_VPTR_P (type))
13607 : : /* Read and install the thunks. */
13608 : 2454 : while (tree vfunc = tree_node ())
13609 : : {
13610 : 376 : tree thunks = chained_decls ();
13611 : 376 : if (installing)
13612 : 186 : SET_DECL_THUNKS (vfunc, thunks);
13613 : : }
13614 : :
13615 : 51188 : vec_free (vtables);
13616 : : }
13617 : :
13618 : : /* Propagate to all variants. */
13619 : 52230 : if (installing)
13620 : 23240 : fixup_type_variants (type);
13621 : :
13622 : : /* IS_FAKE_BASE_TYPE is inaccurate at this point, because if this is
13623 : : the fake base, we've not hooked it into the containing class's
13624 : : data structure yet. Fortunately it has a unique name. */
13625 : 23240 : if (installing
13626 : 23240 : && DECL_NAME (defn) != as_base_identifier
13627 : 22708 : && (!CLASSTYPE_TEMPLATE_INFO (type)
13628 : 19279 : || !uses_template_parms (TI_ARGS (CLASSTYPE_TEMPLATE_INFO (type)))))
13629 : : /* Emit debug info. It'd be nice to know if the interface TU
13630 : : already emitted this. */
13631 : 12722 : rest_of_type_compilation (type, !LOCAL_CLASS_P (type));
13632 : :
13633 : 52230 : vec_free (fields);
13634 : :
13635 : 52230 : return !get_overrun ();
13636 : : }
13637 : :
13638 : : void
13639 : 7368 : trees_out::write_enum_def (tree decl)
13640 : : {
13641 : 7368 : tree type = TREE_TYPE (decl);
13642 : :
13643 : 7368 : tree_node (TYPE_VALUES (type));
13644 : : /* Note that we stream TYPE_MIN/MAX_VALUE directly as part of the
13645 : : ENUMERAL_TYPE. */
13646 : 7368 : }
13647 : :
13648 : : void
13649 : 7368 : trees_out::mark_enum_def (tree decl)
13650 : : {
13651 : 7368 : tree type = TREE_TYPE (decl);
13652 : :
13653 : 36238 : for (tree values = TYPE_VALUES (type); values; values = TREE_CHAIN (values))
13654 : : {
13655 : 28870 : tree cst = TREE_VALUE (values);
13656 : 28870 : mark_by_value (cst);
13657 : : /* We must mark the init to avoid circularity in tt_enum_int. */
13658 : 28870 : if (tree init = DECL_INITIAL (cst))
13659 : 28662 : if (TREE_CODE (init) == INTEGER_CST)
13660 : 28102 : mark_by_value (init);
13661 : : }
13662 : 7368 : }
13663 : :
13664 : : bool
13665 : 2413 : trees_in::read_enum_def (tree defn, tree maybe_template)
13666 : : {
13667 : 2413 : tree type = TREE_TYPE (defn);
13668 : 2413 : tree values = tree_node ();
13669 : :
13670 : 2413 : if (get_overrun ())
13671 : : return false;
13672 : :
13673 : 2413 : tree maybe_dup = odr_duplicate (maybe_template, TYPE_VALUES (type));
13674 : 4826 : bool installing = maybe_dup && !TYPE_VALUES (type);
13675 : :
13676 : 2413 : if (installing)
13677 : : {
13678 : 1004 : TYPE_VALUES (type) = values;
13679 : : /* Note that we stream TYPE_MIN/MAX_VALUE directly as part of the
13680 : : ENUMERAL_TYPE. */
13681 : :
13682 : 1637 : rest_of_type_compilation (type, DECL_NAMESPACE_SCOPE_P (defn));
13683 : : }
13684 : 1409 : else if (maybe_dup)
13685 : : {
13686 : 1409 : tree known = TYPE_VALUES (type);
13687 : 8092 : for (; known && values;
13688 : 6683 : known = TREE_CHAIN (known), values = TREE_CHAIN (values))
13689 : : {
13690 : 6692 : tree known_decl = TREE_VALUE (known);
13691 : 6692 : tree new_decl = TREE_VALUE (values);
13692 : :
13693 : 6692 : if (DECL_NAME (known_decl) != DECL_NAME (new_decl))
13694 : : break;
13695 : :
13696 : 6686 : new_decl = maybe_duplicate (new_decl);
13697 : :
13698 : 6686 : if (!cp_tree_equal (DECL_INITIAL (known_decl),
13699 : 6686 : DECL_INITIAL (new_decl)))
13700 : : break;
13701 : : }
13702 : :
13703 : 1409 : if (known || values)
13704 : : {
13705 : 12 : auto_diagnostic_group d;
13706 : 12 : error_at (DECL_SOURCE_LOCATION (maybe_dup),
13707 : : "definition of %qD does not match", maybe_dup);
13708 : 12 : inform (DECL_SOURCE_LOCATION (defn),
13709 : : "existing definition %qD", defn);
13710 : :
13711 : 12 : tree known_decl = NULL_TREE, new_decl = NULL_TREE;
13712 : :
13713 : 12 : if (known)
13714 : 9 : known_decl = TREE_VALUE (known);
13715 : 12 : if (values)
13716 : 12 : new_decl = maybe_duplicate (TREE_VALUE (values));
13717 : :
13718 : 12 : if (known_decl && new_decl)
13719 : : {
13720 : 9 : inform (DECL_SOURCE_LOCATION (new_decl),
13721 : : "enumerator %qD does not match ...", new_decl);
13722 : 9 : inform (DECL_SOURCE_LOCATION (known_decl),
13723 : : "... this enumerator %qD", known_decl);
13724 : : }
13725 : 3 : else if (known_decl || new_decl)
13726 : : {
13727 : 3 : tree extra = known_decl ? known_decl : new_decl;
13728 : 3 : inform (DECL_SOURCE_LOCATION (extra),
13729 : : "additional enumerators beginning with %qD", extra);
13730 : : }
13731 : : else
13732 : 0 : inform (DECL_SOURCE_LOCATION (maybe_dup),
13733 : : "enumeration range differs");
13734 : :
13735 : : /* Mark it bad. */
13736 : 12 : unmatched_duplicate (maybe_template);
13737 : 12 : }
13738 : : }
13739 : :
13740 : : return true;
13741 : : }
13742 : :
13743 : : /* Write out the body of DECL. See above circularity note. */
13744 : :
13745 : : void
13746 : 618359 : trees_out::write_definition (tree decl, bool refs_tu_local)
13747 : : {
13748 : 618359 : auto ovr = make_temp_override (writing_local_entities,
13749 : 618359 : writing_local_entities || refs_tu_local);
13750 : :
13751 : 618359 : if (streaming_p ())
13752 : : {
13753 : 309101 : assert_definition (decl);
13754 : 309101 : dump ()
13755 : 641 : && dump ("Writing definition %C:%N", TREE_CODE (decl), decl);
13756 : : }
13757 : : else
13758 : 309258 : dump (dumper::DEPEND)
13759 : 96 : && dump ("Depending definition %C:%N", TREE_CODE (decl), decl);
13760 : :
13761 : 952883 : again:
13762 : 952883 : switch (TREE_CODE (decl))
13763 : : {
13764 : 0 : default:
13765 : 0 : gcc_unreachable ();
13766 : :
13767 : 334524 : case TEMPLATE_DECL:
13768 : 334524 : decl = DECL_TEMPLATE_RESULT (decl);
13769 : 334524 : goto again;
13770 : :
13771 : 361509 : case FUNCTION_DECL:
13772 : 361509 : write_function_def (decl);
13773 : 361509 : break;
13774 : :
13775 : 149176 : case TYPE_DECL:
13776 : 149176 : {
13777 : 149176 : tree type = TREE_TYPE (decl);
13778 : 149176 : gcc_assert (TYPE_MAIN_VARIANT (type) == type
13779 : : && TYPE_NAME (type) == decl);
13780 : 149176 : if (TREE_CODE (type) == ENUMERAL_TYPE)
13781 : 7368 : write_enum_def (decl);
13782 : : else
13783 : 141808 : write_class_def (decl);
13784 : : }
13785 : : break;
13786 : :
13787 : 107674 : case VAR_DECL:
13788 : 107674 : case CONCEPT_DECL:
13789 : 107674 : write_var_def (decl);
13790 : 107674 : break;
13791 : : }
13792 : 618359 : }
13793 : :
13794 : : /* Mark a declaration for by-value walking. If DO_DEFN is true, mark
13795 : : its body too. */
13796 : :
13797 : : void
13798 : 2584802 : trees_out::mark_declaration (tree decl, bool do_defn)
13799 : : {
13800 : 2584802 : mark_by_value (decl);
13801 : :
13802 : 2584802 : if (TREE_CODE (decl) == TEMPLATE_DECL)
13803 : 833856 : decl = DECL_TEMPLATE_RESULT (decl);
13804 : :
13805 : 2584802 : if (!do_defn)
13806 : : return;
13807 : :
13808 : 618387 : switch (TREE_CODE (decl))
13809 : : {
13810 : 0 : default:
13811 : 0 : gcc_unreachable ();
13812 : :
13813 : : case FUNCTION_DECL:
13814 : : mark_function_def (decl);
13815 : : break;
13816 : :
13817 : 149204 : case TYPE_DECL:
13818 : 149204 : {
13819 : 149204 : tree type = TREE_TYPE (decl);
13820 : 149204 : gcc_assert (TYPE_MAIN_VARIANT (type) == type
13821 : : && TYPE_NAME (type) == decl);
13822 : 149204 : if (TREE_CODE (type) == ENUMERAL_TYPE)
13823 : 7368 : mark_enum_def (decl);
13824 : : else
13825 : 141836 : mark_class_def (decl);
13826 : : }
13827 : : break;
13828 : :
13829 : : case VAR_DECL:
13830 : : case CONCEPT_DECL:
13831 : : mark_var_def (decl);
13832 : : break;
13833 : : }
13834 : : }
13835 : :
13836 : : /* Read in the body of DECL. See above circularity note. */
13837 : :
13838 : : bool
13839 : 242313 : trees_in::read_definition (tree decl)
13840 : : {
13841 : 243412 : dump () && dump ("Reading definition %C %N", TREE_CODE (decl), decl);
13842 : :
13843 : : tree maybe_template = decl;
13844 : :
13845 : 242313 : again:
13846 : 384007 : switch (TREE_CODE (decl))
13847 : : {
13848 : : default:
13849 : : break;
13850 : :
13851 : 141694 : case TEMPLATE_DECL:
13852 : 141694 : decl = DECL_TEMPLATE_RESULT (decl);
13853 : 141694 : goto again;
13854 : :
13855 : 154728 : case FUNCTION_DECL:
13856 : 154728 : return read_function_def (decl, maybe_template);
13857 : :
13858 : 54643 : case TYPE_DECL:
13859 : 54643 : {
13860 : 54643 : tree type = TREE_TYPE (decl);
13861 : 54643 : gcc_assert (TYPE_MAIN_VARIANT (type) == type
13862 : : && TYPE_NAME (type) == decl);
13863 : 54643 : if (TREE_CODE (type) == ENUMERAL_TYPE)
13864 : 2413 : return read_enum_def (decl, maybe_template);
13865 : : else
13866 : 52230 : return read_class_def (decl, maybe_template);
13867 : : }
13868 : 32942 : break;
13869 : :
13870 : 32942 : case VAR_DECL:
13871 : 32942 : case CONCEPT_DECL:
13872 : 32942 : return read_var_def (decl, maybe_template);
13873 : : }
13874 : :
13875 : : return false;
13876 : : }
13877 : :
13878 : : /* Lookup an maybe insert a slot for depset for KEY. */
13879 : :
13880 : : depset **
13881 : 11882042 : depset::hash::entity_slot (tree entity, bool insert)
13882 : : {
13883 : 11882042 : traits::compare_type key (entity, NULL);
13884 : 17994165 : depset **slot = find_slot_with_hash (key, traits::hash (key),
13885 : : insert ? INSERT : NO_INSERT);
13886 : :
13887 : 11882042 : return slot;
13888 : : }
13889 : :
13890 : : depset **
13891 : 153145 : depset::hash::binding_slot (tree ctx, tree name, bool insert)
13892 : : {
13893 : 153145 : traits::compare_type key (ctx, name);
13894 : 187612 : depset **slot = find_slot_with_hash (key, traits::hash (key),
13895 : : insert ? INSERT : NO_INSERT);
13896 : :
13897 : 153145 : return slot;
13898 : : }
13899 : :
13900 : : depset *
13901 : 5823659 : depset::hash::find_dependency (tree decl)
13902 : : {
13903 : 5823659 : depset **slot = entity_slot (decl, false);
13904 : :
13905 : 5823659 : return slot ? *slot : NULL;
13906 : : }
13907 : :
13908 : : depset *
13909 : 34467 : depset::hash::find_binding (tree ctx, tree name)
13910 : : {
13911 : 34467 : depset **slot = binding_slot (ctx, name, false);
13912 : :
13913 : 34467 : return slot ? *slot : NULL;
13914 : : }
13915 : :
13916 : : /* Returns true if DECL is a TU-local entity, as defined by [basic.link].
13917 : : If EXPLAIN is true, emit an informative note about why DECL is TU-local. */
13918 : :
13919 : : bool
13920 : 1105298 : depset::hash::is_tu_local_entity (tree decl, bool explain/*=false*/)
13921 : : {
13922 : 1105298 : gcc_checking_assert (DECL_P (decl));
13923 : 1105298 : location_t loc = DECL_SOURCE_LOCATION (decl);
13924 : 1105298 : tree type = TREE_TYPE (decl);
13925 : :
13926 : : /* Only types, functions, variables, and template (specialisations)
13927 : : can be TU-local. */
13928 : 1105298 : if (TREE_CODE (decl) != TYPE_DECL
13929 : 1105298 : && TREE_CODE (decl) != FUNCTION_DECL
13930 : 415853 : && TREE_CODE (decl) != VAR_DECL
13931 : 393195 : && TREE_CODE (decl) != TEMPLATE_DECL)
13932 : : return false;
13933 : :
13934 : : /* An explicit type alias is not an entity; we don't want to stream
13935 : : such aliases if they refer to TU-local entities, so propagate this
13936 : : from the original type. The built-in declarations of 'int' and such
13937 : : are never TU-local. */
13938 : 1102578 : if (TREE_CODE (decl) == TYPE_DECL
13939 : 590625 : && !DECL_SELF_REFERENCE_P (decl)
13940 : 1667217 : && !DECL_IMPLICIT_TYPEDEF_P (decl))
13941 : : {
13942 : 322404 : tree orig = DECL_ORIGINAL_TYPE (decl);
13943 : 322404 : if (orig && TYPE_NAME (orig))
13944 : : {
13945 : 66818 : if (explain)
13946 : 11 : inform (loc, "%qD is an alias of TU-local type %qT", decl, orig);
13947 : 66818 : return is_tu_local_entity (TYPE_NAME (orig), explain);
13948 : : }
13949 : : else
13950 : : return false;
13951 : : }
13952 : :
13953 : : /* Check specializations first for slightly better explanations. */
13954 : 780174 : int use_tpl = -1;
13955 : 780174 : tree ti = node_template_info (decl, use_tpl);
13956 : 1099547 : if (use_tpl > 0 && TREE_CODE (TI_TEMPLATE (ti)) == TEMPLATE_DECL)
13957 : : {
13958 : : /* A specialization of a TU-local template. */
13959 : 319357 : tree tmpl = TI_TEMPLATE (ti);
13960 : 319357 : if (is_tu_local_entity (tmpl))
13961 : : {
13962 : 42 : if (explain)
13963 : : {
13964 : 9 : inform (loc, "%qD is a specialization of TU-local template %qD",
13965 : : decl, tmpl);
13966 : 9 : is_tu_local_entity (tmpl, /*explain=*/true);
13967 : : }
13968 : 42 : return true;
13969 : : }
13970 : :
13971 : : /* A specialization of a template with any TU-local template argument. */
13972 : 319315 : if (has_tu_local_tmpl_arg (decl, TI_ARGS (ti), explain))
13973 : : return true;
13974 : :
13975 : : /* FIXME A specialization of a template whose (possibly instantiated)
13976 : : declaration is an exposure. This should always be covered by the
13977 : : above cases?? */
13978 : : }
13979 : :
13980 : : /* A type, function, variable, or template with internal linkage. */
13981 : 780103 : linkage_kind kind = decl_linkage (decl);
13982 : 780103 : if (kind == lk_internal
13983 : : /* But although weakrefs are marked static, don't consider them
13984 : : to be TU-local. */
13985 : 780103 : && !lookup_attribute ("weakref", DECL_ATTRIBUTES (decl)))
13986 : : {
13987 : 644 : if (explain)
13988 : 132 : inform (loc, "%qD declared with internal linkage", decl);
13989 : 644 : return true;
13990 : : }
13991 : :
13992 : : /* Does not have a name with linkage and is declared, or introduced by a
13993 : : lambda-expression, within the definition of a TU-local entity. */
13994 : 779459 : if (kind == lk_none)
13995 : : {
13996 : 16659 : tree ctx = CP_DECL_CONTEXT (decl);
13997 : 28795 : if (LAMBDA_TYPE_P (type))
13998 : 7654 : if (tree extra = LAMBDA_TYPE_EXTRA_SCOPE (type))
13999 : 16659 : ctx = extra;
14000 : :
14001 : 16659 : if (TREE_CODE (ctx) == NAMESPACE_DECL)
14002 : : {
14003 : 31 : if (!TREE_PUBLIC (ctx))
14004 : : {
14005 : 0 : if (explain)
14006 : 0 : inform (loc, "%qD has no linkage and is declared in an "
14007 : : "anonymous namespace", decl);
14008 : 0 : return true;
14009 : : }
14010 : : }
14011 : 16628 : else if (TYPE_P (ctx))
14012 : : {
14013 : 5753 : tree ctx_decl = TYPE_MAIN_DECL (ctx);
14014 : 5753 : if (is_tu_local_entity (ctx_decl))
14015 : : {
14016 : 0 : if (explain)
14017 : : {
14018 : 0 : inform (loc, "%qD has no linkage and is declared within "
14019 : : "TU-local entity %qT", decl, ctx);
14020 : 0 : is_tu_local_entity (ctx_decl, /*explain=*/true);
14021 : : }
14022 : 0 : return true;
14023 : : }
14024 : : }
14025 : 10875 : else if (is_tu_local_entity (ctx))
14026 : : {
14027 : 21 : if (explain)
14028 : : {
14029 : 6 : inform (loc, "%qD has no linkage and is declared within "
14030 : : "TU-local entity %qD", decl, ctx);
14031 : 6 : is_tu_local_entity (ctx, /*explain=*/true);
14032 : : }
14033 : 21 : return true;
14034 : : }
14035 : : }
14036 : :
14037 : : /* A type with no name that is defined outside a class-specifier, function
14038 : : body, or initializer; or is introduced by a defining-type-specifier that
14039 : : is used to declare only TU-local entities.
14040 : :
14041 : : We consider types with names for linkage purposes as having names, since
14042 : : these aren't really TU-local. */
14043 : 779438 : tree inner = STRIP_TEMPLATE (decl);
14044 : 390370 : if (inner
14045 : 779438 : && TREE_CODE (inner) == TYPE_DECL
14046 : 1058514 : && TYPE_ANON_P (type)
14047 : 5674 : && !DECL_SELF_REFERENCE_P (inner)
14048 : : /* An enum with an enumerator name for linkage. */
14049 : 395225 : && !(UNSCOPED_ENUM_P (type) && TYPE_VALUES (type)))
14050 : : {
14051 : 3531 : tree main_decl = TYPE_MAIN_DECL (type);
14052 : 6845 : if (LAMBDA_TYPE_P (type))
14053 : : {
14054 : : /* A lambda expression is, in practice, TU-local iff it has no
14055 : : mangling scope. This currently doesn't line up exactly with
14056 : : the standard's definition due to some ABI issues, but it's
14057 : : pretty close, and avoids other issues down the line. */
14058 : 6566 : if (!LAMBDA_TYPE_EXTRA_SCOPE (type))
14059 : : {
14060 : 4 : if (explain)
14061 : 2 : inform (loc, "%qT has no name and cannot be differentiated "
14062 : : "from similar lambdas in other TUs", type);
14063 : 4 : return true;
14064 : : }
14065 : : }
14066 : 496 : else if (!DECL_CLASS_SCOPE_P (main_decl)
14067 : 278 : && !decl_function_context (main_decl))
14068 : : {
14069 : 30 : if (explain)
14070 : 12 : inform (loc, "%qT has no name and is not defined within a class, "
14071 : : "function, or initializer", type);
14072 : 30 : return true;
14073 : : }
14074 : :
14075 : : // FIXME introduced by a defining-type-specifier only declaring TU-local
14076 : : // entities; does this refer to e.g. 'static struct {} a;"? I can't
14077 : : // think of any cases where this isn't covered by earlier cases. */
14078 : : }
14079 : :
14080 : : return false;
14081 : : }
14082 : :
14083 : : /* Helper for is_tu_local_entity. Returns true if one of the ARGS of
14084 : : DECL is TU-local. Emits an explanation if EXPLAIN is true. */
14085 : :
14086 : : bool
14087 : 357191 : depset::hash::has_tu_local_tmpl_arg (tree decl, tree args, bool explain)
14088 : : {
14089 : 357191 : if (!args || TREE_CODE (args) != TREE_VEC)
14090 : : return false;
14091 : :
14092 : 954641 : for (tree a : tree_vec_range (args))
14093 : : {
14094 : 597479 : if (TREE_CODE (a) == TREE_VEC)
14095 : : {
14096 : 37876 : if (has_tu_local_tmpl_arg (decl, a, explain))
14097 : 29 : return true;
14098 : : }
14099 : 559603 : else if (!WILDCARD_TYPE_P (a))
14100 : : {
14101 : 505061 : if (DECL_P (a) && is_tu_local_entity (a))
14102 : : {
14103 : 0 : if (explain)
14104 : : {
14105 : 0 : inform (DECL_SOURCE_LOCATION (decl),
14106 : : "%qD has TU-local template argument %qD",
14107 : : decl, a);
14108 : 0 : is_tu_local_entity (a, /*explain=*/true);
14109 : : }
14110 : 0 : return true;
14111 : : }
14112 : :
14113 : 505061 : if (TYPE_P (a) && TYPE_NAME (a) && is_tu_local_entity (TYPE_NAME (a)))
14114 : : {
14115 : 14 : if (explain)
14116 : : {
14117 : 1 : inform (DECL_SOURCE_LOCATION (decl),
14118 : : "%qD has TU-local template argument %qT",
14119 : : decl, a);
14120 : 1 : is_tu_local_entity (TYPE_NAME (a), /*explain=*/true);
14121 : : }
14122 : 14 : return true;
14123 : : }
14124 : :
14125 : 505047 : if (EXPR_P (a) && is_tu_local_value (decl, a, explain))
14126 : : return true;
14127 : : }
14128 : : }
14129 : :
14130 : 357162 : return false;
14131 : : }
14132 : :
14133 : : /* Returns true if EXPR (part of the initializer for DECL) is a TU-local value
14134 : : or object. Emits an explanation if EXPLAIN is true. */
14135 : :
14136 : : bool
14137 : 43324 : depset::hash::is_tu_local_value (tree decl, tree expr, bool explain)
14138 : : {
14139 : 43324 : if (!expr)
14140 : : return false;
14141 : :
14142 : 42301 : tree e = expr;
14143 : 42301 : STRIP_ANY_LOCATION_WRAPPER (e);
14144 : 42301 : STRIP_NOPS (e);
14145 : 42301 : if (TREE_CODE (e) == TARGET_EXPR)
14146 : 0 : e = TARGET_EXPR_INITIAL (e);
14147 : 0 : if (!e)
14148 : : return false;
14149 : :
14150 : : /* It is, or is a pointer to, a TU-local function or the object associated
14151 : : with a TU-local variable. */
14152 : 42301 : tree object = NULL_TREE;
14153 : 42301 : if (TREE_CODE (e) == ADDR_EXPR)
14154 : 1886 : object = TREE_OPERAND (e, 0);
14155 : 40415 : else if (TREE_CODE (e) == PTRMEM_CST)
14156 : 0 : object = PTRMEM_CST_MEMBER (e);
14157 : 40415 : else if (VAR_OR_FUNCTION_DECL_P (e))
14158 : : object = e;
14159 : :
14160 : 1886 : if (object
14161 : 2162 : && VAR_OR_FUNCTION_DECL_P (object)
14162 : 2246 : && is_tu_local_entity (object))
14163 : : {
14164 : 54 : if (explain)
14165 : : {
14166 : : /* We've lost a lot of location information by the time we get here,
14167 : : so let's just do our best effort. */
14168 : 18 : auto loc = cp_expr_loc_or_loc (expr, DECL_SOURCE_LOCATION (decl));
14169 : 18 : if (VAR_P (object))
14170 : 9 : inform (loc, "%qD refers to TU-local object %qD", decl, object);
14171 : : else
14172 : 9 : inform (loc, "%qD refers to TU-local function %qD", decl, object);
14173 : 18 : is_tu_local_entity (object, true);
14174 : : }
14175 : 54 : return true;
14176 : : }
14177 : :
14178 : : /* It is an object of class or array type and any of its subobjects or
14179 : : any of the objects or functions to which its non-static data members
14180 : : of reference type refer is TU-local and is usable in constant
14181 : : expressions. */
14182 : 42247 : if (TREE_CODE (e) == CONSTRUCTOR && AGGREGATE_TYPE_P (TREE_TYPE (e)))
14183 : 20789 : for (auto &f : CONSTRUCTOR_ELTS (e))
14184 : 14434 : if (is_tu_local_value (decl, f.value, explain))
14185 : : return true;
14186 : :
14187 : : return false;
14188 : : }
14189 : :
14190 : : /* DECL is a newly discovered dependency. Create the depset, if it
14191 : : doesn't already exist. Add it to the worklist if so.
14192 : :
14193 : : DECL will be an OVL_USING_P OVERLOAD, if it's from a binding that's
14194 : : a using decl.
14195 : :
14196 : : We do not have to worry about adding the same dependency more than
14197 : : once. First it's harmless, but secondly the TREE_VISITED marking
14198 : : prevents us wanting to do it anyway. */
14199 : :
14200 : : depset *
14201 : 4979402 : depset::hash::make_dependency (tree decl, entity_kind ek)
14202 : : {
14203 : : /* Make sure we're being told consistent information. */
14204 : 9241405 : gcc_checking_assert ((ek == EK_NAMESPACE)
14205 : : == (TREE_CODE (decl) == NAMESPACE_DECL
14206 : : && !DECL_NAMESPACE_ALIAS (decl)));
14207 : 4979402 : gcc_checking_assert (ek != EK_BINDING && ek != EK_REDIRECT);
14208 : 4979402 : gcc_checking_assert (TREE_CODE (decl) != FIELD_DECL
14209 : : && (TREE_CODE (decl) != USING_DECL
14210 : : || TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL));
14211 : 4979402 : gcc_checking_assert (!is_key_order ());
14212 : 4979402 : if (ek == EK_USING)
14213 : 13560 : gcc_checking_assert (TREE_CODE (decl) == OVERLOAD);
14214 : 4979402 : if (ek == EK_TU_LOCAL)
14215 : 87 : gcc_checking_assert (DECL_DECLARES_FUNCTION_P (decl));
14216 : :
14217 : 4979402 : if (TREE_CODE (decl) == TEMPLATE_DECL)
14218 : : /* The template should have copied these from its result decl. */
14219 : 1768870 : gcc_checking_assert (DECL_MODULE_EXPORT_P (decl)
14220 : : == DECL_MODULE_EXPORT_P (DECL_TEMPLATE_RESULT (decl)));
14221 : :
14222 : 4979402 : depset **slot = entity_slot (decl, true);
14223 : 4979402 : depset *dep = *slot;
14224 : 4979402 : bool for_binding = ek == EK_FOR_BINDING;
14225 : :
14226 : 4979402 : if (!dep)
14227 : : {
14228 : 448711 : if ((DECL_IMPLICIT_TYPEDEF_P (decl)
14229 : : /* ... not an enum, for instance. */
14230 : 227001 : && RECORD_OR_UNION_TYPE_P (TREE_TYPE (decl))
14231 : 223171 : && TYPE_LANG_SPECIFIC (TREE_TYPE (decl))
14232 : 201656 : && CLASSTYPE_USE_TEMPLATE (TREE_TYPE (decl)) == 2)
14233 : 1525615 : || (VAR_P (decl)
14234 : 65191 : && DECL_LANG_SPECIFIC (decl)
14235 : 65110 : && DECL_USE_TEMPLATE (decl) == 2))
14236 : : {
14237 : : /* A partial or explicit specialization. Partial
14238 : : specializations might not be in the hash table, because
14239 : : there can be multiple differently-constrained variants.
14240 : :
14241 : : template<typename T> class silly;
14242 : : template<typename T> requires true class silly {};
14243 : :
14244 : : We need to find them, insert their TEMPLATE_DECL in the
14245 : : dep_hash, and then convert the dep we just found into a
14246 : : redirect. */
14247 : :
14248 : 34468 : tree ti = get_template_info (decl);
14249 : 34468 : tree tmpl = TI_TEMPLATE (ti);
14250 : 34468 : tree partial = NULL_TREE;
14251 : 34468 : for (tree spec = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
14252 : 106023 : spec; spec = TREE_CHAIN (spec))
14253 : 89714 : if (DECL_TEMPLATE_RESULT (TREE_VALUE (spec)) == decl)
14254 : : {
14255 : : partial = TREE_VALUE (spec);
14256 : : break;
14257 : : }
14258 : :
14259 : 34468 : if (partial)
14260 : : {
14261 : : /* Eagerly create an empty redirect. The following
14262 : : make_dependency call could cause hash reallocation,
14263 : : and invalidate slot's value. */
14264 : 18159 : depset *redirect = make_entity (decl, EK_REDIRECT);
14265 : :
14266 : : /* Redirects are never reached -- always snap to their target. */
14267 : 18159 : redirect->set_flag_bit<DB_UNREACHED_BIT> ();
14268 : :
14269 : 18159 : *slot = redirect;
14270 : :
14271 : 18159 : depset *tmpl_dep = make_dependency (partial, EK_PARTIAL);
14272 : 18159 : gcc_checking_assert (tmpl_dep->get_entity_kind () == EK_PARTIAL);
14273 : :
14274 : 18159 : redirect->deps.safe_push (tmpl_dep);
14275 : :
14276 : 18159 : return redirect;
14277 : : }
14278 : : }
14279 : :
14280 : 1088657 : bool has_def = ek != EK_USING && has_definition (decl);
14281 : 1075097 : if (ek > EK_BINDING)
14282 : 133296 : ek = EK_DECL;
14283 : :
14284 : : /* The only OVERLOADS we should see are USING decls from
14285 : : bindings. */
14286 : 1088657 : *slot = dep = make_entity (decl, ek, has_def);
14287 : :
14288 : 1088657 : if (CHECKING_P && TREE_CODE (decl) == TEMPLATE_DECL)
14289 : : /* The template_result should otherwise not be in the
14290 : : table, or be an empty redirect (created above). */
14291 : 288464 : if (auto *eslot = entity_slot (DECL_TEMPLATE_RESULT (decl), false))
14292 : 18159 : gcc_checking_assert ((*eslot)->get_entity_kind () == EK_REDIRECT
14293 : : && !(*eslot)->deps.length ());
14294 : :
14295 : 1088657 : if (ek != EK_USING)
14296 : : {
14297 : 1075097 : tree not_tmpl = STRIP_TEMPLATE (decl);
14298 : 1075097 : bool imported_from_module_p = false;
14299 : :
14300 : 1075097 : if (DECL_LANG_SPECIFIC (not_tmpl)
14301 : 2085709 : && DECL_MODULE_IMPORT_P (not_tmpl))
14302 : : {
14303 : : /* Store the module number and index in cluster/section,
14304 : : so we don't have to look them up again. */
14305 : 2261 : unsigned index = import_entity_index (decl);
14306 : 2261 : module_state *from = import_entity_module (index);
14307 : : /* Remap will be zero for imports from partitions, which
14308 : : we want to treat as-if declared in this TU. */
14309 : 2261 : if (from->remap)
14310 : : {
14311 : 1810 : dep->cluster = index - from->entity_lwm;
14312 : 1810 : dep->section = from->remap;
14313 : 1810 : dep->set_flag_bit<DB_IMPORTED_BIT> ();
14314 : :
14315 : 1810 : if (!from->is_header ())
14316 : 1075097 : imported_from_module_p = true;
14317 : : }
14318 : : }
14319 : :
14320 : : /* Check for TU-local entities. This is unnecessary in header
14321 : : units because we can export internal-linkage decls, and
14322 : : no declarations are exposures. Similarly, if the decl was
14323 : : imported from a non-header module we know it cannot have
14324 : : been TU-local. */
14325 : 1075097 : if (!header_module_p () && !imported_from_module_p)
14326 : : {
14327 : 314348 : if (is_tu_local_entity (decl))
14328 : 258 : dep->set_flag_bit<DB_TU_LOCAL_BIT> ();
14329 : :
14330 : 314348 : if (VAR_P (decl)
14331 : 21485 : && decl_maybe_constant_var_p (decl)
14332 : 335175 : && is_tu_local_value (decl, DECL_INITIAL (decl)))
14333 : : {
14334 : : /* A potentially-constant variable initialized to a TU-local
14335 : : value is not usable in constant expressions within other
14336 : : translation units. We can achieve this by simply not
14337 : : streaming the definition in such cases. */
14338 : 24 : dep->clear_flag_bit<DB_DEFN_BIT> ();
14339 : :
14340 : 24 : if (DECL_DECLARED_CONSTEXPR_P (decl)
14341 : 39 : || DECL_INLINE_VAR_P (decl))
14342 : : /* A constexpr variable initialized to a TU-local value,
14343 : : or an inline value (PR c++/119996), is an exposure. */
14344 : 15 : dep->set_flag_bit<DB_EXPOSURE_BIT> ();
14345 : : }
14346 : : }
14347 : :
14348 : : /* A namespace-scope type may be declared in one module unit
14349 : : and defined in another; make sure that we're found when
14350 : : completing the class. */
14351 : 1075097 : if (ek == EK_DECL
14352 : 463160 : && !dep->is_import ()
14353 : 462654 : && dep->has_defn ()
14354 : 238200 : && DECL_NAMESPACE_SCOPE_P (not_tmpl)
14355 : 87496 : && DECL_IMPLICIT_TYPEDEF_P (not_tmpl)
14356 : : /* Anonymous types can't be forward-declared. */
14357 : 1100219 : && !IDENTIFIER_ANON_P (DECL_NAME (not_tmpl)))
14358 : 24713 : dep->set_flag_bit<DB_IS_PENDING_BIT> ();
14359 : :
14360 : : /* Namespace-scope functions can be found by ADL by template
14361 : : instantiations in this module. We need to create bindings
14362 : : for them so that name lookup recognises they exist, if they
14363 : : won't be discarded. add_binding_entity is too early to do
14364 : : this for GM functions, because if nobody ends up using them
14365 : : we'll have leftover bindings laying around, and it's tricky
14366 : : to delete them and any namespaces they've implicitly created
14367 : : deps on. The downside is this means we don't pick up on
14368 : : using-decls, but by [module.global.frag] p3.6 we don't have
14369 : : to. */
14370 : 1075097 : if (ek == EK_DECL
14371 : 1075097 : && !for_binding
14372 : 329876 : && !dep->is_import ()
14373 : 329376 : && !dep->is_tu_local ()
14374 : 329269 : && DECL_NAMESPACE_SCOPE_P (decl)
14375 : 20178 : && DECL_DECLARES_FUNCTION_P (decl)
14376 : : /* Compiler-generated functions won't participate in ADL. */
14377 : 12824 : && !DECL_ARTIFICIAL (decl)
14378 : : /* A hidden friend doesn't need a binding. */
14379 : 1083001 : && !(DECL_LANG_SPECIFIC (not_tmpl)
14380 : 7904 : && DECL_UNIQUE_FRIEND_P (not_tmpl)))
14381 : : {
14382 : : /* This will only affect GM functions. */
14383 : 5136 : gcc_checking_assert (!DECL_LANG_SPECIFIC (not_tmpl)
14384 : : || !DECL_MODULE_PURVIEW_P (not_tmpl));
14385 : : /* We shouldn't see any instantiations or specialisations. */
14386 : 2568 : gcc_checking_assert (!DECL_LANG_SPECIFIC (decl)
14387 : : || !DECL_USE_TEMPLATE (decl));
14388 : :
14389 : 2568 : tree ns = CP_DECL_CONTEXT (decl);
14390 : 2568 : tree name = DECL_NAME (decl);
14391 : 2568 : depset *binding = find_binding (ns, name);
14392 : 2568 : if (!binding)
14393 : : {
14394 : 1225 : binding = make_binding (ns, name);
14395 : 1225 : add_namespace_context (binding, ns);
14396 : :
14397 : 1225 : depset **slot = binding_slot (ns, name, /*insert=*/true);
14398 : 1225 : *slot = binding;
14399 : : }
14400 : :
14401 : 2568 : binding->deps.safe_push (dep);
14402 : 2568 : dep->deps.safe_push (binding);
14403 : 2577 : dump (dumper::DEPEND)
14404 : 9 : && dump ("Built ADL binding for %C:%N",
14405 : 9 : TREE_CODE (decl), decl);
14406 : : }
14407 : : }
14408 : :
14409 : 1088657 : if (!dep->is_import ())
14410 : 1086847 : worklist.safe_push (dep);
14411 : : }
14412 : :
14413 : 4961243 : dump (dumper::DEPEND)
14414 : 35976 : && dump ("%s on %s %C:%N found",
14415 : : ek == EK_REDIRECT ? "Redirect"
14416 : 35976 : : (for_binding || ek == EK_TU_LOCAL) ? "Binding"
14417 : : : "Dependency",
14418 : 35976 : dep->entity_kind_name (), TREE_CODE (decl), decl);
14419 : :
14420 : 4961243 : return dep;
14421 : : }
14422 : :
14423 : : /* Whether REF is an exposure of a member type of SOURCE.
14424 : :
14425 : : This comes up with exposures of class-scope lambdas, that we currently
14426 : : treat as TU-local due to ABI reasons. In such a case the type of the
14427 : : lambda will be exposed in two places, first by the class type it is in
14428 : : the TYPE_FIELDS list of, and second by the actual member declaring that
14429 : : lambda. We only want the second case to warn. */
14430 : :
14431 : : static bool
14432 : 169 : is_exposure_of_member_type (depset *source, depset *ref)
14433 : : {
14434 : 338 : gcc_checking_assert (source->refs_tu_local () && ref->is_tu_local ());
14435 : 169 : tree source_entity = STRIP_TEMPLATE (source->get_entity ());
14436 : 169 : tree ref_entity = STRIP_TEMPLATE (ref->get_entity ());
14437 : :
14438 : 169 : if (source_entity
14439 : 169 : && ref_entity
14440 : 169 : && DECL_IMPLICIT_TYPEDEF_P (source_entity)
14441 : 2 : && DECL_IMPLICIT_TYPEDEF_P (ref_entity)
14442 : 2 : && DECL_CLASS_SCOPE_P (ref_entity)
14443 : 171 : && DECL_CONTEXT (ref_entity) == TREE_TYPE (source_entity))
14444 : : {
14445 : 4 : gcc_checking_assert (LAMBDA_TYPE_P (TREE_TYPE (ref_entity)));
14446 : : return true;
14447 : : }
14448 : : else
14449 : : return false;
14450 : : }
14451 : :
14452 : : /* DEP is a newly discovered dependency. Append it to current's
14453 : : depset. */
14454 : :
14455 : : void
14456 : 3673904 : depset::hash::add_dependency (depset *dep)
14457 : : {
14458 : 3673904 : gcc_checking_assert (current && !is_key_order ());
14459 : 3673904 : current->deps.safe_push (dep);
14460 : :
14461 : 3673904 : if (dep->is_tu_local ())
14462 : : {
14463 : 260 : current->set_flag_bit<DB_REFS_TU_LOCAL_BIT> ();
14464 : 260 : if (!ignore_tu_local && !is_exposure_of_member_type (current, dep))
14465 : 85 : current->set_flag_bit<DB_EXPOSURE_BIT> ();
14466 : : }
14467 : :
14468 : 3673904 : if (current->get_entity_kind () == EK_USING
14469 : 13560 : && DECL_IMPLICIT_TYPEDEF_P (dep->get_entity ())
14470 : 3677796 : && TREE_CODE (TREE_TYPE (dep->get_entity ())) == ENUMERAL_TYPE)
14471 : : {
14472 : : /* CURRENT is an unwrapped using-decl and DECL is an enum's
14473 : : implicit typedef. Is CURRENT a member of the enum? */
14474 : 3766 : tree c_decl = OVL_FUNCTION (current->get_entity ());
14475 : :
14476 : 3766 : if (TREE_CODE (c_decl) == CONST_DECL
14477 : 7521 : && (current->deps[0]->get_entity ()
14478 : 3755 : == CP_DECL_CONTEXT (dep->get_entity ())))
14479 : : /* Make DECL depend on CURRENT. */
14480 : 3704 : dep->deps.safe_push (current);
14481 : : }
14482 : :
14483 : : /* If two dependencies recursively depend on each other existing within
14484 : : their own merge keys, we must ensure that the first dep we saw while
14485 : : walking is written first in this cluster. See sort_cluster for more
14486 : : details. */
14487 : 3673904 : if (writing_merge_key)
14488 : : {
14489 : 646748 : dep->set_flag_bit<DB_MAYBE_RECURSIVE_BIT> ();
14490 : 646748 : if (!current->is_maybe_recursive ())
14491 : 610412 : current->set_flag_bit<DB_ENTRY_BIT> ();
14492 : : }
14493 : :
14494 : 3673904 : if (dep->is_unreached ())
14495 : : {
14496 : : /* The dependency is reachable now. */
14497 : 282069 : reached_unreached = true;
14498 : 282069 : dep->clear_flag_bit<DB_UNREACHED_BIT> ();
14499 : 282069 : dump (dumper::DEPEND)
14500 : 30 : && dump ("Reaching unreached %s %C:%N", dep->entity_kind_name (),
14501 : 30 : TREE_CODE (dep->get_entity ()), dep->get_entity ());
14502 : : }
14503 : 3673904 : }
14504 : :
14505 : : depset *
14506 : 5519234 : depset::hash::add_dependency (tree decl, entity_kind ek)
14507 : : {
14508 : 5519234 : depset *dep;
14509 : :
14510 : 5519234 : if (is_key_order ())
14511 : : {
14512 : 1804249 : dep = find_dependency (decl);
14513 : 1804249 : if (dep)
14514 : : {
14515 : 891505 : current->deps.safe_push (dep);
14516 : 891505 : dump (dumper::MERGE)
14517 : 471 : && dump ("Key dependency on %s %C:%N found",
14518 : 471 : dep->entity_kind_name (), TREE_CODE (decl), decl);
14519 : : }
14520 : : else
14521 : : {
14522 : : /* It's not a mergeable decl, look for it in the original
14523 : : table. */
14524 : 912744 : dep = chain->find_dependency (decl);
14525 : 912744 : gcc_checking_assert (dep);
14526 : : }
14527 : : }
14528 : : else
14529 : : {
14530 : 3714985 : dep = make_dependency (decl, ek);
14531 : 3714985 : if (dep->get_entity_kind () != EK_REDIRECT)
14532 : 3673904 : add_dependency (dep);
14533 : : }
14534 : :
14535 : 5519234 : return dep;
14536 : : }
14537 : :
14538 : : void
14539 : 473848 : depset::hash::add_namespace_context (depset *dep, tree ns)
14540 : : {
14541 : 473848 : depset *ns_dep = make_dependency (ns, depset::EK_NAMESPACE);
14542 : 473848 : dep->deps.safe_push (ns_dep);
14543 : :
14544 : : /* Mark it as special if imported so we don't walk connect when
14545 : : SCCing. */
14546 : 473848 : if (!dep->is_binding () && ns_dep->is_import ())
14547 : 0 : dep->set_special ();
14548 : 473848 : }
14549 : :
14550 : : struct add_binding_data
14551 : : {
14552 : : tree ns;
14553 : : bitmap partitions;
14554 : : depset *binding;
14555 : : depset::hash *hash;
14556 : : bool met_namespace;
14557 : : };
14558 : :
14559 : : /* Return true if we are, or contain something that is exported. */
14560 : :
14561 : : bool
14562 : 5172490 : depset::hash::add_binding_entity (tree decl, WMB_Flags flags, void *data_)
14563 : : {
14564 : 5172490 : auto data = static_cast <add_binding_data *> (data_);
14565 : 5172490 : decl = strip_using_decl (decl);
14566 : :
14567 : 5172490 : if (!(TREE_CODE (decl) == NAMESPACE_DECL && !DECL_NAMESPACE_ALIAS (decl)))
14568 : : {
14569 : 5165224 : tree inner = decl;
14570 : :
14571 : 5165224 : if (TREE_CODE (inner) == CONST_DECL
14572 : 4981 : && TREE_CODE (DECL_CONTEXT (inner)) == ENUMERAL_TYPE
14573 : : /* A using-decl could make a CONST_DECL purview for a non-purview
14574 : : enumeration. */
14575 : 5170205 : && (!DECL_LANG_SPECIFIC (inner) || !DECL_MODULE_PURVIEW_P (inner)))
14576 : 4943 : inner = TYPE_NAME (DECL_CONTEXT (inner));
14577 : 5160281 : else if (TREE_CODE (inner) == TEMPLATE_DECL)
14578 : 94717 : inner = DECL_TEMPLATE_RESULT (inner);
14579 : :
14580 : 10171777 : if ((!DECL_LANG_SPECIFIC (inner) || !DECL_MODULE_PURVIEW_P (inner))
14581 : 10024919 : && !((flags & WMB_Using) && (flags & WMB_Purview)))
14582 : : /* Ignore entities not within the module purview. We'll need to
14583 : : create bindings for any non-discarded function calls for ADL,
14584 : : but it's simpler to handle that at the point of use rather
14585 : : than trying to clear out bindings after the fact. */
14586 : : return false;
14587 : :
14588 : 146969 : bool internal_decl = false;
14589 : 146969 : if (!header_module_p () && data->hash->is_tu_local_entity (decl))
14590 : : {
14591 : : /* A TU-local entity. For ADL we still need to create bindings
14592 : : for internal-linkage functions attached to a named module. */
14593 : 120 : if (DECL_DECLARES_FUNCTION_P (inner)
14594 : 96 : && DECL_LANG_SPECIFIC (inner)
14595 : 312 : && DECL_MODULE_ATTACH_P (inner))
14596 : : {
14597 : 87 : gcc_checking_assert (!DECL_MODULE_EXPORT_P (inner));
14598 : : internal_decl = true;
14599 : : }
14600 : : else
14601 : : return false;
14602 : : }
14603 : :
14604 : 146840 : if ((TREE_CODE (decl) == VAR_DECL
14605 : 146840 : || TREE_CODE (decl) == TYPE_DECL)
14606 : 146840 : && DECL_TINFO_P (decl))
14607 : : /* Ignore TINFO things. */
14608 : : return false;
14609 : :
14610 : 146840 : if (TREE_CODE (decl) == VAR_DECL && DECL_NTTP_OBJECT_P (decl))
14611 : : /* Ignore NTTP objects. */
14612 : : return false;
14613 : :
14614 : 146840 : if (deduction_guide_p (decl))
14615 : : {
14616 : : /* Ignore deduction guides, bindings for them will be created within
14617 : : find_dependencies for their class template. But still build a dep
14618 : : for them so that we don't discard them. */
14619 : 1333 : data->hash->make_dependency (decl, EK_FOR_BINDING);
14620 : 1333 : return false;
14621 : : }
14622 : :
14623 : 145507 : if (!(flags & WMB_Using) && CP_DECL_CONTEXT (decl) != data->ns)
14624 : : {
14625 : : /* An unscoped enum constant implicitly brought into the containing
14626 : : namespace. We treat this like a using-decl. */
14627 : 3117 : gcc_checking_assert (TREE_CODE (decl) == CONST_DECL);
14628 : :
14629 : 3117 : flags = WMB_Flags (flags | WMB_Using);
14630 : 3117 : if (DECL_MODULE_EXPORT_P (TYPE_NAME (TREE_TYPE (decl)))
14631 : : /* A using-decl can make an enum constant exported for a
14632 : : non-exported enumeration. */
14633 : 3117 : || (DECL_LANG_SPECIFIC (decl) && DECL_MODULE_EXPORT_P (decl)))
14634 : 2895 : flags = WMB_Flags (flags | WMB_Export);
14635 : : }
14636 : :
14637 : 145507 : if (!data->binding)
14638 : : /* No binding to check. */;
14639 : 28645 : else if (flags & WMB_Using)
14640 : : {
14641 : : /* Look in the binding to see if we already have this
14642 : : using. */
14643 : 19124 : for (unsigned ix = data->binding->deps.length (); --ix;)
14644 : : {
14645 : 15064 : depset *d = data->binding->deps[ix];
14646 : 30128 : if (d->get_entity_kind () == EK_USING
14647 : 15064 : && OVL_FUNCTION (d->get_entity ()) == decl)
14648 : : {
14649 : 0 : if (!(flags & WMB_Hidden))
14650 : 0 : d->clear_hidden_binding ();
14651 : 0 : OVL_PURVIEW_P (d->get_entity ()) = true;
14652 : 0 : if (flags & WMB_Export)
14653 : 0 : OVL_EXPORT_P (d->get_entity ()) = true;
14654 : 0 : return bool (flags & WMB_Export);
14655 : : }
14656 : : }
14657 : : }
14658 : 26615 : else if (flags & WMB_Dups)
14659 : : {
14660 : : /* Look in the binding to see if we already have this decl. */
14661 : 78 : for (unsigned ix = data->binding->deps.length (); --ix;)
14662 : : {
14663 : 39 : depset *d = data->binding->deps[ix];
14664 : 39 : if (d->get_entity () == decl)
14665 : : {
14666 : 33 : if (!(flags & WMB_Hidden))
14667 : 30 : d->clear_hidden_binding ();
14668 : 33 : return false;
14669 : : }
14670 : : }
14671 : : }
14672 : :
14673 : : /* We're adding something. */
14674 : 145474 : if (!data->binding)
14675 : : {
14676 : 116862 : data->binding = make_binding (data->ns, DECL_NAME (decl));
14677 : 116862 : data->hash->add_namespace_context (data->binding, data->ns);
14678 : :
14679 : 116862 : depset **slot = data->hash->binding_slot (data->ns,
14680 : 116862 : DECL_NAME (decl), true);
14681 : 116862 : gcc_checking_assert (!*slot);
14682 : 116862 : *slot = data->binding;
14683 : : }
14684 : :
14685 : : /* Make sure nobody left a tree visited lying about. */
14686 : 145474 : gcc_checking_assert (!TREE_VISITED (decl));
14687 : :
14688 : 145474 : if (flags & WMB_Using)
14689 : : {
14690 : 13560 : decl = ovl_make (decl, NULL_TREE);
14691 : 13560 : OVL_USING_P (decl) = true;
14692 : 13560 : OVL_PURVIEW_P (decl) = true;
14693 : 13560 : if (flags & WMB_Export)
14694 : 12700 : OVL_EXPORT_P (decl) = true;
14695 : : }
14696 : :
14697 : 145474 : entity_kind ek = EK_FOR_BINDING;
14698 : 145474 : if (internal_decl)
14699 : : ek = EK_TU_LOCAL;
14700 : 145387 : else if (flags & WMB_Using)
14701 : 13560 : ek = EK_USING;
14702 : :
14703 : 145474 : depset *dep = data->hash->make_dependency (decl, ek);
14704 : 145474 : if (flags & WMB_Hidden)
14705 : 4690 : dep->set_hidden_binding ();
14706 : 145474 : data->binding->deps.safe_push (dep);
14707 : : /* Binding and contents are mutually dependent. */
14708 : 145474 : dep->deps.safe_push (data->binding);
14709 : :
14710 : 145474 : return (flags & WMB_Using
14711 : 145474 : ? flags & WMB_Export : DECL_MODULE_EXPORT_P (decl));
14712 : : }
14713 : 7266 : else if (!data->met_namespace)
14714 : : {
14715 : : /* Namespace, walk exactly once. */
14716 : 7257 : data->met_namespace = true;
14717 : 7257 : if (data->hash->add_namespace_entities (decl, data->partitions))
14718 : : {
14719 : : /* It contains an exported thing, so it is exported. */
14720 : 1385 : gcc_checking_assert (DECL_MODULE_PURVIEW_P (decl));
14721 : 1385 : gcc_checking_assert (TREE_PUBLIC (decl) || header_module_p ());
14722 : 1385 : DECL_MODULE_EXPORT_P (decl) = true;
14723 : : }
14724 : :
14725 : 7257 : if (DECL_MODULE_PURVIEW_P (decl))
14726 : : {
14727 : 1689 : data->hash->make_dependency (decl, depset::EK_NAMESPACE);
14728 : :
14729 : 1689 : return DECL_MODULE_EXPORT_P (decl);
14730 : : }
14731 : : }
14732 : :
14733 : : return false;
14734 : : }
14735 : :
14736 : : /* Recursively find all the namespace bindings of NS. Add a depset
14737 : : for every binding that contains an export or module-linkage entity.
14738 : : Add a defining depset for every such decl that we need to write a
14739 : : definition. Such defining depsets depend on the binding depset.
14740 : : Returns true if we contain something exported. */
14741 : :
14742 : : bool
14743 : 9770 : depset::hash::add_namespace_entities (tree ns, bitmap partitions)
14744 : : {
14745 : 10838 : dump () && dump ("Looking for writables in %N", ns);
14746 : 9770 : dump.indent ();
14747 : :
14748 : 9770 : unsigned count = 0;
14749 : 9770 : add_binding_data data;
14750 : 9770 : data.ns = ns;
14751 : 9770 : data.partitions = partitions;
14752 : 9770 : data.hash = this;
14753 : :
14754 : 13550756 : for (tree binding : *DECL_NAMESPACE_BINDINGS (ns))
14755 : : {
14756 : 6770493 : data.binding = nullptr;
14757 : 6770493 : data.met_namespace = false;
14758 : 6770493 : if (walk_module_binding (binding, partitions, add_binding_entity, &data))
14759 : 109315 : count++;
14760 : : }
14761 : :
14762 : : /* Seed any using-directives so that we emit the relevant namespaces. */
14763 : 10244 : for (tree udir : NAMESPACE_LEVEL (ns)->using_directives)
14764 : 162 : if (TREE_CODE (udir) == USING_DECL && DECL_MODULE_EXPORT_P (udir))
14765 : : {
14766 : 85 : make_dependency (USING_DECL_DECLS (udir), depset::EK_NAMESPACE);
14767 : 85 : count++;
14768 : : }
14769 : :
14770 : 9770 : if (count)
14771 : 3500 : dump () && dump ("Found %u entries", count);
14772 : 9770 : dump.outdent ();
14773 : :
14774 : 9770 : return count != 0;
14775 : : }
14776 : :
14777 : : void
14778 : 197 : depset::hash::add_partial_entities (vec<tree, va_gc> *partial_classes)
14779 : : {
14780 : 15707 : for (unsigned ix = 0; ix != partial_classes->length (); ix++)
14781 : : {
14782 : 15510 : tree inner = (*partial_classes)[ix];
14783 : :
14784 : 15510 : depset *dep = make_dependency (inner, depset::EK_DECL);
14785 : :
14786 : 15510 : if (dep->get_entity_kind () == depset::EK_REDIRECT)
14787 : : {
14788 : 15510 : dep = dep->deps[0];
14789 : : /* We should have recorded the template as a partial
14790 : : specialization. */
14791 : 15510 : gcc_checking_assert (dep->get_entity_kind ()
14792 : : == depset::EK_PARTIAL);
14793 : :
14794 : : /* Only emit GM entities if reached. */
14795 : 15510 : if (!DECL_LANG_SPECIFIC (inner)
14796 : 26083 : || !DECL_MODULE_PURVIEW_P (inner))
14797 : 4979 : dep->set_flag_bit<DB_UNREACHED_BIT> ();
14798 : : }
14799 : : else
14800 : : {
14801 : : /* It was an explicit specialization, not a partial one.
14802 : : We should have already added this. */
14803 : 0 : gcc_checking_assert (dep->get_entity_kind ()
14804 : : == depset::EK_SPECIALIZATION);
14805 : 0 : gcc_checking_assert (dep->is_special ());
14806 : : }
14807 : : }
14808 : 197 : }
14809 : :
14810 : : /* Add the members of imported classes that we defined in this TU.
14811 : : This will also include lazily created implicit member function
14812 : : declarations. (All others will be definitions.) */
14813 : :
14814 : : void
14815 : 12 : depset::hash::add_class_entities (vec<tree, va_gc> *class_members)
14816 : : {
14817 : 24 : for (unsigned ix = 0; ix != class_members->length (); ix++)
14818 : : {
14819 : 12 : tree defn = (*class_members)[ix];
14820 : 12 : depset *dep = make_dependency (defn, EK_INNER_DECL);
14821 : :
14822 : 12 : if (dep->get_entity_kind () == EK_REDIRECT)
14823 : 0 : dep = dep->deps[0];
14824 : :
14825 : : /* Only non-instantiations need marking as pendings. */
14826 : 24 : if (dep->get_entity_kind () == EK_DECL)
14827 : 12 : dep->set_flag_bit <DB_IS_PENDING_BIT> ();
14828 : : }
14829 : 12 : }
14830 : :
14831 : : /* We add the partial & explicit specializations, and the explicit
14832 : : instantiations. */
14833 : :
14834 : : static void
14835 : 609763 : specialization_add (bool decl_p, spec_entry *entry, void *data_)
14836 : : {
14837 : 609763 : vec<spec_entry *> *data = reinterpret_cast <vec<spec_entry *> *> (data_);
14838 : :
14839 : 609763 : if (!decl_p)
14840 : : {
14841 : : /* We exclusively use decls to locate things. Make sure there's
14842 : : no mismatch between the two specialization tables we keep.
14843 : : pt.cc optimizes instantiation lookup using a complicated
14844 : : heuristic. We don't attempt to replicate that algorithm, but
14845 : : observe its behaviour and reproduce it upon read back. */
14846 : :
14847 : 193227 : gcc_checking_assert (TREE_CODE (entry->spec) == ENUMERAL_TYPE
14848 : : || DECL_CLASS_TEMPLATE_P (entry->tmpl));
14849 : :
14850 : 193227 : gcc_checking_assert (!match_mergeable_specialization (true, entry));
14851 : : }
14852 : 416536 : else if (VAR_OR_FUNCTION_DECL_P (entry->spec))
14853 : 204569 : gcc_checking_assert (!DECL_LOCAL_DECL_P (entry->spec));
14854 : :
14855 : 609763 : data->safe_push (entry);
14856 : 609763 : }
14857 : :
14858 : : /* Arbitrary stable comparison. */
14859 : :
14860 : : static int
14861 : 36082294 : specialization_cmp (const void *a_, const void *b_)
14862 : : {
14863 : 36082294 : const spec_entry *ea = *reinterpret_cast<const spec_entry *const *> (a_);
14864 : 36082294 : const spec_entry *eb = *reinterpret_cast<const spec_entry *const *> (b_);
14865 : :
14866 : 36082294 : if (ea == eb)
14867 : : return 0;
14868 : :
14869 : 36082294 : tree a = ea->spec;
14870 : 36082294 : tree b = eb->spec;
14871 : 36082294 : if (TYPE_P (a))
14872 : : {
14873 : 10861258 : a = TYPE_NAME (a);
14874 : 10861258 : b = TYPE_NAME (b);
14875 : : }
14876 : :
14877 : 36082294 : if (a == b)
14878 : : /* This can happen with friend specializations. Just order by
14879 : : entry address. See note in depset_cmp. */
14880 : 157 : return ea < eb ? -1 : +1;
14881 : :
14882 : 36082176 : return DECL_UID (a) < DECL_UID (b) ? -1 : +1;
14883 : : }
14884 : :
14885 : : /* We add all kinds of specialializations. Implicit specializations
14886 : : should only streamed and walked if they are reachable from
14887 : : elsewhere. Hence the UNREACHED flag. This is making the
14888 : : assumption that it is cheaper to reinstantiate them on demand
14889 : : elsewhere, rather than stream them in when we instantiate their
14890 : : general template. Also, if we do stream them, we can only do that
14891 : : if they are not internal (which they can become if they themselves
14892 : : touch an internal entity?). */
14893 : :
14894 : : void
14895 : 5026 : depset::hash::add_specializations (bool decl_p)
14896 : : {
14897 : 5026 : vec<spec_entry *> data;
14898 : 5026 : data.create (100);
14899 : 5026 : walk_specializations (decl_p, specialization_add, &data);
14900 : 5026 : data.qsort (specialization_cmp);
14901 : 614789 : while (data.length ())
14902 : : {
14903 : 609763 : spec_entry *entry = data.pop ();
14904 : 609763 : tree spec = entry->spec;
14905 : 609763 : int use_tpl = 0;
14906 : 609763 : bool is_friend = false;
14907 : :
14908 : 609763 : if (decl_p && DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (entry->tmpl))
14909 : : /* A friend of a template. This is keyed to the
14910 : : instantiation. */
14911 : : is_friend = true;
14912 : :
14913 : 609763 : if (decl_p)
14914 : : {
14915 : 416536 : if (tree ti = DECL_TEMPLATE_INFO (spec))
14916 : : {
14917 : 416536 : tree tmpl = TI_TEMPLATE (ti);
14918 : :
14919 : 416536 : use_tpl = DECL_USE_TEMPLATE (spec);
14920 : 416536 : if (spec == DECL_TEMPLATE_RESULT (tmpl))
14921 : : {
14922 : 2784 : spec = tmpl;
14923 : 2784 : gcc_checking_assert (DECL_USE_TEMPLATE (spec) == use_tpl);
14924 : : }
14925 : 413752 : else if (is_friend)
14926 : : {
14927 : 2913 : if (TI_TEMPLATE (ti) != entry->tmpl
14928 : 2913 : || !template_args_equal (TI_ARGS (ti), entry->tmpl))
14929 : 2913 : goto template_friend;
14930 : : }
14931 : : }
14932 : : else
14933 : : {
14934 : 0 : template_friend:;
14935 : 2913 : gcc_checking_assert (is_friend);
14936 : : /* This is a friend of a template class, but not the one
14937 : : that generated entry->spec itself (i.e. it's an
14938 : : equivalent clone). We do not need to record
14939 : : this. */
14940 : 2913 : continue;
14941 : : }
14942 : : }
14943 : : else
14944 : : {
14945 : 193227 : if (TREE_CODE (spec) == ENUMERAL_TYPE)
14946 : : {
14947 : 1051 : tree ctx = DECL_CONTEXT (TYPE_NAME (spec));
14948 : :
14949 : 1051 : if (TYPE_P (ctx))
14950 : 1045 : use_tpl = CLASSTYPE_USE_TEMPLATE (ctx);
14951 : : else
14952 : 6 : use_tpl = DECL_USE_TEMPLATE (ctx);
14953 : : }
14954 : : else
14955 : 192176 : use_tpl = CLASSTYPE_USE_TEMPLATE (spec);
14956 : :
14957 : 193227 : tree ti = TYPE_TEMPLATE_INFO (spec);
14958 : 193227 : tree tmpl = TI_TEMPLATE (ti);
14959 : :
14960 : 193227 : spec = TYPE_NAME (spec);
14961 : 193227 : if (spec == DECL_TEMPLATE_RESULT (tmpl))
14962 : : {
14963 : 775 : spec = tmpl;
14964 : 775 : use_tpl = DECL_USE_TEMPLATE (spec);
14965 : : }
14966 : : }
14967 : :
14968 : 606850 : bool needs_reaching = false;
14969 : 606850 : if (use_tpl == 1)
14970 : : /* Implicit instantiations only walked if we reach them. */
14971 : : needs_reaching = true;
14972 : 58703 : else if (!DECL_LANG_SPECIFIC (STRIP_TEMPLATE (spec))
14973 : 106716 : || !DECL_MODULE_PURVIEW_P (STRIP_TEMPLATE (spec)))
14974 : : /* Likewise, GMF explicit or partial specializations. */
14975 : : needs_reaching = true;
14976 : :
14977 : : #if false && CHECKING_P
14978 : : /* The instantiation isn't always on
14979 : : DECL_TEMPLATE_INSTANTIATIONS, */
14980 : : // FIXME: we probably need to remember this information?
14981 : : /* Verify the specialization is on the
14982 : : DECL_TEMPLATE_INSTANTIATIONS of the template. */
14983 : : for (tree cons = DECL_TEMPLATE_INSTANTIATIONS (entry->tmpl);
14984 : : cons; cons = TREE_CHAIN (cons))
14985 : : if (TREE_VALUE (cons) == entry->spec)
14986 : : {
14987 : : gcc_assert (entry->args == TREE_PURPOSE (cons));
14988 : : goto have_spec;
14989 : : }
14990 : : gcc_unreachable ();
14991 : : have_spec:;
14992 : : #endif
14993 : :
14994 : : /* Make sure nobody left a tree visited lying about. */
14995 : 606850 : gcc_checking_assert (!TREE_VISITED (spec));
14996 : 606850 : depset *dep = make_dependency (spec, depset::EK_SPECIALIZATION);
14997 : 606850 : if (dep->is_special ())
14998 : 0 : gcc_unreachable ();
14999 : : else
15000 : : {
15001 : 606850 : if (dep->get_entity_kind () == depset::EK_REDIRECT)
15002 : 17470 : dep = dep->deps[0];
15003 : 589380 : else if (dep->get_entity_kind () == depset::EK_SPECIALIZATION)
15004 : : {
15005 : 589380 : dep->set_special ();
15006 : 589380 : dep->deps.safe_push (reinterpret_cast<depset *> (entry));
15007 : 589380 : if (!decl_p)
15008 : 178406 : dep->set_flag_bit<DB_TYPE_SPEC_BIT> ();
15009 : : }
15010 : :
15011 : 606850 : if (needs_reaching)
15012 : 566234 : dep->set_flag_bit<DB_UNREACHED_BIT> ();
15013 : 606850 : if (is_friend)
15014 : 0 : dep->set_flag_bit<DB_FRIEND_SPEC_BIT> ();
15015 : : }
15016 : : }
15017 : 5026 : data.release ();
15018 : 5026 : }
15019 : :
15020 : : /* Add a depset into the mergeable hash. */
15021 : :
15022 : : void
15023 : 790517 : depset::hash::add_mergeable (depset *mergeable)
15024 : : {
15025 : 790517 : gcc_checking_assert (is_key_order ());
15026 : 790517 : entity_kind ek = mergeable->get_entity_kind ();
15027 : 790517 : tree decl = mergeable->get_entity ();
15028 : 790517 : gcc_checking_assert (ek < EK_DIRECT_HWM);
15029 : :
15030 : 790517 : depset **slot = entity_slot (decl, true);
15031 : 790517 : gcc_checking_assert (!*slot);
15032 : 790517 : depset *dep = make_entity (decl, ek);
15033 : 790517 : *slot = dep;
15034 : :
15035 : 790517 : worklist.safe_push (dep);
15036 : :
15037 : : /* So we can locate the mergeable depset this depset refers to,
15038 : : mark the first dep. */
15039 : 790517 : dep->set_special ();
15040 : 790517 : dep->deps.safe_push (mergeable);
15041 : 790517 : }
15042 : :
15043 : : /* Find the innermost-namespace scope of DECL, and that
15044 : : namespace-scope decl. */
15045 : :
15046 : : tree
15047 : 25762561 : find_pending_key (tree decl, tree *decl_p = nullptr)
15048 : : {
15049 : 25762561 : tree ns = decl;
15050 : 30811887 : do
15051 : : {
15052 : 30811887 : decl = ns;
15053 : 30811887 : ns = CP_DECL_CONTEXT (ns);
15054 : 30811887 : if (TYPE_P (ns))
15055 : 3188586 : ns = TYPE_NAME (ns);
15056 : : }
15057 : 30811887 : while (TREE_CODE (ns) != NAMESPACE_DECL);
15058 : :
15059 : 25762561 : if (decl_p)
15060 : 25409428 : *decl_p = decl;
15061 : :
15062 : 25762561 : return ns;
15063 : : }
15064 : :
15065 : : /* Creates bindings and dependencies for all deduction guides of
15066 : : the given class template DECL as needed. */
15067 : :
15068 : : void
15069 : 39918 : depset::hash::add_deduction_guides (tree decl)
15070 : : {
15071 : : /* Alias templates never have deduction guides. */
15072 : 39918 : if (DECL_ALIAS_TEMPLATE_P (decl))
15073 : 39321 : return;
15074 : :
15075 : : /* We don't need to do anything for class-scope deduction guides,
15076 : : as they will be added as members anyway. */
15077 : 39918 : if (!DECL_NAMESPACE_SCOPE_P (decl))
15078 : : return;
15079 : :
15080 : 31899 : tree ns = CP_DECL_CONTEXT (decl);
15081 : 31899 : tree name = dguide_name (decl);
15082 : :
15083 : : /* We always add all deduction guides with a given name at once,
15084 : : so if there's already a binding there's nothing to do. */
15085 : 31899 : if (find_binding (ns, name))
15086 : : return;
15087 : :
15088 : 30314 : tree guides = lookup_qualified_name (ns, name, LOOK_want::NORMAL,
15089 : : /*complain=*/false);
15090 : 30314 : if (guides == error_mark_node)
15091 : : return;
15092 : :
15093 : 597 : depset *binding = nullptr;
15094 : 2651 : for (tree t : lkp_range (guides))
15095 : : {
15096 : 1457 : gcc_checking_assert (!TREE_VISITED (t));
15097 : 1457 : depset *dep = make_dependency (t, EK_FOR_BINDING);
15098 : :
15099 : : /* We don't want to create bindings for imported deduction guides, as
15100 : : this would potentially cause name lookup to return duplicates. */
15101 : 1457 : if (dep->is_import ())
15102 : 6 : continue;
15103 : :
15104 : 1451 : if (!binding)
15105 : : {
15106 : : /* We have bindings to add. */
15107 : 591 : binding = make_binding (ns, name);
15108 : 591 : add_namespace_context (binding, ns);
15109 : :
15110 : 591 : depset **slot = binding_slot (ns, name, /*insert=*/true);
15111 : 591 : *slot = binding;
15112 : : }
15113 : :
15114 : 1451 : binding->deps.safe_push (dep);
15115 : 1451 : dep->deps.safe_push (binding);
15116 : 1451 : dump (dumper::DEPEND)
15117 : 0 : && dump ("Built binding for deduction guide %C:%N",
15118 : 0 : TREE_CODE (decl), decl);
15119 : : }
15120 : : }
15121 : :
15122 : : /* Iteratively find dependencies. During the walk we may find more
15123 : : entries on the same binding that need walking. */
15124 : :
15125 : : void
15126 : 225133 : depset::hash::find_dependencies (module_state *module)
15127 : : {
15128 : 225133 : trees_out walker (NULL, module, *this);
15129 : 225133 : vec<depset *> unreached;
15130 : 450266 : unreached.create (worklist.length ());
15131 : :
15132 : 1018 : for (;;)
15133 : : {
15134 : 226151 : reached_unreached = false;
15135 : 3399851 : while (worklist.length ())
15136 : : {
15137 : 3173700 : depset *item = worklist.pop ();
15138 : :
15139 : 3173700 : gcc_checking_assert (!item->is_binding ());
15140 : 3173700 : if (item->is_unreached ())
15141 : 1574163 : unreached.quick_push (item);
15142 : : else
15143 : : {
15144 : 1599537 : current = item;
15145 : 1599537 : tree decl = current->get_entity ();
15146 : 1599537 : dump (is_key_order () ? dumper::MERGE : dumper::DEPEND)
15147 : 1600590 : && dump ("Dependencies of %s %C:%N",
15148 : 1053 : is_key_order () ? "key-order"
15149 : 1053 : : current->entity_kind_name (), TREE_CODE (decl), decl);
15150 : 1599537 : dump.indent ();
15151 : 1599537 : walker.begin ();
15152 : 1599537 : if (current->get_entity_kind () == EK_USING)
15153 : 13560 : walker.tree_node (OVL_FUNCTION (decl));
15154 : 1585977 : else if (current->get_entity_kind () == EK_TU_LOCAL)
15155 : : /* We only stream its name and location. */
15156 : 87 : module->note_location (DECL_SOURCE_LOCATION (decl));
15157 : 1585890 : else if (TREE_VISITED (decl))
15158 : : /* A global tree. */;
15159 : 1583616 : else if (current->get_entity_kind () == EK_NAMESPACE)
15160 : : {
15161 : 2037 : module->note_location (DECL_SOURCE_LOCATION (decl));
15162 : 2037 : add_namespace_context (current, CP_DECL_CONTEXT (decl));
15163 : : }
15164 : : else
15165 : : {
15166 : 1581579 : walker.mark_declaration (decl, current->has_defn ());
15167 : :
15168 : 1581579 : if (!is_key_order ()
15169 : 1581579 : && item->is_pending_entity ())
15170 : : {
15171 : 353133 : tree ns = find_pending_key (decl, nullptr);
15172 : 353133 : add_namespace_context (item, ns);
15173 : : }
15174 : :
15175 : 1581579 : walker.decl_value (decl, current);
15176 : 1581579 : if (current->has_defn ())
15177 : 306440 : walker.write_definition (decl, current->refs_tu_local ());
15178 : : }
15179 : 1599537 : walker.end ();
15180 : :
15181 : : /* If we see either a class template or a deduction guide, make
15182 : : sure to add all visible deduction guides. We need to check
15183 : : both in case they have been added in separate modules, or
15184 : : one is in the GMF and would have otherwise been discarded. */
15185 : 1599537 : if (!is_key_order ()
15186 : 1599537 : && DECL_CLASS_TEMPLATE_P (decl))
15187 : 38461 : add_deduction_guides (decl);
15188 : 1599537 : if (!is_key_order ()
15189 : 1599537 : && deduction_guide_p (decl))
15190 : 1457 : add_deduction_guides (TYPE_NAME (TREE_TYPE (TREE_TYPE (decl))));
15191 : :
15192 : 1599537 : if (!is_key_order ()
15193 : 809020 : && TREE_CODE (decl) == TEMPLATE_DECL
15194 : 1877549 : && !DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (decl))
15195 : : {
15196 : : /* Mark all the explicit & partial specializations as
15197 : : reachable. We search both specialization lists as some
15198 : : constrained partial specializations for class types are
15199 : : only found in DECL_TEMPLATE_SPECIALIZATIONS. */
15200 : 788314 : auto mark_reached = [this](tree spec)
15201 : : {
15202 : 515261 : if (TYPE_P (spec))
15203 : 156193 : spec = TYPE_NAME (spec);
15204 : 515261 : int use_tpl;
15205 : 515261 : node_template_info (spec, use_tpl);
15206 : 515261 : if (use_tpl & 2)
15207 : : {
15208 : 60732 : depset *spec_dep = find_dependency (spec);
15209 : 60732 : if (spec_dep->get_entity_kind () == EK_REDIRECT)
15210 : 13664 : spec_dep = spec_dep->deps[0];
15211 : 60732 : if (spec_dep->is_unreached ())
15212 : : {
15213 : 5365 : reached_unreached = true;
15214 : 5365 : spec_dep->clear_flag_bit<DB_UNREACHED_BIT> ();
15215 : 5365 : dump (dumper::DEPEND)
15216 : 0 : && dump ("Reaching unreached specialization"
15217 : 0 : " %C:%N", TREE_CODE (spec), spec);
15218 : : }
15219 : : }
15220 : 788314 : };
15221 : :
15222 : 273053 : for (tree cons = DECL_TEMPLATE_INSTANTIATIONS (decl);
15223 : 774031 : cons; cons = TREE_CHAIN (cons))
15224 : 500978 : mark_reached (TREE_VALUE (cons));
15225 : 273053 : for (tree cons = DECL_TEMPLATE_SPECIALIZATIONS (decl);
15226 : 287336 : cons; cons = TREE_CHAIN (cons))
15227 : 14283 : mark_reached (TREE_VALUE (cons));
15228 : : }
15229 : :
15230 : 1599537 : dump.outdent ();
15231 : 1599537 : current = NULL;
15232 : : }
15233 : : }
15234 : :
15235 : 226151 : if (!reached_unreached)
15236 : : break;
15237 : :
15238 : : /* It's possible the we reached the unreached before we
15239 : : processed it in the above loop, so we'll be doing this an
15240 : : extra time. However, to avoid that we have to do some
15241 : : bit shuffling that also involves a scan of the list.
15242 : : Swings & roundabouts I guess. */
15243 : 1018 : std::swap (worklist, unreached);
15244 : : }
15245 : :
15246 : 225133 : unreached.release ();
15247 : 225133 : }
15248 : :
15249 : : /* Compare two entries of a single binding. TYPE_DECL before
15250 : : non-exported before exported. */
15251 : :
15252 : : static int
15253 : 436644 : binding_cmp (const void *a_, const void *b_)
15254 : : {
15255 : 436644 : depset *a = *(depset *const *)a_;
15256 : 436644 : depset *b = *(depset *const *)b_;
15257 : :
15258 : 436644 : tree a_ent = a->get_entity ();
15259 : 436644 : tree b_ent = b->get_entity ();
15260 : 436644 : gcc_checking_assert (a_ent != b_ent
15261 : : && !a->is_binding ()
15262 : : && !b->is_binding ());
15263 : :
15264 : : /* Implicit typedefs come first. */
15265 : 436644 : bool a_implicit = DECL_IMPLICIT_TYPEDEF_P (a_ent);
15266 : 436644 : bool b_implicit = DECL_IMPLICIT_TYPEDEF_P (b_ent);
15267 : 436536 : if (a_implicit || b_implicit)
15268 : : {
15269 : : /* A binding with two implicit type decls? That's unpossible! */
15270 : 216 : gcc_checking_assert (!(a_implicit && b_implicit));
15271 : 324 : return a_implicit ? -1 : +1; /* Implicit first. */
15272 : : }
15273 : :
15274 : : /* TU-local before non-TU-local. */
15275 : 436428 : bool a_internal = a->get_entity_kind () == depset::EK_TU_LOCAL;
15276 : 436428 : bool b_internal = b->get_entity_kind () == depset::EK_TU_LOCAL;
15277 : 436428 : if (a_internal != b_internal)
15278 : 0 : return a_internal ? -1 : +1; /* Internal first. */
15279 : :
15280 : : /* Hidden before non-hidden. */
15281 : 436428 : bool a_hidden = a->is_hidden ();
15282 : 436428 : bool b_hidden = b->is_hidden ();
15283 : 436428 : if (a_hidden != b_hidden)
15284 : 49281 : return a_hidden ? -1 : +1;
15285 : :
15286 : 404116 : bool a_using = a->get_entity_kind () == depset::EK_USING;
15287 : 404116 : bool a_export;
15288 : 404116 : if (a_using)
15289 : : {
15290 : 27715 : a_export = OVL_EXPORT_P (a_ent);
15291 : 27715 : a_ent = OVL_FUNCTION (a_ent);
15292 : : }
15293 : 376401 : else if (TREE_CODE (a_ent) == CONST_DECL
15294 : 0 : && DECL_LANG_SPECIFIC (a_ent)
15295 : 376401 : && DECL_MODULE_EXPORT_P (a_ent))
15296 : : a_export = true;
15297 : : else
15298 : 376401 : a_export = DECL_MODULE_EXPORT_P (TREE_CODE (a_ent) == CONST_DECL
15299 : : ? TYPE_NAME (TREE_TYPE (a_ent))
15300 : : : STRIP_TEMPLATE (a_ent));
15301 : :
15302 : 404116 : bool b_using = b->get_entity_kind () == depset::EK_USING;
15303 : 404116 : bool b_export;
15304 : 404116 : if (b_using)
15305 : : {
15306 : 24238 : b_export = OVL_EXPORT_P (b_ent);
15307 : 24238 : b_ent = OVL_FUNCTION (b_ent);
15308 : : }
15309 : 379878 : else if (TREE_CODE (b_ent) == CONST_DECL
15310 : 0 : && DECL_LANG_SPECIFIC (b_ent)
15311 : 379878 : && DECL_MODULE_EXPORT_P (b_ent))
15312 : : b_export = true;
15313 : : else
15314 : 379878 : b_export = DECL_MODULE_EXPORT_P (TREE_CODE (b_ent) == CONST_DECL
15315 : : ? TYPE_NAME (TREE_TYPE (b_ent))
15316 : : : STRIP_TEMPLATE (b_ent));
15317 : :
15318 : : /* Non-exports before exports. */
15319 : 404116 : if (a_export != b_export)
15320 : 360 : return a_export ? +1 : -1;
15321 : :
15322 : : /* At this point we don't care, but want a stable sort. */
15323 : :
15324 : 403906 : if (a_using != b_using)
15325 : : /* using first. */
15326 : 20004 : return a_using? -1 : +1;
15327 : :
15328 : 389384 : return DECL_UID (a_ent) < DECL_UID (b_ent) ? -1 : +1;
15329 : : }
15330 : :
15331 : : /* True iff TMPL has an explicit instantiation definition.
15332 : :
15333 : : This is local to module.cc because register_specialization skips adding most
15334 : : instantiations unless module_maybe_has_cmi_p. */
15335 : :
15336 : : static bool
15337 : 55 : template_has_explicit_inst (tree tmpl)
15338 : : {
15339 : 61 : for (tree t = DECL_TEMPLATE_INSTANTIATIONS (tmpl); t; t = TREE_CHAIN (t))
15340 : : {
15341 : 12 : tree spec = TREE_VALUE (t);
15342 : 12 : if (DECL_EXPLICIT_INSTANTIATION (spec)
15343 : 12 : && !DECL_REALLY_EXTERN (spec))
15344 : : return true;
15345 : : }
15346 : : return false;
15347 : : }
15348 : :
15349 : : /* Sort the bindings, issue errors about bad internal refs. */
15350 : :
15351 : : bool
15352 : 2513 : depset::hash::finalize_dependencies ()
15353 : : {
15354 : 2513 : bool ok = true;
15355 : 2453501 : for (depset *dep : *this)
15356 : : {
15357 : 1225494 : if (dep->is_binding ())
15358 : : {
15359 : : /* Keep the containing namespace dep first. */
15360 : 118678 : gcc_checking_assert (dep->deps.length () > 1
15361 : : && (dep->deps[0]->get_entity_kind ()
15362 : : == EK_NAMESPACE)
15363 : : && (dep->deps[0]->get_entity ()
15364 : : == dep->get_entity ()));
15365 : 118678 : if (dep->deps.length () > 2)
15366 : 8841 : gcc_qsort (&dep->deps[1], dep->deps.length () - 1,
15367 : : sizeof (dep->deps[1]), binding_cmp);
15368 : :
15369 : : /* Bindings shouldn't refer to imported entities. */
15370 : 118678 : if (CHECKING_P)
15371 : 624205 : for (depset *entity : dep->deps)
15372 : 268171 : gcc_checking_assert (!entity->is_import ());
15373 : : }
15374 : 1106913 : else if (dep->is_exposure () && !dep->is_tu_local ())
15375 : : {
15376 : 97 : ok = false;
15377 : 97 : bool explained = false;
15378 : 97 : tree decl = dep->get_entity ();
15379 : :
15380 : 394 : for (depset *rdep : dep->deps)
15381 : 185 : if (!rdep->is_binding ()
15382 : 96 : && rdep->is_tu_local ()
15383 : 267 : && !is_exposure_of_member_type (dep, rdep))
15384 : : {
15385 : : // FIXME:QOI Better location information? We're
15386 : : // losing, so it doesn't matter about efficiency
15387 : 82 : tree exposed = rdep->get_entity ();
15388 : 82 : auto_diagnostic_group d;
15389 : 82 : error_at (DECL_SOURCE_LOCATION (decl),
15390 : : "%qD exposes TU-local entity %qD", decl, exposed);
15391 : 82 : bool informed = is_tu_local_entity (exposed, /*explain=*/true);
15392 : 82 : gcc_checking_assert (informed);
15393 : 82 : explained = true;
15394 : 82 : break;
15395 : 82 : }
15396 : :
15397 : 82 : if (!explained
15398 : 15 : && VAR_P (decl)
15399 : 15 : && (DECL_DECLARED_CONSTEXPR_P (decl)
15400 : 6 : || DECL_INLINE_VAR_P (decl)))
15401 : : {
15402 : 15 : auto_diagnostic_group d;
15403 : 15 : if (DECL_DECLARED_CONSTEXPR_P (decl))
15404 : 9 : error_at (DECL_SOURCE_LOCATION (decl),
15405 : : "%qD is declared %<constexpr%> and is initialized to "
15406 : : "a TU-local value", decl);
15407 : : else
15408 : : {
15409 : : /* This can only occur with references. */
15410 : 6 : gcc_checking_assert (TYPE_REF_P (TREE_TYPE (decl)));
15411 : 6 : error_at (DECL_SOURCE_LOCATION (decl),
15412 : : "%qD is a reference declared %<inline%> and is "
15413 : : "constant-initialized to a TU-local value", decl);
15414 : : }
15415 : 15 : bool informed = is_tu_local_value (decl, DECL_INITIAL (decl),
15416 : : /*explain=*/true);
15417 : 15 : gcc_checking_assert (informed);
15418 : 15 : explained = true;
15419 : 15 : }
15420 : :
15421 : : /* We should have emitted an error above. */
15422 : 97 : gcc_checking_assert (explained);
15423 : : }
15424 : 1106719 : else if (warn_template_names_tu_local
15425 : 1190000 : && dep->refs_tu_local () && !dep->is_tu_local ())
15426 : : {
15427 : 64 : tree decl = dep->get_entity ();
15428 : :
15429 : : /* Friend decls in a class body are ignored, but this is harmless:
15430 : : it should not impact any consumers. */
15431 : 64 : if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (decl)))
15432 : 9 : continue;
15433 : :
15434 : : /* We should now only be warning about templates. */
15435 : 55 : gcc_checking_assert
15436 : : (TREE_CODE (decl) == TEMPLATE_DECL
15437 : : && VAR_OR_FUNCTION_DECL_P (DECL_TEMPLATE_RESULT (decl)));
15438 : :
15439 : : /* Don't warn if we've seen any explicit instantiation definitions,
15440 : : the intent might be for importers to only use those. */
15441 : 55 : if (template_has_explicit_inst (decl))
15442 : 6 : continue;
15443 : :
15444 : 199 : for (depset *rdep : dep->deps)
15445 : 156 : if (!rdep->is_binding () && rdep->is_tu_local ())
15446 : : {
15447 : 49 : tree ref = rdep->get_entity ();
15448 : 49 : auto_diagnostic_group d;
15449 : 49 : if (warning_at (DECL_SOURCE_LOCATION (decl),
15450 : 49 : OPT_Wtemplate_names_tu_local,
15451 : : "%qD refers to TU-local entity %qD and cannot "
15452 : : "be instantiated in other TUs", decl, ref))
15453 : 49 : is_tu_local_entity (ref, /*explain=*/true);
15454 : 49 : break;
15455 : 49 : }
15456 : : }
15457 : : }
15458 : :
15459 : 2513 : return ok;
15460 : : }
15461 : :
15462 : : /* Core of TARJAN's algorithm to find Strongly Connected Components
15463 : : within a graph. See https://en.wikipedia.org/wiki/
15464 : : Tarjan%27s_strongly_connected_components_algorithm for details.
15465 : :
15466 : : We use depset::section as lowlink. Completed nodes have
15467 : : depset::cluster containing the cluster number, with the top
15468 : : bit set.
15469 : :
15470 : : A useful property is that the output vector is a reverse
15471 : : topological sort of the resulting DAG. In our case that means
15472 : : dependent SCCs are found before their dependers. We make use of
15473 : : that property. */
15474 : :
15475 : : void
15476 : 1717291 : depset::tarjan::connect (depset *v)
15477 : : {
15478 : 1717291 : gcc_checking_assert (v->is_binding ()
15479 : : || !(v->is_tu_local ()
15480 : : || v->is_unreached ()
15481 : : || v->is_import ()));
15482 : :
15483 : 1717291 : v->cluster = v->section = ++index;
15484 : 1717291 : stack.safe_push (v);
15485 : :
15486 : : /* Walk all our dependencies, ignore a first marked slot */
15487 : 14108309 : for (unsigned ix = v->is_special (); ix != v->deps.length (); ix++)
15488 : : {
15489 : 5340528 : depset *dep = v->deps[ix];
15490 : :
15491 : 5340528 : if (dep->is_binding ()
15492 : 15722402 : || !(dep->is_import () || dep->is_tu_local ()))
15493 : : {
15494 : 5339558 : unsigned lwm = dep->cluster;
15495 : :
15496 : 5339558 : if (!dep->cluster)
15497 : : {
15498 : : /* A new node. Connect it. */
15499 : 947396 : connect (dep);
15500 : 947396 : lwm = dep->section;
15501 : : }
15502 : :
15503 : 5339558 : if (dep->section && v->section > lwm)
15504 : 858232 : v->section = lwm;
15505 : : }
15506 : : }
15507 : :
15508 : 1717291 : if (v->section == v->cluster)
15509 : : {
15510 : : /* Root of a new SCC. Push all the members onto the result list. */
15511 : : unsigned num = v->cluster;
15512 : 1717291 : depset *p;
15513 : 1717291 : do
15514 : : {
15515 : 1717291 : p = stack.pop ();
15516 : 1717291 : p->cluster = num;
15517 : 1717291 : p->section = 0;
15518 : 1717291 : result.quick_push (p);
15519 : : }
15520 : 1717291 : while (p != v);
15521 : : }
15522 : 1717291 : }
15523 : :
15524 : : /* Compare two depsets. The specific ordering is unimportant, we're
15525 : : just trying to get consistency. */
15526 : :
15527 : : static int
15528 : 79927969 : depset_cmp (const void *a_, const void *b_)
15529 : : {
15530 : 79927969 : depset *a = *(depset *const *)a_;
15531 : 79927969 : depset *b = *(depset *const *)b_;
15532 : :
15533 : 79927969 : depset::entity_kind a_kind = a->get_entity_kind ();
15534 : 79927969 : depset::entity_kind b_kind = b->get_entity_kind ();
15535 : :
15536 : 79927969 : if (a_kind != b_kind)
15537 : : /* Different entity kinds, order by that. */
15538 : 4119751 : return a_kind < b_kind ? -1 : +1;
15539 : :
15540 : 77004021 : tree a_decl = a->get_entity ();
15541 : 77004021 : tree b_decl = b->get_entity ();
15542 : 77004021 : if (a_kind == depset::EK_USING)
15543 : : {
15544 : : /* If one is a using, the other must be too. */
15545 : 742274 : a_decl = OVL_FUNCTION (a_decl);
15546 : 742274 : b_decl = OVL_FUNCTION (b_decl);
15547 : : }
15548 : :
15549 : 77004021 : if (a_decl != b_decl)
15550 : : /* Different entities, order by their UID. */
15551 : 71213753 : return DECL_UID (a_decl) < DECL_UID (b_decl) ? -1 : +1;
15552 : :
15553 : 5790268 : if (a_kind == depset::EK_BINDING)
15554 : : {
15555 : : /* Both are bindings. Order by identifier hash. */
15556 : 5787345 : gcc_checking_assert (a->get_name () != b->get_name ());
15557 : 5787345 : hashval_t ah = IDENTIFIER_HASH_VALUE (a->get_name ());
15558 : 5787345 : hashval_t bh = IDENTIFIER_HASH_VALUE (b->get_name ());
15559 : 8621304 : return (ah == bh ? 0 : ah < bh ? -1 : +1);
15560 : : }
15561 : :
15562 : : /* They are the same decl. This can happen with two using decls
15563 : : pointing to the same target. The best we can aim for is
15564 : : consistently telling qsort how to order them. Hopefully we'll
15565 : : never have to debug a case that depends on this. Oh, who am I
15566 : : kidding? Good luck. */
15567 : 2923 : gcc_checking_assert (a_kind == depset::EK_USING);
15568 : :
15569 : : /* Order by depset address. Not the best, but it is something. */
15570 : 2923 : return a < b ? -1 : +1;
15571 : : }
15572 : :
15573 : : /* Sort the clusters in SCC such that those that depend on one another
15574 : : are placed later. */
15575 : :
15576 : : // FIXME: I am not convinced this is needed and, if needed,
15577 : : // sufficient. We emit the decls in this order but that emission
15578 : : // could walk into later decls (from the body of the decl, or default
15579 : : // arg-like things). Why doesn't that walk do the right thing? And
15580 : : // if it DTRT why do we need to sort here -- won't things naturally
15581 : : // work? I think part of the issue is that when we're going to refer
15582 : : // to an entity by name, and that entity is in the same cluster as us,
15583 : : // we need to actually walk that entity, if we've not already walked
15584 : : // it.
15585 : : static void
15586 : 222620 : sort_cluster (depset::hash *original, depset *scc[], unsigned size)
15587 : : {
15588 : 222620 : depset::hash table (size, original);
15589 : :
15590 : 222620 : dump.indent ();
15591 : :
15592 : : /* Place bindings last, usings before that. It's not strictly
15593 : : necessary, but it does make things neater. Says Mr OCD. */
15594 : : unsigned bind_lwm = size;
15595 : : unsigned use_lwm = size;
15596 : 1145130 : for (unsigned ix = 0; ix != use_lwm;)
15597 : : {
15598 : 922510 : depset *dep = scc[ix];
15599 : 922510 : switch (dep->get_entity_kind ())
15600 : : {
15601 : 118436 : case depset::EK_BINDING:
15602 : : /* Move to end. No increment. Notice this could be moving
15603 : : a using decl, which we'll then move again. */
15604 : 118436 : if (--bind_lwm != ix)
15605 : : {
15606 : 62139 : scc[ix] = scc[bind_lwm];
15607 : 62139 : scc[bind_lwm] = dep;
15608 : : }
15609 : 118436 : if (use_lwm > bind_lwm)
15610 : : {
15611 : 100041 : use_lwm--;
15612 : 100041 : break;
15613 : : }
15614 : : /* We must have copied a using or TU-local, so move it too. */
15615 : 18395 : dep = scc[ix];
15616 : 18395 : gcc_checking_assert
15617 : : (dep->get_entity_kind () == depset::EK_USING
15618 : : || dep->get_entity_kind () == depset::EK_TU_LOCAL);
15619 : : /* FALLTHROUGH */
15620 : :
15621 : 31952 : case depset::EK_USING:
15622 : 31952 : case depset::EK_TU_LOCAL:
15623 : 31952 : if (--use_lwm != ix)
15624 : : {
15625 : 22784 : scc[ix] = scc[use_lwm];
15626 : 22784 : scc[use_lwm] = dep;
15627 : : }
15628 : : break;
15629 : :
15630 : 790517 : case depset::EK_DECL:
15631 : 790517 : case depset::EK_SPECIALIZATION:
15632 : 790517 : case depset::EK_PARTIAL:
15633 : 790517 : table.add_mergeable (dep);
15634 : 790517 : ix++;
15635 : 790517 : break;
15636 : :
15637 : 0 : default:
15638 : 0 : gcc_unreachable ();
15639 : : }
15640 : : }
15641 : :
15642 : 222620 : gcc_checking_assert (use_lwm <= bind_lwm);
15643 : 222884 : dump (dumper::MERGE) && dump ("Ordering %u/%u depsets", use_lwm, size);
15644 : :
15645 : 222620 : table.find_dependencies (nullptr);
15646 : :
15647 : 445240 : auto_vec<depset *> order = table.connect ();
15648 : 445240 : gcc_checking_assert (order.length () == use_lwm);
15649 : :
15650 : : /* Now rewrite entries [0,lwm), in the dependency order we
15651 : : discovered. Usually each entity is in its own cluster. Rarely,
15652 : : we can get multi-entity clusters, in which case all but one must
15653 : : only be reached from within the cluster. This happens for
15654 : : something like:
15655 : :
15656 : : template<typename T>
15657 : : auto Foo (const T &arg) -> TPL<decltype (arg)>;
15658 : :
15659 : : The instantiation of TPL will be in the specialization table, and
15660 : : refer to Foo via arg. But we can only get to that specialization
15661 : : from Foo's declaration, so we only need to treat Foo as mergable
15662 : : (We'll do structural comparison of TPL<decltype (arg)>).
15663 : :
15664 : : We approximate finding the single cluster entry dep by checking for
15665 : : entities recursively depending on a dep first seen when streaming
15666 : : its own merge key; the first dep we see in such a cluster should be
15667 : : the first one streamed. */
15668 : : unsigned entry_pos = ~0u;
15669 : : unsigned cluster = ~0u;
15670 : 2026274 : for (unsigned ix = 0; ix != order.length (); ix++)
15671 : : {
15672 : 790517 : gcc_checking_assert (order[ix]->is_special ());
15673 : 790517 : bool tight = order[ix]->cluster == cluster;
15674 : 790517 : depset *dep = order[ix]->deps[0];
15675 : 791240 : dump (dumper::MERGE)
15676 : 1443 : && dump ("Mergeable %u is %N%s%s", ix, dep->get_entity (),
15677 : 723 : tight ? " (tight)" : "", dep->is_entry () ? " (entry)" : "");
15678 : 790517 : scc[ix] = dep;
15679 : 790517 : if (tight)
15680 : : {
15681 : 93 : gcc_checking_assert (dep->is_maybe_recursive ());
15682 : 93 : if (dep->is_entry ())
15683 : : {
15684 : : /* There should only be one entry dep in a cluster. */
15685 : 6 : gcc_checking_assert (!scc[entry_pos]->is_entry ());
15686 : 6 : gcc_checking_assert (scc[entry_pos]->is_maybe_recursive ());
15687 : 6 : scc[ix] = scc[entry_pos];
15688 : 6 : scc[entry_pos] = dep;
15689 : : }
15690 : : }
15691 : : else
15692 : : entry_pos = ix;
15693 : 790517 : cluster = order[ix]->cluster;
15694 : : }
15695 : :
15696 : 222884 : dump (dumper::MERGE) && dump ("Ordered %u keys", order.length ());
15697 : 222620 : dump.outdent ();
15698 : 222620 : }
15699 : :
15700 : : /* Reduce graph to SCCS clusters. SCCS will be populated with the
15701 : : depsets in dependency order. Each depset's CLUSTER field contains
15702 : : its cluster number. Each SCC has a unique cluster number, and are
15703 : : contiguous in SCCS. Cluster numbers are otherwise arbitrary. */
15704 : :
15705 : : vec<depset *>
15706 : 225104 : depset::hash::connect ()
15707 : : {
15708 : 225104 : tarjan connector (size ());
15709 : 225104 : vec<depset *> deps;
15710 : 225104 : deps.create (size ());
15711 : 4255490 : for (depset *item : *this)
15712 : : {
15713 : 2015193 : entity_kind kind = item->get_entity_kind ();
15714 : 1896757 : if (kind == EK_BINDING
15715 : 1896757 : || !(kind == EK_REDIRECT
15716 : 1878598 : || item->is_tu_local ()
15717 : 1878473 : || item->is_unreached ()
15718 : 1599425 : || item->is_import ()))
15719 : 1717291 : deps.quick_push (item);
15720 : : }
15721 : :
15722 : : /* Iteration over the hash table is an unspecified ordering. While
15723 : : that has advantages, it causes 2 problems. Firstly repeatable
15724 : : builds are tricky. Secondly creating testcases that check
15725 : : dependencies are correct by making sure a bad ordering would
15726 : : happen if that was wrong. */
15727 : 1220103 : deps.qsort (depset_cmp);
15728 : :
15729 : 1942395 : while (deps.length ())
15730 : : {
15731 : 1717291 : depset *v = deps.pop ();
15732 : 1717291 : dump (dumper::CLUSTER) &&
15733 : 1800 : (v->is_binding ()
15734 : 210 : ? dump ("Connecting binding %P", v->get_entity (), v->get_name ())
15735 : 1590 : : dump ("Connecting %s %s %C:%N",
15736 : 1590 : is_key_order () ? "key-order"
15737 : 870 : : !v->has_defn () ? "declaration" : "definition",
15738 : 1590 : v->entity_kind_name (), TREE_CODE (v->get_entity ()),
15739 : : v->get_entity ()));
15740 : 1717291 : if (!v->cluster)
15741 : 769895 : connector.connect (v);
15742 : : }
15743 : :
15744 : 225104 : deps.release ();
15745 : 450208 : return connector.result;
15746 : 225104 : }
15747 : :
15748 : : /* Initialize location spans. */
15749 : :
15750 : : void
15751 : 4489 : loc_spans::init (const line_maps *lmaps, const line_map_ordinary *map)
15752 : : {
15753 : 4489 : gcc_checking_assert (!init_p ());
15754 : 4489 : spans = new vec<span> ();
15755 : 4489 : spans->reserve (20);
15756 : :
15757 : 4489 : span interval;
15758 : 4489 : interval.ordinary.first = 0;
15759 : 4489 : interval.macro.second = MAX_LOCATION_T + 1;
15760 : 4489 : interval.ordinary_delta = interval.macro_delta = 0;
15761 : :
15762 : : /* A span for reserved fixed locs. */
15763 : 4489 : interval.ordinary.second
15764 : 4489 : = MAP_START_LOCATION (LINEMAPS_ORDINARY_MAP_AT (line_table, 0));
15765 : 4489 : interval.macro.first = interval.macro.second;
15766 : 4489 : dump (dumper::LOCATION)
15767 : 42 : && dump ("Fixed span %u ordinary:[%K,%K) macro:[%K,%K)", spans->length (),
15768 : : interval.ordinary.first, interval.ordinary.second,
15769 : : interval.macro.first, interval.macro.second);
15770 : 4489 : spans->quick_push (interval);
15771 : :
15772 : : /* A span for command line & forced headers. */
15773 : 4489 : interval.ordinary.first = interval.ordinary.second;
15774 : 4489 : interval.macro.second = interval.macro.first;
15775 : 4489 : if (map)
15776 : : {
15777 : 4483 : interval.ordinary.second = map->start_location;
15778 : 4483 : interval.macro.first = LINEMAPS_MACRO_LOWEST_LOCATION (lmaps);
15779 : : }
15780 : 4489 : dump (dumper::LOCATION)
15781 : 21 : && dump ("Pre span %u ordinary:[%K,%K) macro:[%K,%K)", spans->length (),
15782 : : interval.ordinary.first, interval.ordinary.second,
15783 : : interval.macro.first, interval.macro.second);
15784 : 4489 : spans->quick_push (interval);
15785 : :
15786 : : /* Start an interval for the main file. */
15787 : 4489 : interval.ordinary.first = interval.ordinary.second;
15788 : 4489 : interval.macro.second = interval.macro.first;
15789 : 4489 : dump (dumper::LOCATION)
15790 : 21 : && dump ("Main span %u ordinary:[%K,*) macro:[*,%K)", spans->length (),
15791 : : interval.ordinary.first, interval.macro.second);
15792 : 4489 : spans->quick_push (interval);
15793 : 4489 : }
15794 : :
15795 : : /* Reopen the span, if we want the about-to-be-inserted set of maps to
15796 : : be propagated in our own location table. I.e. we are the primary
15797 : : interface and we're importing a partition. */
15798 : :
15799 : : bool
15800 : 2785 : loc_spans::maybe_propagate (module_state *import, location_t hwm)
15801 : : {
15802 : 2785 : bool opened = (module_interface_p () && !module_partition_p ()
15803 : 3157 : && import->is_partition ());
15804 : 148 : if (opened)
15805 : 148 : open (hwm);
15806 : 2785 : return opened;
15807 : : }
15808 : :
15809 : : /* Open a new linemap interval. The just-created ordinary map is the
15810 : : first map of the interval. */
15811 : :
15812 : : void
15813 : 1013 : loc_spans::open (location_t hwm)
15814 : : {
15815 : 1013 : span interval;
15816 : 1013 : interval.ordinary.first = interval.ordinary.second = hwm;
15817 : 2026 : interval.macro.first = interval.macro.second
15818 : 1013 : = LINEMAPS_MACRO_LOWEST_LOCATION (line_table);
15819 : 1013 : interval.ordinary_delta = interval.macro_delta = 0;
15820 : 1013 : dump (dumper::LOCATION)
15821 : 0 : && dump ("Opening span %u ordinary:[%K,... macro:...,%K)",
15822 : 0 : spans->length (), interval.ordinary.first,
15823 : : interval.macro.second);
15824 : 1013 : if (spans->length ())
15825 : : {
15826 : : /* No overlapping! */
15827 : 1013 : auto &last = spans->last ();
15828 : 1013 : gcc_checking_assert (interval.ordinary.first >= last.ordinary.second);
15829 : 1013 : gcc_checking_assert (interval.macro.second <= last.macro.first);
15830 : : }
15831 : 1013 : spans->safe_push (interval);
15832 : 1013 : }
15833 : :
15834 : : /* Close out the current linemap interval. The last maps are within
15835 : : the interval. */
15836 : :
15837 : : void
15838 : 5499 : loc_spans::close ()
15839 : : {
15840 : 5499 : span &interval = spans->last ();
15841 : :
15842 : 5499 : interval.ordinary.second
15843 : 5499 : = ((line_table->highest_location
15844 : 5499 : + (loc_one << line_table->default_range_bits))
15845 : 5499 : & ~((loc_one << line_table->default_range_bits) - 1));
15846 : 5499 : interval.macro.first = LINEMAPS_MACRO_LOWEST_LOCATION (line_table);
15847 : 5499 : dump (dumper::LOCATION)
15848 : 21 : && dump ("Closing span %u ordinary:[%K,%K) macro:[%K,%K)",
15849 : 21 : spans->length () - 1,
15850 : : interval.ordinary.first,interval.ordinary.second,
15851 : : interval.macro.first, interval.macro.second);
15852 : 5499 : }
15853 : :
15854 : : /* Given an ordinary location LOC, return the lmap_interval it resides
15855 : : in. NULL if it is not in an interval. */
15856 : :
15857 : : const loc_spans::span *
15858 : 24127462 : loc_spans::ordinary (location_t loc)
15859 : : {
15860 : 24127462 : unsigned len = spans->length ();
15861 : 48124429 : unsigned pos = 0;
15862 : 48131495 : while (len)
15863 : : {
15864 : 48123922 : unsigned half = len / 2;
15865 : 48123922 : const span &probe = (*spans)[pos + half];
15866 : 48123922 : if (loc < probe.ordinary.first)
15867 : : len = half;
15868 : 48116856 : else if (loc < probe.ordinary.second)
15869 : : return &probe;
15870 : : else
15871 : : {
15872 : 23996967 : pos += half + 1;
15873 : 23996967 : len = len - (half + 1);
15874 : : }
15875 : : }
15876 : : return NULL;
15877 : : }
15878 : :
15879 : : /* Likewise, given a macro location LOC, return the lmap interval it
15880 : : resides in. */
15881 : :
15882 : : const loc_spans::span *
15883 : 2093734 : loc_spans::macro (location_t loc)
15884 : : {
15885 : 2093734 : unsigned len = spans->length ();
15886 : 4184410 : unsigned pos = 0;
15887 : 4184410 : while (len)
15888 : : {
15889 : 4184398 : unsigned half = len / 2;
15890 : 4184398 : const span &probe = (*spans)[pos + half];
15891 : 4184398 : if (loc >= probe.macro.second)
15892 : : len = half;
15893 : 4184398 : else if (loc >= probe.macro.first)
15894 : : return &probe;
15895 : : else
15896 : : {
15897 : 2090676 : pos += half + 1;
15898 : 2090676 : len = len - (half + 1);
15899 : : }
15900 : : }
15901 : : return NULL;
15902 : : }
15903 : :
15904 : : /* Return the ordinary location closest to FROM. */
15905 : :
15906 : : static location_t
15907 : 6304 : ordinary_loc_of (line_maps *lmaps, location_t from)
15908 : : {
15909 : 12611 : while (!IS_ORDINARY_LOC (from))
15910 : : {
15911 : 3 : if (IS_ADHOC_LOC (from))
15912 : 3 : from = get_location_from_adhoc_loc (lmaps, from);
15913 : 3 : if (from >= LINEMAPS_MACRO_LOWEST_LOCATION (lmaps))
15914 : : {
15915 : : /* Find the ordinary location nearest FROM. */
15916 : 0 : const line_map *map = linemap_lookup (lmaps, from);
15917 : 0 : const line_map_macro *mac_map = linemap_check_macro (map);
15918 : 0 : from = mac_map->get_expansion_point_location ();
15919 : : }
15920 : : }
15921 : 6304 : return from;
15922 : : }
15923 : :
15924 : : static module_state **
15925 : 11144 : get_module_slot (tree name, module_state *parent, bool partition, bool insert)
15926 : : {
15927 : 11144 : module_state_hash::compare_type ct (name, uintptr_t (parent) | partition);
15928 : 11144 : hashval_t hv = module_state_hash::hash (ct);
15929 : :
15930 : 11144 : return modules_hash->find_slot_with_hash (ct, hv, insert ? INSERT : NO_INSERT);
15931 : : }
15932 : :
15933 : : static module_state *
15934 : 89192 : get_primary (module_state *parent)
15935 : : {
15936 : 92710 : while (parent->is_partition ())
15937 : 464 : parent = parent->parent;
15938 : :
15939 : 92246 : if (!parent->name)
15940 : : // Implementation unit has null name
15941 : 47924 : parent = parent->parent;
15942 : :
15943 : 88541 : return parent;
15944 : : }
15945 : :
15946 : : /* Find or create module NAME & PARENT in the hash table. */
15947 : :
15948 : : module_state *
15949 : 11144 : get_module (tree name, module_state *parent, bool partition)
15950 : : {
15951 : : /* We might be given an empty NAME if preprocessing fails to handle
15952 : : a header-name token. */
15953 : 11144 : if (name && TREE_CODE (name) == STRING_CST
15954 : 13909 : && TREE_STRING_LENGTH (name) == 0)
15955 : : return nullptr;
15956 : :
15957 : 11144 : if (partition)
15958 : : {
15959 : 910 : if (!parent)
15960 : 193 : parent = get_primary (this_module ());
15961 : :
15962 : 910 : if (!parent->is_partition () && !parent->flatname)
15963 : 214 : parent->set_flatname ();
15964 : : }
15965 : :
15966 : 11144 : module_state **slot = get_module_slot (name, parent, partition, true);
15967 : 11144 : module_state *state = *slot;
15968 : 11144 : if (!state)
15969 : : {
15970 : 6067 : state = (new (ggc_alloc<module_state> ())
15971 : 6067 : module_state (name, parent, partition));
15972 : 6067 : *slot = state;
15973 : : }
15974 : : return state;
15975 : : }
15976 : :
15977 : : /* Process string name PTR into a module_state. */
15978 : :
15979 : : static module_state *
15980 : 394 : get_module (const char *ptr)
15981 : : {
15982 : : /* On DOS based file systems, there is an ambiguity with A:B which can be
15983 : : interpreted as a module Module:Partition or Drive:PATH. Interpret strings
15984 : : which clearly starts as pathnames as header-names and everything else is
15985 : : treated as a (possibly malformed) named moduled. */
15986 : 394 : if (IS_DIR_SEPARATOR (ptr[ptr[0] == '.']) // ./FOO or /FOO
15987 : : #if HAVE_DOS_BASED_FILE_SYSTEM
15988 : : || (HAS_DRIVE_SPEC (ptr) && IS_DIR_SEPARATOR (ptr[2])) // A:/FOO
15989 : : #endif
15990 : : || false)
15991 : : /* A header name. */
15992 : 107 : return get_module (build_string (strlen (ptr), ptr));
15993 : :
15994 : : bool partition = false;
15995 : : module_state *mod = NULL;
15996 : :
15997 : 1135 : for (const char *probe = ptr;; probe++)
15998 : 1422 : if (!*probe || *probe == '.' || *probe == ':')
15999 : : {
16000 : 362 : if (probe == ptr)
16001 : : return NULL;
16002 : :
16003 : 362 : mod = get_module (get_identifier_with_length (ptr, probe - ptr),
16004 : : mod, partition);
16005 : 362 : ptr = probe;
16006 : 362 : if (*ptr == ':')
16007 : : {
16008 : 72 : if (partition)
16009 : : return NULL;
16010 : : partition = true;
16011 : : }
16012 : :
16013 : 362 : if (!*ptr++)
16014 : : break;
16015 : : }
16016 : 1060 : else if (!(ISALPHA (*probe) || *probe == '_'
16017 : 18 : || (probe != ptr && ISDIGIT (*probe))))
16018 : : return NULL;
16019 : :
16020 : : return mod;
16021 : : }
16022 : :
16023 : : /* Create a new mapper connecting to OPTION. */
16024 : :
16025 : : module_client *
16026 : 4489 : make_mapper (location_t loc, class mkdeps *deps)
16027 : : {
16028 : 4489 : timevar_start (TV_MODULE_MAPPER);
16029 : 4489 : const char *option = module_mapper_name;
16030 : 4489 : if (!option)
16031 : 4447 : option = getenv ("CXX_MODULE_MAPPER");
16032 : :
16033 : 8978 : mapper = module_client::open_module_client
16034 : 4489 : (loc, option, deps, &set_cmi_repo,
16035 : 4489 : (save_decoded_options[0].opt_index == OPT_SPECIAL_program_name)
16036 : 4489 : && save_decoded_options[0].arg != progname
16037 : : ? save_decoded_options[0].arg : nullptr);
16038 : :
16039 : 4489 : timevar_stop (TV_MODULE_MAPPER);
16040 : :
16041 : 4489 : return mapper;
16042 : : }
16043 : :
16044 : : static unsigned lazy_snum;
16045 : :
16046 : : static bool
16047 : 7594 : recursive_lazy (unsigned snum = ~0u)
16048 : : {
16049 : 7594 : if (lazy_snum)
16050 : : {
16051 : 0 : error_at (input_location, "recursive lazy load");
16052 : 0 : return true;
16053 : : }
16054 : :
16055 : 7594 : lazy_snum = snum;
16056 : 7594 : return false;
16057 : : }
16058 : :
16059 : : /* If THIS has an interface dependency on itself, report an error and
16060 : : return false. */
16061 : :
16062 : : bool
16063 : 2639 : module_state::check_circular_import (location_t from)
16064 : : {
16065 : 2639 : if (this == this_module ())
16066 : : {
16067 : : /* Cannot import the current module. */
16068 : 9 : auto_diagnostic_group d;
16069 : 9 : error_at (from, "module %qs depends on itself", get_flatname ());
16070 : 9 : if (!header_module_p ())
16071 : 6 : inform (loc, "module %qs declared here", get_flatname ());
16072 : 9 : return false;
16073 : 9 : }
16074 : : return true;
16075 : : }
16076 : :
16077 : : /* Module name substitutions. */
16078 : : static vec<module_state *,va_heap> substs;
16079 : :
16080 : : void
16081 : 7506 : module_state::mangle (bool include_partition)
16082 : : {
16083 : 7506 : if (subst)
16084 : 192 : mangle_module_substitution (subst);
16085 : : else
16086 : : {
16087 : 7314 : if (parent)
16088 : 714 : parent->mangle (include_partition);
16089 : 7314 : if (include_partition || !is_partition ())
16090 : : {
16091 : : // Partitions are significant for global initializer
16092 : : // functions
16093 : 7225 : bool partition = is_partition () && !parent->is_partition ();
16094 : 7225 : subst = mangle_module_component (name, partition);
16095 : 7225 : substs.safe_push (this);
16096 : : }
16097 : : }
16098 : 7506 : }
16099 : :
16100 : : void
16101 : 6792 : mangle_module (int mod, bool include_partition)
16102 : : {
16103 : 6792 : module_state *imp = (*modules)[mod];
16104 : :
16105 : 6792 : gcc_checking_assert (!imp->is_header ());
16106 : :
16107 : 6792 : if (!imp->name)
16108 : : /* Set when importing the primary module interface. */
16109 : 234 : imp = imp->parent;
16110 : :
16111 : 6792 : imp->mangle (include_partition);
16112 : 6792 : }
16113 : :
16114 : : /* Clean up substitutions. */
16115 : : void
16116 : 6557 : mangle_module_fini ()
16117 : : {
16118 : 13782 : while (substs.length ())
16119 : 7225 : substs.pop ()->subst = 0;
16120 : 6557 : }
16121 : :
16122 : : /* Announce WHAT about the module. */
16123 : :
16124 : : void
16125 : 11536 : module_state::announce (const char *what) const
16126 : : {
16127 : 11536 : if (noisy_p ())
16128 : : {
16129 : 0 : fprintf (stderr, " %s:%s", what, get_flatname ());
16130 : 0 : fflush (stderr);
16131 : : }
16132 : 11536 : }
16133 : :
16134 : : /* A human-readable README section. The contents of this section to
16135 : : not contribute to the CRC, so the contents can change per
16136 : : compilation. That allows us to embed CWD, hostname, build time and
16137 : : what not. It is a STRTAB that may be extracted with:
16138 : : readelf -pgnu.c++.README $(module).gcm */
16139 : :
16140 : : void
16141 : 2484 : module_state::write_readme (elf_out *to, cpp_reader *reader, const char *dialect)
16142 : : {
16143 : 2484 : bytes_out readme (to);
16144 : :
16145 : 2484 : readme.begin (false);
16146 : :
16147 : 2484 : readme.printf ("GNU C++ %s",
16148 : 2484 : is_header () ? "header unit"
16149 : 1617 : : !is_partition () ? "primary interface"
16150 : 166 : : is_interface () ? "interface partition"
16151 : : : "internal partition");
16152 : :
16153 : : /* Compiler's version. */
16154 : 2484 : readme.printf ("compiler: %s", version_string);
16155 : :
16156 : : /* Module format version. */
16157 : 2484 : verstr_t string;
16158 : 2484 : version2string (MODULE_VERSION, string);
16159 : 2484 : readme.printf ("version: %s", string);
16160 : :
16161 : : /* Module information. */
16162 : 2484 : readme.printf ("module: %s", get_flatname ());
16163 : 2484 : readme.printf ("source: %s", main_input_filename);
16164 : 2484 : readme.printf ("dialect: %s", dialect);
16165 : 2484 : if (extensions)
16166 : 24 : readme.printf ("extensions: %s%s%s",
16167 : : extensions & SE_OPENMP ? "-fopenmp"
16168 : 6 : : extensions & SE_OPENMP_SIMD ? "-fopenmp-simd" : "",
16169 : : (extensions & SE_OPENACC)
16170 : 3 : && (extensions & (SE_OPENMP | SE_OPENMP_SIMD))
16171 : : ? " " : "",
16172 : 9 : extensions & SE_OPENACC ? "-fopenacc" : "");
16173 : :
16174 : : /* The following fields could be expected to change between
16175 : : otherwise identical compilations. Consider a distributed build
16176 : : system. We should have a way of overriding that. */
16177 : 2484 : if (char *cwd = getcwd (NULL, 0))
16178 : : {
16179 : 2484 : readme.printf ("cwd: %s", cwd);
16180 : 2484 : free (cwd);
16181 : : }
16182 : 4968 : readme.printf ("repository: %s", cmi_repo ? cmi_repo : ".");
16183 : : #if NETWORKING
16184 : : {
16185 : : char hostname[64];
16186 : : if (!gethostname (hostname, sizeof (hostname)))
16187 : : readme.printf ("host: %s", hostname);
16188 : : }
16189 : : #endif
16190 : 2484 : {
16191 : : /* This of course will change! */
16192 : 2484 : time_t stampy;
16193 : 2484 : auto kind = cpp_get_date (reader, &stampy);
16194 : 2484 : if (kind != CPP_time_kind::UNKNOWN)
16195 : : {
16196 : 2484 : struct tm *time;
16197 : :
16198 : 2484 : time = gmtime (&stampy);
16199 : 2484 : readme.print_time ("build", time, "UTC");
16200 : :
16201 : 2484 : if (kind == CPP_time_kind::DYNAMIC)
16202 : : {
16203 : 2484 : time = localtime (&stampy);
16204 : 2484 : readme.print_time ("local", time,
16205 : : #if defined (__USE_MISC) || defined (__USE_BSD) /* Is there a better way? */
16206 : : time->tm_zone
16207 : : #else
16208 : : ""
16209 : : #endif
16210 : : );
16211 : : }
16212 : : }
16213 : : }
16214 : :
16215 : : /* Its direct imports. */
16216 : 3070 : for (unsigned ix = 1; ix < modules->length (); ix++)
16217 : : {
16218 : 586 : module_state *state = (*modules)[ix];
16219 : :
16220 : 586 : if (state->is_direct ())
16221 : 867 : readme.printf ("%s: %s %s", state->exported_p ? "export" : "import",
16222 : : state->get_flatname (), state->filename);
16223 : : }
16224 : :
16225 : 2484 : readme.end (to, to->name (MOD_SNAME_PFX ".README"), NULL);
16226 : 2484 : }
16227 : :
16228 : : /* Sort environment var names in reverse order. */
16229 : :
16230 : : static int
16231 : 0 : env_var_cmp (const void *a_, const void *b_)
16232 : : {
16233 : 0 : const unsigned char *a = *(const unsigned char *const *)a_;
16234 : 0 : const unsigned char *b = *(const unsigned char *const *)b_;
16235 : :
16236 : 0 : for (unsigned ix = 0; ; ix++)
16237 : : {
16238 : 0 : bool a_end = !a[ix] || a[ix] == '=';
16239 : 0 : if (a[ix] == b[ix])
16240 : : {
16241 : 0 : if (a_end)
16242 : : break;
16243 : : }
16244 : : else
16245 : : {
16246 : 0 : bool b_end = !b[ix] || b[ix] == '=';
16247 : :
16248 : 0 : if (!a_end && !b_end)
16249 : 0 : return a[ix] < b[ix] ? +1 : -1;
16250 : 0 : if (a_end && b_end)
16251 : : break;
16252 : 0 : return a_end ? +1 : -1;
16253 : : }
16254 : 0 : }
16255 : :
16256 : : return 0;
16257 : : }
16258 : :
16259 : : /* Write the environment. It is a STRTAB that may be extracted with:
16260 : : readelf -pgnu.c++.ENV $(module).gcm */
16261 : :
16262 : : void
16263 : 0 : module_state::write_env (elf_out *to)
16264 : : {
16265 : 0 : vec<const char *> vars;
16266 : 0 : vars.create (20);
16267 : :
16268 : 0 : extern char **environ;
16269 : 0 : while (const char *var = environ[vars.length ()])
16270 : 0 : vars.safe_push (var);
16271 : 0 : vars.qsort (env_var_cmp);
16272 : :
16273 : 0 : bytes_out env (to);
16274 : 0 : env.begin (false);
16275 : 0 : while (vars.length ())
16276 : 0 : env.printf ("%s", vars.pop ());
16277 : 0 : env.end (to, to->name (MOD_SNAME_PFX ".ENV"), NULL);
16278 : :
16279 : 0 : vars.release ();
16280 : 0 : }
16281 : :
16282 : : /* Write the direct or indirect imports.
16283 : : u:N
16284 : : {
16285 : : u:index
16286 : : s:name
16287 : : u32:crc
16288 : : s:filename (direct)
16289 : : u:exported (direct)
16290 : : } imports[N]
16291 : : */
16292 : :
16293 : : void
16294 : 798 : module_state::write_imports (bytes_out &sec, bool direct)
16295 : : {
16296 : 798 : unsigned count = 0;
16297 : :
16298 : 1680 : for (unsigned ix = 1; ix < modules->length (); ix++)
16299 : : {
16300 : 882 : module_state *imp = (*modules)[ix];
16301 : :
16302 : 882 : if (imp->remap && imp->is_direct () == direct)
16303 : 426 : count++;
16304 : : }
16305 : :
16306 : 798 : gcc_assert (!direct || count);
16307 : :
16308 : 798 : sec.u (count);
16309 : 1680 : for (unsigned ix = 1; ix < modules->length (); ix++)
16310 : : {
16311 : 882 : module_state *imp = (*modules)[ix];
16312 : :
16313 : 882 : if (imp->remap && imp->is_direct () == direct)
16314 : : {
16315 : 576 : dump () && dump ("Writing %simport:%u->%u %M (crc=%x)",
16316 : : !direct ? "indirect "
16317 : 75 : : imp->exported_p ? "exported " : "",
16318 : : ix, imp->remap, imp, imp->crc);
16319 : 426 : sec.u (imp->remap);
16320 : 426 : sec.str (imp->get_flatname ());
16321 : 426 : sec.u32 (imp->crc);
16322 : 426 : if (direct)
16323 : : {
16324 : 418 : write_location (sec, imp->imported_from ());
16325 : 418 : sec.str (imp->filename);
16326 : 418 : int exportedness = 0;
16327 : 418 : if (imp->exported_p)
16328 : : exportedness = +1;
16329 : 251 : else if (!imp->is_purview_direct ())
16330 : 12 : exportedness = -1;
16331 : 418 : sec.i (exportedness);
16332 : : }
16333 : : }
16334 : : }
16335 : 798 : }
16336 : :
16337 : : /* READER, LMAPS != NULL == direct imports,
16338 : : == NUL == indirect imports. */
16339 : :
16340 : : unsigned
16341 : 686 : module_state::read_imports (bytes_in &sec, cpp_reader *reader, line_maps *lmaps)
16342 : : {
16343 : 686 : unsigned count = sec.u ();
16344 : 686 : unsigned loaded = 0;
16345 : :
16346 : 1733 : while (count--)
16347 : : {
16348 : 361 : unsigned ix = sec.u ();
16349 : 361 : if (ix >= slurp->remap->length () || !ix || (*slurp->remap)[ix])
16350 : : {
16351 : 0 : sec.set_overrun ();
16352 : 0 : break;
16353 : : }
16354 : :
16355 : 361 : const char *name = sec.str (NULL);
16356 : 361 : module_state *imp = get_module (name);
16357 : 361 : unsigned crc = sec.u32 ();
16358 : 361 : int exportedness = 0;
16359 : :
16360 : : /* If the import is a partition, it must be the same primary
16361 : : module as this TU. */
16362 : 361 : if (imp && imp->is_partition () &&
16363 : : (!named_module_p ()
16364 : 135 : || (get_primary (this_module ()) != get_primary (imp))))
16365 : : imp = NULL;
16366 : :
16367 : 361 : if (!imp)
16368 : 0 : sec.set_overrun ();
16369 : 361 : if (sec.get_overrun ())
16370 : : break;
16371 : :
16372 : 361 : if (lmaps)
16373 : : {
16374 : : /* A direct import, maybe load it. */
16375 : 358 : location_t floc = read_location (sec);
16376 : 358 : const char *fname = sec.str (NULL);
16377 : 358 : exportedness = sec.i ();
16378 : :
16379 : 358 : if (sec.get_overrun ())
16380 : : break;
16381 : :
16382 : 358 : if (!imp->check_circular_import (floc))
16383 : 3 : continue;
16384 : :
16385 : 355 : if (imp->loadedness == ML_NONE)
16386 : : {
16387 : 281 : imp->loc = floc;
16388 : 281 : imp->crc = crc;
16389 : 281 : if (!imp->get_flatname ())
16390 : 239 : imp->set_flatname ();
16391 : :
16392 : 281 : unsigned n = dump.push (imp);
16393 : :
16394 : 281 : if (!imp->filename && fname)
16395 : 239 : imp->filename = xstrdup (fname);
16396 : :
16397 : 281 : if (imp->is_partition ())
16398 : 33 : dump () && dump ("Importing elided partition %M", imp);
16399 : :
16400 : 281 : if (!imp->do_import (reader, false))
16401 : 3 : imp = NULL;
16402 : 281 : dump.pop (n);
16403 : 281 : if (!imp)
16404 : 3 : continue;
16405 : : }
16406 : :
16407 : 352 : if (is_partition ())
16408 : : {
16409 : 63 : if (!imp->is_direct ())
16410 : 33 : imp->directness = MD_PARTITION_DIRECT;
16411 : 63 : if (exportedness > 0)
16412 : 3 : imp->exported_p = true;
16413 : : }
16414 : : }
16415 : : else
16416 : : {
16417 : : /* An indirect import, find it, it should already be here. */
16418 : 3 : if (imp->loadedness == ML_NONE)
16419 : : {
16420 : 0 : error_at (loc, "indirect import %qs is not already loaded", name);
16421 : 0 : continue;
16422 : : }
16423 : : }
16424 : :
16425 : 355 : if (imp->crc != crc)
16426 : 0 : error_at (loc, "import %qs has CRC mismatch", imp->get_flatname ());
16427 : :
16428 : 355 : (*slurp->remap)[ix] = (imp->mod << 1) | (lmaps != NULL);
16429 : :
16430 : 355 : if (lmaps && exportedness >= 0)
16431 : 340 : set_import (imp, bool (exportedness));
16432 : 517 : dump () && dump ("Found %simport:%u %M->%u", !lmaps ? "indirect "
16433 : 81 : : exportedness > 0 ? "exported "
16434 : 48 : : exportedness < 0 ? "gmf" : "", ix, imp,
16435 : : imp->mod);
16436 : 355 : loaded++;
16437 : : }
16438 : :
16439 : 686 : return loaded;
16440 : : }
16441 : :
16442 : : /* Write the import table to MOD_SNAME_PFX.imp. */
16443 : :
16444 : : void
16445 : 399 : module_state::write_imports (elf_out *to, unsigned *crc_ptr)
16446 : : {
16447 : 471 : dump () && dump ("Writing imports");
16448 : 399 : dump.indent ();
16449 : :
16450 : 399 : bytes_out sec (to);
16451 : 399 : sec.begin ();
16452 : :
16453 : 399 : write_imports (sec, true);
16454 : 399 : write_imports (sec, false);
16455 : :
16456 : 399 : sec.end (to, to->name (MOD_SNAME_PFX ".imp"), crc_ptr);
16457 : 399 : dump.outdent ();
16458 : 399 : }
16459 : :
16460 : : bool
16461 : 343 : module_state::read_imports (cpp_reader *reader, line_maps *lmaps)
16462 : : {
16463 : 343 : bytes_in sec;
16464 : :
16465 : 343 : if (!sec.begin (loc, from (), MOD_SNAME_PFX ".imp"))
16466 : : return false;
16467 : :
16468 : 421 : dump () && dump ("Reading %u imports", slurp->remap->length () - 1);
16469 : 343 : dump.indent ();
16470 : :
16471 : : /* Read the imports. */
16472 : 343 : unsigned direct = read_imports (sec, reader, lmaps);
16473 : 343 : unsigned indirect = read_imports (sec, NULL, NULL);
16474 : 343 : if (direct + indirect + 1 != slurp->remap->length ())
16475 : 6 : from ()->set_error (elf::E_BAD_IMPORT);
16476 : :
16477 : 343 : dump.outdent ();
16478 : 343 : if (!sec.end (from ()))
16479 : : return false;
16480 : : return true;
16481 : 343 : }
16482 : :
16483 : : /* We're the primary module interface, but have partitions. Document
16484 : : them so that non-partition module implementation units know which
16485 : : have already been loaded. */
16486 : :
16487 : : void
16488 : 112 : module_state::write_partitions (elf_out *to, unsigned count, unsigned *crc_ptr)
16489 : : {
16490 : 130 : dump () && dump ("Writing %u elided partitions", count);
16491 : 112 : dump.indent ();
16492 : :
16493 : 112 : bytes_out sec (to);
16494 : 112 : sec.begin ();
16495 : :
16496 : 287 : for (unsigned ix = 1; ix != modules->length (); ix++)
16497 : : {
16498 : 175 : module_state *imp = (*modules)[ix];
16499 : 175 : if (imp->is_partition ())
16500 : : {
16501 : 190 : dump () && dump ("Writing elided partition %M (crc=%x)",
16502 : : imp, imp->crc);
16503 : 160 : sec.str (imp->get_flatname ());
16504 : 160 : sec.u32 (imp->crc);
16505 : 311 : write_location (sec, imp->is_direct ()
16506 : 151 : ? imp->imported_from () : UNKNOWN_LOCATION);
16507 : 160 : sec.str (imp->filename);
16508 : : }
16509 : : }
16510 : :
16511 : 112 : sec.end (to, to->name (MOD_SNAME_PFX ".prt"), crc_ptr);
16512 : 112 : dump.outdent ();
16513 : 112 : }
16514 : :
16515 : : bool
16516 : 15 : module_state::read_partitions (unsigned count)
16517 : : {
16518 : 15 : bytes_in sec;
16519 : 15 : if (!sec.begin (loc, from (), MOD_SNAME_PFX ".prt"))
16520 : : return false;
16521 : :
16522 : 18 : dump () && dump ("Reading %u elided partitions", count);
16523 : 15 : dump.indent ();
16524 : :
16525 : 42 : while (count--)
16526 : : {
16527 : 27 : const char *name = sec.str (NULL);
16528 : 27 : unsigned crc = sec.u32 ();
16529 : 27 : location_t floc = read_location (sec);
16530 : 27 : const char *fname = sec.str (NULL);
16531 : :
16532 : 27 : if (sec.get_overrun ())
16533 : : break;
16534 : :
16535 : 33 : dump () && dump ("Reading elided partition %s (crc=%x)", name, crc);
16536 : :
16537 : 27 : module_state *imp = get_module (name);
16538 : 27 : if (!imp /* Partition should be ... */
16539 : 27 : || !imp->is_partition () /* a partition ... */
16540 : 27 : || imp->loadedness != ML_NONE /* that is not yet loaded ... */
16541 : 54 : || get_primary (imp) != this) /* whose primary is this. */
16542 : : {
16543 : 0 : sec.set_overrun ();
16544 : 0 : break;
16545 : : }
16546 : :
16547 : 27 : if (!imp->has_location ())
16548 : 24 : imp->loc = floc;
16549 : 27 : imp->crc = crc;
16550 : 27 : if (!imp->filename && fname[0])
16551 : 24 : imp->filename = xstrdup (fname);
16552 : : }
16553 : :
16554 : 15 : dump.outdent ();
16555 : 15 : if (!sec.end (from ()))
16556 : : return false;
16557 : : return true;
16558 : 15 : }
16559 : :
16560 : : /* Data for config reading and writing. */
16561 : : struct module_state_config {
16562 : : const char *dialect_str = get_dialect ();
16563 : : line_map_uint_t ordinary_locs = 0;
16564 : : line_map_uint_t macro_locs = 0;
16565 : : unsigned num_imports = 0;
16566 : : unsigned num_partitions = 0;
16567 : : unsigned num_entities = 0;
16568 : : unsigned loc_range_bits = 0;
16569 : : unsigned active_init = 0;
16570 : :
16571 : 94835 : static void release ()
16572 : : {
16573 : 94835 : XDELETEVEC (dialect);
16574 : 94835 : dialect = NULL;
16575 : : }
16576 : :
16577 : : private:
16578 : : static const char *get_dialect ();
16579 : : static char *dialect;
16580 : : };
16581 : :
16582 : : char *module_state_config::dialect;
16583 : :
16584 : : /* Generate a string of the significant compilation options.
16585 : : Generally assume the user knows what they're doing, in the same way
16586 : : that object files can be mixed. */
16587 : :
16588 : : const char *
16589 : 5351 : module_state_config::get_dialect ()
16590 : : {
16591 : 5351 : if (!dialect)
16592 : 8586 : dialect = concat (get_cxx_dialect_name (cxx_dialect),
16593 : : /* C++ implies these, only show if disabled. */
16594 : 4293 : flag_exceptions ? "" : "/no-exceptions",
16595 : 4293 : flag_rtti ? "" : "/no-rtti",
16596 : 4293 : flag_new_inheriting_ctors ? "" : "/old-inheriting-ctors",
16597 : : /* C++ 20 implies concepts and coroutines. */
16598 : 1373 : cxx_dialect < cxx20 && flag_concepts ? "/concepts" : "",
16599 : 4293 : (cxx_dialect < cxx20 && flag_coroutines
16600 : : ? "/coroutines" : ""),
16601 : 4293 : flag_module_implicit_inline ? "/implicit-inline" : "",
16602 : 4293 : flag_contracts ? "/contracts" : "",
16603 : : NULL);
16604 : :
16605 : 5351 : return dialect;
16606 : : }
16607 : :
16608 : : /* Contents of a cluster. */
16609 : : enum cluster_tag {
16610 : : ct_decl, /* A decl. */
16611 : : ct_defn, /* A definition. */
16612 : : ct_bind, /* A binding. */
16613 : : ct_hwm
16614 : : };
16615 : :
16616 : : /* Binding modifiers. */
16617 : : enum ct_bind_flags
16618 : : {
16619 : : cbf_export = 0x1, /* An exported decl. */
16620 : : cbf_hidden = 0x2, /* A hidden (friend) decl. */
16621 : : cbf_using = 0x4, /* A using decl. */
16622 : : cbf_internal = 0x8, /* A TU-local decl. */
16623 : : };
16624 : :
16625 : : /* DEP belongs to a different cluster, seed it to prevent
16626 : : unfortunately timed duplicate import. */
16627 : : // FIXME: QOI For inter-cluster references we could just only pick
16628 : : // one entity from an earlier cluster. Even better track
16629 : : // dependencies between earlier clusters
16630 : :
16631 : : void
16632 : 5065190 : module_state::intercluster_seed (trees_out &sec, unsigned index_hwm, depset *dep)
16633 : : {
16634 : 5065190 : if (dep->is_tu_local ())
16635 : : /* We only stream placeholders for TU-local entities anyway. */;
16636 : 5064990 : else if (dep->is_import () || dep->cluster < index_hwm)
16637 : : {
16638 : 2239257 : tree ent = dep->get_entity ();
16639 : 2239257 : if (!TREE_VISITED (ent))
16640 : : {
16641 : 986212 : sec.tree_node (ent);
16642 : 986662 : dump (dumper::CLUSTER)
16643 : 450 : && dump ("Seeded %s %N",
16644 : 450 : dep->is_import () ? "import" : "intercluster", ent);
16645 : : }
16646 : : }
16647 : 5065190 : }
16648 : :
16649 : : /* Write the cluster of depsets in SCC[0-SIZE).
16650 : : dep->section -> section number
16651 : : dep->cluster -> entity number
16652 : : */
16653 : :
16654 : : unsigned
16655 : 222620 : module_state::write_cluster (elf_out *to, depset *scc[], unsigned size,
16656 : : depset::hash &table, unsigned *counts,
16657 : : unsigned *crc_ptr)
16658 : : {
16659 : 223955 : dump () && dump ("Writing section:%u %u depsets", table.section, size);
16660 : 222620 : dump.indent ();
16661 : :
16662 : 222620 : trees_out sec (to, this, table, table.section);
16663 : 222620 : sec.begin ();
16664 : 222620 : unsigned index_lwm = counts[MSC_entities];
16665 : :
16666 : : /* Determine entity numbers, mark for writing. */
16667 : 222929 : dump (dumper::CLUSTER) && dump ("Cluster members:") && (dump.indent (), true);
16668 : 1145130 : for (unsigned ix = 0; ix != size; ix++)
16669 : : {
16670 : 922510 : depset *b = scc[ix];
16671 : :
16672 : 922510 : switch (b->get_entity_kind ())
16673 : : {
16674 : 0 : default:
16675 : 0 : gcc_unreachable ();
16676 : :
16677 : 118436 : case depset::EK_BINDING:
16678 : 118436 : {
16679 : 118436 : dump (dumper::CLUSTER)
16680 : 210 : && dump ("[%u]=%s %P", ix, b->entity_kind_name (),
16681 : : b->get_entity (), b->get_name ());
16682 : 118436 : depset *ns_dep = b->deps[0];
16683 : 118436 : gcc_checking_assert (ns_dep->get_entity_kind ()
16684 : : == depset::EK_NAMESPACE
16685 : : && ns_dep->get_entity () == b->get_entity ());
16686 : 267687 : for (unsigned jx = b->deps.length (); --jx;)
16687 : : {
16688 : 149251 : depset *dep = b->deps[jx];
16689 : : // We could be declaring something that is also a
16690 : : // (merged) import
16691 : 162853 : gcc_checking_assert (dep->is_import ()
16692 : : || TREE_VISITED (dep->get_entity ())
16693 : : || (dep->get_entity_kind ()
16694 : : == depset::EK_USING)
16695 : : || (dep->get_entity_kind ()
16696 : : == depset::EK_TU_LOCAL));
16697 : : }
16698 : : }
16699 : : break;
16700 : :
16701 : 790517 : case depset::EK_DECL:
16702 : 790517 : case depset::EK_SPECIALIZATION:
16703 : 790517 : case depset::EK_PARTIAL:
16704 : 790517 : b->cluster = counts[MSC_entities]++;
16705 : 790517 : sec.mark_declaration (b->get_entity (), b->has_defn ());
16706 : : /* FALLTHROUGH */
16707 : :
16708 : 804074 : case depset::EK_USING:
16709 : 804074 : case depset::EK_TU_LOCAL:
16710 : 1608148 : gcc_checking_assert (!b->is_import ()
16711 : : && !b->is_unreached ());
16712 : 924847 : dump (dumper::CLUSTER)
16713 : 1161 : && dump ("[%u]=%s %s %N", ix, b->entity_kind_name (),
16714 : 729 : b->has_defn () ? "definition" : "declaration",
16715 : : b->get_entity ());
16716 : : break;
16717 : : }
16718 : : }
16719 : 222929 : dump (dumper::CLUSTER) && (dump.outdent (), true);
16720 : :
16721 : : /* Ensure every out-of-cluster decl is referenced before we start
16722 : : streaming. We must do both imports *and* earlier clusters,
16723 : : because the latter could reach into the former and cause a
16724 : : duplicate loop. */
16725 : 222620 : sec.set_importing (+1);
16726 : 1145130 : for (unsigned ix = 0; ix != size; ix++)
16727 : : {
16728 : 922510 : depset *b = scc[ix];
16729 : 10733944 : for (unsigned jx = b->is_special (); jx != b->deps.length (); jx++)
16730 : : {
16731 : 4447004 : depset *dep = b->deps[jx];
16732 : :
16733 : 4447004 : if (dep->is_binding ())
16734 : : {
16735 : 916598 : for (unsigned ix = dep->deps.length (); --ix;)
16736 : : {
16737 : 618186 : depset *bind = dep->deps[ix];
16738 : 618186 : if (bind->get_entity_kind () == depset::EK_USING)
16739 : 40129 : bind = bind->deps[1];
16740 : :
16741 : 618186 : intercluster_seed (sec, index_lwm, bind);
16742 : : }
16743 : : /* Also check the namespace itself. */
16744 : 149206 : dep = dep->deps[0];
16745 : : }
16746 : :
16747 : 4447004 : intercluster_seed (sec, index_lwm, dep);
16748 : : }
16749 : : }
16750 : 222620 : sec.tree_node (NULL_TREE);
16751 : : /* We're done importing now. */
16752 : 222620 : sec.set_importing (-1);
16753 : :
16754 : : /* Write non-definitions. */
16755 : 1145130 : for (unsigned ix = 0; ix != size; ix++)
16756 : : {
16757 : 922510 : depset *b = scc[ix];
16758 : 922510 : tree decl = b->get_entity ();
16759 : 922510 : switch (b->get_entity_kind ())
16760 : : {
16761 : 0 : default:
16762 : 0 : gcc_unreachable ();
16763 : 118436 : break;
16764 : :
16765 : 118436 : case depset::EK_BINDING:
16766 : 118436 : {
16767 : 118436 : gcc_assert (TREE_CODE (decl) == NAMESPACE_DECL);
16768 : 119526 : dump () && dump ("Depset:%u binding %C:%P", ix, TREE_CODE (decl),
16769 : : decl, b->get_name ());
16770 : 118436 : sec.u (ct_bind);
16771 : 118436 : sec.tree_node (decl);
16772 : 118436 : sec.tree_node (b->get_name ());
16773 : :
16774 : : /* Write in reverse order, so reading will see the exports
16775 : : first, thus building the overload chain will be
16776 : : optimized. */
16777 : 386123 : for (unsigned jx = b->deps.length (); --jx;)
16778 : : {
16779 : 149251 : depset *dep = b->deps[jx];
16780 : 149251 : tree bound = dep->get_entity ();
16781 : 149251 : unsigned flags = 0;
16782 : 149251 : if (dep->get_entity_kind () == depset::EK_TU_LOCAL)
16783 : : flags |= cbf_internal;
16784 : 149206 : else if (dep->get_entity_kind () == depset::EK_USING)
16785 : : {
16786 : 13557 : tree ovl = bound;
16787 : 13557 : bound = OVL_FUNCTION (bound);
16788 : 13557 : if (!(TREE_CODE (bound) == CONST_DECL
16789 : 3752 : && UNSCOPED_ENUM_P (TREE_TYPE (bound))
16790 : 3147 : && decl == TYPE_NAME (TREE_TYPE (bound))))
16791 : : /* An unscoped enumerator in its enumeration's
16792 : : scope is not a using. */
16793 : : flags |= cbf_using;
16794 : 13557 : if (OVL_EXPORT_P (ovl))
16795 : 12700 : flags |= cbf_export;
16796 : : }
16797 : : else
16798 : : {
16799 : : /* An implicit typedef must be at one. */
16800 : 135649 : gcc_assert (!DECL_IMPLICIT_TYPEDEF_P (bound) || jx == 1);
16801 : 135649 : if (dep->is_hidden ())
16802 : : flags |= cbf_hidden;
16803 : 130959 : else if (DECL_MODULE_EXPORT_P (STRIP_TEMPLATE (bound)))
16804 : 117610 : flags |= cbf_export;
16805 : : }
16806 : :
16807 : 149251 : gcc_checking_assert (DECL_P (bound));
16808 : :
16809 : 149251 : sec.i (flags);
16810 : 149251 : if (flags & cbf_internal)
16811 : : {
16812 : 45 : sec.tree_node (name_for_tu_local_decl (bound));
16813 : 45 : write_location (sec, DECL_SOURCE_LOCATION (bound));
16814 : : }
16815 : : else
16816 : 149206 : sec.tree_node (bound);
16817 : : }
16818 : :
16819 : : /* Terminate the list. */
16820 : 118436 : sec.i (-1);
16821 : : }
16822 : 118436 : break;
16823 : :
16824 : 13557 : case depset::EK_USING:
16825 : 13557 : case depset::EK_TU_LOCAL:
16826 : 13584 : dump () && dump ("Depset:%u %s %C:%N", ix, b->entity_kind_name (),
16827 : 27 : TREE_CODE (decl), decl);
16828 : : break;
16829 : :
16830 : 790517 : case depset::EK_SPECIALIZATION:
16831 : 790517 : case depset::EK_PARTIAL:
16832 : 790517 : case depset::EK_DECL:
16833 : 792827 : dump () && dump ("Depset:%u %s entity:%u %C:%N", ix,
16834 : : b->entity_kind_name (), b->cluster,
16835 : 2310 : TREE_CODE (decl), decl);
16836 : :
16837 : 790517 : sec.u (ct_decl);
16838 : 790517 : sec.tree_node (decl);
16839 : :
16840 : 924820 : dump () && dump ("Wrote declaration entity:%u %C:%N",
16841 : 2310 : b->cluster, TREE_CODE (decl), decl);
16842 : : break;
16843 : : }
16844 : : }
16845 : :
16846 : : depset *namer = NULL;
16847 : :
16848 : : /* Write out definitions */
16849 : 1145130 : for (unsigned ix = 0; ix != size; ix++)
16850 : : {
16851 : 922510 : depset *b = scc[ix];
16852 : 922510 : tree decl = b->get_entity ();
16853 : 922510 : switch (b->get_entity_kind ())
16854 : : {
16855 : : default:
16856 : : break;
16857 : :
16858 : 790517 : case depset::EK_SPECIALIZATION:
16859 : 790517 : case depset::EK_PARTIAL:
16860 : 790517 : case depset::EK_DECL:
16861 : 790517 : if (!namer)
16862 : 214832 : namer = b;
16863 : :
16864 : 790517 : if (b->has_defn ())
16865 : : {
16866 : 306283 : sec.u (ct_defn);
16867 : 306283 : sec.tree_node (decl);
16868 : 306915 : dump () && dump ("Writing definition %N", decl);
16869 : 306283 : sec.write_definition (decl, b->refs_tu_local ());
16870 : :
16871 : 306283 : if (!namer->has_defn ())
16872 : 922510 : namer = b;
16873 : : }
16874 : : break;
16875 : : }
16876 : : }
16877 : :
16878 : : /* We don't find the section by name. Use depset's decl's name for
16879 : : human friendliness. */
16880 : 222620 : unsigned name = 0;
16881 : 222620 : tree naming_decl = NULL_TREE;
16882 : 222620 : if (namer)
16883 : : {
16884 : 214832 : naming_decl = namer->get_entity ();
16885 : 214832 : if (namer->get_entity_kind () == depset::EK_USING)
16886 : : /* This unfortunately names the section from the target of the
16887 : : using decl. But the name is only a guide, so Do Not Care. */
16888 : 0 : naming_decl = OVL_FUNCTION (naming_decl);
16889 : 214832 : if (DECL_IMPLICIT_TYPEDEF_P (naming_decl))
16890 : : /* Lose any anonymousness. */
16891 : 73882 : naming_decl = TYPE_NAME (TREE_TYPE (naming_decl));
16892 : 214832 : name = to->qualified_name (naming_decl, namer->has_defn ());
16893 : : }
16894 : :
16895 : 222620 : unsigned bytes = sec.pos;
16896 : 222620 : unsigned snum = sec.end (to, name, crc_ptr);
16897 : :
16898 : 1145130 : for (unsigned ix = size; ix--;)
16899 : 922510 : gcc_checking_assert (scc[ix]->section == snum);
16900 : :
16901 : 222620 : dump.outdent ();
16902 : 223955 : dump () && dump ("Wrote section:%u named-by:%N", table.section, naming_decl);
16903 : :
16904 : 222620 : return bytes;
16905 : 222620 : }
16906 : :
16907 : : /* Read a cluster from section SNUM. */
16908 : :
16909 : : bool
16910 : 165245 : module_state::read_cluster (unsigned snum)
16911 : : {
16912 : 165245 : trees_in sec (this);
16913 : :
16914 : 165245 : if (!sec.begin (loc, from (), snum))
16915 : : return false;
16916 : :
16917 : 166394 : dump () && dump ("Reading section:%u", snum);
16918 : 165245 : dump.indent ();
16919 : :
16920 : : /* We care about structural equality. */
16921 : 165245 : comparing_dependent_aliases++;
16922 : :
16923 : : /* First seed the imports. */
16924 : 941932 : while (tree import = sec.tree_node ())
16925 : 1718805 : dump (dumper::CLUSTER) && dump ("Seeded import %N", import);
16926 : :
16927 : 1130832 : while (!sec.get_overrun () && sec.more_p ())
16928 : : {
16929 : 965587 : unsigned ct = sec.u ();
16930 : 965587 : switch (ct)
16931 : : {
16932 : 0 : default:
16933 : 0 : sec.set_overrun ();
16934 : 0 : break;
16935 : :
16936 : 88740 : case ct_bind:
16937 : : /* A set of namespace bindings. */
16938 : 88740 : {
16939 : 88740 : tree ns = sec.tree_node ();
16940 : 88740 : tree name = sec.tree_node ();
16941 : 88740 : tree decls = NULL_TREE;
16942 : 88740 : tree visible = NULL_TREE;
16943 : 88740 : tree internal = NULL_TREE;
16944 : 88740 : tree type = NULL_TREE;
16945 : 88740 : bool dedup = false;
16946 : 88740 : bool global_p = is_header ();
16947 : :
16948 : : /* We rely on the bindings being in the reverse order of
16949 : : the resulting overload set. */
16950 : 204197 : for (;;)
16951 : : {
16952 : 204197 : int flags = sec.i ();
16953 : 204197 : if (flags < 0)
16954 : : break;
16955 : :
16956 : 115457 : if ((flags & cbf_hidden)
16957 : 4060 : && (flags & (cbf_using | cbf_export)))
16958 : 0 : sec.set_overrun ();
16959 : 115457 : if ((flags & cbf_internal)
16960 : 15 : && flags != cbf_internal)
16961 : 0 : sec.set_overrun ();
16962 : :
16963 : 0 : if (flags & cbf_internal)
16964 : : {
16965 : 15 : tree name = sec.tree_node ();
16966 : 15 : location_t loc = read_location (sec);
16967 : 15 : if (sec.get_overrun ())
16968 : : break;
16969 : :
16970 : 15 : tree decl = make_node (TU_LOCAL_ENTITY);
16971 : 15 : TU_LOCAL_ENTITY_NAME (decl) = name;
16972 : 15 : TU_LOCAL_ENTITY_LOCATION (decl) = loc;
16973 : 15 : internal = tree_cons (NULL_TREE, decl, internal);
16974 : 15 : continue;
16975 : 15 : }
16976 : :
16977 : 115442 : tree decl = sec.tree_node ();
16978 : 115442 : if (sec.get_overrun ())
16979 : : break;
16980 : :
16981 : 115442 : if (!global_p)
16982 : : {
16983 : : /* Check if the decl could require GM merging. */
16984 : 4052 : tree orig = get_originating_module_decl (decl);
16985 : 4052 : tree inner = STRIP_TEMPLATE (orig);
16986 : 4052 : if (!DECL_LANG_SPECIFIC (inner)
16987 : 8104 : || !DECL_MODULE_ATTACH_P (inner))
16988 : : global_p = true;
16989 : : }
16990 : :
16991 : 115442 : if (decls && TREE_CODE (decl) == TYPE_DECL)
16992 : : {
16993 : : /* Stat hack. */
16994 : 48 : if (type || !DECL_IMPLICIT_TYPEDEF_P (decl))
16995 : 0 : sec.set_overrun ();
16996 : :
16997 : 48 : if (flags & cbf_using)
16998 : : {
16999 : 3 : type = build_lang_decl_loc (UNKNOWN_LOCATION,
17000 : : USING_DECL,
17001 : 3 : DECL_NAME (decl),
17002 : : NULL_TREE);
17003 : 3 : USING_DECL_DECLS (type) = decl;
17004 : 3 : USING_DECL_SCOPE (type) = CP_DECL_CONTEXT (decl);
17005 : 3 : DECL_CONTEXT (type) = ns;
17006 : :
17007 : 3 : DECL_MODULE_PURVIEW_P (type) = true;
17008 : 3 : if (flags & cbf_export)
17009 : 3 : DECL_MODULE_EXPORT_P (type) = true;
17010 : : }
17011 : : else
17012 : : type = decl;
17013 : : }
17014 : : else
17015 : : {
17016 : 115394 : if ((flags & cbf_using) &&
17017 : 9801 : !DECL_DECLARES_FUNCTION_P (decl))
17018 : : {
17019 : : /* We should only see a single non-function using-decl
17020 : : for a binding; more than that would clash. */
17021 : 3799 : if (decls)
17022 : 0 : sec.set_overrun ();
17023 : :
17024 : : /* FIXME: Propagate the location of the using-decl
17025 : : for use in diagnostics. */
17026 : 3799 : decls = build_lang_decl_loc (UNKNOWN_LOCATION,
17027 : : USING_DECL,
17028 : 3799 : DECL_NAME (decl),
17029 : : NULL_TREE);
17030 : 3799 : USING_DECL_DECLS (decls) = decl;
17031 : : /* We don't currently record the actual scope of the
17032 : : using-declaration, but this approximation should
17033 : : generally be good enough. */
17034 : 3799 : USING_DECL_SCOPE (decls) = CP_DECL_CONTEXT (decl);
17035 : 3799 : DECL_CONTEXT (decls) = ns;
17036 : :
17037 : 3799 : DECL_MODULE_PURVIEW_P (decls) = true;
17038 : 3799 : if (flags & cbf_export)
17039 : 3787 : DECL_MODULE_EXPORT_P (decls) = true;
17040 : : }
17041 : 111595 : else if (decls
17042 : 84926 : || (flags & (cbf_hidden | cbf_using))
17043 : 191506 : || DECL_FUNCTION_TEMPLATE_P (decl))
17044 : : {
17045 : 44165 : decls = ovl_make (decl, decls);
17046 : 44165 : if (flags & cbf_using)
17047 : : {
17048 : 6002 : dedup = true;
17049 : 6002 : OVL_USING_P (decls) = true;
17050 : 6002 : OVL_PURVIEW_P (decls) = true;
17051 : 6002 : if (flags & cbf_export)
17052 : 5987 : OVL_EXPORT_P (decls) = true;
17053 : : }
17054 : :
17055 : 44165 : if (flags & cbf_hidden)
17056 : 4060 : OVL_HIDDEN_P (decls) = true;
17057 : 40105 : else if (dedup)
17058 : 6038 : OVL_DEDUP_P (decls) = true;
17059 : : }
17060 : : else
17061 : : decls = decl;
17062 : :
17063 : 115382 : if (flags & cbf_export
17064 : 115394 : || (!(flags & cbf_hidden)
17065 : 3377 : && (is_module () || is_partition ())))
17066 : : visible = decls;
17067 : : }
17068 : : }
17069 : :
17070 : 88740 : if (!decls && !internal)
17071 : 0 : sec.set_overrun ();
17072 : :
17073 : 88740 : if (sec.get_overrun ())
17074 : : break; /* Bail. */
17075 : :
17076 : 89574 : dump () && dump ("Binding of %P", ns, name);
17077 : 88740 : if (!set_module_binding (ns, name, mod, global_p,
17078 : 88740 : is_module () || is_partition (),
17079 : : decls, type, visible, internal))
17080 : 0 : sec.set_overrun ();
17081 : : }
17082 : : break;
17083 : :
17084 : 634534 : case ct_decl:
17085 : : /* A decl. */
17086 : 634534 : {
17087 : 634534 : tree decl = sec.tree_node ();
17088 : 1768380 : dump () && dump ("Read declaration of %N", decl);
17089 : : }
17090 : : break;
17091 : :
17092 : 242313 : case ct_defn:
17093 : 242313 : {
17094 : 242313 : tree decl = sec.tree_node ();
17095 : 243412 : dump () && dump ("Reading definition of %N", decl);
17096 : 242313 : sec.read_definition (decl);
17097 : : }
17098 : 242313 : break;
17099 : : }
17100 : : }
17101 : :
17102 : : /* When lazy loading is in effect, we can be in the middle of
17103 : : parsing or instantiating a function. Save it away.
17104 : : push_function_context does too much work. */
17105 : 165245 : tree old_cfd = current_function_decl;
17106 : 165245 : struct function *old_cfun = cfun;
17107 : 265672 : for (const post_process_data& pdata : sec.post_process ())
17108 : : {
17109 : 71909 : tree decl = pdata.decl;
17110 : :
17111 : 71909 : bool abstract = false;
17112 : 71909 : if (TREE_CODE (decl) == TEMPLATE_DECL)
17113 : : {
17114 : 39734 : abstract = true;
17115 : 39734 : decl = DECL_TEMPLATE_RESULT (decl);
17116 : : }
17117 : :
17118 : 71909 : current_function_decl = decl;
17119 : 71909 : allocate_struct_function (decl, abstract);
17120 : 71909 : cfun->language = ggc_cleared_alloc<language_function> ();
17121 : 71909 : cfun->language->base.x_stmt_tree.stmts_are_full_exprs_p = 1;
17122 : 71909 : cfun->function_start_locus = pdata.start_locus;
17123 : 71909 : cfun->function_end_locus = pdata.end_locus;
17124 : 71909 : cfun->language->returns_value = pdata.returns_value;
17125 : 71909 : cfun->language->returns_null = pdata.returns_null;
17126 : 71909 : cfun->language->returns_abnormally = pdata.returns_abnormally;
17127 : 71909 : cfun->language->infinite_loop = pdata.infinite_loop;
17128 : :
17129 : : /* Make sure we emit explicit instantiations.
17130 : : FIXME do we want to do this in expand_or_defer_fn instead? */
17131 : 71909 : if (DECL_EXPLICIT_INSTANTIATION (decl)
17132 : 71909 : && !DECL_EXTERNAL (decl))
17133 : 24 : setup_explicit_instantiation_definition_linkage (decl);
17134 : :
17135 : 71909 : if (abstract)
17136 : : ;
17137 : 32175 : else if (DECL_MAYBE_IN_CHARGE_CDTOR_P (decl))
17138 : 5397 : vec_safe_push (post_load_decls, decl);
17139 : : else
17140 : : {
17141 : 26778 : bool aggr = aggregate_value_p (DECL_RESULT (decl), decl);
17142 : : #ifdef PCC_STATIC_STRUCT_RETURN
17143 : : cfun->returns_pcc_struct = aggr;
17144 : : #endif
17145 : 26778 : cfun->returns_struct = aggr;
17146 : 26778 : expand_or_defer_fn (decl);
17147 : :
17148 : : /* If we first see this function after at_eof, it doesn't get
17149 : : note_vague_linkage_fn from tentative_decl_linkage, so the loop in
17150 : : c_parse_final_cleanups won't consider it. But with DECL_COMDAT we
17151 : : can just clear DECL_EXTERNAL and let cgraph decide.
17152 : : FIXME handle this outside module.cc after GCC 15. */
17153 : 2909 : if (at_eof && DECL_COMDAT (decl) && DECL_EXTERNAL (decl)
17154 : 27579 : && DECL_NOT_REALLY_EXTERN (decl))
17155 : 768 : DECL_EXTERNAL (decl) = false;
17156 : : }
17157 : :
17158 : : }
17159 : : /* Look, function.cc's interface to cfun does too much for us, we
17160 : : just need to restore the old value. I do not want to go
17161 : : redesigning that API right now. */
17162 : : #undef cfun
17163 : 165245 : cfun = old_cfun;
17164 : 165245 : current_function_decl = old_cfd;
17165 : 165245 : comparing_dependent_aliases--;
17166 : :
17167 : 165245 : dump.outdent ();
17168 : 166394 : dump () && dump ("Read section:%u", snum);
17169 : :
17170 : 165245 : loaded_clusters++;
17171 : :
17172 : 165245 : if (!sec.end (from ()))
17173 : : return false;
17174 : :
17175 : : return true;
17176 : 165245 : }
17177 : :
17178 : : void
17179 : 120625 : module_state::write_namespace (bytes_out &sec, depset *dep)
17180 : : {
17181 : 120625 : unsigned ns_num = dep->cluster;
17182 : 120625 : unsigned ns_import = 0;
17183 : :
17184 : 120625 : if (dep->is_import ())
17185 : 0 : ns_import = dep->section;
17186 : 120625 : else if (dep->get_entity () != global_namespace)
17187 : 75128 : ns_num++;
17188 : :
17189 : 120625 : sec.u (ns_import);
17190 : 120625 : sec.u (ns_num);
17191 : 120625 : }
17192 : :
17193 : : tree
17194 : 145478 : module_state::read_namespace (bytes_in &sec)
17195 : : {
17196 : 145478 : unsigned ns_import = sec.u ();
17197 : 145478 : unsigned ns_num = sec.u ();
17198 : 145478 : tree ns = NULL_TREE;
17199 : :
17200 : 145478 : if (ns_import || ns_num)
17201 : : {
17202 : 89547 : if (!ns_import)
17203 : 89547 : ns_num--;
17204 : :
17205 : 89547 : if (unsigned origin = slurp->remap_module (ns_import))
17206 : : {
17207 : 89547 : module_state *from = (*modules)[origin];
17208 : 89547 : if (ns_num < from->entity_num)
17209 : : {
17210 : 89547 : binding_slot &slot = (*entity_ary)[from->entity_lwm + ns_num];
17211 : :
17212 : 89547 : if (!slot.is_lazy ())
17213 : 89547 : ns = slot;
17214 : : }
17215 : : }
17216 : : else
17217 : 0 : sec.set_overrun ();
17218 : : }
17219 : : else
17220 : 55931 : ns = global_namespace;
17221 : :
17222 : 145478 : return ns;
17223 : : }
17224 : :
17225 : : /* SPACES is a sorted vector of namespaces. Write out the namespaces
17226 : : to MOD_SNAME_PFX.nms section. */
17227 : :
17228 : : void
17229 : 506 : module_state::write_namespaces (elf_out *to, vec<depset *> spaces,
17230 : : unsigned num, unsigned *crc_p)
17231 : : {
17232 : 555 : dump () && dump ("Writing namespaces");
17233 : 506 : dump.indent ();
17234 : :
17235 : 506 : bytes_out sec (to);
17236 : 506 : sec.begin ();
17237 : :
17238 : 2525 : for (unsigned ix = 0; ix != num; ix++)
17239 : : {
17240 : 2019 : depset *b = spaces[ix];
17241 : 2019 : tree ns = b->get_entity ();
17242 : :
17243 : : /* This could be an anonymous namespace even for a named module,
17244 : : since we can still emit no-linkage decls. */
17245 : 2019 : gcc_checking_assert (TREE_CODE (ns) == NAMESPACE_DECL);
17246 : :
17247 : 2019 : unsigned flags = 0;
17248 : 2019 : if (TREE_PUBLIC (ns))
17249 : 1970 : flags |= 1;
17250 : 2019 : if (DECL_NAMESPACE_INLINE_P (ns))
17251 : 372 : flags |= 2;
17252 : 2019 : if (DECL_MODULE_PURVIEW_P (ns))
17253 : 1671 : flags |= 4;
17254 : 2019 : if (DECL_MODULE_EXPORT_P (ns))
17255 : 1505 : flags |= 8;
17256 : :
17257 : 2131 : dump () && dump ("Writing namespace:%u %N%s%s%s%s",
17258 : : b->cluster, ns,
17259 : 112 : flags & 1 ? ", public" : "",
17260 : 112 : flags & 2 ? ", inline" : "",
17261 : 112 : flags & 4 ? ", purview" : "",
17262 : 112 : flags & 8 ? ", export" : "");
17263 : 2019 : sec.u (b->cluster);
17264 : 2019 : sec.u (to->name (DECL_NAME (ns)));
17265 : 2019 : write_namespace (sec, b->deps[0]);
17266 : :
17267 : 2019 : sec.u (flags);
17268 : 2019 : write_location (sec, DECL_SOURCE_LOCATION (ns));
17269 : :
17270 : 2019 : if (DECL_NAMESPACE_INLINE_P (ns))
17271 : : {
17272 : 372 : if (tree attr = lookup_attribute ("abi_tag", DECL_ATTRIBUTES (ns)))
17273 : : {
17274 : 110 : tree tags = TREE_VALUE (attr);
17275 : 110 : sec.u (list_length (tags));
17276 : 223 : for (tree tag = tags; tag; tag = TREE_CHAIN (tag))
17277 : 113 : sec.str (TREE_STRING_POINTER (TREE_VALUE (tag)));
17278 : : }
17279 : : else
17280 : 262 : sec.u (0);
17281 : : }
17282 : : }
17283 : :
17284 : 506 : sec.end (to, to->name (MOD_SNAME_PFX ".nms"), crc_p);
17285 : 506 : dump.outdent ();
17286 : 506 : }
17287 : :
17288 : : /* Read the namespace hierarchy from MOD_SNAME_PFX.namespace. Fill in
17289 : : SPACES from that data. */
17290 : :
17291 : : bool
17292 : 531 : module_state::read_namespaces (unsigned num)
17293 : : {
17294 : 531 : bytes_in sec;
17295 : :
17296 : 531 : if (!sec.begin (loc, from (), MOD_SNAME_PFX ".nms"))
17297 : : return false;
17298 : :
17299 : 595 : dump () && dump ("Reading namespaces");
17300 : 531 : dump.indent ();
17301 : :
17302 : 2860 : for (unsigned ix = 0; ix != num; ix++)
17303 : : {
17304 : 2329 : unsigned entity_index = sec.u ();
17305 : 2329 : unsigned name = sec.u ();
17306 : :
17307 : 2329 : tree parent = read_namespace (sec);
17308 : :
17309 : : /* See comment in write_namespace about why not bits. */
17310 : 2329 : unsigned flags = sec.u ();
17311 : 2329 : location_t src_loc = read_location (sec);
17312 : 2329 : unsigned tags_count = (flags & 2) ? sec.u () : 0;
17313 : :
17314 : 2329 : if (entity_index >= entity_num
17315 : 2329 : || !parent
17316 : 2329 : || (flags & 0xc) == 0x8)
17317 : 0 : sec.set_overrun ();
17318 : :
17319 : : tree tags = NULL_TREE;
17320 : 2452 : while (tags_count--)
17321 : : {
17322 : 123 : size_t len;
17323 : 123 : const char *str = sec.str (&len);
17324 : 123 : tags = tree_cons (NULL_TREE, build_string (len + 1, str), tags);
17325 : 123 : tags = nreverse (tags);
17326 : : }
17327 : :
17328 : 2329 : if (sec.get_overrun ())
17329 : : break;
17330 : :
17331 : 4609 : tree id = name ? get_identifier (from ()->name (name)) : NULL_TREE;
17332 : :
17333 : 2499 : dump () && dump ("Read namespace:%u %P%s%s%s%s",
17334 : : entity_index, parent, id,
17335 : 85 : flags & 1 ? ", public" : "",
17336 : : flags & 2 ? ", inline" : "",
17337 : 85 : flags & 4 ? ", purview" : "",
17338 : 85 : flags & 8 ? ", export" : "");
17339 : 2329 : bool visible_p = ((flags & 8)
17340 : 2329 : || ((flags & 1)
17341 : 494 : && (flags & 4)
17342 : 62 : && (is_partition () || is_module ())));
17343 : 2329 : tree inner = add_imported_namespace (parent, id, src_loc, mod,
17344 : : bool (flags & 2), visible_p);
17345 : 2329 : if (!inner)
17346 : : {
17347 : 0 : sec.set_overrun ();
17348 : 0 : break;
17349 : : }
17350 : :
17351 : 2329 : if (is_partition ())
17352 : : {
17353 : 30 : if (flags & 4)
17354 : 24 : DECL_MODULE_PURVIEW_P (inner) = true;
17355 : 30 : if (flags & 8)
17356 : 24 : DECL_MODULE_EXPORT_P (inner) = true;
17357 : : }
17358 : :
17359 : 2329 : if (tags)
17360 : 120 : DECL_ATTRIBUTES (inner)
17361 : 240 : = tree_cons (get_identifier ("abi_tag"), tags, DECL_ATTRIBUTES (inner));
17362 : :
17363 : : /* Install the namespace. */
17364 : 2329 : (*entity_ary)[entity_lwm + entity_index] = inner;
17365 : 2329 : if (DECL_MODULE_IMPORT_P (inner))
17366 : : {
17367 : 0 : bool existed;
17368 : 0 : unsigned *slot = &entity_map->get_or_insert
17369 : 0 : (DECL_UID (inner), &existed);
17370 : 0 : if (existed)
17371 : : /* If it existed, it should match. */
17372 : 0 : gcc_checking_assert (inner == (*entity_ary)[*slot]);
17373 : : else
17374 : 0 : *slot = entity_lwm + entity_index;
17375 : : }
17376 : : }
17377 : :
17378 : 531 : dump.outdent ();
17379 : 531 : if (!sec.end (from ()))
17380 : : return false;
17381 : : return true;
17382 : 531 : }
17383 : :
17384 : : unsigned
17385 : 506 : module_state::write_using_directives (elf_out *to, depset::hash &table,
17386 : : vec<depset *> spaces, unsigned *crc_p)
17387 : : {
17388 : 555 : dump () && dump ("Writing using-directives");
17389 : 506 : dump.indent ();
17390 : :
17391 : 506 : bytes_out sec (to);
17392 : 506 : sec.begin ();
17393 : :
17394 : 506 : unsigned num = 0;
17395 : 3031 : auto emit_one_ns = [&](depset *parent_dep)
17396 : : {
17397 : 2525 : tree parent = parent_dep->get_entity ();
17398 : 2915 : for (auto udir : NAMESPACE_LEVEL (parent)->using_directives)
17399 : : {
17400 : 134 : if (TREE_CODE (udir) != USING_DECL || !DECL_MODULE_EXPORT_P (udir))
17401 : 49 : continue;
17402 : 85 : tree target = USING_DECL_DECLS (udir);
17403 : 85 : depset *target_dep = table.find_dependency (target);
17404 : 85 : gcc_checking_assert (target_dep);
17405 : :
17406 : 97 : dump () && dump ("Writing using-directive in %N for %N",
17407 : : parent, target);
17408 : 85 : write_namespace (sec, parent_dep);
17409 : 85 : write_namespace (sec, target_dep);
17410 : 85 : ++num;
17411 : : }
17412 : 3031 : };
17413 : :
17414 : 506 : emit_one_ns (table.find_dependency (global_namespace));
17415 : 3537 : for (depset *parent_dep : spaces)
17416 : 2019 : emit_one_ns (parent_dep);
17417 : :
17418 : 506 : sec.end (to, to->name (MOD_SNAME_PFX ".udi"), crc_p);
17419 : 506 : dump.outdent ();
17420 : :
17421 : 506 : return num;
17422 : 506 : }
17423 : :
17424 : : bool
17425 : 85 : module_state::read_using_directives (unsigned num)
17426 : : {
17427 : 85 : if (!bitmap_bit_p (this_module ()->imports, mod))
17428 : : {
17429 : 9 : dump () && dump ("Ignoring using-directives because module %M "
17430 : : "is not visible in this TU", this);
17431 : 6 : return true;
17432 : : }
17433 : :
17434 : 79 : bytes_in sec;
17435 : :
17436 : 79 : if (!sec.begin (loc, from (), MOD_SNAME_PFX ".udi"))
17437 : : return false;
17438 : :
17439 : 82 : dump () && dump ("Reading using-directives");
17440 : 79 : dump.indent ();
17441 : :
17442 : 176 : for (unsigned ix = 0; ix != num; ++ix)
17443 : : {
17444 : 97 : tree parent = read_namespace (sec);
17445 : 97 : tree target = read_namespace (sec);
17446 : 97 : if (sec.get_overrun ())
17447 : : break;
17448 : :
17449 : 100 : dump () && dump ("Read using-directive in %N for %N", parent, target);
17450 : 97 : add_using_namespace (parent, target);
17451 : : }
17452 : :
17453 : 79 : dump.outdent ();
17454 : 79 : if (!sec.end (from ()))
17455 : : return false;
17456 : : return true;
17457 : 79 : }
17458 : :
17459 : : /* Write the binding TABLE to MOD_SNAME_PFX.bnd */
17460 : :
17461 : : unsigned
17462 : 2484 : module_state::write_bindings (elf_out *to, vec<depset *> sccs, unsigned *crc_p)
17463 : : {
17464 : 2757 : dump () && dump ("Writing binding table");
17465 : 2484 : dump.indent ();
17466 : :
17467 : 2484 : unsigned num = 0;
17468 : 2484 : bytes_out sec (to);
17469 : 2484 : sec.begin ();
17470 : :
17471 : 1858516 : for (unsigned ix = 0; ix != sccs.length (); ix++)
17472 : : {
17473 : 926774 : depset *b = sccs[ix];
17474 : 926774 : if (b->is_binding ())
17475 : : {
17476 : 118436 : tree ns = b->get_entity ();
17477 : 119526 : dump () && dump ("Bindings %P section:%u", ns, b->get_name (),
17478 : : b->section);
17479 : 118436 : sec.u (to->name (b->get_name ()));
17480 : 118436 : write_namespace (sec, b->deps[0]);
17481 : 118436 : sec.u (b->section);
17482 : 118436 : num++;
17483 : : }
17484 : : }
17485 : :
17486 : 2484 : sec.end (to, to->name (MOD_SNAME_PFX ".bnd"), crc_p);
17487 : 2484 : dump.outdent ();
17488 : :
17489 : 2484 : return num;
17490 : 2484 : }
17491 : :
17492 : : /* Read the binding table from MOD_SNAME_PFX.bind. */
17493 : :
17494 : : bool
17495 : 2665 : module_state::read_bindings (unsigned num, unsigned lwm, unsigned hwm)
17496 : : {
17497 : 2665 : bytes_in sec;
17498 : :
17499 : 2665 : if (!sec.begin (loc, from (), MOD_SNAME_PFX ".bnd"))
17500 : : return false;
17501 : :
17502 : 3155 : dump () && dump ("Reading binding table");
17503 : 2665 : dump.indent ();
17504 : 145620 : for (; !sec.get_overrun () && num--;)
17505 : : {
17506 : 142955 : const char *name = from ()->name (sec.u ());
17507 : 142955 : tree ns = read_namespace (sec);
17508 : 142955 : unsigned snum = sec.u ();
17509 : :
17510 : 142955 : if (!ns || !name || (snum - lwm) >= (hwm - lwm))
17511 : 0 : sec.set_overrun ();
17512 : 142955 : if (!sec.get_overrun ())
17513 : : {
17514 : 142955 : tree id = get_identifier (name);
17515 : 144418 : dump () && dump ("Bindings %P section:%u", ns, id, snum);
17516 : 142955 : if (mod && !import_module_binding (ns, id, mod, snum))
17517 : : break;
17518 : : }
17519 : : }
17520 : :
17521 : 2665 : dump.outdent ();
17522 : 2665 : if (!sec.end (from ()))
17523 : : return false;
17524 : : return true;
17525 : 2665 : }
17526 : :
17527 : : /* Write the entity table to MOD_SNAME_PFX.ent
17528 : :
17529 : : Each entry is a section number. */
17530 : :
17531 : : void
17532 : 2239 : module_state::write_entities (elf_out *to, vec<depset *> depsets,
17533 : : unsigned count, unsigned *crc_p)
17534 : : {
17535 : 2462 : dump () && dump ("Writing entities");
17536 : 2239 : dump.indent ();
17537 : :
17538 : 2239 : bytes_out sec (to);
17539 : 2239 : sec.begin ();
17540 : :
17541 : 2239 : unsigned current = 0;
17542 : 928992 : for (unsigned ix = 0; ix < depsets.length (); ix++)
17543 : : {
17544 : 926753 : depset *d = depsets[ix];
17545 : :
17546 : 1721528 : switch (d->get_entity_kind ())
17547 : : {
17548 : : default:
17549 : : break;
17550 : :
17551 : 4258 : case depset::EK_NAMESPACE:
17552 : 4258 : if (!d->is_import () && d->get_entity () != global_namespace)
17553 : : {
17554 : 2019 : gcc_checking_assert (d->cluster == current);
17555 : 2019 : current++;
17556 : 2019 : sec.u (0);
17557 : : }
17558 : : break;
17559 : :
17560 : 790517 : case depset::EK_DECL:
17561 : 790517 : case depset::EK_SPECIALIZATION:
17562 : 790517 : case depset::EK_PARTIAL:
17563 : 1581034 : gcc_checking_assert (!d->is_unreached ()
17564 : : && !d->is_import ()
17565 : : && d->cluster == current
17566 : : && d->section);
17567 : 790517 : current++;
17568 : 790517 : sec.u (d->section);
17569 : 790517 : break;
17570 : : }
17571 : : }
17572 : 2239 : gcc_assert (count == current);
17573 : 2239 : sec.end (to, to->name (MOD_SNAME_PFX ".ent"), crc_p);
17574 : 2239 : dump.outdent ();
17575 : 2239 : }
17576 : :
17577 : : bool
17578 : 2460 : module_state::read_entities (unsigned count, unsigned lwm, unsigned hwm)
17579 : : {
17580 : 2460 : trees_in sec (this);
17581 : :
17582 : 2460 : if (!sec.begin (loc, from (), MOD_SNAME_PFX ".ent"))
17583 : : return false;
17584 : :
17585 : 2899 : dump () && dump ("Reading entities");
17586 : 2460 : dump.indent ();
17587 : :
17588 : 958893 : for (binding_slot *slot = entity_ary->begin () + entity_lwm; count--; slot++)
17589 : : {
17590 : 956433 : unsigned snum = sec.u ();
17591 : 956433 : if (snum && (snum - lwm) >= (hwm - lwm))
17592 : 0 : sec.set_overrun ();
17593 : 956433 : if (sec.get_overrun ())
17594 : : break;
17595 : :
17596 : 956433 : if (snum)
17597 : 954104 : slot->set_lazy (snum << 2);
17598 : : }
17599 : :
17600 : 2460 : dump.outdent ();
17601 : 2460 : if (!sec.end (from ()))
17602 : : return false;
17603 : : return true;
17604 : 2460 : }
17605 : :
17606 : : /* Write the pending table to MOD_SNAME_PFX.pnd
17607 : :
17608 : : The pending table holds information about clusters that need to be
17609 : : loaded because they contain information about something that is not
17610 : : found by namespace-scope lookup.
17611 : :
17612 : : The three cases are:
17613 : :
17614 : : (a) Template (maybe-partial) specializations that we have
17615 : : instantiated or defined. When an importer needs to instantiate
17616 : : that template, they /must have/ the partial, explicit & extern
17617 : : specializations available. If they have the other specializations
17618 : : available, they'll have less work to do. Thus, when we're about to
17619 : : instantiate FOO, we have to be able to ask 'are there any
17620 : : specialization of FOO in our imports?'.
17621 : :
17622 : : (b) (Maybe-implicit) member functions definitions. A class could
17623 : : be defined in one header, and an inline member defined in a
17624 : : different header (this occurs in the STL). Similarly, like the
17625 : : specialization case, an implicit member function could have been
17626 : : 'instantiated' in one module, and it'd be nice to not have to
17627 : : reinstantiate it in another.
17628 : :
17629 : : (c) Classes completed elsewhere. A class could be declared in one
17630 : : header and defined in another. We need to know to load the class
17631 : : definition before looking in it. It does highlight an issue --
17632 : : there could be an intermediate import between the outermost containing
17633 : : namespace-scope class and the innermost being-defined class. This is
17634 : : actually possible with all of these cases, so be aware -- we're not
17635 : : just talking of one level of import to get to the innermost namespace.
17636 : :
17637 : : This gets complicated fast, it took me multiple attempts to even
17638 : : get something remotely working. Partially because I focussed on
17639 : : optimizing what I think turns out to be a smaller problem, given
17640 : : the known need to do the more general case *anyway*. I document
17641 : : the smaller problem, because it does appear to be the natural way
17642 : : to do it. It's trap!
17643 : :
17644 : : **** THE TRAP
17645 : :
17646 : : Let's refer to the primary template or the containing class as the
17647 : : KEY. And the specialization or member as the PENDING-ENTITY. (To
17648 : : avoid having to say those mouthfuls all the time.)
17649 : :
17650 : : In either case, we have an entity and we need some way of mapping
17651 : : that to a set of entities that need to be loaded before we can
17652 : : proceed with whatever processing of the entity we were going to do.
17653 : :
17654 : : We need to link the key to the pending-entity in some way. Given a
17655 : : key, tell me the pending-entities I need to have loaded. However
17656 : : we tie the key to the pending-entity must not rely on the key being
17657 : : loaded -- that'd defeat the lazy loading scheme.
17658 : :
17659 : : As the key will be an import in we know its entity number (either
17660 : : because we imported it, or we're writing it out too). Thus we can
17661 : : generate a map of key-indices to pending-entities. The
17662 : : pending-entity indices will be into our span of the entity table,
17663 : : and thus allow them to be lazily loaded. The key index will be
17664 : : into another slot of the entity table. Notice that this checking
17665 : : could be expensive, we don't want to iterate over a bunch of
17666 : : pending-entity indices (across multiple imports), every time we're
17667 : : about do to the thing with the key. We need to quickly determine
17668 : : 'definitely nothing needed'.
17669 : :
17670 : : That's almost good enough, except that key indices are not unique
17671 : : in a couple of cases :( Specifically the Global Module or a module
17672 : : partition can result in multiple modules assigning an entity index
17673 : : for the key. The decl-merging on loading will detect that so we
17674 : : only have one Key loaded, and in the entity hash it'll indicate the
17675 : : entity index of first load. Which might be different to how we
17676 : : know it. Notice this is restricted to GM entities or this-module
17677 : : entities. Foreign imports cannot have this.
17678 : :
17679 : : We can simply resolve this in the direction of how this module
17680 : : referred to the key to how the importer knows it. Look in the
17681 : : entity table slot that we nominate, maybe lazy load it, and then
17682 : : lookup the resultant entity in the entity hash to learn how the
17683 : : importer knows it.
17684 : :
17685 : : But we need to go in the other direction :( Given the key, find all
17686 : : the index-aliases of that key. We can partially solve that by
17687 : : adding an alias hash table. Whenever we load a merged decl, add or
17688 : : augment a mapping from the entity (or its entity-index) to the
17689 : : newly-discovered index. Then when we look for pending entities of
17690 : : a key, we also iterate over this aliases this mapping provides.
17691 : :
17692 : : But that requires the alias to be loaded. And that's not
17693 : : necessarily true.
17694 : :
17695 : : *** THE SIMPLER WAY
17696 : :
17697 : : The remaining fixed thing we have is the innermost namespace
17698 : : containing the ultimate namespace-scope container of the key and
17699 : : the name of that container (which might be the key itself). I.e. a
17700 : : namespace-decl/identifier/module tuple. Let's call this the
17701 : : top-key. We'll discover that the module is not important here,
17702 : : because of cross-module possibilities mentioned in case #c above.
17703 : : We can't markup namespace-binding slots. The best we can do is
17704 : : mark the binding vector with 'there's something here', and have
17705 : : another map from namespace/identifier pairs to a vector of pending
17706 : : entity indices.
17707 : :
17708 : : Maintain a pending-entity map. This is keyed by top-key, and
17709 : : maps to a vector of pending-entity indices. On the binding vector
17710 : : have flags saying whether the pending-name-entity map has contents.
17711 : : (We might want to further extend the key to be GM-vs-Partition and
17712 : : specialization-vs-member, but let's not get ahead of ourselves.)
17713 : :
17714 : : For every key-like entity, find the outermost namespace-scope
17715 : : name. Use that to lookup in the pending-entity map and then make
17716 : : sure the specified entities are loaded.
17717 : :
17718 : : An optimization might be to have a flag in each key-entity saying
17719 : : that its top key might be in the entity table. It's not clear to
17720 : : me how to set that flag cheaply -- cheaper than just looking.
17721 : :
17722 : : FIXME: It'd be nice to have a bit in decls to tell us whether to
17723 : : even try this. We can have a 'already done' flag, that we set when
17724 : : we've done KLASS's lazy pendings. When we import a module that
17725 : : registers pendings on the same top-key as KLASS we need to clear
17726 : : the flag. A recursive walk of the top-key clearing the bit will
17727 : : suffice. Plus we only need to recurse on classes that have the bit
17728 : : set. (That means we need to set the bit on parents of KLASS here,
17729 : : don't forget.) However, first: correctness, second: efficiency. */
17730 : :
17731 : : unsigned
17732 : 2484 : module_state::write_pendings (elf_out *to, vec<depset *> depsets,
17733 : : depset::hash &table, unsigned *crc_p)
17734 : : {
17735 : 2757 : dump () && dump ("Writing pending-entities");
17736 : 2484 : dump.indent ();
17737 : :
17738 : 2484 : trees_out sec (to, this, table);
17739 : 2484 : sec.begin ();
17740 : :
17741 : 2484 : unsigned count = 0;
17742 : 2484 : tree cache_ns = NULL_TREE;
17743 : 2484 : tree cache_id = NULL_TREE;
17744 : 2484 : unsigned cache_section = ~0;
17745 : 929258 : for (unsigned ix = 0; ix < depsets.length (); ix++)
17746 : : {
17747 : 926774 : depset *d = depsets[ix];
17748 : :
17749 : 926774 : if (d->is_binding ())
17750 : 573705 : continue;
17751 : :
17752 : 808338 : if (d->is_import ())
17753 : 0 : continue;
17754 : :
17755 : 808338 : if (!d->is_pending_entity ())
17756 : 455269 : continue;
17757 : :
17758 : 353069 : tree key_decl = nullptr;
17759 : 353069 : tree key_ns = find_pending_key (d->get_entity (), &key_decl);
17760 : 353069 : tree key_name = DECL_NAME (key_decl);
17761 : :
17762 : 353069 : if (IDENTIFIER_ANON_P (key_name))
17763 : : {
17764 : 6 : gcc_checking_assert (IDENTIFIER_LAMBDA_P (key_name));
17765 : 12 : if (tree attached = LAMBDA_TYPE_EXTRA_SCOPE (TREE_TYPE (key_decl)))
17766 : 6 : key_name = DECL_NAME (attached);
17767 : : else
17768 : : {
17769 : : /* There's nothing to attach it to. Must
17770 : : always reinstantiate. */
17771 : 0 : dump ()
17772 : 0 : && dump ("Unattached lambda %N[%u] section:%u",
17773 : 0 : d->get_entity_kind () == depset::EK_DECL
17774 : : ? "Member" : "Specialization", d->get_entity (),
17775 : : d->cluster, d->section);
17776 : 0 : continue;
17777 : : }
17778 : : }
17779 : :
17780 : 353069 : char const *also = "";
17781 : 353069 : if (d->section == cache_section
17782 : 223317 : && key_ns == cache_ns
17783 : 223317 : && key_name == cache_id)
17784 : : /* Same section & key as previous, no need to repeat ourselves. */
17785 : : also = "also ";
17786 : : else
17787 : : {
17788 : 168061 : cache_ns = key_ns;
17789 : 168061 : cache_id = key_name;
17790 : 168061 : cache_section = d->section;
17791 : 168061 : gcc_checking_assert (table.find_dependency (cache_ns));
17792 : 168061 : sec.tree_node (cache_ns);
17793 : 168061 : sec.tree_node (cache_id);
17794 : 168061 : sec.u (d->cluster);
17795 : 168061 : count++;
17796 : : }
17797 : 354229 : dump () && dump ("Pending %s %N entity:%u section:%u %skeyed to %P",
17798 : 580 : d->get_entity_kind () == depset::EK_DECL
17799 : : ? "member" : "specialization", d->get_entity (),
17800 : : d->cluster, cache_section, also, cache_ns, cache_id);
17801 : : }
17802 : 2484 : sec.end (to, to->name (MOD_SNAME_PFX ".pnd"), crc_p);
17803 : 2484 : dump.outdent ();
17804 : :
17805 : 2484 : return count;
17806 : 2484 : }
17807 : :
17808 : : bool
17809 : 1409 : module_state::read_pendings (unsigned count)
17810 : : {
17811 : 1409 : trees_in sec (this);
17812 : :
17813 : 1409 : if (!sec.begin (loc, from (), MOD_SNAME_PFX ".pnd"))
17814 : : return false;
17815 : :
17816 : 1707 : dump () && dump ("Reading %u pendings", count);
17817 : 1409 : dump.indent ();
17818 : :
17819 : 201570 : for (unsigned ix = 0; ix != count; ix++)
17820 : : {
17821 : 200161 : pending_key key;
17822 : 200161 : unsigned index;
17823 : :
17824 : 200161 : key.ns = sec.tree_node ();
17825 : 200161 : key.id = sec.tree_node ();
17826 : 200161 : index = sec.u ();
17827 : :
17828 : 200161 : if (!key.ns || !key.id
17829 : 200161 : || !(TREE_CODE (key.ns) == NAMESPACE_DECL
17830 : 200161 : && !DECL_NAMESPACE_ALIAS (key.ns))
17831 : 200161 : || !identifier_p (key.id)
17832 : 400322 : || index >= entity_num)
17833 : 0 : sec.set_overrun ();
17834 : :
17835 : 200161 : if (sec.get_overrun ())
17836 : : break;
17837 : :
17838 : 200900 : dump () && dump ("Pending:%u keyed to %P", index, key.ns, key.id);
17839 : :
17840 : 200161 : index += entity_lwm;
17841 : 200161 : auto &vec = pending_table->get_or_insert (key);
17842 : 200161 : vec.safe_push (index);
17843 : : }
17844 : :
17845 : 1409 : dump.outdent ();
17846 : 1409 : if (!sec.end (from ()))
17847 : : return false;
17848 : : return true;
17849 : 1409 : }
17850 : :
17851 : : /* Read & write locations. */
17852 : : enum loc_kind {
17853 : : LK_ORDINARY,
17854 : : LK_MACRO,
17855 : : LK_IMPORT_ORDINARY,
17856 : : LK_IMPORT_MACRO,
17857 : : LK_ADHOC,
17858 : : LK_RESERVED,
17859 : : };
17860 : :
17861 : : static const module_state *
17862 : 3785 : module_for_ordinary_loc (location_t loc)
17863 : : {
17864 : 3785 : unsigned pos = 0;
17865 : 7570 : unsigned len = ool->length () - pos;
17866 : :
17867 : 3788 : while (len)
17868 : : {
17869 : 3788 : unsigned half = len / 2;
17870 : 3788 : module_state *probe = (*ool)[pos + half];
17871 : 3788 : if (loc < probe->ordinary_locs.first)
17872 : : len = half;
17873 : 3785 : else if (loc < probe->ordinary_locs.first + probe->ordinary_locs.second)
17874 : : return probe;
17875 : : else
17876 : : {
17877 : 0 : pos += half + 1;
17878 : 0 : len = len - (half + 1);
17879 : : }
17880 : : }
17881 : :
17882 : : return nullptr;
17883 : : }
17884 : :
17885 : : static const module_state *
17886 : 6 : module_for_macro_loc (location_t loc)
17887 : : {
17888 : 6 : unsigned pos = 1;
17889 : 6 : unsigned len = modules->length () - pos;
17890 : :
17891 : 6 : while (len)
17892 : : {
17893 : 6 : unsigned half = len / 2;
17894 : 6 : module_state *probe = (*modules)[pos + half];
17895 : 6 : if (loc < probe->macro_locs.first)
17896 : : {
17897 : 0 : pos += half + 1;
17898 : 0 : len = len - (half + 1);
17899 : : }
17900 : 6 : else if (loc >= probe->macro_locs.first + probe->macro_locs.second)
17901 : : len = half;
17902 : : else
17903 : : return probe;
17904 : : }
17905 : :
17906 : : return NULL;
17907 : : }
17908 : :
17909 : : location_t
17910 : 1142 : module_state::imported_from () const
17911 : : {
17912 : 1142 : location_t from = loc;
17913 : 1142 : line_map_ordinary const *fmap
17914 : 1142 : = linemap_check_ordinary (linemap_lookup (line_table, from));
17915 : :
17916 : 1142 : if (MAP_MODULE_P (fmap))
17917 : 1142 : from = linemap_included_from (fmap);
17918 : :
17919 : 1142 : return from;
17920 : : }
17921 : :
17922 : : /* Note that LOC will need writing. This allows us to prune locations
17923 : : that are not needed. */
17924 : :
17925 : : bool
17926 : 16743750 : module_state::note_location (location_t loc)
17927 : : {
17928 : 16743750 : bool added = false;
17929 : 16743750 : if (!macro_loc_table && !ord_loc_table)
17930 : : ;
17931 : 16743750 : else if (loc < RESERVED_LOCATION_COUNT)
17932 : : ;
17933 : 15239819 : else if (IS_ADHOC_LOC (loc))
17934 : : {
17935 : 1842139 : location_t locus = get_location_from_adhoc_loc (line_table, loc);
17936 : 1842139 : note_location (locus);
17937 : 1842139 : source_range range = get_range_from_loc (line_table, loc);
17938 : 1842139 : if (range.m_start != locus)
17939 : 1782943 : note_location (range.m_start);
17940 : 1842139 : note_location (range.m_finish);
17941 : : }
17942 : 13397680 : else if (loc >= LINEMAPS_MACRO_LOWEST_LOCATION (line_table))
17943 : : {
17944 : 1049898 : if (spans.macro (loc))
17945 : : {
17946 : 1049892 : const line_map *map = linemap_lookup (line_table, loc);
17947 : 1049892 : const line_map_macro *mac_map = linemap_check_macro (map);
17948 : 1049892 : hashval_t hv = macro_loc_traits::hash (mac_map);
17949 : 1049892 : macro_loc_info *slot
17950 : 1049892 : = macro_loc_table->find_slot_with_hash (mac_map, hv, INSERT);
17951 : 1049892 : if (!slot->src)
17952 : : {
17953 : 121585 : slot->src = mac_map;
17954 : 121585 : slot->remap = 0;
17955 : : // Expansion locations could themselves be from a
17956 : : // macro, we need to note them all.
17957 : 121585 : note_location (mac_map->m_expansion);
17958 : 121585 : gcc_checking_assert (mac_map->n_tokens);
17959 : 121585 : location_t tloc = UNKNOWN_LOCATION;
17960 : 4298873 : for (unsigned ix = mac_map->n_tokens * 2; ix--;)
17961 : 4177288 : if (mac_map->macro_locations[ix] != tloc)
17962 : : {
17963 : 2212898 : tloc = mac_map->macro_locations[ix];
17964 : 2212898 : note_location (tloc);
17965 : : }
17966 : : added = true;
17967 : : }
17968 : : }
17969 : : }
17970 : 12347782 : else if (IS_ORDINARY_LOC (loc))
17971 : : {
17972 : 12347782 : if (spans.ordinary (loc))
17973 : : {
17974 : 12343994 : const line_map *map = linemap_lookup (line_table, loc);
17975 : 12343994 : const line_map_ordinary *ord_map = linemap_check_ordinary (map);
17976 : 12343994 : ord_loc_info lkup;
17977 : 12343994 : lkup.src = ord_map;
17978 : 12343994 : lkup.span = loc_one << ord_map->m_column_and_range_bits;
17979 : 12343994 : lkup.offset = (loc - MAP_START_LOCATION (ord_map)) & ~(lkup.span - 1);
17980 : 12343994 : lkup.remap = 0;
17981 : 12343994 : ord_loc_info *slot = (ord_loc_table->find_slot_with_hash
17982 : 12343994 : (lkup, ord_loc_traits::hash (lkup), INSERT));
17983 : 12343994 : if (!slot->src)
17984 : : {
17985 : 1495739 : *slot = lkup;
17986 : 1495739 : added = true;
17987 : : }
17988 : : }
17989 : : }
17990 : : else
17991 : 0 : gcc_unreachable ();
17992 : 16743750 : return added;
17993 : : }
17994 : :
17995 : : /* If we're not streaming, record that we need location LOC.
17996 : : Otherwise stream it. */
17997 : :
17998 : : void
17999 : 24854507 : module_state::write_location (bytes_out &sec, location_t loc)
18000 : : {
18001 : 24854507 : if (!sec.streaming_p ())
18002 : : {
18003 : 8694090 : note_location (loc);
18004 : 8694090 : return;
18005 : : }
18006 : :
18007 : 16160417 : if (loc < RESERVED_LOCATION_COUNT)
18008 : : {
18009 : 1540381 : dump (dumper::LOCATION) && dump ("Reserved location %K", loc);
18010 : 1540363 : sec.loc (LK_RESERVED + loc);
18011 : : }
18012 : 14620054 : else if (IS_ADHOC_LOC (loc))
18013 : : {
18014 : 1796541 : dump (dumper::LOCATION) && dump ("Adhoc location");
18015 : 1796538 : sec.u (LK_ADHOC);
18016 : 1796538 : location_t locus = get_location_from_adhoc_loc (line_table, loc);
18017 : 1796538 : write_location (sec, locus);
18018 : 1796538 : source_range range = get_range_from_loc (line_table, loc);
18019 : 1796538 : if (range.m_start == locus)
18020 : : /* Compress. */
18021 : 55598 : range.m_start = UNKNOWN_LOCATION;
18022 : 1796538 : write_location (sec, range.m_start);
18023 : 1796538 : write_location (sec, range.m_finish);
18024 : 1796538 : unsigned discriminator = get_discriminator_from_adhoc_loc (line_table, loc);
18025 : 1796538 : sec.u (discriminator);
18026 : : }
18027 : 12823516 : else if (loc >= LINEMAPS_MACRO_LOWEST_LOCATION (line_table))
18028 : : {
18029 : 1043836 : const macro_loc_info *info = nullptr;
18030 : 1043836 : line_map_uint_t offset = 0;
18031 : 1043836 : if (unsigned hwm = macro_loc_remap->length ())
18032 : : {
18033 : 1043830 : info = macro_loc_remap->begin ();
18034 : 15361318 : while (hwm != 1)
18035 : : {
18036 : 13273658 : unsigned mid = hwm / 2;
18037 : 13273658 : if (MAP_START_LOCATION (info[mid].src) <= loc)
18038 : : {
18039 : 6714713 : info += mid;
18040 : 6714713 : hwm -= mid;
18041 : : }
18042 : : else
18043 : : hwm = mid;
18044 : : }
18045 : 1043830 : offset = loc - MAP_START_LOCATION (info->src);
18046 : 1043830 : if (offset > info->src->n_tokens)
18047 : 0 : info = nullptr;
18048 : : }
18049 : :
18050 : 1043836 : gcc_checking_assert (bool (info) == bool (spans.macro (loc)));
18051 : :
18052 : 1043836 : if (info)
18053 : : {
18054 : 1043830 : offset += info->remap;
18055 : 1043830 : sec.u (LK_MACRO);
18056 : 1043830 : sec.loc (offset);
18057 : 1043830 : dump (dumper::LOCATION)
18058 : 9 : && dump ("Macro location %K output %K", loc, offset);
18059 : : }
18060 : 6 : else if (const module_state *import = module_for_macro_loc (loc))
18061 : : {
18062 : 6 : auto off = loc - import->macro_locs.first;
18063 : 6 : sec.u (LK_IMPORT_MACRO);
18064 : 6 : sec.u (import->remap);
18065 : 6 : sec.loc (off);
18066 : 6 : dump (dumper::LOCATION)
18067 : 0 : && dump ("Imported macro location %K output %u:%K",
18068 : 0 : loc, import->remap, off);
18069 : : }
18070 : : else
18071 : 0 : gcc_unreachable ();
18072 : : }
18073 : 11779680 : else if (IS_ORDINARY_LOC (loc))
18074 : : {
18075 : : /* If we ran out of locations for imported decls, this location could
18076 : : be a module unit's location. In that case, remap the location
18077 : : to be where we imported the module from. */
18078 : 11779680 : if (spans.locations_exhausted_p () || CHECKING_P)
18079 : : {
18080 : 11779680 : const line_map_ordinary *map
18081 : 11779680 : = linemap_check_ordinary (linemap_lookup (line_table, loc));
18082 : 11779680 : if (MAP_MODULE_P (map) && loc == MAP_START_LOCATION (map))
18083 : : {
18084 : 0 : gcc_checking_assert (spans.locations_exhausted_p ());
18085 : 0 : write_location (sec, linemap_included_from (map));
18086 : 0 : return;
18087 : : }
18088 : : }
18089 : :
18090 : 11779680 : const ord_loc_info *info = nullptr;
18091 : 11779680 : line_map_uint_t offset = 0;
18092 : 11779680 : if (line_map_uint_t hwm = ord_loc_remap->length ())
18093 : : {
18094 : 11779680 : info = ord_loc_remap->begin ();
18095 : 170526574 : while (hwm != 1)
18096 : : {
18097 : 146967214 : auto mid = hwm / 2;
18098 : 146967214 : if (MAP_START_LOCATION (info[mid].src) + info[mid].offset <= loc)
18099 : : {
18100 : 77385681 : info += mid;
18101 : 77385681 : hwm -= mid;
18102 : : }
18103 : : else
18104 : : hwm = mid;
18105 : : }
18106 : 11779680 : offset = loc - MAP_START_LOCATION (info->src) - info->offset;
18107 : 11779680 : if (offset > info->span)
18108 : 3785 : info = nullptr;
18109 : : }
18110 : :
18111 : 11779680 : gcc_checking_assert (bool (info) == bool (spans.ordinary (loc)));
18112 : :
18113 : 11779680 : if (info)
18114 : : {
18115 : 11775895 : offset += info->remap;
18116 : 11775895 : sec.u (LK_ORDINARY);
18117 : 11775895 : sec.loc (offset);
18118 : :
18119 : 11775895 : dump (dumper::LOCATION)
18120 : 78 : && dump ("Ordinary location %K output %K", loc, offset);
18121 : : }
18122 : 3785 : else if (const module_state *import = module_for_ordinary_loc (loc))
18123 : : {
18124 : 3785 : auto off = loc - import->ordinary_locs.first;
18125 : 3785 : sec.u (LK_IMPORT_ORDINARY);
18126 : 3785 : sec.u (import->remap);
18127 : 3785 : sec.loc (off);
18128 : 3785 : dump (dumper::LOCATION)
18129 : 0 : && dump ("Imported ordinary location %K output %u:%K",
18130 : 0 : loc, import->remap, off);
18131 : : }
18132 : : else
18133 : 0 : gcc_unreachable ();
18134 : : }
18135 : : else
18136 : 0 : gcc_unreachable ();
18137 : : }
18138 : :
18139 : : location_t
18140 : 14705722 : module_state::read_location (bytes_in &sec) const
18141 : : {
18142 : 14705722 : location_t locus = UNKNOWN_LOCATION;
18143 : 14705722 : unsigned kind = sec.u ();
18144 : 14705722 : switch (kind)
18145 : : {
18146 : 1357952 : default:
18147 : 1357952 : {
18148 : 1357952 : if (kind < LK_RESERVED + RESERVED_LOCATION_COUNT)
18149 : 1357952 : locus = location_t (kind - LK_RESERVED);
18150 : : else
18151 : 0 : sec.set_overrun ();
18152 : 1357952 : dump (dumper::LOCATION)
18153 : 0 : && dump ("Reserved location %K", locus);
18154 : : }
18155 : : break;
18156 : :
18157 : 1564949 : case LK_ADHOC:
18158 : 1564949 : {
18159 : 1564949 : dump (dumper::LOCATION) && dump ("Adhoc location");
18160 : 1564949 : locus = read_location (sec);
18161 : 1564949 : source_range range;
18162 : 1564949 : range.m_start = read_location (sec);
18163 : 1564949 : if (range.m_start == UNKNOWN_LOCATION)
18164 : 45905 : range.m_start = locus;
18165 : 1564949 : range.m_finish = read_location (sec);
18166 : 1564949 : unsigned discriminator = sec.u ();
18167 : 1564949 : if (locus != loc && range.m_start != loc && range.m_finish != loc)
18168 : 1564949 : locus = line_table->get_or_create_combined_loc (locus, range,
18169 : : nullptr, discriminator);
18170 : : }
18171 : : break;
18172 : :
18173 : 1122817 : case LK_MACRO:
18174 : 1122817 : {
18175 : 1122817 : auto off = sec.loc ();
18176 : :
18177 : 1122817 : if (macro_locs.second)
18178 : : {
18179 : 1122817 : if (off < macro_locs.second)
18180 : 1122817 : locus = off + macro_locs.first;
18181 : : else
18182 : 0 : sec.set_overrun ();
18183 : : }
18184 : : else
18185 : 0 : locus = loc;
18186 : 1122817 : dump (dumper::LOCATION)
18187 : 0 : && dump ("Macro %K becoming %K", off, locus);
18188 : : }
18189 : : break;
18190 : :
18191 : 10658912 : case LK_ORDINARY:
18192 : 10658912 : {
18193 : 10658912 : auto off = sec.loc ();
18194 : 10658912 : if (ordinary_locs.second)
18195 : : {
18196 : 10658912 : if (off < ordinary_locs.second)
18197 : 10658912 : locus = off + ordinary_locs.first;
18198 : : else
18199 : 0 : sec.set_overrun ();
18200 : : }
18201 : : else
18202 : 0 : locus = loc;
18203 : :
18204 : 10658912 : dump (dumper::LOCATION)
18205 : 0 : && dump ("Ordinary location %K becoming %K", off, locus);
18206 : : }
18207 : : break;
18208 : :
18209 : 1092 : case LK_IMPORT_MACRO:
18210 : 1092 : case LK_IMPORT_ORDINARY:
18211 : 1092 : {
18212 : 1092 : unsigned mod = sec.u ();
18213 : 1092 : location_t off = sec.loc ();
18214 : 1092 : const module_state *import = NULL;
18215 : :
18216 : 1092 : if (!mod && !slurp->remap)
18217 : : /* This is an early read of a partition location during the
18218 : : read of our ordinary location map. */
18219 : : import = this;
18220 : : else
18221 : : {
18222 : 1092 : mod = slurp->remap_module (mod);
18223 : 1092 : if (!mod)
18224 : 0 : sec.set_overrun ();
18225 : : else
18226 : 1092 : import = (*modules)[mod];
18227 : : }
18228 : :
18229 : 1092 : if (import)
18230 : : {
18231 : 1092 : if (kind == LK_IMPORT_MACRO)
18232 : : {
18233 : 6 : if (!import->macro_locs.second)
18234 : 0 : locus = import->loc;
18235 : 6 : else if (off < import->macro_locs.second)
18236 : 6 : locus = off + import->macro_locs.first;
18237 : : else
18238 : 0 : sec.set_overrun ();
18239 : : }
18240 : : else
18241 : : {
18242 : 1086 : if (!import->ordinary_locs.second)
18243 : 0 : locus = import->loc;
18244 : 1086 : else if (off < import->ordinary_locs.second)
18245 : 1086 : locus = import->ordinary_locs.first + off;
18246 : : else
18247 : 0 : sec.set_overrun ();
18248 : : }
18249 : : }
18250 : : }
18251 : : break;
18252 : : }
18253 : :
18254 : 14705722 : return locus;
18255 : : }
18256 : :
18257 : : /* Allocate hash tables to record needed locations. */
18258 : :
18259 : : void
18260 : 2513 : module_state::write_init_maps ()
18261 : : {
18262 : 2513 : macro_loc_table = new hash_table<macro_loc_traits> (EXPERIMENT (1, 400));
18263 : 2513 : ord_loc_table = new hash_table<ord_loc_traits> (EXPERIMENT (1, 400));
18264 : 2513 : }
18265 : :
18266 : : /* Prepare the span adjustments. We prune unneeded locations -- at
18267 : : this point every needed location must have been seen by
18268 : : note_location. */
18269 : :
18270 : : range_t
18271 : 2484 : module_state::write_prepare_maps (module_state_config *cfg, bool has_partitions)
18272 : : {
18273 : 2757 : dump () && dump ("Preparing locations");
18274 : 2484 : dump.indent ();
18275 : :
18276 : 2757 : dump () && dump ("Reserved locations [%K,%K) macro [%K,%K)",
18277 : 273 : spans[loc_spans::SPAN_RESERVED].ordinary.first,
18278 : 273 : spans[loc_spans::SPAN_RESERVED].ordinary.second,
18279 : 273 : spans[loc_spans::SPAN_RESERVED].macro.first,
18280 : 273 : spans[loc_spans::SPAN_RESERVED].macro.second);
18281 : :
18282 : 2484 : range_t info {0, 0};
18283 : :
18284 : : // Sort the noted lines.
18285 : 2484 : vec_alloc (ord_loc_remap, ord_loc_table->size ());
18286 : 2484 : for (auto iter = ord_loc_table->begin (), end = ord_loc_table->end ();
18287 : 2972624 : iter != end; ++iter)
18288 : 1485070 : ord_loc_remap->quick_push (*iter);
18289 : 2484 : ord_loc_remap->qsort (&ord_loc_info::compare);
18290 : :
18291 : : // Note included-from maps.
18292 : 2484 : bool added = false;
18293 : 2484 : const line_map_ordinary *current = nullptr;
18294 : 1492522 : for (auto iter = ord_loc_remap->begin (), end = ord_loc_remap->end ();
18295 : 1487554 : iter != end; ++iter)
18296 : 1485070 : if (iter->src != current)
18297 : : {
18298 : 26312 : current = iter->src;
18299 : 10214 : for (auto probe = current;
18300 : 26312 : auto from = linemap_included_from (probe);
18301 : 10214 : probe = linemap_check_ordinary (linemap_lookup (line_table, from)))
18302 : : {
18303 : 23562 : if (has_partitions)
18304 : : {
18305 : : // Partition locations need to elide their module map
18306 : : // entry.
18307 : 181 : probe
18308 : 181 : = linemap_check_ordinary (linemap_lookup (line_table, from));
18309 : 181 : if (MAP_MODULE_P (probe))
18310 : 151 : from = linemap_included_from (probe);
18311 : : }
18312 : :
18313 : 23562 : if (!note_location (from))
18314 : : break;
18315 : 10214 : added = true;
18316 : 10214 : }
18317 : : }
18318 : 2484 : if (added)
18319 : : {
18320 : : // Reconstruct the line array as we added items to the hash table.
18321 : 457 : vec_free (ord_loc_remap);
18322 : 457 : vec_alloc (ord_loc_remap, ord_loc_table->size ());
18323 : 457 : for (auto iter = ord_loc_table->begin (), end = ord_loc_table->end ();
18324 : 2970807 : iter != end; ++iter)
18325 : 1485175 : ord_loc_remap->quick_push (*iter);
18326 : 457 : ord_loc_remap->qsort (&ord_loc_info::compare);
18327 : : }
18328 : 2484 : delete ord_loc_table;
18329 : 2484 : ord_loc_table = nullptr;
18330 : :
18331 : : // Merge (sufficiently) adjacent spans, and calculate remapping.
18332 : 2484 : constexpr line_map_uint_t adjacency = 2; // Allow 2 missing lines.
18333 : 4968 : auto begin = ord_loc_remap->begin (), end = ord_loc_remap->end ();
18334 : 2484 : auto dst = begin;
18335 : 2484 : line_map_uint_t offset = 0;
18336 : 2484 : unsigned range_bits = 0;
18337 : 2484 : ord_loc_info *base = nullptr;
18338 : 1497768 : for (auto iter = begin; iter != end; ++iter)
18339 : : {
18340 : 1495284 : if (base && iter->src == base->src)
18341 : : {
18342 : 2780013 : if (base->offset + base->span +
18343 : 1473045 : ((adjacency << base->src->m_column_and_range_bits)
18344 : : // If there are few c&r bits, allow further separation.
18345 : 1473045 : | (adjacency << 4))
18346 : 1473045 : >= iter->offset)
18347 : : {
18348 : : // Merge.
18349 : 1306968 : offset -= base->span;
18350 : 1306968 : base->span = iter->offset + iter->span - base->offset;
18351 : 1306968 : offset += base->span;
18352 : 1306968 : continue;
18353 : : }
18354 : : }
18355 : 22239 : else if (range_bits < iter->src->m_range_bits)
18356 : 2388 : range_bits = iter->src->m_range_bits;
18357 : :
18358 : 188316 : offset += ((loc_one << iter->src->m_range_bits) - 1);
18359 : 188316 : offset &= ~((loc_one << iter->src->m_range_bits) - 1);
18360 : 188316 : iter->remap = offset;
18361 : 188316 : offset += iter->span;
18362 : 188316 : base = dst;
18363 : 188316 : *dst++ = *iter;
18364 : : }
18365 : 2484 : ord_loc_remap->truncate (dst - begin);
18366 : :
18367 : 2484 : info.first = ord_loc_remap->length ();
18368 : 2484 : cfg->ordinary_locs = offset;
18369 : 2484 : cfg->loc_range_bits = range_bits;
18370 : 2757 : dump () && dump ("Ordinary maps:%K locs:%K range_bits:%u",
18371 : : info.first,
18372 : : cfg->ordinary_locs,
18373 : : cfg->loc_range_bits);
18374 : :
18375 : : // Remap the macro locations.
18376 : 2484 : vec_alloc (macro_loc_remap, macro_loc_table->size ());
18377 : 2484 : for (auto iter = macro_loc_table->begin (), end = macro_loc_table->end ();
18378 : 245654 : iter != end; ++iter)
18379 : 121585 : macro_loc_remap->quick_push (*iter);
18380 : 2484 : delete macro_loc_table;
18381 : 2484 : macro_loc_table = nullptr;
18382 : :
18383 : 2484 : macro_loc_remap->qsort (¯o_loc_info::compare);
18384 : 2484 : offset = 0;
18385 : 7452 : for (auto iter = macro_loc_remap->begin (), end = macro_loc_remap->end ();
18386 : 124069 : iter != end; ++iter)
18387 : : {
18388 : 121585 : auto mac = iter->src;
18389 : 121585 : iter->remap = offset;
18390 : 121585 : offset += mac->n_tokens;
18391 : : }
18392 : 2484 : info.second = macro_loc_remap->length ();
18393 : 2484 : cfg->macro_locs = offset;
18394 : :
18395 : 2757 : dump () && dump ("Macro maps:%K locs:%K", info.second, cfg->macro_locs);
18396 : :
18397 : 2484 : dump.outdent ();
18398 : :
18399 : : // If we have no ordinary locs, we must also have no macro locs.
18400 : 2484 : gcc_checking_assert (cfg->ordinary_locs || !cfg->macro_locs);
18401 : :
18402 : 2484 : return info;
18403 : : }
18404 : :
18405 : : bool
18406 : 2716 : module_state::read_prepare_maps (const module_state_config *cfg)
18407 : : {
18408 : 2716 : location_t ordinary = line_table->highest_location + 1;
18409 : 2716 : ordinary += cfg->ordinary_locs;
18410 : :
18411 : 2716 : location_t macro = LINEMAPS_MACRO_LOWEST_LOCATION (line_table);
18412 : 2716 : macro -= cfg->macro_locs;
18413 : :
18414 : 2716 : if (ordinary < LINE_MAP_MAX_LOCATION_WITH_COLS
18415 : 2716 : && macro >= LINE_MAP_MAX_LOCATION)
18416 : : /* OK, we have enough locations. */
18417 : : return true;
18418 : :
18419 : 0 : ordinary_locs.first = ordinary_locs.second = 0;
18420 : 0 : macro_locs.first = macro_locs.second = 0;
18421 : :
18422 : 0 : spans.report_location_exhaustion (loc);
18423 : :
18424 : : return false;
18425 : : }
18426 : :
18427 : : /* Write & read the location maps. Not called if there are no
18428 : : locations. */
18429 : :
18430 : : void
18431 : 2388 : module_state::write_ordinary_maps (elf_out *to, range_t &info,
18432 : : bool has_partitions, unsigned *crc_p)
18433 : : {
18434 : 2639 : dump () && dump ("Writing ordinary location maps");
18435 : 2388 : dump.indent ();
18436 : :
18437 : 2388 : vec<const char *> filenames;
18438 : 2388 : filenames.create (20);
18439 : :
18440 : : /* Determine the unique filenames. */
18441 : 2388 : const line_map_ordinary *current = nullptr;
18442 : 195480 : for (auto iter = ord_loc_remap->begin (), end = ord_loc_remap->end ();
18443 : 190704 : iter != end; ++iter)
18444 : 188316 : if (iter->src != current)
18445 : : {
18446 : 22239 : current = iter->src;
18447 : 22239 : const char *fname = ORDINARY_MAP_FILE_NAME (iter->src);
18448 : :
18449 : : /* We should never find a module linemap in an interval. */
18450 : 22239 : gcc_checking_assert (!MAP_MODULE_P (iter->src));
18451 : :
18452 : : /* We expect very few filenames, so just an array.
18453 : : (Not true when headers are still in play :() */
18454 : 1306999 : for (unsigned jx = filenames.length (); jx--;)
18455 : : {
18456 : 1272846 : const char *name = filenames[jx];
18457 : 1272846 : if (0 == strcmp (name, fname))
18458 : : {
18459 : : /* Reset the linemap's name, because for things like
18460 : : preprocessed input we could have multiple instances
18461 : : of the same name, and we'd rather not percolate
18462 : : that. */
18463 : 10325 : const_cast<line_map_ordinary *> (iter->src)->to_file = name;
18464 : 10325 : fname = NULL;
18465 : 10325 : break;
18466 : : }
18467 : : }
18468 : 22239 : if (fname)
18469 : 11914 : filenames.safe_push (fname);
18470 : : }
18471 : :
18472 : 2388 : bytes_out sec (to);
18473 : 2388 : sec.begin ();
18474 : :
18475 : : /* Write the filenames. */
18476 : 2388 : unsigned len = filenames.length ();
18477 : 2388 : sec.u (len);
18478 : 2639 : dump () && dump ("%u source file names", len);
18479 : 14302 : for (unsigned ix = 0; ix != len; ix++)
18480 : : {
18481 : 11914 : const char *fname = filenames[ix];
18482 : 11929 : dump (dumper::LOCATION) && dump ("Source file[%u]=%s", ix, fname);
18483 : 11914 : sec.str (fname);
18484 : : }
18485 : :
18486 : 2388 : sec.loc (info.first); /* Num maps. */
18487 : 2388 : const ord_loc_info *base = nullptr;
18488 : 195480 : for (auto iter = ord_loc_remap->begin (), end = ord_loc_remap->end ();
18489 : 190704 : iter != end; ++iter)
18490 : : {
18491 : 188316 : dump (dumper::LOCATION)
18492 : 36 : && dump ("Span:%K ordinary [%K+%K,+%K)->[%K,+%K)",
18493 : 36 : (location_t) (iter - ord_loc_remap->begin ()),
18494 : 18 : MAP_START_LOCATION (iter->src),
18495 : : iter->offset, iter->span, iter->remap,
18496 : : iter->span);
18497 : :
18498 : 188316 : if (!base || iter->src != base->src)
18499 : 22239 : base = iter;
18500 : 188316 : sec.loc (iter->offset - base->offset);
18501 : 188316 : if (base == iter)
18502 : : {
18503 : 22239 : sec.u (iter->src->sysp);
18504 : 22239 : sec.u (iter->src->m_range_bits);
18505 : 22239 : sec.u (iter->src->m_column_and_range_bits - iter->src->m_range_bits);
18506 : :
18507 : 22239 : const char *fname = ORDINARY_MAP_FILE_NAME (iter->src);
18508 : 4074396 : for (unsigned ix = 0; ix != filenames.length (); ix++)
18509 : 2037198 : if (filenames[ix] == fname)
18510 : : {
18511 : 22239 : sec.u (ix);
18512 : 22239 : break;
18513 : : }
18514 : 22239 : unsigned line = ORDINARY_MAP_STARTING_LINE_NUMBER (iter->src);
18515 : 22239 : line += iter->offset >> iter->src->m_column_and_range_bits;
18516 : 22239 : sec.u (line);
18517 : : }
18518 : 188316 : sec.loc (iter->remap);
18519 : 188316 : if (base == iter)
18520 : : {
18521 : : /* Write the included from location, which means reading it
18522 : : while reading in the ordinary maps. So we'd better not
18523 : : be getting ahead of ourselves. */
18524 : 22239 : location_t from = linemap_included_from (iter->src);
18525 : 22239 : gcc_checking_assert (from < MAP_START_LOCATION (iter->src));
18526 : 22239 : if (from != UNKNOWN_LOCATION && has_partitions)
18527 : : {
18528 : : /* A partition's span will have a from pointing at a
18529 : : MODULE_INC. Find that map's from. */
18530 : 175 : line_map_ordinary const *fmap
18531 : 175 : = linemap_check_ordinary (linemap_lookup (line_table, from));
18532 : 175 : if (MAP_MODULE_P (fmap))
18533 : 145 : from = linemap_included_from (fmap);
18534 : : }
18535 : 22239 : write_location (sec, from);
18536 : : }
18537 : : }
18538 : :
18539 : 2388 : filenames.release ();
18540 : :
18541 : 2388 : sec.end (to, to->name (MOD_SNAME_PFX ".olm"), crc_p);
18542 : 2388 : dump.outdent ();
18543 : 2388 : }
18544 : :
18545 : : /* Return the prefix to use for dumping a #pragma diagnostic change to DK. */
18546 : :
18547 : : static const char *
18548 : 850 : dk_string (enum diagnostics::kind dk)
18549 : : {
18550 : 850 : gcc_assert (dk > diagnostics::kind::unspecified
18551 : : && dk < diagnostics::kind::last_diagnostic_kind);
18552 : 850 : if (dk == diagnostics::kind::ignored)
18553 : : /* diagnostics/kinds.def has an empty string for ignored. */
18554 : : return "ignored: ";
18555 : : else
18556 : 0 : return diagnostics::get_text_for_kind (dk);
18557 : : }
18558 : :
18559 : : /* Dump one #pragma GCC diagnostic entry. */
18560 : :
18561 : : static bool
18562 : 1734 : dump_dc_change (unsigned index, unsigned opt, enum diagnostics::kind dk)
18563 : : {
18564 : 1734 : if (dk == diagnostics::kind::pop)
18565 : 884 : return dump (" Index %u: pop from %d", index, opt);
18566 : : else
18567 : 850 : return dump (" Index %u: %s%s", index, dk_string (dk),
18568 : 1700 : cl_options[opt].opt_text);
18569 : : }
18570 : :
18571 : : /* Write out any #pragma GCC diagnostic info to the .dgc section. */
18572 : :
18573 : : void
18574 : 4872 : module_state::write_diagnostic_classification (elf_out *to,
18575 : : diagnostics::context *dc,
18576 : : unsigned *crc_p)
18577 : : {
18578 : 4872 : auto &changes = dc->get_classification_history ();
18579 : :
18580 : 4872 : bytes_out sec (to);
18581 : 4872 : if (sec.streaming_p ())
18582 : : {
18583 : 2388 : sec.begin ();
18584 : 2639 : dump () && dump ("Writing diagnostic change locations");
18585 : 2388 : dump.indent ();
18586 : : }
18587 : :
18588 : 4872 : unsigned len = changes.length ();
18589 : :
18590 : : /* We don't want to write out any entries that came from one of our imports.
18591 : : But then we need to adjust the total, and change diagnostics::kind::pop
18592 : : targets to match the index in our actual output. So remember how many
18593 : : lines we had skipped at each step, where -1 means this line itself
18594 : : is skipped. */
18595 : 4872 : int skips = 0;
18596 : 4872 : auto_vec<int> skips_at (len);
18597 : 4872 : skips_at.safe_grow (len);
18598 : :
18599 : 45094 : for (unsigned i = 0; i < len; ++i)
18600 : : {
18601 : 40222 : const auto &c = changes[i];
18602 : 40222 : skips_at[i] = skips;
18603 : 40222 : if (linemap_location_from_module_p (line_table, c.location))
18604 : : {
18605 : 6346 : ++skips;
18606 : 6346 : skips_at[i] = -1;
18607 : 6346 : continue;
18608 : : }
18609 : : }
18610 : :
18611 : 4872 : if (sec.streaming_p ())
18612 : : {
18613 : 2388 : sec.u (len - skips);
18614 : 2639 : dump () && dump ("Diagnostic changes: %u", len - skips);
18615 : : }
18616 : :
18617 : 45094 : for (unsigned i = 0; i < len; ++i)
18618 : : {
18619 : 40222 : if (skips_at[i] == -1)
18620 : 6346 : continue;
18621 : :
18622 : 33876 : const auto &c = changes[i];
18623 : 33876 : write_location (sec, c.location);
18624 : 33876 : if (sec.streaming_p ())
18625 : : {
18626 : 16938 : unsigned opt = c.option;
18627 : 16938 : if (c.kind == diagnostics::kind::pop)
18628 : 8645 : opt -= skips_at[opt];
18629 : 16938 : sec.u (opt);
18630 : 16938 : sec.u (static_cast<unsigned> (c.kind));
18631 : 41901 : dump () && dump_dc_change (i - skips_at[i], opt, c.kind);
18632 : : }
18633 : : }
18634 : :
18635 : 4872 : if (sec.streaming_p ())
18636 : : {
18637 : 2388 : sec.end (to, to->name (MOD_SNAME_PFX ".dgc"), crc_p);
18638 : 2388 : dump.outdent ();
18639 : : }
18640 : 4872 : }
18641 : :
18642 : : /* Read any #pragma GCC diagnostic info from the .dgc section. */
18643 : :
18644 : : bool
18645 : 2658 : module_state::read_diagnostic_classification (diagnostics::context *dc)
18646 : : {
18647 : 2658 : bytes_in sec;
18648 : :
18649 : 2658 : if (!sec.begin (loc, from (), MOD_SNAME_PFX ".dgc"))
18650 : : return false;
18651 : :
18652 : 3136 : dump () && dump ("Reading diagnostic change locations");
18653 : 2658 : dump.indent ();
18654 : :
18655 : 2658 : unsigned len = sec.u ();
18656 : 3136 : dump () && dump ("Diagnostic changes: %u", len);
18657 : :
18658 : 2658 : auto &changes = dc->get_classification_history ();
18659 : 2658 : int offset = changes.length ();
18660 : 2658 : changes.reserve (len + 1);
18661 : 22585 : for (unsigned i = 0; i < len; ++i)
18662 : : {
18663 : 19927 : location_t loc = read_location (sec);
18664 : 19927 : int opt = sec.u ();
18665 : 19927 : enum diagnostics::kind kind = (enum diagnostics::kind) sec.u ();
18666 : 19927 : if (kind == diagnostics::kind::pop)
18667 : : /* For a pop, opt is the 'changes' index to return to. */
18668 : 10178 : opt += offset;
18669 : 19927 : changes.quick_push ({ loc, opt, kind });
18670 : 19982 : dump () && dump_dc_change (changes.length () - 1, opt, kind);
18671 : : }
18672 : :
18673 : : /* Did the import pop all its diagnostic changes? */
18674 : 2658 : bool last_was_reset = (len == 0);
18675 : 2658 : if (len)
18676 : 208 : for (int i = changes.length () - 1; ; --i)
18677 : : {
18678 : 8911 : gcc_checking_assert (i >= offset);
18679 : :
18680 : 8911 : const auto &c = changes[i];
18681 : 8911 : if (c.kind != diagnostics::kind::pop)
18682 : : break;
18683 : 8902 : else if (c.option == offset)
18684 : : {
18685 : : last_was_reset = true;
18686 : : break;
18687 : : }
18688 : : else
18689 : : /* As in update_effective_level_from_pragmas, the loop will decrement
18690 : : i so we actually jump to c.option - 1. */
18691 : 8807 : i = c.option;
18692 : 8807 : }
18693 : 2658 : if (!last_was_reset)
18694 : : {
18695 : : /* It didn't, so add a pop at its last location to avoid affecting later
18696 : : imports. */
18697 : 9 : location_t last_loc = ordinary_locs.first + ordinary_locs.second - 1;
18698 : 9 : changes.quick_push ({ last_loc, offset, diagnostics::kind::pop });
18699 : 15 : dump () && dump (" Adding final pop from index %d", offset);
18700 : : }
18701 : :
18702 : 2658 : dump.outdent ();
18703 : 2658 : if (!sec.end (from ()))
18704 : : return false;
18705 : :
18706 : : return true;
18707 : 2658 : }
18708 : :
18709 : : void
18710 : 117 : module_state::write_macro_maps (elf_out *to, range_t &info, unsigned *crc_p)
18711 : : {
18712 : 129 : dump () && dump ("Writing macro location maps");
18713 : 117 : dump.indent ();
18714 : :
18715 : 117 : bytes_out sec (to);
18716 : 117 : sec.begin ();
18717 : :
18718 : 129 : dump () && dump ("Macro maps:%K", info.second);
18719 : 117 : sec.loc (info.second);
18720 : :
18721 : 117 : line_map_uint_t macro_num = 0;
18722 : 234 : for (auto iter = macro_loc_remap->end (), begin = macro_loc_remap->begin ();
18723 : 121702 : iter-- != begin;)
18724 : : {
18725 : 121585 : auto mac = iter->src;
18726 : 121585 : sec.loc (iter->remap);
18727 : 121585 : sec.u (mac->n_tokens);
18728 : 121585 : sec.cpp_node (mac->macro);
18729 : 121585 : write_location (sec, mac->m_expansion);
18730 : 121585 : const location_t *locs = mac->macro_locations;
18731 : : /* There are lots of identical runs. */
18732 : 121585 : location_t prev = UNKNOWN_LOCATION;
18733 : 121585 : unsigned count = 0;
18734 : 121585 : unsigned runs = 0;
18735 : 4298873 : for (unsigned jx = mac->n_tokens * 2; jx--;)
18736 : : {
18737 : 4177288 : location_t tok_loc = locs[jx];
18738 : 4177288 : if (tok_loc == prev)
18739 : : {
18740 : 1964390 : count++;
18741 : 1964390 : continue;
18742 : : }
18743 : 2212898 : runs++;
18744 : 2212898 : sec.u (count);
18745 : 2212898 : count = 1;
18746 : 2212898 : prev = tok_loc;
18747 : 2212898 : write_location (sec, tok_loc);
18748 : : }
18749 : 121585 : sec.u (count);
18750 : 121585 : dump (dumper::LOCATION)
18751 : 9 : && dump ("Macro:%K %I %u/%u*2 locations [%K,%K)->%K",
18752 : 9 : macro_num, identifier (mac->macro),
18753 : : runs, mac->n_tokens,
18754 : : MAP_START_LOCATION (mac),
18755 : 9 : MAP_START_LOCATION (mac) + mac->n_tokens,
18756 : : iter->remap);
18757 : 121585 : macro_num++;
18758 : : }
18759 : 117 : gcc_assert (macro_num == info.second);
18760 : :
18761 : 117 : sec.end (to, to->name (MOD_SNAME_PFX ".mlm"), crc_p);
18762 : 117 : dump.outdent ();
18763 : 117 : }
18764 : :
18765 : : bool
18766 : 2658 : module_state::read_ordinary_maps (line_map_uint_t num_ord_locs,
18767 : : unsigned range_bits)
18768 : : {
18769 : 2658 : bytes_in sec;
18770 : :
18771 : 2658 : if (!sec.begin (loc, from (), MOD_SNAME_PFX ".olm"))
18772 : : return false;
18773 : 3136 : dump () && dump ("Reading ordinary location maps");
18774 : 2658 : dump.indent ();
18775 : :
18776 : : /* Read the filename table. */
18777 : 2658 : unsigned len = sec.u ();
18778 : 3136 : dump () && dump ("%u source file names", len);
18779 : 2658 : vec<const char *> filenames;
18780 : 2658 : filenames.create (len);
18781 : 16579 : for (unsigned ix = 0; ix != len; ix++)
18782 : : {
18783 : 13921 : size_t l;
18784 : 13921 : const char *buf = sec.str (&l);
18785 : 13921 : char *fname = XNEWVEC (char, l + 1);
18786 : 13921 : memcpy (fname, buf, l + 1);
18787 : 13921 : dump (dumper::LOCATION) && dump ("Source file[%u]=%s", ix, fname);
18788 : : /* We leak these names into the line-map table. But it
18789 : : doesn't own them. */
18790 : 13921 : filenames.quick_push (fname);
18791 : : }
18792 : :
18793 : 2658 : line_map_uint_t num_ordinary = sec.loc ();
18794 : 3136 : dump () && dump ("Ordinary maps:%K, range_bits:%u",
18795 : : num_ordinary, range_bits);
18796 : :
18797 : 2658 : location_t offset = line_table->highest_location + 1;
18798 : 2658 : offset += ((loc_one << range_bits) - 1);
18799 : 2658 : offset &= ~((loc_one << range_bits) - 1);
18800 : 2658 : ordinary_locs.first = offset;
18801 : :
18802 : 2658 : bool propagated = spans.maybe_propagate (this, offset);
18803 : 2658 : line_map_ordinary *maps = static_cast<line_map_ordinary *>
18804 : 2658 : (line_map_new_raw (line_table, false, num_ordinary));
18805 : :
18806 : 2658 : const line_map_ordinary *base = nullptr;
18807 : 232499 : for (line_map_uint_t ix = 0; ix != num_ordinary && !sec.get_overrun (); ix++)
18808 : : {
18809 : 229841 : line_map_ordinary *map = &maps[ix];
18810 : :
18811 : 229841 : location_t offset = sec.loc ();
18812 : 229841 : if (!offset)
18813 : : {
18814 : 26339 : map->reason = LC_RENAME;
18815 : 26339 : map->sysp = sec.u ();
18816 : 26339 : map->m_range_bits = sec.u ();
18817 : 26339 : map->m_column_and_range_bits = sec.u () + map->m_range_bits;
18818 : 26339 : unsigned fnum = sec.u ();
18819 : 52678 : map->to_file = (fnum < filenames.length () ? filenames[fnum] : "");
18820 : 26339 : map->to_line = sec.u ();
18821 : 26339 : base = map;
18822 : : }
18823 : : else
18824 : : {
18825 : 203502 : *map = *base;
18826 : 203502 : map->to_line += offset >> map->m_column_and_range_bits;
18827 : : }
18828 : 229841 : location_t remap = sec.loc ();
18829 : 229841 : map->start_location = remap + ordinary_locs.first;
18830 : 229841 : if (base == map)
18831 : : {
18832 : : /* Root the outermost map at our location. */
18833 : 26339 : ordinary_locs.second = remap;
18834 : 26339 : location_t from = read_location (sec);
18835 : 26339 : map->included_from = from != UNKNOWN_LOCATION ? from : loc;
18836 : : }
18837 : : }
18838 : :
18839 : 2658 : ordinary_locs.second = num_ord_locs;
18840 : : /* highest_location is the one handed out, not the next one to
18841 : : hand out. */
18842 : 2658 : line_table->highest_location = ordinary_locs.first + ordinary_locs.second - 1;
18843 : :
18844 : 2658 : if (line_table->highest_location >= LINE_MAP_MAX_LOCATION_WITH_COLS)
18845 : : /* We shouldn't run out of locations, as we checked before
18846 : : starting. */
18847 : 0 : sec.set_overrun ();
18848 : 3136 : dump () && dump ("Ordinary location [%K,+%K)",
18849 : : ordinary_locs.first, ordinary_locs.second);
18850 : :
18851 : 2658 : if (propagated)
18852 : 145 : spans.close ();
18853 : :
18854 : 2658 : filenames.release ();
18855 : :
18856 : 2658 : dump.outdent ();
18857 : 2658 : if (!sec.end (from ()))
18858 : : return false;
18859 : :
18860 : : return true;
18861 : 2658 : }
18862 : :
18863 : : bool
18864 : 127 : module_state::read_macro_maps (line_map_uint_t num_macro_locs)
18865 : : {
18866 : 127 : bytes_in sec;
18867 : :
18868 : 127 : if (!sec.begin (loc, from (), MOD_SNAME_PFX ".mlm"))
18869 : : return false;
18870 : 133 : dump () && dump ("Reading macro location maps");
18871 : 127 : dump.indent ();
18872 : :
18873 : 127 : line_map_uint_t num_macros = sec.loc ();
18874 : 133 : dump () && dump ("Macro maps:%K locs:%K",
18875 : : num_macros, num_macro_locs);
18876 : :
18877 : 254 : bool propagated = spans.maybe_propagate (this,
18878 : 127 : line_table->highest_location + 1);
18879 : :
18880 : 127 : location_t offset = LINEMAPS_MACRO_LOWEST_LOCATION (line_table);
18881 : 127 : macro_locs.second = num_macro_locs;
18882 : 127 : macro_locs.first = offset - num_macro_locs;
18883 : :
18884 : 133 : dump () && dump ("Macro loc delta %K", offset);
18885 : 133 : dump () && dump ("Macro locations [%K,%K)",
18886 : : macro_locs.first, macro_locs.second);
18887 : :
18888 : 153523 : for (line_map_uint_t ix = 0; ix != num_macros && !sec.get_overrun (); ix++)
18889 : : {
18890 : 153396 : location_t offset = sec.loc ();
18891 : 153396 : unsigned n_tokens = sec.u ();
18892 : 153396 : cpp_hashnode *node = sec.cpp_node ();
18893 : 153396 : location_t exp_loc = read_location (sec);
18894 : :
18895 : 153396 : const line_map_macro *macro
18896 : 153396 : = linemap_enter_macro (line_table, node, exp_loc, n_tokens);
18897 : 153396 : if (!macro)
18898 : : /* We shouldn't run out of locations, as we checked that we
18899 : : had enough before starting. */
18900 : : break;
18901 : 153396 : gcc_checking_assert (MAP_START_LOCATION (macro)
18902 : : == offset + macro_locs.first);
18903 : :
18904 : 153396 : location_t *locs = macro->macro_locations;
18905 : 153396 : location_t tok_loc = UNKNOWN_LOCATION;
18906 : 153396 : unsigned count = sec.u ();
18907 : 153396 : unsigned runs = 0;
18908 : 5358288 : for (unsigned jx = macro->n_tokens * 2; jx-- && !sec.get_overrun ();)
18909 : : {
18910 : 7963810 : while (!count-- && !sec.get_overrun ())
18911 : : {
18912 : 2758918 : runs++;
18913 : 2758918 : tok_loc = read_location (sec);
18914 : 2758918 : count = sec.u ();
18915 : : }
18916 : 5204892 : locs[jx] = tok_loc;
18917 : : }
18918 : 153396 : if (count)
18919 : 0 : sec.set_overrun ();
18920 : 153423 : dump (dumper::LOCATION)
18921 : 0 : && dump ("Macro:%K %I %u/%u*2 locations [%K,%K)",
18922 : : ix, identifier (node), runs, n_tokens,
18923 : : MAP_START_LOCATION (macro),
18924 : 0 : MAP_START_LOCATION (macro) + n_tokens);
18925 : : }
18926 : :
18927 : 133 : dump () && dump ("Macro location lwm:%K", macro_locs.first);
18928 : 127 : if (propagated)
18929 : 3 : spans.close ();
18930 : :
18931 : 127 : dump.outdent ();
18932 : 127 : if (!sec.end (from ()))
18933 : : return false;
18934 : :
18935 : : return true;
18936 : 127 : }
18937 : :
18938 : : /* Serialize the definition of MACRO. */
18939 : :
18940 : : void
18941 : 67474 : module_state::write_define (bytes_out &sec, const cpp_macro *macro)
18942 : : {
18943 : 67474 : sec.u (macro->count);
18944 : :
18945 : 67474 : bytes_out::bits_out bits = sec.stream_bits ();
18946 : 67474 : bits.b (macro->fun_like);
18947 : 67474 : bits.b (macro->variadic);
18948 : 67474 : bits.b (macro->syshdr);
18949 : 67474 : bits.bflush ();
18950 : :
18951 : 67474 : write_location (sec, macro->line);
18952 : 67474 : if (macro->fun_like)
18953 : : {
18954 : 9021 : sec.u (macro->paramc);
18955 : 9021 : const cpp_hashnode *const *parms = macro->parm.params;
18956 : 22721 : for (unsigned ix = 0; ix != macro->paramc; ix++)
18957 : 13700 : sec.cpp_node (parms[ix]);
18958 : : }
18959 : :
18960 : : unsigned len = 0;
18961 : 221272 : for (unsigned ix = 0; ix != macro->count; ix++)
18962 : : {
18963 : 153798 : const cpp_token *token = ¯o->exp.tokens[ix];
18964 : 153798 : write_location (sec, token->src_loc);
18965 : 153798 : sec.u (token->type);
18966 : 153798 : sec.u (token->flags);
18967 : 153798 : switch (cpp_token_val_index (token))
18968 : : {
18969 : 0 : default:
18970 : 0 : gcc_unreachable ();
18971 : :
18972 : 11999 : case CPP_TOKEN_FLD_ARG_NO:
18973 : : /* An argument reference. */
18974 : 11999 : sec.u (token->val.macro_arg.arg_no);
18975 : 11999 : sec.cpp_node (token->val.macro_arg.spelling);
18976 : 11999 : break;
18977 : :
18978 : 30181 : case CPP_TOKEN_FLD_NODE:
18979 : : /* An identifier. */
18980 : 30181 : sec.cpp_node (token->val.node.node);
18981 : 30181 : if (token->val.node.spelling == token->val.node.node)
18982 : : /* The spelling will usually be the same. so optimize
18983 : : that. */
18984 : 30181 : sec.str (NULL, 0);
18985 : : else
18986 : 0 : sec.cpp_node (token->val.node.spelling);
18987 : : break;
18988 : :
18989 : : case CPP_TOKEN_FLD_NONE:
18990 : : break;
18991 : :
18992 : 47952 : case CPP_TOKEN_FLD_STR:
18993 : : /* A string, number or comment. Not always NUL terminated,
18994 : : we stream out in a single contatenation with embedded
18995 : : NULs as that's a safe default. */
18996 : 47952 : len += token->val.str.len + 1;
18997 : 47952 : sec.u (token->val.str.len);
18998 : 47952 : break;
18999 : :
19000 : 0 : case CPP_TOKEN_FLD_SOURCE:
19001 : 0 : case CPP_TOKEN_FLD_TOKEN_NO:
19002 : 0 : case CPP_TOKEN_FLD_PRAGMA:
19003 : : /* These do not occur inside a macro itself. */
19004 : 0 : gcc_unreachable ();
19005 : : }
19006 : : }
19007 : :
19008 : 67474 : if (len)
19009 : : {
19010 : 44440 : char *ptr = reinterpret_cast<char *> (sec.buf (len));
19011 : 44440 : len = 0;
19012 : 135786 : for (unsigned ix = 0; ix != macro->count; ix++)
19013 : : {
19014 : 91346 : const cpp_token *token = ¯o->exp.tokens[ix];
19015 : 91346 : if (cpp_token_val_index (token) == CPP_TOKEN_FLD_STR)
19016 : : {
19017 : 47952 : memcpy (ptr + len, token->val.str.text,
19018 : 47952 : token->val.str.len);
19019 : 47952 : len += token->val.str.len;
19020 : 47952 : ptr[len++] = 0;
19021 : : }
19022 : : }
19023 : : }
19024 : 67474 : }
19025 : :
19026 : : /* Read a macro definition. */
19027 : :
19028 : : cpp_macro *
19029 : 321 : module_state::read_define (bytes_in &sec, cpp_reader *reader) const
19030 : : {
19031 : 321 : unsigned count = sec.u ();
19032 : : /* We rely on knowing cpp_reader's hash table is ident_hash, and
19033 : : its subobject allocator is stringpool_ggc_alloc and that is just
19034 : : a wrapper for ggc_alloc_atomic. */
19035 : 321 : cpp_macro *macro
19036 : 642 : = (cpp_macro *)ggc_alloc_atomic (sizeof (cpp_macro)
19037 : 321 : + sizeof (cpp_token) * (count - !!count));
19038 : 321 : memset (macro, 0, sizeof (cpp_macro) + sizeof (cpp_token) * (count - !!count));
19039 : :
19040 : 321 : macro->count = count;
19041 : 321 : macro->kind = cmk_macro;
19042 : 321 : macro->imported_p = true;
19043 : :
19044 : 321 : bytes_in::bits_in bits = sec.stream_bits ();
19045 : 321 : macro->fun_like = bits.b ();
19046 : 321 : macro->variadic = bits.b ();
19047 : 321 : macro->syshdr = bits.b ();
19048 : 321 : bits.bflush ();
19049 : :
19050 : 321 : macro->line = read_location (sec);
19051 : :
19052 : 321 : if (macro->fun_like)
19053 : : {
19054 : 66 : unsigned paramc = sec.u ();
19055 : 66 : cpp_hashnode **params
19056 : 66 : = (cpp_hashnode **)ggc_alloc_atomic (sizeof (cpp_hashnode *) * paramc);
19057 : 66 : macro->paramc = paramc;
19058 : 66 : macro->parm.params = params;
19059 : 141 : for (unsigned ix = 0; ix != paramc; ix++)
19060 : 75 : params[ix] = sec.cpp_node ();
19061 : : }
19062 : :
19063 : : unsigned len = 0;
19064 : 1124 : for (unsigned ix = 0; ix != count && !sec.get_overrun (); ix++)
19065 : : {
19066 : 803 : cpp_token *token = ¯o->exp.tokens[ix];
19067 : 803 : token->src_loc = read_location (sec);
19068 : 803 : token->type = cpp_ttype (sec.u ());
19069 : 803 : token->flags = sec.u ();
19070 : 803 : switch (cpp_token_val_index (token))
19071 : : {
19072 : 0 : default:
19073 : 0 : sec.set_overrun ();
19074 : 0 : break;
19075 : :
19076 : 62 : case CPP_TOKEN_FLD_ARG_NO:
19077 : : /* An argument reference. */
19078 : 62 : {
19079 : 62 : unsigned arg_no = sec.u ();
19080 : 62 : if (arg_no - 1 >= macro->paramc)
19081 : 0 : sec.set_overrun ();
19082 : 62 : token->val.macro_arg.arg_no = arg_no;
19083 : 62 : token->val.macro_arg.spelling = sec.cpp_node ();
19084 : : }
19085 : 62 : break;
19086 : :
19087 : 209 : case CPP_TOKEN_FLD_NODE:
19088 : : /* An identifier. */
19089 : 209 : token->val.node.node = sec.cpp_node ();
19090 : 209 : token->val.node.spelling = sec.cpp_node ();
19091 : 209 : if (!token->val.node.spelling)
19092 : 209 : token->val.node.spelling = token->val.node.node;
19093 : : break;
19094 : :
19095 : : case CPP_TOKEN_FLD_NONE:
19096 : : break;
19097 : :
19098 : 180 : case CPP_TOKEN_FLD_STR:
19099 : : /* A string, number or comment. */
19100 : 180 : token->val.str.len = sec.u ();
19101 : 180 : len += token->val.str.len + 1;
19102 : 180 : break;
19103 : : }
19104 : : }
19105 : :
19106 : 321 : if (len)
19107 : 179 : if (const char *ptr = reinterpret_cast<const char *> (sec.buf (len)))
19108 : : {
19109 : : /* There should be a final NUL. */
19110 : 179 : if (ptr[len-1])
19111 : 0 : sec.set_overrun ();
19112 : : /* cpp_alloc_token_string will add a final NUL. */
19113 : 179 : const unsigned char *buf
19114 : 179 : = cpp_alloc_token_string (reader, (const unsigned char *)ptr, len - 1);
19115 : 179 : len = 0;
19116 : 661 : for (unsigned ix = 0; ix != count && !sec.get_overrun (); ix++)
19117 : : {
19118 : 482 : cpp_token *token = ¯o->exp.tokens[ix];
19119 : 482 : if (cpp_token_val_index (token) == CPP_TOKEN_FLD_STR)
19120 : : {
19121 : 180 : token->val.str.text = buf + len;
19122 : 180 : len += token->val.str.len;
19123 : 180 : if (buf[len++])
19124 : 0 : sec.set_overrun ();
19125 : : }
19126 : : }
19127 : : }
19128 : :
19129 : 321 : if (sec.get_overrun ())
19130 : 0 : return NULL;
19131 : : return macro;
19132 : 321 : }
19133 : :
19134 : : /* Exported macro data. */
19135 : : struct GTY(()) macro_export {
19136 : : cpp_macro *def;
19137 : : location_t undef_loc;
19138 : :
19139 : 96992 : macro_export ()
19140 : 96992 : :def (NULL), undef_loc (UNKNOWN_LOCATION)
19141 : : {
19142 : : }
19143 : : };
19144 : :
19145 : : /* Imported macro data. */
19146 : : class macro_import {
19147 : : public:
19148 : : struct slot {
19149 : : #if defined (WORDS_BIGENDIAN) && SIZEOF_VOID_P == 8
19150 : : int offset;
19151 : : #endif
19152 : : /* We need to ensure we don't use the LSB for representation, as
19153 : : that's the union discriminator below. */
19154 : : unsigned bits;
19155 : :
19156 : : #if !(defined (WORDS_BIGENDIAN) && SIZEOF_VOID_P == 8)
19157 : : int offset;
19158 : : #endif
19159 : :
19160 : : public:
19161 : : enum Layout {
19162 : : L_DEF = 1,
19163 : : L_UNDEF = 2,
19164 : : L_BOTH = 3,
19165 : : L_MODULE_SHIFT = 2
19166 : : };
19167 : :
19168 : : public:
19169 : : /* Not a regular ctor, because we put it in a union, and that's
19170 : : not allowed in C++ 98. */
19171 : 168687 : static slot ctor (unsigned module, unsigned defness)
19172 : : {
19173 : 168687 : gcc_checking_assert (defness);
19174 : 168687 : slot s;
19175 : 168687 : s.bits = defness | (module << L_MODULE_SHIFT);
19176 : 168687 : s.offset = -1;
19177 : 168687 : return s;
19178 : : }
19179 : :
19180 : : public:
19181 : 136469 : unsigned get_defness () const
19182 : : {
19183 : 136469 : return bits & L_BOTH;
19184 : : }
19185 : 98734 : unsigned get_module () const
19186 : : {
19187 : 98734 : return bits >> L_MODULE_SHIFT;
19188 : : }
19189 : 12 : void become_undef ()
19190 : : {
19191 : 12 : bits &= ~unsigned (L_DEF);
19192 : 12 : bits |= unsigned (L_UNDEF);
19193 : : }
19194 : : };
19195 : :
19196 : : private:
19197 : : typedef vec<slot, va_heap, vl_embed> ary_t;
19198 : : union either {
19199 : : /* Discriminated by bits 0|1 != 0. The expected case is that
19200 : : there will be exactly one slot per macro, hence the effort of
19201 : : packing that. */
19202 : : ary_t *ary;
19203 : : slot single;
19204 : : } u;
19205 : :
19206 : : public:
19207 : 134745 : macro_import ()
19208 : 134745 : {
19209 : 134745 : u.ary = NULL;
19210 : : }
19211 : :
19212 : : private:
19213 : 7402958 : bool single_p () const
19214 : : {
19215 : 7402958 : return u.single.bits & slot::L_BOTH;
19216 : : }
19217 : 7537712 : bool occupied_p () const
19218 : : {
19219 : 7537712 : return u.ary != NULL;
19220 : : }
19221 : :
19222 : : public:
19223 : 921 : unsigned length () const
19224 : : {
19225 : 921 : gcc_checking_assert (occupied_p ());
19226 : 921 : return single_p () ? 1 : u.ary->length ();
19227 : : }
19228 : 7271430 : slot &operator[] (unsigned ix)
19229 : : {
19230 : 7271430 : gcc_checking_assert (occupied_p ());
19231 : 7271430 : if (single_p ())
19232 : : {
19233 : 7185351 : gcc_checking_assert (!ix);
19234 : 7185351 : return u.single;
19235 : : }
19236 : : else
19237 : 86079 : return (*u.ary)[ix];
19238 : : }
19239 : :
19240 : : public:
19241 : : slot &exported ();
19242 : : slot &append (unsigned module, unsigned defness);
19243 : : };
19244 : :
19245 : : /* O is a new import to append to the list for. If we're an empty
19246 : : set, initialize us. */
19247 : :
19248 : : macro_import::slot &
19249 : 168687 : macro_import::append (unsigned module, unsigned defness)
19250 : : {
19251 : 168687 : if (!occupied_p ())
19252 : : {
19253 : 134745 : u.single = slot::ctor (module, defness);
19254 : 134745 : return u.single;
19255 : : }
19256 : : else
19257 : : {
19258 : 33942 : bool single = single_p ();
19259 : 33942 : ary_t *m = single ? NULL : u.ary;
19260 : 33942 : vec_safe_reserve (m, 1 + single);
19261 : 33942 : if (single)
19262 : 33939 : m->quick_push (u.single);
19263 : 33942 : u.ary = m;
19264 : 33942 : return *u.ary->quick_push (slot::ctor (module, defness));
19265 : : }
19266 : : }
19267 : :
19268 : : /* We're going to export something. Make sure the first import slot
19269 : : is us. */
19270 : :
19271 : : macro_import::slot &
19272 : 96674 : macro_import::exported ()
19273 : : {
19274 : 96674 : if (occupied_p () && !(*this)[0].get_module ())
19275 : : {
19276 : 9 : slot &res = (*this)[0];
19277 : 9 : res.bits |= slot::L_DEF;
19278 : 9 : return res;
19279 : : }
19280 : :
19281 : 96665 : slot *a = &append (0, slot::L_DEF);
19282 : 96665 : if (!single_p ())
19283 : : {
19284 : 29738 : slot &f = (*this)[0];
19285 : 29738 : std::swap (f, *a);
19286 : 29738 : a = &f;
19287 : : }
19288 : : return *a;
19289 : : }
19290 : :
19291 : : /* The import (&exported) macros. cpp_hasnode's deferred field
19292 : : indexes this array (offset by 1, so zero means 'not present'. */
19293 : :
19294 : : static vec<macro_import, va_heap, vl_embed> *macro_imports;
19295 : :
19296 : : /* The exported macros. A macro_import slot's zeroth element's offset
19297 : : indexes this array. If the zeroth slot is not for module zero,
19298 : : there is no export. */
19299 : :
19300 : : static GTY(()) vec<macro_export, va_gc> *macro_exports;
19301 : :
19302 : : /* The reachable set of header imports from this TU. */
19303 : :
19304 : : static GTY(()) bitmap headers;
19305 : :
19306 : : /* Get the (possibly empty) macro imports for NODE. */
19307 : :
19308 : : static macro_import &
19309 : 138958 : get_macro_imports (cpp_hashnode *node)
19310 : : {
19311 : 138958 : if (node->deferred)
19312 : 4213 : return (*macro_imports)[node->deferred - 1];
19313 : :
19314 : 134745 : vec_safe_reserve (macro_imports, 1);
19315 : 134745 : node->deferred = macro_imports->length () + 1;
19316 : 134745 : return *vec_safe_push (macro_imports, macro_import ());
19317 : : }
19318 : :
19319 : : /* Get the macro export for export EXP of NODE. */
19320 : :
19321 : : static macro_export &
19322 : 96674 : get_macro_export (macro_import::slot &slot)
19323 : : {
19324 : 96674 : if (slot.offset >= 0)
19325 : 9 : return (*macro_exports)[slot.offset];
19326 : :
19327 : 96665 : vec_safe_reserve (macro_exports, 1);
19328 : 96665 : slot.offset = macro_exports->length ();
19329 : 96665 : return *macro_exports->quick_push (macro_export ());
19330 : : }
19331 : :
19332 : : /* If NODE is an exportable macro, add it to the export set. */
19333 : :
19334 : : static int
19335 : 3754265 : maybe_add_macro (cpp_reader *, cpp_hashnode *node, void *data_)
19336 : : {
19337 : 3754265 : bool exporting = false;
19338 : :
19339 : 3754265 : if (cpp_user_macro_p (node))
19340 : 482140 : if (cpp_macro *macro = node->value.macro)
19341 : : /* Ignore imported, builtins, command line and forced header macros. */
19342 : 481705 : if (!macro->imported_p
19343 : 481705 : && !macro->lazy && macro->line >= spans.main_start ())
19344 : : {
19345 : 66936 : gcc_checking_assert (macro->kind == cmk_macro);
19346 : : /* I don't want to deal with this corner case, that I suspect is
19347 : : a devil's advocate reading of the standard. */
19348 : 66936 : gcc_checking_assert (!macro->extra_tokens);
19349 : :
19350 : 66936 : macro_import::slot &slot = get_macro_imports (node).exported ();
19351 : 66936 : macro_export &exp = get_macro_export (slot);
19352 : 66936 : exp.def = macro;
19353 : 66936 : exporting = true;
19354 : : }
19355 : :
19356 : 3687329 : if (!exporting && node->deferred)
19357 : : {
19358 : 576 : macro_import &imports = (*macro_imports)[node->deferred - 1];
19359 : 576 : macro_import::slot &slot = imports[0];
19360 : 576 : if (!slot.get_module ())
19361 : : {
19362 : 545 : gcc_checking_assert (slot.get_defness ());
19363 : : exporting = true;
19364 : : }
19365 : : }
19366 : :
19367 : 66936 : if (exporting)
19368 : 67481 : static_cast<vec<cpp_hashnode *> *> (data_)->safe_push (node);
19369 : :
19370 : 3754265 : return 1; /* Don't stop. */
19371 : : }
19372 : :
19373 : : /* Order cpp_hashnodes A_ and B_ by their exported macro locations. */
19374 : :
19375 : : static int
19376 : 3503850 : macro_loc_cmp (const void *a_, const void *b_)
19377 : : {
19378 : 3503850 : const cpp_hashnode *node_a = *(const cpp_hashnode *const *)a_;
19379 : 3503850 : macro_import &import_a = (*macro_imports)[node_a->deferred - 1];
19380 : 3503850 : const macro_export &export_a = (*macro_exports)[import_a[0].offset];
19381 : 3503850 : location_t loc_a = export_a.def ? export_a.def->line : export_a.undef_loc;
19382 : :
19383 : 3503850 : const cpp_hashnode *node_b = *(const cpp_hashnode *const *)b_;
19384 : 3503850 : macro_import &import_b = (*macro_imports)[node_b->deferred - 1];
19385 : 3503850 : const macro_export &export_b = (*macro_exports)[import_b[0].offset];
19386 : 3503850 : location_t loc_b = export_b.def ? export_b.def->line : export_b.undef_loc;
19387 : :
19388 : 3503850 : if (loc_a < loc_b)
19389 : : return +1;
19390 : 1800883 : else if (loc_a > loc_b)
19391 : : return -1;
19392 : : else
19393 : 0 : return 0;
19394 : : }
19395 : :
19396 : : /* Gather the macro definitions and undefinitions that we will need to
19397 : : write out. */
19398 : :
19399 : : vec<cpp_hashnode *> *
19400 : 867 : module_state::prepare_macros (cpp_reader *reader)
19401 : : {
19402 : 867 : vec<cpp_hashnode *> *macros;
19403 : 867 : vec_alloc (macros, 100);
19404 : :
19405 : 867 : cpp_forall_identifiers (reader, maybe_add_macro, macros);
19406 : :
19407 : 891 : dump (dumper::MACRO) && dump ("No more than %u macros", macros->length ());
19408 : :
19409 : 867 : macros->qsort (macro_loc_cmp);
19410 : :
19411 : : // Note the locations.
19412 : 69215 : for (unsigned ix = macros->length (); ix--;)
19413 : : {
19414 : 67481 : cpp_hashnode *node = (*macros)[ix];
19415 : 67481 : macro_import::slot &slot = (*macro_imports)[node->deferred - 1][0];
19416 : 67481 : macro_export &mac = (*macro_exports)[slot.offset];
19417 : :
19418 : 67481 : if (IDENTIFIER_KEYWORD_P (identifier (node)))
19419 : 1 : continue;
19420 : :
19421 : 67480 : if (mac.undef_loc != UNKNOWN_LOCATION)
19422 : 12 : note_location (mac.undef_loc);
19423 : 67480 : if (mac.def)
19424 : : {
19425 : 67474 : note_location (mac.def->line);
19426 : 221272 : for (unsigned ix = 0; ix != mac.def->count; ix++)
19427 : 153798 : note_location (mac.def->exp.tokens[ix].src_loc);
19428 : : }
19429 : : }
19430 : :
19431 : 867 : return macros;
19432 : : }
19433 : :
19434 : : /* Write out the exported defines. This is two sections, one
19435 : : containing the definitions, the other a table of node names. */
19436 : :
19437 : : unsigned
19438 : 867 : module_state::write_macros (elf_out *to, vec<cpp_hashnode *> *macros,
19439 : : unsigned *crc_p)
19440 : : {
19441 : 934 : dump () && dump ("Writing macros");
19442 : 867 : dump.indent ();
19443 : :
19444 : : /* Write the defs */
19445 : 867 : bytes_out sec (to);
19446 : 867 : sec.begin ();
19447 : :
19448 : 867 : unsigned count = 0;
19449 : 69215 : for (unsigned ix = macros->length (); ix--;)
19450 : : {
19451 : 67481 : cpp_hashnode *node = (*macros)[ix];
19452 : 67481 : macro_import::slot &slot = (*macro_imports)[node->deferred - 1][0];
19453 : 67481 : gcc_assert (!slot.get_module () && slot.get_defness ());
19454 : :
19455 : 67481 : macro_export &mac = (*macro_exports)[slot.offset];
19456 : 67481 : gcc_assert (!!(slot.get_defness () & macro_import::slot::L_UNDEF)
19457 : : == (mac.undef_loc != UNKNOWN_LOCATION)
19458 : : && !!(slot.get_defness () & macro_import::slot::L_DEF)
19459 : : == (mac.def != NULL));
19460 : :
19461 : 67481 : if (IDENTIFIER_KEYWORD_P (identifier (node)))
19462 : : {
19463 : 1 : warning_at (mac.def->line, 0,
19464 : : "not exporting %<#define %E%> as it is a keyword",
19465 : : identifier (node));
19466 : 1 : slot.offset = 0;
19467 : 1 : continue;
19468 : : }
19469 : :
19470 : 67480 : count++;
19471 : 67480 : slot.offset = sec.pos;
19472 : 67480 : dump (dumper::MACRO)
19473 : 24 : && dump ("Writing macro %s%s%s %I at %u",
19474 : 24 : slot.get_defness () & macro_import::slot::L_UNDEF
19475 : : ? "#undef" : "",
19476 : 24 : slot.get_defness () == macro_import::slot::L_BOTH
19477 : : ? " & " : "",
19478 : 24 : slot.get_defness () & macro_import::slot::L_DEF
19479 : : ? "#define" : "",
19480 : : identifier (node), slot.offset);
19481 : 67480 : if (mac.undef_loc != UNKNOWN_LOCATION)
19482 : 12 : write_location (sec, mac.undef_loc);
19483 : 67480 : if (mac.def)
19484 : 67474 : write_define (sec, mac.def);
19485 : : }
19486 : 867 : if (count)
19487 : : // We may have ended on a tokenless macro with a very short
19488 : : // location, that will cause problems reading its bit flags.
19489 : 145 : sec.u (0);
19490 : 867 : sec.end (to, to->name (MOD_SNAME_PFX ".def"), crc_p);
19491 : :
19492 : 867 : if (count)
19493 : : {
19494 : : /* Write the table. */
19495 : 145 : bytes_out sec (to);
19496 : 145 : sec.begin ();
19497 : 145 : sec.u (count);
19498 : :
19499 : 67770 : for (unsigned ix = macros->length (); ix--;)
19500 : : {
19501 : 67480 : const cpp_hashnode *node = (*macros)[ix];
19502 : 67480 : macro_import::slot &slot = (*macro_imports)[node->deferred - 1][0];
19503 : :
19504 : 67480 : if (slot.offset)
19505 : : {
19506 : 67480 : sec.cpp_node (node);
19507 : 67480 : sec.u (slot.get_defness ());
19508 : 67480 : sec.u (slot.offset);
19509 : : }
19510 : : }
19511 : 145 : sec.end (to, to->name (MOD_SNAME_PFX ".mac"), crc_p);
19512 : 145 : }
19513 : :
19514 : 867 : dump.outdent ();
19515 : 867 : return count;
19516 : 867 : }
19517 : :
19518 : : bool
19519 : 898 : module_state::read_macros ()
19520 : : {
19521 : : /* Get the def section. */
19522 : 898 : if (!slurp->macro_defs.begin (loc, from (), MOD_SNAME_PFX ".def"))
19523 : : return false;
19524 : :
19525 : : /* Get the tbl section, if there are defs. */
19526 : 898 : if (slurp->macro_defs.more_p ()
19527 : 898 : && !slurp->macro_tbl.begin (loc, from (), MOD_SNAME_PFX ".mac"))
19528 : : return false;
19529 : :
19530 : : return true;
19531 : : }
19532 : :
19533 : : /* Install the macro name table. */
19534 : :
19535 : : void
19536 : 904 : module_state::install_macros ()
19537 : : {
19538 : 904 : bytes_in &sec = slurp->macro_tbl;
19539 : 904 : if (!sec.size)
19540 : : return;
19541 : :
19542 : 203 : dump () && dump ("Reading macro table %M", this);
19543 : 181 : dump.indent ();
19544 : :
19545 : 181 : unsigned count = sec.u ();
19546 : 203 : dump () && dump ("%u macros", count);
19547 : 72203 : while (count--)
19548 : : {
19549 : 72022 : cpp_hashnode *node = sec.cpp_node ();
19550 : 72022 : macro_import &imp = get_macro_imports (node);
19551 : 72022 : unsigned flags = sec.u () & macro_import::slot::L_BOTH;
19552 : 72022 : if (!flags)
19553 : 0 : sec.set_overrun ();
19554 : :
19555 : 72022 : if (sec.get_overrun ())
19556 : : break;
19557 : :
19558 : 72022 : macro_import::slot &slot = imp.append (mod, flags);
19559 : 72022 : slot.offset = sec.u ();
19560 : :
19561 : 72022 : dump (dumper::MACRO)
19562 : 84 : && dump ("Read %s macro %s%s%s %I at %u",
19563 : 30 : imp.length () > 1 ? "add" : "new",
19564 : 27 : flags & macro_import::slot::L_UNDEF ? "#undef" : "",
19565 : : flags == macro_import::slot::L_BOTH ? " & " : "",
19566 : 30 : flags & macro_import::slot::L_DEF ? "#define" : "",
19567 : : identifier (node), slot.offset);
19568 : :
19569 : : /* We'll leak an imported definition's TOKEN_FLD_STR's data
19570 : : here. But that only happens when we've had to resolve the
19571 : : deferred macro before this import -- why are you doing
19572 : : that? */
19573 : 72022 : if (cpp_macro *cur = cpp_set_deferred_macro (node))
19574 : 29726 : if (!cur->imported_p)
19575 : : {
19576 : 29726 : macro_import::slot &slot = imp.exported ();
19577 : 29726 : macro_export &exp = get_macro_export (slot);
19578 : 29726 : exp.def = cur;
19579 : 101929 : dump (dumper::MACRO)
19580 : 0 : && dump ("Saving current #define %I", identifier (node));
19581 : : }
19582 : : }
19583 : :
19584 : : /* We're now done with the table. */
19585 : 181 : elf_in::release (slurp->from, sec);
19586 : :
19587 : 181 : dump.outdent ();
19588 : : }
19589 : :
19590 : : /* Import the transitive macros. */
19591 : :
19592 : : void
19593 : 862 : module_state::import_macros ()
19594 : : {
19595 : 862 : bitmap_ior_into (headers, slurp->headers);
19596 : :
19597 : 862 : bitmap_iterator bititer;
19598 : 862 : unsigned bitnum;
19599 : 1766 : EXECUTE_IF_SET_IN_BITMAP (slurp->headers, 0, bitnum, bititer)
19600 : 904 : (*modules)[bitnum]->install_macros ();
19601 : 862 : }
19602 : :
19603 : : /* NODE is being undefined at LOC. Record it in the export table, if
19604 : : necessary. */
19605 : :
19606 : : void
19607 : 201758 : module_state::undef_macro (cpp_reader *, location_t loc, cpp_hashnode *node)
19608 : : {
19609 : 201758 : if (!node->deferred)
19610 : : /* The macro is not imported, so our undef is irrelevant. */
19611 : : return;
19612 : :
19613 : 12 : unsigned n = dump.push (NULL);
19614 : :
19615 : 12 : macro_import::slot &slot = (*macro_imports)[node->deferred - 1].exported ();
19616 : 12 : macro_export &exp = get_macro_export (slot);
19617 : :
19618 : 12 : exp.undef_loc = loc;
19619 : 12 : slot.become_undef ();
19620 : 12 : exp.def = NULL;
19621 : :
19622 : 18 : dump (dumper::MACRO) && dump ("Recording macro #undef %I", identifier (node));
19623 : :
19624 : 12 : dump.pop (n);
19625 : : }
19626 : :
19627 : : /* NODE is a deferred macro node. Determine the definition and return
19628 : : it, with NULL if undefined. May issue diagnostics.
19629 : :
19630 : : This can leak memory, when merging declarations -- the string
19631 : : contents (TOKEN_FLD_STR) of each definition are allocated in
19632 : : unreclaimable cpp objstack. Only one will win. However, I do not
19633 : : expect this to be common -- mostly macros have a single point of
19634 : : definition. Perhaps we could restore the objstack to its position
19635 : : after the first imported definition (if that wins)? The macros
19636 : : themselves are GC'd. */
19637 : :
19638 : : cpp_macro *
19639 : 297 : module_state::deferred_macro (cpp_reader *reader, location_t loc,
19640 : : cpp_hashnode *node)
19641 : : {
19642 : 297 : macro_import &imports = (*macro_imports)[node->deferred - 1];
19643 : :
19644 : 297 : unsigned n = dump.push (NULL);
19645 : 303 : dump (dumper::MACRO) && dump ("Deferred macro %I", identifier (node));
19646 : :
19647 : 297 : bitmap visible (BITMAP_GGC_ALLOC ());
19648 : :
19649 : 297 : if (!((imports[0].get_defness () & macro_import::slot::L_UNDEF)
19650 : 0 : && !imports[0].get_module ()))
19651 : : {
19652 : : /* Calculate the set of visible header imports. */
19653 : 297 : bitmap_copy (visible, headers);
19654 : 762 : for (unsigned ix = imports.length (); ix--;)
19655 : : {
19656 : 465 : const macro_import::slot &slot = imports[ix];
19657 : 465 : unsigned mod = slot.get_module ();
19658 : 465 : if ((slot.get_defness () & macro_import::slot::L_UNDEF)
19659 : 465 : && bitmap_bit_p (visible, mod))
19660 : : {
19661 : 12 : bitmap arg = mod ? (*modules)[mod]->slurp->headers : headers;
19662 : 12 : bitmap_and_compl_into (visible, arg);
19663 : 12 : bitmap_set_bit (visible, mod);
19664 : : }
19665 : : }
19666 : : }
19667 : 297 : bitmap_set_bit (visible, 0);
19668 : :
19669 : : /* Now find the macros that are still visible. */
19670 : 297 : bool failed = false;
19671 : 297 : cpp_macro *def = NULL;
19672 : 297 : vec<macro_export> defs;
19673 : 297 : defs.create (imports.length ());
19674 : 762 : for (unsigned ix = imports.length (); ix--;)
19675 : : {
19676 : 465 : const macro_import::slot &slot = imports[ix];
19677 : 465 : unsigned mod = slot.get_module ();
19678 : 465 : if (bitmap_bit_p (visible, mod))
19679 : : {
19680 : 453 : macro_export *pushed = NULL;
19681 : 453 : if (mod)
19682 : : {
19683 : 327 : const module_state *imp = (*modules)[mod];
19684 : 327 : bytes_in &sec = imp->slurp->macro_defs;
19685 : 327 : if (!sec.get_overrun ())
19686 : : {
19687 : 327 : dump (dumper::MACRO)
19688 : 6 : && dump ("Reading macro %s%s%s %I module %M at %u",
19689 : 6 : slot.get_defness () & macro_import::slot::L_UNDEF
19690 : : ? "#undef" : "",
19691 : 6 : slot.get_defness () == macro_import::slot::L_BOTH
19692 : : ? " & " : "",
19693 : 6 : slot.get_defness () & macro_import::slot::L_DEF
19694 : : ? "#define" : "",
19695 : 6 : identifier (node), imp, slot.offset);
19696 : 327 : sec.random_access (slot.offset);
19697 : :
19698 : 327 : macro_export exp;
19699 : 327 : if (slot.get_defness () & macro_import::slot::L_UNDEF)
19700 : 12 : exp.undef_loc = imp->read_location (sec);
19701 : 327 : if (slot.get_defness () & macro_import::slot::L_DEF)
19702 : 321 : exp.def = imp->read_define (sec, reader);
19703 : 327 : if (sec.get_overrun ())
19704 : 0 : error_at (loc, "macro definitions of %qE corrupted",
19705 : 0 : imp->name);
19706 : : else
19707 : 327 : pushed = defs.quick_push (exp);
19708 : : }
19709 : : }
19710 : : else
19711 : 126 : pushed = defs.quick_push ((*macro_exports)[slot.offset]);
19712 : 453 : if (pushed && pushed->def)
19713 : : {
19714 : 447 : if (!def)
19715 : : def = pushed->def;
19716 : 153 : else if (cpp_compare_macros (def, pushed->def))
19717 : 465 : failed = true;
19718 : : }
19719 : : }
19720 : : }
19721 : :
19722 : 297 : if (failed)
19723 : : {
19724 : : /* If LOC is the first loc, this is the end of file check, which
19725 : : is a warning. */
19726 : 15 : auto_diagnostic_group d;
19727 : 15 : if (loc == MAP_START_LOCATION (LINEMAPS_ORDINARY_MAP_AT (line_table, 0)))
19728 : 9 : warning_at (loc, OPT_Winvalid_imported_macros,
19729 : : "inconsistent imported macro definition %qE",
19730 : : identifier (node));
19731 : : else
19732 : 6 : error_at (loc, "inconsistent imported macro definition %qE",
19733 : : identifier (node));
19734 : 60 : for (unsigned ix = defs.length (); ix--;)
19735 : : {
19736 : 30 : macro_export &exp = defs[ix];
19737 : 30 : if (exp.undef_loc)
19738 : 0 : inform (exp.undef_loc, "%<#undef %E%>", identifier (node));
19739 : 30 : if (exp.def)
19740 : 30 : inform (exp.def->line, "%<#define %s%>",
19741 : : cpp_macro_definition (reader, node, exp.def));
19742 : : }
19743 : 15 : def = NULL;
19744 : 15 : }
19745 : :
19746 : 297 : defs.release ();
19747 : :
19748 : 297 : dump.pop (n);
19749 : :
19750 : 297 : return def;
19751 : : }
19752 : :
19753 : : /* Stream the static aggregates. Sadly some headers (ahem:
19754 : : iostream) contain static vars, and rely on them to run global
19755 : : ctors. */
19756 : : unsigned
19757 : 867 : module_state::write_inits (elf_out *to, depset::hash &table, unsigned *crc_ptr)
19758 : : {
19759 : 867 : if (!static_aggregates && !tls_aggregates)
19760 : : return 0;
19761 : :
19762 : 45 : dump () && dump ("Writing initializers");
19763 : 45 : dump.indent ();
19764 : :
19765 : 45 : static_aggregates = nreverse (static_aggregates);
19766 : 45 : tls_aggregates = nreverse (tls_aggregates);
19767 : :
19768 : 45 : unsigned count = 0;
19769 : 45 : trees_out sec (to, this, table, ~0u);
19770 : 45 : sec.begin ();
19771 : :
19772 : 45 : tree list = static_aggregates;
19773 : 135 : for (int passes = 0; passes != 2; passes++)
19774 : : {
19775 : 258 : for (tree init = list; init; init = TREE_CHAIN (init))
19776 : 168 : if (TREE_LANG_FLAG_0 (init))
19777 : : {
19778 : 144 : if (STATIC_INIT_DECOMP_BASE_P (init))
19779 : : {
19780 : : /* Ensure that in the returned result chain if the
19781 : : STATIC_INIT_DECOMP_*BASE_P flags are set, there is
19782 : : always one or more STATIC_INIT_DECOMP_BASE_P TREE_LIST
19783 : : followed by one or more STATIC_INIT_DECOMP_NONBASE_P. */
19784 : 21 : int phase = 0;
19785 : 21 : tree last = NULL_TREE;
19786 : 21 : for (tree init2 = TREE_CHAIN (init);
19787 : 126 : init2; init2 = TREE_CHAIN (init2))
19788 : : {
19789 : 147 : if (phase == 0 && STATIC_INIT_DECOMP_BASE_P (init2))
19790 : : ;
19791 : 126 : else if (phase == 0
19792 : 147 : && STATIC_INIT_DECOMP_NONBASE_P (init2))
19793 : : {
19794 : 120 : phase = TREE_LANG_FLAG_0 (init2) ? 2 : 1;
19795 : : last = init2;
19796 : : }
19797 : 105 : else if (IN_RANGE (phase, 1, 2)
19798 : 210 : && STATIC_INIT_DECOMP_NONBASE_P (init2))
19799 : : {
19800 : 84 : if (TREE_LANG_FLAG_0 (init2))
19801 : 81 : phase = 2;
19802 : : last = init2;
19803 : : }
19804 : : else
19805 : : break;
19806 : : }
19807 : 21 : if (phase == 2)
19808 : : {
19809 : : /* In that case, add markers about it so that the
19810 : : STATIC_INIT_DECOMP_BASE_P and
19811 : : STATIC_INIT_DECOMP_NONBASE_P flags can be restored. */
19812 : 21 : sec.tree_node (build_int_cst (integer_type_node,
19813 : 21 : 2 * passes + 1));
19814 : 21 : phase = 1;
19815 : 147 : for (tree init2 = init; init2 != TREE_CHAIN (last);
19816 : 126 : init2 = TREE_CHAIN (init2))
19817 : 126 : if (TREE_LANG_FLAG_0 (init2))
19818 : : {
19819 : 102 : tree decl = TREE_VALUE (init2);
19820 : 102 : if (phase == 1
19821 : 102 : && STATIC_INIT_DECOMP_NONBASE_P (init2))
19822 : : {
19823 : 21 : sec.tree_node (build_int_cst (integer_type_node,
19824 : 21 : 2 * passes + 2));
19825 : 21 : phase = 2;
19826 : : }
19827 : 102 : dump ("Initializer:%u for %N", count, decl);
19828 : 102 : sec.tree_node (decl);
19829 : 102 : ++count;
19830 : : }
19831 : 21 : sec.tree_node (integer_zero_node);
19832 : 21 : init = last;
19833 : 21 : continue;
19834 : 21 : }
19835 : : }
19836 : :
19837 : 123 : tree decl = TREE_VALUE (init);
19838 : :
19839 : 123 : dump ("Initializer:%u for %N", count, decl);
19840 : 123 : sec.tree_node (decl);
19841 : 123 : ++count;
19842 : : }
19843 : :
19844 : 90 : list = tls_aggregates;
19845 : : }
19846 : :
19847 : 45 : sec.end (to, to->name (MOD_SNAME_PFX ".ini"), crc_ptr);
19848 : 45 : dump.outdent ();
19849 : :
19850 : 45 : return count;
19851 : 45 : }
19852 : :
19853 : : /* We have to defer some post-load processing until we've completed
19854 : : reading, because they can cause more reading. */
19855 : :
19856 : : static void
19857 : 8125 : post_load_processing ()
19858 : : {
19859 : : /* We mustn't cause a GC, our caller should have arranged for that
19860 : : not to happen. */
19861 : 8125 : gcc_checking_assert (function_depth);
19862 : :
19863 : 8125 : if (!post_load_decls)
19864 : : return;
19865 : :
19866 : 3959 : tree old_cfd = current_function_decl;
19867 : 3959 : struct function *old_cfun = cfun;
19868 : 9356 : while (post_load_decls->length ())
19869 : : {
19870 : 5397 : tree decl = post_load_decls->pop ();
19871 : :
19872 : 5452 : dump () && dump ("Post-load processing of %N", decl);
19873 : :
19874 : 5397 : gcc_checking_assert (DECL_MAYBE_IN_CHARGE_CDTOR_P (decl));
19875 : 5397 : expand_or_defer_fn (decl);
19876 : : /* As in module_state::read_cluster. */
19877 : 511 : if (at_eof && DECL_COMDAT (decl) && DECL_EXTERNAL (decl)
19878 : 5427 : && DECL_NOT_REALLY_EXTERN (decl))
19879 : 8 : DECL_EXTERNAL (decl) = false;
19880 : : }
19881 : :
19882 : 3959 : cfun = old_cfun;
19883 : 3959 : current_function_decl = old_cfd;
19884 : : }
19885 : :
19886 : : bool
19887 : 45 : module_state::read_inits (unsigned count)
19888 : : {
19889 : 45 : trees_in sec (this);
19890 : 45 : if (!sec.begin (loc, from (), from ()->find (MOD_SNAME_PFX ".ini")))
19891 : : return false;
19892 : 57 : dump () && dump ("Reading %u initializers", count);
19893 : 45 : dump.indent ();
19894 : :
19895 : 45 : lazy_snum = ~0u;
19896 : 45 : int decomp_phase = 0;
19897 : 45 : tree *aggrp = NULL;
19898 : 270 : for (unsigned ix = 0; ix != count; ix++)
19899 : : {
19900 : 225 : tree last = NULL_TREE;
19901 : 225 : if (decomp_phase)
19902 : 102 : last = *aggrp;
19903 : : /* Merely referencing the decl causes its initializer to be read
19904 : : and added to the correct list. */
19905 : 225 : tree decl = sec.tree_node ();
19906 : : /* module_state::write_inits can add special INTEGER_CST markers in
19907 : : between the decls. 1 means STATIC_INIT_DECOMP_BASE_P entries
19908 : : follow in static_aggregates, 2 means STATIC_INIT_DECOMP_NONBASE_P
19909 : : entries follow in static_aggregates, 3 means
19910 : : STATIC_INIT_DECOMP_BASE_P entries follow in tls_aggregates,
19911 : : 4 means STATIC_INIT_DECOMP_NONBASE_P follow in tls_aggregates,
19912 : : 0 means end of STATIC_INIT_DECOMP_{,NON}BASE_P sequence. */
19913 : 225 : if (tree_fits_shwi_p (decl))
19914 : : {
19915 : 63 : if (sec.get_overrun ())
19916 : : break;
19917 : 63 : decomp_phase = tree_to_shwi (decl);
19918 : 63 : if (decomp_phase)
19919 : : {
19920 : 42 : aggrp = decomp_phase > 2 ? &tls_aggregates : &static_aggregates;
19921 : : last = *aggrp;
19922 : : }
19923 : 63 : decl = sec.tree_node ();
19924 : : }
19925 : :
19926 : 225 : if (sec.get_overrun ())
19927 : : break;
19928 : 225 : if (decl)
19929 : 225 : dump ("Initializer:%u for %N", ix, decl);
19930 : 225 : if (decomp_phase)
19931 : : {
19932 : 102 : tree init = *aggrp;
19933 : 102 : gcc_assert (TREE_VALUE (init) == decl && TREE_CHAIN (init) == last);
19934 : 102 : if ((decomp_phase & 1) != 0)
19935 : 21 : STATIC_INIT_DECOMP_BASE_P (init) = 1;
19936 : : else
19937 : 81 : STATIC_INIT_DECOMP_NONBASE_P (init) = 1;
19938 : : }
19939 : : }
19940 : 45 : if (decomp_phase && !sec.get_overrun ())
19941 : : {
19942 : 0 : tree decl = sec.tree_node ();
19943 : 0 : gcc_assert (integer_zerop (decl));
19944 : : }
19945 : 45 : lazy_snum = 0;
19946 : 45 : post_load_processing ();
19947 : 45 : dump.outdent ();
19948 : 45 : if (!sec.end (from ()))
19949 : : return false;
19950 : : return true;
19951 : 45 : }
19952 : :
19953 : : void
19954 : 2484 : module_state::write_counts (elf_out *to, unsigned counts[MSC_HWM],
19955 : : unsigned *crc_ptr)
19956 : : {
19957 : 2484 : bytes_out cfg (to);
19958 : :
19959 : 2484 : cfg.begin ();
19960 : :
19961 : 24840 : for (unsigned ix = MSC_HWM; ix--;)
19962 : 22356 : cfg.u (counts[ix]);
19963 : :
19964 : 2484 : if (dump ())
19965 : : {
19966 : 273 : dump ("Cluster sections are [%u,%u)",
19967 : : counts[MSC_sec_lwm], counts[MSC_sec_hwm]);
19968 : 273 : dump ("Bindings %u", counts[MSC_bindings]);
19969 : 273 : dump ("Pendings %u", counts[MSC_pendings]);
19970 : 273 : dump ("Entities %u", counts[MSC_entities]);
19971 : 273 : dump ("Namespaces %u", counts[MSC_namespaces]);
19972 : 273 : dump ("Using-directives %u", counts[MSC_using_directives]);
19973 : 273 : dump ("Macros %u", counts[MSC_macros]);
19974 : 273 : dump ("Initializers %u", counts[MSC_inits]);
19975 : : }
19976 : :
19977 : 2484 : cfg.end (to, to->name (MOD_SNAME_PFX ".cnt"), crc_ptr);
19978 : 2484 : }
19979 : :
19980 : : bool
19981 : 2665 : module_state::read_counts (unsigned counts[MSC_HWM])
19982 : : {
19983 : 2665 : bytes_in cfg;
19984 : :
19985 : 2665 : if (!cfg.begin (loc, from (), MOD_SNAME_PFX ".cnt"))
19986 : : return false;
19987 : :
19988 : 26650 : for (unsigned ix = MSC_HWM; ix--;)
19989 : 23985 : counts[ix] = cfg.u ();
19990 : :
19991 : 2665 : if (dump ())
19992 : : {
19993 : 490 : dump ("Declaration sections are [%u,%u)",
19994 : : counts[MSC_sec_lwm], counts[MSC_sec_hwm]);
19995 : 490 : dump ("Bindings %u", counts[MSC_bindings]);
19996 : 490 : dump ("Pendings %u", counts[MSC_pendings]);
19997 : 490 : dump ("Entities %u", counts[MSC_entities]);
19998 : 490 : dump ("Namespaces %u", counts[MSC_namespaces]);
19999 : 490 : dump ("Using-directives %u", counts[MSC_using_directives]);
20000 : 490 : dump ("Macros %u", counts[MSC_macros]);
20001 : 490 : dump ("Initializers %u", counts[MSC_inits]);
20002 : : }
20003 : :
20004 : 2665 : return cfg.end (from ());
20005 : 2665 : }
20006 : :
20007 : : /* Tool configuration: MOD_SNAME_PFX .config
20008 : :
20009 : : This is data that confirms current state (or fails). */
20010 : :
20011 : : void
20012 : 2484 : module_state::write_config (elf_out *to, module_state_config &config,
20013 : : unsigned inner_crc)
20014 : : {
20015 : 2484 : bytes_out cfg (to);
20016 : :
20017 : 2484 : cfg.begin ();
20018 : :
20019 : : /* Write version and inner crc as u32 values, for easier
20020 : : debug inspection. */
20021 : 2757 : dump () && dump ("Writing version=%V, inner_crc=%x",
20022 : : MODULE_VERSION, inner_crc);
20023 : 2484 : cfg.u32 (unsigned (MODULE_VERSION));
20024 : 2484 : cfg.u32 (inner_crc);
20025 : :
20026 : 2484 : cfg.u (to->name (is_header () ? "" : get_flatname ()));
20027 : :
20028 : : /* Configuration. */
20029 : 2757 : dump () && dump ("Writing target='%s', host='%s'",
20030 : : TARGET_MACHINE, HOST_MACHINE);
20031 : 2484 : unsigned target = to->name (TARGET_MACHINE);
20032 : 2484 : unsigned host = (!strcmp (TARGET_MACHINE, HOST_MACHINE)
20033 : : ? target : to->name (HOST_MACHINE));
20034 : 2484 : cfg.u (target);
20035 : 2484 : cfg.u (host);
20036 : :
20037 : 2484 : cfg.str (config.dialect_str);
20038 : 2484 : cfg.u (extensions);
20039 : :
20040 : : /* Global tree information. We write the globals crc separately,
20041 : : rather than mix it directly into the overall crc, as it is used
20042 : : to ensure data match between instances of the compiler, not
20043 : : integrity of the file. */
20044 : 2757 : dump () && dump ("Writing globals=%u, crc=%x",
20045 : : fixed_trees->length (), global_crc);
20046 : 2484 : cfg.u (fixed_trees->length ());
20047 : 2484 : cfg.u32 (global_crc);
20048 : :
20049 : 2484 : if (is_partition ())
20050 : 166 : cfg.u (is_interface ());
20051 : :
20052 : 2484 : cfg.u (config.num_imports);
20053 : 2484 : cfg.u (config.num_partitions);
20054 : 2484 : cfg.u (config.num_entities);
20055 : :
20056 : 2484 : cfg.loc (config.ordinary_locs);
20057 : 2484 : cfg.loc (config.macro_locs);
20058 : 2484 : cfg.u (config.loc_range_bits);
20059 : :
20060 : 2484 : cfg.u (config.active_init);
20061 : :
20062 : : /* Now generate CRC, we'll have incorporated the inner CRC because
20063 : : of its serialization above. */
20064 : 2484 : cfg.end (to, to->name (MOD_SNAME_PFX ".cfg"), &crc);
20065 : 2757 : dump () && dump ("Writing CRC=%x", crc);
20066 : 2484 : }
20067 : :
20068 : : void
20069 : 40 : module_state::note_cmi_name ()
20070 : : {
20071 : 40 : if (!cmi_noted_p && filename)
20072 : : {
20073 : 40 : cmi_noted_p = true;
20074 : 40 : inform (loc, "compiled module file is %qs",
20075 : : maybe_add_cmi_prefix (filename));
20076 : : }
20077 : 40 : }
20078 : :
20079 : : bool
20080 : 2729 : module_state::read_config (module_state_config &config)
20081 : : {
20082 : 2729 : bytes_in cfg;
20083 : :
20084 : 2729 : if (!cfg.begin (loc, from (), MOD_SNAME_PFX ".cfg"))
20085 : : return false;
20086 : :
20087 : : /* Check version. */
20088 : 2729 : unsigned my_ver = MODULE_VERSION;
20089 : 2729 : unsigned their_ver = cfg.u32 ();
20090 : 3219 : dump () && dump (my_ver == their_ver ? "Version %V"
20091 : : : "Expecting %V found %V", my_ver, their_ver);
20092 : 2729 : if (their_ver != my_ver)
20093 : : {
20094 : : /* The compiler versions differ. Close enough? */
20095 : 0 : verstr_t my_string, their_string;
20096 : :
20097 : 0 : version2string (my_ver, my_string);
20098 : 0 : version2string (their_ver, their_string);
20099 : :
20100 : : /* Reject when either is non-experimental or when experimental
20101 : : major versions differ. */
20102 : 0 : auto_diagnostic_group d;
20103 : 0 : bool reject_p = ((!IS_EXPERIMENTAL (my_ver)
20104 : : || !IS_EXPERIMENTAL (their_ver)
20105 : 0 : || MODULE_MAJOR (my_ver) != MODULE_MAJOR (their_ver))
20106 : : /* The 'I know what I'm doing' switch. */
20107 : 0 : && !flag_module_version_ignore);
20108 : 0 : bool inform_p = true;
20109 : 0 : if (reject_p)
20110 : : {
20111 : 0 : cfg.set_overrun ();
20112 : 0 : error_at (loc, "compiled module is %sversion %s",
20113 : : IS_EXPERIMENTAL (their_ver) ? "experimental " : "",
20114 : : their_string);
20115 : : }
20116 : : else
20117 : 0 : inform_p = warning_at (loc, 0, "compiled module is %sversion %s",
20118 : : IS_EXPERIMENTAL (their_ver) ? "experimental " : "",
20119 : : their_string);
20120 : :
20121 : 0 : if (inform_p)
20122 : : {
20123 : 0 : inform (loc, "compiler is %sversion %s%s%s",
20124 : : IS_EXPERIMENTAL (my_ver) ? "experimental " : "",
20125 : : my_string,
20126 : 0 : reject_p ? "" : flag_module_version_ignore
20127 : 0 : ? ", be it on your own head!" : ", close enough?",
20128 : : reject_p ? "" : " \xc2\xaf\\_(\xe3\x83\x84)_/\xc2\xaf");
20129 : 0 : note_cmi_name ();
20130 : : }
20131 : :
20132 : 0 : if (reject_p)
20133 : 0 : goto done;
20134 : 0 : }
20135 : :
20136 : : /* We wrote the inner crc merely to merge it, so simply read it
20137 : : back and forget it. */
20138 : 2729 : cfg.u32 ();
20139 : :
20140 : : /* Check module name. */
20141 : 2729 : {
20142 : 2729 : const char *their_name = from ()->name (cfg.u ());
20143 : 2729 : const char *our_name = "";
20144 : :
20145 : 2729 : if (!is_header ())
20146 : 1777 : our_name = get_flatname ();
20147 : :
20148 : : /* Header units can be aliased, so name checking is
20149 : : inappropriate. */
20150 : 2729 : if (0 != strcmp (their_name, our_name))
20151 : : {
20152 : 0 : error_at (loc,
20153 : 0 : their_name[0] && our_name[0] ? G_("module %qs found")
20154 : : : their_name[0]
20155 : : ? G_("header module expected, module %qs found")
20156 : : : G_("module %qs expected, header module found"),
20157 : 0 : their_name[0] ? their_name : our_name);
20158 : 0 : cfg.set_overrun ();
20159 : 0 : goto done;
20160 : : }
20161 : : }
20162 : :
20163 : : /* Check the CRC after the above sanity checks, so that the user is
20164 : : clued in. */
20165 : 2729 : {
20166 : 2729 : unsigned e_crc = crc;
20167 : 2729 : crc = cfg.get_crc ();
20168 : 3219 : dump () && dump ("Reading CRC=%x", crc);
20169 : 2729 : if (!is_direct () && crc != e_crc)
20170 : : {
20171 : 3 : error_at (loc, "module %qs CRC mismatch", get_flatname ());
20172 : 3 : cfg.set_overrun ();
20173 : 3 : goto done;
20174 : : }
20175 : : }
20176 : :
20177 : : /* Check target & host. */
20178 : 2726 : {
20179 : 2726 : const char *their_target = from ()->name (cfg.u ());
20180 : 2726 : const char *their_host = from ()->name (cfg.u ());
20181 : 3216 : dump () && dump ("Read target='%s', host='%s'", their_target, their_host);
20182 : 2726 : if (strcmp (their_target, TARGET_MACHINE)
20183 : 2726 : || strcmp (their_host, HOST_MACHINE))
20184 : : {
20185 : 0 : error_at (loc, "target & host is %qs:%qs, expected %qs:%qs",
20186 : : their_target, TARGET_MACHINE, their_host, HOST_MACHINE);
20187 : 0 : cfg.set_overrun ();
20188 : 0 : goto done;
20189 : : }
20190 : : }
20191 : :
20192 : : /* Check compilation dialect. This must match. */
20193 : 2726 : {
20194 : 2726 : const char *their_dialect = cfg.str ();
20195 : 2726 : if (strcmp (their_dialect, config.dialect_str))
20196 : : {
20197 : 1 : error_at (loc, "language dialect differs %qs, expected %qs",
20198 : : their_dialect, config.dialect_str);
20199 : 1 : cfg.set_overrun ();
20200 : 1 : goto done;
20201 : : }
20202 : : }
20203 : :
20204 : : /* Check for extensions. If they set any, we must have them set
20205 : : too. */
20206 : 2725 : {
20207 : 2725 : unsigned ext = cfg.u ();
20208 : 2725 : unsigned allowed = (flag_openmp ? SE_OPENMP | SE_OPENMP_SIMD : 0);
20209 : 2725 : if (flag_openmp_simd)
20210 : 3 : allowed |= SE_OPENMP_SIMD;
20211 : 2725 : if (flag_openacc)
20212 : 3 : allowed |= SE_OPENACC;
20213 : :
20214 : 2725 : if (unsigned bad = ext & ~allowed)
20215 : : {
20216 : 9 : if (bad & SE_OPENMP)
20217 : 3 : error_at (loc, "module contains OpenMP, use %<-fopenmp%> to enable");
20218 : 6 : else if (bad & SE_OPENMP_SIMD)
20219 : 3 : error_at (loc, "module contains OpenMP, use %<-fopenmp%> or "
20220 : : "%<-fopenmp-simd%> to enable");
20221 : 9 : if (bad & SE_OPENACC)
20222 : 3 : error_at (loc, "module contains OpenACC, use %<-fopenacc%> to "
20223 : : "enable");
20224 : 9 : cfg.set_overrun ();
20225 : 9 : goto done;
20226 : : }
20227 : 2716 : extensions = ext;
20228 : : }
20229 : :
20230 : : /* Check global trees. */
20231 : 2716 : {
20232 : 2716 : unsigned their_fixed_length = cfg.u ();
20233 : 2716 : unsigned their_fixed_crc = cfg.u32 ();
20234 : 3206 : dump () && dump ("Read globals=%u, crc=%x",
20235 : : their_fixed_length, their_fixed_crc);
20236 : 2716 : if (!flag_preprocess_only
20237 : 2716 : && (their_fixed_length != fixed_trees->length ()
20238 : 2677 : || their_fixed_crc != global_crc))
20239 : : {
20240 : 0 : error_at (loc, "fixed tree mismatch");
20241 : 0 : cfg.set_overrun ();
20242 : 0 : goto done;
20243 : : }
20244 : : }
20245 : :
20246 : : /* All non-partitions are interfaces. */
20247 : 2716 : interface_p = !is_partition () || cfg.u ();
20248 : :
20249 : 2716 : config.num_imports = cfg.u ();
20250 : 2716 : config.num_partitions = cfg.u ();
20251 : 2716 : config.num_entities = cfg.u ();
20252 : :
20253 : 2716 : config.ordinary_locs = cfg.loc ();
20254 : 2716 : config.macro_locs = cfg.loc ();
20255 : 2716 : config.loc_range_bits = cfg.u ();
20256 : :
20257 : 2716 : config.active_init = cfg.u ();
20258 : :
20259 : 2729 : done:
20260 : 2729 : return cfg.end (from ());
20261 : 2729 : }
20262 : :
20263 : : /* Comparator for ordering the Ordered Ordinary Location array. */
20264 : :
20265 : : static int
20266 : 108 : ool_cmp (const void *a_, const void *b_)
20267 : : {
20268 : 108 : auto *a = *static_cast<const module_state *const *> (a_);
20269 : 108 : auto *b = *static_cast<const module_state *const *> (b_);
20270 : 108 : if (a == b)
20271 : : return 0;
20272 : 108 : else if (a->ordinary_locs.first < b->ordinary_locs.first)
20273 : : return -1;
20274 : : else
20275 : 44 : return +1;
20276 : : }
20277 : :
20278 : : /* Use ELROND format to record the following sections:
20279 : : qualified-names : binding value(s)
20280 : : MOD_SNAME_PFX.README : human readable, strings
20281 : : MOD_SNAME_PFX.ENV : environment strings, strings
20282 : : MOD_SNAME_PFX.nms : namespace hierarchy
20283 : : MOD_SNAME_PFX.udi : namespace using-directives
20284 : : MOD_SNAME_PFX.bnd : binding table
20285 : : MOD_SNAME_PFX.spc : specialization table
20286 : : MOD_SNAME_PFX.imp : import table
20287 : : MOD_SNAME_PFX.ent : entity table
20288 : : MOD_SNAME_PFX.prt : partitions table
20289 : : MOD_SNAME_PFX.olm : ordinary line maps
20290 : : MOD_SNAME_PFX.mlm : macro line maps
20291 : : MOD_SNAME_PFX.def : macro definitions
20292 : : MOD_SNAME_PFX.mac : macro index
20293 : : MOD_SNAME_PFX.ini : inits
20294 : : MOD_SNAME_PFX.cnt : counts
20295 : : MOD_SNAME_PFX.cfg : config data
20296 : : */
20297 : :
20298 : : bool
20299 : 2513 : module_state::write_begin (elf_out *to, cpp_reader *reader,
20300 : : module_state_config &config, unsigned &crc)
20301 : : {
20302 : : /* Figure out remapped module numbers, which might elide
20303 : : partitions. */
20304 : 2513 : bitmap partitions = NULL;
20305 : 2513 : if (!is_header () && !is_partition ())
20306 : 1480 : partitions = BITMAP_GGC_ALLOC ();
20307 : 2513 : write_init_maps ();
20308 : :
20309 : 2513 : unsigned mod_hwm = 1;
20310 : 3103 : for (unsigned ix = 1; ix != modules->length (); ix++)
20311 : : {
20312 : 590 : module_state *imp = (*modules)[ix];
20313 : :
20314 : : /* Promote any non-partition direct import from a partition, unless
20315 : : we're a partition. */
20316 : 539 : if (!is_partition () && !imp->is_partition ()
20317 : 969 : && imp->is_partition_direct ())
20318 : 9 : imp->directness = MD_PURVIEW_DIRECT;
20319 : :
20320 : : /* Write any import that is not a partition, unless we're a
20321 : : partition. */
20322 : 590 : if (!partitions || !imp->is_partition ())
20323 : 430 : imp->remap = mod_hwm++;
20324 : : else
20325 : : {
20326 : 190 : dump () && dump ("Partition %M %u", imp, ix);
20327 : 160 : bitmap_set_bit (partitions, ix);
20328 : 160 : imp->remap = 0;
20329 : : /* All interface partitions must be exported. */
20330 : 160 : if (imp->is_interface () && !bitmap_bit_p (exports, imp->mod))
20331 : : {
20332 : 3 : error_at (imp->loc, "interface partition is not exported");
20333 : 3 : bitmap_set_bit (exports, imp->mod);
20334 : : }
20335 : :
20336 : : /* All the partition entities should have been loaded when
20337 : : loading the partition. */
20338 : : if (CHECKING_P)
20339 : 882 : for (unsigned jx = 0; jx != imp->entity_num; jx++)
20340 : : {
20341 : 722 : binding_slot *slot = &(*entity_ary)[imp->entity_lwm + jx];
20342 : 722 : gcc_checking_assert (!slot->is_lazy ());
20343 : : }
20344 : : }
20345 : :
20346 : 590 : if (imp->is_direct () && (imp->remap || imp->is_partition ()))
20347 : 573 : note_location (imp->imported_from ());
20348 : : }
20349 : :
20350 : 2513 : if (partitions && bitmap_empty_p (partitions))
20351 : : /* No partitions present. */
20352 : : partitions = nullptr;
20353 : :
20354 : : /* Find the set of decls we must write out. */
20355 : 2513 : depset::hash table (DECL_NAMESPACE_BINDINGS (global_namespace)->size () * 8);
20356 : : /* Add the specializations before the writables, so that we can
20357 : : detect injected friend specializations. */
20358 : 2513 : table.add_specializations (true);
20359 : 2513 : table.add_specializations (false);
20360 : 2513 : if (partial_specializations)
20361 : : {
20362 : 197 : table.add_partial_entities (partial_specializations);
20363 : 197 : partial_specializations = NULL;
20364 : : }
20365 : 2513 : table.add_namespace_entities (global_namespace, partitions);
20366 : 2513 : if (class_members)
20367 : : {
20368 : 12 : table.add_class_entities (class_members);
20369 : 12 : class_members = NULL;
20370 : : }
20371 : :
20372 : : /* Now join everything up. */
20373 : 2513 : table.find_dependencies (this);
20374 : :
20375 : 2513 : if (!table.finalize_dependencies ())
20376 : : return false;
20377 : :
20378 : : #if CHECKING_P
20379 : : /* We're done verifying at-most once reading, reset to verify
20380 : : at-most once writing. */
20381 : 2484 : note_defs = note_defs_table_t::create_ggc (1000);
20382 : : #endif
20383 : :
20384 : : /* Determine Strongly Connected Components. This will also strip any
20385 : : unnecessary dependencies on imported or TU-local entities. */
20386 : 2484 : vec<depset *> sccs = table.connect ();
20387 : :
20388 : 2484 : vec_alloc (ool, modules->length ());
20389 : 3070 : for (unsigned ix = modules->length (); --ix;)
20390 : : {
20391 : 586 : auto *import = (*modules)[ix];
20392 : 586 : if (import->loadedness > ML_NONE
20393 : 586 : && !(partitions && bitmap_bit_p (partitions, import->mod)))
20394 : 426 : ool->quick_push (import);
20395 : : }
20396 : 2484 : ool->qsort (ool_cmp);
20397 : :
20398 : 2484 : write_diagnostic_classification (nullptr, global_dc, nullptr);
20399 : :
20400 : 2484 : vec<cpp_hashnode *> *macros = nullptr;
20401 : 2484 : if (is_header ())
20402 : 867 : macros = prepare_macros (reader);
20403 : :
20404 : 2484 : config.num_imports = mod_hwm;
20405 : 2484 : config.num_partitions = modules->length () - mod_hwm;
20406 : 2484 : auto map_info = write_prepare_maps (&config, bool (config.num_partitions));
20407 : 2484 : unsigned counts[MSC_HWM];
20408 : 2484 : memset (counts, 0, sizeof (counts));
20409 : :
20410 : : /* depset::cluster is the cluster number,
20411 : : depset::section is unspecified scratch value.
20412 : :
20413 : : The following loops make use of the tarjan property that
20414 : : dependencies will be earlier in the SCCS array. */
20415 : :
20416 : : /* This first loop determines the number of depsets in each SCC, and
20417 : : also the number of namespaces we're dealing with. During the
20418 : : loop, the meaning of a couple of depset fields now change:
20419 : :
20420 : : depset::cluster -> size_of cluster, if first of cluster & !namespace
20421 : : depset::section -> section number of cluster (if !namespace). */
20422 : :
20423 : 2484 : unsigned n_spaces = 0;
20424 : 2484 : counts[MSC_sec_lwm] = counts[MSC_sec_hwm] = to->get_section_limit ();
20425 : 229368 : for (unsigned size, ix = 0; ix < sccs.length (); ix += size)
20426 : : {
20427 : 226884 : depset **base = &sccs[ix];
20428 : :
20429 : 428239 : if (base[0]->get_entity_kind () == depset::EK_NAMESPACE)
20430 : : {
20431 : 4264 : n_spaces++;
20432 : 4264 : size = 1;
20433 : : }
20434 : : else
20435 : : {
20436 : : /* Count the members in this cluster. */
20437 : 922510 : for (size = 1; ix + size < sccs.length (); size++)
20438 : 920318 : if (base[size]->cluster != base[0]->cluster)
20439 : : break;
20440 : :
20441 : 1145130 : for (unsigned jx = 0; jx != size; jx++)
20442 : : {
20443 : : /* Set the section number. */
20444 : 922510 : base[jx]->cluster = ~(~0u >> 1); /* A bad value. */
20445 : 922510 : base[jx]->section = counts[MSC_sec_hwm];
20446 : : }
20447 : :
20448 : : /* Save the size in the first member's cluster slot. */
20449 : 222620 : base[0]->cluster = size;
20450 : :
20451 : 222620 : counts[MSC_sec_hwm]++;
20452 : : }
20453 : : }
20454 : :
20455 : : /* Write the clusters. Namespace decls are put in the spaces array.
20456 : : The meaning of depset::cluster changes to provide the
20457 : : unnamed-decl count of the depset's decl (and remains zero for
20458 : : non-decls and non-unnamed). */
20459 : 2484 : unsigned bytes = 0;
20460 : 2484 : vec<depset *> spaces;
20461 : 2484 : spaces.create (n_spaces);
20462 : :
20463 : 229368 : for (unsigned size, ix = 0; ix < sccs.length (); ix += size)
20464 : : {
20465 : 226884 : depset **base = &sccs[ix];
20466 : :
20467 : 226884 : if (base[0]->get_entity_kind () == depset::EK_NAMESPACE)
20468 : : {
20469 : 4264 : tree decl = base[0]->get_entity ();
20470 : 4264 : if (decl == global_namespace)
20471 : 2245 : base[0]->cluster = 0;
20472 : 2019 : else if (!base[0]->is_import ())
20473 : : {
20474 : 2019 : base[0]->cluster = counts[MSC_entities]++;
20475 : 2019 : spaces.quick_push (base[0]);
20476 : 2019 : counts[MSC_namespaces]++;
20477 : 2019 : if (CHECKING_P)
20478 : : {
20479 : : /* Add it to the entity map, such that we can tell it is
20480 : : part of us. */
20481 : 2019 : bool existed;
20482 : 2019 : unsigned *slot = &entity_map->get_or_insert
20483 : 2019 : (DECL_UID (decl), &existed);
20484 : 2019 : if (existed)
20485 : : /* It must have come from a partition. */
20486 : 0 : gcc_checking_assert
20487 : : (import_entity_module (*slot)->is_partition ());
20488 : 2019 : *slot = ~base[0]->cluster;
20489 : : }
20490 : 228933 : dump (dumper::CLUSTER) && dump ("Cluster namespace %N", decl);
20491 : : }
20492 : : size = 1;
20493 : : }
20494 : : else
20495 : : {
20496 : 222620 : size = base[0]->cluster;
20497 : :
20498 : : /* Cluster is now used to number entities. */
20499 : 222620 : base[0]->cluster = ~(~0u >> 1); /* A bad value. */
20500 : :
20501 : 222620 : sort_cluster (&table, base, size);
20502 : :
20503 : : /* Record the section for consistency checking during stream
20504 : : out -- we don't want to start writing decls in different
20505 : : sections. */
20506 : 222620 : table.section = base[0]->section;
20507 : 222620 : bytes += write_cluster (to, base, size, table, counts, &crc);
20508 : 222620 : table.section = 0;
20509 : : }
20510 : : }
20511 : :
20512 : : /* depset::cluster - entity number (on entities)
20513 : : depset::section - cluster number */
20514 : : /* We'd better have written as many sections and found as many
20515 : : namespaces as we predicted. */
20516 : 4968 : gcc_assert (counts[MSC_sec_hwm] == to->get_section_limit ()
20517 : : && spaces.length () == counts[MSC_namespaces]);
20518 : :
20519 : : /* Write the entitites. None happens if we contain namespaces or
20520 : : nothing. */
20521 : 2484 : config.num_entities = counts[MSC_entities];
20522 : 2484 : if (counts[MSC_entities])
20523 : 2239 : write_entities (to, sccs, counts[MSC_entities], &crc);
20524 : :
20525 : : /* Write the namespaces. */
20526 : 2484 : if (counts[MSC_namespaces])
20527 : 506 : write_namespaces (to, spaces, counts[MSC_namespaces], &crc);
20528 : :
20529 : : /* Write any using-directives. */
20530 : 2484 : if (counts[MSC_namespaces])
20531 : 506 : counts[MSC_using_directives]
20532 : 506 : = write_using_directives (to, table, spaces, &crc);
20533 : :
20534 : : /* Write the bindings themselves. */
20535 : 2484 : counts[MSC_bindings] = write_bindings (to, sccs, &crc);
20536 : :
20537 : : /* Write the unnamed. */
20538 : 2484 : counts[MSC_pendings] = write_pendings (to, sccs, table, &crc);
20539 : :
20540 : : /* Write the import table. */
20541 : 2484 : if (config.num_imports > 1)
20542 : 399 : write_imports (to, &crc);
20543 : :
20544 : : /* Write elided partition table. */
20545 : 2484 : if (config.num_partitions)
20546 : 112 : write_partitions (to, config.num_partitions, &crc);
20547 : :
20548 : : /* Write the line maps. */
20549 : 2484 : if (config.ordinary_locs)
20550 : : {
20551 : 2388 : write_ordinary_maps (to, map_info, bool (config.num_partitions), &crc);
20552 : 2388 : write_diagnostic_classification (to, global_dc, &crc);
20553 : : }
20554 : 2484 : if (config.macro_locs)
20555 : 117 : write_macro_maps (to, map_info, &crc);
20556 : :
20557 : 2484 : if (is_header ())
20558 : : {
20559 : 867 : counts[MSC_macros] = write_macros (to, macros, &crc);
20560 : 867 : counts[MSC_inits] = write_inits (to, table, &crc);
20561 : 867 : vec_free (macros);
20562 : : }
20563 : :
20564 : 2484 : unsigned clusters = counts[MSC_sec_hwm] - counts[MSC_sec_lwm];
20565 : 2484 : dump () && dump ("Wrote %u clusters, average %u bytes/cluster",
20566 : 273 : clusters, (bytes + clusters / 2) / (clusters + !clusters));
20567 : 2484 : trees_out::instrument ();
20568 : :
20569 : 2484 : write_counts (to, counts, &crc);
20570 : :
20571 : 2484 : spaces.release ();
20572 : 2484 : sccs.release ();
20573 : :
20574 : 2484 : vec_free (macro_loc_remap);
20575 : 2484 : vec_free (ord_loc_remap);
20576 : 2484 : vec_free (ool);
20577 : :
20578 : : // FIXME:QOI: Have a command line switch to control more detailed
20579 : : // information (which might leak data you do not want to leak).
20580 : : // Perhaps (some of) the write_readme contents should also be
20581 : : // so-controlled.
20582 : 2484 : if (false)
20583 : : write_env (to);
20584 : :
20585 : 2484 : return true;
20586 : 2513 : }
20587 : :
20588 : : // Finish module writing after we've emitted all dynamic initializers.
20589 : :
20590 : : void
20591 : 2484 : module_state::write_end (elf_out *to, cpp_reader *reader,
20592 : : module_state_config &config, unsigned &crc)
20593 : : {
20594 : : /* And finish up. */
20595 : 2484 : write_config (to, config, crc);
20596 : :
20597 : : /* Human-readable info. */
20598 : 2484 : write_readme (to, reader, config.dialect_str);
20599 : :
20600 : 2757 : dump () && dump ("Wrote %u sections", to->get_section_limit ());
20601 : 2484 : }
20602 : :
20603 : : /* Initial read of a CMI. Checks config, loads up imports and line
20604 : : maps. */
20605 : :
20606 : : bool
20607 : 2729 : module_state::read_initial (cpp_reader *reader)
20608 : : {
20609 : 2729 : module_state_config config;
20610 : 2729 : bool ok = true;
20611 : :
20612 : 2729 : if (ok && !from ()->begin (loc))
20613 : : ok = false;
20614 : :
20615 : 2729 : if (ok && !read_config (config))
20616 : : ok = false;
20617 : :
20618 : 2716 : bool have_locs = ok && read_prepare_maps (&config);
20619 : :
20620 : : /* Ordinary maps before the imports. */
20621 : 2716 : if (!(have_locs && config.ordinary_locs))
20622 : 71 : ordinary_locs.first = line_table->highest_location + 1;
20623 : 2658 : else if (!read_ordinary_maps (config.ordinary_locs, config.loc_range_bits))
20624 : : ok = false;
20625 : :
20626 : 2716 : if (ok && have_locs && config.ordinary_locs
20627 : 5387 : && !read_diagnostic_classification (global_dc))
20628 : : ok = false;
20629 : :
20630 : : /* Allocate the REMAP vector. */
20631 : 2729 : slurp->alloc_remap (config.num_imports);
20632 : :
20633 : 2729 : if (ok)
20634 : : {
20635 : : /* Read the import table. Decrement current to stop this CMI
20636 : : from being evicted during the import. */
20637 : 2716 : slurp->current--;
20638 : 2716 : if (config.num_imports > 1 && !read_imports (reader, line_table))
20639 : : ok = false;
20640 : 2716 : slurp->current++;
20641 : : }
20642 : :
20643 : : /* Read the elided partition table, if we're the primary partition. */
20644 : 2716 : if (ok && config.num_partitions && is_module ()
20645 : 2731 : && !read_partitions (config.num_partitions))
20646 : : ok = false;
20647 : :
20648 : : /* Determine the module's number. */
20649 : 2729 : gcc_checking_assert (mod == MODULE_UNKNOWN);
20650 : 2729 : gcc_checking_assert (this != this_module ());
20651 : :
20652 : 2729 : {
20653 : : /* Allocate space in the entities array now -- that array must be
20654 : : monotonically in step with the modules array. */
20655 : 2729 : entity_lwm = vec_safe_length (entity_ary);
20656 : 2729 : entity_num = config.num_entities;
20657 : 2729 : gcc_checking_assert (modules->length () == 1
20658 : : || modules->last ()->entity_lwm <= entity_lwm);
20659 : 2729 : vec_safe_reserve (entity_ary, config.num_entities);
20660 : :
20661 : 2729 : binding_slot slot;
20662 : 2729 : slot.u.binding = NULL_TREE;
20663 : 959183 : for (unsigned count = config.num_entities; count--;)
20664 : 956454 : entity_ary->quick_push (slot);
20665 : : }
20666 : :
20667 : : /* We'll run out of other resources before we run out of module
20668 : : indices. */
20669 : 2729 : mod = modules->length ();
20670 : 2729 : vec_safe_push (modules, this);
20671 : :
20672 : : /* We always import and export ourselves. */
20673 : 2729 : bitmap_set_bit (imports, mod);
20674 : 2729 : bitmap_set_bit (exports, mod);
20675 : :
20676 : 2729 : if (ok)
20677 : 2716 : (*slurp->remap)[0] = mod << 1;
20678 : 3219 : dump () && dump ("Assigning %M module number %u", this, mod);
20679 : :
20680 : : /* We should not have been frozen during the importing done by
20681 : : read_config. */
20682 : 2729 : gcc_assert (!from ()->is_frozen ());
20683 : :
20684 : : /* Macro maps after the imports. */
20685 : 2729 : if (!(ok && have_locs && config.macro_locs))
20686 : 2602 : macro_locs.first = LINEMAPS_MACRO_LOWEST_LOCATION (line_table);
20687 : 127 : else if (!read_macro_maps (config.macro_locs))
20688 : : ok = false;
20689 : :
20690 : : /* Note whether there's an active initializer. */
20691 : 2729 : active_init_p = !is_header () && bool (config.active_init);
20692 : :
20693 : 2729 : gcc_assert (slurp->current == ~0u);
20694 : 2729 : return ok;
20695 : : }
20696 : :
20697 : : /* Read a preprocessor state. */
20698 : :
20699 : : bool
20700 : 904 : module_state::read_preprocessor (bool outermost)
20701 : : {
20702 : 904 : gcc_checking_assert (is_header () && slurp
20703 : : && slurp->remap_module (0) == mod);
20704 : :
20705 : 904 : if (loadedness == ML_PREPROCESSOR)
20706 : 6 : return !(from () && from ()->get_error ());
20707 : :
20708 : 898 : bool ok = true;
20709 : :
20710 : : /* Read direct header imports. */
20711 : 898 : unsigned len = slurp->remap->length ();
20712 : 940 : for (unsigned ix = 1; ok && ix != len; ix++)
20713 : : {
20714 : 42 : unsigned map = (*slurp->remap)[ix];
20715 : 42 : if (map & 1)
20716 : : {
20717 : 42 : module_state *import = (*modules)[map >> 1];
20718 : 42 : if (import->is_header ())
20719 : : {
20720 : 42 : ok = import->read_preprocessor (false);
20721 : 42 : bitmap_ior_into (slurp->headers, import->slurp->headers);
20722 : : }
20723 : : }
20724 : : }
20725 : :
20726 : : /* Record as a direct header. */
20727 : 898 : if (ok)
20728 : 898 : bitmap_set_bit (slurp->headers, mod);
20729 : :
20730 : 898 : if (ok && !read_macros ())
20731 : : ok = false;
20732 : :
20733 : 898 : loadedness = ML_PREPROCESSOR;
20734 : 898 : announce ("macros");
20735 : :
20736 : 898 : if (flag_preprocess_only)
20737 : : /* We're done with the string table. */
20738 : 39 : from ()->release ();
20739 : :
20740 : 898 : return check_read (outermost, ok);
20741 : : }
20742 : :
20743 : : /* Read language state. */
20744 : :
20745 : : bool
20746 : 2739 : module_state::read_language (bool outermost)
20747 : : {
20748 : 2739 : gcc_checking_assert (!lazy_snum);
20749 : :
20750 : 2739 : if (loadedness == ML_LANGUAGE)
20751 : 74 : return !(slurp && from () && from ()->get_error ());
20752 : :
20753 : 2665 : gcc_checking_assert (slurp && slurp->current == ~0u
20754 : : && slurp->remap_module (0) == mod);
20755 : :
20756 : 2665 : bool ok = true;
20757 : :
20758 : : /* Read direct imports. */
20759 : 2665 : unsigned len = slurp->remap->length ();
20760 : 3020 : for (unsigned ix = 1; ok && ix != len; ix++)
20761 : : {
20762 : 355 : unsigned map = (*slurp->remap)[ix];
20763 : 355 : if (map & 1)
20764 : : {
20765 : 352 : module_state *import = (*modules)[map >> 1];
20766 : 352 : if (!import->read_language (false))
20767 : 355 : ok = false;
20768 : : }
20769 : : }
20770 : :
20771 : 2665 : unsigned counts[MSC_HWM];
20772 : :
20773 : 2665 : if (ok && !read_counts (counts))
20774 : : ok = false;
20775 : :
20776 : 2665 : function_depth++; /* Prevent unexpected GCs. */
20777 : :
20778 : 2665 : if (ok && counts[MSC_entities] != entity_num)
20779 : : ok = false;
20780 : 2665 : if (ok && counts[MSC_entities]
20781 : 2460 : && !read_entities (counts[MSC_entities],
20782 : : counts[MSC_sec_lwm], counts[MSC_sec_hwm]))
20783 : : ok = false;
20784 : :
20785 : : /* Read the namespace hierarchy. */
20786 : 2665 : if (ok && counts[MSC_namespaces]
20787 : 3196 : && !read_namespaces (counts[MSC_namespaces]))
20788 : : ok = false;
20789 : :
20790 : : /* Read any using-directives. */
20791 : 2665 : if (ok && counts[MSC_using_directives]
20792 : 2750 : && !read_using_directives (counts[MSC_using_directives]))
20793 : : ok = false;
20794 : :
20795 : 2665 : if (ok && !read_bindings (counts[MSC_bindings],
20796 : : counts[MSC_sec_lwm], counts[MSC_sec_hwm]))
20797 : : ok = false;
20798 : :
20799 : : /* And unnamed. */
20800 : 2665 : if (ok && counts[MSC_pendings] && !read_pendings (counts[MSC_pendings]))
20801 : : ok = false;
20802 : :
20803 : 2665 : if (ok)
20804 : : {
20805 : 2665 : slurp->remaining = counts[MSC_sec_hwm] - counts[MSC_sec_lwm];
20806 : 2665 : available_clusters += counts[MSC_sec_hwm] - counts[MSC_sec_lwm];
20807 : : }
20808 : :
20809 : 2665 : if (!flag_module_lazy
20810 : 2665 : || (is_partition ()
20811 : 208 : && module_interface_p ()
20812 : 184 : && !module_partition_p ()))
20813 : : {
20814 : : /* Read the sections in forward order, so that dependencies are read
20815 : : first. See note about tarjan_connect. */
20816 : 486 : ggc_collect ();
20817 : :
20818 : 486 : lazy_snum = ~0u;
20819 : :
20820 : 486 : unsigned hwm = counts[MSC_sec_hwm];
20821 : 137249 : for (unsigned ix = counts[MSC_sec_lwm]; ok && ix != hwm; ix++)
20822 : 136763 : if (!load_section (ix, NULL))
20823 : : {
20824 : : ok = false;
20825 : : break;
20826 : : }
20827 : 486 : lazy_snum = 0;
20828 : 486 : post_load_processing ();
20829 : :
20830 : 486 : ggc_collect ();
20831 : :
20832 : 486 : if (ok && CHECKING_P)
20833 : 497433 : for (unsigned ix = 0; ix != entity_num; ix++)
20834 : 496947 : gcc_assert (!(*entity_ary)[ix + entity_lwm].is_lazy ());
20835 : : }
20836 : :
20837 : : // If the import is a header-unit, we need to register initializers
20838 : : // of any static objects it contains (looking at you _Ioinit).
20839 : : // Notice, the ordering of these initializers will be that of a
20840 : : // dynamic initializer at this point in the current TU. (Other
20841 : : // instances of these objects in other TUs will be initialized as
20842 : : // part of that TU's global initializers.)
20843 : 2665 : if (ok && counts[MSC_inits] && !read_inits (counts[MSC_inits]))
20844 : : ok = false;
20845 : :
20846 : 2665 : function_depth--;
20847 : :
20848 : 2991 : announce (flag_module_lazy ? "lazy" : "imported");
20849 : 2665 : loadedness = ML_LANGUAGE;
20850 : :
20851 : 2665 : gcc_assert (slurp->current == ~0u);
20852 : :
20853 : : /* We're done with the string table. */
20854 : 2665 : from ()->release ();
20855 : :
20856 : 2665 : return check_read (outermost, ok);
20857 : : }
20858 : :
20859 : : bool
20860 : 165245 : module_state::maybe_defrost ()
20861 : : {
20862 : 165245 : bool ok = true;
20863 : 165245 : if (from ()->is_frozen ())
20864 : : {
20865 : 9 : if (lazy_open >= lazy_limit)
20866 : 3 : freeze_an_elf ();
20867 : 18 : dump () && dump ("Defrosting '%s'", filename);
20868 : 9 : ok = from ()->defrost (maybe_add_cmi_prefix (filename));
20869 : 9 : lazy_open++;
20870 : : }
20871 : :
20872 : 165245 : return ok;
20873 : : }
20874 : :
20875 : : /* Load section SNUM, dealing with laziness. It doesn't matter if we
20876 : : have multiple concurrent loads, because we do not use TREE_VISITED
20877 : : when reading back in. */
20878 : :
20879 : : bool
20880 : 165245 : module_state::load_section (unsigned snum, binding_slot *mslot)
20881 : : {
20882 : 165245 : if (from ()->get_error ())
20883 : : return false;
20884 : :
20885 : 165245 : if (snum >= slurp->current)
20886 : 0 : from ()->set_error (elf::E_BAD_LAZY);
20887 : 165245 : else if (maybe_defrost ())
20888 : : {
20889 : 165245 : unsigned old_current = slurp->current;
20890 : 165245 : slurp->current = snum;
20891 : 165245 : slurp->lru = 0; /* Do not swap out. */
20892 : 165245 : slurp->remaining--;
20893 : 165245 : read_cluster (snum);
20894 : 165245 : slurp->lru = ++lazy_lru;
20895 : 165245 : slurp->current = old_current;
20896 : : }
20897 : :
20898 : 165245 : if (mslot && mslot->is_lazy ())
20899 : : {
20900 : : /* Oops, the section didn't set this slot. */
20901 : 0 : from ()->set_error (elf::E_BAD_DATA);
20902 : 0 : *mslot = NULL_TREE;
20903 : : }
20904 : :
20905 : 165245 : bool ok = !from ()->get_error ();
20906 : 165245 : if (!ok)
20907 : : {
20908 : 0 : error_at (loc, "failed to read compiled module cluster %u: %s",
20909 : 0 : snum, from ()->get_error (filename));
20910 : 0 : note_cmi_name ();
20911 : : }
20912 : :
20913 : 165245 : maybe_completed_reading ();
20914 : :
20915 : 165245 : return ok;
20916 : : }
20917 : :
20918 : : void
20919 : 171521 : module_state::maybe_completed_reading ()
20920 : : {
20921 : 171521 : if (loadedness == ML_LANGUAGE && slurp->current == ~0u && !slurp->remaining)
20922 : : {
20923 : 2199 : lazy_open--;
20924 : : /* We no longer need the macros, all tokenizing has been done. */
20925 : 2199 : slurp->release_macros ();
20926 : :
20927 : 2199 : from ()->end ();
20928 : 2199 : slurp->close ();
20929 : 2199 : slurped ();
20930 : : }
20931 : 171521 : }
20932 : :
20933 : : /* After a reading operation, make sure things are still ok. If not,
20934 : : emit an error and clean up. */
20935 : :
20936 : : bool
20937 : 6313 : module_state::check_read (bool outermost, bool ok)
20938 : : {
20939 : 6313 : gcc_checking_assert (!outermost || slurp->current == ~0u);
20940 : :
20941 : 6313 : if (!ok)
20942 : 13 : from ()->set_error ();
20943 : :
20944 : 6313 : if (int e = from ()->get_error ())
20945 : : {
20946 : 40 : auto_diagnostic_group d;
20947 : 40 : error_at (loc, "failed to read compiled module: %s",
20948 : 40 : from ()->get_error (filename));
20949 : 40 : note_cmi_name ();
20950 : :
20951 : 40 : if (e == EMFILE
20952 : 40 : || e == ENFILE
20953 : : #if MAPPED_READING
20954 : 40 : || e == ENOMEM
20955 : : #endif
20956 : : || false)
20957 : 0 : inform (loc, "consider using %<-fno-module-lazy%>,"
20958 : : " increasing %<-param-lazy-modules=%u%> value,"
20959 : : " or increasing the per-process file descriptor limit",
20960 : : param_lazy_modules);
20961 : 40 : else if (e == ENOENT)
20962 : 21 : inform (loc, "imports must be built before being imported");
20963 : :
20964 : 40 : if (outermost)
20965 : 37 : fatal_error (loc, "returning to the gate for a mechanical issue");
20966 : :
20967 : 3 : ok = false;
20968 : 3 : }
20969 : :
20970 : 6276 : maybe_completed_reading ();
20971 : :
20972 : 6276 : return ok;
20973 : : }
20974 : :
20975 : : /* Return the IDENTIFIER_NODE naming module IX. This is the name
20976 : : including dots. */
20977 : :
20978 : : char const *
20979 : 322 : module_name (unsigned ix, bool header_ok)
20980 : : {
20981 : 322 : if (modules)
20982 : : {
20983 : 322 : module_state *imp = (*modules)[ix];
20984 : :
20985 : 322 : if (ix && !imp->name)
20986 : 0 : imp = imp->parent;
20987 : :
20988 : 322 : if (header_ok || !imp->is_header ())
20989 : 322 : return imp->get_flatname ();
20990 : : }
20991 : :
20992 : : return NULL;
20993 : : }
20994 : :
20995 : : /* Return the bitmap describing what modules are imported. Remember,
20996 : : we always import ourselves. */
20997 : :
20998 : : bitmap
20999 : 72680 : get_import_bitmap ()
21000 : : {
21001 : 72680 : return this_module ()->imports;
21002 : : }
21003 : :
21004 : : /* Return the visible imports and path of instantiation for an
21005 : : instantiation at TINST. If TINST is nullptr, we're not in an
21006 : : instantiation, and thus will return the visible imports of the
21007 : : current TU (and NULL *PATH_MAP_P). We cache the information on
21008 : : the tinst level itself. */
21009 : :
21010 : : static bitmap
21011 : 93994 : path_of_instantiation (tinst_level *tinst, bitmap *path_map_p)
21012 : : {
21013 : 93994 : gcc_checking_assert (modules_p ());
21014 : :
21015 : 93994 : if (!tinst || TREE_CODE (tinst->tldcl) == TEMPLATE_FOR_STMT)
21016 : : {
21017 : 1 : gcc_assert (!tinst || !tinst->next);
21018 : : /* Not inside an instantiation, just the regular case. */
21019 : 43753 : *path_map_p = nullptr;
21020 : 43753 : return get_import_bitmap ();
21021 : : }
21022 : :
21023 : 50241 : if (!tinst->path)
21024 : : {
21025 : : /* Calculate. */
21026 : 14560 : bitmap visible = path_of_instantiation (tinst->next, path_map_p);
21027 : 14560 : bitmap path_map = *path_map_p;
21028 : :
21029 : 14560 : if (!path_map)
21030 : : {
21031 : 2038 : path_map = BITMAP_GGC_ALLOC ();
21032 : 2038 : bitmap_set_bit (path_map, 0);
21033 : : }
21034 : :
21035 : 14560 : tree decl = tinst->tldcl;
21036 : 14560 : if (TREE_CODE (decl) == TREE_LIST)
21037 : 0 : decl = TREE_PURPOSE (decl);
21038 : 14560 : if (TYPE_P (decl))
21039 : 1689 : decl = TYPE_NAME (decl);
21040 : :
21041 : 14560 : if (unsigned mod = get_originating_module (decl))
21042 : 1827 : if (!bitmap_bit_p (path_map, mod))
21043 : : {
21044 : : /* This is brand new information! */
21045 : 106 : bitmap new_path = BITMAP_GGC_ALLOC ();
21046 : 106 : bitmap_copy (new_path, path_map);
21047 : 106 : bitmap_set_bit (new_path, mod);
21048 : 106 : path_map = new_path;
21049 : :
21050 : 106 : bitmap imports = (*modules)[mod]->imports;
21051 : 106 : if (bitmap_intersect_compl_p (imports, visible))
21052 : : {
21053 : : /* IMPORTS contains additional modules to VISIBLE. */
21054 : 12 : bitmap new_visible = BITMAP_GGC_ALLOC ();
21055 : :
21056 : 12 : bitmap_ior (new_visible, visible, imports);
21057 : 12 : visible = new_visible;
21058 : : }
21059 : : }
21060 : :
21061 : 14560 : tinst->path = path_map;
21062 : 14560 : tinst->visible = visible;
21063 : : }
21064 : :
21065 : 50241 : *path_map_p = tinst->path;
21066 : 50241 : return tinst->visible;
21067 : : }
21068 : :
21069 : : /* Return the bitmap describing what modules are visible along the
21070 : : path of instantiation. If we're not an instantiation, this will be
21071 : : the visible imports of the TU. *PATH_MAP_P is filled in with the
21072 : : modules owning the instantiation path -- we see the module-linkage
21073 : : entities of those modules. */
21074 : :
21075 : : bitmap
21076 : 21182862 : visible_instantiation_path (bitmap *path_map_p)
21077 : : {
21078 : 21182862 : if (!modules_p ())
21079 : : return NULL;
21080 : :
21081 : 79434 : return path_of_instantiation (current_instantiation (), path_map_p);
21082 : : }
21083 : :
21084 : : /* We've just directly imported IMPORT. Update our import/export
21085 : : bitmaps. IS_EXPORT is true if we're reexporting the OTHER. */
21086 : :
21087 : : void
21088 : 2806 : module_state::set_import (module_state const *import, bool is_export)
21089 : : {
21090 : 2806 : gcc_checking_assert (this != import);
21091 : :
21092 : : /* We see IMPORT's exports (which includes IMPORT). If IMPORT is
21093 : : the primary interface or a partition we'll see its imports. */
21094 : 2806 : bitmap_ior_into (imports, import->is_module () || import->is_partition ()
21095 : : ? import->imports : import->exports);
21096 : :
21097 : 2806 : if (is_export)
21098 : : /* We'll export OTHER's exports. */
21099 : 407 : bitmap_ior_into (exports, import->exports);
21100 : 2806 : }
21101 : :
21102 : : /* Return the declaring entity of DECL. That is the decl determining
21103 : : how to decorate DECL with module information. Returns NULL_TREE if
21104 : : it's the global module. */
21105 : :
21106 : : tree
21107 : 162863024 : get_originating_module_decl (tree decl)
21108 : : {
21109 : : /* An enumeration constant. */
21110 : 162863024 : if (TREE_CODE (decl) == CONST_DECL
21111 : 4860 : && DECL_CONTEXT (decl)
21112 : 162867884 : && (TREE_CODE (DECL_CONTEXT (decl)) == ENUMERAL_TYPE))
21113 : 4341 : decl = TYPE_NAME (DECL_CONTEXT (decl));
21114 : 162858683 : else if (TREE_CODE (decl) == FIELD_DECL
21115 : 162850247 : || TREE_CODE (decl) == USING_DECL
21116 : 325701427 : || CONST_DECL_USING_P (decl))
21117 : : {
21118 : 16458 : decl = DECL_CONTEXT (decl);
21119 : 16458 : if (TREE_CODE (decl) != FUNCTION_DECL)
21120 : 16458 : decl = TYPE_NAME (decl);
21121 : : }
21122 : :
21123 : 162863024 : gcc_checking_assert (TREE_CODE (decl) == TEMPLATE_DECL
21124 : : || TREE_CODE (decl) == FUNCTION_DECL
21125 : : || TREE_CODE (decl) == TYPE_DECL
21126 : : || TREE_CODE (decl) == VAR_DECL
21127 : : || TREE_CODE (decl) == CONCEPT_DECL
21128 : : || TREE_CODE (decl) == NAMESPACE_DECL);
21129 : :
21130 : 164003266 : for (;;)
21131 : : {
21132 : : /* Uninstantiated template friends are owned by the befriending
21133 : : class -- not their context. */
21134 : 163433145 : if (TREE_CODE (decl) == TEMPLATE_DECL
21135 : 163433145 : && DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (decl))
21136 : 5864 : decl = TYPE_NAME (DECL_CHAIN (decl));
21137 : :
21138 : : /* An imported temploid friend is attached to the same module the
21139 : : befriending class was. */
21140 : 163433145 : if (imported_temploid_friends)
21141 : 2093320 : if (tree *slot = imported_temploid_friends->get (decl))
21142 : 354 : decl = *slot;
21143 : :
21144 : 163433145 : int use;
21145 : 163433145 : if (tree ti = node_template_info (decl, use))
21146 : : {
21147 : 460620 : decl = TI_TEMPLATE (ti);
21148 : 460620 : if (TREE_CODE (decl) != TEMPLATE_DECL)
21149 : : {
21150 : : /* A friend template specialization. */
21151 : 16 : gcc_checking_assert (OVL_P (decl));
21152 : 16 : return global_namespace;
21153 : : }
21154 : : }
21155 : : else
21156 : : {
21157 : 162972525 : tree ctx = CP_DECL_CONTEXT (decl);
21158 : 162972525 : if (TREE_CODE (ctx) == NAMESPACE_DECL)
21159 : : break;
21160 : :
21161 : 109517 : if (TYPE_P (ctx))
21162 : : {
21163 : 103317 : ctx = TYPE_NAME (ctx);
21164 : 103317 : if (!ctx)
21165 : : {
21166 : : /* Some kind of internal type. */
21167 : 0 : gcc_checking_assert (DECL_ARTIFICIAL (decl));
21168 : 0 : return global_namespace;
21169 : : }
21170 : : }
21171 : 109517 : decl = ctx;
21172 : : }
21173 : 570121 : }
21174 : :
21175 : 162863008 : return decl;
21176 : : }
21177 : :
21178 : : /* If DECL is imported, return which module imported it, or 0 for the current
21179 : : module. Except that if GLOBAL_M1, return -1 for decls attached to the
21180 : : global module. */
21181 : :
21182 : : int
21183 : 834783 : get_originating_module (tree decl, bool global_m1)
21184 : : {
21185 : 834783 : tree owner = get_originating_module_decl (decl);
21186 : 834783 : tree not_tmpl = STRIP_TEMPLATE (owner);
21187 : :
21188 : 834783 : if (!DECL_LANG_SPECIFIC (not_tmpl))
21189 : 221894 : return global_m1 ? -1 : 0;
21190 : :
21191 : 1192617 : if (global_m1 && !DECL_MODULE_ATTACH_P (not_tmpl))
21192 : : return -1;
21193 : :
21194 : 87807 : int mod = !DECL_MODULE_IMPORT_P (not_tmpl) ? 0 : get_importing_module (owner);
21195 : 87807 : gcc_checking_assert (!global_m1 || !(*modules)[mod]->is_header ());
21196 : : return mod;
21197 : : }
21198 : :
21199 : : /* DECL is imported, return which module imported it.
21200 : : If FLEXIBLE, return -1 if not found, otherwise checking ICE. */
21201 : :
21202 : : unsigned
21203 : 4863 : get_importing_module (tree decl, bool flexible)
21204 : : {
21205 : 4863 : unsigned index = import_entity_index (decl, flexible);
21206 : 4863 : if (index == ~(~0u >> 1))
21207 : : return -1;
21208 : 4863 : module_state *module = import_entity_module (index);
21209 : :
21210 : 4863 : return module->mod;
21211 : : }
21212 : :
21213 : : /* Is it permissible to redeclare OLDDECL with NEWDECL.
21214 : :
21215 : : If NEWDECL is NULL, assumes that OLDDECL will be redeclared using
21216 : : the current scope's module and attachment. */
21217 : :
21218 : : bool
21219 : 139575 : module_may_redeclare (tree olddecl, tree newdecl)
21220 : : {
21221 : 139575 : tree decl = olddecl;
21222 : 156063 : for (;;)
21223 : : {
21224 : 147819 : tree ctx = CP_DECL_CONTEXT (decl);
21225 : 147819 : if (TREE_CODE (ctx) == NAMESPACE_DECL)
21226 : : // Found the namespace-scope decl.
21227 : : break;
21228 : 15849 : if (!CLASS_TYPE_P (ctx))
21229 : : // We've met a non-class scope. Such a thing is not
21230 : : // reopenable, so we must be ok.
21231 : : return true;
21232 : 8244 : decl = TYPE_NAME (ctx);
21233 : 8244 : }
21234 : :
21235 : 131970 : int use_tpl = 0;
21236 : 131970 : if (node_template_info (STRIP_TEMPLATE (decl), use_tpl) && use_tpl)
21237 : : // Specializations of any kind can be redeclared anywhere.
21238 : : // FIXME: Should we be checking this in more places on the scope chain?
21239 : : return true;
21240 : :
21241 : 88382 : module_state *old_mod = get_primary (this_module ());
21242 : 88382 : module_state *new_mod = old_mod;
21243 : :
21244 : 88382 : tree old_origin = get_originating_module_decl (decl);
21245 : 88382 : tree old_inner = STRIP_TEMPLATE (old_origin);
21246 : 88382 : bool olddecl_attached_p = (DECL_LANG_SPECIFIC (old_inner)
21247 : 141385 : && DECL_MODULE_ATTACH_P (old_inner));
21248 : 141385 : if (DECL_LANG_SPECIFIC (old_inner) && DECL_MODULE_IMPORT_P (old_inner))
21249 : : {
21250 : 411 : unsigned index = import_entity_index (old_origin);
21251 : 411 : old_mod = get_primary (import_entity_module (index));
21252 : : }
21253 : :
21254 : 88382 : bool newdecl_attached_p = module_attach_p ();
21255 : 88382 : if (newdecl)
21256 : : {
21257 : 24804 : tree new_origin = get_originating_module_decl (newdecl);
21258 : 24804 : tree new_inner = STRIP_TEMPLATE (new_origin);
21259 : 24804 : newdecl_attached_p = (DECL_LANG_SPECIFIC (new_inner)
21260 : 48407 : && DECL_MODULE_ATTACH_P (new_inner));
21261 : 48407 : if (DECL_LANG_SPECIFIC (new_inner) && DECL_MODULE_IMPORT_P (new_inner))
21262 : : {
21263 : 116 : unsigned index = import_entity_index (new_origin);
21264 : 116 : new_mod = get_primary (import_entity_module (index));
21265 : : }
21266 : : }
21267 : :
21268 : : /* Module attachment needs to match. */
21269 : 88382 : if (olddecl_attached_p == newdecl_attached_p)
21270 : : {
21271 : 88340 : if (!olddecl_attached_p)
21272 : : /* Both are GM entities, OK. */
21273 : : return true;
21274 : :
21275 : 1161 : if (new_mod == old_mod)
21276 : : /* Both attached to same named module, OK. */
21277 : : return true;
21278 : : }
21279 : :
21280 : : /* Attached to different modules, error. */
21281 : 45 : decl = newdecl ? newdecl : olddecl;
21282 : 45 : location_t loc = newdecl ? DECL_SOURCE_LOCATION (newdecl) : input_location;
21283 : 45 : if (DECL_IS_UNDECLARED_BUILTIN (olddecl))
21284 : : {
21285 : 3 : if (newdecl_attached_p)
21286 : 3 : error_at (loc, "declaring %qD in module %qs conflicts with builtin "
21287 : : "in global module", decl, new_mod->get_flatname ());
21288 : : else
21289 : 0 : error_at (loc, "declaration %qD conflicts with builtin", decl);
21290 : : }
21291 : 81 : else if (DECL_LANG_SPECIFIC (old_inner) && DECL_MODULE_IMPORT_P (old_inner))
21292 : : {
21293 : 27 : auto_diagnostic_group d;
21294 : 27 : if (newdecl_attached_p)
21295 : 3 : error_at (loc, "redeclaring %qD in module %qs conflicts with import",
21296 : : decl, new_mod->get_flatname ());
21297 : : else
21298 : 24 : error_at (loc, "redeclaring %qD in global module conflicts with import",
21299 : : decl);
21300 : :
21301 : 27 : if (olddecl_attached_p)
21302 : 27 : inform (DECL_SOURCE_LOCATION (olddecl),
21303 : : "import declared attached to module %qs",
21304 : : old_mod->get_flatname ());
21305 : : else
21306 : 0 : inform (DECL_SOURCE_LOCATION (olddecl),
21307 : : "import declared in global module");
21308 : 27 : }
21309 : : else
21310 : : {
21311 : 15 : auto_diagnostic_group d;
21312 : 15 : if (newdecl_attached_p)
21313 : 15 : error_at (loc, "conflicting declaration of %qD in module %qs",
21314 : : decl, new_mod->get_flatname ());
21315 : : else
21316 : 0 : error_at (loc, "conflicting declaration of %qD in global module",
21317 : : decl);
21318 : :
21319 : 15 : if (olddecl_attached_p)
21320 : 0 : inform (DECL_SOURCE_LOCATION (olddecl),
21321 : : "previously declared in module %qs",
21322 : : old_mod->get_flatname ());
21323 : : else
21324 : 15 : inform (DECL_SOURCE_LOCATION (olddecl),
21325 : : "previously declared in global module");
21326 : 15 : }
21327 : : return false;
21328 : : }
21329 : :
21330 : : /* DECL is being created by this TU. Record it came from here. We
21331 : : record module purview, so we can see if partial or explicit
21332 : : specialization needs to be written out, even though its purviewness
21333 : : comes from the most general template. */
21334 : :
21335 : : void
21336 : 781634373 : set_instantiating_module (tree decl)
21337 : : {
21338 : 781634373 : gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
21339 : : || VAR_P (decl)
21340 : : || TREE_CODE (decl) == TYPE_DECL
21341 : : || TREE_CODE (decl) == CONCEPT_DECL
21342 : : || TREE_CODE (decl) == TEMPLATE_DECL
21343 : : || TREE_CODE (decl) == CONST_DECL
21344 : : || (TREE_CODE (decl) == NAMESPACE_DECL
21345 : : && DECL_NAMESPACE_ALIAS (decl)));
21346 : :
21347 : 781634373 : if (!modules_p ())
21348 : : return;
21349 : :
21350 : 2514853 : decl = STRIP_TEMPLATE (decl);
21351 : :
21352 : 2514853 : if (!DECL_LANG_SPECIFIC (decl) && module_purview_p ())
21353 : 319048 : retrofit_lang_decl (decl);
21354 : :
21355 : 2514853 : if (DECL_LANG_SPECIFIC (decl))
21356 : : {
21357 : 1998635 : DECL_MODULE_PURVIEW_P (decl) = module_purview_p ();
21358 : : /* If this was imported, we'll still be in the entity_hash. */
21359 : 1998635 : DECL_MODULE_IMPORT_P (decl) = false;
21360 : : }
21361 : : }
21362 : :
21363 : : /* If DECL is a class member, whose class is not defined in this TU
21364 : : (it was imported), remember this decl. */
21365 : :
21366 : : void
21367 : 118364 : set_defining_module (tree decl)
21368 : : {
21369 : 118364 : gcc_checking_assert (!DECL_LANG_SPECIFIC (decl)
21370 : : || !DECL_MODULE_IMPORT_P (decl));
21371 : :
21372 : 118364 : if (module_maybe_has_cmi_p ())
21373 : : {
21374 : : /* We need to track all declarations within a module, not just those
21375 : : in the module purview, because we don't necessarily know yet if
21376 : : this module will require a CMI while in the global fragment. */
21377 : 79338 : tree ctx = DECL_CONTEXT (decl);
21378 : 79338 : if (ctx
21379 : 79338 : && (TREE_CODE (ctx) == RECORD_TYPE || TREE_CODE (ctx) == UNION_TYPE)
21380 : 13844 : && DECL_LANG_SPECIFIC (TYPE_NAME (ctx))
21381 : 89527 : && DECL_MODULE_IMPORT_P (TYPE_NAME (ctx)))
21382 : : {
21383 : : /* This entity's context is from an import. We may need to
21384 : : record this entity to make sure we emit it in the CMI.
21385 : : Template specializations are in the template hash tables,
21386 : : so we don't need to record them here as well. */
21387 : 12 : int use_tpl = -1;
21388 : 12 : tree ti = node_template_info (decl, use_tpl);
21389 : 12 : if (use_tpl <= 0)
21390 : : {
21391 : 12 : if (ti)
21392 : : {
21393 : 3 : gcc_checking_assert (!use_tpl);
21394 : : /* Get to the TEMPLATE_DECL. */
21395 : 3 : decl = TI_TEMPLATE (ti);
21396 : : }
21397 : :
21398 : : /* Record it on the class_members list. */
21399 : 12 : vec_safe_push (class_members, decl);
21400 : : }
21401 : : }
21402 : : }
21403 : 118364 : }
21404 : :
21405 : : /* Also remember DECL if it's a newly declared class template partial
21406 : : specialization, because these are not necessarily added to the
21407 : : instantiation tables. */
21408 : :
21409 : : void
21410 : 7105495 : set_defining_module_for_partial_spec (tree decl)
21411 : : {
21412 : 7105495 : if (module_maybe_has_cmi_p ()
21413 : 18186 : && DECL_IMPLICIT_TYPEDEF_P (decl)
21414 : 7121032 : && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
21415 : 15537 : vec_safe_push (partial_specializations, decl);
21416 : 7105495 : }
21417 : :
21418 : : void
21419 : 271500611 : set_originating_module (tree decl, bool friend_p ATTRIBUTE_UNUSED)
21420 : : {
21421 : 271500611 : set_instantiating_module (decl);
21422 : :
21423 : 271500611 : if (!DECL_NAMESPACE_SCOPE_P (decl))
21424 : : return;
21425 : :
21426 : 166041296 : gcc_checking_assert (friend_p || decl == get_originating_module_decl (decl));
21427 : :
21428 : 166041296 : if (module_attach_p ())
21429 : : {
21430 : 4003 : retrofit_lang_decl (decl);
21431 : 4003 : DECL_MODULE_ATTACH_P (decl) = true;
21432 : : }
21433 : :
21434 : : /* It is ill-formed to export a declaration with internal linkage. However,
21435 : : at the point this function is called we don't yet always know whether this
21436 : : declaration has internal linkage; instead we defer this check for callers
21437 : : to do once visibility has been determined. */
21438 : 166041296 : if (module_exporting_p ())
21439 : 197745 : DECL_MODULE_EXPORT_P (decl) = true;
21440 : : }
21441 : :
21442 : : /* Checks whether DECL within a module unit has valid linkage for its kind.
21443 : : Must be called after visibility for DECL has been finalised. */
21444 : :
21445 : : void
21446 : 334315254 : check_module_decl_linkage (tree decl)
21447 : : {
21448 : 334315254 : if (!module_has_cmi_p ())
21449 : : return;
21450 : :
21451 : : /* A header unit shall not contain a definition of a non-inline function
21452 : : or variable (not template) whose name has external linkage. */
21453 : 456255 : if (header_module_p ()
21454 : 408009 : && !processing_template_decl
21455 : 136166 : && ((TREE_CODE (decl) == FUNCTION_DECL
21456 : 79907 : && !DECL_DECLARED_INLINE_P (decl))
21457 : 104549 : || (TREE_CODE (decl) == VAR_DECL
21458 : 31503 : && !DECL_INLINE_VAR_P (decl)))
21459 : 39783 : && decl_defined_p (decl)
21460 : 3805 : && !(DECL_LANG_SPECIFIC (decl)
21461 : 3805 : && DECL_TEMPLATE_INSTANTIATION (decl))
21462 : 460060 : && decl_linkage (decl) == lk_external)
21463 : 60 : error_at (DECL_SOURCE_LOCATION (decl),
21464 : : "external linkage definition of %qD in header module must "
21465 : : "be declared %<inline%>", decl);
21466 : :
21467 : : /* An internal-linkage declaration cannot be generally be exported.
21468 : : But it's OK to export any declaration from a header unit, including
21469 : : internal linkage declarations. */
21470 : 456255 : if (!header_module_p () && DECL_MODULE_EXPORT_P (decl))
21471 : : {
21472 : : /* Let's additionally treat any exported declaration within an
21473 : : internal namespace as exporting a declaration with internal
21474 : : linkage, as this would also implicitly export the internal
21475 : : linkage namespace. */
21476 : 1901 : if (decl_internal_context_p (decl))
21477 : : {
21478 : 50 : error_at (DECL_SOURCE_LOCATION (decl),
21479 : : "exporting declaration %qD declared in unnamed namespace",
21480 : : decl);
21481 : 50 : DECL_MODULE_EXPORT_P (decl) = false;
21482 : : }
21483 : 1851 : else if (decl_linkage (decl) == lk_internal)
21484 : : {
21485 : 17 : error_at (DECL_SOURCE_LOCATION (decl),
21486 : : "exporting declaration %qD with internal linkage", decl);
21487 : 17 : DECL_MODULE_EXPORT_P (decl) = false;
21488 : : }
21489 : : }
21490 : : }
21491 : :
21492 : : /* DECL is keyed to CTX for odr purposes. */
21493 : :
21494 : : void
21495 : 974404 : maybe_key_decl (tree ctx, tree decl)
21496 : : {
21497 : 974404 : if (!modules_p ())
21498 : : return;
21499 : :
21500 : : /* We only need to deal with lambdas attached to var, field,
21501 : : parm, type, or concept decls. */
21502 : 6919 : if (TREE_CODE (ctx) != VAR_DECL
21503 : 6919 : && TREE_CODE (ctx) != FIELD_DECL
21504 : 6692 : && TREE_CODE (ctx) != PARM_DECL
21505 : 6680 : && TREE_CODE (ctx) != TYPE_DECL
21506 : 6652 : && TREE_CODE (ctx) != CONCEPT_DECL)
21507 : : return;
21508 : :
21509 : : /* For fields, key it to the containing type to handle deduplication
21510 : : correctly. */
21511 : 304 : if (TREE_CODE (ctx) == FIELD_DECL)
21512 : 6 : ctx = TYPE_NAME (DECL_CONTEXT (ctx));
21513 : :
21514 : 304 : if (!keyed_table)
21515 : 76 : keyed_table = new keyed_map_t (EXPERIMENT (1, 400));
21516 : :
21517 : 304 : auto &vec = keyed_table->get_or_insert (ctx);
21518 : 304 : if (!vec.length ())
21519 : : {
21520 : 290 : retrofit_lang_decl (ctx);
21521 : 290 : DECL_MODULE_KEYED_DECLS_P (ctx) = true;
21522 : : }
21523 : 304 : vec.safe_push (decl);
21524 : : }
21525 : :
21526 : : /* DECL is an instantiated friend that should be attached to the same
21527 : : module that ORIG is. */
21528 : :
21529 : : void
21530 : 2760876 : propagate_defining_module (tree decl, tree orig)
21531 : : {
21532 : 2760876 : if (!modules_p ())
21533 : : return;
21534 : :
21535 : 4670 : tree not_tmpl = STRIP_TEMPLATE (orig);
21536 : 9319 : if (DECL_LANG_SPECIFIC (not_tmpl) && DECL_MODULE_ATTACH_P (not_tmpl))
21537 : : {
21538 : 102 : tree inner = STRIP_TEMPLATE (decl);
21539 : 102 : retrofit_lang_decl (inner);
21540 : 102 : DECL_MODULE_ATTACH_P (inner) = true;
21541 : : }
21542 : :
21543 : 9319 : if (DECL_LANG_SPECIFIC (not_tmpl) && DECL_MODULE_IMPORT_P (not_tmpl))
21544 : : {
21545 : 220 : bool exists = imported_temploid_friends->put (decl, orig);
21546 : :
21547 : : /* We should only be called if lookup for an existing decl
21548 : : failed, in which case there shouldn't already be an entry
21549 : : in the map. */
21550 : 220 : gcc_assert (!exists);
21551 : : }
21552 : : }
21553 : :
21554 : : /* DECL is being freed, clear data we don't need anymore. */
21555 : :
21556 : : void
21557 : 29803 : remove_defining_module (tree decl)
21558 : : {
21559 : 29803 : if (!modules_p ())
21560 : : return;
21561 : :
21562 : 29803 : if (imported_temploid_friends)
21563 : 29803 : imported_temploid_friends->remove (decl);
21564 : : }
21565 : :
21566 : : /* Create the flat name string. It is simplest to have it handy. */
21567 : :
21568 : : void
21569 : 5769 : module_state::set_flatname ()
21570 : : {
21571 : 5769 : gcc_checking_assert (!flatname);
21572 : 5769 : if (parent)
21573 : : {
21574 : 601 : auto_vec<tree,5> ids;
21575 : 601 : size_t len = 0;
21576 : 601 : char const *primary = NULL;
21577 : 601 : size_t pfx_len = 0;
21578 : :
21579 : 601 : for (module_state *probe = this;
21580 : 1495 : probe;
21581 : 894 : probe = probe->parent)
21582 : 1334 : if (is_partition () && !probe->is_partition ())
21583 : : {
21584 : 440 : primary = probe->get_flatname ();
21585 : 440 : pfx_len = strlen (primary);
21586 : 440 : break;
21587 : : }
21588 : : else
21589 : : {
21590 : 894 : ids.safe_push (probe->name);
21591 : 894 : len += IDENTIFIER_LENGTH (probe->name) + 1;
21592 : : }
21593 : :
21594 : 601 : char *flat = XNEWVEC (char, pfx_len + len + is_partition ());
21595 : 601 : flatname = flat;
21596 : :
21597 : 601 : if (primary)
21598 : : {
21599 : 440 : memcpy (flat, primary, pfx_len);
21600 : 440 : flat += pfx_len;
21601 : 440 : *flat++ = ':';
21602 : : }
21603 : :
21604 : 1495 : for (unsigned len = 0; ids.length ();)
21605 : : {
21606 : 894 : if (len)
21607 : 293 : flat[len++] = '.';
21608 : 894 : tree elt = ids.pop ();
21609 : 894 : unsigned l = IDENTIFIER_LENGTH (elt);
21610 : 894 : memcpy (flat + len, IDENTIFIER_POINTER (elt), l + 1);
21611 : 894 : len += l;
21612 : : }
21613 : 601 : }
21614 : 5168 : else if (is_header ())
21615 : 1843 : flatname = TREE_STRING_POINTER (name);
21616 : : else
21617 : 3325 : flatname = IDENTIFIER_POINTER (name);
21618 : 5769 : }
21619 : :
21620 : : /* Read the CMI file for a module. */
21621 : :
21622 : : bool
21623 : 2750 : module_state::do_import (cpp_reader *reader, bool outermost)
21624 : : {
21625 : 2750 : gcc_assert (global_namespace == current_scope () && loadedness == ML_NONE);
21626 : :
21627 : : /* If this TU is a partition of the module we're importing,
21628 : : that module is the primary module interface. */
21629 : 2750 : if (this_module ()->is_partition ()
21630 : 2792 : && this == get_primary (this_module ()))
21631 : 6 : module_p = true;
21632 : :
21633 : 2750 : loc = linemap_module_loc (line_table, loc, get_flatname ());
21634 : :
21635 : 2750 : if (lazy_open >= lazy_limit)
21636 : 9 : freeze_an_elf ();
21637 : :
21638 : 2750 : int fd = -1;
21639 : 2750 : int e = ENOENT;
21640 : 2750 : if (filename)
21641 : : {
21642 : 2750 : const char *file = maybe_add_cmi_prefix (filename);
21643 : 3240 : dump () && dump ("CMI is %s", file);
21644 : 2750 : if (note_module_cmi_yes || inform_cmi_p)
21645 : 12 : inform (loc, "reading CMI %qs", file);
21646 : : /* Add the CMI file to the dependency tracking. */
21647 : 2750 : if (cpp_get_deps (reader))
21648 : 12 : deps_add_dep (cpp_get_deps (reader), file);
21649 : 2750 : fd = open (file, O_RDONLY | O_CLOEXEC | O_BINARY);
21650 : 2750 : e = errno;
21651 : : }
21652 : :
21653 : 2750 : gcc_checking_assert (!slurp);
21654 : 5479 : slurp = new slurping (new elf_in (fd, e));
21655 : :
21656 : 2750 : bool ok = true;
21657 : 2750 : if (!from ()->get_error ())
21658 : : {
21659 : 2729 : announce ("importing");
21660 : 2729 : loadedness = ML_CONFIG;
21661 : 2729 : lazy_open++;
21662 : 2729 : ok = read_initial (reader);
21663 : 2729 : slurp->lru = ++lazy_lru;
21664 : : }
21665 : :
21666 : 2750 : gcc_assert (slurp->current == ~0u);
21667 : :
21668 : 2750 : return check_read (outermost, ok);
21669 : : }
21670 : :
21671 : : /* Attempt to increase the file descriptor limit. */
21672 : :
21673 : : static bool
21674 : 4495 : try_increase_lazy (unsigned want)
21675 : : {
21676 : 4495 : gcc_checking_assert (lazy_open >= lazy_limit);
21677 : :
21678 : : /* If we're increasing, saturate at hard limit. */
21679 : 4495 : if (want > lazy_hard_limit && lazy_limit < lazy_hard_limit)
21680 : 4495 : want = lazy_hard_limit;
21681 : :
21682 : : #if HAVE_SETRLIMIT
21683 : 4495 : if ((!lazy_limit || !param_lazy_modules)
21684 : 4483 : && lazy_hard_limit
21685 : 4483 : && want <= lazy_hard_limit)
21686 : : {
21687 : 4483 : struct rlimit rlimit;
21688 : 4483 : rlimit.rlim_cur = want + LAZY_HEADROOM;
21689 : 4483 : rlimit.rlim_max = lazy_hard_limit + LAZY_HEADROOM;
21690 : 4483 : if (!setrlimit (RLIMIT_NOFILE, &rlimit))
21691 : 4483 : lazy_limit = want;
21692 : : }
21693 : : #endif
21694 : :
21695 : 4495 : return lazy_open < lazy_limit;
21696 : : }
21697 : :
21698 : : /* Pick a victim module to freeze its reader. */
21699 : :
21700 : : void
21701 : 12 : module_state::freeze_an_elf ()
21702 : : {
21703 : 12 : if (try_increase_lazy (lazy_open * 2))
21704 : : return;
21705 : :
21706 : 12 : module_state *victim = NULL;
21707 : 12 : for (unsigned ix = modules->length (); ix--;)
21708 : : {
21709 : 30 : module_state *candidate = (*modules)[ix];
21710 : 30 : if (candidate && candidate->slurp && candidate->slurp->lru
21711 : 60 : && candidate->from ()->is_freezable ()
21712 : 39 : && (!victim || victim->slurp->lru > candidate->slurp->lru))
21713 : : victim = candidate;
21714 : : }
21715 : :
21716 : 12 : if (victim)
21717 : : {
21718 : 18 : dump () && dump ("Freezing '%s'", victim->filename);
21719 : 9 : if (victim->slurp->macro_defs.size)
21720 : : /* Save the macro definitions to a buffer. */
21721 : 0 : victim->from ()->preserve (victim->slurp->macro_defs);
21722 : 9 : if (victim->slurp->macro_tbl.size)
21723 : : /* Save the macro definitions to a buffer. */
21724 : 0 : victim->from ()->preserve (victim->slurp->macro_tbl);
21725 : 9 : victim->from ()->freeze ();
21726 : 9 : lazy_open--;
21727 : : }
21728 : : else
21729 : 3 : dump () && dump ("No module available for freezing");
21730 : : }
21731 : :
21732 : : /* Load the lazy slot *MSLOT, INDEX'th slot of the module. */
21733 : :
21734 : : bool
21735 : 25735 : module_state::lazy_load (unsigned index, binding_slot *mslot)
21736 : : {
21737 : 25735 : unsigned n = dump.push (this);
21738 : :
21739 : 25735 : gcc_checking_assert (function_depth);
21740 : :
21741 : 25735 : unsigned cookie = mslot->get_lazy ();
21742 : 25735 : unsigned snum = cookie >> 2;
21743 : 26070 : dump () && dump ("Loading entity %M[%u] section:%u", this, index, snum);
21744 : :
21745 : 25735 : bool ok = load_section (snum, mslot);
21746 : :
21747 : 25735 : dump.pop (n);
21748 : :
21749 : 25735 : return ok;
21750 : : }
21751 : :
21752 : : /* Load MOD's binding for NS::ID into *MSLOT. *MSLOT contains the
21753 : : lazy cookie. OUTER is true if this is the outermost lazy, (used
21754 : : for diagnostics). */
21755 : :
21756 : : void
21757 : 2747 : lazy_load_binding (unsigned mod, tree ns, tree id, binding_slot *mslot)
21758 : : {
21759 : 2747 : int count = errorcount + warningcount;
21760 : :
21761 : 2747 : bool timer_running = timevar_cond_start (TV_MODULE_IMPORT);
21762 : :
21763 : : /* Make sure lazy loading from a template context behaves as if
21764 : : from a non-template context. */
21765 : 2747 : processing_template_decl_sentinel ptds;
21766 : :
21767 : : /* Stop GC happening, even in outermost loads (because our caller
21768 : : could well be building up a lookup set). */
21769 : 2747 : function_depth++;
21770 : :
21771 : 2747 : gcc_checking_assert (mod);
21772 : 2747 : module_state *module = (*modules)[mod];
21773 : 2747 : unsigned n = dump.push (module);
21774 : :
21775 : 2747 : unsigned snum = mslot->get_lazy ();
21776 : 3054 : dump () && dump ("Lazily binding %P@%N section:%u", ns, id,
21777 : : module->name, snum);
21778 : :
21779 : 2747 : bool ok = !recursive_lazy (snum);
21780 : 2747 : if (ok)
21781 : : {
21782 : 2747 : ok = module->load_section (snum, mslot);
21783 : 2747 : lazy_snum = 0;
21784 : 2747 : post_load_processing ();
21785 : : }
21786 : :
21787 : 2747 : dump.pop (n);
21788 : :
21789 : 2747 : function_depth--;
21790 : :
21791 : 2747 : timevar_cond_stop (TV_MODULE_IMPORT, timer_running);
21792 : :
21793 : 2747 : if (!ok)
21794 : 0 : fatal_error (input_location,
21795 : 0 : module->is_header ()
21796 : : ? G_("failed to load binding %<%E%s%E%>")
21797 : : : G_("failed to load binding %<%E%s%E@%s%>"),
21798 : 0 : ns, &"::"[ns == global_namespace ? 2 : 0], id,
21799 : : module->get_flatname ());
21800 : :
21801 : 2747 : if (count != errorcount + warningcount)
21802 : 27 : inform (input_location,
21803 : 27 : module->is_header ()
21804 : : ? G_("during load of binding %<%E%s%E%>")
21805 : : : G_("during load of binding %<%E%s%E@%s%>"),
21806 : 27 : ns, &"::"[ns == global_namespace ? 2 : 0], id,
21807 : : module->get_flatname ());
21808 : 2747 : }
21809 : :
21810 : : /* Load any pending entities keyed to the top-key of DECL. */
21811 : :
21812 : : void
21813 : 25056359 : lazy_load_pendings (tree decl)
21814 : : {
21815 : : /* Make sure lazy loading from a template context behaves as if
21816 : : from a non-template context. */
21817 : 25056359 : processing_template_decl_sentinel ptds;
21818 : :
21819 : 25056359 : tree key_decl;
21820 : 25056359 : pending_key key;
21821 : 25056359 : key.ns = find_pending_key (decl, &key_decl);
21822 : 25056359 : key.id = DECL_NAME (key_decl);
21823 : :
21824 : 25056359 : auto *pending_vec = pending_table ? pending_table->get (key) : nullptr;
21825 : 25051872 : if (!pending_vec)
21826 : 25051512 : return;
21827 : :
21828 : 4847 : int count = errorcount + warningcount;
21829 : :
21830 : 4847 : bool timer_running = timevar_cond_start (TV_MODULE_IMPORT);
21831 : 4847 : bool ok = !recursive_lazy ();
21832 : 4847 : if (ok)
21833 : : {
21834 : 4847 : function_depth++; /* Prevent GC */
21835 : 4847 : unsigned n = dump.push (NULL);
21836 : 5289 : dump () && dump ("Reading %u pending entities keyed to %P",
21837 : : pending_vec->length (), key.ns, key.id);
21838 : 4847 : for (unsigned ix = pending_vec->length (); ix--;)
21839 : : {
21840 : 50436 : unsigned index = (*pending_vec)[ix];
21841 : 50436 : binding_slot *slot = &(*entity_ary)[index];
21842 : :
21843 : 50436 : if (slot->is_lazy ())
21844 : : {
21845 : 2607 : module_state *import = import_entity_module (index);
21846 : 2607 : if (!import->lazy_load (index - import->entity_lwm, slot))
21847 : 50436 : ok = false;
21848 : : }
21849 : 103112 : else if (dump ())
21850 : : {
21851 : 323 : module_state *import = import_entity_module (index);
21852 : 323 : dump () && dump ("Entity %M[%u] already loaded",
21853 : 323 : import, index - import->entity_lwm);
21854 : : }
21855 : : }
21856 : :
21857 : 4847 : pending_table->remove (key);
21858 : 4847 : dump.pop (n);
21859 : 4847 : lazy_snum = 0;
21860 : 4847 : post_load_processing ();
21861 : 4847 : function_depth--;
21862 : : }
21863 : :
21864 : 4847 : timevar_cond_stop (TV_MODULE_IMPORT, timer_running);
21865 : :
21866 : 4847 : if (!ok)
21867 : 0 : fatal_error (input_location, "failed to load pendings for %<%E%s%E%>",
21868 : 0 : key.ns, &"::"[key.ns == global_namespace ? 2 : 0], key.id);
21869 : :
21870 : 4847 : if (count != errorcount + warningcount)
21871 : 0 : inform (input_location, "during load of pendings for %<%E%s%E%>",
21872 : 0 : key.ns, &"::"[key.ns == global_namespace ? 2 : 0], key.id);
21873 : 25056359 : }
21874 : :
21875 : : static void
21876 : 2503 : direct_import (module_state *import, cpp_reader *reader)
21877 : : {
21878 : 2503 : timevar_start (TV_MODULE_IMPORT);
21879 : 2503 : unsigned n = dump.push (import);
21880 : :
21881 : 2503 : gcc_checking_assert (import->is_direct () && import->has_location ());
21882 : 2503 : if (import->loadedness == ML_NONE)
21883 : 1598 : if (!import->do_import (reader, true))
21884 : 0 : gcc_unreachable ();
21885 : :
21886 : 2466 : this_module ()->set_import (import, import->exported_p);
21887 : :
21888 : 2466 : if (import->loadedness < ML_LANGUAGE)
21889 : : {
21890 : 2387 : if (!keyed_table)
21891 : 2099 : keyed_table = new keyed_map_t (EXPERIMENT (1, 400));
21892 : 2387 : import->read_language (true);
21893 : : }
21894 : :
21895 : 2466 : dump.pop (n);
21896 : 2466 : timevar_stop (TV_MODULE_IMPORT);
21897 : 2466 : }
21898 : :
21899 : : /* Import module IMPORT. */
21900 : :
21901 : : void
21902 : 2284 : import_module (module_state *import, location_t from_loc, bool exporting_p,
21903 : : tree, cpp_reader *reader)
21904 : : {
21905 : : /* A non-partition implementation unit has no name. */
21906 : 2284 : if (!this_module ()->name && this_module ()->parent == import)
21907 : : {
21908 : 3 : auto_diagnostic_group d;
21909 : 3 : error_at (from_loc, "import of %qs within its own implementation unit",
21910 : : import->get_flatname());
21911 : 3 : inform (import->loc, "module declared here");
21912 : 3 : return;
21913 : 3 : }
21914 : :
21915 : 2281 : if (!import->check_circular_import (from_loc))
21916 : : return;
21917 : :
21918 : 2275 : if (!import->is_header () && current_lang_depth ())
21919 : : /* Only header units should appear inside language
21920 : : specifications. The std doesn't specify this, but I think
21921 : : that's an error in resolving US 033, because language linkage
21922 : : is also our escape clause to getting things into the global
21923 : : module, so we don't want to confuse things by having to think
21924 : : about whether 'extern "C++" { import foo; }' puts foo's
21925 : : contents into the global module all of a sudden. */
21926 : 6 : warning (0, "import of named module %qs inside language-linkage block",
21927 : : import->get_flatname ());
21928 : :
21929 : 2275 : if (exporting_p || module_exporting_p ())
21930 : 280 : import->exported_p = true;
21931 : :
21932 : 2275 : if (import->loadedness != ML_NONE)
21933 : : {
21934 : 902 : from_loc = ordinary_loc_of (line_table, from_loc);
21935 : 902 : linemap_module_reparent (line_table, import->loc, from_loc);
21936 : : }
21937 : :
21938 : 2275 : gcc_checking_assert (import->is_direct () && import->has_location ());
21939 : :
21940 : 2275 : direct_import (import, reader);
21941 : : }
21942 : :
21943 : : /* Declare the name of the current module to be NAME. EXPORTING_p is
21944 : : true if this TU is the exporting module unit. */
21945 : :
21946 : : void
21947 : 2862 : declare_module (module_state *module, location_t from_loc, bool exporting_p,
21948 : : tree, cpp_reader *reader)
21949 : : {
21950 : 2862 : gcc_assert (global_namespace == current_scope ());
21951 : :
21952 : 2862 : module_state *current = this_module ();
21953 : 2862 : if (module_purview_p () || module->loadedness > ML_CONFIG)
21954 : : {
21955 : 6 : auto_diagnostic_group d;
21956 : 12 : error_at (from_loc, module_purview_p ()
21957 : : ? G_("module already declared")
21958 : : : G_("module already imported"));
21959 : 6 : if (module_purview_p ())
21960 : 0 : module = current;
21961 : 12 : inform (module->loc, module_purview_p ()
21962 : : ? G_("module %qs declared here")
21963 : : : G_("module %qs imported here"),
21964 : : module->get_flatname ());
21965 : 6 : return;
21966 : 6 : }
21967 : :
21968 : 2856 : gcc_checking_assert (module->is_module ());
21969 : 2856 : gcc_checking_assert (module->is_direct () && module->has_location ());
21970 : :
21971 : : /* Yer a module, 'arry. */
21972 : 2856 : module_kind = module->is_header () ? MK_HEADER : MK_NAMED | MK_ATTACH;
21973 : :
21974 : : // Even in header units, we consider the decls to be purview
21975 : 2856 : module_kind |= MK_PURVIEW;
21976 : :
21977 : 2856 : if (module->is_partition ())
21978 : 172 : module_kind |= MK_PARTITION;
21979 : 2856 : if (exporting_p)
21980 : : {
21981 : 2575 : module->interface_p = true;
21982 : 2575 : module_kind |= MK_INTERFACE;
21983 : : }
21984 : :
21985 : 2856 : if (module_has_cmi_p ())
21986 : : {
21987 : : /* Copy the importing information we may have already done. We
21988 : : do not need to separate out the imports that only happen in
21989 : : the GMF, inspite of what the literal wording of the std
21990 : : might imply. See p2191, the core list had a discussion
21991 : : where the module implementors agreed that the GMF of a named
21992 : : module is invisible to importers. */
21993 : 2628 : module->imports = current->imports;
21994 : :
21995 : 2628 : module->mod = 0;
21996 : 2628 : (*modules)[0] = module;
21997 : : }
21998 : : else
21999 : : {
22000 : 228 : module->interface_p = true;
22001 : 228 : current->parent = module; /* So mangler knows module identity. */
22002 : 228 : direct_import (module, reader);
22003 : : }
22004 : : }
22005 : :
22006 : : /* Return true IFF we must emit a module global initializer function
22007 : : (which will be called by importers' init code). */
22008 : :
22009 : : bool
22010 : 101826 : module_global_init_needed ()
22011 : : {
22012 : 101826 : return module_has_cmi_p () && !header_module_p ();
22013 : : }
22014 : :
22015 : : /* Calculate which, if any, import initializers need calling. */
22016 : :
22017 : : bool
22018 : 94835 : module_determine_import_inits ()
22019 : : {
22020 : 94835 : if (!modules || header_module_p ())
22021 : : return false;
22022 : :
22023 : : /* Prune active_init_p. We need the same bitmap allocation
22024 : : scheme as for the imports member. */
22025 : 3437 : function_depth++; /* Disable GC. */
22026 : 3437 : bitmap covered_imports (BITMAP_GGC_ALLOC ());
22027 : :
22028 : 3437 : bool any = false;
22029 : :
22030 : : /* Because indirect imports are before their direct import, and
22031 : : we're scanning the array backwards, we only need one pass! */
22032 : 6003 : for (unsigned ix = modules->length (); --ix;)
22033 : : {
22034 : 2566 : module_state *import = (*modules)[ix];
22035 : :
22036 : 2566 : if (!import->active_init_p)
22037 : : ;
22038 : 43 : else if (bitmap_bit_p (covered_imports, ix))
22039 : 6 : import->active_init_p = false;
22040 : : else
22041 : : {
22042 : : /* Everything this imports is therefore handled by its
22043 : : initializer, so doesn't need initializing by us. */
22044 : 37 : bitmap_ior_into (covered_imports, import->imports);
22045 : 37 : any = true;
22046 : : }
22047 : : }
22048 : 3437 : function_depth--;
22049 : :
22050 : 3437 : return any;
22051 : : }
22052 : :
22053 : : /* Emit calls to each direct import's global initializer. Including
22054 : : direct imports of directly imported header units. The initializers
22055 : : of (static) entities in header units will be called by their
22056 : : importing modules (for the instance contained within that), or by
22057 : : the current TU (for the instances we've brought in). Of course
22058 : : such header unit behaviour is evil, but iostream went through that
22059 : : door some time ago. */
22060 : :
22061 : : void
22062 : 37 : module_add_import_initializers ()
22063 : : {
22064 : 37 : if (!modules || header_module_p ())
22065 : 0 : return;
22066 : :
22067 : 37 : tree fntype = build_function_type (void_type_node, void_list_node);
22068 : 37 : releasing_vec args; // There are no args
22069 : :
22070 : 83 : for (unsigned ix = modules->length (); --ix;)
22071 : : {
22072 : 46 : module_state *import = (*modules)[ix];
22073 : 46 : if (import->active_init_p)
22074 : : {
22075 : 37 : tree name = mangle_module_global_init (ix);
22076 : 37 : tree fndecl = build_lang_decl (FUNCTION_DECL, name, fntype);
22077 : :
22078 : 37 : DECL_CONTEXT (fndecl) = FROB_CONTEXT (global_namespace);
22079 : 37 : SET_DECL_ASSEMBLER_NAME (fndecl, name);
22080 : 37 : TREE_PUBLIC (fndecl) = true;
22081 : 37 : determine_visibility (fndecl);
22082 : :
22083 : 37 : tree call = cp_build_function_call_vec (fndecl, &args,
22084 : : tf_warning_or_error);
22085 : 37 : finish_expr_stmt (call);
22086 : : }
22087 : : }
22088 : 37 : }
22089 : :
22090 : : /* NAME & LEN are a preprocessed header name, possibly including the
22091 : : surrounding "" or <> characters. Return the raw string name of the
22092 : : module to which it refers. This will be an absolute path, or begin
22093 : : with ./, so it is immediately distinguishable from a (non-header
22094 : : unit) module name. If READER is non-null, ask the preprocessor to
22095 : : locate the header to which it refers using the appropriate include
22096 : : path. Note that we do never do \ processing of the string, as that
22097 : : matches the preprocessor's behaviour. */
22098 : :
22099 : : static const char *
22100 : 23332 : canonicalize_header_name (cpp_reader *reader, location_t loc, bool unquoted,
22101 : : const char *str, size_t &len_r)
22102 : : {
22103 : 23332 : size_t len = len_r;
22104 : 23332 : static char *buf = 0;
22105 : 23332 : static size_t alloc = 0;
22106 : :
22107 : 23332 : if (!unquoted)
22108 : : {
22109 : 4 : gcc_checking_assert (len >= 2
22110 : : && ((reader && str[0] == '<' && str[len-1] == '>')
22111 : : || (str[0] == '"' && str[len-1] == '"')));
22112 : 4 : str += 1;
22113 : 4 : len -= 2;
22114 : : }
22115 : :
22116 : 23332 : if (reader)
22117 : : {
22118 : 4 : gcc_assert (!unquoted);
22119 : :
22120 : 4 : if (len >= alloc)
22121 : : {
22122 : 4 : alloc = len + 1;
22123 : 4 : buf = XRESIZEVEC (char, buf, alloc);
22124 : : }
22125 : 4 : memcpy (buf, str, len);
22126 : 4 : buf[len] = 0;
22127 : :
22128 : 8 : if (const char *hdr
22129 : 4 : = cpp_probe_header_unit (reader, buf, str[-1] == '<', loc))
22130 : : {
22131 : 4 : len = strlen (hdr);
22132 : 4 : str = hdr;
22133 : : }
22134 : : else
22135 : 0 : str = buf;
22136 : : }
22137 : :
22138 : 23332 : if (!(str[0] == '.' ? IS_DIR_SEPARATOR (str[1]) : IS_ABSOLUTE_PATH (str)))
22139 : : {
22140 : : /* Prepend './' */
22141 : 9 : if (len + 3 > alloc)
22142 : : {
22143 : 9 : alloc = len + 3;
22144 : 9 : buf = XRESIZEVEC (char, buf, alloc);
22145 : : }
22146 : :
22147 : 9 : buf[0] = '.';
22148 : 9 : buf[1] = DIR_SEPARATOR;
22149 : 9 : memmove (buf + 2, str, len);
22150 : 9 : len += 2;
22151 : 9 : buf[len] = 0;
22152 : 9 : str = buf;
22153 : : }
22154 : :
22155 : 23332 : len_r = len;
22156 : 23332 : return str;
22157 : : }
22158 : :
22159 : : /* Set the CMI name from a cody packet. Issue an error if
22160 : : ill-formed. */
22161 : :
22162 : 5256 : void module_state::set_filename (const Cody::Packet &packet)
22163 : : {
22164 : 5256 : if (packet.GetCode () == Cody::Client::PC_PATHNAME)
22165 : : {
22166 : : /* If we've seen this import before we better have the same CMI. */
22167 : 5253 : const std::string &path = packet.GetString ();
22168 : 5253 : if (!filename)
22169 : 5250 : filename = xstrdup (packet.GetString ().c_str ());
22170 : 3 : else if (filename != path)
22171 : 0 : error_at (loc, "mismatching compiled module interface: "
22172 : : "had %qs, got %qs", filename, path.c_str ());
22173 : : }
22174 : : else
22175 : : {
22176 : 3 : gcc_checking_assert (packet.GetCode () == Cody::Client::PC_ERROR);
22177 : 3 : fatal_error (loc, "unknown compiled module interface: %s",
22178 : 3 : packet.GetString ().c_str ());
22179 : : }
22180 : 5253 : }
22181 : :
22182 : : /* Figure out whether to treat HEADER as an include or an import. */
22183 : :
22184 : : static char *
22185 : 22437 : maybe_translate_include (cpp_reader *reader, line_maps *lmaps, location_t loc,
22186 : : const char *path)
22187 : : {
22188 : 22437 : if (!modules_p ())
22189 : : {
22190 : : /* Turn off. */
22191 : 0 : cpp_get_callbacks (reader)->translate_include = NULL;
22192 : 0 : return nullptr;
22193 : : }
22194 : :
22195 : 22437 : dump.push (NULL);
22196 : :
22197 : 24349 : dump () && dump ("Checking include translation '%s'", path);
22198 : 22437 : auto *mapper = get_mapper (cpp_main_loc (reader), cpp_get_deps (reader));
22199 : :
22200 : 22437 : size_t len = strlen (path);
22201 : 22437 : path = canonicalize_header_name (NULL, loc, true, path, len);
22202 : 22437 : auto packet = mapper->IncludeTranslate (path, Cody::Flags::None, len);
22203 : :
22204 : 22437 : enum class xlate_kind {
22205 : : unknown, text, import,
22206 : 22437 : } translate = xlate_kind::unknown;
22207 : :
22208 : 22437 : if (packet.GetCode () == Cody::Client::PC_BOOL)
22209 : 22390 : translate = packet.GetInteger () ? xlate_kind::text : xlate_kind::unknown;
22210 : 47 : else if (packet.GetCode () == Cody::Client::PC_PATHNAME)
22211 : : {
22212 : : /* Record the CMI name for when we do the import.
22213 : : We may already know about this import, but libcpp doesn't yet. */
22214 : 47 : module_state *import = get_module (build_string (len, path));
22215 : 47 : import->set_filename (packet);
22216 : 47 : translate = xlate_kind::import;
22217 : : }
22218 : : else
22219 : : {
22220 : 0 : gcc_checking_assert (packet.GetCode () == Cody::Client::PC_ERROR);
22221 : 0 : error_at (loc, "cannot determine %<#include%> translation of %s: %s",
22222 : 0 : path, packet.GetString ().c_str ());
22223 : : }
22224 : :
22225 : 22437 : bool note = false;
22226 : 22437 : if (note_include_translate_yes && translate == xlate_kind::import)
22227 : : note = true;
22228 : 22434 : else if (note_include_translate_no && translate == xlate_kind::unknown)
22229 : : note = true;
22230 : 22428 : else if (note_includes)
22231 : : /* We do not expect the note_includes vector to be large, so O(N)
22232 : : iteration. */
22233 : 410 : for (unsigned ix = note_includes->length (); !note && ix--;)
22234 : 205 : if (!strcmp ((*note_includes)[ix], path))
22235 : 1 : note = true;
22236 : :
22237 : 205 : if (note)
22238 : 16 : inform (loc, translate == xlate_kind::import
22239 : : ? G_("include %qs translated to import")
22240 : : : G_("include %qs processed textually"), path);
22241 : :
22242 : 26258 : dump () && dump (translate == xlate_kind::import
22243 : : ? "Translating include to import"
22244 : : : "Keeping include as include");
22245 : 22437 : dump.pop (0);
22246 : :
22247 : 22437 : if (translate != xlate_kind::import)
22248 : : return nullptr;
22249 : :
22250 : : /* Create the translation text. */
22251 : 47 : loc = ordinary_loc_of (lmaps, loc);
22252 : 47 : const line_map_ordinary *map
22253 : 47 : = linemap_check_ordinary (linemap_lookup (lmaps, loc));
22254 : 47 : unsigned col = SOURCE_COLUMN (map, loc);
22255 : 47 : col -= (col != 0); /* Columns are 1-based. */
22256 : :
22257 : 47 : unsigned alloc = len + col + 60;
22258 : 47 : char *res = XNEWVEC (char, alloc);
22259 : :
22260 : 47 : strcpy (res, "__import");
22261 : 47 : unsigned actual = 8;
22262 : 47 : if (col > actual)
22263 : : {
22264 : : /* Pad out so the filename appears at the same position. */
22265 : 44 : memset (res + actual, ' ', col - actual);
22266 : 44 : actual = col;
22267 : : }
22268 : : /* No need to encode characters, that's not how header names are
22269 : : handled. */
22270 : 47 : actual += snprintf (res + actual, alloc - actual,
22271 : : "\"%s\" [[__translated]];\n", path);
22272 : 47 : gcc_checking_assert (actual < alloc);
22273 : :
22274 : : /* cpplib will delete the buffer. */
22275 : : return res;
22276 : 22437 : }
22277 : :
22278 : : static void
22279 : 891 : begin_header_unit (cpp_reader *reader)
22280 : : {
22281 : : /* Set the module header name from the main_input_filename. */
22282 : 891 : const char *main = main_input_filename;
22283 : 891 : size_t len = strlen (main);
22284 : 891 : main = canonicalize_header_name (NULL, 0, true, main, len);
22285 : 891 : module_state *module = get_module (build_string (len, main));
22286 : :
22287 : 891 : preprocess_module (module, cpp_main_loc (reader), false, false, true, reader);
22288 : 891 : }
22289 : :
22290 : : /* We've just properly entered the main source file. I.e. after the
22291 : : command line, builtins and forced headers. Record the line map and
22292 : : location of this map. Note we may be called more than once. The
22293 : : first call sticks. */
22294 : :
22295 : : void
22296 : 96747 : module_begin_main_file (cpp_reader *reader, line_maps *lmaps,
22297 : : const line_map_ordinary *map)
22298 : : {
22299 : 96747 : gcc_checking_assert (lmaps == line_table);
22300 : 96747 : if (modules_p () && !spans.init_p ())
22301 : : {
22302 : 4483 : unsigned n = dump.push (NULL);
22303 : 4483 : spans.init (lmaps, map);
22304 : 4483 : dump.pop (n);
22305 : 4483 : if (flag_header_unit && !cpp_get_options (reader)->preprocessed)
22306 : : {
22307 : : /* Tell the preprocessor this is an include file. */
22308 : 882 : cpp_retrofit_as_include (reader);
22309 : 882 : begin_header_unit (reader);
22310 : : }
22311 : : }
22312 : 96747 : }
22313 : :
22314 : : /* Process the pending_import queue, making sure we know the
22315 : : filenames. */
22316 : :
22317 : : static void
22318 : 5354 : name_pending_imports (cpp_reader *reader)
22319 : : {
22320 : 5354 : auto *mapper = get_mapper (cpp_main_loc (reader), cpp_get_deps (reader));
22321 : :
22322 : 5354 : if (!vec_safe_length (pending_imports))
22323 : : /* Not doing anything. */
22324 : : return;
22325 : :
22326 : 4554 : timevar_start (TV_MODULE_MAPPER);
22327 : :
22328 : 4554 : auto n = dump.push (NULL);
22329 : 5134 : dump () && dump ("Resolving direct import names");
22330 : 4554 : bool want_deps = (bool (mapper->get_flags () & Cody::Flags::NameOnly)
22331 : 4554 : || cpp_get_deps (reader));
22332 : 4554 : bool any = false;
22333 : :
22334 : 9936 : for (unsigned ix = 0; ix != pending_imports->length (); ix++)
22335 : : {
22336 : 5382 : module_state *module = (*pending_imports)[ix];
22337 : 5382 : gcc_checking_assert (module->is_direct ());
22338 : 5382 : if (!module->filename && !module->visited_p)
22339 : : {
22340 : 5275 : bool export_p = (module->is_module ()
22341 : 5275 : && (module->is_partition ()
22342 : 2774 : || module->is_exported ()));
22343 : :
22344 : 5275 : Cody::Flags flags = Cody::Flags::None;
22345 : 5275 : if (flag_preprocess_only
22346 : 5275 : && !(module->is_header () && !export_p))
22347 : : {
22348 : 129 : if (!want_deps)
22349 : 66 : continue;
22350 : : flags = Cody::Flags::NameOnly;
22351 : : }
22352 : :
22353 : 5209 : if (!any)
22354 : : {
22355 : 4484 : any = true;
22356 : 4484 : mapper->Cork ();
22357 : : }
22358 : 5209 : if (export_p)
22359 : 2679 : mapper->ModuleExport (module->get_flatname (), flags);
22360 : : else
22361 : 2530 : mapper->ModuleImport (module->get_flatname (), flags);
22362 : 5209 : module->visited_p = true;
22363 : : }
22364 : : }
22365 : :
22366 : 4554 : if (any)
22367 : : {
22368 : 4484 : auto response = mapper->Uncork ();
22369 : 4484 : auto r_iter = response.begin ();
22370 : 9781 : for (unsigned ix = 0; ix != pending_imports->length (); ix++)
22371 : : {
22372 : 5300 : module_state *module = (*pending_imports)[ix];
22373 : 5300 : if (module->visited_p)
22374 : : {
22375 : 5209 : module->visited_p = false;
22376 : 5209 : gcc_checking_assert (!module->filename);
22377 : :
22378 : 5209 : module->set_filename (*r_iter);
22379 : 5206 : ++r_iter;
22380 : : }
22381 : : }
22382 : 4481 : }
22383 : :
22384 : 4551 : dump.pop (n);
22385 : :
22386 : 4551 : timevar_stop (TV_MODULE_MAPPER);
22387 : : }
22388 : :
22389 : : /* We've just lexed a module-specific control line for MODULE. Mark
22390 : : the module as a direct import, and possibly load up its macro
22391 : : state. Returns the primary module, if this is a module
22392 : : declaration. */
22393 : : /* Perhaps we should offer a preprocessing mode where we read the
22394 : : directives from the header unit, rather than require the header's
22395 : : CMI. */
22396 : :
22397 : : module_state *
22398 : 5401 : preprocess_module (module_state *module, location_t from_loc,
22399 : : bool in_purview, bool is_import, bool is_export,
22400 : : cpp_reader *reader)
22401 : : {
22402 : 5401 : if (!is_import)
22403 : : {
22404 : 3006 : if (module->loc)
22405 : : /* It's already been mentioned, so ignore its module-ness. */
22406 : : is_import = true;
22407 : : else
22408 : : {
22409 : : /* Record it is the module. */
22410 : 2985 : module->module_p = true;
22411 : 2985 : if (is_export)
22412 : : {
22413 : 2671 : module->exported_p = true;
22414 : 2671 : module->interface_p = true;
22415 : : }
22416 : : }
22417 : : }
22418 : :
22419 : 5401 : if (module->directness < MD_DIRECT + in_purview)
22420 : : {
22421 : : /* Mark as a direct import. */
22422 : 5355 : module->directness = module_directness (MD_DIRECT + in_purview);
22423 : :
22424 : : /* Set the location to be most informative for users. */
22425 : 5355 : from_loc = ordinary_loc_of (line_table, from_loc);
22426 : 5355 : if (module->loadedness != ML_NONE)
22427 : 6 : linemap_module_reparent (line_table, module->loc, from_loc);
22428 : : else
22429 : : {
22430 : : /* Don't overwrite the location if we're importing ourselves
22431 : : after already having seen a module-declaration. */
22432 : 5349 : if (!(is_import && module->is_module ()))
22433 : 5322 : module->loc = from_loc;
22434 : 5349 : if (!module->flatname)
22435 : 5316 : module->set_flatname ();
22436 : : }
22437 : : }
22438 : :
22439 : 5401 : auto desired = ML_CONFIG;
22440 : 5401 : if (is_import
22441 : 2416 : && module->is_header ()
22442 : 6285 : && (!cpp_get_options (reader)->preprocessed
22443 : 3 : || cpp_get_options (reader)->directives_only))
22444 : : /* We need preprocessor state now. */
22445 : : desired = ML_PREPROCESSOR;
22446 : :
22447 : 5401 : if (!is_import || module->loadedness < desired)
22448 : : {
22449 : 5382 : vec_safe_push (pending_imports, module);
22450 : :
22451 : 5382 : if (desired == ML_PREPROCESSOR)
22452 : : {
22453 : 865 : unsigned n = dump.push (NULL);
22454 : :
22455 : 1064 : dump () && dump ("Reading %M preprocessor state", module);
22456 : 865 : name_pending_imports (reader);
22457 : :
22458 : : /* Preserve the state of the line-map. */
22459 : 865 : auto pre_hwm = LINEMAPS_ORDINARY_USED (line_table);
22460 : :
22461 : : /* We only need to close the span, if we're going to emit a
22462 : : CMI. But that's a little tricky -- our token scanner
22463 : : needs to be smarter -- and this isn't much state.
22464 : : Remember, we've not parsed anything at this point, so
22465 : : our module state flags are inadequate. */
22466 : 865 : spans.maybe_init ();
22467 : 865 : spans.close ();
22468 : :
22469 : 865 : timevar_start (TV_MODULE_IMPORT);
22470 : :
22471 : : /* Load the config of each pending import -- we must assign
22472 : : module numbers monotonically. */
22473 : 1915 : for (unsigned ix = 0; ix != pending_imports->length (); ix++)
22474 : : {
22475 : 1050 : auto *import = (*pending_imports)[ix];
22476 : 1226 : if (!(import->is_module ()
22477 : 176 : && (import->is_partition () || import->is_exported ()))
22478 : 880 : && import->loadedness == ML_NONE
22479 : 1930 : && (import->is_header () || !flag_preprocess_only))
22480 : : {
22481 : 871 : unsigned n = dump.push (import);
22482 : 871 : import->do_import (reader, true);
22483 : 871 : dump.pop (n);
22484 : : }
22485 : : }
22486 : 865 : vec_free (pending_imports);
22487 : :
22488 : : /* Restore the line-map state. */
22489 : 865 : spans.open (linemap_module_restore (line_table, pre_hwm));
22490 : :
22491 : : /* Now read the preprocessor state of this particular
22492 : : import. */
22493 : 865 : if (module->loadedness == ML_CONFIG
22494 : 865 : && module->read_preprocessor (true))
22495 : 862 : module->import_macros ();
22496 : :
22497 : 865 : timevar_stop (TV_MODULE_IMPORT);
22498 : :
22499 : 865 : dump.pop (n);
22500 : : }
22501 : : }
22502 : :
22503 : 5401 : return is_import ? NULL : get_primary (module);
22504 : : }
22505 : :
22506 : : /* We've completed phase-4 translation. Emit any dependency
22507 : : information for the not-yet-loaded direct imports, and fill in
22508 : : their file names. We'll have already loaded up the direct header
22509 : : unit wavefront. */
22510 : :
22511 : : void
22512 : 4489 : preprocessed_module (cpp_reader *reader)
22513 : : {
22514 : 4489 : unsigned n = dump.push (NULL);
22515 : :
22516 : 5033 : dump () && dump ("Completed phase-4 (tokenization) processing");
22517 : :
22518 : 4489 : name_pending_imports (reader);
22519 : 4486 : vec_free (pending_imports);
22520 : :
22521 : 4486 : spans.maybe_init ();
22522 : 4486 : spans.close ();
22523 : :
22524 : 4486 : using iterator = hash_table<module_state_hash>::iterator;
22525 : 4486 : if (mkdeps *deps = cpp_get_deps (reader))
22526 : : {
22527 : : /* Walk the module hash, informing the dependency machinery. */
22528 : 51 : iterator end = modules_hash->end ();
22529 : 294 : for (iterator iter = modules_hash->begin (); iter != end; ++iter)
22530 : : {
22531 : 96 : module_state *module = *iter;
22532 : :
22533 : 96 : if (module->is_direct ())
22534 : : {
22535 : 81 : if (module->is_module ()
22536 : 81 : && (module->is_interface () || module->is_partition ()))
22537 : 33 : deps_add_module_target (deps, module->get_flatname (),
22538 : 33 : maybe_add_cmi_prefix (module->filename),
22539 : 33 : module->is_header (),
22540 : 33 : module->is_exported ());
22541 : : else
22542 : 48 : deps_add_module_dep (deps, module->get_flatname ());
22543 : : }
22544 : : }
22545 : : }
22546 : :
22547 : 4486 : if (flag_header_unit && !flag_preprocess_only)
22548 : : {
22549 : : /* Find the main module -- remember, it's not yet in the module
22550 : : array. */
22551 : 879 : iterator end = modules_hash->end ();
22552 : 1866 : for (iterator iter = modules_hash->begin (); iter != end; ++iter)
22553 : : {
22554 : 933 : module_state *module = *iter;
22555 : 933 : if (module->is_module ())
22556 : : {
22557 : 879 : declare_module (module, cpp_main_loc (reader), true, NULL, reader);
22558 : 879 : module_kind |= MK_EXPORTING;
22559 : 879 : break;
22560 : : }
22561 : : }
22562 : : }
22563 : :
22564 : 4486 : dump.pop (n);
22565 : 4486 : }
22566 : :
22567 : : /* VAL is a global tree, add it to the global vec if it is
22568 : : interesting. Add some of its targets, if they too are
22569 : : interesting. We do not add identifiers, as they can be re-found
22570 : : via the identifier hash table. There is a cost to the number of
22571 : : global trees. */
22572 : :
22573 : : static int
22574 : 2777260 : maybe_add_global (tree val, unsigned &crc)
22575 : : {
22576 : 2777260 : int v = 0;
22577 : :
22578 : 2777260 : if (val && !(identifier_p (val) || TREE_VISITED (val)))
22579 : : {
22580 : 855968 : TREE_VISITED (val) = true;
22581 : 855968 : crc = crc32_unsigned (crc, fixed_trees->length ());
22582 : 855968 : vec_safe_push (fixed_trees, val);
22583 : 855968 : v++;
22584 : :
22585 : 855968 : if (CODE_CONTAINS_STRUCT (TREE_CODE (val), TS_TYPED))
22586 : 855968 : v += maybe_add_global (TREE_TYPE (val), crc);
22587 : 855968 : if (CODE_CONTAINS_STRUCT (TREE_CODE (val), TS_TYPE_COMMON))
22588 : 561125 : v += maybe_add_global (TYPE_NAME (val), crc);
22589 : : }
22590 : :
22591 : 2777260 : return v;
22592 : : }
22593 : :
22594 : : /* Initialize module state. Create the hash table, determine the
22595 : : global trees. Create the module for current TU. */
22596 : :
22597 : : void
22598 : 4489 : init_modules (cpp_reader *reader)
22599 : : {
22600 : : /* PCH should not be reachable because of lang-specs, but the
22601 : : user could have overriden that. */
22602 : 4489 : if (pch_file)
22603 : 0 : fatal_error (input_location,
22604 : : "C++ modules are incompatible with precompiled headers");
22605 : :
22606 : 4489 : if (cpp_get_options (reader)->traditional)
22607 : 0 : fatal_error (input_location,
22608 : : "C++ modules are incompatible with traditional preprocessing");
22609 : :
22610 : : /* :: is always exported. */
22611 : 4489 : DECL_MODULE_EXPORT_P (global_namespace) = true;
22612 : :
22613 : 4489 : modules_hash = hash_table<module_state_hash>::create_ggc (31);
22614 : 4489 : vec_safe_reserve (modules, 20);
22615 : :
22616 : : /* Create module for current TU. */
22617 : 4489 : module_state *current
22618 : 4489 : = new (ggc_alloc<module_state> ()) module_state (NULL_TREE, NULL, false);
22619 : 4489 : current->mod = 0;
22620 : 4489 : bitmap_set_bit (current->imports, 0);
22621 : 4489 : modules->quick_push (current);
22622 : :
22623 : 4489 : gcc_checking_assert (!fixed_trees);
22624 : :
22625 : 4489 : headers = BITMAP_GGC_ALLOC ();
22626 : :
22627 : 4489 : if (note_includes)
22628 : : /* Canonicalize header names. */
22629 : 2 : for (unsigned ix = 0; ix != note_includes->length (); ix++)
22630 : : {
22631 : 1 : const char *hdr = (*note_includes)[ix];
22632 : 1 : size_t len = strlen (hdr);
22633 : :
22634 : 1 : bool system = hdr[0] == '<';
22635 : 1 : bool user = hdr[0] == '"';
22636 : 1 : bool delimed = system || user;
22637 : :
22638 : 1 : if (len <= (delimed ? 2 : 0)
22639 : 1 : || (delimed && hdr[len-1] != (system ? '>' : '"')))
22640 : 0 : error ("invalid header name %qs", hdr);
22641 : :
22642 : 1 : hdr = canonicalize_header_name (delimed ? reader : NULL,
22643 : : 0, !delimed, hdr, len);
22644 : 1 : char *path = XNEWVEC (char, len + 1);
22645 : 1 : memcpy (path, hdr, len);
22646 : 1 : path[len] = 0;
22647 : :
22648 : 1 : (*note_includes)[ix] = path;
22649 : : }
22650 : :
22651 : 4489 : if (note_cmis)
22652 : : /* Canonicalize & mark module names. */
22653 : 12 : for (unsigned ix = 0; ix != note_cmis->length (); ix++)
22654 : : {
22655 : 6 : const char *name = (*note_cmis)[ix];
22656 : 6 : size_t len = strlen (name);
22657 : :
22658 : 6 : bool is_system = name[0] == '<';
22659 : 6 : bool is_user = name[0] == '"';
22660 : 6 : bool is_pathname = false;
22661 : 6 : if (!(is_system || is_user))
22662 : 12 : for (unsigned ix = len; !is_pathname && ix--;)
22663 : 9 : is_pathname = IS_DIR_SEPARATOR (name[ix]);
22664 : 6 : if (is_system || is_user || is_pathname)
22665 : : {
22666 : 3 : if (len <= (is_pathname ? 0 : 2)
22667 : 3 : || (!is_pathname && name[len-1] != (is_system ? '>' : '"')))
22668 : : {
22669 : 0 : error ("invalid header name %qs", name);
22670 : 0 : continue;
22671 : : }
22672 : : else
22673 : 3 : name = canonicalize_header_name (is_pathname ? nullptr : reader,
22674 : : 0, is_pathname, name, len);
22675 : : }
22676 : 6 : if (auto module = get_module (name))
22677 : 6 : module->inform_cmi_p = 1;
22678 : : else
22679 : 0 : error ("invalid module name %qs", name);
22680 : : }
22681 : :
22682 : 4489 : dump.push (NULL);
22683 : :
22684 : : /* Determine lazy handle bound. */
22685 : 4489 : {
22686 : 4489 : unsigned limit = 1000;
22687 : : #if HAVE_GETRLIMIT
22688 : 4489 : struct rlimit rlimit;
22689 : 4489 : if (!getrlimit (RLIMIT_NOFILE, &rlimit))
22690 : : {
22691 : 4489 : lazy_hard_limit = (rlimit.rlim_max < 1000000
22692 : 4489 : ? unsigned (rlimit.rlim_max) : 1000000);
22693 : 4489 : lazy_hard_limit = (lazy_hard_limit > LAZY_HEADROOM
22694 : 4489 : ? lazy_hard_limit - LAZY_HEADROOM : 0);
22695 : 4489 : if (rlimit.rlim_cur < limit)
22696 : 0 : limit = unsigned (rlimit.rlim_cur);
22697 : : }
22698 : : #endif
22699 : 4489 : limit = limit > LAZY_HEADROOM ? limit - LAZY_HEADROOM : 1;
22700 : :
22701 : 4489 : if (unsigned parm = param_lazy_modules)
22702 : : {
22703 : 4489 : if (parm <= limit || !lazy_hard_limit || !try_increase_lazy (parm))
22704 : 6 : lazy_limit = parm;
22705 : : }
22706 : : else
22707 : 0 : lazy_limit = limit;
22708 : : }
22709 : :
22710 : 4489 : if (dump ())
22711 : : {
22712 : 544 : verstr_t ver;
22713 : 544 : version2string (MODULE_VERSION, ver);
22714 : 544 : dump ("Source: %s", main_input_filename);
22715 : 544 : dump ("Compiler: %s", version_string);
22716 : 544 : dump ("Modules: %s", ver);
22717 : 544 : dump ("Checking: %s",
22718 : : #if CHECKING_P
22719 : : "checking"
22720 : : #elif ENABLE_ASSERT_CHECKING
22721 : : "asserting"
22722 : : #else
22723 : : "release"
22724 : : #endif
22725 : : );
22726 : 544 : dump ("Compiled by: "
22727 : : #ifdef __GNUC__
22728 : : "GCC %d.%d, %s", __GNUC__, __GNUC_MINOR__,
22729 : : #ifdef __OPTIMIZE__
22730 : : "optimizing"
22731 : : #else
22732 : : "not optimizing"
22733 : : #endif
22734 : : #else
22735 : : "not GCC"
22736 : : #endif
22737 : : );
22738 : 544 : dump ("Reading: %s", MAPPED_READING ? "mmap" : "fileio");
22739 : 544 : dump ("Writing: %s", MAPPED_WRITING ? "mmap" : "fileio");
22740 : 544 : dump ("Lazy limit: %u", lazy_limit);
22741 : 544 : dump ("Lazy hard limit: %u", lazy_hard_limit);
22742 : 544 : dump ("");
22743 : : }
22744 : :
22745 : : /* Construct the global tree array. This is an array of unique
22746 : : global trees (& types). Do this now, rather than lazily, as
22747 : : some global trees are lazily created and we don't want that to
22748 : : mess with our syndrome of fixed trees. */
22749 : 4489 : unsigned crc = 0;
22750 : 4489 : vec_alloc (fixed_trees, 250);
22751 : :
22752 : 5033 : dump () && dump ("+Creating globals");
22753 : : /* Insert the TRANSLATION_UNIT_DECL. */
22754 : 4489 : TREE_VISITED (DECL_CONTEXT (global_namespace)) = true;
22755 : 4489 : fixed_trees->quick_push (DECL_CONTEXT (global_namespace));
22756 : 26934 : for (unsigned jx = 0; global_tree_arys[jx].first; jx++)
22757 : : {
22758 : 22445 : const tree *ptr = global_tree_arys[jx].first;
22759 : 22445 : unsigned limit = global_tree_arys[jx].second;
22760 : :
22761 : 1369145 : for (unsigned ix = 0; ix != limit; ix++, ptr++)
22762 : : {
22763 : 1346700 : !(ix & 31) && dump ("") && dump ("+\t%u:%u:", jx, ix);
22764 : 1346700 : unsigned v = maybe_add_global (*ptr, crc);
22765 : 1509900 : dump () && dump ("+%u", v);
22766 : : }
22767 : : }
22768 : : /* OS- and machine-specific types are dynamically registered at
22769 : : runtime, so cannot be part of global_tree_arys. */
22770 : 4489 : registered_builtin_types && dump ("") && dump ("+\tB:");
22771 : 17956 : for (tree t = registered_builtin_types; t; t = TREE_CHAIN (t))
22772 : : {
22773 : 13467 : unsigned v = maybe_add_global (TREE_VALUE (t), crc);
22774 : 15099 : dump () && dump ("+%u", v);
22775 : : }
22776 : 4489 : global_crc = crc32_unsigned (crc, fixed_trees->length ());
22777 : 4489 : dump ("") && dump ("Created %u unique globals, crc=%x",
22778 : : fixed_trees->length (), global_crc);
22779 : 864946 : for (unsigned ix = fixed_trees->length (); ix--;)
22780 : 860457 : TREE_VISITED ((*fixed_trees)[ix]) = false;
22781 : :
22782 : 4489 : dump.pop (0);
22783 : :
22784 : 4489 : if (!flag_module_lazy)
22785 : : /* Get the mapper now, if we're not being lazy. */
22786 : 284 : get_mapper (cpp_main_loc (reader), cpp_get_deps (reader));
22787 : :
22788 : 4489 : if (!flag_preprocess_only)
22789 : : {
22790 : 4356 : pending_table = new pending_map_t (EXPERIMENT (1, 400));
22791 : 4356 : entity_map = new entity_map_t (EXPERIMENT (1, 400));
22792 : 4356 : vec_safe_reserve (entity_ary, EXPERIMENT (1, 400));
22793 : 4356 : imported_temploid_friends
22794 : 4356 : = decl_tree_cache_map::create_ggc (EXPERIMENT (1, 400));
22795 : : }
22796 : :
22797 : : #if CHECKING_P
22798 : 4489 : note_defs = note_defs_table_t::create_ggc (1000);
22799 : : #endif
22800 : :
22801 : 4489 : if (flag_header_unit && cpp_get_options (reader)->preprocessed)
22802 : 9 : begin_header_unit (reader);
22803 : :
22804 : : /* Collect here to make sure things are tagged correctly (when
22805 : : aggressively GC'd). */
22806 : 4489 : ggc_collect ();
22807 : 4489 : }
22808 : :
22809 : : /* If NODE is a deferred macro, load it. */
22810 : :
22811 : : static int
22812 : 82771 : load_macros (cpp_reader *reader, cpp_hashnode *node, void *)
22813 : : {
22814 : 82771 : location_t main_loc
22815 : 82771 : = MAP_START_LOCATION (LINEMAPS_ORDINARY_MAP_AT (line_table, 0));
22816 : :
22817 : 82771 : if (cpp_user_macro_p (node)
22818 : 82771 : && !node->value.macro)
22819 : : {
22820 : 72 : cpp_macro *macro = cpp_get_deferred_macro (reader, node, main_loc);
22821 : 72 : dump () && dump ("Loaded macro #%s %I",
22822 : : macro ? "define" : "undef", identifier (node));
22823 : : }
22824 : :
22825 : 82771 : return 1;
22826 : : }
22827 : :
22828 : : /* At the end of tokenizing, we no longer need the macro tables of
22829 : : imports. But the user might have requested some checking. */
22830 : :
22831 : : void
22832 : 95039 : maybe_check_all_macros (cpp_reader *reader)
22833 : : {
22834 : 95039 : if (!warn_imported_macros)
22835 : : return;
22836 : :
22837 : : /* Force loading of any remaining deferred macros. This will
22838 : : produce diagnostics if they are ill-formed. */
22839 : 21 : unsigned n = dump.push (NULL);
22840 : 21 : cpp_forall_identifiers (reader, load_macros, NULL);
22841 : 21 : dump.pop (n);
22842 : : }
22843 : :
22844 : : // State propagated from finish_module_processing to fini_modules
22845 : :
22846 : : struct module_processing_cookie
22847 : : {
22848 : : elf_out out;
22849 : : module_state_config config;
22850 : : char *cmi_name;
22851 : : char *tmp_name;
22852 : : unsigned crc;
22853 : : bool began;
22854 : :
22855 : 2622 : module_processing_cookie (char *cmi, char *tmp, int fd, int e)
22856 : 2622 : : out (fd, e), cmi_name (cmi), tmp_name (tmp), crc (0), began (false)
22857 : : {
22858 : : }
22859 : 2622 : ~module_processing_cookie ()
22860 : : {
22861 : 2622 : XDELETEVEC (tmp_name);
22862 : 2622 : XDELETEVEC (cmi_name);
22863 : 2622 : }
22864 : : };
22865 : :
22866 : : /* Write the CMI, if we're a module interface. */
22867 : :
22868 : : void *
22869 : 94835 : finish_module_processing (cpp_reader *reader)
22870 : : {
22871 : 94835 : module_processing_cookie *cookie = nullptr;
22872 : :
22873 : 94835 : if (header_module_p ())
22874 : 879 : module_kind &= ~MK_EXPORTING;
22875 : :
22876 : 94835 : if (!modules || !this_module ()->name)
22877 : : {
22878 : 92210 : if (flag_module_only)
22879 : 6 : warning (0, "%<-fmodule-only%> used for non-interface");
22880 : : }
22881 : 2625 : else if (!flag_syntax_only)
22882 : : {
22883 : 2622 : int fd = -1;
22884 : 2622 : int e = -1;
22885 : :
22886 : 2622 : timevar_start (TV_MODULE_EXPORT);
22887 : :
22888 : : /* Force a valid but empty line map at the end. This simplifies
22889 : : the line table preparation and writing logic. */
22890 : 2622 : linemap_add (line_table, LC_ENTER, false, "", 0);
22891 : :
22892 : : /* We write to a tmpname, and then atomically rename. */
22893 : 2622 : char *cmi_name = NULL;
22894 : 2622 : char *tmp_name = NULL;
22895 : 2622 : module_state *state = this_module ();
22896 : :
22897 : 2622 : unsigned n = dump.push (state);
22898 : 2622 : state->announce ("creating");
22899 : 2622 : if (state->filename)
22900 : : {
22901 : 2622 : size_t len = 0;
22902 : 2622 : cmi_name = xstrdup (maybe_add_cmi_prefix (state->filename, &len));
22903 : 2622 : tmp_name = XNEWVEC (char, len + 3);
22904 : 2622 : memcpy (tmp_name, cmi_name, len);
22905 : 2622 : strcpy (&tmp_name[len], "~");
22906 : :
22907 : 2622 : if (!errorcount)
22908 : 33 : for (unsigned again = 2; ; again--)
22909 : : {
22910 : 2552 : fd = open (tmp_name,
22911 : : O_RDWR | O_CREAT | O_TRUNC | O_CLOEXEC | O_BINARY,
22912 : : S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH);
22913 : 2552 : e = errno;
22914 : 2552 : if (fd >= 0 || !again || e != ENOENT)
22915 : : break;
22916 : 33 : create_dirs (tmp_name);
22917 : : }
22918 : 2622 : if (note_module_cmi_yes || state->inform_cmi_p)
22919 : 3 : inform (state->loc, "writing CMI %qs", cmi_name);
22920 : 2895 : dump () && dump ("CMI is %s", cmi_name);
22921 : : }
22922 : :
22923 : 2622 : cookie = new module_processing_cookie (cmi_name, tmp_name, fd, e);
22924 : :
22925 : 2622 : if (errorcount)
22926 : : /* Don't write the module if we have reported errors. */;
22927 : 2519 : else if (erroneous_templates
22928 : 2519 : && !erroneous_templates->is_empty ())
22929 : : {
22930 : : /* Don't write the module if it contains an erroneous template.
22931 : : Also emit notes about where errors occurred in case
22932 : : -Wno-template-body was passed. */
22933 : 6 : auto_diagnostic_group d;
22934 : 6 : error_at (state->loc, "not writing module %qs due to errors "
22935 : : "in template bodies", state->get_flatname ());
22936 : 6 : if (!warn_template_body)
22937 : 3 : inform (state->loc, "enable %<-Wtemplate-body%> for more details");
22938 : 12 : for (auto e : *erroneous_templates)
22939 : 6 : inform (e.second, "first error in %qD appeared here", e.first);
22940 : 6 : }
22941 : 2513 : else if (cookie->out.begin ())
22942 : : {
22943 : : /* So crashes finger-point the module decl. */
22944 : 2513 : iloc_sentinel ils = state->loc;
22945 : 2513 : if (state->write_begin (&cookie->out, reader, cookie->config,
22946 : 2513 : cookie->crc))
22947 : 2484 : cookie->began = true;
22948 : 2513 : }
22949 : :
22950 : 2622 : dump.pop (n);
22951 : 2622 : timevar_stop (TV_MODULE_EXPORT);
22952 : :
22953 : 2622 : ggc_collect ();
22954 : : }
22955 : :
22956 : 94835 : if (modules)
22957 : : {
22958 : 4316 : unsigned n = dump.push (NULL);
22959 : 4860 : dump () && dump ("Imported %u modules", modules->length () - 1);
22960 : 4860 : dump () && dump ("Containing %u clusters", available_clusters);
22961 : 4316 : dump () && dump ("Loaded %u clusters (%u%%)", loaded_clusters,
22962 : 544 : (loaded_clusters * 100 + available_clusters / 2) /
22963 : 544 : (available_clusters + !available_clusters));
22964 : 4316 : dump.pop (n);
22965 : : }
22966 : :
22967 : 94835 : return cookie;
22968 : : }
22969 : :
22970 : : // Do the final emission of a module. At this point we know whether
22971 : : // the module static initializer is a NOP or not.
22972 : :
22973 : : static void
22974 : 2622 : late_finish_module (cpp_reader *reader, module_processing_cookie *cookie,
22975 : : bool init_fn_non_empty)
22976 : : {
22977 : 2622 : timevar_start (TV_MODULE_EXPORT);
22978 : :
22979 : 2622 : module_state *state = this_module ();
22980 : 2622 : unsigned n = dump.push (state);
22981 : 2622 : state->announce ("finishing");
22982 : :
22983 : 2622 : cookie->config.active_init = init_fn_non_empty;
22984 : 2622 : if (cookie->began)
22985 : 2484 : state->write_end (&cookie->out, reader, cookie->config, cookie->crc);
22986 : :
22987 : 2622 : if (cookie->out.end () && cookie->cmi_name)
22988 : : {
22989 : : /* Some OS's do not replace NEWNAME if it already exists.
22990 : : This'll have a race condition in erroneous concurrent
22991 : : builds. */
22992 : 2519 : unlink (cookie->cmi_name);
22993 : 2519 : if (rename (cookie->tmp_name, cookie->cmi_name))
22994 : : {
22995 : 0 : dump () && dump ("Rename ('%s','%s') errno=%u",
22996 : 0 : cookie->tmp_name, cookie->cmi_name, errno);
22997 : 0 : cookie->out.set_error (errno);
22998 : : }
22999 : : }
23000 : :
23001 : 2622 : if (cookie->out.get_error () && cookie->began)
23002 : : {
23003 : 0 : error_at (state->loc, "failed to write compiled module: %s",
23004 : 0 : cookie->out.get_error (state->filename));
23005 : 0 : state->note_cmi_name ();
23006 : : }
23007 : :
23008 : 2622 : if (!errorcount)
23009 : : {
23010 : 2481 : auto *mapper = get_mapper (cpp_main_loc (reader), cpp_get_deps (reader));
23011 : 2481 : mapper->ModuleCompiled (state->get_flatname ());
23012 : : }
23013 : 141 : else if (cookie->cmi_name)
23014 : : {
23015 : : /* We failed, attempt to erase all evidence we even tried. */
23016 : 141 : unlink (cookie->tmp_name);
23017 : 141 : unlink (cookie->cmi_name);
23018 : : }
23019 : :
23020 : 2622 : delete cookie;
23021 : 2622 : dump.pop (n);
23022 : 2622 : timevar_stop (TV_MODULE_EXPORT);
23023 : 2622 : }
23024 : :
23025 : : void
23026 : 94835 : fini_modules (cpp_reader *reader, void *cookie, bool has_inits)
23027 : : {
23028 : 94835 : if (cookie)
23029 : 2622 : late_finish_module (reader,
23030 : : static_cast<module_processing_cookie *> (cookie),
23031 : : has_inits);
23032 : :
23033 : : /* We're done with the macro tables now. */
23034 : 94835 : vec_free (macro_exports);
23035 : 94835 : vec_free (macro_imports);
23036 : 94835 : headers = NULL;
23037 : :
23038 : : /* We're now done with everything but the module names. */
23039 : 94835 : set_cmi_repo (NULL);
23040 : 94835 : if (mapper)
23041 : : {
23042 : 4316 : timevar_start (TV_MODULE_MAPPER);
23043 : 4316 : module_client::close_module_client (0, mapper);
23044 : 4316 : mapper = nullptr;
23045 : 4316 : timevar_stop (TV_MODULE_MAPPER);
23046 : : }
23047 : 94835 : module_state_config::release ();
23048 : :
23049 : : #if CHECKING_P
23050 : 94835 : note_defs = NULL;
23051 : : #endif
23052 : :
23053 : 94835 : if (modules)
23054 : 6987 : for (unsigned ix = modules->length (); --ix;)
23055 : 2671 : if (module_state *state = (*modules)[ix])
23056 : 2671 : state->release ();
23057 : :
23058 : : /* No need to lookup modules anymore. */
23059 : 94835 : modules_hash = NULL;
23060 : :
23061 : : /* Or entity array. We still need the entity map to find import numbers. */
23062 : 94835 : vec_free (entity_ary);
23063 : 94835 : entity_ary = NULL;
23064 : :
23065 : : /* Or remember any pending entities. */
23066 : 99151 : delete pending_table;
23067 : 94835 : pending_table = NULL;
23068 : :
23069 : : /* Or any keys -- Let it go! */
23070 : 97010 : delete keyed_table;
23071 : 94835 : keyed_table = NULL;
23072 : :
23073 : : /* Allow a GC, we've possibly made much data unreachable. */
23074 : 94835 : ggc_collect ();
23075 : 94835 : }
23076 : :
23077 : : /* If CODE is a module option, handle it & return true. Otherwise
23078 : : return false. For unknown reasons I cannot get the option
23079 : : generation machinery to set fmodule-mapper or -fmodule-header to
23080 : : make a string type option variable. */
23081 : :
23082 : : bool
23083 : 1882013 : handle_module_option (unsigned code, const char *str, int)
23084 : : {
23085 : 1882013 : auto hdr = CMS_header;
23086 : :
23087 : 1882013 : switch (opt_code (code))
23088 : : {
23089 : 42 : case OPT_fmodule_mapper_:
23090 : 42 : module_mapper_name = str;
23091 : 42 : return true;
23092 : :
23093 : 11 : case OPT_fmodule_header_:
23094 : 11 : {
23095 : 11 : if (!strcmp (str, "user"))
23096 : : hdr = CMS_user;
23097 : 11 : else if (!strcmp (str, "system"))
23098 : : hdr = CMS_system;
23099 : : else
23100 : 0 : error ("unknown header kind %qs", str);
23101 : : }
23102 : : /* Fallthrough. */
23103 : :
23104 : 894 : case OPT_fmodule_header:
23105 : 894 : flag_header_unit = hdr;
23106 : 894 : flag_modules = 1;
23107 : 894 : return true;
23108 : :
23109 : 1 : case OPT_flang_info_include_translate_:
23110 : 1 : vec_safe_push (note_includes, str);
23111 : 1 : return true;
23112 : :
23113 : 6 : case OPT_flang_info_module_cmi_:
23114 : 6 : vec_safe_push (note_cmis, str);
23115 : 6 : return true;
23116 : :
23117 : : default:
23118 : : return false;
23119 : : }
23120 : : }
23121 : :
23122 : : /* Set preprocessor callbacks and options for modules. */
23123 : :
23124 : : void
23125 : 96555 : module_preprocess_options (cpp_reader *reader)
23126 : : {
23127 : 96555 : gcc_checking_assert (!lang_hooks.preprocess_undef);
23128 : 96555 : if (modules_p ())
23129 : : {
23130 : 4489 : auto *cb = cpp_get_callbacks (reader);
23131 : :
23132 : 4489 : cb->translate_include = maybe_translate_include;
23133 : 4489 : cb->user_deferred_macro = module_state::deferred_macro;
23134 : 4489 : if (flag_header_unit)
23135 : : {
23136 : : /* If the preprocessor hook is already in use, that
23137 : : implementation will call the undef langhook. */
23138 : 891 : if (cb->undef)
23139 : 0 : lang_hooks.preprocess_undef = module_state::undef_macro;
23140 : : else
23141 : 891 : cb->undef = module_state::undef_macro;
23142 : : }
23143 : 4489 : auto *opt = cpp_get_options (reader);
23144 : 4489 : opt->module_directives = true;
23145 : 4489 : if (flag_no_output)
23146 : 12 : opt->directives_only = true;
23147 : 4489 : if (opt->main_search == CMS_none)
23148 : 4489 : opt->main_search = cpp_main_search (flag_header_unit);
23149 : : }
23150 : 96555 : }
23151 : :
23152 : : #include "gt-cp-module.h"
|